x86, msr: add rdmsr_safe_on_cpu_resched() and use it in msr_read()
Subsystems (2)
PATCH v2 1/2 x86, msr: add rdmsr_safe_on_cpu_resched() and use it in msr_read()
I noticed high latencies caused by a daemon periodically reading various MSR on all cpus. KASAN kernels would see ~10ms latencies simply reading one MSR. Even without KASAN, sending IPI to CPU in deep sleep state or blocking hard IRQ in a a long section, then waiting for the answer can consume hundreds of usec.
This patch adds rdmsr_safe_on_cpu_resched() which does not spin.
I use this function from msr_read() but future patches might convert other callers to use this variant as well.
Overall daemon cpu usage was reduced by 35 %,
and latencies caused by msr_read() disappeared.
---
v2: fixed the missing part for !CONFIG_SMP
arch/x86/include/asm/msr.h | 6 ++++++ arch/x86/kernel/msr.c | 2 +- arch/x86/lib/msr-smp.c | 43 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr.h +6 -0
@@ -321,6 +321,7 @@ int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q);void rdmsr_on_cpus (const struct cpumask *mask,u32 msr_no,struct msr *msrs);void wrmsr_on_cpus (const struct cpumask *mask,u32 msr_no,struct msr *msrs);int rdmsr_safe_on_cpu (unsigned int cpu,u32 msr_no,u32 *l,u32 *h); +int rdmsr_safe_on_cpu_resched (unsigned int cpu,u32 msr_no,u32 *l,u32 *h);int wrmsr_safe_on_cpu (unsigned int cpu,u32 msr_no,u32 l,u32 h);int rdmsrl_safe_on_cpu (unsigned int cpu,u32 msr_no,u64 *q);int wrmsrl_safe_on_cpu (unsigned int cpu,u32 msr_no,u64 q);
@@ -362,6 +363,11 @@ static inline int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, {return rdmsr_safe (msr_no, l, h); } +static inline int rdmsr_safe_on_cpu_resched (unsigned int cpu,u32 msr_no, +u32 *l,u32 *h) +{ +return rdmsr_safe (msr_no, l, h); +}static inline int wrmsr_safe_on_cpu (unsigned int cpu,u32 msr_no,u32 l,u32 h) {return wrmsr_safe (msr_no, l, h);
arch/x86/kernel/msr.c +1 -1
@@ -60,7 +60,7 @@ static ssize_t msr_read(struct file *file, char __user *buf,return -EINVAL ;/* Invalid chunk size */ for (; count; count -=8 ) { - err =rdmsr_safe_on_cpu (cpu, reg, &data[0 ], &data[1 ]); + err =rdmsr_safe_on_cpu_resched (cpu, reg, &data[0 ], &data[1 ]);if (err)break ;if (copy_to_user (tmp, &data,8 )) {
arch/x86/lib/msr-smp.c +43 -0
61 lines not on this page · raw message
Signature · 1 line
2.16.2.804.g6dcf76e118-goog
PATCH v2 2/2 x86, cpuid: allow cpuid_read() to schedule
I noticed high latencies caused by a daemon periodically reading various MSR and cpuid on all cpus. KASAN kernels would see ~10ms latencies simply reading one cpuid. Even without KASAN, sending IPI to CPU in deep sleep state or blocking hard IRQ in a a long section, then waiting for the answer can consume hundreds of usec or more.
Switching to smp_call_function_single_async() and a completion allows to reschedule and not burn cpu cycles.
--- arch/x86/kernel/cpuid.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-)
arch/x86/kernel/cpuid.c +26 -8
@@ -40,6 +40,7 @@#include <linux/notifier.h> #include <linux/uaccess.h> #include <linux/gfp.h> +#include <linux/completion.h> #include <asm/processor.h> #include <asm/msr.h>
@@ -47,19 +48,27 @@static struct class *cpuid_class;static enum cpuhp_state cpuhp_cpuid_state; +struct cpuid_regs_done { +struct cpuid_regs regs; +struct completion done; +}; +static void cpuid_smp_cpuid (void *cmd_block) { -struct cpuid_regs *cmd = (struct cpuid_regs *)cmd_block; +struct cpuid_regs_done *cmd = cmd_block; + +cpuid_count (cmd->regs.eax, cmd->regs.ecx, + &cmd->regs.eax, &cmd->regs.ebx, + &cmd->regs.ecx, &cmd->regs.edx); -cpuid_count (cmd->eax, cmd->ecx, - &cmd->eax, &cmd->ebx, &cmd->ecx, &cmd->edx); +complete (&cmd->done); }static ssize_t cpuid_read (struct file *file,char __user *buf,size_t count,loff_t *ppos) {char __user *tmp = buf; -struct cpuid_regs cmd; +struct cpuid_regs_done cmd;int cpu =iminor (file_inode (file));u64 pos = *ppos;ssize_t bytes =0 ;
@@ -68,19 +77,28 @@ static ssize_t cpuid_read(struct file *file, char __user *buf,if (count %16 )return -EINVAL ;/* Invalid chunk size */ +init_completion (&cmd.done);for (; count; count -=16 ) { - cmd.eax = pos; - cmd.ecx = pos >>32 ; - err =smp_call_function_single (cpu, cpuid_smp_cpuid, &cmd,1 ); +call_single_data_t csd = { + .func = cpuid_smp_cpuid, + .info = &cmd, + }; + + cmd.regs.eax = pos; + cmd.regs.ecx = pos >>32 ; + + err =smp_call_function_single_async (cpu, &csd);if (err)break ; -if (copy_to_user (tmp, &cmd,16 )) { +wait_for_completion (&cmd.done); +if (copy_to_user (tmp, &cmd.regs,16 )) { err = -EFAULT ;break ; } tmp +=16 ; bytes +=16 ; *ppos = ++pos; +reinit_completion (&cmd.done); }return bytes ? bytes : err;
Signature · 1 line
2.16.2.804.g6dcf76e118-goog
Quoted 6 lines · Eric Dumazet
I noticed high latencies caused by a daemon periodically reading various MSR on all cpus. KASAN kernels would see ~10ms latencies simply reading one MSR. Even without KASAN, sending IPI to CPU in deep sleep state or blocking hard IRQ in a a long section, then waiting for the answer can consume hundreds of usec.
Quoted 7 lines
This patch adds
rdmsr_safe_on_cpu_resched()which does not spin.I use this function from
msr_read()but future patches might convert other callers to use this variant as well.Overall daemon cpu usage was reduced by 35 %,
and latencies caused bymsr_read()disappeared.
Looking at all call sites. None of them is performance critical and all of them are in preemptible context.
So we simply can switch the rdmsr_safe_on_cpu() implementation over to wait mode completely.
Quoted 2 lines
+/* Note: This version spins in
smp_call_function_single().
+ * Consider usingrdmsr_safe_on_cpu_resched()variant instead.
Bah. This is not networking code. x86 uses sensible comment style :)
Thanks,
tglx
Quoted 6 lines · Thomas Gleixner
Looking at all call sites. None of them is performance critical and all of them are in preemptible context.
So we simply can switch the
rdmsr_safe_on_cpu()implementation over to wait mode completely.
SGTM, thanks for looking, I will send a v3 then.
Quoted 5 lines
Quoted 3 lines
+/* Note: This version spins in
smp_call_function_single().
+ * Consider usingrdmsr_safe_on_cpu_resched()variant instead.Bah. This is not networking code. x86 uses sensible comment style :)
Right ;)