Fix: lttng_kvmalloc helper NULL pointer OOPS
[lttng-modules.git] / wrapper / vmalloc.h
CommitLineData
a90917c3
MD
1#ifndef _LTTNG_WRAPPER_VMALLOC_H
2#define _LTTNG_WRAPPER_VMALLOC_H
b13f3ebe 3
6d2a620c 4/*
886d51a3 5 * wrapper/vmalloc.h
6d2a620c
MD
6 *
7 * wrapper around vmalloc_sync_all. Using KALLSYMS to get its address when
8 * available, else we need to have a kernel that exports this function to GPL
9 * modules.
10 *
886d51a3
MD
11 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; only
16 * version 2.1 of the License.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6d2a620c
MD
26 */
27
48f5e0b5
MJ
28#include <linux/version.h>
29#include <linux/vmalloc.h>
01ab5113 30#include <linux/mm.h>
48f5e0b5 31
6d2a620c
MD
32#ifdef CONFIG_KALLSYMS
33
34#include <linux/kallsyms.h>
5a2f5e92 35#include <wrapper/kallsyms.h>
6d2a620c
MD
36
37static inline
38void wrapper_vmalloc_sync_all(void)
39{
40 void (*vmalloc_sync_all_sym)(void);
41
c539a324 42 vmalloc_sync_all_sym = (void *) kallsyms_lookup_funcptr("vmalloc_sync_all");
6d2a620c
MD
43 if (vmalloc_sync_all_sym) {
44 vmalloc_sync_all_sym();
45 } else {
46#ifdef CONFIG_X86
47 /*
48 * Only x86 needs vmalloc_sync_all to make sure LTTng does not
49 * trigger recursive page faults.
50 */
e36de50d
MD
51 printk_once(KERN_WARNING "LTTng: vmalloc_sync_all symbol lookup failed.\n");
52 printk_once(KERN_WARNING "Page fault handler and NMI tracing might trigger faults.\n");
6d2a620c
MD
53#endif
54 }
55}
56#else
57
6d2a620c
MD
58static inline
59void wrapper_vmalloc_sync_all(void)
60{
61 return vmalloc_sync_all();
62}
63#endif
b13f3ebe 64
48f5e0b5
MJ
65#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0))
66static inline
67void *lttng_kvmalloc_node(unsigned long size, gfp_t flags, int node)
68{
69 void *ret;
70
71 ret = kvmalloc_node(size, flags, node);
72 if (is_vmalloc_addr(ret)) {
73 /*
74 * Make sure we don't trigger recursive page faults in the
75 * tracing fast path.
76 */
77 wrapper_vmalloc_sync_all();
78 }
79 return ret;
80}
81
82static inline
83void *lttng_kvzalloc_node(unsigned long size, gfp_t flags, int node)
84{
85 return lttng_kvmalloc_node(size, flags | __GFP_ZERO, node);
86}
87
88static inline
89void *lttng_kvmalloc(unsigned long size, gfp_t flags)
90{
91 return lttng_kvmalloc_node(size, flags, NUMA_NO_NODE);
92}
93
94static inline
95void *lttng_kvzalloc(unsigned long size, gfp_t flags)
96{
97 return lttng_kvzalloc_node(size, flags, NUMA_NO_NODE);
98}
99
100static inline
101void lttng_kvfree(const void *addr)
102{
103 kvfree(addr);
104}
105
106#else
107
108#include <linux/slab.h>
48f5e0b5 109
20eb87c9
MD
110static inline
111void print_vmalloc_node_range_warning(void)
112{
113 printk_once(KERN_WARNING "LTTng: __vmalloc_node_range symbol lookup failed.\n");
114 printk_once(KERN_WARNING "Tracer performance will be degraded on NUMA systems.\n");
115 printk_once(KERN_WARNING "Please rebuild your kernel with CONFIG_KALLSYMS enabled.\n");
116}
117
48f5e0b5
MJ
118/*
119 * kallsyms wrapper of __vmalloc_node with a fallback to kmalloc_node.
120 */
121static inline
20eb87c9
MD
122void *__lttng_vmalloc_node_range(unsigned long size, unsigned long align,
123 unsigned long start, unsigned long end, gfp_t gfp_mask,
124 pgprot_t prot, unsigned long vm_flags, int node,
125 const void *caller)
48f5e0b5 126{
48f5e0b5
MJ
127#ifdef CONFIG_KALLSYMS
128 /*
20eb87c9 129 * If we have KALLSYMS, get * __vmalloc_node_range which is not exported.
48f5e0b5 130 */
20eb87c9
MD
131 void *(*lttng__vmalloc_node_range)(unsigned long size, unsigned long align,
132 unsigned long start, unsigned long end, gfp_t gfp_mask,
133 pgprot_t prot, unsigned long vm_flags, int node,
134 const void *caller);
135
136 lttng__vmalloc_node_range = (void *) kallsyms_lookup_funcptr("__vmalloc_node_range");
137 if (lttng__vmalloc_node_range)
138 return lttng__vmalloc_node_range(size, align, start, end, gfp_mask, prot,
139 vm_flags, node, caller);
48f5e0b5 140#endif
20eb87c9
MD
141 if (node != NUMA_NO_NODE)
142 print_vmalloc_node_range_warning();
143 return __vmalloc(size, gfp_mask, prot);
48f5e0b5
MJ
144}
145
146/**
147 * lttng_kvmalloc_node - attempt to allocate physically contiguous memory, but upon
148 * failure, fall back to non-contiguous (vmalloc) allocation.
149 * @size: size of the request.
150 * @flags: gfp mask for the allocation - must be compatible with GFP_KERNEL.
151 *
152 * Uses kmalloc to get the memory but if the allocation fails then falls back
153 * to the vmalloc allocator. Use lttng_kvfree to free the memory.
154 *
155 * Reclaim modifiers - __GFP_NORETRY, __GFP_REPEAT and __GFP_NOFAIL are not supported
156 */
157static inline
158void *lttng_kvmalloc_node(unsigned long size, gfp_t flags, int node)
159{
160 void *ret;
161
162 /*
163 * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
164 * so the given set of flags has to be compatible.
165 */
166 WARN_ON_ONCE((flags & GFP_KERNEL) != GFP_KERNEL);
167
168 /*
169 * If the allocation fits in a single page, do not fallback.
170 */
171 if (size <= PAGE_SIZE) {
172 return kmalloc_node(size, flags, node);
173 }
174
175 /*
176 * Make sure that larger requests are not too disruptive - no OOM
177 * killer and no allocation failure warnings as we have a fallback
178 */
179 ret = kmalloc_node(size, flags | __GFP_NOWARN | __GFP_NORETRY, node);
180 if (!ret) {
20eb87c9
MD
181 ret = __lttng_vmalloc_node_range(size, 1,
182 VMALLOC_START, VMALLOC_END,
183 flags | __GFP_HIGHMEM, PAGE_KERNEL, 0,
184 node, __builtin_return_address(0));
48f5e0b5
MJ
185 /*
186 * Make sure we don't trigger recursive page faults in the
187 * tracing fast path.
188 */
189 wrapper_vmalloc_sync_all();
190 }
191 return ret;
192}
193
194static inline
195void *lttng_kvzalloc_node(unsigned long size, gfp_t flags, int node)
196{
197 return lttng_kvmalloc_node(size, flags | __GFP_ZERO, node);
198}
199
200static inline
201void *lttng_kvmalloc(unsigned long size, gfp_t flags)
202{
203 return lttng_kvmalloc_node(size, flags, NUMA_NO_NODE);
204}
205
206static inline
207void *lttng_kvzalloc(unsigned long size, gfp_t flags)
208{
209 return lttng_kvzalloc_node(size, flags, NUMA_NO_NODE);
210}
211
212static inline
213void lttng_kvfree(const void *addr)
214{
215 if (is_vmalloc_addr(addr)) {
216 vfree(addr);
217 } else {
218 kfree(addr);
219 }
220}
221#endif
222
a90917c3 223#endif /* _LTTNG_WRAPPER_VMALLOC_H */
This page took 0.042243 seconds and 4 git commands to generate.