Skip to content

Commit bb630f9

Browse files
KAGA-KOKOingomolnar
authored andcommitted
locking/rtmutex: Add mutex variant for RT
Add the necessary defines, helpers and API functions for replacing struct mutex on a PREEMPT_RT enabled kernel with an rtmutex based variant. No functional change when CONFIG_PREEMPT_RT=n 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.081517417@linutronix.de
1 parent f8635d5 commit bb630f9

4 files changed

Lines changed: 187 additions & 16 deletions

File tree

‎include/linux/mutex.h‎

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020
#include <linux/osq_lock.h>
2121
#include <linux/debug_locks.h>
2222

23+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
24+
# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
25+
, .dep_map = { \
26+
.name = #lockname, \
27+
.wait_type_inner = LD_WAIT_SLEEP, \
28+
}
29+
#else
30+
# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
31+
#endif
32+
33+
#ifndef CONFIG_PREEMPT_RT
34+
2335
/*
2436
* Simple, straightforward mutexes with strict semantics:
2537
*
@@ -93,16 +105,6 @@ do { \
93105
__mutex_init((mutex), #mutex, &__key); \
94106
} while (0)
95107

96-
#ifdef CONFIG_DEBUG_LOCK_ALLOC
97-
# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
98-
, .dep_map = { \
99-
.name = #lockname, \
100-
.wait_type_inner = LD_WAIT_SLEEP, \
101-
}
102-
#else
103-
# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
104-
#endif
105-
106108
#define __MUTEX_INITIALIZER(lockname) \
107109
{ .owner = ATOMIC_LONG_INIT(0) \
108110
, .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
@@ -124,6 +126,50 @@ extern void __mutex_init(struct mutex *lock, const char *name,
124126
*/
125127
extern bool mutex_is_locked(struct mutex *lock);
126128

129+
#else /* !CONFIG_PREEMPT_RT */
130+
/*
131+
* Preempt-RT variant based on rtmutexes.
132+
*/
133+
#include <linux/rtmutex.h>
134+
135+
struct mutex {
136+
struct rt_mutex_base rtmutex;
137+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
138+
struct lockdep_map dep_map;
139+
#endif
140+
};
141+
142+
#define __MUTEX_INITIALIZER(mutexname) \
143+
{ \
144+
.rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex) \
145+
__DEP_MAP_MUTEX_INITIALIZER(mutexname) \
146+
}
147+
148+
#define DEFINE_MUTEX(mutexname) \
149+
struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
150+
151+
extern void __mutex_rt_init(struct mutex *lock, const char *name,
152+
struct lock_class_key *key);
153+
extern int mutex_trylock(struct mutex *lock);
154+
155+
static inline void mutex_destroy(struct mutex *lock) { }
156+
157+
#define mutex_is_locked(l) rt_mutex_base_is_locked(&(l)->rtmutex)
158+
159+
#define __mutex_init(mutex, name, key) \
160+
do { \
161+
rt_mutex_base_init(&(mutex)->rtmutex); \
162+
__mutex_rt_init((mutex), name, key); \
163+
} while (0)
164+
165+
#define mutex_init(mutex) \
166+
do { \
167+
static struct lock_class_key __key; \
168+
\
169+
__mutex_init((mutex), #mutex, &__key); \
170+
} while (0)
171+
#endif /* CONFIG_PREEMPT_RT */
172+
127173
/*
128174
* See kernel/locking/mutex.c for detailed documentation of these APIs.
129175
* Also see Documentation/locking/mutex-design.rst.

‎kernel/locking/mutex.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/debug_locks.h>
3131
#include <linux/osq_lock.h>
3232

33+
#ifndef CONFIG_PREEMPT_RT
3334
#include "mutex.h"
3435

3536
#ifdef CONFIG_DEBUG_MUTEXES
@@ -1066,7 +1067,8 @@ ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
10661067
}
10671068
EXPORT_SYMBOL(ww_mutex_lock_interruptible);
10681069

1069-
#endif
1070+
#endif /* !CONFIG_DEBUG_LOCK_ALLOC */
1071+
#endif /* !CONFIG_PREEMPT_RT */
10701072

10711073
/**
10721074
* atomic_dec_and_mutex_lock - return holding mutex if we dec to 0

‎kernel/locking/rtmutex_api.c‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,125 @@ void rt_mutex_debug_task_free(struct task_struct *task)
454454
DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
455455
}
456456
#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 */

‎lib/Kconfig.debug‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ config PROVE_LOCKING
12351235
depends on DEBUG_KERNEL && LOCK_DEBUGGING_SUPPORT
12361236
select LOCKDEP
12371237
select DEBUG_SPINLOCK
1238-
select DEBUG_MUTEXES
1238+
select DEBUG_MUTEXES if !PREEMPT_RT
12391239
select DEBUG_RT_MUTEXES if RT_MUTEXES
12401240
select DEBUG_RWSEMS
12411241
select DEBUG_WW_MUTEX_SLOWPATH
@@ -1299,7 +1299,7 @@ config LOCK_STAT
12991299
depends on DEBUG_KERNEL && LOCK_DEBUGGING_SUPPORT
13001300
select LOCKDEP
13011301
select DEBUG_SPINLOCK
1302-
select DEBUG_MUTEXES
1302+
select DEBUG_MUTEXES if !PREEMPT_RT
13031303
select DEBUG_RT_MUTEXES if RT_MUTEXES
13041304
select DEBUG_LOCK_ALLOC
13051305
default n
@@ -1335,7 +1335,7 @@ config DEBUG_SPINLOCK
13351335

13361336
config DEBUG_MUTEXES
13371337
bool "Mutex debugging: basic checks"
1338-
depends on DEBUG_KERNEL
1338+
depends on DEBUG_KERNEL && !PREEMPT_RT
13391339
help
13401340
This feature allows mutex semantics violations to be detected and
13411341
reported.
@@ -1345,7 +1345,8 @@ config DEBUG_WW_MUTEX_SLOWPATH
13451345
depends on DEBUG_KERNEL && LOCK_DEBUGGING_SUPPORT
13461346
select DEBUG_LOCK_ALLOC
13471347
select DEBUG_SPINLOCK
1348-
select DEBUG_MUTEXES
1348+
select DEBUG_MUTEXES if !PREEMPT_RT
1349+
select DEBUG_RT_MUTEXES if PREEMPT_RT
13491350
help
13501351
This feature enables slowpath testing for w/w mutex users by
13511352
injecting additional -EDEADLK wound/backoff cases. Together with
@@ -1368,7 +1369,7 @@ config DEBUG_LOCK_ALLOC
13681369
bool "Lock debugging: detect incorrect freeing of live locks"
13691370
depends on DEBUG_KERNEL && LOCK_DEBUGGING_SUPPORT
13701371
select DEBUG_SPINLOCK
1371-
select DEBUG_MUTEXES
1372+
select DEBUG_MUTEXES if !PREEMPT_RT
13721373
select DEBUG_RT_MUTEXES if RT_MUTEXES
13731374
select LOCKDEP
13741375
help

0 commit comments

Comments
 (0)