Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6965444rwb; Mon, 12 Dec 2022 08:26:09 -0800 (PST) X-Google-Smtp-Source: AA0mqf6mcJW7cV2Nyvq9ZxqYfyRgAHgH23eA2FUTQ61E6Z7BhJhc/Nz3OEg57I7zOQ2u1aDQiXB8 X-Received: by 2002:a17:907:bd8:b0:7c1:706:d5bb with SMTP id ez24-20020a1709070bd800b007c10706d5bbmr16927069ejc.12.1670862369435; Mon, 12 Dec 2022 08:26:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670862369; cv=none; d=google.com; s=arc-20160816; b=aU9Pq9yAjU8H+gahYeTkDgISPZ/6Kyuy2VY6ZTS2AmsRCrnz6L5FfNqiDGPkTi9t5n Lb8KcZCf1NnGPgf3QbRlxhgzvZjDErqVp4UNqek7Pl1nJasvJTLvIYHdm4j3m7OkxXdk PrEnSS1hbUsDXj4jsjKYuAP4yZIDYgapU4MPuEQq4AJtockuqAo5BVuWdnc81OyZids9 9xJhd3F541cqsZmYp+ImTpouBza66370upipm74z8czcXgn+mm4hhVr57RHvEGpHGF4a eKYNLoW9UCMBd5fsO5liktIkXBEZDnnixHUInZqXHGtYABuyoUyZ6s14xO/bo/wqThmx bEYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WmyZB7P+T7cldWqjuRT6Y6vUTkEvSfVPUCpeMf28+1M=; b=SoN80hx4FQ7iZjVMCWrYWVVNwOBDwHQmOT9LvHlcX/y1i7XHi+CVwKksMgdFA3zyBZ UB5MN3ApGG4vHBTvnU5j0O+aS7QMV3B9im4FwPkugeubNpktwZPRJOle+CQe7jvLYfF6 keOUpCx6zNi1r8U6AQKckGbwjPYisyvgwQqYRhm8gqntBQmr29LgVAqBzP9aozriQPf2 +XcgzQ/BS77FQ3j8Ut9cBe5k4b83jo3rwJ7fjpc2o78iQeznL8gd/rkPmRM+r0SNUGY0 eqFUSxZ5T5OEtS2J494mnFm7vgwVagPO0ERCR+OuPoIln4KMgFv393z2X+TueTx82yMO 7+ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AJ45vMDi; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qk41-20020a1709077fa900b007416e100f3dsi5991418ejc.986.2022.12.12.08.25.50; Mon, 12 Dec 2022 08:26:09 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=AJ45vMDi; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232452AbiLLPbK (ORCPT + 74 others); Mon, 12 Dec 2022 10:31:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231715AbiLLPbG (ORCPT ); Mon, 12 Dec 2022 10:31:06 -0500 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE19CFCFF; Mon, 12 Dec 2022 07:31:04 -0800 (PST) Received: by mail-pj1-x1030.google.com with SMTP id 3-20020a17090a098300b00219041dcbe9so148885pjo.3; Mon, 12 Dec 2022 07:31:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WmyZB7P+T7cldWqjuRT6Y6vUTkEvSfVPUCpeMf28+1M=; b=AJ45vMDiLsDYC0RGs8ey/ELAXs1CCXKzolW+nCXoW6DuKV5tPfmB2l3CyrBXA3/93U 3d6O8be+Sz1gg0Lzo4hGPAWY/HGiXJ0OX6QHboSQFyZmVr/Iim+X/1JTMDoJE1J9lQjr g8hIUb+vXQ4CMD6fImvPp3fMGzSIi963oKLr+IprrFytd56NhpoJQ28Sk0J0YHGim1/B hwc1FN+QV0Ka3J73gqAL5bybDJ/ZwrkauKS/Kf2SnnzMD+8uP/2Znek+kQgfyKjbfbDM l1mavjbYTuhYWg07jQ8+D0mC2yJ1QBXEmCGr2/+mFWQWc1u5KHoFr4yTR8jLl7lr+Nyq Uz7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WmyZB7P+T7cldWqjuRT6Y6vUTkEvSfVPUCpeMf28+1M=; b=Y8xEOb/BSqcQTdOdLSRiETYuQkYxCl8qc/mPkAmfL6HRf6UmN1jiCpZXAACowseNgK vH18pSepnS2om2vFOEBsLjCXz8qP3scXRT9m2sjUvo6hPWWm4ve2heq0JQRSNx/BzdV6 IJ879NZi4dDnTiyJoGbgIWAkl19h0oa2Rdic1PPELwKw3VCwiMHvA/DVP+xgNyghNGK+ dxvFzdOuKpxik2EC2VZnZJYejMGwEebYKYrYqdqNWhYVo6gSM5Bzm8kdIauvQf8SF+01 GU3g9+eeBlvGgBiiDNeV9CZJ61tzqlhcT0zhUbq041QrGSy1HlTj68uAGgGdAnA1hdlp v67w== X-Gm-Message-State: ANoB5pmwnnOCrfGBv/TrLIkmO6DqKzBao5gCX84fNwuBUEm/Piw/HUHA MqvaRI3PDpuSLyYy9/N4VlrYYNXUjug= X-Received: by 2002:a17:903:2112:b0:189:e711:170 with SMTP id o18-20020a170903211200b00189e7110170mr15330874ple.64.1670859063909; Mon, 12 Dec 2022 07:31:03 -0800 (PST) Received: from localhost ([47.254.32.37]) by smtp.gmail.com with ESMTPSA id b4-20020a170902d50400b0017f73caf588sm6538974plg.218.2022.12.12.07.31.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Dec 2022 07:31:03 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Lai Jiangshan , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org Subject: [PATCH 1/2] kvm: x86/mmu: Reduce the update to the spte in FNAME(sync_page) Date: Mon, 12 Dec 2022 23:32:04 +0800 Message-Id: <20221212153205.3360-2-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20221212153205.3360-1-jiangshanlai@gmail.com> References: <20221212153205.3360-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 From: Lai Jiangshan Sometimes when the guest updates its pagetable, it adds only new gptes to it without changing any existed one, so there is no point to update the sptes for these existed gptes. Also when the sptes for these unchanged gptes are updated, the AD bits are also removed since make_spte() is called with prefetch=true which might result unneeded TLB flushing. Do nothing if the permissions are unchanged or only write-access is being added. Only update the spte when write-access is being removed. Drop the SPTE otherwise. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/mmu/paging_tmpl.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index e5662dbd519c..613f043a3e9e 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -1023,7 +1023,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) for (i = 0; i < SPTE_ENT_PER_PAGE; i++) { u64 *sptep, spte; struct kvm_memory_slot *slot; - unsigned pte_access; + unsigned old_pte_access, pte_access; pt_element_t gpte; gpa_t pte_gpa; gfn_t gfn; @@ -1064,6 +1064,23 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) continue; } + /* + * Drop the SPTE if the new protections would result in access + * permissions other than write-access is changing. Do nothing + * if the permissions are unchanged or only write-access is + * being added. Only update the spte when write-access is being + * removed. + */ + old_pte_access = kvm_mmu_page_get_access(sp, i); + if (old_pte_access == pte_access || + (old_pte_access | ACC_WRITE_MASK) == pte_access) + continue; + if (old_pte_access != (pte_access | ACC_WRITE_MASK)) { + drop_spte(vcpu->kvm, &sp->spt[i]); + flush = true; + continue; + } + /* Update the shadowed access bits in case they changed. */ kvm_mmu_page_set_access(sp, i, pte_access); -- 2.19.1.6.gb485710b