public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open()
@ 2022-10-14  7:55 Hou Wenlong
  2022-10-14 14:09 ` Peter Xu
  0 siblings, 1 reply; 3+ messages in thread
From: Hou Wenlong @ 2022-10-14  7:55 UTC (permalink / raw)
  To: kvm
  Cc: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Peter Xu,
	linux-kernel

Refcount is increased before calling single_open() in
kvm_mmu_rmaps_stat_open(), If single_open() fails, refcount should be
restored, otherwise the vm couldn't be destroyed.

Fixes: 3bcd0662d66fd ("KVM: X86: Introduce mmu_rmaps_stat per-vm debugfs file")
Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
---
 arch/x86/kvm/debugfs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/debugfs.c b/arch/x86/kvm/debugfs.c
index cfed36aba2f7..412ed1b341fa 100644
--- a/arch/x86/kvm/debugfs.c
+++ b/arch/x86/kvm/debugfs.c
@@ -162,7 +162,12 @@ static int kvm_mmu_rmaps_stat_open(struct inode *inode, struct file *file)
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
-	return single_open(file, kvm_mmu_rmaps_stat_show, kvm);
+	if (single_open(file, kvm_mmu_rmaps_stat_show, kvm)) {
+		kvm_put_kvm(kvm);
+		return -ENOMEM;
+	}
+
+	return 0;
 }
 
 static int kvm_mmu_rmaps_stat_release(struct inode *inode, struct file *file)
-- 
2.31.1


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

* Re: [PATCH] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open()
  2022-10-14  7:55 [PATCH] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open() Hou Wenlong
@ 2022-10-14 14:09 ` Peter Xu
  2022-10-17  2:54   ` Hou Wenlong
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Xu @ 2022-10-14 14:09 UTC (permalink / raw)
  To: Hou Wenlong
  Cc: kvm, Sean Christopherson, Paolo Bonzini, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
	linux-kernel

On Fri, Oct 14, 2022 at 03:55:11PM +0800, Hou Wenlong wrote:
> Refcount is increased before calling single_open() in
> kvm_mmu_rmaps_stat_open(), If single_open() fails, refcount should be
> restored, otherwise the vm couldn't be destroyed.
> 
> Fixes: 3bcd0662d66fd ("KVM: X86: Introduce mmu_rmaps_stat per-vm debugfs file")
> Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
> ---
>  arch/x86/kvm/debugfs.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/debugfs.c b/arch/x86/kvm/debugfs.c
> index cfed36aba2f7..412ed1b341fa 100644
> --- a/arch/x86/kvm/debugfs.c
> +++ b/arch/x86/kvm/debugfs.c
> @@ -162,7 +162,12 @@ static int kvm_mmu_rmaps_stat_open(struct inode *inode, struct file *file)
>  	if (!kvm_get_kvm_safe(kvm))
>  		return -ENOENT;
>  
> -	return single_open(file, kvm_mmu_rmaps_stat_show, kvm);
> +	if (single_open(file, kvm_mmu_rmaps_stat_show, kvm)) {
> +		kvm_put_kvm(kvm);
> +		return -ENOMEM;
> +	}

Thanks for fixing this.  Would it be nicer to wire the single_open retval
(even though it'll only fail with -ENOMEM with current code base)?

-- 
Peter Xu


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

* Re: [PATCH] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open()
  2022-10-14 14:09 ` Peter Xu
@ 2022-10-17  2:54   ` Hou Wenlong
  0 siblings, 0 replies; 3+ messages in thread
From: Hou Wenlong @ 2022-10-17  2:54 UTC (permalink / raw)
  To: Peter Xu
  Cc: kvm, Sean Christopherson, Paolo Bonzini, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
	linux-kernel

On Fri, Oct 14, 2022 at 10:09:55PM +0800, Peter Xu wrote:
> On Fri, Oct 14, 2022 at 03:55:11PM +0800, Hou Wenlong wrote:
> > Refcount is increased before calling single_open() in
> > kvm_mmu_rmaps_stat_open(), If single_open() fails, refcount should be
> > restored, otherwise the vm couldn't be destroyed.
> > 
> > Fixes: 3bcd0662d66fd ("KVM: X86: Introduce mmu_rmaps_stat per-vm debugfs file")
> > Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
> > ---
> >  arch/x86/kvm/debugfs.c | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/x86/kvm/debugfs.c b/arch/x86/kvm/debugfs.c
> > index cfed36aba2f7..412ed1b341fa 100644
> > --- a/arch/x86/kvm/debugfs.c
> > +++ b/arch/x86/kvm/debugfs.c
> > @@ -162,7 +162,12 @@ static int kvm_mmu_rmaps_stat_open(struct inode *inode, struct file *file)
> >  	if (!kvm_get_kvm_safe(kvm))
> >  		return -ENOENT;
> >  
> > -	return single_open(file, kvm_mmu_rmaps_stat_show, kvm);
> > +	if (single_open(file, kvm_mmu_rmaps_stat_show, kvm)) {
> > +		kvm_put_kvm(kvm);
> > +		return -ENOMEM;
> > +	}
> 
> Thanks for fixing this.  Would it be nicer to wire the single_open retval
> (even though it'll only fail with -ENOMEM with current code base)?
> 
I agree, it would be nicer to return the single_open() retval directly.
I just follow how failure of simple_attr_open() is handled in kvm_debugfs_open(),
but I think it should be changed too. 

> -- 
> Peter Xu

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

end of thread, other threads:[~2022-10-17  2:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-14  7:55 [PATCH] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open() Hou Wenlong
2022-10-14 14:09 ` Peter Xu
2022-10-17  2:54   ` Hou Wenlong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox