Skip to content

Commit b09be67

Browse files
lgebyungchulparkingomolnar
authored andcommitted
locking/lockdep: Implement the 'crossrelease' feature
Lockdep is a runtime locking correctness validator that detects and reports a deadlock or its possibility by checking dependencies between locks. It's useful since it does not report just an actual deadlock but also the possibility of a deadlock that has not actually happened yet. That enables problems to be fixed before they affect real systems. However, this facility is only applicable to typical locks, such as spinlocks and mutexes, which are normally released within the context in which they were acquired. However, synchronization primitives like page locks or completions, which are allowed to be released in any context, also create dependencies and can cause a deadlock. So lockdep should track these locks to do a better job. The 'crossrelease' implementation makes these primitives also be tracked. 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-6-git-send-email-byungchul.park@lge.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ce07a94 commit b09be67

8 files changed

Lines changed: 635 additions & 34 deletions

File tree

‎include/linux/irqflags.h‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,26 @@
2323
# define trace_softirq_context(p) ((p)->softirq_context)
2424
# define trace_hardirqs_enabled(p) ((p)->hardirqs_enabled)
2525
# define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
26-
# define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
27-
# define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
28-
# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
29-
# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
26+
# define trace_hardirq_enter() \
27+
do { \
28+
current->hardirq_context++; \
29+
crossrelease_hist_start(XHLOCK_HARD); \
30+
} while (0)
31+
# define trace_hardirq_exit() \
32+
do { \
33+
current->hardirq_context--; \
34+
crossrelease_hist_end(XHLOCK_HARD); \
35+
} while (0)
36+
# define lockdep_softirq_enter() \
37+
do { \
38+
current->softirq_context++; \
39+
crossrelease_hist_start(XHLOCK_SOFT); \
40+
} while (0)
41+
# define lockdep_softirq_exit() \
42+
do { \
43+
current->softirq_context--; \
44+
crossrelease_hist_end(XHLOCK_SOFT); \
45+
} while (0)
3046
# define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
3147
#else
3248
# define trace_hardirqs_on() do { } while (0)

‎include/linux/lockdep.h‎

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ struct lockdep_map {
155155
int cpu;
156156
unsigned long ip;
157157
#endif
158+
#ifdef CONFIG_LOCKDEP_CROSSRELEASE
159+
/*
160+
* Whether it's a crosslock.
161+
*/
162+
int cross;
163+
#endif
158164
};
159165

160166
static inline void lockdep_copy_map(struct lockdep_map *to,
@@ -258,8 +264,62 @@ struct held_lock {
258264
unsigned int hardirqs_off:1;
259265
unsigned int references:12; /* 32 bits */
260266
unsigned int pin_count;
267+
#ifdef CONFIG_LOCKDEP_CROSSRELEASE
268+
/*
269+
* Generation id.
270+
*
271+
* A value of cross_gen_id will be stored when holding this,
272+
* which is globally increased whenever each crosslock is held.
273+
*/
274+
unsigned int gen_id;
275+
#endif
276+
};
277+
278+
#ifdef CONFIG_LOCKDEP_CROSSRELEASE
279+
#define MAX_XHLOCK_TRACE_ENTRIES 5
280+
281+
/*
282+
* This is for keeping locks waiting for commit so that true dependencies
283+
* can be added at commit step.
284+
*/
285+
struct hist_lock {
286+
/*
287+
* Seperate stack_trace data. This will be used at commit step.
288+
*/
289+
struct stack_trace trace;
290+
unsigned long trace_entries[MAX_XHLOCK_TRACE_ENTRIES];
291+
292+
/*
293+
* Seperate hlock instance. This will be used at commit step.
294+
*
295+
* TODO: Use a smaller data structure containing only necessary
296+
* data. However, we should make lockdep code able to handle the
297+
* smaller one first.
298+
*/
299+
struct held_lock hlock;
300+
};
301+
302+
/*
303+
* To initialize a lock as crosslock, lockdep_init_map_crosslock() should
304+
* be called instead of lockdep_init_map().
305+
*/
306+
struct cross_lock {
307+
/*
308+
* Seperate hlock instance. This will be used at commit step.
309+
*
310+
* TODO: Use a smaller data structure containing only necessary
311+
* data. However, we should make lockdep code able to handle the
312+
* smaller one first.
313+
*/
314+
struct held_lock hlock;
261315
};
262316

317+
struct lockdep_map_cross {
318+
struct lockdep_map map;
319+
struct cross_lock xlock;
320+
};
321+
#endif
322+
263323
/*
264324
* Initialization, self-test and debugging-output methods:
265325
*/
@@ -281,13 +341,6 @@ extern void lockdep_on(void);
281341
extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
282342
struct lock_class_key *key, int subclass);
283343

