stable.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, linux-raid@vger.kernel.org,
	Oleg Nesterov <oleg@redhat.com>,
	"Srivatsa S. Bhat" <srivatsa.bhat@linux.vnet.ibm.com>,
	NeilBrown <neilb@suse.de>
Subject: [PATCH 3.13 90/99] md/raid5: Fix CPU hotplug callback registration
Date: Thu, 20 Feb 2014 15:53:25 -0800	[thread overview]
Message-ID: <20140220235120.815456819@linuxfoundation.org> (raw)
In-Reply-To: <20140220235118.191692546@linuxfoundation.org>

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

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

From: Oleg Nesterov <oleg@redhat.com>

commit 789b5e0315284463617e106baad360cb9e8db3ac upstream.

Subsystems that want to register CPU hotplug callbacks, as well as perform
initialization for the CPUs that are already online, often do it as shown
below:

	get_online_cpus();

	for_each_online_cpu(cpu)
		init_cpu(cpu);

	register_cpu_notifier(&foobar_cpu_notifier);

	put_online_cpus();

This is wrong, since it is prone to ABBA deadlocks involving the
cpu_add_remove_lock and the cpu_hotplug.lock (when running concurrently
with CPU hotplug operations).

Interestingly, the raid5 code can actually prevent double initialization and
hence can use the following simplified form of callback registration:

	register_cpu_notifier(&foobar_cpu_notifier);

	get_online_cpus();

	for_each_online_cpu(cpu)
		init_cpu(cpu);

	put_online_cpus();

A hotplug operation that occurs between registering the notifier and calling
get_online_cpus(), won't disrupt anything, because the code takes care to
perform the memory allocations only once.

So reorganize the code in raid5 this way to fix the deadlock with callback
registration.

Cc: linux-raid@vger.kernel.org
Fixes: 36d1c6476be51101778882897b315bd928c8c7b5
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
[Srivatsa: Fixed the unregister_cpu_notifier() deadlock, added the
free_scratch_buffer() helper to condense code further and wrote the changelog.]
Signed-off-by: Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 drivers/md/raid5.c |   90 +++++++++++++++++++++++++----------------------------
 1 file changed, 44 insertions(+), 46 deletions(-)

--- a/drivers/md/raid5.c
+++ b/drivers/md/raid5.c
@@ -5512,23 +5512,43 @@ raid5_size(struct mddev *mddev, sector_t
 	return sectors * (raid_disks - conf->max_degraded);
 }
 
