KVM Archive mirror
 help / color / mirror / Atom feed
From: Colton Lewis <coltonlewis@google.com>
To: Oliver Upton <oliver.upton@linux.dev>
Cc: kvm@vger.kernel.org, corbet@lwn.net, maz@kernel.org,
	james.morse@arm.com,  suzuki.poulose@arm.com,
	yuzenghui@huawei.com, catalin.marinas@arm.com,  will@kernel.org,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev
Subject: Re: [PATCH v4] KVM: arm64: Add early_param to control WFx trapping
Date: Thu, 25 Apr 2024 20:44:37 +0000	[thread overview]
Message-ID: <gsntedat9npm.fsf@coltonlewis-kvm.c.googlers.com> (raw)
In-Reply-To: <ZibaBKCFMz-dJNM4@linux.dev> (message from Oliver Upton on Mon, 22 Apr 2024 14:43:32 -0700)

Oliver Upton <oliver.upton@linux.dev> writes:

> Hi Colton,

> On Mon, Apr 22, 2024 at 06:17:16PM +0000, Colton Lewis wrote:
>> @@ -2653,6 +2653,27 @@
>>   			[KVM,ARM] Allow use of GICv4 for direct injection of
>>   			LPIs.

>> +	kvm-arm.wfe_trap_policy=
>> +			[KVM,ARM] Control when to set wfe instruction trap.

> nitpick: when referring to the instruction, please capitalize it.

> Also, it doesn't hurt to be verbose here and say this cmdline option
> "Controls the WFE instruction trap behavior for KVM VMs"

> I say this because there is a separate set of trap controls that allow
> WFE or WFI to execute in EL0 (i.e. host userspace).

Will do.

>> +			trap: set wfe instruction trap
>> +
>> +			notrap: clear wfe instruction trap
>> +
>> +			default: set wfe instruction trap only if multiple
>> +				 tasks are running on the CPU

> I would strongly prefer we not make any default behavior user-visible.
> The default KVM behavior can (and will) change in the future.

> Only the absence of an explicit trap / notrap policy should fall back to
> KVM's default heuristics.

Makes sense to me. Will do.

>> -static inline void vcpu_clear_wfx_traps(struct kvm_vcpu *vcpu)
>> +static inline void vcpu_clear_wfe_trap(struct kvm_vcpu *vcpu)
>>   {
>>   	vcpu->arch.hcr_el2 &= ~HCR_TWE;
>> +}
>> +
>> +static inline void vcpu_clear_wfi_trap(struct kvm_vcpu *vcpu)
>> +{
>>   	if (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
>>   	    vcpu->kvm->arch.vgic.nassgireq)
>>   		vcpu->arch.hcr_el2 &= ~HCR_TWI;
>> @@ -119,12 +123,28 @@ static inline void vcpu_clear_wfx_traps(struct  
>> kvm_vcpu *vcpu)
>>   		vcpu->arch.hcr_el2 |= HCR_TWI;
>>   }

> This helper definitely does not do as it says on the tin. It ignores the
> policy requested on the command line and conditionally *sets* TWI. If
> the operator believes they know best and ask for a particular trap policy
> KVM should uphold it unconditionally. Even if they've managed to shoot
> themselves in the foot.

Will do. I was only splitting up what the existing helper did here.

>> @@ -423,6 +425,12 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)

>>   }

>> +static bool kvm_should_clear_wfx_trap(enum kvm_wfx_trap_policy p)
>> +{
>> +	return (p == KVM_WFX_NOTRAP && kvm_vgic_global_state.has_gicv4)
>> +		|| (p == KVM_WFX_NOTRAP_SINGLE_TASK && single_task_running());
>> +}

> style nitpick: operators should always go on the preceding line for a
> multi-line statement.

Will do.

>>   void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
>>   {
>>   	struct kvm_s2_mmu *mmu;
>> @@ -456,10 +464,15 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int  
>> cpu)
>>   	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
>>   		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);

>> -	if (single_task_running())
>> -		vcpu_clear_wfx_traps(vcpu);
>> +	if (kvm_should_clear_wfx_trap(kvm_wfi_trap_policy))
>> +		vcpu_clear_wfi_trap(vcpu);
>>   	else
>> -		vcpu_set_wfx_traps(vcpu);
>> +		vcpu_set_wfi_trap(vcpu);
>> +
>> +	if (kvm_should_clear_wfx_trap(kvm_wfe_trap_policy))
>> +		vcpu_clear_wfe_trap(vcpu);
>> +	else
>> +		vcpu_set_wfe_trap(vcpu);

>>   	if (vcpu_has_ptrauth(vcpu))
>>   		vcpu_ptrauth_disable(vcpu);

> I find all of the layering rather hard to follow; we don't need
> accessors for doing simple bit manipulation.

> Rough sketch:

> static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu)
> {
> 	if (unlikely(kvm_wfi_trap != KVM_WFX_DEFAULT))
> 		return kvm_wfi_trap == 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 (unlikely(kvm_wfe_trap != KVM_WFX_DEFAULT))
> 		return kvm_wfe_trap == KVM_WFX_NOTRAP;

> 	return single_task_running();
> }

> static void kvm_vcpu_load_compute_hcr(struct kvm_vcpu *vcpu)
> {
> 	vcpu->arch.hcr_el2 |= HCR_TWE | HCR_TWI;

> 	if (kvm_vcpu_should_clear_twe(vcpu))
> 		vcpu->arch.hcr_el2 &= ~HCR_TWE;
> 	if (kvm_vcpu_should_clear_twi(vcpu))
> 		vcpu->arch.hcr_el2 &= ~HCR_TWI;
> }

Will do.

> And if we really wanted to, the non-default trap configuration could be
> moved to vcpu_reset_hcr() if we cared.

Might as well.

      reply	other threads:[~2024-04-25 20:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-22 18:17 [PATCH v4] KVM: arm64: Add early_param to control WFx trapping Colton Lewis
2024-04-22 21:43 ` Oliver Upton
2024-04-25 20:44   ` Colton Lewis [this message]

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=gsntedat9npm.fsf@coltonlewis-kvm.c.googlers.com \
    --to=coltonlewis@google.com \
    --cc=catalin.marinas@arm.com \
    --cc=corbet@lwn.net \
    --cc=james.morse@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=oliver.upton@linux.dev \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    --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 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 read-only IMAP folder(s) and NNTP newsgroup(s).