hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/include/linux/rwsem.h
....@@ -19,55 +19,55 @@
1919 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
2020 #include <linux/osq_lock.h>
2121 #endif
22
+#include <linux/android_vendor.h>
2223
23
-#ifdef CONFIG_PREEMPT_RT_FULL
24
-#include <linux/rwsem_rt.h>
25
-#else /* PREEMPT_RT_FULL */
26
-
27
-struct rw_semaphore;
28
-
29
-#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
30
-#include <linux/rwsem-spinlock.h> /* use a generic implementation */
31
-#define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE
32
-#else
33
-/* All arch specific implementations share the same struct */
24
+/*
25
+ * For an uncontended rwsem, count and owner are the only fields a task
26
+ * needs to touch when acquiring the rwsem. So they are put next to each
27
+ * other to increase the chance that they will share the same cacheline.
28
+ *
29
+ * In a contended rwsem, the owner is likely the most frequently accessed
30
+ * field in the structure as the optimistic waiter that holds the osq lock
31
+ * will spin on owner. For an embedded rwsem, other hot fields in the
32
+ * containing structure should be moved further away from the rwsem to
33
+ * reduce the chance that they will share the same cacheline causing
34
+ * cacheline bouncing problem.
35
+ */
3436 struct rw_semaphore {
3537 atomic_long_t count;
36
- struct list_head wait_list;
37
- raw_spinlock_t wait_lock;
38
+ /*
39
+ * Write owner or one of the read owners as well flags regarding
40
+ * the current state of the rwsem. Can be used as a speculative
41
+ * check to see if the write owner is running on the cpu.
42
+ */
43
+ atomic_long_t owner;
3844 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
3945 struct optimistic_spin_queue osq; /* spinner MCS lock */
40
- /*
41
- * Write owner. Used as a speculative check to see
42
- * if the owner is running on the cpu.
43
- */
44
- struct task_struct *owner;
46
+#endif
47
+ raw_spinlock_t wait_lock;
48
+ struct list_head wait_list;
49
+#ifdef CONFIG_DEBUG_RWSEMS
50
+ void *magic;
4551 #endif
4652 #ifdef CONFIG_DEBUG_LOCK_ALLOC
4753 struct lockdep_map dep_map;
4854 #endif
49
- /* NOTICE: m_count is a vendor variable used for the config
50
- * CONFIG_RWSEM_PRIO_AWARE. This is included here to maintain ABI
51
- * compatibility with our vendors */
52
- /* count for waiters preempt to queue in wait list */
53
- long m_count;
55
+ ANDROID_VENDOR_DATA(1);
56
+ ANDROID_OEM_DATA_ARRAY(1, 2);
5457 };
5558
56
-/*
57
- * Setting bit 0 of the owner field with other non-zero bits will indicate
58
- * that the rwsem is writer-owned with an unknown owner.
59
- */
60
-#define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-1L)
59
+enum rwsem_waiter_type {
60
+ RWSEM_WAITING_FOR_WRITE,
61
+ RWSEM_WAITING_FOR_READ
62
+};
6163
62
-extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
63
-extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem);
64
-extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
65
-extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
66
-extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
67
-extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
68
-
69
-/* Include the arch specific part */
70
-#include <asm/rwsem.h>
64
+struct rwsem_waiter {
65
+ struct list_head list;
66
+ struct task_struct *task;
67
+ enum rwsem_waiter_type type;
68
+ unsigned long timeout;
69
+ unsigned long last_rowner;
70
+};
7171
7272 /* In all implementations count != 0 means locked */
7373 static inline int rwsem_is_locked(struct rw_semaphore *sem)
....@@ -75,28 +75,40 @@
7575 return atomic_long_read(&sem->count) != 0;
7676 }
7777
78
-#define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
79
-#endif
78
+#define RWSEM_UNLOCKED_VALUE 0L
79
+#define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
8080
8181 /* Common initializer macros and functions */
8282
8383 #ifdef CONFIG_DEBUG_LOCK_ALLOC
84
-# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
84
+# define __RWSEM_DEP_MAP_INIT(lockname) \
85
+ .dep_map = { \
86
+ .name = #lockname, \
87
+ .wait_type_inner = LD_WAIT_SLEEP, \
88
+ },
8589 #else
8690 # define __RWSEM_DEP_MAP_INIT(lockname)
8791 #endif
8892
93
+#ifdef CONFIG_DEBUG_RWSEMS
94
+# define __RWSEM_DEBUG_INIT(lockname) .magic = &lockname,
95
+#else
96
+# define __RWSEM_DEBUG_INIT(lockname)
97
+#endif
98
+
8999 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
90
-#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
100
+#define __RWSEM_OPT_INIT(lockname) .osq = OSQ_LOCK_UNLOCKED,
91101 #else
92102 #define __RWSEM_OPT_INIT(lockname)
93103 #endif
94104
95105 #define __RWSEM_INITIALIZER(name) \
96
- { __RWSEM_INIT_COUNT(name), \
97
- .wait_list = LIST_HEAD_INIT((name).wait_list), \
98
- .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
106
+ { __RWSEM_COUNT_INIT(name), \
107
+ .owner = ATOMIC_LONG_INIT(0), \
99108 __RWSEM_OPT_INIT(name) \
109
+ .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
110
+ .wait_list = LIST_HEAD_INIT((name).wait_list), \
111
+ __RWSEM_DEBUG_INIT(name) \
100112 __RWSEM_DEP_MAP_INIT(name) }
101113
102114 #define DECLARE_RWSEM(name) \
....@@ -123,17 +135,11 @@
123135 return !list_empty(&sem->wait_list);
124136 }
125137
126
-#endif /* !PREEMPT_RT_FULL */
127
-
128
-/*
129
- * The functions below are the same for all rwsem implementations including
130
- * the RT specific variant.
131
- */
132
-
133138 /*
134139 * lock for reading
135140 */
136141 extern void down_read(struct rw_semaphore *sem);
142
+extern int __must_check down_read_interruptible(struct rw_semaphore *sem);
137143 extern int __must_check down_read_killable(struct rw_semaphore *sem);
138144
139145 /*
....@@ -179,9 +185,10 @@
179185 * static then another method for expressing nested locking is
180186 * the explicit definition of lock class keys and the use of
181187 * lockdep_set_class() at lock initialization time.
182
- * See Documentation/locking/lockdep-design.txt for more details.)
188
+ * See Documentation/locking/lockdep-design.rst for more details.)
183189 */
184190 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
191
+extern int __must_check down_read_killable_nested(struct rw_semaphore *sem, int subclass);
185192 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
186193 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
187194 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
....@@ -202,6 +209,7 @@
202209 extern void up_read_non_owner(struct rw_semaphore *sem);
203210 #else
204211 # define down_read_nested(sem, subclass) down_read(sem)
212
+# define down_read_killable_nested(sem, subclass) down_read_killable(sem)
205213 # define down_write_nest_lock(sem, nest_lock) down_write(sem)
206214 # define down_write_nested(sem, subclass) down_write(sem)
207215 # define down_write_killable_nested(sem, subclass) down_write_killable(sem)