[PATCH v4 07/13] KVM: arm64: Deprivilege the host under stop_machine()

From: Fuad Tabba

Date: Wed Nov 18 2026 - 10:25:52 EST


pkvm_drop_host_privileges() installs the host stage 2 with
on_each_cpu(), and each CPU goes back to running the host as soon as its
own __pkvm_prot_finalize() returns. That doesn't work for the ITS
takeover that a subsequent patch adds here. The other CPUs must not post
ITS commands while the driver switches to its copy of the command queue,
and a takeover that fails has to leave every CPU privileged.

Finalise from a stop_machine() callback instead, so that the takeover
can run with the other CPUs stopped and before any CPU is finalised. An
error from any CPU is still returned to finalize_pkvm().

Signed-off-by: Fuad Tabba <fuad.tabba@xxxxxxxxx>
---
arch/arm64/kvm/pkvm.c | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 016997288fd23..16dfb249b4961 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -11,6 +11,7 @@
#include <asm/kvm_mmu.h>
#include <linux/memblock.h>
#include <linux/mutex.h>
+#include <linux/stop_machine.h>

#include <asm/kvm_pkvm.h>

@@ -312,25 +313,19 @@ int pkvm_init_host_vm(struct kvm *kvm)
return 0;
}

-static void __init _kvm_host_prot_finalize(void *arg)
+static int __init __pkvm_drop_host_privileges(void *arg)
{
- int *err = arg;
-
- if (WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize)))
- WRITE_ONCE(*err, -EINVAL);
+ return WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize)) ? -EINVAL : 0;
}

static int __init pkvm_drop_host_privileges(void)
{
- int ret = 0;
-
/*
* Flip the static key upfront as that may no longer be possible
* once the host stage 2 is installed.
*/
static_branch_enable(&kvm_protected_mode_initialized);
- on_each_cpu(_kvm_host_prot_finalize, &ret, 1);
- return ret;
+ return stop_machine(__pkvm_drop_host_privileges, NULL, cpu_online_mask);
}

static void __init pkvm_selftests(void)
--
2.47.3