All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marc Zyngier <maz@kernel.org>
To: Oliver Upton <oliver.upton@linux.dev>
Cc: kvmarm@lists.linux.dev, Joey Gouly <joey.gouly@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Zenghui Yu <yuzenghui@huawei.com>
Subject: Re: [PATCH 2/2] KVM: arm64: Ensure vgic_ready() is ordered against MMIO registration
Date: Thu, 17 Oct 2024 08:58:30 +0100	[thread overview]
Message-ID: <86ttdb40ux.wl-maz@kernel.org> (raw)
In-Reply-To: <20241017001947.2707312-3-oliver.upton@linux.dev>

On Thu, 17 Oct 2024 01:19:47 +0100,
Oliver Upton <oliver.upton@linux.dev> wrote:
> 
> kvm_vgic_map_resources() prematurely marks the distributor as 'ready',
> potentially allowing vCPUs to enter the guest before the distributor's
> MMIO registration has been made visible.
> 
> Plug the race by marking the distributor as ready only after MMIO
> registration is completed. Rely on the implied ordering of
> synchronize_srcu() to ensure the MMIO registration is visible before
> vgic_dist::ready. This also means that writers to vgic_dist::ready are
> now serialized by the slots_lock, which was effectively the case already
> as all writers held the slots_lock in addition to the config_lock.
> 
> Fixes: 59112e9c390b ("KVM: arm64: vgic: Fix a circular locking issue")
> Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
> ---
>  arch/arm64/kvm/vgic/vgic-init.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c
> index d88fdeaf6144..48c952563e85 100644
> --- a/arch/arm64/kvm/vgic/vgic-init.c
> +++ b/arch/arm64/kvm/vgic/vgic-init.c
> @@ -544,14 +544,23 @@ int kvm_vgic_map_resources(struct kvm *kvm)
>  	if (ret)
>  		goto out;
>  
> -	dist->ready = true;
>  	dist_base = dist->vgic_dist_base;
>  	mutex_unlock(&kvm->arch.config_lock);
>  
>  	ret = vgic_register_dist_iodev(kvm, dist_base, type);
> -	if (ret)
> +	if (ret) {
>  		kvm_err("Unable to register VGIC dist MMIO regions\n");
> +		goto out_slots;
> +	}
>  
> +	/*
> +	 * 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 = true;
>  	goto out_slots;
>  out:
>  	mutex_unlock(&kvm->arch.config_lock);

I thought we would benefit from the removal of the early-out at the
beginning of the function, specially given that this is only called on
the first run of any vcpu -- and we can probably afford to take the
locks on such a rare event. My impression is that we'd gain in
robustness and maintainability.

This can come as a later patch if we agree that this is valuable.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.

  reply	other threads:[~2024-10-17  7:58 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-17  0:19 [PATCH 0/2] KVM: arm64: Ensure VGIC is fully initialized before entering guest Oliver Upton
2024-10-17  0:19 ` [PATCH 1/2] KVM: arm64: vgic: Don't check for vgic_ready() when setting NR_IRQS Oliver Upton
2024-10-17  0:19 ` [PATCH 2/2] KVM: arm64: Ensure vgic_ready() is ordered against MMIO registration Oliver Upton
2024-10-17  7:58   ` Marc Zyngier [this message]
2024-10-17 15:53     ` Oliver Upton
2024-10-17  0:22 ` [PATCH 0/2] KVM: arm64: Ensure VGIC is fully initialized before entering guest Oliver Upton
2024-10-17  8:23 ` 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=86ttdb40ux.wl-maz@kernel.org \
    --to=maz@kernel.org \
    --cc=joey.gouly@arm.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=oliver.upton@linux.dev \
    --cc=suzuki.poulose@arm.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.