From nobody Fri Oct 3 01:08:47 2025 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 64BC93148DE for ; Tue, 9 Sep 2025 10:00:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757412020; cv=none; b=GmGuYJELkU8eqiaI6JpTXqDOZLp638HxzcIEX+OeyKdHS4/xkweaCEbYIGrkII4GebWIo8bUxuzS5IjZVdwzBkiwzg3TvYOWqZxh0Kcdk7QtapQsFQvkCtA4/IYUeAEpXTFRx4BCb6SeBGXelxIJuFAUKjRG3/OF4b6a+C4m3XY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757412020; c=relaxed/simple; bh=8/sRC1VX1IpKftvdxciILiaO55fvW2OqsWm6irySeLA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=husplApvz0PNTWnckLixo0fiqhc5RskH15pXkGmwofDJHlIVH7SkcNI7BTXPaS6ZMCwjfZyEQ8X/Bdf2L4CW4WgmCc91itJTH+rGkJU0NBqgdcCHAd58DmNuR8c6ZJQH3a9F5ce2PV5Vxc1io5Po+MbNPxnwDxZ8O8QDkTbOnmc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--keirf.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LYcsAh3p; arc=none smtp.client-ip=209.85.221.74 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--keirf.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LYcsAh3p" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3df2f4aedd1so3936461f8f.3 for ; Tue, 09 Sep 2025 03:00:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1757412017; x=1758016817; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=naUvPBgX3c5Rxz+GLdwV0lKRhpqfT3UVnsLZMdoq3bI=; b=LYcsAh3pxIWtX9cwqog079az7ek0fkXEl7FFQQQyknCG/M6tBtvqy3cVw/JJYf4h67 2qxJvhZLkcToeCU//EuDLaHC0Fi+U8yvTegUj/du1OHJbHZwt4tzgkjUTd4d54gb5U2G 7ZlBgkJFAelpBIsnLo81e6ahqGgN/4aliJuVeKiPeyaIvlNyjNWhF5nDPfuj+O03vTsz QZEcisWoKLOeW1zBgxEU41p+9vfIcxf5ZIOt+6g0CjF8qGm+MoJ0kSJgBMbNJ8USrder zyfs4wsB8O9X9VVMweA4az8BeapVQDMR3XJrVy5lu8P9oeO/rN8vl/0fqMk0OTo2hs6D GXYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757412017; x=1758016817; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=naUvPBgX3c5Rxz+GLdwV0lKRhpqfT3UVnsLZMdoq3bI=; b=md8zkQbmceAgB5MIoGU/qwPCt0/NFRn+L9x0w8yW2muxtmNDcvBP51f+jokaIKHvRa Uad+sqzugzh6JfKZ4mVmI836cY5h73TivVNflh3NrXWx/SrifKzrFE5xhVCarTC4eYMO VJQ/u+rJbLkFsEGeCzB+9GKrHu/eS73pVkKb9QAgjFtSqOT9DSq4SZsB7OkUetpGeoVo zcXxXqcGPbWh/gNGc2aiXijKHgVW7+s3Vt+BtnHblacLBbSbQOwR2D8dp/mZR+zYZgM1 djUd7layzclzu9a7OMXEja7JlQF9w8cpryzkqkPRRWhD1Q9OQYGcYFkMSOogqyhZLnsO suMA== X-Forwarded-Encrypted: i=1; AJvYcCU5QIa/3Va5eRCexSjw31puxIRCkcFUCkWz1/I1lNmxhdaDh/cRK/kueRvTFqPyjbOy661tPq/UI1OibTY=@vger.kernel.org X-Gm-Message-State: AOJu0Yzx3cwRDOYXjcjR2y58uoYRuiCuMICiH2eQNEdgw7oMZovRVO2X yY5fKSpZGdmmmcnTBWBHsy07m/DyrLpF3tc70F01Eb55M/1p7WicAJjlBUbdnYxyaMMeTjhNfhE UbA== X-Google-Smtp-Source: AGHT+IHmRvR3R+1MwJeFj/j7L7E831lkKiWcOJ9Jdj1nDD8J0dRSwBSxqm629IKVNfBpTcEIAykfMOGmsA== X-Received: from wrbei3.prod.google.com ([2002:a05:6000:4183:b0:3e6:f3df:9b4d]) (user=keirf job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2883:b0:3e3:f332:73f0 with SMTP id ffacd0b85a97d-3e64c693923mr8140093f8f.49.1757412016697; Tue, 09 Sep 2025 03:00:16 -0700 (PDT) Date: Tue, 9 Sep 2025 10:00:06 +0000 In-Reply-To: <20250909100007.3136249-1-keirf@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250909100007.3136249-1-keirf@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250909100007.3136249-4-keirf@google.com> Subject: [PATCH v4 3/4] KVM: Implement barriers before accessing kvm->buses[] on SRCU read paths From: Keir Fraser To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , Eric Auger , Oliver Upton , Marc Zyngier , Will Deacon , Paolo Bonzini , Keir Fraser Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This ensures that, if a VCPU has "observed" that an IO registration has occurred, the instruction currently being trapped or emulated will also observe the IO registration. At the same time, enforce that kvm_get_bus() is used only on the update side, ensuring that a long-term reference cannot be obtained by an SRCU reader. Signed-off-by: Keir Fraser --- arch/x86/kvm/vmx/vmx.c | 7 +++++++ include/linux/kvm_host.h | 10 +++++++--- virt/kvm/kvm_main.c | 32 ++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index aa157fe5b7b3..0bdf9405969a 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5785,6 +5785,13 @@ static int handle_invalid_guest_state(struct kvm_vcp= u *vcpu) if (kvm_test_request(KVM_REQ_EVENT, vcpu)) return 1; =20 + /* + * Ensure that any updates to kvm->buses[] observed by the + * previous instruction (emulated or otherwise) are also + * visible to the instruction KVM is about to emulate. + */ + smp_rmb(); + if (!kvm_emulate_instruction(vcpu, 0)) return 0; =20 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 15656b7fba6c..e7d6111cf254 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -966,11 +966,15 @@ static inline bool kvm_dirty_log_manual_protect_and_i= nit_set(struct kvm *kvm) return !!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET); } =20 +/* + * Get a bus reference under the update-side lock. No long-term SRCU reader + * references are permitted, to avoid stale reads vs concurrent IO + * registrations. + */ static inline struct kvm_io_bus *kvm_get_bus(struct kvm *kvm, enum kvm_bus= idx) { - return srcu_dereference_check(kvm->buses[idx], &kvm->srcu, - lockdep_is_held(&kvm->slots_lock) || - !refcount_read(&kvm->users_count)); + return rcu_dereference_protected(kvm->buses[idx], + lockdep_is_held(&kvm->slots_lock)); } =20 static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6c07dd423458..870ad8ea93a7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1103,6 +1103,14 @@ void __weak kvm_arch_create_vm_debugfs(struct kvm *k= vm) { } =20 +/* Called only on cleanup and destruction paths when there are no users. */ +static inline struct kvm_io_bus *kvm_get_bus_for_destruction(struct kvm *k= vm, + enum kvm_bus idx) +{ + return rcu_dereference_protected(kvm->buses[idx], + !refcount_read(&kvm->users_count)); +} + static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) { struct kvm *kvm =3D kvm_arch_alloc_vm(); @@ -1228,7 +1236,7 @@ static struct kvm *kvm_create_vm(unsigned long type, = const char *fdname) out_err_no_arch_destroy_vm: WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count)); for (i =3D 0; i < KVM_NR_BUSES; i++) - kfree(kvm_get_bus(kvm, i)); + kfree(kvm_get_bus_for_destruction(kvm, i)); kvm_free_irq_routing(kvm); out_err_no_irq_routing: cleanup_srcu_struct(&kvm->irq_srcu); @@ -1276,7 +1284,7 @@ static void kvm_destroy_vm(struct kvm *kvm) =20 kvm_free_irq_routing(kvm); for (i =3D 0; i < KVM_NR_BUSES; i++) { - struct kvm_io_bus *bus =3D kvm_get_bus(kvm, i); + struct kvm_io_bus *bus =3D kvm_get_bus_for_destruction(kvm, i); =20 if (bus) kvm_io_bus_destroy(bus); @@ -5843,6 +5851,18 @@ static int __kvm_io_bus_write(struct kvm_vcpu *vcpu,= struct kvm_io_bus *bus, return -EOPNOTSUPP; } =20 +static struct kvm_io_bus *kvm_get_bus_srcu(struct kvm *kvm, enum kvm_bus i= dx) +{ + /* + * Ensure that any updates to kvm_buses[] observed by the previous vCPU + * machine instruction are also visible to the vCPU machine instruction + * that triggered this call. + */ + smp_mb__after_srcu_read_lock(); + + return srcu_dereference(kvm->buses[idx], &kvm->srcu); +} + int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t ad= dr, int len, const void *val) { @@ -5855,7 +5875,7 @@ int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_= bus bus_idx, gpa_t addr, .len =3D len, }; =20 - bus =3D srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); + bus =3D kvm_get_bus_srcu(vcpu->kvm, bus_idx); if (!bus) return -ENOMEM; r =3D __kvm_io_bus_write(vcpu, bus, &range, val); @@ -5874,7 +5894,7 @@ int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, en= um kvm_bus bus_idx, .len =3D len, }; =20 - bus =3D srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); + bus =3D kvm_get_bus_srcu(vcpu->kvm, bus_idx); if (!bus) return -ENOMEM; =20 @@ -5924,7 +5944,7 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_b= us bus_idx, gpa_t addr, .len =3D len, }; =20 - bus =3D srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); + bus =3D kvm_get_bus_srcu(vcpu->kvm, bus_idx); if (!bus) return -ENOMEM; r =3D __kvm_io_bus_read(vcpu, bus, &range, val); @@ -6033,7 +6053,7 @@ struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *= kvm, enum kvm_bus bus_idx, =20 srcu_idx =3D srcu_read_lock(&kvm->srcu); =20 - bus =3D srcu_dereference(kvm->buses[bus_idx], &kvm->srcu); + bus =3D kvm_get_bus_srcu(kvm, bus_idx); if (!bus) goto out_unlock; =20 --=20 2.51.0.384.g4c02a37b29-goog