Skip to content

Commit 30c9539

Browse files
David BrazdilMarc Zyngier
authored andcommitted
kvm: arm64: Set up hyp percpu data for nVHE
Add hyp percpu section to linker script and rename the corresponding ELF sections of hyp/nvhe object files. This moves all nVHE-specific percpu variables to the new hyp percpu section. Allocate sufficient amount of memory for all percpu hyp regions at global KVM init time and create corresponding hyp mappings. The base addresses of hyp percpu regions are kept in a dynamically allocated array in the kernel. Add NULL checks in PMU event-reset code as it may run before KVM memory is initialized. Signed-off-by: David Brazdil <dbrazdil@google.com> Signed-off-by: Marc Zyngier <maz@kernel.org> Acked-by: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20200922204910.7265-10-dbrazdil@google.com
1 parent 2a1198c commit 30c9539

5 files changed

Lines changed: 87 additions & 6 deletions

File tree

‎arch/arm64/include/asm/kvm_asm.h‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,19 @@
6666
#define CHOOSE_VHE_SYM(sym) sym
6767
#define CHOOSE_NVHE_SYM(sym) kvm_nvhe_sym(sym)
6868

69-
#define this_cpu_ptr_nvhe_sym(sym) this_cpu_ptr(&kvm_nvhe_sym(sym))
70-
#define per_cpu_ptr_nvhe_sym(sym, cpu) per_cpu_ptr(&kvm_nvhe_sym(sym), cpu)
69+
/*
70+
* Compute pointer to a symbol defined in nVHE percpu region.
71+
* Returns NULL if percpu memory has not been allocated yet.
72+
*/
73+
#define this_cpu_ptr_nvhe_sym(sym) per_cpu_ptr_nvhe_sym(sym, smp_processor_id())
74+
#define per_cpu_ptr_nvhe_sym(sym, cpu) \
75+
({ \
76+
unsigned long base, off; \
77+
base = kvm_arm_hyp_percpu_base[cpu]; \
78+
off = (unsigned long)&CHOOSE_NVHE_SYM(sym) - \
79+
(unsigned long)&CHOOSE_NVHE_SYM(__per_cpu_start); \
80+
base ? (typeof(CHOOSE_NVHE_SYM(sym))*)(base + off) : NULL; \
81+
})
7182

7283
#ifndef __KVM_NVHE_HYPERVISOR__
7384
/*
@@ -117,6 +128,10 @@ DECLARE_KVM_HYP_SYM(__kvm_hyp_vector);
117128
#define __kvm_hyp_init CHOOSE_NVHE_SYM(__kvm_hyp_init)
118129
#define __kvm_hyp_vector CHOOSE_HYP_SYM(__kvm_hyp_vector)
119130

131+
extern unsigned long kvm_arm_hyp_percpu_base[NR_CPUS];
132+
DECLARE_KVM_NVHE_SYM(__per_cpu_start);
133+
DECLARE_KVM_NVHE_SYM(__per_cpu_end);
134+
120135
#ifdef CONFIG_KVM_INDIRECT_VECTORS
121136
extern atomic_t arm64_el2_vector_last_slot;
122137
DECLARE_KVM_HYP_SYM(__bp_harden_hyp_vecs);

‎arch/arm64/kernel/vmlinux.lds.S‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ jiffies = jiffies_64;
2828
__start___kvm_ex_table = .; \
2929
*(__kvm_ex_table) \
3030
__stop___kvm_ex_table = .;
31+
32+
#define HYPERVISOR_PERCPU_SECTION \
33+
. = ALIGN(PAGE_SIZE); \
34+
HYP_SECTION_NAME(.data..percpu) : { \
35+
*(HYP_SECTION_NAME(.data..percpu)) \
36+
}
3137
#else /* CONFIG_KVM */
3238
#define HYPERVISOR_EXTABLE
39+
#define HYPERVISOR_PERCPU_SECTION
3340
#endif
3441

3542
#define HYPERVISOR_TEXT \
@@ -195,6 +202,7 @@ SECTIONS
195202
}
196203

197204
PERCPU_SECTION(L1_CACHE_BYTES)
205+
HYPERVISOR_PERCPU_SECTION
198206

199207
.rela.dyn : ALIGN(8) {
200208
*(.rela .rela*)

‎arch/arm64/kvm/arm.c‎

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ __asm__(".arch_extension virt");
4747
#endif
4848

4949
static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
50+
unsigned long kvm_arm_hyp_percpu_base[NR_CPUS];
5051

5152
/* The VMID used in the VTTBR */
5253
static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
@@ -1255,6 +1256,19 @@ long kvm_arch_vm_ioctl(struct file *filp,
12551256
}
12561257
}
12571258

