Skip to content

Commit cd8084f

Browse files
lgebyungchulparkingomolnar
authored andcommitted
locking/lockdep: Apply crossrelease to completions
Although wait_for_completion() and its family can cause deadlock, the lock correctness validator could not be applied to them until now, because things like complete() are usually called in a different context from the waiting context, which violates lockdep's assumption. Thanks to CONFIG_LOCKDEP_CROSSRELEASE, we can now apply the lockdep detector to those completion operations. Applied it. Signed-off-by: Byungchul Park <byungchul.park@lge.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: akpm@linux-foundation.org Cc: boqun.feng@gmail.com Cc: kernel-team@lge.com Cc: kirill@shutemov.name Cc: npiggin@gmail.com Cc: walken@google.com Cc: willy@infradead.org Link: http://lkml.kernel.org/r/1502089981-21272-10-git-send-email-byungchul.park@lge.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 383a4bc commit cd8084f

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎include/linux/completion.h‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010

1111
#include <linux/wait.h>
12+
#ifdef CONFIG_LOCKDEP_COMPLETE
13+
#include <linux/lockdep.h>
14+
#endif
1215

1316
/*
1417
* struct completion - structure used to maintain state for a "completion"
@@ -25,10 +28,50 @@
2528
struct completion {
2629
unsigned int done;
2730
wait_queue_head_t wait;
31+
#ifdef CONFIG_LOCKDEP_COMPLETE
32+
struct lockdep_map_cross map;
33+
#endif
2834
};
2935

36+
#ifdef CONFIG_LOCKDEP_COMPLETE
37+
static inline void complete_acquire(struct completion *x)
38+
{
39+
lock_acquire_exclusive((struct lockdep_map *)&x->map, 0, 0, NULL, _RET_IP_);
40+
}
41+
42+
static inline void complete_release(struct completion *x)
43+
{
44+
lock_release((struct lockdep_map *)&x->map, 0, _RET_IP_);
45+
}
46+
47+
static inline void complete_release_commit(struct completion *x)
48+
{
49+
lock_commit_crosslock((struct lockdep_map *)&x->map);
50+
}
51+
52+
#define init_completion(x) \
53+
do { \
54+
static struct lock_class_key __key; \
55+
lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map, \
56+
"(complete)" #x, \
57+
&__key, 0); \
58+
__init_completion(x); \
59+
} while (0)
60+
#else
61+
#define init_completion(x) __init_completion(x)
62+
static inline void complete_acquire(struct completion *x) {}
63+
static inline void complete_release(struct completion *x) {}
64+
static inline void complete_release_commit(struct completion *x) {}
65+
#endif
66+
67+
#ifdef CONFIG_LOCKDEP_COMPLETE
68+
#define COMPLETION_INITIALIZER(work) \
69+
{ 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait), \
70+
STATIC_CROSS_LOCKDEP_MAP_INIT("(complete)" #work, &(work)) }
71+
#else
3072
#define COMPLETION_INITIALIZER(work) \
3173
{ 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
74+
#endif
3275

3376
#define COMPLETION_INITIALIZER_ONSTACK(work) \
3477
({ init_completion(&work); work; })
@@ -70,7 +113,7 @@ struct completion {
70113
* This inline function will initialize a dynamically created completion
71114
* structure.
72115
*/
73-
static inline void init_completion(struct completion *x)
116+
static inline void __init_completion(struct completion *x)
74117
{
75118
x->done = 0;
76119
init_waitqueue_head(&x->wait);

‎kernel/sched/completion.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ void complete(struct completion *x)
3232
unsigned long flags;
3333

3434
spin_lock_irqsave(&x->wait.lock, flags);
35+
36+
/*
37+
* Perform commit of crossrelease here.
38+
*/
39+
complete_release_commit(x);
40+
3541
if (x->done != UINT_MAX)
3642
x->done++;
3743
__wake_up_locked(&x->wait, TASK_NORMAL, 1);
@@ -92,9 +98,14 @@ __wait_for_common(struct completion *x,
9298
{
9399
might_sleep();
94100

101+
complete_acquire(x);
102+
95103
spin_lock_irq(&x->wait.lock);
96104
timeout = do_wait_for_common(x, action, timeout, state);
97105
spin_unlock_irq(&x->wait.lock);
106+
107+
complete_release(x);
108+
98109
return timeout;
99110
}
100111

‎lib/Kconfig.debug‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,15 @@ config LOCKDEP_CROSSRELEASE
11621162
such as page locks or completions can use the lock correctness
11631163
detector, lockdep.
11641164

1165+
config LOCKDEP_COMPLETE
1166+
bool "Lock debugging: allow completions to use deadlock detector"
1167+
depends on PROVE_LOCKING
1168+
select LOCKDEP_CROSSRELEASE
1169+
default n
1170+
help
1171+
A deadlock caused by wait_for_completion() and complete() can be
1172+
detected by lockdep using crossrelease feature.
1173+
11651174
config DEBUG_LOCKDEP
11661175
bool "Lock dependency engine debugging"
11671176
depends on DEBUG_KERNEL && LOCKDEP

0 commit comments

Comments
 (0)