blob: db19136111921b8f0d222018e429d07ed1c9f5e3 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Ingo Molnar6053ee32006-01-09 15:59:19 -08002/*
Peter Zijlstra67a6de42013-11-08 08:26:39 +01003 * kernel/locking/mutex.c
Ingo Molnar6053ee32006-01-09 15:59:19 -08004 *
5 * Mutexes: blocking mutual exclusion locks
6 *
7 * Started by Ingo Molnar:
8 *
9 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
10 *
11 * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
12 * David Howells for suggestions and improvements.
13 *
Peter Zijlstra0d66bf62009-01-12 14:01:47 +010014 * - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
15 * from the -rt tree, where it was originally implemented for rtmutexes
16 * by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
17 * and Sven Dietrich.
18 *
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030019 * Also see Documentation/locking/mutex-design.rst.
Ingo Molnar6053ee32006-01-09 15:59:19 -080020 */
21#include <linux/mutex.h>
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +020022#include <linux/ww_mutex.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060024#include <linux/sched/rt.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010025#include <linux/sched/wake_q.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010026#include <linux/sched/debug.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040027#include <linux/export.h>
Ingo Molnar6053ee32006-01-09 15:59:19 -080028#include <linux/spinlock.h>
29#include <linux/interrupt.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070030#include <linux/debug_locks.h>
Davidlohr Bueso7a215f82015-01-30 01:14:25 -080031#include <linux/osq_lock.h>
Ingo Molnar6053ee32006-01-09 15:59:19 -080032
Thomas Gleixnerbb630f92021-08-15 23:29:01 +020033#ifndef CONFIG_PREEMPT_RT
Thomas Gleixnera321fb92021-08-17 16:17:38 +020034#include "mutex.h"
35
Ingo Molnar6053ee32006-01-09 15:59:19 -080036#ifdef CONFIG_DEBUG_MUTEXES
Peter Zijlstrae6b44572021-06-30 17:35:20 +020037# define MUTEX_WARN_ON(cond) DEBUG_LOCKS_WARN_ON(cond)
Ingo Molnar6053ee32006-01-09 15:59:19 -080038#else
Peter Zijlstrae6b44572021-06-30 17:35:20 +020039# define MUTEX_WARN_ON(cond)
Ingo Molnar6053ee32006-01-09 15:59:19 -080040#endif
41
Ingo Molnaref5d4702006-07-03 00:24:55 -070042void
43__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
Ingo Molnar6053ee32006-01-09 15:59:19 -080044{
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020045 atomic_long_set(&lock->owner, 0);
Thomas Gleixnerebf4c552021-08-15 23:28:36 +020046 raw_spin_lock_init(&lock->wait_lock);
Ingo Molnar6053ee32006-01-09 15:59:19 -080047 INIT_LIST_HEAD(&lock->wait_list);
Waiman Long2bd2c922013-04-17 15:23:13 -040048#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
Jason Low4d9d9512014-07-14 10:27:50 -070049 osq_lock_init(&lock->osq);
Waiman Long2bd2c922013-04-17 15:23:13 -040050#endif
Ingo Molnar6053ee32006-01-09 15:59:19 -080051
Ingo Molnaref5d4702006-07-03 00:24:55 -070052 debug_mutex_init(lock, name, key);
Ingo Molnar6053ee32006-01-09 15:59:19 -080053}
Ingo Molnar6053ee32006-01-09 15:59:19 -080054EXPORT_SYMBOL(__mutex_init);
55
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020056/*
57 * @owner: contains: 'struct task_struct *' to the current lock owner,
58 * NULL means not owned. Since task_struct pointers are aligned at
Peter Zijlstrae2747952017-01-11 14:17:48 +010059 * at least L1_CACHE_BYTES, we have low bits to store extra state.
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020060 *
61 * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
Peter Zijlstra9d659ae2016-08-23 14:40:16 +020062 * Bit1 indicates unlock needs to hand the lock to the top-waiter
Peter Zijlstrae2747952017-01-11 14:17:48 +010063 * Bit2 indicates handoff has been done and we're waiting for pickup.
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020064 */
65#define MUTEX_FLAG_WAITERS 0x01
Peter Zijlstra9d659ae2016-08-23 14:40:16 +020066#define MUTEX_FLAG_HANDOFF 0x02
Peter Zijlstrae2747952017-01-11 14:17:48 +010067#define MUTEX_FLAG_PICKUP 0x04
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020068
Peter Zijlstrae2747952017-01-11 14:17:48 +010069#define MUTEX_FLAGS 0x07
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020070
Mukesh Ojha5f35d5a2019-07-31 20:35:03 +053071/*
72 * Internal helper function; C doesn't allow us to hide it :/
73 *
74 * DO NOT USE (outside of mutex code).
75 */
76static inline struct task_struct *__mutex_owner(struct mutex *lock)
77{
Mukesh Ojhaa037d262019-07-31 20:35:04 +053078 return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS);
Mukesh Ojha5f35d5a2019-07-31 20:35:03 +053079}
80
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020081static inline struct task_struct *__owner_task(unsigned long owner)
82{
83 return (struct task_struct *)(owner & ~MUTEX_FLAGS);
84}
85
Mukesh Ojha5f35d5a2019-07-31 20:35:03 +053086bool mutex_is_locked(struct mutex *lock)
87{
88 return __mutex_owner(lock) != NULL;
89}
90EXPORT_SYMBOL(mutex_is_locked);
91
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +020092static inline unsigned long __owner_flags(unsigned long owner)
93{
94 return owner & MUTEX_FLAGS;
95}
96
Maarten Lankhorst12235da2021-09-09 11:32:18 +020097/*
98 * Returns: __mutex_owner(lock) on failure or NULL on success.
99 */
Peter Zijlstraad908802021-06-30 17:35:19 +0200100static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff)
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200101{
102 unsigned long owner, curr = (unsigned long)current;
103
104 owner = atomic_long_read(&lock->owner);
105 for (;;) { /* must loop, can race against a flag */
Peter Zijlstraab4e4d92021-06-30 17:35:17 +0200106 unsigned long flags = __owner_flags(owner);
Peter Zijlstrae2747952017-01-11 14:17:48 +0100107 unsigned long task = owner & ~MUTEX_FLAGS;
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200108
Peter Zijlstrae2747952017-01-11 14:17:48 +0100109 if (task) {
Peter Zijlstraad908802021-06-30 17:35:19 +0200110 if (flags & MUTEX_FLAG_PICKUP) {
111 if (task != curr)
112 break;
113 flags &= ~MUTEX_FLAG_PICKUP;
114 } else if (handoff) {
115 if (flags & MUTEX_FLAG_HANDOFF)
116 break;
117 flags |= MUTEX_FLAG_HANDOFF;
118 } else {
Peter Zijlstrae2747952017-01-11 14:17:48 +0100119 break;
Peter Zijlstraad908802021-06-30 17:35:19 +0200120 }
Peter Zijlstrae2747952017-01-11 14:17:48 +0100121 } else {
Peter Zijlstrae6b44572021-06-30 17:35:20 +0200122 MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP));
Peter Zijlstraad908802021-06-30 17:35:19 +0200123 task = curr;
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200124 }
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200125
Peter Zijlstraad908802021-06-30 17:35:19 +0200126 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) {
127 if (task == curr)
128 return NULL;
129 break;
130 }
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200131 }
Peter Zijlstrae2747952017-01-11 14:17:48 +0100132
133 return __owner_task(owner);
134}
135
136/*
Peter Zijlstraad908802021-06-30 17:35:19 +0200137 * Trylock or set HANDOFF
138 */
139static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff)
140{
141 return !__mutex_trylock_common(lock, handoff);
142}
143
144/*
Peter Zijlstrae2747952017-01-11 14:17:48 +0100145 * Actual trylock that will work on any unlocked state.
146 */
147static inline bool __mutex_trylock(struct mutex *lock)
148{
Peter Zijlstraad908802021-06-30 17:35:19 +0200149 return !__mutex_trylock_common(lock, false);
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200150}
151
152#ifndef CONFIG_DEBUG_LOCK_ALLOC
153/*
154 * Lockdep annotations are contained to the slow paths for simplicity.
155 * There is nothing that would stop spreading the lockdep annotations outwards
156 * except more code.
157 */
158
159/*
160 * Optimistic trylock that only works in the uncontended case. Make sure to
161 * follow with a __mutex_trylock() before failing.
162 */
163static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
164{
165 unsigned long curr = (unsigned long)current;
Peter Zijlstrac427f692018-04-05 11:05:35 +0200166 unsigned long zero = 0UL;
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200167
Peter Zijlstrac427f692018-04-05 11:05:35 +0200168 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200169 return true;
170
171 return false;
172}
173
174static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
175{
176 unsigned long curr = (unsigned long)current;
177
Peter Zijlstraab4e4d92021-06-30 17:35:17 +0200178 return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL);
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200179}
180#endif
181
182static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
183{
184 atomic_long_or(flag, &lock->owner);
185}
186
187static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
188{
189 atomic_long_andnot(flag, &lock->owner);
190}
191
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200192static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter)
193{
194 return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter;
195}
196
197/*
Thomas Hellstrom08295b32018-06-15 10:17:38 +0200198 * Add @waiter to a given location in the lock wait_list and set the
199 * FLAG_WAITERS flag if it's the first waiter.
200 */
Zqiang3a010c42021-05-17 11:40:05 +0800201static void
Thomas Hellstrom08295b32018-06-15 10:17:38 +0200202__mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
203 struct list_head *list)
204{
205 debug_mutex_add_waiter(lock, waiter, current);
206
207 list_add_tail(&waiter->list, list);
208 if (__mutex_waiter_is_first(lock, waiter))
209 __mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
210}
211
Zqiang3a010c42021-05-17 11:40:05 +0800212static void
213__mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter)
214{
215 list_del(&waiter->list);
216 if (likely(list_empty(&lock->wait_list)))
217 __mutex_clear_flag(lock, MUTEX_FLAGS);
218
219 debug_mutex_remove_waiter(lock, waiter, current);
220}
221
Thomas Hellstrom08295b32018-06-15 10:17:38 +0200222/*
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200223 * Give up ownership to a specific task, when @task = NULL, this is equivalent
Ingo Molnare2db7592021-03-22 02:35:05 +0100224 * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves
Peter Zijlstrae2747952017-01-11 14:17:48 +0100225 * WAITERS. Provides RELEASE semantics like a regular unlock, the
226 * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200227 */
228static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
229{
230 unsigned long owner = atomic_long_read(&lock->owner);
231
232 for (;;) {
Peter Zijlstraab4e4d92021-06-30 17:35:17 +0200233 unsigned long new;
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200234
Peter Zijlstrae6b44572021-06-30 17:35:20 +0200235 MUTEX_WARN_ON(__owner_task(owner) != current);
236 MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200237
238 new = (owner & MUTEX_FLAG_WAITERS);
239 new |= (unsigned long)task;
Peter Zijlstrae2747952017-01-11 14:17:48 +0100240 if (task)
241 new |= MUTEX_FLAG_PICKUP;
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200242
Peter Zijlstraab4e4d92021-06-30 17:35:17 +0200243 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new))
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200244 break;
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200245 }
246}
247
Peter Zijlstrae4564f72007-10-11 22:11:12 +0200248#ifndef CONFIG_DEBUG_LOCK_ALLOC
Ingo Molnar6053ee32006-01-09 15:59:19 -0800249/*
250 * We split the mutex lock/unlock logic into separate fastpath and
251 * slowpath functions, to reduce the register pressure on the fastpath.
252 * We also put the fastpath first in the kernel image, to make sure the
253 * branch is predicted by the CPU as default-untaken.
254 */
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200255static void __sched __mutex_lock_slowpath(struct mutex *lock);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800256
Randy Dunlapef5dc122010-09-02 15:48:16 -0700257/**
Ingo Molnar6053ee32006-01-09 15:59:19 -0800258 * mutex_lock - acquire the mutex
259 * @lock: the mutex to be acquired
260 *
261 * Lock the mutex exclusively for this task. If the mutex is not
262 * available right now, it will sleep until it can get it.
263 *
264 * The mutex must later on be released by the same task that
265 * acquired it. Recursive locking is not allowed. The task
266 * may not exit without first unlocking the mutex. Also, kernel
Sharon Dvir139b6fd2015-02-01 23:47:32 +0200267 * memory where the mutex resides must not be freed with
Ingo Molnar6053ee32006-01-09 15:59:19 -0800268 * the mutex still locked. The mutex must first be initialized
269 * (or statically defined) before it can be locked. memset()-ing
270 * the mutex to 0 is not allowed.
271 *
Mauro Carvalho Chehab7b4ff1a2017-05-11 10:17:45 -0300272 * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging
273 * checks that will enforce the restrictions and will also do
274 * deadlock debugging)
Ingo Molnar6053ee32006-01-09 15:59:19 -0800275 *
276 * This function is similar to (but not equivalent to) down().
277 */
H. Peter Anvinb09d2502009-04-01 17:21:56 -0700278void __sched mutex_lock(struct mutex *lock)
Ingo Molnar6053ee32006-01-09 15:59:19 -0800279{
Ingo Molnarc544bdb2006-01-10 22:10:36 +0100280 might_sleep();
Ingo Molnar6053ee32006-01-09 15:59:19 -0800281
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200282 if (!__mutex_trylock_fast(lock))
283 __mutex_lock_slowpath(lock);
284}
Ingo Molnar6053ee32006-01-09 15:59:19 -0800285EXPORT_SYMBOL(mutex_lock);
Peter Zijlstrae4564f72007-10-11 22:11:12 +0200286#endif
Ingo Molnar6053ee32006-01-09 15:59:19 -0800287
Peter Zijlstra (Intel)2674bd12021-08-17 16:31:54 +0200288#include "ww_mutex.h"
Davidlohr Bueso76916512014-07-30 13:41:53 -0700289
Waiman Long41fcb9f2013-04-17 15:23:11 -0400290#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
Nicolai Hähnlec516df92016-12-21 19:46:38 +0100291
Peter Zijlstraad908802021-06-30 17:35:19 +0200292/*
293 * Trylock variant that returns the owning task on failure.
294 */
295static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
296{
297 return __mutex_trylock_common(lock, false);
298}
299
Nicolai Hähnlec516df92016-12-21 19:46:38 +0100300static inline
301bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
302 struct mutex_waiter *waiter)
303{
304 struct ww_mutex *ww;
305
306 ww = container_of(lock, struct ww_mutex, base);
307
308 /*
309 * If ww->ctx is set the contents are undefined, only
310 * by acquiring wait_lock there is a guarantee that
311 * they are not invalid when reading.
312 *
313 * As such, when deadlock detection needs to be
314 * performed the optimistic spinning cannot be done.
315 *
316 * Check this in every inner iteration because we may
317 * be racing against another thread's ww_mutex_lock.
318 */
319 if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
320 return false;
321
322 /*
323 * If we aren't on the wait list yet, cancel the spin
324 * if there are waiters. We want to avoid stealing the
325 * lock from a waiter with an earlier stamp, since the
326 * other thread may already own a lock that we also
327 * need.
328 */
329 if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
330 return false;
331
332 /*
333 * Similarly, stop spinning if we are no longer the
334 * first waiter.
335 */
336 if (waiter && !__mutex_waiter_is_first(lock, waiter))
337 return false;
338
339 return true;
340}
341
Waiman Long41fcb9f2013-04-17 15:23:11 -0400342/*
Nicolai Hähnle25f13b42016-12-21 19:46:37 +0100343 * Look out! "owner" is an entirely speculative pointer access and not
344 * reliable.
345 *
346 * "noinline" so that this function shows up on perf profiles.
Waiman Long41fcb9f2013-04-17 15:23:11 -0400347 */
348static noinline
Nicolai Hähnle25f13b42016-12-21 19:46:37 +0100349bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
Nicolai Hähnlec516df92016-12-21 19:46:38 +0100350 struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
Waiman Long41fcb9f2013-04-17 15:23:11 -0400351{
Jason Low01ac33c2015-04-08 12:39:19 -0700352 bool ret = true;
Jason Lowbe1f7bf2015-02-02 13:59:27 -0800353
Yanfei Xu6c2787f2021-10-13 21:41:52 +0800354 lockdep_assert_preemption_disabled();
355
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200356 while (__mutex_owner(lock) == owner) {
Jason Lowbe1f7bf2015-02-02 13:59:27 -0800357 /*
358 * Ensure we emit the owner->on_cpu, dereference _after_
Yanfei Xu6c2787f2021-10-13 21:41:52 +0800359 * checking lock->owner still matches owner. And we already
360 * disabled preemption which is equal to the RCU read-side
361 * crital section in optimistic spinning code. Thus the
362 * task_strcut structure won't go away during the spinning
363 * period
Jason Lowbe1f7bf2015-02-02 13:59:27 -0800364 */
365 barrier();
366
Pan Xinhui05ffc952016-11-02 05:08:30 -0400367 /*
368 * Use vcpu_is_preempted to detect lock holder preemption issue.
369 */
370 if (!owner->on_cpu || need_resched() ||
371 vcpu_is_preempted(task_cpu(owner))) {
Jason Lowbe1f7bf2015-02-02 13:59:27 -0800372 ret = false;
373 break;
374 }
Waiman Long41fcb9f2013-04-17 15:23:11 -0400375
Nicolai Hähnlec516df92016-12-21 19:46:38 +0100376 if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
377 ret = false;
378 break;
Nicolai Hähnle25f13b42016-12-21 19:46:37 +0100379 }
380
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200381 cpu_relax();
Waiman Long41fcb9f2013-04-17 15:23:11 -0400382 }
Waiman Long41fcb9f2013-04-17 15:23:11 -0400383
Jason Lowbe1f7bf2015-02-02 13:59:27 -0800384 return ret;
Waiman Long41fcb9f2013-04-17 15:23:11 -0400385}
Waiman Long2bd2c922013-04-17 15:23:13 -0400386
387/*
388 * Initial check for entering the mutex spinning loop
389 */
390static inline int mutex_can_spin_on_owner(struct mutex *lock)
391{
Peter Zijlstra1e40c2e2013-07-19 20:31:01 +0200392 struct task_struct *owner;
Waiman Long2bd2c922013-04-17 15:23:13 -0400393 int retval = 1;
394
Yanfei Xu6c2787f2021-10-13 21:41:52 +0800395 lockdep_assert_preemption_disabled();
396
Jason Low46af29e2014-01-28 11:13:12 -0800397 if (need_resched())
398 return 0;
399
Yanfei Xu6c2787f2021-10-13 21:41:52 +0800400 /*
401 * We already disabled preemption which is equal to the RCU read-side
402 * crital section in optimistic spinning code. Thus the task_strcut
403 * structure won't go away during the spinning period.
404 */
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200405 owner = __mutex_owner(lock);
Pan Xinhui05ffc952016-11-02 05:08:30 -0400406
407 /*
408 * As lock holder preemption issue, we both skip spinning if task is not
409 * on cpu or its cpu is preempted
410 */
Yanfei Xu6c2787f2021-10-13 21:41:52 +0800411
Peter Zijlstra1e40c2e2013-07-19 20:31:01 +0200412 if (owner)
Pan Xinhui05ffc952016-11-02 05:08:30 -0400413 retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200414
Waiman Long2bd2c922013-04-17 15:23:13 -0400415 /*
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200416 * If lock->owner is not set, the mutex has been released. Return true
417 * such that we'll trylock in the spin path, which is a faster option
418 * than the blocking slow path.
Waiman Long2bd2c922013-04-17 15:23:13 -0400419 */
420 return retval;
421}
Davidlohr Bueso76916512014-07-30 13:41:53 -0700422
423/*
Davidlohr Bueso76916512014-07-30 13:41:53 -0700424 * Optimistic spinning.
425 *
426 * We try to spin for acquisition when we find that the lock owner
427 * is currently running on a (different) CPU and while we don't
428 * need to reschedule. The rationale is that if the lock owner is
429 * running, it is likely to release the lock soon.
430 *
Davidlohr Bueso76916512014-07-30 13:41:53 -0700431 * The mutex spinners are queued up using MCS lock so that only one
432 * spinner can compete for the mutex. However, if mutex spinning isn't
433 * going to happen, there is no point in going through the lock/unlock
434 * overhead.
435 *
436 * Returns true when the lock was taken, otherwise false, indicating
437 * that we need to jump to the slowpath and sleep.
Waiman Longb341afb2016-08-26 19:35:09 -0400438 *
439 * The waiter flag is set to true if the spinner is a waiter in the wait
440 * queue. The waiter-spinner will spin on the lock directly and concurrently
441 * with the spinner at the head of the OSQ, if present, until the owner is
442 * changed to itself.
Davidlohr Bueso76916512014-07-30 13:41:53 -0700443 */
Peter Zijlstra427b1822016-12-23 10:36:00 +0100444static __always_inline bool
445mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
Waiman Long5de20552021-03-16 11:31:16 -0400446 struct mutex_waiter *waiter)
Davidlohr Bueso76916512014-07-30 13:41:53 -0700447{
Waiman Longb341afb2016-08-26 19:35:09 -0400448 if (!waiter) {
449 /*
450 * The purpose of the mutex_can_spin_on_owner() function is
451 * to eliminate the overhead of osq_lock() and osq_unlock()
452 * in case spinning isn't possible. As a waiter-spinner
453 * is not going to take OSQ lock anyway, there is no need
454 * to call mutex_can_spin_on_owner().
455 */
456 if (!mutex_can_spin_on_owner(lock))
457 goto fail;
Davidlohr Bueso76916512014-07-30 13:41:53 -0700458
Waiman Longb341afb2016-08-26 19:35:09 -0400459 /*
460 * In order to avoid a stampede of mutex spinners trying to
461 * acquire the mutex all at once, the spinners need to take a
462 * MCS (queued) lock first before spinning on the owner field.
463 */
464 if (!osq_lock(&lock->osq))
465 goto fail;
466 }
Davidlohr Bueso76916512014-07-30 13:41:53 -0700467
Waiman Longb341afb2016-08-26 19:35:09 -0400468 for (;;) {
Davidlohr Bueso76916512014-07-30 13:41:53 -0700469 struct task_struct *owner;
470
Peter Zijlstrae2747952017-01-11 14:17:48 +0100471 /* Try to acquire the mutex... */
472 owner = __mutex_trylock_or_owner(lock);
473 if (!owner)
474 break;
Davidlohr Bueso76916512014-07-30 13:41:53 -0700475
476 /*
Peter Zijlstrae2747952017-01-11 14:17:48 +0100477 * There's an owner, wait for it to either
Davidlohr Bueso76916512014-07-30 13:41:53 -0700478 * release the lock or go to sleep.
479 */
Nicolai Hähnlec516df92016-12-21 19:46:38 +0100480 if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
Peter Zijlstrae2747952017-01-11 14:17:48 +0100481 goto fail_unlock;
Davidlohr Bueso76916512014-07-30 13:41:53 -0700482
483 /*
Davidlohr Bueso76916512014-07-30 13:41:53 -0700484 * The cpu_relax() call is a compiler barrier which forces
485 * everything in this loop to be re-loaded. We don't need
486 * memory barriers as we'll eventually observe the right
487 * values at the cost of a few extra spins.
488 */
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200489 cpu_relax();
Davidlohr Bueso76916512014-07-30 13:41:53 -0700490 }
491
Waiman Longb341afb2016-08-26 19:35:09 -0400492 if (!waiter)
493 osq_unlock(&lock->osq);
494
495 return true;
496
497
498fail_unlock:
499 if (!waiter)
500 osq_unlock(&lock->osq);
501
502fail:
Davidlohr Bueso76916512014-07-30 13:41:53 -0700503 /*
504 * If we fell out of the spin path because of need_resched(),
505 * reschedule now, before we try-lock the mutex. This avoids getting
506 * scheduled out right after we obtained the mutex.
507 */
Peter Zijlstra6f942a12014-09-24 10:18:46 +0200508 if (need_resched()) {
509 /*
510 * We _should_ have TASK_RUNNING here, but just in case
511 * we do not, make it so, otherwise we might get stuck.
512 */
513 __set_current_state(TASK_RUNNING);
Davidlohr Bueso76916512014-07-30 13:41:53 -0700514 schedule_preempt_disabled();
Peter Zijlstra6f942a12014-09-24 10:18:46 +0200515 }
Davidlohr Bueso76916512014-07-30 13:41:53 -0700516
517 return false;
518}
519#else
Peter Zijlstra427b1822016-12-23 10:36:00 +0100520static __always_inline bool
521mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
Waiman Long5de20552021-03-16 11:31:16 -0400522 struct mutex_waiter *waiter)
Davidlohr Bueso76916512014-07-30 13:41:53 -0700523{
524 return false;
525}
Waiman Long41fcb9f2013-04-17 15:23:11 -0400526#endif
527
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200528static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800529
Randy Dunlapef5dc122010-09-02 15:48:16 -0700530/**
Ingo Molnar6053ee32006-01-09 15:59:19 -0800531 * mutex_unlock - release the mutex
532 * @lock: the mutex to be released
533 *
534 * Unlock a mutex that has been locked by this task previously.
535 *
536 * This function must not be used in interrupt context. Unlocking
537 * of a not locked mutex is not allowed.
538 *
539 * This function is similar to (but not equivalent to) up().
540 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800541void __sched mutex_unlock(struct mutex *lock)
Ingo Molnar6053ee32006-01-09 15:59:19 -0800542{
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200543#ifndef CONFIG_DEBUG_LOCK_ALLOC
544 if (__mutex_unlock_fast(lock))
545 return;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +0100546#endif
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200547 __mutex_unlock_slowpath(lock, _RET_IP_);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800548}
Ingo Molnar6053ee32006-01-09 15:59:19 -0800549EXPORT_SYMBOL(mutex_unlock);
550
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200551/**
552 * ww_mutex_unlock - release the w/w mutex
553 * @lock: the mutex to be released
554 *
555 * Unlock a mutex that has been locked by this task previously with any of the
556 * ww_mutex_lock* functions (with or without an acquire context). It is
557 * forbidden to release the locks after releasing the acquire context.
558 *
559 * This function must not be used in interrupt context. Unlocking
560 * of a unlocked mutex is not allowed.
561 */
562void __sched ww_mutex_unlock(struct ww_mutex *lock)
563{
Peter Zijlstra (Intel)aaa77de2021-08-17 16:19:04 +0200564 __ww_mutex_unlock(lock);
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200565 mutex_unlock(&lock->base);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200566}
567EXPORT_SYMBOL(ww_mutex_unlock);
568
Ingo Molnar6053ee32006-01-09 15:59:19 -0800569/*
570 * Lock a mutex (possibly interruptible), slowpath:
571 */
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200572static __always_inline int __sched
Peter Zijlstra2f064a52021-06-11 10:28:17 +0200573__mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass,
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200574 struct lockdep_map *nest_lock, unsigned long ip,
Tetsuo Handab0267502013-10-17 19:45:29 +0900575 struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
Ingo Molnar6053ee32006-01-09 15:59:19 -0800576{
Ingo Molnar6053ee32006-01-09 15:59:19 -0800577 struct mutex_waiter waiter;
Waiman Longa40ca562016-08-26 19:35:08 -0400578 struct ww_mutex *ww;
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200579 int ret;
Ingo Molnar6053ee32006-01-09 15:59:19 -0800580
Waiman Long5de20552021-03-16 11:31:16 -0400581 if (!use_ww_ctx)
582 ww_ctx = NULL;
583
Peter Zijlstra427b1822016-12-23 10:36:00 +0100584 might_sleep();
Nicolai Hähnleea9e0fb2016-12-21 19:46:32 +0100585
Peter Zijlstrae6b44572021-06-30 17:35:20 +0200586 MUTEX_WARN_ON(lock->magic != lock);
Sebastian Andrzej Siewior6c11c6e2019-07-03 11:21:26 +0200587
Peter Zijlstra427b1822016-12-23 10:36:00 +0100588 ww = container_of(lock, struct ww_mutex, base);
Waiman Long5de20552021-03-16 11:31:16 -0400589 if (ww_ctx) {
Chris Wilson0422e832016-05-26 21:08:17 +0100590 if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
591 return -EALREADY;
Thomas Hellstrom08295b32018-06-15 10:17:38 +0200592
593 /*
594 * Reset the wounded flag after a kill. No other process can
595 * race and wound us here since they can't have a valid owner
596 * pointer if we don't have any locks held.
597 */
598 if (ww_ctx->acquired == 0)
599 ww_ctx->wounded = 0;
Peter Zijlstracf702ed2021-08-15 23:28:38 +0200600
601#ifdef CONFIG_DEBUG_LOCK_ALLOC
602 nest_lock = &ww_ctx->dep_map;
603#endif
Chris Wilson0422e832016-05-26 21:08:17 +0100604 }
605
Peter Zijlstra41719b02009-01-14 15:36:26 +0100606 preempt_disable();
Peter Zijlstrae4c70a62011-05-24 17:12:03 -0700607 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
Frederic Weisbeckerc0226022009-12-02 20:49:16 +0100608
Peter Zijlstrae2747952017-01-11 14:17:48 +0100609 if (__mutex_trylock(lock) ||
Waiman Long5de20552021-03-16 11:31:16 -0400610 mutex_optimistic_spin(lock, ww_ctx, NULL)) {
Davidlohr Bueso76916512014-07-30 13:41:53 -0700611 /* got the lock, yay! */
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200612 lock_acquired(&lock->dep_map, ip);
Waiman Long5de20552021-03-16 11:31:16 -0400613 if (ww_ctx)
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200614 ww_mutex_set_context_fastpath(ww, ww_ctx);
Davidlohr Bueso76916512014-07-30 13:41:53 -0700615 preempt_enable();
616 return 0;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +0100617 }
Davidlohr Bueso76916512014-07-30 13:41:53 -0700618
Thomas Gleixnerebf4c552021-08-15 23:28:36 +0200619 raw_spin_lock(&lock->wait_lock);
Jason Low1e820c92014-06-11 11:37:21 -0700620 /*
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200621 * After waiting to acquire the wait_lock, try again.
Jason Low1e820c92014-06-11 11:37:21 -0700622 */
Nicolai Hähnle659cf9f2016-12-21 19:46:36 +0100623 if (__mutex_trylock(lock)) {
Waiman Long5de20552021-03-16 11:31:16 -0400624 if (ww_ctx)
Peter Ziljstra55f036c2018-06-15 10:07:12 +0200625 __ww_mutex_check_waiters(lock, ww_ctx);
Nicolai Hähnle659cf9f2016-12-21 19:46:36 +0100626
Davidlohr Buesoec83f422013-06-28 13:13:18 -0700627 goto skip_wait;
Nicolai Hähnle659cf9f2016-12-21 19:46:36 +0100628 }
Davidlohr Buesoec83f422013-06-28 13:13:18 -0700629
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700630 debug_mutex_lock_common(lock, &waiter);
Peter Zijlstrac0afb0f2021-08-15 23:28:39 +0200631 waiter.task = current;
Sebastian Andrzej Siewiorb8571742021-08-19 21:30:30 +0200632 if (use_ww_ctx)
Peter Zijlstrac0afb0f2021-08-15 23:28:39 +0200633 waiter.ww_ctx = ww_ctx;
Ingo Molnar6053ee32006-01-09 15:59:19 -0800634
Nicolai Hähnle6baa5c62016-12-21 19:46:34 +0100635 lock_contended(&lock->dep_map, ip);
636
637 if (!use_ww_ctx) {
638 /* add waiting tasks to the end of the waitqueue (FIFO): */
Thomas Hellstrom08295b32018-06-15 10:17:38 +0200639 __mutex_add_waiter(lock, &waiter, &lock->wait_list);
Nicolai Hähnle6baa5c62016-12-21 19:46:34 +0100640 } else {
Peter Ziljstra55f036c2018-06-15 10:07:12 +0200641 /*
642 * Add in stamp order, waking up waiters that must kill
643 * themselves.
644 */
Nicolai Hähnle6baa5c62016-12-21 19:46:34 +0100645 ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
646 if (ret)
Peter Ziljstra55f036c2018-06-15 10:07:12 +0200647 goto err_early_kill;
Nicolai Hähnle6baa5c62016-12-21 19:46:34 +0100648 }
649
Davidlohr Bueso642fa442017-01-03 13:43:14 -0800650 set_current_state(state);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800651 for (;;) {
Peter Zijlstra048661a2021-06-30 17:35:18 +0200652 bool first;
653
Peter Zijlstra5bbd7e62016-09-02 13:42:12 +0200654 /*
655 * Once we hold wait_lock, we're serialized against
656 * mutex_unlock() handing the lock off to us, do a trylock
657 * before testing the error conditions to make sure we pick up
658 * the handoff.
659 */
Peter Zijlstrae2747952017-01-11 14:17:48 +0100660 if (__mutex_trylock(lock))
Peter Zijlstra5bbd7e62016-09-02 13:42:12 +0200661 goto acquired;
Ingo Molnar6053ee32006-01-09 15:59:19 -0800662
663 /*
Peter Ziljstra55f036c2018-06-15 10:07:12 +0200664 * Check for signals and kill conditions while holding
Peter Zijlstra5bbd7e62016-09-02 13:42:12 +0200665 * wait_lock. This ensures the lock cancellation is ordered
666 * against mutex_unlock() and wake-ups do not go missing.
Ingo Molnar6053ee32006-01-09 15:59:19 -0800667 */
Davidlohr Bueso3bb5f4a2019-01-03 15:28:44 -0800668 if (signal_pending_state(state, current)) {
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200669 ret = -EINTR;
670 goto err;
Ingo Molnar6053ee32006-01-09 15:59:19 -0800671 }
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200672
Waiman Long5de20552021-03-16 11:31:16 -0400673 if (ww_ctx) {
Peter Ziljstra55f036c2018-06-15 10:07:12 +0200674 ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200675 if (ret)
676 goto err;
677 }
678
Thomas Gleixnerebf4c552021-08-15 23:28:36 +0200679 raw_spin_unlock(&lock->wait_lock);
Thomas Gleixnerbd2f5532011-03-21 12:33:18 +0100680 schedule_preempt_disabled();
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200681
Peter Zijlstra048661a2021-06-30 17:35:18 +0200682 first = __mutex_waiter_is_first(lock, &waiter);
Peter Zijlstra5bbd7e62016-09-02 13:42:12 +0200683
Davidlohr Bueso642fa442017-01-03 13:43:14 -0800684 set_current_state(state);
Peter Zijlstra5bbd7e62016-09-02 13:42:12 +0200685 /*
686 * Here we order against unlock; we must either see it change
687 * state back to RUNNING and fall through the next schedule(),
688 * or we must see its unlock and acquire.
689 */
Peter Zijlstraad908802021-06-30 17:35:19 +0200690 if (__mutex_trylock_or_handoff(lock, first) ||
Waiman Long5de20552021-03-16 11:31:16 -0400691 (first && mutex_optimistic_spin(lock, ww_ctx, &waiter)))
Peter Zijlstra5bbd7e62016-09-02 13:42:12 +0200692 break;
693
Thomas Gleixnerebf4c552021-08-15 23:28:36 +0200694 raw_spin_lock(&lock->wait_lock);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800695 }
Thomas Gleixnerebf4c552021-08-15 23:28:36 +0200696 raw_spin_lock(&lock->wait_lock);
Peter Zijlstra5bbd7e62016-09-02 13:42:12 +0200697acquired:
Davidlohr Bueso642fa442017-01-03 13:43:14 -0800698 __set_current_state(TASK_RUNNING);
Davidlohr Bueso51587bc2015-01-19 17:39:21 -0800699
Waiman Long5de20552021-03-16 11:31:16 -0400700 if (ww_ctx) {
Thomas Hellstrom08295b32018-06-15 10:17:38 +0200701 /*
702 * Wound-Wait; we stole the lock (!first_waiter), check the
703 * waiters as anyone might want to wound us.
704 */
705 if (!ww_ctx->is_wait_die &&
706 !__mutex_waiter_is_first(lock, &waiter))
707 __ww_mutex_check_waiters(lock, ww_ctx);
708 }
709
Zqiang3a010c42021-05-17 11:40:05 +0800710 __mutex_remove_waiter(lock, &waiter);
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200711
Davidlohr Buesoec83f422013-06-28 13:13:18 -0700712 debug_mutex_free_waiter(&waiter);
713
714skip_wait:
715 /* got the lock - cleanup and rejoice! */
716 lock_acquired(&lock->dep_map, ip);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800717
Waiman Long5de20552021-03-16 11:31:16 -0400718 if (ww_ctx)
Peter Ziljstra55f036c2018-06-15 10:07:12 +0200719 ww_mutex_lock_acquired(ww, ww_ctx);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200720
Thomas Gleixnerebf4c552021-08-15 23:28:36 +0200721 raw_spin_unlock(&lock->wait_lock);
Peter Zijlstra41719b02009-01-14 15:36:26 +0100722 preempt_enable();
Ingo Molnar6053ee32006-01-09 15:59:19 -0800723 return 0;
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200724
725err:
Davidlohr Bueso642fa442017-01-03 13:43:14 -0800726 __set_current_state(TASK_RUNNING);
Zqiang3a010c42021-05-17 11:40:05 +0800727 __mutex_remove_waiter(lock, &waiter);
Peter Ziljstra55f036c2018-06-15 10:07:12 +0200728err_early_kill:
Thomas Gleixnerebf4c552021-08-15 23:28:36 +0200729 raw_spin_unlock(&lock->wait_lock);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200730 debug_mutex_free_waiter(&waiter);
Qian Cai5facae42019-09-19 12:09:40 -0400731 mutex_release(&lock->dep_map, ip);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200732 preempt_enable();
733 return ret;
Ingo Molnar6053ee32006-01-09 15:59:19 -0800734}
735
Peter Zijlstra427b1822016-12-23 10:36:00 +0100736static int __sched
Peter Zijlstra2f064a52021-06-11 10:28:17 +0200737__mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
Peter Zijlstra427b1822016-12-23 10:36:00 +0100738 struct lockdep_map *nest_lock, unsigned long ip)
739{
740 return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
741}
742
743static int __sched
Peter Zijlstra2f064a52021-06-11 10:28:17 +0200744__ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
Peter Zijlstracf702ed2021-08-15 23:28:38 +0200745 unsigned long ip, struct ww_acquire_ctx *ww_ctx)
Peter Zijlstra427b1822016-12-23 10:36:00 +0100746{
Peter Zijlstracf702ed2021-08-15 23:28:38 +0200747 return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true);
Peter Zijlstra427b1822016-12-23 10:36:00 +0100748}
749
Maarten Lankhorst12235da2021-09-09 11:32:18 +0200750/**
751 * ww_mutex_trylock - tries to acquire the w/w mutex with optional acquire context
752 * @ww: mutex to lock
753 * @ww_ctx: optional w/w acquire context
754 *
755 * Trylocks a mutex with the optional acquire context; no deadlock detection is
756 * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
757 *
758 * Unlike ww_mutex_lock, no deadlock handling is performed. However, if a @ctx is
759 * specified, -EALREADY handling may happen in calls to ww_mutex_trylock.
760 *
761 * A mutex acquired with this function must be released with ww_mutex_unlock.
762 */
763int ww_mutex_trylock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
764{
765 if (!ww_ctx)
766 return mutex_trylock(&ww->base);
767
768 MUTEX_WARN_ON(ww->base.magic != &ww->base);
769
770 /*
771 * Reset the wounded flag after a kill. No other process can
772 * race and wound us here, since they can't have a valid owner
773 * pointer if we don't have any locks held.
774 */
775 if (ww_ctx->acquired == 0)
776 ww_ctx->wounded = 0;
777
778 if (__mutex_trylock(&ww->base)) {
779 ww_mutex_set_context_fastpath(ww, ww_ctx);
780 mutex_acquire_nest(&ww->base.dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_);
781 return 1;
782 }
783
784 return 0;
785}
786EXPORT_SYMBOL(ww_mutex_trylock);
787
Ingo Molnaref5d4702006-07-03 00:24:55 -0700788#ifdef CONFIG_DEBUG_LOCK_ALLOC
789void __sched
790mutex_lock_nested(struct mutex *lock, unsigned int subclass)
791{
Peter Zijlstra427b1822016-12-23 10:36:00 +0100792 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
Ingo Molnaref5d4702006-07-03 00:24:55 -0700793}
794
795EXPORT_SYMBOL_GPL(mutex_lock_nested);
NeilBrownd63a5a72006-12-08 02:36:17 -0800796
Peter Zijlstrae4c70a62011-05-24 17:12:03 -0700797void __sched
798_mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
799{
Peter Zijlstra427b1822016-12-23 10:36:00 +0100800 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
Peter Zijlstrae4c70a62011-05-24 17:12:03 -0700801}
Peter Zijlstrae4c70a62011-05-24 17:12:03 -0700802EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
803
NeilBrownd63a5a72006-12-08 02:36:17 -0800804int __sched
Liam R. Howlettad776532007-12-06 17:37:59 -0500805mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
806{
Peter Zijlstra427b1822016-12-23 10:36:00 +0100807 return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
Liam R. Howlettad776532007-12-06 17:37:59 -0500808}
809EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
810
811int __sched
NeilBrownd63a5a72006-12-08 02:36:17 -0800812mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
813{
Peter Zijlstra427b1822016-12-23 10:36:00 +0100814 return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
NeilBrownd63a5a72006-12-08 02:36:17 -0800815}
NeilBrownd63a5a72006-12-08 02:36:17 -0800816EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200817
Tejun Heo1460cb62016-10-28 12:58:11 -0400818void __sched
819mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
820{
821 int token;
822
823 might_sleep();
824
825 token = io_schedule_prepare();
826 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
827 subclass, NULL, _RET_IP_, NULL, 0);
828 io_schedule_finish(token);
829}
830EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
831
Daniel Vetter23010022013-06-20 13:31:17 +0200832static inline int
833ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
834{
835#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
836 unsigned tmp;
837
838 if (ctx->deadlock_inject_countdown-- == 0) {
839 tmp = ctx->deadlock_inject_interval;
840 if (tmp > UINT_MAX/4)
841 tmp = UINT_MAX;
842 else
843 tmp = tmp*2 + tmp + tmp/2;
844
845 ctx->deadlock_inject_interval = tmp;
846 ctx->deadlock_inject_countdown = tmp;
847 ctx->contending_lock = lock;
848
849 ww_mutex_unlock(lock);
850
851 return -EDEADLK;
852 }
853#endif
854
855 return 0;
856}
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200857
858int __sched
Nicolai Hähnlec5470b22016-12-21 19:46:33 +0100859ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200860{
Daniel Vetter23010022013-06-20 13:31:17 +0200861 int ret;
862
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200863 might_sleep();
Peter Zijlstra427b1822016-12-23 10:36:00 +0100864 ret = __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
Peter Zijlstracf702ed2021-08-15 23:28:38 +0200865 0, _RET_IP_, ctx);
Nicolai Hähnleea9e0fb2016-12-21 19:46:32 +0100866 if (!ret && ctx && ctx->acquired > 1)
Daniel Vetter23010022013-06-20 13:31:17 +0200867 return ww_mutex_deadlock_injection(lock, ctx);
868
869 return ret;
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200870}
Nicolai Hähnlec5470b22016-12-21 19:46:33 +0100871EXPORT_SYMBOL_GPL(ww_mutex_lock);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200872
873int __sched
Nicolai Hähnlec5470b22016-12-21 19:46:33 +0100874ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200875{
Daniel Vetter23010022013-06-20 13:31:17 +0200876 int ret;
877
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200878 might_sleep();
Peter Zijlstra427b1822016-12-23 10:36:00 +0100879 ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
Peter Zijlstracf702ed2021-08-15 23:28:38 +0200880 0, _RET_IP_, ctx);
Daniel Vetter23010022013-06-20 13:31:17 +0200881
Nicolai Hähnleea9e0fb2016-12-21 19:46:32 +0100882 if (!ret && ctx && ctx->acquired > 1)
Daniel Vetter23010022013-06-20 13:31:17 +0200883 return ww_mutex_deadlock_injection(lock, ctx);
884
885 return ret;
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200886}
Nicolai Hähnlec5470b22016-12-21 19:46:33 +0100887EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +0200888
Ingo Molnaref5d4702006-07-03 00:24:55 -0700889#endif
890
Ingo Molnar6053ee32006-01-09 15:59:19 -0800891/*
892 * Release the lock, slowpath:
893 */
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200894static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
Ingo Molnar6053ee32006-01-09 15:59:19 -0800895{
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200896 struct task_struct *next = NULL;
Waiman Long194a6b52016-11-17 11:46:38 -0500897 DEFINE_WAKE_Q(wake_q);
Peter Zijlstrab9c16a02017-01-17 16:06:09 +0100898 unsigned long owner;
Ingo Molnar6053ee32006-01-09 15:59:19 -0800899
Qian Cai5facae42019-09-19 12:09:40 -0400900 mutex_release(&lock->dep_map, ip);
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200901
Ingo Molnar6053ee32006-01-09 15:59:19 -0800902 /*
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200903 * Release the lock before (potentially) taking the spinlock such that
904 * other contenders can get on with things ASAP.
905 *
906 * Except when HANDOFF, in that case we must not clear the owner field,
907 * but instead set it to the top waiter.
Ingo Molnar6053ee32006-01-09 15:59:19 -0800908 */
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200909 owner = atomic_long_read(&lock->owner);
910 for (;;) {
Peter Zijlstrae6b44572021-06-30 17:35:20 +0200911 MUTEX_WARN_ON(__owner_task(owner) != current);
912 MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200913
914 if (owner & MUTEX_FLAG_HANDOFF)
915 break;
916
Peter Zijlstraab4e4d92021-06-30 17:35:17 +0200917 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) {
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200918 if (owner & MUTEX_FLAG_WAITERS)
919 break;
920
921 return;
922 }
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200923 }
Ingo Molnar6053ee32006-01-09 15:59:19 -0800924
Thomas Gleixnerebf4c552021-08-15 23:28:36 +0200925 raw_spin_lock(&lock->wait_lock);
Jason Low1d8fe7d2014-01-28 11:13:14 -0800926 debug_mutex_unlock(lock);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800927 if (!list_empty(&lock->wait_list)) {
928 /* get the first entry from the wait-list: */
929 struct mutex_waiter *waiter =
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200930 list_first_entry(&lock->wait_list,
931 struct mutex_waiter, list);
932
933 next = waiter->task;
Ingo Molnar6053ee32006-01-09 15:59:19 -0800934
935 debug_mutex_wake_waiter(lock, waiter);
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200936 wake_q_add(&wake_q, next);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800937 }
938
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200939 if (owner & MUTEX_FLAG_HANDOFF)
940 __mutex_handoff(lock, next);
941
Thomas Gleixnerebf4c552021-08-15 23:28:36 +0200942 raw_spin_unlock(&lock->wait_lock);
Peter Zijlstra9d659ae2016-08-23 14:40:16 +0200943
Davidlohr Bueso1329ce62016-01-24 18:23:43 -0800944 wake_up_q(&wake_q);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800945}
946
Peter Zijlstrae4564f72007-10-11 22:11:12 +0200947#ifndef CONFIG_DEBUG_LOCK_ALLOC
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700948/*
Ingo Molnar6053ee32006-01-09 15:59:19 -0800949 * Here come the less common (and hence less performance-critical) APIs:
950 * mutex_lock_interruptible() and mutex_trylock().
951 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800952static noinline int __sched
Maarten Lankhorsta41b56e2013-06-20 13:31:05 +0200953__mutex_lock_killable_slowpath(struct mutex *lock);
Liam R. Howlettad776532007-12-06 17:37:59 -0500954
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800955static noinline int __sched
Maarten Lankhorsta41b56e2013-06-20 13:31:05 +0200956__mutex_lock_interruptible_slowpath(struct mutex *lock);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800957
Randy Dunlapef5dc122010-09-02 15:48:16 -0700958/**
Matthew Wilcox45dbac02018-03-15 04:58:12 -0700959 * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
960 * @lock: The mutex to be acquired.
Ingo Molnar6053ee32006-01-09 15:59:19 -0800961 *
Matthew Wilcox45dbac02018-03-15 04:58:12 -0700962 * Lock the mutex like mutex_lock(). If a signal is delivered while the
963 * process is sleeping, this function will return without acquiring the
964 * mutex.
Ingo Molnar6053ee32006-01-09 15:59:19 -0800965 *
Matthew Wilcox45dbac02018-03-15 04:58:12 -0700966 * Context: Process context.
967 * Return: 0 if the lock was successfully acquired or %-EINTR if a
968 * signal arrived.
Ingo Molnar6053ee32006-01-09 15:59:19 -0800969 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800970int __sched mutex_lock_interruptible(struct mutex *lock)
Ingo Molnar6053ee32006-01-09 15:59:19 -0800971{
Ingo Molnarc544bdb2006-01-10 22:10:36 +0100972 might_sleep();
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200973
974 if (__mutex_trylock_fast(lock))
Maarten Lankhorsta41b56e2013-06-20 13:31:05 +0200975 return 0;
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200976
977 return __mutex_lock_interruptible_slowpath(lock);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800978}
979
980EXPORT_SYMBOL(mutex_lock_interruptible);
981
Matthew Wilcox45dbac02018-03-15 04:58:12 -0700982/**
983 * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
984 * @lock: The mutex to be acquired.
985 *
986 * Lock the mutex like mutex_lock(). If a signal which will be fatal to
987 * the current process is delivered while the process is sleeping, this
988 * function will return without acquiring the mutex.
989 *
990 * Context: Process context.
991 * Return: 0 if the lock was successfully acquired or %-EINTR if a
992 * fatal signal arrived.
993 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800994int __sched mutex_lock_killable(struct mutex *lock)
Liam R. Howlettad776532007-12-06 17:37:59 -0500995{
996 might_sleep();
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +0200997
998 if (__mutex_trylock_fast(lock))
Maarten Lankhorsta41b56e2013-06-20 13:31:05 +0200999 return 0;
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +02001000
1001 return __mutex_lock_killable_slowpath(lock);
Liam R. Howlettad776532007-12-06 17:37:59 -05001002}
1003EXPORT_SYMBOL(mutex_lock_killable);
1004
Matthew Wilcox45dbac02018-03-15 04:58:12 -07001005/**
1006 * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
1007 * @lock: The mutex to be acquired.
1008 *
1009 * Lock the mutex like mutex_lock(). While the task is waiting for this
1010 * mutex, it will be accounted as being in the IO wait state by the
1011 * scheduler.
1012 *
1013 * Context: Process context.
1014 */
Tejun Heo1460cb62016-10-28 12:58:11 -04001015void __sched mutex_lock_io(struct mutex *lock)
1016{
1017 int token;
1018
1019 token = io_schedule_prepare();
1020 mutex_lock(lock);
1021 io_schedule_finish(token);
1022}
1023EXPORT_SYMBOL_GPL(mutex_lock_io);
1024
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +02001025static noinline void __sched
1026__mutex_lock_slowpath(struct mutex *lock)
Peter Zijlstrae4564f72007-10-11 22:11:12 +02001027{
Peter Zijlstra427b1822016-12-23 10:36:00 +01001028 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
Peter Zijlstrae4564f72007-10-11 22:11:12 +02001029}
1030
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001031static noinline int __sched
Maarten Lankhorsta41b56e2013-06-20 13:31:05 +02001032__mutex_lock_killable_slowpath(struct mutex *lock)
Liam R. Howlettad776532007-12-06 17:37:59 -05001033{
Peter Zijlstra427b1822016-12-23 10:36:00 +01001034 return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
Liam R. Howlettad776532007-12-06 17:37:59 -05001035}
1036
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001037static noinline int __sched
Maarten Lankhorsta41b56e2013-06-20 13:31:05 +02001038__mutex_lock_interruptible_slowpath(struct mutex *lock)
Ingo Molnar6053ee32006-01-09 15:59:19 -08001039{
Peter Zijlstra427b1822016-12-23 10:36:00 +01001040 return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
Ingo Molnar6053ee32006-01-09 15:59:19 -08001041}
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001042
1043static noinline int __sched
1044__ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1045{
Peter Zijlstracf702ed2021-08-15 23:28:38 +02001046 return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0,
Peter Zijlstra427b1822016-12-23 10:36:00 +01001047 _RET_IP_, ctx);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001048}
1049
1050static noinline int __sched
1051__ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
1052 struct ww_acquire_ctx *ctx)
1053{
Peter Zijlstracf702ed2021-08-15 23:28:38 +02001054 return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0,
Peter Zijlstra427b1822016-12-23 10:36:00 +01001055 _RET_IP_, ctx);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001056}
1057
Peter Zijlstrae4564f72007-10-11 22:11:12 +02001058#endif
Ingo Molnar6053ee32006-01-09 15:59:19 -08001059
Randy Dunlapef5dc122010-09-02 15:48:16 -07001060/**
1061 * mutex_trylock - try to acquire the mutex, without waiting
Ingo Molnar6053ee32006-01-09 15:59:19 -08001062 * @lock: the mutex to be acquired
1063 *
1064 * Try to acquire the mutex atomically. Returns 1 if the mutex
1065 * has been acquired successfully, and 0 on contention.
1066 *
1067 * NOTE: this function follows the spin_trylock() convention, so
Randy Dunlapef5dc122010-09-02 15:48:16 -07001068 * it is negated from the down_trylock() return values! Be careful
Ingo Molnar6053ee32006-01-09 15:59:19 -08001069 * about this when converting semaphore users to mutexes.
1070 *
1071 * This function must not be used in interrupt context. The
1072 * mutex must be released by the same task that acquired it.
1073 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001074int __sched mutex_trylock(struct mutex *lock)
Ingo Molnar6053ee32006-01-09 15:59:19 -08001075{
Sebastian Andrzej Siewior6c11c6e2019-07-03 11:21:26 +02001076 bool locked;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01001077
Peter Zijlstrae6b44572021-06-30 17:35:20 +02001078 MUTEX_WARN_ON(lock->magic != lock);
Sebastian Andrzej Siewior6c11c6e2019-07-03 11:21:26 +02001079
1080 locked = __mutex_trylock(lock);
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +02001081 if (locked)
1082 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01001083
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +02001084 return locked;
Ingo Molnar6053ee32006-01-09 15:59:19 -08001085}
Ingo Molnar6053ee32006-01-09 15:59:19 -08001086EXPORT_SYMBOL(mutex_trylock);
Andrew Mortona511e3f2009-04-29 15:59:58 -07001087
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001088#ifndef CONFIG_DEBUG_LOCK_ALLOC
1089int __sched
Nicolai Hähnlec5470b22016-12-21 19:46:33 +01001090ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001091{
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001092 might_sleep();
1093
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +02001094 if (__mutex_trylock_fast(&lock->base)) {
Nicolai Hähnleea9e0fb2016-12-21 19:46:32 +01001095 if (ctx)
1096 ww_mutex_set_context_fastpath(lock, ctx);
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +02001097 return 0;
1098 }
1099
1100 return __ww_mutex_lock_slowpath(lock, ctx);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001101}
Nicolai Hähnlec5470b22016-12-21 19:46:33 +01001102EXPORT_SYMBOL(ww_mutex_lock);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001103
1104int __sched
Nicolai Hähnlec5470b22016-12-21 19:46:33 +01001105ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001106{
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001107 might_sleep();
1108
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +02001109 if (__mutex_trylock_fast(&lock->base)) {
Nicolai Hähnleea9e0fb2016-12-21 19:46:32 +01001110 if (ctx)
1111 ww_mutex_set_context_fastpath(lock, ctx);
Peter Zijlstra3ca0ff52016-08-23 13:36:04 +02001112 return 0;
1113 }
1114
1115 return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001116}
Nicolai Hähnlec5470b22016-12-21 19:46:33 +01001117EXPORT_SYMBOL(ww_mutex_lock_interruptible);
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001118
Thomas Gleixnerbb630f92021-08-15 23:29:01 +02001119#endif /* !CONFIG_DEBUG_LOCK_ALLOC */
1120#endif /* !CONFIG_PREEMPT_RT */
Maarten Lankhorst040a0a32013-06-24 10:30:04 +02001121
Andrew Mortona511e3f2009-04-29 15:59:58 -07001122/**
1123 * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
1124 * @cnt: the atomic which we are to dec
1125 * @lock: the mutex to return holding if we dec to 0
1126 *
1127 * return true and hold lock if we dec to 0, return false otherwise
1128 */
1129int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
1130{
1131 /* dec if we can't possibly hit 0 */
1132 if (atomic_add_unless(cnt, -1, 1))
1133 return 0;
1134 /* we might hit 0, so take the lock */
1135 mutex_lock(lock);
1136 if (!atomic_dec_and_test(cnt)) {
1137 /* when we actually did the dec, we didn't hit 0 */
1138 mutex_unlock(lock);
1139 return 0;
1140 }
1141 /* we hit 0, and we hold the lock */
1142 return 1;
1143}
1144EXPORT_SYMBOL(atomic_dec_and_mutex_lock);