mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg KH <gregkh@suse.de>
To: linux-kernel@vger.kernel.org, stable@kernel.org
Cc: stable-review@kernel.org, torvalds@linux-foundation.org,
	akpm@linux-foundation.org, alan@lxorguk.ukuu.org.uk,
	Hugh Dickins <hughd@google.com>
Subject: [46/71] tmpfs: fix race between umount and writepage
Date: Thu, 19 May 2011 11:05:14 -0700	[thread overview]
Message-ID: <20110519180558.710554606@clark.kroah.org> (raw)
In-Reply-To: <20110519180626.GA16555@kroah.com>

2.6.38-stable review patch.  If anyone has any objections, please let us know.

------------------

From: Hugh Dickins <hughd@google.com>

commit b1dea800ac39599301d4bb8dcf2b1d29c2558211 upstream.

Konstanin Khlebnikov reports that a dangerous race between umount and
shmem_writepage can be reproduced by this script:

  for i in {1..300} ; do
	mkdir $i
	while true ; do
		mount -t tmpfs none $i
		dd if=/dev/zero of=$i/test bs=1M count=$(($RANDOM % 100))
		umount $i
	done &
  done

on a 6xCPU node with 8Gb RAM: kernel very unstable after this accident. =)

Kernel log:

  VFS: Busy inodes after unmount of tmpfs.
                 Self-destruct in 5 seconds.  Have a nice day...

  WARNING: at lib/list_debug.c:53 __list_del_entry+0x8d/0x98()
  list_del corruption. prev->next should be ffff880222fdaac8, but was (null)
  Pid: 11222, comm: mount.tmpfs Not tainted 2.6.39-rc2+ #4
  Call Trace:
   warn_slowpath_common+0x80/0x98
   warn_slowpath_fmt+0x41/0x43
   __list_del_entry+0x8d/0x98
   evict+0x50/0x113
   iput+0x138/0x141
  ...
  BUG: unable to handle kernel paging request at ffffffffffffffff
  IP: shmem_free_blocks+0x18/0x4c
  Pid: 10422, comm: dd Tainted: G        W   2.6.39-rc2+ #4
  Call Trace:
   shmem_recalc_inode+0x61/0x66
   shmem_writepage+0xba/0x1dc
   pageout+0x13c/0x24c
   shrink_page_list+0x28e/0x4be
   shrink_inactive_list+0x21f/0x382
  ...

shmem_writepage() calls igrab() on the inode for the page which came from
page reclaim, to add it later into shmem_swaplist for swapoff operation.

