linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Silas Boyd-Wickizer <sbw@mit.edu>
Cc: linux-kernel@vger.kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	x86@kernel.org, Ingo Molnar <mingo@elte.hu>
Subject: Re: [PATCH 1/4 V2] Use get_online_cpus to avoid races involving CPU hotplug
Date: Mon, 13 Aug 2012 09:50:00 -0700	[thread overview]
Message-ID: <20120813165000.GG2458@linux.vnet.ibm.com> (raw)
In-Reply-To: <20120803193327.GB4227@mit.edu>

On Fri, Aug 03, 2012 at 12:33:27PM -0700, Silas Boyd-Wickizer wrote:
> If arch/x86/kernel/msr.c is a module, a CPU might offline or online
> between the for_each_online_cpu(i) loop and the call to
> register_hotcpu_notifier in msr_init or the call to
> unregister_hotcpu_notifier in msr_exit. The potential races can lead
> to leaks/duplicates, attempts to destroy non-existant devices, or
> random pointer dereferences.
> 
> For example, in msr_init if:
> 
>         for_each_online_cpu(i) {
>                 err = msr_device_create(i);
>                 if (err != 0)
>                         goto out_class;
>         }
>         <----- CPU offlines
>         register_hotcpu_notifier(&msr_class_cpu_notifier);
> 
> and the CPU never onlines before msr_exit, then the module will never
> call msr_device_destroy for the associated CPU.
> 
> This fix surrounds for_each_online_cpu and register_hotcpu_notifier or
> unregister_hotcpu_notifier with get_online_cpus+put_online_cpus.
> 
> Tested on a VM.
> 
> Signed-off-by: Silas Boyd-Wickizer <sbw@mit.edu>

Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>

> ---
>  arch/x86/kernel/msr.c |    5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c
> index eb11369..a7c5661 100644
> --- a/arch/x86/kernel/msr.c
> +++ b/arch/x86/kernel/msr.c
> @@ -257,12 +257,14 @@ static int __init msr_init(void)
>  		goto out_chrdev;
>  	}
>  	msr_class->devnode = msr_devnode;
> +	get_online_cpus();
>  	for_each_online_cpu(i) {
>  		err = msr_device_create(i);
>  		if (err != 0)
>  			goto out_class;
>  	}
>  	register_hotcpu_notifier(&msr_class_cpu_notifier);
> +	put_online_cpus();
> 
>  	err = 0;
>  	goto out;
> @@ -271,6 +273,7 @@ out_class:
>  	i = 0;
>  	for_each_online_cpu(i)
>  		msr_device_destroy(i);
> +	put_online_cpus();
>  	class_destroy(msr_class);
>  out_chrdev:
>  	__unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
> @@ -281,11 +284,13 @@ out:
>  static void __exit msr_exit(void)
>  {
>  	int cpu = 0;
> +	get_online_cpus();
>  	for_each_online_cpu(cpu)
>  		msr_device_destroy(cpu);
>  	class_destroy(msr_class);
>  	__unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
>  	unregister_hotcpu_notifier(&msr_class_cpu_notifier);
> +	put_online_cpus();
>  }
> 
>  module_init(msr_init);
> -- 
> 1.7.10.4
> 


      reply	other threads:[~2012-08-13 16:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-03 19:33 [PATCH 1/4 V2] Use get_online_cpus to avoid races involving CPU hotplug Silas Boyd-Wickizer
2012-08-13 16:50 ` Paul E. McKenney [this message]

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=20120813165000.GG2458@linux.vnet.ibm.com \
    --to=paulmck@linux.vnet.ibm.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=sbw@mit.edu \
    --cc=tglx@linutronix.de \
    --cc=x86@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).