2000-06-27 21:29:17 +04:00
|
|
|
/* $NetBSD: uvm_pglist.c,v 1.11 2000/06/27 17:29:34 mrg Exp $ */
|
1998-02-07 01:26:13 +03:00
|
|
|
|
1998-02-05 09:25:08 +03:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
|
|
|
* NASA Ames Research Center.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uvm_pglist.c: pglist functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
|
|
|
|
#ifdef VM_PAGE_ALLOC_MEMORY_STATS
|
|
|
|
#define STAT_INCR(v) (v)++
|
|
|
|
#define STAT_DECR(v) do { \
|
|
|
|
if ((v) == 0) \
|
|
|
|
printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
|
|
|
|
else \
|
|
|
|
(v)--; \
|
|
|
|
} while (0)
|
|
|
|
u_long uvm_pglistalloc_npages;
|
|
|
|
#else
|
|
|
|
#define STAT_INCR(v)
|
|
|
|
#define STAT_DECR(v)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uvm_pglistalloc: allocate a list of pages
|
|
|
|
*
|
|
|
|
* => allocated pages are placed at the tail of rlist. rlist is
|
|
|
|
* assumed to be properly initialized by caller.
|
|
|
|
* => returns 0 on success or errno on failure
|
|
|
|
* => XXX: implementation allocates only a single segment, also
|
|
|
|
* might be able to better advantage of vm_physeg[].
|
|
|
|
* => doesn't take into account clean non-busy pages on inactive list
|
|
|
|
* that could be used(?)
|
|
|
|
* => params:
|
|
|
|
* size the size of the allocation, rounded to page size.
|
|
|
|
* low the low address of the allowed allocation range.
|
|
|
|
* high the high address of the allowed allocation range.
|
|
|
|
* alignment memory must be aligned to this power-of-two boundary.
|
|
|
|
* boundary no segment in the allocation may cross this
|
|
|
|
* power-of-two boundary (relative to zero).
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
int
|
|
|
|
uvm_pglistalloc(size, low, high, alignment, boundary, rlist, nsegs, waitok)
|
1998-08-13 06:10:37 +04:00
|
|
|
psize_t size;
|
|
|
|
paddr_t low, high, alignment, boundary;
|
1998-03-09 03:58:55 +03:00
|
|
|
struct pglist *rlist;
|
|
|
|
int nsegs, waitok;
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-08-13 06:10:37 +04:00
|
|
|
paddr_t try, idxpa, lastidxpa;
|
1998-03-09 03:58:55 +03:00
|
|
|
int psi;
|
|
|
|
struct vm_page *pgs;
|
2000-04-24 21:12:00 +04:00
|
|
|
int s, tryidx, idx, pgflidx, end, error, free_list;
|
1998-03-09 03:58:55 +03:00
|
|
|
vm_page_t m;
|
|
|
|
u_long pagemask;
|
1998-02-05 09:25:08 +03:00
|
|
|
#ifdef DEBUG
|
1998-03-09 03:58:55 +03:00
|
|
|
vm_page_t tp;
|
1998-02-05 09:25:08 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
1998-03-09 03:58:55 +03:00
|
|
|
if ((alignment & (alignment - 1)) != 0)
|
2000-04-24 21:12:00 +04:00
|
|
|
panic("uvm_pglistalloc: alignment must be power of 2");
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
if ((boundary & (boundary - 1)) != 0)
|
2000-04-24 21:12:00 +04:00
|
|
|
panic("uvm_pglistalloc: boundary must be power of 2");
|
1998-02-05 09:25:08 +03:00
|
|
|
#endif
|
1998-03-09 03:58:55 +03:00
|
|
|
|
1998-02-05 09:25:08 +03:00
|
|
|
/*
|
1998-03-09 03:58:55 +03:00
|
|
|
* Our allocations are always page granularity, so our alignment
|
|
|
|
* must be, too.
|
1998-02-05 09:25:08 +03:00
|
|
|
*/
|
1998-03-09 03:58:55 +03:00
|
|
|
if (alignment < PAGE_SIZE)
|
|
|
|
alignment = PAGE_SIZE;
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
size = round_page(size);
|
|
|
|
try = roundup(low, alignment);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
if (boundary != 0 && boundary < size)
|
|
|
|
return (EINVAL);
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
pagemask = ~(boundary - 1);
|
|
|
|
|
|
|
|
/* Default to "lose". */
|
|
|
|
error = ENOMEM;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block all memory allocation and lock the free list.
|
|
|
|
*/
|
1999-05-24 23:10:57 +04:00
|
|
|
s = uvm_lock_fpageq(); /* lock free page queue */
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/* Are there even any free pages? */
|
2000-04-24 21:12:00 +04:00
|
|
|
if (uvmexp.free <= (uvmexp.reserve_pagedaemon +
|
|
|
|
uvmexp.reserve_kernel))
|
1998-03-09 03:58:55 +03:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (;; try += alignment) {
|
|
|
|
if (try + size > high) {
|
|
|
|
/*
|
|
|
|
* We've run past the allowable range.
|
|
|
|
*/
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure this is a managed physical page.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((psi = vm_physseg_find(atop(try), &idx)) == -1)
|
|
|
|
continue; /* managed? */
|
|
|
|
if (vm_physseg_find(atop(try + size), NULL) != psi)
|
|
|
|
continue; /* end must be in this segment */
|
|
|
|
|
|
|
|
tryidx = idx;
|
|
|
|
end = idx + (size / PAGE_SIZE);
|
|
|
|
pgs = vm_physmem[psi].pgs;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Found a suitable starting page. See of the range is free.
|
|
|
|
*/
|
|
|
|
for (; idx < end; idx++) {
|
|
|
|
if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) {
|
|
|
|
/*
|
|
|
|
* Page not available.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
|
|
|
|
|
|
|
|
if (idx > tryidx) {
|
|
|
|
lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
|
|
|
|
|
|
|
|
if ((lastidxpa + PAGE_SIZE) != idxpa) {
|
|
|
|
/*
|
|
|
|
* Region not contiguous.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (boundary != 0 &&
|
|
|
|
((lastidxpa ^ idxpa) & pagemask) != 0) {
|
|
|
|
/*
|
|
|
|
* Region crosses boundary.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx == end) {
|
|
|
|
/*
|
|
|
|
* Woo hoo! Found one.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
1998-03-09 03:58:55 +03:00
|
|
|
|
2000-04-24 21:12:00 +04:00
|
|
|
#if PGFL_NQUEUES != 2
|
|
|
|
#error uvm_pglistalloc needs to be updated
|
|
|
|
#endif
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* we have a chunk of memory that conforms to the requested constraints.
|
|
|
|
*/
|
|
|
|
idx = tryidx;
|
|
|
|
while (idx < end) {
|
|
|
|
m = &pgs[idx];
|
Add support for multiple memory free lists. There is at least one
default free list, and 0 - N additional free list, in order of descending
priority.
A new page allocation function, uvm_pagealloc_strat(), has been added,
providing three page allocation strategies:
- normal: high -> low priority free list walk, taking the
page off the first free list that has one.
- only: attempt to allocate a page only from the specified free
list, failing if that free list has none available.
- fallback: if `only' fails, fall back on `normal'.
uvm_pagealloc(...) is provided for normal use (and is a synonym for
uvm_pagealloc_strat(..., UVM_PGA_STRAT_NORMAL, 0); the free list argument
is ignored for the `normal' case).
uvm_page_physload() now specified which free list the pages will be
loaded onto. This means that some platforms which have multiple physical
memory segments may define additional vm_physsegs if they wish to break
individual physical segments into differing priorities.
Machine-dependent code must define _at least_ the following constants
in <machine/vmparam.h>:
VM_NFREELIST: the number of free lists the system will have
VM_FREELIST_DEFAULT: the default freelist (should always be 0,
but is defined in machdep code so that it's with all of the
other free list-related constants).
Additional free list names may be defined by machine-dependent code, but
they will only be used by machine-dependent code (e.g. for loading the
vm_physsegs).
1998-07-08 08:28:27 +04:00
|
|
|
free_list = uvm_page_lookup_freelist(m);
|
2000-04-24 21:12:00 +04:00
|
|
|
pgflidx = (m->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
|
1998-02-05 09:25:08 +03:00
|
|
|
#ifdef DEBUG
|
2000-04-24 21:12:00 +04:00
|
|
|
for (tp = TAILQ_FIRST(&uvm.page_free[
|
|
|
|
free_list].pgfl_queues[pgflidx]);
|
|
|
|
tp != NULL;
|
|
|
|
tp = TAILQ_NEXT(tp, pageq)) {
|
1998-03-09 03:58:55 +03:00
|
|
|
if (tp == m)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tp == NULL)
|
|
|
|
panic("uvm_pglistalloc: page not on freelist");
|
1998-02-05 09:25:08 +03:00
|
|
|
#endif
|
2000-04-24 21:12:00 +04:00
|
|
|
TAILQ_REMOVE(&uvm.page_free[free_list].pgfl_queues[pgflidx],
|
|
|
|
m, pageq);
|
1998-03-09 03:58:55 +03:00
|
|
|
uvmexp.free--;
|
2000-04-24 21:12:00 +04:00
|
|
|
if (m->flags & PG_ZERO)
|
|
|
|
uvmexp.zeropages--;
|
1998-03-09 03:58:55 +03:00
|
|
|
m->flags = PG_CLEAN;
|
|
|
|
m->pqflags = 0;
|
|
|
|
m->uobject = NULL;
|
|
|
|
m->uanon = NULL;
|
|
|
|
m->wire_count = 0;
|
|
|
|
m->loan_count = 0;
|
|
|
|
TAILQ_INSERT_TAIL(rlist, m, pageq);
|
|
|
|
idx++;
|
|
|
|
STAT_INCR(uvm_pglistalloc_npages);
|
|
|
|
}
|
|
|
|
error = 0;
|
1998-02-05 09:25:08 +03:00
|
|
|
|
|
|
|
out:
|
1999-05-24 23:10:57 +04:00
|
|
|
uvm_unlock_fpageq(s);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check to see if we need to generate some free pages waking
|
|
|
|
* the pagedaemon.
|
|
|
|
* XXX: we read uvm.free without locking
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (uvmexp.free < uvmexp.freemin ||
|
|
|
|
(uvmexp.free < uvmexp.freetarg &&
|
|
|
|
uvmexp.inactive < uvmexp.inactarg))
|
1999-07-23 02:58:38 +04:00
|
|
|
wakeup(&uvm.pagedaemon);
|
1998-03-09 03:58:55 +03:00
|
|
|
|
|
|
|
return (error);
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uvm_pglistfree: free a list of pages
|
|
|
|
*
|
|
|
|
* => pages should already be unmapped
|
|
|
|
*/
|
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
void
|
|
|
|
uvm_pglistfree(list)
|
|
|
|
struct pglist *list;
|
1998-02-05 09:25:08 +03:00
|
|
|
{
|
1998-03-09 03:58:55 +03:00
|
|
|
vm_page_t m;
|
|
|
|
int s;
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
/*
|
|
|
|
* Block all memory allocation and lock the free list.
|
|
|
|
*/
|
1999-05-24 23:10:57 +04:00
|
|
|
s = uvm_lock_fpageq();
|
1998-02-05 09:25:08 +03:00
|
|
|
|
1998-03-09 03:58:55 +03:00
|
|
|
while ((m = list->tqh_first) != NULL) {
|
1998-02-05 09:25:08 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
1998-03-09 03:58:55 +03:00
|
|
|
if (m->pqflags & (PQ_ACTIVE|PQ_INACTIVE))
|
|
|
|
panic("uvm_pglistfree: active/inactive page!");
|
1998-02-05 09:25:08 +03:00
|
|
|
#endif
|
1998-03-09 03:58:55 +03:00
|
|
|
TAILQ_REMOVE(list, m, pageq);
|
|
|
|
m->pqflags = PQ_FREE;
|
2000-04-24 21:12:00 +04:00
|
|
|
TAILQ_INSERT_TAIL(&uvm.page_free[
|
|
|
|
uvm_page_lookup_freelist(m)].pgfl_queues[PGFL_UNKNOWN],
|
Add support for multiple memory free lists. There is at least one
default free list, and 0 - N additional free list, in order of descending
priority.
A new page allocation function, uvm_pagealloc_strat(), has been added,
providing three page allocation strategies:
- normal: high -> low priority free list walk, taking the
page off the first free list that has one.
- only: attempt to allocate a page only from the specified free
list, failing if that free list has none available.
- fallback: if `only' fails, fall back on `normal'.
uvm_pagealloc(...) is provided for normal use (and is a synonym for
uvm_pagealloc_strat(..., UVM_PGA_STRAT_NORMAL, 0); the free list argument
is ignored for the `normal' case).
uvm_page_physload() now specified which free list the pages will be
loaded onto. This means that some platforms which have multiple physical
memory segments may define additional vm_physsegs if they wish to break
individual physical segments into differing priorities.
Machine-dependent code must define _at least_ the following constants
in <machine/vmparam.h>:
VM_NFREELIST: the number of free lists the system will have
VM_FREELIST_DEFAULT: the default freelist (should always be 0,
but is defined in machdep code so that it's with all of the
other free list-related constants).
Additional free list names may be defined by machine-dependent code, but
they will only be used by machine-dependent code (e.g. for loading the
vm_physsegs).
1998-07-08 08:28:27 +04:00
|
|
|
m, pageq);
|
1998-03-09 03:58:55 +03:00
|
|
|
uvmexp.free++;
|
2000-04-24 21:12:00 +04:00
|
|
|
if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
|
|
|
|
uvm.page_idle_zero = vm_page_zero_enable;
|
1998-03-09 03:58:55 +03:00
|
|
|
STAT_DECR(uvm_pglistalloc_npages);
|
|
|
|
}
|
|
|
|
|
1999-05-24 23:10:57 +04:00
|
|
|
uvm_unlock_fpageq(s);
|
1998-02-05 09:25:08 +03:00
|
|
|
}
|