Re: [PATCH v3 3/4] KVM: arm64: Introduce attribute to control GICD_TYPER2.nASSGIcap

Marc Zyngier
2025-06-21T08:50:51Z

On Fri, 13 Jun 2025 16:52:37 +0100,
Raghavendra Rao Ananta <rananta@google.com> wrote:
>
> KVM unconditionally advertises GICD_TYPER2.nASSGIcap (which internally
> implies vSGIs) on GICv4.1 systems. Allow userspace to change whether a
> VM supports the feature. Only allow changes prior to VGIC initialization
> as at that point vPEs need to be allocated for the VM.
>
> For convenience, bundle support for vLPIs and vSGIs behind this feature,
> allowing userspace to control vPE allocation for VMs in environments
> that may be constrained on vPE IDs.
>
> Signed-off-by: Raghavendra Rao Ananta <rananta@google.com>
> Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
> ---
> .../virt/kvm/devices/arm-vgic-v3.rst | 29 +++++++++++++++
> arch/arm64/include/uapi/asm/kvm.h | 3 ++
> arch/arm64/kvm/vgic/vgic-init.c | 3 ++
> arch/arm64/kvm/vgic/vgic-kvm-device.c | 37 +++++++++++++++++++
> arch/arm64/kvm/vgic/vgic-mmio-v3.c | 10 ++++-
> arch/arm64/kvm/vgic/vgic-v3.c | 5 ++-
> arch/arm64/kvm/vgic/vgic-v4.c | 2 +-
> include/kvm/arm_vgic.h | 3 ++
> 8 files changed, 88 insertions(+), 4 deletions(-)
>
> diff --git a/Documentation/virt/kvm/devices/arm-vgic-v3.rst b/Documentation/virt/kvm/devices/arm-vgic-v3.rst
> index e860498b1e35..049d77eae591 100644
> --- a/Documentation/virt/kvm/devices/arm-vgic-v3.rst
> +++ b/Documentation/virt/kvm/devices/arm-vgic-v3.rst
> @@ -306,3 +306,32 @@ Groups:
>
> The vINTID specifies which interrupt is generated when the vGIC
> must generate a maintenance interrupt. This must be a PPI.
> +
> + KVM_DEV_ARM_VGIC_GRP_FEATURES
> + Attributes:
> +
> + KVM_DEV_ARM_VGIC_FEATURE_nASSGIcap
> + Control whether support for SGIs without an active state is exposed
> + to the VM. attr->addr points to a __u8 value which indicates whether
> + he feature is enabled / disabled.

s/he/the/

> +
> + A value of 0 indicates that the feature is disabled. A nonzero value
> + indicates that the feature is enabled.
> +
> + This attribute can only be set prior to initializing the VGIC (i.e.
> + KVM_DEV_ARM_VGIC_CTRL_INIT).
> +
> + Support for SGIs without an active state depends on hardware support.
> + Userspace can discover support for the feature by reading the
> + attribute after creating a VGICv3. It is possible that
> + KVM_DEV_ARM_VGIC_CTRL_INIT can later fail if this feature is enabled
> + and KVM is unable to allocate GIC vPEs for the VM.

Can you please add a sentence about the default behaviour? We
currently rely on the GICv4.1 capabilities to be available by default,
and it'd be important to capture this.

