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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
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/>.
23 \*****************************************************************************/
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include <linux/mm_compat.h>
32 #include <linux/spinlock.h>
33 #include <linux/rwsem.h>
34 #include <linux/hash.h>
35 #include <linux/ctype.h>
36 #include <asm/atomic.h>
37 #include <sys/types.h>
38 #include <sys/vmsystm.h>
39 #include <sys/kstat.h>
42 * Memory allocation interfaces
44 #define KM_SLEEP GFP_KERNEL /* Can sleep, never fails */
45 #define KM_NOSLEEP GFP_ATOMIC /* Can not sleep, may fail */
46 #define KM_PUSHPAGE (GFP_NOFS | __GFP_HIGH) /* Use reserved memory */
47 #define KM_NODEBUG __GFP_NOWARN /* Suppress warnings */
48 #define KM_FLAGS __GFP_BITS_MASK
49 #define KM_VMFLAGS GFP_LEVEL_MASK
52 * Used internally, the kernel does not need to support this flag
55 # define __GFP_ZERO 0x8000
59 * __GFP_NOFAIL looks like it will be removed from the kernel perhaps as
60 * early as 2.6.32. To avoid this issue when it occurs in upstream kernels
61 * we retry the allocation here as long as it is not __GFP_WAIT (GFP_ATOMIC).
62 * I would prefer the caller handle the failure case cleanly but we are
63 * trying to emulate Solaris and those are not the Solaris semantics.
66 kmalloc_nofail(size_t size
, gfp_t flags
)
71 ptr
= kmalloc(size
, flags
);
72 } while (ptr
== NULL
&& (flags
& __GFP_WAIT
));
78 kzalloc_nofail(size_t size
, gfp_t flags
)
83 ptr
= kzalloc(size
, flags
);
84 } while (ptr
== NULL
&& (flags
& __GFP_WAIT
));
90 kmalloc_node_nofail(size_t size
, gfp_t flags
, int node
)
92 #ifdef HAVE_KMALLOC_NODE
96 ptr
= kmalloc_node(size
, flags
, node
);
97 } while (ptr
== NULL
&& (flags
& __GFP_WAIT
));
101 return kmalloc_nofail(size
, flags
);
102 #endif /* HAVE_KMALLOC_NODE */
106 vmalloc_nofail(size_t size
, gfp_t flags
)
111 * Retry failed __vmalloc() allocations once every second. The
112 * rational for the delay is that the likely failure modes are:
114 * 1) The system has completely exhausted memory, in which case
115 * delaying 1 second for the memory reclaim to run is reasonable
116 * to avoid thrashing the system.
117 * 2) The system has memory but has exhausted the small virtual
118 * address space available on 32-bit systems. Retrying the
119 * allocation immediately will only result in spinning on the
120 * virtual address space lock. It is better delay a second and
121 * hope that another process will free some of the address space.
122 * But the bottom line is there is not much we can actually do
123 * since we can never safely return a failure and honor the
127 ptr
= __vmalloc(size
, flags
| __GFP_HIGHMEM
, PAGE_KERNEL
);
128 if (unlikely((ptr
== NULL
) && (flags
& __GFP_WAIT
))) {
129 set_current_state(TASK_INTERRUPTIBLE
);
130 schedule_timeout(HZ
);
140 vzalloc_nofail(size_t size
, gfp_t flags
)
144 ptr
= vmalloc_nofail(size
, flags
);
146 memset(ptr
, 0, (size
));
154 * Memory accounting functions to be used only when DEBUG_KMEM is set.
156 # ifdef HAVE_ATOMIC64_T
158 # define kmem_alloc_used_add(size) atomic64_add(size, &kmem_alloc_used)
159 # define kmem_alloc_used_sub(size) atomic64_sub(size, &kmem_alloc_used)
160 # define kmem_alloc_used_read() atomic64_read(&kmem_alloc_used)
161 # define kmem_alloc_used_set(size) atomic64_set(&kmem_alloc_used, size)
162 # define vmem_alloc_used_add(size) atomic64_add(size, &vmem_alloc_used)
163 # define vmem_alloc_used_sub(size) atomic64_sub(size, &vmem_alloc_used)
164 # define vmem_alloc_used_read() atomic64_read(&vmem_alloc_used)
165 # define vmem_alloc_used_set(size) atomic64_set(&vmem_alloc_used, size)
167 extern atomic64_t kmem_alloc_used
;
168 extern unsigned long long kmem_alloc_max
;
169 extern atomic64_t vmem_alloc_used
;
170 extern unsigned long long vmem_alloc_max
;
172 # else /* HAVE_ATOMIC64_T */
174 # define kmem_alloc_used_add(size) atomic_add(size, &kmem_alloc_used)
175 # define kmem_alloc_used_sub(size) atomic_sub(size, &kmem_alloc_used)
176 # define kmem_alloc_used_read() atomic_read(&kmem_alloc_used)
177 # define kmem_alloc_used_set(size) atomic_set(&kmem_alloc_used, size)
178 # define vmem_alloc_used_add(size) atomic_add(size, &vmem_alloc_used)
179 # define vmem_alloc_used_sub(size) atomic_sub(size, &vmem_alloc_used)
180 # define vmem_alloc_used_read() atomic_read(&vmem_alloc_used)
181 # define vmem_alloc_used_set(size) atomic_set(&vmem_alloc_used, size)
183 extern atomic_t kmem_alloc_used
;
184 extern unsigned long long kmem_alloc_max
;
185 extern atomic_t vmem_alloc_used
;
186 extern unsigned long long vmem_alloc_max
;
188 # endif /* HAVE_ATOMIC64_T */
190 # ifdef DEBUG_KMEM_TRACKING
192 * DEBUG_KMEM && DEBUG_KMEM_TRACKING
194 * The maximum level of memory debugging. All memory will be accounted
195 * for and each allocation will be explicitly tracked. Any allocation
196 * which is leaked will be reported on module unload and the exact location
197 * where that memory was allocation will be reported. This level of memory
198 * tracking will have a significant impact on performance and should only
199 * be enabled for debugging. This feature may be enabled by passing
200 * --enable-debug-kmem-tracking to configure.
202 # define kmem_alloc(sz, fl) kmem_alloc_track((sz), (fl), \
203 __FUNCTION__, __LINE__, 0, 0)
204 # define kmem_zalloc(sz, fl) kmem_alloc_track((sz), (fl)|__GFP_ZERO,\
205 __FUNCTION__, __LINE__, 0, 0)
206 # define kmem_alloc_node(sz, fl, nd) kmem_alloc_track((sz), (fl), \
207 __FUNCTION__, __LINE__, 1, nd)
208 # define kmem_free(ptr, sz) kmem_free_track((ptr), (sz))
210 # define vmem_alloc(sz, fl) vmem_alloc_track((sz), (fl), \
211 __FUNCTION__, __LINE__)
212 # define vmem_zalloc(sz, fl) vmem_alloc_track((sz), (fl)|__GFP_ZERO,\
213 __FUNCTION__, __LINE__)
214 # define vmem_free(ptr, sz) vmem_free_track((ptr), (sz))
216 extern void *kmem_alloc_track(size_t, int, const char *, int, int, int);
217 extern void kmem_free_track(void *, size_t);
218 extern void *vmem_alloc_track(size_t, int, const char *, int);
219 extern void vmem_free_track(void *, size_t);
221 # else /* DEBUG_KMEM_TRACKING */
223 * DEBUG_KMEM && !DEBUG_KMEM_TRACKING
225 * The default build will set DEBUG_KEM. This provides basic memory
226 * accounting with little to no impact on performance. When the module
227 * is unloaded in any memory was leaked the total number of leaked bytes
228 * will be reported on the console. To disable this basic accounting
229 * pass the --disable-debug-kmem option to configure.
231 # define kmem_alloc(sz, fl) kmem_alloc_debug((sz), (fl), \
232 __FUNCTION__, __LINE__, 0, 0)
233 # define kmem_zalloc(sz, fl) kmem_alloc_debug((sz), (fl)|__GFP_ZERO,\
234 __FUNCTION__, __LINE__, 0, 0)
235 # define kmem_alloc_node(sz, fl, nd) kmem_alloc_debug((sz), (fl), \
236 __FUNCTION__, __LINE__, 1, nd)
237 # define kmem_free(ptr, sz) kmem_free_debug((ptr), (sz))
239 # define vmem_alloc(sz, fl) vmem_alloc_debug((sz), (fl), \
240 __FUNCTION__, __LINE__)
241 # define vmem_zalloc(sz, fl) vmem_alloc_debug((sz), (fl)|__GFP_ZERO,\
242 __FUNCTION__, __LINE__)
243 # define vmem_free(ptr, sz) vmem_free_debug((ptr), (sz))
245 extern void *kmem_alloc_debug(size_t, int, const char *, int, int, int);
246 extern void kmem_free_debug(void *, size_t);
247 extern void *vmem_alloc_debug(size_t, int, const char *, int);
248 extern void vmem_free_debug(void *, size_t);
250 # endif /* DEBUG_KMEM_TRACKING */
251 #else /* DEBUG_KMEM */
253 * !DEBUG_KMEM && !DEBUG_KMEM_TRACKING
255 * All debugging is disabled. There will be no overhead even for
256 * minimal memory accounting. To enable basic accounting pass the
257 * --enable-debug-kmem option to configure.
259 # define kmem_alloc(sz, fl) kmalloc_nofail((sz), (fl))
260 # define kmem_zalloc(sz, fl) kzalloc_nofail((sz), (fl))
261 # define kmem_alloc_node(sz, fl, nd) kmalloc_node_nofail((sz), (fl), (nd))
262 # define kmem_free(ptr, sz) ((void)(sz), kfree(ptr))
264 # define vmem_alloc(sz, fl) vmalloc_nofail((sz), (fl))
265 # define vmem_zalloc(sz, fl) vzalloc_nofail((sz), (fl))
266 # define vmem_free(ptr, sz) ((void)(sz), vfree(ptr))
268 #endif /* DEBUG_KMEM */
270 extern int kmem_debugging(void);
271 extern char *kmem_vasprintf(const char *fmt
, va_list ap
);
272 extern char *kmem_asprintf(const char *fmt
, ...);
273 extern char *strdup(const char *str
);
274 extern void strfree(char *str
);
278 * Slab allocation interfaces. The SPL slab differs from the standard
279 * Linux SLAB or SLUB primarily in that each cache may be backed by slabs
280 * allocated from the physical or virtal memory address space. The virtual
281 * slabs allow for good behavior when allocation large objects of identical
282 * size. This slab implementation also supports both constructors and
283 * destructions which the Linux slab does not.
286 KMC_BIT_NOTOUCH
= 0, /* Don't update ages */
287 KMC_BIT_NODEBUG
= 1, /* Default behavior */
288 KMC_BIT_NOMAGAZINE
= 2, /* XXX: Unsupported */
289 KMC_BIT_NOHASH
= 3, /* XXX: Unsupported */
290 KMC_BIT_QCACHE
= 4, /* XXX: Unsupported */
291 KMC_BIT_KMEM
= 5, /* Use kmem cache */
292 KMC_BIT_VMEM
= 6, /* Use vmem cache */
293 KMC_BIT_OFFSLAB
= 7, /* Objects not on slab */
294 KMC_BIT_REAPING
= 16, /* Reaping in progress */
295 KMC_BIT_DESTROY
= 17, /* Destroy in progress */
296 KMC_BIT_TOTAL
= 18, /* Proc handler helper bit */
297 KMC_BIT_ALLOC
= 19, /* Proc handler helper bit */
298 KMC_BIT_MAX
= 20, /* Proc handler helper bit */
301 /* kmem move callback return values */
302 typedef enum kmem_cbrc
{
303 KMEM_CBRC_YES
= 0, /* Object moved */
304 KMEM_CBRC_NO
= 1, /* Object not moved */
305 KMEM_CBRC_LATER
= 2, /* Object not moved, try again later */
306 KMEM_CBRC_DONT_NEED
= 3, /* Neither object is needed */
307 KMEM_CBRC_DONT_KNOW
= 4, /* Object unknown */
310 #define KMC_NOTOUCH (1 << KMC_BIT_NOTOUCH)
311 #define KMC_NODEBUG (1 << KMC_BIT_NODEBUG)
312 #define KMC_NOMAGAZINE (1 << KMC_BIT_NOMAGAZINE)
313 #define KMC_NOHASH (1 << KMC_BIT_NOHASH)
314 #define KMC_QCACHE (1 << KMC_BIT_QCACHE)
315 #define KMC_KMEM (1 << KMC_BIT_KMEM)
316 #define KMC_VMEM (1 << KMC_BIT_VMEM)
317 #define KMC_OFFSLAB (1 << KMC_BIT_OFFSLAB)
318 #define KMC_REAPING (1 << KMC_BIT_REAPING)
319 #define KMC_DESTROY (1 << KMC_BIT_DESTROY)
320 #define KMC_TOTAL (1 << KMC_BIT_TOTAL)
321 #define KMC_ALLOC (1 << KMC_BIT_ALLOC)
322 #define KMC_MAX (1 << KMC_BIT_MAX)
324 #define KMC_REAP_CHUNK INT_MAX
325 #define KMC_DEFAULT_SEEKS 1
327 extern struct list_head spl_kmem_cache_list
;
328 extern struct rw_semaphore spl_kmem_cache_sem
;
330 #define SKM_MAGIC 0x2e2e2e2e
331 #define SKO_MAGIC 0x20202020
332 #define SKS_MAGIC 0x22222222
333 #define SKC_MAGIC 0x2c2c2c2c
335 #define SPL_KMEM_CACHE_DELAY 15 /* Minimum slab release age */
336 #define SPL_KMEM_CACHE_REAP 0 /* Default reap everything */
337 #define SPL_KMEM_CACHE_OBJ_PER_SLAB 16 /* Target objects per slab */
338 #define SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN 8 /* Minimum objects per slab */
339 #define SPL_KMEM_CACHE_ALIGN 8 /* Default object alignment */
341 #define POINTER_IS_VALID(p) 0 /* Unimplemented */
342 #define POINTER_INVALIDATE(pp) /* Unimplemented */
344 typedef int (*spl_kmem_ctor_t
)(void *, void *, int);
345 typedef void (*spl_kmem_dtor_t
)(void *, void *);
346 typedef void (*spl_kmem_reclaim_t
)(void *);
348 typedef struct spl_kmem_magazine
{
349 uint32_t skm_magic
; /* Sanity magic */
350 uint32_t skm_avail
; /* Available objects */
351 uint32_t skm_size
; /* Magazine size */
352 uint32_t skm_refill
; /* Batch refill size */
353 struct spl_kmem_cache
*skm_cache
; /* Owned by cache */
354 struct delayed_work skm_work
; /* Magazine reclaim work */
355 unsigned long skm_age
; /* Last cache access */
356 void *skm_objs
[0]; /* Object pointers */
357 } spl_kmem_magazine_t
;
359 typedef struct spl_kmem_obj
{
360 uint32_t sko_magic
; /* Sanity magic */
361 void *sko_addr
; /* Buffer address */
362 struct spl_kmem_slab
*sko_slab
; /* Owned by slab */
363 struct list_head sko_list
; /* Free object list linkage */
366 typedef struct spl_kmem_slab
{
367 uint32_t sks_magic
; /* Sanity magic */
368 uint32_t sks_objs
; /* Objects per slab */
369 struct spl_kmem_cache
*sks_cache
; /* Owned by cache */
370 struct list_head sks_list
; /* Slab list linkage */
371 struct list_head sks_free_list
; /* Free object list */
372 unsigned long sks_age
; /* Last modify jiffie */
373 uint32_t sks_ref
; /* Ref count used objects */
376 typedef struct spl_kmem_cache
{
377 uint32_t skc_magic
; /* Sanity magic */
378 uint32_t skc_name_size
; /* Name length */
379 char *skc_name
; /* Name string */
380 spl_kmem_magazine_t
*skc_mag
[NR_CPUS
]; /* Per-CPU warm cache */
381 uint32_t skc_mag_size
; /* Magazine size */
382 uint32_t skc_mag_refill
; /* Magazine refill count */
383 spl_kmem_ctor_t skc_ctor
; /* Constructor */
384 spl_kmem_dtor_t skc_dtor
; /* Destructor */
385 spl_kmem_reclaim_t skc_reclaim
; /* Reclaimator */
386 void *skc_private
; /* Private data */
387 void *skc_vmp
; /* Unused */
388 unsigned long skc_flags
; /* Flags */
389 uint32_t skc_obj_size
; /* Object size */
390 uint32_t skc_obj_align
; /* Object alignment */
391 uint32_t skc_slab_objs
; /* Objects per slab */
392 uint32_t skc_slab_size
; /* Slab size */
393 uint32_t skc_delay
; /* Slab reclaim interval */
394 uint32_t skc_reap
; /* Slab reclaim count */
395 atomic_t skc_ref
; /* Ref count callers */
396 struct delayed_work skc_work
; /* Slab reclaim work */
397 struct list_head skc_list
; /* List of caches linkage */
398 struct list_head skc_complete_list
;/* Completely alloc'ed */
399 struct list_head skc_partial_list
; /* Partially alloc'ed */
400 spinlock_t skc_lock
; /* Cache lock */
401 uint64_t skc_slab_fail
; /* Slab alloc failures */
402 uint64_t skc_slab_create
;/* Slab creates */
403 uint64_t skc_slab_destroy
;/* Slab destroys */
404 uint64_t skc_slab_total
; /* Slab total current */
405 uint64_t skc_slab_alloc
; /* Slab alloc current */
406 uint64_t skc_slab_max
; /* Slab max historic */
407 uint64_t skc_obj_total
; /* Obj total current */
408 uint64_t skc_obj_alloc
; /* Obj alloc current */
409 uint64_t skc_obj_max
; /* Obj max historic */
411 #define kmem_cache_t spl_kmem_cache_t
413 extern spl_kmem_cache_t
*spl_kmem_cache_create(char *name
, size_t size
,
414 size_t align
, spl_kmem_ctor_t ctor
, spl_kmem_dtor_t dtor
,
415 spl_kmem_reclaim_t reclaim
, void *priv
, void *vmp
, int flags
);
416 extern void spl_kmem_cache_set_move(kmem_cache_t
*,
417 kmem_cbrc_t (*)(void *, void *, size_t, void *));
418 extern void spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
);
419 extern void *spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
);
420 extern void spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
);
421 extern void spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
, int count
);
422 extern void spl_kmem_reap(void);
424 int spl_kmem_init_kallsyms_lookup(void);
425 int spl_kmem_init(void);
426 void spl_kmem_fini(void);
428 #define kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags) \
429 spl_kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags)
430 #define kmem_cache_set_move(skc, move) spl_kmem_cache_set_move(skc, move)
431 #define kmem_cache_destroy(skc) spl_kmem_cache_destroy(skc)
432 #define kmem_cache_alloc(skc, flags) spl_kmem_cache_alloc(skc, flags)
433 #define kmem_cache_free(skc, obj) spl_kmem_cache_free(skc, obj)
434 #define kmem_cache_reap_now(skc) \
435 spl_kmem_cache_reap_now(skc, skc->skc_reap)
436 #define kmem_reap() spl_kmem_reap()
437 #define kmem_virt(ptr) (((ptr) >= (void *)VMALLOC_START) && \
438 ((ptr) < (void *)VMALLOC_END))
440 #endif /* _SPL_KMEM_H */