1 /* $NetBSD: tmpfs_mem.c,v 1.1 2010/06/22 18:32:07 rmind Exp $ */ 2 3 /* 4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * tmpfs memory allocation routines. 31 * Implements memory usage accounting and limiting. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: tmpfs_mem.c,v 1.1 2010/06/22 18:32:07 rmind Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/atomic.h> 39 #include <sys/kmem.h> 40 #include <sys/namei.h> 41 #include <sys/pool.h> 42 43 #include <fs/tmpfs/tmpfs.h> 44 45 void 46 tmpfs_mntmem_init(struct tmpfs_mount *mp, uint64_t memlimit) 47 { 48 49 sprintf(mp->tm_dwchan, "tmpfs_dirent_%p", mp); 50 pool_init(&mp->tm_dirent_pool, sizeof(struct tmpfs_dirent), 0, 0, 0, 51 mp->tm_dwchan, &pool_allocator_nointr, IPL_NONE); 52 53 sprintf(mp->tm_nwchan, "tmpfs_node_%p", mp); 54 pool_init(&mp->tm_node_pool, sizeof(struct tmpfs_node), 0, 0, 0, 55 mp->tm_dwchan, &pool_allocator_nointr, IPL_NONE); 56 57 mutex_init(&mp->tm_acc_lock, MUTEX_DEFAULT, IPL_NONE); 58 mp->tm_mem_limit = memlimit; 59 mp->tm_bytes_used = 0; 60 } 61 62 void 63 tmpfs_mntmem_destroy(struct tmpfs_mount *mp) 64 { 65 66 KASSERT(mp->tm_bytes_used == 0); 67 mutex_destroy(&mp->tm_acc_lock); 68 pool_destroy(&mp->tm_dirent_pool); 69 pool_destroy(&mp->tm_node_pool); 70 } 71 72 /* 73 * tmpfs_mem_info: return the number of available memory pages. 74 * 75 * => If 'total' is true, then return _total_ amount of pages. 76 * => If false, then return the amount of _free_ memory pages. 77 * 78 * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid 79 * excessive memory usage. 80 */ 81 size_t 82 tmpfs_mem_info(bool total) 83 { 84 size_t size = 0; 85 86 /* XXX: unlocked */ 87 size += uvmexp.swpgavail; 88 if (!total) { 89 size -= uvmexp.swpgonly; 90 } 91 size += uvmexp.free; 92 size += uvmexp.filepages; 93 if (size > uvmexp.wired) { 94 size -= uvmexp.wired; 95 } else { 96 size = 0; 97 } 98 return size; 99 } 100 101 uint64_t 102 tmpfs_bytes_max(struct tmpfs_mount *mp) 103 { 104 size_t freepages = tmpfs_mem_info(false); 105 uint64_t avail_mem; 106 107 if (freepages < TMPFS_PAGES_RESERVED) { 108 freepages = 0; 109 } else { 110 freepages -= TMPFS_PAGES_RESERVED; 111 } 112 avail_mem = round_page(mp->tm_bytes_used) + (freepages << PAGE_SHIFT); 113 return min(mp->tm_mem_limit, avail_mem); 114 } 115 116 size_t 117 tmpfs_pages_avail(struct tmpfs_mount *mp) 118 { 119 120 return (tmpfs_bytes_max(mp) - mp->tm_bytes_used) >> PAGE_SHIFT; 121 } 122 123 bool 124 tmpfs_mem_incr(struct tmpfs_mount *mp, size_t sz) 125 { 126 const uint64_t lim = tmpfs_bytes_max(mp); 127 128 mutex_enter(&mp->tm_acc_lock); 129 if (mp->tm_bytes_used + sz >= lim) { 130 mutex_exit(&mp->tm_acc_lock); 131 return false; 132 } 133 mp->tm_bytes_used += sz; 134 mutex_exit(&mp->tm_acc_lock); 135 return true; 136 } 137 138 void 139 tmpfs_mem_decr(struct tmpfs_mount *mp, size_t sz) 140 { 141 142 mutex_enter(&mp->tm_acc_lock); 143 KASSERT(mp->tm_bytes_used >= sz); 144 mp->tm_bytes_used -= sz; 145 mutex_exit(&mp->tm_acc_lock); 146 } 147 148 struct tmpfs_dirent * 149 tmpfs_dirent_get(struct tmpfs_mount *mp) 150 { 151 152 if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_dirent))) { 153 return NULL; 154 } 155 return pool_get(&mp->tm_dirent_pool, PR_WAITOK); 156 } 157 158 void 159 tmpfs_dirent_put(struct tmpfs_mount *mp, struct tmpfs_dirent *de) 160 { 161 162 tmpfs_mem_decr(mp, sizeof(struct tmpfs_dirent)); 163 pool_put(&mp->tm_dirent_pool, de); 164 } 165 166 struct tmpfs_node * 167 tmpfs_node_get(struct tmpfs_mount *mp) 168 { 169 170 if (!tmpfs_mem_incr(mp, sizeof(struct tmpfs_node))) { 171 return NULL; 172 } 173 return pool_get(&mp->tm_node_pool, PR_WAITOK); 174 } 175 176 void 177 tmpfs_node_put(struct tmpfs_mount *mp, struct tmpfs_node *tn) 178 { 179 180 tmpfs_mem_decr(mp, sizeof(struct tmpfs_node)); 181 pool_put(&mp->tm_node_pool, tn); 182 } 183 184 /* 185 * Quantum size to round-up the tmpfs names in order to reduce re-allocations. 186 */ 187 188 #define TMPFS_NAME_QUANTUM (32) 189 190 char * 191 tmpfs_strname_alloc(struct tmpfs_mount *mp, size_t len) 192 { 193 const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM); 194 195 KASSERT(sz > 0 && sz <= 1024); 196 if (!tmpfs_mem_incr(mp, sz)) { 197 return NULL; 198 } 199 return kmem_alloc(sz, KM_SLEEP); 200 } 201 202 void 203 tmpfs_strname_free(struct tmpfs_mount *mp, char *str, size_t len) 204 { 205 const size_t sz = roundup2(len, TMPFS_NAME_QUANTUM); 206 207 KASSERT(sz > 0 && sz <= 1024); 208 tmpfs_mem_decr(mp, sz); 209 kmem_free(str, sz); 210 } 211 212 bool 213 tmpfs_strname_neqlen(struct componentname *fcnp, struct componentname *tcnp) 214 { 215 const size_t fln = roundup2(fcnp->cn_namelen, TMPFS_NAME_QUANTUM); 216 const size_t tln = roundup2(tcnp->cn_namelen, TMPFS_NAME_QUANTUM); 217 218 return (fln != tln) || memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fln); 219 } 220