KVM: arm64: Stop mapping current thread_info at EL2
Marc Zyngier
maintainercover KVM: arm64: Stop mapping current thread_info at EL2
It recently became apparent that we are mapping each vcpu thread's thread_info structure at EL2 for the sole purpose of checking on the TIF_FOREIGN_FPSTATE flag.
Given that this looks like a slightly over-engineered way of sharing a single bit of information, let's move to a slightly more obvious implementation by maintaining a vcpu-private shadow flag that represents the same state.
I also take this opportunity to add what looks like a missing, and nonetheless crucial piece of information to the FPSIMD code regarding the way KVM (ab)uses the TIF_FOREIGN_FPSTATE.
Lightly tested on an A53 box with a bunch of paranoia instances running in both host and guests.
Marc Zyngier (4):
KVM: arm64: Reorder vcpu flag definitions
KVM: arm64: Introduce flag shadowing TIF_FOREIGN_FPSTATE
KVM: arm64: Stop mapping current thread_info at EL2 arm64/fpsimd: Document the use of TIF_FOREIGN_FPSTATE by KVM
arch/arm64/include/asm/kvm_host.h | 28 ++++++++++++------------- arch/arm64/kernel/fpsimd.c | 5 ++++- arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/fpsimd.c | 20 ++++++++---------- arch/arm64/kvm/hyp/include/hyp/switch.h | 3 +-- arch/arm64/kvm/hyp/nvhe/switch.c | 1 - arch/arm64/kvm/hyp/vhe/switch.c | 1 - 7 files changed, 29 insertions(+), 30 deletions(-)
Signature · 1 line
2.30.2
Marc Zyngier
maintainerPATCH 1/4 KVM: arm64: Reorder vcpu flag definitions
The vcpu arch flags are in an interesting, semi random order. As I have made the mistake of reusing a flag once, let's rework this in an order that I find a bit less confusing.
--- arch/arm64/include/asm/kvm_host.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_host.h +12 -12
@@ -417,14 +417,12 @@ struct kvm_vcpu_arch {#define KVM_ARM64_VCPU_SVE_FINALIZED (1 << 6)/* SVE config completed */ #define KVM_ARM64_GUEST_HAS_PTRAUTH (1 << 7)/* PTRAUTH exposed to guest */ #define KVM_ARM64_PENDING_EXCEPTION (1 << 8)/* Exception pending */ +/* +* Overlaps with KVM_ARM64_EXCEPT_MASK on purpose so that it can't be +* set together with an exception... +*/ +#define KVM_ARM64_INCREMENT_PC (1 << 9)/* Increment PC */ #define KVM_ARM64_EXCEPT_MASK (7 << 9)/* Target EL/MODE */ -#define KVM_ARM64_DEBUG_STATE_SAVE_SPE (1 << 12)/* Save SPE context if active */ -#define KVM_ARM64_DEBUG_STATE_SAVE_TRBE (1 << 13)/* Save TRBE context if active */ - -#define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \ - KVM_GUESTDBG_USE_SW_BP | \ - KVM_GUESTDBG_USE_HW | \ - KVM_GUESTDBG_SINGLESTEP)/* * When KVM_ARM64_PENDING_EXCEPTION is set, KVM_ARM64_EXCEPT_MASK can * take the following values:
@@ -442,11 +440,13 @@ struct kvm_vcpu_arch {#define KVM_ARM64_EXCEPT_AA64_EL1 (0 << 11)#define KVM_ARM64_EXCEPT_AA64_EL2 (1 << 11) -/* -* Overlaps with KVM_ARM64_EXCEPT_MASK on purpose so that it can't be -* set together with an exception... -*/ -#define KVM_ARM64_INCREMENT_PC (1 << 9)/* Increment PC */ +#define KVM_ARM64_DEBUG_STATE_SAVE_SPE (1 << 12)/* Save SPE context if active */ +#define KVM_ARM64_DEBUG_STATE_SAVE_TRBE (1 << 13)/* Save TRBE context if active */ + +#define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \ + KVM_GUESTDBG_USE_SW_BP | \ + KVM_GUESTDBG_USE_HW | \ + KVM_GUESTDBG_SINGLESTEP)#define vcpu_has_sve (vcpu) (system_supports_sve() && \ ((vcpu)->arch.flags & KVM_ARM64_GUEST_HAS_SVE))
Signature · 1 line
2.30.2
Marc Zyngier
maintainerPATCH 2/4 KVM: arm64: Introduce flag shadowing TIF_FOREIGN_FPSTATE
In reply to Marc ZyngierWe currently have to maintain a mapping the thread_info structure at EL2 in order to be able to check the TIF_FOREIGN_FPSTATE flag.
In order to eventually get rid of this, start with a vcpu flag that shadows the thread flag on each entry into the hypervisor.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/fpsimd.c | 8 ++++++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 2 +- 4 files changed, 12 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -442,6 +442,7 @@ struct kvm_vcpu_arch {#define KVM_ARM64_DEBUG_STATE_SAVE_SPE (1 << 12)/* Save SPE context if active */ #define KVM_ARM64_DEBUG_STATE_SAVE_TRBE (1 << 13)/* Save TRBE context if active */ +#define KVM_ARM64_FP_FOREIGN_FPSTATE (1 << 14)#define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \ KVM_GUESTDBG_USE_SW_BP | \
@@ -737,6 +738,7 @@ long kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,/* Guest/host FPSIMD coordination helpers */ int kvm_arch_vcpu_run_map_fp (struct kvm_vcpu *vcpu);void kvm_arch_vcpu_load_fp (struct kvm_vcpu *vcpu); +void kvm_arch_vcpu_ctxflush_fp (struct kvm_vcpu *vcpu);void kvm_arch_vcpu_ctxsync_fp (struct kvm_vcpu *vcpu);void kvm_arch_vcpu_put_fp (struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +1 -0
@@ -850,6 +850,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) }kvm_arm_setup_debug (vcpu); +kvm_arch_vcpu_ctxflush_fp (vcpu);/************************************************************** * Enter the guest
arch/arm64/kvm/fpsimd.c +8 -0
@@ -86,6 +86,14 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu) vcpu->arch.flags |=KVM_ARM64_HOST_SVE_ENABLED ; } +void kvm_arch_vcpu_ctxflush_fp (struct kvm_vcpu *vcpu) +{ +if (test_thread_flag (TIF_FOREIGN_FPSTATE )) + vcpu->arch.flags |=KVM_ARM64_FP_FOREIGN_FPSTATE ; +else + vcpu->arch.flags &= ~KVM_ARM64_FP_FOREIGN_FPSTATE ; +} +/* * If the guest FPSIMD state was loaded, update the host's context * tracking data mark the CPU FPSIMD regs as dirty and belonging to vcpu
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -1
@@ -45,7 +45,7 @@ static inline bool update_fp_enabled(struct kvm_vcpu *vcpu)* trap the accesses. */ if (!system_supports_fpsimd () || - vcpu->arch.host_thread_info->flags & _TIF_FOREIGN_FPSTATE) + vcpu->arch.flags &KVM_ARM64_FP_FOREIGN_FPSTATE ) vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |KVM_ARM64_FP_HOST );
Signature · 1 line
2.30.2
Re: [PATCH 2/4] KVM: arm64: Introduce flag shadowing TIF_FOREIGN_FPSTATE
Quoted 5 lines · Marc Zyngier
We currently have to maintain a mapping the
thread_infostructure at EL2 in order to be able to check theTIF_FOREIGN_FPSTATEflag.In order to eventually get rid of this, start with a vcpu flag that shadows the thread flag on each entry into the hypervisor.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerPATCH 3/4 KVM: arm64: Stop mapping current thread_info at EL2
In reply to Marc ZyngierNow that we can track an equivalent of TIF_FOREIGN_FPSTATE, drop the mapping of current's thread_info at EL2.
--- arch/arm64/include/asm/kvm_host.h | 2 -- arch/arm64/kvm/fpsimd.c | 12 +----------- arch/arm64/kvm/hyp/include/hyp/switch.h | 1 - arch/arm64/kvm/hyp/nvhe/switch.c | 1 - arch/arm64/kvm/hyp/vhe/switch.c | 1 - 5 files changed, 1 insertion(+), 16 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
@@ -26,7 +26,6 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_asm.h> -#include <asm/thread_info.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -321,7 +320,6 @@ struct kvm_vcpu_arch {struct kvm_guest_debug_arch vcpu_debug_state;struct kvm_guest_debug_arch external_debug_state; -struct thread_info *host_thread_info;/* hyp VA */ struct user_fpsimd_state *host_fpsimd_state;/* hyp VA */ struct {
arch/arm64/kvm/fpsimd.c +1 -11
@@ -7,7 +7,6 @@*/ #include <linux/irqflags.h> #include <linux/sched.h> -#include <linux/thread_info.h> #include <linux/kvm_host.h> #include <asm/fpsimd.h> #include <asm/kvm_asm.h>
@@ -28,17 +27,9 @@ int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu) {int ret; -struct thread_info *ti = ¤t->thread_info;struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state; -/* -* Make sure the host task thread flags and fpsimd state are -* visible to hyp: -*/ - ret =create_hyp_mappings (ti, ti +1 ,PAGE_HYP ); -if (ret) - goto error; - +/* Make sure the host task fpsimd state is visible to hyp: */ ret =create_hyp_mappings (fpsimd, fpsimd +1 ,PAGE_HYP );if (ret) goto error;
@@ -54,7 +45,6 @@ int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu) goto error; } - vcpu->arch.host_thread_info =kern_hyp_va (ti); vcpu->arch.host_fpsimd_state =kern_hyp_va (fpsimd); error:return ret;
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -1
@@ -29,7 +29,6 @@#include <asm/fpsimd.h> #include <asm/debug-monitors.h> #include <asm/processor.h> -#include <asm/thread_info.h> extern struct exception_table_entry __start___kvm_ex_table;extern struct exception_table_entry __stop___kvm_ex_table;
arch/arm64/kvm/hyp/nvhe/switch.c +0 -1
@@ -25,7 +25,6 @@#include <asm/fpsimd.h> #include <asm/debug-monitors.h> #include <asm/processor.h> -#include <asm/thread_info.h> #include <nvhe/fixed_config.h> #include <nvhe/mem_protect.h>
arch/arm64/kvm/hyp/vhe/switch.c +0 -1
@@ -24,7 +24,6 @@#include <asm/fpsimd.h> #include <asm/debug-monitors.h> #include <asm/processor.h> -#include <asm/thread_info.h> /* VHE specific context */ DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data);
Signature · 1 line
2.30.2
Marc Zyngier
maintainerPATCH 4/4 arm64/fpsimd: Document the use of TIF_FOREIGN_FPSTATE by KVM
In reply to Marc ZyngierThe bit of documentation that talks about TIF_FOREIGN_FPSTATE does not mention the ungodly tricks that KVM plays with this flag.
Try and document this for the posterity.
--- arch/arm64/kernel/fpsimd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/arm64/kernel/fpsimd.c +4 -1
@@ -78,7 +78,10 @@* indicate whether or not the userland FPSIMD state of the current task is * present in the registers. The flag is set unless the FPSIMD registers of this * CPU currently contain the most recent userland FPSIMD state of the current -* task. +* task *or* the state of the corresponding KVM vcpu if userspace is behaving +* as a VMM and that the vcpu has used FP during its last run. In the latter +* case, KVM will set TIF_FOREIGN_FPSTATE on kvm_vcpu_put(). For all intents +* and purposes, the vcpu FP state is treated identically to userspace's. * * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may * save the task's FPSIMD context back to task_struct from softirq context.
Signature · 1 line
2.30.2
Re: [PATCH 4/4] arm64/fpsimd: Document the use of TIF_FOREIGN_FPSTATE by KVM
Quoted 4 lines · Marc Zyngier
The bit of documentation that talks about
TIF_FOREIGN_FPSTATEdoes not mention the ungodly tricks that KVM plays with this flag.Try and document this for the posterity.
Yes, more documentation here would definitely be helpful - it's pretty hard to follow what KVM is doing here.
Quoted 6 lines
* CPU currently contain the most recent userland FPSIMD state of the current
- * task.
+ * task *or* the state of the corresponding KVM vcpu if userspace is behaving
+ * as a VMM and that the vcpu has used FP during its last run. In the latter
+ * case, KVM will setTIF_FOREIGN_FPSTATEonkvm_vcpu_put(). For all intents
+ * and purposes, the vcpu FP state is treated identically to userspace's.
I'm not able to find a kvm_vcpu_put() function in upstream, just kvm_cpu_put_sysregs_vhe(). There's kvm_arch_vcpu_put() which is called from the vcpu_put() function in generic KVM code but they don't show up until you start mangling the name in that comment. It'd be good to mention what vcpu_put() is actually doing and a bit more about the general model, KVM is behaving differently here AFAICT in that it flags the current state as invalid when it saves the context to memory rather than when an event happens that requires that the context be reloaded. There's no problem there but it's a bit surprising due the difference and worth highlighting.
I think I'd also be inclined to restructure this to foreground the fact that it's the state of the current task but that task may be a VMM. So something more like
...contain the most recent FPSIMD state of the current userspace task. If the task is behaving as a VMM then this will be managed by KVM which will...
making it a bit easier to follow (assuming my understanding of what's going on is correct, if not then I guess something else needs clarifying!).
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerRe: [PATCH 4/4] arm64/fpsimd: Document the use of TIF_FOREIGN_FPSTATE by KVM
Quoted 18 lines · Mark Brown
[1 <text/plain; us-ascii (quoted-printable)>]
Quoted 4 lines · Marc Zyngier
The bit of documentation that talks about
TIF_FOREIGN_FPSTATEdoes not mention the ungodly tricks that KVM plays with this flag.Try and document this for the posterity.
Yes, more documentation here would definitely be helpful - it's pretty hard to follow what KVM is doing here.
Quoted 6 lines
* CPU currently contain the most recent userland FPSIMD state of the current
- * task.
+ * task *or* the state of the corresponding KVM vcpu if userspace is behaving
+ * as a VMM and that the vcpu has used FP during its last run. In the latter
+ * case, KVM will setTIF_FOREIGN_FPSTATEonkvm_vcpu_put(). For all intents
+ * and purposes, the vcpu FP state is treated identically to userspace's.
Quoted 4 lines
I'm not able to find a
kvm_vcpu_put()function in upstream, justkvm_cpu_put_sysregs_vhe(). There'skvm_arch_vcpu_put()which is called from thevcpu_put()function in generic KVM code but they don't show up until you start mangling the name in that comment.
You, vcpu_put() is the one I had in mind.
Quoted 7 lines
It'd be good to
mention whatvcpu_put()is actually doing and a bit more about the general model, KVM is behaving differently here AFAICT in that it flags the current state as invalid when it saves the context to memory rather than when an event happens that requires that the context be reloaded. There's no problem there but it's a bit surprising due the difference and worth highlighting.
There is a bit more to it: KVM flags the userspace state as invalid, but also ties the guest state to the current task via fpsimd_bind_state_to_cpu() so that the state can be saved on vcpu_put() via fpsimd_save_and_flush_cpu_state(), or if we end-up running kernel_neon_begin() because of some softirq handling.
Quoted 11 lines
I think I'd also be inclined to restructure this to foreground the fact that it's the state of the current task but that task may be a VMM. So something more like
...contain the most recent FPSIMD state of the current userspace task. If the task is behaving as a VMM then this will be managed by KVM which will...
making it a bit easier to follow (assuming my understanding of what's going on is correct, if not then I guess something else needs clarifying!).
I'll have a go at rewriting this.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.