diff mbox series

[v3,2/6] KVM: Shove vcpu stats_id init into kvm_vcpu_init()

Message ID 20220720092259.3491733-3-oliver.upton@linux.dev (mailing list archive)
State New, archived
Headers show
Series KVM: Clean up debugfs init/destroy | expand

Commit Message

Oliver Upton July 20, 2022, 9:22 a.m. UTC
From: Oliver Upton <oupton@google.com>

Initialize stats_id alongside other kvm_vcpu fields to futureproof
against possible initialization order mistakes in KVM.

No functional change intended.

Signed-off-by: Oliver Upton <oupton@google.com>
---
 virt/kvm/kvm_main.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Sean Christopherson Aug. 5, 2022, 7:06 p.m. UTC | #1
On Wed, Jul 20, 2022, Oliver Upton wrote:
> From: Oliver Upton <oupton@google.com>
> 
> Initialize stats_id alongside other kvm_vcpu fields to futureproof
> against possible initialization order mistakes in KVM.

Nit, I dislike the handwaving, it doesn't take much effort to explain exactly
what this guards against.

  Initialize stats_id alongside other kvm_vcpu fields to make it more
  difficult to unintentionally access stats_id before it's set.

> No functional change intended.
> 
> Signed-off-by: Oliver Upton <oupton@google.com>
> ---

Reviewed-by: Sean Christopherson <seanjc@google.com>
Paolo Bonzini Aug. 9, 2022, 2:52 p.m. UTC | #2
On 8/5/22 21:06, Sean Christopherson wrote:
>    Initialize stats_id alongside other kvm_vcpu fields to make it more
>    difficult to unintentionally access stats_id before it's set.

Thanks for the proposal, I'll fix it when applying.

Paolo
diff mbox series

Patch

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index cc760ebcd390..1f78b7ad5430 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -484,6 +484,10 @@  static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 	vcpu->ready = false;
 	preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
 	vcpu->last_used_slot = NULL;
+
+	/* Fill the stats id string for the vcpu */
+	snprintf(vcpu->stats_id, sizeof(vcpu->stats_id), "kvm-%d/vcpu-%d",
+		 task_pid_nr(current), id);
 }
 
 static void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
@@ -3919,10 +3923,6 @@  static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
 	if (r)
 		goto unlock_vcpu_destroy;
 
-	/* Fill the stats id string for the vcpu */
-	snprintf(vcpu->stats_id, sizeof(vcpu->stats_id), "kvm-%d/vcpu-%d",
-		 task_pid_nr(current), id);
-
 	/* Now it's all set up, let userspace reach it */
 	kvm_get_kvm(kvm);
 	r = create_vcpu_fd(vcpu);