All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Srivatsa S. Bhat" <srivatsa.bhat@linux.vnet.ibm.com>
To: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: linux-arch@vger.kernel.org, ego@linux.vnet.ibm.com,
	walken@google.com, linux@arm.linux.org.uk,
	akpm@linux-foundation.org, peterz@infradead.org,
	rusty@rustcorp.com.au, rjw@rjwysocki.net, oleg@redhat.com,
	linux-kernel@vger.kernel.org, paulus@samba.org,
	David Vrabel <david.vrabel@citrix.com>,
	tj@kernel.org, xen-devel@lists.xenproject.org,
	tglx@linutronix.de, paulmck@linux.vnet.ibm.com, mingo@kernel.org
Subject: [UPDATED][PATCH v2 46/52] xen, balloon: Fix CPU hotplug callback registration
Date: Sat, 15 Feb 2014 22:21:32 +0530	[thread overview]
Message-ID: <52FF9B14.8000308__12051.4291259044$1392483562$gmane$org@linux.vnet.ibm.com> (raw)
In-Reply-To: <52FE493A.2030206@linux.vnet.ibm.com>

On 02/14/2014 10:20 PM, Srivatsa S. Bhat wrote:
> On 02/14/2014 10:19 PM, Boris Ostrovsky wrote:
>> On 02/14/2014 02:59 AM, Srivatsa S. Bhat wrote:
>>> 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:
>>>
[...]
>> This looks exactly like the earlier version (i.e the notifier is still
>> kept registered on allocation failure and commit message doesn't exactly
>> reflect the change).
>>
> 
> Sorry, your earlier reply (for some unknown reason) missed the email-threading
> and landed elsewhere in my inbox, and hence unfortunately I forgot to take
> your suggestions into account while sending out the v2.
> 
> I'll send out an updated version of just this patch, as a reply.

Here is the updated patch. Please let me know what you think!

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

From: Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
Subject: [PATCH] xen, balloon: Fix CPU hotplug callback registration

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).

The xen balloon driver doesn't take get/put_online_cpus() around this code,
but that is also buggy, since it can miss CPU hotplug events in between the
initialization and callback registration:

	for_each_online_cpu(cpu)
		init_cpu(cpu);
		   ^
		   |  Race window; Can miss CPU hotplug events here.
		   v
	register_cpu_notifier(&foobar_cpu_notifier);

Interestingly, the balloon code in xen can simply be reorganized as shown
below, to have a race-free method to register hotplug callbacks, without even
taking get/put_online_cpus(). This is because the initialization performed for
already online CPUs is exactly the same as that performed for CPUs that come
online later. Moreover, the code has checks in place to avoid double
initialization.

	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 balloon code in xen this way to fix the issues with CPU
hotplug callback registration.

Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: David Vrabel <david.vrabel@citrix.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: xen-devel@lists.xenproject.org
Signed-off-by: Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
---

 drivers/xen/balloon.c |   36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c
index 37d06ea..dd79549 100644
--- a/drivers/xen/balloon.c
+++ b/drivers/xen/balloon.c
@@ -592,19 +592,29 @@ static void __init balloon_add_region(unsigned long start_pfn,
 	}
 }
 
