blob: 5fda40f97fe916043b8217e8fd8b6a3ccdb0f26f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Mikulas Patocka62ac6652012-09-26 07:46:43 +02002#ifndef _LINUX_PERCPU_RWSEM_H
3#define _LINUX_PERCPU_RWSEM_H
4
Oleg Nesterov9390ef02012-12-17 16:01:36 -08005#include <linux/atomic.h>
Mikulas Patocka62ac6652012-09-26 07:46:43 +02006#include <linux/percpu.h>
Davidlohr Bueso52b94122017-01-11 07:22:26 -08007#include <linux/rcuwait.h>
Peter Zijlstra7f264822019-10-30 20:30:41 +01008#include <linux/wait.h>
Oleg Nesterov001dac62015-08-21 19:42:57 +02009#include <linux/rcu_sync.h>
Oleg Nesterov8ebe3472012-12-17 16:01:38 -080010#include <linux/lockdep.h>
Mikulas Patocka62ac6652012-09-26 07:46:43 +020011
12struct percpu_rw_semaphore {
Oleg Nesterov001dac62015-08-21 19:42:57 +020013 struct rcu_sync rss;
Peter Zijlstra80127a32016-07-14 20:08:46 +020014 unsigned int __percpu *read_count;
Peter Zijlstra7f264822019-10-30 20:30:41 +010015 struct rcuwait writer;
16 wait_queue_head_t waiters;
17 atomic_t block;
Peter Zijlstra17510602019-10-30 20:01:26 +010018#ifdef CONFIG_DEBUG_LOCK_ALLOC
19 struct lockdep_map dep_map;
20#endif
Mikulas Patocka62ac6652012-09-26 07:46:43 +020021};
22
Peter Zijlstra17510602019-10-30 20:01:26 +010023#ifdef CONFIG_DEBUG_LOCK_ALLOC
24#define __PERCPU_RWSEM_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname },
25#else
26#define __PERCPU_RWSEM_DEP_MAP_INIT(lockname)
27#endif
28
Oleg Nesterov3f2947b2019-04-23 18:32:41 +020029#define __DEFINE_PERCPU_RWSEM(name, is_static) \
Peter Zijlstra11d96842015-06-22 14:16:31 +020030static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \
Oleg Nesterov3f2947b2019-04-23 18:32:41 +020031is_static struct percpu_rw_semaphore name = { \
Oleg Nesterov95bf33b2019-04-23 14:07:24 +020032 .rss = __RCU_SYNC_INITIALIZER(name.rss), \
Peter Zijlstra11d96842015-06-22 14:16:31 +020033 .read_count = &__percpu_rwsem_rc_##name, \
Davidlohr Bueso52b94122017-01-11 07:22:26 -080034 .writer = __RCUWAIT_INITIALIZER(name.writer), \
Peter Zijlstra7f264822019-10-30 20:30:41 +010035 .waiters = __WAIT_QUEUE_HEAD_INITIALIZER(name.waiters), \
36 .block = ATOMIC_INIT(0), \
Peter Zijlstra17510602019-10-30 20:01:26 +010037 __PERCPU_RWSEM_DEP_MAP_INIT(name) \
Peter Zijlstra11d96842015-06-22 14:16:31 +020038}
Peter Zijlstra17510602019-10-30 20:01:26 +010039
Oleg Nesterov3f2947b2019-04-23 18:32:41 +020040#define DEFINE_PERCPU_RWSEM(name) \
41 __DEFINE_PERCPU_RWSEM(name, /* not static */)
42#define DEFINE_STATIC_PERCPU_RWSEM(name) \
43 __DEFINE_PERCPU_RWSEM(name, static)
Peter Zijlstra11d96842015-06-22 14:16:31 +020044
Peter Zijlstra206c98f2019-10-30 20:12:37 +010045extern bool __percpu_down_read(struct percpu_rw_semaphore *, bool);
Peter Zijlstra80127a32016-07-14 20:08:46 +020046
Peter Zijlstra02e525b22019-02-21 15:38:40 +010047static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
Peter Zijlstra80127a32016-07-14 20:08:46 +020048{
49 might_sleep();
50
Peter Zijlstra17510602019-10-30 20:01:26 +010051 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
Peter Zijlstra80127a32016-07-14 20:08:46 +020052
53 preempt_disable();
54 /*
55 * We are in an RCU-sched read-side critical section, so the writer
56 * cannot both change sem->state from readers_fast and start checking
57 * counters while we are here. So if we see !sem->state, we know that
58 * the writer won't be checking until we're past the preempt_enable()
Paul E. McKenneye3e74052018-11-07 13:53:34 -080059 * and that once the synchronize_rcu() is done, the writer will see
Peter Zijlstra80127a32016-07-14 20:08:46 +020060 * anything we did within this RCU-sched read-size critical section.
61 */
Peter Zijlstra71365d42019-10-30 20:17:51 +010062 if (likely(rcu_sync_is_idle(&sem->rss)))
Hou Taoe6b1a442020-09-15 22:07:50 +080063 this_cpu_inc(*sem->read_count);
Peter Zijlstra71365d42019-10-30 20:17:51 +010064 else
Peter Zijlstra80127a32016-07-14 20:08:46 +020065 __percpu_down_read(sem, false); /* Unconditional memory barrier */
Peter Zijlstra80127a32016-07-14 20:08:46 +020066 /*
Peter Zijlstra02e525b22019-02-21 15:38:40 +010067 * The preempt_enable() prevents the compiler from
Peter Zijlstra80127a32016-07-14 20:08:46 +020068 * bleeding the critical section out.
69 */
Peter Zijlstra259d69b2015-11-23 15:23:55 +010070 preempt_enable();
71}
72
Peter Zijlstra206c98f2019-10-30 20:12:37 +010073static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
Peter Zijlstra80127a32016-07-14 20:08:46 +020074{
Peter Zijlstra206c98f2019-10-30 20:12:37 +010075 bool ret = true;
Peter Zijlstra80127a32016-07-14 20:08:46 +020076
77 preempt_disable();
78 /*
79 * Same as in percpu_down_read().
80 */
Peter Zijlstra71365d42019-10-30 20:17:51 +010081 if (likely(rcu_sync_is_idle(&sem->rss)))
Hou Taoe6b1a442020-09-15 22:07:50 +080082 this_cpu_inc(*sem->read_count);
Peter Zijlstra71365d42019-10-30 20:17:51 +010083 else
Peter Zijlstra80127a32016-07-14 20:08:46 +020084 ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */
85 preempt_enable();
86 /*
87 * The barrier() from preempt_enable() prevents the compiler from
88 * bleeding the critical section out.
89 */
90
91 if (ret)
Peter Zijlstra17510602019-10-30 20:01:26 +010092 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
Peter Zijlstra80127a32016-07-14 20:08:46 +020093
94 return ret;
95}
96
Peter Zijlstra02e525b22019-02-21 15:38:40 +010097static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
Peter Zijlstra80127a32016-07-14 20:08:46 +020098{
Peter Zijlstra17510602019-10-30 20:01:26 +010099 rwsem_release(&sem->dep_map, _RET_IP_);
100
Peter Zijlstra02e525b22019-02-21 15:38:40 +0100101 preempt_disable();
Peter Zijlstra80127a32016-07-14 20:08:46 +0200102 /*
103 * Same as in percpu_down_read().
104 */
Davidlohr Buesoac8dec42019-11-18 15:19:35 -0800105 if (likely(rcu_sync_is_idle(&sem->rss))) {
Hou Taoe6b1a442020-09-15 22:07:50 +0800106 this_cpu_dec(*sem->read_count);
Davidlohr Buesoac8dec42019-11-18 15:19:35 -0800107 } else {
108 /*
109 * slowpath; reader will only ever wake a single blocked
110 * writer.
111 */
112 smp_mb(); /* B matches C */
113 /*
114 * In other words, if they see our decrement (presumably to
115 * aggregate zero, as that is the only time it matters) they
116 * will also see our critical section.
117 */
Hou Taoe6b1a442020-09-15 22:07:50 +0800118 this_cpu_dec(*sem->read_count);
Davidlohr Buesoac8dec42019-11-18 15:19:35 -0800119 rcuwait_wake_up(&sem->writer);
120 }
Peter Zijlstra80127a32016-07-14 20:08:46 +0200121 preempt_enable();
Peter Zijlstra80127a32016-07-14 20:08:46 +0200122}
Mikulas Patocka5c1eabe2012-10-22 19:37:47 -0400123
Oleg Nesterova1fd3e22012-12-17 16:01:32 -0800124extern void percpu_down_write(struct percpu_rw_semaphore *);
125extern void percpu_up_write(struct percpu_rw_semaphore *);
Mikulas Patocka62ac6652012-09-26 07:46:43 +0200126
Oleg Nesterov8ebe3472012-12-17 16:01:38 -0800127extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
128 const char *, struct lock_class_key *);
Peter Zijlstra80127a32016-07-14 20:08:46 +0200129
Oleg Nesterova1fd3e22012-12-17 16:01:32 -0800130extern void percpu_free_rwsem(struct percpu_rw_semaphore *);
Mikulas Patocka62ac6652012-09-26 07:46:43 +0200131
Peter Zijlstra80127a32016-07-14 20:08:46 +0200132#define percpu_init_rwsem(sem) \
Oleg Nesterov8ebe3472012-12-17 16:01:38 -0800133({ \
134 static struct lock_class_key rwsem_key; \
Peter Zijlstra80127a32016-07-14 20:08:46 +0200135 __percpu_init_rwsem(sem, #sem, &rwsem_key); \
Oleg Nesterov8ebe3472012-12-17 16:01:38 -0800136})
137
Peter Zijlstra17510602019-10-30 20:01:26 +0100138#define percpu_rwsem_is_held(sem) lockdep_is_held(sem)
139#define percpu_rwsem_assert_held(sem) lockdep_assert_held(sem)
Peter Zijlstra11d96842015-06-22 14:16:31 +0200140
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200141static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
142 bool read, unsigned long ip)
143{
Peter Zijlstra17510602019-10-30 20:01:26 +0100144 lock_release(&sem->dep_map, ip);
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200145}
146
147static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
148 bool read, unsigned long ip)
149{
Peter Zijlstra17510602019-10-30 20:01:26 +0100150 lock_acquire(&sem->dep_map, 0, 1, read, 1, NULL, ip);
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200151}
152
Mikulas Patocka62ac6652012-09-26 07:46:43 +0200153#endif