Re: single-threaded wq lockdep is broken

From: Johannes Berg
Date: Wed May 31 2017 - 04:36:45 EST


Hi,

> > #include <linux/kernel.h>
> > #include <linux/mutex.h>
> > #include <linux/workqueue.h>
> > #include <linux/module.h>
> > #include <linux/delay.h>
> >
> > DEFINE_MUTEX(mtx);
> > static struct workqueue_struct *wq;
> > static struct work_struct w1, w2;
> >
> > static void w1_wk(struct work_struct *w)
> > {
> > ÂÂÂÂÂÂÂÂmutex_lock(&mtx);
> > ÂÂÂÂÂÂÂÂmsleep(100);
> > ÂÂÂÂÂÂÂÂmutex_unlock(&mtx);
> > }
> >
> > static void w2_wk(struct work_struct *w)
> > {
> > }
> >
> > /*
> > Â* if not defined, then lockdep should warn only,
>
> I guess when DEADLOCK not defined, there is no
> work is queued nor executed, therefore, no lock
> dependence is recorded, and there is no warn
> either.
>
> > Â* if defined, the system will really deadlock.
> > Â*/
> >
> > //#define DEADLOCK
> >
> > static int init(void)
> > {
> > ÂÂÂÂÂÂÂÂwq = create_singlethread_workqueue("test");
> > ÂÂÂÂÂÂÂÂif (!wq)
> > ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂreturn -ENOMEM;
> > ÂÂÂÂÂÂÂÂINIT_WORK(&w1, w1_wk);
> > ÂÂÂÂÂÂÂÂINIT_WORK(&w2, w2_wk);
> >
>
> ÂÂÂÂÂÂÂÂ/* add lock dependence, the lockdep should warn */
> ÂÂÂÂÂÂÂÂqueue_work(wq, &w1);
> ÂÂÂÂÂÂÂÂqueue_work(wq, &w2);
> ÂÂÂÂÂÂÂÂflush_work(&w1);
>
> > #ifdef DEADLOCK
> > ÂÂÂÂÂÂÂÂqueue_work(wq, &w1);
> > ÂÂÂÂÂÂÂÂqueue_work(wq, &w2);
> > #endif
> > ÂÂÂÂÂÂÂÂmutex_lock(&mtx);
> > ÂÂÂÂÂÂÂÂflush_work(&w2);
> > ÂÂÂÂÂÂÂÂmutex_unlock(&mtx);
> >
> > #ifndef DEADLOCK
> > ÂÂÂÂÂÂÂÂqueue_work(wq, &w1);
> > ÂÂÂÂÂÂÂÂqueue_work(wq, &w2);
> > #endif

This was "ifndef", so it does in fact run here, just like you
suggested. It doesn't warn though.

I don't think the order of queue/flush would matter, in fact, if you
insert it like you did, with the flush outside the mutex, no issue
exists (until the later flush)

johannes