Skip to content

Commit 23f78d4

Browse files
Ingo MolnarLinus Torvalds
authored andcommitted
[PATCH] pi-futex: rt mutex core
Core functions for the rt-mutex subsystem. Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Arjan van de Ven <arjan@linux.intel.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent b29739f commit 23f78d4

11 files changed

Lines changed: 1181 additions & 0 deletions

File tree

‎include/linux/init_task.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ extern struct group_info init_groups;
124124
.cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
125125
.fs_excl = ATOMIC_INIT(0), \
126126
.pi_lock = SPIN_LOCK_UNLOCKED, \
127+
INIT_RT_MUTEXES(tsk) \
127128
}
128129

129130

‎include/linux/rtmutex.h‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* RT Mutexes: blocking mutual exclusion locks with PI support
3+
*
4+
* started by Ingo Molnar and Thomas Gleixner:
5+
*
6+
* Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7+
* Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
8+
*
9+
* This file contains the public data structure and API definitions.
10+
*/
11+
12+
#ifndef __LINUX_RT_MUTEX_H
13+
#define __LINUX_RT_MUTEX_H
14+
15+
#include <linux/linkage.h>
16+
#include <linux/plist.h>
17+
#include <linux/spinlock_types.h>
18+
19+
/*
20+
* The rt_mutex structure
21+
*
22+
* @wait_lock: spinlock to protect the structure
23+
* @wait_list: pilist head to enqueue waiters in priority order
24+
* @owner: the mutex owner
25+
*/
26+
struct rt_mutex {
27+
spinlock_t wait_lock;
28+
struct plist_head wait_list;
29+
struct task_struct *owner;
30+
#ifdef CONFIG_DEBUG_RT_MUTEXES
31+
int save_state;
32+
struct list_head held_list_entry;
33+
unsigned long acquire_ip;
34+
const char *name, *file;
35+
int line;
36+
void *magic;
37+
#endif
38+
};
39+
40+
struct rt_mutex_waiter;
41+
struct hrtimer_sleeper;
42+
43+
#ifdef CONFIG_DEBUG_RT_MUTEXES
44+
# define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
45+
, .name = #mutexname, .file = __FILE__, .line = __LINE__
46+
# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __FUNCTION__)
47+
extern void rt_mutex_debug_task_free(struct task_struct *tsk);
48+
#else
49+
# define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
50+
# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
51+
# define rt_mutex_debug_task_free(t) do { } while (0)
52+
#endif
53+
54+
#define __RT_MUTEX_INITIALIZER(mutexname) \
55+
{ .wait_lock = SPIN_LOCK_UNLOCKED \
56+
, .wait_list = PLIST_HEAD_INIT(mutexname.wait_list, mutexname.wait_lock) \
57+
, .owner = NULL \
58+
__DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
59+
60+
#define DEFINE_RT_MUTEX(mutexname) \
61+
struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
62+
63+
/***
64+
* rt_mutex_is_locked - is the mutex locked
65+
* @lock: the mutex to be queried
66+
*
67+
* Returns 1 if the mutex is locked, 0 if unlocked.
68+
*/
69+
static inline int rt_mutex_is_locked(struct rt_mutex *lock)
70+
{
71+
return lock->owner != NULL;
72+
}
73+
74+
extern void __rt_mutex_init(struct rt_mutex *lock, const char *name);
75+
extern void rt_mutex_destroy(struct rt_mutex *lock);
76+
77+
extern void rt_mutex_lock(struct rt_mutex *lock);
78+
extern int rt_mutex_lock_interruptible(struct rt_mutex *lock,
79+
int detect_deadlock);
80+
extern int rt_mutex_timed_lock(struct rt_mutex *lock,
81+
struct hrtimer_sleeper *timeout,
82+
int detect_deadlock);
83+
84+
extern int rt_mutex_trylock(struct rt_mutex *lock);
85+
86+
extern void rt_mutex_unlock(struct rt_mutex *lock);
87+
88+
#ifdef CONFIG_DEBUG_RT_MUTEXES
89+
# define INIT_RT_MUTEX_DEBUG(tsk) \
90+
.held_list_head = LIST_HEAD_INIT(tsk.held_list_head), \
91+
.held_list_lock = SPIN_LOCK_UNLOCKED
92+
#else
93+
# define INIT_RT_MUTEX_DEBUG(tsk)
94+
#endif
95+
96+
#ifdef CONFIG_RT_MUTEXES
97+
# define INIT_RT_MUTEXES(tsk) \
98+
.pi_waiters = PLIST_HEAD_INIT(tsk.pi_waiters, tsk.pi_lock), \
99+
INIT_RT_MUTEX_DEBUG(tsk)
100+
#else
101+
# define INIT_RT_MUTEXES(tsk)
102+
#endif
103+
104+
#endif

‎include/linux/sched.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct sched_param {
7373
#include <linux/seccomp.h>
7474
#include <linux/rcupdate.h>
7575
#include <linux/futex.h>
76+
#include <linux/rtmutex.h>
7677

7778
#include <linux/time.h>
7879
#include <linux/param.h>
@@ -858,6 +859,17 @@ struct task_struct {
858859
/* Protection of the PI data structures: */
859860
spinlock_t pi_lock;
860861

862+
#ifdef CONFIG_RT_MUTEXES
863+
/* PI waiters blocked on a rt_mutex held by this task */
864+
struct plist_head pi_waiters;
865+
/* Deadlock detection and priority inheritance handling */
866+
struct rt_mutex_waiter *pi_blocked_on;
867+
# ifdef CONFIG_DEBUG_RT_MUTEXES
868+
spinlock_t held_list_lock;
869+
struct list_head held_list_head;
870+
# endif
871+
#endif
872+
861873
#ifdef CONFIG_DEBUG_MUTEXES
862874
/* mutex deadlock detection */
863875
struct mutex_waiter *blocked_on;

‎include/linux/sysctl.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ enum
149149
KERN_ACPI_VIDEO_FLAGS=71, /* int: flags for setting up video after ACPI sleep */
150150
KERN_IA64_UNALIGNED=72, /* int: ia64 unaligned userland trap enable */
151151
KERN_COMPAT_LOG=73, /* int: print compat layer messages */
152+
KERN_MAX_LOCK_DEPTH=74,
152153
};
153154

154155

‎init/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,14 @@ config BASE_FULL
339339
kernel data structures. This saves memory on small machines,
340340
but may reduce performance.
341341

342+
config RT_MUTEXES
343+
boolean
344+
select PLIST
345+
342346
config FUTEX
343347
bool "Enable futex support" if EMBEDDED
344348
default y
349+
select RT_MUTEXES
345350
help
346351
Disabling this option will cause the kernel to be built without
347352
support for "fast userspace mutexes". The resulting kernel may not

‎kernel/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ obj-$(CONFIG_FUTEX) += futex.o
1616
ifeq ($(CONFIG_COMPAT),y)
1717
obj-$(CONFIG_FUTEX) += futex_compat.o
1818
endif
19+
obj-$(CONFIG_RT_MUTEXES) += rtmutex.o
1920
obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o
2021
obj-$(CONFIG_SMP) += cpu.o spinlock.o
2122
obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o

‎kernel/fork.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static kmem_cache_t *mm_cachep;
104104
void free_task(struct task_struct *tsk)
105105
{
106106
free_thread_info(tsk->thread_info);
107+
rt_mutex_debug_task_free(tsk);
107108
free_task_struct(tsk);
108109
}
109110
EXPORT_SYMBOL(free_task);
@@ -913,6 +914,19 @@ asmlinkage long sys_set_tid_address(int __user *tidptr)
913914
return current->pid;
914915
}
915916

917+
static inline void rt_mutex_init_task(struct task_struct *p)
918+
{
919+
#ifdef CONFIG_RT_MUTEXES
920+
spin_lock_init(&p->pi_lock);
921+
plist_head_init(&p->pi_waiters, &p->pi_lock);
922+
p->pi_blocked_on = NULL;
923+
# ifdef CONFIG_DEBUG_RT_MUTEXES
924+
spin_lock_init(&p->held_list_lock);
925+
INIT_LIST_HEAD(&p->held_list_head);
926+
# endif
927+
#endif
928+
}
929+
916930
/*
917931
* This creates a new process as a copy of the old one,
918932
* but does not actually start it yet.
@@ -1034,6 +1048,8 @@ static task_t *copy_process(unsigned long clone_flags,
10341048
mpol_fix_fork_child_flag(p);
10351049
#endif
10361050

1051+
rt_mutex_init_task(p);
1052+
10371053
#ifdef CONFIG_DEBUG_MUTEXES
10381054
p->blocked_on = NULL; /* not blocked yet */
10391055
#endif

0 commit comments

Comments
 (0)