1259+
static unsigned long nvhe_percpu_size(void)
1260+
{
1261+
return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
1262+
(unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
1263+
}
1264+
1265+
static unsigned long nvhe_percpu_order(void)
1266+
{
1267+
unsigned long size = nvhe_percpu_size();
1268+
1269+
return size ? get_order(size) : 0;
1270+
}
1271+
12581272
static void cpu_init_hyp_mode(void)
12591273
{
12601274
phys_addr_t pgd_ptr;
@@ -1270,8 +1284,8 @@ static void cpu_init_hyp_mode(void)
12701284
* kernel's mapping to the linear mapping, and store it in tpidr_el2
12711285
* so that we can use adr_l to access per-cpu variables in EL2.
12721286
*/
1273-
tpidr_el2 = ((unsigned long)this_cpu_ptr(&kvm_host_data) -
1274-
(unsigned long)kvm_ksym_ref(&kvm_host_data));
1287+
tpidr_el2 = (unsigned long)this_cpu_ptr_nvhe_sym(__per_cpu_start) -
1288+
(unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
12751289

12761290
pgd_ptr = kvm_mmu_get_httbr();
12771291
hyp_stack_ptr = __this_cpu_read(kvm_arm_hyp_stack_page) + PAGE_SIZE;
@@ -1461,8 +1475,10 @@ static void teardown_hyp_mode(void)
14611475
int cpu;
14621476

14631477
free_hyp_pgds();
1464-
for_each_possible_cpu(cpu)
1478+
for_each_possible_cpu(cpu) {
14651479
free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1480+
free_pages(kvm_arm_hyp_percpu_base[cpu], nvhe_percpu_order());
1481+
}
14661482
}
14671483

14681484
/**
@@ -1495,6 +1511,24 @@ static int init_hyp_mode(void)
14951511
per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
14961512
}
14971513

1514+
/*
1515+
* Allocate and initialize pages for Hypervisor-mode percpu regions.
1516+
*/
1517+
for_each_possible_cpu(cpu) {
1518+
struct page *page;
1519+
void *page_addr;
1520+
1521+
page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
1522+
if (!page) {
1523+
err = -ENOMEM;
1524+
goto out_err;
1525+
}
1526+
1527+
page_addr = page_address(page);
1528+
memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
1529+
kvm_arm_hyp_percpu_base[cpu] = (unsigned long)page_addr;
1530+
}
1531+
14981532
/*
14991533
* Map the Hyp-code called directly from the host
15001534
*/
@@ -1539,6 +1573,21 @@ static int init_hyp_mode(void)
15391573
}
15401574
}
15411575

1576+
/*
1577+
* Map Hyp percpu pages
1578+
*/
1579+
for_each_possible_cpu(cpu) {
1580+
char *percpu_begin = (char *)kvm_arm_hyp_percpu_base[cpu];
1581+
char *percpu_end = percpu_begin + nvhe_percpu_size();
1582+
1583+
err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
1584+
1585+
if (err) {
1586+
kvm_err("Cannot map hyp percpu region\n");
1587+
goto out_err;
1588+
}
1589+
}
1590+
15421591
for_each_possible_cpu(cpu) {
15431592
kvm_host_data_t *cpu_data;
15441593

‎arch/arm64/kvm/hyp/nvhe/hyp.lds.S‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
*/
88

99
#include <asm/hyp_image.h>
10+
#include <asm-generic/vmlinux.lds.h>
11+
#include <asm/cache.h>
12+
#include <asm/memory.h>
1013

1114
SECTIONS {
1215
HYP_SECTION(.text)
16+
HYP_SECTION_NAME(.data..percpu) : {
17+
PERCPU_INPUT(L1_CACHE_BYTES)
18+
}
1319
}

‎arch/arm64/kvm/pmu.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr)
3333
{
3434
struct kvm_host_data *ctx = this_cpu_ptr_hyp_sym(kvm_host_data);
3535

36-
if (!kvm_pmu_switch_needed(attr))
36+
if (!ctx || !kvm_pmu_switch_needed(attr))
3737
return;
3838

3939
if (!attr->exclude_host)
@@ -49,6 +49,9 @@ void kvm_clr_pmu_events(u32 clr)
4949
{
5050
struct kvm_host_data *ctx = this_cpu_ptr_hyp_sym(kvm_host_data);
5151

52+
if (!ctx)
53+
return;
54+
5255
ctx->pmu_events.events_host &= ~clr;
5356
ctx->pmu_events.events_guest &= ~clr;
5457
}

0 commit comments

Comments
 (0)