blob: ad2ca2a89d5bc7a56f6b998749f2c4e0b5fc83b3 [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>
Oleg Nesterova1fd3e22012-12-17 16:01:32 -08006#include <linux/rwsem.h>
Mikulas Patocka62ac6652012-09-26 07:46:43 +02007#include <linux/percpu.h>
Davidlohr Bueso52b94122017-01-11 07:22:26 -08008#include <linux/rcuwait.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;
Davidlohr Bueso52b94122017-01-11 07:22:26 -080015 struct rw_semaphore rw_sem; /* slowpath */
16 struct rcuwait writer; /* blocked writer */
Peter Zijlstra80127a32016-07-14 20:08:46 +020017 int readers_block;
Mikulas Patocka62ac6652012-09-26 07:46:43 +020018};
19
Oleg Nesterov3f2947b2019-04-23 18:32:41 +020020#define __DEFINE_PERCPU_RWSEM(name, is_static) \
Peter Zijlstra11d96842015-06-22 14:16:31 +020021static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \
Oleg Nesterov3f2947b2019-04-23 18:32:41 +020022is_static struct percpu_rw_semaphore name = { \
Oleg Nesterov95bf33b2019-04-23 14:07:24 +020023 .rss = __RCU_SYNC_INITIALIZER(name.rss), \
Peter Zijlstra11d96842015-06-22 14:16:31 +020024 .read_count = &__percpu_rwsem_rc_##name, \
25 .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \
Davidlohr Bueso52b94122017-01-11 07:22:26 -080026 .writer = __RCUWAIT_INITIALIZER(name.writer), \
Peter Zijlstra11d96842015-06-22 14:16:31 +020027}
Oleg Nesterov3f2947b2019-04-23 18:32:41 +020028#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)
Peter Zijlstra11d96842015-06-22 14:16:31 +020032
Peter Zijlstra80127a32016-07-14 20:08:46 +020033extern int __percpu_down_read(struct percpu_rw_semaphore *, int);
34extern void __percpu_up_read(struct percpu_rw_semaphore *);
35
Peter Zijlstra02e525b22019-02-21 15:38:40 +010036static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
Peter Zijlstra80127a32016-07-14 20:08:46 +020037{
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()
Paul E. McKenneye3e74052018-11-07 13:53:34 -080048 * and that once the synchronize_rcu() is done, the writer will see
Peter Zijlstra80127a32016-07-14 20:08:46 +020049 * 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 */
Peter Zijlstra80127a32016-07-14 20:08:46 +020054 /*
Peter Zijlstra02e525b22019-02-21 15:38:40 +010055 * The preempt_enable() prevents the compiler from
Peter Zijlstra80127a32016-07-14 20:08:46 +020056 * bleeding the critical section out.
57 */
Peter Zijlstra259d69b2015-11-23 15:23:55 +010058 preempt_enable();
59}
60
Peter Zijlstra80127a32016-07-14 20:08:46 +020061static 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
Peter Zijlstra02e525b22019-02-21 15:38:40 +010084static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
Peter Zijlstra80127a32016-07-14 20:08:46 +020085{
Peter Zijlstra02e525b22019-02-21 15:38:40 +010086 preempt_disable();
Peter Zijlstra80127a32016-07-14 20:08:46 +020087 /*
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
Qian Cai5facae42019-09-19 12:09:40 -040096 rwsem_release(&sem->rw_sem.dep_map, _RET_IP_);
Peter Zijlstra80127a32016-07-14 20:08:46 +020097}
Mikulas Patocka5c1eabe2012-10-22 19:37:47 -040098
Oleg Nesterova1fd3e22012-12-17 16:01:32 -080099extern void percpu_down_write(struct percpu_rw_semaphore *);
100extern void percpu_up_write(struct percpu_rw_semaphore *);
Mikulas Patocka62ac6652012-09-26 07:46:43 +0200101
Oleg Nesterov8ebe3472012-12-17 16:01:38 -0800102extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
103 const char *, struct lock_class_key *);
Peter Zijlstra80127a32016-07-14 20:08:46 +0200104
Oleg Nesterova1fd3e22012-12-17 16:01:32 -0800105extern void percpu_free_rwsem(struct percpu_rw_semaphore *);
Mikulas Patocka62ac6652012-09-26 07:46:43 +0200106
Peter Zijlstra80127a32016-07-14 20:08:46 +0200107#define percpu_init_rwsem(sem) \
Oleg Nesterov8ebe3472012-12-17 16:01:38 -0800108({ \
109 static struct lock_class_key rwsem_key; \
Peter Zijlstra80127a32016-07-14 20:08:46 +0200110 __percpu_init_rwsem(sem, #sem, &rwsem_key); \
Oleg Nesterov8ebe3472012-12-17 16:01:38 -0800111})
112
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200113#define percpu_rwsem_is_held(sem) lockdep_is_held(&(sem)->rw_sem)
114
Peter Zijlstra11d96842015-06-22 14:16:31 +0200115#define percpu_rwsem_assert_held(sem) \
116 lockdep_assert_held(&(sem)->rw_sem)
117
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200118static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
119 bool read, unsigned long ip)
120{
Qian Cai5facae42019-09-19 12:09:40 -0400121 lock_release(&sem->rw_sem.dep_map, ip);
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200122#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
123 if (!read)
Waiman Long94a97172019-05-20 16:59:12 -0400124 atomic_long_set(&sem->rw_sem.owner, RWSEM_OWNER_UNKNOWN);
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200125#endif
126}
127
128static 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);
Waiman Long5a817642018-05-15 17:49:51 -0400132#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
133 if (!read)
Waiman Long94a97172019-05-20 16:59:12 -0400134 atomic_long_set(&sem->rw_sem.owner, (long)current);
Waiman Long5a817642018-05-15 17:49:51 -0400135#endif
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200136}
137
Mikulas Patocka62ac6652012-09-26 07:46:43 +0200138#endif