Re: [PATCH] writeback: fix dereferencing NULL bdi->dev ontrace_writeback_queue

From: Wu Fengguang
Date: Sun Feb 05 2012 - 22:29:36 EST


> >> However, I've found one more race condition leading to a crash when
> >> tracing is enabled, this time from the writeback:queue trace point from
> >> bdi_queue_work(). ÂThe cause is the same, i.e. Âbdi->dev is NULL. ÂThis
> >> was produced with the help of the following delay patch. Âtrace+log is
> >> attached.
> >
> > Rabin, this should fix the bug. Note that I take no efforts to remove
> > the to-be-queued and already-queued works. I'm also a bit afraid if
> > the traces in the balance_dirty_pages() path (trace_balance_dirty_pages,
> > trace_bdi_dirty_ratelimit and writeback_wake_background) will have
> > similar NULL dereference bug. Do you test it by physically hot
> > removing a SD card, or with some detach command or emulation?
> >
> > Thanks,
> > Fengguang
>
> Hi. Wu.
> I can reproduce this problem too. And I know this problem is fixed
> with your patch.
> Thanks.
> Tested-by: Namjae Jeon <linkinjeon@xxxxxxxxx>

Namjae, thank you for the testing! FYI I've pushed it to linux-next.

Thanks,
Fengguang


> >
> > ---
> > Subject: writeback: fix dereferencing NULL bdi->dev on trace_writeback_queue
> > Date: Sat Feb 04 20:54:03 CST 2012
> >
> > When the SD card is hot removed without umount, del_gendisk() will call
> > bdi_unregister() but not destroy/free it. This leaves the bdi in the
> > bdi->dev = NULL, bdi->wb.task = NULL, bdi->bdi_list removed state.
> >
> > If someone gets the bdi before bdi_unregister() and calls
> > bdi_queue_work() after the unregister, trace_writeback_queue will be
> > dereferencing the NULL bdi->dev. Fix it with a simple test for NULL.
> >
> > LKML-reference: http://lkml.org/lkml/2012/1/18/346
> > Reported-by: Rabin Vincent <rabin@xxxxxx>
> > Signed-off-by: Wu Fengguang <fengguang.wu@xxxxxxxxx>
> > ---
> > Âinclude/trace/events/writeback.h | Â Â5 ++++-
> > Â1 file changed, 4 insertions(+), 1 deletion(-)
> >
> > --- linux-next.orig/include/trace/events/writeback.h  Â2012-02-04 20:51:01.000000000 +0800
> > +++ linux-next/include/trace/events/writeback.h 2012-02-04 20:54:00.000000000 +0800
> > @@ -47,7 +47,10 @@ DECLARE_EVENT_CLASS(writeback_work_class
> > Â Â Â Â Â Â Â Â__field(int, reason)
> > Â Â Â Â),
> > Â Â Â ÂTP_fast_assign(
> > - Â Â Â Â Â Â Â strncpy(__entry->name, dev_name(bdi->dev), 32);
> > + Â Â Â Â Â Â Â struct device *dev = bdi->dev;
> > + Â Â Â Â Â Â Â if (!dev)
> > + Â Â Â Â Â Â Â Â Â Â Â dev = default_backing_dev_info.dev;
> > + Â Â Â Â Â Â Â strncpy(__entry->name, dev_name(dev), 32);
> > Â Â Â Â Â Â Â Â__entry->nr_pages = work->nr_pages;
> > Â Â Â Â Â Â Â Â__entry->sb_dev = work->sb ? work->sb->s_dev : 0;
> > Â Â Â Â Â Â Â Â__entry->sync_mode = work->sync_mode;
> > --
> > To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
> > the body of a message to majordomo@xxxxxxxxxxxxxxx
> > More majordomo info at Âhttp://vger.kernel.org/majordomo-info.html
--
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/