284-
/*
285-
* To initialize a lockdep_map statically use this macro.
286-
* Note that _name must not be NULL.
287-
*/
288-
#define STATIC_LOCKDEP_MAP_INIT(_name, _key) \
289-
{ .name = (_name), .key = (void *)(_key), }
290-
291344
/*
292345
* Reinitialize a lock key - for cases where there is special locking or
293346
* special initialization of locks so that the validator gets the scope
@@ -460,6 +513,49 @@ struct pin_cookie { };
460513

461514
#endif /* !LOCKDEP */
462515

516+
enum xhlock_context_t {
517+
XHLOCK_HARD,
518+
XHLOCK_SOFT,
519+
XHLOCK_PROC,
520+
XHLOCK_CTX_NR,
521+
};
522+
523+
#ifdef CONFIG_LOCKDEP_CROSSRELEASE
524+
extern void lockdep_init_map_crosslock(struct lockdep_map *lock,
525+
const char *name,
526+
struct lock_class_key *key,
527+
int subclass);
528+
extern void lock_commit_crosslock(struct lockdep_map *lock);
529+
530+
#define STATIC_CROSS_LOCKDEP_MAP_INIT(_name, _key) \
531+
{ .map.name = (_name), .map.key = (void *)(_key), \
532+
.map.cross = 1, }
533+
534+
/*
535+
* To initialize a lockdep_map statically use this macro.
536+
* Note that _name must not be NULL.
537+
*/
538+
#define STATIC_LOCKDEP_MAP_INIT(_name, _key) \
539+
{ .name = (_name), .key = (void *)(_key), .cross = 0, }
540+
541+
extern void crossrelease_hist_start(enum xhlock_context_t c);
542+
extern void crossrelease_hist_end(enum xhlock_context_t c);
543+
extern void lockdep_init_task(struct task_struct *task);
544+
extern void lockdep_free_task(struct task_struct *task);
545+
#else
546+
/*
547+
* To initialize a lockdep_map statically use this macro.
548+
* Note that _name must not be NULL.
549+
*/
550+
#define STATIC_LOCKDEP_MAP_INIT(_name, _key) \
551+
{ .name = (_name), .key = (void *)(_key), }
552+
553+
static inline void crossrelease_hist_start(enum xhlock_context_t c) {}
554+
static inline void crossrelease_hist_end(enum xhlock_context_t c) {}
555+
static inline void lockdep_init_task(struct task_struct *task) {}
556+
static inline void lockdep_free_task(struct task_struct *task) {}
557+
#endif
558+
463559
#ifdef CONFIG_LOCK_STAT
464560

465561
extern void lock_contended(struct lockdep_map *lock, unsigned long ip);

‎include/linux/sched.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,14 @@ struct task_struct {
848848
struct held_lock held_locks[MAX_LOCK_DEPTH];
849849
#endif
850850

851+
#ifdef CONFIG_LOCKDEP_CROSSRELEASE
852+
#define MAX_XHLOCKS_NR 64UL
853+
struct hist_lock *xhlocks; /* Crossrelease history locks */
854+
unsigned int xhlock_idx;
855+
/* For restoring at history boundaries */
856+
unsigned int xhlock_idx_hist[XHLOCK_CTX_NR];
857+
#endif
858+
851859
#ifdef CONFIG_UBSAN
852860
unsigned int in_ubsan;
853861
#endif

‎kernel/exit.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ void __noreturn do_exit(long code)
920920
exit_rcu();
921921
TASKS_RCU(__srcu_read_unlock(&tasks_rcu_exit_srcu, tasks_rcu_i));
922922

923+
lockdep_free_task(tsk);
923924
do_task_dead();
924925
}
925926
EXPORT_SYMBOL_GPL(do_exit);

‎kernel/fork.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ void __init fork_init(void)
484484
cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache",
485485
NULL, free_vm_stack_cache);
486486
#endif
487+
488+
lockdep_init_task(&init_task);
487489
}
488490

489491
int __weak arch_dup_task_struct(struct task_struct *dst,
@@ -1691,6 +1693,7 @@ static __latent_entropy struct task_struct *copy_process(
16911693
p->lockdep_depth = 0; /* no locks held yet */
16921694
p->curr_chain_key = 0;
16931695
p->lockdep_recursion = 0;
1696+
lockdep_init_task(p);
16941697
#endif
16951698

16961699
#ifdef CONFIG_DEBUG_MUTEXES
@@ -1949,6 +1952,7 @@ static __latent_entropy struct task_struct *copy_process(
19491952
bad_fork_cleanup_perf:
19501953
perf_event_free_task(p);
19511954
bad_fork_cleanup_policy:
1955+
lockdep_free_task(p);
19521956
#ifdef CONFIG_NUMA
19531957
mpol_put(p->mempolicy);
19541958
bad_fork_cleanup_threadgroup_lock:

0 commit comments

Comments
 (0)