]>
Commit | Line | Data |
---|---|---|
b34b9563 | 1 | /* |
e5b9b344 BB |
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/>. | |
b34b9563 | 23 | */ |
e5b9b344 BB |
24 | |
25 | #include <sys/debug.h> | |
26 | #include <sys/vmem.h> | |
1b457bcb | 27 | #include <sys/kmem_cache.h> |
a9125891 | 28 | #include <sys/shrinker.h> |
e5b9b344 BB |
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 | ||
1b457bcb BB |
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 | */ | |
e5b9b344 BB |
50 | size_t |
51 | vmem_size(vmem_t *vmp, int typemask) | |
52 | { | |
1b457bcb BB |
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); | |
e5b9b344 | 66 | |
1b457bcb BB |
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); | |
e5b9b344 BB |
73 | } |
74 | EXPORT_SYMBOL(vmem_size); | |
75 | ||
76 | /* | |
c3eabc75 | 77 | * Public vmem_alloc(), vmem_zalloc() and vmem_free() interfaces. |
e5b9b344 | 78 | */ |
e5b9b344 | 79 | void * |
c3eabc75 | 80 | spl_vmem_alloc(size_t size, int flags, const char *func, int line) |
e5b9b344 | 81 | { |
c3eabc75 | 82 | ASSERT0(flags & ~KM_PUBLIC_MASK); |
e5b9b344 | 83 | |
c3eabc75 | 84 | flags |= KM_VMEM; |
e5b9b344 | 85 | |
c3eabc75 BB |
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 | |
e5b9b344 | 93 | } |
c3eabc75 | 94 | EXPORT_SYMBOL(spl_vmem_alloc); |
e5b9b344 BB |
95 | |
96 | void * | |
c3eabc75 | 97 | spl_vmem_zalloc(size_t size, int flags, const char *func, int line) |
e5b9b344 | 98 | { |
c3eabc75 | 99 | ASSERT0(flags & ~KM_PUBLIC_MASK); |
e5b9b344 | 100 | |
c3eabc75 | 101 | flags |= (KM_VMEM | KM_ZERO); |
e5b9b344 | 102 | |
c3eabc75 BB |
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 | |
e5b9b344 | 110 | } |
c3eabc75 | 111 | EXPORT_SYMBOL(spl_vmem_zalloc); |
e5b9b344 BB |
112 | |
113 | void | |
c3eabc75 | 114 | spl_vmem_free(const void *buf, size_t size) |
e5b9b344 | 115 | { |
c3eabc75 BB |
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 | |
e5b9b344 | 123 | } |
c3eabc75 | 124 | EXPORT_SYMBOL(spl_vmem_free); |
e5b9b344 BB |
125 | |
126 | int | |
127 | spl_vmem_init(void) | |
128 | { | |
c3eabc75 | 129 | return (0); |
e5b9b344 BB |
130 | } |
131 | ||
132 | void | |
133 | spl_vmem_fini(void) | |
134 | { | |
e5b9b344 | 135 | } |