1f8a47341SAlan Cox /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 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 #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> 5484e2ae64SDoug Moore #include <sys/bitstring.h> 5572346b22SCy Schubert #include <sys/counter.h> 5672346b22SCy Schubert #include <sys/ktr.h> 579ed01c32SGleb Smirnoff #include <sys/vmmeter.h> 585c930c89SJeff Roberson #include <sys/smp.h> 59f8a47341SAlan Cox 60f8a47341SAlan Cox #include <vm/vm.h> 61f76916c0SDoug Moore #include <vm/vm_extern.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> 66e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 67431fb8abSMark Johnston #include <vm/vm_phys.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 803e00c11aSAlan Cox /* 813e00c11aSAlan Cox * Temporarily simulate two-level reservations. Effectively, VM_LEVEL_0_* is 823e00c11aSAlan Cox * level 1, and VM_SUBLEVEL_0_* is level 0. 833e00c11aSAlan Cox */ 843e00c11aSAlan Cox #if VM_NRESERVLEVEL == 2 853e00c11aSAlan Cox #undef VM_NRESERVLEVEL 863e00c11aSAlan Cox #define VM_NRESERVLEVEL 1 873e00c11aSAlan Cox #if VM_LEVEL_0_ORDER == 4 883e00c11aSAlan Cox #undef VM_LEVEL_0_ORDER 893e00c11aSAlan Cox #define VM_LEVEL_0_ORDER (4 + VM_LEVEL_1_ORDER) 903e00c11aSAlan Cox #define VM_SUBLEVEL_0_NPAGES (1 << 4) 913e00c11aSAlan Cox #elif VM_LEVEL_0_ORDER == 7 923e00c11aSAlan Cox #undef VM_LEVEL_0_ORDER 933e00c11aSAlan Cox #define VM_LEVEL_0_ORDER (7 + VM_LEVEL_1_ORDER) 943e00c11aSAlan Cox #define VM_SUBLEVEL_0_NPAGES (1 << 7) 953e00c11aSAlan Cox #else 963e00c11aSAlan Cox #error "Unsupported level 0 reservation size" 973e00c11aSAlan Cox #endif 983e00c11aSAlan Cox #define VM_LEVEL_0_PSIND 2 993e00c11aSAlan Cox #else 1003e00c11aSAlan Cox #define VM_LEVEL_0_PSIND 1 1013e00c11aSAlan Cox #endif 1023e00c11aSAlan Cox 103f2a496d6SKonstantin Belousov #ifndef VM_LEVEL_0_ORDER_MAX 104f2a496d6SKonstantin Belousov #define VM_LEVEL_0_ORDER_MAX VM_LEVEL_0_ORDER 105f2a496d6SKonstantin Belousov #endif 106f2a496d6SKonstantin Belousov 107f8a47341SAlan Cox /* 108f8a47341SAlan Cox * The number of small pages that are contained in a level 0 reservation 109f8a47341SAlan Cox */ 110f8a47341SAlan Cox #define VM_LEVEL_0_NPAGES (1 << VM_LEVEL_0_ORDER) 111f2a496d6SKonstantin Belousov #define VM_LEVEL_0_NPAGES_MAX (1 << VM_LEVEL_0_ORDER_MAX) 112f8a47341SAlan Cox 113f8a47341SAlan Cox /* 114f8a47341SAlan Cox * The number of bits by which a physical address is shifted to obtain the 115f8a47341SAlan Cox * reservation number 116f8a47341SAlan Cox */ 117f8a47341SAlan Cox #define VM_LEVEL_0_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT) 118f8a47341SAlan Cox 119f8a47341SAlan Cox /* 120f8a47341SAlan Cox * The size of a level 0 reservation in bytes 121f8a47341SAlan Cox */ 122f8a47341SAlan Cox #define VM_LEVEL_0_SIZE (1 << VM_LEVEL_0_SHIFT) 123f8a47341SAlan Cox 124f8a47341SAlan Cox /* 125f8a47341SAlan Cox * Computes the index of the small page underlying the given (object, pindex) 126f8a47341SAlan Cox * within the reservation's array of small pages. 127f8a47341SAlan Cox */ 128f8a47341SAlan Cox #define VM_RESERV_INDEX(object, pindex) \ 129f8a47341SAlan Cox (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1)) 130f8a47341SAlan Cox 131f8a47341SAlan Cox /* 1322ef6727eSJeff Roberson * Number of elapsed ticks before we update the LRU queue position. Used 1332ef6727eSJeff Roberson * to reduce contention and churn on the list. 1342ef6727eSJeff Roberson */ 1352ef6727eSJeff Roberson #define PARTPOPSLOP 1 1362ef6727eSJeff Roberson 1372ef6727eSJeff Roberson /* 138f8a47341SAlan Cox * The reservation structure 139f8a47341SAlan Cox * 140f8a47341SAlan Cox * A reservation structure is constructed whenever a large physical page is 141f8a47341SAlan Cox * speculatively allocated to an object. The reservation provides the small 142f8a47341SAlan Cox * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets 143f8a47341SAlan Cox * within that object. The reservation's "popcnt" tracks the number of these 144f8a47341SAlan Cox * small physical pages that are in use at any given time. When and if the 1453453bca8SAlan Cox * reservation is not fully utilized, it appears in the queue of partially 146f8a47341SAlan Cox * populated reservations. The reservation always appears on the containing 147f8a47341SAlan Cox * object's list of reservations. 148f8a47341SAlan Cox * 1493453bca8SAlan Cox * A partially populated reservation can be broken and reclaimed at any time. 150e2068d0bSJeff Roberson * 151b378d296SMark Johnston * c - constant after boot 1525c930c89SJeff Roberson * d - vm_reserv_domain_lock 153e2068d0bSJeff Roberson * o - vm_reserv_object_lock 154b378d296SMark Johnston * r - vm_reserv_lock 155b378d296SMark Johnston * s - vm_reserv_domain_scan_lock 156f8a47341SAlan Cox */ 157f8a47341SAlan Cox struct vm_reserv { 1585c930c89SJeff Roberson struct mtx lock; /* reservation lock. */ 159fe6d5344SMark Johnston TAILQ_ENTRY(vm_reserv) partpopq; /* (d, r) per-domain queue. */ 1605c930c89SJeff Roberson LIST_ENTRY(vm_reserv) objq; /* (o, r) object queue */ 1615c930c89SJeff Roberson vm_object_t object; /* (o, r) containing object */ 1625c930c89SJeff Roberson vm_pindex_t pindex; /* (o, r) offset in object */ 163e2068d0bSJeff Roberson vm_page_t pages; /* (c) first page */ 1645c930c89SJeff Roberson uint16_t popcnt; /* (r) # of pages in use */ 165fe6d5344SMark Johnston uint8_t domain; /* (c) NUMA domain. */ 166fe6d5344SMark Johnston char inpartpopq; /* (d, r) */ 1672ef6727eSJeff Roberson int lasttick; /* (r) last pop update tick. */ 16884e2ae64SDoug Moore bitstr_t bit_decl(popmap, VM_LEVEL_0_NPAGES_MAX); 16984e2ae64SDoug Moore /* (r) bit vector, used pages */ 170f8a47341SAlan Cox }; 171f8a47341SAlan Cox 172b378d296SMark Johnston TAILQ_HEAD(vm_reserv_queue, vm_reserv); 173b378d296SMark Johnston 1745c930c89SJeff Roberson #define vm_reserv_lockptr(rv) (&(rv)->lock) 1755c930c89SJeff Roberson #define vm_reserv_assert_locked(rv) \ 1765c930c89SJeff Roberson mtx_assert(vm_reserv_lockptr(rv), MA_OWNED) 1775c930c89SJeff Roberson #define vm_reserv_lock(rv) mtx_lock(vm_reserv_lockptr(rv)) 1785c930c89SJeff Roberson #define vm_reserv_trylock(rv) mtx_trylock(vm_reserv_lockptr(rv)) 1795c930c89SJeff Roberson #define vm_reserv_unlock(rv) mtx_unlock(vm_reserv_lockptr(rv)) 1805c930c89SJeff Roberson 181f8a47341SAlan Cox /* 182f8a47341SAlan Cox * The reservation array 183f8a47341SAlan Cox * 184f8a47341SAlan Cox * This array is analoguous in function to vm_page_array. It differs in the 185f8a47341SAlan Cox * respect that it may contain a greater number of useful reservation 186f8a47341SAlan Cox * structures than there are (physical) superpages. These "invalid" 187f8a47341SAlan Cox * reservation structures exist to trade-off space for time in the 188f8a47341SAlan Cox * implementation of vm_reserv_from_page(). Invalid reservation structures are 189f8a47341SAlan Cox * distinguishable from "valid" reservation structures by inspecting the 190f8a47341SAlan Cox * reservation's "pages" field. Invalid reservation structures have a NULL 191f8a47341SAlan Cox * "pages" field. 192f8a47341SAlan Cox * 193f8a47341SAlan Cox * vm_reserv_from_page() maps a small (physical) page to an element of this 194f8a47341SAlan Cox * array by computing a physical reservation number from the page's physical 195f8a47341SAlan Cox * address. The physical reservation number is used as the array index. 196f8a47341SAlan Cox * 197f8a47341SAlan Cox * An "active" reservation is a valid reservation structure that has a non-NULL 198f8a47341SAlan Cox * "object" field and a non-zero "popcnt" field. In other words, every active 199f8a47341SAlan Cox * reservation belongs to a particular object. Moreover, every active 200f8a47341SAlan Cox * reservation has an entry in the containing object's list of reservations. 201f8a47341SAlan Cox */ 202f8a47341SAlan Cox static vm_reserv_t vm_reserv_array; 203f8a47341SAlan Cox 204f8a47341SAlan Cox /* 205fe6d5344SMark Johnston * The per-domain partially populated reservation queues 206f8a47341SAlan Cox * 207fe6d5344SMark Johnston * These queues enable the fast recovery of an unused free small page from a 208fe6d5344SMark Johnston * partially populated reservation. The reservation at the head of a queue 2093453bca8SAlan Cox * is the least recently changed, partially populated reservation. 210f8a47341SAlan Cox * 211fe6d5344SMark Johnston * Access to this queue is synchronized by the per-domain reservation lock. 212b378d296SMark Johnston * Threads reclaiming free pages from the queue must hold the per-domain scan 213b378d296SMark Johnston * lock. 214f8a47341SAlan Cox */ 215fe6d5344SMark Johnston struct vm_reserv_domain { 216fe6d5344SMark Johnston struct mtx lock; 217b378d296SMark Johnston struct vm_reserv_queue partpop; /* (d) */ 218b378d296SMark Johnston struct vm_reserv marker; /* (d, s) scan marker/lock */ 219fe6d5344SMark Johnston } __aligned(CACHE_LINE_SIZE); 220fe6d5344SMark Johnston 221fe6d5344SMark Johnston static struct vm_reserv_domain vm_rvd[MAXMEMDOM]; 222fe6d5344SMark Johnston 223fe6d5344SMark Johnston #define vm_reserv_domain_lockptr(d) (&vm_rvd[(d)].lock) 224b378d296SMark Johnston #define vm_reserv_domain_assert_locked(d) \ 225b378d296SMark Johnston mtx_assert(vm_reserv_domain_lockptr(d), MA_OWNED) 226fe6d5344SMark Johnston #define vm_reserv_domain_lock(d) mtx_lock(vm_reserv_domain_lockptr(d)) 227fe6d5344SMark Johnston #define vm_reserv_domain_unlock(d) mtx_unlock(vm_reserv_domain_lockptr(d)) 228f8a47341SAlan Cox 229b378d296SMark Johnston #define vm_reserv_domain_scan_lock(d) mtx_lock(&vm_rvd[(d)].marker.lock) 230b378d296SMark Johnston #define vm_reserv_domain_scan_unlock(d) mtx_unlock(&vm_rvd[(d)].marker.lock) 231b378d296SMark Johnston 2327029da5cSPawel Biernacki static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 2337029da5cSPawel Biernacki "Reservation Info"); 234f8a47341SAlan Cox 235d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(vm_reserv_broken); 2365c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD, 2375c930c89SJeff Roberson &vm_reserv_broken, "Cumulative number of broken reservations"); 238f8a47341SAlan Cox 239d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(vm_reserv_freed); 2405c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD, 2415c930c89SJeff Roberson &vm_reserv_freed, "Cumulative number of freed reservations"); 242f8a47341SAlan Cox 243e0a63baaSAlan Cox static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS); 244e0a63baaSAlan Cox 245a314aba8SMateusz Guzik SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RD, 246a314aba8SMateusz Guzik NULL, 0, sysctl_vm_reserv_fullpop, "I", "Current number of full reservations"); 247e0a63baaSAlan Cox 248f8a47341SAlan Cox static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS); 249f8a47341SAlan Cox 2507029da5cSPawel Biernacki SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, 251114484b7SMark Johnston CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 2527029da5cSPawel Biernacki sysctl_vm_reserv_partpopq, "A", 2537029da5cSPawel Biernacki "Partially populated reservation queues"); 254f8a47341SAlan Cox 255d869a17eSMark Johnston static COUNTER_U64_DEFINE_EARLY(vm_reserv_reclaimed); 2565c930c89SJeff Roberson SYSCTL_COUNTER_U64(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD, 2575c930c89SJeff Roberson &vm_reserv_reclaimed, "Cumulative number of reclaimed reservations"); 258f8a47341SAlan Cox 259e2068d0bSJeff Roberson /* 260e2068d0bSJeff Roberson * The object lock pool is used to synchronize the rvq. We can not use a 261e2068d0bSJeff Roberson * pool mutex because it is required before malloc works. 262e2068d0bSJeff Roberson * 263e2068d0bSJeff Roberson * The "hash" function could be made faster without divide and modulo. 264e2068d0bSJeff Roberson */ 265e2068d0bSJeff Roberson #define VM_RESERV_OBJ_LOCK_COUNT MAXCPU 266e2068d0bSJeff Roberson 267e2068d0bSJeff Roberson struct mtx_padalign vm_reserv_object_mtx[VM_RESERV_OBJ_LOCK_COUNT]; 268e2068d0bSJeff Roberson 269e2068d0bSJeff Roberson #define vm_reserv_object_lock_idx(object) \ 270e2068d0bSJeff Roberson (((uintptr_t)object / sizeof(*object)) % VM_RESERV_OBJ_LOCK_COUNT) 271e2068d0bSJeff Roberson #define vm_reserv_object_lock_ptr(object) \ 272e2068d0bSJeff Roberson &vm_reserv_object_mtx[vm_reserv_object_lock_idx((object))] 273e2068d0bSJeff Roberson #define vm_reserv_object_lock(object) \ 274e2068d0bSJeff Roberson mtx_lock(vm_reserv_object_lock_ptr((object))) 275e2068d0bSJeff Roberson #define vm_reserv_object_unlock(object) \ 276e2068d0bSJeff Roberson mtx_unlock(vm_reserv_object_lock_ptr((object))) 277e2068d0bSJeff Roberson 278ada27a3bSKonstantin Belousov static void vm_reserv_break(vm_reserv_t rv); 279ec179322SAlan Cox static void vm_reserv_depopulate(vm_reserv_t rv, int index); 280f8a47341SAlan Cox static vm_reserv_t vm_reserv_from_page(vm_page_t m); 281f8a47341SAlan Cox static boolean_t vm_reserv_has_pindex(vm_reserv_t rv, 282f8a47341SAlan Cox vm_pindex_t pindex); 283ec179322SAlan Cox static void vm_reserv_populate(vm_reserv_t rv, int index); 28444aab2c3SAlan Cox static void vm_reserv_reclaim(vm_reserv_t rv); 285f8a47341SAlan Cox 286f8a47341SAlan Cox /* 287e0a63baaSAlan Cox * Returns the current number of full reservations. 288e0a63baaSAlan Cox * 289fe6d5344SMark Johnston * Since the number of full reservations is computed without acquiring any 290fe6d5344SMark Johnston * locks, the returned value is inexact. 291e0a63baaSAlan Cox */ 292e0a63baaSAlan Cox static int 293e0a63baaSAlan Cox sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS) 294e0a63baaSAlan Cox { 295e0a63baaSAlan Cox vm_paddr_t paddr; 296e0a63baaSAlan Cox struct vm_phys_seg *seg; 297e0a63baaSAlan Cox vm_reserv_t rv; 298e0a63baaSAlan Cox int fullpop, segind; 299e0a63baaSAlan Cox 300e0a63baaSAlan Cox fullpop = 0; 301e0a63baaSAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 302e0a63baaSAlan Cox seg = &vm_phys_segs[segind]; 303e0a63baaSAlan Cox paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 3047988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 3057988971aSD Scott Phillips rv = seg->first_reserv + (paddr >> VM_LEVEL_0_SHIFT) - 3067988971aSD Scott Phillips (seg->start >> VM_LEVEL_0_SHIFT); 3077988971aSD Scott Phillips #else 3087988971aSD Scott Phillips rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT]; 3097988971aSD Scott Phillips #endif 3106b821a74SAleksandr Rybalko while (paddr + VM_LEVEL_0_SIZE > paddr && paddr + 3116b821a74SAleksandr Rybalko VM_LEVEL_0_SIZE <= seg->end) { 312e0a63baaSAlan Cox fullpop += rv->popcnt == VM_LEVEL_0_NPAGES; 313e0a63baaSAlan Cox paddr += VM_LEVEL_0_SIZE; 3147988971aSD Scott Phillips rv++; 315e0a63baaSAlan Cox } 316e0a63baaSAlan Cox } 317e0a63baaSAlan Cox return (sysctl_handle_int(oidp, &fullpop, 0, req)); 318e0a63baaSAlan Cox } 319e0a63baaSAlan Cox 320e0a63baaSAlan Cox /* 3213453bca8SAlan Cox * Describes the current state of the partially populated reservation queue. 322f8a47341SAlan Cox */ 323f8a47341SAlan Cox static int 324f8a47341SAlan Cox sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS) 325f8a47341SAlan Cox { 326f8a47341SAlan Cox struct sbuf sbuf; 327f8a47341SAlan Cox vm_reserv_t rv; 328ef435ae7SJeff Roberson int counter, error, domain, level, unused_pages; 329f8a47341SAlan Cox 33000f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 33100f0e671SMatthew D Fleming if (error != 0) 33200f0e671SMatthew D Fleming return (error); 3334e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 334ef435ae7SJeff Roberson sbuf_printf(&sbuf, "\nDOMAIN LEVEL SIZE NUMBER\n\n"); 335ef435ae7SJeff Roberson for (domain = 0; domain < vm_ndomains; domain++) { 336f8a47341SAlan Cox for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) { 337f8a47341SAlan Cox counter = 0; 338f8a47341SAlan Cox unused_pages = 0; 3395c930c89SJeff Roberson vm_reserv_domain_lock(domain); 340fe6d5344SMark Johnston TAILQ_FOREACH(rv, &vm_rvd[domain].partpop, partpopq) { 341b378d296SMark Johnston if (rv == &vm_rvd[domain].marker) 342b378d296SMark Johnston continue; 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 3865c930c89SJeff Roberson vm_reserv_assert_locked(rv); 3875c930c89SJeff Roberson CTR6(KTR_VM, 3885c930c89SJeff Roberson "%s: rv %p(%p) object %p new %p popcnt %d", 3895c930c89SJeff Roberson __FUNCTION__, rv, rv->pages, rv->object, object, 3905c930c89SJeff Roberson rv->popcnt); 391e2068d0bSJeff Roberson KASSERT(rv->object == NULL, 392e2068d0bSJeff Roberson ("vm_reserv_insert: reserv %p isn't free", rv)); 393e2068d0bSJeff Roberson KASSERT(rv->popcnt == 0, 394e2068d0bSJeff Roberson ("vm_reserv_insert: reserv %p's popcnt is corrupted", rv)); 395e2068d0bSJeff Roberson KASSERT(!rv->inpartpopq, 396e2068d0bSJeff Roberson ("vm_reserv_insert: reserv %p's inpartpopq is TRUE", rv)); 39784e2ae64SDoug Moore KASSERT(bit_ntest(rv->popmap, 0, VM_LEVEL_0_NPAGES - 1, 0), 398e2068d0bSJeff Roberson ("vm_reserv_insert: reserv %p's popmap is corrupted", rv)); 399e2068d0bSJeff Roberson vm_reserv_object_lock(object); 400e2068d0bSJeff Roberson rv->pindex = pindex; 401e2068d0bSJeff Roberson rv->object = object; 4022ef6727eSJeff Roberson rv->lasttick = ticks; 403e2068d0bSJeff Roberson LIST_INSERT_HEAD(&object->rvq, rv, objq); 404e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 405e2068d0bSJeff Roberson } 406e2068d0bSJeff Roberson 4073e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 4083e00c11aSAlan Cox static inline bool 4093e00c11aSAlan Cox vm_reserv_is_sublevel_full(vm_reserv_t rv, int index) 4103e00c11aSAlan Cox { 4113e00c11aSAlan Cox _Static_assert(VM_SUBLEVEL_0_NPAGES == 16 || 4123e00c11aSAlan Cox VM_SUBLEVEL_0_NPAGES == 128, 4133e00c11aSAlan Cox "vm_reserv_is_sublevel_full: unsupported VM_SUBLEVEL_0_NPAGES"); 4143e00c11aSAlan Cox /* An equivalent bit_ntest() compiles to more instructions. */ 4153e00c11aSAlan Cox switch (VM_SUBLEVEL_0_NPAGES) { 4163e00c11aSAlan Cox case 16: 4173e00c11aSAlan Cox return (((uint16_t *)rv->popmap)[index / 16] == UINT16_MAX); 4183e00c11aSAlan Cox case 128: 4193e00c11aSAlan Cox index = rounddown2(index, 128) / 64; 4203e00c11aSAlan Cox return (((uint64_t *)rv->popmap)[index] == UINT64_MAX && 4213e00c11aSAlan Cox ((uint64_t *)rv->popmap)[index + 1] == UINT64_MAX); 4223e00c11aSAlan Cox default: 4233e00c11aSAlan Cox __unreachable(); 4243e00c11aSAlan Cox } 4253e00c11aSAlan Cox } 4263e00c11aSAlan Cox #endif 4273e00c11aSAlan Cox 428e2068d0bSJeff Roberson /* 429f8a47341SAlan Cox * Reduces the given reservation's population count. If the population count 430f8a47341SAlan Cox * becomes zero, the reservation is destroyed. Additionally, moves the 4313453bca8SAlan Cox * reservation to the tail of the partially populated reservation queue if the 432f8a47341SAlan Cox * population count is non-zero. 433f8a47341SAlan Cox */ 434f8a47341SAlan Cox static void 435ec179322SAlan Cox vm_reserv_depopulate(vm_reserv_t rv, int index) 436f8a47341SAlan Cox { 4375c930c89SJeff Roberson struct vm_domain *vmd; 438f8a47341SAlan Cox 4395c930c89SJeff Roberson vm_reserv_assert_locked(rv); 4405c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 4415c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 442f8a47341SAlan Cox KASSERT(rv->object != NULL, 443f8a47341SAlan Cox ("vm_reserv_depopulate: reserv %p is free", rv)); 44484e2ae64SDoug Moore KASSERT(bit_test(rv->popmap, index), 445a08c1515SAlan Cox ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv, 446a08c1515SAlan Cox index)); 447f8a47341SAlan Cox KASSERT(rv->popcnt > 0, 448f8a47341SAlan Cox ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv)); 4492d3f4181SJeff Roberson KASSERT(rv->domain < vm_ndomains, 450ef435ae7SJeff Roberson ("vm_reserv_depopulate: reserv %p's domain is corrupted %d", 451ef435ae7SJeff Roberson rv, rv->domain)); 4525c930c89SJeff Roberson if (rv->popcnt == VM_LEVEL_0_NPAGES) { 4533e00c11aSAlan Cox KASSERT(rv->pages->psind == VM_LEVEL_0_PSIND, 454dd05fa19SAlan Cox ("vm_reserv_depopulate: reserv %p is already demoted", 455dd05fa19SAlan Cox rv)); 4563e00c11aSAlan Cox rv->pages->psind = VM_LEVEL_0_PSIND - 1; 457f8a47341SAlan Cox } 4583e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 4593e00c11aSAlan Cox if (vm_reserv_is_sublevel_full(rv, index)) 4603e00c11aSAlan Cox rv->pages[rounddown2(index, VM_SUBLEVEL_0_NPAGES)].psind = 0; 4613e00c11aSAlan Cox #endif 46284e2ae64SDoug Moore bit_clear(rv->popmap, index); 463f8a47341SAlan Cox rv->popcnt--; 4642ef6727eSJeff Roberson if ((unsigned)(ticks - rv->lasttick) >= PARTPOPSLOP || 4652ef6727eSJeff Roberson rv->popcnt == 0) { 4665c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 4675c930c89SJeff Roberson if (rv->inpartpopq) { 468fe6d5344SMark Johnston TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq); 4695c930c89SJeff Roberson rv->inpartpopq = FALSE; 4705c930c89SJeff Roberson } 4715c930c89SJeff Roberson if (rv->popcnt != 0) { 472f8a47341SAlan Cox rv->inpartpopq = TRUE; 473fe6d5344SMark Johnston TAILQ_INSERT_TAIL(&vm_rvd[rv->domain].partpop, rv, 474fe6d5344SMark Johnston partpopq); 475f8a47341SAlan Cox } 4765c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 4772ef6727eSJeff Roberson rv->lasttick = ticks; 4782ef6727eSJeff Roberson } 4795c930c89SJeff Roberson vmd = VM_DOMAIN(rv->domain); 4805c930c89SJeff Roberson if (rv->popcnt == 0) { 4815c930c89SJeff Roberson vm_reserv_remove(rv); 4825c930c89SJeff Roberson vm_domain_free_lock(vmd); 483*ee511f83SDoug Moore vm_phys_free_pages(rv->pages, VM_FREEPOOL_DEFAULT, 484*ee511f83SDoug Moore VM_LEVEL_0_ORDER); 4855c930c89SJeff Roberson vm_domain_free_unlock(vmd); 4865c930c89SJeff Roberson counter_u64_add(vm_reserv_freed, 1); 4875c930c89SJeff Roberson } 4885c930c89SJeff Roberson vm_domain_freecnt_inc(vmd, 1); 489f8a47341SAlan Cox } 490f8a47341SAlan Cox 491f8a47341SAlan Cox /* 492f8a47341SAlan Cox * Returns the reservation to which the given page might belong. 493f8a47341SAlan Cox */ 494f8a47341SAlan Cox static __inline vm_reserv_t 495f8a47341SAlan Cox vm_reserv_from_page(vm_page_t m) 496f8a47341SAlan Cox { 4977988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 4987988971aSD Scott Phillips struct vm_phys_seg *seg; 499f8a47341SAlan Cox 5007988971aSD Scott Phillips seg = &vm_phys_segs[m->segind]; 5017988971aSD Scott Phillips return (seg->first_reserv + (VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT) - 5027988971aSD Scott Phillips (seg->start >> VM_LEVEL_0_SHIFT)); 5037988971aSD Scott Phillips #else 504f8a47341SAlan Cox return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]); 5057988971aSD Scott Phillips #endif 506f8a47341SAlan Cox } 507f8a47341SAlan Cox 508f8a47341SAlan Cox /* 509e2068d0bSJeff Roberson * Returns an existing reservation or NULL and initialized successor pointer. 510e2068d0bSJeff Roberson */ 511e2068d0bSJeff Roberson static vm_reserv_t 512e2068d0bSJeff Roberson vm_reserv_from_object(vm_object_t object, vm_pindex_t pindex, 513e2068d0bSJeff Roberson vm_page_t mpred, vm_page_t *msuccp) 514e2068d0bSJeff Roberson { 515e2068d0bSJeff Roberson vm_reserv_t rv; 516e2068d0bSJeff Roberson vm_page_t msucc; 517e2068d0bSJeff Roberson 518e2068d0bSJeff Roberson msucc = NULL; 519e2068d0bSJeff Roberson if (mpred != NULL) { 520e2068d0bSJeff Roberson KASSERT(mpred->object == object, 521e2068d0bSJeff Roberson ("vm_reserv_from_object: object doesn't contain mpred")); 522e2068d0bSJeff Roberson KASSERT(mpred->pindex < pindex, 523e2068d0bSJeff Roberson ("vm_reserv_from_object: mpred doesn't precede pindex")); 524e2068d0bSJeff Roberson rv = vm_reserv_from_page(mpred); 525e2068d0bSJeff Roberson if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 526e2068d0bSJeff Roberson goto found; 527e2068d0bSJeff Roberson msucc = TAILQ_NEXT(mpred, listq); 528e2068d0bSJeff Roberson } else 529e2068d0bSJeff Roberson msucc = TAILQ_FIRST(&object->memq); 530e2068d0bSJeff Roberson if (msucc != NULL) { 531e2068d0bSJeff Roberson KASSERT(msucc->pindex > pindex, 532e2068d0bSJeff Roberson ("vm_reserv_from_object: msucc doesn't succeed pindex")); 533e2068d0bSJeff Roberson rv = vm_reserv_from_page(msucc); 534e2068d0bSJeff Roberson if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 535e2068d0bSJeff Roberson goto found; 536e2068d0bSJeff Roberson } 537e2068d0bSJeff Roberson rv = NULL; 538e2068d0bSJeff Roberson 539e2068d0bSJeff Roberson found: 540e2068d0bSJeff Roberson *msuccp = msucc; 541e2068d0bSJeff Roberson 542e2068d0bSJeff Roberson return (rv); 543e2068d0bSJeff Roberson } 544e2068d0bSJeff Roberson 545e2068d0bSJeff Roberson /* 546f8a47341SAlan Cox * Returns TRUE if the given reservation contains the given page index and 547f8a47341SAlan Cox * FALSE otherwise. 548f8a47341SAlan Cox */ 549f8a47341SAlan Cox static __inline boolean_t 550f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex) 551f8a47341SAlan Cox { 552f8a47341SAlan Cox 553f8a47341SAlan Cox return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0); 554f8a47341SAlan Cox } 555f8a47341SAlan Cox 556f8a47341SAlan Cox /* 557f8a47341SAlan Cox * Increases the given reservation's population count. Moves the reservation 5583453bca8SAlan Cox * to the tail of the partially populated reservation queue. 559f8a47341SAlan Cox */ 560f8a47341SAlan Cox static void 561ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index) 562f8a47341SAlan Cox { 563f8a47341SAlan Cox 5645c930c89SJeff Roberson vm_reserv_assert_locked(rv); 5655c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 5665c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 567f8a47341SAlan Cox KASSERT(rv->object != NULL, 568f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is free", rv)); 56984e2ae64SDoug Moore KASSERT(!bit_test(rv->popmap, index), 570a08c1515SAlan Cox ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv, 571a08c1515SAlan Cox index)); 572f8a47341SAlan Cox KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES, 573f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is already full", rv)); 5743e00c11aSAlan Cox KASSERT(rv->pages->psind >= 0 && 5753e00c11aSAlan Cox rv->pages->psind < VM_LEVEL_0_PSIND, 576dd05fa19SAlan Cox ("vm_reserv_populate: reserv %p is already promoted", rv)); 5772d3f4181SJeff Roberson KASSERT(rv->domain < vm_ndomains, 578ef435ae7SJeff Roberson ("vm_reserv_populate: reserv %p's domain is corrupted %d", 579ef435ae7SJeff Roberson rv, rv->domain)); 58084e2ae64SDoug Moore bit_set(rv->popmap, index); 5813e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 5823e00c11aSAlan Cox if (vm_reserv_is_sublevel_full(rv, index)) 5833e00c11aSAlan Cox rv->pages[rounddown2(index, VM_SUBLEVEL_0_NPAGES)].psind = 1; 5843e00c11aSAlan Cox #endif 5855c930c89SJeff Roberson rv->popcnt++; 5862ef6727eSJeff Roberson if ((unsigned)(ticks - rv->lasttick) < PARTPOPSLOP && 5872ef6727eSJeff Roberson rv->inpartpopq && rv->popcnt != VM_LEVEL_0_NPAGES) 5882ef6727eSJeff Roberson return; 5892ef6727eSJeff Roberson rv->lasttick = ticks; 5905c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 591f8a47341SAlan Cox if (rv->inpartpopq) { 592fe6d5344SMark Johnston TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq); 593f8a47341SAlan Cox rv->inpartpopq = FALSE; 594f8a47341SAlan Cox } 595f8a47341SAlan Cox if (rv->popcnt < VM_LEVEL_0_NPAGES) { 596f8a47341SAlan Cox rv->inpartpopq = TRUE; 597fe6d5344SMark Johnston TAILQ_INSERT_TAIL(&vm_rvd[rv->domain].partpop, rv, partpopq); 5985c930c89SJeff Roberson } else { 5993e00c11aSAlan Cox KASSERT(rv->pages->psind == VM_LEVEL_0_PSIND - 1, 6005c930c89SJeff Roberson ("vm_reserv_populate: reserv %p is already promoted", 6015c930c89SJeff Roberson rv)); 6023e00c11aSAlan Cox rv->pages->psind = VM_LEVEL_0_PSIND; 603f8a47341SAlan Cox } 6045c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 6055c930c89SJeff Roberson } 606f8a47341SAlan Cox 607f8a47341SAlan Cox /* 608e2068d0bSJeff Roberson * Allocates a contiguous set of physical pages of the given size "npages" 6092d5039dbSAlan Cox * from existing or newly created reservations. All of the physical pages 610e2068d0bSJeff Roberson * must be at or above the given physical address "low" and below the given 611e2068d0bSJeff Roberson * physical address "high". The given value "alignment" determines the 612e2068d0bSJeff Roberson * alignment of the first physical page in the set. If the given value 613e2068d0bSJeff Roberson * "boundary" is non-zero, then the set of physical pages cannot cross any 614e2068d0bSJeff Roberson * physical address boundary that is a multiple of that value. Both 615e2068d0bSJeff Roberson * "alignment" and "boundary" must be a power of two. 616e2068d0bSJeff Roberson * 617e2068d0bSJeff Roberson * The page "mpred" must immediately precede the offset "pindex" within the 618e2068d0bSJeff Roberson * specified object. 619e2068d0bSJeff Roberson * 6202d5039dbSAlan Cox * The object must be locked. 621e2068d0bSJeff Roberson */ 622e2068d0bSJeff Roberson vm_page_t 6232d5039dbSAlan Cox vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, int domain, 6242d5039dbSAlan Cox int req, vm_page_t mpred, u_long npages, vm_paddr_t low, vm_paddr_t high, 6252d5039dbSAlan Cox u_long alignment, vm_paddr_t boundary) 626c68c3537SAlan Cox { 6275c930c89SJeff Roberson struct vm_domain *vmd; 628c68c3537SAlan Cox vm_paddr_t pa, size; 629920da7e4SAlan Cox vm_page_t m, m_ret, msucc; 630c68c3537SAlan Cox vm_pindex_t first, leftcap, rightcap; 631c68c3537SAlan Cox vm_reserv_t rv; 632c68c3537SAlan Cox u_long allocpages, maxpages, minpages; 633c68c3537SAlan Cox int i, index, n; 634c68c3537SAlan Cox 63589f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 636c68c3537SAlan Cox KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0")); 637c68c3537SAlan Cox 638c68c3537SAlan Cox /* 639c68c3537SAlan Cox * Is a reservation fundamentally impossible? 640c68c3537SAlan Cox */ 641c68c3537SAlan Cox if (pindex < VM_RESERV_INDEX(object, pindex) || 642c68c3537SAlan Cox pindex + npages > object->size) 643c68c3537SAlan Cox return (NULL); 644c68c3537SAlan Cox 645c68c3537SAlan Cox /* 646c68c3537SAlan Cox * All reservations of a particular size have the same alignment. 647c68c3537SAlan Cox * Assuming that the first page is allocated from a reservation, the 648c68c3537SAlan Cox * least significant bits of its physical address can be determined 649c68c3537SAlan Cox * from its offset from the beginning of the reservation and the size 650c68c3537SAlan Cox * of the reservation. 651c68c3537SAlan Cox * 652c68c3537SAlan Cox * Could the specified index within a reservation of the smallest 653c68c3537SAlan Cox * possible size satisfy the alignment and boundary requirements? 654c68c3537SAlan Cox */ 655c68c3537SAlan Cox pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT; 656c68c3537SAlan Cox size = npages << PAGE_SHIFT; 657c606ab59SDoug Moore if (!vm_addr_ok(pa, size, alignment, boundary)) 658c68c3537SAlan Cox return (NULL); 659c68c3537SAlan Cox 660c68c3537SAlan Cox /* 6612d5039dbSAlan Cox * Look for an existing reservation. 662c68c3537SAlan Cox */ 663e2068d0bSJeff Roberson rv = vm_reserv_from_object(object, pindex, mpred, &msucc); 6642d5039dbSAlan Cox if (rv != NULL) { 6652d5039dbSAlan Cox KASSERT(object != kernel_object || rv->domain == domain, 6662d5039dbSAlan Cox ("vm_reserv_alloc_contig: domain mismatch")); 6672d5039dbSAlan Cox index = VM_RESERV_INDEX(object, pindex); 6682d5039dbSAlan Cox /* Does the allocation fit within the reservation? */ 6692d5039dbSAlan Cox if (index + npages > VM_LEVEL_0_NPAGES) 670e2068d0bSJeff Roberson return (NULL); 6712d5039dbSAlan Cox domain = rv->domain; 6722d5039dbSAlan Cox vmd = VM_DOMAIN(domain); 6732d5039dbSAlan Cox vm_reserv_lock(rv); 6742d5039dbSAlan Cox /* Handle reclaim race. */ 6752d5039dbSAlan Cox if (rv->object != object) 6762d5039dbSAlan Cox goto out; 6772d5039dbSAlan Cox m = &rv->pages[index]; 6782d5039dbSAlan Cox pa = VM_PAGE_TO_PHYS(m); 6792d5039dbSAlan Cox if (pa < low || pa + size > high || 680c606ab59SDoug Moore !vm_addr_ok(pa, size, alignment, boundary)) 6812d5039dbSAlan Cox goto out; 682c296ac7eSAlan Cox /* Handle vm_page_iter_rename(..., m, new_object, ...). */ 68384e2ae64SDoug Moore if (!bit_ntest(rv->popmap, index, index + npages - 1, 0)) 6842d5039dbSAlan Cox goto out; 6852d5039dbSAlan Cox if (!vm_domain_allocate(vmd, req, npages)) 6862d5039dbSAlan Cox goto out; 6872d5039dbSAlan Cox for (i = 0; i < npages; i++) 6882d5039dbSAlan Cox vm_reserv_populate(rv, index + i); 6892d5039dbSAlan Cox vm_reserv_unlock(rv); 6902d5039dbSAlan Cox return (m); 6912d5039dbSAlan Cox out: 6922d5039dbSAlan Cox vm_reserv_unlock(rv); 6932d5039dbSAlan Cox return (NULL); 6942d5039dbSAlan Cox } 695c68c3537SAlan Cox 696c68c3537SAlan Cox /* 697c68c3537SAlan Cox * Could at least one reservation fit between the first index to the 69864f096eeSAlan Cox * left that can be used ("leftcap") and the first index to the right 69964f096eeSAlan Cox * that cannot be used ("rightcap")? 700e2068d0bSJeff Roberson * 701e2068d0bSJeff Roberson * We must synchronize with the reserv object lock to protect the 702e2068d0bSJeff Roberson * pindex/object of the resulting reservations against rename while 703e2068d0bSJeff Roberson * we are inspecting. 704c68c3537SAlan Cox */ 705c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 706e2068d0bSJeff Roberson minpages = VM_RESERV_INDEX(object, pindex) + npages; 707e2068d0bSJeff Roberson maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES); 708e2068d0bSJeff Roberson allocpages = maxpages; 709e2068d0bSJeff Roberson vm_reserv_object_lock(object); 710c68c3537SAlan Cox if (mpred != NULL) { 711c68c3537SAlan Cox if ((rv = vm_reserv_from_page(mpred))->object != object) 712c68c3537SAlan Cox leftcap = mpred->pindex + 1; 713c68c3537SAlan Cox else 714c68c3537SAlan Cox leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 715e2068d0bSJeff Roberson if (leftcap > first) { 716e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 717c68c3537SAlan Cox return (NULL); 718c68c3537SAlan Cox } 719e2068d0bSJeff Roberson } 720c68c3537SAlan Cox if (msucc != NULL) { 721c68c3537SAlan Cox if ((rv = vm_reserv_from_page(msucc))->object != object) 722c68c3537SAlan Cox rightcap = msucc->pindex; 723c68c3537SAlan Cox else 724c68c3537SAlan Cox rightcap = rv->pindex; 725c68c3537SAlan Cox if (first + maxpages > rightcap) { 726e2068d0bSJeff Roberson if (maxpages == VM_LEVEL_0_NPAGES) { 727e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 728c68c3537SAlan Cox return (NULL); 729e2068d0bSJeff Roberson } 73064f096eeSAlan Cox 73164f096eeSAlan Cox /* 73264f096eeSAlan Cox * At least one reservation will fit between "leftcap" 73364f096eeSAlan Cox * and "rightcap". However, a reservation for the 73464f096eeSAlan Cox * last of the requested pages will not fit. Reduce 73564f096eeSAlan Cox * the size of the upcoming allocation accordingly. 73664f096eeSAlan Cox */ 737c68c3537SAlan Cox allocpages = minpages; 738c68c3537SAlan Cox } 739c68c3537SAlan Cox } 740e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 741c68c3537SAlan Cox 742c68c3537SAlan Cox /* 743c68c3537SAlan Cox * Would the last new reservation extend past the end of the object? 74463967687SJeff Roberson * 74563967687SJeff Roberson * If the object is unlikely to grow don't allocate a reservation for 74663967687SJeff Roberson * the tail. 747c68c3537SAlan Cox */ 74863967687SJeff Roberson if ((object->flags & OBJ_ANON) == 0 && 74963967687SJeff Roberson first + maxpages > object->size) { 750c68c3537SAlan Cox if (maxpages == VM_LEVEL_0_NPAGES) 751c68c3537SAlan Cox return (NULL); 752c68c3537SAlan Cox allocpages = minpages; 753c68c3537SAlan Cox } 754c68c3537SAlan Cox 755c68c3537SAlan Cox /* 75664f096eeSAlan Cox * Allocate the physical pages. The alignment and boundary specified 75764f096eeSAlan Cox * for this allocation may be different from the alignment and 75864f096eeSAlan Cox * boundary specified for the requested pages. For instance, the 75964f096eeSAlan Cox * specified index may not be the first page within the first new 76064f096eeSAlan Cox * reservation. 761c68c3537SAlan Cox */ 7625c930c89SJeff Roberson m = NULL; 7635c930c89SJeff Roberson vmd = VM_DOMAIN(domain); 7645c930c89SJeff Roberson if (vm_domain_allocate(vmd, req, npages)) { 7655c930c89SJeff Roberson vm_domain_free_lock(vmd); 7665c930c89SJeff Roberson m = vm_phys_alloc_contig(domain, allocpages, low, high, 7675c930c89SJeff Roberson ulmax(alignment, VM_LEVEL_0_SIZE), 7685c930c89SJeff Roberson boundary > VM_LEVEL_0_SIZE ? boundary : 0); 7695c930c89SJeff Roberson vm_domain_free_unlock(vmd); 7705c930c89SJeff Roberson if (m == NULL) { 7715c930c89SJeff Roberson vm_domain_freecnt_inc(vmd, npages); 7725c930c89SJeff Roberson return (NULL); 7735c930c89SJeff Roberson } 7745c930c89SJeff Roberson } else 775c68c3537SAlan Cox return (NULL); 776431fb8abSMark Johnston KASSERT(vm_page_domain(m) == domain, 7777a469c8eSJeff Roberson ("vm_reserv_alloc_contig: Page domain does not match requested.")); 77864f096eeSAlan Cox 77964f096eeSAlan Cox /* 78064f096eeSAlan Cox * The allocated physical pages always begin at a reservation 78164f096eeSAlan Cox * boundary, but they do not always end at a reservation boundary. 78264f096eeSAlan Cox * Initialize every reservation that is completely covered by the 78364f096eeSAlan Cox * allocated physical pages. 78464f096eeSAlan Cox */ 785c68c3537SAlan Cox m_ret = NULL; 786c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 787c68c3537SAlan Cox do { 788c68c3537SAlan Cox rv = vm_reserv_from_page(m); 789c68c3537SAlan Cox KASSERT(rv->pages == m, 790c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p's pages is corrupted", 791c68c3537SAlan Cox rv)); 7925c930c89SJeff Roberson vm_reserv_lock(rv); 793e2068d0bSJeff Roberson vm_reserv_insert(rv, object, first); 794c68c3537SAlan Cox n = ulmin(VM_LEVEL_0_NPAGES - index, npages); 795c68c3537SAlan Cox for (i = 0; i < n; i++) 796ec179322SAlan Cox vm_reserv_populate(rv, index + i); 797c68c3537SAlan Cox npages -= n; 798c68c3537SAlan Cox if (m_ret == NULL) { 799c68c3537SAlan Cox m_ret = &rv->pages[index]; 800c68c3537SAlan Cox index = 0; 801c68c3537SAlan Cox } 8025c930c89SJeff Roberson vm_reserv_unlock(rv); 803c68c3537SAlan Cox m += VM_LEVEL_0_NPAGES; 804c68c3537SAlan Cox first += VM_LEVEL_0_NPAGES; 805c68c3537SAlan Cox allocpages -= VM_LEVEL_0_NPAGES; 80664f096eeSAlan Cox } while (allocpages >= VM_LEVEL_0_NPAGES); 807c68c3537SAlan Cox return (m_ret); 808e2068d0bSJeff Roberson } 809c68c3537SAlan Cox 810c68c3537SAlan Cox /* 8112d5039dbSAlan Cox * Allocate a physical page from an existing or newly created reservation. 812e2068d0bSJeff Roberson * 813e2068d0bSJeff Roberson * The page "mpred" must immediately precede the offset "pindex" within the 814e2068d0bSJeff Roberson * specified object. 815e2068d0bSJeff Roberson * 816e2068d0bSJeff Roberson * The object must be locked. 817c68c3537SAlan Cox */ 818e2068d0bSJeff Roberson vm_page_t 8192d5039dbSAlan Cox vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, int domain, 8202d5039dbSAlan Cox int req, vm_page_t mpred) 821e2068d0bSJeff Roberson { 822e2068d0bSJeff Roberson struct vm_domain *vmd; 823e2068d0bSJeff Roberson vm_page_t m, msucc; 8242d5039dbSAlan Cox vm_pindex_t first, leftcap, rightcap; 825e2068d0bSJeff Roberson vm_reserv_t rv; 82630fbfddaSJeff Roberson int index; 827e2068d0bSJeff Roberson 828e2068d0bSJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 829e2068d0bSJeff Roberson 830e2068d0bSJeff Roberson /* 8312d5039dbSAlan Cox * Is a reservation fundamentally impossible? 832e2068d0bSJeff Roberson */ 833e2068d0bSJeff Roberson if (pindex < VM_RESERV_INDEX(object, pindex) || 8342d5039dbSAlan Cox pindex >= object->size) 835e2068d0bSJeff Roberson return (NULL); 836e2068d0bSJeff Roberson 837e2068d0bSJeff Roberson /* 838e2068d0bSJeff Roberson * Look for an existing reservation. 839e2068d0bSJeff Roberson */ 840e2068d0bSJeff Roberson rv = vm_reserv_from_object(object, pindex, mpred, &msucc); 8412d5039dbSAlan Cox if (rv != NULL) { 842e2068d0bSJeff Roberson KASSERT(object != kernel_object || rv->domain == domain, 8432d5039dbSAlan Cox ("vm_reserv_alloc_page: domain mismatch")); 844e2068d0bSJeff Roberson domain = rv->domain; 845e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 846c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 847c68c3537SAlan Cox m = &rv->pages[index]; 8485c930c89SJeff Roberson vm_reserv_lock(rv); 849e2068d0bSJeff Roberson /* Handle reclaim race. */ 8505c930c89SJeff Roberson if (rv->object != object || 851c296ac7eSAlan Cox /* Handle vm_page_iter_rename(..., m, new_object, ...). */ 85284e2ae64SDoug Moore bit_test(rv->popmap, index)) { 853e2068d0bSJeff Roberson m = NULL; 8545c930c89SJeff Roberson goto out; 85530fbfddaSJeff Roberson } 8565c930c89SJeff Roberson if (vm_domain_allocate(vmd, req, 1) == 0) 8575c930c89SJeff Roberson m = NULL; 8585c930c89SJeff Roberson else 8595c930c89SJeff Roberson vm_reserv_populate(rv, index); 8605c930c89SJeff Roberson out: 8615c930c89SJeff Roberson vm_reserv_unlock(rv); 862c68c3537SAlan Cox return (m); 863c68c3537SAlan Cox } 864c68c3537SAlan Cox 865c68c3537SAlan Cox /* 866c68c3537SAlan Cox * Could a reservation fit between the first index to the left that 867c68c3537SAlan Cox * can be used and the first index to the right that cannot be used? 868e2068d0bSJeff Roberson * 869e2068d0bSJeff Roberson * We must synchronize with the reserv object lock to protect the 870e2068d0bSJeff Roberson * pindex/object of the resulting reservations against rename while 871e2068d0bSJeff Roberson * we are inspecting. 872f8a47341SAlan Cox */ 873c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 874e2068d0bSJeff Roberson vm_reserv_object_lock(object); 875c68c3537SAlan Cox if (mpred != NULL) { 876c68c3537SAlan Cox if ((rv = vm_reserv_from_page(mpred))->object != object) 877f8a47341SAlan Cox leftcap = mpred->pindex + 1; 878f8a47341SAlan Cox else 879f8a47341SAlan Cox leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 880e2068d0bSJeff Roberson if (leftcap > first) { 881e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 882c68c3537SAlan Cox return (NULL); 883c68c3537SAlan Cox } 884e2068d0bSJeff Roberson } 885c68c3537SAlan Cox if (msucc != NULL) { 886c68c3537SAlan Cox if ((rv = vm_reserv_from_page(msucc))->object != object) 887f8a47341SAlan Cox rightcap = msucc->pindex; 888f8a47341SAlan Cox else 889f8a47341SAlan Cox rightcap = rv->pindex; 890e2068d0bSJeff Roberson if (first + VM_LEVEL_0_NPAGES > rightcap) { 891e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 892f8a47341SAlan Cox return (NULL); 893c68c3537SAlan Cox } 894e2068d0bSJeff Roberson } 895e2068d0bSJeff Roberson vm_reserv_object_unlock(object); 896f8a47341SAlan Cox 897f8a47341SAlan Cox /* 89863967687SJeff Roberson * Would the last new reservation extend past the end of the object? 89963967687SJeff Roberson * 90063967687SJeff Roberson * If the object is unlikely to grow don't allocate a reservation for 90163967687SJeff Roberson * the tail. 902f8a47341SAlan Cox */ 90363967687SJeff Roberson if ((object->flags & OBJ_ANON) == 0 && 90463967687SJeff Roberson first + VM_LEVEL_0_NPAGES > object->size) 905f8a47341SAlan Cox return (NULL); 906f8a47341SAlan Cox 907f8a47341SAlan Cox /* 908c68c3537SAlan Cox * Allocate and populate the new reservation. 909f8a47341SAlan Cox */ 9105c930c89SJeff Roberson m = NULL; 9115c930c89SJeff Roberson vmd = VM_DOMAIN(domain); 9125c930c89SJeff Roberson if (vm_domain_allocate(vmd, req, 1)) { 9135c930c89SJeff Roberson vm_domain_free_lock(vmd); 9145c930c89SJeff Roberson m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT, 9155c930c89SJeff Roberson VM_LEVEL_0_ORDER); 9165c930c89SJeff Roberson vm_domain_free_unlock(vmd); 9175c930c89SJeff Roberson if (m == NULL) { 9185c930c89SJeff Roberson vm_domain_freecnt_inc(vmd, 1); 9195c930c89SJeff Roberson return (NULL); 9205c930c89SJeff Roberson } 9215c930c89SJeff Roberson } else 922c68c3537SAlan Cox return (NULL); 923f8a47341SAlan Cox rv = vm_reserv_from_page(m); 9245c930c89SJeff Roberson vm_reserv_lock(rv); 925f8a47341SAlan Cox KASSERT(rv->pages == m, 926c68c3537SAlan Cox ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv)); 927e2068d0bSJeff Roberson vm_reserv_insert(rv, object, first); 928ec179322SAlan Cox index = VM_RESERV_INDEX(object, pindex); 929ec179322SAlan Cox vm_reserv_populate(rv, index); 9305c930c89SJeff Roberson vm_reserv_unlock(rv); 9315c930c89SJeff Roberson 932ec179322SAlan Cox return (&rv->pages[index]); 933f8a47341SAlan Cox } 934f8a47341SAlan Cox 935f8a47341SAlan Cox /* 936ada27a3bSKonstantin Belousov * Breaks the given reservation. All free pages in the reservation 937ada27a3bSKonstantin Belousov * are returned to the physical memory allocator. The reservation's 938ada27a3bSKonstantin Belousov * population count and map are reset to their initial state. 939ec179322SAlan Cox * 9403453bca8SAlan Cox * The given reservation must not be in the partially populated reservation 941fe6d5344SMark Johnston * queue. 942ec179322SAlan Cox */ 943ec179322SAlan Cox static void 944ada27a3bSKonstantin Belousov vm_reserv_break(vm_reserv_t rv) 945ec179322SAlan Cox { 9463e00c11aSAlan Cox vm_page_t m; 9470078df5fSDoug Moore int pos, pos0, pos1; 948ec179322SAlan Cox 9495c930c89SJeff Roberson vm_reserv_assert_locked(rv); 9505c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 9515c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 952e2068d0bSJeff Roberson vm_reserv_remove(rv); 9533e00c11aSAlan Cox m = rv->pages; 9543e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 9553e00c11aSAlan Cox for (; m < rv->pages + VM_LEVEL_0_NPAGES; m += VM_SUBLEVEL_0_NPAGES) 9563e00c11aSAlan Cox #endif 9573e00c11aSAlan Cox m->psind = 0; 9580078df5fSDoug Moore pos0 = bit_test(rv->popmap, 0) ? -1 : 0; 9590078df5fSDoug Moore pos1 = -1 - pos0; 9600078df5fSDoug Moore for (pos = 0; pos < VM_LEVEL_0_NPAGES; ) { 9610078df5fSDoug Moore /* Find the first different bit after pos. */ 9620078df5fSDoug Moore bit_ff_at(rv->popmap, pos + 1, VM_LEVEL_0_NPAGES, 9630078df5fSDoug Moore pos1 < pos0, &pos); 96418c47eabSDoug Moore if (pos == -1) 96518c47eabSDoug Moore pos = VM_LEVEL_0_NPAGES; 9660078df5fSDoug Moore if (pos0 < pos1) { 9670078df5fSDoug Moore pos0 = pos; 9680078df5fSDoug Moore continue; 9690078df5fSDoug Moore } 9700078df5fSDoug Moore /* Free unused pages from pos0 to pos. */ 9710078df5fSDoug Moore pos1 = pos; 9725c930c89SJeff Roberson vm_domain_free_lock(VM_DOMAIN(rv->domain)); 9730078df5fSDoug Moore vm_phys_enqueue_contig(&rv->pages[pos0], VM_FREEPOOL_DEFAULT, 9740078df5fSDoug Moore pos1 - pos0); 9755c930c89SJeff Roberson vm_domain_free_unlock(VM_DOMAIN(rv->domain)); 976e67a5068SDoug Moore } 97784e2ae64SDoug Moore bit_nclear(rv->popmap, 0, VM_LEVEL_0_NPAGES - 1); 978e67a5068SDoug Moore rv->popcnt = 0; 9795c930c89SJeff Roberson counter_u64_add(vm_reserv_broken, 1); 980ec179322SAlan Cox } 981ec179322SAlan Cox 982ec179322SAlan Cox /* 983f8a47341SAlan Cox * Breaks all reservations belonging to the given object. 984f8a47341SAlan Cox */ 985f8a47341SAlan Cox void 986f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object) 987f8a47341SAlan Cox { 988f8a47341SAlan Cox vm_reserv_t rv; 989f8a47341SAlan Cox 990e2068d0bSJeff Roberson /* 991e2068d0bSJeff Roberson * This access of object->rvq is unsynchronized so that the 992e2068d0bSJeff Roberson * object rvq lock can nest after the domain_free lock. We 993e2068d0bSJeff Roberson * must check for races in the results. However, the object 994e2068d0bSJeff Roberson * lock prevents new additions, so we are guaranteed that when 995e2068d0bSJeff Roberson * it returns NULL the object is properly empty. 996e2068d0bSJeff Roberson */ 997f8a47341SAlan Cox while ((rv = LIST_FIRST(&object->rvq)) != NULL) { 9985c930c89SJeff Roberson vm_reserv_lock(rv); 999e2068d0bSJeff Roberson /* Reclaim race. */ 10005c930c89SJeff Roberson if (rv->object != object) { 10015c930c89SJeff Roberson vm_reserv_unlock(rv); 1002e2068d0bSJeff Roberson continue; 10035c930c89SJeff Roberson } 10045c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 1005f8a47341SAlan Cox if (rv->inpartpopq) { 1006fe6d5344SMark Johnston TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq); 1007f8a47341SAlan Cox rv->inpartpopq = FALSE; 1008f8a47341SAlan Cox } 10095c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 1010ada27a3bSKonstantin Belousov vm_reserv_break(rv); 10115c930c89SJeff Roberson vm_reserv_unlock(rv); 1012f8a47341SAlan Cox } 1013f8a47341SAlan Cox } 1014f8a47341SAlan Cox 1015f8a47341SAlan Cox /* 1016f8a47341SAlan Cox * Frees the given page if it belongs to a reservation. Returns TRUE if the 1017f8a47341SAlan Cox * page is freed and FALSE otherwise. 1018f8a47341SAlan Cox */ 1019f8a47341SAlan Cox boolean_t 1020f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m) 1021f8a47341SAlan Cox { 1022f8a47341SAlan Cox vm_reserv_t rv; 10235c930c89SJeff Roberson boolean_t ret; 1024f8a47341SAlan Cox 1025f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1026908e3da1SAlan Cox if (rv->object == NULL) 1027908e3da1SAlan Cox return (FALSE); 10285c930c89SJeff Roberson vm_reserv_lock(rv); 10295c930c89SJeff Roberson /* Re-validate after lock. */ 10305c930c89SJeff Roberson if (rv->object != NULL) { 1031ec179322SAlan Cox vm_reserv_depopulate(rv, m - rv->pages); 10325c930c89SJeff Roberson ret = TRUE; 10335c930c89SJeff Roberson } else 10345c930c89SJeff Roberson ret = FALSE; 10355c930c89SJeff Roberson vm_reserv_unlock(rv); 10365c930c89SJeff Roberson 10375c930c89SJeff Roberson return (ret); 1038f8a47341SAlan Cox } 1039f8a47341SAlan Cox 1040f8a47341SAlan Cox /* 1041f8a47341SAlan Cox * Initializes the reservation management system. Specifically, initializes 1042f8a47341SAlan Cox * the reservation array. 1043f8a47341SAlan Cox * 1044f8a47341SAlan Cox * Requires that vm_page_array and first_page are initialized! 1045f8a47341SAlan Cox */ 1046f8a47341SAlan Cox void 1047f8a47341SAlan Cox vm_reserv_init(void) 1048f8a47341SAlan Cox { 1049f8a47341SAlan Cox vm_paddr_t paddr; 105009e5f3c4SAlan Cox struct vm_phys_seg *seg; 10515c930c89SJeff Roberson struct vm_reserv *rv; 1052b378d296SMark Johnston struct vm_reserv_domain *rvd; 10537988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 10547988971aSD Scott Phillips vm_pindex_t used; 10557988971aSD Scott Phillips #endif 105684e2ae64SDoug Moore int i, segind; 1057f8a47341SAlan Cox 1058f8a47341SAlan Cox /* 1059f8a47341SAlan Cox * Initialize the reservation array. Specifically, initialize the 1060f8a47341SAlan Cox * "pages" field for every element that has an underlying superpage. 1061f8a47341SAlan Cox */ 10627988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 10637988971aSD Scott Phillips used = 0; 10647988971aSD Scott Phillips #endif 106509e5f3c4SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 106609e5f3c4SAlan Cox seg = &vm_phys_segs[segind]; 10677988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 10687988971aSD Scott Phillips seg->first_reserv = &vm_reserv_array[used]; 10697988971aSD Scott Phillips used += howmany(seg->end, VM_LEVEL_0_SIZE) - 10707988971aSD Scott Phillips seg->start / VM_LEVEL_0_SIZE; 10717988971aSD Scott Phillips #else 10727988971aSD Scott Phillips seg->first_reserv = 10737988971aSD Scott Phillips &vm_reserv_array[seg->start >> VM_LEVEL_0_SHIFT]; 10747988971aSD Scott Phillips #endif 107509e5f3c4SAlan Cox paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 10767988971aSD Scott Phillips rv = seg->first_reserv + (paddr >> VM_LEVEL_0_SHIFT) - 10777988971aSD Scott Phillips (seg->start >> VM_LEVEL_0_SHIFT); 10786b821a74SAleksandr Rybalko while (paddr + VM_LEVEL_0_SIZE > paddr && paddr + 10796b821a74SAleksandr Rybalko VM_LEVEL_0_SIZE <= seg->end) { 10805c930c89SJeff Roberson rv->pages = PHYS_TO_VM_PAGE(paddr); 10815c930c89SJeff Roberson rv->domain = seg->domain; 10825c930c89SJeff Roberson mtx_init(&rv->lock, "vm reserv", NULL, MTX_DEF); 1083f8a47341SAlan Cox paddr += VM_LEVEL_0_SIZE; 10847988971aSD Scott Phillips rv++; 1085f8a47341SAlan Cox } 1086f8a47341SAlan Cox } 10875c930c89SJeff Roberson for (i = 0; i < MAXMEMDOM; i++) { 1088b378d296SMark Johnston rvd = &vm_rvd[i]; 1089b378d296SMark Johnston mtx_init(&rvd->lock, "vm reserv domain", NULL, MTX_DEF); 1090b378d296SMark Johnston TAILQ_INIT(&rvd->partpop); 1091b378d296SMark Johnston mtx_init(&rvd->marker.lock, "vm reserv marker", NULL, MTX_DEF); 1092b378d296SMark Johnston 1093b378d296SMark Johnston /* 1094b378d296SMark Johnston * Fully populated reservations should never be present in the 1095b378d296SMark Johnston * partially populated reservation queues. 1096b378d296SMark Johnston */ 1097b378d296SMark Johnston rvd->marker.popcnt = VM_LEVEL_0_NPAGES; 109884e2ae64SDoug Moore bit_nset(rvd->marker.popmap, 0, VM_LEVEL_0_NPAGES - 1); 1099f8a47341SAlan Cox } 1100f8a47341SAlan Cox 11015c930c89SJeff Roberson for (i = 0; i < VM_RESERV_OBJ_LOCK_COUNT; i++) 11025c930c89SJeff Roberson mtx_init(&vm_reserv_object_mtx[i], "resv obj lock", NULL, 11035c930c89SJeff Roberson MTX_DEF); 11045c930c89SJeff Roberson } 11055c930c89SJeff Roberson 1106f8a47341SAlan Cox /* 1107c869e672SAlan Cox * Returns true if the given page belongs to a reservation and that page is 1108c869e672SAlan Cox * free. Otherwise, returns false. 1109c869e672SAlan Cox */ 1110c869e672SAlan Cox bool 1111c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m) 1112c869e672SAlan Cox { 1113c869e672SAlan Cox vm_reserv_t rv; 1114c869e672SAlan Cox 1115c869e672SAlan Cox rv = vm_reserv_from_page(m); 1116c869e672SAlan Cox if (rv->object == NULL) 1117c869e672SAlan Cox return (false); 111884e2ae64SDoug Moore return (!bit_test(rv->popmap, m - rv->pages)); 1119c869e672SAlan Cox } 1120c869e672SAlan Cox 1121c869e672SAlan Cox /* 11221526667bSDoug Moore * Returns true if the given page is part of a block of npages, starting at a 11231526667bSDoug Moore * multiple of npages, that are all allocated. Otherwise, returns false. 11241526667bSDoug Moore */ 11251526667bSDoug Moore bool 11261526667bSDoug Moore vm_reserv_is_populated(vm_page_t m, int npages) 11271526667bSDoug Moore { 11281526667bSDoug Moore vm_reserv_t rv; 11291526667bSDoug Moore int index; 11301526667bSDoug Moore 11311526667bSDoug Moore KASSERT(npages <= VM_LEVEL_0_NPAGES, 11321526667bSDoug Moore ("%s: npages %d exceeds VM_LEVEL_0_NPAGES", __func__, npages)); 11331526667bSDoug Moore KASSERT(powerof2(npages), 11341526667bSDoug Moore ("%s: npages %d is not a power of 2", __func__, npages)); 11351526667bSDoug Moore rv = vm_reserv_from_page(m); 11361526667bSDoug Moore if (rv->object == NULL) 11371526667bSDoug Moore return (false); 11381526667bSDoug Moore index = rounddown2(m - rv->pages, npages); 11391526667bSDoug Moore return (bit_ntest(rv->popmap, index, index + npages - 1, 1)); 11401526667bSDoug Moore } 11411526667bSDoug Moore 11421526667bSDoug Moore /* 1143c869e672SAlan Cox * If the given page belongs to a reservation, returns the level of that 1144c869e672SAlan Cox * reservation. Otherwise, returns -1. 1145c869e672SAlan Cox */ 1146c869e672SAlan Cox int 1147c869e672SAlan Cox vm_reserv_level(vm_page_t m) 1148c869e672SAlan Cox { 1149c869e672SAlan Cox vm_reserv_t rv; 1150c869e672SAlan Cox 1151c869e672SAlan Cox rv = vm_reserv_from_page(m); 11523e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 11533e00c11aSAlan Cox return (rv->object != NULL ? 1 : -1); 11543e00c11aSAlan Cox #else 1155c869e672SAlan Cox return (rv->object != NULL ? 0 : -1); 11563e00c11aSAlan Cox #endif 1157c869e672SAlan Cox } 1158c869e672SAlan Cox 1159c869e672SAlan Cox /* 11603453bca8SAlan Cox * Returns a reservation level if the given page belongs to a fully populated 1161f8a47341SAlan Cox * reservation and -1 otherwise. 1162f8a47341SAlan Cox */ 1163f8a47341SAlan Cox int 1164f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m) 1165f8a47341SAlan Cox { 1166f8a47341SAlan Cox vm_reserv_t rv; 1167f8a47341SAlan Cox 1168f8a47341SAlan Cox rv = vm_reserv_from_page(m); 11693e00c11aSAlan Cox if (rv->popcnt == VM_LEVEL_0_NPAGES) { 11703e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 11713e00c11aSAlan Cox return (1); 11723e00c11aSAlan Cox } else if (rv->pages != NULL && 11733e00c11aSAlan Cox vm_reserv_is_sublevel_full(rv, m - rv->pages)) { 11743e00c11aSAlan Cox #endif 11753e00c11aSAlan Cox return (0); 11763e00c11aSAlan Cox } 11773e00c11aSAlan Cox return (-1); 1178f8a47341SAlan Cox } 1179f8a47341SAlan Cox 1180f8a47341SAlan Cox /* 1181b378d296SMark Johnston * Remove a partially populated reservation from the queue. 1182b378d296SMark Johnston */ 1183b378d296SMark Johnston static void 1184b378d296SMark Johnston vm_reserv_dequeue(vm_reserv_t rv) 1185b378d296SMark Johnston { 1186b378d296SMark Johnston 1187b378d296SMark Johnston vm_reserv_domain_assert_locked(rv->domain); 1188b378d296SMark Johnston vm_reserv_assert_locked(rv); 1189b378d296SMark Johnston CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 1190b378d296SMark Johnston __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 1191b378d296SMark Johnston KASSERT(rv->inpartpopq, 1192b378d296SMark Johnston ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv)); 1193b378d296SMark Johnston 1194b378d296SMark Johnston TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq); 1195b378d296SMark Johnston rv->inpartpopq = FALSE; 1196b378d296SMark Johnston } 1197b378d296SMark Johnston 1198b378d296SMark Johnston /* 11993453bca8SAlan Cox * Breaks the given partially populated reservation, releasing its free pages 12003453bca8SAlan Cox * to the physical memory allocator. 1201f8a47341SAlan Cox */ 120244aab2c3SAlan Cox static void 120344aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv) 1204f8a47341SAlan Cox { 1205f8a47341SAlan Cox 12065c930c89SJeff Roberson vm_reserv_assert_locked(rv); 12075c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 12085c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 1209b378d296SMark Johnston if (rv->inpartpopq) { 12105c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 1211b378d296SMark Johnston vm_reserv_dequeue(rv); 12125c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 1213b378d296SMark Johnston } 1214ada27a3bSKonstantin Belousov vm_reserv_break(rv); 12155c930c89SJeff Roberson counter_u64_add(vm_reserv_reclaimed, 1); 121644aab2c3SAlan Cox } 121744aab2c3SAlan Cox 121844aab2c3SAlan Cox /* 1219b378d296SMark Johnston * Breaks a reservation near the head of the partially populated reservation 12203453bca8SAlan Cox * queue, releasing its free pages to the physical memory allocator. Returns 12213453bca8SAlan Cox * TRUE if a reservation is broken and FALSE otherwise. 122244aab2c3SAlan Cox */ 1223b378d296SMark Johnston bool 1224ef435ae7SJeff Roberson vm_reserv_reclaim_inactive(int domain) 122544aab2c3SAlan Cox { 122644aab2c3SAlan Cox vm_reserv_t rv; 122744aab2c3SAlan Cox 1228b378d296SMark Johnston vm_reserv_domain_lock(domain); 1229b378d296SMark Johnston TAILQ_FOREACH(rv, &vm_rvd[domain].partpop, partpopq) { 1230b378d296SMark Johnston /* 1231b378d296SMark Johnston * A locked reservation is likely being updated or reclaimed, 1232b378d296SMark Johnston * so just skip ahead. 1233b378d296SMark Johnston */ 1234b378d296SMark Johnston if (rv != &vm_rvd[domain].marker && vm_reserv_trylock(rv)) { 1235b378d296SMark Johnston vm_reserv_dequeue(rv); 1236b378d296SMark Johnston break; 12375c930c89SJeff Roberson } 1238b378d296SMark Johnston } 1239b378d296SMark Johnston vm_reserv_domain_unlock(domain); 1240b378d296SMark Johnston if (rv != NULL) { 124144aab2c3SAlan Cox vm_reserv_reclaim(rv); 12425c930c89SJeff Roberson vm_reserv_unlock(rv); 1243b378d296SMark Johnston return (true); 1244f8a47341SAlan Cox } 1245b378d296SMark Johnston return (false); 1246f8a47341SAlan Cox } 1247f8a47341SAlan Cox 1248f8a47341SAlan Cox /* 1249f96e8a0bSDoug Moore * Determine whether this reservation has free pages that satisfy the given 1250f96e8a0bSDoug Moore * request for contiguous physical memory. Start searching from the lower 12516f1c8908SDoug Moore * bound, defined by lo, and stop at the upper bound, hi. Return the index 12526f1c8908SDoug Moore * of the first satisfactory free page, or -1 if none is found. 1253f96e8a0bSDoug Moore */ 12546f1c8908SDoug Moore static int 12556f1c8908SDoug Moore vm_reserv_find_contig(vm_reserv_t rv, int npages, int lo, 12566f1c8908SDoug Moore int hi, int ppn_align, int ppn_bound) 1257f96e8a0bSDoug Moore { 1258f96e8a0bSDoug Moore 1259f96e8a0bSDoug Moore vm_reserv_assert_locked(rv); 12606f1c8908SDoug Moore KASSERT(npages <= VM_LEVEL_0_NPAGES - 1, 12616f1c8908SDoug Moore ("%s: Too many pages", __func__)); 12626f1c8908SDoug Moore KASSERT(ppn_bound <= VM_LEVEL_0_NPAGES, 12636f1c8908SDoug Moore ("%s: Too big a boundary for reservation size", __func__)); 12646f1c8908SDoug Moore KASSERT(npages <= ppn_bound, 12656f1c8908SDoug Moore ("%s: Too many pages for given boundary", __func__)); 12666f1c8908SDoug Moore KASSERT(ppn_align != 0 && powerof2(ppn_align), 12676f1c8908SDoug Moore ("ppn_align is not a positive power of 2")); 12686f1c8908SDoug Moore KASSERT(ppn_bound != 0 && powerof2(ppn_bound), 12696f1c8908SDoug Moore ("ppn_bound is not a positive power of 2")); 127084e2ae64SDoug Moore while (bit_ffc_area_at(rv->popmap, lo, hi, npages, &lo), lo != -1) { 12716f1c8908SDoug Moore if (lo < roundup2(lo, ppn_align)) { 1272f96e8a0bSDoug Moore /* Skip to next aligned page. */ 12736f1c8908SDoug Moore lo = roundup2(lo, ppn_align); 127484e2ae64SDoug Moore } else if (roundup2(lo + 1, ppn_bound) >= lo + npages) 12756f1c8908SDoug Moore return (lo); 127684e2ae64SDoug Moore if (roundup2(lo + 1, ppn_bound) < lo + npages) { 127784e2ae64SDoug Moore /* Skip to next boundary-matching page. */ 127884e2ae64SDoug Moore lo = roundup2(lo + 1, ppn_bound); 1279f96e8a0bSDoug Moore } 128084e2ae64SDoug Moore } 12816f1c8908SDoug Moore return (-1); 1282f96e8a0bSDoug Moore } 1283f96e8a0bSDoug Moore 1284f96e8a0bSDoug Moore /* 12853453bca8SAlan Cox * Searches the partially populated reservation queue for the least recently 12863453bca8SAlan Cox * changed reservation with free pages that satisfy the given request for 12873453bca8SAlan Cox * contiguous physical memory. If a satisfactory reservation is found, it is 1288989a2cf1SMinsoo Choo * broken. Returns a page if a reservation is broken and NULL otherwise. 128944aab2c3SAlan Cox */ 12900d5fac28SDoug Moore vm_page_t 1291ef435ae7SJeff Roberson vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low, 1292ef435ae7SJeff Roberson vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 129344aab2c3SAlan Cox { 1294b378d296SMark Johnston struct vm_reserv_queue *queue; 1295ec179322SAlan Cox vm_paddr_t pa, size; 12960d5fac28SDoug Moore vm_page_t m_ret; 1297b378d296SMark Johnston vm_reserv_t marker, rv, rvn; 12986f1c8908SDoug Moore int hi, lo, posn, ppn_align, ppn_bound; 129944aab2c3SAlan Cox 13006f1c8908SDoug Moore KASSERT(npages > 0, ("npages is 0")); 13016f1c8908SDoug Moore KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 13026f1c8908SDoug Moore KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 1303c68c3537SAlan Cox if (npages > VM_LEVEL_0_NPAGES - 1) 1304989a2cf1SMinsoo Choo return (NULL); 13056f1c8908SDoug Moore size = npages << PAGE_SHIFT; 13066f1c8908SDoug Moore /* 13076f1c8908SDoug Moore * Ensure that a free range starting at a boundary-multiple 13086f1c8908SDoug Moore * doesn't include a boundary-multiple within it. Otherwise, 13096f1c8908SDoug Moore * no boundary-constrained allocation is possible. 13106f1c8908SDoug Moore */ 1311c606ab59SDoug Moore if (!vm_addr_bound_ok(0, size, boundary)) 13120d5fac28SDoug Moore return (NULL); 1313b378d296SMark Johnston marker = &vm_rvd[domain].marker; 1314b378d296SMark Johnston queue = &vm_rvd[domain].partpop; 13156f1c8908SDoug Moore /* 13166f1c8908SDoug Moore * Compute shifted alignment, boundary values for page-based 13176f1c8908SDoug Moore * calculations. Constrain to range [1, VM_LEVEL_0_NPAGES] to 13186f1c8908SDoug Moore * avoid overflow. 13196f1c8908SDoug Moore */ 13206f1c8908SDoug Moore ppn_align = (int)(ulmin(ulmax(PAGE_SIZE, alignment), 13216f1c8908SDoug Moore VM_LEVEL_0_SIZE) >> PAGE_SHIFT); 132249fd2d51SDoug Moore ppn_bound = boundary == 0 ? VM_LEVEL_0_NPAGES : 132349fd2d51SDoug Moore (int)(MIN(MAX(PAGE_SIZE, boundary), 13246f1c8908SDoug Moore VM_LEVEL_0_SIZE) >> PAGE_SHIFT); 1325b378d296SMark Johnston 1326b378d296SMark Johnston vm_reserv_domain_scan_lock(domain); 13275c930c89SJeff Roberson vm_reserv_domain_lock(domain); 1328b378d296SMark Johnston TAILQ_FOREACH_SAFE(rv, queue, partpopq, rvn) { 1329f96e8a0bSDoug Moore pa = VM_PAGE_TO_PHYS(&rv->pages[0]); 1330f96e8a0bSDoug Moore if (pa + VM_LEVEL_0_SIZE - size < low) { 1331ec179322SAlan Cox /* This entire reservation is too low; go to next. */ 133244aab2c3SAlan Cox continue; 133344aab2c3SAlan Cox } 133444aab2c3SAlan Cox if (pa + size > high) { 1335ec179322SAlan Cox /* This entire reservation is too high; go to next. */ 1336ec179322SAlan Cox continue; 133785f2a0c9SMax Laier } 1338c606ab59SDoug Moore if (!vm_addr_align_ok(pa, alignment)) { 13396f1c8908SDoug Moore /* This entire reservation is unaligned; go to next. */ 13406f1c8908SDoug Moore continue; 13416f1c8908SDoug Moore } 1342b378d296SMark Johnston 13435c930c89SJeff Roberson if (vm_reserv_trylock(rv) == 0) { 1344b378d296SMark Johnston TAILQ_INSERT_AFTER(queue, rv, marker, partpopq); 13455c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 13465c930c89SJeff Roberson vm_reserv_lock(rv); 1347968079f2SMark Johnston if (TAILQ_PREV(marker, vm_reserv_queue, partpopq) != 1348968079f2SMark Johnston rv) { 1349b378d296SMark Johnston vm_reserv_unlock(rv); 13505c930c89SJeff Roberson vm_reserv_domain_lock(domain); 1351b378d296SMark Johnston rvn = TAILQ_NEXT(marker, partpopq); 1352b378d296SMark Johnston TAILQ_REMOVE(queue, marker, partpopq); 13535c930c89SJeff Roberson continue; 13545c930c89SJeff Roberson } 1355b378d296SMark Johnston vm_reserv_domain_lock(domain); 1356b378d296SMark Johnston TAILQ_REMOVE(queue, marker, partpopq); 1357b378d296SMark Johnston } 13585c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 13596f1c8908SDoug Moore lo = (pa >= low) ? 0 : 13606f1c8908SDoug Moore (int)((low + PAGE_MASK - pa) >> PAGE_SHIFT); 13616f1c8908SDoug Moore hi = (pa + VM_LEVEL_0_SIZE <= high) ? VM_LEVEL_0_NPAGES : 13626f1c8908SDoug Moore (int)((high - pa) >> PAGE_SHIFT); 13636f1c8908SDoug Moore posn = vm_reserv_find_contig(rv, (int)npages, lo, hi, 13646f1c8908SDoug Moore ppn_align, ppn_bound); 13656f1c8908SDoug Moore if (posn >= 0) { 13660d5fac28SDoug Moore vm_reserv_domain_scan_unlock(domain); 13670d5fac28SDoug Moore /* Allocate requested space */ 13680d5fac28SDoug Moore rv->popcnt += npages; 136984e2ae64SDoug Moore bit_nset(rv->popmap, posn, posn + npages - 1); 13700d5fac28SDoug Moore vm_reserv_reclaim(rv); 13710d5fac28SDoug Moore vm_reserv_unlock(rv); 13720d5fac28SDoug Moore m_ret = &rv->pages[posn]; 13730d5fac28SDoug Moore pa = VM_PAGE_TO_PHYS(m_ret); 1374c606ab59SDoug Moore KASSERT(vm_addr_ok(pa, size, alignment, boundary), 1375c606ab59SDoug Moore ("%s: adjusted address not aligned/bounded to " 1376c606ab59SDoug Moore "%lx/%jx", 1377c606ab59SDoug Moore __func__, alignment, (uintmax_t)boundary)); 13780d5fac28SDoug Moore return (m_ret); 137944aab2c3SAlan Cox } 13805c930c89SJeff Roberson vm_reserv_domain_lock(domain); 1381968079f2SMark Johnston rvn = TAILQ_NEXT(rv, partpopq); 1382968079f2SMark Johnston vm_reserv_unlock(rv); 138344aab2c3SAlan Cox } 13845c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 1385b378d296SMark Johnston vm_reserv_domain_scan_unlock(domain); 13860d5fac28SDoug Moore return (NULL); 138744aab2c3SAlan Cox } 138844aab2c3SAlan Cox 138944aab2c3SAlan Cox /* 1390f8a47341SAlan Cox * Transfers the reservation underlying the given page to a new object. 1391f8a47341SAlan Cox * 1392f8a47341SAlan Cox * The object must be locked. 1393f8a47341SAlan Cox */ 1394f8a47341SAlan Cox void 1395f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object, 1396f8a47341SAlan Cox vm_pindex_t old_object_offset) 1397f8a47341SAlan Cox { 1398f8a47341SAlan Cox vm_reserv_t rv; 1399f8a47341SAlan Cox 140089f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(new_object); 1401f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1402f8a47341SAlan Cox if (rv->object == old_object) { 14035c930c89SJeff Roberson vm_reserv_lock(rv); 14045c930c89SJeff Roberson CTR6(KTR_VM, 14055c930c89SJeff Roberson "%s: rv %p object %p new %p popcnt %d inpartpop %d", 14065c930c89SJeff Roberson __FUNCTION__, rv, rv->object, new_object, rv->popcnt, 14075c930c89SJeff Roberson rv->inpartpopq); 1408f8a47341SAlan Cox if (rv->object == old_object) { 1409e2068d0bSJeff Roberson vm_reserv_object_lock(old_object); 1410e2068d0bSJeff Roberson rv->object = NULL; 1411f8a47341SAlan Cox LIST_REMOVE(rv, objq); 1412e2068d0bSJeff Roberson vm_reserv_object_unlock(old_object); 1413e2068d0bSJeff Roberson vm_reserv_object_lock(new_object); 1414f8a47341SAlan Cox rv->object = new_object; 1415f8a47341SAlan Cox rv->pindex -= old_object_offset; 1416e2068d0bSJeff Roberson LIST_INSERT_HEAD(&new_object->rvq, rv, objq); 1417e2068d0bSJeff Roberson vm_reserv_object_unlock(new_object); 1418f8a47341SAlan Cox } 14195c930c89SJeff Roberson vm_reserv_unlock(rv); 1420f8a47341SAlan Cox } 1421f8a47341SAlan Cox } 1422f8a47341SAlan Cox 1423f8a47341SAlan Cox /* 1424c869e672SAlan Cox * Returns the size (in bytes) of a reservation of the specified level. 1425c869e672SAlan Cox */ 1426c869e672SAlan Cox int 1427c869e672SAlan Cox vm_reserv_size(int level) 1428c869e672SAlan Cox { 1429c869e672SAlan Cox 1430c869e672SAlan Cox switch (level) { 1431c869e672SAlan Cox case 0: 14323e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 14333e00c11aSAlan Cox return (VM_SUBLEVEL_0_NPAGES * PAGE_SIZE); 14343e00c11aSAlan Cox case 1: 14353e00c11aSAlan Cox #endif 1436c869e672SAlan Cox return (VM_LEVEL_0_SIZE); 1437c869e672SAlan Cox case -1: 1438c869e672SAlan Cox return (PAGE_SIZE); 1439c869e672SAlan Cox default: 1440c869e672SAlan Cox return (0); 1441c869e672SAlan Cox } 1442c869e672SAlan Cox } 1443c869e672SAlan Cox 1444c869e672SAlan Cox /* 1445f8a47341SAlan Cox * Allocates the virtual and physical memory required by the reservation 1446f8a47341SAlan Cox * management system's data structures, in particular, the reservation array. 1447f8a47341SAlan Cox */ 1448f8a47341SAlan Cox vm_paddr_t 14493e5e1b51SJeff Roberson vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end) 1450f8a47341SAlan Cox { 14517988971aSD Scott Phillips vm_paddr_t new_end; 14527988971aSD Scott Phillips vm_pindex_t count; 1453f8a47341SAlan Cox size_t size; 14543e5e1b51SJeff Roberson int i; 14553e5e1b51SJeff Roberson 14567988971aSD Scott Phillips count = 0; 14573e5e1b51SJeff Roberson for (i = 0; i < vm_phys_nsegs; i++) { 14587988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 14597988971aSD Scott Phillips count += howmany(vm_phys_segs[i].end, VM_LEVEL_0_SIZE) - 14607988971aSD Scott Phillips vm_phys_segs[i].start / VM_LEVEL_0_SIZE; 14617988971aSD Scott Phillips #else 14627988971aSD Scott Phillips count = MAX(count, 14637988971aSD Scott Phillips howmany(vm_phys_segs[i].end, VM_LEVEL_0_SIZE)); 14647988971aSD Scott Phillips #endif 14653e5e1b51SJeff Roberson } 14663e5e1b51SJeff Roberson 14677988971aSD Scott Phillips for (i = 0; phys_avail[i + 1] != 0; i += 2) { 14687988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 14697988971aSD Scott Phillips count += howmany(phys_avail[i + 1], VM_LEVEL_0_SIZE) - 14707988971aSD Scott Phillips phys_avail[i] / VM_LEVEL_0_SIZE; 14717988971aSD Scott Phillips #else 14727988971aSD Scott Phillips count = MAX(count, 14737988971aSD Scott Phillips howmany(phys_avail[i + 1], VM_LEVEL_0_SIZE)); 14747988971aSD Scott Phillips #endif 14753e5e1b51SJeff Roberson } 1476f8a47341SAlan Cox 1477f8a47341SAlan Cox /* 14787988971aSD Scott Phillips * Calculate the size (in bytes) of the reservation array. Rounding up 14797988971aSD Scott Phillips * for partial superpages at boundaries, as every small page is mapped 14807988971aSD Scott Phillips * to an element in the reservation array based on its physical address. 14817988971aSD Scott Phillips * Thus, the number of elements in the reservation array can be greater 14827988971aSD Scott Phillips * than the number of superpages. 1483f8a47341SAlan Cox */ 14847988971aSD Scott Phillips size = count * sizeof(struct vm_reserv); 1485f8a47341SAlan Cox 1486f8a47341SAlan Cox /* 1487f8a47341SAlan Cox * Allocate and map the physical memory for the reservation array. The 1488f8a47341SAlan Cox * next available virtual address is returned by reference. 1489f8a47341SAlan Cox */ 1490f8a47341SAlan Cox new_end = end - round_page(size); 1491f8a47341SAlan Cox vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end, 1492f8a47341SAlan Cox VM_PROT_READ | VM_PROT_WRITE); 1493f8a47341SAlan Cox bzero(vm_reserv_array, size); 1494f8a47341SAlan Cox 1495f8a47341SAlan Cox /* 1496f8a47341SAlan Cox * Return the next available physical address. 1497f8a47341SAlan Cox */ 1498f8a47341SAlan Cox return (new_end); 1499f8a47341SAlan Cox } 1500f8a47341SAlan Cox 15018b5e1472SAlan Cox /* 15028b5e1472SAlan Cox * Returns the superpage containing the given page. 15038b5e1472SAlan Cox */ 15048b5e1472SAlan Cox vm_page_t 15058b5e1472SAlan Cox vm_reserv_to_superpage(vm_page_t m) 15068b5e1472SAlan Cox { 15078b5e1472SAlan Cox vm_reserv_t rv; 15088b5e1472SAlan Cox 15098b5e1472SAlan Cox VM_OBJECT_ASSERT_LOCKED(m->object); 15108b5e1472SAlan Cox rv = vm_reserv_from_page(m); 15113e00c11aSAlan Cox if (rv->object == m->object) { 15123e00c11aSAlan Cox if (rv->popcnt == VM_LEVEL_0_NPAGES) 15133e00c11aSAlan Cox return (rv->pages); 15143e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 15153e00c11aSAlan Cox if (vm_reserv_is_sublevel_full(rv, m - rv->pages)) 15163e00c11aSAlan Cox return (rv->pages + rounddown2(m - rv->pages, 15173e00c11aSAlan Cox VM_SUBLEVEL_0_NPAGES)); 15183e00c11aSAlan Cox #endif 15193e00c11aSAlan Cox } 15203e00c11aSAlan Cox return (NULL); 15218b5e1472SAlan Cox } 15228b5e1472SAlan Cox 1523f8a47341SAlan Cox #endif /* VM_NRESERVLEVEL > 0 */ 1524