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 65*3453bca8SAlan 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 158*3453bca8SAlan 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 * 162*3453bca8SAlan 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 /* 199*3453bca8SAlan Cox * The partially populated reservation queue 200f8a47341SAlan Cox * 201*3453bca8SAlan Cox * This queue enables the fast recovery of an unused free small page from a 202*3453bca8SAlan Cox * partially populated reservation. The reservation at the head of this queue 203*3453bca8SAlan Cox * 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, 228*3453bca8SAlan 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 /* 270*3453bca8SAlan 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 304*3453bca8SAlan 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 366*3453bca8SAlan 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 /* 600*3453bca8SAlan 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 /* 724*3453bca8SAlan Cox * Breaks the given reservation. Except for the specified free page, all free 725*3453bca8SAlan Cox * pages in the reservation are returned to the physical memory allocator. 726*3453bca8SAlan Cox * The reservation's population count and map are reset to their initial 727*3453bca8SAlan Cox * state. 728ec179322SAlan Cox * 729*3453bca8SAlan 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 /* 869c869e672SAlan Cox * Returns true if the given page belongs to a reservation and that page is 870c869e672SAlan Cox * free. Otherwise, returns false. 871c869e672SAlan Cox */ 872c869e672SAlan Cox bool 873c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m) 874c869e672SAlan Cox { 875c869e672SAlan Cox vm_reserv_t rv; 876c869e672SAlan Cox 877c869e672SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 878c869e672SAlan Cox rv = vm_reserv_from_page(m); 879c869e672SAlan Cox if (rv->object == NULL) 880c869e672SAlan Cox return (false); 881c869e672SAlan Cox return (popmap_is_clear(rv->popmap, m - rv->pages)); 882c869e672SAlan Cox } 883c869e672SAlan Cox 884c869e672SAlan Cox /* 885c869e672SAlan Cox * If the given page belongs to a reservation, returns the level of that 886c869e672SAlan Cox * reservation. Otherwise, returns -1. 887c869e672SAlan Cox */ 888c869e672SAlan Cox int 889c869e672SAlan Cox vm_reserv_level(vm_page_t m) 890c869e672SAlan Cox { 891c869e672SAlan Cox vm_reserv_t rv; 892c869e672SAlan Cox 893c869e672SAlan Cox rv = vm_reserv_from_page(m); 894c869e672SAlan Cox return (rv->object != NULL ? 0 : -1); 895c869e672SAlan Cox } 896c869e672SAlan Cox 897c869e672SAlan Cox /* 898*3453bca8SAlan 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 /* 911*3453bca8SAlan Cox * Breaks the given partially populated reservation, releasing its free pages 912*3453bca8SAlan Cox * to the physical memory allocator. 913f8a47341SAlan Cox * 914f8a47341SAlan Cox * The free page queue lock must be held. 915f8a47341SAlan Cox */ 91644aab2c3SAlan Cox static void 91744aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv) 918f8a47341SAlan Cox { 919f8a47341SAlan Cox 920f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 921f8a47341SAlan Cox KASSERT(rv->inpartpopq, 922ec179322SAlan Cox ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv)); 923f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 924f8a47341SAlan Cox rv->inpartpopq = FALSE; 925ec179322SAlan Cox vm_reserv_break(rv, NULL); 926f8a47341SAlan Cox vm_reserv_reclaimed++; 92744aab2c3SAlan Cox } 92844aab2c3SAlan Cox 92944aab2c3SAlan Cox /* 930*3453bca8SAlan Cox * Breaks the reservation at the head of the partially populated reservation 931*3453bca8SAlan Cox * queue, releasing its free pages to the physical memory allocator. Returns 932*3453bca8SAlan Cox * TRUE if a reservation is broken and FALSE otherwise. 93344aab2c3SAlan Cox * 93444aab2c3SAlan Cox * The free page queue lock must be held. 93544aab2c3SAlan Cox */ 93644aab2c3SAlan Cox boolean_t 93744aab2c3SAlan Cox vm_reserv_reclaim_inactive(void) 93844aab2c3SAlan Cox { 93944aab2c3SAlan Cox vm_reserv_t rv; 94044aab2c3SAlan Cox 94144aab2c3SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 94244aab2c3SAlan Cox if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) { 94344aab2c3SAlan Cox vm_reserv_reclaim(rv); 944f8a47341SAlan Cox return (TRUE); 945f8a47341SAlan Cox } 946f8a47341SAlan Cox return (FALSE); 947f8a47341SAlan Cox } 948f8a47341SAlan Cox 949f8a47341SAlan Cox /* 950*3453bca8SAlan Cox * Searches the partially populated reservation queue for the least recently 951*3453bca8SAlan Cox * changed reservation with free pages that satisfy the given request for 952*3453bca8SAlan Cox * contiguous physical memory. If a satisfactory reservation is found, it is 953*3453bca8SAlan Cox * broken. Returns TRUE if a reservation is broken and FALSE otherwise. 95444aab2c3SAlan Cox * 95544aab2c3SAlan Cox * The free page queue lock must be held. 95644aab2c3SAlan Cox */ 95744aab2c3SAlan Cox boolean_t 958c68c3537SAlan Cox vm_reserv_reclaim_contig(u_long npages, vm_paddr_t low, vm_paddr_t high, 9595c1f2cc4SAlan Cox u_long alignment, vm_paddr_t boundary) 96044aab2c3SAlan Cox { 961ec179322SAlan Cox vm_paddr_t pa, size; 96244aab2c3SAlan Cox vm_reserv_t rv; 96367b7e434SAlan Cox int hi, i, lo, low_index, next_free; 96444aab2c3SAlan Cox 96544aab2c3SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 966c68c3537SAlan Cox if (npages > VM_LEVEL_0_NPAGES - 1) 96744aab2c3SAlan Cox return (FALSE); 968c68c3537SAlan Cox size = npages << PAGE_SHIFT; 96944aab2c3SAlan Cox TAILQ_FOREACH(rv, &vm_rvq_partpop, partpopq) { 97044aab2c3SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]); 97144aab2c3SAlan Cox if (pa + PAGE_SIZE - size < low) { 972ec179322SAlan Cox /* This entire reservation is too low; go to next. */ 97344aab2c3SAlan Cox continue; 97444aab2c3SAlan Cox } 975ec179322SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[0]); 97644aab2c3SAlan Cox if (pa + size > high) { 977ec179322SAlan Cox /* This entire reservation is too high; go to next. */ 978ec179322SAlan Cox continue; 97985f2a0c9SMax Laier } 980ec179322SAlan Cox if (pa < low) { 981ec179322SAlan Cox /* Start the search for free pages at "low". */ 98267b7e434SAlan Cox low_index = (low + PAGE_MASK - pa) >> PAGE_SHIFT; 98367b7e434SAlan Cox i = low_index / NBPOPMAP; 98467b7e434SAlan Cox hi = low_index % NBPOPMAP; 985ec179322SAlan Cox } else 986ec179322SAlan Cox i = hi = 0; 987ec179322SAlan Cox do { 988ec179322SAlan Cox /* Find the next free page. */ 989ec179322SAlan Cox lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i])); 990ec179322SAlan Cox while (lo == 0 && ++i < NPOPMAP) 991ec179322SAlan Cox lo = ffsl(~rv->popmap[i]); 992ec179322SAlan Cox if (i == NPOPMAP) 993ec179322SAlan Cox break; 994ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 995ec179322SAlan Cox lo--; 996ec179322SAlan Cox next_free = NBPOPMAP * i + lo; 997ec179322SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]); 998ec179322SAlan Cox KASSERT(pa >= low, 999ec179322SAlan Cox ("vm_reserv_reclaim_contig: pa is too low")); 1000ec179322SAlan Cox if (pa + size > high) { 1001ec179322SAlan Cox /* The rest of this reservation is too high. */ 1002ec179322SAlan Cox break; 1003ec179322SAlan Cox } else if ((pa & (alignment - 1)) != 0 || 1004ec179322SAlan Cox ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) { 10056a93e36bSAlan Cox /* 10066a93e36bSAlan Cox * The current page doesn't meet the alignment 10076a93e36bSAlan Cox * and/or boundary requirements. Continue 10086a93e36bSAlan Cox * searching this reservation until the rest 10096a93e36bSAlan Cox * of its free pages are either excluded or 10106a93e36bSAlan Cox * exhausted. 10116a93e36bSAlan Cox */ 10126a93e36bSAlan Cox hi = lo + 1; 10136a93e36bSAlan Cox if (hi >= NBPOPMAP) { 10146a93e36bSAlan Cox hi = 0; 10156a93e36bSAlan Cox i++; 10166a93e36bSAlan Cox } 1017ec179322SAlan Cox continue; 1018ec179322SAlan Cox } 1019ec179322SAlan Cox /* Find the next used page. */ 1020ec179322SAlan Cox hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1)); 1021ec179322SAlan Cox while (hi == 0 && ++i < NPOPMAP) { 1022ec179322SAlan Cox if ((NBPOPMAP * i - next_free) * PAGE_SIZE >= 1023ec179322SAlan Cox size) { 102444aab2c3SAlan Cox vm_reserv_reclaim(rv); 102544aab2c3SAlan Cox return (TRUE); 102644aab2c3SAlan Cox } 1027ec179322SAlan Cox hi = ffsl(rv->popmap[i]); 1028ec179322SAlan Cox } 1029ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 1030ec179322SAlan Cox if (i != NPOPMAP) 1031ec179322SAlan Cox hi--; 1032ec179322SAlan Cox if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >= 1033ec179322SAlan Cox size) { 1034ec179322SAlan Cox vm_reserv_reclaim(rv); 1035ec179322SAlan Cox return (TRUE); 1036ec179322SAlan Cox } 1037ec179322SAlan Cox } while (i < NPOPMAP); 103844aab2c3SAlan Cox } 103944aab2c3SAlan Cox return (FALSE); 104044aab2c3SAlan Cox } 104144aab2c3SAlan Cox 104244aab2c3SAlan Cox /* 1043f8a47341SAlan Cox * Transfers the reservation underlying the given page to a new object. 1044f8a47341SAlan Cox * 1045f8a47341SAlan Cox * The object must be locked. 1046f8a47341SAlan Cox */ 1047f8a47341SAlan Cox void 1048f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object, 1049f8a47341SAlan Cox vm_pindex_t old_object_offset) 1050f8a47341SAlan Cox { 1051f8a47341SAlan Cox vm_reserv_t rv; 1052f8a47341SAlan Cox 105389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(new_object); 1054f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1055f8a47341SAlan Cox if (rv->object == old_object) { 1056f8a47341SAlan Cox mtx_lock(&vm_page_queue_free_mtx); 1057f8a47341SAlan Cox if (rv->object == old_object) { 1058f8a47341SAlan Cox LIST_REMOVE(rv, objq); 1059f8a47341SAlan Cox LIST_INSERT_HEAD(&new_object->rvq, rv, objq); 1060f8a47341SAlan Cox rv->object = new_object; 1061f8a47341SAlan Cox rv->pindex -= old_object_offset; 1062f8a47341SAlan Cox } 1063f8a47341SAlan Cox mtx_unlock(&vm_page_queue_free_mtx); 1064f8a47341SAlan Cox } 1065f8a47341SAlan Cox } 1066f8a47341SAlan Cox 1067f8a47341SAlan Cox /* 1068c869e672SAlan Cox * Returns the size (in bytes) of a reservation of the specified level. 1069c869e672SAlan Cox */ 1070c869e672SAlan Cox int 1071c869e672SAlan Cox vm_reserv_size(int level) 1072c869e672SAlan Cox { 1073c869e672SAlan Cox 1074c869e672SAlan Cox switch (level) { 1075c869e672SAlan Cox case 0: 1076c869e672SAlan Cox return (VM_LEVEL_0_SIZE); 1077c869e672SAlan Cox case -1: 1078c869e672SAlan Cox return (PAGE_SIZE); 1079c869e672SAlan Cox default: 1080c869e672SAlan Cox return (0); 1081c869e672SAlan Cox } 1082c869e672SAlan Cox } 1083c869e672SAlan Cox 1084c869e672SAlan Cox /* 1085f8a47341SAlan Cox * Allocates the virtual and physical memory required by the reservation 1086f8a47341SAlan Cox * management system's data structures, in particular, the reservation array. 1087f8a47341SAlan Cox */ 1088f8a47341SAlan Cox vm_paddr_t 1089f8a47341SAlan Cox vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water) 1090f8a47341SAlan Cox { 1091f8a47341SAlan Cox vm_paddr_t new_end; 1092f8a47341SAlan Cox size_t size; 1093f8a47341SAlan Cox 1094f8a47341SAlan Cox /* 1095f8a47341SAlan Cox * Calculate the size (in bytes) of the reservation array. Round up 1096f8a47341SAlan Cox * from "high_water" because every small page is mapped to an element 1097f8a47341SAlan Cox * in the reservation array based on its physical address. Thus, the 1098f8a47341SAlan Cox * number of elements in the reservation array can be greater than the 1099f8a47341SAlan Cox * number of superpages. 1100f8a47341SAlan Cox */ 1101f8a47341SAlan Cox size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv); 1102f8a47341SAlan Cox 1103f8a47341SAlan Cox /* 1104f8a47341SAlan Cox * Allocate and map the physical memory for the reservation array. The 1105f8a47341SAlan Cox * next available virtual address is returned by reference. 1106f8a47341SAlan Cox */ 1107f8a47341SAlan Cox new_end = end - round_page(size); 1108f8a47341SAlan Cox vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end, 1109f8a47341SAlan Cox VM_PROT_READ | VM_PROT_WRITE); 1110f8a47341SAlan Cox bzero(vm_reserv_array, size); 1111f8a47341SAlan Cox 1112f8a47341SAlan Cox /* 1113f8a47341SAlan Cox * Return the next available physical address. 1114f8a47341SAlan Cox */ 1115f8a47341SAlan Cox return (new_end); 1116f8a47341SAlan Cox } 1117f8a47341SAlan Cox 1118f8a47341SAlan Cox #endif /* VM_NRESERVLEVEL > 0 */ 1119