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=1796407441; b=BIZ1SuygE0DeGMrtJNrWC1GrmbhrPJOWFZBFaH4knDKae9KaDHB8Dtowq3JTK+nUY3YC b29+iu99Q2spemOcjbD+DxMVb5CzJwFyP3zJvHXRth0LwROkpngmkisCxH0QSTR+t8+xK emVThcM7U0GHUxODlNvtQPNg7imglAVgMIRtymH7r7E3FDtKJV4kl3/vTZ/R/w3I6NIKC yklXziUCEy5HxL8wM5oMHFIz0yC69xzwXbYlX+dHAeFVnOnAfFfeMoD7ACjo2GEkLrGXm 6pmQdmKiCixtB7rdBUV0LshrgKeiZ3FpIxvg8+NE49G04ddlNeld/TmGUoFDcP6ug4g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407441; 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=3I6SSqnNGzbhUahMl3dHyw5e0p1jjCYsz7lsg4nt4uE=; b=HGw5VPZc8Wr2szH42/f+fWL6H60qH+DDbd7r6X+ltcCeA7gcn1PRR9fcDVqRgtq7o3Qt D+RmnMjJ/ZDKhab4c+UGUPPgHUPfqW+7qL0tP6yBaR/1AEE1uZo936SMU5y7wUOkBNb8F STtLZH9mET+PDLagMUnEDVP4PsszQJBt7Xj35BaQuHrzTp2Ru5h/GGmtB2rb7EvRTYvYr HsZ6kBaJoQFSGZZQkKm7ft6qivrCOCd+CkfroZB3QdzKM+Psi7qVPST0kLtQDMH4mcwbO D023k42jYaYlOlrncgA1Vy03E7Gx+Z+LhXSy+AsCv/X9k0dqeRFtkzkpLGFVnA86V2g== 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="AyT+OKH/"; 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 3CB4B606CBF2 for ; Fri, 4 Dec 2026 18:04:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3CB4B606CBF2 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=AyT+OKH/ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407441; 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=3I6SSqnNGzbhUahMl3dHyw5e0p1jjCYsz7lsg4nt4uE=; b=AyT+OKH/uoVEuWujkIhbaGCTqZ/B1kDsGw5TO/E1dc3Jk4cHKntb+mbdqtZ0ArMPcJN25q XptAeALzwbORdzY0BjweCCnOdUCZaydGw4mVTU7K9wIGLUoCcdLTxnYTml6ngPDl+sVy60 3bwAdXI/7QzvYYAoXle90XwQKmppEZ8= Received: from mx-prod-mc-05.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-440-0XPo6YoAMDSkzAFiKXlKpA-1; Fri, 4 Dec 2026 18:03:55 +0000 X-MC-Unique: 0XPo6YoAMDSkzAFiKXlKpA-1 X-Mimecast-MFC-AGG-ID: 0XPo6YoAMDSkzAFiKXlKpA_1796407434 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 332991909047; Fri, 4 Dec 2026 18:03:54 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 36B7C18019CC; Fri, 4 Dec 2026 18:03:52 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , stable@vger.kernel.org, Kevin Liu , Chris Mason , Kyle Zeng Subject: [PATCH 6.6.y 07/17] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Date: Fri, 4 Dec 2026 19:03:27 +0100 Message-ID: <20261204180337.2168031-8-pbonzini@redhat.com> In-Reply-To: <20261204180337.2168031-1-pbonzini@redhat.com> References: <20261204180337.2168031-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.93 From: Sean Christopherson [ Upstream commit 27e2443f180a769a6e58a8f3821cacd01325bfc4 ] Immediately flush TLBs if a flush is pending after unmapping a GFN range, *before* returning to the caller, if the overall unmap operation may block, i.e. if a *future* call to kvm_unmap_gfn_range() may yield and drop mmu_lock. Because kvm_unmap_gfn_range() starts with "flush = false", KVM incorrectly loses track of the pending flush if an overall unmap operation straddles multiple memslots. KVM will still service the TLB flush before the *caller* completes, but KVM relies on its mappings being completely unreachable prior to dropping mmu_lock. E.g. if a MMU notifier invalidation event runs after mmu_lock is dropped, but before TLBs are flushed, then the MMU notifier task can observe the missing PTE and return without issuing its own TLB flush, resulting in a use-after-free (if the to-be-invalidated page was freed or moved). Hack a fix into x86, but leave a TODO behind to undo the hack, or at least make it less awful. By dumb luck, x86 is the only arch that conditionally yields *and* utilizes the common TLB flushing mechanism. Fixes: e1eed5847b09 ("KVM: x86/mmu: Allow yielding during MMU notifier unmap/zap, if possible") Cc: stable@vger.kernel.org Reported-by: Kevin Liu Reported-by: Chris Mason Cc: Kyle Zeng Signed-off-by: Sean Christopherson Link: https://patch.msgid.link/20261119172647.64295-5-seanjc@google.com Signed-off-by: Paolo Bonzini Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index b0ed5c2f1086..5a012d758c1e 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1576,6 +1576,21 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) range->slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT) kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD); + /* + * Immediately flush TLBs if a *future* invocation of the current unmap + * operation may block, in which case the pending flush will be dropped + * due to starting with "flush = false" above. + * + * TODO: Plumb @flush around common KVM so that pending flushes can be + * processed if and only if KVM actually yields, and so that this + * code doesn't rely on may_block being constant for the entirety + * of an unmap operation. + */ + if (flush && range->may_block) { + kvm_flush_remote_tlbs(kvm); + return false; + } + return flush; } -- 2.56.1