Home | History | Annotate | Line # | Download | only in uvm
uvm_pglist.c revision 1.70.14.1
      1  1.70.14.1  pgoyette /*	$NetBSD: uvm_pglist.c,v 1.70.14.1 2018/09/06 06:56:48 pgoyette Exp $	*/
      2       1.45    nonaka 
      3        1.1       mrg /*-
      4        1.1       mrg  * Copyright (c) 1997 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.15       chs  * NASA Ames Research Center.
     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.70.14.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.70.14.1 2018/09/06 06:56:48 pgoyette Exp $");
     39        1.1       mrg 
     40        1.1       mrg #include <sys/param.h>
     41        1.1       mrg #include <sys/systm.h>
     42        1.1       mrg 
     43        1.1       mrg #include <uvm/uvm.h>
     44       1.36      yamt #include <uvm/uvm_pdpolicy.h>
     45        1.1       mrg 
     46        1.1       mrg #ifdef VM_PAGE_ALLOC_MEMORY_STATS
     47        1.1       mrg #define	STAT_INCR(v)	(v)++
     48        1.1       mrg #define	STAT_DECR(v)	do { \
     49        1.1       mrg 		if ((v) == 0) \
     50        1.1       mrg 			printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
     51        1.1       mrg 		else \
     52        1.1       mrg 			(v)--; \
     53       1.25     perry 	} while (/*CONSTCOND*/ 0)
     54        1.1       mrg u_long	uvm_pglistalloc_npages;
     55        1.1       mrg #else
     56        1.1       mrg #define	STAT_INCR(v)
     57        1.1       mrg #define	STAT_DECR(v)
     58        1.1       mrg #endif
     59        1.1       mrg 
     60        1.1       mrg /*
     61        1.1       mrg  * uvm_pglistalloc: allocate a list of pages
     62        1.1       mrg  *
     63       1.27  drochner  * => allocated pages are placed onto an rlist.  rlist is
     64       1.27  drochner  *    initialized by uvm_pglistalloc.
     65        1.1       mrg  * => returns 0 on success or errno on failure
     66       1.27  drochner  * => implementation allocates a single segment if any constraints are
     67       1.27  drochner  *	imposed by call arguments.
     68        1.1       mrg  * => doesn't take into account clean non-busy pages on inactive list
     69        1.1       mrg  *	that could be used(?)
     70        1.1       mrg  * => params:
     71        1.1       mrg  *	size		the size of the allocation, rounded to page size.
     72        1.1       mrg  *	low		the low address of the allowed allocation range.
     73        1.1       mrg  *	high		the high address of the allowed allocation range.
     74        1.1       mrg  *	alignment	memory must be aligned to this power-of-two boundary.
     75       1.15       chs  *	boundary	no segment in the allocation may cross this
     76        1.1       mrg  *			power-of-two boundary (relative to zero).
     77        1.1       mrg  */
     78        1.1       mrg 
     79       1.20  drochner static void
     80       1.33   thorpej uvm_pglist_add(struct vm_page *pg, struct pglist *rlist)
     81       1.20  drochner {
     82       1.63    martin 	int free_list __unused, color __unused, pgflidx;
     83       1.20  drochner 
     84       1.39        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
     85       1.39        ad 
     86       1.20  drochner #if PGFL_NQUEUES != 2
     87       1.20  drochner #error uvm_pglistalloc needs to be updated
     88       1.20  drochner #endif
     89       1.20  drochner 
     90       1.20  drochner 	free_list = uvm_page_lookup_freelist(pg);
     91       1.20  drochner 	color = VM_PGCOLOR_BUCKET(pg);
     92       1.20  drochner 	pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
     93       1.67  christos #ifdef UVMDEBUG
     94       1.52      matt 	struct vm_page *tp;
     95       1.52      matt 	LIST_FOREACH(tp,
     96       1.52      matt 	    &uvm.page_free[free_list].pgfl_buckets[color].pgfl_queues[pgflidx],
     97       1.52      matt 	    pageq.list) {
     98       1.20  drochner 		if (tp == pg)
     99       1.20  drochner 			break;
    100       1.20  drochner 	}
    101       1.20  drochner 	if (tp == NULL)
    102       1.20  drochner 		panic("uvm_pglistalloc: page not on freelist");
    103       1.20  drochner #endif
    104       1.42        ad 	LIST_REMOVE(pg, pageq.list);	/* global */
    105       1.42        ad 	LIST_REMOVE(pg, listq.list);	/* cpu */
    106       1.20  drochner 	uvmexp.free--;
    107       1.20  drochner 	if (pg->flags & PG_ZERO)
    108       1.20  drochner 		uvmexp.zeropages--;
    109       1.42        ad 	VM_FREE_PAGE_TO_CPU(pg)->pages[pgflidx]--;
    110       1.20  drochner 	pg->flags = PG_CLEAN;
    111       1.20  drochner 	pg->pqflags = 0;
    112       1.20  drochner 	pg->uobject = NULL;
    113       1.20  drochner 	pg->uanon = NULL;
    114       1.42        ad 	TAILQ_INSERT_TAIL(rlist, pg, pageq.queue);
    115       1.20  drochner 	STAT_INCR(uvm_pglistalloc_npages);
    116       1.20  drochner }
    117       1.20  drochner 
    118       1.20  drochner static int
    119       1.68    cherry uvm_pglistalloc_c_ps(uvm_physseg_t psi, int num, paddr_t low, paddr_t high,
    120       1.33   thorpej     paddr_t alignment, paddr_t boundary, struct pglist *rlist)
    121        1.1       mrg {
    122       1.66      matt 	signed int candidate, limit, candidateidx, end, idx, skip;
    123       1.24  drochner 	int pagemask;
    124       1.52      matt 	bool second_pass;
    125       1.24  drochner #ifdef DEBUG
    126       1.22  drochner 	paddr_t idxpa, lastidxpa;
    127       1.68    cherry 	paddr_t cidx = 0;	/* XXX: GCC */
    128       1.22  drochner #endif
    129       1.24  drochner #ifdef PGALLOC_VERBOSE
    130       1.57      matt 	printf("pgalloc: contig %d pgs from psi %zd\n", num, ps - vm_physmem);
    131       1.24  drochner #endif
    132        1.1       mrg 
    133       1.39        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
    134       1.39        ad 
    135       1.52      matt 	low = atop(low);
    136       1.52      matt 	high = atop(high);
    137       1.52      matt 	alignment = atop(alignment);
    138       1.52      matt 
    139       1.52      matt 	/*
    140       1.57      matt 	 * Make sure that physseg falls within with range to be allocated from.
    141       1.57      matt 	 */
    142       1.68    cherry 	if (high <= uvm_physseg_get_avail_start(psi) || low >= uvm_physseg_get_avail_end(psi))
    143       1.57      matt 		return 0;
    144       1.57      matt 
    145       1.57      matt 	/*
    146       1.52      matt 	 * We start our search at the just after where the last allocation
    147       1.52      matt 	 * succeeded.
    148       1.52      matt 	 */
    149  1.70.14.1  pgoyette 	candidate = roundup2(uimax(low, uvm_physseg_get_avail_start(psi) +
    150       1.68    cherry 		uvm_physseg_get_start_hint(psi)), alignment);
    151  1.70.14.1  pgoyette 	limit = uimin(high, uvm_physseg_get_avail_end(psi));
    152       1.24  drochner 	pagemask = ~((boundary >> PAGE_SHIFT) - 1);
    153       1.52      matt 	skip = 0;
    154       1.52      matt 	second_pass = false;
    155       1.12       chs 
    156       1.24  drochner 	for (;;) {
    157       1.52      matt 		bool ok = true;
    158       1.52      matt 		signed int cnt;
    159       1.52      matt 
    160       1.66      matt 		if (candidate + num > limit) {
    161       1.68    cherry 			if (uvm_physseg_get_start_hint(psi) == 0 || second_pass) {
    162       1.52      matt 				/*
    163       1.52      matt 				 * We've run past the allowable range.
    164       1.52      matt 				 */
    165       1.52      matt 				return 0; /* FAIL = 0 pages*/
    166       1.52      matt 			}
    167        1.3       mrg 			/*
    168       1.52      matt 			 * We've wrapped around the end of this segment
    169       1.52      matt 			 * so restart at the beginning but now our limit
    170       1.52      matt 			 * is were we started.
    171        1.3       mrg 			 */
    172       1.52      matt 			second_pass = true;
    173  1.70.14.1  pgoyette 			candidate = roundup2(uimax(low, uvm_physseg_get_avail_start(psi)), alignment);
    174  1.70.14.1  pgoyette 			limit = uimin(limit, uvm_physseg_get_avail_start(psi) +
    175       1.68    cherry 			    uvm_physseg_get_start_hint(psi));
    176       1.52      matt 			skip = 0;
    177       1.52      matt 			continue;
    178        1.3       mrg 		}
    179       1.24  drochner 		if (boundary != 0 &&
    180       1.66      matt 		    ((candidate ^ (candidate + num - 1)) & pagemask) != 0) {
    181       1.24  drochner 			/*
    182       1.24  drochner 			 * Region crosses boundary. Jump to the boundary
    183       1.24  drochner 			 * just crossed and ensure alignment.
    184       1.24  drochner 			 */
    185       1.66      matt 			candidate = (candidate + num - 1) & pagemask;
    186       1.66      matt 			candidate = roundup2(candidate, alignment);
    187       1.52      matt 			skip = 0;
    188       1.24  drochner 			continue;
    189       1.24  drochner 		}
    190       1.22  drochner #ifdef DEBUG
    191        1.3       mrg 		/*
    192        1.3       mrg 		 * Make sure this is a managed physical page.
    193        1.3       mrg 		 */
    194        1.3       mrg 
    195       1.68    cherry 		if (uvm_physseg_find(candidate, &cidx) != psi)
    196       1.22  drochner 			panic("pgalloc contig: botch1");
    197       1.68    cherry 		if (cidx != candidate - uvm_physseg_get_start(psi))
    198       1.22  drochner 			panic("pgalloc contig: botch2");
    199       1.68    cherry 		if (uvm_physseg_find(candidate + num - 1, &cidx) != psi)
    200       1.22  drochner 			panic("pgalloc contig: botch3");
    201       1.68    cherry 		if (cidx != candidate - uvm_physseg_get_start(psi) + num - 1)
    202       1.31  junyoung 			panic("pgalloc contig: botch4");
    203       1.22  drochner #endif
    204       1.68    cherry 		candidateidx = candidate - uvm_physseg_get_start(psi);
    205       1.66      matt 		end = candidateidx + num;
    206        1.3       mrg 
    207        1.3       mrg 		/*
    208       1.24  drochner 		 * Found a suitable starting page.  See if the range is free.
    209        1.3       mrg 		 */
    210       1.52      matt #ifdef PGALLOC_VERBOSE
    211       1.66      matt 		printf("%s: ps=%p candidate=%#x end=%#x skip=%#x, align=%#"PRIxPADDR,
    212       1.66      matt 		    __func__, ps, candidateidx, end, skip, alignment);
    213       1.52      matt #endif
    214       1.52      matt 		/*
    215       1.52      matt 		 * We start at the end and work backwards since if we find a
    216       1.52      matt 		 * non-free page, it makes no sense to continue.
    217       1.52      matt 		 *
    218       1.52      matt 		 * But on the plus size we have "vetted" some number of free
    219       1.52      matt 		 * pages.  If this iteration fails, we may be able to skip
    220       1.52      matt 		 * testing most of those pages again in the next pass.
    221       1.52      matt 		 */
    222       1.66      matt 		for (idx = end - 1; idx >= candidateidx + skip; idx--) {
    223       1.68    cherry 			if (VM_PAGE_IS_FREE(uvm_physseg_get_pg(psi, idx)) == 0) {
    224       1.52      matt 				ok = false;
    225        1.3       mrg 				break;
    226       1.52      matt 			}
    227       1.24  drochner 
    228       1.24  drochner #ifdef DEBUG
    229       1.66      matt 			if (idx > candidateidx) {
    230       1.68    cherry 				idxpa = VM_PAGE_TO_PHYS(uvm_physseg_get_pg(psi, idx));
    231       1.68    cherry 				lastidxpa = VM_PAGE_TO_PHYS(uvm_physseg_get_pg(psi, idx - 1));
    232       1.12       chs 				if ((lastidxpa + PAGE_SIZE) != idxpa) {
    233        1.3       mrg 					/*
    234        1.3       mrg 					 * Region not contiguous.
    235        1.3       mrg 					 */
    236       1.22  drochner 					panic("pgalloc contig: botch5");
    237        1.3       mrg 				}
    238        1.3       mrg 				if (boundary != 0 &&
    239       1.24  drochner 				    ((lastidxpa ^ idxpa) & ~(boundary - 1))
    240       1.24  drochner 				    != 0) {
    241        1.3       mrg 					/*
    242        1.3       mrg 					 * Region crosses boundary.
    243        1.3       mrg 					 */
    244       1.24  drochner 					panic("pgalloc contig: botch6");
    245        1.3       mrg 				}
    246        1.3       mrg 			}
    247       1.24  drochner #endif
    248        1.3       mrg 		}
    249       1.52      matt 
    250       1.52      matt 		if (ok) {
    251       1.52      matt 			while (skip-- > 0) {
    252       1.68    cherry 				KDASSERT(VM_PAGE_IS_FREE(uvm_physseg_get_pg(psi, candidateidx + skip)));
    253       1.52      matt 			}
    254       1.52      matt #ifdef PGALLOC_VERBOSE
    255       1.52      matt 			printf(": ok\n");
    256       1.52      matt #endif
    257        1.3       mrg 			break;
    258       1.52      matt 		}
    259       1.24  drochner 
    260       1.52      matt #ifdef PGALLOC_VERBOSE
    261       1.66      matt 		printf(": non-free at %#x\n", idx - candidateidx);
    262       1.52      matt #endif
    263       1.52      matt 		/*
    264       1.52      matt 		 * count the number of pages we can advance
    265       1.52      matt 		 * since we know they aren't all free.
    266       1.52      matt 		 */
    267       1.66      matt 		cnt = idx + 1 - candidateidx;
    268       1.52      matt 		/*
    269       1.52      matt 		 * now round up that to the needed alignment.
    270       1.52      matt 		 */
    271       1.52      matt 		cnt = roundup2(cnt, alignment);
    272       1.52      matt 		/*
    273       1.52      matt 		 * The number of pages we can skip checking
    274       1.52      matt 		 * (might be 0 if cnt > num).
    275       1.52      matt 		 */
    276  1.70.14.1  pgoyette 		skip = uimax(num - cnt, 0);
    277       1.66      matt 		candidate += cnt;
    278        1.1       mrg 	}
    279        1.1       mrg 
    280        1.3       mrg 	/*
    281        1.3       mrg 	 * we have a chunk of memory that conforms to the requested constraints.
    282        1.3       mrg 	 */
    283       1.68    cherry 	for (idx = candidateidx; idx < end; idx++)
    284       1.68    cherry 		uvm_pglist_add(uvm_physseg_get_pg(psi, idx), rlist);
    285       1.52      matt 
    286       1.52      matt 	/*
    287       1.52      matt 	 * the next time we need to search this segment, start after this
    288       1.52      matt 	 * chunk of pages we just allocated.
    289       1.52      matt 	 */
    290       1.68    cherry 	uvm_physseg_set_start_hint(psi, candidate + num -
    291       1.68    cherry 	    uvm_physseg_get_avail_start(psi));
    292       1.68    cherry 	KASSERTMSG(uvm_physseg_get_start_hint(psi) <=
    293       1.68    cherry 	    uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi),
    294       1.62       jym 	    "%x %u (%#x) <= %#"PRIxPADDR" - %#"PRIxPADDR" (%#"PRIxPADDR")",
    295       1.66      matt 	    candidate + num,
    296       1.68    cherry 	    uvm_physseg_get_start_hint(psi), uvm_physseg_get_start_hint(psi),
    297       1.68    cherry 	    uvm_physseg_get_avail_end(psi), uvm_physseg_get_avail_start(psi),
    298       1.68    cherry 	    uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi));
    299       1.24  drochner 
    300       1.24  drochner #ifdef PGALLOC_VERBOSE
    301       1.24  drochner 	printf("got %d pgs\n", num);
    302       1.24  drochner #endif
    303       1.52      matt 	return num; /* number of pages allocated */
    304       1.22  drochner }
    305       1.22  drochner 
    306       1.22  drochner static int
    307       1.33   thorpej uvm_pglistalloc_contig(int num, paddr_t low, paddr_t high, paddr_t alignment,
    308       1.33   thorpej     paddr_t boundary, struct pglist *rlist)
    309       1.22  drochner {
    310       1.68    cherry 	int fl;
    311       1.38        ad 	int error;
    312       1.22  drochner 
    313       1.68    cherry 	uvm_physseg_t psi;
    314       1.22  drochner 	/* Default to "lose". */
    315       1.22  drochner 	error = ENOMEM;
    316       1.22  drochner 
    317       1.22  drochner 	/*
    318       1.22  drochner 	 * Block all memory allocation and lock the free list.
    319       1.22  drochner 	 */
    320       1.38        ad 	mutex_spin_enter(&uvm_fpageqlock);
    321       1.22  drochner 
    322       1.22  drochner 	/* Are there even any free pages? */
    323       1.22  drochner 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    324       1.22  drochner 		goto out;
    325       1.22  drochner 
    326       1.22  drochner 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    327       1.22  drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    328       1.68    cherry 		for (psi = uvm_physseg_get_last(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_prev(psi))
    329       1.22  drochner #else
    330       1.68    cherry 		for (psi = uvm_physseg_get_first(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_next(psi))
    331       1.22  drochner #endif
    332       1.22  drochner 		{
    333       1.68    cherry 			if (uvm_physseg_get_free_list(psi) != fl)
    334       1.22  drochner 				continue;
    335       1.22  drochner 
    336       1.68    cherry 			num -= uvm_pglistalloc_c_ps(psi, num, low, high,
    337       1.24  drochner 						    alignment, boundary, rlist);
    338       1.24  drochner 			if (num == 0) {
    339       1.24  drochner #ifdef PGALLOC_VERBOSE
    340       1.44   reinoud 				printf("pgalloc: %"PRIxMAX"-%"PRIxMAX"\n",
    341       1.44   reinoud 				       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    342       1.44   reinoud 				       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    343       1.22  drochner #endif
    344       1.22  drochner 				error = 0;
    345       1.22  drochner 				goto out;
    346       1.22  drochner 			}
    347       1.22  drochner 		}
    348       1.22  drochner 	}
    349       1.20  drochner 
    350       1.20  drochner out:
    351       1.20  drochner 	/*
    352       1.20  drochner 	 * check to see if we need to generate some free pages waking
    353       1.20  drochner 	 * the pagedaemon.
    354       1.20  drochner 	 */
    355       1.20  drochner 
    356       1.36      yamt 	uvm_kick_pdaemon();
    357       1.38        ad 	mutex_spin_exit(&uvm_fpageqlock);
    358       1.20  drochner 	return (error);
    359       1.20  drochner }
    360       1.20  drochner 
    361       1.24  drochner static int
    362       1.68    cherry uvm_pglistalloc_s_ps(uvm_physseg_t psi, int num, paddr_t low, paddr_t high,
    363       1.33   thorpej     struct pglist *rlist)
    364       1.22  drochner {
    365       1.66      matt 	int todo, limit, candidate;
    366       1.22  drochner 	struct vm_page *pg;
    367       1.52      matt 	bool second_pass;
    368       1.24  drochner #ifdef PGALLOC_VERBOSE
    369       1.68    cherry 	printf("pgalloc: simple %d pgs from psi %zd\n", num, psi);
    370       1.24  drochner #endif
    371       1.22  drochner 
    372       1.39        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
    373       1.68    cherry 	KASSERT(uvm_physseg_get_start(psi) <= uvm_physseg_get_avail_start(psi));
    374       1.68    cherry 	KASSERT(uvm_physseg_get_start(psi) <= uvm_physseg_get_avail_end(psi));
    375       1.68    cherry 	KASSERT(uvm_physseg_get_avail_start(psi) <= uvm_physseg_get_end(psi));
    376       1.68    cherry 	KASSERT(uvm_physseg_get_avail_end(psi) <= uvm_physseg_get_end(psi));
    377       1.39        ad 
    378       1.52      matt 	low = atop(low);
    379       1.52      matt 	high = atop(high);
    380       1.24  drochner 	todo = num;
    381  1.70.14.1  pgoyette 	candidate = uimax(low, uvm_physseg_get_avail_start(psi) +
    382       1.68    cherry 	    uvm_physseg_get_start_hint(psi));
    383  1.70.14.1  pgoyette 	limit = uimin(high, uvm_physseg_get_avail_end(psi));
    384       1.68    cherry 	pg = uvm_physseg_get_pg(psi, candidate - uvm_physseg_get_start(psi));
    385       1.52      matt 	second_pass = false;
    386       1.52      matt 
    387       1.57      matt 	/*
    388       1.57      matt 	 * Make sure that physseg falls within with range to be allocated from.
    389       1.57      matt 	 */
    390       1.68    cherry 	if (high <= uvm_physseg_get_avail_start(psi) ||
    391       1.68    cherry 	    low >= uvm_physseg_get_avail_end(psi))
    392       1.57      matt 		return 0;
    393       1.57      matt 
    394       1.60     enami again:
    395       1.66      matt 	for (;; candidate++, pg++) {
    396       1.66      matt 		if (candidate >= limit) {
    397       1.68    cherry 			if (uvm_physseg_get_start_hint(psi) == 0 || second_pass) {
    398       1.66      matt 				candidate = limit - 1;
    399       1.52      matt 				break;
    400       1.57      matt 			}
    401       1.52      matt 			second_pass = true;
    402  1.70.14.1  pgoyette 			candidate = uimax(low, uvm_physseg_get_avail_start(psi));
    403  1.70.14.1  pgoyette 			limit = uimin(limit, uvm_physseg_get_avail_start(psi) +
    404       1.68    cherry 			    uvm_physseg_get_start_hint(psi));
    405       1.68    cherry 			pg = uvm_physseg_get_pg(psi, candidate - uvm_physseg_get_start(psi));
    406       1.60     enami 			goto again;
    407       1.52      matt 		}
    408       1.58      matt #if defined(DEBUG)
    409       1.54      matt 		{
    410       1.68    cherry 			paddr_t cidx = 0;
    411       1.68    cherry 			const uvm_physseg_t bank = uvm_physseg_find(candidate, &cidx);
    412       1.68    cherry 			KDASSERTMSG(bank == psi,
    413       1.70     skrll 			    "uvm_physseg_find(%#x) (%"PRIxPHYSSEG ") != psi %"PRIxPHYSSEG,
    414       1.68    cherry 			     candidate, bank, psi);
    415       1.68    cherry 			KDASSERTMSG(cidx == candidate - uvm_physseg_get_start(psi),
    416       1.68    cherry 			    "uvm_physseg_find(%#x): %#"PRIxPADDR" != off %"PRIxPADDR,
    417       1.68    cherry 			     candidate, cidx, candidate - uvm_physseg_get_start(psi));
    418       1.54      matt 		}
    419       1.22  drochner #endif
    420       1.22  drochner 		if (VM_PAGE_IS_FREE(pg) == 0)
    421       1.22  drochner 			continue;
    422       1.22  drochner 
    423       1.22  drochner 		uvm_pglist_add(pg, rlist);
    424       1.52      matt 		if (--todo == 0) {
    425       1.22  drochner 			break;
    426       1.52      matt 		}
    427       1.22  drochner 	}
    428       1.24  drochner 
    429       1.52      matt 	/*
    430       1.52      matt 	 * The next time we need to search this segment,
    431       1.52      matt 	 * start just after the pages we just allocated.
    432       1.52      matt 	 */
    433       1.68    cherry 	uvm_physseg_set_start_hint(psi, candidate + 1 - uvm_physseg_get_avail_start(psi));
    434       1.68    cherry 	KASSERTMSG(uvm_physseg_get_start_hint(psi) <= uvm_physseg_get_avail_end(psi) -
    435       1.68    cherry 	    uvm_physseg_get_avail_start(psi),
    436       1.62       jym 	    "%#x %u (%#x) <= %#"PRIxPADDR" - %#"PRIxPADDR" (%#"PRIxPADDR")",
    437       1.66      matt 	    candidate + 1,
    438       1.68    cherry 	    uvm_physseg_get_start_hint(psi),
    439       1.68    cherry 	    uvm_physseg_get_start_hint(psi),
    440       1.68    cherry 	    uvm_physseg_get_avail_end(psi),
    441       1.68    cherry 	    uvm_physseg_get_avail_start(psi),
    442       1.68    cherry 	    uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi));
    443       1.52      matt 
    444       1.24  drochner #ifdef PGALLOC_VERBOSE
    445       1.24  drochner 	printf("got %d pgs\n", num - todo);
    446       1.24  drochner #endif
    447       1.24  drochner 	return (num - todo); /* number of pages allocated */
    448       1.22  drochner }
    449       1.22  drochner 
    450       1.20  drochner static int
    451       1.33   thorpej uvm_pglistalloc_simple(int num, paddr_t low, paddr_t high,
    452       1.33   thorpej     struct pglist *rlist, int waitok)
    453       1.20  drochner {
    454       1.68    cherry 	int fl, error;
    455       1.68    cherry 	uvm_physseg_t psi;
    456       1.20  drochner 
    457       1.20  drochner 	/* Default to "lose". */
    458       1.20  drochner 	error = ENOMEM;
    459       1.20  drochner 
    460       1.20  drochner again:
    461       1.20  drochner 	/*
    462       1.20  drochner 	 * Block all memory allocation and lock the free list.
    463       1.20  drochner 	 */
    464       1.38        ad 	mutex_spin_enter(&uvm_fpageqlock);
    465       1.20  drochner 
    466       1.20  drochner 	/* Are there even any free pages? */
    467       1.20  drochner 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    468       1.20  drochner 		goto out;
    469       1.20  drochner 
    470       1.22  drochner 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    471       1.22  drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    472       1.68    cherry 		for (psi = uvm_physseg_get_last(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_prev(psi))
    473       1.22  drochner #else
    474       1.68    cherry 		for (psi = uvm_physseg_get_first(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_next(psi))
    475       1.22  drochner #endif
    476       1.22  drochner 		{
    477       1.68    cherry 			if (uvm_physseg_get_free_list(psi) != fl)
    478       1.22  drochner 				continue;
    479       1.22  drochner 
    480       1.68    cherry 			num -= uvm_pglistalloc_s_ps(psi, num, low, high, rlist);
    481       1.24  drochner 			if (num == 0) {
    482       1.22  drochner 				error = 0;
    483       1.22  drochner 				goto out;
    484       1.22  drochner 			}
    485       1.22  drochner 		}
    486       1.20  drochner 
    487        1.3       mrg 	}
    488        1.1       mrg 
    489        1.1       mrg out:
    490        1.3       mrg 	/*
    491        1.3       mrg 	 * check to see if we need to generate some free pages waking
    492        1.3       mrg 	 * the pagedaemon.
    493        1.3       mrg 	 */
    494       1.15       chs 
    495       1.36      yamt 	uvm_kick_pdaemon();
    496       1.38        ad 	mutex_spin_exit(&uvm_fpageqlock);
    497       1.38        ad 
    498       1.20  drochner 	if (error) {
    499       1.20  drochner 		if (waitok) {
    500       1.20  drochner 			/* XXX perhaps some time limitation? */
    501       1.20  drochner #ifdef DEBUG
    502       1.20  drochner 			printf("pglistalloc waiting\n");
    503       1.20  drochner #endif
    504       1.20  drochner 			uvm_wait("pglalloc");
    505       1.20  drochner 			goto again;
    506       1.20  drochner 		} else
    507       1.20  drochner 			uvm_pglistfree(rlist);
    508       1.20  drochner 	}
    509       1.24  drochner #ifdef PGALLOC_VERBOSE
    510       1.22  drochner 	if (!error)
    511       1.44   reinoud 		printf("pgalloc: %"PRIxMAX"..%"PRIxMAX"\n",
    512       1.44   reinoud 		       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    513       1.44   reinoud 		       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    514       1.22  drochner #endif
    515        1.3       mrg 	return (error);
    516       1.20  drochner }
    517       1.20  drochner 
    518       1.20  drochner int
    519       1.33   thorpej uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment,
    520       1.33   thorpej     paddr_t boundary, struct pglist *rlist, int nsegs, int waitok)
    521       1.20  drochner {
    522       1.24  drochner 	int num, res;
    523       1.20  drochner 
    524       1.20  drochner 	KASSERT((alignment & (alignment - 1)) == 0);
    525       1.20  drochner 	KASSERT((boundary & (boundary - 1)) == 0);
    526       1.20  drochner 
    527       1.20  drochner 	/*
    528       1.20  drochner 	 * Our allocations are always page granularity, so our alignment
    529       1.20  drochner 	 * must be, too.
    530       1.20  drochner 	 */
    531       1.20  drochner 	if (alignment < PAGE_SIZE)
    532       1.20  drochner 		alignment = PAGE_SIZE;
    533       1.24  drochner 	if (boundary != 0 && boundary < size)
    534       1.24  drochner 		return (EINVAL);
    535       1.24  drochner 	num = atop(round_page(size));
    536       1.52      matt 	low = roundup2(low, alignment);
    537       1.21  drochner 
    538       1.21  drochner 	TAILQ_INIT(rlist);
    539       1.20  drochner 
    540       1.23     enami 	if ((nsegs < size >> PAGE_SHIFT) || (alignment != PAGE_SIZE) ||
    541       1.23     enami 	    (boundary != 0))
    542       1.24  drochner 		res = uvm_pglistalloc_contig(num, low, high, alignment,
    543       1.24  drochner 					     boundary, rlist);
    544       1.20  drochner 	else
    545       1.24  drochner 		res = uvm_pglistalloc_simple(num, low, high, rlist, waitok);
    546       1.20  drochner 
    547       1.20  drochner 	return (res);
    548        1.1       mrg }
    549        1.1       mrg 
    550        1.1       mrg /*
    551        1.1       mrg  * uvm_pglistfree: free a list of pages
    552        1.1       mrg  *
    553        1.1       mrg  * => pages should already be unmapped
    554        1.1       mrg  */
    555        1.1       mrg 
    556        1.3       mrg void
    557       1.33   thorpej uvm_pglistfree(struct pglist *list)
    558        1.1       mrg {
    559       1.42        ad 	struct uvm_cpu *ucpu;
    560       1.18       chs 	struct vm_page *pg;
    561       1.42        ad 	int index, color, queue;
    562       1.42        ad 	bool iszero;
    563        1.1       mrg 
    564        1.3       mrg 	/*
    565       1.18       chs 	 * Lock the free list and free each page.
    566        1.3       mrg 	 */
    567       1.18       chs 
    568       1.38        ad 	mutex_spin_enter(&uvm_fpageqlock);
    569       1.42        ad 	ucpu = curcpu()->ci_data.cpu_uvm;
    570       1.18       chs 	while ((pg = TAILQ_FIRST(list)) != NULL) {
    571       1.36      yamt 		KASSERT(!uvmpdpol_pageisqueued_p(pg));
    572       1.42        ad 		TAILQ_REMOVE(list, pg, pageq.queue);
    573       1.29      yamt 		iszero = (pg->flags & PG_ZERO);
    574       1.18       chs 		pg->pqflags = PQ_FREE;
    575       1.29      yamt #ifdef DEBUG
    576       1.29      yamt 		pg->uobject = (void *)0xdeadbeef;
    577       1.29      yamt 		pg->uanon = (void *)0xdeadbeef;
    578       1.30      yamt #endif /* DEBUG */
    579       1.30      yamt #ifdef DEBUG
    580       1.30      yamt 		if (iszero)
    581       1.30      yamt 			uvm_pagezerocheck(pg);
    582       1.30      yamt #endif /* DEBUG */
    583       1.42        ad 		index = uvm_page_lookup_freelist(pg);
    584       1.42        ad 		color = VM_PGCOLOR_BUCKET(pg);
    585       1.42        ad 		queue = iszero ? PGFL_ZEROS : PGFL_UNKNOWN;
    586       1.42        ad 		pg->offset = (uintptr_t)ucpu;
    587       1.42        ad 		LIST_INSERT_HEAD(&uvm.page_free[index].pgfl_buckets[color].
    588       1.42        ad 		    pgfl_queues[queue], pg, pageq.list);
    589       1.42        ad 		LIST_INSERT_HEAD(&ucpu->page_free[index].pgfl_buckets[color].
    590       1.42        ad 		    pgfl_queues[queue], pg, listq.list);
    591        1.3       mrg 		uvmexp.free++;
    592       1.29      yamt 		if (iszero)
    593       1.29      yamt 			uvmexp.zeropages++;
    594       1.42        ad 		ucpu->pages[queue]++;
    595        1.3       mrg 		STAT_DECR(uvm_pglistalloc_npages);
    596        1.3       mrg 	}
    597       1.42        ad 	if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN])
    598       1.42        ad 		ucpu->page_idle_zero = vm_page_zero_enable;
    599       1.38        ad 	mutex_spin_exit(&uvm_fpageqlock);
    600        1.1       mrg }
    601