From mboxrd@z Thu Jan 1 00:00:00 1970 From: Wu Fengguang Subject: [PATCH] writeback: fix dereferencing NULL bdi->dev on trace_writeback_queue Date: Mon, 6 Feb 2012 07:31:21 +0800 Message-ID: <20120205233121.GA30168@localhost> References: <20120115152806.GA32106@debian> <20120117033253.GA399@localhost> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org To: Rabin Vincent Return-path: Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org List-Id: linux-fsdevel.vger.kernel.org On Thu, Jan 19, 2012 at 01:39:21AM +0530, Rabin Vincent wrote: > On Tue, Jan 17, 2012 at 09:02, Wu Fengguang = wrote: > > On Sun, Jan 15, 2012 at 08:58:06PM +0530, Rabin Vincent wrote: > >> =C2=A0Unable to handle kernel NULL pointer dereference at virtual = address 0000002c > >> =C2=A0pgd =3D c0004000 > >> =C2=A0[0000002c] *pgd=3D00000000 > >> =C2=A0Internal error: Oops: 17 [#1] PREEMPT SMP > >> =C2=A0PC is at ftrace_raw_event_writeback_single_inode_template+0x= 60/0xe4 > >> =C2=A0LR is at ftrace_raw_event_writeback_single_inode_template+0x= 50/0xe4 > >> > >> The full trace+log is attached. =C2=A0My kernel (current linus) ha= s a delay > >> inserted in __mark_inode_dirty, to easily trigger the condition: > > > > Rabin, thanks for showing the helpful details! It should be fixable= by > > the use of inode_to_bdi(): >=20 > Thanks, this fixes that one. >=20 > However, I've found one more race condition leading to a crash when > tracing is enabled, this time from the writeback:queue trace point fr= om > bdi_queue_work(). The cause is the same, i.e. bdi->dev is NULL. Th= is > was produced with the help of the following delay patch. trace+log i= s > 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, =46engguang --- Subject: writeback: fix dereferencing NULL bdi->dev on trace_writeback_= queue=20 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 =3D NULL, bdi->wb.task =3D 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 Signed-off-by: Wu Fengguang --- 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:0= 1.000000000 +0800 +++ linux-next/include/trace/events/writeback.h 2012-02-04 20:54:00.000= 000000 +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 =3D bdi->dev; + if (!dev) + dev =3D default_backing_dev_info.dev; + strncpy(__entry->name, dev_name(dev), 32); __entry->nr_pages =3D work->nr_pages; __entry->sb_dev =3D work->sb ? work->sb->s_dev : 0; __entry->sync_mode =3D work->sync_mode;