Re: [RFC PATCH kernel] KVM: Stop leaking memory in debugfs

From: Greg KH
Date: Tue Aug 03 2021 - 07:16:29 EST


On Fri, Jul 30, 2021 at 02:32:17PM +1000, Alexey Kardashevskiy wrote:
> The debugfs folder name is made of a supposedly unique pair of
> the process pid and a VM fd. However it is possible to get a race here
> which manifests in these messages:
>
> [ 471.846235] debugfs: Directory '20245-4' with parent 'kvm' already present!
>
> debugfs_create_dir() returns an error which is handled correctly
> everywhere except kvm_create_vm_debugfs() where the code allocates
> stat data structs and overwrites the older values regardless.
>
> Spotted by syzkaller. This slow memory leak produces way too many
> OOM reports.
>
> Signed-off-by: Alexey Kardashevskiy <aik@xxxxxxxxx>
> ---
>
> I am pretty sure we better fix the race but I am not quite sure what
> lock is appropriate here, ideas?
>
> ---
> virt/kvm/kvm_main.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 986959833d70..89496fd8127a 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -904,6 +904,10 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
>
> snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd);
> kvm->debugfs_dentry = debugfs_create_dir(dir_name, kvm_debugfs_dir);
> + if (IS_ERR_OR_NULL(kvm->debugfs_dentry)) {
> + pr_err("Failed to create %s\n", dir_name);
> + return 0;
> + }

It should not matter if you fail a debugfs call at all.

If there is a larger race at work here, please fix that root cause, do
not paper over it by attempting to have debugfs catch the issue for you.

thanks,

greg k-h