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 From patchwork Tue Jun 20 14:06:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13285957 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 ACA8EEB64DB for ; Tue, 20 Jun 2023 14:10:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233056AbjFTOJw (ORCPT ); Tue, 20 Jun 2023 10:09:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233179AbjFTOJq (ORCPT ); Tue, 20 Jun 2023 10:09:46 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31F5EE60 for ; Tue, 20 Jun 2023 07:08:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687270137; 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=+DkmE6hETltBu3bPa+S3gxCm/8V55tvpJfZg0jQ8Gvw=; b=RbrYDQodAoRTDl7yNfYx9xXnJVfCT8r5+HNJ88d801pVpe+zgwgApyDUuStl3SiViQQSYc 4PBdGmJHsIgvTQVrYuNlnxSU1J1of2FPYSYntJ74j3vy8RhQPcD4RI8HHjgUAor5ORBi82 j9et0ziX2lL1fc9IuASSYKk73JpewSY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-37-2A_wpEtmMHKKyXq6jJXqlg-1; Tue, 20 Jun 2023 10:08:34 -0400 X-MC-Unique: 2A_wpEtmMHKKyXq6jJXqlg-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 5130888711B; Tue, 20 Jun 2023 14:06:33 +0000 (UTC) Received: from llong.com (unknown [10.22.34.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 91537425356; Tue, 20 Jun 2023 14:06:32 +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 2/5] x86/idle: Disable IBRS when cpu is offline Date: Tue, 20 Jun 2023 10:06:22 -0400 Message-Id: <20230620140625.1001886-3-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 Commit bf5835bcdb96 ("intel_idle: Disable IBRS during long idle") disables IBRS when the CPU enters long idle. However, when a CPU becomes offline, the IBRS bit is still set when X86_FEATURE_KERNEL_IBRS is enabled. That will impact the performance of a sibling CPU. Mitigate this performance impact by clearing all the mitigation bits in SPEC_CTRL MSR when offline and restoring the value of the MSR when it becomes online again. Signed-off-by: Waiman Long --- arch/x86/kernel/smpboot.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c index 352f0ce1ece4..5ff82fef413c 100644 --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -84,6 +84,7 @@ #include #include #include +#include /* representing HT siblings of each logical CPU */ DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map); @@ -1838,12 +1839,24 @@ void __noreturn hlt_play_dead(void) void native_play_dead(void) { + u64 spec_ctrl = spec_ctrl_current(); + + if (cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS)) { + this_cpu_write(x86_spec_ctrl_current, 0); + native_wrmsrl(MSR_IA32_SPEC_CTRL, 0); + } + play_dead_common(); tboot_shutdown(TB_SHUTDOWN_WFS); mwait_play_dead(); if (cpuidle_play_dead()) hlt_play_dead(); + + if (cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS)) { + native_wrmsrl(MSR_IA32_SPEC_CTRL, spec_ctrl); + this_cpu_write(x86_spec_ctrl_current, spec_ctrl); + } } #else /* ... !CONFIG_HOTPLUG_CPU */ From patchwork Tue Jun 20 14:06:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13285962 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 E4C2FEB64DC for ; Tue, 20 Jun 2023 14:13:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231842AbjFTONP (ORCPT ); Tue, 20 Jun 2023 10:13:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233204AbjFTONE (ORCPT ); Tue, 20 Jun 2023 10:13:04 -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 0D5E5E6C for ; Tue, 20 Jun 2023 07:12:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687270326; 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=Vsqv+DP+Q1yGAa17LUaSDJcWkXqyYpdPTd1SszzLemY=; b=JUZoVOlubD5/rgx63fv3WQbCXKVorHNdos4SaQ6xY1mtXvWZlnntYcSgq7HKoWo6cAkLiy UTFLpG3oI+Dp5nm4EGNPiYM0GlJ3YxhDwr9EqQQerwXexmfKgbGFo7LWuPAswWodqV/nuf HQ4LVwagqKejkfBssFsGYg0WT1vjAf8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-652-ZZYo27n3NOGPXHo8bwi6VQ-1; Tue, 20 Jun 2023 10:09:47 -0400 X-MC-Unique: ZZYo27n3NOGPXHo8bwi6VQ-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 198FC8871FB; Tue, 20 Jun 2023 14:06:34 +0000 (UTC) Received: from llong.com (unknown [10.22.34.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 601E7425356; Tue, 20 Jun 2023 14:06:33 +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 3/5] intel_idle: Sync up the SPEC_CTRL MSR value to x86_spec_ctrl_current Date: Tue, 20 Jun 2023 10:06:23 -0400 Message-Id: <20230620140625.1001886-4-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 When intel_idle_ibrs() is called, it modifies the SPEC_CTRL MSR to 0 in order disable IBRS. However, the new MSR value isn't reflected in x86_spec_ctrl_current. That will cause the new spec_ctrl_msrs debugfs file to show incorrect result. Fix that by updating x86_spec_ctrl_current percpu value to always match the content of the SPEC_CTRL MSR. Signed-off-by: Waiman Long --- drivers/idle/intel_idle.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/idle/intel_idle.c b/drivers/idle/intel_idle.c index aa2d19db2b1d..07fa23707b3c 100644 --- a/drivers/idle/intel_idle.c +++ b/drivers/idle/intel_idle.c @@ -181,13 +181,17 @@ static __cpuidle int intel_idle_ibrs(struct cpuidle_device *dev, u64 spec_ctrl = spec_ctrl_current(); int ret; - if (smt_active) + if (smt_active) { + __this_cpu_write(x86_spec_ctrl_current, 0); native_wrmsrl(MSR_IA32_SPEC_CTRL, 0); + } ret = __intel_idle(dev, drv, index); - if (smt_active) + if (smt_active) { native_wrmsrl(MSR_IA32_SPEC_CTRL, spec_ctrl); + __this_cpu_write(x86_spec_ctrl_current, spec_ctrl); + } return ret; } From patchwork Tue Jun 20 14:06:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13285956 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 2DFF7EB64D7 for ; Tue, 20 Jun 2023 14:10:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232144AbjFTOJx (ORCPT ); Tue, 20 Jun 2023 10:09:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233205AbjFTOJt (ORCPT ); Tue, 20 Jun 2023 10:09:49 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20880E68 for ; Tue, 20 Jun 2023 07:09:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687270147; 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=+LliIS9GzKVY7r+2Pd1dAZaFc5KbOmlOofBoCL5VmAU=; b=Uxrnv2+DdKsm5B12CJO9YlS2w5bgKV/Y/gMvEi2fOzenDCdrUIC+/OaGfnVdXfgBI4pvEE S8S3QmU5LrQJx70Qu6rB5QuBC4jHzK1b7rrV7rB7K6IDKlXI4COvRYiTWsdxHIk7iW/rdY QiAZzzvyH39sZt5UJk8hRxiJ63+o/O4= 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-9-uWgs1p_OOFmaIFWneI7weA-1; Tue, 20 Jun 2023 10:08:56 -0400 X-MC-Unique: uWgs1p_OOFmaIFWneI7weA-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 EC30F28EA71F; Tue, 20 Jun 2023 14:06:34 +0000 (UTC) Received: from llong.com (unknown [10.22.34.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2A712425356; Tue, 20 Jun 2023 14:06:34 +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 4/5] intel_idle: Add no_ibrs module parameter to force disable IBRS Date: Tue, 20 Jun 2023 10:06:24 -0400 Message-Id: <20230620140625.1001886-5-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 Commit bf5835bcdb96 ("intel_idle: Disable IBRS during long idle") disables IBRS when the cstate is 6 or lower. However, there are some use cases where a customer may want to use max_cstate=1 to lower latency. Such use cases will suffer from the performance degradation caused by the enabling of IBRS in the sibling idle thread. Add a "no_ibrs" module parameter to force disable IBRS and the CPUIDLE_FLAG_IRQ_ENABLE flag if set. In the case of a Skylake server with max_cstate=1, this new no_ibrs option will increase the IRQ response latency as IRQ will now be disabled. Signed-off-by: Waiman Long --- drivers/idle/intel_idle.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/idle/intel_idle.c b/drivers/idle/intel_idle.c index 07fa23707b3c..366dacccc971 100644 --- a/drivers/idle/intel_idle.c +++ b/drivers/idle/intel_idle.c @@ -69,6 +69,7 @@ static int max_cstate = CPUIDLE_STATE_MAX - 1; static unsigned int disabled_states_mask __read_mostly; static unsigned int preferred_states_mask __read_mostly; static bool force_irq_on __read_mostly; +static bool no_ibrs __read_mostly; static struct cpuidle_device __percpu *intel_idle_cpuidle_devices; @@ -1907,12 +1908,15 @@ static void __init intel_idle_init_cstates_icpu(struct cpuidle_driver *drv) WARN_ON_ONCE(state->flags & CPUIDLE_FLAG_IRQ_ENABLE); state->enter = intel_idle_xstate; } else if (cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS) && - state->flags & CPUIDLE_FLAG_IBRS) { + ((state->flags & CPUIDLE_FLAG_IBRS) || no_ibrs)) { /* * IBRS mitigation requires that C-states are entered * with interrupts disabled. */ - WARN_ON_ONCE(state->flags & CPUIDLE_FLAG_IRQ_ENABLE); + if (no_ibrs && (state->flags & CPUIDLE_FLAG_IRQ_ENABLE)) + state->flags &= ~CPUIDLE_FLAG_IRQ_ENABLE; + else + WARN_ON_ONCE(state->flags & CPUIDLE_FLAG_IRQ_ENABLE); state->enter = intel_idle_ibrs; } else if (state->flags & CPUIDLE_FLAG_IRQ_ENABLE) { state->enter = intel_idle_irq; @@ -2165,3 +2169,9 @@ MODULE_PARM_DESC(preferred_cstates, "Mask of preferred idle states"); * 'CPUIDLE_FLAG_INIT_XSTATE' and 'CPUIDLE_FLAG_IBRS' flags. */ module_param(force_irq_on, bool, 0444); +/* + * Force the disabling of IBRS when X86_FEATURE_KERNEL_IBRS is on and + * CPUIDLE_FLAG_IRQ_ENABLE isn't set. + */ +module_param(no_ibrs, bool, 0444); +MODULE_PARM_DESC(no_ibrs, "Disable IBRS when idle"); From patchwork Tue Jun 20 14:06:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13285958 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 62541EB64DB for ; Tue, 20 Jun 2023 14:10:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231710AbjFTOKf (ORCPT ); Tue, 20 Jun 2023 10:10:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233008AbjFTOK0 (ORCPT ); Tue, 20 Jun 2023 10:10:26 -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 94CD610D0 for ; Tue, 20 Jun 2023 07:09:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687270181; 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=VrByPcLlZJlbrBKqAsdOzQ0Sy0EOhnQ6P87fZxDadIk=; b=i6OHoRugFlYo9CT4soGMlAxmnqbZa1omNDrw4yMiL75n+YOKZZ94enHs95UtBm6Kquwd7b EPcoDFwBE09PWh3ETHDQ2ArvauxDlRmBXEThUvumZMrxoxie3MSG4GjaX7EB9jI2OsdDaP wfqzHR1QHIF3XjKE7SsLH7v8RUmLGkw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-466-rhmpZFmgOz-Gw5i0hq2awQ-1; Tue, 20 Jun 2023 10:09:26 -0400 X-MC-Unique: rhmpZFmgOz-Gw5i0hq2awQ-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 BC295104D984; Tue, 20 Jun 2023 14:06:35 +0000 (UTC) Received: from llong.com (unknown [10.22.34.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 078BD425357; Tue, 20 Jun 2023 14:06:34 +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 5/5] x86/idle: Disable IBRS entering mwait idle and enable it on wakeup Date: Tue, 20 Jun 2023 10:06:25 -0400 Message-Id: <20230620140625.1001886-6-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 When a user sets "intel_idle.max_cstate=0", it will disable intel_idle and fall back to acpi_idle instead. The acpi_idle code will then call mwait_idle_with_hints() to enter idle state. So when X86_FEATURE_KERNEL_IBRS is enabled, it is necessary to disable IBRS within mwait_idle_with_hints() when IRQ was disabled to avoid performance degradation on silbing thread running user workload. Signed-off-by: Waiman Long --- arch/x86/include/asm/mwait.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index 778df05f8539..1e36cdc21661 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -108,15 +108,32 @@ static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx) static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) { if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) { + bool ibrs_disabled = false; + u64 spec_ctrl; + if (static_cpu_has_bug(X86_BUG_CLFLUSH_MONITOR)) { mb(); clflush((void *)¤t_thread_info()->flags); mb(); } + if (irqs_disabled() && + cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS)) { + /* NMI always enable IBRS on exception entry */ + ibrs_disabled = true; + spec_ctrl = spec_ctrl_current(); + __this_cpu_write(x86_spec_ctrl_current, 0); + native_wrmsrl(MSR_IA32_SPEC_CTRL, 0); + } + __monitor((void *)¤t_thread_info()->flags, 0, 0); if (!need_resched()) __mwait(eax, ecx); + + if (ibrs_disabled) { + native_wrmsrl(MSR_IA32_SPEC_CTRL, spec_ctrl); + __this_cpu_write(x86_spec_ctrl_current, spec_ctrl); + } } current_clr_polling(); }