| .. | .. |
|---|
| 3 | 3 | #define _LINUX_PERCPU_RWSEM_H |
|---|
| 4 | 4 | |
|---|
| 5 | 5 | #include <linux/atomic.h> |
|---|
| 6 | | -#include <linux/rwsem.h> |
|---|
| 7 | 6 | #include <linux/percpu.h> |
|---|
| 8 | 7 | #include <linux/rcuwait.h> |
|---|
| 8 | +#include <linux/wait.h> |
|---|
| 9 | 9 | #include <linux/rcu_sync.h> |
|---|
| 10 | 10 | #include <linux/lockdep.h> |
|---|
| 11 | + |
|---|
| 12 | +void _trace_android_vh_record_pcpu_rwsem_starttime( |
|---|
| 13 | + struct task_struct *tsk, unsigned long settime); |
|---|
| 11 | 14 | |
|---|
| 12 | 15 | struct percpu_rw_semaphore { |
|---|
| 13 | 16 | struct rcu_sync rss; |
|---|
| 14 | 17 | unsigned int __percpu *read_count; |
|---|
| 15 | | - struct rw_semaphore rw_sem; /* slowpath */ |
|---|
| 16 | | - struct rcuwait writer; /* blocked writer */ |
|---|
| 17 | | - int readers_block; |
|---|
| 18 | + struct rcuwait writer; |
|---|
| 19 | + wait_queue_head_t waiters; |
|---|
| 20 | + atomic_t block; |
|---|
| 21 | +#ifdef CONFIG_DEBUG_LOCK_ALLOC |
|---|
| 22 | + struct lockdep_map dep_map; |
|---|
| 23 | +#endif |
|---|
| 18 | 24 | }; |
|---|
| 19 | 25 | |
|---|
| 20 | | -#define DEFINE_STATIC_PERCPU_RWSEM(name) \ |
|---|
| 26 | +struct percpu_rw_semaphore_atomic { |
|---|
| 27 | + struct percpu_rw_semaphore rw_sem; |
|---|
| 28 | + struct list_head destroy_list_entry; |
|---|
| 29 | +}; |
|---|
| 30 | + |
|---|
| 31 | +#ifdef CONFIG_DEBUG_LOCK_ALLOC |
|---|
| 32 | +#define __PERCPU_RWSEM_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }, |
|---|
| 33 | +#else |
|---|
| 34 | +#define __PERCPU_RWSEM_DEP_MAP_INIT(lockname) |
|---|
| 35 | +#endif |
|---|
| 36 | + |
|---|
| 37 | +#define __DEFINE_PERCPU_RWSEM(name, is_static) \ |
|---|
| 21 | 38 | static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \ |
|---|
| 22 | | -static struct percpu_rw_semaphore name = { \ |
|---|
| 23 | | - .rss = __RCU_SYNC_INITIALIZER(name.rss, RCU_SCHED_SYNC), \ |
|---|
| 39 | +is_static struct percpu_rw_semaphore name = { \ |
|---|
| 40 | + .rss = __RCU_SYNC_INITIALIZER(name.rss), \ |
|---|
| 24 | 41 | .read_count = &__percpu_rwsem_rc_##name, \ |
|---|
| 25 | | - .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \ |
|---|
| 26 | 42 | .writer = __RCUWAIT_INITIALIZER(name.writer), \ |
|---|
| 43 | + .waiters = __WAIT_QUEUE_HEAD_INITIALIZER(name.waiters), \ |
|---|
| 44 | + .block = ATOMIC_INIT(0), \ |
|---|
| 45 | + __PERCPU_RWSEM_DEP_MAP_INIT(name) \ |
|---|
| 27 | 46 | } |
|---|
| 28 | 47 | |
|---|
| 29 | | -extern int __percpu_down_read(struct percpu_rw_semaphore *, int); |
|---|
| 30 | | -extern void __percpu_up_read(struct percpu_rw_semaphore *); |
|---|
| 48 | +#define DEFINE_PERCPU_RWSEM(name) \ |
|---|
| 49 | + __DEFINE_PERCPU_RWSEM(name, /* not static */) |
|---|
| 50 | +#define DEFINE_STATIC_PERCPU_RWSEM(name) \ |
|---|
| 51 | + __DEFINE_PERCPU_RWSEM(name, static) |
|---|
| 52 | + |
|---|
| 53 | +extern bool __percpu_down_read(struct percpu_rw_semaphore *, bool); |
|---|
| 31 | 54 | |
|---|
| 32 | 55 | static inline void percpu_down_read(struct percpu_rw_semaphore *sem) |
|---|
| 33 | 56 | { |
|---|
| 34 | 57 | might_sleep(); |
|---|
| 35 | 58 | |
|---|
| 36 | | - rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 0, _RET_IP_); |
|---|
| 59 | + rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); |
|---|
| 37 | 60 | |
|---|
| 38 | 61 | preempt_disable(); |
|---|
| 39 | 62 | /* |
|---|
| .. | .. |
|---|
| 41 | 64 | * cannot both change sem->state from readers_fast and start checking |
|---|
| 42 | 65 | * counters while we are here. So if we see !sem->state, we know that |
|---|
| 43 | 66 | * 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 |
|---|
| 67 | + * and that once the synchronize_rcu() is done, the writer will see |
|---|
| 45 | 68 | * anything we did within this RCU-sched read-size critical section. |
|---|
| 46 | 69 | */ |
|---|
| 47 | | - __this_cpu_inc(*sem->read_count); |
|---|
| 48 | | - if (unlikely(!rcu_sync_is_idle(&sem->rss))) |
|---|
| 70 | + if (likely(rcu_sync_is_idle(&sem->rss))) |
|---|
| 71 | + this_cpu_inc(*sem->read_count); |
|---|
| 72 | + else |
|---|
| 49 | 73 | __percpu_down_read(sem, false); /* Unconditional memory barrier */ |
|---|
| 50 | 74 | /* |
|---|
| 51 | 75 | * The preempt_enable() prevents the compiler from |
|---|
| 52 | 76 | * bleeding the critical section out. |
|---|
| 53 | 77 | */ |
|---|
| 54 | 78 | preempt_enable(); |
|---|
| 79 | + _trace_android_vh_record_pcpu_rwsem_starttime(current, jiffies); |
|---|
| 55 | 80 | } |
|---|
| 56 | 81 | |
|---|
| 57 | | -static inline int percpu_down_read_trylock(struct percpu_rw_semaphore *sem) |
|---|
| 82 | +static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem) |
|---|
| 58 | 83 | { |
|---|
| 59 | | - int ret = 1; |
|---|
| 84 | + bool ret = true; |
|---|
| 60 | 85 | |
|---|
| 61 | 86 | preempt_disable(); |
|---|
| 62 | 87 | /* |
|---|
| 63 | 88 | * Same as in percpu_down_read(). |
|---|
| 64 | 89 | */ |
|---|
| 65 | | - __this_cpu_inc(*sem->read_count); |
|---|
| 66 | | - if (unlikely(!rcu_sync_is_idle(&sem->rss))) |
|---|
| 90 | + if (likely(rcu_sync_is_idle(&sem->rss))) |
|---|
| 91 | + this_cpu_inc(*sem->read_count); |
|---|
| 92 | + else |
|---|
| 67 | 93 | ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */ |
|---|
| 68 | 94 | preempt_enable(); |
|---|
| 69 | 95 | /* |
|---|
| .. | .. |
|---|
| 71 | 97 | * bleeding the critical section out. |
|---|
| 72 | 98 | */ |
|---|
| 73 | 99 | |
|---|
| 74 | | - if (ret) |
|---|
| 75 | | - rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 1, _RET_IP_); |
|---|
| 100 | + if (ret) { |
|---|
| 101 | + _trace_android_vh_record_pcpu_rwsem_starttime(current, jiffies); |
|---|
| 102 | + rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_); |
|---|
| 103 | + } |
|---|
| 76 | 104 | |
|---|
| 77 | 105 | return ret; |
|---|
| 78 | 106 | } |
|---|
| 79 | 107 | |
|---|
| 80 | 108 | static inline void percpu_up_read(struct percpu_rw_semaphore *sem) |
|---|
| 81 | 109 | { |
|---|
| 110 | + rwsem_release(&sem->dep_map, _RET_IP_); |
|---|
| 111 | + |
|---|
| 82 | 112 | preempt_disable(); |
|---|
| 83 | 113 | /* |
|---|
| 84 | 114 | * Same as in percpu_down_read(). |
|---|
| 85 | 115 | */ |
|---|
| 86 | | - if (likely(rcu_sync_is_idle(&sem->rss))) |
|---|
| 87 | | - __this_cpu_dec(*sem->read_count); |
|---|
| 88 | | - else |
|---|
| 89 | | - __percpu_up_read(sem); /* Unconditional memory barrier */ |
|---|
| 116 | + if (likely(rcu_sync_is_idle(&sem->rss))) { |
|---|
| 117 | + this_cpu_dec(*sem->read_count); |
|---|
| 118 | + } else { |
|---|
| 119 | + /* |
|---|
| 120 | + * slowpath; reader will only ever wake a single blocked |
|---|
| 121 | + * writer. |
|---|
| 122 | + */ |
|---|
| 123 | + smp_mb(); /* B matches C */ |
|---|
| 124 | + /* |
|---|
| 125 | + * In other words, if they see our decrement (presumably to |
|---|
| 126 | + * aggregate zero, as that is the only time it matters) they |
|---|
| 127 | + * will also see our critical section. |
|---|
| 128 | + */ |
|---|
| 129 | + this_cpu_dec(*sem->read_count); |
|---|
| 130 | + rcuwait_wake_up(&sem->writer); |
|---|
| 131 | + } |
|---|
| 132 | + _trace_android_vh_record_pcpu_rwsem_starttime(current, 0); |
|---|
| 90 | 133 | preempt_enable(); |
|---|
| 91 | | - |
|---|
| 92 | | - rwsem_release(&sem->rw_sem.dep_map, 1, _RET_IP_); |
|---|
| 93 | 134 | } |
|---|
| 94 | 135 | |
|---|
| 95 | 136 | extern void percpu_down_write(struct percpu_rw_semaphore *); |
|---|
| .. | .. |
|---|
| 98 | 139 | extern int __percpu_init_rwsem(struct percpu_rw_semaphore *, |
|---|
| 99 | 140 | const char *, struct lock_class_key *); |
|---|
| 100 | 141 | |
|---|
| 142 | +/* Can't be called in atomic context. */ |
|---|
| 101 | 143 | extern void percpu_free_rwsem(struct percpu_rw_semaphore *); |
|---|
| 144 | + |
|---|
| 145 | +/* Invokes percpu_free_rwsem and frees the semaphore from a worker thread. */ |
|---|
| 146 | +extern void percpu_rwsem_async_destroy(struct percpu_rw_semaphore_atomic *sem); |
|---|
| 102 | 147 | |
|---|
| 103 | 148 | #define percpu_init_rwsem(sem) \ |
|---|
| 104 | 149 | ({ \ |
|---|
| .. | .. |
|---|
| 106 | 151 | __percpu_init_rwsem(sem, #sem, &rwsem_key); \ |
|---|
| 107 | 152 | }) |
|---|
| 108 | 153 | |
|---|
| 109 | | -#define percpu_rwsem_is_held(sem) lockdep_is_held(&(sem)->rw_sem) |
|---|
| 110 | | - |
|---|
| 111 | | -#define percpu_rwsem_assert_held(sem) \ |
|---|
| 112 | | - lockdep_assert_held(&(sem)->rw_sem) |
|---|
| 154 | +#define percpu_rwsem_is_held(sem) lockdep_is_held(sem) |
|---|
| 155 | +#define percpu_rwsem_assert_held(sem) lockdep_assert_held(sem) |
|---|
| 113 | 156 | |
|---|
| 114 | 157 | static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem, |
|---|
| 115 | 158 | bool read, unsigned long ip) |
|---|
| 116 | 159 | { |
|---|
| 117 | | - lock_release(&sem->rw_sem.dep_map, 1, ip); |
|---|
| 118 | | -#ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
|---|
| 119 | | - if (!read) |
|---|
| 120 | | - sem->rw_sem.owner = RWSEM_OWNER_UNKNOWN; |
|---|
| 121 | | -#endif |
|---|
| 160 | + lock_release(&sem->dep_map, ip); |
|---|
| 122 | 161 | } |
|---|
| 123 | 162 | |
|---|
| 124 | 163 | static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem, |
|---|
| 125 | 164 | bool read, unsigned long ip) |
|---|
| 126 | 165 | { |
|---|
| 127 | | - lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip); |
|---|
| 128 | | -#ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
|---|
| 129 | | - if (!read) |
|---|
| 130 | | - sem->rw_sem.owner = current; |
|---|
| 131 | | -#endif |
|---|
| 166 | + lock_acquire(&sem->dep_map, 0, 1, read, 1, NULL, ip); |
|---|
| 132 | 167 | } |
|---|
| 133 | 168 | |
|---|
| 134 | 169 | #endif |
|---|