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