From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796407483; b=GaNG5AT86aIdoyweVlekqpgZJ2SxQCNeo2Y5LJFBEIN04mhD9tC8Z8J4kC2vBdetqZUM 5u/FzTlz8s+iq2h/eywG4KHeNftYtNogB50odQacFbFYvhvfPiMJYc0PwUJLBYPYmLCWi ckC0GGqbWDtPQKBc3vf97nldAy0iRZgxG79AB6P4PNuua4zEzP6uRyg7WKGcfxWIK7WhV jDeelEuB58qptNQuERSrtjFNdI8S7ImU3COgvY0J/nGj9GMoM8aVvbeDSRnQhWV/1uEKH c7CTi9sFHGWKDYcItFDVuD+uN++mg4rNCeI+rjJByqSWo9ZD3Ic4aGYxd+zGgkZZWiw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407483; 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=8yIc55uIEKewn5c4Vw0PTK7iMk62Hol8f9Ms7Ekh3Z4=; b=PxtH/uTptc8Cos8B7w+J1EuynLG0SHwyANIDsKRxAjRmItCicq5dg7tJ1P2OXNTYT9jf 79ho95dOqHp/KFrVmIqe7t06mghAa6AA0ARomx0vJUA2Ac/5x/SofffzKmpaR2haNNm03 oDuLWQsieByuWyvHOlPNzK9GfbjpE47DOLoHTcpATKnAcTkCIURqFEed9Swagvq2IpWoK ysg8zE/91aF8IglAXdfcpnVwpgc4aDlOPLFfgZPvgwyOlex2UTa/PpYH+3cx2LsTwuPrp mktd/NyABbWdm7RAUv6HwmXH1cIrzLPIQMB4C848qZTcDZP7nn9SBXcaycvdwIERiqA== 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=h+aTUWrc; 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 0A658606BB79 for ; Fri, 4 Dec 2026 18:04:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0A658606BB79 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=h+aTUWrc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407483; 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=8yIc55uIEKewn5c4Vw0PTK7iMk62Hol8f9Ms7Ekh3Z4=; b=h+aTUWrcyr6a/REwZq/6PL+Dhjw+IL02gJ+k0EaGTne/7QpZWV5rvW3+oVgViffjBjb6zg WK8XIN69JyTGwNunPrVWHE9JtrjS24mOudIcKWlRVNH2rb4RNso/mIG50CPhFYPeaaZabY S9r0OvD+1icT7kJb82t7KfuOFf1+V7o= Received: from mx-prod-mc-06.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-673-eN1kgKbqMwOJd0xoqBuaMg-1; Fri, 4 Dec 2026 18:04:38 +0000 X-MC-Unique: eN1kgKbqMwOJd0xoqBuaMg-1 X-Mimecast-MFC-AGG-ID: eN1kgKbqMwOJd0xoqBuaMg_1796407476 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C1852188CDAE; Fri, 4 Dec 2026 18:04:36 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1CBC73003EA7; Fri, 4 Dec 2026 18:04:34 +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.1.y 06/24] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Date: Fri, 4 Dec 2026 19:04:05 +0100 Message-ID: <20261204180423.2168641-7-pbonzini@redhat.com> In-Reply-To: <20261204180423.2168641-1-pbonzini@redhat.com> References: <20261204180423.2168641-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.4.1 on 10.30.177.4 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 a240e014a50e..1db7e30fca88 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1597,7 +1597,7 @@ static bool kvm_test_age_rmap(struct kvm *kvm, struct kvm_rmap_head *rmap_head, #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) @@ -1617,8 +1617,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, @@ -1626,7 +1627,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); } bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) @@ -6259,7 +6262,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. @@ -6271,7 +6274,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