Re: [PATCH v2 5/7] KVM: guest_memfd: Add cleanup interface for guest teardown

From: Ackerley Tng

Date: Mon Mar 09 2026 - 05:02:14 EST


Ashish Kalra <Ashish.Kalra@xxxxxxx> writes:

> From: Ashish Kalra <ashish.kalra@xxxxxxx>
>
> Introduce kvm_arch_gmem_cleanup() to perform architecture-specific
> cleanups when the last file descriptor for the guest_memfd inode is
> closed. This typically occurs during guest shutdown and termination
> and allows for final resource release.
>
> Signed-off-by: Ashish Kalra <ashish.kalra@xxxxxxx>
> ---
>
> [...snip...]
>
> diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> index 017d84a7adf3..2724dd1099f2 100644
> --- a/virt/kvm/guest_memfd.c
> +++ b/virt/kvm/guest_memfd.c
> @@ -955,6 +955,14 @@ static void kvm_gmem_destroy_inode(struct inode *inode)
>
> static void kvm_gmem_free_inode(struct inode *inode)
> {
> +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_CLEANUP
> + /*
> + * Finalize cleanup for the inode once the last guest_memfd
> + * reference is released. This usually occurs after guest
> + * termination.
> + */
> + kvm_arch_gmem_cleanup();
> +#endif

Folks have already talked about the performance implications of doing
the scan and rmpopt, I just want to call out that one VM could have more
than one associated guest_memfd too.

I think the cleanup function should be thought of as cleanup for the
inode (even if it doesn't take an inode pointer since it's not (yet)
required).

So, the gmem cleanup function should not handle deduplicating cleanup
requests, but the arch function should, if the cleanup needs
deduplicating.

Also, .free_inode() is called through RCU, so it could be called after
some delay. Could it be possible that .free_inode() ends up being called
way after the associated VM gets torn down, or after KVM the module gets
unloaded? Does rmpopt still work fine if KVM the module got unloaded?

IIUC the current kmem_cache_free(kvm_gmem_inode_cachep, GMEM_I(inode));
is fine because in kvm_gmem_exit(), there is a rcu_barrier() before
kmem_cache_destroy(kvm_gmem_inode_cachep);.

> kmem_cache_free(kvm_gmem_inode_cachep, GMEM_I(inode));
> }
>
> --
> 2.43.0