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