linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Zhang Xingcai <zhangxingcai@huawei.com>,
	Giuseppe Cantavenera <giuseppe.cantavenera.ext@nokia.com>,
	Alexander Sverdlin <alexander.sverdlin@nokia.com>,
	Brian Norris <computersforpeace@gmail.com>
Subject: [PATCH 4.0 52/58] mtd: fix: avoid race condition when accessing mtd->usecount
Date: Sun, 19 Jul 2015 12:11:30 -0700	[thread overview]
Message-ID: <20150719190812.963815982@linuxfoundation.org> (raw)
In-Reply-To: <20150719190811.308546345@linuxfoundation.org>

4.0-stable review patch.  If anyone has any objections, please let me know.

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

From: Brian Norris <computersforpeace@gmail.com>

commit 073db4a51ee43ccb827f54a4261c0583b028d5ab upstream.

On A MIPS 32-cores machine a BUG_ON was triggered because some acesses to
mtd->usecount were done without taking mtd_table_mutex.
kernel: Call Trace:
kernel: [<ffffffff80401818>] __put_mtd_device+0x20/0x50
kernel: [<ffffffff804086f4>] blktrans_release+0x8c/0xd8
kernel: [<ffffffff802577e0>] __blkdev_put+0x1a8/0x200
kernel: [<ffffffff802579a4>] blkdev_close+0x1c/0x30
kernel: [<ffffffff8022006c>] __fput+0xac/0x250
kernel: [<ffffffff80171208>] task_work_run+0xd8/0x120
kernel: [<ffffffff8012c23c>] work_notifysig+0x10/0x18
kernel:
kernel:
        Code: 2442ffff  ac8202d8  000217fe <00020336> dc820128  10400003
               00000000  0040f809  00000000
kernel: ---[ end trace 080fbb4579b47a73 ]---

Fixed by taking the mutex in blktrans_open and blktrans_release.

Note that this locking is already suggested in
include/linux/mtd/blktrans.h:

struct mtd_blktrans_ops {
...
	/* Called with mtd_table_mutex held; no race with add/remove */
	int (*open)(struct mtd_blktrans_dev *dev);
	void (*release)(struct mtd_blktrans_dev *dev);
...
};

But we weren't following it.

Originally reported by (and patched by) Zhang and Giuseppe,
independently. Improved and rewritten.

Reported-by: Zhang Xingcai <zhangxingcai@huawei.com>
Reported-by: Giuseppe Cantavenera <giuseppe.cantavenera.ext@nokia.com>
Tested-by: Giuseppe Cantavenera <giuseppe.cantavenera.ext@nokia.com>
Acked-by: Alexander Sverdlin <alexander.sverdlin@nokia.com>
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 drivers/mtd/mtd_blkdevs.c |    5 +++++
 1 file changed, 5 insertions(+)

--- a/drivers/mtd/mtd_blkdevs.c
+++ b/drivers/mtd/mtd_blkdevs.c
@@ -200,6 +200,7 @@ static int blktrans_open(struct block_de
 		return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/
 
 	mutex_lock(&dev->lock);
+	mutex_lock(&mtd_table_mutex);
 
 	if (dev->open)
 		goto unlock;
@@ -223,6 +224,7 @@ static int blktrans_open(struct block_de
 
 unlock:
 	dev->open++;
+	mutex_unlock(&mtd_table_mutex);
 	mutex_unlock(&dev->lock);
 	blktrans_dev_put(dev);
 	return ret;
@@ -233,6 +235,7 @@ error_release:
 error_put:
 	module_put(dev->tr->owner);
 	kref_put(&dev->ref, blktrans_dev_release);
+	mutex_unlock(&mtd_table_mutex);
 	mutex_unlock(&dev->lock);
 	blktrans_dev_put(dev);
 	return ret;
@@ -246,6 +249,7 @@ static void blktrans_release(struct gend
 		return;
 
 	mutex_lock(&dev->lock);
+	mutex_lock(&mtd_table_mutex);
 
 	if (--dev->open)
 		goto unlock;
@@ -259,6 +263,7 @@ static void blktrans_release(struct gend
 		__put_mtd_device(dev->mtd);
 	}
 unlock:
+	mutex_unlock(&mtd_table_mutex);
 	mutex_unlock(&dev->lock);
 	blktrans_dev_put(dev);
 }



  parent reply	other threads:[~2015-07-19 19:21 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-19 19:10 [PATCH 4.0 00/58] 4.0.9-stable review Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 01/58] fs: Add helper functions for permanently empty directories Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 02/58] sysctl: Allow creating permanently empty directories that serve as mountpoints Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 03/58] proc: Allow creating permanently empty directories that serve as mount points Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 04/58] kernfs: Add support for always empty directories Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 05/58] sysfs: Add support for permanently empty directories to serve as mount points Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 06/58] mnt: Update fs_fully_visible to test for permanently empty directories Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 07/58] mnt: Refactor the logic for mounting sysfs and proc in a user namespace Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 08/58] mnt: Modify fs_fully_visible to deal with locked ro nodev and atime Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 09/58] sysfs: Create mountpoints with sysfs_create_mount_point Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 10/58] gpio: crystalcove: set IRQCHIP_SKIP_SET_WAKE for the irqchip Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 11/58] rcu: Correctly handle non-empty Tiny RCU callback list with none ready Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 12/58] ipr: Increase default adapter init stage change timeout Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 13/58] Disable write buffering on Toshiba ToPIC95 Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 14/58] ALSA: pcm: Fix pcm_class sysfs output Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 15/58] ALSA: hda - Fix Dock Headphone on Thinkpad X250 seen as a Line Out Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 16/58] ALSA: hda - set proper caps for newer AMD hda audio in KB/KV Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 17/58] ALSA: hda - Add headset support to Acer Aspire V5 Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 18/58] ALSA: hda - Fix the dock headphone output on Fujitsu Lifebook E780 Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 19/58] ACPI / init: Switch over platform to the ACPI mode later Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 20/58] ACPI / PM: Add missing pm_generic_complete() invocation Greg Kroah-Hartman
