xref: /linux/kernel/locking/percpu-rwsem.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
232cf7c3cSPeter Zijlstra #include <linux/atomic.h>
332cf7c3cSPeter Zijlstra #include <linux/percpu.h>
47f26482aSPeter Zijlstra #include <linux/wait.h>
532cf7c3cSPeter Zijlstra #include <linux/lockdep.h>
632cf7c3cSPeter Zijlstra #include <linux/percpu-rwsem.h>
732cf7c3cSPeter Zijlstra #include <linux/rcupdate.h>
832cf7c3cSPeter Zijlstra #include <linux/sched.h>
97f26482aSPeter Zijlstra #include <linux/sched/task.h>
10c441e934SMinchan Kim #include <linux/sched/debug.h>
1132cf7c3cSPeter Zijlstra #include <linux/errno.h>
12ee042be1SNamhyung Kim #include <trace/events/lock.h>
1332cf7c3cSPeter Zijlstra 
__percpu_init_rwsem(struct percpu_rw_semaphore * sem,const char * name,struct lock_class_key * key)1480127a39SPeter Zijlstra int __percpu_init_rwsem(struct percpu_rw_semaphore *sem,
151751060eSPeter Zijlstra 			const char *name, struct lock_class_key *key)
1632cf7c3cSPeter Zijlstra {
1780127a39SPeter Zijlstra 	sem->read_count = alloc_percpu(int);
1880127a39SPeter Zijlstra 	if (unlikely(!sem->read_count))
1932cf7c3cSPeter Zijlstra 		return -ENOMEM;
2032cf7c3cSPeter Zijlstra 
2195bf33b5SOleg Nesterov 	rcu_sync_init(&sem->rss);
2252b94129SDavidlohr Bueso 	rcuwait_init(&sem->writer);
237f26482aSPeter Zijlstra 	init_waitqueue_head(&sem->waiters);
247f26482aSPeter Zijlstra 	atomic_set(&sem->block, 0);
251751060eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
261751060eSPeter Zijlstra 	debug_check_no_locks_freed((void *)sem, sizeof(*sem));
271751060eSPeter Zijlstra 	lockdep_init_map(&sem->dep_map, name, key, 0);
281751060eSPeter Zijlstra #endif
2932cf7c3cSPeter Zijlstra 	return 0;
3032cf7c3cSPeter Zijlstra }
31302707fdSPaul E. McKenney EXPORT_SYMBOL_GPL(__percpu_init_rwsem);
3232cf7c3cSPeter Zijlstra 
percpu_free_rwsem(struct percpu_rw_semaphore * sem)3380127a39SPeter Zijlstra void percpu_free_rwsem(struct percpu_rw_semaphore *sem)
3432cf7c3cSPeter Zijlstra {
3595b19f68SOleg Nesterov 	/*
3695b19f68SOleg Nesterov 	 * XXX: temporary kludge. The error path in alloc_super()
3795b19f68SOleg Nesterov 	 * assumes that percpu_free_rwsem() is safe after kzalloc().
3895b19f68SOleg Nesterov 	 */
3980127a39SPeter Zijlstra 	if (!sem->read_count)
4095b19f68SOleg Nesterov 		return;
4195b19f68SOleg Nesterov 
4280127a39SPeter Zijlstra 	rcu_sync_dtor(&sem->rss);
4380127a39SPeter Zijlstra 	free_percpu(sem->read_count);
4480127a39SPeter Zijlstra 	sem->read_count = NULL; /* catch use after free bugs */
4532cf7c3cSPeter Zijlstra }
46c8585c6fSDaeho Jeong EXPORT_SYMBOL_GPL(percpu_free_rwsem);
4732cf7c3cSPeter Zijlstra 
__percpu_down_read_trylock(struct percpu_rw_semaphore * sem)4875ff6457SPeter Zijlstra static bool __percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
4932cf7c3cSPeter Zijlstra {
50e6b1a44eSHou Tao 	this_cpu_inc(*sem->read_count);
5171365d40SPeter Zijlstra 
5280127a39SPeter Zijlstra 	/*
5380127a39SPeter Zijlstra 	 * Due to having preemption disabled the decrement happens on
5480127a39SPeter Zijlstra 	 * the same CPU as the increment, avoiding the
5580127a39SPeter Zijlstra 	 * increment-on-one-CPU-and-decrement-on-another problem.
5680127a39SPeter Zijlstra 	 *
577f26482aSPeter Zijlstra 	 * If the reader misses the writer's assignment of sem->block, then the
587f26482aSPeter Zijlstra 	 * writer is guaranteed to see the reader's increment.
5980127a39SPeter Zijlstra 	 *
6080127a39SPeter Zijlstra 	 * Conversely, any readers that increment their sem->read_count after
617f26482aSPeter Zijlstra 	 * the writer looks are guaranteed to see the sem->block value, which
627f26482aSPeter Zijlstra 	 * in turn means that they are guaranteed to immediately decrement
637f26482aSPeter Zijlstra 	 * their sem->read_count, so that it doesn't matter that the writer
647f26482aSPeter Zijlstra 	 * missed them.
6580127a39SPeter Zijlstra 	 */
6680127a39SPeter Zijlstra 
6780127a39SPeter Zijlstra 	smp_mb(); /* A matches D */
6880127a39SPeter Zijlstra 
6980127a39SPeter Zijlstra 	/*
707f26482aSPeter Zijlstra 	 * If !sem->block the critical section starts here, matched by the
7180127a39SPeter Zijlstra 	 * release in percpu_up_write().
7280127a39SPeter Zijlstra 	 */
737f26482aSPeter Zijlstra 	if (likely(!atomic_read_acquire(&sem->block)))
74206c98ffSPeter Zijlstra 		return true;
7580127a39SPeter Zijlstra 
76e6b1a44eSHou Tao 	this_cpu_dec(*sem->read_count);
7775ff6457SPeter Zijlstra 
7875ff6457SPeter Zijlstra 	/* Prod writer to re-evaluate readers_active_check() */
7975ff6457SPeter Zijlstra 	rcuwait_wake_up(&sem->writer);
8075ff6457SPeter Zijlstra 
8175ff6457SPeter Zijlstra 	return false;
8275ff6457SPeter Zijlstra }
8375ff6457SPeter Zijlstra 
__percpu_down_write_trylock(struct percpu_rw_semaphore * sem)847f26482aSPeter Zijlstra static inline bool __percpu_down_write_trylock(struct percpu_rw_semaphore *sem)
857f26482aSPeter Zijlstra {
867f26482aSPeter Zijlstra 	if (atomic_read(&sem->block))
877f26482aSPeter Zijlstra 		return false;
887f26482aSPeter Zijlstra 
897f26482aSPeter Zijlstra 	return atomic_xchg(&sem->block, 1) == 0;
907f26482aSPeter Zijlstra }
917f26482aSPeter Zijlstra 
__percpu_rwsem_trylock(struct percpu_rw_semaphore * sem,bool reader)927f26482aSPeter Zijlstra static bool __percpu_rwsem_trylock(struct percpu_rw_semaphore *sem, bool reader)
937f26482aSPeter Zijlstra {
947f26482aSPeter Zijlstra 	if (reader) {
957f26482aSPeter Zijlstra 		bool ret;
967f26482aSPeter Zijlstra 
977f26482aSPeter Zijlstra 		preempt_disable();
987f26482aSPeter Zijlstra 		ret = __percpu_down_read_trylock(sem);
997f26482aSPeter Zijlstra 		preempt_enable();
1007f26482aSPeter Zijlstra 
1017f26482aSPeter Zijlstra 		return ret;
1027f26482aSPeter Zijlstra 	}
1037f26482aSPeter Zijlstra 	return __percpu_down_write_trylock(sem);
1047f26482aSPeter Zijlstra }
1057f26482aSPeter Zijlstra 
1067f26482aSPeter Zijlstra /*
1077f26482aSPeter Zijlstra  * The return value of wait_queue_entry::func means:
1087f26482aSPeter Zijlstra  *
1097f26482aSPeter Zijlstra  *  <0 - error, wakeup is terminated and the error is returned
1107f26482aSPeter Zijlstra  *   0 - no wakeup, a next waiter is tried
1117f26482aSPeter Zijlstra  *  >0 - woken, if EXCLUSIVE, counted towards @nr_exclusive.
1127f26482aSPeter Zijlstra  *
1137f26482aSPeter Zijlstra  * We use EXCLUSIVE for both readers and writers to preserve FIFO order,
1147f26482aSPeter Zijlstra  * and play games with the return value to allow waking multiple readers.
1157f26482aSPeter Zijlstra  *
1167f26482aSPeter Zijlstra  * Specifically, we wake readers until we've woken a single writer, or until a
1177f26482aSPeter Zijlstra  * trylock fails.
1187f26482aSPeter Zijlstra  */
percpu_rwsem_wake_function(struct wait_queue_entry * wq_entry,unsigned int mode,int wake_flags,void * key)1197f26482aSPeter Zijlstra static int percpu_rwsem_wake_function(struct wait_queue_entry *wq_entry,
1207f26482aSPeter Zijlstra 				      unsigned int mode, int wake_flags,
1217f26482aSPeter Zijlstra 				      void *key)
1227f26482aSPeter Zijlstra {
1237f26482aSPeter Zijlstra 	bool reader = wq_entry->flags & WQ_FLAG_CUSTOM;
1247f26482aSPeter Zijlstra 	struct percpu_rw_semaphore *sem = key;
125d22cc7f6SQian Cai 	struct task_struct *p;
1267f26482aSPeter Zijlstra 
1277f26482aSPeter Zijlstra 	/* concurrent against percpu_down_write(), can get stolen */
1287f26482aSPeter Zijlstra 	if (!__percpu_rwsem_trylock(sem, reader))
1297f26482aSPeter Zijlstra 		return 1;
1307f26482aSPeter Zijlstra 
131d22cc7f6SQian Cai 	p = get_task_struct(wq_entry->private);
1327f26482aSPeter Zijlstra 	list_del_init(&wq_entry->entry);
1337f26482aSPeter Zijlstra 	smp_store_release(&wq_entry->private, NULL);
1347f26482aSPeter Zijlstra 
1357f26482aSPeter Zijlstra 	wake_up_process(p);
1367f26482aSPeter Zijlstra 	put_task_struct(p);
1377f26482aSPeter Zijlstra 
1387f26482aSPeter Zijlstra 	return !reader; /* wake (readers until) 1 writer */
1397f26482aSPeter Zijlstra }
1407f26482aSPeter Zijlstra 
percpu_rwsem_wait(struct percpu_rw_semaphore * sem,bool reader)1417f26482aSPeter Zijlstra static void percpu_rwsem_wait(struct percpu_rw_semaphore *sem, bool reader)
1427f26482aSPeter Zijlstra {
1437f26482aSPeter Zijlstra 	DEFINE_WAIT_FUNC(wq_entry, percpu_rwsem_wake_function);
1447f26482aSPeter Zijlstra 	bool wait;
1457f26482aSPeter Zijlstra 
1467f26482aSPeter Zijlstra 	spin_lock_irq(&sem->waiters.lock);
1477f26482aSPeter Zijlstra 	/*
1487f26482aSPeter Zijlstra 	 * Serialize against the wakeup in percpu_up_write(), if we fail
1497f26482aSPeter Zijlstra 	 * the trylock, the wakeup must see us on the list.
1507f26482aSPeter Zijlstra 	 */
1517f26482aSPeter Zijlstra 	wait = !__percpu_rwsem_trylock(sem, reader);
1527f26482aSPeter Zijlstra 	if (wait) {
1537f26482aSPeter Zijlstra 		wq_entry.flags |= WQ_FLAG_EXCLUSIVE | reader * WQ_FLAG_CUSTOM;
1547f26482aSPeter Zijlstra 		__add_wait_queue_entry_tail(&sem->waiters, &wq_entry);
1557f26482aSPeter Zijlstra 	}
1567f26482aSPeter Zijlstra 	spin_unlock_irq(&sem->waiters.lock);
1577f26482aSPeter Zijlstra 
1587f26482aSPeter Zijlstra 	while (wait) {
1597f26482aSPeter Zijlstra 		set_current_state(TASK_UNINTERRUPTIBLE);
1607f26482aSPeter Zijlstra 		if (!smp_load_acquire(&wq_entry.private))
1617f26482aSPeter Zijlstra 			break;
1627f26482aSPeter Zijlstra 		schedule();
1637f26482aSPeter Zijlstra 	}
1647f26482aSPeter Zijlstra 	__set_current_state(TASK_RUNNING);
1657f26482aSPeter Zijlstra }
1667f26482aSPeter Zijlstra 
__percpu_down_read(struct percpu_rw_semaphore * sem,bool try)167c441e934SMinchan Kim bool __sched __percpu_down_read(struct percpu_rw_semaphore *sem, bool try)
16875ff6457SPeter Zijlstra {
16975ff6457SPeter Zijlstra 	if (__percpu_down_read_trylock(sem))
17075ff6457SPeter Zijlstra 		return true;
17180127a39SPeter Zijlstra 
17280127a39SPeter Zijlstra 	if (try)
173206c98ffSPeter Zijlstra 		return false;
17480127a39SPeter Zijlstra 
175ee042be1SNamhyung Kim 	trace_contention_begin(sem, LCB_F_PERCPU | LCB_F_READ);
1767f26482aSPeter Zijlstra 	preempt_enable();
1777f26482aSPeter Zijlstra 	percpu_rwsem_wait(sem, /* .reader = */ true);
17832cf7c3cSPeter Zijlstra 	preempt_disable();
179ee042be1SNamhyung Kim 	trace_contention_end(sem, 0);
1807f26482aSPeter Zijlstra 
181206c98ffSPeter Zijlstra 	return true;
1829287f692SOleg Nesterov }
18380127a39SPeter Zijlstra EXPORT_SYMBOL_GPL(__percpu_down_read);
1849287f692SOleg Nesterov 
18580127a39SPeter Zijlstra #define per_cpu_sum(var)						\
18680127a39SPeter Zijlstra ({									\
18780127a39SPeter Zijlstra 	typeof(var) __sum = 0;						\
18880127a39SPeter Zijlstra 	int cpu;							\
18980127a39SPeter Zijlstra 	compiletime_assert_atomic_type(__sum);				\
19080127a39SPeter Zijlstra 	for_each_possible_cpu(cpu)					\
19180127a39SPeter Zijlstra 		__sum += per_cpu(var, cpu);				\
19280127a39SPeter Zijlstra 	__sum;								\
19380127a39SPeter Zijlstra })
19432cf7c3cSPeter Zijlstra 
percpu_is_read_locked(struct percpu_rw_semaphore * sem)19501fe8a3fSMarco Elver bool percpu_is_read_locked(struct percpu_rw_semaphore *sem)
19601fe8a3fSMarco Elver {
19701fe8a3fSMarco Elver 	return per_cpu_sum(*sem->read_count) != 0 && !atomic_read(&sem->block);
19801fe8a3fSMarco Elver }
19901fe8a3fSMarco Elver EXPORT_SYMBOL_GPL(percpu_is_read_locked);
20001fe8a3fSMarco Elver 
20180127a39SPeter Zijlstra /*
20280127a39SPeter Zijlstra  * Return true if the modular sum of the sem->read_count per-CPU variable is
20380127a39SPeter Zijlstra  * zero.  If this sum is zero, then it is stable due to the fact that if any
20480127a39SPeter Zijlstra  * newly arriving readers increment a given counter, they will immediately
20580127a39SPeter Zijlstra  * decrement that same counter.
2067f26482aSPeter Zijlstra  *
2077f26482aSPeter Zijlstra  * Assumes sem->block is set.
20880127a39SPeter Zijlstra  */
readers_active_check(struct percpu_rw_semaphore * sem)20980127a39SPeter Zijlstra static bool readers_active_check(struct percpu_rw_semaphore *sem)
21080127a39SPeter Zijlstra {
21180127a39SPeter Zijlstra 	if (per_cpu_sum(*sem->read_count) != 0)
21280127a39SPeter Zijlstra 		return false;
21380127a39SPeter Zijlstra 
21480127a39SPeter Zijlstra 	/*
21580127a39SPeter Zijlstra 	 * If we observed the decrement; ensure we see the entire critical
21680127a39SPeter Zijlstra 	 * section.
21780127a39SPeter Zijlstra 	 */
21880127a39SPeter Zijlstra 
21980127a39SPeter Zijlstra 	smp_mb(); /* C matches B */
22080127a39SPeter Zijlstra 
22180127a39SPeter Zijlstra 	return true;
22280127a39SPeter Zijlstra }
22380127a39SPeter Zijlstra 
percpu_down_write(struct percpu_rw_semaphore * sem)224c441e934SMinchan Kim void __sched percpu_down_write(struct percpu_rw_semaphore *sem)
22580127a39SPeter Zijlstra {
226*f3e3620fSNamhyung Kim 	bool contended = false;
227*f3e3620fSNamhyung Kim 
22841f0e291SDavidlohr Bueso 	might_sleep();
2291751060eSPeter Zijlstra 	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);
2301751060eSPeter Zijlstra 
23180127a39SPeter Zijlstra 	/* Notify readers to take the slow path. */
23280127a39SPeter Zijlstra 	rcu_sync_enter(&sem->rss);
23380127a39SPeter Zijlstra 
2347f26482aSPeter Zijlstra 	/*
2357f26482aSPeter Zijlstra 	 * Try set sem->block; this provides writer-writer exclusion.
2367f26482aSPeter Zijlstra 	 * Having sem->block set makes new readers block.
2377f26482aSPeter Zijlstra 	 */
238*f3e3620fSNamhyung Kim 	if (!__percpu_down_write_trylock(sem)) {
239*f3e3620fSNamhyung Kim 		trace_contention_begin(sem, LCB_F_PERCPU | LCB_F_WRITE);
2407f26482aSPeter Zijlstra 		percpu_rwsem_wait(sem, /* .reader = */ false);
241*f3e3620fSNamhyung Kim 		contended = true;
242*f3e3620fSNamhyung Kim 	}
2437f26482aSPeter Zijlstra 
2447f26482aSPeter Zijlstra 	/* smp_mb() implied by __percpu_down_write_trylock() on success -- D matches A */
24580127a39SPeter Zijlstra 
24680127a39SPeter Zijlstra 	/*
2477f26482aSPeter Zijlstra 	 * If they don't see our store of sem->block, then we are guaranteed to
2487f26482aSPeter Zijlstra 	 * see their sem->read_count increment, and therefore will wait for
2497f26482aSPeter Zijlstra 	 * them.
25080127a39SPeter Zijlstra 	 */
25180127a39SPeter Zijlstra 
2527f26482aSPeter Zijlstra 	/* Wait for all active readers to complete. */
25380fbaf1cSPeter Zijlstra (Intel) 	rcuwait_wait_event(&sem->writer, readers_active_check(sem), TASK_UNINTERRUPTIBLE);
254*f3e3620fSNamhyung Kim 	if (contended)
255ee042be1SNamhyung Kim 		trace_contention_end(sem, 0);
25632cf7c3cSPeter Zijlstra }
257302707fdSPaul E. McKenney EXPORT_SYMBOL_GPL(percpu_down_write);
25832cf7c3cSPeter Zijlstra 
percpu_up_write(struct percpu_rw_semaphore * sem)25980127a39SPeter Zijlstra void percpu_up_write(struct percpu_rw_semaphore *sem)
26032cf7c3cSPeter Zijlstra {
2611751060eSPeter Zijlstra 	rwsem_release(&sem->dep_map, _RET_IP_);
2621751060eSPeter Zijlstra 
26332cf7c3cSPeter Zijlstra 	/*
26480127a39SPeter Zijlstra 	 * Signal the writer is done, no fast path yet.
26580127a39SPeter Zijlstra 	 *
26680127a39SPeter Zijlstra 	 * One reason that we cannot just immediately flip to readers_fast is
26780127a39SPeter Zijlstra 	 * that new readers might fail to see the results of this writer's
26880127a39SPeter Zijlstra 	 * critical section.
26980127a39SPeter Zijlstra 	 *
27080127a39SPeter Zijlstra 	 * Therefore we force it through the slow path which guarantees an
27180127a39SPeter Zijlstra 	 * acquire and thereby guarantees the critical section's consistency.
27232cf7c3cSPeter Zijlstra 	 */
2737f26482aSPeter Zijlstra 	atomic_set_release(&sem->block, 0);
27480127a39SPeter Zijlstra 
27580127a39SPeter Zijlstra 	/*
2767f26482aSPeter Zijlstra 	 * Prod any pending reader/writer to make progress.
27780127a39SPeter Zijlstra 	 */
2787f26482aSPeter Zijlstra 	__wake_up(&sem->waiters, TASK_NORMAL, 1, sem);
27980127a39SPeter Zijlstra 
28080127a39SPeter Zijlstra 	/*
28180127a39SPeter Zijlstra 	 * Once this completes (at least one RCU-sched grace period hence) the
28280127a39SPeter Zijlstra 	 * reader fast path will be available again. Safe to use outside the
28380127a39SPeter Zijlstra 	 * exclusive write lock because its counting.
28480127a39SPeter Zijlstra 	 */
28580127a39SPeter Zijlstra 	rcu_sync_exit(&sem->rss);
28632cf7c3cSPeter Zijlstra }
287302707fdSPaul E. McKenney EXPORT_SYMBOL_GPL(percpu_up_write);
288