From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Srivatsa S. Bhat" Subject: Re: [PATCH v2 46/52] xen, balloon: Fix CPU hotplug callback registration Date: Fri, 14 Feb 2014 22:20:02 +0530 Message-ID: <52FE493A.2030206__939.585144124244$1392397040$gmane$org@linux.vnet.ibm.com> References: <20140214074750.22701.47330.stgit@srivatsabhat.in.ibm.com> <20140214075935.22701.71000.stgit@srivatsabhat.in.ibm.com> <52FE490B.8000908@oracle.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WEM3C-0002fe-06 for xen-devel@lists.xenproject.org; Fri, 14 Feb 2014 16:55:42 +0000 Received: from /spool/local by e28smtp02.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 14 Feb 2014 22:25:37 +0530 Received: from d28relay01.in.ibm.com (d28relay01.in.ibm.com [9.184.220.58]) by d28dlp03.in.ibm.com (Postfix) with ESMTP id DCFB21258054 for ; Fri, 14 Feb 2014 22:27:30 +0530 (IST) Received: from d28av05.in.ibm.com (d28av05.in.ibm.com [9.184.220.67]) by d28relay01.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id s1EGtQca63832186 for ; Fri, 14 Feb 2014 22:25:26 +0530 Received: from d28av05.in.ibm.com (localhost [127.0.0.1]) by d28av05.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id s1EGtW48005809 for ; Fri, 14 Feb 2014 22:25:34 +0530 In-Reply-To: <52FE490B.8000908@oracle.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Boris Ostrovsky 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 , tj@kernel.org, xen-devel@lists.xenproject.org, tglx@linutronix.de, paulmck@linux.vnet.ibm.com, mingo@kernel.org List-Id: xen-devel@lists.xenproject.org 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: >> >> 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 balloon code in xen 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 balloon code in xen this way to fix the deadlock with >> callback registration. >> >> Cc: Konrad Rzeszutek Wilk >> Cc: Boris Ostrovsky >> Cc: David Vrabel >> Cc: Ingo Molnar >> Cc: xen-devel@lists.xenproject.org >> Signed-off-by: Srivatsa S. Bhat >> --- >> >> drivers/xen/balloon.c | 35 +++++++++++++++++++++++------------ >> 1 file changed, 23 insertions(+), 12 deletions(-) > > > 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. Thank you! Regards, Srivatsa S. Bhat >> >> diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c >> index 37d06ea..afe1a3f 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,16 @@ 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(); >> return -ENOMEM; >> } >> } >> - register_cpu_notifier(&balloon_cpu_notifier); >> + put_online_cpus(); >> } >> pr_info("Initialising balloon driver\n"); >> >