]> git.proxmox.com Git - mirror_zfs.git/blob - module/spl/spl-kmem.c
Add missing __GFP_HIGHMEM flag to vmalloc
[mirror_zfs.git] / module / spl / spl-kmem.c
1 /*
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include <sys/debug.h>
26 #include <sys/sysmacros.h>
27 #include <sys/kmem.h>
28 #include <sys/vmem.h>
29 #include <linux/mm.h>
30
31 /*
32 * As a general rule kmem_alloc() allocations should be small, preferably
33 * just a few pages since they must by physically contiguous. Therefore, a
34 * rate limited warning will be printed to the console for any kmem_alloc()
35 * which exceeds a reasonable threshold.
36 *
37 * The default warning threshold is set to sixteen pages but capped at 64K to
38 * accommodate systems using large pages. This value was selected to be small
39 * enough to ensure the largest allocations are quickly noticed and fixed.
40 * But large enough to avoid logging any warnings when a allocation size is
41 * larger than optimal but not a serious concern. Since this value is tunable,
42 * developers are encouraged to set it lower when testing so any new largish
43 * allocations are quickly caught. These warnings may be disabled by setting
44 * the threshold to zero.
45 */
46 /* BEGIN CSTYLED */
47 unsigned int spl_kmem_alloc_warn = MIN(16 * PAGE_SIZE, 64 * 1024);
48 module_param(spl_kmem_alloc_warn, uint, 0644);
49 MODULE_PARM_DESC(spl_kmem_alloc_warn,
50 "Warning threshold in bytes for a kmem_alloc()");
51 EXPORT_SYMBOL(spl_kmem_alloc_warn);
52
53 /*
54 * Large kmem_alloc() allocations will fail if they exceed KMALLOC_MAX_SIZE.
55 * Allocations which are marginally smaller than this limit may succeed but
56 * should still be avoided due to the expense of locating a contiguous range
57 * of free pages. Therefore, a maximum kmem size with reasonable safely
58 * margin of 4x is set. Kmem_alloc() allocations larger than this maximum
59 * will quickly fail. Vmem_alloc() allocations less than or equal to this
60 * value will use kmalloc(), but shift to vmalloc() when exceeding this value.
61 */
62 unsigned int spl_kmem_alloc_max = (KMALLOC_MAX_SIZE >> 2);
63 module_param(spl_kmem_alloc_max, uint, 0644);
64 MODULE_PARM_DESC(spl_kmem_alloc_max,
65 "Maximum size in bytes for a kmem_alloc()");
66 EXPORT_SYMBOL(spl_kmem_alloc_max);
67 /* END CSTYLED */
68
69 int
70 kmem_debugging(void)
71 {
72 return (0);
73 }
74 EXPORT_SYMBOL(kmem_debugging);
75
76 char *
77 kmem_vasprintf(const char *fmt, va_list ap)
78 {
79 va_list aq;
80 char *ptr;
81
82 do {
83 va_copy(aq, ap);
84 ptr = kvasprintf(kmem_flags_convert(KM_SLEEP), fmt, aq);
85 va_end(aq);
86 } while (ptr == NULL);
87
88 return (ptr);
89 }
90 EXPORT_SYMBOL(kmem_vasprintf);
91
92 char *
93 kmem_asprintf(const char *fmt, ...)
94 {
95 va_list ap;
96 char *ptr;
97
98 do {
99 va_start(ap, fmt);
100 ptr = kvasprintf(kmem_flags_convert(KM_SLEEP), fmt, ap);
101 va_end(ap);
102 } while (ptr == NULL);
103
104 return (ptr);
105 }
106 EXPORT_SYMBOL(kmem_asprintf);
107
108 static char *
109 __strdup(const char *str, int flags)
110 {
111 char *ptr;
112 int n;
113
114 n = strlen(str);
115 ptr = kmalloc(n + 1, kmem_flags_convert(flags));
116 if (ptr)
117 memcpy(ptr, str, n + 1);
118
119 return (ptr);
120 }
121
122 char *
123 strdup(const char *str)
124 {
125 return (__strdup(str, KM_SLEEP));
126 }
127 EXPORT_SYMBOL(strdup);
128
129 void
130 strfree(char *str)
131 {
132 kfree(str);
133 }
134 EXPORT_SYMBOL(strfree);
135
136 /*
137 * General purpose unified implementation of kmem_alloc(). It is an
138 * amalgamation of Linux and Illumos allocator design. It should never be
139 * exported to ensure that code using kmem_alloc()/kmem_zalloc() remains
140 * relatively portable. Consumers may only access this function through
141 * wrappers that enforce the common flags to ensure portability.
142 */
143 inline void *
144 spl_kmem_alloc_impl(size_t size, int flags, int node)
145 {
146 gfp_t lflags = kmem_flags_convert(flags);
147 int use_vmem = 0;
148 void *ptr;
149
150 /*
151 * Log abnormally large allocations and rate limit the console output.
152 * Allocations larger than spl_kmem_alloc_warn should be performed
153 * through the vmem_alloc()/vmem_zalloc() interfaces.
154 */
155 if ((spl_kmem_alloc_warn > 0) && (size > spl_kmem_alloc_warn) &&
156 !(flags & KM_VMEM)) {
157 printk(KERN_WARNING
158 "Large kmem_alloc(%lu, 0x%x), please file an issue at:\n"
159 "https://github.com/zfsonlinux/zfs/issues/new\n",
160 (unsigned long)size, flags);
161 dump_stack();
162 }
163
164 /*
165 * Use a loop because kmalloc_node() can fail when GFP_KERNEL is used
166 * unlike kmem_alloc() with KM_SLEEP on Illumos.
167 */
168 do {
169 /*
170 * Calling kmalloc_node() when the size >= spl_kmem_alloc_max
171 * is unsafe. This must fail for all for kmem_alloc() and
172 * kmem_zalloc() callers.
173 *
174 * For vmem_alloc() and vmem_zalloc() callers it is permissible
175 * to use __vmalloc(). However, in general use of __vmalloc()
176 * is strongly discouraged because a global lock must be
177 * acquired. Contention on this lock can significantly
178 * impact performance so frequently manipulating the virtual
179 * address space is strongly discouraged.
180 */
181 if ((size > spl_kmem_alloc_max) || use_vmem) {
182 if (flags & KM_VMEM) {
183 ptr = __vmalloc(size, lflags | __GFP_HIGHMEM,
184 PAGE_KERNEL);
185 } else {
186 return (NULL);
187 }
188 } else {
189 ptr = kmalloc_node(size, lflags, node);
190 }
191
192 if (likely(ptr) || (flags & KM_NOSLEEP))
193 return (ptr);
194
195 /*
196 * For vmem_alloc() and vmem_zalloc() callers retry immediately
197 * using __vmalloc() which is unlikely to fail.
198 */
199 if ((flags & KM_VMEM) && (use_vmem == 0)) {
200 use_vmem = 1;
201 continue;
202 }
203
204 /*
205 * Use cond_resched() instead of congestion_wait() to avoid
206 * deadlocking systems where there are no block devices.
207 */
208 cond_resched();
209 } while (1);
210
211 return (NULL);
212 }
213
214 inline void
215 spl_kmem_free_impl(const void *buf, size_t size)
216 {
217 if (is_vmalloc_addr(buf))
218 vfree(buf);
219 else
220 kfree(buf);
221 }
222
223 /*
224 * Memory allocation and accounting for kmem_* * style allocations. When
225 * DEBUG_KMEM is enabled the total memory allocated will be tracked and
226 * any memory leaked will be reported during module unload.
227 *
228 * ./configure --enable-debug-kmem
229 */
230 #ifdef DEBUG_KMEM
231
232 /* Shim layer memory accounting */
233 #ifdef HAVE_ATOMIC64_T
234 atomic64_t kmem_alloc_used = ATOMIC64_INIT(0);
235 unsigned long long kmem_alloc_max = 0;
236 #else /* HAVE_ATOMIC64_T */
237 atomic_t kmem_alloc_used = ATOMIC_INIT(0);
238 unsigned long long kmem_alloc_max = 0;
239 #endif /* HAVE_ATOMIC64_T */
240
241 EXPORT_SYMBOL(kmem_alloc_used);
242 EXPORT_SYMBOL(kmem_alloc_max);
243
244 inline void *
245 spl_kmem_alloc_debug(size_t size, int flags, int node)
246 {
247 void *ptr;
248
249 ptr = spl_kmem_alloc_impl(size, flags, node);
250 if (ptr) {
251 kmem_alloc_used_add(size);
252 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
253 kmem_alloc_max = kmem_alloc_used_read();
254 }
255
256 return (ptr);
257 }
258
259 inline void
260 spl_kmem_free_debug(const void *ptr, size_t size)
261 {
262 kmem_alloc_used_sub(size);
263 spl_kmem_free_impl(ptr, size);
264 }
265
266 /*
267 * When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
268 * but also the location of every alloc and free. When the SPL module is
269 * unloaded a list of all leaked addresses and where they were allocated
270 * will be dumped to the console. Enabling this feature has a significant
271 * impact on performance but it makes finding memory leaks straight forward.
272 *
273 * Not surprisingly with debugging enabled the xmem_locks are very highly
274 * contended particularly on xfree(). If we want to run with this detailed
275 * debugging enabled for anything other than debugging we need to minimize
276 * the contention by moving to a lock per xmem_table entry model.
277 *
278 * ./configure --enable-debug-kmem-tracking
279 */
280 #ifdef DEBUG_KMEM_TRACKING
281
282 #include <linux/hash.h>
283 #include <linux/ctype.h>
284
285 #define KMEM_HASH_BITS 10
286 #define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
287
288 typedef struct kmem_debug {
289 struct hlist_node kd_hlist; /* Hash node linkage */
290 struct list_head kd_list; /* List of all allocations */
291 void *kd_addr; /* Allocation pointer */
292 size_t kd_size; /* Allocation size */
293 const char *kd_func; /* Allocation function */
294 int kd_line; /* Allocation line */
295 } kmem_debug_t;
296
297 static spinlock_t kmem_lock;
298 static struct hlist_head kmem_table[KMEM_TABLE_SIZE];
299 static struct list_head kmem_list;
300
301 static kmem_debug_t *
302 kmem_del_init(spinlock_t *lock, struct hlist_head *table,
303 int bits, const void *addr)
304 {
305 struct hlist_head *head;
306 struct hlist_node *node;
307 struct kmem_debug *p;
308 unsigned long flags;
309
310 spin_lock_irqsave(lock, flags);
311
312 head = &table[hash_ptr((void *)addr, bits)];
313 hlist_for_each(node, head) {
314 p = list_entry(node, struct kmem_debug, kd_hlist);
315 if (p->kd_addr == addr) {
316 hlist_del_init(&p->kd_hlist);
317 list_del_init(&p->kd_list);
318 spin_unlock_irqrestore(lock, flags);
319 return (p);
320 }
321 }
322
323 spin_unlock_irqrestore(lock, flags);
324
325 return (NULL);
326 }
327
328 inline void *
329 spl_kmem_alloc_track(size_t size, int flags,
330 const char *func, int line, int node)
331 {
332 void *ptr = NULL;
333 kmem_debug_t *dptr;
334 unsigned long irq_flags;
335
336 dptr = kmalloc(sizeof (kmem_debug_t), kmem_flags_convert(flags));
337 if (dptr == NULL)
338 return (NULL);
339
340 dptr->kd_func = __strdup(func, flags);
341 if (dptr->kd_func == NULL) {
342 kfree(dptr);
343 return (NULL);
344 }
345
346 ptr = spl_kmem_alloc_debug(size, flags, node);
347 if (ptr == NULL) {
348 kfree(dptr->kd_func);
349 kfree(dptr);
350 return (NULL);
351 }
352
353 INIT_HLIST_NODE(&dptr->kd_hlist);
354 INIT_LIST_HEAD(&dptr->kd_list);
355
356 dptr->kd_addr = ptr;
357 dptr->kd_size = size;
358 dptr->kd_line = line;
359
360 spin_lock_irqsave(&kmem_lock, irq_flags);
361 hlist_add_head(&dptr->kd_hlist,
362 &kmem_table[hash_ptr(ptr, KMEM_HASH_BITS)]);
363 list_add_tail(&dptr->kd_list, &kmem_list);
364 spin_unlock_irqrestore(&kmem_lock, irq_flags);
365
366 return (ptr);
367 }
368
369 inline void
370 spl_kmem_free_track(const void *ptr, size_t size)
371 {
372 kmem_debug_t *dptr;
373
374 /* Ignore NULL pointer since we haven't tracked it at all */
375 if (ptr == NULL)
376 return;
377
378 /* Must exist in hash due to kmem_alloc() */
379 dptr = kmem_del_init(&kmem_lock, kmem_table, KMEM_HASH_BITS, ptr);
380 ASSERT3P(dptr, !=, NULL);
381 ASSERT3S(dptr->kd_size, ==, size);
382
383 kfree(dptr->kd_func);
384 kfree(dptr);
385
386 spl_kmem_free_debug(ptr, size);
387 }
388 #endif /* DEBUG_KMEM_TRACKING */
389 #endif /* DEBUG_KMEM */
390
391 /*
392 * Public kmem_alloc(), kmem_zalloc() and kmem_free() interfaces.
393 */
394 void *
395 spl_kmem_alloc(size_t size, int flags, const char *func, int line)
396 {
397 ASSERT0(flags & ~KM_PUBLIC_MASK);
398
399 #if !defined(DEBUG_KMEM)
400 return (spl_kmem_alloc_impl(size, flags, NUMA_NO_NODE));
401 #elif !defined(DEBUG_KMEM_TRACKING)
402 return (spl_kmem_alloc_debug(size, flags, NUMA_NO_NODE));
403 #else
404 return (spl_kmem_alloc_track(size, flags, func, line, NUMA_NO_NODE));
405 #endif
406 }
407 EXPORT_SYMBOL(spl_kmem_alloc);
408
409 void *
410 spl_kmem_zalloc(size_t size, int flags, const char *func, int line)
411 {
412 ASSERT0(flags & ~KM_PUBLIC_MASK);
413
414 flags |= KM_ZERO;
415
416 #if !defined(DEBUG_KMEM)
417 return (spl_kmem_alloc_impl(size, flags, NUMA_NO_NODE));
418 #elif !defined(DEBUG_KMEM_TRACKING)
419 return (spl_kmem_alloc_debug(size, flags, NUMA_NO_NODE));
420 #else
421 return (spl_kmem_alloc_track(size, flags, func, line, NUMA_NO_NODE));
422 #endif
423 }
424 EXPORT_SYMBOL(spl_kmem_zalloc);
425
426 void
427 spl_kmem_free(const void *buf, size_t size)
428 {
429 #if !defined(DEBUG_KMEM)
430 return (spl_kmem_free_impl(buf, size));
431 #elif !defined(DEBUG_KMEM_TRACKING)
432 return (spl_kmem_free_debug(buf, size));
433 #else
434 return (spl_kmem_free_track(buf, size));
435 #endif
436 }
437 EXPORT_SYMBOL(spl_kmem_free);
438
439 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
440 static char *
441 spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min)
442 {
443 int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size;
444 int i, flag = 1;
445
446 ASSERT(str != NULL && len >= 17);
447 memset(str, 0, len);
448
449 /*
450 * Check for a fully printable string, and while we are at
451 * it place the printable characters in the passed buffer.
452 */
453 for (i = 0; i < size; i++) {
454 str[i] = ((char *)(kd->kd_addr))[i];
455 if (isprint(str[i])) {
456 continue;
457 } else {
458 /*
459 * Minimum number of printable characters found
460 * to make it worthwhile to print this as ascii.
461 */
462 if (i > min)
463 break;
464
465 flag = 0;
466 break;
467 }
468 }
469
470 if (!flag) {
471 sprintf(str, "%02x%02x%02x%02x%02x%02x%02x%02x",
472 *((uint8_t *)kd->kd_addr),
473 *((uint8_t *)kd->kd_addr + 2),
474 *((uint8_t *)kd->kd_addr + 4),
475 *((uint8_t *)kd->kd_addr + 6),
476 *((uint8_t *)kd->kd_addr + 8),
477 *((uint8_t *)kd->kd_addr + 10),
478 *((uint8_t *)kd->kd_addr + 12),
479 *((uint8_t *)kd->kd_addr + 14));
480 }
481
482 return (str);
483 }
484
485 static int
486 spl_kmem_init_tracking(struct list_head *list, spinlock_t *lock, int size)
487 {
488 int i;
489
490 spin_lock_init(lock);
491 INIT_LIST_HEAD(list);
492
493 for (i = 0; i < size; i++)
494 INIT_HLIST_HEAD(&kmem_table[i]);
495
496 return (0);
497 }
498
499 static void
500 spl_kmem_fini_tracking(struct list_head *list, spinlock_t *lock)
501 {
502 unsigned long flags;
503 kmem_debug_t *kd;
504 char str[17];
505
506 spin_lock_irqsave(lock, flags);
507 if (!list_empty(list))
508 printk(KERN_WARNING "%-16s %-5s %-16s %s:%s\n", "address",
509 "size", "data", "func", "line");
510
511 list_for_each_entry(kd, list, kd_list) {
512 printk(KERN_WARNING "%p %-5d %-16s %s:%d\n", kd->kd_addr,
513 (int)kd->kd_size, spl_sprintf_addr(kd, str, 17, 8),
514 kd->kd_func, kd->kd_line);
515 }
516
517 spin_unlock_irqrestore(lock, flags);
518 }
519 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
520
521 int
522 spl_kmem_init(void)
523 {
524
525 #ifdef DEBUG_KMEM
526 kmem_alloc_used_set(0);
527
528
529
530 #ifdef DEBUG_KMEM_TRACKING
531 spl_kmem_init_tracking(&kmem_list, &kmem_lock, KMEM_TABLE_SIZE);
532 #endif /* DEBUG_KMEM_TRACKING */
533 #endif /* DEBUG_KMEM */
534
535 return (0);
536 }
537
538 void
539 spl_kmem_fini(void)
540 {
541 #ifdef DEBUG_KMEM
542 /*
543 * Display all unreclaimed memory addresses, including the
544 * allocation size and the first few bytes of what's located
545 * at that address to aid in debugging. Performance is not
546 * a serious concern here since it is module unload time.
547 */
548 if (kmem_alloc_used_read() != 0)
549 printk(KERN_WARNING "kmem leaked %ld/%llu bytes\n",
550 (unsigned long)kmem_alloc_used_read(), kmem_alloc_max);
551
552 #ifdef DEBUG_KMEM_TRACKING
553 spl_kmem_fini_tracking(&kmem_list, &kmem_lock);
554 #endif /* DEBUG_KMEM_TRACKING */
555 #endif /* DEBUG_KMEM */
556 }