Received: by 10.213.65.68 with SMTP id h4csp1600830imn; Mon, 19 Mar 2018 08:27:03 -0700 (PDT) X-Google-Smtp-Source: AG47ELuoDy3myS93yChbNT/VhFOfCdez5AtvV8vlShVZ8a/HFjR02WvAS8Lybbb7hiJM4wq8Xc6b X-Received: by 10.99.98.134 with SMTP id w128mr9388623pgb.217.1521473223633; Mon, 19 Mar 2018 08:27:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521473223; cv=none; d=google.com; s=arc-20160816; b=Wm69HuQfsQOVMORpSzHQOONfG6AI5Gg2O49Jg1rR9rQX6Feowm4+XgbehA0ZX2+2GO 9V0p/iI0iZbauAVVZW2QSx6LKHSlFye4QjbJYCNgpfrTw4cKoKkexvHk/IUzy5IP/LQ+ wgwJ0410/cZj6CD7xmonK59EXsPbprhH8XWftmRCrdglRa8N++zix4lfgmac9w7CGKi4 Iccr/P6tpO0AgWs5VZ9qtb1Xlj3eaL8sUES8okCLWQSlKLEzZWM8VxNasOlWYn7i5X4D D3uOteiI6MA5wbuKjWjZ6Kt5znO8nR91xaC9lSySVY4pLCHlwUHiIar2pjDSWsx0Hryb MLHg== 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=IaFD7YoI8Y7vrggxdliLjFd+GZXkCuYi5ojoKcVvOB0=; b=IvQSbv74ztOvkjiuCyMuvtVT7+MLWFfdo8WPT7DP5P3QlclTYAjanL/QQ36LWnyQ+C My/UqnsDX/KjsESr4rIka5+VDQtNYb480Hv2afWxgum9U/a1vWueFL41UovJFqsZ95BN 6b/rB/J0WClFpRS1ZA4+PWhrndUmaPtThrWFPgzKXug4jDPjmxA6S5RwTV6LOOPzxxO/ kGCpiZA/q96UUskf//hIi5sMqX4GhNVC2LIOp1YNhlM20P0b5u6a1KkV/x+YqJIeeyIh cUPEDf50SXGRT73TFLZ4oz8/Y410N7gbYM30HX+TCQ5Wpd4NgPHAJxv+ZBRqf9itzv5w RxEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ToxRmzxx; 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 f33-v6si145586plb.482.2018.03.19.08.26.49; Mon, 19 Mar 2018 08:27:03 -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=ToxRmzxx; 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 S1755834AbeCSPZJ (ORCPT + 99 others); Mon, 19 Mar 2018 11:25:09 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:42630 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755793AbeCSPZH (ORCPT ); Mon, 19 Mar 2018 11:25:07 -0400 Received: by mail-pf0-f196.google.com with SMTP id a16so7135745pfn.9 for ; Mon, 19 Mar 2018 08:25:07 -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=IaFD7YoI8Y7vrggxdliLjFd+GZXkCuYi5ojoKcVvOB0=; b=ToxRmzxx/yRd8trFruOHWw3yP1Hp06r1ZXY8vWUaEWzhyS0aYolcZS2whTXbqZNFT7 FKbPpvpLrtTilw6AG69+881aSL3MsFKNCkHLwt+zZlwqJy8hE+De9bpQfQM1LIHj+nio vKRwHvVNII24KvCi7arsnRAHLqhircFtQphDLdp5YV0xO5jOfHn+wno01KGOVzt0JN0o k1KQm9CO4LPmBVoS+qfsdiEGzHK2WNVBvTZQb3EUeuQFrR6/WRXaEx5AyVphUhWgq4YX ZnOBP8GkQqlwp8Lp8BZye0is1fAyalDOrrLPXfwxtUs+hfuNLfecz98BbKwC5O8ULyS3 iFOA== 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=IaFD7YoI8Y7vrggxdliLjFd+GZXkCuYi5ojoKcVvOB0=; b=Hgrp+A2XOMBL/Bzfxm9UUI+sfu3487rZAL52bTsS80KaTK5iulriYoMrIbIPpCWJqV ZHaMmaHfHW2gHHkDB+s0ND75TPVxZ0Zj5rKItJ9LK7NYpeu5B5E7jJ69E51QprISZRlr tKOzKr5my+wFGZlJs4lH2kYQNT/DQiJqFKrGJDt95sCu+rBKXRt0FPkakmcLrfEsXn++ hVubwrSSZFZTKxV5DNLIJLvgDwelQvS5oy5iN9me4m2LNpggeutY3FJP/3tliHbblkd0 OUzpOYkeRAiEv2l+rr+orb2hgnwcgR2agw1CBV8O22OV4Fcu9ixSNCZVX1ZonKSmqhtc zSPg== X-Gm-Message-State: AElRT7GJ9NxhLeiffPrIhpZ8eDQn9nrx/aqK8ghSWUvCR01gmTBiR9X+ prDeFlNOMvaGf1EN0+zI1PxmNg== X-Received: by 10.101.64.9 with SMTP id f9mr9632057pgp.247.1521473106753; Mon, 19 Mar 2018 08:25:06 -0700 (PDT) Received: from localhost ([2620:15c:2c4:201:f5a:7eca:440a:3ead]) by smtp.gmail.com with ESMTPSA id v2sm502520pgo.36.2018.03.19.08.25.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 19 Mar 2018 08:25:05 -0700 (PDT) From: Eric Dumazet To: x86 Cc: lkml , Eric Dumazet , Eric Dumazet , "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , Hugh Dickins Subject: [PATCH v2 1/2] x86, msr: add rdmsr_safe_on_cpu_resched() and use it in msr_read() Date: Mon, 19 Mar 2018 08:25:02 -0700 Message-Id: <20180319152503.123372-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 --- 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(-) diff --git a/arch/x86/include/asm/msr.h b/arch/x86/include/asm/msr.h index 30df295f6d94c8ac6d87613acae8a32c50436c6d..15e220243a4d5e9da524fb7733e23e2766b6eb12 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); @@ -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); 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