xref: /linux/virt/kvm/kvm_mm.h (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef __KVM_MM_H__
4 #define __KVM_MM_H__ 1
5 
6 #include <linux/kvm.h>
7 #include <linux/kvm_types.h>
8 
9 /*
10  * Architectures can choose whether to use an rwlock or spinlock
11  * for the mmu_lock.  These macros, for use in common code
12  * only, avoids using #ifdefs in places that must deal with
13  * multiple architectures.
14  */
15 
16 #ifdef KVM_HAVE_MMU_RWLOCK
17 #define KVM_MMU_LOCK_INIT(kvm)		rwlock_init(&(kvm)->mmu_lock)
18 #define KVM_MMU_LOCK(kvm)		write_lock(&(kvm)->mmu_lock)
19 #define KVM_MMU_UNLOCK(kvm)		write_unlock(&(kvm)->mmu_lock)
20 #else
21 #define KVM_MMU_LOCK_INIT(kvm)		spin_lock_init(&(kvm)->mmu_lock)
22 #define KVM_MMU_LOCK(kvm)		spin_lock(&(kvm)->mmu_lock)
23 #define KVM_MMU_UNLOCK(kvm)		spin_unlock(&(kvm)->mmu_lock)
24 #endif /* KVM_HAVE_MMU_RWLOCK */
25 
26 
27 struct kvm_follow_pfn {
28 	const struct kvm_memory_slot *slot;
29 	const gfn_t gfn;
30 
31 	unsigned long hva;
32 
33 	/* FOLL_* flags modifying lookup behavior, e.g. FOLL_WRITE. */
34 	unsigned int flags;
35 
36 	/*
37 	 * Pin the page (effectively FOLL_PIN, which is an mm/ internal flag).
38 	 * The page *must* be pinned if KVM will write to the page via a kernel
39 	 * mapping, e.g. via kmap(), mremap(), etc.
40 	 */
41 	bool pin;
42 
43 	/*
44 	 * If non-NULL, try to get a writable mapping even for a read fault.
45 	 * Set to true if a writable mapping was obtained.
46 	 */
47 	bool *map_writable;
48 
49 	/*
50 	 * Optional output.  Set to a valid "struct page" if the returned pfn
51 	 * is for a refcounted or pinned struct page, NULL if the returned pfn
52 	 * has no struct page or if the struct page is not being refcounted
53 	 * (e.g. tail pages of non-compound higher order allocations from
54 	 * IO/PFNMAP mappings).
55 	 */
56 	struct page **refcounted_page;
57 };
58 
59 kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp);
60 
61 #ifdef CONFIG_HAVE_KVM_PFNCACHE
62 void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
63 				       unsigned long start,
64 				       unsigned long end);
65 #else
66 static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
67 						     unsigned long start,
68 						     unsigned long end)
69 {
70 }
71 #endif /* HAVE_KVM_PFNCACHE */
72 
73 #ifdef CONFIG_KVM_GUEST_MEMFD
74 int kvm_gmem_init(struct module *module);
75 void kvm_gmem_exit(void);
76 int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args);
77 int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
78 		  unsigned int fd, uoff_t offset);
79 void kvm_gmem_unbind(struct kvm_memory_slot *slot);
80 #else
81 static inline int kvm_gmem_init(struct module *module)
82 {
83 	return 0;
84 }
85 static inline void kvm_gmem_exit(void) {};
86 static inline int kvm_gmem_bind(struct kvm *kvm,
87 					 struct kvm_memory_slot *slot,
88 					 unsigned int fd, uoff_t offset)
89 {
90 	WARN_ON_ONCE(1);
91 	return -EIO;
92 }
93 
94 static inline void kvm_gmem_unbind(struct kvm_memory_slot *slot)
95 {
96 	WARN_ON_ONCE(1);
97 }
98 #endif /* CONFIG_KVM_GUEST_MEMFD */
99 
100 #endif /* __KVM_MM_H__ */
101