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