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=1796407197; b=vHNMHdTU94FD+szVFQmdKRd6+nFo/eYr6ifVEJpEpvs4UDooZ6c1toSHbzza3GuPs/n/ RoY+M/WDB/+JAj72iPH7TLJaUmtVbOUthTUhfTdjO8omp0Uo85BGfR+85qDq6ve56vc6T djriLc5aIQsTIrHYzJONzv90aVAJtEI5opzRdJp/obnSE2UlJio6z9HEfVjKoBlwWg7DW VkmFa9meFy8QJfGBTcdobCOtNgAbAnboReS25Ownc+w8FIbaHqTuZftyKgFQPDupczuxq pveJ3U4nGvLbuO+D/OoFzPUHQWBw5o3kIlRYq2/a08ETbK/PAhKh1fXWTokzRj9pvMQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407197; 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=2UKhDmBlGX8IvqcZvt4FCv2QykUKCF8lpsqMV+Ui43slWLNn3FMpUI/YIZqSxQZy2sZ8 qkqSh6KJX3Dtd5momEzha5nmyob0jUnbTv1pyLlJ3eL4Yxa1fwO6x43vsnwXktkD3+Iqz lB/iRHEx/tnXp6MlIn0uJnWBvBiBuxJV5+ndMGUeoZ71jYGTLiCXjmZMDa27GT2RDby6Y f1rdMbMB+359Xlx9Qut/0+fgsm0HfhJe1uoBeOTU+2OcfzivV50rf0t4NLPGf8qpv3IKX VFnXaaiFgMcCa7fKlRnKNCO1MiqRI48xzPdXEai68Kx6jAbWlqNgxYsxqkWpFhaz+Hg== 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=KTmwT6v9; 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 8297D606A32C for ; Fri, 4 Dec 2026 17:59:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8297D606A32C 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=KTmwT6v9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407196; 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=KTmwT6v9JqEjSz6PypvuLSAX3iQdh83x1dSN4G0tQvH1dgOO/eNc5EX5v0JHcNIOYo1bPG QzE97wCPKWOane3VX0vnn1DQIURtkV+WScwM9LPe0DYMIOXv9O7haoYiuArkBtzDk+TQBB g8HjbmX+cWIrQ0kUaoFEkrF07RLM/BU= 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: 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.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