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