net: dev: Makes sure netif_rx() can be invoked in any context.
[linux-block.git] / include / linux / percpu-rwsem.h
CommitLineData
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>
62ac665f 6#include <linux/percpu.h>
52b94129 7#include <linux/rcuwait.h>
7f26482a 8#include <linux/wait.h>
001dac62 9#include <linux/rcu_sync.h>
8ebe3473 10#include <linux/lockdep.h>
62ac665f
MP
11
12struct percpu_rw_semaphore {
001dac62 13 struct rcu_sync rss;
80127a39 14 unsigned int __percpu *read_count;
7f26482a
PZ
15 struct rcuwait writer;
16 wait_queue_head_t waiters;
17 atomic_t block;
1751060e
PZ
18#ifdef CONFIG_DEBUG_LOCK_ALLOC
19 struct lockdep_map dep_map;
20#endif
62ac665f
MP
21};
22
1751060e
PZ
23#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
3f2947b7 29#define __DEFINE_PERCPU_RWSEM(name, is_static) \
11d9684c 30static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \
3f2947b7 31is_static struct percpu_rw_semaphore name = { \
95bf33b5 32 .rss = __RCU_SYNC_INITIALIZER(name.rss), \
11d9684c 33 .read_count = &__percpu_rwsem_rc_##name, \
52b94129 34 .writer = __RCUWAIT_INITIALIZER(name.writer), \
7f26482a
PZ
35 .waiters = __WAIT_QUEUE_HEAD_INITIALIZER(name.waiters), \
36 .block = ATOMIC_INIT(0), \
1751060e 37 __PERCPU_RWSEM_DEP_MAP_INIT(name) \
11d9684c 38}
1751060e 39
3f2947b7
ON
40#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)
11d9684c 44
206c98ff 45extern bool __percpu_down_read(struct percpu_rw_semaphore *, bool);
80127a39 46
02e525b2 47static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
80127a39
PZ
48{
49 might_sleep();
50
1751060e 51 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
80127a39
PZ
52
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()
e3e74054 59 * and that once the synchronize_rcu() is done, the writer will see
80127a39
PZ
60 * anything we did within this RCU-sched read-size critical section.
61 */
71365d40 62 if (likely(rcu_sync_is_idle(&sem->rss)))
e6b1a44e 63 this_cpu_inc(*sem->read_count);
71365d40 64 else
80127a39 65 __percpu_down_read(sem, false); /* Unconditional memory barrier */
80127a39 66 /*
02e525b2 67 * The preempt_enable() prevents the compiler from
80127a39
PZ
68 * bleeding the critical section out.
69 */
259d69b7
PZ
70 preempt_enable();
71}
72
206c98ff 73static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
80127a39 74{
206c98ff 75 bool ret = true;
80127a39
PZ
76
77 preempt_disable();
78 /*
79 * Same as in percpu_down_read().
80 */
71365d40 81 if (likely(rcu_sync_is_idle(&sem->rss)))
e6b1a44e 82 this_cpu_inc(*sem->read_count);
71365d40 83 else
80127a39
PZ
84 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)
1751060e 92 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
80127a39
PZ
93
94 return ret;
95}
96
02e525b2 97static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
80127a39 98{
1751060e
PZ
99 rwsem_release(&sem->dep_map, _RET_IP_);
100
02e525b2 101 preempt_disable();
80127a39
PZ
102 /*
103 * Same as in percpu_down_read().
104 */
ac8dec42 105 if (likely(rcu_sync_is_idle(&sem->rss))) {
e6b1a44e 106 this_cpu_dec(*sem->read_count);
ac8dec42
DB
107 } 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 */
e6b1a44e 118 this_cpu_dec(*sem->read_count);
ac8dec42
DB
119 rcuwait_wake_up(&sem->writer);
120 }
80127a39 121 preempt_enable();
80127a39 122}
5c1eabe6 123
a1fd3e24
ON
124extern void percpu_down_write(struct percpu_rw_semaphore *);
125extern void percpu_up_write(struct percpu_rw_semaphore *);
62ac665f 126
8ebe3473
ON
127extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
128 const char *, struct lock_class_key *);
80127a39 129
a1fd3e24 130extern void percpu_free_rwsem(struct percpu_rw_semaphore *);
62ac665f 131
80127a39 132#define percpu_init_rwsem(sem) \
8ebe3473
ON
133({ \
134 static struct lock_class_key rwsem_key; \
80127a39 135 __percpu_init_rwsem(sem, #sem, &rwsem_key); \
8ebe3473
ON
136})
137
1751060e
PZ
138#define percpu_rwsem_is_held(sem) lockdep_is_held(sem)
139#define percpu_rwsem_assert_held(sem) lockdep_assert_held(sem)
11d9684c 140
55cc1565
ON
141static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
142 bool read, unsigned long ip)
143{
1751060e 144 lock_release(&sem->dep_map, ip);
55cc1565
ON
145}
146
147static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
148 bool read, unsigned long ip)
149{
1751060e 150 lock_acquire(&sem->dep_map, 0, 1, read, 1, NULL, ip);
55cc1565
ON
151}
152
62ac665f 153#endif