提交 67bbd7a8 编写于 作者: E Eric Dumazet 提交者: Thomas Gleixner

x86/cpuid: Allow cpuid_read() to schedule

High latencies can be observed caused by a daemon periodically reading
CPUID on all cpus. On KASAN enabled kernels ~10ms latencies can be
observed. Even without KASAN, sending an IPI to a CPU, which is in a deep
sleep state or in a long hard IRQ disabled section, waiting for the answer
can consume hundreds of microseconds.

cpuid_read() is invoked in preemptible context, so it can be converted to
sleep instead of busy wait.

Switching to smp_call_function_single_async() and a completion allows to
reschedule and reduces CPU usage and latencies.
Signed-off-by: NEric Dumazet <edumazet@google.com>
Signed-off-by: NThomas Gleixner <tglx@linutronix.de>
Acked-by: NIngo Molnar <mingo@kernel.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Link: https://lkml.kernel.org/r/20180323215818.127774-2-edumazet@google.com
上级 07cde313
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include <linux/notifier.h> #include <linux/notifier.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/gfp.h> #include <linux/gfp.h>
#include <linux/completion.h>
#include <asm/processor.h> #include <asm/processor.h>
#include <asm/msr.h> #include <asm/msr.h>
...@@ -47,19 +48,27 @@ ...@@ -47,19 +48,27 @@
static struct class *cpuid_class; static struct class *cpuid_class;
static enum cpuhp_state cpuhp_cpuid_state; 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) 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, complete(&cmd->done);
&cmd->eax, &cmd->ebx, &cmd->ecx, &cmd->edx);
} }
static ssize_t cpuid_read(struct file *file, char __user *buf, static ssize_t cpuid_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
char __user *tmp = buf; char __user *tmp = buf;
struct cpuid_regs cmd; struct cpuid_regs_done cmd;
int cpu = iminor(file_inode(file)); int cpu = iminor(file_inode(file));
u64 pos = *ppos; u64 pos = *ppos;
ssize_t bytes = 0; ssize_t bytes = 0;
...@@ -68,19 +77,28 @@ static ssize_t cpuid_read(struct file *file, char __user *buf, ...@@ -68,19 +77,28 @@ static ssize_t cpuid_read(struct file *file, char __user *buf,
if (count % 16) if (count % 16)
return -EINVAL; /* Invalid chunk size */ return -EINVAL; /* Invalid chunk size */
init_completion(&cmd.done);
for (; count; count -= 16) { for (; count; count -= 16) {
cmd.eax = pos; call_single_data_t csd = {
cmd.ecx = pos >> 32; .func = cpuid_smp_cpuid,
err = smp_call_function_single(cpu, cpuid_smp_cpuid, &cmd, 1); .info = &cmd,
};
cmd.regs.eax = pos;
cmd.regs.ecx = pos >> 32;
err = smp_call_function_single_async(cpu, &csd);
if (err) if (err)
break; break;
if (copy_to_user(tmp, &cmd, 16)) { wait_for_completion(&cmd.done);
if (copy_to_user(tmp, &cmd.regs, 16)) {
err = -EFAULT; err = -EFAULT;
break; break;
} }
tmp += 16; tmp += 16;
bytes += 16; bytes += 16;
*ppos = ++pos; *ppos = ++pos;
reinit_completion(&cmd.done);
} }
return bytes ? bytes : err; return bytes ? bytes : err;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册