Update: Additional kernel ranges for vmalloc_sync_mappings
[lttng-modules.git] / include / wrapper / vmalloc.h
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * wrapper/vmalloc.h
4 *
5 * wrapper around vmalloc_sync_all. Using KALLSYMS to get its address when
6 * available, else we need to have a kernel that exports this function to GPL
7 * modules.
8 *
9 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 */
11
12 #ifndef _LTTNG_WRAPPER_VMALLOC_H
13 #define _LTTNG_WRAPPER_VMALLOC_H
14
15 #include <linux/version.h>
16 #include <linux/vmalloc.h>
17 #include <linux/mm.h>
18
19 #ifdef CONFIG_KALLSYMS
20
21 #include <linux/kallsyms.h>
22 #include <wrapper/kallsyms.h>
23 #include <lttng/kernel-version.h>
24
25 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0) \
26 || LTTNG_KERNEL_RANGE(5,4,28, 5,5,0) \
27 || LTTNG_KERNEL_RANGE(5,2,37, 5,3,0) \
28 || LTTNG_KERNEL_RANGE(4,19,113, 4,20,0) \
29 || LTTNG_KERNEL_RANGE(4,14,175, 4,15,0) \
30 || LTTNG_KERNEL_RANGE(4,9,218, 4,10,0) \
31 || LTTNG_KERNEL_RANGE(4,4,218, 4,5,0)) \
32 || LTTNG_UBUNTU_KERNEL_RANGE(4,15,18,97, 4,16,0,0) \
33 || LTTNG_UBUNTU_KERNEL_RANGE(5,0,21,48, 5,1,0,0) \
34 || LTTNG_UBUNTU_KERNEL_RANGE(5,3,18,52, 5,4,0,0)
35
36 static inline
37 void wrapper_vmalloc_sync_mappings(void)
38 {
39 void (*vmalloc_sync_mappings_sym)(void);
40
41 vmalloc_sync_mappings_sym = (void *) kallsyms_lookup_funcptr("vmalloc_sync_mappings");
42 if (vmalloc_sync_mappings_sym) {
43 vmalloc_sync_mappings_sym();
44 } else {
45 #ifdef CONFIG_X86
46 /*
47 * Only x86 needs vmalloc_sync_mappings to make sure LTTng does not
48 * trigger recursive page faults.
49 */
50 printk_once(KERN_WARNING "LTTng: vmalloc_sync_mappings symbol lookup failed.\n");
51 printk_once(KERN_WARNING "Page fault handler and NMI tracing might trigger faults.\n");
52 #endif
53 }
54 }
55
56 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) */
57
58 /*
59 * Map vmalloc_sync_mappings to vmalloc_sync_all() on kernels before 5.7.
60 */
61 static inline
62 void wrapper_vmalloc_sync_mappings(void)
63 {
64 void (*vmalloc_sync_all_sym)(void);
65
66 vmalloc_sync_all_sym = (void *) kallsyms_lookup_funcptr("vmalloc_sync_all");
67 if (vmalloc_sync_all_sym) {
68 vmalloc_sync_all_sym();
69 } else {
70 #ifdef CONFIG_X86
71 /*
72 * Only x86 needs vmalloc_sync_all to make sure LTTng does not
73 * trigger recursive page faults.
74 */
75 printk_once(KERN_WARNING "LTTng: vmalloc_sync_all symbol lookup failed.\n");
76 printk_once(KERN_WARNING "Page fault handler and NMI tracing might trigger faults.\n");
77 #endif
78 }
79 }
80
81 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) */
82
83 #else
84
85 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0) \
86 || LTTNG_KERNEL_RANGE(5,4,28, 5,5,0) \
87 || LTTNG_KERNEL_RANGE(5,2,37, 5,3,0) \
88 || LTTNG_KERNEL_RANGE(4,19,113, 4,20,0) \
89 || LTTNG_KERNEL_RANGE(4,14,175, 4,15,0) \
90 || LTTNG_KERNEL_RANGE(4,9,218, 4,10,0) \
91 || LTTNG_KERNEL_RANGE(4,4,218, 4,5,0)) \
92 || LTTNG_UBUNTU_KERNEL_RANGE(4,15,18,97, 4,18,0,0) \
93 || LTTNG_UBUNTU_KERNEL_RANGE(5,0,21,48, 5,1,0,0) \
94 || LTTNG_UBUNTU_KERNEL_RANGE(5,3,18,52, 5,4,0,0)
95
96 static inline
97 void wrapper_vmalloc_sync_mappings(void)
98 {
99 return vmalloc_sync_mappings();
100 }
101
102 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) */
103
104 static inline
105 void wrapper_vmalloc_sync_mappings(void)
106 {
107 return vmalloc_sync_all();
108 }
109
110 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) */
111
112 #endif
113
114 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0))
115 static inline
116 void *lttng_kvmalloc_node(unsigned long size, gfp_t flags, int node)
117 {
118 void *ret;
119
120 ret = kvmalloc_node(size, flags, node);
121 if (is_vmalloc_addr(ret)) {
122 /*
123 * Make sure we don't trigger recursive page faults in the
124 * tracing fast path.
125 */
126 wrapper_vmalloc_sync_mappings();
127 }
128 return ret;
129 }
130
131 static inline
132 void *lttng_kvzalloc_node(unsigned long size, gfp_t flags, int node)
133 {
134 return lttng_kvmalloc_node(size, flags | __GFP_ZERO, node);
135 }
136
137 static inline
138 void *lttng_kvmalloc(unsigned long size, gfp_t flags)
139 {
140 return lttng_kvmalloc_node(size, flags, NUMA_NO_NODE);
141 }
142
143 static inline
144 void *lttng_kvzalloc(unsigned long size, gfp_t flags)
145 {
146 return lttng_kvzalloc_node(size, flags, NUMA_NO_NODE);
147 }
148
149 static inline
150 void lttng_kvfree(const void *addr)
151 {
152 kvfree(addr);
153 }
154
155 #else
156
157 #include <linux/slab.h>
158
159 static inline
160 void print_vmalloc_node_range_warning(void)
161 {
162 printk_once(KERN_WARNING "LTTng: __vmalloc_node_range symbol lookup failed.\n");
163 printk_once(KERN_WARNING "Tracer performance will be degraded on NUMA systems.\n");
164 printk_once(KERN_WARNING "Please rebuild your kernel with CONFIG_KALLSYMS enabled.\n");
165 }
166
167 /*
168 * kallsyms wrapper of __vmalloc_node with a fallback to kmalloc_node.
169 */
170 static inline
171 void *__lttng_vmalloc_node_range(unsigned long size, unsigned long align,
172 unsigned long start, unsigned long end, gfp_t gfp_mask,
173 pgprot_t prot, unsigned long vm_flags, int node,
174 const void *caller)
175 {
176 #ifdef CONFIG_KALLSYMS
177 /*
178 * If we have KALLSYMS, get * __vmalloc_node_range which is not exported.
179 */
180 void *(*lttng__vmalloc_node_range)(unsigned long size, unsigned long align,
181 unsigned long start, unsigned long end, gfp_t gfp_mask,
182 pgprot_t prot, unsigned long vm_flags, int node,
183 const void *caller);
184
185 lttng__vmalloc_node_range = (void *) kallsyms_lookup_funcptr("__vmalloc_node_range");
186 if (lttng__vmalloc_node_range)
187 return lttng__vmalloc_node_range(size, align, start, end, gfp_mask, prot,
188 vm_flags, node, caller);
189 #endif
190 if (node != NUMA_NO_NODE)
191 print_vmalloc_node_range_warning();
192 return __vmalloc(size, gfp_mask, prot);
193 }
194
195 /**
196 * lttng_kvmalloc_node - attempt to allocate physically contiguous memory, but upon
197 * failure, fall back to non-contiguous (vmalloc) allocation.
198 * @size: size of the request.
199 * @flags: gfp mask for the allocation - must be compatible with GFP_KERNEL.
200 *
201 * Uses kmalloc to get the memory but if the allocation fails then falls back
202 * to the vmalloc allocator. Use lttng_kvfree to free the memory.
203 *
204 * Reclaim modifiers - __GFP_NORETRY, __GFP_REPEAT and __GFP_NOFAIL are not supported
205 */
206 static inline
207 void *lttng_kvmalloc_node(unsigned long size, gfp_t flags, int node)
208 {
209 void *ret;
210
211 /*
212 * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
213 * so the given set of flags has to be compatible.
214 */
215 WARN_ON_ONCE((flags & GFP_KERNEL) != GFP_KERNEL);
216
217 /*
218 * If the allocation fits in a single page, do not fallback.
219 */
220 if (size <= PAGE_SIZE) {
221 return kmalloc_node(size, flags, node);
222 }
223
224 /*
225 * Make sure that larger requests are not too disruptive - no OOM
226 * killer and no allocation failure warnings as we have a fallback
227 */
228 ret = kmalloc_node(size, flags | __GFP_NOWARN | __GFP_NORETRY, node);
229 if (!ret) {
230 ret = __lttng_vmalloc_node_range(size, 1,
231 VMALLOC_START, VMALLOC_END,
232 flags | __GFP_HIGHMEM, PAGE_KERNEL, 0,
233 node, __builtin_return_address(0));
234 /*
235 * Make sure we don't trigger recursive page faults in the
236 * tracing fast path.
237 */
238 wrapper_vmalloc_sync_mappings();
239 }
240 return ret;
241 }
242
243 static inline
244 void *lttng_kvzalloc_node(unsigned long size, gfp_t flags, int node)
245 {
246 return lttng_kvmalloc_node(size, flags | __GFP_ZERO, node);
247 }
248
249 static inline
250 void *lttng_kvmalloc(unsigned long size, gfp_t flags)
251 {
252 return lttng_kvmalloc_node(size, flags, NUMA_NO_NODE);
253 }
254
255 static inline
256 void *lttng_kvzalloc(unsigned long size, gfp_t flags)
257 {
258 return lttng_kvzalloc_node(size, flags, NUMA_NO_NODE);
259 }
260
261 static inline
262 void lttng_kvfree(const void *addr)
263 {
264 if (is_vmalloc_addr(addr)) {
265 vfree(addr);
266 } else {
267 kfree(addr);
268 }
269 }
270 #endif
271
272 #endif /* _LTTNG_WRAPPER_VMALLOC_H */
This page took 0.042621 seconds and 4 git commands to generate.