2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017, Jeffrey Roberson <jeff@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bitset.h>
38 #include <sys/domainset.h>
41 #include <sys/mutex.h>
42 #include <sys/malloc.h>
43 #include <sys/vmmeter.h>
46 #include <vm/vm_param.h>
47 #include <vm/vm_domainset.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_phys.h>
54 * Iterators are written such that the first nowait pass has as short a
55 * codepath as possible to eliminate bloat from the allocator. It is
56 * assumed that most allocations are successful.
59 static int vm_domainset_default_stride = 64;
62 * Determine which policy is to be used for this allocation.
65 vm_domainset_iter_init(struct vm_domainset_iter *di, struct vm_object *obj,
68 struct domainset *domain;
71 * object policy takes precedence over thread policy. The policies
72 * are immutable and unsynchronized. Updates can race but pointer
73 * loads are assumed to be atomic.
75 if (obj != NULL && (domain = obj->domain.dr_policy) != NULL) {
76 di->di_domain = domain;
77 di->di_iter = &obj->domain.dr_iterator;
79 di->di_domain = curthread->td_domain.dr_policy;
80 di->di_iter = &curthread->td_domain.dr_iterator;
82 di->di_policy = di->di_domain->ds_policy;
83 if (di->di_policy == DOMAINSET_POLICY_INTERLEAVE) {
84 #if VM_NRESERVLEVEL > 0
85 if (vm_object_reserv(obj)) {
87 * Color the pindex so we end up on the correct
88 * reservation boundary.
90 pindex += obj->pg_color;
91 pindex >>= VM_LEVEL_0_ORDER;
94 pindex /= vm_domainset_default_stride;
96 * Offset pindex so the first page of each object does
97 * not end up in domain 0.
100 pindex += (((uintptr_t)obj) / sizeof(*obj));
101 di->di_offset = pindex;
103 /* Skip domains below min on the first pass. */
104 di->di_minskip = true;
108 vm_domainset_iter_rr(struct vm_domainset_iter *di, int *domain)
111 *domain = di->di_domain->ds_order[
112 ++(*di->di_iter) % di->di_domain->ds_cnt];
116 vm_domainset_iter_prefer(struct vm_domainset_iter *di, int *domain)
121 d = di->di_domain->ds_order[
122 ++(*di->di_iter) % di->di_domain->ds_cnt];
123 } while (d == di->di_domain->ds_prefer);
128 vm_domainset_iter_interleave(struct vm_domainset_iter *di, int *domain)
132 d = di->di_offset % di->di_domain->ds_cnt;
134 *domain = di->di_domain->ds_order[d];
138 vm_domainset_iter_next(struct vm_domainset_iter *di, int *domain)
141 KASSERT(di->di_n > 0,
142 ("vm_domainset_iter_first: Invalid n %d", di->di_n));
143 switch (di->di_policy) {
144 case DOMAINSET_POLICY_FIRSTTOUCH:
146 * To prevent impossible allocations we convert an invalid
147 * first-touch to round-robin.
150 case DOMAINSET_POLICY_INTERLEAVE:
152 case DOMAINSET_POLICY_ROUNDROBIN:
153 vm_domainset_iter_rr(di, domain);
155 case DOMAINSET_POLICY_PREFER:
156 vm_domainset_iter_prefer(di, domain);
159 panic("vm_domainset_iter_first: Unknown policy %d",
162 KASSERT(*domain < vm_ndomains,
163 ("vm_domainset_iter_next: Invalid domain %d", *domain));
167 vm_domainset_iter_first(struct vm_domainset_iter *di, int *domain)
170 switch (di->di_policy) {
171 case DOMAINSET_POLICY_FIRSTTOUCH:
172 *domain = PCPU_GET(domain);
173 if (DOMAINSET_ISSET(*domain, &di->di_domain->ds_mask)) {
175 * Add an extra iteration because we will visit the
176 * current domain a second time in the rr iterator.
178 di->di_n = di->di_domain->ds_cnt + 1;
182 * To prevent impossible allocations we convert an invalid
183 * first-touch to round-robin.
186 case DOMAINSET_POLICY_ROUNDROBIN:
187 di->di_n = di->di_domain->ds_cnt;
188 vm_domainset_iter_rr(di, domain);
190 case DOMAINSET_POLICY_PREFER:
191 *domain = di->di_domain->ds_prefer;
192 di->di_n = di->di_domain->ds_cnt;
194 case DOMAINSET_POLICY_INTERLEAVE:
195 vm_domainset_iter_interleave(di, domain);
196 di->di_n = di->di_domain->ds_cnt;
199 panic("vm_domainset_iter_first: Unknown policy %d",
202 KASSERT(di->di_n > 0,
203 ("vm_domainset_iter_first: Invalid n %d", di->di_n));
204 KASSERT(*domain < vm_ndomains,
205 ("vm_domainset_iter_first: Invalid domain %d", *domain));
209 vm_domainset_iter_page_init(struct vm_domainset_iter *di, struct vm_object *obj,
210 vm_pindex_t pindex, int *domain, int *req)
213 vm_domainset_iter_init(di, obj, pindex);
215 *req = (di->di_flags & ~(VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL)) |
217 vm_domainset_iter_first(di, domain);
218 if (DOMAINSET_ISSET(*domain, &vm_min_domains))
219 vm_domainset_iter_page(di, domain, req);
223 vm_domainset_iter_page(struct vm_domainset_iter *di, int *domain, int *req)
227 * If we exhausted all options with NOWAIT and did a WAITFAIL it
228 * is time to return an error to the caller.
230 if ((*req & VM_ALLOC_WAITFAIL) != 0)
233 /* If there are more domains to visit we run the iterator. */
234 while (--di->di_n != 0) {
235 vm_domainset_iter_next(di, domain);
236 if (!di->di_minskip ||
237 !DOMAINSET_ISSET(*domain, &vm_min_domains))
240 if (di->di_minskip) {
241 di->di_minskip = false;
242 vm_domainset_iter_first(di, domain);
246 /* If we visited all domains and this was a NOWAIT we return error. */
247 if ((di->di_flags & (VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL)) == 0)
251 * We have visited all domains with non-blocking allocations, try
252 * from the beginning with a blocking allocation.
254 vm_domainset_iter_first(di, domain);
262 vm_domainset_iter_malloc_init(struct vm_domainset_iter *di,
263 struct vm_object *obj, int *domain, int *flags)
266 vm_domainset_iter_init(di, obj, 0);
267 if (di->di_policy == DOMAINSET_POLICY_INTERLEAVE)
268 di->di_policy = DOMAINSET_POLICY_ROUNDROBIN;
269 di->di_flags = *flags;
270 *flags = (di->di_flags & ~M_WAITOK) | M_NOWAIT;
271 vm_domainset_iter_first(di, domain);
272 if (DOMAINSET_ISSET(*domain, &vm_min_domains))
273 vm_domainset_iter_malloc(di, domain, flags);
277 vm_domainset_iter_malloc(struct vm_domainset_iter *di, int *domain, int *flags)
280 /* If there are more domains to visit we run the iterator. */
281 while (--di->di_n != 0) {
282 vm_domainset_iter_next(di, domain);
283 if (!di->di_minskip ||
284 !DOMAINSET_ISSET(*domain, &vm_min_domains))
288 /* If we skipped domains below min restart the search. */
289 if (di->di_minskip) {
290 di->di_minskip = false;
291 vm_domainset_iter_first(di, domain);
295 /* If we visited all domains and this was a NOWAIT we return error. */
296 if ((di->di_flags & M_WAITOK) == 0)
300 * We have visited all domains with non-blocking allocations, try
301 * from the beginning with a blocking allocation.
303 vm_domainset_iter_first(di, domain);
304 *flags = di->di_flags;
311 vm_domainset_iter_page(struct vm_domainset_iter *di, int *domain, int *flags)
314 return (EJUSTRETURN);
318 vm_domainset_iter_page_init(struct vm_domainset_iter *di,
319 struct vm_object *obj, vm_pindex_t pindex, int *domain, int *flags)
326 vm_domainset_iter_malloc(struct vm_domainset_iter *di, int *domain, int *flags)
329 return (EJUSTRETURN);
333 vm_domainset_iter_malloc_init(struct vm_domainset_iter *di,
334 struct vm_object *obj, int *domain, int *flags)