percpu-rwsem.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. struct percpu_rw_semaphore {
  11. struct rcu_sync rss;
  12. unsigned int __percpu *read_count;
  13. struct rcuwait writer;
  14. wait_queue_head_t waiters;
  15. atomic_t block;
  16. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  17. struct lockdep_map dep_map;
  18. #endif
  19. };
  20. struct percpu_rw_semaphore_atomic {
  21. struct percpu_rw_semaphore rw_sem;
  22. struct list_head destroy_list_entry;
  23. };
  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. /*
  50. * We are in an RCU-sched read-side critical section, so the writer
  51. * cannot both change sem->state from readers_fast and start checking
  52. * counters while we are here. So if we see !sem->state, we know that
  53. * the writer won't be checking until we're past the preempt_enable()
  54. * and that once the synchronize_rcu() is done, the writer will see
  55. * anything we did within this RCU-sched read-size critical section.
  56. */
  57. if (likely(rcu_sync_is_idle(&sem->rss)))
  58. this_cpu_inc(*sem->read_count);
  59. else
  60. __percpu_down_read(sem, false); /* Unconditional memory barrier */
  61. /*
  62. * The preempt_enable() prevents the compiler from
  63. * bleeding the critical section out.
  64. */
  65. preempt_enable();
  66. }
  67. static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
  68. {
  69. bool ret = true;
  70. preempt_disable();
  71. /*
  72. * Same as in percpu_down_read().
  73. */
  74. if (likely(rcu_sync_is_idle(&sem->rss)))
  75. this_cpu_inc(*sem->read_count);
  76. else
  77. ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */
  78. preempt_enable();
  79. /*
  80. * The barrier() from preempt_enable() prevents the compiler from
  81. * bleeding the critical section out.
  82. */
  83. if (ret)
  84. rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
  85. return ret;
  86. }
  87. static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
  88. {
  89. rwsem_release(&sem->dep_map, _RET_IP_);
  90. preempt_disable();
  91. /*
  92. * Same as in percpu_down_read().
  93. */
  94. if (likely(rcu_sync_is_idle(&sem->rss))) {
  95. this_cpu_dec(*sem->read_count);
  96. } else {
  97. /*
  98. * slowpath; reader will only ever wake a single blocked
  99. * writer.
  100. */
  101. smp_mb(); /* B matches C */
  102. /*
  103. * In other words, if they see our decrement (presumably to
  104. * aggregate zero, as that is the only time it matters) they
  105. * will also see our critical section.
  106. */
  107. this_cpu_dec(*sem->read_count);
  108. rcuwait_wake_up(&sem->writer);
  109. }
  110. preempt_enable();
  111. }
  112. extern void percpu_down_write(struct percpu_rw_semaphore *);
  113. extern void percpu_up_write(struct percpu_rw_semaphore *);
  114. extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
  115. const char *, struct lock_class_key *);
  116. /* Can't be called in atomic context. */
  117. extern void percpu_free_rwsem(struct percpu_rw_semaphore *);
  118. /* Invokes percpu_free_rwsem and frees the semaphore from a worker thread. */
  119. extern void percpu_rwsem_async_destroy(struct percpu_rw_semaphore_atomic *sem);
  120. #define percpu_init_rwsem(sem) \
  121. ({ \
  122. static struct lock_class_key rwsem_key; \
  123. __percpu_init_rwsem(sem, #sem, &rwsem_key); \
  124. })
  125. #define percpu_rwsem_is_held(sem) lockdep_is_held(sem)
  126. #define percpu_rwsem_assert_held(sem) lockdep_assert_held(sem)
  127. static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
  128. bool read, unsigned long ip)
  129. {
  130. lock_release(&sem->dep_map, ip);
  131. }
  132. static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
  133. bool read, unsigned long ip)
  134. {
  135. lock_acquire(&sem->dep_map, 0, 1, read, 1, NULL, ip);
  136. }
  137. #endif