1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
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
9 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #ifndef _LTTNG_WRAPPER_VMALLOC_H
13 #define _LTTNG_WRAPPER_VMALLOC_H
15 #include <linux/version.h>
16 #include <linux/vmalloc.h>
19 #ifdef CONFIG_KALLSYMS
21 #include <linux/kallsyms.h>
22 #include <wrapper/kallsyms.h>
23 #include <lttng-kernel-version.h>
25 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,8,0))
28 * wrapper_vmalloc_sync_mappings was removed in v5.8, the vmalloc mappings
29 * are now synchronized when they are created or torn down.
32 void wrapper_vmalloc_sync_mappings(void)
35 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0) \
36 || LTTNG_KERNEL_RANGE(5,5,12, 5,6,0) \
37 || LTTNG_KERNEL_RANGE(5,4,28, 5,5,0) \
38 || LTTNG_KERNEL_RANGE(5,2,37, 5,3,0) \
39 || LTTNG_KERNEL_RANGE(4,19,113, 4,20,0) \
40 || LTTNG_KERNEL_RANGE(4,14,175, 4,15,0) \
41 || LTTNG_KERNEL_RANGE(4,9,218, 4,10,0) \
42 || LTTNG_KERNEL_RANGE(4,4,218, 4,5,0)) \
43 || LTTNG_UBUNTU_KERNEL_RANGE(4,15,18,97, 4,16,0,0) \
44 || LTTNG_UBUNTU_KERNEL_RANGE(5,0,21,48, 5,1,0,0) \
45 || LTTNG_UBUNTU_KERNEL_RANGE(5,3,18,52, 5,4,0,0)
48 void wrapper_vmalloc_sync_mappings(void)
50 void (*vmalloc_sync_mappings_sym
)(void);
52 vmalloc_sync_mappings_sym
= (void *) kallsyms_lookup_funcptr("vmalloc_sync_mappings");
53 if (vmalloc_sync_mappings_sym
) {
54 vmalloc_sync_mappings_sym();
58 * Only x86 needs vmalloc_sync_mappings to make sure LTTng does not
59 * trigger recursive page faults.
61 printk_once(KERN_WARNING
"LTTng: vmalloc_sync_mappings symbol lookup failed.\n");
62 printk_once(KERN_WARNING
"Page fault handler and NMI tracing might trigger faults.\n");
68 * Canary function to check for 'vmalloc_sync_mappings()' at compile time.
70 * From 'include/linux/vmalloc.h':
72 * void vmalloc_sync_mappings(void);
75 void __canary__vmalloc_sync_mappings(void)
77 vmalloc_sync_mappings();
80 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) */
83 * Map vmalloc_sync_mappings to vmalloc_sync_all() on kernels before 5.6.
86 void wrapper_vmalloc_sync_mappings(void)
88 void (*vmalloc_sync_all_sym
)(void);
90 vmalloc_sync_all_sym
= (void *) kallsyms_lookup_funcptr("vmalloc_sync_all");
91 if (vmalloc_sync_all_sym
) {
92 vmalloc_sync_all_sym();
96 * Only x86 needs vmalloc_sync_all to make sure LTTng does not
97 * trigger recursive page faults.
99 printk_once(KERN_WARNING
"LTTng: vmalloc_sync_all symbol lookup failed.\n");
100 printk_once(KERN_WARNING
"Page fault handler and NMI tracing might trigger faults.\n");
106 * Canary function to check for 'vmalloc_sync_all()' at compile time.
108 * From 'include/linux/vmalloc.h':
110 * void vmalloc_sync_all(void);
113 void __canary__vmalloc_sync_all(void)
118 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) */
120 #else /* CONFIG_KALLSYMS */
122 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,8,0))
125 * wrapper_vmalloc_sync_mappings was removed in v5.8, the vmalloc mappings
126 * are now synchronized when they are created or torn down.
129 void wrapper_vmalloc_sync_mappings(void)
132 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0) \
133 || LTTNG_KERNEL_RANGE(5,5,12, 5,6,0) \
134 || LTTNG_KERNEL_RANGE(5,4,28, 5,5,0) \
135 || LTTNG_KERNEL_RANGE(5,2,37, 5,3,0) \
136 || LTTNG_KERNEL_RANGE(4,19,113, 4,20,0) \
137 || LTTNG_KERNEL_RANGE(4,14,175, 4,15,0) \
138 || LTTNG_KERNEL_RANGE(4,9,218, 4,10,0) \
139 || LTTNG_KERNEL_RANGE(4,4,218, 4,5,0)) \
140 || LTTNG_UBUNTU_KERNEL_RANGE(4,15,18,97, 4,18,0,0) \
141 || LTTNG_UBUNTU_KERNEL_RANGE(5,0,21,48, 5,1,0,0) \
142 || LTTNG_UBUNTU_KERNEL_RANGE(5,3,18,52, 5,4,0,0)
145 void wrapper_vmalloc_sync_mappings(void)
147 return vmalloc_sync_mappings();
150 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) */
153 void wrapper_vmalloc_sync_mappings(void)
155 return vmalloc_sync_all();
158 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) */
162 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0))
164 void *lttng_kvmalloc_node(unsigned long size
, gfp_t flags
, int node
)
168 ret
= kvmalloc_node(size
, flags
, node
);
169 if (is_vmalloc_addr(ret
)) {
171 * Make sure we don't trigger recursive page faults in the
174 wrapper_vmalloc_sync_mappings();
180 void *lttng_kvzalloc_node(unsigned long size
, gfp_t flags
, int node
)
182 return lttng_kvmalloc_node(size
, flags
| __GFP_ZERO
, node
);
186 void *lttng_kvmalloc(unsigned long size
, gfp_t flags
)
188 return lttng_kvmalloc_node(size
, flags
, NUMA_NO_NODE
);
192 void *lttng_kvzalloc(unsigned long size
, gfp_t flags
)
194 return lttng_kvzalloc_node(size
, flags
, NUMA_NO_NODE
);
198 void lttng_kvfree(const void *addr
)
205 #include <linux/slab.h>
208 void print_vmalloc_node_range_warning(void)
210 printk_once(KERN_WARNING
"LTTng: __vmalloc_node_range symbol lookup failed.\n");
211 printk_once(KERN_WARNING
"Tracer performance will be degraded on NUMA systems.\n");
212 printk_once(KERN_WARNING
"Please rebuild your kernel with CONFIG_KALLSYMS enabled.\n");
216 * kallsyms wrapper of __vmalloc_node with a fallback to kmalloc_node.
219 void *__lttng_vmalloc_node_range(unsigned long size
, unsigned long align
,
220 unsigned long start
, unsigned long end
, gfp_t gfp_mask
,
221 pgprot_t prot
, unsigned long vm_flags
, int node
,
224 #ifdef CONFIG_KALLSYMS
226 * If we have KALLSYMS, get * __vmalloc_node_range which is not exported.
228 void *(*lttng__vmalloc_node_range
)(unsigned long size
, unsigned long align
,
229 unsigned long start
, unsigned long end
, gfp_t gfp_mask
,
230 pgprot_t prot
, unsigned long vm_flags
, int node
,
233 lttng__vmalloc_node_range
= (void *) kallsyms_lookup_funcptr("__vmalloc_node_range");
234 if (lttng__vmalloc_node_range
)
235 return lttng__vmalloc_node_range(size
, align
, start
, end
, gfp_mask
, prot
,
236 vm_flags
, node
, caller
);
238 if (node
!= NUMA_NO_NODE
)
239 print_vmalloc_node_range_warning();
240 return __vmalloc(size
, gfp_mask
, prot
);
244 * Canary function to check for '__vmalloc_node_range()' at compile time.
246 * From 'include/linux/vmalloc.h':
248 * extern void *__vmalloc_node_range(unsigned long size, unsigned long align,
249 * unsigned long start, unsigned long end, gfp_t gfp_mask,
250 * pgprot_t prot, unsigned long vm_flags, int node,
251 * const void *caller);
254 void *__canary____lttng_vmalloc_node_range(unsigned long size
, unsigned long align
,
255 unsigned long start
, unsigned long end
, gfp_t gfp_mask
,
256 pgprot_t prot
, unsigned long vm_flags
, int node
,
259 return __vmalloc_node_range(size
, align
, start
, end
, gfp_mask
, prot
,
260 vm_flags
, node
, caller
);
264 * lttng_kvmalloc_node - attempt to allocate physically contiguous memory, but upon
265 * failure, fall back to non-contiguous (vmalloc) allocation.
266 * @size: size of the request.
267 * @flags: gfp mask for the allocation - must be compatible with GFP_KERNEL.
269 * Uses kmalloc to get the memory but if the allocation fails then falls back
270 * to the vmalloc allocator. Use lttng_kvfree to free the memory.
272 * Reclaim modifiers - __GFP_NORETRY, __GFP_REPEAT and __GFP_NOFAIL are not supported
275 void *lttng_kvmalloc_node(unsigned long size
, gfp_t flags
, int node
)
280 * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
281 * so the given set of flags has to be compatible.
283 WARN_ON_ONCE((flags
& GFP_KERNEL
) != GFP_KERNEL
);
286 * If the allocation fits in a single page, do not fallback.
288 if (size
<= PAGE_SIZE
) {
289 return kmalloc_node(size
, flags
, node
);
293 * Make sure that larger requests are not too disruptive - no OOM
294 * killer and no allocation failure warnings as we have a fallback
296 ret
= kmalloc_node(size
, flags
| __GFP_NOWARN
| __GFP_NORETRY
, node
);
298 ret
= __lttng_vmalloc_node_range(size
, 1,
299 VMALLOC_START
, VMALLOC_END
,
300 flags
| __GFP_HIGHMEM
, PAGE_KERNEL
, 0,
301 node
, __builtin_return_address(0));
303 * Make sure we don't trigger recursive page faults in the
306 wrapper_vmalloc_sync_mappings();
312 void *lttng_kvzalloc_node(unsigned long size
, gfp_t flags
, int node
)
314 return lttng_kvmalloc_node(size
, flags
| __GFP_ZERO
, node
);
318 void *lttng_kvmalloc(unsigned long size
, gfp_t flags
)
320 return lttng_kvmalloc_node(size
, flags
, NUMA_NO_NODE
);
324 void *lttng_kvzalloc(unsigned long size
, gfp_t flags
)
326 return lttng_kvzalloc_node(size
, flags
, NUMA_NO_NODE
);
330 void lttng_kvfree(const void *addr
)
332 if (is_vmalloc_addr(addr
)) {
340 #endif /* _LTTNG_WRAPPER_VMALLOC_H */