Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tao Cui" <cui.tao@linux.dev>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH] LoongArch: KVM: Fix TOCTOU race on pv_features
Date: Mon, 10 Aug 2026 08:31:22 +0000	[thread overview]
Message-ID: <20260810083122.AD46C1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260810081321.157258-1-cui.tao@linux.dev>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] KVM corrupts guest memory by unconditionally updating steal time for a disabled feature due to an unhandled state-machine transition.
- [High] LoongArch PV features are silently enabled by default, violating KVM cross-architecture enumeration invariants.
--

--- Patch [1]: [PATCH] LoongArch: KVM: Fix TOCTOU race on pv_features ---
commit 28d84af1392db0a3c512a369b492d598370fb64d
Author: Tao Cui <cuitao@kylinos.cn>

LoongArch: KVM: Fix TOCTOU race on pv_features

This patch fixes a check-then-set race condition in
kvm_loongarch_cpucfg_set_attr() by replacing it with a cmpxchg loop. This
ensures atomic updates of kvm->arch.pv_features when concurrent vCPUs
attempt to set the attributes.

> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
> index 20c207d80e31a..55030c37cf061 100644
> --- a/arch/loongarch/kvm/vcpu.c
> +++ b/arch/loongarch/kvm/vcpu.c
> @@ -1164,12 +1164,18 @@ static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu,
[ ... ]
> -		/* All vCPUs need set the same PV features */
> -		if ((kvm->arch.pv_features & LOONGARCH_PV_FEAT_UPDATED)
> -				&& ((kvm->arch.pv_features & valid) != val))
> -			return -EINVAL;
> -		kvm->arch.pv_features = val | LOONGARCH_PV_FEAT_UPDATED;
> -		return 0;
> +		/* Atomically install val; the cmpxchg serializes concurrent setters. */
> +		for (;;) {
> +			unsigned long old, new;
> +
> +			old = READ_ONCE(kvm->arch.pv_features);
> +			if ((old & LOONGARCH_PV_FEAT_UPDATED) &&
> +			    ((old & valid) != val))
> +				return -EINVAL;

[Severity: High]
This isn't a bug introduced by this patch, but does this logic allow the first
configuration write to disable features (ON-to-OFF) without clearing previously
initialized dependent states?

Since LOONGARCH_PV_FEAT_UPDATED is not yet set on the first write, an explicit
ON-to-OFF transition is allowed.

If userspace initially configures KVM_LOONGARCH_VCPU_PVTIME_CTRL (because the
feature is enabled by default), and later disables KVM_FEATURE_STEAL_TIME via
CPUCFG_KVM_FEATURE, _kvm_vcpu_load() will still unconditionally request
KVM_REQ_STEAL_UPDATE.

Could this lead to guest memory corruption? When kvm_update_stolen_time()
executes, it relies solely on the cached GPA valid bit:

arch/loongarch/kvm/vcpu.c:kvm_update_stolen_time() {
    ...
    if (!(gpa & KVM_STEAL_PHYS_VALID))
        return;
    ...
}

Should we verify if KVM_FEATURE_STEAL_TIME is still enabled before updating
steal time, or prevent the ON-to-OFF transition if dependent states are
already initialized?

[Severity: High]
This is also a pre-existing issue, but are LoongArch PV features silently
enabled by default?

Looking at kvm_vm_init_features() in arch/loongarch/kvm/vm.c:

arch/loongarch/kvm/vm.c:kvm_vm_init_features() {
    ...
    /* Enable all PV features by default */
    kvm->arch.pv_features |= BIT(KVM_FEATURE_IPI);
    kvm->arch.kvm_features |= BIT(KVM_LOONGARCH_VM_FEAT_PV_IPI);
    ...
}

Does this violate the KVM subsystem architecture rule that new guest-visible
features must be off by default and discoverable? It seems silently enabling
features could break capability negotiation and live migration.

> +			new = val | LOONGARCH_PV_FEAT_UPDATED;
> +			if (cmpxchg(&kvm->arch.pv_features, old, new) == old)
> +				return 0;
> +		}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260810081321.157258-1-cui.tao@linux.dev?part=1

      parent reply	other threads:[~2026-08-10  8:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-10  8:13 [PATCH] LoongArch: KVM: Fix TOCTOU race on pv_features Tao Cui
2026-08-10  8:17 ` Tao Cui
2026-08-10  8:31 ` sashiko-bot [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=20260810083122.AD46C1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=cui.tao@linux.dev \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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