linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Oliver Upton <oliver.upton@linux.dev>
To: Sebastian Ott <sebott@redhat.com>
Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
	linux-kernel@vger.kernel.org, Marc Zyngier <maz@kernel.org>,
	James Morse <james.morse@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Shaoqin Huang <shahuang@redhat.com>,
	Eric Auger <eric.auger@redhat.com>
Subject: Re: [PATCH v4 3/6] KVM: arm64: add emulation for CTR_EL0 register
Date: Thu, 13 Jun 2024 22:19:56 +0000	[thread overview]
Message-ID: <ZmtwjLbP283ra0Xq@linux.dev> (raw)
In-Reply-To: <20240603130507.17597-4-sebott@redhat.com>

Hi Sebastian,

On Mon, Jun 03, 2024 at 03:05:04PM +0200, Sebastian Ott wrote:

[...]

> +static int validate_cache_topology(struct kvm_vcpu *vcpu, u64 ctr_el0)
> +{
> +	const struct sys_reg_desc *clidr_el1;
> +	unsigned int i;
> +	int ret;
> +
> +	clidr_el1 = get_sys_reg_desc(SYS_CLIDR_EL1);
> +	if (!clidr_el1)
> +		return -ENOENT;

This doesn't actually matter if we agree on dropping the cross-checking,
but if this lookup fails it is 100% a KVM bug. Returning ENOENT isn't
exactly right here, since it gives userspace the impression that the
sysreg index it tried to access does not exist.

So in the future it'd be good to return EINVAL in places where the
kernel did something stupid, probably with a warning for good measure.

> +static int set_ctr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
> +		   u64 val)
> +{
> +	u64 ctr, writable_mask = rd->val;
> +	int ret = 0;
> +
> +	mutex_lock(&vcpu->kvm->arch.config_lock);
> +	ctr  = vcpu->kvm->arch.ctr_el0;
> +	if (val == ctr)
> +		goto out_unlock;
> +
> +	ret = -EBUSY;
> +	if (kvm_vm_has_ran_once(vcpu->kvm))
> +		goto out_unlock;
> +
> +	ret = -EINVAL;
> +	if ((ctr & ~writable_mask) != (val & ~writable_mask))
> +		goto out_unlock;
> +
> +	if (((ctr & CTR_EL0_DIC_MASK) < (val & CTR_EL0_DIC_MASK)) ||
> +	    ((ctr & CTR_EL0_IDC_MASK) < (val & CTR_EL0_IDC_MASK)) ||
> +	    ((ctr & CTR_EL0_DminLine_MASK) < (val & CTR_EL0_DminLine_MASK)) ||
> +	    ((ctr & CTR_EL0_IminLine_MASK) < (val & CTR_EL0_IminLine_MASK))) {
> +		goto out_unlock;

I'd prefer if we addressed the issue w/ arm64_check_features() by making
CTR_EL0 behave like the other registers in the ID space instead of
open-coding these sorts of checks.

I believe that can be accomplished by using kvm_read_sanitised_id_reg()
as the ::reset() function in the descriptor and initializing
kvm->arch.ctr_el0 in kvm_reset_id_regs().

> +	}
> +	ret = validate_cache_topology(vcpu, val);
> +	if (ret)
> +		goto out_unlock;

My concerns about adding these sort of cross-checks remains. The sysreg
code will become exponentially more messy with each cross-register check
we add, given the complete lack of ordering on the UAPI.

So long as KVM has independently tested the validity of the cache
hierarchy and CTR_EL0 against the capabilities of hardware, we know that
userspace cannot advertise more than what's supported in hardware.

If CLIDR_EL1 doesn't line up with the value of CTR_EL0 exposed to the
guest then it is a userspace bug. There simply is no amount of
foolproofing that can be done in KVM to protect against a buggy VMM.

-- 
Thanks,
Oliver


  reply	other threads:[~2024-06-13 22:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-03 13:05 [PATCH v4 0/6] KVM: arm64: emulation for CTR_EL0 Sebastian Ott
2024-06-03 13:05 ` [PATCH v4 1/6] KVM: arm64: unify code to prepare traps Sebastian Ott
2024-06-03 13:05 ` [PATCH v4 2/6] KVM: arm64: maintain per VM value for CTR_EL0 Sebastian Ott
2024-06-03 13:05 ` [PATCH v4 3/6] KVM: arm64: add emulation for CTR_EL0 register Sebastian Ott
2024-06-13 22:19   ` Oliver Upton [this message]
2024-06-13 22:30     ` Oliver Upton
2024-06-14 15:31       ` Sebastian Ott
2024-06-14 18:32         ` Oliver Upton
2024-06-17 10:45           ` Sebastian Ott
2024-06-03 13:05 ` [PATCH v4 4/6] KVM: arm64: show writable masks for feature registers Sebastian Ott
2024-06-03 13:05 ` [PATCH v4 5/6] KVM: arm64: rename functions for invariant sys regs Sebastian Ott
2024-06-03 13:05 ` [PATCH v4 6/6] KVM: selftests: arm64: Test writes to CTR_EL0 Sebastian Ott
2024-06-11 10:38 ` [PATCH v4 0/6] KVM: arm64: emulation for CTR_EL0 Sebastian Ott

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=ZmtwjLbP283ra0Xq@linux.dev \
    --to=oliver.upton@linux.dev \
    --cc=catalin.marinas@arm.com \
    --cc=eric.auger@redhat.com \
    --cc=james.morse@arm.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=sebott@redhat.com \
    --cc=shahuang@redhat.com \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    /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).