From nobody Tue Oct 7 00:22:38 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 470402F3C36 for ; Wed, 16 Jul 2025 11:07:46 +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=1752664069; cv=none; b=MtR/hXTZvAoZkl3zTt1JWIzPVgkGa0CisAQ9BZP538c1hhE7vM9UbPxH/jOn7189hTPD0UHHy4i7ycoI4zdzquyQE0KsL4IXManW2Hjk0c8Xt4bDMxcGLfm74YZFZkTjqkOBXb+KXzFcvY/eMgadqGA4b4LINftuALRBR02EeTs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752664069; c=relaxed/simple; bh=7MCXu2XIExm1XsO3hQiAoT4CvnT5CB3Dy282TZ97t3U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rADBJ8jh9M2+4zrceNZ+eFAqFYEGoLXx0yYAKsTlO5XpxEW8WKDPly1CH4vl96yxPHwDF0ZH0EHAAkZgTrmIIHTFbcio39OWCpUdjtihybYcV2tGv2IUfChwty0AWOM9nO5oM/LJ4imjzxWvPS296ESgt92G+youdbF22B+PVPE= 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=KGPB21K/; 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="KGPB21K/" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3a4f858bc5eso4896085f8f.0 for ; Wed, 16 Jul 2025 04:07:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752664064; x=1753268864; 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=lw9Wdz3TAUoCooOMv3X14CEUxWY0AuV/hf0NzcKNFE8=; b=KGPB21K/wFDrm3ArnevTOzbNa0dlmdb8VQFUxn1geeeW9/fwrbhbxjMWWystOnUPOA RShHNYAmZ2Xe8ypB8PPRaeBMe+eSHG1yUgf76MQDC8cOrnwLwNK/rF2ZiQna9lk3HLyS 1CR993ZYB+wyZulszyisgY2S6esPjON/a1LiAPqP2KJv0N/d7nP0C8bT4IZfAlF4H48Q WiGvY1bZ3p82EeMnxgGfo13kifv+HuQB/NMkLwtOUEx2QYqJhMAm4NZ1x2/9EhilkCB0 OGeM/TbPxdAKq6mCaJNAApv461aRMttAIJ+/hJQ9oPn/Gk5f3ohaQsBQv9HUAmqpb9+O LUQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752664064; x=1753268864; 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=lw9Wdz3TAUoCooOMv3X14CEUxWY0AuV/hf0NzcKNFE8=; b=jRCfAfLpMQC7OpCMWHGywqZ+ebkPEWw+CNU1w38PDRjMnoN5cjBh2UEsFlph0LfU5j Am7GFUp1crHRTBXNl6lPgE4lg76kAy4IGzzkvNvxYzqRi12Bw5LJ4Mt8qeHCaLnikTQf xVxS2g8Gv8tMoQKPUtV1zyXMMzm94hb4coWs5ZgLjfoLH1UNLVL+IhFAQ81rilNV/wn9 eTYBPoLy167uZp+nu50GamLtUTN0DbgH4Q4zuUbDMfVEGiFDOr/5Ig6T8eZqCpPusUjO 8xUUAUmqsDvdZNygrLNfWLQCjsZfoYdQ08lbYQ2iYwBUqgpba6MxZR3tuFj9AF4pxCZh 9osA== X-Forwarded-Encrypted: i=1; AJvYcCXBlWlM0sOh1T005zP5xv2iiJzfD4TQMV45+q95WvnODXKF3vYsCsKQq+bEqDw0fqUa8bam8rrS7MAx1Tc=@vger.kernel.org X-Gm-Message-State: AOJu0Yyp25s0BtwBbFvEK6eFtpSalTW1e6fY2fC7pHtUxZsFh1GVoJYV 8euAoQness1CfjM7PJJLRAGktZMMlgyLA0oXHzWVwjy91+WP703DIece4inkMmXGgN/Hp53aoa2 QEQ== X-Google-Smtp-Source: AGHT+IHuNxCrx7B1F7XbHUaYm3AAwwPyLKTJsfFZ+D2KSuL1269JyI8e442f/hH+yv6mKmRQSSBPsJCPSA== X-Received: from wmbek10.prod.google.com ([2002:a05:600c:3eca:b0:451:4d6b:5b7e]) (user=keirf job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3481:b0:43c:fcbc:9680 with SMTP id 5b1f17b1804b1-4562e3c4b8bmr18712195e9.25.1752664064367; Wed, 16 Jul 2025 04:07:44 -0700 (PDT) Date: Wed, 16 Jul 2025 11:07:34 +0000 In-Reply-To: <20250716110737.2513665-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: <20250716110737.2513665-1-keirf@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716110737.2513665-2-keirf@google.com> Subject: [PATCH v2 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 --- 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 eb1205654ac8..502b65049703 100644 --- a/arch/arm64/kvm/vgic/vgic-init.c +++ b/arch/arm64/kvm/vgic/vgic-init.c @@ -559,7 +559,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) { @@ -568,12 +567,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 4a34f7f0a864..233eaa6d1267 100644 --- a/include/kvm/arm_vgic.h +++ b/include/kvm/arm_vgic.h @@ -399,7 +399,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.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 00:22:38 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 9EDFA2F3C3E for ; Wed, 16 Jul 2025 11:07:47 +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=1752664070; cv=none; b=r9ssUCYkvTU6eFcj6pb6EJWWGrFv497/uX7YAQUXt7acfFtLLM6pYmHCDdBSmRyYYmf6m3Q+ZPJtiWeI0rA4/wGf28hS0Dg+eYaY9Z2quHHEFjHY7BCjvvkAuILyTmTTLsjs0xE6Dfk0ymYLtdAezvAnEvE5zokPyrCG/U5Phbw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752664070; c=relaxed/simple; bh=PrPZi9FLkMWKmIdJSQLq3BcigSFeFqZOp9S9N0GiAU8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=izI9vTEzQ/5yepsRmH1VbLclsWCHz66oxe+JSgoTcetVcVrt/B+bKzUJoEM4INmFTHq6ZG8mnqHJ/PRjd3imzOKs7os71rh6JMMRYzdYPgAKy8HS7HNu6CVw1XUhyxV4g5M1MbQvUM3E1zoSKFJExl/uLRezc0yGiNyk7u0XCsA= 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=QZ3BZ3oU; 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="QZ3BZ3oU" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-454dee17a91so50627445e9.3 for ; Wed, 16 Jul 2025 04:07:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752664066; x=1753268866; 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=Hxqszaeuljrqlm0t8DgnPuG7w9pgNEhhBTVGlWt0Bfs=; b=QZ3BZ3oUkA5fkjLHPoQ7w9WB+U8mzCKcAEeN0r8Iyqav47Z++28pidd4psSSn/x8V4 poA1PNJO9vGFIzQEGMey0jzvcgnsMnAsiaxZybhVZPKqAPJldexQNKA8MBKjxcrvYxm9 zCN5m5sNokork1u9xiDzhF23F0cNV/zX2xEzLZmjAK5hBC90emprwHkoJWtiZWrhahv9 U4mj3AXpurSCJiI6lyoKrTyxUuNJUVhuTXYVNA/oYZsznxg5Zzr1HCkCyrOAg9BkiRvT e88onoQZYFS3OWSmk1HvkklD6L/PGLg6gHNCWz+wcYYXj/3V5GcHwNfwRMoGba9RpoBF ibng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752664066; x=1753268866; 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=Hxqszaeuljrqlm0t8DgnPuG7w9pgNEhhBTVGlWt0Bfs=; b=C6uk8Cv62ttx5JQYNqzcGLI4Q8yrAVlHYNontZvF9LNT5eSOr9CHr/ClqI4E0A9j40 jwSuq2G9mmL2q5cRkrNReDazgWSyruTanGb7N82HP1akPiZMQ2rCptLwtI2yi/nALGfL m5xt1Do+eiCO/RFjIa+JagXgnRb5dQWSIDANkU2dLLzEDUAvKk2dXANCBNJ4N7DcWmtU 5P8IuFfcaL6rql+6d1bURZswzddLRi75iFNQfaocnnQ/FkjD1FaQRmEKE8cpBlDHUqh1 6srDbuEfsi6xixnY+w+vvTMx8UlA7NJufZcekiTTvUlrl8C10gnaGUN/wvT2t8MBTxxX tfqQ== X-Forwarded-Encrypted: i=1; AJvYcCUdx53uKw4lud3SmLAonDu8Odf1Tp0Q+/CVVBT5R+fIBdQWgoOpNy37R9iIDgRMkY+LzBDQ42Gfv0JAnFc=@vger.kernel.org X-Gm-Message-State: AOJu0YyGSKdA49BQQKQJ5bN06TOUwBPdT5DWtuFmzGystkfhvizrR6LU lJn0XzHqLjuXL5cyh0IbfpTAGoUZCEu3VjgekocJaHjvCl5omlYtjDkoZnNdHd/3csES7Ouplp7 l5A== X-Google-Smtp-Source: AGHT+IEc90RI4GCOsQRZO8PqFs/HKd6dYrEM+oA0RTzrbntMXuFw8OS9F/kKOUPw5mrVP3l/ZU5GEG7EJA== X-Received: from wmqa10.prod.google.com ([2002:a05:600c:348a:b0:455:76a8:b3a]) (user=keirf job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5299:b0:456:1c4a:82ca with SMTP id 5b1f17b1804b1-4562e3c227cmr18678235e9.32.1752664065812; Wed, 16 Jul 2025 04:07:45 -0700 (PDT) Date: Wed, 16 Jul 2025 11:07:35 +0000 In-Reply-To: <20250716110737.2513665-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: <20250716110737.2513665-1-keirf@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716110737.2513665-3-keirf@google.com> Subject: [PATCH v2 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 --- 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 502b65049703..bc83672e461b 100644 --- a/arch/arm64/kvm/vgic/vgic-init.c +++ b/arch/arm64/kvm/vgic/vgic-init.c @@ -567,7 +567,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); @@ -598,14 +598,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.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 00:22:38 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 CE8A52F431A for ; Wed, 16 Jul 2025 11:07:48 +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=1752664070; cv=none; b=gvigZJcgXTDszrXdi8gT09jUiLzkri8nATZyElx0qAFhUiJz37pIocjOBfb47neQVm1GA6cHskbK/ttFnm6RoU6ZocttWEH4VBoR5SMofFIbF3bA6VnwjsKfV1Z8jhK0mU2F/ShPfFik8rESLBj/2QOFo3cF5ckY68WGOK/+LDQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752664070; c=relaxed/simple; bh=bp/5ZyCkFH6I6+8VSptNOyl0ICot7qcphUT+rW69J2I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PaQIGaP//E5KMYi1eaNtFn4x6v9FBbuPs6jofIeFBk2/Ua5wvpb/7tynyx8gmSW8gHlVeZUAEcxCjMoNOHFIfhmLe4iF7YcIuJotxgq+HyXf7Vjti9ry9RasRaVfqYw5MJNWhBezW0bfVan4qq7o8+mm12gSD+t1buAmAxWNZ9s= 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=aq14s6dg; 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="aq14s6dg" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-455eda09c57so34670445e9.2 for ; Wed, 16 Jul 2025 04:07:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752664067; x=1753268867; 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=DkXH9+Ju9qLefqDmLXr88CY0f/QuBhkcRpVqVhxrcMo=; b=aq14s6dgk9teEly7y0VydG+qFPGKTz6+qSoLKD0O9GQj4NToKUpegJeot8h7TzZZzd OAj3fVyItf4C9MsoRpF5bNxUAQ5fX7LXQPAiHW3GxqmB6dnyWhQ7KIo/wkiLPnkN4t5d 2XIXMeyGtVlsUDmfiKW8/uYIF8xRel/yTlILq1jNGPVN0K+e2qpYHUzU+ijTnpuamlIh 80z5pkZHnEVQU+yxEljN6j9T4HiufCRVTzrX4mkoab1mA+k3K+MvmDXe9VwHXYy/DtpA 8w9qybfWCbDD5i3q4PtJL3xcHjIf3DvBTw89lcQaNTh/BSJqf//TsqbvQunrmVW8Kofq N9vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752664067; x=1753268867; 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=DkXH9+Ju9qLefqDmLXr88CY0f/QuBhkcRpVqVhxrcMo=; b=FfJl/Cwi0kdVt+h8TUBorm41nnX4eTHpqdd0KF9Fod0WgzCgAv453LsDMPD13VxSd1 dqKKJxmNxPSkVrwavb8LQ5gZO0+GHlg861H5WttY0f1Wi1trriRYM1N7ORx79B2ZN90T Djz++/p952cYakm4om/OO3i3UukaFuVOhGnQKLEuF6k/MvgJuwvhLAPHoBIbV5nhq+gf XkP8n897Evs9kqOLIjHAv2BVwndLFjA8cJXfTOBqi0i4FIVNmwsdZJ12Rbax6YQo6x0r zU1iEpfb8Ly64ptqAXXDop6SpO1JFtL1lAIr3W7l/RHjFc8G+ZtPplijO9g+5Lro1z2W 5hIw== X-Forwarded-Encrypted: i=1; AJvYcCVz1tvhBTMa0to4ETVB+csa0+iyeMb64el9KRAu/U0AHR8ESguSnc3txZ08XIGNhrB/OtHcHmWX0kAifgU=@vger.kernel.org X-Gm-Message-State: AOJu0YwwhhaCtkEG9Mtah06OXmUus3V3XPsV1XV30tqoqw/pZa/gyAuI SbUYq/Idk2P/h38HEILaKOPWo4aDw25t2T9VNL3cwcSfezRG4ihcQlO8Z2JCi14tvJ6E61JK2Lh Mnw== X-Google-Smtp-Source: AGHT+IFpoWhoafJFWbwsE4WRdqTI2xCnHqs5MSPVmR8sRs6DH+HGmbw1n5hUPgrnRCYka3njSht1T7VPaw== X-Received: from wmby19.prod.google.com ([2002:a05:600c:c053:b0:456:1b6f:c878]) (user=keirf job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3e15:b0:439:86fb:7340 with SMTP id 5b1f17b1804b1-4562e39865dmr26121765e9.30.1752664067193; Wed, 16 Jul 2025 04:07:47 -0700 (PDT) Date: Wed, 16 Jul 2025 11:07:36 +0000 In-Reply-To: <20250716110737.2513665-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: <20250716110737.2513665-1-keirf@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716110737.2513665-4-keirf@google.com> Subject: [PATCH v2 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 | 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 191a9ed0da22..425e3d8074ab 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5861,6 +5861,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 3bde4fb5c6aa..9132148fb467 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -965,11 +965,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 222f0e894a0c..9ec3b96b9666 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); @@ -5838,6 +5847,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) { @@ -5850,7 +5871,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); @@ -5869,7 +5890,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 @@ -5919,7 +5940,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); @@ -6028,7 +6049,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.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 00:22:38 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 21B142F433C for ; Wed, 16 Jul 2025 11:07:50 +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=1752664071; cv=none; b=GMB6dmd8GuglslXP87LVB6FPHXzfgDpGI8kmYVBxpKhD5hLINp7QfMy9LkrrZBZTr3J9whwO23YzErDoWNGkWeX6cVB6rgERtwbypfQhlRoTUuzVLWL5xiGSXAwt9TMrNCKrAUHequ7TyR7V8nRLmXqy1bmBUHly0MwyKe5UwH8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752664071; c=relaxed/simple; bh=SwRbVQZAJVuZZ3KPArKlcrn8a8LengWToebo4ivdzPo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fwbbg45qzUfW80scpyQwRI5u5KGnnP3veFRcSZ1a0aOvrK2fTcMu1Ajj2sKhe/wZ2QxN7PhmrXH483kK9pj7Cso0lc3kLQ2PamA4q+FJfF+3dqghccSViO+tdrlXerTotQUPQHEHfI39Po/V5cdZ9q28kUHsrBaLEpsRcJLMbM4= 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=3ZTgm0gA; 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="3ZTgm0gA" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3a52bfda108so3166697f8f.3 for ; Wed, 16 Jul 2025 04:07:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752664069; x=1753268869; 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=bFiBh24vfyxUkvL/qAF91ZLaWMijXEEuq00t+Y6HBLE=; b=3ZTgm0gAiZmMJU+oBt2B575JcO1wGtvdpQ9FrcIeml0lmRslMfAPKZGeL9D9NoL8pZ rPhFECcZ98Wdu8mk0a6KfRsvGYPlPmmi0jBK/b8/vaZOiolxrz1Xj4bLftBszW5HwYCW Rj8eqQOm2XuCtDDy51GdpCg9EmgAjrhUfw/KxRiDbjTZ7A595qLGfQTHH0dD6hxtf/BG eyeEImos8O5UZ4YyCGJs9u7D6Xhq835577Vq7ZeJbDrm3DG4dXCKqJEs6fUcLa1SICYE gy8iTgy4InENVuloePP9+8MTT8VVsLoLGeTzcpOcmCCDoqpF/CcuJRL01RfTCXGtzvr9 IZHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752664069; x=1753268869; 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=bFiBh24vfyxUkvL/qAF91ZLaWMijXEEuq00t+Y6HBLE=; b=ix0PAn1a6RRt/ayw0KrZI9JYfVjwzSCaj0e7DbINA5mKD403iHpVH0fLKiBFegIgOU WBWkKZRIKBpLZ0gswzGQdi7Db1dRO2vGrbCg7CAEkYdn+N0AahBKUVWBzeUzz/gw9Fbh 9vScxY15+9NtfzP+KHY72/lgddstjFWyAMsVRvF4z7qHNHzyk0vPtCx27eGIvd5eSJLr L4Uf4Z/vuAn8J8YGvS3nC7eA+AEgcR8GmrFYrjCuMp56+LsAK9Gc8K7b1Eh7rGaSE2wr LVkhQVJgPKsCHV/N49KKxV+EMg2ki2BZu+KL0/vPl5Ta/gpHJ4zFoWRmS0cOLc3TL6Uo kJdw== X-Forwarded-Encrypted: i=1; AJvYcCXZcdVgYhR0ftqNFoLSjbGMPUaoeloxKIin2645FaMhfPQYR2bIay/HHqUFHj7ECSx+q9RgLWlsiy2O5aM=@vger.kernel.org X-Gm-Message-State: AOJu0Yye+FN3KD+aDDPPamPt+bEyNJW9N1MVWxcZNVX0pDbz0Sh/gMwk udor18O55E2f47T7mIrfsRvnJzHDli9t6KR6XKBUHCDQ2bAwm7y2dF/hF4co7iVIwgUL77LksU+ rwg== X-Google-Smtp-Source: AGHT+IHS3mDNKI5f7HxVRvsGrhgPtKyzq6M9m0VkLvornMAR5mJ3Yg4oC8wem/+qjRBiPoyYxBKnOYGMMA== X-Received: from wmqd13.prod.google.com ([2002:a05:600c:34cd:b0:456:23aa:8bf]) (user=keirf job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2486:b0:3a5:232c:6976 with SMTP id ffacd0b85a97d-3b60dd99677mr1925159f8f.44.1752664068593; Wed, 16 Jul 2025 04:07:48 -0700 (PDT) Date: Wed, 16 Jul 2025 11:07:37 +0000 In-Reply-To: <20250716110737.2513665-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: <20250716110737.2513665-1-keirf@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250716110737.2513665-5-keirf@google.com> Subject: [PATCH v2 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 --- 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 9132148fb467..802ca46f7537 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -205,6 +205,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 9ec3b96b9666..f690a4997a0f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5948,6 +5948,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) { @@ -5986,8 +5993,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.50.0.727.gbf7dc18ff4-goog