From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 425C4346A0C for ; Fri, 23 Jan 2026 08:58:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158735; cv=none; b=OUMwSsAkRLWKitzDeKv0CLQxveuW7hUbwD6RDLb1/Pe/vjU1pz016mEbK6ExOuT7lqv2SE+ZKenNbGiNGR5xT5xwVO1FjFpYZdo0jR1G7GqKzRNHGLEYAT9dBjWe8UTJ0FzaI7fJtyU+C/kTjmLihv5MrqzoTI3knF2BEviGke0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769158735; c=relaxed/simple; bh=H3kIgqZ1fpIQgO5dvj4uCp4+YHfpl+70b9+j6uz199k=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=D0OrkjHOJOuleqvUyIDHWOAbJ4+gIb98F1NmuyusZG0QP6hMczeRoOV7nkuvCAoeGcq3TE4snKRCXGpHxfJyaYwZq8vCUNzyH43FzQkGG2XxSX91d2/SK9CjszT2AUyda88pEc7rmjtX4M3vhk2/MqfL6tXImOpwENbXvY0tvUo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lHiUoaIk; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lHiUoaIk" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2a102494058so15413065ad.0 for ; Fri, 23 Jan 2026 00:58:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769158730; x=1769763530; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=tw87ncFPvx87YF4rRJcW420dhLX4aAoK8XINI/y7618=; b=lHiUoaIkHquSC2uXFusOL3zGCxZ6PzvXBOsCAirwNVY343UC3TesL1evpXOTpsi+Ch 14/WNDhXYgek6+okFntIkcjyNgom8geIsulVkQvCczgVVnUMwHVJD7EwK/3AgzxRjjvE gG5BxXsjs3vduH/lCPg24WRAbOi0JM3bef8vWmq69a2A9ESIq38XXttXDqwZc2KfbXCH VuQzWYAs95y9vl/cXzZVytx5LrFlczG2LqeY5+nRBg12zLRiqls/dNJomViFmEYv9pnr 6m76O66cqk97hhanjlAD5GGQ6osIvHjJvYAqXm73VKkuwelCxzI9SOSImGhz1/RixzhD cTLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769158730; x=1769763530; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=tw87ncFPvx87YF4rRJcW420dhLX4aAoK8XINI/y7618=; b=svLlUvSOM67oGs+a//ueKaxenqja4VF5p1BVcnAV5BJC+RKc7/+Cz8HoMN5E8RHoKJ LTZJ5dN4hlTnObDAOyK5lrMnOmFRwICqHY9Vvvmabq8RGM2XPQyaI/8YSVqgEK9ABqmE 7t+8SSrhiho9XfyV+Z2Pbc0+2BwkiHiHIOm0eA5pZyhQCzQ2VMgbbtfXPWB6sgdCWIL3 NiHLip37PwyXr6DmACYgfT9NGwusARBTCZvJpwFEATr+QUbPrM9RQzE3zs+qRasy76FG tWKp+DhnnBeLhcd++gP8jvl2muzR47YFqUjZuzLIs8PxQ2szmVOcc9xR7NKYLWvlcTYe 6N3Q== X-Gm-Message-State: AOJu0Yzi8m6V7SEJD+L2X+VPTb2NIXWxv/vFsp9cXU4AMMagOAlDFXqc r/Vx2jwUMKWtemFUzlzoJ3kRWcGRHX+6t+PlRnEPs7x617jDakYCLuxnU01gZiN8 X-Gm-Gg: AZuq6aKWLO/lxCT5eDs6OrycWYNvBLZKzFAXR8y5983mkmKIDW81/fpOI6BiXUAEvaT CgMUmyi/HTqSuS7e7zqr8FV3SeHtLoUnSbpb0ZTOv8TkFUVChiMbpHJSoUiPTIGlCan7i9/cbib r3Y3PH6e6AmozDzQyVUz0s2L7UmBQ1fAfJttQJOIijGJb7K8iQp/FZcd4bXTcXrj6vjzWdkue4b 7IRjlHqhzDM+1/IDjYguF13w6sCMJxzibiFcxX3sCbbEc7Kk9TqGQa1zTRlXNX6iGaidNuNIibX KbFDUQ5m8hTX8J8pfj4evKgaV268dxJcDrMOs6tgSgVEg80JMMAP3sdepKhLI7MRJyX+epElCVJ AFSS0yxi6yy/Xpjv/couoQeSF++aeq9a5JHdk3ZWarHhSs2BaYBacpRnqYPOqEHHDovIMmARm6Q /7YsH5DEItPj8= X-Received: by 2002:a17:903:4b0d:b0:2a0:eaf5:5cd8 with SMTP id d9443c01a7336-2a7d2f17f93mr46191495ad.9.1769158730217; Fri, 23 Jan 2026 00:58:50 -0800 (PST) Received: from localhost ([240b:4000:bb:1700:7b36:494d:5625:5a0]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a802dcd776sm13937155ad.26.2026.01.23.00.58.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Jan 2026 00:58:49 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: Lai Jiangshan , Sean Christopherson , Paolo Bonzini , 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: Don't check old SPTE permissions when trying to unsync Date: Fri, 23 Jan 2026 17:03:02 +0800 Message-Id: <20260123090304.32286-1-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Lai Jiangshan Commit ecc5589f19a5 ("KVM: MMU: optimize set_spte for page sync") added a writable permission check on the old SPTE to avoid unnecessary calls to mmu_try_to_unsync_pages() when syncing SPTEs. Later, commit e6722d9211b2 ("KVM: x86/mmu: Reduce the update to the spte in FNAME(sync_spte)") indirectly achieves it by avoiding some SPTE updates altogether, which makes the writable permission check in make_spte() much less useful. Remove the old-SPTE writable permission check from make_spte() to simplify the code. This may cause mmu_try_to_unsync_pages() to be called in a few additional cases not covered by commit e6722d9211b2, such as when the guest toggles the execute bit, which is expected to be rare. Signed-off-by: Lai Jiangshan --- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/spte.c | 12 ++---------- arch/x86/kvm/mmu/spte.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- 5 files changed, 6 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 02c450686b4a..4535d2836004 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3073,7 +3073,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot, was_rmapped = 1; } - wrprot = make_spte(vcpu, sp, slot, pte_access, gfn, pfn, *sptep, prefetch, + wrprot = make_spte(vcpu, sp, slot, pte_access, gfn, pfn, prefetch, false, host_writable, &spte); if (*sptep == spte) { diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index 901cd2bd40b8..95fccee63563 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -954,7 +954,7 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int host_writable = spte & shadow_host_writable_mask; slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); make_spte(vcpu, sp, slot, pte_access, gfn, - spte_to_pfn(spte), spte, true, true, + spte_to_pfn(spte), true, true, host_writable, &spte); /* diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index 85a0473809b0..a8e2606ccd22 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -186,7 +186,7 @@ bool spte_needs_atomic_update(u64 spte) bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, const struct kvm_memory_slot *slot, unsigned int pte_access, gfn_t gfn, kvm_pfn_t pfn, - u64 old_spte, bool prefetch, bool synchronizing, + bool prefetch, bool synchronizing, bool host_writable, u64 *new_spte) { int level = sp->role.level; @@ -258,16 +258,8 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, * SPTE. Write-protect the SPTE if the page can't be unsync'd, * e.g. it's write-tracked (upper-level SPs) or has one or more * shadow pages and unsync'ing pages is not allowed. - * - * When overwriting an existing leaf SPTE, and the old SPTE was - * writable, skip trying to unsync shadow pages as any relevant - * shadow pages must already be unsync, i.e. the hash lookup is - * unnecessary (and expensive). Note, this relies on KVM not - * changing PFNs without first zapping the old SPTE, which is - * guaranteed by both the shadow MMU and the TDP MMU. */ - if ((!is_last_spte(old_spte, level) || !is_writable_pte(old_spte)) && - mmu_try_to_unsync_pages(vcpu->kvm, slot, gfn, synchronizing, prefetch)) + if (mmu_try_to_unsync_pages(vcpu->kvm, slot, gfn, synchronizing, prefetch)) wrprot = true; else spte |= PT_WRITABLE_MASK | shadow_mmu_writable_mask | diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h index 91ce29fd6f1b..cf9cd27bcd4f 100644 --- a/arch/x86/kvm/mmu/spte.h +++ b/arch/x86/kvm/mmu/spte.h @@ -543,7 +543,7 @@ bool spte_needs_atomic_update(u64 spte); bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, const struct kvm_memory_slot *slot, unsigned int pte_access, gfn_t gfn, kvm_pfn_t pfn, - u64 old_spte, bool prefetch, bool synchronizing, + bool prefetch, bool synchronizing, bool host_writable, u64 *new_spte); u64 make_small_spte(struct kvm *kvm, u64 huge_spte, union kvm_mmu_page_role role, int index); diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 9c26038f6b77..8dfaab2a4fd9 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1188,7 +1188,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, new_spte = make_mmio_spte(vcpu, iter->gfn, ACC_ALL); else wrprot = make_spte(vcpu, sp, fault->slot, ACC_ALL, iter->gfn, - fault->pfn, iter->old_spte, fault->prefetch, + fault->pfn, fault->prefetch, false, fault->map_writable, &new_spte); if (new_spte == iter->old_spte) -- 2.19.1.6.gb485710b