Home | History | Annotate | Line # | Download | only in uvm
uvm_pglist.c revision 1.50
      1 /*	$NetBSD: uvm_pglist.c,v 1.50 2010/11/18 11:49:41 cegger 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.50 2010/11/18 11:49:41 cegger 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 #ifdef DEBUG
     63 static int
     64 vm_physmem_index(struct vm_physseg *ps)
     65 {
     66 	int i;
     67 
     68 	for (i = 0; i < vm_nphysmem; i++) {
     69 		if (VM_PHYSMEM_PTR(i) == ps)
     70 			return i;
     71 	}
     72 	return -1;
     73 }
     74 #endif
     75 
     76 /*
     77  * uvm_pglistalloc: allocate a list of pages
     78  *
     79  * => allocated pages are placed onto an rlist.  rlist is
     80  *    initialized by uvm_pglistalloc.
     81  * => returns 0 on success or errno on failure
     82  * => implementation allocates a single segment if any constraints are
     83  *	imposed by call arguments.
     84  * => doesn't take into account clean non-busy pages on inactive list
     85  *	that could be used(?)
     86  * => params:
     87  *	size		the size of the allocation, rounded to page size.
     88  *	low		the low address of the allowed allocation range.
     89  *	high		the high address of the allowed allocation range.
     90  *	alignment	memory must be aligned to this power-of-two boundary.
     91  *	boundary	no segment in the allocation may cross this
     92  *			power-of-two boundary (relative to zero).
     93  */
     94 
     95 static void
     96 uvm_pglist_add(struct vm_page *pg, struct pglist *rlist)
     97 {
     98 	int free_list, color, pgflidx;
     99 #ifdef NOT_DEBUG
    100 	struct vm_page *tp;
    101 #endif
    102 
    103 	KASSERT(mutex_owned(&uvm_fpageqlock));
    104 
    105 #if PGFL_NQUEUES != 2
    106 #error uvm_pglistalloc needs to be updated
    107 #endif
    108 
    109 	free_list = uvm_page_lookup_freelist(pg);
    110 	color = VM_PGCOLOR_BUCKET(pg);
    111 	pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
    112 #ifdef NOT_DEBUG
    113 	for (tp = LIST_FIRST(&uvm.page_free[
    114 		free_list].pgfl_buckets[color].pgfl_queues[pgflidx]);
    115 	     tp != NULL;
    116 	     tp = LIST_NEXT(tp, pageq.list)) {
    117 		if (tp == pg)
    118 			break;
    119 	}
    120 	if (tp == NULL)
    121 		panic("uvm_pglistalloc: page not on freelist");
    122 #endif
    123 	LIST_REMOVE(pg, pageq.list);	/* global */
    124 	LIST_REMOVE(pg, listq.list);	/* cpu */
    125 	uvmexp.free--;
    126 	if (pg->flags & PG_ZERO)
    127 		uvmexp.zeropages--;
    128 	VM_FREE_PAGE_TO_CPU(pg)->pages[pgflidx]--;
    129 	pg->flags = PG_CLEAN;
    130 	pg->pqflags = 0;
    131 	pg->uobject = NULL;
    132 	pg->uanon = NULL;
    133 	TAILQ_INSERT_TAIL(rlist, pg, pageq.queue);
    134 	STAT_INCR(uvm_pglistalloc_npages);
    135 }
    136 
    137 static int
    138 uvm_pglistalloc_c_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high,
    139     paddr_t alignment, paddr_t boundary, struct pglist *rlist)
    140 {
    141 	int try, limit, tryidx, end, idx;
    142 	struct vm_page *pgs;
    143 	int pagemask;
    144 #ifdef DEBUG
    145 	paddr_t idxpa, lastidxpa;
    146 	int cidx = 0;	/* XXX: GCC */
    147 #endif
    148 #ifdef PGALLOC_VERBOSE
    149 	printf("pgalloc: contig %d pgs from psi %ld\n", num,
    150 	(long)(ps - VM_PHYSMEM_PTR(0)));
    151 #endif
    152 
    153 	KASSERT(mutex_owned(&uvm_fpageqlock));
    154 
    155 	try = roundup(max(atop(low), ps->avail_start), atop(alignment));
    156 	limit = min(atop(high), ps->avail_end);
    157 	pagemask = ~((boundary >> PAGE_SHIFT) - 1);
    158 
    159 	for (;;) {
    160 		if (try + num > limit) {
    161 			/*
    162 			 * We've run past the allowable range.
    163 			 */
    164 			return (0); /* FAIL */
    165 		}
    166 		if (boundary != 0 &&
    167 		    ((try ^ (try + num - 1)) & pagemask) != 0) {
    168 			/*
    169 			 * Region crosses boundary. Jump to the boundary
    170 			 * just crossed and ensure alignment.
    171 			 */
    172 			try = (try + num - 1) & pagemask;
    173 			try = roundup(try, atop(alignment));
    174 			continue;
    175 		}
    176 #ifdef DEBUG
    177 		/*
    178 		 * Make sure this is a managed physical page.
    179 		 */
    180 
    181 		if (vm_physseg_find(try, &cidx) != vm_physmem_index(ps))
    182 			panic("pgalloc contig: botch1");
    183 		if (cidx != try - ps->start)
    184 			panic("pgalloc contig: botch2");
    185 		if (vm_physseg_find(try + num - 1, &cidx) != vm_physmem_index(ps))
    186 			panic("pgalloc contig: botch3");
    187 		if (cidx != try - ps->start + num - 1)
    188 			panic("pgalloc contig: botch4");
    189 #endif
    190 		tryidx = try - ps->start;
    191 		end = tryidx + num;
    192 		pgs = ps->pgs;
    193 
    194 		/*
    195 		 * Found a suitable starting page.  See if the range is free.
    196 		 */
    197 		for (idx = tryidx; idx < end; idx++) {
    198 			if (VM_PAGE_IS_FREE(&pgs[idx]) == 0)
    199 				break;
    200 
    201 #ifdef DEBUG
    202 			idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
    203 			if (idx > tryidx) {
    204 				lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
    205 				if ((lastidxpa + PAGE_SIZE) != idxpa) {
    206 					/*
    207 					 * Region not contiguous.
    208 					 */
    209 					panic("pgalloc contig: botch5");
    210 				}
    211 				if (boundary != 0 &&
    212 				    ((lastidxpa ^ idxpa) & ~(boundary - 1))
    213 				    != 0) {
    214 					/*
    215 					 * Region crosses boundary.
    216 					 */
    217 					panic("pgalloc contig: botch6");
    218 				}
    219 			}
    220 #endif
    221 		}
    222 		if (idx == end)
    223 			break;
    224 
    225 		try += atop(alignment);
    226 	}
    227 
    228 	/*
    229 	 * we have a chunk of memory that conforms to the requested constraints.
    230 	 */
    231 	idx = tryidx;
    232 	while (idx < end)
    233 		uvm_pglist_add(&pgs[idx++], rlist);
    234 
    235 #ifdef PGALLOC_VERBOSE
    236 	printf("got %d pgs\n", num);
    237 #endif
    238 	return (num); /* number of pages allocated */
    239 }
    240 
    241 static int
    242 uvm_pglistalloc_contig(int num, paddr_t low, paddr_t high, paddr_t alignment,
    243     paddr_t boundary, struct pglist *rlist)
    244 {
    245 	int fl, psi;
    246 	struct vm_physseg *ps;
    247 	int error;
    248 
    249 	/* Default to "lose". */
    250 	error = ENOMEM;
    251 
    252 	/*
    253 	 * Block all memory allocation and lock the free list.
    254 	 */
    255 	mutex_spin_enter(&uvm_fpageqlock);
    256 
    257 	/* Are there even any free pages? */
    258 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    259 		goto out;
    260 
    261 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    262 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    263 		for (psi = vm_nphysmem - 1 ; psi >= 0 ; psi--)
    264 #else
    265 		for (psi = 0 ; psi < vm_nphysmem ; psi++)
    266 #endif
    267 		{
    268 			ps = VM_PHYSMEM_PTR(psi);
    269 
    270 			if (ps->free_list != fl)
    271 				continue;
    272 
    273 			num -= uvm_pglistalloc_c_ps(ps, num, low, high,
    274 						    alignment, boundary, rlist);
    275 			if (num == 0) {
    276 #ifdef PGALLOC_VERBOSE
    277 				printf("pgalloc: %"PRIxMAX"-%"PRIxMAX"\n",
    278 				       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    279 				       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    280 #endif
    281 				error = 0;
    282 				goto out;
    283 			}
    284 		}
    285 	}
    286 
    287 out:
    288 	/*
    289 	 * check to see if we need to generate some free pages waking
    290 	 * the pagedaemon.
    291 	 */
    292 
    293 	uvm_kick_pdaemon();
    294 	mutex_spin_exit(&uvm_fpageqlock);
    295 	return (error);
    296 }
    297 
    298 static int
    299 uvm_pglistalloc_s_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high,
    300     struct pglist *rlist)
    301 {
    302 	int todo, limit, try;
    303 	struct vm_page *pg;
    304 #ifdef DEBUG
    305 	int cidx = 0;	/* XXX: GCC */
    306 #endif
    307 #ifdef PGALLOC_VERBOSE
    308 	printf("pgalloc: simple %d pgs from psi %ld\n", num,
    309 	    (long)(ps - VM_PHYSMEM_PTR(0)));
    310 #endif
    311 
    312 	KASSERT(mutex_owned(&uvm_fpageqlock));
    313 
    314 	todo = num;
    315 	limit = min(atop(high), ps->avail_end);
    316 
    317 	for (try = max(atop(low), ps->avail_start);
    318 	     try < limit; try ++) {
    319 #ifdef DEBUG
    320 		if (vm_physseg_find(try, &cidx) != vm_physmem_index(ps))
    321 			panic("pgalloc simple: botch1");
    322 		if (cidx != (try - ps->start))
    323 			panic("pgalloc simple: botch2");
    324 #endif
    325 		pg = &ps->pgs[try - ps->start];
    326 		if (VM_PAGE_IS_FREE(pg) == 0)
    327 			continue;
    328 
    329 		uvm_pglist_add(pg, rlist);
    330 		if (--todo == 0)
    331 			break;
    332 	}
    333 
    334 #ifdef PGALLOC_VERBOSE
    335 	printf("got %d pgs\n", num - todo);
    336 #endif
    337 	return (num - todo); /* number of pages allocated */
    338 }
    339 
    340 static int
    341 uvm_pglistalloc_simple(int num, paddr_t low, paddr_t high,
    342     struct pglist *rlist, int waitok)
    343 {
    344 	int fl, psi, error;
    345 	struct vm_physseg *ps;
    346 
    347 	/* Default to "lose". */
    348 	error = ENOMEM;
    349 
    350 again:
    351 	/*
    352 	 * Block all memory allocation and lock the free list.
    353 	 */
    354 	mutex_spin_enter(&uvm_fpageqlock);
    355 
    356 	/* Are there even any free pages? */
    357 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    358 		goto out;
    359 
    360 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    361 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    362 		for (psi = vm_nphysmem - 1 ; psi >= 0 ; psi--)
    363 #else
    364 		for (psi = 0 ; psi < vm_nphysmem ; psi++)
    365 #endif
    366 		{
    367 			ps = VM_PHYSMEM_PTR(psi);
    368 
    369 			if (ps->free_list != fl)
    370 				continue;
    371 
    372 			num -= uvm_pglistalloc_s_ps(ps, num, low, high, rlist);
    373 			if (num == 0) {
    374 				error = 0;
    375 				goto out;
    376 			}
    377 		}
    378 
    379 	}
    380 
    381 out:
    382 	/*
    383 	 * check to see if we need to generate some free pages waking
    384 	 * the pagedaemon.
    385 	 */
    386 
    387 	uvm_kick_pdaemon();
    388 	mutex_spin_exit(&uvm_fpageqlock);
    389 
    390 	if (error) {
    391 		if (waitok) {
    392 			/* XXX perhaps some time limitation? */
    393 #ifdef DEBUG
    394 			printf("pglistalloc waiting\n");
    395 #endif
    396 			uvm_wait("pglalloc");
    397 			goto again;
    398 		} else
    399 			uvm_pglistfree(rlist);
    400 	}
    401 #ifdef PGALLOC_VERBOSE
    402 	if (!error)
    403 		printf("pgalloc: %"PRIxMAX"..%"PRIxMAX"\n",
    404 		       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    405 		       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    406 #endif
    407 	return (error);
    408 }
    409 
    410 int
    411 uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment,
    412     paddr_t boundary, struct pglist *rlist, int nsegs, int waitok)
    413 {
    414 	int num, res;
    415 
    416 	KASSERT((alignment & (alignment - 1)) == 0);
    417 	KASSERT((boundary & (boundary - 1)) == 0);
    418 
    419 	/*
    420 	 * Our allocations are always page granularity, so our alignment
    421 	 * must be, too.
    422 	 */
    423 	if (alignment < PAGE_SIZE)
    424 		alignment = PAGE_SIZE;
    425 	if (boundary != 0 && boundary < size)
    426 		return (EINVAL);
    427 	num = atop(round_page(size));
    428 	low = roundup(low, alignment);
    429 
    430 	TAILQ_INIT(rlist);
    431 
    432 	if ((nsegs < size >> PAGE_SHIFT) || (alignment != PAGE_SIZE) ||
    433 	    (boundary != 0))
    434 		res = uvm_pglistalloc_contig(num, low, high, alignment,
    435 					     boundary, rlist);
    436 	else
    437 		res = uvm_pglistalloc_simple(num, low, high, rlist, waitok);
    438 
    439 	return (res);
    440 }
    441 
    442 /*
    443  * uvm_pglistfree: free a list of pages
    444  *
    445  * => pages should already be unmapped
    446  */
    447 
    448 void
    449 uvm_pglistfree(struct pglist *list)
    450 {
    451 	struct uvm_cpu *ucpu;
    452 	struct vm_page *pg;
    453 	int index, color, queue;
    454 	bool iszero;
    455 
    456 	/*
    457 	 * Lock the free list and free each page.
    458 	 */
    459 
    460 	mutex_spin_enter(&uvm_fpageqlock);
    461 	ucpu = curcpu()->ci_data.cpu_uvm;
    462 	while ((pg = TAILQ_FIRST(list)) != NULL) {
    463 		KASSERT(!uvmpdpol_pageisqueued_p(pg));
    464 		TAILQ_REMOVE(list, pg, pageq.queue);
    465 		iszero = (pg->flags & PG_ZERO);
    466 		pg->pqflags = PQ_FREE;
    467 #ifdef DEBUG
    468 		pg->uobject = (void *)0xdeadbeef;
    469 		pg->uanon = (void *)0xdeadbeef;
    470 #endif /* DEBUG */
    471 #ifdef DEBUG
    472 		if (iszero)
    473 			uvm_pagezerocheck(pg);
    474 #endif /* DEBUG */
    475 		index = uvm_page_lookup_freelist(pg);
    476 		color = VM_PGCOLOR_BUCKET(pg);
    477 		queue = iszero ? PGFL_ZEROS : PGFL_UNKNOWN;
    478 		pg->offset = (uintptr_t)ucpu;
    479 		LIST_INSERT_HEAD(&uvm.page_free[index].pgfl_buckets[color].
    480 		    pgfl_queues[queue], pg, pageq.list);
    481 		LIST_INSERT_HEAD(&ucpu->page_free[index].pgfl_buckets[color].
    482 		    pgfl_queues[queue], pg, listq.list);
    483 		uvmexp.free++;
    484 		if (iszero)
    485 			uvmexp.zeropages++;
    486 		ucpu->pages[queue]++;
    487 		STAT_DECR(uvm_pglistalloc_npages);
    488 	}
    489 	if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN])
    490 		ucpu->page_idle_zero = vm_page_zero_enable;
    491 	mutex_spin_exit(&uvm_fpageqlock);
    492 }
    493