kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open()
@ 2022-10-17  3:06 Hou Wenlong
  2022-10-17  3:06 ` [PATCH v2 2/2] KVM: debugfs: Return retval of simple_attr_open() if it fails Hou Wenlong
  2022-10-17 18:05 ` [PATCH v2 1/2] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open() Peter Xu
  0 siblings, 2 replies; 4+ messages in thread
From: Hou Wenlong @ 2022-10-17  3:06 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..e6efd0821c59 100644
--- a/arch/x86/kvm/debugfs.c
+++ b/arch/x86/kvm/debugfs.c
@@ -157,12 +157,17 @@ static int kvm_mmu_rmaps_stat_show(struct seq_file *m, void *v)
 
 static int kvm_mmu_rmaps_stat_open(struct inode *inode, struct file *file)
 {
+	int ret;
 	struct kvm *kvm = inode->i_private;
 
 	if (!kvm_get_kvm_safe(kvm))
 		return -ENOENT;
 
-	return single_open(file, kvm_mmu_rmaps_stat_show, kvm);
+	ret = single_open(file, kvm_mmu_rmaps_stat_show, kvm);
+	if (ret)
+		kvm_put_kvm(kvm);
+
+	return ret;
 }
 
 static int kvm_mmu_rmaps_stat_release(struct inode *inode, struct file *file)
-- 
2.31.1


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

* [PATCH v2 2/2] KVM: debugfs: Return retval of simple_attr_open() if it fails
  2022-10-17  3:06 [PATCH v2 1/2] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open() Hou Wenlong
@ 2022-10-17  3:06 ` Hou Wenlong
  2022-10-22  9:08   ` Paolo Bonzini
  2022-10-17 18:05 ` [PATCH v2 1/2] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open() Peter Xu
  1 sibling, 1 reply; 4+ messages in thread
From: Hou Wenlong @ 2022-10-17  3:06 UTC (permalink / raw)
  To: kvm; +Cc: Paolo Bonzini, linux-kernel

Although simple_attr_open() fails only with -ENOMEM with current code
base, it would be nicer to return retval of simple_attr_open() directly
in kvm_debugfs_open().

No functional change intended.

Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
---
 virt/kvm/kvm_main.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index e30f1b4ecfa5..f7b06c1e8827 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -5398,6 +5398,7 @@ static int kvm_debugfs_open(struct inode *inode, struct file *file,
 			   int (*get)(void *, u64 *), int (*set)(void *, u64),
 			   const char *fmt)
 {
+	int ret;
 	struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
 					  inode->i_private;
 
@@ -5409,15 +5410,13 @@ static int kvm_debugfs_open(struct inode *inode, struct file *file,
 	if (!kvm_get_kvm_safe(stat_data->kvm))
 		return -ENOENT;
 
-	if (simple_attr_open(inode, file, get,
-		    kvm_stats_debugfs_mode(stat_data->desc) & 0222
-		    ? set : NULL,
-		    fmt)) {
+	ret = simple_attr_open(inode, file, get,
+			       kvm_stats_debugfs_mode(stat_data->desc) & 0222
+			       ? set : NULL, fmt);
+	if (ret)
 		kvm_put_kvm(stat_data->kvm);
-		return -ENOMEM;
-	}
 
-	return 0;
+	return ret;
 }
 
 static int kvm_debugfs_release(struct inode *inode, struct file *file)
-- 
2.31.1


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

* Re: [PATCH v2 1/2] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open()
  2022-10-17  3:06 [PATCH v2 1/2] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open() Hou Wenlong
  2022-10-17  3:06 ` [PATCH v2 2/2] KVM: debugfs: Return retval of simple_attr_open() if it fails Hou Wenlong
@ 2022-10-17 18:05 ` Peter Xu
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Xu @ 2022-10-17 18:05 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 Mon, Oct 17, 2022 at 11:06:09AM +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>

Reviewed-by: Peter Xu <peterx@redhat.com>

Thanks,

-- 
Peter Xu


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

* Re: [PATCH v2 2/2] KVM: debugfs: Return retval of simple_attr_open() if it fails
  2022-10-17  3:06 ` [PATCH v2 2/2] KVM: debugfs: Return retval of simple_attr_open() if it fails Hou Wenlong
@ 2022-10-22  9:08   ` Paolo Bonzini
  0 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2022-10-22  9:08 UTC (permalink / raw)
  To: Hou Wenlong, kvm; +Cc: linux-kernel

On 10/17/22 05:06, Hou Wenlong wrote:
> Although simple_attr_open() fails only with -ENOMEM with current code
> base, it would be nicer to return retval of simple_attr_open() directly
> in kvm_debugfs_open().
> 
> No functional change intended.
> 
> Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
> ---
>   virt/kvm/kvm_main.c | 13 ++++++-------
>   1 file changed, 6 insertions(+), 7 deletions(-)
> 
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index e30f1b4ecfa5..f7b06c1e8827 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -5398,6 +5398,7 @@ static int kvm_debugfs_open(struct inode *inode, struct file *file,
>   			   int (*get)(void *, u64 *), int (*set)(void *, u64),
>   			   const char *fmt)
>   {
> +	int ret;
>   	struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
>   					  inode->i_private;
>   
> @@ -5409,15 +5410,13 @@ static int kvm_debugfs_open(struct inode *inode, struct file *file,
>   	if (!kvm_get_kvm_safe(stat_data->kvm))
>   		return -ENOENT;
>   
> -	if (simple_attr_open(inode, file, get,
> -		    kvm_stats_debugfs_mode(stat_data->desc) & 0222
> -		    ? set : NULL,
> -		    fmt)) {
> +	ret = simple_attr_open(inode, file, get,
> +			       kvm_stats_debugfs_mode(stat_data->desc) & 0222
> +			       ? set : NULL, fmt);
> +	if (ret)
>   		kvm_put_kvm(stat_data->kvm);
> -		return -ENOMEM;
> -	}
>   
> -	return 0;
> +	return ret;
>   }
>   
>   static int kvm_debugfs_release(struct inode *inode, struct file *file)

Queued, thanks.

Paolo


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

end of thread, other threads:[~2022-10-22 10:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-17  3:06 [PATCH v2 1/2] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open() Hou Wenlong
2022-10-17  3:06 ` [PATCH v2 2/2] KVM: debugfs: Return retval of simple_attr_open() if it fails Hou Wenlong
2022-10-22  9:08   ` Paolo Bonzini
2022-10-17 18:05 ` [PATCH v2 1/2] KVM: x86: Reduce refcount if single_open() fails in kvm_mmu_rmaps_stat_open() Peter Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).