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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
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
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/>.
25 #include <sys/debug.h>
26 #include <sys/sysmacros.h>
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.
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.
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
);
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.
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
);
74 EXPORT_SYMBOL(kmem_debugging
);
77 kmem_vasprintf(const char *fmt
, va_list ap
)
84 ptr
= kvasprintf(kmem_flags_convert(KM_SLEEP
), fmt
, aq
);
86 } while (ptr
== NULL
);
90 EXPORT_SYMBOL(kmem_vasprintf
);
93 kmem_asprintf(const char *fmt
, ...)
100 ptr
= kvasprintf(kmem_flags_convert(KM_SLEEP
), fmt
, ap
);
102 } while (ptr
== NULL
);
106 EXPORT_SYMBOL(kmem_asprintf
);
109 __strdup(const char *str
, int flags
)
115 ptr
= kmalloc(n
+ 1, kmem_flags_convert(flags
));
117 memcpy(ptr
, str
, n
+ 1);
123 strdup(const char *str
)
125 return (__strdup(str
, KM_SLEEP
));
127 EXPORT_SYMBOL(strdup
);
134 EXPORT_SYMBOL(strfree
);
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.
144 spl_kmem_alloc_impl(size_t size
, int flags
, int node
)
146 gfp_t lflags
= kmem_flags_convert(flags
);
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.
155 if ((spl_kmem_alloc_warn
> 0) && (size
> spl_kmem_alloc_warn
) &&
156 !(flags
& KM_VMEM
)) {
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
);
165 * Use a loop because kmalloc_node() can fail when GFP_KERNEL is used
166 * unlike kmem_alloc() with KM_SLEEP on Illumos.
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.
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.
181 if ((size
> spl_kmem_alloc_max
) || use_vmem
) {
182 if (flags
& KM_VMEM
) {
183 ptr
= __vmalloc(size
, lflags
, PAGE_KERNEL
);
188 ptr
= kmalloc_node(size
, lflags
, node
);
191 if (likely(ptr
) || (flags
& KM_NOSLEEP
))
195 * For vmem_alloc() and vmem_zalloc() callers retry immediately
196 * using __vmalloc() which is unlikely to fail.
198 if ((flags
& KM_VMEM
) && (use_vmem
== 0)) {
204 * Use cond_resched() instead of congestion_wait() to avoid
205 * deadlocking systems where there are no block devices.
214 spl_kmem_free_impl(const void *buf
, size_t size
)
216 if (is_vmalloc_addr(buf
))
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.
227 * ./configure --enable-debug-kmem
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 */
240 EXPORT_SYMBOL(kmem_alloc_used
);
241 EXPORT_SYMBOL(kmem_alloc_max
);
244 spl_kmem_alloc_debug(size_t size
, int flags
, int node
)
248 ptr
= spl_kmem_alloc_impl(size
, flags
, node
);
250 kmem_alloc_used_add(size
);
251 if (unlikely(kmem_alloc_used_read() > kmem_alloc_max
))
252 kmem_alloc_max
= kmem_alloc_used_read();
259 spl_kmem_free_debug(const void *ptr
, size_t size
)
261 kmem_alloc_used_sub(size
);
262 spl_kmem_free_impl(ptr
, size
);
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.
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.
277 * ./configure --enable-debug-kmem-tracking
279 #ifdef DEBUG_KMEM_TRACKING
281 #include <linux/hash.h>
282 #include <linux/ctype.h>
284 #define KMEM_HASH_BITS 10
285 #define KMEM_TABLE_SIZE (1 << KMEM_HASH_BITS)
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 */
296 static spinlock_t kmem_lock
;
297 static struct hlist_head kmem_table
[KMEM_TABLE_SIZE
];
298 static struct list_head kmem_list
;
300 static kmem_debug_t
*
301 kmem_del_init(spinlock_t
*lock
, struct hlist_head
*table
,
302 int bits
, const void *addr
)
304 struct hlist_head
*head
;
305 struct hlist_node
*node
;
306 struct kmem_debug
*p
;
309 spin_lock_irqsave(lock
, flags
);
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
);
322 spin_unlock_irqrestore(lock
, flags
);
328 spl_kmem_alloc_track(size_t size
, int flags
,
329 const char *func
, int line
, int node
)
333 unsigned long irq_flags
;
335 dptr
= kmalloc(sizeof (kmem_debug_t
), kmem_flags_convert(flags
));
339 dptr
->kd_func
= __strdup(func
, flags
);
340 if (dptr
->kd_func
== NULL
) {
345 ptr
= spl_kmem_alloc_debug(size
, flags
, node
);
347 kfree(dptr
->kd_func
);
352 INIT_HLIST_NODE(&dptr
->kd_hlist
);
353 INIT_LIST_HEAD(&dptr
->kd_list
);
356 dptr
->kd_size
= size
;
357 dptr
->kd_line
= line
;
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
);
369 spl_kmem_free_track(const void *ptr
, size_t size
)
373 /* Ignore NULL pointer since we haven't tracked it at all */
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
);
382 kfree(dptr
->kd_func
);
385 spl_kmem_free_debug(ptr
, size
);
387 #endif /* DEBUG_KMEM_TRACKING */
388 #endif /* DEBUG_KMEM */
391 * Public kmem_alloc(), kmem_zalloc() and kmem_free() interfaces.
394 spl_kmem_alloc(size_t size
, int flags
, const char *func
, int line
)
396 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
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
));
403 return (spl_kmem_alloc_track(size
, flags
, func
, line
, NUMA_NO_NODE
));
406 EXPORT_SYMBOL(spl_kmem_alloc
);
409 spl_kmem_zalloc(size_t size
, int flags
, const char *func
, int line
)
411 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
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
));
420 return (spl_kmem_alloc_track(size
, flags
, func
, line
, NUMA_NO_NODE
));
423 EXPORT_SYMBOL(spl_kmem_zalloc
);
426 spl_kmem_free(const void *buf
, size_t size
)
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
));
433 return (spl_kmem_free_track(buf
, size
));
436 EXPORT_SYMBOL(spl_kmem_free
);
438 #if defined(DEBUG_KMEM) && defined(DEBUG_KMEM_TRACKING)
440 spl_sprintf_addr(kmem_debug_t
*kd
, char *str
, int len
, int min
)
442 int size
= ((len
- 1) < kd
->kd_size
) ? (len
- 1) : kd
->kd_size
;
445 ASSERT(str
!= NULL
&& len
>= 17);
449 * Check for a fully printable string, and while we are at
450 * it place the printable characters in the passed buffer.
452 for (i
= 0; i
< size
; i
++) {
453 str
[i
] = ((char *)(kd
->kd_addr
))[i
];
454 if (isprint(str
[i
])) {
458 * Minimum number of printable characters found
459 * to make it worthwhile to print this as ascii.
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));
485 spl_kmem_init_tracking(struct list_head
*list
, spinlock_t
*lock
, int size
)
489 spin_lock_init(lock
);
490 INIT_LIST_HEAD(list
);
492 for (i
= 0; i
< size
; i
++)
493 INIT_HLIST_HEAD(&kmem_table
[i
]);
499 spl_kmem_fini_tracking(struct list_head
*list
, spinlock_t
*lock
)
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");
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
);
516 spin_unlock_irqrestore(lock
, flags
);
518 #endif /* DEBUG_KMEM && DEBUG_KMEM_TRACKING */
525 kmem_alloc_used_set(0);
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 */
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.
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
);
551 #ifdef DEBUG_KMEM_TRACKING
552 spl_kmem_fini_tracking(&kmem_list
, &kmem_lock
);
553 #endif /* DEBUG_KMEM_TRACKING */
554 #endif /* DEBUG_KMEM */