From nobody Tue Apr 7 07:50:44 2026 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 68867C4332F for ; Wed, 12 Oct 2022 18:18:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229961AbiJLSSf (ORCPT ); Wed, 12 Oct 2022 14:18:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229846AbiJLSRq (ORCPT ); Wed, 12 Oct 2022 14:17:46 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8116CAE237 for ; Wed, 12 Oct 2022 11:17:25 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id pj13-20020a17090b4f4d00b0020b0a13cba4so2725705pjb.0 for ; Wed, 12 Oct 2022 11:17:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=oyyfiq1u1ow61sJxYdtEop67BKEuTOwTRoB44Qlve6k=; b=P2usEtoXL2d7Kadfzc6pHmhtZOrLbkU1dncF458jZB05kEaDR4KjrjxeuY2vSXDSFo bzwldY+iDn3YjfRcxMznyMfidl6/WuUWEP2QFEgmLDI3MAwPiaWHJCuCHdIGXcW+eSGN g/sEzO9PGlOgo6Vr6vFw6JZI73bhDmBxShW+cbqHQzscfO6bl7+73l8UYRTpUv1xyY3g +iU0rq7hqAlv49yzPLEq1daKsx2PPabZ8Od1fF8YSttMwMcfv7oszFPM2d32wM99sV4e h1fDGa/bn6rViRE0jfB29EyHUGVv1B9iTHYnpZW4bVSacmvCNnfVBmr1nw2Q07saZkag o1uA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oyyfiq1u1ow61sJxYdtEop67BKEuTOwTRoB44Qlve6k=; b=KZ+ZAncOzMputnKTpLKOh84OZdNYgTzsRNjZuNMUZfKXM/97WXhF7zG+ImvpFR9g5Y L3MRRBHAJ67rt4UtLbUuJEGhV9c9fiR9WblfVlrHclmvzGYi5PM0pXqs/N3Hf6y0lz8V TQatmSRUL+yNqz54mLZT1YKFKMqgfQv/4vckv9AoE1q8LtG6kIDGAWhv3KSuXy1ySem3 zZXRx2VAZ6CHpFBuUkdNHStAaUD62m8Z0qMYiG1+JBo4FXx65dJkyZqWn7pTVVddNpQO 1qSFFtvQjNT0kcO0w8IcTyzR4vV5gttH42LR1TZ5mKie6Bl1OM6ql8Agwe80yuscdyd6 oX6g== X-Gm-Message-State: ACrzQf0MkWNSpel8rOgCNaIee3FIeTfPgDvlpAB39tFv54C4of+NiPiz XJDqTaZ4UNovbsoS1HwYFP84roD4MzQ= X-Google-Smtp-Source: AMsMyM5a7PIgQlH+XQAxQkl3E0hWyOTVe15+NXdqge73zQWdRTWS96H4/6KaihK8TGdSJwuYFfp8urRmBZs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1349:b0:563:654d:ce3f with SMTP id k9-20020a056a00134900b00563654dce3fmr17351716pfu.32.1665598644547; Wed, 12 Oct 2022 11:17:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 12 Oct 2022 18:17:01 +0000 In-Reply-To: <20221012181702.3663607-1-seanjc@google.com> Mime-Version: 1.0 References: <20221012181702.3663607-1-seanjc@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20221012181702.3663607-11-seanjc@google.com> Subject: [PATCH v4 10/11] KVM: x86/mmu: Use static key/branches for checking if TDP MMU is enabled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Matlack , Isaku Yamahata Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that the TDP MMU being enabled is read-only after the vendor module is loaded, use a static key to track whether or not the TDP MMU is enabled to avoid conditional branches in hot paths, e.g. in direct_page_fault() and fast_page_fault(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu.h | 5 +++-- arch/x86/kvm/mmu/mmu.c | 14 ++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 1ad6d02e103f..bc0d8a5c09f9 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -2,6 +2,7 @@ #ifndef __KVM_X86_MMU_H #define __KVM_X86_MMU_H =20 +#include #include #include "kvm_cache_regs.h" #include "cpuid.h" @@ -230,13 +231,13 @@ static inline bool kvm_shadow_root_allocated(struct k= vm *kvm) } =20 #ifdef CONFIG_X86_64 -extern bool tdp_mmu_enabled; +DECLARE_STATIC_KEY_TRUE(tdp_mmu_enabled); #endif =20 static inline bool is_tdp_mmu_enabled(void) { #ifdef CONFIG_X86_64 - return tdp_mmu_enabled; + return static_branch_likely(&tdp_mmu_enabled); #else return false; #endif diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 4792d76edd6d..a5ba7b41263d 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -101,8 +101,10 @@ bool tdp_enabled =3D false; #ifdef CONFIG_X86_64 static bool __ro_after_init tdp_mmu_allowed; =20 -bool __read_mostly tdp_mmu_enabled =3D true; -module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0444); +static bool __read_mostly __tdp_mmu_enabled =3D true; +module_param_named(tdp_mmu, __tdp_mmu_enabled, bool, 0444); + +DEFINE_STATIC_KEY_TRUE(tdp_mmu_enabled); #endif =20 static int max_huge_page_level __read_mostly; @@ -5702,7 +5704,11 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forc= ed_root_level, max_tdp_level =3D tdp_max_root_level; =20 #ifdef CONFIG_X86_64 - tdp_mmu_enabled =3D tdp_mmu_allowed && tdp_enabled; + __tdp_mmu_enabled =3D tdp_mmu_allowed && tdp_enabled; + if (__tdp_mmu_enabled) + static_branch_enable(&tdp_mmu_enabled); + else + static_branch_disable(&tdp_mmu_enabled); #endif /* * max_huge_page_level reflects KVM's MMU capabilities irrespective @@ -6712,7 +6718,7 @@ void __init kvm_mmu_x86_module_init(void) * TDP MMU is actually enabled is determined in kvm_configure_mmu() * when the vendor module is loaded. */ - tdp_mmu_allowed =3D tdp_mmu_enabled; + tdp_mmu_allowed =3D __tdp_mmu_enabled; #endif =20 kvm_mmu_spte_module_init(); --=20 2.38.0.rc1.362.ged0d419d3c-goog