From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407378; b=cpYkLjJDS9qi0GBZ6xr1oSbhBpG63rRzjuRzKJRw8BlZTgWCNhaojk66pf+Iu2sTlrbw knzCuzlJhUO+b85nnU3tp946IwbV6nxu0OaJK/qmeMnlAT/aD6SAVJZD8Fer38eicAnmh vxKO74mJPRDLDbl5R9ZJCjwJP0S6abQUVQfMnh+hJlT3fVSzcqA71Hmpbfi+vpqkj75se g9k3aabdcSKhZdP/79zV8TwZHLO6qj7F2wFe852wlBEHc23yXwF12RmOEEtIrzRxxPZjQ asy0agFsU1kMzE1BVZgjUX/epaRb+2S9asHQO7mzJwk5laEtS8D2+dpIqA6DtFH2K2g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407378; 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=OMwKivXnjCGmKhS6BbD3xWH+c+E8pdnVEnkm8CVVscw=; b=aKrVXE1vopZ6z4KxZhULIj3QKKoSfk1AXbbTi0aHSeGbGMUGs+Y6zI6p1bKa+tY4kJhj W9LqngUxQXGRaLKn6gGCwO1uOPJQWGbA4+DMrk4afPa6wagb8vNkmPwH/qJORniv735Tu VVLEv6ZF3yGhN7FzdlZNe41oCH6+pIotpX+bU9ZqA5dAtlWI5BPaIA0JEESW73uDV7r2+ PLb0Mp0BCmOZu4Fy8FX0QOylAi+X46PScj3SpBUDUP9pptiG/Hay3nAY2z7c5DLREPRbE 5V/bgzk0SMmxI7n5z0fDe2vzcxT/3gWZHoofdpMrGIUCZC1qwQrIGv7Uk3VK1Mqntiw== 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=hIHUVFLH; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 0082B606A6A0 for ; Fri, 4 Dec 2026 18:02:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0082B606A6A0 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=hIHUVFLH DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407378; 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=OMwKivXnjCGmKhS6BbD3xWH+c+E8pdnVEnkm8CVVscw=; b=hIHUVFLHcVt0DkRvPRo4KBt3oS8Bd3nKm0hLCJBDFCWMsP2bwaAWYtkulQUvxa9IIz7uHV BmdrCJDDauLuUZigzBjqQvsPvHLLGHLjDialeUEb30WlXAupyK6vhty16VB4lawV5ClN8V gDkhKEDz6vG44OUlyfyzJhq+3Hl0S8Q= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-588-XFHdXa8qNaKKVNgMz5Ng9A-1; Fri, 4 Dec 2026 18:02:52 +0000 X-MC-Unique: XFHdXa8qNaKKVNgMz5Ng9A-1 X-Mimecast-MFC-AGG-ID: XFHdXa8qNaKKVNgMz5Ng9A_1796407371 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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 77FEC18BC480; Fri, 4 Dec 2026 18:02:51 +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 BD535BB9; Fri, 4 Dec 2026 18:02:49 +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 6.12.y 02/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Date: Fri, 4 Dec 2026 19:02:30 +0100 Message-ID: <20261204180245.2166967-3-pbonzini@redhat.com> In-Reply-To: <20261204180245.2166967-1-pbonzini@redhat.com> References: <20261204180245.2166967-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 [ Upstream commit d31322f7c0003c4b3cba9d44c85fd73aa877c9f9 ] 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 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 c4f33dc6dabc..0c2bbb9bd301 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1573,7 +1573,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) @@ -1593,8 +1593,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, @@ -1602,7 +1603,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, @@ -6828,7 +6831,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. @@ -6840,7 +6843,15 @@ static void shadow_mmu_split_huge_page(struct kvm *kvm, spte = make_huge_page_split_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