Home | History | Annotate | Line # | Download | only in uvm
uvm_pglist.c revision 1.89
      1 /*	$NetBSD: uvm_pglist.c,v 1.89 2021/12/20 22:40:46 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and by Andrew Doran.
     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  * uvm_pglist.c: pglist functions
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.89 2021/12/20 22:40:46 skrll Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/cpu.h>
     43 
     44 #include <uvm/uvm.h>
     45 #include <uvm/uvm_pdpolicy.h>
     46 #include <uvm/uvm_pgflcache.h>
     47 
     48 #ifdef VM_PAGE_ALLOC_MEMORY_STATS
     49 #define	STAT_INCR(v)	(v)++
     50 #define	STAT_DECR(v)	do { \
     51 		if ((v) == 0) \
     52 			printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
     53 		else \
     54 			(v)--; \
     55 	} while (/*CONSTCOND*/ 0)
     56 u_long	uvm_pglistalloc_npages;
     57 #else
     58 #define	STAT_INCR(v)
     59 #define	STAT_DECR(v)
     60 #endif
     61 
     62 kmutex_t uvm_pglistalloc_contig_lock;
     63 
     64 /*
     65  * uvm_pglistalloc: allocate a list of pages
     66  *
     67  * => allocated pages are placed onto an rlist.  rlist is
     68  *    initialized by uvm_pglistalloc.
     69  * => returns 0 on success or errno on failure
     70  * => implementation allocates a single segment if any constraints are
     71  *	imposed by call arguments.
     72  * => doesn't take into account clean non-busy pages on inactive list
     73  *	that could be used(?)
     74  * => params:
     75  *	size		the size of the allocation, rounded to page size.
     76  *	low		the low address of the allowed allocation range.
     77  *	high		the high address of the allowed allocation range.
     78  *	alignment	memory must be aligned to this power-of-two boundary.
     79  *	boundary	no segment in the allocation may cross this
     80  *			power-of-two boundary (relative to zero).
     81  */
     82 
     83 static void
     84 uvm_pglist_add(struct vm_page *pg, struct pglist *rlist)
     85 {
     86 	struct pgfreelist *pgfl;
     87 	struct pgflbucket *pgb;
     88 
     89 	pgfl = &uvm.page_free[uvm_page_get_freelist(pg)];
     90 	pgb = pgfl->pgfl_buckets[uvm_page_get_bucket(pg)];
     91 
     92 #ifdef UVMDEBUG
     93 	struct vm_page *tp;
     94 	LIST_FOREACH(tp, &pgb->pgb_colors[VM_PGCOLOR(pg)], pageq.list) {
     95 		if (tp == pg)
     96 			break;
     97 	}
     98 	if (tp == NULL)
     99 		panic("uvm_pglistalloc: page not on freelist");
    100 #endif
    101 	LIST_REMOVE(pg, pageq.list);
    102 	pgb->pgb_nfree--;
    103     	CPU_COUNT(CPU_COUNT_FREEPAGES, -1);
    104 	pg->flags = PG_CLEAN;
    105 	pg->uobject = NULL;
    106 	pg->uanon = NULL;
    107 	TAILQ_INSERT_TAIL(rlist, pg, pageq.queue);
    108 	STAT_INCR(uvm_pglistalloc_npages);
    109 }
    110 
    111 static int
    112 uvm_pglistalloc_c_ps(uvm_physseg_t psi, int num, paddr_t low, paddr_t high,
    113     paddr_t alignment, paddr_t boundary, struct pglist *rlist)
    114 {
    115 	signed int candidate, limit, candidateidx, end, idx, skip;
    116 	int pagemask;
    117 	bool second_pass;
    118 #ifdef DEBUG
    119 	paddr_t idxpa, lastidxpa;
    120 	paddr_t cidx = 0;	/* XXX: GCC */
    121 #endif
    122 #ifdef PGALLOC_VERBOSE
    123 	printf("pgalloc: contig %d pgs from psi %d\n", num, psi);
    124 #endif
    125 
    126 	low = atop(low);
    127 	high = atop(high);
    128 
    129 	/*
    130 	 * Make sure that physseg falls within with range to be allocated from.
    131 	 */
    132 	if (high <= uvm_physseg_get_avail_start(psi) ||
    133 	    low >= uvm_physseg_get_avail_end(psi))
    134 		return 0;
    135 
    136 	/*
    137 	 * We start our search at the just after where the last allocation
    138 	 * succeeded.
    139 	 */
    140 	alignment = atop(alignment);
    141 	candidate = roundup2(uimax(low, uvm_physseg_get_avail_start(psi) +
    142 		uvm_physseg_get_start_hint(psi)), alignment);
    143 	limit = uimin(high, uvm_physseg_get_avail_end(psi));
    144 	pagemask = ~((boundary >> PAGE_SHIFT) - 1);
    145 	skip = 0;
    146 	second_pass = false;
    147 
    148 	for (;;) {
    149 		bool ok = true;
    150 		signed int cnt;
    151 
    152 		if (candidate + num > limit) {
    153 			if (uvm_physseg_get_start_hint(psi) == 0 || second_pass) {
    154 				/*
    155 				 * We've run past the allowable range.
    156 				 */
    157 				return 0; /* FAIL = 0 pages*/
    158 			}
    159 			/*
    160 			 * We've wrapped around the end of this segment
    161 			 * so restart at the beginning but now our limit
    162 			 * is were we started.
    163 			 */
    164 			second_pass = true;
    165 			candidate = roundup2(uimax(low, uvm_physseg_get_avail_start(psi)), alignment);
    166 			limit = uimin(limit, uvm_physseg_get_avail_start(psi) +
    167 			    uvm_physseg_get_start_hint(psi));
    168 			skip = 0;
    169 			continue;
    170 		}
    171 		if (boundary != 0 &&
    172 		    ((candidate ^ (candidate + num - 1)) & pagemask) != 0) {
    173 			/*
    174 			 * Region crosses boundary. Jump to the boundary
    175 			 * just crossed and ensure alignment.
    176 			 */
    177 			candidate = (candidate + num - 1) & pagemask;
    178 			candidate = roundup2(candidate, alignment);
    179 			skip = 0;
    180 			continue;
    181 		}
    182 #ifdef DEBUG
    183 		/*
    184 		 * Make sure this is a managed physical page.
    185 		 */
    186 
    187 		if (uvm_physseg_find(candidate, &cidx) != psi)
    188 			panic("pgalloc contig: botch1");
    189 		if (cidx != candidate - uvm_physseg_get_start(psi))
    190 			panic("pgalloc contig: botch2");
    191 		if (uvm_physseg_find(candidate + num - 1, &cidx) != psi)
    192 			panic("pgalloc contig: botch3");
    193 		if (cidx != candidate - uvm_physseg_get_start(psi) + num - 1)
    194 			panic("pgalloc contig: botch4");
    195 #endif
    196 		candidateidx = candidate - uvm_physseg_get_start(psi);
    197 		end = candidateidx + num;
    198 
    199 		/*
    200 		 * Found a suitable starting page.  See if the range is free.
    201 		 */
    202 #ifdef PGALLOC_VERBOSE
    203 		printf("%s: psi=%d candidate=%#x end=%#x skip=%#x, align=%#"PRIxPADDR,
    204 		    __func__, psi, candidateidx, end, skip, alignment);
    205 #endif
    206 		/*
    207 		 * We start at the end and work backwards since if we find a
    208 		 * non-free page, it makes no sense to continue.
    209 		 *
    210 		 * But on the plus size we have "vetted" some number of free
    211 		 * pages.  If this iteration fails, we may be able to skip
    212 		 * testing most of those pages again in the next pass.
    213 		 */
    214 		for (idx = end - 1; idx >= candidateidx + skip; idx--) {
    215 			if (VM_PAGE_IS_FREE(uvm_physseg_get_pg(psi, idx)) == 0) {
    216 				ok = false;
    217 				break;
    218 			}
    219 
    220 #ifdef DEBUG
    221 			if (idx > candidateidx) {
    222 				idxpa = VM_PAGE_TO_PHYS(uvm_physseg_get_pg(psi, idx));
    223 				lastidxpa = VM_PAGE_TO_PHYS(uvm_physseg_get_pg(psi, idx - 1));
    224 				if ((lastidxpa + PAGE_SIZE) != idxpa) {
    225 					/*
    226 					 * Region not contiguous.
    227 					 */
    228 					panic("pgalloc contig: botch5");
    229 				}
    230 				if (boundary != 0 &&
    231 				    ((lastidxpa ^ idxpa) & ~(boundary - 1))
    232 				    != 0) {
    233 					/*
    234 					 * Region crosses boundary.
    235 					 */
    236 					panic("pgalloc contig: botch6");
    237 				}
    238 			}
    239 #endif
    240 		}
    241 
    242 		if (ok) {
    243 			while (skip-- > 0) {
    244 				KDASSERT(VM_PAGE_IS_FREE(uvm_physseg_get_pg(psi, candidateidx + skip)));
    245 			}
    246 #ifdef PGALLOC_VERBOSE
    247 			printf(": ok\n");
    248 #endif
    249 			break;
    250 		}
    251 
    252 #ifdef PGALLOC_VERBOSE
    253 		printf(": non-free at %#x\n", idx - candidateidx);
    254 #endif
    255 		/*
    256 		 * count the number of pages we can advance
    257 		 * since we know they aren't all free.
    258 		 */
    259 		cnt = idx + 1 - candidateidx;
    260 		/*
    261 		 * now round up that to the needed alignment.
    262 		 */
    263 		cnt = roundup2(cnt, alignment);
    264 		/*
    265 		 * The number of pages we can skip checking
    266 		 * (might be 0 if cnt > num).
    267 		 */
    268 		skip = uimax(num - cnt, 0);
    269 		candidate += cnt;
    270 	}
    271 
    272 	/*
    273 	 * we have a chunk of memory that conforms to the requested constraints.
    274 	 */
    275 	for (idx = candidateidx; idx < end; idx++)
    276 		uvm_pglist_add(uvm_physseg_get_pg(psi, idx), rlist);
    277 
    278 	/*
    279 	 * the next time we need to search this segment, start after this
    280 	 * chunk of pages we just allocated.
    281 	 */
    282 	uvm_physseg_set_start_hint(psi, candidate + num -
    283 	    uvm_physseg_get_avail_start(psi));
    284 	KASSERTMSG(uvm_physseg_get_start_hint(psi) <=
    285 	    uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi),
    286 	    "%x %u (%#x) <= %#"PRIxPADDR" - %#"PRIxPADDR" (%#"PRIxPADDR")",
    287 	    candidate + num,
    288 	    uvm_physseg_get_start_hint(psi), uvm_physseg_get_start_hint(psi),
    289 	    uvm_physseg_get_avail_end(psi), uvm_physseg_get_avail_start(psi),
    290 	    uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi));
    291 
    292 #ifdef PGALLOC_VERBOSE
    293 	printf("got %d pgs\n", num);
    294 #endif
    295 	return num; /* number of pages allocated */
    296 }
    297 
    298 static int
    299 uvm_pglistalloc_contig_aggressive(int num, paddr_t low, paddr_t high,
    300     paddr_t alignment, paddr_t boundary, struct pglist *rlist)
    301 {
    302 	struct vm_page *pg;
    303 	struct pglist tmp;
    304 	paddr_t pa, off, spa, amask, bmask, rlo, rhi;
    305 	uvm_physseg_t upm;
    306 	int error, i, run, acnt;
    307 
    308 	/*
    309 	 * Allocate pages the normal way and for each new page, check if
    310 	 * the page completes a range satisfying the request.
    311 	 * The pagedaemon will evict pages as we go and we are very likely
    312 	 * to get compatible pages eventually.
    313 	 */
    314 
    315 	error = ENOMEM;
    316 	TAILQ_INIT(&tmp);
    317 	acnt = atop(alignment);
    318 	amask = ~(alignment - 1);
    319 	bmask = ~(boundary - 1);
    320 	KASSERT(bmask <= amask);
    321 	mutex_enter(&uvm_pglistalloc_contig_lock);
    322 	while (uvm_reclaimable()) {
    323 		pg = uvm_pagealloc(NULL, 0, NULL, 0);
    324 		if (pg == NULL) {
    325 			uvm_wait("pglac2");
    326 			continue;
    327 		}
    328 		pg->flags |= PG_PGLCA;
    329 		TAILQ_INSERT_HEAD(&tmp, pg, pageq.queue);
    330 
    331 		pa = VM_PAGE_TO_PHYS(pg);
    332 		if (pa < low || pa >= high) {
    333 			continue;
    334 		}
    335 
    336 		upm = uvm_physseg_find(atop(pa), &off);
    337 		KASSERT(uvm_physseg_valid_p(upm));
    338 
    339 		spa = pa & amask;
    340 
    341 		/*
    342 		 * Look backward for at most num - 1 pages, back to
    343 		 * the highest of:
    344 		 *  - the first page in the physseg
    345 		 *  - the specified low address
    346 		 *  - num-1 pages before the one we just allocated
    347 		 *  - the start of the boundary range containing pa
    348 		 * all rounded up to alignment.
    349 		 */
    350 
    351 		rlo = roundup2(ptoa(uvm_physseg_get_avail_start(upm)), alignment);
    352 		rlo = MAX(rlo, roundup2(low, alignment));
    353 		rlo = MAX(rlo, roundup2(pa - ptoa(num - 1), alignment));
    354 		if (boundary) {
    355 			rlo = MAX(rlo, spa & bmask);
    356 		}
    357 
    358 		/*
    359 		 * Look forward as far as the lowest of:
    360 		 *  - the last page of the physseg
    361 		 *  - the specified high address
    362 		 *  - the boundary after pa
    363 		 */
    364 
    365 		rhi = ptoa(uvm_physseg_get_avail_end(upm));
    366 		rhi = MIN(rhi, high);
    367 		if (boundary) {
    368 			rhi = MIN(rhi, rounddown2(pa, boundary) + boundary);
    369 		}
    370 
    371 		/*
    372 		 * Make sure our range to consider is big enough.
    373 		 */
    374 
    375 		if (rhi - rlo < ptoa(num)) {
    376 			continue;
    377 		}
    378 
    379 		run = 0;
    380 		while (spa > rlo) {
    381 
    382 			/*
    383 			 * Examine pages before spa in groups of acnt.
    384 			 * If all the pages in a group are marked then add
    385 			 * these pages to the run.
    386 			 */
    387 
    388 			for (i = 0; i < acnt; i++) {
    389 				pg = PHYS_TO_VM_PAGE(spa - alignment + ptoa(i));
    390 				if ((pg->flags & PG_PGLCA) == 0) {
    391 					break;
    392 				}
    393 			}
    394 			if (i < acnt) {
    395 				break;
    396 			}
    397 			spa -= alignment;
    398 			run += acnt;
    399 		}
    400 
    401 		/*
    402 		 * Look forward for any remaining pages.
    403 		 */
    404 
    405 		if (spa + ptoa(num) > rhi) {
    406 			continue;
    407 		}
    408 		for (; run < num; run++) {
    409 			pg = PHYS_TO_VM_PAGE(spa + ptoa(run));
    410 			if ((pg->flags & PG_PGLCA) == 0) {
    411 				break;
    412 			}
    413 		}
    414 		if (run < num) {
    415 			continue;
    416 		}
    417 
    418 		/*
    419 		 * We found a match.  Move these pages from the tmp list to
    420 		 * the caller's list.
    421 		 */
    422 
    423 		for (i = 0; i < num; i++) {
    424 			pg = PHYS_TO_VM_PAGE(spa + ptoa(i));
    425 			TAILQ_REMOVE(&tmp, pg, pageq.queue);
    426 			pg->flags &= ~PG_PGLCA;
    427 			TAILQ_INSERT_TAIL(rlist, pg, pageq.queue);
    428 			STAT_INCR(uvm_pglistalloc_npages);
    429 		}
    430 
    431 		error = 0;
    432 		break;
    433 	}
    434 
    435 	/*
    436 	 * Free all the pages that we didn't need.
    437 	 */
    438 
    439 	while (!TAILQ_EMPTY(&tmp)) {
    440 		pg = TAILQ_FIRST(&tmp);
    441 		TAILQ_REMOVE(&tmp, pg, pageq.queue);
    442 		pg->flags &= ~PG_PGLCA;
    443 		uvm_pagefree(pg);
    444 	}
    445 	mutex_exit(&uvm_pglistalloc_contig_lock);
    446 	return error;
    447 }
    448 
    449 static int
    450 uvm_pglistalloc_contig(int num, paddr_t low, paddr_t high, paddr_t alignment,
    451     paddr_t boundary, struct pglist *rlist, int waitok)
    452 {
    453 	int fl;
    454 	int error;
    455 	uvm_physseg_t psi;
    456 
    457 	/* Default to "lose". */
    458 	error = ENOMEM;
    459 
    460 	/*
    461 	 * Block all memory allocation and lock the free list.
    462 	 */
    463 	uvm_pgfl_lock();
    464 
    465 	/* Are there even any free pages? */
    466 	if (uvm_availmem(false) <=
    467 	    (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    468 		goto out;
    469 
    470 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    471 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    472 		for (psi = uvm_physseg_get_last(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_prev(psi))
    473 #else
    474 		for (psi = uvm_physseg_get_first(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_next(psi))
    475 #endif
    476 		{
    477 			if (uvm_physseg_get_free_list(psi) != fl)
    478 				continue;
    479 
    480 			num -= uvm_pglistalloc_c_ps(psi, num, low, high,
    481 						    alignment, boundary, rlist);
    482 			if (num == 0) {
    483 #ifdef PGALLOC_VERBOSE
    484 				printf("pgalloc: %"PRIxMAX"-%"PRIxMAX"\n",
    485 				       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    486 				       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    487 #endif
    488 				error = 0;
    489 				goto out;
    490 			}
    491 		}
    492 	}
    493 
    494 out:
    495 	uvm_pgfl_unlock();
    496 
    497 	/*
    498 	 * If that didn't work, try the more aggressive approach.
    499 	 */
    500 
    501 	if (error) {
    502 		if (waitok) {
    503 			error = uvm_pglistalloc_contig_aggressive(num, low, high,
    504 			    alignment, boundary, rlist);
    505 		} else {
    506 			uvm_pglistfree(rlist);
    507 			uvm_kick_pdaemon();
    508 		}
    509 	}
    510 	return error;
    511 }
    512 
    513 static int
    514 uvm_pglistalloc_s_ps(uvm_physseg_t psi, int num, paddr_t low, paddr_t high,
    515     struct pglist *rlist)
    516 {
    517 	int todo, limit, candidate;
    518 	struct vm_page *pg;
    519 	bool second_pass;
    520 #ifdef PGALLOC_VERBOSE
    521 	printf("pgalloc: simple %d pgs from psi %d\n", num, psi);
    522 #endif
    523 
    524 	KASSERT(uvm_physseg_get_start(psi) <= uvm_physseg_get_avail_start(psi));
    525 	KASSERT(uvm_physseg_get_start(psi) <= uvm_physseg_get_avail_end(psi));
    526 	KASSERT(uvm_physseg_get_avail_start(psi) <= uvm_physseg_get_end(psi));
    527 	KASSERT(uvm_physseg_get_avail_end(psi) <= uvm_physseg_get_end(psi));
    528 
    529 	low = atop(low);
    530 	high = atop(high);
    531 
    532 	/*
    533 	 * Make sure that physseg falls within with range to be allocated from.
    534 	 */
    535 	if (high <= uvm_physseg_get_avail_start(psi) ||
    536 	    low >= uvm_physseg_get_avail_end(psi))
    537 		return 0;
    538 
    539 	todo = num;
    540 	candidate = uimax(low, uvm_physseg_get_avail_start(psi) +
    541 	    uvm_physseg_get_start_hint(psi));
    542 	limit = uimin(high, uvm_physseg_get_avail_end(psi));
    543 	pg = uvm_physseg_get_pg(psi, candidate - uvm_physseg_get_start(psi));
    544 	second_pass = false;
    545 
    546 again:
    547 	for (;; candidate++, pg++) {
    548 		if (candidate >= limit) {
    549 			if (uvm_physseg_get_start_hint(psi) == 0 || second_pass) {
    550 				candidate = limit - 1;
    551 				break;
    552 			}
    553 			second_pass = true;
    554 			candidate = uimax(low, uvm_physseg_get_avail_start(psi));
    555 			limit = uimin(limit, uvm_physseg_get_avail_start(psi) +
    556 			    uvm_physseg_get_start_hint(psi));
    557 			pg = uvm_physseg_get_pg(psi, candidate - uvm_physseg_get_start(psi));
    558 			goto again;
    559 		}
    560 #if defined(DEBUG)
    561 		{
    562 			paddr_t cidx = 0;
    563 			const uvm_physseg_t bank = uvm_physseg_find(candidate, &cidx);
    564 			KDASSERTMSG(bank == psi,
    565 			    "uvm_physseg_find(%#x) (%"PRIxPHYSSEG ") != psi %"PRIxPHYSSEG,
    566 			     candidate, bank, psi);
    567 			KDASSERTMSG(cidx == candidate - uvm_physseg_get_start(psi),
    568 			    "uvm_physseg_find(%#x): %#"PRIxPADDR" != off %"PRIxPADDR,
    569 			     candidate, cidx, candidate - uvm_physseg_get_start(psi));
    570 		}
    571 #endif
    572 		if (VM_PAGE_IS_FREE(pg) == 0)
    573 			continue;
    574 
    575 		uvm_pglist_add(pg, rlist);
    576 		if (--todo == 0) {
    577 			break;
    578 		}
    579 	}
    580 
    581 	/*
    582 	 * The next time we need to search this segment,
    583 	 * start just after the pages we just allocated.
    584 	 */
    585 	uvm_physseg_set_start_hint(psi, candidate + 1 - uvm_physseg_get_avail_start(psi));
    586 	KASSERTMSG(uvm_physseg_get_start_hint(psi) <= uvm_physseg_get_avail_end(psi) -
    587 	    uvm_physseg_get_avail_start(psi),
    588 	    "%#x %u (%#x) <= %#"PRIxPADDR" - %#"PRIxPADDR" (%#"PRIxPADDR")",
    589 	    candidate + 1,
    590 	    uvm_physseg_get_start_hint(psi),
    591 	    uvm_physseg_get_start_hint(psi),
    592 	    uvm_physseg_get_avail_end(psi),
    593 	    uvm_physseg_get_avail_start(psi),
    594 	    uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi));
    595 
    596 #ifdef PGALLOC_VERBOSE
    597 	printf("got %d pgs\n", num - todo);
    598 #endif
    599 	return (num - todo); /* number of pages allocated */
    600 }
    601 
    602 static int
    603 uvm_pglistalloc_simple(int num, paddr_t low, paddr_t high,
    604     struct pglist *rlist, int waitok)
    605 {
    606 	int fl, error;
    607 	uvm_physseg_t psi;
    608 	int count = 0;
    609 
    610 	/* Default to "lose". */
    611 	error = ENOMEM;
    612 
    613 again:
    614 	/*
    615 	 * Block all memory allocation and lock the free list.
    616 	 */
    617 	uvm_pgfl_lock();
    618 	count++;
    619 
    620 	/* Are there even any free pages? */
    621 	if (uvm_availmem(false) <=
    622 	    (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    623 		goto out;
    624 
    625 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    626 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    627 		for (psi = uvm_physseg_get_last(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_prev(psi))
    628 #else
    629 		for (psi = uvm_physseg_get_first(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_next(psi))
    630 #endif
    631 		{
    632 			if (uvm_physseg_get_free_list(psi) != fl)
    633 				continue;
    634 
    635 			num -= uvm_pglistalloc_s_ps(psi, num, low, high, rlist);
    636 			if (num == 0) {
    637 				error = 0;
    638 				goto out;
    639 			}
    640 		}
    641 
    642 	}
    643 
    644 out:
    645 	/*
    646 	 * check to see if we need to generate some free pages waking
    647 	 * the pagedaemon.
    648 	 */
    649 
    650 	uvm_pgfl_unlock();
    651 	uvm_kick_pdaemon();
    652 
    653 	if (error) {
    654 		if (waitok) {
    655 			uvm_wait("pglalloc");
    656 			goto again;
    657 		} else
    658 			uvm_pglistfree(rlist);
    659 	}
    660 #ifdef PGALLOC_VERBOSE
    661 	if (!error)
    662 		printf("pgalloc: %"PRIxMAX"..%"PRIxMAX"\n",
    663 		       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    664 		       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    665 #endif
    666 	return (error);
    667 }
    668 
    669 int
    670 uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment,
    671     paddr_t boundary, struct pglist *rlist, int nsegs, int waitok)
    672 {
    673 	int num, res;
    674 
    675 	KASSERT(!cpu_intr_p());
    676 	KASSERT(!cpu_softintr_p());
    677 	KASSERT((alignment & (alignment - 1)) == 0);
    678 	KASSERT((boundary & (boundary - 1)) == 0);
    679 
    680 	/*
    681 	 * Our allocations are always page granularity, so our alignment
    682 	 * must be, too.
    683 	 */
    684 	if (alignment < PAGE_SIZE)
    685 		alignment = PAGE_SIZE;
    686 	if (boundary != 0 && boundary < size)
    687 		return (EINVAL);
    688 	num = atop(round_page(size));
    689 	low = roundup2(low, alignment);
    690 
    691 	TAILQ_INIT(rlist);
    692 
    693 	/*
    694 	 * Turn off the caching of free pages - we need everything to be on
    695 	 * the global freelists.
    696 	 */
    697 	uvm_pgflcache_pause();
    698 
    699 	if (nsegs < num || alignment != PAGE_SIZE || boundary != 0)
    700 		res = uvm_pglistalloc_contig(num, low, high, alignment,
    701 					     boundary, rlist, waitok);
    702 	else
    703 		res = uvm_pglistalloc_simple(num, low, high, rlist, waitok);
    704 
    705 	uvm_pgflcache_resume();
    706 
    707 	return (res);
    708 }
    709 
    710 /*
    711  * uvm_pglistfree: free a list of pages
    712  *
    713  * => pages should already be unmapped
    714  */
    715 
    716 void
    717 uvm_pglistfree(struct pglist *list)
    718 {
    719 	struct vm_page *pg;
    720 
    721 	KASSERT(!cpu_intr_p());
    722 	KASSERT(!cpu_softintr_p());
    723 
    724 	while ((pg = TAILQ_FIRST(list)) != NULL) {
    725 		TAILQ_REMOVE(list, pg, pageq.queue);
    726 		uvm_pagefree(pg);
    727 		STAT_DECR(uvm_pglistalloc_npages);
    728 	}
    729 }
    730 
    731 void
    732 uvm_pglistalloc_init(void)
    733 {
    734 
    735 	mutex_init(&uvm_pglistalloc_contig_lock, MUTEX_DEFAULT, IPL_NONE);
    736 }
    737