2015-07-19 19:10 ` [PATCH 4.0 21/58] iio: accel: kxcjk-1013: add the "KXCJ9000" ACPI id Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 22/58] tools selftests: Fix clean target with make 3.81 Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 23/58] ARC: add smp barriers around atomics per Documentation/atomic_ops.txt Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 24/58] ARC: add compiler barrier to LLSC based cmpxchg Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 25/58] mei: me: wait for power gating exit confirmation Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 26/58] mei: txe: reduce suspend/resume time Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 27/58] arm64: Do not attempt to use init_mm in reset_context() Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 28/58] arm64: entry: fix context tracking for el0_sp_pc Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 29/58] arm64: mm: Fix freeing of the wrong memmap entries with !SPARSEMEM_VMEMMAP Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 30/58] arm64: vdso: work-around broken ELF toolchains in Makefile Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 31/58] mm: kmemleak: allow safe memory scanning during kmemleak disabling Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 32/58] mm: kmemleak_alloc_percpu() should follow the gfp from per_alloc() Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 33/58] mm, thp: respect MPOL_PREFERRED policy with non-local node Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 34/58] iser-target: release stale iser connections Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 35/58] regmap: Fix regmap_bulk_read in BE mode Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 36/58] regmap: Fix possible shift overflow in regmap_field_init() Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 37/58] regulator: max77686: fix gpio_enabled shift wrapping bug Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 38/58] regulator: core: fix constraints output buffer Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 39/58] livepatch: add module locking around kallsyms calls Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 41/58] spi: orion: Fix maximum baud rates for Armada 370/XP Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 42/58] spi: pl022: Specify num-cs property as required in devicetree binding Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 43/58] scsi_transport_srp: Introduce srp_wait_for_queuecommand() Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 44/58] scsi_transport_srp: Fix a race condition Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 45/58] IB/srp: Remove an extraneous scsi_host_put() from an error path Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 46/58] IB/srp: Fix a connection setup race Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 47/58] IB/srp: Fix connection state tracking Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 48/58] IB/srp: Fix reconnection failure handling Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 49/58] genirq: devres: Fix testing return value of request_any_context_irq() Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 50/58] video: mxsfb: Make sure axi clock is enabled when accessing registers Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 51/58] leds / PM: fix hibernation on arm when gpio-led used with CPU led trigger Greg Kroah-Hartman
2015-07-19 19:11 ` Greg Kroah-Hartman [this message]
2015-07-19 19:11 ` [PATCH 4.0 54/58] PCI: Propagate the "ignore hotplug" setting to parent Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 55/58] PCI: Add pci_bus_addr_t Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 56/58] PCI: pciehp: Wait for hotplug command completion where necessary Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 57/58] of/pci: Fix pci_address_to_pio() conversion of CPU address to I/O port Greg Kroah-Hartman
2015-07-19 19:11 ` [PATCH 4.0 58/58] Input: pixcir_i2c_ts - fix receive error Greg Kroah-Hartman
2015-07-20  3:17 ` [PATCH 4.0 00/58] 4.0.9-stable review Guenter Roeck
2015-07-20  7:18 ` Sudip Mukherjee
2015-07-20 17:18 ` Shuah Khan
2015-07-20 21:29 ` Rafael J. Wysocki
2015-07-20 22:04   ` Greg Kroah-Hartman
2015-07-20 22:38     ` Rafael J. Wysocki

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=20150719190812.963815982@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=alexander.sverdlin@nokia.com \
    --cc=computersforpeace@gmail.com \
    --cc=giuseppe.cantavenera.ext@nokia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=zhangxingcai@huawei.com \
    /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;
as well as URLs for NNTP newsgroup(s).