rdmsr_on_cpu, wrmsr_on_cpu

There was OpenVZ specific bug rendering some cpufreq drivers unusable on SMP.
In short, when cpufreq code thinks it confined itself to needed cpu by means
of set_cpus_allowed() to execute rdmsr, some "virtual cpu" feature can migrate
process to anywhere. This triggers bugons and does wrong things in general.

This got fixed by introducing rdmsr_on_cpu and wrmsr_on_cpu executing rdmsr
and wrmsr on given physical cpu by means of smp_call_function_single().

Dave Jones mentioned cpufreq might be not only user of rdmsr_on_cpu() and
wrmsr_on_cpu(), so I'm putting them into arch/{i386,x86_64}/lib/ .

Signed-off-by: Alexey Dobriyan <adobriyan@openvz.org>
Cc: Andi Kleen <ak@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Dave Jones <davej@redhat.com>

authored by

Alexey Dobriyan and committed by
Dave Jones
b077ffb3 22f7bb03

+79 -1
+2
arch/i386/lib/Makefile
··· 7 7 bitops.o semaphore.o 8 8 9 9 lib-$(CONFIG_X86_USE_3DNOW) += mmx.o 10 + 11 + obj-y = msr-on-cpu.o
+70
arch/i386/lib/msr-on-cpu.c
··· 1 + #include <linux/module.h> 2 + #include <linux/preempt.h> 3 + #include <linux/smp.h> 4 + #include <asm/msr.h> 5 + 6 + #ifdef CONFIG_SMP 7 + struct msr_info { 8 + u32 msr_no; 9 + u32 l, h; 10 + }; 11 + 12 + static void __rdmsr_on_cpu(void *info) 13 + { 14 + struct msr_info *rv = info; 15 + 16 + rdmsr(rv->msr_no, rv->l, rv->h); 17 + } 18 + 19 + void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h) 20 + { 21 + preempt_disable(); 22 + if (smp_processor_id() == cpu) 23 + rdmsr(msr_no, *l, *h); 24 + else { 25 + struct msr_info rv; 26 + 27 + rv.msr_no = msr_no; 28 + smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 0, 1); 29 + *l = rv.l; 30 + *h = rv.h; 31 + } 32 + preempt_enable(); 33 + } 34 + 35 + static void __wrmsr_on_cpu(void *info) 36 + { 37 + struct msr_info *rv = info; 38 + 39 + wrmsr(rv->msr_no, rv->l, rv->h); 40 + } 41 + 42 + void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h) 43 + { 44 + preempt_disable(); 45 + if (smp_processor_id() == cpu) 46 + wrmsr(msr_no, l, h); 47 + else { 48 + struct msr_info rv; 49 + 50 + rv.msr_no = msr_no; 51 + rv.l = l; 52 + rv.h = h; 53 + smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 0, 1); 54 + } 55 + preempt_enable(); 56 + } 57 + #else 58 + void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h) 59 + { 60 + rdmsr(msr_no, *l, *h); 61 + } 62 + 63 + void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h) 64 + { 65 + wrmsr(msr_no, l, h); 66 + } 67 + #endif 68 + 69 + EXPORT_SYMBOL(rdmsr_on_cpu); 70 + EXPORT_SYMBOL(wrmsr_on_cpu);
+1 -1
arch/x86_64/lib/Makefile
··· 4 4 5 5 CFLAGS_csum-partial.o := -funroll-loops 6 6 7 - obj-y := io.o iomap_copy.o 7 + obj-y := io.o iomap_copy.o msr-on-cpu.o 8 8 9 9 lib-y := csum-partial.o csum-copy.o csum-wrappers.o delay.o \ 10 10 usercopy.o getuser.o putuser.o \
+1
arch/x86_64/lib/msr-on-cpu.c
··· 1 + #include "../../i386/lib/msr-on-cpu.c"
+3
include/asm-i386/msr.h
··· 83 83 : "c" (counter)) 84 84 #endif /* !CONFIG_PARAVIRT */ 85 85 86 + void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h); 87 + void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h); 88 + 86 89 /* symbolic names for some interesting MSRs */ 87 90 /* Intel defined MSRs. */ 88 91 #define MSR_IA32_P5_MC_ADDR 0
+2
include/asm-x86_64/msr.h
··· 160 160 #define MSR_IA32_UCODE_WRITE 0x79 161 161 #define MSR_IA32_UCODE_REV 0x8b 162 162 163 + void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h); 164 + void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h); 163 165 164 166 #endif 165 167