percpu-rwsem.h 3.9 KB

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