Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sean Christopherson" <seanjc@google.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH 2/5] KVM: Protect all of kvm_vm_ioctl_create_vcpu() with kvm->lock
Date: Mon, 14 Sep 2026 18:37:18 +0000	[thread overview]
Message-ID: <20260914183719.141931F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260914181223.289061-3-seanjc@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Self-deadlock in KVM vCPU creation due to recursive `kvm->lock` acquisition on PowerPC and s390.
- [Low] Mixing goto-based error handling and guard() cleanup in the same function.
--

commit de58d2836963a7ea1b99cd6c2ad5be4f18984b33
Author: Sean Christopherson <seanjc@google.com>
KVM: Protect all of kvm_vm_ioctl_create_vcpu() with kvm->lock

This commit serializes vCPU creation by holding kvm->lock throughout
the entirety of kvm_vm_ioctl_create_vcpu(). It prevents concurrent
vCPU creations to simplify the code and prevent race conditions.

> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 78cc090435be5..c17cc8dd371b6 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -4165,6 +4165,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
>  	struct kvm_vcpu *vcpu;
>  	struct page *page;
>  
> +	guard(mutex)(&kvm->lock);
> +

[Severity: Critical]
Could this cause a self-deadlock on PowerPC and s390? The guard acquires
kvm->lock for the entire scope of kvm_vm_ioctl_create_vcpu(), but this
function subsequently calls architecture-specific hooks like
kvm_arch_vcpu_create() and kvm_arch_vcpu_postcreate().

On PowerPC, kvm_arch_vcpu_create() eventually calls
kvmppc_core_vcpu_create_hv() in arch/powerpc/kvm/book3s_hv.c, which
explicitly attempts to acquire kvm->lock via mutex_lock(&kvm->lock).

Similarly, on s390, both kvm_s390_vcpu_setup() and
kvm_arch_vcpu_postcreate() in arch/s390/kvm/s390/s390.c call
mutex_lock(&vcpu->kvm->lock).

Since standard Linux mutexes are not recursive, it appears any userspace
process invoking the KVM_CREATE_VCPU ioctl on these architectures will
indefinitely hang the creating thread.

[ ... ]

> @@ -4267,7 +4259,6 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
>  	atomic_inc(&kvm->online_vcpus);
>  	mutex_unlock(&vcpu->mutex);
>  
> -	mutex_unlock(&kvm->lock);
>  	kvm_arch_vcpu_postcreate(vcpu);
>  	kvm_create_vcpu_debugfs(vcpu);
>  	return r;
> @@ -4278,7 +4269,6 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
>  	xa_erase(&kvm->vcpu_array, vcpu->vcpu_idx);
>  unlock_vcpu_destroy:
>  	vcpu->vcpu_idx = -1;
> -	mutex_unlock(&kvm->lock);
>  	kvm_dirty_ring_free(&vcpu->dirty_ring);
>  arch_vcpu_destroy:

[Severity: Low]
Does this mix goto-based error handling and scope-based cleanup within
the same function? The cleanup subsystem guidelines specify that a function
should either use goto for cleanup or scope-based cleanup helpers, but
never both.

While the lock is safely released on return here, introducing
guard(mutex)(&kvm->lock) at the top of kvm_vm_ioctl_create_vcpu()
while retaining manual goto cleanup labels like unlock_vcpu_destroy
creates confusing ownership semantics.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260914181223.289061-1-seanjc@google.com?part=2

  reply	other threads:[~2026-09-14 18:37 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-14 18:12 [PATCH 0/5] KVM: Serialize vCPU creation and revert vcpu_ids tracking Sean Christopherson
2026-09-14 18:12 ` [PATCH 1/5] KVM: Reject attempts to lock all vCPUs if vCPU creation is in-progress Sean Christopherson
2026-09-14 18:24   ` sashiko-bot
2026-09-14 18:12 ` [PATCH 2/5] KVM: Protect all of kvm_vm_ioctl_create_vcpu() with kvm->lock Sean Christopherson
2026-09-14 18:37   ` sashiko-bot [this message]
2026-09-14 18:12 ` [PATCH 3/5] KVM: Move check for existing vCPU ID to the top of vCPU creation Sean Christopherson
2026-09-14 18:12 ` [PATCH 4/5] Revert "KVM: Check for duplicate vcpu_id as early as possible" Sean Christopherson
2026-09-14 18:12 ` [PATCH 5/5] KVM: WARN if vCPU creation is in-progress when locking all vCPUs Sean Christopherson
2026-09-14 18:39 ` [PATCH 0/5] KVM: Serialize vCPU creation and revert vcpu_ids tracking Christian Borntraeger
2026-09-15 13:13 ` Jean-Christophe Guillain
2026-09-21 13:57 ` Thorsten Leemhuis
2026-09-21 14:00   ` Paolo Bonzini
2026-09-21 14:11     ` Sean Christopherson

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=20260914183719.141931F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=seanjc@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox