From nobody Sat Oct 4 08:05:13 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 DCA6525C822 for ; Tue, 19 Aug 2025 09:08:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755594541; cv=none; b=lYCA3DgPh8aBohUxRFzJ/1Ue5vXjloYE4XeOOkdz3sd8G4fIp0DrkRlOwCPuTmu6RFcCnbjDHHznI+fTVSZUfXA/GJYmMRhM6wCfyOCO4ndgeVJ+x7wBm6yeZm7VhCqH22XKt4Aod4nwHyLeDwwBBPYx4L2Q42WZdpdoKtpsLVY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755594541; c=relaxed/simple; bh=iliNESQOeDobGewdbbUbXsvs6Y5CEaxm25avhAkMT18=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=meLWqZOMtznOxCeFerIQsX16XKxsyA49oHPQ7Rg9l/0ixqDCrLlWKqvn7YP9VS15GM8kqq95DOOsimEjkipJC8PmQJu0UYT4EL2ietVqOICa1QqNvn7CRlUOeOc1TrXyl5vfK2r/K85PajNVDiibhTYcoP7e6M5+4a2QQzj96BY= 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=eh0xO4Gt; arc=none smtp.client-ip=209.85.128.73 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="eh0xO4Gt" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-45a1b0098c0so30634325e9.0 for ; Tue, 19 Aug 2025 02:08:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755594538; x=1756199338; 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=RbYlBne+CRt0rANt7gz2XsCKAdtbMUR7nWNO2HYDU48=; b=eh0xO4GtyM5YGXjPr5twFepjqa1qk8oalobxydLGISrWYb/dxjpQl7V4K5QOR5GAyp VGNJ5gmm/pq8CB3uydQCTbf9r/Uar/4yZh+PuYH5xgIUb96ThRQXLBYfxawDuoIW6nMR oMWLJKWL99jMj/V04nZ3JmJaTq9cgGVZqXUiYSGojRU1d3KA3Kc2KjMTxzFb+aHt5MFV ukBqfFUMXVTKoDHoA0kgDcTBVvqvQLPm0/W1TNGULKH4I0y1knkSdFcmjX6wqdP896L6 SLHJAbHDNAqAKBi9a6ltI2bGv3GQkgGG1O+jqY0dXTuu6XAL0s8hjz5wWy37SylgrgO+ p54w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755594538; x=1756199338; 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=RbYlBne+CRt0rANt7gz2XsCKAdtbMUR7nWNO2HYDU48=; b=NZgCy9lKBr33dSTaiynVN2mY3tQ8zyrD4e8Wov0pQ+nSVQItGzKn3mWDNpH4GAjF9/ A3lz6x/jc/z7JDMPO6fSjW63Y79S+9IVNI/KM4ZTOwiJ0zAjbb9oMbcL7FEw130V1Noh worBKoLp/52t9arGgsyyfL7/yu+k5f/NK3rxynxChMp71ZJaGNWfqamWOviztkMkrc+7 MTNF4M/RozWeQOdkZlK78DTQnPUikWC2fsrcHB7MOUqtSb9q4sxRxrTWk4++J1jTfdew 3x+TwIBfFbAV5Nc9RopY0tCPX6jBAZ6cnJFzfUT/T6ALLGLHHksuF0cWgmhRpXpEZjbB 8JAA== X-Forwarded-Encrypted: i=1; AJvYcCVShFtZ1VE+SYkbmc1ktAbc6wYxMgv0gCprlEjxLne71yZZyyH6XyEwr9J8HoyQFqzB9eG4f1kLllXSzZ0=@vger.kernel.org X-Gm-Message-State: AOJu0YwL5CkgbrGj69Du6YIZBB0RcZV+h8tsi5aHl08dt3osE3egdyyH DjaYhFLHUL4abpQmwZxdCTzbGa5OddzszRU2kSEphxD+wmvbRRJ5yXq9LnOKvcWTED9gTto0y1+ zjg== X-Google-Smtp-Source: AGHT+IFTda9OEOY5S6WXkDS//BLctKGWXODfwGT2L71EAeMG+xw30IhSWH1WfqqbuVudUz36x/Bwh88Big== X-Received: from wmte11.prod.google.com ([2002:a05:600c:8b2b:b0:459:dac7:4ea6]) (user=keirf job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4750:b0:459:df48:3b19 with SMTP id 5b1f17b1804b1-45b43e07202mr14121285e9.18.1755594538057; Tue, 19 Aug 2025 02:08:58 -0700 (PDT) Date: Tue, 19 Aug 2025 09:08:50 +0000 In-Reply-To: <20250819090853.3988626-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: <20250819090853.3988626-1-keirf@google.com> X-Mailer: git-send-email 2.51.0.rc1.193.gad69d77794-goog Message-ID: <20250819090853.3988626-2-keirf@google.com> Subject: [PATCH v3 1/4] KVM: arm64: vgic-init: Remove vgic_ready() macro 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" It is now used only within kvm_vgic_map_resources(). vgic_dist::ready is already written directly by this function, so it is clearer to bypass the macro for reads as well. Signed-off-by: Keir Fraser Tested-by: syzbot@syzkaller.appspotmail.com --- arch/arm64/kvm/vgic/vgic-init.c | 5 ++--- include/kvm/arm_vgic.h | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-ini= t.c index 1e680ad6e863..3f207b5f80a5 100644 --- a/arch/arm64/kvm/vgic/vgic-init.c +++ b/arch/arm64/kvm/vgic/vgic-init.c @@ -554,7 +554,6 @@ int vgic_lazy_init(struct kvm *kvm) * Also map the virtual CPU interface into the VM. * v2 calls vgic_init() if not already done. * v3 and derivatives return an error if the VGIC is not initialized. - * vgic_ready() returns true if this function has succeeded. */ int kvm_vgic_map_resources(struct kvm *kvm) { @@ -563,12 +562,12 @@ int kvm_vgic_map_resources(struct kvm *kvm) gpa_t dist_base; int ret =3D 0; =20 - if (likely(vgic_ready(kvm))) + if (likely(dist->ready)) return 0; =20 mutex_lock(&kvm->slots_lock); mutex_lock(&kvm->arch.config_lock); - if (vgic_ready(kvm)) + if (dist->ready) goto out; =20 if (!irqchip_in_kernel(kvm)) diff --git a/include/kvm/arm_vgic.h b/include/kvm/arm_vgic.h index 404883c7af6e..e7ffaf4bf2e7 100644 --- a/include/kvm/arm_vgic.h +++ b/include/kvm/arm_vgic.h @@ -406,7 +406,6 @@ u64 vgic_v3_get_misr(struct kvm_vcpu *vcpu); =20 #define irqchip_in_kernel(k) (!!((k)->arch.vgic.in_kernel)) #define vgic_initialized(k) ((k)->arch.vgic.initialized) -#define vgic_ready(k) ((k)->arch.vgic.ready) #define vgic_valid_spi(k, i) (((i) >=3D VGIC_NR_PRIVATE_IRQS) && \ ((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS)) =20 --=20 2.51.0.rc1.193.gad69d77794-goog From nobody Sat Oct 4 08:05:13 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.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 1CC5231A054 for ; Tue, 19 Aug 2025 09:09:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755594542; cv=none; b=Zd68lWnZ+Cf7cV/v5TDfX3mfq+aJs0ZDd2nouYLI0DUB9848547arnDglWmUtcOcy6YMxtoyJppRMfa55+ij2RxozPvZFFykXVXCghxqrBTCFE3QsVdIYYq4o+BAz2ve70AzzhZfo/tgREePkVjZnnsQKjsYEOz+qs6Qz64Iokw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755594542; c=relaxed/simple; bh=Lj8R+QAbvGD/j2/IfIqy36p+Mv51YViuIDGXC2HRQXM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KBfGdJFicWVS4D/FjmtaP4Skf65RToB0Pz1sXb70W0zlr7zXEnuPPuj9MJFP3sSNZnAtZtHD080oiTEPV6kx31E3Gm2pptmp/+EAtSz1BDvCgnpnjGvaZKrg/4rm/55kcI/vuWfu8vgKWVaX7pBXDgfk47c7YzDJdRbzrv+UTQ8= 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=IbYXET9f; arc=none smtp.client-ip=209.85.128.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="IbYXET9f" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-45a1b05b15eso35990395e9.1 for ; Tue, 19 Aug 2025 02:09:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755594539; x=1756199339; 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=b7GQRkJjLj0j3dNMqeMnq8zCLevsX26SkayOg/7wVlQ=; b=IbYXET9fBZ6ANe2D6j+vU9pkGZuz09OzoA2CQAinHdUIOe6osNlpxMFXwfXJS6XEc6 3T6PHm7DC9KaV6wLALEpASqZjAllcog87rGyStN6QhBnr0E13nDe4xwWaehohUlGlPZG 7WxAzHBvgAkJdykdEsjtKmYe75cndKm1WPrSYI8nU4+P5NWfaaPBVnkpoNZruTQY8xlq mYOXHxha078tz0QihctSBf3zpkQR+b2U+DZwbFu3dTdSBIRzLhPZYcz/c73UoM9hb/tE U67ciOSJ+ZMxpZ/vW5CjcBCzdXVFUuDMyefzY3BkoqThbOn05y2nXLs6Z9DZTSr2mAYR mpoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755594539; x=1756199339; 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=b7GQRkJjLj0j3dNMqeMnq8zCLevsX26SkayOg/7wVlQ=; b=AyZxWgVFJqs+VZEEnrGKTITGpU6L3T1uz5VHst79SiWVD6BXvLcv8LIK7jOWkKE4Bj NyVaZ/j72p+7fDG3bpvhSl2T272JRqUvhVyQERYq99Ml10hIIg/4vQUwnRumvKE4BLkO 6WD1ylpgW8mW0Viy/IM9ASryPVBtCljDYEO9Jvb2qK5g9tZM/afzFVTnGdQ801OdaP9X LZeaB18QqL14oWBkOR+/6sBipz5WxGo6H+aQ1jUCk9MpRj6yWGCkntre6Q9tyn1I9Ayr DugtbDhEd6TBEnVIrGHwRNwIsketDkA8T72UFnmQnhHNw2b/HL+mjKaECaNQu1QWsHxt TXug== X-Forwarded-Encrypted: i=1; AJvYcCXOrIZqqs680FiegK8JM3Z/KFhdZwkeCCgn7YzXd2v/N6UbGF4uFoxxj9O+bP0K6K/y+Hmsqv47MPTPtxM=@vger.kernel.org X-Gm-Message-State: AOJu0Yzge3AfLpR3189aYxiQM3oWSL/3PokbW7FdabLLeawoteuOFSwT UVgMuE0iCqHtDauDnTE/c4/5x4XIdKU+PYR4awi6QUqMCCPxvYJnMFUwMpDYCSMOMUQxdYgm/wz LKg== X-Google-Smtp-Source: AGHT+IEl+5DEbXQSzekfCwJnh9tAA1jzlM78/0IsLI3MJWSPRp03wLItZPI7GL4eVRBnoTBswnyDHI1oMA== X-Received: from wmbeq12.prod.google.com ([2002:a05:600c:848c:b0:459:db29:1efa]) (user=keirf job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4f12:b0:459:e3a1:a0c4 with SMTP id 5b1f17b1804b1-45b46190a32mr1402635e9.30.1755594539541; Tue, 19 Aug 2025 02:08:59 -0700 (PDT) Date: Tue, 19 Aug 2025 09:08:51 +0000 In-Reply-To: <20250819090853.3988626-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: <20250819090853.3988626-1-keirf@google.com> X-Mailer: git-send-email 2.51.0.rc1.193.gad69d77794-goog Message-ID: <20250819090853.3988626-3-keirf@google.com> Subject: [PATCH v3 2/4] KVM: arm64: vgic: Explicitly implement vgic_dist::ready ordering 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" In preparation to remove synchronize_srcu() from MMIO registration, remove the distributor's dependency on this implicit barrier by direct acquire-release synchronization on the flag write and its lock-free check. Signed-off-by: Keir Fraser Tested-by: syzbot@syzkaller.appspotmail.com --- arch/arm64/kvm/vgic/vgic-init.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-ini= t.c index 3f207b5f80a5..ccccb5c04ac1 100644 --- a/arch/arm64/kvm/vgic/vgic-init.c +++ b/arch/arm64/kvm/vgic/vgic-init.c @@ -562,7 +562,7 @@ int kvm_vgic_map_resources(struct kvm *kvm) gpa_t dist_base; int ret =3D 0; =20 - if (likely(dist->ready)) + if (likely(smp_load_acquire(&dist->ready))) return 0; =20 mutex_lock(&kvm->slots_lock); @@ -593,14 +593,7 @@ int kvm_vgic_map_resources(struct kvm *kvm) goto out_slots; } =20 - /* - * kvm_io_bus_register_dev() guarantees all readers see the new MMIO - * registration before returning through synchronize_srcu(), which also - * implies a full memory barrier. As such, marking the distributor as - * 'ready' here is guaranteed to be ordered after all vCPUs having seen - * a completely configured distributor. - */ - dist->ready =3D true; + smp_store_release(&dist->ready, true); goto out_slots; out: mutex_unlock(&kvm->arch.config_lock); --=20 2.51.0.rc1.193.gad69d77794-goog From nobody Sat Oct 4 08:05:13 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 858BD31AF21 for ; Tue, 19 Aug 2025 09:09:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755594544; cv=none; b=PLi9vSPTdXgg1n8tOkccNPqqYpovzDSgLQePutj78rCNrej6lJ7M8NXsHdqhhGeizx/Mw/Gqy4MnLFVOYPGVrLqdTSpYGV8skJVd1OZCJM8a+cCAs1SH6mqj1JnQiFN5bCz/Y4ZfOpSxPW/titIi9OHCKvGKjH5B4JHtPrnVRLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755594544; c=relaxed/simple; bh=/UoRxomHA1M2V2aUUXQmoA60DOqPM2oKLqITF3Q6naA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GWajoCukOhCnuPw/YkIjipGztTQKdgR81HzSz3MQmYYcTOAO4bJGcAqsgAqUrqhocPi6uKbV8PvZmV3Z7u7rzy0rbXDFkyjmhUQHdEhoXZbl7GUyoI+mvz7WBHKKzmkevtVwnC2wZ+6/RKa6BCqmRALMU/bNgjnaTiYWrt++Zsk= 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=sg0bko44; arc=none smtp.client-ip=209.85.128.73 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="sg0bko44" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-45a1b10418aso29558155e9.3 for ; Tue, 19 Aug 2025 02:09:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755594541; x=1756199341; 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=YVk9a2k32/h6AWhwvz8AjLIHmDt75XL0D5Be34oDUlo=; b=sg0bko44sJDUYHrBCLSYVahhF6x0qEz6BJHINEOzrA9qoe2zGwE1nn7c43Lb0j9yFr pOokajXSTvsB8UcybNwX2X/fzoS0+UUPlScGPC1dGa362ZLsfRGnz1/ZUPl80VIlzT8/ HCXlPA6WN3fnWRHNtyJKr5GsGfi5XxG94IkgDC3qSJ7SSSivJLkVp0nPR+q4r/erckRd /Agry6SeZiFsOE+XAeTE0mEI0JpvdP9ti/11mP8OaJKuautYAhp45Fmox5ytHm4qPrik WI78kXXgt/8xSYUdyMFGQc1cs6uKa81nCc2qlA8u/aUrqWJANtKIAkfRj+dLguDVJtPA 9SRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755594541; x=1756199341; 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=YVk9a2k32/h6AWhwvz8AjLIHmDt75XL0D5Be34oDUlo=; b=cbt3JbcqwqJIN1h0E3Co+RSxJeB2CydLiFg/E0Drf/FLVbBTGjmFyKKRnqGXwJ9d6Y HlvKMd695T5ewiUGDtrJjw/tbM0Np4DHXxNJYpIroNrB20PZr0hksq7jBM9WZpbUTTO0 7xS3VXxmnuGYhliKxRlJnzJeOEahn3t5rpEZERMpbpDoYhXfJS/baR6vgoLUWvGzHXUU 87Drv8oRIUaC92R8fewNYzN+k/b0EbTUCDhI7C/j2BAcVsIz+XGTwyrQfTwJiVwrIsm9 1PZd7IaLR4bSz9k+ijH0gxXr+21KODCZi6ETVnlzfMRLr89M6lMp0Irf7HoOgLWtRCRd lpvg== X-Forwarded-Encrypted: i=1; AJvYcCU7laWfDT3OBCBrlCd+f4U01UwTwDuKz8xsfXpuAzS4EXXOc4r/YoKxHRs0zHonRrWgZFqHnyBKio9GeBg=@vger.kernel.org X-Gm-Message-State: AOJu0YxAOeZWdV5ZvXsqL92WPopubknM2D24+4sO41uzJfJlGfuW+TlE 8+9vkzJi5R0QmM+c8Fb8HIyOZDLXB1M3XtO3yk7ZQVOB5Yzo9/gq8mkt4FjgsbB/+WZX5abcRHI eGw== X-Google-Smtp-Source: AGHT+IGTjX+cyo/FUylA2G0KujL7y3t2s4M6bbVMk0SnbkXB4aY/ErPaSd+f/dDH9fy1iC89j9dagd09LA== X-Received: from wmbel5.prod.google.com ([2002:a05:600c:3e05:b0:459:e01c:3d6d]) (user=keirf job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4e91:b0:456:19be:5cc with SMTP id 5b1f17b1804b1-45b43dd3118mr11414045e9.14.1755594540927; Tue, 19 Aug 2025 02:09:00 -0700 (PDT) Date: Tue, 19 Aug 2025 09:08:52 +0000 In-Reply-To: <20250819090853.3988626-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: <20250819090853.3988626-1-keirf@google.com> X-Mailer: git-send-email 2.51.0.rc1.193.gad69d77794-goog Message-ID: <20250819090853.3988626-4-keirf@google.com> Subject: [PATCH v3 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 Tested-by: syzbot@syzkaller.appspotmail.com --- arch/x86/kvm/vmx/vmx.c | 7 +++++++ include/linux/kvm_host.h | 10 +++++++--- virt/kvm/kvm_main.c | 33 +++++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index aa157fe5b7b3..2d3c8cb4f860 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 we are 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..4f35ae23ee5a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1103,6 +1103,15 @@ 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 +1237,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 +1285,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 +5852,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 +5876,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 +5895,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 +5945,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 +6054,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.rc1.193.gad69d77794-goog From nobody Sat Oct 4 08:05:13 2025 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (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 C203131B12D for ; Tue, 19 Aug 2025 09:09:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755594545; cv=none; b=Ob2g8Jsp4eMPiaf7+oeqW8yzshC0TgEMCKhVfLIzbpO4840Zfq4jngQnHTBOGwhuMwhCiY9D4ewSbcjCyIBZ93F2CLih93Zv7HVDi2o9MyEgC0SaxKOk4EpkzYyO6awdDoAwLOB/o1HixCiXkebW5YY+zR7If8cP0DDar72B3VQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755594545; c=relaxed/simple; bh=H2tOAJiT9+UKslPsSm2DpbqQjItGsMTWZFtsWcCliJo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NNneBp7ZFZsMqklF562b+7PJ+skc4K1Bo+1Eg7GDuxvmwok0bV0Dhtj7R7UyaAktahs57vFCFc5G0UrYgejTk4iBBmpEEAr2llo8p4EafS8/Ty2aLFcfo1kPP0ndeD54CzCX305k/NAb9vWuFFj1XjgE9tqqJ/zBAuQq4156yQU= 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=fYXNToH9; arc=none smtp.client-ip=209.85.221.73 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="fYXNToH9" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-3b9d41b779aso3277409f8f.0 for ; Tue, 19 Aug 2025 02:09:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755594542; x=1756199342; 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=Dryunec7C15FT43WsTYqyK8BC2tH9vRzD5ncvT1VTyI=; b=fYXNToH9tEqgbEMXVOP5e5crtcIxIxr+Uk7zs27IWvQhA5rlV9cw2n5Gr4i1RSqO9u OSUsTNsfQw40GtiIQ8viYHgfpbsyxzt+Y4Z4OeLH4CqfSZBu6iT85sZ3N+lIfq0nJsmt OSHbojn6RtuhFGZrgsgTRoP5qrfN4Yu0TTnvhF+DHThf/qKRMmgbo/CmNIGlfYXGfjb5 h7baHupfizv+ggwN787/wgd9mvqjLHrFrLc530OfPDPrg/iseoFcii4f2xX4/PyBk8+I q18KOIViTOhkj61WHgqWOgyt3cxVfK/lqmXeQlZaGUNbpunOpwzodlVsbG/detztO2GL gObg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755594542; x=1756199342; 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=Dryunec7C15FT43WsTYqyK8BC2tH9vRzD5ncvT1VTyI=; b=NgggKVXwCWJNnBWdS2Xv/nIAxIuvzuukJ2yyBnKL42DxxEZefYY2NMiSPezpA5Okwz cEY8UGyKngrWJBACt3uWCb5BoSC3Y54v2BdEIJL1HpsMjL/06AGM8e0sF57GZZj5/yP4 6CXIANY7HOnuDcHGIwaP1FtUV8H82j4ncCX8EqiaARTsEkGlj/y5pgrBP/VJdXnpP0Ak kDVdA27pLPsw77WicsM6Ywua6ETRw4SJCxEHHwiNwmP2DNI1BLB4ggaVjfKXlb3EIwnt Z2vLSs3zLEIHsEWdP3GmLYBe5jazvkzfBH27FXnoAsJ6giiEa2gXJ7yTGg84C5lNOD9h DyHg== X-Forwarded-Encrypted: i=1; AJvYcCVteIJWWkX704Dz03GmyH+TOCO6MPxnh+GEx3Q+RwmjDlHrIKSGxP3ePfQWnGzDsEasLzKH5Fik8fwQHF0=@vger.kernel.org X-Gm-Message-State: AOJu0YwfpbTsoFHY/aQa/UG6x/+8GTsIeuK61fDhATDPyWiRIMg/gssO ON9H24kv8K3DboKBCAQADGSBlWHm51cpFED7C4bPGrj9HsA+fX9f0h64bMaxBQ6qCw8v55vRkTZ vpg== X-Google-Smtp-Source: AGHT+IEWZK/S3bmfcTBpMD/OWSO9i/fgVmwIt0LtH8csguKl7P4ZrXGSAdstuEmNYdGpRILLFl16F7mudg== X-Received: from wrqx7.prod.google.com ([2002:a5d:4907:0:b0:3b7:8342:dc50]) (user=keirf job=prod-delivery.src-stubby-dispatcher) by 2002:a5d:64ef:0:b0:3b7:9c28:f846 with SMTP id ffacd0b85a97d-3c0ed1f3372mr1452415f8f.44.1755594542204; Tue, 19 Aug 2025 02:09:02 -0700 (PDT) Date: Tue, 19 Aug 2025 09:08:53 +0000 In-Reply-To: <20250819090853.3988626-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: <20250819090853.3988626-1-keirf@google.com> X-Mailer: git-send-email 2.51.0.rc1.193.gad69d77794-goog Message-ID: <20250819090853.3988626-5-keirf@google.com> Subject: [PATCH v3 4/4] KVM: Avoid synchronize_srcu() in kvm_io_bus_register_dev() 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" Device MMIO registration may happen quite frequently during VM boot, and the SRCU synchronization each time has a measurable effect on VM startup time. In our experiments it can account for around 25% of a VM's startup time. Replace the synchronization with a deferred free of the old kvm_io_bus structure. Signed-off-by: Keir Fraser Tested-by: syzbot@syzkaller.appspotmail.com --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index e7d6111cf254..103be35caf0d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -206,6 +206,7 @@ struct kvm_io_range { struct kvm_io_bus { int dev_count; int ioeventfd_count; + struct rcu_head rcu; struct kvm_io_range range[]; }; =20 diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4f35ae23ee5a..9144a0b4a268 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5953,6 +5953,13 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_= bus bus_idx, gpa_t addr, } EXPORT_SYMBOL_GPL(kvm_io_bus_read); =20 +static void __free_bus(struct rcu_head *rcu) +{ + struct kvm_io_bus *bus =3D container_of(rcu, struct kvm_io_bus, rcu); + + kfree(bus); +} + int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t a= ddr, int len, struct kvm_io_device *dev) { @@ -5991,8 +5998,7 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm= _bus bus_idx, gpa_t addr, memcpy(new_bus->range + i + 1, bus->range + i, (bus->dev_count - i) * sizeof(struct kvm_io_range)); rcu_assign_pointer(kvm->buses[bus_idx], new_bus); - synchronize_srcu_expedited(&kvm->srcu); - kfree(bus); + call_srcu(&kvm->srcu, &bus->rcu, __free_bus); =20 return 0; } --=20 2.51.0.rc1.193.gad69d77794-goog