+static int alloc_balloon_scratch_page(int cpu)
+{
+	if (per_cpu(balloon_scratch_page, cpu) != NULL)
+		return 0;
+
+	per_cpu(balloon_scratch_page, cpu) = alloc_page(GFP_KERNEL);
+	if (per_cpu(balloon_scratch_page, cpu) == NULL) {
+		pr_warn("Failed to allocate balloon_scratch_page for cpu %d\n", cpu);
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
+
 static int balloon_cpu_notify(struct notifier_block *self,
 				    unsigned long action, void *hcpu)
 {
 	int cpu = (long)hcpu;
 	switch (action) {
 	case CPU_UP_PREPARE:
-		if (per_cpu(balloon_scratch_page, cpu) != NULL)
-			break;
-		per_cpu(balloon_scratch_page, cpu) = alloc_page(GFP_KERNEL);
-		if (per_cpu(balloon_scratch_page, cpu) == NULL) {
-			pr_warn("Failed to allocate balloon_scratch_page for cpu %d\n", cpu);
+		if (alloc_balloon_scratch_page(cpu))
 			return NOTIFY_BAD;
-		}
 		break;
 	default:
 		break;
@@ -624,15 +634,17 @@ static int __init balloon_init(void)
 		return -ENODEV;
 
 	if (!xen_feature(XENFEAT_auto_translated_physmap)) {
-		for_each_online_cpu(cpu)
-		{
-			per_cpu(balloon_scratch_page, cpu) = alloc_page(GFP_KERNEL);
-			if (per_cpu(balloon_scratch_page, cpu) == NULL) {
-				pr_warn("Failed to allocate balloon_scratch_page for cpu %d\n", cpu);
+		register_cpu_notifier(&balloon_cpu_notifier);
+
+		get_online_cpus();
+		for_each_online_cpu(cpu) {
+			if (alloc_balloon_scratch_page(cpu)) {
+				put_online_cpus();
+				unregister_cpu_notifier(&balloon_cpu_notifier);
 				return -ENOMEM;
 			}
 		}
-		register_cpu_notifier(&balloon_cpu_notifier);
+		put_online_cpus();
 	}
 
 	pr_info("Initialising balloon driver\n");

  reply	other threads:[~2014-02-15 16:57 UTC|newest]

Thread overview: 202+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-14  7:49 [PATCH v2 00/52] CPU hotplug: Fix issues with callback registration Srivatsa S. Bhat
2014-02-14  7:49 ` [PATCH v2 01/52] CPU hotplug: Add lockdep annotations to get/put_online_cpus() Srivatsa S. Bhat
2014-02-14  7:49   ` Srivatsa S. Bhat
2014-02-14  7:49   ` Srivatsa S. Bhat
2014-02-14  7:49 ` [PATCH v2 02/52] CPU hotplug: Provide lockless versions of callback registration functions Srivatsa S. Bhat
2014-02-14  7:49   ` Srivatsa S. Bhat
2014-02-14  7:49   ` Srivatsa S. Bhat
2014-02-17 13:26   ` Gautham R Shenoy
2014-02-19 21:34   ` Toshi Kani
2014-02-14  7:49 ` [PATCH v2 03/52] Doc/cpu-hotplug: Specify race-free way to register CPU hotplug callbacks Srivatsa S. Bhat
2014-02-14  7:49   ` Srivatsa S. Bhat
2014-02-14  7:49   ` Srivatsa S. Bhat
2014-02-14  7:49 ` [PATCH v2 04/52] CPU hotplug, perf: Fix CPU hotplug callback registration Srivatsa S. Bhat
2014-02-14  7:49   ` Srivatsa S. Bhat
2014-02-14  7:49   ` Srivatsa S. Bhat
2014-02-14  7:50 ` [PATCH v2 05/52] ia64, salinfo: Fix " Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:50   ` Srivatsa S. Bhat
2014-02-14  7:50   ` Srivatsa S. Bhat
2014-02-14  7:50 ` [PATCH v2 06/52] ia64, palinfo: Fix CPU " Srivatsa S. Bhat
2014-02-14  8:02   ` Srivatsa S. Bhat
2014-02-14  7:50   ` Srivatsa S. Bhat
2014-02-14  7:50   ` Srivatsa S. Bhat
2014-02-14  7:50 ` [PATCH v2 07/52] ia64, topology: " Srivatsa S. Bhat
2014-02-14  8:02   ` Srivatsa S. Bhat
2014-02-14  7:50   ` Srivatsa S. Bhat
2014-02-14  7:50   ` Srivatsa S. Bhat
2014-02-14  7:50 ` [PATCH v2 08/52] ia64, err-inject: " Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:50   ` Srivatsa S. Bhat
2014-02-14  7:50   ` Srivatsa S. Bhat
2014-02-14  7:51 ` [PATCH v2 09/52] arm, hw-breakpoint: " Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:51 ` [PATCH v2 10/52] arm, kvm: " Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14 10:29   ` Paolo Bonzini
2014-02-14 10:29     ` Paolo Bonzini
2014-02-14  7:51 ` [PATCH v2 11/52] s390, cacheinfo: " Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:51 ` [PATCH v2 12/52] s390, smp: " Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:51   ` Srivatsa S. Bhat
2014-02-14  7:52 ` [PATCH v2 13/52] sparc, sysfs: " Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14 18:30   ` David Miller
2014-02-14 18:30     ` David Miller
2014-02-14  7:52 ` [PATCH v2 14/52] powerpc, " Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-03-07  2:57   ` Benjamin Herrenschmidt
2014-03-07  2:57     ` Benjamin Herrenschmidt
2014-03-07  2:57     ` Benjamin Herrenschmidt
2014-03-07  6:21     ` Gautham R Shenoy
2014-03-07  6:21       ` Gautham R Shenoy
2014-02-14  7:52 ` [PATCH v2 15/52] x86, msr: " Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14  7:52 ` [PATCH v2 16/52] x86, cpuid: " Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14  7:52 ` [PATCH v2 17/52] x86, vsyscall: " Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14  7:52   ` Srivatsa S. Bhat
2014-02-14  7:53 ` [PATCH v2 18/52] x86, intel, uncore: " Srivatsa S. Bhat
2014-02-14  7:53   ` Srivatsa S. Bhat
2014-02-14  7:53   ` Srivatsa S. Bhat
2014-02-14  7:53 ` [PATCH v2 19/52] x86, mce: " Srivatsa S. Bhat
2014-02-14  7:53   ` Srivatsa S. Bhat
2014-02-14  7:53   ` Srivatsa S. Bhat
2014-02-14  7:53 ` [PATCH v2 20/52] x86, therm_throt.c: " Srivatsa S. Bhat
2014-02-14  7:53   ` Srivatsa S. Bhat
2014-02-14  7:53   ` Srivatsa S. Bhat
2014-02-14  7:53 ` [PATCH v2 21/52] x86, therm_throt.c: Remove unused therm_cpu_lock Srivatsa S. Bhat
2014-02-14  7:53   ` Srivatsa S. Bhat
2014-02-14  7:53   ` Srivatsa S. Bhat
2014-02-14  7:54 ` [PATCH v2 22/52] x86, amd, ibs: Fix CPU hotplug callback registration Srivatsa S. Bhat
2014-02-14  7:54   ` Srivatsa S. Bhat
2014-02-14  7:54   ` Srivatsa S. Bhat
2014-02-14  7:54 ` [PATCH v2 23/52] x86, intel, cacheinfo: " Srivatsa S. Bhat
2014-02-14  7:54   ` Srivatsa S. Bhat
2014-02-14  7:54   ` Srivatsa S. Bhat
2014-02-14  7:54 ` [PATCH v2 24/52] x86, intel, rapl: " Srivatsa S. Bhat
2014-02-14  7:54   ` Srivatsa S. Bhat
2014-02-14  7:54   ` Srivatsa S. Bhat
2014-02-14  7:54 ` [PATCH v2 25/52] x86, amd, uncore: " Srivatsa S. Bhat
2014-02-14  7:54   ` Srivatsa S. Bhat
2014-02-14  7:54   ` Srivatsa S. Bhat
2014-02-14  7:55 ` [PATCH v2 26/52] x86, hpet: " Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14  7:55 ` [PATCH v2 27/52] x86, pci, amd-bus: " Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14 17:35   ` Bjorn Helgaas
2014-02-14 18:03     ` Srivatsa S. Bhat
2014-02-14  7:55 ` [PATCH v2 28/52] x86, oprofile, nmi: " Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14  7:55 ` [PATCH v2 29/52] x86, kvm: " Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14 10:29   ` Paolo Bonzini
2014-02-14  7:55 ` [PATCH v2 30/52] arm64, hw_breakpoint.c: " Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14  7:55   ` Srivatsa S. Bhat
2014-02-14  7:56 ` [PATCH v2 31/52] arm64, debug-monitors: " Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:56 ` [PATCH v2 32/52] powercap, intel-rapl: " Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:56 ` [PATCH v2 33/52] scsi, bnx2i: " Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:56 ` [PATCH v2 34/52] scsi, bnx2fc: " Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:56   ` Srivatsa S. Bhat
2014-02-14  7:57 ` [PATCH v2 35/52] scsi, fcoe: " Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-14  7:57 ` [PATCH v2 36/52] zsmalloc: " Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-14  7:57 ` [PATCH v2 37/52] acpi-cpufreq: " Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-14  7:57 ` [PATCH v2 38/52] drivers/base/topology.c: " Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-14  7:57   ` Srivatsa S. Bhat
2014-02-15 19:38   ` Greg Kroah-Hartman
2014-02-14  7:58 ` [PATCH v2 39/52] clocksource, dummy-timer: " Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:58 ` [PATCH v2 40/52] intel-idle: " Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:58 ` [PATCH v2 41/52] oprofile, nmi-timer: " Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:58 ` [PATCH v2 42/52] octeon, watchdog: " Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:58 ` [PATCH v2 43/52] thermal, x86-pkg-temp: " Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:58   ` Srivatsa S. Bhat
2014-02-14  7:59 ` [PATCH v2 44/52] hwmon, coretemp: " Srivatsa S. Bhat
2014-02-14  8:11   ` [lm-sensors] " Srivatsa S. Bhat
2014-02-14  7:59   ` Srivatsa S. Bhat
2014-02-14  7:59   ` Srivatsa S. Bhat
2014-02-14  7:59 ` [PATCH v2 45/52] hwmon, via-cputemp: " Srivatsa S. Bhat
2014-02-14  8:11   ` [lm-sensors] " Srivatsa S. Bhat
2014-02-14  7:59   ` Srivatsa S. Bhat
2014-02-14  7:59   ` Srivatsa S. Bhat
2014-02-14  7:59 ` [PATCH v2 46/52] xen, balloon: " Srivatsa S. Bhat
2014-02-14  7:59   ` Srivatsa S. Bhat
2014-02-14  7:59   ` Srivatsa S. Bhat
2014-02-14 16:49   ` Boris Ostrovsky
2014-02-14 16:50     ` Srivatsa S. Bhat
2014-02-15 16:51       ` Srivatsa S. Bhat [this message]
2014-02-15 16:51       ` [UPDATED][PATCH " Srivatsa S. Bhat
2014-02-17 14:50         ` Boris Ostrovsky
2014-02-17 14:50         ` Boris Ostrovsky
2014-02-14 16:50     ` [PATCH " Srivatsa S. Bhat
2014-02-14 16:49   ` Boris Ostrovsky
2014-02-14  7:59 ` Srivatsa S. Bhat
2014-02-14  7:59 ` [PATCH v2 47/52] trace, ring-buffer: " Srivatsa S. Bhat
2014-02-14  7:59   ` Srivatsa S. Bhat
2014-02-14  7:59   ` Srivatsa S. Bhat
2014-02-14  8:00 ` [PATCH v2 48/52] profile: " Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14  8:00 ` [PATCH v2 49/52] mm, vmstat: " Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14 14:26   ` Rik van Riel
2014-02-14 14:26     ` Rik van Riel
2014-02-14  8:00 ` [PATCH v2 50/52] mm, zswap: " Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14  8:00 ` [PATCH v2 51/52] net/core/flow.c: " Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14 18:31   ` David Miller
2014-02-14  8:00 ` [PATCH v2 52/52] net/iucv/iucv.c: " Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14  8:00   ` Srivatsa S. Bhat
2014-02-14 18:31   ` David Miller
2014-02-18  8:56 ` [PATCH v2 00/52] CPU hotplug: Fix issues with " Srivatsa S. Bhat

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='52FF9B14.8000308__12051.4291259044$1392483562$gmane$org@linux.vnet.ibm.com' \
    --to=srivatsa.bhat@linux.vnet.ibm.com \
    --cc=akpm@linux-foundation.org \
    --cc=boris.ostrovsky@oracle.com \
    --cc=david.vrabel@citrix.com \
    --cc=ego@linux.vnet.ibm.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@arm.linux.org.uk \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=paulus@samba.org \
    --cc=peterz@infradead.org \
    --cc=rjw@rjwysocki.net \
    --cc=rusty@rustcorp.com.au \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.org \
    --cc=walken@google.com \
    --cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.