xref: /freebsd-head/sys/vm/vm_radix.h (revision 8f58b693814e06afc51eade99a8c38e4abe9a804)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 EMC Corp.
5  * Copyright (c) 2011 Jeffrey Roberson <jeff@freebsd.org>
6  * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef _VM_RADIX_H_
32 #define _VM_RADIX_H_
33 
34 #include <vm/_vm_radix.h>
35 
36 #ifdef _KERNEL
37 #include <sys/pctrie.h>
38 #include <vm/vm_page.h>
39 #include <vm/vm.h>
40 
41 void		vm_radix_wait(void);
42 void		vm_radix_zinit(void);
43 void		*vm_radix_node_alloc(struct pctrie *ptree);
44 void		vm_radix_node_free(struct pctrie *ptree, void *node);
45 extern smr_t	vm_radix_smr;
46 
47 static __inline void
vm_radix_init(struct vm_radix * rtree)48 vm_radix_init(struct vm_radix *rtree)
49 {
50 	pctrie_init(&rtree->rt_trie);
51 }
52 
53 static __inline bool
vm_radix_is_empty(struct vm_radix * rtree)54 vm_radix_is_empty(struct vm_radix *rtree)
55 {
56 	return (pctrie_is_empty(&rtree->rt_trie));
57 }
58 
59 PCTRIE_DEFINE_SMR(VM_RADIX, vm_page, pindex, vm_radix_node_alloc,
60     vm_radix_node_free, vm_radix_smr);
61 
62 /*
63  * Inserts the key-value pair into the trie, starting search from root.
64  * Panics if the key already exists.
65  */
66 static __inline int
vm_radix_insert(struct vm_radix * rtree,vm_page_t page)67 vm_radix_insert(struct vm_radix *rtree, vm_page_t page)
68 {
69 	return (VM_RADIX_PCTRIE_INSERT(&rtree->rt_trie, page));
70 }
71 
72 /*
73  * Inserts the key-value pair into the trie, starting search from iterator.
74  * Panics if the key already exists.
75  */
76 static __inline int
vm_radix_iter_insert(struct pctrie_iter * pages,vm_page_t page)77 vm_radix_iter_insert(struct pctrie_iter *pages, vm_page_t page)
78 {
79 	return (VM_RADIX_PCTRIE_ITER_INSERT(pages, page));
80 }
81 
82 /*
83  * Insert the page into the vm_radix tree with its pindex as the key.  Panic if
84  * the pindex already exists.  Return zero on success or a non-zero error on
85  * memory allocation failure.  Set the out parameter mpred to the previous page
86  * in the tree as if found by a previous call to vm_radix_lookup_le with the
87  * new page pindex.
88  */
89 static __inline int
vm_radix_insert_lookup_lt(struct vm_radix * rtree,vm_page_t page,vm_page_t * mpred)90 vm_radix_insert_lookup_lt(struct vm_radix *rtree, vm_page_t page,
91     vm_page_t *mpred)
92 {
93 	int error;
94 
95 	error = VM_RADIX_PCTRIE_INSERT_LOOKUP_LE(&rtree->rt_trie, page, mpred);
96 	if (__predict_false(error == EEXIST))
97 		panic("vm_radix_insert_lookup_lt: page already present, %p",
98 		    *mpred);
99 	return (error);
100 }
101 
102 /*
103  * Returns the value stored at the index assuming there is an external lock.
104  *
105  * If the index is not present, NULL is returned.
106  */
107 static __inline vm_page_t
vm_radix_lookup(struct vm_radix * rtree,vm_pindex_t index)108 vm_radix_lookup(struct vm_radix *rtree, vm_pindex_t index)
109 {
110 	return (VM_RADIX_PCTRIE_LOOKUP(&rtree->rt_trie, index));
111 }
112 
113 /*
114  * Returns the value stored at the index without requiring an external lock.
115  *
116  * If the index is not present, NULL is returned.
117  */
118 static __inline vm_page_t
vm_radix_lookup_unlocked(struct vm_radix * rtree,vm_pindex_t index)119 vm_radix_lookup_unlocked(struct vm_radix *rtree, vm_pindex_t index)
120 {
121 	return (VM_RADIX_PCTRIE_LOOKUP_UNLOCKED(&rtree->rt_trie, index));
122 }
123 
124 /*
125  * Returns the number of contiguous, non-NULL pages read into the ma[]
126  * array, without requiring an external lock.
127  */
128 static __inline int
vm_radix_lookup_range_unlocked(struct vm_radix * rtree,vm_pindex_t index,vm_page_t ma[],int count)129 vm_radix_lookup_range_unlocked(struct vm_radix *rtree, vm_pindex_t index,
130     vm_page_t ma[], int count)
131 {
132 	return (VM_RADIX_PCTRIE_LOOKUP_RANGE_UNLOCKED(&rtree->rt_trie, index,
133 	    ma, count));
134 }
135 
136 /*
137  * Initialize an iterator for vm_radix.
138  */
139 static __inline void
vm_radix_iter_init(struct pctrie_iter * pages,struct vm_radix * rtree)140 vm_radix_iter_init(struct pctrie_iter *pages, struct vm_radix *rtree)
141 {
142 	pctrie_iter_init(pages, &rtree->rt_trie);
143 }
144 
145 /*
146  * Initialize an iterator for vm_radix.
147  */
148 static __inline void
vm_radix_iter_limit_init(struct pctrie_iter * pages,struct vm_radix * rtree,vm_pindex_t limit)149 vm_radix_iter_limit_init(struct pctrie_iter *pages, struct vm_radix *rtree,
150     vm_pindex_t limit)
151 {
152 	pctrie_iter_limit_init(pages, &rtree->rt_trie, limit);
153 }
154 
155 /*
156  * Returns the value stored at the index.
157  * Requires that access be externally synchronized by a lock.
158  *
159  * If the index is not present, NULL is returned.
160  */
161 static __inline vm_page_t
vm_radix_iter_lookup(struct pctrie_iter * pages,vm_pindex_t index)162 vm_radix_iter_lookup(struct pctrie_iter *pages, vm_pindex_t index)
163 {
164 	return (VM_RADIX_PCTRIE_ITER_LOOKUP(pages, index));
165 }
166 
167 /*
168  * Returns the value stored 'stride' steps beyond the current position.
169  * Requires that access be externally synchronized by a lock.
170  *
171  * If the index is not present, NULL is returned.
172  */
173 static __inline vm_page_t
vm_radix_iter_stride(struct pctrie_iter * pages,int stride)174 vm_radix_iter_stride(struct pctrie_iter *pages, int stride)
175 {
176 	return (VM_RADIX_PCTRIE_ITER_STRIDE(pages, stride));
177 }
178 
179 /*
180  * Returns the page with the least pindex that is greater than or equal to the
181  * specified pindex, or NULL if there are no such pages.
182  *
183  * Requires that access be externally synchronized by a lock.
184  */
185 static __inline vm_page_t
vm_radix_lookup_ge(struct vm_radix * rtree,vm_pindex_t index)186 vm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index)
187 {
188 	return (VM_RADIX_PCTRIE_LOOKUP_GE(&rtree->rt_trie, index));
189 }
190 
191 /*
192  * Returns the page with the greatest pindex that is less than or equal to the
193  * specified pindex, or NULL if there are no such pages.
194  *
195  * Requires that access be externally synchronized by a lock.
196  */
197 static __inline vm_page_t
vm_radix_lookup_le(struct vm_radix * rtree,vm_pindex_t index)198 vm_radix_lookup_le(struct vm_radix *rtree, vm_pindex_t index)
199 {
200 	return (VM_RADIX_PCTRIE_LOOKUP_LE(&rtree->rt_trie, index));
201 }
202 
203 /*
204  * Remove the specified index from the trie, and return the value stored at
205  * that index.  If the index is not present, return NULL.
206  */
207 static __inline vm_page_t
vm_radix_remove(struct vm_radix * rtree,vm_pindex_t index)208 vm_radix_remove(struct vm_radix *rtree, vm_pindex_t index)
209 {
210 	return (VM_RADIX_PCTRIE_REMOVE_LOOKUP(&rtree->rt_trie, index));
211 }
212 
213 /*
214  * Remove the current page from the trie.
215  */
216 static __inline void
vm_radix_iter_remove(struct pctrie_iter * pages)217 vm_radix_iter_remove(struct pctrie_iter *pages)
218 {
219 	VM_RADIX_PCTRIE_ITER_REMOVE(pages);
220 }
221 
222 /*
223  * Reclaim all the interior nodes of the trie, and invoke the callback
224  * on all the pages, in order.
225  */
226 static __inline void
vm_radix_reclaim_callback(struct vm_radix * rtree,void (* page_cb)(vm_page_t,void *),void * arg)227 vm_radix_reclaim_callback(struct vm_radix *rtree,
228     void (*page_cb)(vm_page_t, void *), void *arg)
229 {
230 	VM_RADIX_PCTRIE_RECLAIM_CALLBACK(&rtree->rt_trie, page_cb, arg);
231 }
232 
233 /*
234  * Initialize an iterator pointing to the page with the least pindex that is
235  * greater than or equal to the specified pindex, or NULL if there are no such
236  * pages.  Return the page.
237  *
238  * Requires that access be externally synchronized by a lock.
239  */
240 static __inline vm_page_t
vm_radix_iter_lookup_ge(struct pctrie_iter * pages,vm_pindex_t index)241 vm_radix_iter_lookup_ge(struct pctrie_iter *pages, vm_pindex_t index)
242 {
243 	return (VM_RADIX_PCTRIE_ITER_LOOKUP_GE(pages, index));
244 }
245 
246 /*
247  * Update the iterator to point to the page with the least pindex that is 'jump'
248  * or more greater than or equal to the current pindex, or NULL if there are no
249  * such pages.  Return the page.
250  *
251  * Requires that access be externally synchronized by a lock.
252  */
253 static __inline vm_page_t
vm_radix_iter_jump(struct pctrie_iter * pages,vm_pindex_t jump)254 vm_radix_iter_jump(struct pctrie_iter *pages, vm_pindex_t jump)
255 {
256 	return (VM_RADIX_PCTRIE_ITER_JUMP_GE(pages, jump));
257 }
258 
259 /*
260  * Update the iterator to point to the page with the least pindex that is one or
261  * more greater than the current pindex, or NULL if there are no such pages.
262  * Return the page.
263  *
264  * Requires that access be externally synchronized by a lock.
265  */
266 static __inline vm_page_t
vm_radix_iter_step(struct pctrie_iter * pages)267 vm_radix_iter_step(struct pctrie_iter *pages)
268 {
269 	return (VM_RADIX_PCTRIE_ITER_STEP_GE(pages));
270 }
271 
272 /*
273  * Iterate over each non-NULL page from page 'start' to the end of the object.
274  */
275 #define VM_RADIX_FOREACH_FROM(m, pages, start)				\
276 	for (m = vm_radix_iter_lookup_ge(pages, start); m != NULL;	\
277 	    m = vm_radix_iter_step(pages))
278 
279 /*
280  * Iterate over each non-NULL page from the beginning to the end of the object.
281  */
282 #define VM_RADIX_FOREACH(m, pages) VM_RADIX_FOREACH_FROM(m, pages, 0)
283 
284 /*
285  * Initialize an iterator pointing to the page with the greatest pindex that is
286  * less than or equal to the specified pindex, or NULL if there are no such
287  * pages.  Return the page.
288  *
289  * Requires that access be externally synchronized by a lock.
290  */
291 static __inline vm_page_t
vm_radix_iter_lookup_le(struct pctrie_iter * pages,vm_pindex_t index)292 vm_radix_iter_lookup_le(struct pctrie_iter *pages, vm_pindex_t index)
293 {
294 	return (VM_RADIX_PCTRIE_ITER_LOOKUP_LE(pages, index));
295 }
296 
297 /*
298  * Initialize an iterator pointing to the page with the greatest pindex that is
299  * less than to the specified pindex, or NULL if there are no such
300  * pages.  Return the page.
301  *
302  * Requires that access be externally synchronized by a lock.
303  */
304 static __inline vm_page_t
vm_radix_iter_lookup_lt(struct pctrie_iter * pages,vm_pindex_t index)305 vm_radix_iter_lookup_lt(struct pctrie_iter *pages, vm_pindex_t index)
306 {
307 	return (index == 0 ? NULL : vm_radix_iter_lookup_le(pages, index - 1));
308 }
309 
310 /*
311  * Update the iterator to point to the page with the pindex that is one greater
312  * than the current pindex, or NULL if there is no such page.  Return the page.
313  *
314  * Requires that access be externally synchronized by a lock.
315  */
316 static __inline vm_page_t
vm_radix_iter_next(struct pctrie_iter * pages)317 vm_radix_iter_next(struct pctrie_iter *pages)
318 {
319 	return (VM_RADIX_PCTRIE_ITER_NEXT(pages));
320 }
321 
322 /*
323  * Iterate over consecutive non-NULL pages from position 'start' to first NULL
324  * page.
325  */
326 #define VM_RADIX_FORALL_FROM(m, pages, start)				\
327 	for (m = vm_radix_iter_lookup(pages, start); m != NULL;		\
328 	    m = vm_radix_iter_next(pages))
329 
330 /*
331  * Iterate over consecutive non-NULL pages from the beginning to first NULL
332  * page.
333  */
334 #define VM_RADIX_FORALL(m, pages) VM_RADIX_FORALL_FROM(m, pages, 0)
335 
336 /*
337  * Update the iterator to point to the page with the pindex that is one less
338  * than the current pindex, or NULL if there is no such page.  Return the page.
339  *
340  * Requires that access be externally synchronized by a lock.
341  */
342 static __inline vm_page_t
vm_radix_iter_prev(struct pctrie_iter * pages)343 vm_radix_iter_prev(struct pctrie_iter *pages)
344 {
345 	return (VM_RADIX_PCTRIE_ITER_PREV(pages));
346 }
347 
348 /*
349  * Return the current page.
350  *
351  * Requires that access be externally synchronized by a lock.
352  */
353 static __inline vm_page_t
vm_radix_iter_page(struct pctrie_iter * pages)354 vm_radix_iter_page(struct pctrie_iter *pages)
355 {
356 	return (VM_RADIX_PCTRIE_ITER_VALUE(pages));
357 }
358 
359 /*
360  * Replace an existing page in the trie with another one.
361  * Panics if there is not an old page in the trie at the new page's index.
362  */
363 static __inline vm_page_t
vm_radix_replace(struct vm_radix * rtree,vm_page_t newpage)364 vm_radix_replace(struct vm_radix *rtree, vm_page_t newpage)
365 {
366 	return (VM_RADIX_PCTRIE_REPLACE(&rtree->rt_trie, newpage));
367 }
368 
369 #endif /* _KERNEL */
370 #endif /* !_VM_RADIX_H_ */
371