Re: [PATCH] KVM: Add the check and free to avoid unknown errors.

From: Paolo Bonzini
Date: Mon Feb 17 2020 - 12:15:57 EST


On 14/02/20 22:02, Haiwei Li wrote:
> From: Haiwei Li <lihaiwei@xxxxxxxxxxx>
>
> If 'kvm_create_vm_debugfs()' fails in 'kzalloc(sizeof(*stat_data), ...)',
> 'kvm_destroy_vm_debugfs()' will be called by the final fput(file) in
> 'kvm_dev_ioctl_create_vm()'.

Can you explain better? It is okay to pass NULL to kfree.

Paolo

> Add the check and free to avoid unknown errors.
>
> Signed-off-by: Haiwei Li <lihaiwei@xxxxxxxxxxx>
> ---
> Âvirt/kvm/kvm_main.c | 5 ++++-
> Â1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 67ae2d5..18a32e1 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -617,8 +617,11 @@ static void kvm_destroy_vm_debugfs(struct kvm *kvm)
> ÂÂÂÂ debugfs_remove_recursive(kvm->debugfs_dentry);
>
> ÂÂÂÂ if (kvm->debugfs_stat_data) {
> -ÂÂÂÂÂÂÂ for (i = 0; i < kvm_debugfs_num_entries; i++)
> +ÂÂÂÂÂÂÂ for (i = 0; i < kvm_debugfs_num_entries; i++) {
> +ÂÂÂÂÂÂÂÂÂÂÂ if (!kvm->debugfs_stat_data[i])
> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
> ÂÂÂÂÂÂÂÂÂÂÂÂ kfree(kvm->debugfs_stat_data[i]);
> +ÂÂÂÂÂÂÂ }
> ÂÂÂÂÂÂÂÂ kfree(kvm->debugfs_stat_data);
> ÂÂÂÂ }
> Â}
> --
> 1.8.3.1
>