From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97848423A66 for ; Fri, 29 May 2026 18:36:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780079796; cv=none; b=YbBUEH1cefZWt7DqfPKzgL+iHYIqajS0rtpzDhydiWQYDYCyRqj0o9keVK+5yjd/pHPNOu68H7hAR+f67wXbXA7WYTIyGiEOULBjWHpNKSqlR2+5QmRI5lFMazl0ZolN8pzMsJ60oSG2gpZ5ffXdPZCaH4I1qORYTRmKLEy08XI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780079796; c=relaxed/simple; bh=ulXu0NlgwaVVwx3zWk7XuRF/3f9bu/79QRsDBDGWEjo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JZ0C+COyBPFLC+eGccxE9pX9LXvxbjdP++h+oWhTYJ/pL+El2g+KcHF+mYudH4OTKVxyEzys8R4d8MW6FkChrBlYV9Emla6V89N/iFTlfDcdS11dA01aO0woKMpnr6L7G+zyaaTHAcgvRPLxlWp/cNKcDubrhIDL2uzk5SZbmYA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=hpMYdvI1; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=XT41YxNM; arc=none smtp.client-ip=170.10.133.124 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.b="hpMYdvI1"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="XT41YxNM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780079790; 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=+/A1VY1s6jcVoa3c05BPgiOhjQWKntgd+9grtxQ/UaE=; b=hpMYdvI1rd1LAhmvjOV0+PMxQukzbBtd1AYuQ1fu+C9k4vjLsX8tZcMSKSAHTKqIwX6xJl Z9uEa1WrNKJZ6lvpTEiSL1F2SkyaxJ/FgJfouR+dlhAZ2zX3ozRc4PAalh2sIRW/UQ0ugc vR7x/vkrUyuI6epBqrCPwIaSvkwSyIA= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-466-8ahqLvo-PNGhtBplIYqjdw-1; Fri, 29 May 2026 14:36:29 -0400 X-MC-Unique: 8ahqLvo-PNGhtBplIYqjdw-1 X-Mimecast-MFC-AGG-ID: 8ahqLvo-PNGhtBplIYqjdw_1780079788 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-45e73a4f1dfso10095376f8f.0 for ; Fri, 29 May 2026 11:36:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1780079788; x=1780684588; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+/A1VY1s6jcVoa3c05BPgiOhjQWKntgd+9grtxQ/UaE=; b=XT41YxNMUCIAu7hgtAXY7IT66gfTWDAbjEbeSqaIADMqFcV08Awt5YUyfl8apmCNZA wRcGK6jQiAWeGH9XC3bGYwgDzWaHumTivIvjASA/C9X8ebEWBlaErnszrHGn3Ukp0vSu qMEEA3tTVnJ3+fWnRr4gTg4ysDVUl3bu2OdsuLCkefIYUZj6q95mkTLqK+0FfX2UoDMX YS7vkR8Pt/O0jbJ0K9bNTkE+hKn9Siel7/ggsrvnrdSh6tn1bEEUxAf7M+Lx6eNQqHGz nxRI0Y6ZwPXIhAcJYV/G9uH0cBQ1byEttzdw1AG1DQbVxBWEB99kmo+1UkOX+8ALxM5T m5OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780079788; x=1780684588; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=+/A1VY1s6jcVoa3c05BPgiOhjQWKntgd+9grtxQ/UaE=; b=VTcgsfIKaFnbOz6JxV9uOAiGUeL2LUaWe8HWJWo8pGT3ii0Qeq+UVCDxZ3ekcoxWZV ja2KUZs9tas5q8/IFhM2VIm4bM2gvC10UAkffHcLV6FMBXB0diStHuXjh9j9TmQJf1GM ulb3Qbop5aVxJHo6iJsnH3pzNOd4ksKBrbzxe9f8ju4FRfDtwGs65z68qV1zkbHS7tWG 0nmPtu58jIk5s0mYdPKMVmGCaB+13S7OmlikV4TISEmqCDw++WdR1RGEMsiH7Qzak8Js pC+AkndD7xtO+jJxGONMdjHjC0FPDpPWTA/+5A8M1QX+x7givjyQYYG8BdldalM/MGv9 Vmlw== X-Gm-Message-State: AOJu0Yz8oZ0NINUexkpO0HVJil0XDVD469QlGJ9WyNWCUen6ET2YlTHg i/Bjb/1rH6SkXvjbatpPWqZRAHb0Iu9QZouZB+UDRXZvz/aI+G0f2sBG2M9HhLL99F8voWHM7bc FyXP1yfJlEQnBwmO/VHAsaWH9FDZzpc1nVg4EO/Z4S00VZfO2O42k9MBKBcYVJ1Ulxe7hbbpnJY BwKIokT/m0RqKfC5tKfjXjlre2mkYbb4ROSrCFldmWOXZZJKCUSA== X-Gm-Gg: Acq92OHMmYE/klJnFDItOy7tvNCX4/u47C1NF2uhh32MhkrwSzSNJlVK1fXYvatMuyU 5vcLapdHAfLpS8Pc91hbiyW8g88HYTFFCOzJbQNg+zOhB1LBvSKdMrpJ6l+1ZRQmZ5bZ2MJeoQS WmNuHcLrgqVpigC2+q5OOZcn5MbiH+q2b9HAp6MtsAU6A7CLcuR4jmXD58EJWMgFPgl3YnliXBc ICGq/R3yTuBIeYFNMX/neIvdCoIfIR5/G9B2msH9ndM+Hmnw0K4Z4wGuC4vezHPlqerpdxV69cj 3TB21JFCZq/AZRQiOINykVwQJ7ziD63IS/pO5vPzzDwsYcxbINqaYzicKUDumnytQdWOM7/cN4H cfc0S+AMmI6TEXqJ6yIe1W8SVjzuFtaE9xpXWuiV9wppvPSK3ptTR7lZfmXmxaorw0mLxVz0HWj bK+LPcJanBoghtl+JbaOCy/Ppw7tWimvuo1bGkOA== X-Received: by 2002:a05:6000:1376:b0:45e:f52b:f4b7 with SMTP id ffacd0b85a97d-45ef6b20038mr1460696f8f.17.1780079787948; Fri, 29 May 2026 11:36:27 -0700 (PDT) X-Received: by 2002:a05:6000:1376:b0:45e:f52b:f4b7 with SMTP id ffacd0b85a97d-45ef6b20038mr1460639f8f.17.1780079787478; Fri, 29 May 2026 11:36:27 -0700 (PDT) Received: from [192.168.10.48] ([151.49.251.208]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45ef356b129sm6889251f8f.32.2026.05.29.11.36.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 11:36:26 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, Tom Lendacky , Michael Roth , stable@vger.kernel.org Subject: [PATCH 14/24] KVM: Don't WARN if memory is dirtied without a vCPU when the VM is dying Date: Fri, 29 May 2026 20:35:39 +0200 Message-ID: <20260529183549.1104619-15-pbonzini@redhat.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260529183549.1104619-1-pbonzini@redhat.com> References: <20260529183549.1104619-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sean Christopherson When marking a page dirty, complain about not having a running/loaded vCPU if and only if the VM is still alive, i.e. its refcount is non-zero. This will allow fixing a memory leak for x86 SEV-ES guests without hitting what is effectively a false positive on the WARN. For some SEV-ES VM-Exits, KVM keeps a writable mapping of a guest page across an exit to userspace, and typically unmaps the page on the next KVM_RUN. But if userspace never calls KVM_RUN after such an exit, then KVM needs to unmap the page when the vCPU is destroyed, which in turn triggers the WARN about not having a running vCPU. Alternatively, SEV-ES could temporarily load the vCPU to suppress the WARN, as is done in nested_vmx_free_vcpu() (but for completely unrelated reasons; suppressing WARN from nested_put_vmcs12_pages() is pure happenstance). But loading a vCPU during destruction is gross (ideally nVMX code would be cleaned up), risks complicating the SEV-ES code (KVM would need to ensure the temporarily load()+put() only runs when the vCPU isn't already loaded), and is ultimately pointless. The motivation for the WARN is to guard against KVM dirtying guest memory without pushing the corresponding GFN to the active vCPU's dirty ring, e.g. to ensure userspace doesn't miss a dirty page. But for the VM's refcount to reach zero, there can't be _any_ userspace mappings to the dirty ring, as mapping the dirty ring requires doing mmap() on the vCPU FD. I.e. if userspace had a valid mapping for the dirty ring, then the vCPU file and thus the owning VM would still be alive. And so since userspace can't possibly reach the dirty ring, whether or not KVM technically "misses" a push to the dirty ring is irrelevant. Reported-by: Michael Roth Cc: stable@vger.kernel.org Reviewed-by: Michael Roth Signed-off-by: Sean Christopherson Message-ID: <20260501202250.2115252-15-seanjc@google.com> Signed-off-by: Paolo Bonzini --- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 89489996fbc1..881f92d7a469 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3520,7 +3520,8 @@ void mark_page_dirty_in_slot(struct kvm *kvm, if (WARN_ON_ONCE(vcpu && vcpu->kvm != kvm)) return; - WARN_ON_ONCE(!vcpu && !kvm_arch_allow_write_without_running_vcpu(kvm)); + WARN_ON_ONCE(!vcpu && refcount_read(&kvm->users_count) && + !kvm_arch_allow_write_without_running_vcpu(kvm)); #endif if (memslot && kvm_slot_dirty_track_enabled(memslot)) { -- 2.54.0