hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/include/linux/rwsem.h
....@@ -16,54 +16,63 @@
1616 #include <linux/spinlock.h>
1717 #include <linux/atomic.h>
1818 #include <linux/err.h>
19
+
20
+#ifdef CONFIG_PREEMPT_RT
21
+#include <linux/rwsem-rt.h>
22
+#else /* PREEMPT_RT */
23
+
1924 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
2025 #include <linux/osq_lock.h>
2126 #endif
27
+#include <linux/android_vendor.h>
2228
23
-struct rw_semaphore;
24
-
25
-#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
26
-#include <linux/rwsem-spinlock.h> /* use a generic implementation */
27
-#define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE
28
-#else
29
-/* All arch specific implementations share the same struct */
29
+/*
30
+ * For an uncontended rwsem, count and owner are the only fields a task
31
+ * needs to touch when acquiring the rwsem. So they are put next to each
32
+ * other to increase the chance that they will share the same cacheline.
33
+ *
34
+ * In a contended rwsem, the owner is likely the most frequently accessed
35
+ * field in the structure as the optimistic waiter that holds the osq lock
36
+ * will spin on owner. For an embedded rwsem, other hot fields in the
37
+ * containing structure should be moved further away from the rwsem to
38
+ * reduce the chance that they will share the same cacheline causing
39
+ * cacheline bouncing problem.
40
+ */
3041 struct rw_semaphore {
3142 atomic_long_t count;
32
- struct list_head wait_list;
33
- raw_spinlock_t wait_lock;
43
+ /*
44
+ * Write owner or one of the read owners as well flags regarding
45
+ * the current state of the rwsem. Can be used as a speculative
46
+ * check to see if the write owner is running on the cpu.
47
+ */
48
+ atomic_long_t owner;
3449 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
3550 struct optimistic_spin_queue osq; /* spinner MCS lock */
36
- /*
37
- * Write owner. Used as a speculative check to see
38
- * if the owner is running on the cpu.
39
- */
40
- struct task_struct *owner;
51
+#endif
52
+ raw_spinlock_t wait_lock;
53
+ struct list_head wait_list;
54
+#ifdef CONFIG_DEBUG_RWSEMS
55
+ void *magic;
4156 #endif
4257 #ifdef CONFIG_DEBUG_LOCK_ALLOC
4358 struct lockdep_map dep_map;
4459 #endif
45
- /* NOTICE: m_count is a vendor variable used for the config
46
- * CONFIG_RWSEM_PRIO_AWARE. This is included here to maintain ABI
47
- * compatibility with our vendors */
48
- /* count for waiters preempt to queue in wait list */
49
- long m_count;
60
+ ANDROID_VENDOR_DATA(1);
61
+ ANDROID_OEM_DATA_ARRAY(1, 2);
5062 };
5163
52
-/*
53
- * Setting bit 0 of the owner field with other non-zero bits will indicate
54
- * that the rwsem is writer-owned with an unknown owner.
55
- */
56
-#define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-1L)
64
+enum rwsem_waiter_type {
65
+ RWSEM_WAITING_FOR_WRITE,
66
+ RWSEM_WAITING_FOR_READ
67
+};
5768
58
-extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
59
-extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem);
60
-extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
61
-extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
62
-extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
63
-extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
64
-
65
-/* Include the arch specific part */
66
-#include <asm/rwsem.h>
69
+struct rwsem_waiter {
70
+ struct list_head list;
71
+ struct task_struct *task;
72
+ enum rwsem_waiter_type type;
73
+ unsigned long timeout;
74
+ unsigned long last_rowner;
75
+};
6776
6877 /* In all implementations count != 0 means locked */
6978 static inline int rwsem_is_locked(struct rw_semaphore *sem)
....@@ -71,28 +80,40 @@
7180 return atomic_long_read(&sem->count) != 0;
7281 }
7382
74
-#define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
75
-#endif
83
+#define RWSEM_UNLOCKED_VALUE 0L
84
+#define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
7685
7786 /* Common initializer macros and functions */
7887
7988 #ifdef CONFIG_DEBUG_LOCK_ALLOC
80
-# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
89
+# define __RWSEM_DEP_MAP_INIT(lockname) \
90
+ .dep_map = { \
91
+ .name = #lockname, \
92
+ .wait_type_inner = LD_WAIT_SLEEP, \
93
+ },
8194 #else
8295 # define __RWSEM_DEP_MAP_INIT(lockname)
8396 #endif
8497
98
+#ifdef CONFIG_DEBUG_RWSEMS
99
+# define __RWSEM_DEBUG_INIT(lockname) .magic = &lockname,
100
+#else
101
+# define __RWSEM_DEBUG_INIT(lockname)
102
+#endif
103
+
85104 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
86
-#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
105
+#define __RWSEM_OPT_INIT(lockname) .osq = OSQ_LOCK_UNLOCKED,
87106 #else
88107 #define __RWSEM_OPT_INIT(lockname)
89108 #endif
90109
91110 #define __RWSEM_INITIALIZER(name) \
92
- { __RWSEM_INIT_COUNT(name), \
93
- .wait_list = LIST_HEAD_INIT((name).wait_list), \
94
- .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
111
+ { __RWSEM_COUNT_INIT(name), \
112
+ .owner = ATOMIC_LONG_INIT(0), \
95113 __RWSEM_OPT_INIT(name) \
114
+ .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
115
+ .wait_list = LIST_HEAD_INIT((name).wait_list), \
116
+ __RWSEM_DEBUG_INIT(name) \
96117 __RWSEM_DEP_MAP_INIT(name) }
97118
98119 #define DECLARE_RWSEM(name) \
....@@ -119,10 +140,18 @@
119140 return !list_empty(&sem->wait_list);
120141 }
121142
143
+#endif /* !PREEMPT_RT */
144
+
145
+/*
146
+ * The functions below are the same for all rwsem implementations including
147
+ * the RT specific variant.
148
+ */
149
+
122150 /*
123151 * lock for reading
124152 */
125153 extern void down_read(struct rw_semaphore *sem);
154
+extern int __must_check down_read_interruptible(struct rw_semaphore *sem);
126155 extern int __must_check down_read_killable(struct rw_semaphore *sem);
127156
128157 /*
....@@ -168,9 +197,10 @@
168197 * static then another method for expressing nested locking is
169198 * the explicit definition of lock class keys and the use of
170199 * lockdep_set_class() at lock initialization time.
171
- * See Documentation/locking/lockdep-design.txt for more details.)
200
+ * See Documentation/locking/lockdep-design.rst for more details.)
172201 */
173202 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
203
+extern int __must_check down_read_killable_nested(struct rw_semaphore *sem, int subclass);
174204 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
175205 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
176206 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
....@@ -191,6 +221,7 @@
191221 extern void up_read_non_owner(struct rw_semaphore *sem);
192222 #else
193223 # define down_read_nested(sem, subclass) down_read(sem)
224
+# define down_read_killable_nested(sem, subclass) down_read_killable(sem)
194225 # define down_write_nest_lock(sem, nest_lock) down_write(sem)
195226 # define down_write_nested(sem, subclass) down_write(sem)
196227 # define down_write_killable_nested(sem, subclass) down_write_killable(sem)