Re: [PATCH 1/7] lockdep: Print a nice description of an irq locking issue

From: Yong Zhang
Date: Thu Apr 21 2011 - 03:08:48 EST


On Thu, Apr 21, 2011 at 3:02 PM, Yong Zhang <yong.zhang0@xxxxxxxxx> wrote:
> On Thu, Apr 21, 2011 at 9:41 AM, Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:
>> Â---
>>
>> The above is the case when the unsafe lock is taken while holding
>> a lock taken in irq context. But when a lock is taken that also
>> grabs a unsafe lock, the call chain is shown:
>>
>> Â---
>> other info that might help us debug this:
>>
>> Chain exists of:
>> Â&rq->lock --> lockA --> lockC
>>
>> ÂPossible interrupt unsafe locking scenario:
>>
>> Â Â Â CPU0 Â Â Â Â Â Â Â Â Â ÂCPU1
>> Â Â Â ---- Â Â Â Â Â Â Â Â Â Â----
>> Âlock(lockC);
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â local_irq_disable();
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â lock(&rq->lock);
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â lock(lockA);
>> Â<Interrupt>
>> Â Âlock(&rq->lock);
>>
>> Â*** DEADLOCK ***
>
> Or we could show this:
> Chain exists of:
> &rq->lock --> lockA --> lockC
>
> ÂPossible interrupt unsafe locking scenario:
>
> Â Â ÂCPU0 Â Â Â Â Â Â Â Â Â ÂCPU1 Â Â Â Â Â Â Â Â Â Â Â Â Â CPU2
> Â Â Â---- Â Â Â Â Â Â Â Â Â Â---- Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ----
> Âlock(lockC);
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âlocal_irq_disable();

Forget
local_irq_disable(); here :)

> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âlock(&rq->lock); Â Â Â Â Â Âlock(lockA);
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âlock(lockA); Â Â Â Â Â Â Â Â Â lock(lockC);
> Â<Interrupt>
> Â lock(&rq->lock);
>
> Â*** DEADLOCK ***
>
> Thanks,
> Yong
>
>>
>> Acked-by: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
>> Signed-off-by: Steven Rostedt <rostedt@xxxxxxxxxxx>
>> ---
>> Âkernel/lockdep.c | Â 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> Â1 files changed, 70 insertions(+), 0 deletions(-)
>>
>> diff --git a/kernel/lockdep.c b/kernel/lockdep.c
>> index 0d2058d..bb77c030 100644
>> --- a/kernel/lockdep.c
>> +++ b/kernel/lockdep.c
>> @@ -490,6 +490,18 @@ void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
>> Â Â Â Âusage[i] = '\0';
>> Â}
>>
>> +static int __print_lock_name(struct lock_class *class)
>> +{
>> + Â Â Â char str[KSYM_NAME_LEN];
>> + Â Â Â const char *name;
>> +
>> + Â Â Â name = class->name;
>> + Â Â Â if (!name)
>> + Â Â Â Â Â Â Â name = __get_key_name(class->key, str);
>> +
>> + Â Â Â return printk("%s", name);
>> +}
>> +
>> Âstatic void print_lock_name(struct lock_class *class)
>> Â{
>> Â Â Â Âchar str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
>> @@ -1325,6 +1337,62 @@ print_shortest_lock_dependencies(struct lock_list *leaf,
>> Â Â Â Âreturn;
>> Â}
>>
>> +static void
>> +print_irq_lock_scenario(struct lock_list *safe_entry,
>> + Â Â Â Â Â Â Â Â Â Â Â struct lock_list *unsafe_entry,
>> + Â Â Â Â Â Â Â Â Â Â Â struct held_lock *prev,
>> + Â Â Â Â Â Â Â Â Â Â Â struct held_lock *next)
>> +{
>> + Â Â Â struct lock_class *safe_class = safe_entry->class;
>> + Â Â Â struct lock_class *unsafe_class = unsafe_entry->class;
>> + Â Â Â struct lock_class *middle_class = hlock_class(prev);
>> +
>> + Â Â Â if (middle_class == safe_class)
>> + Â Â Â Â Â Â Â middle_class = hlock_class(next);
>> +
>> + Â Â Â /*
>> + Â Â Â Â* A direct locking problem where unsafe_class lock is taken
>> + Â Â Â Â* directly by safe_class lock, then all we need to show
>> + Â Â Â Â* is the deadlock scenario, as it is obvious that the
>> + Â Â Â Â* unsafe lock is taken under the safe lock.
>> + Â Â Â Â*
>> + Â Â Â Â* But if there is a chain instead, where the safe lock takes
>> + Â Â Â Â* an intermediate lock (middle_class) where this lock is
>> + Â Â Â Â* not the same as the safe lock, then the lock chain is
>> + Â Â Â Â* used to describe the problem. Otherwise we would need
>> + Â Â Â Â* to show a different CPU case for each link in the chain
>> + Â Â Â Â* from the safe_class lock to the unsafe_class lock.
>> + Â Â Â Â*/
>> + Â Â Â if (middle_class != unsafe_class) {
>> + Â Â Â Â Â Â Â printk("Chain exists of:\n Â");
>> + Â Â Â Â Â Â Â __print_lock_name(safe_class);
>> + Â Â Â Â Â Â Â printk(" --> ");
>> + Â Â Â Â Â Â Â __print_lock_name(middle_class);
>> + Â Â Â Â Â Â Â printk(" --> ");
>> + Â Â Â Â Â Â Â __print_lock_name(unsafe_class);
>> + Â Â Â Â Â Â Â printk("\n\n");
>> + Â Â Â }
>> +
>> + Â Â Â printk(" Possible interrupt unsafe locking scenario:\n\n");
>> + Â Â Â printk(" Â Â Â CPU0 Â Â Â Â Â Â Â Â Â ÂCPU1\n");
>> + Â Â Â printk(" Â Â Â ---- Â Â Â Â Â Â Â Â Â Â----\n");
>> + Â Â Â printk(" Âlock(");
>> + Â Â Â __print_lock_name(unsafe_class);
>> + Â Â Â printk(");\n");
>> + Â Â Â printk(" Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â local_irq_disable();\n");
>> + Â Â Â printk(" Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â lock(");
>> + Â Â Â __print_lock_name(safe_class);
>> + Â Â Â printk(");\n");
>> + Â Â Â printk(" Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â lock(");
>> + Â Â Â __print_lock_name(middle_class);
>> + Â Â Â printk(");\n");
>> + Â Â Â printk(" Â<Interrupt>\n");
>> + Â Â Â printk(" Â Âlock(");
>> + Â Â Â __print_lock_name(safe_class);
>> + Â Â Â printk(");\n");
>> + Â Â Â printk("\n *** DEADLOCK ***\n\n");
>> +}
>> +
>> Âstatic int
>> Âprint_bad_irq_dependency(struct task_struct *curr,
>> Â Â Â Â Â Â Â Â Â Â Â Â struct lock_list *prev_root,
>> @@ -1376,6 +1444,8 @@ print_bad_irq_dependency(struct task_struct *curr,
>> Â Â Â Âprint_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
>>
>> Â Â Â Âprintk("\nother info that might help us debug this:\n\n");
>> + Â Â Â print_irq_lock_scenario(backwards_entry, forwards_entry, prev, next);
>> +
>> Â Â Â Âlockdep_print_held_locks(curr);
>>
>> Â Â Â Âprintk("\nthe dependencies between %s-irq-safe lock", irqclass);
>> --
>> 1.7.2.3
>>
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>> the body of a message to majordomo@xxxxxxxxxxxxxxx
>> More majordomo info at Âhttp://vger.kernel.org/majordomo-info.html
>> Please read the FAQ at Âhttp://www.tux.org/lkml/
>>
>
>
>
> --
> Only stand for myself
>



--
Only stand for myself
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/