Commit cf40bd16 authored by Nick Piggin's avatar Nick Piggin Committed by Ingo Molnar

lockdep: annotate reclaim context (__GFP_NOFS)

Here is another version, with the incremental patch rolled up, and
added reclaim context annotation to kswapd, and allocation tracing
to slab allocators (which may only ever reach the page allocator
in rare cases, so it is good to put annotations here too).

Haven't tested this version as such, but it should be getting closer
to merge worthy ;)

--
After noticing some code in mm/filemap.c accidentally perform a __GFP_FS
allocation when it should not have been, I thought it might be a good idea to
try to catch this kind of thing with lockdep.

I coded up a little idea that seems to work. Unfortunately the system has to
actually be in __GFP_FS page reclaim, then take the lock, before it will mark
it. But at least that might still be some orders of magnitude more common
(and more debuggable) than an actual deadlock condition, so we have some
improvement I hope (the concept is no less complete than discovery of a lock's
interrupt contexts).

I guess we could even do the same thing with __GFP_IO (normal reclaim), and
even GFP_NOIO locks too... but filesystems will have the most locks and fiddly
code paths, so let's start there and see how it goes.

It *seems* to work. I did a quick test.

=================================
[ INFO: inconsistent lock state ]
2.6.28-rc6-00007-ged313489-dirty #26
---------------------------------
inconsistent {in-reclaim-W} -> {ov-reclaim-W} usage.
modprobe/8526 [HC0[0]:SC0[0]:HE1:SE1] takes:
 (testlock){--..}, at: [<ffffffffa0020055>] brd_init+0x55/0x216 [brd]
{in-reclaim-W} state was registered at:
  [<ffffffff80267bdb>] __lock_acquire+0x75b/0x1a60
  [<ffffffff80268f71>] lock_acquire+0x91/0xc0
  [<ffffffff8070f0e1>] mutex_lock_nested+0xb1/0x310
  [<ffffffffa002002b>] brd_init+0x2b/0x216 [brd]
  [<ffffffff8020903b>] _stext+0x3b/0x170
  [<ffffffff80272ebf>] sys_init_module+0xaf/0x1e0
  [<ffffffff8020c3fb>] system_call_fastpath+0x16/0x1b
  [<ffffffffffffffff>] 0xffffffffffffffff
irq event stamp: 3929
hardirqs last  enabled at (3929): [<ffffffff8070f2b5>] mutex_lock_nested+0x285/0x310
hardirqs last disabled at (3928): [<ffffffff8070f089>] mutex_lock_nested+0x59/0x310
softirqs last  enabled at (3732): [<ffffffff8061f623>] sk_filter+0x83/0xe0
softirqs last disabled at (3730): [<ffffffff8061f5b6>] sk_filter+0x16/0xe0

other info that might help us debug this:
1 lock held by modprobe/8526:
 #0:  (testlock){--..}, at: [<ffffffffa0020055>] brd_init+0x55/0x216 [brd]

stack backtrace:
Pid: 8526, comm: modprobe Not tainted 2.6.28-rc6-00007-ged313489-dirty #26
Call Trace:
 [<ffffffff80265483>] print_usage_bug+0x193/0x1d0
 [<ffffffff80266530>] mark_lock+0xaf0/0xca0
 [<ffffffff80266735>] mark_held_locks+0x55/0xc0
 [<ffffffffa0020000>] ? brd_init+0x0/0x216 [brd]
 [<ffffffff802667ca>] trace_reclaim_fs+0x2a/0x60
 [<ffffffff80285005>] __alloc_pages_internal+0x475/0x580
 [<ffffffff8070f29e>] ? mutex_lock_nested+0x26e/0x310
 [<ffffffffa0020000>] ? brd_init+0x0/0x216 [brd]
 [<ffffffffa002006a>] brd_init+0x6a/0x216 [brd]
 [<ffffffffa0020000>] ? brd_init+0x0/0x216 [brd]
 [<ffffffff8020903b>] _stext+0x3b/0x170
 [<ffffffff8070f8b9>] ? mutex_unlock+0x9/0x10
 [<ffffffff8070f83d>] ? __mutex_unlock_slowpath+0x10d/0x180
 [<ffffffff802669ec>] ? trace_hardirqs_on_caller+0x12c/0x190
 [<ffffffff80272ebf>] sys_init_module+0xaf/0x1e0
 [<ffffffff8020c3fb>] system_call_fastpath+0x16/0x1b
