Skip to content

Commit 20c4892

Browse files
sean-jcbonzini
authored andcommitted
KVM: Export KVM-internal symbols for sub-modules only
Rework the vast majority of KVM's exports to expose symbols only to KVM submodules, i.e. to x86's kvm-{amd,intel}.ko and PPC's kvm-{pr,hv}.ko. With few exceptions, KVM's exported APIs are intended (and safe) for KVM- internal usage only. Keep kvm_get_kvm(), kvm_get_kvm_safe(), and kvm_put_kvm() as normal exports, as they are needed by VFIO, and are generally safe for external usage (though ideally even the get/put APIs would be KVM-internal, and VFIO would pin a VM by grabbing a reference to its associated file). Implement a framework in kvm_types.h in anticipation of providing a macro to restrict KVM-specific kernel exports, i.e. to provide symbol exports for KVM if and only if KVM is built as one or more modules. Link: https://lore.kernel.org/r/[email protected] Cc: Nathan Chancellor <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 15463ee commit 20c4892

File tree

7 files changed

+110
-75
lines changed

7 files changed

+110
-75
lines changed

arch/powerpc/include/asm/Kbuild

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ generated-y += syscall_table_32.h
33
generated-y += syscall_table_64.h
44
generated-y += syscall_table_spu.h
55
generic-y += agp.h
6-
generic-y += kvm_types.h
76
generic-y += mcs_spinlock.h
87
generic-y += qrwlock.h
98
generic-y += early_ioremap.h
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_PPC_KVM_TYPES_H
3+
#define _ASM_PPC_KVM_TYPES_H
4+
5+
#if IS_MODULE(CONFIG_KVM_BOOK3S_64_PR) && IS_MODULE(CONFIG_KVM_BOOK3S_64_HV)
6+
#define KVM_SUB_MODULES kvm-pr,kvm-hv
7+
#elif IS_MODULE(CONFIG_KVM_BOOK3S_64_PR)
8+
#define KVM_SUB_MODULES kvm-pr
9+
#elif IS_MODULE(CONFIG_KVM_BOOK3S_64_HV)
10+
#define KVM_SUB_MODULES kvm-hv
11+
#else
12+
#undef KVM_SUB_MODULES
13+
#endif
14+
15+
#endif

arch/x86/include/asm/kvm_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
#ifndef _ASM_X86_KVM_TYPES_H
33
#define _ASM_X86_KVM_TYPES_H
44

5+
#if IS_MODULE(CONFIG_KVM_AMD) && IS_MODULE(CONFIG_KVM_INTEL)
6+
#define KVM_SUB_MODULES kvm-amd,kvm-intel
7+
#elif IS_MODULE(CONFIG_KVM_AMD)
8+
#define KVM_SUB_MODULES kvm-amd
9+
#elif IS_MODULE(CONFIG_KVM_INTEL)
10+
#define KVM_SUB_MODULES kvm-intel
11+
#else
12+
#undef KVM_SUB_MODULES
13+
#endif
14+
515
#define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
616

717
#endif /* _ASM_X86_KVM_TYPES_H */

include/linux/kvm_types.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
#ifndef __KVM_TYPES_H__
44
#define __KVM_TYPES_H__
55

6+
#include <linux/bits.h>
7+
#include <linux/export.h>
8+
#include <linux/types.h>
9+
#include <asm/kvm_types.h>
10+
11+
#ifdef KVM_SUB_MODULES
12+
#define EXPORT_SYMBOL_FOR_KVM_INTERNAL(symbol) \
13+
EXPORT_SYMBOL_FOR_MODULES(symbol, __stringify(KVM_SUB_MODULES))
14+
#else
15+
#define EXPORT_SYMBOL_FOR_KVM_INTERNAL(symbol)
16+
#endif
17+
18+
#ifndef __ASSEMBLER__
19+
20+
#include <linux/mutex.h>
21+
#include <linux/spinlock_types.h>
22+
623
struct kvm;
724
struct kvm_async_pf;
825
struct kvm_device_ops;
@@ -19,13 +36,6 @@ struct kvm_memslots;
1936

2037
enum kvm_mr_change;
2138

22-
#include <linux/bits.h>
23-
#include <linux/mutex.h>
24-
#include <linux/types.h>
25-
#include <linux/spinlock_types.h>
26-
27-
#include <asm/kvm_types.h>
28-
2939
/*
3040
* Address types:
3141
*
@@ -116,5 +126,6 @@ struct kvm_vcpu_stat_generic {
116126
};
117127

118128
#define KVM_STATS_NAME_SIZE 48
129+
#endif /* !__ASSEMBLER__ */
119130

120131
#endif /* __KVM_TYPES_H__ */

virt/kvm/eventfd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin)
525525

526526
return false;
527527
}
528-
EXPORT_SYMBOL_GPL(kvm_irq_has_notifier);
528+
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_irq_has_notifier);
529529

530530
void kvm_notify_acked_gsi(struct kvm *kvm, int gsi)
531531
{

virt/kvm/guest_memfd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
702702
fput(file);
703703
return r;
704704
}
705-
EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn);
705+
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_gmem_get_pfn);
706706

707707
#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_POPULATE
708708
long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long npages,
@@ -785,5 +785,5 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long
785785
fput(file);
786786
return ret && !i ? ret : i;
787787
}
788-
EXPORT_SYMBOL_GPL(kvm_gmem_populate);
788+
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_gmem_populate);
789789
#endif

0 commit comments

Comments
 (0)