LKML Archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/4 V2] Use get_online_cpus to avoid races involving CPU hotplug
@ 2012-08-03 19:34 Silas Boyd-Wickizer
  2012-08-13 16:50 ` Paul E. McKenney
  0 siblings, 1 reply; 2+ messages in thread
From: Silas Boyd-Wickizer @ 2012-08-03 19:34 UTC (permalink / raw
  To: linux-kernel; +Cc: Paul E. McKenney, H. Peter Anvin, Thomas Gleixner, x86

If arch/x86/kernel/cpuid.c is a module, a CPU might offline or online
between the for_each_online_cpu() loop and the call to
register_hotcpu_notifier in cpuid_init or the call to
unregister_hotcpu_notifier in cpuid_exit.  The potential races can
lead to leaks/duplicates, attempts to destroy non-existant devices, or
random pointer dereferences.

For example, in cpuid_exit if:

        for_each_online_cpu(cpu)
                cpuid_device_destroy(cpu);
        class_destroy(cpuid_class);
        __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
        <----- CPU onlines
        unregister_hotcpu_notifier(&cpuid_class_cpu_notifier);

the hotcpu notifier will attempt to create a device for the
cpuid_class, which the module already destroyed.

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>
---
 arch/x86/kernel/cpuid.c |    5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/x86/kernel/cpuid.c b/arch/x86/kernel/cpuid.c
index 39472dd..60c7891 100644
--- a/arch/x86/kernel/cpuid.c
+++ b/arch/x86/kernel/cpuid.c
@@ -199,12 +199,14 @@ static int __init cpuid_init(void)
 		goto out_chrdev;
 	}
 	cpuid_class->devnode = cpuid_devnode;
+	get_online_cpus();
 	for_each_online_cpu(i) {
 		err = cpuid_device_create(i);
 		if (err != 0)
 			goto out_class;
 	}
 	register_hotcpu_notifier(&cpuid_class_cpu_notifier);
+	put_online_cpus();
 
 	err = 0;
 	goto out;
@@ -214,6 +216,7 @@ out_class:
 	for_each_online_cpu(i) {
 		cpuid_device_destroy(i);
 	}
+	put_online_cpus();
 	class_destroy(cpuid_class);
 out_chrdev:
 	__unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
@@ -225,11 +228,13 @@ static void __exit cpuid_exit(void)
 {
 	int cpu = 0;
 
+	get_online_cpus();
 	for_each_online_cpu(cpu)
 		cpuid_device_destroy(cpu);
 	class_destroy(cpuid_class);
 	__unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
 	unregister_hotcpu_notifier(&cpuid_class_cpu_notifier);
+	put_online_cpus();
 }
 
 module_init(cpuid_init);
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 2/4 V2] Use get_online_cpus to avoid races involving CPU hotplug
  2012-08-03 19:34 [PATCH 2/4 V2] Use get_online_cpus to avoid races involving CPU hotplug Silas Boyd-Wickizer
@ 2012-08-13 16:50 ` Paul E. McKenney
  0 siblings, 0 replies; 2+ messages in thread
From: Paul E. McKenney @ 2012-08-13 16:50 UTC (permalink / raw
  To: Silas Boyd-Wickizer; +Cc: linux-kernel, H. Peter Anvin, Thomas Gleixner, x86

On Fri, Aug 03, 2012 at 12:34:50PM -0700, Silas Boyd-Wickizer wrote:
> If arch/x86/kernel/cpuid.c is a module, a CPU might offline or online
> between the for_each_online_cpu() loop and the call to
> register_hotcpu_notifier in cpuid_init or the call to
> unregister_hotcpu_notifier in cpuid_exit.  The potential races can
> lead to leaks/duplicates, attempts to destroy non-existant devices, or
> random pointer dereferences.
> 
> For example, in cpuid_exit if:
> 
>         for_each_online_cpu(cpu)
>                 cpuid_device_destroy(cpu);
>         class_destroy(cpuid_class);
>         __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
>         <----- CPU onlines
>         unregister_hotcpu_notifier(&cpuid_class_cpu_notifier);
> 
> the hotcpu notifier will attempt to create a device for the
> cpuid_class, which the module already destroyed.
> 
> 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/cpuid.c |    5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/arch/x86/kernel/cpuid.c b/arch/x86/kernel/cpuid.c
> index 39472dd..60c7891 100644
> --- a/arch/x86/kernel/cpuid.c
> +++ b/arch/x86/kernel/cpuid.c
> @@ -199,12 +199,14 @@ static int __init cpuid_init(void)
>  		goto out_chrdev;
>  	}
>  	cpuid_class->devnode = cpuid_devnode;
> +	get_online_cpus();
>  	for_each_online_cpu(i) {
>  		err = cpuid_device_create(i);
>  		if (err != 0)
>  			goto out_class;
>  	}
>  	register_hotcpu_notifier(&cpuid_class_cpu_notifier);
> +	put_online_cpus();
> 
>  	err = 0;
>  	goto out;
> @@ -214,6 +216,7 @@ out_class:
>  	for_each_online_cpu(i) {
>  		cpuid_device_destroy(i);
>  	}
> +	put_online_cpus();
>  	class_destroy(cpuid_class);
>  out_chrdev:
>  	__unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
> @@ -225,11 +228,13 @@ static void __exit cpuid_exit(void)
>  {
>  	int cpu = 0;
> 
> +	get_online_cpus();
>  	for_each_online_cpu(cpu)
>  		cpuid_device_destroy(cpu);
>  	class_destroy(cpuid_class);
>  	__unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
>  	unregister_hotcpu_notifier(&cpuid_class_cpu_notifier);
> +	put_online_cpus();
>  }
> 
>  module_init(cpuid_init);
> -- 
> 1.7.10.4
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-08-13 16:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-03 19:34 [PATCH 2/4 V2] Use get_online_cpus to avoid races involving CPU hotplug Silas Boyd-Wickizer
2012-08-13 16:50 ` Paul E. McKenney

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 read-only IMAP folder(s) and NNTP newsgroup(s).