From patchwork Mon Jun 5 00:43:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 103050 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2395121vqr; Sun, 4 Jun 2023 18:24:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5NITrTxzlp6QE/BBSuM4t+2Dy7BjFiyLGKLTIxH4XURgCP5WllSzqufkvO9BVU9cx+7a1/ X-Received: by 2002:a17:903:32c6:b0:1ad:cba5:5505 with SMTP id i6-20020a17090332c600b001adcba55505mr7015341plr.14.1685928254590; Sun, 04 Jun 2023 18:24:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685928254; cv=none; d=google.com; s=arc-20160816; b=Zp2RCp9DemeuGYvf/F8wKoOgj7mnG3zKfqRQsfF9d3NsnVvTX2GtgwujGqIgFg2MD3 NuUzyYkwUhSkF3KFNtC1aFSpxYsW8Uc+d3u8E/yoRTCQ8yzYaKkfpK4qSZcj/GDjeu61 bHdR7wm3vMEZenHlpX14516T7AmTlhh4zpztUp+C6vKRwhYCcBo+YIAt3OpvtbguXETM 33AQnlKmXIxQekIP6b9ztyZRM/MEXnqiy38Z5YsDeM1PrFAmc7W8CZcEUFxDk1CyNRk/ EoooAEwzoyPL14KxINIH26HiPs/MF2/jYYAfEH8PAq0gxEMDmj2QiAvzfGAAt8vkqwGb /U/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :reply-to:dkim-signature; bh=BYxU3tDQaD0OtzoBTGZivwsSzNx0WWibXW+uDpPnJvU=; b=QOVpwD1AjZ33ha2qrqoTbJ0SGDAmaFBMw3/PcrvgA4Od2dTxWnlrouYKs7huG109iW LQOwuQ+RRP3y26FHmeX3e2pNcPKw+cNSp7rz5l0KHM5lHpVcmp1aSQ0h9qiQpnPU16pG Tnv/6qhw15yI7Y+rvxFQkSaWyPvaYhT4HrP9M+yI59nfmvISRXFFBa7GimJLhooJcFyL Wd9fYKWNmUc03ZTAq55iji1Yx2bavgzEKzOScRTTP+FS5SgXdSGGP95CJ/aIlp8pVYSz U/KsC57tPB+dfCnSh21gHH63wAD94sJExo2afncfFODg24Mg5HZo6n1cwhdmpPpMpyKn lr0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=oAgqtfKX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ik6-20020a170902ab0600b001ac6b926624si4563223plb.122.2023.06.04.18.24.01; Sun, 04 Jun 2023 18:24:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=oAgqtfKX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S232302AbjFEAnl (ORCPT + 99 others); Sun, 4 Jun 2023 20:43:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229449AbjFEAni (ORCPT ); Sun, 4 Jun 2023 20:43:38 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC44FD2 for ; Sun, 4 Jun 2023 17:43:37 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-650cb5a6b0cso3540276b3a.1 for ; Sun, 04 Jun 2023 17:43:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685925817; x=1688517817; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=BYxU3tDQaD0OtzoBTGZivwsSzNx0WWibXW+uDpPnJvU=; b=oAgqtfKXKNUrueRjish0M8AHkqAawHUKAJ+c4CZWvHBzzQrhldagmTBUj+xNuhrnIh 6dewdVNCfLHvWt8cNP67wp5N3KZEkztShDNowKlbfWluw0icCU5hpvk7BSOsBEbavKo3 gHWEvALztvaN3xUitH4aSGZ7pmlJRqE9uGY0ZrTkHmK4w7M9RqNQTJvx0+4HbrJaSXkq 9fTHEyzXXQOisQvJJ+aPNza0nHJmbDZpRJR1KHw+cZka55S1aTQf7yJ1GghVcmnzj9sC m1tre3ObKgwLQKwqGu2MrneFQHDw1nIjdcL1hrp0uThybKKCk2629Uqw3HcJ1FgB+4ma eP9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685925817; x=1688517817; h=cc:to:from:subject:message-id:mime-version:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BYxU3tDQaD0OtzoBTGZivwsSzNx0WWibXW+uDpPnJvU=; b=Q9YUv+1rlcsRYOSEroGOgLSeCnD86jV88hlH3fiRC2OxY+WCHTbQRI5WFgzD+jXWo4 QLC4RzSrgFx5lyZLiKgzgMBW79feUMxdif/zYUHP97bnaIrSSRHTkxoXyQPhtNwaLNWb UfQPsPfH/UByx8UiYJFU1SP+xdHObqfJ+PIMGYK9voMnE+d/cBcw6iUFYJ8sQpBFvpO/ fhfA1VYAsA47FrMfjD+xoV94/KNRg3Mn2hFAXuiacxbi/qUPsIJPGyESWWmQtpnSMjpc Ntj2Ebj6tTIOAWasg5hbcWPM45yod0xtsi0GVz0cNTEX/N6bx7HYk+KrYcRaTIi0MILq kEQg== X-Gm-Message-State: AC+VfDyLEC00fN+OARcU1ysrkf1b2oucYYyN1PcQR7mnP7WRNRqsreQ+ jNWnLrPaNPwIqKoBj0mOUoBRstIqBH8i X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a05:6a00:2d24:b0:63d:2cff:bfbc with SMTP id fa36-20020a056a002d2400b0063d2cffbfbcmr6805016pfb.3.1685925817423; Sun, 04 Jun 2023 17:43:37 -0700 (PDT) Reply-To: Mingwei Zhang Date: Mon, 5 Jun 2023 00:43:34 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230605004334.1930091-1-mizhang@google.com> Subject: [PATCH] KVM: x86/mmu: Remove KVM MMU write lock when accessing indirect_shadow_pages From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini Cc: "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jim Mattson , Mingwei Zhang , Ben Gardon X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767823905731786293?= X-GMAIL-MSGID: =?utf-8?q?1767823905731786293?= Remove KVM MMU write lock when accessing indirect_shadow_pages counter when page role is direct because this counter value is used as a coarse-grained heuristics to check if there is nested guest active. Racing with this heuristics without mmu lock will be harmless because the corresponding indirect shadow sptes for the GPA will either be zapped by this thread or some other thread who has previously zapped all indirect shadow pages and makes the value to 0. Because of that, remove the KVM MMU write lock pair to potentially reduce the lock contension and improve the performance of nested VM. In addition opportunistically change the comment of 'direct mmu' to make the description consistent with other places. Reported-by: Jim Mattson Signed-off-by: Mingwei Zhang --- arch/x86/kvm/x86.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) base-commit: 31b4fc3bc64aadd660c5bfa5178c86a7ba61e0f7 diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 5ad55ef71433..97cfa5a00ff2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8585,15 +8585,9 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, kvm_release_pfn_clean(pfn); - /* The instructions are well-emulated on direct mmu. */ + /* The instructions are well-emulated on Direct MMUs. */ if (vcpu->arch.mmu->root_role.direct) { - unsigned int indirect_shadow_pages; - - write_lock(&vcpu->kvm->mmu_lock); - indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; - write_unlock(&vcpu->kvm->mmu_lock); - - if (indirect_shadow_pages) + if (READ_ONCE(vcpu->kvm->arch.indirect_shadow_pages)) kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); return true;