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>
25 void wrapper_vmalloc_sync_all(void)
27 void (*vmalloc_sync_all_sym
)(void);
29 vmalloc_sync_all_sym
= (void *) kallsyms_lookup_funcptr("vmalloc_sync_all");
30 if (vmalloc_sync_all_sym
) {
31 vmalloc_sync_all_sym();
35 * Only x86 needs vmalloc_sync_all to make sure LTTng does not
36 * trigger recursive page faults.
38 printk_once(KERN_WARNING
"LTTng: vmalloc_sync_all symbol lookup failed.\n");
39 printk_once(KERN_WARNING
"Page fault handler and NMI tracing might trigger faults.\n");
46 void wrapper_vmalloc_sync_all(void)
48 return vmalloc_sync_all();
52 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0))
54 void *lttng_kvmalloc_node(unsigned long size
, gfp_t flags
, int node
)
58 ret
= kvmalloc_node(size
, flags
, node
);
59 if (is_vmalloc_addr(ret
)) {
61 * Make sure we don't trigger recursive page faults in the
64 wrapper_vmalloc_sync_all();
70 void *lttng_kvzalloc_node(unsigned long size
, gfp_t flags
, int node
)
72 return lttng_kvmalloc_node(size
, flags
| __GFP_ZERO
, node
);
76 void *lttng_kvmalloc(unsigned long size
, gfp_t flags
)
78 return lttng_kvmalloc_node(size
, flags
, NUMA_NO_NODE
);
82 void *lttng_kvzalloc(unsigned long size
, gfp_t flags
)
84 return lttng_kvzalloc_node(size
, flags
, NUMA_NO_NODE
);
88 void lttng_kvfree(const void *addr
)
95 #include <linux/slab.h>
98 void print_vmalloc_node_range_warning(void)
100 printk_once(KERN_WARNING
"LTTng: __vmalloc_node_range symbol lookup failed.\n");
101 printk_once(KERN_WARNING
"Tracer performance will be degraded on NUMA systems.\n");
102 printk_once(KERN_WARNING
"Please rebuild your kernel with CONFIG_KALLSYMS enabled.\n");
106 * kallsyms wrapper of __vmalloc_node with a fallback to kmalloc_node.
109 void *__lttng_vmalloc_node_range(unsigned long size
, unsigned long align
,
110 unsigned long start
, unsigned long end
, gfp_t gfp_mask
,
111 pgprot_t prot
, unsigned long vm_flags
, int node
,
114 #ifdef CONFIG_KALLSYMS
116 * If we have KALLSYMS, get * __vmalloc_node_range which is not exported.
118 void *(*lttng__vmalloc_node_range
)(unsigned long size
, unsigned long align
,
119 unsigned long start
, unsigned long end
, gfp_t gfp_mask
,
120 pgprot_t prot
, unsigned long vm_flags
, int node
,
123 lttng__vmalloc_node_range
= (void *) kallsyms_lookup_funcptr("__vmalloc_node_range");
124 if (lttng__vmalloc_node_range
)
125 return lttng__vmalloc_node_range(size
, align
, start
, end
, gfp_mask
, prot
,
126 vm_flags
, node
, caller
);
128 if (node
!= NUMA_NO_NODE
)
129 print_vmalloc_node_range_warning();
130 return __vmalloc(size
, gfp_mask
, prot
);
134 * lttng_kvmalloc_node - attempt to allocate physically contiguous memory, but upon
135 * failure, fall back to non-contiguous (vmalloc) allocation.
136 * @size: size of the request.
137 * @flags: gfp mask for the allocation - must be compatible with GFP_KERNEL.
139 * Uses kmalloc to get the memory but if the allocation fails then falls back
140 * to the vmalloc allocator. Use lttng_kvfree to free the memory.
142 * Reclaim modifiers - __GFP_NORETRY, __GFP_REPEAT and __GFP_NOFAIL are not supported
145 void *lttng_kvmalloc_node(unsigned long size
, gfp_t flags
, int node
)
150 * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
151 * so the given set of flags has to be compatible.
153 WARN_ON_ONCE((flags
& GFP_KERNEL
) != GFP_KERNEL
);
156 * If the allocation fits in a single page, do not fallback.
158 if (size
<= PAGE_SIZE
) {
159 return kmalloc_node(size
, flags
, node
);
163 * Make sure that larger requests are not too disruptive - no OOM
164 * killer and no allocation failure warnings as we have a fallback
166 ret
= kmalloc_node(size
, flags
| __GFP_NOWARN
| __GFP_NORETRY
, node
);
168 ret
= __lttng_vmalloc_node_range(size
, 1,
169 VMALLOC_START
, VMALLOC_END
,
170 flags
| __GFP_HIGHMEM
, PAGE_KERNEL
, 0,
171 node
, __builtin_return_address(0));
173 * Make sure we don't trigger recursive page faults in the
176 wrapper_vmalloc_sync_all();
182 void *lttng_kvzalloc_node(unsigned long size
, gfp_t flags
, int node
)
184 return lttng_kvmalloc_node(size
, flags
| __GFP_ZERO
, node
);
188 void *lttng_kvmalloc(unsigned long size
, gfp_t flags
)
190 return lttng_kvmalloc_node(size
, flags
, NUMA_NO_NODE
);
194 void *lttng_kvzalloc(unsigned long size
, gfp_t flags
)
196 return lttng_kvzalloc_node(size
, flags
, NUMA_NO_NODE
);
200 void lttng_kvfree(const void *addr
)
202 if (is_vmalloc_addr(addr
)) {
210 #endif /* _LTTNG_WRAPPER_VMALLOC_H */