1 // SPDX-License-Identifier: GPL-2.0-only
5 #include <linux/spinlock.h>
6 #include <linux/export.h>
8 #define RT_MUTEX_BUILD_MUTEX
12 int ww_mutex_trylock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
14 struct rt_mutex *rtm = &lock->base;
17 return rt_mutex_trylock(rtm);
20 * Reset the wounded flag after a kill. No other process can
21 * race and wound us here, since they can't have a valid owner
22 * pointer if we don't have any locks held.
24 if (ww_ctx->acquired == 0)
27 if (__rt_mutex_trylock(&rtm->rtmutex)) {
28 ww_mutex_set_context_fastpath(lock, ww_ctx);
29 mutex_acquire_nest(&rtm->dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_);
35 EXPORT_SYMBOL(ww_mutex_trylock);
38 __ww_rt_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx,
39 unsigned int state, unsigned long ip)
41 struct lockdep_map __maybe_unused *nest_lock = NULL;
42 struct rt_mutex *rtm = &lock->base;
48 if (unlikely(ww_ctx == READ_ONCE(lock->ctx)))
52 * Reset the wounded flag after a kill. No other process can
53 * race and wound us here, since they can't have a valid owner
54 * pointer if we don't have any locks held.
56 if (ww_ctx->acquired == 0)
59 #ifdef CONFIG_DEBUG_LOCK_ALLOC
60 nest_lock = &ww_ctx->dep_map;
63 mutex_acquire_nest(&rtm->dep_map, 0, 0, nest_lock, ip);
65 if (likely(rt_mutex_cmpxchg_acquire(&rtm->rtmutex, NULL, current))) {
67 ww_mutex_set_context_fastpath(lock, ww_ctx);
71 ret = rt_mutex_slowlock(&rtm->rtmutex, ww_ctx, state);
74 mutex_release(&rtm->dep_map, ip);
79 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
81 return __ww_rt_mutex_lock(lock, ctx, TASK_UNINTERRUPTIBLE, _RET_IP_);
83 EXPORT_SYMBOL(ww_mutex_lock);
86 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
88 return __ww_rt_mutex_lock(lock, ctx, TASK_INTERRUPTIBLE, _RET_IP_);
90 EXPORT_SYMBOL(ww_mutex_lock_interruptible);
92 void __sched ww_mutex_unlock(struct ww_mutex *lock)
94 struct rt_mutex *rtm = &lock->base;
96 __ww_mutex_unlock(lock);
98 mutex_release(&rtm->dep_map, _RET_IP_);
99 __rt_mutex_unlock(&rtm->rtmutex);
101 EXPORT_SYMBOL(ww_mutex_unlock);