From: Andre Przywara <andre.przywara@arm.com>
To: Marc Zyngier <marc.zyngier@arm.com>,
Christoffer Dall <christoffer.dall@linaro.org>,
Eric Auger <eric.auger@redhat.com>
Cc: linux-arm-kernel@lists.infradead.org,
kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org
Subject: Re: [PATCH v9 06/17] KVM: arm/arm64: VGIC: add refcounting for IRQs
Date: Wed, 13 Jul 2016 14:50:25 +0100 [thread overview]
Message-ID: <f9aeb32e-ff1a-ab2e-945c-0e8e531e43b9@arm.com> (raw)
In-Reply-To: <578630E4.3000305@arm.com>
Hi Marc,
On 13/07/16 13:15, Marc Zyngier wrote:
> On 13/07/16 02:58, Andre Przywara wrote:
>> In the moment our struct vgic_irq's are statically allocated at guest
>> creation time. So getting a pointer to an IRQ structure is trivial and
>> safe. LPIs are more dynamic, they can be mapped and unmapped at any time
>> during the guest's _runtime_.
>> In preparation for supporting LPIs we introduce reference counting for
>> those structures using the kernel's kref infrastructure.
>> Since private IRQs and SPIs are statically allocated, the refcount never
>> drops to 0 at the moment, but we increase it when an IRQ gets onto a VCPU
>> list and decrease it when it gets removed.
>> This introduces vgic_put_irq(), which wraps kref_put and hides the
>> release function from the callers.
>>
>> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
>> ---
>> include/kvm/arm_vgic.h | 1 +
>> virt/kvm/arm/vgic/vgic-init.c | 2 ++
>> virt/kvm/arm/vgic/vgic-mmio-v2.c | 8 ++++++
>> virt/kvm/arm/vgic/vgic-mmio-v3.c | 20 +++++++++------
>> virt/kvm/arm/vgic/vgic-mmio.c | 25 ++++++++++++++++++-
>> virt/kvm/arm/vgic/vgic-v2.c | 1 +
>> virt/kvm/arm/vgic/vgic-v3.c | 1 +
>> virt/kvm/arm/vgic/vgic.c | 53 ++++++++++++++++++++++++++++++++--------
>> virt/kvm/arm/vgic/vgic.h | 1 +
>> 9 files changed, 94 insertions(+), 18 deletions(-)
>
> [...]
>
>> --- a/virt/kvm/arm/vgic/vgic.c
>> +++ b/virt/kvm/arm/vgic/vgic.c
>> @@ -48,13 +48,20 @@ struct vgic_global __section(.hyp.text) kvm_vgic_global_state;
>> struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
>> u32 intid)
>> {
>> - /* SGIs and PPIs */
>> - if (intid <= VGIC_MAX_PRIVATE)
>> - return &vcpu->arch.vgic_cpu.private_irqs[intid];
>> + struct vgic_dist *dist = &kvm->arch.vgic;
>> + struct vgic_irq *irq;
>>
>> - /* SPIs */
>> - if (intid <= VGIC_MAX_SPI)
>> - return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
>> + if (intid <= VGIC_MAX_PRIVATE) { /* SGIs and PPIs */
>> + irq = &vcpu->arch.vgic_cpu.private_irqs[intid];
>> + kref_get(&irq->refcount);
>> + return irq;
>> + }
>> +
>> + if (intid <= VGIC_MAX_SPI) { /* SPIs */
>> + irq = &dist->spis[intid - VGIC_NR_PRIVATE_IRQS];
>> + kref_get(&irq->refcount);
>> + return irq;
>> + }
>
> I'm a bit concerned by the fact that we perform the refcounting on
> objects that shouldn't be concerned by it. None of the static interrupts
> should have to suffer from the overhead, as they cannot be freed.
> So I came up with the following patch:
>
> diff --git a/virt/kvm/arm/vgic/vgic.c b/virt/kvm/arm/vgic/vgic.c
> index c7cd1a3..6bbff9a 100644
> --- a/virt/kvm/arm/vgic/vgic.c
> +++ b/virt/kvm/arm/vgic/vgic.c
> @@ -91,19 +91,12 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
> u32 intid)
> {
> struct vgic_dist *dist = &kvm->arch.vgic;
> - struct vgic_irq *irq;
>
> - if (intid <= VGIC_MAX_PRIVATE) { /* SGIs and PPIs */
> - irq = &vcpu->arch.vgic_cpu.private_irqs[intid];
> - kref_get(&irq->refcount);
> - return irq;
> - }
> + if (intid <= VGIC_MAX_PRIVATE) /* SGIs and PPIs */
> + return &vcpu->arch.vgic_cpu.private_irqs[intid];
>
> - if (intid <= VGIC_MAX_SPI) { /* SPIs */
> - irq = &dist->spis[intid - VGIC_NR_PRIVATE_IRQS];
> - kref_get(&irq->refcount);
> - return irq;
> - }
> + if (intid <= VGIC_MAX_SPI) /* SPIs */
> + return &dist->spis[intid - VGIC_NR_PRIVATE_IRQS];
>
> if (intid >= VGIC_MIN_LPI) /* LPIs */
> return vgic_get_lpi(kvm, intid);
> @@ -112,6 +105,14 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
> return NULL;
> }
>
> +static void vgic_get_irq_kref(struct vgic_irq *irq)
> +{
> + if (irq->intid < VGIC_MIN_LPI)
> + return;
> +
> + kref_get(&irq->refcount);
> +}
> +
> /*
> * We can't do anything in here, because we lack the kvm pointer to
> * lock and remove the item from the lpi_list. So we keep this function
> @@ -125,10 +126,10 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
> {
> struct vgic_dist *dist;
>
> - if (!kref_put(&irq->refcount, vgic_irq_release))
> + if (irq->intid < VGIC_MIN_LPI)
> return;
>
> - if (irq->intid < VGIC_MIN_LPI)
> + if (!kref_put(&irq->refcount, vgic_irq_release))
> return;
>
> dist = &kvm->arch.vgic;
> @@ -313,7 +314,11 @@ retry:
> goto retry;
> }
>
> - kref_get(&irq->refcount);
> + /*
> + * Grab a reference to the irq to reflect the fact that it is
> + * now in the ap_list.
> + */
> + vgic_get_irq_kref(irq);
> list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
> irq->vcpu = vcpu;
>
> @@ -473,8 +478,11 @@ retry:
> spin_unlock(&irq->irq_lock);
>
> /*
> - * This put matches the get when we added the LPI to
> - * the ap_list. We now drop the reference from the list.
> + * This vgic_put_irq call matches the
> + * vgic_get_irq_kref in vgic_queue_irq_unlock,
> + * where we added the LPI to the ap_list. As
> + * we remove the irq from the list, we drop
> + * also drop the refcount.
> */
> vgic_put_irq(vcpu->kvm, irq);
> continue;
>
> Thoughts?
Looks good to me, I checked the code and quickly tested it as well,
without any issues.
I'd integrate this in next respin.
Thanks!
Andre.
next prev parent reply other threads:[~2016-07-13 13:50 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-13 1:58 [PATCH v9 00/17] KVM: arm64: GICv3 ITS emulation Andre Przywara
2016-07-13 1:58 ` [PATCH v9 01/17] KVM: arm/arm64: move redistributor kvm_io_devices Andre Przywara
2016-07-13 1:58 ` [PATCH v9 02/17] KVM: arm/arm64: check return value for kvm_register_vgic_device Andre Przywara
2016-07-13 1:58 ` [PATCH v9 03/17] KVM: extend struct kvm_msi to hold a 32-bit device ID Andre Przywara
2016-07-13 10:28 ` Paolo Bonzini
2016-07-13 1:58 ` [PATCH v9 04/17] KVM: arm/arm64: extend arch CAP checks to allow per-VM capabilities Andre Przywara
2016-07-13 1:58 ` [PATCH v9 05/17] KVM: kvm_io_bus: add kvm_io_bus_get_dev() call Andre Przywara
2016-07-13 10:29 ` Paolo Bonzini
2016-07-13 1:58 ` [PATCH v9 06/17] KVM: arm/arm64: VGIC: add refcounting for IRQs Andre Przywara
2016-07-13 12:15 ` Marc Zyngier
2016-07-13 13:50 ` Andre Przywara [this message]
2016-07-13 1:58 ` [PATCH v9 07/17] irqchip: refactor and add GICv3 definitions Andre Przywara
2016-07-13 12:28 ` Marc Zyngier
2016-07-13 1:59 ` [PATCH v9 08/17] KVM: arm64: handle ITS related GICv3 redistributor registers Andre Przywara
2016-07-13 1:59 ` [PATCH v9 09/17] KVM: arm64: introduce ITS emulation file with MMIO framework Andre Przywara
2016-07-13 1:59 ` [PATCH v9 10/17] KVM: arm64: introduce new KVM ITS device Andre Przywara
2016-07-14 8:36 ` Marc Zyngier
2016-07-14 11:11 ` Andre Przywara
2016-07-14 12:57 ` Marc Zyngier
2016-07-15 9:33 ` Andre Przywara
2016-07-13 1:59 ` [PATCH v9 11/17] KVM: arm64: implement basic ITS register handlers Andre Przywara
2016-07-14 9:13 ` Marc Zyngier
2016-07-13 1:59 ` [PATCH v9 12/17] KVM: arm64: connect LPIs to the VGIC emulation Andre Przywara
2016-07-13 1:59 ` [PATCH v9 13/17] KVM: arm64: read initial LPI pending table Andre Przywara
2016-07-14 9:34 ` Marc Zyngier
2016-07-14 10:16 ` Andre Przywara
2016-07-13 1:59 ` [PATCH v9 14/17] KVM: arm64: allow updates of LPI configuration table Andre Przywara
2016-07-14 9:46 ` Marc Zyngier
2016-07-14 10:00 ` Andre Przywara
2016-07-14 10:10 ` Marc Zyngier
2016-07-13 1:59 ` [PATCH v9 15/17] KVM: arm64: implement ITS command queue command handlers Andre Przywara
2016-07-14 10:38 ` Marc Zyngier
2016-07-14 15:35 ` Andre Przywara
2016-07-14 16:33 ` Marc Zyngier
2016-07-15 8:19 ` Marc Zyngier
2016-07-13 1:59 ` [PATCH v9 16/17] KVM: arm64: implement MSI injection in ITS emulation Andre Przywara
2016-07-13 1:59 ` [PATCH v9 17/17] KVM: arm64: enable ITS emulation as a virtual MSI controller Andre Przywara
2016-07-13 7:57 ` [PATCH v9 00/17] KVM: arm64: GICv3 ITS emulation Diana Madalina Craciun
2016-07-13 8:15 ` Andre Przywara
2016-07-14 10:52 ` Marc Zyngier
2016-07-14 11:01 ` Paolo Bonzini
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=f9aeb32e-ff1a-ab2e-945c-0e8e531e43b9@arm.com \
--to=andre.przywara@arm.com \
--cc=christoffer.dall@linaro.org \
--cc=eric.auger@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.cs.columbia.edu \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=marc.zyngier@arm.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;
as well as URLs for NNTP newsgroup(s).