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