rdmsr_on_cpu, wrmsr_on_cpu
authorAlexey Dobriyan <adobriyan@openvz.org>
Fri, 16 Feb 2007 09:48:11 +0000 (01:48 -0800)
committerDave Jones <davej@redhat.com>
Tue, 20 Feb 2007 19:23:43 +0000 (14:23 -0500)
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>
arch/i386/lib/Makefile
arch/i386/lib/msr-on-cpu.c [new file with mode: 0644]
arch/x86_64/lib/Makefile
arch/x86_64/lib/msr-on-cpu.c [new file with mode: 0644]
include/asm-i386/msr.h
include/asm-x86_64/msr.h

index d86a548..0d41223 100644 (file)
@@ -7,3 +7,5 @@ lib-y = checksum.o delay.o usercopy.o getuser.o putuser.o memcpy.o strstr.o \
        bitops.o semaphore.o
 
 lib-$(CONFIG_X86_USE_3DNOW) += mmx.o
+
+obj-y = msr-on-cpu.o
diff --git a/arch/i386/lib/msr-on-cpu.c b/arch/i386/lib/msr-on-cpu.c
new file mode 100644 (file)
index 0000000..2092ea1
--- /dev/null
@@ -0,0 +1,70 @@
+#include <linux/module.h>
+#include <linux/preempt.h>
+#include <linux/smp.h>
+#include <asm/msr.h>
+
+#ifdef CONFIG_SMP
+struct msr_info {
+       u32 msr_no;
+       u32 l, h;
+};
+
+static void __rdmsr_on_cpu(void *info)
+{
+       struct msr_info *rv = info;
+
+       rdmsr(rv->msr_no, rv->l, rv->h);
+}
+
+void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
+{
+       preempt_disable();
+       if (smp_processor_id() == cpu)
+               rdmsr(msr_no, *l, *h);
+       else {
+               struct msr_info rv;
+
+               rv.msr_no = msr_no;
+               smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 0, 1);
+               *l = rv.l;
+               *h = rv.h;
+       }
+       preempt_enable();
+}
+
+static void __wrmsr_on_cpu(void *info)
+{
+       struct msr_info *rv = info;
+
+       wrmsr(rv->msr_no, rv->l, rv->h);
+}
+
+void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
+{
+       preempt_disable();
+       if (smp_processor_id() == cpu)
+               wrmsr(msr_no, l, h);
+       else {
+               struct msr_info rv;
+
+               rv.msr_no = msr_no;
+               rv.l = l;
+               rv.h = h;
+               smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 0, 1);
+       }
+       preempt_enable();
+}
+#else
+void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
+{
+       rdmsr(msr_no, *l, *h);
+}
+
+void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
+{
+       wrmsr(msr_no, l, h);
+}
+#endif
+
+EXPORT_SYMBOL(rdmsr_on_cpu);
+EXPORT_SYMBOL(wrmsr_on_cpu);
index b78d417..0a43f07 100644 (file)
@@ -4,7 +4,7 @@
 
 CFLAGS_csum-partial.o := -funroll-loops
 
-obj-y := io.o iomap_copy.o
+obj-y := io.o iomap_copy.o msr-on-cpu.o
 
 lib-y := csum-partial.o csum-copy.o csum-wrappers.o delay.o \
        usercopy.o getuser.o putuser.o  \
diff --git a/arch/x86_64/lib/msr-on-cpu.c b/arch/x86_64/lib/msr-on-cpu.c
new file mode 100644 (file)
index 0000000..47e0ec4
--- /dev/null
@@ -0,0 +1 @@
+#include "../../i386/lib/msr-on-cpu.c"
index 609a389..3516a1f 100644 (file)
@@ -83,6 +83,9 @@ static inline void wrmsrl (unsigned long msr, unsigned long long val)
                          : "c" (counter))
 #endif /* !CONFIG_PARAVIRT */
 
+void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
+void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
+
 /* symbolic names for some interesting MSRs */
 /* Intel defined MSRs. */
 #define MSR_IA32_P5_MC_ADDR            0
index 3227bc9..995a2b5 100644 (file)
@@ -160,6 +160,8 @@ static inline unsigned int cpuid_edx(unsigned int op)
 #define MSR_IA32_UCODE_WRITE           0x79
 #define MSR_IA32_UCODE_REV             0x8b
 
+void rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h);
+void wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h);
 
 #endif