percpu-rwsem.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/rwsem.h>
  6. #include <linux/percpu.h>
  7. #include <linux/rcuwait.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 rw_semaphore rw_sem; /* slowpath */
  14. struct rcuwait writer; /* blocked writer */
  15. int readers_block;
  16. };
  17. #define DEFINE_STATIC_PERCPU_RWSEM(name) \
  18. static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \
  19. static struct percpu_rw_semaphore name = { \
  20. .rss = __RCU_SYNC_INITIALIZER(name.rss, RCU_SCHED_SYNC), \
  21. .read_count = &__percpu_rwsem_rc_##name, \
  22. .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \
  23. .writer = __RCUWAIT_INITIALIZER(name.writer), \
  24. }
  25. extern int __percpu_down_read(struct percpu_rw_semaphore *, int);
  26. extern void __percpu_up_read(struct percpu_rw_semaphore *);
  27. static inline void percpu_down_read_preempt_disable(struct percpu_rw_semaphore *sem)
  28. {
  29. might_sleep();
  30. rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 0, _RET_IP_);
  31. preempt_disable();
  32. /*
  33. * We are in an RCU-sched read-side critical section, so the writer
  34. * cannot both change sem->state from readers_fast and start checking
  35. * counters while we are here. So if we see !sem->state, we know that
  36. * the writer won't be checking until we're past the preempt_enable()
  37. * and that one the synchronize_sched() is done, the writer will see
  38. * anything we did within this RCU-sched read-size critical section.
  39. */
  40. __this_cpu_inc(*sem->read_count);
  41. if (unlikely(!rcu_sync_is_idle(&sem->rss)))
  42. __percpu_down_read(sem, false); /* Unconditional memory barrier */
  43. barrier();
  44. /*
  45. * The barrier() prevents the compiler from
  46. * bleeding the critical section out.
  47. */
  48. }
  49. static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
  50. {
  51. percpu_down_read_preempt_disable(sem);
  52. preempt_enable();
  53. }
  54. static inline int percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
  55. {
  56. int ret = 1;
  57. preempt_disable();
  58. /*
  59. * Same as in percpu_down_read().
  60. */
  61. __this_cpu_inc(*sem->read_count);
  62. if (unlikely(!rcu_sync_is_idle(&sem->rss)))
  63. ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */
  64. preempt_enable();
  65. /*
  66. * The barrier() from preempt_enable() prevents the compiler from
  67. * bleeding the critical section out.
  68. */
  69. if (ret)
  70. rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 1, _RET_IP_);
  71. return ret;
  72. }
  73. static inline void percpu_up_read_preempt_enable(struct percpu_rw_semaphore *sem)
  74. {
  75. /*
  76. * The barrier() prevents the compiler from
  77. * bleeding the critical section out.
  78. */
  79. barrier();
  80. /*
  81. * Same as in percpu_down_read().
  82. */
  83. if (likely(rcu_sync_is_idle(&sem->rss)))
  84. __this_cpu_dec(*sem->read_count);
  85. else
  86. __percpu_up_read(sem); /* Unconditional memory barrier */
  87. preempt_enable();
  88. rwsem_release(&sem->rw_sem.dep_map, 1, _RET_IP_);
  89. }
  90. static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
  91. {
  92. preempt_disable();
  93. percpu_up_read_preempt_enable(sem);
  94. }
  95. extern void percpu_down_write(struct percpu_rw_semaphore *);
  96. extern void percpu_up_write(struct percpu_rw_semaphore *);
  97. extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
  98. const char *, struct lock_class_key *);
  99. extern void percpu_free_rwsem(struct percpu_rw_semaphore *);
  100. #define percpu_init_rwsem(sem) \
  101. ({ \
  102. static struct lock_class_key rwsem_key; \
  103. __percpu_init_rwsem(sem, #sem, &rwsem_key); \
  104. })
  105. #define percpu_rwsem_is_held(sem) lockdep_is_held(&(sem)->rw_sem)
  106. #define percpu_rwsem_assert_held(sem) \
  107. lockdep_assert_held(&(sem)->rw_sem)
  108. static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
  109. bool read, unsigned long ip)
  110. {
  111. lock_release(&sem->rw_sem.dep_map, 1, ip);
  112. #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
  113. if (!read)
  114. sem->rw_sem.owner = RWSEM_OWNER_UNKNOWN;
  115. #endif
  116. }
  117. static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
  118. bool read, unsigned long ip)
  119. {
  120. lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);
  121. #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
  122. if (!read)
  123. sem->rw_sem.owner = current;
  124. #endif
  125. }
  126. #endif