All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ben Horgan <ben.horgan@arm.com>
To: Ted Chen <znscnchen@gmail.com>, pbonzini@redhat.com
Cc: kvm@vger.kernel.org, Dave Martin <Dave.Martin@arm.com>
Subject: Re: [PATCH] KVM: Avoid debugfs warning caused by repeated vm fd number
Date: Mon, 1 Sep 2025 14:39:06 +0100	[thread overview]
Message-ID: <b227a304-9b2f-4e89-9ca5-41d836ae4bae@arm.com> (raw)
In-Reply-To: <20250901130336.112842-1-znscnchen@gmail.com>

Hi Ted,

On 9/1/25 14:03, Ted Chen wrote:
> Avoid debugfs warning like "KVM: debugfs: duplicate directory 59904-4"
> caused by creating VMs with the same vm fd number in a single process.
> 
> As shown in the below test case, two test() are executed sequentially in a
> single process, each creating a new VM.
> 
> Though the 2nd test() creates a new VM after the 1st test() closes the
> vm_fd, KVM prints warnings like "KVM: debugfs: duplicate directory 59904-4"
> on creating the 2nd VM.
> 
> This is due to the dup() of the vcpu_fd in test(). So, after closing the
> 1st vm_fd, kvm->users_count of the 1st VM is still > 0 when creating the
> 2nd VM. So, KVM has not yet invoked kvm_destroy_vm() and
> kvm_destroy_vm_debugfs() for the 1st VM after closing the 1st vm_fd. The
> 2nd test() thus will be able to create a different VM with the same vm fd
> number as the 1st VM.
> 
> Therefore, besides having "pid" and "fdname" in the dir_name of the
> debugfs, add a random number to differentiate different VMs to avoid
> printing warning, also allowing the 2nd VM to have a functional debugfs.
> 
> Use get_random_u32() to avoid dir_name() taking up too much memory while
> greatly reducing the chance of printing warning.
> 
> void test(void)
> {
>         int kvm_fd, vm_fd, vcpu_fd;
> 
>         kvm_fd = open("/dev/kvm", O_RDWR);
>         if (kvm_fd == -1)
>                 return;
> 
>         vm_fd = ioctl(kvm_fd, KVM_CREATE_VM, 0);
>         if (vm_fd == -1)
>                 return;
>         vcpu_fd = ioctl(vm_fd, KVM_CREATE_VCPU, 0);
>         if (vcpu_fd == -1)
>                 return;
> 
>         dup(vcpu_fd);
>         close(vcpu_fd);
>         close(vm_fd);
>         close(kvm_fd);
> }
> 
> int main()
> {
>         test();
>         test();
> 
>         return 0;
> }
> 
> Signed-off-by: Ted Chen <znscnchen@gmail.com>
> ---
>  virt/kvm/kvm_main.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 6c07dd423458..f92a60ed5de8 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1017,7 +1017,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
>  {
>  	static DEFINE_MUTEX(kvm_debugfs_lock);
>  	struct dentry *dent;
> -	char dir_name[ITOA_MAX_LEN * 2];
> +	char dir_name[ITOA_MAX_LEN * 3];
>  	struct kvm_stat_data *stat_data;
>  	const struct _kvm_stats_desc *pdesc;
>  	int i, ret = -ENOMEM;
> @@ -1027,7 +1027,8 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
>  	if (!debugfs_initialized())
>  		return 0;
>  
> -	snprintf(dir_name, sizeof(dir_name), "%d-%s", task_pid_nr(current), fdname);
> +	snprintf(dir_name, sizeof(dir_name), "%d-%s-%u", task_pid_nr(current),
> +		 fdname, get_random_u32());

This does make the directory names (very likely) to be unique but it's
not helpful in distinguishing which directory maps to which vm. I wonder
if there is some better id we could use here.

Should the vm stats_id also be updated to be unique and use the same scheme?

>  	mutex_lock(&kvm_debugfs_lock);
>  	dent = debugfs_lookup(dir_name, kvm_debugfs_dir);
>  	if (dent) {

Thanks,

Ben


  reply	other threads:[~2025-09-01 13:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-01 13:03 [PATCH] KVM: Avoid debugfs warning caused by repeated vm fd number Ted Chen
2025-09-01 13:39 ` Ben Horgan [this message]
2025-09-02 12:46   ` Ted Chen
2025-09-02 15:58     ` Dave Martin
2025-09-08 13:02       ` Ted Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b227a304-9b2f-4e89-9ca5-41d836ae4bae@arm.com \
    --to=ben.horgan@arm.com \
    --cc=Dave.Martin@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=znscnchen@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.