1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/atomic.h>
3 #include <linux/percpu.h>
4 #include <linux/wait.h>
5 #include <linux/lockdep.h>
6 #include <linux/percpu-rwsem.h>
7 #include <linux/rcupdate.h>
8 #include <linux/sched.h>
9 #include <linux/sched/task.h>
10 #include <linux/sched/debug.h>
11 #include <linux/errno.h>
13 int __percpu_init_rwsem(struct percpu_rw_semaphore *sem,
14 const char *name, struct lock_class_key *key)
16 sem->read_count = alloc_percpu(int);
17 if (unlikely(!sem->read_count))
20 rcu_sync_init(&sem->rss);
21 rcuwait_init(&sem->writer);
22 init_waitqueue_head(&sem->waiters);
23 atomic_set(&sem->block, 0);
24 #ifdef CONFIG_DEBUG_LOCK_ALLOC
25 debug_check_no_locks_freed((void *)sem, sizeof(*sem));
26 lockdep_init_map(&sem->dep_map, name, key, 0);
30 EXPORT_SYMBOL_GPL(__percpu_init_rwsem);
32 void percpu_free_rwsem(struct percpu_rw_semaphore *sem)
35 * XXX: temporary kludge. The error path in alloc_super()
36 * assumes that percpu_free_rwsem() is safe after kzalloc().
41 rcu_sync_dtor(&sem->rss);
42 free_percpu(sem->read_count);
43 sem->read_count = NULL; /* catch use after free bugs */
45 EXPORT_SYMBOL_GPL(percpu_free_rwsem);
47 static bool __percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
49 this_cpu_inc(*sem->read_count);
52 * Due to having preemption disabled the decrement happens on
53 * the same CPU as the increment, avoiding the
54 * increment-on-one-CPU-and-decrement-on-another problem.
56 * If the reader misses the writer's assignment of sem->block, then the
57 * writer is guaranteed to see the reader's increment.
59 * Conversely, any readers that increment their sem->read_count after
60 * the writer looks are guaranteed to see the sem->block value, which
61 * in turn means that they are guaranteed to immediately decrement
62 * their sem->read_count, so that it doesn't matter that the writer
66 smp_mb(); /* A matches D */
69 * If !sem->block the critical section starts here, matched by the
70 * release in percpu_up_write().
72 if (likely(!atomic_read_acquire(&sem->block)))
75 this_cpu_dec(*sem->read_count);
77 /* Prod writer to re-evaluate readers_active_check() */
78 rcuwait_wake_up(&sem->writer);
83 static inline bool __percpu_down_write_trylock(struct percpu_rw_semaphore *sem)
85 if (atomic_read(&sem->block))
88 return atomic_xchg(&sem->block, 1) == 0;
91 static bool __percpu_rwsem_trylock(struct percpu_rw_semaphore *sem, bool reader)
97 ret = __percpu_down_read_trylock(sem);
102 return __percpu_down_write_trylock(sem);
106 * The return value of wait_queue_entry::func means:
108 * <0 - error, wakeup is terminated and the error is returned
109 * 0 - no wakeup, a next waiter is tried
110 * >0 - woken, if EXCLUSIVE, counted towards @nr_exclusive.
112 * We use EXCLUSIVE for both readers and writers to preserve FIFO order,
113 * and play games with the return value to allow waking multiple readers.
115 * Specifically, we wake readers until we've woken a single writer, or until a
118 static int percpu_rwsem_wake_function(struct wait_queue_entry *wq_entry,
119 unsigned int mode, int wake_flags,
122 bool reader = wq_entry->flags & WQ_FLAG_CUSTOM;
123 struct percpu_rw_semaphore *sem = key;
124 struct task_struct *p;
126 /* concurrent against percpu_down_write(), can get stolen */
127 if (!__percpu_rwsem_trylock(sem, reader))
130 p = get_task_struct(wq_entry->private);
131 list_del_init(&wq_entry->entry);
132 smp_store_release(&wq_entry->private, NULL);
137 return !reader; /* wake (readers until) 1 writer */
140 static void percpu_rwsem_wait(struct percpu_rw_semaphore *sem, bool reader)
142 DEFINE_WAIT_FUNC(wq_entry, percpu_rwsem_wake_function);
145 spin_lock_irq(&sem->waiters.lock);
147 * Serialize against the wakeup in percpu_up_write(), if we fail
148 * the trylock, the wakeup must see us on the list.
150 wait = !__percpu_rwsem_trylock(sem, reader);
152 wq_entry.flags |= WQ_FLAG_EXCLUSIVE | reader * WQ_FLAG_CUSTOM;
153 __add_wait_queue_entry_tail(&sem->waiters, &wq_entry);
155 spin_unlock_irq(&sem->waiters.lock);
158 set_current_state(TASK_UNINTERRUPTIBLE);
159 if (!smp_load_acquire(&wq_entry.private))
163 __set_current_state(TASK_RUNNING);
166 bool __sched __percpu_down_read(struct percpu_rw_semaphore *sem, bool try)
168 if (__percpu_down_read_trylock(sem))
175 percpu_rwsem_wait(sem, /* .reader = */ true);
180 EXPORT_SYMBOL_GPL(__percpu_down_read);
182 #define per_cpu_sum(var) \
184 typeof(var) __sum = 0; \
186 compiletime_assert_atomic_type(__sum); \
187 for_each_possible_cpu(cpu) \
188 __sum += per_cpu(var, cpu); \
193 * Return true if the modular sum of the sem->read_count per-CPU variable is
194 * zero. If this sum is zero, then it is stable due to the fact that if any
195 * newly arriving readers increment a given counter, they will immediately
196 * decrement that same counter.
198 * Assumes sem->block is set.
200 static bool readers_active_check(struct percpu_rw_semaphore *sem)
202 if (per_cpu_sum(*sem->read_count) != 0)
206 * If we observed the decrement; ensure we see the entire critical
210 smp_mb(); /* C matches B */
215 void __sched percpu_down_write(struct percpu_rw_semaphore *sem)
218 rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
220 /* Notify readers to take the slow path. */
221 rcu_sync_enter(&sem->rss);
224 * Try set sem->block; this provides writer-writer exclusion.
225 * Having sem->block set makes new readers block.
227 if (!__percpu_down_write_trylock(sem))
228 percpu_rwsem_wait(sem, /* .reader = */ false);
230 /* smp_mb() implied by __percpu_down_write_trylock() on success -- D matches A */
233 * If they don't see our store of sem->block, then we are guaranteed to
234 * see their sem->read_count increment, and therefore will wait for
238 /* Wait for all active readers to complete. */
239 rcuwait_wait_event(&sem->writer, readers_active_check(sem), TASK_UNINTERRUPTIBLE);
241 EXPORT_SYMBOL_GPL(percpu_down_write);
243 void percpu_up_write(struct percpu_rw_semaphore *sem)
245 rwsem_release(&sem->dep_map, _RET_IP_);
248 * Signal the writer is done, no fast path yet.
250 * One reason that we cannot just immediately flip to readers_fast is
251 * that new readers might fail to see the results of this writer's
254 * Therefore we force it through the slow path which guarantees an
255 * acquire and thereby guarantees the critical section's consistency.
257 atomic_set_release(&sem->block, 0);
260 * Prod any pending reader/writer to make progress.
262 __wake_up(&sem->waiters, TASK_NORMAL, 1, sem);
265 * Once this completes (at least one RCU-sched grace period hence) the
266 * reader fast path will be available again. Safe to use outside the
267 * exclusive write lock because its counting.
269 rcu_sync_exit(&sem->rss);
271 EXPORT_SYMBOL_GPL(percpu_up_write);