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