1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2002-2006 Rice University 5 * Copyright (c) 2007-2011 Alan L. Cox <alc@cs.rice.edu> 6 * All rights reserved. 7 * 8 * This software was developed for the FreeBSD Project by Alan L. Cox, 9 * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 30 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Superpage reservation management module 36 * 37 * Any external functions defined by this module are only to be used by the 38 * virtual memory system. 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include "opt_vm.h" 45 46 #include <sys/param.h> 47 #include <sys/kernel.h> 48 #include <sys/lock.h> 49 #include <sys/malloc.h> 50 #include <sys/mutex.h> 51 #include <sys/queue.h> 52 #include <sys/rwlock.h> 53 #include <sys/sbuf.h> 54 #include <sys/sysctl.h> 55 #include <sys/systm.h> 56 #include <sys/vmmeter.h> 57 58 #include <vm/vm.h> 59 #include <vm/vm_param.h> 60 #include <vm/vm_object.h> 61 #include <vm/vm_page.h> 62 #include <vm/vm_phys.h> 63 #include <vm/vm_radix.h> 64 #include <vm/vm_reserv.h> 65 66 /* 67 * The reservation system supports the speculative allocation of large physical 68 * pages ("superpages"). Speculative allocation enables the fully automatic 69 * utilization of superpages by the virtual memory system. In other words, no 70 * programmatic directives are required to use superpages. 71 */ 72 73 #if VM_NRESERVLEVEL > 0 74 75 /* 76 * The number of small pages that are contained in a level 0 reservation 77 */ 78 #define VM_LEVEL_0_NPAGES (1 << VM_LEVEL_0_ORDER) 79 80 /* 81 * The number of bits by which a physical address is shifted to obtain the 82 * reservation number 83 */ 84 #define VM_LEVEL_0_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT) 85 86 /* 87 * The size of a level 0 reservation in bytes 88 */ 89 #define VM_LEVEL_0_SIZE (1 << VM_LEVEL_0_SHIFT) 90 91 /* 92 * Computes the index of the small page underlying the given (object, pindex) 93 * within the reservation's array of small pages. 94 */ 95 #define VM_RESERV_INDEX(object, pindex) \ 96 (((object)->pg_color + (pindex)) & (VM_LEVEL_0_NPAGES - 1)) 97 98 /* 99 * The size of a population map entry 100 */ 101 typedef u_long popmap_t; 102 103 /* 104 * The number of bits in a population map entry 105 */ 106 #define NBPOPMAP (NBBY * sizeof(popmap_t)) 107 108 /* 109 * The number of population map entries in a reservation 110 */ 111 #define NPOPMAP howmany(VM_LEVEL_0_NPAGES, NBPOPMAP) 112 113 /* 114 * Clear a bit in the population map. 115 */ 116 static __inline void 117 popmap_clear(popmap_t popmap[], int i) 118 { 119 120 popmap[i / NBPOPMAP] &= ~(1UL << (i % NBPOPMAP)); 121 } 122 123 /* 124 * Set a bit in the population map. 125 */ 126 static __inline void 127 popmap_set(popmap_t popmap[], int i) 128 { 129 130 popmap[i / NBPOPMAP] |= 1UL << (i % NBPOPMAP); 131 } 132 133 /* 134 * Is a bit in the population map clear? 135 */ 136 static __inline boolean_t 137 popmap_is_clear(popmap_t popmap[], int i) 138 { 139 140 return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) == 0); 141 } 142 143 /* 144 * Is a bit in the population map set? 145 */ 146 static __inline boolean_t 147 popmap_is_set(popmap_t popmap[], int i) 148 { 149 150 return ((popmap[i / NBPOPMAP] & (1UL << (i % NBPOPMAP))) != 0); 151 } 152 153 /* 154 * The reservation structure 155 * 156 * A reservation structure is constructed whenever a large physical page is 157 * speculatively allocated to an object. The reservation provides the small 158 * physical pages for the range [pindex, pindex + VM_LEVEL_0_NPAGES) of offsets 159 * within that object. The reservation's "popcnt" tracks the number of these 160 * small physical pages that are in use at any given time. When and if the 161 * reservation is not fully utilized, it appears in the queue of partially 162 * populated reservations. The reservation always appears on the containing 163 * object's list of reservations. 164 * 165 * A partially populated reservation can be broken and reclaimed at any time. 166 */ 167 struct vm_reserv { 168 TAILQ_ENTRY(vm_reserv) partpopq; 169 LIST_ENTRY(vm_reserv) objq; 170 vm_object_t object; /* containing object */ 171 vm_pindex_t pindex; /* offset within object */ 172 vm_page_t pages; /* first page of a superpage */ 173 int domain; /* NUMA domain */ 174 int popcnt; /* # of pages in use */ 175 char inpartpopq; 176 popmap_t popmap[NPOPMAP]; /* bit vector of used pages */ 177 }; 178 179 /* 180 * The reservation array 181 * 182 * This array is analoguous in function to vm_page_array. It differs in the 183 * respect that it may contain a greater number of useful reservation 184 * structures than there are (physical) superpages. These "invalid" 185 * reservation structures exist to trade-off space for time in the 186 * implementation of vm_reserv_from_page(). Invalid reservation structures are 187 * distinguishable from "valid" reservation structures by inspecting the 188 * reservation's "pages" field. Invalid reservation structures have a NULL 189 * "pages" field. 190 * 191 * vm_reserv_from_page() maps a small (physical) page to an element of this 192 * array by computing a physical reservation number from the page's physical 193 * address. The physical reservation number is used as the array index. 194 * 195 * An "active" reservation is a valid reservation structure that has a non-NULL 196 * "object" field and a non-zero "popcnt" field. In other words, every active 197 * reservation belongs to a particular object. Moreover, every active 198 * reservation has an entry in the containing object's list of reservations. 199 */ 200 static vm_reserv_t vm_reserv_array; 201 202 /* 203 * The partially populated reservation queue 204 * 205 * This queue enables the fast recovery of an unused free small page from a 206 * partially populated reservation. The reservation at the head of this queue 207 * is the least recently changed, partially populated reservation. 208 * 209 * Access to this queue is synchronized by the free page queue lock. 210 */ 211 static TAILQ_HEAD(, vm_reserv) vm_rvq_partpop[MAXMEMDOM]; 212 213 static SYSCTL_NODE(_vm, OID_AUTO, reserv, CTLFLAG_RD, 0, "Reservation Info"); 214 215 static long vm_reserv_broken; 216 SYSCTL_LONG(_vm_reserv, OID_AUTO, broken, CTLFLAG_RD, 217 &vm_reserv_broken, 0, "Cumulative number of broken reservations"); 218 219 static long vm_reserv_freed; 220 SYSCTL_LONG(_vm_reserv, OID_AUTO, freed, CTLFLAG_RD, 221 &vm_reserv_freed, 0, "Cumulative number of freed reservations"); 222 223 static int sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS); 224 225 SYSCTL_PROC(_vm_reserv, OID_AUTO, fullpop, CTLTYPE_INT | CTLFLAG_RD, NULL, 0, 226 sysctl_vm_reserv_fullpop, "I", "Current number of full reservations"); 227 228 static int sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS); 229 230 SYSCTL_OID(_vm_reserv, OID_AUTO, partpopq, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, 231 sysctl_vm_reserv_partpopq, "A", "Partially populated reservation queues"); 232 233 static long vm_reserv_reclaimed; 234 SYSCTL_LONG(_vm_reserv, OID_AUTO, reclaimed, CTLFLAG_RD, 235 &vm_reserv_reclaimed, 0, "Cumulative number of reclaimed reservations"); 236 237 static void vm_reserv_break(vm_reserv_t rv, vm_page_t m); 238 static void vm_reserv_depopulate(vm_reserv_t rv, int index); 239 static vm_reserv_t vm_reserv_from_page(vm_page_t m); 240 static boolean_t vm_reserv_has_pindex(vm_reserv_t rv, 241 vm_pindex_t pindex); 242 static void vm_reserv_populate(vm_reserv_t rv, int index); 243 static void vm_reserv_reclaim(vm_reserv_t rv); 244 245 /* 246 * Returns the current number of full reservations. 247 * 248 * Since the number of full reservations is computed without acquiring the 249 * free page queue lock, the returned value may be inexact. 250 */ 251 static int 252 sysctl_vm_reserv_fullpop(SYSCTL_HANDLER_ARGS) 253 { 254 vm_paddr_t paddr; 255 struct vm_phys_seg *seg; 256 vm_reserv_t rv; 257 int fullpop, segind; 258 259 fullpop = 0; 260 for (segind = 0; segind < vm_phys_nsegs; segind++) { 261 seg = &vm_phys_segs[segind]; 262 paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 263 while (paddr + VM_LEVEL_0_SIZE <= seg->end) { 264 rv = &vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT]; 265 fullpop += rv->popcnt == VM_LEVEL_0_NPAGES; 266 paddr += VM_LEVEL_0_SIZE; 267 } 268 } 269 return (sysctl_handle_int(oidp, &fullpop, 0, req)); 270 } 271 272 /* 273 * Describes the current state of the partially populated reservation queue. 274 */ 275 static int 276 sysctl_vm_reserv_partpopq(SYSCTL_HANDLER_ARGS) 277 { 278 struct sbuf sbuf; 279 vm_reserv_t rv; 280 int counter, error, domain, level, unused_pages; 281 282 error = sysctl_wire_old_buffer(req, 0); 283 if (error != 0) 284 return (error); 285 sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 286 sbuf_printf(&sbuf, "\nDOMAIN LEVEL SIZE NUMBER\n\n"); 287 for (domain = 0; domain < vm_ndomains; domain++) { 288 for (level = -1; level <= VM_NRESERVLEVEL - 2; level++) { 289 counter = 0; 290 unused_pages = 0; 291 mtx_lock(&vm_page_queue_free_mtx); 292 TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) { 293 counter++; 294 unused_pages += VM_LEVEL_0_NPAGES - rv->popcnt; 295 } 296 mtx_unlock(&vm_page_queue_free_mtx); 297 sbuf_printf(&sbuf, "%6d, %7d, %6dK, %6d\n", 298 domain, level, 299 unused_pages * ((int)PAGE_SIZE / 1024), counter); 300 } 301 } 302 error = sbuf_finish(&sbuf); 303 sbuf_delete(&sbuf); 304 return (error); 305 } 306 307 /* 308 * Reduces the given reservation's population count. If the population count 309 * becomes zero, the reservation is destroyed. Additionally, moves the 310 * reservation to the tail of the partially populated reservation queue if the 311 * population count is non-zero. 312 * 313 * The free page queue lock must be held. 314 */ 315 static void 316 vm_reserv_depopulate(vm_reserv_t rv, int index) 317 { 318 319 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 320 KASSERT(rv->object != NULL, 321 ("vm_reserv_depopulate: reserv %p is free", rv)); 322 KASSERT(popmap_is_set(rv->popmap, index), 323 ("vm_reserv_depopulate: reserv %p's popmap[%d] is clear", rv, 324 index)); 325 KASSERT(rv->popcnt > 0, 326 ("vm_reserv_depopulate: reserv %p's popcnt is corrupted", rv)); 327 KASSERT(rv->domain >= 0 && rv->domain < vm_ndomains, 328 ("vm_reserv_depopulate: reserv %p's domain is corrupted %d", 329 rv, rv->domain)); 330 if (rv->inpartpopq) { 331 TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq); 332 rv->inpartpopq = FALSE; 333 } else { 334 KASSERT(rv->pages->psind == 1, 335 ("vm_reserv_depopulate: reserv %p is already demoted", 336 rv)); 337 rv->pages->psind = 0; 338 } 339 popmap_clear(rv->popmap, index); 340 rv->popcnt--; 341 if (rv->popcnt == 0) { 342 LIST_REMOVE(rv, objq); 343 rv->object = NULL; 344 rv->domain = -1; 345 vm_phys_free_pages(rv->pages, VM_LEVEL_0_ORDER); 346 vm_reserv_freed++; 347 } else { 348 rv->inpartpopq = TRUE; 349 TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq); 350 } 351 } 352 353 /* 354 * Returns the reservation to which the given page might belong. 355 */ 356 static __inline vm_reserv_t 357 vm_reserv_from_page(vm_page_t m) 358 { 359 360 return (&vm_reserv_array[VM_PAGE_TO_PHYS(m) >> VM_LEVEL_0_SHIFT]); 361 } 362 363 /* 364 * Returns TRUE if the given reservation contains the given page index and 365 * FALSE otherwise. 366 */ 367 static __inline boolean_t 368 vm_reserv_has_pindex(vm_reserv_t rv, vm_pindex_t pindex) 369 { 370 371 return (((pindex - rv->pindex) & ~(VM_LEVEL_0_NPAGES - 1)) == 0); 372 } 373 374 /* 375 * Increases the given reservation's population count. Moves the reservation 376 * to the tail of the partially populated reservation queue. 377 * 378 * The free page queue must be locked. 379 */ 380 static void 381 vm_reserv_populate(vm_reserv_t rv, int index) 382 { 383 384 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 385 KASSERT(rv->object != NULL, 386 ("vm_reserv_populate: reserv %p is free", rv)); 387 KASSERT(popmap_is_clear(rv->popmap, index), 388 ("vm_reserv_populate: reserv %p's popmap[%d] is set", rv, 389 index)); 390 KASSERT(rv->popcnt < VM_LEVEL_0_NPAGES, 391 ("vm_reserv_populate: reserv %p is already full", rv)); 392 KASSERT(rv->pages->psind == 0, 393 ("vm_reserv_populate: reserv %p is already promoted", rv)); 394 KASSERT(rv->domain >= 0 && rv->domain < vm_ndomains, 395 ("vm_reserv_populate: reserv %p's domain is corrupted %d", 396 rv, rv->domain)); 397 if (rv->inpartpopq) { 398 TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq); 399 rv->inpartpopq = FALSE; 400 } 401 popmap_set(rv->popmap, index); 402 rv->popcnt++; 403 if (rv->popcnt < VM_LEVEL_0_NPAGES) { 404 rv->inpartpopq = TRUE; 405 TAILQ_INSERT_TAIL(&vm_rvq_partpop[rv->domain], rv, partpopq); 406 } else 407 rv->pages->psind = 1; 408 } 409 410 /* 411 * Allocates a contiguous set of physical pages of the given size "npages" 412 * from existing or newly created reservations. All of the physical pages 413 * must be at or above the given physical address "low" and below the given 414 * physical address "high". The given value "alignment" determines the 415 * alignment of the first physical page in the set. If the given value 416 * "boundary" is non-zero, then the set of physical pages cannot cross any 417 * physical address boundary that is a multiple of that value. Both 418 * "alignment" and "boundary" must be a power of two. 419 * 420 * The page "mpred" must immediately precede the offset "pindex" within the 421 * specified object. 422 * 423 * The object and free page queue must be locked. 424 */ 425 vm_page_t 426 vm_reserv_alloc_contig(vm_object_t object, vm_pindex_t pindex, int domain, 427 u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 428 vm_paddr_t boundary, vm_page_t mpred) 429 { 430 vm_paddr_t pa, size; 431 vm_page_t m, m_ret, msucc; 432 vm_pindex_t first, leftcap, rightcap; 433 vm_reserv_t rv; 434 u_long allocpages, maxpages, minpages; 435 int i, index, n; 436 437 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 438 VM_OBJECT_ASSERT_WLOCKED(object); 439 KASSERT(npages != 0, ("vm_reserv_alloc_contig: npages is 0")); 440 441 /* 442 * Is a reservation fundamentally impossible? 443 */ 444 if (pindex < VM_RESERV_INDEX(object, pindex) || 445 pindex + npages > object->size) 446 return (NULL); 447 448 /* 449 * All reservations of a particular size have the same alignment. 450 * Assuming that the first page is allocated from a reservation, the 451 * least significant bits of its physical address can be determined 452 * from its offset from the beginning of the reservation and the size 453 * of the reservation. 454 * 455 * Could the specified index within a reservation of the smallest 456 * possible size satisfy the alignment and boundary requirements? 457 */ 458 pa = VM_RESERV_INDEX(object, pindex) << PAGE_SHIFT; 459 if ((pa & (alignment - 1)) != 0) 460 return (NULL); 461 size = npages << PAGE_SHIFT; 462 if (((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 463 return (NULL); 464 465 /* 466 * Look for an existing reservation. 467 */ 468 if (mpred != NULL) { 469 KASSERT(mpred->object == object, 470 ("vm_reserv_alloc_contig: object doesn't contain mpred")); 471 KASSERT(mpred->pindex < pindex, 472 ("vm_reserv_alloc_contig: mpred doesn't precede pindex")); 473 rv = vm_reserv_from_page(mpred); 474 if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 475 goto found; 476 msucc = TAILQ_NEXT(mpred, listq); 477 } else 478 msucc = TAILQ_FIRST(&object->memq); 479 if (msucc != NULL) { 480 KASSERT(msucc->pindex > pindex, 481 ("vm_reserv_alloc_contig: msucc doesn't succeed pindex")); 482 rv = vm_reserv_from_page(msucc); 483 if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 484 goto found; 485 } 486 487 /* 488 * Could at least one reservation fit between the first index to the 489 * left that can be used ("leftcap") and the first index to the right 490 * that cannot be used ("rightcap")? 491 */ 492 first = pindex - VM_RESERV_INDEX(object, pindex); 493 if (mpred != NULL) { 494 if ((rv = vm_reserv_from_page(mpred))->object != object) 495 leftcap = mpred->pindex + 1; 496 else 497 leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 498 if (leftcap > first) 499 return (NULL); 500 } 501 minpages = VM_RESERV_INDEX(object, pindex) + npages; 502 maxpages = roundup2(minpages, VM_LEVEL_0_NPAGES); 503 allocpages = maxpages; 504 if (msucc != NULL) { 505 if ((rv = vm_reserv_from_page(msucc))->object != object) 506 rightcap = msucc->pindex; 507 else 508 rightcap = rv->pindex; 509 if (first + maxpages > rightcap) { 510 if (maxpages == VM_LEVEL_0_NPAGES) 511 return (NULL); 512 513 /* 514 * At least one reservation will fit between "leftcap" 515 * and "rightcap". However, a reservation for the 516 * last of the requested pages will not fit. Reduce 517 * the size of the upcoming allocation accordingly. 518 */ 519 allocpages = minpages; 520 } 521 } 522 523 /* 524 * Would the last new reservation extend past the end of the object? 525 */ 526 if (first + maxpages > object->size) { 527 /* 528 * Don't allocate the last new reservation if the object is a 529 * vnode or backed by another object that is a vnode. 530 */ 531 if (object->type == OBJT_VNODE || 532 (object->backing_object != NULL && 533 object->backing_object->type == OBJT_VNODE)) { 534 if (maxpages == VM_LEVEL_0_NPAGES) 535 return (NULL); 536 allocpages = minpages; 537 } 538 /* Speculate that the object may grow. */ 539 } 540 541 /* 542 * Allocate the physical pages. The alignment and boundary specified 543 * for this allocation may be different from the alignment and 544 * boundary specified for the requested pages. For instance, the 545 * specified index may not be the first page within the first new 546 * reservation. 547 */ 548 m = vm_phys_alloc_contig(domain, allocpages, low, high, ulmax(alignment, 549 VM_LEVEL_0_SIZE), boundary > VM_LEVEL_0_SIZE ? boundary : 0); 550 if (m == NULL) 551 return (NULL); 552 KASSERT(vm_phys_domidx(m) == domain, 553 ("vm_reserv_alloc_contig: Page domain does not match requested.")); 554 555 /* 556 * The allocated physical pages always begin at a reservation 557 * boundary, but they do not always end at a reservation boundary. 558 * Initialize every reservation that is completely covered by the 559 * allocated physical pages. 560 */ 561 m_ret = NULL; 562 index = VM_RESERV_INDEX(object, pindex); 563 do { 564 rv = vm_reserv_from_page(m); 565 KASSERT(rv->pages == m, 566 ("vm_reserv_alloc_contig: reserv %p's pages is corrupted", 567 rv)); 568 KASSERT(rv->object == NULL, 569 ("vm_reserv_alloc_contig: reserv %p isn't free", rv)); 570 LIST_INSERT_HEAD(&object->rvq, rv, objq); 571 rv->object = object; 572 rv->pindex = first; 573 rv->domain = domain; 574 KASSERT(rv->popcnt == 0, 575 ("vm_reserv_alloc_contig: reserv %p's popcnt is corrupted", 576 rv)); 577 KASSERT(!rv->inpartpopq, 578 ("vm_reserv_alloc_contig: reserv %p's inpartpopq is TRUE", 579 rv)); 580 for (i = 0; i < NPOPMAP; i++) 581 KASSERT(rv->popmap[i] == 0, 582 ("vm_reserv_alloc_contig: reserv %p's popmap is corrupted", 583 rv)); 584 n = ulmin(VM_LEVEL_0_NPAGES - index, npages); 585 for (i = 0; i < n; i++) 586 vm_reserv_populate(rv, index + i); 587 npages -= n; 588 if (m_ret == NULL) { 589 m_ret = &rv->pages[index]; 590 index = 0; 591 } 592 m += VM_LEVEL_0_NPAGES; 593 first += VM_LEVEL_0_NPAGES; 594 allocpages -= VM_LEVEL_0_NPAGES; 595 } while (allocpages >= VM_LEVEL_0_NPAGES); 596 return (m_ret); 597 598 /* 599 * Found a matching reservation. 600 */ 601 found: 602 index = VM_RESERV_INDEX(object, pindex); 603 /* Does the allocation fit within the reservation? */ 604 if (index + npages > VM_LEVEL_0_NPAGES) 605 return (NULL); 606 m = &rv->pages[index]; 607 pa = VM_PAGE_TO_PHYS(m); 608 if (pa < low || pa + size > high || (pa & (alignment - 1)) != 0 || 609 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) 610 return (NULL); 611 /* Handle vm_page_rename(m, new_object, ...). */ 612 for (i = 0; i < npages; i++) 613 if (popmap_is_set(rv->popmap, index + i)) 614 return (NULL); 615 for (i = 0; i < npages; i++) 616 vm_reserv_populate(rv, index + i); 617 return (m); 618 } 619 620 /* 621 * Allocates a page from an existing or newly created reservation. 622 * 623 * The page "mpred" must immediately precede the offset "pindex" within the 624 * specified object. 625 * 626 * The object and free page queue must be locked. 627 */ 628 vm_page_t 629 vm_reserv_alloc_page(vm_object_t object, vm_pindex_t pindex, int domain, 630 vm_page_t mpred) 631 { 632 vm_page_t m, msucc; 633 vm_pindex_t first, leftcap, rightcap; 634 vm_reserv_t rv; 635 int i, index; 636 637 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 638 VM_OBJECT_ASSERT_WLOCKED(object); 639 640 /* 641 * Is a reservation fundamentally impossible? 642 */ 643 if (pindex < VM_RESERV_INDEX(object, pindex) || 644 pindex >= object->size) 645 return (NULL); 646 647 /* 648 * Look for an existing reservation. 649 */ 650 if (mpred != NULL) { 651 KASSERT(mpred->object == object, 652 ("vm_reserv_alloc_page: object doesn't contain mpred")); 653 KASSERT(mpred->pindex < pindex, 654 ("vm_reserv_alloc_page: mpred doesn't precede pindex")); 655 rv = vm_reserv_from_page(mpred); 656 if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 657 goto found; 658 msucc = TAILQ_NEXT(mpred, listq); 659 } else 660 msucc = TAILQ_FIRST(&object->memq); 661 if (msucc != NULL) { 662 KASSERT(msucc->pindex > pindex, 663 ("vm_reserv_alloc_page: msucc doesn't succeed pindex")); 664 rv = vm_reserv_from_page(msucc); 665 if (rv->object == object && vm_reserv_has_pindex(rv, pindex)) 666 goto found; 667 } 668 669 /* 670 * Could a reservation fit between the first index to the left that 671 * can be used and the first index to the right that cannot be used? 672 */ 673 first = pindex - VM_RESERV_INDEX(object, pindex); 674 if (mpred != NULL) { 675 if ((rv = vm_reserv_from_page(mpred))->object != object) 676 leftcap = mpred->pindex + 1; 677 else 678 leftcap = rv->pindex + VM_LEVEL_0_NPAGES; 679 if (leftcap > first) 680 return (NULL); 681 } 682 if (msucc != NULL) { 683 if ((rv = vm_reserv_from_page(msucc))->object != object) 684 rightcap = msucc->pindex; 685 else 686 rightcap = rv->pindex; 687 if (first + VM_LEVEL_0_NPAGES > rightcap) 688 return (NULL); 689 } 690 691 /* 692 * Would a new reservation extend past the end of the object? 693 */ 694 if (first + VM_LEVEL_0_NPAGES > object->size) { 695 /* 696 * Don't allocate a new reservation if the object is a vnode or 697 * backed by another object that is a vnode. 698 */ 699 if (object->type == OBJT_VNODE || 700 (object->backing_object != NULL && 701 object->backing_object->type == OBJT_VNODE)) 702 return (NULL); 703 /* Speculate that the object may grow. */ 704 } 705 706 /* 707 * Allocate and populate the new reservation. 708 */ 709 m = vm_phys_alloc_pages(domain, VM_FREEPOOL_DEFAULT, VM_LEVEL_0_ORDER); 710 if (m == NULL) 711 return (NULL); 712 rv = vm_reserv_from_page(m); 713 KASSERT(rv->pages == m, 714 ("vm_reserv_alloc_page: reserv %p's pages is corrupted", rv)); 715 KASSERT(rv->object == NULL, 716 ("vm_reserv_alloc_page: reserv %p isn't free", rv)); 717 LIST_INSERT_HEAD(&object->rvq, rv, objq); 718 rv->object = object; 719 rv->pindex = first; 720 rv->domain = domain; 721 KASSERT(rv->popcnt == 0, 722 ("vm_reserv_alloc_page: reserv %p's popcnt is corrupted", rv)); 723 KASSERT(!rv->inpartpopq, 724 ("vm_reserv_alloc_page: reserv %p's inpartpopq is TRUE", rv)); 725 for (i = 0; i < NPOPMAP; i++) 726 KASSERT(rv->popmap[i] == 0, 727 ("vm_reserv_alloc_page: reserv %p's popmap is corrupted", 728 rv)); 729 index = VM_RESERV_INDEX(object, pindex); 730 vm_reserv_populate(rv, index); 731 return (&rv->pages[index]); 732 733 /* 734 * Found a matching reservation. 735 */ 736 found: 737 index = VM_RESERV_INDEX(object, pindex); 738 m = &rv->pages[index]; 739 KASSERT(object != kernel_object || vm_phys_domidx(m) == domain, 740 ("vm_reserv_alloc_page: Domain mismatch from reservation.")); 741 /* Handle vm_page_rename(m, new_object, ...). */ 742 if (popmap_is_set(rv->popmap, index)) 743 return (NULL); 744 vm_reserv_populate(rv, index); 745 return (m); 746 } 747 748 /* 749 * Breaks the given reservation. Except for the specified free page, all free 750 * pages in the reservation are returned to the physical memory allocator. 751 * The reservation's population count and map are reset to their initial 752 * state. 753 * 754 * The given reservation must not be in the partially populated reservation 755 * queue. The free page queue lock must be held. 756 */ 757 static void 758 vm_reserv_break(vm_reserv_t rv, vm_page_t m) 759 { 760 int begin_zeroes, hi, i, lo; 761 762 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 763 KASSERT(rv->object != NULL, 764 ("vm_reserv_break: reserv %p is free", rv)); 765 KASSERT(!rv->inpartpopq, 766 ("vm_reserv_break: reserv %p's inpartpopq is TRUE", rv)); 767 LIST_REMOVE(rv, objq); 768 rv->object = NULL; 769 rv->domain = -1; 770 if (m != NULL) { 771 /* 772 * Since the reservation is being broken, there is no harm in 773 * abusing the population map to stop "m" from being returned 774 * to the physical memory allocator. 775 */ 776 i = m - rv->pages; 777 KASSERT(popmap_is_clear(rv->popmap, i), 778 ("vm_reserv_break: reserv %p's popmap is corrupted", rv)); 779 popmap_set(rv->popmap, i); 780 rv->popcnt++; 781 } 782 i = hi = 0; 783 do { 784 /* Find the next 0 bit. Any previous 0 bits are < "hi". */ 785 lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i])); 786 if (lo == 0) { 787 /* Redundantly clears bits < "hi". */ 788 rv->popmap[i] = 0; 789 rv->popcnt -= NBPOPMAP - hi; 790 while (++i < NPOPMAP) { 791 lo = ffsl(~rv->popmap[i]); 792 if (lo == 0) { 793 rv->popmap[i] = 0; 794 rv->popcnt -= NBPOPMAP; 795 } else 796 break; 797 } 798 if (i == NPOPMAP) 799 break; 800 hi = 0; 801 } 802 KASSERT(lo > 0, ("vm_reserv_break: lo is %d", lo)); 803 /* Convert from ffsl() to ordinary bit numbering. */ 804 lo--; 805 if (lo > 0) { 806 /* Redundantly clears bits < "hi". */ 807 rv->popmap[i] &= ~((1UL << lo) - 1); 808 rv->popcnt -= lo - hi; 809 } 810 begin_zeroes = NBPOPMAP * i + lo; 811 /* Find the next 1 bit. */ 812 do 813 hi = ffsl(rv->popmap[i]); 814 while (hi == 0 && ++i < NPOPMAP); 815 if (i != NPOPMAP) 816 /* Convert from ffsl() to ordinary bit numbering. */ 817 hi--; 818 vm_phys_free_contig(&rv->pages[begin_zeroes], NBPOPMAP * i + 819 hi - begin_zeroes); 820 } while (i < NPOPMAP); 821 KASSERT(rv->popcnt == 0, 822 ("vm_reserv_break: reserv %p's popcnt is corrupted", rv)); 823 vm_reserv_broken++; 824 } 825 826 /* 827 * Breaks all reservations belonging to the given object. 828 */ 829 void 830 vm_reserv_break_all(vm_object_t object) 831 { 832 vm_reserv_t rv; 833 834 mtx_lock(&vm_page_queue_free_mtx); 835 while ((rv = LIST_FIRST(&object->rvq)) != NULL) { 836 KASSERT(rv->object == object, 837 ("vm_reserv_break_all: reserv %p is corrupted", rv)); 838 if (rv->inpartpopq) { 839 TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq); 840 rv->inpartpopq = FALSE; 841 } 842 vm_reserv_break(rv, NULL); 843 } 844 mtx_unlock(&vm_page_queue_free_mtx); 845 } 846 847 /* 848 * Frees the given page if it belongs to a reservation. Returns TRUE if the 849 * page is freed and FALSE otherwise. 850 * 851 * The free page queue lock must be held. 852 */ 853 boolean_t 854 vm_reserv_free_page(vm_page_t m) 855 { 856 vm_reserv_t rv; 857 858 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 859 rv = vm_reserv_from_page(m); 860 if (rv->object == NULL) 861 return (FALSE); 862 vm_reserv_depopulate(rv, m - rv->pages); 863 return (TRUE); 864 } 865 866 /* 867 * Initializes the reservation management system. Specifically, initializes 868 * the reservation array. 869 * 870 * Requires that vm_page_array and first_page are initialized! 871 */ 872 void 873 vm_reserv_init(void) 874 { 875 vm_paddr_t paddr; 876 struct vm_phys_seg *seg; 877 int i, segind; 878 879 /* 880 * Initialize the reservation array. Specifically, initialize the 881 * "pages" field for every element that has an underlying superpage. 882 */ 883 for (segind = 0; segind < vm_phys_nsegs; segind++) { 884 seg = &vm_phys_segs[segind]; 885 paddr = roundup2(seg->start, VM_LEVEL_0_SIZE); 886 while (paddr + VM_LEVEL_0_SIZE <= seg->end) { 887 vm_reserv_array[paddr >> VM_LEVEL_0_SHIFT].pages = 888 PHYS_TO_VM_PAGE(paddr); 889 paddr += VM_LEVEL_0_SIZE; 890 } 891 } 892 for (i = 0; i < MAXMEMDOM; i++) 893 TAILQ_INIT(&vm_rvq_partpop[i]); 894 } 895 896 /* 897 * Returns true if the given page belongs to a reservation and that page is 898 * free. Otherwise, returns false. 899 */ 900 bool 901 vm_reserv_is_page_free(vm_page_t m) 902 { 903 vm_reserv_t rv; 904 905 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 906 rv = vm_reserv_from_page(m); 907 if (rv->object == NULL) 908 return (false); 909 return (popmap_is_clear(rv->popmap, m - rv->pages)); 910 } 911 912 /* 913 * If the given page belongs to a reservation, returns the level of that 914 * reservation. Otherwise, returns -1. 915 */ 916 int 917 vm_reserv_level(vm_page_t m) 918 { 919 vm_reserv_t rv; 920 921 rv = vm_reserv_from_page(m); 922 return (rv->object != NULL ? 0 : -1); 923 } 924 925 /* 926 * Returns a reservation level if the given page belongs to a fully populated 927 * reservation and -1 otherwise. 928 */ 929 int 930 vm_reserv_level_iffullpop(vm_page_t m) 931 { 932 vm_reserv_t rv; 933 934 rv = vm_reserv_from_page(m); 935 return (rv->popcnt == VM_LEVEL_0_NPAGES ? 0 : -1); 936 } 937 938 /* 939 * Breaks the given partially populated reservation, releasing its free pages 940 * to the physical memory allocator. 941 * 942 * The free page queue lock must be held. 943 */ 944 static void 945 vm_reserv_reclaim(vm_reserv_t rv) 946 { 947 948 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 949 KASSERT(rv->inpartpopq, 950 ("vm_reserv_reclaim: reserv %p's inpartpopq is FALSE", rv)); 951 KASSERT(rv->domain >= 0 && rv->domain < vm_ndomains, 952 ("vm_reserv_reclaim: reserv %p's domain is corrupted %d", 953 rv, rv->domain)); 954 TAILQ_REMOVE(&vm_rvq_partpop[rv->domain], rv, partpopq); 955 rv->inpartpopq = FALSE; 956 vm_reserv_break(rv, NULL); 957 vm_reserv_reclaimed++; 958 } 959 960 /* 961 * Breaks the reservation at the head of the partially populated reservation 962 * queue, releasing its free pages to the physical memory allocator. Returns 963 * TRUE if a reservation is broken and FALSE otherwise. 964 * 965 * The free page queue lock must be held. 966 */ 967 boolean_t 968 vm_reserv_reclaim_inactive(int domain) 969 { 970 vm_reserv_t rv; 971 972 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 973 if ((rv = TAILQ_FIRST(&vm_rvq_partpop[domain])) != NULL) { 974 vm_reserv_reclaim(rv); 975 return (TRUE); 976 } 977 return (FALSE); 978 } 979 980 /* 981 * Searches the partially populated reservation queue for the least recently 982 * changed reservation with free pages that satisfy the given request for 983 * contiguous physical memory. If a satisfactory reservation is found, it is 984 * broken. Returns TRUE if a reservation is broken and FALSE otherwise. 985 * 986 * The free page queue lock must be held. 987 */ 988 boolean_t 989 vm_reserv_reclaim_contig(int domain, u_long npages, vm_paddr_t low, 990 vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 991 { 992 vm_paddr_t pa, size; 993 vm_reserv_t rv; 994 int hi, i, lo, low_index, next_free; 995 996 mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 997 if (npages > VM_LEVEL_0_NPAGES - 1) 998 return (FALSE); 999 size = npages << PAGE_SHIFT; 1000 TAILQ_FOREACH(rv, &vm_rvq_partpop[domain], partpopq) { 1001 pa = VM_PAGE_TO_PHYS(&rv->pages[VM_LEVEL_0_NPAGES - 1]); 1002 if (pa + PAGE_SIZE - size < low) { 1003 /* This entire reservation is too low; go to next. */ 1004 continue; 1005 } 1006 pa = VM_PAGE_TO_PHYS(&rv->pages[0]); 1007 if (pa + size > high) { 1008 /* This entire reservation is too high; go to next. */ 1009 continue; 1010 } 1011 if (pa < low) { 1012 /* Start the search for free pages at "low". */ 1013 low_index = (low + PAGE_MASK - pa) >> PAGE_SHIFT; 1014 i = low_index / NBPOPMAP; 1015 hi = low_index % NBPOPMAP; 1016 } else 1017 i = hi = 0; 1018 do { 1019 /* Find the next free page. */ 1020 lo = ffsl(~(((1UL << hi) - 1) | rv->popmap[i])); 1021 while (lo == 0 && ++i < NPOPMAP) 1022 lo = ffsl(~rv->popmap[i]); 1023 if (i == NPOPMAP) 1024 break; 1025 /* Convert from ffsl() to ordinary bit numbering. */ 1026 lo--; 1027 next_free = NBPOPMAP * i + lo; 1028 pa = VM_PAGE_TO_PHYS(&rv->pages[next_free]); 1029 KASSERT(pa >= low, 1030 ("vm_reserv_reclaim_contig: pa is too low")); 1031 if (pa + size > high) { 1032 /* The rest of this reservation is too high. */ 1033 break; 1034 } else if ((pa & (alignment - 1)) != 0 || 1035 ((pa ^ (pa + size - 1)) & ~(boundary - 1)) != 0) { 1036 /* 1037 * The current page doesn't meet the alignment 1038 * and/or boundary requirements. Continue 1039 * searching this reservation until the rest 1040 * of its free pages are either excluded or 1041 * exhausted. 1042 */ 1043 hi = lo + 1; 1044 if (hi >= NBPOPMAP) { 1045 hi = 0; 1046 i++; 1047 } 1048 continue; 1049 } 1050 /* Find the next used page. */ 1051 hi = ffsl(rv->popmap[i] & ~((1UL << lo) - 1)); 1052 while (hi == 0 && ++i < NPOPMAP) { 1053 if ((NBPOPMAP * i - next_free) * PAGE_SIZE >= 1054 size) { 1055 vm_reserv_reclaim(rv); 1056 return (TRUE); 1057 } 1058 hi = ffsl(rv->popmap[i]); 1059 } 1060 /* Convert from ffsl() to ordinary bit numbering. */ 1061 if (i != NPOPMAP) 1062 hi--; 1063 if ((NBPOPMAP * i + hi - next_free) * PAGE_SIZE >= 1064 size) { 1065 vm_reserv_reclaim(rv); 1066 return (TRUE); 1067 } 1068 } while (i < NPOPMAP); 1069 } 1070 return (FALSE); 1071 } 1072 1073 /* 1074 * Transfers the reservation underlying the given page to a new object. 1075 * 1076 * The object must be locked. 1077 */ 1078 void 1079 vm_reserv_rename(vm_page_t m, vm_object_t new_object, vm_object_t old_object, 1080 vm_pindex_t old_object_offset) 1081 { 1082 vm_reserv_t rv; 1083 1084 VM_OBJECT_ASSERT_WLOCKED(new_object); 1085 rv = vm_reserv_from_page(m); 1086 if (rv->object == old_object) { 1087 mtx_lock(&vm_page_queue_free_mtx); 1088 if (rv->object == old_object) { 1089 LIST_REMOVE(rv, objq); 1090 LIST_INSERT_HEAD(&new_object->rvq, rv, objq); 1091 rv->object = new_object; 1092 rv->pindex -= old_object_offset; 1093 } 1094 mtx_unlock(&vm_page_queue_free_mtx); 1095 } 1096 } 1097 1098 /* 1099 * Returns the size (in bytes) of a reservation of the specified level. 1100 */ 1101 int 1102 vm_reserv_size(int level) 1103 { 1104 1105 switch (level) { 1106 case 0: 1107 return (VM_LEVEL_0_SIZE); 1108 case -1: 1109 return (PAGE_SIZE); 1110 default: 1111 return (0); 1112 } 1113 } 1114 1115 /* 1116 * Allocates the virtual and physical memory required by the reservation 1117 * management system's data structures, in particular, the reservation array. 1118 */ 1119 vm_paddr_t 1120 vm_reserv_startup(vm_offset_t *vaddr, vm_paddr_t end, vm_paddr_t high_water) 1121 { 1122 vm_paddr_t new_end; 1123 size_t size; 1124 1125 /* 1126 * Calculate the size (in bytes) of the reservation array. Round up 1127 * from "high_water" because every small page is mapped to an element 1128 * in the reservation array based on its physical address. Thus, the 1129 * number of elements in the reservation array can be greater than the 1130 * number of superpages. 1131 */ 1132 size = howmany(high_water, VM_LEVEL_0_SIZE) * sizeof(struct vm_reserv); 1133 1134 /* 1135 * Allocate and map the physical memory for the reservation array. The 1136 * next available virtual address is returned by reference. 1137 */ 1138 new_end = end - round_page(size); 1139 vm_reserv_array = (void *)(uintptr_t)pmap_map(vaddr, new_end, end, 1140 VM_PROT_READ | VM_PROT_WRITE); 1141 bzero(vm_reserv_array, size); 1142 1143 /* 1144 * Return the next available physical address. 1145 */ 1146 return (new_end); 1147 } 1148 1149 /* 1150 * Returns the superpage containing the given page. 1151 */ 1152 vm_page_t 1153 vm_reserv_to_superpage(vm_page_t m) 1154 { 1155 vm_reserv_t rv; 1156 1157 VM_OBJECT_ASSERT_LOCKED(m->object); 1158 rv = vm_reserv_from_page(m); 1159 return (rv->object == m->object && rv->popcnt == VM_LEVEL_0_NPAGES ? 1160 rv->pages : NULL); 1161 } 1162 1163 #endif /* VM_NRESERVLEVEL > 0 */ 1164