Re: [PATCH] timer: implement lockdep deadlock detection

From: Johannes Berg
Date: Tue Jan 27 2009 - 03:46:00 EST


On Tue, 2009-01-27 at 00:07 +0100, Ingo Molnar wrote:
> * Johannes Berg <johannes@xxxxxxxxxxxxxxxx> wrote:
>
> > This modifies the timer code in a way to allow lockdep to detect
> > deadlocks resulting from a lock being taken in the timer function as
> > well as around the del_timer_sync() call.
> >
> > Validated with this module, otherwise bootup was clean.
>
> That's a really neat trick ...

Did it with workqueues earlier :)

> Curious: have you hit such a bug recently that motivated you to implement
> it?

No, I haven't hit a bug, but code inspection made me wonder whether or
not it could trigger. Didn't have a chance to try yet, had no hardware
available for this particular case.

> > +#ifdef CONFIG_LOCKDEP
> > +#define init_timer(timer) \
> > + do { \
> > + static struct lock_class_key __key; \
> > + init_timer_key((timer), #timer, &__key); \
> > + } while (0)
> > +#define init_timer_deferrable(timer) \
> > + do { \
>
> (Style detail: please put a newline after each macro block to make them
> stand apart a bit more.)

Sure.

> > int del_timer_sync(struct timer_list *timer)
> > {
> > +#ifdef CONFIG_LOCKDEP
> > + unsigned long flags;
> > +
> > + local_irq_save(flags);
> > + lock_map_acquire(&timer->lockdep_map);
> > + lock_map_release(&timer->lockdep_map);
> > + local_irq_restore(flags);
> > +#endif
>
> yummie. We have repeat bugs in this area that are rather tricky to find.
> This will trigger them in a debuggable way.
>
> > @@ -861,10 +881,21 @@ static inline void __run_timers(struct t
> >
> > set_running_timer(base, timer);
> > detach_timer(timer, 1);
> > +
> > spin_unlock_irq(&base->lock);
>
> Yes, the newline is needed there :-) [seriously]

Hah. I was editing code there in that area and removed it later again,
not realising I left the newline.

> > int preempt_count = preempt_count();
> > +
> > + /* Couple the lock chain with the lock chain at
> > + * del_timer_sync by acquiring the lock_map around
> > + * the fn() call here and in del_timer_sync.
> > + */
>
> Please use the standard multi-line comment style:
>
> /*
> * Comment .....
> * ...... goes here:
> */

Oops, yes.

johannes

Attachment: signature.asc
Description: This is a digitally signed message part