Skip to content

Commit 6276c67

Browse files
sean-jchansendc
authored andcommitted
x86: Restrict KVM-induced symbol exports to KVM modules where obvious/possible
Extend KVM's export macro framework to provide EXPORT_SYMBOL_FOR_KVM(), and use the helper macro to export symbols for KVM throughout x86 if and only if KVM will build one or more modules, and only for those modules. To avoid unnecessary exports when CONFIG_KVM=m but kvm.ko will not be built (because no vendor modules are selected), let arch code #define EXPORT_SYMBOL_FOR_KVM to suppress/override the exports. Note, the set of symbols to restrict to KVM was generated by manual search and audit; any "misses" are due to human error, not some grand plan. Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Acked-by: Kai Huang <kai.huang@intel.com> Tested-by: Kai Huang <kai.huang@intel.com> Link: https://patch.msgid.link/20251112173944.1380633-5-seanjc%40google.com
1 parent e6f2d58 commit 6276c67

32 files changed

Lines changed: 144 additions & 104 deletions

File tree

arch/x86/entry/entry.S

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <linux/export.h>
7+
#include <linux/kvm_types.h>
78
#include <linux/linkage.h>
89
#include <linux/objtool.h>
910
#include <asm/msr-index.h>
@@ -29,8 +30,7 @@ SYM_FUNC_START(write_ibpb)
2930
FILL_RETURN_BUFFER %rax, RSB_CLEAR_LOOPS, X86_BUG_IBPB_NO_RET
3031
RET
3132
SYM_FUNC_END(write_ibpb)
32-
/* For KVM */
33-
EXPORT_SYMBOL_GPL(write_ibpb);
33+
EXPORT_SYMBOL_FOR_KVM(write_ibpb);
3434

3535
.popsection
3636

@@ -48,8 +48,7 @@ SYM_CODE_START_NOALIGN(x86_verw_sel)
4848
.word __KERNEL_DS
4949
.align L1_CACHE_BYTES, 0xcc
5050
SYM_CODE_END(x86_verw_sel);
51-
/* For KVM */
52-
EXPORT_SYMBOL_GPL(x86_verw_sel);
51+
EXPORT_SYMBOL_FOR_KVM(x86_verw_sel);
5352

5453
.popsection
5554

arch/x86/entry/entry_64.S

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* - idtentry: Define exception entry points.
2020
*/
2121
#include <linux/export.h>
22+
#include <linux/kvm_types.h>
2223
#include <linux/linkage.h>
2324
#include <asm/segment.h>
2425
#include <asm/cache.h>
@@ -1566,5 +1567,5 @@ SYM_FUNC_START(clear_bhb_loop)
15661567
pop %rbp
15671568
RET
15681569
SYM_FUNC_END(clear_bhb_loop)
1569-
EXPORT_SYMBOL_GPL(clear_bhb_loop)
1570+
EXPORT_SYMBOL_FOR_KVM(clear_bhb_loop)
15701571
STACK_FRAME_NON_STANDARD(clear_bhb_loop)

arch/x86/entry/entry_64_fred.S

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <linux/export.h>
7+
#include <linux/kvm_types.h>
78

89
#include <asm/asm.h>
910
#include <asm/fred.h>
@@ -146,5 +147,5 @@ SYM_FUNC_START(asm_fred_entry_from_kvm)
146147
RET
147148

148149
SYM_FUNC_END(asm_fred_entry_from_kvm)
149-
EXPORT_SYMBOL_GPL(asm_fred_entry_from_kvm);
150+
EXPORT_SYMBOL_FOR_KVM(asm_fred_entry_from_kvm);
150151
#endif

arch/x86/events/amd/core.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <linux/perf_event.h>
33
#include <linux/jump_label.h>
44
#include <linux/export.h>
5+
#include <linux/kvm_types.h>
56
#include <linux/types.h>
67
#include <linux/init.h>
78
#include <linux/slab.h>
@@ -1569,7 +1570,7 @@ void amd_pmu_enable_virt(void)
15691570
/* Reload all events */
15701571
amd_pmu_reload_virt();
15711572
}
1572-
EXPORT_SYMBOL_GPL(amd_pmu_enable_virt);
1573+
EXPORT_SYMBOL_FOR_KVM(amd_pmu_enable_virt);
15731574

15741575
void amd_pmu_disable_virt(void)
15751576
{
@@ -1586,4 +1587,4 @@ void amd_pmu_disable_virt(void)
15861587
/* Reload all events */
15871588
amd_pmu_reload_virt();
15881589
}
1589-
EXPORT_SYMBOL_GPL(amd_pmu_disable_virt);
1590+
EXPORT_SYMBOL_FOR_KVM(amd_pmu_disable_virt);

arch/x86/events/core.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/export.h>
2121
#include <linux/init.h>
2222
#include <linux/kdebug.h>
23+
#include <linux/kvm_types.h>
2324
#include <linux/sched/mm.h>
2425
#include <linux/sched/clock.h>
2526
#include <linux/uaccess.h>
@@ -714,7 +715,7 @@ struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, void *data)
714715
{
715716
return static_call(x86_pmu_guest_get_msrs)(nr, data);
716717
}
717-
EXPORT_SYMBOL_GPL(perf_guest_get_msrs);
718+
EXPORT_SYMBOL_FOR_KVM(perf_guest_get_msrs);
718719

