Received: by 10.213.65.68 with SMTP id h4csp736869imn; Fri, 23 Mar 2018 15:00:37 -0700 (PDT) X-Google-Smtp-Source: AG47ELtpsIOtsjelbgG0fmuy6L3p8woytZA9iQw9Fxc3rUlpsEx9Hx2DTvd5vJH9baFzawEOz6YP X-Received: by 10.98.7.68 with SMTP id b65mr25248454pfd.39.1521842437497; Fri, 23 Mar 2018 15:00:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521842437; cv=none; d=google.com; s=arc-20160816; b=R9bU2IJ7BUbNHRA1fti15CU+quSyjfxQVu3jWsf/u1kMTMzA1WLi4+mt1M05yLgxC/ Fz7t1cKbFOfykH0vbR968sNGKN8qyE4eQX5KiKmTJVHidxM8z97j/Atr78ewSia+w5ju z/kMt502jySgsu3i62qjdaezZ2/7o9fw81q6sKW+g7XhApFAHsJ9Af5oYQq1XB90XAEd CgSEbjDNM+hINrp41Nb77BawibFto0nee0wbvRr0tXw4/C5aMna/j5VpJH7RuelRh1td s0i7lUIDyCWn0gJgdc/FS0LVTQDAV1e2MCG0DG2qCzV+zM441BzRjgvtVxLMvcAtufdP 82dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=lxup9BtBT4ovJ85gMksoq7R+B8vu50sASiaQwNrQizU=; b=kZUTwiP6Y6vFn/ifcIa524gh8oUAmtt3EqVzeFYCmE6dTlVJMpEu9jiYsBKFmnLsuM HBaW5umO2U3I2XZvAAmSFp2lV5d0ZDe/z3aycIw+/kMGXI6n46PW8t5UoQppL4quBa+H 2cVIAF/0k+nxvJv3YBFFaFM2Fp/vz5mU/2oUSYnLQKlWCEWSjwfCJEADMOZ/YePLZyju h0CyI0mRkWl/n82P9jC/cjoK1VTqnQs6oY7sVXf0wdjcVgr9kqQtFDEScA6KwZwKnjel QcQ8lwU75EXHxsJIqVlCeDTw2EVzCjdkLDCArnbjbGQAKUff2dasMC6ytgQjPq296iss Im5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WLIRaIF0; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r1-v6si9893438plb.430.2018.03.23.15.00.22; Fri, 23 Mar 2018 15:00:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=WLIRaIF0; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752259AbeCWV7b (ORCPT + 99 others); Fri, 23 Mar 2018 17:59:31 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:46597 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752152AbeCWV71 (ORCPT ); Fri, 23 Mar 2018 17:59:27 -0400 Received: by mail-pf0-f193.google.com with SMTP id z10so5230069pfh.13 for ; Fri, 23 Mar 2018 14:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lxup9BtBT4ovJ85gMksoq7R+B8vu50sASiaQwNrQizU=; b=WLIRaIF0u6y/WhP2VVF4lL7fyNHXct+tAe3tNXioO5K/7Fx5vQikE9MbdfMc1QVxPi UqzC74sUicFGrW2I/HcakXj1Nie+6YOhI4oCNYIVT4BXLbCMMEosD051QJnmSQrp9yeC 9xn+i783UO85Dd5ESm0HuqGj2v5admnYyJ+jy0IU1al4pVvhO1jPvA6NkpAuvLBJp2/Y mkpqZVEixVhD2tF6QZrEBE0P9l9OnNNzZNVvUqnldh4QY4Fr5CkMjI8OLdksLM8gPOm7 1pxO2Kd5+CVxyAGEbba9v4AheFGMqeIZE4cEdI+q4ArXPvtzZSUi81mnVbhWYB/4VWvY JtPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lxup9BtBT4ovJ85gMksoq7R+B8vu50sASiaQwNrQizU=; b=EML4KVY8DQUoe6aJNZgSAm3x+DSW45op5TR3//6jam+HfqwTzuM6nDr8xlcUu7y1BB yYth9ojcm4iNoEj4CYuKfJJcpiRlAyR8corrSWfwLpAbiOG+D+y+DEmkw8X66YIr0UHM dpgyieDLpHVLByBWfZvDiTBm0zv8uLHM3NJz33ytXVGQO6CkgvxOawigGb/N1bch+eXZ 4qQ0tAFthBWZLVPqQ7vQRBhtmEXqUHSUtm0qHISRvZ+10me3TCnPzi1788L9yArfL/mC 2vaIYGkf9h7uGgCZ2IBAkctNceeRqcfCnYAd3sypI3+IiZAk7w11FNhCcZM171Bk95I6 W2Pw== X-Gm-Message-State: AElRT7FqBGbX2VHBsA19liRqsQozQ6n4nav50/zBXRqshT+X/Jd53p9T yFMbx/itxu6bBWTizgb1yzPOGQ== X-Received: by 10.98.106.193 with SMTP id f184mr13729188pfc.201.1521842366389; Fri, 23 Mar 2018 14:59:26 -0700 (PDT) Received: from localhost ([2620:15c:2c4:201:f5a:7eca:440a:3ead]) by smtp.gmail.com with ESMTPSA id g186sm3208965pfb.3.2018.03.23.14.59.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 23 Mar 2018 14:59:25 -0700 (PDT) From: Eric Dumazet To: x86 Cc: lkml , Eric Dumazet , Eric Dumazet , "H. Peter Anvin" , Thomas Gleixner , Borislav Petkov , Ingo Molnar , Hugh Dickins Subject: [PATCH v3 2/2] x86, cpuid: allow cpuid_read() to schedule Date: Fri, 23 Mar 2018 14:58:18 -0700 Message-Id: <20180323215818.127774-2-edumazet@google.com> X-Mailer: git-send-email 2.17.0.rc0.231.g781580f067-goog In-Reply-To: <20180323215818.127774-1-edumazet@google.com> References: <20180323215818.127774-1-edumazet@google.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Eric Dumazet Cc: "H. Peter Anvin" Cc: Thomas Gleixner Cc: Borislav Petkov Cc: Ingo Molnar Cc: Hugh Dickins --- arch/x86/kernel/cpuid.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/cpuid.c b/arch/x86/kernel/cpuid.c index 0931a105ffe16cde4640e759efa600b23a756d84..1d300f96df4b316dbe3392c8221467cfd8593272 100644 --- a/arch/x86/kernel/cpuid.c +++ b/arch/x86/kernel/cpuid.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -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; -- 2.17.0.rc0.231.g781580f067-goog