LKML Archive on lore.kernel.org
 help / color / Atom feed
* Re: [PATCH] KVM: Add the check and free to avoid unknown errors.
@ 2020-02-15  2:00 linmiaohe
  2020-02-15  5:33 ` Haiwei Li
  0 siblings, 1 reply; 4+ messages in thread
From: linmiaohe @ 2020-02-15  2:00 UTC (permalink / raw)
  To: Haiwei Li; +Cc: pbonzini, kvm, linux-kernel

Hi:
Haiwei Li <lihaiwei.kernel@gmail.com> wrote:
> From: Haiwei Li <lihaiwei@tencent.com>
>
> 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()'.
>
> Add the check and free to avoid unknown errors.

Add the check and free? According to the code,it seem what you mean is "add the check against free" ?
 
>
> Signed-off-by: Haiwei Li <lihaiwei@tencent.com>
>
>   	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);
>   	}
>   }

If (!kvm->debugfs_stat_data[i]) is checked in kfree() internal. And break early seems have no different effect.
Could you please explain what unknown errors may occur? And how? Thanks.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] KVM: Add the check and free to avoid unknown errors.
  2020-02-15  2:00 [PATCH] KVM: Add the check and free to avoid unknown errors linmiaohe
@ 2020-02-15  5:33 ` Haiwei Li
  0 siblings, 0 replies; 4+ messages in thread
From: Haiwei Li @ 2020-02-15  5:33 UTC (permalink / raw)
  To: linmiaohe; +Cc: pbonzini, kvm, linux-kernel

linmiaohe <linmiaohe@huawei.com> 于2020年2月15日周六 上午10:00写道:
>
> Hi:
> Haiwei Li <lihaiwei.kernel@gmail.com> wrote:
> > From: Haiwei Li <lihaiwei@tencent.com>
> >
> > 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()'.
> >
> > Add the check and free to avoid unknown errors.
>
> Add the check and free? According to the code,it seem what you mean is "add the check against free" ?

Right, i can change the description.

>
> >
> > Signed-off-by: Haiwei Li <lihaiwei@tencent.com>
> >
> >       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);
> >       }
> >   }
>
> If (!kvm->debugfs_stat_data[i]) is checked in kfree() internal. And break early seems have no different effect.
> Could you please explain what unknown errors may occur? And how? Thanks.

I get the free() code. It is just like what you said. Thanks a lot.
Break early is useful. If kvm->debugfs_stat_data[i] is null, breaking
early can reduce the check.

>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] KVM: Add the check and free to avoid unknown errors.
  2020-02-14 21:02 Haiwei Li
@ 2020-02-17 17:15 ` Paolo Bonzini
  0 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2020-02-17 17:15 UTC (permalink / raw)
  To: Haiwei Li, kvm, linux-kernel

On 14/02/20 22:02, Haiwei Li wrote:
> From: Haiwei Li <lihaiwei@tencent.com>
> 
> 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@tencent.com>
> ---
>  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
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] KVM: Add the check and free to avoid unknown errors.
@ 2020-02-14 21:02 Haiwei Li
  2020-02-17 17:15 ` Paolo Bonzini
  0 siblings, 1 reply; 4+ messages in thread
From: Haiwei Li @ 2020-02-14 21:02 UTC (permalink / raw)
  To: kvm, linux-kernel; +Cc: pbonzini

From: Haiwei Li <lihaiwei@tencent.com>

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()'.

Add the check and free to avoid unknown errors.

Signed-off-by: Haiwei Li <lihaiwei@tencent.com>
---
  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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-15  2:00 [PATCH] KVM: Add the check and free to avoid unknown errors linmiaohe
2020-02-15  5:33 ` Haiwei Li
  -- strict thread matches above, loose matches on Subject: below --
2020-02-14 21:02 Haiwei Li
2020-02-17 17:15 ` Paolo Bonzini

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git
	git clone --mirror https://lore.kernel.org/lkml/8 lkml/git/8.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org
	public-inbox-index lkml

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git