Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407180; b=0xrPeyCTVM+K4/NkLyQH0hPl/LLMDJ6Q2loT7TXeE4/0JBTkYzuilujVfgexSEvqVnxb 4/nGbaiO1rTrI9Du055ZUNkZXOhrj/Tuxj+SES8r5GxTWTnHjHPfBPFRo+gLzcax9eifz J0Cr058Y65aU+kZDMtrCSu79UOqVTL+ywWyc8igFcjDegPSk9US0M+X3a+khtXjKN/PqR bhXTRJ/tpiE1Lk1SuiuVmw3Sm0Oa50rja746xxrnRGJd3DhPNRv9ckxbfsCrEMmuEdWj8 huaKk2MIh+jH1dWxJuu2GA5nWsBvmc9HeJ7h8d/Ta/85WOB1qaU2jfLUNmxfx4azu2Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407180; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Scanned-By; bh=PlC74ktF8iO2gWkKM2FnYeWAkOU2donVcPXqUJFf3EE=; b=rhMfBgZpyInrHEeKSDq0cLNwi01seazycyQ5N7tQz9TfncvgastHHkpx8pKPQ9yAJzQf 6IEotiCXOGTGhwXn0ROmdWU3z9XDBxKMj7f2I6tznS8zqgS1Y4eWVQx2ZAUEQI9eZbPa6 p4ycZkTPhQ4y1k8Hprom3VPo3w4U7UfC3BDWkhKqRHAmDzrp6pSh/cq0pXCUewmz/9NAV GWFWlpOFyJB718N95NVBF5QIwex1xBPSjlPGoxk+FUXfdB/Y9lviTIxoEcmpyn+xGzJFf EAepAvF2XytqQAtRzE0NmZhuDAcgWx00CnEIrL6AZfjwzAdALqaUS0Rks6eBnBw1vsQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Z96In1Cc; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28975606A68A for ; Fri, 4 Dec 2026 17:59:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 28975606A68A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Z96In1Cc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407180; 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=PlC74ktF8iO2gWkKM2FnYeWAkOU2donVcPXqUJFf3EE=; b=Z96In1CcT7c1CGOyPqlpRXhvGAJ3UIhP2y+4xUoWkbZW+D6sCLWPPLehCPStEInzJem6MH NaILwjnZzxFZHlQj9TxMkrBJX5FKmhqvznfGKcMTEFLK1r8OIa/Mi3Bc0pwmcdIqzRm5NG W6XBXdj2IbwYf6URLt2e+sDXDSHPTw4= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-139-yGB6AAOPPBOqrvubjv7B5g-1; Fri, 4 Dec 2026 17:59:36 +0000 X-MC-Unique: yGB6AAOPPBOqrvubjv7B5g-1 X-Mimecast-MFC-AGG-ID: yGB6AAOPPBOqrvubjv7B5g_1796407175 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 63D0119670CD; Fri, 4 Dec 2026 17:59:35 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B52F8BB9; Fri, 4 Dec 2026 17:59:33 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , stable@vger.kernel.org, Chris Mason , Stefan Teodorescu Subject: [PATCH 01/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Date: Fri, 4 Dec 2026 18:59:14 +0100 Message-ID: <20261204175930.2154542-2-pbonzini@redhat.com> In-Reply-To: <20261204175930.2154542-1-pbonzini@redhat.com> References: <20261204175930.2154542-1-pbonzini@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 From: Sean Christopherson Queue a TLB flush when eagerly splitting a huge SPTE and adding a child SPTE to the rmap fails due to hitting the recycle threshold. If adding to the rmap fails, __rmap_add() zaps the entire list of rmaps, including the recently-added child SPTE. Eliding the flush in that case is unsafe as the huge SPTE is still PRESENT, after the TLB flush performed by __rmap_add(), i.e. can be (re)cached by running vCPUs. Push the TLB flush out from __rmap_add() to its two callers by returning a bool to indicate that a TLB flush is needed. Return "TLB flush needed" instead of success/failuire so that a feature cleanup can simply return the result of kvm_zap_all_rmap_sptes() (which *should* always be true; if there isn't at least one rmap entry, then things have really gone sideways). Fixes: 0378739401cf ("KVM: x86/mmu: Avoid unnecessary flush on eager page split") Cc: stable@vger.kernel.org Reported-by: Chris Mason Reported-by: Stefan Teodorescu Signed-off-by: Sean Christopherson Link: https://patch.msgid.link/20261113155010.442816-2-seanjc@google.com Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index b122061b1fab..1fae6743aacd 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1736,7 +1736,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) #define RMAP_RECYCLE_THRESHOLD 1000 -static void __rmap_add(struct kvm *kvm, +static bool __rmap_add(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, const struct kvm_memory_slot *slot, u64 *spte, gfn_t gfn, unsigned int access) @@ -1756,8 +1756,9 @@ static void __rmap_add(struct kvm *kvm, kvm->stat.max_mmu_rmap_size = rmap_count; if (rmap_count > RMAP_RECYCLE_THRESHOLD) { kvm_zap_all_rmap_sptes(kvm, rmap_head); - kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level); + return true; } + return false; } static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot, @@ -1765,7 +1766,9 @@ static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot, { struct kvm_mmu_memory_cache *cache = &vcpu->arch.mmu_pte_list_desc_cache; - __rmap_add(vcpu->kvm, cache, slot, spte, gfn, access); + if (__rmap_add(vcpu->kvm, cache, slot, spte, gfn, access)) + kvm_flush_remote_tlbs_gfn(vcpu->kvm, gfn, + sptep_to_sp(spte)->role.level); } static bool kvm_rmap_age_gfn_range(struct kvm *kvm, @@ -7372,7 +7375,7 @@ static void shadow_mmu_split_huge_page(struct kvm *kvm, * * However, if a given SPTE points to a lower level page table, * that lower level page table may only be partially populated. - * Installing such SPTEs would effectively unmap a potion of the + * Installing such SPTEs would effectively unmap a portion of the * huge page. Unmapping guest memory always requires a TLB flush * since a subsequent operation on the unmapped regions would * fail to detect the need to flush. @@ -7384,7 +7387,15 @@ static void shadow_mmu_split_huge_page(struct kvm *kvm, spte = make_small_spte(kvm, huge_spte, sp->role, index); mmu_spte_set(sptep, spte); - __rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access); + + /* + * Queue a TLB flush if adding the SPTE to the rmap zapped the + * child SPTE that was just added. In that case, replacing the + * huge SPTE with a shadow page will effectively unmap guest + * memory (see above), as the huge SPTE is still PRESENT and + * can be (re)cached in the TLB before it is replaced. + */ + flush |= __rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access); } __link_shadow_page(kvm, cache, huge_sptep, sp, flush); -- 2.56.1