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=1796407285; b=36UZ12+XNdgjtqTpJGqxZLmGH/o/NGNFmOEJy4o5lPmuJcO5ifTpkR9VEqoIh6Zczgz8 GCl5yNdvSHzXD4PLUkLHAgGOhBmSjpbm66R2OMmlZHEBgLyLOEbWxIE1jx/bNXfb56SWm YkCCqpftwWG7boP7f7tGIk33VUTCh/ykh7lkRNEfZt5oyRE8QRHRULy/sxbS3FtOD4qwh Y0L8jc8W8gjMKRoDtG7PaOJIiDV1rLser3kEFC3g9rbppdP2DpshSEjABjvpSTar0GG/d 6RreXHw+3tVmBD6IhvbX4wKrW4+fjzBF+l1oDhpL3Ii345nbgmQzFJQQrRDygNLHklA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407285; 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=SHQ2NIdGG82wmgJQzzjrsnsU/SmtNJ4cOZh1X5zIWwU=; b=X8YxONwIhqGk95VMSUaoMz/+GbtyI3vHZpB7SsCT9GhMBFZIXKWR5dLskFC43AsMaBSi VPd5MgjqA8kC/H7SJ8mQ5k8YtaH1apTRC+9o3QuUdemGOZaHyYHqFBjeSYdZZ01T9CCD2 pfqRKY4BMNsNT1yNRr+AlaQhzp/Bi2jDMmZJ5bcyALo6X8uXfV2K0ZkU6OIr7PhboYwFC 3ipBPFzDPTjKuCc+vdpPrqK4XyVFt4nA30ysi0JWEn7Jnggs3NSewxhAZRif8RioFN/9A hC5Vc017dfKjK7dKSJd/nTh9PyVsvgzbNZi959DtT/mwLGf6YVpsEtN2KZnOyt4r2HA== 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=iqWMWbpn; 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 E97C4606A693 for ; Fri, 4 Dec 2026 18:01:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E97C4606A693 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=iqWMWbpn DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407285; 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=SHQ2NIdGG82wmgJQzzjrsnsU/SmtNJ4cOZh1X5zIWwU=; b=iqWMWbpnb0ZWkwJ+PWw5sZzLr2ivacz9yiE4ukNHn5trCwWNyxZpAnQInumjGjHeRZ7l1V 2CNdihvTisDaaneZfkkZM2qtLE7z31Igtw/zpffcrUDDEA14m5385VfJY3ySqlsJ0whQJi hnf/1yM7CTfIE+AS5ti7OHmTwE1eXfg= 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-278-rvYet7jAOLm1b_H5wK7YOg-1; Fri, 4 Dec 2026 18:01:19 +0000 X-MC-Unique: rvYet7jAOLm1b_H5wK7YOg-1 X-Mimecast-MFC-AGG-ID: rvYet7jAOLm1b_H5wK7YOg_1796407277 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 D04C718C0D9F; Fri, 4 Dec 2026 18:01:17 +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 EB75A1955E80; Fri, 4 Dec 2026 18:01:15 +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.18.y 08/17] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Date: Fri, 4 Dec 2026 19:00:50 +0100 Message-ID: <20261204180059.2165490-9-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 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 c976f44f7c46..662178e48441 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1669,6 +1669,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