Drop support for kernels < 4.4 from 'wrapper/vmalloc.h'
[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 <lttng/kernel-version.h>
16 #include <linux/vmalloc.h>
17 #include <linux/slab.h>
18 #include <linux/mm.h>
19
20 #ifdef CONFIG_KALLSYMS
21
22 #include <linux/kallsyms.h>
23 #include <wrapper/kallsyms.h>
24 #include <lttng/kernel-version.h>
25
26 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,8,0))
27
28 /*
29 * wrapper_vmalloc_sync_mappings was removed in v5.8, the vmalloc mappings
30 * are now synchronized when they are created or torn down.
31 */
32 static inline
33 void wrapper_vmalloc_sync_mappings(void)
34 {}
35
36 #elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0) \
37 || LTTNG_KERNEL_RANGE(5,5,12, 5,6,0) \
38 || LTTNG_KERNEL_RANGE(5,4,28, 5,5,0) \
39 || LTTNG_KERNEL_RANGE(5,2,37, 5,3,0) \
40 || LTTNG_KERNEL_RANGE(4,19,113, 4,20,0) \
41 || LTTNG_KERNEL_RANGE(4,14,175, 4,15,0) \
42 || LTTNG_KERNEL_RANGE(4,9,218, 4,10,0) \
43 || LTTNG_KERNEL_RANGE(4,4,218, 4,5,0) \
44 || LTTNG_UBUNTU_KERNEL_RANGE(4,15,18,97, 4,16,0,0) \
45 || LTTNG_UBUNTU_KERNEL_RANGE(5,0,21,48, 5,1,0,0) \
46 || LTTNG_UBUNTU_KERNEL_RANGE(5,3,18,52, 5,4,0,0) \
47 || LTTNG_RHEL_KERNEL_RANGE(4,18,0,240,0,0, 4,19,0,0,0,0))
48
49 static inline
50 void wrapper_vmalloc_sync_mappings(void)
51 {
52 void (*vmalloc_sync_mappings_sym)(void);
53
54 vmalloc_sync_mappings_sym = (void *) kallsyms_lookup_funcptr("vmalloc_sync_mappings");
55 if (vmalloc_sync_mappings_sym) {
56 vmalloc_sync_mappings_sym();
57 } else {
58 #ifdef CONFIG_X86
59 /*
60 * Only x86 needs vmalloc_sync_mappings to make sure LTTng does not
61 * trigger recursive page faults.
62 */
63 printk_once(KERN_WARNING "LTTng: vmalloc_sync_mappings symbol lookup failed.\n");
64 printk_once(KERN_WARNING "LTTng: Page fault handler and NMI tracing might trigger faults.\n");
65 #endif
66 }
67 }
68
69 /*
70 * Canary function to check for 'vmalloc_sync_mappings()' at compile time.
71 *
72 * From 'include/linux/vmalloc.h':
73 *
74 * void vmalloc_sync_mappings(void);
75 */
76 static inline
77 void __canary__vmalloc_sync_mappings(void)
78 {
79 vmalloc_sync_mappings();
80 }
81
82 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0)) */
83
84 /*
85 * Map vmalloc_sync_mappings to vmalloc_sync_all() on kernels before 5.6.
86 */
87 static inline
88 void wrapper_vmalloc_sync_mappings(void)
89 {
90 void (*vmalloc_sync_all_sym)(void);
91
92 vmalloc_sync_all_sym = (void *) kallsyms_lookup_funcptr("vmalloc_sync_all");
93 if (vmalloc_sync_all_sym) {
94 vmalloc_sync_all_sym();
95 } else {
96 #ifdef CONFIG_X86
97 /*
98 * Only x86 needs vmalloc_sync_all to make sure LTTng does not
99 * trigger recursive page faults.
100 */
101 printk_once(KERN_WARNING "LTTng: vmalloc_sync_all symbol lookup failed.\n");
102 printk_once(KERN_WARNING "LTTng: Page fault handler and NMI tracing might trigger faults.\n");
103 #endif
104 }
105 }
106
107 /*
108 * Canary function to check for 'vmalloc_sync_all()' at compile time.
109 *
110 * From 'include/linux/vmalloc.h':
111 *
112 * void vmalloc_sync_all(void);
113 */
114 static inline
115 void __canary__vmalloc_sync_all(void)
116 {
117 vmalloc_sync_all();
118 }
119
120 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0)) */
121
122 #else /* CONFIG_KALLSYMS */
123
124 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,8,0))
125
126 /*
127 * wrapper_vmalloc_sync_mappings was removed in v5.8, the vmalloc mappings
128 * are now synchronized when they are created or torn down.
129 */
130 static inline
131 void wrapper_vmalloc_sync_mappings(void)
132 {}
133
134 #elif (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0) \
135 || LTTNG_KERNEL_RANGE(5,5,12, 5,6,0) \
136 || LTTNG_KERNEL_RANGE(5,4,28, 5,5,0) \
137 || LTTNG_KERNEL_RANGE(5,2,37, 5,3,0) \
138 || LTTNG_KERNEL_RANGE(4,19,113, 4,20,0) \
139 || LTTNG_KERNEL_RANGE(4,14,175, 4,15,0) \
140 || LTTNG_KERNEL_RANGE(4,9,218, 4,10,0) \
141 || LTTNG_KERNEL_RANGE(4,4,218, 4,5,0)) \
142 || LTTNG_UBUNTU_KERNEL_RANGE(4,15,18,97, 4,18,0,0) \
143 || LTTNG_UBUNTU_KERNEL_RANGE(5,0,21,48, 5,1,0,0) \
144 || LTTNG_UBUNTU_KERNEL_RANGE(5,3,18,52, 5,4,0,0)
145
146 static inline
147 void wrapper_vmalloc_sync_mappings(void)
148 {
149 return vmalloc_sync_mappings();
150 }
151
152 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0)) */
153
154 static inline
155 void wrapper_vmalloc_sync_mappings(void)
156 {
157 return vmalloc_sync_all();
158 }
159
160 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,6,0)) */
161
162 #endif /* CONFIG_KALLSYMS */
163
164 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,12,0))
165 static inline
166 void *lttng_kvmalloc_node(unsigned long size, gfp_t flags, int node)
167 {
168 void *ret;
169
170 ret = kvmalloc_node(size, flags, node);
171 if (is_vmalloc_addr(ret)) {
172 /*
173 * Make sure we don't trigger recursive page faults in the
174 * tracing fast path.
175 */
176 wrapper_vmalloc_sync_mappings();
177 }
178 return ret;
179 }
180
181 static inline
182 void *lttng_kvzalloc_node(unsigned long size, gfp_t flags, int node)
183 {
184 return lttng_kvmalloc_node(size, flags | __GFP_ZERO, node);
185 }
186
187 static inline
188 void *lttng_kvmalloc(unsigned long size, gfp_t flags)
189 {
190 return lttng_kvmalloc_node(size, flags, NUMA_NO_NODE);
191 }
192
193 static inline
194 void *lttng_kvzalloc(unsigned long size, gfp_t flags)
195 {
196 return lttng_kvzalloc_node(size, flags, NUMA_NO_NODE);
197 }
198
199 static inline
200 void lttng_kvfree(const void *addr)
201 {
202 kvfree(addr);
203 }
204
205 #else /* >= LTTNG_KERNEL_VERSION(4,12,0) */
206
207 #include <linux/slab.h>
208
209 static inline
210 void print_vmalloc_node_range_warning(void)
211 {
212 printk_once(KERN_WARNING "LTTng: __vmalloc_node_range symbol lookup failed.\n");
213 printk_once(KERN_WARNING "LTTng: Tracer performance will be degraded on NUMA systems.\n");
214 printk_once(KERN_WARNING "LTTng: Please rebuild your kernel with CONFIG_KALLSYMS enabled.\n");
215 }
216
217 /*
218 * kallsyms wrapper of __vmalloc_node with a fallback to kmalloc_node.
219 */
220 static inline
221 void *__lttng_vmalloc_node_range(unsigned long size, unsigned long align,
222 unsigned long start, unsigned long end, gfp_t gfp_mask,
223 pgprot_t prot, unsigned long vm_flags, int node,
224 const void *caller)
225 {
226 #ifdef CONFIG_KALLSYMS
227 /*
228 * If we have KALLSYMS, get * __vmalloc_node_range which is not exported.
229 */
230 void *(*lttng__vmalloc_node_range)(unsigned long size, unsigned long align,
231 unsigned long start, unsigned long end, gfp_t gfp_mask,
232 pgprot_t prot, unsigned long vm_flags, int node,
233 const void *caller);
234
235 lttng__vmalloc_node_range = (void *) kallsyms_lookup_funcptr("__vmalloc_node_range");
236 if (lttng__vmalloc_node_range)
237 return lttng__vmalloc_node_range(size, align, start, end, gfp_mask, prot,
238 vm_flags, node, caller);
239 #endif
240 if (node != NUMA_NO_NODE)
241 print_vmalloc_node_range_warning();
242 return __vmalloc(size, gfp_mask, prot);
243 }
244
245 /*
246 * Canary function to check for '__vmalloc_node_range()' at compile time.
247 *
248 * From 'include/linux/vmalloc.h':
249 *
250 * extern void *__vmalloc_node_range(unsigned long size, unsigned long align,
251 * unsigned long start, unsigned long end, gfp_t gfp_mask,
252 * pgprot_t prot, unsigned long vm_flags, int node,
253 * const void *caller);
254 */
255 static inline
256 void *__canary____lttng_vmalloc_node_range(unsigned long size, unsigned long align,
257 unsigned long start, unsigned long end, gfp_t gfp_mask,
258 pgprot_t prot, unsigned long vm_flags, int node,
259 const void *caller)
260 {
261 return __vmalloc_node_range(size, align, start, end, gfp_mask, prot,
262 vm_flags, node, caller);
263 }
264
265 /**
266 * lttng_kvmalloc_node - attempt to allocate physically contiguous memory, but upon
267 * failure, fall back to non-contiguous (vmalloc) allocation.
268 * @size: size of the request.
269 * @flags: gfp mask for the allocation - must be compatible with GFP_KERNEL.
270 *
271 * Uses kmalloc to get the memory but if the allocation fails then falls back
272 * to the vmalloc allocator. Use lttng_kvfree to free the memory.
273 *
274 * Reclaim modifiers - __GFP_NORETRY, __GFP_REPEAT and __GFP_NOFAIL are not supported
275 */
276 static inline
277 void *lttng_kvmalloc_node(unsigned long size, gfp_t flags, int node)
278 {
279 void *ret;
280
281 /*
282 * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
283 * so the given set of flags has to be compatible.
284 */
285 WARN_ON_ONCE((flags & GFP_KERNEL) != GFP_KERNEL);
286
287 /*
288 * If the allocation fits in a single page, do not fallback.
289 */
290 if (size <= PAGE_SIZE) {
291 return kmalloc_node(size, flags, node);
292 }
293
294 /*
295 * Make sure that larger requests are not too disruptive - no OOM
296 * killer and no allocation failure warnings as we have a fallback
297 */
298 ret = kmalloc_node(size, flags | __GFP_NOWARN | __GFP_NORETRY, node);
299 if (!ret) {
300 ret = __lttng_vmalloc_node_range(size, 1,
301 VMALLOC_START, VMALLOC_END,
302 flags | __GFP_HIGHMEM, PAGE_KERNEL, 0,
303 node, __builtin_return_address(0));
304 /*
305 * Make sure we don't trigger recursive page faults in the
306 * tracing fast path.
307 */
308 wrapper_vmalloc_sync_mappings();
309 }
310 return ret;
311 }
312
313 static inline
314 void *lttng_kvzalloc_node(unsigned long size, gfp_t flags, int node)
315 {
316 return lttng_kvmalloc_node(size, flags | __GFP_ZERO, node);
317 }
318
319 static inline
320 void *lttng_kvmalloc(unsigned long size, gfp_t flags)
321 {
322 return lttng_kvmalloc_node(size, flags, NUMA_NO_NODE);
323 }
324
325 static inline
326 void *lttng_kvzalloc(unsigned long size, gfp_t flags)
327 {
328 return lttng_kvzalloc_node(size, flags, NUMA_NO_NODE);
329 }
330
331 static inline
332 void lttng_kvfree(const void *addr)
333 {
334 if (is_vmalloc_addr(addr)) {
335 vfree(addr);
336 } else {
337 kfree(addr);
338 }
339 }
340 #endif /* >= LTTNG_KERNEL_VERSION(4,12,0) */
341
342 #endif /* _LTTNG_WRAPPER_VMALLOC_H */
This page took 0.036445 seconds and 4 git commands to generate.