From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D743C3D669A for ; Fri, 12 Jun 2026 23:06:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781305593; cv=none; b=arCgy3qKoEQRVfilgN4UA+z3EAVdKuLd/wMklD+DRt0brD4Yj3XKNOf7WlIPGqSIGS4j4mXfbN011Q2VfYFdVRfDubsj30foLBIay2TgeYbIi37puHmd1A2nX1Y2q9cyFeeGkb9zzllJH6Um4f/rUgrVI4WH/xf4P0KqTT2FifY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781305593; c=relaxed/simple; bh=ahTuH3NgpLKhsp7+7EDi8H+OFcF6qnTnFS+SEGqyZ58=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=COp0glrk5Hb/XWzZbnCD4wa+7K4Yvwu4nUhRpaeKRbpEg3lCwMKjtVYqgoCMSjMsctZ+hZPkJ95yrM9pHhnP5SJtbwH1jae5D7kHwjCY7Rr5kQgy9kBOD8aNaf721dUfub4Wqrajj8rXwivBA3Rx//7ZCNyD9kmL8HoXWvsrDyE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=S+qJ7Myt; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="S+qJ7Myt" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-8421ffff8a3so1856738b3a.2 for ; Fri, 12 Jun 2026 16:06:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781305590; x=1781910390; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=nNOCXVI1belceCflWgR3ReJ/ENbvUtfVA/ywdxNpwW8=; b=S+qJ7MytaGiWAuKKQgE+ReATljkfM7iwwUTs7O2WkvLASzmHagtqTCYF6sK++17cHy 7J6n0x8OzBa9KbZcKf6BqX3HAUpvG0J5EBPdAXFgwtxlBUBzajN7bjQ7Oah+M2Ls9CN+ WG04pJAcMldA2z0ClYMU7CJaLWfAw4ZTIj1jaD5n6GqJKzFYrg4B/Y5gFpCLmG5qCaPv dyL9KH5GMjkZQExiewU1StXN3mIc2s+RCouoRZn362+Dyw1v5LnH+8hZ69KNL4Gjnh9K ycSL7Yizvn/DnmZ1JNAlP8zlR184CYtHSi30yIO2MxgD5bZPln2QzZUCC4bm3abo0IQX Lqjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781305590; x=1781910390; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nNOCXVI1belceCflWgR3ReJ/ENbvUtfVA/ywdxNpwW8=; b=ZdBLXmpA3cVnEB3BdDLVzX/HKKm0Q5bxJpsQTaJGyT1kVZSEVAI4BIpv6p25sunPKu dkxxKUSp6R/zJxCs3nTgHOF+//ZhgOk2llRlSNRMqqCNslm8gNNhItubvzN9ValwFYnZ FGpbCfH9skTOd63Li60igzInTD4plAKYWBKgYgubbbgfzDuuhd3VA00adNRBqY0uFdxy oc9s7KRjh4A4Ukf5MtuGeV/g+yVC3K2bLJhkFDrTV3/WbOcqT0cLIWajJHYIZLApOohy /VVuSzbSJ3SSckjEP5Tj4d/8dCGEPI0FhQ1P0vglWmlPzQp2s2Da/mkwHRThDfOY3ker UZHQ== X-Forwarded-Encrypted: i=1; AFNElJ/cf826kLs+ydTKVlc7cZD/e2BBkd36JSJao3U3yEjjt8Hh29FoUn98m9CcxR3MpTU1Xrmfm8tuTd0XbH0=@vger.kernel.org X-Gm-Message-State: AOJu0YznXqVGToWKaAErh4VPrhBy+1lHyzJ6DOgUvJQ+o6EWHcdWUEqU Z3+TuI4koL7lK1tjzw7Zli4eiNeZ/LJGs7aDqTD4hHnYP3ewx5IpuhJ+jm3AUQrylVtmFNsZlGo noerB+Q== X-Received: from pfbhu47.prod.google.com ([2002:a05:6a00:69af:b0:842:301:313e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1d99:b0:83f:a040:a3d3 with SMTP id d2e1a72fcca58-8434cec7839mr4743675b3a.43.1781305589970; Fri, 12 Jun 2026 16:06:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 12 Jun 2026 16:06:17 -0700 In-Reply-To: <20260612230622.687665-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260612230622.687665-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.1136.gdb2ca164c4-goog Message-ID: <20260612230622.687665-4-seanjc@google.com> Subject: [PATCH v2 3/8] KVM: x86/hyperv: Ensure vCPU's Hyper-V object is initialized on cross-vCPU accesses From: Sean Christopherson To: Vitaly Kuznetsov , Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+5b32c49cd8f005e65654@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" When initializing a vCPU's Hyper-V object, ensure the object is fully initialized prior to exposing it through the vCPU, and ensure accesses from other tasks (e.g. other vCPUs) see the fully initialized object if vcpu->arch.hyperv is non-NULL. Lack of ordering manifests as a lockdep splat due to attempting to lock a TLB flush FIFO before the spinlock is initialized. INFO: trying to register non-static key. The code is fine but needs lockdep annotation, or maybe you didn't initialize this object before use? turning off the locking correctness validator. CPU: 1 PID: 5005 Comm: syz-executor189 Not tainted 6.6.120-smp-DEV #1 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 03/18/2026 Call Trace: [] dump_stack_lvl+0xcc/0x130 lib/dump_stack.c:106 [] assign_lock_key+0x1fd/0x230 kernel/locking/lockdep.c:977 [] register_lock_class+0x187/0x7a0 kernel/locking/lockdep.c:1291 [] __lock_acquire+0x179/0x7650 kernel/locking/lockdep.c:5016 [] lock_acquire+0x13f/0x3d0 kernel/locking/lockdep.c:5756 [] __raw_spin_lock include/linux/spinlock_api_smp.h:133 [inline] [] _raw_spin_lock+0x2b/0x40 kernel/locking/spinlock.c:154 [] spin_lock include/linux/spinlock.h:351 [inline] [] hv_tlb_flush_enqueue+0xb4/0x270 arch/x86/kvm/hyperv.c:1946 [] kvm_hv_flush_tlb+0xa96/0x1dc0 arch/x86/kvm/hyperv.c:2145 [] kvm_hv_hypercall+0x103b/0x1fe0 arch/x86/kvm/hyperv.c:-1 [] __vmx_handle_exit arch/x86/kvm/vmx/vmx.c:6624 [inline] [] vmx_handle_exit+0x12e3/0x21f0 arch/x86/kvm/vmx/vmx.c:6641 [] vcpu_enter_guest arch/x86/kvm/x86.c:11649 [inline] [] vcpu_run+0x4d01/0x79c0 arch/x86/kvm/x86.c:11832 [] kvm_arch_vcpu_ioctl_run+0xb49/0x1c80 arch/x86/kvm/x86.c:12179 [] kvm_vcpu_ioctl+0xc80/0xff0 virt/kvm/kvm_main.c:6029 [] vfs_ioctl fs/ioctl.c:52 [inline] [] __do_sys_ioctl fs/ioctl.c:872 [inline] [] __se_sys_ioctl+0xfd/0x170 fs/ioctl.c:858 [] do_syscall_x64 arch/x86/entry/common.c:52 [inline] [] do_syscall_64+0x69/0xb0 arch/x86/entry/common.c:93 [] entry_SYSCALL_64_after_hwframe+0x68/0xd2 Use the "safe" variant in all paths that are known to access the Hyper-V object, as detected by an upcoming lockdep assertion. Fixes: 0823570f0198 ("KVM: x86: hyper-v: Introduce TLB flush fifo") Fixes: fc08b628d7c9 ("KVM: x86: hyper-v: Allocate Hyper-V context lazily") Reported-by: syzbot+5b32c49cd8f005e65654@syzkaller.appspotmail.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/hyperv.c | 23 ++++++++++++++++++----- arch/x86/kvm/hyperv.h | 16 ++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index ecd344b91739..107eb7df20f1 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -206,13 +206,19 @@ static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx) static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx) { - struct kvm_vcpu *vcpu; struct kvm_vcpu_hv_synic *synic; + struct kvm_vcpu_hv *hv_vcpu; + struct kvm_vcpu *vcpu; vcpu = get_vcpu_by_vpidx(kvm, vpidx); - if (!vcpu || !to_hv_vcpu(vcpu)) + if (!vcpu) return NULL; - synic = to_hv_synic(vcpu); + + hv_vcpu = to_hv_vcpu_safe(vcpu); + if (!hv_vcpu) + return NULL; + + synic = &hv_vcpu->synic; return (synic->active) ? synic : NULL; } @@ -972,7 +978,6 @@ int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) if (!hv_vcpu) return -ENOMEM; - vcpu->arch.hyperv = hv_vcpu; hv_vcpu->vcpu = vcpu; synic_init(&hv_vcpu->synic); @@ -988,6 +993,14 @@ int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) spin_lock_init(&hv_vcpu->tlb_flush_fifo[i].write_lock); } + /* + * Ensure the structure is fully initialized before it's visible to + * other tasks, as much of the state can be legally accessed without + * holding vcpu->mutex. + * + * Pairs with the smp_load_acquire() in to_hv_vcpu_safe(). + */ + smp_store_release(&vcpu->arch.hyperv, hv_vcpu); return 0; } @@ -2166,7 +2179,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) bitmap_zero(vcpu_mask, KVM_MAX_VCPUS); kvm_for_each_vcpu(i, v, kvm) { - hv_v = to_hv_vcpu(v); + hv_v = to_hv_vcpu_safe(v); /* * The following check races with nested vCPUs entering/exiting diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index e6c74cfbb1cb..821b586ed746 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -61,6 +61,18 @@ static inline struct kvm_hv *to_kvm_hv(struct kvm *kvm) return &kvm->arch.hyperv; } +static inline struct kvm_vcpu_hv *to_hv_vcpu_safe(struct kvm_vcpu *vcpu) +{ + /* + * Ensure the HyperV structure is fully initialized when accessing it + * without holding vcpu->mutex (or some other guarantee that KVM can't + * concurrently instantiate the structure). + * + * Pairs with the smp_store_release() in kvm_hv_vcpu_init(). + */ + return smp_load_acquire(&vcpu->arch.hyperv); +} + static inline struct kvm_vcpu_hv *to_hv_vcpu(struct kvm_vcpu *vcpu) { return vcpu->arch.hyperv; @@ -87,7 +99,7 @@ static inline struct kvm_hv_syndbg *to_hv_syndbg(struct kvm_vcpu *vcpu) static inline u32 kvm_hv_get_vpindex(struct kvm_vcpu *vcpu) { - struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); + struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu_safe(vcpu); return hv_vcpu ? hv_vcpu->vp_index : vcpu->vcpu_idx; } @@ -197,7 +209,7 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, static inline struct kvm_vcpu_hv_tlb_flush_fifo *kvm_hv_get_tlb_flush_fifo(struct kvm_vcpu *vcpu, bool is_guest_mode) { - struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); + struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu_safe(vcpu); int i = is_guest_mode ? HV_L2_TLB_FLUSH_FIFO : HV_L1_TLB_FLUSH_FIFO; -- 2.54.0.1136.gdb2ca164c4-goog