1 #ifndef _LINUX_PERCPU_RWSEM_H
2 #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>
11 struct percpu_rw_semaphore {
13 unsigned int __percpu *read_count;
14 struct rw_semaphore rw_sem; /* slowpath */
15 struct rcuwait writer; /* blocked writer */
19 #define DEFINE_STATIC_PERCPU_RWSEM(name) \
20 static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \
21 static struct percpu_rw_semaphore name = { \
22 .rss = __RCU_SYNC_INITIALIZER(name.rss, RCU_SCHED_SYNC), \
23 .read_count = &__percpu_rwsem_rc_##name, \
24 .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \
25 .writer = __RCUWAIT_INITIALIZER(name.writer), \
28 extern int __percpu_down_read(struct percpu_rw_semaphore *, int);
29 extern void __percpu_up_read(struct percpu_rw_semaphore *);
31 static inline void percpu_down_read_preempt_disable(struct percpu_rw_semaphore *sem)
35 rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 0, _RET_IP_);
39 * We are in an RCU-sched read-side critical section, so the writer
40 * cannot both change sem->state from readers_fast and start checking
41 * counters while we are here. So if we see !sem->state, we know that
42 * the writer won't be checking until we're past the preempt_enable()
43 * and that one the synchronize_sched() is done, the writer will see
44 * anything we did within this RCU-sched read-size critical section.
46 __this_cpu_inc(*sem->read_count);
47 if (unlikely(!rcu_sync_is_idle(&sem->rss)))
48 __percpu_down_read(sem, false); /* Unconditional memory barrier */
51 * The barrier() prevents the compiler from
52 * bleeding the critical section out.
56 static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
58 percpu_down_read_preempt_disable(sem);
62 static inline int percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
68 * Same as in percpu_down_read().
70 __this_cpu_inc(*sem->read_count);
71 if (unlikely(!rcu_sync_is_idle(&sem->rss)))
72 ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */
75 * The barrier() from preempt_enable() prevents the compiler from
76 * bleeding the critical section out.
80 rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 1, _RET_IP_);
85 static inline void percpu_up_read_preempt_enable(struct percpu_rw_semaphore *sem)
88 * The barrier() prevents the compiler from
89 * bleeding the critical section out.
93 * Same as in percpu_down_read().
95 if (likely(rcu_sync_is_idle(&sem->rss)))
96 __this_cpu_dec(*sem->read_count);
98 __percpu_up_read(sem); /* Unconditional memory barrier */
101 rwsem_release(&sem->rw_sem.dep_map, 1, _RET_IP_);
104 static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
107 percpu_up_read_preempt_enable(sem);
110 extern void percpu_down_write(struct percpu_rw_semaphore *);
111 extern void percpu_up_write(struct percpu_rw_semaphore *);
113 extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
114 const char *, struct lock_class_key *);
116 extern void percpu_free_rwsem(struct percpu_rw_semaphore *);
118 #define percpu_init_rwsem(sem) \
120 static struct lock_class_key rwsem_key; \
121 __percpu_init_rwsem(sem, #sem, &rwsem_key); \
124 #define percpu_rwsem_is_held(sem) lockdep_is_held(&(sem)->rw_sem)
126 #define percpu_rwsem_assert_held(sem) \
127 lockdep_assert_held(&(sem)->rw_sem)
129 static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
130 bool read, unsigned long ip)
132 lock_release(&sem->rw_sem.dep_map, 1, ip);
133 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
135 sem->rw_sem.owner = NULL;
139 static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
140 bool read, unsigned long ip)
142 lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);