* [PATCH] KVM: arm64: pkvm: Roll back only the caller's own CPU_ON
@ 2026-12-04 20:08 Xuheng Li via B4 Relay
2026-12-06 14:18 ` Fuad Tabba
0 siblings, 1 reply; 2+ messages in thread
From: Xuheng Li via B4 Relay @ 2026-12-04 20:08 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland
Cc: linux-arm-kernel, kvmarm, Xuheng Li
From: Xuheng Li <xuheng@cs.columbia.edu>
When the host answers a protected vCPU's forwarded PSCI CPU_ON with a
failure, handle_pvm_entry_hvc64() rolls the target back by moving its
power_state from ON_PENDING to OFF. ON_PENDING does not say whose CPU_ON
made it so. Between the forward and the caller's next entry the target
can run, power itself off, and be targeted by another vCPU's CPU_ON. The
rollback then cancels that other CPU_ON:
vCPU i vCPU j target t
CPU_ON(t): OFF ->
ON_PENDING, publish;
the host fails it but
runs t anyway
reset, ON, ..., CPU_OFF
CPU_ON(t): OFF ->
ON_PENDING
re-entry: rollback
ON_PENDING -> OFF
publish pc/r0; SUCCESS
i's caller is told INTERNAL_FAILURE although t ran its CPU_ON, and j's
caller is told SUCCESS, but t stays OFF and never runs j's entry point.
As the rollback leaves reset_state.reset set, the next CPU_ON of t can
also boot it from j's pc/r0, or from a mix of j's and its own values if
t's reset runs before that CPU_ON has written them.
Only a host that fails a forwarded CPU_ON yet runs its target can set
this up, but that is exactly the case to defend against: pKVM does not
trust the host, and a protected guest must be able to rely on the PSCI
results and power-on semantics that EL2 gives it, whatever the host
does with its scheduling or its return values.
Keep the protocol in power_state. An ON_PENDING value now also carries
its caller (vcpu_idx + 1, or 0 for the first run) and a PUBLISHED bit:
pvm_psci_vcpu_on() moves OFF to its own pending value, writes
reset_state, and publishes with a release of the published value; the
rollback moves only its own published value back to OFF; and
pkvm_reset_vcpu() resets only a published ON_PENDING, with an acquire
cmpxchg from the value it read, so the reset_state it reads is the one
that value published. reset_state.reset is no longer used at EL2.
The caller tag only fits for vcpu_idx below 2^23, so __pkvm_init_vm()
now also rejects a VM with more than KVM_MAX_VCPUS vCPUs, which the host
never creates. Without that bound, a host could create a VM in which two
vCPUs share a tag.
Fixes: 872383bd12e1 ("KVM: arm64: Add per-EC entry/exit state marshalling for protected guests")
Signed-off-by: Xuheng Li <xuheng@cs.columbia.edu>
---
Reproduced under QEMU (TCG, nVHE host, kvm-arm.mode=protected) with a
three-vCPU protected guest and a test-only change to the host's
kvm_psci_vcpu_on(). The change fails vCPU 0's CPU_ON of vCPU 2 after
waking vCPU 2, holds vCPU 0 until vCPU 1's CPU_ON of vCPU 2 has been
forwarded, and delays that wake-up so that vCPU 0 re-enters EL2 first.
vCPU 2's first entry point records its context ID and calls CPU_OFF:
- without this patch, vCPU 0 is told INTERNAL_FAILURE although vCPU 2 ran
its entry point, vCPU 1 is told SUCCESS, and vCPU 2 never runs vCPU 1's
entry point: its next KVM_RUN fails with -EINVAL;
- with it, vCPU 0 is told SUCCESS, and vCPU 2 starts at vCPU 1's entry
point with its context ID.
The host change, the VMM and the guest are not part of this patch; I can
post them if they help.
arch/arm64/kvm builds with W=1 and no new warnings.
The patch and its commit message were drafted with an AI assistant
(Claude) and reviewed by me.
---
arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 28 +++++++++++++-
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 39 ++++++++++---------
arch/arm64/kvm/hyp/nvhe/pkvm.c | 70 +++++++++++++++++++---------------
3 files changed, 87 insertions(+), 50 deletions(-)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
index 0261b2bc681f..25eebaa57c0c 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
@@ -10,6 +10,8 @@
#include <asm/kvm_hcall.h>
#include <asm/kvm_pkvm.h>
+#include <uapi/linux/psci.h>
+
#include <nvhe/gfp.h>
#include <nvhe/spinlock.h>
@@ -42,12 +44,34 @@ struct pkvm_hyp_vcpu {
u32 exit_code;
/*
- * PSCI_0_2_AFFINITY_LEVEL_{OFF, ON_PENDING, ON}. A non-protected
- * vCPU is always ON.
+ * PSCI_0_2_AFFINITY_LEVEL_{OFF, ON_PENDING, ON}, read with
+ * pkvm_power_level(). A non-protected vCPU is always ON. ON_PENDING
+ * also names the CPU_ON that made it so: its caller above
+ * PKVM_POWER_CALLER_SHIFT (vcpu_idx + 1, or 0 for the first run), and
+ * PKVM_POWER_PUBLISHED once that CPU_ON has written reset_state.
*/
int power_state;
};
+#define PKVM_POWER_LEVEL_MASK 0x7f
+#define PKVM_POWER_PUBLISHED 0x80
+#define PKVM_POWER_CALLER_SHIFT 8
+
+static inline int pkvm_power_level(int power_state)
+{
+ return power_state & PKVM_POWER_LEVEL_MASK;
+}
+
+/* The ON_PENDING value of a CPU_ON by @caller, before it publishes. */
+static inline int pkvm_power_pending(struct pkvm_hyp_vcpu *caller)
+{
+ /* __pkvm_init_vm() keeps vcpu_idx below KVM_MAX_VCPUS. */
+ BUILD_BUG_ON(KVM_MAX_VCPUS + 1 > (INT_MAX >> PKVM_POWER_CALLER_SHIFT));
+
+ return PSCI_0_2_AFFINITY_LEVEL_ON_PENDING |
+ ((caller->vcpu.vcpu_idx + 1) << PKVM_POWER_CALLER_SHIFT);
+}
+
/*
* Holds the relevant data for running a vm in protected mode.
*/
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 0c023ee0cf42..ce205ddc4993 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -282,7 +282,7 @@ static void handle_pvm_entry_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu)
unsigned long cpu_id = smccc_get_arg1(vcpu);
struct pkvm_hyp_vcpu *target_vcpu;
struct pkvm_hyp_vm *hyp_vm;
- int prev;
+ int mine, prev;
hyp_vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu);
target_vcpu = pkvm_mpidr_to_hyp_vcpu(hyp_vm, cpu_id);
@@ -292,29 +292,32 @@ static void handle_pvm_entry_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu)
* entries are never removed, so the lookup cannot miss.
* The release orders this vCPU's reset_state writes
* before OFF, for the next CPU_ON.
+ *
+ * Roll back only this vCPU's own CPU_ON, which it
+ * published before returning to the host. This vCPU
+ * has not run since, so it has made no other one. A
+ * bare ON_PENDING could be another vCPU's CPU_ON of the
+ * same target, made after this one's target ran and
+ * powered off again; cancelling that would report
+ * SUCCESS to its caller for a target left OFF.
*/
- prev = cmpxchg_release(&target_vcpu->power_state,
- PSCI_0_2_AFFINITY_LEVEL_ON_PENDING,
+ mine = pkvm_power_pending(hyp_vcpu) | PKVM_POWER_PUBLISHED;
+ prev = cmpxchg_release(&target_vcpu->power_state, mine,
PSCI_0_2_AFFINITY_LEVEL_OFF);
- switch (prev) {
- case PSCI_0_2_AFFINITY_LEVEL_ON_PENDING:
+ if (prev == mine) {
/*
- * Leave reset_state.reset set: a clear races a
- * fresh CPU_ON's publish. The stale pc/r0/be are
- * the guest's own. ALREADY_ON is PSCI's retry
- * signal for a CPU_ON that raced the CPU_OFF.
+ * The target never ran this CPU_ON. ALREADY_ON
+ * is PSCI's retry signal for a CPU_ON that raced
+ * the CPU_OFF.
*/
if (ret != PSCI_RET_ALREADY_ON)
ret = PSCI_RET_INTERNAL_FAILURE;
- break;
- case PSCI_0_2_AFFINITY_LEVEL_ON:
- case PSCI_0_2_AFFINITY_LEVEL_OFF:
- /* Target already ran (and may have stopped). */
+ } else {
+ /*
+ * The target already ran this CPU_ON (and may
+ * have stopped, or been targeted again since).
+ */
ret = PSCI_RET_SUCCESS;
- break;
- default:
- ret = PSCI_RET_INTERNAL_FAILURE;
- break;
}
}
@@ -940,7 +943,7 @@ DEFINE_KVM_HOST_HCALL(int, __kvm_vcpu_run, struct kvm_vcpu __kern *, vcpu)
* READ_ONCE suffices. ON_PENDING takes the reset; -ECANCELED
* is a rollback that raced it.
*/
- switch (READ_ONCE(hyp_vcpu->power_state)) {
+ switch (pkvm_power_level(READ_ONCE(hyp_vcpu->power_state))) {
case PSCI_0_2_AFFINITY_LEVEL_ON:
break;
case PSCI_0_2_AFFINITY_LEVEL_ON_PENDING:
diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index 6442200cd40b..99f469af24bb 100644
--- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
+++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
@@ -466,7 +466,6 @@ static int pkvm_vcpu_init_psci(struct pkvm_hyp_vcpu *hyp_vcpu, u32 mp_state)
return -EINVAL;
if (mp_state == KVM_MP_STATE_STOPPED) {
- reset_state->reset = false;
hyp_vcpu->power_state = PSCI_0_2_AFFINITY_LEVEL_OFF;
return 0;
}
@@ -479,8 +478,9 @@ static int pkvm_vcpu_init_psci(struct pkvm_hyp_vcpu *hyp_vcpu, u32 mp_state)
host_vcpu = hyp_vcpu->host_vcpu;
reset_state->pc = READ_ONCE(*vcpu_pc(host_vcpu));
reset_state->r0 = READ_ONCE(vcpu_gp_regs(host_vcpu)[0]);
- reset_state->reset = true;
- hyp_vcpu->power_state = PSCI_0_2_AFFINITY_LEVEL_ON_PENDING;
+ /* The first run: published, with no caller to roll it back. */
+ hyp_vcpu->power_state = PSCI_0_2_AFFINITY_LEVEL_ON_PENDING |
+ PKVM_POWER_PUBLISHED;
return 0;
}
@@ -929,7 +929,7 @@ int __pkvm_init_vm(struct kvm *host_kvm, void __kern *pgd_hva)
return ret;
nr_vcpus = READ_ONCE(host_kvm->created_vcpus);
- if (nr_vcpus < 1) {
+ if (nr_vcpus < 1 || nr_vcpus > KVM_MAX_VCPUS) {
ret = -EINVAL;
goto err_unpin_kvm;
}
@@ -1225,40 +1225,50 @@ static void pkvm_memunshare_call(u64 *ret, struct kvm_vcpu *vcpu)
* target's own CPU. Returns -ECANCELED, with no side effects, if a rollback
* raced the reset.
*
- * The requestor's PSCI CPU_ON, in pvm_psci_vcpu_on():
+ * The requestor's PSCI CPU_ON, in pvm_psci_vcpu_on(), where P names the
+ * requestor (pkvm_power_pending()):
*
- * cmpxchg_relaxed(power_state): OFF -> ON_PENDING
+ * cmpxchg_relaxed(power_state): OFF -> P
* <ctrl>
* write_reset_state();
- * smp_store_release(reset_state->reset, true);
+ * smp_store_release(power_state, P | PUBLISHED);
*
* The target coming online here, running, then its PSCI CPU_OFF in
* pvm_psci_vcpu_off():
*
- * smp_load_acquire(reset_state->reset) == true;
- * cmpxchg_relaxed(power_state): ON_PENDING -> ON;
+ * p = READ_ONCE(power_state), a published ON_PENDING;
+ * cmpxchg_acquire(power_state): p -> ON;
* read_reset_state();
*
* <run guest>
*
- * WRITE_ONCE(reset_state->reset, false);
* smp_store_release(power_state, OFF);
+ *
+ * A rollback in handle_pvm_entry_hvc64() moves only its own requestor's
+ * P | PUBLISHED back to OFF, so it can't cancel another vCPU's CPU_ON.
*/
int pkvm_reset_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
{
struct vcpu_reset_state *reset_state = &hyp_vcpu->vcpu.arch.reset_state;
+ int pending;
/*
- * Pairs with smp_store_release(&reset_state->reset, true) in
- * pvm_psci_vcpu_on(). The acquire must precede the cmpxchg: reversed, a
- * winning cmpxchg with a false acquire would leave power_state == ON
- * with the reset skipped.
+ * Only a published ON_PENDING is reset: its CPU_ON has written
+ * reset_state (or pkvm_vcpu_init_psci() has, for the first run).
*/
- if (!smp_load_acquire(&reset_state->reset))
+ pending = READ_ONCE(hyp_vcpu->power_state);
+ if (pkvm_power_level(pending) != PSCI_0_2_AFFINITY_LEVEL_ON_PENDING ||
+ !(pending & PKVM_POWER_PUBLISHED))
return -ECANCELED;
- if (cmpxchg_relaxed(&hyp_vcpu->power_state, PSCI_0_2_AFFINITY_LEVEL_ON_PENDING,
- PSCI_0_2_AFFINITY_LEVEL_ON) != PSCI_0_2_AFFINITY_LEVEL_ON_PENDING)
+ /*
+ * Pairs with smp_store_release(&power_state, P | PUBLISHED) in
+ * pvm_psci_vcpu_on(), the store of the value replaced here, so the
+ * reset_state read below is the one that CPU_ON published. A rollback
+ * that raced the read makes the cmpxchg fail.
+ */
+ if (cmpxchg_acquire(&hyp_vcpu->power_state, pending,
+ PSCI_0_2_AFFINITY_LEVEL_ON) != pending)
return -ECANCELED;
kvm_reset_vcpu_core(&hyp_vcpu->vcpu);
@@ -1302,7 +1312,7 @@ static bool pvm_psci_vcpu_on(struct pkvm_hyp_vcpu *hyp_vcpu)
struct vcpu_reset_state *reset_state;
struct pkvm_hyp_vcpu *target;
unsigned long cpu_id, ret;
- int power_state;
+ int pending, power_state;
cpu_id = smccc_get_arg1(&hyp_vcpu->vcpu);
if (!kvm_psci_valid_affinity(&hyp_vcpu->vcpu, cpu_id)) {
@@ -1320,12 +1330,13 @@ static bool pvm_psci_vcpu_on(struct pkvm_hyp_vcpu *hyp_vcpu)
* vCPUs race to power on the same target. The writes below depend on
* the cmpxchg reading OFF, and that control dependency with the release
* of OFF in pvm_psci_vcpu_off() orders them after the target's accesses
- * to reset_state, so relaxed suffices.
+ * to reset_state, so relaxed suffices. The new value names this
+ * requestor, so that only its own rollback can cancel it.
*/
+ pending = pkvm_power_pending(hyp_vcpu);
power_state = cmpxchg_relaxed(&target->power_state,
- PSCI_0_2_AFFINITY_LEVEL_OFF,
- PSCI_0_2_AFFINITY_LEVEL_ON_PENDING);
- switch (power_state) {
+ PSCI_0_2_AFFINITY_LEVEL_OFF, pending);
+ switch (pkvm_power_level(power_state)) {
case PSCI_0_2_AFFINITY_LEVEL_ON_PENDING:
ret = PSCI_RET_ON_PENDING;
goto error;
@@ -1344,10 +1355,12 @@ static bool pvm_psci_vcpu_on(struct pkvm_hyp_vcpu *hyp_vcpu)
WRITE_ONCE(reset_state->r0, smccc_get_arg3(&hyp_vcpu->vcpu));
WRITE_ONCE(reset_state->be, kvm_vcpu_is_be(&hyp_vcpu->vcpu));
/*
- * Publish reset_state.{pc, r0, be} to the target vCPU. Pairs with
- * smp_load_acquire(&reset_state->reset) in pkvm_reset_vcpu().
+ * Publish this CPU_ON and its reset_state.{pc, r0, be} to the target
+ * vCPU. Pairs with cmpxchg_acquire() in pkvm_reset_vcpu(). Nothing
+ * else writes an unpublished ON_PENDING: the rollback and the reset
+ * act only on a published one.
*/
- smp_store_release(&reset_state->reset, true);
+ smp_store_release(&target->power_state, pending | PKVM_POWER_PUBLISHED);
/* The host requests KVM_REQ_VCPU_RESET and wakes the target. */
return false;
@@ -1366,12 +1379,9 @@ static bool pvm_psci_vcpu_off(struct pkvm_hyp_vcpu *hyp_vcpu)
/* No other writer runs while this vCPU is ON and executing. */
WARN_ON(READ_ONCE(hyp_vcpu->power_state) != PSCI_0_2_AFFINITY_LEVEL_ON);
- WRITE_ONCE(hyp_vcpu->vcpu.arch.reset_state.reset, false);
-
/*
- * Orders pkvm_reset_vcpu()'s accesses to reset_state, and the clear
- * above, before OFF. Pairs with the control dependency in
- * pvm_psci_vcpu_on().
+ * Orders pkvm_reset_vcpu()'s accesses to reset_state before OFF.
+ * Pairs with the control dependency in pvm_psci_vcpu_on().
*/
smp_store_release(&hyp_vcpu->power_state, PSCI_0_2_AFFINITY_LEVEL_OFF);
---
base-commit: 007722b8211f7b34fdf21b1e6a9c32497633ea29
change-id: 20261204-pkvm-cpu-on-rollback-3483e463d4f7
Best regards,
--
Xuheng Li <xuheng@cs.columbia.edu>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] KVM: arm64: pkvm: Roll back only the caller's own CPU_ON
2026-12-04 20:08 [PATCH] KVM: arm64: pkvm: Roll back only the caller's own CPU_ON Xuheng Li via B4 Relay
@ 2026-12-06 14:18 ` Fuad Tabba
0 siblings, 0 replies; 2+ messages in thread
From: Fuad Tabba @ 2026-12-06 14:18 UTC (permalink / raw)
To: Xuheng Li
Cc: Marc Zyngier, Oliver Upton, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, linux-arm-kernel, kvmarm, Fuad Tabba
Hi Xuheng,
On Fri, 04 Dec 2026 20:08:27 +0000, Xuheng Li <xuheng@cs.columbia.edu> wrote:
> When the host answers a protected vCPU's forwarded PSCI CPU_ON with a
> failure, handle_pvm_entry_hvc64() rolls the target back by moving its
> power_state from ON_PENDING to OFF. ON_PENDING does not say whose CPU_ON
> made it so. Between the forward and the caller's next entry the target
> can run, power itself off, and be targeted by another vCPU's CPU_ON. The
> rollback then cancels that other CPU_ON:
>
> vCPU i vCPU j target t
> CPU_ON(t): OFF ->
> ON_PENDING, publish;
> the host fails it but
> runs t anyway
> reset, ON, ..., CPU_OFF
> CPU_ON(t): OFF ->
> ON_PENDING
> re-entry: rollback
> ON_PENDING -> OFF
> publish pc/r0; SUCCESS
>
> i's caller is told INTERNAL_FAILURE although t ran its CPU_ON, and j's
> caller is told SUCCESS, but t stays OFF and never runs j's entry point.
> As the rollback leaves reset_state.reset set, the next CPU_ON of t can
> also boot it from j's pc/r0, or from a mix of j's and its own values if
> t's reset runs before that CPU_ON has written them.
>
> Only a host that fails a forwarded CPU_ON yet runs its target can set
> this up, but that is exactly the case to defend against: pKVM does not
> trust the host, and a protected guest must be able to rely on the PSCI
> results and power-on semantics that EL2 gives it, whatever the host
> does with its scheduling or its return values.
>
> Keep the protocol in power_state. An ON_PENDING value now also carries
> its caller (vcpu_idx + 1, or 0 for the first run) and a PUBLISHED bit:
> pvm_psci_vcpu_on() moves OFF to its own pending value, writes
> reset_state, and publishes with a release of the published value; the
> rollback moves only its own published value back to OFF; and
> pkvm_reset_vcpu() resets only a published ON_PENDING, with an acquire
> cmpxchg from the value it read, so the reset_state it reads is the one
> that value published. reset_state.reset is no longer used at EL2.
>
> The caller tag only fits for vcpu_idx below 2^23, so __pkvm_init_vm()
> now also rejects a VM with more than KVM_MAX_VCPUS vCPUs, which the host
> never creates. Without that bound, a host could create a VM in which two
> vCPUs share a tag.
>
> Fixes: 872383bd12e1 ("KVM: arm64: Add per-EC entry/exit state marshalling for protected guests")
> Signed-off-by: Xuheng Li <xuheng@cs.columbia.edu>
Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
Tested on QEMU with a protected guest, with CPU hotplug and with a
test-only host change that fails CPU_ON before or after it wakes the
target:
Tested-by: Fuad Tabba <fuad.tabba@linux.dev>
> diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
[...]
> @@ -479,8 +478,9 @@ static int pkvm_vcpu_init_psci(struct pkvm_hyp_vcpu *hyp_vcpu, u32 mp_state)
> host_vcpu = hyp_vcpu->host_vcpu;
> reset_state->pc = READ_ONCE(*vcpu_pc(host_vcpu));
> reset_state->r0 = READ_ONCE(vcpu_gp_regs(host_vcpu)[0]);
> - reset_state->reset = true;
> - hyp_vcpu->power_state = PSCI_0_2_AFFINITY_LEVEL_ON_PENDING;
> + /* The first run: published, with no caller to roll it back. */
> + hyp_vcpu->power_state = PSCI_0_2_AFFINITY_LEVEL_ON_PENDING |
> + PKVM_POWER_PUBLISHED;
nit: this assignment fits on one line.
Cheers,
/fuad
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-06 14:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 20:08 [PATCH] KVM: arm64: pkvm: Roll back only the caller's own CPU_ON Xuheng Li via B4 Relay
2026-12-06 14:18 ` Fuad Tabba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox