Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
62ac665f MP |
2 | #ifndef _LINUX_PERCPU_RWSEM_H |
3 | #define _LINUX_PERCPU_RWSEM_H | |
4 | ||
9390ef0c | 5 | #include <linux/atomic.h> |
a1fd3e24 | 6 | #include <linux/rwsem.h> |
62ac665f | 7 | #include <linux/percpu.h> |
52b94129 | 8 | #include <linux/rcuwait.h> |
001dac62 | 9 | #include <linux/rcu_sync.h> |
8ebe3473 | 10 | #include <linux/lockdep.h> |
62ac665f MP |
11 | |
12 | struct percpu_rw_semaphore { | |
001dac62 | 13 | struct rcu_sync rss; |
80127a39 | 14 | unsigned int __percpu *read_count; |
52b94129 DB |
15 | struct rw_semaphore rw_sem; /* slowpath */ |
16 | struct rcuwait writer; /* blocked writer */ | |
80127a39 | 17 | int readers_block; |
62ac665f MP |
18 | }; |
19 | ||
3f2947b7 | 20 | #define __DEFINE_PERCPU_RWSEM(name, is_static) \ |
11d9684c | 21 | static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \ |
3f2947b7 | 22 | is_static struct percpu_rw_semaphore name = { \ |
95bf33b5 | 23 | .rss = __RCU_SYNC_INITIALIZER(name.rss), \ |
11d9684c PZ |
24 | .read_count = &__percpu_rwsem_rc_##name, \ |
25 | .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \ | |
52b94129 | 26 | .writer = __RCUWAIT_INITIALIZER(name.writer), \ |
11d9684c | 27 | } |
3f2947b7 ON |
28 | #define DEFINE_PERCPU_RWSEM(name) \ |
29 | __DEFINE_PERCPU_RWSEM(name, /* not static */) | |
30 | #define DEFINE_STATIC_PERCPU_RWSEM(name) \ | |
31 | __DEFINE_PERCPU_RWSEM(name, static) | |
11d9684c | 32 | |
80127a39 PZ |
33 | extern int __percpu_down_read(struct percpu_rw_semaphore *, int); |
34 | extern void __percpu_up_read(struct percpu_rw_semaphore *); | |
35 | ||
02e525b2 | 36 | static inline void percpu_down_read(struct percpu_rw_semaphore *sem) |
80127a39 PZ |
37 | { |
38 | might_sleep(); | |
39 | ||
40 | rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 0, _RET_IP_); | |
41 | ||
42 | preempt_disable(); | |
43 | /* | |
44 | * We are in an RCU-sched read-side critical section, so the writer | |
45 | * cannot both change sem->state from readers_fast and start checking | |
46 | * counters while we are here. So if we see !sem->state, we know that | |
47 | * the writer won't be checking until we're past the preempt_enable() | |
e3e74054 | 48 | * and that once the synchronize_rcu() is done, the writer will see |
80127a39 PZ |
49 | * anything we did within this RCU-sched read-size critical section. |
50 | */ | |
51 | __this_cpu_inc(*sem->read_count); | |
52 | if (unlikely(!rcu_sync_is_idle(&sem->rss))) | |
53 | __percpu_down_read(sem, false); /* Unconditional memory barrier */ | |
80127a39 | 54 | /* |
02e525b2 | 55 | * The preempt_enable() prevents the compiler from |
80127a39 PZ |
56 | * bleeding the critical section out. |
57 | */ | |
259d69b7 PZ |
58 | preempt_enable(); |
59 | } | |
60 | ||
80127a39 PZ |
61 | static inline int percpu_down_read_trylock(struct percpu_rw_semaphore *sem) |
62 | { | |
63 | int ret = 1; | |
64 | ||
65 | preempt_disable(); | |
66 | /* | |
67 | * Same as in percpu_down_read(). | |
68 | */ | |
69 | __this_cpu_inc(*sem->read_count); | |
70 | if (unlikely(!rcu_sync_is_idle(&sem->rss))) | |
71 | ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */ | |
72 | preempt_enable(); | |
73 | /* | |
74 | * The barrier() from preempt_enable() prevents the compiler from | |
75 | * bleeding the critical section out. | |
76 | */ | |
77 | ||
78 | if (ret) | |
79 | rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 1, _RET_IP_); | |
80 | ||
81 | return ret; | |
82 | } | |
83 | ||
02e525b2 | 84 | static inline void percpu_up_read(struct percpu_rw_semaphore *sem) |
80127a39 | 85 | { |
02e525b2 | 86 | preempt_disable(); |
80127a39 PZ |
87 | /* |
88 | * Same as in percpu_down_read(). | |
89 | */ | |
90 | if (likely(rcu_sync_is_idle(&sem->rss))) | |
91 | __this_cpu_dec(*sem->read_count); | |
92 | else | |
93 | __percpu_up_read(sem); /* Unconditional memory barrier */ | |
94 | preempt_enable(); | |
95 | ||
96 | rwsem_release(&sem->rw_sem.dep_map, 1, _RET_IP_); | |
97 | } | |
5c1eabe6 | 98 | |
a1fd3e24 ON |
99 | extern void percpu_down_write(struct percpu_rw_semaphore *); |
100 | extern void percpu_up_write(struct percpu_rw_semaphore *); | |
62ac665f | 101 | |
8ebe3473 ON |
102 | extern int __percpu_init_rwsem(struct percpu_rw_semaphore *, |
103 | const char *, struct lock_class_key *); | |
80127a39 | 104 | |
a1fd3e24 | 105 | extern void percpu_free_rwsem(struct percpu_rw_semaphore *); |
62ac665f | 106 | |
80127a39 | 107 | #define percpu_init_rwsem(sem) \ |
8ebe3473 ON |
108 | ({ \ |
109 | static struct lock_class_key rwsem_key; \ | |
80127a39 | 110 | __percpu_init_rwsem(sem, #sem, &rwsem_key); \ |
8ebe3473 ON |
111 | }) |
112 | ||
55cc1565 ON |
113 | #define percpu_rwsem_is_held(sem) lockdep_is_held(&(sem)->rw_sem) |
114 | ||
11d9684c PZ |
115 | #define percpu_rwsem_assert_held(sem) \ |
116 | lockdep_assert_held(&(sem)->rw_sem) | |
117 | ||
55cc1565 ON |
118 | static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem, |
119 | bool read, unsigned long ip) | |
120 | { | |
121 | lock_release(&sem->rw_sem.dep_map, 1, ip); | |
122 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER | |
123 | if (!read) | |
94a9717b | 124 | atomic_long_set(&sem->rw_sem.owner, RWSEM_OWNER_UNKNOWN); |
55cc1565 ON |
125 | #endif |
126 | } | |
127 | ||
128 | static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem, | |
129 | bool read, unsigned long ip) | |
130 | { | |
131 | lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip); | |
5a817641 WL |
132 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
133 | if (!read) | |
94a9717b | 134 | atomic_long_set(&sem->rw_sem.owner, (long)current); |
5a817641 | 135 | #endif |
55cc1565 ON |
136 | } |
137 | ||
62ac665f | 138 | #endif |