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=1796407276; b=wmImDGC6NJp4JHKRnPC1m5pTDjXuaAlgILUM7v9FeAEEa4alwCI6mc8t5j0tEwZ2gkGk 9e+WFHknb3dKnxCgYxdCGC8Xeja0YatLnYQ68CkKgqSqCwpGmV36BdDlfOUjshK1HnYS0 5fRjrox5h/3YRRAbs3HM7G+lHLCHeRx7c467EjCdKLh9oEdAS9HakawnhHUDzU9CxErUn YTEY7CbchRc2qT7ZVTZTUyrDRA4CyqIVJAJ+nAgOzWWJRaksv3QpbUtx9EA/Dpwjmxp3f c8o8dsoRZxeLGvv2TCnukXHxT+kkeMibOvcnzr41RsEgHmvBdTgvco0apcz3YtzaD8g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407276; 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=YuUPp3fbT9K6yehT4zQzkhNRBAr4kzyp0Yhyi7R33DA=; b=R1DNZ33k1XWaqQkxS2jc3CobVGZ3XnZd5RvkzlnS3WZjNtRlgnG23SLxYB5ZNUl/EbWU vTvUjxkQTgDoXRV7n/TQfr/ZFsmW9XgNe2LmztE3nMie7AhIVAdMiVssKNYHzHlGA9ubw lJw8H5dQm5xy1yVKU9+I8nAkoSXo6bXivt3HkkDuV5bw2SgeQbtqjeSr8N5fflRjlHLs/ JdwKey4yWJNOdU61bhHryiRB6zhPpY9lLjOPSC3uCshA4ZRag/jL//TayJ6f3B4MtX0z1 FcESo5ZsLAWJZY1ZdBHQAZPnSvlpWaFxv2egxJ5alyNZxQilaStQFNU2/3io+9++o7g== 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="MARS1TK/"; 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 121DC606A682 for ; Fri, 4 Dec 2026 18:01:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 121DC606A682 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=MARS1TK/ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407276; 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=YuUPp3fbT9K6yehT4zQzkhNRBAr4kzyp0Yhyi7R33DA=; b=MARS1TK/+oprfGmFhIMwsSt1dFDyhs4cjuTSGqDFHiAxiSGAiAbhqtERlLjarOukZX9TYT 6lq4YwI3cDZi9yZ4zkhOtTLhU4CPfZi8RXcXN6JVYUazBS4rkRvi5lPVw4u+zhxvlnbOt1 zqvnev1swPUHWNYiVTNuJnbRtdwMulU= 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-248-zqWBBzgnP7O0xyNguQyeJA-1; Fri, 4 Dec 2026 18:01:09 +0000 X-MC-Unique: zqWBBzgnP7O0xyNguQyeJA-1 X-Mimecast-MFC-AGG-ID: zqWBBzgnP7O0xyNguQyeJA_1796407268 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 8B53718C9F03; Fri, 4 Dec 2026 18:01:05 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D46071955E83; Fri, 4 Dec 2026 18:01:03 +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.18.y 02/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Date: Fri, 4 Dec 2026 19:00:44 +0100 Message-ID: <20261204180059.2165490-3-pbonzini@redhat.com> In-Reply-To: <20261204180059.2165490-1-pbonzini@redhat.com> References: <20261204180059.2165490-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.0 on 10.30.177.12 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 3b1fb5696ed6..66bec9ee99f6 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1670,7 +1670,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) @@ -1690,8 +1690,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, @@ -1699,7 +1700,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, @@ -7040,7 +7043,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. @@ -7052,7 +7055,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