blob: 79b99d653e030d113e4401fc26c7b47e81dcff8c [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
Peter Zijlstra11d96842015-06-22 14:16:31 +020020#define DEFINE_STATIC_PERCPU_RWSEM(name) \
21static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \
22static struct percpu_rw_semaphore name = { \
23 .rss = __RCU_SYNC_INITIALIZER(name.rss, RCU_SCHED_SYNC), \
24 .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}
28
Peter Zijlstra80127a32016-07-14 20:08:46 +020029extern int __percpu_down_read(struct percpu_rw_semaphore *, int);
30extern void __percpu_up_read(struct percpu_rw_semaphore *);
31
Peter Zijlstra259d69b2015-11-23 15:23:55 +010032static inline void percpu_down_read_preempt_disable(struct percpu_rw_semaphore *sem)
Peter Zijlstra80127a32016-07-14 20:08:46 +020033{
34 might_sleep();
35
36 rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 0, _RET_IP_);
37
38 preempt_disable();
39 /*
40 * We are in an RCU-sched read-side critical section, so the writer
41 * cannot both change sem->state from readers_fast and start checking
42 * counters while we are here. So if we see !sem->state, we know that
43 * the writer won't be checking until we're past the preempt_enable()
44 * and that one the synchronize_sched() is done, the writer will see
45 * anything we did within this RCU-sched read-size critical section.
46 */
47 __this_cpu_inc(*sem->read_count);
48 if (unlikely(!rcu_sync_is_idle(&sem->rss)))
49 __percpu_down_read(sem, false); /* Unconditional memory barrier */
Peter Zijlstra259d69b2015-11-23 15:23:55 +010050 barrier();
Peter Zijlstra80127a32016-07-14 20:08:46 +020051 /*
Peter Zijlstra259d69b2015-11-23 15:23:55 +010052 * The barrier() prevents the compiler from
Peter Zijlstra80127a32016-07-14 20:08:46 +020053 * bleeding the critical section out.
54 */
55}
56
Peter Zijlstra259d69b2015-11-23 15:23:55 +010057static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
58{
59 percpu_down_read_preempt_disable(sem);
60 preempt_enable();
61}
62
Peter Zijlstra80127a32016-07-14 20:08:46 +020063static inline int percpu_down_read_trylock(struct percpu_rw_semaphore *sem)
64{
65 int ret = 1;
66
67 preempt_disable();
68 /*
69 * Same as in percpu_down_read().
70 */
71 __this_cpu_inc(*sem->read_count);
72 if (unlikely(!rcu_sync_is_idle(&sem->rss)))
73 ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */
74 preempt_enable();
75 /*
76 * The barrier() from preempt_enable() prevents the compiler from
77 * bleeding the critical section out.
78 */
79
80 if (ret)
81 rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 1, _RET_IP_);
82
83 return ret;
84}
85
Peter Zijlstra259d69b2015-11-23 15:23:55 +010086static inline void percpu_up_read_preempt_enable(struct percpu_rw_semaphore *sem)
Peter Zijlstra80127a32016-07-14 20:08:46 +020087{
88 /*
Peter Zijlstra259d69b2015-11-23 15:23:55 +010089 * The barrier() prevents the compiler from
Peter Zijlstra80127a32016-07-14 20:08:46 +020090 * bleeding the critical section out.
91 */
Peter Zijlstra259d69b2015-11-23 15:23:55 +010092 barrier();
Peter Zijlstra80127a32016-07-14 20:08:46 +020093 /*
94 * Same as in percpu_down_read().
95 */
96 if (likely(rcu_sync_is_idle(&sem->rss)))
97 __this_cpu_dec(*sem->read_count);
98 else
99 __percpu_up_read(sem); /* Unconditional memory barrier */
100 preempt_enable();
101
102 rwsem_release(&sem->rw_sem.dep_map, 1, _RET_IP_);
103}
Mikulas Patocka5c1eabe2012-10-22 19:37:47 -0400104
Peter Zijlstra259d69b2015-11-23 15:23:55 +0100105static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
106{
107 preempt_disable();
108 percpu_up_read_preempt_enable(sem);
109}
110
Oleg Nesterova1fd3e22012-12-17 16:01:32 -0800111extern void percpu_down_write(struct percpu_rw_semaphore *);
112extern void percpu_up_write(struct percpu_rw_semaphore *);
Mikulas Patocka62ac6652012-09-26 07:46:43 +0200113
Oleg Nesterov8ebe3472012-12-17 16:01:38 -0800114extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
115 const char *, struct lock_class_key *);
Peter Zijlstra80127a32016-07-14 20:08:46 +0200116
Oleg Nesterova1fd3e22012-12-17 16:01:32 -0800117extern void percpu_free_rwsem(struct percpu_rw_semaphore *);
Mikulas Patocka62ac6652012-09-26 07:46:43 +0200118
Peter Zijlstra80127a32016-07-14 20:08:46 +0200119#define percpu_init_rwsem(sem) \
Oleg Nesterov8ebe3472012-12-17 16:01:38 -0800120({ \
121 static struct lock_class_key rwsem_key; \
Peter Zijlstra80127a32016-07-14 20:08:46 +0200122 __percpu_init_rwsem(sem, #sem, &rwsem_key); \
Oleg Nesterov8ebe3472012-12-17 16:01:38 -0800123})
124
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200125#define percpu_rwsem_is_held(sem) lockdep_is_held(&(sem)->rw_sem)
126
Peter Zijlstra11d96842015-06-22 14:16:31 +0200127#define percpu_rwsem_assert_held(sem) \
128 lockdep_assert_held(&(sem)->rw_sem)
129
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200130static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
131 bool read, unsigned long ip)
132{
133 lock_release(&sem->rw_sem.dep_map, 1, ip);
134#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
135 if (!read)
Waiman Long5a817642018-05-15 17:49:51 -0400136 sem->rw_sem.owner = RWSEM_OWNER_UNKNOWN;
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200137#endif
138}
139
140static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
141 bool read, unsigned long ip)
142{
143 lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);
Waiman Long5a817642018-05-15 17:49:51 -0400144#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
145 if (!read)
146 sem->rw_sem.owner = current;
147#endif
Oleg Nesterov55cc156502015-07-21 20:26:44 +0200148}
149
Mikulas Patocka62ac6652012-09-26 07:46:43 +0200150#endif