percpu-rwsem.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_PERCPU_RWSEM_H
  3. #define _LINUX_PERCPU_RWSEM_H
  4. #include <linux/atomic.h>
  5. #include <linux/percpu.h>
  6. #include <linux/rcuwait.h>
  7. #include <linux/wait.h>
  8. #include <linux/rcu_sync.h>
  9. #include <linux/lockdep.h>
  10. void _trace_android_vh_record_pcpu_rwsem_starttime(
  11. struct task_struct *tsk, unsigned long settime);
  12. struct percpu_rw_semaphore {
  13. struct rcu_sync rss;
  14. unsigned int __percpu *read_count;
  15. struct rcuwait writer;
  16. wait_queue_head_t waiters;
  17. atomic_t block;
  18. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  19. struct lockdep_map dep_map;
  20. #endif
  21. };
  22. void _trace_android_vh_record_pcpu_rwsem_time_early(
  23. unsigned long settime, struct percpu_rw_semaphore *sem);
  24. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  25. #define __PERCPU_RWSEM_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname },
  26. #else
  27. #define __PERCPU_RWSEM_DEP_MAP_INIT(lockname)
  28. #endif
  29. #define __DEFINE_PERCPU_RWSEM(name, is_static) \
  30. static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \
  31. is_static struct percpu_rw_semaphore name = { \
  32. .rss = __RCU_SYNC_INITIALIZER(name.rss), \
  33. .read_count = &__percpu_rwsem_rc_##name, \
  34. .writer = __RCUWAIT_INITIALIZER(name.writer), \
  35. .waiters = __WAIT_QUEUE_HEAD_INITIALIZER(name.waiters), \
  36. .block = ATOMIC_INIT(0), \
  37. __PERCPU_RWSEM_DEP_MAP_INIT(name) \
  38. }
  39. #define DEFINE_PERCPU_RWSEM(name) \
  40. __DEFINE_PERCPU_RWSEM(name, /* not static */)
  41. #define DEFINE_STATIC_PERCPU_RWSEM(name) \
  42. __DEFINE_PERCPU_RWSEM(name, static)
  43. extern bool __percpu_down_read(struct percpu_rw_semaphore *, bool);
  44. static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
  45. {
  46. might_sleep();
  47. rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
  48. preempt_disable();
  49. _trace_android_vh_record_pcpu_rwsem_time_early(jiffies, sem);
  50. /*
  51. * We are in an RCU-sched read-side critical section, so the writer
  52. * cannot both change sem->state from readers_fast and start checking
  53. * counters while we are here. So if we see !sem->state, we know that
  54. * the writer won't be checking until we're past the preempt_enable()
  55. * and that once the synchronize_rcu() is done, the writer will see
  56. * anything we did within this RCU-sched read-size critical section.
  57. */
  58. if (likely(rcu_sync_is_idle(&sem->rss)))
  59. this_cpu_inc(*sem->read_count);
  60. else
  61. __percpu_down_read(sem, false); /* Unconditional memory barrier */
  62. /*
  63. * The preempt_enable() prevents the compiler from
  64. * bleeding the critical section out.
  65. */
  66. preempt_enable();
  67. _trace_android_vh_record_pcpu_rwsem_starttime(current, jiffies);
  68. }
  69. static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
  70. {
  71. bool ret = true;
  72. preempt_disable();
  73. /*
  74. * Same as in percpu_down_read().
  75. */
  76. if (likely(rcu_sync_is_idle(&sem->rss)))
  77. this_cpu_inc(*sem->read_count);
  78. else
  79. ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */
  80. preempt_enable();
  81. /*
  82. * The barrier() from preempt_enable() prevents the compiler from
  83. * bleeding the critical section out.
  84. */
  85. if (ret) {
  86. _trace_android_vh_record_pcpu_rwsem_time_early(jiffies, sem);
  87. _trace_android_vh_record_pcpu_rwsem_starttime(current, jiffies);
  88. rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
  89. }
  90. return ret;
  91. }
  92. static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
  93. {
  94. rwsem_release(&sem->dep_map, _RET_IP_);
  95. preempt_disable();
  96. /*
  97. * Same as in percpu_down_read().
  98. */
  99. if (likely(rcu_sync_is_idle(&sem->rss))) {
  100. this_cpu_dec(*sem->read_count);
  101. } else {
  102. /*
  103. * slowpath; reader will only ever wake a single blocked
  104. * writer.
  105. */
  106. smp_mb(); /* B matches C */
  107. /*
  108. * In other words, if they see our decrement (presumably to
  109. * aggregate zero, as that is the only time it matters) they
  110. * will also see our critical section.
  111. */
  112. this_cpu_dec(*sem->read_count);
  113. rcuwait_wake_up(&sem->writer);
  114. }
  115. _trace_android_vh_record_pcpu_rwsem_time_early(0, sem);
  116. _trace_android_vh_record_pcpu_rwsem_starttime(current, 0);
  117. preempt_enable();
  118. }
  119. extern bool percpu_is_read_locked(struct percpu_rw_semaphore *);
  120. extern void percpu_down_write(struct percpu_rw_semaphore *);
  121. extern void percpu_up_write(struct percpu_rw_semaphore *);
  122. static inline bool percpu_is_write_locked(struct percpu_rw_semaphore *sem)
  123. {
  124. return atomic_read(&sem->block);
  125. }
  126. extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
  127. const char *, struct lock_class_key *);
  128. extern void percpu_free_rwsem(struct percpu_rw_semaphore *);
  129. #define percpu_init_rwsem(sem) \
  130. ({ \
  131. static struct lock_class_key rwsem_key; \
  132. __percpu_init_rwsem(sem, #sem, &rwsem_key); \
  133. })
  134. #define percpu_rwsem_is_held(sem) lockdep_is_held(sem)
  135. #define percpu_rwsem_assert_held(sem) lockdep_assert_held(sem)
  136. static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
  137. bool read, unsigned long ip)
  138. {
  139. lock_release(&sem->dep_map, ip);
  140. }
  141. static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
  142. bool read, unsigned long ip)
  143. {
  144. lock_acquire(&sem->dep_map, 0, 1, read, 1, NULL, ip);
  145. }
  146. #endif