1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_PERCPU_RWSEM_H 3 #define _LINUX_PERCPU_RWSEM_H 4 5 #include <linux/atomic.h> 6 #include <linux/rwsem.h> 7 #include <linux/percpu.h> 8 #include <linux/rcuwait.h> 9 #include <linux/rcu_sync.h> 10 #include <linux/lockdep.h> 11 12 struct percpu_rw_semaphore { 13 struct rcu_sync rss; 14 unsigned int __percpu *read_count; 15 struct rw_semaphore rw_sem; /* slowpath */ 16 struct rcuwait writer; /* blocked writer */ 17 int readers_block; 18 #ifdef CONFIG_DEBUG_LOCK_ALLOC 19 struct lockdep_map dep_map; 20 #endif 21 }; 22 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 29 #define __DEFINE_PERCPU_RWSEM(name, is_static) \ 30 static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \ 31 is_static struct percpu_rw_semaphore name = { \ 32 .rss = __RCU_SYNC_INITIALIZER(name.rss), \ 33 .read_count = &__percpu_rwsem_rc_##name, \ 34 .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \ 35 .writer = __RCUWAIT_INITIALIZER(name.writer), \ 36 __PERCPU_RWSEM_DEP_MAP_INIT(name) \ 37 } 38 39 #define DEFINE_PERCPU_RWSEM(name) \ 40 __DEFINE_PERCPU_RWSEM(name, /* not static */) 41 #define DEFINE_STATIC_PERCPU_RWSEM(name) \ 42 __DEFINE_PERCPU_RWSEM(name, static) 43 44 extern bool __percpu_down_read(struct percpu_rw_semaphore *, bool); 45 extern void __percpu_up_read(struct percpu_rw_semaphore *); 46 47 static inline void percpu_down_read(struct percpu_rw_semaphore *sem) 48 { 49 might_sleep(); 50 51 rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); 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() 59 * and that once the synchronize_rcu() is done, the writer will see 60 * anything we did within this RCU-sched read-size critical section. 61 */ 62 __this_cpu_inc(*sem->read_count); 63 if (unlikely(!rcu_sync_is_idle(&sem->rss))) 64 __percpu_down_read(sem, false); /* Unconditional memory barrier */ 65 /* 66 * The preempt_enable() prevents the compiler from 67 * bleeding the critical section out. 68 */ 69 preempt_enable(); 70 } 71 72 static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem) 73 { 74 bool ret = true; 75 76 preempt_disable(); 77 /* 78 * Same as in percpu_down_read(). 79 */ 80 __this_cpu_inc(*sem->read_count); 81 if (unlikely(!rcu_sync_is_idle(&sem->rss))) 82 ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */ 83 preempt_enable(); 84 /* 85 * The barrier() from preempt_enable() prevents the compiler from 86 * bleeding the critical section out. 87 */ 88 89 if (ret) 90 rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_); 91 92 return ret; 93 } 94 95 static inline void percpu_up_read(struct percpu_rw_semaphore *sem) 96 { 97 rwsem_release(&sem->dep_map, _RET_IP_); 98 99 preempt_disable(); 100 /* 101 * Same as in percpu_down_read(). 102 */ 103 if (likely(rcu_sync_is_idle(&sem->rss))) 104 __this_cpu_dec(*sem->read_count); 105 else 106 __percpu_up_read(sem); /* Unconditional memory barrier */ 107 preempt_enable(); 108 } 109 110 extern void percpu_down_write(struct percpu_rw_semaphore *); 111 extern void percpu_up_write(struct percpu_rw_semaphore *); 112 113 extern int __percpu_init_rwsem(struct percpu_rw_semaphore *, 114 const char *, struct lock_class_key *); 115 116 extern void percpu_free_rwsem(struct percpu_rw_semaphore *); 117 118 #define percpu_init_rwsem(sem) \ 119 ({ \ 120 static struct lock_class_key rwsem_key; \ 121 __percpu_init_rwsem(sem, #sem, &rwsem_key); \ 122 }) 123 124 #define percpu_rwsem_is_held(sem) lockdep_is_held(sem) 125 #define percpu_rwsem_assert_held(sem) lockdep_assert_held(sem) 126 127 static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem, 128 bool read, unsigned long ip) 129 { 130 lock_release(&sem->dep_map, ip); 131 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 132 if (!read) 133 atomic_long_set(&sem->rw_sem.owner, RWSEM_OWNER_UNKNOWN); 134 #endif 135 } 136 137 static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem, 138 bool read, unsigned long ip) 139 { 140 lock_acquire(&sem->dep_map, 0, 1, read, 1, NULL, ip); 141 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER 142 if (!read) 143 atomic_long_set(&sem->rw_sem.owner, (long)current); 144 #endif 145 } 146 147 #endif 148