hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/include/linux/percpu-rwsem.h
....@@ -3,37 +3,60 @@
33 #define _LINUX_PERCPU_RWSEM_H
44
55 #include <linux/atomic.h>
6
-#include <linux/rwsem.h>
76 #include <linux/percpu.h>
87 #include <linux/rcuwait.h>
8
+#include <linux/wait.h>
99 #include <linux/rcu_sync.h>
1010 #include <linux/lockdep.h>
11
+
12
+void _trace_android_vh_record_pcpu_rwsem_starttime(
13
+ struct task_struct *tsk, unsigned long settime);
1114
1215 struct percpu_rw_semaphore {
1316 struct rcu_sync rss;
1417 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
1824 };
1925
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) \
2138 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), \
2441 .read_count = &__percpu_rwsem_rc_##name, \
25
- .rw_sem = __RWSEM_INITIALIZER(name.rw_sem), \
2642 .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) \
2746 }
2847
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);
3154
3255 static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
3356 {
3457 might_sleep();
3558
36
- rwsem_acquire_read(&sem->rw_sem.dep_map, 0, 0, _RET_IP_);
59
+ rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);
3760
3861 preempt_disable();
3962 /*
....@@ -41,29 +64,32 @@
4164 * cannot both change sem->state from readers_fast and start checking
4265 * counters while we are here. So if we see !sem->state, we know that
4366 * 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
4568 * anything we did within this RCU-sched read-size critical section.
4669 */
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
4973 __percpu_down_read(sem, false); /* Unconditional memory barrier */
5074 /*
5175 * The preempt_enable() prevents the compiler from
5276 * bleeding the critical section out.
5377 */
5478 preempt_enable();
79
+ _trace_android_vh_record_pcpu_rwsem_starttime(current, jiffies);
5580 }
5681
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)
5883 {
59
- int ret = 1;
84
+ bool ret = true;
6085
6186 preempt_disable();
6287 /*
6388 * Same as in percpu_down_read().
6489 */
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
6793 ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */
6894 preempt_enable();
6995 /*
....@@ -71,25 +97,40 @@
7197 * bleeding the critical section out.
7298 */
7399
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
+ }
76104
77105 return ret;
78106 }
79107
80108 static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
81109 {
110
+ rwsem_release(&sem->dep_map, _RET_IP_);
111
+
82112 preempt_disable();
83113 /*
84114 * Same as in percpu_down_read().
85115 */
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);
90133 preempt_enable();
91
-
92
- rwsem_release(&sem->rw_sem.dep_map, 1, _RET_IP_);
93134 }
94135
95136 extern void percpu_down_write(struct percpu_rw_semaphore *);
....@@ -98,7 +139,11 @@
98139 extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
99140 const char *, struct lock_class_key *);
100141
142
+/* Can't be called in atomic context. */
101143 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);
102147
103148 #define percpu_init_rwsem(sem) \
104149 ({ \
....@@ -106,29 +151,19 @@
106151 __percpu_init_rwsem(sem, #sem, &rwsem_key); \
107152 })
108153
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)
113156
114157 static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
115158 bool read, unsigned long ip)
116159 {
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);
122161 }
123162
124163 static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
125164 bool read, unsigned long ip)
126165 {
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);
132167 }
133168
134169 #endif