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)
3152
32
-static inline void percpu_down_read_preempt_disable(struct percpu_rw_semaphore *sem)
53
+extern bool __percpu_down_read(struct percpu_rw_semaphore *, bool);
54
+
55
+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,35 +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 */
50
- barrier();
5174 /*
52
- * The barrier() prevents the compiler from
75
+ * The preempt_enable() prevents the compiler from
5376 * bleeding the critical section out.
5477 */
55
-}
56
-
57
-static inline void percpu_down_read(struct percpu_rw_semaphore *sem)
58
-{
59
- percpu_down_read_preempt_disable(sem);
6078 preempt_enable();
79
+ _trace_android_vh_record_pcpu_rwsem_starttime(current, jiffies);
6180 }
6281
63
-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)
6483 {
65
- int ret = 1;
84
+ bool ret = true;
6685
6786 preempt_disable();
6887 /*
6988 * Same as in percpu_down_read().
7089 */
71
- __this_cpu_inc(*sem->read_count);
72
- 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
7393 ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */
7494 preempt_enable();
7595 /*
....@@ -77,35 +97,40 @@
7797 * bleeding the critical section out.
7898 */
7999
80
- if (ret)
81
- 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
+ }
82104
83105 return ret;
84106 }
85107
86
-static inline void percpu_up_read_preempt_enable(struct percpu_rw_semaphore *sem)
108
+static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
87109 {
88
- /*
89
- * The barrier() prevents the compiler from
90
- * bleeding the critical section out.
91
- */
92
- barrier();
110
+ rwsem_release(&sem->dep_map, _RET_IP_);
111
+
112
+ preempt_disable();
93113 /*
94114 * Same as in percpu_down_read().
95115 */
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 */
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);
100133 preempt_enable();
101
-
102
- rwsem_release(&sem->rw_sem.dep_map, 1, _RET_IP_);
103
-}
104
-
105
-static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
106
-{
107
- preempt_disable();
108
- percpu_up_read_preempt_enable(sem);
109134 }
110135
111136 extern void percpu_down_write(struct percpu_rw_semaphore *);
....@@ -114,7 +139,11 @@
114139 extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
115140 const char *, struct lock_class_key *);
116141
142
+/* Can't be called in atomic context. */
117143 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);
118147
119148 #define percpu_init_rwsem(sem) \
120149 ({ \
....@@ -122,29 +151,19 @@
122151 __percpu_init_rwsem(sem, #sem, &rwsem_key); \
123152 })
124153
125
-#define percpu_rwsem_is_held(sem) lockdep_is_held(&(sem)->rw_sem)
126
-
127
-#define percpu_rwsem_assert_held(sem) \
128
- 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)
129156
130157 static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
131158 bool read, unsigned long ip)
132159 {
133
- lock_release(&sem->rw_sem.dep_map, 1, ip);
134
-#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
135
- if (!read)
136
- sem->rw_sem.owner = RWSEM_OWNER_UNKNOWN;
137
-#endif
160
+ lock_release(&sem->dep_map, ip);
138161 }
139162
140163 static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
141164 bool read, unsigned long ip)
142165 {
143
- lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);
144
-#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
145
- if (!read)
146
- sem->rw_sem.owner = current;
147
-#endif
166
+ lock_acquire(&sem->dep_map, 0, 1, read, 1, NULL, ip);
148167 }
149168
150169 #endif