]> git.proxmox.com Git - mirror_spl.git/blob - include/sys/kmem.h
Fix kmem cstyle issues
[mirror_spl.git] / include / sys / kmem.h
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 #ifndef _SPL_KMEM_H
26 #define _SPL_KMEM_H
27
28 #include <linux/slab.h>
29
30 extern int kmem_debugging(void);
31 extern char *kmem_vasprintf(const char *fmt, va_list ap);
32 extern char *kmem_asprintf(const char *fmt, ...);
33 extern char *strdup(const char *str);
34 extern void strfree(char *str);
35
36 /*
37 * Memory allocation interfaces
38 */
39 #define KM_SLEEP GFP_KERNEL /* Can sleep, never fails */
40 #define KM_NOSLEEP GFP_ATOMIC /* Can not sleep, may fail */
41 #define KM_PUSHPAGE (GFP_NOIO | __GFP_HIGH) /* Use reserved memory */
42 #define KM_NODEBUG __GFP_NOWARN /* Suppress warnings */
43 #define KM_FLAGS __GFP_BITS_MASK
44 #define KM_VMFLAGS GFP_LEVEL_MASK
45
46 /*
47 * Used internally, the kernel does not need to support this flag
48 */
49 #ifndef __GFP_ZERO
50 #define __GFP_ZERO 0x8000
51 #endif
52
53 /*
54 * __GFP_NOFAIL looks like it will be removed from the kernel perhaps as
55 * early as 2.6.32. To avoid this issue when it occurs in upstream kernels
56 * we retry the allocation here as long as it is not __GFP_WAIT (GFP_ATOMIC).
57 * I would prefer the caller handle the failure case cleanly but we are
58 * trying to emulate Solaris and those are not the Solaris semantics.
59 */
60 static inline void *
61 kmalloc_nofail(size_t size, gfp_t flags)
62 {
63 void *ptr;
64
65 do {
66 ptr = kmalloc(size, flags);
67 } while (ptr == NULL && (flags & __GFP_WAIT));
68
69 return (ptr);
70 }
71
72 static inline void *
73 kzalloc_nofail(size_t size, gfp_t flags)
74 {
75 void *ptr;
76
77 do {
78 ptr = kzalloc(size, flags);
79 } while (ptr == NULL && (flags & __GFP_WAIT));
80
81 return (ptr);
82 }
83
84 static inline void *
85 kmalloc_node_nofail(size_t size, gfp_t flags, int node)
86 {
87 void *ptr;
88
89 do {
90 ptr = kmalloc_node(size, flags, node);
91 } while (ptr == NULL && (flags & __GFP_WAIT));
92
93 return (ptr);
94 }
95
96 #ifdef DEBUG_KMEM
97
98 /*
99 * Memory accounting functions to be used only when DEBUG_KMEM is set.
100 */
101 #ifdef HAVE_ATOMIC64_T
102 #define kmem_alloc_used_add(size) atomic64_add(size, &kmem_alloc_used)
103 #define kmem_alloc_used_sub(size) atomic64_sub(size, &kmem_alloc_used)
104 #define kmem_alloc_used_read() atomic64_read(&kmem_alloc_used)
105 #define kmem_alloc_used_set(size) atomic64_set(&kmem_alloc_used, size)
106 extern atomic64_t kmem_alloc_used;
107 extern unsigned long long kmem_alloc_max;
108 #else /* HAVE_ATOMIC64_T */
109 #define kmem_alloc_used_add(size) atomic_add(size, &kmem_alloc_used)
110 #define kmem_alloc_used_sub(size) atomic_sub(size, &kmem_alloc_used)
111 #define kmem_alloc_used_read() atomic_read(&kmem_alloc_used)
112 #define kmem_alloc_used_set(size) atomic_set(&kmem_alloc_used, size)
113 extern atomic_t kmem_alloc_used;
114 extern unsigned long long kmem_alloc_max;
115 #endif /* HAVE_ATOMIC64_T */
116
117 #ifdef DEBUG_KMEM_TRACKING
118 /*
119 * DEBUG_KMEM && DEBUG_KMEM_TRACKING
120 *
121 * The maximum level of memory debugging. All memory will be accounted
122 * for and each allocation will be explicitly tracked. Any allocation
123 * which is leaked will be reported on module unload and the exact location
124 * where that memory was allocation will be reported. This level of memory
125 * tracking will have a significant impact on performance and should only
126 * be enabled for debugging. This feature may be enabled by passing
127 * --enable-debug-kmem-tracking to configure.
128 */
129 #define kmem_alloc(sz, fl) kmem_alloc_track((sz), (fl), \
130 __FUNCTION__, __LINE__, 0, 0)
131 #define kmem_zalloc(sz, fl) kmem_alloc_track((sz), (fl)|__GFP_ZERO,\
132 __FUNCTION__, __LINE__, 0, 0)
133 #define kmem_alloc_node(sz, fl, nd) kmem_alloc_track((sz), (fl), \
134 __FUNCTION__, __LINE__, 1, nd)
135 #define kmem_free(ptr, sz) kmem_free_track((ptr), (sz))
136
137 extern void *kmem_alloc_track(size_t, int, const char *, int, int, int);
138 extern void kmem_free_track(const void *, size_t);
139
140 #else /* DEBUG_KMEM_TRACKING */
141 /*
142 * DEBUG_KMEM && !DEBUG_KMEM_TRACKING
143 *
144 * The default build will set DEBUG_KEM. This provides basic memory
145 * accounting with little to no impact on performance. When the module
146 * is unloaded in any memory was leaked the total number of leaked bytes
147 * will be reported on the console. To disable this basic accounting
148 * pass the --disable-debug-kmem option to configure.
149 */
150 #define kmem_alloc(sz, fl) kmem_alloc_debug((sz), (fl), \
151 __FUNCTION__, __LINE__, 0, 0)
152 #define kmem_zalloc(sz, fl) kmem_alloc_debug((sz), (fl)|__GFP_ZERO,\
153 __FUNCTION__, __LINE__, 0, 0)
154 #define kmem_alloc_node(sz, fl, nd) kmem_alloc_debug((sz), (fl), \
155 __FUNCTION__, __LINE__, 1, nd)
156 #define kmem_free(ptr, sz) kmem_free_debug((ptr), (sz))
157
158 extern void *kmem_alloc_debug(size_t, int, const char *, int, int, int);
159 extern void kmem_free_debug(const void *, size_t);
160
161 #endif /* DEBUG_KMEM_TRACKING */
162 #else /* DEBUG_KMEM */
163 /*
164 * !DEBUG_KMEM && !DEBUG_KMEM_TRACKING
165 *
166 * All debugging is disabled. There will be no overhead even for
167 * minimal memory accounting. To enable basic accounting pass the
168 * --enable-debug-kmem option to configure.
169 */
170 #define kmem_alloc(sz, fl) kmalloc_nofail((sz), (fl))
171 #define kmem_zalloc(sz, fl) kzalloc_nofail((sz), (fl))
172 #define kmem_alloc_node(sz, fl, nd) kmalloc_node_nofail((sz), (fl), (nd))
173 #define kmem_free(ptr, sz) ((void)(sz), kfree(ptr))
174
175 #endif /* DEBUG_KMEM */
176
177 int spl_kmem_init(void);
178 void spl_kmem_fini(void);
179
180 #define kmem_virt(ptr) (((ptr) >= (void *)VMALLOC_START) && \
181 ((ptr) < (void *)VMALLOC_END))
182
183 #endif /* _SPL_KMEM_H */