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>
27 #include <sys/kmem_cache.h>
28 #include <sys/shrinker.h>
29 #include <linux/module.h>
31 vmem_t
*heap_arena
= NULL
;
32 EXPORT_SYMBOL(heap_arena
);
34 vmem_t
*zio_alloc_arena
= NULL
;
35 EXPORT_SYMBOL(zio_alloc_arena
);
37 vmem_t
*zio_arena
= NULL
;
38 EXPORT_SYMBOL(zio_arena
);
40 #define VMEM_FLOOR_SIZE (4 * 1024 * 1024) /* 4MB floor */
43 * Return approximate virtual memory usage based on these assumptions:
45 * 1) The major SPL consumer of virtual memory is the kmem cache.
46 * 2) Memory allocated with vmem_alloc() is short lived and can be ignored.
47 * 3) Allow a 4MB floor as a generous pad given normal consumption.
48 * 4) The spl_kmem_cache_sem only contends with cache create/destroy.
51 vmem_size(vmem_t
*vmp
, int typemask
)
53 spl_kmem_cache_t
*skc
;
54 size_t alloc
= VMEM_FLOOR_SIZE
;
56 if ((typemask
& VMEM_ALLOC
) && (typemask
& VMEM_FREE
))
57 return (VMALLOC_TOTAL
);
60 down_read(&spl_kmem_cache_sem
);
61 list_for_each_entry(skc
, &spl_kmem_cache_list
, skc_list
) {
62 if (skc
->skc_flags
& KMC_VMEM
)
63 alloc
+= skc
->skc_slab_size
* skc
->skc_slab_total
;
65 up_read(&spl_kmem_cache_sem
);
67 if (typemask
& VMEM_ALLOC
)
68 return (MIN(alloc
, VMALLOC_TOTAL
));
69 else if (typemask
& VMEM_FREE
)
70 return (MAX(VMALLOC_TOTAL
- alloc
, 0));
74 EXPORT_SYMBOL(vmem_size
);
77 * Public vmem_alloc(), vmem_zalloc() and vmem_free() interfaces.
80 spl_vmem_alloc(size_t size
, int flags
, const char *func
, int line
)
82 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
86 #if !defined(DEBUG_KMEM)
87 return (spl_kmem_alloc_impl(size
, flags
, NUMA_NO_NODE
));
88 #elif !defined(DEBUG_KMEM_TRACKING)
89 return (spl_kmem_alloc_debug(size
, flags
, NUMA_NO_NODE
));
91 return (spl_kmem_alloc_track(size
, flags
, func
, line
, NUMA_NO_NODE
));
94 EXPORT_SYMBOL(spl_vmem_alloc
);
97 spl_vmem_zalloc(size_t size
, int flags
, const char *func
, int line
)
99 ASSERT0(flags
& ~KM_PUBLIC_MASK
);
101 flags
|= (KM_VMEM
| KM_ZERO
);
103 #if !defined(DEBUG_KMEM)
104 return (spl_kmem_alloc_impl(size
, flags
, NUMA_NO_NODE
));
105 #elif !defined(DEBUG_KMEM_TRACKING)
106 return (spl_kmem_alloc_debug(size
, flags
, NUMA_NO_NODE
));
108 return (spl_kmem_alloc_track(size
, flags
, func
, line
, NUMA_NO_NODE
));
111 EXPORT_SYMBOL(spl_vmem_zalloc
);
114 spl_vmem_free(const void *buf
, size_t size
)
116 #if !defined(DEBUG_KMEM)
117 return (spl_kmem_free_impl(buf
, size
));
118 #elif !defined(DEBUG_KMEM_TRACKING)
119 return (spl_kmem_free_debug(buf
, size
));
121 return (spl_kmem_free_track(buf
, size
));
124 EXPORT_SYMBOL(spl_vmem_free
);