From 98b830d26095007aeb04041147b93d2b74e0a0c0 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Tue, 28 Aug 2007 20:33:32 +0300 Subject: [PATCH] [MTD] [NAND] nandsim: avoid deadlocking FS Make nandsim use GFP_NOFS when allocating memory, because it might be used by a file-system (e.g. UBIFS2) which means, if we are short of memory, we may deadlock. Indee, UBIFS is holding a lock, writes to the media, reaches this place in NANDsim, kmalloc does not find the requested amount of RAM, calls memory shrinker, which decides to writeback inodes, calls FS, and it deadlocks on the lock which is already being held. Below is the UBIFS backtrace which demonstrates that: [] __mutex_lock_slowpath+0xc8/0x2e6 [] mutex_lock+0x1c/0x1f [] reserve_space+0x3d/0xa9 [ubifs] [] make_one_reservation+0x2b/0x86 [ubifs] [] ubifs_jrn_write_block+0xda/0x12f [ubifs] [] ubifs_writepage+0x11d/0x1ec [ubifs] [] shrink_inactive_list+0x7fa/0x969 [] shrink_zone+0xae/0x10c [] try_to_free_pages+0x159/0x251 [] __alloc_pages+0x125/0x2f0 [] cache_alloc_refill+0x380/0x6ba [] __kmalloc+0x14f/0x157 [] do_state_action+0xab7/0xc74 [nandsim] [] switch_state+0x225/0x402 [nandsim] [] ns_hwcontrol+0x3e2/0x620 [nandsim] [] nand_command+0x2e/0x1a5 [nand] [] nand_write_page+0x4a/0x9a [nand] [] nand_do_write_ops+0x1cf/0x343 [nand] [] nand_write+0x88/0xa6 [nand] [] part_write+0x72/0x8b [mtd] [] ubi_io_write+0x189/0x29c [ubi] [] ubi_eba_write_leb+0xb6/0x699 [ubi] [] ubi_leb_write+0xe4/0xe9 [ubi] [] ubifs_wbuf_write_nolock+0x333/0x4c9 [ubifs] [] write_node+0x74/0x8e [ubifs] [] ubifs_jrn_write_block+0x100/0x12f [ubifs] [] ubifs_writepage+0x11d/0x1ec [ubifs] [] __writepage+0xb/0x26 [] write_cache_pages+0x203/0x2d9 [] generic_writepages+0x23/0x2d [] do_writepages+0x37/0x39 [] __writeback_single_inode+0x96/0x399 [] sync_sb_inodes+0x1a3/0x274 [] writeback_inodes+0xa6/0xd8 [] background_writeout+0x86/0x9e [] pdflush+0xfb/0x1b6 [] kthread+0x37/0x59 [] kernel_thread_helper+0x7/0x14 The deadlock is funny because it starts in pdflush/writeback, and comes back to writeback, then deadlocks. It seems we should look carefully for other places in UBI and MTD and use GFP_NOFS instead of GFP_KERNEL. Caught-by: Adrian Hunter Signed-off-by: Artem Bityutskiy Signed-off-by: David Woodhouse --- drivers/mtd/nand/nandsim.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/nand/nandsim.c b/drivers/mtd/nand/nandsim.c index 205df0f771..a7574807dc 100644 --- a/drivers/mtd/nand/nandsim.c +++ b/drivers/mtd/nand/nandsim.c @@ -1272,7 +1272,13 @@ static int prog_page(struct nandsim *ns, int num) mypage = NS_GET_PAGE(ns); if (mypage->byte == NULL) { NS_DBG("prog_page: allocating page %d\n", ns->regs.row); - mypage->byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL); + /* + * We allocate memory with GFP_NOFS because a flash FS may + * utilize this. If it is holding an FS lock, then gets here, + * then kmalloc runs writeback which goes to the FS again + * and deadlocks. This was seen in practice. + */ + mypage->byte = kmalloc(ns->geom.pgszoob, GFP_NOFS); if (mypage->byte == NULL) { NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row); return -1; -- 2.39.5