1f8a47341SAlan Cox /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 4f8a47341SAlan Cox * Copyright (c) 2002-2006 Rice University 5ec179322SAlan Cox * Copyright (c) 2007-2011 Alan L. Cox <alc@cs.rice.edu> 6f8a47341SAlan Cox * All rights reserved. 7f8a47341SAlan Cox * 8f8a47341SAlan Cox * This software was developed for the FreeBSD Project by Alan L. Cox, 9f8a47341SAlan Cox * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro. 10f8a47341SAlan Cox * 11f8a47341SAlan Cox * Redistribution and use in source and binary forms, with or without 12f8a47341SAlan Cox * modification, are permitted provided that the following conditions 13f8a47341SAlan Cox * are met: 14f8a47341SAlan Cox * 1. Redistributions of source code must retain the above copyright 15f8a47341SAlan Cox * notice, this list of conditions and the following disclaimer. 16f8a47341SAlan Cox * 2. Redistributions in binary form must reproduce the above copyright 17f8a47341SAlan Cox * notice, this list of conditions and the following disclaimer in the 18f8a47341SAlan Cox * documentation and/or other materials provided with the distribution. 19f8a47341SAlan Cox * 20f8a47341SAlan Cox * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21f8a47341SAlan Cox * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22f8a47341SAlan Cox * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23f8a47341SAlan Cox * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24f8a47341SAlan Cox * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25f8a47341SAlan Cox * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26f8a47341SAlan Cox * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27f8a47341SAlan Cox * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28f8a47341SAlan Cox * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29f8a47341SAlan Cox * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 30f8a47341SAlan Cox * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31f8a47341SAlan Cox * POSSIBILITY OF SUCH DAMAGE. 32f8a47341SAlan Cox */ 33f8a47341SAlan Cox 34f8a47341SAlan Cox /* 35f8a47341SAlan Cox * Superpage reservation management module 36c68c3537SAlan Cox * 37c68c3537SAlan Cox * Any external functions defined by this module are only to be used by the 38c68c3537SAlan Cox * virtual memory system. 39f8a47341SAlan Cox */ 40f8a47341SAlan Cox 41f8a47341SAlan Cox #include <sys/cdefs.h> 42f8a47341SAlan Cox __FBSDID("$FreeBSD$"); 43f8a47341SAlan Cox 44f8a47341SAlan Cox #include "opt_vm.h" 45f8a47341SAlan Cox 46f8a47341SAlan Cox #include <sys/param.h> 47f8a47341SAlan Cox #include <sys/kernel.h> 48f8a47341SAlan Cox #include <sys/lock.h> 49f8a47341SAlan Cox #include <sys/malloc.h> 50f8a47341SAlan Cox #include <sys/mutex.h> 51f8a47341SAlan Cox #include <sys/queue.h> 5289f6b863SAttilio Rao #include <sys/rwlock.h> 53f8a47341SAlan Cox #include <sys/sbuf.h> 54f8a47341SAlan Cox #include <sys/sysctl.h> 55f8a47341SAlan Cox #include <sys/systm.h> 5672346b22SCy Schubert #include <sys/counter.h> 5772346b22SCy Schubert #include <sys/ktr.h> 589ed01c32SGleb Smirnoff #include <sys/vmmeter.h> 595c930c89SJeff Roberson #include <sys/smp.h> 60f8a47341SAlan Cox 61f8a47341SAlan Cox #include <vm/vm.h> 62f8a47341SAlan Cox #include <vm/vm_param.h> 63f8a47341SAlan Cox #include <vm/vm_object.h> 64f8a47341SAlan Cox #include <vm/vm_page.h> 65e2068d0bSJeff Roberson #include <vm/vm_pageout.h> 66f8a47341SAlan Cox #include <vm/vm_phys.h> 67e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 68774d251dSAttilio Rao #include <vm/vm_radix.h> 69f8a47341SAlan Cox #include <vm/vm_reserv.h> 70f8a47341SAlan Cox 71f8a47341SAlan Cox /* 72f8a47341SAlan Cox * The reservation system supports the speculative allocation of large physical 733453bca8SAlan Cox * pages ("superpages"). Speculative allocation enables the fully automatic 74f8a47341SAlan Cox * utilization of superpages by the virtual memory system. In other words, no 75f8a47341SAlan Cox * programmatic directives are required to use superpages. 76f8a47341SAlan Cox */ 77f8a47341SAlan Cox 78f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0 79f8a47341SAlan Cox 80f8a47341SAlan Cox /* 81f8a47341SAlan Cox * The number of small pages that are contained in a level 0 reservation 82f8a47341SAlan Cox */ 83f8a47341SAlan Cox #define VM_LEVEL_0_NPAGES (1 << VM_LEVEL_0_ORDER) 84f8a47341SAlan Cox 85f8a47341SAlan Cox /* 86f8a47341SAlan Cox * The number of bits by which a physical address is shifted to obtain the 87f8a47341SAlan Cox * reservation number 88f8a47341SAlan Cox */ 89f8a47341SAlan Cox #define VM_LEVEL_0_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT) 90f8a47341SAlan Cox 91f8a47341SAlan Cox /* 92f8a47341SAlan Cox * The size of a level 0 reservation in bytes 93f8a47341SAlan Cox */ 94f8a47341SAlan Cox #define VM_LEVEL_0_SIZE (1 << VM_LEVEL_0_SHIFT) 95f8a47341SAlan Cox 96f8a47341SAlan Cox /* 97f8a47341SAlan Cox * Computes the index of the small page underlying the given (object, pindex) 98f8a47341SAlan Cox * within the reservation's array of small pages. 99f8a47341SAlan Cox */ 100f8a47341SAlan Cox #define VM_RESERV_INDEX(object, pindex) \ 101f8a47341SAlan Cox (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1)) 102f8a47341SAlan Cox 103f8a47341SAlan Cox /* 104ec179322SAlan Cox * The size of a population map entry 105ec179322SAlan Cox */ 106ec179322SAlan Cox typedef u_long popmap_t; 107ec179322SAlan Cox 108ec179322SAlan Cox /* 109ec179322SAlan Cox * The number of bits in a population map entry 110ec179322SAlan Cox */ 111ec179322SAlan Cox #define NBPOPMAP (NBBY * sizeof(popmap_t)) 112ec179322SAlan Cox 113ec179322SAlan Cox /* 114ec179322SAlan Cox * The number of population map entries in a reservation 115ec179322SAlan Cox */ 116ec179322SAlan Cox #define NPOPMAP howmany(VM_LEVEL_0_NPAGES, NBPOPMAP) 117ec179322SAlan Cox 118ec179322SAlan Cox /* 119*2ef6727eSJeff Roberson * Number of elapsed ticks before we update the LRU queue position. Used 120*2ef6727eSJeff Roberson * to reduce contention and churn on the list. 121*2ef6727eSJeff Roberson */ 122*2ef6727eSJeff Roberson #define PARTPOPSLOP 1 123*2ef6727eSJeff Roberson 124*2ef6727eSJeff Roberson /* 1253180f757SAlan Cox * Clear a bit in the population map. 1263180f757SAlan Cox */ 1273180f757SAlan Cox static __inline void 1283180f757SAlan Cox popmap_clear(popmap_t popmap[], int i) 1293180f757SAlan Cox { 1303180f757SAlan Cox 1313180f757SAlan Cox popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP)); 1323180f757SAlan Cox } 1333180f757SAlan Cox 1343180f757SAlan Cox /* 1353180f757SAlan Cox * Set a bit in the population map. 1363180f757SAlan Cox */ 1373180f757SAlan Cox static __inline void 1383180f757SAlan Cox popmap_set(popmap_t popmap[], int i) 1393180f757SAlan Cox { 1403180f757SAlan Cox 1413180f757SAlan Cox popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP); 1423180f757SAlan Cox } 1433180f757SAlan Cox 1443180f757SAlan Cox /* 1453180f757SAlan Cox * Is a bit in the population map clear? 1463180f757SAlan Cox */ 1473180f757SAlan Cox static __inline boolean_t 1483180f757SAlan Cox popmap_is_clear(popmap_t popmap[], int i) 1493180f757SAlan Cox { 1503180f757SAlan Cox 1513180f757SAlan Cox return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0); 1523180f757SAlan Cox } 1533180f757SAlan Cox 1543180f757SAlan Cox /* 1553180f757SAlan Cox * Is a bit in the population map set? 1563180f757SAlan Cox */ 1573180f757SAlan Cox static __inline boolean_t 1583180f757SAlan Cox popmap_is_set(popmap_t popmap[], int i) 1593180f757SAlan Cox { 1603180f757SAlan Cox 1613180f757SAlan Cox return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0); 1623180f757SAlan Cox } 1633180f757SAlan Cox 1643180f757SAlan Cox /* 165f8a47341SAlan Cox * The reservation structure 166f8a47341SAlan Cox * 167f8a47341SAlan Cox * A reservation structure is constructed whenever a large physical page is 168f8a47341SAlan Cox * speculatively allocated to an object. The reservation provides the small 169f8a47341SAlan Cox * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets 170f8a47341SAlan Cox * within that object. The reservation's "popcnt" tracks the number of these 171f8a47341SAlan Cox * small physical pages that are in use at any given time. When and if the 1723453bca8SAlan Cox * reservation is not fully utilized, it appears in the queue of partially 173f8a47341SAlan Cox * populated reservations. The reservation always appears on the containing 174f8a47341SAlan Cox * object's list of reservations. 175f8a47341SAlan Cox * 1763453bca8SAlan Cox * A partially populated reservation can be broken and reclaimed at any time. 177e2068d0bSJeff Roberson * 1785c930c89SJeff Roberson * r - vm_reserv_lock 1795c930c89SJeff Roberson * d - vm_reserv_domain_lock 180e2068d0bSJeff Roberson * o - vm_reserv_object_lock 181e2068d0bSJeff Roberson * c - constant after boot 182f8a47341SAlan Cox */ 183f8a47341SAlan Cox struct vm_reserv { 1845c930c89SJeff Roberson struct mtx lock; /* reservation lock. */ 1855c930c89SJeff Roberson TAILQ_ENTRY(vm_reserv) partpopq; /* (d) per-domain queue. */ 1865c930c89SJeff Roberson LIST_ENTRY(vm_reserv) objq; /* (o, r) object queue */ 1875c930c89SJeff Roberson vm_object_t object; /* (o, r) containing object */ 1885c930c89SJeff Roberson vm_pindex_t pindex; /* (o, r) offset in object */ 189e2068d0bSJeff Roberson vm_page_t pages; /* (c) first page */ 1905c930c89SJeff Roberson uint16_t domain; /* (c) NUMA domain. */ 1915c930c89SJeff Roberson uint16_t popcnt; /* (r) # of pages in use */ 192*2ef6727eSJeff Roberson int lasttick; /* (r) last pop update tick. */ 1935c930c89SJeff Roberson char inpartpopq; /* (d) */ 1945c930c89SJeff Roberson popmap_t popmap[NPOPMAP]; /* (r) bit vector, used pages */ 195f8a47341SAlan Cox }; 196f8a47341SAlan Cox 1975c930c89SJeff Roberson #define vm_reserv_lockptr(rv) (&(rv)->lock) 1985c930c89SJeff Roberson #define vm_reserv_assert_locked(rv) \ 1995c930c89SJeff Roberson mtx_assert(vm_reserv_lockptr(rv), MA_OWNED) 2005c930c89SJeff Roberson #define vm_reserv_lock(rv) mtx_lock(vm_reserv_lockptr(rv)) 2015c930c89SJeff Roberson #define vm_reserv_trylock(rv) mtx_trylock(vm_reserv_lockptr(rv)) 2025c930c89SJeff Roberson #define vm_reserv_unlock(rv) mtx_unlock(vm_reserv_lockptr(rv)) 2035c930c89SJeff Roberson 2045c930c89SJeff Roberson static struct mtx_padalign vm_reserv_domain_locks[MAXMEMDOM]; 2055c930c89SJeff Roberson 2065c930c89SJeff Roberson #define vm_reserv_domain_lockptr(d) &vm_reserv_domain_locks[(d)] 2075c930c89SJeff Roberson #define vm_reserv_domain_lock(d) mtx_lock(vm_reserv_domain_lockptr(d)) 2085c930c89SJeff Roberson #define vm_reserv_domain_unlock(d) mtx_unlock(vm_reserv_domain_lockptr(d)) 2095c930c89SJeff Roberson 210f8a47341SAlan Cox /* 211f8a47341SAlan Cox * The reservation array 212f8a47341SAlan Cox * 213f8a47341SAlan Cox * This array is analoguous in function to vm_page_array. It differs in the 214f8a47341SAlan Cox * respect that it may contain a greater number of useful reservation 215f8a47341SAlan Cox * structures than there are (physical) superpages. These "invalid" 216f8a47341SAlan Cox * reservation structures exist to trade-off space for time in the 217f8a47341SAlan Cox * implementation of vm_reserv_from_page(). Invalid reservation structures are 218f8a47341SAlan Cox * distinguishable from "valid" reservation structures by inspecting the 219f8a47341SAlan Cox * reservation's "pages" field. Invalid reservation structures have a NULL 220f8a47341SAlan Cox * "pages" field. 221f8a47341SAlan Cox * 222f8a47341SAlan Cox * vm_reserv_from_page() maps a small (physical) page to an element of this 223f8a47341SAlan Cox * array by computing a physical reservation number from the page's physical 224f8a47341SAlan Cox * address. The physical reservation number is used as the array index. 225f8a47341SAlan Cox * 226f8a47341SAlan Cox * An "active" reservation is a valid reservation structure that has a non-NULL 227f8a47341SAlan Cox * "object" field and a non-zero "popcnt" field. In other words, every active 228f8a47341SAlan Cox * reservation belongs to a particular object. Moreover, every active 229f8a47341SAlan Cox * reservation has an entry in the containing object's list of reservations. 230f8a47341SAlan Cox */ 231f8a47341SAlan Cox static vm_reserv_t vm_reserv_array; 232f8a47341SAlan Cox 233f8a47341SAlan Cox /* 2343453bca8SAlan Cox * The partially populated reservation queue 235f8a47341SAlan Cox * 2363453bca8SAlan Cox * This queue enables the fast recovery of an unused free small page from a 2373453bca8SAlan Cox * partially populated reservation. The reservation at the head of this queue 2383453bca8SAlan Cox * is the least recently changed, partially populated reservation. 239f8a47341SAlan Cox * 240f8a47341SAlan Cox * Access to this queue is synchronized by the free page queue lock. 241f8a47341SAlan Cox */ 242ef435ae7SJeff Roberson static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop[MAXMEMDOM]; 243f8a47341SAlan Cox 244f8a47341SAlan Cox static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info"); 245f8a47341SAlan Cox 2465c930c89SJeff Roberson static counter_u64_t vm_reserv_broken = EARLY_COUNTER; 2475c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD, 2485c930c89SJeff Roberson &vm_reserv_broken, "Cumulative number of broken reservations"); 249f8a47341SAlan Cox 2505c930c89SJeff Roberson static counter_u64_t vm_reserv_freed = EARLY_COUNTER; 2515c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD, 2525c930c89SJeff Roberson &vm_reserv_freed, "Cumulative number of freed reservations"); 253f8a47341SAlan Cox 254e0a63baaSAlan Cox static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS); 255e0a63baaSAlan Cox 256e0a63baaSAlan Cox SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_RD, NULL, 0, 257e0a63baaSAlan Cox sysctl_vm_reserv_fullpop, "I", "Current number of full reservations"); 258e0a63baaSAlan Cox 259f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS); 260f8a47341SAlan Cox 261f8a47341SAlan Cox SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, 2623453bca8SAlan Cox sysctl_vm_reserv_partpopq, "A", "Partially populated reservation queues"); 263f8a47341SAlan Cox 2645c930c89SJeff Roberson static counter_u64_t vm_reserv_reclaimed = EARLY_COUNTER; 2655c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD, 2665c930c89SJeff Roberson &vm_reserv_reclaimed, "Cumulative number of reclaimed reservations"); 267f8a47341SAlan Cox 268e2068d0bSJeff Roberson /* 269e2068d0bSJeff Roberson * The object lock pool is used to synchronize the rvq. We can not use a 270e2068d0bSJeff Roberson * pool mutex because it is required before malloc works. 271e2068d0bSJeff Roberson * 272e2068d0bSJeff Roberson * The "hash" function could be made faster without divide and modulo. 273e2068d0bSJeff Roberson */ 274e2068d0bSJeff Roberson #define VM_RESERV_OBJ_LOCK_COUNT MAXCPU 275e2068d0bSJeff Roberson 276e2068d0bSJeff Roberson struct mtx_padalign vm_reserv_object_mtx[VM_RESERV_OBJ_LOCK_COUNT]; 277e2068d0bSJeff Roberson 278e2068d0bSJeff Roberson #define vm_reserv_object_lock_idx(object) \ 279e2068d0bSJeff Roberson (((uintptr_t)object / sizeof(*object)) % VM_RESERV_OBJ_LOCK_COUNT) 280e2068d0bSJeff Roberson #define vm_reserv_object_lock_ptr(object) \ 281e2068d0bSJeff Roberson &vm_reserv_object_mtx[vm_reserv_object_lock_idx((object))] 282e2068d0bSJeff Roberson #define vm_reserv_object_lock(object) \ 283e2068d0bSJeff Roberson mtx_lock(vm_reserv_object_lock_ptr((object))) 284e2068d0bSJeff Roberson #define vm_reserv_object_unlock(object) \ 285e2068d0bSJeff Roberson mtx_unlock(vm_reserv_object_lock_ptr((object))) 286e2068d0bSJeff Roberson 287ada27a3bSKonstantin Belousov static void vm_reserv_break(vm_reserv_t rv); 288ec179322SAlan Cox static void vm_reserv_depopulate(vm_reserv_t rv, int index); 289f8a47341SAlan Cox static vm_reserv_t vm_reserv_from_page(vm_page_t m); 290f8a47341SAlan Cox static boolean_t vm_reserv_has_pindex(vm_reserv_t rv, 291f8a47341SAlan Cox vm_pindex_t pindex); 292ec179322SAlan Cox static void vm_reserv_populate(vm_reserv_t rv, int index); 29344aab2c3SAlan Cox static void vm_reserv_reclaim(vm_reserv_t rv); 294f8a47341SAlan Cox 295f8a47341SAlan Cox /* 296e0a63baaSAlan Cox * Returns the current number of full reservations. 297e0a63baaSAlan Cox * 298e0a63baaSAlan Cox * Since the number of full reservations is computed without acquiring the 299e0a63baaSAlan Cox * free page queue lock, the returned value may be inexact. 300e0a63baaSAlan Cox */ 301e0a63baaSAlan Cox static int 302e0a63baaSAlan Cox sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS) 303e0a63baaSAlan Cox { 304e0a63baaSAlan Cox vm_paddr_t paddr; 305e0a63baaSAlan Cox struct vm_phys_seg *seg; 306e0a63baaSAlan Cox vm_reserv_t rv; 307e0a63baaSAlan Cox int fullpop, segind; 308e0a63baaSAlan Cox 309e0a63baaSAlan Cox fullpop = 0; 310e0a63baaSAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 311e0a63baaSAlan Cox seg = &vm_phys_segs[segind]; 312e0a63baaSAlan Cox paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 313e0a63baaSAlan Cox while (paddr + VM_LEVEL_0_SIZE <= seg->end) { 314e0a63baaSAlan Cox rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT]; 315e0a63baaSAlan Cox fullpop += rv->popcnt == VM_LEVEL_0_NPAGES; 316e0a63baaSAlan Cox paddr += VM_LEVEL_0_SIZE; 317e0a63baaSAlan Cox } 318e0a63baaSAlan Cox } 319e0a63baaSAlan Cox return (sysctl_handle_int(oidp, &fullpop, 0, req)); 320e0a63baaSAlan Cox } 321e0a63baaSAlan Cox 322e0a63baaSAlan Cox /* 3233453bca8SAlan Cox * Describes the current state of the partially populated reservation queue. 324f8a47341SAlan Cox */ 325f8a47341SAlan Cox static int 326f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS) 327f8a47341SAlan Cox { 328f8a47341SAlan Cox struct sbuf sbuf; 329f8a47341SAlan Cox vm_reserv_t rv; 330ef435ae7SJeff Roberson int counter, error, domain, level, unused_pages; 331f8a47341SAlan Cox 33200f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 33300f0e671SMatthew D Fleming if (error != 0) 33400f0e671SMatthew D Fleming return (error); 3354e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 336ef435ae7SJeff Roberson sbuf_printf(&sbuf, "\nDOMAIN LEVEL SIZE NUMBER\n\n"); 337ef435ae7SJeff Roberson for (domain = 0; domain < vm_ndomains; domain++) { 338f8a47341SAlan Cox for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) { 339f8a47341SAlan Cox counter = 0; 340f8a47341SAlan Cox unused_pages = 0; 3415c930c89SJeff Roberson vm_reserv_domain_lock(domain); 342ef435ae7SJeff Roberson TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) { 343f8a47341SAlan Cox counter++; 344f8a47341SAlan Cox unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt; 345f8a47341SAlan Cox } 3465c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 347ef435ae7SJeff Roberson sbuf_printf(&sbuf, "%6d, %7d, %6dK, %6d\n", 348ef435ae7SJeff Roberson domain, level, 3492cf36c8fSAlan Cox unused_pages * ((int)PAGE_SIZE / 1024), counter); 350f8a47341SAlan Cox } 351ef435ae7SJeff Roberson } 3524e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 353f8a47341SAlan Cox sbuf_delete(&sbuf); 354f8a47341SAlan Cox return (error); 355f8a47341SAlan Cox } 356f8a47341SAlan Cox 357f8a47341SAlan Cox /* 358e2068d0bSJeff Roberson * Remove a reservation from the object's objq. 359e2068d0bSJeff Roberson */ 360e2068d0bSJeff Roberson static void 361e2068d0bSJeff Roberson vm_reserv_remove(vm_reserv_t rv) 362e2068d0bSJeff Roberson { 363e2068d0bSJeff Roberson vm_object_t object; 364e2068d0bSJeff Roberson 3655c930c89SJeff Roberson vm_reserv_assert_locked(rv); 3665c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 3675c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 368e2068d0bSJeff Roberson KASSERT(rv->object != NULL, 369e2068d0bSJeff Roberson ("vm_reserv_remove: reserv %p is free", rv)); 370e2068d0bSJeff Roberson KASSERT(!rv->inpartpopq, 371e2068d0bSJeff Roberson ("vm_reserv_remove: reserv %p's inpartpopq is TRUE", rv)); 372e2068d0bSJeff Roberson object = rv->object; 373e2068d0bSJeff Roberson vm_reserv_object_lock(object); 374e2068d0bSJeff Roberson LIST_REMOVE(rv, objq); 375e2068d0bSJeff Roberson rv->object = NULL; 376e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 377e2068d0bSJeff Roberson } 378e2068d0bSJeff Roberson 379e2068d0bSJeff Roberson /* 380e2068d0bSJeff Roberson * Insert a new reservation into the object's objq. 381e2068d0bSJeff Roberson */ 382e2068d0bSJeff Roberson static void 383e2068d0bSJeff Roberson vm_reserv_insert(vm_reserv_t rv, vm_object_t object, vm_pindex_t pindex) 384e2068d0bSJeff Roberson { 385e2068d0bSJeff Roberson int i; 386e2068d0bSJeff Roberson 3875c930c89SJeff Roberson vm_reserv_assert_locked(rv); 3885c930c89SJeff Roberson CTR6(KTR_VM, 3895c930c89SJeff Roberson "%s: rv %p(%p) object %p new %p popcnt %d", 3905c930c89SJeff Roberson __FUNCTION__, rv, rv->pages, rv->object, object, 3915c930c89SJeff Roberson rv->popcnt); 392e2068d0bSJeff Roberson KASSERT(rv->object == NULL, 393e2068d0bSJeff Roberson ("vm_reserv_insert: reserv %p isn't free", rv)); 394e2068d0bSJeff Roberson KASSERT(rv->popcnt == 0, 395e2068d0bSJeff Roberson ("vm_reserv_insert: reserv %p's popcnt is corrupted", rv)); 396e2068d0bSJeff Roberson KASSERT(!rv->inpartpopq, 397e2068d0bSJeff Roberson ("vm_reserv_insert: reserv %p's inpartpopq is TRUE", rv)); 398e2068d0bSJeff Roberson for (i = 0; i < NPOPMAP; i++) 399e2068d0bSJeff Roberson KASSERT(rv->popmap[i] == 0, 400e2068d0bSJeff Roberson ("vm_reserv_insert: reserv %p's popmap is corrupted", rv)); 401e2068d0bSJeff Roberson vm_reserv_object_lock(object); 402e2068d0bSJeff Roberson rv->pindex = pindex; 403e2068d0bSJeff Roberson rv->object = object; 404*2ef6727eSJeff Roberson rv->lasttick = ticks; 405e2068d0bSJeff Roberson LIST_INSERT_HEAD(&object->rvq, rv, objq); 406e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 407e2068d0bSJeff Roberson } 408e2068d0bSJeff Roberson 409e2068d0bSJeff Roberson /* 410f8a47341SAlan Cox * Reduces the given reservation's population count. If the population count 411f8a47341SAlan Cox * becomes zero, the reservation is destroyed. Additionally, moves the 4123453bca8SAlan Cox * reservation to the tail of the partially populated reservation queue if the 413f8a47341SAlan Cox * population count is non-zero. 414f8a47341SAlan Cox */ 415f8a47341SAlan Cox static void 416ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index) 417f8a47341SAlan Cox { 4185c930c89SJeff Roberson struct vm_domain *vmd; 419f8a47341SAlan Cox 4205c930c89SJeff Roberson vm_reserv_assert_locked(rv); 4215c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 4225c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 423f8a47341SAlan Cox KASSERT(rv->object != NULL, 424f8a47341SAlan Cox ("vm_reserv_depopulate: reserv %p is free", rv)); 4253180f757SAlan Cox KASSERT(popmap_is_set(rv->popmap, index), 426a08c1515SAlan Cox ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv, 427a08c1515SAlan Cox index)); 428f8a47341SAlan Cox KASSERT(rv->popcnt > 0, 429f8a47341SAlan Cox ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv)); 4302d3f4181SJeff Roberson KASSERT(rv->domain < vm_ndomains, 431ef435ae7SJeff Roberson ("vm_reserv_depopulate: reserv %p's domain is corrupted %d", 432ef435ae7SJeff Roberson rv, rv->domain)); 4335c930c89SJeff Roberson if (rv->popcnt == VM_LEVEL_0_NPAGES) { 434dd05fa19SAlan Cox KASSERT(rv->pages->psind == 1, 435dd05fa19SAlan Cox ("vm_reserv_depopulate: reserv %p is already demoted", 436dd05fa19SAlan Cox rv)); 437dd05fa19SAlan Cox rv->pages->psind = 0; 438f8a47341SAlan Cox } 4393180f757SAlan Cox popmap_clear(rv->popmap, index); 440f8a47341SAlan Cox rv->popcnt--; 441*2ef6727eSJeff Roberson if ((unsigned)(ticks - rv->lasttick) >= PARTPOPSLOP || 442*2ef6727eSJeff Roberson rv->popcnt == 0) { 4435c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 4445c930c89SJeff Roberson if (rv->inpartpopq) { 4455c930c89SJeff Roberson TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq); 4465c930c89SJeff Roberson rv->inpartpopq = FALSE; 4475c930c89SJeff Roberson } 4485c930c89SJeff Roberson if (rv->popcnt != 0) { 449f8a47341SAlan Cox rv->inpartpopq = TRUE; 450ef435ae7SJeff Roberson TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq); 451f8a47341SAlan Cox } 4525c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 453*2ef6727eSJeff Roberson rv->lasttick = ticks; 454*2ef6727eSJeff Roberson } 4555c930c89SJeff Roberson vmd = VM_DOMAIN(rv->domain); 4565c930c89SJeff Roberson if (rv->popcnt == 0) { 4575c930c89SJeff Roberson vm_reserv_remove(rv); 4585c930c89SJeff Roberson vm_domain_free_lock(vmd); 4595c930c89SJeff Roberson vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER); 4605c930c89SJeff Roberson vm_domain_free_unlock(vmd); 4615c930c89SJeff Roberson counter_u64_add(vm_reserv_freed, 1); 4625c930c89SJeff Roberson } 4635c930c89SJeff Roberson vm_domain_freecnt_inc(vmd, 1); 464f8a47341SAlan Cox } 465f8a47341SAlan Cox 466f8a47341SAlan Cox /* 467f8a47341SAlan Cox * Returns the reservation to which the given page might belong. 468f8a47341SAlan Cox */ 469f8a47341SAlan Cox static __inline vm_reserv_t 470f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m) 471f8a47341SAlan Cox { 472f8a47341SAlan Cox 473f8a47341SAlan Cox return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]); 474f8a47341SAlan Cox } 475f8a47341SAlan Cox 476f8a47341SAlan Cox /* 477e2068d0bSJeff Roberson * Returns an existing reservation or NULL and initialized successor pointer. 478e2068d0bSJeff Roberson */ 479e2068d0bSJeff Roberson static vm_reserv_t 480e2068d0bSJeff Roberson vm_reserv_from_object(vm_object_t object, vm_pindex_t pindex, 481e2068d0bSJeff Roberson vm_page_t mpred, vm_page_t *msuccp) 482e2068d0bSJeff Roberson { 483e2068d0bSJeff Roberson vm_reserv_t rv; 484e2068d0bSJeff Roberson vm_page_t msucc; 485e2068d0bSJeff Roberson 486e2068d0bSJeff Roberson msucc = NULL; 487e2068d0bSJeff Roberson if (mpred != NULL) { 488e2068d0bSJeff Roberson KASSERT(mpred->object == object, 489e2068d0bSJeff Roberson ("vm_reserv_from_object: object doesn't contain mpred")); 490e2068d0bSJeff Roberson KASSERT(mpred->pindex < pindex, 491e2068d0bSJeff Roberson ("vm_reserv_from_object: mpred doesn't precede pindex")); 492e2068d0bSJeff Roberson rv = vm_reserv_from_page(mpred); 493e2068d0bSJeff Roberson if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 494e2068d0bSJeff Roberson goto found; 495e2068d0bSJeff Roberson msucc = TAILQ_NEXT(mpred, listq); 496e2068d0bSJeff Roberson } else 497e2068d0bSJeff Roberson msucc = TAILQ_FIRST(&object->memq); 498e2068d0bSJeff Roberson if (msucc != NULL) { 499e2068d0bSJeff Roberson KASSERT(msucc->pindex > pindex, 500e2068d0bSJeff Roberson ("vm_reserv_from_object: msucc doesn't succeed pindex")); 501e2068d0bSJeff Roberson rv = vm_reserv_from_page(msucc); 502e2068d0bSJeff Roberson if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 503e2068d0bSJeff Roberson goto found; 504e2068d0bSJeff Roberson } 505e2068d0bSJeff Roberson rv = NULL; 506e2068d0bSJeff Roberson 507e2068d0bSJeff Roberson found: 508e2068d0bSJeff Roberson *msuccp = msucc; 509e2068d0bSJeff Roberson 510e2068d0bSJeff Roberson return (rv); 511e2068d0bSJeff Roberson } 512e2068d0bSJeff Roberson 513e2068d0bSJeff Roberson /* 514f8a47341SAlan Cox * Returns TRUE if the given reservation contains the given page index and 515f8a47341SAlan Cox * FALSE otherwise. 516f8a47341SAlan Cox */ 517f8a47341SAlan Cox static __inline boolean_t 518f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex) 519f8a47341SAlan Cox { 520f8a47341SAlan Cox 521f8a47341SAlan Cox return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0); 522f8a47341SAlan Cox } 523f8a47341SAlan Cox 524f8a47341SAlan Cox /* 525f8a47341SAlan Cox * Increases the given reservation's population count. Moves the reservation 5263453bca8SAlan Cox * to the tail of the partially populated reservation queue. 527f8a47341SAlan Cox * 528f8a47341SAlan Cox * The free page queue must be locked. 529f8a47341SAlan Cox */ 530f8a47341SAlan Cox static void 531ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index) 532f8a47341SAlan Cox { 533f8a47341SAlan Cox 5345c930c89SJeff Roberson vm_reserv_assert_locked(rv); 5355c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 5365c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 537f8a47341SAlan Cox KASSERT(rv->object != NULL, 538f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is free", rv)); 5393180f757SAlan Cox KASSERT(popmap_is_clear(rv->popmap, index), 540a08c1515SAlan Cox ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv, 541a08c1515SAlan Cox index)); 542f8a47341SAlan Cox KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES, 543f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is already full", rv)); 544dd05fa19SAlan Cox KASSERT(rv->pages->psind == 0, 545dd05fa19SAlan Cox ("vm_reserv_populate: reserv %p is already promoted", rv)); 5462d3f4181SJeff Roberson KASSERT(rv->domain < vm_ndomains, 547ef435ae7SJeff Roberson ("vm_reserv_populate: reserv %p's domain is corrupted %d", 548ef435ae7SJeff Roberson rv, rv->domain)); 5495c930c89SJeff Roberson popmap_set(rv->popmap, index); 5505c930c89SJeff Roberson rv->popcnt++; 551*2ef6727eSJeff Roberson if ((unsigned)(ticks - rv->lasttick) < PARTPOPSLOP && 552*2ef6727eSJeff Roberson rv->inpartpopq && rv->popcnt != VM_LEVEL_0_NPAGES) 553*2ef6727eSJeff Roberson return; 554*2ef6727eSJeff Roberson rv->lasttick = ticks; 5555c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 556f8a47341SAlan Cox if (rv->inpartpopq) { 557ef435ae7SJeff Roberson TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq); 558f8a47341SAlan Cox rv->inpartpopq = FALSE; 559f8a47341SAlan Cox } 560f8a47341SAlan Cox if (rv->popcnt < VM_LEVEL_0_NPAGES) { 561f8a47341SAlan Cox rv->inpartpopq = TRUE; 562ef435ae7SJeff Roberson TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq); 5635c930c89SJeff Roberson } else { 5645c930c89SJeff Roberson KASSERT(rv->pages->psind == 0, 5655c930c89SJeff Roberson ("vm_reserv_populate: reserv %p is already promoted", 5665c930c89SJeff Roberson rv)); 567dd05fa19SAlan Cox rv->pages->psind = 1; 568f8a47341SAlan Cox } 5695c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 5705c930c89SJeff Roberson } 571f8a47341SAlan Cox 572f8a47341SAlan Cox /* 5735f70fb14SMark Johnston * Attempts to allocate a contiguous set of physical pages from existing 5745f70fb14SMark Johnston * reservations. See vm_reserv_alloc_contig() for a description of the 5755f70fb14SMark Johnston * function's parameters. 576c68c3537SAlan Cox * 577920da7e4SAlan Cox * The page "mpred" must immediately precede the offset "pindex" within the 578920da7e4SAlan Cox * specified object. 579920da7e4SAlan Cox * 5805f70fb14SMark Johnston * The object must be locked. 581c68c3537SAlan Cox */ 582c68c3537SAlan Cox vm_page_t 583e2068d0bSJeff Roberson vm_reserv_extend_contig(int req, vm_object_t object, vm_pindex_t pindex, 584e2068d0bSJeff Roberson int domain, u_long npages, vm_paddr_t low, vm_paddr_t high, 585e2068d0bSJeff Roberson u_long alignment, vm_paddr_t boundary, vm_page_t mpred) 586e2068d0bSJeff Roberson { 587e2068d0bSJeff Roberson struct vm_domain *vmd; 588e2068d0bSJeff Roberson vm_paddr_t pa, size; 589e2068d0bSJeff Roberson vm_page_t m, msucc; 590e2068d0bSJeff Roberson vm_reserv_t rv; 591e2068d0bSJeff Roberson int i, index; 592e2068d0bSJeff Roberson 593e2068d0bSJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 594e2068d0bSJeff Roberson KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0")); 595e2068d0bSJeff Roberson 596e2068d0bSJeff Roberson /* 597e2068d0bSJeff Roberson * Is a reservation fundamentally impossible? 598e2068d0bSJeff Roberson */ 599e2068d0bSJeff Roberson if (pindex < VM_RESERV_INDEX(object, pindex) || 600e2068d0bSJeff Roberson pindex + npages > object->size || object->resident_page_count == 0) 601e2068d0bSJeff Roberson return (NULL); 602e2068d0bSJeff Roberson 603e2068d0bSJeff Roberson /* 604e2068d0bSJeff Roberson * All reservations of a particular size have the same alignment. 605e2068d0bSJeff Roberson * Assuming that the first page is allocated from a reservation, the 606e2068d0bSJeff Roberson * least significant bits of its physical address can be determined 607e2068d0bSJeff Roberson * from its offset from the beginning of the reservation and the size 608e2068d0bSJeff Roberson * of the reservation. 609e2068d0bSJeff Roberson * 610e2068d0bSJeff Roberson * Could the specified index within a reservation of the smallest 611e2068d0bSJeff Roberson * possible size satisfy the alignment and boundary requirements? 612e2068d0bSJeff Roberson */ 613e2068d0bSJeff Roberson pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT; 614e2068d0bSJeff Roberson if ((pa & (alignment - 1)) != 0) 615e2068d0bSJeff Roberson return (NULL); 616e2068d0bSJeff Roberson size = npages << PAGE_SHIFT; 617e2068d0bSJeff Roberson if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 618e2068d0bSJeff Roberson return (NULL); 619e2068d0bSJeff Roberson 620e2068d0bSJeff Roberson /* 621e2068d0bSJeff Roberson * Look for an existing reservation. 622e2068d0bSJeff Roberson */ 623e2068d0bSJeff Roberson rv = vm_reserv_from_object(object, pindex, mpred, &msucc); 624e2068d0bSJeff Roberson if (rv == NULL) 625e2068d0bSJeff Roberson return (NULL); 626e2068d0bSJeff Roberson KASSERT(object != kernel_object || rv->domain == domain, 627e2068d0bSJeff Roberson ("vm_reserv_extend_contig: Domain mismatch from reservation.")); 628e2068d0bSJeff Roberson index = VM_RESERV_INDEX(object, pindex); 629e2068d0bSJeff Roberson /* Does the allocation fit within the reservation? */ 630e2068d0bSJeff Roberson if (index + npages > VM_LEVEL_0_NPAGES) 631e2068d0bSJeff Roberson return (NULL); 632e2068d0bSJeff Roberson domain = rv->domain; 633e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 6345c930c89SJeff Roberson vm_reserv_lock(rv); 6355c930c89SJeff Roberson if (rv->object != object) 636e2068d0bSJeff Roberson goto out; 637e2068d0bSJeff Roberson m = &rv->pages[index]; 638e2068d0bSJeff Roberson pa = VM_PAGE_TO_PHYS(m); 639e2068d0bSJeff Roberson if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 || 6405c930c89SJeff Roberson ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 641e2068d0bSJeff Roberson goto out; 642e2068d0bSJeff Roberson /* Handle vm_page_rename(m, new_object, ...). */ 643e2068d0bSJeff Roberson for (i = 0; i < npages; i++) { 6445c930c89SJeff Roberson if (popmap_is_set(rv->popmap, index + i)) 645e2068d0bSJeff Roberson goto out; 646e2068d0bSJeff Roberson } 6475c930c89SJeff Roberson if (!vm_domain_allocate(vmd, req, npages)) 6485c930c89SJeff Roberson goto out; 649e2068d0bSJeff Roberson for (i = 0; i < npages; i++) 650e2068d0bSJeff Roberson vm_reserv_populate(rv, index + i); 6515c930c89SJeff Roberson vm_reserv_unlock(rv); 652e2068d0bSJeff Roberson return (m); 6535c930c89SJeff Roberson 6545c930c89SJeff Roberson out: 6555c930c89SJeff Roberson vm_reserv_unlock(rv); 6565c930c89SJeff Roberson return (NULL); 657e2068d0bSJeff Roberson } 658e2068d0bSJeff Roberson 659e2068d0bSJeff Roberson /* 660e2068d0bSJeff Roberson * Allocates a contiguous set of physical pages of the given size "npages" 6615f70fb14SMark Johnston * from newly created reservations. All of the physical pages 662e2068d0bSJeff Roberson * must be at or above the given physical address "low" and below the given 663e2068d0bSJeff Roberson * physical address "high". The given value "alignment" determines the 664e2068d0bSJeff Roberson * alignment of the first physical page in the set. If the given value 665e2068d0bSJeff Roberson * "boundary" is non-zero, then the set of physical pages cannot cross any 666e2068d0bSJeff Roberson * physical address boundary that is a multiple of that value. Both 667e2068d0bSJeff Roberson * "alignment" and "boundary" must be a power of two. 668e2068d0bSJeff Roberson * 6695f70fb14SMark Johnston * Callers should first invoke vm_reserv_extend_contig() to attempt an 6705f70fb14SMark Johnston * allocation from existing reservations. 6715f70fb14SMark Johnston * 672e2068d0bSJeff Roberson * The page "mpred" must immediately precede the offset "pindex" within the 673e2068d0bSJeff Roberson * specified object. 674e2068d0bSJeff Roberson * 675e2068d0bSJeff Roberson * The object and free page queue must be locked. 676e2068d0bSJeff Roberson */ 677e2068d0bSJeff Roberson vm_page_t 6785c930c89SJeff Roberson vm_reserv_alloc_contig(int req, vm_object_t object, vm_pindex_t pindex, int domain, 679ef435ae7SJeff Roberson u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 680ef435ae7SJeff Roberson vm_paddr_t boundary, vm_page_t mpred) 681c68c3537SAlan Cox { 6825c930c89SJeff Roberson struct vm_domain *vmd; 683c68c3537SAlan Cox vm_paddr_t pa, size; 684920da7e4SAlan Cox vm_page_t m, m_ret, msucc; 685c68c3537SAlan Cox vm_pindex_t first, leftcap, rightcap; 686c68c3537SAlan Cox vm_reserv_t rv; 687c68c3537SAlan Cox u_long allocpages, maxpages, minpages; 688c68c3537SAlan Cox int i, index, n; 689c68c3537SAlan Cox 69089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 691c68c3537SAlan Cox KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0")); 692c68c3537SAlan Cox 693c68c3537SAlan Cox /* 694c68c3537SAlan Cox * Is a reservation fundamentally impossible? 695c68c3537SAlan Cox */ 696c68c3537SAlan Cox if (pindex < VM_RESERV_INDEX(object, pindex) || 697c68c3537SAlan Cox pindex + npages > object->size) 698c68c3537SAlan Cox return (NULL); 699c68c3537SAlan Cox 700c68c3537SAlan Cox /* 701c68c3537SAlan Cox * All reservations of a particular size have the same alignment. 702c68c3537SAlan Cox * Assuming that the first page is allocated from a reservation, the 703c68c3537SAlan Cox * least significant bits of its physical address can be determined 704c68c3537SAlan Cox * from its offset from the beginning of the reservation and the size 705c68c3537SAlan Cox * of the reservation. 706c68c3537SAlan Cox * 707c68c3537SAlan Cox * Could the specified index within a reservation of the smallest 708c68c3537SAlan Cox * possible size satisfy the alignment and boundary requirements? 709c68c3537SAlan Cox */ 710c68c3537SAlan Cox pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT; 711c68c3537SAlan Cox if ((pa & (alignment - 1)) != 0) 712c68c3537SAlan Cox return (NULL); 713c68c3537SAlan Cox size = npages << PAGE_SHIFT; 714c68c3537SAlan Cox if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 715c68c3537SAlan Cox return (NULL); 716c68c3537SAlan Cox 717c68c3537SAlan Cox /* 718e2068d0bSJeff Roberson * Callers should've extended an existing reservation prior to 719e2068d0bSJeff Roberson * calling this function. If a reservation exists it is 720e2068d0bSJeff Roberson * incompatible with the allocation. 721c68c3537SAlan Cox */ 722e2068d0bSJeff Roberson rv = vm_reserv_from_object(object, pindex, mpred, &msucc); 723e2068d0bSJeff Roberson if (rv != NULL) 724e2068d0bSJeff Roberson return (NULL); 725c68c3537SAlan Cox 726c68c3537SAlan Cox /* 727c68c3537SAlan Cox * Could at least one reservation fit between the first index to the 72864f096eeSAlan Cox * left that can be used ("leftcap") and the first index to the right 72964f096eeSAlan Cox * that cannot be used ("rightcap")? 730e2068d0bSJeff Roberson * 731e2068d0bSJeff Roberson * We must synchronize with the reserv object lock to protect the 732e2068d0bSJeff Roberson * pindex/object of the resulting reservations against rename while 733e2068d0bSJeff Roberson * we are inspecting. 734c68c3537SAlan Cox */ 735c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 736e2068d0bSJeff Roberson minpages = VM_RESERV_INDEX(object, pindex) + npages; 737e2068d0bSJeff Roberson maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES); 738e2068d0bSJeff Roberson allocpages = maxpages; 739e2068d0bSJeff Roberson vm_reserv_object_lock(object); 740c68c3537SAlan Cox if (mpred != NULL) { 741c68c3537SAlan Cox if ((rv = vm_reserv_from_page(mpred))->object != object) 742c68c3537SAlan Cox leftcap = mpred->pindex + 1; 743c68c3537SAlan Cox else 744c68c3537SAlan Cox leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 745e2068d0bSJeff Roberson if (leftcap > first) { 746e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 747c68c3537SAlan Cox return (NULL); 748c68c3537SAlan Cox } 749e2068d0bSJeff Roberson } 750c68c3537SAlan Cox if (msucc != NULL) { 751c68c3537SAlan Cox if ((rv = vm_reserv_from_page(msucc))->object != object) 752c68c3537SAlan Cox rightcap = msucc->pindex; 753c68c3537SAlan Cox else 754c68c3537SAlan Cox rightcap = rv->pindex; 755c68c3537SAlan Cox if (first + maxpages > rightcap) { 756e2068d0bSJeff Roberson if (maxpages == VM_LEVEL_0_NPAGES) { 757e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 758c68c3537SAlan Cox return (NULL); 759e2068d0bSJeff Roberson } 76064f096eeSAlan Cox 76164f096eeSAlan Cox /* 76264f096eeSAlan Cox * At least one reservation will fit between "leftcap" 76364f096eeSAlan Cox * and "rightcap". However, a reservation for the 76464f096eeSAlan Cox * last of the requested pages will not fit. Reduce 76564f096eeSAlan Cox * the size of the upcoming allocation accordingly. 76664f096eeSAlan Cox */ 767c68c3537SAlan Cox allocpages = minpages; 768c68c3537SAlan Cox } 769c68c3537SAlan Cox } 770e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 771c68c3537SAlan Cox 772c68c3537SAlan Cox /* 773c68c3537SAlan Cox * Would the last new reservation extend past the end of the object? 774c68c3537SAlan Cox */ 775c68c3537SAlan Cox if (first + maxpages > object->size) { 776c68c3537SAlan Cox /* 777c68c3537SAlan Cox * Don't allocate the last new reservation if the object is a 778c68c3537SAlan Cox * vnode or backed by another object that is a vnode. 779c68c3537SAlan Cox */ 780c68c3537SAlan Cox if (object->type == OBJT_VNODE || 781c68c3537SAlan Cox (object->backing_object != NULL && 782c68c3537SAlan Cox object->backing_object->type == OBJT_VNODE)) { 783c68c3537SAlan Cox if (maxpages == VM_LEVEL_0_NPAGES) 784c68c3537SAlan Cox return (NULL); 785c68c3537SAlan Cox allocpages = minpages; 786c68c3537SAlan Cox } 787c68c3537SAlan Cox /* Speculate that the object may grow. */ 788c68c3537SAlan Cox } 789c68c3537SAlan Cox 790c68c3537SAlan Cox /* 79164f096eeSAlan Cox * Allocate the physical pages. The alignment and boundary specified 79264f096eeSAlan Cox * for this allocation may be different from the alignment and 79364f096eeSAlan Cox * boundary specified for the requested pages. For instance, the 79464f096eeSAlan Cox * specified index may not be the first page within the first new 79564f096eeSAlan Cox * reservation. 796c68c3537SAlan Cox */ 7975c930c89SJeff Roberson m = NULL; 7985c930c89SJeff Roberson vmd = VM_DOMAIN(domain); 7995c930c89SJeff Roberson if (vm_domain_allocate(vmd, req, npages)) { 8005c930c89SJeff Roberson vm_domain_free_lock(vmd); 8015c930c89SJeff Roberson m = vm_phys_alloc_contig(domain, allocpages, low, high, 8025c930c89SJeff Roberson ulmax(alignment, VM_LEVEL_0_SIZE), 8035c930c89SJeff Roberson boundary > VM_LEVEL_0_SIZE ? boundary : 0); 8045c930c89SJeff Roberson vm_domain_free_unlock(vmd); 8055c930c89SJeff Roberson if (m == NULL) { 8065c930c89SJeff Roberson vm_domain_freecnt_inc(vmd, npages); 8075c930c89SJeff Roberson return (NULL); 8085c930c89SJeff Roberson } 8095c930c89SJeff Roberson } else 810c68c3537SAlan Cox return (NULL); 811e2068d0bSJeff Roberson KASSERT(vm_phys_domain(m) == domain, 8127a469c8eSJeff Roberson ("vm_reserv_alloc_contig: Page domain does not match requested.")); 81364f096eeSAlan Cox 81464f096eeSAlan Cox /* 81564f096eeSAlan Cox * The allocated physical pages always begin at a reservation 81664f096eeSAlan Cox * boundary, but they do not always end at a reservation boundary. 81764f096eeSAlan Cox * Initialize every reservation that is completely covered by the 81864f096eeSAlan Cox * allocated physical pages. 81964f096eeSAlan Cox */ 820c68c3537SAlan Cox m_ret = NULL; 821c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 822c68c3537SAlan Cox do { 823c68c3537SAlan Cox rv = vm_reserv_from_page(m); 824c68c3537SAlan Cox KASSERT(rv->pages == m, 825c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p's pages is corrupted", 826c68c3537SAlan Cox rv)); 8275c930c89SJeff Roberson vm_reserv_lock(rv); 828e2068d0bSJeff Roberson vm_reserv_insert(rv, object, first); 829c68c3537SAlan Cox n = ulmin(VM_LEVEL_0_NPAGES - index, npages); 830c68c3537SAlan Cox for (i = 0; i < n; i++) 831ec179322SAlan Cox vm_reserv_populate(rv, index + i); 832c68c3537SAlan Cox npages -= n; 833c68c3537SAlan Cox if (m_ret == NULL) { 834c68c3537SAlan Cox m_ret = &rv->pages[index]; 835c68c3537SAlan Cox index = 0; 836c68c3537SAlan Cox } 8375c930c89SJeff Roberson vm_reserv_unlock(rv); 838c68c3537SAlan Cox m += VM_LEVEL_0_NPAGES; 839c68c3537SAlan Cox first += VM_LEVEL_0_NPAGES; 840c68c3537SAlan Cox allocpages -= VM_LEVEL_0_NPAGES; 84164f096eeSAlan Cox } while (allocpages >= VM_LEVEL_0_NPAGES); 842c68c3537SAlan Cox return (m_ret); 843e2068d0bSJeff Roberson } 844c68c3537SAlan Cox 845c68c3537SAlan Cox /* 846e2068d0bSJeff Roberson * Attempts to extend an existing reservation and allocate the page to the 847e2068d0bSJeff Roberson * object. 848e2068d0bSJeff Roberson * 849e2068d0bSJeff Roberson * The page "mpred" must immediately precede the offset "pindex" within the 850e2068d0bSJeff Roberson * specified object. 851e2068d0bSJeff Roberson * 852e2068d0bSJeff Roberson * The object must be locked. 853c68c3537SAlan Cox */ 854e2068d0bSJeff Roberson vm_page_t 855e2068d0bSJeff Roberson vm_reserv_extend(int req, vm_object_t object, vm_pindex_t pindex, int domain, 856e2068d0bSJeff Roberson vm_page_t mpred) 857e2068d0bSJeff Roberson { 858e2068d0bSJeff Roberson struct vm_domain *vmd; 859e2068d0bSJeff Roberson vm_page_t m, msucc; 860e2068d0bSJeff Roberson vm_reserv_t rv; 86130fbfddaSJeff Roberson int index; 862e2068d0bSJeff Roberson 863e2068d0bSJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 864e2068d0bSJeff Roberson 865e2068d0bSJeff Roberson /* 866e2068d0bSJeff Roberson * Could a reservation currently exist? 867e2068d0bSJeff Roberson */ 868e2068d0bSJeff Roberson if (pindex < VM_RESERV_INDEX(object, pindex) || 869e2068d0bSJeff Roberson pindex >= object->size || object->resident_page_count == 0) 870e2068d0bSJeff Roberson return (NULL); 871e2068d0bSJeff Roberson 872e2068d0bSJeff Roberson /* 873e2068d0bSJeff Roberson * Look for an existing reservation. 874e2068d0bSJeff Roberson */ 875e2068d0bSJeff Roberson rv = vm_reserv_from_object(object, pindex, mpred, &msucc); 876e2068d0bSJeff Roberson if (rv == NULL) 877e2068d0bSJeff Roberson return (NULL); 878e2068d0bSJeff Roberson 879e2068d0bSJeff Roberson KASSERT(object != kernel_object || rv->domain == domain, 880e2068d0bSJeff Roberson ("vm_reserv_extend: Domain mismatch from reservation.")); 881e2068d0bSJeff Roberson domain = rv->domain; 882e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 883c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 884c68c3537SAlan Cox m = &rv->pages[index]; 8855c930c89SJeff Roberson vm_reserv_lock(rv); 886e2068d0bSJeff Roberson /* Handle reclaim race. */ 8875c930c89SJeff Roberson if (rv->object != object || 888c68c3537SAlan Cox /* Handle vm_page_rename(m, new_object, ...). */ 8895c930c89SJeff Roberson popmap_is_set(rv->popmap, index)) { 890e2068d0bSJeff Roberson m = NULL; 8915c930c89SJeff Roberson goto out; 89230fbfddaSJeff Roberson } 8935c930c89SJeff Roberson if (vm_domain_allocate(vmd, req, 1) == 0) 8945c930c89SJeff Roberson m = NULL; 8955c930c89SJeff Roberson else 8965c930c89SJeff Roberson vm_reserv_populate(rv, index); 8975c930c89SJeff Roberson out: 8985c930c89SJeff Roberson vm_reserv_unlock(rv); 899e2068d0bSJeff Roberson 900c68c3537SAlan Cox return (m); 901c68c3537SAlan Cox } 902c68c3537SAlan Cox 903c68c3537SAlan Cox /* 9045f70fb14SMark Johnston * Attempts to allocate a new reservation for the object, and allocates a 9055f70fb14SMark Johnston * page from that reservation. Callers should first invoke vm_reserv_extend() 9065f70fb14SMark Johnston * to attempt an allocation from an existing reservation. 907f8a47341SAlan Cox * 908404eb1b3SAlan Cox * The page "mpred" must immediately precede the offset "pindex" within the 909404eb1b3SAlan Cox * specified object. 910404eb1b3SAlan Cox * 911f8a47341SAlan Cox * The object and free page queue must be locked. 912f8a47341SAlan Cox */ 913f8a47341SAlan Cox vm_page_t 9145c930c89SJeff Roberson vm_reserv_alloc_page(int req, vm_object_t object, vm_pindex_t pindex, int domain, 915ef435ae7SJeff Roberson vm_page_t mpred) 916f8a47341SAlan Cox { 9175c930c89SJeff Roberson struct vm_domain *vmd; 918404eb1b3SAlan Cox vm_page_t m, msucc; 919f8a47341SAlan Cox vm_pindex_t first, leftcap, rightcap; 920f8a47341SAlan Cox vm_reserv_t rv; 921e2068d0bSJeff Roberson int index; 922f8a47341SAlan Cox 92389f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 924f8a47341SAlan Cox 925f8a47341SAlan Cox /* 926c68c3537SAlan Cox * Is a reservation fundamentally impossible? 927f8a47341SAlan Cox */ 928f8a47341SAlan Cox if (pindex < VM_RESERV_INDEX(object, pindex) || 929f8a47341SAlan Cox pindex >= object->size) 930f8a47341SAlan Cox return (NULL); 931f8a47341SAlan Cox 932f8a47341SAlan Cox /* 933e2068d0bSJeff Roberson * Callers should've extended an existing reservation prior to 934e2068d0bSJeff Roberson * calling this function. If a reservation exists it is 935e2068d0bSJeff Roberson * incompatible with the allocation. 936f8a47341SAlan Cox */ 937e2068d0bSJeff Roberson rv = vm_reserv_from_object(object, pindex, mpred, &msucc); 938e2068d0bSJeff Roberson if (rv != NULL) 939e2068d0bSJeff Roberson return (NULL); 940f8a47341SAlan Cox 941f8a47341SAlan Cox /* 942c68c3537SAlan Cox * Could a reservation fit between the first index to the left that 943c68c3537SAlan Cox * can be used and the first index to the right that cannot be used? 944e2068d0bSJeff Roberson * 945e2068d0bSJeff Roberson * We must synchronize with the reserv object lock to protect the 946e2068d0bSJeff Roberson * pindex/object of the resulting reservations against rename while 947e2068d0bSJeff Roberson * we are inspecting. 948f8a47341SAlan Cox */ 949c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 950e2068d0bSJeff Roberson vm_reserv_object_lock(object); 951c68c3537SAlan Cox if (mpred != NULL) { 952c68c3537SAlan Cox if ((rv = vm_reserv_from_page(mpred))->object != object) 953f8a47341SAlan Cox leftcap = mpred->pindex + 1; 954f8a47341SAlan Cox else 955f8a47341SAlan Cox leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 956e2068d0bSJeff Roberson if (leftcap > first) { 957e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 958c68c3537SAlan Cox return (NULL); 959c68c3537SAlan Cox } 960e2068d0bSJeff Roberson } 961c68c3537SAlan Cox if (msucc != NULL) { 962c68c3537SAlan Cox if ((rv = vm_reserv_from_page(msucc))->object != object) 963f8a47341SAlan Cox rightcap = msucc->pindex; 964f8a47341SAlan Cox else 965f8a47341SAlan Cox rightcap = rv->pindex; 966e2068d0bSJeff Roberson if (first + VM_LEVEL_0_NPAGES > rightcap) { 967e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 968f8a47341SAlan Cox return (NULL); 969c68c3537SAlan Cox } 970e2068d0bSJeff Roberson } 971e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 972f8a47341SAlan Cox 973f8a47341SAlan Cox /* 974c68c3537SAlan Cox * Would a new reservation extend past the end of the object? 975f8a47341SAlan Cox */ 976c68c3537SAlan Cox if (first + VM_LEVEL_0_NPAGES > object->size) { 977f8a47341SAlan Cox /* 978f8a47341SAlan Cox * Don't allocate a new reservation if the object is a vnode or 979f8a47341SAlan Cox * backed by another object that is a vnode. 980f8a47341SAlan Cox */ 981f8a47341SAlan Cox if (object->type == OBJT_VNODE || 982f8a47341SAlan Cox (object->backing_object != NULL && 983f8a47341SAlan Cox object->backing_object->type == OBJT_VNODE)) 984f8a47341SAlan Cox return (NULL); 985f8a47341SAlan Cox /* Speculate that the object may grow. */ 986f8a47341SAlan Cox } 987f8a47341SAlan Cox 988f8a47341SAlan Cox /* 989c68c3537SAlan Cox * Allocate and populate the new reservation. 990f8a47341SAlan Cox */ 9915c930c89SJeff Roberson m = NULL; 9925c930c89SJeff Roberson vmd = VM_DOMAIN(domain); 9935c930c89SJeff Roberson if (vm_domain_allocate(vmd, req, 1)) { 9945c930c89SJeff Roberson vm_domain_free_lock(vmd); 9955c930c89SJeff Roberson m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT, 9965c930c89SJeff Roberson VM_LEVEL_0_ORDER); 9975c930c89SJeff Roberson vm_domain_free_unlock(vmd); 9985c930c89SJeff Roberson if (m == NULL) { 9995c930c89SJeff Roberson vm_domain_freecnt_inc(vmd, 1); 10005c930c89SJeff Roberson return (NULL); 10015c930c89SJeff Roberson } 10025c930c89SJeff Roberson } else 1003c68c3537SAlan Cox return (NULL); 1004f8a47341SAlan Cox rv = vm_reserv_from_page(m); 10055c930c89SJeff Roberson vm_reserv_lock(rv); 1006f8a47341SAlan Cox KASSERT(rv->pages == m, 1007c68c3537SAlan Cox ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv)); 1008e2068d0bSJeff Roberson vm_reserv_insert(rv, object, first); 1009ec179322SAlan Cox index = VM_RESERV_INDEX(object, pindex); 1010ec179322SAlan Cox vm_reserv_populate(rv, index); 10115c930c89SJeff Roberson vm_reserv_unlock(rv); 10125c930c89SJeff Roberson 1013ec179322SAlan Cox return (&rv->pages[index]); 1014f8a47341SAlan Cox } 1015f8a47341SAlan Cox 1016f8a47341SAlan Cox /* 1017ada27a3bSKonstantin Belousov * Breaks the given reservation. All free pages in the reservation 1018ada27a3bSKonstantin Belousov * are returned to the physical memory allocator. The reservation's 1019ada27a3bSKonstantin Belousov * population count and map are reset to their initial state. 1020ec179322SAlan Cox * 10213453bca8SAlan Cox * The given reservation must not be in the partially populated reservation 1022ec179322SAlan Cox * queue. The free page queue lock must be held. 1023ec179322SAlan Cox */ 1024ec179322SAlan Cox static void 1025ada27a3bSKonstantin Belousov vm_reserv_break(vm_reserv_t rv) 1026ec179322SAlan Cox { 1027ec179322SAlan Cox int begin_zeroes, hi, i, lo; 1028ec179322SAlan Cox 10295c930c89SJeff Roberson vm_reserv_assert_locked(rv); 10305c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 10315c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 1032e2068d0bSJeff Roberson vm_reserv_remove(rv); 1033c4be9169SKonstantin Belousov rv->pages->psind = 0; 1034ec179322SAlan Cox i = hi = 0; 1035ec179322SAlan Cox do { 1036ec179322SAlan Cox /* Find the next 0 bit. Any previous 0 bits are < "hi". */ 1037ec179322SAlan Cox lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i])); 1038ec179322SAlan Cox if (lo == 0) { 1039ec179322SAlan Cox /* Redundantly clears bits < "hi". */ 1040ec179322SAlan Cox rv->popmap[i] = 0; 1041ec179322SAlan Cox rv->popcnt -= NBPOPMAP - hi; 1042ec179322SAlan Cox while (++i < NPOPMAP) { 1043ec179322SAlan Cox lo = ffsl(~rv->popmap[i]); 1044ec179322SAlan Cox if (lo == 0) { 1045ec179322SAlan Cox rv->popmap[i] = 0; 1046ec179322SAlan Cox rv->popcnt -= NBPOPMAP; 1047ec179322SAlan Cox } else 1048ec179322SAlan Cox break; 1049ec179322SAlan Cox } 1050ec179322SAlan Cox if (i == NPOPMAP) 1051ec179322SAlan Cox break; 1052ec179322SAlan Cox hi = 0; 1053ec179322SAlan Cox } 1054ec179322SAlan Cox KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo)); 1055ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 1056ec179322SAlan Cox lo--; 1057ec179322SAlan Cox if (lo > 0) { 1058ec179322SAlan Cox /* Redundantly clears bits < "hi". */ 1059ec179322SAlan Cox rv->popmap[i] &= ~((1UL << lo) - 1); 1060ec179322SAlan Cox rv->popcnt -= lo - hi; 1061ec179322SAlan Cox } 1062ec179322SAlan Cox begin_zeroes = NBPOPMAP * i + lo; 1063ec179322SAlan Cox /* Find the next 1 bit. */ 1064ec179322SAlan Cox do 1065ec179322SAlan Cox hi = ffsl(rv->popmap[i]); 1066ec179322SAlan Cox while (hi == 0 && ++i < NPOPMAP); 1067ec179322SAlan Cox if (i != NPOPMAP) 1068ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 1069ec179322SAlan Cox hi--; 10705c930c89SJeff Roberson vm_domain_free_lock(VM_DOMAIN(rv->domain)); 1071ec179322SAlan Cox vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i + 1072ec179322SAlan Cox hi - begin_zeroes); 10735c930c89SJeff Roberson vm_domain_free_unlock(VM_DOMAIN(rv->domain)); 1074ec179322SAlan Cox } while (i < NPOPMAP); 1075ec179322SAlan Cox KASSERT(rv->popcnt == 0, 1076ec179322SAlan Cox ("vm_reserv_break: reserv %p's popcnt is corrupted", rv)); 10775c930c89SJeff Roberson counter_u64_add(vm_reserv_broken, 1); 1078ec179322SAlan Cox } 1079ec179322SAlan Cox 1080ec179322SAlan Cox /* 1081f8a47341SAlan Cox * Breaks all reservations belonging to the given object. 1082f8a47341SAlan Cox */ 1083f8a47341SAlan Cox void 1084f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object) 1085f8a47341SAlan Cox { 1086f8a47341SAlan Cox vm_reserv_t rv; 1087f8a47341SAlan Cox 1088e2068d0bSJeff Roberson /* 1089e2068d0bSJeff Roberson * This access of object->rvq is unsynchronized so that the 1090e2068d0bSJeff Roberson * object rvq lock can nest after the domain_free lock. We 1091e2068d0bSJeff Roberson * must check for races in the results. However, the object 1092e2068d0bSJeff Roberson * lock prevents new additions, so we are guaranteed that when 1093e2068d0bSJeff Roberson * it returns NULL the object is properly empty. 1094e2068d0bSJeff Roberson */ 1095f8a47341SAlan Cox while ((rv = LIST_FIRST(&object->rvq)) != NULL) { 10965c930c89SJeff Roberson vm_reserv_lock(rv); 1097e2068d0bSJeff Roberson /* Reclaim race. */ 10985c930c89SJeff Roberson if (rv->object != object) { 10995c930c89SJeff Roberson vm_reserv_unlock(rv); 1100e2068d0bSJeff Roberson continue; 11015c930c89SJeff Roberson } 11025c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 1103f8a47341SAlan Cox if (rv->inpartpopq) { 1104ef435ae7SJeff Roberson TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq); 1105f8a47341SAlan Cox rv->inpartpopq = FALSE; 1106f8a47341SAlan Cox } 11075c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 1108ada27a3bSKonstantin Belousov vm_reserv_break(rv); 11095c930c89SJeff Roberson vm_reserv_unlock(rv); 1110f8a47341SAlan Cox } 1111f8a47341SAlan Cox } 1112f8a47341SAlan Cox 1113f8a47341SAlan Cox /* 1114f8a47341SAlan Cox * Frees the given page if it belongs to a reservation. Returns TRUE if the 1115f8a47341SAlan Cox * page is freed and FALSE otherwise. 1116f8a47341SAlan Cox * 1117f8a47341SAlan Cox * The free page queue lock must be held. 1118f8a47341SAlan Cox */ 1119f8a47341SAlan Cox boolean_t 1120f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m) 1121f8a47341SAlan Cox { 1122f8a47341SAlan Cox vm_reserv_t rv; 11235c930c89SJeff Roberson boolean_t ret; 1124f8a47341SAlan Cox 1125f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1126908e3da1SAlan Cox if (rv->object == NULL) 1127908e3da1SAlan Cox return (FALSE); 11285c930c89SJeff Roberson vm_reserv_lock(rv); 11295c930c89SJeff Roberson /* Re-validate after lock. */ 11305c930c89SJeff Roberson if (rv->object != NULL) { 1131ec179322SAlan Cox vm_reserv_depopulate(rv, m - rv->pages); 11325c930c89SJeff Roberson ret = TRUE; 11335c930c89SJeff Roberson } else 11345c930c89SJeff Roberson ret = FALSE; 11355c930c89SJeff Roberson vm_reserv_unlock(rv); 11365c930c89SJeff Roberson 11375c930c89SJeff Roberson return (ret); 1138f8a47341SAlan Cox } 1139f8a47341SAlan Cox 1140f8a47341SAlan Cox /* 1141f8a47341SAlan Cox * Initializes the reservation management system. Specifically, initializes 1142f8a47341SAlan Cox * the reservation array. 1143f8a47341SAlan Cox * 1144f8a47341SAlan Cox * Requires that vm_page_array and first_page are initialized! 1145f8a47341SAlan Cox */ 1146f8a47341SAlan Cox void 1147f8a47341SAlan Cox vm_reserv_init(void) 1148f8a47341SAlan Cox { 1149f8a47341SAlan Cox vm_paddr_t paddr; 115009e5f3c4SAlan Cox struct vm_phys_seg *seg; 11515c930c89SJeff Roberson struct vm_reserv *rv; 1152ef435ae7SJeff Roberson int i, segind; 1153f8a47341SAlan Cox 1154f8a47341SAlan Cox /* 1155f8a47341SAlan Cox * Initialize the reservation array. Specifically, initialize the 1156f8a47341SAlan Cox * "pages" field for every element that has an underlying superpage. 1157f8a47341SAlan Cox */ 115809e5f3c4SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 115909e5f3c4SAlan Cox seg = &vm_phys_segs[segind]; 116009e5f3c4SAlan Cox paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 116109e5f3c4SAlan Cox while (paddr + VM_LEVEL_0_SIZE <= seg->end) { 11625c930c89SJeff Roberson rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT]; 11635c930c89SJeff Roberson rv->pages = PHYS_TO_VM_PAGE(paddr); 11645c930c89SJeff Roberson rv->domain = seg->domain; 11655c930c89SJeff Roberson mtx_init(&rv->lock, "vm reserv", NULL, MTX_DEF); 1166f8a47341SAlan Cox paddr += VM_LEVEL_0_SIZE; 1167f8a47341SAlan Cox } 1168f8a47341SAlan Cox } 11695c930c89SJeff Roberson for (i = 0; i < MAXMEMDOM; i++) { 11705c930c89SJeff Roberson mtx_init(&vm_reserv_domain_locks[i], "VM reserv domain", NULL, 11715c930c89SJeff Roberson MTX_DEF); 1172ef435ae7SJeff Roberson TAILQ_INIT(&vm_rvq_partpop[i]); 1173f8a47341SAlan Cox } 1174f8a47341SAlan Cox 11755c930c89SJeff Roberson for (i = 0; i < VM_RESERV_OBJ_LOCK_COUNT; i++) 11765c930c89SJeff Roberson mtx_init(&vm_reserv_object_mtx[i], "resv obj lock", NULL, 11775c930c89SJeff Roberson MTX_DEF); 11785c930c89SJeff Roberson } 11795c930c89SJeff Roberson 1180f8a47341SAlan Cox /* 1181c869e672SAlan Cox * Returns true if the given page belongs to a reservation and that page is 1182c869e672SAlan Cox * free. Otherwise, returns false. 1183c869e672SAlan Cox */ 1184c869e672SAlan Cox bool 1185c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m) 1186c869e672SAlan Cox { 1187c869e672SAlan Cox vm_reserv_t rv; 1188c869e672SAlan Cox 1189c869e672SAlan Cox rv = vm_reserv_from_page(m); 1190c869e672SAlan Cox if (rv->object == NULL) 1191c869e672SAlan Cox return (false); 1192c869e672SAlan Cox return (popmap_is_clear(rv->popmap, m - rv->pages)); 1193c869e672SAlan Cox } 1194c869e672SAlan Cox 1195c869e672SAlan Cox /* 1196c869e672SAlan Cox * If the given page belongs to a reservation, returns the level of that 1197c869e672SAlan Cox * reservation. Otherwise, returns -1. 1198c869e672SAlan Cox */ 1199c869e672SAlan Cox int 1200c869e672SAlan Cox vm_reserv_level(vm_page_t m) 1201c869e672SAlan Cox { 1202c869e672SAlan Cox vm_reserv_t rv; 1203c869e672SAlan Cox 1204c869e672SAlan Cox rv = vm_reserv_from_page(m); 1205c869e672SAlan Cox return (rv->object != NULL ? 0 : -1); 1206c869e672SAlan Cox } 1207c869e672SAlan Cox 1208c869e672SAlan Cox /* 12093453bca8SAlan Cox * Returns a reservation level if the given page belongs to a fully populated 1210f8a47341SAlan Cox * reservation and -1 otherwise. 1211f8a47341SAlan Cox */ 1212f8a47341SAlan Cox int 1213f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m) 1214f8a47341SAlan Cox { 1215f8a47341SAlan Cox vm_reserv_t rv; 1216f8a47341SAlan Cox 1217f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1218f8a47341SAlan Cox return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1); 1219f8a47341SAlan Cox } 1220f8a47341SAlan Cox 1221f8a47341SAlan Cox /* 12223453bca8SAlan Cox * Breaks the given partially populated reservation, releasing its free pages 12233453bca8SAlan Cox * to the physical memory allocator. 1224f8a47341SAlan Cox * 1225f8a47341SAlan Cox * The free page queue lock must be held. 1226f8a47341SAlan Cox */ 122744aab2c3SAlan Cox static void 122844aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv) 1229f8a47341SAlan Cox { 1230f8a47341SAlan Cox 12315c930c89SJeff Roberson vm_reserv_assert_locked(rv); 12325c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 12335c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 12345c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 1235f8a47341SAlan Cox KASSERT(rv->inpartpopq, 1236ec179322SAlan Cox ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv)); 12372d3f4181SJeff Roberson KASSERT(rv->domain < vm_ndomains, 1238ef435ae7SJeff Roberson ("vm_reserv_reclaim: reserv %p's domain is corrupted %d", 1239ef435ae7SJeff Roberson rv, rv->domain)); 1240ef435ae7SJeff Roberson TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq); 1241f8a47341SAlan Cox rv->inpartpopq = FALSE; 12425c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 1243ada27a3bSKonstantin Belousov vm_reserv_break(rv); 12445c930c89SJeff Roberson counter_u64_add(vm_reserv_reclaimed, 1); 124544aab2c3SAlan Cox } 124644aab2c3SAlan Cox 124744aab2c3SAlan Cox /* 12483453bca8SAlan Cox * Breaks the reservation at the head of the partially populated reservation 12493453bca8SAlan Cox * queue, releasing its free pages to the physical memory allocator. Returns 12503453bca8SAlan Cox * TRUE if a reservation is broken and FALSE otherwise. 125144aab2c3SAlan Cox * 125244aab2c3SAlan Cox * The free page queue lock must be held. 125344aab2c3SAlan Cox */ 125444aab2c3SAlan Cox boolean_t 1255ef435ae7SJeff Roberson vm_reserv_reclaim_inactive(int domain) 125644aab2c3SAlan Cox { 125744aab2c3SAlan Cox vm_reserv_t rv; 125844aab2c3SAlan Cox 12595c930c89SJeff Roberson while ((rv = TAILQ_FIRST(&vm_rvq_partpop[domain])) != NULL) { 12605c930c89SJeff Roberson vm_reserv_lock(rv); 12615c930c89SJeff Roberson if (rv != TAILQ_FIRST(&vm_rvq_partpop[domain])) { 12625c930c89SJeff Roberson vm_reserv_unlock(rv); 12635c930c89SJeff Roberson continue; 12645c930c89SJeff Roberson } 126544aab2c3SAlan Cox vm_reserv_reclaim(rv); 12665c930c89SJeff Roberson vm_reserv_unlock(rv); 1267f8a47341SAlan Cox return (TRUE); 1268f8a47341SAlan Cox } 1269f8a47341SAlan Cox return (FALSE); 1270f8a47341SAlan Cox } 1271f8a47341SAlan Cox 1272f8a47341SAlan Cox /* 12733453bca8SAlan Cox * Searches the partially populated reservation queue for the least recently 12743453bca8SAlan Cox * changed reservation with free pages that satisfy the given request for 12753453bca8SAlan Cox * contiguous physical memory. If a satisfactory reservation is found, it is 12763453bca8SAlan Cox * broken. Returns TRUE if a reservation is broken and FALSE otherwise. 127744aab2c3SAlan Cox * 127844aab2c3SAlan Cox * The free page queue lock must be held. 127944aab2c3SAlan Cox */ 128044aab2c3SAlan Cox boolean_t 1281ef435ae7SJeff Roberson vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low, 1282ef435ae7SJeff Roberson vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 128344aab2c3SAlan Cox { 1284ec179322SAlan Cox vm_paddr_t pa, size; 12855c930c89SJeff Roberson vm_reserv_t rv, rvn; 128667b7e434SAlan Cox int hi, i, lo, low_index, next_free; 128744aab2c3SAlan Cox 1288c68c3537SAlan Cox if (npages > VM_LEVEL_0_NPAGES - 1) 128944aab2c3SAlan Cox return (FALSE); 1290c68c3537SAlan Cox size = npages << PAGE_SHIFT; 12915c930c89SJeff Roberson vm_reserv_domain_lock(domain); 12925c930c89SJeff Roberson again: 12935c930c89SJeff Roberson for (rv = TAILQ_FIRST(&vm_rvq_partpop[domain]); rv != NULL; rv = rvn) { 12945c930c89SJeff Roberson rvn = TAILQ_NEXT(rv, partpopq); 129544aab2c3SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]); 129644aab2c3SAlan Cox if (pa + PAGE_SIZE - size < low) { 1297ec179322SAlan Cox /* This entire reservation is too low; go to next. */ 129844aab2c3SAlan Cox continue; 129944aab2c3SAlan Cox } 1300ec179322SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[0]); 130144aab2c3SAlan Cox if (pa + size > high) { 1302ec179322SAlan Cox /* This entire reservation is too high; go to next. */ 1303ec179322SAlan Cox continue; 130485f2a0c9SMax Laier } 13055c930c89SJeff Roberson if (vm_reserv_trylock(rv) == 0) { 13065c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 13075c930c89SJeff Roberson vm_reserv_lock(rv); 13085c930c89SJeff Roberson if (!rv->inpartpopq) { 13095c930c89SJeff Roberson vm_reserv_domain_lock(domain); 13105c930c89SJeff Roberson if (!rvn->inpartpopq) 13115c930c89SJeff Roberson goto again; 13125c930c89SJeff Roberson continue; 13135c930c89SJeff Roberson } 13145c930c89SJeff Roberson } else 13155c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 1316ec179322SAlan Cox if (pa < low) { 1317ec179322SAlan Cox /* Start the search for free pages at "low". */ 131867b7e434SAlan Cox low_index = (low + PAGE_MASK - pa) >> PAGE_SHIFT; 131967b7e434SAlan Cox i = low_index / NBPOPMAP; 132067b7e434SAlan Cox hi = low_index % NBPOPMAP; 1321ec179322SAlan Cox } else 1322ec179322SAlan Cox i = hi = 0; 1323ec179322SAlan Cox do { 1324ec179322SAlan Cox /* Find the next free page. */ 1325ec179322SAlan Cox lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i])); 1326ec179322SAlan Cox while (lo == 0 && ++i < NPOPMAP) 1327ec179322SAlan Cox lo = ffsl(~rv->popmap[i]); 1328ec179322SAlan Cox if (i == NPOPMAP) 1329ec179322SAlan Cox break; 1330ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 1331ec179322SAlan Cox lo--; 1332ec179322SAlan Cox next_free = NBPOPMAP * i + lo; 1333ec179322SAlan Cox pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]); 1334ec179322SAlan Cox KASSERT(pa >= low, 1335ec179322SAlan Cox ("vm_reserv_reclaim_contig: pa is too low")); 1336ec179322SAlan Cox if (pa + size > high) { 1337ec179322SAlan Cox /* The rest of this reservation is too high. */ 1338ec179322SAlan Cox break; 1339ec179322SAlan Cox } else if ((pa & (alignment - 1)) != 0 || 1340ec179322SAlan Cox ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) { 13416a93e36bSAlan Cox /* 13426a93e36bSAlan Cox * The current page doesn't meet the alignment 13436a93e36bSAlan Cox * and/or boundary requirements. Continue 13446a93e36bSAlan Cox * searching this reservation until the rest 13456a93e36bSAlan Cox * of its free pages are either excluded or 13466a93e36bSAlan Cox * exhausted. 13476a93e36bSAlan Cox */ 13486a93e36bSAlan Cox hi = lo + 1; 13496a93e36bSAlan Cox if (hi >= NBPOPMAP) { 13506a93e36bSAlan Cox hi = 0; 13516a93e36bSAlan Cox i++; 13526a93e36bSAlan Cox } 1353ec179322SAlan Cox continue; 1354ec179322SAlan Cox } 1355ec179322SAlan Cox /* Find the next used page. */ 1356ec179322SAlan Cox hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1)); 1357ec179322SAlan Cox while (hi == 0 && ++i < NPOPMAP) { 1358ec179322SAlan Cox if ((NBPOPMAP * i - next_free) * PAGE_SIZE >= 1359ec179322SAlan Cox size) { 136044aab2c3SAlan Cox vm_reserv_reclaim(rv); 13615c930c89SJeff Roberson vm_reserv_unlock(rv); 136244aab2c3SAlan Cox return (TRUE); 136344aab2c3SAlan Cox } 1364ec179322SAlan Cox hi = ffsl(rv->popmap[i]); 1365ec179322SAlan Cox } 1366ec179322SAlan Cox /* Convert from ffsl() to ordinary bit numbering. */ 1367ec179322SAlan Cox if (i != NPOPMAP) 1368ec179322SAlan Cox hi--; 1369ec179322SAlan Cox if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >= 1370ec179322SAlan Cox size) { 1371ec179322SAlan Cox vm_reserv_reclaim(rv); 13725c930c89SJeff Roberson vm_reserv_unlock(rv); 1373ec179322SAlan Cox return (TRUE); 1374ec179322SAlan Cox } 1375ec179322SAlan Cox } while (i < NPOPMAP); 13765c930c89SJeff Roberson vm_reserv_unlock(rv); 13775c930c89SJeff Roberson vm_reserv_domain_lock(domain); 13785c930c89SJeff Roberson if (rvn != NULL && !rvn->inpartpopq) 13795c930c89SJeff Roberson goto again; 138044aab2c3SAlan Cox } 13815c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 138244aab2c3SAlan Cox return (FALSE); 138344aab2c3SAlan Cox } 138444aab2c3SAlan Cox 138544aab2c3SAlan Cox /* 1386f8a47341SAlan Cox * Transfers the reservation underlying the given page to a new object. 1387f8a47341SAlan Cox * 1388f8a47341SAlan Cox * The object must be locked. 1389f8a47341SAlan Cox */ 1390f8a47341SAlan Cox void 1391f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object, 1392f8a47341SAlan Cox vm_pindex_t old_object_offset) 1393f8a47341SAlan Cox { 1394f8a47341SAlan Cox vm_reserv_t rv; 1395f8a47341SAlan Cox 139689f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(new_object); 1397f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1398f8a47341SAlan Cox if (rv->object == old_object) { 13995c930c89SJeff Roberson vm_reserv_lock(rv); 14005c930c89SJeff Roberson CTR6(KTR_VM, 14015c930c89SJeff Roberson "%s: rv %p object %p new %p popcnt %d inpartpop %d", 14025c930c89SJeff Roberson __FUNCTION__, rv, rv->object, new_object, rv->popcnt, 14035c930c89SJeff Roberson rv->inpartpopq); 1404f8a47341SAlan Cox if (rv->object == old_object) { 1405e2068d0bSJeff Roberson vm_reserv_object_lock(old_object); 1406e2068d0bSJeff Roberson rv->object = NULL; 1407f8a47341SAlan Cox LIST_REMOVE(rv, objq); 1408e2068d0bSJeff Roberson vm_reserv_object_unlock(old_object); 1409e2068d0bSJeff Roberson vm_reserv_object_lock(new_object); 1410f8a47341SAlan Cox rv->object = new_object; 1411f8a47341SAlan Cox rv->pindex -= old_object_offset; 1412e2068d0bSJeff Roberson LIST_INSERT_HEAD(&new_object->rvq, rv, objq); 1413e2068d0bSJeff Roberson vm_reserv_object_unlock(new_object); 1414f8a47341SAlan Cox } 14155c930c89SJeff Roberson vm_reserv_unlock(rv); 1416f8a47341SAlan Cox } 1417f8a47341SAlan Cox } 1418f8a47341SAlan Cox 1419f8a47341SAlan Cox /* 1420c869e672SAlan Cox * Returns the size (in bytes) of a reservation of the specified level. 1421c869e672SAlan Cox */ 1422c869e672SAlan Cox int 1423c869e672SAlan Cox vm_reserv_size(int level) 1424c869e672SAlan Cox { 1425c869e672SAlan Cox 1426c869e672SAlan Cox switch (level) { 1427c869e672SAlan Cox case 0: 1428c869e672SAlan Cox return (VM_LEVEL_0_SIZE); 1429c869e672SAlan Cox case -1: 1430c869e672SAlan Cox return (PAGE_SIZE); 1431c869e672SAlan Cox default: 1432c869e672SAlan Cox return (0); 1433c869e672SAlan Cox } 1434c869e672SAlan Cox } 1435c869e672SAlan Cox 1436c869e672SAlan Cox /* 1437f8a47341SAlan Cox * Allocates the virtual and physical memory required by the reservation 1438f8a47341SAlan Cox * management system's data structures, in particular, the reservation array. 1439f8a47341SAlan Cox */ 1440f8a47341SAlan Cox vm_paddr_t 1441f8a47341SAlan Cox vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water) 1442f8a47341SAlan Cox { 1443f8a47341SAlan Cox vm_paddr_t new_end; 1444f8a47341SAlan Cox size_t size; 1445f8a47341SAlan Cox 1446f8a47341SAlan Cox /* 1447f8a47341SAlan Cox * Calculate the size (in bytes) of the reservation array. Round up 1448f8a47341SAlan Cox * from "high_water" because every small page is mapped to an element 1449f8a47341SAlan Cox * in the reservation array based on its physical address. Thus, the 1450f8a47341SAlan Cox * number of elements in the reservation array can be greater than the 1451f8a47341SAlan Cox * number of superpages. 1452f8a47341SAlan Cox */ 1453f8a47341SAlan Cox size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv); 1454f8a47341SAlan Cox 1455f8a47341SAlan Cox /* 1456f8a47341SAlan Cox * Allocate and map the physical memory for the reservation array. The 1457f8a47341SAlan Cox * next available virtual address is returned by reference. 1458f8a47341SAlan Cox */ 1459f8a47341SAlan Cox new_end = end - round_page(size); 1460f8a47341SAlan Cox vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end, 1461f8a47341SAlan Cox VM_PROT_READ | VM_PROT_WRITE); 1462f8a47341SAlan Cox bzero(vm_reserv_array, size); 1463f8a47341SAlan Cox 1464f8a47341SAlan Cox /* 1465f8a47341SAlan Cox * Return the next available physical address. 1466f8a47341SAlan Cox */ 1467f8a47341SAlan Cox return (new_end); 1468f8a47341SAlan Cox } 1469f8a47341SAlan Cox 14708b5e1472SAlan Cox /* 14715c930c89SJeff Roberson * Initializes the reservation management system. Specifically, initializes 14725c930c89SJeff Roberson * the reservation counters. 14735c930c89SJeff Roberson */ 14745c930c89SJeff Roberson static void 14755c930c89SJeff Roberson vm_reserv_counter_init(void *unused) 14765c930c89SJeff Roberson { 14775c930c89SJeff Roberson 14785c930c89SJeff Roberson vm_reserv_freed = counter_u64_alloc(M_WAITOK); 14795c930c89SJeff Roberson vm_reserv_broken = counter_u64_alloc(M_WAITOK); 14805c930c89SJeff Roberson vm_reserv_reclaimed = counter_u64_alloc(M_WAITOK); 14815c930c89SJeff Roberson } 14825c930c89SJeff Roberson SYSINIT(vm_reserv_counter_init, SI_SUB_CPU, SI_ORDER_ANY, 14835c930c89SJeff Roberson vm_reserv_counter_init, NULL); 14845c930c89SJeff Roberson 14855c930c89SJeff Roberson /* 14868b5e1472SAlan Cox * Returns the superpage containing the given page. 14878b5e1472SAlan Cox */ 14888b5e1472SAlan Cox vm_page_t 14898b5e1472SAlan Cox vm_reserv_to_superpage(vm_page_t m) 14908b5e1472SAlan Cox { 14918b5e1472SAlan Cox vm_reserv_t rv; 14928b5e1472SAlan Cox 14938b5e1472SAlan Cox VM_OBJECT_ASSERT_LOCKED(m->object); 14948b5e1472SAlan Cox rv = vm_reserv_from_page(m); 14955c930c89SJeff Roberson if (rv->object == m->object && rv->popcnt == VM_LEVEL_0_NPAGES) 14965c930c89SJeff Roberson m = rv->pages; 14975c930c89SJeff Roberson else 14985c930c89SJeff Roberson m = NULL; 14995c930c89SJeff Roberson 15005c930c89SJeff Roberson return (m); 15018b5e1472SAlan Cox } 15028b5e1472SAlan Cox 1503f8a47341SAlan Cox #endif /* VM_NRESERVLEVEL > 0 */ 1504