summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sys/conf/files1
-rw-r--r--sys/kern/kern_exec.c7
-rw-r--r--sys/vm/vm_fault.c13
-rw-r--r--sys/vm/vm_object.c45
-rw-r--r--sys/vm/vm_page.c53
-rw-r--r--sys/vm/vm_reserv.c668
-rw-r--r--sys/vm/vm_reserv.h59
7 files changed, 842 insertions, 4 deletions
diff --git a/sys/conf/files b/sys/conf/files
index a3392eb..558c6d1 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -2108,6 +2108,7 @@ vm/vm_pageout.c standard
vm/vm_pageq.c standard
vm/vm_pager.c standard
vm/vm_phys.c standard
+vm/vm_reserv.c standard
vm/vm_unix.c standard
vm/vm_zeroidle.c standard
vm/vnode_pager.c standard
diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c
index 2878b37..b0a8dcc 100644
--- a/sys/kern/kern_exec.c
+++ b/sys/kern/kern_exec.c
@@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$");
#include "opt_hwpmc_hooks.h"
#include "opt_ktrace.h"
#include "opt_mac.h"
+#include "opt_vm.h"
#include <sys/param.h>
#include <sys/systm.h>
@@ -820,6 +821,12 @@ exec_map_first_page(imgp)
if (object == NULL)
return (EACCES);
VM_OBJECT_LOCK(object);
+#if VM_NRESERVLEVEL > 0
+ if ((object->flags & OBJ_COLORED) == 0) {
+ object->flags |= OBJ_COLORED;
+ object->pg_color = 0;
+ }
+#endif
ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
initial_pagein = VM_INITIAL_PAGEIN;
diff --git a/sys/vm/vm_fault.c b/sys/vm/vm_fault.c
index 49a4077..d2a8220 100644
--- a/sys/vm/vm_fault.c
+++ b/sys/vm/vm_fault.c
@@ -74,6 +74,8 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include "opt_vm.h"
+
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
@@ -429,6 +431,13 @@ RetryFault:;
*/
fs.m = NULL;
if (!vm_page_count_severe()) {
+#if VM_NRESERVLEVEL > 0
+ if ((fs.object->flags & OBJ_COLORED) == 0) {
+ fs.object->flags |= OBJ_COLORED;
+ fs.object->pg_color = atop(vaddr) -
+ fs.pindex;
+ }
+#endif
fs.m = vm_page_alloc(fs.object, fs.pindex,
(fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO);
}
@@ -1107,6 +1116,10 @@ vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
*/
dst_object = vm_object_allocate(OBJT_DEFAULT,
OFF_TO_IDX(dst_entry->end - dst_entry->start));
+#if VM_NRESERVLEVEL > 0
+ dst_object->flags |= OBJ_COLORED;
+ dst_object->pg_color = atop(dst_entry->start);
+#endif
VM_OBJECT_LOCK(dst_object);
dst_entry->object.vm_object = dst_object;
diff --git a/sys/vm/vm_object.c b/sys/vm/vm_object.c
index 4c433d3..8f24a8a 100644
--- a/sys/vm/vm_object.c
+++ b/sys/vm/vm_object.c
@@ -65,6 +65,8 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include "opt_vm.h"
+
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/lock.h>
@@ -90,6 +92,7 @@ __FBSDID("$FreeBSD$");
#include <vm/swap_pager.h>
#include <vm/vm_kern.h>
#include <vm/vm_extern.h>
+#include <vm/vm_reserv.h>
#include <vm/uma.h>
#define EASY_SCAN_FACTOR 8
@@ -170,6 +173,11 @@ vm_object_zdtor(void *mem, int size, void *arg)
KASSERT(TAILQ_EMPTY(&object->memq),
("object %p has resident pages",
object));
+#if VM_NRESERVLEVEL > 0
+ KASSERT(LIST_EMPTY(&object->rvq),
+ ("object %p has reservations",
+ object));
+#endif
KASSERT(object->cache == NULL,
("object %p has cached pages",
object));
@@ -220,6 +228,9 @@ _vm_object_allocate(objtype_t type, vm_pindex_t size, vm_object_t object)
object->handle = NULL;
object->backing_object = NULL;
object->backing_object_offset = (vm_ooffset_t) 0;
+#if VM_NRESERVLEVEL > 0
+ LIST_INIT(&object->rvq);
+#endif
object->cache = NULL;
mtx_lock(&vm_object_list_mtx);
@@ -241,10 +252,18 @@ vm_object_init(void)
VM_OBJECT_LOCK_INIT(&kernel_object_store, "kernel object");
_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
kernel_object);
+#if VM_NRESERVLEVEL > 0
+ kernel_object->flags |= OBJ_COLORED;
+ kernel_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
+#endif
VM_OBJECT_LOCK_INIT(&kmem_object_store, "kmem object");
_vm_object_allocate(OBJT_PHYS, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
kmem_object);
+#if VM_NRESERVLEVEL > 0
+ kmem_object->flags |= OBJ_COLORED;
+ kmem_object->pg_color = (u_short)atop(VM_MIN_KERNEL_ADDRESS);
+#endif
/*
* The lock portion of struct vm_object must be type stable due
@@ -652,6 +671,10 @@ vm_object_terminate(vm_object_t object)
}
vm_page_unlock_queues();
+#if VM_NRESERVLEVEL > 0
+ if (__predict_false(!LIST_EMPTY(&object->rvq)))
+ vm_reserv_break_all(object);
+#endif
if (__predict_false(object->cache != NULL))
vm_page_cache_free(object, 0, 0);
@@ -1248,7 +1271,13 @@ vm_object_shadow(
LIST_INSERT_HEAD(&source->shadow_head, result, shadow_list);
source->shadow_count++;
source->generation++;
+#if VM_NRESERVLEVEL > 0
+ result->flags |= source->flags & (OBJ_NEEDGIANT | OBJ_COLORED);
+ result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) &
+ ((1 << (VM_NFREEORDER - 1)) - 1);
+#else
result->flags |= source->flags & OBJ_NEEDGIANT;
+#endif
VM_OBJECT_UNLOCK(source);
}
@@ -1560,6 +1589,14 @@ vm_object_backing_scan(vm_object_t object, int op)
continue;
}
+#if VM_NRESERVLEVEL > 0
+ /*
+ * Rename the reservation.
+ */
+ vm_reserv_rename(p, object, backing_object,
+ backing_offset_index);
+#endif
+
/*
* Page does not exist in parent, rename the
* page from the backing object to the main object.
@@ -1662,6 +1699,14 @@ vm_object_collapse(vm_object_t object)
*/
vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
+#if VM_NRESERVLEVEL > 0
+ /*
+ * Break any reservations from backing_object.
+ */
+ if (__predict_false(!LIST_EMPTY(&backing_object->rvq)))
+ vm_reserv_break_all(backing_object);
+#endif
+
/*
* Move the pager from backing_object to object.
*/
diff --git a/sys/vm/vm_page.c b/sys/vm/vm_page.c
index 622f5fd..485b738 100644
--- a/sys/vm/vm_page.c
+++ b/sys/vm/vm_page.c
@@ -99,6 +99,8 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include "opt_vm.h"
+
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/lock.h>
@@ -118,6 +120,7 @@ __FBSDID("$FreeBSD$");
#include <vm/vm_pageout.h>
#include <vm/vm_pager.h>
#include <vm/vm_phys.h>
+#include <vm/vm_reserv.h>
#include <vm/vm_extern.h>
#include <vm/uma.h>
#include <vm/uma_int.h>
@@ -318,6 +321,13 @@ vm_page_startup(vm_offset_t vaddr)
mapped = pmap_map(&vaddr, new_end, end,
VM_PROT_READ | VM_PROT_WRITE);
vm_page_array = (vm_page_t) mapped;
+#if VM_NRESERVLEVEL > 0
+ /*
+ * Allocate memory for the reservation management system's data
+ * structures.
+ */
+ new_end = vm_reserv_startup(&vaddr, new_end, high_water);
+#endif
#ifdef __amd64__
/*
* pmap_map on amd64 comes out of the direct-map, not kvm like i386,
@@ -363,6 +373,12 @@ vm_page_startup(vm_offset_t vaddr)
}
}
freeenv(list);
+#if VM_NRESERVLEVEL > 0
+ /*
+ * Initialize the reservation management system.
+ */
+ vm_reserv_init();
+#endif
return (vaddr);
}
@@ -1028,15 +1044,33 @@ vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
}
if (vm_phys_unfree_page(m))
vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
+#if VM_NRESERVLEVEL > 0
+ else if (!vm_reserv_reactivate_page(m))
+#else
else
+#endif
panic("vm_page_alloc: cache page %p is missing"
" from the free queue", m);
} else if ((req & VM_ALLOC_IFCACHED) != 0) {
mtx_unlock(&vm_page_queue_free_mtx);
return (NULL);
- } else
+#if VM_NRESERVLEVEL > 0
+ } else if (object == NULL ||
+ (object->flags & OBJ_COLORED) == 0 ||
+ (m = vm_reserv_alloc_page(object, pindex)) == NULL) {
+#else
+ } else {
+#endif
m = vm_phys_alloc_pages(object != NULL ?
VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
+#if VM_NRESERVLEVEL > 0
+ if (m == NULL && vm_reserv_reclaim()) {
+ m = vm_phys_alloc_pages(object != NULL ?
+ VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
+ 0);
+ }
+#endif
+ }
} else {
/*
* Not allocatable, give up.
@@ -1301,7 +1335,12 @@ vm_page_free_toq(vm_page_t m)
m->flags |= PG_FREE;
mtx_lock(&vm_page_queue_free_mtx);
cnt.v_free_count++;
- vm_phys_free_pages(m, 0);
+#if VM_NRESERVLEVEL > 0
+ if (!vm_reserv_free_page(m))
+#else
+ if (TRUE)
+#endif
+ vm_phys_free_pages(m, 0);
if ((m->flags & PG_ZERO) != 0)
++vm_page_zero_count;
else
@@ -1548,7 +1587,6 @@ vm_page_cache(vm_page_t m)
vm_page_flag_set(m, PG_CACHED);
vm_page_flag_clear(m, PG_ZERO);
mtx_lock(&vm_page_queue_free_mtx);
- vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
cnt.v_cache_count++;
root = object->cache;
if (root == NULL) {
@@ -1569,7 +1607,14 @@ vm_page_cache(vm_page_t m)
}
}
object->cache = m;
- vm_phys_free_pages(m, 0);
+#if VM_NRESERVLEVEL > 0
+ if (!vm_reserv_free_page(m)) {
+#else
+ if (TRUE) {
+#endif
+ vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
+ vm_phys_free_pages(m, 0);
+ }
vm_page_free_wakeup();
mtx_unlock(&vm_page_queue_free_mtx);
diff --git a/sys/vm/vm_reserv.c b/sys/vm/vm_reserv.c
new file mode 100644
index 0000000..3d0690c
--- /dev/null
+++ b/sys/vm/vm_reserv.c
@@ -0,0 +1,668 @@
+/*-
+ * Copyright (c) 2002-2006 Rice University
+ * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu>
+ * All rights reserved.
+ *
+ * This software was developed for the FreeBSD Project by Alan L. Cox,
+ * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
+ * HOLDERS 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.
+ */
+
+/*
+ * Superpage reservation management module
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "opt_vm.h"
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/mutex.h>
+#include <sys/queue.h>
+#include <sys/sbuf.h>
+#include <sys/sysctl.h>
+#include <sys/systm.h>
+
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+#include <vm/vm_object.h>
+#include <vm/vm_page.h>
+#include <vm/vm_phys.h>
+#include <vm/vm_reserv.h>
+
+/*
+ * The reservation system supports the speculative allocation of large physical
+ * pages ("superpages"). Speculative allocation enables the fully-automatic
+ * utilization of superpages by the virtual memory system. In other words, no
+ * programmatic directives are required to use superpages.
+ */
+
+#if VM_NRESERVLEVEL > 0
+
+/*
+ * The number of small pages that are contained in a level 0 reservation
+ */
+#define VM_LEVEL_0_NPAGES (1 << VM_LEVEL_0_ORDER)
+
+/*
+ * The number of bits by which a physical address is shifted to obtain the
+ * reservation number
+ */
+#define VM_LEVEL_0_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT)
+
+/*
+ * The size of a level 0 reservation in bytes
+ */
+#define VM_LEVEL_0_SIZE (1 << VM_LEVEL_0_SHIFT)
+
+/*
+ * Computes the index of the small page underlying the given (object, pindex)
+ * within the reservation's array of small pages.
+ */
+#define VM_RESERV_INDEX(object, pindex) \
+ (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1))
+
+/*
+ * The reservation structure
+ *
+ * A reservation structure is constructed whenever a large physical page is
+ * speculatively allocated to an object. The reservation provides the small
+ * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets
+ * within that object. The reservation's "popcnt" tracks the number of these
+ * small physical pages that are in use at any given time. When and if the
+ * reservation is not fully utilized, it appears in the queue of partially-
+ * populated reservations. The reservation always appears on the containing
+ * object's list of reservations.
+ *
+ * A partially-populated reservation can be broken and reclaimed at any time.
+ */
+struct vm_reserv {
+ TAILQ_ENTRY(vm_reserv) partpopq;
+ LIST_ENTRY(vm_reserv) objq;
+ vm_object_t object; /* containing object */
+ vm_pindex_t pindex; /* offset within object */
+ vm_page_t pages; /* first page of a superpage */
+ int popcnt; /* # of pages in use */
+ char inpartpopq;
+};
+
+/*
+ * The reservation array
+ *
+ * This array is analoguous in function to vm_page_array. It differs in the
+ * respect that it may contain a greater number of useful reservation
+ * structures than there are (physical) superpages. These "invalid"
+ * reservation structures exist to trade-off space for time in the
+ * implementation of vm_reserv_from_page(). Invalid reservation structures are
+ * distinguishable from "valid" reservation structures by inspecting the
+ * reservation's "pages" field. Invalid reservation structures have a NULL
+ * "pages" field.
+ *
+ * vm_reserv_from_page() maps a small (physical) page to an element of this
+ * array by computing a physical reservation number from the page's physical
+ * address. The physical reservation number is used as the array index.
+ *
+ * An "active" reservation is a valid reservation structure that has a non-NULL
+ * "object" field and a non-zero "popcnt" field. In other words, every active
+ * reservation belongs to a particular object. Moreover, every active
+ * reservation has an entry in the containing object's list of reservations.
+ */
+static vm_reserv_t vm_reserv_array;
+
+/*
+ * The partially-populated reservation queue
+ *
+ * This queue enables the fast recovery of an unused cached or free small page
+ * from a partially-populated reservation. The head of this queue is either
+ * the least-recently-populated or most-recently-depopulated reservation.
+ *
+ * Access to this queue is synchronized by the free page queue lock.
+ */
+static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop =
+ TAILQ_HEAD_INITIALIZER(vm_rvq_partpop);
+
+static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info");
+
+static long vm_reserv_broken;
+SYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD,
+ &vm_reserv_broken, 0, "Cumulative number of broken reservations");
+
+static long vm_reserv_freed;
+SYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD,
+ &vm_reserv_freed, 0, "Cumulative number of freed reservations");
+
+static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS);
+
+SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
+ sysctl_vm_reserv_partpopq, "A", "Partially-populated reservation queues");
+
+static long vm_reserv_reclaimed;
+SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD,
+ &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations");
+
+static void vm_reserv_depopulate(vm_reserv_t rv);
+static vm_reserv_t vm_reserv_from_page(vm_page_t m);
+static boolean_t vm_reserv_has_pindex(vm_reserv_t rv,
+ vm_pindex_t pindex);
+static void vm_reserv_populate(vm_reserv_t rv);
+
+/*
+ * Describes the current state of the partially-populated reservation queue.
+ */
+static int
+sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS)
+{
+ struct sbuf sbuf;
+ vm_reserv_t rv;
+ char *cbuf;
+ const int cbufsize = (VM_NRESERVLEVEL + 1) * 81;
+ int counter, error, level, unused_pages;
+
+ cbuf = malloc(cbufsize, M_TEMP, M_WAITOK | M_ZERO);
+ sbuf_new(&sbuf, cbuf, cbufsize, SBUF_FIXEDLEN);
+ sbuf_printf(&sbuf, "\nLEVEL SIZE NUMBER\n\n");
+ for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) {
+ counter = 0;
+ unused_pages = 0;
+ mtx_lock(&vm_page_queue_free_mtx);
+ TAILQ_FOREACH(rv, &vm_rvq_partpop/*[level]*/, partpopq) {
+ counter++;
+ unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt;
+ }
+ mtx_unlock(&vm_page_queue_free_mtx);
+ sbuf_printf(&sbuf, "%5.5d: %6.6dK, %6.6d\n", level,
+ unused_pages * (PAGE_SIZE / 1024), counter);
+ }
+ sbuf_finish(&sbuf);
+ error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
+ sbuf_delete(&sbuf);
+ free(cbuf, M_TEMP);
+ return (error);
+}
+
+/*
+ * Reduces the given reservation's population count. If the population count
+ * becomes zero, the reservation is destroyed. Additionally, moves the
+ * reservation to the head of the partially-populated reservations queue if the
+ * population count is non-zero.
+ *
+ * The free page queue lock must be held.
+ */
+static void
+vm_reserv_depopulate(vm_reserv_t rv)
+{
+
+ mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
+ KASSERT(rv->object != NULL,
+ ("vm_reserv_depopulate: reserv %p is free", rv));
+ KASSERT(rv->popcnt > 0,
+ ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv));
+ if (rv->inpartpopq) {
+ TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
+ rv->inpartpopq = FALSE;
+ }
+ rv->popcnt--;
+ if (rv->popcnt == 0) {
+ LIST_REMOVE(rv, objq);
+ rv->object = NULL;
+ vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER);
+ vm_reserv_freed++;
+ } else {
+ rv->inpartpopq = TRUE;
+ TAILQ_INSERT_HEAD(&vm_rvq_partpop, rv, partpopq);
+ }
+}
+
+/*
+ * Returns the reservation to which the given page might belong.
+ */
+static __inline vm_reserv_t
+vm_reserv_from_page(vm_page_t m)
+{
+
+ return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]);
+}
+
+/*
+ * Returns TRUE if the given reservation contains the given page index and
+ * FALSE otherwise.
+ */
+static __inline boolean_t
+vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex)
+{
+
+ return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0);
+}
+
+/*
+ * Increases the given reservation's population count. Moves the reservation
+ * to the tail of the partially-populated reservation queue.
+ *
+ * The free page queue must be locked.
+ */
+static void
+vm_reserv_populate(vm_reserv_t rv)
+{
+
+ mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
+ KASSERT(rv->object != NULL,
+ ("vm_reserv_populate: reserv %p is free", rv));
+ KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES,
+ ("vm_reserv_populate: reserv %p is already full", rv));
+ if (rv->inpartpopq) {
+ TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
+ rv->inpartpopq = FALSE;
+ }
+ rv->popcnt++;
+ if (rv->popcnt < VM_LEVEL_0_NPAGES) {
+ rv->inpartpopq = TRUE;
+ TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq);
+ }
+}
+
+/*
+ * Allocates a page from an existing or newly-created reservation.
+ *
+ * The object and free page queue must be locked.
+ */
+vm_page_t
+vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex)
+{
+ vm_page_t m, mpred, msucc;
+ vm_pindex_t first, leftcap, rightcap;
+ vm_reserv_t rv;
+
+ mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
+
+ /*
+ * Is a reservation fundamentally not possible?
+ */
+ VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
+ if (pindex < VM_RESERV_INDEX(object, pindex) ||
+ pindex >= object->size)
+ return (NULL);
+
+ /*
+ * Look for an existing reservation.
+ */
+ msucc = NULL;
+ mpred = object->root;
+ while (mpred != NULL) {
+ KASSERT(mpred->pindex != pindex,
+ ("vm_reserv_alloc_page: pindex already allocated"));
+ rv = vm_reserv_from_page(mpred);
+ if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) {
+ m = &rv->pages[VM_RESERV_INDEX(object, pindex)];
+ /* Handle vm_page_rename(m, new_object, ...). */
+ if ((m->flags & (PG_CACHED | PG_FREE)) == 0)
+ return (NULL);
+ vm_reserv_populate(rv);
+ return (m);
+ } else if (mpred->pindex < pindex) {
+ if (msucc != NULL ||
+ (msucc = TAILQ_NEXT(mpred, listq)) == NULL)
+ break;
+ KASSERT(msucc->pindex != pindex,
+ ("vm_reserv_alloc_page: pindex already allocated"));
+ rv = vm_reserv_from_page(msucc);
+ if (rv->object == object &&
+ vm_reserv_has_pindex(rv, pindex)) {
+ m = &rv->pages[VM_RESERV_INDEX(object, pindex)];
+ /* Handle vm_page_rename(m, new_object, ...). */
+ if ((m->flags & (PG_CACHED | PG_FREE)) == 0)
+ return (NULL);
+ vm_reserv_populate(rv);
+ return (m);
+ } else if (pindex < msucc->pindex)
+ break;
+ } else if (msucc == NULL) {
+ msucc = mpred;
+ mpred = TAILQ_PREV(msucc, pglist, listq);
+ continue;
+ }
+ msucc = NULL;
+ mpred = object->root = vm_page_splay(pindex, object->root);
+ }
+
+ /*
+ * Determine the first index to the left that can be used.
+ */
+ if (mpred == NULL)
+ leftcap = 0;
+ else if ((rv = vm_reserv_from_page(mpred))->object != object)
+ leftcap = mpred->pindex + 1;
+ else
+ leftcap = rv->pindex + VM_LEVEL_0_NPAGES;
+
+ /*
+ * Determine the first index to the right that cannot be used.
+ */
+ if (msucc == NULL)
+ rightcap = pindex + VM_LEVEL_0_NPAGES;
+ else if ((rv = vm_reserv_from_page(msucc))->object != object)
+ rightcap = msucc->pindex;
+ else
+ rightcap = rv->pindex;
+
+ /*
+ * Determine if a reservation fits between the first index to
+ * the left that can be used and the first index to the right
+ * that cannot be used.
+ */
+ first = pindex - VM_RESERV_INDEX(object, pindex);
+ if (first < leftcap || first + VM_LEVEL_0_NPAGES > rightcap)
+ return (NULL);
+
+ /*
+ * Would a new reservation extend past the end of the given object?
+ */
+ if (object->size < first + VM_LEVEL_0_NPAGES) {
+ /*
+ * Don't allocate a new reservation if the object is a vnode or
+ * backed by another object that is a vnode.
+ */
+ if (object->type == OBJT_VNODE ||
+ (object->backing_object != NULL &&
+ object->backing_object->type == OBJT_VNODE))
+ return (NULL);
+ /* Speculate that the object may grow. */
+ }
+
+ /*
+ * Allocate a new reservation.
+ */
+ m = vm_phys_alloc_pages(VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER);
+ if (m != NULL) {
+ rv = vm_reserv_from_page(m);
+ KASSERT(rv->pages == m,
+ ("vm_reserv_alloc_page: reserv %p's pages is corrupted",
+ rv));
+ KASSERT(rv->object == NULL,
+ ("vm_reserv_alloc_page: reserv %p isn't free", rv));
+ LIST_INSERT_HEAD(&object->rvq, rv, objq);
+ rv->object = object;
+ rv->pindex = first;
+ KASSERT(rv->popcnt == 0,
+ ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted",
+ rv));
+ KASSERT(!rv->inpartpopq,
+ ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE",
+ rv));
+ vm_reserv_populate(rv);
+ m = &rv->pages[VM_RESERV_INDEX(object, pindex)];
+ }
+ return (m);
+}
+
+/*
+ * Breaks all reservations belonging to the given object.
+ */
+void
+vm_reserv_break_all(vm_object_t object)
+{
+ vm_reserv_t rv;
+ int i;
+
+ mtx_lock(&vm_page_queue_free_mtx);
+ while ((rv = LIST_FIRST(&object->rvq)) != NULL) {
+ KASSERT(rv->object == object,
+ ("vm_reserv_break_all: reserv %p is corrupted", rv));
+ if (rv->inpartpopq) {
+ TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
+ rv->inpartpopq = FALSE;
+ }
+ LIST_REMOVE(rv, objq);
+ rv->object = NULL;
+ for (i = 0; i < VM_LEVEL_0_NPAGES; i++) {
+ if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
+ vm_phys_free_pages(&rv->pages[i], 0);
+ else
+ rv->popcnt--;
+ }
+ KASSERT(rv->popcnt == 0,
+ ("vm_reserv_break_all: reserv %p's popcnt is corrupted",
+ rv));
+ vm_reserv_broken++;
+ }
+ mtx_unlock(&vm_page_queue_free_mtx);
+}
+
+/*
+ * Frees the given page if it belongs to a reservation. Returns TRUE if the
+ * page is freed and FALSE otherwise.
+ *
+ * The free page queue lock must be held.
+ */
+boolean_t
+vm_reserv_free_page(vm_page_t m)
+{
+ vm_reserv_t rv;
+
+ mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
+ rv = vm_reserv_from_page(m);
+ if (rv->object != NULL) {
+ vm_reserv_depopulate(rv);
+ return (TRUE);
+ }
+ return (FALSE);
+}
+
+/*
+ * Initializes the reservation management system. Specifically, initializes
+ * the reservation array.
+ *
+ * Requires that vm_page_array and first_page are initialized!
+ */
+void
+vm_reserv_init(void)
+{
+ vm_paddr_t paddr;
+ int i;
+
+ /*
+ * Initialize the reservation array. Specifically, initialize the
+ * "pages" field for every element that has an underlying superpage.
+ */
+ for (i = 0; phys_avail[i + 1] != 0; i += 2) {
+ paddr = roundup2(phys_avail[i], VM_LEVEL_0_SIZE);
+ while (paddr + VM_LEVEL_0_SIZE <= phys_avail[i + 1]) {
+ vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages =
+ PHYS_TO_VM_PAGE(paddr);
+ paddr += VM_LEVEL_0_SIZE;
+ }
+ }
+}
+
+/*
+ * Returns a reservation level if the given page belongs to a fully-populated
+ * reservation and -1 otherwise.
+ */
+int
+vm_reserv_level_iffullpop(vm_page_t m)
+{
+ vm_reserv_t rv;
+
+ rv = vm_reserv_from_page(m);
+ return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1);
+}
+
+/*
+ * Prepare for the reactivation of a cached page.
+ *
+ * First, suppose that the given page "m" was allocated individually, i.e., not
+ * as part of a reservation, and cached. Then, suppose a reservation
+ * containing "m" is allocated by the same object. Although "m" and the
+ * reservation belong to the same object, "m"'s pindex may not match the
+ * reservation's.
+ *
+ * The free page queue must be locked.
+ */
+boolean_t
+vm_reserv_reactivate_page(vm_page_t m)
+{
+ vm_reserv_t rv;
+ int i, m_index;
+
+ mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
+ rv = vm_reserv_from_page(m);
+ if (rv->object == NULL)
+ return (FALSE);
+ KASSERT((m->flags & PG_CACHED) != 0,
+ ("vm_reserv_uncache_page: page %p is not cached", m));
+ if (m->object == rv->object &&
+ m->pindex - rv->pindex == VM_RESERV_INDEX(m->object, m->pindex))
+ vm_reserv_populate(rv);
+ else {
+ KASSERT(rv->inpartpopq,
+ ("vm_reserv_uncache_page: reserv %p's inpartpopq is FALSE",
+ rv));
+ TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
+ rv->inpartpopq = FALSE;
+ LIST_REMOVE(rv, objq);
+ rv->object = NULL;
+ /* Don't vm_phys_free_pages(m, 0). */
+ m_index = m - rv->pages;
+ for (i = 0; i < m_index; i++) {
+ if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
+ vm_phys_free_pages(&rv->pages[i], 0);
+ else
+ rv->popcnt--;
+ }
+ for (i++; i < VM_LEVEL_0_NPAGES; i++) {
+ if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
+ vm_phys_free_pages(&rv->pages[i], 0);
+ else
+ rv->popcnt--;
+ }
+ KASSERT(rv->popcnt == 0,
+ ("vm_reserv_uncache_page: reserv %p's popcnt is corrupted",
+ rv));
+ vm_reserv_broken++;
+ }
+ return (TRUE);
+}
+
+/*
+ * Breaks the reservation at the head of the partially-populated reservation
+ * queue, releasing its cached and free pages to the physical memory
+ * allocator. Returns TRUE if a reservation is broken and FALSE otherwise.
+ *
+ * The free page queue lock must be held.
+ */
+boolean_t
+vm_reserv_reclaim(void)
+{
+ vm_reserv_t rv;
+ int i;
+
+ mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
+ if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) {
+ KASSERT(rv->inpartpopq,
+ ("vm_reserv_reclaim: reserv %p's inpartpopq is corrupted",
+ rv));
+ TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq);
+ rv->inpartpopq = FALSE;
+ KASSERT(rv->object != NULL,
+ ("vm_reserv_reclaim: reserv %p is free", rv));
+ LIST_REMOVE(rv, objq);
+ rv->object = NULL;
+ for (i = 0; i < VM_LEVEL_0_NPAGES; i++) {
+ if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0)
+ vm_phys_free_pages(&rv->pages[i], 0);
+ else
+ rv->popcnt--;
+ }
+ KASSERT(rv->popcnt == 0,
+ ("vm_reserv_reclaim: reserv %p's popcnt is corrupted",
+ rv));
+ vm_reserv_reclaimed++;
+ return (TRUE);
+ }
+ return (FALSE);
+}
+
+/*
+ * Transfers the reservation underlying the given page to a new object.
+ *
+ * The object must be locked.
+ */
+void
+vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object,
+ vm_pindex_t old_object_offset)
+{
+ vm_reserv_t rv;
+
+ VM_OBJECT_LOCK_ASSERT(new_object, MA_OWNED);
+ rv = vm_reserv_from_page(m);
+ if (rv->object == old_object) {
+ mtx_lock(&vm_page_queue_free_mtx);
+ if (rv->object == old_object) {
+ LIST_REMOVE(rv, objq);
+ LIST_INSERT_HEAD(&new_object->rvq, rv, objq);
+ rv->object = new_object;
+ rv->pindex -= old_object_offset;
+ }
+ mtx_unlock(&vm_page_queue_free_mtx);
+ }
+}
+
+/*
+ * Allocates the virtual and physical memory required by the reservation
+ * management system's data structures, in particular, the reservation array.
+ */
+vm_paddr_t
+vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water)
+{
+ vm_paddr_t new_end;
+ size_t size;
+
+ /*
+ * Calculate the size (in bytes) of the reservation array. Round up
+ * from "high_water" because every small page is mapped to an element
+ * in the reservation array based on its physical address. Thus, the
+ * number of elements in the reservation array can be greater than the
+ * number of superpages.
+ */
+ size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv);
+
+ /*
+ * Allocate and map the physical memory for the reservation array. The
+ * next available virtual address is returned by reference.
+ */
+ new_end = end - round_page(size);
+ vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end,
+ VM_PROT_READ | VM_PROT_WRITE);
+ bzero(vm_reserv_array, size);
+
+ /*
+ * Return the next available physical address.
+ */
+ return (new_end);
+}
+
+#endif /* VM_NRESERVLEVEL > 0 */
diff --git a/sys/vm/vm_reserv.h b/sys/vm/vm_reserv.h
new file mode 100644
index 0000000..7cf814b
--- /dev/null
+++ b/sys/vm/vm_reserv.h
@@ -0,0 +1,59 @@
+/*-
+ * Copyright (c) 2002-2006 Rice University
+ * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu>
+ * All rights reserved.
+ *
+ * This software was developed for the FreeBSD Project by Alan L. Cox,
+ * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro.
+ *
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
+ * HOLDERS 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.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * Superpage reservation management definitions
+ */
+
+#ifndef _VM_RESERV_H_
+#define _VM_RESERV_H_
+
+#ifdef _KERNEL
+
+#if VM_NRESERVLEVEL > 0
+
+vm_page_t vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex);
+void vm_reserv_break_all(vm_object_t object);
+boolean_t vm_reserv_free_page(vm_page_t m);
+void vm_reserv_init(void);
+int vm_reserv_level_iffullpop(vm_page_t m);
+boolean_t vm_reserv_reactivate_page(vm_page_t m);
+boolean_t vm_reserv_reclaim(void);
+void vm_reserv_rename(vm_page_t m, vm_object_t new_object,
+ vm_object_t old_object, vm_pindex_t old_object_offset);
+vm_paddr_t vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end,
+ vm_paddr_t high_water);
+
+#endif /* VM_NRESERVLEVEL > 0 */
+#endif /* _KERNEL */
+#endif /* !_VM_RESERV_H_ */
OpenPOWER on IntegriCloud