Re: [PATCH 07/11] KVM: SVM: do not allocate struct svm_cpu_data dynamically

From: Sean Christopherson
Date: Wed Nov 09 2022 - 10:58:21 EST


On Wed, Nov 09, 2022, Paolo Bonzini wrote:
> The svm_data percpu variable is a pointer, but it is allocated when
> KVM is loaded (via svm_hardware_setup), not at hardware_enable time.

Parantheses.

> Just allocate room for it statically, that is more efficient and
> does not waste any memory compared to the status quo.
>
> Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>

Reviewed-by: Sean Christopherson <seanjc@xxxxxxxxxx>

> @@ -3442,7 +3431,7 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
>
> static void reload_tss(struct kvm_vcpu *vcpu)
> {
> - struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
> + struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu);
>
> sd->tss_desc->type = 9; /* available 32/64-bit TSS */
> load_TR_desc();
> @@ -3450,7 +3439,7 @@ static void reload_tss(struct kvm_vcpu *vcpu)
>
> static void pre_svm_run(struct kvm_vcpu *vcpu)
> {
> - struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
> + struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu);
> struct vcpu_svm *svm = to_svm(vcpu);
>
> /*
> @@ -3919,7 +3908,7 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
> if (sev_es_guest(vcpu->kvm)) {
> __svm_sev_es_vcpu_run(svm);
> } else {
> - struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
> + struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu);

At some point we should replace the vcpu->cpu usage with this_cpu_ptr(). All of
the code that does per_cpu_ptr(&svm_data, vcpu->cpu) is doomed if vcpu->cpu isn't
the current CPU.