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