]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-vmem.c
New upstream version 0.7.2
[mirror_spl-debian.git] / module / spl / spl-vmem.c
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 #include <sys/debug.h>
26 #include <sys/vmem.h>
27 #include <sys/kmem_cache.h>
28 #include <linux/mm_compat.h>
29 #include <linux/module.h>
30
31 vmem_t *heap_arena = NULL;
32 EXPORT_SYMBOL(heap_arena);
33
34 vmem_t *zio_alloc_arena = NULL;
35 EXPORT_SYMBOL(zio_alloc_arena);
36
37 vmem_t *zio_arena = NULL;
38 EXPORT_SYMBOL(zio_arena);
39
40 #define VMEM_FLOOR_SIZE (4 * 1024 * 1024) /* 4MB floor */
41
42 /*
43 * Return approximate virtual memory usage based on these assumptions:
44 *
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.
49 */
50 size_t
51 vmem_size(vmem_t *vmp, int typemask)
52 {
53 spl_kmem_cache_t *skc;
54 size_t alloc = VMEM_FLOOR_SIZE;
55
56 if ((typemask & VMEM_ALLOC) && (typemask & VMEM_FREE))
57 return (VMALLOC_TOTAL);
58
59
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;
64 }
65 up_read(&spl_kmem_cache_sem);
66
67 if (typemask & VMEM_ALLOC)
68 return (MIN(alloc, VMALLOC_TOTAL));
69 else if (typemask & VMEM_FREE)
70 return (MAX(VMALLOC_TOTAL - alloc, 0));
71 else
72 return (0);
73 }
74 EXPORT_SYMBOL(vmem_size);
75
76 /*
77 * Public vmem_alloc(), vmem_zalloc() and vmem_free() interfaces.
78 */
79 void *
80 spl_vmem_alloc(size_t size, int flags, const char *func, int line)
81 {
82 ASSERT0(flags & ~KM_PUBLIC_MASK);
83
84 flags |= KM_VMEM;
85
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));
90 #else
91 return (spl_kmem_alloc_track(size, flags, func, line, NUMA_NO_NODE));
92 #endif
93 }
94 EXPORT_SYMBOL(spl_vmem_alloc);
95
96 void *
97 spl_vmem_zalloc(size_t size, int flags, const char *func, int line)
98 {
99 ASSERT0(flags & ~KM_PUBLIC_MASK);
100
101 flags |= (KM_VMEM | KM_ZERO);
102
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));
107 #else
108 return (spl_kmem_alloc_track(size, flags, func, line, NUMA_NO_NODE));
109 #endif
110 }
111 EXPORT_SYMBOL(spl_vmem_zalloc);
112
113 void
114 spl_vmem_free(const void *buf, size_t size)
115 {
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));
120 #else
121 return (spl_kmem_free_track(buf, size));
122 #endif
123 }
124 EXPORT_SYMBOL(spl_vmem_free);
125
126 int
127 spl_vmem_init(void)
128 {
129 return (0);
130 }
131
132 void
133 spl_vmem_fini(void)
134 {
135 }