1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 /* 30 * University Copyright- Copyright (c) 1982, 1986, 1988 31 * The Regents of the University of California 32 * All Rights Reserved 33 * 34 * University Acknowledgment- Portions of this document are derived from 35 * software developed by the University of California, Berkeley, and its 36 * contributors. 37 */ 38 39 #pragma ident "%Z%%M% %I% %E% SMI" 40 41 /* 42 * VM - physical page management. 43 */ 44 45 #include <sys/types.h> 46 #include <sys/t_lock.h> 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/errno.h> 50 #include <sys/time.h> 51 #include <sys/vnode.h> 52 #include <sys/vm.h> 53 #include <sys/vtrace.h> 54 #include <sys/swap.h> 55 #include <sys/cmn_err.h> 56 #include <sys/tuneable.h> 57 #include <sys/sysmacros.h> 58 #include <sys/cpuvar.h> 59 #include <sys/callb.h> 60 #include <sys/debug.h> 61 #include <sys/tnf_probe.h> 62 #include <sys/condvar_impl.h> 63 #include <sys/mem_config.h> 64 #include <sys/mem_cage.h> 65 #include <sys/kmem.h> 66 #include <sys/atomic.h> 67 #include <sys/strlog.h> 68 #include <sys/mman.h> 69 #include <sys/ontrap.h> 70 #include <sys/lgrp.h> 71 #include <sys/vfs.h> 72 73 #include <vm/hat.h> 74 #include <vm/anon.h> 75 #include <vm/page.h> 76 #include <vm/seg.h> 77 #include <vm/pvn.h> 78 #include <vm/seg_kmem.h> 79 #include <vm/vm_dep.h> 80 #include <sys/vm_usage.h> 81 #include <fs/fs_subr.h> 82 #include <sys/ddi.h> 83 #include <sys/modctl.h> 84 85 static int nopageage = 0; 86 87 static pgcnt_t max_page_get; /* max page_get request size in pages */ 88 pgcnt_t total_pages = 0; /* total number of pages (used by /proc) */ 89 90 /* 91 * freemem_lock protects all freemem variables: 92 * availrmem. Also this lock protects the globals which track the 93 * availrmem changes for accurate kernel footprint calculation. 94 * See below for an explanation of these 95 * globals. 96 */ 97 kmutex_t freemem_lock; 98 pgcnt_t availrmem; 99 pgcnt_t availrmem_initial; 100 101 /* 102 * These globals track availrmem changes to get a more accurate 103 * estimate of tke kernel size. Historically pp_kernel is used for 104 * kernel size and is based on availrmem. But availrmem is adjusted for 105 * locked pages in the system not just for kernel locked pages. 106 * These new counters will track the pages locked through segvn and 107 * by explicit user locking. 108 * 109 * segvn_pages_locked : This keeps track on a global basis how many pages 110 * are currently locked because of I/O. 111 * 112 * pages_locked : How many pages are locked becuase of user specified 113 * locking through mlock or plock. 114 * 115 * pages_useclaim,pages_claimed : These two variables track the 116 * cliam adjustments because of the protection changes on a segvn segment. 117 * 118 * All these globals are protected by the same lock which protects availrmem. 119 */ 120 pgcnt_t segvn_pages_locked; 121 pgcnt_t pages_locked; 122 pgcnt_t pages_useclaim; 123 pgcnt_t pages_claimed; 124 125 126 /* 127 * new_freemem_lock protects freemem, freemem_wait & freemem_cv. 128 */ 129 static kmutex_t new_freemem_lock; 130 static uint_t freemem_wait; /* someone waiting for freemem */ 131 static kcondvar_t freemem_cv; 132 133 /* 134 * The logical page free list is maintained as two lists, the 'free' 135 * and the 'cache' lists. 136 * The free list contains those pages that should be reused first. 137 * 138 * The implementation of the lists is machine dependent. 139 * page_get_freelist(), page_get_cachelist(), 140 * page_list_sub(), and page_list_add() 141 * form the interface to the machine dependent implementation. 142 * 143 * Pages with p_free set are on the cache list. 144 * Pages with p_free and p_age set are on the free list, 145 * 146 * A page may be locked while on either list. 147 */ 148 149 /* 150 * free list accounting stuff. 151 * 152 * 153 * Spread out the value for the number of pages on the 154 * page free and page cache lists. If there is just one 155 * value, then it must be under just one lock. 156 * The lock contention and cache traffic are a real bother. 157 * 158 * When we acquire and then drop a single pcf lock 159 * we can start in the middle of the array of pcf structures. 160 * If we acquire more than one pcf lock at a time, we need to 161 * start at the front to avoid deadlocking. 162 * 163 * pcf_count holds the number of pages in each pool. 164 * 165 * pcf_block is set when page_create_get_something() has asked the 166 * PSM page freelist and page cachelist routines without specifying 167 * a color and nothing came back. This is used to block anything 168 * else from moving pages from one list to the other while the 169 * lists are searched again. If a page is freeed while pcf_block is 170 * set, then pcf_reserve is incremented. pcgs_unblock() takes care 171 * of clearning pcf_block, doing the wakeups, etc. 172 */ 173 174 #if NCPU <= 4 175 #define PAD 2 176 #define PCF_FANOUT 4 177 static uint_t pcf_mask = PCF_FANOUT - 1; 178 #else 179 #define PAD 10 180 #ifdef sun4v 181 #define PCF_FANOUT 32 182 #else 183 #define PCF_FANOUT 128 184 #endif 185 static uint_t pcf_mask = PCF_FANOUT - 1; 186 #endif 187 188 struct pcf { 189 kmutex_t pcf_lock; /* protects the structure */ 190 uint_t pcf_count; /* page count */ 191 uint_t pcf_wait; /* number of waiters */ 192 uint_t pcf_block; /* pcgs flag to page_free() */ 193 uint_t pcf_reserve; /* pages freed after pcf_block set */ 194 uint_t pcf_fill[PAD]; /* to line up on the caches */ 195 }; 196 197 static struct pcf pcf[PCF_FANOUT]; 198 #define PCF_INDEX() ((CPU->cpu_id) & (pcf_mask)) 199 200 kmutex_t pcgs_lock; /* serializes page_create_get_ */ 201 kmutex_t pcgs_cagelock; /* serializes NOSLEEP cage allocs */ 202 kmutex_t pcgs_wait_lock; /* used for delay in pcgs */ 203 static kcondvar_t pcgs_cv; /* cv for delay in pcgs */ 204 205 #ifdef VM_STATS 206 207 /* 208 * No locks, but so what, they are only statistics. 209 */ 210 211 static struct page_tcnt { 212 int pc_free_cache; /* free's into cache list */ 213 int pc_free_dontneed; /* free's with dontneed */ 214 int pc_free_pageout; /* free's from pageout */ 215 int pc_free_free; /* free's into free list */ 216 int pc_free_pages; /* free's into large page free list */ 217 int pc_destroy_pages; /* large page destroy's */ 218 int pc_get_cache; /* get's from cache list */ 219 int pc_get_free; /* get's from free list */ 220 int pc_reclaim; /* reclaim's */ 221 int pc_abortfree; /* abort's of free pages */ 222 int pc_find_hit; /* find's that find page */ 223 int pc_find_miss; /* find's that don't find page */ 224 int pc_destroy_free; /* # of free pages destroyed */ 225 #define PC_HASH_CNT (4*PAGE_HASHAVELEN) 226 int pc_find_hashlen[PC_HASH_CNT+1]; 227 int pc_addclaim_pages; 228 int pc_subclaim_pages; 229 int pc_free_replacement_page[2]; 230 int pc_try_demote_pages[6]; 231 int pc_demote_pages[2]; 232 } pagecnt; 233 234 uint_t hashin_count; 235 uint_t hashin_not_held; 236 uint_t hashin_already; 237 238 uint_t hashout_count; 239 uint_t hashout_not_held; 240 241 uint_t page_create_count; 242 uint_t page_create_not_enough; 243 uint_t page_create_not_enough_again; 244 uint_t page_create_zero; 245 uint_t page_create_hashout; 246 uint_t page_create_page_lock_failed; 247 uint_t page_create_trylock_failed; 248 uint_t page_create_found_one; 249 uint_t page_create_hashin_failed; 250 uint_t page_create_dropped_phm; 251 252 uint_t page_create_new; 253 uint_t page_create_exists; 254 uint_t page_create_putbacks; 255 uint_t page_create_overshoot; 256 257 uint_t page_reclaim_zero; 258 uint_t page_reclaim_zero_locked; 259 260 uint_t page_rename_exists; 261 uint_t page_rename_count; 262 263 uint_t page_lookup_cnt[20]; 264 uint_t page_lookup_nowait_cnt[10]; 265 uint_t page_find_cnt; 266 uint_t page_exists_cnt; 267 uint_t page_exists_forreal_cnt; 268 uint_t page_lookup_dev_cnt; 269 uint_t get_cachelist_cnt; 270 uint_t page_create_cnt[10]; 271 uint_t alloc_pages[8]; 272 uint_t page_exphcontg[19]; 273 uint_t page_create_large_cnt[10]; 274 275 /* 276 * Collects statistics. 277 */ 278 #define PAGE_HASH_SEARCH(index, pp, vp, off) { \ 279 uint_t mylen = 0; \ 280 \ 281 for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash, mylen++) { \ 282 if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \ 283 break; \ 284 } \ 285 if ((pp) != NULL) \ 286 pagecnt.pc_find_hit++; \ 287 else \ 288 pagecnt.pc_find_miss++; \ 289 if (mylen > PC_HASH_CNT) \ 290 mylen = PC_HASH_CNT; \ 291 pagecnt.pc_find_hashlen[mylen]++; \ 292 } 293 294 #else /* VM_STATS */ 295 296 /* 297 * Don't collect statistics 298 */ 299 #define PAGE_HASH_SEARCH(index, pp, vp, off) { \ 300 for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash) { \ 301 if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \ 302 break; \ 303 } \ 304 } 305 306 #endif /* VM_STATS */ 307 308 309 310 #ifdef DEBUG 311 #define MEMSEG_SEARCH_STATS 312 #endif 313 314 #ifdef MEMSEG_SEARCH_STATS 315 struct memseg_stats { 316 uint_t nsearch; 317 uint_t nlastwon; 318 uint_t nhashwon; 319 uint_t nnotfound; 320 } memseg_stats; 321 322 #define MEMSEG_STAT_INCR(v) \ 323 atomic_add_32(&memseg_stats.v, 1) 324 #else 325 #define MEMSEG_STAT_INCR(x) 326 #endif 327 328 struct memseg *memsegs; /* list of memory segments */ 329 330 331 static void page_init_mem_config(void); 332 static int page_do_hashin(page_t *, vnode_t *, u_offset_t); 333 static void page_do_hashout(page_t *); 334 static void page_capture_init(); 335 int page_capture_take_action(page_t *, uint_t, void *); 336 337 static void page_demote_vp_pages(page_t *); 338 339 /* 340 * vm subsystem related initialization 341 */ 342 void 343 vm_init(void) 344 { 345 boolean_t callb_vm_cpr(void *, int); 346 347 (void) callb_add(callb_vm_cpr, 0, CB_CL_CPR_VM, "vm"); 348 page_init_mem_config(); 349 page_retire_init(); 350 vm_usage_init(); 351 page_capture_init(); 352 } 353 354 /* 355 * This function is called at startup and when memory is added or deleted. 356 */ 357 void 358 init_pages_pp_maximum() 359 { 360 static pgcnt_t p_min; 361 static pgcnt_t pages_pp_maximum_startup; 362 static pgcnt_t avrmem_delta; 363 static int init_done; 364 static int user_set; /* true if set in /etc/system */ 365 366 if (init_done == 0) { 367 368 /* If the user specified a value, save it */ 369 if (pages_pp_maximum != 0) { 370 user_set = 1; 371 pages_pp_maximum_startup = pages_pp_maximum; 372 } 373 374 /* 375 * Setting of pages_pp_maximum is based first time 376 * on the value of availrmem just after the start-up 377 * allocations. To preserve this relationship at run 378 * time, use a delta from availrmem_initial. 379 */ 380 ASSERT(availrmem_initial >= availrmem); 381 avrmem_delta = availrmem_initial - availrmem; 382 383 /* The allowable floor of pages_pp_maximum */ 384 p_min = tune.t_minarmem + 100; 385 386 /* Make sure we don't come through here again. */ 387 init_done = 1; 388 } 389 /* 390 * Determine pages_pp_maximum, the number of currently available 391 * pages (availrmem) that can't be `locked'. If not set by 392 * the user, we set it to 4% of the currently available memory 393 * plus 4MB. 394 * But we also insist that it be greater than tune.t_minarmem; 395 * otherwise a process could lock down a lot of memory, get swapped 396 * out, and never have enough to get swapped back in. 397 */ 398 if (user_set) 399 pages_pp_maximum = pages_pp_maximum_startup; 400 else 401 pages_pp_maximum = ((availrmem_initial - avrmem_delta) / 25) 402 + btop(4 * 1024 * 1024); 403 404 if (pages_pp_maximum <= p_min) { 405 pages_pp_maximum = p_min; 406 } 407 } 408 409 void 410 set_max_page_get(pgcnt_t target_total_pages) 411 { 412 max_page_get = target_total_pages / 2; 413 } 414 415 static pgcnt_t pending_delete; 416 417 /*ARGSUSED*/ 418 static void 419 page_mem_config_post_add( 420 void *arg, 421 pgcnt_t delta_pages) 422 { 423 set_max_page_get(total_pages - pending_delete); 424 init_pages_pp_maximum(); 425 } 426 427 /*ARGSUSED*/ 428 static int 429 page_mem_config_pre_del( 430 void *arg, 431 pgcnt_t delta_pages) 432 { 433 pgcnt_t nv; 434 435 nv = atomic_add_long_nv(&pending_delete, (spgcnt_t)delta_pages); 436 set_max_page_get(total_pages - nv); 437 return (0); 438 } 439 440 /*ARGSUSED*/ 441 static void 442 page_mem_config_post_del( 443 void *arg, 444 pgcnt_t delta_pages, 445 int cancelled) 446 { 447 pgcnt_t nv; 448 449 nv = atomic_add_long_nv(&pending_delete, -(spgcnt_t)delta_pages); 450 set_max_page_get(total_pages - nv); 451 if (!cancelled) 452 init_pages_pp_maximum(); 453 } 454 455 static kphysm_setup_vector_t page_mem_config_vec = { 456 KPHYSM_SETUP_VECTOR_VERSION, 457 page_mem_config_post_add, 458 page_mem_config_pre_del, 459 page_mem_config_post_del, 460 }; 461 462 static void 463 page_init_mem_config(void) 464 { 465 int ret; 466 467 ret = kphysm_setup_func_register(&page_mem_config_vec, (void *)NULL); 468 ASSERT(ret == 0); 469 } 470 471 /* 472 * Evenly spread out the PCF counters for large free pages 473 */ 474 static void 475 page_free_large_ctr(pgcnt_t npages) 476 { 477 static struct pcf *p = pcf; 478 pgcnt_t lump; 479 480 freemem += npages; 481 482 lump = roundup(npages, PCF_FANOUT) / PCF_FANOUT; 483 484 while (npages > 0) { 485 486 ASSERT(!p->pcf_block); 487 488 if (lump < npages) { 489 p->pcf_count += (uint_t)lump; 490 npages -= lump; 491 } else { 492 p->pcf_count += (uint_t)npages; 493 npages = 0; 494 } 495 496 ASSERT(!p->pcf_wait); 497 498 if (++p > &pcf[PCF_FANOUT - 1]) 499 p = pcf; 500 } 501 502 ASSERT(npages == 0); 503 } 504 505 /* 506 * Add a physical chunk of memory to the system freee lists during startup. 507 * Platform specific startup() allocates the memory for the page structs. 508 * 509 * num - number of page structures 510 * base - page number (pfn) to be associated with the first page. 511 * 512 * Since we are doing this during startup (ie. single threaded), we will 513 * use shortcut routines to avoid any locking overhead while putting all 514 * these pages on the freelists. 515 * 516 * NOTE: Any changes performed to page_free(), must also be performed to 517 * add_physmem() since this is how we initialize all page_t's at 518 * boot time. 519 */ 520 void 521 add_physmem( 522 page_t *pp, 523 pgcnt_t num, 524 pfn_t pnum) 525 { 526 page_t *root = NULL; 527 uint_t szc = page_num_pagesizes() - 1; 528 pgcnt_t large = page_get_pagecnt(szc); 529 pgcnt_t cnt = 0; 530 531 TRACE_2(TR_FAC_VM, TR_PAGE_INIT, 532 "add_physmem:pp %p num %lu", pp, num); 533 534 /* 535 * Arbitrarily limit the max page_get request 536 * to 1/2 of the page structs we have. 537 */ 538 total_pages += num; 539 set_max_page_get(total_pages); 540 541 PLCNT_MODIFY_MAX(pnum, (long)num); 542 543 /* 544 * The physical space for the pages array 545 * representing ram pages has already been 546 * allocated. Here we initialize each lock 547 * in the page structure, and put each on 548 * the free list 549 */ 550 for (; num; pp++, pnum++, num--) { 551 552 /* 553 * this needs to fill in the page number 554 * and do any other arch specific initialization 555 */ 556 add_physmem_cb(pp, pnum); 557 558 pp->p_lckcnt = 0; 559 pp->p_cowcnt = 0; 560 pp->p_slckcnt = 0; 561 562 /* 563 * Initialize the page lock as unlocked, since nobody 564 * can see or access this page yet. 565 */ 566 pp->p_selock = 0; 567 568 /* 569 * Initialize IO lock 570 */ 571 page_iolock_init(pp); 572 573 /* 574 * initialize other fields in the page_t 575 */ 576 PP_SETFREE(pp); 577 page_clr_all_props(pp); 578 PP_SETAGED(pp); 579 pp->p_offset = (u_offset_t)-1; 580 pp->p_next = pp; 581 pp->p_prev = pp; 582 583 /* 584 * Simple case: System doesn't support large pages. 585 */ 586 if (szc == 0) { 587 pp->p_szc = 0; 588 page_free_at_startup(pp); 589 continue; 590 } 591 592 /* 593 * Handle unaligned pages, we collect them up onto 594 * the root page until we have a full large page. 595 */ 596 if (!IS_P2ALIGNED(pnum, large)) { 597 598 /* 599 * If not in a large page, 600 * just free as small page. 601 */ 602 if (root == NULL) { 603 pp->p_szc = 0; 604 page_free_at_startup(pp); 605 continue; 606 } 607 608 /* 609 * Link a constituent page into the large page. 610 */ 611 pp->p_szc = szc; 612 page_list_concat(&root, &pp); 613 614 /* 615 * When large page is fully formed, free it. 616 */ 617 if (++cnt == large) { 618 page_free_large_ctr(cnt); 619 page_list_add_pages(root, PG_LIST_ISINIT); 620 root = NULL; 621 cnt = 0; 622 } 623 continue; 624 } 625 626 /* 627 * At this point we have a page number which 628 * is aligned. We assert that we aren't already 629 * in a different large page. 630 */ 631 ASSERT(IS_P2ALIGNED(pnum, large)); 632 ASSERT(root == NULL && cnt == 0); 633 634 /* 635 * If insufficient number of pages left to form 636 * a large page, just free the small page. 637 */ 638 if (num < large) { 639 pp->p_szc = 0; 640 page_free_at_startup(pp); 641 continue; 642 } 643 644 /* 645 * Otherwise start a new large page. 646 */ 647 pp->p_szc = szc; 648 cnt++; 649 root = pp; 650 } 651 ASSERT(root == NULL && cnt == 0); 652 } 653 654 /* 655 * Find a page representing the specified [vp, offset]. 656 * If we find the page but it is intransit coming in, 657 * it will have an "exclusive" lock and we wait for 658 * the i/o to complete. A page found on the free list 659 * is always reclaimed and then locked. On success, the page 660 * is locked, its data is valid and it isn't on the free 661 * list, while a NULL is returned if the page doesn't exist. 662 */ 663 page_t * 664 page_lookup(vnode_t *vp, u_offset_t off, se_t se) 665 { 666 return (page_lookup_create(vp, off, se, NULL, NULL, 0)); 667 } 668 669 /* 670 * Find a page representing the specified [vp, offset]. 671 * We either return the one we found or, if passed in, 672 * create one with identity of [vp, offset] of the 673 * pre-allocated page. If we find exsisting page but it is 674 * intransit coming in, it will have an "exclusive" lock 675 * and we wait for the i/o to complete. A page found on 676 * the free list is always reclaimed and then locked. 677 * On success, the page is locked, its data is valid and 678 * it isn't on the free list, while a NULL is returned 679 * if the page doesn't exist and newpp is NULL; 680 */ 681 page_t * 682 page_lookup_create( 683 vnode_t *vp, 684 u_offset_t off, 685 se_t se, 686 page_t *newpp, 687 spgcnt_t *nrelocp, 688 int flags) 689 { 690 page_t *pp; 691 kmutex_t *phm; 692 ulong_t index; 693 uint_t hash_locked; 694 uint_t es; 695 696 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 697 VM_STAT_ADD(page_lookup_cnt[0]); 698 ASSERT(newpp ? PAGE_EXCL(newpp) : 1); 699 700 /* 701 * Acquire the appropriate page hash lock since 702 * we have to search the hash list. Pages that 703 * hash to this list can't change identity while 704 * this lock is held. 705 */ 706 hash_locked = 0; 707 index = PAGE_HASH_FUNC(vp, off); 708 phm = NULL; 709 top: 710 PAGE_HASH_SEARCH(index, pp, vp, off); 711 if (pp != NULL) { 712 VM_STAT_ADD(page_lookup_cnt[1]); 713 es = (newpp != NULL) ? 1 : 0; 714 es |= flags; 715 if (!hash_locked) { 716 VM_STAT_ADD(page_lookup_cnt[2]); 717 if (!page_try_reclaim_lock(pp, se, es)) { 718 /* 719 * On a miss, acquire the phm. Then 720 * next time, page_lock() will be called, 721 * causing a wait if the page is busy. 722 * just looping with page_trylock() would 723 * get pretty boring. 724 */ 725 VM_STAT_ADD(page_lookup_cnt[3]); 726 phm = PAGE_HASH_MUTEX(index); 727 mutex_enter(phm); 728 hash_locked = 1; 729 goto top; 730 } 731 } else { 732 VM_STAT_ADD(page_lookup_cnt[4]); 733 if (!page_lock_es(pp, se, phm, P_RECLAIM, es)) { 734 VM_STAT_ADD(page_lookup_cnt[5]); 735 goto top; 736 } 737 } 738 739 /* 740 * Since `pp' is locked it can not change identity now. 741 * Reconfirm we locked the correct page. 742 * 743 * Both the p_vnode and p_offset *must* be cast volatile 744 * to force a reload of their values: The PAGE_HASH_SEARCH 745 * macro will have stuffed p_vnode and p_offset into 746 * registers before calling page_trylock(); another thread, 747 * actually holding the hash lock, could have changed the 748 * page's identity in memory, but our registers would not 749 * be changed, fooling the reconfirmation. If the hash 750 * lock was held during the search, the casting would 751 * not be needed. 752 */ 753 VM_STAT_ADD(page_lookup_cnt[6]); 754 if (((volatile struct vnode *)(pp->p_vnode) != vp) || 755 ((volatile u_offset_t)(pp->p_offset) != off)) { 756 VM_STAT_ADD(page_lookup_cnt[7]); 757 if (hash_locked) { 758 panic("page_lookup_create: lost page %p", 759 (void *)pp); 760 /*NOTREACHED*/ 761 } 762 page_unlock(pp); 763 phm = PAGE_HASH_MUTEX(index); 764 mutex_enter(phm); 765 hash_locked = 1; 766 goto top; 767 } 768 769 /* 770 * If page_trylock() was called, then pp may still be on 771 * the cachelist (can't be on the free list, it would not 772 * have been found in the search). If it is on the 773 * cachelist it must be pulled now. To pull the page from 774 * the cachelist, it must be exclusively locked. 775 * 776 * The other big difference between page_trylock() and 777 * page_lock(), is that page_lock() will pull the 778 * page from whatever free list (the cache list in this 779 * case) the page is on. If page_trylock() was used 780 * above, then we have to do the reclaim ourselves. 781 */ 782 if ((!hash_locked) && (PP_ISFREE(pp))) { 783 ASSERT(PP_ISAGED(pp) == 0); 784 VM_STAT_ADD(page_lookup_cnt[8]); 785 786 /* 787 * page_relcaim will insure that we 788 * have this page exclusively 789 */ 790 791 if (!page_reclaim(pp, NULL)) { 792 /* 793 * Page_reclaim dropped whatever lock 794 * we held. 795 */ 796 VM_STAT_ADD(page_lookup_cnt[9]); 797 phm = PAGE_HASH_MUTEX(index); 798 mutex_enter(phm); 799 hash_locked = 1; 800 goto top; 801 } else if (se == SE_SHARED && newpp == NULL) { 802 VM_STAT_ADD(page_lookup_cnt[10]); 803 page_downgrade(pp); 804 } 805 } 806 807 if (hash_locked) { 808 mutex_exit(phm); 809 } 810 811 if (newpp != NULL && pp->p_szc < newpp->p_szc && 812 PAGE_EXCL(pp) && nrelocp != NULL) { 813 ASSERT(nrelocp != NULL); 814 (void) page_relocate(&pp, &newpp, 1, 1, nrelocp, 815 NULL); 816 if (*nrelocp > 0) { 817 VM_STAT_COND_ADD(*nrelocp == 1, 818 page_lookup_cnt[11]); 819 VM_STAT_COND_ADD(*nrelocp > 1, 820 page_lookup_cnt[12]); 821 pp = newpp; 822 se = SE_EXCL; 823 } else { 824 if (se == SE_SHARED) { 825 page_downgrade(pp); 826 } 827 VM_STAT_ADD(page_lookup_cnt[13]); 828 } 829 } else if (newpp != NULL && nrelocp != NULL) { 830 if (PAGE_EXCL(pp) && se == SE_SHARED) { 831 page_downgrade(pp); 832 } 833 VM_STAT_COND_ADD(pp->p_szc < newpp->p_szc, 834 page_lookup_cnt[14]); 835 VM_STAT_COND_ADD(pp->p_szc == newpp->p_szc, 836 page_lookup_cnt[15]); 837 VM_STAT_COND_ADD(pp->p_szc > newpp->p_szc, 838 page_lookup_cnt[16]); 839 } else if (newpp != NULL && PAGE_EXCL(pp)) { 840 se = SE_EXCL; 841 } 842 } else if (!hash_locked) { 843 VM_STAT_ADD(page_lookup_cnt[17]); 844 phm = PAGE_HASH_MUTEX(index); 845 mutex_enter(phm); 846 hash_locked = 1; 847 goto top; 848 } else if (newpp != NULL) { 849 /* 850 * If we have a preallocated page then 851 * insert it now and basically behave like 852 * page_create. 853 */ 854 VM_STAT_ADD(page_lookup_cnt[18]); 855 /* 856 * Since we hold the page hash mutex and 857 * just searched for this page, page_hashin 858 * had better not fail. If it does, that 859 * means some thread did not follow the 860 * page hash mutex rules. Panic now and 861 * get it over with. As usual, go down 862 * holding all the locks. 863 */ 864 ASSERT(MUTEX_HELD(phm)); 865 if (!page_hashin(newpp, vp, off, phm)) { 866 ASSERT(MUTEX_HELD(phm)); 867 panic("page_lookup_create: hashin failed %p %p %llx %p", 868 (void *)newpp, (void *)vp, off, (void *)phm); 869 /*NOTREACHED*/ 870 } 871 ASSERT(MUTEX_HELD(phm)); 872 mutex_exit(phm); 873 phm = NULL; 874 page_set_props(newpp, P_REF); 875 page_io_lock(newpp); 876 pp = newpp; 877 se = SE_EXCL; 878 } else { 879 VM_STAT_ADD(page_lookup_cnt[19]); 880 mutex_exit(phm); 881 } 882 883 ASSERT(pp ? PAGE_LOCKED_SE(pp, se) : 1); 884 885 ASSERT(pp ? ((PP_ISFREE(pp) == 0) && (PP_ISAGED(pp) == 0)) : 1); 886 887 return (pp); 888 } 889 890 /* 891 * Search the hash list for the page representing the 892 * specified [vp, offset] and return it locked. Skip 893 * free pages and pages that cannot be locked as requested. 894 * Used while attempting to kluster pages. 895 */ 896 page_t * 897 page_lookup_nowait(vnode_t *vp, u_offset_t off, se_t se) 898 { 899 page_t *pp; 900 kmutex_t *phm; 901 ulong_t index; 902 uint_t locked; 903 904 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 905 VM_STAT_ADD(page_lookup_nowait_cnt[0]); 906 907 index = PAGE_HASH_FUNC(vp, off); 908 PAGE_HASH_SEARCH(index, pp, vp, off); 909 locked = 0; 910 if (pp == NULL) { 911 top: 912 VM_STAT_ADD(page_lookup_nowait_cnt[1]); 913 locked = 1; 914 phm = PAGE_HASH_MUTEX(index); 915 mutex_enter(phm); 916 PAGE_HASH_SEARCH(index, pp, vp, off); 917 } 918 919 if (pp == NULL || PP_ISFREE(pp)) { 920 VM_STAT_ADD(page_lookup_nowait_cnt[2]); 921 pp = NULL; 922 } else { 923 if (!page_trylock(pp, se)) { 924 VM_STAT_ADD(page_lookup_nowait_cnt[3]); 925 pp = NULL; 926 } else { 927 VM_STAT_ADD(page_lookup_nowait_cnt[4]); 928 /* 929 * See the comment in page_lookup() 930 */ 931 if (((volatile struct vnode *)(pp->p_vnode) != vp) || 932 ((u_offset_t)(pp->p_offset) != off)) { 933 VM_STAT_ADD(page_lookup_nowait_cnt[5]); 934 if (locked) { 935 panic("page_lookup_nowait %p", 936 (void *)pp); 937 /*NOTREACHED*/ 938 } 939 page_unlock(pp); 940 goto top; 941 } 942 if (PP_ISFREE(pp)) { 943 VM_STAT_ADD(page_lookup_nowait_cnt[6]); 944 page_unlock(pp); 945 pp = NULL; 946 } 947 } 948 } 949 if (locked) { 950 VM_STAT_ADD(page_lookup_nowait_cnt[7]); 951 mutex_exit(phm); 952 } 953 954 ASSERT(pp ? PAGE_LOCKED_SE(pp, se) : 1); 955 956 return (pp); 957 } 958 959 /* 960 * Search the hash list for a page with the specified [vp, off] 961 * that is known to exist and is already locked. This routine 962 * is typically used by segment SOFTUNLOCK routines. 963 */ 964 page_t * 965 page_find(vnode_t *vp, u_offset_t off) 966 { 967 page_t *pp; 968 kmutex_t *phm; 969 ulong_t index; 970 971 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 972 VM_STAT_ADD(page_find_cnt); 973 974 index = PAGE_HASH_FUNC(vp, off); 975 phm = PAGE_HASH_MUTEX(index); 976 977 mutex_enter(phm); 978 PAGE_HASH_SEARCH(index, pp, vp, off); 979 mutex_exit(phm); 980 981 ASSERT(pp == NULL || PAGE_LOCKED(pp) || panicstr); 982 return (pp); 983 } 984 985 /* 986 * Determine whether a page with the specified [vp, off] 987 * currently exists in the system. Obviously this should 988 * only be considered as a hint since nothing prevents the 989 * page from disappearing or appearing immediately after 990 * the return from this routine. Subsequently, we don't 991 * even bother to lock the list. 992 */ 993 page_t * 994 page_exists(vnode_t *vp, u_offset_t off) 995 { 996 page_t *pp; 997 ulong_t index; 998 999 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 1000 VM_STAT_ADD(page_exists_cnt); 1001 1002 index = PAGE_HASH_FUNC(vp, off); 1003 PAGE_HASH_SEARCH(index, pp, vp, off); 1004 1005 return (pp); 1006 } 1007 1008 /* 1009 * Determine if physically contiguous pages exist for [vp, off] - [vp, off + 1010 * page_size(szc)) range. if they exist and ppa is not NULL fill ppa array 1011 * with these pages locked SHARED. If necessary reclaim pages from 1012 * freelist. Return 1 if contiguous pages exist and 0 otherwise. 1013 * 1014 * If we fail to lock pages still return 1 if pages exist and contiguous. 1015 * But in this case return value is just a hint. ppa array won't be filled. 1016 * Caller should initialize ppa[0] as NULL to distinguish return value. 1017 * 1018 * Returns 0 if pages don't exist or not physically contiguous. 1019 * 1020 * This routine doesn't work for anonymous(swapfs) pages. 1021 */ 1022 int 1023 page_exists_physcontig(vnode_t *vp, u_offset_t off, uint_t szc, page_t *ppa[]) 1024 { 1025 pgcnt_t pages; 1026 pfn_t pfn; 1027 page_t *rootpp; 1028 pgcnt_t i; 1029 pgcnt_t j; 1030 u_offset_t save_off = off; 1031 ulong_t index; 1032 kmutex_t *phm; 1033 page_t *pp; 1034 uint_t pszc; 1035 int loopcnt = 0; 1036 1037 ASSERT(szc != 0); 1038 ASSERT(vp != NULL); 1039 ASSERT(!IS_SWAPFSVP(vp)); 1040 ASSERT(!VN_ISKAS(vp)); 1041 1042 again: 1043 if (++loopcnt > 3) { 1044 VM_STAT_ADD(page_exphcontg[0]); 1045 return (0); 1046 } 1047 1048 index = PAGE_HASH_FUNC(vp, off); 1049 phm = PAGE_HASH_MUTEX(index); 1050 1051 mutex_enter(phm); 1052 PAGE_HASH_SEARCH(index, pp, vp, off); 1053 mutex_exit(phm); 1054 1055 VM_STAT_ADD(page_exphcontg[1]); 1056 1057 if (pp == NULL) { 1058 VM_STAT_ADD(page_exphcontg[2]); 1059 return (0); 1060 } 1061 1062 pages = page_get_pagecnt(szc); 1063 rootpp = pp; 1064 pfn = rootpp->p_pagenum; 1065 1066 if ((pszc = pp->p_szc) >= szc && ppa != NULL) { 1067 VM_STAT_ADD(page_exphcontg[3]); 1068 if (!page_trylock(pp, SE_SHARED)) { 1069 VM_STAT_ADD(page_exphcontg[4]); 1070 return (1); 1071 } 1072 if (pp->p_szc != pszc || pp->p_vnode != vp || 1073 pp->p_offset != off) { 1074 VM_STAT_ADD(page_exphcontg[5]); 1075 page_unlock(pp); 1076 off = save_off; 1077 goto again; 1078 } 1079 /* 1080 * szc was non zero and vnode and offset matched after we 1081 * locked the page it means it can't become free on us. 1082 */ 1083 ASSERT(!PP_ISFREE(pp)); 1084 if (!IS_P2ALIGNED(pfn, pages)) { 1085 page_unlock(pp); 1086 return (0); 1087 } 1088 ppa[0] = pp; 1089 pp++; 1090 off += PAGESIZE; 1091 pfn++; 1092 for (i = 1; i < pages; i++, pp++, off += PAGESIZE, pfn++) { 1093 if (!page_trylock(pp, SE_SHARED)) { 1094 VM_STAT_ADD(page_exphcontg[6]); 1095 pp--; 1096 while (i-- > 0) { 1097 page_unlock(pp); 1098 pp--; 1099 } 1100 ppa[0] = NULL; 1101 return (1); 1102 } 1103 if (pp->p_szc != pszc) { 1104 VM_STAT_ADD(page_exphcontg[7]); 1105 page_unlock(pp); 1106 pp--; 1107 while (i-- > 0) { 1108 page_unlock(pp); 1109 pp--; 1110 } 1111 ppa[0] = NULL; 1112 off = save_off; 1113 goto again; 1114 } 1115 /* 1116 * szc the same as for previous already locked pages 1117 * with right identity. Since this page had correct 1118 * szc after we locked it can't get freed or destroyed 1119 * and therefore must have the expected identity. 1120 */ 1121 ASSERT(!PP_ISFREE(pp)); 1122 if (pp->p_vnode != vp || 1123 pp->p_offset != off) { 1124 panic("page_exists_physcontig: " 1125 "large page identity doesn't match"); 1126 } 1127 ppa[i] = pp; 1128 ASSERT(pp->p_pagenum == pfn); 1129 } 1130 VM_STAT_ADD(page_exphcontg[8]); 1131 ppa[pages] = NULL; 1132 return (1); 1133 } else if (pszc >= szc) { 1134 VM_STAT_ADD(page_exphcontg[9]); 1135 if (!IS_P2ALIGNED(pfn, pages)) { 1136 return (0); 1137 } 1138 return (1); 1139 } 1140 1141 if (!IS_P2ALIGNED(pfn, pages)) { 1142 VM_STAT_ADD(page_exphcontg[10]); 1143 return (0); 1144 } 1145 1146 if (page_numtomemseg_nolock(pfn) != 1147 page_numtomemseg_nolock(pfn + pages - 1)) { 1148 VM_STAT_ADD(page_exphcontg[11]); 1149 return (0); 1150 } 1151 1152 /* 1153 * We loop up 4 times across pages to promote page size. 1154 * We're extra cautious to promote page size atomically with respect 1155 * to everybody else. But we can probably optimize into 1 loop if 1156 * this becomes an issue. 1157 */ 1158 1159 for (i = 0; i < pages; i++, pp++, off += PAGESIZE, pfn++) { 1160 ASSERT(pp->p_pagenum == pfn); 1161 if (!page_trylock(pp, SE_EXCL)) { 1162 VM_STAT_ADD(page_exphcontg[12]); 1163 break; 1164 } 1165 if (pp->p_vnode != vp || 1166 pp->p_offset != off) { 1167 VM_STAT_ADD(page_exphcontg[13]); 1168 page_unlock(pp); 1169 break; 1170 } 1171 if (pp->p_szc >= szc) { 1172 ASSERT(i == 0); 1173 page_unlock(pp); 1174 off = save_off; 1175 goto again; 1176 } 1177 } 1178 1179 if (i != pages) { 1180 VM_STAT_ADD(page_exphcontg[14]); 1181 --pp; 1182 while (i-- > 0) { 1183 page_unlock(pp); 1184 --pp; 1185 } 1186 return (0); 1187 } 1188 1189 pp = rootpp; 1190 for (i = 0; i < pages; i++, pp++) { 1191 if (PP_ISFREE(pp)) { 1192 VM_STAT_ADD(page_exphcontg[15]); 1193 ASSERT(!PP_ISAGED(pp)); 1194 ASSERT(pp->p_szc == 0); 1195 if (!page_reclaim(pp, NULL)) { 1196 break; 1197 } 1198 } else { 1199 ASSERT(pp->p_szc < szc); 1200 VM_STAT_ADD(page_exphcontg[16]); 1201 (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 1202 } 1203 } 1204 if (i < pages) { 1205 VM_STAT_ADD(page_exphcontg[17]); 1206 /* 1207 * page_reclaim failed because we were out of memory. 1208 * drop the rest of the locks and return because this page 1209 * must be already reallocated anyway. 1210 */ 1211 pp = rootpp; 1212 for (j = 0; j < pages; j++, pp++) { 1213 if (j != i) { 1214 page_unlock(pp); 1215 } 1216 } 1217 return (0); 1218 } 1219 1220 off = save_off; 1221 pp = rootpp; 1222 for (i = 0; i < pages; i++, pp++, off += PAGESIZE) { 1223 ASSERT(PAGE_EXCL(pp)); 1224 ASSERT(!PP_ISFREE(pp)); 1225 ASSERT(!hat_page_is_mapped(pp)); 1226 ASSERT(pp->p_vnode == vp); 1227 ASSERT(pp->p_offset == off); 1228 pp->p_szc = szc; 1229 } 1230 pp = rootpp; 1231 for (i = 0; i < pages; i++, pp++) { 1232 if (ppa == NULL) { 1233 page_unlock(pp); 1234 } else { 1235 ppa[i] = pp; 1236 page_downgrade(ppa[i]); 1237 } 1238 } 1239 if (ppa != NULL) { 1240 ppa[pages] = NULL; 1241 } 1242 VM_STAT_ADD(page_exphcontg[18]); 1243 ASSERT(vp->v_pages != NULL); 1244 return (1); 1245 } 1246 1247 /* 1248 * Determine whether a page with the specified [vp, off] 1249 * currently exists in the system and if so return its 1250 * size code. Obviously this should only be considered as 1251 * a hint since nothing prevents the page from disappearing 1252 * or appearing immediately after the return from this routine. 1253 */ 1254 int 1255 page_exists_forreal(vnode_t *vp, u_offset_t off, uint_t *szc) 1256 { 1257 page_t *pp; 1258 kmutex_t *phm; 1259 ulong_t index; 1260 int rc = 0; 1261 1262 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 1263 ASSERT(szc != NULL); 1264 VM_STAT_ADD(page_exists_forreal_cnt); 1265 1266 index = PAGE_HASH_FUNC(vp, off); 1267 phm = PAGE_HASH_MUTEX(index); 1268 1269 mutex_enter(phm); 1270 PAGE_HASH_SEARCH(index, pp, vp, off); 1271 if (pp != NULL) { 1272 *szc = pp->p_szc; 1273 rc = 1; 1274 } 1275 mutex_exit(phm); 1276 return (rc); 1277 } 1278 1279 /* wakeup threads waiting for pages in page_create_get_something() */ 1280 void 1281 wakeup_pcgs(void) 1282 { 1283 if (!CV_HAS_WAITERS(&pcgs_cv)) 1284 return; 1285 cv_broadcast(&pcgs_cv); 1286 } 1287 1288 /* 1289 * 'freemem' is used all over the kernel as an indication of how many 1290 * pages are free (either on the cache list or on the free page list) 1291 * in the system. In very few places is a really accurate 'freemem' 1292 * needed. To avoid contention of the lock protecting a the 1293 * single freemem, it was spread out into NCPU buckets. Set_freemem 1294 * sets freemem to the total of all NCPU buckets. It is called from 1295 * clock() on each TICK. 1296 */ 1297 void 1298 set_freemem() 1299 { 1300 struct pcf *p; 1301 ulong_t t; 1302 uint_t i; 1303 1304 t = 0; 1305 p = pcf; 1306 for (i = 0; i < PCF_FANOUT; i++) { 1307 t += p->pcf_count; 1308 p++; 1309 } 1310 freemem = t; 1311 1312 /* 1313 * Don't worry about grabbing mutex. It's not that 1314 * critical if we miss a tick or two. This is 1315 * where we wakeup possible delayers in 1316 * page_create_get_something(). 1317 */ 1318 wakeup_pcgs(); 1319 } 1320 1321 ulong_t 1322 get_freemem() 1323 { 1324 struct pcf *p; 1325 ulong_t t; 1326 uint_t i; 1327 1328 t = 0; 1329 p = pcf; 1330 for (i = 0; i < PCF_FANOUT; i++) { 1331 t += p->pcf_count; 1332 p++; 1333 } 1334 /* 1335 * We just calculated it, might as well set it. 1336 */ 1337 freemem = t; 1338 return (t); 1339 } 1340 1341 /* 1342 * Acquire all of the page cache & free (pcf) locks. 1343 */ 1344 void 1345 pcf_acquire_all() 1346 { 1347 struct pcf *p; 1348 uint_t i; 1349 1350 p = pcf; 1351 for (i = 0; i < PCF_FANOUT; i++) { 1352 mutex_enter(&p->pcf_lock); 1353 p++; 1354 } 1355 } 1356 1357 /* 1358 * Release all the pcf_locks. 1359 */ 1360 void 1361 pcf_release_all() 1362 { 1363 struct pcf *p; 1364 uint_t i; 1365 1366 p = pcf; 1367 for (i = 0; i < PCF_FANOUT; i++) { 1368 mutex_exit(&p->pcf_lock); 1369 p++; 1370 } 1371 } 1372 1373 /* 1374 * Inform the VM system that we need some pages freed up. 1375 * Calls must be symmetric, e.g.: 1376 * 1377 * page_needfree(100); 1378 * wait a bit; 1379 * page_needfree(-100); 1380 */ 1381 void 1382 page_needfree(spgcnt_t npages) 1383 { 1384 mutex_enter(&new_freemem_lock); 1385 needfree += npages; 1386 mutex_exit(&new_freemem_lock); 1387 } 1388 1389 /* 1390 * Throttle for page_create(): try to prevent freemem from dropping 1391 * below throttlefree. We can't provide a 100% guarantee because 1392 * KM_NOSLEEP allocations, page_reclaim(), and various other things 1393 * nibble away at the freelist. However, we can block all PG_WAIT 1394 * allocations until memory becomes available. The motivation is 1395 * that several things can fall apart when there's no free memory: 1396 * 1397 * (1) If pageout() needs memory to push a page, the system deadlocks. 1398 * 1399 * (2) By (broken) specification, timeout(9F) can neither fail nor 1400 * block, so it has no choice but to panic the system if it 1401 * cannot allocate a callout structure. 1402 * 1403 * (3) Like timeout(), ddi_set_callback() cannot fail and cannot block; 1404 * it panics if it cannot allocate a callback structure. 1405 * 1406 * (4) Untold numbers of third-party drivers have not yet been hardened 1407 * against KM_NOSLEEP and/or allocb() failures; they simply assume 1408 * success and panic the system with a data fault on failure. 1409 * (The long-term solution to this particular problem is to ship 1410 * hostile fault-injecting DEBUG kernels with the DDK.) 1411 * 1412 * It is theoretically impossible to guarantee success of non-blocking 1413 * allocations, but in practice, this throttle is very hard to break. 1414 */ 1415 static int 1416 page_create_throttle(pgcnt_t npages, int flags) 1417 { 1418 ulong_t fm; 1419 uint_t i; 1420 pgcnt_t tf; /* effective value of throttlefree */ 1421 1422 /* 1423 * Never deny pages when: 1424 * - it's a thread that cannot block [NOMEMWAIT()] 1425 * - the allocation cannot block and must not fail 1426 * - the allocation cannot block and is pageout dispensated 1427 */ 1428 if (NOMEMWAIT() || 1429 ((flags & (PG_WAIT | PG_PANIC)) == PG_PANIC) || 1430 ((flags & (PG_WAIT | PG_PUSHPAGE)) == PG_PUSHPAGE)) 1431 return (1); 1432 1433 /* 1434 * If the allocation can't block, we look favorably upon it 1435 * unless we're below pageout_reserve. In that case we fail 1436 * the allocation because we want to make sure there are a few 1437 * pages available for pageout. 1438 */ 1439 if ((flags & PG_WAIT) == 0) 1440 return (freemem >= npages + pageout_reserve); 1441 1442 /* Calculate the effective throttlefree value */ 1443 tf = throttlefree - 1444 ((flags & PG_PUSHPAGE) ? pageout_reserve : 0); 1445 1446 cv_signal(&proc_pageout->p_cv); 1447 1448 while (freemem < npages + tf) { 1449 pcf_acquire_all(); 1450 mutex_enter(&new_freemem_lock); 1451 fm = 0; 1452 for (i = 0; i < PCF_FANOUT; i++) { 1453 fm += pcf[i].pcf_count; 1454 pcf[i].pcf_wait++; 1455 mutex_exit(&pcf[i].pcf_lock); 1456 } 1457 freemem = fm; 1458 needfree += npages; 1459 freemem_wait++; 1460 cv_wait(&freemem_cv, &new_freemem_lock); 1461 freemem_wait--; 1462 needfree -= npages; 1463 mutex_exit(&new_freemem_lock); 1464 } 1465 return (1); 1466 } 1467 1468 /* 1469 * page_create_wait() is called to either coalecse pages from the 1470 * different pcf buckets or to wait because there simply are not 1471 * enough pages to satisfy the caller's request. 1472 * 1473 * Sadly, this is called from platform/vm/vm_machdep.c 1474 */ 1475 int 1476 page_create_wait(size_t npages, uint_t flags) 1477 { 1478 pgcnt_t total; 1479 uint_t i; 1480 struct pcf *p; 1481 1482 /* 1483 * Wait until there are enough free pages to satisfy our 1484 * entire request. 1485 * We set needfree += npages before prodding pageout, to make sure 1486 * it does real work when npages > lotsfree > freemem. 1487 */ 1488 VM_STAT_ADD(page_create_not_enough); 1489 1490 ASSERT(!kcage_on ? !(flags & PG_NORELOC) : 1); 1491 checkagain: 1492 if ((flags & PG_NORELOC) && 1493 kcage_freemem < kcage_throttlefree + npages) 1494 (void) kcage_create_throttle(npages, flags); 1495 1496 if (freemem < npages + throttlefree) 1497 if (!page_create_throttle(npages, flags)) 1498 return (0); 1499 1500 /* 1501 * Since page_create_va() looked at every 1502 * bucket, assume we are going to have to wait. 1503 * Get all of the pcf locks. 1504 */ 1505 total = 0; 1506 p = pcf; 1507 for (i = 0; i < PCF_FANOUT; i++) { 1508 mutex_enter(&p->pcf_lock); 1509 total += p->pcf_count; 1510 if (total >= npages) { 1511 /* 1512 * Wow! There are enough pages laying around 1513 * to satisfy the request. Do the accounting, 1514 * drop the locks we acquired, and go back. 1515 * 1516 * freemem is not protected by any lock. So, 1517 * we cannot have any assertion containing 1518 * freemem. 1519 */ 1520 freemem -= npages; 1521 1522 while (p >= pcf) { 1523 if (p->pcf_count <= npages) { 1524 npages -= p->pcf_count; 1525 p->pcf_count = 0; 1526 } else { 1527 p->pcf_count -= (uint_t)npages; 1528 npages = 0; 1529 } 1530 mutex_exit(&p->pcf_lock); 1531 p--; 1532 } 1533 ASSERT(npages == 0); 1534 return (1); 1535 } 1536 p++; 1537 } 1538 1539 /* 1540 * All of the pcf locks are held, there are not enough pages 1541 * to satisfy the request (npages < total). 1542 * Be sure to acquire the new_freemem_lock before dropping 1543 * the pcf locks. This prevents dropping wakeups in page_free(). 1544 * The order is always pcf_lock then new_freemem_lock. 1545 * 1546 * Since we hold all the pcf locks, it is a good time to set freemem. 1547 * 1548 * If the caller does not want to wait, return now. 1549 * Else turn the pageout daemon loose to find something 1550 * and wait till it does. 1551 * 1552 */ 1553 freemem = total; 1554 1555 if ((flags & PG_WAIT) == 0) { 1556 pcf_release_all(); 1557 1558 TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_NOMEM, 1559 "page_create_nomem:npages %ld freemem %ld", npages, freemem); 1560 return (0); 1561 } 1562 1563 ASSERT(proc_pageout != NULL); 1564 cv_signal(&proc_pageout->p_cv); 1565 1566 TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SLEEP_START, 1567 "page_create_sleep_start: freemem %ld needfree %ld", 1568 freemem, needfree); 1569 1570 /* 1571 * We are going to wait. 1572 * We currently hold all of the pcf_locks, 1573 * get the new_freemem_lock (it protects freemem_wait), 1574 * before dropping the pcf_locks. 1575 */ 1576 mutex_enter(&new_freemem_lock); 1577 1578 p = pcf; 1579 for (i = 0; i < PCF_FANOUT; i++) { 1580 p->pcf_wait++; 1581 mutex_exit(&p->pcf_lock); 1582 p++; 1583 } 1584 1585 needfree += npages; 1586 freemem_wait++; 1587 1588 cv_wait(&freemem_cv, &new_freemem_lock); 1589 1590 freemem_wait--; 1591 needfree -= npages; 1592 1593 mutex_exit(&new_freemem_lock); 1594 1595 TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SLEEP_END, 1596 "page_create_sleep_end: freemem %ld needfree %ld", 1597 freemem, needfree); 1598 1599 VM_STAT_ADD(page_create_not_enough_again); 1600 goto checkagain; 1601 } 1602 1603 /* 1604 * A routine to do the opposite of page_create_wait(). 1605 */ 1606 void 1607 page_create_putback(spgcnt_t npages) 1608 { 1609 struct pcf *p; 1610 pgcnt_t lump; 1611 uint_t *which; 1612 1613 /* 1614 * When a contiguous lump is broken up, we have to 1615 * deal with lots of pages (min 64) so lets spread 1616 * the wealth around. 1617 */ 1618 lump = roundup(npages, PCF_FANOUT) / PCF_FANOUT; 1619 freemem += npages; 1620 1621 for (p = pcf; (npages > 0) && (p < &pcf[PCF_FANOUT]); p++) { 1622 which = &p->pcf_count; 1623 1624 mutex_enter(&p->pcf_lock); 1625 1626 if (p->pcf_block) { 1627 which = &p->pcf_reserve; 1628 } 1629 1630 if (lump < npages) { 1631 *which += (uint_t)lump; 1632 npages -= lump; 1633 } else { 1634 *which += (uint_t)npages; 1635 npages = 0; 1636 } 1637 1638 if (p->pcf_wait) { 1639 mutex_enter(&new_freemem_lock); 1640 /* 1641 * Check to see if some other thread 1642 * is actually waiting. Another bucket 1643 * may have woken it up by now. If there 1644 * are no waiters, then set our pcf_wait 1645 * count to zero to avoid coming in here 1646 * next time. 1647 */ 1648 if (freemem_wait) { 1649 if (npages > 1) { 1650 cv_broadcast(&freemem_cv); 1651 } else { 1652 cv_signal(&freemem_cv); 1653 } 1654 p->pcf_wait--; 1655 } else { 1656 p->pcf_wait = 0; 1657 } 1658 mutex_exit(&new_freemem_lock); 1659 } 1660 mutex_exit(&p->pcf_lock); 1661 } 1662 ASSERT(npages == 0); 1663 } 1664 1665 /* 1666 * A helper routine for page_create_get_something. 1667 * The indenting got to deep down there. 1668 * Unblock the pcf counters. Any pages freed after 1669 * pcf_block got set are moved to pcf_count and 1670 * wakeups (cv_broadcast() or cv_signal()) are done as needed. 1671 */ 1672 static void 1673 pcgs_unblock(void) 1674 { 1675 int i; 1676 struct pcf *p; 1677 1678 /* Update freemem while we're here. */ 1679 freemem = 0; 1680 p = pcf; 1681 for (i = 0; i < PCF_FANOUT; i++) { 1682 mutex_enter(&p->pcf_lock); 1683 ASSERT(p->pcf_count == 0); 1684 p->pcf_count = p->pcf_reserve; 1685 p->pcf_block = 0; 1686 freemem += p->pcf_count; 1687 if (p->pcf_wait) { 1688 mutex_enter(&new_freemem_lock); 1689 if (freemem_wait) { 1690 if (p->pcf_reserve > 1) { 1691 cv_broadcast(&freemem_cv); 1692 p->pcf_wait = 0; 1693 } else { 1694 cv_signal(&freemem_cv); 1695 p->pcf_wait--; 1696 } 1697 } else { 1698 p->pcf_wait = 0; 1699 } 1700 mutex_exit(&new_freemem_lock); 1701 } 1702 p->pcf_reserve = 0; 1703 mutex_exit(&p->pcf_lock); 1704 p++; 1705 } 1706 } 1707 1708 /* 1709 * Called from page_create_va() when both the cache and free lists 1710 * have been checked once. 1711 * 1712 * Either returns a page or panics since the accounting was done 1713 * way before we got here. 1714 * 1715 * We don't come here often, so leave the accounting on permanently. 1716 */ 1717 1718 #define MAX_PCGS 100 1719 1720 #ifdef DEBUG 1721 #define PCGS_TRIES 100 1722 #else /* DEBUG */ 1723 #define PCGS_TRIES 10 1724 #endif /* DEBUG */ 1725 1726 #ifdef VM_STATS 1727 uint_t pcgs_counts[PCGS_TRIES]; 1728 uint_t pcgs_too_many; 1729 uint_t pcgs_entered; 1730 uint_t pcgs_entered_noreloc; 1731 uint_t pcgs_locked; 1732 uint_t pcgs_cagelocked; 1733 #endif /* VM_STATS */ 1734 1735 static page_t * 1736 page_create_get_something(vnode_t *vp, u_offset_t off, struct seg *seg, 1737 caddr_t vaddr, uint_t flags) 1738 { 1739 uint_t count; 1740 page_t *pp; 1741 uint_t locked, i; 1742 struct pcf *p; 1743 lgrp_t *lgrp; 1744 int cagelocked = 0; 1745 1746 VM_STAT_ADD(pcgs_entered); 1747 1748 /* 1749 * Tap any reserve freelists: if we fail now, we'll die 1750 * since the page(s) we're looking for have already been 1751 * accounted for. 1752 */ 1753 flags |= PG_PANIC; 1754 1755 if ((flags & PG_NORELOC) != 0) { 1756 VM_STAT_ADD(pcgs_entered_noreloc); 1757 /* 1758 * Requests for free pages from critical threads 1759 * such as pageout still won't throttle here, but 1760 * we must try again, to give the cageout thread 1761 * another chance to catch up. Since we already 1762 * accounted for the pages, we had better get them 1763 * this time. 1764 * 1765 * N.B. All non-critical threads acquire the pcgs_cagelock 1766 * to serialize access to the freelists. This implements a 1767 * turnstile-type synchornization to avoid starvation of 1768 * critical requests for PG_NORELOC memory by non-critical 1769 * threads: all non-critical threads must acquire a 'ticket' 1770 * before passing through, which entails making sure 1771 * kcage_freemem won't fall below minfree prior to grabbing 1772 * pages from the freelists. 1773 */ 1774 if (kcage_create_throttle(1, flags) == KCT_NONCRIT) { 1775 mutex_enter(&pcgs_cagelock); 1776 cagelocked = 1; 1777 VM_STAT_ADD(pcgs_cagelocked); 1778 } 1779 } 1780 1781 /* 1782 * Time to get serious. 1783 * We failed to get a `correctly colored' page from both the 1784 * free and cache lists. 1785 * We escalate in stage. 1786 * 1787 * First try both lists without worring about color. 1788 * 1789 * Then, grab all page accounting locks (ie. pcf[]) and 1790 * steal any pages that they have and set the pcf_block flag to 1791 * stop deletions from the lists. This will help because 1792 * a page can get added to the free list while we are looking 1793 * at the cache list, then another page could be added to the cache 1794 * list allowing the page on the free list to be removed as we 1795 * move from looking at the cache list to the free list. This 1796 * could happen over and over. We would never find the page 1797 * we have accounted for. 1798 * 1799 * Noreloc pages are a subset of the global (relocatable) page pool. 1800 * They are not tracked separately in the pcf bins, so it is 1801 * impossible to know when doing pcf accounting if the available 1802 * page(s) are noreloc pages or not. When looking for a noreloc page 1803 * it is quite easy to end up here even if the global (relocatable) 1804 * page pool has plenty of free pages but the noreloc pool is empty. 1805 * 1806 * When the noreloc pool is empty (or low), additional noreloc pages 1807 * are created by converting pages from the global page pool. This 1808 * process will stall during pcf accounting if the pcf bins are 1809 * already locked. Such is the case when a noreloc allocation is 1810 * looping here in page_create_get_something waiting for more noreloc 1811 * pages to appear. 1812 * 1813 * Short of adding a new field to the pcf bins to accurately track 1814 * the number of free noreloc pages, we instead do not grab the 1815 * pcgs_lock, do not set the pcf blocks and do not timeout when 1816 * allocating a noreloc page. This allows noreloc allocations to 1817 * loop without blocking global page pool allocations. 1818 * 1819 * NOTE: the behaviour of page_create_get_something has not changed 1820 * for the case of global page pool allocations. 1821 */ 1822 1823 flags &= ~PG_MATCH_COLOR; 1824 locked = 0; 1825 #if defined(__i386) || defined(__amd64) 1826 /* 1827 * page_create_get_something may be called because 4g memory may be 1828 * depleted. Set flags to allow for relocation of base page below 1829 * 4g if necessary. 1830 */ 1831 if (physmax4g) 1832 flags |= (PGI_PGCPSZC0 | PGI_PGCPHIPRI); 1833 #endif 1834 1835 lgrp = lgrp_mem_choose(seg, vaddr, PAGESIZE); 1836 1837 for (count = 0; kcage_on || count < MAX_PCGS; count++) { 1838 pp = page_get_freelist(vp, off, seg, vaddr, PAGESIZE, 1839 flags, lgrp); 1840 if (pp == NULL) { 1841 pp = page_get_cachelist(vp, off, seg, vaddr, 1842 flags, lgrp); 1843 } 1844 if (pp == NULL) { 1845 /* 1846 * Serialize. Don't fight with other pcgs(). 1847 */ 1848 if (!locked && (!kcage_on || !(flags & PG_NORELOC))) { 1849 mutex_enter(&pcgs_lock); 1850 VM_STAT_ADD(pcgs_locked); 1851 locked = 1; 1852 p = pcf; 1853 for (i = 0; i < PCF_FANOUT; i++) { 1854 mutex_enter(&p->pcf_lock); 1855 ASSERT(p->pcf_block == 0); 1856 p->pcf_block = 1; 1857 p->pcf_reserve = p->pcf_count; 1858 p->pcf_count = 0; 1859 mutex_exit(&p->pcf_lock); 1860 p++; 1861 } 1862 freemem = 0; 1863 } 1864 1865 if (count) { 1866 /* 1867 * Since page_free() puts pages on 1868 * a list then accounts for it, we 1869 * just have to wait for page_free() 1870 * to unlock any page it was working 1871 * with. The page_lock()-page_reclaim() 1872 * path falls in the same boat. 1873 * 1874 * We don't need to check on the 1875 * PG_WAIT flag, we have already 1876 * accounted for the page we are 1877 * looking for in page_create_va(). 1878 * 1879 * We just wait a moment to let any 1880 * locked pages on the lists free up, 1881 * then continue around and try again. 1882 * 1883 * Will be awakened by set_freemem(). 1884 */ 1885 mutex_enter(&pcgs_wait_lock); 1886 cv_wait(&pcgs_cv, &pcgs_wait_lock); 1887 mutex_exit(&pcgs_wait_lock); 1888 } 1889 } else { 1890 #ifdef VM_STATS 1891 if (count >= PCGS_TRIES) { 1892 VM_STAT_ADD(pcgs_too_many); 1893 } else { 1894 VM_STAT_ADD(pcgs_counts[count]); 1895 } 1896 #endif 1897 if (locked) { 1898 pcgs_unblock(); 1899 mutex_exit(&pcgs_lock); 1900 } 1901 if (cagelocked) 1902 mutex_exit(&pcgs_cagelock); 1903 return (pp); 1904 } 1905 } 1906 /* 1907 * we go down holding the pcf locks. 1908 */ 1909 panic("no %spage found %d", 1910 ((flags & PG_NORELOC) ? "non-reloc " : ""), count); 1911 /*NOTREACHED*/ 1912 } 1913 1914 /* 1915 * Create enough pages for "bytes" worth of data starting at 1916 * "off" in "vp". 1917 * 1918 * Where flag must be one of: 1919 * 1920 * PG_EXCL: Exclusive create (fail if any page already 1921 * exists in the page cache) which does not 1922 * wait for memory to become available. 1923 * 1924 * PG_WAIT: Non-exclusive create which can wait for 1925 * memory to become available. 1926 * 1927 * PG_PHYSCONTIG: Allocate physically contiguous pages. 1928 * (Not Supported) 1929 * 1930 * A doubly linked list of pages is returned to the caller. Each page 1931 * on the list has the "exclusive" (p_selock) lock and "iolock" (p_iolock) 1932 * lock. 1933 * 1934 * Unable to change the parameters to page_create() in a minor release, 1935 * we renamed page_create() to page_create_va(), changed all known calls 1936 * from page_create() to page_create_va(), and created this wrapper. 1937 * 1938 * Upon a major release, we should break compatibility by deleting this 1939 * wrapper, and replacing all the strings "page_create_va", with "page_create". 1940 * 1941 * NOTE: There is a copy of this interface as page_create_io() in 1942 * i86/vm/vm_machdep.c. Any bugs fixed here should be applied 1943 * there. 1944 */ 1945 page_t * 1946 page_create(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags) 1947 { 1948 caddr_t random_vaddr; 1949 struct seg kseg; 1950 1951 #ifdef DEBUG 1952 cmn_err(CE_WARN, "Using deprecated interface page_create: caller %p", 1953 (void *)caller()); 1954 #endif 1955 1956 random_vaddr = (caddr_t)(((uintptr_t)vp >> 7) ^ 1957 (uintptr_t)(off >> PAGESHIFT)); 1958 kseg.s_as = &kas; 1959 1960 return (page_create_va(vp, off, bytes, flags, &kseg, random_vaddr)); 1961 } 1962 1963 #ifdef DEBUG 1964 uint32_t pg_alloc_pgs_mtbf = 0; 1965 #endif 1966 1967 /* 1968 * Used for large page support. It will attempt to allocate 1969 * a large page(s) off the freelist. 1970 * 1971 * Returns non zero on failure. 1972 */ 1973 int 1974 page_alloc_pages(struct vnode *vp, struct seg *seg, caddr_t addr, 1975 page_t **basepp, page_t *ppa[], uint_t szc, int anypgsz) 1976 { 1977 pgcnt_t npgs, curnpgs, totpgs; 1978 size_t pgsz; 1979 page_t *pplist = NULL, *pp; 1980 int err = 0; 1981 lgrp_t *lgrp; 1982 1983 ASSERT(szc != 0 && szc <= (page_num_pagesizes() - 1)); 1984 1985 VM_STAT_ADD(alloc_pages[0]); 1986 1987 #ifdef DEBUG 1988 if (pg_alloc_pgs_mtbf && !(gethrtime() % pg_alloc_pgs_mtbf)) { 1989 return (ENOMEM); 1990 } 1991 #endif 1992 1993 pgsz = page_get_pagesize(szc); 1994 totpgs = curnpgs = npgs = pgsz >> PAGESHIFT; 1995 1996 ASSERT(((uintptr_t)addr & (pgsz - 1)) == 0); 1997 /* 1998 * One must be NULL but not both. 1999 * And one must be non NULL but not both. 2000 */ 2001 ASSERT(basepp != NULL || ppa != NULL); 2002 ASSERT(basepp == NULL || ppa == NULL); 2003 2004 (void) page_create_wait(npgs, PG_WAIT); 2005 2006 while (npgs && szc) { 2007 lgrp = lgrp_mem_choose(seg, addr, pgsz); 2008 pp = page_get_freelist(vp, 0, seg, addr, pgsz, 0, lgrp); 2009 if (pp != NULL) { 2010 VM_STAT_ADD(alloc_pages[1]); 2011 page_list_concat(&pplist, &pp); 2012 ASSERT(npgs >= curnpgs); 2013 npgs -= curnpgs; 2014 } else if (anypgsz) { 2015 VM_STAT_ADD(alloc_pages[2]); 2016 szc--; 2017 pgsz = page_get_pagesize(szc); 2018 curnpgs = pgsz >> PAGESHIFT; 2019 } else { 2020 VM_STAT_ADD(alloc_pages[3]); 2021 ASSERT(npgs == totpgs); 2022 page_create_putback(npgs); 2023 return (ENOMEM); 2024 } 2025 } 2026 if (szc == 0) { 2027 VM_STAT_ADD(alloc_pages[4]); 2028 ASSERT(npgs != 0); 2029 page_create_putback(npgs); 2030 err = ENOMEM; 2031 } else if (basepp != NULL) { 2032 ASSERT(npgs == 0); 2033 ASSERT(ppa == NULL); 2034 *basepp = pplist; 2035 } 2036 2037 npgs = totpgs - npgs; 2038 pp = pplist; 2039 2040 /* 2041 * Clear the free and age bits. Also if we were passed in a ppa then 2042 * fill it in with all the constituent pages from the large page. But 2043 * if we failed to allocate all the pages just free what we got. 2044 */ 2045 while (npgs != 0) { 2046 ASSERT(PP_ISFREE(pp)); 2047 ASSERT(PP_ISAGED(pp)); 2048 if (ppa != NULL || err != 0) { 2049 if (err == 0) { 2050 VM_STAT_ADD(alloc_pages[5]); 2051 PP_CLRFREE(pp); 2052 PP_CLRAGED(pp); 2053 page_sub(&pplist, pp); 2054 *ppa++ = pp; 2055 npgs--; 2056 } else { 2057 VM_STAT_ADD(alloc_pages[6]); 2058 ASSERT(pp->p_szc != 0); 2059 curnpgs = page_get_pagecnt(pp->p_szc); 2060 page_list_break(&pp, &pplist, curnpgs); 2061 page_list_add_pages(pp, 0); 2062 page_create_putback(curnpgs); 2063 ASSERT(npgs >= curnpgs); 2064 npgs -= curnpgs; 2065 } 2066 pp = pplist; 2067 } else { 2068 VM_STAT_ADD(alloc_pages[7]); 2069 PP_CLRFREE(pp); 2070 PP_CLRAGED(pp); 2071 pp = pp->p_next; 2072 npgs--; 2073 } 2074 } 2075 return (err); 2076 } 2077 2078 /* 2079 * Get a single large page off of the freelists, and set it up for use. 2080 * Number of bytes requested must be a supported page size. 2081 * 2082 * Note that this call may fail even if there is sufficient 2083 * memory available or PG_WAIT is set, so the caller must 2084 * be willing to fallback on page_create_va(), block and retry, 2085 * or fail the requester. 2086 */ 2087 page_t * 2088 page_create_va_large(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags, 2089 struct seg *seg, caddr_t vaddr, void *arg) 2090 { 2091 pgcnt_t npages, pcftotal; 2092 page_t *pp; 2093 page_t *rootpp; 2094 lgrp_t *lgrp; 2095 uint_t enough; 2096 uint_t pcf_index; 2097 uint_t i; 2098 struct pcf *p; 2099 struct pcf *q; 2100 lgrp_id_t *lgrpid = (lgrp_id_t *)arg; 2101 2102 ASSERT(vp != NULL); 2103 2104 ASSERT((flags & ~(PG_EXCL | PG_WAIT | 2105 PG_NORELOC | PG_PANIC | PG_PUSHPAGE)) == 0); 2106 /* but no others */ 2107 2108 ASSERT((flags & PG_EXCL) == PG_EXCL); 2109 2110 npages = btop(bytes); 2111 2112 if (!kcage_on || panicstr) { 2113 /* 2114 * Cage is OFF, or we are single threaded in 2115 * panic, so make everything a RELOC request. 2116 */ 2117 flags &= ~PG_NORELOC; 2118 } 2119 2120 /* 2121 * Make sure there's adequate physical memory available. 2122 * Note: PG_WAIT is ignored here. 2123 */ 2124 if (freemem <= throttlefree + npages) { 2125 VM_STAT_ADD(page_create_large_cnt[1]); 2126 return (NULL); 2127 } 2128 2129 /* 2130 * If cage is on, dampen draw from cage when available 2131 * cage space is low. 2132 */ 2133 if ((flags & (PG_NORELOC | PG_WAIT)) == (PG_NORELOC | PG_WAIT) && 2134 kcage_freemem < kcage_throttlefree + npages) { 2135 2136 /* 2137 * The cage is on, the caller wants PG_NORELOC 2138 * pages and available cage memory is very low. 2139 * Call kcage_create_throttle() to attempt to 2140 * control demand on the cage. 2141 */ 2142 if (kcage_create_throttle(npages, flags) == KCT_FAILURE) { 2143 VM_STAT_ADD(page_create_large_cnt[2]); 2144 return (NULL); 2145 } 2146 } 2147 2148 enough = 0; 2149 pcf_index = PCF_INDEX(); 2150 p = &pcf[pcf_index]; 2151 q = &pcf[PCF_FANOUT]; 2152 for (pcftotal = 0, i = 0; i < PCF_FANOUT; i++) { 2153 if (p->pcf_count > npages) { 2154 /* 2155 * a good one to try. 2156 */ 2157 mutex_enter(&p->pcf_lock); 2158 if (p->pcf_count > npages) { 2159 p->pcf_count -= (uint_t)npages; 2160 /* 2161 * freemem is not protected by any lock. 2162 * Thus, we cannot have any assertion 2163 * containing freemem here. 2164 */ 2165 freemem -= npages; 2166 enough = 1; 2167 mutex_exit(&p->pcf_lock); 2168 break; 2169 } 2170 mutex_exit(&p->pcf_lock); 2171 } 2172 pcftotal += p->pcf_count; 2173 p++; 2174 if (p >= q) { 2175 p = pcf; 2176 } 2177 } 2178 2179 if (!enough) { 2180 /* If there isn't enough memory available, give up. */ 2181 if (pcftotal < npages) { 2182 VM_STAT_ADD(page_create_large_cnt[3]); 2183 return (NULL); 2184 } 2185 2186 /* try to collect pages from several pcf bins */ 2187 for (p = pcf, pcftotal = 0, i = 0; i < PCF_FANOUT; i++) { 2188 mutex_enter(&p->pcf_lock); 2189 pcftotal += p->pcf_count; 2190 if (pcftotal >= npages) { 2191 /* 2192 * Wow! There are enough pages laying around 2193 * to satisfy the request. Do the accounting, 2194 * drop the locks we acquired, and go back. 2195 * 2196 * freemem is not protected by any lock. So, 2197 * we cannot have any assertion containing 2198 * freemem. 2199 */ 2200 pgcnt_t tpages = npages; 2201 freemem -= npages; 2202 while (p >= pcf) { 2203 if (p->pcf_count <= tpages) { 2204 tpages -= p->pcf_count; 2205 p->pcf_count = 0; 2206 } else { 2207 p->pcf_count -= (uint_t)tpages; 2208 tpages = 0; 2209 } 2210 mutex_exit(&p->pcf_lock); 2211 p--; 2212 } 2213 ASSERT(tpages == 0); 2214 break; 2215 } 2216 p++; 2217 } 2218 if (i == PCF_FANOUT) { 2219 /* failed to collect pages - release the locks */ 2220 while (--p >= pcf) { 2221 mutex_exit(&p->pcf_lock); 2222 } 2223 VM_STAT_ADD(page_create_large_cnt[4]); 2224 return (NULL); 2225 } 2226 } 2227 2228 /* 2229 * This is where this function behaves fundamentally differently 2230 * than page_create_va(); since we're intending to map the page 2231 * with a single TTE, we have to get it as a physically contiguous 2232 * hardware pagesize chunk. If we can't, we fail. 2233 */ 2234 if (lgrpid != NULL && *lgrpid >= 0 && *lgrpid <= lgrp_alloc_max && 2235 LGRP_EXISTS(lgrp_table[*lgrpid])) 2236 lgrp = lgrp_table[*lgrpid]; 2237 else 2238 lgrp = lgrp_mem_choose(seg, vaddr, bytes); 2239 2240 if ((rootpp = page_get_freelist(&kvp, off, seg, vaddr, 2241 bytes, flags & ~PG_MATCH_COLOR, lgrp)) == NULL) { 2242 page_create_putback(npages); 2243 VM_STAT_ADD(page_create_large_cnt[5]); 2244 return (NULL); 2245 } 2246 2247 /* 2248 * if we got the page with the wrong mtype give it back this is a 2249 * workaround for CR 6249718. When CR 6249718 is fixed we never get 2250 * inside "if" and the workaround becomes just a nop 2251 */ 2252 if (kcage_on && (flags & PG_NORELOC) && !PP_ISNORELOC(rootpp)) { 2253 page_list_add_pages(rootpp, 0); 2254 page_create_putback(npages); 2255 VM_STAT_ADD(page_create_large_cnt[6]); 2256 return (NULL); 2257 } 2258 2259 /* 2260 * If satisfying this request has left us with too little 2261 * memory, start the wheels turning to get some back. The 2262 * first clause of the test prevents waking up the pageout 2263 * daemon in situations where it would decide that there's 2264 * nothing to do. 2265 */ 2266 if (nscan < desscan && freemem < minfree) { 2267 TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL, 2268 "pageout_cv_signal:freemem %ld", freemem); 2269 cv_signal(&proc_pageout->p_cv); 2270 } 2271 2272 pp = rootpp; 2273 while (npages--) { 2274 ASSERT(PAGE_EXCL(pp)); 2275 ASSERT(pp->p_vnode == NULL); 2276 ASSERT(!hat_page_is_mapped(pp)); 2277 PP_CLRFREE(pp); 2278 PP_CLRAGED(pp); 2279 if (!page_hashin(pp, vp, off, NULL)) 2280 panic("page_create_large: hashin failed: page %p", 2281 (void *)pp); 2282 page_io_lock(pp); 2283 off += PAGESIZE; 2284 pp = pp->p_next; 2285 } 2286 2287 VM_STAT_ADD(page_create_large_cnt[0]); 2288 return (rootpp); 2289 } 2290 2291 page_t * 2292 page_create_va(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags, 2293 struct seg *seg, caddr_t vaddr) 2294 { 2295 page_t *plist = NULL; 2296 pgcnt_t npages; 2297 pgcnt_t found_on_free = 0; 2298 pgcnt_t pages_req; 2299 page_t *npp = NULL; 2300 uint_t enough; 2301 uint_t i; 2302 uint_t pcf_index; 2303 struct pcf *p; 2304 struct pcf *q; 2305 lgrp_t *lgrp; 2306 2307 TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_START, 2308 "page_create_start:vp %p off %llx bytes %lu flags %x", 2309 vp, off, bytes, flags); 2310 2311 ASSERT(bytes != 0 && vp != NULL); 2312 2313 if ((flags & PG_EXCL) == 0 && (flags & PG_WAIT) == 0) { 2314 panic("page_create: invalid flags"); 2315 /*NOTREACHED*/ 2316 } 2317 ASSERT((flags & ~(PG_EXCL | PG_WAIT | 2318 PG_NORELOC | PG_PANIC | PG_PUSHPAGE)) == 0); 2319 /* but no others */ 2320 2321 pages_req = npages = btopr(bytes); 2322 /* 2323 * Try to see whether request is too large to *ever* be 2324 * satisfied, in order to prevent deadlock. We arbitrarily 2325 * decide to limit maximum size requests to max_page_get. 2326 */ 2327 if (npages >= max_page_get) { 2328 if ((flags & PG_WAIT) == 0) { 2329 TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_TOOBIG, 2330 "page_create_toobig:vp %p off %llx npages " 2331 "%lu max_page_get %lu", 2332 vp, off, npages, max_page_get); 2333 return (NULL); 2334 } else { 2335 cmn_err(CE_WARN, 2336 "Request for too much kernel memory " 2337 "(%lu bytes), will hang forever", bytes); 2338 for (;;) 2339 delay(1000000000); 2340 } 2341 } 2342 2343 if (!kcage_on || panicstr) { 2344 /* 2345 * Cage is OFF, or we are single threaded in 2346 * panic, so make everything a RELOC request. 2347 */ 2348 flags &= ~PG_NORELOC; 2349 } 2350 2351 if (freemem <= throttlefree + npages) 2352 if (!page_create_throttle(npages, flags)) 2353 return (NULL); 2354 2355 /* 2356 * If cage is on, dampen draw from cage when available 2357 * cage space is low. 2358 */ 2359 if ((flags & PG_NORELOC) && 2360 kcage_freemem < kcage_throttlefree + npages) { 2361 2362 /* 2363 * The cage is on, the caller wants PG_NORELOC 2364 * pages and available cage memory is very low. 2365 * Call kcage_create_throttle() to attempt to 2366 * control demand on the cage. 2367 */ 2368 if (kcage_create_throttle(npages, flags) == KCT_FAILURE) 2369 return (NULL); 2370 } 2371 2372 VM_STAT_ADD(page_create_cnt[0]); 2373 2374 enough = 0; 2375 pcf_index = PCF_INDEX(); 2376 2377 p = &pcf[pcf_index]; 2378 q = &pcf[PCF_FANOUT]; 2379 for (i = 0; i < PCF_FANOUT; i++) { 2380 if (p->pcf_count > npages) { 2381 /* 2382 * a good one to try. 2383 */ 2384 mutex_enter(&p->pcf_lock); 2385 if (p->pcf_count > npages) { 2386 p->pcf_count -= (uint_t)npages; 2387 /* 2388 * freemem is not protected by any lock. 2389 * Thus, we cannot have any assertion 2390 * containing freemem here. 2391 */ 2392 freemem -= npages; 2393 enough = 1; 2394 mutex_exit(&p->pcf_lock); 2395 break; 2396 } 2397 mutex_exit(&p->pcf_lock); 2398 } 2399 p++; 2400 if (p >= q) { 2401 p = pcf; 2402 } 2403 } 2404 2405 if (!enough) { 2406 /* 2407 * Have to look harder. If npages is greater than 2408 * one, then we might have to coalecse the counters. 2409 * 2410 * Go wait. We come back having accounted 2411 * for the memory. 2412 */ 2413 VM_STAT_ADD(page_create_cnt[1]); 2414 if (!page_create_wait(npages, flags)) { 2415 VM_STAT_ADD(page_create_cnt[2]); 2416 return (NULL); 2417 } 2418 } 2419 2420 TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SUCCESS, 2421 "page_create_success:vp %p off %llx", vp, off); 2422 2423 /* 2424 * If satisfying this request has left us with too little 2425 * memory, start the wheels turning to get some back. The 2426 * first clause of the test prevents waking up the pageout 2427 * daemon in situations where it would decide that there's 2428 * nothing to do. 2429 */ 2430 if (nscan < desscan && freemem < minfree) { 2431 TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL, 2432 "pageout_cv_signal:freemem %ld", freemem); 2433 cv_signal(&proc_pageout->p_cv); 2434 } 2435 2436 /* 2437 * Loop around collecting the requested number of pages. 2438 * Most of the time, we have to `create' a new page. With 2439 * this in mind, pull the page off the free list before 2440 * getting the hash lock. This will minimize the hash 2441 * lock hold time, nesting, and the like. If it turns 2442 * out we don't need the page, we put it back at the end. 2443 */ 2444 while (npages--) { 2445 page_t *pp; 2446 kmutex_t *phm = NULL; 2447 ulong_t index; 2448 2449 index = PAGE_HASH_FUNC(vp, off); 2450 top: 2451 ASSERT(phm == NULL); 2452 ASSERT(index == PAGE_HASH_FUNC(vp, off)); 2453 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 2454 2455 if (npp == NULL) { 2456 /* 2457 * Try to get a page from the freelist (ie, 2458 * a page with no [vp, off] tag). If that 2459 * fails, use the cachelist. 2460 * 2461 * During the first attempt at both the free 2462 * and cache lists we try for the correct color. 2463 */ 2464 /* 2465 * XXXX-how do we deal with virtual indexed 2466 * caches and and colors? 2467 */ 2468 VM_STAT_ADD(page_create_cnt[4]); 2469 /* 2470 * Get lgroup to allocate next page of shared memory 2471 * from and use it to specify where to allocate 2472 * the physical memory 2473 */ 2474 lgrp = lgrp_mem_choose(seg, vaddr, PAGESIZE); 2475 npp = page_get_freelist(vp, off, seg, vaddr, PAGESIZE, 2476 flags | PG_MATCH_COLOR, lgrp); 2477 if (npp == NULL) { 2478 npp = page_get_cachelist(vp, off, seg, 2479 vaddr, flags | PG_MATCH_COLOR, lgrp); 2480 if (npp == NULL) { 2481 npp = page_create_get_something(vp, 2482 off, seg, vaddr, 2483 flags & ~PG_MATCH_COLOR); 2484 } 2485 2486 if (PP_ISAGED(npp) == 0) { 2487 /* 2488 * Since this page came from the 2489 * cachelist, we must destroy the 2490 * old vnode association. 2491 */ 2492 page_hashout(npp, NULL); 2493 } 2494 } 2495 } 2496 2497 /* 2498 * We own this page! 2499 */ 2500 ASSERT(PAGE_EXCL(npp)); 2501 ASSERT(npp->p_vnode == NULL); 2502 ASSERT(!hat_page_is_mapped(npp)); 2503 PP_CLRFREE(npp); 2504 PP_CLRAGED(npp); 2505 2506 /* 2507 * Here we have a page in our hot little mits and are 2508 * just waiting to stuff it on the appropriate lists. 2509 * Get the mutex and check to see if it really does 2510 * not exist. 2511 */ 2512 phm = PAGE_HASH_MUTEX(index); 2513 mutex_enter(phm); 2514 PAGE_HASH_SEARCH(index, pp, vp, off); 2515 if (pp == NULL) { 2516 VM_STAT_ADD(page_create_new); 2517 pp = npp; 2518 npp = NULL; 2519 if (!page_hashin(pp, vp, off, phm)) { 2520 /* 2521 * Since we hold the page hash mutex and 2522 * just searched for this page, page_hashin 2523 * had better not fail. If it does, that 2524 * means somethread did not follow the 2525 * page hash mutex rules. Panic now and 2526 * get it over with. As usual, go down 2527 * holding all the locks. 2528 */ 2529 ASSERT(MUTEX_HELD(phm)); 2530 panic("page_create: " 2531 "hashin failed %p %p %llx %p", 2532 (void *)pp, (void *)vp, off, (void *)phm); 2533 /*NOTREACHED*/ 2534 } 2535 ASSERT(MUTEX_HELD(phm)); 2536 mutex_exit(phm); 2537 phm = NULL; 2538 2539 /* 2540 * Hat layer locking need not be done to set 2541 * the following bits since the page is not hashed 2542 * and was on the free list (i.e., had no mappings). 2543 * 2544 * Set the reference bit to protect 2545 * against immediate pageout 2546 * 2547 * XXXmh modify freelist code to set reference 2548 * bit so we don't have to do it here. 2549 */ 2550 page_set_props(pp, P_REF); 2551 found_on_free++; 2552 } else { 2553 VM_STAT_ADD(page_create_exists); 2554 if (flags & PG_EXCL) { 2555 /* 2556 * Found an existing page, and the caller 2557 * wanted all new pages. Undo all of the work 2558 * we have done. 2559 */ 2560 mutex_exit(phm); 2561 phm = NULL; 2562 while (plist != NULL) { 2563 pp = plist; 2564 page_sub(&plist, pp); 2565 page_io_unlock(pp); 2566 /* large pages should not end up here */ 2567 ASSERT(pp->p_szc == 0); 2568 /*LINTED: constant in conditional ctx*/ 2569 VN_DISPOSE(pp, B_INVAL, 0, kcred); 2570 } 2571 VM_STAT_ADD(page_create_found_one); 2572 goto fail; 2573 } 2574 ASSERT(flags & PG_WAIT); 2575 if (!page_lock(pp, SE_EXCL, phm, P_NO_RECLAIM)) { 2576 /* 2577 * Start all over again if we blocked trying 2578 * to lock the page. 2579 */ 2580 mutex_exit(phm); 2581 VM_STAT_ADD(page_create_page_lock_failed); 2582 phm = NULL; 2583 goto top; 2584 } 2585 mutex_exit(phm); 2586 phm = NULL; 2587 2588 if (PP_ISFREE(pp)) { 2589 ASSERT(PP_ISAGED(pp) == 0); 2590 VM_STAT_ADD(pagecnt.pc_get_cache); 2591 page_list_sub(pp, PG_CACHE_LIST); 2592 PP_CLRFREE(pp); 2593 found_on_free++; 2594 } 2595 } 2596 2597 /* 2598 * Got a page! It is locked. Acquire the i/o 2599 * lock since we are going to use the p_next and 2600 * p_prev fields to link the requested pages together. 2601 */ 2602 page_io_lock(pp); 2603 page_add(&plist, pp); 2604 plist = plist->p_next; 2605 off += PAGESIZE; 2606 vaddr += PAGESIZE; 2607 } 2608 2609 ASSERT((flags & PG_EXCL) ? (found_on_free == pages_req) : 1); 2610 fail: 2611 if (npp != NULL) { 2612 /* 2613 * Did not need this page after all. 2614 * Put it back on the free list. 2615 */ 2616 VM_STAT_ADD(page_create_putbacks); 2617 PP_SETFREE(npp); 2618 PP_SETAGED(npp); 2619 npp->p_offset = (u_offset_t)-1; 2620 page_list_add(npp, PG_FREE_LIST | PG_LIST_TAIL); 2621 page_unlock(npp); 2622 2623 } 2624 2625 ASSERT(pages_req >= found_on_free); 2626 2627 { 2628 uint_t overshoot = (uint_t)(pages_req - found_on_free); 2629 2630 if (overshoot) { 2631 VM_STAT_ADD(page_create_overshoot); 2632 p = &pcf[pcf_index]; 2633 mutex_enter(&p->pcf_lock); 2634 if (p->pcf_block) { 2635 p->pcf_reserve += overshoot; 2636 } else { 2637 p->pcf_count += overshoot; 2638 if (p->pcf_wait) { 2639 mutex_enter(&new_freemem_lock); 2640 if (freemem_wait) { 2641 cv_signal(&freemem_cv); 2642 p->pcf_wait--; 2643 } else { 2644 p->pcf_wait = 0; 2645 } 2646 mutex_exit(&new_freemem_lock); 2647 } 2648 } 2649 mutex_exit(&p->pcf_lock); 2650 /* freemem is approximate, so this test OK */ 2651 if (!p->pcf_block) 2652 freemem += overshoot; 2653 } 2654 } 2655 2656 return (plist); 2657 } 2658 2659 /* 2660 * One or more constituent pages of this large page has been marked 2661 * toxic. Simply demote the large page to PAGESIZE pages and let 2662 * page_free() handle it. This routine should only be called by 2663 * large page free routines (page_free_pages() and page_destroy_pages(). 2664 * All pages are locked SE_EXCL and have already been marked free. 2665 */ 2666 static void 2667 page_free_toxic_pages(page_t *rootpp) 2668 { 2669 page_t *tpp; 2670 pgcnt_t i, pgcnt = page_get_pagecnt(rootpp->p_szc); 2671 uint_t szc = rootpp->p_szc; 2672 2673 for (i = 0, tpp = rootpp; i < pgcnt; i++, tpp = tpp->p_next) { 2674 ASSERT(tpp->p_szc == szc); 2675 ASSERT((PAGE_EXCL(tpp) && 2676 !page_iolock_assert(tpp)) || panicstr); 2677 tpp->p_szc = 0; 2678 } 2679 2680 while (rootpp != NULL) { 2681 tpp = rootpp; 2682 page_sub(&rootpp, tpp); 2683 ASSERT(PP_ISFREE(tpp)); 2684 PP_CLRFREE(tpp); 2685 page_free(tpp, 1); 2686 } 2687 } 2688 2689 /* 2690 * Put page on the "free" list. 2691 * The free list is really two lists maintained by 2692 * the PSM of whatever machine we happen to be on. 2693 */ 2694 void 2695 page_free(page_t *pp, int dontneed) 2696 { 2697 struct pcf *p; 2698 uint_t pcf_index; 2699 2700 ASSERT((PAGE_EXCL(pp) && 2701 !page_iolock_assert(pp)) || panicstr); 2702 2703 if (PP_ISFREE(pp)) { 2704 panic("page_free: page %p is free", (void *)pp); 2705 } 2706 2707 if (pp->p_szc != 0) { 2708 if (pp->p_vnode == NULL || IS_SWAPFSVP(pp->p_vnode) || 2709 PP_ISKAS(pp)) { 2710 panic("page_free: anon or kernel " 2711 "or no vnode large page %p", (void *)pp); 2712 } 2713 page_demote_vp_pages(pp); 2714 ASSERT(pp->p_szc == 0); 2715 } 2716 2717 /* 2718 * The page_struct_lock need not be acquired to examine these 2719 * fields since the page has an "exclusive" lock. 2720 */ 2721 if (hat_page_is_mapped(pp) || pp->p_lckcnt != 0 || pp->p_cowcnt != 0 || 2722 pp->p_slckcnt != 0) { 2723 panic("page_free pp=%p, pfn=%lx, lckcnt=%d, cowcnt=%d " 2724 "slckcnt = %d", pp, page_pptonum(pp), pp->p_lckcnt, 2725 pp->p_cowcnt, pp->p_slckcnt); 2726 /*NOTREACHED*/ 2727 } 2728 2729 ASSERT(!hat_page_getshare(pp)); 2730 2731 PP_SETFREE(pp); 2732 ASSERT(pp->p_vnode == NULL || !IS_VMODSORT(pp->p_vnode) || 2733 !hat_ismod(pp)); 2734 page_clr_all_props(pp); 2735 ASSERT(!hat_page_getshare(pp)); 2736 2737 /* 2738 * Now we add the page to the head of the free list. 2739 * But if this page is associated with a paged vnode 2740 * then we adjust the head forward so that the page is 2741 * effectively at the end of the list. 2742 */ 2743 if (pp->p_vnode == NULL) { 2744 /* 2745 * Page has no identity, put it on the free list. 2746 */ 2747 PP_SETAGED(pp); 2748 pp->p_offset = (u_offset_t)-1; 2749 page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 2750 VM_STAT_ADD(pagecnt.pc_free_free); 2751 TRACE_1(TR_FAC_VM, TR_PAGE_FREE_FREE, 2752 "page_free_free:pp %p", pp); 2753 } else { 2754 PP_CLRAGED(pp); 2755 2756 if (!dontneed || nopageage) { 2757 /* move it to the tail of the list */ 2758 page_list_add(pp, PG_CACHE_LIST | PG_LIST_TAIL); 2759 2760 VM_STAT_ADD(pagecnt.pc_free_cache); 2761 TRACE_1(TR_FAC_VM, TR_PAGE_FREE_CACHE_TAIL, 2762 "page_free_cache_tail:pp %p", pp); 2763 } else { 2764 page_list_add(pp, PG_CACHE_LIST | PG_LIST_HEAD); 2765 2766 VM_STAT_ADD(pagecnt.pc_free_dontneed); 2767 TRACE_1(TR_FAC_VM, TR_PAGE_FREE_CACHE_HEAD, 2768 "page_free_cache_head:pp %p", pp); 2769 } 2770 } 2771 page_unlock(pp); 2772 2773 /* 2774 * Now do the `freemem' accounting. 2775 */ 2776 pcf_index = PCF_INDEX(); 2777 p = &pcf[pcf_index]; 2778 2779 mutex_enter(&p->pcf_lock); 2780 if (p->pcf_block) { 2781 p->pcf_reserve += 1; 2782 } else { 2783 p->pcf_count += 1; 2784 if (p->pcf_wait) { 2785 mutex_enter(&new_freemem_lock); 2786 /* 2787 * Check to see if some other thread 2788 * is actually waiting. Another bucket 2789 * may have woken it up by now. If there 2790 * are no waiters, then set our pcf_wait 2791 * count to zero to avoid coming in here 2792 * next time. Also, since only one page 2793 * was put on the free list, just wake 2794 * up one waiter. 2795 */ 2796 if (freemem_wait) { 2797 cv_signal(&freemem_cv); 2798 p->pcf_wait--; 2799 } else { 2800 p->pcf_wait = 0; 2801 } 2802 mutex_exit(&new_freemem_lock); 2803 } 2804 } 2805 mutex_exit(&p->pcf_lock); 2806 2807 /* freemem is approximate, so this test OK */ 2808 if (!p->pcf_block) 2809 freemem += 1; 2810 } 2811 2812 /* 2813 * Put page on the "free" list during intial startup. 2814 * This happens during initial single threaded execution. 2815 */ 2816 void 2817 page_free_at_startup(page_t *pp) 2818 { 2819 struct pcf *p; 2820 uint_t pcf_index; 2821 2822 page_list_add(pp, PG_FREE_LIST | PG_LIST_HEAD | PG_LIST_ISINIT); 2823 VM_STAT_ADD(pagecnt.pc_free_free); 2824 2825 /* 2826 * Now do the `freemem' accounting. 2827 */ 2828 pcf_index = PCF_INDEX(); 2829 p = &pcf[pcf_index]; 2830 2831 ASSERT(p->pcf_block == 0); 2832 ASSERT(p->pcf_wait == 0); 2833 p->pcf_count += 1; 2834 2835 /* freemem is approximate, so this is OK */ 2836 freemem += 1; 2837 } 2838 2839 void 2840 page_free_pages(page_t *pp) 2841 { 2842 page_t *tpp, *rootpp = NULL; 2843 pgcnt_t pgcnt = page_get_pagecnt(pp->p_szc); 2844 pgcnt_t i; 2845 uint_t szc = pp->p_szc; 2846 2847 VM_STAT_ADD(pagecnt.pc_free_pages); 2848 TRACE_1(TR_FAC_VM, TR_PAGE_FREE_FREE, 2849 "page_free_free:pp %p", pp); 2850 2851 ASSERT(pp->p_szc != 0 && pp->p_szc < page_num_pagesizes()); 2852 if ((page_pptonum(pp) & (pgcnt - 1)) != 0) { 2853 panic("page_free_pages: not root page %p", (void *)pp); 2854 /*NOTREACHED*/ 2855 } 2856 2857 for (i = 0, tpp = pp; i < pgcnt; i++, tpp++) { 2858 ASSERT((PAGE_EXCL(tpp) && 2859 !page_iolock_assert(tpp)) || panicstr); 2860 if (PP_ISFREE(tpp)) { 2861 panic("page_free_pages: page %p is free", (void *)tpp); 2862 /*NOTREACHED*/ 2863 } 2864 if (hat_page_is_mapped(tpp) || tpp->p_lckcnt != 0 || 2865 tpp->p_cowcnt != 0 || tpp->p_slckcnt != 0) { 2866 panic("page_free_pages %p", (void *)tpp); 2867 /*NOTREACHED*/ 2868 } 2869 2870 ASSERT(!hat_page_getshare(tpp)); 2871 ASSERT(tpp->p_vnode == NULL); 2872 ASSERT(tpp->p_szc == szc); 2873 2874 PP_SETFREE(tpp); 2875 page_clr_all_props(tpp); 2876 PP_SETAGED(tpp); 2877 tpp->p_offset = (u_offset_t)-1; 2878 ASSERT(tpp->p_next == tpp); 2879 ASSERT(tpp->p_prev == tpp); 2880 page_list_concat(&rootpp, &tpp); 2881 } 2882 ASSERT(rootpp == pp); 2883 2884 page_list_add_pages(rootpp, 0); 2885 page_create_putback(pgcnt); 2886 } 2887 2888 int free_pages = 1; 2889 2890 /* 2891 * This routine attempts to return pages to the cachelist via page_release(). 2892 * It does not *have* to be successful in all cases, since the pageout scanner 2893 * will catch any pages it misses. It does need to be fast and not introduce 2894 * too much overhead. 2895 * 2896 * If a page isn't found on the unlocked sweep of the page_hash bucket, we 2897 * don't lock and retry. This is ok, since the page scanner will eventually 2898 * find any page we miss in free_vp_pages(). 2899 */ 2900 void 2901 free_vp_pages(vnode_t *vp, u_offset_t off, size_t len) 2902 { 2903 page_t *pp; 2904 u_offset_t eoff; 2905 extern int swap_in_range(vnode_t *, u_offset_t, size_t); 2906 2907 eoff = off + len; 2908 2909 if (free_pages == 0) 2910 return; 2911 if (swap_in_range(vp, off, len)) 2912 return; 2913 2914 for (; off < eoff; off += PAGESIZE) { 2915 2916 /* 2917 * find the page using a fast, but inexact search. It'll be OK 2918 * if a few pages slip through the cracks here. 2919 */ 2920 pp = page_exists(vp, off); 2921 2922 /* 2923 * If we didn't find the page (it may not exist), the page 2924 * is free, looks still in use (shared), or we can't lock it, 2925 * just give up. 2926 */ 2927 if (pp == NULL || 2928 PP_ISFREE(pp) || 2929 page_share_cnt(pp) > 0 || 2930 !page_trylock(pp, SE_EXCL)) 2931 continue; 2932 2933 /* 2934 * Once we have locked pp, verify that it's still the 2935 * correct page and not already free 2936 */ 2937 ASSERT(PAGE_LOCKED_SE(pp, SE_EXCL)); 2938 if (pp->p_vnode != vp || pp->p_offset != off || PP_ISFREE(pp)) { 2939 page_unlock(pp); 2940 continue; 2941 } 2942 2943 /* 2944 * try to release the page... 2945 */ 2946 (void) page_release(pp, 1); 2947 } 2948 } 2949 2950 /* 2951 * Reclaim the given page from the free list. 2952 * Returns 1 on success or 0 on failure. 2953 * 2954 * The page is unlocked if it can't be reclaimed (when freemem == 0). 2955 * If `lock' is non-null, it will be dropped and re-acquired if 2956 * the routine must wait while freemem is 0. 2957 * 2958 * As it turns out, boot_getpages() does this. It picks a page, 2959 * based on where OBP mapped in some address, gets its pfn, searches 2960 * the memsegs, locks the page, then pulls it off the free list! 2961 */ 2962 int 2963 page_reclaim(page_t *pp, kmutex_t *lock) 2964 { 2965 struct pcf *p; 2966 uint_t pcf_index; 2967 struct cpu *cpup; 2968 uint_t i; 2969 pgcnt_t npgs, need; 2970 pgcnt_t collected = 0; 2971 2972 ASSERT(lock != NULL ? MUTEX_HELD(lock) : 1); 2973 ASSERT(PAGE_EXCL(pp) && PP_ISFREE(pp)); 2974 2975 npgs = page_get_pagecnt(pp->p_szc); 2976 2977 /* 2978 * If `freemem' is 0, we cannot reclaim this page from the 2979 * freelist, so release every lock we might hold: the page, 2980 * and the `lock' before blocking. 2981 * 2982 * The only way `freemem' can become 0 while there are pages 2983 * marked free (have their p->p_free bit set) is when the 2984 * system is low on memory and doing a page_create(). In 2985 * order to guarantee that once page_create() starts acquiring 2986 * pages it will be able to get all that it needs since `freemem' 2987 * was decreased by the requested amount. So, we need to release 2988 * this page, and let page_create() have it. 2989 * 2990 * Since `freemem' being zero is not supposed to happen, just 2991 * use the usual hash stuff as a starting point. If that bucket 2992 * is empty, then assume the worst, and start at the beginning 2993 * of the pcf array. If we always start at the beginning 2994 * when acquiring more than one pcf lock, there won't be any 2995 * deadlock problems. 2996 */ 2997 2998 /* TODO: Do we need to test kcage_freemem if PG_NORELOC(pp)? */ 2999 3000 if (freemem <= throttlefree && !page_create_throttle(npgs, 0)) { 3001 pcf_acquire_all(); 3002 goto page_reclaim_nomem; 3003 } 3004 3005 pcf_index = PCF_INDEX(); 3006 p = &pcf[pcf_index]; 3007 mutex_enter(&p->pcf_lock); 3008 if (p->pcf_count >= npgs) { 3009 collected = npgs; 3010 p->pcf_count -= npgs; 3011 } 3012 mutex_exit(&p->pcf_lock); 3013 need = npgs - collected; 3014 3015 if (need > 0) { 3016 VM_STAT_ADD(page_reclaim_zero); 3017 /* 3018 * Check again. Its possible that some other thread 3019 * could have been right behind us, and added one 3020 * to a list somewhere. Acquire each of the pcf locks 3021 * until we find a page. 3022 */ 3023 p = pcf; 3024 for (i = 0; i < PCF_FANOUT; i++) { 3025 mutex_enter(&p->pcf_lock); 3026 if (p->pcf_count) { 3027 if (p->pcf_count >= need) { 3028 p->pcf_count -= need; 3029 collected += need; 3030 need = 0; 3031 break; 3032 } else if (p->pcf_count) { 3033 collected += p->pcf_count; 3034 need -= p->pcf_count; 3035 p->pcf_count = 0; 3036 } 3037 } 3038 p++; 3039 } 3040 3041 if (need > 0) { 3042 page_reclaim_nomem: 3043 /* 3044 * We really can't have page `pp'. 3045 * Time for the no-memory dance with 3046 * page_free(). This is just like 3047 * page_create_wait(). Plus the added 3048 * attraction of releasing whatever mutex 3049 * we held when we were called with in `lock'. 3050 * Page_unlock() will wakeup any thread 3051 * waiting around for this page. 3052 */ 3053 if (lock) { 3054 VM_STAT_ADD(page_reclaim_zero_locked); 3055 mutex_exit(lock); 3056 } 3057 page_unlock(pp); 3058 3059 /* 3060 * get this before we drop all the pcf locks. 3061 */ 3062 mutex_enter(&new_freemem_lock); 3063 3064 p = pcf; 3065 p->pcf_count += collected; 3066 for (i = 0; i < PCF_FANOUT; i++) { 3067 p->pcf_wait++; 3068 mutex_exit(&p->pcf_lock); 3069 p++; 3070 } 3071 3072 freemem_wait++; 3073 cv_wait(&freemem_cv, &new_freemem_lock); 3074 freemem_wait--; 3075 3076 mutex_exit(&new_freemem_lock); 3077 3078 if (lock) { 3079 mutex_enter(lock); 3080 } 3081 return (0); 3082 } 3083 3084 /* 3085 * We beat the PCF bins over the head until 3086 * we got the memory that we wanted. 3087 * The pcf accounting has been done, 3088 * though none of the pcf_wait flags have been set, 3089 * drop the locks and continue on. 3090 */ 3091 ASSERT(collected == npgs); 3092 while (p >= pcf) { 3093 mutex_exit(&p->pcf_lock); 3094 p--; 3095 } 3096 } 3097 3098 /* 3099 * freemem is not protected by any lock. Thus, we cannot 3100 * have any assertion containing freemem here. 3101 */ 3102 freemem -= npgs; 3103 3104 VM_STAT_ADD(pagecnt.pc_reclaim); 3105 if (PP_ISAGED(pp)) { 3106 if (npgs > 1) { 3107 page_list_sub_pages(pp, pp->p_szc); 3108 } else { 3109 page_list_sub(pp, PG_FREE_LIST); 3110 } 3111 TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_FREE, 3112 "page_reclaim_free:pp %p", pp); 3113 } else { 3114 ASSERT(npgs == 1); 3115 page_list_sub(pp, PG_CACHE_LIST); 3116 TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_CACHE, 3117 "page_reclaim_cache:pp %p", pp); 3118 } 3119 3120 /* 3121 * clear the p_free & p_age bits since this page is no longer 3122 * on the free list. Notice that there was a brief time where 3123 * a page is marked as free, but is not on the list. 3124 * 3125 * Set the reference bit to protect against immediate pageout. 3126 */ 3127 for (i = 0; i < npgs; i++, pp++) { 3128 PP_CLRFREE(pp); 3129 PP_CLRAGED(pp); 3130 page_set_props(pp, P_REF); 3131 } 3132 3133 CPU_STATS_ENTER_K(); 3134 cpup = CPU; /* get cpup now that CPU cannot change */ 3135 CPU_STATS_ADDQ(cpup, vm, pgrec, 1); 3136 CPU_STATS_ADDQ(cpup, vm, pgfrec, 1); 3137 CPU_STATS_EXIT_K(); 3138 3139 return (1); 3140 } 3141 3142 3143 3144 /* 3145 * Destroy identity of the page and put it back on 3146 * the page free list. Assumes that the caller has 3147 * acquired the "exclusive" lock on the page. 3148 */ 3149 void 3150 page_destroy(page_t *pp, int dontfree) 3151 { 3152 ASSERT((PAGE_EXCL(pp) && 3153 !page_iolock_assert(pp)) || panicstr); 3154 ASSERT(pp->p_slckcnt == 0 || panicstr); 3155 3156 if (pp->p_szc != 0) { 3157 if (pp->p_vnode == NULL || IS_SWAPFSVP(pp->p_vnode) || 3158 PP_ISKAS(pp)) { 3159 panic("page_destroy: anon or kernel or no vnode " 3160 "large page %p", (void *)pp); 3161 } 3162 page_demote_vp_pages(pp); 3163 ASSERT(pp->p_szc == 0); 3164 } 3165 3166 TRACE_1(TR_FAC_VM, TR_PAGE_DESTROY, "page_destroy:pp %p", pp); 3167 3168 /* 3169 * Unload translations, if any, then hash out the 3170 * page to erase its identity. 3171 */ 3172 (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 3173 page_hashout(pp, NULL); 3174 3175 if (!dontfree) { 3176 /* 3177 * Acquire the "freemem_lock" for availrmem. 3178 * The page_struct_lock need not be acquired for lckcnt 3179 * and cowcnt since the page has an "exclusive" lock. 3180 */ 3181 if ((pp->p_lckcnt != 0) || (pp->p_cowcnt != 0)) { 3182 mutex_enter(&freemem_lock); 3183 if (pp->p_lckcnt != 0) { 3184 availrmem++; 3185 pp->p_lckcnt = 0; 3186 } 3187 if (pp->p_cowcnt != 0) { 3188 availrmem += pp->p_cowcnt; 3189 pp->p_cowcnt = 0; 3190 } 3191 mutex_exit(&freemem_lock); 3192 } 3193 /* 3194 * Put the page on the "free" list. 3195 */ 3196 page_free(pp, 0); 3197 } 3198 } 3199 3200 void 3201 page_destroy_pages(page_t *pp) 3202 { 3203 3204 page_t *tpp, *rootpp = NULL; 3205 pgcnt_t pgcnt = page_get_pagecnt(pp->p_szc); 3206 pgcnt_t i, pglcks = 0; 3207 uint_t szc = pp->p_szc; 3208 3209 ASSERT(pp->p_szc != 0 && pp->p_szc < page_num_pagesizes()); 3210 3211 VM_STAT_ADD(pagecnt.pc_destroy_pages); 3212 3213 TRACE_1(TR_FAC_VM, TR_PAGE_DESTROY, "page_destroy_pages:pp %p", pp); 3214 3215 if ((page_pptonum(pp) & (pgcnt - 1)) != 0) { 3216 panic("page_destroy_pages: not root page %p", (void *)pp); 3217 /*NOTREACHED*/ 3218 } 3219 3220 for (i = 0, tpp = pp; i < pgcnt; i++, tpp++) { 3221 ASSERT((PAGE_EXCL(tpp) && 3222 !page_iolock_assert(tpp)) || panicstr); 3223 ASSERT(tpp->p_slckcnt == 0 || panicstr); 3224 (void) hat_pageunload(tpp, HAT_FORCE_PGUNLOAD); 3225 page_hashout(tpp, NULL); 3226 ASSERT(tpp->p_offset == (u_offset_t)-1); 3227 if (tpp->p_lckcnt != 0) { 3228 pglcks++; 3229 tpp->p_lckcnt = 0; 3230 } else if (tpp->p_cowcnt != 0) { 3231 pglcks += tpp->p_cowcnt; 3232 tpp->p_cowcnt = 0; 3233 } 3234 ASSERT(!hat_page_getshare(tpp)); 3235 ASSERT(tpp->p_vnode == NULL); 3236 ASSERT(tpp->p_szc == szc); 3237 3238 PP_SETFREE(tpp); 3239 page_clr_all_props(tpp); 3240 PP_SETAGED(tpp); 3241 ASSERT(tpp->p_next == tpp); 3242 ASSERT(tpp->p_prev == tpp); 3243 page_list_concat(&rootpp, &tpp); 3244 } 3245 3246 ASSERT(rootpp == pp); 3247 if (pglcks != 0) { 3248 mutex_enter(&freemem_lock); 3249 availrmem += pglcks; 3250 mutex_exit(&freemem_lock); 3251 } 3252 3253 page_list_add_pages(rootpp, 0); 3254 page_create_putback(pgcnt); 3255 } 3256 3257 /* 3258 * Similar to page_destroy(), but destroys pages which are 3259 * locked and known to be on the page free list. Since 3260 * the page is known to be free and locked, no one can access 3261 * it. 3262 * 3263 * Also, the number of free pages does not change. 3264 */ 3265 void 3266 page_destroy_free(page_t *pp) 3267 { 3268 ASSERT(PAGE_EXCL(pp)); 3269 ASSERT(PP_ISFREE(pp)); 3270 ASSERT(pp->p_vnode); 3271 ASSERT(hat_page_getattr(pp, P_MOD | P_REF | P_RO) == 0); 3272 ASSERT(!hat_page_is_mapped(pp)); 3273 ASSERT(PP_ISAGED(pp) == 0); 3274 ASSERT(pp->p_szc == 0); 3275 3276 VM_STAT_ADD(pagecnt.pc_destroy_free); 3277 page_list_sub(pp, PG_CACHE_LIST); 3278 3279 page_hashout(pp, NULL); 3280 ASSERT(pp->p_vnode == NULL); 3281 ASSERT(pp->p_offset == (u_offset_t)-1); 3282 ASSERT(pp->p_hash == NULL); 3283 3284 PP_SETAGED(pp); 3285 page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 3286 page_unlock(pp); 3287 3288 mutex_enter(&new_freemem_lock); 3289 if (freemem_wait) { 3290 cv_signal(&freemem_cv); 3291 } 3292 mutex_exit(&new_freemem_lock); 3293 } 3294 3295 /* 3296 * Rename the page "opp" to have an identity specified 3297 * by [vp, off]. If a page already exists with this name 3298 * it is locked and destroyed. Note that the page's 3299 * translations are not unloaded during the rename. 3300 * 3301 * This routine is used by the anon layer to "steal" the 3302 * original page and is not unlike destroying a page and 3303 * creating a new page using the same page frame. 3304 * 3305 * XXX -- Could deadlock if caller 1 tries to rename A to B while 3306 * caller 2 tries to rename B to A. 3307 */ 3308 void 3309 page_rename(page_t *opp, vnode_t *vp, u_offset_t off) 3310 { 3311 page_t *pp; 3312 int olckcnt = 0; 3313 int ocowcnt = 0; 3314 kmutex_t *phm; 3315 ulong_t index; 3316 3317 ASSERT(PAGE_EXCL(opp) && !page_iolock_assert(opp)); 3318 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 3319 ASSERT(PP_ISFREE(opp) == 0); 3320 3321 VM_STAT_ADD(page_rename_count); 3322 3323 TRACE_3(TR_FAC_VM, TR_PAGE_RENAME, 3324 "page rename:pp %p vp %p off %llx", opp, vp, off); 3325 3326 /* 3327 * CacheFS may call page_rename for a large NFS page 3328 * when both CacheFS and NFS mount points are used 3329 * by applications. Demote this large page before 3330 * renaming it, to ensure that there are no "partial" 3331 * large pages left lying around. 3332 */ 3333 if (opp->p_szc != 0) { 3334 vnode_t *ovp = opp->p_vnode; 3335 ASSERT(ovp != NULL); 3336 ASSERT(!IS_SWAPFSVP(ovp)); 3337 ASSERT(!VN_ISKAS(ovp)); 3338 page_demote_vp_pages(opp); 3339 ASSERT(opp->p_szc == 0); 3340 } 3341 3342 page_hashout(opp, NULL); 3343 PP_CLRAGED(opp); 3344 3345 /* 3346 * Acquire the appropriate page hash lock, since 3347 * we're going to rename the page. 3348 */ 3349 index = PAGE_HASH_FUNC(vp, off); 3350 phm = PAGE_HASH_MUTEX(index); 3351 mutex_enter(phm); 3352 top: 3353 /* 3354 * Look for an existing page with this name and destroy it if found. 3355 * By holding the page hash lock all the way to the page_hashin() 3356 * call, we are assured that no page can be created with this 3357 * identity. In the case when the phm lock is dropped to undo any 3358 * hat layer mappings, the existing page is held with an "exclusive" 3359 * lock, again preventing another page from being created with 3360 * this identity. 3361 */ 3362 PAGE_HASH_SEARCH(index, pp, vp, off); 3363 if (pp != NULL) { 3364 VM_STAT_ADD(page_rename_exists); 3365 3366 /* 3367 * As it turns out, this is one of only two places where 3368 * page_lock() needs to hold the passed in lock in the 3369 * successful case. In all of the others, the lock could 3370 * be dropped as soon as the attempt is made to lock 3371 * the page. It is tempting to add yet another arguement, 3372 * PL_KEEP or PL_DROP, to let page_lock know what to do. 3373 */ 3374 if (!page_lock(pp, SE_EXCL, phm, P_RECLAIM)) { 3375 /* 3376 * Went to sleep because the page could not 3377 * be locked. We were woken up when the page 3378 * was unlocked, or when the page was destroyed. 3379 * In either case, `phm' was dropped while we 3380 * slept. Hence we should not just roar through 3381 * this loop. 3382 */ 3383 goto top; 3384 } 3385 3386 /* 3387 * If an existing page is a large page, then demote 3388 * it to ensure that no "partial" large pages are 3389 * "created" after page_rename. An existing page 3390 * can be a CacheFS page, and can't belong to swapfs. 3391 */ 3392 if (hat_page_is_mapped(pp)) { 3393 /* 3394 * Unload translations. Since we hold the 3395 * exclusive lock on this page, the page 3396 * can not be changed while we drop phm. 3397 * This is also not a lock protocol violation, 3398 * but rather the proper way to do things. 3399 */ 3400 mutex_exit(phm); 3401 (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 3402 if (pp->p_szc != 0) { 3403 ASSERT(!IS_SWAPFSVP(vp)); 3404 ASSERT(!VN_ISKAS(vp)); 3405 page_demote_vp_pages(pp); 3406 ASSERT(pp->p_szc == 0); 3407 } 3408 mutex_enter(phm); 3409 } else if (pp->p_szc != 0) { 3410 ASSERT(!IS_SWAPFSVP(vp)); 3411 ASSERT(!VN_ISKAS(vp)); 3412 mutex_exit(phm); 3413 page_demote_vp_pages(pp); 3414 ASSERT(pp->p_szc == 0); 3415 mutex_enter(phm); 3416 } 3417 page_hashout(pp, phm); 3418 } 3419 /* 3420 * Hash in the page with the new identity. 3421 */ 3422 if (!page_hashin(opp, vp, off, phm)) { 3423 /* 3424 * We were holding phm while we searched for [vp, off] 3425 * and only dropped phm if we found and locked a page. 3426 * If we can't create this page now, then some thing 3427 * is really broken. 3428 */ 3429 panic("page_rename: Can't hash in page: %p", (void *)pp); 3430 /*NOTREACHED*/ 3431 } 3432 3433 ASSERT(MUTEX_HELD(phm)); 3434 mutex_exit(phm); 3435 3436 /* 3437 * Now that we have dropped phm, lets get around to finishing up 3438 * with pp. 3439 */ 3440 if (pp != NULL) { 3441 ASSERT(!hat_page_is_mapped(pp)); 3442 /* for now large pages should not end up here */ 3443 ASSERT(pp->p_szc == 0); 3444 /* 3445 * Save the locks for transfer to the new page and then 3446 * clear them so page_free doesn't think they're important. 3447 * The page_struct_lock need not be acquired for lckcnt and 3448 * cowcnt since the page has an "exclusive" lock. 3449 */ 3450 olckcnt = pp->p_lckcnt; 3451 ocowcnt = pp->p_cowcnt; 3452 pp->p_lckcnt = pp->p_cowcnt = 0; 3453 3454 /* 3455 * Put the page on the "free" list after we drop 3456 * the lock. The less work under the lock the better. 3457 */ 3458 /*LINTED: constant in conditional context*/ 3459 VN_DISPOSE(pp, B_FREE, 0, kcred); 3460 } 3461 3462 /* 3463 * Transfer the lock count from the old page (if any). 3464 * The page_struct_lock need not be acquired for lckcnt and 3465 * cowcnt since the page has an "exclusive" lock. 3466 */ 3467 opp->p_lckcnt += olckcnt; 3468 opp->p_cowcnt += ocowcnt; 3469 } 3470 3471 /* 3472 * low level routine to add page `pp' to the hash and vp chains for [vp, offset] 3473 * 3474 * Pages are normally inserted at the start of a vnode's v_pages list. 3475 * If the vnode is VMODSORT and the page is modified, it goes at the end. 3476 * This can happen when a modified page is relocated for DR. 3477 * 3478 * Returns 1 on success and 0 on failure. 3479 */ 3480 static int 3481 page_do_hashin(page_t *pp, vnode_t *vp, u_offset_t offset) 3482 { 3483 page_t **listp; 3484 page_t *tp; 3485 ulong_t index; 3486 3487 ASSERT(PAGE_EXCL(pp)); 3488 ASSERT(vp != NULL); 3489 ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 3490 3491 /* 3492 * Be sure to set these up before the page is inserted on the hash 3493 * list. As soon as the page is placed on the list some other 3494 * thread might get confused and wonder how this page could 3495 * possibly hash to this list. 3496 */ 3497 pp->p_vnode = vp; 3498 pp->p_offset = offset; 3499 3500 /* 3501 * record if this page is on a swap vnode 3502 */ 3503 if ((vp->v_flag & VISSWAP) != 0) 3504 PP_SETSWAP(pp); 3505 3506 index = PAGE_HASH_FUNC(vp, offset); 3507 ASSERT(MUTEX_HELD(PAGE_HASH_MUTEX(index))); 3508 listp = &page_hash[index]; 3509 3510 /* 3511 * If this page is already hashed in, fail this attempt to add it. 3512 */ 3513 for (tp = *listp; tp != NULL; tp = tp->p_hash) { 3514 if (tp->p_vnode == vp && tp->p_offset == offset) { 3515 pp->p_vnode = NULL; 3516 pp->p_offset = (u_offset_t)(-1); 3517 return (0); 3518 } 3519 } 3520 pp->p_hash = *listp; 3521 *listp = pp; 3522 3523 /* 3524 * Add the page to the vnode's list of pages 3525 */ 3526 if (vp->v_pages != NULL && IS_VMODSORT(vp) && hat_ismod(pp)) 3527 listp = &vp->v_pages->p_vpprev->p_vpnext; 3528 else 3529 listp = &vp->v_pages; 3530 3531 page_vpadd(listp, pp); 3532 3533 return (1); 3534 } 3535 3536 /* 3537 * Add page `pp' to both the hash and vp chains for [vp, offset]. 3538 * 3539 * Returns 1 on success and 0 on failure. 3540 * If hold is passed in, it is not dropped. 3541 */ 3542 int 3543 page_hashin(page_t *pp, vnode_t *vp, u_offset_t offset, kmutex_t *hold) 3544 { 3545 kmutex_t *phm = NULL; 3546 kmutex_t *vphm; 3547 int rc; 3548 3549 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 3550 3551 TRACE_3(TR_FAC_VM, TR_PAGE_HASHIN, 3552 "page_hashin:pp %p vp %p offset %llx", 3553 pp, vp, offset); 3554 3555 VM_STAT_ADD(hashin_count); 3556 3557 if (hold != NULL) 3558 phm = hold; 3559 else { 3560 VM_STAT_ADD(hashin_not_held); 3561 phm = PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, offset)); 3562 mutex_enter(phm); 3563 } 3564 3565 vphm = page_vnode_mutex(vp); 3566 mutex_enter(vphm); 3567 rc = page_do_hashin(pp, vp, offset); 3568 mutex_exit(vphm); 3569 if (hold == NULL) 3570 mutex_exit(phm); 3571 if (rc == 0) 3572 VM_STAT_ADD(hashin_already); 3573 return (rc); 3574 } 3575 3576 /* 3577 * Remove page ``pp'' from the hash and vp chains and remove vp association. 3578 * All mutexes must be held 3579 */ 3580 static void 3581 page_do_hashout(page_t *pp) 3582 { 3583 page_t **hpp; 3584 page_t *hp; 3585 vnode_t *vp = pp->p_vnode; 3586 3587 ASSERT(vp != NULL); 3588 ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 3589 3590 /* 3591 * First, take pp off of its hash chain. 3592 */ 3593 hpp = &page_hash[PAGE_HASH_FUNC(vp, pp->p_offset)]; 3594 3595 for (;;) { 3596 hp = *hpp; 3597 if (hp == pp) 3598 break; 3599 if (hp == NULL) { 3600 panic("page_do_hashout"); 3601 /*NOTREACHED*/ 3602 } 3603 hpp = &hp->p_hash; 3604 } 3605 *hpp = pp->p_hash; 3606 3607 /* 3608 * Now remove it from its associated vnode. 3609 */ 3610 if (vp->v_pages) 3611 page_vpsub(&vp->v_pages, pp); 3612 3613 pp->p_hash = NULL; 3614 page_clr_all_props(pp); 3615 PP_CLRSWAP(pp); 3616 pp->p_vnode = NULL; 3617 pp->p_offset = (u_offset_t)-1; 3618 } 3619 3620 /* 3621 * Remove page ``pp'' from the hash and vp chains and remove vp association. 3622 * 3623 * When `phm' is non-NULL it contains the address of the mutex protecting the 3624 * hash list pp is on. It is not dropped. 3625 */ 3626 void 3627 page_hashout(page_t *pp, kmutex_t *phm) 3628 { 3629 vnode_t *vp; 3630 ulong_t index; 3631 kmutex_t *nphm; 3632 kmutex_t *vphm; 3633 kmutex_t *sep; 3634 3635 ASSERT(phm != NULL ? MUTEX_HELD(phm) : 1); 3636 ASSERT(pp->p_vnode != NULL); 3637 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 3638 ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(pp->p_vnode))); 3639 3640 vp = pp->p_vnode; 3641 3642 TRACE_2(TR_FAC_VM, TR_PAGE_HASHOUT, 3643 "page_hashout:pp %p vp %p", pp, vp); 3644 3645 /* Kernel probe */ 3646 TNF_PROBE_2(page_unmap, "vm pagefault", /* CSTYLED */, 3647 tnf_opaque, vnode, vp, 3648 tnf_offset, offset, pp->p_offset); 3649 3650 /* 3651 * 3652 */ 3653 VM_STAT_ADD(hashout_count); 3654 index = PAGE_HASH_FUNC(vp, pp->p_offset); 3655 if (phm == NULL) { 3656 VM_STAT_ADD(hashout_not_held); 3657 nphm = PAGE_HASH_MUTEX(index); 3658 mutex_enter(nphm); 3659 } 3660 ASSERT(phm ? phm == PAGE_HASH_MUTEX(index) : 1); 3661 3662 3663 /* 3664 * grab page vnode mutex and remove it... 3665 */ 3666 vphm = page_vnode_mutex(vp); 3667 mutex_enter(vphm); 3668 3669 page_do_hashout(pp); 3670 3671 mutex_exit(vphm); 3672 if (phm == NULL) 3673 mutex_exit(nphm); 3674 3675 /* 3676 * Wake up processes waiting for this page. The page's 3677 * identity has been changed, and is probably not the 3678 * desired page any longer. 3679 */ 3680 sep = page_se_mutex(pp); 3681 mutex_enter(sep); 3682 pp->p_selock &= ~SE_EWANTED; 3683 if (CV_HAS_WAITERS(&pp->p_cv)) 3684 cv_broadcast(&pp->p_cv); 3685 mutex_exit(sep); 3686 } 3687 3688 /* 3689 * Add the page to the front of a linked list of pages 3690 * using the p_next & p_prev pointers for the list. 3691 * The caller is responsible for protecting the list pointers. 3692 */ 3693 void 3694 page_add(page_t **ppp, page_t *pp) 3695 { 3696 ASSERT(PAGE_EXCL(pp) || (PAGE_SHARED(pp) && page_iolock_assert(pp))); 3697 3698 page_add_common(ppp, pp); 3699 } 3700 3701 3702 3703 /* 3704 * Common code for page_add() and mach_page_add() 3705 */ 3706 void 3707 page_add_common(page_t **ppp, page_t *pp) 3708 { 3709 if (*ppp == NULL) { 3710 pp->p_next = pp->p_prev = pp; 3711 } else { 3712 pp->p_next = *ppp; 3713 pp->p_prev = (*ppp)->p_prev; 3714 (*ppp)->p_prev = pp; 3715 pp->p_prev->p_next = pp; 3716 } 3717 *ppp = pp; 3718 } 3719 3720 3721 /* 3722 * Remove this page from a linked list of pages 3723 * using the p_next & p_prev pointers for the list. 3724 * 3725 * The caller is responsible for protecting the list pointers. 3726 */ 3727 void 3728 page_sub(page_t **ppp, page_t *pp) 3729 { 3730 ASSERT((PP_ISFREE(pp)) ? 1 : 3731 (PAGE_EXCL(pp)) || (PAGE_SHARED(pp) && page_iolock_assert(pp))); 3732 3733 if (*ppp == NULL || pp == NULL) { 3734 panic("page_sub: bad arg(s): pp %p, *ppp %p", 3735 (void *)pp, (void *)(*ppp)); 3736 /*NOTREACHED*/ 3737 } 3738 3739 page_sub_common(ppp, pp); 3740 } 3741 3742 3743 /* 3744 * Common code for page_sub() and mach_page_sub() 3745 */ 3746 void 3747 page_sub_common(page_t **ppp, page_t *pp) 3748 { 3749 if (*ppp == pp) 3750 *ppp = pp->p_next; /* go to next page */ 3751 3752 if (*ppp == pp) 3753 *ppp = NULL; /* page list is gone */ 3754 else { 3755 pp->p_prev->p_next = pp->p_next; 3756 pp->p_next->p_prev = pp->p_prev; 3757 } 3758 pp->p_prev = pp->p_next = pp; /* make pp a list of one */ 3759 } 3760 3761 3762 /* 3763 * Break page list cppp into two lists with npages in the first list. 3764 * The tail is returned in nppp. 3765 */ 3766 void 3767 page_list_break(page_t **oppp, page_t **nppp, pgcnt_t npages) 3768 { 3769 page_t *s1pp = *oppp; 3770 page_t *s2pp; 3771 page_t *e1pp, *e2pp; 3772 long n = 0; 3773 3774 if (s1pp == NULL) { 3775 *nppp = NULL; 3776 return; 3777 } 3778 if (npages == 0) { 3779 *nppp = s1pp; 3780 *oppp = NULL; 3781 return; 3782 } 3783 for (n = 0, s2pp = *oppp; n < npages; n++) { 3784 s2pp = s2pp->p_next; 3785 } 3786 /* Fix head and tail of new lists */ 3787 e1pp = s2pp->p_prev; 3788 e2pp = s1pp->p_prev; 3789 s1pp->p_prev = e1pp; 3790 e1pp->p_next = s1pp; 3791 s2pp->p_prev = e2pp; 3792 e2pp->p_next = s2pp; 3793 3794 /* second list empty */ 3795 if (s2pp == s1pp) { 3796 *oppp = s1pp; 3797 *nppp = NULL; 3798 } else { 3799 *oppp = s1pp; 3800 *nppp = s2pp; 3801 } 3802 } 3803 3804 /* 3805 * Concatenate page list nppp onto the end of list ppp. 3806 */ 3807 void 3808 page_list_concat(page_t **ppp, page_t **nppp) 3809 { 3810 page_t *s1pp, *s2pp, *e1pp, *e2pp; 3811 3812 if (*nppp == NULL) { 3813 return; 3814 } 3815 if (*ppp == NULL) { 3816 *ppp = *nppp; 3817 return; 3818 } 3819 s1pp = *ppp; 3820 e1pp = s1pp->p_prev; 3821 s2pp = *nppp; 3822 e2pp = s2pp->p_prev; 3823 s1pp->p_prev = e2pp; 3824 e2pp->p_next = s1pp; 3825 e1pp->p_next = s2pp; 3826 s2pp->p_prev = e1pp; 3827 } 3828 3829 /* 3830 * return the next page in the page list 3831 */ 3832 page_t * 3833 page_list_next(page_t *pp) 3834 { 3835 return (pp->p_next); 3836 } 3837 3838 3839 /* 3840 * Add the page to the front of the linked list of pages 3841 * using p_vpnext/p_vpprev pointers for the list. 3842 * 3843 * The caller is responsible for protecting the lists. 3844 */ 3845 void 3846 page_vpadd(page_t **ppp, page_t *pp) 3847 { 3848 if (*ppp == NULL) { 3849 pp->p_vpnext = pp->p_vpprev = pp; 3850 } else { 3851 pp->p_vpnext = *ppp; 3852 pp->p_vpprev = (*ppp)->p_vpprev; 3853 (*ppp)->p_vpprev = pp; 3854 pp->p_vpprev->p_vpnext = pp; 3855 } 3856 *ppp = pp; 3857 } 3858 3859 /* 3860 * Remove this page from the linked list of pages 3861 * using p_vpnext/p_vpprev pointers for the list. 3862 * 3863 * The caller is responsible for protecting the lists. 3864 */ 3865 void 3866 page_vpsub(page_t **ppp, page_t *pp) 3867 { 3868 if (*ppp == NULL || pp == NULL) { 3869 panic("page_vpsub: bad arg(s): pp %p, *ppp %p", 3870 (void *)pp, (void *)(*ppp)); 3871 /*NOTREACHED*/ 3872 } 3873 3874 if (*ppp == pp) 3875 *ppp = pp->p_vpnext; /* go to next page */ 3876 3877 if (*ppp == pp) 3878 *ppp = NULL; /* page list is gone */ 3879 else { 3880 pp->p_vpprev->p_vpnext = pp->p_vpnext; 3881 pp->p_vpnext->p_vpprev = pp->p_vpprev; 3882 } 3883 pp->p_vpprev = pp->p_vpnext = pp; /* make pp a list of one */ 3884 } 3885 3886 /* 3887 * Lock a physical page into memory "long term". Used to support "lock 3888 * in memory" functions. Accepts the page to be locked, and a cow variable 3889 * to indicate whether a the lock will travel to the new page during 3890 * a potential copy-on-write. 3891 */ 3892 int 3893 page_pp_lock( 3894 page_t *pp, /* page to be locked */ 3895 int cow, /* cow lock */ 3896 int kernel) /* must succeed -- ignore checking */ 3897 { 3898 int r = 0; /* result -- assume failure */ 3899 3900 ASSERT(PAGE_LOCKED(pp)); 3901 3902 page_struct_lock(pp); 3903 /* 3904 * Acquire the "freemem_lock" for availrmem. 3905 */ 3906 if (cow) { 3907 mutex_enter(&freemem_lock); 3908 if ((availrmem > pages_pp_maximum) && 3909 (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM)) { 3910 availrmem--; 3911 pages_locked++; 3912 mutex_exit(&freemem_lock); 3913 r = 1; 3914 if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 3915 cmn_err(CE_WARN, 3916 "COW lock limit reached on pfn 0x%lx", 3917 page_pptonum(pp)); 3918 } 3919 } else 3920 mutex_exit(&freemem_lock); 3921 } else { 3922 if (pp->p_lckcnt) { 3923 if (pp->p_lckcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 3924 r = 1; 3925 if (++pp->p_lckcnt == 3926 (ushort_t)PAGE_LOCK_MAXIMUM) { 3927 cmn_err(CE_WARN, "Page lock limit " 3928 "reached on pfn 0x%lx", 3929 page_pptonum(pp)); 3930 } 3931 } 3932 } else { 3933 if (kernel) { 3934 /* availrmem accounting done by caller */ 3935 ++pp->p_lckcnt; 3936 r = 1; 3937 } else { 3938 mutex_enter(&freemem_lock); 3939 if (availrmem > pages_pp_maximum) { 3940 availrmem--; 3941 pages_locked++; 3942 ++pp->p_lckcnt; 3943 r = 1; 3944 } 3945 mutex_exit(&freemem_lock); 3946 } 3947 } 3948 } 3949 page_struct_unlock(pp); 3950 return (r); 3951 } 3952 3953 /* 3954 * Decommit a lock on a physical page frame. Account for cow locks if 3955 * appropriate. 3956 */ 3957 void 3958 page_pp_unlock( 3959 page_t *pp, /* page to be unlocked */ 3960 int cow, /* expect cow lock */ 3961 int kernel) /* this was a kernel lock */ 3962 { 3963 ASSERT(PAGE_LOCKED(pp)); 3964 3965 page_struct_lock(pp); 3966 /* 3967 * Acquire the "freemem_lock" for availrmem. 3968 * If cowcnt or lcknt is already 0 do nothing; i.e., we 3969 * could be called to unlock even if nothing is locked. This could 3970 * happen if locked file pages were truncated (removing the lock) 3971 * and the file was grown again and new pages faulted in; the new 3972 * pages are unlocked but the segment still thinks they're locked. 3973 */ 3974 if (cow) { 3975 if (pp->p_cowcnt) { 3976 mutex_enter(&freemem_lock); 3977 pp->p_cowcnt--; 3978 availrmem++; 3979 pages_locked--; 3980 mutex_exit(&freemem_lock); 3981 } 3982 } else { 3983 if (pp->p_lckcnt && --pp->p_lckcnt == 0) { 3984 if (!kernel) { 3985 mutex_enter(&freemem_lock); 3986 availrmem++; 3987 pages_locked--; 3988 mutex_exit(&freemem_lock); 3989 } 3990 } 3991 } 3992 page_struct_unlock(pp); 3993 } 3994 3995 /* 3996 * This routine reserves availrmem for npages; 3997 * flags: KM_NOSLEEP or KM_SLEEP 3998 * returns 1 on success or 0 on failure 3999 */ 4000 int 4001 page_resv(pgcnt_t npages, uint_t flags) 4002 { 4003 mutex_enter(&freemem_lock); 4004 while (availrmem < tune.t_minarmem + npages) { 4005 if (flags & KM_NOSLEEP) { 4006 mutex_exit(&freemem_lock); 4007 return (0); 4008 } 4009 mutex_exit(&freemem_lock); 4010 page_needfree(npages); 4011 kmem_reap(); 4012 delay(hz >> 2); 4013 page_needfree(-(spgcnt_t)npages); 4014 mutex_enter(&freemem_lock); 4015 } 4016 availrmem -= npages; 4017 mutex_exit(&freemem_lock); 4018 return (1); 4019 } 4020 4021 /* 4022 * This routine unreserves availrmem for npages; 4023 */ 4024 void 4025 page_unresv(pgcnt_t npages) 4026 { 4027 mutex_enter(&freemem_lock); 4028 availrmem += npages; 4029 mutex_exit(&freemem_lock); 4030 } 4031 4032 /* 4033 * See Statement at the beginning of segvn_lockop() regarding 4034 * the way we handle cowcnts and lckcnts. 4035 * 4036 * Transfer cowcnt on 'opp' to cowcnt on 'npp' if the vpage 4037 * that breaks COW has PROT_WRITE. 4038 * 4039 * Note that, we may also break COW in case we are softlocking 4040 * on read access during physio; 4041 * in this softlock case, the vpage may not have PROT_WRITE. 4042 * So, we need to transfer lckcnt on 'opp' to lckcnt on 'npp' 4043 * if the vpage doesn't have PROT_WRITE. 4044 * 4045 * This routine is never called if we are stealing a page 4046 * in anon_private. 4047 * 4048 * The caller subtracted from availrmem for read only mapping. 4049 * if lckcnt is 1 increment availrmem. 4050 */ 4051 void 4052 page_pp_useclaim( 4053 page_t *opp, /* original page frame losing lock */ 4054 page_t *npp, /* new page frame gaining lock */ 4055 uint_t write_perm) /* set if vpage has PROT_WRITE */ 4056 { 4057 int payback = 0; 4058 4059 ASSERT(PAGE_LOCKED(opp)); 4060 ASSERT(PAGE_LOCKED(npp)); 4061 4062 page_struct_lock(opp); 4063 4064 ASSERT(npp->p_cowcnt == 0); 4065 ASSERT(npp->p_lckcnt == 0); 4066 4067 /* Don't use claim if nothing is locked (see page_pp_unlock above) */ 4068 if ((write_perm && opp->p_cowcnt != 0) || 4069 (!write_perm && opp->p_lckcnt != 0)) { 4070 4071 if (write_perm) { 4072 npp->p_cowcnt++; 4073 ASSERT(opp->p_cowcnt != 0); 4074 opp->p_cowcnt--; 4075 } else { 4076 4077 ASSERT(opp->p_lckcnt != 0); 4078 4079 /* 4080 * We didn't need availrmem decremented if p_lckcnt on 4081 * original page is 1. Here, we are unlocking 4082 * read-only copy belonging to original page and 4083 * are locking a copy belonging to new page. 4084 */ 4085 if (opp->p_lckcnt == 1) 4086 payback = 1; 4087 4088 npp->p_lckcnt++; 4089 opp->p_lckcnt--; 4090 } 4091 } 4092 if (payback) { 4093 mutex_enter(&freemem_lock); 4094 availrmem++; 4095 pages_useclaim--; 4096 mutex_exit(&freemem_lock); 4097 } 4098 page_struct_unlock(opp); 4099 } 4100 4101 /* 4102 * Simple claim adjust functions -- used to support changes in 4103 * claims due to changes in access permissions. Used by segvn_setprot(). 4104 */ 4105 int 4106 page_addclaim(page_t *pp) 4107 { 4108 int r = 0; /* result */ 4109 4110 ASSERT(PAGE_LOCKED(pp)); 4111 4112 page_struct_lock(pp); 4113 ASSERT(pp->p_lckcnt != 0); 4114 4115 if (pp->p_lckcnt == 1) { 4116 if (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 4117 --pp->p_lckcnt; 4118 r = 1; 4119 if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 4120 cmn_err(CE_WARN, 4121 "COW lock limit reached on pfn 0x%lx", 4122 page_pptonum(pp)); 4123 } 4124 } 4125 } else { 4126 mutex_enter(&freemem_lock); 4127 if ((availrmem > pages_pp_maximum) && 4128 (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM)) { 4129 --availrmem; 4130 ++pages_claimed; 4131 mutex_exit(&freemem_lock); 4132 --pp->p_lckcnt; 4133 r = 1; 4134 if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 4135 cmn_err(CE_WARN, 4136 "COW lock limit reached on pfn 0x%lx", 4137 page_pptonum(pp)); 4138 } 4139 } else 4140 mutex_exit(&freemem_lock); 4141 } 4142 page_struct_unlock(pp); 4143 return (r); 4144 } 4145 4146 int 4147 page_subclaim(page_t *pp) 4148 { 4149 int r = 0; 4150 4151 ASSERT(PAGE_LOCKED(pp)); 4152 4153 page_struct_lock(pp); 4154 ASSERT(pp->p_cowcnt != 0); 4155 4156 if (pp->p_lckcnt) { 4157 if (pp->p_lckcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 4158 r = 1; 4159 /* 4160 * for availrmem 4161 */ 4162 mutex_enter(&freemem_lock); 4163 availrmem++; 4164 pages_claimed--; 4165 mutex_exit(&freemem_lock); 4166 4167 pp->p_cowcnt--; 4168 4169 if (++pp->p_lckcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 4170 cmn_err(CE_WARN, 4171 "Page lock limit reached on pfn 0x%lx", 4172 page_pptonum(pp)); 4173 } 4174 } 4175 } else { 4176 r = 1; 4177 pp->p_cowcnt--; 4178 pp->p_lckcnt++; 4179 } 4180 page_struct_unlock(pp); 4181 return (r); 4182 } 4183 4184 int 4185 page_addclaim_pages(page_t **ppa) 4186 { 4187 4188 pgcnt_t lckpgs = 0, pg_idx; 4189 4190 VM_STAT_ADD(pagecnt.pc_addclaim_pages); 4191 4192 mutex_enter(&page_llock); 4193 for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 4194 4195 ASSERT(PAGE_LOCKED(ppa[pg_idx])); 4196 ASSERT(ppa[pg_idx]->p_lckcnt != 0); 4197 if (ppa[pg_idx]->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 4198 mutex_exit(&page_llock); 4199 return (0); 4200 } 4201 if (ppa[pg_idx]->p_lckcnt > 1) 4202 lckpgs++; 4203 } 4204 4205 if (lckpgs != 0) { 4206 mutex_enter(&freemem_lock); 4207 if (availrmem >= pages_pp_maximum + lckpgs) { 4208 availrmem -= lckpgs; 4209 pages_claimed += lckpgs; 4210 } else { 4211 mutex_exit(&freemem_lock); 4212 mutex_exit(&page_llock); 4213 return (0); 4214 } 4215 mutex_exit(&freemem_lock); 4216 } 4217 4218 for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 4219 ppa[pg_idx]->p_lckcnt--; 4220 ppa[pg_idx]->p_cowcnt++; 4221 } 4222 mutex_exit(&page_llock); 4223 return (1); 4224 } 4225 4226 int 4227 page_subclaim_pages(page_t **ppa) 4228 { 4229 pgcnt_t ulckpgs = 0, pg_idx; 4230 4231 VM_STAT_ADD(pagecnt.pc_subclaim_pages); 4232 4233 mutex_enter(&page_llock); 4234 for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 4235 4236 ASSERT(PAGE_LOCKED(ppa[pg_idx])); 4237 ASSERT(ppa[pg_idx]->p_cowcnt != 0); 4238 if (ppa[pg_idx]->p_lckcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 4239 mutex_exit(&page_llock); 4240 return (0); 4241 } 4242 if (ppa[pg_idx]->p_lckcnt != 0) 4243 ulckpgs++; 4244 } 4245 4246 if (ulckpgs != 0) { 4247 mutex_enter(&freemem_lock); 4248 availrmem += ulckpgs; 4249 pages_claimed -= ulckpgs; 4250 mutex_exit(&freemem_lock); 4251 } 4252 4253 for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 4254 ppa[pg_idx]->p_cowcnt--; 4255 ppa[pg_idx]->p_lckcnt++; 4256 4257 } 4258 mutex_exit(&page_llock); 4259 return (1); 4260 } 4261 4262 page_t * 4263 page_numtopp(pfn_t pfnum, se_t se) 4264 { 4265 page_t *pp; 4266 4267 retry: 4268 pp = page_numtopp_nolock(pfnum); 4269 if (pp == NULL) { 4270 return ((page_t *)NULL); 4271 } 4272 4273 /* 4274 * Acquire the appropriate lock on the page. 4275 */ 4276 while (!page_lock(pp, se, (kmutex_t *)NULL, P_RECLAIM)) { 4277 if (page_pptonum(pp) != pfnum) 4278 goto retry; 4279 continue; 4280 } 4281 4282 if (page_pptonum(pp) != pfnum) { 4283 page_unlock(pp); 4284 goto retry; 4285 } 4286 4287 return (pp); 4288 } 4289 4290 page_t * 4291 page_numtopp_noreclaim(pfn_t pfnum, se_t se) 4292 { 4293 page_t *pp; 4294 4295 retry: 4296 pp = page_numtopp_nolock(pfnum); 4297 if (pp == NULL) { 4298 return ((page_t *)NULL); 4299 } 4300 4301 /* 4302 * Acquire the appropriate lock on the page. 4303 */ 4304 while (!page_lock(pp, se, (kmutex_t *)NULL, P_NO_RECLAIM)) { 4305 if (page_pptonum(pp) != pfnum) 4306 goto retry; 4307 continue; 4308 } 4309 4310 if (page_pptonum(pp) != pfnum) { 4311 page_unlock(pp); 4312 goto retry; 4313 } 4314 4315 return (pp); 4316 } 4317 4318 /* 4319 * This routine is like page_numtopp, but will only return page structs 4320 * for pages which are ok for loading into hardware using the page struct. 4321 */ 4322 page_t * 4323 page_numtopp_nowait(pfn_t pfnum, se_t se) 4324 { 4325 page_t *pp; 4326 4327 retry: 4328 pp = page_numtopp_nolock(pfnum); 4329 if (pp == NULL) { 4330 return ((page_t *)NULL); 4331 } 4332 4333 /* 4334 * Try to acquire the appropriate lock on the page. 4335 */ 4336 if (PP_ISFREE(pp)) 4337 pp = NULL; 4338 else { 4339 if (!page_trylock(pp, se)) 4340 pp = NULL; 4341 else { 4342 if (page_pptonum(pp) != pfnum) { 4343 page_unlock(pp); 4344 goto retry; 4345 } 4346 if (PP_ISFREE(pp)) { 4347 page_unlock(pp); 4348 pp = NULL; 4349 } 4350 } 4351 } 4352 return (pp); 4353 } 4354 4355 /* 4356 * Returns a count of dirty pages that are in the process 4357 * of being written out. If 'cleanit' is set, try to push the page. 4358 */ 4359 pgcnt_t 4360 page_busy(int cleanit) 4361 { 4362 page_t *page0 = page_first(); 4363 page_t *pp = page0; 4364 pgcnt_t nppbusy = 0; 4365 u_offset_t off; 4366 4367 do { 4368 vnode_t *vp = pp->p_vnode; 4369 4370 /* 4371 * A page is a candidate for syncing if it is: 4372 * 4373 * (a) On neither the freelist nor the cachelist 4374 * (b) Hashed onto a vnode 4375 * (c) Not a kernel page 4376 * (d) Dirty 4377 * (e) Not part of a swapfile 4378 * (f) a page which belongs to a real vnode; eg has a non-null 4379 * v_vfsp pointer. 4380 * (g) Backed by a filesystem which doesn't have a 4381 * stubbed-out sync operation 4382 */ 4383 if (!PP_ISFREE(pp) && vp != NULL && !VN_ISKAS(vp) && 4384 hat_ismod(pp) && !IS_SWAPVP(vp) && vp->v_vfsp != NULL && 4385 vfs_can_sync(vp->v_vfsp)) { 4386 nppbusy++; 4387 vfs_syncprogress(); 4388 4389 if (!cleanit) 4390 continue; 4391 if (!page_trylock(pp, SE_EXCL)) 4392 continue; 4393 4394 if (PP_ISFREE(pp) || vp == NULL || IS_SWAPVP(vp) || 4395 pp->p_lckcnt != 0 || pp->p_cowcnt != 0 || 4396 !(hat_pagesync(pp, 4397 HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD) & P_MOD)) { 4398 page_unlock(pp); 4399 continue; 4400 } 4401 off = pp->p_offset; 4402 VN_HOLD(vp); 4403 page_unlock(pp); 4404 (void) VOP_PUTPAGE(vp, off, PAGESIZE, 4405 B_ASYNC | B_FREE, kcred); 4406 VN_RELE(vp); 4407 } 4408 } while ((pp = page_next(pp)) != page0); 4409 4410 return (nppbusy); 4411 } 4412 4413 void page_invalidate_pages(void); 4414 4415 /* 4416 * callback handler to vm sub-system 4417 * 4418 * callers make sure no recursive entries to this func. 4419 */ 4420 /*ARGSUSED*/ 4421 boolean_t 4422 callb_vm_cpr(void *arg, int code) 4423 { 4424 if (code == CB_CODE_CPR_CHKPT) 4425 page_invalidate_pages(); 4426 return (B_TRUE); 4427 } 4428 4429 /* 4430 * Invalidate all pages of the system. 4431 * It shouldn't be called until all user page activities are all stopped. 4432 */ 4433 void 4434 page_invalidate_pages() 4435 { 4436 page_t *pp; 4437 page_t *page0; 4438 pgcnt_t nbusypages; 4439 int retry = 0; 4440 const int MAXRETRIES = 4; 4441 #if defined(__sparc) 4442 extern struct vnode prom_ppages; 4443 #endif /* __sparc */ 4444 4445 top: 4446 /* 4447 * Flush dirty pages and destroy the clean ones. 4448 */ 4449 nbusypages = 0; 4450 4451 pp = page0 = page_first(); 4452 do { 4453 struct vnode *vp; 4454 u_offset_t offset; 4455 int mod; 4456 4457 /* 4458 * skip the page if it has no vnode or the page associated 4459 * with the kernel vnode or prom allocated kernel mem. 4460 */ 4461 #if defined(__sparc) 4462 if ((vp = pp->p_vnode) == NULL || VN_ISKAS(vp) || 4463 vp == &prom_ppages) 4464 #else /* x86 doesn't have prom or prom_ppage */ 4465 if ((vp = pp->p_vnode) == NULL || VN_ISKAS(vp)) 4466 #endif /* __sparc */ 4467 continue; 4468 4469 /* 4470 * skip the page which is already free invalidated. 4471 */ 4472 if (PP_ISFREE(pp) && PP_ISAGED(pp)) 4473 continue; 4474 4475 /* 4476 * skip pages that are already locked or can't be "exclusively" 4477 * locked or are already free. After we lock the page, check 4478 * the free and age bits again to be sure it's not destroied 4479 * yet. 4480 * To achieve max. parallelization, we use page_trylock instead 4481 * of page_lock so that we don't get block on individual pages 4482 * while we have thousands of other pages to process. 4483 */ 4484 if (!page_trylock(pp, SE_EXCL)) { 4485 nbusypages++; 4486 continue; 4487 } else if (PP_ISFREE(pp)) { 4488 if (!PP_ISAGED(pp)) { 4489 page_destroy_free(pp); 4490 } else { 4491 page_unlock(pp); 4492 } 4493 continue; 4494 } 4495 /* 4496 * Is this page involved in some I/O? shared? 4497 * 4498 * The page_struct_lock need not be acquired to 4499 * examine these fields since the page has an 4500 * "exclusive" lock. 4501 */ 4502 if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) { 4503 page_unlock(pp); 4504 continue; 4505 } 4506 4507 if (vp->v_type == VCHR) { 4508 panic("vp->v_type == VCHR"); 4509 /*NOTREACHED*/ 4510 } 4511 4512 if (!page_try_demote_pages(pp)) { 4513 page_unlock(pp); 4514 continue; 4515 } 4516 4517 /* 4518 * Check the modified bit. Leave the bits alone in hardware 4519 * (they will be modified if we do the putpage). 4520 */ 4521 mod = (hat_pagesync(pp, HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD) 4522 & P_MOD); 4523 if (mod) { 4524 offset = pp->p_offset; 4525 /* 4526 * Hold the vnode before releasing the page lock 4527 * to prevent it from being freed and re-used by 4528 * some other thread. 4529 */ 4530 VN_HOLD(vp); 4531 page_unlock(pp); 4532 /* 4533 * No error return is checked here. Callers such as 4534 * cpr deals with the dirty pages at the dump time 4535 * if this putpage fails. 4536 */ 4537 (void) VOP_PUTPAGE(vp, offset, PAGESIZE, B_INVAL, 4538 kcred); 4539 VN_RELE(vp); 4540 } else { 4541 page_destroy(pp, 0); 4542 } 4543 } while ((pp = page_next(pp)) != page0); 4544 if (nbusypages && retry++ < MAXRETRIES) { 4545 delay(1); 4546 goto top; 4547 } 4548 } 4549 4550 /* 4551 * Replace the page "old" with the page "new" on the page hash and vnode lists 4552 * 4553 * the replacemnt must be done in place, ie the equivalent sequence: 4554 * 4555 * vp = old->p_vnode; 4556 * off = old->p_offset; 4557 * page_do_hashout(old) 4558 * page_do_hashin(new, vp, off) 4559 * 4560 * doesn't work, since 4561 * 1) if old is the only page on the vnode, the v_pages list has a window 4562 * where it looks empty. This will break file system assumptions. 4563 * and 4564 * 2) pvn_vplist_dirty() can't deal with pages moving on the v_pages list. 4565 */ 4566 static void 4567 page_do_relocate_hash(page_t *new, page_t *old) 4568 { 4569 page_t **hash_list; 4570 vnode_t *vp = old->p_vnode; 4571 kmutex_t *sep; 4572 4573 ASSERT(PAGE_EXCL(old)); 4574 ASSERT(PAGE_EXCL(new)); 4575 ASSERT(vp != NULL); 4576 ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 4577 ASSERT(MUTEX_HELD(PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, old->p_offset)))); 4578 4579 /* 4580 * First find old page on the page hash list 4581 */ 4582 hash_list = &page_hash[PAGE_HASH_FUNC(vp, old->p_offset)]; 4583 4584 for (;;) { 4585 if (*hash_list == old) 4586 break; 4587 if (*hash_list == NULL) { 4588 panic("page_do_hashout"); 4589 /*NOTREACHED*/ 4590 } 4591 hash_list = &(*hash_list)->p_hash; 4592 } 4593 4594 /* 4595 * update new and replace old with new on the page hash list 4596 */ 4597 new->p_vnode = old->p_vnode; 4598 new->p_offset = old->p_offset; 4599 new->p_hash = old->p_hash; 4600 *hash_list = new; 4601 4602 if ((new->p_vnode->v_flag & VISSWAP) != 0) 4603 PP_SETSWAP(new); 4604 4605 /* 4606 * replace old with new on the vnode's page list 4607 */ 4608 if (old->p_vpnext == old) { 4609 new->p_vpnext = new; 4610 new->p_vpprev = new; 4611 } else { 4612 new->p_vpnext = old->p_vpnext; 4613 new->p_vpprev = old->p_vpprev; 4614 new->p_vpnext->p_vpprev = new; 4615 new->p_vpprev->p_vpnext = new; 4616 } 4617 if (vp->v_pages == old) 4618 vp->v_pages = new; 4619 4620 /* 4621 * clear out the old page 4622 */ 4623 old->p_hash = NULL; 4624 old->p_vpnext = NULL; 4625 old->p_vpprev = NULL; 4626 old->p_vnode = NULL; 4627 PP_CLRSWAP(old); 4628 old->p_offset = (u_offset_t)-1; 4629 page_clr_all_props(old); 4630 4631 /* 4632 * Wake up processes waiting for this page. The page's 4633 * identity has been changed, and is probably not the 4634 * desired page any longer. 4635 */ 4636 sep = page_se_mutex(old); 4637 mutex_enter(sep); 4638 old->p_selock &= ~SE_EWANTED; 4639 if (CV_HAS_WAITERS(&old->p_cv)) 4640 cv_broadcast(&old->p_cv); 4641 mutex_exit(sep); 4642 } 4643 4644 /* 4645 * This function moves the identity of page "pp_old" to page "pp_new". 4646 * Both pages must be locked on entry. "pp_new" is free, has no identity, 4647 * and need not be hashed out from anywhere. 4648 */ 4649 void 4650 page_relocate_hash(page_t *pp_new, page_t *pp_old) 4651 { 4652 vnode_t *vp = pp_old->p_vnode; 4653 u_offset_t off = pp_old->p_offset; 4654 kmutex_t *phm, *vphm; 4655 4656 /* 4657 * Rehash two pages 4658 */ 4659 ASSERT(PAGE_EXCL(pp_old)); 4660 ASSERT(PAGE_EXCL(pp_new)); 4661 ASSERT(vp != NULL); 4662 ASSERT(pp_new->p_vnode == NULL); 4663 4664 /* 4665 * hashout then hashin while holding the mutexes 4666 */ 4667 phm = PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, off)); 4668 mutex_enter(phm); 4669 vphm = page_vnode_mutex(vp); 4670 mutex_enter(vphm); 4671 4672 page_do_relocate_hash(pp_new, pp_old); 4673 4674 mutex_exit(vphm); 4675 mutex_exit(phm); 4676 4677 /* 4678 * The page_struct_lock need not be acquired for lckcnt and 4679 * cowcnt since the page has an "exclusive" lock. 4680 */ 4681 ASSERT(pp_new->p_lckcnt == 0); 4682 ASSERT(pp_new->p_cowcnt == 0); 4683 pp_new->p_lckcnt = pp_old->p_lckcnt; 4684 pp_new->p_cowcnt = pp_old->p_cowcnt; 4685 pp_old->p_lckcnt = pp_old->p_cowcnt = 0; 4686 4687 /* The following comment preserved from page_flip(). */ 4688 /* XXX - Do we need to protect fsdata? */ 4689 pp_new->p_fsdata = pp_old->p_fsdata; 4690 } 4691 4692 /* 4693 * Helper routine used to lock all remaining members of a 4694 * large page. The caller is responsible for passing in a locked 4695 * pp. If pp is a large page, then it succeeds in locking all the 4696 * remaining constituent pages or it returns with only the 4697 * original page locked. 4698 * 4699 * Returns 1 on success, 0 on failure. 4700 * 4701 * If success is returned this routine gurantees p_szc for all constituent 4702 * pages of a large page pp belongs to can't change. To achieve this we 4703 * recheck szc of pp after locking all constituent pages and retry if szc 4704 * changed (it could only decrease). Since hat_page_demote() needs an EXCL 4705 * lock on one of constituent pages it can't be running after all constituent 4706 * pages are locked. hat_page_demote() with a lock on a constituent page 4707 * outside of this large page (i.e. pp belonged to a larger large page) is 4708 * already done with all constituent pages of pp since the root's p_szc is 4709 * changed last. Thefore no need to synchronize with hat_page_demote() that 4710 * locked a constituent page outside of pp's current large page. 4711 */ 4712 #ifdef DEBUG 4713 uint32_t gpg_trylock_mtbf = 0; 4714 #endif 4715 4716 int 4717 group_page_trylock(page_t *pp, se_t se) 4718 { 4719 page_t *tpp; 4720 pgcnt_t npgs, i, j; 4721 uint_t pszc = pp->p_szc; 4722 4723 #ifdef DEBUG 4724 if (gpg_trylock_mtbf && !(gethrtime() % gpg_trylock_mtbf)) { 4725 return (0); 4726 } 4727 #endif 4728 4729 if (pp != PP_GROUPLEADER(pp, pszc)) { 4730 return (0); 4731 } 4732 4733 retry: 4734 ASSERT(PAGE_LOCKED_SE(pp, se)); 4735 ASSERT(!PP_ISFREE(pp)); 4736 if (pszc == 0) { 4737 return (1); 4738 } 4739 npgs = page_get_pagecnt(pszc); 4740 tpp = pp + 1; 4741 for (i = 1; i < npgs; i++, tpp++) { 4742 if (!page_trylock(tpp, se)) { 4743 tpp = pp + 1; 4744 for (j = 1; j < i; j++, tpp++) { 4745 page_unlock(tpp); 4746 } 4747 return (0); 4748 } 4749 } 4750 if (pp->p_szc != pszc) { 4751 ASSERT(pp->p_szc < pszc); 4752 ASSERT(pp->p_vnode != NULL && !PP_ISKAS(pp) && 4753 !IS_SWAPFSVP(pp->p_vnode)); 4754 tpp = pp + 1; 4755 for (i = 1; i < npgs; i++, tpp++) { 4756 page_unlock(tpp); 4757 } 4758 pszc = pp->p_szc; 4759 goto retry; 4760 } 4761 return (1); 4762 } 4763 4764 void 4765 group_page_unlock(page_t *pp) 4766 { 4767 page_t *tpp; 4768 pgcnt_t npgs, i; 4769 4770 ASSERT(PAGE_LOCKED(pp)); 4771 ASSERT(!PP_ISFREE(pp)); 4772 ASSERT(pp == PP_PAGEROOT(pp)); 4773 npgs = page_get_pagecnt(pp->p_szc); 4774 for (i = 1, tpp = pp + 1; i < npgs; i++, tpp++) { 4775 page_unlock(tpp); 4776 } 4777 } 4778 4779 /* 4780 * returns 4781 * 0 : on success and *nrelocp is number of relocated PAGESIZE pages 4782 * ERANGE : this is not a base page 4783 * EBUSY : failure to get locks on the page/pages 4784 * ENOMEM : failure to obtain replacement pages 4785 * EAGAIN : OBP has not yet completed its boot-time handoff to the kernel 4786 * EIO : An error occurred while trying to copy the page data 4787 * 4788 * Return with all constituent members of target and replacement 4789 * SE_EXCL locked. It is the callers responsibility to drop the 4790 * locks. 4791 */ 4792 int 4793 do_page_relocate( 4794 page_t **target, 4795 page_t **replacement, 4796 int grouplock, 4797 spgcnt_t *nrelocp, 4798 lgrp_t *lgrp) 4799 { 4800 page_t *first_repl; 4801 page_t *repl; 4802 page_t *targ; 4803 page_t *pl = NULL; 4804 uint_t ppattr; 4805 pfn_t pfn, repl_pfn; 4806 uint_t szc; 4807 spgcnt_t npgs, i; 4808 int repl_contig = 0; 4809 uint_t flags = 0; 4810 spgcnt_t dofree = 0; 4811 4812 *nrelocp = 0; 4813 4814 #if defined(__sparc) 4815 /* 4816 * We need to wait till OBP has completed 4817 * its boot-time handoff of its resources to the kernel 4818 * before we allow page relocation 4819 */ 4820 if (page_relocate_ready == 0) { 4821 return (EAGAIN); 4822 } 4823 #endif 4824 4825 /* 4826 * If this is not a base page, 4827 * just return with 0x0 pages relocated. 4828 */ 4829 targ = *target; 4830 ASSERT(PAGE_EXCL(targ)); 4831 ASSERT(!PP_ISFREE(targ)); 4832 szc = targ->p_szc; 4833 ASSERT(szc < mmu_page_sizes); 4834 VM_STAT_ADD(vmm_vmstats.ppr_reloc[szc]); 4835 pfn = targ->p_pagenum; 4836 if (pfn != PFN_BASE(pfn, szc)) { 4837 VM_STAT_ADD(vmm_vmstats.ppr_relocnoroot[szc]); 4838 return (ERANGE); 4839 } 4840 4841 if ((repl = *replacement) != NULL && repl->p_szc >= szc) { 4842 repl_pfn = repl->p_pagenum; 4843 if (repl_pfn != PFN_BASE(repl_pfn, szc)) { 4844 VM_STAT_ADD(vmm_vmstats.ppr_reloc_replnoroot[szc]); 4845 return (ERANGE); 4846 } 4847 repl_contig = 1; 4848 } 4849 4850 /* 4851 * We must lock all members of this large page or we cannot 4852 * relocate any part of it. 4853 */ 4854 if (grouplock != 0 && !group_page_trylock(targ, SE_EXCL)) { 4855 VM_STAT_ADD(vmm_vmstats.ppr_relocnolock[targ->p_szc]); 4856 return (EBUSY); 4857 } 4858 4859 /* 4860 * reread szc it could have been decreased before 4861 * group_page_trylock() was done. 4862 */ 4863 szc = targ->p_szc; 4864 ASSERT(szc < mmu_page_sizes); 4865 VM_STAT_ADD(vmm_vmstats.ppr_reloc[szc]); 4866 ASSERT(pfn == PFN_BASE(pfn, szc)); 4867 4868 npgs = page_get_pagecnt(targ->p_szc); 4869 4870 if (repl == NULL) { 4871 dofree = npgs; /* Size of target page in MMU pages */ 4872 if (!page_create_wait(dofree, 0)) { 4873 if (grouplock != 0) { 4874 group_page_unlock(targ); 4875 } 4876 VM_STAT_ADD(vmm_vmstats.ppr_relocnomem[szc]); 4877 return (ENOMEM); 4878 } 4879 4880 /* 4881 * seg kmem pages require that the target and replacement 4882 * page be the same pagesize. 4883 */ 4884 flags = (VN_ISKAS(targ->p_vnode)) ? PGR_SAMESZC : 0; 4885 repl = page_get_replacement_page(targ, lgrp, flags); 4886 if (repl == NULL) { 4887 if (grouplock != 0) { 4888 group_page_unlock(targ); 4889 } 4890 page_create_putback(dofree); 4891 VM_STAT_ADD(vmm_vmstats.ppr_relocnomem[szc]); 4892 return (ENOMEM); 4893 } 4894 } 4895 #ifdef DEBUG 4896 else { 4897 ASSERT(PAGE_LOCKED(repl)); 4898 } 4899 #endif /* DEBUG */ 4900 4901 #if defined(__sparc) 4902 /* 4903 * Let hat_page_relocate() complete the relocation if it's kernel page 4904 */ 4905 if (VN_ISKAS(targ->p_vnode)) { 4906 *replacement = repl; 4907 if (hat_page_relocate(target, replacement, nrelocp) != 0) { 4908 if (grouplock != 0) { 4909 group_page_unlock(targ); 4910 } 4911 if (dofree) { 4912 *replacement = NULL; 4913 page_free_replacement_page(repl); 4914 page_create_putback(dofree); 4915 } 4916 VM_STAT_ADD(vmm_vmstats.ppr_krelocfail[szc]); 4917 return (EAGAIN); 4918 } 4919 VM_STAT_ADD(vmm_vmstats.ppr_relocok[szc]); 4920 return (0); 4921 } 4922 #else 4923 #if defined(lint) 4924 dofree = dofree; 4925 #endif 4926 #endif 4927 4928 first_repl = repl; 4929 4930 for (i = 0; i < npgs; i++) { 4931 ASSERT(PAGE_EXCL(targ)); 4932 ASSERT(targ->p_slckcnt == 0); 4933 ASSERT(repl->p_slckcnt == 0); 4934 4935 (void) hat_pageunload(targ, HAT_FORCE_PGUNLOAD); 4936 4937 ASSERT(hat_page_getshare(targ) == 0); 4938 ASSERT(!PP_ISFREE(targ)); 4939 ASSERT(targ->p_pagenum == (pfn + i)); 4940 ASSERT(repl_contig == 0 || 4941 repl->p_pagenum == (repl_pfn + i)); 4942 4943 /* 4944 * Copy the page contents and attributes then 4945 * relocate the page in the page hash. 4946 */ 4947 if (ppcopy(targ, repl) == 0) { 4948 targ = *target; 4949 repl = first_repl; 4950 VM_STAT_ADD(vmm_vmstats.ppr_copyfail); 4951 if (grouplock != 0) { 4952 group_page_unlock(targ); 4953 } 4954 if (dofree) { 4955 *replacement = NULL; 4956 page_free_replacement_page(repl); 4957 page_create_putback(dofree); 4958 } 4959 return (EIO); 4960 } 4961 4962 targ++; 4963 if (repl_contig != 0) { 4964 repl++; 4965 } else { 4966 repl = repl->p_next; 4967 } 4968 } 4969 4970 repl = first_repl; 4971 targ = *target; 4972 4973 for (i = 0; i < npgs; i++) { 4974 ppattr = hat_page_getattr(targ, (P_MOD | P_REF | P_RO)); 4975 page_clr_all_props(repl); 4976 page_set_props(repl, ppattr); 4977 page_relocate_hash(repl, targ); 4978 4979 ASSERT(hat_page_getshare(targ) == 0); 4980 ASSERT(hat_page_getshare(repl) == 0); 4981 /* 4982 * Now clear the props on targ, after the 4983 * page_relocate_hash(), they no longer 4984 * have any meaning. 4985 */ 4986 page_clr_all_props(targ); 4987 ASSERT(targ->p_next == targ); 4988 ASSERT(targ->p_prev == targ); 4989 page_list_concat(&pl, &targ); 4990 4991 targ++; 4992 if (repl_contig != 0) { 4993 repl++; 4994 } else { 4995 repl = repl->p_next; 4996 } 4997 } 4998 /* assert that we have come full circle with repl */ 4999 ASSERT(repl_contig == 1 || first_repl == repl); 5000 5001 *target = pl; 5002 if (*replacement == NULL) { 5003 ASSERT(first_repl == repl); 5004 *replacement = repl; 5005 } 5006 VM_STAT_ADD(vmm_vmstats.ppr_relocok[szc]); 5007 *nrelocp = npgs; 5008 return (0); 5009 } 5010 /* 5011 * On success returns 0 and *nrelocp the number of PAGESIZE pages relocated. 5012 */ 5013 int 5014 page_relocate( 5015 page_t **target, 5016 page_t **replacement, 5017 int grouplock, 5018 int freetarget, 5019 spgcnt_t *nrelocp, 5020 lgrp_t *lgrp) 5021 { 5022 spgcnt_t ret; 5023 5024 /* do_page_relocate returns 0 on success or errno value */ 5025 ret = do_page_relocate(target, replacement, grouplock, nrelocp, lgrp); 5026 5027 if (ret != 0 || freetarget == 0) { 5028 return (ret); 5029 } 5030 if (*nrelocp == 1) { 5031 ASSERT(*target != NULL); 5032 page_free(*target, 1); 5033 } else { 5034 page_t *tpp = *target; 5035 uint_t szc = tpp->p_szc; 5036 pgcnt_t npgs = page_get_pagecnt(szc); 5037 ASSERT(npgs > 1); 5038 ASSERT(szc != 0); 5039 do { 5040 ASSERT(PAGE_EXCL(tpp)); 5041 ASSERT(!hat_page_is_mapped(tpp)); 5042 ASSERT(tpp->p_szc == szc); 5043 PP_SETFREE(tpp); 5044 PP_SETAGED(tpp); 5045 npgs--; 5046 } while ((tpp = tpp->p_next) != *target); 5047 ASSERT(npgs == 0); 5048 page_list_add_pages(*target, 0); 5049 npgs = page_get_pagecnt(szc); 5050 page_create_putback(npgs); 5051 } 5052 return (ret); 5053 } 5054 5055 /* 5056 * it is up to the caller to deal with pcf accounting. 5057 */ 5058 void 5059 page_free_replacement_page(page_t *pplist) 5060 { 5061 page_t *pp; 5062 5063 while (pplist != NULL) { 5064 /* 5065 * pp_targ is a linked list. 5066 */ 5067 pp = pplist; 5068 if (pp->p_szc == 0) { 5069 page_sub(&pplist, pp); 5070 page_clr_all_props(pp); 5071 PP_SETFREE(pp); 5072 PP_SETAGED(pp); 5073 page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 5074 page_unlock(pp); 5075 VM_STAT_ADD(pagecnt.pc_free_replacement_page[0]); 5076 } else { 5077 spgcnt_t curnpgs = page_get_pagecnt(pp->p_szc); 5078 page_t *tpp; 5079 page_list_break(&pp, &pplist, curnpgs); 5080 tpp = pp; 5081 do { 5082 ASSERT(PAGE_EXCL(tpp)); 5083 ASSERT(!hat_page_is_mapped(tpp)); 5084 page_clr_all_props(pp); 5085 PP_SETFREE(tpp); 5086 PP_SETAGED(tpp); 5087 } while ((tpp = tpp->p_next) != pp); 5088 page_list_add_pages(pp, 0); 5089 VM_STAT_ADD(pagecnt.pc_free_replacement_page[1]); 5090 } 5091 } 5092 } 5093 5094 /* 5095 * Relocate target to non-relocatable replacement page. 5096 */ 5097 int 5098 page_relocate_cage(page_t **target, page_t **replacement) 5099 { 5100 page_t *tpp, *rpp; 5101 spgcnt_t pgcnt, npgs; 5102 int result; 5103 5104 tpp = *target; 5105 5106 ASSERT(PAGE_EXCL(tpp)); 5107 ASSERT(tpp->p_szc == 0); 5108 5109 pgcnt = btop(page_get_pagesize(tpp->p_szc)); 5110 5111 do { 5112 (void) page_create_wait(pgcnt, PG_WAIT | PG_NORELOC); 5113 rpp = page_get_replacement_page(tpp, NULL, PGR_NORELOC); 5114 if (rpp == NULL) { 5115 page_create_putback(pgcnt); 5116 kcage_cageout_wakeup(); 5117 } 5118 } while (rpp == NULL); 5119 5120 ASSERT(PP_ISNORELOC(rpp)); 5121 5122 result = page_relocate(&tpp, &rpp, 0, 1, &npgs, NULL); 5123 5124 if (result == 0) { 5125 *replacement = rpp; 5126 if (pgcnt != npgs) 5127 panic("page_relocate_cage: partial relocation"); 5128 } 5129 5130 return (result); 5131 } 5132 5133 /* 5134 * Release the page lock on a page, place on cachelist 5135 * tail if no longer mapped. Caller can let us know if 5136 * the page is known to be clean. 5137 */ 5138 int 5139 page_release(page_t *pp, int checkmod) 5140 { 5141 int status; 5142 5143 ASSERT(PAGE_LOCKED(pp) && !PP_ISFREE(pp) && 5144 (pp->p_vnode != NULL)); 5145 5146 if (!hat_page_is_mapped(pp) && !IS_SWAPVP(pp->p_vnode) && 5147 ((PAGE_SHARED(pp) && page_tryupgrade(pp)) || PAGE_EXCL(pp)) && 5148 pp->p_lckcnt == 0 && pp->p_cowcnt == 0 && 5149 !hat_page_is_mapped(pp)) { 5150 5151 /* 5152 * If page is modified, unlock it 5153 * 5154 * (p_nrm & P_MOD) bit has the latest stuff because: 5155 * (1) We found that this page doesn't have any mappings 5156 * _after_ holding SE_EXCL and 5157 * (2) We didn't drop SE_EXCL lock after the check in (1) 5158 */ 5159 if (checkmod && hat_ismod(pp)) { 5160 page_unlock(pp); 5161 status = PGREL_MOD; 5162 } else { 5163 /*LINTED: constant in conditional context*/ 5164 VN_DISPOSE(pp, B_FREE, 0, kcred); 5165 status = PGREL_CLEAN; 5166 } 5167 } else { 5168 page_unlock(pp); 5169 status = PGREL_NOTREL; 5170 } 5171 return (status); 5172 } 5173 5174 /* 5175 * Given a constituent page, try to demote the large page on the freelist. 5176 * 5177 * Returns nonzero if the page could be demoted successfully. Returns with 5178 * the constituent page still locked. 5179 */ 5180 int 5181 page_try_demote_free_pages(page_t *pp) 5182 { 5183 page_t *rootpp = pp; 5184 pfn_t pfn = page_pptonum(pp); 5185 spgcnt_t npgs; 5186 uint_t szc = pp->p_szc; 5187 5188 ASSERT(PP_ISFREE(pp)); 5189 ASSERT(PAGE_EXCL(pp)); 5190 5191 /* 5192 * Adjust rootpp and lock it, if `pp' is not the base 5193 * constituent page. 5194 */ 5195 npgs = page_get_pagecnt(pp->p_szc); 5196 if (npgs == 1) { 5197 return (0); 5198 } 5199 5200 if (!IS_P2ALIGNED(pfn, npgs)) { 5201 pfn = P2ALIGN(pfn, npgs); 5202 rootpp = page_numtopp_nolock(pfn); 5203 } 5204 5205 if (pp != rootpp && !page_trylock(rootpp, SE_EXCL)) { 5206 return (0); 5207 } 5208 5209 if (rootpp->p_szc != szc) { 5210 if (pp != rootpp) 5211 page_unlock(rootpp); 5212 return (0); 5213 } 5214 5215 page_demote_free_pages(rootpp); 5216 5217 if (pp != rootpp) 5218 page_unlock(rootpp); 5219 5220 ASSERT(PP_ISFREE(pp)); 5221 ASSERT(PAGE_EXCL(pp)); 5222 return (1); 5223 } 5224 5225 /* 5226 * Given a constituent page, try to demote the large page. 5227 * 5228 * Returns nonzero if the page could be demoted successfully. Returns with 5229 * the constituent page still locked. 5230 */ 5231 int 5232 page_try_demote_pages(page_t *pp) 5233 { 5234 page_t *tpp, *rootpp = pp; 5235 pfn_t pfn = page_pptonum(pp); 5236 spgcnt_t i, npgs; 5237 uint_t szc = pp->p_szc; 5238 vnode_t *vp = pp->p_vnode; 5239 5240 ASSERT(PAGE_EXCL(pp)); 5241 5242 VM_STAT_ADD(pagecnt.pc_try_demote_pages[0]); 5243 5244 if (pp->p_szc == 0) { 5245 VM_STAT_ADD(pagecnt.pc_try_demote_pages[1]); 5246 return (1); 5247 } 5248 5249 if (vp != NULL && !IS_SWAPFSVP(vp) && !VN_ISKAS(vp)) { 5250 VM_STAT_ADD(pagecnt.pc_try_demote_pages[2]); 5251 page_demote_vp_pages(pp); 5252 ASSERT(pp->p_szc == 0); 5253 return (1); 5254 } 5255 5256 /* 5257 * Adjust rootpp if passed in is not the base 5258 * constituent page. 5259 */ 5260 npgs = page_get_pagecnt(pp->p_szc); 5261 ASSERT(npgs > 1); 5262 if (!IS_P2ALIGNED(pfn, npgs)) { 5263 pfn = P2ALIGN(pfn, npgs); 5264 rootpp = page_numtopp_nolock(pfn); 5265 VM_STAT_ADD(pagecnt.pc_try_demote_pages[3]); 5266 ASSERT(rootpp->p_vnode != NULL); 5267 ASSERT(rootpp->p_szc == szc); 5268 } 5269 5270 /* 5271 * We can't demote kernel pages since we can't hat_unload() 5272 * the mappings. 5273 */ 5274 if (VN_ISKAS(rootpp->p_vnode)) 5275 return (0); 5276 5277 /* 5278 * Attempt to lock all constituent pages except the page passed 5279 * in since it's already locked. 5280 */ 5281 for (tpp = rootpp, i = 0; i < npgs; i++, tpp++) { 5282 ASSERT(!PP_ISFREE(tpp)); 5283 ASSERT(tpp->p_vnode != NULL); 5284 5285 if (tpp != pp && !page_trylock(tpp, SE_EXCL)) 5286 break; 5287 ASSERT(tpp->p_szc == rootpp->p_szc); 5288 ASSERT(page_pptonum(tpp) == page_pptonum(rootpp) + i); 5289 } 5290 5291 /* 5292 * If we failed to lock them all then unlock what we have 5293 * locked so far and bail. 5294 */ 5295 if (i < npgs) { 5296 tpp = rootpp; 5297 while (i-- > 0) { 5298 if (tpp != pp) 5299 page_unlock(tpp); 5300 tpp++; 5301 } 5302 VM_STAT_ADD(pagecnt.pc_try_demote_pages[4]); 5303 return (0); 5304 } 5305 5306 for (tpp = rootpp, i = 0; i < npgs; i++, tpp++) { 5307 ASSERT(PAGE_EXCL(tpp)); 5308 ASSERT(tpp->p_slckcnt == 0); 5309 (void) hat_pageunload(tpp, HAT_FORCE_PGUNLOAD); 5310 tpp->p_szc = 0; 5311 } 5312 5313 /* 5314 * Unlock all pages except the page passed in. 5315 */ 5316 for (tpp = rootpp, i = 0; i < npgs; i++, tpp++) { 5317 ASSERT(!hat_page_is_mapped(tpp)); 5318 if (tpp != pp) 5319 page_unlock(tpp); 5320 } 5321 5322 VM_STAT_ADD(pagecnt.pc_try_demote_pages[5]); 5323 return (1); 5324 } 5325 5326 /* 5327 * Called by page_free() and page_destroy() to demote the page size code 5328 * (p_szc) to 0 (since we can't just put a single PAGESIZE page with non zero 5329 * p_szc on free list, neither can we just clear p_szc of a single page_t 5330 * within a large page since it will break other code that relies on p_szc 5331 * being the same for all page_t's of a large page). Anonymous pages should 5332 * never end up here because anon_map_getpages() cannot deal with p_szc 5333 * changes after a single constituent page is locked. While anonymous or 5334 * kernel large pages are demoted or freed the entire large page at a time 5335 * with all constituent pages locked EXCL for the file system pages we 5336 * have to be able to demote a large page (i.e. decrease all constituent pages 5337 * p_szc) with only just an EXCL lock on one of constituent pages. The reason 5338 * we can easily deal with anonymous page demotion the entire large page at a 5339 * time is that those operation originate at address space level and concern 5340 * the entire large page region with actual demotion only done when pages are 5341 * not shared with any other processes (therefore we can always get EXCL lock 5342 * on all anonymous constituent pages after clearing segment page 5343 * cache). However file system pages can be truncated or invalidated at a 5344 * PAGESIZE level from the file system side and end up in page_free() or 5345 * page_destroy() (we also allow only part of the large page to be SOFTLOCKed 5346 * and therfore pageout should be able to demote a large page by EXCL locking 5347 * any constituent page that is not under SOFTLOCK). In those cases we cannot 5348 * rely on being able to lock EXCL all constituent pages. 5349 * 5350 * To prevent szc changes on file system pages one has to lock all constituent 5351 * pages at least SHARED (or call page_szc_lock()). The only subsystem that 5352 * doesn't rely on locking all constituent pages (or using page_szc_lock()) to 5353 * prevent szc changes is hat layer that uses its own page level mlist 5354 * locks. hat assumes that szc doesn't change after mlist lock for a page is 5355 * taken. Therefore we need to change szc under hat level locks if we only 5356 * have an EXCL lock on a single constituent page and hat still references any 5357 * of constituent pages. (Note we can't "ignore" hat layer by simply 5358 * hat_pageunload() all constituent pages without having EXCL locks on all of 5359 * constituent pages). We use hat_page_demote() call to safely demote szc of 5360 * all constituent pages under hat locks when we only have an EXCL lock on one 5361 * of constituent pages. 5362 * 5363 * This routine calls page_szc_lock() before calling hat_page_demote() to 5364 * allow segvn in one special case not to lock all constituent pages SHARED 5365 * before calling hat_memload_array() that relies on p_szc not changeing even 5366 * before hat level mlist lock is taken. In that case segvn uses 5367 * page_szc_lock() to prevent hat_page_demote() changeing p_szc values. 5368 * 5369 * Anonymous or kernel page demotion still has to lock all pages exclusively 5370 * and do hat_pageunload() on all constituent pages before demoting the page 5371 * therefore there's no need for anonymous or kernel page demotion to use 5372 * hat_page_demote() mechanism. 5373 * 5374 * hat_page_demote() removes all large mappings that map pp and then decreases 5375 * p_szc starting from the last constituent page of the large page. By working 5376 * from the tail of a large page in pfn decreasing order allows one looking at 5377 * the root page to know that hat_page_demote() is done for root's szc area. 5378 * e.g. if a root page has szc 1 one knows it only has to lock all constituent 5379 * pages within szc 1 area to prevent szc changes because hat_page_demote() 5380 * that started on this page when it had szc > 1 is done for this szc 1 area. 5381 * 5382 * We are guranteed that all constituent pages of pp's large page belong to 5383 * the same vnode with the consecutive offsets increasing in the direction of 5384 * the pfn i.e. the identity of constituent pages can't change until their 5385 * p_szc is decreased. Therefore it's safe for hat_page_demote() to remove 5386 * large mappings to pp even though we don't lock any constituent page except 5387 * pp (i.e. we won't unload e.g. kernel locked page). 5388 */ 5389 static void 5390 page_demote_vp_pages(page_t *pp) 5391 { 5392 kmutex_t *mtx; 5393 5394 ASSERT(PAGE_EXCL(pp)); 5395 ASSERT(!PP_ISFREE(pp)); 5396 ASSERT(pp->p_vnode != NULL); 5397 ASSERT(!IS_SWAPFSVP(pp->p_vnode)); 5398 ASSERT(!PP_ISKAS(pp)); 5399 5400 VM_STAT_ADD(pagecnt.pc_demote_pages[0]); 5401 5402 mtx = page_szc_lock(pp); 5403 if (mtx != NULL) { 5404 hat_page_demote(pp); 5405 mutex_exit(mtx); 5406 } 5407 ASSERT(pp->p_szc == 0); 5408 } 5409 5410 /* 5411 * Mark any existing pages for migration in the given range 5412 */ 5413 void 5414 page_mark_migrate(struct seg *seg, caddr_t addr, size_t len, 5415 struct anon_map *amp, ulong_t anon_index, vnode_t *vp, 5416 u_offset_t vnoff, int rflag) 5417 { 5418 struct anon *ap; 5419 vnode_t *curvp; 5420 lgrp_t *from; 5421 pgcnt_t i; 5422 pgcnt_t nlocked; 5423 u_offset_t off; 5424 pfn_t pfn; 5425 size_t pgsz; 5426 size_t segpgsz; 5427 pgcnt_t pages; 5428 uint_t pszc; 5429 page_t **ppa; 5430 pgcnt_t ppa_nentries; 5431 page_t *pp; 5432 caddr_t va; 5433 ulong_t an_idx; 5434 anon_sync_obj_t cookie; 5435 5436 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 5437 5438 /* 5439 * Don't do anything if don't need to do lgroup optimizations 5440 * on this system 5441 */ 5442 if (!lgrp_optimizations()) 5443 return; 5444 5445 /* 5446 * Align address and length to (potentially large) page boundary 5447 */ 5448 segpgsz = page_get_pagesize(seg->s_szc); 5449 addr = (caddr_t)P2ALIGN((uintptr_t)addr, segpgsz); 5450 if (rflag) 5451 len = P2ROUNDUP(len, segpgsz); 5452 5453 /* 5454 * Allocate page array to accomodate largest page size 5455 */ 5456 pgsz = page_get_pagesize(page_num_pagesizes() - 1); 5457 ppa_nentries = btop(pgsz); 5458 ppa = kmem_zalloc(ppa_nentries * sizeof (page_t *), KM_SLEEP); 5459 5460 /* 5461 * Do one (large) page at a time 5462 */ 5463 va = addr; 5464 while (va < addr + len) { 5465 /* 5466 * Lookup (root) page for vnode and offset corresponding to 5467 * this virtual address 5468 * Try anonmap first since there may be copy-on-write 5469 * pages, but initialize vnode pointer and offset using 5470 * vnode arguments just in case there isn't an amp. 5471 */ 5472 curvp = vp; 5473 off = vnoff + va - seg->s_base; 5474 if (amp) { 5475 ANON_LOCK_ENTER(&->a_rwlock, RW_READER); 5476 an_idx = anon_index + seg_page(seg, va); 5477 anon_array_enter(amp, an_idx, &cookie); 5478 ap = anon_get_ptr(amp->ahp, an_idx); 5479 if (ap) 5480 swap_xlate(ap, &curvp, &off); 5481 anon_array_exit(&cookie); 5482 ANON_LOCK_EXIT(&->a_rwlock); 5483 } 5484 5485 pp = NULL; 5486 if (curvp) 5487 pp = page_lookup(curvp, off, SE_SHARED); 5488 5489 /* 5490 * If there isn't a page at this virtual address, 5491 * skip to next page 5492 */ 5493 if (pp == NULL) { 5494 va += PAGESIZE; 5495 continue; 5496 } 5497 5498 /* 5499 * Figure out which lgroup this page is in for kstats 5500 */ 5501 pfn = page_pptonum(pp); 5502 from = lgrp_pfn_to_lgrp(pfn); 5503 5504 /* 5505 * Get page size, and round up and skip to next page boundary 5506 * if unaligned address 5507 */ 5508 pszc = pp->p_szc; 5509 pgsz = page_get_pagesize(pszc); 5510 pages = btop(pgsz); 5511 if (!IS_P2ALIGNED(va, pgsz) || 5512 !IS_P2ALIGNED(pfn, pages) || 5513 pgsz > segpgsz) { 5514 pgsz = MIN(pgsz, segpgsz); 5515 page_unlock(pp); 5516 i = btop(P2END((uintptr_t)va, pgsz) - 5517 (uintptr_t)va); 5518 va = (caddr_t)P2END((uintptr_t)va, pgsz); 5519 lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, i); 5520 continue; 5521 } 5522 5523 /* 5524 * Upgrade to exclusive lock on page 5525 */ 5526 if (!page_tryupgrade(pp)) { 5527 page_unlock(pp); 5528 va += pgsz; 5529 lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, 5530 btop(pgsz)); 5531 continue; 5532 } 5533 5534 /* 5535 * Remember pages locked exclusively and how many 5536 */ 5537 ppa[0] = pp; 5538 nlocked = 1; 5539 5540 /* 5541 * Lock constituent pages if this is large page 5542 */ 5543 if (pages > 1) { 5544 /* 5545 * Lock all constituents except root page, since it 5546 * should be locked already. 5547 */ 5548 for (i = 1; i < pages; i++) { 5549 pp++; 5550 if (!page_trylock(pp, SE_EXCL)) { 5551 break; 5552 } 5553 if (PP_ISFREE(pp) || 5554 pp->p_szc != pszc) { 5555 /* 5556 * hat_page_demote() raced in with us. 5557 */ 5558 ASSERT(!IS_SWAPFSVP(curvp)); 5559 page_unlock(pp); 5560 break; 5561 } 5562 ppa[nlocked] = pp; 5563 nlocked++; 5564 } 5565 } 5566 5567 /* 5568 * If all constituent pages couldn't be locked, 5569 * unlock pages locked so far and skip to next page. 5570 */ 5571 if (nlocked != pages) { 5572 for (i = 0; i < nlocked; i++) 5573 page_unlock(ppa[i]); 5574 va += pgsz; 5575 lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, 5576 btop(pgsz)); 5577 continue; 5578 } 5579 5580 /* 5581 * hat_page_demote() can no longer happen 5582 * since last cons page had the right p_szc after 5583 * all cons pages were locked. all cons pages 5584 * should now have the same p_szc. 5585 */ 5586 5587 /* 5588 * All constituent pages locked successfully, so mark 5589 * large page for migration and unload the mappings of 5590 * constituent pages, so a fault will occur on any part of the 5591 * large page 5592 */ 5593 PP_SETMIGRATE(ppa[0]); 5594 for (i = 0; i < nlocked; i++) { 5595 pp = ppa[i]; 5596 (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 5597 ASSERT(hat_page_getshare(pp) == 0); 5598 page_unlock(pp); 5599 } 5600 lgrp_stat_add(from->lgrp_id, LGRP_PMM_PGS, nlocked); 5601 5602 va += pgsz; 5603 } 5604 kmem_free(ppa, ppa_nentries * sizeof (page_t *)); 5605 } 5606 5607 /* 5608 * Migrate any pages that have been marked for migration in the given range 5609 */ 5610 void 5611 page_migrate( 5612 struct seg *seg, 5613 caddr_t addr, 5614 page_t **ppa, 5615 pgcnt_t npages) 5616 { 5617 lgrp_t *from; 5618 lgrp_t *to; 5619 page_t *newpp; 5620 page_t *pp; 5621 pfn_t pfn; 5622 size_t pgsz; 5623 spgcnt_t page_cnt; 5624 spgcnt_t i; 5625 uint_t pszc; 5626 5627 ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 5628 5629 while (npages > 0) { 5630 pp = *ppa; 5631 pszc = pp->p_szc; 5632 pgsz = page_get_pagesize(pszc); 5633 page_cnt = btop(pgsz); 5634 5635 /* 5636 * Check to see whether this page is marked for migration 5637 * 5638 * Assume that root page of large page is marked for 5639 * migration and none of the other constituent pages 5640 * are marked. This really simplifies clearing the 5641 * migrate bit by not having to clear it from each 5642 * constituent page. 5643 * 5644 * note we don't want to relocate an entire large page if 5645 * someone is only using one subpage. 5646 */ 5647 if (npages < page_cnt) 5648 break; 5649 5650 /* 5651 * Is it marked for migration? 5652 */ 5653 if (!PP_ISMIGRATE(pp)) 5654 goto next; 5655 5656 /* 5657 * Determine lgroups that page is being migrated between 5658 */ 5659 pfn = page_pptonum(pp); 5660 if (!IS_P2ALIGNED(pfn, page_cnt)) { 5661 break; 5662 } 5663 from = lgrp_pfn_to_lgrp(pfn); 5664 to = lgrp_mem_choose(seg, addr, pgsz); 5665 5666 /* 5667 * Check to see whether we are trying to migrate page to lgroup 5668 * where it is allocated already 5669 */ 5670 if (to == from) { 5671 PP_CLRMIGRATE(pp); 5672 goto next; 5673 } 5674 5675 /* 5676 * Need to get exclusive lock's to migrate 5677 */ 5678 for (i = 0; i < page_cnt; i++) { 5679 ASSERT(PAGE_LOCKED(ppa[i])); 5680 if (page_pptonum(ppa[i]) != pfn + i || 5681 ppa[i]->p_szc != pszc) { 5682 break; 5683 } 5684 if (!page_tryupgrade(ppa[i])) { 5685 lgrp_stat_add(from->lgrp_id, 5686 LGRP_PM_FAIL_LOCK_PGS, 5687 page_cnt); 5688 break; 5689 } 5690 } 5691 if (i != page_cnt) { 5692 while (--i != -1) { 5693 page_downgrade(ppa[i]); 5694 } 5695 goto next; 5696 } 5697 5698 (void) page_create_wait(page_cnt, PG_WAIT); 5699 newpp = page_get_replacement_page(pp, to, PGR_SAMESZC); 5700 if (newpp == NULL) { 5701 page_create_putback(page_cnt); 5702 for (i = 0; i < page_cnt; i++) { 5703 page_downgrade(ppa[i]); 5704 } 5705 lgrp_stat_add(to->lgrp_id, LGRP_PM_FAIL_ALLOC_PGS, 5706 page_cnt); 5707 goto next; 5708 } 5709 ASSERT(newpp->p_szc == pszc); 5710 /* 5711 * Clear migrate bit and relocate page 5712 */ 5713 PP_CLRMIGRATE(pp); 5714 if (page_relocate(&pp, &newpp, 0, 1, &page_cnt, to)) { 5715 panic("page_migrate: page_relocate failed"); 5716 } 5717 ASSERT(page_cnt * PAGESIZE == pgsz); 5718 5719 /* 5720 * Keep stats for number of pages migrated from and to 5721 * each lgroup 5722 */ 5723 lgrp_stat_add(from->lgrp_id, LGRP_PM_SRC_PGS, page_cnt); 5724 lgrp_stat_add(to->lgrp_id, LGRP_PM_DEST_PGS, page_cnt); 5725 /* 5726 * update the page_t array we were passed in and 5727 * unlink constituent pages of a large page. 5728 */ 5729 for (i = 0; i < page_cnt; ++i, ++pp) { 5730 ASSERT(PAGE_EXCL(newpp)); 5731 ASSERT(newpp->p_szc == pszc); 5732 ppa[i] = newpp; 5733 pp = newpp; 5734 page_sub(&newpp, pp); 5735 page_downgrade(pp); 5736 } 5737 ASSERT(newpp == NULL); 5738 next: 5739 addr += pgsz; 5740 ppa += page_cnt; 5741 npages -= page_cnt; 5742 } 5743 } 5744 5745 ulong_t mem_waiters = 0; 5746 ulong_t max_count = 20; 5747 #define MAX_DELAY 0x1ff 5748 5749 /* 5750 * Check if enough memory is available to proceed. 5751 * Depending on system configuration and how much memory is 5752 * reserved for swap we need to check against two variables. 5753 * e.g. on systems with little physical swap availrmem can be 5754 * more reliable indicator of how much memory is available. 5755 * On systems with large phys swap freemem can be better indicator. 5756 * If freemem drops below threshold level don't return an error 5757 * immediately but wake up pageout to free memory and block. 5758 * This is done number of times. If pageout is not able to free 5759 * memory within certain time return an error. 5760 * The same applies for availrmem but kmem_reap is used to 5761 * free memory. 5762 */ 5763 int 5764 page_mem_avail(pgcnt_t npages) 5765 { 5766 ulong_t count; 5767 5768 #if defined(__i386) 5769 if (freemem > desfree + npages && 5770 availrmem > swapfs_reserve + npages && 5771 btop(vmem_size(heap_arena, VMEM_FREE)) > tune.t_minarmem + 5772 npages) 5773 return (1); 5774 #else 5775 if (freemem > desfree + npages && 5776 availrmem > swapfs_reserve + npages) 5777 return (1); 5778 #endif 5779 5780 count = max_count; 5781 atomic_add_long(&mem_waiters, 1); 5782 5783 while (freemem < desfree + npages && --count) { 5784 cv_signal(&proc_pageout->p_cv); 5785 if (delay_sig(hz + (mem_waiters & MAX_DELAY))) { 5786 atomic_add_long(&mem_waiters, -1); 5787 return (0); 5788 } 5789 } 5790 if (count == 0) { 5791 atomic_add_long(&mem_waiters, -1); 5792 return (0); 5793 } 5794 5795 count = max_count; 5796 while (availrmem < swapfs_reserve + npages && --count) { 5797 kmem_reap(); 5798 if (delay_sig(hz + (mem_waiters & MAX_DELAY))) { 5799 atomic_add_long(&mem_waiters, -1); 5800 return (0); 5801 } 5802 } 5803 atomic_add_long(&mem_waiters, -1); 5804 if (count == 0) 5805 return (0); 5806 5807 #if defined(__i386) 5808 if (btop(vmem_size(heap_arena, VMEM_FREE)) < 5809 tune.t_minarmem + npages) 5810 return (0); 5811 #endif 5812 return (1); 5813 } 5814 5815 #define MAX_CNT 60 /* max num of iterations */ 5816 /* 5817 * Reclaim/reserve availrmem for npages. 5818 * If there is not enough memory start reaping seg, kmem caches. 5819 * Start pageout scanner (via page_needfree()). 5820 * Exit after ~ MAX_CNT s regardless of how much memory has been released. 5821 * Note: There is no guarantee that any availrmem will be freed as 5822 * this memory typically is locked (kernel heap) or reserved for swap. 5823 * Also due to memory fragmentation kmem allocator may not be able 5824 * to free any memory (single user allocated buffer will prevent 5825 * freeing slab or a page). 5826 */ 5827 int 5828 page_reclaim_mem(pgcnt_t npages, pgcnt_t epages, int adjust) 5829 { 5830 int i = 0; 5831 int ret = 0; 5832 pgcnt_t deficit; 5833 pgcnt_t old_availrmem; 5834 5835 mutex_enter(&freemem_lock); 5836 old_availrmem = availrmem - 1; 5837 while ((availrmem < tune.t_minarmem + npages + epages) && 5838 (old_availrmem < availrmem) && (i++ < MAX_CNT)) { 5839 old_availrmem = availrmem; 5840 deficit = tune.t_minarmem + npages + epages - availrmem; 5841 mutex_exit(&freemem_lock); 5842 page_needfree(deficit); 5843 seg_preap(); 5844 kmem_reap(); 5845 delay(hz); 5846 page_needfree(-(spgcnt_t)deficit); 5847 mutex_enter(&freemem_lock); 5848 } 5849 5850 if (adjust && (availrmem >= tune.t_minarmem + npages + epages)) { 5851 availrmem -= npages; 5852 ret = 1; 5853 } 5854 5855 mutex_exit(&freemem_lock); 5856 5857 return (ret); 5858 } 5859 5860 /* 5861 * Search the memory segments to locate the desired page. Within a 5862 * segment, pages increase linearly with one page structure per 5863 * physical page frame (size PAGESIZE). The search begins 5864 * with the segment that was accessed last, to take advantage of locality. 5865 * If the hint misses, we start from the beginning of the sorted memseg list 5866 */ 5867 5868 5869 /* 5870 * Some data structures for pfn to pp lookup. 5871 */ 5872 ulong_t mhash_per_slot; 5873 struct memseg *memseg_hash[N_MEM_SLOTS]; 5874 5875 page_t * 5876 page_numtopp_nolock(pfn_t pfnum) 5877 { 5878 struct memseg *seg; 5879 page_t *pp; 5880 vm_cpu_data_t *vc = CPU->cpu_vm_data; 5881 5882 ASSERT(vc != NULL); 5883 5884 MEMSEG_STAT_INCR(nsearch); 5885 5886 /* Try last winner first */ 5887 if (((seg = vc->vc_pnum_memseg) != NULL) && 5888 (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 5889 MEMSEG_STAT_INCR(nlastwon); 5890 pp = seg->pages + (pfnum - seg->pages_base); 5891 if (pp->p_pagenum == pfnum) 5892 return ((page_t *)pp); 5893 } 5894 5895 /* Else Try hash */ 5896 if (((seg = memseg_hash[MEMSEG_PFN_HASH(pfnum)]) != NULL) && 5897 (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 5898 MEMSEG_STAT_INCR(nhashwon); 5899 vc->vc_pnum_memseg = seg; 5900 pp = seg->pages + (pfnum - seg->pages_base); 5901 if (pp->p_pagenum == pfnum) 5902 return ((page_t *)pp); 5903 } 5904 5905 /* Else Brute force */ 5906 for (seg = memsegs; seg != NULL; seg = seg->next) { 5907 if (pfnum >= seg->pages_base && pfnum < seg->pages_end) { 5908 vc->vc_pnum_memseg = seg; 5909 pp = seg->pages + (pfnum - seg->pages_base); 5910 return ((page_t *)pp); 5911 } 5912 } 5913 vc->vc_pnum_memseg = NULL; 5914 MEMSEG_STAT_INCR(nnotfound); 5915 return ((page_t *)NULL); 5916 5917 } 5918 5919 struct memseg * 5920 page_numtomemseg_nolock(pfn_t pfnum) 5921 { 5922 struct memseg *seg; 5923 page_t *pp; 5924 5925 /* Try hash */ 5926 if (((seg = memseg_hash[MEMSEG_PFN_HASH(pfnum)]) != NULL) && 5927 (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 5928 pp = seg->pages + (pfnum - seg->pages_base); 5929 if (pp->p_pagenum == pfnum) 5930 return (seg); 5931 } 5932 5933 /* Else Brute force */ 5934 for (seg = memsegs; seg != NULL; seg = seg->next) { 5935 if (pfnum >= seg->pages_base && pfnum < seg->pages_end) { 5936 return (seg); 5937 } 5938 } 5939 return ((struct memseg *)NULL); 5940 } 5941 5942 /* 5943 * Given a page and a count return the page struct that is 5944 * n structs away from the current one in the global page 5945 * list. 5946 * 5947 * This function wraps to the first page upon 5948 * reaching the end of the memseg list. 5949 */ 5950 page_t * 5951 page_nextn(page_t *pp, ulong_t n) 5952 { 5953 struct memseg *seg; 5954 page_t *ppn; 5955 vm_cpu_data_t *vc = (vm_cpu_data_t *)CPU->cpu_vm_data; 5956 5957 ASSERT(vc != NULL); 5958 5959 if (((seg = vc->vc_pnext_memseg) == NULL) || 5960 (seg->pages_base == seg->pages_end) || 5961 !(pp >= seg->pages && pp < seg->epages)) { 5962 5963 for (seg = memsegs; seg; seg = seg->next) { 5964 if (pp >= seg->pages && pp < seg->epages) 5965 break; 5966 } 5967 5968 if (seg == NULL) { 5969 /* Memory delete got in, return something valid. */ 5970 /* TODO: fix me. */ 5971 seg = memsegs; 5972 pp = seg->pages; 5973 } 5974 } 5975 5976 /* check for wraparound - possible if n is large */ 5977 while ((ppn = (pp + n)) >= seg->epages || ppn < pp) { 5978 n -= seg->epages - pp; 5979 seg = seg->next; 5980 if (seg == NULL) 5981 seg = memsegs; 5982 pp = seg->pages; 5983 } 5984 vc->vc_pnext_memseg = seg; 5985 return (ppn); 5986 } 5987 5988 /* 5989 * Initialize for a loop using page_next_scan_large(). 5990 */ 5991 page_t * 5992 page_next_scan_init(void **cookie) 5993 { 5994 ASSERT(cookie != NULL); 5995 *cookie = (void *)memsegs; 5996 return ((page_t *)memsegs->pages); 5997 } 5998 5999 /* 6000 * Return the next page in a scan of page_t's, assuming we want 6001 * to skip over sub-pages within larger page sizes. 6002 * 6003 * The cookie is used to keep track of the current memseg. 6004 */ 6005 page_t * 6006 page_next_scan_large( 6007 page_t *pp, 6008 ulong_t *n, 6009 void **cookie) 6010 { 6011 struct memseg *seg = (struct memseg *)*cookie; 6012 page_t *new_pp; 6013 ulong_t cnt; 6014 pfn_t pfn; 6015 6016 6017 /* 6018 * get the count of page_t's to skip based on the page size 6019 */ 6020 ASSERT(pp != NULL); 6021 if (pp->p_szc == 0) { 6022 cnt = 1; 6023 } else { 6024 pfn = page_pptonum(pp); 6025 cnt = page_get_pagecnt(pp->p_szc); 6026 cnt -= pfn & (cnt - 1); 6027 } 6028 *n += cnt; 6029 new_pp = pp + cnt; 6030 6031 /* 6032 * Catch if we went past the end of the current memory segment. If so, 6033 * just move to the next segment with pages. 6034 */ 6035 if (new_pp >= seg->epages) { 6036 do { 6037 seg = seg->next; 6038 if (seg == NULL) 6039 seg = memsegs; 6040 } while (seg->pages == seg->epages); 6041 new_pp = seg->pages; 6042 *cookie = (void *)seg; 6043 } 6044 6045 return (new_pp); 6046 } 6047 6048 6049 /* 6050 * Returns next page in list. Note: this function wraps 6051 * to the first page in the list upon reaching the end 6052 * of the list. Callers should be aware of this fact. 6053 */ 6054 6055 /* We should change this be a #define */ 6056 6057 page_t * 6058 page_next(page_t *pp) 6059 { 6060 return (page_nextn(pp, 1)); 6061 } 6062 6063 page_t * 6064 page_first() 6065 { 6066 return ((page_t *)memsegs->pages); 6067 } 6068 6069 6070 /* 6071 * This routine is called at boot with the initial memory configuration 6072 * and when memory is added or removed. 6073 */ 6074 void 6075 build_pfn_hash() 6076 { 6077 pfn_t cur; 6078 pgcnt_t index; 6079 struct memseg *pseg; 6080 int i; 6081 6082 /* 6083 * Clear memseg_hash array. 6084 * Since memory add/delete is designed to operate concurrently 6085 * with normal operation, the hash rebuild must be able to run 6086 * concurrently with page_numtopp_nolock(). To support this 6087 * functionality, assignments to memseg_hash array members must 6088 * be done atomically. 6089 * 6090 * NOTE: bzero() does not currently guarantee this for kernel 6091 * threads, and cannot be used here. 6092 */ 6093 for (i = 0; i < N_MEM_SLOTS; i++) 6094 memseg_hash[i] = NULL; 6095 6096 hat_kpm_mseghash_clear(N_MEM_SLOTS); 6097 6098 /* 6099 * Physmax is the last valid pfn. 6100 */ 6101 mhash_per_slot = (physmax + 1) >> MEM_HASH_SHIFT; 6102 for (pseg = memsegs; pseg != NULL; pseg = pseg->next) { 6103 index = MEMSEG_PFN_HASH(pseg->pages_base); 6104 cur = pseg->pages_base; 6105 do { 6106 if (index >= N_MEM_SLOTS) 6107 index = MEMSEG_PFN_HASH(cur); 6108 6109 if (memseg_hash[index] == NULL || 6110 memseg_hash[index]->pages_base > pseg->pages_base) { 6111 memseg_hash[index] = pseg; 6112 hat_kpm_mseghash_update(index, pseg); 6113 } 6114 cur += mhash_per_slot; 6115 index++; 6116 } while (cur < pseg->pages_end); 6117 } 6118 } 6119 6120 /* 6121 * Return the pagenum for the pp 6122 */ 6123 pfn_t 6124 page_pptonum(page_t *pp) 6125 { 6126 return (pp->p_pagenum); 6127 } 6128 6129 /* 6130 * interface to the referenced and modified etc bits 6131 * in the PSM part of the page struct 6132 * when no locking is desired. 6133 */ 6134 void 6135 page_set_props(page_t *pp, uint_t flags) 6136 { 6137 ASSERT((flags & ~(P_MOD | P_REF | P_RO)) == 0); 6138 pp->p_nrm |= (uchar_t)flags; 6139 } 6140 6141 void 6142 page_clr_all_props(page_t *pp) 6143 { 6144 pp->p_nrm = 0; 6145 } 6146 6147 /* 6148 * Clear p_lckcnt and p_cowcnt, adjusting freemem if required. 6149 */ 6150 int 6151 page_clear_lck_cow(page_t *pp, int adjust) 6152 { 6153 int f_amount; 6154 6155 ASSERT(PAGE_EXCL(pp)); 6156 6157 /* 6158 * The page_struct_lock need not be acquired here since 6159 * we require the caller hold the page exclusively locked. 6160 */ 6161 f_amount = 0; 6162 if (pp->p_lckcnt) { 6163 f_amount = 1; 6164 pp->p_lckcnt = 0; 6165 } 6166 if (pp->p_cowcnt) { 6167 f_amount += pp->p_cowcnt; 6168 pp->p_cowcnt = 0; 6169 } 6170 6171 if (adjust && f_amount) { 6172 mutex_enter(&freemem_lock); 6173 availrmem += f_amount; 6174 mutex_exit(&freemem_lock); 6175 } 6176 6177 return (f_amount); 6178 } 6179 6180 /* 6181 * The following functions is called from free_vp_pages() 6182 * for an inexact estimate of a newly free'd page... 6183 */ 6184 ulong_t 6185 page_share_cnt(page_t *pp) 6186 { 6187 return (hat_page_getshare(pp)); 6188 } 6189 6190 int 6191 page_isshared(page_t *pp) 6192 { 6193 return (hat_page_getshare(pp) > 1); 6194 } 6195 6196 int 6197 page_isfree(page_t *pp) 6198 { 6199 return (PP_ISFREE(pp)); 6200 } 6201 6202 int 6203 page_isref(page_t *pp) 6204 { 6205 return (hat_page_getattr(pp, P_REF)); 6206 } 6207 6208 int 6209 page_ismod(page_t *pp) 6210 { 6211 return (hat_page_getattr(pp, P_MOD)); 6212 } 6213 6214 /* 6215 * Reclaim the given constituent page from the freelist, regardless of it's 6216 * size. The page will be demoted as required. 6217 * Returns 1 on success or 0 on failure. 6218 * 6219 * The page is unlocked if it can't be reclaimed (when freemem == 0). 6220 * If `lock' is non-null, it will be dropped and re-acquired if 6221 * the routine must wait while freemem is 0. 6222 */ 6223 int 6224 page_reclaim_page(page_t *pp, kmutex_t *lock) 6225 { 6226 struct pcf *p; 6227 uint_t pcf_index; 6228 struct cpu *cpup; 6229 uint_t i; 6230 pgcnt_t collected = 0; 6231 6232 ASSERT(lock != NULL ? MUTEX_HELD(lock) : 1); 6233 ASSERT(PAGE_EXCL(pp) && PP_ISFREE(pp)); 6234 6235 /* 6236 * If `freemem' is 0, we cannot reclaim this page from the 6237 * freelist, so release every lock we might hold: the page, 6238 * and the `lock' before blocking. 6239 * 6240 * The only way `freemem' can become 0 while there are pages 6241 * marked free (have their p->p_free bit set) is when the 6242 * system is low on memory and doing a page_create(). In 6243 * order to guarantee that once page_create() starts acquiring 6244 * pages it will be able to get all that it needs since `freemem' 6245 * was decreased by the requested amount. So, we need to release 6246 * this page, and let page_create() have it. 6247 * 6248 * Since `freemem' being zero is not supposed to happen, just 6249 * use the usual hash stuff as a starting point. If that bucket 6250 * is empty, then assume the worst, and start at the beginning 6251 * of the pcf array. If we always start at the beginning 6252 * when acquiring more than one pcf lock, there won't be any 6253 * deadlock problems. 6254 */ 6255 6256 /* TODO: Do we need to test kcage_freemem if PG_NORELOC(pp)? */ 6257 6258 if (freemem <= throttlefree && !page_create_throttle(1, 0)) { 6259 pcf_acquire_all(); 6260 goto page_reclaim_nomem; 6261 } 6262 6263 pcf_index = PCF_INDEX(); 6264 p = &pcf[pcf_index]; 6265 mutex_enter(&p->pcf_lock); 6266 if (p->pcf_count > 0) { 6267 collected = 1; 6268 p->pcf_count -= 1; 6269 } 6270 mutex_exit(&p->pcf_lock); 6271 6272 if (!collected) { 6273 VM_STAT_ADD(page_reclaim_zero); 6274 /* 6275 * Check again. Its possible that some other thread 6276 * could have been right behind us, and added one 6277 * to a list somewhere. Acquire each of the pcf locks 6278 * until we find a page. 6279 */ 6280 p = pcf; 6281 for (i = 0; i < PCF_FANOUT; i++) { 6282 mutex_enter(&p->pcf_lock); 6283 if (p->pcf_count) { 6284 if (p->pcf_count > 0) { 6285 p->pcf_count -= 1; 6286 collected = 1; 6287 break; 6288 } 6289 } 6290 p++; 6291 } 6292 6293 if (!collected) { 6294 page_reclaim_nomem: 6295 /* 6296 * We really can't have page `pp'. 6297 * Time for the no-memory dance with 6298 * page_free(). This is just like 6299 * page_create_wait(). Plus the added 6300 * attraction of releasing whatever mutex 6301 * we held when we were called with in `lock'. 6302 * Page_unlock() will wakeup any thread 6303 * waiting around for this page. 6304 */ 6305 if (lock) { 6306 VM_STAT_ADD(page_reclaim_zero_locked); 6307 mutex_exit(lock); 6308 } 6309 page_unlock(pp); 6310 6311 /* 6312 * get this before we drop all the pcf locks. 6313 */ 6314 mutex_enter(&new_freemem_lock); 6315 6316 p = pcf; 6317 for (i = 0; i < PCF_FANOUT; i++) { 6318 p->pcf_wait++; 6319 mutex_exit(&p->pcf_lock); 6320 p++; 6321 } 6322 6323 freemem_wait++; 6324 cv_wait(&freemem_cv, &new_freemem_lock); 6325 freemem_wait--; 6326 6327 mutex_exit(&new_freemem_lock); 6328 6329 if (lock) { 6330 mutex_enter(lock); 6331 } 6332 return (0); 6333 } 6334 6335 /* 6336 * We beat the PCF bins over the head until 6337 * we got the memory that we wanted. 6338 * The pcf accounting has been done, 6339 * though none of the pcf_wait flags have been set, 6340 * drop the locks and continue on. 6341 */ 6342 ASSERT(collected == 1); 6343 while (p >= pcf) { 6344 mutex_exit(&p->pcf_lock); 6345 p--; 6346 } 6347 } 6348 6349 /* 6350 * freemem is not protected by any lock. Thus, we cannot 6351 * have any assertion containing freemem here. 6352 */ 6353 freemem -= 1; 6354 6355 VM_STAT_ADD(pagecnt.pc_reclaim); 6356 if (PP_ISAGED(pp)) { 6357 page_list_sub(pp, PG_FREE_LIST); 6358 TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_FREE, 6359 "page_reclaim_page_free:pp %p", pp); 6360 } else { 6361 page_list_sub(pp, PG_CACHE_LIST); 6362 TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_CACHE, 6363 "page_reclaim_page_cache:pp %p", pp); 6364 } 6365 6366 /* 6367 * The page we took off the freelist must be szc 0 as 6368 * we used page_list_sub which will demote the page if needed. 6369 */ 6370 ASSERT(pp->p_szc == 0); 6371 6372 /* 6373 * clear the p_free & p_age bits since this page is no longer 6374 * on the free list. Notice that there was a brief time where 6375 * a page is marked as free, but is not on the list. 6376 * 6377 * Set the reference bit to protect against immediate pageout. 6378 */ 6379 PP_CLRFREE(pp); 6380 PP_CLRAGED(pp); 6381 page_set_props(pp, P_REF); 6382 6383 CPU_STATS_ENTER_K(); 6384 cpup = CPU; /* get cpup now that CPU cannot change */ 6385 CPU_STATS_ADDQ(cpup, vm, pgrec, 1); 6386 CPU_STATS_ADDQ(cpup, vm, pgfrec, 1); 6387 CPU_STATS_EXIT_K(); 6388 6389 return (1); 6390 } 6391 6392 /* 6393 * The following code all currently relates to the page capture logic: 6394 * 6395 * This logic is used for cases where there is a desire to claim a certain 6396 * physical page in the system for the caller. As it may not be possible 6397 * to capture the page immediately, the p_toxic bits are used in the page 6398 * structure to indicate that someone wants to capture this page. When the 6399 * page gets unlocked, the toxic flag will be noted and an attempt to capture 6400 * the page will be made. If it is successful, the original callers callback 6401 * will be called with the page to do with it what they please. 6402 * 6403 * There is also an async thread which wakes up to attempt to capture 6404 * pages occasionally which have the capture bit set. All of the pages which 6405 * need to be captured asynchronously have been inserted into the 6406 * page_capture_hash and thus this thread walks that hash list. Items in the 6407 * hash have an expiration time so this thread handles that as well by removing 6408 * the item from the hash if it has expired. 6409 * 6410 * Some important things to note are: 6411 * - if the PR_CAPTURE bit is set on a page, then the page is in the 6412 * page_capture_hash. The page_capture_hash_head.pchh_mutex is needed 6413 * to set and clear this bit, and while the lock is held is the only time 6414 * you can add or remove an entry from the hash. 6415 * - the PR_CAPTURE bit can only be set and cleared while holding the 6416 * page_capture_hash_head.pchh_mutex 6417 * - the t_flag field of the thread struct is used with the T_CAPTURING 6418 * flag to prevent recursion while dealing with large pages. 6419 * - pages which need to be retired never expire on the page_capture_hash. 6420 */ 6421 6422 static void page_capture_thread(void); 6423 static kthread_t *pc_thread_id; 6424 kcondvar_t pc_cv; 6425 static kmutex_t pc_thread_mutex; 6426 static clock_t pc_thread_shortwait; 6427 static clock_t pc_thread_longwait; 6428 static int pc_thread_ism_retry; 6429 6430 struct page_capture_callback pc_cb[PC_NUM_CALLBACKS]; 6431 6432 /* Note that this is a circular linked list */ 6433 typedef struct page_capture_hash_bucket { 6434 page_t *pp; 6435 uint_t szc; 6436 uint_t flags; 6437 clock_t expires; /* lbolt at which this request expires. */ 6438 void *datap; /* Cached data passed in for callback */ 6439 struct page_capture_hash_bucket *next; 6440 struct page_capture_hash_bucket *prev; 6441 } page_capture_hash_bucket_t; 6442 6443 /* 6444 * Each hash bucket will have it's own mutex and two lists which are: 6445 * active (0): represents requests which have not been processed by 6446 * the page_capture async thread yet. 6447 * walked (1): represents requests which have been processed by the 6448 * page_capture async thread within it's given walk of this bucket. 6449 * 6450 * These are all needed so that we can synchronize all async page_capture 6451 * events. When the async thread moves to a new bucket, it will append the 6452 * walked list to the active list and walk each item one at a time, moving it 6453 * from the active list to the walked list. Thus if there is an async request 6454 * outstanding for a given page, it will always be in one of the two lists. 6455 * New requests will always be added to the active list. 6456 * If we were not able to capture a page before the request expired, we'd free 6457 * up the request structure which would indicate to page_capture that there is 6458 * no longer a need for the given page, and clear the PR_CAPTURE flag if 6459 * possible. 6460 */ 6461 typedef struct page_capture_hash_head { 6462 kmutex_t pchh_mutex; 6463 uint_t num_pages; 6464 page_capture_hash_bucket_t lists[2]; /* sentinel nodes */ 6465 } page_capture_hash_head_t; 6466 6467 #ifdef DEBUG 6468 #define NUM_PAGE_CAPTURE_BUCKETS 4 6469 #else 6470 #define NUM_PAGE_CAPTURE_BUCKETS 64 6471 #endif 6472 6473 page_capture_hash_head_t page_capture_hash[NUM_PAGE_CAPTURE_BUCKETS]; 6474 6475 /* for now use a very simple hash based upon the size of a page struct */ 6476 #define PAGE_CAPTURE_HASH(pp) \ 6477 ((int)(((uintptr_t)pp >> 7) & (NUM_PAGE_CAPTURE_BUCKETS - 1))) 6478 6479 extern pgcnt_t swapfs_minfree; 6480 6481 int page_trycapture(page_t *pp, uint_t szc, uint_t flags, void *datap); 6482 6483 /* 6484 * a callback function is required for page capture requests. 6485 */ 6486 void 6487 page_capture_register_callback(uint_t index, clock_t duration, 6488 int (*cb_func)(page_t *, void *, uint_t)) 6489 { 6490 ASSERT(pc_cb[index].cb_active == 0); 6491 ASSERT(cb_func != NULL); 6492 rw_enter(&pc_cb[index].cb_rwlock, RW_WRITER); 6493 pc_cb[index].duration = duration; 6494 pc_cb[index].cb_func = cb_func; 6495 pc_cb[index].cb_active = 1; 6496 rw_exit(&pc_cb[index].cb_rwlock); 6497 } 6498 6499 void 6500 page_capture_unregister_callback(uint_t index) 6501 { 6502 int i, j; 6503 struct page_capture_hash_bucket *bp1; 6504 struct page_capture_hash_bucket *bp2; 6505 struct page_capture_hash_bucket *head = NULL; 6506 uint_t flags = (1 << index); 6507 6508 rw_enter(&pc_cb[index].cb_rwlock, RW_WRITER); 6509 ASSERT(pc_cb[index].cb_active == 1); 6510 pc_cb[index].duration = 0; /* Paranoia */ 6511 pc_cb[index].cb_func = NULL; /* Paranoia */ 6512 pc_cb[index].cb_active = 0; 6513 rw_exit(&pc_cb[index].cb_rwlock); 6514 6515 /* 6516 * Just move all the entries to a private list which we can walk 6517 * through without the need to hold any locks. 6518 * No more requests can get added to the hash lists for this consumer 6519 * as the cb_active field for the callback has been cleared. 6520 */ 6521 for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 6522 mutex_enter(&page_capture_hash[i].pchh_mutex); 6523 for (j = 0; j < 2; j++) { 6524 bp1 = page_capture_hash[i].lists[j].next; 6525 /* walk through all but first (sentinel) element */ 6526 while (bp1 != &page_capture_hash[i].lists[j]) { 6527 bp2 = bp1; 6528 if (bp2->flags & flags) { 6529 bp1 = bp2->next; 6530 bp1->prev = bp2->prev; 6531 bp2->prev->next = bp1; 6532 bp2->next = head; 6533 head = bp2; 6534 /* 6535 * Clear the PR_CAPTURE bit as we 6536 * hold appropriate locks here. 6537 */ 6538 page_clrtoxic(head->pp, PR_CAPTURE); 6539 page_capture_hash[i].num_pages--; 6540 continue; 6541 } 6542 bp1 = bp1->next; 6543 } 6544 } 6545 mutex_exit(&page_capture_hash[i].pchh_mutex); 6546 } 6547 6548 while (head != NULL) { 6549 bp1 = head; 6550 head = head->next; 6551 kmem_free(bp1, sizeof (*bp1)); 6552 } 6553 } 6554 6555 6556 /* 6557 * Find pp in the active list and move it to the walked list if it 6558 * exists. 6559 * Note that most often pp should be at the front of the active list 6560 * as it is currently used and thus there is no other sort of optimization 6561 * being done here as this is a linked list data structure. 6562 * Returns 1 on successful move or 0 if page could not be found. 6563 */ 6564 static int 6565 page_capture_move_to_walked(page_t *pp) 6566 { 6567 page_capture_hash_bucket_t *bp; 6568 int index; 6569 6570 index = PAGE_CAPTURE_HASH(pp); 6571 6572 mutex_enter(&page_capture_hash[index].pchh_mutex); 6573 bp = page_capture_hash[index].lists[0].next; 6574 while (bp != &page_capture_hash[index].lists[0]) { 6575 if (bp->pp == pp) { 6576 /* Remove from old list */ 6577 bp->next->prev = bp->prev; 6578 bp->prev->next = bp->next; 6579 6580 /* Add to new list */ 6581 bp->next = page_capture_hash[index].lists[1].next; 6582 bp->prev = &page_capture_hash[index].lists[1]; 6583 page_capture_hash[index].lists[1].next = bp; 6584 bp->next->prev = bp; 6585 mutex_exit(&page_capture_hash[index].pchh_mutex); 6586 6587 return (1); 6588 } 6589 bp = bp->next; 6590 } 6591 mutex_exit(&page_capture_hash[index].pchh_mutex); 6592 return (0); 6593 } 6594 6595 /* 6596 * Add a new entry to the page capture hash. The only case where a new 6597 * entry is not added is when the page capture consumer is no longer registered. 6598 * In this case, we'll silently not add the page to the hash. We know that 6599 * page retire will always be registered for the case where we are currently 6600 * unretiring a page and thus there are no conflicts. 6601 */ 6602 static void 6603 page_capture_add_hash(page_t *pp, uint_t szc, uint_t flags, void *datap) 6604 { 6605 page_capture_hash_bucket_t *bp1; 6606 page_capture_hash_bucket_t *bp2; 6607 int index; 6608 int cb_index; 6609 int i; 6610 #ifdef DEBUG 6611 page_capture_hash_bucket_t *tp1; 6612 int l; 6613 #endif 6614 6615 ASSERT(!(flags & CAPTURE_ASYNC)); 6616 6617 bp1 = kmem_alloc(sizeof (struct page_capture_hash_bucket), KM_SLEEP); 6618 6619 bp1->pp = pp; 6620 bp1->szc = szc; 6621 bp1->flags = flags; 6622 bp1->datap = datap; 6623 6624 for (cb_index = 0; cb_index < PC_NUM_CALLBACKS; cb_index++) { 6625 if ((flags >> cb_index) & 1) { 6626 break; 6627 } 6628 } 6629 6630 ASSERT(cb_index != PC_NUM_CALLBACKS); 6631 6632 rw_enter(&pc_cb[cb_index].cb_rwlock, RW_READER); 6633 if (pc_cb[cb_index].cb_active) { 6634 if (pc_cb[cb_index].duration == -1) { 6635 bp1->expires = (clock_t)-1; 6636 } else { 6637 bp1->expires = lbolt + pc_cb[cb_index].duration; 6638 } 6639 } else { 6640 /* There's no callback registered so don't add to the hash */ 6641 rw_exit(&pc_cb[cb_index].cb_rwlock); 6642 kmem_free(bp1, sizeof (*bp1)); 6643 return; 6644 } 6645 6646 index = PAGE_CAPTURE_HASH(pp); 6647 6648 /* 6649 * Only allow capture flag to be modified under this mutex. 6650 * Prevents multiple entries for same page getting added. 6651 */ 6652 mutex_enter(&page_capture_hash[index].pchh_mutex); 6653 6654 /* 6655 * if not already on the hash, set capture bit and add to the hash 6656 */ 6657 if (!(pp->p_toxic & PR_CAPTURE)) { 6658 #ifdef DEBUG 6659 /* Check for duplicate entries */ 6660 for (l = 0; l < 2; l++) { 6661 tp1 = page_capture_hash[index].lists[l].next; 6662 while (tp1 != &page_capture_hash[index].lists[l]) { 6663 if (tp1->pp == pp) { 6664 panic("page pp 0x%p already on hash " 6665 "at 0x%p\n", pp, tp1); 6666 } 6667 tp1 = tp1->next; 6668 } 6669 } 6670 6671 #endif 6672 page_settoxic(pp, PR_CAPTURE); 6673 bp1->next = page_capture_hash[index].lists[0].next; 6674 bp1->prev = &page_capture_hash[index].lists[0]; 6675 bp1->next->prev = bp1; 6676 page_capture_hash[index].lists[0].next = bp1; 6677 page_capture_hash[index].num_pages++; 6678 if (flags & CAPTURE_RETIRE) { 6679 page_retire_incr_pend_count(); 6680 } 6681 mutex_exit(&page_capture_hash[index].pchh_mutex); 6682 rw_exit(&pc_cb[cb_index].cb_rwlock); 6683 cv_signal(&pc_cv); 6684 return; 6685 } 6686 6687 /* 6688 * A page retire request will replace any other request. 6689 * A second physmem request which is for a different process than 6690 * the currently registered one will be dropped as there is 6691 * no way to hold the private data for both calls. 6692 * In the future, once there are more callers, this will have to 6693 * be worked out better as there needs to be private storage for 6694 * at least each type of caller (maybe have datap be an array of 6695 * *void's so that we can index based upon callers index). 6696 */ 6697 6698 /* walk hash list to update expire time */ 6699 for (i = 0; i < 2; i++) { 6700 bp2 = page_capture_hash[index].lists[i].next; 6701 while (bp2 != &page_capture_hash[index].lists[i]) { 6702 if (bp2->pp == pp) { 6703 if (flags & CAPTURE_RETIRE) { 6704 if (!(bp2->flags & CAPTURE_RETIRE)) { 6705 page_retire_incr_pend_count(); 6706 bp2->flags = flags; 6707 bp2->expires = bp1->expires; 6708 bp2->datap = datap; 6709 } 6710 } else { 6711 ASSERT(flags & CAPTURE_PHYSMEM); 6712 if (!(bp2->flags & CAPTURE_RETIRE) && 6713 (datap == bp2->datap)) { 6714 bp2->expires = bp1->expires; 6715 } 6716 } 6717 mutex_exit(&page_capture_hash[index]. 6718 pchh_mutex); 6719 rw_exit(&pc_cb[cb_index].cb_rwlock); 6720 kmem_free(bp1, sizeof (*bp1)); 6721 return; 6722 } 6723 bp2 = bp2->next; 6724 } 6725 } 6726 6727 /* 6728 * the PR_CAPTURE flag is protected by the page_capture_hash mutexes 6729 * and thus it either has to be set or not set and can't change 6730 * while holding the mutex above. 6731 */ 6732 panic("page_capture_add_hash, PR_CAPTURE flag set on pp %p\n", pp); 6733 } 6734 6735 /* 6736 * We have a page in our hands, lets try and make it ours by turning 6737 * it into a clean page like it had just come off the freelists. 6738 * 6739 * Returns 0 on success, with the page still EXCL locked. 6740 * On failure, the page will be unlocked, and returns EAGAIN 6741 */ 6742 static int 6743 page_capture_clean_page(page_t *pp) 6744 { 6745 page_t *newpp; 6746 int skip_unlock = 0; 6747 spgcnt_t count; 6748 page_t *tpp; 6749 int ret = 0; 6750 int extra; 6751 6752 ASSERT(PAGE_EXCL(pp)); 6753 ASSERT(!PP_RETIRED(pp)); 6754 ASSERT(curthread->t_flag & T_CAPTURING); 6755 6756 if (PP_ISFREE(pp)) { 6757 if (!page_reclaim_page(pp, NULL)) { 6758 skip_unlock = 1; 6759 ret = EAGAIN; 6760 goto cleanup; 6761 } 6762 if (pp->p_vnode != NULL) { 6763 /* 6764 * Since this page came from the 6765 * cachelist, we must destroy the 6766 * old vnode association. 6767 */ 6768 page_hashout(pp, NULL); 6769 } 6770 goto cleanup; 6771 } 6772 6773 /* 6774 * If we know page_relocate will fail, skip it 6775 * It could still fail due to a UE on another page but we 6776 * can't do anything about that. 6777 */ 6778 if (pp->p_toxic & PR_UE) { 6779 goto skip_relocate; 6780 } 6781 6782 /* 6783 * It's possible that pages can not have a vnode as fsflush comes 6784 * through and cleans up these pages. It's ugly but that's how it is. 6785 */ 6786 if (pp->p_vnode == NULL) { 6787 goto skip_relocate; 6788 } 6789 6790 /* 6791 * Page was not free, so lets try to relocate it. 6792 * page_relocate only works with root pages, so if this is not a root 6793 * page, we need to demote it to try and relocate it. 6794 * Unfortunately this is the best we can do right now. 6795 */ 6796 newpp = NULL; 6797 if ((pp->p_szc > 0) && (pp != PP_PAGEROOT(pp))) { 6798 if (page_try_demote_pages(pp) == 0) { 6799 ret = EAGAIN; 6800 goto cleanup; 6801 } 6802 } 6803 ret = page_relocate(&pp, &newpp, 1, 0, &count, NULL); 6804 if (ret == 0) { 6805 page_t *npp; 6806 /* unlock the new page(s) */ 6807 while (count-- > 0) { 6808 ASSERT(newpp != NULL); 6809 npp = newpp; 6810 page_sub(&newpp, npp); 6811 page_unlock(npp); 6812 } 6813 ASSERT(newpp == NULL); 6814 /* 6815 * Check to see if the page we have is too large. 6816 * If so, demote it freeing up the extra pages. 6817 */ 6818 if (pp->p_szc > 0) { 6819 /* For now demote extra pages to szc == 0 */ 6820 extra = page_get_pagecnt(pp->p_szc) - 1; 6821 while (extra > 0) { 6822 tpp = pp->p_next; 6823 page_sub(&pp, tpp); 6824 tpp->p_szc = 0; 6825 page_free(tpp, 1); 6826 extra--; 6827 } 6828 /* Make sure to set our page to szc 0 as well */ 6829 ASSERT(pp->p_next == pp && pp->p_prev == pp); 6830 pp->p_szc = 0; 6831 } 6832 goto cleanup; 6833 } else if (ret == EIO) { 6834 ret = EAGAIN; 6835 goto cleanup; 6836 } else { 6837 /* 6838 * Need to reset return type as we failed to relocate the page 6839 * but that does not mean that some of the next steps will not 6840 * work. 6841 */ 6842 ret = 0; 6843 } 6844 6845 skip_relocate: 6846 6847 if (pp->p_szc > 0) { 6848 if (page_try_demote_pages(pp) == 0) { 6849 ret = EAGAIN; 6850 goto cleanup; 6851 } 6852 } 6853 6854 ASSERT(pp->p_szc == 0); 6855 6856 if (hat_ismod(pp)) { 6857 ret = EAGAIN; 6858 goto cleanup; 6859 } 6860 if (PP_ISKAS(pp)) { 6861 ret = EAGAIN; 6862 goto cleanup; 6863 } 6864 if (pp->p_lckcnt || pp->p_cowcnt) { 6865 ret = EAGAIN; 6866 goto cleanup; 6867 } 6868 6869 (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 6870 ASSERT(!hat_page_is_mapped(pp)); 6871 6872 if (hat_ismod(pp)) { 6873 /* 6874 * This is a semi-odd case as the page is now modified but not 6875 * mapped as we just unloaded the mappings above. 6876 */ 6877 ret = EAGAIN; 6878 goto cleanup; 6879 } 6880 if (pp->p_vnode != NULL) { 6881 page_hashout(pp, NULL); 6882 } 6883 6884 /* 6885 * At this point, the page should be in a clean state and 6886 * we can do whatever we want with it. 6887 */ 6888 6889 cleanup: 6890 if (ret != 0) { 6891 if (!skip_unlock) { 6892 page_unlock(pp); 6893 } 6894 } else { 6895 ASSERT(pp->p_szc == 0); 6896 ASSERT(PAGE_EXCL(pp)); 6897 6898 pp->p_next = pp; 6899 pp->p_prev = pp; 6900 } 6901 return (ret); 6902 } 6903 6904 /* 6905 * Various callers of page_trycapture() can have different restrictions upon 6906 * what memory they have access to. 6907 * Returns 0 on success, with the following error codes on failure: 6908 * EPERM - The requested page is long term locked, and thus repeated 6909 * requests to capture this page will likely fail. 6910 * ENOMEM - There was not enough free memory in the system to safely 6911 * map the requested page. 6912 * ENOENT - The requested page was inside the kernel cage, and the 6913 * PHYSMEM_CAGE flag was not set. 6914 */ 6915 int 6916 page_capture_pre_checks(page_t *pp, uint_t flags) 6917 { 6918 #if defined(__sparc) 6919 extern struct vnode prom_ppages; 6920 #endif /* __sparc */ 6921 6922 ASSERT(pp != NULL); 6923 6924 /* only physmem currently has restrictions */ 6925 if (!(flags & CAPTURE_PHYSMEM)) { 6926 return (0); 6927 } 6928 6929 #if defined(__sparc) 6930 if (pp->p_vnode == &prom_ppages) { 6931 return (EPERM); 6932 } 6933 6934 if (PP_ISNORELOC(pp) && !(flags & CAPTURE_GET_CAGE)) { 6935 return (ENOENT); 6936 } 6937 6938 if (PP_ISNORELOCKERNEL(pp)) { 6939 return (EPERM); 6940 } 6941 #else 6942 if (PP_ISKAS(pp)) { 6943 return (EPERM); 6944 } 6945 #endif /* __sparc */ 6946 6947 if (availrmem < swapfs_minfree) { 6948 /* 6949 * We won't try to capture this page as we are 6950 * running low on memory. 6951 */ 6952 return (ENOMEM); 6953 } 6954 return (0); 6955 } 6956 6957 /* 6958 * Once we have a page in our mits, go ahead and complete the capture 6959 * operation. 6960 * Returns 1 on failure where page is no longer needed 6961 * Returns 0 on success 6962 * Returns -1 if there was a transient failure. 6963 * Failure cases must release the SE_EXCL lock on pp (usually via page_free). 6964 */ 6965 int 6966 page_capture_take_action(page_t *pp, uint_t flags, void *datap) 6967 { 6968 int cb_index; 6969 int ret = 0; 6970 page_capture_hash_bucket_t *bp1; 6971 page_capture_hash_bucket_t *bp2; 6972 int index; 6973 int found = 0; 6974 int i; 6975 6976 ASSERT(PAGE_EXCL(pp)); 6977 ASSERT(curthread->t_flag & T_CAPTURING); 6978 6979 for (cb_index = 0; cb_index < PC_NUM_CALLBACKS; cb_index++) { 6980 if ((flags >> cb_index) & 1) { 6981 break; 6982 } 6983 } 6984 ASSERT(cb_index < PC_NUM_CALLBACKS); 6985 6986 /* 6987 * Remove the entry from the page_capture hash, but don't free it yet 6988 * as we may need to put it back. 6989 * Since we own the page at this point in time, we should find it 6990 * in the hash if this is an ASYNC call. If we don't it's likely 6991 * that the page_capture_async() thread decided that this request 6992 * had expired, in which case we just continue on. 6993 */ 6994 if (flags & CAPTURE_ASYNC) { 6995 6996 index = PAGE_CAPTURE_HASH(pp); 6997 6998 mutex_enter(&page_capture_hash[index].pchh_mutex); 6999 for (i = 0; i < 2 && !found; i++) { 7000 bp1 = page_capture_hash[index].lists[i].next; 7001 while (bp1 != &page_capture_hash[index].lists[i]) { 7002 if (bp1->pp == pp) { 7003 bp1->next->prev = bp1->prev; 7004 bp1->prev->next = bp1->next; 7005 page_capture_hash[index].num_pages--; 7006 page_clrtoxic(pp, PR_CAPTURE); 7007 found = 1; 7008 break; 7009 } 7010 bp1 = bp1->next; 7011 } 7012 } 7013 mutex_exit(&page_capture_hash[index].pchh_mutex); 7014 } 7015 7016 /* Synchronize with the unregister func. */ 7017 rw_enter(&pc_cb[cb_index].cb_rwlock, RW_READER); 7018 if (!pc_cb[cb_index].cb_active) { 7019 page_free(pp, 1); 7020 rw_exit(&pc_cb[cb_index].cb_rwlock); 7021 if (found) { 7022 kmem_free(bp1, sizeof (*bp1)); 7023 } 7024 return (1); 7025 } 7026 7027 /* 7028 * We need to remove the entry from the page capture hash and turn off 7029 * the PR_CAPTURE bit before calling the callback. We'll need to cache 7030 * the entry here, and then based upon the return value, cleanup 7031 * appropriately or re-add it to the hash, making sure that someone else 7032 * hasn't already done so. 7033 * It should be rare for the callback to fail and thus it's ok for 7034 * the failure path to be a bit complicated as the success path is 7035 * cleaner and the locking rules are easier to follow. 7036 */ 7037 7038 ret = pc_cb[cb_index].cb_func(pp, datap, flags); 7039 7040 rw_exit(&pc_cb[cb_index].cb_rwlock); 7041 7042 /* 7043 * If this was an ASYNC request, we need to cleanup the hash if the 7044 * callback was successful or if the request was no longer valid. 7045 * For non-ASYNC requests, we return failure to map and the caller 7046 * will take care of adding the request to the hash. 7047 * Note also that the callback itself is responsible for the page 7048 * at this point in time in terms of locking ... The most common 7049 * case for the failure path should just be a page_free. 7050 */ 7051 if (ret >= 0) { 7052 if (found) { 7053 if (bp1->flags & CAPTURE_RETIRE) { 7054 page_retire_decr_pend_count(); 7055 } 7056 kmem_free(bp1, sizeof (*bp1)); 7057 } 7058 return (ret); 7059 } 7060 if (!found) { 7061 return (ret); 7062 } 7063 7064 ASSERT(flags & CAPTURE_ASYNC); 7065 7066 /* 7067 * Check for expiration time first as we can just free it up if it's 7068 * expired. 7069 */ 7070 if (lbolt > bp1->expires && bp1->expires != -1) { 7071 kmem_free(bp1, sizeof (*bp1)); 7072 return (ret); 7073 } 7074 7075 /* 7076 * The callback failed and there used to be an entry in the hash for 7077 * this page, so we need to add it back to the hash. 7078 */ 7079 mutex_enter(&page_capture_hash[index].pchh_mutex); 7080 if (!(pp->p_toxic & PR_CAPTURE)) { 7081 /* just add bp1 back to head of walked list */ 7082 page_settoxic(pp, PR_CAPTURE); 7083 bp1->next = page_capture_hash[index].lists[1].next; 7084 bp1->prev = &page_capture_hash[index].lists[1]; 7085 bp1->next->prev = bp1; 7086 page_capture_hash[index].lists[1].next = bp1; 7087 page_capture_hash[index].num_pages++; 7088 mutex_exit(&page_capture_hash[index].pchh_mutex); 7089 return (ret); 7090 } 7091 7092 /* 7093 * Otherwise there was a new capture request added to list 7094 * Need to make sure that our original data is represented if 7095 * appropriate. 7096 */ 7097 for (i = 0; i < 2; i++) { 7098 bp2 = page_capture_hash[index].lists[i].next; 7099 while (bp2 != &page_capture_hash[index].lists[i]) { 7100 if (bp2->pp == pp) { 7101 if (bp1->flags & CAPTURE_RETIRE) { 7102 if (!(bp2->flags & CAPTURE_RETIRE)) { 7103 bp2->szc = bp1->szc; 7104 bp2->flags = bp1->flags; 7105 bp2->expires = bp1->expires; 7106 bp2->datap = bp1->datap; 7107 } 7108 } else { 7109 ASSERT(bp1->flags & CAPTURE_PHYSMEM); 7110 if (!(bp2->flags & CAPTURE_RETIRE)) { 7111 bp2->szc = bp1->szc; 7112 bp2->flags = bp1->flags; 7113 bp2->expires = bp1->expires; 7114 bp2->datap = bp1->datap; 7115 } 7116 } 7117 mutex_exit(&page_capture_hash[index]. 7118 pchh_mutex); 7119 kmem_free(bp1, sizeof (*bp1)); 7120 return (ret); 7121 } 7122 bp2 = bp2->next; 7123 } 7124 } 7125 panic("PR_CAPTURE set but not on hash for pp 0x%p\n", pp); 7126 /*NOTREACHED*/ 7127 } 7128 7129 /* 7130 * Try to capture the given page for the caller specified in the flags 7131 * parameter. The page will either be captured and handed over to the 7132 * appropriate callback, or will be queued up in the page capture hash 7133 * to be captured asynchronously. 7134 * If the current request is due to an async capture, the page must be 7135 * exclusively locked before calling this function. 7136 * Currently szc must be 0 but in the future this should be expandable to 7137 * other page sizes. 7138 * Returns 0 on success, with the following error codes on failure: 7139 * EPERM - The requested page is long term locked, and thus repeated 7140 * requests to capture this page will likely fail. 7141 * ENOMEM - There was not enough free memory in the system to safely 7142 * map the requested page. 7143 * ENOENT - The requested page was inside the kernel cage, and the 7144 * CAPTURE_GET_CAGE flag was not set. 7145 * EAGAIN - The requested page could not be capturead at this point in 7146 * time but future requests will likely work. 7147 * EBUSY - The requested page is retired and the CAPTURE_GET_RETIRED flag 7148 * was not set. 7149 */ 7150 int 7151 page_itrycapture(page_t *pp, uint_t szc, uint_t flags, void *datap) 7152 { 7153 int ret; 7154 int cb_index; 7155 7156 if (flags & CAPTURE_ASYNC) { 7157 ASSERT(PAGE_EXCL(pp)); 7158 goto async; 7159 } 7160 7161 /* Make sure there's enough availrmem ... */ 7162 ret = page_capture_pre_checks(pp, flags); 7163 if (ret != 0) { 7164 return (ret); 7165 } 7166 7167 if (!page_trylock(pp, SE_EXCL)) { 7168 for (cb_index = 0; cb_index < PC_NUM_CALLBACKS; cb_index++) { 7169 if ((flags >> cb_index) & 1) { 7170 break; 7171 } 7172 } 7173 ASSERT(cb_index < PC_NUM_CALLBACKS); 7174 ret = EAGAIN; 7175 /* Special case for retired pages */ 7176 if (PP_RETIRED(pp)) { 7177 if (flags & CAPTURE_GET_RETIRED) { 7178 if (!page_unretire_pp(pp, PR_UNR_TEMP)) { 7179 /* 7180 * Need to set capture bit and add to 7181 * hash so that the page will be 7182 * retired when freed. 7183 */ 7184 page_capture_add_hash(pp, szc, 7185 CAPTURE_RETIRE, NULL); 7186 ret = 0; 7187 goto own_page; 7188 } 7189 } else { 7190 return (EBUSY); 7191 } 7192 } 7193 page_capture_add_hash(pp, szc, flags, datap); 7194 return (ret); 7195 } 7196 7197 async: 7198 ASSERT(PAGE_EXCL(pp)); 7199 7200 /* Need to check for physmem async requests that availrmem is sane */ 7201 if ((flags & (CAPTURE_ASYNC | CAPTURE_PHYSMEM)) == 7202 (CAPTURE_ASYNC | CAPTURE_PHYSMEM) && 7203 (availrmem < swapfs_minfree)) { 7204 page_unlock(pp); 7205 return (ENOMEM); 7206 } 7207 7208 ret = page_capture_clean_page(pp); 7209 7210 if (ret != 0) { 7211 /* We failed to get the page, so lets add it to the hash */ 7212 if (!(flags & CAPTURE_ASYNC)) { 7213 page_capture_add_hash(pp, szc, flags, datap); 7214 } 7215 return (ret); 7216 } 7217 7218 own_page: 7219 ASSERT(PAGE_EXCL(pp)); 7220 ASSERT(pp->p_szc == 0); 7221 7222 /* Call the callback */ 7223 ret = page_capture_take_action(pp, flags, datap); 7224 7225 if (ret == 0) { 7226 return (0); 7227 } 7228 7229 /* 7230 * Note that in the failure cases from page_capture_take_action, the 7231 * EXCL lock will have already been dropped. 7232 */ 7233 if ((ret == -1) && (!(flags & CAPTURE_ASYNC))) { 7234 page_capture_add_hash(pp, szc, flags, datap); 7235 } 7236 return (EAGAIN); 7237 } 7238 7239 int 7240 page_trycapture(page_t *pp, uint_t szc, uint_t flags, void *datap) 7241 { 7242 int ret; 7243 7244 curthread->t_flag |= T_CAPTURING; 7245 ret = page_itrycapture(pp, szc, flags, datap); 7246 curthread->t_flag &= ~T_CAPTURING; /* xor works as we know its set */ 7247 return (ret); 7248 } 7249 7250 /* 7251 * When unlocking a page which has the PR_CAPTURE bit set, this routine 7252 * gets called to try and capture the page. 7253 */ 7254 void 7255 page_unlock_capture(page_t *pp) 7256 { 7257 page_capture_hash_bucket_t *bp; 7258 int index; 7259 int i; 7260 uint_t szc; 7261 uint_t flags = 0; 7262 void *datap; 7263 kmutex_t *mp; 7264 extern vnode_t retired_pages; 7265 7266 /* 7267 * We need to protect against a possible deadlock here where we own 7268 * the vnode page hash mutex and want to acquire it again as there 7269 * are locations in the code, where we unlock a page while holding 7270 * the mutex which can lead to the page being captured and eventually 7271 * end up here. As we may be hashing out the old page and hashing into 7272 * the retire vnode, we need to make sure we don't own them. 7273 * Other callbacks who do hash operations also need to make sure that 7274 * before they hashin to a vnode that they do not currently own the 7275 * vphm mutex otherwise there will be a panic. 7276 */ 7277 if (mutex_owned(page_vnode_mutex(&retired_pages))) { 7278 page_unlock_nocapture(pp); 7279 return; 7280 } 7281 if (pp->p_vnode != NULL && mutex_owned(page_vnode_mutex(pp->p_vnode))) { 7282 page_unlock_nocapture(pp); 7283 return; 7284 } 7285 7286 index = PAGE_CAPTURE_HASH(pp); 7287 7288 mp = &page_capture_hash[index].pchh_mutex; 7289 mutex_enter(mp); 7290 for (i = 0; i < 2; i++) { 7291 bp = page_capture_hash[index].lists[i].next; 7292 while (bp != &page_capture_hash[index].lists[i]) { 7293 if (bp->pp == pp) { 7294 szc = bp->szc; 7295 flags = bp->flags | CAPTURE_ASYNC; 7296 datap = bp->datap; 7297 mutex_exit(mp); 7298 (void) page_trycapture(pp, szc, flags, datap); 7299 return; 7300 } 7301 bp = bp->next; 7302 } 7303 } 7304 7305 /* Failed to find page in hash so clear flags and unlock it. */ 7306 page_clrtoxic(pp, PR_CAPTURE); 7307 page_unlock(pp); 7308 7309 mutex_exit(mp); 7310 } 7311 7312 void 7313 page_capture_init() 7314 { 7315 int i; 7316 for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 7317 page_capture_hash[i].lists[0].next = 7318 &page_capture_hash[i].lists[0]; 7319 page_capture_hash[i].lists[0].prev = 7320 &page_capture_hash[i].lists[0]; 7321 page_capture_hash[i].lists[1].next = 7322 &page_capture_hash[i].lists[1]; 7323 page_capture_hash[i].lists[1].prev = 7324 &page_capture_hash[i].lists[1]; 7325 } 7326 7327 pc_thread_shortwait = 23 * hz; 7328 pc_thread_longwait = 1201 * hz; 7329 pc_thread_ism_retry = 3; 7330 mutex_init(&pc_thread_mutex, NULL, MUTEX_DEFAULT, NULL); 7331 cv_init(&pc_cv, NULL, CV_DEFAULT, NULL); 7332 pc_thread_id = thread_create(NULL, 0, page_capture_thread, NULL, 0, &p0, 7333 TS_RUN, minclsyspri); 7334 } 7335 7336 /* 7337 * It is necessary to scrub any failing pages prior to reboot in order to 7338 * prevent a latent error trap from occurring on the next boot. 7339 */ 7340 void 7341 page_retire_mdboot() 7342 { 7343 page_t *pp; 7344 int i, j; 7345 page_capture_hash_bucket_t *bp; 7346 7347 /* walk lists looking for pages to scrub */ 7348 for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 7349 if (page_capture_hash[i].num_pages == 0) 7350 continue; 7351 7352 mutex_enter(&page_capture_hash[i].pchh_mutex); 7353 7354 for (j = 0; j < 2; j++) { 7355 bp = page_capture_hash[i].lists[j].next; 7356 while (bp != &page_capture_hash[i].lists[j]) { 7357 pp = bp->pp; 7358 if (!PP_ISKAS(pp) && PP_TOXIC(pp)) { 7359 pp->p_selock = -1; /* pacify ASSERTs */ 7360 PP_CLRFREE(pp); 7361 pagescrub(pp, 0, PAGESIZE); 7362 pp->p_selock = 0; 7363 } 7364 bp = bp->next; 7365 } 7366 } 7367 mutex_exit(&page_capture_hash[i].pchh_mutex); 7368 } 7369 } 7370 7371 /* 7372 * Walk the page_capture_hash trying to capture pages and also cleanup old 7373 * entries which have expired. 7374 */ 7375 void 7376 page_capture_async() 7377 { 7378 page_t *pp; 7379 int i; 7380 int ret; 7381 page_capture_hash_bucket_t *bp1, *bp2; 7382 uint_t szc; 7383 uint_t flags; 7384 void *datap; 7385 7386 /* If there are outstanding pages to be captured, get to work */ 7387 for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 7388 if (page_capture_hash[i].num_pages == 0) 7389 continue; 7390 /* Append list 1 to list 0 and then walk through list 0 */ 7391 mutex_enter(&page_capture_hash[i].pchh_mutex); 7392 bp1 = &page_capture_hash[i].lists[1]; 7393 bp2 = bp1->next; 7394 if (bp1 != bp2) { 7395 bp1->prev->next = page_capture_hash[i].lists[0].next; 7396 bp2->prev = &page_capture_hash[i].lists[0]; 7397 page_capture_hash[i].lists[0].next->prev = bp1->prev; 7398 page_capture_hash[i].lists[0].next = bp2; 7399 bp1->next = bp1; 7400 bp1->prev = bp1; 7401 } 7402 7403 /* list[1] will be empty now */ 7404 7405 bp1 = page_capture_hash[i].lists[0].next; 7406 while (bp1 != &page_capture_hash[i].lists[0]) { 7407 /* Check expiration time */ 7408 if ((lbolt > bp1->expires && bp1->expires != -1) || 7409 page_deleted(bp1->pp)) { 7410 page_capture_hash[i].lists[0].next = bp1->next; 7411 bp1->next->prev = 7412 &page_capture_hash[i].lists[0]; 7413 page_capture_hash[i].num_pages--; 7414 7415 /* 7416 * We can safely remove the PR_CAPTURE bit 7417 * without holding the EXCL lock on the page 7418 * as the PR_CAPTURE bit requres that the 7419 * page_capture_hash[].pchh_mutex be held 7420 * to modify it. 7421 */ 7422 page_clrtoxic(bp1->pp, PR_CAPTURE); 7423 mutex_exit(&page_capture_hash[i].pchh_mutex); 7424 kmem_free(bp1, sizeof (*bp1)); 7425 mutex_enter(&page_capture_hash[i].pchh_mutex); 7426 bp1 = page_capture_hash[i].lists[0].next; 7427 continue; 7428 } 7429 pp = bp1->pp; 7430 szc = bp1->szc; 7431 flags = bp1->flags; 7432 datap = bp1->datap; 7433 mutex_exit(&page_capture_hash[i].pchh_mutex); 7434 if (page_trylock(pp, SE_EXCL)) { 7435 ret = page_trycapture(pp, szc, 7436 flags | CAPTURE_ASYNC, datap); 7437 } else { 7438 ret = 1; /* move to walked hash */ 7439 } 7440 7441 if (ret != 0) { 7442 /* Move to walked hash */ 7443 (void) page_capture_move_to_walked(pp); 7444 } 7445 mutex_enter(&page_capture_hash[i].pchh_mutex); 7446 bp1 = page_capture_hash[i].lists[0].next; 7447 } 7448 7449 mutex_exit(&page_capture_hash[i].pchh_mutex); 7450 } 7451 } 7452 7453 /* 7454 * This function is called by the page_capture_thread, and is needed in 7455 * in order to initiate aio cleanup, so that pages used in aio 7456 * will be unlocked and subsequently retired by page_capture_thread. 7457 */ 7458 static int 7459 do_aio_cleanup(void) 7460 { 7461 proc_t *procp; 7462 int (*aio_cleanup_dr_delete_memory)(proc_t *); 7463 int cleaned = 0; 7464 7465 if (modload("sys", "kaio") == -1) { 7466 cmn_err(CE_WARN, "do_aio_cleanup: cannot load kaio"); 7467 return (0); 7468 } 7469 /* 7470 * We use the aio_cleanup_dr_delete_memory function to 7471 * initiate the actual clean up; this function will wake 7472 * up the per-process aio_cleanup_thread. 7473 */ 7474 aio_cleanup_dr_delete_memory = (int (*)(proc_t *)) 7475 modgetsymvalue("aio_cleanup_dr_delete_memory", 0); 7476 if (aio_cleanup_dr_delete_memory == NULL) { 7477 cmn_err(CE_WARN, 7478 "aio_cleanup_dr_delete_memory not found in kaio"); 7479 return (0); 7480 } 7481 mutex_enter(&pidlock); 7482 for (procp = practive; (procp != NULL); procp = procp->p_next) { 7483 mutex_enter(&procp->p_lock); 7484 if (procp->p_aio != NULL) { 7485 /* cleanup proc's outstanding kaio */ 7486 cleaned += (*aio_cleanup_dr_delete_memory)(procp); 7487 } 7488 mutex_exit(&procp->p_lock); 7489 } 7490 mutex_exit(&pidlock); 7491 return (cleaned); 7492 } 7493 7494 /* 7495 * helper function for page_capture_thread 7496 */ 7497 static void 7498 page_capture_handle_outstanding(void) 7499 { 7500 extern size_t spt_used; 7501 int ntry; 7502 7503 if (!page_retire_pend_count()) { 7504 /* 7505 * Do we really want to be this aggressive 7506 * for things other than page_retire? 7507 * Maybe have a counter for each callback 7508 * type to guide how aggressive we should 7509 * be here. Thus if there's at least one 7510 * page for page_retire we go ahead and reap 7511 * like this. 7512 */ 7513 kmem_reap(); 7514 seg_preap(); 7515 page_capture_async(); 7516 } else { 7517 /* 7518 * There are pages pending retirement, so 7519 * we reap prior to attempting to capture. 7520 */ 7521 kmem_reap(); 7522 /* 7523 * When ISM is in use, we need to disable and 7524 * purge the seg_pcache, and initiate aio 7525 * cleanup in order to release page locks and 7526 * subsquently retire pages in need of 7527 * retirement. 7528 */ 7529 if (spt_used) { 7530 /* disable and purge seg_pcache */ 7531 (void) seg_p_disable(); 7532 for (ntry = 0; ntry < pc_thread_ism_retry; ntry++) { 7533 if (!page_retire_pend_count()) 7534 break; 7535 if (do_aio_cleanup()) { 7536 /* 7537 * allow the apps cleanup threads 7538 * to run 7539 */ 7540 delay(pc_thread_shortwait); 7541 } 7542 page_capture_async(); 7543 } 7544 /* reenable seg_pcache */ 7545 seg_p_enable(); 7546 } else { 7547 seg_preap(); 7548 page_capture_async(); 7549 } 7550 } 7551 } 7552 7553 /* 7554 * The page_capture_thread loops forever, looking to see if there are 7555 * pages still waiting to be captured. 7556 */ 7557 static void 7558 page_capture_thread(void) 7559 { 7560 callb_cpr_t c; 7561 int outstanding; 7562 int i; 7563 7564 CALLB_CPR_INIT(&c, &pc_thread_mutex, callb_generic_cpr, "page_capture"); 7565 7566 mutex_enter(&pc_thread_mutex); 7567 for (;;) { 7568 outstanding = 0; 7569 for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) 7570 outstanding += page_capture_hash[i].num_pages; 7571 if (outstanding) { 7572 page_capture_handle_outstanding(); 7573 CALLB_CPR_SAFE_BEGIN(&c); 7574 (void) cv_timedwait(&pc_cv, &pc_thread_mutex, 7575 lbolt + pc_thread_shortwait); 7576 CALLB_CPR_SAFE_END(&c, &pc_thread_mutex); 7577 } else { 7578 CALLB_CPR_SAFE_BEGIN(&c); 7579 (void) cv_timedwait(&pc_cv, &pc_thread_mutex, 7580 lbolt + pc_thread_longwait); 7581 CALLB_CPR_SAFE_END(&c, &pc_thread_mutex); 7582 } 7583 } 7584 /*NOTREACHED*/ 7585 } 7586