1174982Salc/*- 2174982Salc * Copyright (c) 2002-2006 Rice University 3177956Salc * Copyright (c) 2007-2008 Alan L. Cox <alc@cs.rice.edu> 4174982Salc * All rights reserved. 5174982Salc * 6174982Salc * This software was developed for the FreeBSD Project by Alan L. Cox, 7174982Salc * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro. 8174982Salc * 9174982Salc * Redistribution and use in source and binary forms, with or without 10174982Salc * modification, are permitted provided that the following conditions 11174982Salc * are met: 12174982Salc * 1. Redistributions of source code must retain the above copyright 13174982Salc * notice, this list of conditions and the following disclaimer. 14174982Salc * 2. Redistributions in binary form must reproduce the above copyright 15174982Salc * notice, this list of conditions and the following disclaimer in the 16174982Salc * documentation and/or other materials provided with the distribution. 17174982Salc * 18174982Salc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19174982Salc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20174982Salc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21174982Salc * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22174982Salc * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23174982Salc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24174982Salc * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 25174982Salc * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26174982Salc * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27174982Salc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 28174982Salc * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29174982Salc * POSSIBILITY OF SUCH DAMAGE. 30174982Salc */ 31174982Salc 32174982Salc/* 33174982Salc * Superpage reservation management module 34228287Salc * 35228287Salc * Any external functions defined by this module are only to be used by the 36228287Salc * virtual memory system. 37174982Salc */ 38174982Salc 39174982Salc#include <sys/cdefs.h> 40174982Salc__FBSDID("$FreeBSD$"); 41174982Salc 42174982Salc#include "opt_vm.h" 43174982Salc 44174982Salc#include <sys/param.h> 45174982Salc#include <sys/kernel.h> 46174982Salc#include <sys/lock.h> 47174982Salc#include <sys/malloc.h> 48174982Salc#include <sys/mutex.h> 49174982Salc#include <sys/queue.h> 50248084Sattilio#include <sys/rwlock.h> 51174982Salc#include <sys/sbuf.h> 52174982Salc#include <sys/sysctl.h> 53174982Salc#include <sys/systm.h> 54174982Salc 55174982Salc#include <vm/vm.h> 56174982Salc#include <vm/vm_param.h> 57174982Salc#include <vm/vm_object.h> 58174982Salc#include <vm/vm_page.h> 59174982Salc#include <vm/vm_phys.h> 60248449Sattilio#include <vm/vm_radix.h> 61174982Salc#include <vm/vm_reserv.h> 62174982Salc 63174982Salc/* 64174982Salc * The reservation system supports the speculative allocation of large physical 65174982Salc * pages ("superpages"). Speculative allocation enables the fully-automatic 66174982Salc * utilization of superpages by the virtual memory system. In other words, no 67174982Salc * programmatic directives are required to use superpages. 68174982Salc */ 69174982Salc 70174982Salc#if VM_NRESERVLEVEL > 0 71174982Salc 72174982Salc/* 73174982Salc * The number of small pages that are contained in a level 0 reservation 74174982Salc */ 75174982Salc#define VM_LEVEL_0_NPAGES (1 << VM_LEVEL_0_ORDER) 76174982Salc 77174982Salc/* 78174982Salc * The number of bits by which a physical address is shifted to obtain the 79174982Salc * reservation number 80174982Salc */ 81174982Salc#define VM_LEVEL_0_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT) 82174982Salc 83174982Salc/* 84174982Salc * The size of a level 0 reservation in bytes 85174982Salc */ 86174982Salc#define VM_LEVEL_0_SIZE (1 << VM_LEVEL_0_SHIFT) 87174982Salc 88174982Salc/* 89174982Salc * Computes the index of the small page underlying the given (object, pindex) 90174982Salc * within the reservation's array of small pages. 91174982Salc */ 92174982Salc#define VM_RESERV_INDEX(object, pindex) \ 93174982Salc (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1)) 94174982Salc 95174982Salc/* 96174982Salc * The reservation structure 97174982Salc * 98174982Salc * A reservation structure is constructed whenever a large physical page is 99174982Salc * speculatively allocated to an object. The reservation provides the small 100174982Salc * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets 101174982Salc * within that object. The reservation's "popcnt" tracks the number of these 102174982Salc * small physical pages that are in use at any given time. When and if the 103174982Salc * reservation is not fully utilized, it appears in the queue of partially- 104174982Salc * populated reservations. The reservation always appears on the containing 105174982Salc * object's list of reservations. 106174982Salc * 107174982Salc * A partially-populated reservation can be broken and reclaimed at any time. 108174982Salc */ 109174982Salcstruct vm_reserv { 110174982Salc TAILQ_ENTRY(vm_reserv) partpopq; 111174982Salc LIST_ENTRY(vm_reserv) objq; 112174982Salc vm_object_t object; /* containing object */ 113174982Salc vm_pindex_t pindex; /* offset within object */ 114174982Salc vm_page_t pages; /* first page of a superpage */ 115174982Salc int popcnt; /* # of pages in use */ 116174982Salc char inpartpopq; 117174982Salc}; 118174982Salc 119174982Salc/* 120174982Salc * The reservation array 121174982Salc * 122174982Salc * This array is analoguous in function to vm_page_array. It differs in the 123174982Salc * respect that it may contain a greater number of useful reservation 124174982Salc * structures than there are (physical) superpages. These "invalid" 125174982Salc * reservation structures exist to trade-off space for time in the 126174982Salc * implementation of vm_reserv_from_page(). Invalid reservation structures are 127174982Salc * distinguishable from "valid" reservation structures by inspecting the 128174982Salc * reservation's "pages" field. Invalid reservation structures have a NULL 129174982Salc * "pages" field. 130174982Salc * 131174982Salc * vm_reserv_from_page() maps a small (physical) page to an element of this 132174982Salc * array by computing a physical reservation number from the page's physical 133174982Salc * address. The physical reservation number is used as the array index. 134174982Salc * 135174982Salc * An "active" reservation is a valid reservation structure that has a non-NULL 136174982Salc * "object" field and a non-zero "popcnt" field. In other words, every active 137174982Salc * reservation belongs to a particular object. Moreover, every active 138174982Salc * reservation has an entry in the containing object's list of reservations. 139174982Salc */ 140174982Salcstatic vm_reserv_t vm_reserv_array; 141174982Salc 142174982Salc/* 143174982Salc * The partially-populated reservation queue 144174982Salc * 145174982Salc * This queue enables the fast recovery of an unused cached or free small page 146190912Salc * from a partially-populated reservation. The reservation at the head of 147190912Salc * this queue is the least-recently-changed, partially-populated reservation. 148174982Salc * 149174982Salc * Access to this queue is synchronized by the free page queue lock. 150174982Salc */ 151174982Salcstatic TAILQ_HEAD(, vm_reserv) vm_rvq_partpop = 152174982Salc TAILQ_HEAD_INITIALIZER(vm_rvq_partpop); 153174982Salc 154174982Salcstatic SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info"); 155174982Salc 156174982Salcstatic long vm_reserv_broken; 157174982SalcSYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD, 158174982Salc &vm_reserv_broken, 0, "Cumulative number of broken reservations"); 159174982Salc 160174982Salcstatic long vm_reserv_freed; 161174982SalcSYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD, 162174982Salc &vm_reserv_freed, 0, "Cumulative number of freed reservations"); 163174982Salc 164174982Salcstatic int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS); 165174982Salc 166174982SalcSYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, 167174982Salc sysctl_vm_reserv_partpopq, "A", "Partially-populated reservation queues"); 168174982Salc 169174982Salcstatic long vm_reserv_reclaimed; 170174982SalcSYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD, 171174982Salc &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations"); 172174982Salc 173174982Salcstatic void vm_reserv_depopulate(vm_reserv_t rv); 174174982Salcstatic vm_reserv_t vm_reserv_from_page(vm_page_t m); 175174982Salcstatic boolean_t vm_reserv_has_pindex(vm_reserv_t rv, 176174982Salc vm_pindex_t pindex); 177174982Salcstatic void vm_reserv_populate(vm_reserv_t rv); 178177956Salcstatic void vm_reserv_reclaim(vm_reserv_t rv); 179174982Salc 180174982Salc/* 181174982Salc * Describes the current state of the partially-populated reservation queue. 182174982Salc */ 183174982Salcstatic int 184174982Salcsysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS) 185174982Salc{ 186174982Salc struct sbuf sbuf; 187174982Salc vm_reserv_t rv; 188174982Salc int counter, error, level, unused_pages; 189174982Salc 190217916Smdf error = sysctl_wire_old_buffer(req, 0); 191217916Smdf if (error != 0) 192217916Smdf return (error); 193212750Smdf sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 194174982Salc sbuf_printf(&sbuf, "\nLEVEL SIZE NUMBER\n\n"); 195174982Salc for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) { 196174982Salc counter = 0; 197174982Salc unused_pages = 0; 198174982Salc mtx_lock(&vm_page_queue_free_mtx); 199174982Salc TAILQ_FOREACH(rv, &vm_rvq_partpop/*[level]*/, partpopq) { 200174982Salc counter++; 201174982Salc unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt; 202174982Salc } 203174982Salc mtx_unlock(&vm_page_queue_free_mtx); 204214564Salc sbuf_printf(&sbuf, "%5d: %6dK, %6d\n", level, 205215093Salc unused_pages * ((int)PAGE_SIZE / 1024), counter); 206174982Salc } 207212750Smdf error = sbuf_finish(&sbuf); 208174982Salc sbuf_delete(&sbuf); 209174982Salc return (error); 210174982Salc} 211174982Salc 212174982Salc/* 213174982Salc * Reduces the given reservation's population count. If the population count 214174982Salc * becomes zero, the reservation is destroyed. Additionally, moves the 215190912Salc * reservation to the tail of the partially-populated reservations queue if the 216174982Salc * population count is non-zero. 217174982Salc * 218174982Salc * The free page queue lock must be held. 219174982Salc */ 220174982Salcstatic void 221174982Salcvm_reserv_depopulate(vm_reserv_t rv) 222174982Salc{ 223174982Salc 224174982Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 225174982Salc KASSERT(rv->object != NULL, 226174982Salc ("vm_reserv_depopulate: reserv %p is free", rv)); 227174982Salc KASSERT(rv->popcnt > 0, 228174982Salc ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv)); 229174982Salc if (rv->inpartpopq) { 230174982Salc TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 231174982Salc rv->inpartpopq = FALSE; 232269072Skib } else { 233269072Skib KASSERT(rv->pages->psind == 1, 234269072Skib ("vm_reserv_depopulate: reserv %p is already demoted", 235269072Skib rv)); 236269072Skib rv->pages->psind = 0; 237174982Salc } 238174982Salc rv->popcnt--; 239174982Salc if (rv->popcnt == 0) { 240174982Salc LIST_REMOVE(rv, objq); 241174982Salc rv->object = NULL; 242174982Salc vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER); 243174982Salc vm_reserv_freed++; 244174982Salc } else { 245174982Salc rv->inpartpopq = TRUE; 246190912Salc TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq); 247174982Salc } 248174982Salc} 249174982Salc 250174982Salc/* 251174982Salc * Returns the reservation to which the given page might belong. 252174982Salc */ 253174982Salcstatic __inline vm_reserv_t 254174982Salcvm_reserv_from_page(vm_page_t m) 255174982Salc{ 256174982Salc 257174982Salc return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]); 258174982Salc} 259174982Salc 260174982Salc/* 261174982Salc * Returns TRUE if the given reservation contains the given page index and 262174982Salc * FALSE otherwise. 263174982Salc */ 264174982Salcstatic __inline boolean_t 265174982Salcvm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex) 266174982Salc{ 267174982Salc 268174982Salc return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0); 269174982Salc} 270174982Salc 271174982Salc/* 272174982Salc * Increases the given reservation's population count. Moves the reservation 273174982Salc * to the tail of the partially-populated reservation queue. 274174982Salc * 275174982Salc * The free page queue must be locked. 276174982Salc */ 277174982Salcstatic void 278174982Salcvm_reserv_populate(vm_reserv_t rv) 279174982Salc{ 280174982Salc 281174982Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 282174982Salc KASSERT(rv->object != NULL, 283174982Salc ("vm_reserv_populate: reserv %p is free", rv)); 284174982Salc KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES, 285174982Salc ("vm_reserv_populate: reserv %p is already full", rv)); 286269072Skib KASSERT(rv->pages->psind == 0, 287269072Skib ("vm_reserv_populate: reserv %p is already promoted", rv)); 288174982Salc if (rv->inpartpopq) { 289174982Salc TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 290174982Salc rv->inpartpopq = FALSE; 291174982Salc } 292174982Salc rv->popcnt++; 293174982Salc if (rv->popcnt < VM_LEVEL_0_NPAGES) { 294174982Salc rv->inpartpopq = TRUE; 295174982Salc TAILQ_INSERT_TAIL(&vm_rvq_partpop, rv, partpopq); 296269072Skib } else 297269072Skib rv->pages->psind = 1; 298174982Salc} 299174982Salc 300174982Salc/* 301228287Salc * Allocates a contiguous set of physical pages of the given size "npages" 302272543Salc * from existing or newly created reservations. All of the physical pages 303228287Salc * must be at or above the given physical address "low" and below the given 304228287Salc * physical address "high". The given value "alignment" determines the 305228287Salc * alignment of the first physical page in the set. If the given value 306228287Salc * "boundary" is non-zero, then the set of physical pages cannot cross any 307228287Salc * physical address boundary that is a multiple of that value. Both 308228287Salc * "alignment" and "boundary" must be a power of two. 309174982Salc * 310174982Salc * The object and free page queue must be locked. 311174982Salc */ 312174982Salcvm_page_t 313228287Salcvm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, u_long npages, 314228287Salc vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 315174982Salc{ 316228287Salc vm_paddr_t pa, size; 317228287Salc vm_page_t m, m_ret, mpred, msucc; 318174982Salc vm_pindex_t first, leftcap, rightcap; 319174982Salc vm_reserv_t rv; 320228287Salc u_long allocpages, maxpages, minpages; 321228287Salc int i, index, n; 322174982Salc 323174982Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 324248084Sattilio VM_OBJECT_ASSERT_WLOCKED(object); 325228287Salc KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0")); 326174982Salc 327174982Salc /* 328228287Salc * Is a reservation fundamentally impossible? 329174982Salc */ 330174982Salc if (pindex < VM_RESERV_INDEX(object, pindex) || 331228287Salc pindex + npages > object->size) 332174982Salc return (NULL); 333174982Salc 334174982Salc /* 335228287Salc * All reservations of a particular size have the same alignment. 336228287Salc * Assuming that the first page is allocated from a reservation, the 337228287Salc * least significant bits of its physical address can be determined 338228287Salc * from its offset from the beginning of the reservation and the size 339228287Salc * of the reservation. 340228287Salc * 341228287Salc * Could the specified index within a reservation of the smallest 342228287Salc * possible size satisfy the alignment and boundary requirements? 343228287Salc */ 344228287Salc pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT; 345228287Salc if ((pa & (alignment - 1)) != 0) 346228287Salc return (NULL); 347228287Salc size = npages << PAGE_SHIFT; 348228287Salc if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 349228287Salc return (NULL); 350228287Salc 351228287Salc /* 352174982Salc * Look for an existing reservation. 353174982Salc */ 354248449Sattilio mpred = vm_radix_lookup_le(&object->rtree, pindex); 355248449Sattilio if (mpred != NULL) { 356248449Sattilio KASSERT(mpred->pindex < pindex, 357228287Salc ("vm_reserv_alloc_contig: pindex already allocated")); 358174982Salc rv = vm_reserv_from_page(mpred); 359228287Salc if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 360228287Salc goto found; 361248449Sattilio msucc = TAILQ_NEXT(mpred, listq); 362248449Sattilio } else 363248449Sattilio msucc = TAILQ_FIRST(&object->memq); 364248449Sattilio if (msucc != NULL) { 365248449Sattilio KASSERT(msucc->pindex > pindex, 366280045Skib ("vm_reserv_alloc_contig: pindex already allocated")); 367248449Sattilio rv = vm_reserv_from_page(msucc); 368248449Sattilio if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 369248449Sattilio goto found; 370174982Salc } 371174982Salc 372174982Salc /* 373228287Salc * Could at least one reservation fit between the first index to the 374272543Salc * left that can be used ("leftcap") and the first index to the right 375272543Salc * that cannot be used ("rightcap")? 376174982Salc */ 377174982Salc first = pindex - VM_RESERV_INDEX(object, pindex); 378228287Salc if (mpred != NULL) { 379228287Salc if ((rv = vm_reserv_from_page(mpred))->object != object) 380228287Salc leftcap = mpred->pindex + 1; 381228287Salc else 382228287Salc leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 383228287Salc if (leftcap > first) 384228287Salc return (NULL); 385228287Salc } 386228287Salc minpages = VM_RESERV_INDEX(object, pindex) + npages; 387228287Salc maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES); 388228287Salc allocpages = maxpages; 389228287Salc if (msucc != NULL) { 390228287Salc if ((rv = vm_reserv_from_page(msucc))->object != object) 391228287Salc rightcap = msucc->pindex; 392228287Salc else 393228287Salc rightcap = rv->pindex; 394228287Salc if (first + maxpages > rightcap) { 395228287Salc if (maxpages == VM_LEVEL_0_NPAGES) 396228287Salc return (NULL); 397272543Salc 398272543Salc /* 399272543Salc * At least one reservation will fit between "leftcap" 400272543Salc * and "rightcap". However, a reservation for the 401272543Salc * last of the requested pages will not fit. Reduce 402272543Salc * the size of the upcoming allocation accordingly. 403272543Salc */ 404228287Salc allocpages = minpages; 405228287Salc } 406228287Salc } 407174982Salc 408174982Salc /* 409228287Salc * Would the last new reservation extend past the end of the object? 410174982Salc */ 411228287Salc if (first + maxpages > object->size) { 412174982Salc /* 413228287Salc * Don't allocate the last new reservation if the object is a 414228287Salc * vnode or backed by another object that is a vnode. 415174982Salc */ 416174982Salc if (object->type == OBJT_VNODE || 417174982Salc (object->backing_object != NULL && 418228287Salc object->backing_object->type == OBJT_VNODE)) { 419228287Salc if (maxpages == VM_LEVEL_0_NPAGES) 420228287Salc return (NULL); 421228287Salc allocpages = minpages; 422228287Salc } 423174982Salc /* Speculate that the object may grow. */ 424174982Salc } 425174982Salc 426174982Salc /* 427272543Salc * Allocate the physical pages. The alignment and boundary specified 428272543Salc * for this allocation may be different from the alignment and 429272543Salc * boundary specified for the requested pages. For instance, the 430272543Salc * specified index may not be the first page within the first new 431272543Salc * reservation. 432174982Salc */ 433228287Salc m = vm_phys_alloc_contig(allocpages, low, high, ulmax(alignment, 434228287Salc VM_LEVEL_0_SIZE), boundary > VM_LEVEL_0_SIZE ? boundary : 0); 435228287Salc if (m == NULL) 436228287Salc return (NULL); 437272543Salc 438272543Salc /* 439272543Salc * The allocated physical pages always begin at a reservation 440272543Salc * boundary, but they do not always end at a reservation boundary. 441272543Salc * Initialize every reservation that is completely covered by the 442272543Salc * allocated physical pages. 443272543Salc */ 444228287Salc m_ret = NULL; 445228287Salc index = VM_RESERV_INDEX(object, pindex); 446228287Salc do { 447174982Salc rv = vm_reserv_from_page(m); 448174982Salc KASSERT(rv->pages == m, 449228287Salc ("vm_reserv_alloc_contig: reserv %p's pages is corrupted", 450174982Salc rv)); 451174982Salc KASSERT(rv->object == NULL, 452228287Salc ("vm_reserv_alloc_contig: reserv %p isn't free", rv)); 453174982Salc LIST_INSERT_HEAD(&object->rvq, rv, objq); 454174982Salc rv->object = object; 455174982Salc rv->pindex = first; 456174982Salc KASSERT(rv->popcnt == 0, 457228287Salc ("vm_reserv_alloc_contig: reserv %p's popcnt is corrupted", 458174982Salc rv)); 459174982Salc KASSERT(!rv->inpartpopq, 460228287Salc ("vm_reserv_alloc_contig: reserv %p's inpartpopq is TRUE", 461174982Salc rv)); 462228287Salc n = ulmin(VM_LEVEL_0_NPAGES - index, npages); 463228287Salc for (i = 0; i < n; i++) 464228287Salc vm_reserv_populate(rv); 465228287Salc npages -= n; 466228287Salc if (m_ret == NULL) { 467228287Salc m_ret = &rv->pages[index]; 468228287Salc index = 0; 469228287Salc } 470228287Salc m += VM_LEVEL_0_NPAGES; 471228287Salc first += VM_LEVEL_0_NPAGES; 472228287Salc allocpages -= VM_LEVEL_0_NPAGES; 473272543Salc } while (allocpages >= VM_LEVEL_0_NPAGES); 474228287Salc return (m_ret); 475228287Salc 476228287Salc /* 477228287Salc * Found a matching reservation. 478228287Salc */ 479228287Salcfound: 480228287Salc index = VM_RESERV_INDEX(object, pindex); 481228287Salc /* Does the allocation fit within the reservation? */ 482228287Salc if (index + npages > VM_LEVEL_0_NPAGES) 483228287Salc return (NULL); 484228287Salc m = &rv->pages[index]; 485228287Salc pa = VM_PAGE_TO_PHYS(m); 486228287Salc if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 || 487228287Salc ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 488228287Salc return (NULL); 489228287Salc /* Handle vm_page_rename(m, new_object, ...). */ 490228287Salc for (i = 0; i < npages; i++) 491228287Salc if ((rv->pages[index + i].flags & (PG_CACHED | PG_FREE)) == 0) 492228287Salc return (NULL); 493228287Salc for (i = 0; i < npages; i++) 494174982Salc vm_reserv_populate(rv); 495228287Salc return (m); 496228287Salc} 497228287Salc 498228287Salc/* 499228287Salc * Allocates a page from an existing or newly-created reservation. 500228287Salc * 501250577Salc * The page "mpred" must immediately precede the offset "pindex" within the 502250577Salc * specified object. 503250577Salc * 504228287Salc * The object and free page queue must be locked. 505228287Salc */ 506228287Salcvm_page_t 507250577Salcvm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, vm_page_t mpred) 508228287Salc{ 509250577Salc vm_page_t m, msucc; 510228287Salc vm_pindex_t first, leftcap, rightcap; 511228287Salc vm_reserv_t rv; 512228287Salc 513228287Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 514248084Sattilio VM_OBJECT_ASSERT_WLOCKED(object); 515228287Salc 516228287Salc /* 517228287Salc * Is a reservation fundamentally impossible? 518228287Salc */ 519228287Salc if (pindex < VM_RESERV_INDEX(object, pindex) || 520228287Salc pindex >= object->size) 521228287Salc return (NULL); 522228287Salc 523228287Salc /* 524228287Salc * Look for an existing reservation. 525228287Salc */ 526248449Sattilio if (mpred != NULL) { 527255626Skib KASSERT(mpred->object == object, 528250577Salc ("vm_reserv_alloc_page: object doesn't contain mpred")); 529248449Sattilio KASSERT(mpred->pindex < pindex, 530250577Salc ("vm_reserv_alloc_page: mpred doesn't precede pindex")); 531228287Salc rv = vm_reserv_from_page(mpred); 532228287Salc if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 533228287Salc goto found; 534248449Sattilio msucc = TAILQ_NEXT(mpred, listq); 535248449Sattilio } else 536248449Sattilio msucc = TAILQ_FIRST(&object->memq); 537248449Sattilio if (msucc != NULL) { 538248449Sattilio KASSERT(msucc->pindex > pindex, 539250577Salc ("vm_reserv_alloc_page: msucc doesn't succeed pindex")); 540248449Sattilio rv = vm_reserv_from_page(msucc); 541248449Sattilio if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 542248449Sattilio goto found; 543174982Salc } 544228287Salc 545228287Salc /* 546228287Salc * Could a reservation fit between the first index to the left that 547228287Salc * can be used and the first index to the right that cannot be used? 548228287Salc */ 549228287Salc first = pindex - VM_RESERV_INDEX(object, pindex); 550228287Salc if (mpred != NULL) { 551228287Salc if ((rv = vm_reserv_from_page(mpred))->object != object) 552228287Salc leftcap = mpred->pindex + 1; 553228287Salc else 554228287Salc leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 555228287Salc if (leftcap > first) 556228287Salc return (NULL); 557228287Salc } 558228287Salc if (msucc != NULL) { 559228287Salc if ((rv = vm_reserv_from_page(msucc))->object != object) 560228287Salc rightcap = msucc->pindex; 561228287Salc else 562228287Salc rightcap = rv->pindex; 563228287Salc if (first + VM_LEVEL_0_NPAGES > rightcap) 564228287Salc return (NULL); 565228287Salc } 566228287Salc 567228287Salc /* 568228287Salc * Would a new reservation extend past the end of the object? 569228287Salc */ 570228287Salc if (first + VM_LEVEL_0_NPAGES > object->size) { 571228287Salc /* 572228287Salc * Don't allocate a new reservation if the object is a vnode or 573228287Salc * backed by another object that is a vnode. 574228287Salc */ 575228287Salc if (object->type == OBJT_VNODE || 576228287Salc (object->backing_object != NULL && 577228287Salc object->backing_object->type == OBJT_VNODE)) 578228287Salc return (NULL); 579228287Salc /* Speculate that the object may grow. */ 580228287Salc } 581228287Salc 582228287Salc /* 583228287Salc * Allocate and populate the new reservation. 584228287Salc */ 585228287Salc m = vm_phys_alloc_pages(VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER); 586228287Salc if (m == NULL) 587228287Salc return (NULL); 588228287Salc rv = vm_reserv_from_page(m); 589228287Salc KASSERT(rv->pages == m, 590228287Salc ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv)); 591228287Salc KASSERT(rv->object == NULL, 592228287Salc ("vm_reserv_alloc_page: reserv %p isn't free", rv)); 593228287Salc LIST_INSERT_HEAD(&object->rvq, rv, objq); 594228287Salc rv->object = object; 595228287Salc rv->pindex = first; 596228287Salc KASSERT(rv->popcnt == 0, 597228287Salc ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted", rv)); 598228287Salc KASSERT(!rv->inpartpopq, 599228287Salc ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE", rv)); 600228287Salc vm_reserv_populate(rv); 601228287Salc return (&rv->pages[VM_RESERV_INDEX(object, pindex)]); 602228287Salc 603228287Salc /* 604228287Salc * Found a matching reservation. 605228287Salc */ 606228287Salcfound: 607228287Salc m = &rv->pages[VM_RESERV_INDEX(object, pindex)]; 608228287Salc /* Handle vm_page_rename(m, new_object, ...). */ 609228287Salc if ((m->flags & (PG_CACHED | PG_FREE)) == 0) 610228287Salc return (NULL); 611228287Salc vm_reserv_populate(rv); 612174982Salc return (m); 613174982Salc} 614174982Salc 615174982Salc/* 616174982Salc * Breaks all reservations belonging to the given object. 617174982Salc */ 618174982Salcvoid 619174982Salcvm_reserv_break_all(vm_object_t object) 620174982Salc{ 621174982Salc vm_reserv_t rv; 622174982Salc int i; 623174982Salc 624174982Salc mtx_lock(&vm_page_queue_free_mtx); 625174982Salc while ((rv = LIST_FIRST(&object->rvq)) != NULL) { 626174982Salc KASSERT(rv->object == object, 627174982Salc ("vm_reserv_break_all: reserv %p is corrupted", rv)); 628174982Salc if (rv->inpartpopq) { 629174982Salc TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 630174982Salc rv->inpartpopq = FALSE; 631174982Salc } 632174982Salc LIST_REMOVE(rv, objq); 633174982Salc rv->object = NULL; 634174982Salc for (i = 0; i < VM_LEVEL_0_NPAGES; i++) { 635174982Salc if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0) 636174982Salc vm_phys_free_pages(&rv->pages[i], 0); 637174982Salc else 638174982Salc rv->popcnt--; 639174982Salc } 640174982Salc KASSERT(rv->popcnt == 0, 641174982Salc ("vm_reserv_break_all: reserv %p's popcnt is corrupted", 642174982Salc rv)); 643174982Salc vm_reserv_broken++; 644174982Salc } 645174982Salc mtx_unlock(&vm_page_queue_free_mtx); 646174982Salc} 647174982Salc 648174982Salc/* 649174982Salc * Frees the given page if it belongs to a reservation. Returns TRUE if the 650174982Salc * page is freed and FALSE otherwise. 651174982Salc * 652174982Salc * The free page queue lock must be held. 653174982Salc */ 654174982Salcboolean_t 655174982Salcvm_reserv_free_page(vm_page_t m) 656174982Salc{ 657174982Salc vm_reserv_t rv; 658174982Salc 659174982Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 660174982Salc rv = vm_reserv_from_page(m); 661234038Salc if (rv->object == NULL) 662234038Salc return (FALSE); 663234038Salc if ((m->flags & PG_CACHED) != 0 && m->pool != VM_FREEPOOL_CACHE) 664234038Salc vm_phys_set_pool(VM_FREEPOOL_CACHE, rv->pages, 665234038Salc VM_LEVEL_0_ORDER); 666234038Salc vm_reserv_depopulate(rv); 667234038Salc return (TRUE); 668174982Salc} 669174982Salc 670174982Salc/* 671174982Salc * Initializes the reservation management system. Specifically, initializes 672174982Salc * the reservation array. 673174982Salc * 674174982Salc * Requires that vm_page_array and first_page are initialized! 675174982Salc */ 676174982Salcvoid 677174982Salcvm_reserv_init(void) 678174982Salc{ 679174982Salc vm_paddr_t paddr; 680174982Salc int i; 681174982Salc 682174982Salc /* 683174982Salc * Initialize the reservation array. Specifically, initialize the 684174982Salc * "pages" field for every element that has an underlying superpage. 685174982Salc */ 686174982Salc for (i = 0; phys_avail[i + 1] != 0; i += 2) { 687174982Salc paddr = roundup2(phys_avail[i], VM_LEVEL_0_SIZE); 688174982Salc while (paddr + VM_LEVEL_0_SIZE <= phys_avail[i + 1]) { 689174982Salc vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages = 690174982Salc PHYS_TO_VM_PAGE(paddr); 691174982Salc paddr += VM_LEVEL_0_SIZE; 692174982Salc } 693174982Salc } 694174982Salc} 695174982Salc 696174982Salc/* 697174982Salc * Returns a reservation level if the given page belongs to a fully-populated 698174982Salc * reservation and -1 otherwise. 699174982Salc */ 700174982Salcint 701174982Salcvm_reserv_level_iffullpop(vm_page_t m) 702174982Salc{ 703174982Salc vm_reserv_t rv; 704174982Salc 705174982Salc rv = vm_reserv_from_page(m); 706174982Salc return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1); 707174982Salc} 708174982Salc 709174982Salc/* 710174982Salc * Prepare for the reactivation of a cached page. 711174982Salc * 712174982Salc * First, suppose that the given page "m" was allocated individually, i.e., not 713174982Salc * as part of a reservation, and cached. Then, suppose a reservation 714174982Salc * containing "m" is allocated by the same object. Although "m" and the 715174982Salc * reservation belong to the same object, "m"'s pindex may not match the 716174982Salc * reservation's. 717174982Salc * 718174982Salc * The free page queue must be locked. 719174982Salc */ 720174982Salcboolean_t 721174982Salcvm_reserv_reactivate_page(vm_page_t m) 722174982Salc{ 723174982Salc vm_reserv_t rv; 724174982Salc int i, m_index; 725174982Salc 726174982Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 727174982Salc rv = vm_reserv_from_page(m); 728174982Salc if (rv->object == NULL) 729174982Salc return (FALSE); 730174982Salc KASSERT((m->flags & PG_CACHED) != 0, 731174982Salc ("vm_reserv_uncache_page: page %p is not cached", m)); 732174982Salc if (m->object == rv->object && 733174982Salc m->pindex - rv->pindex == VM_RESERV_INDEX(m->object, m->pindex)) 734174982Salc vm_reserv_populate(rv); 735174982Salc else { 736174982Salc KASSERT(rv->inpartpopq, 737174982Salc ("vm_reserv_uncache_page: reserv %p's inpartpopq is FALSE", 738174982Salc rv)); 739174982Salc TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 740174982Salc rv->inpartpopq = FALSE; 741174982Salc LIST_REMOVE(rv, objq); 742174982Salc rv->object = NULL; 743174982Salc /* Don't vm_phys_free_pages(m, 0). */ 744174982Salc m_index = m - rv->pages; 745174982Salc for (i = 0; i < m_index; i++) { 746174982Salc if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0) 747174982Salc vm_phys_free_pages(&rv->pages[i], 0); 748174982Salc else 749174982Salc rv->popcnt--; 750174982Salc } 751174982Salc for (i++; i < VM_LEVEL_0_NPAGES; i++) { 752174982Salc if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0) 753174982Salc vm_phys_free_pages(&rv->pages[i], 0); 754174982Salc else 755174982Salc rv->popcnt--; 756174982Salc } 757174982Salc KASSERT(rv->popcnt == 0, 758174982Salc ("vm_reserv_uncache_page: reserv %p's popcnt is corrupted", 759174982Salc rv)); 760174982Salc vm_reserv_broken++; 761174982Salc } 762174982Salc return (TRUE); 763174982Salc} 764174982Salc 765174982Salc/* 766177956Salc * Breaks the given partially-populated reservation, releasing its cached and 767177956Salc * free pages to the physical memory allocator. 768177956Salc * 769177956Salc * The free page queue lock must be held. 770177956Salc */ 771177956Salcstatic void 772177956Salcvm_reserv_reclaim(vm_reserv_t rv) 773177956Salc{ 774177956Salc int i; 775177956Salc 776177956Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 777177956Salc KASSERT(rv->inpartpopq, 778177956Salc ("vm_reserv_reclaim: reserv %p's inpartpopq is corrupted", rv)); 779177956Salc TAILQ_REMOVE(&vm_rvq_partpop, rv, partpopq); 780177956Salc rv->inpartpopq = FALSE; 781177956Salc KASSERT(rv->object != NULL, 782177956Salc ("vm_reserv_reclaim: reserv %p is free", rv)); 783177956Salc LIST_REMOVE(rv, objq); 784177956Salc rv->object = NULL; 785177956Salc for (i = 0; i < VM_LEVEL_0_NPAGES; i++) { 786177956Salc if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0) 787177956Salc vm_phys_free_pages(&rv->pages[i], 0); 788177956Salc else 789177956Salc rv->popcnt--; 790177956Salc } 791177956Salc KASSERT(rv->popcnt == 0, 792177956Salc ("vm_reserv_reclaim: reserv %p's popcnt is corrupted", rv)); 793177956Salc vm_reserv_reclaimed++; 794177956Salc} 795177956Salc 796177956Salc/* 797174982Salc * Breaks the reservation at the head of the partially-populated reservation 798174982Salc * queue, releasing its cached and free pages to the physical memory 799174982Salc * allocator. Returns TRUE if a reservation is broken and FALSE otherwise. 800174982Salc * 801174982Salc * The free page queue lock must be held. 802174982Salc */ 803174982Salcboolean_t 804177956Salcvm_reserv_reclaim_inactive(void) 805174982Salc{ 806174982Salc vm_reserv_t rv; 807174982Salc 808174982Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 809174982Salc if ((rv = TAILQ_FIRST(&vm_rvq_partpop)) != NULL) { 810177956Salc vm_reserv_reclaim(rv); 811174982Salc return (TRUE); 812174982Salc } 813174982Salc return (FALSE); 814174982Salc} 815174982Salc 816174982Salc/* 817177956Salc * Searches the partially-populated reservation queue for the least recently 818177956Salc * active reservation with unused pages, i.e., cached or free, that satisfy the 819177956Salc * given request for contiguous physical memory. If a satisfactory reservation 820177956Salc * is found, it is broken. Returns TRUE if a reservation is broken and FALSE 821177956Salc * otherwise. 822177956Salc * 823177956Salc * The free page queue lock must be held. 824177956Salc */ 825177956Salcboolean_t 826228287Salcvm_reserv_reclaim_contig(u_long npages, vm_paddr_t low, vm_paddr_t high, 827226928Salc u_long alignment, vm_paddr_t boundary) 828177956Salc{ 829228287Salc vm_paddr_t pa, pa_length, size; 830177956Salc vm_reserv_t rv; 831177956Salc int i; 832177956Salc 833177956Salc mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 834228287Salc if (npages > VM_LEVEL_0_NPAGES - 1) 835177956Salc return (FALSE); 836228287Salc size = npages << PAGE_SHIFT; 837177956Salc TAILQ_FOREACH(rv, &vm_rvq_partpop, partpopq) { 838177956Salc pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]); 839177956Salc if (pa + PAGE_SIZE - size < low) { 840177956Salc /* this entire reservation is too low; go to next */ 841177956Salc continue; 842177956Salc } 843177956Salc pa_length = 0; 844177956Salc for (i = 0; i < VM_LEVEL_0_NPAGES; i++) 845177956Salc if ((rv->pages[i].flags & (PG_CACHED | PG_FREE)) != 0) { 846177956Salc pa_length += PAGE_SIZE; 847177956Salc if (pa_length == PAGE_SIZE) { 848177956Salc pa = VM_PAGE_TO_PHYS(&rv->pages[i]); 849177956Salc if (pa + size > high) { 850177956Salc /* skip to next reservation */ 851177956Salc break; 852177956Salc } else if (pa < low || 853177956Salc (pa & (alignment - 1)) != 0 || 854177956Salc ((pa ^ (pa + size - 1)) & 855177956Salc ~(boundary - 1)) != 0) 856177956Salc pa_length = 0; 857215508Smlaier } 858215508Smlaier if (pa_length >= size) { 859177956Salc vm_reserv_reclaim(rv); 860177956Salc return (TRUE); 861177956Salc } 862177956Salc } else 863177956Salc pa_length = 0; 864177956Salc } 865177956Salc return (FALSE); 866177956Salc} 867177956Salc 868177956Salc/* 869174982Salc * Transfers the reservation underlying the given page to a new object. 870174982Salc * 871174982Salc * The object must be locked. 872174982Salc */ 873174982Salcvoid 874174982Salcvm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object, 875174982Salc vm_pindex_t old_object_offset) 876174982Salc{ 877174982Salc vm_reserv_t rv; 878174982Salc 879248084Sattilio VM_OBJECT_ASSERT_WLOCKED(new_object); 880174982Salc rv = vm_reserv_from_page(m); 881174982Salc if (rv->object == old_object) { 882174982Salc mtx_lock(&vm_page_queue_free_mtx); 883174982Salc if (rv->object == old_object) { 884174982Salc LIST_REMOVE(rv, objq); 885174982Salc LIST_INSERT_HEAD(&new_object->rvq, rv, objq); 886174982Salc rv->object = new_object; 887174982Salc rv->pindex -= old_object_offset; 888174982Salc } 889174982Salc mtx_unlock(&vm_page_queue_free_mtx); 890174982Salc } 891174982Salc} 892174982Salc 893174982Salc/* 894174982Salc * Allocates the virtual and physical memory required by the reservation 895174982Salc * management system's data structures, in particular, the reservation array. 896174982Salc */ 897174982Salcvm_paddr_t 898174982Salcvm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water) 899174982Salc{ 900174982Salc vm_paddr_t new_end; 901174982Salc size_t size; 902174982Salc 903174982Salc /* 904174982Salc * Calculate the size (in bytes) of the reservation array. Round up 905174982Salc * from "high_water" because every small page is mapped to an element 906174982Salc * in the reservation array based on its physical address. Thus, the 907174982Salc * number of elements in the reservation array can be greater than the 908174982Salc * number of superpages. 909174982Salc */ 910174982Salc size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv); 911174982Salc 912174982Salc /* 913174982Salc * Allocate and map the physical memory for the reservation array. The 914174982Salc * next available virtual address is returned by reference. 915174982Salc */ 916174982Salc new_end = end - round_page(size); 917174982Salc vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end, 918174982Salc VM_PROT_READ | VM_PROT_WRITE); 919174982Salc bzero(vm_reserv_array, size); 920174982Salc 921174982Salc /* 922174982Salc * Return the next available physical address. 923174982Salc */ 924174982Salc return (new_end); 925174982Salc} 926174982Salc 927174982Salc#endif /* VM_NRESERVLEVEL > 0 */ 928