[PATCH 3/5] KVM: arm64: Move double-checked lock to kvm_vgic_map_resources()
Marc Zyngier
maz at kernel.org
Sun Dec 27 09:36:06 EST 2020
On Mon, 14 Dec 2020 12:55:51 +0000,
Auger Eric <eric.auger at redhat.com> wrote:
>
> Hi Alexandru,
>
> On 12/1/20 4:01 PM, Alexandru Elisei wrote:
> > kvm_vgic_map_resources() is called when a VCPU if first run and it maps all
> > the VGIC MMIO regions. To prevent double-initialization, the VGIC uses the
> > ready variable to keep track of the state of resources and the global KVM
> > mutex to protect against concurrent accesses. After the lock is taken, the
> > variable is checked again in case another VCPU took the lock between the
> > current VCPU reading ready equals false and taking the lock.
> >
> > The double-checked lock pattern is spread across four different functions:
> > in kvm_vcpu_first_run_init(), in kvm_vgic_map_resource() and in
> > vgic_{v2,v3}_map_resources(), which makes it hard to reason about and
> > introduces minor code duplication. Consolidate the checks in
> > kvm_vgic_map_resources(), where the lock is taken.
> >
> > No functional change intended.
> >
> > Signed-off-by: Alexandru Elisei <alexandru.elisei at arm.com>
> > ---
> > arch/arm64/kvm/arm.c | 8 +++-----
> > arch/arm64/kvm/vgic/vgic-init.c | 6 ++++++
> > arch/arm64/kvm/vgic/vgic-v2.c | 3 ---
> > arch/arm64/kvm/vgic/vgic-v3.c | 3 ---
> > 4 files changed, 9 insertions(+), 11 deletions(-)
> >
> > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
> > index 9d69d2bf6943..65a5e89f5133 100644
> > --- a/arch/arm64/kvm/arm.c
> > +++ b/arch/arm64/kvm/arm.c
> > @@ -530,11 +530,9 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
> > * Map the VGIC hardware resources before running a vcpu the
> > * first time on this VM.
> > */
> > - if (unlikely(!vgic_ready(kvm))) {
> > - ret = kvm_vgic_map_resources(kvm);
> > - if (ret)
> > - return ret;
> > - }
> > + ret = kvm_vgic_map_resources(kvm);
> > + if (ret)
> > + return ret;
> > } else {
> > /*
> > * Tell the rest of the code that there are userspace irqchip
> > diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c
> > index 32e32d67a127..a2f4d1c85f00 100644
> > --- a/arch/arm64/kvm/vgic/vgic-init.c
> > +++ b/arch/arm64/kvm/vgic/vgic-init.c
> > @@ -428,7 +428,13 @@ int kvm_vgic_map_resources(struct kvm *kvm)
> > struct vgic_dist *dist = &kvm->arch.vgic;
> > int ret = 0;
> >
> > + if (likely(vgic_ready(kvm)))
> > + return 0;
> > +
> > mutex_lock(&kvm->lock);
> > + if (vgic_ready(kvm))
> > + goto out;
> > +
> While we are at it, the setting of dist->ready may be moved in
> kvm_vgic_map_resources() too.
Good point. FWIW, I have added an extra patch on top to that effect.
Thanks,
M.
--
Without deviation from the norm, progress is not possible.
More information about the linux-arm-kernel
mailing list