This igrab() can race with super-block deactivating process:

  shrink_inactive_list()          deactivate_super()
  pageout()                       tmpfs_fs_type->kill_sb()
  shmem_writepage()               kill_litter_super()
                                  generic_shutdown_super()
                                   evict_inodes()
   igrab()
                                    atomic_read(&inode->i_count)
                                     skip-inode
   iput()
                                   if (!list_empty(&sb->s_inodes))
                                          printk("VFS: Busy inodes after...

This igrap-iput pair was added in commit 1b1b32f2c6f6 "tmpfs: fix
shmem_swaplist races" based on incorrect assumptions: igrab() protects the
inode from concurrent eviction by deletion, but it does nothing to protect
it from concurrent unmounting, which goes ahead despite the raised
i_count.

So this use of igrab() was wrong all along, but the race made much worse
in 2.6.37 when commit 63997e98a3be "split invalidate_inodes()" replaced
two attempts at invalidate_inodes() by a single evict_inodes().

Konstantin posted a plausible patch, raising sb->s_active too: I'm unsure
whether it was correct or not; but burnt once by igrab(), I am sure that
we don't want to rely more deeply upon externals here.

Fix it by adding the inode to shmem_swaplist earlier, while the page lock
on page in page cache still secures the inode against eviction, without
artifically raising i_count.  It was originally added later because
shmem_unuse_inode() is liable to remove an inode from the list while it's
unswapped; but we can guard against that by taking spinlock before
dropping mutex.

Reported-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Signed-off-by: Hugh Dickins <hughd@google.com>
Tested-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

---
 mm/shmem.c |   31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1037,6 +1037,7 @@ static int shmem_writepage(struct page *
 	struct address_space *mapping;
 	unsigned long index;
 	struct inode *inode;
+	bool unlock_mutex = false;
 
 	BUG_ON(!PageLocked(page));
 	mapping = page->mapping;
@@ -1062,7 +1063,26 @@ static int shmem_writepage(struct page *
 	else
 		swap.val = 0;
 
+	/*
+	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
+	 * if it's not already there.  Do it now because we cannot take
+	 * mutex while holding spinlock, and must do so before the page
+	 * is moved to swap cache, when its pagelock no longer protects
+	 * the inode from eviction.  But don't unlock the mutex until
+	 * we've taken the spinlock, because shmem_unuse_inode() will
+	 * prune a !swapped inode from the swaplist under both locks.
+	 */
+	if (swap.val && list_empty(&info->swaplist)) {
+		mutex_lock(&shmem_swaplist_mutex);
+		/* move instead of add in case we're racing */
+		list_move_tail(&info->swaplist, &shmem_swaplist);
+		unlock_mutex = true;
+	}
+
 	spin_lock(&info->lock);
+	if (unlock_mutex)
+		mutex_unlock(&shmem_swaplist_mutex);
+
 	if (index >= info->next_index) {
 		BUG_ON(!(info->flags & SHMEM_TRUNCATE));
 		goto unlock;
@@ -1082,22 +1102,11 @@ static int shmem_writepage(struct page *
 		remove_from_page_cache(page);
 		shmem_swp_set(info, entry, swap.val);
 		shmem_swp_unmap(entry);
-		if (list_empty(&info->swaplist))
-			inode = igrab(inode);
-		else
-			inode = NULL;
 		spin_unlock(&info->lock);
 		swap_shmem_alloc(swap);
 		BUG_ON(page_mapped(page));
 		page_cache_release(page);	/* pagecache ref */
 		swap_writepage(page, wbc);
-		if (inode) {
-			mutex_lock(&shmem_swaplist_mutex);
-			/* move instead of add in case we're racing */
-			list_move_tail(&info->swaplist, &shmem_swaplist);
-			mutex_unlock(&shmem_swaplist_mutex);
-			iput(inode);
-		}
 		return 0;
 	}
 



  parent reply	other threads:[~2011-05-19 18:09 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-19 18:06 [00/71] 2.6.38.7-stable review Greg KH
2011-05-19 18:04 ` [01/71] cifs: change bleft in decode_unicode_ssetup back to signed type Greg KH
2011-05-19 18:04 ` [02/71] cifs: check for bytes_remaining going to zero in CIFS_SessSetup Greg KH
2011-05-19 18:04 ` [03/71] cifs: sanitize length checking in coalesce_t2 (try #3) Greg KH
2011-05-19 18:04 ` [04/71] cifs: refactor mid finding loop in cifs_demultiplex_thread Greg KH
2011-05-19 18:04 ` [05/71] cifs: handle errors from coalesce_t2 Greg KH
2011-05-19 18:04 ` [06/71] Validate size of EFI GUID partition entries Greg KH
2011-05-19 18:04 ` [07/71] drm/radeon/kms: add pci id to acer travelmate quirk for 5730 Greg KH
2011-05-19 18:04 ` [08/71] thinkpad-acpi: module autoloading for newer Lenovo ThinkPads Greg KH
2011-05-20  0:58   ` Henrique de Moraes Holschuh
2011-05-19 18:04 ` [09/71] x86, hw_breakpoints: Fix racy access to ptrace breakpoints Greg KH
2011-05-19 18:04 ` [10/71] ptrace: Prepare to fix racy accesses on task breakpoints Greg KH
2011-05-19 18:04 ` [11/71] hw_breakpoints, powerpc: Fix CONFIG_HAVE_HW_BREAKPOINT off-case in ptrace_set_debugreg() Greg KH
2011-05-19 18:04 ` [12/71] iwlwifi: add {ack, plpc}_check module parameters Greg KH
2011-05-19 18:04 ` [13/71] [stable] [PATCH] drm/radeon/kms: fix gart setup on fusion parts (v2) backport Greg KH
2011-05-19 18:04 ` [14/71] vm: fix vm_pgoff wrap in upward expansion Greg KH
2011-05-19 18:04 ` [15/71] Dont lock guardpage if the stack is growing up Greg KH
2011-05-19 18:04 ` [16/71] drm/i915/dp: Be paranoid in case we disable a DP before it is attached Greg KH
2011-05-19 18:04 ` [17/71] drm/i915/lvds: Only act on lid notify when the device is on Greg KH
2011-05-19 18:04 ` [18/71] drm/i915: Release object along create user fb error path Greg KH
2011-05-19 18:04 ` [19/71] dccp: handle invalid feature options length Greg KH
2011-05-19 18:04 ` [20/71] CIFS: Fix memory over bound bug in cifs_parse_mount_options Greg KH
2011-05-19 18:04 ` [21/71] drivers/rtc/rtc-s3c.c: fixup wake support for rtc Greg KH
2011-05-19 18:04 ` [22/71] mm: use alloc_bootmem_node_nopanic() on really needed path Greg KH
2011-05-19 18:04 ` [23/71] tmpfs: fix race between umount and swapoff Greg KH
2011-05-21  4:48   ` Hugh Dickins
2011-05-21 21:43     ` Greg KH
2011-05-19 18:04 ` [24/71] ARM: zImage: make sure the stack is 64-bit aligned Greg KH
2011-05-19 18:04 ` [25/71] PM: Fix warning in pm_restrict_gfp_mask() during SNAPSHOT_S2RAM ioctl Greg KH
2011-05-19 18:04 ` [26/71] PM / Hibernate: Make snapshot_release() restore GFP mask Greg KH
2011-05-19 18:04 ` [27/71] PM / Hibernate: Fix ioctl SNAPSHOT_S2RAM Greg KH
2011-05-19 18:04 ` [28/71] net: ip_expire() must revalidate route Greg KH
2011-05-19 18:04 ` [29/71] can: fix SJA1000 dlc for RTR packets Greg KH
2011-05-19 20:17   ` Kurt Van Dijck
2011-05-19 18:04 ` [30/71] ipheth: Properly distinguish length and alignment in URBs and skbs Greg KH
2011-05-19 18:04 ` [31/71] vmxnet3: Consistently disable irqs when taking adapter->cmd_lock Greg KH
2011-05-19 18:05 ` [32/71] ehea: fix wrongly reported speed and port Greg KH
2011-05-19 18:05 ` [33/71] NET: slip, fix ldisc->open retval Greg KH
2011-05-19 18:05 ` [34/71] PCH_GbE : Fixed the issue of collision detection Greg KH
2011-05-19 18:05 ` [35/71] PCH_GbE : Fixed the issue of checksum judgment Greg KH
2011-05-19 18:05 ` [36/71] pch_gbe: support ML7223 IOH Greg KH
2011-05-19 18:05 ` [37/71] net: dev_close() should check IFF_UP Greg KH
2011-05-19 18:05 ` [38/71] slcan: fix ldisc->open retval Greg KH
2011-05-19 18:05 ` [39/71] ASoC: UDA134x: Remove POWER_OFF_ON_STANDBY define Greg KH
2011-05-19 18:05 ` [40/71] ASoC: SSM2602: Fix Mic Boost2 control Greg KH
2011-05-19 18:05 ` [41/71] ne-h8300: Fix regression caused during net_device_ops conversion Greg KH
2011-05-19 18:05 ` [42/71] hydra: " Greg KH
2011-05-19 18:05 ` [43/71] ehea: Fix memory hotplug oops Greg KH
2011-05-19 18:05 ` [44/71] libertas: fix cmdpendingq locking Greg KH
2011-05-19 18:05 ` [45/71] zorro8390: Fix regression caused during net_device_ops conversion Greg KH
2011-05-19 18:05 ` Greg KH [this message]
2011-05-19 18:05 ` [47/71] tmpfs: fix race between swapoff and writepage Greg KH
2011-05-19 18:05 ` [48/71] tmpfs: fix off-by-one in max_blocks checks Greg KH
2011-05-19 18:05 ` [49/71] tmpfs: fix spurious ENOSPC when racing with unswap Greg KH
2011-05-19 18:05 ` [50/71] libata: fix oops when LPM is used with PMP Greg KH
2011-05-19 18:05 ` [51/71] drm/radeon/kms: fix extended lvds info parsing Greg KH
2011-05-19 18:05 ` [52/71] Revert "mmc: fix a race between card-detect rescan and clock-gate work instances" Greg KH
2011-05-19 18:05 ` [53/71] cifs: add fallback in is_path_accessible for old servers Greg KH
2011-05-19 18:05 ` [54/71] rapidio: fix default routing initialization Greg KH
2011-05-19 18:05 ` [55/71] Revert "x86, AMD: Fix APIC timer erratum 400 affecting K8 Rev.A-E processors" Greg KH
2011-05-19 18:05 ` [56/71] x86, AMD: Fix ARAT feature setting again Greg KH
2011-05-19 18:05 ` [57/71] block: rescan partitions on invalidated devices on -ENOMEDIA too Greg KH
2011-05-19 18:12   ` Tejun Heo
2011-05-19 18:18     ` Greg KH
2011-05-25  9:09       ` Tejun Heo
2011-05-30  0:01         ` [stable] " Greg KH
2011-05-19 18:05 ` [58/71] clocksource: Install completely before selecting Greg KH
2011-05-19 18:05 ` [59/71] tick: Clear broadcast active bit when switching to oneshot Greg KH
2011-05-19 18:05 ` [60/71] x86, apic: Fix spurious error interrupts triggering on all non-boot APs Greg KH
2011-05-19 18:05 ` [61/71] [media] Fix cx88 remote control input Greg KH
2011-05-19 18:05 ` [62/71] [media] v4l: Release module if subdev registration fails Greg KH
2011-05-19 18:05 ` [63/71] x86: Fix UV BAU for non-consecutive nasids Greg KH
2011-05-19 18:05 ` [64/71] x86, mce, AMD: Fix leaving freed data in a list Greg KH
2011-05-19 18:05 ` [65/71] [SCSI] megaraid_sas: Sanity check user supplied length before passing it to dma_alloc_coherent() Greg KH
2011-05-19 18:05 ` [66/71] cdrom: always check_disk_change() on open Greg KH
2011-05-19 18:05 ` [67/71] vmxnet3: Fix inconsistent LRO state after initialization Greg KH
2011-05-19 18:05 ` [68/71] [SCSI] Revert "[SCSI] Retrieve the Caching mode page" Greg KH
2011-05-19 18:05 ` [69/71] cifs: clean up various nits in unicode routines (try #2) Greg KH
2011-05-19 18:05 ` [70/71] cifs: fix cifsConvertToUCS() for the mapchars case Greg KH
2011-05-19 18:05 ` [71/71] iwlegacy: fix IBSS mode crashes Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110519180558.710554606@clark.kroah.org \
    --to=gregkh@suse.de \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable-review@kernel.org \
    --cc=stable@kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®