10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52048Sstans * Common Development and Distribution License (the "License"). 62048Sstans * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 228555SJustin.Frank@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 310Sstevel@tonic-gate * The Regents of the University of California 320Sstevel@tonic-gate * All Rights Reserved 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 360Sstevel@tonic-gate * contributors. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate /* 400Sstevel@tonic-gate * VM - physical page management. 410Sstevel@tonic-gate */ 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include <sys/types.h> 440Sstevel@tonic-gate #include <sys/t_lock.h> 450Sstevel@tonic-gate #include <sys/param.h> 460Sstevel@tonic-gate #include <sys/systm.h> 470Sstevel@tonic-gate #include <sys/errno.h> 480Sstevel@tonic-gate #include <sys/time.h> 490Sstevel@tonic-gate #include <sys/vnode.h> 500Sstevel@tonic-gate #include <sys/vm.h> 510Sstevel@tonic-gate #include <sys/vtrace.h> 520Sstevel@tonic-gate #include <sys/swap.h> 530Sstevel@tonic-gate #include <sys/cmn_err.h> 540Sstevel@tonic-gate #include <sys/tuneable.h> 550Sstevel@tonic-gate #include <sys/sysmacros.h> 560Sstevel@tonic-gate #include <sys/cpuvar.h> 570Sstevel@tonic-gate #include <sys/callb.h> 580Sstevel@tonic-gate #include <sys/debug.h> 590Sstevel@tonic-gate #include <sys/tnf_probe.h> 600Sstevel@tonic-gate #include <sys/condvar_impl.h> 610Sstevel@tonic-gate #include <sys/mem_config.h> 620Sstevel@tonic-gate #include <sys/mem_cage.h> 630Sstevel@tonic-gate #include <sys/kmem.h> 640Sstevel@tonic-gate #include <sys/atomic.h> 650Sstevel@tonic-gate #include <sys/strlog.h> 660Sstevel@tonic-gate #include <sys/mman.h> 670Sstevel@tonic-gate #include <sys/ontrap.h> 680Sstevel@tonic-gate #include <sys/lgrp.h> 690Sstevel@tonic-gate #include <sys/vfs.h> 700Sstevel@tonic-gate 710Sstevel@tonic-gate #include <vm/hat.h> 720Sstevel@tonic-gate #include <vm/anon.h> 730Sstevel@tonic-gate #include <vm/page.h> 740Sstevel@tonic-gate #include <vm/seg.h> 750Sstevel@tonic-gate #include <vm/pvn.h> 760Sstevel@tonic-gate #include <vm/seg_kmem.h> 770Sstevel@tonic-gate #include <vm/vm_dep.h> 783247Sgjelinek #include <sys/vm_usage.h> 790Sstevel@tonic-gate #include <fs/fs_subr.h> 803480Sjfrank #include <sys/ddi.h> 813480Sjfrank #include <sys/modctl.h> 820Sstevel@tonic-gate 830Sstevel@tonic-gate static int nopageage = 0; 840Sstevel@tonic-gate 850Sstevel@tonic-gate static pgcnt_t max_page_get; /* max page_get request size in pages */ 860Sstevel@tonic-gate pgcnt_t total_pages = 0; /* total number of pages (used by /proc) */ 870Sstevel@tonic-gate 880Sstevel@tonic-gate /* 890Sstevel@tonic-gate * freemem_lock protects all freemem variables: 900Sstevel@tonic-gate * availrmem. Also this lock protects the globals which track the 910Sstevel@tonic-gate * availrmem changes for accurate kernel footprint calculation. 920Sstevel@tonic-gate * See below for an explanation of these 930Sstevel@tonic-gate * globals. 940Sstevel@tonic-gate */ 950Sstevel@tonic-gate kmutex_t freemem_lock; 960Sstevel@tonic-gate pgcnt_t availrmem; 970Sstevel@tonic-gate pgcnt_t availrmem_initial; 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * These globals track availrmem changes to get a more accurate 1010Sstevel@tonic-gate * estimate of tke kernel size. Historically pp_kernel is used for 1020Sstevel@tonic-gate * kernel size and is based on availrmem. But availrmem is adjusted for 1030Sstevel@tonic-gate * locked pages in the system not just for kernel locked pages. 1040Sstevel@tonic-gate * These new counters will track the pages locked through segvn and 1050Sstevel@tonic-gate * by explicit user locking. 1060Sstevel@tonic-gate * 1075331Samw * pages_locked : How many pages are locked because of user specified 1080Sstevel@tonic-gate * locking through mlock or plock. 1090Sstevel@tonic-gate * 1100Sstevel@tonic-gate * pages_useclaim,pages_claimed : These two variables track the 1115331Samw * claim adjustments because of the protection changes on a segvn segment. 1120Sstevel@tonic-gate * 1130Sstevel@tonic-gate * All these globals are protected by the same lock which protects availrmem. 1140Sstevel@tonic-gate */ 1156695Saguzovsk pgcnt_t pages_locked = 0; 1166695Saguzovsk pgcnt_t pages_useclaim = 0; 1176695Saguzovsk pgcnt_t pages_claimed = 0; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * new_freemem_lock protects freemem, freemem_wait & freemem_cv. 1220Sstevel@tonic-gate */ 1230Sstevel@tonic-gate static kmutex_t new_freemem_lock; 1240Sstevel@tonic-gate static uint_t freemem_wait; /* someone waiting for freemem */ 1250Sstevel@tonic-gate static kcondvar_t freemem_cv; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate /* 1280Sstevel@tonic-gate * The logical page free list is maintained as two lists, the 'free' 1290Sstevel@tonic-gate * and the 'cache' lists. 1300Sstevel@tonic-gate * The free list contains those pages that should be reused first. 1310Sstevel@tonic-gate * 1320Sstevel@tonic-gate * The implementation of the lists is machine dependent. 1330Sstevel@tonic-gate * page_get_freelist(), page_get_cachelist(), 1340Sstevel@tonic-gate * page_list_sub(), and page_list_add() 1350Sstevel@tonic-gate * form the interface to the machine dependent implementation. 1360Sstevel@tonic-gate * 1370Sstevel@tonic-gate * Pages with p_free set are on the cache list. 1380Sstevel@tonic-gate * Pages with p_free and p_age set are on the free list, 1390Sstevel@tonic-gate * 1400Sstevel@tonic-gate * A page may be locked while on either list. 1410Sstevel@tonic-gate */ 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate /* 1440Sstevel@tonic-gate * free list accounting stuff. 1450Sstevel@tonic-gate * 1460Sstevel@tonic-gate * 1470Sstevel@tonic-gate * Spread out the value for the number of pages on the 1480Sstevel@tonic-gate * page free and page cache lists. If there is just one 1490Sstevel@tonic-gate * value, then it must be under just one lock. 1500Sstevel@tonic-gate * The lock contention and cache traffic are a real bother. 1510Sstevel@tonic-gate * 1520Sstevel@tonic-gate * When we acquire and then drop a single pcf lock 1530Sstevel@tonic-gate * we can start in the middle of the array of pcf structures. 1540Sstevel@tonic-gate * If we acquire more than one pcf lock at a time, we need to 1550Sstevel@tonic-gate * start at the front to avoid deadlocking. 1560Sstevel@tonic-gate * 1570Sstevel@tonic-gate * pcf_count holds the number of pages in each pool. 1580Sstevel@tonic-gate * 1590Sstevel@tonic-gate * pcf_block is set when page_create_get_something() has asked the 1600Sstevel@tonic-gate * PSM page freelist and page cachelist routines without specifying 1610Sstevel@tonic-gate * a color and nothing came back. This is used to block anything 1620Sstevel@tonic-gate * else from moving pages from one list to the other while the 1630Sstevel@tonic-gate * lists are searched again. If a page is freeed while pcf_block is 1640Sstevel@tonic-gate * set, then pcf_reserve is incremented. pcgs_unblock() takes care 1650Sstevel@tonic-gate * of clearning pcf_block, doing the wakeups, etc. 1660Sstevel@tonic-gate */ 1670Sstevel@tonic-gate 1686880Sdv142724 #define MAX_PCF_FANOUT NCPU 1696880Sdv142724 static uint_t pcf_fanout = 1; /* Will get changed at boot time */ 1706880Sdv142724 static uint_t pcf_fanout_mask = 0; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate struct pcf { 1732290Sfr157268 kmutex_t pcf_lock; /* protects the structure */ 1740Sstevel@tonic-gate uint_t pcf_count; /* page count */ 1750Sstevel@tonic-gate uint_t pcf_wait; /* number of waiters */ 1760Sstevel@tonic-gate uint_t pcf_block; /* pcgs flag to page_free() */ 1770Sstevel@tonic-gate uint_t pcf_reserve; /* pages freed after pcf_block set */ 1786880Sdv142724 uint_t pcf_fill[10]; /* to line up on the caches */ 1790Sstevel@tonic-gate }; 1800Sstevel@tonic-gate 1816880Sdv142724 /* 1826880Sdv142724 * PCF_INDEX hash needs to be dynamic (every so often the hash changes where 1836880Sdv142724 * it will hash the cpu to). This is done to prevent a drain condition 1846880Sdv142724 * from happening. This drain condition will occur when pcf_count decrement 1856880Sdv142724 * occurs on cpu A and the increment of pcf_count always occurs on cpu B. An 1866880Sdv142724 * example of this shows up with device interrupts. The dma buffer is allocated 1876880Sdv142724 * by the cpu requesting the IO thus the pcf_count is decremented based on that. 1886880Sdv142724 * When the memory is returned by the interrupt thread, the pcf_count will be 1896880Sdv142724 * incremented based on the cpu servicing the interrupt. 1906880Sdv142724 */ 1916880Sdv142724 static struct pcf pcf[MAX_PCF_FANOUT]; 1926880Sdv142724 #define PCF_INDEX() ((int)(((long)CPU->cpu_seqid) + \ 1936880Sdv142724 (randtick() >> 24)) & (pcf_fanout_mask)) 1946880Sdv142724 1956880Sdv142724 static int pcf_decrement_bucket(pgcnt_t); 1966880Sdv142724 static int pcf_decrement_multiple(pgcnt_t *, pgcnt_t, int); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate kmutex_t pcgs_lock; /* serializes page_create_get_ */ 1990Sstevel@tonic-gate kmutex_t pcgs_cagelock; /* serializes NOSLEEP cage allocs */ 2000Sstevel@tonic-gate kmutex_t pcgs_wait_lock; /* used for delay in pcgs */ 2010Sstevel@tonic-gate static kcondvar_t pcgs_cv; /* cv for delay in pcgs */ 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate #ifdef VM_STATS 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate /* 2060Sstevel@tonic-gate * No locks, but so what, they are only statistics. 2070Sstevel@tonic-gate */ 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate static struct page_tcnt { 2100Sstevel@tonic-gate int pc_free_cache; /* free's into cache list */ 2110Sstevel@tonic-gate int pc_free_dontneed; /* free's with dontneed */ 2120Sstevel@tonic-gate int pc_free_pageout; /* free's from pageout */ 2130Sstevel@tonic-gate int pc_free_free; /* free's into free list */ 2140Sstevel@tonic-gate int pc_free_pages; /* free's into large page free list */ 2150Sstevel@tonic-gate int pc_destroy_pages; /* large page destroy's */ 2160Sstevel@tonic-gate int pc_get_cache; /* get's from cache list */ 2170Sstevel@tonic-gate int pc_get_free; /* get's from free list */ 2180Sstevel@tonic-gate int pc_reclaim; /* reclaim's */ 2190Sstevel@tonic-gate int pc_abortfree; /* abort's of free pages */ 2200Sstevel@tonic-gate int pc_find_hit; /* find's that find page */ 2210Sstevel@tonic-gate int pc_find_miss; /* find's that don't find page */ 2220Sstevel@tonic-gate int pc_destroy_free; /* # of free pages destroyed */ 2230Sstevel@tonic-gate #define PC_HASH_CNT (4*PAGE_HASHAVELEN) 2240Sstevel@tonic-gate int pc_find_hashlen[PC_HASH_CNT+1]; 2250Sstevel@tonic-gate int pc_addclaim_pages; 2260Sstevel@tonic-gate int pc_subclaim_pages; 2270Sstevel@tonic-gate int pc_free_replacement_page[2]; 2280Sstevel@tonic-gate int pc_try_demote_pages[6]; 2290Sstevel@tonic-gate int pc_demote_pages[2]; 2300Sstevel@tonic-gate } pagecnt; 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate uint_t hashin_count; 2330Sstevel@tonic-gate uint_t hashin_not_held; 2340Sstevel@tonic-gate uint_t hashin_already; 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate uint_t hashout_count; 2370Sstevel@tonic-gate uint_t hashout_not_held; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate uint_t page_create_count; 2400Sstevel@tonic-gate uint_t page_create_not_enough; 2410Sstevel@tonic-gate uint_t page_create_not_enough_again; 2420Sstevel@tonic-gate uint_t page_create_zero; 2430Sstevel@tonic-gate uint_t page_create_hashout; 2440Sstevel@tonic-gate uint_t page_create_page_lock_failed; 2450Sstevel@tonic-gate uint_t page_create_trylock_failed; 2460Sstevel@tonic-gate uint_t page_create_found_one; 2470Sstevel@tonic-gate uint_t page_create_hashin_failed; 2480Sstevel@tonic-gate uint_t page_create_dropped_phm; 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate uint_t page_create_new; 2510Sstevel@tonic-gate uint_t page_create_exists; 2520Sstevel@tonic-gate uint_t page_create_putbacks; 2530Sstevel@tonic-gate uint_t page_create_overshoot; 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate uint_t page_reclaim_zero; 2560Sstevel@tonic-gate uint_t page_reclaim_zero_locked; 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate uint_t page_rename_exists; 2590Sstevel@tonic-gate uint_t page_rename_count; 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate uint_t page_lookup_cnt[20]; 2620Sstevel@tonic-gate uint_t page_lookup_nowait_cnt[10]; 2630Sstevel@tonic-gate uint_t page_find_cnt; 2640Sstevel@tonic-gate uint_t page_exists_cnt; 2650Sstevel@tonic-gate uint_t page_exists_forreal_cnt; 2660Sstevel@tonic-gate uint_t page_lookup_dev_cnt; 2670Sstevel@tonic-gate uint_t get_cachelist_cnt; 2680Sstevel@tonic-gate uint_t page_create_cnt[10]; 2695466Skchow uint_t alloc_pages[9]; 2700Sstevel@tonic-gate uint_t page_exphcontg[19]; 2710Sstevel@tonic-gate uint_t page_create_large_cnt[10]; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate /* 2740Sstevel@tonic-gate * Collects statistics. 2750Sstevel@tonic-gate */ 2760Sstevel@tonic-gate #define PAGE_HASH_SEARCH(index, pp, vp, off) { \ 2770Sstevel@tonic-gate uint_t mylen = 0; \ 2780Sstevel@tonic-gate \ 2790Sstevel@tonic-gate for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash, mylen++) { \ 2800Sstevel@tonic-gate if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \ 2810Sstevel@tonic-gate break; \ 2820Sstevel@tonic-gate } \ 2830Sstevel@tonic-gate if ((pp) != NULL) \ 2840Sstevel@tonic-gate pagecnt.pc_find_hit++; \ 2850Sstevel@tonic-gate else \ 2860Sstevel@tonic-gate pagecnt.pc_find_miss++; \ 2870Sstevel@tonic-gate if (mylen > PC_HASH_CNT) \ 2880Sstevel@tonic-gate mylen = PC_HASH_CNT; \ 2890Sstevel@tonic-gate pagecnt.pc_find_hashlen[mylen]++; \ 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate #else /* VM_STATS */ 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * Don't collect statistics 2960Sstevel@tonic-gate */ 2970Sstevel@tonic-gate #define PAGE_HASH_SEARCH(index, pp, vp, off) { \ 2980Sstevel@tonic-gate for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash) { \ 2990Sstevel@tonic-gate if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \ 3000Sstevel@tonic-gate break; \ 3010Sstevel@tonic-gate } \ 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate #endif /* VM_STATS */ 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate #ifdef DEBUG 3090Sstevel@tonic-gate #define MEMSEG_SEARCH_STATS 3100Sstevel@tonic-gate #endif 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate #ifdef MEMSEG_SEARCH_STATS 3130Sstevel@tonic-gate struct memseg_stats { 3140Sstevel@tonic-gate uint_t nsearch; 3150Sstevel@tonic-gate uint_t nlastwon; 3160Sstevel@tonic-gate uint_t nhashwon; 3170Sstevel@tonic-gate uint_t nnotfound; 3180Sstevel@tonic-gate } memseg_stats; 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate #define MEMSEG_STAT_INCR(v) \ 3210Sstevel@tonic-gate atomic_add_32(&memseg_stats.v, 1) 3220Sstevel@tonic-gate #else 3230Sstevel@tonic-gate #define MEMSEG_STAT_INCR(x) 3240Sstevel@tonic-gate #endif 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate struct memseg *memsegs; /* list of memory segments */ 3270Sstevel@tonic-gate 3286181Smec /* 3296181Smec * /etc/system tunable to control large page allocation hueristic. 3306181Smec * 3316181Smec * Setting to LPAP_LOCAL will heavily prefer the local lgroup over remote lgroup 3326181Smec * for large page allocation requests. If a large page is not readily 3336181Smec * avaliable on the local freelists we will go through additional effort 3346181Smec * to create a large page, potentially moving smaller pages around to coalesce 3356181Smec * larger pages in the local lgroup. 3366181Smec * Default value of LPAP_DEFAULT will go to remote freelists if large pages 3376181Smec * are not readily available in the local lgroup. 3386181Smec */ 3396181Smec enum lpap { 3406181Smec LPAP_DEFAULT, /* default large page allocation policy */ 3416181Smec LPAP_LOCAL /* local large page allocation policy */ 3426181Smec }; 3436181Smec 3446181Smec enum lpap lpg_alloc_prefer = LPAP_DEFAULT; 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate static void page_init_mem_config(void); 3470Sstevel@tonic-gate static int page_do_hashin(page_t *, vnode_t *, u_offset_t); 3480Sstevel@tonic-gate static void page_do_hashout(page_t *); 3493253Smec static void page_capture_init(); 3503253Smec int page_capture_take_action(page_t *, uint_t, void *); 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate static void page_demote_vp_pages(page_t *); 3530Sstevel@tonic-gate 3546880Sdv142724 3556880Sdv142724 void 3566880Sdv142724 pcf_init(void) 3576880Sdv142724 3586880Sdv142724 { 3596880Sdv142724 if (boot_ncpus != -1) { 3606880Sdv142724 pcf_fanout = boot_ncpus; 3616880Sdv142724 } else { 3626880Sdv142724 pcf_fanout = max_ncpus; 3636880Sdv142724 } 3646880Sdv142724 #ifdef sun4v 3656880Sdv142724 /* 3666880Sdv142724 * Force at least 4 buckets if possible for sun4v. 3676880Sdv142724 */ 3686880Sdv142724 pcf_fanout = MAX(pcf_fanout, 4); 3696880Sdv142724 #endif /* sun4v */ 3706880Sdv142724 3716880Sdv142724 /* 3726880Sdv142724 * Round up to the nearest power of 2. 3736880Sdv142724 */ 3746880Sdv142724 pcf_fanout = MIN(pcf_fanout, MAX_PCF_FANOUT); 3756880Sdv142724 if (!ISP2(pcf_fanout)) { 3766880Sdv142724 pcf_fanout = 1 << highbit(pcf_fanout); 3776880Sdv142724 3786880Sdv142724 if (pcf_fanout > MAX_PCF_FANOUT) { 3796880Sdv142724 pcf_fanout = 1 << (highbit(MAX_PCF_FANOUT) - 1); 3806880Sdv142724 } 3816880Sdv142724 } 3826880Sdv142724 pcf_fanout_mask = pcf_fanout - 1; 3836880Sdv142724 } 3846880Sdv142724 3850Sstevel@tonic-gate /* 3860Sstevel@tonic-gate * vm subsystem related initialization 3870Sstevel@tonic-gate */ 3880Sstevel@tonic-gate void 3890Sstevel@tonic-gate vm_init(void) 3900Sstevel@tonic-gate { 3910Sstevel@tonic-gate boolean_t callb_vm_cpr(void *, int); 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate (void) callb_add(callb_vm_cpr, 0, CB_CL_CPR_VM, "vm"); 3940Sstevel@tonic-gate page_init_mem_config(); 395917Selowe page_retire_init(); 3963247Sgjelinek vm_usage_init(); 3973253Smec page_capture_init(); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate /* 4010Sstevel@tonic-gate * This function is called at startup and when memory is added or deleted. 4020Sstevel@tonic-gate */ 4030Sstevel@tonic-gate void 4040Sstevel@tonic-gate init_pages_pp_maximum() 4050Sstevel@tonic-gate { 4060Sstevel@tonic-gate static pgcnt_t p_min; 4070Sstevel@tonic-gate static pgcnt_t pages_pp_maximum_startup; 4080Sstevel@tonic-gate static pgcnt_t avrmem_delta; 4090Sstevel@tonic-gate static int init_done; 4100Sstevel@tonic-gate static int user_set; /* true if set in /etc/system */ 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate if (init_done == 0) { 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate /* If the user specified a value, save it */ 4150Sstevel@tonic-gate if (pages_pp_maximum != 0) { 4160Sstevel@tonic-gate user_set = 1; 4170Sstevel@tonic-gate pages_pp_maximum_startup = pages_pp_maximum; 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate /* 4210Sstevel@tonic-gate * Setting of pages_pp_maximum is based first time 4220Sstevel@tonic-gate * on the value of availrmem just after the start-up 4230Sstevel@tonic-gate * allocations. To preserve this relationship at run 4240Sstevel@tonic-gate * time, use a delta from availrmem_initial. 4250Sstevel@tonic-gate */ 4260Sstevel@tonic-gate ASSERT(availrmem_initial >= availrmem); 4270Sstevel@tonic-gate avrmem_delta = availrmem_initial - availrmem; 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate /* The allowable floor of pages_pp_maximum */ 4300Sstevel@tonic-gate p_min = tune.t_minarmem + 100; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate /* Make sure we don't come through here again. */ 4330Sstevel@tonic-gate init_done = 1; 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate /* 4360Sstevel@tonic-gate * Determine pages_pp_maximum, the number of currently available 4370Sstevel@tonic-gate * pages (availrmem) that can't be `locked'. If not set by 4380Sstevel@tonic-gate * the user, we set it to 4% of the currently available memory 4390Sstevel@tonic-gate * plus 4MB. 4400Sstevel@tonic-gate * But we also insist that it be greater than tune.t_minarmem; 4410Sstevel@tonic-gate * otherwise a process could lock down a lot of memory, get swapped 4420Sstevel@tonic-gate * out, and never have enough to get swapped back in. 4430Sstevel@tonic-gate */ 4440Sstevel@tonic-gate if (user_set) 4450Sstevel@tonic-gate pages_pp_maximum = pages_pp_maximum_startup; 4460Sstevel@tonic-gate else 4470Sstevel@tonic-gate pages_pp_maximum = ((availrmem_initial - avrmem_delta) / 25) 4480Sstevel@tonic-gate + btop(4 * 1024 * 1024); 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate if (pages_pp_maximum <= p_min) { 4510Sstevel@tonic-gate pages_pp_maximum = p_min; 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate void 4560Sstevel@tonic-gate set_max_page_get(pgcnt_t target_total_pages) 4570Sstevel@tonic-gate { 4580Sstevel@tonic-gate max_page_get = target_total_pages / 2; 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate static pgcnt_t pending_delete; 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate /*ARGSUSED*/ 4640Sstevel@tonic-gate static void 4650Sstevel@tonic-gate page_mem_config_post_add( 4660Sstevel@tonic-gate void *arg, 4670Sstevel@tonic-gate pgcnt_t delta_pages) 4680Sstevel@tonic-gate { 4690Sstevel@tonic-gate set_max_page_get(total_pages - pending_delete); 4700Sstevel@tonic-gate init_pages_pp_maximum(); 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate /*ARGSUSED*/ 4740Sstevel@tonic-gate static int 4750Sstevel@tonic-gate page_mem_config_pre_del( 4760Sstevel@tonic-gate void *arg, 4770Sstevel@tonic-gate pgcnt_t delta_pages) 4780Sstevel@tonic-gate { 4790Sstevel@tonic-gate pgcnt_t nv; 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate nv = atomic_add_long_nv(&pending_delete, (spgcnt_t)delta_pages); 4820Sstevel@tonic-gate set_max_page_get(total_pages - nv); 4830Sstevel@tonic-gate return (0); 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /*ARGSUSED*/ 4870Sstevel@tonic-gate static void 4880Sstevel@tonic-gate page_mem_config_post_del( 4890Sstevel@tonic-gate void *arg, 4900Sstevel@tonic-gate pgcnt_t delta_pages, 4910Sstevel@tonic-gate int cancelled) 4920Sstevel@tonic-gate { 4930Sstevel@tonic-gate pgcnt_t nv; 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate nv = atomic_add_long_nv(&pending_delete, -(spgcnt_t)delta_pages); 4960Sstevel@tonic-gate set_max_page_get(total_pages - nv); 4970Sstevel@tonic-gate if (!cancelled) 4980Sstevel@tonic-gate init_pages_pp_maximum(); 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate static kphysm_setup_vector_t page_mem_config_vec = { 5020Sstevel@tonic-gate KPHYSM_SETUP_VECTOR_VERSION, 5030Sstevel@tonic-gate page_mem_config_post_add, 5040Sstevel@tonic-gate page_mem_config_pre_del, 5050Sstevel@tonic-gate page_mem_config_post_del, 5060Sstevel@tonic-gate }; 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate static void 5090Sstevel@tonic-gate page_init_mem_config(void) 5100Sstevel@tonic-gate { 5110Sstevel@tonic-gate int ret; 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate ret = kphysm_setup_func_register(&page_mem_config_vec, (void *)NULL); 5140Sstevel@tonic-gate ASSERT(ret == 0); 5150Sstevel@tonic-gate } 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate /* 5180Sstevel@tonic-gate * Evenly spread out the PCF counters for large free pages 5190Sstevel@tonic-gate */ 5200Sstevel@tonic-gate static void 5210Sstevel@tonic-gate page_free_large_ctr(pgcnt_t npages) 5220Sstevel@tonic-gate { 5230Sstevel@tonic-gate static struct pcf *p = pcf; 5240Sstevel@tonic-gate pgcnt_t lump; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate freemem += npages; 5270Sstevel@tonic-gate 5286880Sdv142724 lump = roundup(npages, pcf_fanout) / pcf_fanout; 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate while (npages > 0) { 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate ASSERT(!p->pcf_block); 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate if (lump < npages) { 5350Sstevel@tonic-gate p->pcf_count += (uint_t)lump; 5360Sstevel@tonic-gate npages -= lump; 5370Sstevel@tonic-gate } else { 5380Sstevel@tonic-gate p->pcf_count += (uint_t)npages; 5390Sstevel@tonic-gate npages = 0; 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate ASSERT(!p->pcf_wait); 5430Sstevel@tonic-gate 5446880Sdv142724 if (++p > &pcf[pcf_fanout - 1]) 5450Sstevel@tonic-gate p = pcf; 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate ASSERT(npages == 0); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate /* 5525331Samw * Add a physical chunk of memory to the system free lists during startup. 5530Sstevel@tonic-gate * Platform specific startup() allocates the memory for the page structs. 5540Sstevel@tonic-gate * 5550Sstevel@tonic-gate * num - number of page structures 5560Sstevel@tonic-gate * base - page number (pfn) to be associated with the first page. 5570Sstevel@tonic-gate * 5580Sstevel@tonic-gate * Since we are doing this during startup (ie. single threaded), we will 5590Sstevel@tonic-gate * use shortcut routines to avoid any locking overhead while putting all 5600Sstevel@tonic-gate * these pages on the freelists. 5610Sstevel@tonic-gate * 5620Sstevel@tonic-gate * NOTE: Any changes performed to page_free(), must also be performed to 5630Sstevel@tonic-gate * add_physmem() since this is how we initialize all page_t's at 5640Sstevel@tonic-gate * boot time. 5650Sstevel@tonic-gate */ 5660Sstevel@tonic-gate void 5670Sstevel@tonic-gate add_physmem( 5680Sstevel@tonic-gate page_t *pp, 5690Sstevel@tonic-gate pgcnt_t num, 5700Sstevel@tonic-gate pfn_t pnum) 5710Sstevel@tonic-gate { 5720Sstevel@tonic-gate page_t *root = NULL; 5730Sstevel@tonic-gate uint_t szc = page_num_pagesizes() - 1; 5740Sstevel@tonic-gate pgcnt_t large = page_get_pagecnt(szc); 5750Sstevel@tonic-gate pgcnt_t cnt = 0; 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_INIT, 5783559Smec "add_physmem:pp %p num %lu", pp, num); 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate /* 5810Sstevel@tonic-gate * Arbitrarily limit the max page_get request 5820Sstevel@tonic-gate * to 1/2 of the page structs we have. 5830Sstevel@tonic-gate */ 5840Sstevel@tonic-gate total_pages += num; 5850Sstevel@tonic-gate set_max_page_get(total_pages); 5860Sstevel@tonic-gate 5871373Skchow PLCNT_MODIFY_MAX(pnum, (long)num); 5881373Skchow 5890Sstevel@tonic-gate /* 5900Sstevel@tonic-gate * The physical space for the pages array 5910Sstevel@tonic-gate * representing ram pages has already been 5920Sstevel@tonic-gate * allocated. Here we initialize each lock 5930Sstevel@tonic-gate * in the page structure, and put each on 5940Sstevel@tonic-gate * the free list 5950Sstevel@tonic-gate */ 596414Skchow for (; num; pp++, pnum++, num--) { 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate /* 5990Sstevel@tonic-gate * this needs to fill in the page number 6000Sstevel@tonic-gate * and do any other arch specific initialization 6010Sstevel@tonic-gate */ 6020Sstevel@tonic-gate add_physmem_cb(pp, pnum); 6030Sstevel@tonic-gate 6042414Saguzovsk pp->p_lckcnt = 0; 6052414Saguzovsk pp->p_cowcnt = 0; 6062414Saguzovsk pp->p_slckcnt = 0; 6072414Saguzovsk 6080Sstevel@tonic-gate /* 6090Sstevel@tonic-gate * Initialize the page lock as unlocked, since nobody 6100Sstevel@tonic-gate * can see or access this page yet. 6110Sstevel@tonic-gate */ 6120Sstevel@tonic-gate pp->p_selock = 0; 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate /* 6150Sstevel@tonic-gate * Initialize IO lock 6160Sstevel@tonic-gate */ 6170Sstevel@tonic-gate page_iolock_init(pp); 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate /* 6200Sstevel@tonic-gate * initialize other fields in the page_t 6210Sstevel@tonic-gate */ 6220Sstevel@tonic-gate PP_SETFREE(pp); 6237718SJason.Beloro@Sun.COM page_clr_all_props(pp, 0); 6240Sstevel@tonic-gate PP_SETAGED(pp); 6250Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1; 6260Sstevel@tonic-gate pp->p_next = pp; 6270Sstevel@tonic-gate pp->p_prev = pp; 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * Simple case: System doesn't support large pages. 6310Sstevel@tonic-gate */ 6320Sstevel@tonic-gate if (szc == 0) { 6330Sstevel@tonic-gate pp->p_szc = 0; 6340Sstevel@tonic-gate page_free_at_startup(pp); 6350Sstevel@tonic-gate continue; 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate /* 6390Sstevel@tonic-gate * Handle unaligned pages, we collect them up onto 6400Sstevel@tonic-gate * the root page until we have a full large page. 6410Sstevel@tonic-gate */ 6420Sstevel@tonic-gate if (!IS_P2ALIGNED(pnum, large)) { 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* 6450Sstevel@tonic-gate * If not in a large page, 6460Sstevel@tonic-gate * just free as small page. 6470Sstevel@tonic-gate */ 6480Sstevel@tonic-gate if (root == NULL) { 6490Sstevel@tonic-gate pp->p_szc = 0; 6500Sstevel@tonic-gate page_free_at_startup(pp); 6510Sstevel@tonic-gate continue; 6520Sstevel@tonic-gate } 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate /* 6550Sstevel@tonic-gate * Link a constituent page into the large page. 6560Sstevel@tonic-gate */ 6570Sstevel@tonic-gate pp->p_szc = szc; 6580Sstevel@tonic-gate page_list_concat(&root, &pp); 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /* 6610Sstevel@tonic-gate * When large page is fully formed, free it. 6620Sstevel@tonic-gate */ 6630Sstevel@tonic-gate if (++cnt == large) { 6640Sstevel@tonic-gate page_free_large_ctr(cnt); 6650Sstevel@tonic-gate page_list_add_pages(root, PG_LIST_ISINIT); 6660Sstevel@tonic-gate root = NULL; 6670Sstevel@tonic-gate cnt = 0; 6680Sstevel@tonic-gate } 6690Sstevel@tonic-gate continue; 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate /* 6730Sstevel@tonic-gate * At this point we have a page number which 6740Sstevel@tonic-gate * is aligned. We assert that we aren't already 6750Sstevel@tonic-gate * in a different large page. 6760Sstevel@tonic-gate */ 6770Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(pnum, large)); 6780Sstevel@tonic-gate ASSERT(root == NULL && cnt == 0); 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate /* 6810Sstevel@tonic-gate * If insufficient number of pages left to form 6820Sstevel@tonic-gate * a large page, just free the small page. 6830Sstevel@tonic-gate */ 6840Sstevel@tonic-gate if (num < large) { 6850Sstevel@tonic-gate pp->p_szc = 0; 6860Sstevel@tonic-gate page_free_at_startup(pp); 6870Sstevel@tonic-gate continue; 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate /* 6910Sstevel@tonic-gate * Otherwise start a new large page. 6920Sstevel@tonic-gate */ 6930Sstevel@tonic-gate pp->p_szc = szc; 6940Sstevel@tonic-gate cnt++; 6950Sstevel@tonic-gate root = pp; 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate ASSERT(root == NULL && cnt == 0); 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate /* 7010Sstevel@tonic-gate * Find a page representing the specified [vp, offset]. 7020Sstevel@tonic-gate * If we find the page but it is intransit coming in, 7030Sstevel@tonic-gate * it will have an "exclusive" lock and we wait for 7040Sstevel@tonic-gate * the i/o to complete. A page found on the free list 7050Sstevel@tonic-gate * is always reclaimed and then locked. On success, the page 7060Sstevel@tonic-gate * is locked, its data is valid and it isn't on the free 7070Sstevel@tonic-gate * list, while a NULL is returned if the page doesn't exist. 7080Sstevel@tonic-gate */ 7090Sstevel@tonic-gate page_t * 7100Sstevel@tonic-gate page_lookup(vnode_t *vp, u_offset_t off, se_t se) 7110Sstevel@tonic-gate { 7120Sstevel@tonic-gate return (page_lookup_create(vp, off, se, NULL, NULL, 0)); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate /* 7160Sstevel@tonic-gate * Find a page representing the specified [vp, offset]. 7170Sstevel@tonic-gate * We either return the one we found or, if passed in, 7180Sstevel@tonic-gate * create one with identity of [vp, offset] of the 7195331Samw * pre-allocated page. If we find existing page but it is 7200Sstevel@tonic-gate * intransit coming in, it will have an "exclusive" lock 7210Sstevel@tonic-gate * and we wait for the i/o to complete. A page found on 7220Sstevel@tonic-gate * the free list is always reclaimed and then locked. 7230Sstevel@tonic-gate * On success, the page is locked, its data is valid and 7240Sstevel@tonic-gate * it isn't on the free list, while a NULL is returned 7250Sstevel@tonic-gate * if the page doesn't exist and newpp is NULL; 7260Sstevel@tonic-gate */ 7270Sstevel@tonic-gate page_t * 7280Sstevel@tonic-gate page_lookup_create( 7290Sstevel@tonic-gate vnode_t *vp, 7300Sstevel@tonic-gate u_offset_t off, 7310Sstevel@tonic-gate se_t se, 7320Sstevel@tonic-gate page_t *newpp, 7330Sstevel@tonic-gate spgcnt_t *nrelocp, 7340Sstevel@tonic-gate int flags) 7350Sstevel@tonic-gate { 7360Sstevel@tonic-gate page_t *pp; 7370Sstevel@tonic-gate kmutex_t *phm; 7380Sstevel@tonic-gate ulong_t index; 7390Sstevel@tonic-gate uint_t hash_locked; 7400Sstevel@tonic-gate uint_t es; 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 7430Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[0]); 7440Sstevel@tonic-gate ASSERT(newpp ? PAGE_EXCL(newpp) : 1); 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate /* 7470Sstevel@tonic-gate * Acquire the appropriate page hash lock since 7480Sstevel@tonic-gate * we have to search the hash list. Pages that 7490Sstevel@tonic-gate * hash to this list can't change identity while 7500Sstevel@tonic-gate * this lock is held. 7510Sstevel@tonic-gate */ 7520Sstevel@tonic-gate hash_locked = 0; 7530Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 7540Sstevel@tonic-gate phm = NULL; 7550Sstevel@tonic-gate top: 7560Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 7570Sstevel@tonic-gate if (pp != NULL) { 7580Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[1]); 7590Sstevel@tonic-gate es = (newpp != NULL) ? 1 : 0; 7600Sstevel@tonic-gate es |= flags; 7610Sstevel@tonic-gate if (!hash_locked) { 7620Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[2]); 7630Sstevel@tonic-gate if (!page_try_reclaim_lock(pp, se, es)) { 7640Sstevel@tonic-gate /* 7650Sstevel@tonic-gate * On a miss, acquire the phm. Then 7660Sstevel@tonic-gate * next time, page_lock() will be called, 7670Sstevel@tonic-gate * causing a wait if the page is busy. 7680Sstevel@tonic-gate * just looping with page_trylock() would 7690Sstevel@tonic-gate * get pretty boring. 7700Sstevel@tonic-gate */ 7710Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[3]); 7720Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 7730Sstevel@tonic-gate mutex_enter(phm); 7740Sstevel@tonic-gate hash_locked = 1; 7750Sstevel@tonic-gate goto top; 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate } else { 7780Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[4]); 7790Sstevel@tonic-gate if (!page_lock_es(pp, se, phm, P_RECLAIM, es)) { 7800Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[5]); 7810Sstevel@tonic-gate goto top; 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate /* 7860Sstevel@tonic-gate * Since `pp' is locked it can not change identity now. 7870Sstevel@tonic-gate * Reconfirm we locked the correct page. 7880Sstevel@tonic-gate * 7890Sstevel@tonic-gate * Both the p_vnode and p_offset *must* be cast volatile 7900Sstevel@tonic-gate * to force a reload of their values: The PAGE_HASH_SEARCH 7910Sstevel@tonic-gate * macro will have stuffed p_vnode and p_offset into 7920Sstevel@tonic-gate * registers before calling page_trylock(); another thread, 7930Sstevel@tonic-gate * actually holding the hash lock, could have changed the 7940Sstevel@tonic-gate * page's identity in memory, but our registers would not 7950Sstevel@tonic-gate * be changed, fooling the reconfirmation. If the hash 7960Sstevel@tonic-gate * lock was held during the search, the casting would 7970Sstevel@tonic-gate * not be needed. 7980Sstevel@tonic-gate */ 7990Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[6]); 8000Sstevel@tonic-gate if (((volatile struct vnode *)(pp->p_vnode) != vp) || 8010Sstevel@tonic-gate ((volatile u_offset_t)(pp->p_offset) != off)) { 8020Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[7]); 8030Sstevel@tonic-gate if (hash_locked) { 8040Sstevel@tonic-gate panic("page_lookup_create: lost page %p", 8050Sstevel@tonic-gate (void *)pp); 8060Sstevel@tonic-gate /*NOTREACHED*/ 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate page_unlock(pp); 8090Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 8100Sstevel@tonic-gate mutex_enter(phm); 8110Sstevel@tonic-gate hash_locked = 1; 8120Sstevel@tonic-gate goto top; 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate /* 8160Sstevel@tonic-gate * If page_trylock() was called, then pp may still be on 8170Sstevel@tonic-gate * the cachelist (can't be on the free list, it would not 8180Sstevel@tonic-gate * have been found in the search). If it is on the 8190Sstevel@tonic-gate * cachelist it must be pulled now. To pull the page from 8200Sstevel@tonic-gate * the cachelist, it must be exclusively locked. 8210Sstevel@tonic-gate * 8220Sstevel@tonic-gate * The other big difference between page_trylock() and 8230Sstevel@tonic-gate * page_lock(), is that page_lock() will pull the 8240Sstevel@tonic-gate * page from whatever free list (the cache list in this 8250Sstevel@tonic-gate * case) the page is on. If page_trylock() was used 8260Sstevel@tonic-gate * above, then we have to do the reclaim ourselves. 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate if ((!hash_locked) && (PP_ISFREE(pp))) { 8290Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0); 8300Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[8]); 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate /* 8330Sstevel@tonic-gate * page_relcaim will insure that we 8340Sstevel@tonic-gate * have this page exclusively 8350Sstevel@tonic-gate */ 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate if (!page_reclaim(pp, NULL)) { 8380Sstevel@tonic-gate /* 8390Sstevel@tonic-gate * Page_reclaim dropped whatever lock 8400Sstevel@tonic-gate * we held. 8410Sstevel@tonic-gate */ 8420Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[9]); 8430Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 8440Sstevel@tonic-gate mutex_enter(phm); 8450Sstevel@tonic-gate hash_locked = 1; 8460Sstevel@tonic-gate goto top; 8470Sstevel@tonic-gate } else if (se == SE_SHARED && newpp == NULL) { 8480Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[10]); 8490Sstevel@tonic-gate page_downgrade(pp); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate if (hash_locked) { 8540Sstevel@tonic-gate mutex_exit(phm); 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate 8570Sstevel@tonic-gate if (newpp != NULL && pp->p_szc < newpp->p_szc && 8580Sstevel@tonic-gate PAGE_EXCL(pp) && nrelocp != NULL) { 8590Sstevel@tonic-gate ASSERT(nrelocp != NULL); 8600Sstevel@tonic-gate (void) page_relocate(&pp, &newpp, 1, 1, nrelocp, 8610Sstevel@tonic-gate NULL); 8620Sstevel@tonic-gate if (*nrelocp > 0) { 8630Sstevel@tonic-gate VM_STAT_COND_ADD(*nrelocp == 1, 8640Sstevel@tonic-gate page_lookup_cnt[11]); 8650Sstevel@tonic-gate VM_STAT_COND_ADD(*nrelocp > 1, 8660Sstevel@tonic-gate page_lookup_cnt[12]); 8670Sstevel@tonic-gate pp = newpp; 8680Sstevel@tonic-gate se = SE_EXCL; 8690Sstevel@tonic-gate } else { 8700Sstevel@tonic-gate if (se == SE_SHARED) { 8710Sstevel@tonic-gate page_downgrade(pp); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[13]); 8740Sstevel@tonic-gate } 8750Sstevel@tonic-gate } else if (newpp != NULL && nrelocp != NULL) { 8760Sstevel@tonic-gate if (PAGE_EXCL(pp) && se == SE_SHARED) { 8770Sstevel@tonic-gate page_downgrade(pp); 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate VM_STAT_COND_ADD(pp->p_szc < newpp->p_szc, 8800Sstevel@tonic-gate page_lookup_cnt[14]); 8810Sstevel@tonic-gate VM_STAT_COND_ADD(pp->p_szc == newpp->p_szc, 8820Sstevel@tonic-gate page_lookup_cnt[15]); 8830Sstevel@tonic-gate VM_STAT_COND_ADD(pp->p_szc > newpp->p_szc, 8840Sstevel@tonic-gate page_lookup_cnt[16]); 8850Sstevel@tonic-gate } else if (newpp != NULL && PAGE_EXCL(pp)) { 8860Sstevel@tonic-gate se = SE_EXCL; 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate } else if (!hash_locked) { 8890Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[17]); 8900Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 8910Sstevel@tonic-gate mutex_enter(phm); 8920Sstevel@tonic-gate hash_locked = 1; 8930Sstevel@tonic-gate goto top; 8940Sstevel@tonic-gate } else if (newpp != NULL) { 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * If we have a preallocated page then 8970Sstevel@tonic-gate * insert it now and basically behave like 8980Sstevel@tonic-gate * page_create. 8990Sstevel@tonic-gate */ 9000Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[18]); 9010Sstevel@tonic-gate /* 9020Sstevel@tonic-gate * Since we hold the page hash mutex and 9030Sstevel@tonic-gate * just searched for this page, page_hashin 9040Sstevel@tonic-gate * had better not fail. If it does, that 9050Sstevel@tonic-gate * means some thread did not follow the 9060Sstevel@tonic-gate * page hash mutex rules. Panic now and 9070Sstevel@tonic-gate * get it over with. As usual, go down 9080Sstevel@tonic-gate * holding all the locks. 9090Sstevel@tonic-gate */ 9100Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 9110Sstevel@tonic-gate if (!page_hashin(newpp, vp, off, phm)) { 9120Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 9130Sstevel@tonic-gate panic("page_lookup_create: hashin failed %p %p %llx %p", 9140Sstevel@tonic-gate (void *)newpp, (void *)vp, off, (void *)phm); 9150Sstevel@tonic-gate /*NOTREACHED*/ 9160Sstevel@tonic-gate } 9170Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 9180Sstevel@tonic-gate mutex_exit(phm); 9190Sstevel@tonic-gate phm = NULL; 9200Sstevel@tonic-gate page_set_props(newpp, P_REF); 9210Sstevel@tonic-gate page_io_lock(newpp); 9220Sstevel@tonic-gate pp = newpp; 9230Sstevel@tonic-gate se = SE_EXCL; 9240Sstevel@tonic-gate } else { 9250Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[19]); 9260Sstevel@tonic-gate mutex_exit(phm); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate ASSERT(pp ? PAGE_LOCKED_SE(pp, se) : 1); 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate ASSERT(pp ? ((PP_ISFREE(pp) == 0) && (PP_ISAGED(pp) == 0)) : 1); 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate return (pp); 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate /* 9370Sstevel@tonic-gate * Search the hash list for the page representing the 9380Sstevel@tonic-gate * specified [vp, offset] and return it locked. Skip 9390Sstevel@tonic-gate * free pages and pages that cannot be locked as requested. 9400Sstevel@tonic-gate * Used while attempting to kluster pages. 9410Sstevel@tonic-gate */ 9420Sstevel@tonic-gate page_t * 9430Sstevel@tonic-gate page_lookup_nowait(vnode_t *vp, u_offset_t off, se_t se) 9440Sstevel@tonic-gate { 9450Sstevel@tonic-gate page_t *pp; 9460Sstevel@tonic-gate kmutex_t *phm; 9470Sstevel@tonic-gate ulong_t index; 9480Sstevel@tonic-gate uint_t locked; 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 9510Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[0]); 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 9540Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 9550Sstevel@tonic-gate locked = 0; 9560Sstevel@tonic-gate if (pp == NULL) { 9570Sstevel@tonic-gate top: 9580Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[1]); 9590Sstevel@tonic-gate locked = 1; 9600Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 9610Sstevel@tonic-gate mutex_enter(phm); 9620Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate if (pp == NULL || PP_ISFREE(pp)) { 9660Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[2]); 9670Sstevel@tonic-gate pp = NULL; 9680Sstevel@tonic-gate } else { 9690Sstevel@tonic-gate if (!page_trylock(pp, se)) { 9700Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[3]); 9710Sstevel@tonic-gate pp = NULL; 9720Sstevel@tonic-gate } else { 9730Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[4]); 9740Sstevel@tonic-gate /* 9750Sstevel@tonic-gate * See the comment in page_lookup() 9760Sstevel@tonic-gate */ 9770Sstevel@tonic-gate if (((volatile struct vnode *)(pp->p_vnode) != vp) || 9780Sstevel@tonic-gate ((u_offset_t)(pp->p_offset) != off)) { 9790Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[5]); 9800Sstevel@tonic-gate if (locked) { 9810Sstevel@tonic-gate panic("page_lookup_nowait %p", 9820Sstevel@tonic-gate (void *)pp); 9830Sstevel@tonic-gate /*NOTREACHED*/ 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate page_unlock(pp); 9860Sstevel@tonic-gate goto top; 9870Sstevel@tonic-gate } 9880Sstevel@tonic-gate if (PP_ISFREE(pp)) { 9890Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[6]); 9900Sstevel@tonic-gate page_unlock(pp); 9910Sstevel@tonic-gate pp = NULL; 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate } 9950Sstevel@tonic-gate if (locked) { 9960Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[7]); 9970Sstevel@tonic-gate mutex_exit(phm); 9980Sstevel@tonic-gate } 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate ASSERT(pp ? PAGE_LOCKED_SE(pp, se) : 1); 10010Sstevel@tonic-gate 10020Sstevel@tonic-gate return (pp); 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate /* 10060Sstevel@tonic-gate * Search the hash list for a page with the specified [vp, off] 10070Sstevel@tonic-gate * that is known to exist and is already locked. This routine 10080Sstevel@tonic-gate * is typically used by segment SOFTUNLOCK routines. 10090Sstevel@tonic-gate */ 10100Sstevel@tonic-gate page_t * 10110Sstevel@tonic-gate page_find(vnode_t *vp, u_offset_t off) 10120Sstevel@tonic-gate { 10130Sstevel@tonic-gate page_t *pp; 10140Sstevel@tonic-gate kmutex_t *phm; 10150Sstevel@tonic-gate ulong_t index; 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 10180Sstevel@tonic-gate VM_STAT_ADD(page_find_cnt); 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 10210Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate mutex_enter(phm); 10240Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 10250Sstevel@tonic-gate mutex_exit(phm); 10260Sstevel@tonic-gate 10271338Selowe ASSERT(pp == NULL || PAGE_LOCKED(pp) || panicstr); 10280Sstevel@tonic-gate return (pp); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate /* 10320Sstevel@tonic-gate * Determine whether a page with the specified [vp, off] 10330Sstevel@tonic-gate * currently exists in the system. Obviously this should 10340Sstevel@tonic-gate * only be considered as a hint since nothing prevents the 10350Sstevel@tonic-gate * page from disappearing or appearing immediately after 10360Sstevel@tonic-gate * the return from this routine. Subsequently, we don't 10370Sstevel@tonic-gate * even bother to lock the list. 10380Sstevel@tonic-gate */ 10390Sstevel@tonic-gate page_t * 10400Sstevel@tonic-gate page_exists(vnode_t *vp, u_offset_t off) 10410Sstevel@tonic-gate { 10420Sstevel@tonic-gate page_t *pp; 10430Sstevel@tonic-gate ulong_t index; 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 10460Sstevel@tonic-gate VM_STAT_ADD(page_exists_cnt); 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 10490Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate return (pp); 10520Sstevel@tonic-gate } 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate /* 10550Sstevel@tonic-gate * Determine if physically contiguous pages exist for [vp, off] - [vp, off + 10560Sstevel@tonic-gate * page_size(szc)) range. if they exist and ppa is not NULL fill ppa array 10570Sstevel@tonic-gate * with these pages locked SHARED. If necessary reclaim pages from 10580Sstevel@tonic-gate * freelist. Return 1 if contiguous pages exist and 0 otherwise. 10590Sstevel@tonic-gate * 10600Sstevel@tonic-gate * If we fail to lock pages still return 1 if pages exist and contiguous. 10610Sstevel@tonic-gate * But in this case return value is just a hint. ppa array won't be filled. 10620Sstevel@tonic-gate * Caller should initialize ppa[0] as NULL to distinguish return value. 10630Sstevel@tonic-gate * 10640Sstevel@tonic-gate * Returns 0 if pages don't exist or not physically contiguous. 10650Sstevel@tonic-gate * 10660Sstevel@tonic-gate * This routine doesn't work for anonymous(swapfs) pages. 10670Sstevel@tonic-gate */ 10680Sstevel@tonic-gate int 10690Sstevel@tonic-gate page_exists_physcontig(vnode_t *vp, u_offset_t off, uint_t szc, page_t *ppa[]) 10700Sstevel@tonic-gate { 10710Sstevel@tonic-gate pgcnt_t pages; 10720Sstevel@tonic-gate pfn_t pfn; 10730Sstevel@tonic-gate page_t *rootpp; 10740Sstevel@tonic-gate pgcnt_t i; 10750Sstevel@tonic-gate pgcnt_t j; 10760Sstevel@tonic-gate u_offset_t save_off = off; 10770Sstevel@tonic-gate ulong_t index; 10780Sstevel@tonic-gate kmutex_t *phm; 10790Sstevel@tonic-gate page_t *pp; 10800Sstevel@tonic-gate uint_t pszc; 10810Sstevel@tonic-gate int loopcnt = 0; 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate ASSERT(szc != 0); 10840Sstevel@tonic-gate ASSERT(vp != NULL); 10850Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(vp)); 10863290Sjohansen ASSERT(!VN_ISKAS(vp)); 10870Sstevel@tonic-gate 10880Sstevel@tonic-gate again: 10890Sstevel@tonic-gate if (++loopcnt > 3) { 10900Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[0]); 10910Sstevel@tonic-gate return (0); 10920Sstevel@tonic-gate } 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 10950Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate mutex_enter(phm); 10980Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 10990Sstevel@tonic-gate mutex_exit(phm); 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[1]); 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate if (pp == NULL) { 11040Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[2]); 11050Sstevel@tonic-gate return (0); 11060Sstevel@tonic-gate } 11070Sstevel@tonic-gate 11080Sstevel@tonic-gate pages = page_get_pagecnt(szc); 11090Sstevel@tonic-gate rootpp = pp; 11100Sstevel@tonic-gate pfn = rootpp->p_pagenum; 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate if ((pszc = pp->p_szc) >= szc && ppa != NULL) { 11130Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[3]); 11140Sstevel@tonic-gate if (!page_trylock(pp, SE_SHARED)) { 11150Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[4]); 11160Sstevel@tonic-gate return (1); 11170Sstevel@tonic-gate } 11180Sstevel@tonic-gate if (pp->p_szc != pszc || pp->p_vnode != vp || 11190Sstevel@tonic-gate pp->p_offset != off) { 11200Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[5]); 11210Sstevel@tonic-gate page_unlock(pp); 11220Sstevel@tonic-gate off = save_off; 11230Sstevel@tonic-gate goto again; 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate /* 11260Sstevel@tonic-gate * szc was non zero and vnode and offset matched after we 11270Sstevel@tonic-gate * locked the page it means it can't become free on us. 11280Sstevel@tonic-gate */ 11290Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 11300Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, pages)) { 11310Sstevel@tonic-gate page_unlock(pp); 11320Sstevel@tonic-gate return (0); 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate ppa[0] = pp; 11350Sstevel@tonic-gate pp++; 11360Sstevel@tonic-gate off += PAGESIZE; 11370Sstevel@tonic-gate pfn++; 11380Sstevel@tonic-gate for (i = 1; i < pages; i++, pp++, off += PAGESIZE, pfn++) { 11390Sstevel@tonic-gate if (!page_trylock(pp, SE_SHARED)) { 11400Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[6]); 11410Sstevel@tonic-gate pp--; 11420Sstevel@tonic-gate while (i-- > 0) { 11430Sstevel@tonic-gate page_unlock(pp); 11440Sstevel@tonic-gate pp--; 11450Sstevel@tonic-gate } 11460Sstevel@tonic-gate ppa[0] = NULL; 11470Sstevel@tonic-gate return (1); 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate if (pp->p_szc != pszc) { 11500Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[7]); 11510Sstevel@tonic-gate page_unlock(pp); 11520Sstevel@tonic-gate pp--; 11530Sstevel@tonic-gate while (i-- > 0) { 11540Sstevel@tonic-gate page_unlock(pp); 11550Sstevel@tonic-gate pp--; 11560Sstevel@tonic-gate } 11570Sstevel@tonic-gate ppa[0] = NULL; 11580Sstevel@tonic-gate off = save_off; 11590Sstevel@tonic-gate goto again; 11600Sstevel@tonic-gate } 11610Sstevel@tonic-gate /* 11620Sstevel@tonic-gate * szc the same as for previous already locked pages 11630Sstevel@tonic-gate * with right identity. Since this page had correct 11640Sstevel@tonic-gate * szc after we locked it can't get freed or destroyed 11650Sstevel@tonic-gate * and therefore must have the expected identity. 11660Sstevel@tonic-gate */ 11670Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 11680Sstevel@tonic-gate if (pp->p_vnode != vp || 11690Sstevel@tonic-gate pp->p_offset != off) { 11700Sstevel@tonic-gate panic("page_exists_physcontig: " 11710Sstevel@tonic-gate "large page identity doesn't match"); 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate ppa[i] = pp; 11740Sstevel@tonic-gate ASSERT(pp->p_pagenum == pfn); 11750Sstevel@tonic-gate } 11760Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[8]); 11770Sstevel@tonic-gate ppa[pages] = NULL; 11780Sstevel@tonic-gate return (1); 11790Sstevel@tonic-gate } else if (pszc >= szc) { 11800Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[9]); 11810Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, pages)) { 11820Sstevel@tonic-gate return (0); 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate return (1); 11850Sstevel@tonic-gate } 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, pages)) { 11880Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[10]); 11890Sstevel@tonic-gate return (0); 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate if (page_numtomemseg_nolock(pfn) != 11930Sstevel@tonic-gate page_numtomemseg_nolock(pfn + pages - 1)) { 11940Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[11]); 11950Sstevel@tonic-gate return (0); 11960Sstevel@tonic-gate } 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate /* 11990Sstevel@tonic-gate * We loop up 4 times across pages to promote page size. 12000Sstevel@tonic-gate * We're extra cautious to promote page size atomically with respect 12010Sstevel@tonic-gate * to everybody else. But we can probably optimize into 1 loop if 12020Sstevel@tonic-gate * this becomes an issue. 12030Sstevel@tonic-gate */ 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++, off += PAGESIZE, pfn++) { 12060Sstevel@tonic-gate ASSERT(pp->p_pagenum == pfn); 12070Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) { 12080Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[12]); 12090Sstevel@tonic-gate break; 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate if (pp->p_vnode != vp || 12120Sstevel@tonic-gate pp->p_offset != off) { 12130Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[13]); 12140Sstevel@tonic-gate page_unlock(pp); 12150Sstevel@tonic-gate break; 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate if (pp->p_szc >= szc) { 12180Sstevel@tonic-gate ASSERT(i == 0); 12190Sstevel@tonic-gate page_unlock(pp); 12200Sstevel@tonic-gate off = save_off; 12210Sstevel@tonic-gate goto again; 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate } 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate if (i != pages) { 12260Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[14]); 12270Sstevel@tonic-gate --pp; 12280Sstevel@tonic-gate while (i-- > 0) { 12290Sstevel@tonic-gate page_unlock(pp); 12300Sstevel@tonic-gate --pp; 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate return (0); 12330Sstevel@tonic-gate } 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate pp = rootpp; 12360Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++) { 12370Sstevel@tonic-gate if (PP_ISFREE(pp)) { 12380Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[15]); 12390Sstevel@tonic-gate ASSERT(!PP_ISAGED(pp)); 12400Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 12410Sstevel@tonic-gate if (!page_reclaim(pp, NULL)) { 12420Sstevel@tonic-gate break; 12430Sstevel@tonic-gate } 12440Sstevel@tonic-gate } else { 12450Sstevel@tonic-gate ASSERT(pp->p_szc < szc); 12460Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[16]); 12470Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate if (i < pages) { 12510Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[17]); 12520Sstevel@tonic-gate /* 12530Sstevel@tonic-gate * page_reclaim failed because we were out of memory. 12540Sstevel@tonic-gate * drop the rest of the locks and return because this page 12550Sstevel@tonic-gate * must be already reallocated anyway. 12560Sstevel@tonic-gate */ 12570Sstevel@tonic-gate pp = rootpp; 12580Sstevel@tonic-gate for (j = 0; j < pages; j++, pp++) { 12590Sstevel@tonic-gate if (j != i) { 12600Sstevel@tonic-gate page_unlock(pp); 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate } 12630Sstevel@tonic-gate return (0); 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate off = save_off; 12670Sstevel@tonic-gate pp = rootpp; 12680Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++, off += PAGESIZE) { 12690Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 12700Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 12710Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 12720Sstevel@tonic-gate ASSERT(pp->p_vnode == vp); 12730Sstevel@tonic-gate ASSERT(pp->p_offset == off); 12740Sstevel@tonic-gate pp->p_szc = szc; 12750Sstevel@tonic-gate } 12760Sstevel@tonic-gate pp = rootpp; 12770Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++) { 12780Sstevel@tonic-gate if (ppa == NULL) { 12790Sstevel@tonic-gate page_unlock(pp); 12800Sstevel@tonic-gate } else { 12810Sstevel@tonic-gate ppa[i] = pp; 12820Sstevel@tonic-gate page_downgrade(ppa[i]); 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate if (ppa != NULL) { 12860Sstevel@tonic-gate ppa[pages] = NULL; 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[18]); 12890Sstevel@tonic-gate ASSERT(vp->v_pages != NULL); 12900Sstevel@tonic-gate return (1); 12910Sstevel@tonic-gate } 12920Sstevel@tonic-gate 12930Sstevel@tonic-gate /* 12940Sstevel@tonic-gate * Determine whether a page with the specified [vp, off] 12950Sstevel@tonic-gate * currently exists in the system and if so return its 12960Sstevel@tonic-gate * size code. Obviously this should only be considered as 12970Sstevel@tonic-gate * a hint since nothing prevents the page from disappearing 12980Sstevel@tonic-gate * or appearing immediately after the return from this routine. 12990Sstevel@tonic-gate */ 13000Sstevel@tonic-gate int 13010Sstevel@tonic-gate page_exists_forreal(vnode_t *vp, u_offset_t off, uint_t *szc) 13020Sstevel@tonic-gate { 13030Sstevel@tonic-gate page_t *pp; 13040Sstevel@tonic-gate kmutex_t *phm; 13050Sstevel@tonic-gate ulong_t index; 13060Sstevel@tonic-gate int rc = 0; 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 13090Sstevel@tonic-gate ASSERT(szc != NULL); 13100Sstevel@tonic-gate VM_STAT_ADD(page_exists_forreal_cnt); 13110Sstevel@tonic-gate 13120Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 13130Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 13140Sstevel@tonic-gate 13150Sstevel@tonic-gate mutex_enter(phm); 13160Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 13170Sstevel@tonic-gate if (pp != NULL) { 13180Sstevel@tonic-gate *szc = pp->p_szc; 13190Sstevel@tonic-gate rc = 1; 13200Sstevel@tonic-gate } 13210Sstevel@tonic-gate mutex_exit(phm); 13220Sstevel@tonic-gate return (rc); 13230Sstevel@tonic-gate } 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate /* wakeup threads waiting for pages in page_create_get_something() */ 13260Sstevel@tonic-gate void 13270Sstevel@tonic-gate wakeup_pcgs(void) 13280Sstevel@tonic-gate { 13290Sstevel@tonic-gate if (!CV_HAS_WAITERS(&pcgs_cv)) 13300Sstevel@tonic-gate return; 13310Sstevel@tonic-gate cv_broadcast(&pcgs_cv); 13320Sstevel@tonic-gate } 13330Sstevel@tonic-gate 13340Sstevel@tonic-gate /* 13350Sstevel@tonic-gate * 'freemem' is used all over the kernel as an indication of how many 13360Sstevel@tonic-gate * pages are free (either on the cache list or on the free page list) 13370Sstevel@tonic-gate * in the system. In very few places is a really accurate 'freemem' 13380Sstevel@tonic-gate * needed. To avoid contention of the lock protecting a the 13390Sstevel@tonic-gate * single freemem, it was spread out into NCPU buckets. Set_freemem 13400Sstevel@tonic-gate * sets freemem to the total of all NCPU buckets. It is called from 13410Sstevel@tonic-gate * clock() on each TICK. 13420Sstevel@tonic-gate */ 13430Sstevel@tonic-gate void 13440Sstevel@tonic-gate set_freemem() 13450Sstevel@tonic-gate { 13460Sstevel@tonic-gate struct pcf *p; 13470Sstevel@tonic-gate ulong_t t; 13480Sstevel@tonic-gate uint_t i; 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate t = 0; 13510Sstevel@tonic-gate p = pcf; 13526880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 13530Sstevel@tonic-gate t += p->pcf_count; 13540Sstevel@tonic-gate p++; 13550Sstevel@tonic-gate } 13560Sstevel@tonic-gate freemem = t; 13570Sstevel@tonic-gate 13580Sstevel@tonic-gate /* 13590Sstevel@tonic-gate * Don't worry about grabbing mutex. It's not that 13600Sstevel@tonic-gate * critical if we miss a tick or two. This is 13610Sstevel@tonic-gate * where we wakeup possible delayers in 13620Sstevel@tonic-gate * page_create_get_something(). 13630Sstevel@tonic-gate */ 13640Sstevel@tonic-gate wakeup_pcgs(); 13650Sstevel@tonic-gate } 13660Sstevel@tonic-gate 13670Sstevel@tonic-gate ulong_t 13680Sstevel@tonic-gate get_freemem() 13690Sstevel@tonic-gate { 13700Sstevel@tonic-gate struct pcf *p; 13710Sstevel@tonic-gate ulong_t t; 13720Sstevel@tonic-gate uint_t i; 13730Sstevel@tonic-gate 13740Sstevel@tonic-gate t = 0; 13750Sstevel@tonic-gate p = pcf; 13766880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 13770Sstevel@tonic-gate t += p->pcf_count; 13780Sstevel@tonic-gate p++; 13790Sstevel@tonic-gate } 13800Sstevel@tonic-gate /* 13810Sstevel@tonic-gate * We just calculated it, might as well set it. 13820Sstevel@tonic-gate */ 13830Sstevel@tonic-gate freemem = t; 13840Sstevel@tonic-gate return (t); 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate /* 13880Sstevel@tonic-gate * Acquire all of the page cache & free (pcf) locks. 13890Sstevel@tonic-gate */ 13900Sstevel@tonic-gate void 13910Sstevel@tonic-gate pcf_acquire_all() 13920Sstevel@tonic-gate { 13930Sstevel@tonic-gate struct pcf *p; 13940Sstevel@tonic-gate uint_t i; 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate p = pcf; 13976880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 13980Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 13990Sstevel@tonic-gate p++; 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate } 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate /* 14040Sstevel@tonic-gate * Release all the pcf_locks. 14050Sstevel@tonic-gate */ 14060Sstevel@tonic-gate void 14070Sstevel@tonic-gate pcf_release_all() 14080Sstevel@tonic-gate { 14090Sstevel@tonic-gate struct pcf *p; 14100Sstevel@tonic-gate uint_t i; 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate p = pcf; 14136880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 14140Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 14150Sstevel@tonic-gate p++; 14160Sstevel@tonic-gate } 14170Sstevel@tonic-gate } 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate /* 14200Sstevel@tonic-gate * Inform the VM system that we need some pages freed up. 14210Sstevel@tonic-gate * Calls must be symmetric, e.g.: 14220Sstevel@tonic-gate * 14230Sstevel@tonic-gate * page_needfree(100); 14240Sstevel@tonic-gate * wait a bit; 14250Sstevel@tonic-gate * page_needfree(-100); 14260Sstevel@tonic-gate */ 14270Sstevel@tonic-gate void 14280Sstevel@tonic-gate page_needfree(spgcnt_t npages) 14290Sstevel@tonic-gate { 14300Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 14310Sstevel@tonic-gate needfree += npages; 14320Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate /* 14360Sstevel@tonic-gate * Throttle for page_create(): try to prevent freemem from dropping 14370Sstevel@tonic-gate * below throttlefree. We can't provide a 100% guarantee because 14380Sstevel@tonic-gate * KM_NOSLEEP allocations, page_reclaim(), and various other things 14390Sstevel@tonic-gate * nibble away at the freelist. However, we can block all PG_WAIT 14400Sstevel@tonic-gate * allocations until memory becomes available. The motivation is 14410Sstevel@tonic-gate * that several things can fall apart when there's no free memory: 14420Sstevel@tonic-gate * 14430Sstevel@tonic-gate * (1) If pageout() needs memory to push a page, the system deadlocks. 14440Sstevel@tonic-gate * 14450Sstevel@tonic-gate * (2) By (broken) specification, timeout(9F) can neither fail nor 14460Sstevel@tonic-gate * block, so it has no choice but to panic the system if it 14470Sstevel@tonic-gate * cannot allocate a callout structure. 14480Sstevel@tonic-gate * 14490Sstevel@tonic-gate * (3) Like timeout(), ddi_set_callback() cannot fail and cannot block; 14500Sstevel@tonic-gate * it panics if it cannot allocate a callback structure. 14510Sstevel@tonic-gate * 14520Sstevel@tonic-gate * (4) Untold numbers of third-party drivers have not yet been hardened 14530Sstevel@tonic-gate * against KM_NOSLEEP and/or allocb() failures; they simply assume 14540Sstevel@tonic-gate * success and panic the system with a data fault on failure. 14550Sstevel@tonic-gate * (The long-term solution to this particular problem is to ship 14560Sstevel@tonic-gate * hostile fault-injecting DEBUG kernels with the DDK.) 14570Sstevel@tonic-gate * 14580Sstevel@tonic-gate * It is theoretically impossible to guarantee success of non-blocking 14590Sstevel@tonic-gate * allocations, but in practice, this throttle is very hard to break. 14600Sstevel@tonic-gate */ 14610Sstevel@tonic-gate static int 14620Sstevel@tonic-gate page_create_throttle(pgcnt_t npages, int flags) 14630Sstevel@tonic-gate { 14640Sstevel@tonic-gate ulong_t fm; 14650Sstevel@tonic-gate uint_t i; 14660Sstevel@tonic-gate pgcnt_t tf; /* effective value of throttlefree */ 14670Sstevel@tonic-gate 14680Sstevel@tonic-gate /* 14690Sstevel@tonic-gate * Never deny pages when: 14700Sstevel@tonic-gate * - it's a thread that cannot block [NOMEMWAIT()] 14710Sstevel@tonic-gate * - the allocation cannot block and must not fail 14720Sstevel@tonic-gate * - the allocation cannot block and is pageout dispensated 14730Sstevel@tonic-gate */ 14740Sstevel@tonic-gate if (NOMEMWAIT() || 14750Sstevel@tonic-gate ((flags & (PG_WAIT | PG_PANIC)) == PG_PANIC) || 14760Sstevel@tonic-gate ((flags & (PG_WAIT | PG_PUSHPAGE)) == PG_PUSHPAGE)) 14770Sstevel@tonic-gate return (1); 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate /* 14800Sstevel@tonic-gate * If the allocation can't block, we look favorably upon it 14810Sstevel@tonic-gate * unless we're below pageout_reserve. In that case we fail 14820Sstevel@tonic-gate * the allocation because we want to make sure there are a few 14830Sstevel@tonic-gate * pages available for pageout. 14840Sstevel@tonic-gate */ 14850Sstevel@tonic-gate if ((flags & PG_WAIT) == 0) 14860Sstevel@tonic-gate return (freemem >= npages + pageout_reserve); 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate /* Calculate the effective throttlefree value */ 14890Sstevel@tonic-gate tf = throttlefree - 14900Sstevel@tonic-gate ((flags & PG_PUSHPAGE) ? pageout_reserve : 0); 14910Sstevel@tonic-gate 14920Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 14930Sstevel@tonic-gate 14945466Skchow for (;;) { 14955466Skchow fm = 0; 14960Sstevel@tonic-gate pcf_acquire_all(); 14970Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 14986880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 14990Sstevel@tonic-gate fm += pcf[i].pcf_count; 15000Sstevel@tonic-gate pcf[i].pcf_wait++; 15010Sstevel@tonic-gate mutex_exit(&pcf[i].pcf_lock); 15020Sstevel@tonic-gate } 15030Sstevel@tonic-gate freemem = fm; 15045466Skchow if (freemem >= npages + tf) { 15055466Skchow mutex_exit(&new_freemem_lock); 15065466Skchow break; 15075466Skchow } 15080Sstevel@tonic-gate needfree += npages; 15090Sstevel@tonic-gate freemem_wait++; 15100Sstevel@tonic-gate cv_wait(&freemem_cv, &new_freemem_lock); 15110Sstevel@tonic-gate freemem_wait--; 15120Sstevel@tonic-gate needfree -= npages; 15130Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate return (1); 15160Sstevel@tonic-gate } 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate /* 15195331Samw * page_create_wait() is called to either coalesce pages from the 15200Sstevel@tonic-gate * different pcf buckets or to wait because there simply are not 15210Sstevel@tonic-gate * enough pages to satisfy the caller's request. 15220Sstevel@tonic-gate * 15230Sstevel@tonic-gate * Sadly, this is called from platform/vm/vm_machdep.c 15240Sstevel@tonic-gate */ 15250Sstevel@tonic-gate int 15266880Sdv142724 page_create_wait(pgcnt_t npages, uint_t flags) 15270Sstevel@tonic-gate { 15280Sstevel@tonic-gate pgcnt_t total; 15290Sstevel@tonic-gate uint_t i; 15300Sstevel@tonic-gate struct pcf *p; 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate /* 15330Sstevel@tonic-gate * Wait until there are enough free pages to satisfy our 15340Sstevel@tonic-gate * entire request. 15350Sstevel@tonic-gate * We set needfree += npages before prodding pageout, to make sure 15360Sstevel@tonic-gate * it does real work when npages > lotsfree > freemem. 15370Sstevel@tonic-gate */ 15380Sstevel@tonic-gate VM_STAT_ADD(page_create_not_enough); 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate ASSERT(!kcage_on ? !(flags & PG_NORELOC) : 1); 15410Sstevel@tonic-gate checkagain: 15420Sstevel@tonic-gate if ((flags & PG_NORELOC) && 15430Sstevel@tonic-gate kcage_freemem < kcage_throttlefree + npages) 15440Sstevel@tonic-gate (void) kcage_create_throttle(npages, flags); 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate if (freemem < npages + throttlefree) 15470Sstevel@tonic-gate if (!page_create_throttle(npages, flags)) 15480Sstevel@tonic-gate return (0); 15490Sstevel@tonic-gate 15506880Sdv142724 if (pcf_decrement_bucket(npages) || 15516880Sdv142724 pcf_decrement_multiple(&total, npages, 0)) 15526880Sdv142724 return (1); 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate /* 15550Sstevel@tonic-gate * All of the pcf locks are held, there are not enough pages 15560Sstevel@tonic-gate * to satisfy the request (npages < total). 15570Sstevel@tonic-gate * Be sure to acquire the new_freemem_lock before dropping 15580Sstevel@tonic-gate * the pcf locks. This prevents dropping wakeups in page_free(). 15590Sstevel@tonic-gate * The order is always pcf_lock then new_freemem_lock. 15600Sstevel@tonic-gate * 15610Sstevel@tonic-gate * Since we hold all the pcf locks, it is a good time to set freemem. 15620Sstevel@tonic-gate * 15630Sstevel@tonic-gate * If the caller does not want to wait, return now. 15640Sstevel@tonic-gate * Else turn the pageout daemon loose to find something 15650Sstevel@tonic-gate * and wait till it does. 15660Sstevel@tonic-gate * 15670Sstevel@tonic-gate */ 15680Sstevel@tonic-gate freemem = total; 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate if ((flags & PG_WAIT) == 0) { 15710Sstevel@tonic-gate pcf_release_all(); 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_NOMEM, 15740Sstevel@tonic-gate "page_create_nomem:npages %ld freemem %ld", npages, freemem); 15750Sstevel@tonic-gate return (0); 15760Sstevel@tonic-gate } 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate ASSERT(proc_pageout != NULL); 15790Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SLEEP_START, 15820Sstevel@tonic-gate "page_create_sleep_start: freemem %ld needfree %ld", 15830Sstevel@tonic-gate freemem, needfree); 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate /* 15860Sstevel@tonic-gate * We are going to wait. 15870Sstevel@tonic-gate * We currently hold all of the pcf_locks, 15880Sstevel@tonic-gate * get the new_freemem_lock (it protects freemem_wait), 15890Sstevel@tonic-gate * before dropping the pcf_locks. 15900Sstevel@tonic-gate */ 15910Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 15920Sstevel@tonic-gate 15930Sstevel@tonic-gate p = pcf; 15946880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 15950Sstevel@tonic-gate p->pcf_wait++; 15960Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 15970Sstevel@tonic-gate p++; 15980Sstevel@tonic-gate } 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate needfree += npages; 16010Sstevel@tonic-gate freemem_wait++; 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate cv_wait(&freemem_cv, &new_freemem_lock); 16040Sstevel@tonic-gate 16050Sstevel@tonic-gate freemem_wait--; 16060Sstevel@tonic-gate needfree -= npages; 16070Sstevel@tonic-gate 16080Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SLEEP_END, 16110Sstevel@tonic-gate "page_create_sleep_end: freemem %ld needfree %ld", 16120Sstevel@tonic-gate freemem, needfree); 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate VM_STAT_ADD(page_create_not_enough_again); 16150Sstevel@tonic-gate goto checkagain; 16160Sstevel@tonic-gate } 16170Sstevel@tonic-gate /* 16180Sstevel@tonic-gate * A routine to do the opposite of page_create_wait(). 16190Sstevel@tonic-gate */ 16200Sstevel@tonic-gate void 16210Sstevel@tonic-gate page_create_putback(spgcnt_t npages) 16220Sstevel@tonic-gate { 16230Sstevel@tonic-gate struct pcf *p; 16240Sstevel@tonic-gate pgcnt_t lump; 16250Sstevel@tonic-gate uint_t *which; 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate /* 16280Sstevel@tonic-gate * When a contiguous lump is broken up, we have to 16290Sstevel@tonic-gate * deal with lots of pages (min 64) so lets spread 16300Sstevel@tonic-gate * the wealth around. 16310Sstevel@tonic-gate */ 16326880Sdv142724 lump = roundup(npages, pcf_fanout) / pcf_fanout; 16330Sstevel@tonic-gate freemem += npages; 16340Sstevel@tonic-gate 16356880Sdv142724 for (p = pcf; (npages > 0) && (p < &pcf[pcf_fanout]); p++) { 16360Sstevel@tonic-gate which = &p->pcf_count; 16370Sstevel@tonic-gate 16380Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate if (p->pcf_block) { 16410Sstevel@tonic-gate which = &p->pcf_reserve; 16420Sstevel@tonic-gate } 16430Sstevel@tonic-gate 16440Sstevel@tonic-gate if (lump < npages) { 16450Sstevel@tonic-gate *which += (uint_t)lump; 16460Sstevel@tonic-gate npages -= lump; 16470Sstevel@tonic-gate } else { 16480Sstevel@tonic-gate *which += (uint_t)npages; 16490Sstevel@tonic-gate npages = 0; 16500Sstevel@tonic-gate } 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate if (p->pcf_wait) { 16530Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 16540Sstevel@tonic-gate /* 16550Sstevel@tonic-gate * Check to see if some other thread 16560Sstevel@tonic-gate * is actually waiting. Another bucket 16570Sstevel@tonic-gate * may have woken it up by now. If there 16580Sstevel@tonic-gate * are no waiters, then set our pcf_wait 16590Sstevel@tonic-gate * count to zero to avoid coming in here 16600Sstevel@tonic-gate * next time. 16610Sstevel@tonic-gate */ 16620Sstevel@tonic-gate if (freemem_wait) { 16630Sstevel@tonic-gate if (npages > 1) { 16640Sstevel@tonic-gate cv_broadcast(&freemem_cv); 16650Sstevel@tonic-gate } else { 16660Sstevel@tonic-gate cv_signal(&freemem_cv); 16670Sstevel@tonic-gate } 16680Sstevel@tonic-gate p->pcf_wait--; 16690Sstevel@tonic-gate } else { 16700Sstevel@tonic-gate p->pcf_wait = 0; 16710Sstevel@tonic-gate } 16720Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 16750Sstevel@tonic-gate } 16760Sstevel@tonic-gate ASSERT(npages == 0); 16770Sstevel@tonic-gate } 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate /* 16800Sstevel@tonic-gate * A helper routine for page_create_get_something. 16810Sstevel@tonic-gate * The indenting got to deep down there. 16820Sstevel@tonic-gate * Unblock the pcf counters. Any pages freed after 16830Sstevel@tonic-gate * pcf_block got set are moved to pcf_count and 16840Sstevel@tonic-gate * wakeups (cv_broadcast() or cv_signal()) are done as needed. 16850Sstevel@tonic-gate */ 16860Sstevel@tonic-gate static void 16870Sstevel@tonic-gate pcgs_unblock(void) 16880Sstevel@tonic-gate { 16890Sstevel@tonic-gate int i; 16900Sstevel@tonic-gate struct pcf *p; 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate /* Update freemem while we're here. */ 16930Sstevel@tonic-gate freemem = 0; 16940Sstevel@tonic-gate p = pcf; 16956880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 16960Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 16970Sstevel@tonic-gate ASSERT(p->pcf_count == 0); 16980Sstevel@tonic-gate p->pcf_count = p->pcf_reserve; 16990Sstevel@tonic-gate p->pcf_block = 0; 17000Sstevel@tonic-gate freemem += p->pcf_count; 17010Sstevel@tonic-gate if (p->pcf_wait) { 17020Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 17030Sstevel@tonic-gate if (freemem_wait) { 17040Sstevel@tonic-gate if (p->pcf_reserve > 1) { 17050Sstevel@tonic-gate cv_broadcast(&freemem_cv); 17060Sstevel@tonic-gate p->pcf_wait = 0; 17070Sstevel@tonic-gate } else { 17080Sstevel@tonic-gate cv_signal(&freemem_cv); 17090Sstevel@tonic-gate p->pcf_wait--; 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate } else { 17120Sstevel@tonic-gate p->pcf_wait = 0; 17130Sstevel@tonic-gate } 17140Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 17150Sstevel@tonic-gate } 17160Sstevel@tonic-gate p->pcf_reserve = 0; 17170Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 17180Sstevel@tonic-gate p++; 17190Sstevel@tonic-gate } 17200Sstevel@tonic-gate } 17210Sstevel@tonic-gate 17220Sstevel@tonic-gate /* 17230Sstevel@tonic-gate * Called from page_create_va() when both the cache and free lists 17240Sstevel@tonic-gate * have been checked once. 17250Sstevel@tonic-gate * 17260Sstevel@tonic-gate * Either returns a page or panics since the accounting was done 17270Sstevel@tonic-gate * way before we got here. 17280Sstevel@tonic-gate * 17290Sstevel@tonic-gate * We don't come here often, so leave the accounting on permanently. 17300Sstevel@tonic-gate */ 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate #define MAX_PCGS 100 17330Sstevel@tonic-gate 17340Sstevel@tonic-gate #ifdef DEBUG 17350Sstevel@tonic-gate #define PCGS_TRIES 100 17360Sstevel@tonic-gate #else /* DEBUG */ 17370Sstevel@tonic-gate #define PCGS_TRIES 10 17380Sstevel@tonic-gate #endif /* DEBUG */ 17390Sstevel@tonic-gate 17400Sstevel@tonic-gate #ifdef VM_STATS 17410Sstevel@tonic-gate uint_t pcgs_counts[PCGS_TRIES]; 17420Sstevel@tonic-gate uint_t pcgs_too_many; 17430Sstevel@tonic-gate uint_t pcgs_entered; 17440Sstevel@tonic-gate uint_t pcgs_entered_noreloc; 17450Sstevel@tonic-gate uint_t pcgs_locked; 17460Sstevel@tonic-gate uint_t pcgs_cagelocked; 17470Sstevel@tonic-gate #endif /* VM_STATS */ 17480Sstevel@tonic-gate 17490Sstevel@tonic-gate static page_t * 17500Sstevel@tonic-gate page_create_get_something(vnode_t *vp, u_offset_t off, struct seg *seg, 17510Sstevel@tonic-gate caddr_t vaddr, uint_t flags) 17520Sstevel@tonic-gate { 17530Sstevel@tonic-gate uint_t count; 17540Sstevel@tonic-gate page_t *pp; 17550Sstevel@tonic-gate uint_t locked, i; 17560Sstevel@tonic-gate struct pcf *p; 17570Sstevel@tonic-gate lgrp_t *lgrp; 17580Sstevel@tonic-gate int cagelocked = 0; 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate VM_STAT_ADD(pcgs_entered); 17610Sstevel@tonic-gate 17620Sstevel@tonic-gate /* 17630Sstevel@tonic-gate * Tap any reserve freelists: if we fail now, we'll die 17640Sstevel@tonic-gate * since the page(s) we're looking for have already been 17650Sstevel@tonic-gate * accounted for. 17660Sstevel@tonic-gate */ 17670Sstevel@tonic-gate flags |= PG_PANIC; 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate if ((flags & PG_NORELOC) != 0) { 17700Sstevel@tonic-gate VM_STAT_ADD(pcgs_entered_noreloc); 17710Sstevel@tonic-gate /* 17720Sstevel@tonic-gate * Requests for free pages from critical threads 17730Sstevel@tonic-gate * such as pageout still won't throttle here, but 17740Sstevel@tonic-gate * we must try again, to give the cageout thread 17750Sstevel@tonic-gate * another chance to catch up. Since we already 17760Sstevel@tonic-gate * accounted for the pages, we had better get them 17770Sstevel@tonic-gate * this time. 17780Sstevel@tonic-gate * 17790Sstevel@tonic-gate * N.B. All non-critical threads acquire the pcgs_cagelock 17800Sstevel@tonic-gate * to serialize access to the freelists. This implements a 17810Sstevel@tonic-gate * turnstile-type synchornization to avoid starvation of 17820Sstevel@tonic-gate * critical requests for PG_NORELOC memory by non-critical 17830Sstevel@tonic-gate * threads: all non-critical threads must acquire a 'ticket' 17840Sstevel@tonic-gate * before passing through, which entails making sure 17850Sstevel@tonic-gate * kcage_freemem won't fall below minfree prior to grabbing 17860Sstevel@tonic-gate * pages from the freelists. 17870Sstevel@tonic-gate */ 17880Sstevel@tonic-gate if (kcage_create_throttle(1, flags) == KCT_NONCRIT) { 17890Sstevel@tonic-gate mutex_enter(&pcgs_cagelock); 17900Sstevel@tonic-gate cagelocked = 1; 17910Sstevel@tonic-gate VM_STAT_ADD(pcgs_cagelocked); 17920Sstevel@tonic-gate } 17930Sstevel@tonic-gate } 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate /* 17960Sstevel@tonic-gate * Time to get serious. 17970Sstevel@tonic-gate * We failed to get a `correctly colored' page from both the 17980Sstevel@tonic-gate * free and cache lists. 17990Sstevel@tonic-gate * We escalate in stage. 18000Sstevel@tonic-gate * 18010Sstevel@tonic-gate * First try both lists without worring about color. 18020Sstevel@tonic-gate * 18030Sstevel@tonic-gate * Then, grab all page accounting locks (ie. pcf[]) and 18040Sstevel@tonic-gate * steal any pages that they have and set the pcf_block flag to 18050Sstevel@tonic-gate * stop deletions from the lists. This will help because 18060Sstevel@tonic-gate * a page can get added to the free list while we are looking 18070Sstevel@tonic-gate * at the cache list, then another page could be added to the cache 18080Sstevel@tonic-gate * list allowing the page on the free list to be removed as we 18090Sstevel@tonic-gate * move from looking at the cache list to the free list. This 18100Sstevel@tonic-gate * could happen over and over. We would never find the page 18110Sstevel@tonic-gate * we have accounted for. 18120Sstevel@tonic-gate * 18130Sstevel@tonic-gate * Noreloc pages are a subset of the global (relocatable) page pool. 18140Sstevel@tonic-gate * They are not tracked separately in the pcf bins, so it is 18150Sstevel@tonic-gate * impossible to know when doing pcf accounting if the available 18160Sstevel@tonic-gate * page(s) are noreloc pages or not. When looking for a noreloc page 18170Sstevel@tonic-gate * it is quite easy to end up here even if the global (relocatable) 18180Sstevel@tonic-gate * page pool has plenty of free pages but the noreloc pool is empty. 18190Sstevel@tonic-gate * 18200Sstevel@tonic-gate * When the noreloc pool is empty (or low), additional noreloc pages 18210Sstevel@tonic-gate * are created by converting pages from the global page pool. This 18220Sstevel@tonic-gate * process will stall during pcf accounting if the pcf bins are 18230Sstevel@tonic-gate * already locked. Such is the case when a noreloc allocation is 18240Sstevel@tonic-gate * looping here in page_create_get_something waiting for more noreloc 18250Sstevel@tonic-gate * pages to appear. 18260Sstevel@tonic-gate * 18270Sstevel@tonic-gate * Short of adding a new field to the pcf bins to accurately track 18280Sstevel@tonic-gate * the number of free noreloc pages, we instead do not grab the 18290Sstevel@tonic-gate * pcgs_lock, do not set the pcf blocks and do not timeout when 18300Sstevel@tonic-gate * allocating a noreloc page. This allows noreloc allocations to 18310Sstevel@tonic-gate * loop without blocking global page pool allocations. 18320Sstevel@tonic-gate * 18330Sstevel@tonic-gate * NOTE: the behaviour of page_create_get_something has not changed 18340Sstevel@tonic-gate * for the case of global page pool allocations. 18350Sstevel@tonic-gate */ 18360Sstevel@tonic-gate 18370Sstevel@tonic-gate flags &= ~PG_MATCH_COLOR; 18380Sstevel@tonic-gate locked = 0; 18391385Skchow #if defined(__i386) || defined(__amd64) 18405084Sjohnlev flags = page_create_update_flags_x86(flags); 18410Sstevel@tonic-gate #endif 18420Sstevel@tonic-gate 18430Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, vaddr, PAGESIZE); 18440Sstevel@tonic-gate 18450Sstevel@tonic-gate for (count = 0; kcage_on || count < MAX_PCGS; count++) { 18460Sstevel@tonic-gate pp = page_get_freelist(vp, off, seg, vaddr, PAGESIZE, 18470Sstevel@tonic-gate flags, lgrp); 18480Sstevel@tonic-gate if (pp == NULL) { 18490Sstevel@tonic-gate pp = page_get_cachelist(vp, off, seg, vaddr, 18503559Smec flags, lgrp); 18510Sstevel@tonic-gate } 18520Sstevel@tonic-gate if (pp == NULL) { 18530Sstevel@tonic-gate /* 18540Sstevel@tonic-gate * Serialize. Don't fight with other pcgs(). 18550Sstevel@tonic-gate */ 18560Sstevel@tonic-gate if (!locked && (!kcage_on || !(flags & PG_NORELOC))) { 18570Sstevel@tonic-gate mutex_enter(&pcgs_lock); 18580Sstevel@tonic-gate VM_STAT_ADD(pcgs_locked); 18590Sstevel@tonic-gate locked = 1; 18600Sstevel@tonic-gate p = pcf; 18616880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 18620Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 18630Sstevel@tonic-gate ASSERT(p->pcf_block == 0); 18640Sstevel@tonic-gate p->pcf_block = 1; 18650Sstevel@tonic-gate p->pcf_reserve = p->pcf_count; 18660Sstevel@tonic-gate p->pcf_count = 0; 18670Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 18680Sstevel@tonic-gate p++; 18690Sstevel@tonic-gate } 18700Sstevel@tonic-gate freemem = 0; 18710Sstevel@tonic-gate } 18720Sstevel@tonic-gate 18730Sstevel@tonic-gate if (count) { 18740Sstevel@tonic-gate /* 18750Sstevel@tonic-gate * Since page_free() puts pages on 18760Sstevel@tonic-gate * a list then accounts for it, we 18770Sstevel@tonic-gate * just have to wait for page_free() 18780Sstevel@tonic-gate * to unlock any page it was working 18790Sstevel@tonic-gate * with. The page_lock()-page_reclaim() 18800Sstevel@tonic-gate * path falls in the same boat. 18810Sstevel@tonic-gate * 18820Sstevel@tonic-gate * We don't need to check on the 18830Sstevel@tonic-gate * PG_WAIT flag, we have already 18840Sstevel@tonic-gate * accounted for the page we are 18850Sstevel@tonic-gate * looking for in page_create_va(). 18860Sstevel@tonic-gate * 18870Sstevel@tonic-gate * We just wait a moment to let any 18880Sstevel@tonic-gate * locked pages on the lists free up, 18890Sstevel@tonic-gate * then continue around and try again. 18900Sstevel@tonic-gate * 18910Sstevel@tonic-gate * Will be awakened by set_freemem(). 18920Sstevel@tonic-gate */ 18930Sstevel@tonic-gate mutex_enter(&pcgs_wait_lock); 18940Sstevel@tonic-gate cv_wait(&pcgs_cv, &pcgs_wait_lock); 18950Sstevel@tonic-gate mutex_exit(&pcgs_wait_lock); 18960Sstevel@tonic-gate } 18970Sstevel@tonic-gate } else { 18980Sstevel@tonic-gate #ifdef VM_STATS 18990Sstevel@tonic-gate if (count >= PCGS_TRIES) { 19000Sstevel@tonic-gate VM_STAT_ADD(pcgs_too_many); 19010Sstevel@tonic-gate } else { 19020Sstevel@tonic-gate VM_STAT_ADD(pcgs_counts[count]); 19030Sstevel@tonic-gate } 19040Sstevel@tonic-gate #endif 19050Sstevel@tonic-gate if (locked) { 19060Sstevel@tonic-gate pcgs_unblock(); 19070Sstevel@tonic-gate mutex_exit(&pcgs_lock); 19080Sstevel@tonic-gate } 19090Sstevel@tonic-gate if (cagelocked) 19100Sstevel@tonic-gate mutex_exit(&pcgs_cagelock); 19110Sstevel@tonic-gate return (pp); 19120Sstevel@tonic-gate } 19130Sstevel@tonic-gate } 19140Sstevel@tonic-gate /* 19150Sstevel@tonic-gate * we go down holding the pcf locks. 19160Sstevel@tonic-gate */ 19170Sstevel@tonic-gate panic("no %spage found %d", 19180Sstevel@tonic-gate ((flags & PG_NORELOC) ? "non-reloc " : ""), count); 19190Sstevel@tonic-gate /*NOTREACHED*/ 19200Sstevel@tonic-gate } 19210Sstevel@tonic-gate 19220Sstevel@tonic-gate /* 19230Sstevel@tonic-gate * Create enough pages for "bytes" worth of data starting at 19240Sstevel@tonic-gate * "off" in "vp". 19250Sstevel@tonic-gate * 19260Sstevel@tonic-gate * Where flag must be one of: 19270Sstevel@tonic-gate * 19280Sstevel@tonic-gate * PG_EXCL: Exclusive create (fail if any page already 19290Sstevel@tonic-gate * exists in the page cache) which does not 19300Sstevel@tonic-gate * wait for memory to become available. 19310Sstevel@tonic-gate * 19320Sstevel@tonic-gate * PG_WAIT: Non-exclusive create which can wait for 19330Sstevel@tonic-gate * memory to become available. 19340Sstevel@tonic-gate * 19350Sstevel@tonic-gate * PG_PHYSCONTIG: Allocate physically contiguous pages. 19360Sstevel@tonic-gate * (Not Supported) 19370Sstevel@tonic-gate * 19380Sstevel@tonic-gate * A doubly linked list of pages is returned to the caller. Each page 19390Sstevel@tonic-gate * on the list has the "exclusive" (p_selock) lock and "iolock" (p_iolock) 19400Sstevel@tonic-gate * lock. 19410Sstevel@tonic-gate * 19420Sstevel@tonic-gate * Unable to change the parameters to page_create() in a minor release, 19430Sstevel@tonic-gate * we renamed page_create() to page_create_va(), changed all known calls 19440Sstevel@tonic-gate * from page_create() to page_create_va(), and created this wrapper. 19450Sstevel@tonic-gate * 19460Sstevel@tonic-gate * Upon a major release, we should break compatibility by deleting this 19470Sstevel@tonic-gate * wrapper, and replacing all the strings "page_create_va", with "page_create". 19480Sstevel@tonic-gate * 19490Sstevel@tonic-gate * NOTE: There is a copy of this interface as page_create_io() in 19500Sstevel@tonic-gate * i86/vm/vm_machdep.c. Any bugs fixed here should be applied 19510Sstevel@tonic-gate * there. 19520Sstevel@tonic-gate */ 19530Sstevel@tonic-gate page_t * 19540Sstevel@tonic-gate page_create(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags) 19550Sstevel@tonic-gate { 19560Sstevel@tonic-gate caddr_t random_vaddr; 19570Sstevel@tonic-gate struct seg kseg; 19580Sstevel@tonic-gate 19590Sstevel@tonic-gate #ifdef DEBUG 19600Sstevel@tonic-gate cmn_err(CE_WARN, "Using deprecated interface page_create: caller %p", 19610Sstevel@tonic-gate (void *)caller()); 19620Sstevel@tonic-gate #endif 19630Sstevel@tonic-gate 19640Sstevel@tonic-gate random_vaddr = (caddr_t)(((uintptr_t)vp >> 7) ^ 19650Sstevel@tonic-gate (uintptr_t)(off >> PAGESHIFT)); 19660Sstevel@tonic-gate kseg.s_as = &kas; 19670Sstevel@tonic-gate 19680Sstevel@tonic-gate return (page_create_va(vp, off, bytes, flags, &kseg, random_vaddr)); 19690Sstevel@tonic-gate } 19700Sstevel@tonic-gate 19710Sstevel@tonic-gate #ifdef DEBUG 19720Sstevel@tonic-gate uint32_t pg_alloc_pgs_mtbf = 0; 19730Sstevel@tonic-gate #endif 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate /* 19760Sstevel@tonic-gate * Used for large page support. It will attempt to allocate 19770Sstevel@tonic-gate * a large page(s) off the freelist. 19780Sstevel@tonic-gate * 19790Sstevel@tonic-gate * Returns non zero on failure. 19800Sstevel@tonic-gate */ 19810Sstevel@tonic-gate int 1982749Ssusans page_alloc_pages(struct vnode *vp, struct seg *seg, caddr_t addr, 19834426Saguzovsk page_t **basepp, page_t *ppa[], uint_t szc, int anypgsz, int pgflags) 19840Sstevel@tonic-gate { 19850Sstevel@tonic-gate pgcnt_t npgs, curnpgs, totpgs; 19860Sstevel@tonic-gate size_t pgsz; 19870Sstevel@tonic-gate page_t *pplist = NULL, *pp; 19880Sstevel@tonic-gate int err = 0; 19890Sstevel@tonic-gate lgrp_t *lgrp; 19900Sstevel@tonic-gate 19910Sstevel@tonic-gate ASSERT(szc != 0 && szc <= (page_num_pagesizes() - 1)); 19924426Saguzovsk ASSERT(pgflags == 0 || pgflags == PG_LOCAL); 19930Sstevel@tonic-gate 19946181Smec /* 19956181Smec * Check if system heavily prefers local large pages over remote 19966181Smec * on systems with multiple lgroups. 19976181Smec */ 19986181Smec if (lpg_alloc_prefer == LPAP_LOCAL && nlgrps > 1) { 19996181Smec pgflags = PG_LOCAL; 20006181Smec } 20016181Smec 20020Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[0]); 20030Sstevel@tonic-gate 20040Sstevel@tonic-gate #ifdef DEBUG 20050Sstevel@tonic-gate if (pg_alloc_pgs_mtbf && !(gethrtime() % pg_alloc_pgs_mtbf)) { 20060Sstevel@tonic-gate return (ENOMEM); 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate #endif 20090Sstevel@tonic-gate 20100Sstevel@tonic-gate /* 20110Sstevel@tonic-gate * One must be NULL but not both. 20120Sstevel@tonic-gate * And one must be non NULL but not both. 20130Sstevel@tonic-gate */ 20140Sstevel@tonic-gate ASSERT(basepp != NULL || ppa != NULL); 20150Sstevel@tonic-gate ASSERT(basepp == NULL || ppa == NULL); 20160Sstevel@tonic-gate 20175466Skchow #if defined(__i386) || defined(__amd64) 20185466Skchow while (page_chk_freelist(szc) == 0) { 20195466Skchow VM_STAT_ADD(alloc_pages[8]); 20205466Skchow if (anypgsz == 0 || --szc == 0) 20215466Skchow return (ENOMEM); 20225466Skchow } 20235466Skchow #endif 20245466Skchow 20255466Skchow pgsz = page_get_pagesize(szc); 20265466Skchow totpgs = curnpgs = npgs = pgsz >> PAGESHIFT; 20275466Skchow 20285466Skchow ASSERT(((uintptr_t)addr & (pgsz - 1)) == 0); 20295466Skchow 20300Sstevel@tonic-gate (void) page_create_wait(npgs, PG_WAIT); 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate while (npgs && szc) { 20330Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, addr, pgsz); 20344426Saguzovsk if (pgflags == PG_LOCAL) { 20354426Saguzovsk pp = page_get_freelist(vp, 0, seg, addr, pgsz, 20364426Saguzovsk pgflags, lgrp); 20374426Saguzovsk if (pp == NULL) { 20384426Saguzovsk pp = page_get_freelist(vp, 0, seg, addr, pgsz, 20394426Saguzovsk 0, lgrp); 20404426Saguzovsk } 20414426Saguzovsk } else { 20424426Saguzovsk pp = page_get_freelist(vp, 0, seg, addr, pgsz, 20434426Saguzovsk 0, lgrp); 20444426Saguzovsk } 20450Sstevel@tonic-gate if (pp != NULL) { 20460Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[1]); 20470Sstevel@tonic-gate page_list_concat(&pplist, &pp); 20480Sstevel@tonic-gate ASSERT(npgs >= curnpgs); 20490Sstevel@tonic-gate npgs -= curnpgs; 20500Sstevel@tonic-gate } else if (anypgsz) { 20510Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[2]); 20520Sstevel@tonic-gate szc--; 20530Sstevel@tonic-gate pgsz = page_get_pagesize(szc); 20540Sstevel@tonic-gate curnpgs = pgsz >> PAGESHIFT; 20550Sstevel@tonic-gate } else { 20560Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[3]); 20570Sstevel@tonic-gate ASSERT(npgs == totpgs); 20580Sstevel@tonic-gate page_create_putback(npgs); 20590Sstevel@tonic-gate return (ENOMEM); 20600Sstevel@tonic-gate } 20610Sstevel@tonic-gate } 20620Sstevel@tonic-gate if (szc == 0) { 20630Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[4]); 20640Sstevel@tonic-gate ASSERT(npgs != 0); 20650Sstevel@tonic-gate page_create_putback(npgs); 20660Sstevel@tonic-gate err = ENOMEM; 20670Sstevel@tonic-gate } else if (basepp != NULL) { 20680Sstevel@tonic-gate ASSERT(npgs == 0); 20690Sstevel@tonic-gate ASSERT(ppa == NULL); 20700Sstevel@tonic-gate *basepp = pplist; 20710Sstevel@tonic-gate } 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate npgs = totpgs - npgs; 20740Sstevel@tonic-gate pp = pplist; 20750Sstevel@tonic-gate 20760Sstevel@tonic-gate /* 20770Sstevel@tonic-gate * Clear the free and age bits. Also if we were passed in a ppa then 20780Sstevel@tonic-gate * fill it in with all the constituent pages from the large page. But 20790Sstevel@tonic-gate * if we failed to allocate all the pages just free what we got. 20800Sstevel@tonic-gate */ 20810Sstevel@tonic-gate while (npgs != 0) { 20820Sstevel@tonic-gate ASSERT(PP_ISFREE(pp)); 20830Sstevel@tonic-gate ASSERT(PP_ISAGED(pp)); 20840Sstevel@tonic-gate if (ppa != NULL || err != 0) { 20850Sstevel@tonic-gate if (err == 0) { 20860Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[5]); 20870Sstevel@tonic-gate PP_CLRFREE(pp); 20880Sstevel@tonic-gate PP_CLRAGED(pp); 20890Sstevel@tonic-gate page_sub(&pplist, pp); 20900Sstevel@tonic-gate *ppa++ = pp; 20910Sstevel@tonic-gate npgs--; 20920Sstevel@tonic-gate } else { 20930Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[6]); 20940Sstevel@tonic-gate ASSERT(pp->p_szc != 0); 20950Sstevel@tonic-gate curnpgs = page_get_pagecnt(pp->p_szc); 20960Sstevel@tonic-gate page_list_break(&pp, &pplist, curnpgs); 20970Sstevel@tonic-gate page_list_add_pages(pp, 0); 20980Sstevel@tonic-gate page_create_putback(curnpgs); 20990Sstevel@tonic-gate ASSERT(npgs >= curnpgs); 21000Sstevel@tonic-gate npgs -= curnpgs; 21010Sstevel@tonic-gate } 21020Sstevel@tonic-gate pp = pplist; 21030Sstevel@tonic-gate } else { 21040Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[7]); 21050Sstevel@tonic-gate PP_CLRFREE(pp); 21060Sstevel@tonic-gate PP_CLRAGED(pp); 21070Sstevel@tonic-gate pp = pp->p_next; 21080Sstevel@tonic-gate npgs--; 21090Sstevel@tonic-gate } 21100Sstevel@tonic-gate } 21110Sstevel@tonic-gate return (err); 21120Sstevel@tonic-gate } 21130Sstevel@tonic-gate 21140Sstevel@tonic-gate /* 21150Sstevel@tonic-gate * Get a single large page off of the freelists, and set it up for use. 21160Sstevel@tonic-gate * Number of bytes requested must be a supported page size. 21170Sstevel@tonic-gate * 21180Sstevel@tonic-gate * Note that this call may fail even if there is sufficient 21190Sstevel@tonic-gate * memory available or PG_WAIT is set, so the caller must 21200Sstevel@tonic-gate * be willing to fallback on page_create_va(), block and retry, 21210Sstevel@tonic-gate * or fail the requester. 21220Sstevel@tonic-gate */ 21230Sstevel@tonic-gate page_t * 21240Sstevel@tonic-gate page_create_va_large(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags, 21250Sstevel@tonic-gate struct seg *seg, caddr_t vaddr, void *arg) 21260Sstevel@tonic-gate { 21276880Sdv142724 pgcnt_t npages; 21280Sstevel@tonic-gate page_t *pp; 21290Sstevel@tonic-gate page_t *rootpp; 21300Sstevel@tonic-gate lgrp_t *lgrp; 21310Sstevel@tonic-gate lgrp_id_t *lgrpid = (lgrp_id_t *)arg; 21320Sstevel@tonic-gate 21330Sstevel@tonic-gate ASSERT(vp != NULL); 21340Sstevel@tonic-gate 21350Sstevel@tonic-gate ASSERT((flags & ~(PG_EXCL | PG_WAIT | 21363559Smec PG_NORELOC | PG_PANIC | PG_PUSHPAGE)) == 0); 21370Sstevel@tonic-gate /* but no others */ 21380Sstevel@tonic-gate 21390Sstevel@tonic-gate ASSERT((flags & PG_EXCL) == PG_EXCL); 21400Sstevel@tonic-gate 21410Sstevel@tonic-gate npages = btop(bytes); 21420Sstevel@tonic-gate 21430Sstevel@tonic-gate if (!kcage_on || panicstr) { 21440Sstevel@tonic-gate /* 21450Sstevel@tonic-gate * Cage is OFF, or we are single threaded in 21460Sstevel@tonic-gate * panic, so make everything a RELOC request. 21470Sstevel@tonic-gate */ 21480Sstevel@tonic-gate flags &= ~PG_NORELOC; 21490Sstevel@tonic-gate } 21500Sstevel@tonic-gate 21510Sstevel@tonic-gate /* 21520Sstevel@tonic-gate * Make sure there's adequate physical memory available. 21530Sstevel@tonic-gate * Note: PG_WAIT is ignored here. 21540Sstevel@tonic-gate */ 21550Sstevel@tonic-gate if (freemem <= throttlefree + npages) { 21560Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[1]); 21570Sstevel@tonic-gate return (NULL); 21580Sstevel@tonic-gate } 21590Sstevel@tonic-gate 21600Sstevel@tonic-gate /* 21610Sstevel@tonic-gate * If cage is on, dampen draw from cage when available 21620Sstevel@tonic-gate * cage space is low. 21630Sstevel@tonic-gate */ 21640Sstevel@tonic-gate if ((flags & (PG_NORELOC | PG_WAIT)) == (PG_NORELOC | PG_WAIT) && 21650Sstevel@tonic-gate kcage_freemem < kcage_throttlefree + npages) { 21660Sstevel@tonic-gate 21670Sstevel@tonic-gate /* 21680Sstevel@tonic-gate * The cage is on, the caller wants PG_NORELOC 21690Sstevel@tonic-gate * pages and available cage memory is very low. 21700Sstevel@tonic-gate * Call kcage_create_throttle() to attempt to 21710Sstevel@tonic-gate * control demand on the cage. 21720Sstevel@tonic-gate */ 21730Sstevel@tonic-gate if (kcage_create_throttle(npages, flags) == KCT_FAILURE) { 21740Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[2]); 21750Sstevel@tonic-gate return (NULL); 21760Sstevel@tonic-gate } 21770Sstevel@tonic-gate } 21780Sstevel@tonic-gate 21796880Sdv142724 if (!pcf_decrement_bucket(npages) && 21806880Sdv142724 !pcf_decrement_multiple(NULL, npages, 1)) { 21816880Sdv142724 VM_STAT_ADD(page_create_large_cnt[4]); 21826880Sdv142724 return (NULL); 21830Sstevel@tonic-gate } 21840Sstevel@tonic-gate 21850Sstevel@tonic-gate /* 21860Sstevel@tonic-gate * This is where this function behaves fundamentally differently 21870Sstevel@tonic-gate * than page_create_va(); since we're intending to map the page 21880Sstevel@tonic-gate * with a single TTE, we have to get it as a physically contiguous 21890Sstevel@tonic-gate * hardware pagesize chunk. If we can't, we fail. 21900Sstevel@tonic-gate */ 21910Sstevel@tonic-gate if (lgrpid != NULL && *lgrpid >= 0 && *lgrpid <= lgrp_alloc_max && 21923559Smec LGRP_EXISTS(lgrp_table[*lgrpid])) 21930Sstevel@tonic-gate lgrp = lgrp_table[*lgrpid]; 21940Sstevel@tonic-gate else 21950Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, vaddr, bytes); 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate if ((rootpp = page_get_freelist(&kvp, off, seg, vaddr, 21980Sstevel@tonic-gate bytes, flags & ~PG_MATCH_COLOR, lgrp)) == NULL) { 21990Sstevel@tonic-gate page_create_putback(npages); 22000Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[5]); 22010Sstevel@tonic-gate return (NULL); 22020Sstevel@tonic-gate } 22030Sstevel@tonic-gate 22040Sstevel@tonic-gate /* 22050Sstevel@tonic-gate * if we got the page with the wrong mtype give it back this is a 22060Sstevel@tonic-gate * workaround for CR 6249718. When CR 6249718 is fixed we never get 22070Sstevel@tonic-gate * inside "if" and the workaround becomes just a nop 22080Sstevel@tonic-gate */ 22090Sstevel@tonic-gate if (kcage_on && (flags & PG_NORELOC) && !PP_ISNORELOC(rootpp)) { 22100Sstevel@tonic-gate page_list_add_pages(rootpp, 0); 22110Sstevel@tonic-gate page_create_putback(npages); 22120Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[6]); 22130Sstevel@tonic-gate return (NULL); 22140Sstevel@tonic-gate } 22150Sstevel@tonic-gate 22160Sstevel@tonic-gate /* 22170Sstevel@tonic-gate * If satisfying this request has left us with too little 22180Sstevel@tonic-gate * memory, start the wheels turning to get some back. The 22190Sstevel@tonic-gate * first clause of the test prevents waking up the pageout 22200Sstevel@tonic-gate * daemon in situations where it would decide that there's 22210Sstevel@tonic-gate * nothing to do. 22220Sstevel@tonic-gate */ 22230Sstevel@tonic-gate if (nscan < desscan && freemem < minfree) { 22240Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL, 22250Sstevel@tonic-gate "pageout_cv_signal:freemem %ld", freemem); 22260Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 22270Sstevel@tonic-gate } 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate pp = rootpp; 22300Sstevel@tonic-gate while (npages--) { 22310Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 22320Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL); 22330Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 22340Sstevel@tonic-gate PP_CLRFREE(pp); 22350Sstevel@tonic-gate PP_CLRAGED(pp); 22360Sstevel@tonic-gate if (!page_hashin(pp, vp, off, NULL)) 22370Sstevel@tonic-gate panic("page_create_large: hashin failed: page %p", 22380Sstevel@tonic-gate (void *)pp); 22390Sstevel@tonic-gate page_io_lock(pp); 22400Sstevel@tonic-gate off += PAGESIZE; 22410Sstevel@tonic-gate pp = pp->p_next; 22420Sstevel@tonic-gate } 22430Sstevel@tonic-gate 22440Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[0]); 22450Sstevel@tonic-gate return (rootpp); 22460Sstevel@tonic-gate } 22470Sstevel@tonic-gate 22480Sstevel@tonic-gate page_t * 22490Sstevel@tonic-gate page_create_va(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags, 22500Sstevel@tonic-gate struct seg *seg, caddr_t vaddr) 22510Sstevel@tonic-gate { 22520Sstevel@tonic-gate page_t *plist = NULL; 22530Sstevel@tonic-gate pgcnt_t npages; 22540Sstevel@tonic-gate pgcnt_t found_on_free = 0; 22550Sstevel@tonic-gate pgcnt_t pages_req; 22560Sstevel@tonic-gate page_t *npp = NULL; 22570Sstevel@tonic-gate struct pcf *p; 22580Sstevel@tonic-gate lgrp_t *lgrp; 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_START, 22613559Smec "page_create_start:vp %p off %llx bytes %lu flags %x", 22623559Smec vp, off, bytes, flags); 22630Sstevel@tonic-gate 22640Sstevel@tonic-gate ASSERT(bytes != 0 && vp != NULL); 22650Sstevel@tonic-gate 22660Sstevel@tonic-gate if ((flags & PG_EXCL) == 0 && (flags & PG_WAIT) == 0) { 22670Sstevel@tonic-gate panic("page_create: invalid flags"); 22680Sstevel@tonic-gate /*NOTREACHED*/ 22690Sstevel@tonic-gate } 22700Sstevel@tonic-gate ASSERT((flags & ~(PG_EXCL | PG_WAIT | 22710Sstevel@tonic-gate PG_NORELOC | PG_PANIC | PG_PUSHPAGE)) == 0); 22720Sstevel@tonic-gate /* but no others */ 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate pages_req = npages = btopr(bytes); 22750Sstevel@tonic-gate /* 22760Sstevel@tonic-gate * Try to see whether request is too large to *ever* be 22770Sstevel@tonic-gate * satisfied, in order to prevent deadlock. We arbitrarily 22780Sstevel@tonic-gate * decide to limit maximum size requests to max_page_get. 22790Sstevel@tonic-gate */ 22800Sstevel@tonic-gate if (npages >= max_page_get) { 22810Sstevel@tonic-gate if ((flags & PG_WAIT) == 0) { 22820Sstevel@tonic-gate TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_TOOBIG, 22830Sstevel@tonic-gate "page_create_toobig:vp %p off %llx npages " 22840Sstevel@tonic-gate "%lu max_page_get %lu", 22850Sstevel@tonic-gate vp, off, npages, max_page_get); 22860Sstevel@tonic-gate return (NULL); 22870Sstevel@tonic-gate } else { 22880Sstevel@tonic-gate cmn_err(CE_WARN, 22890Sstevel@tonic-gate "Request for too much kernel memory " 22900Sstevel@tonic-gate "(%lu bytes), will hang forever", bytes); 22910Sstevel@tonic-gate for (;;) 22920Sstevel@tonic-gate delay(1000000000); 22930Sstevel@tonic-gate } 22940Sstevel@tonic-gate } 22950Sstevel@tonic-gate 22960Sstevel@tonic-gate if (!kcage_on || panicstr) { 22970Sstevel@tonic-gate /* 22980Sstevel@tonic-gate * Cage is OFF, or we are single threaded in 22990Sstevel@tonic-gate * panic, so make everything a RELOC request. 23000Sstevel@tonic-gate */ 23010Sstevel@tonic-gate flags &= ~PG_NORELOC; 23020Sstevel@tonic-gate } 23030Sstevel@tonic-gate 23040Sstevel@tonic-gate if (freemem <= throttlefree + npages) 23050Sstevel@tonic-gate if (!page_create_throttle(npages, flags)) 23060Sstevel@tonic-gate return (NULL); 23070Sstevel@tonic-gate 23080Sstevel@tonic-gate /* 23090Sstevel@tonic-gate * If cage is on, dampen draw from cage when available 23100Sstevel@tonic-gate * cage space is low. 23110Sstevel@tonic-gate */ 23120Sstevel@tonic-gate if ((flags & PG_NORELOC) && 23133559Smec kcage_freemem < kcage_throttlefree + npages) { 23140Sstevel@tonic-gate 23150Sstevel@tonic-gate /* 23160Sstevel@tonic-gate * The cage is on, the caller wants PG_NORELOC 23170Sstevel@tonic-gate * pages and available cage memory is very low. 23180Sstevel@tonic-gate * Call kcage_create_throttle() to attempt to 23190Sstevel@tonic-gate * control demand on the cage. 23200Sstevel@tonic-gate */ 23210Sstevel@tonic-gate if (kcage_create_throttle(npages, flags) == KCT_FAILURE) 23220Sstevel@tonic-gate return (NULL); 23230Sstevel@tonic-gate } 23240Sstevel@tonic-gate 23250Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[0]); 23260Sstevel@tonic-gate 23276880Sdv142724 if (!pcf_decrement_bucket(npages)) { 23280Sstevel@tonic-gate /* 23290Sstevel@tonic-gate * Have to look harder. If npages is greater than 23305331Samw * one, then we might have to coalesce the counters. 23310Sstevel@tonic-gate * 23320Sstevel@tonic-gate * Go wait. We come back having accounted 23330Sstevel@tonic-gate * for the memory. 23340Sstevel@tonic-gate */ 23350Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[1]); 23360Sstevel@tonic-gate if (!page_create_wait(npages, flags)) { 23370Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[2]); 23380Sstevel@tonic-gate return (NULL); 23390Sstevel@tonic-gate } 23400Sstevel@tonic-gate } 23410Sstevel@tonic-gate 23420Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SUCCESS, 23433559Smec "page_create_success:vp %p off %llx", vp, off); 23440Sstevel@tonic-gate 23450Sstevel@tonic-gate /* 23460Sstevel@tonic-gate * If satisfying this request has left us with too little 23470Sstevel@tonic-gate * memory, start the wheels turning to get some back. The 23480Sstevel@tonic-gate * first clause of the test prevents waking up the pageout 23490Sstevel@tonic-gate * daemon in situations where it would decide that there's 23500Sstevel@tonic-gate * nothing to do. 23510Sstevel@tonic-gate */ 23520Sstevel@tonic-gate if (nscan < desscan && freemem < minfree) { 23530Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL, 23543559Smec "pageout_cv_signal:freemem %ld", freemem); 23550Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 23560Sstevel@tonic-gate } 23570Sstevel@tonic-gate 23580Sstevel@tonic-gate /* 23590Sstevel@tonic-gate * Loop around collecting the requested number of pages. 23600Sstevel@tonic-gate * Most of the time, we have to `create' a new page. With 23610Sstevel@tonic-gate * this in mind, pull the page off the free list before 23620Sstevel@tonic-gate * getting the hash lock. This will minimize the hash 23630Sstevel@tonic-gate * lock hold time, nesting, and the like. If it turns 23640Sstevel@tonic-gate * out we don't need the page, we put it back at the end. 23650Sstevel@tonic-gate */ 23660Sstevel@tonic-gate while (npages--) { 23670Sstevel@tonic-gate page_t *pp; 23680Sstevel@tonic-gate kmutex_t *phm = NULL; 23690Sstevel@tonic-gate ulong_t index; 23700Sstevel@tonic-gate 23710Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 23720Sstevel@tonic-gate top: 23730Sstevel@tonic-gate ASSERT(phm == NULL); 23740Sstevel@tonic-gate ASSERT(index == PAGE_HASH_FUNC(vp, off)); 23750Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 23760Sstevel@tonic-gate 23770Sstevel@tonic-gate if (npp == NULL) { 23780Sstevel@tonic-gate /* 23790Sstevel@tonic-gate * Try to get a page from the freelist (ie, 23800Sstevel@tonic-gate * a page with no [vp, off] tag). If that 23810Sstevel@tonic-gate * fails, use the cachelist. 23820Sstevel@tonic-gate * 23830Sstevel@tonic-gate * During the first attempt at both the free 23840Sstevel@tonic-gate * and cache lists we try for the correct color. 23850Sstevel@tonic-gate */ 23860Sstevel@tonic-gate /* 23870Sstevel@tonic-gate * XXXX-how do we deal with virtual indexed 23880Sstevel@tonic-gate * caches and and colors? 23890Sstevel@tonic-gate */ 23900Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[4]); 23910Sstevel@tonic-gate /* 23920Sstevel@tonic-gate * Get lgroup to allocate next page of shared memory 23930Sstevel@tonic-gate * from and use it to specify where to allocate 23940Sstevel@tonic-gate * the physical memory 23950Sstevel@tonic-gate */ 23960Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, vaddr, PAGESIZE); 23970Sstevel@tonic-gate npp = page_get_freelist(vp, off, seg, vaddr, PAGESIZE, 23980Sstevel@tonic-gate flags | PG_MATCH_COLOR, lgrp); 23990Sstevel@tonic-gate if (npp == NULL) { 24000Sstevel@tonic-gate npp = page_get_cachelist(vp, off, seg, 24010Sstevel@tonic-gate vaddr, flags | PG_MATCH_COLOR, lgrp); 24020Sstevel@tonic-gate if (npp == NULL) { 24030Sstevel@tonic-gate npp = page_create_get_something(vp, 24040Sstevel@tonic-gate off, seg, vaddr, 24050Sstevel@tonic-gate flags & ~PG_MATCH_COLOR); 24060Sstevel@tonic-gate } 24070Sstevel@tonic-gate 24080Sstevel@tonic-gate if (PP_ISAGED(npp) == 0) { 24090Sstevel@tonic-gate /* 24100Sstevel@tonic-gate * Since this page came from the 24110Sstevel@tonic-gate * cachelist, we must destroy the 24120Sstevel@tonic-gate * old vnode association. 24130Sstevel@tonic-gate */ 24140Sstevel@tonic-gate page_hashout(npp, NULL); 24150Sstevel@tonic-gate } 24160Sstevel@tonic-gate } 24170Sstevel@tonic-gate } 24180Sstevel@tonic-gate 24190Sstevel@tonic-gate /* 24200Sstevel@tonic-gate * We own this page! 24210Sstevel@tonic-gate */ 24220Sstevel@tonic-gate ASSERT(PAGE_EXCL(npp)); 24230Sstevel@tonic-gate ASSERT(npp->p_vnode == NULL); 24240Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(npp)); 24250Sstevel@tonic-gate PP_CLRFREE(npp); 24260Sstevel@tonic-gate PP_CLRAGED(npp); 24270Sstevel@tonic-gate 24280Sstevel@tonic-gate /* 24290Sstevel@tonic-gate * Here we have a page in our hot little mits and are 24300Sstevel@tonic-gate * just waiting to stuff it on the appropriate lists. 24310Sstevel@tonic-gate * Get the mutex and check to see if it really does 24320Sstevel@tonic-gate * not exist. 24330Sstevel@tonic-gate */ 24340Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 24350Sstevel@tonic-gate mutex_enter(phm); 24360Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 24370Sstevel@tonic-gate if (pp == NULL) { 24380Sstevel@tonic-gate VM_STAT_ADD(page_create_new); 24390Sstevel@tonic-gate pp = npp; 24400Sstevel@tonic-gate npp = NULL; 24410Sstevel@tonic-gate if (!page_hashin(pp, vp, off, phm)) { 24420Sstevel@tonic-gate /* 24430Sstevel@tonic-gate * Since we hold the page hash mutex and 24440Sstevel@tonic-gate * just searched for this page, page_hashin 24450Sstevel@tonic-gate * had better not fail. If it does, that 24460Sstevel@tonic-gate * means somethread did not follow the 24470Sstevel@tonic-gate * page hash mutex rules. Panic now and 24480Sstevel@tonic-gate * get it over with. As usual, go down 24490Sstevel@tonic-gate * holding all the locks. 24500Sstevel@tonic-gate */ 24510Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 24520Sstevel@tonic-gate panic("page_create: " 24530Sstevel@tonic-gate "hashin failed %p %p %llx %p", 24540Sstevel@tonic-gate (void *)pp, (void *)vp, off, (void *)phm); 24550Sstevel@tonic-gate /*NOTREACHED*/ 24560Sstevel@tonic-gate } 24570Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 24580Sstevel@tonic-gate mutex_exit(phm); 24590Sstevel@tonic-gate phm = NULL; 24600Sstevel@tonic-gate 24610Sstevel@tonic-gate /* 24620Sstevel@tonic-gate * Hat layer locking need not be done to set 24630Sstevel@tonic-gate * the following bits since the page is not hashed 24640Sstevel@tonic-gate * and was on the free list (i.e., had no mappings). 24650Sstevel@tonic-gate * 24660Sstevel@tonic-gate * Set the reference bit to protect 24670Sstevel@tonic-gate * against immediate pageout 24680Sstevel@tonic-gate * 24690Sstevel@tonic-gate * XXXmh modify freelist code to set reference 24700Sstevel@tonic-gate * bit so we don't have to do it here. 24710Sstevel@tonic-gate */ 24720Sstevel@tonic-gate page_set_props(pp, P_REF); 24730Sstevel@tonic-gate found_on_free++; 24740Sstevel@tonic-gate } else { 24750Sstevel@tonic-gate VM_STAT_ADD(page_create_exists); 24760Sstevel@tonic-gate if (flags & PG_EXCL) { 24770Sstevel@tonic-gate /* 24780Sstevel@tonic-gate * Found an existing page, and the caller 24790Sstevel@tonic-gate * wanted all new pages. Undo all of the work 24800Sstevel@tonic-gate * we have done. 24810Sstevel@tonic-gate */ 24820Sstevel@tonic-gate mutex_exit(phm); 24830Sstevel@tonic-gate phm = NULL; 24840Sstevel@tonic-gate while (plist != NULL) { 24850Sstevel@tonic-gate pp = plist; 24860Sstevel@tonic-gate page_sub(&plist, pp); 24870Sstevel@tonic-gate page_io_unlock(pp); 24880Sstevel@tonic-gate /* large pages should not end up here */ 24890Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 24900Sstevel@tonic-gate /*LINTED: constant in conditional ctx*/ 24910Sstevel@tonic-gate VN_DISPOSE(pp, B_INVAL, 0, kcred); 24920Sstevel@tonic-gate } 24930Sstevel@tonic-gate VM_STAT_ADD(page_create_found_one); 24940Sstevel@tonic-gate goto fail; 24950Sstevel@tonic-gate } 24960Sstevel@tonic-gate ASSERT(flags & PG_WAIT); 24970Sstevel@tonic-gate if (!page_lock(pp, SE_EXCL, phm, P_NO_RECLAIM)) { 24980Sstevel@tonic-gate /* 24990Sstevel@tonic-gate * Start all over again if we blocked trying 25000Sstevel@tonic-gate * to lock the page. 25010Sstevel@tonic-gate */ 25020Sstevel@tonic-gate mutex_exit(phm); 25030Sstevel@tonic-gate VM_STAT_ADD(page_create_page_lock_failed); 25040Sstevel@tonic-gate phm = NULL; 25050Sstevel@tonic-gate goto top; 25060Sstevel@tonic-gate } 25070Sstevel@tonic-gate mutex_exit(phm); 25080Sstevel@tonic-gate phm = NULL; 25090Sstevel@tonic-gate 25100Sstevel@tonic-gate if (PP_ISFREE(pp)) { 25110Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0); 25120Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_get_cache); 25130Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST); 25140Sstevel@tonic-gate PP_CLRFREE(pp); 25150Sstevel@tonic-gate found_on_free++; 25160Sstevel@tonic-gate } 25170Sstevel@tonic-gate } 25180Sstevel@tonic-gate 25190Sstevel@tonic-gate /* 25200Sstevel@tonic-gate * Got a page! It is locked. Acquire the i/o 25210Sstevel@tonic-gate * lock since we are going to use the p_next and 25220Sstevel@tonic-gate * p_prev fields to link the requested pages together. 25230Sstevel@tonic-gate */ 25240Sstevel@tonic-gate page_io_lock(pp); 25250Sstevel@tonic-gate page_add(&plist, pp); 25260Sstevel@tonic-gate plist = plist->p_next; 25270Sstevel@tonic-gate off += PAGESIZE; 25280Sstevel@tonic-gate vaddr += PAGESIZE; 25290Sstevel@tonic-gate } 25300Sstevel@tonic-gate 25310Sstevel@tonic-gate ASSERT((flags & PG_EXCL) ? (found_on_free == pages_req) : 1); 25320Sstevel@tonic-gate fail: 25330Sstevel@tonic-gate if (npp != NULL) { 25340Sstevel@tonic-gate /* 25350Sstevel@tonic-gate * Did not need this page after all. 25360Sstevel@tonic-gate * Put it back on the free list. 25370Sstevel@tonic-gate */ 25380Sstevel@tonic-gate VM_STAT_ADD(page_create_putbacks); 25390Sstevel@tonic-gate PP_SETFREE(npp); 25400Sstevel@tonic-gate PP_SETAGED(npp); 25410Sstevel@tonic-gate npp->p_offset = (u_offset_t)-1; 25420Sstevel@tonic-gate page_list_add(npp, PG_FREE_LIST | PG_LIST_TAIL); 25430Sstevel@tonic-gate page_unlock(npp); 25440Sstevel@tonic-gate 25450Sstevel@tonic-gate } 25460Sstevel@tonic-gate 25470Sstevel@tonic-gate ASSERT(pages_req >= found_on_free); 25480Sstevel@tonic-gate 25490Sstevel@tonic-gate { 25500Sstevel@tonic-gate uint_t overshoot = (uint_t)(pages_req - found_on_free); 25510Sstevel@tonic-gate 25520Sstevel@tonic-gate if (overshoot) { 25530Sstevel@tonic-gate VM_STAT_ADD(page_create_overshoot); 25546880Sdv142724 p = &pcf[PCF_INDEX()]; 25550Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 25560Sstevel@tonic-gate if (p->pcf_block) { 25570Sstevel@tonic-gate p->pcf_reserve += overshoot; 25580Sstevel@tonic-gate } else { 25590Sstevel@tonic-gate p->pcf_count += overshoot; 25600Sstevel@tonic-gate if (p->pcf_wait) { 25610Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 25620Sstevel@tonic-gate if (freemem_wait) { 25630Sstevel@tonic-gate cv_signal(&freemem_cv); 25640Sstevel@tonic-gate p->pcf_wait--; 25650Sstevel@tonic-gate } else { 25660Sstevel@tonic-gate p->pcf_wait = 0; 25670Sstevel@tonic-gate } 25680Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 25690Sstevel@tonic-gate } 25700Sstevel@tonic-gate } 25710Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 25720Sstevel@tonic-gate /* freemem is approximate, so this test OK */ 25730Sstevel@tonic-gate if (!p->pcf_block) 25740Sstevel@tonic-gate freemem += overshoot; 25750Sstevel@tonic-gate } 25760Sstevel@tonic-gate } 25770Sstevel@tonic-gate 25780Sstevel@tonic-gate return (plist); 25790Sstevel@tonic-gate } 25800Sstevel@tonic-gate 25810Sstevel@tonic-gate /* 25820Sstevel@tonic-gate * One or more constituent pages of this large page has been marked 25830Sstevel@tonic-gate * toxic. Simply demote the large page to PAGESIZE pages and let 25840Sstevel@tonic-gate * page_free() handle it. This routine should only be called by 25850Sstevel@tonic-gate * large page free routines (page_free_pages() and page_destroy_pages(). 25860Sstevel@tonic-gate * All pages are locked SE_EXCL and have already been marked free. 25870Sstevel@tonic-gate */ 25880Sstevel@tonic-gate static void 25890Sstevel@tonic-gate page_free_toxic_pages(page_t *rootpp) 25900Sstevel@tonic-gate { 25910Sstevel@tonic-gate page_t *tpp; 25920Sstevel@tonic-gate pgcnt_t i, pgcnt = page_get_pagecnt(rootpp->p_szc); 25930Sstevel@tonic-gate uint_t szc = rootpp->p_szc; 25940Sstevel@tonic-gate 25950Sstevel@tonic-gate for (i = 0, tpp = rootpp; i < pgcnt; i++, tpp = tpp->p_next) { 25960Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 25970Sstevel@tonic-gate ASSERT((PAGE_EXCL(tpp) && 25980Sstevel@tonic-gate !page_iolock_assert(tpp)) || panicstr); 25990Sstevel@tonic-gate tpp->p_szc = 0; 26000Sstevel@tonic-gate } 26010Sstevel@tonic-gate 26020Sstevel@tonic-gate while (rootpp != NULL) { 26030Sstevel@tonic-gate tpp = rootpp; 26040Sstevel@tonic-gate page_sub(&rootpp, tpp); 26050Sstevel@tonic-gate ASSERT(PP_ISFREE(tpp)); 26060Sstevel@tonic-gate PP_CLRFREE(tpp); 26070Sstevel@tonic-gate page_free(tpp, 1); 26080Sstevel@tonic-gate } 26090Sstevel@tonic-gate } 26100Sstevel@tonic-gate 26110Sstevel@tonic-gate /* 26120Sstevel@tonic-gate * Put page on the "free" list. 26130Sstevel@tonic-gate * The free list is really two lists maintained by 26140Sstevel@tonic-gate * the PSM of whatever machine we happen to be on. 26150Sstevel@tonic-gate */ 26160Sstevel@tonic-gate void 26170Sstevel@tonic-gate page_free(page_t *pp, int dontneed) 26180Sstevel@tonic-gate { 26190Sstevel@tonic-gate struct pcf *p; 26200Sstevel@tonic-gate uint_t pcf_index; 26210Sstevel@tonic-gate 26220Sstevel@tonic-gate ASSERT((PAGE_EXCL(pp) && 26230Sstevel@tonic-gate !page_iolock_assert(pp)) || panicstr); 26240Sstevel@tonic-gate 26250Sstevel@tonic-gate if (PP_ISFREE(pp)) { 26260Sstevel@tonic-gate panic("page_free: page %p is free", (void *)pp); 26270Sstevel@tonic-gate } 26280Sstevel@tonic-gate 26290Sstevel@tonic-gate if (pp->p_szc != 0) { 26300Sstevel@tonic-gate if (pp->p_vnode == NULL || IS_SWAPFSVP(pp->p_vnode) || 26313290Sjohansen PP_ISKAS(pp)) { 26320Sstevel@tonic-gate panic("page_free: anon or kernel " 26330Sstevel@tonic-gate "or no vnode large page %p", (void *)pp); 26340Sstevel@tonic-gate } 26350Sstevel@tonic-gate page_demote_vp_pages(pp); 26360Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 26370Sstevel@tonic-gate } 26380Sstevel@tonic-gate 26390Sstevel@tonic-gate /* 26400Sstevel@tonic-gate * The page_struct_lock need not be acquired to examine these 26410Sstevel@tonic-gate * fields since the page has an "exclusive" lock. 26420Sstevel@tonic-gate */ 26432414Saguzovsk if (hat_page_is_mapped(pp) || pp->p_lckcnt != 0 || pp->p_cowcnt != 0 || 26442414Saguzovsk pp->p_slckcnt != 0) { 26452414Saguzovsk panic("page_free pp=%p, pfn=%lx, lckcnt=%d, cowcnt=%d " 26467632SNick.Todd@Sun.COM "slckcnt = %d", (void *)pp, page_pptonum(pp), pp->p_lckcnt, 26472414Saguzovsk pp->p_cowcnt, pp->p_slckcnt); 26480Sstevel@tonic-gate /*NOTREACHED*/ 26490Sstevel@tonic-gate } 26500Sstevel@tonic-gate 26510Sstevel@tonic-gate ASSERT(!hat_page_getshare(pp)); 26520Sstevel@tonic-gate 26530Sstevel@tonic-gate PP_SETFREE(pp); 26540Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL || !IS_VMODSORT(pp->p_vnode) || 26550Sstevel@tonic-gate !hat_ismod(pp)); 26567718SJason.Beloro@Sun.COM page_clr_all_props(pp, 0); 26570Sstevel@tonic-gate ASSERT(!hat_page_getshare(pp)); 26580Sstevel@tonic-gate 26590Sstevel@tonic-gate /* 26600Sstevel@tonic-gate * Now we add the page to the head of the free list. 26610Sstevel@tonic-gate * But if this page is associated with a paged vnode 26620Sstevel@tonic-gate * then we adjust the head forward so that the page is 26630Sstevel@tonic-gate * effectively at the end of the list. 26640Sstevel@tonic-gate */ 26650Sstevel@tonic-gate if (pp->p_vnode == NULL) { 26660Sstevel@tonic-gate /* 26670Sstevel@tonic-gate * Page has no identity, put it on the free list. 26680Sstevel@tonic-gate */ 26690Sstevel@tonic-gate PP_SETAGED(pp); 26700Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1; 26710Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 26720Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_free); 26730Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_FREE, 26740Sstevel@tonic-gate "page_free_free:pp %p", pp); 26750Sstevel@tonic-gate } else { 26760Sstevel@tonic-gate PP_CLRAGED(pp); 26770Sstevel@tonic-gate 26780Sstevel@tonic-gate if (!dontneed || nopageage) { 26790Sstevel@tonic-gate /* move it to the tail of the list */ 26800Sstevel@tonic-gate page_list_add(pp, PG_CACHE_LIST | PG_LIST_TAIL); 26810Sstevel@tonic-gate 26820Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_cache); 26830Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_CACHE_TAIL, 26840Sstevel@tonic-gate "page_free_cache_tail:pp %p", pp); 26850Sstevel@tonic-gate } else { 26860Sstevel@tonic-gate page_list_add(pp, PG_CACHE_LIST | PG_LIST_HEAD); 26870Sstevel@tonic-gate 26880Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_dontneed); 26890Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_CACHE_HEAD, 26900Sstevel@tonic-gate "page_free_cache_head:pp %p", pp); 26910Sstevel@tonic-gate } 26920Sstevel@tonic-gate } 26930Sstevel@tonic-gate page_unlock(pp); 26940Sstevel@tonic-gate 26950Sstevel@tonic-gate /* 26960Sstevel@tonic-gate * Now do the `freemem' accounting. 26970Sstevel@tonic-gate */ 26980Sstevel@tonic-gate pcf_index = PCF_INDEX(); 26990Sstevel@tonic-gate p = &pcf[pcf_index]; 27000Sstevel@tonic-gate 27010Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 27020Sstevel@tonic-gate if (p->pcf_block) { 27030Sstevel@tonic-gate p->pcf_reserve += 1; 27040Sstevel@tonic-gate } else { 27050Sstevel@tonic-gate p->pcf_count += 1; 27060Sstevel@tonic-gate if (p->pcf_wait) { 27070Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 27080Sstevel@tonic-gate /* 27090Sstevel@tonic-gate * Check to see if some other thread 27100Sstevel@tonic-gate * is actually waiting. Another bucket 27110Sstevel@tonic-gate * may have woken it up by now. If there 27120Sstevel@tonic-gate * are no waiters, then set our pcf_wait 27130Sstevel@tonic-gate * count to zero to avoid coming in here 27140Sstevel@tonic-gate * next time. Also, since only one page 27150Sstevel@tonic-gate * was put on the free list, just wake 27160Sstevel@tonic-gate * up one waiter. 27170Sstevel@tonic-gate */ 27180Sstevel@tonic-gate if (freemem_wait) { 27190Sstevel@tonic-gate cv_signal(&freemem_cv); 27200Sstevel@tonic-gate p->pcf_wait--; 27210Sstevel@tonic-gate } else { 27220Sstevel@tonic-gate p->pcf_wait = 0; 27230Sstevel@tonic-gate } 27240Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 27250Sstevel@tonic-gate } 27260Sstevel@tonic-gate } 27270Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 27280Sstevel@tonic-gate 27290Sstevel@tonic-gate /* freemem is approximate, so this test OK */ 27300Sstevel@tonic-gate if (!p->pcf_block) 27310Sstevel@tonic-gate freemem += 1; 27320Sstevel@tonic-gate } 27330Sstevel@tonic-gate 27340Sstevel@tonic-gate /* 27350Sstevel@tonic-gate * Put page on the "free" list during intial startup. 27360Sstevel@tonic-gate * This happens during initial single threaded execution. 27370Sstevel@tonic-gate */ 27380Sstevel@tonic-gate void 27390Sstevel@tonic-gate page_free_at_startup(page_t *pp) 27400Sstevel@tonic-gate { 27410Sstevel@tonic-gate struct pcf *p; 27420Sstevel@tonic-gate uint_t pcf_index; 27430Sstevel@tonic-gate 27440Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_HEAD | PG_LIST_ISINIT); 27450Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_free); 27460Sstevel@tonic-gate 27470Sstevel@tonic-gate /* 27480Sstevel@tonic-gate * Now do the `freemem' accounting. 27490Sstevel@tonic-gate */ 27500Sstevel@tonic-gate pcf_index = PCF_INDEX(); 27510Sstevel@tonic-gate p = &pcf[pcf_index]; 27520Sstevel@tonic-gate 27530Sstevel@tonic-gate ASSERT(p->pcf_block == 0); 27540Sstevel@tonic-gate ASSERT(p->pcf_wait == 0); 27550Sstevel@tonic-gate p->pcf_count += 1; 27560Sstevel@tonic-gate 27570Sstevel@tonic-gate /* freemem is approximate, so this is OK */ 27580Sstevel@tonic-gate freemem += 1; 27590Sstevel@tonic-gate } 27600Sstevel@tonic-gate 27610Sstevel@tonic-gate void 27620Sstevel@tonic-gate page_free_pages(page_t *pp) 27630Sstevel@tonic-gate { 27640Sstevel@tonic-gate page_t *tpp, *rootpp = NULL; 27650Sstevel@tonic-gate pgcnt_t pgcnt = page_get_pagecnt(pp->p_szc); 27660Sstevel@tonic-gate pgcnt_t i; 27670Sstevel@tonic-gate uint_t szc = pp->p_szc; 27680Sstevel@tonic-gate 27690Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_pages); 27700Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_FREE, 27710Sstevel@tonic-gate "page_free_free:pp %p", pp); 27720Sstevel@tonic-gate 27730Sstevel@tonic-gate ASSERT(pp->p_szc != 0 && pp->p_szc < page_num_pagesizes()); 27740Sstevel@tonic-gate if ((page_pptonum(pp) & (pgcnt - 1)) != 0) { 27750Sstevel@tonic-gate panic("page_free_pages: not root page %p", (void *)pp); 27760Sstevel@tonic-gate /*NOTREACHED*/ 27770Sstevel@tonic-gate } 27780Sstevel@tonic-gate 2779414Skchow for (i = 0, tpp = pp; i < pgcnt; i++, tpp++) { 27800Sstevel@tonic-gate ASSERT((PAGE_EXCL(tpp) && 27810Sstevel@tonic-gate !page_iolock_assert(tpp)) || panicstr); 27820Sstevel@tonic-gate if (PP_ISFREE(tpp)) { 27830Sstevel@tonic-gate panic("page_free_pages: page %p is free", (void *)tpp); 27840Sstevel@tonic-gate /*NOTREACHED*/ 27850Sstevel@tonic-gate } 27860Sstevel@tonic-gate if (hat_page_is_mapped(tpp) || tpp->p_lckcnt != 0 || 27872414Saguzovsk tpp->p_cowcnt != 0 || tpp->p_slckcnt != 0) { 27880Sstevel@tonic-gate panic("page_free_pages %p", (void *)tpp); 27890Sstevel@tonic-gate /*NOTREACHED*/ 27900Sstevel@tonic-gate } 27910Sstevel@tonic-gate 27920Sstevel@tonic-gate ASSERT(!hat_page_getshare(tpp)); 27930Sstevel@tonic-gate ASSERT(tpp->p_vnode == NULL); 27940Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 27950Sstevel@tonic-gate 27960Sstevel@tonic-gate PP_SETFREE(tpp); 27977718SJason.Beloro@Sun.COM page_clr_all_props(tpp, 0); 27980Sstevel@tonic-gate PP_SETAGED(tpp); 27990Sstevel@tonic-gate tpp->p_offset = (u_offset_t)-1; 28000Sstevel@tonic-gate ASSERT(tpp->p_next == tpp); 28010Sstevel@tonic-gate ASSERT(tpp->p_prev == tpp); 28020Sstevel@tonic-gate page_list_concat(&rootpp, &tpp); 28030Sstevel@tonic-gate } 28040Sstevel@tonic-gate ASSERT(rootpp == pp); 28050Sstevel@tonic-gate 28060Sstevel@tonic-gate page_list_add_pages(rootpp, 0); 28070Sstevel@tonic-gate page_create_putback(pgcnt); 28080Sstevel@tonic-gate } 28090Sstevel@tonic-gate 28100Sstevel@tonic-gate int free_pages = 1; 28110Sstevel@tonic-gate 28120Sstevel@tonic-gate /* 28130Sstevel@tonic-gate * This routine attempts to return pages to the cachelist via page_release(). 28140Sstevel@tonic-gate * It does not *have* to be successful in all cases, since the pageout scanner 28150Sstevel@tonic-gate * will catch any pages it misses. It does need to be fast and not introduce 28160Sstevel@tonic-gate * too much overhead. 28170Sstevel@tonic-gate * 28180Sstevel@tonic-gate * If a page isn't found on the unlocked sweep of the page_hash bucket, we 28190Sstevel@tonic-gate * don't lock and retry. This is ok, since the page scanner will eventually 28200Sstevel@tonic-gate * find any page we miss in free_vp_pages(). 28210Sstevel@tonic-gate */ 28220Sstevel@tonic-gate void 28230Sstevel@tonic-gate free_vp_pages(vnode_t *vp, u_offset_t off, size_t len) 28240Sstevel@tonic-gate { 28250Sstevel@tonic-gate page_t *pp; 28260Sstevel@tonic-gate u_offset_t eoff; 28270Sstevel@tonic-gate extern int swap_in_range(vnode_t *, u_offset_t, size_t); 28280Sstevel@tonic-gate 28290Sstevel@tonic-gate eoff = off + len; 28300Sstevel@tonic-gate 28310Sstevel@tonic-gate if (free_pages == 0) 28320Sstevel@tonic-gate return; 28330Sstevel@tonic-gate if (swap_in_range(vp, off, len)) 28340Sstevel@tonic-gate return; 28350Sstevel@tonic-gate 28360Sstevel@tonic-gate for (; off < eoff; off += PAGESIZE) { 28370Sstevel@tonic-gate 28380Sstevel@tonic-gate /* 28390Sstevel@tonic-gate * find the page using a fast, but inexact search. It'll be OK 28400Sstevel@tonic-gate * if a few pages slip through the cracks here. 28410Sstevel@tonic-gate */ 28420Sstevel@tonic-gate pp = page_exists(vp, off); 28430Sstevel@tonic-gate 28440Sstevel@tonic-gate /* 28450Sstevel@tonic-gate * If we didn't find the page (it may not exist), the page 28460Sstevel@tonic-gate * is free, looks still in use (shared), or we can't lock it, 28470Sstevel@tonic-gate * just give up. 28480Sstevel@tonic-gate */ 28490Sstevel@tonic-gate if (pp == NULL || 28500Sstevel@tonic-gate PP_ISFREE(pp) || 28510Sstevel@tonic-gate page_share_cnt(pp) > 0 || 28520Sstevel@tonic-gate !page_trylock(pp, SE_EXCL)) 28530Sstevel@tonic-gate continue; 28540Sstevel@tonic-gate 28550Sstevel@tonic-gate /* 28560Sstevel@tonic-gate * Once we have locked pp, verify that it's still the 28570Sstevel@tonic-gate * correct page and not already free 28580Sstevel@tonic-gate */ 28590Sstevel@tonic-gate ASSERT(PAGE_LOCKED_SE(pp, SE_EXCL)); 28600Sstevel@tonic-gate if (pp->p_vnode != vp || pp->p_offset != off || PP_ISFREE(pp)) { 28610Sstevel@tonic-gate page_unlock(pp); 28620Sstevel@tonic-gate continue; 28630Sstevel@tonic-gate } 28640Sstevel@tonic-gate 28650Sstevel@tonic-gate /* 28660Sstevel@tonic-gate * try to release the page... 28670Sstevel@tonic-gate */ 28680Sstevel@tonic-gate (void) page_release(pp, 1); 28690Sstevel@tonic-gate } 28700Sstevel@tonic-gate } 28710Sstevel@tonic-gate 28720Sstevel@tonic-gate /* 28730Sstevel@tonic-gate * Reclaim the given page from the free list. 28743559Smec * If pp is part of a large pages, only the given constituent page is reclaimed 28753559Smec * and the large page it belonged to will be demoted. This can only happen 28763559Smec * if the page is not on the cachelist. 28773559Smec * 28780Sstevel@tonic-gate * Returns 1 on success or 0 on failure. 28790Sstevel@tonic-gate * 28800Sstevel@tonic-gate * The page is unlocked if it can't be reclaimed (when freemem == 0). 28810Sstevel@tonic-gate * If `lock' is non-null, it will be dropped and re-acquired if 28820Sstevel@tonic-gate * the routine must wait while freemem is 0. 28830Sstevel@tonic-gate * 28840Sstevel@tonic-gate * As it turns out, boot_getpages() does this. It picks a page, 28850Sstevel@tonic-gate * based on where OBP mapped in some address, gets its pfn, searches 28860Sstevel@tonic-gate * the memsegs, locks the page, then pulls it off the free list! 28870Sstevel@tonic-gate */ 28880Sstevel@tonic-gate int 28890Sstevel@tonic-gate page_reclaim(page_t *pp, kmutex_t *lock) 28900Sstevel@tonic-gate { 28910Sstevel@tonic-gate struct pcf *p; 28920Sstevel@tonic-gate struct cpu *cpup; 28933559Smec int enough; 28940Sstevel@tonic-gate uint_t i; 28950Sstevel@tonic-gate 28960Sstevel@tonic-gate ASSERT(lock != NULL ? MUTEX_HELD(lock) : 1); 28970Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp) && PP_ISFREE(pp)); 2898917Selowe 28990Sstevel@tonic-gate /* 29000Sstevel@tonic-gate * If `freemem' is 0, we cannot reclaim this page from the 29010Sstevel@tonic-gate * freelist, so release every lock we might hold: the page, 29020Sstevel@tonic-gate * and the `lock' before blocking. 29030Sstevel@tonic-gate * 29040Sstevel@tonic-gate * The only way `freemem' can become 0 while there are pages 29050Sstevel@tonic-gate * marked free (have their p->p_free bit set) is when the 29060Sstevel@tonic-gate * system is low on memory and doing a page_create(). In 29070Sstevel@tonic-gate * order to guarantee that once page_create() starts acquiring 29080Sstevel@tonic-gate * pages it will be able to get all that it needs since `freemem' 29090Sstevel@tonic-gate * was decreased by the requested amount. So, we need to release 29100Sstevel@tonic-gate * this page, and let page_create() have it. 29110Sstevel@tonic-gate * 29120Sstevel@tonic-gate * Since `freemem' being zero is not supposed to happen, just 29130Sstevel@tonic-gate * use the usual hash stuff as a starting point. If that bucket 29140Sstevel@tonic-gate * is empty, then assume the worst, and start at the beginning 29150Sstevel@tonic-gate * of the pcf array. If we always start at the beginning 29160Sstevel@tonic-gate * when acquiring more than one pcf lock, there won't be any 29170Sstevel@tonic-gate * deadlock problems. 29180Sstevel@tonic-gate */ 29190Sstevel@tonic-gate 29200Sstevel@tonic-gate /* TODO: Do we need to test kcage_freemem if PG_NORELOC(pp)? */ 29210Sstevel@tonic-gate 29223559Smec if (freemem <= throttlefree && !page_create_throttle(1l, 0)) { 29230Sstevel@tonic-gate pcf_acquire_all(); 29240Sstevel@tonic-gate goto page_reclaim_nomem; 29250Sstevel@tonic-gate } 29260Sstevel@tonic-gate 29276880Sdv142724 enough = pcf_decrement_bucket(1); 29283559Smec 29293559Smec if (!enough) { 29300Sstevel@tonic-gate VM_STAT_ADD(page_reclaim_zero); 29310Sstevel@tonic-gate /* 29320Sstevel@tonic-gate * Check again. Its possible that some other thread 29330Sstevel@tonic-gate * could have been right behind us, and added one 29340Sstevel@tonic-gate * to a list somewhere. Acquire each of the pcf locks 29350Sstevel@tonic-gate * until we find a page. 29360Sstevel@tonic-gate */ 29370Sstevel@tonic-gate p = pcf; 29386880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 29390Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 29403559Smec if (p->pcf_count >= 1) { 29413559Smec p->pcf_count -= 1; 29423559Smec enough = 1; 29433559Smec break; 29440Sstevel@tonic-gate } 29450Sstevel@tonic-gate p++; 29460Sstevel@tonic-gate } 29470Sstevel@tonic-gate 29483559Smec if (!enough) { 29490Sstevel@tonic-gate page_reclaim_nomem: 29500Sstevel@tonic-gate /* 29510Sstevel@tonic-gate * We really can't have page `pp'. 29520Sstevel@tonic-gate * Time for the no-memory dance with 29530Sstevel@tonic-gate * page_free(). This is just like 29540Sstevel@tonic-gate * page_create_wait(). Plus the added 29550Sstevel@tonic-gate * attraction of releasing whatever mutex 29560Sstevel@tonic-gate * we held when we were called with in `lock'. 29570Sstevel@tonic-gate * Page_unlock() will wakeup any thread 29580Sstevel@tonic-gate * waiting around for this page. 29590Sstevel@tonic-gate */ 29600Sstevel@tonic-gate if (lock) { 29610Sstevel@tonic-gate VM_STAT_ADD(page_reclaim_zero_locked); 29620Sstevel@tonic-gate mutex_exit(lock); 29630Sstevel@tonic-gate } 29640Sstevel@tonic-gate page_unlock(pp); 29650Sstevel@tonic-gate 29660Sstevel@tonic-gate /* 29670Sstevel@tonic-gate * get this before we drop all the pcf locks. 29680Sstevel@tonic-gate */ 29690Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 29700Sstevel@tonic-gate 29710Sstevel@tonic-gate p = pcf; 29726880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 29730Sstevel@tonic-gate p->pcf_wait++; 29740Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 29750Sstevel@tonic-gate p++; 29760Sstevel@tonic-gate } 29770Sstevel@tonic-gate 29780Sstevel@tonic-gate freemem_wait++; 29790Sstevel@tonic-gate cv_wait(&freemem_cv, &new_freemem_lock); 29800Sstevel@tonic-gate freemem_wait--; 29810Sstevel@tonic-gate 29820Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 29830Sstevel@tonic-gate 29840Sstevel@tonic-gate if (lock) { 29850Sstevel@tonic-gate mutex_enter(lock); 29860Sstevel@tonic-gate } 29870Sstevel@tonic-gate return (0); 29880Sstevel@tonic-gate } 29890Sstevel@tonic-gate 29900Sstevel@tonic-gate /* 29910Sstevel@tonic-gate * The pcf accounting has been done, 29920Sstevel@tonic-gate * though none of the pcf_wait flags have been set, 29930Sstevel@tonic-gate * drop the locks and continue on. 29940Sstevel@tonic-gate */ 29950Sstevel@tonic-gate while (p >= pcf) { 29960Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 29970Sstevel@tonic-gate p--; 29980Sstevel@tonic-gate } 29990Sstevel@tonic-gate } 30000Sstevel@tonic-gate 30010Sstevel@tonic-gate /* 30020Sstevel@tonic-gate * freemem is not protected by any lock. Thus, we cannot 30030Sstevel@tonic-gate * have any assertion containing freemem here. 30040Sstevel@tonic-gate */ 30053559Smec freemem -= 1; 30060Sstevel@tonic-gate 30070Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_reclaim); 30083559Smec 30093559Smec /* 30103559Smec * page_list_sub will handle the case where pp is a large page. 30113559Smec * It's possible that the page was promoted while on the freelist 30123559Smec */ 30130Sstevel@tonic-gate if (PP_ISAGED(pp)) { 30143559Smec page_list_sub(pp, PG_FREE_LIST); 30150Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_FREE, 30160Sstevel@tonic-gate "page_reclaim_free:pp %p", pp); 30170Sstevel@tonic-gate } else { 30180Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST); 30190Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_CACHE, 30200Sstevel@tonic-gate "page_reclaim_cache:pp %p", pp); 30210Sstevel@tonic-gate } 30220Sstevel@tonic-gate 30230Sstevel@tonic-gate /* 30240Sstevel@tonic-gate * clear the p_free & p_age bits since this page is no longer 30250Sstevel@tonic-gate * on the free list. Notice that there was a brief time where 30260Sstevel@tonic-gate * a page is marked as free, but is not on the list. 30270Sstevel@tonic-gate * 30280Sstevel@tonic-gate * Set the reference bit to protect against immediate pageout. 30290Sstevel@tonic-gate */ 30303559Smec PP_CLRFREE(pp); 30313559Smec PP_CLRAGED(pp); 30323559Smec page_set_props(pp, P_REF); 30330Sstevel@tonic-gate 30340Sstevel@tonic-gate CPU_STATS_ENTER_K(); 30350Sstevel@tonic-gate cpup = CPU; /* get cpup now that CPU cannot change */ 30360Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgrec, 1); 30370Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgfrec, 1); 30380Sstevel@tonic-gate CPU_STATS_EXIT_K(); 30393559Smec ASSERT(pp->p_szc == 0); 30400Sstevel@tonic-gate 30410Sstevel@tonic-gate return (1); 30420Sstevel@tonic-gate } 30430Sstevel@tonic-gate 30440Sstevel@tonic-gate /* 30450Sstevel@tonic-gate * Destroy identity of the page and put it back on 30460Sstevel@tonic-gate * the page free list. Assumes that the caller has 30470Sstevel@tonic-gate * acquired the "exclusive" lock on the page. 30480Sstevel@tonic-gate */ 30490Sstevel@tonic-gate void 30500Sstevel@tonic-gate page_destroy(page_t *pp, int dontfree) 30510Sstevel@tonic-gate { 30520Sstevel@tonic-gate ASSERT((PAGE_EXCL(pp) && 30530Sstevel@tonic-gate !page_iolock_assert(pp)) || panicstr); 30542414Saguzovsk ASSERT(pp->p_slckcnt == 0 || panicstr); 30550Sstevel@tonic-gate 30560Sstevel@tonic-gate if (pp->p_szc != 0) { 30570Sstevel@tonic-gate if (pp->p_vnode == NULL || IS_SWAPFSVP(pp->p_vnode) || 30583290Sjohansen PP_ISKAS(pp)) { 30590Sstevel@tonic-gate panic("page_destroy: anon or kernel or no vnode " 30600Sstevel@tonic-gate "large page %p", (void *)pp); 30610Sstevel@tonic-gate } 30620Sstevel@tonic-gate page_demote_vp_pages(pp); 30630Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 30640Sstevel@tonic-gate } 30650Sstevel@tonic-gate 30660Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_DESTROY, "page_destroy:pp %p", pp); 30670Sstevel@tonic-gate 30680Sstevel@tonic-gate /* 30690Sstevel@tonic-gate * Unload translations, if any, then hash out the 30700Sstevel@tonic-gate * page to erase its identity. 30710Sstevel@tonic-gate */ 30720Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 30730Sstevel@tonic-gate page_hashout(pp, NULL); 30740Sstevel@tonic-gate 30750Sstevel@tonic-gate if (!dontfree) { 30760Sstevel@tonic-gate /* 30770Sstevel@tonic-gate * Acquire the "freemem_lock" for availrmem. 30780Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt 30790Sstevel@tonic-gate * and cowcnt since the page has an "exclusive" lock. 3080*9975SGangadhar.M@Sun.COM * We are doing a modified version of page_pp_unlock here. 30810Sstevel@tonic-gate */ 30820Sstevel@tonic-gate if ((pp->p_lckcnt != 0) || (pp->p_cowcnt != 0)) { 30830Sstevel@tonic-gate mutex_enter(&freemem_lock); 30840Sstevel@tonic-gate if (pp->p_lckcnt != 0) { 30850Sstevel@tonic-gate availrmem++; 3086*9975SGangadhar.M@Sun.COM pages_locked--; 30870Sstevel@tonic-gate pp->p_lckcnt = 0; 30880Sstevel@tonic-gate } 30890Sstevel@tonic-gate if (pp->p_cowcnt != 0) { 30900Sstevel@tonic-gate availrmem += pp->p_cowcnt; 3091*9975SGangadhar.M@Sun.COM pages_locked -= pp->p_cowcnt; 30920Sstevel@tonic-gate pp->p_cowcnt = 0; 30930Sstevel@tonic-gate } 30940Sstevel@tonic-gate mutex_exit(&freemem_lock); 30950Sstevel@tonic-gate } 30960Sstevel@tonic-gate /* 30970Sstevel@tonic-gate * Put the page on the "free" list. 30980Sstevel@tonic-gate */ 30990Sstevel@tonic-gate page_free(pp, 0); 31000Sstevel@tonic-gate } 31010Sstevel@tonic-gate } 31020Sstevel@tonic-gate 31030Sstevel@tonic-gate void 31040Sstevel@tonic-gate page_destroy_pages(page_t *pp) 31050Sstevel@tonic-gate { 31060Sstevel@tonic-gate 31070Sstevel@tonic-gate page_t *tpp, *rootpp = NULL; 31080Sstevel@tonic-gate pgcnt_t pgcnt = page_get_pagecnt(pp->p_szc); 31090Sstevel@tonic-gate pgcnt_t i, pglcks = 0; 31100Sstevel@tonic-gate uint_t szc = pp->p_szc; 31110Sstevel@tonic-gate 31120Sstevel@tonic-gate ASSERT(pp->p_szc != 0 && pp->p_szc < page_num_pagesizes()); 31130Sstevel@tonic-gate 31140Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_destroy_pages); 31150Sstevel@tonic-gate 31160Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_DESTROY, "page_destroy_pages:pp %p", pp); 31170Sstevel@tonic-gate 31180Sstevel@tonic-gate if ((page_pptonum(pp) & (pgcnt - 1)) != 0) { 31190Sstevel@tonic-gate panic("page_destroy_pages: not root page %p", (void *)pp); 31200Sstevel@tonic-gate /*NOTREACHED*/ 31210Sstevel@tonic-gate } 31220Sstevel@tonic-gate 3123414Skchow for (i = 0, tpp = pp; i < pgcnt; i++, tpp++) { 31240Sstevel@tonic-gate ASSERT((PAGE_EXCL(tpp) && 31250Sstevel@tonic-gate !page_iolock_assert(tpp)) || panicstr); 31262414Saguzovsk ASSERT(tpp->p_slckcnt == 0 || panicstr); 31270Sstevel@tonic-gate (void) hat_pageunload(tpp, HAT_FORCE_PGUNLOAD); 31280Sstevel@tonic-gate page_hashout(tpp, NULL); 31290Sstevel@tonic-gate ASSERT(tpp->p_offset == (u_offset_t)-1); 31300Sstevel@tonic-gate if (tpp->p_lckcnt != 0) { 31310Sstevel@tonic-gate pglcks++; 31320Sstevel@tonic-gate tpp->p_lckcnt = 0; 31330Sstevel@tonic-gate } else if (tpp->p_cowcnt != 0) { 31340Sstevel@tonic-gate pglcks += tpp->p_cowcnt; 31350Sstevel@tonic-gate tpp->p_cowcnt = 0; 31360Sstevel@tonic-gate } 31370Sstevel@tonic-gate ASSERT(!hat_page_getshare(tpp)); 31380Sstevel@tonic-gate ASSERT(tpp->p_vnode == NULL); 31390Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 31400Sstevel@tonic-gate 31410Sstevel@tonic-gate PP_SETFREE(tpp); 31427718SJason.Beloro@Sun.COM page_clr_all_props(tpp, 0); 31430Sstevel@tonic-gate PP_SETAGED(tpp); 31440Sstevel@tonic-gate ASSERT(tpp->p_next == tpp); 31450Sstevel@tonic-gate ASSERT(tpp->p_prev == tpp); 31460Sstevel@tonic-gate page_list_concat(&rootpp, &tpp); 31470Sstevel@tonic-gate } 31480Sstevel@tonic-gate 31490Sstevel@tonic-gate ASSERT(rootpp == pp); 31500Sstevel@tonic-gate if (pglcks != 0) { 31510Sstevel@tonic-gate mutex_enter(&freemem_lock); 31520Sstevel@tonic-gate availrmem += pglcks; 31530Sstevel@tonic-gate mutex_exit(&freemem_lock); 31540Sstevel@tonic-gate } 31550Sstevel@tonic-gate 31560Sstevel@tonic-gate page_list_add_pages(rootpp, 0); 31570Sstevel@tonic-gate page_create_putback(pgcnt); 31580Sstevel@tonic-gate } 31590Sstevel@tonic-gate 31600Sstevel@tonic-gate /* 31610Sstevel@tonic-gate * Similar to page_destroy(), but destroys pages which are 31620Sstevel@tonic-gate * locked and known to be on the page free list. Since 31630Sstevel@tonic-gate * the page is known to be free and locked, no one can access 31640Sstevel@tonic-gate * it. 31650Sstevel@tonic-gate * 31660Sstevel@tonic-gate * Also, the number of free pages does not change. 31670Sstevel@tonic-gate */ 31680Sstevel@tonic-gate void 31690Sstevel@tonic-gate page_destroy_free(page_t *pp) 31700Sstevel@tonic-gate { 31710Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 31720Sstevel@tonic-gate ASSERT(PP_ISFREE(pp)); 31730Sstevel@tonic-gate ASSERT(pp->p_vnode); 31740Sstevel@tonic-gate ASSERT(hat_page_getattr(pp, P_MOD | P_REF | P_RO) == 0); 31750Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 31760Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0); 31770Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 31780Sstevel@tonic-gate 31790Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_destroy_free); 31800Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST); 31810Sstevel@tonic-gate 31820Sstevel@tonic-gate page_hashout(pp, NULL); 31830Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL); 31840Sstevel@tonic-gate ASSERT(pp->p_offset == (u_offset_t)-1); 31850Sstevel@tonic-gate ASSERT(pp->p_hash == NULL); 31860Sstevel@tonic-gate 31870Sstevel@tonic-gate PP_SETAGED(pp); 31880Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 31890Sstevel@tonic-gate page_unlock(pp); 31900Sstevel@tonic-gate 31910Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 31920Sstevel@tonic-gate if (freemem_wait) { 31930Sstevel@tonic-gate cv_signal(&freemem_cv); 31940Sstevel@tonic-gate } 31950Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 31960Sstevel@tonic-gate } 31970Sstevel@tonic-gate 31980Sstevel@tonic-gate /* 31990Sstevel@tonic-gate * Rename the page "opp" to have an identity specified 32000Sstevel@tonic-gate * by [vp, off]. If a page already exists with this name 32010Sstevel@tonic-gate * it is locked and destroyed. Note that the page's 32020Sstevel@tonic-gate * translations are not unloaded during the rename. 32030Sstevel@tonic-gate * 32040Sstevel@tonic-gate * This routine is used by the anon layer to "steal" the 32050Sstevel@tonic-gate * original page and is not unlike destroying a page and 32060Sstevel@tonic-gate * creating a new page using the same page frame. 32070Sstevel@tonic-gate * 32080Sstevel@tonic-gate * XXX -- Could deadlock if caller 1 tries to rename A to B while 32090Sstevel@tonic-gate * caller 2 tries to rename B to A. 32100Sstevel@tonic-gate */ 32110Sstevel@tonic-gate void 32120Sstevel@tonic-gate page_rename(page_t *opp, vnode_t *vp, u_offset_t off) 32130Sstevel@tonic-gate { 32140Sstevel@tonic-gate page_t *pp; 32150Sstevel@tonic-gate int olckcnt = 0; 32160Sstevel@tonic-gate int ocowcnt = 0; 32170Sstevel@tonic-gate kmutex_t *phm; 32180Sstevel@tonic-gate ulong_t index; 32190Sstevel@tonic-gate 32200Sstevel@tonic-gate ASSERT(PAGE_EXCL(opp) && !page_iolock_assert(opp)); 32210Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 32220Sstevel@tonic-gate ASSERT(PP_ISFREE(opp) == 0); 32230Sstevel@tonic-gate 32240Sstevel@tonic-gate VM_STAT_ADD(page_rename_count); 32250Sstevel@tonic-gate 32260Sstevel@tonic-gate TRACE_3(TR_FAC_VM, TR_PAGE_RENAME, 32273559Smec "page rename:pp %p vp %p off %llx", opp, vp, off); 32280Sstevel@tonic-gate 322963Saguzovsk /* 323063Saguzovsk * CacheFS may call page_rename for a large NFS page 323163Saguzovsk * when both CacheFS and NFS mount points are used 323263Saguzovsk * by applications. Demote this large page before 323363Saguzovsk * renaming it, to ensure that there are no "partial" 323463Saguzovsk * large pages left lying around. 323563Saguzovsk */ 323663Saguzovsk if (opp->p_szc != 0) { 323763Saguzovsk vnode_t *ovp = opp->p_vnode; 323863Saguzovsk ASSERT(ovp != NULL); 323963Saguzovsk ASSERT(!IS_SWAPFSVP(ovp)); 32403290Sjohansen ASSERT(!VN_ISKAS(ovp)); 324163Saguzovsk page_demote_vp_pages(opp); 324263Saguzovsk ASSERT(opp->p_szc == 0); 324363Saguzovsk } 324463Saguzovsk 32450Sstevel@tonic-gate page_hashout(opp, NULL); 32460Sstevel@tonic-gate PP_CLRAGED(opp); 32470Sstevel@tonic-gate 32480Sstevel@tonic-gate /* 32490Sstevel@tonic-gate * Acquire the appropriate page hash lock, since 32500Sstevel@tonic-gate * we're going to rename the page. 32510Sstevel@tonic-gate */ 32520Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 32530Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 32540Sstevel@tonic-gate mutex_enter(phm); 32550Sstevel@tonic-gate top: 32560Sstevel@tonic-gate /* 32570Sstevel@tonic-gate * Look for an existing page with this name and destroy it if found. 32580Sstevel@tonic-gate * By holding the page hash lock all the way to the page_hashin() 32590Sstevel@tonic-gate * call, we are assured that no page can be created with this 32600Sstevel@tonic-gate * identity. In the case when the phm lock is dropped to undo any 32610Sstevel@tonic-gate * hat layer mappings, the existing page is held with an "exclusive" 32620Sstevel@tonic-gate * lock, again preventing another page from being created with 32630Sstevel@tonic-gate * this identity. 32640Sstevel@tonic-gate */ 32650Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 32660Sstevel@tonic-gate if (pp != NULL) { 32670Sstevel@tonic-gate VM_STAT_ADD(page_rename_exists); 32680Sstevel@tonic-gate 32690Sstevel@tonic-gate /* 32700Sstevel@tonic-gate * As it turns out, this is one of only two places where 32710Sstevel@tonic-gate * page_lock() needs to hold the passed in lock in the 32720Sstevel@tonic-gate * successful case. In all of the others, the lock could 32730Sstevel@tonic-gate * be dropped as soon as the attempt is made to lock 32740Sstevel@tonic-gate * the page. It is tempting to add yet another arguement, 32750Sstevel@tonic-gate * PL_KEEP or PL_DROP, to let page_lock know what to do. 32760Sstevel@tonic-gate */ 32770Sstevel@tonic-gate if (!page_lock(pp, SE_EXCL, phm, P_RECLAIM)) { 32780Sstevel@tonic-gate /* 32790Sstevel@tonic-gate * Went to sleep because the page could not 32800Sstevel@tonic-gate * be locked. We were woken up when the page 32810Sstevel@tonic-gate * was unlocked, or when the page was destroyed. 32820Sstevel@tonic-gate * In either case, `phm' was dropped while we 32830Sstevel@tonic-gate * slept. Hence we should not just roar through 32840Sstevel@tonic-gate * this loop. 32850Sstevel@tonic-gate */ 32860Sstevel@tonic-gate goto top; 32870Sstevel@tonic-gate } 32880Sstevel@tonic-gate 328963Saguzovsk /* 329063Saguzovsk * If an existing page is a large page, then demote 329163Saguzovsk * it to ensure that no "partial" large pages are 329263Saguzovsk * "created" after page_rename. An existing page 329363Saguzovsk * can be a CacheFS page, and can't belong to swapfs. 329463Saguzovsk */ 32950Sstevel@tonic-gate if (hat_page_is_mapped(pp)) { 32960Sstevel@tonic-gate /* 32970Sstevel@tonic-gate * Unload translations. Since we hold the 32980Sstevel@tonic-gate * exclusive lock on this page, the page 32990Sstevel@tonic-gate * can not be changed while we drop phm. 33000Sstevel@tonic-gate * This is also not a lock protocol violation, 33010Sstevel@tonic-gate * but rather the proper way to do things. 33020Sstevel@tonic-gate */ 33030Sstevel@tonic-gate mutex_exit(phm); 33040Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 330563Saguzovsk if (pp->p_szc != 0) { 330663Saguzovsk ASSERT(!IS_SWAPFSVP(vp)); 33073290Sjohansen ASSERT(!VN_ISKAS(vp)); 330863Saguzovsk page_demote_vp_pages(pp); 330963Saguzovsk ASSERT(pp->p_szc == 0); 331063Saguzovsk } 331163Saguzovsk mutex_enter(phm); 331263Saguzovsk } else if (pp->p_szc != 0) { 331363Saguzovsk ASSERT(!IS_SWAPFSVP(vp)); 33143290Sjohansen ASSERT(!VN_ISKAS(vp)); 331563Saguzovsk mutex_exit(phm); 331663Saguzovsk page_demote_vp_pages(pp); 331763Saguzovsk ASSERT(pp->p_szc == 0); 33180Sstevel@tonic-gate mutex_enter(phm); 33190Sstevel@tonic-gate } 33200Sstevel@tonic-gate page_hashout(pp, phm); 33210Sstevel@tonic-gate } 33220Sstevel@tonic-gate /* 33230Sstevel@tonic-gate * Hash in the page with the new identity. 33240Sstevel@tonic-gate */ 33250Sstevel@tonic-gate if (!page_hashin(opp, vp, off, phm)) { 33260Sstevel@tonic-gate /* 33270Sstevel@tonic-gate * We were holding phm while we searched for [vp, off] 33280Sstevel@tonic-gate * and only dropped phm if we found and locked a page. 33290Sstevel@tonic-gate * If we can't create this page now, then some thing 33300Sstevel@tonic-gate * is really broken. 33310Sstevel@tonic-gate */ 33320Sstevel@tonic-gate panic("page_rename: Can't hash in page: %p", (void *)pp); 33330Sstevel@tonic-gate /*NOTREACHED*/ 33340Sstevel@tonic-gate } 33350Sstevel@tonic-gate 33360Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 33370Sstevel@tonic-gate mutex_exit(phm); 33380Sstevel@tonic-gate 33390Sstevel@tonic-gate /* 33400Sstevel@tonic-gate * Now that we have dropped phm, lets get around to finishing up 33410Sstevel@tonic-gate * with pp. 33420Sstevel@tonic-gate */ 33430Sstevel@tonic-gate if (pp != NULL) { 33440Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 33450Sstevel@tonic-gate /* for now large pages should not end up here */ 33460Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 33470Sstevel@tonic-gate /* 33480Sstevel@tonic-gate * Save the locks for transfer to the new page and then 33490Sstevel@tonic-gate * clear them so page_free doesn't think they're important. 33500Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt and 33510Sstevel@tonic-gate * cowcnt since the page has an "exclusive" lock. 33520Sstevel@tonic-gate */ 33530Sstevel@tonic-gate olckcnt = pp->p_lckcnt; 33540Sstevel@tonic-gate ocowcnt = pp->p_cowcnt; 33550Sstevel@tonic-gate pp->p_lckcnt = pp->p_cowcnt = 0; 33560Sstevel@tonic-gate 33570Sstevel@tonic-gate /* 33580Sstevel@tonic-gate * Put the page on the "free" list after we drop 33590Sstevel@tonic-gate * the lock. The less work under the lock the better. 33600Sstevel@tonic-gate */ 33610Sstevel@tonic-gate /*LINTED: constant in conditional context*/ 33620Sstevel@tonic-gate VN_DISPOSE(pp, B_FREE, 0, kcred); 33630Sstevel@tonic-gate } 33640Sstevel@tonic-gate 33650Sstevel@tonic-gate /* 33660Sstevel@tonic-gate * Transfer the lock count from the old page (if any). 33670Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt and 33680Sstevel@tonic-gate * cowcnt since the page has an "exclusive" lock. 33690Sstevel@tonic-gate */ 33700Sstevel@tonic-gate opp->p_lckcnt += olckcnt; 33710Sstevel@tonic-gate opp->p_cowcnt += ocowcnt; 33720Sstevel@tonic-gate } 33730Sstevel@tonic-gate 33740Sstevel@tonic-gate /* 33750Sstevel@tonic-gate * low level routine to add page `pp' to the hash and vp chains for [vp, offset] 33760Sstevel@tonic-gate * 33770Sstevel@tonic-gate * Pages are normally inserted at the start of a vnode's v_pages list. 33780Sstevel@tonic-gate * If the vnode is VMODSORT and the page is modified, it goes at the end. 33790Sstevel@tonic-gate * This can happen when a modified page is relocated for DR. 33800Sstevel@tonic-gate * 33810Sstevel@tonic-gate * Returns 1 on success and 0 on failure. 33820Sstevel@tonic-gate */ 33830Sstevel@tonic-gate static int 33840Sstevel@tonic-gate page_do_hashin(page_t *pp, vnode_t *vp, u_offset_t offset) 33850Sstevel@tonic-gate { 33860Sstevel@tonic-gate page_t **listp; 33870Sstevel@tonic-gate page_t *tp; 33880Sstevel@tonic-gate ulong_t index; 33890Sstevel@tonic-gate 33900Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 33910Sstevel@tonic-gate ASSERT(vp != NULL); 33920Sstevel@tonic-gate ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 33930Sstevel@tonic-gate 33940Sstevel@tonic-gate /* 33950Sstevel@tonic-gate * Be sure to set these up before the page is inserted on the hash 33960Sstevel@tonic-gate * list. As soon as the page is placed on the list some other 33970Sstevel@tonic-gate * thread might get confused and wonder how this page could 33980Sstevel@tonic-gate * possibly hash to this list. 33990Sstevel@tonic-gate */ 34000Sstevel@tonic-gate pp->p_vnode = vp; 34010Sstevel@tonic-gate pp->p_offset = offset; 34020Sstevel@tonic-gate 34030Sstevel@tonic-gate /* 34040Sstevel@tonic-gate * record if this page is on a swap vnode 34050Sstevel@tonic-gate */ 34060Sstevel@tonic-gate if ((vp->v_flag & VISSWAP) != 0) 34070Sstevel@tonic-gate PP_SETSWAP(pp); 34080Sstevel@tonic-gate 34090Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, offset); 34100Sstevel@tonic-gate ASSERT(MUTEX_HELD(PAGE_HASH_MUTEX(index))); 34110Sstevel@tonic-gate listp = &page_hash[index]; 34120Sstevel@tonic-gate 34130Sstevel@tonic-gate /* 34140Sstevel@tonic-gate * If this page is already hashed in, fail this attempt to add it. 34150Sstevel@tonic-gate */ 34160Sstevel@tonic-gate for (tp = *listp; tp != NULL; tp = tp->p_hash) { 34170Sstevel@tonic-gate if (tp->p_vnode == vp && tp->p_offset == offset) { 34180Sstevel@tonic-gate pp->p_vnode = NULL; 34190Sstevel@tonic-gate pp->p_offset = (u_offset_t)(-1); 34200Sstevel@tonic-gate return (0); 34210Sstevel@tonic-gate } 34220Sstevel@tonic-gate } 34230Sstevel@tonic-gate pp->p_hash = *listp; 34240Sstevel@tonic-gate *listp = pp; 34250Sstevel@tonic-gate 34260Sstevel@tonic-gate /* 34270Sstevel@tonic-gate * Add the page to the vnode's list of pages 34280Sstevel@tonic-gate */ 34290Sstevel@tonic-gate if (vp->v_pages != NULL && IS_VMODSORT(vp) && hat_ismod(pp)) 34300Sstevel@tonic-gate listp = &vp->v_pages->p_vpprev->p_vpnext; 34310Sstevel@tonic-gate else 34320Sstevel@tonic-gate listp = &vp->v_pages; 34330Sstevel@tonic-gate 34340Sstevel@tonic-gate page_vpadd(listp, pp); 34350Sstevel@tonic-gate 34360Sstevel@tonic-gate return (1); 34370Sstevel@tonic-gate } 34380Sstevel@tonic-gate 34390Sstevel@tonic-gate /* 34400Sstevel@tonic-gate * Add page `pp' to both the hash and vp chains for [vp, offset]. 34410Sstevel@tonic-gate * 34420Sstevel@tonic-gate * Returns 1 on success and 0 on failure. 34430Sstevel@tonic-gate * If hold is passed in, it is not dropped. 34440Sstevel@tonic-gate */ 34450Sstevel@tonic-gate int 34460Sstevel@tonic-gate page_hashin(page_t *pp, vnode_t *vp, u_offset_t offset, kmutex_t *hold) 34470Sstevel@tonic-gate { 34480Sstevel@tonic-gate kmutex_t *phm = NULL; 34490Sstevel@tonic-gate kmutex_t *vphm; 34500Sstevel@tonic-gate int rc; 34510Sstevel@tonic-gate 34520Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 34538325SPrakash.Sangappa@Sun.COM ASSERT(pp->p_fsdata == 0 || panicstr); 34540Sstevel@tonic-gate 34550Sstevel@tonic-gate TRACE_3(TR_FAC_VM, TR_PAGE_HASHIN, 34563559Smec "page_hashin:pp %p vp %p offset %llx", 34573559Smec pp, vp, offset); 34580Sstevel@tonic-gate 34590Sstevel@tonic-gate VM_STAT_ADD(hashin_count); 34600Sstevel@tonic-gate 34610Sstevel@tonic-gate if (hold != NULL) 34620Sstevel@tonic-gate phm = hold; 34630Sstevel@tonic-gate else { 34640Sstevel@tonic-gate VM_STAT_ADD(hashin_not_held); 34650Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, offset)); 34660Sstevel@tonic-gate mutex_enter(phm); 34670Sstevel@tonic-gate } 34680Sstevel@tonic-gate 34690Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 34700Sstevel@tonic-gate mutex_enter(vphm); 34710Sstevel@tonic-gate rc = page_do_hashin(pp, vp, offset); 34720Sstevel@tonic-gate mutex_exit(vphm); 34730Sstevel@tonic-gate if (hold == NULL) 34740Sstevel@tonic-gate mutex_exit(phm); 34750Sstevel@tonic-gate if (rc == 0) 34760Sstevel@tonic-gate VM_STAT_ADD(hashin_already); 34770Sstevel@tonic-gate return (rc); 34780Sstevel@tonic-gate } 34790Sstevel@tonic-gate 34800Sstevel@tonic-gate /* 34810Sstevel@tonic-gate * Remove page ``pp'' from the hash and vp chains and remove vp association. 34820Sstevel@tonic-gate * All mutexes must be held 34830Sstevel@tonic-gate */ 34840Sstevel@tonic-gate static void 34850Sstevel@tonic-gate page_do_hashout(page_t *pp) 34860Sstevel@tonic-gate { 34870Sstevel@tonic-gate page_t **hpp; 34880Sstevel@tonic-gate page_t *hp; 34890Sstevel@tonic-gate vnode_t *vp = pp->p_vnode; 34900Sstevel@tonic-gate 34910Sstevel@tonic-gate ASSERT(vp != NULL); 34920Sstevel@tonic-gate ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 34930Sstevel@tonic-gate 34940Sstevel@tonic-gate /* 34950Sstevel@tonic-gate * First, take pp off of its hash chain. 34960Sstevel@tonic-gate */ 34970Sstevel@tonic-gate hpp = &page_hash[PAGE_HASH_FUNC(vp, pp->p_offset)]; 34980Sstevel@tonic-gate 34990Sstevel@tonic-gate for (;;) { 35000Sstevel@tonic-gate hp = *hpp; 35010Sstevel@tonic-gate if (hp == pp) 35020Sstevel@tonic-gate break; 35030Sstevel@tonic-gate if (hp == NULL) { 35040Sstevel@tonic-gate panic("page_do_hashout"); 35050Sstevel@tonic-gate /*NOTREACHED*/ 35060Sstevel@tonic-gate } 35070Sstevel@tonic-gate hpp = &hp->p_hash; 35080Sstevel@tonic-gate } 35090Sstevel@tonic-gate *hpp = pp->p_hash; 35100Sstevel@tonic-gate 35110Sstevel@tonic-gate /* 35120Sstevel@tonic-gate * Now remove it from its associated vnode. 35130Sstevel@tonic-gate */ 35140Sstevel@tonic-gate if (vp->v_pages) 35150Sstevel@tonic-gate page_vpsub(&vp->v_pages, pp); 35160Sstevel@tonic-gate 35170Sstevel@tonic-gate pp->p_hash = NULL; 35187718SJason.Beloro@Sun.COM page_clr_all_props(pp, 1); 35190Sstevel@tonic-gate PP_CLRSWAP(pp); 35200Sstevel@tonic-gate pp->p_vnode = NULL; 35210Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1; 35228325SPrakash.Sangappa@Sun.COM pp->p_fsdata = 0; 35230Sstevel@tonic-gate } 35240Sstevel@tonic-gate 35250Sstevel@tonic-gate /* 35260Sstevel@tonic-gate * Remove page ``pp'' from the hash and vp chains and remove vp association. 35270Sstevel@tonic-gate * 35280Sstevel@tonic-gate * When `phm' is non-NULL it contains the address of the mutex protecting the 35290Sstevel@tonic-gate * hash list pp is on. It is not dropped. 35300Sstevel@tonic-gate */ 35310Sstevel@tonic-gate void 35320Sstevel@tonic-gate page_hashout(page_t *pp, kmutex_t *phm) 35330Sstevel@tonic-gate { 35340Sstevel@tonic-gate vnode_t *vp; 35350Sstevel@tonic-gate ulong_t index; 35360Sstevel@tonic-gate kmutex_t *nphm; 35370Sstevel@tonic-gate kmutex_t *vphm; 35380Sstevel@tonic-gate kmutex_t *sep; 35390Sstevel@tonic-gate 35400Sstevel@tonic-gate ASSERT(phm != NULL ? MUTEX_HELD(phm) : 1); 35410Sstevel@tonic-gate ASSERT(pp->p_vnode != NULL); 35420Sstevel@tonic-gate ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 35430Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(pp->p_vnode))); 35440Sstevel@tonic-gate 35450Sstevel@tonic-gate vp = pp->p_vnode; 35460Sstevel@tonic-gate 35470Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_HASHOUT, 35483559Smec "page_hashout:pp %p vp %p", pp, vp); 35490Sstevel@tonic-gate 35500Sstevel@tonic-gate /* Kernel probe */ 35510Sstevel@tonic-gate TNF_PROBE_2(page_unmap, "vm pagefault", /* CSTYLED */, 35520Sstevel@tonic-gate tnf_opaque, vnode, vp, 35530Sstevel@tonic-gate tnf_offset, offset, pp->p_offset); 35540Sstevel@tonic-gate 35550Sstevel@tonic-gate /* 35560Sstevel@tonic-gate * 35570Sstevel@tonic-gate */ 35580Sstevel@tonic-gate VM_STAT_ADD(hashout_count); 35590Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, pp->p_offset); 35600Sstevel@tonic-gate if (phm == NULL) { 35610Sstevel@tonic-gate VM_STAT_ADD(hashout_not_held); 35620Sstevel@tonic-gate nphm = PAGE_HASH_MUTEX(index); 35630Sstevel@tonic-gate mutex_enter(nphm); 35640Sstevel@tonic-gate } 35650Sstevel@tonic-gate ASSERT(phm ? phm == PAGE_HASH_MUTEX(index) : 1); 35660Sstevel@tonic-gate 35670Sstevel@tonic-gate 35680Sstevel@tonic-gate /* 35690Sstevel@tonic-gate * grab page vnode mutex and remove it... 35700Sstevel@tonic-gate */ 35710Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 35720Sstevel@tonic-gate mutex_enter(vphm); 35730Sstevel@tonic-gate 35740Sstevel@tonic-gate page_do_hashout(pp); 35750Sstevel@tonic-gate 35760Sstevel@tonic-gate mutex_exit(vphm); 35770Sstevel@tonic-gate if (phm == NULL) 35780Sstevel@tonic-gate mutex_exit(nphm); 35790Sstevel@tonic-gate 35800Sstevel@tonic-gate /* 35810Sstevel@tonic-gate * Wake up processes waiting for this page. The page's 35820Sstevel@tonic-gate * identity has been changed, and is probably not the 35830Sstevel@tonic-gate * desired page any longer. 35840Sstevel@tonic-gate */ 35850Sstevel@tonic-gate sep = page_se_mutex(pp); 35860Sstevel@tonic-gate mutex_enter(sep); 3587800Sstans pp->p_selock &= ~SE_EWANTED; 35880Sstevel@tonic-gate if (CV_HAS_WAITERS(&pp->p_cv)) 35890Sstevel@tonic-gate cv_broadcast(&pp->p_cv); 35900Sstevel@tonic-gate mutex_exit(sep); 35910Sstevel@tonic-gate } 35920Sstevel@tonic-gate 35930Sstevel@tonic-gate /* 35940Sstevel@tonic-gate * Add the page to the front of a linked list of pages 35950Sstevel@tonic-gate * using the p_next & p_prev pointers for the list. 35960Sstevel@tonic-gate * The caller is responsible for protecting the list pointers. 35970Sstevel@tonic-gate */ 35980Sstevel@tonic-gate void 35990Sstevel@tonic-gate page_add(page_t **ppp, page_t *pp) 36000Sstevel@tonic-gate { 36010Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp) || (PAGE_SHARED(pp) && page_iolock_assert(pp))); 36020Sstevel@tonic-gate 36030Sstevel@tonic-gate page_add_common(ppp, pp); 36040Sstevel@tonic-gate } 36050Sstevel@tonic-gate 36060Sstevel@tonic-gate 36070Sstevel@tonic-gate 36080Sstevel@tonic-gate /* 36090Sstevel@tonic-gate * Common code for page_add() and mach_page_add() 36100Sstevel@tonic-gate */ 36110Sstevel@tonic-gate void 36120Sstevel@tonic-gate page_add_common(page_t **ppp, page_t *pp) 36130Sstevel@tonic-gate { 36140Sstevel@tonic-gate if (*ppp == NULL) { 36150Sstevel@tonic-gate pp->p_next = pp->p_prev = pp; 36160Sstevel@tonic-gate } else { 36170Sstevel@tonic-gate pp->p_next = *ppp; 36180Sstevel@tonic-gate pp->p_prev = (*ppp)->p_prev; 36190Sstevel@tonic-gate (*ppp)->p_prev = pp; 36200Sstevel@tonic-gate pp->p_prev->p_next = pp; 36210Sstevel@tonic-gate } 36220Sstevel@tonic-gate *ppp = pp; 36230Sstevel@tonic-gate } 36240Sstevel@tonic-gate 36250Sstevel@tonic-gate 36260Sstevel@tonic-gate /* 36270Sstevel@tonic-gate * Remove this page from a linked list of pages 36280Sstevel@tonic-gate * using the p_next & p_prev pointers for the list. 36290Sstevel@tonic-gate * 36300Sstevel@tonic-gate * The caller is responsible for protecting the list pointers. 36310Sstevel@tonic-gate */ 36320Sstevel@tonic-gate void 36330Sstevel@tonic-gate page_sub(page_t **ppp, page_t *pp) 36340Sstevel@tonic-gate { 36350Sstevel@tonic-gate ASSERT((PP_ISFREE(pp)) ? 1 : 36360Sstevel@tonic-gate (PAGE_EXCL(pp)) || (PAGE_SHARED(pp) && page_iolock_assert(pp))); 36370Sstevel@tonic-gate 36380Sstevel@tonic-gate if (*ppp == NULL || pp == NULL) { 36390Sstevel@tonic-gate panic("page_sub: bad arg(s): pp %p, *ppp %p", 36400Sstevel@tonic-gate (void *)pp, (void *)(*ppp)); 36410Sstevel@tonic-gate /*NOTREACHED*/ 36420Sstevel@tonic-gate } 36430Sstevel@tonic-gate 36440Sstevel@tonic-gate page_sub_common(ppp, pp); 36450Sstevel@tonic-gate } 36460Sstevel@tonic-gate 36470Sstevel@tonic-gate 36480Sstevel@tonic-gate /* 36490Sstevel@tonic-gate * Common code for page_sub() and mach_page_sub() 36500Sstevel@tonic-gate */ 36510Sstevel@tonic-gate void 36520Sstevel@tonic-gate page_sub_common(page_t **ppp, page_t *pp) 36530Sstevel@tonic-gate { 36540Sstevel@tonic-gate if (*ppp == pp) 36550Sstevel@tonic-gate *ppp = pp->p_next; /* go to next page */ 36560Sstevel@tonic-gate 36570Sstevel@tonic-gate if (*ppp == pp) 36580Sstevel@tonic-gate *ppp = NULL; /* page list is gone */ 36590Sstevel@tonic-gate else { 36600Sstevel@tonic-gate pp->p_prev->p_next = pp->p_next; 36610Sstevel@tonic-gate pp->p_next->p_prev = pp->p_prev; 36620Sstevel@tonic-gate } 36630Sstevel@tonic-gate pp->p_prev = pp->p_next = pp; /* make pp a list of one */ 36640Sstevel@tonic-gate } 36650Sstevel@tonic-gate 36660Sstevel@tonic-gate 36670Sstevel@tonic-gate /* 36680Sstevel@tonic-gate * Break page list cppp into two lists with npages in the first list. 36690Sstevel@tonic-gate * The tail is returned in nppp. 36700Sstevel@tonic-gate */ 36710Sstevel@tonic-gate void 36720Sstevel@tonic-gate page_list_break(page_t **oppp, page_t **nppp, pgcnt_t npages) 36730Sstevel@tonic-gate { 36740Sstevel@tonic-gate page_t *s1pp = *oppp; 36750Sstevel@tonic-gate page_t *s2pp; 36760Sstevel@tonic-gate page_t *e1pp, *e2pp; 36770Sstevel@tonic-gate long n = 0; 36780Sstevel@tonic-gate 36790Sstevel@tonic-gate if (s1pp == NULL) { 36800Sstevel@tonic-gate *nppp = NULL; 36810Sstevel@tonic-gate return; 36820Sstevel@tonic-gate } 36830Sstevel@tonic-gate if (npages == 0) { 36840Sstevel@tonic-gate *nppp = s1pp; 36850Sstevel@tonic-gate *oppp = NULL; 36860Sstevel@tonic-gate return; 36870Sstevel@tonic-gate } 36880Sstevel@tonic-gate for (n = 0, s2pp = *oppp; n < npages; n++) { 36890Sstevel@tonic-gate s2pp = s2pp->p_next; 36900Sstevel@tonic-gate } 36910Sstevel@tonic-gate /* Fix head and tail of new lists */ 36920Sstevel@tonic-gate e1pp = s2pp->p_prev; 36930Sstevel@tonic-gate e2pp = s1pp->p_prev; 36940Sstevel@tonic-gate s1pp->p_prev = e1pp; 36950Sstevel@tonic-gate e1pp->p_next = s1pp; 36960Sstevel@tonic-gate s2pp->p_prev = e2pp; 36970Sstevel@tonic-gate e2pp->p_next = s2pp; 36980Sstevel@tonic-gate 36990Sstevel@tonic-gate /* second list empty */ 37000Sstevel@tonic-gate if (s2pp == s1pp) { 37010Sstevel@tonic-gate *oppp = s1pp; 37020Sstevel@tonic-gate *nppp = NULL; 37030Sstevel@tonic-gate } else { 37040Sstevel@tonic-gate *oppp = s1pp; 37050Sstevel@tonic-gate *nppp = s2pp; 37060Sstevel@tonic-gate } 37070Sstevel@tonic-gate } 37080Sstevel@tonic-gate 37090Sstevel@tonic-gate /* 37100Sstevel@tonic-gate * Concatenate page list nppp onto the end of list ppp. 37110Sstevel@tonic-gate */ 37120Sstevel@tonic-gate void 37130Sstevel@tonic-gate page_list_concat(page_t **ppp, page_t **nppp) 37140Sstevel@tonic-gate { 37150Sstevel@tonic-gate page_t *s1pp, *s2pp, *e1pp, *e2pp; 37160Sstevel@tonic-gate 37170Sstevel@tonic-gate if (*nppp == NULL) { 37180Sstevel@tonic-gate return; 37190Sstevel@tonic-gate } 37200Sstevel@tonic-gate if (*ppp == NULL) { 37210Sstevel@tonic-gate *ppp = *nppp; 37220Sstevel@tonic-gate return; 37230Sstevel@tonic-gate } 37240Sstevel@tonic-gate s1pp = *ppp; 37250Sstevel@tonic-gate e1pp = s1pp->p_prev; 37260Sstevel@tonic-gate s2pp = *nppp; 37270Sstevel@tonic-gate e2pp = s2pp->p_prev; 37280Sstevel@tonic-gate s1pp->p_prev = e2pp; 37290Sstevel@tonic-gate e2pp->p_next = s1pp; 37300Sstevel@tonic-gate e1pp->p_next = s2pp; 37310Sstevel@tonic-gate s2pp->p_prev = e1pp; 37320Sstevel@tonic-gate } 37330Sstevel@tonic-gate 37340Sstevel@tonic-gate /* 37350Sstevel@tonic-gate * return the next page in the page list 37360Sstevel@tonic-gate */ 37370Sstevel@tonic-gate page_t * 37380Sstevel@tonic-gate page_list_next(page_t *pp) 37390Sstevel@tonic-gate { 37400Sstevel@tonic-gate return (pp->p_next); 37410Sstevel@tonic-gate } 37420Sstevel@tonic-gate 37430Sstevel@tonic-gate 37440Sstevel@tonic-gate /* 37450Sstevel@tonic-gate * Add the page to the front of the linked list of pages 37460Sstevel@tonic-gate * using p_vpnext/p_vpprev pointers for the list. 37470Sstevel@tonic-gate * 37480Sstevel@tonic-gate * The caller is responsible for protecting the lists. 37490Sstevel@tonic-gate */ 37500Sstevel@tonic-gate void 37510Sstevel@tonic-gate page_vpadd(page_t **ppp, page_t *pp) 37520Sstevel@tonic-gate { 37530Sstevel@tonic-gate if (*ppp == NULL) { 37540Sstevel@tonic-gate pp->p_vpnext = pp->p_vpprev = pp; 37550Sstevel@tonic-gate } else { 37560Sstevel@tonic-gate pp->p_vpnext = *ppp; 37570Sstevel@tonic-gate pp->p_vpprev = (*ppp)->p_vpprev; 37580Sstevel@tonic-gate (*ppp)->p_vpprev = pp; 37590Sstevel@tonic-gate pp->p_vpprev->p_vpnext = pp; 37600Sstevel@tonic-gate } 37610Sstevel@tonic-gate *ppp = pp; 37620Sstevel@tonic-gate } 37630Sstevel@tonic-gate 37640Sstevel@tonic-gate /* 37650Sstevel@tonic-gate * Remove this page from the linked list of pages 37660Sstevel@tonic-gate * using p_vpnext/p_vpprev pointers for the list. 37670Sstevel@tonic-gate * 37680Sstevel@tonic-gate * The caller is responsible for protecting the lists. 37690Sstevel@tonic-gate */ 37700Sstevel@tonic-gate void 37710Sstevel@tonic-gate page_vpsub(page_t **ppp, page_t *pp) 37720Sstevel@tonic-gate { 37730Sstevel@tonic-gate if (*ppp == NULL || pp == NULL) { 37740Sstevel@tonic-gate panic("page_vpsub: bad arg(s): pp %p, *ppp %p", 37750Sstevel@tonic-gate (void *)pp, (void *)(*ppp)); 37760Sstevel@tonic-gate /*NOTREACHED*/ 37770Sstevel@tonic-gate } 37780Sstevel@tonic-gate 37790Sstevel@tonic-gate if (*ppp == pp) 37800Sstevel@tonic-gate *ppp = pp->p_vpnext; /* go to next page */ 37810Sstevel@tonic-gate 37820Sstevel@tonic-gate if (*ppp == pp) 37830Sstevel@tonic-gate *ppp = NULL; /* page list is gone */ 37840Sstevel@tonic-gate else { 37850Sstevel@tonic-gate pp->p_vpprev->p_vpnext = pp->p_vpnext; 37860Sstevel@tonic-gate pp->p_vpnext->p_vpprev = pp->p_vpprev; 37870Sstevel@tonic-gate } 37880Sstevel@tonic-gate pp->p_vpprev = pp->p_vpnext = pp; /* make pp a list of one */ 37890Sstevel@tonic-gate } 37900Sstevel@tonic-gate 37910Sstevel@tonic-gate /* 37920Sstevel@tonic-gate * Lock a physical page into memory "long term". Used to support "lock 37930Sstevel@tonic-gate * in memory" functions. Accepts the page to be locked, and a cow variable 37940Sstevel@tonic-gate * to indicate whether a the lock will travel to the new page during 37950Sstevel@tonic-gate * a potential copy-on-write. 37960Sstevel@tonic-gate */ 37970Sstevel@tonic-gate int 37980Sstevel@tonic-gate page_pp_lock( 37990Sstevel@tonic-gate page_t *pp, /* page to be locked */ 38000Sstevel@tonic-gate int cow, /* cow lock */ 38010Sstevel@tonic-gate int kernel) /* must succeed -- ignore checking */ 38020Sstevel@tonic-gate { 38030Sstevel@tonic-gate int r = 0; /* result -- assume failure */ 38040Sstevel@tonic-gate 38050Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 38060Sstevel@tonic-gate 38070Sstevel@tonic-gate page_struct_lock(pp); 38080Sstevel@tonic-gate /* 38090Sstevel@tonic-gate * Acquire the "freemem_lock" for availrmem. 38100Sstevel@tonic-gate */ 38110Sstevel@tonic-gate if (cow) { 38120Sstevel@tonic-gate mutex_enter(&freemem_lock); 38130Sstevel@tonic-gate if ((availrmem > pages_pp_maximum) && 38140Sstevel@tonic-gate (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM)) { 38150Sstevel@tonic-gate availrmem--; 38160Sstevel@tonic-gate pages_locked++; 38170Sstevel@tonic-gate mutex_exit(&freemem_lock); 38180Sstevel@tonic-gate r = 1; 38190Sstevel@tonic-gate if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 38200Sstevel@tonic-gate cmn_err(CE_WARN, 38210Sstevel@tonic-gate "COW lock limit reached on pfn 0x%lx", 38220Sstevel@tonic-gate page_pptonum(pp)); 38230Sstevel@tonic-gate } 38240Sstevel@tonic-gate } else 38250Sstevel@tonic-gate mutex_exit(&freemem_lock); 38260Sstevel@tonic-gate } else { 38270Sstevel@tonic-gate if (pp->p_lckcnt) { 38280Sstevel@tonic-gate if (pp->p_lckcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 38290Sstevel@tonic-gate r = 1; 38300Sstevel@tonic-gate if (++pp->p_lckcnt == 38310Sstevel@tonic-gate (ushort_t)PAGE_LOCK_MAXIMUM) { 38320Sstevel@tonic-gate cmn_err(CE_WARN, "Page lock limit " 38330Sstevel@tonic-gate "reached on pfn 0x%lx", 38340Sstevel@tonic-gate page_pptonum(pp)); 38350Sstevel@tonic-gate } 38360Sstevel@tonic-gate } 38370Sstevel@tonic-gate } else { 38380Sstevel@tonic-gate if (kernel) { 38390Sstevel@tonic-gate /* availrmem accounting done by caller */ 38400Sstevel@tonic-gate ++pp->p_lckcnt; 38410Sstevel@tonic-gate r = 1; 38420Sstevel@tonic-gate } else { 38430Sstevel@tonic-gate mutex_enter(&freemem_lock); 38440Sstevel@tonic-gate if (availrmem > pages_pp_maximum) { 38450Sstevel@tonic-gate availrmem--; 38460Sstevel@tonic-gate pages_locked++; 38470Sstevel@tonic-gate ++pp->p_lckcnt; 38480Sstevel@tonic-gate r = 1; 38490Sstevel@tonic-gate } 38500Sstevel@tonic-gate mutex_exit(&freemem_lock); 38510Sstevel@tonic-gate } 38520Sstevel@tonic-gate } 38530Sstevel@tonic-gate } 38540Sstevel@tonic-gate page_struct_unlock(pp); 38550Sstevel@tonic-gate return (r); 38560Sstevel@tonic-gate } 38570Sstevel@tonic-gate 38580Sstevel@tonic-gate /* 38590Sstevel@tonic-gate * Decommit a lock on a physical page frame. Account for cow locks if 38600Sstevel@tonic-gate * appropriate. 38610Sstevel@tonic-gate */ 38620Sstevel@tonic-gate void 38630Sstevel@tonic-gate page_pp_unlock( 38640Sstevel@tonic-gate page_t *pp, /* page to be unlocked */ 38650Sstevel@tonic-gate int cow, /* expect cow lock */ 38660Sstevel@tonic-gate int kernel) /* this was a kernel lock */ 38670Sstevel@tonic-gate { 38680Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 38690Sstevel@tonic-gate 38700Sstevel@tonic-gate page_struct_lock(pp); 38710Sstevel@tonic-gate /* 38720Sstevel@tonic-gate * Acquire the "freemem_lock" for availrmem. 38730Sstevel@tonic-gate * If cowcnt or lcknt is already 0 do nothing; i.e., we 38740Sstevel@tonic-gate * could be called to unlock even if nothing is locked. This could 38750Sstevel@tonic-gate * happen if locked file pages were truncated (removing the lock) 38760Sstevel@tonic-gate * and the file was grown again and new pages faulted in; the new 38770Sstevel@tonic-gate * pages are unlocked but the segment still thinks they're locked. 38780Sstevel@tonic-gate */ 38790Sstevel@tonic-gate if (cow) { 38800Sstevel@tonic-gate if (pp->p_cowcnt) { 38810Sstevel@tonic-gate mutex_enter(&freemem_lock); 38820Sstevel@tonic-gate pp->p_cowcnt--; 38830Sstevel@tonic-gate availrmem++; 38840Sstevel@tonic-gate pages_locked--; 38850Sstevel@tonic-gate mutex_exit(&freemem_lock); 38860Sstevel@tonic-gate } 38870Sstevel@tonic-gate } else { 38880Sstevel@tonic-gate if (pp->p_lckcnt && --pp->p_lckcnt == 0) { 38890Sstevel@tonic-gate if (!kernel) { 38900Sstevel@tonic-gate mutex_enter(&freemem_lock); 38910Sstevel@tonic-gate availrmem++; 38920Sstevel@tonic-gate pages_locked--; 38930Sstevel@tonic-gate mutex_exit(&freemem_lock); 38940Sstevel@tonic-gate } 38950Sstevel@tonic-gate } 38960Sstevel@tonic-gate } 38970Sstevel@tonic-gate page_struct_unlock(pp); 38980Sstevel@tonic-gate } 38990Sstevel@tonic-gate 39000Sstevel@tonic-gate /* 39010Sstevel@tonic-gate * This routine reserves availrmem for npages; 39020Sstevel@tonic-gate * flags: KM_NOSLEEP or KM_SLEEP 39030Sstevel@tonic-gate * returns 1 on success or 0 on failure 39040Sstevel@tonic-gate */ 39050Sstevel@tonic-gate int 39060Sstevel@tonic-gate page_resv(pgcnt_t npages, uint_t flags) 39070Sstevel@tonic-gate { 39080Sstevel@tonic-gate mutex_enter(&freemem_lock); 39090Sstevel@tonic-gate while (availrmem < tune.t_minarmem + npages) { 39100Sstevel@tonic-gate if (flags & KM_NOSLEEP) { 39110Sstevel@tonic-gate mutex_exit(&freemem_lock); 39120Sstevel@tonic-gate return (0); 39130Sstevel@tonic-gate } 39140Sstevel@tonic-gate mutex_exit(&freemem_lock); 39150Sstevel@tonic-gate page_needfree(npages); 39160Sstevel@tonic-gate kmem_reap(); 39170Sstevel@tonic-gate delay(hz >> 2); 39180Sstevel@tonic-gate page_needfree(-(spgcnt_t)npages); 39190Sstevel@tonic-gate mutex_enter(&freemem_lock); 39200Sstevel@tonic-gate } 39210Sstevel@tonic-gate availrmem -= npages; 39220Sstevel@tonic-gate mutex_exit(&freemem_lock); 39230Sstevel@tonic-gate return (1); 39240Sstevel@tonic-gate } 39250Sstevel@tonic-gate 39260Sstevel@tonic-gate /* 39270Sstevel@tonic-gate * This routine unreserves availrmem for npages; 39280Sstevel@tonic-gate */ 39290Sstevel@tonic-gate void 39300Sstevel@tonic-gate page_unresv(pgcnt_t npages) 39310Sstevel@tonic-gate { 39320Sstevel@tonic-gate mutex_enter(&freemem_lock); 39330Sstevel@tonic-gate availrmem += npages; 39340Sstevel@tonic-gate mutex_exit(&freemem_lock); 39350Sstevel@tonic-gate } 39360Sstevel@tonic-gate 39370Sstevel@tonic-gate /* 39380Sstevel@tonic-gate * See Statement at the beginning of segvn_lockop() regarding 39390Sstevel@tonic-gate * the way we handle cowcnts and lckcnts. 39400Sstevel@tonic-gate * 39410Sstevel@tonic-gate * Transfer cowcnt on 'opp' to cowcnt on 'npp' if the vpage 39420Sstevel@tonic-gate * that breaks COW has PROT_WRITE. 39430Sstevel@tonic-gate * 39440Sstevel@tonic-gate * Note that, we may also break COW in case we are softlocking 39450Sstevel@tonic-gate * on read access during physio; 39460Sstevel@tonic-gate * in this softlock case, the vpage may not have PROT_WRITE. 39470Sstevel@tonic-gate * So, we need to transfer lckcnt on 'opp' to lckcnt on 'npp' 39480Sstevel@tonic-gate * if the vpage doesn't have PROT_WRITE. 39490Sstevel@tonic-gate * 39500Sstevel@tonic-gate * This routine is never called if we are stealing a page 39510Sstevel@tonic-gate * in anon_private. 39520Sstevel@tonic-gate * 39530Sstevel@tonic-gate * The caller subtracted from availrmem for read only mapping. 39540Sstevel@tonic-gate * if lckcnt is 1 increment availrmem. 39550Sstevel@tonic-gate */ 39560Sstevel@tonic-gate void 39570Sstevel@tonic-gate page_pp_useclaim( 39580Sstevel@tonic-gate page_t *opp, /* original page frame losing lock */ 39590Sstevel@tonic-gate page_t *npp, /* new page frame gaining lock */ 39600Sstevel@tonic-gate uint_t write_perm) /* set if vpage has PROT_WRITE */ 39610Sstevel@tonic-gate { 39620Sstevel@tonic-gate int payback = 0; 39630Sstevel@tonic-gate 39640Sstevel@tonic-gate ASSERT(PAGE_LOCKED(opp)); 39650Sstevel@tonic-gate ASSERT(PAGE_LOCKED(npp)); 39660Sstevel@tonic-gate 39670Sstevel@tonic-gate page_struct_lock(opp); 39680Sstevel@tonic-gate 39690Sstevel@tonic-gate ASSERT(npp->p_cowcnt == 0); 39700Sstevel@tonic-gate ASSERT(npp->p_lckcnt == 0); 39710Sstevel@tonic-gate 39720Sstevel@tonic-gate /* Don't use claim if nothing is locked (see page_pp_unlock above) */ 39730Sstevel@tonic-gate if ((write_perm && opp->p_cowcnt != 0) || 39740Sstevel@tonic-gate (!write_perm && opp->p_lckcnt != 0)) { 39750Sstevel@tonic-gate 39760Sstevel@tonic-gate if (write_perm) { 39770Sstevel@tonic-gate npp->p_cowcnt++; 39780Sstevel@tonic-gate ASSERT(opp->p_cowcnt != 0); 39790Sstevel@tonic-gate opp->p_cowcnt--; 39800Sstevel@tonic-gate } else { 39810Sstevel@tonic-gate 39820Sstevel@tonic-gate ASSERT(opp->p_lckcnt != 0); 39830Sstevel@tonic-gate 39840Sstevel@tonic-gate /* 39850Sstevel@tonic-gate * We didn't need availrmem decremented if p_lckcnt on 39860Sstevel@tonic-gate * original page is 1. Here, we are unlocking 39870Sstevel@tonic-gate * read-only copy belonging to original page and 39880Sstevel@tonic-gate * are locking a copy belonging to new page. 39890Sstevel@tonic-gate */ 39900Sstevel@tonic-gate if (opp->p_lckcnt == 1) 39910Sstevel@tonic-gate payback = 1; 39920Sstevel@tonic-gate 39930Sstevel@tonic-gate npp->p_lckcnt++; 39940Sstevel@tonic-gate opp->p_lckcnt--; 39950Sstevel@tonic-gate } 39960Sstevel@tonic-gate } 39970Sstevel@tonic-gate if (payback) { 39980Sstevel@tonic-gate mutex_enter(&freemem_lock); 39990Sstevel@tonic-gate availrmem++; 40000Sstevel@tonic-gate pages_useclaim--; 40010Sstevel@tonic-gate mutex_exit(&freemem_lock); 40020Sstevel@tonic-gate } 40030Sstevel@tonic-gate page_struct_unlock(opp); 40040Sstevel@tonic-gate } 40050Sstevel@tonic-gate 40060Sstevel@tonic-gate /* 40070Sstevel@tonic-gate * Simple claim adjust functions -- used to support changes in 40080Sstevel@tonic-gate * claims due to changes in access permissions. Used by segvn_setprot(). 40090Sstevel@tonic-gate */ 40100Sstevel@tonic-gate int 40110Sstevel@tonic-gate page_addclaim(page_t *pp) 40120Sstevel@tonic-gate { 40130Sstevel@tonic-gate int r = 0; /* result */ 40140Sstevel@tonic-gate 40150Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 40160Sstevel@tonic-gate 40170Sstevel@tonic-gate page_struct_lock(pp); 40180Sstevel@tonic-gate ASSERT(pp->p_lckcnt != 0); 40190Sstevel@tonic-gate 40200Sstevel@tonic-gate if (pp->p_lckcnt == 1) { 40210Sstevel@tonic-gate if (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 40220Sstevel@tonic-gate --pp->p_lckcnt; 40230Sstevel@tonic-gate r = 1; 40240Sstevel@tonic-gate if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 40250Sstevel@tonic-gate cmn_err(CE_WARN, 40260Sstevel@tonic-gate "COW lock limit reached on pfn 0x%lx", 40270Sstevel@tonic-gate page_pptonum(pp)); 40280Sstevel@tonic-gate } 40290Sstevel@tonic-gate } 40300Sstevel@tonic-gate } else { 40310Sstevel@tonic-gate mutex_enter(&freemem_lock); 40320Sstevel@tonic-gate if ((availrmem > pages_pp_maximum) && 40330Sstevel@tonic-gate (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM)) { 40340Sstevel@tonic-gate --availrmem; 40350Sstevel@tonic-gate ++pages_claimed; 40360Sstevel@tonic-gate mutex_exit(&freemem_lock); 40370Sstevel@tonic-gate --pp->p_lckcnt; 40380Sstevel@tonic-gate r = 1; 40390Sstevel@tonic-gate if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 40400Sstevel@tonic-gate cmn_err(CE_WARN, 40410Sstevel@tonic-gate "COW lock limit reached on pfn 0x%lx", 40420Sstevel@tonic-gate page_pptonum(pp)); 40430Sstevel@tonic-gate } 40440Sstevel@tonic-gate } else 40450Sstevel@tonic-gate mutex_exit(&freemem_lock); 40460Sstevel@tonic-gate } 40470Sstevel@tonic-gate page_struct_unlock(pp); 40480Sstevel@tonic-gate return (r); 40490Sstevel@tonic-gate } 40500Sstevel@tonic-gate 40510Sstevel@tonic-gate int 40520Sstevel@tonic-gate page_subclaim(page_t *pp) 40530Sstevel@tonic-gate { 40540Sstevel@tonic-gate int r = 0; 40550Sstevel@tonic-gate 40560Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 40570Sstevel@tonic-gate 40580Sstevel@tonic-gate page_struct_lock(pp); 40590Sstevel@tonic-gate ASSERT(pp->p_cowcnt != 0); 40600Sstevel@tonic-gate 40610Sstevel@tonic-gate if (pp->p_lckcnt) { 40620Sstevel@tonic-gate if (pp->p_lckcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 40630Sstevel@tonic-gate r = 1; 40640Sstevel@tonic-gate /* 40650Sstevel@tonic-gate * for availrmem 40660Sstevel@tonic-gate */ 40670Sstevel@tonic-gate mutex_enter(&freemem_lock); 40680Sstevel@tonic-gate availrmem++; 40690Sstevel@tonic-gate pages_claimed--; 40700Sstevel@tonic-gate mutex_exit(&freemem_lock); 40710Sstevel@tonic-gate 40720Sstevel@tonic-gate pp->p_cowcnt--; 40730Sstevel@tonic-gate 40740Sstevel@tonic-gate if (++pp->p_lckcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 40750Sstevel@tonic-gate cmn_err(CE_WARN, 40760Sstevel@tonic-gate "Page lock limit reached on pfn 0x%lx", 40770Sstevel@tonic-gate page_pptonum(pp)); 40780Sstevel@tonic-gate } 40790Sstevel@tonic-gate } 40800Sstevel@tonic-gate } else { 40810Sstevel@tonic-gate r = 1; 40820Sstevel@tonic-gate pp->p_cowcnt--; 40830Sstevel@tonic-gate pp->p_lckcnt++; 40840Sstevel@tonic-gate } 40850Sstevel@tonic-gate page_struct_unlock(pp); 40860Sstevel@tonic-gate return (r); 40870Sstevel@tonic-gate } 40880Sstevel@tonic-gate 40890Sstevel@tonic-gate int 40900Sstevel@tonic-gate page_addclaim_pages(page_t **ppa) 40910Sstevel@tonic-gate { 40920Sstevel@tonic-gate 40930Sstevel@tonic-gate pgcnt_t lckpgs = 0, pg_idx; 40940Sstevel@tonic-gate 40950Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_addclaim_pages); 40960Sstevel@tonic-gate 40970Sstevel@tonic-gate mutex_enter(&page_llock); 40980Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 40990Sstevel@tonic-gate 41000Sstevel@tonic-gate ASSERT(PAGE_LOCKED(ppa[pg_idx])); 41010Sstevel@tonic-gate ASSERT(ppa[pg_idx]->p_lckcnt != 0); 41020Sstevel@tonic-gate if (ppa[pg_idx]->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 41030Sstevel@tonic-gate mutex_exit(&page_llock); 41040Sstevel@tonic-gate return (0); 41050Sstevel@tonic-gate } 41060Sstevel@tonic-gate if (ppa[pg_idx]->p_lckcnt > 1) 41070Sstevel@tonic-gate lckpgs++; 41080Sstevel@tonic-gate } 41090Sstevel@tonic-gate 41100Sstevel@tonic-gate if (lckpgs != 0) { 41110Sstevel@tonic-gate mutex_enter(&freemem_lock); 41120Sstevel@tonic-gate if (availrmem >= pages_pp_maximum + lckpgs) { 41130Sstevel@tonic-gate availrmem -= lckpgs; 41140Sstevel@tonic-gate pages_claimed += lckpgs; 41150Sstevel@tonic-gate } else { 41160Sstevel@tonic-gate mutex_exit(&freemem_lock); 41170Sstevel@tonic-gate mutex_exit(&page_llock); 41180Sstevel@tonic-gate return (0); 41190Sstevel@tonic-gate } 41200Sstevel@tonic-gate mutex_exit(&freemem_lock); 41210Sstevel@tonic-gate } 41220Sstevel@tonic-gate 41230Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 41240Sstevel@tonic-gate ppa[pg_idx]->p_lckcnt--; 41250Sstevel@tonic-gate ppa[pg_idx]->p_cowcnt++; 41260Sstevel@tonic-gate } 41270Sstevel@tonic-gate mutex_exit(&page_llock); 41280Sstevel@tonic-gate return (1); 41290Sstevel@tonic-gate } 41300Sstevel@tonic-gate 41310Sstevel@tonic-gate int 41320Sstevel@tonic-gate page_subclaim_pages(page_t **ppa) 41330Sstevel@tonic-gate { 41340Sstevel@tonic-gate pgcnt_t ulckpgs = 0, pg_idx; 41350Sstevel@tonic-gate 41360Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_subclaim_pages); 41370Sstevel@tonic-gate 41380Sstevel@tonic-gate mutex_enter(&page_llock); 41390Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 41400Sstevel@tonic-gate 41410Sstevel@tonic-gate ASSERT(PAGE_LOCKED(ppa[pg_idx])); 41420Sstevel@tonic-gate ASSERT(ppa[pg_idx]->p_cowcnt != 0); 41430Sstevel@tonic-gate if (ppa[pg_idx]->p_lckcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 41440Sstevel@tonic-gate mutex_exit(&page_llock); 41450Sstevel@tonic-gate return (0); 41460Sstevel@tonic-gate } 41470Sstevel@tonic-gate if (ppa[pg_idx]->p_lckcnt != 0) 41480Sstevel@tonic-gate ulckpgs++; 41490Sstevel@tonic-gate } 41500Sstevel@tonic-gate 41510Sstevel@tonic-gate if (ulckpgs != 0) { 41520Sstevel@tonic-gate mutex_enter(&freemem_lock); 41530Sstevel@tonic-gate availrmem += ulckpgs; 41540Sstevel@tonic-gate pages_claimed -= ulckpgs; 41550Sstevel@tonic-gate mutex_exit(&freemem_lock); 41560Sstevel@tonic-gate } 41570Sstevel@tonic-gate 41580Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 41590Sstevel@tonic-gate ppa[pg_idx]->p_cowcnt--; 41600Sstevel@tonic-gate ppa[pg_idx]->p_lckcnt++; 41610Sstevel@tonic-gate 41620Sstevel@tonic-gate } 41630Sstevel@tonic-gate mutex_exit(&page_llock); 41640Sstevel@tonic-gate return (1); 41650Sstevel@tonic-gate } 41660Sstevel@tonic-gate 41670Sstevel@tonic-gate page_t * 41680Sstevel@tonic-gate page_numtopp(pfn_t pfnum, se_t se) 41690Sstevel@tonic-gate { 41700Sstevel@tonic-gate page_t *pp; 41710Sstevel@tonic-gate 41720Sstevel@tonic-gate retry: 41730Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 41740Sstevel@tonic-gate if (pp == NULL) { 41750Sstevel@tonic-gate return ((page_t *)NULL); 41760Sstevel@tonic-gate } 41770Sstevel@tonic-gate 41780Sstevel@tonic-gate /* 41790Sstevel@tonic-gate * Acquire the appropriate lock on the page. 41800Sstevel@tonic-gate */ 41810Sstevel@tonic-gate while (!page_lock(pp, se, (kmutex_t *)NULL, P_RECLAIM)) { 41820Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) 41830Sstevel@tonic-gate goto retry; 41840Sstevel@tonic-gate continue; 41850Sstevel@tonic-gate } 41860Sstevel@tonic-gate 41870Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) { 41880Sstevel@tonic-gate page_unlock(pp); 41890Sstevel@tonic-gate goto retry; 41900Sstevel@tonic-gate } 41910Sstevel@tonic-gate 41920Sstevel@tonic-gate return (pp); 41930Sstevel@tonic-gate } 41940Sstevel@tonic-gate 41950Sstevel@tonic-gate page_t * 41960Sstevel@tonic-gate page_numtopp_noreclaim(pfn_t pfnum, se_t se) 41970Sstevel@tonic-gate { 41980Sstevel@tonic-gate page_t *pp; 41990Sstevel@tonic-gate 42000Sstevel@tonic-gate retry: 42010Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 42020Sstevel@tonic-gate if (pp == NULL) { 42030Sstevel@tonic-gate return ((page_t *)NULL); 42040Sstevel@tonic-gate } 42050Sstevel@tonic-gate 42060Sstevel@tonic-gate /* 42070Sstevel@tonic-gate * Acquire the appropriate lock on the page. 42080Sstevel@tonic-gate */ 42090Sstevel@tonic-gate while (!page_lock(pp, se, (kmutex_t *)NULL, P_NO_RECLAIM)) { 42100Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) 42110Sstevel@tonic-gate goto retry; 42120Sstevel@tonic-gate continue; 42130Sstevel@tonic-gate } 42140Sstevel@tonic-gate 42150Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) { 42160Sstevel@tonic-gate page_unlock(pp); 42170Sstevel@tonic-gate goto retry; 42180Sstevel@tonic-gate } 42190Sstevel@tonic-gate 42200Sstevel@tonic-gate return (pp); 42210Sstevel@tonic-gate } 42220Sstevel@tonic-gate 42230Sstevel@tonic-gate /* 42240Sstevel@tonic-gate * This routine is like page_numtopp, but will only return page structs 42250Sstevel@tonic-gate * for pages which are ok for loading into hardware using the page struct. 42260Sstevel@tonic-gate */ 42270Sstevel@tonic-gate page_t * 42280Sstevel@tonic-gate page_numtopp_nowait(pfn_t pfnum, se_t se) 42290Sstevel@tonic-gate { 42300Sstevel@tonic-gate page_t *pp; 42310Sstevel@tonic-gate 42320Sstevel@tonic-gate retry: 42330Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 42340Sstevel@tonic-gate if (pp == NULL) { 42350Sstevel@tonic-gate return ((page_t *)NULL); 42360Sstevel@tonic-gate } 42370Sstevel@tonic-gate 42380Sstevel@tonic-gate /* 42390Sstevel@tonic-gate * Try to acquire the appropriate lock on the page. 42400Sstevel@tonic-gate */ 42410Sstevel@tonic-gate if (PP_ISFREE(pp)) 42420Sstevel@tonic-gate pp = NULL; 42430Sstevel@tonic-gate else { 42440Sstevel@tonic-gate if (!page_trylock(pp, se)) 42450Sstevel@tonic-gate pp = NULL; 42460Sstevel@tonic-gate else { 42470Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) { 42480Sstevel@tonic-gate page_unlock(pp); 42490Sstevel@tonic-gate goto retry; 42500Sstevel@tonic-gate } 42510Sstevel@tonic-gate if (PP_ISFREE(pp)) { 42520Sstevel@tonic-gate page_unlock(pp); 42530Sstevel@tonic-gate pp = NULL; 42540Sstevel@tonic-gate } 42550Sstevel@tonic-gate } 42560Sstevel@tonic-gate } 42570Sstevel@tonic-gate return (pp); 42580Sstevel@tonic-gate } 42590Sstevel@tonic-gate 42600Sstevel@tonic-gate /* 42610Sstevel@tonic-gate * Returns a count of dirty pages that are in the process 42620Sstevel@tonic-gate * of being written out. If 'cleanit' is set, try to push the page. 42630Sstevel@tonic-gate */ 42640Sstevel@tonic-gate pgcnt_t 42650Sstevel@tonic-gate page_busy(int cleanit) 42660Sstevel@tonic-gate { 42670Sstevel@tonic-gate page_t *page0 = page_first(); 42680Sstevel@tonic-gate page_t *pp = page0; 42690Sstevel@tonic-gate pgcnt_t nppbusy = 0; 42700Sstevel@tonic-gate u_offset_t off; 42710Sstevel@tonic-gate 42720Sstevel@tonic-gate do { 42730Sstevel@tonic-gate vnode_t *vp = pp->p_vnode; 42740Sstevel@tonic-gate 42750Sstevel@tonic-gate /* 42760Sstevel@tonic-gate * A page is a candidate for syncing if it is: 42770Sstevel@tonic-gate * 42780Sstevel@tonic-gate * (a) On neither the freelist nor the cachelist 42790Sstevel@tonic-gate * (b) Hashed onto a vnode 42800Sstevel@tonic-gate * (c) Not a kernel page 42810Sstevel@tonic-gate * (d) Dirty 42820Sstevel@tonic-gate * (e) Not part of a swapfile 42830Sstevel@tonic-gate * (f) a page which belongs to a real vnode; eg has a non-null 42840Sstevel@tonic-gate * v_vfsp pointer. 42850Sstevel@tonic-gate * (g) Backed by a filesystem which doesn't have a 42860Sstevel@tonic-gate * stubbed-out sync operation 42870Sstevel@tonic-gate */ 42883290Sjohansen if (!PP_ISFREE(pp) && vp != NULL && !VN_ISKAS(vp) && 42890Sstevel@tonic-gate hat_ismod(pp) && !IS_SWAPVP(vp) && vp->v_vfsp != NULL && 42900Sstevel@tonic-gate vfs_can_sync(vp->v_vfsp)) { 42910Sstevel@tonic-gate nppbusy++; 42920Sstevel@tonic-gate vfs_syncprogress(); 42930Sstevel@tonic-gate 42940Sstevel@tonic-gate if (!cleanit) 42950Sstevel@tonic-gate continue; 42960Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) 42970Sstevel@tonic-gate continue; 42980Sstevel@tonic-gate 42990Sstevel@tonic-gate if (PP_ISFREE(pp) || vp == NULL || IS_SWAPVP(vp) || 43000Sstevel@tonic-gate pp->p_lckcnt != 0 || pp->p_cowcnt != 0 || 43010Sstevel@tonic-gate !(hat_pagesync(pp, 43020Sstevel@tonic-gate HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD) & P_MOD)) { 43030Sstevel@tonic-gate page_unlock(pp); 43040Sstevel@tonic-gate continue; 43050Sstevel@tonic-gate } 43060Sstevel@tonic-gate off = pp->p_offset; 43070Sstevel@tonic-gate VN_HOLD(vp); 43080Sstevel@tonic-gate page_unlock(pp); 43090Sstevel@tonic-gate (void) VOP_PUTPAGE(vp, off, PAGESIZE, 43105331Samw B_ASYNC | B_FREE, kcred, NULL); 43110Sstevel@tonic-gate VN_RELE(vp); 43120Sstevel@tonic-gate } 43130Sstevel@tonic-gate } while ((pp = page_next(pp)) != page0); 43140Sstevel@tonic-gate 43150Sstevel@tonic-gate return (nppbusy); 43160Sstevel@tonic-gate } 43170Sstevel@tonic-gate 43180Sstevel@tonic-gate void page_invalidate_pages(void); 43190Sstevel@tonic-gate 43200Sstevel@tonic-gate /* 43210Sstevel@tonic-gate * callback handler to vm sub-system 43220Sstevel@tonic-gate * 43230Sstevel@tonic-gate * callers make sure no recursive entries to this func. 43240Sstevel@tonic-gate */ 43250Sstevel@tonic-gate /*ARGSUSED*/ 43260Sstevel@tonic-gate boolean_t 43270Sstevel@tonic-gate callb_vm_cpr(void *arg, int code) 43280Sstevel@tonic-gate { 43290Sstevel@tonic-gate if (code == CB_CODE_CPR_CHKPT) 43300Sstevel@tonic-gate page_invalidate_pages(); 43310Sstevel@tonic-gate return (B_TRUE); 43320Sstevel@tonic-gate } 43330Sstevel@tonic-gate 43340Sstevel@tonic-gate /* 43350Sstevel@tonic-gate * Invalidate all pages of the system. 43360Sstevel@tonic-gate * It shouldn't be called until all user page activities are all stopped. 43370Sstevel@tonic-gate */ 43380Sstevel@tonic-gate void 43390Sstevel@tonic-gate page_invalidate_pages() 43400Sstevel@tonic-gate { 43410Sstevel@tonic-gate page_t *pp; 43420Sstevel@tonic-gate page_t *page0; 43430Sstevel@tonic-gate pgcnt_t nbusypages; 43440Sstevel@tonic-gate int retry = 0; 43450Sstevel@tonic-gate const int MAXRETRIES = 4; 43460Sstevel@tonic-gate #if defined(__sparc) 43470Sstevel@tonic-gate extern struct vnode prom_ppages; 43480Sstevel@tonic-gate #endif /* __sparc */ 43490Sstevel@tonic-gate 43500Sstevel@tonic-gate top: 43510Sstevel@tonic-gate /* 43523253Smec * Flush dirty pages and destroy the clean ones. 43530Sstevel@tonic-gate */ 43540Sstevel@tonic-gate nbusypages = 0; 43550Sstevel@tonic-gate 43560Sstevel@tonic-gate pp = page0 = page_first(); 43570Sstevel@tonic-gate do { 43580Sstevel@tonic-gate struct vnode *vp; 43590Sstevel@tonic-gate u_offset_t offset; 43600Sstevel@tonic-gate int mod; 43610Sstevel@tonic-gate 43620Sstevel@tonic-gate /* 43630Sstevel@tonic-gate * skip the page if it has no vnode or the page associated 43640Sstevel@tonic-gate * with the kernel vnode or prom allocated kernel mem. 43650Sstevel@tonic-gate */ 43660Sstevel@tonic-gate #if defined(__sparc) 43673290Sjohansen if ((vp = pp->p_vnode) == NULL || VN_ISKAS(vp) || 43680Sstevel@tonic-gate vp == &prom_ppages) 43690Sstevel@tonic-gate #else /* x86 doesn't have prom or prom_ppage */ 43703290Sjohansen if ((vp = pp->p_vnode) == NULL || VN_ISKAS(vp)) 43710Sstevel@tonic-gate #endif /* __sparc */ 43720Sstevel@tonic-gate continue; 43730Sstevel@tonic-gate 43740Sstevel@tonic-gate /* 43750Sstevel@tonic-gate * skip the page which is already free invalidated. 43760Sstevel@tonic-gate */ 43770Sstevel@tonic-gate if (PP_ISFREE(pp) && PP_ISAGED(pp)) 43780Sstevel@tonic-gate continue; 43790Sstevel@tonic-gate 43800Sstevel@tonic-gate /* 43810Sstevel@tonic-gate * skip pages that are already locked or can't be "exclusively" 43820Sstevel@tonic-gate * locked or are already free. After we lock the page, check 43838433SMichael.Corcoran@Sun.COM * the free and age bits again to be sure it's not destroyed 43840Sstevel@tonic-gate * yet. 43850Sstevel@tonic-gate * To achieve max. parallelization, we use page_trylock instead 43860Sstevel@tonic-gate * of page_lock so that we don't get block on individual pages 43870Sstevel@tonic-gate * while we have thousands of other pages to process. 43880Sstevel@tonic-gate */ 43890Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) { 43900Sstevel@tonic-gate nbusypages++; 43910Sstevel@tonic-gate continue; 43920Sstevel@tonic-gate } else if (PP_ISFREE(pp)) { 43930Sstevel@tonic-gate if (!PP_ISAGED(pp)) { 43940Sstevel@tonic-gate page_destroy_free(pp); 43950Sstevel@tonic-gate } else { 43960Sstevel@tonic-gate page_unlock(pp); 43970Sstevel@tonic-gate } 43980Sstevel@tonic-gate continue; 43990Sstevel@tonic-gate } 44000Sstevel@tonic-gate /* 44010Sstevel@tonic-gate * Is this page involved in some I/O? shared? 44020Sstevel@tonic-gate * 44030Sstevel@tonic-gate * The page_struct_lock need not be acquired to 44040Sstevel@tonic-gate * examine these fields since the page has an 44050Sstevel@tonic-gate * "exclusive" lock. 44060Sstevel@tonic-gate */ 44070Sstevel@tonic-gate if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) { 44080Sstevel@tonic-gate page_unlock(pp); 44090Sstevel@tonic-gate continue; 44100Sstevel@tonic-gate } 44110Sstevel@tonic-gate 44120Sstevel@tonic-gate if (vp->v_type == VCHR) { 44130Sstevel@tonic-gate panic("vp->v_type == VCHR"); 44140Sstevel@tonic-gate /*NOTREACHED*/ 44150Sstevel@tonic-gate } 44160Sstevel@tonic-gate 44170Sstevel@tonic-gate if (!page_try_demote_pages(pp)) { 44180Sstevel@tonic-gate page_unlock(pp); 44190Sstevel@tonic-gate continue; 44200Sstevel@tonic-gate } 44210Sstevel@tonic-gate 44220Sstevel@tonic-gate /* 44230Sstevel@tonic-gate * Check the modified bit. Leave the bits alone in hardware 44240Sstevel@tonic-gate * (they will be modified if we do the putpage). 44250Sstevel@tonic-gate */ 44260Sstevel@tonic-gate mod = (hat_pagesync(pp, HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD) 44273559Smec & P_MOD); 44280Sstevel@tonic-gate if (mod) { 44290Sstevel@tonic-gate offset = pp->p_offset; 44300Sstevel@tonic-gate /* 44310Sstevel@tonic-gate * Hold the vnode before releasing the page lock 44320Sstevel@tonic-gate * to prevent it from being freed and re-used by 44330Sstevel@tonic-gate * some other thread. 44340Sstevel@tonic-gate */ 44350Sstevel@tonic-gate VN_HOLD(vp); 44360Sstevel@tonic-gate page_unlock(pp); 44370Sstevel@tonic-gate /* 44380Sstevel@tonic-gate * No error return is checked here. Callers such as 44390Sstevel@tonic-gate * cpr deals with the dirty pages at the dump time 44400Sstevel@tonic-gate * if this putpage fails. 44410Sstevel@tonic-gate */ 44420Sstevel@tonic-gate (void) VOP_PUTPAGE(vp, offset, PAGESIZE, B_INVAL, 44435331Samw kcred, NULL); 44440Sstevel@tonic-gate VN_RELE(vp); 44450Sstevel@tonic-gate } else { 44468433SMichael.Corcoran@Sun.COM /*LINTED: constant in conditional context*/ 44478433SMichael.Corcoran@Sun.COM VN_DISPOSE(pp, B_INVAL, 0, kcred); 44480Sstevel@tonic-gate } 44490Sstevel@tonic-gate } while ((pp = page_next(pp)) != page0); 44500Sstevel@tonic-gate if (nbusypages && retry++ < MAXRETRIES) { 44510Sstevel@tonic-gate delay(1); 44520Sstevel@tonic-gate goto top; 44530Sstevel@tonic-gate } 44540Sstevel@tonic-gate } 44550Sstevel@tonic-gate 44560Sstevel@tonic-gate /* 44570Sstevel@tonic-gate * Replace the page "old" with the page "new" on the page hash and vnode lists 44580Sstevel@tonic-gate * 44595331Samw * the replacement must be done in place, ie the equivalent sequence: 44600Sstevel@tonic-gate * 44610Sstevel@tonic-gate * vp = old->p_vnode; 44620Sstevel@tonic-gate * off = old->p_offset; 44630Sstevel@tonic-gate * page_do_hashout(old) 44640Sstevel@tonic-gate * page_do_hashin(new, vp, off) 44650Sstevel@tonic-gate * 44660Sstevel@tonic-gate * doesn't work, since 44670Sstevel@tonic-gate * 1) if old is the only page on the vnode, the v_pages list has a window 44680Sstevel@tonic-gate * where it looks empty. This will break file system assumptions. 44690Sstevel@tonic-gate * and 44700Sstevel@tonic-gate * 2) pvn_vplist_dirty() can't deal with pages moving on the v_pages list. 44710Sstevel@tonic-gate */ 44720Sstevel@tonic-gate static void 44730Sstevel@tonic-gate page_do_relocate_hash(page_t *new, page_t *old) 44740Sstevel@tonic-gate { 44750Sstevel@tonic-gate page_t **hash_list; 44760Sstevel@tonic-gate vnode_t *vp = old->p_vnode; 44770Sstevel@tonic-gate kmutex_t *sep; 44780Sstevel@tonic-gate 44790Sstevel@tonic-gate ASSERT(PAGE_EXCL(old)); 44800Sstevel@tonic-gate ASSERT(PAGE_EXCL(new)); 44810Sstevel@tonic-gate ASSERT(vp != NULL); 44820Sstevel@tonic-gate ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 44830Sstevel@tonic-gate ASSERT(MUTEX_HELD(PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, old->p_offset)))); 44840Sstevel@tonic-gate 44850Sstevel@tonic-gate /* 44860Sstevel@tonic-gate * First find old page on the page hash list 44870Sstevel@tonic-gate */ 44880Sstevel@tonic-gate hash_list = &page_hash[PAGE_HASH_FUNC(vp, old->p_offset)]; 44890Sstevel@tonic-gate 44900Sstevel@tonic-gate for (;;) { 44910Sstevel@tonic-gate if (*hash_list == old) 44920Sstevel@tonic-gate break; 44930Sstevel@tonic-gate if (*hash_list == NULL) { 44940Sstevel@tonic-gate panic("page_do_hashout"); 44950Sstevel@tonic-gate /*NOTREACHED*/ 44960Sstevel@tonic-gate } 44970Sstevel@tonic-gate hash_list = &(*hash_list)->p_hash; 44980Sstevel@tonic-gate } 44990Sstevel@tonic-gate 45000Sstevel@tonic-gate /* 45010Sstevel@tonic-gate * update new and replace old with new on the page hash list 45020Sstevel@tonic-gate */ 45030Sstevel@tonic-gate new->p_vnode = old->p_vnode; 45040Sstevel@tonic-gate new->p_offset = old->p_offset; 45050Sstevel@tonic-gate new->p_hash = old->p_hash; 45060Sstevel@tonic-gate *hash_list = new; 45070Sstevel@tonic-gate 45080Sstevel@tonic-gate if ((new->p_vnode->v_flag & VISSWAP) != 0) 45090Sstevel@tonic-gate PP_SETSWAP(new); 45100Sstevel@tonic-gate 45110Sstevel@tonic-gate /* 45120Sstevel@tonic-gate * replace old with new on the vnode's page list 45130Sstevel@tonic-gate */ 45140Sstevel@tonic-gate if (old->p_vpnext == old) { 45150Sstevel@tonic-gate new->p_vpnext = new; 45160Sstevel@tonic-gate new->p_vpprev = new; 45170Sstevel@tonic-gate } else { 45180Sstevel@tonic-gate new->p_vpnext = old->p_vpnext; 45190Sstevel@tonic-gate new->p_vpprev = old->p_vpprev; 45200Sstevel@tonic-gate new->p_vpnext->p_vpprev = new; 45210Sstevel@tonic-gate new->p_vpprev->p_vpnext = new; 45220Sstevel@tonic-gate } 45230Sstevel@tonic-gate if (vp->v_pages == old) 45240Sstevel@tonic-gate vp->v_pages = new; 45250Sstevel@tonic-gate 45260Sstevel@tonic-gate /* 45270Sstevel@tonic-gate * clear out the old page 45280Sstevel@tonic-gate */ 45290Sstevel@tonic-gate old->p_hash = NULL; 45300Sstevel@tonic-gate old->p_vpnext = NULL; 45310Sstevel@tonic-gate old->p_vpprev = NULL; 45320Sstevel@tonic-gate old->p_vnode = NULL; 45330Sstevel@tonic-gate PP_CLRSWAP(old); 45340Sstevel@tonic-gate old->p_offset = (u_offset_t)-1; 45357718SJason.Beloro@Sun.COM page_clr_all_props(old, 1); 45360Sstevel@tonic-gate 45370Sstevel@tonic-gate /* 45380Sstevel@tonic-gate * Wake up processes waiting for this page. The page's 45390Sstevel@tonic-gate * identity has been changed, and is probably not the 45400Sstevel@tonic-gate * desired page any longer. 45410Sstevel@tonic-gate */ 45420Sstevel@tonic-gate sep = page_se_mutex(old); 45430Sstevel@tonic-gate mutex_enter(sep); 4544800Sstans old->p_selock &= ~SE_EWANTED; 45450Sstevel@tonic-gate if (CV_HAS_WAITERS(&old->p_cv)) 45460Sstevel@tonic-gate cv_broadcast(&old->p_cv); 45470Sstevel@tonic-gate mutex_exit(sep); 45480Sstevel@tonic-gate } 45490Sstevel@tonic-gate 45500Sstevel@tonic-gate /* 45510Sstevel@tonic-gate * This function moves the identity of page "pp_old" to page "pp_new". 45520Sstevel@tonic-gate * Both pages must be locked on entry. "pp_new" is free, has no identity, 45530Sstevel@tonic-gate * and need not be hashed out from anywhere. 45540Sstevel@tonic-gate */ 45550Sstevel@tonic-gate void 45560Sstevel@tonic-gate page_relocate_hash(page_t *pp_new, page_t *pp_old) 45570Sstevel@tonic-gate { 45580Sstevel@tonic-gate vnode_t *vp = pp_old->p_vnode; 45590Sstevel@tonic-gate u_offset_t off = pp_old->p_offset; 45600Sstevel@tonic-gate kmutex_t *phm, *vphm; 45610Sstevel@tonic-gate 45620Sstevel@tonic-gate /* 45630Sstevel@tonic-gate * Rehash two pages 45640Sstevel@tonic-gate */ 45650Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp_old)); 45660Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp_new)); 45670Sstevel@tonic-gate ASSERT(vp != NULL); 45680Sstevel@tonic-gate ASSERT(pp_new->p_vnode == NULL); 45690Sstevel@tonic-gate 45700Sstevel@tonic-gate /* 45710Sstevel@tonic-gate * hashout then hashin while holding the mutexes 45720Sstevel@tonic-gate */ 45730Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, off)); 45740Sstevel@tonic-gate mutex_enter(phm); 45750Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 45760Sstevel@tonic-gate mutex_enter(vphm); 45770Sstevel@tonic-gate 45780Sstevel@tonic-gate page_do_relocate_hash(pp_new, pp_old); 45790Sstevel@tonic-gate 45808325SPrakash.Sangappa@Sun.COM /* The following comment preserved from page_flip(). */ 45818325SPrakash.Sangappa@Sun.COM pp_new->p_fsdata = pp_old->p_fsdata; 45828325SPrakash.Sangappa@Sun.COM pp_old->p_fsdata = 0; 45830Sstevel@tonic-gate mutex_exit(vphm); 45840Sstevel@tonic-gate mutex_exit(phm); 45850Sstevel@tonic-gate 45860Sstevel@tonic-gate /* 45870Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt and 45880Sstevel@tonic-gate * cowcnt since the page has an "exclusive" lock. 45890Sstevel@tonic-gate */ 45900Sstevel@tonic-gate ASSERT(pp_new->p_lckcnt == 0); 45910Sstevel@tonic-gate ASSERT(pp_new->p_cowcnt == 0); 45920Sstevel@tonic-gate pp_new->p_lckcnt = pp_old->p_lckcnt; 45930Sstevel@tonic-gate pp_new->p_cowcnt = pp_old->p_cowcnt; 45940Sstevel@tonic-gate pp_old->p_lckcnt = pp_old->p_cowcnt = 0; 45950Sstevel@tonic-gate 45960Sstevel@tonic-gate } 45970Sstevel@tonic-gate 45980Sstevel@tonic-gate /* 45990Sstevel@tonic-gate * Helper routine used to lock all remaining members of a 46000Sstevel@tonic-gate * large page. The caller is responsible for passing in a locked 46010Sstevel@tonic-gate * pp. If pp is a large page, then it succeeds in locking all the 46020Sstevel@tonic-gate * remaining constituent pages or it returns with only the 46030Sstevel@tonic-gate * original page locked. 46040Sstevel@tonic-gate * 46050Sstevel@tonic-gate * Returns 1 on success, 0 on failure. 46060Sstevel@tonic-gate * 46075331Samw * If success is returned this routine guarantees p_szc for all constituent 46080Sstevel@tonic-gate * pages of a large page pp belongs to can't change. To achieve this we 46090Sstevel@tonic-gate * recheck szc of pp after locking all constituent pages and retry if szc 46100Sstevel@tonic-gate * changed (it could only decrease). Since hat_page_demote() needs an EXCL 46110Sstevel@tonic-gate * lock on one of constituent pages it can't be running after all constituent 46120Sstevel@tonic-gate * pages are locked. hat_page_demote() with a lock on a constituent page 46130Sstevel@tonic-gate * outside of this large page (i.e. pp belonged to a larger large page) is 46140Sstevel@tonic-gate * already done with all constituent pages of pp since the root's p_szc is 46155331Samw * changed last. Therefore no need to synchronize with hat_page_demote() that 46160Sstevel@tonic-gate * locked a constituent page outside of pp's current large page. 46170Sstevel@tonic-gate */ 46180Sstevel@tonic-gate #ifdef DEBUG 46190Sstevel@tonic-gate uint32_t gpg_trylock_mtbf = 0; 46200Sstevel@tonic-gate #endif 46210Sstevel@tonic-gate 46220Sstevel@tonic-gate int 46230Sstevel@tonic-gate group_page_trylock(page_t *pp, se_t se) 46240Sstevel@tonic-gate { 46250Sstevel@tonic-gate page_t *tpp; 46260Sstevel@tonic-gate pgcnt_t npgs, i, j; 46270Sstevel@tonic-gate uint_t pszc = pp->p_szc; 46280Sstevel@tonic-gate 46290Sstevel@tonic-gate #ifdef DEBUG 46300Sstevel@tonic-gate if (gpg_trylock_mtbf && !(gethrtime() % gpg_trylock_mtbf)) { 46310Sstevel@tonic-gate return (0); 46320Sstevel@tonic-gate } 46330Sstevel@tonic-gate #endif 46340Sstevel@tonic-gate 46350Sstevel@tonic-gate if (pp != PP_GROUPLEADER(pp, pszc)) { 46360Sstevel@tonic-gate return (0); 46370Sstevel@tonic-gate } 46380Sstevel@tonic-gate 46390Sstevel@tonic-gate retry: 46400Sstevel@tonic-gate ASSERT(PAGE_LOCKED_SE(pp, se)); 46410Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 46420Sstevel@tonic-gate if (pszc == 0) { 46430Sstevel@tonic-gate return (1); 46440Sstevel@tonic-gate } 46450Sstevel@tonic-gate npgs = page_get_pagecnt(pszc); 46460Sstevel@tonic-gate tpp = pp + 1; 46470Sstevel@tonic-gate for (i = 1; i < npgs; i++, tpp++) { 46480Sstevel@tonic-gate if (!page_trylock(tpp, se)) { 46490Sstevel@tonic-gate tpp = pp + 1; 46500Sstevel@tonic-gate for (j = 1; j < i; j++, tpp++) { 46510Sstevel@tonic-gate page_unlock(tpp); 46520Sstevel@tonic-gate } 46530Sstevel@tonic-gate return (0); 46540Sstevel@tonic-gate } 46550Sstevel@tonic-gate } 46560Sstevel@tonic-gate if (pp->p_szc != pszc) { 46570Sstevel@tonic-gate ASSERT(pp->p_szc < pszc); 46583290Sjohansen ASSERT(pp->p_vnode != NULL && !PP_ISKAS(pp) && 46590Sstevel@tonic-gate !IS_SWAPFSVP(pp->p_vnode)); 46600Sstevel@tonic-gate tpp = pp + 1; 46610Sstevel@tonic-gate for (i = 1; i < npgs; i++, tpp++) { 46620Sstevel@tonic-gate page_unlock(tpp); 46630Sstevel@tonic-gate } 46640Sstevel@tonic-gate pszc = pp->p_szc; 46650Sstevel@tonic-gate goto retry; 46660Sstevel@tonic-gate } 46670Sstevel@tonic-gate return (1); 46680Sstevel@tonic-gate } 46690Sstevel@tonic-gate 46700Sstevel@tonic-gate void 46710Sstevel@tonic-gate group_page_unlock(page_t *pp) 46720Sstevel@tonic-gate { 46730Sstevel@tonic-gate page_t *tpp; 46740Sstevel@tonic-gate pgcnt_t npgs, i; 46750Sstevel@tonic-gate 46760Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 46770Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 46780Sstevel@tonic-gate ASSERT(pp == PP_PAGEROOT(pp)); 46790Sstevel@tonic-gate npgs = page_get_pagecnt(pp->p_szc); 46800Sstevel@tonic-gate for (i = 1, tpp = pp + 1; i < npgs; i++, tpp++) { 46810Sstevel@tonic-gate page_unlock(tpp); 46820Sstevel@tonic-gate } 46830Sstevel@tonic-gate } 46840Sstevel@tonic-gate 46850Sstevel@tonic-gate /* 46860Sstevel@tonic-gate * returns 46870Sstevel@tonic-gate * 0 : on success and *nrelocp is number of relocated PAGESIZE pages 46880Sstevel@tonic-gate * ERANGE : this is not a base page 46890Sstevel@tonic-gate * EBUSY : failure to get locks on the page/pages 46900Sstevel@tonic-gate * ENOMEM : failure to obtain replacement pages 46910Sstevel@tonic-gate * EAGAIN : OBP has not yet completed its boot-time handoff to the kernel 46923253Smec * EIO : An error occurred while trying to copy the page data 46930Sstevel@tonic-gate * 46940Sstevel@tonic-gate * Return with all constituent members of target and replacement 46950Sstevel@tonic-gate * SE_EXCL locked. It is the callers responsibility to drop the 46960Sstevel@tonic-gate * locks. 46970Sstevel@tonic-gate */ 46980Sstevel@tonic-gate int 46990Sstevel@tonic-gate do_page_relocate( 47000Sstevel@tonic-gate page_t **target, 47010Sstevel@tonic-gate page_t **replacement, 47020Sstevel@tonic-gate int grouplock, 47030Sstevel@tonic-gate spgcnt_t *nrelocp, 47040Sstevel@tonic-gate lgrp_t *lgrp) 47050Sstevel@tonic-gate { 47060Sstevel@tonic-gate page_t *first_repl; 47070Sstevel@tonic-gate page_t *repl; 47080Sstevel@tonic-gate page_t *targ; 47090Sstevel@tonic-gate page_t *pl = NULL; 47100Sstevel@tonic-gate uint_t ppattr; 47110Sstevel@tonic-gate pfn_t pfn, repl_pfn; 47120Sstevel@tonic-gate uint_t szc; 47130Sstevel@tonic-gate spgcnt_t npgs, i; 47140Sstevel@tonic-gate int repl_contig = 0; 47150Sstevel@tonic-gate uint_t flags = 0; 47160Sstevel@tonic-gate spgcnt_t dofree = 0; 47170Sstevel@tonic-gate 47180Sstevel@tonic-gate *nrelocp = 0; 47190Sstevel@tonic-gate 47200Sstevel@tonic-gate #if defined(__sparc) 47210Sstevel@tonic-gate /* 47220Sstevel@tonic-gate * We need to wait till OBP has completed 47230Sstevel@tonic-gate * its boot-time handoff of its resources to the kernel 47240Sstevel@tonic-gate * before we allow page relocation 47250Sstevel@tonic-gate */ 47260Sstevel@tonic-gate if (page_relocate_ready == 0) { 47270Sstevel@tonic-gate return (EAGAIN); 47280Sstevel@tonic-gate } 47290Sstevel@tonic-gate #endif 47300Sstevel@tonic-gate 47310Sstevel@tonic-gate /* 47320Sstevel@tonic-gate * If this is not a base page, 47330Sstevel@tonic-gate * just return with 0x0 pages relocated. 47340Sstevel@tonic-gate */ 47350Sstevel@tonic-gate targ = *target; 47360Sstevel@tonic-gate ASSERT(PAGE_EXCL(targ)); 47370Sstevel@tonic-gate ASSERT(!PP_ISFREE(targ)); 47380Sstevel@tonic-gate szc = targ->p_szc; 47390Sstevel@tonic-gate ASSERT(szc < mmu_page_sizes); 47400Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_reloc[szc]); 47410Sstevel@tonic-gate pfn = targ->p_pagenum; 47420Sstevel@tonic-gate if (pfn != PFN_BASE(pfn, szc)) { 47430Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnoroot[szc]); 47440Sstevel@tonic-gate return (ERANGE); 47450Sstevel@tonic-gate } 47460Sstevel@tonic-gate 47470Sstevel@tonic-gate if ((repl = *replacement) != NULL && repl->p_szc >= szc) { 47480Sstevel@tonic-gate repl_pfn = repl->p_pagenum; 47490Sstevel@tonic-gate if (repl_pfn != PFN_BASE(repl_pfn, szc)) { 47500Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_reloc_replnoroot[szc]); 47510Sstevel@tonic-gate return (ERANGE); 47520Sstevel@tonic-gate } 47530Sstevel@tonic-gate repl_contig = 1; 47540Sstevel@tonic-gate } 47550Sstevel@tonic-gate 47560Sstevel@tonic-gate /* 47570Sstevel@tonic-gate * We must lock all members of this large page or we cannot 47580Sstevel@tonic-gate * relocate any part of it. 47590Sstevel@tonic-gate */ 47600Sstevel@tonic-gate if (grouplock != 0 && !group_page_trylock(targ, SE_EXCL)) { 47610Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnolock[targ->p_szc]); 47620Sstevel@tonic-gate return (EBUSY); 47630Sstevel@tonic-gate } 47640Sstevel@tonic-gate 47650Sstevel@tonic-gate /* 47660Sstevel@tonic-gate * reread szc it could have been decreased before 47670Sstevel@tonic-gate * group_page_trylock() was done. 47680Sstevel@tonic-gate */ 47690Sstevel@tonic-gate szc = targ->p_szc; 47700Sstevel@tonic-gate ASSERT(szc < mmu_page_sizes); 47710Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_reloc[szc]); 47720Sstevel@tonic-gate ASSERT(pfn == PFN_BASE(pfn, szc)); 47730Sstevel@tonic-gate 47740Sstevel@tonic-gate npgs = page_get_pagecnt(targ->p_szc); 47750Sstevel@tonic-gate 47760Sstevel@tonic-gate if (repl == NULL) { 47770Sstevel@tonic-gate dofree = npgs; /* Size of target page in MMU pages */ 47780Sstevel@tonic-gate if (!page_create_wait(dofree, 0)) { 47790Sstevel@tonic-gate if (grouplock != 0) { 47800Sstevel@tonic-gate group_page_unlock(targ); 47810Sstevel@tonic-gate } 47820Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnomem[szc]); 47830Sstevel@tonic-gate return (ENOMEM); 47840Sstevel@tonic-gate } 47850Sstevel@tonic-gate 47860Sstevel@tonic-gate /* 47870Sstevel@tonic-gate * seg kmem pages require that the target and replacement 47880Sstevel@tonic-gate * page be the same pagesize. 47890Sstevel@tonic-gate */ 47903290Sjohansen flags = (VN_ISKAS(targ->p_vnode)) ? PGR_SAMESZC : 0; 47910Sstevel@tonic-gate repl = page_get_replacement_page(targ, lgrp, flags); 47920Sstevel@tonic-gate if (repl == NULL) { 47930Sstevel@tonic-gate if (grouplock != 0) { 47940Sstevel@tonic-gate group_page_unlock(targ); 47950Sstevel@tonic-gate } 47960Sstevel@tonic-gate page_create_putback(dofree); 47970Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnomem[szc]); 47980Sstevel@tonic-gate return (ENOMEM); 47990Sstevel@tonic-gate } 48000Sstevel@tonic-gate } 48010Sstevel@tonic-gate #ifdef DEBUG 48020Sstevel@tonic-gate else { 48030Sstevel@tonic-gate ASSERT(PAGE_LOCKED(repl)); 48040Sstevel@tonic-gate } 48050Sstevel@tonic-gate #endif /* DEBUG */ 48060Sstevel@tonic-gate 48070Sstevel@tonic-gate #if defined(__sparc) 48080Sstevel@tonic-gate /* 48090Sstevel@tonic-gate * Let hat_page_relocate() complete the relocation if it's kernel page 48100Sstevel@tonic-gate */ 48113290Sjohansen if (VN_ISKAS(targ->p_vnode)) { 48120Sstevel@tonic-gate *replacement = repl; 48130Sstevel@tonic-gate if (hat_page_relocate(target, replacement, nrelocp) != 0) { 48140Sstevel@tonic-gate if (grouplock != 0) { 48150Sstevel@tonic-gate group_page_unlock(targ); 48160Sstevel@tonic-gate } 48170Sstevel@tonic-gate if (dofree) { 48180Sstevel@tonic-gate *replacement = NULL; 48190Sstevel@tonic-gate page_free_replacement_page(repl); 48200Sstevel@tonic-gate page_create_putback(dofree); 48210Sstevel@tonic-gate } 48220Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_krelocfail[szc]); 48230Sstevel@tonic-gate return (EAGAIN); 48240Sstevel@tonic-gate } 48250Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocok[szc]); 48260Sstevel@tonic-gate return (0); 48270Sstevel@tonic-gate } 48280Sstevel@tonic-gate #else 48290Sstevel@tonic-gate #if defined(lint) 48300Sstevel@tonic-gate dofree = dofree; 48310Sstevel@tonic-gate #endif 48320Sstevel@tonic-gate #endif 48330Sstevel@tonic-gate 48340Sstevel@tonic-gate first_repl = repl; 48350Sstevel@tonic-gate 48360Sstevel@tonic-gate for (i = 0; i < npgs; i++) { 48370Sstevel@tonic-gate ASSERT(PAGE_EXCL(targ)); 48382414Saguzovsk ASSERT(targ->p_slckcnt == 0); 48392414Saguzovsk ASSERT(repl->p_slckcnt == 0); 48400Sstevel@tonic-gate 48410Sstevel@tonic-gate (void) hat_pageunload(targ, HAT_FORCE_PGUNLOAD); 48420Sstevel@tonic-gate 48430Sstevel@tonic-gate ASSERT(hat_page_getshare(targ) == 0); 48440Sstevel@tonic-gate ASSERT(!PP_ISFREE(targ)); 48450Sstevel@tonic-gate ASSERT(targ->p_pagenum == (pfn + i)); 48460Sstevel@tonic-gate ASSERT(repl_contig == 0 || 48470Sstevel@tonic-gate repl->p_pagenum == (repl_pfn + i)); 48480Sstevel@tonic-gate 48490Sstevel@tonic-gate /* 48500Sstevel@tonic-gate * Copy the page contents and attributes then 48510Sstevel@tonic-gate * relocate the page in the page hash. 48520Sstevel@tonic-gate */ 48533253Smec if (ppcopy(targ, repl) == 0) { 48543253Smec targ = *target; 48553253Smec repl = first_repl; 48563253Smec VM_STAT_ADD(vmm_vmstats.ppr_copyfail); 48573253Smec if (grouplock != 0) { 48583253Smec group_page_unlock(targ); 48593253Smec } 48603253Smec if (dofree) { 48613253Smec *replacement = NULL; 48623253Smec page_free_replacement_page(repl); 48633253Smec page_create_putback(dofree); 48643253Smec } 48653253Smec return (EIO); 48663253Smec } 48673253Smec 48683253Smec targ++; 48693253Smec if (repl_contig != 0) { 48703253Smec repl++; 48713253Smec } else { 48723253Smec repl = repl->p_next; 48733253Smec } 48743253Smec } 48753253Smec 48763253Smec repl = first_repl; 48773253Smec targ = *target; 48783253Smec 48793253Smec for (i = 0; i < npgs; i++) { 48800Sstevel@tonic-gate ppattr = hat_page_getattr(targ, (P_MOD | P_REF | P_RO)); 48817718SJason.Beloro@Sun.COM page_clr_all_props(repl, 0); 48820Sstevel@tonic-gate page_set_props(repl, ppattr); 48830Sstevel@tonic-gate page_relocate_hash(repl, targ); 48840Sstevel@tonic-gate 48850Sstevel@tonic-gate ASSERT(hat_page_getshare(targ) == 0); 48860Sstevel@tonic-gate ASSERT(hat_page_getshare(repl) == 0); 48870Sstevel@tonic-gate /* 48880Sstevel@tonic-gate * Now clear the props on targ, after the 48890Sstevel@tonic-gate * page_relocate_hash(), they no longer 48900Sstevel@tonic-gate * have any meaning. 48910Sstevel@tonic-gate */ 48927718SJason.Beloro@Sun.COM page_clr_all_props(targ, 0); 48930Sstevel@tonic-gate ASSERT(targ->p_next == targ); 48940Sstevel@tonic-gate ASSERT(targ->p_prev == targ); 48950Sstevel@tonic-gate page_list_concat(&pl, &targ); 48960Sstevel@tonic-gate 48970Sstevel@tonic-gate targ++; 48980Sstevel@tonic-gate if (repl_contig != 0) { 48990Sstevel@tonic-gate repl++; 49000Sstevel@tonic-gate } else { 49010Sstevel@tonic-gate repl = repl->p_next; 49020Sstevel@tonic-gate } 49030Sstevel@tonic-gate } 49040Sstevel@tonic-gate /* assert that we have come full circle with repl */ 49050Sstevel@tonic-gate ASSERT(repl_contig == 1 || first_repl == repl); 49060Sstevel@tonic-gate 49070Sstevel@tonic-gate *target = pl; 49080Sstevel@tonic-gate if (*replacement == NULL) { 49090Sstevel@tonic-gate ASSERT(first_repl == repl); 49100Sstevel@tonic-gate *replacement = repl; 49110Sstevel@tonic-gate } 49120Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocok[szc]); 49130Sstevel@tonic-gate *nrelocp = npgs; 49140Sstevel@tonic-gate return (0); 49150Sstevel@tonic-gate } 49160Sstevel@tonic-gate /* 49170Sstevel@tonic-gate * On success returns 0 and *nrelocp the number of PAGESIZE pages relocated. 49180Sstevel@tonic-gate */ 49190Sstevel@tonic-gate int 49200Sstevel@tonic-gate page_relocate( 49210Sstevel@tonic-gate page_t **target, 49220Sstevel@tonic-gate page_t **replacement, 49230Sstevel@tonic-gate int grouplock, 49240Sstevel@tonic-gate int freetarget, 49250Sstevel@tonic-gate spgcnt_t *nrelocp, 49260Sstevel@tonic-gate lgrp_t *lgrp) 49270Sstevel@tonic-gate { 49280Sstevel@tonic-gate spgcnt_t ret; 49290Sstevel@tonic-gate 49300Sstevel@tonic-gate /* do_page_relocate returns 0 on success or errno value */ 49310Sstevel@tonic-gate ret = do_page_relocate(target, replacement, grouplock, nrelocp, lgrp); 49320Sstevel@tonic-gate 49330Sstevel@tonic-gate if (ret != 0 || freetarget == 0) { 49340Sstevel@tonic-gate return (ret); 49350Sstevel@tonic-gate } 49360Sstevel@tonic-gate if (*nrelocp == 1) { 49370Sstevel@tonic-gate ASSERT(*target != NULL); 49380Sstevel@tonic-gate page_free(*target, 1); 49390Sstevel@tonic-gate } else { 49400Sstevel@tonic-gate page_t *tpp = *target; 49410Sstevel@tonic-gate uint_t szc = tpp->p_szc; 49420Sstevel@tonic-gate pgcnt_t npgs = page_get_pagecnt(szc); 49430Sstevel@tonic-gate ASSERT(npgs > 1); 49440Sstevel@tonic-gate ASSERT(szc != 0); 49450Sstevel@tonic-gate do { 49460Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 49470Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp)); 49480Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 49490Sstevel@tonic-gate PP_SETFREE(tpp); 49500Sstevel@tonic-gate PP_SETAGED(tpp); 49510Sstevel@tonic-gate npgs--; 49520Sstevel@tonic-gate } while ((tpp = tpp->p_next) != *target); 49530Sstevel@tonic-gate ASSERT(npgs == 0); 49540Sstevel@tonic-gate page_list_add_pages(*target, 0); 49550Sstevel@tonic-gate npgs = page_get_pagecnt(szc); 49560Sstevel@tonic-gate page_create_putback(npgs); 49570Sstevel@tonic-gate } 49580Sstevel@tonic-gate return (ret); 49590Sstevel@tonic-gate } 49600Sstevel@tonic-gate 49610Sstevel@tonic-gate /* 49620Sstevel@tonic-gate * it is up to the caller to deal with pcf accounting. 49630Sstevel@tonic-gate */ 49640Sstevel@tonic-gate void 49650Sstevel@tonic-gate page_free_replacement_page(page_t *pplist) 49660Sstevel@tonic-gate { 49670Sstevel@tonic-gate page_t *pp; 49680Sstevel@tonic-gate 49690Sstevel@tonic-gate while (pplist != NULL) { 49700Sstevel@tonic-gate /* 49710Sstevel@tonic-gate * pp_targ is a linked list. 49720Sstevel@tonic-gate */ 49730Sstevel@tonic-gate pp = pplist; 49740Sstevel@tonic-gate if (pp->p_szc == 0) { 49750Sstevel@tonic-gate page_sub(&pplist, pp); 49767718SJason.Beloro@Sun.COM page_clr_all_props(pp, 0); 49770Sstevel@tonic-gate PP_SETFREE(pp); 49780Sstevel@tonic-gate PP_SETAGED(pp); 49790Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 49800Sstevel@tonic-gate page_unlock(pp); 49810Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_replacement_page[0]); 49820Sstevel@tonic-gate } else { 49830Sstevel@tonic-gate spgcnt_t curnpgs = page_get_pagecnt(pp->p_szc); 49840Sstevel@tonic-gate page_t *tpp; 49850Sstevel@tonic-gate page_list_break(&pp, &pplist, curnpgs); 49860Sstevel@tonic-gate tpp = pp; 49870Sstevel@tonic-gate do { 49880Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 49890Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp)); 49907718SJason.Beloro@Sun.COM page_clr_all_props(tpp, 0); 49910Sstevel@tonic-gate PP_SETFREE(tpp); 49920Sstevel@tonic-gate PP_SETAGED(tpp); 49930Sstevel@tonic-gate } while ((tpp = tpp->p_next) != pp); 49940Sstevel@tonic-gate page_list_add_pages(pp, 0); 49950Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_replacement_page[1]); 49960Sstevel@tonic-gate } 49970Sstevel@tonic-gate } 49980Sstevel@tonic-gate } 49990Sstevel@tonic-gate 50000Sstevel@tonic-gate /* 50010Sstevel@tonic-gate * Relocate target to non-relocatable replacement page. 50020Sstevel@tonic-gate */ 50030Sstevel@tonic-gate int 50040Sstevel@tonic-gate page_relocate_cage(page_t **target, page_t **replacement) 50050Sstevel@tonic-gate { 50060Sstevel@tonic-gate page_t *tpp, *rpp; 50070Sstevel@tonic-gate spgcnt_t pgcnt, npgs; 50080Sstevel@tonic-gate int result; 50090Sstevel@tonic-gate 50100Sstevel@tonic-gate tpp = *target; 50110Sstevel@tonic-gate 50120Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 50130Sstevel@tonic-gate ASSERT(tpp->p_szc == 0); 50140Sstevel@tonic-gate 50150Sstevel@tonic-gate pgcnt = btop(page_get_pagesize(tpp->p_szc)); 50160Sstevel@tonic-gate 50170Sstevel@tonic-gate do { 50180Sstevel@tonic-gate (void) page_create_wait(pgcnt, PG_WAIT | PG_NORELOC); 50190Sstevel@tonic-gate rpp = page_get_replacement_page(tpp, NULL, PGR_NORELOC); 50200Sstevel@tonic-gate if (rpp == NULL) { 50210Sstevel@tonic-gate page_create_putback(pgcnt); 50220Sstevel@tonic-gate kcage_cageout_wakeup(); 50230Sstevel@tonic-gate } 50240Sstevel@tonic-gate } while (rpp == NULL); 50250Sstevel@tonic-gate 50260Sstevel@tonic-gate ASSERT(PP_ISNORELOC(rpp)); 50270Sstevel@tonic-gate 50280Sstevel@tonic-gate result = page_relocate(&tpp, &rpp, 0, 1, &npgs, NULL); 50290Sstevel@tonic-gate 50300Sstevel@tonic-gate if (result == 0) { 50310Sstevel@tonic-gate *replacement = rpp; 50320Sstevel@tonic-gate if (pgcnt != npgs) 50330Sstevel@tonic-gate panic("page_relocate_cage: partial relocation"); 50340Sstevel@tonic-gate } 50350Sstevel@tonic-gate 50360Sstevel@tonic-gate return (result); 50370Sstevel@tonic-gate } 50380Sstevel@tonic-gate 50390Sstevel@tonic-gate /* 50400Sstevel@tonic-gate * Release the page lock on a page, place on cachelist 50410Sstevel@tonic-gate * tail if no longer mapped. Caller can let us know if 50420Sstevel@tonic-gate * the page is known to be clean. 50430Sstevel@tonic-gate */ 50440Sstevel@tonic-gate int 50450Sstevel@tonic-gate page_release(page_t *pp, int checkmod) 50460Sstevel@tonic-gate { 50470Sstevel@tonic-gate int status; 50480Sstevel@tonic-gate 50490Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp) && !PP_ISFREE(pp) && 50503559Smec (pp->p_vnode != NULL)); 50510Sstevel@tonic-gate 50520Sstevel@tonic-gate if (!hat_page_is_mapped(pp) && !IS_SWAPVP(pp->p_vnode) && 50530Sstevel@tonic-gate ((PAGE_SHARED(pp) && page_tryupgrade(pp)) || PAGE_EXCL(pp)) && 50540Sstevel@tonic-gate pp->p_lckcnt == 0 && pp->p_cowcnt == 0 && 50550Sstevel@tonic-gate !hat_page_is_mapped(pp)) { 50560Sstevel@tonic-gate 50570Sstevel@tonic-gate /* 50580Sstevel@tonic-gate * If page is modified, unlock it 50590Sstevel@tonic-gate * 50600Sstevel@tonic-gate * (p_nrm & P_MOD) bit has the latest stuff because: 50610Sstevel@tonic-gate * (1) We found that this page doesn't have any mappings 50620Sstevel@tonic-gate * _after_ holding SE_EXCL and 50630Sstevel@tonic-gate * (2) We didn't drop SE_EXCL lock after the check in (1) 50640Sstevel@tonic-gate */ 50650Sstevel@tonic-gate if (checkmod && hat_ismod(pp)) { 50660Sstevel@tonic-gate page_unlock(pp); 50670Sstevel@tonic-gate status = PGREL_MOD; 50680Sstevel@tonic-gate } else { 50690Sstevel@tonic-gate /*LINTED: constant in conditional context*/ 50700Sstevel@tonic-gate VN_DISPOSE(pp, B_FREE, 0, kcred); 50710Sstevel@tonic-gate status = PGREL_CLEAN; 50720Sstevel@tonic-gate } 50730Sstevel@tonic-gate } else { 50740Sstevel@tonic-gate page_unlock(pp); 50750Sstevel@tonic-gate status = PGREL_NOTREL; 50760Sstevel@tonic-gate } 50770Sstevel@tonic-gate return (status); 50780Sstevel@tonic-gate } 50790Sstevel@tonic-gate 5080917Selowe /* 5081917Selowe * Given a constituent page, try to demote the large page on the freelist. 5082917Selowe * 5083917Selowe * Returns nonzero if the page could be demoted successfully. Returns with 5084917Selowe * the constituent page still locked. 5085917Selowe */ 5086917Selowe int 5087917Selowe page_try_demote_free_pages(page_t *pp) 5088917Selowe { 5089917Selowe page_t *rootpp = pp; 5090917Selowe pfn_t pfn = page_pptonum(pp); 5091917Selowe spgcnt_t npgs; 5092917Selowe uint_t szc = pp->p_szc; 5093917Selowe 5094917Selowe ASSERT(PP_ISFREE(pp)); 5095917Selowe ASSERT(PAGE_EXCL(pp)); 5096917Selowe 5097917Selowe /* 5098917Selowe * Adjust rootpp and lock it, if `pp' is not the base 5099917Selowe * constituent page. 5100917Selowe */ 5101917Selowe npgs = page_get_pagecnt(pp->p_szc); 5102917Selowe if (npgs == 1) { 5103917Selowe return (0); 5104917Selowe } 5105917Selowe 5106917Selowe if (!IS_P2ALIGNED(pfn, npgs)) { 5107917Selowe pfn = P2ALIGN(pfn, npgs); 5108917Selowe rootpp = page_numtopp_nolock(pfn); 5109917Selowe } 5110917Selowe 5111917Selowe if (pp != rootpp && !page_trylock(rootpp, SE_EXCL)) { 5112917Selowe return (0); 5113917Selowe } 5114917Selowe 5115917Selowe if (rootpp->p_szc != szc) { 5116917Selowe if (pp != rootpp) 5117917Selowe page_unlock(rootpp); 5118917Selowe return (0); 5119917Selowe } 5120917Selowe 5121917Selowe page_demote_free_pages(rootpp); 5122917Selowe 5123917Selowe if (pp != rootpp) 5124917Selowe page_unlock(rootpp); 5125917Selowe 5126917Selowe ASSERT(PP_ISFREE(pp)); 5127917Selowe ASSERT(PAGE_EXCL(pp)); 5128917Selowe return (1); 5129917Selowe } 5130917Selowe 5131917Selowe /* 5132917Selowe * Given a constituent page, try to demote the large page. 5133917Selowe * 5134917Selowe * Returns nonzero if the page could be demoted successfully. Returns with 5135917Selowe * the constituent page still locked. 5136917Selowe */ 51370Sstevel@tonic-gate int 51380Sstevel@tonic-gate page_try_demote_pages(page_t *pp) 51390Sstevel@tonic-gate { 51400Sstevel@tonic-gate page_t *tpp, *rootpp = pp; 51410Sstevel@tonic-gate pfn_t pfn = page_pptonum(pp); 51420Sstevel@tonic-gate spgcnt_t i, npgs; 51430Sstevel@tonic-gate uint_t szc = pp->p_szc; 51440Sstevel@tonic-gate vnode_t *vp = pp->p_vnode; 51450Sstevel@tonic-gate 5146917Selowe ASSERT(PAGE_EXCL(pp)); 51470Sstevel@tonic-gate 51480Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[0]); 51490Sstevel@tonic-gate 5150917Selowe if (pp->p_szc == 0) { 51510Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[1]); 51520Sstevel@tonic-gate return (1); 51530Sstevel@tonic-gate } 51540Sstevel@tonic-gate 51553290Sjohansen if (vp != NULL && !IS_SWAPFSVP(vp) && !VN_ISKAS(vp)) { 51560Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[2]); 5157917Selowe page_demote_vp_pages(pp); 51580Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 51590Sstevel@tonic-gate return (1); 51600Sstevel@tonic-gate } 51610Sstevel@tonic-gate 51620Sstevel@tonic-gate /* 5163917Selowe * Adjust rootpp if passed in is not the base 51640Sstevel@tonic-gate * constituent page. 51650Sstevel@tonic-gate */ 5166917Selowe npgs = page_get_pagecnt(pp->p_szc); 51670Sstevel@tonic-gate ASSERT(npgs > 1); 51680Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, npgs)) { 51690Sstevel@tonic-gate pfn = P2ALIGN(pfn, npgs); 51700Sstevel@tonic-gate rootpp = page_numtopp_nolock(pfn); 51710Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[3]); 51720Sstevel@tonic-gate ASSERT(rootpp->p_vnode != NULL); 51730Sstevel@tonic-gate ASSERT(rootpp->p_szc == szc); 51740Sstevel@tonic-gate } 51750Sstevel@tonic-gate 51760Sstevel@tonic-gate /* 51770Sstevel@tonic-gate * We can't demote kernel pages since we can't hat_unload() 51780Sstevel@tonic-gate * the mappings. 51790Sstevel@tonic-gate */ 51803290Sjohansen if (VN_ISKAS(rootpp->p_vnode)) 51810Sstevel@tonic-gate return (0); 51820Sstevel@tonic-gate 51830Sstevel@tonic-gate /* 51840Sstevel@tonic-gate * Attempt to lock all constituent pages except the page passed 51850Sstevel@tonic-gate * in since it's already locked. 51860Sstevel@tonic-gate */ 5187414Skchow for (tpp = rootpp, i = 0; i < npgs; i++, tpp++) { 51880Sstevel@tonic-gate ASSERT(!PP_ISFREE(tpp)); 51890Sstevel@tonic-gate ASSERT(tpp->p_vnode != NULL); 51900Sstevel@tonic-gate 51910Sstevel@tonic-gate if (tpp != pp && !page_trylock(tpp, SE_EXCL)) 51920Sstevel@tonic-gate break; 51930Sstevel@tonic-gate ASSERT(tpp->p_szc == rootpp->p_szc); 51940Sstevel@tonic-gate ASSERT(page_pptonum(tpp) == page_pptonum(rootpp) + i); 51950Sstevel@tonic-gate } 51960Sstevel@tonic-gate 51970Sstevel@tonic-gate /* 5198917Selowe * If we failed to lock them all then unlock what we have 5199917Selowe * locked so far and bail. 52000Sstevel@tonic-gate */ 52010Sstevel@tonic-gate if (i < npgs) { 52020Sstevel@tonic-gate tpp = rootpp; 52030Sstevel@tonic-gate while (i-- > 0) { 52040Sstevel@tonic-gate if (tpp != pp) 52050Sstevel@tonic-gate page_unlock(tpp); 5206414Skchow tpp++; 52070Sstevel@tonic-gate } 52080Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[4]); 52090Sstevel@tonic-gate return (0); 52100Sstevel@tonic-gate } 52110Sstevel@tonic-gate 5212414Skchow for (tpp = rootpp, i = 0; i < npgs; i++, tpp++) { 52130Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 52142414Saguzovsk ASSERT(tpp->p_slckcnt == 0); 5215917Selowe (void) hat_pageunload(tpp, HAT_FORCE_PGUNLOAD); 52160Sstevel@tonic-gate tpp->p_szc = 0; 52170Sstevel@tonic-gate } 52180Sstevel@tonic-gate 52190Sstevel@tonic-gate /* 52200Sstevel@tonic-gate * Unlock all pages except the page passed in. 52210Sstevel@tonic-gate */ 5222414Skchow for (tpp = rootpp, i = 0; i < npgs; i++, tpp++) { 52230Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp)); 52240Sstevel@tonic-gate if (tpp != pp) 52250Sstevel@tonic-gate page_unlock(tpp); 52260Sstevel@tonic-gate } 5227917Selowe 52280Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[5]); 52290Sstevel@tonic-gate return (1); 52300Sstevel@tonic-gate } 52310Sstevel@tonic-gate 52320Sstevel@tonic-gate /* 52330Sstevel@tonic-gate * Called by page_free() and page_destroy() to demote the page size code 52340Sstevel@tonic-gate * (p_szc) to 0 (since we can't just put a single PAGESIZE page with non zero 52350Sstevel@tonic-gate * p_szc on free list, neither can we just clear p_szc of a single page_t 52360Sstevel@tonic-gate * within a large page since it will break other code that relies on p_szc 52370Sstevel@tonic-gate * being the same for all page_t's of a large page). Anonymous pages should 52380Sstevel@tonic-gate * never end up here because anon_map_getpages() cannot deal with p_szc 52390Sstevel@tonic-gate * changes after a single constituent page is locked. While anonymous or 52400Sstevel@tonic-gate * kernel large pages are demoted or freed the entire large page at a time 52410Sstevel@tonic-gate * with all constituent pages locked EXCL for the file system pages we 52420Sstevel@tonic-gate * have to be able to demote a large page (i.e. decrease all constituent pages 52430Sstevel@tonic-gate * p_szc) with only just an EXCL lock on one of constituent pages. The reason 52440Sstevel@tonic-gate * we can easily deal with anonymous page demotion the entire large page at a 52450Sstevel@tonic-gate * time is that those operation originate at address space level and concern 52460Sstevel@tonic-gate * the entire large page region with actual demotion only done when pages are 52470Sstevel@tonic-gate * not shared with any other processes (therefore we can always get EXCL lock 52480Sstevel@tonic-gate * on all anonymous constituent pages after clearing segment page 52490Sstevel@tonic-gate * cache). However file system pages can be truncated or invalidated at a 52500Sstevel@tonic-gate * PAGESIZE level from the file system side and end up in page_free() or 52510Sstevel@tonic-gate * page_destroy() (we also allow only part of the large page to be SOFTLOCKed 52525331Samw * and therefore pageout should be able to demote a large page by EXCL locking 52530Sstevel@tonic-gate * any constituent page that is not under SOFTLOCK). In those cases we cannot 52540Sstevel@tonic-gate * rely on being able to lock EXCL all constituent pages. 52550Sstevel@tonic-gate * 52560Sstevel@tonic-gate * To prevent szc changes on file system pages one has to lock all constituent 52570Sstevel@tonic-gate * pages at least SHARED (or call page_szc_lock()). The only subsystem that 52580Sstevel@tonic-gate * doesn't rely on locking all constituent pages (or using page_szc_lock()) to 52590Sstevel@tonic-gate * prevent szc changes is hat layer that uses its own page level mlist 52600Sstevel@tonic-gate * locks. hat assumes that szc doesn't change after mlist lock for a page is 52610Sstevel@tonic-gate * taken. Therefore we need to change szc under hat level locks if we only 52620Sstevel@tonic-gate * have an EXCL lock on a single constituent page and hat still references any 52630Sstevel@tonic-gate * of constituent pages. (Note we can't "ignore" hat layer by simply 52640Sstevel@tonic-gate * hat_pageunload() all constituent pages without having EXCL locks on all of 52650Sstevel@tonic-gate * constituent pages). We use hat_page_demote() call to safely demote szc of 52660Sstevel@tonic-gate * all constituent pages under hat locks when we only have an EXCL lock on one 52670Sstevel@tonic-gate * of constituent pages. 52680Sstevel@tonic-gate * 52690Sstevel@tonic-gate * This routine calls page_szc_lock() before calling hat_page_demote() to 52700Sstevel@tonic-gate * allow segvn in one special case not to lock all constituent pages SHARED 52715331Samw * before calling hat_memload_array() that relies on p_szc not changing even 52720Sstevel@tonic-gate * before hat level mlist lock is taken. In that case segvn uses 52735331Samw * page_szc_lock() to prevent hat_page_demote() changing p_szc values. 52740Sstevel@tonic-gate * 52750Sstevel@tonic-gate * Anonymous or kernel page demotion still has to lock all pages exclusively 52760Sstevel@tonic-gate * and do hat_pageunload() on all constituent pages before demoting the page 52770Sstevel@tonic-gate * therefore there's no need for anonymous or kernel page demotion to use 52780Sstevel@tonic-gate * hat_page_demote() mechanism. 52790Sstevel@tonic-gate * 52800Sstevel@tonic-gate * hat_page_demote() removes all large mappings that map pp and then decreases 52810Sstevel@tonic-gate * p_szc starting from the last constituent page of the large page. By working 52820Sstevel@tonic-gate * from the tail of a large page in pfn decreasing order allows one looking at 52830Sstevel@tonic-gate * the root page to know that hat_page_demote() is done for root's szc area. 52840Sstevel@tonic-gate * e.g. if a root page has szc 1 one knows it only has to lock all constituent 52850Sstevel@tonic-gate * pages within szc 1 area to prevent szc changes because hat_page_demote() 52860Sstevel@tonic-gate * that started on this page when it had szc > 1 is done for this szc 1 area. 52870Sstevel@tonic-gate * 52885331Samw * We are guaranteed that all constituent pages of pp's large page belong to 52890Sstevel@tonic-gate * the same vnode with the consecutive offsets increasing in the direction of 52900Sstevel@tonic-gate * the pfn i.e. the identity of constituent pages can't change until their 52910Sstevel@tonic-gate * p_szc is decreased. Therefore it's safe for hat_page_demote() to remove 52920Sstevel@tonic-gate * large mappings to pp even though we don't lock any constituent page except 52930Sstevel@tonic-gate * pp (i.e. we won't unload e.g. kernel locked page). 52940Sstevel@tonic-gate */ 52950Sstevel@tonic-gate static void 52960Sstevel@tonic-gate page_demote_vp_pages(page_t *pp) 52970Sstevel@tonic-gate { 52980Sstevel@tonic-gate kmutex_t *mtx; 52990Sstevel@tonic-gate 53000Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 53010Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 53020Sstevel@tonic-gate ASSERT(pp->p_vnode != NULL); 53030Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(pp->p_vnode)); 53043290Sjohansen ASSERT(!PP_ISKAS(pp)); 53050Sstevel@tonic-gate 53060Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_demote_pages[0]); 53070Sstevel@tonic-gate 53080Sstevel@tonic-gate mtx = page_szc_lock(pp); 53090Sstevel@tonic-gate if (mtx != NULL) { 53100Sstevel@tonic-gate hat_page_demote(pp); 53110Sstevel@tonic-gate mutex_exit(mtx); 53120Sstevel@tonic-gate } 53130Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 53140Sstevel@tonic-gate } 53150Sstevel@tonic-gate 53160Sstevel@tonic-gate /* 53170Sstevel@tonic-gate * Mark any existing pages for migration in the given range 53180Sstevel@tonic-gate */ 53190Sstevel@tonic-gate void 53200Sstevel@tonic-gate page_mark_migrate(struct seg *seg, caddr_t addr, size_t len, 53210Sstevel@tonic-gate struct anon_map *amp, ulong_t anon_index, vnode_t *vp, 53220Sstevel@tonic-gate u_offset_t vnoff, int rflag) 53230Sstevel@tonic-gate { 53240Sstevel@tonic-gate struct anon *ap; 53250Sstevel@tonic-gate vnode_t *curvp; 53260Sstevel@tonic-gate lgrp_t *from; 53270Sstevel@tonic-gate pgcnt_t i; 53280Sstevel@tonic-gate pgcnt_t nlocked; 53290Sstevel@tonic-gate u_offset_t off; 53300Sstevel@tonic-gate pfn_t pfn; 53310Sstevel@tonic-gate size_t pgsz; 53320Sstevel@tonic-gate size_t segpgsz; 53330Sstevel@tonic-gate pgcnt_t pages; 53340Sstevel@tonic-gate uint_t pszc; 53350Sstevel@tonic-gate page_t **ppa; 53360Sstevel@tonic-gate pgcnt_t ppa_nentries; 53370Sstevel@tonic-gate page_t *pp; 53380Sstevel@tonic-gate caddr_t va; 53390Sstevel@tonic-gate ulong_t an_idx; 53400Sstevel@tonic-gate anon_sync_obj_t cookie; 53410Sstevel@tonic-gate 53420Sstevel@tonic-gate ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 53430Sstevel@tonic-gate 53440Sstevel@tonic-gate /* 53450Sstevel@tonic-gate * Don't do anything if don't need to do lgroup optimizations 53460Sstevel@tonic-gate * on this system 53470Sstevel@tonic-gate */ 53480Sstevel@tonic-gate if (!lgrp_optimizations()) 53490Sstevel@tonic-gate return; 53500Sstevel@tonic-gate 53510Sstevel@tonic-gate /* 53520Sstevel@tonic-gate * Align address and length to (potentially large) page boundary 53530Sstevel@tonic-gate */ 53540Sstevel@tonic-gate segpgsz = page_get_pagesize(seg->s_szc); 53550Sstevel@tonic-gate addr = (caddr_t)P2ALIGN((uintptr_t)addr, segpgsz); 53560Sstevel@tonic-gate if (rflag) 53570Sstevel@tonic-gate len = P2ROUNDUP(len, segpgsz); 53580Sstevel@tonic-gate 53590Sstevel@tonic-gate /* 53605331Samw * Allocate page array to accommodate largest page size 53610Sstevel@tonic-gate */ 53620Sstevel@tonic-gate pgsz = page_get_pagesize(page_num_pagesizes() - 1); 53630Sstevel@tonic-gate ppa_nentries = btop(pgsz); 53640Sstevel@tonic-gate ppa = kmem_zalloc(ppa_nentries * sizeof (page_t *), KM_SLEEP); 53650Sstevel@tonic-gate 53660Sstevel@tonic-gate /* 53670Sstevel@tonic-gate * Do one (large) page at a time 53680Sstevel@tonic-gate */ 53690Sstevel@tonic-gate va = addr; 53700Sstevel@tonic-gate while (va < addr + len) { 53710Sstevel@tonic-gate /* 53720Sstevel@tonic-gate * Lookup (root) page for vnode and offset corresponding to 53730Sstevel@tonic-gate * this virtual address 53740Sstevel@tonic-gate * Try anonmap first since there may be copy-on-write 53750Sstevel@tonic-gate * pages, but initialize vnode pointer and offset using 53760Sstevel@tonic-gate * vnode arguments just in case there isn't an amp. 53770Sstevel@tonic-gate */ 53780Sstevel@tonic-gate curvp = vp; 53790Sstevel@tonic-gate off = vnoff + va - seg->s_base; 53800Sstevel@tonic-gate if (amp) { 53810Sstevel@tonic-gate ANON_LOCK_ENTER(&->a_rwlock, RW_READER); 53820Sstevel@tonic-gate an_idx = anon_index + seg_page(seg, va); 53830Sstevel@tonic-gate anon_array_enter(amp, an_idx, &cookie); 53840Sstevel@tonic-gate ap = anon_get_ptr(amp->ahp, an_idx); 53850Sstevel@tonic-gate if (ap) 53860Sstevel@tonic-gate swap_xlate(ap, &curvp, &off); 53870Sstevel@tonic-gate anon_array_exit(&cookie); 53880Sstevel@tonic-gate ANON_LOCK_EXIT(&->a_rwlock); 53890Sstevel@tonic-gate } 53900Sstevel@tonic-gate 53910Sstevel@tonic-gate pp = NULL; 53920Sstevel@tonic-gate if (curvp) 53930Sstevel@tonic-gate pp = page_lookup(curvp, off, SE_SHARED); 53940Sstevel@tonic-gate 53950Sstevel@tonic-gate /* 53960Sstevel@tonic-gate * If there isn't a page at this virtual address, 53970Sstevel@tonic-gate * skip to next page 53980Sstevel@tonic-gate */ 53990Sstevel@tonic-gate if (pp == NULL) { 54000Sstevel@tonic-gate va += PAGESIZE; 54010Sstevel@tonic-gate continue; 54020Sstevel@tonic-gate } 54030Sstevel@tonic-gate 54040Sstevel@tonic-gate /* 54050Sstevel@tonic-gate * Figure out which lgroup this page is in for kstats 54060Sstevel@tonic-gate */ 54070Sstevel@tonic-gate pfn = page_pptonum(pp); 54080Sstevel@tonic-gate from = lgrp_pfn_to_lgrp(pfn); 54090Sstevel@tonic-gate 54100Sstevel@tonic-gate /* 54110Sstevel@tonic-gate * Get page size, and round up and skip to next page boundary 54120Sstevel@tonic-gate * if unaligned address 54130Sstevel@tonic-gate */ 54140Sstevel@tonic-gate pszc = pp->p_szc; 54150Sstevel@tonic-gate pgsz = page_get_pagesize(pszc); 54160Sstevel@tonic-gate pages = btop(pgsz); 54170Sstevel@tonic-gate if (!IS_P2ALIGNED(va, pgsz) || 54180Sstevel@tonic-gate !IS_P2ALIGNED(pfn, pages) || 54190Sstevel@tonic-gate pgsz > segpgsz) { 54200Sstevel@tonic-gate pgsz = MIN(pgsz, segpgsz); 54210Sstevel@tonic-gate page_unlock(pp); 54220Sstevel@tonic-gate i = btop(P2END((uintptr_t)va, pgsz) - 54230Sstevel@tonic-gate (uintptr_t)va); 54240Sstevel@tonic-gate va = (caddr_t)P2END((uintptr_t)va, pgsz); 54250Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, i); 54260Sstevel@tonic-gate continue; 54270Sstevel@tonic-gate } 54280Sstevel@tonic-gate 54290Sstevel@tonic-gate /* 54300Sstevel@tonic-gate * Upgrade to exclusive lock on page 54310Sstevel@tonic-gate */ 54320Sstevel@tonic-gate if (!page_tryupgrade(pp)) { 54330Sstevel@tonic-gate page_unlock(pp); 54340Sstevel@tonic-gate va += pgsz; 54350Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, 54360Sstevel@tonic-gate btop(pgsz)); 54370Sstevel@tonic-gate continue; 54380Sstevel@tonic-gate } 54390Sstevel@tonic-gate 54400Sstevel@tonic-gate /* 54410Sstevel@tonic-gate * Remember pages locked exclusively and how many 54420Sstevel@tonic-gate */ 54430Sstevel@tonic-gate ppa[0] = pp; 54440Sstevel@tonic-gate nlocked = 1; 54450Sstevel@tonic-gate 54460Sstevel@tonic-gate /* 54470Sstevel@tonic-gate * Lock constituent pages if this is large page 54480Sstevel@tonic-gate */ 54490Sstevel@tonic-gate if (pages > 1) { 54500Sstevel@tonic-gate /* 54510Sstevel@tonic-gate * Lock all constituents except root page, since it 54520Sstevel@tonic-gate * should be locked already. 54530Sstevel@tonic-gate */ 54540Sstevel@tonic-gate for (i = 1; i < pages; i++) { 5455414Skchow pp++; 54560Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) { 54570Sstevel@tonic-gate break; 54580Sstevel@tonic-gate } 54590Sstevel@tonic-gate if (PP_ISFREE(pp) || 54600Sstevel@tonic-gate pp->p_szc != pszc) { 54610Sstevel@tonic-gate /* 54620Sstevel@tonic-gate * hat_page_demote() raced in with us. 54630Sstevel@tonic-gate */ 54640Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(curvp)); 54650Sstevel@tonic-gate page_unlock(pp); 54660Sstevel@tonic-gate break; 54670Sstevel@tonic-gate } 54680Sstevel@tonic-gate ppa[nlocked] = pp; 54690Sstevel@tonic-gate nlocked++; 54700Sstevel@tonic-gate } 54710Sstevel@tonic-gate } 54720Sstevel@tonic-gate 54730Sstevel@tonic-gate /* 54740Sstevel@tonic-gate * If all constituent pages couldn't be locked, 54750Sstevel@tonic-gate * unlock pages locked so far and skip to next page. 54760Sstevel@tonic-gate */ 54770Sstevel@tonic-gate if (nlocked != pages) { 54780Sstevel@tonic-gate for (i = 0; i < nlocked; i++) 54790Sstevel@tonic-gate page_unlock(ppa[i]); 54800Sstevel@tonic-gate va += pgsz; 54810Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, 54820Sstevel@tonic-gate btop(pgsz)); 54830Sstevel@tonic-gate continue; 54840Sstevel@tonic-gate } 54850Sstevel@tonic-gate 54860Sstevel@tonic-gate /* 54870Sstevel@tonic-gate * hat_page_demote() can no longer happen 54880Sstevel@tonic-gate * since last cons page had the right p_szc after 54890Sstevel@tonic-gate * all cons pages were locked. all cons pages 54900Sstevel@tonic-gate * should now have the same p_szc. 54910Sstevel@tonic-gate */ 54920Sstevel@tonic-gate 54930Sstevel@tonic-gate /* 54940Sstevel@tonic-gate * All constituent pages locked successfully, so mark 54950Sstevel@tonic-gate * large page for migration and unload the mappings of 54960Sstevel@tonic-gate * constituent pages, so a fault will occur on any part of the 54970Sstevel@tonic-gate * large page 54980Sstevel@tonic-gate */ 54990Sstevel@tonic-gate PP_SETMIGRATE(ppa[0]); 55000Sstevel@tonic-gate for (i = 0; i < nlocked; i++) { 55010Sstevel@tonic-gate pp = ppa[i]; 55020Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 55030Sstevel@tonic-gate ASSERT(hat_page_getshare(pp) == 0); 55040Sstevel@tonic-gate page_unlock(pp); 55050Sstevel@tonic-gate } 55060Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_PGS, nlocked); 55070Sstevel@tonic-gate 55080Sstevel@tonic-gate va += pgsz; 55090Sstevel@tonic-gate } 55100Sstevel@tonic-gate kmem_free(ppa, ppa_nentries * sizeof (page_t *)); 55110Sstevel@tonic-gate } 55120Sstevel@tonic-gate 55130Sstevel@tonic-gate /* 55140Sstevel@tonic-gate * Migrate any pages that have been marked for migration in the given range 55150Sstevel@tonic-gate */ 55160Sstevel@tonic-gate void 55170Sstevel@tonic-gate page_migrate( 55180Sstevel@tonic-gate struct seg *seg, 55190Sstevel@tonic-gate caddr_t addr, 55200Sstevel@tonic-gate page_t **ppa, 55210Sstevel@tonic-gate pgcnt_t npages) 55220Sstevel@tonic-gate { 55230Sstevel@tonic-gate lgrp_t *from; 55240Sstevel@tonic-gate lgrp_t *to; 55250Sstevel@tonic-gate page_t *newpp; 55260Sstevel@tonic-gate page_t *pp; 55270Sstevel@tonic-gate pfn_t pfn; 55280Sstevel@tonic-gate size_t pgsz; 55290Sstevel@tonic-gate spgcnt_t page_cnt; 55300Sstevel@tonic-gate spgcnt_t i; 55310Sstevel@tonic-gate uint_t pszc; 55320Sstevel@tonic-gate 55330Sstevel@tonic-gate ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 55340Sstevel@tonic-gate 55350Sstevel@tonic-gate while (npages > 0) { 55360Sstevel@tonic-gate pp = *ppa; 55370Sstevel@tonic-gate pszc = pp->p_szc; 55380Sstevel@tonic-gate pgsz = page_get_pagesize(pszc); 55390Sstevel@tonic-gate page_cnt = btop(pgsz); 55400Sstevel@tonic-gate 55410Sstevel@tonic-gate /* 55420Sstevel@tonic-gate * Check to see whether this page is marked for migration 55430Sstevel@tonic-gate * 55440Sstevel@tonic-gate * Assume that root page of large page is marked for 55450Sstevel@tonic-gate * migration and none of the other constituent pages 55460Sstevel@tonic-gate * are marked. This really simplifies clearing the 55470Sstevel@tonic-gate * migrate bit by not having to clear it from each 55480Sstevel@tonic-gate * constituent page. 55490Sstevel@tonic-gate * 55500Sstevel@tonic-gate * note we don't want to relocate an entire large page if 55510Sstevel@tonic-gate * someone is only using one subpage. 55520Sstevel@tonic-gate */ 55530Sstevel@tonic-gate if (npages < page_cnt) 55540Sstevel@tonic-gate break; 55550Sstevel@tonic-gate 55560Sstevel@tonic-gate /* 55570Sstevel@tonic-gate * Is it marked for migration? 55580Sstevel@tonic-gate */ 55590Sstevel@tonic-gate if (!PP_ISMIGRATE(pp)) 55600Sstevel@tonic-gate goto next; 55610Sstevel@tonic-gate 55620Sstevel@tonic-gate /* 55630Sstevel@tonic-gate * Determine lgroups that page is being migrated between 55640Sstevel@tonic-gate */ 55650Sstevel@tonic-gate pfn = page_pptonum(pp); 55660Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, page_cnt)) { 55670Sstevel@tonic-gate break; 55680Sstevel@tonic-gate } 55690Sstevel@tonic-gate from = lgrp_pfn_to_lgrp(pfn); 55700Sstevel@tonic-gate to = lgrp_mem_choose(seg, addr, pgsz); 55710Sstevel@tonic-gate 55720Sstevel@tonic-gate /* 55730Sstevel@tonic-gate * Need to get exclusive lock's to migrate 55740Sstevel@tonic-gate */ 55750Sstevel@tonic-gate for (i = 0; i < page_cnt; i++) { 55760Sstevel@tonic-gate ASSERT(PAGE_LOCKED(ppa[i])); 55770Sstevel@tonic-gate if (page_pptonum(ppa[i]) != pfn + i || 55780Sstevel@tonic-gate ppa[i]->p_szc != pszc) { 55790Sstevel@tonic-gate break; 55800Sstevel@tonic-gate } 55810Sstevel@tonic-gate if (!page_tryupgrade(ppa[i])) { 55820Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, 55830Sstevel@tonic-gate LGRP_PM_FAIL_LOCK_PGS, 55840Sstevel@tonic-gate page_cnt); 55850Sstevel@tonic-gate break; 55860Sstevel@tonic-gate } 55876032Sjj209869 55886032Sjj209869 /* 55896032Sjj209869 * Check to see whether we are trying to migrate 55906032Sjj209869 * page to lgroup where it is allocated already. 55916032Sjj209869 * If so, clear the migrate bit and skip to next 55926032Sjj209869 * page. 55936032Sjj209869 */ 55946032Sjj209869 if (i == 0 && to == from) { 55956032Sjj209869 PP_CLRMIGRATE(ppa[0]); 55966032Sjj209869 page_downgrade(ppa[0]); 55976032Sjj209869 goto next; 55986032Sjj209869 } 55996032Sjj209869 } 56006032Sjj209869 56016032Sjj209869 /* 56026032Sjj209869 * If all constituent pages couldn't be locked, 56036032Sjj209869 * unlock pages locked so far and skip to next page. 56046032Sjj209869 */ 56050Sstevel@tonic-gate if (i != page_cnt) { 56060Sstevel@tonic-gate while (--i != -1) { 56070Sstevel@tonic-gate page_downgrade(ppa[i]); 56080Sstevel@tonic-gate } 56090Sstevel@tonic-gate goto next; 56100Sstevel@tonic-gate } 56110Sstevel@tonic-gate 56120Sstevel@tonic-gate (void) page_create_wait(page_cnt, PG_WAIT); 56130Sstevel@tonic-gate newpp = page_get_replacement_page(pp, to, PGR_SAMESZC); 56140Sstevel@tonic-gate if (newpp == NULL) { 56150Sstevel@tonic-gate page_create_putback(page_cnt); 56160Sstevel@tonic-gate for (i = 0; i < page_cnt; i++) { 56170Sstevel@tonic-gate page_downgrade(ppa[i]); 56180Sstevel@tonic-gate } 56190Sstevel@tonic-gate lgrp_stat_add(to->lgrp_id, LGRP_PM_FAIL_ALLOC_PGS, 56200Sstevel@tonic-gate page_cnt); 56210Sstevel@tonic-gate goto next; 56220Sstevel@tonic-gate } 56230Sstevel@tonic-gate ASSERT(newpp->p_szc == pszc); 56240Sstevel@tonic-gate /* 56250Sstevel@tonic-gate * Clear migrate bit and relocate page 56260Sstevel@tonic-gate */ 56270Sstevel@tonic-gate PP_CLRMIGRATE(pp); 56280Sstevel@tonic-gate if (page_relocate(&pp, &newpp, 0, 1, &page_cnt, to)) { 56290Sstevel@tonic-gate panic("page_migrate: page_relocate failed"); 56300Sstevel@tonic-gate } 56310Sstevel@tonic-gate ASSERT(page_cnt * PAGESIZE == pgsz); 56320Sstevel@tonic-gate 56330Sstevel@tonic-gate /* 56340Sstevel@tonic-gate * Keep stats for number of pages migrated from and to 56350Sstevel@tonic-gate * each lgroup 56360Sstevel@tonic-gate */ 56370Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PM_SRC_PGS, page_cnt); 56380Sstevel@tonic-gate lgrp_stat_add(to->lgrp_id, LGRP_PM_DEST_PGS, page_cnt); 56390Sstevel@tonic-gate /* 56400Sstevel@tonic-gate * update the page_t array we were passed in and 56410Sstevel@tonic-gate * unlink constituent pages of a large page. 56420Sstevel@tonic-gate */ 56430Sstevel@tonic-gate for (i = 0; i < page_cnt; ++i, ++pp) { 56440Sstevel@tonic-gate ASSERT(PAGE_EXCL(newpp)); 56450Sstevel@tonic-gate ASSERT(newpp->p_szc == pszc); 56460Sstevel@tonic-gate ppa[i] = newpp; 56470Sstevel@tonic-gate pp = newpp; 56480Sstevel@tonic-gate page_sub(&newpp, pp); 56490Sstevel@tonic-gate page_downgrade(pp); 56500Sstevel@tonic-gate } 56510Sstevel@tonic-gate ASSERT(newpp == NULL); 56520Sstevel@tonic-gate next: 56530Sstevel@tonic-gate addr += pgsz; 56540Sstevel@tonic-gate ppa += page_cnt; 56550Sstevel@tonic-gate npages -= page_cnt; 56560Sstevel@tonic-gate } 56570Sstevel@tonic-gate } 56580Sstevel@tonic-gate 56590Sstevel@tonic-gate ulong_t mem_waiters = 0; 56600Sstevel@tonic-gate ulong_t max_count = 20; 56610Sstevel@tonic-gate #define MAX_DELAY 0x1ff 56620Sstevel@tonic-gate 56630Sstevel@tonic-gate /* 56640Sstevel@tonic-gate * Check if enough memory is available to proceed. 56650Sstevel@tonic-gate * Depending on system configuration and how much memory is 56660Sstevel@tonic-gate * reserved for swap we need to check against two variables. 56670Sstevel@tonic-gate * e.g. on systems with little physical swap availrmem can be 56680Sstevel@tonic-gate * more reliable indicator of how much memory is available. 56690Sstevel@tonic-gate * On systems with large phys swap freemem can be better indicator. 56700Sstevel@tonic-gate * If freemem drops below threshold level don't return an error 56710Sstevel@tonic-gate * immediately but wake up pageout to free memory and block. 56720Sstevel@tonic-gate * This is done number of times. If pageout is not able to free 56730Sstevel@tonic-gate * memory within certain time return an error. 56740Sstevel@tonic-gate * The same applies for availrmem but kmem_reap is used to 56750Sstevel@tonic-gate * free memory. 56760Sstevel@tonic-gate */ 56770Sstevel@tonic-gate int 56780Sstevel@tonic-gate page_mem_avail(pgcnt_t npages) 56790Sstevel@tonic-gate { 56800Sstevel@tonic-gate ulong_t count; 56810Sstevel@tonic-gate 56820Sstevel@tonic-gate #if defined(__i386) 56830Sstevel@tonic-gate if (freemem > desfree + npages && 56840Sstevel@tonic-gate availrmem > swapfs_reserve + npages && 56850Sstevel@tonic-gate btop(vmem_size(heap_arena, VMEM_FREE)) > tune.t_minarmem + 56860Sstevel@tonic-gate npages) 56870Sstevel@tonic-gate return (1); 56880Sstevel@tonic-gate #else 56890Sstevel@tonic-gate if (freemem > desfree + npages && 56900Sstevel@tonic-gate availrmem > swapfs_reserve + npages) 56910Sstevel@tonic-gate return (1); 56920Sstevel@tonic-gate #endif 56930Sstevel@tonic-gate 56940Sstevel@tonic-gate count = max_count; 56950Sstevel@tonic-gate atomic_add_long(&mem_waiters, 1); 56960Sstevel@tonic-gate 56970Sstevel@tonic-gate while (freemem < desfree + npages && --count) { 56980Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 56990Sstevel@tonic-gate if (delay_sig(hz + (mem_waiters & MAX_DELAY))) { 57000Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 57010Sstevel@tonic-gate return (0); 57020Sstevel@tonic-gate } 57030Sstevel@tonic-gate } 57040Sstevel@tonic-gate if (count == 0) { 57050Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 57060Sstevel@tonic-gate return (0); 57070Sstevel@tonic-gate } 57080Sstevel@tonic-gate 57090Sstevel@tonic-gate count = max_count; 57100Sstevel@tonic-gate while (availrmem < swapfs_reserve + npages && --count) { 57110Sstevel@tonic-gate kmem_reap(); 57120Sstevel@tonic-gate if (delay_sig(hz + (mem_waiters & MAX_DELAY))) { 57130Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 57140Sstevel@tonic-gate return (0); 57150Sstevel@tonic-gate } 57160Sstevel@tonic-gate } 57170Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 57180Sstevel@tonic-gate if (count == 0) 57190Sstevel@tonic-gate return (0); 57200Sstevel@tonic-gate 57210Sstevel@tonic-gate #if defined(__i386) 57220Sstevel@tonic-gate if (btop(vmem_size(heap_arena, VMEM_FREE)) < 57230Sstevel@tonic-gate tune.t_minarmem + npages) 57240Sstevel@tonic-gate return (0); 57250Sstevel@tonic-gate #endif 57260Sstevel@tonic-gate return (1); 57270Sstevel@tonic-gate } 57280Sstevel@tonic-gate 57292048Sstans #define MAX_CNT 60 /* max num of iterations */ 57302048Sstans /* 57312048Sstans * Reclaim/reserve availrmem for npages. 57322048Sstans * If there is not enough memory start reaping seg, kmem caches. 57332048Sstans * Start pageout scanner (via page_needfree()). 57342048Sstans * Exit after ~ MAX_CNT s regardless of how much memory has been released. 57352048Sstans * Note: There is no guarantee that any availrmem will be freed as 57362048Sstans * this memory typically is locked (kernel heap) or reserved for swap. 57372048Sstans * Also due to memory fragmentation kmem allocator may not be able 57382048Sstans * to free any memory (single user allocated buffer will prevent 57392048Sstans * freeing slab or a page). 57402048Sstans */ 57412048Sstans int 57422048Sstans page_reclaim_mem(pgcnt_t npages, pgcnt_t epages, int adjust) 57432048Sstans { 57442048Sstans int i = 0; 57452048Sstans int ret = 0; 57462048Sstans pgcnt_t deficit; 57472048Sstans pgcnt_t old_availrmem; 57482048Sstans 57492048Sstans mutex_enter(&freemem_lock); 57502048Sstans old_availrmem = availrmem - 1; 57512048Sstans while ((availrmem < tune.t_minarmem + npages + epages) && 57522048Sstans (old_availrmem < availrmem) && (i++ < MAX_CNT)) { 57532048Sstans old_availrmem = availrmem; 57542048Sstans deficit = tune.t_minarmem + npages + epages - availrmem; 57552048Sstans mutex_exit(&freemem_lock); 57562048Sstans page_needfree(deficit); 57572048Sstans kmem_reap(); 57582048Sstans delay(hz); 57592048Sstans page_needfree(-(spgcnt_t)deficit); 57602048Sstans mutex_enter(&freemem_lock); 57612048Sstans } 57622048Sstans 57632048Sstans if (adjust && (availrmem >= tune.t_minarmem + npages + epages)) { 57642048Sstans availrmem -= npages; 57652048Sstans ret = 1; 57662048Sstans } 57672048Sstans 57682048Sstans mutex_exit(&freemem_lock); 57692048Sstans 57702048Sstans return (ret); 57712048Sstans } 57720Sstevel@tonic-gate 57730Sstevel@tonic-gate /* 57740Sstevel@tonic-gate * Search the memory segments to locate the desired page. Within a 57750Sstevel@tonic-gate * segment, pages increase linearly with one page structure per 57760Sstevel@tonic-gate * physical page frame (size PAGESIZE). The search begins 57770Sstevel@tonic-gate * with the segment that was accessed last, to take advantage of locality. 57780Sstevel@tonic-gate * If the hint misses, we start from the beginning of the sorted memseg list 57790Sstevel@tonic-gate */ 57800Sstevel@tonic-gate 57810Sstevel@tonic-gate 57820Sstevel@tonic-gate /* 57830Sstevel@tonic-gate * Some data structures for pfn to pp lookup. 57840Sstevel@tonic-gate */ 57850Sstevel@tonic-gate ulong_t mhash_per_slot; 57860Sstevel@tonic-gate struct memseg *memseg_hash[N_MEM_SLOTS]; 57870Sstevel@tonic-gate 57880Sstevel@tonic-gate page_t * 57890Sstevel@tonic-gate page_numtopp_nolock(pfn_t pfnum) 57900Sstevel@tonic-gate { 57910Sstevel@tonic-gate struct memseg *seg; 57920Sstevel@tonic-gate page_t *pp; 57938887SMichael.Corcoran@Sun.COM vm_cpu_data_t *vc; 57948887SMichael.Corcoran@Sun.COM 57958887SMichael.Corcoran@Sun.COM /* 57968887SMichael.Corcoran@Sun.COM * We need to disable kernel preemption while referencing the 57978887SMichael.Corcoran@Sun.COM * cpu_vm_data field in order to prevent us from being switched to 57988887SMichael.Corcoran@Sun.COM * another cpu and trying to reference it after it has been freed. 57998887SMichael.Corcoran@Sun.COM * This will keep us on cpu and prevent it from being removed while 58008887SMichael.Corcoran@Sun.COM * we are still on it. 58018887SMichael.Corcoran@Sun.COM */ 58028887SMichael.Corcoran@Sun.COM kpreempt_disable(); 58038887SMichael.Corcoran@Sun.COM vc = CPU->cpu_vm_data; 5804414Skchow ASSERT(vc != NULL); 58050Sstevel@tonic-gate 58060Sstevel@tonic-gate MEMSEG_STAT_INCR(nsearch); 58070Sstevel@tonic-gate 58080Sstevel@tonic-gate /* Try last winner first */ 5809414Skchow if (((seg = vc->vc_pnum_memseg) != NULL) && 58103559Smec (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 58110Sstevel@tonic-gate MEMSEG_STAT_INCR(nlastwon); 58120Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 58138887SMichael.Corcoran@Sun.COM if (pp->p_pagenum == pfnum) { 58148887SMichael.Corcoran@Sun.COM kpreempt_enable(); 58150Sstevel@tonic-gate return ((page_t *)pp); 58168887SMichael.Corcoran@Sun.COM } 58170Sstevel@tonic-gate } 58180Sstevel@tonic-gate 58190Sstevel@tonic-gate /* Else Try hash */ 58200Sstevel@tonic-gate if (((seg = memseg_hash[MEMSEG_PFN_HASH(pfnum)]) != NULL) && 58213559Smec (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 58220Sstevel@tonic-gate MEMSEG_STAT_INCR(nhashwon); 5823414Skchow vc->vc_pnum_memseg = seg; 58240Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 58258887SMichael.Corcoran@Sun.COM if (pp->p_pagenum == pfnum) { 58268887SMichael.Corcoran@Sun.COM kpreempt_enable(); 58270Sstevel@tonic-gate return ((page_t *)pp); 58288887SMichael.Corcoran@Sun.COM } 58290Sstevel@tonic-gate } 58300Sstevel@tonic-gate 58310Sstevel@tonic-gate /* Else Brute force */ 58320Sstevel@tonic-gate for (seg = memsegs; seg != NULL; seg = seg->next) { 58330Sstevel@tonic-gate if (pfnum >= seg->pages_base && pfnum < seg->pages_end) { 5834414Skchow vc->vc_pnum_memseg = seg; 58350Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 58368887SMichael.Corcoran@Sun.COM kpreempt_enable(); 58370Sstevel@tonic-gate return ((page_t *)pp); 58380Sstevel@tonic-gate } 58390Sstevel@tonic-gate } 5840414Skchow vc->vc_pnum_memseg = NULL; 58418887SMichael.Corcoran@Sun.COM kpreempt_enable(); 58420Sstevel@tonic-gate MEMSEG_STAT_INCR(nnotfound); 58430Sstevel@tonic-gate return ((page_t *)NULL); 58440Sstevel@tonic-gate 58450Sstevel@tonic-gate } 58460Sstevel@tonic-gate 58470Sstevel@tonic-gate struct memseg * 58480Sstevel@tonic-gate page_numtomemseg_nolock(pfn_t pfnum) 58490Sstevel@tonic-gate { 58500Sstevel@tonic-gate struct memseg *seg; 58510Sstevel@tonic-gate page_t *pp; 58520Sstevel@tonic-gate 58530Sstevel@tonic-gate /* Try hash */ 58540Sstevel@tonic-gate if (((seg = memseg_hash[MEMSEG_PFN_HASH(pfnum)]) != NULL) && 58553559Smec (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 58560Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 58570Sstevel@tonic-gate if (pp->p_pagenum == pfnum) 58580Sstevel@tonic-gate return (seg); 58590Sstevel@tonic-gate } 58600Sstevel@tonic-gate 58610Sstevel@tonic-gate /* Else Brute force */ 58620Sstevel@tonic-gate for (seg = memsegs; seg != NULL; seg = seg->next) { 58630Sstevel@tonic-gate if (pfnum >= seg->pages_base && pfnum < seg->pages_end) { 58640Sstevel@tonic-gate return (seg); 58650Sstevel@tonic-gate } 58660Sstevel@tonic-gate } 58670Sstevel@tonic-gate return ((struct memseg *)NULL); 58680Sstevel@tonic-gate } 58690Sstevel@tonic-gate 58700Sstevel@tonic-gate /* 58710Sstevel@tonic-gate * Given a page and a count return the page struct that is 58720Sstevel@tonic-gate * n structs away from the current one in the global page 58730Sstevel@tonic-gate * list. 58740Sstevel@tonic-gate * 58750Sstevel@tonic-gate * This function wraps to the first page upon 58760Sstevel@tonic-gate * reaching the end of the memseg list. 58770Sstevel@tonic-gate */ 58780Sstevel@tonic-gate page_t * 58790Sstevel@tonic-gate page_nextn(page_t *pp, ulong_t n) 58800Sstevel@tonic-gate { 58810Sstevel@tonic-gate struct memseg *seg; 58820Sstevel@tonic-gate page_t *ppn; 58838887SMichael.Corcoran@Sun.COM vm_cpu_data_t *vc; 58848887SMichael.Corcoran@Sun.COM 58858887SMichael.Corcoran@Sun.COM /* 58868887SMichael.Corcoran@Sun.COM * We need to disable kernel preemption while referencing the 58878887SMichael.Corcoran@Sun.COM * cpu_vm_data field in order to prevent us from being switched to 58888887SMichael.Corcoran@Sun.COM * another cpu and trying to reference it after it has been freed. 58898887SMichael.Corcoran@Sun.COM * This will keep us on cpu and prevent it from being removed while 58908887SMichael.Corcoran@Sun.COM * we are still on it. 58918887SMichael.Corcoran@Sun.COM */ 58928887SMichael.Corcoran@Sun.COM kpreempt_disable(); 58938887SMichael.Corcoran@Sun.COM vc = (vm_cpu_data_t *)CPU->cpu_vm_data; 5894414Skchow 5895414Skchow ASSERT(vc != NULL); 5896414Skchow 5897414Skchow if (((seg = vc->vc_pnext_memseg) == NULL) || 58980Sstevel@tonic-gate (seg->pages_base == seg->pages_end) || 58990Sstevel@tonic-gate !(pp >= seg->pages && pp < seg->epages)) { 59000Sstevel@tonic-gate 59010Sstevel@tonic-gate for (seg = memsegs; seg; seg = seg->next) { 59020Sstevel@tonic-gate if (pp >= seg->pages && pp < seg->epages) 59030Sstevel@tonic-gate break; 59040Sstevel@tonic-gate } 59050Sstevel@tonic-gate 59060Sstevel@tonic-gate if (seg == NULL) { 59070Sstevel@tonic-gate /* Memory delete got in, return something valid. */ 59080Sstevel@tonic-gate /* TODO: fix me. */ 59090Sstevel@tonic-gate seg = memsegs; 59100Sstevel@tonic-gate pp = seg->pages; 59110Sstevel@tonic-gate } 59120Sstevel@tonic-gate } 59130Sstevel@tonic-gate 59140Sstevel@tonic-gate /* check for wraparound - possible if n is large */ 59150Sstevel@tonic-gate while ((ppn = (pp + n)) >= seg->epages || ppn < pp) { 59160Sstevel@tonic-gate n -= seg->epages - pp; 59170Sstevel@tonic-gate seg = seg->next; 59180Sstevel@tonic-gate if (seg == NULL) 59190Sstevel@tonic-gate seg = memsegs; 59200Sstevel@tonic-gate pp = seg->pages; 59210Sstevel@tonic-gate } 5922414Skchow vc->vc_pnext_memseg = seg; 59238887SMichael.Corcoran@Sun.COM kpreempt_enable(); 59240Sstevel@tonic-gate return (ppn); 59250Sstevel@tonic-gate } 59260Sstevel@tonic-gate 59270Sstevel@tonic-gate /* 59280Sstevel@tonic-gate * Initialize for a loop using page_next_scan_large(). 59290Sstevel@tonic-gate */ 59300Sstevel@tonic-gate page_t * 59310Sstevel@tonic-gate page_next_scan_init(void **cookie) 59320Sstevel@tonic-gate { 59330Sstevel@tonic-gate ASSERT(cookie != NULL); 59340Sstevel@tonic-gate *cookie = (void *)memsegs; 59350Sstevel@tonic-gate return ((page_t *)memsegs->pages); 59360Sstevel@tonic-gate } 59370Sstevel@tonic-gate 59380Sstevel@tonic-gate /* 59390Sstevel@tonic-gate * Return the next page in a scan of page_t's, assuming we want 59400Sstevel@tonic-gate * to skip over sub-pages within larger page sizes. 59410Sstevel@tonic-gate * 59420Sstevel@tonic-gate * The cookie is used to keep track of the current memseg. 59430Sstevel@tonic-gate */ 59440Sstevel@tonic-gate page_t * 59450Sstevel@tonic-gate page_next_scan_large( 59460Sstevel@tonic-gate page_t *pp, 59470Sstevel@tonic-gate ulong_t *n, 59480Sstevel@tonic-gate void **cookie) 59490Sstevel@tonic-gate { 59500Sstevel@tonic-gate struct memseg *seg = (struct memseg *)*cookie; 59510Sstevel@tonic-gate page_t *new_pp; 59520Sstevel@tonic-gate ulong_t cnt; 59530Sstevel@tonic-gate pfn_t pfn; 59540Sstevel@tonic-gate 59550Sstevel@tonic-gate 59560Sstevel@tonic-gate /* 59570Sstevel@tonic-gate * get the count of page_t's to skip based on the page size 59580Sstevel@tonic-gate */ 59590Sstevel@tonic-gate ASSERT(pp != NULL); 59600Sstevel@tonic-gate if (pp->p_szc == 0) { 59610Sstevel@tonic-gate cnt = 1; 59620Sstevel@tonic-gate } else { 59630Sstevel@tonic-gate pfn = page_pptonum(pp); 59640Sstevel@tonic-gate cnt = page_get_pagecnt(pp->p_szc); 59650Sstevel@tonic-gate cnt -= pfn & (cnt - 1); 59660Sstevel@tonic-gate } 59670Sstevel@tonic-gate *n += cnt; 59680Sstevel@tonic-gate new_pp = pp + cnt; 59690Sstevel@tonic-gate 59700Sstevel@tonic-gate /* 59710Sstevel@tonic-gate * Catch if we went past the end of the current memory segment. If so, 59720Sstevel@tonic-gate * just move to the next segment with pages. 59730Sstevel@tonic-gate */ 59740Sstevel@tonic-gate if (new_pp >= seg->epages) { 59750Sstevel@tonic-gate do { 59760Sstevel@tonic-gate seg = seg->next; 59770Sstevel@tonic-gate if (seg == NULL) 59780Sstevel@tonic-gate seg = memsegs; 59790Sstevel@tonic-gate } while (seg->pages == seg->epages); 59800Sstevel@tonic-gate new_pp = seg->pages; 59810Sstevel@tonic-gate *cookie = (void *)seg; 59820Sstevel@tonic-gate } 59830Sstevel@tonic-gate 59840Sstevel@tonic-gate return (new_pp); 59850Sstevel@tonic-gate } 59860Sstevel@tonic-gate 59870Sstevel@tonic-gate 59880Sstevel@tonic-gate /* 59890Sstevel@tonic-gate * Returns next page in list. Note: this function wraps 59900Sstevel@tonic-gate * to the first page in the list upon reaching the end 59910Sstevel@tonic-gate * of the list. Callers should be aware of this fact. 59920Sstevel@tonic-gate */ 59930Sstevel@tonic-gate 59940Sstevel@tonic-gate /* We should change this be a #define */ 59950Sstevel@tonic-gate 59960Sstevel@tonic-gate page_t * 59970Sstevel@tonic-gate page_next(page_t *pp) 59980Sstevel@tonic-gate { 59990Sstevel@tonic-gate return (page_nextn(pp, 1)); 60000Sstevel@tonic-gate } 60010Sstevel@tonic-gate 60020Sstevel@tonic-gate page_t * 60030Sstevel@tonic-gate page_first() 60040Sstevel@tonic-gate { 60050Sstevel@tonic-gate return ((page_t *)memsegs->pages); 60060Sstevel@tonic-gate } 60070Sstevel@tonic-gate 60080Sstevel@tonic-gate 60090Sstevel@tonic-gate /* 60100Sstevel@tonic-gate * This routine is called at boot with the initial memory configuration 60110Sstevel@tonic-gate * and when memory is added or removed. 60120Sstevel@tonic-gate */ 60130Sstevel@tonic-gate void 60140Sstevel@tonic-gate build_pfn_hash() 60150Sstevel@tonic-gate { 60160Sstevel@tonic-gate pfn_t cur; 60170Sstevel@tonic-gate pgcnt_t index; 60180Sstevel@tonic-gate struct memseg *pseg; 60190Sstevel@tonic-gate int i; 60200Sstevel@tonic-gate 60210Sstevel@tonic-gate /* 60220Sstevel@tonic-gate * Clear memseg_hash array. 60230Sstevel@tonic-gate * Since memory add/delete is designed to operate concurrently 60240Sstevel@tonic-gate * with normal operation, the hash rebuild must be able to run 60250Sstevel@tonic-gate * concurrently with page_numtopp_nolock(). To support this 60260Sstevel@tonic-gate * functionality, assignments to memseg_hash array members must 60270Sstevel@tonic-gate * be done atomically. 60280Sstevel@tonic-gate * 60290Sstevel@tonic-gate * NOTE: bzero() does not currently guarantee this for kernel 60300Sstevel@tonic-gate * threads, and cannot be used here. 60310Sstevel@tonic-gate */ 60320Sstevel@tonic-gate for (i = 0; i < N_MEM_SLOTS; i++) 60330Sstevel@tonic-gate memseg_hash[i] = NULL; 60340Sstevel@tonic-gate 60350Sstevel@tonic-gate hat_kpm_mseghash_clear(N_MEM_SLOTS); 60360Sstevel@tonic-gate 60370Sstevel@tonic-gate /* 60380Sstevel@tonic-gate * Physmax is the last valid pfn. 60390Sstevel@tonic-gate */ 60400Sstevel@tonic-gate mhash_per_slot = (physmax + 1) >> MEM_HASH_SHIFT; 60410Sstevel@tonic-gate for (pseg = memsegs; pseg != NULL; pseg = pseg->next) { 60420Sstevel@tonic-gate index = MEMSEG_PFN_HASH(pseg->pages_base); 60430Sstevel@tonic-gate cur = pseg->pages_base; 60440Sstevel@tonic-gate do { 60450Sstevel@tonic-gate if (index >= N_MEM_SLOTS) 60460Sstevel@tonic-gate index = MEMSEG_PFN_HASH(cur); 60470Sstevel@tonic-gate 60480Sstevel@tonic-gate if (memseg_hash[index] == NULL || 60490Sstevel@tonic-gate memseg_hash[index]->pages_base > pseg->pages_base) { 60500Sstevel@tonic-gate memseg_hash[index] = pseg; 60510Sstevel@tonic-gate hat_kpm_mseghash_update(index, pseg); 60520Sstevel@tonic-gate } 60530Sstevel@tonic-gate cur += mhash_per_slot; 60540Sstevel@tonic-gate index++; 60550Sstevel@tonic-gate } while (cur < pseg->pages_end); 60560Sstevel@tonic-gate } 60570Sstevel@tonic-gate } 60580Sstevel@tonic-gate 60590Sstevel@tonic-gate /* 60600Sstevel@tonic-gate * Return the pagenum for the pp 60610Sstevel@tonic-gate */ 60620Sstevel@tonic-gate pfn_t 60630Sstevel@tonic-gate page_pptonum(page_t *pp) 60640Sstevel@tonic-gate { 60650Sstevel@tonic-gate return (pp->p_pagenum); 60660Sstevel@tonic-gate } 60670Sstevel@tonic-gate 60680Sstevel@tonic-gate /* 60690Sstevel@tonic-gate * interface to the referenced and modified etc bits 60700Sstevel@tonic-gate * in the PSM part of the page struct 60710Sstevel@tonic-gate * when no locking is desired. 60720Sstevel@tonic-gate */ 60730Sstevel@tonic-gate void 60740Sstevel@tonic-gate page_set_props(page_t *pp, uint_t flags) 60750Sstevel@tonic-gate { 60760Sstevel@tonic-gate ASSERT((flags & ~(P_MOD | P_REF | P_RO)) == 0); 60770Sstevel@tonic-gate pp->p_nrm |= (uchar_t)flags; 60780Sstevel@tonic-gate } 60790Sstevel@tonic-gate 60807718SJason.Beloro@Sun.COM extern void mach_sync_icache_pp(page_t *); 60817718SJason.Beloro@Sun.COM #pragma weak mach_sync_icache_pp 60827718SJason.Beloro@Sun.COM 60837718SJason.Beloro@Sun.COM /* 60847718SJason.Beloro@Sun.COM * Flush I-cache if the page is being reassigned. The hashout flag is 60857718SJason.Beloro@Sun.COM * set when a page has been removed from a hash chain (i.e. vnode 60867718SJason.Beloro@Sun.COM * pages). If the page stays on the hash chain there is a chance it 60877718SJason.Beloro@Sun.COM * will be re-used, therefore there is no need to flush the 60887718SJason.Beloro@Sun.COM * I-cache. However, if the page is being removed from a hash chain 60897718SJason.Beloro@Sun.COM * then the page can be used for any new purpose, and the I-cache must 60907718SJason.Beloro@Sun.COM * be flushed. 60917718SJason.Beloro@Sun.COM */ 60927718SJason.Beloro@Sun.COM /* ARGSUSED */ 60930Sstevel@tonic-gate void 60947718SJason.Beloro@Sun.COM page_clr_all_props(page_t *pp, int hashout) 60950Sstevel@tonic-gate { 60967718SJason.Beloro@Sun.COM if (&mach_sync_icache_pp != NULL && hashout) { 60977718SJason.Beloro@Sun.COM mach_sync_icache_pp(pp); 60987718SJason.Beloro@Sun.COM } 60990Sstevel@tonic-gate pp->p_nrm = 0; 61000Sstevel@tonic-gate } 61010Sstevel@tonic-gate 61020Sstevel@tonic-gate /* 6103917Selowe * Clear p_lckcnt and p_cowcnt, adjusting freemem if required. 6104917Selowe */ 6105917Selowe int 6106917Selowe page_clear_lck_cow(page_t *pp, int adjust) 6107917Selowe { 6108917Selowe int f_amount; 6109917Selowe 6110917Selowe ASSERT(PAGE_EXCL(pp)); 6111917Selowe 6112917Selowe /* 6113917Selowe * The page_struct_lock need not be acquired here since 6114917Selowe * we require the caller hold the page exclusively locked. 6115917Selowe */ 6116917Selowe f_amount = 0; 6117917Selowe if (pp->p_lckcnt) { 6118917Selowe f_amount = 1; 6119917Selowe pp->p_lckcnt = 0; 6120917Selowe } 6121917Selowe if (pp->p_cowcnt) { 6122917Selowe f_amount += pp->p_cowcnt; 6123917Selowe pp->p_cowcnt = 0; 6124917Selowe } 6125917Selowe 6126917Selowe if (adjust && f_amount) { 6127917Selowe mutex_enter(&freemem_lock); 6128917Selowe availrmem += f_amount; 6129917Selowe mutex_exit(&freemem_lock); 6130917Selowe } 6131917Selowe 6132917Selowe return (f_amount); 6133917Selowe } 6134917Selowe 6135917Selowe /* 61360Sstevel@tonic-gate * The following functions is called from free_vp_pages() 61370Sstevel@tonic-gate * for an inexact estimate of a newly free'd page... 61380Sstevel@tonic-gate */ 61390Sstevel@tonic-gate ulong_t 61400Sstevel@tonic-gate page_share_cnt(page_t *pp) 61410Sstevel@tonic-gate { 61420Sstevel@tonic-gate return (hat_page_getshare(pp)); 61430Sstevel@tonic-gate } 61440Sstevel@tonic-gate 61450Sstevel@tonic-gate int 61460Sstevel@tonic-gate page_isshared(page_t *pp) 61470Sstevel@tonic-gate { 61484528Spaulsan return (hat_page_checkshare(pp, 1)); 61490Sstevel@tonic-gate } 61500Sstevel@tonic-gate 61510Sstevel@tonic-gate int 61520Sstevel@tonic-gate page_isfree(page_t *pp) 61530Sstevel@tonic-gate { 61540Sstevel@tonic-gate return (PP_ISFREE(pp)); 61550Sstevel@tonic-gate } 61560Sstevel@tonic-gate 61570Sstevel@tonic-gate int 61580Sstevel@tonic-gate page_isref(page_t *pp) 61590Sstevel@tonic-gate { 61600Sstevel@tonic-gate return (hat_page_getattr(pp, P_REF)); 61610Sstevel@tonic-gate } 61620Sstevel@tonic-gate 61630Sstevel@tonic-gate int 61640Sstevel@tonic-gate page_ismod(page_t *pp) 61650Sstevel@tonic-gate { 61660Sstevel@tonic-gate return (hat_page_getattr(pp, P_MOD)); 61670Sstevel@tonic-gate } 61683253Smec 61693253Smec /* 61703253Smec * The following code all currently relates to the page capture logic: 61713253Smec * 61723253Smec * This logic is used for cases where there is a desire to claim a certain 61733253Smec * physical page in the system for the caller. As it may not be possible 61743253Smec * to capture the page immediately, the p_toxic bits are used in the page 61753253Smec * structure to indicate that someone wants to capture this page. When the 61763253Smec * page gets unlocked, the toxic flag will be noted and an attempt to capture 61773253Smec * the page will be made. If it is successful, the original callers callback 61783253Smec * will be called with the page to do with it what they please. 61793253Smec * 61803253Smec * There is also an async thread which wakes up to attempt to capture 61813253Smec * pages occasionally which have the capture bit set. All of the pages which 61823253Smec * need to be captured asynchronously have been inserted into the 61833253Smec * page_capture_hash and thus this thread walks that hash list. Items in the 61843253Smec * hash have an expiration time so this thread handles that as well by removing 61853253Smec * the item from the hash if it has expired. 61863253Smec * 61873253Smec * Some important things to note are: 61883253Smec * - if the PR_CAPTURE bit is set on a page, then the page is in the 61893253Smec * page_capture_hash. The page_capture_hash_head.pchh_mutex is needed 61903253Smec * to set and clear this bit, and while the lock is held is the only time 61913253Smec * you can add or remove an entry from the hash. 61923253Smec * - the PR_CAPTURE bit can only be set and cleared while holding the 61933253Smec * page_capture_hash_head.pchh_mutex 61943253Smec * - the t_flag field of the thread struct is used with the T_CAPTURING 61953253Smec * flag to prevent recursion while dealing with large pages. 61963253Smec * - pages which need to be retired never expire on the page_capture_hash. 61973253Smec */ 61983253Smec 61993253Smec static void page_capture_thread(void); 62003253Smec static kthread_t *pc_thread_id; 62013253Smec kcondvar_t pc_cv; 62023253Smec static kmutex_t pc_thread_mutex; 62033253Smec static clock_t pc_thread_shortwait; 62043253Smec static clock_t pc_thread_longwait; 62056695Saguzovsk static int pc_thread_retry; 62063253Smec 62073253Smec struct page_capture_callback pc_cb[PC_NUM_CALLBACKS]; 62083253Smec 62093253Smec /* Note that this is a circular linked list */ 62103253Smec typedef struct page_capture_hash_bucket { 62113253Smec page_t *pp; 62123253Smec uint_t szc; 62133253Smec uint_t flags; 62143253Smec clock_t expires; /* lbolt at which this request expires. */ 62153253Smec void *datap; /* Cached data passed in for callback */ 62163253Smec struct page_capture_hash_bucket *next; 62173253Smec struct page_capture_hash_bucket *prev; 62183253Smec } page_capture_hash_bucket_t; 62193253Smec 62203253Smec /* 62213253Smec * Each hash bucket will have it's own mutex and two lists which are: 62223253Smec * active (0): represents requests which have not been processed by 62233253Smec * the page_capture async thread yet. 62243253Smec * walked (1): represents requests which have been processed by the 62253253Smec * page_capture async thread within it's given walk of this bucket. 62263253Smec * 62273253Smec * These are all needed so that we can synchronize all async page_capture 62283253Smec * events. When the async thread moves to a new bucket, it will append the 62293253Smec * walked list to the active list and walk each item one at a time, moving it 62303253Smec * from the active list to the walked list. Thus if there is an async request 62313253Smec * outstanding for a given page, it will always be in one of the two lists. 62323253Smec * New requests will always be added to the active list. 62333253Smec * If we were not able to capture a page before the request expired, we'd free 62343253Smec * up the request structure which would indicate to page_capture that there is 62353253Smec * no longer a need for the given page, and clear the PR_CAPTURE flag if 62363253Smec * possible. 62373253Smec */ 62383253Smec typedef struct page_capture_hash_head { 62393253Smec kmutex_t pchh_mutex; 62403253Smec uint_t num_pages; 62413253Smec page_capture_hash_bucket_t lists[2]; /* sentinel nodes */ 62423253Smec } page_capture_hash_head_t; 62433253Smec 62443253Smec #ifdef DEBUG 62453253Smec #define NUM_PAGE_CAPTURE_BUCKETS 4 62463253Smec #else 62473253Smec #define NUM_PAGE_CAPTURE_BUCKETS 64 62483253Smec #endif 62493253Smec 62503253Smec page_capture_hash_head_t page_capture_hash[NUM_PAGE_CAPTURE_BUCKETS]; 62513253Smec 62523253Smec /* for now use a very simple hash based upon the size of a page struct */ 62533253Smec #define PAGE_CAPTURE_HASH(pp) \ 62543253Smec ((int)(((uintptr_t)pp >> 7) & (NUM_PAGE_CAPTURE_BUCKETS - 1))) 62553253Smec 62563253Smec extern pgcnt_t swapfs_minfree; 62573253Smec 62583253Smec int page_trycapture(page_t *pp, uint_t szc, uint_t flags, void *datap); 62593253Smec 62603253Smec /* 62613253Smec * a callback function is required for page capture requests. 62623253Smec */ 62633253Smec void 62643253Smec page_capture_register_callback(uint_t index, clock_t duration, 62653253Smec int (*cb_func)(page_t *, void *, uint_t)) 62663253Smec { 62673253Smec ASSERT(pc_cb[index].cb_active == 0); 62683253Smec ASSERT(cb_func != NULL); 62693253Smec rw_enter(&pc_cb[index].cb_rwlock, RW_WRITER); 62703253Smec pc_cb[index].duration = duration; 62713253Smec pc_cb[index].cb_func = cb_func; 62723253Smec pc_cb[index].cb_active = 1; 62733253Smec rw_exit(&pc_cb[index].cb_rwlock); 62743253Smec } 62753253Smec 62763253Smec void 62773253Smec page_capture_unregister_callback(uint_t index) 62783253Smec { 62793253Smec int i, j; 62803253Smec struct page_capture_hash_bucket *bp1; 62813253Smec struct page_capture_hash_bucket *bp2; 62823253Smec struct page_capture_hash_bucket *head = NULL; 62833253Smec uint_t flags = (1 << index); 62843253Smec 62853253Smec rw_enter(&pc_cb[index].cb_rwlock, RW_WRITER); 62863253Smec ASSERT(pc_cb[index].cb_active == 1); 62873253Smec pc_cb[index].duration = 0; /* Paranoia */ 62883253Smec pc_cb[index].cb_func = NULL; /* Paranoia */ 62893253Smec pc_cb[index].cb_active = 0; 62903253Smec rw_exit(&pc_cb[index].cb_rwlock); 62913253Smec 62923253Smec /* 62933253Smec * Just move all the entries to a private list which we can walk 62943253Smec * through without the need to hold any locks. 62953253Smec * No more requests can get added to the hash lists for this consumer 62963253Smec * as the cb_active field for the callback has been cleared. 62973253Smec */ 62983253Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 62993253Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 63003253Smec for (j = 0; j < 2; j++) { 63013253Smec bp1 = page_capture_hash[i].lists[j].next; 63023253Smec /* walk through all but first (sentinel) element */ 63033253Smec while (bp1 != &page_capture_hash[i].lists[j]) { 63043253Smec bp2 = bp1; 63053253Smec if (bp2->flags & flags) { 63063253Smec bp1 = bp2->next; 63073253Smec bp1->prev = bp2->prev; 63083253Smec bp2->prev->next = bp1; 63093253Smec bp2->next = head; 63103253Smec head = bp2; 63113253Smec /* 63123253Smec * Clear the PR_CAPTURE bit as we 63133253Smec * hold appropriate locks here. 63143253Smec */ 63153253Smec page_clrtoxic(head->pp, PR_CAPTURE); 63163253Smec page_capture_hash[i].num_pages--; 63173253Smec continue; 63183253Smec } 63193253Smec bp1 = bp1->next; 63203253Smec } 63213253Smec } 63223253Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 63233253Smec } 63243253Smec 63253253Smec while (head != NULL) { 63263253Smec bp1 = head; 63273253Smec head = head->next; 63283253Smec kmem_free(bp1, sizeof (*bp1)); 63293253Smec } 63303253Smec } 63313253Smec 63323253Smec 63333253Smec /* 63343253Smec * Find pp in the active list and move it to the walked list if it 63353253Smec * exists. 63363253Smec * Note that most often pp should be at the front of the active list 63373253Smec * as it is currently used and thus there is no other sort of optimization 63383253Smec * being done here as this is a linked list data structure. 63393253Smec * Returns 1 on successful move or 0 if page could not be found. 63403253Smec */ 63413253Smec static int 63423253Smec page_capture_move_to_walked(page_t *pp) 63433253Smec { 63443253Smec page_capture_hash_bucket_t *bp; 63453253Smec int index; 63463253Smec 63473253Smec index = PAGE_CAPTURE_HASH(pp); 63483253Smec 63493253Smec mutex_enter(&page_capture_hash[index].pchh_mutex); 63503253Smec bp = page_capture_hash[index].lists[0].next; 63513253Smec while (bp != &page_capture_hash[index].lists[0]) { 63523253Smec if (bp->pp == pp) { 63533253Smec /* Remove from old list */ 63543253Smec bp->next->prev = bp->prev; 63553253Smec bp->prev->next = bp->next; 63563253Smec 63573253Smec /* Add to new list */ 63583253Smec bp->next = page_capture_hash[index].lists[1].next; 63593253Smec bp->prev = &page_capture_hash[index].lists[1]; 63603253Smec page_capture_hash[index].lists[1].next = bp; 63613253Smec bp->next->prev = bp; 63623253Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 63633253Smec 63643253Smec return (1); 63653253Smec } 63663253Smec bp = bp->next; 63673253Smec } 63683253Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 63693253Smec return (0); 63703253Smec } 63713253Smec 63723253Smec /* 63733253Smec * Add a new entry to the page capture hash. The only case where a new 63743253Smec * entry is not added is when the page capture consumer is no longer registered. 63753253Smec * In this case, we'll silently not add the page to the hash. We know that 63763253Smec * page retire will always be registered for the case where we are currently 63773253Smec * unretiring a page and thus there are no conflicts. 63783253Smec */ 63793253Smec static void 63803253Smec page_capture_add_hash(page_t *pp, uint_t szc, uint_t flags, void *datap) 63813253Smec { 63823253Smec page_capture_hash_bucket_t *bp1; 63833253Smec page_capture_hash_bucket_t *bp2; 63843253Smec int index; 63853253Smec int cb_index; 63863253Smec int i; 63873253Smec #ifdef DEBUG 63883253Smec page_capture_hash_bucket_t *tp1; 63893253Smec int l; 63903253Smec #endif 63913253Smec 63923253Smec ASSERT(!(flags & CAPTURE_ASYNC)); 63933253Smec 63943253Smec bp1 = kmem_alloc(sizeof (struct page_capture_hash_bucket), KM_SLEEP); 63953253Smec 63963253Smec bp1->pp = pp; 63973253Smec bp1->szc = szc; 63983253Smec bp1->flags = flags; 63993253Smec bp1->datap = datap; 64003253Smec 64013253Smec for (cb_index = 0; cb_index < PC_NUM_CALLBACKS; cb_index++) { 64023253Smec if ((flags >> cb_index) & 1) { 64033253Smec break; 64043253Smec } 64053253Smec } 64063253Smec 64073253Smec ASSERT(cb_index != PC_NUM_CALLBACKS); 64083253Smec 64093253Smec rw_enter(&pc_cb[cb_index].cb_rwlock, RW_READER); 64103253Smec if (pc_cb[cb_index].cb_active) { 64113253Smec if (pc_cb[cb_index].duration == -1) { 64123253Smec bp1->expires = (clock_t)-1; 64133253Smec } else { 64143253Smec bp1->expires = lbolt + pc_cb[cb_index].duration; 64153253Smec } 64163253Smec } else { 64173253Smec /* There's no callback registered so don't add to the hash */ 64183253Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 64193253Smec kmem_free(bp1, sizeof (*bp1)); 64203253Smec return; 64213253Smec } 64223253Smec 64233253Smec index = PAGE_CAPTURE_HASH(pp); 64243253Smec 64253253Smec /* 64263253Smec * Only allow capture flag to be modified under this mutex. 64273253Smec * Prevents multiple entries for same page getting added. 64283253Smec */ 64293253Smec mutex_enter(&page_capture_hash[index].pchh_mutex); 64303253Smec 64313253Smec /* 64323253Smec * if not already on the hash, set capture bit and add to the hash 64333253Smec */ 64343253Smec if (!(pp->p_toxic & PR_CAPTURE)) { 64353253Smec #ifdef DEBUG 64363253Smec /* Check for duplicate entries */ 64373253Smec for (l = 0; l < 2; l++) { 64383253Smec tp1 = page_capture_hash[index].lists[l].next; 64393253Smec while (tp1 != &page_capture_hash[index].lists[l]) { 64403253Smec if (tp1->pp == pp) { 64413253Smec panic("page pp 0x%p already on hash " 64427632SNick.Todd@Sun.COM "at 0x%p\n", 64437632SNick.Todd@Sun.COM (void *)pp, (void *)tp1); 64443253Smec } 64453253Smec tp1 = tp1->next; 64463253Smec } 64473253Smec } 64483253Smec 64493253Smec #endif 64503253Smec page_settoxic(pp, PR_CAPTURE); 64513253Smec bp1->next = page_capture_hash[index].lists[0].next; 64523253Smec bp1->prev = &page_capture_hash[index].lists[0]; 64533253Smec bp1->next->prev = bp1; 64543253Smec page_capture_hash[index].lists[0].next = bp1; 64553253Smec page_capture_hash[index].num_pages++; 64563480Sjfrank if (flags & CAPTURE_RETIRE) { 64579544SChristopher.Baumbauer@Sun.COM page_retire_incr_pend_count(datap); 64583480Sjfrank } 64593253Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 64603253Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 64613253Smec cv_signal(&pc_cv); 64623253Smec return; 64633253Smec } 64643253Smec 64653253Smec /* 64663253Smec * A page retire request will replace any other request. 64673253Smec * A second physmem request which is for a different process than 64683253Smec * the currently registered one will be dropped as there is 64693253Smec * no way to hold the private data for both calls. 64703253Smec * In the future, once there are more callers, this will have to 64713253Smec * be worked out better as there needs to be private storage for 64723253Smec * at least each type of caller (maybe have datap be an array of 64733253Smec * *void's so that we can index based upon callers index). 64743253Smec */ 64753253Smec 64763253Smec /* walk hash list to update expire time */ 64773253Smec for (i = 0; i < 2; i++) { 64783253Smec bp2 = page_capture_hash[index].lists[i].next; 64793253Smec while (bp2 != &page_capture_hash[index].lists[i]) { 64803253Smec if (bp2->pp == pp) { 64813253Smec if (flags & CAPTURE_RETIRE) { 64823253Smec if (!(bp2->flags & CAPTURE_RETIRE)) { 64839544SChristopher.Baumbauer@Sun.COM page_retire_incr_pend_count( 64849544SChristopher.Baumbauer@Sun.COM datap); 64853253Smec bp2->flags = flags; 64863253Smec bp2->expires = bp1->expires; 64873253Smec bp2->datap = datap; 64883253Smec } 64893253Smec } else { 64903253Smec ASSERT(flags & CAPTURE_PHYSMEM); 64913253Smec if (!(bp2->flags & CAPTURE_RETIRE) && 64923253Smec (datap == bp2->datap)) { 64933253Smec bp2->expires = bp1->expires; 64943253Smec } 64953253Smec } 64963253Smec mutex_exit(&page_capture_hash[index]. 64973253Smec pchh_mutex); 64983253Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 64993253Smec kmem_free(bp1, sizeof (*bp1)); 65003253Smec return; 65013253Smec } 65023253Smec bp2 = bp2->next; 65033253Smec } 65043253Smec } 65053253Smec 65063253Smec /* 65073253Smec * the PR_CAPTURE flag is protected by the page_capture_hash mutexes 65083253Smec * and thus it either has to be set or not set and can't change 65093253Smec * while holding the mutex above. 65103253Smec */ 65117632SNick.Todd@Sun.COM panic("page_capture_add_hash, PR_CAPTURE flag set on pp %p\n", 65127632SNick.Todd@Sun.COM (void *)pp); 65133253Smec } 65143253Smec 65153253Smec /* 65163253Smec * We have a page in our hands, lets try and make it ours by turning 65173253Smec * it into a clean page like it had just come off the freelists. 65183253Smec * 65193253Smec * Returns 0 on success, with the page still EXCL locked. 65203253Smec * On failure, the page will be unlocked, and returns EAGAIN 65213253Smec */ 65223253Smec static int 65233253Smec page_capture_clean_page(page_t *pp) 65243253Smec { 65253253Smec page_t *newpp; 65263253Smec int skip_unlock = 0; 65273253Smec spgcnt_t count; 65283253Smec page_t *tpp; 65293253Smec int ret = 0; 65303253Smec int extra; 65313253Smec 65323253Smec ASSERT(PAGE_EXCL(pp)); 65333253Smec ASSERT(!PP_RETIRED(pp)); 65343253Smec ASSERT(curthread->t_flag & T_CAPTURING); 65353253Smec 65363253Smec if (PP_ISFREE(pp)) { 65373559Smec if (!page_reclaim(pp, NULL)) { 65383253Smec skip_unlock = 1; 65393253Smec ret = EAGAIN; 65403253Smec goto cleanup; 65413253Smec } 65423559Smec ASSERT(pp->p_szc == 0); 65433253Smec if (pp->p_vnode != NULL) { 65443253Smec /* 65453253Smec * Since this page came from the 65463253Smec * cachelist, we must destroy the 65473253Smec * old vnode association. 65483253Smec */ 65493253Smec page_hashout(pp, NULL); 65503253Smec } 65513253Smec goto cleanup; 65523253Smec } 65533253Smec 65543253Smec /* 65553253Smec * If we know page_relocate will fail, skip it 65563253Smec * It could still fail due to a UE on another page but we 65573253Smec * can't do anything about that. 65583253Smec */ 65593253Smec if (pp->p_toxic & PR_UE) { 65603253Smec goto skip_relocate; 65613253Smec } 65623253Smec 65633253Smec /* 65643253Smec * It's possible that pages can not have a vnode as fsflush comes 65653253Smec * through and cleans up these pages. It's ugly but that's how it is. 65663253Smec */ 65673253Smec if (pp->p_vnode == NULL) { 65683253Smec goto skip_relocate; 65693253Smec } 65703253Smec 65713253Smec /* 65723253Smec * Page was not free, so lets try to relocate it. 65733253Smec * page_relocate only works with root pages, so if this is not a root 65743253Smec * page, we need to demote it to try and relocate it. 65753253Smec * Unfortunately this is the best we can do right now. 65763253Smec */ 65773253Smec newpp = NULL; 65783253Smec if ((pp->p_szc > 0) && (pp != PP_PAGEROOT(pp))) { 65793253Smec if (page_try_demote_pages(pp) == 0) { 65803253Smec ret = EAGAIN; 65813253Smec goto cleanup; 65823253Smec } 65833253Smec } 65843253Smec ret = page_relocate(&pp, &newpp, 1, 0, &count, NULL); 65853253Smec if (ret == 0) { 65863253Smec page_t *npp; 65873253Smec /* unlock the new page(s) */ 65883253Smec while (count-- > 0) { 65893253Smec ASSERT(newpp != NULL); 65903253Smec npp = newpp; 65913253Smec page_sub(&newpp, npp); 65923253Smec page_unlock(npp); 65933253Smec } 65943253Smec ASSERT(newpp == NULL); 65953253Smec /* 65963253Smec * Check to see if the page we have is too large. 65973253Smec * If so, demote it freeing up the extra pages. 65983253Smec */ 65993253Smec if (pp->p_szc > 0) { 66003253Smec /* For now demote extra pages to szc == 0 */ 66013253Smec extra = page_get_pagecnt(pp->p_szc) - 1; 66023253Smec while (extra > 0) { 66033253Smec tpp = pp->p_next; 66043253Smec page_sub(&pp, tpp); 66053253Smec tpp->p_szc = 0; 66063253Smec page_free(tpp, 1); 66073253Smec extra--; 66083253Smec } 66093253Smec /* Make sure to set our page to szc 0 as well */ 66103253Smec ASSERT(pp->p_next == pp && pp->p_prev == pp); 66113253Smec pp->p_szc = 0; 66123253Smec } 66133253Smec goto cleanup; 66143253Smec } else if (ret == EIO) { 66153253Smec ret = EAGAIN; 66163253Smec goto cleanup; 66173253Smec } else { 66183253Smec /* 66193253Smec * Need to reset return type as we failed to relocate the page 66203253Smec * but that does not mean that some of the next steps will not 66213253Smec * work. 66223253Smec */ 66233253Smec ret = 0; 66243253Smec } 66253253Smec 66263253Smec skip_relocate: 66273253Smec 66283253Smec if (pp->p_szc > 0) { 66293253Smec if (page_try_demote_pages(pp) == 0) { 66303253Smec ret = EAGAIN; 66313253Smec goto cleanup; 66323253Smec } 66333253Smec } 66343253Smec 66353253Smec ASSERT(pp->p_szc == 0); 66363253Smec 66373253Smec if (hat_ismod(pp)) { 66383253Smec ret = EAGAIN; 66393253Smec goto cleanup; 66403253Smec } 66413290Sjohansen if (PP_ISKAS(pp)) { 66423253Smec ret = EAGAIN; 66433253Smec goto cleanup; 66443253Smec } 66453253Smec if (pp->p_lckcnt || pp->p_cowcnt) { 66463253Smec ret = EAGAIN; 66473253Smec goto cleanup; 66483253Smec } 66493253Smec 66503253Smec (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 66513253Smec ASSERT(!hat_page_is_mapped(pp)); 66523253Smec 66533253Smec if (hat_ismod(pp)) { 66543253Smec /* 66553253Smec * This is a semi-odd case as the page is now modified but not 66563253Smec * mapped as we just unloaded the mappings above. 66573253Smec */ 66583253Smec ret = EAGAIN; 66593253Smec goto cleanup; 66603253Smec } 66613253Smec if (pp->p_vnode != NULL) { 66623253Smec page_hashout(pp, NULL); 66633253Smec } 66643253Smec 66653253Smec /* 66663253Smec * At this point, the page should be in a clean state and 66673253Smec * we can do whatever we want with it. 66683253Smec */ 66693253Smec 66703253Smec cleanup: 66713253Smec if (ret != 0) { 66723253Smec if (!skip_unlock) { 66733253Smec page_unlock(pp); 66743253Smec } 66753253Smec } else { 66763253Smec ASSERT(pp->p_szc == 0); 66773253Smec ASSERT(PAGE_EXCL(pp)); 66783253Smec 66793253Smec pp->p_next = pp; 66803253Smec pp->p_prev = pp; 66813253Smec } 66823253Smec return (ret); 66833253Smec } 66843253Smec 66853253Smec /* 66863253Smec * Various callers of page_trycapture() can have different restrictions upon 66873253Smec * what memory they have access to. 66883253Smec * Returns 0 on success, with the following error codes on failure: 66893253Smec * EPERM - The requested page is long term locked, and thus repeated 66903253Smec * requests to capture this page will likely fail. 66913253Smec * ENOMEM - There was not enough free memory in the system to safely 66923253Smec * map the requested page. 66933253Smec * ENOENT - The requested page was inside the kernel cage, and the 66943253Smec * PHYSMEM_CAGE flag was not set. 66953253Smec */ 66963253Smec int 66973253Smec page_capture_pre_checks(page_t *pp, uint_t flags) 66983253Smec { 66993253Smec #if defined(__sparc) 67003253Smec extern struct vnode prom_ppages; 67013253Smec #endif /* __sparc */ 67023253Smec 67033253Smec ASSERT(pp != NULL); 67043253Smec 67053253Smec #if defined(__sparc) 67063253Smec if (pp->p_vnode == &prom_ppages) { 67073253Smec return (EPERM); 67083253Smec } 67093253Smec 67106695Saguzovsk if (PP_ISNORELOC(pp) && !(flags & CAPTURE_GET_CAGE) && 67116695Saguzovsk (flags & CAPTURE_PHYSMEM)) { 67123253Smec return (ENOENT); 67133253Smec } 67143253Smec 67153253Smec if (PP_ISNORELOCKERNEL(pp)) { 67163253Smec return (EPERM); 67173253Smec } 67183253Smec #else 67193290Sjohansen if (PP_ISKAS(pp)) { 67203253Smec return (EPERM); 67213253Smec } 67223253Smec #endif /* __sparc */ 67233253Smec 67246695Saguzovsk /* only physmem currently has the restrictions checked below */ 67256695Saguzovsk if (!(flags & CAPTURE_PHYSMEM)) { 67266695Saguzovsk return (0); 67276695Saguzovsk } 67286695Saguzovsk 67293253Smec if (availrmem < swapfs_minfree) { 67303253Smec /* 67313253Smec * We won't try to capture this page as we are 67323253Smec * running low on memory. 67333253Smec */ 67343253Smec return (ENOMEM); 67353253Smec } 67363253Smec return (0); 67373253Smec } 67383253Smec 67393253Smec /* 67403253Smec * Once we have a page in our mits, go ahead and complete the capture 67413253Smec * operation. 67423253Smec * Returns 1 on failure where page is no longer needed 67433253Smec * Returns 0 on success 67443253Smec * Returns -1 if there was a transient failure. 67453253Smec * Failure cases must release the SE_EXCL lock on pp (usually via page_free). 67463253Smec */ 67473253Smec int 67483253Smec page_capture_take_action(page_t *pp, uint_t flags, void *datap) 67493253Smec { 67503253Smec int cb_index; 67513253Smec int ret = 0; 67523253Smec page_capture_hash_bucket_t *bp1; 67533253Smec page_capture_hash_bucket_t *bp2; 67543253Smec int index; 67553253Smec int found = 0; 67563253Smec int i; 67573253Smec 67583253Smec ASSERT(PAGE_EXCL(pp)); 67593253Smec ASSERT(curthread->t_flag & T_CAPTURING); 67603253Smec 67613253Smec for (cb_index = 0; cb_index < PC_NUM_CALLBACKS; cb_index++) { 67623253Smec if ((flags >> cb_index) & 1) { 67633253Smec break; 67643253Smec } 67653253Smec } 67663253Smec ASSERT(cb_index < PC_NUM_CALLBACKS); 67673253Smec 67683253Smec /* 67693253Smec * Remove the entry from the page_capture hash, but don't free it yet 67703253Smec * as we may need to put it back. 67713253Smec * Since we own the page at this point in time, we should find it 67723253Smec * in the hash if this is an ASYNC call. If we don't it's likely 67733253Smec * that the page_capture_async() thread decided that this request 67743253Smec * had expired, in which case we just continue on. 67753253Smec */ 67763253Smec if (flags & CAPTURE_ASYNC) { 67773253Smec 67783253Smec index = PAGE_CAPTURE_HASH(pp); 67793253Smec 67803253Smec mutex_enter(&page_capture_hash[index].pchh_mutex); 67813253Smec for (i = 0; i < 2 && !found; i++) { 67823253Smec bp1 = page_capture_hash[index].lists[i].next; 67833253Smec while (bp1 != &page_capture_hash[index].lists[i]) { 67843253Smec if (bp1->pp == pp) { 67853253Smec bp1->next->prev = bp1->prev; 67863253Smec bp1->prev->next = bp1->next; 67873253Smec page_capture_hash[index].num_pages--; 67883253Smec page_clrtoxic(pp, PR_CAPTURE); 67893253Smec found = 1; 67903253Smec break; 67913253Smec } 67923253Smec bp1 = bp1->next; 67933253Smec } 67943253Smec } 67953253Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 67963253Smec } 67973253Smec 67983253Smec /* Synchronize with the unregister func. */ 67993253Smec rw_enter(&pc_cb[cb_index].cb_rwlock, RW_READER); 68003253Smec if (!pc_cb[cb_index].cb_active) { 68013253Smec page_free(pp, 1); 68023253Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 68033253Smec if (found) { 68043253Smec kmem_free(bp1, sizeof (*bp1)); 68053253Smec } 68063253Smec return (1); 68073253Smec } 68083253Smec 68093253Smec /* 68103253Smec * We need to remove the entry from the page capture hash and turn off 68113253Smec * the PR_CAPTURE bit before calling the callback. We'll need to cache 68123253Smec * the entry here, and then based upon the return value, cleanup 68133253Smec * appropriately or re-add it to the hash, making sure that someone else 68143253Smec * hasn't already done so. 68153253Smec * It should be rare for the callback to fail and thus it's ok for 68163253Smec * the failure path to be a bit complicated as the success path is 68173253Smec * cleaner and the locking rules are easier to follow. 68183253Smec */ 68193253Smec 68203253Smec ret = pc_cb[cb_index].cb_func(pp, datap, flags); 68213253Smec 68223253Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 68233253Smec 68243253Smec /* 68253253Smec * If this was an ASYNC request, we need to cleanup the hash if the 68263253Smec * callback was successful or if the request was no longer valid. 68273253Smec * For non-ASYNC requests, we return failure to map and the caller 68283253Smec * will take care of adding the request to the hash. 68293253Smec * Note also that the callback itself is responsible for the page 68303253Smec * at this point in time in terms of locking ... The most common 68313253Smec * case for the failure path should just be a page_free. 68323253Smec */ 68333253Smec if (ret >= 0) { 68343253Smec if (found) { 68353480Sjfrank if (bp1->flags & CAPTURE_RETIRE) { 68369544SChristopher.Baumbauer@Sun.COM page_retire_decr_pend_count(datap); 68373480Sjfrank } 68383253Smec kmem_free(bp1, sizeof (*bp1)); 68393253Smec } 68403253Smec return (ret); 68413253Smec } 68423253Smec if (!found) { 68433253Smec return (ret); 68443253Smec } 68453253Smec 68463253Smec ASSERT(flags & CAPTURE_ASYNC); 68473253Smec 68483253Smec /* 68493253Smec * Check for expiration time first as we can just free it up if it's 68503253Smec * expired. 68513253Smec */ 68523253Smec if (lbolt > bp1->expires && bp1->expires != -1) { 68533253Smec kmem_free(bp1, sizeof (*bp1)); 68543253Smec return (ret); 68553253Smec } 68563253Smec 68573253Smec /* 68583253Smec * The callback failed and there used to be an entry in the hash for 68593253Smec * this page, so we need to add it back to the hash. 68603253Smec */ 68613253Smec mutex_enter(&page_capture_hash[index].pchh_mutex); 68623253Smec if (!(pp->p_toxic & PR_CAPTURE)) { 68633253Smec /* just add bp1 back to head of walked list */ 68643253Smec page_settoxic(pp, PR_CAPTURE); 68653253Smec bp1->next = page_capture_hash[index].lists[1].next; 68663253Smec bp1->prev = &page_capture_hash[index].lists[1]; 68673253Smec bp1->next->prev = bp1; 68683253Smec page_capture_hash[index].lists[1].next = bp1; 68693253Smec page_capture_hash[index].num_pages++; 68703253Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 68713253Smec return (ret); 68723253Smec } 68733253Smec 68743253Smec /* 68753253Smec * Otherwise there was a new capture request added to list 68763253Smec * Need to make sure that our original data is represented if 68773253Smec * appropriate. 68783253Smec */ 68793253Smec for (i = 0; i < 2; i++) { 68803253Smec bp2 = page_capture_hash[index].lists[i].next; 68813253Smec while (bp2 != &page_capture_hash[index].lists[i]) { 68823253Smec if (bp2->pp == pp) { 68833253Smec if (bp1->flags & CAPTURE_RETIRE) { 68843253Smec if (!(bp2->flags & CAPTURE_RETIRE)) { 68853253Smec bp2->szc = bp1->szc; 68863253Smec bp2->flags = bp1->flags; 68873253Smec bp2->expires = bp1->expires; 68883253Smec bp2->datap = bp1->datap; 68893253Smec } 68903253Smec } else { 68913253Smec ASSERT(bp1->flags & CAPTURE_PHYSMEM); 68923253Smec if (!(bp2->flags & CAPTURE_RETIRE)) { 68933253Smec bp2->szc = bp1->szc; 68943253Smec bp2->flags = bp1->flags; 68953253Smec bp2->expires = bp1->expires; 68963253Smec bp2->datap = bp1->datap; 68973253Smec } 68983253Smec } 68993253Smec mutex_exit(&page_capture_hash[index]. 69003253Smec pchh_mutex); 69013253Smec kmem_free(bp1, sizeof (*bp1)); 69023253Smec return (ret); 69033253Smec } 69043253Smec bp2 = bp2->next; 69053253Smec } 69063253Smec } 69077632SNick.Todd@Sun.COM panic("PR_CAPTURE set but not on hash for pp 0x%p\n", (void *)pp); 69083253Smec /*NOTREACHED*/ 69093253Smec } 69103253Smec 69113253Smec /* 69123253Smec * Try to capture the given page for the caller specified in the flags 69133253Smec * parameter. The page will either be captured and handed over to the 69143253Smec * appropriate callback, or will be queued up in the page capture hash 69153253Smec * to be captured asynchronously. 69163253Smec * If the current request is due to an async capture, the page must be 69173253Smec * exclusively locked before calling this function. 69183253Smec * Currently szc must be 0 but in the future this should be expandable to 69193253Smec * other page sizes. 69203253Smec * Returns 0 on success, with the following error codes on failure: 69213253Smec * EPERM - The requested page is long term locked, and thus repeated 69223253Smec * requests to capture this page will likely fail. 69233253Smec * ENOMEM - There was not enough free memory in the system to safely 69243253Smec * map the requested page. 69253253Smec * ENOENT - The requested page was inside the kernel cage, and the 69263253Smec * CAPTURE_GET_CAGE flag was not set. 69273253Smec * EAGAIN - The requested page could not be capturead at this point in 69283253Smec * time but future requests will likely work. 69293253Smec * EBUSY - The requested page is retired and the CAPTURE_GET_RETIRED flag 69303253Smec * was not set. 69313253Smec */ 69323253Smec int 69333253Smec page_itrycapture(page_t *pp, uint_t szc, uint_t flags, void *datap) 69343253Smec { 69353253Smec int ret; 69363253Smec int cb_index; 69373253Smec 69383253Smec if (flags & CAPTURE_ASYNC) { 69393253Smec ASSERT(PAGE_EXCL(pp)); 69403253Smec goto async; 69413253Smec } 69423253Smec 69433253Smec /* Make sure there's enough availrmem ... */ 69443253Smec ret = page_capture_pre_checks(pp, flags); 69453253Smec if (ret != 0) { 69463253Smec return (ret); 69473253Smec } 69483253Smec 69493253Smec if (!page_trylock(pp, SE_EXCL)) { 69503253Smec for (cb_index = 0; cb_index < PC_NUM_CALLBACKS; cb_index++) { 69513253Smec if ((flags >> cb_index) & 1) { 69523253Smec break; 69533253Smec } 69543253Smec } 69553253Smec ASSERT(cb_index < PC_NUM_CALLBACKS); 69563253Smec ret = EAGAIN; 69573253Smec /* Special case for retired pages */ 69583253Smec if (PP_RETIRED(pp)) { 69593253Smec if (flags & CAPTURE_GET_RETIRED) { 69603253Smec if (!page_unretire_pp(pp, PR_UNR_TEMP)) { 69613253Smec /* 69623253Smec * Need to set capture bit and add to 69633253Smec * hash so that the page will be 69643253Smec * retired when freed. 69653253Smec */ 69663253Smec page_capture_add_hash(pp, szc, 69673253Smec CAPTURE_RETIRE, NULL); 69683253Smec ret = 0; 69693253Smec goto own_page; 69703253Smec } 69713253Smec } else { 69723253Smec return (EBUSY); 69733253Smec } 69743253Smec } 69753253Smec page_capture_add_hash(pp, szc, flags, datap); 69763253Smec return (ret); 69773253Smec } 69783253Smec 69793253Smec async: 69803253Smec ASSERT(PAGE_EXCL(pp)); 69813253Smec 69823253Smec /* Need to check for physmem async requests that availrmem is sane */ 69833253Smec if ((flags & (CAPTURE_ASYNC | CAPTURE_PHYSMEM)) == 69843253Smec (CAPTURE_ASYNC | CAPTURE_PHYSMEM) && 69853253Smec (availrmem < swapfs_minfree)) { 69863253Smec page_unlock(pp); 69873253Smec return (ENOMEM); 69883253Smec } 69893253Smec 69903253Smec ret = page_capture_clean_page(pp); 69913253Smec 69923253Smec if (ret != 0) { 69933253Smec /* We failed to get the page, so lets add it to the hash */ 69943253Smec if (!(flags & CAPTURE_ASYNC)) { 69953253Smec page_capture_add_hash(pp, szc, flags, datap); 69963253Smec } 69973253Smec return (ret); 69983253Smec } 69993253Smec 70003253Smec own_page: 70013253Smec ASSERT(PAGE_EXCL(pp)); 70023253Smec ASSERT(pp->p_szc == 0); 70033253Smec 70043253Smec /* Call the callback */ 70053253Smec ret = page_capture_take_action(pp, flags, datap); 70063253Smec 70073253Smec if (ret == 0) { 70083253Smec return (0); 70093253Smec } 70103253Smec 70113253Smec /* 70123253Smec * Note that in the failure cases from page_capture_take_action, the 70133253Smec * EXCL lock will have already been dropped. 70143253Smec */ 70153253Smec if ((ret == -1) && (!(flags & CAPTURE_ASYNC))) { 70163253Smec page_capture_add_hash(pp, szc, flags, datap); 70173253Smec } 70183253Smec return (EAGAIN); 70193253Smec } 70203253Smec 70213253Smec int 70223253Smec page_trycapture(page_t *pp, uint_t szc, uint_t flags, void *datap) 70233253Smec { 70243253Smec int ret; 70253253Smec 70263253Smec curthread->t_flag |= T_CAPTURING; 70273253Smec ret = page_itrycapture(pp, szc, flags, datap); 70283253Smec curthread->t_flag &= ~T_CAPTURING; /* xor works as we know its set */ 70293253Smec return (ret); 70303253Smec } 70313253Smec 70323253Smec /* 70333253Smec * When unlocking a page which has the PR_CAPTURE bit set, this routine 70343253Smec * gets called to try and capture the page. 70353253Smec */ 70363253Smec void 70373253Smec page_unlock_capture(page_t *pp) 70383253Smec { 70393253Smec page_capture_hash_bucket_t *bp; 70403253Smec int index; 70413253Smec int i; 70423253Smec uint_t szc; 70433253Smec uint_t flags = 0; 70443253Smec void *datap; 70453253Smec kmutex_t *mp; 70463253Smec extern vnode_t retired_pages; 70473253Smec 70483253Smec /* 70493253Smec * We need to protect against a possible deadlock here where we own 70503253Smec * the vnode page hash mutex and want to acquire it again as there 70513253Smec * are locations in the code, where we unlock a page while holding 70523253Smec * the mutex which can lead to the page being captured and eventually 70533253Smec * end up here. As we may be hashing out the old page and hashing into 70543253Smec * the retire vnode, we need to make sure we don't own them. 70553253Smec * Other callbacks who do hash operations also need to make sure that 70563253Smec * before they hashin to a vnode that they do not currently own the 70573253Smec * vphm mutex otherwise there will be a panic. 70583253Smec */ 70593253Smec if (mutex_owned(page_vnode_mutex(&retired_pages))) { 70603311Smec page_unlock_nocapture(pp); 70613253Smec return; 70623253Smec } 70633253Smec if (pp->p_vnode != NULL && mutex_owned(page_vnode_mutex(pp->p_vnode))) { 70643311Smec page_unlock_nocapture(pp); 70653253Smec return; 70663253Smec } 70673253Smec 70683253Smec index = PAGE_CAPTURE_HASH(pp); 70693253Smec 70703253Smec mp = &page_capture_hash[index].pchh_mutex; 70713253Smec mutex_enter(mp); 70723253Smec for (i = 0; i < 2; i++) { 70733253Smec bp = page_capture_hash[index].lists[i].next; 70743253Smec while (bp != &page_capture_hash[index].lists[i]) { 70753253Smec if (bp->pp == pp) { 70763253Smec szc = bp->szc; 70773253Smec flags = bp->flags | CAPTURE_ASYNC; 70783253Smec datap = bp->datap; 70793253Smec mutex_exit(mp); 70803253Smec (void) page_trycapture(pp, szc, flags, datap); 70813253Smec return; 70823253Smec } 70833253Smec bp = bp->next; 70843253Smec } 70853253Smec } 70863253Smec 70873253Smec /* Failed to find page in hash so clear flags and unlock it. */ 70883253Smec page_clrtoxic(pp, PR_CAPTURE); 70893253Smec page_unlock(pp); 70903253Smec 70913253Smec mutex_exit(mp); 70923253Smec } 70933253Smec 70943253Smec void 70953253Smec page_capture_init() 70963253Smec { 70973253Smec int i; 70983253Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 70993253Smec page_capture_hash[i].lists[0].next = 71003253Smec &page_capture_hash[i].lists[0]; 71013253Smec page_capture_hash[i].lists[0].prev = 71023253Smec &page_capture_hash[i].lists[0]; 71033253Smec page_capture_hash[i].lists[1].next = 71043253Smec &page_capture_hash[i].lists[1]; 71053253Smec page_capture_hash[i].lists[1].prev = 71063253Smec &page_capture_hash[i].lists[1]; 71073253Smec } 71083253Smec 71093253Smec pc_thread_shortwait = 23 * hz; 71103253Smec pc_thread_longwait = 1201 * hz; 71116695Saguzovsk pc_thread_retry = 3; 71123253Smec mutex_init(&pc_thread_mutex, NULL, MUTEX_DEFAULT, NULL); 71133253Smec cv_init(&pc_cv, NULL, CV_DEFAULT, NULL); 71143253Smec pc_thread_id = thread_create(NULL, 0, page_capture_thread, NULL, 0, &p0, 71153253Smec TS_RUN, minclsyspri); 71163253Smec } 71173253Smec 71183253Smec /* 71193253Smec * It is necessary to scrub any failing pages prior to reboot in order to 71203253Smec * prevent a latent error trap from occurring on the next boot. 71213253Smec */ 71223253Smec void 71233253Smec page_retire_mdboot() 71243253Smec { 71253253Smec page_t *pp; 71263253Smec int i, j; 71273253Smec page_capture_hash_bucket_t *bp; 71283253Smec 71293253Smec /* walk lists looking for pages to scrub */ 71303253Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 71313253Smec if (page_capture_hash[i].num_pages == 0) 71323253Smec continue; 71333253Smec 71343253Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 71353253Smec 71363253Smec for (j = 0; j < 2; j++) { 71373253Smec bp = page_capture_hash[i].lists[j].next; 71383253Smec while (bp != &page_capture_hash[i].lists[j]) { 71393253Smec pp = bp->pp; 71408555SJustin.Frank@Sun.COM if (PP_TOXIC(pp)) { 71418555SJustin.Frank@Sun.COM if (page_trylock(pp, SE_EXCL)) { 71428555SJustin.Frank@Sun.COM PP_CLRFREE(pp); 71438555SJustin.Frank@Sun.COM pagescrub(pp, 0, PAGESIZE); 71448555SJustin.Frank@Sun.COM page_unlock(pp); 71458555SJustin.Frank@Sun.COM } 71463253Smec } 71473253Smec bp = bp->next; 71483253Smec } 71493253Smec } 71503253Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 71513253Smec } 71523253Smec } 71533253Smec 71543253Smec /* 71553253Smec * Walk the page_capture_hash trying to capture pages and also cleanup old 71563253Smec * entries which have expired. 71573253Smec */ 71583253Smec void 71593253Smec page_capture_async() 71603253Smec { 71613253Smec page_t *pp; 71623253Smec int i; 71633253Smec int ret; 71643253Smec page_capture_hash_bucket_t *bp1, *bp2; 71653253Smec uint_t szc; 71663253Smec uint_t flags; 71673253Smec void *datap; 71683253Smec 71693253Smec /* If there are outstanding pages to be captured, get to work */ 71703253Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 71713253Smec if (page_capture_hash[i].num_pages == 0) 71723253Smec continue; 71733253Smec /* Append list 1 to list 0 and then walk through list 0 */ 71743253Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 71753253Smec bp1 = &page_capture_hash[i].lists[1]; 71763253Smec bp2 = bp1->next; 71773253Smec if (bp1 != bp2) { 71783253Smec bp1->prev->next = page_capture_hash[i].lists[0].next; 71793253Smec bp2->prev = &page_capture_hash[i].lists[0]; 71803253Smec page_capture_hash[i].lists[0].next->prev = bp1->prev; 71813253Smec page_capture_hash[i].lists[0].next = bp2; 71823253Smec bp1->next = bp1; 71833253Smec bp1->prev = bp1; 71843253Smec } 71853253Smec 71863253Smec /* list[1] will be empty now */ 71873253Smec 71883253Smec bp1 = page_capture_hash[i].lists[0].next; 71893253Smec while (bp1 != &page_capture_hash[i].lists[0]) { 71903253Smec /* Check expiration time */ 71913253Smec if ((lbolt > bp1->expires && bp1->expires != -1) || 71923253Smec page_deleted(bp1->pp)) { 71933253Smec page_capture_hash[i].lists[0].next = bp1->next; 71943253Smec bp1->next->prev = 71953253Smec &page_capture_hash[i].lists[0]; 71963253Smec page_capture_hash[i].num_pages--; 71973253Smec 71983253Smec /* 71993253Smec * We can safely remove the PR_CAPTURE bit 72003253Smec * without holding the EXCL lock on the page 72013253Smec * as the PR_CAPTURE bit requres that the 72023253Smec * page_capture_hash[].pchh_mutex be held 72033253Smec * to modify it. 72043253Smec */ 72053253Smec page_clrtoxic(bp1->pp, PR_CAPTURE); 72063253Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 72073253Smec kmem_free(bp1, sizeof (*bp1)); 72083253Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 72093253Smec bp1 = page_capture_hash[i].lists[0].next; 72103253Smec continue; 72113253Smec } 72123253Smec pp = bp1->pp; 72133253Smec szc = bp1->szc; 72143253Smec flags = bp1->flags; 72153253Smec datap = bp1->datap; 72163253Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 72173253Smec if (page_trylock(pp, SE_EXCL)) { 72183253Smec ret = page_trycapture(pp, szc, 72193253Smec flags | CAPTURE_ASYNC, datap); 72203253Smec } else { 72213253Smec ret = 1; /* move to walked hash */ 72223253Smec } 72233253Smec 72243253Smec if (ret != 0) { 72253253Smec /* Move to walked hash */ 72263253Smec (void) page_capture_move_to_walked(pp); 72273253Smec } 72283253Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 72293253Smec bp1 = page_capture_hash[i].lists[0].next; 72303253Smec } 72313253Smec 72323253Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 72333253Smec } 72343253Smec } 72353253Smec 72363253Smec /* 72373480Sjfrank * This function is called by the page_capture_thread, and is needed in 72383480Sjfrank * in order to initiate aio cleanup, so that pages used in aio 72393480Sjfrank * will be unlocked and subsequently retired by page_capture_thread. 72403480Sjfrank */ 72413480Sjfrank static int 72423480Sjfrank do_aio_cleanup(void) 72433480Sjfrank { 72443480Sjfrank proc_t *procp; 72453480Sjfrank int (*aio_cleanup_dr_delete_memory)(proc_t *); 72463480Sjfrank int cleaned = 0; 72473480Sjfrank 72483480Sjfrank if (modload("sys", "kaio") == -1) { 72493480Sjfrank cmn_err(CE_WARN, "do_aio_cleanup: cannot load kaio"); 72503480Sjfrank return (0); 72513480Sjfrank } 72523480Sjfrank /* 72533480Sjfrank * We use the aio_cleanup_dr_delete_memory function to 72543480Sjfrank * initiate the actual clean up; this function will wake 72553480Sjfrank * up the per-process aio_cleanup_thread. 72563480Sjfrank */ 72573480Sjfrank aio_cleanup_dr_delete_memory = (int (*)(proc_t *)) 72583480Sjfrank modgetsymvalue("aio_cleanup_dr_delete_memory", 0); 72593480Sjfrank if (aio_cleanup_dr_delete_memory == NULL) { 72603480Sjfrank cmn_err(CE_WARN, 72613480Sjfrank "aio_cleanup_dr_delete_memory not found in kaio"); 72623480Sjfrank return (0); 72633480Sjfrank } 72643480Sjfrank mutex_enter(&pidlock); 72653480Sjfrank for (procp = practive; (procp != NULL); procp = procp->p_next) { 72663480Sjfrank mutex_enter(&procp->p_lock); 72673480Sjfrank if (procp->p_aio != NULL) { 72683480Sjfrank /* cleanup proc's outstanding kaio */ 72693480Sjfrank cleaned += (*aio_cleanup_dr_delete_memory)(procp); 72703480Sjfrank } 72713480Sjfrank mutex_exit(&procp->p_lock); 72723480Sjfrank } 72733480Sjfrank mutex_exit(&pidlock); 72743480Sjfrank return (cleaned); 72753480Sjfrank } 72763480Sjfrank 72773480Sjfrank /* 72783480Sjfrank * helper function for page_capture_thread 72793480Sjfrank */ 72803480Sjfrank static void 72813480Sjfrank page_capture_handle_outstanding(void) 72823480Sjfrank { 72833480Sjfrank int ntry; 72843480Sjfrank 72859544SChristopher.Baumbauer@Sun.COM /* Reap pages before attempting capture pages */ 72869544SChristopher.Baumbauer@Sun.COM kmem_reap(); 72879544SChristopher.Baumbauer@Sun.COM 72889544SChristopher.Baumbauer@Sun.COM if ((page_retire_pend_count() > page_retire_pend_kas_count()) && 72899544SChristopher.Baumbauer@Sun.COM hat_supported(HAT_DYNAMIC_ISM_UNMAP, (void *)0)) { 72903480Sjfrank /* 72918046SVijay.Balakrishna@Sun.COM * Note: Purging only for platforms that support 72928046SVijay.Balakrishna@Sun.COM * ISM hat_pageunload() - mainly SPARC. On x86/x64 72938046SVijay.Balakrishna@Sun.COM * platforms ISM pages SE_SHARED locked until destroyed. 72943480Sjfrank */ 72956695Saguzovsk 72966695Saguzovsk /* disable and purge seg_pcache */ 72976695Saguzovsk (void) seg_p_disable(); 72986695Saguzovsk for (ntry = 0; ntry < pc_thread_retry; ntry++) { 72996695Saguzovsk if (!page_retire_pend_count()) 73006695Saguzovsk break; 73016695Saguzovsk if (do_aio_cleanup()) { 73026695Saguzovsk /* 73036695Saguzovsk * allow the apps cleanup threads 73046695Saguzovsk * to run 73056695Saguzovsk */ 73066695Saguzovsk delay(pc_thread_shortwait); 73073480Sjfrank } 73083480Sjfrank page_capture_async(); 73093480Sjfrank } 73106695Saguzovsk /* reenable seg_pcache */ 73116695Saguzovsk seg_p_enable(); 73129544SChristopher.Baumbauer@Sun.COM 73139544SChristopher.Baumbauer@Sun.COM /* completed what can be done. break out */ 73149544SChristopher.Baumbauer@Sun.COM return; 73159544SChristopher.Baumbauer@Sun.COM } 73169544SChristopher.Baumbauer@Sun.COM 73179544SChristopher.Baumbauer@Sun.COM /* 73189544SChristopher.Baumbauer@Sun.COM * For kernel pages and/or unsupported HAT_DYNAMIC_ISM_UNMAP, reap 73199544SChristopher.Baumbauer@Sun.COM * and then attempt to capture. 73209544SChristopher.Baumbauer@Sun.COM */ 73219544SChristopher.Baumbauer@Sun.COM seg_preap(); 73229544SChristopher.Baumbauer@Sun.COM page_capture_async(); 73233480Sjfrank } 73243480Sjfrank 73253480Sjfrank /* 73263253Smec * The page_capture_thread loops forever, looking to see if there are 73273253Smec * pages still waiting to be captured. 73283253Smec */ 73293253Smec static void 73303253Smec page_capture_thread(void) 73313253Smec { 73323253Smec callb_cpr_t c; 73333253Smec int outstanding; 73343253Smec int i; 73353253Smec 73363253Smec CALLB_CPR_INIT(&c, &pc_thread_mutex, callb_generic_cpr, "page_capture"); 73373253Smec 73383253Smec mutex_enter(&pc_thread_mutex); 73393253Smec for (;;) { 73403253Smec outstanding = 0; 73413253Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) 73423253Smec outstanding += page_capture_hash[i].num_pages; 73433253Smec if (outstanding) { 73443480Sjfrank page_capture_handle_outstanding(); 73453253Smec CALLB_CPR_SAFE_BEGIN(&c); 73463253Smec (void) cv_timedwait(&pc_cv, &pc_thread_mutex, 73473253Smec lbolt + pc_thread_shortwait); 73483253Smec CALLB_CPR_SAFE_END(&c, &pc_thread_mutex); 73493253Smec } else { 73503253Smec CALLB_CPR_SAFE_BEGIN(&c); 73513253Smec (void) cv_timedwait(&pc_cv, &pc_thread_mutex, 73523253Smec lbolt + pc_thread_longwait); 73533253Smec CALLB_CPR_SAFE_END(&c, &pc_thread_mutex); 73543253Smec } 73553253Smec } 73563253Smec /*NOTREACHED*/ 73573253Smec } 73586880Sdv142724 /* 73596880Sdv142724 * Attempt to locate a bucket that has enough pages to satisfy the request. 73606880Sdv142724 * The initial check is done without the lock to avoid unneeded contention. 73616880Sdv142724 * The function returns 1 if enough pages were found, else 0 if it could not 73626880Sdv142724 * find enough pages in a bucket. 73636880Sdv142724 */ 73646880Sdv142724 static int 73656880Sdv142724 pcf_decrement_bucket(pgcnt_t npages) 73666880Sdv142724 { 73676880Sdv142724 struct pcf *p; 73686880Sdv142724 struct pcf *q; 73696880Sdv142724 int i; 73706880Sdv142724 73716880Sdv142724 p = &pcf[PCF_INDEX()]; 73726880Sdv142724 q = &pcf[pcf_fanout]; 73736880Sdv142724 for (i = 0; i < pcf_fanout; i++) { 73746880Sdv142724 if (p->pcf_count > npages) { 73756880Sdv142724 /* 73766880Sdv142724 * a good one to try. 73776880Sdv142724 */ 73786880Sdv142724 mutex_enter(&p->pcf_lock); 73796880Sdv142724 if (p->pcf_count > npages) { 73806880Sdv142724 p->pcf_count -= (uint_t)npages; 73816880Sdv142724 /* 73826880Sdv142724 * freemem is not protected by any lock. 73836880Sdv142724 * Thus, we cannot have any assertion 73846880Sdv142724 * containing freemem here. 73856880Sdv142724 */ 73866880Sdv142724 freemem -= npages; 73876880Sdv142724 mutex_exit(&p->pcf_lock); 73886880Sdv142724 return (1); 73896880Sdv142724 } 73906880Sdv142724 mutex_exit(&p->pcf_lock); 73916880Sdv142724 } 73926880Sdv142724 p++; 73936880Sdv142724 if (p >= q) { 73946880Sdv142724 p = pcf; 73956880Sdv142724 } 73966880Sdv142724 } 73976880Sdv142724 return (0); 73986880Sdv142724 } 73996880Sdv142724 74006880Sdv142724 /* 74016880Sdv142724 * Arguments: 74026880Sdv142724 * pcftotal_ret: If the value is not NULL and we have walked all the 74036880Sdv142724 * buckets but did not find enough pages then it will 74046880Sdv142724 * be set to the total number of pages in all the pcf 74056880Sdv142724 * buckets. 74066880Sdv142724 * npages: Is the number of pages we have been requested to 74076880Sdv142724 * find. 74086880Sdv142724 * unlock: If set to 0 we will leave the buckets locked if the 74096880Sdv142724 * requested number of pages are not found. 74106880Sdv142724 * 74116880Sdv142724 * Go and try to satisfy the page request from any number of buckets. 74126880Sdv142724 * This can be a very expensive operation as we have to lock the buckets 74136880Sdv142724 * we are checking (and keep them locked), starting at bucket 0. 74146880Sdv142724 * 74156880Sdv142724 * The function returns 1 if enough pages were found, else 0 if it could not 74166880Sdv142724 * find enough pages in the buckets. 74176880Sdv142724 * 74186880Sdv142724 */ 74196880Sdv142724 static int 74206880Sdv142724 pcf_decrement_multiple(pgcnt_t *pcftotal_ret, pgcnt_t npages, int unlock) 74216880Sdv142724 { 74226880Sdv142724 struct pcf *p; 74236880Sdv142724 pgcnt_t pcftotal; 74246880Sdv142724 int i; 74256880Sdv142724 74266880Sdv142724 p = pcf; 74276880Sdv142724 /* try to collect pages from several pcf bins */ 74286880Sdv142724 for (pcftotal = 0, i = 0; i < pcf_fanout; i++) { 74296880Sdv142724 mutex_enter(&p->pcf_lock); 74306880Sdv142724 pcftotal += p->pcf_count; 74316880Sdv142724 if (pcftotal >= npages) { 74326880Sdv142724 /* 74336880Sdv142724 * Wow! There are enough pages laying around 74346880Sdv142724 * to satisfy the request. Do the accounting, 74356880Sdv142724 * drop the locks we acquired, and go back. 74366880Sdv142724 * 74376880Sdv142724 * freemem is not protected by any lock. So, 74386880Sdv142724 * we cannot have any assertion containing 74396880Sdv142724 * freemem. 74406880Sdv142724 */ 74416880Sdv142724 freemem -= npages; 74426880Sdv142724 while (p >= pcf) { 74436880Sdv142724 if (p->pcf_count <= npages) { 74446880Sdv142724 npages -= p->pcf_count; 74456880Sdv142724 p->pcf_count = 0; 74466880Sdv142724 } else { 74476880Sdv142724 p->pcf_count -= (uint_t)npages; 74486880Sdv142724 npages = 0; 74496880Sdv142724 } 74506880Sdv142724 mutex_exit(&p->pcf_lock); 74516880Sdv142724 p--; 74526880Sdv142724 } 74536880Sdv142724 ASSERT(npages == 0); 74546880Sdv142724 return (1); 74556880Sdv142724 } 74566880Sdv142724 p++; 74576880Sdv142724 } 74586880Sdv142724 if (unlock) { 74596880Sdv142724 /* failed to collect pages - release the locks */ 74606880Sdv142724 while (--p >= pcf) { 74616880Sdv142724 mutex_exit(&p->pcf_lock); 74626880Sdv142724 } 74636880Sdv142724 } 74646880Sdv142724 if (pcftotal_ret != NULL) 74656880Sdv142724 *pcftotal_ret = pcftotal; 74666880Sdv142724 return (0); 74676880Sdv142724 } 7468