719720
/*
720721
* There may be PMI landing after enabled=0. The PMI hitting could be before or
@@ -3106,7 +3107,7 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
31063107
cap->events_mask_len = x86_pmu.events_mask_len;
31073108
cap->pebs_ept = x86_pmu.pebs_ept;
31083109
}
3109-
EXPORT_SYMBOL_GPL(perf_get_x86_pmu_capability);
3110+
EXPORT_SYMBOL_FOR_KVM(perf_get_x86_pmu_capability);
31103111

31113112
u64 perf_get_hw_event_config(int hw_event)
31123113
{
@@ -3117,4 +3118,4 @@ u64 perf_get_hw_event_config(int hw_event)
31173118

31183119
return 0;
31193120
}
3120-
EXPORT_SYMBOL_GPL(perf_get_hw_event_config);
3121+
EXPORT_SYMBOL_FOR_KVM(perf_get_hw_event_config);

arch/x86/events/intel/lbr.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/kvm_types.h>
23
#include <linux/perf_event.h>
34
#include <linux/types.h>
45

@@ -1705,7 +1706,7 @@ void x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
17051706
lbr->info = x86_pmu.lbr_info;
17061707
lbr->has_callstack = x86_pmu_has_lbr_callstack();
17071708
}
1708-
EXPORT_SYMBOL_GPL(x86_perf_get_lbr);
1709+
EXPORT_SYMBOL_FOR_KVM(x86_perf_get_lbr);
17091710

17101711
struct event_constraint vlbr_constraint =
17111712
__EVENT_CONSTRAINT(INTEL_FIXED_VLBR_EVENT, (1ULL << INTEL_PMC_IDX_FIXED_VLBR),

arch/x86/events/intel/pt.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/limits.h>
1818
#include <linux/slab.h>
1919
#include <linux/device.h>
20+
#include <linux/kvm_types.h>
2021

2122
#include <asm/cpuid/api.h>
2223
#include <asm/perf_event.h>
@@ -82,13 +83,13 @@ u32 intel_pt_validate_cap(u32 *caps, enum pt_capabilities capability)
8283

8384
return (c & cd->mask) >> shift;
8485
}
85-
EXPORT_SYMBOL_GPL(intel_pt_validate_cap);
86+
EXPORT_SYMBOL_FOR_KVM(intel_pt_validate_cap);
8687

8788
u32 intel_pt_validate_hw_cap(enum pt_capabilities cap)
8889
{
8990
return intel_pt_validate_cap(pt_pmu.caps, cap);
9091
}
91-
EXPORT_SYMBOL_GPL(intel_pt_validate_hw_cap);
92+
EXPORT_SYMBOL_FOR_KVM(intel_pt_validate_hw_cap);
9293

9394
static ssize_t pt_cap_show(struct device *cdev,
9495
struct device_attribute *attr,
@@ -1590,7 +1591,7 @@ void intel_pt_handle_vmx(int on)
15901591

15911592
local_irq_restore(flags);
15921593
}
1593-
EXPORT_SYMBOL_GPL(intel_pt_handle_vmx);
1594+
EXPORT_SYMBOL_FOR_KVM(intel_pt_handle_vmx);
15941595

15951596
/*
15961597
* PMU callbacks

arch/x86/include/asm/kvm_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#define KVM_SUB_MODULES kvm-intel
1111
#else
1212
#undef KVM_SUB_MODULES
13+
/*
14+
* Don't export symbols for KVM without vendor modules, as kvm.ko is built iff
15+
* at least one vendor module is enabled.
16+
*/
17+
#define EXPORT_SYMBOL_FOR_KVM(symbol)
1318
#endif
1419

1520
#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40

arch/x86/kernel/apic/apic.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/dmi.h>
3737
#include <linux/smp.h>
3838
#include <linux/mm.h>
39+
#include <linux/kvm_types.h>
3940

4041
#include <xen/xen.h>
4142

@@ -2316,7 +2317,7 @@ u32 x86_msi_msg_get_destid(struct msi_msg *msg, bool extid)
23162317
dest |= msg->arch_addr_hi.destid_8_31 << 8;
23172318
return dest;
23182319
}
2319-
EXPORT_SYMBOL_GPL(x86_msi_msg_get_destid);
2320+
EXPORT_SYMBOL_FOR_KVM(x86_msi_msg_get_destid);
23202321

23212322
static void __init apic_bsp_up_setup(void)
23222323
{

arch/x86/kernel/apic/apic_common.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: GPL-2.0
55
*/
66
#include <linux/irq.h>
7+
#include <linux/kvm_types.h>
78
#include <asm/apic.h>
89

910
#include "local.h"
@@ -25,7 +26,7 @@ u32 default_cpu_present_to_apicid(int mps_cpu)
2526
else
2627
return BAD_APICID;
2728
}
28-
EXPORT_SYMBOL_GPL(default_cpu_present_to_apicid);
29+
EXPORT_SYMBOL_FOR_KVM(default_cpu_present_to_apicid);
2930

3031
/*
3132
* Set up the logical destination ID when the APIC operates in logical

0 commit comments

Comments
 (0)