LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: sashiko-reviews@lists.linux.dev
Cc: kvm@vger.kernel.org, Gautam Menghani <gautam@linux.ibm.com>,
	 Madhavan Srinivasan <maddy@linux.ibm.com>,
	Nicholas Piggin <npiggin@gmail.com>,
	linuxppc-dev@lists.ozlabs.org
Subject: Re: [PATCH v4 12/23] KVM: Destroy memslots immediately after mmu_notifiers are unregistered
Date: Mon, 2 Nov 2026 07:33:45 -0800	[thread overview]
Message-ID: <auitWWkOopYYbBH3@google.com> (raw)
In-Reply-To: <sashiko-outbox-191627@kernel.org>

+Gautam and PPC lists/maintainers

On Fri, Oct 30, 2026, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Medium] KVM: PPC: Book3S HV: rmap_nested memory leak in kvmhv_release_all_nested()
> --
> 
> commit 9c25a1b4a76e8526eb8a2b11e1b9e63893b0de80
> Author: Sean Christopherson <seanjc@google.com>
> 
> KVM: Destroy memslots immediately after mmu_notifiers are unregistered
> 
> This commit replaces active memslots with an empty memslot set and frees all
> memslots immediately after unregistering the MMU notifier in kvm_destroy_vm(),
> prior to calling kvm_arch_destroy_vm().
> 
> > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > index fa548b12228ff..2eda12a52bf4b 100644
> > --- a/virt/kvm/kvm_main.c
> > +++ b/virt/kvm/kvm_main.c
> 
> [ ... ]
> 
> > @@ -1310,12 +1329,10 @@ static void kvm_destroy_vm(struct kvm *kvm)
> >  		kvm->mn_active_invalidate_count = 0;
> >  	else
> >  		WARN_ON(kvm->mmu_invalidate_in_progress);
> > +	kvm_destroy_memslots(kvm);
> > +
> >  	kvm_arch_destroy_vm(kvm);
> >  	kvm_destroy_devices(kvm);
> > -	for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
> > -		kvm_free_memslots(kvm, &kvm->__memslots[i][0]);
> > -		kvm_free_memslots(kvm, &kvm->__memslots[i][1]);
> > -	}
> 
> [Severity: Medium]
> Does hoisting kvm_destroy_memslots() above kvm_arch_destroy_vm() leak
> struct rmap_nested allocations in slot->arch.rmap on PowerPC Book3S HV when
> destroying a VM with nested virtualization enabled?

I don't think so?  Patch 3[*], "KVM: PPC: Flush/zap all memslots on kvm_arch_flush_shadow_all()",
should free the rmaps in response to kvm_arch_flush_shadow_all().   From what I
can tell, the page tables have to be in radix mode, and so the rmaps are freed via:

  kvmppc_radix_flush_memslot()
  |
  -> kvmppc_unmap_pte()
     |
     -> kvmhv_remove_nest_rmap_range()
        |
        -> kvmhv_remove_nest_rmap_list()

And if that doesn't work, it means memslot deletion on an active VM is completely
broken.

However, it means the memslot code in kvmhv_release_all_nested() is dead code, so
this as follow-up to patch 3?  Or maybe folded in?

diff --git a/arch/powerpc/kvm/book3s_hv_nested.c b/arch/powerpc/kvm/book3s_hv_nested.c
index a6ff42d7666c..27985210f972 100644
--- a/arch/powerpc/kvm/book3s_hv_nested.c
+++ b/arch/powerpc/kvm/book3s_hv_nested.c
@@ -802,8 +802,6 @@ void kvmhv_release_all_nested(struct kvm *kvm)
 	int lpid;
 	struct kvm_nested_guest *gp;
 	struct kvm_nested_guest *freelist = NULL;
-	struct kvm_memory_slot *memslot;
-	int srcu_idx, bkt;
 
 	spin_lock(&kvm->mmu_lock);
 	idr_for_each_entry(&kvm->arch.kvm_nested_guest_idr, gp, lpid) {
@@ -820,11 +818,6 @@ void kvmhv_release_all_nested(struct kvm *kvm)
 		freelist = gp->next;
 		kvmhv_release_nested(gp);
 	}
-
-	srcu_idx = srcu_read_lock(&kvm->srcu);
-	kvm_for_each_memslot(memslot, bkt, kvm_memslots(kvm))
-		kvmhv_free_memslot_nest_rmap(memslot);
-	srcu_read_unlock(&kvm->srcu, srcu_idx);
 }
 
 /* caller must hold gp->tlb_lock */
			

[*] https://lore.kernel.org/all/20261030212112.2598640-4-seanjc@google.com

