Use get_online_cpus to avoid races involving CPU hotplug
Subsystems (2)
PATCH v2 1/4 Use get_online_cpus to avoid races involving CPU hotplug
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.
--- arch/x86/kernel/msr.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kernel/msr.c +5 -0
@@ -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);
Signature · 1 line
1.7.10.4
Quoted 20 lines · Silas Boyd-Wickizer
If
arch/x86/kernel/msr.cis a module, a CPU might offline or online between thefor_each_online_cpu(i)loop and the call toregister_hotcpu_notifierinmsr_initor the call tounregister_hotcpu_notifierinmsr_exit. The potential races can lead to leaks/duplicates, attempts to destroy non-existant devices, or random pointer dereferences.For example, in
msr_initif: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 callmsr_device_destroyfor the associated CPU.
Quoted 6 lines
This fix surrounds
for_each_online_cpuandregister_hotcpu_notifierorunregister_hotcpu_notifierwith get_online_cpus+put_online_cpus.Tested on a VM.
Signed-off-by Silas Boyd-Wickizer <[email protected]>
Quoted 48 lines
--- arch/x86/kernel/msr.c | 5 +++++ 1 file changed, 5 insertions(+)arch/x86/kernel/msr.c +5
-0@@ -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);Signature · 1 line
1.7.10.4