Skip to content

Commit f8635d5

Browse files
Peter Zijlstraingomolnar
authored andcommitted
locking/ww_mutex: Implement rtmutex based ww_mutex API functions
Add the actual ww_mutex API functions which replace the mutex based variant on RT enabled kernels. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20210815211305.024057938@linutronix.de
1 parent add4613 commit f8635d5

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎kernel/locking/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ obj-$(CONFIG_LOCK_SPIN_ON_OWNER) += osq_lock.o
2525
obj-$(CONFIG_PROVE_LOCKING) += spinlock.o
2626
obj-$(CONFIG_QUEUED_SPINLOCKS) += qspinlock.o
2727
obj-$(CONFIG_RT_MUTEXES) += rtmutex_api.o
28-
obj-$(CONFIG_PREEMPT_RT) += spinlock_rt.o
28+
obj-$(CONFIG_PREEMPT_RT) += spinlock_rt.o ww_rt_mutex.o
2929
obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
3030
obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
3131
obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o

‎kernel/locking/ww_rt_mutex.c‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
#define WW_RT
10+
#include "rtmutex.c"
11+
12+
static int __sched
13+
__ww_rt_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx,
14+
unsigned int state, unsigned long ip)
15+
{
16+
struct lockdep_map __maybe_unused *nest_lock = NULL;
17+
struct rt_mutex *rtm = &lock->base;
18+
int ret;
19+
20+
might_sleep();
21+
22+
if (ww_ctx) {
23+
if (unlikely(ww_ctx == READ_ONCE(lock->ctx)))
24+
return -EALREADY;
25+
26+
/*
27+
* Reset the wounded flag after a kill. No other process can
28+
* race and wound us here, since they can't have a valid owner
29+
* pointer if we don't have any locks held.
30+
*/
31+
if (ww_ctx->acquired == 0)
32+
ww_ctx->wounded = 0;
33+
34+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
35+
nest_lock = &ww_ctx->dep_map;
36+
#endif
37+
}
38+
mutex_acquire_nest(&rtm->dep_map, 0, 0, nest_lock, ip);
39+
40+
if (likely(rt_mutex_cmpxchg_acquire(&rtm->rtmutex, NULL, current))) {
41+
if (ww_ctx)
42+
ww_mutex_set_context_fastpath(lock, ww_ctx);
43+
return 0;
44+
}
45+
46+
ret = rt_mutex_slowlock(&rtm->rtmutex, ww_ctx, state);
47+
48+
if (ret)
49+
mutex_release(&rtm->dep_map, ip);
50+
return ret;
51+
}
52+
53+
int __sched
54+
ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
55+
{
56+
return __ww_rt_mutex_lock(lock, ctx, TASK_UNINTERRUPTIBLE, _RET_IP_);
57+
}
58+
EXPORT_SYMBOL(ww_mutex_lock);
59+
60+
int __sched
61+
ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
62+
{
63+
return __ww_rt_mutex_lock(lock, ctx, TASK_INTERRUPTIBLE, _RET_IP_);
64+
}
65+
EXPORT_SYMBOL(ww_mutex_lock_interruptible);
66+
67+
void __sched ww_mutex_unlock(struct ww_mutex *lock)
68+
{
69+
struct rt_mutex *rtm = &lock->base;
70+
71+
__ww_mutex_unlock(lock);
72+
73+
mutex_release(&rtm->dep_map, _RET_IP_);
74+
__rt_mutex_unlock(&rtm->rtmutex);
75+
}
76+
EXPORT_SYMBOL(ww_mutex_unlock);

0 commit comments

Comments
 (0)