KVM: arm64: Consolidate initializing the host data's fpsimd_state/sve in pKVM
authorFuad Tabba <tabba@google.com>
Mon, 3 Jun 2024 12:28:49 +0000 (13:28 +0100)
committerMarc Zyngier <maz@kernel.org>
Tue, 4 Jun 2024 14:06:33 +0000 (15:06 +0100)
Now that we have introduced finalize_init_hyp_mode(), lets
consolidate the initializing of the host_data fpsimd_state and
sve state.

Reviewed-by: Oliver Upton <oliver.upton@linux.dev>
Signed-off-by: Fuad Tabba <tabba@google.com>
Reviewed-by: Mark Brown <broonie@kernel.org>
Link: https://lore.kernel.org/r/20240603122852.3923848-8-tabba@google.com
Signed-off-by: Marc Zyngier <maz@kernel.org>
arch/arm64/include/asm/kvm_host.h
arch/arm64/kvm/arm.c
arch/arm64/kvm/hyp/include/nvhe/pkvm.h
arch/arm64/kvm/hyp/nvhe/pkvm.c
arch/arm64/kvm/hyp/nvhe/setup.c

index 90df7ccec5f4fa40751789a12ad002025e323db1..36b8e97bf49ec4e742f58f1f8cccccf2ed015666 100644 (file)
@@ -550,8 +550,14 @@ struct cpu_sve_state {
 struct kvm_host_data {
        struct kvm_cpu_context host_ctxt;
 
-       struct user_fpsimd_state *fpsimd_state; /* hyp VA */
-       struct cpu_sve_state *sve_state;        /* hyp VA */
+       /*
+        * All pointers in this union are hyp VA.
+        * sve_state is only used in pKVM and if system_supports_sve().
+        */
+       union {
+               struct user_fpsimd_state *fpsimd_state;
+               struct cpu_sve_state *sve_state;
+       };
 
        /* Ownership of the FP regs */
        enum {
index 1acf7415e831b8b6af5b294aa6662ddf3e19d606..59716789fe0f38186cd1700f92757b005f48d1cb 100644 (file)
@@ -2444,14 +2444,22 @@ static void finalize_init_hyp_mode(void)
 {
        int cpu;
 
-       if (!is_protected_kvm_enabled() || !system_supports_sve())
-               return;
+       if (system_supports_sve() && is_protected_kvm_enabled()) {
+               for_each_possible_cpu(cpu) {
+                       struct cpu_sve_state *sve_state;
 
-       for_each_possible_cpu(cpu) {
-               struct cpu_sve_state *sve_state;
+                       sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
+                       per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state =
+                               kern_hyp_va(sve_state);
+               }
+       } else {
+               for_each_possible_cpu(cpu) {
+                       struct user_fpsimd_state *fpsimd_state;
 
-               sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
-               per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = kern_hyp_va(sve_state);
+                       fpsimd_state = &per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->host_ctxt.fp_regs;
+                       per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->fpsimd_state =
+                               kern_hyp_va(fpsimd_state);
+               }
        }
 }
 
index 22f374e9f532968937fd179c6732e458cffeceee..24a9a8330d190396e90abc7202e6b7e86ea1de63 100644 (file)
@@ -59,7 +59,6 @@ static inline bool pkvm_hyp_vcpu_is_protected(struct pkvm_hyp_vcpu *hyp_vcpu)
 }
 
 void pkvm_hyp_vm_table_init(void *tbl);
-void pkvm_host_fpsimd_state_init(void);
 
 int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
                   unsigned long pgd_hva);
index feb27b4ce45925c93675f4c193f4e5e081f0f794..ea67fcbf837617d570732d2e99c890bb317ba380 100644 (file)
@@ -249,17 +249,6 @@ void pkvm_hyp_vm_table_init(void *tbl)
        vm_table = tbl;
 }
 
-void pkvm_host_fpsimd_state_init(void)
-{
-       unsigned long i;
-
-       for (i = 0; i < hyp_nr_cpus; i++) {
-               struct kvm_host_data *host_data = per_cpu_ptr(&kvm_host_data, i);
-
-               host_data->fpsimd_state = &host_data->host_ctxt.fp_regs;
-       }
-}
-
 /*
  * Return the hyp vm structure corresponding to the handle.
  */
index 3fae42479598239af7e186a4fefed9c874017856..f4350ba07b0b0c37440af0783d313108e1cc0a92 100644 (file)
@@ -324,7 +324,6 @@ void __noreturn __pkvm_init_finalise(void)
                goto out;
 
        pkvm_hyp_vm_table_init(vm_table_base);
-       pkvm_host_fpsimd_state_init();
 out:
        /*
         * We tail-called to here from handle___pkvm_init() and will not return,