Re: [PATCH v2 02/29] LoongArch: KVM: Implement kvm module related interface

From: Tianrui Zhao
Date: Mon Feb 20 2023 - 22:02:50 EST




在 2023年02月21日 01:46, Paolo Bonzini 写道:
On 2/20/23 07:57, Tianrui Zhao wrote:
+ order = get_order(kvm_vector_size + kvm_enter_guest_size);
+ addr = (void *)__get_free_pages(GFP_KERNEL, order);
+ if (!addr) {
+ free_percpu(vmcs);
+ return -ENOMEM;
+ }
+
+ memcpy(addr, kvm_vector_entry, kvm_vector_size);
+ memcpy(addr + kvm_vector_size, kvm_enter_guest, kvm_enter_guest_size);
+ flush_icache_range((unsigned long)addr, (unsigned long)addr +
+ kvm_vector_size + kvm_enter_guest_size);
+
+ vpid_mask = read_csr_gstat();
+ vpid_mask = (vpid_mask & CSR_GSTAT_GIDBIT) >> CSR_GSTAT_GIDBIT_SHIFT;
+ if (vpid_mask)
+ vpid_mask = GENMASK(vpid_mask - 1, 0);
+
+ for_each_possible_cpu(cpu) {
+ context = per_cpu_ptr(vmcs, cpu);
+ context->vpid_mask = vpid_mask;
+ context->vpid_cache = context->vpid_mask + 1;
+ context->last_vcpu = NULL;
+ context->kvm_eentry = addr;
+ context->kvm_enter_guest = addr + kvm_vector_size;
+ context->page_order = order;
+ }

A lot of these variables are constant across all pCPUs, any reason to have them in a per-CPU variable? Likewise, since they are all the same as the constant global vmcs variable, why make them part of struct kvm_context instead of just making them globals?

Ok thanks, it is more appropriate to use global variables to save those information.

Thanks
Tianrui Zhao


Also, why does the world switch code need a copy?

Paolo