Received: by 10.213.65.68 with SMTP id h4csp456533imn; Sat, 17 Mar 2018 10:12:02 -0700 (PDT) X-Google-Smtp-Source: AG47ELuxK97gdpbNzhKsp79NQb6nNcyFpOT4u2Qm9HOHZdFqgKY32GHXdn0ivMDxLk8eKBAwMSkG X-Received: by 2002:a17:902:b683:: with SMTP id c3-v6mr6202726pls.154.1521306722439; Sat, 17 Mar 2018 10:12:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521306722; cv=none; d=google.com; s=arc-20160816; b=W551NLX5Kln5uVz8UeNq5G9jH+ET3eAM+tJQLQl2jbV+91Fn9V2HduJiW6UAnE5pAl l28OY1R93OKeQnrMYu9vfnnEsPe31b3d8Ev49y/XgNJTca/6Nhq80gh2nUXt8Pzz/Wvf /yanRMP7CRye6iV2lz054e8KyPzcYhQ1VMpfGIHQl3zj6BOkoRYPHRJ04oa5QKVH1qcd q+2yqkUvqypnqQd0V1TqOjHMaeWH/fVQjmCvCXqS4RdZjiQ/ZCVb54zlqWsarI+On86a fgVcuSaIh2Ax4x5zi4ShTHjI/WPis9KoO2oThgLxOjn3f9PuGroG+S/ypkK0UN6uOLMH FYdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=LyZ8I6A/Gh+ASQlNH+kyOXF4wKwK378bkV92vtpGt8s=; b=ZGJQoXu7TYj8a2bosKIxm6p5ZuI9oPPkzU2E2Gl80xIKdN9ACBQOIZHmmkPSDdipfU Hv09sWWXS8jrYCOTyBjjzCZ3HciIiMOh9Drc/f5Z1XrsAmaoTgaPGowj6EZuWH2tNcvP gikWBkzA5rQ9oZB1vSKH6kauH56oxn2dtolamRITFKZwRoSmJ8BC0yzktalBi6wVSr++ 4nX1uotEwvD03poKqEnAauSpCr9k+5AVQ7Z25IWFj4rAxN6Nb3LYk77Y+ls4uqjHFEho /YiwFK7VlJJjMQ5uPcsO3fE7Ah7E2gLAozAADY+zk9E6xwuXmjmzjASBot8IIwKJTBRB GqBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=aejWrdxB; 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 91-v6si8374452pla.473.2018.03.17.10.11.33; Sat, 17 Mar 2018 10:12:02 -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=aejWrdxB; 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 S1753192AbeCQRKB (ORCPT + 99 others); Sat, 17 Mar 2018 13:10:01 -0400 Received: from mail-pl0-f67.google.com ([209.85.160.67]:36427 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752709AbeCQRJ7 (ORCPT ); Sat, 17 Mar 2018 13:09:59 -0400 Received: by mail-pl0-f67.google.com with SMTP id 61-v6so7712694plf.3 for ; Sat, 17 Mar 2018 10:09:59 -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; bh=LyZ8I6A/Gh+ASQlNH+kyOXF4wKwK378bkV92vtpGt8s=; b=aejWrdxBWml4XNr5HZUhHi4M+PbufeE1NC+SSBuRRnfpqjZkS9U7Vj/dYTZXUdtFqQ lC0R+WQ82dRl0Rp86fijssPzGoe9wG0XCNnDBi+i0vsnZsDBlFgl9PJcUjJjIbi1BEwi CYbqZrDfcuHxbtXktwEanQwZIODFzKWb21Hu4Q6yKPLHFQdcorqbZxBmaoW4j/e0uGO8 r+S/JaNLzUeoGiSGUxPm6o2C5XMoQu3g5PukQkCzJmCmX93vKZ/Roo+g0RlyAexeiTxP Y5s8WkZ14ylMN+5XpyIDH+QbOx/9bL5pPBUCprwB689nq84RFZOg288V+5Ahf5qO+YDh RrJQ== 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; bh=LyZ8I6A/Gh+ASQlNH+kyOXF4wKwK378bkV92vtpGt8s=; b=aaBetLanQpl3qJiA9IXMsxVo4qC5CemPukVqaqyh0ofdNV+hT6Mu0phz6X+iaJUdL6 z8P/7KzNmn1RT6khdMP6hPu3sFIAwh5trh/bTSUyKTf69eJl0sLmYd+EBJVJnN3oYtNG PIo992mZ6bBZ8S8b6oHSEUFEB8g4o0jQuXkKgnVH4a8lQyeUJ3WQK1OmA2eoE0+ZEqmv SxWva9446R+UIPaOdLpFOc6qMTP3OouNay50nYR/DgxRJYh6JciE+uxHOuqhNXTJsPTc fI56+8Loj3ObgBq/dffm7iOd3yHwzOk/nLUdTI4CvsHmt5aFrogBuLywvpbry55MjL+P Eq4A== X-Gm-Message-State: AElRT7HODmLeG4SBG8QhVbdnCwKiAeJ/kNsjwSZzvx3aKmbi9UM0eGX4 VqRmCfJJPVjwYlW4ViiPG6Uc2Q== X-Received: by 2002:a17:902:684:: with SMTP id 4-v6mr6429166plh.262.1521306598724; Sat, 17 Mar 2018 10:09:58 -0700 (PDT) Received: from localhost ([2620:15c:2c4:201:f5a:7eca:440a:3ead]) by smtp.gmail.com with ESMTPSA id j15sm9067273pfk.44.2018.03.17.10.09.55 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 17 Mar 2018 10:09:56 -0700 (PDT) From: Eric Dumazet To: x86 Cc: lkml , Eric Dumazet , Eric Dumazet , "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , Hugh Dickins Subject: [PATCH 1/2] x86, msr: add rdmsr_safe_on_cpu_resched() and use it in msr_read() Date: Sat, 17 Mar 2018 10:09:52 -0700 Message-Id: <20180317170953.111610-1-edumazet@google.com> X-Mailer: git-send-email 2.16.2.804.g6dcf76e118-goog 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 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. Signed-off-by: Eric Dumazet Cc: "H. Peter Anvin" Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Hugh Dickins --- arch/x86/include/asm/msr.h | 1 + arch/x86/kernel/msr.c | 2 +- arch/x86/lib/msr-smp.c | 43 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/msr.h b/arch/x86/include/asm/msr.h index 30df295f6d94c8ac6d87613acae8a32c50436c6d..117a286660c61cf9c10e68f0b48d27e2de17deab 100644 --- a/arch/x86/include/asm/msr.h +++ b/arch/x86/include/asm/msr.h @@ -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); diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c index ef688804f80d33088fef15448996a97f69e2b193..d464858cdcad59cb08a913388d60f1aee6d2277a 100644 --- a/arch/x86/kernel/msr.c +++ b/arch/x86/kernel/msr.c @@ -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)) { diff --git a/arch/x86/lib/msr-smp.c b/arch/x86/lib/msr-smp.c index 693cce0be82dffb822cecd0c7e38d2821aff896c..80eb10a759fd8356519c05db5c311285027d3463 100644 --- a/arch/x86/lib/msr-smp.c +++ b/arch/x86/lib/msr-smp.c @@ -2,6 +2,7 @@ #include #include #include +#include #include static void __rdmsr_on_cpu(void *info) @@ -159,6 +160,9 @@ static void __wrmsr_safe_on_cpu(void *info) rv->err = wrmsr_safe(rv->msr_no, rv->reg.l, rv->reg.h); } +/* Note: This version spins in smp_call_function_single(). + * Consider using rdmsr_safe_on_cpu_resched() variant instead. + */ int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h) { int err; @@ -175,6 +179,45 @@ int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h) } EXPORT_SYMBOL(rdmsr_safe_on_cpu); +struct msr_info_completion { + struct msr_info msr; + struct completion done; +}; + +static void __rdmsr_safe_on_cpu_resched(void *info) +{ + struct msr_info_completion *rv = info; + + __rdmsr_safe_on_cpu(&rv->msr); + complete(&rv->done); +} + +/* This variant of rdmsr_safe_on_cpu() does reschedule instead of polling */ +int rdmsr_safe_on_cpu_resched(unsigned int cpu, u32 msr_no, u32 *l, u32 *h) +{ + struct msr_info_completion rv; + call_single_data_t csd = { + .func = __rdmsr_safe_on_cpu_resched, + .info = &rv, + }; + int err; + + memset(&rv, 0, sizeof(rv)); + init_completion(&rv.done); + rv.msr.msr_no = msr_no; + + err = smp_call_function_single_async(cpu, &csd); + if (!err) { + wait_for_completion(&rv.done); + err = rv.msr.err; + } + *l = rv.msr.reg.l; + *h = rv.msr.reg.h; + + return err; +} +EXPORT_SYMBOL(rdmsr_safe_on_cpu_resched); + int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h) { int err; -- 2.16.2.804.g6dcf76e118-goog