1f8a47341SAlan Cox /*- 2f8a47341SAlan Cox * Copyright (c) 2002-2006 Rice University 3ec179322SAlan Cox * Copyright (c) 2007-2011 Alan L. Cox <alc@cs.rice.edu> 4f8a47341SAlan Cox * All rights reserved. 5f8a47341SAlan Cox * 6f8a47341SAlan Cox * This software was developed for the FreeBSD Project by Alan L. Cox, 7f8a47341SAlan Cox * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro. 8f8a47341SAlan Cox * 9f8a47341SAlan Cox * Redistribution and use in source and binary forms, with or without 10f8a47341SAlan Cox * modification, are permitted provided that the following conditions 11f8a47341SAlan Cox * are met: 12f8a47341SAlan Cox * 1. Redistributions of source code must retain the above copyright 13f8a47341SAlan Cox * notice, this list of conditions and the following disclaimer. 14f8a47341SAlan Cox * 2. Redistributions in binary form must reproduce the above copyright 15f8a47341SAlan Cox * notice, this list of conditions and the following disclaimer in the 16f8a47341SAlan Cox * documentation and/or other materials provided with the distribution. 17f8a47341SAlan Cox * 18f8a47341SAlan Cox * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19f8a47341SAlan Cox * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20f8a47341SAlan Cox * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21f8a47341SAlan Cox * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22f8a47341SAlan Cox * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23f8a47341SAlan Cox * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24f8a47341SAlan Cox * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 25f8a47341SAlan Cox * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26f8a47341SAlan Cox * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27f8a47341SAlan Cox * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 28f8a47341SAlan Cox * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29f8a47341SAlan Cox * POSSIBILITY OF SUCH DAMAGE. 30f8a47341SAlan Cox */ 31f8a47341SAlan Cox 32f8a47341SAlan Cox /* 33f8a47341SAlan Cox * Superpage reservation management module 34c68c3537SAlan Cox * 35c68c3537SAlan Cox * Any external functions defined by this module are only to be used by the 36c68c3537SAlan Cox * virtual memory system. 37f8a47341SAlan Cox */ 38f8a47341SAlan Cox 39f8a47341SAlan Cox #include <sys/cdefs.h> 40f8a47341SAlan Cox __FBSDID("$FreeBSD$"); 41f8a47341SAlan Cox 42f8a47341SAlan Cox #include "opt_vm.h" 43f8a47341SAlan Cox 44f8a47341SAlan Cox #include <sys/param.h> 45f8a47341SAlan Cox #include <sys/kernel.h> 46f8a47341SAlan Cox #include <sys/lock.h> 47f8a47341SAlan Cox #include <sys/malloc.h> 48f8a47341SAlan Cox #include <sys/mutex.h> 49f8a47341SAlan Cox #include <sys/queue.h> 5089f6b863SAttilio Rao #include <sys/rwlock.h> 51f8a47341SAlan Cox #include <sys/sbuf.h> 52f8a47341SAlan Cox #include <sys/sysctl.h> 53f8a47341SAlan Cox #include <sys/systm.h> 54f8a47341SAlan Cox 55f8a47341SAlan Cox #include <vm/vm.h> 56f8a47341SAlan Cox #include <vm/vm_param.h> 57f8a47341SAlan Cox #include <vm/vm_object.h> 58f8a47341SAlan Cox #include <vm/vm_page.h> 59f8a47341SAlan Cox #include <vm/vm_phys.h> 60774d251dSAttilio Rao #include <vm/vm_radix.h> 61f8a47341SAlan Cox #include <vm/vm_reserv.h> 62f8a47341SAlan Cox 63f8a47341SAlan Cox /* 64f8a47341SAlan Cox * The reservation system supports the speculative allocation of large physical 65f8a47341SAlan Cox * pages ("superpages"). Speculative allocation enables the fully-automatic 66f8a47341SAlan Cox * utilization of superpages by the virtual memory system. In other words, no 67f8a47341SAlan Cox * programmatic directives are required to use superpages. 68f8a47341SAlan Cox */ 69f8a47341SAlan Cox 70f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 71f8a47341SAlan Cox 72f8a47341SAlan Cox /* 73f8a47341SAlan Cox * The number of small pages that are contained in a level 0 reservation 74f8a47341SAlan Cox */ 75f8a47341SAlan Cox #define VM_LEVEL_0_NPAGES (1 << VM_LEVEL_0_ORDER) 76f8a47341SAlan Cox 77f8a47341SAlan Cox /* 78f8a47341SAlan Cox * The number of bits by which a physical address is shifted to obtain the 79f8a47341SAlan Cox * reservation number 80f8a47341SAlan Cox */ 81f8a47341SAlan Cox #define VM_LEVEL_0_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT) 82f8a47341SAlan Cox 83f8a47341SAlan Cox /* 84f8a47341SAlan Cox * The size of a level 0 reservation in bytes 85f8a47341SAlan Cox */ 86f8a47341SAlan Cox #define VM_LEVEL_0_SIZE (1 << VM_LEVEL_0_SHIFT) 87f8a47341SAlan Cox 88f8a47341SAlan Cox /* 89f8a47341SAlan Cox * Computes the index of the small page underlying the given (object, pindex) 90f8a47341SAlan Cox * within the reservation's array of small pages. 91f8a47341SAlan Cox */ 92f8a47341SAlan Cox #define VM_RESERV_INDEX(object, pindex) \ 93f8a47341SAlan Cox (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1)) 94f8a47341SAlan Cox 95f8a47341SAlan Cox /* 96ec179322SAlan Cox * The size of a population map entry 97ec179322SAlan Cox */ 98ec179322SAlan Cox typedef u_long popmap_t; 99ec179322SAlan Cox 100ec179322SAlan Cox /* 101ec179322SAlan Cox * The number of bits in a population map entry 102ec179322SAlan Cox */ 103ec179322SAlan Cox #define NBPOPMAP (NBBY * sizeof(popmap_t)) 104ec179322SAlan Cox 105ec179322SAlan Cox /* 106ec179322SAlan Cox * The number of population map entries in a reservation 107ec179322SAlan Cox */ 108ec179322SAlan Cox #define NPOPMAP howmany(VM_LEVEL_0_NPAGES, NBPOPMAP) 109ec179322SAlan Cox 110ec179322SAlan Cox /* 1113180f757SAlan Cox * Clear a bit in the population map. 1123180f757SAlan Cox */ 1133180f757SAlan Cox static __inline void 1143180f757SAlan Cox popmap_clear(popmap_t popmap[], int i) 1153180f757SAlan Cox { 1163180f757SAlan Cox 1173180f757SAlan Cox popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP)); 1183180f757SAlan Cox } 1193180f757SAlan Cox 1203180f757SAlan Cox /* 1213180f757SAlan Cox * Set a bit in the population map. 1223180f757SAlan Cox */ 1233180f757SAlan Cox static __inline void 1243180f757SAlan Cox popmap_set(popmap_t popmap[], int i) 1253180f757SAlan Cox { 1263180f757SAlan Cox 1273180f757SAlan Cox popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP); 1283180f757SAlan Cox } 1293180f757SAlan Cox 1303180f757SAlan Cox /* 1313180f757SAlan Cox * Is a bit in the population map clear? 1323180f757SAlan Cox */ 1333180f757SAlan Cox static __inline boolean_t 1343180f757SAlan Cox popmap_is_clear(popmap_t popmap[], int i) 1353180f757SAlan Cox { 1363180f757SAlan Cox 1373180f757SAlan Cox return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0); 1383180f757SAlan Cox } 1393180f757SAlan Cox 1403180f757SAlan Cox /* 1413180f757SAlan Cox * Is a bit in the population map set? 1423180f757SAlan Cox */ 1433180f757SAlan Cox static __inline boolean_t 1443180f757SAlan Cox popmap_is_set(popmap_t popmap[], int i) 1453180f757SAlan Cox { 1463180f757SAlan Cox 1473180f757SAlan Cox return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0); 1483180f757SAlan Cox } 1493180f757SAlan Cox 1503180f757SAlan Cox /* 151f8a47341SAlan Cox * The reservation structure 152f8a47341SAlan Cox * 153f8a47341SAlan Cox * A reservation structure is constructed whenever a large physical page is 154f8a47341SAlan Cox * speculatively allocated to an object. The reservation provides the small 155f8a47341SAlan Cox * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets 156f8a47341SAlan Cox * within that object. The reservation's "popcnt" tracks the number of these 157f8a47341SAlan Cox * small physical pages that are in use at any given time. When and if the 158f8a47341SAlan Cox * reservation is not fully utilized, it appears in the queue of partially- 159f8a47341SAlan Cox * populated reservations. The reservation always appears on the containing 160f8a47341SAlan Cox * object's list of reservations. 161f8a47341SAlan Cox * 162f8a47341SAlan Cox * A partially-populated reservation can be broken and reclaimed at any time. 163f8a47341SAlan Cox */ 164f8a47341SAlan Cox struct vm_reserv { 165f8a47341SAlan Cox TAILQ_ENTRY(vm_reserv) partpopq; 166f8a47341SAlan Cox LIST_ENTRY(vm_reserv) objq; 167f8a47341SAlan Cox vm_object_t object; /* containing object */ 168f8a47341SAlan Cox vm_pindex_t pindex; /* offset within object */ 169f8a47341SAlan Cox vm_page_t pages; /* first page of a superpage */ 170f8a47341SAlan Cox int popcnt; /* # of pages in use */ 171f8a47341SAlan Cox char inpartpopq; 172ec179322SAlan Cox popmap_t popmap[NPOPMAP]; /* bit vector of used pages */ 173f8a47341SAlan Cox }; 174f8a47341SAlan Cox 175f8a47341SAlan Cox /* 176f8a47341SAlan Cox * The reservation array 177f8a47341SAlan Cox * 178f8a47341SAlan Cox * This array is analoguous in function to vm_page_array. It differs in the 179f8a47341SAlan Cox * respect that it may contain a greater number of useful reservation 180f8a47341SAlan Cox * structures than there are (physical) superpages. These "invalid" 181f8a47341SAlan Cox * reservation structures exist to trade-off space for time in the 182f8a47341SAlan Cox * implementation of vm_reserv_from_page(). Invalid reservation structures are 183f8a47341SAlan Cox * distinguishable from "valid" reservation structures by inspecting the 184f8a47341SAlan Cox * reservation's "pages" field. Invalid reservation structures have a NULL 185f8a47341SAlan Cox * "pages" field. 186f8a47341SAlan Cox * 187f8a47341SAlan Cox * vm_reserv_from_page() maps a small (physical) page to an element of this 188f8a47341SAlan Cox * array by computing a physical reservation number from the page's physical 189f8a47341SAlan Cox * address. The physical reservation number is used as the array index. 190f8a47341SAlan Cox * 191f8a47341SAlan Cox * An "active" reservation is a valid reservation structure that has a non-NULL 192f8a47341SAlan Cox * "object" field and a non-zero "popcnt" field. In other words, every active 193f8a47341SAlan Cox * reservation belongs to a particular object. Moreover, every active 194f8a47341SAlan Cox * reservation has an entry in the containing object's list of reservations. 195f8a47341SAlan Cox */ 196f8a47341SAlan Cox static vm_reserv_t vm_reserv_array; 197f8a47341SAlan Cox 198f8a47341SAlan Cox /* 199f8a47341SAlan Cox * The partially-populated reservation queue 200f8a47341SAlan Cox * 201f8a47341SAlan Cox * This queue enables the fast recovery of an unused cached or free small page 202ab5378cfSAlan Cox * from a partially-populated reservation. The reservation at the head of 203ab5378cfSAlan Cox * this queue is the least-recently-changed, partially-populated reservation. 204f8a47341SAlan Cox * 205f8a47341SAlan Cox * Access to this queue is synchronized by the free page queue lock. 206f8a47341SAlan Cox */ 207f8a47341SAlan Cox static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop = 208f8a47341SAlan Cox TAILQ_HEAD_INITIALIZER(vm_rvq_partpop); 209f8a47341SAlan Cox 210f8a47341SAlan Cox static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info"); 211f8a47341SAlan Cox 212f8a47341SAlan Cox static long vm_reserv_broken; 213f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD, 214f8a47341SAlan Cox &vm_reserv_broken, 0, "Cumulative number of broken reservations"); 215f8a47341SAlan Cox 216f8a47341SAlan Cox static long vm_reserv_freed; 217f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD, 218f8a47341SAlan Cox &vm_reserv_freed, 0, "Cumulative number of freed reservations"); 219f8a47341SAlan Cox 220e0a63baaSAlan Cox static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS); 221e0a63baaSAlan Cox 222e0a63baaSAlan Cox SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_RD, NULL, 0, 223e0a63baaSAlan Cox sysctl_vm_reserv_fullpop, "I", "Current number of full reservations"); 224e0a63baaSAlan Cox 225f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS); 226f8a47341SAlan Cox 227f8a47341SAlan Cox SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, 228f8a47341SAlan Cox sysctl_vm_reserv_partpopq, "A", "Partially-populated reservation queues"); 229f8a47341SAlan Cox 230f8a47341SAlan Cox static long vm_reserv_reclaimed; 231f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD, 232f8a47341SAlan Cox &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations"); 233f8a47341SAlan Cox 234ec179322SAlan Cox static void vm_reserv_break(vm_reserv_t rv, vm_page_t m); 235ec179322SAlan Cox static void vm_reserv_depopulate(vm_reserv_t rv, int index); 236f8a47341SAlan Cox static vm_reserv_t vm_reserv_from_page(vm_page_t m); 237f8a47341SAlan Cox static boolean_t vm_reserv_has_pindex(vm_reserv_t rv, 238f8a47341SAlan Cox vm_pindex_t pindex); 239ec179322SAlan Cox static void vm_reserv_populate(vm_reserv_t rv, int index); 24044aab2c3SAlan Cox static void vm_reserv_reclaim(vm_reserv_t rv); 241f8a47341SAlan Cox 242f8a47341SAlan Cox /* 243e0a63baaSAlan Cox * Returns the current number of full reservations. 244e0a63baaSAlan Cox * 245e0a63baaSAlan Cox * Since the number of full reservations is computed without acquiring the 246e0a63baaSAlan Cox * free page queue lock, the returned value may be inexact. 247e0a63baaSAlan Cox */ 248e0a63baaSAlan Cox static int 249e0a63baaSAlan Cox sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS) 250e0a63baaSAlan Cox { 251e0a63baaSAlan Cox vm_paddr_t paddr; 252e0a63baaSAlan Cox struct vm_phys_seg *seg; 253e0a63baaSAlan Cox vm_reserv_t rv; 254e0a63baaSAlan Cox int fullpop, segind; 255e0a63baaSAlan Cox 256e0a63baaSAlan Cox fullpop = 0; 257e0a63baaSAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 258e0a63baaSAlan Cox seg = &vm_phys_segs[segind]; 259e0a63baaSAlan Cox paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 260e0a63baaSAlan Cox while (paddr + VM_LEVEL_0_SIZE <= seg->end) { 261e0a63baaSAlan Cox rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT]; 262e0a63baaSAlan Cox fullpop += rv->popcnt == VM_LEVEL_0_NPAGES; 263e0a63baaSAlan Cox paddr += VM_LEVEL_0_SIZE; 264e0a63baaSAlan Cox } 265e0a63baaSAlan Cox } 266e0a63baaSAlan Cox return (sysctl_handle_int(oidp, &fullpop, 0, req)); 267e0a63baaSAlan Cox } 268e0a63baaSAlan Cox 269e0a63baaSAlan Cox /* 270f8a47341SAlan Cox * Describes the current state of the partially-populated reservation queue. 271f8a47341SAlan Cox */ 272f8a47341SAlan Cox static int 273f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS) 274f8a47341SAlan Cox { 275f8a47341SAlan Cox struct sbuf sbuf; 276f8a47341SAlan Cox vm_reserv_t rv; 277f8a47341SAlan Cox int counter, error, level, unused_pages; 278f8a47341SAlan Cox 27900f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 28000f0e671SMatthew D Fleming if (error != 0) 28100f0e671SMatthew D Fleming return (error); 2824e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 283f8a47341SAlan Cox sbuf_printf(&sbuf, "\nLEVEL SIZE NUMBER\n\n"); 284f8a47341SAlan Cox for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) { 285f8a47341SAlan Cox counter = 0; 286f8a47341SAlan Cox unused_pages = 0; 287f8a47341SAlan Cox mtx_lock(&vm_page_queue_free_mtx); 288f8a47341SAlan Cox TAILQ_FOREACH(rv, &vm_rvq_partpop/*[level]*/, partpopq) { 289f8a47341SAlan Cox counter++; 290f8a47341SAlan Cox unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt; 291f8a47341SAlan Cox } 292f8a47341SAlan Cox mtx_unlock(&vm_page_queue_free_mtx); 293d689bc00SAlan Cox sbuf_printf(&sbuf, "%5d: %6dK, %6d\n", level, 2942cf36c8fSAlan Cox unused_pages * ((int)PAGE_SIZE / 1024), counter); 295f8a47341SAlan Cox } 2964e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 297f8a47341SAlan Cox sbuf_delete(&sbuf); 298f8a47341SAlan Cox return (error); 299f8a47341SAlan Cox } 300f8a47341SAlan Cox 301f8a47341SAlan Cox /* 302f8a47341SAlan Cox * Reduces the given reservation's population count. If the population count 303f8a47341SAlan Cox * becomes zero, the reservation is destroyed. Additionally, moves the 304ec179322SAlan Cox * reservation to the tail of the partially-populated reservation queue if the 305f8a47341SAlan Cox * population count is non-zero. 306f8a47341SAlan Cox * 307f8a47341SAlan Cox * The free page queue lock must be held. 308f8a47341SAlan Cox */ 309f8a47341SAlan Cox static void 310ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index) 311f8a47341SAlan Cox { 312f8a47341SAlan Cox 313f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 314f8a47341SAlan Cox KASSERT(rv->object != NULL, 315f8a47341SAlan Cox ("vm_reserv_depopulate: reserv %p is free", rv)); 3163180f757SAlan Cox KASSERT(popmap_is_set(rv->popmap, index), 317a08c1515SAlan Cox ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv, 318a08c1515SAlan Cox index)); 319f8a47341SAlan Cox KASSERT(rv->popcnt > 0, 320f8a47341SAlan Cox ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv)); 321f8a47341SAlan Cox if (rv->inpartpopq) { 322f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 323f8a47341SAlan Cox rv->inpartpopq = FALSE; 324dd05fa19SAlan Cox } else { 325dd05fa19SAlan Cox KASSERT(rv->pages->psind == 1, 326dd05fa19SAlan Cox ("vm_reserv_depopulate: reserv %p is already demoted", 327dd05fa19SAlan Cox rv)); 328dd05fa19SAlan Cox rv->pages->psind = 0; 329f8a47341SAlan Cox } 3303180f757SAlan Cox popmap_clear(rv->popmap, index); 331f8a47341SAlan Cox rv->popcnt--; 332f8a47341SAlan Cox if (rv->popcnt == 0) { 333f8a47341SAlan Cox LIST_REMOVE(rv, objq); 334f8a47341SAlan Cox rv->object = NULL; 335f8a47341SAlan Cox vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER); 336f8a47341SAlan Cox vm_reserv_freed++; 337f8a47341SAlan Cox } else { 338f8a47341SAlan Cox rv->inpartpopq = TRUE; 339ab5378cfSAlan Cox TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq); 340f8a47341SAlan Cox } 341f8a47341SAlan Cox } 342f8a47341SAlan Cox 343f8a47341SAlan Cox /* 344f8a47341SAlan Cox * Returns the reservation to which the given page might belong. 345f8a47341SAlan Cox */ 346f8a47341SAlan Cox static __inline vm_reserv_t 347f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m) 348f8a47341SAlan Cox { 349f8a47341SAlan Cox 350f8a47341SAlan Cox return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]); 351f8a47341SAlan Cox } 352f8a47341SAlan Cox 353f8a47341SAlan Cox /* 354f8a47341SAlan Cox * Returns TRUE if the given reservation contains the given page index and 355f8a47341SAlan Cox * FALSE otherwise. 356f8a47341SAlan Cox */ 357f8a47341SAlan Cox static __inline boolean_t 358f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex) 359f8a47341SAlan Cox { 360f8a47341SAlan Cox 361f8a47341SAlan Cox return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0); 362f8a47341SAlan Cox } 363f8a47341SAlan Cox 364f8a47341SAlan Cox /* 365f8a47341SAlan Cox * Increases the given reservation's population count. Moves the reservation 366f8a47341SAlan Cox * to the tail of the partially-populated reservation queue. 367f8a47341SAlan Cox * 368f8a47341SAlan Cox * The free page queue must be locked. 369f8a47341SAlan Cox */ 370f8a47341SAlan Cox static void 371ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index) 372f8a47341SAlan Cox { 373f8a47341SAlan Cox 374f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 375f8a47341SAlan Cox KASSERT(rv->object != NULL, 376f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is free", rv)); 3773180f757SAlan Cox KASSERT(popmap_is_clear(rv->popmap, index), 378a08c1515SAlan Cox ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv, 379a08c1515SAlan Cox index)); 380f8a47341SAlan Cox KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES, 381f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is already full", rv)); 382dd05fa19SAlan Cox KASSERT(rv->pages->psind == 0, 383dd05fa19SAlan Cox ("vm_reserv_populate: reserv %p is already promoted", rv)); 384f8a47341SAlan Cox if (rv->inpartpopq) { 385f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 386f8a47341SAlan Cox rv->inpartpopq = FALSE; 387f8a47341SAlan Cox } 3883180f757SAlan Cox popmap_set(rv->popmap, index); 389f8a47341SAlan Cox rv->popcnt++; 390f8a47341SAlan Cox if (rv->popcnt < VM_LEVEL_0_NPAGES) { 391f8a47341SAlan Cox rv->inpartpopq = TRUE; 392f8a47341SAlan Cox TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq); 393dd05fa19SAlan Cox } else 394dd05fa19SAlan Cox rv->pages->psind = 1; 395f8a47341SAlan Cox } 396f8a47341SAlan Cox 397f8a47341SAlan Cox /* 398c68c3537SAlan Cox * Allocates a contiguous set of physical pages of the given size "npages" 39964f096eeSAlan Cox * from existing or newly created reservations. All of the physical pages 400c68c3537SAlan Cox * must be at or above the given physical address "low" and below the given 401c68c3537SAlan Cox * physical address "high". The given value "alignment" determines the 402c68c3537SAlan Cox * alignment of the first physical page in the set. If the given value 403c68c3537SAlan Cox * "boundary" is non-zero, then the set of physical pages cannot cross any 404c68c3537SAlan Cox * physical address boundary that is a multiple of that value. Both 405c68c3537SAlan Cox * "alignment" and "boundary" must be a power of two. 406c68c3537SAlan Cox * 407c68c3537SAlan Cox * The object and free page queue must be locked. 408c68c3537SAlan Cox */ 409c68c3537SAlan Cox vm_page_t 410c68c3537SAlan Cox vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, u_long npages, 411c68c3537SAlan Cox vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 412c68c3537SAlan Cox { 413c68c3537SAlan Cox vm_paddr_t pa, size; 414c68c3537SAlan Cox vm_page_t m, m_ret, mpred, msucc; 415c68c3537SAlan Cox vm_pindex_t first, leftcap, rightcap; 416c68c3537SAlan Cox vm_reserv_t rv; 417c68c3537SAlan Cox u_long allocpages, maxpages, minpages; 418c68c3537SAlan Cox int i, index, n; 419c68c3537SAlan Cox 420c68c3537SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 42189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 422c68c3537SAlan Cox KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0")); 423c68c3537SAlan Cox 424c68c3537SAlan Cox /* 425c68c3537SAlan Cox * Is a reservation fundamentally impossible? 426c68c3537SAlan Cox */ 427c68c3537SAlan Cox if (pindex < VM_RESERV_INDEX(object, pindex) || 428c68c3537SAlan Cox pindex + npages > object->size) 429c68c3537SAlan Cox return (NULL); 430c68c3537SAlan Cox 431c68c3537SAlan Cox /* 432c68c3537SAlan Cox * All reservations of a particular size have the same alignment. 433c68c3537SAlan Cox * Assuming that the first page is allocated from a reservation, the 434c68c3537SAlan Cox * least significant bits of its physical address can be determined 435c68c3537SAlan Cox * from its offset from the beginning of the reservation and the size 436c68c3537SAlan Cox * of the reservation. 437c68c3537SAlan Cox * 438c68c3537SAlan Cox * Could the specified index within a reservation of the smallest 439c68c3537SAlan Cox * possible size satisfy the alignment and boundary requirements? 440c68c3537SAlan Cox */ 441c68c3537SAlan Cox pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT; 442c68c3537SAlan Cox if ((pa & (alignment - 1)) != 0) 443c68c3537SAlan Cox return (NULL); 444c68c3537SAlan Cox size = npages << PAGE_SHIFT; 445c68c3537SAlan Cox if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 446c68c3537SAlan Cox return (NULL); 447c68c3537SAlan Cox 448c68c3537SAlan Cox /* 449c68c3537SAlan Cox * Look for an existing reservation. 450c68c3537SAlan Cox */ 451774d251dSAttilio Rao mpred = vm_radix_lookup_le(&object->rtree, pindex); 452774d251dSAttilio Rao if (mpred != NULL) { 453774d251dSAttilio Rao KASSERT(mpred->pindex < pindex, 454c68c3537SAlan Cox ("vm_reserv_alloc_contig: pindex already allocated")); 455c68c3537SAlan Cox rv = vm_reserv_from_page(mpred); 456c68c3537SAlan Cox if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 457c68c3537SAlan Cox goto found; 458774d251dSAttilio Rao msucc = TAILQ_NEXT(mpred, listq); 459774d251dSAttilio Rao } else 460774d251dSAttilio Rao msucc = TAILQ_FIRST(&object->memq); 461774d251dSAttilio Rao if (msucc != NULL) { 462774d251dSAttilio Rao KASSERT(msucc->pindex > pindex, 46379d7993dSKonstantin Belousov ("vm_reserv_alloc_contig: pindex already allocated")); 464c68c3537SAlan Cox rv = vm_reserv_from_page(msucc); 465774d251dSAttilio Rao if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 466c68c3537SAlan Cox goto found; 467c68c3537SAlan Cox } 468c68c3537SAlan Cox 469c68c3537SAlan Cox /* 470c68c3537SAlan Cox * Could at least one reservation fit between the first index to the 47164f096eeSAlan Cox * left that can be used ("leftcap") and the first index to the right 47264f096eeSAlan Cox * that cannot be used ("rightcap")? 473c68c3537SAlan Cox */ 474c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 475c68c3537SAlan Cox if (mpred != NULL) { 476c68c3537SAlan Cox if ((rv = vm_reserv_from_page(mpred))->object != object) 477c68c3537SAlan Cox leftcap = mpred->pindex + 1; 478c68c3537SAlan Cox else 479c68c3537SAlan Cox leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 480c68c3537SAlan Cox if (leftcap > first) 481c68c3537SAlan Cox return (NULL); 482c68c3537SAlan Cox } 483c68c3537SAlan Cox minpages = VM_RESERV_INDEX(object, pindex) + npages; 484c68c3537SAlan Cox maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES); 485c68c3537SAlan Cox allocpages = maxpages; 486c68c3537SAlan Cox if (msucc != NULL) { 487c68c3537SAlan Cox if ((rv = vm_reserv_from_page(msucc))->object != object) 488c68c3537SAlan Cox rightcap = msucc->pindex; 489c68c3537SAlan Cox else 490c68c3537SAlan Cox rightcap = rv->pindex; 491c68c3537SAlan Cox if (first + maxpages > rightcap) { 492c68c3537SAlan Cox if (maxpages == VM_LEVEL_0_NPAGES) 493c68c3537SAlan Cox return (NULL); 49464f096eeSAlan Cox 49564f096eeSAlan Cox /* 49664f096eeSAlan Cox * At least one reservation will fit between "leftcap" 49764f096eeSAlan Cox * and "rightcap". However, a reservation for the 49864f096eeSAlan Cox * last of the requested pages will not fit. Reduce 49964f096eeSAlan Cox * the size of the upcoming allocation accordingly. 50064f096eeSAlan Cox */ 501c68c3537SAlan Cox allocpages = minpages; 502c68c3537SAlan Cox } 503c68c3537SAlan Cox } 504c68c3537SAlan Cox 505c68c3537SAlan Cox /* 506c68c3537SAlan Cox * Would the last new reservation extend past the end of the object? 507c68c3537SAlan Cox */ 508c68c3537SAlan Cox if (first + maxpages > object->size) { 509c68c3537SAlan Cox /* 510c68c3537SAlan Cox * Don't allocate the last new reservation if the object is a 511c68c3537SAlan Cox * vnode or backed by another object that is a vnode. 512c68c3537SAlan Cox */ 513c68c3537SAlan Cox if (object->type == OBJT_VNODE || 514c68c3537SAlan Cox (object->backing_object != NULL && 515c68c3537SAlan Cox object->backing_object->type == OBJT_VNODE)) { 516c68c3537SAlan Cox if (maxpages == VM_LEVEL_0_NPAGES) 517c68c3537SAlan Cox return (NULL); 518c68c3537SAlan Cox allocpages = minpages; 519c68c3537SAlan Cox } 520c68c3537SAlan Cox /* Speculate that the object may grow. */ 521c68c3537SAlan Cox } 522c68c3537SAlan Cox 523c68c3537SAlan Cox /* 52464f096eeSAlan Cox * Allocate the physical pages. The alignment and boundary specified 52564f096eeSAlan Cox * for this allocation may be different from the alignment and 52664f096eeSAlan Cox * boundary specified for the requested pages. For instance, the 52764f096eeSAlan Cox * specified index may not be the first page within the first new 52864f096eeSAlan Cox * reservation. 529c68c3537SAlan Cox */ 530c68c3537SAlan Cox m = vm_phys_alloc_contig(allocpages, low, high, ulmax(alignment, 531c68c3537SAlan Cox VM_LEVEL_0_SIZE), boundary > VM_LEVEL_0_SIZE ? boundary : 0); 532c68c3537SAlan Cox if (m == NULL) 533c68c3537SAlan Cox return (NULL); 53464f096eeSAlan Cox 53564f096eeSAlan Cox /* 53664f096eeSAlan Cox * The allocated physical pages always begin at a reservation 53764f096eeSAlan Cox * boundary, but they do not always end at a reservation boundary. 53864f096eeSAlan Cox * Initialize every reservation that is completely covered by the 53964f096eeSAlan Cox * allocated physical pages. 54064f096eeSAlan Cox */ 541c68c3537SAlan Cox m_ret = NULL; 542c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 543c68c3537SAlan Cox do { 544c68c3537SAlan Cox rv = vm_reserv_from_page(m); 545c68c3537SAlan Cox KASSERT(rv->pages == m, 546c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p's pages is corrupted", 547c68c3537SAlan Cox rv)); 548c68c3537SAlan Cox KASSERT(rv->object == NULL, 549c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p isn't free", rv)); 550c68c3537SAlan Cox LIST_INSERT_HEAD(&object->rvq, rv, objq); 551c68c3537SAlan Cox rv->object = object; 552c68c3537SAlan Cox rv->pindex = first; 553c68c3537SAlan Cox KASSERT(rv->popcnt == 0, 554c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p's popcnt is corrupted", 555c68c3537SAlan Cox rv)); 556c68c3537SAlan Cox KASSERT(!rv->inpartpopq, 557c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p's inpartpopq is TRUE", 558c68c3537SAlan Cox rv)); 559ec179322SAlan Cox for (i = 0; i < NPOPMAP; i++) 560ec179322SAlan Cox KASSERT(rv->popmap[i] == 0, 561ec179322SAlan Cox ("vm_reserv_alloc_contig: reserv %p's popmap is corrupted", 562ec179322SAlan Cox rv)); 563c68c3537SAlan Cox n = ulmin(VM_LEVEL_0_NPAGES - index, npages); 564c68c3537SAlan Cox for (i = 0; i < n; i++) 565ec179322SAlan Cox vm_reserv_populate(rv, index + i); 566c68c3537SAlan Cox npages -= n; 567c68c3537SAlan Cox if (m_ret == NULL) { 568c68c3537SAlan Cox m_ret = &rv->pages[index]; 569c68c3537SAlan Cox index = 0; 570c68c3537SAlan Cox } 571c68c3537SAlan Cox m += VM_LEVEL_0_NPAGES; 572c68c3537SAlan Cox first += VM_LEVEL_0_NPAGES; 573c68c3537SAlan Cox allocpages -= VM_LEVEL_0_NPAGES; 57464f096eeSAlan Cox } while (allocpages >= VM_LEVEL_0_NPAGES); 575c68c3537SAlan Cox return (m_ret); 576c68c3537SAlan Cox 577c68c3537SAlan Cox /* 578c68c3537SAlan Cox * Found a matching reservation. 579c68c3537SAlan Cox */ 580c68c3537SAlan Cox found: 581c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 582c68c3537SAlan Cox /* Does the allocation fit within the reservation? */ 583c68c3537SAlan Cox if (index + npages > VM_LEVEL_0_NPAGES) 584c68c3537SAlan Cox return (NULL); 585c68c3537SAlan Cox m = &rv->pages[index]; 586c68c3537SAlan Cox pa = VM_PAGE_TO_PHYS(m); 587c68c3537SAlan Cox if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 || 588c68c3537SAlan Cox ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 589c68c3537SAlan Cox return (NULL); 590c68c3537SAlan Cox /* Handle vm_page_rename(m, new_object, ...). */ 591c68c3537SAlan Cox for (i = 0; i < npages; i++) 5923180f757SAlan Cox if (popmap_is_set(rv->popmap, index + i)) 593c68c3537SAlan Cox return (NULL); 594c68c3537SAlan Cox for (i = 0; i < npages; i++) 595ec179322SAlan Cox vm_reserv_populate(rv, index + i); 596c68c3537SAlan Cox return (m); 597c68c3537SAlan Cox } 598c68c3537SAlan Cox 599c68c3537SAlan Cox /* 600f8a47341SAlan Cox * Allocates a page from an existing or newly-created reservation. 601f8a47341SAlan Cox * 602404eb1b3SAlan Cox * The page "mpred" must immediately precede the offset "pindex" within the 603404eb1b3SAlan Cox * specified object. 604404eb1b3SAlan Cox * 605f8a47341SAlan Cox * The object and free page queue must be locked. 606f8a47341SAlan Cox */ 607f8a47341SAlan Cox vm_page_t 608404eb1b3SAlan Cox vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, vm_page_t mpred) 609f8a47341SAlan Cox { 610404eb1b3SAlan Cox vm_page_t m, msucc; 611f8a47341SAlan Cox vm_pindex_t first, leftcap, rightcap; 612f8a47341SAlan Cox vm_reserv_t rv; 613ec179322SAlan Cox int i, index; 614f8a47341SAlan Cox 615f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 61689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 617f8a47341SAlan Cox 618f8a47341SAlan Cox /* 619c68c3537SAlan Cox * Is a reservation fundamentally impossible? 620f8a47341SAlan Cox */ 621f8a47341SAlan Cox if (pindex < VM_RESERV_INDEX(object, pindex) || 622f8a47341SAlan Cox pindex >= object->size) 623f8a47341SAlan Cox return (NULL); 624f8a47341SAlan Cox 625f8a47341SAlan Cox /* 626f8a47341SAlan Cox * Look for an existing reservation. 627f8a47341SAlan Cox */ 628774d251dSAttilio Rao if (mpred != NULL) { 6299eab5484SKonstantin Belousov KASSERT(mpred->object == object, 630404eb1b3SAlan Cox ("vm_reserv_alloc_page: object doesn't contain mpred")); 631774d251dSAttilio Rao KASSERT(mpred->pindex < pindex, 632404eb1b3SAlan Cox ("vm_reserv_alloc_page: mpred doesn't precede pindex")); 633f8a47341SAlan Cox rv = vm_reserv_from_page(mpred); 634c68c3537SAlan Cox if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 635c68c3537SAlan Cox goto found; 636774d251dSAttilio Rao msucc = TAILQ_NEXT(mpred, listq); 637774d251dSAttilio Rao } else 638774d251dSAttilio Rao msucc = TAILQ_FIRST(&object->memq); 639774d251dSAttilio Rao if (msucc != NULL) { 640774d251dSAttilio Rao KASSERT(msucc->pindex > pindex, 641404eb1b3SAlan Cox ("vm_reserv_alloc_page: msucc doesn't succeed pindex")); 642f8a47341SAlan Cox rv = vm_reserv_from_page(msucc); 643774d251dSAttilio Rao if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 644c68c3537SAlan Cox goto found; 645f8a47341SAlan Cox } 646f8a47341SAlan Cox 647f8a47341SAlan Cox /* 648c68c3537SAlan Cox * Could a reservation fit between the first index to the left that 649c68c3537SAlan Cox * can be used and the first index to the right that cannot be used? 650f8a47341SAlan Cox */ 651c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 652c68c3537SAlan Cox if (mpred != NULL) { 653c68c3537SAlan Cox if ((rv = vm_reserv_from_page(mpred))->object != object) 654f8a47341SAlan Cox leftcap = mpred->pindex + 1; 655f8a47341SAlan Cox else 656f8a47341SAlan Cox leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 657c68c3537SAlan Cox if (leftcap > first) 658c68c3537SAlan Cox return (NULL); 659c68c3537SAlan Cox } 660c68c3537SAlan Cox if (msucc != NULL) { 661c68c3537SAlan Cox if ((rv = vm_reserv_from_page(msucc))->object != object) 662f8a47341SAlan Cox rightcap = msucc->pindex; 663f8a47341SAlan Cox else 664f8a47341SAlan Cox rightcap = rv->pindex; 665c68c3537SAlan Cox if (first + VM_LEVEL_0_NPAGES > rightcap) 666f8a47341SAlan Cox return (NULL); 667c68c3537SAlan Cox } 668f8a47341SAlan Cox 669f8a47341SAlan Cox /* 670c68c3537SAlan Cox * Would a new reservation extend past the end of the object? 671f8a47341SAlan Cox */ 672c68c3537SAlan Cox if (first + VM_LEVEL_0_NPAGES > object->size) { 673f8a47341SAlan Cox /* 674f8a47341SAlan Cox * Don't allocate a new reservation if the object is a vnode or 675f8a47341SAlan Cox * backed by another object that is a vnode. 676f8a47341SAlan Cox */ 677f8a47341SAlan Cox if (object->type == OBJT_VNODE || 678f8a47341SAlan Cox (object->backing_object != NULL && 679f8a47341SAlan Cox object->backing_object->type == OBJT_VNODE)) 680f8a47341SAlan Cox return (NULL); 681f8a47341SAlan Cox /* Speculate that the object may grow. */ 682f8a47341SAlan Cox } 683f8a47341SAlan Cox 684f8a47341SAlan Cox /* 685c68c3537SAlan Cox * Allocate and populate the new reservation. 686f8a47341SAlan Cox */ 687f8a47341SAlan Cox m = vm_phys_alloc_pages(VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER); 688c68c3537SAlan Cox if (m == NULL) 689c68c3537SAlan Cox return (NULL); 690f8a47341SAlan Cox rv = vm_reserv_from_page(m); 691f8a47341SAlan Cox KASSERT(rv->pages == m, 692c68c3537SAlan Cox ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv)); 693f8a47341SAlan Cox KASSERT(rv->object == NULL, 694f8a47341SAlan Cox ("vm_reserv_alloc_page: reserv %p isn't free", rv)); 695f8a47341SAlan Cox LIST_INSERT_HEAD(&object->rvq, rv, objq); 696f8a47341SAlan Cox rv->object = object; 697f8a47341SAlan Cox rv->pindex = first; 698f8a47341SAlan Cox KASSERT(rv->popcnt == 0, 699c68c3537SAlan Cox ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted", rv)); 700f8a47341SAlan Cox KASSERT(!rv->inpartpopq, 701c68c3537SAlan Cox ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE", rv)); 702ec179322SAlan Cox for (i = 0; i < NPOPMAP; i++) 703ec179322SAlan Cox KASSERT(rv->popmap[i] == 0, 704ec179322SAlan Cox ("vm_reserv_alloc_page: reserv %p's popmap is corrupted", 705ec179322SAlan Cox rv)); 706ec179322SAlan Cox index = VM_RESERV_INDEX(object, pindex); 707ec179322SAlan Cox vm_reserv_populate(rv, index); 708ec179322SAlan Cox return (&rv->pages[index]); 709c68c3537SAlan Cox 710c68c3537SAlan Cox /* 711c68c3537SAlan Cox * Found a matching reservation. 712c68c3537SAlan Cox */ 713c68c3537SAlan Cox found: 714ec179322SAlan Cox index = VM_RESERV_INDEX(object, pindex); 715ec179322SAlan Cox m = &rv->pages[index]; 716c68c3537SAlan Cox /* Handle vm_page_rename(m, new_object, ...). */ 7173180f757SAlan Cox if (popmap_is_set(rv->popmap, index)) 718c68c3537SAlan Cox return (NULL); 719ec179322SAlan Cox vm_reserv_populate(rv, index); 720f8a47341SAlan Cox return (m); 721f8a47341SAlan Cox } 722f8a47341SAlan Cox 723f8a47341SAlan Cox /* 724ec179322SAlan Cox * Breaks the given reservation. Except for the specified cached or free 725ec179322SAlan Cox * page, all cached and free pages in the reservation are returned to the 726ec179322SAlan Cox * physical memory allocator. The reservation's population count and map are 727ec179322SAlan Cox * reset to their initial state. 728ec179322SAlan Cox * 729ec179322SAlan Cox * The given reservation must not be in the partially-populated reservation 730ec179322SAlan Cox * queue. The free page queue lock must be held. 731ec179322SAlan Cox */ 732ec179322SAlan Cox static void 733ec179322SAlan Cox vm_reserv_break(vm_reserv_t rv, vm_page_t m) 734ec179322SAlan Cox { 735ec179322SAlan Cox int begin_zeroes, hi, i, lo; 736ec179322SAlan Cox 737ec179322SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 738ec179322SAlan Cox KASSERT(rv->object != NULL, 739ec179322SAlan Cox ("vm_reserv_break: reserv %p is free", rv)); 740ec179322SAlan Cox KASSERT(!rv->inpartpopq, 741ec179322SAlan Cox ("vm_reserv_break: reserv %p's inpartpopq is TRUE", rv)); 742ec179322SAlan Cox LIST_REMOVE(rv, objq); 743ec179322SAlan Cox rv->object = NULL; 744ec179322SAlan Cox if (m != NULL) { 745ec179322SAlan Cox /* 746ec179322SAlan Cox * Since the reservation is being broken, there is no harm in 747ec179322SAlan Cox * abusing the population map to stop "m" from being returned 748ec179322SAlan Cox * to the physical memory allocator. 749ec179322SAlan Cox */ 750ec179322SAlan Cox i = m - rv->pages; 7513180f757SAlan Cox KASSERT(popmap_is_clear(rv->popmap, i), 752ec179322SAlan Cox ("vm_reserv_break: reserv %p's popmap is corrupted", rv)); 7533180f757SAlan Cox popmap_set(rv->popmap, i); 754ec179322SAlan Cox rv->popcnt++; 755ec179322SAlan Cox } 756ec179322SAlan Cox i = hi = 0; 757ec179322SAlan Cox do { 758ec179322SAlan Cox /* Find the next 0 bit. Any previous 0 bits are < "hi". */ 759ec179322SAlan Cox lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i])); 760ec179322SAlan Cox if (lo == 0) { 761ec179322SAlan Cox /* Redundantly clears bits < "hi". */ 762ec179322SAlan Cox rv->popmap[i] = 0; 763ec179322SAlan Cox rv->popcnt -= NBPOPMAP - hi; 764ec179322SAlan Cox while (++i < NPOPMAP) { 765ec179322SAlan Cox lo = ffsl(~rv->popmap[i]); 766ec179322SAlan Cox if (lo == 0) { 767ec179322SAlan Cox rv->popmap[i] = 0; 768ec179322SAlan Cox rv->popcnt -= NBPOPMAP; 769ec179322SAlan Cox } else 770ec179322SAlan Cox break; 771ec179322SAlan Cox } 772ec179322SAlan Cox if (i == NPOPMAP) 773ec179322SAlan Cox break; 774ec179322SAlan Cox hi = 0; 775ec179322SAlan Cox } 776ec179322SAlan Cox KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo)); 777ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 778ec179322SAlan Cox lo--; 779ec179322SAlan Cox if (lo > 0) { 780ec179322SAlan Cox /* Redundantly clears bits < "hi". */ 781ec179322SAlan Cox rv->popmap[i] &= ~((1UL << lo) - 1); 782ec179322SAlan Cox rv->popcnt -= lo - hi; 783ec179322SAlan Cox } 784ec179322SAlan Cox begin_zeroes = NBPOPMAP * i + lo; 785ec179322SAlan Cox /* Find the next 1 bit. */ 786ec179322SAlan Cox do 787ec179322SAlan Cox hi = ffsl(rv->popmap[i]); 788ec179322SAlan Cox while (hi == 0 && ++i < NPOPMAP); 789ec179322SAlan Cox if (i != NPOPMAP) 790ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 791ec179322SAlan Cox hi--; 792ec179322SAlan Cox vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i + 793ec179322SAlan Cox hi - begin_zeroes); 794ec179322SAlan Cox } while (i < NPOPMAP); 795ec179322SAlan Cox KASSERT(rv->popcnt == 0, 796ec179322SAlan Cox ("vm_reserv_break: reserv %p's popcnt is corrupted", rv)); 797ec179322SAlan Cox vm_reserv_broken++; 798ec179322SAlan Cox } 799ec179322SAlan Cox 800ec179322SAlan Cox /* 801f8a47341SAlan Cox * Breaks all reservations belonging to the given object. 802f8a47341SAlan Cox */ 803f8a47341SAlan Cox void 804f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object) 805f8a47341SAlan Cox { 806f8a47341SAlan Cox vm_reserv_t rv; 807f8a47341SAlan Cox 808f8a47341SAlan Cox mtx_lock(&vm_page_queue_free_mtx); 809f8a47341SAlan Cox while ((rv = LIST_FIRST(&object->rvq)) != NULL) { 810f8a47341SAlan Cox KASSERT(rv->object == object, 811f8a47341SAlan Cox ("vm_reserv_break_all: reserv %p is corrupted", rv)); 812f8a47341SAlan Cox if (rv->inpartpopq) { 813f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 814f8a47341SAlan Cox rv->inpartpopq = FALSE; 815f8a47341SAlan Cox } 816ec179322SAlan Cox vm_reserv_break(rv, NULL); 817f8a47341SAlan Cox } 818f8a47341SAlan Cox mtx_unlock(&vm_page_queue_free_mtx); 819f8a47341SAlan Cox } 820f8a47341SAlan Cox 821f8a47341SAlan Cox /* 822f8a47341SAlan Cox * Frees the given page if it belongs to a reservation. Returns TRUE if the 823f8a47341SAlan Cox * page is freed and FALSE otherwise. 824f8a47341SAlan Cox * 825f8a47341SAlan Cox * The free page queue lock must be held. 826f8a47341SAlan Cox */ 827f8a47341SAlan Cox boolean_t 828f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m) 829f8a47341SAlan Cox { 830f8a47341SAlan Cox vm_reserv_t rv; 831f8a47341SAlan Cox 832f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 833f8a47341SAlan Cox rv = vm_reserv_from_page(m); 834908e3da1SAlan Cox if (rv->object == NULL) 835908e3da1SAlan Cox return (FALSE); 836ec179322SAlan Cox vm_reserv_depopulate(rv, m - rv->pages); 837f8a47341SAlan Cox return (TRUE); 838f8a47341SAlan Cox } 839f8a47341SAlan Cox 840f8a47341SAlan Cox /* 841f8a47341SAlan Cox * Initializes the reservation management system. Specifically, initializes 842f8a47341SAlan Cox * the reservation array. 843f8a47341SAlan Cox * 844f8a47341SAlan Cox * Requires that vm_page_array and first_page are initialized! 845f8a47341SAlan Cox */ 846f8a47341SAlan Cox void 847f8a47341SAlan Cox vm_reserv_init(void) 848f8a47341SAlan Cox { 849f8a47341SAlan Cox vm_paddr_t paddr; 85009e5f3c4SAlan Cox struct vm_phys_seg *seg; 85109e5f3c4SAlan Cox int segind; 852f8a47341SAlan Cox 853f8a47341SAlan Cox /* 854f8a47341SAlan Cox * Initialize the reservation array. Specifically, initialize the 855f8a47341SAlan Cox * "pages" field for every element that has an underlying superpage. 856f8a47341SAlan Cox */ 85709e5f3c4SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 85809e5f3c4SAlan Cox seg = &vm_phys_segs[segind]; 85909e5f3c4SAlan Cox paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 86009e5f3c4SAlan Cox while (paddr + VM_LEVEL_0_SIZE <= seg->end) { 861f8a47341SAlan Cox vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages = 862f8a47341SAlan Cox PHYS_TO_VM_PAGE(paddr); 863f8a47341SAlan Cox paddr += VM_LEVEL_0_SIZE; 864f8a47341SAlan Cox } 865f8a47341SAlan Cox } 866f8a47341SAlan Cox } 867f8a47341SAlan Cox 868f8a47341SAlan Cox /* 869*c869e672SAlan Cox * Returns true if the given page belongs to a reservation and that page is 870*c869e672SAlan Cox * free. Otherwise, returns false. 871*c869e672SAlan Cox */ 872*c869e672SAlan Cox bool 873*c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m) 874*c869e672SAlan Cox { 875*c869e672SAlan Cox vm_reserv_t rv; 876*c869e672SAlan Cox 877*c869e672SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 878*c869e672SAlan Cox rv = vm_reserv_from_page(m); 879*c869e672SAlan Cox if (rv->object == NULL) 880*c869e672SAlan Cox return (false); 881*c869e672SAlan Cox return (popmap_is_clear(rv->popmap, m - rv->pages)); 882*c869e672SAlan Cox } 883*c869e672SAlan Cox 884*c869e672SAlan Cox /* 885*c869e672SAlan Cox * If the given page belongs to a reservation, returns the level of that 886*c869e672SAlan Cox * reservation. Otherwise, returns -1. 887*c869e672SAlan Cox */ 888*c869e672SAlan Cox int 889*c869e672SAlan Cox vm_reserv_level(vm_page_t m) 890*c869e672SAlan Cox { 891*c869e672SAlan Cox vm_reserv_t rv; 892*c869e672SAlan Cox 893*c869e672SAlan Cox rv = vm_reserv_from_page(m); 894*c869e672SAlan Cox return (rv->object != NULL ? 0 : -1); 895*c869e672SAlan Cox } 896*c869e672SAlan Cox 897*c869e672SAlan Cox /* 898f8a47341SAlan Cox * Returns a reservation level if the given page belongs to a fully-populated 899f8a47341SAlan Cox * reservation and -1 otherwise. 900f8a47341SAlan Cox */ 901f8a47341SAlan Cox int 902f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m) 903f8a47341SAlan Cox { 904f8a47341SAlan Cox vm_reserv_t rv; 905f8a47341SAlan Cox 906f8a47341SAlan Cox rv = vm_reserv_from_page(m); 907f8a47341SAlan Cox return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1); 908f8a47341SAlan Cox } 909f8a47341SAlan Cox 910f8a47341SAlan Cox /* 911f8a47341SAlan Cox * Prepare for the reactivation of a cached page. 912f8a47341SAlan Cox * 913f8a47341SAlan Cox * First, suppose that the given page "m" was allocated individually, i.e., not 914f8a47341SAlan Cox * as part of a reservation, and cached. Then, suppose a reservation 915f8a47341SAlan Cox * containing "m" is allocated by the same object. Although "m" and the 916f8a47341SAlan Cox * reservation belong to the same object, "m"'s pindex may not match the 917f8a47341SAlan Cox * reservation's. 918f8a47341SAlan Cox * 919f8a47341SAlan Cox * The free page queue must be locked. 920f8a47341SAlan Cox */ 921f8a47341SAlan Cox boolean_t 922f8a47341SAlan Cox vm_reserv_reactivate_page(vm_page_t m) 923f8a47341SAlan Cox { 924f8a47341SAlan Cox vm_reserv_t rv; 925ec179322SAlan Cox int index; 926f8a47341SAlan Cox 927f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 928f8a47341SAlan Cox rv = vm_reserv_from_page(m); 929f8a47341SAlan Cox if (rv->object == NULL) 930f8a47341SAlan Cox return (FALSE); 931f8a47341SAlan Cox KASSERT((m->flags & PG_CACHED) != 0, 932ec179322SAlan Cox ("vm_reserv_reactivate_page: page %p is not cached", m)); 933f8a47341SAlan Cox if (m->object == rv->object && 934ec179322SAlan Cox m->pindex - rv->pindex == (index = VM_RESERV_INDEX(m->object, 935ec179322SAlan Cox m->pindex))) 936ec179322SAlan Cox vm_reserv_populate(rv, index); 937f8a47341SAlan Cox else { 938f8a47341SAlan Cox KASSERT(rv->inpartpopq, 939ec179322SAlan Cox ("vm_reserv_reactivate_page: reserv %p's inpartpopq is FALSE", 940f8a47341SAlan Cox rv)); 941f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 942f8a47341SAlan Cox rv->inpartpopq = FALSE; 943ec179322SAlan Cox /* Don't release "m" to the physical memory allocator. */ 944ec179322SAlan Cox vm_reserv_break(rv, m); 945f8a47341SAlan Cox } 946f8a47341SAlan Cox return (TRUE); 947f8a47341SAlan Cox } 948f8a47341SAlan Cox 949f8a47341SAlan Cox /* 95044aab2c3SAlan Cox * Breaks the given partially-populated reservation, releasing its cached and 95144aab2c3SAlan Cox * free pages to the physical memory allocator. 952f8a47341SAlan Cox * 953f8a47341SAlan Cox * The free page queue lock must be held. 954f8a47341SAlan Cox */ 95544aab2c3SAlan Cox static void 95644aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv) 957f8a47341SAlan Cox { 958f8a47341SAlan Cox 959f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 960f8a47341SAlan Cox KASSERT(rv->inpartpopq, 961ec179322SAlan Cox ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv)); 962f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 963f8a47341SAlan Cox rv->inpartpopq = FALSE; 964ec179322SAlan Cox vm_reserv_break(rv, NULL); 965f8a47341SAlan Cox vm_reserv_reclaimed++; 96644aab2c3SAlan Cox } 96744aab2c3SAlan Cox 96844aab2c3SAlan Cox /* 96944aab2c3SAlan Cox * Breaks the reservation at the head of the partially-populated reservation 97044aab2c3SAlan Cox * queue, releasing its cached and free pages to the physical memory 97144aab2c3SAlan Cox * allocator. Returns TRUE if a reservation is broken and FALSE otherwise. 97244aab2c3SAlan Cox * 97344aab2c3SAlan Cox * The free page queue lock must be held. 97444aab2c3SAlan Cox */ 97544aab2c3SAlan Cox boolean_t 97644aab2c3SAlan Cox vm_reserv_reclaim_inactive(void) 97744aab2c3SAlan Cox { 97844aab2c3SAlan Cox vm_reserv_t rv; 97944aab2c3SAlan Cox 98044aab2c3SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 98144aab2c3SAlan Cox if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) { 98244aab2c3SAlan Cox vm_reserv_reclaim(rv); 983f8a47341SAlan Cox return (TRUE); 984f8a47341SAlan Cox } 985f8a47341SAlan Cox return (FALSE); 986f8a47341SAlan Cox } 987f8a47341SAlan Cox 988f8a47341SAlan Cox /* 98944aab2c3SAlan Cox * Searches the partially-populated reservation queue for the least recently 99044aab2c3SAlan Cox * active reservation with unused pages, i.e., cached or free, that satisfy the 99144aab2c3SAlan Cox * given request for contiguous physical memory. If a satisfactory reservation 99244aab2c3SAlan Cox * is found, it is broken. Returns TRUE if a reservation is broken and FALSE 99344aab2c3SAlan Cox * otherwise. 99444aab2c3SAlan Cox * 99544aab2c3SAlan Cox * The free page queue lock must be held. 99644aab2c3SAlan Cox */ 99744aab2c3SAlan Cox boolean_t 998c68c3537SAlan Cox vm_reserv_reclaim_contig(u_long npages, vm_paddr_t low, vm_paddr_t high, 9995c1f2cc4SAlan Cox u_long alignment, vm_paddr_t boundary) 100044aab2c3SAlan Cox { 1001ec179322SAlan Cox vm_paddr_t pa, size; 100244aab2c3SAlan Cox vm_reserv_t rv; 100367b7e434SAlan Cox int hi, i, lo, low_index, next_free; 100444aab2c3SAlan Cox 100544aab2c3SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1006c68c3537SAlan Cox if (npages > VM_LEVEL_0_NPAGES - 1) 100744aab2c3SAlan Cox return (FALSE); 1008c68c3537SAlan Cox size = npages << PAGE_SHIFT; 100944aab2c3SAlan Cox TAILQ_FOREACH(rv, &vm_rvq_partpop, partpopq) { 101044aab2c3SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]); 101144aab2c3SAlan Cox if (pa + PAGE_SIZE - size < low) { 1012ec179322SAlan Cox /* This entire reservation is too low; go to next. */ 101344aab2c3SAlan Cox continue; 101444aab2c3SAlan Cox } 1015ec179322SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[0]); 101644aab2c3SAlan Cox if (pa + size > high) { 1017ec179322SAlan Cox /* This entire reservation is too high; go to next. */ 1018ec179322SAlan Cox continue; 101985f2a0c9SMax Laier } 1020ec179322SAlan Cox if (pa < low) { 1021ec179322SAlan Cox /* Start the search for free pages at "low". */ 102267b7e434SAlan Cox low_index = (low + PAGE_MASK - pa) >> PAGE_SHIFT; 102367b7e434SAlan Cox i = low_index / NBPOPMAP; 102467b7e434SAlan Cox hi = low_index % NBPOPMAP; 1025ec179322SAlan Cox } else 1026ec179322SAlan Cox i = hi = 0; 1027ec179322SAlan Cox do { 1028ec179322SAlan Cox /* Find the next free page. */ 1029ec179322SAlan Cox lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i])); 1030ec179322SAlan Cox while (lo == 0 && ++i < NPOPMAP) 1031ec179322SAlan Cox lo = ffsl(~rv->popmap[i]); 1032ec179322SAlan Cox if (i == NPOPMAP) 1033ec179322SAlan Cox break; 1034ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 1035ec179322SAlan Cox lo--; 1036ec179322SAlan Cox next_free = NBPOPMAP * i + lo; 1037ec179322SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]); 1038ec179322SAlan Cox KASSERT(pa >= low, 1039ec179322SAlan Cox ("vm_reserv_reclaim_contig: pa is too low")); 1040ec179322SAlan Cox if (pa + size > high) { 1041ec179322SAlan Cox /* The rest of this reservation is too high. */ 1042ec179322SAlan Cox break; 1043ec179322SAlan Cox } else if ((pa & (alignment - 1)) != 0 || 1044ec179322SAlan Cox ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) { 10456a93e36bSAlan Cox /* 10466a93e36bSAlan Cox * The current page doesn't meet the alignment 10476a93e36bSAlan Cox * and/or boundary requirements. Continue 10486a93e36bSAlan Cox * searching this reservation until the rest 10496a93e36bSAlan Cox * of its free pages are either excluded or 10506a93e36bSAlan Cox * exhausted. 10516a93e36bSAlan Cox */ 10526a93e36bSAlan Cox hi = lo + 1; 10536a93e36bSAlan Cox if (hi >= NBPOPMAP) { 10546a93e36bSAlan Cox hi = 0; 10556a93e36bSAlan Cox i++; 10566a93e36bSAlan Cox } 1057ec179322SAlan Cox continue; 1058ec179322SAlan Cox } 1059ec179322SAlan Cox /* Find the next used page. */ 1060ec179322SAlan Cox hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1)); 1061ec179322SAlan Cox while (hi == 0 && ++i < NPOPMAP) { 1062ec179322SAlan Cox if ((NBPOPMAP * i - next_free) * PAGE_SIZE >= 1063ec179322SAlan Cox size) { 106444aab2c3SAlan Cox vm_reserv_reclaim(rv); 106544aab2c3SAlan Cox return (TRUE); 106644aab2c3SAlan Cox } 1067ec179322SAlan Cox hi = ffsl(rv->popmap[i]); 1068ec179322SAlan Cox } 1069ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 1070ec179322SAlan Cox if (i != NPOPMAP) 1071ec179322SAlan Cox hi--; 1072ec179322SAlan Cox if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >= 1073ec179322SAlan Cox size) { 1074ec179322SAlan Cox vm_reserv_reclaim(rv); 1075ec179322SAlan Cox return (TRUE); 1076ec179322SAlan Cox } 1077ec179322SAlan Cox } while (i < NPOPMAP); 107844aab2c3SAlan Cox } 107944aab2c3SAlan Cox return (FALSE); 108044aab2c3SAlan Cox } 108144aab2c3SAlan Cox 108244aab2c3SAlan Cox /* 1083f8a47341SAlan Cox * Transfers the reservation underlying the given page to a new object. 1084f8a47341SAlan Cox * 1085f8a47341SAlan Cox * The object must be locked. 1086f8a47341SAlan Cox */ 1087f8a47341SAlan Cox void 1088f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object, 1089f8a47341SAlan Cox vm_pindex_t old_object_offset) 1090f8a47341SAlan Cox { 1091f8a47341SAlan Cox vm_reserv_t rv; 1092f8a47341SAlan Cox 109389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(new_object); 1094f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1095f8a47341SAlan Cox if (rv->object == old_object) { 1096f8a47341SAlan Cox mtx_lock(&vm_page_queue_free_mtx); 1097f8a47341SAlan Cox if (rv->object == old_object) { 1098f8a47341SAlan Cox LIST_REMOVE(rv, objq); 1099f8a47341SAlan Cox LIST_INSERT_HEAD(&new_object->rvq, rv, objq); 1100f8a47341SAlan Cox rv->object = new_object; 1101f8a47341SAlan Cox rv->pindex -= old_object_offset; 1102f8a47341SAlan Cox } 1103f8a47341SAlan Cox mtx_unlock(&vm_page_queue_free_mtx); 1104f8a47341SAlan Cox } 1105f8a47341SAlan Cox } 1106f8a47341SAlan Cox 1107f8a47341SAlan Cox /* 1108*c869e672SAlan Cox * Returns the size (in bytes) of a reservation of the specified level. 1109*c869e672SAlan Cox */ 1110*c869e672SAlan Cox int 1111*c869e672SAlan Cox vm_reserv_size(int level) 1112*c869e672SAlan Cox { 1113*c869e672SAlan Cox 1114*c869e672SAlan Cox switch (level) { 1115*c869e672SAlan Cox case 0: 1116*c869e672SAlan Cox return (VM_LEVEL_0_SIZE); 1117*c869e672SAlan Cox case -1: 1118*c869e672SAlan Cox return (PAGE_SIZE); 1119*c869e672SAlan Cox default: 1120*c869e672SAlan Cox return (0); 1121*c869e672SAlan Cox } 1122*c869e672SAlan Cox } 1123*c869e672SAlan Cox 1124*c869e672SAlan Cox /* 1125f8a47341SAlan Cox * Allocates the virtual and physical memory required by the reservation 1126f8a47341SAlan Cox * management system's data structures, in particular, the reservation array. 1127f8a47341SAlan Cox */ 1128f8a47341SAlan Cox vm_paddr_t 1129f8a47341SAlan Cox vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water) 1130f8a47341SAlan Cox { 1131f8a47341SAlan Cox vm_paddr_t new_end; 1132f8a47341SAlan Cox size_t size; 1133f8a47341SAlan Cox 1134f8a47341SAlan Cox /* 1135f8a47341SAlan Cox * Calculate the size (in bytes) of the reservation array. Round up 1136f8a47341SAlan Cox * from "high_water" because every small page is mapped to an element 1137f8a47341SAlan Cox * in the reservation array based on its physical address. Thus, the 1138f8a47341SAlan Cox * number of elements in the reservation array can be greater than the 1139f8a47341SAlan Cox * number of superpages. 1140f8a47341SAlan Cox */ 1141f8a47341SAlan Cox size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv); 1142f8a47341SAlan Cox 1143f8a47341SAlan Cox /* 1144f8a47341SAlan Cox * Allocate and map the physical memory for the reservation array. The 1145f8a47341SAlan Cox * next available virtual address is returned by reference. 1146f8a47341SAlan Cox */ 1147f8a47341SAlan Cox new_end = end - round_page(size); 1148f8a47341SAlan Cox vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end, 1149f8a47341SAlan Cox VM_PROT_READ | VM_PROT_WRITE); 1150f8a47341SAlan Cox bzero(vm_reserv_array, size); 1151f8a47341SAlan Cox 1152f8a47341SAlan Cox /* 1153f8a47341SAlan Cox * Return the next available physical address. 1154f8a47341SAlan Cox */ 1155f8a47341SAlan Cox return (new_end); 1156f8a47341SAlan Cox } 1157f8a47341SAlan Cox 1158f8a47341SAlan Cox #endif /* VM_NRESERVLEVEL > 0 */ 1159