On Tue, Sep 22, 2026, Oliver Upton wrote:
> Hi Lorenzo,
>
> On Tue, Sep 22, 2026 at 03:17:55PM +0100, Lorenzo Stoakes (ARM) wrote:
> > +bool __weak kvm_arch_vcpu_allow_pre_fault_memory(struct kvm_vcpu *vcpu)
> > +{
> > + return true;
> > +}
> > +
> > void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
> > {
> > int nr_vcpus, start, i, idx, yielded;
> > @@ -4365,6 +4370,9 @@ static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu
> > *vcpu,
> > range->gpa + range->size <= range->gpa)
> > return -EINVAL;
> >
> > + if (!kvm_arch_vcpu_allow_pre_fault_memory(vcpu))
> > + return -ENOEXEC;
> > +
>
> nit: it'd be better to let the arch hook return an error of its choosing
> but in reality this is only going to be used by arm64.
Heh, except x86 already has something similar.
if (!vcpu->kvm->arch.pre_fault_allowed)
return -EOPNOTSUPP;
As does s390:
if (kvm_is_ucontrol(vcpu->kvm))
return -EINVAL;
I also don't like that this is subtly about avoiding vcpu_load(); it will be all
too easy to overlook that detail in the future.
Rather than have kvm_arch_vcpu_allow_pre_fault_memory(), what if we add a more
generic kvm_is_vcpu_loadable()? That way we don't need to worry as much about
the return value, the connection to vcpu_load() is obvious, and we don't need to
add another pre-check if future (or cleaned-up existing?) ioctls want to do
vcpu_load() in common code.
I'd also be tempted to say it can be a macro, not a __weak function. E.g.
diff --git a/arch/arm64/include/asm/kvm_host.h
b/arch/arm64/include/asm/kvm_host.h
index 27fe0cd5b2d7..99613df254cf 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -1533,6 +1533,7 @@ static inline bool __vcpu_has_feature(const struct
kvm_arch *ka, int feature)
#define vcpu_has_feature(v, f) __vcpu_has_feature(&(v)->kvm->arch, (f))
#define kvm_vcpu_initialized(v) vcpu_get_flag(v, VCPU_INITIALIZED)
+#define kvm_is_vcpu_loadable kvm_vcpu_initialized
int kvm_trng_call(struct kvm_vcpu *vcpu);
#ifdef CONFIG_KVM
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 3dd04605f2e5..02401b080507 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -1053,6 +1053,9 @@ int kvm_trylock_all_vcpus(struct kvm *kvm);
int kvm_lock_all_vcpus(struct kvm *kvm);
void kvm_unlock_all_vcpus(struct kvm *kvm);
+#ifndef kvm_is_vcpu_loadable
+#define kvm_is_vcpu_loadable(v) true
+#endif
void vcpu_load(struct kvm_vcpu *vcpu);
void vcpu_put(struct kvm_vcpu *vcpu);