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=1796407194; b=BGUzXO5Am6YSr5hae9w31I8xWvh9ekgQ+ggLOZ9zkQrJbPy8NmoLwraPG+VnzfYGdsg0 o/zbiJh+jyRBmXX4d544KG79WGaxvI5QHi3fC+L413Ihg8NJgdg1qvDghmHA6ac3FnxcN ebWyPXfYFaEk+ucF8wHcbI5ygYYePVfQdsq369fxv6RbwJtLTG4jTTfCW2nspyr1iMUxO uhzHrrv+8uU75mQilKHCZVlctVYlsAK5Sq80wYKz7HjwiEv1+7LaCqC25H8TvUG4x6hPt xuMvFYmEJQnWspQTSEU+C3N6HyAmtciQQkpPtEQRV6JvQeWW3n60WeLPzlDPnty0deA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407194; 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=aZbR4xibe1N/BYmo/C2FUhOqJTvd73Hz/vl+oAArIXA=; b=nZa9PuzlR+7q7Gh6r9eQg16qU8hRlUz6kaUHIEAGVSvljXpEjpuwXOLFFjdvWY8u3QtM wRMvQi+uyNILV/kmUvgBgRmGeS6TFc5JHNCZnznW/1e8j/xtd6+jKc8k4yLIZpoN5q/1d OXWIz4VMjxhvMvZk6XhMqvw9jKPrU3lDg1zb4GQJi/YYnoUbZY4x2GbyYC+tXWynBb7X5 LsnBtfj+FJwd9ks3QqEBUlBtcWNzWXNzKXlu4nuBQKvKrpkhAuLuAm6IpaCLSz4kk7CdA pBPJM3J2I3dNEy1Usj8jUGCW+8LvWSTwNuu0kKu2XHe3sF1Fn/Rvb/v3ymPNtBNqOaA== 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=P093vMA+; 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 565EC606A333 for ; Fri, 4 Dec 2026 17:59:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 565EC606A333 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=P093vMA+ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407194; 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=aZbR4xibe1N/BYmo/C2FUhOqJTvd73Hz/vl+oAArIXA=; b=P093vMA+ckFWaLFyOfYKDcPQTKMtfrBv+7CTHGpdaxXr2EWs7THxDdDpyNWygDgIdb6hrf J116On2YInqu8EVDRazTSioqrlRMiw1NceOHwDLj7YbcyaEInNRe9OmD6au1bbHuc6DiTV qL7MHraJUxstqHvyGY72WGZP+zARKmI= Received: from mx-prod-mc-03.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-9-NvluYG0JPd-7pruUitFEvA-1; Fri, 4 Dec 2026 17:59:51 +0000 X-MC-Unique: NvluYG0JPd-7pruUitFEvA-1 X-Mimecast-MFC-AGG-ID: NvluYG0JPd-7pruUitFEvA_1796407190 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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E5D0D19670C5; Fri, 4 Dec 2026 17:59:49 +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 0D6F4BAC; Fri, 4 Dec 2026 17:59:47 +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 08/17] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Date: Fri, 4 Dec 2026 18:59:21 +0100 Message-ID: <20261204175930.2154542-9-pbonzini@redhat.com> In-Reply-To: <20261204175930.2154542-1-pbonzini@redhat.com> References: <20261204175930.2154542-1-pbonzini@redhat.com> X-Mailing-List: stable@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 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 --- 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 a891b45c6a56..842945fb18d2 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1735,6 +1735,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