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