From: Will Deacon <will@kernel.org>
To: Fuad Tabba <tabba@google.com>
Cc: Quentin Perret <qperret@google.com>,
kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
maz@kernel.org, oliver.upton@linux.dev, mark.rutland@arm.com,
joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com,
catalin.marinas@arm.com, broonie@kernel.org,
vdonnefort@google.com
Subject: Re: [PATCH v2 3/4] KVM: arm64: Factor out pKVM hyp vcpu creation to separate function
Date: Mon, 3 Mar 2025 21:49:50 +0000 [thread overview]
Message-ID: <20250303214947.GA30619@willie-the-truck> (raw)
In-Reply-To: <CA+EHjTySqDHDpOy=Vix5ZaNKWg61coySvV3Yk=+i0p8vg3mXTQ@mail.gmail.com>
On Mon, Mar 03, 2025 at 07:21:33PM +0000, Fuad Tabba wrote:
> On Mon, 3 Mar 2025 at 19:18, Will Deacon <will@kernel.org> wrote:
> > On Mon, Mar 03, 2025 at 07:57:00AM +0000, Fuad Tabba wrote:
> > > On Fri, 28 Feb 2025 at 19:44, Quentin Perret <qperret@google.com> wrote:
> > > > On Wednesday 26 Feb 2025 at 21:55:19 (+0000), Fuad Tabba wrote:
> > > > > static int __pkvm_create_hyp_vm(struct kvm *host_kvm)
> > > > > {
> > > > > - size_t pgd_sz, hyp_vm_sz, hyp_vcpu_sz;
> > > > > + size_t pgd_sz, hyp_vm_sz;
> > > > > struct kvm_vcpu *host_vcpu;
> > > > > - pkvm_handle_t handle;
> > > > > void *pgd, *hyp_vm;
> > > > > unsigned long idx;
> > > > > int ret;
> > > > > @@ -161,33 +178,12 @@ static int __pkvm_create_hyp_vm(struct kvm *host_kvm)
> > > > > if (ret < 0)
> > > > > goto free_vm;
> > > > >
> > > > > - handle = ret;
> > > > > + WRITE_ONCE(host_kvm->arch.pkvm.handle, ret);
> > > >
> > > > What's the reason to make this a WRITE_ONCE? Does it mean we should
> > > > update the readers to be READ_ONCE()?
> > >
> > > I don't remember the original reason, to be honest. In this case, it
> > > was to make it consistent with downstream code in Android. That said,
> > > I plan on revising all of these soon and fixing this (and related
> > > code) in light of Will's comment regarding potential specter gadgets:
> > >
> > > https://lore.kernel.org/all/20250218092705.GA17030@willie-the-truck/
> >
> > I'm not sure the spectre stuff changes the concurrency aspects here, so
> > Quentin's question presumably still stands even after that.
> >
> > Looking at the Android code, this WRITE_ONCE() pairs with a READ_ONCE()
> > in pkvm_is_hyp_created() which is called without the config_lock held
> > by kvm_arch_prepare_memory_region(). However, given that
> > pkvm_is_hyp_created() is only testing against 0, I don't think the
> > _ONCE() accessors are doing anything useful in that case.
> >
> > The more confusing stuff is where 'kvm->arch.pkvm.handle' is read
> > directly without the 'config_lock' held. The MMU notifiers look like
> > they can do that, so I wonder if there's a theoretical race where they
> > can race with first run and issue TLB invalidation with the wrong handle?
> > That would apply equally to the upstream code, I think.
>
> Would using _ONCE accessors with the MMU notifiers be enough to avoid
> the race, or do we need to reconsider the lock protecting the handle
> and apply it to the notifiers?
I'm not entirely sure... if we used _ONCE() then we'd get either the
correct handle or zero, but we presumably need to order that against the
page-table somehow. The 'mmu_lock' looks like it gives us that, but I
don't think the notifiers are expecting an uninitialised handle in the
case where the page-table is empty (i.e. if they fire before first run).
Given that the handle is necessary for TLB invalidation, I'd be inclined
to make sure that the handle is allocated and published _before_ the
kvm_pgtable pointer checked by the notifiers is set, but that means
moving the handle allocation into kvm_arch_init_vm(). Is that do-able?
Will
next prev parent reply other threads:[~2025-03-03 21:51 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-26 21:55 [PATCH v2 0/4] KVM: arm64: Fix initializing HCRX_EL2 and other traps in pKVM Fuad Tabba
2025-02-26 21:55 ` [PATCH v2 1/4] KVM: arm64: Factor out setting HCRX_EL2 traps into separate function Fuad Tabba
2025-02-26 21:55 ` [PATCH v2 2/4] KVM: arm64: Initialize HCRX_EL2 traps in pKVM Fuad Tabba
2025-02-26 21:55 ` [PATCH v2 3/4] KVM: arm64: Factor out pKVM hyp vcpu creation to separate function Fuad Tabba
2025-02-28 19:44 ` Quentin Perret
2025-03-03 7:57 ` Fuad Tabba
2025-03-03 19:18 ` Will Deacon
2025-03-03 19:21 ` Fuad Tabba
2025-03-03 21:49 ` Will Deacon [this message]
2025-03-04 12:33 ` Fuad Tabba
2025-03-12 15:29 ` Will Deacon
2025-03-12 15:31 ` Fuad Tabba
2025-03-14 11:14 ` Will Deacon
2025-02-26 21:55 ` [PATCH v2 4/4] KVM: arm64: Create each pKVM hyp vcpu after its corresponding host vcpu Fuad Tabba
2025-02-27 12:09 ` Marc Zyngier
2025-02-27 12:47 ` Fuad Tabba
2025-02-27 14:13 ` Marc Zyngier
2025-02-27 14:13 ` [PATCH v2 0/4] KVM: arm64: Fix initializing HCRX_EL2 and other traps in pKVM Marc Zyngier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250303214947.GA30619@willie-the-truck \
--to=will@kernel.org \
--cc=broonie@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=joey.gouly@arm.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=qperret@google.com \
--cc=suzuki.poulose@arm.com \
--cc=tabba@google.com \
--cc=vdonnefort@google.com \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox