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(); }