hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/kernel/locking/rtmutex.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * RT-Mutexes: simple blocking mutual exclusion locks with PI support
34 *
....@@ -7,8 +8,13 @@
78 * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
89 * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
910 * Copyright (C) 2006 Esben Nielsen
11
+ * Adaptive Spinlocks:
12
+ * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
13
+ * and Peter Morreale,
14
+ * Adaptive Spinlocks simplification:
15
+ * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
1016 *
11
- * See Documentation/locking/rt-mutex-design.txt for details.
17
+ * See Documentation/locking/rt-mutex-design.rst for details.
1218 */
1319 #include <linux/spinlock.h>
1420 #include <linux/export.h>
....@@ -18,6 +24,8 @@
1824 #include <linux/sched/wake_q.h>
1925 #include <linux/sched/debug.h>
2026 #include <linux/timer.h>
27
+#include <trace/hooks/dtask.h>
28
+#include <linux/ww_mutex.h>
2129
2230 #include "rtmutex_common.h"
2331
....@@ -56,7 +64,7 @@
5664 if (rt_mutex_has_waiters(lock))
5765 val |= RT_MUTEX_HAS_WAITERS;
5866
59
- lock->owner = (struct task_struct *)val;
67
+ WRITE_ONCE(lock->owner, (struct task_struct *)val);
6068 }
6169
6270 static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
....@@ -135,12 +143,17 @@
135143 WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
136144 }
137145
146
+static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
147
+{
148
+ return waiter && waiter != PI_WAKEUP_INPROGRESS &&
149
+ waiter != PI_REQUEUE_INPROGRESS;
150
+}
151
+
138152 /*
139153 * We can speed up the acquire/release, if there's no debugging state to be
140154 * set up.
141155 */
142156 #ifndef CONFIG_DEBUG_RT_MUTEXES
143
-# define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
144157 # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
145158 # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
146159
....@@ -201,7 +214,6 @@
201214 }
202215
203216 #else
204
-# define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
205217 # define rt_mutex_cmpxchg_acquire(l,c,n) (0)
206218 # define rt_mutex_cmpxchg_release(l,c,n) (0)
207219
....@@ -228,7 +240,7 @@
228240 * Only use with rt_mutex_waiter_{less,equal}()
229241 */
230242 #define task_to_waiter(p) \
231
- &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
243
+ &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline, .task = (p) }
232244
233245 static inline int
234246 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
....@@ -266,6 +278,27 @@
266278 return left->deadline == right->deadline;
267279
268280 return 1;
281
+}
282
+
283
+#define STEAL_NORMAL 0
284
+#define STEAL_LATERAL 1
285
+
286
+static inline int
287
+rt_mutex_steal(struct rt_mutex *lock, struct rt_mutex_waiter *waiter, int mode)
288
+{
289
+ struct rt_mutex_waiter *top_waiter = rt_mutex_top_waiter(lock);
290
+
291
+ if (waiter == top_waiter || rt_mutex_waiter_less(waiter, top_waiter))
292
+ return 1;
293
+
294
+ /*
295
+ * Note that RT tasks are excluded from lateral-steals
296
+ * to prevent the introduction of an unbounded latency.
297
+ */
298
+ if (mode == STEAL_NORMAL || rt_task(waiter->task))
299
+ return 0;
300
+
301
+ return rt_mutex_waiter_equal(waiter, top_waiter);
269302 }
270303
271304 static void
....@@ -372,6 +405,14 @@
372405 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
373406 }
374407
408
+static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
409
+{
410
+ if (waiter->savestate)
411
+ wake_up_lock_sleeper(waiter->task);
412
+ else
413
+ wake_up_process(waiter->task);
414
+}
415
+
375416 /*
376417 * Max number of times we'll walk the boosting chain:
377418 */
....@@ -379,7 +420,8 @@
379420
380421 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
381422 {
382
- return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
423
+ return rt_mutex_real_waiter(p->pi_blocked_on) ?
424
+ p->pi_blocked_on->lock : NULL;
383425 }
384426
385427 /*
....@@ -515,7 +557,7 @@
515557 * reached or the state of the chain has changed while we
516558 * dropped the locks.
517559 */
518
- if (!waiter)
560
+ if (!rt_mutex_real_waiter(waiter))
519561 goto out_unlock_pi;
520562
521563 /*
....@@ -598,7 +640,6 @@
598640 * walk, we detected a deadlock.
599641 */
600642 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
601
- debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
602643 raw_spin_unlock(&lock->wait_lock);
603644 ret = -EDEADLK;
604645 goto out_unlock_pi;
....@@ -627,8 +668,7 @@
627668 }
628669
629670 /* [10] Grab the next task, i.e. owner of @lock */
630
- task = rt_mutex_owner(lock);
631
- get_task_struct(task);
671
+ task = get_task_struct(rt_mutex_owner(lock));
632672 raw_spin_lock(&task->pi_lock);
633673
634674 /*
....@@ -696,20 +736,22 @@
696736 * follow here. This is the end of the chain we are walking.
697737 */
698738 if (!rt_mutex_owner(lock)) {
739
+ struct rt_mutex_waiter *lock_top_waiter;
740
+
699741 /*
700742 * If the requeue [7] above changed the top waiter,
701743 * then we need to wake the new top waiter up to try
702744 * to get the lock.
703745 */
704
- if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
705
- wake_up_process(rt_mutex_top_waiter(lock)->task);
746
+ lock_top_waiter = rt_mutex_top_waiter(lock);
747
+ if (prerequeue_top_waiter != lock_top_waiter)
748
+ rt_mutex_wake_waiter(lock_top_waiter);
706749 raw_spin_unlock_irq(&lock->wait_lock);
707750 return 0;
708751 }
709752
710753 /* [10] Grab the next task, i.e. the owner of @lock */
711
- task = rt_mutex_owner(lock);
712
- get_task_struct(task);
754
+ task = get_task_struct(rt_mutex_owner(lock));
713755 raw_spin_lock(&task->pi_lock);
714756
715757 /* [11] requeue the pi waiters if necessary */
....@@ -804,9 +846,11 @@
804846 * @task: The task which wants to acquire the lock
805847 * @waiter: The waiter that is queued to the lock's wait tree if the
806848 * callsite called task_blocked_on_lock(), otherwise NULL
849
+ * @mode: Lock steal mode (STEAL_NORMAL, STEAL_LATERAL)
807850 */
808
-static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
809
- struct rt_mutex_waiter *waiter)
851
+static int __try_to_take_rt_mutex(struct rt_mutex *lock,
852
+ struct task_struct *task,
853
+ struct rt_mutex_waiter *waiter, int mode)
810854 {
811855 lockdep_assert_held(&lock->wait_lock);
812856
....@@ -842,12 +886,11 @@
842886 */
843887 if (waiter) {
844888 /*
845
- * If waiter is not the highest priority waiter of
846
- * @lock, give up.
889
+ * If waiter is not the highest priority waiter of @lock,
890
+ * or its peer when lateral steal is allowed, give up.
847891 */
848
- if (waiter != rt_mutex_top_waiter(lock))
892
+ if (!rt_mutex_steal(lock, waiter, mode))
849893 return 0;
850
-
851894 /*
852895 * We can acquire the lock. Remove the waiter from the
853896 * lock waiters tree.
....@@ -865,14 +908,12 @@
865908 */
866909 if (rt_mutex_has_waiters(lock)) {
867910 /*
868
- * If @task->prio is greater than or equal to
869
- * the top waiter priority (kernel view),
870
- * @task lost.
911
+ * If @task->prio is greater than the top waiter
912
+ * priority (kernel view), or equal to it when a
913
+ * lateral steal is forbidden, @task lost.
871914 */
872
- if (!rt_mutex_waiter_less(task_to_waiter(task),
873
- rt_mutex_top_waiter(lock)))
915
+ if (!rt_mutex_steal(lock, task_to_waiter(task), mode))
874916 return 0;
875
-
876917 /*
877918 * The current top waiter stays enqueued. We
878919 * don't have to change anything in the lock
....@@ -919,6 +960,329 @@
919960 return 1;
920961 }
921962
963
+#ifdef CONFIG_PREEMPT_RT
964
+/*
965
+ * preemptible spin_lock functions:
966
+ */
967
+static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
968
+ void (*slowfn)(struct rt_mutex *lock))
969
+{
970
+ might_sleep_no_state_check();
971
+
972
+ if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
973
+ return;
974
+ else
975
+ slowfn(lock);
976
+}
977
+
978
+static inline void rt_spin_lock_fastunlock(struct rt_mutex *lock,
979
+ void (*slowfn)(struct rt_mutex *lock))
980
+{
981
+ if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
982
+ return;
983
+ else
984
+ slowfn(lock);
985
+}
986
+#ifdef CONFIG_SMP
987
+/*
988
+ * Note that owner is a speculative pointer and dereferencing relies
989
+ * on rcu_read_lock() and the check against the lock owner.
990
+ */
991
+static int adaptive_wait(struct rt_mutex *lock,
992
+ struct task_struct *owner)
993
+{
994
+ int res = 0;
995
+
996
+ rcu_read_lock();
997
+ for (;;) {
998
+ if (owner != rt_mutex_owner(lock))
999
+ break;
1000
+ /*
1001
+ * Ensure that owner->on_cpu is dereferenced _after_
1002
+ * checking the above to be valid.
1003
+ */
1004
+ barrier();
1005
+ if (!owner->on_cpu) {
1006
+ res = 1;
1007
+ break;
1008
+ }
1009
+ cpu_relax();
1010
+ }
1011
+ rcu_read_unlock();
1012
+ return res;
1013
+}
1014
+#else
1015
+static int adaptive_wait(struct rt_mutex *lock,
1016
+ struct task_struct *orig_owner)
1017
+{
1018
+ return 1;
1019
+}
1020
+#endif
1021
+
1022
+static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
1023
+ struct rt_mutex_waiter *waiter,
1024
+ struct task_struct *task,
1025
+ enum rtmutex_chainwalk chwalk);
1026
+/*
1027
+ * Slow path lock function spin_lock style: this variant is very
1028
+ * careful not to miss any non-lock wakeups.
1029
+ *
1030
+ * We store the current state under p->pi_lock in p->saved_state and
1031
+ * the try_to_wake_up() code handles this accordingly.
1032
+ */
1033
+void __sched rt_spin_lock_slowlock_locked(struct rt_mutex *lock,
1034
+ struct rt_mutex_waiter *waiter,
1035
+ unsigned long flags)
1036
+{
1037
+ struct task_struct *lock_owner, *self = current;
1038
+ struct rt_mutex_waiter *top_waiter;
1039
+ int ret;
1040
+
1041
+ if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL))
1042
+ return;
1043
+
1044
+ BUG_ON(rt_mutex_owner(lock) == self);
1045
+
1046
+ /*
1047
+ * We save whatever state the task is in and we'll restore it
1048
+ * after acquiring the lock taking real wakeups into account
1049
+ * as well. We are serialized via pi_lock against wakeups. See
1050
+ * try_to_wake_up().
1051
+ */
1052
+ raw_spin_lock(&self->pi_lock);
1053
+ self->saved_state = self->state;
1054
+ __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
1055
+ raw_spin_unlock(&self->pi_lock);
1056
+
1057
+ ret = task_blocks_on_rt_mutex(lock, waiter, self, RT_MUTEX_MIN_CHAINWALK);
1058
+ BUG_ON(ret);
1059
+
1060
+ for (;;) {
1061
+ /* Try to acquire the lock again. */
1062
+ if (__try_to_take_rt_mutex(lock, self, waiter, STEAL_LATERAL))
1063
+ break;
1064
+
1065
+ top_waiter = rt_mutex_top_waiter(lock);
1066
+ lock_owner = rt_mutex_owner(lock);
1067
+
1068
+ raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1069
+
1070
+ if (top_waiter != waiter || adaptive_wait(lock, lock_owner))
1071
+ preempt_schedule_lock();
1072
+
1073
+ raw_spin_lock_irqsave(&lock->wait_lock, flags);
1074
+
1075
+ raw_spin_lock(&self->pi_lock);
1076
+ __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
1077
+ raw_spin_unlock(&self->pi_lock);
1078
+ }
1079
+
1080
+ /*
1081
+ * Restore the task state to current->saved_state. We set it
1082
+ * to the original state above and the try_to_wake_up() code
1083
+ * has possibly updated it when a real (non-rtmutex) wakeup
1084
+ * happened while we were blocked. Clear saved_state so
1085
+ * try_to_wakeup() does not get confused.
1086
+ */
1087
+ raw_spin_lock(&self->pi_lock);
1088
+ __set_current_state_no_track(self->saved_state);
1089
+ self->saved_state = TASK_RUNNING;
1090
+ raw_spin_unlock(&self->pi_lock);
1091
+
1092
+ /*
1093
+ * try_to_take_rt_mutex() sets the waiter bit
1094
+ * unconditionally. We might have to fix that up:
1095
+ */
1096
+ fixup_rt_mutex_waiters(lock);
1097
+
1098
+ BUG_ON(rt_mutex_has_waiters(lock) && waiter == rt_mutex_top_waiter(lock));
1099
+ BUG_ON(!RB_EMPTY_NODE(&waiter->tree_entry));
1100
+}
1101
+
1102
+static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock)
1103
+{
1104
+ struct rt_mutex_waiter waiter;
1105
+ unsigned long flags;
1106
+
1107
+ rt_mutex_init_waiter(&waiter, true);
1108
+
1109
+ raw_spin_lock_irqsave(&lock->wait_lock, flags);
1110
+ rt_spin_lock_slowlock_locked(lock, &waiter, flags);
1111
+ raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1112
+ debug_rt_mutex_free_waiter(&waiter);
1113
+}
1114
+
1115
+static bool __sched __rt_mutex_unlock_common(struct rt_mutex *lock,
1116
+ struct wake_q_head *wake_q,
1117
+ struct wake_q_head *wq_sleeper);
1118
+/*
1119
+ * Slow path to release a rt_mutex spin_lock style
1120
+ */
1121
+void __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
1122
+{
1123
+ unsigned long flags;
1124
+ DEFINE_WAKE_Q(wake_q);
1125
+ DEFINE_WAKE_Q(wake_sleeper_q);
1126
+ bool postunlock;
1127
+
1128
+ raw_spin_lock_irqsave(&lock->wait_lock, flags);
1129
+ postunlock = __rt_mutex_unlock_common(lock, &wake_q, &wake_sleeper_q);
1130
+ raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1131
+
1132
+ if (postunlock)
1133
+ rt_mutex_postunlock(&wake_q, &wake_sleeper_q);
1134
+}
1135
+
1136
+void __lockfunc rt_spin_lock(spinlock_t *lock)
1137
+{
1138
+ spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1139
+ rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock);
1140
+ rcu_read_lock();
1141
+ migrate_disable();
1142
+}
1143
+EXPORT_SYMBOL(rt_spin_lock);
1144
+
1145
+void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
1146
+{
1147
+ rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock);
1148
+}
1149
+
1150
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
1151
+void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
1152
+{
1153
+ spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
1154
+ rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock);
1155
+ rcu_read_lock();
1156
+ migrate_disable();
1157
+}
1158
+EXPORT_SYMBOL(rt_spin_lock_nested);
1159
+
1160
+void __lockfunc rt_spin_lock_nest_lock(spinlock_t *lock,
1161
+ struct lockdep_map *nest_lock)
1162
+{
1163
+ spin_acquire_nest(&lock->dep_map, 0, 0, nest_lock, _RET_IP_);
1164
+ rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock);
1165
+ rcu_read_lock();
1166
+ migrate_disable();
1167
+}
1168
+EXPORT_SYMBOL(rt_spin_lock_nest_lock);
1169
+#endif
1170
+
1171
+void __lockfunc rt_spin_unlock(spinlock_t *lock)
1172
+{
1173
+ /* NOTE: we always pass in '1' for nested, for simplicity */
1174
+ spin_release(&lock->dep_map, _RET_IP_);
1175
+ migrate_enable();
1176
+ rcu_read_unlock();
1177
+ rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
1178
+}
1179
+EXPORT_SYMBOL(rt_spin_unlock);
1180
+
1181
+void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
1182
+{
1183
+ rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
1184
+}
1185
+EXPORT_SYMBOL(__rt_spin_unlock);
1186
+
1187
+/*
1188
+ * Wait for the lock to get unlocked: instead of polling for an unlock
1189
+ * (like raw spinlocks do), we lock and unlock, to force the kernel to
1190
+ * schedule if there's contention:
1191
+ */
1192
+void __lockfunc rt_spin_lock_unlock(spinlock_t *lock)
1193
+{
1194
+ spin_lock(lock);
1195
+ spin_unlock(lock);
1196
+}
1197
+EXPORT_SYMBOL(rt_spin_lock_unlock);
1198
+
1199
+int __lockfunc rt_spin_trylock(spinlock_t *lock)
1200
+{
1201
+ int ret;
1202
+
1203
+ ret = __rt_mutex_trylock(&lock->lock);
1204
+ if (ret) {
1205
+ spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1206
+ rcu_read_lock();
1207
+ migrate_disable();
1208
+ }
1209
+ return ret;
1210
+}
1211
+EXPORT_SYMBOL(rt_spin_trylock);
1212
+
1213
+int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
1214
+{
1215
+ int ret;
1216
+
1217
+ local_bh_disable();
1218
+ ret = __rt_mutex_trylock(&lock->lock);
1219
+ if (ret) {
1220
+ spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1221
+ rcu_read_lock();
1222
+ migrate_disable();
1223
+ } else {
1224
+ local_bh_enable();
1225
+ }
1226
+ return ret;
1227
+}
1228
+EXPORT_SYMBOL(rt_spin_trylock_bh);
1229
+
1230
+void
1231
+__rt_spin_lock_init(spinlock_t *lock, const char *name, struct lock_class_key *key)
1232
+{
1233
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
1234
+ /*
1235
+ * Make sure we are not reinitializing a held lock:
1236
+ */
1237
+ debug_check_no_locks_freed((void *)lock, sizeof(*lock));
1238
+ lockdep_init_map(&lock->dep_map, name, key, 0);
1239
+#endif
1240
+}
1241
+EXPORT_SYMBOL(__rt_spin_lock_init);
1242
+
1243
+#endif /* PREEMPT_RT */
1244
+
1245
+#ifdef CONFIG_PREEMPT_RT
1246
+ static inline int __sched
1247
+__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
1248
+{
1249
+ struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
1250
+ struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);
1251
+
1252
+ if (!hold_ctx)
1253
+ return 0;
1254
+
1255
+ if (unlikely(ctx == hold_ctx))
1256
+ return -EALREADY;
1257
+
1258
+ if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
1259
+ (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
1260
+#ifdef CONFIG_DEBUG_MUTEXES
1261
+ DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
1262
+ ctx->contending_lock = ww;
1263
+#endif
1264
+ return -EDEADLK;
1265
+ }
1266
+
1267
+ return 0;
1268
+}
1269
+#else
1270
+ static inline int __sched
1271
+__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
1272
+{
1273
+ BUG();
1274
+ return 0;
1275
+}
1276
+
1277
+#endif
1278
+
1279
+static inline int
1280
+try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
1281
+ struct rt_mutex_waiter *waiter)
1282
+{
1283
+ return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
1284
+}
1285
+
9221286 /*
9231287 * Task blocks on lock.
9241288 *
....@@ -951,6 +1315,22 @@
9511315 return -EDEADLK;
9521316
9531317 raw_spin_lock(&task->pi_lock);
1318
+ /*
1319
+ * In the case of futex requeue PI, this will be a proxy
1320
+ * lock. The task will wake unaware that it is enqueueed on
1321
+ * this lock. Avoid blocking on two locks and corrupting
1322
+ * pi_blocked_on via the PI_WAKEUP_INPROGRESS
1323
+ * flag. futex_wait_requeue_pi() sets this when it wakes up
1324
+ * before requeue (due to a signal or timeout). Do not enqueue
1325
+ * the task if PI_WAKEUP_INPROGRESS is set.
1326
+ */
1327
+ if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
1328
+ raw_spin_unlock(&task->pi_lock);
1329
+ return -EAGAIN;
1330
+ }
1331
+
1332
+ BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
1333
+
9541334 waiter->task = task;
9551335 waiter->lock = lock;
9561336 waiter->prio = task->prio;
....@@ -974,7 +1354,7 @@
9741354 rt_mutex_enqueue_pi(owner, waiter);
9751355
9761356 rt_mutex_adjust_prio(owner);
977
- if (owner->pi_blocked_on)
1357
+ if (rt_mutex_real_waiter(owner->pi_blocked_on))
9781358 chain_walk = 1;
9791359 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
9801360 chain_walk = 1;
....@@ -1016,6 +1396,7 @@
10161396 * Called with lock->wait_lock held and interrupts disabled.
10171397 */
10181398 static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
1399
+ struct wake_q_head *wake_sleeper_q,
10191400 struct rt_mutex *lock)
10201401 {
10211402 struct rt_mutex_waiter *waiter;
....@@ -1055,7 +1436,10 @@
10551436 * Pairs with preempt_enable() in rt_mutex_postunlock();
10561437 */
10571438 preempt_disable();
1058
- wake_q_add(wake_q, waiter->task);
1439
+ if (waiter->savestate)
1440
+ wake_q_add_sleeper(wake_sleeper_q, waiter->task);
1441
+ else
1442
+ wake_q_add(wake_q, waiter->task);
10591443 raw_spin_unlock(&current->pi_lock);
10601444 }
10611445
....@@ -1070,7 +1454,7 @@
10701454 {
10711455 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
10721456 struct task_struct *owner = rt_mutex_owner(lock);
1073
- struct rt_mutex *next_lock;
1457
+ struct rt_mutex *next_lock = NULL;
10741458
10751459 lockdep_assert_held(&lock->wait_lock);
10761460
....@@ -1096,7 +1480,8 @@
10961480 rt_mutex_adjust_prio(owner);
10971481
10981482 /* Store the lock on which owner is blocked or NULL */
1099
- next_lock = task_blocked_on_lock(owner);
1483
+ if (rt_mutex_real_waiter(owner->pi_blocked_on))
1484
+ next_lock = task_blocked_on_lock(owner);
11001485
11011486 raw_spin_unlock(&owner->pi_lock);
11021487
....@@ -1132,26 +1517,28 @@
11321517 raw_spin_lock_irqsave(&task->pi_lock, flags);
11331518
11341519 waiter = task->pi_blocked_on;
1135
- if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
1520
+ if (!rt_mutex_real_waiter(waiter) ||
1521
+ rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
11361522 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
11371523 return;
11381524 }
11391525 next_lock = waiter->lock;
1140
- raw_spin_unlock_irqrestore(&task->pi_lock, flags);
11411526
11421527 /* gets dropped in rt_mutex_adjust_prio_chain()! */
11431528 get_task_struct(task);
11441529
1530
+ raw_spin_unlock_irqrestore(&task->pi_lock, flags);
11451531 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
11461532 next_lock, NULL, task);
11471533 }
11481534
1149
-void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
1535
+void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
11501536 {
11511537 debug_rt_mutex_init_waiter(waiter);
11521538 RB_CLEAR_NODE(&waiter->pi_tree_entry);
11531539 RB_CLEAR_NODE(&waiter->tree_entry);
11541540 waiter->task = NULL;
1541
+ waiter->savestate = savestate;
11551542 }
11561543
11571544 /**
....@@ -1167,32 +1554,33 @@
11671554 static int __sched
11681555 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
11691556 struct hrtimer_sleeper *timeout,
1170
- struct rt_mutex_waiter *waiter)
1557
+ struct rt_mutex_waiter *waiter,
1558
+ struct ww_acquire_ctx *ww_ctx)
11711559 {
11721560 int ret = 0;
11731561
1562
+ trace_android_vh_rtmutex_wait_start(lock);
11741563 for (;;) {
11751564 /* Try to acquire the lock: */
11761565 if (try_to_take_rt_mutex(lock, current, waiter))
11771566 break;
11781567
1179
- /*
1180
- * TASK_INTERRUPTIBLE checks for signals and
1181
- * timeout. Ignored otherwise.
1182
- */
1183
- if (likely(state == TASK_INTERRUPTIBLE)) {
1184
- /* Signal pending? */
1185
- if (signal_pending(current))
1186
- ret = -EINTR;
1187
- if (timeout && !timeout->task)
1188
- ret = -ETIMEDOUT;
1568
+ if (timeout && !timeout->task) {
1569
+ ret = -ETIMEDOUT;
1570
+ break;
1571
+ }
1572
+ if (signal_pending_state(state, current)) {
1573
+ ret = -EINTR;
1574
+ break;
1575
+ }
1576
+
1577
+ if (ww_ctx && ww_ctx->acquired > 0) {
1578
+ ret = __mutex_lock_check_stamp(lock, ww_ctx);
11891579 if (ret)
11901580 break;
11911581 }
11921582
11931583 raw_spin_unlock_irq(&lock->wait_lock);
1194
-
1195
- debug_rt_mutex_print_deadlock(waiter);
11961584
11971585 schedule();
11981586
....@@ -1200,6 +1588,7 @@
12001588 set_current_state(state);
12011589 }
12021590
1591
+ trace_android_vh_rtmutex_wait_finish(lock);
12031592 __set_current_state(TASK_RUNNING);
12041593 return ret;
12051594 }
....@@ -1214,14 +1603,147 @@
12141603 if (res != -EDEADLOCK || detect_deadlock)
12151604 return;
12161605
1217
- /*
1218
- * Yell lowdly and stop the task right here.
1219
- */
1220
- rt_mutex_print_deadlock(w);
12211606 while (1) {
12221607 set_current_state(TASK_INTERRUPTIBLE);
12231608 schedule();
12241609 }
1610
+}
1611
+
1612
+static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
1613
+ struct ww_acquire_ctx *ww_ctx)
1614
+{
1615
+#ifdef CONFIG_DEBUG_MUTEXES
1616
+ /*
1617
+ * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
1618
+ * but released with a normal mutex_unlock in this call.
1619
+ *
1620
+ * This should never happen, always use ww_mutex_unlock.
1621
+ */
1622
+ DEBUG_LOCKS_WARN_ON(ww->ctx);
1623
+
1624
+ /*
1625
+ * Not quite done after calling ww_acquire_done() ?
1626
+ */
1627
+ DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
1628
+
1629
+ if (ww_ctx->contending_lock) {
1630
+ /*
1631
+ * After -EDEADLK you tried to
1632
+ * acquire a different ww_mutex? Bad!
1633
+ */
1634
+ DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
1635
+
1636
+ /*
1637
+ * You called ww_mutex_lock after receiving -EDEADLK,
1638
+ * but 'forgot' to unlock everything else first?
1639
+ */
1640
+ DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
1641
+ ww_ctx->contending_lock = NULL;
1642
+ }
1643
+
1644
+ /*
1645
+ * Naughty, using a different class will lead to undefined behavior!
1646
+ */
1647
+ DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
1648
+#endif
1649
+ ww_ctx->acquired++;
1650
+}
1651
+
1652
+#ifdef CONFIG_PREEMPT_RT
1653
+static void ww_mutex_account_lock(struct rt_mutex *lock,
1654
+ struct ww_acquire_ctx *ww_ctx)
1655
+{
1656
+ struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
1657
+ struct rt_mutex_waiter *waiter, *n;
1658
+
1659
+ /*
1660
+ * This branch gets optimized out for the common case,
1661
+ * and is only important for ww_mutex_lock.
1662
+ */
1663
+ ww_mutex_lock_acquired(ww, ww_ctx);
1664
+ ww->ctx = ww_ctx;
1665
+
1666
+ /*
1667
+ * Give any possible sleeping processes the chance to wake up,
1668
+ * so they can recheck if they have to back off.
1669
+ */
1670
+ rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters.rb_root,
1671
+ tree_entry) {
1672
+ /* XXX debug rt mutex waiter wakeup */
1673
+
1674
+ BUG_ON(waiter->lock != lock);
1675
+ rt_mutex_wake_waiter(waiter);
1676
+ }
1677
+}
1678
+
1679
+#else
1680
+
1681
+static void ww_mutex_account_lock(struct rt_mutex *lock,
1682
+ struct ww_acquire_ctx *ww_ctx)
1683
+{
1684
+ BUG();
1685
+}
1686
+#endif
1687
+
1688
+int __sched rt_mutex_slowlock_locked(struct rt_mutex *lock, int state,
1689
+ struct hrtimer_sleeper *timeout,
1690
+ enum rtmutex_chainwalk chwalk,
1691
+ struct ww_acquire_ctx *ww_ctx,
1692
+ struct rt_mutex_waiter *waiter)
1693
+{
1694
+ int ret;
1695
+
1696
+#ifdef CONFIG_PREEMPT_RT
1697
+ if (ww_ctx) {
1698
+ struct ww_mutex *ww;
1699
+
1700
+ ww = container_of(lock, struct ww_mutex, base.lock);
1701
+ if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
1702
+ return -EALREADY;
1703
+ }
1704
+#endif
1705
+
1706
+ /* Try to acquire the lock again: */
1707
+ if (try_to_take_rt_mutex(lock, current, NULL)) {
1708
+ if (ww_ctx)
1709
+ ww_mutex_account_lock(lock, ww_ctx);
1710
+ return 0;
1711
+ }
1712
+
1713
+ set_current_state(state);
1714
+
1715
+ /* Setup the timer, when timeout != NULL */
1716
+ if (unlikely(timeout))
1717
+ hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
1718
+
1719
+ ret = task_blocks_on_rt_mutex(lock, waiter, current, chwalk);
1720
+
1721
+ if (likely(!ret)) {
1722
+ /* sleep on the mutex */
1723
+ ret = __rt_mutex_slowlock(lock, state, timeout, waiter,
1724
+ ww_ctx);
1725
+ } else if (ww_ctx) {
1726
+ /* ww_mutex received EDEADLK, let it become EALREADY */
1727
+ ret = __mutex_lock_check_stamp(lock, ww_ctx);
1728
+ BUG_ON(!ret);
1729
+ }
1730
+
1731
+ if (unlikely(ret)) {
1732
+ __set_current_state(TASK_RUNNING);
1733
+ remove_waiter(lock, waiter);
1734
+ /* ww_mutex wants to report EDEADLK/EALREADY, let it */
1735
+ if (!ww_ctx)
1736
+ rt_mutex_handle_deadlock(ret, chwalk, waiter);
1737
+ } else if (ww_ctx) {
1738
+ ww_mutex_account_lock(lock, ww_ctx);
1739
+ }
1740
+
1741
+ /*
1742
+ * try_to_take_rt_mutex() sets the waiter bit
1743
+ * unconditionally. We might have to fix that up.
1744
+ */
1745
+ fixup_rt_mutex_waiters(lock);
1746
+ return ret;
12251747 }
12261748
12271749 /*
....@@ -1230,13 +1752,14 @@
12301752 static int __sched
12311753 rt_mutex_slowlock(struct rt_mutex *lock, int state,
12321754 struct hrtimer_sleeper *timeout,
1233
- enum rtmutex_chainwalk chwalk)
1755
+ enum rtmutex_chainwalk chwalk,
1756
+ struct ww_acquire_ctx *ww_ctx)
12341757 {
12351758 struct rt_mutex_waiter waiter;
12361759 unsigned long flags;
12371760 int ret = 0;
12381761
1239
- rt_mutex_init_waiter(&waiter);
1762
+ rt_mutex_init_waiter(&waiter, false);
12401763
12411764 /*
12421765 * Technically we could use raw_spin_[un]lock_irq() here, but this can
....@@ -1248,35 +1771,8 @@
12481771 */
12491772 raw_spin_lock_irqsave(&lock->wait_lock, flags);
12501773
1251
- /* Try to acquire the lock again: */
1252
- if (try_to_take_rt_mutex(lock, current, NULL)) {
1253
- raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1254
- return 0;
1255
- }
1256
-
1257
- set_current_state(state);
1258
-
1259
- /* Setup the timer, when timeout != NULL */
1260
- if (unlikely(timeout))
1261
- hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
1262
-
1263
- ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
1264
-
1265
- if (likely(!ret))
1266
- /* sleep on the mutex */
1267
- ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
1268
-
1269
- if (unlikely(ret)) {
1270
- __set_current_state(TASK_RUNNING);
1271
- remove_waiter(lock, &waiter);
1272
- rt_mutex_handle_deadlock(ret, chwalk, &waiter);
1273
- }
1274
-
1275
- /*
1276
- * try_to_take_rt_mutex() sets the waiter bit
1277
- * unconditionally. We might have to fix that up.
1278
- */
1279
- fixup_rt_mutex_waiters(lock);
1774
+ ret = rt_mutex_slowlock_locked(lock, state, timeout, chwalk, ww_ctx,
1775
+ &waiter);
12801776
12811777 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
12821778
....@@ -1337,7 +1833,8 @@
13371833 * Return whether the current task needs to call rt_mutex_postunlock().
13381834 */
13391835 static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1340
- struct wake_q_head *wake_q)
1836
+ struct wake_q_head *wake_q,
1837
+ struct wake_q_head *wake_sleeper_q)
13411838 {
13421839 unsigned long flags;
13431840
....@@ -1391,7 +1888,7 @@
13911888 *
13921889 * Queue the next waiter for wakeup once we release the wait_lock.
13931890 */
1394
- mark_wakeup_next_waiter(wake_q, lock);
1891
+ mark_wakeup_next_waiter(wake_q, wake_sleeper_q, lock);
13951892 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
13961893
13971894 return true; /* call rt_mutex_postunlock() */
....@@ -1405,29 +1902,16 @@
14051902 */
14061903 static inline int
14071904 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1905
+ struct ww_acquire_ctx *ww_ctx,
14081906 int (*slowfn)(struct rt_mutex *lock, int state,
14091907 struct hrtimer_sleeper *timeout,
1410
- enum rtmutex_chainwalk chwalk))
1908
+ enum rtmutex_chainwalk chwalk,
1909
+ struct ww_acquire_ctx *ww_ctx))
14111910 {
14121911 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
14131912 return 0;
14141913
1415
- return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
1416
-}
1417
-
1418
-static inline int
1419
-rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1420
- struct hrtimer_sleeper *timeout,
1421
- enum rtmutex_chainwalk chwalk,
1422
- int (*slowfn)(struct rt_mutex *lock, int state,
1423
- struct hrtimer_sleeper *timeout,
1424
- enum rtmutex_chainwalk chwalk))
1425
-{
1426
- if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1427
- likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1428
- return 0;
1429
-
1430
- return slowfn(lock, state, timeout, chwalk);
1914
+ return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK, ww_ctx);
14311915 }
14321916
14331917 static inline int
....@@ -1441,11 +1925,13 @@
14411925 }
14421926
14431927 /*
1444
- * Performs the wakeup of the the top-waiter and re-enables preemption.
1928
+ * Performs the wakeup of the top-waiter and re-enables preemption.
14451929 */
1446
-void rt_mutex_postunlock(struct wake_q_head *wake_q)
1930
+void rt_mutex_postunlock(struct wake_q_head *wake_q,
1931
+ struct wake_q_head *wake_sleeper_q)
14471932 {
14481933 wake_up_q(wake_q);
1934
+ wake_up_q_sleeper(wake_sleeper_q);
14491935
14501936 /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
14511937 preempt_enable();
....@@ -1454,23 +1940,48 @@
14541940 static inline void
14551941 rt_mutex_fastunlock(struct rt_mutex *lock,
14561942 bool (*slowfn)(struct rt_mutex *lock,
1457
- struct wake_q_head *wqh))
1943
+ struct wake_q_head *wqh,
1944
+ struct wake_q_head *wq_sleeper))
14581945 {
14591946 DEFINE_WAKE_Q(wake_q);
1947
+ DEFINE_WAKE_Q(wake_sleeper_q);
14601948
14611949 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
14621950 return;
14631951
1464
- if (slowfn(lock, &wake_q))
1465
- rt_mutex_postunlock(&wake_q);
1952
+ if (slowfn(lock, &wake_q, &wake_sleeper_q))
1953
+ rt_mutex_postunlock(&wake_q, &wake_sleeper_q);
1954
+}
1955
+
1956
+int __sched __rt_mutex_lock_state(struct rt_mutex *lock, int state)
1957
+{
1958
+ might_sleep();
1959
+ return rt_mutex_fastlock(lock, state, NULL, rt_mutex_slowlock);
1960
+}
1961
+
1962
+/**
1963
+ * rt_mutex_lock_state - lock a rt_mutex with a given state
1964
+ *
1965
+ * @lock: The rt_mutex to be locked
1966
+ * @state: The state to set when blocking on the rt_mutex
1967
+ */
1968
+static inline int __sched rt_mutex_lock_state(struct rt_mutex *lock,
1969
+ unsigned int subclass, int state)
1970
+{
1971
+ int ret;
1972
+
1973
+ mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
1974
+ ret = __rt_mutex_lock_state(lock, state);
1975
+ if (ret)
1976
+ mutex_release(&lock->dep_map, _RET_IP_);
1977
+ trace_android_vh_record_rtmutex_lock_starttime(current, jiffies);
1978
+
1979
+ return ret;
14661980 }
14671981
14681982 static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
14691983 {
1470
- might_sleep();
1471
-
1472
- mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
1473
- rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
1984
+ rt_mutex_lock_state(lock, subclass, TASK_UNINTERRUPTIBLE);
14741985 }
14751986
14761987 #ifdef CONFIG_DEBUG_LOCK_ALLOC
....@@ -1485,9 +1996,9 @@
14851996 __rt_mutex_lock(lock, subclass);
14861997 }
14871998 EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
1488
-#endif
14891999
1490
-#ifndef CONFIG_DEBUG_LOCK_ALLOC
2000
+#else /* !CONFIG_DEBUG_LOCK_ALLOC */
2001
+
14912002 /**
14922003 * rt_mutex_lock - lock a rt_mutex
14932004 *
....@@ -1511,16 +2022,7 @@
15112022 */
15122023 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
15132024 {
1514
- int ret;
1515
-
1516
- might_sleep();
1517
-
1518
- mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1519
- ret = rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1520
- if (ret)
1521
- mutex_release(&lock->dep_map, 1, _RET_IP_);
1522
-
1523
- return ret;
2025
+ return rt_mutex_lock_state(lock, 0, TASK_INTERRUPTIBLE);
15242026 }
15252027 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
15262028
....@@ -1537,36 +2039,17 @@
15372039 return __rt_mutex_slowtrylock(lock);
15382040 }
15392041
1540
-/**
1541
- * rt_mutex_timed_lock - lock a rt_mutex interruptible
1542
- * the timeout structure is provided
1543
- * by the caller
1544
- *
1545
- * @lock: the rt_mutex to be locked
1546
- * @timeout: timeout structure or NULL (no timeout)
1547
- *
1548
- * Returns:
1549
- * 0 on success
1550
- * -EINTR when interrupted by a signal
1551
- * -ETIMEDOUT when the timeout expired
1552
- */
1553
-int
1554
-rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
2042
+int __sched __rt_mutex_trylock(struct rt_mutex *lock)
15552043 {
1556
- int ret;
2044
+#ifdef CONFIG_PREEMPT_RT
2045
+ if (WARN_ON_ONCE(in_irq() || in_nmi()))
2046
+#else
2047
+ if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
2048
+#endif
2049
+ return 0;
15572050
1558
- might_sleep();
1559
-
1560
- mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
1561
- ret = rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1562
- RT_MUTEX_MIN_CHAINWALK,
1563
- rt_mutex_slowlock);
1564
- if (ret)
1565
- mutex_release(&lock->dep_map, 1, _RET_IP_);
1566
-
1567
- return ret;
2051
+ return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
15682052 }
1569
-EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
15702053
15712054 /**
15722055 * rt_mutex_trylock - try to lock a rt_mutex
....@@ -1583,16 +2066,19 @@
15832066 {
15842067 int ret;
15852068
1586
- if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
1587
- return 0;
1588
-
1589
- ret = rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
2069
+ ret = __rt_mutex_trylock(lock);
15902070 if (ret)
15912071 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
2072
+ else
2073
+ trace_android_vh_record_rtmutex_lock_starttime(current, jiffies);
15922074
15932075 return ret;
15942076 }
1595
-EXPORT_SYMBOL_GPL(rt_mutex_trylock);
2077
+
2078
+void __sched __rt_mutex_unlock(struct rt_mutex *lock)
2079
+{
2080
+ rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
2081
+}
15962082
15972083 /**
15982084 * rt_mutex_unlock - unlock a rt_mutex
....@@ -1601,17 +2087,15 @@
16012087 */
16022088 void __sched rt_mutex_unlock(struct rt_mutex *lock)
16032089 {
1604
- mutex_release(&lock->dep_map, 1, _RET_IP_);
1605
- rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
2090
+ mutex_release(&lock->dep_map, _RET_IP_);
2091
+ __rt_mutex_unlock(lock);
2092
+ trace_android_vh_record_rtmutex_lock_starttime(current, 0);
16062093 }
16072094 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
16082095
1609
-/**
1610
- * Futex variant, that since futex variants do not use the fast-path, can be
1611
- * simple and will not need to retry.
1612
- */
1613
-bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
1614
- struct wake_q_head *wake_q)
2096
+static bool __sched __rt_mutex_unlock_common(struct rt_mutex *lock,
2097
+ struct wake_q_head *wake_q,
2098
+ struct wake_q_head *wq_sleeper)
16152099 {
16162100 lockdep_assert_held(&lock->wait_lock);
16172101
....@@ -1628,23 +2112,35 @@
16282112 * avoid inversion prior to the wakeup. preempt_disable()
16292113 * therein pairs with rt_mutex_postunlock().
16302114 */
1631
- mark_wakeup_next_waiter(wake_q, lock);
2115
+ mark_wakeup_next_waiter(wake_q, wq_sleeper, lock);
16322116
16332117 return true; /* call postunlock() */
2118
+}
2119
+
2120
+/**
2121
+ * Futex variant, that since futex variants do not use the fast-path, can be
2122
+ * simple and will not need to retry.
2123
+ */
2124
+bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
2125
+ struct wake_q_head *wake_q,
2126
+ struct wake_q_head *wq_sleeper)
2127
+{
2128
+ return __rt_mutex_unlock_common(lock, wake_q, wq_sleeper);
16342129 }
16352130
16362131 void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
16372132 {
16382133 DEFINE_WAKE_Q(wake_q);
2134
+ DEFINE_WAKE_Q(wake_sleeper_q);
16392135 unsigned long flags;
16402136 bool postunlock;
16412137
16422138 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1643
- postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
2139
+ postunlock = __rt_mutex_futex_unlock(lock, &wake_q, &wake_sleeper_q);
16442140 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
16452141
16462142 if (postunlock)
1647
- rt_mutex_postunlock(&wake_q);
2143
+ rt_mutex_postunlock(&wake_q, &wake_sleeper_q);
16482144 }
16492145
16502146 /**
....@@ -1658,9 +2154,6 @@
16582154 void rt_mutex_destroy(struct rt_mutex *lock)
16592155 {
16602156 WARN_ON(rt_mutex_is_locked(lock));
1661
-#ifdef CONFIG_DEBUG_RT_MUTEXES
1662
- lock->magic = NULL;
1663
-#endif
16642157 }
16652158 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
16662159
....@@ -1683,7 +2176,7 @@
16832176 if (name && key)
16842177 debug_rt_mutex_init(lock, name, key);
16852178 }
1686
-EXPORT_SYMBOL_GPL(__rt_mutex_init);
2179
+EXPORT_SYMBOL(__rt_mutex_init);
16872180
16882181 /**
16892182 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
....@@ -1703,6 +2196,14 @@
17032196 struct task_struct *proxy_owner)
17042197 {
17052198 __rt_mutex_init(lock, NULL, NULL);
2199
+#ifdef CONFIG_DEBUG_SPINLOCK
2200
+ /*
2201
+ * get another key class for the wait_lock. LOCK_PI and UNLOCK_PI is
2202
+ * holding the ->wait_lock of the proxy_lock while unlocking a sleeping
2203
+ * lock.
2204
+ */
2205
+ raw_spin_lock_init(&lock->wait_lock);
2206
+#endif
17062207 debug_rt_mutex_proxy_lock(lock, proxy_owner);
17072208 rt_mutex_set_owner(lock, proxy_owner);
17082209 }
....@@ -1723,6 +2224,26 @@
17232224 {
17242225 debug_rt_mutex_proxy_unlock(lock);
17252226 rt_mutex_set_owner(lock, NULL);
2227
+}
2228
+
2229
+static void fixup_rt_mutex_blocked(struct rt_mutex *lock)
2230
+{
2231
+ struct task_struct *tsk = current;
2232
+ /*
2233
+ * RT has a problem here when the wait got interrupted by a timeout
2234
+ * or a signal. task->pi_blocked_on is still set. The task must
2235
+ * acquire the hash bucket lock when returning from this function.
2236
+ *
2237
+ * If the hash bucket lock is contended then the
2238
+ * BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on)) in
2239
+ * task_blocks_on_rt_mutex() will trigger. This can be avoided by
2240
+ * clearing task->pi_blocked_on which removes the task from the
2241
+ * boosting chain of the rtmutex. That's correct because the task
2242
+ * is not longer blocked on it.
2243
+ */
2244
+ raw_spin_lock(&tsk->pi_lock);
2245
+ tsk->pi_blocked_on = NULL;
2246
+ raw_spin_unlock(&tsk->pi_lock);
17262247 }
17272248
17282249 /**
....@@ -1755,6 +2276,34 @@
17552276 if (try_to_take_rt_mutex(lock, task, NULL))
17562277 return 1;
17572278
2279
+#ifdef CONFIG_PREEMPT_RT
2280
+ /*
2281
+ * In PREEMPT_RT there's an added race.
2282
+ * If the task, that we are about to requeue, times out,
2283
+ * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
2284
+ * to skip this task. But right after the task sets
2285
+ * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
2286
+ * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
2287
+ * This will replace the PI_WAKEUP_INPROGRESS with the actual
2288
+ * lock that it blocks on. We *must not* place this task
2289
+ * on this proxy lock in that case.
2290
+ *
2291
+ * To prevent this race, we first take the task's pi_lock
2292
+ * and check if it has updated its pi_blocked_on. If it has,
2293
+ * we assume that it woke up and we return -EAGAIN.
2294
+ * Otherwise, we set the task's pi_blocked_on to
2295
+ * PI_REQUEUE_INPROGRESS, so that if the task is waking up
2296
+ * it will know that we are in the process of requeuing it.
2297
+ */
2298
+ raw_spin_lock(&task->pi_lock);
2299
+ if (task->pi_blocked_on) {
2300
+ raw_spin_unlock(&task->pi_lock);
2301
+ return -EAGAIN;
2302
+ }
2303
+ task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
2304
+ raw_spin_unlock(&task->pi_lock);
2305
+#endif
2306
+
17582307 /* We enforce deadlock detection for futexes */
17592308 ret = task_blocks_on_rt_mutex(lock, waiter, task,
17602309 RT_MUTEX_FULL_CHAINWALK);
....@@ -1769,7 +2318,8 @@
17692318 ret = 0;
17702319 }
17712320
1772
- debug_rt_mutex_print_deadlock(waiter);
2321
+ if (ret)
2322
+ fixup_rt_mutex_blocked(lock);
17732323
17742324 return ret;
17752325 }
....@@ -1835,7 +2385,7 @@
18352385 * been started.
18362386 * @waiter: the pre-initialized rt_mutex_waiter
18372387 *
1838
- * Wait for the the lock acquisition started on our behalf by
2388
+ * Wait for the lock acquisition started on our behalf by
18392389 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
18402390 * rt_mutex_cleanup_proxy_lock().
18412391 *
....@@ -1854,12 +2404,15 @@
18542404 raw_spin_lock_irq(&lock->wait_lock);
18552405 /* sleep on the mutex */
18562406 set_current_state(TASK_INTERRUPTIBLE);
1857
- ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
2407
+ ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
18582408 /*
18592409 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
18602410 * have to fix that up.
18612411 */
18622412 fixup_rt_mutex_waiters(lock);
2413
+ if (ret)
2414
+ fixup_rt_mutex_blocked(lock);
2415
+
18632416 raw_spin_unlock_irq(&lock->wait_lock);
18642417
18652418 return ret;
....@@ -1921,3 +2474,97 @@
19212474
19222475 return cleanup;
19232476 }
2477
+
2478
+static inline int
2479
+ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
2480
+{
2481
+#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
2482
+ unsigned int tmp;
2483
+
2484
+ if (ctx->deadlock_inject_countdown-- == 0) {
2485
+ tmp = ctx->deadlock_inject_interval;
2486
+ if (tmp > UINT_MAX/4)
2487
+ tmp = UINT_MAX;
2488
+ else
2489
+ tmp = tmp*2 + tmp + tmp/2;
2490
+
2491
+ ctx->deadlock_inject_interval = tmp;
2492
+ ctx->deadlock_inject_countdown = tmp;
2493
+ ctx->contending_lock = lock;
2494
+
2495
+ ww_mutex_unlock(lock);
2496
+
2497
+ return -EDEADLK;
2498
+ }
2499
+#endif
2500
+
2501
+ return 0;
2502
+}
2503
+
2504
+#ifdef CONFIG_PREEMPT_RT
2505
+int __sched
2506
+ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
2507
+{
2508
+ int ret;
2509
+
2510
+ might_sleep();
2511
+
2512
+ mutex_acquire_nest(&lock->base.dep_map, 0, 0,
2513
+ ctx ? &ctx->dep_map : NULL, _RET_IP_);
2514
+ ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0,
2515
+ ctx);
2516
+ if (ret)
2517
+ mutex_release(&lock->base.dep_map, _RET_IP_);
2518
+ else if (!ret && ctx && ctx->acquired > 1)
2519
+ return ww_mutex_deadlock_injection(lock, ctx);
2520
+
2521
+ return ret;
2522
+}
2523
+EXPORT_SYMBOL(ww_mutex_lock_interruptible);
2524
+
2525
+int __sched
2526
+ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
2527
+{
2528
+ int ret;
2529
+
2530
+ might_sleep();
2531
+
2532
+ mutex_acquire_nest(&lock->base.dep_map, 0, 0,
2533
+ ctx ? &ctx->dep_map : NULL, _RET_IP_);
2534
+ ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0,
2535
+ ctx);
2536
+ if (ret)
2537
+ mutex_release(&lock->base.dep_map, _RET_IP_);
2538
+ else if (!ret && ctx && ctx->acquired > 1)
2539
+ return ww_mutex_deadlock_injection(lock, ctx);
2540
+
2541
+ return ret;
2542
+}
2543
+EXPORT_SYMBOL(ww_mutex_lock);
2544
+
2545
+void __sched ww_mutex_unlock(struct ww_mutex *lock)
2546
+{
2547
+ /*
2548
+ * The unlocking fastpath is the 0->1 transition from 'locked'
2549
+ * into 'unlocked' state:
2550
+ */
2551
+ if (lock->ctx) {
2552
+#ifdef CONFIG_DEBUG_MUTEXES
2553
+ DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
2554
+#endif
2555
+ if (lock->ctx->acquired > 0)
2556
+ lock->ctx->acquired--;
2557
+ lock->ctx = NULL;
2558
+ }
2559
+
2560
+ mutex_release(&lock->base.dep_map, _RET_IP_);
2561
+ __rt_mutex_unlock(&lock->base.lock);
2562
+}
2563
+EXPORT_SYMBOL(ww_mutex_unlock);
2564
+
2565
+int __rt_mutex_owner_current(struct rt_mutex *lock)
2566
+{
2567
+ return rt_mutex_owner(lock) == current;
2568
+}
2569
+EXPORT_SYMBOL(__rt_mutex_owner_current);
2570
+#endif