> +
> + Errors:
> +
> + ======= ========================================================
> + -ENXIO Invalid attribute in attr->attr
> + -EFAULT Invalid user address in attr->addr
> + -EBUSY The VGIC has already been initialized
> + -EINVAL KVM doesn't support the requested feature setting
> + ======= ========================================================
> diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h
> index ed5f3892674c..41e9ce412afd 100644
> --- a/arch/arm64/include/uapi/asm/kvm.h
> +++ b/arch/arm64/include/uapi/asm/kvm.h
> @@ -417,6 +417,7 @@ enum {
> #define KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO 7
> #define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8
> #define KVM_DEV_ARM_VGIC_GRP_MAINT_IRQ 9
> +#define KVM_DEV_ARM_VGIC_GRP_FEATURES 10
> #define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10
> #define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \
> (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
> @@ -429,6 +430,8 @@ enum {
> #define KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES 3
> #define KVM_DEV_ARM_ITS_CTRL_RESET 4
>
> +#define KVM_DEV_ARM_VGIC_FEATURE_nASSGIcap 0
> +
> /* Device Control API on vcpu fd */
> #define KVM_ARM_VCPU_PMU_V3_CTRL 0
> #define KVM_ARM_VCPU_PMU_V3_IRQ 0
> diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c
> index 5e0e4559004b..944e24750ac4 100644
> --- a/arch/arm64/kvm/vgic/vgic-init.c
> +++ b/arch/arm64/kvm/vgic/vgic-init.c
> @@ -157,6 +157,9 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
>
> kvm->arch.vgic.in_kernel = true;
> kvm->arch.vgic.vgic_model = type;
> + if (type == KVM_DEV_TYPE_ARM_VGIC_V3)
> + kvm->arch.vgic.nassgicap = kvm_vgic_global_state.has_gicv4_1 &&
> + gic_cpuif_has_vsgi();
>
> kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
>
> diff --git a/arch/arm64/kvm/vgic/vgic-kvm-device.c b/arch/arm64/kvm/vgic/vgic-kvm-device.c
> index e28cf68a49c3..629f56063a13 100644
> --- a/arch/arm64/kvm/vgic/vgic-kvm-device.c
> +++ b/arch/arm64/kvm/vgic/vgic-kvm-device.c
> @@ -626,6 +626,26 @@ static int vgic_v3_set_attr(struct kvm_device *dev,
> dev->kvm->arch.vgic.mi_intid = val;
> return 0;
> }
> + case KVM_DEV_ARM_VGIC_GRP_FEATURES: {
> + u8 __user *uaddr = (u8 __user *)attr->addr;
> + u8 val;
> +
> + if (attr->attr != KVM_DEV_ARM_VGIC_FEATURE_nASSGIcap)
> + return -ENXIO;
> +
> + if (get_user(val, uaddr))
> + return -EFAULT;
> +
> + guard(mutex)(&dev->kvm->arch.config_lock);
> + if (vgic_initialized(dev->kvm))
> + return -EBUSY;
> +
> + if (!(kvm_vgic_global_state.has_gicv4_1 && gic_cpuif_has_vsgi()) && val)
> + return -EINVAL;
> +
> + dev->kvm->arch.vgic.nassgicap = val;
> + return 0;
> + }
> default:
> return vgic_set_common_attr(dev, attr);
> }
> @@ -646,6 +666,17 @@ static int vgic_v3_get_attr(struct kvm_device *dev,
> guard(mutex)(&dev->kvm->arch.config_lock);
> return put_user(dev->kvm->arch.vgic.mi_intid, uaddr);
> }
> + case KVM_DEV_ARM_VGIC_GRP_FEATURES: {
> + u8 __user *uaddr = (u8 __user *)attr->addr;
> + u8 val;
> +
> + if (attr->attr != KVM_DEV_ARM_VGIC_FEATURE_nASSGIcap)
> + return -ENXIO;
> +
> + guard(mutex)(&dev->kvm->arch.config_lock);
> + val = dev->kvm->arch.vgic.nassgicap;
> + return put_user(val, uaddr);
> + }
> default:
> return vgic_get_common_attr(dev, attr);
> }
> @@ -683,8 +714,14 @@ static int vgic_v3_has_attr(struct kvm_device *dev,
> return 0;
> case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES:
> return 0;
> + default:
> + return -ENXIO;
> }
> + case KVM_DEV_ARM_VGIC_GRP_FEATURES:
> + return attr->attr != KVM_DEV_ARM_VGIC_FEATURE_nASSGIcap ?
> + -ENXIO : 0;

Do we really want to advertise KVM_DEV_ARM_VGIC_FEATURE_nASSGIcap even
when we don't have GICv4.1? This seems rather odd. My take on this API
is that this should report whether the feature is configurable, making
it backward compatible with older versions of KVM.

Thanks,

M.

--
Jazz isn't dead. It just smells funny.