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) 1984, 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 - anonymous pages. 43 * 44 * This layer sits immediately above the vm_swap layer. It manages 45 * physical pages that have no permanent identity in the file system 46 * name space, using the services of the vm_swap layer to allocate 47 * backing storage for these pages. Since these pages have no external 48 * identity, they are discarded when the last reference is removed. 49 * 50 * An important function of this layer is to manage low-level sharing 51 * of pages that are logically distinct but that happen to be 52 * physically identical (e.g., the corresponding pages of the processes 53 * resulting from a fork before one process or the other changes their 54 * contents). This pseudo-sharing is present only as an optimization 55 * and is not to be confused with true sharing in which multiple 56 * address spaces deliberately contain references to the same object; 57 * such sharing is managed at a higher level. 58 * 59 * The key data structure here is the anon struct, which contains a 60 * reference count for its associated physical page and a hint about 61 * the identity of that page. Anon structs typically live in arrays, 62 * with an instance's position in its array determining where the 63 * corresponding backing storage is allocated; however, the swap_xlate() 64 * routine abstracts away this representation information so that the 65 * rest of the anon layer need not know it. (See the swap layer for 66 * more details on anon struct layout.) 67 * 68 * In the future versions of the system, the association between an 69 * anon struct and its position on backing store will change so that 70 * we don't require backing store all anonymous pages in the system. 71 * This is important for consideration for large memory systems. 72 * We can also use this technique to delay binding physical locations 73 * to anonymous pages until pageout/swapout time where we can make 74 * smarter allocation decisions to improve anonymous klustering. 75 * 76 * Many of the routines defined here take a (struct anon **) argument, 77 * which allows the code at this level to manage anon pages directly, 78 * so that callers can regard anon structs as opaque objects and not be 79 * concerned with assigning or inspecting their contents. 80 * 81 * Clients of this layer refer to anon pages indirectly. That is, they 82 * maintain arrays of pointers to anon structs rather than maintaining 83 * anon structs themselves. The (struct anon **) arguments mentioned 84 * above are pointers to entries in these arrays. It is these arrays 85 * that capture the mapping between offsets within a given segment and 86 * the corresponding anonymous backing storage address. 87 */ 88 89 #ifdef DEBUG 90 #define ANON_DEBUG 91 #endif 92 93 #include <sys/types.h> 94 #include <sys/t_lock.h> 95 #include <sys/param.h> 96 #include <sys/systm.h> 97 #include <sys/mman.h> 98 #include <sys/cred.h> 99 #include <sys/thread.h> 100 #include <sys/vnode.h> 101 #include <sys/cpuvar.h> 102 #include <sys/swap.h> 103 #include <sys/cmn_err.h> 104 #include <sys/vtrace.h> 105 #include <sys/kmem.h> 106 #include <sys/sysmacros.h> 107 #include <sys/bitmap.h> 108 #include <sys/vmsystm.h> 109 #include <sys/debug.h> 110 #include <sys/fs/swapnode.h> 111 #include <sys/tnf_probe.h> 112 #include <sys/lgrp.h> 113 #include <sys/policy.h> 114 #include <sys/condvar_impl.h> 115 #include <sys/mutex_impl.h> 116 #include <sys/rctl.h> 117 118 #include <vm/as.h> 119 #include <vm/hat.h> 120 #include <vm/anon.h> 121 #include <vm/page.h> 122 #include <vm/vpage.h> 123 #include <vm/seg.h> 124 #include <vm/rm.h> 125 126 #include <fs/fs_subr.h> 127 128 struct vnode *anon_vp; 129 130 int anon_debug; 131 132 kmutex_t anoninfo_lock; 133 struct k_anoninfo k_anoninfo; 134 ani_free_t ani_free_pool[ANI_MAX_POOL]; 135 pad_mutex_t anon_array_lock[ANON_LOCKSIZE]; 136 kcondvar_t anon_array_cv[ANON_LOCKSIZE]; 137 138 /* 139 * Global hash table for (vp, off) -> anon slot 140 */ 141 extern int swap_maxcontig; 142 size_t anon_hash_size; 143 struct anon **anon_hash; 144 145 static struct kmem_cache *anon_cache; 146 static struct kmem_cache *anonmap_cache; 147 148 #ifdef VM_STATS 149 static struct anonvmstats_str { 150 ulong_t getpages[30]; 151 ulong_t privatepages[10]; 152 ulong_t demotepages[9]; 153 ulong_t decrefpages[9]; 154 ulong_t dupfillholes[4]; 155 ulong_t freepages[1]; 156 } anonvmstats; 157 #endif /* VM_STATS */ 158 159 160 /*ARGSUSED*/ 161 static int 162 anonmap_cache_constructor(void *buf, void *cdrarg, int kmflags) 163 { 164 struct anon_map *amp = buf; 165 166 rw_init(&->a_rwlock, NULL, RW_DEFAULT, NULL); 167 return (0); 168 } 169 170 /*ARGSUSED1*/ 171 static void 172 anonmap_cache_destructor(void *buf, void *cdrarg) 173 { 174 struct anon_map *amp = buf; 175 176 rw_destroy(&->a_rwlock); 177 } 178 179 kmutex_t anonhash_lock[AH_LOCK_SIZE]; 180 kmutex_t anonpages_hash_lock[AH_LOCK_SIZE]; 181 182 void 183 anon_init(void) 184 { 185 int i; 186 187 anon_hash_size = 1L << highbit(physmem / ANON_HASHAVELEN); 188 189 for (i = 0; i < AH_LOCK_SIZE; i++) { 190 mutex_init(&anonhash_lock[i], NULL, MUTEX_DEFAULT, NULL); 191 mutex_init(&anonpages_hash_lock[i], NULL, MUTEX_DEFAULT, NULL); 192 } 193 194 for (i = 0; i < ANON_LOCKSIZE; i++) { 195 mutex_init(&anon_array_lock[i].pad_mutex, NULL, 196 MUTEX_DEFAULT, NULL); 197 cv_init(&anon_array_cv[i], NULL, CV_DEFAULT, NULL); 198 } 199 200 anon_hash = (struct anon **) 201 kmem_zalloc(sizeof (struct anon *) * anon_hash_size, KM_SLEEP); 202 anon_cache = kmem_cache_create("anon_cache", sizeof (struct anon), 203 AN_CACHE_ALIGN, NULL, NULL, NULL, NULL, NULL, 0); 204 anonmap_cache = kmem_cache_create("anonmap_cache", 205 sizeof (struct anon_map), 0, 206 anonmap_cache_constructor, anonmap_cache_destructor, NULL, 207 NULL, NULL, 0); 208 swap_maxcontig = (1024 * 1024) >> PAGESHIFT; /* 1MB of pages */ 209 210 anon_vp = vn_alloc(KM_SLEEP); 211 vn_setops(anon_vp, swap_vnodeops); 212 anon_vp->v_type = VREG; 213 anon_vp->v_flag |= (VISSWAP|VISSWAPFS); 214 } 215 216 /* 217 * Global anon slot hash table manipulation. 218 */ 219 220 static void 221 anon_addhash(struct anon *ap) 222 { 223 int index; 224 225 ASSERT(MUTEX_HELD(&anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)])); 226 index = ANON_HASH(ap->an_vp, ap->an_off); 227 ap->an_hash = anon_hash[index]; 228 anon_hash[index] = ap; 229 } 230 231 static void 232 anon_rmhash(struct anon *ap) 233 { 234 struct anon **app; 235 236 ASSERT(MUTEX_HELD(&anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)])); 237 238 for (app = &anon_hash[ANON_HASH(ap->an_vp, ap->an_off)]; 239 *app; app = &((*app)->an_hash)) { 240 if (*app == ap) { 241 *app = ap->an_hash; 242 break; 243 } 244 } 245 } 246 247 /* 248 * The anon array interfaces. Functions allocating, 249 * freeing array of pointers, and returning/setting 250 * entries in the array of pointers for a given offset. 251 * 252 * Create the list of pointers 253 */ 254 struct anon_hdr * 255 anon_create(pgcnt_t npages, int flags) 256 { 257 struct anon_hdr *ahp; 258 ulong_t nchunks; 259 int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP; 260 261 if ((ahp = kmem_zalloc(sizeof (struct anon_hdr), kmemflags)) == NULL) { 262 return (NULL); 263 } 264 265 mutex_init(&ahp->serial_lock, NULL, MUTEX_DEFAULT, NULL); 266 /* 267 * Single level case. 268 */ 269 ahp->size = npages; 270 if (npages <= ANON_CHUNK_SIZE || (flags & ANON_ALLOC_FORCE)) { 271 272 if (flags & ANON_ALLOC_FORCE) 273 ahp->flags |= ANON_ALLOC_FORCE; 274 275 ahp->array_chunk = kmem_zalloc( 276 ahp->size * sizeof (struct anon *), kmemflags); 277 278 if (ahp->array_chunk == NULL) { 279 kmem_free(ahp, sizeof (struct anon_hdr)); 280 return (NULL); 281 } 282 } else { 283 /* 284 * 2 Level case. 285 * anon hdr size needs to be rounded off to be a multiple 286 * of ANON_CHUNK_SIZE. This is important as various anon 287 * related functions depend on this. 288 * NOTE - 289 * anon_grow() makes anon hdr size a multiple of 290 * ANON_CHUNK_SIZE. 291 * amp size is <= anon hdr size. 292 * anon_index + seg_pgs <= anon hdr size. 293 */ 294 ahp->size = P2ROUNDUP(npages, ANON_CHUNK_SIZE); 295 nchunks = ahp->size >> ANON_CHUNK_SHIFT; 296 297 ahp->array_chunk = kmem_zalloc(nchunks * sizeof (ulong_t *), 298 kmemflags); 299 300 if (ahp->array_chunk == NULL) { 301 kmem_free(ahp, sizeof (struct anon_hdr)); 302 return (NULL); 303 } 304 } 305 return (ahp); 306 } 307 308 /* 309 * Free the array of pointers 310 */ 311 void 312 anon_release(struct anon_hdr *ahp, pgcnt_t npages) 313 { 314 ulong_t i; 315 void **ppp; 316 ulong_t nchunks; 317 318 ASSERT(npages <= ahp->size); 319 320 /* 321 * Single level case. 322 */ 323 if (npages <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) { 324 kmem_free(ahp->array_chunk, ahp->size * sizeof (struct anon *)); 325 } else { 326 /* 327 * 2 level case. 328 */ 329 nchunks = ahp->size >> ANON_CHUNK_SHIFT; 330 for (i = 0; i < nchunks; i++) { 331 ppp = &ahp->array_chunk[i]; 332 if (*ppp != NULL) 333 kmem_free(*ppp, PAGESIZE); 334 } 335 kmem_free(ahp->array_chunk, nchunks * sizeof (ulong_t *)); 336 } 337 mutex_destroy(&ahp->serial_lock); 338 kmem_free(ahp, sizeof (struct anon_hdr)); 339 } 340 341 /* 342 * Return the pointer from the list for a 343 * specified anon index. 344 */ 345 struct anon * 346 anon_get_ptr(struct anon_hdr *ahp, ulong_t an_idx) 347 { 348 struct anon **app; 349 350 ASSERT(an_idx < ahp->size); 351 352 /* 353 * Single level case. 354 */ 355 if ((ahp->size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) { 356 return ((struct anon *) 357 ((uintptr_t)ahp->array_chunk[an_idx] & ANON_PTRMASK)); 358 } else { 359 360 /* 361 * 2 level case. 362 */ 363 app = ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT]; 364 if (app) { 365 return ((struct anon *) 366 ((uintptr_t)app[an_idx & ANON_CHUNK_OFF] & 367 ANON_PTRMASK)); 368 } else { 369 return (NULL); 370 } 371 } 372 } 373 374 /* 375 * Return the anon pointer for the first valid entry in the anon list, 376 * starting from the given index. 377 */ 378 struct anon * 379 anon_get_next_ptr(struct anon_hdr *ahp, ulong_t *index) 380 { 381 struct anon *ap; 382 struct anon **app; 383 ulong_t chunkoff; 384 ulong_t i; 385 ulong_t j; 386 pgcnt_t size; 387 388 i = *index; 389 size = ahp->size; 390 391 ASSERT(i < size); 392 393 if ((size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) { 394 /* 395 * 1 level case 396 */ 397 while (i < size) { 398 ap = (struct anon *) 399 ((uintptr_t)ahp->array_chunk[i] & ANON_PTRMASK); 400 if (ap) { 401 *index = i; 402 return (ap); 403 } 404 i++; 405 } 406 } else { 407 /* 408 * 2 level case 409 */ 410 chunkoff = i & ANON_CHUNK_OFF; 411 while (i < size) { 412 app = ahp->array_chunk[i >> ANON_CHUNK_SHIFT]; 413 if (app) 414 for (j = chunkoff; j < ANON_CHUNK_SIZE; j++) { 415 ap = (struct anon *) 416 ((uintptr_t)app[j] & 417 ANON_PTRMASK); 418 if (ap) { 419 *index = i + (j - chunkoff); 420 return (ap); 421 } 422 } 423 chunkoff = 0; 424 i = (i + ANON_CHUNK_SIZE) & ~ANON_CHUNK_OFF; 425 } 426 } 427 *index = size; 428 return (NULL); 429 } 430 431 /* 432 * Set list entry with a given pointer for a specified offset 433 */ 434 int 435 anon_set_ptr(struct anon_hdr *ahp, ulong_t an_idx, struct anon *ap, int flags) 436 { 437 void **ppp; 438 struct anon **app; 439 int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP; 440 uintptr_t *ap_addr; 441 442 ASSERT(an_idx < ahp->size); 443 444 /* 445 * Single level case. 446 */ 447 if (ahp->size <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) { 448 ap_addr = (uintptr_t *)&ahp->array_chunk[an_idx]; 449 } else { 450 451 /* 452 * 2 level case. 453 */ 454 ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT]; 455 456 ASSERT(ppp != NULL); 457 if (*ppp == NULL) { 458 mutex_enter(&ahp->serial_lock); 459 ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT]; 460 if (*ppp == NULL) { 461 *ppp = kmem_zalloc(PAGESIZE, kmemflags); 462 if (*ppp == NULL) { 463 mutex_exit(&ahp->serial_lock); 464 return (ENOMEM); 465 } 466 } 467 mutex_exit(&ahp->serial_lock); 468 } 469 app = *ppp; 470 ap_addr = (uintptr_t *)&app[an_idx & ANON_CHUNK_OFF]; 471 } 472 *ap_addr = (*ap_addr & ~ANON_PTRMASK) | (uintptr_t)ap; 473 return (0); 474 } 475 476 /* 477 * Copy anon array into a given new anon array 478 */ 479 int 480 anon_copy_ptr(struct anon_hdr *sahp, ulong_t s_idx, 481 struct anon_hdr *dahp, ulong_t d_idx, 482 pgcnt_t npages, int flags) 483 { 484 void **sapp, **dapp; 485 void *ap; 486 int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP; 487 488 ASSERT((s_idx < sahp->size) && (d_idx < dahp->size)); 489 ASSERT((npages <= sahp->size) && (npages <= dahp->size)); 490 491 /* 492 * Both arrays are 1 level. 493 */ 494 if (((sahp->size <= ANON_CHUNK_SIZE) && 495 (dahp->size <= ANON_CHUNK_SIZE)) || 496 ((sahp->flags & ANON_ALLOC_FORCE) && 497 (dahp->flags & ANON_ALLOC_FORCE))) { 498 499 bcopy(&sahp->array_chunk[s_idx], &dahp->array_chunk[d_idx], 500 npages * sizeof (struct anon *)); 501 return (0); 502 } 503 504 /* 505 * Both arrays are 2 levels. 506 */ 507 if (sahp->size > ANON_CHUNK_SIZE && 508 dahp->size > ANON_CHUNK_SIZE && 509 ((sahp->flags & ANON_ALLOC_FORCE) == 0) && 510 ((dahp->flags & ANON_ALLOC_FORCE) == 0)) { 511 512 ulong_t sapidx, dapidx; 513 ulong_t *sap, *dap; 514 ulong_t chknp; 515 516 while (npages != 0) { 517 518 sapidx = s_idx & ANON_CHUNK_OFF; 519 dapidx = d_idx & ANON_CHUNK_OFF; 520 chknp = ANON_CHUNK_SIZE - MAX(sapidx, dapidx); 521 if (chknp > npages) 522 chknp = npages; 523 524 sapp = &sahp->array_chunk[s_idx >> ANON_CHUNK_SHIFT]; 525 if ((sap = *sapp) != NULL) { 526 dapp = &dahp->array_chunk[d_idx 527 >> ANON_CHUNK_SHIFT]; 528 if ((dap = *dapp) == NULL) { 529 *dapp = kmem_zalloc(PAGESIZE, 530 kmemflags); 531 if ((dap = *dapp) == NULL) 532 return (ENOMEM); 533 } 534 bcopy((sap + sapidx), (dap + dapidx), 535 chknp << ANON_PTRSHIFT); 536 } 537 s_idx += chknp; 538 d_idx += chknp; 539 npages -= chknp; 540 } 541 return (0); 542 } 543 544 /* 545 * At least one of the arrays is 2 level. 546 */ 547 while (npages--) { 548 if ((ap = anon_get_ptr(sahp, s_idx)) != NULL) { 549 ASSERT(!ANON_ISBUSY(anon_get_slot(sahp, s_idx))); 550 if (anon_set_ptr(dahp, d_idx, ap, flags) == ENOMEM) 551 return (ENOMEM); 552 } 553 s_idx++; 554 d_idx++; 555 } 556 return (0); 557 } 558 559 560 /* 561 * ANON_INITBUF is a convenience macro for anon_grow() below. It 562 * takes a buffer dst, which is at least as large as buffer src. It 563 * does a bcopy from src into dst, and then bzeros the extra bytes 564 * of dst. If tail is set, the data in src is tail aligned within 565 * dst instead of head aligned. 566 */ 567 568 #define ANON_INITBUF(src, srclen, dst, dstsize, tail) \ 569 if (tail) { \ 570 bzero((dst), (dstsize) - (srclen)); \ 571 bcopy((src), (char *)(dst) + (dstsize) - (srclen), (srclen)); \ 572 } else { \ 573 bcopy((src), (dst), (srclen)); \ 574 bzero((char *)(dst) + (srclen), (dstsize) - (srclen)); \ 575 } 576 577 #define ANON_1_LEVEL_INC (ANON_CHUNK_SIZE / 8) 578 #define ANON_2_LEVEL_INC (ANON_1_LEVEL_INC * ANON_CHUNK_SIZE) 579 580 /* 581 * anon_grow() is used to efficiently extend an existing anon array. 582 * startidx_p points to the index into the anon array of the first page 583 * that is in use. oldseg_pgs is the number of pages in use, starting at 584 * *startidx_p. newpages is the number of additional pages desired. 585 * 586 * If startidx_p == NULL, startidx is taken to be 0 and cannot be changed. 587 * 588 * The growth is done by creating a new top level of the anon array, 589 * and (if the array is 2-level) reusing the existing second level arrays. 590 * 591 * flags can be used to specify ANON_NOSLEEP and ANON_GROWDOWN. 592 * 593 * Returns the new number of pages in the anon array. 594 */ 595 pgcnt_t 596 anon_grow(struct anon_hdr *ahp, ulong_t *startidx_p, pgcnt_t oldseg_pgs, 597 pgcnt_t newseg_pgs, int flags) 598 { 599 ulong_t startidx = startidx_p ? *startidx_p : 0; 600 pgcnt_t oldamp_pgs = ahp->size, newamp_pgs; 601 pgcnt_t oelems, nelems, totpages; 602 void **level1; 603 int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP; 604 int growdown = (flags & ANON_GROWDOWN); 605 size_t newarrsz, oldarrsz; 606 void *level2; 607 608 ASSERT(!(startidx_p == NULL && growdown)); 609 ASSERT(startidx + oldseg_pgs <= ahp->size); 610 611 /* 612 * Determine the total number of pages needed in the new 613 * anon array. If growing down, totpages is all pages from 614 * startidx through the end of the array, plus <newseg_pgs> 615 * pages. If growing up, keep all pages from page 0 through 616 * the last page currently in use, plus <newseg_pgs> pages. 617 */ 618 if (growdown) 619 totpages = oldamp_pgs - startidx + newseg_pgs; 620 else 621 totpages = startidx + oldseg_pgs + newseg_pgs; 622 623 /* If the array is already large enough, just return. */ 624 625 if (oldamp_pgs >= totpages) { 626 if (growdown) 627 *startidx_p = oldamp_pgs - totpages; 628 return (oldamp_pgs); 629 } 630 631 /* 632 * oldamp_pgs/newamp_pgs are the total numbers of pages represented 633 * by the corresponding arrays. 634 * oelems/nelems are the number of pointers in the top level arrays 635 * which may be either level 1 or level 2. 636 * Will the new anon array be one level or two levels? 637 */ 638 if (totpages <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) { 639 newamp_pgs = P2ROUNDUP(totpages, ANON_1_LEVEL_INC); 640 oelems = oldamp_pgs; 641 nelems = newamp_pgs; 642 } else { 643 newamp_pgs = P2ROUNDUP(totpages, ANON_2_LEVEL_INC); 644 oelems = (oldamp_pgs + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT; 645 nelems = newamp_pgs >> ANON_CHUNK_SHIFT; 646 } 647 648 newarrsz = nelems * sizeof (void *); 649 level1 = kmem_alloc(newarrsz, kmemflags); 650 if (level1 == NULL) 651 return (0); 652 653 /* Are we converting from a one level to a two level anon array? */ 654 655 if (newamp_pgs > ANON_CHUNK_SIZE && oldamp_pgs <= ANON_CHUNK_SIZE && 656 !(ahp->flags & ANON_ALLOC_FORCE)) { 657 658 /* 659 * Yes, we're converting to a two level. Reuse old level 1 660 * as new level 2 if it is exactly PAGESIZE. Otherwise 661 * alloc a new level 2 and copy the old level 1 data into it. 662 */ 663 if (oldamp_pgs == ANON_CHUNK_SIZE) { 664 level2 = (void *)ahp->array_chunk; 665 } else { 666 level2 = kmem_alloc(PAGESIZE, kmemflags); 667 if (level2 == NULL) { 668 kmem_free(level1, newarrsz); 669 return (0); 670 } 671 oldarrsz = oldamp_pgs * sizeof (void *); 672 673 ANON_INITBUF(ahp->array_chunk, oldarrsz, 674 level2, PAGESIZE, growdown); 675 kmem_free(ahp->array_chunk, oldarrsz); 676 } 677 bzero(level1, newarrsz); 678 if (growdown) 679 level1[nelems - 1] = level2; 680 else 681 level1[0] = level2; 682 } else { 683 oldarrsz = oelems * sizeof (void *); 684 685 ANON_INITBUF(ahp->array_chunk, oldarrsz, 686 level1, newarrsz, growdown); 687 kmem_free(ahp->array_chunk, oldarrsz); 688 } 689 690 ahp->array_chunk = level1; 691 ahp->size = newamp_pgs; 692 if (growdown) 693 *startidx_p = newamp_pgs - totpages; 694 695 return (newamp_pgs); 696 } 697 698 699 /* 700 * Called from clock handler to sync ani_free value. 701 */ 702 703 void 704 set_anoninfo(void) 705 { 706 int ix; 707 pgcnt_t total = 0; 708 709 for (ix = 0; ix < ANI_MAX_POOL; ix++) { 710 total += ani_free_pool[ix].ani_count; 711 } 712 k_anoninfo.ani_free = total; 713 } 714 715 /* 716 * Reserve anon space. 717 * 718 * It's no longer simply a matter of incrementing ani_resv to 719 * reserve swap space, we need to check memory-based as well 720 * as disk-backed (physical) swap. The following algorithm 721 * is used: 722 * Check the space on physical swap 723 * i.e. amount needed < ani_max - ani_phys_resv 724 * If we are swapping on swapfs check 725 * amount needed < (availrmem - swapfs_minfree) 726 * Since the algorithm to check for the quantity of swap space is 727 * almost the same as that for reserving it, we'll just use anon_resvmem 728 * with a flag to decrement availrmem. 729 * 730 * Return non-zero on success. 731 */ 732 int 733 anon_resvmem(size_t size, boolean_t takemem, zone_t *zone, int tryhard) 734 { 735 pgcnt_t npages = btopr(size); 736 pgcnt_t mswap_pages = 0; 737 pgcnt_t pswap_pages = 0; 738 proc_t *p = curproc; 739 740 if (zone != NULL && takemem) { 741 /* test zone.max-swap resource control */ 742 mutex_enter(&p->p_lock); 743 if (rctl_incr_swap(p, zone, ptob(npages)) != 0) { 744 mutex_exit(&p->p_lock); 745 return (0); 746 } 747 mutex_exit(&p->p_lock); 748 } 749 mutex_enter(&anoninfo_lock); 750 751 /* 752 * pswap_pages is the number of pages we can take from 753 * physical (i.e. disk-backed) swap. 754 */ 755 ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 756 pswap_pages = k_anoninfo.ani_max - k_anoninfo.ani_phys_resv; 757 758 ANON_PRINT(A_RESV, 759 ("anon_resvmem: npages %lu takemem %u pswap %lu caller %p\n", 760 npages, takemem, pswap_pages, (void *)caller())); 761 762 if (npages <= pswap_pages) { 763 /* 764 * we have enough space on a physical swap 765 */ 766 if (takemem) 767 k_anoninfo.ani_phys_resv += npages; 768 mutex_exit(&anoninfo_lock); 769 return (1); 770 } else if (pswap_pages != 0) { 771 /* 772 * we have some space on a physical swap 773 */ 774 if (takemem) { 775 /* 776 * use up remainder of phys swap 777 */ 778 k_anoninfo.ani_phys_resv += pswap_pages; 779 ASSERT(k_anoninfo.ani_phys_resv == k_anoninfo.ani_max); 780 } 781 } 782 /* 783 * since (npages > pswap_pages) we need mem swap 784 * mswap_pages is the number of pages needed from availrmem 785 */ 786 ASSERT(npages > pswap_pages); 787 mswap_pages = npages - pswap_pages; 788 789 ANON_PRINT(A_RESV, ("anon_resvmem: need %ld pages from memory\n", 790 mswap_pages)); 791 792 /* 793 * priv processes can reserve memory as swap as long as availrmem 794 * remains greater than swapfs_minfree; in the case of non-priv 795 * processes, memory can be reserved as swap only if availrmem 796 * doesn't fall below (swapfs_minfree + swapfs_reserve). Thus, 797 * swapfs_reserve amount of memswap is not available to non-priv 798 * processes. This protects daemons such as automounter dying 799 * as a result of application processes eating away almost entire 800 * membased swap. This safeguard becomes useless if apps are run 801 * with root access. 802 * 803 * swapfs_reserve is minimum of 4Mb or 1/16 of physmem. 804 * 805 */ 806 if (tryhard) { 807 mutex_exit(&anoninfo_lock); 808 (void) page_reclaim_mem(mswap_pages, 809 swapfs_minfree + swapfs_reserve, 0); 810 mutex_enter(&anoninfo_lock); 811 } 812 813 mutex_enter(&freemem_lock); 814 if (availrmem > (swapfs_minfree + swapfs_reserve + mswap_pages) || 815 (availrmem > (swapfs_minfree + mswap_pages) && 816 secpolicy_resource(CRED()) == 0)) { 817 818 if (takemem) { 819 /* 820 * Take the memory from the rest of the system. 821 */ 822 availrmem -= mswap_pages; 823 mutex_exit(&freemem_lock); 824 k_anoninfo.ani_mem_resv += mswap_pages; 825 ANI_ADD(mswap_pages); 826 ANON_PRINT((A_RESV | A_MRESV), 827 ("anon_resvmem: took %ld pages of availrmem\n", 828 mswap_pages)); 829 } else { 830 mutex_exit(&freemem_lock); 831 } 832 833 ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 834 mutex_exit(&anoninfo_lock); 835 return (1); 836 837 } else { 838 /* 839 * Fail if not enough memory 840 */ 841 842 if (takemem) { 843 k_anoninfo.ani_phys_resv -= pswap_pages; 844 } 845 846 mutex_exit(&freemem_lock); 847 mutex_exit(&anoninfo_lock); 848 ANON_PRINT(A_RESV, 849 ("anon_resvmem: not enough space from swapfs\n")); 850 if (zone != NULL && takemem) 851 rctl_decr_swap(zone, ptob(npages)); 852 return (0); 853 } 854 } 855 856 /* 857 * Give back an anon reservation. 858 */ 859 void 860 anon_unresvmem(size_t size, zone_t *zone) 861 { 862 pgcnt_t npages = btopr(size); 863 spgcnt_t mem_free_pages = 0; 864 pgcnt_t phys_free_slots; 865 #ifdef ANON_DEBUG 866 pgcnt_t mem_resv; 867 #endif 868 if (zone != NULL) 869 rctl_decr_swap(zone, ptob(npages)); 870 871 mutex_enter(&anoninfo_lock); 872 873 ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 874 /* 875 * If some of this reservation belonged to swapfs 876 * give it back to availrmem. 877 * ani_mem_resv is the amount of availrmem swapfs has reserved. 878 * but some of that memory could be locked by segspt so we can only 879 * return non locked ani_mem_resv back to availrmem 880 */ 881 if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) { 882 ANON_PRINT((A_RESV | A_MRESV), 883 ("anon_unresv: growing availrmem by %ld pages\n", 884 MIN(k_anoninfo.ani_mem_resv, npages))); 885 886 mem_free_pages = MIN((spgcnt_t)(k_anoninfo.ani_mem_resv - 887 k_anoninfo.ani_locked_swap), npages); 888 mutex_enter(&freemem_lock); 889 availrmem += mem_free_pages; 890 mutex_exit(&freemem_lock); 891 k_anoninfo.ani_mem_resv -= mem_free_pages; 892 893 ANI_ADD(-mem_free_pages); 894 } 895 /* 896 * The remainder of the pages is returned to phys swap 897 */ 898 ASSERT(npages >= mem_free_pages); 899 phys_free_slots = npages - mem_free_pages; 900 901 if (phys_free_slots) { 902 k_anoninfo.ani_phys_resv -= phys_free_slots; 903 } 904 905 #ifdef ANON_DEBUG 906 mem_resv = k_anoninfo.ani_mem_resv; 907 #endif 908 909 ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 910 ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 911 912 mutex_exit(&anoninfo_lock); 913 914 ANON_PRINT(A_RESV, ("anon_unresv: %lu, tot %lu, caller %p\n", 915 npages, mem_resv, (void *)caller())); 916 } 917 918 /* 919 * Allocate an anon slot and return it with the lock held. 920 */ 921 struct anon * 922 anon_alloc(struct vnode *vp, anoff_t off) 923 { 924 struct anon *ap; 925 kmutex_t *ahm; 926 927 ap = kmem_cache_alloc(anon_cache, KM_SLEEP); 928 if (vp == NULL) { 929 swap_alloc(ap); 930 } else { 931 ap->an_vp = vp; 932 ap->an_off = off; 933 } 934 ap->an_refcnt = 1; 935 ap->an_pvp = NULL; 936 ap->an_poff = 0; 937 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]; 938 mutex_enter(ahm); 939 anon_addhash(ap); 940 mutex_exit(ahm); 941 ANI_ADD(-1); 942 ANON_PRINT(A_ANON, ("anon_alloc: returning ap %p, vp %p\n", 943 (void *)ap, (ap ? (void *)ap->an_vp : NULL))); 944 return (ap); 945 } 946 947 /* 948 * Decrement the reference count of an anon page. 949 * If reference count goes to zero, free it and 950 * its associated page (if any). 951 */ 952 void 953 anon_decref(struct anon *ap) 954 { 955 page_t *pp; 956 struct vnode *vp; 957 anoff_t off; 958 kmutex_t *ahm; 959 960 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]; 961 mutex_enter(ahm); 962 ASSERT(ap->an_refcnt != 0); 963 if (ap->an_refcnt == 0) 964 panic("anon_decref: slot count 0"); 965 if (--ap->an_refcnt == 0) { 966 swap_xlate(ap, &vp, &off); 967 mutex_exit(ahm); 968 969 /* 970 * If there is a page for this anon slot we will need to 971 * call VN_DISPOSE to get rid of the vp association and 972 * put the page back on the free list as really free. 973 * Acquire the "exclusive" lock to ensure that any 974 * pending i/o always completes before the swap slot 975 * is freed. 976 */ 977 pp = page_lookup(vp, (u_offset_t)off, SE_EXCL); 978 979 /* 980 * If there was a page, we've synchronized on it (getting 981 * the exclusive lock is as good as gettting the iolock) 982 * so now we can free the physical backing store. Also, this 983 * is where we would free the name of the anonymous page 984 * (swap_free(ap)), a no-op in the current implementation. 985 */ 986 mutex_enter(ahm); 987 ASSERT(ap->an_refcnt == 0); 988 anon_rmhash(ap); 989 if (ap->an_pvp) 990 swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE); 991 mutex_exit(ahm); 992 993 if (pp != NULL) { 994 /*LINTED: constant in conditional context */ 995 VN_DISPOSE(pp, B_INVAL, 0, kcred); 996 } 997 ANON_PRINT(A_ANON, ("anon_decref: free ap %p, vp %p\n", 998 (void *)ap, (void *)ap->an_vp)); 999 kmem_cache_free(anon_cache, ap); 1000 1001 ANI_ADD(1); 1002 } else { 1003 mutex_exit(ahm); 1004 } 1005 } 1006 1007 static int 1008 anon_share(struct anon_hdr *ahp, ulong_t anon_index, pgcnt_t nslots) 1009 { 1010 struct anon *ap; 1011 1012 while (nslots-- > 0) { 1013 if ((ap = anon_get_ptr(ahp, anon_index)) != NULL && 1014 ap->an_refcnt > 1) 1015 return (1); 1016 anon_index++; 1017 } 1018 1019 return (0); 1020 } 1021 1022 static void 1023 anon_decref_pages( 1024 struct anon_hdr *ahp, 1025 ulong_t an_idx, 1026 uint_t szc) 1027 { 1028 struct anon *ap = anon_get_ptr(ahp, an_idx); 1029 kmutex_t *ahmpages = NULL; 1030 page_t *pp; 1031 pgcnt_t pgcnt = page_get_pagecnt(szc); 1032 pgcnt_t i; 1033 struct vnode *vp; 1034 anoff_t off; 1035 kmutex_t *ahm; 1036 #ifdef DEBUG 1037 int refcnt = 1; 1038 #endif 1039 1040 ASSERT(szc != 0); 1041 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt)); 1042 ASSERT(IS_P2ALIGNED(an_idx, pgcnt)); 1043 ASSERT(an_idx < ahp->size); 1044 1045 if (ahp->size - an_idx < pgcnt) { 1046 /* 1047 * In case of shared mappings total anon map size may not be 1048 * the largest page size aligned. 1049 */ 1050 pgcnt = ahp->size - an_idx; 1051 } 1052 1053 VM_STAT_ADD(anonvmstats.decrefpages[0]); 1054 1055 if (ap != NULL) { 1056 ahmpages = &anonpages_hash_lock[AH_LOCK(ap->an_vp, ap->an_off)]; 1057 mutex_enter(ahmpages); 1058 ASSERT((refcnt = ap->an_refcnt) != 0); 1059 VM_STAT_ADD(anonvmstats.decrefpages[1]); 1060 if (ap->an_refcnt == 1) { 1061 VM_STAT_ADD(anonvmstats.decrefpages[2]); 1062 ASSERT(!anon_share(ahp, an_idx, pgcnt)); 1063 mutex_exit(ahmpages); 1064 ahmpages = NULL; 1065 } 1066 } 1067 1068 i = 0; 1069 while (i < pgcnt) { 1070 if ((ap = anon_get_ptr(ahp, an_idx + i)) == NULL) { 1071 ASSERT(refcnt == 1 && ahmpages == NULL); 1072 i++; 1073 continue; 1074 } 1075 ASSERT(ap->an_refcnt == refcnt); 1076 ASSERT(ahmpages != NULL || ap->an_refcnt == 1); 1077 ASSERT(ahmpages == NULL || ap->an_refcnt > 1); 1078 1079 if (ahmpages == NULL) { 1080 swap_xlate(ap, &vp, &off); 1081 pp = page_lookup(vp, (u_offset_t)off, SE_EXCL); 1082 if (pp == NULL || pp->p_szc == 0) { 1083 VM_STAT_ADD(anonvmstats.decrefpages[3]); 1084 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, 1085 ap->an_off)]; 1086 (void) anon_set_ptr(ahp, an_idx + i, NULL, 1087 ANON_SLEEP); 1088 mutex_enter(ahm); 1089 ap->an_refcnt--; 1090 ASSERT(ap->an_refcnt == 0); 1091 anon_rmhash(ap); 1092 if (ap->an_pvp) 1093 swap_phys_free(ap->an_pvp, ap->an_poff, 1094 PAGESIZE); 1095 mutex_exit(ahm); 1096 if (pp != NULL) { 1097 VM_STAT_ADD(anonvmstats.decrefpages[4]); 1098 /*LINTED*/ 1099 VN_DISPOSE(pp, B_INVAL, 0, kcred); 1100 } 1101 kmem_cache_free(anon_cache, ap); 1102 ANI_ADD(1); 1103 i++; 1104 } else { 1105 pgcnt_t j; 1106 pgcnt_t curpgcnt = 1107 page_get_pagecnt(pp->p_szc); 1108 size_t ppasize = curpgcnt * sizeof (page_t *); 1109 page_t **ppa = kmem_alloc(ppasize, KM_SLEEP); 1110 int dispose = 0; 1111 1112 VM_STAT_ADD(anonvmstats.decrefpages[5]); 1113 1114 ASSERT(pp->p_szc <= szc); 1115 ASSERT(IS_P2ALIGNED(curpgcnt, curpgcnt)); 1116 ASSERT(IS_P2ALIGNED(i, curpgcnt)); 1117 ASSERT(i + curpgcnt <= pgcnt); 1118 ASSERT(!(page_pptonum(pp) & (curpgcnt - 1))); 1119 ppa[0] = pp; 1120 for (j = i + 1; j < i + curpgcnt; j++) { 1121 ap = anon_get_ptr(ahp, an_idx + j); 1122 ASSERT(ap != NULL && 1123 ap->an_refcnt == 1); 1124 swap_xlate(ap, &vp, &off); 1125 pp = page_lookup(vp, (u_offset_t)off, 1126 SE_EXCL); 1127 if (pp == NULL) 1128 panic("anon_decref_pages: " 1129 "no page"); 1130 1131 (void) hat_pageunload(pp, 1132 HAT_FORCE_PGUNLOAD); 1133 ASSERT(pp->p_szc == ppa[0]->p_szc); 1134 ASSERT(page_pptonum(pp) - 1 == 1135 page_pptonum(ppa[j - i - 1])); 1136 ppa[j - i] = pp; 1137 if (ap->an_pvp != NULL && 1138 !vn_matchopval(ap->an_pvp, 1139 VOPNAME_DISPOSE, 1140 (fs_generic_func_p)fs_dispose)) 1141 dispose = 1; 1142 } 1143 if (!dispose) { 1144 VM_STAT_ADD(anonvmstats.decrefpages[6]); 1145 page_destroy_pages(ppa[0]); 1146 } else { 1147 VM_STAT_ADD(anonvmstats.decrefpages[7]); 1148 for (j = 0; j < curpgcnt; j++) { 1149 ASSERT(PAGE_EXCL(ppa[j])); 1150 ppa[j]->p_szc = 0; 1151 } 1152 for (j = 0; j < curpgcnt; j++) { 1153 ASSERT(!hat_page_is_mapped( 1154 ppa[j])); 1155 /*LINTED*/ 1156 VN_DISPOSE(ppa[j], B_INVAL, 0, 1157 kcred); 1158 } 1159 } 1160 kmem_free(ppa, ppasize); 1161 for (j = i; j < i + curpgcnt; j++) { 1162 ap = anon_get_ptr(ahp, an_idx + j); 1163 ASSERT(ap != NULL && 1164 ap->an_refcnt == 1); 1165 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, 1166 ap->an_off)]; 1167 (void) anon_set_ptr(ahp, an_idx + j, 1168 NULL, ANON_SLEEP); 1169 mutex_enter(ahm); 1170 ap->an_refcnt--; 1171 ASSERT(ap->an_refcnt == 0); 1172 anon_rmhash(ap); 1173 if (ap->an_pvp) 1174 swap_phys_free(ap->an_pvp, 1175 ap->an_poff, PAGESIZE); 1176 mutex_exit(ahm); 1177 kmem_cache_free(anon_cache, ap); 1178 ANI_ADD(1); 1179 } 1180 i += curpgcnt; 1181 } 1182 } else { 1183 VM_STAT_ADD(anonvmstats.decrefpages[8]); 1184 (void) anon_set_ptr(ahp, an_idx + i, NULL, ANON_SLEEP); 1185 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]; 1186 mutex_enter(ahm); 1187 ap->an_refcnt--; 1188 mutex_exit(ahm); 1189 i++; 1190 } 1191 } 1192 1193 if (ahmpages != NULL) { 1194 mutex_exit(ahmpages); 1195 } 1196 } 1197 1198 /* 1199 * Duplicate references to size bytes worth of anon pages. 1200 * Used when duplicating a segment that contains private anon pages. 1201 * This code assumes that procedure calling this one has already used 1202 * hat_chgprot() to disable write access to the range of addresses that 1203 * that *old actually refers to. 1204 */ 1205 void 1206 anon_dup(struct anon_hdr *old, ulong_t old_idx, struct anon_hdr *new, 1207 ulong_t new_idx, size_t size) 1208 { 1209 spgcnt_t npages; 1210 kmutex_t *ahm; 1211 struct anon *ap; 1212 ulong_t off; 1213 ulong_t index; 1214 1215 npages = btopr(size); 1216 while (npages > 0) { 1217 index = old_idx; 1218 if ((ap = anon_get_next_ptr(old, &index)) == NULL) 1219 break; 1220 1221 ASSERT(!ANON_ISBUSY(anon_get_slot(old, index))); 1222 off = index - old_idx; 1223 npages -= off; 1224 if (npages <= 0) 1225 break; 1226 1227 (void) anon_set_ptr(new, new_idx + off, ap, ANON_SLEEP); 1228 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]; 1229 1230 mutex_enter(ahm); 1231 ap->an_refcnt++; 1232 mutex_exit(ahm); 1233 1234 off++; 1235 new_idx += off; 1236 old_idx += off; 1237 npages--; 1238 } 1239 } 1240 1241 /* 1242 * Just like anon_dup but also guarantees there are no holes (unallocated anon 1243 * slots) within any large page region. That means if a large page region is 1244 * empty in the old array it will skip it. If there are 1 or more valid slots 1245 * in the large page region of the old array it will make sure to fill in any 1246 * unallocated ones and also copy them to the new array. If noalloc is 1 large 1247 * page region should either have no valid anon slots or all slots should be 1248 * valid. 1249 */ 1250 void 1251 anon_dup_fill_holes( 1252 struct anon_hdr *old, 1253 ulong_t old_idx, 1254 struct anon_hdr *new, 1255 ulong_t new_idx, 1256 size_t size, 1257 uint_t szc, 1258 int noalloc) 1259 { 1260 struct anon *ap; 1261 spgcnt_t npages; 1262 kmutex_t *ahm, *ahmpages = NULL; 1263 pgcnt_t pgcnt, i; 1264 ulong_t index, off; 1265 #ifdef DEBUG 1266 int refcnt; 1267 #endif 1268 1269 ASSERT(szc != 0); 1270 pgcnt = page_get_pagecnt(szc); 1271 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt)); 1272 npages = btopr(size); 1273 ASSERT(IS_P2ALIGNED(npages, pgcnt)); 1274 ASSERT(IS_P2ALIGNED(old_idx, pgcnt)); 1275 1276 VM_STAT_ADD(anonvmstats.dupfillholes[0]); 1277 1278 while (npages > 0) { 1279 index = old_idx; 1280 1281 /* 1282 * Find the next valid slot. 1283 */ 1284 if (anon_get_next_ptr(old, &index) == NULL) 1285 break; 1286 1287 ASSERT(!ANON_ISBUSY(anon_get_slot(old, index))); 1288 /* 1289 * Now backup index to the beginning of the 1290 * current large page region of the old array. 1291 */ 1292 index = P2ALIGN(index, pgcnt); 1293 off = index - old_idx; 1294 ASSERT(IS_P2ALIGNED(off, pgcnt)); 1295 npages -= off; 1296 if (npages <= 0) 1297 break; 1298 1299 /* 1300 * Fill and copy a large page regions worth 1301 * of anon slots. 1302 */ 1303 for (i = 0; i < pgcnt; i++) { 1304 if ((ap = anon_get_ptr(old, index + i)) == NULL) { 1305 if (noalloc) { 1306 panic("anon_dup_fill_holes: " 1307 "empty anon slot\n"); 1308 } 1309 VM_STAT_ADD(anonvmstats.dupfillholes[1]); 1310 ap = anon_alloc(NULL, 0); 1311 (void) anon_set_ptr(old, index + i, ap, 1312 ANON_SLEEP); 1313 } else if (i == 0) { 1314 /* 1315 * make the increment of all refcnts of all 1316 * anon slots of a large page appear atomic by 1317 * getting an anonpages_hash_lock for the 1318 * first anon slot of a large page. 1319 */ 1320 int hash = AH_LOCK(ap->an_vp, ap->an_off); 1321 1322 VM_STAT_ADD(anonvmstats.dupfillholes[2]); 1323 1324 ahmpages = &anonpages_hash_lock[hash]; 1325 mutex_enter(ahmpages); 1326 /*LINTED*/ 1327 ASSERT(refcnt = ap->an_refcnt); 1328 1329 VM_STAT_COND_ADD(ap->an_refcnt > 1, 1330 anonvmstats.dupfillholes[3]); 1331 } 1332 (void) anon_set_ptr(new, new_idx + off + i, ap, 1333 ANON_SLEEP); 1334 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]; 1335 mutex_enter(ahm); 1336 ASSERT(ahmpages != NULL || ap->an_refcnt == 1); 1337 ASSERT(i == 0 || ahmpages == NULL || 1338 refcnt == ap->an_refcnt); 1339 ap->an_refcnt++; 1340 mutex_exit(ahm); 1341 } 1342 if (ahmpages != NULL) { 1343 mutex_exit(ahmpages); 1344 ahmpages = NULL; 1345 } 1346 off += pgcnt; 1347 new_idx += off; 1348 old_idx += off; 1349 npages -= pgcnt; 1350 } 1351 } 1352 1353 /* 1354 * Used when a segment with a vnode changes szc. similarly to 1355 * anon_dup_fill_holes() makes sure each large page region either has no anon 1356 * slots or all of them. but new slots are created by COWing the file 1357 * pages. on entrance no anon slots should be shared. 1358 */ 1359 int 1360 anon_fill_cow_holes( 1361 struct seg *seg, 1362 caddr_t addr, 1363 struct anon_hdr *ahp, 1364 ulong_t an_idx, 1365 struct vnode *vp, 1366 u_offset_t vp_off, 1367 size_t size, 1368 uint_t szc, 1369 uint_t prot, 1370 struct vpage vpage[], 1371 struct cred *cred) 1372 { 1373 struct anon *ap; 1374 spgcnt_t npages; 1375 pgcnt_t pgcnt, i; 1376 ulong_t index, off; 1377 int err = 0; 1378 int pageflags = 0; 1379 1380 ASSERT(szc != 0); 1381 pgcnt = page_get_pagecnt(szc); 1382 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt)); 1383 npages = btopr(size); 1384 ASSERT(IS_P2ALIGNED(npages, pgcnt)); 1385 ASSERT(IS_P2ALIGNED(an_idx, pgcnt)); 1386 1387 while (npages > 0) { 1388 index = an_idx; 1389 1390 /* 1391 * Find the next valid slot. 1392 */ 1393 if (anon_get_next_ptr(ahp, &index) == NULL) { 1394 break; 1395 } 1396 1397 ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index))); 1398 /* 1399 * Now backup index to the beginning of the 1400 * current large page region of the anon array. 1401 */ 1402 index = P2ALIGN(index, pgcnt); 1403 off = index - an_idx; 1404 ASSERT(IS_P2ALIGNED(off, pgcnt)); 1405 npages -= off; 1406 if (npages <= 0) 1407 break; 1408 an_idx += off; 1409 vp_off += ptob(off); 1410 addr += ptob(off); 1411 if (vpage != NULL) { 1412 vpage += off; 1413 } 1414 1415 for (i = 0; i < pgcnt; i++, an_idx++, vp_off += PAGESIZE) { 1416 if ((ap = anon_get_ptr(ahp, an_idx)) == NULL) { 1417 page_t *pl[1 + 1]; 1418 page_t *pp; 1419 1420 err = VOP_GETPAGE(vp, vp_off, PAGESIZE, NULL, 1421 pl, PAGESIZE, seg, addr, S_READ, cred); 1422 if (err) { 1423 break; 1424 } 1425 if (vpage != NULL) { 1426 prot = VPP_PROT(vpage); 1427 pageflags = VPP_ISPPLOCK(vpage) ? 1428 LOCK_PAGE : 0; 1429 } 1430 pp = anon_private(&ap, seg, addr, prot, pl[0], 1431 pageflags, cred); 1432 if (pp == NULL) { 1433 err = ENOMEM; 1434 break; 1435 } 1436 (void) anon_set_ptr(ahp, an_idx, ap, 1437 ANON_SLEEP); 1438 page_unlock(pp); 1439 } 1440 ASSERT(ap->an_refcnt == 1); 1441 addr += PAGESIZE; 1442 if (vpage != NULL) { 1443 vpage++; 1444 } 1445 } 1446 npages -= pgcnt; 1447 } 1448 1449 return (err); 1450 } 1451 1452 /* 1453 * Free a group of "size" anon pages, size in bytes, 1454 * and clear out the pointers to the anon entries. 1455 */ 1456 void 1457 anon_free(struct anon_hdr *ahp, ulong_t index, size_t size) 1458 { 1459 spgcnt_t npages; 1460 struct anon *ap; 1461 ulong_t old; 1462 1463 npages = btopr(size); 1464 1465 while (npages > 0) { 1466 old = index; 1467 if ((ap = anon_get_next_ptr(ahp, &index)) == NULL) 1468 break; 1469 1470 ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index))); 1471 npages -= index - old; 1472 if (npages <= 0) 1473 break; 1474 1475 (void) anon_set_ptr(ahp, index, NULL, ANON_SLEEP); 1476 anon_decref(ap); 1477 /* 1478 * Bump index and decrement page count 1479 */ 1480 index++; 1481 npages--; 1482 } 1483 } 1484 1485 void 1486 anon_free_pages( 1487 struct anon_hdr *ahp, 1488 ulong_t an_idx, 1489 size_t size, 1490 uint_t szc) 1491 { 1492 spgcnt_t npages; 1493 pgcnt_t pgcnt; 1494 ulong_t index, off; 1495 1496 ASSERT(szc != 0); 1497 pgcnt = page_get_pagecnt(szc); 1498 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt)); 1499 npages = btopr(size); 1500 ASSERT(IS_P2ALIGNED(npages, pgcnt)); 1501 ASSERT(IS_P2ALIGNED(an_idx, pgcnt)); 1502 ASSERT(an_idx < ahp->size); 1503 1504 VM_STAT_ADD(anonvmstats.freepages[0]); 1505 1506 while (npages > 0) { 1507 index = an_idx; 1508 1509 /* 1510 * Find the next valid slot. 1511 */ 1512 if (anon_get_next_ptr(ahp, &index) == NULL) 1513 break; 1514 1515 ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index))); 1516 /* 1517 * Now backup index to the beginning of the 1518 * current large page region of the old array. 1519 */ 1520 index = P2ALIGN(index, pgcnt); 1521 off = index - an_idx; 1522 ASSERT(IS_P2ALIGNED(off, pgcnt)); 1523 npages -= off; 1524 if (npages <= 0) 1525 break; 1526 1527 anon_decref_pages(ahp, index, szc); 1528 1529 off += pgcnt; 1530 an_idx += off; 1531 npages -= pgcnt; 1532 } 1533 } 1534 1535 /* 1536 * Make anonymous pages discardable 1537 */ 1538 void 1539 anon_disclaim(struct anon_map *amp, ulong_t index, size_t size) 1540 { 1541 spgcnt_t npages = btopr(size); 1542 struct anon *ap; 1543 struct vnode *vp; 1544 anoff_t off; 1545 page_t *pp, *root_pp; 1546 kmutex_t *ahm; 1547 pgcnt_t pgcnt; 1548 ulong_t old_idx, idx, i; 1549 struct anon_hdr *ahp = amp->ahp; 1550 anon_sync_obj_t cookie; 1551 1552 ASSERT(RW_READ_HELD(&->a_rwlock)); 1553 pgcnt = 1; 1554 for (; npages > 0; index = (pgcnt == 1) ? index + 1 : 1555 P2ROUNDUP(index + 1, pgcnt), npages -= pgcnt) { 1556 1557 /* 1558 * get anon pointer and index for the first valid entry 1559 * in the anon list, starting from "index" 1560 */ 1561 old_idx = index; 1562 if ((ap = anon_get_next_ptr(ahp, &index)) == NULL) 1563 break; 1564 1565 /* 1566 * decrement npages by number of NULL anon slots we skipped 1567 */ 1568 npages -= index - old_idx; 1569 if (npages <= 0) 1570 break; 1571 1572 anon_array_enter(amp, index, &cookie); 1573 ap = anon_get_ptr(ahp, index); 1574 ASSERT(ap != NULL); 1575 1576 /* 1577 * Get anonymous page and try to lock it SE_EXCL; 1578 * if we couldn't grab the lock we skip to next page. 1579 */ 1580 swap_xlate(ap, &vp, &off); 1581 pp = page_lookup_nowait(vp, (u_offset_t)off, SE_EXCL); 1582 if (pp == NULL) { 1583 segadvstat.MADV_FREE_miss.value.ul++; 1584 pgcnt = 1; 1585 anon_array_exit(&cookie); 1586 continue; 1587 } 1588 pgcnt = page_get_pagecnt(pp->p_szc); 1589 1590 /* 1591 * we cannot free a page which is permanently locked. 1592 * The page_struct_lock need not be acquired to examine 1593 * these fields since the page has an "exclusive" lock. 1594 */ 1595 if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) { 1596 page_unlock(pp); 1597 segadvstat.MADV_FREE_miss.value.ul++; 1598 anon_array_exit(&cookie); 1599 continue; 1600 } 1601 1602 ahm = &anonhash_lock[AH_LOCK(vp, off)]; 1603 mutex_enter(ahm); 1604 ASSERT(ap->an_refcnt != 0); 1605 /* 1606 * skip this one if copy-on-write is not yet broken. 1607 */ 1608 if (ap->an_refcnt > 1) { 1609 mutex_exit(ahm); 1610 page_unlock(pp); 1611 segadvstat.MADV_FREE_miss.value.ul++; 1612 anon_array_exit(&cookie); 1613 continue; 1614 } 1615 1616 if (pp->p_szc == 0) { 1617 pgcnt = 1; 1618 1619 /* 1620 * free swap slot; 1621 */ 1622 if (ap->an_pvp) { 1623 swap_phys_free(ap->an_pvp, ap->an_poff, 1624 PAGESIZE); 1625 ap->an_pvp = NULL; 1626 ap->an_poff = 0; 1627 } 1628 mutex_exit(ahm); 1629 segadvstat.MADV_FREE_hit.value.ul++; 1630 1631 /* 1632 * while we are at it, unload all the translations 1633 * and attempt to free the page. 1634 */ 1635 (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 1636 /*LINTED: constant in conditional context */ 1637 VN_DISPOSE(pp, B_FREE, 0, kcred); 1638 anon_array_exit(&cookie); 1639 continue; 1640 } 1641 1642 pgcnt = page_get_pagecnt(pp->p_szc); 1643 if (!IS_P2ALIGNED(index, pgcnt) || npages < pgcnt) { 1644 if (!page_try_demote_pages(pp)) { 1645 mutex_exit(ahm); 1646 page_unlock(pp); 1647 segadvstat.MADV_FREE_miss.value.ul++; 1648 anon_array_exit(&cookie); 1649 continue; 1650 } else { 1651 pgcnt = 1; 1652 if (ap->an_pvp) { 1653 swap_phys_free(ap->an_pvp, 1654 ap->an_poff, PAGESIZE); 1655 ap->an_pvp = NULL; 1656 ap->an_poff = 0; 1657 } 1658 mutex_exit(ahm); 1659 (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 1660 /*LINTED*/ 1661 VN_DISPOSE(pp, B_FREE, 0, kcred); 1662 segadvstat.MADV_FREE_hit.value.ul++; 1663 anon_array_exit(&cookie); 1664 continue; 1665 } 1666 } 1667 mutex_exit(ahm); 1668 root_pp = pp; 1669 1670 /* 1671 * try to lock remaining pages 1672 */ 1673 for (idx = 1; idx < pgcnt; idx++) { 1674 pp++; 1675 if (!page_trylock(pp, SE_EXCL)) 1676 break; 1677 if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) { 1678 page_unlock(pp); 1679 break; 1680 } 1681 } 1682 1683 if (idx == pgcnt) { 1684 for (i = 0; i < pgcnt; i++) { 1685 ap = anon_get_ptr(ahp, index + i); 1686 if (ap == NULL) 1687 break; 1688 swap_xlate(ap, &vp, &off); 1689 ahm = &anonhash_lock[AH_LOCK(vp, off)]; 1690 mutex_enter(ahm); 1691 ASSERT(ap->an_refcnt != 0); 1692 1693 /* 1694 * skip this one if copy-on-write 1695 * is not yet broken. 1696 */ 1697 if (ap->an_refcnt > 1) { 1698 mutex_exit(ahm); 1699 goto skiplp; 1700 } 1701 if (ap->an_pvp) { 1702 swap_phys_free(ap->an_pvp, 1703 ap->an_poff, PAGESIZE); 1704 ap->an_pvp = NULL; 1705 ap->an_poff = 0; 1706 } 1707 mutex_exit(ahm); 1708 } 1709 page_destroy_pages(root_pp); 1710 segadvstat.MADV_FREE_hit.value.ul += pgcnt; 1711 anon_array_exit(&cookie); 1712 continue; 1713 } 1714 skiplp: 1715 segadvstat.MADV_FREE_miss.value.ul += pgcnt; 1716 for (i = 0, pp = root_pp; i < idx; pp++, i++) 1717 page_unlock(pp); 1718 anon_array_exit(&cookie); 1719 } 1720 } 1721 1722 /* 1723 * Return the kept page(s) and protections back to the segment driver. 1724 */ 1725 int 1726 anon_getpage( 1727 struct anon **app, 1728 uint_t *protp, 1729 page_t *pl[], 1730 size_t plsz, 1731 struct seg *seg, 1732 caddr_t addr, 1733 enum seg_rw rw, 1734 struct cred *cred) 1735 { 1736 page_t *pp; 1737 struct anon *ap = *app; 1738 struct vnode *vp; 1739 anoff_t off; 1740 int err; 1741 kmutex_t *ahm; 1742 1743 swap_xlate(ap, &vp, &off); 1744 1745 /* 1746 * Lookup the page. If page is being paged in, 1747 * wait for it to finish as we must return a list of 1748 * pages since this routine acts like the VOP_GETPAGE 1749 * routine does. 1750 */ 1751 if (pl != NULL && (pp = page_lookup(vp, (u_offset_t)off, SE_SHARED))) { 1752 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]; 1753 mutex_enter(ahm); 1754 if (ap->an_refcnt == 1) 1755 *protp = PROT_ALL; 1756 else 1757 *protp = PROT_ALL & ~PROT_WRITE; 1758 mutex_exit(ahm); 1759 pl[0] = pp; 1760 pl[1] = NULL; 1761 return (0); 1762 } 1763 1764 /* 1765 * Simply treat it as a vnode fault on the anon vp. 1766 */ 1767 1768 TRACE_3(TR_FAC_VM, TR_ANON_GETPAGE, 1769 "anon_getpage:seg %x addr %x vp %x", 1770 seg, addr, vp); 1771 1772 err = VOP_GETPAGE(vp, (u_offset_t)off, PAGESIZE, protp, pl, plsz, 1773 seg, addr, rw, cred); 1774 1775 if (err == 0 && pl != NULL) { 1776 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, ap->an_off)]; 1777 mutex_enter(ahm); 1778 if (ap->an_refcnt != 1) 1779 *protp &= ~PROT_WRITE; /* make read-only */ 1780 mutex_exit(ahm); 1781 } 1782 return (err); 1783 } 1784 1785 /* 1786 * Creates or returns kept pages to the segment driver. returns -1 if a large 1787 * page cannot be allocated. returns -2 if some other process has allocated a 1788 * larger page. 1789 * 1790 * For cowfault it will alocate any size pages to fill the requested area to 1791 * avoid partially overwritting anon slots (i.e. sharing only some of the anon 1792 * slots within a large page with other processes). This policy greatly 1793 * simplifies large page freeing (which is only freed when all anon slot 1794 * refcnts are 0). 1795 */ 1796 int 1797 anon_map_getpages( 1798 struct anon_map *amp, 1799 ulong_t start_idx, 1800 uint_t szc, 1801 struct seg *seg, 1802 caddr_t addr, 1803 uint_t prot, 1804 uint_t *protp, 1805 page_t *ppa[], 1806 uint_t *ppa_szc, 1807 struct vpage vpage[], 1808 enum seg_rw rw, 1809 int brkcow, 1810 int anypgsz, 1811 int pgflags, 1812 struct cred *cred) 1813 { 1814 pgcnt_t pgcnt; 1815 struct anon *ap; 1816 struct vnode *vp; 1817 anoff_t off; 1818 page_t *pp, *pl[2], *conpp = NULL; 1819 caddr_t vaddr; 1820 ulong_t pg_idx, an_idx, i; 1821 spgcnt_t nreloc = 0; 1822 int prealloc = 1; 1823 int err, slotcreate; 1824 uint_t vpprot; 1825 int upsize = (szc < seg->s_szc); 1826 1827 #if !defined(__i386) && !defined(__amd64) 1828 ASSERT(seg->s_szc != 0); 1829 #endif 1830 ASSERT(szc <= seg->s_szc); 1831 ASSERT(ppa_szc != NULL); 1832 ASSERT(rw != S_CREATE); 1833 1834 *protp = PROT_ALL; 1835 1836 VM_STAT_ADD(anonvmstats.getpages[0]); 1837 1838 if (szc == 0) { 1839 VM_STAT_ADD(anonvmstats.getpages[1]); 1840 if ((ap = anon_get_ptr(amp->ahp, start_idx)) != NULL) { 1841 err = anon_getpage(&ap, protp, pl, PAGESIZE, seg, 1842 addr, rw, cred); 1843 if (err) 1844 return (err); 1845 ppa[0] = pl[0]; 1846 if (brkcow == 0 || (*protp & PROT_WRITE)) { 1847 VM_STAT_ADD(anonvmstats.getpages[2]); 1848 if (ppa[0]->p_szc != 0 && upsize) { 1849 VM_STAT_ADD(anonvmstats.getpages[3]); 1850 *ppa_szc = MIN(ppa[0]->p_szc, 1851 seg->s_szc); 1852 page_unlock(ppa[0]); 1853 return (-2); 1854 } 1855 return (0); 1856 } 1857 panic("anon_map_getpages: cowfault for szc 0"); 1858 } else { 1859 VM_STAT_ADD(anonvmstats.getpages[4]); 1860 ppa[0] = anon_zero(seg, addr, &ap, cred); 1861 if (ppa[0] == NULL) 1862 return (ENOMEM); 1863 (void) anon_set_ptr(amp->ahp, start_idx, ap, 1864 ANON_SLEEP); 1865 return (0); 1866 } 1867 } 1868 1869 pgcnt = page_get_pagecnt(szc); 1870 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt)); 1871 ASSERT(IS_P2ALIGNED(start_idx, pgcnt)); 1872 1873 /* 1874 * First we check for the case that the requtested large 1875 * page or larger page already exists in the system. 1876 * Actually we only check if the first constituent page 1877 * exists and only preallocate if it's not found. 1878 */ 1879 ap = anon_get_ptr(amp->ahp, start_idx); 1880 if (ap) { 1881 uint_t pszc; 1882 swap_xlate(ap, &vp, &off); 1883 if (page_exists_forreal(vp, (u_offset_t)off, &pszc)) { 1884 if (pszc > szc && upsize) { 1885 *ppa_szc = MIN(pszc, seg->s_szc); 1886 return (-2); 1887 } 1888 if (pszc >= szc) { 1889 prealloc = 0; 1890 } 1891 } 1892 } 1893 1894 VM_STAT_COND_ADD(prealloc == 0, anonvmstats.getpages[5]); 1895 VM_STAT_COND_ADD(prealloc != 0, anonvmstats.getpages[6]); 1896 1897 top: 1898 /* 1899 * If a smaller page or no page at all was found, 1900 * grab a large page off the freelist. 1901 */ 1902 if (prealloc) { 1903 ASSERT(conpp == NULL); 1904 if (page_alloc_pages(anon_vp, seg, addr, NULL, ppa, 1905 szc, 0, pgflags) != 0) { 1906 VM_STAT_ADD(anonvmstats.getpages[7]); 1907 if (brkcow == 0 || 1908 !anon_share(amp->ahp, start_idx, pgcnt)) { 1909 /* 1910 * If the refcnt's of all anon slots are <= 1 1911 * they can't increase since we are holding 1912 * the address space's lock. So segvn can 1913 * safely decrease szc without risking to 1914 * generate a cow fault for the region smaller 1915 * than the segment's largest page size. 1916 */ 1917 VM_STAT_ADD(anonvmstats.getpages[8]); 1918 return (-1); 1919 } 1920 docow: 1921 /* 1922 * This is a cow fault. Copy away the entire 1 large 1923 * page region of this segment. 1924 */ 1925 if (szc != seg->s_szc) 1926 panic("anon_map_getpages: cowfault for szc %d", 1927 szc); 1928 vaddr = addr; 1929 for (pg_idx = 0, an_idx = start_idx; pg_idx < pgcnt; 1930 pg_idx++, an_idx++, vaddr += PAGESIZE) { 1931 if ((ap = anon_get_ptr(amp->ahp, an_idx)) != 1932 NULL) { 1933 err = anon_getpage(&ap, &vpprot, pl, 1934 PAGESIZE, seg, vaddr, rw, cred); 1935 if (err) { 1936 for (i = 0; i < pg_idx; i++) { 1937 if ((pp = ppa[i]) != 1938 NULL) 1939 page_unlock(pp); 1940 } 1941 return (err); 1942 } 1943 ppa[pg_idx] = pl[0]; 1944 } else { 1945 /* 1946 * Since this is a cowfault we know 1947 * that this address space has a 1948 * parent or children which means 1949 * anon_dup_fill_holes() has initialized 1950 * all anon slots within a large page 1951 * region that had at least one anon 1952 * slot at the time of fork(). 1953 */ 1954 panic("anon_map_getpages: " 1955 "cowfault but anon slot is empty"); 1956 } 1957 } 1958 VM_STAT_ADD(anonvmstats.getpages[9]); 1959 *protp = PROT_ALL; 1960 return (anon_map_privatepages(amp, start_idx, szc, seg, 1961 addr, prot, ppa, vpage, anypgsz, pgflags, cred)); 1962 } 1963 } 1964 1965 VM_STAT_ADD(anonvmstats.getpages[10]); 1966 1967 an_idx = start_idx; 1968 pg_idx = 0; 1969 vaddr = addr; 1970 while (pg_idx < pgcnt) { 1971 slotcreate = 0; 1972 if ((ap = anon_get_ptr(amp->ahp, an_idx)) == NULL) { 1973 VM_STAT_ADD(anonvmstats.getpages[11]); 1974 /* 1975 * For us to have decided not to preallocate 1976 * would have meant that a large page 1977 * was found. Which also means that all of the 1978 * anon slots for that page would have been 1979 * already created for us. 1980 */ 1981 if (prealloc == 0) 1982 panic("anon_map_getpages: prealloc = 0"); 1983 1984 slotcreate = 1; 1985 ap = anon_alloc(NULL, 0); 1986 } 1987 swap_xlate(ap, &vp, &off); 1988 1989 /* 1990 * Now setup our preallocated page to pass down 1991 * to swap_getpage(). 1992 */ 1993 if (prealloc) { 1994 ASSERT(ppa[pg_idx]->p_szc == szc); 1995 conpp = ppa[pg_idx]; 1996 } 1997 ASSERT(prealloc || conpp == NULL); 1998 1999 /* 2000 * If we just created this anon slot then call 2001 * with S_CREATE to prevent doing IO on the page. 2002 * Similar to the anon_zero case. 2003 */ 2004 err = swap_getconpage(vp, (u_offset_t)off, PAGESIZE, 2005 NULL, pl, PAGESIZE, conpp, ppa_szc, &nreloc, seg, vaddr, 2006 slotcreate == 1 ? S_CREATE : rw, cred); 2007 2008 if (err) { 2009 ASSERT(err != -2 || upsize); 2010 VM_STAT_ADD(anonvmstats.getpages[12]); 2011 ASSERT(slotcreate == 0); 2012 goto io_err; 2013 } 2014 2015 pp = pl[0]; 2016 2017 if (pp->p_szc < szc || (pp->p_szc > szc && upsize)) { 2018 VM_STAT_ADD(anonvmstats.getpages[13]); 2019 ASSERT(slotcreate == 0); 2020 ASSERT(prealloc == 0); 2021 ASSERT(pg_idx == 0); 2022 if (pp->p_szc > szc) { 2023 ASSERT(upsize); 2024 *ppa_szc = MIN(pp->p_szc, seg->s_szc); 2025 page_unlock(pp); 2026 VM_STAT_ADD(anonvmstats.getpages[14]); 2027 return (-2); 2028 } 2029 page_unlock(pp); 2030 prealloc = 1; 2031 goto top; 2032 } 2033 2034 /* 2035 * If we decided to preallocate but VOP_GETPAGE 2036 * found a page in the system that satisfies our 2037 * request then free up our preallocated large page 2038 * and continue looping accross the existing large 2039 * page via VOP_GETPAGE. 2040 */ 2041 if (prealloc && pp != ppa[pg_idx]) { 2042 VM_STAT_ADD(anonvmstats.getpages[15]); 2043 ASSERT(slotcreate == 0); 2044 ASSERT(pg_idx == 0); 2045 conpp = NULL; 2046 prealloc = 0; 2047 page_free_pages(ppa[0]); 2048 } 2049 2050 if (prealloc && nreloc > 1) { 2051 /* 2052 * we have relocated out of a smaller large page. 2053 * skip npgs - 1 iterations and continue which will 2054 * increment by one the loop indices. 2055 */ 2056 spgcnt_t npgs = nreloc; 2057 2058 VM_STAT_ADD(anonvmstats.getpages[16]); 2059 2060 ASSERT(pp == ppa[pg_idx]); 2061 ASSERT(slotcreate == 0); 2062 ASSERT(pg_idx + npgs <= pgcnt); 2063 if ((*protp & PROT_WRITE) && 2064 anon_share(amp->ahp, an_idx, npgs)) { 2065 *protp &= ~PROT_WRITE; 2066 } 2067 pg_idx += npgs; 2068 an_idx += npgs; 2069 vaddr += PAGESIZE * npgs; 2070 continue; 2071 } 2072 2073 VM_STAT_ADD(anonvmstats.getpages[17]); 2074 2075 /* 2076 * Anon_zero case. 2077 */ 2078 if (slotcreate) { 2079 ASSERT(prealloc); 2080 pagezero(pp, 0, PAGESIZE); 2081 CPU_STATS_ADD_K(vm, zfod, 1); 2082 hat_setrefmod(pp); 2083 } 2084 2085 ASSERT(prealloc == 0 || ppa[pg_idx] == pp); 2086 ASSERT(prealloc != 0 || PAGE_SHARED(pp)); 2087 ASSERT(prealloc == 0 || PAGE_EXCL(pp)); 2088 2089 if (pg_idx > 0 && 2090 ((page_pptonum(pp) != page_pptonum(ppa[pg_idx - 1]) + 1) || 2091 (pp->p_szc != ppa[pg_idx - 1]->p_szc))) { 2092 panic("anon_map_getpages: unexpected page"); 2093 } else if (pg_idx == 0 && (page_pptonum(pp) & (pgcnt - 1))) { 2094 panic("anon_map_getpages: unaligned page"); 2095 } 2096 2097 if (prealloc == 0) { 2098 ppa[pg_idx] = pp; 2099 } 2100 2101 if (ap->an_refcnt > 1) { 2102 VM_STAT_ADD(anonvmstats.getpages[18]); 2103 *protp &= ~PROT_WRITE; 2104 } 2105 2106 /* 2107 * If this is a new anon slot then initialize 2108 * the anon array entry. 2109 */ 2110 if (slotcreate) { 2111 (void) anon_set_ptr(amp->ahp, an_idx, ap, ANON_SLEEP); 2112 } 2113 pg_idx++; 2114 an_idx++; 2115 vaddr += PAGESIZE; 2116 } 2117 2118 /* 2119 * Since preallocated pages come off the freelist 2120 * they are locked SE_EXCL. Simply downgrade and return. 2121 */ 2122 if (prealloc) { 2123 VM_STAT_ADD(anonvmstats.getpages[19]); 2124 conpp = NULL; 2125 for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) { 2126 page_downgrade(ppa[pg_idx]); 2127 } 2128 } 2129 ASSERT(conpp == NULL); 2130 2131 if (brkcow == 0 || (*protp & PROT_WRITE)) { 2132 VM_STAT_ADD(anonvmstats.getpages[20]); 2133 return (0); 2134 } 2135 2136 if (szc < seg->s_szc) 2137 panic("anon_map_getpages: cowfault for szc %d", szc); 2138 2139 VM_STAT_ADD(anonvmstats.getpages[21]); 2140 2141 *protp = PROT_ALL; 2142 return (anon_map_privatepages(amp, start_idx, szc, seg, addr, prot, 2143 ppa, vpage, anypgsz, pgflags, cred)); 2144 io_err: 2145 /* 2146 * We got an IO error somewhere in our large page. 2147 * If we were using a preallocated page then just demote 2148 * all the constituent pages that we've succeeded with sofar 2149 * to PAGESIZE pages and leave them in the system 2150 * unlocked. 2151 */ 2152 2153 ASSERT(err != -2 || ((pg_idx == 0) && upsize)); 2154 2155 VM_STAT_COND_ADD(err > 0, anonvmstats.getpages[22]); 2156 VM_STAT_COND_ADD(err == -1, anonvmstats.getpages[23]); 2157 VM_STAT_COND_ADD(err == -2, anonvmstats.getpages[24]); 2158 2159 if (prealloc) { 2160 conpp = NULL; 2161 if (pg_idx > 0) { 2162 VM_STAT_ADD(anonvmstats.getpages[25]); 2163 for (i = 0; i < pgcnt; i++) { 2164 pp = ppa[i]; 2165 ASSERT(PAGE_EXCL(pp)); 2166 ASSERT(pp->p_szc == szc); 2167 pp->p_szc = 0; 2168 } 2169 for (i = 0; i < pg_idx; i++) { 2170 ASSERT(!hat_page_is_mapped(ppa[i])); 2171 page_unlock(ppa[i]); 2172 } 2173 /* 2174 * Now free up the remaining unused constituent 2175 * pages. 2176 */ 2177 while (pg_idx < pgcnt) { 2178 ASSERT(!hat_page_is_mapped(ppa[pg_idx])); 2179 page_free(ppa[pg_idx], 0); 2180 pg_idx++; 2181 } 2182 } else { 2183 VM_STAT_ADD(anonvmstats.getpages[26]); 2184 page_free_pages(ppa[0]); 2185 } 2186 } else { 2187 VM_STAT_ADD(anonvmstats.getpages[27]); 2188 ASSERT(err > 0); 2189 for (i = 0; i < pg_idx; i++) 2190 page_unlock(ppa[i]); 2191 } 2192 ASSERT(conpp == NULL); 2193 if (err != -1) 2194 return (err); 2195 /* 2196 * we are here because we failed to relocate. 2197 */ 2198 ASSERT(prealloc); 2199 if (brkcow == 0 || !anon_share(amp->ahp, start_idx, pgcnt)) { 2200 VM_STAT_ADD(anonvmstats.getpages[28]); 2201 return (-1); 2202 } 2203 VM_STAT_ADD(anonvmstats.getpages[29]); 2204 goto docow; 2205 } 2206 2207 2208 /* 2209 * Turn a reference to an object or shared anon page 2210 * into a private page with a copy of the data from the 2211 * original page which is always locked by the caller. 2212 * This routine unloads the translation and unlocks the 2213 * original page, if it isn't being stolen, before returning 2214 * to the caller. 2215 * 2216 * NOTE: The original anon slot is not freed by this routine 2217 * It must be freed by the caller while holding the 2218 * "anon_map" lock to prevent races which can occur if 2219 * a process has multiple lwps in its address space. 2220 */ 2221 page_t * 2222 anon_private( 2223 struct anon **app, 2224 struct seg *seg, 2225 caddr_t addr, 2226 uint_t prot, 2227 page_t *opp, 2228 int oppflags, 2229 struct cred *cred) 2230 { 2231 struct anon *old = *app; 2232 struct anon *new; 2233 page_t *pp = NULL; 2234 struct vnode *vp; 2235 anoff_t off; 2236 page_t *anon_pl[1 + 1]; 2237 int err; 2238 2239 if (oppflags & STEAL_PAGE) 2240 ASSERT(PAGE_EXCL(opp)); 2241 else 2242 ASSERT(PAGE_LOCKED(opp)); 2243 2244 CPU_STATS_ADD_K(vm, cow_fault, 1); 2245 2246 /* Kernel probe */ 2247 TNF_PROBE_1(anon_private, "vm pagefault", /* CSTYLED */, 2248 tnf_opaque, address, addr); 2249 2250 *app = new = anon_alloc(NULL, 0); 2251 swap_xlate(new, &vp, &off); 2252 2253 if (oppflags & STEAL_PAGE) { 2254 page_rename(opp, vp, (u_offset_t)off); 2255 pp = opp; 2256 TRACE_5(TR_FAC_VM, TR_ANON_PRIVATE, 2257 "anon_private:seg %p addr %x pp %p vp %p off %lx", 2258 seg, addr, pp, vp, off); 2259 hat_setmod(pp); 2260 2261 /* bug 4026339 */ 2262 page_downgrade(pp); 2263 return (pp); 2264 } 2265 2266 /* 2267 * Call the VOP_GETPAGE routine to create the page, thereby 2268 * enabling the vnode driver to allocate any filesystem 2269 * space (e.g., disk block allocation for UFS). This also 2270 * prevents more than one page from being added to the 2271 * vnode at the same time. 2272 */ 2273 err = VOP_GETPAGE(vp, (u_offset_t)off, PAGESIZE, NULL, 2274 anon_pl, PAGESIZE, seg, addr, S_CREATE, cred); 2275 if (err) 2276 goto out; 2277 2278 pp = anon_pl[0]; 2279 2280 /* 2281 * If the original page was locked, we need to move the lock 2282 * to the new page by transfering 'cowcnt/lckcnt' of the original 2283 * page to 'cowcnt/lckcnt' of the new page. 2284 * 2285 * See Statement at the beginning of segvn_lockop() and 2286 * comments in page_pp_useclaim() regarding the way 2287 * cowcnts/lckcnts are handled. 2288 * 2289 * Also availrmem must be decremented up front for read only mapping 2290 * before calling page_pp_useclaim. page_pp_useclaim will bump it back 2291 * if availrmem did not need to be decremented after all. 2292 */ 2293 if (oppflags & LOCK_PAGE) { 2294 if ((prot & PROT_WRITE) == 0) { 2295 mutex_enter(&freemem_lock); 2296 if (availrmem > pages_pp_maximum) { 2297 availrmem--; 2298 pages_useclaim++; 2299 } else { 2300 mutex_exit(&freemem_lock); 2301 goto out; 2302 } 2303 mutex_exit(&freemem_lock); 2304 } 2305 page_pp_useclaim(opp, pp, prot & PROT_WRITE); 2306 } 2307 2308 /* 2309 * Now copy the contents from the original page, 2310 * which is locked and loaded in the MMU by 2311 * the caller to prevent yet another page fault. 2312 */ 2313 /* XXX - should set mod bit in here */ 2314 if (ppcopy(opp, pp) == 0) { 2315 /* 2316 * Before ppcopy could hanlde UE or other faults, we 2317 * would have panicked here, and still have no option 2318 * but to do so now. 2319 */ 2320 panic("anon_private, ppcopy failed, opp = 0x%p, pp = 0x%p", 2321 opp, pp); 2322 } 2323 2324 hat_setrefmod(pp); /* mark as modified */ 2325 2326 /* 2327 * Unload the old translation. 2328 */ 2329 hat_unload(seg->s_as->a_hat, addr, PAGESIZE, HAT_UNLOAD); 2330 2331 /* 2332 * Free unmapped, unmodified original page. 2333 * or release the lock on the original page, 2334 * otherwise the process will sleep forever in 2335 * anon_decref() waiting for the "exclusive" lock 2336 * on the page. 2337 */ 2338 (void) page_release(opp, 1); 2339 2340 /* 2341 * we are done with page creation so downgrade the new 2342 * page's selock to shared, this helps when multiple 2343 * as_fault(...SOFTLOCK...) are done to the same 2344 * page(aio) 2345 */ 2346 page_downgrade(pp); 2347 2348 /* 2349 * NOTE: The original anon slot must be freed by the 2350 * caller while holding the "anon_map" lock, if we 2351 * copied away from an anonymous page. 2352 */ 2353 return (pp); 2354 2355 out: 2356 *app = old; 2357 if (pp) 2358 page_unlock(pp); 2359 anon_decref(new); 2360 page_unlock(opp); 2361 return ((page_t *)NULL); 2362 } 2363 2364 int 2365 anon_map_privatepages( 2366 struct anon_map *amp, 2367 ulong_t start_idx, 2368 uint_t szc, 2369 struct seg *seg, 2370 caddr_t addr, 2371 uint_t prot, 2372 page_t *ppa[], 2373 struct vpage vpage[], 2374 int anypgsz, 2375 int pgflags, 2376 struct cred *cred) 2377 { 2378 pgcnt_t pgcnt; 2379 struct vnode *vp; 2380 anoff_t off; 2381 page_t *pl[2], *conpp = NULL; 2382 int err; 2383 int prealloc = 1; 2384 struct anon *ap, *oldap; 2385 caddr_t vaddr; 2386 page_t *pplist, *pp; 2387 ulong_t pg_idx, an_idx; 2388 spgcnt_t nreloc = 0; 2389 int pagelock = 0; 2390 kmutex_t *ahmpages = NULL; 2391 #ifdef DEBUG 2392 int refcnt; 2393 #endif 2394 2395 ASSERT(szc != 0); 2396 ASSERT(szc == seg->s_szc); 2397 2398 VM_STAT_ADD(anonvmstats.privatepages[0]); 2399 2400 pgcnt = page_get_pagecnt(szc); 2401 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt)); 2402 ASSERT(IS_P2ALIGNED(start_idx, pgcnt)); 2403 2404 ASSERT(amp != NULL); 2405 ap = anon_get_ptr(amp->ahp, start_idx); 2406 ASSERT(ap == NULL || ap->an_refcnt >= 1); 2407 2408 VM_STAT_COND_ADD(ap == NULL, anonvmstats.privatepages[1]); 2409 2410 /* 2411 * Now try and allocate the large page. If we fail then just 2412 * let VOP_GETPAGE give us PAGESIZE pages. Normally we let 2413 * the caller make this decision but to avoid added complexity 2414 * it's simplier to handle that case here. 2415 */ 2416 if (anypgsz == -1) { 2417 VM_STAT_ADD(anonvmstats.privatepages[2]); 2418 prealloc = 0; 2419 } else if (page_alloc_pages(anon_vp, seg, addr, &pplist, NULL, szc, 2420 anypgsz, pgflags) != 0) { 2421 VM_STAT_ADD(anonvmstats.privatepages[3]); 2422 prealloc = 0; 2423 } 2424 2425 /* 2426 * make the decrement of all refcnts of all 2427 * anon slots of a large page appear atomic by 2428 * getting an anonpages_hash_lock for the 2429 * first anon slot of a large page. 2430 */ 2431 if (ap != NULL) { 2432 ahmpages = &anonpages_hash_lock[AH_LOCK(ap->an_vp, 2433 ap->an_off)]; 2434 mutex_enter(ahmpages); 2435 if (ap->an_refcnt == 1) { 2436 VM_STAT_ADD(anonvmstats.privatepages[4]); 2437 ASSERT(!anon_share(amp->ahp, start_idx, pgcnt)); 2438 mutex_exit(ahmpages); 2439 2440 if (prealloc) { 2441 page_free_replacement_page(pplist); 2442 page_create_putback(pgcnt); 2443 } 2444 ASSERT(ppa[0]->p_szc <= szc); 2445 if (ppa[0]->p_szc == szc) { 2446 VM_STAT_ADD(anonvmstats.privatepages[5]); 2447 return (0); 2448 } 2449 for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) { 2450 ASSERT(ppa[pg_idx] != NULL); 2451 page_unlock(ppa[pg_idx]); 2452 } 2453 return (-1); 2454 } 2455 } 2456 2457 /* 2458 * If we are passed in the vpage array and this is 2459 * not PROT_WRITE then we need to decrement availrmem 2460 * up front before we try anything. If we need to and 2461 * can't decrement availrmem then its better to fail now 2462 * than in the middle of processing the new large page. 2463 * page_pp_usclaim() on behalf of each constituent page 2464 * below will adjust availrmem back for the cases not needed. 2465 */ 2466 if (vpage != NULL && (prot & PROT_WRITE) == 0) { 2467 for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) { 2468 if (VPP_ISPPLOCK(&vpage[pg_idx])) { 2469 pagelock = 1; 2470 break; 2471 } 2472 } 2473 if (pagelock) { 2474 VM_STAT_ADD(anonvmstats.privatepages[6]); 2475 mutex_enter(&freemem_lock); 2476 if (availrmem >= pages_pp_maximum + pgcnt) { 2477 availrmem -= pgcnt; 2478 pages_useclaim += pgcnt; 2479 } else { 2480 VM_STAT_ADD(anonvmstats.privatepages[7]); 2481 mutex_exit(&freemem_lock); 2482 if (ahmpages != NULL) { 2483 mutex_exit(ahmpages); 2484 } 2485 if (prealloc) { 2486 page_free_replacement_page(pplist); 2487 page_create_putback(pgcnt); 2488 } 2489 for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) 2490 if (ppa[pg_idx] != NULL) 2491 page_unlock(ppa[pg_idx]); 2492 return (ENOMEM); 2493 } 2494 mutex_exit(&freemem_lock); 2495 } 2496 } 2497 2498 CPU_STATS_ADD_K(vm, cow_fault, pgcnt); 2499 2500 VM_STAT_ADD(anonvmstats.privatepages[8]); 2501 2502 an_idx = start_idx; 2503 pg_idx = 0; 2504 vaddr = addr; 2505 for (; pg_idx < pgcnt; pg_idx++, an_idx++, vaddr += PAGESIZE) { 2506 ASSERT(ppa[pg_idx] != NULL); 2507 oldap = anon_get_ptr(amp->ahp, an_idx); 2508 ASSERT(ahmpages != NULL || oldap == NULL); 2509 ASSERT(ahmpages == NULL || oldap != NULL); 2510 ASSERT(ahmpages == NULL || oldap->an_refcnt > 1); 2511 ASSERT(ahmpages == NULL || pg_idx != 0 || 2512 (refcnt = oldap->an_refcnt)); 2513 ASSERT(ahmpages == NULL || pg_idx == 0 || 2514 refcnt == oldap->an_refcnt); 2515 2516 ap = anon_alloc(NULL, 0); 2517 2518 swap_xlate(ap, &vp, &off); 2519 2520 /* 2521 * Now setup our preallocated page to pass down to 2522 * swap_getpage(). 2523 */ 2524 if (prealloc) { 2525 pp = pplist; 2526 page_sub(&pplist, pp); 2527 conpp = pp; 2528 } 2529 2530 err = swap_getconpage(vp, (u_offset_t)off, PAGESIZE, NULL, pl, 2531 PAGESIZE, conpp, NULL, &nreloc, seg, vaddr, 2532 S_CREATE, cred); 2533 2534 /* 2535 * Impossible to fail this is S_CREATE. 2536 */ 2537 if (err) 2538 panic("anon_map_privatepages: VOP_GETPAGE failed"); 2539 2540 ASSERT(prealloc ? pp == pl[0] : pl[0]->p_szc == 0); 2541 ASSERT(prealloc == 0 || nreloc == 1); 2542 2543 pp = pl[0]; 2544 2545 /* 2546 * If the original page was locked, we need to move 2547 * the lock to the new page by transfering 2548 * 'cowcnt/lckcnt' of the original page to 'cowcnt/lckcnt' 2549 * of the new page. pg_idx can be used to index 2550 * into the vpage array since the caller will guarentee 2551 * that vpage struct passed in corresponds to addr 2552 * and forward. 2553 */ 2554 if (vpage != NULL && VPP_ISPPLOCK(&vpage[pg_idx])) { 2555 page_pp_useclaim(ppa[pg_idx], pp, prot & PROT_WRITE); 2556 } else if (pagelock) { 2557 mutex_enter(&freemem_lock); 2558 availrmem++; 2559 pages_useclaim--; 2560 mutex_exit(&freemem_lock); 2561 } 2562 2563 /* 2564 * Now copy the contents from the original page. 2565 */ 2566 if (ppcopy(ppa[pg_idx], pp) == 0) { 2567 /* 2568 * Before ppcopy could hanlde UE or other faults, we 2569 * would have panicked here, and still have no option 2570 * but to do so now. 2571 */ 2572 panic("anon_map_privatepages, ppcopy failed"); 2573 } 2574 2575 hat_setrefmod(pp); /* mark as modified */ 2576 2577 /* 2578 * Release the lock on the original page, 2579 * derement the old slot, and down grade the lock 2580 * on the new copy. 2581 */ 2582 page_unlock(ppa[pg_idx]); 2583 2584 if (!prealloc) 2585 page_downgrade(pp); 2586 2587 ppa[pg_idx] = pp; 2588 2589 /* 2590 * Now reflect the copy in the new anon array. 2591 */ 2592 ASSERT(ahmpages == NULL || oldap->an_refcnt > 1); 2593 if (oldap != NULL) 2594 anon_decref(oldap); 2595 (void) anon_set_ptr(amp->ahp, an_idx, ap, ANON_SLEEP); 2596 } 2597 if (ahmpages != NULL) { 2598 mutex_exit(ahmpages); 2599 } 2600 ASSERT(prealloc == 0 || pplist == NULL); 2601 if (prealloc) { 2602 VM_STAT_ADD(anonvmstats.privatepages[9]); 2603 for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) { 2604 page_downgrade(ppa[pg_idx]); 2605 } 2606 } 2607 2608 /* 2609 * Unload the old large page translation. 2610 */ 2611 hat_unload(seg->s_as->a_hat, addr, pgcnt << PAGESHIFT, HAT_UNLOAD); 2612 return (0); 2613 } 2614 2615 /* 2616 * Allocate a private zero-filled anon page. 2617 */ 2618 page_t * 2619 anon_zero(struct seg *seg, caddr_t addr, struct anon **app, struct cred *cred) 2620 { 2621 struct anon *ap; 2622 page_t *pp; 2623 struct vnode *vp; 2624 anoff_t off; 2625 page_t *anon_pl[1 + 1]; 2626 int err; 2627 2628 /* Kernel probe */ 2629 TNF_PROBE_1(anon_zero, "vm pagefault", /* CSTYLED */, 2630 tnf_opaque, address, addr); 2631 2632 *app = ap = anon_alloc(NULL, 0); 2633 swap_xlate(ap, &vp, &off); 2634 2635 /* 2636 * Call the VOP_GETPAGE routine to create the page, thereby 2637 * enabling the vnode driver to allocate any filesystem 2638 * dependent structures (e.g., disk block allocation for UFS). 2639 * This also prevents more than on page from being added to 2640 * the vnode at the same time since it is locked. 2641 */ 2642 err = VOP_GETPAGE(vp, off, PAGESIZE, NULL, 2643 anon_pl, PAGESIZE, seg, addr, S_CREATE, cred); 2644 if (err) { 2645 *app = NULL; 2646 anon_decref(ap); 2647 return (NULL); 2648 } 2649 pp = anon_pl[0]; 2650 2651 pagezero(pp, 0, PAGESIZE); /* XXX - should set mod bit */ 2652 page_downgrade(pp); 2653 CPU_STATS_ADD_K(vm, zfod, 1); 2654 hat_setrefmod(pp); /* mark as modified so pageout writes back */ 2655 return (pp); 2656 } 2657 2658 2659 /* 2660 * Allocate array of private zero-filled anon pages for empty slots 2661 * and kept pages for non empty slots within given range. 2662 * 2663 * NOTE: This rontine will try and use large pages 2664 * if available and supported by underlying platform. 2665 */ 2666 int 2667 anon_map_createpages( 2668 struct anon_map *amp, 2669 ulong_t start_index, 2670 size_t len, 2671 page_t *ppa[], 2672 struct seg *seg, 2673 caddr_t addr, 2674 enum seg_rw rw, 2675 struct cred *cred) 2676 { 2677 2678 struct anon *ap; 2679 struct vnode *ap_vp; 2680 page_t *pp, *pplist, *anon_pl[1 + 1], *conpp = NULL; 2681 int err = 0; 2682 ulong_t p_index, index; 2683 pgcnt_t npgs, pg_cnt; 2684 spgcnt_t nreloc = 0; 2685 uint_t l_szc, szc, prot; 2686 anoff_t ap_off; 2687 size_t pgsz; 2688 lgrp_t *lgrp; 2689 kmutex_t *ahm; 2690 2691 /* 2692 * XXX For now only handle S_CREATE. 2693 */ 2694 ASSERT(rw == S_CREATE); 2695 2696 index = start_index; 2697 p_index = 0; 2698 npgs = btopr(len); 2699 2700 /* 2701 * If this platform supports multiple page sizes 2702 * then try and allocate directly from the free 2703 * list for pages larger than PAGESIZE. 2704 * 2705 * NOTE:When we have page_create_ru we can stop 2706 * directly allocating from the freelist. 2707 */ 2708 l_szc = seg->s_szc; 2709 ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER); 2710 while (npgs) { 2711 2712 /* 2713 * if anon slot already exists 2714 * (means page has been created) 2715 * so 1) look up the page 2716 * 2) if the page is still in memory, get it. 2717 * 3) if not, create a page and 2718 * page in from physical swap device. 2719 * These are done in anon_getpage(). 2720 */ 2721 ap = anon_get_ptr(amp->ahp, index); 2722 if (ap) { 2723 err = anon_getpage(&ap, &prot, anon_pl, PAGESIZE, 2724 seg, addr, S_READ, cred); 2725 if (err) { 2726 ANON_LOCK_EXIT(&->a_rwlock); 2727 panic("anon_map_createpages: anon_getpage"); 2728 } 2729 pp = anon_pl[0]; 2730 ppa[p_index++] = pp; 2731 2732 /* 2733 * an_pvp can become non-NULL after SysV's page was 2734 * paged out before ISM was attached to this SysV 2735 * shared memory segment. So free swap slot if needed. 2736 */ 2737 if (ap->an_pvp != NULL) { 2738 page_io_lock(pp); 2739 ahm = &anonhash_lock[AH_LOCK(ap->an_vp, 2740 ap->an_off)]; 2741 mutex_enter(ahm); 2742 if (ap->an_pvp != NULL) { 2743 swap_phys_free(ap->an_pvp, 2744 ap->an_poff, PAGESIZE); 2745 ap->an_pvp = NULL; 2746 ap->an_poff = 0; 2747 mutex_exit(ahm); 2748 hat_setmod(pp); 2749 } else { 2750 mutex_exit(ahm); 2751 } 2752 page_io_unlock(pp); 2753 } 2754 2755 addr += PAGESIZE; 2756 index++; 2757 npgs--; 2758 continue; 2759 } 2760 /* 2761 * Now try and allocate the largest page possible 2762 * for the current address and range. 2763 * Keep dropping down in page size until: 2764 * 2765 * 1) Properly aligned 2766 * 2) Does not overlap existing anon pages 2767 * 3) Fits in remaining range. 2768 * 4) able to allocate one. 2769 * 2770 * NOTE: XXX When page_create_ru is completed this code 2771 * will change. 2772 */ 2773 szc = l_szc; 2774 pplist = NULL; 2775 pg_cnt = 0; 2776 while (szc) { 2777 pgsz = page_get_pagesize(szc); 2778 pg_cnt = pgsz >> PAGESHIFT; 2779 if (IS_P2ALIGNED(addr, pgsz) && pg_cnt <= npgs && 2780 anon_pages(amp->ahp, index, pg_cnt) == 0) { 2781 /* 2782 * XXX 2783 * Since we are faking page_create() 2784 * we also need to do the freemem and 2785 * pcf accounting. 2786 */ 2787 (void) page_create_wait(pg_cnt, PG_WAIT); 2788 2789 /* 2790 * Get lgroup to allocate next page of shared 2791 * memory from and use it to specify where to 2792 * allocate the physical memory 2793 */ 2794 lgrp = lgrp_mem_choose(seg, addr, pgsz); 2795 2796 pplist = page_get_freelist( 2797 anon_vp, (u_offset_t)0, seg, 2798 addr, pgsz, 0, lgrp); 2799 2800 if (pplist == NULL) { 2801 page_create_putback(pg_cnt); 2802 } 2803 2804 /* 2805 * If a request for a page of size 2806 * larger than PAGESIZE failed 2807 * then don't try that size anymore. 2808 */ 2809 if (pplist == NULL) { 2810 l_szc = szc - 1; 2811 } else { 2812 break; 2813 } 2814 } 2815 szc--; 2816 } 2817 2818 /* 2819 * If just using PAGESIZE pages then don't 2820 * directly allocate from the free list. 2821 */ 2822 if (pplist == NULL) { 2823 ASSERT(szc == 0); 2824 pp = anon_zero(seg, addr, &ap, cred); 2825 if (pp == NULL) { 2826 ANON_LOCK_EXIT(&->a_rwlock); 2827 panic("anon_map_createpages: anon_zero"); 2828 } 2829 ppa[p_index++] = pp; 2830 2831 ASSERT(anon_get_ptr(amp->ahp, index) == NULL); 2832 (void) anon_set_ptr(amp->ahp, index, ap, ANON_SLEEP); 2833 2834 addr += PAGESIZE; 2835 index++; 2836 npgs--; 2837 continue; 2838 } 2839 2840 /* 2841 * pplist is a list of pg_cnt PAGESIZE pages. 2842 * These pages are locked SE_EXCL since they 2843 * came directly off the free list. 2844 */ 2845 ASSERT(IS_P2ALIGNED(pg_cnt, pg_cnt)); 2846 ASSERT(IS_P2ALIGNED(index, pg_cnt)); 2847 ASSERT(conpp == NULL); 2848 while (pg_cnt--) { 2849 2850 ap = anon_alloc(NULL, 0); 2851 swap_xlate(ap, &ap_vp, &ap_off); 2852 2853 ASSERT(pplist != NULL); 2854 pp = pplist; 2855 page_sub(&pplist, pp); 2856 PP_CLRFREE(pp); 2857 PP_CLRAGED(pp); 2858 conpp = pp; 2859 2860 err = swap_getconpage(ap_vp, ap_off, PAGESIZE, 2861 (uint_t *)NULL, anon_pl, PAGESIZE, conpp, NULL, 2862 &nreloc, seg, addr, S_CREATE, cred); 2863 2864 if (err) { 2865 ANON_LOCK_EXIT(&->a_rwlock); 2866 panic("anon_map_createpages: S_CREATE"); 2867 } 2868 2869 ASSERT(anon_pl[0] == pp); 2870 ASSERT(nreloc == 1); 2871 pagezero(pp, 0, PAGESIZE); 2872 CPU_STATS_ADD_K(vm, zfod, 1); 2873 hat_setrefmod(pp); 2874 2875 ASSERT(anon_get_ptr(amp->ahp, index) == NULL); 2876 (void) anon_set_ptr(amp->ahp, index, ap, ANON_SLEEP); 2877 2878 ppa[p_index++] = pp; 2879 2880 addr += PAGESIZE; 2881 index++; 2882 npgs--; 2883 } 2884 conpp = NULL; 2885 pg_cnt = pgsz >> PAGESHIFT; 2886 p_index = p_index - pg_cnt; 2887 while (pg_cnt--) { 2888 page_downgrade(ppa[p_index++]); 2889 } 2890 } 2891 ANON_LOCK_EXIT(&->a_rwlock); 2892 return (0); 2893 } 2894 2895 static int 2896 anon_try_demote_pages( 2897 struct anon_hdr *ahp, 2898 ulong_t sidx, 2899 uint_t szc, 2900 page_t **ppa, 2901 int private) 2902 { 2903 struct anon *ap; 2904 pgcnt_t pgcnt = page_get_pagecnt(szc); 2905 page_t *pp; 2906 pgcnt_t i; 2907 kmutex_t *ahmpages = NULL; 2908 int root = 0; 2909 pgcnt_t npgs; 2910 pgcnt_t curnpgs = 0; 2911 size_t ppasize = 0; 2912 2913 ASSERT(szc != 0); 2914 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt)); 2915 ASSERT(IS_P2ALIGNED(sidx, pgcnt)); 2916 ASSERT(sidx < ahp->size); 2917 2918 if (ppa == NULL) { 2919 ppasize = pgcnt * sizeof (page_t *); 2920 ppa = kmem_alloc(ppasize, KM_SLEEP); 2921 } 2922 2923 ap = anon_get_ptr(ahp, sidx); 2924 if (ap != NULL && private) { 2925 VM_STAT_ADD(anonvmstats.demotepages[1]); 2926 ahmpages = &anonpages_hash_lock[AH_LOCK(ap->an_vp, ap->an_off)]; 2927 mutex_enter(ahmpages); 2928 } 2929 2930 if (ap != NULL && ap->an_refcnt > 1) { 2931 if (ahmpages != NULL) { 2932 VM_STAT_ADD(anonvmstats.demotepages[2]); 2933 mutex_exit(ahmpages); 2934 } 2935 if (ppasize != 0) { 2936 kmem_free(ppa, ppasize); 2937 } 2938 return (0); 2939 } 2940 if (ahmpages != NULL) { 2941 mutex_exit(ahmpages); 2942 } 2943 if (ahp->size - sidx < pgcnt) { 2944 ASSERT(private == 0); 2945 pgcnt = ahp->size - sidx; 2946 } 2947 for (i = 0; i < pgcnt; i++, sidx++) { 2948 ap = anon_get_ptr(ahp, sidx); 2949 if (ap != NULL) { 2950 if (ap->an_refcnt != 1) { 2951 panic("anon_try_demote_pages: an_refcnt != 1"); 2952 } 2953 pp = ppa[i] = page_lookup(ap->an_vp, ap->an_off, 2954 SE_EXCL); 2955 if (pp != NULL) { 2956 (void) hat_pageunload(pp, 2957 HAT_FORCE_PGUNLOAD); 2958 } 2959 } else { 2960 ppa[i] = NULL; 2961 } 2962 } 2963 for (i = 0; i < pgcnt; i++) { 2964 if ((pp = ppa[i]) != NULL && pp->p_szc != 0) { 2965 ASSERT(pp->p_szc <= szc); 2966 if (!root) { 2967 VM_STAT_ADD(anonvmstats.demotepages[3]); 2968 if (curnpgs != 0) 2969 panic("anon_try_demote_pages: " 2970 "bad large page"); 2971 2972 root = 1; 2973 curnpgs = npgs = 2974 page_get_pagecnt(pp->p_szc); 2975 2976 ASSERT(npgs <= pgcnt); 2977 ASSERT(IS_P2ALIGNED(npgs, npgs)); 2978 ASSERT(!(page_pptonum(pp) & 2979 (npgs - 1))); 2980 } else { 2981 ASSERT(i > 0); 2982 ASSERT(page_pptonum(pp) - 1 == 2983 page_pptonum(ppa[i - 1])); 2984 if ((page_pptonum(pp) & (npgs - 1)) == 2985 npgs - 1) 2986 root = 0; 2987 } 2988 ASSERT(PAGE_EXCL(pp)); 2989 pp->p_szc = 0; 2990 ASSERT(curnpgs > 0); 2991 curnpgs--; 2992 } 2993 } 2994 if (root != 0 || curnpgs != 0) 2995 panic("anon_try_demote_pages: bad large page"); 2996 2997 for (i = 0; i < pgcnt; i++) { 2998 if ((pp = ppa[i]) != NULL) { 2999 ASSERT(!hat_page_is_mapped(pp)); 3000 ASSERT(pp->p_szc == 0); 3001 page_unlock(pp); 3002 } 3003 } 3004 if (ppasize != 0) { 3005 kmem_free(ppa, ppasize); 3006 } 3007 return (1); 3008 } 3009 3010 /* 3011 * anon_map_demotepages() can only be called by MAP_PRIVATE segments. 3012 */ 3013 int 3014 anon_map_demotepages( 3015 struct anon_map *amp, 3016 ulong_t start_idx, 3017 struct seg *seg, 3018 caddr_t addr, 3019 uint_t prot, 3020 struct vpage vpage[], 3021 struct cred *cred) 3022 { 3023 struct anon *ap; 3024 uint_t szc = seg->s_szc; 3025 pgcnt_t pgcnt = page_get_pagecnt(szc); 3026 size_t ppasize = pgcnt * sizeof (page_t *); 3027 page_t **ppa = kmem_alloc(ppasize, KM_SLEEP); 3028 page_t *pp; 3029 page_t *pl[2]; 3030 pgcnt_t i, pg_idx; 3031 ulong_t an_idx; 3032 caddr_t vaddr; 3033 int err; 3034 int retry = 0; 3035 uint_t vpprot; 3036 3037 ASSERT(RW_WRITE_HELD(&->a_rwlock)); 3038 ASSERT(IS_P2ALIGNED(pgcnt, pgcnt)); 3039 ASSERT(IS_P2ALIGNED(start_idx, pgcnt)); 3040 ASSERT(ppa != NULL); 3041 ASSERT(szc != 0); 3042 ASSERT(szc == amp->a_szc); 3043 3044 VM_STAT_ADD(anonvmstats.demotepages[0]); 3045 3046 top: 3047 if (anon_try_demote_pages(amp->ahp, start_idx, szc, ppa, 1)) { 3048 kmem_free(ppa, ppasize); 3049 return (0); 3050 } 3051 3052 VM_STAT_ADD(anonvmstats.demotepages[4]); 3053 3054 ASSERT(retry == 0); /* we can be here only once */ 3055 3056 vaddr = addr; 3057 for (pg_idx = 0, an_idx = start_idx; pg_idx < pgcnt; 3058 pg_idx++, an_idx++, vaddr += PAGESIZE) { 3059 ap = anon_get_ptr(amp->ahp, an_idx); 3060 if (ap == NULL) 3061 panic("anon_map_demotepages: no anon slot"); 3062 err = anon_getpage(&ap, &vpprot, pl, PAGESIZE, seg, vaddr, 3063 S_READ, cred); 3064 if (err) { 3065 for (i = 0; i < pg_idx; i++) { 3066 if ((pp = ppa[i]) != NULL) 3067 page_unlock(pp); 3068 } 3069 kmem_free(ppa, ppasize); 3070 return (err); 3071 } 3072 ppa[pg_idx] = pl[0]; 3073 } 3074 3075 err = anon_map_privatepages(amp, start_idx, szc, seg, addr, prot, ppa, 3076 vpage, -1, 0, cred); 3077 if (err > 0) { 3078 VM_STAT_ADD(anonvmstats.demotepages[5]); 3079 kmem_free(ppa, ppasize); 3080 return (err); 3081 } 3082 ASSERT(err == 0 || err == -1); 3083 if (err == -1) { 3084 VM_STAT_ADD(anonvmstats.demotepages[6]); 3085 retry = 1; 3086 goto top; 3087 } 3088 for (i = 0; i < pgcnt; i++) { 3089 ASSERT(ppa[i] != NULL); 3090 if (ppa[i]->p_szc != 0) 3091 retry = 1; 3092 page_unlock(ppa[i]); 3093 } 3094 if (retry) { 3095 VM_STAT_ADD(anonvmstats.demotepages[7]); 3096 goto top; 3097 } 3098 3099 VM_STAT_ADD(anonvmstats.demotepages[8]); 3100 3101 kmem_free(ppa, ppasize); 3102 3103 return (0); 3104 } 3105 3106 /* 3107 * Free pages of shared anon map. It's assumed that anon maps don't share anon 3108 * structures with private anon maps. Therefore all anon structures should 3109 * have at most one reference at this point. This means underlying pages can 3110 * be exclusively locked and demoted or freed. If not freeing the entire 3111 * large pages demote the ends of the region we free to be able to free 3112 * subpages. Page roots correspend to aligned index positions in anon map. 3113 */ 3114 void 3115 anon_shmap_free_pages(struct anon_map *amp, ulong_t sidx, size_t len) 3116 { 3117 ulong_t eidx = sidx + btopr(len); 3118 pgcnt_t pages = page_get_pagecnt(amp->a_szc); 3119 struct anon_hdr *ahp = amp->ahp; 3120 ulong_t tidx; 3121 size_t size; 3122 ulong_t sidx_aligned; 3123 ulong_t eidx_aligned; 3124 3125 ASSERT(RW_WRITE_HELD(&->a_rwlock)); 3126 ASSERT(amp->refcnt <= 1); 3127 ASSERT(amp->a_szc > 0); 3128 ASSERT(eidx <= ahp->size); 3129 ASSERT(!anon_share(ahp, sidx, btopr(len))); 3130 3131 if (len == 0) { /* XXX */ 3132 return; 3133 } 3134 3135 sidx_aligned = P2ALIGN(sidx, pages); 3136 if (sidx_aligned != sidx || 3137 (eidx < sidx_aligned + pages && eidx < ahp->size)) { 3138 if (!anon_try_demote_pages(ahp, sidx_aligned, 3139 amp->a_szc, NULL, 0)) { 3140 panic("anon_shmap_free_pages: demote failed"); 3141 } 3142 size = (eidx <= sidx_aligned + pages) ? (eidx - sidx) : 3143 P2NPHASE(sidx, pages); 3144 size <<= PAGESHIFT; 3145 anon_free(ahp, sidx, size); 3146 sidx = sidx_aligned + pages; 3147 if (eidx <= sidx) { 3148 return; 3149 } 3150 } 3151 eidx_aligned = P2ALIGN(eidx, pages); 3152 if (sidx < eidx_aligned) { 3153 anon_free_pages(ahp, sidx, 3154 (eidx_aligned - sidx) << PAGESHIFT, 3155 amp->a_szc); 3156 sidx = eidx_aligned; 3157 } 3158 ASSERT(sidx == eidx_aligned); 3159 if (eidx == eidx_aligned) { 3160 return; 3161 } 3162 tidx = eidx; 3163 if (eidx != ahp->size && anon_get_next_ptr(ahp, &tidx) != NULL && 3164 tidx - sidx < pages) { 3165 if (!anon_try_demote_pages(ahp, sidx, amp->a_szc, NULL, 0)) { 3166 panic("anon_shmap_free_pages: demote failed"); 3167 } 3168 size = (eidx - sidx) << PAGESHIFT; 3169 anon_free(ahp, sidx, size); 3170 } else { 3171 anon_free_pages(ahp, sidx, pages << PAGESHIFT, amp->a_szc); 3172 } 3173 } 3174 3175 /* 3176 * Allocate and initialize an anon_map structure for seg 3177 * associating the given swap reservation with the new anon_map. 3178 */ 3179 struct anon_map * 3180 anonmap_alloc(size_t size, size_t swresv, int flags) 3181 { 3182 struct anon_map *amp; 3183 int kmflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP; 3184 3185 amp = kmem_cache_alloc(anonmap_cache, kmflags); 3186 if (amp == NULL) { 3187 ASSERT(kmflags == KM_NOSLEEP); 3188 return (NULL); 3189 } 3190 3191 amp->ahp = anon_create(btopr(size), flags); 3192 if (amp->ahp == NULL) { 3193 ASSERT(flags == ANON_NOSLEEP); 3194 kmem_cache_free(anonmap_cache, amp); 3195 return (NULL); 3196 } 3197 amp->refcnt = 1; 3198 amp->size = size; 3199 amp->swresv = swresv; 3200 amp->locality = 0; 3201 amp->a_szc = 0; 3202 amp->a_sp = NULL; 3203 return (amp); 3204 } 3205 3206 void 3207 anonmap_free(struct anon_map *amp) 3208 { 3209 ASSERT(amp->ahp); 3210 ASSERT(amp->refcnt == 0); 3211 3212 lgrp_shm_policy_fini(amp, NULL); 3213 anon_release(amp->ahp, btopr(amp->size)); 3214 kmem_cache_free(anonmap_cache, amp); 3215 } 3216 3217 /* 3218 * Returns true if the app array has some empty slots. 3219 * The offp and lenp paramters are in/out paramters. On entry 3220 * these values represent the starting offset and length of the 3221 * mapping. When true is returned, these values may be modified 3222 * to be the largest range which includes empty slots. 3223 */ 3224 int 3225 non_anon(struct anon_hdr *ahp, ulong_t anon_idx, u_offset_t *offp, 3226 size_t *lenp) 3227 { 3228 ulong_t i, el; 3229 ssize_t low, high; 3230 struct anon *ap; 3231 3232 low = -1; 3233 for (i = 0, el = *lenp; i < el; i += PAGESIZE, anon_idx++) { 3234 ap = anon_get_ptr(ahp, anon_idx); 3235 if (ap == NULL) { 3236 if (low == -1) 3237 low = i; 3238 high = i; 3239 } 3240 } 3241 if (low != -1) { 3242 /* 3243 * Found at least one non-anon page. 3244 * Set up the off and len return values. 3245 */ 3246 if (low != 0) 3247 *offp += low; 3248 *lenp = high - low + PAGESIZE; 3249 return (1); 3250 } 3251 return (0); 3252 } 3253 3254 /* 3255 * Return a count of the number of existing anon pages in the anon array 3256 * app in the range (off, off+len). The array and slots must be guaranteed 3257 * stable by the caller. 3258 */ 3259 pgcnt_t 3260 anon_pages(struct anon_hdr *ahp, ulong_t anon_index, pgcnt_t nslots) 3261 { 3262 pgcnt_t cnt = 0; 3263 3264 while (nslots-- > 0) { 3265 if ((anon_get_ptr(ahp, anon_index)) != NULL) 3266 cnt++; 3267 anon_index++; 3268 } 3269 return (cnt); 3270 } 3271 3272 /* 3273 * Move reserved phys swap into memory swap (unreserve phys swap 3274 * and reserve mem swap by the same amount). 3275 * Used by segspt when it needs to lock resrved swap npages in memory 3276 */ 3277 int 3278 anon_swap_adjust(pgcnt_t npages) 3279 { 3280 pgcnt_t unlocked_mem_swap; 3281 3282 mutex_enter(&anoninfo_lock); 3283 3284 ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 3285 ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 3286 3287 unlocked_mem_swap = k_anoninfo.ani_mem_resv 3288 - k_anoninfo.ani_locked_swap; 3289 if (npages > unlocked_mem_swap) { 3290 spgcnt_t adjusted_swap = npages - unlocked_mem_swap; 3291 3292 /* 3293 * if there is not enough unlocked mem swap we take missing 3294 * amount from phys swap and give it to mem swap 3295 */ 3296 if (!page_reclaim_mem(adjusted_swap, segspt_minfree, 1)) { 3297 mutex_exit(&anoninfo_lock); 3298 return (ENOMEM); 3299 } 3300 3301 k_anoninfo.ani_mem_resv += adjusted_swap; 3302 ASSERT(k_anoninfo.ani_phys_resv >= adjusted_swap); 3303 k_anoninfo.ani_phys_resv -= adjusted_swap; 3304 3305 ANI_ADD(adjusted_swap); 3306 } 3307 k_anoninfo.ani_locked_swap += npages; 3308 3309 ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap); 3310 ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv); 3311 3312 mutex_exit(&anoninfo_lock); 3313 3314 return (0); 3315 } 3316 3317 /* 3318 * 'unlocked' reserved mem swap so when it is unreserved it 3319 * can be moved back phys (disk) swap 3320 */ 3321 void 3322 anon_swap_restore(pgcnt_t npages) 3323 { 3324 mutex_enter(&anoninfo_lock); 3325 3326 ASSERT(k_anoninfo.ani_locked_swap <= k_anoninfo.ani_mem_resv); 3327 3328 ASSERT(k_anoninfo.ani_locked_swap >= npages); 3329 k_anoninfo.ani_locked_swap -= npages; 3330 3331 ASSERT(k_anoninfo.ani_locked_swap <= k_anoninfo.ani_mem_resv); 3332 3333 mutex_exit(&anoninfo_lock); 3334 } 3335 3336 /* 3337 * Return the pointer from the list for a 3338 * specified anon index. 3339 */ 3340 ulong_t * 3341 anon_get_slot(struct anon_hdr *ahp, ulong_t an_idx) 3342 { 3343 struct anon **app; 3344 void **ppp; 3345 3346 ASSERT(an_idx < ahp->size); 3347 3348 /* 3349 * Single level case. 3350 */ 3351 if ((ahp->size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) { 3352 return ((ulong_t *)&ahp->array_chunk[an_idx]); 3353 } else { 3354 3355 /* 3356 * 2 level case. 3357 */ 3358 ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT]; 3359 if (*ppp == NULL) { 3360 mutex_enter(&ahp->serial_lock); 3361 ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT]; 3362 if (*ppp == NULL) 3363 *ppp = kmem_zalloc(PAGESIZE, KM_SLEEP); 3364 mutex_exit(&ahp->serial_lock); 3365 } 3366 app = *ppp; 3367 return ((ulong_t *)&app[an_idx & ANON_CHUNK_OFF]); 3368 } 3369 } 3370 3371 void 3372 anon_array_enter(struct anon_map *amp, ulong_t an_idx, anon_sync_obj_t *sobj) 3373 { 3374 ulong_t *ap_slot; 3375 kmutex_t *mtx; 3376 kcondvar_t *cv; 3377 int hash; 3378 3379 /* 3380 * Use szc to determine anon slot(s) to appear atomic. 3381 * If szc = 0, then lock the anon slot and mark it busy. 3382 * If szc > 0, then lock the range of slots by getting the 3383 * anon_array_lock for the first anon slot, and mark only the 3384 * first anon slot busy to represent whole range being busy. 3385 */ 3386 3387 ASSERT(RW_READ_HELD(&->a_rwlock)); 3388 an_idx = P2ALIGN(an_idx, page_get_pagecnt(amp->a_szc)); 3389 hash = ANON_ARRAY_HASH(amp, an_idx); 3390 sobj->sync_mutex = mtx = &anon_array_lock[hash].pad_mutex; 3391 sobj->sync_cv = cv = &anon_array_cv[hash]; 3392 mutex_enter(mtx); 3393 ap_slot = anon_get_slot(amp->ahp, an_idx); 3394 while (ANON_ISBUSY(ap_slot)) 3395 cv_wait(cv, mtx); 3396 ANON_SETBUSY(ap_slot); 3397 sobj->sync_data = ap_slot; 3398 mutex_exit(mtx); 3399 } 3400 3401 int 3402 anon_array_try_enter(struct anon_map *amp, ulong_t an_idx, 3403 anon_sync_obj_t *sobj) 3404 { 3405 ulong_t *ap_slot; 3406 kmutex_t *mtx; 3407 int hash; 3408 3409 /* 3410 * Try to lock a range of anon slots. 3411 * Use szc to determine anon slot(s) to appear atomic. 3412 * If szc = 0, then lock the anon slot and mark it busy. 3413 * If szc > 0, then lock the range of slots by getting the 3414 * anon_array_lock for the first anon slot, and mark only the 3415 * first anon slot busy to represent whole range being busy. 3416 * Fail if the mutex or the anon_array are busy. 3417 */ 3418 3419 ASSERT(RW_READ_HELD(&->a_rwlock)); 3420 an_idx = P2ALIGN(an_idx, page_get_pagecnt(amp->a_szc)); 3421 hash = ANON_ARRAY_HASH(amp, an_idx); 3422 sobj->sync_mutex = mtx = &anon_array_lock[hash].pad_mutex; 3423 sobj->sync_cv = &anon_array_cv[hash]; 3424 if (!mutex_tryenter(mtx)) { 3425 return (EWOULDBLOCK); 3426 } 3427 ap_slot = anon_get_slot(amp->ahp, an_idx); 3428 if (ANON_ISBUSY(ap_slot)) { 3429 mutex_exit(mtx); 3430 return (EWOULDBLOCK); 3431 } 3432 ANON_SETBUSY(ap_slot); 3433 sobj->sync_data = ap_slot; 3434 mutex_exit(mtx); 3435 return (0); 3436 } 3437 3438 void 3439 anon_array_exit(anon_sync_obj_t *sobj) 3440 { 3441 mutex_enter(sobj->sync_mutex); 3442 ASSERT(ANON_ISBUSY(sobj->sync_data)); 3443 ANON_CLRBUSY(sobj->sync_data); 3444 if (CV_HAS_WAITERS(sobj->sync_cv)) 3445 cv_broadcast(sobj->sync_cv); 3446 mutex_exit(sobj->sync_mutex); 3447 } 3448