Re: [PATCH v6] KVM: arm64: Add early_param to control WFx trapping

From: Oliver Upton
Date: Fri Jun 14 2024 - 15:09:51 EST


On Thu, May 23, 2024 at 05:40:55PM +0000, Colton Lewis wrote:
> Add an early_params to control WFI and WFE trapping. This is to
> control the degree guests can wait for interrupts on their own without
> being trapped by KVM. Options for each param are trap and notrap. trap
> enables the trap. notrap disables the trap. Note that when enabled,
> traps are allowed but not guaranteed by the CPU architecture. Absent
> an explicitly set policy, default to current behavior: disabling the
> trap if only a single task is running and enabling otherwise.
>
> Signed-off-by: Colton Lewis <coltonlewis@xxxxxxxxxx>
> ---
> v6:
> * Rebase to v6.9.1

As in from the stable tree? Please base your patches on an -rc tag, and
especially one from this release cycle.

> +static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu)
> +{
> + if (likely(kvm_wfi_trap_policy == KVM_WFX_NOTRAP_SINGLE_TASK))
> + return single_task_running() &&
> + (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
> + vcpu->kvm->arch.vgic.nassgireq);
> +
> + return kvm_wfi_trap_policy == KVM_WFX_NOTRAP;
> +}

Generally, it is more readable to organize your code in such a way that
multiline statements are unnested as much as possible. So if you were to
invert the if condition it'd become a bit cleaner.

Here is what I plan on squashing into this patch,
kvm_vcpu_should_clear_twe() got the same treatment for the sake of
consistency.

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 9cddd1096b0a..53e23528d2cf 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -557,20 +557,20 @@ static void vcpu_set_pauth_traps(struct kvm_vcpu *vcpu)

static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu)
{
- if (likely(kvm_wfi_trap_policy == KVM_WFX_NOTRAP_SINGLE_TASK))
- return single_task_running() &&
- (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
- vcpu->kvm->arch.vgic.nassgireq);
+ if (unlikely(kvm_wfi_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK))
+ return kvm_wfi_trap_policy == KVM_WFX_NOTRAP;

- return kvm_wfi_trap_policy == KVM_WFX_NOTRAP;
+ return single_task_running() &&
+ (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
+ vcpu->kvm->arch.vgic.nassgireq);
}

static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu)
{
- if (likely(kvm_wfe_trap_policy == KVM_WFX_NOTRAP_SINGLE_TASK))
- return single_task_running();
+ if (unlikely(kvm_wfe_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK))
+ return kvm_wfe_trap_policy == KVM_WFX_NOTRAP;

- return kvm_wfe_trap_policy == KVM_WFX_NOTRAP;
+ return single_task_running();
}

void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)

--
Thanks,
Oliver