Skip to content

Commit 07da1ff

Browse files
author
Marc Zyngier
committed
KVM: arm64: Remove host_cpu_context member from vcpu structure
For very long, we have kept this pointer back to the per-cpu host state, despite having working per-cpu accessors at EL2 for some time now. Recent investigations have shown that this pointer is easy to abuse in preemptible context, which is a sure sign that it would better be gone. Not to mention that a per-cpu pointer is faster to access at all times. Reported-by: Andrew Scull <[email protected]> Acked-by: Mark Rutland <[email protected] Reviewed-by: Andrew Scull <[email protected]> Signed-off-by: Marc Zyngier <[email protected]>
1 parent b990d37 commit 07da1ff

File tree

6 files changed

+11
-19
lines changed

6 files changed

+11
-19
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,6 @@ struct kvm_vcpu_arch {
284284
struct kvm_guest_debug_arch vcpu_debug_state;
285285
struct kvm_guest_debug_arch external_debug_state;
286286

287-
/* Pointer to host CPU context */
288-
struct kvm_cpu_context *host_cpu_context;
289-
290287
struct thread_info *host_thread_info; /* hyp VA */
291288
struct user_fpsimd_state *host_fpsimd_state; /* hyp VA */
292289

arch/arm64/kvm/arm.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,8 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
340340
void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
341341
{
342342
int *last_ran;
343-
kvm_host_data_t *cpu_data;
344343

345344
last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran);
346-
cpu_data = this_cpu_ptr(&kvm_host_data);
347345

348346
/*
349347
* We might get preempted before the vCPU actually runs, but
@@ -355,7 +353,6 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
355353
}
356354

357355
vcpu->cpu = cpu;
358-
vcpu->arch.host_cpu_context = &cpu_data->host_ctxt;
359356

360357
kvm_vgic_load(vcpu);
361358
kvm_timer_vcpu_load(vcpu);

arch/arm64/kvm/hyp/debug-sr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ void __hyp_text __debug_switch_to_guest(struct kvm_vcpu *vcpu)
185185
if (!(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
186186
return;
187187

188-
host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
188+
host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt;
189189
guest_ctxt = &vcpu->arch.ctxt;
190190
host_dbg = &vcpu->arch.host_debug_state.regs;
191191
guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
@@ -207,7 +207,7 @@ void __hyp_text __debug_switch_to_host(struct kvm_vcpu *vcpu)
207207
if (!(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
208208
return;
209209

210-
host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
210+
host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt;
211211
guest_ctxt = &vcpu->arch.ctxt;
212212
host_dbg = &vcpu->arch.host_debug_state.regs;
213213
guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);

arch/arm64/kvm/hyp/switch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ static bool __hyp_text __hyp_handle_ptrauth(struct kvm_vcpu *vcpu)
532532
!esr_is_ptrauth_trap(kvm_vcpu_get_hsr(vcpu)))
533533
return false;
534534

535-
ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
535+
ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt;
536536
__ptrauth_save_key(ctxt->sys_regs, APIA);
537537
__ptrauth_save_key(ctxt->sys_regs, APIB);
538538
__ptrauth_save_key(ctxt->sys_regs, APDA);
@@ -703,7 +703,7 @@ static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
703703
struct kvm_cpu_context *guest_ctxt;
704704
u64 exit_code;
705705

706-
host_ctxt = vcpu->arch.host_cpu_context;
706+
host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt;
707707
host_ctxt->__hyp_running_vcpu = vcpu;
708708
guest_ctxt = &vcpu->arch.ctxt;
709709

@@ -808,7 +808,7 @@ int __hyp_text __kvm_vcpu_run_nvhe(struct kvm_vcpu *vcpu)
808808

809809
vcpu = kern_hyp_va(vcpu);
810810

811-
host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
811+
host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt;
812812
host_ctxt->__hyp_running_vcpu = vcpu;
813813
guest_ctxt = &vcpu->arch.ctxt;
814814

arch/arm64/kvm/hyp/sysreg-sr.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,13 @@ void __hyp_text __sysreg32_restore_state(struct kvm_vcpu *vcpu)
265265
*/
266266
void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu)
267267
{
268-
struct kvm_cpu_context *host_ctxt = vcpu->arch.host_cpu_context;
269268
struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
269+
struct kvm_cpu_context *host_ctxt;
270270

271271
if (!has_vhe())
272272
return;
273273

274+
host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt;
274275
__sysreg_save_user_state(host_ctxt);
275276

276277
/*
@@ -301,12 +302,13 @@ void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu)
301302
*/
302303
void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu)
303304
{
304-
struct kvm_cpu_context *host_ctxt = vcpu->arch.host_cpu_context;
305305
struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
306+
struct kvm_cpu_context *host_ctxt;
306307

307308
if (!has_vhe())
308309
return;
309310

311+
host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt;
310312
deactivate_traps_vhe_put();
311313

312314
__sysreg_save_el1_state(guest_ctxt);

arch/arm64/kvm/pmu.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,13 @@ static void kvm_vcpu_pmu_disable_el0(unsigned long events)
163163
*/
164164
void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu)
165165
{
166-
struct kvm_cpu_context *host_ctxt;
167166
struct kvm_host_data *host;
168167
u32 events_guest, events_host;
169168

170169
if (!has_vhe())
171170
return;
172171

173-
host_ctxt = vcpu->arch.host_cpu_context;
174-
host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
172+
host = this_cpu_ptr(&kvm_host_data);
175173
events_guest = host->pmu_events.events_guest;
176174
events_host = host->pmu_events.events_host;
177175

@@ -184,15 +182,13 @@ void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu)
184182
*/
185183
void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu)
186184
{
187-
struct kvm_cpu_context *host_ctxt;
188185
struct kvm_host_data *host;
189186
u32 events_guest, events_host;
190187

191188
if (!has_vhe())
192189
return;
193190

194-
host_ctxt = vcpu->arch.host_cpu_context;
195-
host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
191+
host = this_cpu_ptr(&kvm_host_data);
196192
events_guest = host->pmu_events.events_guest;
197193
events_host = host->pmu_events.events_host;
198194

0 commit comments

Comments
 (0)