xref: /linux-6.15/kernel/locking/rtmutex_api.c (revision bb630f9f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * rtmutex API
4  */
5 #include <linux/spinlock.h>
6 #include <linux/export.h>
7 
8 #define RT_MUTEX_BUILD_MUTEX
9 #include "rtmutex.c"
10 
11 /*
12  * Max number of times we'll walk the boosting chain:
13  */
14 int max_lock_depth = 1024;
15 
16 /*
17  * Debug aware fast / slowpath lock,trylock,unlock
18  *
19  * The atomic acquire/release ops are compiled away, when either the
20  * architecture does not support cmpxchg or when debugging is enabled.
21  */
22 static __always_inline int __rt_mutex_lock_common(struct rt_mutex *lock,
23 						  unsigned int state,
24 						  unsigned int subclass)
25 {
26 	int ret;
27 
28 	might_sleep();
29 	mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
30 	ret = __rt_mutex_lock(&lock->rtmutex, state);
31 	if (ret)
32 		mutex_release(&lock->dep_map, _RET_IP_);
33 	return ret;
34 }
35 
36 void rt_mutex_base_init(struct rt_mutex_base *rtb)
37 {
38 	__rt_mutex_base_init(rtb);
39 }
40 EXPORT_SYMBOL(rt_mutex_base_init);
41 
42 #ifdef CONFIG_DEBUG_LOCK_ALLOC
43 /**
44  * rt_mutex_lock_nested - lock a rt_mutex
45  *
46  * @lock: the rt_mutex to be locked
47  * @subclass: the lockdep subclass
48  */
49 void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
50 {
51 	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass);
52 }
53 EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
54 
55 #else /* !CONFIG_DEBUG_LOCK_ALLOC */
56 
57 /**
58  * rt_mutex_lock - lock a rt_mutex
59  *
60  * @lock: the rt_mutex to be locked
61  */
62 void __sched rt_mutex_lock(struct rt_mutex *lock)
63 {
64 	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0);
65 }
66 EXPORT_SYMBOL_GPL(rt_mutex_lock);
67 #endif
68 
69 /**
70  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
71  *
72  * @lock:		the rt_mutex to be locked
73  *
74  * Returns:
75  *  0		on success
76  * -EINTR	when interrupted by a signal
77  */
78 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
79 {
80 	return __rt_mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0);
81 }
82 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
83 
84 /**
85  * rt_mutex_trylock - try to lock a rt_mutex
86  *
87  * @lock:	the rt_mutex to be locked
88  *
89  * This function can only be called in thread context. It's safe to call it
90  * from atomic regions, but not from hard or soft interrupt context.
91  *
92  * Returns:
93  *  1 on success
94  *  0 on contention
95  */
96 int __sched rt_mutex_trylock(struct rt_mutex *lock)
97 {
98 	int ret;
99 
100 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
101 		return 0;
102 
103 	ret = __rt_mutex_trylock(&lock->rtmutex);
104 	if (ret)
105 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
106 
107 	return ret;
108 }
109 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
110 
111 /**
112  * rt_mutex_unlock - unlock a rt_mutex
113  *
114  * @lock: the rt_mutex to be unlocked
115  */
116 void __sched rt_mutex_unlock(struct rt_mutex *lock)
117 {
118 	mutex_release(&lock->dep_map, _RET_IP_);
119 	__rt_mutex_unlock(&lock->rtmutex);
120 }
121 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
122 
123 /*
124  * Futex variants, must not use fastpath.
125  */
126 int __sched rt_mutex_futex_trylock(struct rt_mutex_base *lock)
127 {
128 	return rt_mutex_slowtrylock(lock);
129 }
130 
131 int __sched __rt_mutex_futex_trylock(struct rt_mutex_base *lock)
132 {
133 	return __rt_mutex_slowtrylock(lock);
134 }
135 
136 /**
137  * __rt_mutex_futex_unlock - Futex variant, that since futex variants
138  * do not use the fast-path, can be simple and will not need to retry.
139  *
140  * @lock:	The rt_mutex to be unlocked
141  * @wqh:	The wake queue head from which to get the next lock waiter
142  */
143 bool __sched __rt_mutex_futex_unlock(struct rt_mutex_base *lock,
144 				     struct rt_wake_q_head *wqh)
145 {
146 	lockdep_assert_held(&lock->wait_lock);
147 
148 	debug_rt_mutex_unlock(lock);
149 
150 	if (!rt_mutex_has_waiters(lock)) {
151 		lock->owner = NULL;
152 		return false; /* done */
153 	}
154 
155 	/*
156 	 * We've already deboosted, mark_wakeup_next_waiter() will
157 	 * retain preempt_disabled when we drop the wait_lock, to
158 	 * avoid inversion prior to the wakeup.  preempt_disable()
159 	 * therein pairs with rt_mutex_postunlock().
160 	 */
161 	mark_wakeup_next_waiter(wqh, lock);
162 
163 	return true; /* call postunlock() */
164 }
165 
166 void __sched rt_mutex_futex_unlock(struct rt_mutex_base *lock)
167 {
168 	DEFINE_RT_WAKE_Q(wqh);
169 	unsigned long flags;
170 	bool postunlock;
171 
172 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
173 	postunlock = __rt_mutex_futex_unlock(lock, &wqh);
174 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
175 
176 	if (postunlock)
177 		rt_mutex_postunlock(&wqh);
178 }
179 
180 /**
181  * __rt_mutex_init - initialize the rt_mutex
182  *
183  * @lock:	The rt_mutex to be initialized
184  * @name:	The lock name used for debugging
185  * @key:	The lock class key used for debugging
186  *
187  * Initialize the rt_mutex to unlocked state.
188  *
189  * Initializing of a locked rt_mutex is not allowed
190  */
191 void __sched __rt_mutex_init(struct rt_mutex *lock, const char *name,
192 			     struct lock_class_key *key)
193 {
194 	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
195 	__rt_mutex_base_init(&lock->rtmutex);
196 	lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP);
197 }
198 EXPORT_SYMBOL_GPL(__rt_mutex_init);
199 
200 /**
201  * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
202  *				proxy owner
203  *
204  * @lock:	the rt_mutex to be locked
205  * @proxy_owner:the task to set as owner
206  *
207  * No locking. Caller has to do serializing itself
208  *
209  * Special API call for PI-futex support. This initializes the rtmutex and
210  * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
211  * possible at this point because the pi_state which contains the rtmutex
212  * is not yet visible to other tasks.
213  */
214 void __sched rt_mutex_init_proxy_locked(struct rt_mutex_base *lock,
215 					struct task_struct *proxy_owner)
216 {
217 	__rt_mutex_base_init(lock);
218 	rt_mutex_set_owner(lock, proxy_owner);
219 }
220 
221 /**
222  * rt_mutex_proxy_unlock - release a lock on behalf of owner
223  *
224  * @lock:	the rt_mutex to be locked
225  *
226  * No locking. Caller has to do serializing itself
227  *
228  * Special API call for PI-futex support. This just cleans up the rtmutex
229  * (debugging) state. Concurrent operations on this rt_mutex are not
230  * possible because it belongs to the pi_state which is about to be freed
231  * and it is not longer visible to other tasks.
232  */
233 void __sched rt_mutex_proxy_unlock(struct rt_mutex_base *lock)
234 {
235 	debug_rt_mutex_proxy_unlock(lock);
236 	rt_mutex_set_owner(lock, NULL);
237 }
238 
239 /**
240  * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
241  * @lock:		the rt_mutex to take
242  * @waiter:		the pre-initialized rt_mutex_waiter
243  * @task:		the task to prepare
244  *
245  * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
246  * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
247  *
248  * NOTE: does _NOT_ remove the @waiter on failure; must either call
249  * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
250  *
251  * Returns:
252  *  0 - task blocked on lock
253  *  1 - acquired the lock for task, caller should wake it up
254  * <0 - error
255  *
256  * Special API call for PI-futex support.
257  */
258 int __sched __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
259 					struct rt_mutex_waiter *waiter,
260 					struct task_struct *task)
261 {
262 	int ret;
263 
264 	lockdep_assert_held(&lock->wait_lock);
265 
266 	if (try_to_take_rt_mutex(lock, task, NULL))
267 		return 1;
268 
269 	/* We enforce deadlock detection for futexes */
270 	ret = task_blocks_on_rt_mutex(lock, waiter, task, NULL,
271 				      RT_MUTEX_FULL_CHAINWALK);
272 
273 	if (ret && !rt_mutex_owner(lock)) {
274 		/*
275 		 * Reset the return value. We might have
276 		 * returned with -EDEADLK and the owner
277 		 * released the lock while we were walking the
278 		 * pi chain.  Let the waiter sort it out.
279 		 */
280 		ret = 0;
281 	}
282 
283 	return ret;
284 }
285 
286 /**
287  * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
288  * @lock:		the rt_mutex to take
289  * @waiter:		the pre-initialized rt_mutex_waiter
290  * @task:		the task to prepare
291  *
292  * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
293  * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
294  *
295  * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
296  * on failure.
297  *
298  * Returns:
299  *  0 - task blocked on lock
300  *  1 - acquired the lock for task, caller should wake it up
301  * <0 - error
302  *
303  * Special API call for PI-futex support.
304  */
305 int __sched rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
306 				      struct rt_mutex_waiter *waiter,
307 				      struct task_struct *task)
308 {
309 	int ret;
310 
311 	raw_spin_lock_irq(&lock->wait_lock);
312 	ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
313 	if (unlikely(ret))
314 		remove_waiter(lock, waiter);
315 	raw_spin_unlock_irq(&lock->wait_lock);
316 
317 	return ret;
318 }
319 
320 /**
321  * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
322  * @lock:		the rt_mutex we were woken on
323  * @to:			the timeout, null if none. hrtimer should already have
324  *			been started.
325  * @waiter:		the pre-initialized rt_mutex_waiter
326  *
327  * Wait for the lock acquisition started on our behalf by
328  * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
329  * rt_mutex_cleanup_proxy_lock().
330  *
331  * Returns:
332  *  0 - success
333  * <0 - error, one of -EINTR, -ETIMEDOUT
334  *
335  * Special API call for PI-futex support
336  */
337 int __sched rt_mutex_wait_proxy_lock(struct rt_mutex_base *lock,
338 				     struct hrtimer_sleeper *to,
339 				     struct rt_mutex_waiter *waiter)
340 {
341 	int ret;
342 
343 	raw_spin_lock_irq(&lock->wait_lock);
344 	/* sleep on the mutex */
345 	set_current_state(TASK_INTERRUPTIBLE);
346 	ret = rt_mutex_slowlock_block(lock, NULL, TASK_INTERRUPTIBLE, to, waiter);
347 	/*
348 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
349 	 * have to fix that up.
350 	 */
351 	fixup_rt_mutex_waiters(lock);
352 	raw_spin_unlock_irq(&lock->wait_lock);
353 
354 	return ret;
355 }
356 
357 /**
358  * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
359  * @lock:		the rt_mutex we were woken on
360  * @waiter:		the pre-initialized rt_mutex_waiter
361  *
362  * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
363  * rt_mutex_wait_proxy_lock().
364  *
365  * Unless we acquired the lock; we're still enqueued on the wait-list and can
366  * in fact still be granted ownership until we're removed. Therefore we can
367  * find we are in fact the owner and must disregard the
368  * rt_mutex_wait_proxy_lock() failure.
369  *
370  * Returns:
371  *  true  - did the cleanup, we done.
372  *  false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
373  *          caller should disregards its return value.
374  *
375  * Special API call for PI-futex support
376  */
377 bool __sched rt_mutex_cleanup_proxy_lock(struct rt_mutex_base *lock,
378 					 struct rt_mutex_waiter *waiter)
379 {
380 	bool cleanup = false;
381 
382 	raw_spin_lock_irq(&lock->wait_lock);
383 	/*
384 	 * Do an unconditional try-lock, this deals with the lock stealing
385 	 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
386 	 * sets a NULL owner.
387 	 *
388 	 * We're not interested in the return value, because the subsequent
389 	 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
390 	 * we will own the lock and it will have removed the waiter. If we
391 	 * failed the trylock, we're still not owner and we need to remove
392 	 * ourselves.
393 	 */
394 	try_to_take_rt_mutex(lock, current, waiter);
395 	/*
396 	 * Unless we're the owner; we're still enqueued on the wait_list.
397 	 * So check if we became owner, if not, take us off the wait_list.
398 	 */
399 	if (rt_mutex_owner(lock) != current) {
400 		remove_waiter(lock, waiter);
401 		cleanup = true;
402 	}
403 	/*
404 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
405 	 * have to fix that up.
406 	 */
407 	fixup_rt_mutex_waiters(lock);
408 
409 	raw_spin_unlock_irq(&lock->wait_lock);
410 
411 	return cleanup;
412 }
413 
414 /*
415  * Recheck the pi chain, in case we got a priority setting
416  *
417  * Called from sched_setscheduler
418  */
419 void __sched rt_mutex_adjust_pi(struct task_struct *task)
420 {
421 	struct rt_mutex_waiter *waiter;
422 	struct rt_mutex_base *next_lock;
423 	unsigned long flags;
424 
425 	raw_spin_lock_irqsave(&task->pi_lock, flags);
426 
427 	waiter = task->pi_blocked_on;
428 	if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
429 		raw_spin_unlock_irqrestore(&task->pi_lock, flags);
430 		return;
431 	}
432 	next_lock = waiter->lock;
433 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
434 
435 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
436 	get_task_struct(task);
437 
438 	rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
439 				   next_lock, NULL, task);
440 }
441 
442 /*
443  * Performs the wakeup of the top-waiter and re-enables preemption.
444  */
445 void __sched rt_mutex_postunlock(struct rt_wake_q_head *wqh)
446 {
447 	rt_mutex_wake_up_q(wqh);
448 }
449 
450 #ifdef CONFIG_DEBUG_RT_MUTEXES
451 void rt_mutex_debug_task_free(struct task_struct *task)
452 {
453 	DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
454 	DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
455 }
456 #endif
457 
458 #ifdef CONFIG_PREEMPT_RT
459 /* Mutexes */
460 void __mutex_rt_init(struct mutex *mutex, const char *name,
461 		     struct lock_class_key *key)
462 {
463 	debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
464 	lockdep_init_map_wait(&mutex->dep_map, name, key, 0, LD_WAIT_SLEEP);
465 }
466 EXPORT_SYMBOL(__mutex_rt_init);
467 
468 static __always_inline int __mutex_lock_common(struct mutex *lock,
469 					       unsigned int state,
470 					       unsigned int subclass,
471 					       struct lockdep_map *nest_lock,
472 					       unsigned long ip)
473 {
474 	int ret;
475 
476 	might_sleep();
477 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
478 	ret = __rt_mutex_lock(&lock->rtmutex, state);
479 	if (ret)
480 		mutex_release(&lock->dep_map, ip);
481 	else
482 		lock_acquired(&lock->dep_map, ip);
483 	return ret;
484 }
485 
486 #ifdef CONFIG_DEBUG_LOCK_ALLOC
487 void __sched mutex_lock_nested(struct mutex *lock, unsigned int subclass)
488 {
489 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
490 }
491 EXPORT_SYMBOL_GPL(mutex_lock_nested);
492 
493 void __sched _mutex_lock_nest_lock(struct mutex *lock,
494 				   struct lockdep_map *nest_lock)
495 {
496 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, nest_lock, _RET_IP_);
497 }
498 EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
499 
500 int __sched mutex_lock_interruptible_nested(struct mutex *lock,
501 					    unsigned int subclass)
502 {
503 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
504 }
505 EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
506 
507 int __sched mutex_lock_killable_nested(struct mutex *lock,
508 					    unsigned int subclass)
509 {
510 	return __mutex_lock_common(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
511 }
512 EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
513 
514 void __sched mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
515 {
516 	int token;
517 
518 	might_sleep();
519 
520 	token = io_schedule_prepare();
521 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
522 	io_schedule_finish(token);
523 }
524 EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
525 
526 #else /* CONFIG_DEBUG_LOCK_ALLOC */
527 
528 void __sched mutex_lock(struct mutex *lock)
529 {
530 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
531 }
532 EXPORT_SYMBOL(mutex_lock);
533 
534 int __sched mutex_lock_interruptible(struct mutex *lock)
535 {
536 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
537 }
538 EXPORT_SYMBOL(mutex_lock_interruptible);
539 
540 int __sched mutex_lock_killable(struct mutex *lock)
541 {
542 	return __mutex_lock_common(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
543 }
544 EXPORT_SYMBOL(mutex_lock_killable);
545 
546 void __sched mutex_lock_io(struct mutex *lock)
547 {
548 	int token = io_schedule_prepare();
549 
550 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
551 	io_schedule_finish(token);
552 }
553 EXPORT_SYMBOL(mutex_lock_io);
554 #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
555 
556 int __sched mutex_trylock(struct mutex *lock)
557 {
558 	int ret;
559 
560 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
561 		return 0;
562 
563 	ret = __rt_mutex_trylock(&lock->rtmutex);
564 	if (ret)
565 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
566 
567 	return ret;
568 }
569 EXPORT_SYMBOL(mutex_trylock);
570 
571 void __sched mutex_unlock(struct mutex *lock)
572 {
573 	mutex_release(&lock->dep_map, _RET_IP_);
574 	__rt_mutex_unlock(&lock->rtmutex);
575 }
576 EXPORT_SYMBOL(mutex_unlock);
577 
578 #endif /* CONFIG_PREEMPT_RT */
579