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); 483ee511f83SDoug Moore vm_phys_free_pages(rv->pages, VM_FREEPOOL_DEFAULT, 484ee511f83SDoug 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 /* 509626ec04cSDoug Moore * Either returns an existing reservation or returns NULL and initializes 510626ec04cSDoug Moore * successor pointer. 511e2068d0bSJeff Roberson */ 512e2068d0bSJeff Roberson static vm_reserv_t 513e2068d0bSJeff Roberson vm_reserv_from_object(vm_object_t object, vm_pindex_t pindex, 514e2068d0bSJeff Roberson vm_page_t mpred, vm_page_t *msuccp) 515e2068d0bSJeff Roberson { 516e2068d0bSJeff Roberson vm_reserv_t rv; 517e2068d0bSJeff Roberson vm_page_t msucc; 518e2068d0bSJeff Roberson 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)) 526626ec04cSDoug Moore return (rv); 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 return (rv); 536e2068d0bSJeff Roberson } 537626ec04cSDoug Moore *msuccp = msucc; 538626ec04cSDoug Moore return (NULL); 539626ec04cSDoug Moore } 540e2068d0bSJeff Roberson 541e2068d0bSJeff Roberson /* 542f8a47341SAlan Cox * Returns TRUE if the given reservation contains the given page index and 543f8a47341SAlan Cox * FALSE otherwise. 544f8a47341SAlan Cox */ 545f8a47341SAlan Cox static __inline boolean_t 546f8a47341SAlan Cox vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex) 547f8a47341SAlan Cox { 548f8a47341SAlan Cox 549f8a47341SAlan Cox return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0); 550f8a47341SAlan Cox } 551f8a47341SAlan Cox 552f8a47341SAlan Cox /* 553*b68c7ebfSDoug Moore * How many pages should be in a new allocation that starts at the first page of 554*b68c7ebfSDoug Moore * the reservation superpage that contains 'first', fits between the allocations 555*b68c7ebfSDoug Moore * that include 'mpred' and 'msucc', fits within 'object', includes at least 556*b68c7ebfSDoug Moore * 'minpages' pages, and tries to include every allocated page in a superpage? 557*b68c7ebfSDoug Moore * 558*b68c7ebfSDoug Moore * We must synchronize with the reserv object lock to protect the pindex/object 559*b68c7ebfSDoug Moore * of the resulting reservations against rename while we are inspecting. 560*b68c7ebfSDoug Moore */ 561*b68c7ebfSDoug Moore static u_long 562*b68c7ebfSDoug Moore vm_reserv_num_alloc_pages(vm_object_t object, vm_pindex_t first, 563*b68c7ebfSDoug Moore u_long minpages, vm_page_t mpred, vm_page_t msucc) 564*b68c7ebfSDoug Moore { 565*b68c7ebfSDoug Moore vm_pindex_t leftcap, rightcap; 566*b68c7ebfSDoug Moore vm_reserv_t rv; 567*b68c7ebfSDoug Moore u_int allocpages; 568*b68c7ebfSDoug Moore 569*b68c7ebfSDoug Moore allocpages = roundup2(minpages, VM_LEVEL_0_NPAGES); 570*b68c7ebfSDoug Moore 571*b68c7ebfSDoug Moore vm_reserv_object_lock(object); 572*b68c7ebfSDoug Moore if (mpred != NULL) { 573*b68c7ebfSDoug Moore if ((rv = vm_reserv_from_page(mpred))->object != object) 574*b68c7ebfSDoug Moore leftcap = mpred->pindex + 1; 575*b68c7ebfSDoug Moore else 576*b68c7ebfSDoug Moore leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 577*b68c7ebfSDoug Moore if (leftcap > first) 578*b68c7ebfSDoug Moore allocpages = 0; 579*b68c7ebfSDoug Moore } 580*b68c7ebfSDoug Moore if (minpages < allocpages) { 581*b68c7ebfSDoug Moore if (msucc == NULL) { 582*b68c7ebfSDoug Moore /* 583*b68c7ebfSDoug Moore * Would the last new reservation extend past the end of 584*b68c7ebfSDoug Moore * the object? 585*b68c7ebfSDoug Moore * 586*b68c7ebfSDoug Moore * If the object is unlikely to grow don't allocate a 587*b68c7ebfSDoug Moore * reservation for the tail. 588*b68c7ebfSDoug Moore */ 589*b68c7ebfSDoug Moore if ((object->flags & OBJ_ANON) == 0) 590*b68c7ebfSDoug Moore rightcap = object->size; 591*b68c7ebfSDoug Moore else 592*b68c7ebfSDoug Moore rightcap = OBJ_MAX_SIZE; 593*b68c7ebfSDoug Moore } else { 594*b68c7ebfSDoug Moore /* 595*b68c7ebfSDoug Moore * Would the last new reservation extend past the start 596*b68c7ebfSDoug Moore * of another page or reservation? 597*b68c7ebfSDoug Moore * 598*b68c7ebfSDoug Moore * If the object would, don't allocate a reservation for 599*b68c7ebfSDoug Moore * the tail. 600*b68c7ebfSDoug Moore */ 601*b68c7ebfSDoug Moore if ((rv = vm_reserv_from_page(msucc))->object != object) 602*b68c7ebfSDoug Moore rightcap = msucc->pindex; 603*b68c7ebfSDoug Moore else 604*b68c7ebfSDoug Moore rightcap = rv->pindex; 605*b68c7ebfSDoug Moore } 606*b68c7ebfSDoug Moore if (first + allocpages > rightcap) { 607*b68c7ebfSDoug Moore /* 608*b68c7ebfSDoug Moore * A reservation for the last of the requested pages 609*b68c7ebfSDoug Moore * will not fit. Reduce the size of the upcoming 610*b68c7ebfSDoug Moore * allocation accordingly. 611*b68c7ebfSDoug Moore */ 612*b68c7ebfSDoug Moore allocpages = minpages; 613*b68c7ebfSDoug Moore } 614*b68c7ebfSDoug Moore } 615*b68c7ebfSDoug Moore vm_reserv_object_unlock(object); 616*b68c7ebfSDoug Moore return (allocpages); 617*b68c7ebfSDoug Moore } 618*b68c7ebfSDoug Moore 619*b68c7ebfSDoug Moore /* 620f8a47341SAlan Cox * Increases the given reservation's population count. Moves the reservation 6213453bca8SAlan Cox * to the tail of the partially populated reservation queue. 622f8a47341SAlan Cox */ 623f8a47341SAlan Cox static void 624ec179322SAlan Cox vm_reserv_populate(vm_reserv_t rv, int index) 625f8a47341SAlan Cox { 626f8a47341SAlan Cox 6275c930c89SJeff Roberson vm_reserv_assert_locked(rv); 6285c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 6295c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 630f8a47341SAlan Cox KASSERT(rv->object != NULL, 631f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is free", rv)); 63284e2ae64SDoug Moore KASSERT(!bit_test(rv->popmap, index), 633a08c1515SAlan Cox ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv, 634a08c1515SAlan Cox index)); 635f8a47341SAlan Cox KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES, 636f8a47341SAlan Cox ("vm_reserv_populate: reserv %p is already full", rv)); 6373e00c11aSAlan Cox KASSERT(rv->pages->psind >= 0 && 6383e00c11aSAlan Cox rv->pages->psind < VM_LEVEL_0_PSIND, 639dd05fa19SAlan Cox ("vm_reserv_populate: reserv %p is already promoted", rv)); 6402d3f4181SJeff Roberson KASSERT(rv->domain < vm_ndomains, 641ef435ae7SJeff Roberson ("vm_reserv_populate: reserv %p's domain is corrupted %d", 642ef435ae7SJeff Roberson rv, rv->domain)); 64384e2ae64SDoug Moore bit_set(rv->popmap, index); 6443e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 6453e00c11aSAlan Cox if (vm_reserv_is_sublevel_full(rv, index)) 6463e00c11aSAlan Cox rv->pages[rounddown2(index, VM_SUBLEVEL_0_NPAGES)].psind = 1; 6473e00c11aSAlan Cox #endif 6485c930c89SJeff Roberson rv->popcnt++; 6492ef6727eSJeff Roberson if ((unsigned)(ticks - rv->lasttick) < PARTPOPSLOP && 6502ef6727eSJeff Roberson rv->inpartpopq && rv->popcnt != VM_LEVEL_0_NPAGES) 6512ef6727eSJeff Roberson return; 6522ef6727eSJeff Roberson rv->lasttick = ticks; 6535c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 654f8a47341SAlan Cox if (rv->inpartpopq) { 655fe6d5344SMark Johnston TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq); 656f8a47341SAlan Cox rv->inpartpopq = FALSE; 657f8a47341SAlan Cox } 658f8a47341SAlan Cox if (rv->popcnt < VM_LEVEL_0_NPAGES) { 659f8a47341SAlan Cox rv->inpartpopq = TRUE; 660fe6d5344SMark Johnston TAILQ_INSERT_TAIL(&vm_rvd[rv->domain].partpop, rv, partpopq); 6615c930c89SJeff Roberson } else { 6623e00c11aSAlan Cox KASSERT(rv->pages->psind == VM_LEVEL_0_PSIND - 1, 6635c930c89SJeff Roberson ("vm_reserv_populate: reserv %p is already promoted", 6645c930c89SJeff Roberson rv)); 6653e00c11aSAlan Cox rv->pages->psind = VM_LEVEL_0_PSIND; 666f8a47341SAlan Cox } 6675c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 6685c930c89SJeff Roberson } 669f8a47341SAlan Cox 670f8a47341SAlan Cox /* 671e2068d0bSJeff Roberson * Allocates a contiguous set of physical pages of the given size "npages" 6722d5039dbSAlan Cox * from existing or newly created reservations. All of the physical pages 673e2068d0bSJeff Roberson * must be at or above the given physical address "low" and below the given 674e2068d0bSJeff Roberson * physical address "high". The given value "alignment" determines the 675e2068d0bSJeff Roberson * alignment of the first physical page in the set. If the given value 676e2068d0bSJeff Roberson * "boundary" is non-zero, then the set of physical pages cannot cross any 677e2068d0bSJeff Roberson * physical address boundary that is a multiple of that value. Both 678e2068d0bSJeff Roberson * "alignment" and "boundary" must be a power of two. 679e2068d0bSJeff Roberson * 680e2068d0bSJeff Roberson * The page "mpred" must immediately precede the offset "pindex" within the 681e2068d0bSJeff Roberson * specified object. 682e2068d0bSJeff Roberson * 6832d5039dbSAlan Cox * The object must be locked. 684e2068d0bSJeff Roberson */ 685e2068d0bSJeff Roberson vm_page_t 6862d5039dbSAlan Cox vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, int domain, 6872d5039dbSAlan Cox int req, vm_page_t mpred, u_long npages, vm_paddr_t low, vm_paddr_t high, 6882d5039dbSAlan Cox u_long alignment, vm_paddr_t boundary) 689c68c3537SAlan Cox { 6905c930c89SJeff Roberson struct vm_domain *vmd; 691c68c3537SAlan Cox vm_paddr_t pa, size; 692920da7e4SAlan Cox vm_page_t m, m_ret, msucc; 693*b68c7ebfSDoug Moore vm_pindex_t first; 694c68c3537SAlan Cox vm_reserv_t rv; 695*b68c7ebfSDoug Moore u_long allocpages; 696c68c3537SAlan Cox int i, index, n; 697c68c3537SAlan Cox 69889f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(object); 699c68c3537SAlan Cox KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0")); 700c68c3537SAlan Cox 701c68c3537SAlan Cox /* 702c68c3537SAlan Cox * Is a reservation fundamentally impossible? 703c68c3537SAlan Cox */ 704c68c3537SAlan Cox if (pindex < VM_RESERV_INDEX(object, pindex) || 705c68c3537SAlan Cox pindex + npages > object->size) 706c68c3537SAlan Cox return (NULL); 707c68c3537SAlan Cox 708c68c3537SAlan Cox /* 709c68c3537SAlan Cox * All reservations of a particular size have the same alignment. 710c68c3537SAlan Cox * Assuming that the first page is allocated from a reservation, the 711c68c3537SAlan Cox * least significant bits of its physical address can be determined 712c68c3537SAlan Cox * from its offset from the beginning of the reservation and the size 713c68c3537SAlan Cox * of the reservation. 714c68c3537SAlan Cox * 715c68c3537SAlan Cox * Could the specified index within a reservation of the smallest 716c68c3537SAlan Cox * possible size satisfy the alignment and boundary requirements? 717c68c3537SAlan Cox */ 718c68c3537SAlan Cox pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT; 719c68c3537SAlan Cox size = npages << PAGE_SHIFT; 720c606ab59SDoug Moore if (!vm_addr_ok(pa, size, alignment, boundary)) 721c68c3537SAlan Cox return (NULL); 722c68c3537SAlan Cox 723c68c3537SAlan Cox /* 7242d5039dbSAlan Cox * Look for an existing reservation. 725c68c3537SAlan Cox */ 726e2068d0bSJeff Roberson rv = vm_reserv_from_object(object, pindex, mpred, &msucc); 7272d5039dbSAlan Cox if (rv != NULL) { 7282d5039dbSAlan Cox KASSERT(object != kernel_object || rv->domain == domain, 7292d5039dbSAlan Cox ("vm_reserv_alloc_contig: domain mismatch")); 7302d5039dbSAlan Cox index = VM_RESERV_INDEX(object, pindex); 7312d5039dbSAlan Cox /* Does the allocation fit within the reservation? */ 7322d5039dbSAlan Cox if (index + npages > VM_LEVEL_0_NPAGES) 733e2068d0bSJeff Roberson return (NULL); 7342d5039dbSAlan Cox domain = rv->domain; 7352d5039dbSAlan Cox vmd = VM_DOMAIN(domain); 7362d5039dbSAlan Cox vm_reserv_lock(rv); 7372d5039dbSAlan Cox /* Handle reclaim race. */ 7382d5039dbSAlan Cox if (rv->object != object) 7392d5039dbSAlan Cox goto out; 7402d5039dbSAlan Cox m = &rv->pages[index]; 7412d5039dbSAlan Cox pa = VM_PAGE_TO_PHYS(m); 7422d5039dbSAlan Cox if (pa < low || pa + size > high || 743c606ab59SDoug Moore !vm_addr_ok(pa, size, alignment, boundary)) 7442d5039dbSAlan Cox goto out; 745c296ac7eSAlan Cox /* Handle vm_page_iter_rename(..., m, new_object, ...). */ 74684e2ae64SDoug Moore if (!bit_ntest(rv->popmap, index, index + npages - 1, 0)) 7472d5039dbSAlan Cox goto out; 7482d5039dbSAlan Cox if (!vm_domain_allocate(vmd, req, npages)) 7492d5039dbSAlan Cox goto out; 7502d5039dbSAlan Cox for (i = 0; i < npages; i++) 7512d5039dbSAlan Cox vm_reserv_populate(rv, index + i); 7522d5039dbSAlan Cox vm_reserv_unlock(rv); 7532d5039dbSAlan Cox return (m); 7542d5039dbSAlan Cox out: 7552d5039dbSAlan Cox vm_reserv_unlock(rv); 7562d5039dbSAlan Cox return (NULL); 7572d5039dbSAlan Cox } 758c68c3537SAlan Cox 759c68c3537SAlan Cox /* 760*b68c7ebfSDoug Moore * Check whether an allocation including at least one reservation can 761*b68c7ebfSDoug Moore * fit between mpred and msucc. 762c68c3537SAlan Cox */ 763c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 764*b68c7ebfSDoug Moore allocpages = vm_reserv_num_alloc_pages(object, first, 765*b68c7ebfSDoug Moore VM_RESERV_INDEX(object, pindex) + npages, mpred, msucc); 766*b68c7ebfSDoug Moore if (allocpages < VM_LEVEL_0_NPAGES) 767c68c3537SAlan Cox return (NULL); 768c68c3537SAlan Cox 769c68c3537SAlan Cox /* 77064f096eeSAlan Cox * Allocate the physical pages. The alignment and boundary specified 77164f096eeSAlan Cox * for this allocation may be different from the alignment and 77264f096eeSAlan Cox * boundary specified for the requested pages. For instance, the 77364f096eeSAlan Cox * specified index may not be the first page within the first new 77464f096eeSAlan Cox * reservation. 775c68c3537SAlan Cox */ 7765c930c89SJeff Roberson m = NULL; 7775c930c89SJeff Roberson vmd = VM_DOMAIN(domain); 7785c930c89SJeff Roberson if (vm_domain_allocate(vmd, req, npages)) { 7795c930c89SJeff Roberson vm_domain_free_lock(vmd); 7805c930c89SJeff Roberson m = vm_phys_alloc_contig(domain, allocpages, low, high, 7815c930c89SJeff Roberson ulmax(alignment, VM_LEVEL_0_SIZE), 7825c930c89SJeff Roberson boundary > VM_LEVEL_0_SIZE ? boundary : 0); 7835c930c89SJeff Roberson vm_domain_free_unlock(vmd); 7845c930c89SJeff Roberson if (m == NULL) { 7855c930c89SJeff Roberson vm_domain_freecnt_inc(vmd, npages); 7865c930c89SJeff Roberson return (NULL); 7875c930c89SJeff Roberson } 7885c930c89SJeff Roberson } else 789c68c3537SAlan Cox return (NULL); 790431fb8abSMark Johnston KASSERT(vm_page_domain(m) == domain, 7917a469c8eSJeff Roberson ("vm_reserv_alloc_contig: Page domain does not match requested.")); 79264f096eeSAlan Cox 79364f096eeSAlan Cox /* 79464f096eeSAlan Cox * The allocated physical pages always begin at a reservation 79564f096eeSAlan Cox * boundary, but they do not always end at a reservation boundary. 79664f096eeSAlan Cox * Initialize every reservation that is completely covered by the 79764f096eeSAlan Cox * allocated physical pages. 79864f096eeSAlan Cox */ 799c68c3537SAlan Cox m_ret = NULL; 800c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 801c68c3537SAlan Cox do { 802c68c3537SAlan Cox rv = vm_reserv_from_page(m); 803c68c3537SAlan Cox KASSERT(rv->pages == m, 804c68c3537SAlan Cox ("vm_reserv_alloc_contig: reserv %p's pages is corrupted", 805c68c3537SAlan Cox rv)); 8065c930c89SJeff Roberson vm_reserv_lock(rv); 807e2068d0bSJeff Roberson vm_reserv_insert(rv, object, first); 808c68c3537SAlan Cox n = ulmin(VM_LEVEL_0_NPAGES - index, npages); 809c68c3537SAlan Cox for (i = 0; i < n; i++) 810ec179322SAlan Cox vm_reserv_populate(rv, index + i); 811c68c3537SAlan Cox npages -= n; 812c68c3537SAlan Cox if (m_ret == NULL) { 813c68c3537SAlan Cox m_ret = &rv->pages[index]; 814c68c3537SAlan Cox index = 0; 815c68c3537SAlan Cox } 8165c930c89SJeff Roberson vm_reserv_unlock(rv); 817c68c3537SAlan Cox m += VM_LEVEL_0_NPAGES; 818c68c3537SAlan Cox first += VM_LEVEL_0_NPAGES; 819c68c3537SAlan Cox allocpages -= VM_LEVEL_0_NPAGES; 82064f096eeSAlan Cox } while (allocpages >= VM_LEVEL_0_NPAGES); 821c68c3537SAlan Cox return (m_ret); 822e2068d0bSJeff Roberson } 823c68c3537SAlan Cox 824c68c3537SAlan Cox /* 8252d5039dbSAlan Cox * Allocate a physical page from an existing or newly created reservation. 826e2068d0bSJeff Roberson * 827e2068d0bSJeff Roberson * The page "mpred" must immediately precede the offset "pindex" within the 828e2068d0bSJeff Roberson * specified object. 829e2068d0bSJeff Roberson * 830e2068d0bSJeff Roberson * The object must be locked. 831c68c3537SAlan Cox */ 832e2068d0bSJeff Roberson vm_page_t 8332d5039dbSAlan Cox vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, int domain, 8342d5039dbSAlan Cox int req, vm_page_t mpred) 835e2068d0bSJeff Roberson { 836e2068d0bSJeff Roberson struct vm_domain *vmd; 837e2068d0bSJeff Roberson vm_page_t m, msucc; 838*b68c7ebfSDoug Moore vm_pindex_t first; 839e2068d0bSJeff Roberson vm_reserv_t rv; 84030fbfddaSJeff Roberson int index; 841e2068d0bSJeff Roberson 842e2068d0bSJeff Roberson VM_OBJECT_ASSERT_WLOCKED(object); 843e2068d0bSJeff Roberson 844e2068d0bSJeff Roberson /* 8452d5039dbSAlan Cox * Is a reservation fundamentally impossible? 846e2068d0bSJeff Roberson */ 847e2068d0bSJeff Roberson if (pindex < VM_RESERV_INDEX(object, pindex) || 8482d5039dbSAlan Cox pindex >= object->size) 849e2068d0bSJeff Roberson return (NULL); 850e2068d0bSJeff Roberson 851e2068d0bSJeff Roberson /* 852e2068d0bSJeff Roberson * Look for an existing reservation. 853e2068d0bSJeff Roberson */ 854e2068d0bSJeff Roberson rv = vm_reserv_from_object(object, pindex, mpred, &msucc); 8552d5039dbSAlan Cox if (rv != NULL) { 856e2068d0bSJeff Roberson KASSERT(object != kernel_object || rv->domain == domain, 8572d5039dbSAlan Cox ("vm_reserv_alloc_page: domain mismatch")); 858e2068d0bSJeff Roberson domain = rv->domain; 859e2068d0bSJeff Roberson vmd = VM_DOMAIN(domain); 860c68c3537SAlan Cox index = VM_RESERV_INDEX(object, pindex); 861c68c3537SAlan Cox m = &rv->pages[index]; 8625c930c89SJeff Roberson vm_reserv_lock(rv); 863e2068d0bSJeff Roberson /* Handle reclaim race. */ 8645c930c89SJeff Roberson if (rv->object != object || 865c296ac7eSAlan Cox /* Handle vm_page_iter_rename(..., m, new_object, ...). */ 86684e2ae64SDoug Moore bit_test(rv->popmap, index)) { 867e2068d0bSJeff Roberson m = NULL; 8685c930c89SJeff Roberson goto out; 86930fbfddaSJeff Roberson } 8705c930c89SJeff Roberson if (vm_domain_allocate(vmd, req, 1) == 0) 8715c930c89SJeff Roberson m = NULL; 8725c930c89SJeff Roberson else 8735c930c89SJeff Roberson vm_reserv_populate(rv, index); 8745c930c89SJeff Roberson out: 8755c930c89SJeff Roberson vm_reserv_unlock(rv); 876c68c3537SAlan Cox return (m); 877c68c3537SAlan Cox } 878c68c3537SAlan Cox 879c68c3537SAlan Cox /* 880*b68c7ebfSDoug Moore * Check whether an allocation including reservations can fit 881*b68c7ebfSDoug Moore * between mpred and msucc. 882f8a47341SAlan Cox */ 883c68c3537SAlan Cox first = pindex - VM_RESERV_INDEX(object, pindex); 884*b68c7ebfSDoug Moore if (vm_reserv_num_alloc_pages(object, first, 1, mpred, msucc) < 885*b68c7ebfSDoug Moore VM_LEVEL_0_NPAGES) 886f8a47341SAlan Cox return (NULL); 887f8a47341SAlan Cox 888f8a47341SAlan Cox /* 889c68c3537SAlan Cox * Allocate and populate the new reservation. 890f8a47341SAlan Cox */ 8915c930c89SJeff Roberson m = NULL; 8925c930c89SJeff Roberson vmd = VM_DOMAIN(domain); 8935c930c89SJeff Roberson if (vm_domain_allocate(vmd, req, 1)) { 8945c930c89SJeff Roberson vm_domain_free_lock(vmd); 8955c930c89SJeff Roberson m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT, 8965c930c89SJeff Roberson VM_LEVEL_0_ORDER); 8975c930c89SJeff Roberson vm_domain_free_unlock(vmd); 8985c930c89SJeff Roberson if (m == NULL) { 8995c930c89SJeff Roberson vm_domain_freecnt_inc(vmd, 1); 9005c930c89SJeff Roberson return (NULL); 9015c930c89SJeff Roberson } 9025c930c89SJeff Roberson } else 903c68c3537SAlan Cox return (NULL); 904f8a47341SAlan Cox rv = vm_reserv_from_page(m); 9055c930c89SJeff Roberson vm_reserv_lock(rv); 906f8a47341SAlan Cox KASSERT(rv->pages == m, 907c68c3537SAlan Cox ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv)); 908e2068d0bSJeff Roberson vm_reserv_insert(rv, object, first); 909ec179322SAlan Cox index = VM_RESERV_INDEX(object, pindex); 910ec179322SAlan Cox vm_reserv_populate(rv, index); 9115c930c89SJeff Roberson vm_reserv_unlock(rv); 9125c930c89SJeff Roberson 913ec179322SAlan Cox return (&rv->pages[index]); 914f8a47341SAlan Cox } 915f8a47341SAlan Cox 916f8a47341SAlan Cox /* 917ada27a3bSKonstantin Belousov * Breaks the given reservation. All free pages in the reservation 918ada27a3bSKonstantin Belousov * are returned to the physical memory allocator. The reservation's 919ada27a3bSKonstantin Belousov * population count and map are reset to their initial state. 920ec179322SAlan Cox * 9213453bca8SAlan Cox * The given reservation must not be in the partially populated reservation 922fe6d5344SMark Johnston * queue. 923ec179322SAlan Cox */ 924ec179322SAlan Cox static void 925ada27a3bSKonstantin Belousov vm_reserv_break(vm_reserv_t rv) 926ec179322SAlan Cox { 9273e00c11aSAlan Cox vm_page_t m; 9280078df5fSDoug Moore int pos, pos0, pos1; 929ec179322SAlan Cox 9305c930c89SJeff Roberson vm_reserv_assert_locked(rv); 9315c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 9325c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 933e2068d0bSJeff Roberson vm_reserv_remove(rv); 9343e00c11aSAlan Cox m = rv->pages; 9353e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 9363e00c11aSAlan Cox for (; m < rv->pages + VM_LEVEL_0_NPAGES; m += VM_SUBLEVEL_0_NPAGES) 9373e00c11aSAlan Cox #endif 9383e00c11aSAlan Cox m->psind = 0; 9390078df5fSDoug Moore pos0 = bit_test(rv->popmap, 0) ? -1 : 0; 9400078df5fSDoug Moore pos1 = -1 - pos0; 9410078df5fSDoug Moore for (pos = 0; pos < VM_LEVEL_0_NPAGES; ) { 9420078df5fSDoug Moore /* Find the first different bit after pos. */ 9430078df5fSDoug Moore bit_ff_at(rv->popmap, pos + 1, VM_LEVEL_0_NPAGES, 9440078df5fSDoug Moore pos1 < pos0, &pos); 94518c47eabSDoug Moore if (pos == -1) 94618c47eabSDoug Moore pos = VM_LEVEL_0_NPAGES; 9470078df5fSDoug Moore if (pos0 < pos1) { 9480078df5fSDoug Moore pos0 = pos; 9490078df5fSDoug Moore continue; 9500078df5fSDoug Moore } 9510078df5fSDoug Moore /* Free unused pages from pos0 to pos. */ 9520078df5fSDoug Moore pos1 = pos; 9535c930c89SJeff Roberson vm_domain_free_lock(VM_DOMAIN(rv->domain)); 9540078df5fSDoug Moore vm_phys_enqueue_contig(&rv->pages[pos0], VM_FREEPOOL_DEFAULT, 9550078df5fSDoug Moore pos1 - pos0); 9565c930c89SJeff Roberson vm_domain_free_unlock(VM_DOMAIN(rv->domain)); 957e67a5068SDoug Moore } 95884e2ae64SDoug Moore bit_nclear(rv->popmap, 0, VM_LEVEL_0_NPAGES - 1); 959e67a5068SDoug Moore rv->popcnt = 0; 9605c930c89SJeff Roberson counter_u64_add(vm_reserv_broken, 1); 961ec179322SAlan Cox } 962ec179322SAlan Cox 963ec179322SAlan Cox /* 964f8a47341SAlan Cox * Breaks all reservations belonging to the given object. 965f8a47341SAlan Cox */ 966f8a47341SAlan Cox void 967f8a47341SAlan Cox vm_reserv_break_all(vm_object_t object) 968f8a47341SAlan Cox { 969f8a47341SAlan Cox vm_reserv_t rv; 970f8a47341SAlan Cox 971e2068d0bSJeff Roberson /* 972e2068d0bSJeff Roberson * This access of object->rvq is unsynchronized so that the 973e2068d0bSJeff Roberson * object rvq lock can nest after the domain_free lock. We 974e2068d0bSJeff Roberson * must check for races in the results. However, the object 975e2068d0bSJeff Roberson * lock prevents new additions, so we are guaranteed that when 976e2068d0bSJeff Roberson * it returns NULL the object is properly empty. 977e2068d0bSJeff Roberson */ 978f8a47341SAlan Cox while ((rv = LIST_FIRST(&object->rvq)) != NULL) { 9795c930c89SJeff Roberson vm_reserv_lock(rv); 980e2068d0bSJeff Roberson /* Reclaim race. */ 9815c930c89SJeff Roberson if (rv->object != object) { 9825c930c89SJeff Roberson vm_reserv_unlock(rv); 983e2068d0bSJeff Roberson continue; 9845c930c89SJeff Roberson } 9855c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 986f8a47341SAlan Cox if (rv->inpartpopq) { 987fe6d5344SMark Johnston TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq); 988f8a47341SAlan Cox rv->inpartpopq = FALSE; 989f8a47341SAlan Cox } 9905c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 991ada27a3bSKonstantin Belousov vm_reserv_break(rv); 9925c930c89SJeff Roberson vm_reserv_unlock(rv); 993f8a47341SAlan Cox } 994f8a47341SAlan Cox } 995f8a47341SAlan Cox 996f8a47341SAlan Cox /* 997f8a47341SAlan Cox * Frees the given page if it belongs to a reservation. Returns TRUE if the 998f8a47341SAlan Cox * page is freed and FALSE otherwise. 999f8a47341SAlan Cox */ 1000f8a47341SAlan Cox boolean_t 1001f8a47341SAlan Cox vm_reserv_free_page(vm_page_t m) 1002f8a47341SAlan Cox { 1003f8a47341SAlan Cox vm_reserv_t rv; 10045c930c89SJeff Roberson boolean_t ret; 1005f8a47341SAlan Cox 1006f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1007908e3da1SAlan Cox if (rv->object == NULL) 1008908e3da1SAlan Cox return (FALSE); 10095c930c89SJeff Roberson vm_reserv_lock(rv); 10105c930c89SJeff Roberson /* Re-validate after lock. */ 10115c930c89SJeff Roberson if (rv->object != NULL) { 1012ec179322SAlan Cox vm_reserv_depopulate(rv, m - rv->pages); 10135c930c89SJeff Roberson ret = TRUE; 10145c930c89SJeff Roberson } else 10155c930c89SJeff Roberson ret = FALSE; 10165c930c89SJeff Roberson vm_reserv_unlock(rv); 10175c930c89SJeff Roberson 10185c930c89SJeff Roberson return (ret); 1019f8a47341SAlan Cox } 1020f8a47341SAlan Cox 1021f8a47341SAlan Cox /* 1022f8a47341SAlan Cox * Initializes the reservation management system. Specifically, initializes 1023f8a47341SAlan Cox * the reservation array. 1024f8a47341SAlan Cox * 1025f8a47341SAlan Cox * Requires that vm_page_array and first_page are initialized! 1026f8a47341SAlan Cox */ 1027f8a47341SAlan Cox void 1028f8a47341SAlan Cox vm_reserv_init(void) 1029f8a47341SAlan Cox { 1030f8a47341SAlan Cox vm_paddr_t paddr; 103109e5f3c4SAlan Cox struct vm_phys_seg *seg; 10325c930c89SJeff Roberson struct vm_reserv *rv; 1033b378d296SMark Johnston struct vm_reserv_domain *rvd; 10347988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 10357988971aSD Scott Phillips vm_pindex_t used; 10367988971aSD Scott Phillips #endif 103784e2ae64SDoug Moore int i, segind; 1038f8a47341SAlan Cox 1039f8a47341SAlan Cox /* 1040f8a47341SAlan Cox * Initialize the reservation array. Specifically, initialize the 1041f8a47341SAlan Cox * "pages" field for every element that has an underlying superpage. 1042f8a47341SAlan Cox */ 10437988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 10447988971aSD Scott Phillips used = 0; 10457988971aSD Scott Phillips #endif 104609e5f3c4SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 104709e5f3c4SAlan Cox seg = &vm_phys_segs[segind]; 10487988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 10497988971aSD Scott Phillips seg->first_reserv = &vm_reserv_array[used]; 10507988971aSD Scott Phillips used += howmany(seg->end, VM_LEVEL_0_SIZE) - 10517988971aSD Scott Phillips seg->start / VM_LEVEL_0_SIZE; 10527988971aSD Scott Phillips #else 10537988971aSD Scott Phillips seg->first_reserv = 10547988971aSD Scott Phillips &vm_reserv_array[seg->start >> VM_LEVEL_0_SHIFT]; 10557988971aSD Scott Phillips #endif 105609e5f3c4SAlan Cox paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 10577988971aSD Scott Phillips rv = seg->first_reserv + (paddr >> VM_LEVEL_0_SHIFT) - 10587988971aSD Scott Phillips (seg->start >> VM_LEVEL_0_SHIFT); 10596b821a74SAleksandr Rybalko while (paddr + VM_LEVEL_0_SIZE > paddr && paddr + 10606b821a74SAleksandr Rybalko VM_LEVEL_0_SIZE <= seg->end) { 10615c930c89SJeff Roberson rv->pages = PHYS_TO_VM_PAGE(paddr); 10625c930c89SJeff Roberson rv->domain = seg->domain; 10635c930c89SJeff Roberson mtx_init(&rv->lock, "vm reserv", NULL, MTX_DEF); 1064f8a47341SAlan Cox paddr += VM_LEVEL_0_SIZE; 10657988971aSD Scott Phillips rv++; 1066f8a47341SAlan Cox } 1067f8a47341SAlan Cox } 10685c930c89SJeff Roberson for (i = 0; i < MAXMEMDOM; i++) { 1069b378d296SMark Johnston rvd = &vm_rvd[i]; 1070b378d296SMark Johnston mtx_init(&rvd->lock, "vm reserv domain", NULL, MTX_DEF); 1071b378d296SMark Johnston TAILQ_INIT(&rvd->partpop); 1072b378d296SMark Johnston mtx_init(&rvd->marker.lock, "vm reserv marker", NULL, MTX_DEF); 1073b378d296SMark Johnston 1074b378d296SMark Johnston /* 1075b378d296SMark Johnston * Fully populated reservations should never be present in the 1076b378d296SMark Johnston * partially populated reservation queues. 1077b378d296SMark Johnston */ 1078b378d296SMark Johnston rvd->marker.popcnt = VM_LEVEL_0_NPAGES; 107984e2ae64SDoug Moore bit_nset(rvd->marker.popmap, 0, VM_LEVEL_0_NPAGES - 1); 1080f8a47341SAlan Cox } 1081f8a47341SAlan Cox 10825c930c89SJeff Roberson for (i = 0; i < VM_RESERV_OBJ_LOCK_COUNT; i++) 10835c930c89SJeff Roberson mtx_init(&vm_reserv_object_mtx[i], "resv obj lock", NULL, 10845c930c89SJeff Roberson MTX_DEF); 10855c930c89SJeff Roberson } 10865c930c89SJeff Roberson 1087f8a47341SAlan Cox /* 1088c869e672SAlan Cox * Returns true if the given page belongs to a reservation and that page is 1089c869e672SAlan Cox * free. Otherwise, returns false. 1090c869e672SAlan Cox */ 1091c869e672SAlan Cox bool 1092c869e672SAlan Cox vm_reserv_is_page_free(vm_page_t m) 1093c869e672SAlan Cox { 1094c869e672SAlan Cox vm_reserv_t rv; 1095c869e672SAlan Cox 1096c869e672SAlan Cox rv = vm_reserv_from_page(m); 1097c869e672SAlan Cox if (rv->object == NULL) 1098c869e672SAlan Cox return (false); 109984e2ae64SDoug Moore return (!bit_test(rv->popmap, m - rv->pages)); 1100c869e672SAlan Cox } 1101c869e672SAlan Cox 1102c869e672SAlan Cox /* 11031526667bSDoug Moore * Returns true if the given page is part of a block of npages, starting at a 11041526667bSDoug Moore * multiple of npages, that are all allocated. Otherwise, returns false. 11051526667bSDoug Moore */ 11061526667bSDoug Moore bool 11071526667bSDoug Moore vm_reserv_is_populated(vm_page_t m, int npages) 11081526667bSDoug Moore { 11091526667bSDoug Moore vm_reserv_t rv; 11101526667bSDoug Moore int index; 11111526667bSDoug Moore 11121526667bSDoug Moore KASSERT(npages <= VM_LEVEL_0_NPAGES, 11131526667bSDoug Moore ("%s: npages %d exceeds VM_LEVEL_0_NPAGES", __func__, npages)); 11141526667bSDoug Moore KASSERT(powerof2(npages), 11151526667bSDoug Moore ("%s: npages %d is not a power of 2", __func__, npages)); 11161526667bSDoug Moore rv = vm_reserv_from_page(m); 11171526667bSDoug Moore if (rv->object == NULL) 11181526667bSDoug Moore return (false); 11191526667bSDoug Moore index = rounddown2(m - rv->pages, npages); 11201526667bSDoug Moore return (bit_ntest(rv->popmap, index, index + npages - 1, 1)); 11211526667bSDoug Moore } 11221526667bSDoug Moore 11231526667bSDoug Moore /* 1124c869e672SAlan Cox * If the given page belongs to a reservation, returns the level of that 1125c869e672SAlan Cox * reservation. Otherwise, returns -1. 1126c869e672SAlan Cox */ 1127c869e672SAlan Cox int 1128c869e672SAlan Cox vm_reserv_level(vm_page_t m) 1129c869e672SAlan Cox { 1130c869e672SAlan Cox vm_reserv_t rv; 1131c869e672SAlan Cox 1132c869e672SAlan Cox rv = vm_reserv_from_page(m); 11333e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 11343e00c11aSAlan Cox return (rv->object != NULL ? 1 : -1); 11353e00c11aSAlan Cox #else 1136c869e672SAlan Cox return (rv->object != NULL ? 0 : -1); 11373e00c11aSAlan Cox #endif 1138c869e672SAlan Cox } 1139c869e672SAlan Cox 1140c869e672SAlan Cox /* 11413453bca8SAlan Cox * Returns a reservation level if the given page belongs to a fully populated 1142f8a47341SAlan Cox * reservation and -1 otherwise. 1143f8a47341SAlan Cox */ 1144f8a47341SAlan Cox int 1145f8a47341SAlan Cox vm_reserv_level_iffullpop(vm_page_t m) 1146f8a47341SAlan Cox { 1147f8a47341SAlan Cox vm_reserv_t rv; 1148f8a47341SAlan Cox 1149f8a47341SAlan Cox rv = vm_reserv_from_page(m); 11503e00c11aSAlan Cox if (rv->popcnt == VM_LEVEL_0_NPAGES) { 11513e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 11523e00c11aSAlan Cox return (1); 11533e00c11aSAlan Cox } else if (rv->pages != NULL && 11543e00c11aSAlan Cox vm_reserv_is_sublevel_full(rv, m - rv->pages)) { 11553e00c11aSAlan Cox #endif 11563e00c11aSAlan Cox return (0); 11573e00c11aSAlan Cox } 11583e00c11aSAlan Cox return (-1); 1159f8a47341SAlan Cox } 1160f8a47341SAlan Cox 1161f8a47341SAlan Cox /* 1162b378d296SMark Johnston * Remove a partially populated reservation from the queue. 1163b378d296SMark Johnston */ 1164b378d296SMark Johnston static void 1165b378d296SMark Johnston vm_reserv_dequeue(vm_reserv_t rv) 1166b378d296SMark Johnston { 1167b378d296SMark Johnston 1168b378d296SMark Johnston vm_reserv_domain_assert_locked(rv->domain); 1169b378d296SMark Johnston vm_reserv_assert_locked(rv); 1170b378d296SMark Johnston CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 1171b378d296SMark Johnston __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 1172b378d296SMark Johnston KASSERT(rv->inpartpopq, 1173b378d296SMark Johnston ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv)); 1174b378d296SMark Johnston 1175b378d296SMark Johnston TAILQ_REMOVE(&vm_rvd[rv->domain].partpop, rv, partpopq); 1176b378d296SMark Johnston rv->inpartpopq = FALSE; 1177b378d296SMark Johnston } 1178b378d296SMark Johnston 1179b378d296SMark Johnston /* 11803453bca8SAlan Cox * Breaks the given partially populated reservation, releasing its free pages 11813453bca8SAlan Cox * to the physical memory allocator. 1182f8a47341SAlan Cox */ 118344aab2c3SAlan Cox static void 118444aab2c3SAlan Cox vm_reserv_reclaim(vm_reserv_t rv) 1185f8a47341SAlan Cox { 1186f8a47341SAlan Cox 11875c930c89SJeff Roberson vm_reserv_assert_locked(rv); 11885c930c89SJeff Roberson CTR5(KTR_VM, "%s: rv %p object %p popcnt %d inpartpop %d", 11895c930c89SJeff Roberson __FUNCTION__, rv, rv->object, rv->popcnt, rv->inpartpopq); 1190b378d296SMark Johnston if (rv->inpartpopq) { 11915c930c89SJeff Roberson vm_reserv_domain_lock(rv->domain); 1192b378d296SMark Johnston vm_reserv_dequeue(rv); 11935c930c89SJeff Roberson vm_reserv_domain_unlock(rv->domain); 1194b378d296SMark Johnston } 1195ada27a3bSKonstantin Belousov vm_reserv_break(rv); 11965c930c89SJeff Roberson counter_u64_add(vm_reserv_reclaimed, 1); 119744aab2c3SAlan Cox } 119844aab2c3SAlan Cox 119944aab2c3SAlan Cox /* 1200b378d296SMark Johnston * Breaks a reservation near the head of the partially populated reservation 12013453bca8SAlan Cox * queue, releasing its free pages to the physical memory allocator. Returns 12023453bca8SAlan Cox * TRUE if a reservation is broken and FALSE otherwise. 120344aab2c3SAlan Cox */ 1204b378d296SMark Johnston bool 1205ef435ae7SJeff Roberson vm_reserv_reclaim_inactive(int domain) 120644aab2c3SAlan Cox { 120744aab2c3SAlan Cox vm_reserv_t rv; 120844aab2c3SAlan Cox 1209b378d296SMark Johnston vm_reserv_domain_lock(domain); 1210b378d296SMark Johnston TAILQ_FOREACH(rv, &vm_rvd[domain].partpop, partpopq) { 1211b378d296SMark Johnston /* 1212b378d296SMark Johnston * A locked reservation is likely being updated or reclaimed, 1213b378d296SMark Johnston * so just skip ahead. 1214b378d296SMark Johnston */ 1215b378d296SMark Johnston if (rv != &vm_rvd[domain].marker && vm_reserv_trylock(rv)) { 1216b378d296SMark Johnston vm_reserv_dequeue(rv); 1217b378d296SMark Johnston break; 12185c930c89SJeff Roberson } 1219b378d296SMark Johnston } 1220b378d296SMark Johnston vm_reserv_domain_unlock(domain); 1221b378d296SMark Johnston if (rv != NULL) { 122244aab2c3SAlan Cox vm_reserv_reclaim(rv); 12235c930c89SJeff Roberson vm_reserv_unlock(rv); 1224b378d296SMark Johnston return (true); 1225f8a47341SAlan Cox } 1226b378d296SMark Johnston return (false); 1227f8a47341SAlan Cox } 1228f8a47341SAlan Cox 1229f8a47341SAlan Cox /* 1230f96e8a0bSDoug Moore * Determine whether this reservation has free pages that satisfy the given 1231f96e8a0bSDoug Moore * request for contiguous physical memory. Start searching from the lower 12326f1c8908SDoug Moore * bound, defined by lo, and stop at the upper bound, hi. Return the index 12336f1c8908SDoug Moore * of the first satisfactory free page, or -1 if none is found. 1234f96e8a0bSDoug Moore */ 12356f1c8908SDoug Moore static int 12366f1c8908SDoug Moore vm_reserv_find_contig(vm_reserv_t rv, int npages, int lo, 12376f1c8908SDoug Moore int hi, int ppn_align, int ppn_bound) 1238f96e8a0bSDoug Moore { 1239f96e8a0bSDoug Moore 1240f96e8a0bSDoug Moore vm_reserv_assert_locked(rv); 12416f1c8908SDoug Moore KASSERT(npages <= VM_LEVEL_0_NPAGES - 1, 12426f1c8908SDoug Moore ("%s: Too many pages", __func__)); 12436f1c8908SDoug Moore KASSERT(ppn_bound <= VM_LEVEL_0_NPAGES, 12446f1c8908SDoug Moore ("%s: Too big a boundary for reservation size", __func__)); 12456f1c8908SDoug Moore KASSERT(npages <= ppn_bound, 12466f1c8908SDoug Moore ("%s: Too many pages for given boundary", __func__)); 12476f1c8908SDoug Moore KASSERT(ppn_align != 0 && powerof2(ppn_align), 12486f1c8908SDoug Moore ("ppn_align is not a positive power of 2")); 12496f1c8908SDoug Moore KASSERT(ppn_bound != 0 && powerof2(ppn_bound), 12506f1c8908SDoug Moore ("ppn_bound is not a positive power of 2")); 125184e2ae64SDoug Moore while (bit_ffc_area_at(rv->popmap, lo, hi, npages, &lo), lo != -1) { 12526f1c8908SDoug Moore if (lo < roundup2(lo, ppn_align)) { 1253f96e8a0bSDoug Moore /* Skip to next aligned page. */ 12546f1c8908SDoug Moore lo = roundup2(lo, ppn_align); 125584e2ae64SDoug Moore } else if (roundup2(lo + 1, ppn_bound) >= lo + npages) 12566f1c8908SDoug Moore return (lo); 125784e2ae64SDoug Moore if (roundup2(lo + 1, ppn_bound) < lo + npages) { 125884e2ae64SDoug Moore /* Skip to next boundary-matching page. */ 125984e2ae64SDoug Moore lo = roundup2(lo + 1, ppn_bound); 1260f96e8a0bSDoug Moore } 126184e2ae64SDoug Moore } 12626f1c8908SDoug Moore return (-1); 1263f96e8a0bSDoug Moore } 1264f96e8a0bSDoug Moore 1265f96e8a0bSDoug Moore /* 12663453bca8SAlan Cox * Searches the partially populated reservation queue for the least recently 12673453bca8SAlan Cox * changed reservation with free pages that satisfy the given request for 12683453bca8SAlan Cox * contiguous physical memory. If a satisfactory reservation is found, it is 1269989a2cf1SMinsoo Choo * broken. Returns a page if a reservation is broken and NULL otherwise. 127044aab2c3SAlan Cox */ 12710d5fac28SDoug Moore vm_page_t 1272ef435ae7SJeff Roberson vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low, 1273ef435ae7SJeff Roberson vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 127444aab2c3SAlan Cox { 1275b378d296SMark Johnston struct vm_reserv_queue *queue; 1276ec179322SAlan Cox vm_paddr_t pa, size; 12770d5fac28SDoug Moore vm_page_t m_ret; 1278b378d296SMark Johnston vm_reserv_t marker, rv, rvn; 12796f1c8908SDoug Moore int hi, lo, posn, ppn_align, ppn_bound; 128044aab2c3SAlan Cox 12816f1c8908SDoug Moore KASSERT(npages > 0, ("npages is 0")); 12826f1c8908SDoug Moore KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 12836f1c8908SDoug Moore KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 1284c68c3537SAlan Cox if (npages > VM_LEVEL_0_NPAGES - 1) 1285989a2cf1SMinsoo Choo return (NULL); 12866f1c8908SDoug Moore size = npages << PAGE_SHIFT; 12876f1c8908SDoug Moore /* 12886f1c8908SDoug Moore * Ensure that a free range starting at a boundary-multiple 12896f1c8908SDoug Moore * doesn't include a boundary-multiple within it. Otherwise, 12906f1c8908SDoug Moore * no boundary-constrained allocation is possible. 12916f1c8908SDoug Moore */ 1292c606ab59SDoug Moore if (!vm_addr_bound_ok(0, size, boundary)) 12930d5fac28SDoug Moore return (NULL); 1294b378d296SMark Johnston marker = &vm_rvd[domain].marker; 1295b378d296SMark Johnston queue = &vm_rvd[domain].partpop; 12966f1c8908SDoug Moore /* 12976f1c8908SDoug Moore * Compute shifted alignment, boundary values for page-based 12986f1c8908SDoug Moore * calculations. Constrain to range [1, VM_LEVEL_0_NPAGES] to 12996f1c8908SDoug Moore * avoid overflow. 13006f1c8908SDoug Moore */ 13016f1c8908SDoug Moore ppn_align = (int)(ulmin(ulmax(PAGE_SIZE, alignment), 13026f1c8908SDoug Moore VM_LEVEL_0_SIZE) >> PAGE_SHIFT); 130349fd2d51SDoug Moore ppn_bound = boundary == 0 ? VM_LEVEL_0_NPAGES : 130449fd2d51SDoug Moore (int)(MIN(MAX(PAGE_SIZE, boundary), 13056f1c8908SDoug Moore VM_LEVEL_0_SIZE) >> PAGE_SHIFT); 1306b378d296SMark Johnston 1307b378d296SMark Johnston vm_reserv_domain_scan_lock(domain); 13085c930c89SJeff Roberson vm_reserv_domain_lock(domain); 1309b378d296SMark Johnston TAILQ_FOREACH_SAFE(rv, queue, partpopq, rvn) { 1310f96e8a0bSDoug Moore pa = VM_PAGE_TO_PHYS(&rv->pages[0]); 1311f96e8a0bSDoug Moore if (pa + VM_LEVEL_0_SIZE - size < low) { 1312ec179322SAlan Cox /* This entire reservation is too low; go to next. */ 131344aab2c3SAlan Cox continue; 131444aab2c3SAlan Cox } 131544aab2c3SAlan Cox if (pa + size > high) { 1316ec179322SAlan Cox /* This entire reservation is too high; go to next. */ 1317ec179322SAlan Cox continue; 131885f2a0c9SMax Laier } 1319c606ab59SDoug Moore if (!vm_addr_align_ok(pa, alignment)) { 13206f1c8908SDoug Moore /* This entire reservation is unaligned; go to next. */ 13216f1c8908SDoug Moore continue; 13226f1c8908SDoug Moore } 1323b378d296SMark Johnston 13245c930c89SJeff Roberson if (vm_reserv_trylock(rv) == 0) { 1325b378d296SMark Johnston TAILQ_INSERT_AFTER(queue, rv, marker, partpopq); 13265c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 13275c930c89SJeff Roberson vm_reserv_lock(rv); 1328968079f2SMark Johnston if (TAILQ_PREV(marker, vm_reserv_queue, partpopq) != 1329968079f2SMark Johnston rv) { 1330b378d296SMark Johnston vm_reserv_unlock(rv); 13315c930c89SJeff Roberson vm_reserv_domain_lock(domain); 1332b378d296SMark Johnston rvn = TAILQ_NEXT(marker, partpopq); 1333b378d296SMark Johnston TAILQ_REMOVE(queue, marker, partpopq); 13345c930c89SJeff Roberson continue; 13355c930c89SJeff Roberson } 1336b378d296SMark Johnston vm_reserv_domain_lock(domain); 1337b378d296SMark Johnston TAILQ_REMOVE(queue, marker, partpopq); 1338b378d296SMark Johnston } 13395c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 13406f1c8908SDoug Moore lo = (pa >= low) ? 0 : 13416f1c8908SDoug Moore (int)((low + PAGE_MASK - pa) >> PAGE_SHIFT); 13426f1c8908SDoug Moore hi = (pa + VM_LEVEL_0_SIZE <= high) ? VM_LEVEL_0_NPAGES : 13436f1c8908SDoug Moore (int)((high - pa) >> PAGE_SHIFT); 13446f1c8908SDoug Moore posn = vm_reserv_find_contig(rv, (int)npages, lo, hi, 13456f1c8908SDoug Moore ppn_align, ppn_bound); 13466f1c8908SDoug Moore if (posn >= 0) { 13470d5fac28SDoug Moore vm_reserv_domain_scan_unlock(domain); 13480d5fac28SDoug Moore /* Allocate requested space */ 13490d5fac28SDoug Moore rv->popcnt += npages; 135084e2ae64SDoug Moore bit_nset(rv->popmap, posn, posn + npages - 1); 13510d5fac28SDoug Moore vm_reserv_reclaim(rv); 13520d5fac28SDoug Moore vm_reserv_unlock(rv); 13530d5fac28SDoug Moore m_ret = &rv->pages[posn]; 13540d5fac28SDoug Moore pa = VM_PAGE_TO_PHYS(m_ret); 1355c606ab59SDoug Moore KASSERT(vm_addr_ok(pa, size, alignment, boundary), 1356c606ab59SDoug Moore ("%s: adjusted address not aligned/bounded to " 1357c606ab59SDoug Moore "%lx/%jx", 1358c606ab59SDoug Moore __func__, alignment, (uintmax_t)boundary)); 13590d5fac28SDoug Moore return (m_ret); 136044aab2c3SAlan Cox } 13615c930c89SJeff Roberson vm_reserv_domain_lock(domain); 1362968079f2SMark Johnston rvn = TAILQ_NEXT(rv, partpopq); 1363968079f2SMark Johnston vm_reserv_unlock(rv); 136444aab2c3SAlan Cox } 13655c930c89SJeff Roberson vm_reserv_domain_unlock(domain); 1366b378d296SMark Johnston vm_reserv_domain_scan_unlock(domain); 13670d5fac28SDoug Moore return (NULL); 136844aab2c3SAlan Cox } 136944aab2c3SAlan Cox 137044aab2c3SAlan Cox /* 1371f8a47341SAlan Cox * Transfers the reservation underlying the given page to a new object. 1372f8a47341SAlan Cox * 1373f8a47341SAlan Cox * The object must be locked. 1374f8a47341SAlan Cox */ 1375f8a47341SAlan Cox void 1376f8a47341SAlan Cox vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object, 1377f8a47341SAlan Cox vm_pindex_t old_object_offset) 1378f8a47341SAlan Cox { 1379f8a47341SAlan Cox vm_reserv_t rv; 1380f8a47341SAlan Cox 138189f6b863SAttilio Rao VM_OBJECT_ASSERT_WLOCKED(new_object); 1382f8a47341SAlan Cox rv = vm_reserv_from_page(m); 1383f8a47341SAlan Cox if (rv->object == old_object) { 13845c930c89SJeff Roberson vm_reserv_lock(rv); 13855c930c89SJeff Roberson CTR6(KTR_VM, 13865c930c89SJeff Roberson "%s: rv %p object %p new %p popcnt %d inpartpop %d", 13875c930c89SJeff Roberson __FUNCTION__, rv, rv->object, new_object, rv->popcnt, 13885c930c89SJeff Roberson rv->inpartpopq); 1389f8a47341SAlan Cox if (rv->object == old_object) { 1390e2068d0bSJeff Roberson vm_reserv_object_lock(old_object); 1391e2068d0bSJeff Roberson rv->object = NULL; 1392f8a47341SAlan Cox LIST_REMOVE(rv, objq); 1393e2068d0bSJeff Roberson vm_reserv_object_unlock(old_object); 1394e2068d0bSJeff Roberson vm_reserv_object_lock(new_object); 1395f8a47341SAlan Cox rv->object = new_object; 1396f8a47341SAlan Cox rv->pindex -= old_object_offset; 1397e2068d0bSJeff Roberson LIST_INSERT_HEAD(&new_object->rvq, rv, objq); 1398e2068d0bSJeff Roberson vm_reserv_object_unlock(new_object); 1399f8a47341SAlan Cox } 14005c930c89SJeff Roberson vm_reserv_unlock(rv); 1401f8a47341SAlan Cox } 1402f8a47341SAlan Cox } 1403f8a47341SAlan Cox 1404f8a47341SAlan Cox /* 1405c869e672SAlan Cox * Returns the size (in bytes) of a reservation of the specified level. 1406c869e672SAlan Cox */ 1407c869e672SAlan Cox int 1408c869e672SAlan Cox vm_reserv_size(int level) 1409c869e672SAlan Cox { 1410c869e672SAlan Cox 1411c869e672SAlan Cox switch (level) { 1412c869e672SAlan Cox case 0: 14133e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 14143e00c11aSAlan Cox return (VM_SUBLEVEL_0_NPAGES * PAGE_SIZE); 14153e00c11aSAlan Cox case 1: 14163e00c11aSAlan Cox #endif 1417c869e672SAlan Cox return (VM_LEVEL_0_SIZE); 1418c869e672SAlan Cox case -1: 1419c869e672SAlan Cox return (PAGE_SIZE); 1420c869e672SAlan Cox default: 1421c869e672SAlan Cox return (0); 1422c869e672SAlan Cox } 1423c869e672SAlan Cox } 1424c869e672SAlan Cox 1425c869e672SAlan Cox /* 1426f8a47341SAlan Cox * Allocates the virtual and physical memory required by the reservation 1427f8a47341SAlan Cox * management system's data structures, in particular, the reservation array. 1428f8a47341SAlan Cox */ 1429f8a47341SAlan Cox vm_paddr_t 14303e5e1b51SJeff Roberson vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end) 1431f8a47341SAlan Cox { 14327988971aSD Scott Phillips vm_paddr_t new_end; 14337988971aSD Scott Phillips vm_pindex_t count; 1434f8a47341SAlan Cox size_t size; 14353e5e1b51SJeff Roberson int i; 14363e5e1b51SJeff Roberson 14377988971aSD Scott Phillips count = 0; 14383e5e1b51SJeff Roberson for (i = 0; i < vm_phys_nsegs; i++) { 14397988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 14407988971aSD Scott Phillips count += howmany(vm_phys_segs[i].end, VM_LEVEL_0_SIZE) - 14417988971aSD Scott Phillips vm_phys_segs[i].start / VM_LEVEL_0_SIZE; 14427988971aSD Scott Phillips #else 14437988971aSD Scott Phillips count = MAX(count, 14447988971aSD Scott Phillips howmany(vm_phys_segs[i].end, VM_LEVEL_0_SIZE)); 14457988971aSD Scott Phillips #endif 14463e5e1b51SJeff Roberson } 14473e5e1b51SJeff Roberson 14487988971aSD Scott Phillips for (i = 0; phys_avail[i + 1] != 0; i += 2) { 14497988971aSD Scott Phillips #ifdef VM_PHYSSEG_SPARSE 14507988971aSD Scott Phillips count += howmany(phys_avail[i + 1], VM_LEVEL_0_SIZE) - 14517988971aSD Scott Phillips phys_avail[i] / VM_LEVEL_0_SIZE; 14527988971aSD Scott Phillips #else 14537988971aSD Scott Phillips count = MAX(count, 14547988971aSD Scott Phillips howmany(phys_avail[i + 1], VM_LEVEL_0_SIZE)); 14557988971aSD Scott Phillips #endif 14563e5e1b51SJeff Roberson } 1457f8a47341SAlan Cox 1458f8a47341SAlan Cox /* 14597988971aSD Scott Phillips * Calculate the size (in bytes) of the reservation array. Rounding up 14607988971aSD Scott Phillips * for partial superpages at boundaries, as every small page is mapped 14617988971aSD Scott Phillips * to an element in the reservation array based on its physical address. 14627988971aSD Scott Phillips * Thus, the number of elements in the reservation array can be greater 14637988971aSD Scott Phillips * than the number of superpages. 1464f8a47341SAlan Cox */ 14657988971aSD Scott Phillips size = count * sizeof(struct vm_reserv); 1466f8a47341SAlan Cox 1467f8a47341SAlan Cox /* 1468f8a47341SAlan Cox * Allocate and map the physical memory for the reservation array. The 1469f8a47341SAlan Cox * next available virtual address is returned by reference. 1470f8a47341SAlan Cox */ 1471f8a47341SAlan Cox new_end = end - round_page(size); 1472f8a47341SAlan Cox vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end, 1473f8a47341SAlan Cox VM_PROT_READ | VM_PROT_WRITE); 1474f8a47341SAlan Cox bzero(vm_reserv_array, size); 1475f8a47341SAlan Cox 1476f8a47341SAlan Cox /* 1477f8a47341SAlan Cox * Return the next available physical address. 1478f8a47341SAlan Cox */ 1479f8a47341SAlan Cox return (new_end); 1480f8a47341SAlan Cox } 1481f8a47341SAlan Cox 14828b5e1472SAlan Cox /* 14838b5e1472SAlan Cox * Returns the superpage containing the given page. 14848b5e1472SAlan Cox */ 14858b5e1472SAlan Cox vm_page_t 14868b5e1472SAlan Cox vm_reserv_to_superpage(vm_page_t m) 14878b5e1472SAlan Cox { 14888b5e1472SAlan Cox vm_reserv_t rv; 14898b5e1472SAlan Cox 14908b5e1472SAlan Cox VM_OBJECT_ASSERT_LOCKED(m->object); 14918b5e1472SAlan Cox rv = vm_reserv_from_page(m); 14923e00c11aSAlan Cox if (rv->object == m->object) { 14933e00c11aSAlan Cox if (rv->popcnt == VM_LEVEL_0_NPAGES) 14943e00c11aSAlan Cox return (rv->pages); 14953e00c11aSAlan Cox #ifdef VM_SUBLEVEL_0_NPAGES 14963e00c11aSAlan Cox if (vm_reserv_is_sublevel_full(rv, m - rv->pages)) 14973e00c11aSAlan Cox return (rv->pages + rounddown2(m - rv->pages, 14983e00c11aSAlan Cox VM_SUBLEVEL_0_NPAGES)); 14993e00c11aSAlan Cox #endif 15003e00c11aSAlan Cox } 15013e00c11aSAlan Cox return (NULL); 15028b5e1472SAlan Cox } 15038b5e1472SAlan Cox 1504f8a47341SAlan Cox #endif /* VM_NRESERVLEVEL > 0 */ 1505