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 220f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS); 221f8a47341SAlan Cox 222f8a47341SAlan Cox SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, 223f8a47341SAlan Cox sysctl_vm_reserv_partpopq, "A", "Partially-populated reservation queues"); 224f8a47341SAlan Cox 225f8a47341SAlan Cox static long vm_reserv_reclaimed; 226f8a47341SAlan Cox SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD, 227f8a47341SAlan Cox &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations"); 228f8a47341SAlan Cox 229ec179322SAlan Cox static void vm_reserv_break(vm_reserv_t rv, vm_page_t m); 230ec179322SAlan Cox static void vm_reserv_depopulate(vm_reserv_t rv, int index); 231f8a47341SAlan Cox static vm_reserv_t vm_reserv_from_page(vm_page_t m); 232f8a47341SAlan Cox static boolean_t vm_reserv_has_pindex(vm_reserv_t rv, 233f8a47341SAlan Cox vm_pindex_t pindex); 234ec179322SAlan Cox static void vm_reserv_populate(vm_reserv_t rv, int index); 23544aab2c3SAlan Cox static void vm_reserv_reclaim(vm_reserv_t rv); 236f8a47341SAlan Cox 237f8a47341SAlan Cox /* 238f8a47341SAlan Cox * Describes the current state of the partially-populated reservation queue. 239f8a47341SAlan Cox */ 240f8a47341SAlan Cox static int 241f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS) 242f8a47341SAlan Cox { 243f8a47341SAlan Cox struct sbuf sbuf; 244f8a47341SAlan Cox vm_reserv_t rv; 245f8a47341SAlan Cox int counter, error, level, unused_pages; 246f8a47341SAlan Cox 24700f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 24800f0e671SMatthew D Fleming if (error != 0) 24900f0e671SMatthew D Fleming return (error); 2504e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 251f8a47341SAlan Cox sbuf_printf(&sbuf, "\nLEVEL SIZE NUMBER\n\n"); 252f8a47341SAlan Cox for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) { 253f8a47341SAlan Cox counter = 0; 254f8a47341SAlan Cox unused_pages = 0; 255f8a47341SAlan Cox mtx_lock(&vm_page_queue_free_mtx); 256f8a47341SAlan Cox TAILQ_FOREACH(rv, &vm_rvq_partpop/*[level]*/, partpopq) { 257f8a47341SAlan Cox counter++; 258f8a47341SAlan Cox unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt; 259f8a47341SAlan Cox } 260f8a47341SAlan Cox mtx_unlock(&vm_page_queue_free_mtx); 261d689bc00SAlan Cox sbuf_printf(&sbuf, "%5d: %6dK, %6d\n", level, 2622cf36c8fSAlan Cox unused_pages * ((int)PAGE_SIZE / 1024), counter); 263f8a47341SAlan Cox } 2644e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 265f8a47341SAlan Cox sbuf_delete(&sbuf); 266f8a47341SAlan Cox return (error); 267f8a47341SAlan Cox } 268f8a47341SAlan Cox 269f8a47341SAlan Cox /* 270f8a47341SAlan Cox * Reduces the given reservation's population count. If the population count 271f8a47341SAlan Cox * becomes zero, the reservation is destroyed. Additionally, moves the 272ec179322SAlan Cox * reservation to the tail of the partially-populated reservation queue if the 273f8a47341SAlan Cox * population count is non-zero. 274f8a47341SAlan Cox * 275f8a47341SAlan Cox * The free page queue lock must be held. 276f8a47341SAlan Cox */ 277f8a47341SAlan Cox static void 278ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index) 279f8a47341SAlan Cox { 280f8a47341SAlan Cox 281f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 282f8a47341SAlan Cox KASSERT(rv->object != NULL, 283f8a47341SAlan Cox ("vm_reserv_depopulate: reserv %p is free", rv)); 2843180f757SAlan Cox KASSERT(popmap_is_set(rv->popmap, index), 285a08c1515SAlan Cox ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv, 286a08c1515SAlan Cox index)); 287f8a47341SAlan Cox KASSERT(rv->popcnt > 0, 288f8a47341SAlan Cox ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv)); 289f8a47341SAlan Cox if (rv->inpartpopq) { 290f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 291f8a47341SAlan Cox rv->inpartpopq = FALSE; 292dd05fa19SAlan Cox } else { 293dd05fa19SAlan Cox KASSERT(rv->pages->psind == 1, 294dd05fa19SAlan Cox ("vm_reserv_depopulate: reserv %p is already demoted", 295dd05fa19SAlan Cox rv)); 296dd05fa19SAlan Cox rv->pages->psind = 0; 297f8a47341SAlan Cox } 2983180f757SAlan Cox popmap_clear(rv->popmap, index); 299f8a47341SAlan Cox rv->popcnt--; 300f8a47341SAlan Cox if (rv->popcnt == 0) { 301f8a47341SAlan Cox LIST_REMOVE(rv, objq); 302f8a47341SAlan Cox rv->object = NULL; 303f8a47341SAlan Cox vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER); 304f8a47341SAlan Cox vm_reserv_freed++; 305f8a47341SAlan Cox } else { 306f8a47341SAlan Cox rv->inpartpopq = TRUE; 307ab5378cfSAlan Cox TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq); 308f8a47341SAlan Cox } 309f8a47341SAlan Cox } 310f8a47341SAlan Cox 311f8a47341SAlan Cox /* 312f8a47341SAlan Cox * Returns the reservation to which the given page might belong. 313f8a47341SAlan Cox */ 314f8a47341SAlan Cox static __inline vm_reserv_t 315f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m) 316f8a47341SAlan Cox { 317f8a47341SAlan Cox 318f8a47341SAlan Cox return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]); 319f8a47341SAlan Cox } 320f8a47341SAlan Cox 321f8a47341SAlan Cox /* 322f8a47341SAlan Cox * Returns TRUE if the given reservation contains the given page index and 323f8a47341SAlan Cox * FALSE otherwise. 324f8a47341SAlan Cox */ 325f8a47341SAlan Cox static __inline boolean_t 326f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex) 327f8a47341SAlan Cox { 328f8a47341SAlan Cox 329f8a47341SAlan Cox return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0); 330f8a47341SAlan Cox } 331f8a47341SAlan Cox 332f8a47341SAlan Cox /* 333f8a47341SAlan Cox * Increases the given reservation's population count. Moves the reservation 334f8a47341SAlan Cox * to the tail of the partially-populated reservation queue. 335f8a47341SAlan Cox * 336f8a47341SAlan Cox * The free page queue must be locked. 337f8a47341SAlan Cox */ 338f8a47341SAlan Cox static void 339ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index) 340f8a47341SAlan Cox { 341f8a47341SAlan Cox 342f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 343f8a47341SAlan Cox KASSERT(rv->object != NULL, 344f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is free", rv)); 3453180f757SAlan Cox KASSERT(popmap_is_clear(rv->popmap, index), 346a08c1515SAlan Cox ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv, 347a08c1515SAlan Cox index)); 348f8a47341SAlan Cox KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES, 349f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is already full", rv)); 350dd05fa19SAlan Cox KASSERT(rv->pages->psind == 0, 351dd05fa19SAlan Cox ("vm_reserv_populate: reserv %p is already promoted", rv)); 352f8a47341SAlan Cox if (rv->inpartpopq) { 353f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 354f8a47341SAlan Cox rv->inpartpopq = FALSE; 355f8a47341SAlan Cox } 3563180f757SAlan Cox popmap_set(rv->popmap, index); 357f8a47341SAlan Cox rv->popcnt++; 358f8a47341SAlan Cox if (rv->popcnt < VM_LEVEL_0_NPAGES) { 359f8a47341SAlan Cox rv->inpartpopq = TRUE; 360f8a47341SAlan Cox TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq); 361dd05fa19SAlan Cox } else 362dd05fa19SAlan Cox rv->pages->psind = 1; 363f8a47341SAlan Cox } 364f8a47341SAlan Cox 365f8a47341SAlan Cox /* 366c68c3537SAlan Cox * Allocates a contiguous set of physical pages of the given size "npages" 36764f096eeSAlan Cox * from existing or newly created reservations. All of the physical pages 368c68c3537SAlan Cox * must be at or above the given physical address "low" and below the given 369c68c3537SAlan Cox * physical address "high". The given value "alignment" determines the 370c68c3537SAlan Cox * alignment of the first physical page in the set. If the given value 371c68c3537SAlan Cox * "boundary" is non-zero, then the set of physical pages cannot cross any 372c68c3537SAlan Cox * physical address boundary that is a multiple of that value. Both 373c68c3537SAlan Cox * "alignment" and "boundary" must be a power of two. 374c68c3537SAlan Cox * 375c68c3537SAlan Cox * The object and free page queue must be locked. 376c68c3537SAlan Cox */ 377c68c3537SAlan Cox vm_page_t 378c68c3537SAlan Cox vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, u_long npages, 379c68c3537SAlan Cox vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 380c68c3537SAlan Cox { 381c68c3537SAlan Cox vm_paddr_t pa, size; 382c68c3537SAlan Cox vm_page_t m, m_ret, mpred, msucc; 383c68c3537SAlan Cox vm_pindex_t first, leftcap, rightcap; 384c68c3537SAlan Cox vm_reserv_t rv; 385c68c3537SAlan Cox u_long allocpages, maxpages, minpages; 386c68c3537SAlan Cox int i, index, n; 387c68c3537SAlan Cox 388c68c3537SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 38989f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 390c68c3537SAlan Cox KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0")); 391c68c3537SAlan Cox 392c68c3537SAlan Cox /* 393c68c3537SAlan Cox * Is a reservation fundamentally impossible? 394c68c3537SAlan Cox */ 395c68c3537SAlan Cox if (pindex < VM_RESERV_INDEX(object, pindex) || 396c68c3537SAlan Cox pindex + npages > object->size) 397c68c3537SAlan Cox return (NULL); 398c68c3537SAlan Cox 399c68c3537SAlan Cox /* 400c68c3537SAlan Cox * All reservations of a particular size have the same alignment. 401c68c3537SAlan Cox * Assuming that the first page is allocated from a reservation, the 402c68c3537SAlan Cox * least significant bits of its physical address can be determined 403c68c3537SAlan Cox * from its offset from the beginning of the reservation and the size 404c68c3537SAlan Cox * of the reservation. 405c68c3537SAlan Cox * 406c68c3537SAlan Cox * Could the specified index within a reservation of the smallest 407c68c3537SAlan Cox * possible size satisfy the alignment and boundary requirements? 408c68c3537SAlan Cox */ 409c68c3537SAlan Cox pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT; 410c68c3537SAlan Cox if ((pa & (alignment - 1)) != 0) 411c68c3537SAlan Cox return (NULL); 412c68c3537SAlan Cox size = npages << PAGE_SHIFT; 413c68c3537SAlan Cox if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 414c68c3537SAlan Cox return (NULL); 415c68c3537SAlan Cox 416c68c3537SAlan Cox /* 417c68c3537SAlan Cox * Look for an existing reservation. 418c68c3537SAlan Cox */ 419774d251dSAttilio Rao mpred = vm_radix_lookup_le(&object->rtree, pindex); 420774d251dSAttilio Rao if (mpred != NULL) { 421774d251dSAttilio Rao KASSERT(mpred->pindex < pindex, 422c68c3537SAlan Cox ("vm_reserv_alloc_contig: pindex already allocated")); 423c68c3537SAlan Cox rv = vm_reserv_from_page(mpred); 424c68c3537SAlan Cox if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 425c68c3537SAlan Cox goto found; 426774d251dSAttilio Rao msucc = TAILQ_NEXT(mpred, listq); 427774d251dSAttilio Rao } else 428774d251dSAttilio Rao msucc = TAILQ_FIRST(&object->memq); 429774d251dSAttilio Rao if (msucc != NULL) { 430774d251dSAttilio Rao KASSERT(msucc->pindex > pindex, 431774d251dSAttilio Rao ("vm_reserv_alloc_page: pindex already allocated")); 432c68c3537SAlan Cox rv = vm_reserv_from_page(msucc); 433774d251dSAttilio Rao if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 434c68c3537SAlan Cox goto found; 435c68c3537SAlan Cox } 436c68c3537SAlan Cox 437c68c3537SAlan Cox /* 438c68c3537SAlan Cox * Could at least one reservation fit between the first index to the 43964f096eeSAlan Cox * left that can be used ("leftcap") and the first index to the right 44064f096eeSAlan Cox * that cannot be used ("rightcap")? 441c68c3537SAlan Cox */ 442c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 443c68c3537SAlan Cox if (mpred != NULL) { 444c68c3537SAlan Cox if ((rv = vm_reserv_from_page(mpred))->object != object) 445c68c3537SAlan Cox leftcap = mpred->pindex + 1; 446c68c3537SAlan Cox else 447c68c3537SAlan Cox leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 448c68c3537SAlan Cox if (leftcap > first) 449c68c3537SAlan Cox return (NULL); 450c68c3537SAlan Cox } 451c68c3537SAlan Cox minpages = VM_RESERV_INDEX(object, pindex) + npages; 452c68c3537SAlan Cox maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES); 453c68c3537SAlan Cox allocpages = maxpages; 454c68c3537SAlan Cox if (msucc != NULL) { 455c68c3537SAlan Cox if ((rv = vm_reserv_from_page(msucc))->object != object) 456c68c3537SAlan Cox rightcap = msucc->pindex; 457c68c3537SAlan Cox else 458c68c3537SAlan Cox rightcap = rv->pindex; 459c68c3537SAlan Cox if (first + maxpages > rightcap) { 460c68c3537SAlan Cox if (maxpages == VM_LEVEL_0_NPAGES) 461c68c3537SAlan Cox return (NULL); 46264f096eeSAlan Cox 46364f096eeSAlan Cox /* 46464f096eeSAlan Cox * At least one reservation will fit between "leftcap" 46564f096eeSAlan Cox * and "rightcap". However, a reservation for the 46664f096eeSAlan Cox * last of the requested pages will not fit. Reduce 46764f096eeSAlan Cox * the size of the upcoming allocation accordingly. 46864f096eeSAlan Cox */ 469c68c3537SAlan Cox allocpages = minpages; 470c68c3537SAlan Cox } 471c68c3537SAlan Cox } 472c68c3537SAlan Cox 473c68c3537SAlan Cox /* 474c68c3537SAlan Cox * Would the last new reservation extend past the end of the object? 475c68c3537SAlan Cox */ 476c68c3537SAlan Cox if (first + maxpages > object->size) { 477c68c3537SAlan Cox /* 478c68c3537SAlan Cox * Don't allocate the last new reservation if the object is a 479c68c3537SAlan Cox * vnode or backed by another object that is a vnode. 480c68c3537SAlan Cox */ 481c68c3537SAlan Cox if (object->type == OBJT_VNODE || 482c68c3537SAlan Cox (object->backing_object != NULL && 483c68c3537SAlan Cox object->backing_object->type == OBJT_VNODE)) { 484c68c3537SAlan Cox if (maxpages == VM_LEVEL_0_NPAGES) 485c68c3537SAlan Cox return (NULL); 486c68c3537SAlan Cox allocpages = minpages; 487c68c3537SAlan Cox } 488c68c3537SAlan Cox /* Speculate that the object may grow. */ 489c68c3537SAlan Cox } 490c68c3537SAlan Cox 491c68c3537SAlan Cox /* 49264f096eeSAlan Cox * Allocate the physical pages. The alignment and boundary specified 49364f096eeSAlan Cox * for this allocation may be different from the alignment and 49464f096eeSAlan Cox * boundary specified for the requested pages. For instance, the 49564f096eeSAlan Cox * specified index may not be the first page within the first new 49664f096eeSAlan Cox * reservation. 497c68c3537SAlan Cox */ 498c68c3537SAlan Cox m = vm_phys_alloc_contig(allocpages, low, high, ulmax(alignment, 499c68c3537SAlan Cox VM_LEVEL_0_SIZE), boundary > VM_LEVEL_0_SIZE ? boundary : 0); 500c68c3537SAlan Cox if (m == NULL) 501c68c3537SAlan Cox return (NULL); 50264f096eeSAlan Cox 50364f096eeSAlan Cox /* 50464f096eeSAlan Cox * The allocated physical pages always begin at a reservation 50564f096eeSAlan Cox * boundary, but they do not always end at a reservation boundary. 50664f096eeSAlan Cox * Initialize every reservation that is completely covered by the 50764f096eeSAlan Cox * allocated physical pages. 50864f096eeSAlan Cox */ 509c68c3537SAlan Cox m_ret = NULL; 510c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 511c68c3537SAlan Cox do { 512c68c3537SAlan Cox rv = vm_reserv_from_page(m); 513c68c3537SAlan Cox KASSERT(rv->pages == m, 514c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p's pages is corrupted", 515c68c3537SAlan Cox rv)); 516c68c3537SAlan Cox KASSERT(rv->object == NULL, 517c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p isn't free", rv)); 518c68c3537SAlan Cox LIST_INSERT_HEAD(&object->rvq, rv, objq); 519c68c3537SAlan Cox rv->object = object; 520c68c3537SAlan Cox rv->pindex = first; 521c68c3537SAlan Cox KASSERT(rv->popcnt == 0, 522c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p's popcnt is corrupted", 523c68c3537SAlan Cox rv)); 524c68c3537SAlan Cox KASSERT(!rv->inpartpopq, 525c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p's inpartpopq is TRUE", 526c68c3537SAlan Cox rv)); 527ec179322SAlan Cox for (i = 0; i < NPOPMAP; i++) 528ec179322SAlan Cox KASSERT(rv->popmap[i] == 0, 529ec179322SAlan Cox ("vm_reserv_alloc_contig: reserv %p's popmap is corrupted", 530ec179322SAlan Cox rv)); 531c68c3537SAlan Cox n = ulmin(VM_LEVEL_0_NPAGES - index, npages); 532c68c3537SAlan Cox for (i = 0; i < n; i++) 533ec179322SAlan Cox vm_reserv_populate(rv, index + i); 534c68c3537SAlan Cox npages -= n; 535c68c3537SAlan Cox if (m_ret == NULL) { 536c68c3537SAlan Cox m_ret = &rv->pages[index]; 537c68c3537SAlan Cox index = 0; 538c68c3537SAlan Cox } 539c68c3537SAlan Cox m += VM_LEVEL_0_NPAGES; 540c68c3537SAlan Cox first += VM_LEVEL_0_NPAGES; 541c68c3537SAlan Cox allocpages -= VM_LEVEL_0_NPAGES; 54264f096eeSAlan Cox } while (allocpages >= VM_LEVEL_0_NPAGES); 543c68c3537SAlan Cox return (m_ret); 544c68c3537SAlan Cox 545c68c3537SAlan Cox /* 546c68c3537SAlan Cox * Found a matching reservation. 547c68c3537SAlan Cox */ 548c68c3537SAlan Cox found: 549c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 550c68c3537SAlan Cox /* Does the allocation fit within the reservation? */ 551c68c3537SAlan Cox if (index + npages > VM_LEVEL_0_NPAGES) 552c68c3537SAlan Cox return (NULL); 553c68c3537SAlan Cox m = &rv->pages[index]; 554c68c3537SAlan Cox pa = VM_PAGE_TO_PHYS(m); 555c68c3537SAlan Cox if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 || 556c68c3537SAlan Cox ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 557c68c3537SAlan Cox return (NULL); 558c68c3537SAlan Cox /* Handle vm_page_rename(m, new_object, ...). */ 559c68c3537SAlan Cox for (i = 0; i < npages; i++) 5603180f757SAlan Cox if (popmap_is_set(rv->popmap, index + i)) 561c68c3537SAlan Cox return (NULL); 562c68c3537SAlan Cox for (i = 0; i < npages; i++) 563ec179322SAlan Cox vm_reserv_populate(rv, index + i); 564c68c3537SAlan Cox return (m); 565c68c3537SAlan Cox } 566c68c3537SAlan Cox 567c68c3537SAlan Cox /* 568f8a47341SAlan Cox * Allocates a page from an existing or newly-created reservation. 569f8a47341SAlan Cox * 570404eb1b3SAlan Cox * The page "mpred" must immediately precede the offset "pindex" within the 571404eb1b3SAlan Cox * specified object. 572404eb1b3SAlan Cox * 573f8a47341SAlan Cox * The object and free page queue must be locked. 574f8a47341SAlan Cox */ 575f8a47341SAlan Cox vm_page_t 576404eb1b3SAlan Cox vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, vm_page_t mpred) 577f8a47341SAlan Cox { 578404eb1b3SAlan Cox vm_page_t m, msucc; 579f8a47341SAlan Cox vm_pindex_t first, leftcap, rightcap; 580f8a47341SAlan Cox vm_reserv_t rv; 581ec179322SAlan Cox int i, index; 582f8a47341SAlan Cox 583f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 58489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 585f8a47341SAlan Cox 586f8a47341SAlan Cox /* 587c68c3537SAlan Cox * Is a reservation fundamentally impossible? 588f8a47341SAlan Cox */ 589f8a47341SAlan Cox if (pindex < VM_RESERV_INDEX(object, pindex) || 590f8a47341SAlan Cox pindex >= object->size) 591f8a47341SAlan Cox return (NULL); 592f8a47341SAlan Cox 593f8a47341SAlan Cox /* 594f8a47341SAlan Cox * Look for an existing reservation. 595f8a47341SAlan Cox */ 596774d251dSAttilio Rao if (mpred != NULL) { 5979eab5484SKonstantin Belousov KASSERT(mpred->object == object, 598404eb1b3SAlan Cox ("vm_reserv_alloc_page: object doesn't contain mpred")); 599774d251dSAttilio Rao KASSERT(mpred->pindex < pindex, 600404eb1b3SAlan Cox ("vm_reserv_alloc_page: mpred doesn't precede pindex")); 601f8a47341SAlan Cox rv = vm_reserv_from_page(mpred); 602c68c3537SAlan Cox if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 603c68c3537SAlan Cox goto found; 604774d251dSAttilio Rao msucc = TAILQ_NEXT(mpred, listq); 605774d251dSAttilio Rao } else 606774d251dSAttilio Rao msucc = TAILQ_FIRST(&object->memq); 607774d251dSAttilio Rao if (msucc != NULL) { 608774d251dSAttilio Rao KASSERT(msucc->pindex > pindex, 609404eb1b3SAlan Cox ("vm_reserv_alloc_page: msucc doesn't succeed pindex")); 610f8a47341SAlan Cox rv = vm_reserv_from_page(msucc); 611774d251dSAttilio Rao if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 612c68c3537SAlan Cox goto found; 613f8a47341SAlan Cox } 614f8a47341SAlan Cox 615f8a47341SAlan Cox /* 616c68c3537SAlan Cox * Could a reservation fit between the first index to the left that 617c68c3537SAlan Cox * can be used and the first index to the right that cannot be used? 618f8a47341SAlan Cox */ 619c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 620c68c3537SAlan Cox if (mpred != NULL) { 621c68c3537SAlan Cox if ((rv = vm_reserv_from_page(mpred))->object != object) 622f8a47341SAlan Cox leftcap = mpred->pindex + 1; 623f8a47341SAlan Cox else 624f8a47341SAlan Cox leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 625c68c3537SAlan Cox if (leftcap > first) 626c68c3537SAlan Cox return (NULL); 627c68c3537SAlan Cox } 628c68c3537SAlan Cox if (msucc != NULL) { 629c68c3537SAlan Cox if ((rv = vm_reserv_from_page(msucc))->object != object) 630f8a47341SAlan Cox rightcap = msucc->pindex; 631f8a47341SAlan Cox else 632f8a47341SAlan Cox rightcap = rv->pindex; 633c68c3537SAlan Cox if (first + VM_LEVEL_0_NPAGES > rightcap) 634f8a47341SAlan Cox return (NULL); 635c68c3537SAlan Cox } 636f8a47341SAlan Cox 637f8a47341SAlan Cox /* 638c68c3537SAlan Cox * Would a new reservation extend past the end of the object? 639f8a47341SAlan Cox */ 640c68c3537SAlan Cox if (first + VM_LEVEL_0_NPAGES > object->size) { 641f8a47341SAlan Cox /* 642f8a47341SAlan Cox * Don't allocate a new reservation if the object is a vnode or 643f8a47341SAlan Cox * backed by another object that is a vnode. 644f8a47341SAlan Cox */ 645f8a47341SAlan Cox if (object->type == OBJT_VNODE || 646f8a47341SAlan Cox (object->backing_object != NULL && 647f8a47341SAlan Cox object->backing_object->type == OBJT_VNODE)) 648f8a47341SAlan Cox return (NULL); 649f8a47341SAlan Cox /* Speculate that the object may grow. */ 650f8a47341SAlan Cox } 651f8a47341SAlan Cox 652f8a47341SAlan Cox /* 653c68c3537SAlan Cox * Allocate and populate the new reservation. 654f8a47341SAlan Cox */ 655f8a47341SAlan Cox m = vm_phys_alloc_pages(VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER); 656c68c3537SAlan Cox if (m == NULL) 657c68c3537SAlan Cox return (NULL); 658f8a47341SAlan Cox rv = vm_reserv_from_page(m); 659f8a47341SAlan Cox KASSERT(rv->pages == m, 660c68c3537SAlan Cox ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv)); 661f8a47341SAlan Cox KASSERT(rv->object == NULL, 662f8a47341SAlan Cox ("vm_reserv_alloc_page: reserv %p isn't free", rv)); 663f8a47341SAlan Cox LIST_INSERT_HEAD(&object->rvq, rv, objq); 664f8a47341SAlan Cox rv->object = object; 665f8a47341SAlan Cox rv->pindex = first; 666f8a47341SAlan Cox KASSERT(rv->popcnt == 0, 667c68c3537SAlan Cox ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted", rv)); 668f8a47341SAlan Cox KASSERT(!rv->inpartpopq, 669c68c3537SAlan Cox ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE", rv)); 670ec179322SAlan Cox for (i = 0; i < NPOPMAP; i++) 671ec179322SAlan Cox KASSERT(rv->popmap[i] == 0, 672ec179322SAlan Cox ("vm_reserv_alloc_page: reserv %p's popmap is corrupted", 673ec179322SAlan Cox rv)); 674ec179322SAlan Cox index = VM_RESERV_INDEX(object, pindex); 675ec179322SAlan Cox vm_reserv_populate(rv, index); 676ec179322SAlan Cox return (&rv->pages[index]); 677c68c3537SAlan Cox 678c68c3537SAlan Cox /* 679c68c3537SAlan Cox * Found a matching reservation. 680c68c3537SAlan Cox */ 681c68c3537SAlan Cox found: 682ec179322SAlan Cox index = VM_RESERV_INDEX(object, pindex); 683ec179322SAlan Cox m = &rv->pages[index]; 684c68c3537SAlan Cox /* Handle vm_page_rename(m, new_object, ...). */ 6853180f757SAlan Cox if (popmap_is_set(rv->popmap, index)) 686c68c3537SAlan Cox return (NULL); 687ec179322SAlan Cox vm_reserv_populate(rv, index); 688f8a47341SAlan Cox return (m); 689f8a47341SAlan Cox } 690f8a47341SAlan Cox 691f8a47341SAlan Cox /* 692ec179322SAlan Cox * Breaks the given reservation. Except for the specified cached or free 693ec179322SAlan Cox * page, all cached and free pages in the reservation are returned to the 694ec179322SAlan Cox * physical memory allocator. The reservation's population count and map are 695ec179322SAlan Cox * reset to their initial state. 696ec179322SAlan Cox * 697ec179322SAlan Cox * The given reservation must not be in the partially-populated reservation 698ec179322SAlan Cox * queue. The free page queue lock must be held. 699ec179322SAlan Cox */ 700ec179322SAlan Cox static void 701ec179322SAlan Cox vm_reserv_break(vm_reserv_t rv, vm_page_t m) 702ec179322SAlan Cox { 703ec179322SAlan Cox int begin_zeroes, hi, i, lo; 704ec179322SAlan Cox 705ec179322SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 706ec179322SAlan Cox KASSERT(rv->object != NULL, 707ec179322SAlan Cox ("vm_reserv_break: reserv %p is free", rv)); 708ec179322SAlan Cox KASSERT(!rv->inpartpopq, 709ec179322SAlan Cox ("vm_reserv_break: reserv %p's inpartpopq is TRUE", rv)); 710ec179322SAlan Cox LIST_REMOVE(rv, objq); 711ec179322SAlan Cox rv->object = NULL; 712ec179322SAlan Cox if (m != NULL) { 713ec179322SAlan Cox /* 714ec179322SAlan Cox * Since the reservation is being broken, there is no harm in 715ec179322SAlan Cox * abusing the population map to stop "m" from being returned 716ec179322SAlan Cox * to the physical memory allocator. 717ec179322SAlan Cox */ 718ec179322SAlan Cox i = m - rv->pages; 7193180f757SAlan Cox KASSERT(popmap_is_clear(rv->popmap, i), 720ec179322SAlan Cox ("vm_reserv_break: reserv %p's popmap is corrupted", rv)); 7213180f757SAlan Cox popmap_set(rv->popmap, i); 722ec179322SAlan Cox rv->popcnt++; 723ec179322SAlan Cox } 724ec179322SAlan Cox i = hi = 0; 725ec179322SAlan Cox do { 726ec179322SAlan Cox /* Find the next 0 bit. Any previous 0 bits are < "hi". */ 727ec179322SAlan Cox lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i])); 728ec179322SAlan Cox if (lo == 0) { 729ec179322SAlan Cox /* Redundantly clears bits < "hi". */ 730ec179322SAlan Cox rv->popmap[i] = 0; 731ec179322SAlan Cox rv->popcnt -= NBPOPMAP - hi; 732ec179322SAlan Cox while (++i < NPOPMAP) { 733ec179322SAlan Cox lo = ffsl(~rv->popmap[i]); 734ec179322SAlan Cox if (lo == 0) { 735ec179322SAlan Cox rv->popmap[i] = 0; 736ec179322SAlan Cox rv->popcnt -= NBPOPMAP; 737ec179322SAlan Cox } else 738ec179322SAlan Cox break; 739ec179322SAlan Cox } 740ec179322SAlan Cox if (i == NPOPMAP) 741ec179322SAlan Cox break; 742ec179322SAlan Cox hi = 0; 743ec179322SAlan Cox } 744ec179322SAlan Cox KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo)); 745ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 746ec179322SAlan Cox lo--; 747ec179322SAlan Cox if (lo > 0) { 748ec179322SAlan Cox /* Redundantly clears bits < "hi". */ 749ec179322SAlan Cox rv->popmap[i] &= ~((1UL << lo) - 1); 750ec179322SAlan Cox rv->popcnt -= lo - hi; 751ec179322SAlan Cox } 752ec179322SAlan Cox begin_zeroes = NBPOPMAP * i + lo; 753ec179322SAlan Cox /* Find the next 1 bit. */ 754ec179322SAlan Cox do 755ec179322SAlan Cox hi = ffsl(rv->popmap[i]); 756ec179322SAlan Cox while (hi == 0 && ++i < NPOPMAP); 757ec179322SAlan Cox if (i != NPOPMAP) 758ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 759ec179322SAlan Cox hi--; 760ec179322SAlan Cox vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i + 761ec179322SAlan Cox hi - begin_zeroes); 762ec179322SAlan Cox } while (i < NPOPMAP); 763ec179322SAlan Cox KASSERT(rv->popcnt == 0, 764ec179322SAlan Cox ("vm_reserv_break: reserv %p's popcnt is corrupted", rv)); 765ec179322SAlan Cox vm_reserv_broken++; 766ec179322SAlan Cox } 767ec179322SAlan Cox 768ec179322SAlan Cox /* 769f8a47341SAlan Cox * Breaks all reservations belonging to the given object. 770f8a47341SAlan Cox */ 771f8a47341SAlan Cox void 772f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object) 773f8a47341SAlan Cox { 774f8a47341SAlan Cox vm_reserv_t rv; 775f8a47341SAlan Cox 776f8a47341SAlan Cox mtx_lock(&vm_page_queue_free_mtx); 777f8a47341SAlan Cox while ((rv = LIST_FIRST(&object->rvq)) != NULL) { 778f8a47341SAlan Cox KASSERT(rv->object == object, 779f8a47341SAlan Cox ("vm_reserv_break_all: reserv %p is corrupted", rv)); 780f8a47341SAlan Cox if (rv->inpartpopq) { 781f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 782f8a47341SAlan Cox rv->inpartpopq = FALSE; 783f8a47341SAlan Cox } 784ec179322SAlan Cox vm_reserv_break(rv, NULL); 785f8a47341SAlan Cox } 786f8a47341SAlan Cox mtx_unlock(&vm_page_queue_free_mtx); 787f8a47341SAlan Cox } 788f8a47341SAlan Cox 789f8a47341SAlan Cox /* 790f8a47341SAlan Cox * Frees the given page if it belongs to a reservation. Returns TRUE if the 791f8a47341SAlan Cox * page is freed and FALSE otherwise. 792f8a47341SAlan Cox * 793f8a47341SAlan Cox * The free page queue lock must be held. 794f8a47341SAlan Cox */ 795f8a47341SAlan Cox boolean_t 796f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m) 797f8a47341SAlan Cox { 798f8a47341SAlan Cox vm_reserv_t rv; 799f8a47341SAlan Cox 800f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 801f8a47341SAlan Cox rv = vm_reserv_from_page(m); 802908e3da1SAlan Cox if (rv->object == NULL) 803908e3da1SAlan Cox return (FALSE); 804908e3da1SAlan Cox if ((m->flags & PG_CACHED) != 0 && m->pool != VM_FREEPOOL_CACHE) 805908e3da1SAlan Cox vm_phys_set_pool(VM_FREEPOOL_CACHE, rv->pages, 806908e3da1SAlan Cox VM_LEVEL_0_ORDER); 807ec179322SAlan Cox vm_reserv_depopulate(rv, m - rv->pages); 808f8a47341SAlan Cox return (TRUE); 809f8a47341SAlan Cox } 810f8a47341SAlan Cox 811f8a47341SAlan Cox /* 812f8a47341SAlan Cox * Initializes the reservation management system. Specifically, initializes 813f8a47341SAlan Cox * the reservation array. 814f8a47341SAlan Cox * 815f8a47341SAlan Cox * Requires that vm_page_array and first_page are initialized! 816f8a47341SAlan Cox */ 817f8a47341SAlan Cox void 818f8a47341SAlan Cox vm_reserv_init(void) 819f8a47341SAlan Cox { 820f8a47341SAlan Cox vm_paddr_t paddr; 821*09e5f3c4SAlan Cox struct vm_phys_seg *seg; 822*09e5f3c4SAlan Cox int segind; 823f8a47341SAlan Cox 824f8a47341SAlan Cox /* 825f8a47341SAlan Cox * Initialize the reservation array. Specifically, initialize the 826f8a47341SAlan Cox * "pages" field for every element that has an underlying superpage. 827f8a47341SAlan Cox */ 828*09e5f3c4SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 829*09e5f3c4SAlan Cox seg = &vm_phys_segs[segind]; 830*09e5f3c4SAlan Cox paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 831*09e5f3c4SAlan Cox while (paddr + VM_LEVEL_0_SIZE <= seg->end) { 832f8a47341SAlan Cox vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages = 833f8a47341SAlan Cox PHYS_TO_VM_PAGE(paddr); 834f8a47341SAlan Cox paddr += VM_LEVEL_0_SIZE; 835f8a47341SAlan Cox } 836f8a47341SAlan Cox } 837f8a47341SAlan Cox } 838f8a47341SAlan Cox 839f8a47341SAlan Cox /* 840f8a47341SAlan Cox * Returns a reservation level if the given page belongs to a fully-populated 841f8a47341SAlan Cox * reservation and -1 otherwise. 842f8a47341SAlan Cox */ 843f8a47341SAlan Cox int 844f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m) 845f8a47341SAlan Cox { 846f8a47341SAlan Cox vm_reserv_t rv; 847f8a47341SAlan Cox 848f8a47341SAlan Cox rv = vm_reserv_from_page(m); 849f8a47341SAlan Cox return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1); 850f8a47341SAlan Cox } 851f8a47341SAlan Cox 852f8a47341SAlan Cox /* 853f8a47341SAlan Cox * Prepare for the reactivation of a cached page. 854f8a47341SAlan Cox * 855f8a47341SAlan Cox * First, suppose that the given page "m" was allocated individually, i.e., not 856f8a47341SAlan Cox * as part of a reservation, and cached. Then, suppose a reservation 857f8a47341SAlan Cox * containing "m" is allocated by the same object. Although "m" and the 858f8a47341SAlan Cox * reservation belong to the same object, "m"'s pindex may not match the 859f8a47341SAlan Cox * reservation's. 860f8a47341SAlan Cox * 861f8a47341SAlan Cox * The free page queue must be locked. 862f8a47341SAlan Cox */ 863f8a47341SAlan Cox boolean_t 864f8a47341SAlan Cox vm_reserv_reactivate_page(vm_page_t m) 865f8a47341SAlan Cox { 866f8a47341SAlan Cox vm_reserv_t rv; 867ec179322SAlan Cox int index; 868f8a47341SAlan Cox 869f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 870f8a47341SAlan Cox rv = vm_reserv_from_page(m); 871f8a47341SAlan Cox if (rv->object == NULL) 872f8a47341SAlan Cox return (FALSE); 873f8a47341SAlan Cox KASSERT((m->flags & PG_CACHED) != 0, 874ec179322SAlan Cox ("vm_reserv_reactivate_page: page %p is not cached", m)); 875f8a47341SAlan Cox if (m->object == rv->object && 876ec179322SAlan Cox m->pindex - rv->pindex == (index = VM_RESERV_INDEX(m->object, 877ec179322SAlan Cox m->pindex))) 878ec179322SAlan Cox vm_reserv_populate(rv, index); 879f8a47341SAlan Cox else { 880f8a47341SAlan Cox KASSERT(rv->inpartpopq, 881ec179322SAlan Cox ("vm_reserv_reactivate_page: reserv %p's inpartpopq is FALSE", 882f8a47341SAlan Cox rv)); 883f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 884f8a47341SAlan Cox rv->inpartpopq = FALSE; 885ec179322SAlan Cox /* Don't release "m" to the physical memory allocator. */ 886ec179322SAlan Cox vm_reserv_break(rv, m); 887f8a47341SAlan Cox } 888f8a47341SAlan Cox return (TRUE); 889f8a47341SAlan Cox } 890f8a47341SAlan Cox 891f8a47341SAlan Cox /* 89244aab2c3SAlan Cox * Breaks the given partially-populated reservation, releasing its cached and 89344aab2c3SAlan Cox * free pages to the physical memory allocator. 894f8a47341SAlan Cox * 895f8a47341SAlan Cox * The free page queue lock must be held. 896f8a47341SAlan Cox */ 89744aab2c3SAlan Cox static void 89844aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv) 899f8a47341SAlan Cox { 900f8a47341SAlan Cox 901f8a47341SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 902f8a47341SAlan Cox KASSERT(rv->inpartpopq, 903ec179322SAlan Cox ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv)); 904f8a47341SAlan Cox TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 905f8a47341SAlan Cox rv->inpartpopq = FALSE; 906ec179322SAlan Cox vm_reserv_break(rv, NULL); 907f8a47341SAlan Cox vm_reserv_reclaimed++; 90844aab2c3SAlan Cox } 90944aab2c3SAlan Cox 91044aab2c3SAlan Cox /* 91144aab2c3SAlan Cox * Breaks the reservation at the head of the partially-populated reservation 91244aab2c3SAlan Cox * queue, releasing its cached and free pages to the physical memory 91344aab2c3SAlan Cox * allocator. Returns TRUE if a reservation is broken and FALSE otherwise. 91444aab2c3SAlan Cox * 91544aab2c3SAlan Cox * The free page queue lock must be held. 91644aab2c3SAlan Cox */ 91744aab2c3SAlan Cox boolean_t 91844aab2c3SAlan Cox vm_reserv_reclaim_inactive(void) 91944aab2c3SAlan Cox { 92044aab2c3SAlan Cox vm_reserv_t rv; 92144aab2c3SAlan Cox 92244aab2c3SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 92344aab2c3SAlan Cox if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) { 92444aab2c3SAlan Cox vm_reserv_reclaim(rv); 925f8a47341SAlan Cox return (TRUE); 926f8a47341SAlan Cox } 927f8a47341SAlan Cox return (FALSE); 928f8a47341SAlan Cox } 929f8a47341SAlan Cox 930f8a47341SAlan Cox /* 93144aab2c3SAlan Cox * Searches the partially-populated reservation queue for the least recently 93244aab2c3SAlan Cox * active reservation with unused pages, i.e., cached or free, that satisfy the 93344aab2c3SAlan Cox * given request for contiguous physical memory. If a satisfactory reservation 93444aab2c3SAlan Cox * is found, it is broken. Returns TRUE if a reservation is broken and FALSE 93544aab2c3SAlan Cox * otherwise. 93644aab2c3SAlan Cox * 93744aab2c3SAlan Cox * The free page queue lock must be held. 93844aab2c3SAlan Cox */ 93944aab2c3SAlan Cox boolean_t 940c68c3537SAlan Cox vm_reserv_reclaim_contig(u_long npages, vm_paddr_t low, vm_paddr_t high, 9415c1f2cc4SAlan Cox u_long alignment, vm_paddr_t boundary) 94244aab2c3SAlan Cox { 943ec179322SAlan Cox vm_paddr_t pa, size; 94444aab2c3SAlan Cox vm_reserv_t rv; 945ec179322SAlan Cox int hi, i, lo, next_free; 94644aab2c3SAlan Cox 94744aab2c3SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 948c68c3537SAlan Cox if (npages > VM_LEVEL_0_NPAGES - 1) 94944aab2c3SAlan Cox return (FALSE); 950c68c3537SAlan Cox size = npages << PAGE_SHIFT; 95144aab2c3SAlan Cox TAILQ_FOREACH(rv, &vm_rvq_partpop, partpopq) { 95244aab2c3SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]); 95344aab2c3SAlan Cox if (pa + PAGE_SIZE - size < low) { 954ec179322SAlan Cox /* This entire reservation is too low; go to next. */ 95544aab2c3SAlan Cox continue; 95644aab2c3SAlan Cox } 957ec179322SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[0]); 95844aab2c3SAlan Cox if (pa + size > high) { 959ec179322SAlan Cox /* This entire reservation is too high; go to next. */ 960ec179322SAlan Cox continue; 96185f2a0c9SMax Laier } 962ec179322SAlan Cox if (pa < low) { 963ec179322SAlan Cox /* Start the search for free pages at "low". */ 964ec179322SAlan Cox i = (low - pa) / NBPOPMAP; 965ec179322SAlan Cox hi = (low - pa) % NBPOPMAP; 966ec179322SAlan Cox } else 967ec179322SAlan Cox i = hi = 0; 968ec179322SAlan Cox do { 969ec179322SAlan Cox /* Find the next free page. */ 970ec179322SAlan Cox lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i])); 971ec179322SAlan Cox while (lo == 0 && ++i < NPOPMAP) 972ec179322SAlan Cox lo = ffsl(~rv->popmap[i]); 973ec179322SAlan Cox if (i == NPOPMAP) 974ec179322SAlan Cox break; 975ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 976ec179322SAlan Cox lo--; 977ec179322SAlan Cox next_free = NBPOPMAP * i + lo; 978ec179322SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]); 979ec179322SAlan Cox KASSERT(pa >= low, 980ec179322SAlan Cox ("vm_reserv_reclaim_contig: pa is too low")); 981ec179322SAlan Cox if (pa + size > high) { 982ec179322SAlan Cox /* The rest of this reservation is too high. */ 983ec179322SAlan Cox break; 984ec179322SAlan Cox } else if ((pa & (alignment - 1)) != 0 || 985ec179322SAlan Cox ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) { 986ec179322SAlan Cox /* Continue with this reservation. */ 987ec179322SAlan Cox hi = lo; 988ec179322SAlan Cox continue; 989ec179322SAlan Cox } 990ec179322SAlan Cox /* Find the next used page. */ 991ec179322SAlan Cox hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1)); 992ec179322SAlan Cox while (hi == 0 && ++i < NPOPMAP) { 993ec179322SAlan Cox if ((NBPOPMAP * i - next_free) * PAGE_SIZE >= 994ec179322SAlan Cox size) { 99544aab2c3SAlan Cox vm_reserv_reclaim(rv); 99644aab2c3SAlan Cox return (TRUE); 99744aab2c3SAlan Cox } 998ec179322SAlan Cox hi = ffsl(rv->popmap[i]); 999ec179322SAlan Cox } 1000ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 1001ec179322SAlan Cox if (i != NPOPMAP) 1002ec179322SAlan Cox hi--; 1003ec179322SAlan Cox if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >= 1004ec179322SAlan Cox size) { 1005ec179322SAlan Cox vm_reserv_reclaim(rv); 1006ec179322SAlan Cox return (TRUE); 1007ec179322SAlan Cox } 1008ec179322SAlan Cox } while (i < NPOPMAP); 100944aab2c3SAlan Cox } 101044aab2c3SAlan Cox return (FALSE); 101144aab2c3SAlan Cox } 101244aab2c3SAlan Cox 101344aab2c3SAlan Cox /* 1014f8a47341SAlan Cox * Transfers the reservation underlying the given page to a new object. 1015f8a47341SAlan Cox * 1016f8a47341SAlan Cox * The object must be locked. 1017f8a47341SAlan Cox */ 1018f8a47341SAlan Cox void 1019f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object, 1020f8a47341SAlan Cox vm_pindex_t old_object_offset) 1021f8a47341SAlan Cox { 1022f8a47341SAlan Cox vm_reserv_t rv; 1023f8a47341SAlan Cox 102489f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(new_object); 1025f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1026f8a47341SAlan Cox if (rv->object == old_object) { 1027f8a47341SAlan Cox mtx_lock(&vm_page_queue_free_mtx); 1028f8a47341SAlan Cox if (rv->object == old_object) { 1029f8a47341SAlan Cox LIST_REMOVE(rv, objq); 1030f8a47341SAlan Cox LIST_INSERT_HEAD(&new_object->rvq, rv, objq); 1031f8a47341SAlan Cox rv->object = new_object; 1032f8a47341SAlan Cox rv->pindex -= old_object_offset; 1033f8a47341SAlan Cox } 1034f8a47341SAlan Cox mtx_unlock(&vm_page_queue_free_mtx); 1035f8a47341SAlan Cox } 1036f8a47341SAlan Cox } 1037f8a47341SAlan Cox 1038f8a47341SAlan Cox /* 1039f8a47341SAlan Cox * Allocates the virtual and physical memory required by the reservation 1040f8a47341SAlan Cox * management system's data structures, in particular, the reservation array. 1041f8a47341SAlan Cox */ 1042f8a47341SAlan Cox vm_paddr_t 1043f8a47341SAlan Cox vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water) 1044f8a47341SAlan Cox { 1045f8a47341SAlan Cox vm_paddr_t new_end; 1046f8a47341SAlan Cox size_t size; 1047f8a47341SAlan Cox 1048f8a47341SAlan Cox /* 1049f8a47341SAlan Cox * Calculate the size (in bytes) of the reservation array. Round up 1050f8a47341SAlan Cox * from "high_water" because every small page is mapped to an element 1051f8a47341SAlan Cox * in the reservation array based on its physical address. Thus, the 1052f8a47341SAlan Cox * number of elements in the reservation array can be greater than the 1053f8a47341SAlan Cox * number of superpages. 1054f8a47341SAlan Cox */ 1055f8a47341SAlan Cox size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv); 1056f8a47341SAlan Cox 1057f8a47341SAlan Cox /* 1058f8a47341SAlan Cox * Allocate and map the physical memory for the reservation array. The 1059f8a47341SAlan Cox * next available virtual address is returned by reference. 1060f8a47341SAlan Cox */ 1061f8a47341SAlan Cox new_end = end - round_page(size); 1062f8a47341SAlan Cox vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end, 1063f8a47341SAlan Cox VM_PROT_READ | VM_PROT_WRITE); 1064f8a47341SAlan Cox bzero(vm_reserv_array, size); 1065f8a47341SAlan Cox 1066f8a47341SAlan Cox /* 1067f8a47341SAlan Cox * Return the next available physical address. 1068f8a47341SAlan Cox */ 1069f8a47341SAlan Cox return (new_end); 1070f8a47341SAlan Cox } 1071f8a47341SAlan Cox 1072f8a47341SAlan Cox #endif /* VM_NRESERVLEVEL > 0 */ 1073