Signed-off-by: default avatarNick Piggin <npiggin@suse.de>
Signed-off-by: default avatarPeter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 6f2b9b9a
...@@ -27,12 +27,16 @@ enum lock_usage_bit ...@@ -27,12 +27,16 @@ enum lock_usage_bit
LOCK_USED = 0, LOCK_USED = 0,
LOCK_USED_IN_HARDIRQ, LOCK_USED_IN_HARDIRQ,
LOCK_USED_IN_SOFTIRQ, LOCK_USED_IN_SOFTIRQ,
LOCK_USED_IN_RECLAIM_FS,
LOCK_ENABLED_SOFTIRQS, LOCK_ENABLED_SOFTIRQS,
LOCK_ENABLED_HARDIRQS, LOCK_ENABLED_HARDIRQS,
LOCK_HELD_OVER_RECLAIM_FS,
LOCK_USED_IN_HARDIRQ_READ, LOCK_USED_IN_HARDIRQ_READ,
LOCK_USED_IN_SOFTIRQ_READ, LOCK_USED_IN_SOFTIRQ_READ,
LOCK_USED_IN_RECLAIM_FS_READ,
LOCK_ENABLED_SOFTIRQS_READ, LOCK_ENABLED_SOFTIRQS_READ,
LOCK_ENABLED_HARDIRQS_READ, LOCK_ENABLED_HARDIRQS_READ,
LOCK_HELD_OVER_RECLAIM_FS_READ,
LOCK_USAGE_STATES LOCK_USAGE_STATES
}; };
...@@ -42,16 +46,20 @@ enum lock_usage_bit ...@@ -42,16 +46,20 @@ enum lock_usage_bit
#define LOCKF_USED (1 << LOCK_USED) #define LOCKF_USED (1 << LOCK_USED)
#define LOCKF_USED_IN_HARDIRQ (1 << LOCK_USED_IN_HARDIRQ) #define LOCKF_USED_IN_HARDIRQ (1 << LOCK_USED_IN_HARDIRQ)
#define LOCKF_USED_IN_SOFTIRQ (1 << LOCK_USED_IN_SOFTIRQ) #define LOCKF_USED_IN_SOFTIRQ (1 << LOCK_USED_IN_SOFTIRQ)
#define LOCKF_USED_IN_RECLAIM_FS (1 << LOCK_USED_IN_RECLAIM_FS)
#define LOCKF_ENABLED_HARDIRQS (1 << LOCK_ENABLED_HARDIRQS) #define LOCKF_ENABLED_HARDIRQS (1 << LOCK_ENABLED_HARDIRQS)
#define LOCKF_ENABLED_SOFTIRQS (1 << LOCK_ENABLED_SOFTIRQS) #define LOCKF_ENABLED_SOFTIRQS (1 << LOCK_ENABLED_SOFTIRQS)
#define LOCKF_HELD_OVER_RECLAIM_FS (1 << LOCK_HELD_OVER_RECLAIM_FS)
#define LOCKF_ENABLED_IRQS (LOCKF_ENABLED_HARDIRQS | LOCKF_ENABLED_SOFTIRQS) #define LOCKF_ENABLED_IRQS (LOCKF_ENABLED_HARDIRQS | LOCKF_ENABLED_SOFTIRQS)
#define LOCKF_USED_IN_IRQ (LOCKF_USED_IN_HARDIRQ | LOCKF_USED_IN_SOFTIRQ) #define LOCKF_USED_IN_IRQ (LOCKF_USED_IN_HARDIRQ | LOCKF_USED_IN_SOFTIRQ)
#define LOCKF_USED_IN_HARDIRQ_READ (1 << LOCK_USED_IN_HARDIRQ_READ) #define LOCKF_USED_IN_HARDIRQ_READ (1 << LOCK_USED_IN_HARDIRQ_READ)
#define LOCKF_USED_IN_SOFTIRQ_READ (1 << LOCK_USED_IN_SOFTIRQ_READ) #define LOCKF_USED_IN_SOFTIRQ_READ (1 << LOCK_USED_IN_SOFTIRQ_READ)
#define LOCKF_USED_IN_RECLAIM_FS_READ (1 << LOCK_USED_IN_RECLAIM_FS_READ)
#define LOCKF_ENABLED_HARDIRQS_READ (1 << LOCK_ENABLED_HARDIRQS_READ) #define LOCKF_ENABLED_HARDIRQS_READ (1 << LOCK_ENABLED_HARDIRQS_READ)
#define LOCKF_ENABLED_SOFTIRQS_READ (1 << LOCK_ENABLED_SOFTIRQS_READ) #define LOCKF_ENABLED_SOFTIRQS_READ (1 << LOCK_ENABLED_SOFTIRQS_READ)
#define LOCKF_HELD_OVER_RECLAIM_FS_READ (1 << LOCK_HELD_OVER_RECLAIM_FS_READ)
#define LOCKF_ENABLED_IRQS_READ \ #define LOCKF_ENABLED_IRQS_READ \
(LOCKF_ENABLED_HARDIRQS_READ | LOCKF_ENABLED_SOFTIRQS_READ) (LOCKF_ENABLED_HARDIRQS_READ | LOCKF_ENABLED_SOFTIRQS_READ)
...@@ -324,7 +332,11 @@ static inline void lock_set_subclass(struct lockdep_map *lock, ...@@ -324,7 +332,11 @@ static inline void lock_set_subclass(struct lockdep_map *lock,
lock_set_class(lock, lock->name, lock->key, subclass, ip); lock_set_class(lock, lock->name, lock->key, subclass, ip);
} }
# define INIT_LOCKDEP .lockdep_recursion = 0, extern void lockdep_set_current_reclaim_state(gfp_t gfp_mask);
extern void lockdep_clear_current_reclaim_state(void);
extern void lockdep_trace_alloc(gfp_t mask);
# define INIT_LOCKDEP .lockdep_recursion = 0, .lockdep_reclaim_gfp = 0,
#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0) #define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
...@@ -342,6 +354,9 @@ static inline void lockdep_on(void) ...@@ -342,6 +354,9 @@ static inline void lockdep_on(void)
# define lock_release(l, n, i) do { } while (0) # define lock_release(l, n, i) do { } while (0)
# define lock_set_class(l, n, k, s, i) do { } while (0) # define lock_set_class(l, n, k, s, i) do { } while (0)
# define lock_set_subclass(l, s, i) do { } while (0) # define lock_set_subclass(l, s, i) do { } while (0)
# define lockdep_set_current_reclaim_state(g) do { } while (0)
# define lockdep_clear_current_reclaim_state() do { } while (0)
# define lockdep_trace_alloc(g) do { } while (0)
# define lockdep_init() do { } while (0) # define lockdep_init() do { } while (0)
# define lockdep_info() do { } while (0) # define lockdep_info() do { } while (0)
# define lockdep_init_map(lock, name, key, sub) \ # define lockdep_init_map(lock, name, key, sub) \
......
...@@ -1313,6 +1313,7 @@ struct task_struct { ...@@ -1313,6 +1313,7 @@ struct task_struct {
int lockdep_depth; int lockdep_depth;
unsigned int lockdep_recursion; unsigned int lockdep_recursion;
struct held_lock held_locks[MAX_LOCK_DEPTH]; struct held_lock held_locks[MAX_LOCK_DEPTH];
gfp_t lockdep_reclaim_gfp;
#endif #endif
/* journalling filesystem info */ /* journalling filesystem info */
......
This diff is collapsed.
...@@ -32,7 +32,8 @@ extern struct list_head all_lock_classes; ...@@ -32,7 +32,8 @@ extern struct list_head all_lock_classes;
extern struct lock_chain lock_chains[]; extern struct lock_chain lock_chains[];
extern void extern void
get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4); get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3,
char *c4, char *c5, char *c6);
extern const char * __get_key_name(struct lockdep_subclass_key *key, char *str); extern const char * __get_key_name(struct lockdep_subclass_key *key, char *str);
......
...@@ -84,7 +84,7 @@ static int l_show(struct seq_file *m, void *v) ...@@ -84,7 +84,7 @@ static int l_show(struct seq_file *m, void *v)
{ {
struct lock_class *class = v; struct lock_class *class = v;
struct lock_list *entry; struct lock_list *entry;
char c1, c2, c3, c4; char c1, c2, c3, c4, c5, c6;
if (v == SEQ_START_TOKEN) { if (v == SEQ_START_TOKEN) {
seq_printf(m, "all lock classes:\n"); seq_printf(m, "all lock classes:\n");
...@@ -100,8 +100,8 @@ static int l_show(struct seq_file *m, void *v) ...@@ -100,8 +100,8 @@ static int l_show(struct seq_file *m, void *v)
seq_printf(m, " BD:%5ld", lockdep_count_backward_deps(class)); seq_printf(m, " BD:%5ld", lockdep_count_backward_deps(class));
#endif #endif
get_usage_chars(class, &c1, &c2, &c3, &c4); get_usage_chars(class, &c1, &c2, &c3, &c4, &c5, &c6);
seq_printf(m, " %c%c%c%c", c1, c2, c3, c4); seq_printf(m, " %c%c%c%c%c%c", c1, c2, c3, c4, c5, c6);
seq_printf(m, ": "); seq_printf(m, ": ");
print_name(m, class); print_name(m, class);
......
...@@ -1479,6 +1479,8 @@ __alloc_pages_internal(gfp_t gfp_mask, unsigned int order, ...@@ -1479,6 +1479,8 @@ __alloc_pages_internal(gfp_t gfp_mask, unsigned int order,
unsigned long did_some_progress; unsigned long did_some_progress;
unsigned long pages_reclaimed = 0; unsigned long pages_reclaimed = 0;
lockdep_trace_alloc(gfp_mask);
might_sleep_if(wait); might_sleep_if(wait);
if (should_fail_alloc_page(gfp_mask, order)) if (should_fail_alloc_page(gfp_mask, order))
...@@ -1578,12 +1580,15 @@ __alloc_pages_internal(gfp_t gfp_mask, unsigned int order, ...@@ -1578,12 +1580,15 @@ __alloc_pages_internal(gfp_t gfp_mask, unsigned int order,
*/ */
cpuset_update_task_memory_state(); cpuset_update_task_memory_state();
p->flags |= PF_MEMALLOC; p->flags |= PF_MEMALLOC;
lockdep_set_current_reclaim_state(gfp_mask);
reclaim_state.reclaimed_slab = 0; reclaim_state.reclaimed_slab = 0;
p->reclaim_state = &reclaim_state; p->reclaim_state = &reclaim_state;
did_some_progress = try_to_free_pages(zonelist, order, gfp_mask); did_some_progress = try_to_free_pages(zonelist, order, gfp_mask);
p->reclaim_state = NULL; p->reclaim_state = NULL;
lockdep_clear_current_reclaim_state();
p->flags &= ~PF_MEMALLOC; p->flags &= ~PF_MEMALLOC;
cond_resched(); cond_resched();
......
...@@ -3318,6 +3318,8 @@ __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid, ...@@ -3318,6 +3318,8 @@ __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
unsigned long save_flags; unsigned long save_flags;
void *ptr; void *ptr;
lockdep_trace_alloc(flags);
if (slab_should_failslab(cachep, flags)) if (slab_should_failslab(cachep, flags))
return NULL; return NULL;
...@@ -3394,6 +3396,8 @@ __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller) ...@@ -3394,6 +3396,8 @@ __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
unsigned long save_flags; unsigned long save_flags;
void *objp; void *objp;
lockdep_trace_alloc(flags);
if (slab_should_failslab(cachep, flags)) if (slab_should_failslab(cachep, flags))
return NULL; return NULL;
......
...@@ -464,6 +464,8 @@ void *__kmalloc_node(size_t size, gfp_t gfp, int node) ...@@ -464,6 +464,8 @@ void *__kmalloc_node(size_t size, gfp_t gfp, int node)
unsigned int *m; unsigned int *m;
int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
lockdep_trace_alloc(flags);
if (size < PAGE_SIZE - align) { if (size < PAGE_SIZE - align) {
if (!size) if (!size)
return ZERO_SIZE_PTR; return ZERO_SIZE_PTR;
......
...@@ -1596,6 +1596,7 @@ static __always_inline void *slab_alloc(struct kmem_cache *s, ...@@ -1596,6 +1596,7 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,
unsigned long flags; unsigned long flags;
unsigned int objsize; unsigned int objsize;
lockdep_trace_alloc(gfpflags);
might_sleep_if(gfpflags & __GFP_WAIT); might_sleep_if(gfpflags & __GFP_WAIT);
if (should_failslab(s->objsize, gfpflags)) if (should_failslab(s->objsize, gfpflags))
......
...@@ -1963,6 +1963,9 @@ static int kswapd(void *p) ...@@ -1963,6 +1963,9 @@ static int kswapd(void *p)
struct reclaim_state reclaim_state = { struct reclaim_state reclaim_state = {
.reclaimed_slab = 0, .reclaimed_slab = 0,
}; };
lockdep_set_current_reclaim_state(GFP_KERNEL);
node_to_cpumask_ptr(cpumask, pgdat->node_id); node_to_cpumask_ptr(cpumask, pgdat->node_id);
if (!cpumask_empty(cpumask)) if (!cpumask_empty(cpumask))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment