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_NOIO | __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(const void *, size_t);
218 extern void *vmem_alloc_track(size_t, int, const char *, int);
219 extern void vmem_free_track(const 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(const void *, size_t);
247 extern void *vmem_alloc_debug(size_t, int, const char *, int);
248 extern void vmem_free_debug(const 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_GROWING
= 15, /* Growing in progress */
295 KMC_BIT_REAPING
= 16, /* Reaping in progress */
296 KMC_BIT_DESTROY
= 17, /* Destroy in progress */
297 KMC_BIT_TOTAL
= 18, /* Proc handler helper bit */
298 KMC_BIT_ALLOC
= 19, /* Proc handler helper bit */
299 KMC_BIT_MAX
= 20, /* Proc handler helper bit */
302 /* kmem move callback return values */
303 typedef enum kmem_cbrc
{
304 KMEM_CBRC_YES
= 0, /* Object moved */
305 KMEM_CBRC_NO
= 1, /* Object not moved */
306 KMEM_CBRC_LATER
= 2, /* Object not moved, try again later */
307 KMEM_CBRC_DONT_NEED
= 3, /* Neither object is needed */
308 KMEM_CBRC_DONT_KNOW
= 4, /* Object unknown */
311 #define KMC_NOTOUCH (1 << KMC_BIT_NOTOUCH)
312 #define KMC_NODEBUG (1 << KMC_BIT_NODEBUG)
313 #define KMC_NOMAGAZINE (1 << KMC_BIT_NOMAGAZINE)
314 #define KMC_NOHASH (1 << KMC_BIT_NOHASH)
315 #define KMC_QCACHE (1 << KMC_BIT_QCACHE)
316 #define KMC_KMEM (1 << KMC_BIT_KMEM)
317 #define KMC_VMEM (1 << KMC_BIT_VMEM)
318 #define KMC_OFFSLAB (1 << KMC_BIT_OFFSLAB)
319 #define KMC_GROWING (1 << KMC_BIT_GROWING)
320 #define KMC_REAPING (1 << KMC_BIT_REAPING)
321 #define KMC_DESTROY (1 << KMC_BIT_DESTROY)
322 #define KMC_TOTAL (1 << KMC_BIT_TOTAL)
323 #define KMC_ALLOC (1 << KMC_BIT_ALLOC)
324 #define KMC_MAX (1 << KMC_BIT_MAX)
326 #define KMC_REAP_CHUNK INT_MAX
327 #define KMC_DEFAULT_SEEKS 1
329 extern struct list_head spl_kmem_cache_list
;
330 extern struct rw_semaphore spl_kmem_cache_sem
;
332 #define SKM_MAGIC 0x2e2e2e2e
333 #define SKO_MAGIC 0x20202020
334 #define SKS_MAGIC 0x22222222
335 #define SKC_MAGIC 0x2c2c2c2c
337 #define SPL_KMEM_CACHE_DELAY 15 /* Minimum slab release age */
338 #define SPL_KMEM_CACHE_REAP 0 /* Default reap everything */
339 #define SPL_KMEM_CACHE_OBJ_PER_SLAB 16 /* Target objects per slab */
340 #define SPL_KMEM_CACHE_OBJ_PER_SLAB_MIN 8 /* Minimum objects per slab */
341 #define SPL_KMEM_CACHE_ALIGN 8 /* Default object alignment */
343 #define POINTER_IS_VALID(p) 0 /* Unimplemented */
344 #define POINTER_INVALIDATE(pp) /* Unimplemented */
346 typedef int (*spl_kmem_ctor_t
)(void *, void *, int);
347 typedef void (*spl_kmem_dtor_t
)(void *, void *);
348 typedef void (*spl_kmem_reclaim_t
)(void *);
350 typedef struct spl_kmem_magazine
{
351 uint32_t skm_magic
; /* Sanity magic */
352 uint32_t skm_avail
; /* Available objects */
353 uint32_t skm_size
; /* Magazine size */
354 uint32_t skm_refill
; /* Batch refill size */
355 struct spl_kmem_cache
*skm_cache
; /* Owned by cache */
356 struct delayed_work skm_work
; /* Magazine reclaim work */
357 unsigned long skm_age
; /* Last cache access */
358 unsigned int skm_cpu
; /* Owned by cpu */
359 void *skm_objs
[0]; /* Object pointers */
360 } spl_kmem_magazine_t
;
362 typedef struct spl_kmem_obj
{
363 uint32_t sko_magic
; /* Sanity magic */
364 void *sko_addr
; /* Buffer address */
365 struct spl_kmem_slab
*sko_slab
; /* Owned by slab */
366 struct list_head sko_list
; /* Free object list linkage */
369 typedef struct spl_kmem_slab
{
370 uint32_t sks_magic
; /* Sanity magic */
371 uint32_t sks_objs
; /* Objects per slab */
372 struct spl_kmem_cache
*sks_cache
; /* Owned by cache */
373 struct list_head sks_list
; /* Slab list linkage */
374 struct list_head sks_free_list
; /* Free object list */
375 unsigned long sks_age
; /* Last modify jiffie */
376 uint32_t sks_ref
; /* Ref count used objects */
379 typedef struct spl_kmem_alloc
{
380 struct spl_kmem_cache
*ska_cache
; /* Owned by cache */
381 int ska_flags
; /* Allocation flags */
382 struct delayed_work ska_work
; /* Allocation work */
385 typedef struct spl_kmem_emergency
{
386 void *ske_obj
; /* Buffer address */
387 struct list_head ske_list
; /* Emergency list linkage */
388 } spl_kmem_emergency_t
;
390 typedef struct spl_kmem_cache
{
391 uint32_t skc_magic
; /* Sanity magic */
392 uint32_t skc_name_size
; /* Name length */
393 char *skc_name
; /* Name string */
394 spl_kmem_magazine_t
*skc_mag
[NR_CPUS
]; /* Per-CPU warm cache */
395 uint32_t skc_mag_size
; /* Magazine size */
396 uint32_t skc_mag_refill
; /* Magazine refill count */
397 spl_kmem_ctor_t skc_ctor
; /* Constructor */
398 spl_kmem_dtor_t skc_dtor
; /* Destructor */
399 spl_kmem_reclaim_t skc_reclaim
; /* Reclaimator */
400 void *skc_private
; /* Private data */
401 void *skc_vmp
; /* Unused */
402 unsigned long skc_flags
; /* Flags */
403 uint32_t skc_obj_size
; /* Object size */
404 uint32_t skc_obj_align
; /* Object alignment */
405 uint32_t skc_slab_objs
; /* Objects per slab */
406 uint32_t skc_slab_size
; /* Slab size */
407 uint32_t skc_delay
; /* Slab reclaim interval */
408 uint32_t skc_reap
; /* Slab reclaim count */
409 atomic_t skc_ref
; /* Ref count callers */
410 struct delayed_work skc_work
; /* Slab reclaim work */
411 struct list_head skc_list
; /* List of caches linkage */
412 struct list_head skc_complete_list
;/* Completely alloc'ed */
413 struct list_head skc_partial_list
; /* Partially alloc'ed */
414 struct list_head skc_emergency_list
; /* Min sized objects */
415 spinlock_t skc_lock
; /* Cache lock */
416 wait_queue_head_t skc_waitq
; /* Allocation waiters */
417 uint64_t skc_slab_fail
; /* Slab alloc failures */
418 uint64_t skc_slab_create
;/* Slab creates */
419 uint64_t skc_slab_destroy
;/* Slab destroys */
420 uint64_t skc_slab_total
; /* Slab total current */
421 uint64_t skc_slab_alloc
; /* Slab alloc current */
422 uint64_t skc_slab_max
; /* Slab max historic */
423 uint64_t skc_obj_total
; /* Obj total current */
424 uint64_t skc_obj_alloc
; /* Obj alloc current */
425 uint64_t skc_obj_max
; /* Obj max historic */
426 uint64_t skc_obj_emergency
; /* Obj emergency current */
427 uint64_t skc_obj_emergency_max
; /* Obj emergency max */
429 #define kmem_cache_t spl_kmem_cache_t
431 extern spl_kmem_cache_t
*spl_kmem_cache_create(char *name
, size_t size
,
432 size_t align
, spl_kmem_ctor_t ctor
, spl_kmem_dtor_t dtor
,
433 spl_kmem_reclaim_t reclaim
, void *priv
, void *vmp
, int flags
);
434 extern void spl_kmem_cache_set_move(spl_kmem_cache_t
*,
435 kmem_cbrc_t (*)(void *, void *, size_t, void *));
436 extern void spl_kmem_cache_destroy(spl_kmem_cache_t
*skc
);
437 extern void *spl_kmem_cache_alloc(spl_kmem_cache_t
*skc
, int flags
);
438 extern void spl_kmem_cache_free(spl_kmem_cache_t
*skc
, void *obj
);
439 extern void spl_kmem_cache_reap_now(spl_kmem_cache_t
*skc
, int count
);
440 extern void spl_kmem_reap(void);
442 int spl_kmem_init_kallsyms_lookup(void);
443 int spl_kmem_init(void);
444 void spl_kmem_fini(void);
446 #define kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags) \
447 spl_kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags)
448 #define kmem_cache_set_move(skc, move) spl_kmem_cache_set_move(skc, move)
449 #define kmem_cache_destroy(skc) spl_kmem_cache_destroy(skc)
450 #define kmem_cache_alloc(skc, flags) spl_kmem_cache_alloc(skc, flags)
451 #define kmem_cache_free(skc, obj) spl_kmem_cache_free(skc, obj)
452 #define kmem_cache_reap_now(skc) \
453 spl_kmem_cache_reap_now(skc, skc->skc_reap)
454 #define kmem_reap() spl_kmem_reap()
455 #define kmem_virt(ptr) (((ptr) >= (void *)VMALLOC_START) && \
456 ((ptr) < (void *)VMALLOC_END))
458 #endif /* _SPL_KMEM_H */