+static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
+{
+	safe_put_page(percpu->spare_page);
+	kfree(percpu->scribble);
+	percpu->spare_page = NULL;
+	percpu->scribble = NULL;
+}
+
+static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
+{
+	if (conf->level == 6 && !percpu->spare_page)
+		percpu->spare_page = alloc_page(GFP_KERNEL);
+	if (!percpu->scribble)
+		percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
+
+	if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
+		free_scratch_buffer(conf, percpu);
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
 static void raid5_free_percpu(struct r5conf *conf)
 {
-	struct raid5_percpu *percpu;
 	unsigned long cpu;
 
 	if (!conf->percpu)
 		return;
 
-	get_online_cpus();
-	for_each_possible_cpu(cpu) {
-		percpu = per_cpu_ptr(conf->percpu, cpu);
-		safe_put_page(percpu->spare_page);
-		kfree(percpu->scribble);
-	}
 #ifdef CONFIG_HOTPLUG_CPU
 	unregister_cpu_notifier(&conf->cpu_notify);
 #endif
+
+	get_online_cpus();
+	for_each_possible_cpu(cpu)
+		free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
 	put_online_cpus();
 
 	free_percpu(conf->percpu);
@@ -5555,15 +5575,7 @@ static int raid456_cpu_notify(struct not
 	switch (action) {
 	case CPU_UP_PREPARE:
 	case CPU_UP_PREPARE_FROZEN:
-		if (conf->level == 6 && !percpu->spare_page)
-			percpu->spare_page = alloc_page(GFP_KERNEL);
-		if (!percpu->scribble)
-			percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
-
-		if (!percpu->scribble ||
-		    (conf->level == 6 && !percpu->spare_page)) {
-			safe_put_page(percpu->spare_page);
-			kfree(percpu->scribble);
+		if (alloc_scratch_buffer(conf, percpu)) {
 			pr_err("%s: failed memory allocation for cpu%ld\n",
 			       __func__, cpu);
 			return notifier_from_errno(-ENOMEM);
@@ -5571,10 +5583,7 @@ static int raid456_cpu_notify(struct not
 		break;
 	case CPU_DEAD:
 	case CPU_DEAD_FROZEN:
-		safe_put_page(percpu->spare_page);
-		kfree(percpu->scribble);
-		percpu->spare_page = NULL;
-		percpu->scribble = NULL;
+		free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
 		break;
 	default:
 		break;
@@ -5586,40 +5595,29 @@ static int raid456_cpu_notify(struct not
 static int raid5_alloc_percpu(struct r5conf *conf)
 {
 	unsigned long cpu;
-	struct page *spare_page;
-	struct raid5_percpu __percpu *allcpus;
-	void *scribble;
-	int err;
+	int err = 0;
 
-	allcpus = alloc_percpu(struct raid5_percpu);
-	if (!allcpus)
+	conf->percpu = alloc_percpu(struct raid5_percpu);
+	if (!conf->percpu)
 		return -ENOMEM;
-	conf->percpu = allcpus;
+
+#ifdef CONFIG_HOTPLUG_CPU
+	conf->cpu_notify.notifier_call = raid456_cpu_notify;
+	conf->cpu_notify.priority = 0;
+	err = register_cpu_notifier(&conf->cpu_notify);
+	if (err)
+		return err;
+#endif
 
 	get_online_cpus();
-	err = 0;
 	for_each_present_cpu(cpu) {
-		if (conf->level == 6) {
-			spare_page = alloc_page(GFP_KERNEL);
-			if (!spare_page) {
-				err = -ENOMEM;
-				break;
-			}
-			per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
-		}
-		scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
-		if (!scribble) {
-			err = -ENOMEM;
+		err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
+		if (err) {
+			pr_err("%s: failed memory allocation for cpu%ld\n",
+			       __func__, cpu);
 			break;
 		}
-		per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
 	}
-#ifdef CONFIG_HOTPLUG_CPU
-	conf->cpu_notify.notifier_call = raid456_cpu_notify;
-	conf->cpu_notify.priority = 0;
-	if (err == 0)
-		err = register_cpu_notifier(&conf->cpu_notify);
-#endif
 	put_online_cpus();
 
 	return err;



  parent reply	other threads:[~2014-02-20 23:53 UTC|newest]

Thread overview: 108+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-20 23:51 [PATCH 3.13 00/99] 3.13.5-stable review Greg Kroah-Hartman
2014-02-20 23:51 ` [PATCH 3.13 01/99] xen: properly account for _PAGE_NUMA during xen pte translations Greg Kroah-Hartman
2014-02-20 23:51 ` [PATCH 3.13 02/99] xen-blkfront: handle backend CLOSED without CLOSING Greg Kroah-Hartman
2014-02-20 23:51 ` [PATCH 3.13 03/99] fs/file.c:fdtable: avoid triggering OOMs from alloc_fdmem Greg Kroah-Hartman
2014-02-20 23:51 ` [PATCH 3.13 04/99] mm: fix page leak at nfs_symlink() Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 05/99] mm/memory-failure.c: move refcount only in !MF_COUNT_INCREASED Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 07/99] CIFS: Fix SMB2 mounts so they dont try to set or get xattrs via cifs Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 08/99] Add protocol specific operation for CIFS xattrs Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 09/99] retrieving CIFS ACLs when mounted with SMB2 fails dropping session Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 10/99] mac80211: move roc cookie assignment earlier Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 11/99] mac80211: release the channel in error path in start_ap Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 12/99] mac80211: Fix IBSS disconnect Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 13/99] mac80211: fix fragmentation code, particularly for encryption Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 14/99] ath9k_htc: make ->sta_rc_update atomic for most calls Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 15/99] ath9k_htc: Do not support PowerSave by default Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 16/99] ath9k: " Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 17/99] ar5523: fix usb id for Gigaset Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 18/99] s390/dump: Fix dump memory detection Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 19/99] s390: fix kernel crash due to linkage stack instructions Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 20/99] nl80211: Reset split_start when netlink skb is exhausted Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 21/99] spi: Fix crash with double message finalisation on error handling Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 22/99] iwlwifi: mvm: dont allow A band if SKU forbids it Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 23/99] iwlwifi: mvm: disable scheduled scan Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 24/99] iwlwifi: mvm: print the version of the firmware when it asserts Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 25/99] iwlwifi: add more 7265 HW IDs Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 26/99] iwlwifi: mvm: BT Coex - disable BT when TXing probe request in scan Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 27/99] powerpc: Fix endian issues in kexec and crash dump code Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 28/99] of: fix PCI bus match for PCIe slots Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 29/99] spi: nuc900: Set SPI_LSB_FIRST for master->mode_bits if hw->pdata->lsb is true Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 30/99] serial: sirf: fix kernel panic caused by unpaired spinlock Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 31/99] raw: test against runtime value of max_raw_minors Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 32/99] hwmon: (ntc_thermistor) Avoid math overflow Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 33/99] lockd: send correct lock when granting a delayed lock Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 34/99] tty: n_gsm: Fix for modems with brk in modem status control Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 35/99] tty: Set correct tty name in active sysfs attribute Greg Kroah-Hartman
2014-02-21 14:48   ` Josh Boyer
2014-02-21 14:52     ` Hannes Reinecke
2014-02-21 14:56       ` Josh Boyer
2014-02-21 16:01         ` Kay Sievers
2014-02-21 22:53           ` Ray Strode
2014-02-22 13:25             ` David Herrmann
2014-02-22 13:40               ` Peter Hurley
2014-02-21 14:56       ` Peter Hurley
2014-02-21 16:01       ` Greg Kroah-Hartman
2014-02-21 20:21     ` Greg Kroah-Hartman
2014-02-21 22:18       ` Ray Strode
2014-02-22 15:16         ` Hannes Reinecke
2014-02-22 14:27           ` David Herrmann
2014-02-23  0:23             ` Ray Strode
     [not found]             ` <CAA_UwzKrqBw3o3z4LGkYfQ3H11ERKQCscdHNSjkVgHs9F117ow@mail.gmail.com>
2014-02-23 15:05               ` David Herrmann
2014-02-24  9:51                 ` Hannes Reinecke
2014-02-23 14:41           ` Ray Strode
2014-02-20 23:52 ` [PATCH 3.13 36/99] n_tty: Fix stale echo output Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 37/99] drm/radeon: fix UVD IRQ support on 7xx Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 41/99] drm/i915: Pair va_copy with va_end in i915_error_vprintf Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 42/99] drm/radeon: consolidate sdma hdp flushing code for CIK Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 43/99] drm/radeon/cik: use POLL_REG_MEM special op for sDMA HDP flush Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 49/99] staging: r8188eu: Fix typo in USB_DEVICE list Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 50/99] staging: comedi: adv_pci1710: fix analog output readback value Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 51/99] staging:iio:impedance:ad5933: correct error check Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 52/99] staging:iio:ad799x fix error_free_irq which was freeing an irq that may not have been requested Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 53/99] staging:iio:ad799x fix typo in ad799x_events[] Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 54/99] iio: max1363: Use devm_regulator_get_optional for optional regulator Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 55/99] iio: adis16400: Set timestamp as the last element in chan_spec Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 56/99] iio: ak8975: Fix calculation formula for convert micro tesla to gauss unit Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 57/99] x86, smap: Dont enable SMAP if CONFIG_X86_SMAP is disabled Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 58/99] x86, smap: smap_violation() is bogus if CONFIG_X86_SMAP is off Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 59/99] ftrace/x86: Use breakpoints for converting function graph caller Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 60/99] ALSA: hda - Add a headset quirk for Dell XPS 13 Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 61/99] ALSA: hda - Fix mic capture on Sony VAIO Pro 11 Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 62/99] mei: clear write cb from waiting list on reset Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 63/99] mei: dont unset read cb ptr " Greg Kroah-Hartman
2014-02-20 23:52 ` [PATCH 3.13 64/99] VME: Correct read/write alignment algorithm Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 65/99] Drivers: hv: vmbus: Specify the target CPU that should receive notification Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 66/99] Drivers: hv: vmbus: Dont timeout during the initial connection with host Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 68/99] USB: ftdi_sio: add Tagsys RFID Reader IDs Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 69/99] usb-storage: add unusual-devs entry for BlackBerry 9000 Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 70/99] usb-storage: restrict bcdDevice range for Super Top in Cypress ATACB Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 71/99] usb-storage: enable multi-LUN scanning when needed Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 72/99] usb: option: blacklist ZTE MF667 net interface Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 75/99] Revert "xhci: Set scatter-gather limit to avoid failed block writes." Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 76/99] Revert "xhci: Avoid infinite loop when sg urb requires too many trbs" Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 77/99] Revert "usb: xhci: Link TRB must not occur within a USB payload burst" Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 78/99] Revert "usbcore: set lpm_capable field for LPM capable root hubs" Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 79/99] Modpost: fixed USB alias generation for ranges including 0x9 and 0xA Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 80/99] block: __elv_next_request() shouldnt call into the elevator if bypassing Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 81/99] block: Fix nr_vecs for inline integrity vectors Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 82/99] block: add cond_resched() to potentially long running ioctl discard loop Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 83/99] ACPI / hotplug / PCI: Relax the checking of _STA return values Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 84/99] compiler/gcc4: Make quirk for asm_volatile_goto() unconditional Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 85/99] misc: mic: fix possible signed underflow (undefined behavior) in userspace API Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 86/99] IB/qib: Add missing serdes init sequence Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 87/99] KVM: return an error code in kvm_vm_ioctl_register_coalesced_mmio() Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 88/99] tick: Clear broadcast pending bit when switching to oneshot Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 89/99] md/raid1: restore ability for check and repair to fix read errors Greg Kroah-Hartman
2014-02-20 23:53 ` Greg Kroah-Hartman [this message]
2014-02-20 23:53 ` [PATCH 3.13 91/99] i2c: mv64xxx: refactor message start to ensure proper initialization Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 92/99] time: Fix overflow when HZ is smaller than 60 Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 93/99] power: max17040: Fix NULL pointer dereference when there is no platform_data Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 94/99] ring-buffer: Fix first commit on sub-buffer having non-zero delta Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 95/99] target: Fix free-after-use regression in PR unregister Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 96/99] genirq: Add missing irq_to_desc export for CONFIG_SPARSE_IRQ=n Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 97/99] drivers/edac/edac_mc_sysfs.c: poll timeout cannot be zero Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 98/99] EDAC: Poll timeout cannot be zero, p2 Greg Kroah-Hartman
2014-02-20 23:53 ` [PATCH 3.13 99/99] EDAC: Correct workqueue setup path Greg Kroah-Hartman
2014-02-21  5:03 ` [PATCH 3.13 00/99] 3.13.5-stable review Guenter Roeck
2014-02-21 23:42 ` Shuah Khan
2014-02-22  1:50 ` Satoru Takeuchi

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=20140220235120.815456819@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=neilb@suse.de \
    --cc=oleg@redhat.com \
    --cc=srivatsa.bhat@linux.vnet.ibm.com \
    --cc=stable@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).