]> git.proxmox.com Git - mirror_zfs.git/blob - module/spl/spl-kmem.c
Linux 5.0 compat: Fix bio_set_dev()
[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, PAGE_KERNEL);
184 } else {
185 return (NULL);
186 }
187 } else {
188 ptr = kmalloc_node(size, lflags, node);
189 }
190
191 if (likely(ptr) || (flags & KM_NOSLEEP))
192 return (ptr);
193
194 /*
195 * For vmem_alloc() and vmem_zalloc() callers retry immediately
196 * using __vmalloc() which is unlikely to fail.
197 */
198 if ((flags & KM_VMEM) && (use_vmem == 0)) {
199 use_vmem = 1;
200 continue;
201 }
202
203 /*
204 * Use cond_resched() instead of congestion_wait() to avoid
205 * deadlocking systems where there are no block devices.
206 */
207 cond_resched();
208 } while (1);
209
210 return (NULL);
211 }
212
213 inline void
214 spl_kmem_free_impl(const void *buf, size_t size)
215 {
216 if (is_vmalloc_addr(buf))
217 vfree(buf);
218 else
219 kfree(buf);
220 }
221
222 /*
223 * Memory allocation and accounting for kmem_* * style allocations. When
224 * DEBUG_KMEM is enabled the total memory allocated will be tracked and
225 * any memory leaked will be reported during module unload.
226 *
227 * ./configure --enable-debug-kmem
228 */
229 #ifdef DEBUG_KMEM
230
231 /* Shim layer memory accounting */
232 #ifdef HAVE_ATOMIC64_T
233 atomic64_t kmem_alloc_used = ATOMIC64_INIT(0);
234 unsigned long long kmem_alloc_max = 0;
235 #else /* HAVE_ATOMIC64_T */
236 atomic_t kmem_alloc_used = ATOMIC_INIT(0);
237 unsigned long long kmem_alloc_max = 0;
238 #endif /* HAVE_ATOMIC64_T */
239
240 EXPORT_SYMBOL(kmem_alloc_used);
241 EXPORT_SYMBOL(kmem_alloc_max);
242
243 inline void *
244 spl_kmem_alloc_debug(size_t size, int flags, int node)
245 {
246 void *ptr;
247
248 ptr = spl_kmem_alloc_impl(size, flags, node);
249 if (ptr) {
250 kmem_alloc_used_add(size);
251 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max))
252 kmem_alloc_max = kmem_alloc_used_read();
253 }
254
255 return (ptr);
256 }
257
258 inline void
259 spl_kmem_free_debug(const void *ptr, size_t size)
260 {
261 kmem_alloc_used_sub(size);
262 spl_kmem_free_impl(ptr, size);
263 }
264
265 /*
266 * When DEBUG_KMEM_TRACKING is enabled not only will total bytes be tracked
267 * but also the location of every alloc and free. When the SPL module is
268 * unloaded a list of all leaked addresses and where they were allocated
269 * will be dumped to the console. Enabling this feature has a significant
270 * impact on performance but it makes finding memory leaks straight forward.
271 *
272 * Not surprisingly with debugging enabled the xmem_locks are very highly
273 * contended particularly on xfree(). If we want to run with this detailed
274 * debugging enabled for anything other than debugging we need to minimize
275 * the contention by moving to a lock per xmem_table entry model.
276 *
277 * ./configure --enable-debug-kmem-tracking
278 */
279 #ifdef DEBUG_KMEM_TRACKING
280
281 #include <linux/hash.h>
282 #include <linux/ctype.h>
283
284 #define KMEM_HASH_BITS 10
285 #define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
286
287 typedef struct kmem_debug {
288 struct hlist_node kd_hlist; /* Hash node linkage */
289 struct list_head kd_list; /* List of all allocations */
290 void *kd_addr; /* Allocation pointer */
291 size_t kd_size; /* Allocation size */
292 const char *kd_func; /* Allocation function */
293 int kd_line; /* Allocation line */
294 } kmem_debug_t;
295
296 static spinlock_t kmem_lock;
297 static struct hlist_head kmem_table[KMEM_TABLE_SIZE];
298 static struct list_head kmem_list;
299
300 static kmem_debug_t *
301 kmem_del_init(spinlock_t *lock, struct hlist_head *table,
302 int bits, const void *addr)
303 {
304 struct hlist_head *head;
305 struct hlist_node *node;
306 struct kmem_debug *p;
307 unsigned long flags;
308
309 spin_lock_irqsave(lock, flags);
310
311 head = &table[hash_ptr((void *)addr, bits)];
312 hlist_for_each(node, head) {
313 p = list_entry(node, struct kmem_debug, kd_hlist);
314 if (p->kd_addr == addr) {
315 hlist_del_init(&p->kd_hlist);
316 list_del_init(&p->kd_list);
317 spin_unlock_irqrestore(lock, flags);
318 return (p);
319 }
320 }
321
322 spin_unlock_irqrestore(lock, flags);
323
324 return (NULL);
325 }
326
327 inline void *
328 spl_kmem_alloc_track(size_t size, int flags,
329 const char *func, int line, int node)
330 {
331 void *ptr = NULL;
332 kmem_debug_t *dptr;
333 unsigned long irq_flags;
334
335 dptr = kmalloc(sizeof (kmem_debug_t), kmem_flags_convert(flags));
336 if (dptr == NULL)
337 return (NULL);
338
339 dptr->kd_func = __strdup(func, flags);
340 if (dptr->kd_func == NULL) {
341 kfree(dptr);
342 return (NULL);
343 }
344
345 ptr = spl_kmem_alloc_debug(size, flags, node);
346 if (ptr == NULL) {
347 kfree(dptr->kd_func);
348 kfree(dptr);
349 return (NULL);
350 }
351
352 INIT_HLIST_NODE(&dptr->kd_hlist);
353 INIT_LIST_HEAD(&dptr->kd_list);
354
355 dptr->kd_addr = ptr;
356 dptr->kd_size = size;
357 dptr->kd_line = line;
358
359 spin_lock_irqsave(&kmem_lock, irq_flags);
360 hlist_add_head(&dptr->kd_hlist,
361 &kmem_table[hash_ptr(ptr, KMEM_HASH_BITS)]);
362 list_add_tail(&dptr->kd_list, &kmem_list);
363 spin_unlock_irqrestore(&kmem_lock, irq_flags);
364
365 return (ptr);
366 }
367
368 inline void
369 spl_kmem_free_track(const void *ptr, size_t size)
370 {
371 kmem_debug_t *dptr;
372
373 /* Ignore NULL pointer since we haven't tracked it at all */
374 if (ptr == NULL)
375 return;
376
377 /* Must exist in hash due to kmem_alloc() */
378 dptr = kmem_del_init(&kmem_lock, kmem_table, KMEM_HASH_BITS, ptr);
379 ASSERT3P(dptr, !=, NULL);
380 ASSERT3S(dptr->kd_size, ==, size);
381
382 kfree(dptr->kd_func);
383 kfree(dptr);
384
385 spl_kmem_free_debug(ptr, size);
386 }
387 #endif /* DEBUG_KMEM_TRACKING */
388 #endif /* DEBUG_KMEM */
389
390 /*
391 * Public kmem_alloc(), kmem_zalloc() and kmem_free() interfaces.
392 */
393 void *
394 spl_kmem_alloc(size_t size, int flags, const char *func, int line)
395 {
396 ASSERT0(flags & ~KM_PUBLIC_MASK);
397
398 #if !defined(DEBUG_KMEM)
399 return (spl_kmem_alloc_impl(size, flags, NUMA_NO_NODE));
400 #elif !defined(DEBUG_KMEM_TRACKING)
401 return (spl_kmem_alloc_debug(size, flags, NUMA_NO_NODE));
402 #else
403 return (spl_kmem_alloc_track(size, flags, func, line, NUMA_NO_NODE));
404 #endif
405 }
406 EXPORT_SYMBOL(spl_kmem_alloc);
407
408 void *
409 spl_kmem_zalloc(size_t size, int flags, const char *func, int line)
410 {
411 ASSERT0(flags & ~KM_PUBLIC_MASK);
412
413 flags |= KM_ZERO;
414
415 #if !defined(DEBUG_KMEM)
416 return (spl_kmem_alloc_impl(size, flags, NUMA_NO_NODE));
417 #elif !defined(DEBUG_KMEM_TRACKING)
418 return (spl_kmem_alloc_debug(size, flags, NUMA_NO_NODE));
419 #else
420 return (spl_kmem_alloc_track(size, flags, func, line, NUMA_NO_NODE));
421 #endif
422 }
423 EXPORT_SYMBOL(spl_kmem_zalloc);
424
425 void
426 spl_kmem_free(const void *buf, size_t size)
427 {
428 #if !defined(DEBUG_KMEM)
429 return (spl_kmem_free_impl(buf, size));
430 #elif !defined(DEBUG_KMEM_TRACKING)
431 return (spl_kmem_free_debug(buf, size));
432 #else
433 return (spl_kmem_free_track(buf, size));
434 #endif
435 }
436 EXPORT_SYMBOL(spl_kmem_free);
437
438 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
439 static char *
440 spl_sprintf_addr(kmem_debug_t *kd, char *str, int len, int min)
441 {
442 int size = ((len - 1) < kd->kd_size) ? (len - 1) : kd->kd_size;
443 int i, flag = 1;
444
445 ASSERT(str != NULL && len >= 17);
446 memset(str, 0, len);
447
448 /*
449 * Check for a fully printable string, and while we are at
450 * it place the printable characters in the passed buffer.
451 */
452 for (i = 0; i < size; i++) {
453 str[i] = ((char *)(kd->kd_addr))[i];
454 if (isprint(str[i])) {
455 continue;
456 } else {
457 /*
458 * Minimum number of printable characters found
459 * to make it worthwhile to print this as ascii.
460 */
461 if (i > min)
462 break;
463
464 flag = 0;
465 break;
466 }
467 }
468
469 if (!flag) {
470 sprintf(str, "%02x%02x%02x%02x%02x%02x%02x%02x",
471 *((uint8_t *)kd->kd_addr),
472 *((uint8_t *)kd->kd_addr + 2),
473 *((uint8_t *)kd->kd_addr + 4),
474 *((uint8_t *)kd->kd_addr + 6),
475 *((uint8_t *)kd->kd_addr + 8),
476 *((uint8_t *)kd->kd_addr + 10),
477 *((uint8_t *)kd->kd_addr + 12),
478 *((uint8_t *)kd->kd_addr + 14));
479 }
480
481 return (str);
482 }
483
484 static int
485 spl_kmem_init_tracking(struct list_head *list, spinlock_t *lock, int size)
486 {
487 int i;
488
489 spin_lock_init(lock);
490 INIT_LIST_HEAD(list);
491
492 for (i = 0; i < size; i++)
493 INIT_HLIST_HEAD(&kmem_table[i]);
494
495 return (0);
496 }
497
498 static void
499 spl_kmem_fini_tracking(struct list_head *list, spinlock_t *lock)
500 {
501 unsigned long flags;
502 kmem_debug_t *kd;
503 char str[17];
504
505 spin_lock_irqsave(lock, flags);
506 if (!list_empty(list))
507 printk(KERN_WARNING "%-16s %-5s %-16s %s:%s\n", "address",
508 "size", "data", "func", "line");
509
510 list_for_each_entry(kd, list, kd_list) {
511 printk(KERN_WARNING "%p %-5d %-16s %s:%d\n", kd->kd_addr,
512 (int)kd->kd_size, spl_sprintf_addr(kd, str, 17, 8),
513 kd->kd_func, kd->kd_line);
514 }
515
516 spin_unlock_irqrestore(lock, flags);
517 }
518 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
519
520 int
521 spl_kmem_init(void)
522 {
523
524 #ifdef DEBUG_KMEM
525 kmem_alloc_used_set(0);
526
527
528
529 #ifdef DEBUG_KMEM_TRACKING
530 spl_kmem_init_tracking(&kmem_list, &kmem_lock, KMEM_TABLE_SIZE);
531 #endif /* DEBUG_KMEM_TRACKING */
532 #endif /* DEBUG_KMEM */
533
534 return (0);
535 }
536
537 void
538 spl_kmem_fini(void)
539 {
540 #ifdef DEBUG_KMEM
541 /*
542 * Display all unreclaimed memory addresses, including the
543 * allocation size and the first few bytes of what's located
544 * at that address to aid in debugging. Performance is not
545 * a serious concern here since it is module unload time.
546 */
547 if (kmem_alloc_used_read() != 0)
548 printk(KERN_WARNING "kmem leaked %ld/%llu bytes\n",
549 (unsigned long)kmem_alloc_used_read(), kmem_alloc_max);
550
551 #ifdef DEBUG_KMEM_TRACKING
552 spl_kmem_fini_tracking(&kmem_list, &kmem_lock);
553 #endif /* DEBUG_KMEM_TRACKING */
554 #endif /* DEBUG_KMEM */
555 }