> In virt/kvm/kvm_main.c, kvm_destroy_memslots() replaces kvm->memslots[i]
> with kvm_empty_memslots and frees each memslot via
> kvm_free_memslot() -> kvm_arch_free_memslot() -> kvmppc_core_free_memslot_hv()
> in arch/powerpc/kvm/book3s_hv.c:
> 
>     static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *slot)
>     {
>             vfree(slot->arch.rmap);
>             slot->arch.rmap = NULL;
>     }
> 
> which frees the slot->arch.rmap array without freeing the linked
> struct rmap_nested entries.
> 
> Previously, those entries were freed before memslot destruction via
> kvm_arch_destroy_vm() -> kvmppc_core_destroy_vm_hv() ->
> kvmhv_release_all_nested() in arch/powerpc/kvm/book3s_hv_nested.c:
> 
>     srcu_idx = srcu_read_lock(&kvm->srcu);
>     kvm_for_each_memslot(memslot, bkt, kvm_memslots(kvm))
>             kvmhv_free_memslot_nest_rmap(memslot);
>     srcu_read_unlock(&kvm->srcu, srcu_idx);
> 
> With kvm_destroy_memslots() running before kvm_arch_destroy_vm(),
> kvm_memslots(kvm) now returns kvm_empty_memslots and the memslots have
> already been freed when kvmhv_release_all_nested() runs, so
> kvmhv_free_memslot_nest_rmap() is never called. Any struct rmap_nested
> entries remaining in slot->arch.rmap at teardown (for example, when
> KVMPPC_SECURE_INIT_DONE causes kvmppc_radix_flush_memslot() to return early,
> or when 4KB PTEs were replaced by a huge page in kvmppc_unmap_free_pte())
> will be leaked.
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20261030212112.2598640-1-seanjc@google.com?part=12
> 


  parent reply	other threads:[~2026-11-02 15:33 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-30 21:20 [PATCH v4 00/23] KVM: Fix+harden against bad uaccess using dying VM Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 01/23] KVM: Reject user accesses to guest memory if current->mm != kvm->mm Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 02/23] KVM: nVMX: Don't force a full nested VM-Exit when destroying a vCPU Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 03/23] KVM: PPC: Flush/zap all memslots on kvm_arch_flush_shadow_all() Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 04/23] KVM: x86: Drop dead check on an existing memslot not having any pages Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 05/23] KVM: WARN and error out if memslot update encounters existing slot with no pages Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 06/23] KVM: x86: Unmap VMAs for KVM-internal memslots when the memslot is freed Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 07/23] KVM: x86: Delete non-SMM internal memslot if creating SMM memslot fails Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 08/23] KVM: x86: Process all address spaces when deleting internal memslot Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 09/23] KVM: x86: Re-queue APICv update request if inhibiting APIC access page fails Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 10/23] KVM: x86: Map a separate VMA for each address space for internal memslots Sean Christopherson
2026-10-30 21:20 ` [PATCH v4 11/23] KVM: Disallow setting memslots when the VM is being destroyed Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 12/23] KVM: Destroy memslots immediately after mmu_notifiers are unregistered Sean Christopherson
     [not found]   ` <sashiko-outbox-191627@kernel.org>
2026-11-02 15:33     ` Sean Christopherson [this message]
2026-11-02 16:08       ` Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 13/23] KVM: WARN if KVM attempts to do guest-related uaccess with "wrong" process Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 14/23] KVM: WARN and reject guest-based uaccess if VM is dying Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 15/23] KVM: Pre-check uaccesses in KVM's APIs to read/write guest memory Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 16/23] KVM: VMX: Explicility free vmcs01, not the current VMCS, in vCPU free paths Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 17/23] KVM: nVMX: Don't explicitly VMCLEAR+free() vmcs01's shadow VMCS when freeing vCPU Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 18/23] KVM: nVMX: Cancel the nested VMX Preemption Timer's hrtimer in free_nested() Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 19/23] KVM: Add lockdep-only APIs to query and assert that a vCPU is unreachable Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 20/23] KVM: VMX: Explicitly remove dying vCPUs from the PI wakeup list Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 21/23] KVM: nVMX: Don't switch to vmcs01 when freeing a vCPU Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 22/23] KVM: nVMX: Don't load+put the vCPU when freeing nested state for a dying vCPU Sean Christopherson
2026-10-30 21:21 ` [PATCH v4 23/23] KVM: x86: Rename __x86_set_memory_region() => x86_set_internal_memslot() Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=auitWWkOopYYbBH3@google.com \
    --to=seanjc@google.com \
    --cc=gautam@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=npiggin@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox