From patchwork Tue Jun 20 14:06:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13285960 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5389FEB64D8 for ; Tue, 20 Jun 2023 14:11:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233183AbjFTOLH (ORCPT ); Tue, 20 Jun 2023 10:11:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233185AbjFTOKs (ORCPT ); Tue, 20 Jun 2023 10:10:48 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0615B1 for ; Tue, 20 Jun 2023 07:09:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687270198; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BqzD+6X0IKQNZN3ZvD+oTa51EhB1X9vozv9qaWAmHMs=; b=KGxe2PwlkWJGYgtBytb0UyYLPDeTZSOb91vVcQXa4jL3t2gKBq7vFAhOTr6dL8ENqd2uaK WDVh3S60JMX0ZmvBkfQGT/8ni6BOGqEfttQbFR6MCVlRHlBKYzyzGv5wnNcYcVxMOw63Ym bspjqCM3iH1p/PIccK0psSdoW0Er23o= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-306-VnIh31SuOY-wqOcCljomwQ-1; Tue, 20 Jun 2023 10:09:25 -0400 X-MC-Unique: VnIh31SuOY-wqOcCljomwQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 82C6228EA712; Tue, 20 Jun 2023 14:06:32 +0000 (UTC) Received: from llong.com (unknown [10.22.34.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id C7CDF425357; Tue, 20 Jun 2023 14:06:31 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Josh Poimboeuf , Pawan Gupta , Jacob Pan , Len Brown Cc: linux-kernel@vger.kernel.org, x86@kernel.org, linux-pm@vger.kernel.org, Robin Jarry , Joe Mario , Waiman Long Subject: [PATCH v2 1/5] x86/speculation: Provide a debugfs file to dump SPEC_CTRL MSRs Date: Tue, 20 Jun 2023 10:06:21 -0400 Message-Id: <20230620140625.1001886-2-longman@redhat.com> In-Reply-To: <20230620140625.1001886-1-longman@redhat.com> References: <20230620140625.1001886-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Sometimes it is useful to know the states the SPEC_CTRL MSRs to see what mitigations are enabled at run time. Provide a new x86/spec_ctrl_msrs debugfs file to dump the cached versions of the current SPEC_CTRL MSRs. Signed-off-by: Waiman Long --- arch/x86/kernel/cpu/bugs.c | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index 182af64387d0..f6e5910a4a2d 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -1733,6 +1734,84 @@ void cpu_bugs_smt_update(void) mutex_unlock(&spec_ctrl_mutex); } +#ifdef CONFIG_DEBUG_FS +/* + * Provide a debugfs file to dump SPEC_CTRL MSRs of all the CPUs + * Consecutive MSR values are collapsed together if they are the same. + */ +static ssize_t spec_ctrl_msrs_read(struct file *file, char __user *user_buf, + size_t count, loff_t *ppos) +{ + int bufsiz = min(count, PAGE_SIZE); + int cpu, prev_cpu, len, cnt = 0; + u64 val, prev_val; + char *buf; + + /* + * The MSRs info should be small enough that the whole buffer is + * copied out in one call. However, user space may read it again + * to see if there is any data left. Rereading the cached SPEC_CTRL + * MSR values may produce a different result causing corruption in + * output data. So skipping the call if *ppos is not starting from 0. + */ + if (*ppos) + return 0; + + buf = kmalloc(bufsiz, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + for_each_possible_cpu(cpu) { + val = per_cpu(x86_spec_ctrl_current, cpu); + + if (!cpu) + goto next; + + if (val == prev_val) + continue; + + if (prev_cpu == cpu - 1) + len = snprintf(buf + cnt, bufsiz - cnt, "CPU %d: 0x%llx\n", + prev_cpu, prev_val); + else + len = snprintf(buf + cnt, bufsiz - cnt, "CPUs %d-%d: 0x%llx\n", + prev_cpu, cpu - 1, prev_val); + + cnt += len; + if (!len) + break; /* Out of buffer */ +next: + prev_cpu = cpu; + prev_val = val; + } + + if (prev_cpu == cpu - 1) + cnt += snprintf(buf + cnt, bufsiz - cnt, "CPU %d: 0x%llx\n", + prev_cpu, prev_val); + else + cnt += snprintf(buf + cnt, bufsiz - cnt, "CPUs %d-%d: 0x%llx\n", + prev_cpu, cpu - 1, prev_val); + + count = simple_read_from_buffer(user_buf, count, ppos, buf, cnt); + kfree(buf); + return count; +} + +static const struct file_operations fops_spec_ctrl = { + .read = spec_ctrl_msrs_read, + .llseek = default_llseek, +}; + +static int __init init_spec_ctrl_debugfs(void) +{ + if (!debugfs_create_file("spec_ctrl_msrs", 0400, arch_debugfs_dir, + NULL, &fops_spec_ctrl)) + return -ENOMEM; + return 0; +} +fs_initcall(init_spec_ctrl_debugfs); +#endif + #undef pr_fmt #define pr_fmt(fmt) "Speculative Store Bypass: " fmt