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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 320Sstevel@tonic-gate * The Regents of the University of California 330Sstevel@tonic-gate * All Rights Reserved 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 370Sstevel@tonic-gate * contributors. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * VM - physical page management. 440Sstevel@tonic-gate */ 450Sstevel@tonic-gate 460Sstevel@tonic-gate #include <sys/types.h> 470Sstevel@tonic-gate #include <sys/t_lock.h> 480Sstevel@tonic-gate #include <sys/param.h> 490Sstevel@tonic-gate #include <sys/systm.h> 500Sstevel@tonic-gate #include <sys/errno.h> 510Sstevel@tonic-gate #include <sys/time.h> 520Sstevel@tonic-gate #include <sys/vnode.h> 530Sstevel@tonic-gate #include <sys/vm.h> 540Sstevel@tonic-gate #include <sys/vtrace.h> 550Sstevel@tonic-gate #include <sys/swap.h> 560Sstevel@tonic-gate #include <sys/cmn_err.h> 570Sstevel@tonic-gate #include <sys/tuneable.h> 580Sstevel@tonic-gate #include <sys/sysmacros.h> 590Sstevel@tonic-gate #include <sys/cpuvar.h> 600Sstevel@tonic-gate #include <sys/callb.h> 610Sstevel@tonic-gate #include <sys/debug.h> 620Sstevel@tonic-gate #include <sys/tnf_probe.h> 630Sstevel@tonic-gate #include <sys/condvar_impl.h> 640Sstevel@tonic-gate #include <sys/mem_config.h> 650Sstevel@tonic-gate #include <sys/mem_cage.h> 660Sstevel@tonic-gate #include <sys/kmem.h> 670Sstevel@tonic-gate #include <sys/atomic.h> 680Sstevel@tonic-gate #include <sys/strlog.h> 690Sstevel@tonic-gate #include <sys/mman.h> 700Sstevel@tonic-gate #include <sys/ontrap.h> 710Sstevel@tonic-gate #include <sys/lgrp.h> 720Sstevel@tonic-gate #include <sys/vfs.h> 730Sstevel@tonic-gate 740Sstevel@tonic-gate #include <vm/hat.h> 750Sstevel@tonic-gate #include <vm/anon.h> 760Sstevel@tonic-gate #include <vm/page.h> 770Sstevel@tonic-gate #include <vm/seg.h> 780Sstevel@tonic-gate #include <vm/pvn.h> 790Sstevel@tonic-gate #include <vm/seg_kmem.h> 800Sstevel@tonic-gate #include <vm/vm_dep.h> 810Sstevel@tonic-gate 820Sstevel@tonic-gate #include <fs/fs_subr.h> 830Sstevel@tonic-gate 840Sstevel@tonic-gate static int nopageage = 0; 850Sstevel@tonic-gate 860Sstevel@tonic-gate static pgcnt_t max_page_get; /* max page_get request size in pages */ 870Sstevel@tonic-gate pgcnt_t total_pages = 0; /* total number of pages (used by /proc) */ 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * vnode for all pages which are retired from the VM system; 910Sstevel@tonic-gate * such as pages with Uncorrectable Errors. 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate struct vnode retired_ppages; 940Sstevel@tonic-gate 950Sstevel@tonic-gate static void page_retired_init(void); 960Sstevel@tonic-gate static void retired_dispose(vnode_t *vp, page_t *pp, int flag, 970Sstevel@tonic-gate int dn, cred_t *cr); 980Sstevel@tonic-gate static void retired_inactive(vnode_t *vp, cred_t *cr); 990Sstevel@tonic-gate static void page_retired(page_t *pp); 1000Sstevel@tonic-gate static void retired_page_removed(page_t *pp); 1010Sstevel@tonic-gate void page_unretire_pages(void); 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * The maximum number of pages that will be unretired in one iteration. 1050Sstevel@tonic-gate * This number is totally arbitrary. 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate #define UNRETIRE_PAGES 256 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * We limit the number of pages that may be retired to 1110Sstevel@tonic-gate * a percentage of the total physical memory. Note that 1120Sstevel@tonic-gate * the percentage values are stored as 'basis points', 1130Sstevel@tonic-gate * ie, 100 basis points is 1%. 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate #define MAX_PAGES_RETIRED_BPS_DEFAULT 10 /* .1% */ 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate uint64_t max_pages_retired_bps = MAX_PAGES_RETIRED_BPS_DEFAULT; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate static int pages_retired_limit_exceeded(void); 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * operations vector for vnode with retired pages. Only VOP_DISPOSE 1230Sstevel@tonic-gate * and VOP_INACTIVE are intercepted. 1240Sstevel@tonic-gate */ 1250Sstevel@tonic-gate struct vnodeops retired_vnodeops = { 1260Sstevel@tonic-gate "retired_vnodeops", 1270Sstevel@tonic-gate fs_nosys, /* open */ 1280Sstevel@tonic-gate fs_nosys, /* close */ 1290Sstevel@tonic-gate fs_nosys, /* read */ 1300Sstevel@tonic-gate fs_nosys, /* write */ 1310Sstevel@tonic-gate fs_nosys, /* ioctl */ 1320Sstevel@tonic-gate fs_nosys, /* setfl */ 1330Sstevel@tonic-gate fs_nosys, /* getattr */ 1340Sstevel@tonic-gate fs_nosys, /* setattr */ 1350Sstevel@tonic-gate fs_nosys, /* access */ 1360Sstevel@tonic-gate fs_nosys, /* lookup */ 1370Sstevel@tonic-gate fs_nosys, /* create */ 1380Sstevel@tonic-gate fs_nosys, /* remove */ 1390Sstevel@tonic-gate fs_nosys, /* link */ 1400Sstevel@tonic-gate fs_nosys, /* rename */ 1410Sstevel@tonic-gate fs_nosys, /* mkdir */ 1420Sstevel@tonic-gate fs_nosys, /* rmdir */ 1430Sstevel@tonic-gate fs_nosys, /* readdir */ 1440Sstevel@tonic-gate fs_nosys, /* symlink */ 1450Sstevel@tonic-gate fs_nosys, /* readlink */ 1460Sstevel@tonic-gate fs_nosys, /* fsync */ 1470Sstevel@tonic-gate retired_inactive, 1480Sstevel@tonic-gate fs_nosys, /* fid */ 1490Sstevel@tonic-gate fs_rwlock, /* rwlock */ 1500Sstevel@tonic-gate fs_rwunlock, /* rwunlock */ 1510Sstevel@tonic-gate fs_nosys, /* seek */ 1520Sstevel@tonic-gate fs_nosys, /* cmp */ 1530Sstevel@tonic-gate fs_nosys, /* frlock */ 1540Sstevel@tonic-gate fs_nosys, /* space */ 1550Sstevel@tonic-gate fs_nosys, /* realvp */ 1560Sstevel@tonic-gate fs_nosys, /* getpage */ 1570Sstevel@tonic-gate fs_nosys, /* putpage */ 1580Sstevel@tonic-gate fs_nosys_map, 1590Sstevel@tonic-gate fs_nosys_addmap, 1600Sstevel@tonic-gate fs_nosys, /* delmap */ 1610Sstevel@tonic-gate fs_nosys_poll, 1620Sstevel@tonic-gate fs_nosys, /* dump */ 1630Sstevel@tonic-gate fs_nosys, /* l_pathconf */ 1640Sstevel@tonic-gate fs_nosys, /* pageio */ 1650Sstevel@tonic-gate fs_nosys, /* dumpctl */ 1660Sstevel@tonic-gate retired_dispose, 1670Sstevel@tonic-gate fs_nosys, /* setsecattr */ 1680Sstevel@tonic-gate fs_nosys, /* getsecatt */ 1690Sstevel@tonic-gate fs_nosys, /* shrlock */ 1700Sstevel@tonic-gate fs_vnevent_nosupport /* vnevent */ 1710Sstevel@tonic-gate }; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * freemem_lock protects all freemem variables: 1750Sstevel@tonic-gate * availrmem. Also this lock protects the globals which track the 1760Sstevel@tonic-gate * availrmem changes for accurate kernel footprint calculation. 1770Sstevel@tonic-gate * See below for an explanation of these 1780Sstevel@tonic-gate * globals. 1790Sstevel@tonic-gate */ 1800Sstevel@tonic-gate kmutex_t freemem_lock; 1810Sstevel@tonic-gate pgcnt_t availrmem; 1820Sstevel@tonic-gate pgcnt_t availrmem_initial; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* 1850Sstevel@tonic-gate * These globals track availrmem changes to get a more accurate 1860Sstevel@tonic-gate * estimate of tke kernel size. Historically pp_kernel is used for 1870Sstevel@tonic-gate * kernel size and is based on availrmem. But availrmem is adjusted for 1880Sstevel@tonic-gate * locked pages in the system not just for kernel locked pages. 1890Sstevel@tonic-gate * These new counters will track the pages locked through segvn and 1900Sstevel@tonic-gate * by explicit user locking. 1910Sstevel@tonic-gate * 1920Sstevel@tonic-gate * segvn_pages_locked : This keeps track on a global basis how many pages 1930Sstevel@tonic-gate * are currently locked because of I/O. 1940Sstevel@tonic-gate * 1950Sstevel@tonic-gate * pages_locked : How many pages are locked becuase of user specified 1960Sstevel@tonic-gate * locking through mlock or plock. 1970Sstevel@tonic-gate * 1980Sstevel@tonic-gate * pages_useclaim,pages_claimed : These two variables track the 1990Sstevel@tonic-gate * cliam adjustments because of the protection changes on a segvn segment. 2000Sstevel@tonic-gate * 2010Sstevel@tonic-gate * All these globals are protected by the same lock which protects availrmem. 2020Sstevel@tonic-gate */ 2030Sstevel@tonic-gate pgcnt_t segvn_pages_locked; 2040Sstevel@tonic-gate pgcnt_t pages_locked; 2050Sstevel@tonic-gate pgcnt_t pages_useclaim; 2060Sstevel@tonic-gate pgcnt_t pages_claimed; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate /* 2100Sstevel@tonic-gate * new_freemem_lock protects freemem, freemem_wait & freemem_cv. 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate static kmutex_t new_freemem_lock; 2130Sstevel@tonic-gate static uint_t freemem_wait; /* someone waiting for freemem */ 2140Sstevel@tonic-gate static kcondvar_t freemem_cv; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * The logical page free list is maintained as two lists, the 'free' 2180Sstevel@tonic-gate * and the 'cache' lists. 2190Sstevel@tonic-gate * The free list contains those pages that should be reused first. 2200Sstevel@tonic-gate * 2210Sstevel@tonic-gate * The implementation of the lists is machine dependent. 2220Sstevel@tonic-gate * page_get_freelist(), page_get_cachelist(), 2230Sstevel@tonic-gate * page_list_sub(), and page_list_add() 2240Sstevel@tonic-gate * form the interface to the machine dependent implementation. 2250Sstevel@tonic-gate * 2260Sstevel@tonic-gate * Pages with p_free set are on the cache list. 2270Sstevel@tonic-gate * Pages with p_free and p_age set are on the free list, 2280Sstevel@tonic-gate * 2290Sstevel@tonic-gate * A page may be locked while on either list. 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* 2330Sstevel@tonic-gate * free list accounting stuff. 2340Sstevel@tonic-gate * 2350Sstevel@tonic-gate * 2360Sstevel@tonic-gate * Spread out the value for the number of pages on the 2370Sstevel@tonic-gate * page free and page cache lists. If there is just one 2380Sstevel@tonic-gate * value, then it must be under just one lock. 2390Sstevel@tonic-gate * The lock contention and cache traffic are a real bother. 2400Sstevel@tonic-gate * 2410Sstevel@tonic-gate * When we acquire and then drop a single pcf lock 2420Sstevel@tonic-gate * we can start in the middle of the array of pcf structures. 2430Sstevel@tonic-gate * If we acquire more than one pcf lock at a time, we need to 2440Sstevel@tonic-gate * start at the front to avoid deadlocking. 2450Sstevel@tonic-gate * 2460Sstevel@tonic-gate * pcf_count holds the number of pages in each pool. 2470Sstevel@tonic-gate * 2480Sstevel@tonic-gate * pcf_block is set when page_create_get_something() has asked the 2490Sstevel@tonic-gate * PSM page freelist and page cachelist routines without specifying 2500Sstevel@tonic-gate * a color and nothing came back. This is used to block anything 2510Sstevel@tonic-gate * else from moving pages from one list to the other while the 2520Sstevel@tonic-gate * lists are searched again. If a page is freeed while pcf_block is 2530Sstevel@tonic-gate * set, then pcf_reserve is incremented. pcgs_unblock() takes care 2540Sstevel@tonic-gate * of clearning pcf_block, doing the wakeups, etc. 2550Sstevel@tonic-gate */ 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate #if NCPU <= 4 2580Sstevel@tonic-gate #define PAD 1 2590Sstevel@tonic-gate #define PCF_FANOUT 4 2600Sstevel@tonic-gate static uint_t pcf_mask = PCF_FANOUT - 1; 2610Sstevel@tonic-gate #else 2620Sstevel@tonic-gate #define PAD 9 2630Sstevel@tonic-gate #ifdef sun4v 2640Sstevel@tonic-gate #define PCF_FANOUT 32 2650Sstevel@tonic-gate #else 2660Sstevel@tonic-gate #define PCF_FANOUT 128 2670Sstevel@tonic-gate #endif 2680Sstevel@tonic-gate static uint_t pcf_mask = PCF_FANOUT - 1; 2690Sstevel@tonic-gate #endif 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate struct pcf { 2720Sstevel@tonic-gate uint_t pcf_touch; /* just to help the cache */ 2730Sstevel@tonic-gate uint_t pcf_count; /* page count */ 2740Sstevel@tonic-gate kmutex_t pcf_lock; /* protects the structure */ 2750Sstevel@tonic-gate uint_t pcf_wait; /* number of waiters */ 2760Sstevel@tonic-gate uint_t pcf_block; /* pcgs flag to page_free() */ 2770Sstevel@tonic-gate uint_t pcf_reserve; /* pages freed after pcf_block set */ 2780Sstevel@tonic-gate uint_t pcf_fill[PAD]; /* to line up on the caches */ 2790Sstevel@tonic-gate }; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate static struct pcf pcf[PCF_FANOUT]; 2820Sstevel@tonic-gate #define PCF_INDEX() ((CPU->cpu_id) & (pcf_mask)) 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate kmutex_t pcgs_lock; /* serializes page_create_get_ */ 2850Sstevel@tonic-gate kmutex_t pcgs_cagelock; /* serializes NOSLEEP cage allocs */ 2860Sstevel@tonic-gate kmutex_t pcgs_wait_lock; /* used for delay in pcgs */ 2870Sstevel@tonic-gate static kcondvar_t pcgs_cv; /* cv for delay in pcgs */ 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate #define PAGE_LOCK_MAXIMUM \ 2900Sstevel@tonic-gate ((1 << (sizeof (((page_t *)0)->p_lckcnt) * NBBY)) - 1) 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * Control over the verbosity of page retirement. When set to zero, no messages 2940Sstevel@tonic-gate * will be printed. A value of one will trigger messages for retirement 2950Sstevel@tonic-gate * operations, and is intended for processors which don't yet support FMA 2960Sstevel@tonic-gate * (spitfire). Two will cause verbose messages to be printed when retirements 2970Sstevel@tonic-gate * complete, and is intended only for debugging purposes. 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate int page_retire_messages = 0; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate #ifdef VM_STATS 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /* 3040Sstevel@tonic-gate * No locks, but so what, they are only statistics. 3050Sstevel@tonic-gate */ 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate static struct page_tcnt { 3080Sstevel@tonic-gate int pc_free_cache; /* free's into cache list */ 3090Sstevel@tonic-gate int pc_free_dontneed; /* free's with dontneed */ 3100Sstevel@tonic-gate int pc_free_pageout; /* free's from pageout */ 3110Sstevel@tonic-gate int pc_free_free; /* free's into free list */ 3120Sstevel@tonic-gate int pc_free_pages; /* free's into large page free list */ 3130Sstevel@tonic-gate int pc_destroy_pages; /* large page destroy's */ 3140Sstevel@tonic-gate int pc_get_cache; /* get's from cache list */ 3150Sstevel@tonic-gate int pc_get_free; /* get's from free list */ 3160Sstevel@tonic-gate int pc_reclaim; /* reclaim's */ 3170Sstevel@tonic-gate int pc_abortfree; /* abort's of free pages */ 3180Sstevel@tonic-gate int pc_find_hit; /* find's that find page */ 3190Sstevel@tonic-gate int pc_find_miss; /* find's that don't find page */ 3200Sstevel@tonic-gate int pc_destroy_free; /* # of free pages destroyed */ 3210Sstevel@tonic-gate #define PC_HASH_CNT (4*PAGE_HASHAVELEN) 3220Sstevel@tonic-gate int pc_find_hashlen[PC_HASH_CNT+1]; 3230Sstevel@tonic-gate int pc_addclaim_pages; 3240Sstevel@tonic-gate int pc_subclaim_pages; 3250Sstevel@tonic-gate int pc_free_replacement_page[2]; 3260Sstevel@tonic-gate int pc_try_demote_pages[6]; 3270Sstevel@tonic-gate int pc_demote_pages[2]; 3280Sstevel@tonic-gate } pagecnt; 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate uint_t hashin_count; 3310Sstevel@tonic-gate uint_t hashin_not_held; 3320Sstevel@tonic-gate uint_t hashin_already; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate uint_t hashout_count; 3350Sstevel@tonic-gate uint_t hashout_not_held; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate uint_t page_create_count; 3380Sstevel@tonic-gate uint_t page_create_not_enough; 3390Sstevel@tonic-gate uint_t page_create_not_enough_again; 3400Sstevel@tonic-gate uint_t page_create_zero; 3410Sstevel@tonic-gate uint_t page_create_hashout; 3420Sstevel@tonic-gate uint_t page_create_page_lock_failed; 3430Sstevel@tonic-gate uint_t page_create_trylock_failed; 3440Sstevel@tonic-gate uint_t page_create_found_one; 3450Sstevel@tonic-gate uint_t page_create_hashin_failed; 3460Sstevel@tonic-gate uint_t page_create_dropped_phm; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate uint_t page_create_new; 3490Sstevel@tonic-gate uint_t page_create_exists; 3500Sstevel@tonic-gate uint_t page_create_putbacks; 3510Sstevel@tonic-gate uint_t page_create_overshoot; 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate uint_t page_reclaim_zero; 3540Sstevel@tonic-gate uint_t page_reclaim_zero_locked; 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate uint_t page_rename_exists; 3570Sstevel@tonic-gate uint_t page_rename_count; 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate uint_t page_lookup_cnt[20]; 3600Sstevel@tonic-gate uint_t page_lookup_nowait_cnt[10]; 3610Sstevel@tonic-gate uint_t page_find_cnt; 3620Sstevel@tonic-gate uint_t page_exists_cnt; 3630Sstevel@tonic-gate uint_t page_exists_forreal_cnt; 3640Sstevel@tonic-gate uint_t page_lookup_dev_cnt; 3650Sstevel@tonic-gate uint_t get_cachelist_cnt; 3660Sstevel@tonic-gate uint_t page_create_cnt[10]; 3670Sstevel@tonic-gate uint_t alloc_pages[8]; 3680Sstevel@tonic-gate uint_t page_exphcontg[19]; 3690Sstevel@tonic-gate uint_t page_create_large_cnt[10]; 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * Collects statistics. 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate #define PAGE_HASH_SEARCH(index, pp, vp, off) { \ 3750Sstevel@tonic-gate uint_t mylen = 0; \ 3760Sstevel@tonic-gate \ 3770Sstevel@tonic-gate for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash, mylen++) { \ 3780Sstevel@tonic-gate if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \ 3790Sstevel@tonic-gate break; \ 3800Sstevel@tonic-gate } \ 3810Sstevel@tonic-gate if ((pp) != NULL) \ 3820Sstevel@tonic-gate pagecnt.pc_find_hit++; \ 3830Sstevel@tonic-gate else \ 3840Sstevel@tonic-gate pagecnt.pc_find_miss++; \ 3850Sstevel@tonic-gate if (mylen > PC_HASH_CNT) \ 3860Sstevel@tonic-gate mylen = PC_HASH_CNT; \ 3870Sstevel@tonic-gate pagecnt.pc_find_hashlen[mylen]++; \ 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate #else /* VM_STATS */ 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate /* 3930Sstevel@tonic-gate * Don't collect statistics 3940Sstevel@tonic-gate */ 3950Sstevel@tonic-gate #define PAGE_HASH_SEARCH(index, pp, vp, off) { \ 3960Sstevel@tonic-gate for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash) { \ 3970Sstevel@tonic-gate if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \ 3980Sstevel@tonic-gate break; \ 3990Sstevel@tonic-gate } \ 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate #endif /* VM_STATS */ 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate #ifdef DEBUG 4070Sstevel@tonic-gate #define MEMSEG_SEARCH_STATS 4080Sstevel@tonic-gate #endif 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate #ifdef MEMSEG_SEARCH_STATS 4110Sstevel@tonic-gate struct memseg_stats { 4120Sstevel@tonic-gate uint_t nsearch; 4130Sstevel@tonic-gate uint_t nlastwon; 4140Sstevel@tonic-gate uint_t nhashwon; 4150Sstevel@tonic-gate uint_t nnotfound; 4160Sstevel@tonic-gate } memseg_stats; 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate #define MEMSEG_STAT_INCR(v) \ 4190Sstevel@tonic-gate atomic_add_32(&memseg_stats.v, 1) 4200Sstevel@tonic-gate #else 4210Sstevel@tonic-gate #define MEMSEG_STAT_INCR(x) 4220Sstevel@tonic-gate #endif 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate struct memseg *memsegs; /* list of memory segments */ 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate static void page_init_mem_config(void); 4280Sstevel@tonic-gate static int page_do_hashin(page_t *, vnode_t *, u_offset_t); 4290Sstevel@tonic-gate static void page_do_hashout(page_t *); 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate static void page_demote_vp_pages(page_t *); 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * vm subsystem related initialization 4350Sstevel@tonic-gate */ 4360Sstevel@tonic-gate void 4370Sstevel@tonic-gate vm_init(void) 4380Sstevel@tonic-gate { 4390Sstevel@tonic-gate boolean_t callb_vm_cpr(void *, int); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate (void) callb_add(callb_vm_cpr, 0, CB_CL_CPR_VM, "vm"); 4420Sstevel@tonic-gate page_init_mem_config(); 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate /* 4450Sstevel@tonic-gate * initialise the vnode for retired pages 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate page_retired_init(); 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate /* 4510Sstevel@tonic-gate * This function is called at startup and when memory is added or deleted. 4520Sstevel@tonic-gate */ 4530Sstevel@tonic-gate void 4540Sstevel@tonic-gate init_pages_pp_maximum() 4550Sstevel@tonic-gate { 4560Sstevel@tonic-gate static pgcnt_t p_min; 4570Sstevel@tonic-gate static pgcnt_t pages_pp_maximum_startup; 4580Sstevel@tonic-gate static pgcnt_t avrmem_delta; 4590Sstevel@tonic-gate static int init_done; 4600Sstevel@tonic-gate static int user_set; /* true if set in /etc/system */ 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate if (init_done == 0) { 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate /* If the user specified a value, save it */ 4650Sstevel@tonic-gate if (pages_pp_maximum != 0) { 4660Sstevel@tonic-gate user_set = 1; 4670Sstevel@tonic-gate pages_pp_maximum_startup = pages_pp_maximum; 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate /* 4710Sstevel@tonic-gate * Setting of pages_pp_maximum is based first time 4720Sstevel@tonic-gate * on the value of availrmem just after the start-up 4730Sstevel@tonic-gate * allocations. To preserve this relationship at run 4740Sstevel@tonic-gate * time, use a delta from availrmem_initial. 4750Sstevel@tonic-gate */ 4760Sstevel@tonic-gate ASSERT(availrmem_initial >= availrmem); 4770Sstevel@tonic-gate avrmem_delta = availrmem_initial - availrmem; 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate /* The allowable floor of pages_pp_maximum */ 4800Sstevel@tonic-gate p_min = tune.t_minarmem + 100; 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate /* Make sure we don't come through here again. */ 4830Sstevel@tonic-gate init_done = 1; 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate /* 4860Sstevel@tonic-gate * Determine pages_pp_maximum, the number of currently available 4870Sstevel@tonic-gate * pages (availrmem) that can't be `locked'. If not set by 4880Sstevel@tonic-gate * the user, we set it to 4% of the currently available memory 4890Sstevel@tonic-gate * plus 4MB. 4900Sstevel@tonic-gate * But we also insist that it be greater than tune.t_minarmem; 4910Sstevel@tonic-gate * otherwise a process could lock down a lot of memory, get swapped 4920Sstevel@tonic-gate * out, and never have enough to get swapped back in. 4930Sstevel@tonic-gate */ 4940Sstevel@tonic-gate if (user_set) 4950Sstevel@tonic-gate pages_pp_maximum = pages_pp_maximum_startup; 4960Sstevel@tonic-gate else 4970Sstevel@tonic-gate pages_pp_maximum = ((availrmem_initial - avrmem_delta) / 25) 4980Sstevel@tonic-gate + btop(4 * 1024 * 1024); 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate if (pages_pp_maximum <= p_min) { 5010Sstevel@tonic-gate pages_pp_maximum = p_min; 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate void 5060Sstevel@tonic-gate set_max_page_get(pgcnt_t target_total_pages) 5070Sstevel@tonic-gate { 5080Sstevel@tonic-gate max_page_get = target_total_pages / 2; 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate static pgcnt_t pending_delete; 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate /*ARGSUSED*/ 5140Sstevel@tonic-gate static void 5150Sstevel@tonic-gate page_mem_config_post_add( 5160Sstevel@tonic-gate void *arg, 5170Sstevel@tonic-gate pgcnt_t delta_pages) 5180Sstevel@tonic-gate { 5190Sstevel@tonic-gate set_max_page_get(total_pages - pending_delete); 5200Sstevel@tonic-gate init_pages_pp_maximum(); 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate /*ARGSUSED*/ 5240Sstevel@tonic-gate static int 5250Sstevel@tonic-gate page_mem_config_pre_del( 5260Sstevel@tonic-gate void *arg, 5270Sstevel@tonic-gate pgcnt_t delta_pages) 5280Sstevel@tonic-gate { 5290Sstevel@tonic-gate pgcnt_t nv; 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate nv = atomic_add_long_nv(&pending_delete, (spgcnt_t)delta_pages); 5320Sstevel@tonic-gate set_max_page_get(total_pages - nv); 5330Sstevel@tonic-gate return (0); 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /*ARGSUSED*/ 5370Sstevel@tonic-gate static void 5380Sstevel@tonic-gate page_mem_config_post_del( 5390Sstevel@tonic-gate void *arg, 5400Sstevel@tonic-gate pgcnt_t delta_pages, 5410Sstevel@tonic-gate int cancelled) 5420Sstevel@tonic-gate { 5430Sstevel@tonic-gate pgcnt_t nv; 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate nv = atomic_add_long_nv(&pending_delete, -(spgcnt_t)delta_pages); 5460Sstevel@tonic-gate set_max_page_get(total_pages - nv); 5470Sstevel@tonic-gate if (!cancelled) 5480Sstevel@tonic-gate init_pages_pp_maximum(); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate static kphysm_setup_vector_t page_mem_config_vec = { 5520Sstevel@tonic-gate KPHYSM_SETUP_VECTOR_VERSION, 5530Sstevel@tonic-gate page_mem_config_post_add, 5540Sstevel@tonic-gate page_mem_config_pre_del, 5550Sstevel@tonic-gate page_mem_config_post_del, 5560Sstevel@tonic-gate }; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate static void 5590Sstevel@tonic-gate page_init_mem_config(void) 5600Sstevel@tonic-gate { 5610Sstevel@tonic-gate int ret; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate ret = kphysm_setup_func_register(&page_mem_config_vec, (void *)NULL); 5640Sstevel@tonic-gate ASSERT(ret == 0); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * Evenly spread out the PCF counters for large free pages 5690Sstevel@tonic-gate */ 5700Sstevel@tonic-gate static void 5710Sstevel@tonic-gate page_free_large_ctr(pgcnt_t npages) 5720Sstevel@tonic-gate { 5730Sstevel@tonic-gate static struct pcf *p = pcf; 5740Sstevel@tonic-gate pgcnt_t lump; 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate freemem += npages; 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate lump = roundup(npages, PCF_FANOUT) / PCF_FANOUT; 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate while (npages > 0) { 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate ASSERT(!p->pcf_block); 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate if (lump < npages) { 5850Sstevel@tonic-gate p->pcf_count += (uint_t)lump; 5860Sstevel@tonic-gate npages -= lump; 5870Sstevel@tonic-gate } else { 5880Sstevel@tonic-gate p->pcf_count += (uint_t)npages; 5890Sstevel@tonic-gate npages = 0; 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate ASSERT(!p->pcf_wait); 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate if (++p > &pcf[PCF_FANOUT - 1]) 5950Sstevel@tonic-gate p = pcf; 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate ASSERT(npages == 0); 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * Add a physical chunk of memory to the system freee lists during startup. 6030Sstevel@tonic-gate * Platform specific startup() allocates the memory for the page structs. 6040Sstevel@tonic-gate * 6050Sstevel@tonic-gate * num - number of page structures 6060Sstevel@tonic-gate * base - page number (pfn) to be associated with the first page. 6070Sstevel@tonic-gate * 6080Sstevel@tonic-gate * Since we are doing this during startup (ie. single threaded), we will 6090Sstevel@tonic-gate * use shortcut routines to avoid any locking overhead while putting all 6100Sstevel@tonic-gate * these pages on the freelists. 6110Sstevel@tonic-gate * 6120Sstevel@tonic-gate * NOTE: Any changes performed to page_free(), must also be performed to 6130Sstevel@tonic-gate * add_physmem() since this is how we initialize all page_t's at 6140Sstevel@tonic-gate * boot time. 6150Sstevel@tonic-gate */ 6160Sstevel@tonic-gate void 6170Sstevel@tonic-gate add_physmem( 6180Sstevel@tonic-gate page_t *pp, 6190Sstevel@tonic-gate pgcnt_t num, 6200Sstevel@tonic-gate pfn_t pnum) 6210Sstevel@tonic-gate { 6220Sstevel@tonic-gate page_t *root = NULL; 6230Sstevel@tonic-gate uint_t szc = page_num_pagesizes() - 1; 6240Sstevel@tonic-gate pgcnt_t large = page_get_pagecnt(szc); 6250Sstevel@tonic-gate pgcnt_t cnt = 0; 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_INIT, 6280Sstevel@tonic-gate "add_physmem:pp %p num %lu", pp, num); 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * Arbitrarily limit the max page_get request 6320Sstevel@tonic-gate * to 1/2 of the page structs we have. 6330Sstevel@tonic-gate */ 6340Sstevel@tonic-gate total_pages += num; 6350Sstevel@tonic-gate set_max_page_get(total_pages); 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate /* 6380Sstevel@tonic-gate * The physical space for the pages array 6390Sstevel@tonic-gate * representing ram pages has already been 6400Sstevel@tonic-gate * allocated. Here we initialize each lock 6410Sstevel@tonic-gate * in the page structure, and put each on 6420Sstevel@tonic-gate * the free list 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate for (; num; pp = page_next_raw(pp), pnum++, num--) { 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate /* 6470Sstevel@tonic-gate * this needs to fill in the page number 6480Sstevel@tonic-gate * and do any other arch specific initialization 6490Sstevel@tonic-gate */ 6500Sstevel@tonic-gate add_physmem_cb(pp, pnum); 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate /* 6530Sstevel@tonic-gate * Initialize the page lock as unlocked, since nobody 6540Sstevel@tonic-gate * can see or access this page yet. 6550Sstevel@tonic-gate */ 6560Sstevel@tonic-gate pp->p_selock = 0; 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /* 6590Sstevel@tonic-gate * Initialize IO lock 6600Sstevel@tonic-gate */ 6610Sstevel@tonic-gate page_iolock_init(pp); 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /* 6640Sstevel@tonic-gate * initialize other fields in the page_t 6650Sstevel@tonic-gate */ 6660Sstevel@tonic-gate PP_SETFREE(pp); 6670Sstevel@tonic-gate page_clr_all_props(pp); 6680Sstevel@tonic-gate PP_SETAGED(pp); 6690Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1; 6700Sstevel@tonic-gate pp->p_next = pp; 6710Sstevel@tonic-gate pp->p_prev = pp; 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * Simple case: System doesn't support large pages. 6750Sstevel@tonic-gate */ 6760Sstevel@tonic-gate if (szc == 0) { 6770Sstevel@tonic-gate pp->p_szc = 0; 6780Sstevel@tonic-gate page_free_at_startup(pp); 6790Sstevel@tonic-gate continue; 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate /* 6830Sstevel@tonic-gate * Handle unaligned pages, we collect them up onto 6840Sstevel@tonic-gate * the root page until we have a full large page. 6850Sstevel@tonic-gate */ 6860Sstevel@tonic-gate if (!IS_P2ALIGNED(pnum, large)) { 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate /* 6890Sstevel@tonic-gate * If not in a large page, 6900Sstevel@tonic-gate * just free as small page. 6910Sstevel@tonic-gate */ 6920Sstevel@tonic-gate if (root == NULL) { 6930Sstevel@tonic-gate pp->p_szc = 0; 6940Sstevel@tonic-gate page_free_at_startup(pp); 6950Sstevel@tonic-gate continue; 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate /* 6990Sstevel@tonic-gate * Link a constituent page into the large page. 7000Sstevel@tonic-gate */ 7010Sstevel@tonic-gate pp->p_szc = szc; 7020Sstevel@tonic-gate page_list_concat(&root, &pp); 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * When large page is fully formed, free it. 7060Sstevel@tonic-gate */ 7070Sstevel@tonic-gate if (++cnt == large) { 7080Sstevel@tonic-gate page_free_large_ctr(cnt); 7090Sstevel@tonic-gate page_list_add_pages(root, PG_LIST_ISINIT); 7100Sstevel@tonic-gate root = NULL; 7110Sstevel@tonic-gate cnt = 0; 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate continue; 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate /* 7170Sstevel@tonic-gate * At this point we have a page number which 7180Sstevel@tonic-gate * is aligned. We assert that we aren't already 7190Sstevel@tonic-gate * in a different large page. 7200Sstevel@tonic-gate */ 7210Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(pnum, large)); 7220Sstevel@tonic-gate ASSERT(root == NULL && cnt == 0); 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate /* 7250Sstevel@tonic-gate * If insufficient number of pages left to form 7260Sstevel@tonic-gate * a large page, just free the small page. 7270Sstevel@tonic-gate */ 7280Sstevel@tonic-gate if (num < large) { 7290Sstevel@tonic-gate pp->p_szc = 0; 7300Sstevel@tonic-gate page_free_at_startup(pp); 7310Sstevel@tonic-gate continue; 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate /* 7350Sstevel@tonic-gate * Otherwise start a new large page. 7360Sstevel@tonic-gate */ 7370Sstevel@tonic-gate pp->p_szc = szc; 7380Sstevel@tonic-gate cnt++; 7390Sstevel@tonic-gate root = pp; 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate ASSERT(root == NULL && cnt == 0); 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * Find a page representing the specified [vp, offset]. 7460Sstevel@tonic-gate * If we find the page but it is intransit coming in, 7470Sstevel@tonic-gate * it will have an "exclusive" lock and we wait for 7480Sstevel@tonic-gate * the i/o to complete. A page found on the free list 7490Sstevel@tonic-gate * is always reclaimed and then locked. On success, the page 7500Sstevel@tonic-gate * is locked, its data is valid and it isn't on the free 7510Sstevel@tonic-gate * list, while a NULL is returned if the page doesn't exist. 7520Sstevel@tonic-gate */ 7530Sstevel@tonic-gate page_t * 7540Sstevel@tonic-gate page_lookup(vnode_t *vp, u_offset_t off, se_t se) 7550Sstevel@tonic-gate { 7560Sstevel@tonic-gate return (page_lookup_create(vp, off, se, NULL, NULL, 0)); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate /* 7600Sstevel@tonic-gate * Find a page representing the specified [vp, offset]. 7610Sstevel@tonic-gate * We either return the one we found or, if passed in, 7620Sstevel@tonic-gate * create one with identity of [vp, offset] of the 7630Sstevel@tonic-gate * pre-allocated page. If we find exsisting page but it is 7640Sstevel@tonic-gate * intransit coming in, it will have an "exclusive" lock 7650Sstevel@tonic-gate * and we wait for the i/o to complete. A page found on 7660Sstevel@tonic-gate * the free list is always reclaimed and then locked. 7670Sstevel@tonic-gate * On success, the page is locked, its data is valid and 7680Sstevel@tonic-gate * it isn't on the free list, while a NULL is returned 7690Sstevel@tonic-gate * if the page doesn't exist and newpp is NULL; 7700Sstevel@tonic-gate */ 7710Sstevel@tonic-gate page_t * 7720Sstevel@tonic-gate page_lookup_create( 7730Sstevel@tonic-gate vnode_t *vp, 7740Sstevel@tonic-gate u_offset_t off, 7750Sstevel@tonic-gate se_t se, 7760Sstevel@tonic-gate page_t *newpp, 7770Sstevel@tonic-gate spgcnt_t *nrelocp, 7780Sstevel@tonic-gate int flags) 7790Sstevel@tonic-gate { 7800Sstevel@tonic-gate page_t *pp; 7810Sstevel@tonic-gate kmutex_t *phm; 7820Sstevel@tonic-gate ulong_t index; 7830Sstevel@tonic-gate uint_t hash_locked; 7840Sstevel@tonic-gate uint_t es; 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 7870Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[0]); 7880Sstevel@tonic-gate ASSERT(newpp ? PAGE_EXCL(newpp) : 1); 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate /* 7910Sstevel@tonic-gate * Acquire the appropriate page hash lock since 7920Sstevel@tonic-gate * we have to search the hash list. Pages that 7930Sstevel@tonic-gate * hash to this list can't change identity while 7940Sstevel@tonic-gate * this lock is held. 7950Sstevel@tonic-gate */ 7960Sstevel@tonic-gate hash_locked = 0; 7970Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 7980Sstevel@tonic-gate phm = NULL; 7990Sstevel@tonic-gate top: 8000Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 8010Sstevel@tonic-gate if (pp != NULL) { 8020Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[1]); 8030Sstevel@tonic-gate es = (newpp != NULL) ? 1 : 0; 8040Sstevel@tonic-gate es |= flags; 8050Sstevel@tonic-gate if (!hash_locked) { 8060Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[2]); 8070Sstevel@tonic-gate if (!page_try_reclaim_lock(pp, se, es)) { 8080Sstevel@tonic-gate /* 8090Sstevel@tonic-gate * On a miss, acquire the phm. Then 8100Sstevel@tonic-gate * next time, page_lock() will be called, 8110Sstevel@tonic-gate * causing a wait if the page is busy. 8120Sstevel@tonic-gate * just looping with page_trylock() would 8130Sstevel@tonic-gate * get pretty boring. 8140Sstevel@tonic-gate */ 8150Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[3]); 8160Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 8170Sstevel@tonic-gate mutex_enter(phm); 8180Sstevel@tonic-gate hash_locked = 1; 8190Sstevel@tonic-gate goto top; 8200Sstevel@tonic-gate } 8210Sstevel@tonic-gate } else { 8220Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[4]); 8230Sstevel@tonic-gate if (!page_lock_es(pp, se, phm, P_RECLAIM, es)) { 8240Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[5]); 8250Sstevel@tonic-gate goto top; 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * Since `pp' is locked it can not change identity now. 8310Sstevel@tonic-gate * Reconfirm we locked the correct page. 8320Sstevel@tonic-gate * 8330Sstevel@tonic-gate * Both the p_vnode and p_offset *must* be cast volatile 8340Sstevel@tonic-gate * to force a reload of their values: The PAGE_HASH_SEARCH 8350Sstevel@tonic-gate * macro will have stuffed p_vnode and p_offset into 8360Sstevel@tonic-gate * registers before calling page_trylock(); another thread, 8370Sstevel@tonic-gate * actually holding the hash lock, could have changed the 8380Sstevel@tonic-gate * page's identity in memory, but our registers would not 8390Sstevel@tonic-gate * be changed, fooling the reconfirmation. If the hash 8400Sstevel@tonic-gate * lock was held during the search, the casting would 8410Sstevel@tonic-gate * not be needed. 8420Sstevel@tonic-gate */ 8430Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[6]); 8440Sstevel@tonic-gate if (((volatile struct vnode *)(pp->p_vnode) != vp) || 8450Sstevel@tonic-gate ((volatile u_offset_t)(pp->p_offset) != off)) { 8460Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[7]); 8470Sstevel@tonic-gate if (hash_locked) { 8480Sstevel@tonic-gate panic("page_lookup_create: lost page %p", 8490Sstevel@tonic-gate (void *)pp); 8500Sstevel@tonic-gate /*NOTREACHED*/ 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate page_unlock(pp); 8530Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 8540Sstevel@tonic-gate mutex_enter(phm); 8550Sstevel@tonic-gate hash_locked = 1; 8560Sstevel@tonic-gate goto top; 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * If page_trylock() was called, then pp may still be on 8610Sstevel@tonic-gate * the cachelist (can't be on the free list, it would not 8620Sstevel@tonic-gate * have been found in the search). If it is on the 8630Sstevel@tonic-gate * cachelist it must be pulled now. To pull the page from 8640Sstevel@tonic-gate * the cachelist, it must be exclusively locked. 8650Sstevel@tonic-gate * 8660Sstevel@tonic-gate * The other big difference between page_trylock() and 8670Sstevel@tonic-gate * page_lock(), is that page_lock() will pull the 8680Sstevel@tonic-gate * page from whatever free list (the cache list in this 8690Sstevel@tonic-gate * case) the page is on. If page_trylock() was used 8700Sstevel@tonic-gate * above, then we have to do the reclaim ourselves. 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate if ((!hash_locked) && (PP_ISFREE(pp))) { 8730Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0); 8740Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[8]); 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate /* 8770Sstevel@tonic-gate * page_relcaim will insure that we 8780Sstevel@tonic-gate * have this page exclusively 8790Sstevel@tonic-gate */ 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate if (!page_reclaim(pp, NULL)) { 8820Sstevel@tonic-gate /* 8830Sstevel@tonic-gate * Page_reclaim dropped whatever lock 8840Sstevel@tonic-gate * we held. 8850Sstevel@tonic-gate */ 8860Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[9]); 8870Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 8880Sstevel@tonic-gate mutex_enter(phm); 8890Sstevel@tonic-gate hash_locked = 1; 8900Sstevel@tonic-gate goto top; 8910Sstevel@tonic-gate } else if (se == SE_SHARED && newpp == NULL) { 8920Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[10]); 8930Sstevel@tonic-gate page_downgrade(pp); 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate } 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate if (hash_locked) { 8980Sstevel@tonic-gate mutex_exit(phm); 8990Sstevel@tonic-gate } 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate if (newpp != NULL && pp->p_szc < newpp->p_szc && 9020Sstevel@tonic-gate PAGE_EXCL(pp) && nrelocp != NULL) { 9030Sstevel@tonic-gate ASSERT(nrelocp != NULL); 9040Sstevel@tonic-gate (void) page_relocate(&pp, &newpp, 1, 1, nrelocp, 9050Sstevel@tonic-gate NULL); 9060Sstevel@tonic-gate if (*nrelocp > 0) { 9070Sstevel@tonic-gate VM_STAT_COND_ADD(*nrelocp == 1, 9080Sstevel@tonic-gate page_lookup_cnt[11]); 9090Sstevel@tonic-gate VM_STAT_COND_ADD(*nrelocp > 1, 9100Sstevel@tonic-gate page_lookup_cnt[12]); 9110Sstevel@tonic-gate pp = newpp; 9120Sstevel@tonic-gate se = SE_EXCL; 9130Sstevel@tonic-gate } else { 9140Sstevel@tonic-gate if (se == SE_SHARED) { 9150Sstevel@tonic-gate page_downgrade(pp); 9160Sstevel@tonic-gate } 9170Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[13]); 9180Sstevel@tonic-gate } 9190Sstevel@tonic-gate } else if (newpp != NULL && nrelocp != NULL) { 9200Sstevel@tonic-gate if (PAGE_EXCL(pp) && se == SE_SHARED) { 9210Sstevel@tonic-gate page_downgrade(pp); 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate VM_STAT_COND_ADD(pp->p_szc < newpp->p_szc, 9240Sstevel@tonic-gate page_lookup_cnt[14]); 9250Sstevel@tonic-gate VM_STAT_COND_ADD(pp->p_szc == newpp->p_szc, 9260Sstevel@tonic-gate page_lookup_cnt[15]); 9270Sstevel@tonic-gate VM_STAT_COND_ADD(pp->p_szc > newpp->p_szc, 9280Sstevel@tonic-gate page_lookup_cnt[16]); 9290Sstevel@tonic-gate } else if (newpp != NULL && PAGE_EXCL(pp)) { 9300Sstevel@tonic-gate se = SE_EXCL; 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate } else if (!hash_locked) { 9330Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[17]); 9340Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 9350Sstevel@tonic-gate mutex_enter(phm); 9360Sstevel@tonic-gate hash_locked = 1; 9370Sstevel@tonic-gate goto top; 9380Sstevel@tonic-gate } else if (newpp != NULL) { 9390Sstevel@tonic-gate /* 9400Sstevel@tonic-gate * If we have a preallocated page then 9410Sstevel@tonic-gate * insert it now and basically behave like 9420Sstevel@tonic-gate * page_create. 9430Sstevel@tonic-gate */ 9440Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[18]); 9450Sstevel@tonic-gate /* 9460Sstevel@tonic-gate * Since we hold the page hash mutex and 9470Sstevel@tonic-gate * just searched for this page, page_hashin 9480Sstevel@tonic-gate * had better not fail. If it does, that 9490Sstevel@tonic-gate * means some thread did not follow the 9500Sstevel@tonic-gate * page hash mutex rules. Panic now and 9510Sstevel@tonic-gate * get it over with. As usual, go down 9520Sstevel@tonic-gate * holding all the locks. 9530Sstevel@tonic-gate */ 9540Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 9550Sstevel@tonic-gate if (!page_hashin(newpp, vp, off, phm)) { 9560Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 9570Sstevel@tonic-gate panic("page_lookup_create: hashin failed %p %p %llx %p", 9580Sstevel@tonic-gate (void *)newpp, (void *)vp, off, (void *)phm); 9590Sstevel@tonic-gate /*NOTREACHED*/ 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 9620Sstevel@tonic-gate mutex_exit(phm); 9630Sstevel@tonic-gate phm = NULL; 9640Sstevel@tonic-gate page_set_props(newpp, P_REF); 9650Sstevel@tonic-gate page_io_lock(newpp); 9660Sstevel@tonic-gate pp = newpp; 9670Sstevel@tonic-gate se = SE_EXCL; 9680Sstevel@tonic-gate } else { 9690Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[19]); 9700Sstevel@tonic-gate mutex_exit(phm); 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate ASSERT(pp ? PAGE_LOCKED_SE(pp, se) : 1); 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate ASSERT(pp ? ((PP_ISFREE(pp) == 0) && (PP_ISAGED(pp) == 0)) : 1); 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate return (pp); 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate /* 9810Sstevel@tonic-gate * Search the hash list for the page representing the 9820Sstevel@tonic-gate * specified [vp, offset] and return it locked. Skip 9830Sstevel@tonic-gate * free pages and pages that cannot be locked as requested. 9840Sstevel@tonic-gate * Used while attempting to kluster pages. 9850Sstevel@tonic-gate */ 9860Sstevel@tonic-gate page_t * 9870Sstevel@tonic-gate page_lookup_nowait(vnode_t *vp, u_offset_t off, se_t se) 9880Sstevel@tonic-gate { 9890Sstevel@tonic-gate page_t *pp; 9900Sstevel@tonic-gate kmutex_t *phm; 9910Sstevel@tonic-gate ulong_t index; 9920Sstevel@tonic-gate uint_t locked; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 9950Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[0]); 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 9980Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 9990Sstevel@tonic-gate locked = 0; 10000Sstevel@tonic-gate if (pp == NULL) { 10010Sstevel@tonic-gate top: 10020Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[1]); 10030Sstevel@tonic-gate locked = 1; 10040Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 10050Sstevel@tonic-gate mutex_enter(phm); 10060Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate if (pp == NULL || PP_ISFREE(pp)) { 10100Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[2]); 10110Sstevel@tonic-gate pp = NULL; 10120Sstevel@tonic-gate } else { 10130Sstevel@tonic-gate if (!page_trylock(pp, se)) { 10140Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[3]); 10150Sstevel@tonic-gate pp = NULL; 10160Sstevel@tonic-gate } else { 10170Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[4]); 10180Sstevel@tonic-gate /* 10190Sstevel@tonic-gate * See the comment in page_lookup() 10200Sstevel@tonic-gate */ 10210Sstevel@tonic-gate if (((volatile struct vnode *)(pp->p_vnode) != vp) || 10220Sstevel@tonic-gate ((u_offset_t)(pp->p_offset) != off)) { 10230Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[5]); 10240Sstevel@tonic-gate if (locked) { 10250Sstevel@tonic-gate panic("page_lookup_nowait %p", 10260Sstevel@tonic-gate (void *)pp); 10270Sstevel@tonic-gate /*NOTREACHED*/ 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate page_unlock(pp); 10300Sstevel@tonic-gate goto top; 10310Sstevel@tonic-gate } 10320Sstevel@tonic-gate if (PP_ISFREE(pp)) { 10330Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[6]); 10340Sstevel@tonic-gate page_unlock(pp); 10350Sstevel@tonic-gate pp = NULL; 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate } 10390Sstevel@tonic-gate if (locked) { 10400Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[7]); 10410Sstevel@tonic-gate mutex_exit(phm); 10420Sstevel@tonic-gate } 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate ASSERT(pp ? PAGE_LOCKED_SE(pp, se) : 1); 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate return (pp); 10470Sstevel@tonic-gate } 10480Sstevel@tonic-gate 10490Sstevel@tonic-gate /* 10500Sstevel@tonic-gate * Search the hash list for a page with the specified [vp, off] 10510Sstevel@tonic-gate * that is known to exist and is already locked. This routine 10520Sstevel@tonic-gate * is typically used by segment SOFTUNLOCK routines. 10530Sstevel@tonic-gate */ 10540Sstevel@tonic-gate page_t * 10550Sstevel@tonic-gate page_find(vnode_t *vp, u_offset_t off) 10560Sstevel@tonic-gate { 10570Sstevel@tonic-gate page_t *pp; 10580Sstevel@tonic-gate kmutex_t *phm; 10590Sstevel@tonic-gate ulong_t index; 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 10620Sstevel@tonic-gate VM_STAT_ADD(page_find_cnt); 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 10650Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate mutex_enter(phm); 10680Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 10690Sstevel@tonic-gate mutex_exit(phm); 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate ASSERT(pp != NULL); 10720Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp) || panicstr); 10730Sstevel@tonic-gate return (pp); 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate /* 10770Sstevel@tonic-gate * Determine whether a page with the specified [vp, off] 10780Sstevel@tonic-gate * currently exists in the system. Obviously this should 10790Sstevel@tonic-gate * only be considered as a hint since nothing prevents the 10800Sstevel@tonic-gate * page from disappearing or appearing immediately after 10810Sstevel@tonic-gate * the return from this routine. Subsequently, we don't 10820Sstevel@tonic-gate * even bother to lock the list. 10830Sstevel@tonic-gate */ 10840Sstevel@tonic-gate page_t * 10850Sstevel@tonic-gate page_exists(vnode_t *vp, u_offset_t off) 10860Sstevel@tonic-gate { 10870Sstevel@tonic-gate page_t *pp; 10880Sstevel@tonic-gate ulong_t index; 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 10910Sstevel@tonic-gate VM_STAT_ADD(page_exists_cnt); 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 10940Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate return (pp); 10970Sstevel@tonic-gate } 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate /* 11000Sstevel@tonic-gate * Determine if physically contiguous pages exist for [vp, off] - [vp, off + 11010Sstevel@tonic-gate * page_size(szc)) range. if they exist and ppa is not NULL fill ppa array 11020Sstevel@tonic-gate * with these pages locked SHARED. If necessary reclaim pages from 11030Sstevel@tonic-gate * freelist. Return 1 if contiguous pages exist and 0 otherwise. 11040Sstevel@tonic-gate * 11050Sstevel@tonic-gate * If we fail to lock pages still return 1 if pages exist and contiguous. 11060Sstevel@tonic-gate * But in this case return value is just a hint. ppa array won't be filled. 11070Sstevel@tonic-gate * Caller should initialize ppa[0] as NULL to distinguish return value. 11080Sstevel@tonic-gate * 11090Sstevel@tonic-gate * Returns 0 if pages don't exist or not physically contiguous. 11100Sstevel@tonic-gate * 11110Sstevel@tonic-gate * This routine doesn't work for anonymous(swapfs) pages. 11120Sstevel@tonic-gate */ 11130Sstevel@tonic-gate int 11140Sstevel@tonic-gate page_exists_physcontig(vnode_t *vp, u_offset_t off, uint_t szc, page_t *ppa[]) 11150Sstevel@tonic-gate { 11160Sstevel@tonic-gate pgcnt_t pages; 11170Sstevel@tonic-gate pfn_t pfn; 11180Sstevel@tonic-gate page_t *rootpp; 11190Sstevel@tonic-gate pgcnt_t i; 11200Sstevel@tonic-gate pgcnt_t j; 11210Sstevel@tonic-gate u_offset_t save_off = off; 11220Sstevel@tonic-gate ulong_t index; 11230Sstevel@tonic-gate kmutex_t *phm; 11240Sstevel@tonic-gate page_t *pp; 11250Sstevel@tonic-gate uint_t pszc; 11260Sstevel@tonic-gate int loopcnt = 0; 11270Sstevel@tonic-gate 11280Sstevel@tonic-gate ASSERT(szc != 0); 11290Sstevel@tonic-gate ASSERT(vp != NULL); 11300Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(vp)); 11310Sstevel@tonic-gate ASSERT(vp != &kvp); 11320Sstevel@tonic-gate 11330Sstevel@tonic-gate again: 11340Sstevel@tonic-gate if (++loopcnt > 3) { 11350Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[0]); 11360Sstevel@tonic-gate return (0); 11370Sstevel@tonic-gate } 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 11400Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate mutex_enter(phm); 11430Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 11440Sstevel@tonic-gate mutex_exit(phm); 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[1]); 11470Sstevel@tonic-gate 11480Sstevel@tonic-gate if (pp == NULL) { 11490Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[2]); 11500Sstevel@tonic-gate return (0); 11510Sstevel@tonic-gate } 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate pages = page_get_pagecnt(szc); 11540Sstevel@tonic-gate rootpp = pp; 11550Sstevel@tonic-gate pfn = rootpp->p_pagenum; 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate if ((pszc = pp->p_szc) >= szc && ppa != NULL) { 11580Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[3]); 11590Sstevel@tonic-gate if (!page_trylock(pp, SE_SHARED)) { 11600Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[4]); 11610Sstevel@tonic-gate return (1); 11620Sstevel@tonic-gate } 11630Sstevel@tonic-gate if (pp->p_szc != pszc || pp->p_vnode != vp || 11640Sstevel@tonic-gate pp->p_offset != off) { 11650Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[5]); 11660Sstevel@tonic-gate page_unlock(pp); 11670Sstevel@tonic-gate off = save_off; 11680Sstevel@tonic-gate goto again; 11690Sstevel@tonic-gate } 11700Sstevel@tonic-gate /* 11710Sstevel@tonic-gate * szc was non zero and vnode and offset matched after we 11720Sstevel@tonic-gate * locked the page it means it can't become free on us. 11730Sstevel@tonic-gate */ 11740Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 11750Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, pages)) { 11760Sstevel@tonic-gate page_unlock(pp); 11770Sstevel@tonic-gate return (0); 11780Sstevel@tonic-gate } 11790Sstevel@tonic-gate ppa[0] = pp; 11800Sstevel@tonic-gate pp++; 11810Sstevel@tonic-gate off += PAGESIZE; 11820Sstevel@tonic-gate pfn++; 11830Sstevel@tonic-gate for (i = 1; i < pages; i++, pp++, off += PAGESIZE, pfn++) { 11840Sstevel@tonic-gate if (!page_trylock(pp, SE_SHARED)) { 11850Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[6]); 11860Sstevel@tonic-gate pp--; 11870Sstevel@tonic-gate while (i-- > 0) { 11880Sstevel@tonic-gate page_unlock(pp); 11890Sstevel@tonic-gate pp--; 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate ppa[0] = NULL; 11920Sstevel@tonic-gate return (1); 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate if (pp->p_szc != pszc) { 11950Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[7]); 11960Sstevel@tonic-gate page_unlock(pp); 11970Sstevel@tonic-gate pp--; 11980Sstevel@tonic-gate while (i-- > 0) { 11990Sstevel@tonic-gate page_unlock(pp); 12000Sstevel@tonic-gate pp--; 12010Sstevel@tonic-gate } 12020Sstevel@tonic-gate ppa[0] = NULL; 12030Sstevel@tonic-gate off = save_off; 12040Sstevel@tonic-gate goto again; 12050Sstevel@tonic-gate } 12060Sstevel@tonic-gate /* 12070Sstevel@tonic-gate * szc the same as for previous already locked pages 12080Sstevel@tonic-gate * with right identity. Since this page had correct 12090Sstevel@tonic-gate * szc after we locked it can't get freed or destroyed 12100Sstevel@tonic-gate * and therefore must have the expected identity. 12110Sstevel@tonic-gate */ 12120Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 12130Sstevel@tonic-gate if (pp->p_vnode != vp || 12140Sstevel@tonic-gate pp->p_offset != off) { 12150Sstevel@tonic-gate panic("page_exists_physcontig: " 12160Sstevel@tonic-gate "large page identity doesn't match"); 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate ppa[i] = pp; 12190Sstevel@tonic-gate ASSERT(pp->p_pagenum == pfn); 12200Sstevel@tonic-gate } 12210Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[8]); 12220Sstevel@tonic-gate ppa[pages] = NULL; 12230Sstevel@tonic-gate return (1); 12240Sstevel@tonic-gate } else if (pszc >= szc) { 12250Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[9]); 12260Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, pages)) { 12270Sstevel@tonic-gate return (0); 12280Sstevel@tonic-gate } 12290Sstevel@tonic-gate return (1); 12300Sstevel@tonic-gate } 12310Sstevel@tonic-gate 12320Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, pages)) { 12330Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[10]); 12340Sstevel@tonic-gate return (0); 12350Sstevel@tonic-gate } 12360Sstevel@tonic-gate 12370Sstevel@tonic-gate if (page_numtomemseg_nolock(pfn) != 12380Sstevel@tonic-gate page_numtomemseg_nolock(pfn + pages - 1)) { 12390Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[11]); 12400Sstevel@tonic-gate return (0); 12410Sstevel@tonic-gate } 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate /* 12440Sstevel@tonic-gate * We loop up 4 times across pages to promote page size. 12450Sstevel@tonic-gate * We're extra cautious to promote page size atomically with respect 12460Sstevel@tonic-gate * to everybody else. But we can probably optimize into 1 loop if 12470Sstevel@tonic-gate * this becomes an issue. 12480Sstevel@tonic-gate */ 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++, off += PAGESIZE, pfn++) { 12510Sstevel@tonic-gate ASSERT(pp->p_pagenum == pfn); 12520Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) { 12530Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[12]); 12540Sstevel@tonic-gate break; 12550Sstevel@tonic-gate } 12560Sstevel@tonic-gate if (pp->p_vnode != vp || 12570Sstevel@tonic-gate pp->p_offset != off) { 12580Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[13]); 12590Sstevel@tonic-gate page_unlock(pp); 12600Sstevel@tonic-gate break; 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate if (pp->p_szc >= szc) { 12630Sstevel@tonic-gate ASSERT(i == 0); 12640Sstevel@tonic-gate page_unlock(pp); 12650Sstevel@tonic-gate off = save_off; 12660Sstevel@tonic-gate goto again; 12670Sstevel@tonic-gate } 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate if (i != pages) { 12710Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[14]); 12720Sstevel@tonic-gate --pp; 12730Sstevel@tonic-gate while (i-- > 0) { 12740Sstevel@tonic-gate page_unlock(pp); 12750Sstevel@tonic-gate --pp; 12760Sstevel@tonic-gate } 12770Sstevel@tonic-gate return (0); 12780Sstevel@tonic-gate } 12790Sstevel@tonic-gate 12800Sstevel@tonic-gate pp = rootpp; 12810Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++) { 12820Sstevel@tonic-gate if (PP_ISFREE(pp)) { 12830Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[15]); 12840Sstevel@tonic-gate ASSERT(!PP_ISAGED(pp)); 12850Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 12860Sstevel@tonic-gate if (!page_reclaim(pp, NULL)) { 12870Sstevel@tonic-gate break; 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate } else { 12900Sstevel@tonic-gate ASSERT(pp->p_szc < szc); 12910Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[16]); 12920Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 12930Sstevel@tonic-gate } 12940Sstevel@tonic-gate } 12950Sstevel@tonic-gate if (i < pages) { 12960Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[17]); 12970Sstevel@tonic-gate /* 12980Sstevel@tonic-gate * page_reclaim failed because we were out of memory. 12990Sstevel@tonic-gate * drop the rest of the locks and return because this page 13000Sstevel@tonic-gate * must be already reallocated anyway. 13010Sstevel@tonic-gate */ 13020Sstevel@tonic-gate pp = rootpp; 13030Sstevel@tonic-gate for (j = 0; j < pages; j++, pp++) { 13040Sstevel@tonic-gate if (j != i) { 13050Sstevel@tonic-gate page_unlock(pp); 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate } 13080Sstevel@tonic-gate return (0); 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate off = save_off; 13120Sstevel@tonic-gate pp = rootpp; 13130Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++, off += PAGESIZE) { 13140Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 13150Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 13160Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 13170Sstevel@tonic-gate ASSERT(pp->p_vnode == vp); 13180Sstevel@tonic-gate ASSERT(pp->p_offset == off); 13190Sstevel@tonic-gate pp->p_szc = szc; 13200Sstevel@tonic-gate } 13210Sstevel@tonic-gate pp = rootpp; 13220Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++) { 13230Sstevel@tonic-gate if (ppa == NULL) { 13240Sstevel@tonic-gate page_unlock(pp); 13250Sstevel@tonic-gate } else { 13260Sstevel@tonic-gate ppa[i] = pp; 13270Sstevel@tonic-gate page_downgrade(ppa[i]); 13280Sstevel@tonic-gate } 13290Sstevel@tonic-gate } 13300Sstevel@tonic-gate if (ppa != NULL) { 13310Sstevel@tonic-gate ppa[pages] = NULL; 13320Sstevel@tonic-gate } 13330Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[18]); 13340Sstevel@tonic-gate ASSERT(vp->v_pages != NULL); 13350Sstevel@tonic-gate return (1); 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate 13380Sstevel@tonic-gate /* 13390Sstevel@tonic-gate * Determine whether a page with the specified [vp, off] 13400Sstevel@tonic-gate * currently exists in the system and if so return its 13410Sstevel@tonic-gate * size code. Obviously this should only be considered as 13420Sstevel@tonic-gate * a hint since nothing prevents the page from disappearing 13430Sstevel@tonic-gate * or appearing immediately after the return from this routine. 13440Sstevel@tonic-gate */ 13450Sstevel@tonic-gate int 13460Sstevel@tonic-gate page_exists_forreal(vnode_t *vp, u_offset_t off, uint_t *szc) 13470Sstevel@tonic-gate { 13480Sstevel@tonic-gate page_t *pp; 13490Sstevel@tonic-gate kmutex_t *phm; 13500Sstevel@tonic-gate ulong_t index; 13510Sstevel@tonic-gate int rc = 0; 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 13540Sstevel@tonic-gate ASSERT(szc != NULL); 13550Sstevel@tonic-gate VM_STAT_ADD(page_exists_forreal_cnt); 13560Sstevel@tonic-gate 13570Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 13580Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 13590Sstevel@tonic-gate 13600Sstevel@tonic-gate mutex_enter(phm); 13610Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 13620Sstevel@tonic-gate if (pp != NULL) { 13630Sstevel@tonic-gate *szc = pp->p_szc; 13640Sstevel@tonic-gate rc = 1; 13650Sstevel@tonic-gate } 13660Sstevel@tonic-gate mutex_exit(phm); 13670Sstevel@tonic-gate return (rc); 13680Sstevel@tonic-gate } 13690Sstevel@tonic-gate 13700Sstevel@tonic-gate /* wakeup threads waiting for pages in page_create_get_something() */ 13710Sstevel@tonic-gate void 13720Sstevel@tonic-gate wakeup_pcgs(void) 13730Sstevel@tonic-gate { 13740Sstevel@tonic-gate if (!CV_HAS_WAITERS(&pcgs_cv)) 13750Sstevel@tonic-gate return; 13760Sstevel@tonic-gate cv_broadcast(&pcgs_cv); 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate /* 13800Sstevel@tonic-gate * 'freemem' is used all over the kernel as an indication of how many 13810Sstevel@tonic-gate * pages are free (either on the cache list or on the free page list) 13820Sstevel@tonic-gate * in the system. In very few places is a really accurate 'freemem' 13830Sstevel@tonic-gate * needed. To avoid contention of the lock protecting a the 13840Sstevel@tonic-gate * single freemem, it was spread out into NCPU buckets. Set_freemem 13850Sstevel@tonic-gate * sets freemem to the total of all NCPU buckets. It is called from 13860Sstevel@tonic-gate * clock() on each TICK. 13870Sstevel@tonic-gate */ 13880Sstevel@tonic-gate void 13890Sstevel@tonic-gate set_freemem() 13900Sstevel@tonic-gate { 13910Sstevel@tonic-gate struct pcf *p; 13920Sstevel@tonic-gate ulong_t t; 13930Sstevel@tonic-gate uint_t i; 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate t = 0; 13960Sstevel@tonic-gate p = pcf; 13970Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 13980Sstevel@tonic-gate t += p->pcf_count; 13990Sstevel@tonic-gate p++; 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate freemem = t; 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate /* 14040Sstevel@tonic-gate * Don't worry about grabbing mutex. It's not that 14050Sstevel@tonic-gate * critical if we miss a tick or two. This is 14060Sstevel@tonic-gate * where we wakeup possible delayers in 14070Sstevel@tonic-gate * page_create_get_something(). 14080Sstevel@tonic-gate */ 14090Sstevel@tonic-gate wakeup_pcgs(); 14100Sstevel@tonic-gate } 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate ulong_t 14130Sstevel@tonic-gate get_freemem() 14140Sstevel@tonic-gate { 14150Sstevel@tonic-gate struct pcf *p; 14160Sstevel@tonic-gate ulong_t t; 14170Sstevel@tonic-gate uint_t i; 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate t = 0; 14200Sstevel@tonic-gate p = pcf; 14210Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 14220Sstevel@tonic-gate t += p->pcf_count; 14230Sstevel@tonic-gate p++; 14240Sstevel@tonic-gate } 14250Sstevel@tonic-gate /* 14260Sstevel@tonic-gate * We just calculated it, might as well set it. 14270Sstevel@tonic-gate */ 14280Sstevel@tonic-gate freemem = t; 14290Sstevel@tonic-gate return (t); 14300Sstevel@tonic-gate } 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate /* 14330Sstevel@tonic-gate * Acquire all of the page cache & free (pcf) locks. 14340Sstevel@tonic-gate */ 14350Sstevel@tonic-gate void 14360Sstevel@tonic-gate pcf_acquire_all() 14370Sstevel@tonic-gate { 14380Sstevel@tonic-gate struct pcf *p; 14390Sstevel@tonic-gate uint_t i; 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate p = pcf; 14420Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 14430Sstevel@tonic-gate p->pcf_touch = 1; 14440Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 14450Sstevel@tonic-gate p++; 14460Sstevel@tonic-gate } 14470Sstevel@tonic-gate } 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate /* 14500Sstevel@tonic-gate * Release all the pcf_locks. 14510Sstevel@tonic-gate */ 14520Sstevel@tonic-gate void 14530Sstevel@tonic-gate pcf_release_all() 14540Sstevel@tonic-gate { 14550Sstevel@tonic-gate struct pcf *p; 14560Sstevel@tonic-gate uint_t i; 14570Sstevel@tonic-gate 14580Sstevel@tonic-gate p = pcf; 14590Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 14600Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 14610Sstevel@tonic-gate p++; 14620Sstevel@tonic-gate } 14630Sstevel@tonic-gate } 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate /* 14660Sstevel@tonic-gate * Inform the VM system that we need some pages freed up. 14670Sstevel@tonic-gate * Calls must be symmetric, e.g.: 14680Sstevel@tonic-gate * 14690Sstevel@tonic-gate * page_needfree(100); 14700Sstevel@tonic-gate * wait a bit; 14710Sstevel@tonic-gate * page_needfree(-100); 14720Sstevel@tonic-gate */ 14730Sstevel@tonic-gate void 14740Sstevel@tonic-gate page_needfree(spgcnt_t npages) 14750Sstevel@tonic-gate { 14760Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 14770Sstevel@tonic-gate needfree += npages; 14780Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 14790Sstevel@tonic-gate } 14800Sstevel@tonic-gate 14810Sstevel@tonic-gate /* 14820Sstevel@tonic-gate * Throttle for page_create(): try to prevent freemem from dropping 14830Sstevel@tonic-gate * below throttlefree. We can't provide a 100% guarantee because 14840Sstevel@tonic-gate * KM_NOSLEEP allocations, page_reclaim(), and various other things 14850Sstevel@tonic-gate * nibble away at the freelist. However, we can block all PG_WAIT 14860Sstevel@tonic-gate * allocations until memory becomes available. The motivation is 14870Sstevel@tonic-gate * that several things can fall apart when there's no free memory: 14880Sstevel@tonic-gate * 14890Sstevel@tonic-gate * (1) If pageout() needs memory to push a page, the system deadlocks. 14900Sstevel@tonic-gate * 14910Sstevel@tonic-gate * (2) By (broken) specification, timeout(9F) can neither fail nor 14920Sstevel@tonic-gate * block, so it has no choice but to panic the system if it 14930Sstevel@tonic-gate * cannot allocate a callout structure. 14940Sstevel@tonic-gate * 14950Sstevel@tonic-gate * (3) Like timeout(), ddi_set_callback() cannot fail and cannot block; 14960Sstevel@tonic-gate * it panics if it cannot allocate a callback structure. 14970Sstevel@tonic-gate * 14980Sstevel@tonic-gate * (4) Untold numbers of third-party drivers have not yet been hardened 14990Sstevel@tonic-gate * against KM_NOSLEEP and/or allocb() failures; they simply assume 15000Sstevel@tonic-gate * success and panic the system with a data fault on failure. 15010Sstevel@tonic-gate * (The long-term solution to this particular problem is to ship 15020Sstevel@tonic-gate * hostile fault-injecting DEBUG kernels with the DDK.) 15030Sstevel@tonic-gate * 15040Sstevel@tonic-gate * It is theoretically impossible to guarantee success of non-blocking 15050Sstevel@tonic-gate * allocations, but in practice, this throttle is very hard to break. 15060Sstevel@tonic-gate */ 15070Sstevel@tonic-gate static int 15080Sstevel@tonic-gate page_create_throttle(pgcnt_t npages, int flags) 15090Sstevel@tonic-gate { 15100Sstevel@tonic-gate ulong_t fm; 15110Sstevel@tonic-gate uint_t i; 15120Sstevel@tonic-gate pgcnt_t tf; /* effective value of throttlefree */ 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate /* 15150Sstevel@tonic-gate * Never deny pages when: 15160Sstevel@tonic-gate * - it's a thread that cannot block [NOMEMWAIT()] 15170Sstevel@tonic-gate * - the allocation cannot block and must not fail 15180Sstevel@tonic-gate * - the allocation cannot block and is pageout dispensated 15190Sstevel@tonic-gate */ 15200Sstevel@tonic-gate if (NOMEMWAIT() || 15210Sstevel@tonic-gate ((flags & (PG_WAIT | PG_PANIC)) == PG_PANIC) || 15220Sstevel@tonic-gate ((flags & (PG_WAIT | PG_PUSHPAGE)) == PG_PUSHPAGE)) 15230Sstevel@tonic-gate return (1); 15240Sstevel@tonic-gate 15250Sstevel@tonic-gate /* 15260Sstevel@tonic-gate * If the allocation can't block, we look favorably upon it 15270Sstevel@tonic-gate * unless we're below pageout_reserve. In that case we fail 15280Sstevel@tonic-gate * the allocation because we want to make sure there are a few 15290Sstevel@tonic-gate * pages available for pageout. 15300Sstevel@tonic-gate */ 15310Sstevel@tonic-gate if ((flags & PG_WAIT) == 0) 15320Sstevel@tonic-gate return (freemem >= npages + pageout_reserve); 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate /* Calculate the effective throttlefree value */ 15350Sstevel@tonic-gate tf = throttlefree - 15360Sstevel@tonic-gate ((flags & PG_PUSHPAGE) ? pageout_reserve : 0); 15370Sstevel@tonic-gate 15380Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 15390Sstevel@tonic-gate 15400Sstevel@tonic-gate while (freemem < npages + tf) { 15410Sstevel@tonic-gate pcf_acquire_all(); 15420Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 15430Sstevel@tonic-gate fm = 0; 15440Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 15450Sstevel@tonic-gate fm += pcf[i].pcf_count; 15460Sstevel@tonic-gate pcf[i].pcf_wait++; 15470Sstevel@tonic-gate mutex_exit(&pcf[i].pcf_lock); 15480Sstevel@tonic-gate } 15490Sstevel@tonic-gate freemem = fm; 15500Sstevel@tonic-gate needfree += npages; 15510Sstevel@tonic-gate freemem_wait++; 15520Sstevel@tonic-gate cv_wait(&freemem_cv, &new_freemem_lock); 15530Sstevel@tonic-gate freemem_wait--; 15540Sstevel@tonic-gate needfree -= npages; 15550Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 15560Sstevel@tonic-gate } 15570Sstevel@tonic-gate return (1); 15580Sstevel@tonic-gate } 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate /* 15610Sstevel@tonic-gate * page_create_wait() is called to either coalecse pages from the 15620Sstevel@tonic-gate * different pcf buckets or to wait because there simply are not 15630Sstevel@tonic-gate * enough pages to satisfy the caller's request. 15640Sstevel@tonic-gate * 15650Sstevel@tonic-gate * Sadly, this is called from platform/vm/vm_machdep.c 15660Sstevel@tonic-gate */ 15670Sstevel@tonic-gate int 15680Sstevel@tonic-gate page_create_wait(size_t npages, uint_t flags) 15690Sstevel@tonic-gate { 15700Sstevel@tonic-gate pgcnt_t total; 15710Sstevel@tonic-gate uint_t i; 15720Sstevel@tonic-gate struct pcf *p; 15730Sstevel@tonic-gate 15740Sstevel@tonic-gate /* 15750Sstevel@tonic-gate * Wait until there are enough free pages to satisfy our 15760Sstevel@tonic-gate * entire request. 15770Sstevel@tonic-gate * We set needfree += npages before prodding pageout, to make sure 15780Sstevel@tonic-gate * it does real work when npages > lotsfree > freemem. 15790Sstevel@tonic-gate */ 15800Sstevel@tonic-gate VM_STAT_ADD(page_create_not_enough); 15810Sstevel@tonic-gate 15820Sstevel@tonic-gate ASSERT(!kcage_on ? !(flags & PG_NORELOC) : 1); 15830Sstevel@tonic-gate checkagain: 15840Sstevel@tonic-gate if ((flags & PG_NORELOC) && 15850Sstevel@tonic-gate kcage_freemem < kcage_throttlefree + npages) 15860Sstevel@tonic-gate (void) kcage_create_throttle(npages, flags); 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate if (freemem < npages + throttlefree) 15890Sstevel@tonic-gate if (!page_create_throttle(npages, flags)) 15900Sstevel@tonic-gate return (0); 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate /* 15930Sstevel@tonic-gate * Since page_create_va() looked at every 15940Sstevel@tonic-gate * bucket, assume we are going to have to wait. 15950Sstevel@tonic-gate * Get all of the pcf locks. 15960Sstevel@tonic-gate */ 15970Sstevel@tonic-gate total = 0; 15980Sstevel@tonic-gate p = pcf; 15990Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 16000Sstevel@tonic-gate p->pcf_touch = 1; 16010Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 16020Sstevel@tonic-gate total += p->pcf_count; 16030Sstevel@tonic-gate if (total >= npages) { 16040Sstevel@tonic-gate /* 16050Sstevel@tonic-gate * Wow! There are enough pages laying around 16060Sstevel@tonic-gate * to satisfy the request. Do the accounting, 16070Sstevel@tonic-gate * drop the locks we acquired, and go back. 16080Sstevel@tonic-gate * 16090Sstevel@tonic-gate * freemem is not protected by any lock. So, 16100Sstevel@tonic-gate * we cannot have any assertion containing 16110Sstevel@tonic-gate * freemem. 16120Sstevel@tonic-gate */ 16130Sstevel@tonic-gate freemem -= npages; 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate while (p >= pcf) { 16160Sstevel@tonic-gate if (p->pcf_count <= npages) { 16170Sstevel@tonic-gate npages -= p->pcf_count; 16180Sstevel@tonic-gate p->pcf_count = 0; 16190Sstevel@tonic-gate } else { 16200Sstevel@tonic-gate p->pcf_count -= (uint_t)npages; 16210Sstevel@tonic-gate npages = 0; 16220Sstevel@tonic-gate } 16230Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 16240Sstevel@tonic-gate p--; 16250Sstevel@tonic-gate } 16260Sstevel@tonic-gate ASSERT(npages == 0); 16270Sstevel@tonic-gate return (1); 16280Sstevel@tonic-gate } 16290Sstevel@tonic-gate p++; 16300Sstevel@tonic-gate } 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate /* 16330Sstevel@tonic-gate * All of the pcf locks are held, there are not enough pages 16340Sstevel@tonic-gate * to satisfy the request (npages < total). 16350Sstevel@tonic-gate * Be sure to acquire the new_freemem_lock before dropping 16360Sstevel@tonic-gate * the pcf locks. This prevents dropping wakeups in page_free(). 16370Sstevel@tonic-gate * The order is always pcf_lock then new_freemem_lock. 16380Sstevel@tonic-gate * 16390Sstevel@tonic-gate * Since we hold all the pcf locks, it is a good time to set freemem. 16400Sstevel@tonic-gate * 16410Sstevel@tonic-gate * If the caller does not want to wait, return now. 16420Sstevel@tonic-gate * Else turn the pageout daemon loose to find something 16430Sstevel@tonic-gate * and wait till it does. 16440Sstevel@tonic-gate * 16450Sstevel@tonic-gate */ 16460Sstevel@tonic-gate freemem = total; 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate if ((flags & PG_WAIT) == 0) { 16490Sstevel@tonic-gate pcf_release_all(); 16500Sstevel@tonic-gate 16510Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_NOMEM, 16520Sstevel@tonic-gate "page_create_nomem:npages %ld freemem %ld", npages, freemem); 16530Sstevel@tonic-gate return (0); 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate 16560Sstevel@tonic-gate ASSERT(proc_pageout != NULL); 16570Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SLEEP_START, 16600Sstevel@tonic-gate "page_create_sleep_start: freemem %ld needfree %ld", 16610Sstevel@tonic-gate freemem, needfree); 16620Sstevel@tonic-gate 16630Sstevel@tonic-gate /* 16640Sstevel@tonic-gate * We are going to wait. 16650Sstevel@tonic-gate * We currently hold all of the pcf_locks, 16660Sstevel@tonic-gate * get the new_freemem_lock (it protects freemem_wait), 16670Sstevel@tonic-gate * before dropping the pcf_locks. 16680Sstevel@tonic-gate */ 16690Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate p = pcf; 16720Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 16730Sstevel@tonic-gate p->pcf_wait++; 16740Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 16750Sstevel@tonic-gate p++; 16760Sstevel@tonic-gate } 16770Sstevel@tonic-gate 16780Sstevel@tonic-gate needfree += npages; 16790Sstevel@tonic-gate freemem_wait++; 16800Sstevel@tonic-gate 16810Sstevel@tonic-gate cv_wait(&freemem_cv, &new_freemem_lock); 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate freemem_wait--; 16840Sstevel@tonic-gate needfree -= npages; 16850Sstevel@tonic-gate 16860Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 16870Sstevel@tonic-gate 16880Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SLEEP_END, 16890Sstevel@tonic-gate "page_create_sleep_end: freemem %ld needfree %ld", 16900Sstevel@tonic-gate freemem, needfree); 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate VM_STAT_ADD(page_create_not_enough_again); 16930Sstevel@tonic-gate goto checkagain; 16940Sstevel@tonic-gate } 16950Sstevel@tonic-gate 16960Sstevel@tonic-gate /* 16970Sstevel@tonic-gate * A routine to do the opposite of page_create_wait(). 16980Sstevel@tonic-gate */ 16990Sstevel@tonic-gate void 17000Sstevel@tonic-gate page_create_putback(spgcnt_t npages) 17010Sstevel@tonic-gate { 17020Sstevel@tonic-gate struct pcf *p; 17030Sstevel@tonic-gate pgcnt_t lump; 17040Sstevel@tonic-gate uint_t *which; 17050Sstevel@tonic-gate 17060Sstevel@tonic-gate /* 17070Sstevel@tonic-gate * When a contiguous lump is broken up, we have to 17080Sstevel@tonic-gate * deal with lots of pages (min 64) so lets spread 17090Sstevel@tonic-gate * the wealth around. 17100Sstevel@tonic-gate */ 17110Sstevel@tonic-gate lump = roundup(npages, PCF_FANOUT) / PCF_FANOUT; 17120Sstevel@tonic-gate freemem += npages; 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate for (p = pcf; (npages > 0) && (p < &pcf[PCF_FANOUT]); p++) { 17150Sstevel@tonic-gate which = &p->pcf_count; 17160Sstevel@tonic-gate 17170Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 17180Sstevel@tonic-gate 17190Sstevel@tonic-gate if (p->pcf_block) { 17200Sstevel@tonic-gate which = &p->pcf_reserve; 17210Sstevel@tonic-gate } 17220Sstevel@tonic-gate 17230Sstevel@tonic-gate if (lump < npages) { 17240Sstevel@tonic-gate *which += (uint_t)lump; 17250Sstevel@tonic-gate npages -= lump; 17260Sstevel@tonic-gate } else { 17270Sstevel@tonic-gate *which += (uint_t)npages; 17280Sstevel@tonic-gate npages = 0; 17290Sstevel@tonic-gate } 17300Sstevel@tonic-gate 17310Sstevel@tonic-gate if (p->pcf_wait) { 17320Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 17330Sstevel@tonic-gate /* 17340Sstevel@tonic-gate * Check to see if some other thread 17350Sstevel@tonic-gate * is actually waiting. Another bucket 17360Sstevel@tonic-gate * may have woken it up by now. If there 17370Sstevel@tonic-gate * are no waiters, then set our pcf_wait 17380Sstevel@tonic-gate * count to zero to avoid coming in here 17390Sstevel@tonic-gate * next time. 17400Sstevel@tonic-gate */ 17410Sstevel@tonic-gate if (freemem_wait) { 17420Sstevel@tonic-gate if (npages > 1) { 17430Sstevel@tonic-gate cv_broadcast(&freemem_cv); 17440Sstevel@tonic-gate } else { 17450Sstevel@tonic-gate cv_signal(&freemem_cv); 17460Sstevel@tonic-gate } 17470Sstevel@tonic-gate p->pcf_wait--; 17480Sstevel@tonic-gate } else { 17490Sstevel@tonic-gate p->pcf_wait = 0; 17500Sstevel@tonic-gate } 17510Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 17520Sstevel@tonic-gate } 17530Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 17540Sstevel@tonic-gate } 17550Sstevel@tonic-gate ASSERT(npages == 0); 17560Sstevel@tonic-gate } 17570Sstevel@tonic-gate 17580Sstevel@tonic-gate /* 17590Sstevel@tonic-gate * A helper routine for page_create_get_something. 17600Sstevel@tonic-gate * The indenting got to deep down there. 17610Sstevel@tonic-gate * Unblock the pcf counters. Any pages freed after 17620Sstevel@tonic-gate * pcf_block got set are moved to pcf_count and 17630Sstevel@tonic-gate * wakeups (cv_broadcast() or cv_signal()) are done as needed. 17640Sstevel@tonic-gate */ 17650Sstevel@tonic-gate static void 17660Sstevel@tonic-gate pcgs_unblock(void) 17670Sstevel@tonic-gate { 17680Sstevel@tonic-gate int i; 17690Sstevel@tonic-gate struct pcf *p; 17700Sstevel@tonic-gate 17710Sstevel@tonic-gate /* Update freemem while we're here. */ 17720Sstevel@tonic-gate freemem = 0; 17730Sstevel@tonic-gate p = pcf; 17740Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 17750Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 17760Sstevel@tonic-gate ASSERT(p->pcf_count == 0); 17770Sstevel@tonic-gate p->pcf_count = p->pcf_reserve; 17780Sstevel@tonic-gate p->pcf_block = 0; 17790Sstevel@tonic-gate freemem += p->pcf_count; 17800Sstevel@tonic-gate if (p->pcf_wait) { 17810Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 17820Sstevel@tonic-gate if (freemem_wait) { 17830Sstevel@tonic-gate if (p->pcf_reserve > 1) { 17840Sstevel@tonic-gate cv_broadcast(&freemem_cv); 17850Sstevel@tonic-gate p->pcf_wait = 0; 17860Sstevel@tonic-gate } else { 17870Sstevel@tonic-gate cv_signal(&freemem_cv); 17880Sstevel@tonic-gate p->pcf_wait--; 17890Sstevel@tonic-gate } 17900Sstevel@tonic-gate } else { 17910Sstevel@tonic-gate p->pcf_wait = 0; 17920Sstevel@tonic-gate } 17930Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 17940Sstevel@tonic-gate } 17950Sstevel@tonic-gate p->pcf_reserve = 0; 17960Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 17970Sstevel@tonic-gate p++; 17980Sstevel@tonic-gate } 17990Sstevel@tonic-gate } 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate /* 18020Sstevel@tonic-gate * Called from page_create_va() when both the cache and free lists 18030Sstevel@tonic-gate * have been checked once. 18040Sstevel@tonic-gate * 18050Sstevel@tonic-gate * Either returns a page or panics since the accounting was done 18060Sstevel@tonic-gate * way before we got here. 18070Sstevel@tonic-gate * 18080Sstevel@tonic-gate * We don't come here often, so leave the accounting on permanently. 18090Sstevel@tonic-gate */ 18100Sstevel@tonic-gate 18110Sstevel@tonic-gate #define MAX_PCGS 100 18120Sstevel@tonic-gate 18130Sstevel@tonic-gate #ifdef DEBUG 18140Sstevel@tonic-gate #define PCGS_TRIES 100 18150Sstevel@tonic-gate #else /* DEBUG */ 18160Sstevel@tonic-gate #define PCGS_TRIES 10 18170Sstevel@tonic-gate #endif /* DEBUG */ 18180Sstevel@tonic-gate 18190Sstevel@tonic-gate #ifdef VM_STATS 18200Sstevel@tonic-gate uint_t pcgs_counts[PCGS_TRIES]; 18210Sstevel@tonic-gate uint_t pcgs_too_many; 18220Sstevel@tonic-gate uint_t pcgs_entered; 18230Sstevel@tonic-gate uint_t pcgs_entered_noreloc; 18240Sstevel@tonic-gate uint_t pcgs_locked; 18250Sstevel@tonic-gate uint_t pcgs_cagelocked; 18260Sstevel@tonic-gate #endif /* VM_STATS */ 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate static page_t * 18290Sstevel@tonic-gate page_create_get_something(vnode_t *vp, u_offset_t off, struct seg *seg, 18300Sstevel@tonic-gate caddr_t vaddr, uint_t flags) 18310Sstevel@tonic-gate { 18320Sstevel@tonic-gate uint_t count; 18330Sstevel@tonic-gate page_t *pp; 18340Sstevel@tonic-gate uint_t locked, i; 18350Sstevel@tonic-gate struct pcf *p; 18360Sstevel@tonic-gate lgrp_t *lgrp; 18370Sstevel@tonic-gate int cagelocked = 0; 18380Sstevel@tonic-gate 18390Sstevel@tonic-gate VM_STAT_ADD(pcgs_entered); 18400Sstevel@tonic-gate 18410Sstevel@tonic-gate /* 18420Sstevel@tonic-gate * Tap any reserve freelists: if we fail now, we'll die 18430Sstevel@tonic-gate * since the page(s) we're looking for have already been 18440Sstevel@tonic-gate * accounted for. 18450Sstevel@tonic-gate */ 18460Sstevel@tonic-gate flags |= PG_PANIC; 18470Sstevel@tonic-gate 18480Sstevel@tonic-gate if ((flags & PG_NORELOC) != 0) { 18490Sstevel@tonic-gate VM_STAT_ADD(pcgs_entered_noreloc); 18500Sstevel@tonic-gate /* 18510Sstevel@tonic-gate * Requests for free pages from critical threads 18520Sstevel@tonic-gate * such as pageout still won't throttle here, but 18530Sstevel@tonic-gate * we must try again, to give the cageout thread 18540Sstevel@tonic-gate * another chance to catch up. Since we already 18550Sstevel@tonic-gate * accounted for the pages, we had better get them 18560Sstevel@tonic-gate * this time. 18570Sstevel@tonic-gate * 18580Sstevel@tonic-gate * N.B. All non-critical threads acquire the pcgs_cagelock 18590Sstevel@tonic-gate * to serialize access to the freelists. This implements a 18600Sstevel@tonic-gate * turnstile-type synchornization to avoid starvation of 18610Sstevel@tonic-gate * critical requests for PG_NORELOC memory by non-critical 18620Sstevel@tonic-gate * threads: all non-critical threads must acquire a 'ticket' 18630Sstevel@tonic-gate * before passing through, which entails making sure 18640Sstevel@tonic-gate * kcage_freemem won't fall below minfree prior to grabbing 18650Sstevel@tonic-gate * pages from the freelists. 18660Sstevel@tonic-gate */ 18670Sstevel@tonic-gate if (kcage_create_throttle(1, flags) == KCT_NONCRIT) { 18680Sstevel@tonic-gate mutex_enter(&pcgs_cagelock); 18690Sstevel@tonic-gate cagelocked = 1; 18700Sstevel@tonic-gate VM_STAT_ADD(pcgs_cagelocked); 18710Sstevel@tonic-gate } 18720Sstevel@tonic-gate } 18730Sstevel@tonic-gate 18740Sstevel@tonic-gate /* 18750Sstevel@tonic-gate * Time to get serious. 18760Sstevel@tonic-gate * We failed to get a `correctly colored' page from both the 18770Sstevel@tonic-gate * free and cache lists. 18780Sstevel@tonic-gate * We escalate in stage. 18790Sstevel@tonic-gate * 18800Sstevel@tonic-gate * First try both lists without worring about color. 18810Sstevel@tonic-gate * 18820Sstevel@tonic-gate * Then, grab all page accounting locks (ie. pcf[]) and 18830Sstevel@tonic-gate * steal any pages that they have and set the pcf_block flag to 18840Sstevel@tonic-gate * stop deletions from the lists. This will help because 18850Sstevel@tonic-gate * a page can get added to the free list while we are looking 18860Sstevel@tonic-gate * at the cache list, then another page could be added to the cache 18870Sstevel@tonic-gate * list allowing the page on the free list to be removed as we 18880Sstevel@tonic-gate * move from looking at the cache list to the free list. This 18890Sstevel@tonic-gate * could happen over and over. We would never find the page 18900Sstevel@tonic-gate * we have accounted for. 18910Sstevel@tonic-gate * 18920Sstevel@tonic-gate * Noreloc pages are a subset of the global (relocatable) page pool. 18930Sstevel@tonic-gate * They are not tracked separately in the pcf bins, so it is 18940Sstevel@tonic-gate * impossible to know when doing pcf accounting if the available 18950Sstevel@tonic-gate * page(s) are noreloc pages or not. When looking for a noreloc page 18960Sstevel@tonic-gate * it is quite easy to end up here even if the global (relocatable) 18970Sstevel@tonic-gate * page pool has plenty of free pages but the noreloc pool is empty. 18980Sstevel@tonic-gate * 18990Sstevel@tonic-gate * When the noreloc pool is empty (or low), additional noreloc pages 19000Sstevel@tonic-gate * are created by converting pages from the global page pool. This 19010Sstevel@tonic-gate * process will stall during pcf accounting if the pcf bins are 19020Sstevel@tonic-gate * already locked. Such is the case when a noreloc allocation is 19030Sstevel@tonic-gate * looping here in page_create_get_something waiting for more noreloc 19040Sstevel@tonic-gate * pages to appear. 19050Sstevel@tonic-gate * 19060Sstevel@tonic-gate * Short of adding a new field to the pcf bins to accurately track 19070Sstevel@tonic-gate * the number of free noreloc pages, we instead do not grab the 19080Sstevel@tonic-gate * pcgs_lock, do not set the pcf blocks and do not timeout when 19090Sstevel@tonic-gate * allocating a noreloc page. This allows noreloc allocations to 19100Sstevel@tonic-gate * loop without blocking global page pool allocations. 19110Sstevel@tonic-gate * 19120Sstevel@tonic-gate * NOTE: the behaviour of page_create_get_something has not changed 19130Sstevel@tonic-gate * for the case of global page pool allocations. 19140Sstevel@tonic-gate */ 19150Sstevel@tonic-gate 19160Sstevel@tonic-gate flags &= ~PG_MATCH_COLOR; 19170Sstevel@tonic-gate locked = 0; 19180Sstevel@tonic-gate #ifndef __sparc 19190Sstevel@tonic-gate /* 19200Sstevel@tonic-gate * page_create_get_something may be called because 4g memory may be 19210Sstevel@tonic-gate * depleted. Set flags to allow for relocation of base page below 19220Sstevel@tonic-gate * 4g if necessary. 19230Sstevel@tonic-gate */ 19240Sstevel@tonic-gate if (physmax4g) 19250Sstevel@tonic-gate flags |= (PGI_PGCPSZC0 | PGI_PGCPHIPRI); 19260Sstevel@tonic-gate #endif 19270Sstevel@tonic-gate 19280Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, vaddr, PAGESIZE); 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate for (count = 0; kcage_on || count < MAX_PCGS; count++) { 19310Sstevel@tonic-gate pp = page_get_freelist(vp, off, seg, vaddr, PAGESIZE, 19320Sstevel@tonic-gate flags, lgrp); 19330Sstevel@tonic-gate if (pp == NULL) { 19340Sstevel@tonic-gate pp = page_get_cachelist(vp, off, seg, vaddr, 19350Sstevel@tonic-gate flags, lgrp); 19360Sstevel@tonic-gate } 19370Sstevel@tonic-gate if (pp == NULL) { 19380Sstevel@tonic-gate /* 19390Sstevel@tonic-gate * Serialize. Don't fight with other pcgs(). 19400Sstevel@tonic-gate */ 19410Sstevel@tonic-gate if (!locked && (!kcage_on || !(flags & PG_NORELOC))) { 19420Sstevel@tonic-gate mutex_enter(&pcgs_lock); 19430Sstevel@tonic-gate VM_STAT_ADD(pcgs_locked); 19440Sstevel@tonic-gate locked = 1; 19450Sstevel@tonic-gate p = pcf; 19460Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 19470Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 19480Sstevel@tonic-gate ASSERT(p->pcf_block == 0); 19490Sstevel@tonic-gate p->pcf_block = 1; 19500Sstevel@tonic-gate p->pcf_reserve = p->pcf_count; 19510Sstevel@tonic-gate p->pcf_count = 0; 19520Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 19530Sstevel@tonic-gate p++; 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate freemem = 0; 19560Sstevel@tonic-gate } 19570Sstevel@tonic-gate 19580Sstevel@tonic-gate if (count) { 19590Sstevel@tonic-gate /* 19600Sstevel@tonic-gate * Since page_free() puts pages on 19610Sstevel@tonic-gate * a list then accounts for it, we 19620Sstevel@tonic-gate * just have to wait for page_free() 19630Sstevel@tonic-gate * to unlock any page it was working 19640Sstevel@tonic-gate * with. The page_lock()-page_reclaim() 19650Sstevel@tonic-gate * path falls in the same boat. 19660Sstevel@tonic-gate * 19670Sstevel@tonic-gate * We don't need to check on the 19680Sstevel@tonic-gate * PG_WAIT flag, we have already 19690Sstevel@tonic-gate * accounted for the page we are 19700Sstevel@tonic-gate * looking for in page_create_va(). 19710Sstevel@tonic-gate * 19720Sstevel@tonic-gate * We just wait a moment to let any 19730Sstevel@tonic-gate * locked pages on the lists free up, 19740Sstevel@tonic-gate * then continue around and try again. 19750Sstevel@tonic-gate * 19760Sstevel@tonic-gate * Will be awakened by set_freemem(). 19770Sstevel@tonic-gate */ 19780Sstevel@tonic-gate mutex_enter(&pcgs_wait_lock); 19790Sstevel@tonic-gate cv_wait(&pcgs_cv, &pcgs_wait_lock); 19800Sstevel@tonic-gate mutex_exit(&pcgs_wait_lock); 19810Sstevel@tonic-gate } 19820Sstevel@tonic-gate } else { 19830Sstevel@tonic-gate #ifdef VM_STATS 19840Sstevel@tonic-gate if (count >= PCGS_TRIES) { 19850Sstevel@tonic-gate VM_STAT_ADD(pcgs_too_many); 19860Sstevel@tonic-gate } else { 19870Sstevel@tonic-gate VM_STAT_ADD(pcgs_counts[count]); 19880Sstevel@tonic-gate } 19890Sstevel@tonic-gate #endif 19900Sstevel@tonic-gate if (locked) { 19910Sstevel@tonic-gate pcgs_unblock(); 19920Sstevel@tonic-gate mutex_exit(&pcgs_lock); 19930Sstevel@tonic-gate } 19940Sstevel@tonic-gate if (cagelocked) 19950Sstevel@tonic-gate mutex_exit(&pcgs_cagelock); 19960Sstevel@tonic-gate return (pp); 19970Sstevel@tonic-gate } 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate /* 20000Sstevel@tonic-gate * we go down holding the pcf locks. 20010Sstevel@tonic-gate */ 20020Sstevel@tonic-gate panic("no %spage found %d", 20030Sstevel@tonic-gate ((flags & PG_NORELOC) ? "non-reloc " : ""), count); 20040Sstevel@tonic-gate /*NOTREACHED*/ 20050Sstevel@tonic-gate } 20060Sstevel@tonic-gate 20070Sstevel@tonic-gate /* 20080Sstevel@tonic-gate * Create enough pages for "bytes" worth of data starting at 20090Sstevel@tonic-gate * "off" in "vp". 20100Sstevel@tonic-gate * 20110Sstevel@tonic-gate * Where flag must be one of: 20120Sstevel@tonic-gate * 20130Sstevel@tonic-gate * PG_EXCL: Exclusive create (fail if any page already 20140Sstevel@tonic-gate * exists in the page cache) which does not 20150Sstevel@tonic-gate * wait for memory to become available. 20160Sstevel@tonic-gate * 20170Sstevel@tonic-gate * PG_WAIT: Non-exclusive create which can wait for 20180Sstevel@tonic-gate * memory to become available. 20190Sstevel@tonic-gate * 20200Sstevel@tonic-gate * PG_PHYSCONTIG: Allocate physically contiguous pages. 20210Sstevel@tonic-gate * (Not Supported) 20220Sstevel@tonic-gate * 20230Sstevel@tonic-gate * A doubly linked list of pages is returned to the caller. Each page 20240Sstevel@tonic-gate * on the list has the "exclusive" (p_selock) lock and "iolock" (p_iolock) 20250Sstevel@tonic-gate * lock. 20260Sstevel@tonic-gate * 20270Sstevel@tonic-gate * Unable to change the parameters to page_create() in a minor release, 20280Sstevel@tonic-gate * we renamed page_create() to page_create_va(), changed all known calls 20290Sstevel@tonic-gate * from page_create() to page_create_va(), and created this wrapper. 20300Sstevel@tonic-gate * 20310Sstevel@tonic-gate * Upon a major release, we should break compatibility by deleting this 20320Sstevel@tonic-gate * wrapper, and replacing all the strings "page_create_va", with "page_create". 20330Sstevel@tonic-gate * 20340Sstevel@tonic-gate * NOTE: There is a copy of this interface as page_create_io() in 20350Sstevel@tonic-gate * i86/vm/vm_machdep.c. Any bugs fixed here should be applied 20360Sstevel@tonic-gate * there. 20370Sstevel@tonic-gate */ 20380Sstevel@tonic-gate page_t * 20390Sstevel@tonic-gate page_create(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags) 20400Sstevel@tonic-gate { 20410Sstevel@tonic-gate caddr_t random_vaddr; 20420Sstevel@tonic-gate struct seg kseg; 20430Sstevel@tonic-gate 20440Sstevel@tonic-gate #ifdef DEBUG 20450Sstevel@tonic-gate cmn_err(CE_WARN, "Using deprecated interface page_create: caller %p", 20460Sstevel@tonic-gate (void *)caller()); 20470Sstevel@tonic-gate #endif 20480Sstevel@tonic-gate 20490Sstevel@tonic-gate random_vaddr = (caddr_t)(((uintptr_t)vp >> 7) ^ 20500Sstevel@tonic-gate (uintptr_t)(off >> PAGESHIFT)); 20510Sstevel@tonic-gate kseg.s_as = &kas; 20520Sstevel@tonic-gate 20530Sstevel@tonic-gate return (page_create_va(vp, off, bytes, flags, &kseg, random_vaddr)); 20540Sstevel@tonic-gate } 20550Sstevel@tonic-gate 20560Sstevel@tonic-gate #ifdef DEBUG 20570Sstevel@tonic-gate uint32_t pg_alloc_pgs_mtbf = 0; 20580Sstevel@tonic-gate #endif 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate /* 20610Sstevel@tonic-gate * Used for large page support. It will attempt to allocate 20620Sstevel@tonic-gate * a large page(s) off the freelist. 20630Sstevel@tonic-gate * 20640Sstevel@tonic-gate * Returns non zero on failure. 20650Sstevel@tonic-gate */ 20660Sstevel@tonic-gate int 20670Sstevel@tonic-gate page_alloc_pages(struct seg *seg, caddr_t addr, page_t **basepp, 20680Sstevel@tonic-gate page_t *ppa[], uint_t szc, int anypgsz) 20690Sstevel@tonic-gate { 20700Sstevel@tonic-gate pgcnt_t npgs, curnpgs, totpgs; 20710Sstevel@tonic-gate size_t pgsz; 20720Sstevel@tonic-gate page_t *pplist = NULL, *pp; 20730Sstevel@tonic-gate int err = 0; 20740Sstevel@tonic-gate lgrp_t *lgrp; 20750Sstevel@tonic-gate 20760Sstevel@tonic-gate ASSERT(szc != 0 && szc <= (page_num_pagesizes() - 1)); 20770Sstevel@tonic-gate 20780Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[0]); 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate #ifdef DEBUG 20810Sstevel@tonic-gate if (pg_alloc_pgs_mtbf && !(gethrtime() % pg_alloc_pgs_mtbf)) { 20820Sstevel@tonic-gate return (ENOMEM); 20830Sstevel@tonic-gate } 20840Sstevel@tonic-gate #endif 20850Sstevel@tonic-gate 20860Sstevel@tonic-gate pgsz = page_get_pagesize(szc); 20870Sstevel@tonic-gate totpgs = curnpgs = npgs = pgsz >> PAGESHIFT; 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate ASSERT(((uintptr_t)addr & (pgsz - 1)) == 0); 20900Sstevel@tonic-gate /* 20910Sstevel@tonic-gate * One must be NULL but not both. 20920Sstevel@tonic-gate * And one must be non NULL but not both. 20930Sstevel@tonic-gate */ 20940Sstevel@tonic-gate ASSERT(basepp != NULL || ppa != NULL); 20950Sstevel@tonic-gate ASSERT(basepp == NULL || ppa == NULL); 20960Sstevel@tonic-gate 20970Sstevel@tonic-gate (void) page_create_wait(npgs, PG_WAIT); 20980Sstevel@tonic-gate 20990Sstevel@tonic-gate while (npgs && szc) { 21000Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, addr, pgsz); 21010Sstevel@tonic-gate pp = page_get_freelist(NULL, 0, seg, addr, pgsz, 0, lgrp); 21020Sstevel@tonic-gate if (pp != NULL) { 21030Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[1]); 21040Sstevel@tonic-gate page_list_concat(&pplist, &pp); 21050Sstevel@tonic-gate ASSERT(npgs >= curnpgs); 21060Sstevel@tonic-gate npgs -= curnpgs; 21070Sstevel@tonic-gate } else if (anypgsz) { 21080Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[2]); 21090Sstevel@tonic-gate szc--; 21100Sstevel@tonic-gate pgsz = page_get_pagesize(szc); 21110Sstevel@tonic-gate curnpgs = pgsz >> PAGESHIFT; 21120Sstevel@tonic-gate } else { 21130Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[3]); 21140Sstevel@tonic-gate ASSERT(npgs == totpgs); 21150Sstevel@tonic-gate page_create_putback(npgs); 21160Sstevel@tonic-gate return (ENOMEM); 21170Sstevel@tonic-gate } 21180Sstevel@tonic-gate } 21190Sstevel@tonic-gate if (szc == 0) { 21200Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[4]); 21210Sstevel@tonic-gate ASSERT(npgs != 0); 21220Sstevel@tonic-gate page_create_putback(npgs); 21230Sstevel@tonic-gate err = ENOMEM; 21240Sstevel@tonic-gate } else if (basepp != NULL) { 21250Sstevel@tonic-gate ASSERT(npgs == 0); 21260Sstevel@tonic-gate ASSERT(ppa == NULL); 21270Sstevel@tonic-gate *basepp = pplist; 21280Sstevel@tonic-gate } 21290Sstevel@tonic-gate 21300Sstevel@tonic-gate npgs = totpgs - npgs; 21310Sstevel@tonic-gate pp = pplist; 21320Sstevel@tonic-gate 21330Sstevel@tonic-gate /* 21340Sstevel@tonic-gate * Clear the free and age bits. Also if we were passed in a ppa then 21350Sstevel@tonic-gate * fill it in with all the constituent pages from the large page. But 21360Sstevel@tonic-gate * if we failed to allocate all the pages just free what we got. 21370Sstevel@tonic-gate */ 21380Sstevel@tonic-gate while (npgs != 0) { 21390Sstevel@tonic-gate ASSERT(PP_ISFREE(pp)); 21400Sstevel@tonic-gate ASSERT(PP_ISAGED(pp)); 21410Sstevel@tonic-gate if (ppa != NULL || err != 0) { 21420Sstevel@tonic-gate if (err == 0) { 21430Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[5]); 21440Sstevel@tonic-gate PP_CLRFREE(pp); 21450Sstevel@tonic-gate PP_CLRAGED(pp); 21460Sstevel@tonic-gate page_sub(&pplist, pp); 21470Sstevel@tonic-gate *ppa++ = pp; 21480Sstevel@tonic-gate npgs--; 21490Sstevel@tonic-gate } else { 21500Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[6]); 21510Sstevel@tonic-gate ASSERT(pp->p_szc != 0); 21520Sstevel@tonic-gate curnpgs = page_get_pagecnt(pp->p_szc); 21530Sstevel@tonic-gate page_list_break(&pp, &pplist, curnpgs); 21540Sstevel@tonic-gate page_list_add_pages(pp, 0); 21550Sstevel@tonic-gate page_create_putback(curnpgs); 21560Sstevel@tonic-gate ASSERT(npgs >= curnpgs); 21570Sstevel@tonic-gate npgs -= curnpgs; 21580Sstevel@tonic-gate } 21590Sstevel@tonic-gate pp = pplist; 21600Sstevel@tonic-gate } else { 21610Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[7]); 21620Sstevel@tonic-gate PP_CLRFREE(pp); 21630Sstevel@tonic-gate PP_CLRAGED(pp); 21640Sstevel@tonic-gate pp = pp->p_next; 21650Sstevel@tonic-gate npgs--; 21660Sstevel@tonic-gate } 21670Sstevel@tonic-gate } 21680Sstevel@tonic-gate return (err); 21690Sstevel@tonic-gate } 21700Sstevel@tonic-gate 21710Sstevel@tonic-gate /* 21720Sstevel@tonic-gate * Get a single large page off of the freelists, and set it up for use. 21730Sstevel@tonic-gate * Number of bytes requested must be a supported page size. 21740Sstevel@tonic-gate * 21750Sstevel@tonic-gate * Note that this call may fail even if there is sufficient 21760Sstevel@tonic-gate * memory available or PG_WAIT is set, so the caller must 21770Sstevel@tonic-gate * be willing to fallback on page_create_va(), block and retry, 21780Sstevel@tonic-gate * or fail the requester. 21790Sstevel@tonic-gate */ 21800Sstevel@tonic-gate page_t * 21810Sstevel@tonic-gate page_create_va_large(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags, 21820Sstevel@tonic-gate struct seg *seg, caddr_t vaddr, void *arg) 21830Sstevel@tonic-gate { 21840Sstevel@tonic-gate pgcnt_t npages, pcftotal; 21850Sstevel@tonic-gate page_t *pp; 21860Sstevel@tonic-gate page_t *rootpp; 21870Sstevel@tonic-gate lgrp_t *lgrp; 21880Sstevel@tonic-gate uint_t enough; 21890Sstevel@tonic-gate uint_t pcf_index; 21900Sstevel@tonic-gate uint_t i; 21910Sstevel@tonic-gate struct pcf *p; 21920Sstevel@tonic-gate struct pcf *q; 21930Sstevel@tonic-gate lgrp_id_t *lgrpid = (lgrp_id_t *)arg; 21940Sstevel@tonic-gate 21950Sstevel@tonic-gate ASSERT(vp != NULL); 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate ASSERT((flags & ~(PG_EXCL | PG_WAIT | 21980Sstevel@tonic-gate PG_NORELOC | PG_PANIC | PG_PUSHPAGE)) == 0); 21990Sstevel@tonic-gate /* but no others */ 22000Sstevel@tonic-gate 22010Sstevel@tonic-gate ASSERT((flags & PG_EXCL) == PG_EXCL); 22020Sstevel@tonic-gate 22030Sstevel@tonic-gate npages = btop(bytes); 22040Sstevel@tonic-gate 22050Sstevel@tonic-gate if (!kcage_on || panicstr) { 22060Sstevel@tonic-gate /* 22070Sstevel@tonic-gate * Cage is OFF, or we are single threaded in 22080Sstevel@tonic-gate * panic, so make everything a RELOC request. 22090Sstevel@tonic-gate */ 22100Sstevel@tonic-gate flags &= ~PG_NORELOC; 22110Sstevel@tonic-gate } 22120Sstevel@tonic-gate 22130Sstevel@tonic-gate /* 22140Sstevel@tonic-gate * Make sure there's adequate physical memory available. 22150Sstevel@tonic-gate * Note: PG_WAIT is ignored here. 22160Sstevel@tonic-gate */ 22170Sstevel@tonic-gate if (freemem <= throttlefree + npages) { 22180Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[1]); 22190Sstevel@tonic-gate return (NULL); 22200Sstevel@tonic-gate } 22210Sstevel@tonic-gate 22220Sstevel@tonic-gate /* 22230Sstevel@tonic-gate * If cage is on, dampen draw from cage when available 22240Sstevel@tonic-gate * cage space is low. 22250Sstevel@tonic-gate */ 22260Sstevel@tonic-gate if ((flags & (PG_NORELOC | PG_WAIT)) == (PG_NORELOC | PG_WAIT) && 22270Sstevel@tonic-gate kcage_freemem < kcage_throttlefree + npages) { 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate /* 22300Sstevel@tonic-gate * The cage is on, the caller wants PG_NORELOC 22310Sstevel@tonic-gate * pages and available cage memory is very low. 22320Sstevel@tonic-gate * Call kcage_create_throttle() to attempt to 22330Sstevel@tonic-gate * control demand on the cage. 22340Sstevel@tonic-gate */ 22350Sstevel@tonic-gate if (kcage_create_throttle(npages, flags) == KCT_FAILURE) { 22360Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[2]); 22370Sstevel@tonic-gate return (NULL); 22380Sstevel@tonic-gate } 22390Sstevel@tonic-gate } 22400Sstevel@tonic-gate 22410Sstevel@tonic-gate enough = 0; 22420Sstevel@tonic-gate pcf_index = PCF_INDEX(); 22430Sstevel@tonic-gate p = &pcf[pcf_index]; 22440Sstevel@tonic-gate p->pcf_touch = 1; 22450Sstevel@tonic-gate q = &pcf[PCF_FANOUT]; 22460Sstevel@tonic-gate for (pcftotal = 0, i = 0; i < PCF_FANOUT; i++) { 22470Sstevel@tonic-gate if (p->pcf_count > npages) { 22480Sstevel@tonic-gate /* 22490Sstevel@tonic-gate * a good one to try. 22500Sstevel@tonic-gate */ 22510Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 22520Sstevel@tonic-gate if (p->pcf_count > npages) { 22530Sstevel@tonic-gate p->pcf_count -= (uint_t)npages; 22540Sstevel@tonic-gate /* 22550Sstevel@tonic-gate * freemem is not protected by any lock. 22560Sstevel@tonic-gate * Thus, we cannot have any assertion 22570Sstevel@tonic-gate * containing freemem here. 22580Sstevel@tonic-gate */ 22590Sstevel@tonic-gate freemem -= npages; 22600Sstevel@tonic-gate enough = 1; 22610Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 22620Sstevel@tonic-gate break; 22630Sstevel@tonic-gate } 22640Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 22650Sstevel@tonic-gate } 22660Sstevel@tonic-gate pcftotal += p->pcf_count; 22670Sstevel@tonic-gate p++; 22680Sstevel@tonic-gate if (p >= q) { 22690Sstevel@tonic-gate p = pcf; 22700Sstevel@tonic-gate } 22710Sstevel@tonic-gate p->pcf_touch = 1; 22720Sstevel@tonic-gate } 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate if (!enough) { 22750Sstevel@tonic-gate /* If there isn't enough memory available, give up. */ 22760Sstevel@tonic-gate if (pcftotal < npages) { 22770Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[3]); 22780Sstevel@tonic-gate return (NULL); 22790Sstevel@tonic-gate } 22800Sstevel@tonic-gate 22810Sstevel@tonic-gate /* try to collect pages from several pcf bins */ 22820Sstevel@tonic-gate for (p = pcf, pcftotal = 0, i = 0; i < PCF_FANOUT; i++) { 22830Sstevel@tonic-gate p->pcf_touch = 1; 22840Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 22850Sstevel@tonic-gate pcftotal += p->pcf_count; 22860Sstevel@tonic-gate if (pcftotal >= npages) { 22870Sstevel@tonic-gate /* 22880Sstevel@tonic-gate * Wow! There are enough pages laying around 22890Sstevel@tonic-gate * to satisfy the request. Do the accounting, 22900Sstevel@tonic-gate * drop the locks we acquired, and go back. 22910Sstevel@tonic-gate * 22920Sstevel@tonic-gate * freemem is not protected by any lock. So, 22930Sstevel@tonic-gate * we cannot have any assertion containing 22940Sstevel@tonic-gate * freemem. 22950Sstevel@tonic-gate */ 22960Sstevel@tonic-gate pgcnt_t tpages = npages; 22970Sstevel@tonic-gate freemem -= npages; 22980Sstevel@tonic-gate while (p >= pcf) { 22990Sstevel@tonic-gate if (p->pcf_count <= tpages) { 23000Sstevel@tonic-gate tpages -= p->pcf_count; 23010Sstevel@tonic-gate p->pcf_count = 0; 23020Sstevel@tonic-gate } else { 23030Sstevel@tonic-gate p->pcf_count -= (uint_t)tpages; 23040Sstevel@tonic-gate tpages = 0; 23050Sstevel@tonic-gate } 23060Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 23070Sstevel@tonic-gate p--; 23080Sstevel@tonic-gate } 23090Sstevel@tonic-gate ASSERT(tpages == 0); 23100Sstevel@tonic-gate break; 23110Sstevel@tonic-gate } 23120Sstevel@tonic-gate p++; 23130Sstevel@tonic-gate } 23140Sstevel@tonic-gate if (i == PCF_FANOUT) { 23150Sstevel@tonic-gate /* failed to collect pages - release the locks */ 23160Sstevel@tonic-gate while (--p >= pcf) { 23170Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 23180Sstevel@tonic-gate } 23190Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[4]); 23200Sstevel@tonic-gate return (NULL); 23210Sstevel@tonic-gate } 23220Sstevel@tonic-gate } 23230Sstevel@tonic-gate 23240Sstevel@tonic-gate /* 23250Sstevel@tonic-gate * This is where this function behaves fundamentally differently 23260Sstevel@tonic-gate * than page_create_va(); since we're intending to map the page 23270Sstevel@tonic-gate * with a single TTE, we have to get it as a physically contiguous 23280Sstevel@tonic-gate * hardware pagesize chunk. If we can't, we fail. 23290Sstevel@tonic-gate */ 23300Sstevel@tonic-gate if (lgrpid != NULL && *lgrpid >= 0 && *lgrpid <= lgrp_alloc_max && 23310Sstevel@tonic-gate LGRP_EXISTS(lgrp_table[*lgrpid])) 23320Sstevel@tonic-gate lgrp = lgrp_table[*lgrpid]; 23330Sstevel@tonic-gate else 23340Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, vaddr, bytes); 23350Sstevel@tonic-gate 23360Sstevel@tonic-gate if ((rootpp = page_get_freelist(&kvp, off, seg, vaddr, 23370Sstevel@tonic-gate bytes, flags & ~PG_MATCH_COLOR, lgrp)) == NULL) { 23380Sstevel@tonic-gate page_create_putback(npages); 23390Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[5]); 23400Sstevel@tonic-gate return (NULL); 23410Sstevel@tonic-gate } 23420Sstevel@tonic-gate 23430Sstevel@tonic-gate /* 23440Sstevel@tonic-gate * if we got the page with the wrong mtype give it back this is a 23450Sstevel@tonic-gate * workaround for CR 6249718. When CR 6249718 is fixed we never get 23460Sstevel@tonic-gate * inside "if" and the workaround becomes just a nop 23470Sstevel@tonic-gate */ 23480Sstevel@tonic-gate if (kcage_on && (flags & PG_NORELOC) && !PP_ISNORELOC(rootpp)) { 23490Sstevel@tonic-gate page_list_add_pages(rootpp, 0); 23500Sstevel@tonic-gate page_create_putback(npages); 23510Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[6]); 23520Sstevel@tonic-gate return (NULL); 23530Sstevel@tonic-gate } 23540Sstevel@tonic-gate 23550Sstevel@tonic-gate /* 23560Sstevel@tonic-gate * If satisfying this request has left us with too little 23570Sstevel@tonic-gate * memory, start the wheels turning to get some back. The 23580Sstevel@tonic-gate * first clause of the test prevents waking up the pageout 23590Sstevel@tonic-gate * daemon in situations where it would decide that there's 23600Sstevel@tonic-gate * nothing to do. 23610Sstevel@tonic-gate */ 23620Sstevel@tonic-gate if (nscan < desscan && freemem < minfree) { 23630Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL, 23640Sstevel@tonic-gate "pageout_cv_signal:freemem %ld", freemem); 23650Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 23660Sstevel@tonic-gate } 23670Sstevel@tonic-gate 23680Sstevel@tonic-gate pp = rootpp; 23690Sstevel@tonic-gate while (npages--) { 23700Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 23710Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL); 23720Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 23730Sstevel@tonic-gate PP_CLRFREE(pp); 23740Sstevel@tonic-gate PP_CLRAGED(pp); 23750Sstevel@tonic-gate if (!page_hashin(pp, vp, off, NULL)) 23760Sstevel@tonic-gate panic("page_create_large: hashin failed: page %p", 23770Sstevel@tonic-gate (void *)pp); 23780Sstevel@tonic-gate page_io_lock(pp); 23790Sstevel@tonic-gate off += PAGESIZE; 23800Sstevel@tonic-gate pp = pp->p_next; 23810Sstevel@tonic-gate } 23820Sstevel@tonic-gate 23830Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[0]); 23840Sstevel@tonic-gate return (rootpp); 23850Sstevel@tonic-gate } 23860Sstevel@tonic-gate 23870Sstevel@tonic-gate page_t * 23880Sstevel@tonic-gate page_create_va(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags, 23890Sstevel@tonic-gate struct seg *seg, caddr_t vaddr) 23900Sstevel@tonic-gate { 23910Sstevel@tonic-gate page_t *plist = NULL; 23920Sstevel@tonic-gate pgcnt_t npages; 23930Sstevel@tonic-gate pgcnt_t found_on_free = 0; 23940Sstevel@tonic-gate pgcnt_t pages_req; 23950Sstevel@tonic-gate page_t *npp = NULL; 23960Sstevel@tonic-gate uint_t enough; 23970Sstevel@tonic-gate uint_t i; 23980Sstevel@tonic-gate uint_t pcf_index; 23990Sstevel@tonic-gate struct pcf *p; 24000Sstevel@tonic-gate struct pcf *q; 24010Sstevel@tonic-gate lgrp_t *lgrp; 24020Sstevel@tonic-gate 24030Sstevel@tonic-gate TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_START, 24040Sstevel@tonic-gate "page_create_start:vp %p off %llx bytes %lu flags %x", 24050Sstevel@tonic-gate vp, off, bytes, flags); 24060Sstevel@tonic-gate 24070Sstevel@tonic-gate ASSERT(bytes != 0 && vp != NULL); 24080Sstevel@tonic-gate 24090Sstevel@tonic-gate if ((flags & PG_EXCL) == 0 && (flags & PG_WAIT) == 0) { 24100Sstevel@tonic-gate panic("page_create: invalid flags"); 24110Sstevel@tonic-gate /*NOTREACHED*/ 24120Sstevel@tonic-gate } 24130Sstevel@tonic-gate ASSERT((flags & ~(PG_EXCL | PG_WAIT | 24140Sstevel@tonic-gate PG_NORELOC | PG_PANIC | PG_PUSHPAGE)) == 0); 24150Sstevel@tonic-gate /* but no others */ 24160Sstevel@tonic-gate 24170Sstevel@tonic-gate pages_req = npages = btopr(bytes); 24180Sstevel@tonic-gate /* 24190Sstevel@tonic-gate * Try to see whether request is too large to *ever* be 24200Sstevel@tonic-gate * satisfied, in order to prevent deadlock. We arbitrarily 24210Sstevel@tonic-gate * decide to limit maximum size requests to max_page_get. 24220Sstevel@tonic-gate */ 24230Sstevel@tonic-gate if (npages >= max_page_get) { 24240Sstevel@tonic-gate if ((flags & PG_WAIT) == 0) { 24250Sstevel@tonic-gate TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_TOOBIG, 24260Sstevel@tonic-gate "page_create_toobig:vp %p off %llx npages " 24270Sstevel@tonic-gate "%lu max_page_get %lu", 24280Sstevel@tonic-gate vp, off, npages, max_page_get); 24290Sstevel@tonic-gate return (NULL); 24300Sstevel@tonic-gate } else { 24310Sstevel@tonic-gate cmn_err(CE_WARN, 24320Sstevel@tonic-gate "Request for too much kernel memory " 24330Sstevel@tonic-gate "(%lu bytes), will hang forever", bytes); 24340Sstevel@tonic-gate for (;;) 24350Sstevel@tonic-gate delay(1000000000); 24360Sstevel@tonic-gate } 24370Sstevel@tonic-gate } 24380Sstevel@tonic-gate 24390Sstevel@tonic-gate if (!kcage_on || panicstr) { 24400Sstevel@tonic-gate /* 24410Sstevel@tonic-gate * Cage is OFF, or we are single threaded in 24420Sstevel@tonic-gate * panic, so make everything a RELOC request. 24430Sstevel@tonic-gate */ 24440Sstevel@tonic-gate flags &= ~PG_NORELOC; 24450Sstevel@tonic-gate } 24460Sstevel@tonic-gate 24470Sstevel@tonic-gate if (freemem <= throttlefree + npages) 24480Sstevel@tonic-gate if (!page_create_throttle(npages, flags)) 24490Sstevel@tonic-gate return (NULL); 24500Sstevel@tonic-gate 24510Sstevel@tonic-gate /* 24520Sstevel@tonic-gate * If cage is on, dampen draw from cage when available 24530Sstevel@tonic-gate * cage space is low. 24540Sstevel@tonic-gate */ 24550Sstevel@tonic-gate if ((flags & PG_NORELOC) && 24560Sstevel@tonic-gate kcage_freemem < kcage_throttlefree + npages) { 24570Sstevel@tonic-gate 24580Sstevel@tonic-gate /* 24590Sstevel@tonic-gate * The cage is on, the caller wants PG_NORELOC 24600Sstevel@tonic-gate * pages and available cage memory is very low. 24610Sstevel@tonic-gate * Call kcage_create_throttle() to attempt to 24620Sstevel@tonic-gate * control demand on the cage. 24630Sstevel@tonic-gate */ 24640Sstevel@tonic-gate if (kcage_create_throttle(npages, flags) == KCT_FAILURE) 24650Sstevel@tonic-gate return (NULL); 24660Sstevel@tonic-gate } 24670Sstevel@tonic-gate 24680Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[0]); 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate enough = 0; 24710Sstevel@tonic-gate pcf_index = PCF_INDEX(); 24720Sstevel@tonic-gate 24730Sstevel@tonic-gate p = &pcf[pcf_index]; 24740Sstevel@tonic-gate p->pcf_touch = 1; 24750Sstevel@tonic-gate q = &pcf[PCF_FANOUT]; 24760Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 24770Sstevel@tonic-gate if (p->pcf_count > npages) { 24780Sstevel@tonic-gate /* 24790Sstevel@tonic-gate * a good one to try. 24800Sstevel@tonic-gate */ 24810Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 24820Sstevel@tonic-gate if (p->pcf_count > npages) { 24830Sstevel@tonic-gate p->pcf_count -= (uint_t)npages; 24840Sstevel@tonic-gate /* 24850Sstevel@tonic-gate * freemem is not protected by any lock. 24860Sstevel@tonic-gate * Thus, we cannot have any assertion 24870Sstevel@tonic-gate * containing freemem here. 24880Sstevel@tonic-gate */ 24890Sstevel@tonic-gate freemem -= npages; 24900Sstevel@tonic-gate enough = 1; 24910Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 24920Sstevel@tonic-gate break; 24930Sstevel@tonic-gate } 24940Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 24950Sstevel@tonic-gate } 24960Sstevel@tonic-gate p++; 24970Sstevel@tonic-gate if (p >= q) { 24980Sstevel@tonic-gate p = pcf; 24990Sstevel@tonic-gate } 25000Sstevel@tonic-gate p->pcf_touch = 1; 25010Sstevel@tonic-gate } 25020Sstevel@tonic-gate 25030Sstevel@tonic-gate if (!enough) { 25040Sstevel@tonic-gate /* 25050Sstevel@tonic-gate * Have to look harder. If npages is greater than 25060Sstevel@tonic-gate * one, then we might have to coalecse the counters. 25070Sstevel@tonic-gate * 25080Sstevel@tonic-gate * Go wait. We come back having accounted 25090Sstevel@tonic-gate * for the memory. 25100Sstevel@tonic-gate */ 25110Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[1]); 25120Sstevel@tonic-gate if (!page_create_wait(npages, flags)) { 25130Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[2]); 25140Sstevel@tonic-gate return (NULL); 25150Sstevel@tonic-gate } 25160Sstevel@tonic-gate } 25170Sstevel@tonic-gate 25180Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SUCCESS, 25190Sstevel@tonic-gate "page_create_success:vp %p off %llx", vp, off); 25200Sstevel@tonic-gate 25210Sstevel@tonic-gate /* 25220Sstevel@tonic-gate * If satisfying this request has left us with too little 25230Sstevel@tonic-gate * memory, start the wheels turning to get some back. The 25240Sstevel@tonic-gate * first clause of the test prevents waking up the pageout 25250Sstevel@tonic-gate * daemon in situations where it would decide that there's 25260Sstevel@tonic-gate * nothing to do. 25270Sstevel@tonic-gate */ 25280Sstevel@tonic-gate if (nscan < desscan && freemem < minfree) { 25290Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL, 25300Sstevel@tonic-gate "pageout_cv_signal:freemem %ld", freemem); 25310Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 25320Sstevel@tonic-gate } 25330Sstevel@tonic-gate 25340Sstevel@tonic-gate /* 25350Sstevel@tonic-gate * Loop around collecting the requested number of pages. 25360Sstevel@tonic-gate * Most of the time, we have to `create' a new page. With 25370Sstevel@tonic-gate * this in mind, pull the page off the free list before 25380Sstevel@tonic-gate * getting the hash lock. This will minimize the hash 25390Sstevel@tonic-gate * lock hold time, nesting, and the like. If it turns 25400Sstevel@tonic-gate * out we don't need the page, we put it back at the end. 25410Sstevel@tonic-gate */ 25420Sstevel@tonic-gate while (npages--) { 25430Sstevel@tonic-gate page_t *pp; 25440Sstevel@tonic-gate kmutex_t *phm = NULL; 25450Sstevel@tonic-gate ulong_t index; 25460Sstevel@tonic-gate 25470Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 25480Sstevel@tonic-gate top: 25490Sstevel@tonic-gate ASSERT(phm == NULL); 25500Sstevel@tonic-gate ASSERT(index == PAGE_HASH_FUNC(vp, off)); 25510Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 25520Sstevel@tonic-gate 25530Sstevel@tonic-gate if (npp == NULL) { 25540Sstevel@tonic-gate /* 25550Sstevel@tonic-gate * Try to get a page from the freelist (ie, 25560Sstevel@tonic-gate * a page with no [vp, off] tag). If that 25570Sstevel@tonic-gate * fails, use the cachelist. 25580Sstevel@tonic-gate * 25590Sstevel@tonic-gate * During the first attempt at both the free 25600Sstevel@tonic-gate * and cache lists we try for the correct color. 25610Sstevel@tonic-gate */ 25620Sstevel@tonic-gate /* 25630Sstevel@tonic-gate * XXXX-how do we deal with virtual indexed 25640Sstevel@tonic-gate * caches and and colors? 25650Sstevel@tonic-gate */ 25660Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[4]); 25670Sstevel@tonic-gate /* 25680Sstevel@tonic-gate * Get lgroup to allocate next page of shared memory 25690Sstevel@tonic-gate * from and use it to specify where to allocate 25700Sstevel@tonic-gate * the physical memory 25710Sstevel@tonic-gate */ 25720Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, vaddr, PAGESIZE); 25730Sstevel@tonic-gate npp = page_get_freelist(vp, off, seg, vaddr, PAGESIZE, 25740Sstevel@tonic-gate flags | PG_MATCH_COLOR, lgrp); 25750Sstevel@tonic-gate if (npp == NULL) { 25760Sstevel@tonic-gate npp = page_get_cachelist(vp, off, seg, 25770Sstevel@tonic-gate vaddr, flags | PG_MATCH_COLOR, lgrp); 25780Sstevel@tonic-gate if (npp == NULL) { 25790Sstevel@tonic-gate npp = page_create_get_something(vp, 25800Sstevel@tonic-gate off, seg, vaddr, 25810Sstevel@tonic-gate flags & ~PG_MATCH_COLOR); 25820Sstevel@tonic-gate } 25830Sstevel@tonic-gate 25840Sstevel@tonic-gate if (PP_ISAGED(npp) == 0) { 25850Sstevel@tonic-gate /* 25860Sstevel@tonic-gate * Since this page came from the 25870Sstevel@tonic-gate * cachelist, we must destroy the 25880Sstevel@tonic-gate * old vnode association. 25890Sstevel@tonic-gate */ 25900Sstevel@tonic-gate page_hashout(npp, NULL); 25910Sstevel@tonic-gate } 25920Sstevel@tonic-gate } 25930Sstevel@tonic-gate } 25940Sstevel@tonic-gate 25950Sstevel@tonic-gate /* 25960Sstevel@tonic-gate * We own this page! 25970Sstevel@tonic-gate */ 25980Sstevel@tonic-gate ASSERT(PAGE_EXCL(npp)); 25990Sstevel@tonic-gate ASSERT(npp->p_vnode == NULL); 26000Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(npp)); 26010Sstevel@tonic-gate PP_CLRFREE(npp); 26020Sstevel@tonic-gate PP_CLRAGED(npp); 26030Sstevel@tonic-gate 26040Sstevel@tonic-gate /* 26050Sstevel@tonic-gate * Here we have a page in our hot little mits and are 26060Sstevel@tonic-gate * just waiting to stuff it on the appropriate lists. 26070Sstevel@tonic-gate * Get the mutex and check to see if it really does 26080Sstevel@tonic-gate * not exist. 26090Sstevel@tonic-gate */ 26100Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 26110Sstevel@tonic-gate mutex_enter(phm); 26120Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 26130Sstevel@tonic-gate if (pp == NULL) { 26140Sstevel@tonic-gate VM_STAT_ADD(page_create_new); 26150Sstevel@tonic-gate pp = npp; 26160Sstevel@tonic-gate npp = NULL; 26170Sstevel@tonic-gate if (!page_hashin(pp, vp, off, phm)) { 26180Sstevel@tonic-gate /* 26190Sstevel@tonic-gate * Since we hold the page hash mutex and 26200Sstevel@tonic-gate * just searched for this page, page_hashin 26210Sstevel@tonic-gate * had better not fail. If it does, that 26220Sstevel@tonic-gate * means somethread did not follow the 26230Sstevel@tonic-gate * page hash mutex rules. Panic now and 26240Sstevel@tonic-gate * get it over with. As usual, go down 26250Sstevel@tonic-gate * holding all the locks. 26260Sstevel@tonic-gate */ 26270Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 26280Sstevel@tonic-gate panic("page_create: " 26290Sstevel@tonic-gate "hashin failed %p %p %llx %p", 26300Sstevel@tonic-gate (void *)pp, (void *)vp, off, (void *)phm); 26310Sstevel@tonic-gate /*NOTREACHED*/ 26320Sstevel@tonic-gate } 26330Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 26340Sstevel@tonic-gate mutex_exit(phm); 26350Sstevel@tonic-gate phm = NULL; 26360Sstevel@tonic-gate 26370Sstevel@tonic-gate /* 26380Sstevel@tonic-gate * Hat layer locking need not be done to set 26390Sstevel@tonic-gate * the following bits since the page is not hashed 26400Sstevel@tonic-gate * and was on the free list (i.e., had no mappings). 26410Sstevel@tonic-gate * 26420Sstevel@tonic-gate * Set the reference bit to protect 26430Sstevel@tonic-gate * against immediate pageout 26440Sstevel@tonic-gate * 26450Sstevel@tonic-gate * XXXmh modify freelist code to set reference 26460Sstevel@tonic-gate * bit so we don't have to do it here. 26470Sstevel@tonic-gate */ 26480Sstevel@tonic-gate page_set_props(pp, P_REF); 26490Sstevel@tonic-gate found_on_free++; 26500Sstevel@tonic-gate } else { 26510Sstevel@tonic-gate VM_STAT_ADD(page_create_exists); 26520Sstevel@tonic-gate if (flags & PG_EXCL) { 26530Sstevel@tonic-gate /* 26540Sstevel@tonic-gate * Found an existing page, and the caller 26550Sstevel@tonic-gate * wanted all new pages. Undo all of the work 26560Sstevel@tonic-gate * we have done. 26570Sstevel@tonic-gate */ 26580Sstevel@tonic-gate mutex_exit(phm); 26590Sstevel@tonic-gate phm = NULL; 26600Sstevel@tonic-gate while (plist != NULL) { 26610Sstevel@tonic-gate pp = plist; 26620Sstevel@tonic-gate page_sub(&plist, pp); 26630Sstevel@tonic-gate page_io_unlock(pp); 26640Sstevel@tonic-gate /* large pages should not end up here */ 26650Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 26660Sstevel@tonic-gate /*LINTED: constant in conditional ctx*/ 26670Sstevel@tonic-gate VN_DISPOSE(pp, B_INVAL, 0, kcred); 26680Sstevel@tonic-gate } 26690Sstevel@tonic-gate VM_STAT_ADD(page_create_found_one); 26700Sstevel@tonic-gate goto fail; 26710Sstevel@tonic-gate } 26720Sstevel@tonic-gate ASSERT(flags & PG_WAIT); 26730Sstevel@tonic-gate if (!page_lock(pp, SE_EXCL, phm, P_NO_RECLAIM)) { 26740Sstevel@tonic-gate /* 26750Sstevel@tonic-gate * Start all over again if we blocked trying 26760Sstevel@tonic-gate * to lock the page. 26770Sstevel@tonic-gate */ 26780Sstevel@tonic-gate mutex_exit(phm); 26790Sstevel@tonic-gate VM_STAT_ADD(page_create_page_lock_failed); 26800Sstevel@tonic-gate phm = NULL; 26810Sstevel@tonic-gate goto top; 26820Sstevel@tonic-gate } 26830Sstevel@tonic-gate mutex_exit(phm); 26840Sstevel@tonic-gate phm = NULL; 26850Sstevel@tonic-gate 26860Sstevel@tonic-gate if (PP_ISFREE(pp)) { 26870Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0); 26880Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_get_cache); 26890Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST); 26900Sstevel@tonic-gate PP_CLRFREE(pp); 26910Sstevel@tonic-gate found_on_free++; 26920Sstevel@tonic-gate } 26930Sstevel@tonic-gate } 26940Sstevel@tonic-gate 26950Sstevel@tonic-gate /* 26960Sstevel@tonic-gate * Got a page! It is locked. Acquire the i/o 26970Sstevel@tonic-gate * lock since we are going to use the p_next and 26980Sstevel@tonic-gate * p_prev fields to link the requested pages together. 26990Sstevel@tonic-gate */ 27000Sstevel@tonic-gate page_io_lock(pp); 27010Sstevel@tonic-gate page_add(&plist, pp); 27020Sstevel@tonic-gate plist = plist->p_next; 27030Sstevel@tonic-gate off += PAGESIZE; 27040Sstevel@tonic-gate vaddr += PAGESIZE; 27050Sstevel@tonic-gate } 27060Sstevel@tonic-gate 27070Sstevel@tonic-gate ASSERT((flags & PG_EXCL) ? (found_on_free == pages_req) : 1); 27080Sstevel@tonic-gate fail: 27090Sstevel@tonic-gate if (npp != NULL) { 27100Sstevel@tonic-gate /* 27110Sstevel@tonic-gate * Did not need this page after all. 27120Sstevel@tonic-gate * Put it back on the free list. 27130Sstevel@tonic-gate */ 27140Sstevel@tonic-gate VM_STAT_ADD(page_create_putbacks); 27150Sstevel@tonic-gate PP_SETFREE(npp); 27160Sstevel@tonic-gate PP_SETAGED(npp); 27170Sstevel@tonic-gate npp->p_offset = (u_offset_t)-1; 27180Sstevel@tonic-gate page_list_add(npp, PG_FREE_LIST | PG_LIST_TAIL); 27190Sstevel@tonic-gate page_unlock(npp); 27200Sstevel@tonic-gate 27210Sstevel@tonic-gate } 27220Sstevel@tonic-gate 27230Sstevel@tonic-gate ASSERT(pages_req >= found_on_free); 27240Sstevel@tonic-gate 27250Sstevel@tonic-gate { 27260Sstevel@tonic-gate uint_t overshoot = (uint_t)(pages_req - found_on_free); 27270Sstevel@tonic-gate 27280Sstevel@tonic-gate if (overshoot) { 27290Sstevel@tonic-gate VM_STAT_ADD(page_create_overshoot); 27300Sstevel@tonic-gate p = &pcf[pcf_index]; 27310Sstevel@tonic-gate p->pcf_touch = 1; 27320Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 27330Sstevel@tonic-gate if (p->pcf_block) { 27340Sstevel@tonic-gate p->pcf_reserve += overshoot; 27350Sstevel@tonic-gate } else { 27360Sstevel@tonic-gate p->pcf_count += overshoot; 27370Sstevel@tonic-gate if (p->pcf_wait) { 27380Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 27390Sstevel@tonic-gate if (freemem_wait) { 27400Sstevel@tonic-gate cv_signal(&freemem_cv); 27410Sstevel@tonic-gate p->pcf_wait--; 27420Sstevel@tonic-gate } else { 27430Sstevel@tonic-gate p->pcf_wait = 0; 27440Sstevel@tonic-gate } 27450Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 27460Sstevel@tonic-gate } 27470Sstevel@tonic-gate } 27480Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 27490Sstevel@tonic-gate /* freemem is approximate, so this test OK */ 27500Sstevel@tonic-gate if (!p->pcf_block) 27510Sstevel@tonic-gate freemem += overshoot; 27520Sstevel@tonic-gate } 27530Sstevel@tonic-gate } 27540Sstevel@tonic-gate 27550Sstevel@tonic-gate return (plist); 27560Sstevel@tonic-gate } 27570Sstevel@tonic-gate 27580Sstevel@tonic-gate /* 27590Sstevel@tonic-gate * One or more constituent pages of this large page has been marked 27600Sstevel@tonic-gate * toxic. Simply demote the large page to PAGESIZE pages and let 27610Sstevel@tonic-gate * page_free() handle it. This routine should only be called by 27620Sstevel@tonic-gate * large page free routines (page_free_pages() and page_destroy_pages(). 27630Sstevel@tonic-gate * All pages are locked SE_EXCL and have already been marked free. 27640Sstevel@tonic-gate */ 27650Sstevel@tonic-gate static void 27660Sstevel@tonic-gate page_free_toxic_pages(page_t *rootpp) 27670Sstevel@tonic-gate { 27680Sstevel@tonic-gate page_t *tpp; 27690Sstevel@tonic-gate pgcnt_t i, pgcnt = page_get_pagecnt(rootpp->p_szc); 27700Sstevel@tonic-gate uint_t szc = rootpp->p_szc; 27710Sstevel@tonic-gate 27720Sstevel@tonic-gate for (i = 0, tpp = rootpp; i < pgcnt; i++, tpp = tpp->p_next) { 27730Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 27740Sstevel@tonic-gate ASSERT((PAGE_EXCL(tpp) && 27750Sstevel@tonic-gate !page_iolock_assert(tpp)) || panicstr); 27760Sstevel@tonic-gate tpp->p_szc = 0; 27770Sstevel@tonic-gate } 27780Sstevel@tonic-gate 27790Sstevel@tonic-gate while (rootpp != NULL) { 27800Sstevel@tonic-gate tpp = rootpp; 27810Sstevel@tonic-gate page_sub(&rootpp, tpp); 27820Sstevel@tonic-gate ASSERT(PP_ISFREE(tpp)); 27830Sstevel@tonic-gate PP_CLRFREE(tpp); 27840Sstevel@tonic-gate page_free(tpp, 1); 27850Sstevel@tonic-gate } 27860Sstevel@tonic-gate } 27870Sstevel@tonic-gate 27880Sstevel@tonic-gate /* 27890Sstevel@tonic-gate * Put page on the "free" list. 27900Sstevel@tonic-gate * The free list is really two lists maintained by 27910Sstevel@tonic-gate * the PSM of whatever machine we happen to be on. 27920Sstevel@tonic-gate */ 27930Sstevel@tonic-gate void 27940Sstevel@tonic-gate page_free(page_t *pp, int dontneed) 27950Sstevel@tonic-gate { 27960Sstevel@tonic-gate struct pcf *p; 27970Sstevel@tonic-gate uint_t pcf_index; 27980Sstevel@tonic-gate 27990Sstevel@tonic-gate ASSERT((PAGE_EXCL(pp) && 28000Sstevel@tonic-gate !page_iolock_assert(pp)) || panicstr); 28010Sstevel@tonic-gate 28020Sstevel@tonic-gate if (page_deteriorating(pp)) { 28030Sstevel@tonic-gate volatile int i = 0; 28040Sstevel@tonic-gate char *kaddr; 28050Sstevel@tonic-gate volatile int rb, wb; 28060Sstevel@tonic-gate uint64_t pa; 28070Sstevel@tonic-gate volatile int ue = 0; 28080Sstevel@tonic-gate on_trap_data_t otd; 28090Sstevel@tonic-gate 28100Sstevel@tonic-gate if (pp->p_vnode != NULL) { 28110Sstevel@tonic-gate /* 28120Sstevel@tonic-gate * Let page_destroy() do its bean counting and 28130Sstevel@tonic-gate * hash out the page; it will then call back 28140Sstevel@tonic-gate * into page_free() with pp->p_vnode == NULL. 28150Sstevel@tonic-gate */ 28160Sstevel@tonic-gate page_destroy(pp, 0); 28170Sstevel@tonic-gate return; 28180Sstevel@tonic-gate } 28190Sstevel@tonic-gate 28200Sstevel@tonic-gate if (page_isfailing(pp)) { 28210Sstevel@tonic-gate /* 28220Sstevel@tonic-gate * If we have already exceeded the limit for 28230Sstevel@tonic-gate * pages retired, we will treat this page as 28240Sstevel@tonic-gate * 'toxic' rather than failing. That will ensure 28250Sstevel@tonic-gate * that the page is at least cleaned, and if 28260Sstevel@tonic-gate * a UE is detected, the page will be retired 28270Sstevel@tonic-gate * anyway. 28280Sstevel@tonic-gate */ 28290Sstevel@tonic-gate if (pages_retired_limit_exceeded()) { 28300Sstevel@tonic-gate /* 28310Sstevel@tonic-gate * clear the flag and reset to toxic 28320Sstevel@tonic-gate */ 28330Sstevel@tonic-gate page_clrtoxic(pp); 28340Sstevel@tonic-gate page_settoxic(pp, PAGE_IS_TOXIC); 28350Sstevel@tonic-gate } else { 28360Sstevel@tonic-gate pa = ptob((uint64_t)page_pptonum(pp)); 28370Sstevel@tonic-gate if (page_retire_messages) { 28380Sstevel@tonic-gate cmn_err(CE_NOTE, "Page 0x%08x.%08x " 28390Sstevel@tonic-gate "removed from service", 28400Sstevel@tonic-gate (uint32_t)(pa >> 32), (uint32_t)pa); 28410Sstevel@tonic-gate } 28420Sstevel@tonic-gate goto page_failed; 28430Sstevel@tonic-gate } 28440Sstevel@tonic-gate } 28450Sstevel@tonic-gate 28460Sstevel@tonic-gate pagescrub(pp, 0, PAGESIZE); 28470Sstevel@tonic-gate 28480Sstevel@tonic-gate /* 28490Sstevel@tonic-gate * We want to determine whether the error that occurred on 28500Sstevel@tonic-gate * this page is transient or persistent, so we get a mapping 28510Sstevel@tonic-gate * to the page and try every possible bit pattern to compare 28520Sstevel@tonic-gate * what we write with what we read back. A smaller number 28530Sstevel@tonic-gate * of bit patterns might suffice, but there's no point in 28540Sstevel@tonic-gate * getting fancy. If this is the hot path on your system, 28550Sstevel@tonic-gate * you've got bigger problems. 28560Sstevel@tonic-gate */ 28570Sstevel@tonic-gate kaddr = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 28580Sstevel@tonic-gate for (wb = 0xff; wb >= 0; wb--) { 28590Sstevel@tonic-gate if (on_trap(&otd, OT_DATA_EC)) { 28600Sstevel@tonic-gate pa = ptob((uint64_t)page_pptonum(pp)) + i; 28610Sstevel@tonic-gate page_settoxic(pp, PAGE_IS_FAILING); 28620Sstevel@tonic-gate 28630Sstevel@tonic-gate if (page_retire_messages) { 28640Sstevel@tonic-gate cmn_err(CE_WARN, "Uncorrectable Error " 28650Sstevel@tonic-gate "occurred at PA 0x%08x.%08x while " 28660Sstevel@tonic-gate "attempting to clear previously " 28670Sstevel@tonic-gate "reported error; page removed from " 28680Sstevel@tonic-gate "service", (uint32_t)(pa >> 32), 28690Sstevel@tonic-gate (uint32_t)pa); 28700Sstevel@tonic-gate } 28710Sstevel@tonic-gate 28720Sstevel@tonic-gate ue++; 28730Sstevel@tonic-gate break; 28740Sstevel@tonic-gate } 28750Sstevel@tonic-gate 28760Sstevel@tonic-gate /* 28770Sstevel@tonic-gate * Write out the bit pattern, flush it to memory, and 28780Sstevel@tonic-gate * read it back while under on_trap() protection. 28790Sstevel@tonic-gate */ 28800Sstevel@tonic-gate for (i = 0; i < PAGESIZE; i++) 28810Sstevel@tonic-gate kaddr[i] = wb; 28820Sstevel@tonic-gate 28830Sstevel@tonic-gate sync_data_memory(kaddr, PAGESIZE); 28840Sstevel@tonic-gate 28850Sstevel@tonic-gate for (i = 0; i < PAGESIZE; i++) { 28860Sstevel@tonic-gate if ((rb = (uchar_t)kaddr[i]) != wb) { 28870Sstevel@tonic-gate page_settoxic(pp, PAGE_IS_FAILING); 28880Sstevel@tonic-gate goto out; 28890Sstevel@tonic-gate } 28900Sstevel@tonic-gate } 28910Sstevel@tonic-gate } 28920Sstevel@tonic-gate out: 28930Sstevel@tonic-gate no_trap(); 28940Sstevel@tonic-gate ppmapout(kaddr); 28950Sstevel@tonic-gate 28960Sstevel@tonic-gate if (wb >= 0 && !ue) { 28970Sstevel@tonic-gate pa = ptob((uint64_t)page_pptonum(pp)) + i; 28980Sstevel@tonic-gate if (page_retire_messages) { 28990Sstevel@tonic-gate cmn_err(CE_WARN, "Data Mismatch occurred at PA " 29000Sstevel@tonic-gate "0x%08x.%08x [ 0x%x != 0x%x ] while " 29010Sstevel@tonic-gate "attempting to clear previously reported " 29020Sstevel@tonic-gate "error; page removed from service", 29030Sstevel@tonic-gate (uint32_t)(pa >> 32), (uint32_t)pa, rb, wb); 29040Sstevel@tonic-gate } 29050Sstevel@tonic-gate } 29060Sstevel@tonic-gate page_failed: 29070Sstevel@tonic-gate /* 29080Sstevel@tonic-gate * DR operations change the association between a page_t 29090Sstevel@tonic-gate * and the physical page it represents. Check if the 29100Sstevel@tonic-gate * page is still bad. If it is, then retire it. 29110Sstevel@tonic-gate */ 29120Sstevel@tonic-gate if (page_isfaulty(pp) && page_isfailing(pp)) { 29130Sstevel@tonic-gate /* 29140Sstevel@tonic-gate * In the future, it might be useful to have a platform 29150Sstevel@tonic-gate * callback here to tell the hardware to fence off this 29160Sstevel@tonic-gate * page during the next reboot. 29170Sstevel@tonic-gate * 29180Sstevel@tonic-gate * We move the page to the retired_vnode here 29190Sstevel@tonic-gate */ 29200Sstevel@tonic-gate (void) page_hashin(pp, &retired_ppages, 29210Sstevel@tonic-gate (u_offset_t)ptob((uint64_t)page_pptonum(pp)), NULL); 29220Sstevel@tonic-gate mutex_enter(&freemem_lock); 29230Sstevel@tonic-gate availrmem--; 29240Sstevel@tonic-gate mutex_exit(&freemem_lock); 29250Sstevel@tonic-gate page_retired(pp); 29260Sstevel@tonic-gate page_downgrade(pp); 29270Sstevel@tonic-gate 29280Sstevel@tonic-gate /* 29290Sstevel@tonic-gate * If DR raced with the above page retirement code, 29300Sstevel@tonic-gate * we might have retired a good page. If so, unretire 29310Sstevel@tonic-gate * the page. 29320Sstevel@tonic-gate */ 29330Sstevel@tonic-gate if (!page_isfaulty(pp)) 29340Sstevel@tonic-gate page_unretire_pages(); 29350Sstevel@tonic-gate return; 29360Sstevel@tonic-gate } 29370Sstevel@tonic-gate 29380Sstevel@tonic-gate pa = ptob((uint64_t)page_pptonum(pp)); 29390Sstevel@tonic-gate 29400Sstevel@tonic-gate if (page_retire_messages) { 29410Sstevel@tonic-gate cmn_err(CE_NOTE, "Previously reported error on page " 29420Sstevel@tonic-gate "0x%08x.%08x cleared", (uint32_t)(pa >> 32), 29430Sstevel@tonic-gate (uint32_t)pa); 29440Sstevel@tonic-gate } 29450Sstevel@tonic-gate 29460Sstevel@tonic-gate page_clrtoxic(pp); 29470Sstevel@tonic-gate } 29480Sstevel@tonic-gate 29490Sstevel@tonic-gate if (PP_ISFREE(pp)) { 29500Sstevel@tonic-gate panic("page_free: page %p is free", (void *)pp); 29510Sstevel@tonic-gate } 29520Sstevel@tonic-gate 29530Sstevel@tonic-gate if (pp->p_szc != 0) { 29540Sstevel@tonic-gate if (pp->p_vnode == NULL || IS_SWAPFSVP(pp->p_vnode) || 29550Sstevel@tonic-gate pp->p_vnode == &kvp) { 29560Sstevel@tonic-gate panic("page_free: anon or kernel " 29570Sstevel@tonic-gate "or no vnode large page %p", (void *)pp); 29580Sstevel@tonic-gate } 29590Sstevel@tonic-gate page_demote_vp_pages(pp); 29600Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 29610Sstevel@tonic-gate } 29620Sstevel@tonic-gate 29630Sstevel@tonic-gate /* 29640Sstevel@tonic-gate * The page_struct_lock need not be acquired to examine these 29650Sstevel@tonic-gate * fields since the page has an "exclusive" lock. 29660Sstevel@tonic-gate */ 29670Sstevel@tonic-gate if (hat_page_is_mapped(pp) || pp->p_lckcnt != 0 || pp->p_cowcnt != 0) { 29680Sstevel@tonic-gate panic("page_free pp=%p, pfn=%lx, lckcnt=%d, cowcnt=%d", 29690Sstevel@tonic-gate pp, page_pptonum(pp), pp->p_lckcnt, pp->p_cowcnt); 29700Sstevel@tonic-gate /*NOTREACHED*/ 29710Sstevel@tonic-gate } 29720Sstevel@tonic-gate 29730Sstevel@tonic-gate ASSERT(!hat_page_getshare(pp)); 29740Sstevel@tonic-gate 29750Sstevel@tonic-gate PP_SETFREE(pp); 29760Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL || !IS_VMODSORT(pp->p_vnode) || 29770Sstevel@tonic-gate !hat_ismod(pp)); 29780Sstevel@tonic-gate page_clr_all_props(pp); 29790Sstevel@tonic-gate ASSERT(!hat_page_getshare(pp)); 29800Sstevel@tonic-gate 29810Sstevel@tonic-gate /* 29820Sstevel@tonic-gate * Now we add the page to the head of the free list. 29830Sstevel@tonic-gate * But if this page is associated with a paged vnode 29840Sstevel@tonic-gate * then we adjust the head forward so that the page is 29850Sstevel@tonic-gate * effectively at the end of the list. 29860Sstevel@tonic-gate */ 29870Sstevel@tonic-gate if (pp->p_vnode == NULL) { 29880Sstevel@tonic-gate /* 29890Sstevel@tonic-gate * Page has no identity, put it on the free list. 29900Sstevel@tonic-gate */ 29910Sstevel@tonic-gate PP_SETAGED(pp); 29920Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1; 29930Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 29940Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_free); 29950Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_FREE, 29960Sstevel@tonic-gate "page_free_free:pp %p", pp); 29970Sstevel@tonic-gate } else { 29980Sstevel@tonic-gate PP_CLRAGED(pp); 29990Sstevel@tonic-gate 30000Sstevel@tonic-gate if (!dontneed || nopageage) { 30010Sstevel@tonic-gate /* move it to the tail of the list */ 30020Sstevel@tonic-gate page_list_add(pp, PG_CACHE_LIST | PG_LIST_TAIL); 30030Sstevel@tonic-gate 30040Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_cache); 30050Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_CACHE_TAIL, 30060Sstevel@tonic-gate "page_free_cache_tail:pp %p", pp); 30070Sstevel@tonic-gate } else { 30080Sstevel@tonic-gate page_list_add(pp, PG_CACHE_LIST | PG_LIST_HEAD); 30090Sstevel@tonic-gate 30100Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_dontneed); 30110Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_CACHE_HEAD, 30120Sstevel@tonic-gate "page_free_cache_head:pp %p", pp); 30130Sstevel@tonic-gate } 30140Sstevel@tonic-gate } 30150Sstevel@tonic-gate page_unlock(pp); 30160Sstevel@tonic-gate 30170Sstevel@tonic-gate /* 30180Sstevel@tonic-gate * Now do the `freemem' accounting. 30190Sstevel@tonic-gate */ 30200Sstevel@tonic-gate pcf_index = PCF_INDEX(); 30210Sstevel@tonic-gate p = &pcf[pcf_index]; 30220Sstevel@tonic-gate p->pcf_touch = 1; 30230Sstevel@tonic-gate 30240Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 30250Sstevel@tonic-gate if (p->pcf_block) { 30260Sstevel@tonic-gate p->pcf_reserve += 1; 30270Sstevel@tonic-gate } else { 30280Sstevel@tonic-gate p->pcf_count += 1; 30290Sstevel@tonic-gate if (p->pcf_wait) { 30300Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 30310Sstevel@tonic-gate /* 30320Sstevel@tonic-gate * Check to see if some other thread 30330Sstevel@tonic-gate * is actually waiting. Another bucket 30340Sstevel@tonic-gate * may have woken it up by now. If there 30350Sstevel@tonic-gate * are no waiters, then set our pcf_wait 30360Sstevel@tonic-gate * count to zero to avoid coming in here 30370Sstevel@tonic-gate * next time. Also, since only one page 30380Sstevel@tonic-gate * was put on the free list, just wake 30390Sstevel@tonic-gate * up one waiter. 30400Sstevel@tonic-gate */ 30410Sstevel@tonic-gate if (freemem_wait) { 30420Sstevel@tonic-gate cv_signal(&freemem_cv); 30430Sstevel@tonic-gate p->pcf_wait--; 30440Sstevel@tonic-gate } else { 30450Sstevel@tonic-gate p->pcf_wait = 0; 30460Sstevel@tonic-gate } 30470Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 30480Sstevel@tonic-gate } 30490Sstevel@tonic-gate } 30500Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 30510Sstevel@tonic-gate 30520Sstevel@tonic-gate /* freemem is approximate, so this test OK */ 30530Sstevel@tonic-gate if (!p->pcf_block) 30540Sstevel@tonic-gate freemem += 1; 30550Sstevel@tonic-gate } 30560Sstevel@tonic-gate 30570Sstevel@tonic-gate /* 30580Sstevel@tonic-gate * Put page on the "free" list during intial startup. 30590Sstevel@tonic-gate * This happens during initial single threaded execution. 30600Sstevel@tonic-gate */ 30610Sstevel@tonic-gate void 30620Sstevel@tonic-gate page_free_at_startup(page_t *pp) 30630Sstevel@tonic-gate { 30640Sstevel@tonic-gate struct pcf *p; 30650Sstevel@tonic-gate uint_t pcf_index; 30660Sstevel@tonic-gate 30670Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_HEAD | PG_LIST_ISINIT); 30680Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_free); 30690Sstevel@tonic-gate 30700Sstevel@tonic-gate /* 30710Sstevel@tonic-gate * Now do the `freemem' accounting. 30720Sstevel@tonic-gate */ 30730Sstevel@tonic-gate pcf_index = PCF_INDEX(); 30740Sstevel@tonic-gate p = &pcf[pcf_index]; 30750Sstevel@tonic-gate p->pcf_touch = 1; 30760Sstevel@tonic-gate 30770Sstevel@tonic-gate ASSERT(p->pcf_block == 0); 30780Sstevel@tonic-gate ASSERT(p->pcf_wait == 0); 30790Sstevel@tonic-gate p->pcf_count += 1; 30800Sstevel@tonic-gate 30810Sstevel@tonic-gate /* freemem is approximate, so this is OK */ 30820Sstevel@tonic-gate freemem += 1; 30830Sstevel@tonic-gate } 30840Sstevel@tonic-gate 30850Sstevel@tonic-gate void 30860Sstevel@tonic-gate page_free_pages(page_t *pp) 30870Sstevel@tonic-gate { 30880Sstevel@tonic-gate page_t *tpp, *rootpp = NULL; 30890Sstevel@tonic-gate pgcnt_t pgcnt = page_get_pagecnt(pp->p_szc); 30900Sstevel@tonic-gate pgcnt_t i; 30910Sstevel@tonic-gate uint_t szc = pp->p_szc; 30920Sstevel@tonic-gate int toxic = 0; 30930Sstevel@tonic-gate 30940Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_pages); 30950Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_FREE, 30960Sstevel@tonic-gate "page_free_free:pp %p", pp); 30970Sstevel@tonic-gate 30980Sstevel@tonic-gate ASSERT(pp->p_szc != 0 && pp->p_szc < page_num_pagesizes()); 30990Sstevel@tonic-gate if ((page_pptonum(pp) & (pgcnt - 1)) != 0) { 31000Sstevel@tonic-gate panic("page_free_pages: not root page %p", (void *)pp); 31010Sstevel@tonic-gate /*NOTREACHED*/ 31020Sstevel@tonic-gate } 31030Sstevel@tonic-gate 31040Sstevel@tonic-gate for (i = 0, tpp = pp; i < pgcnt; i++, tpp = page_next(tpp)) { 31050Sstevel@tonic-gate ASSERT((PAGE_EXCL(tpp) && 31060Sstevel@tonic-gate !page_iolock_assert(tpp)) || panicstr); 31070Sstevel@tonic-gate if (PP_ISFREE(tpp)) { 31080Sstevel@tonic-gate panic("page_free_pages: page %p is free", (void *)tpp); 31090Sstevel@tonic-gate /*NOTREACHED*/ 31100Sstevel@tonic-gate } 31110Sstevel@tonic-gate if (hat_page_is_mapped(tpp) || tpp->p_lckcnt != 0 || 31120Sstevel@tonic-gate tpp->p_cowcnt != 0) { 31130Sstevel@tonic-gate panic("page_free_pages %p", (void *)tpp); 31140Sstevel@tonic-gate /*NOTREACHED*/ 31150Sstevel@tonic-gate } 31160Sstevel@tonic-gate 31170Sstevel@tonic-gate ASSERT(!hat_page_getshare(tpp)); 31180Sstevel@tonic-gate ASSERT(tpp->p_vnode == NULL); 31190Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 31200Sstevel@tonic-gate 31210Sstevel@tonic-gate if (page_deteriorating(tpp)) 31220Sstevel@tonic-gate toxic = 1; 31230Sstevel@tonic-gate 31240Sstevel@tonic-gate PP_SETFREE(tpp); 31250Sstevel@tonic-gate page_clr_all_props(tpp); 31260Sstevel@tonic-gate PP_SETAGED(tpp); 31270Sstevel@tonic-gate tpp->p_offset = (u_offset_t)-1; 31280Sstevel@tonic-gate ASSERT(tpp->p_next == tpp); 31290Sstevel@tonic-gate ASSERT(tpp->p_prev == tpp); 31300Sstevel@tonic-gate page_list_concat(&rootpp, &tpp); 31310Sstevel@tonic-gate } 31320Sstevel@tonic-gate ASSERT(rootpp == pp); 31330Sstevel@tonic-gate 31340Sstevel@tonic-gate if (toxic) { 31350Sstevel@tonic-gate page_free_toxic_pages(rootpp); 31360Sstevel@tonic-gate return; 31370Sstevel@tonic-gate } 31380Sstevel@tonic-gate page_list_add_pages(rootpp, 0); 31390Sstevel@tonic-gate page_create_putback(pgcnt); 31400Sstevel@tonic-gate } 31410Sstevel@tonic-gate 31420Sstevel@tonic-gate int free_pages = 1; 31430Sstevel@tonic-gate 31440Sstevel@tonic-gate /* 31450Sstevel@tonic-gate * This routine attempts to return pages to the cachelist via page_release(). 31460Sstevel@tonic-gate * It does not *have* to be successful in all cases, since the pageout scanner 31470Sstevel@tonic-gate * will catch any pages it misses. It does need to be fast and not introduce 31480Sstevel@tonic-gate * too much overhead. 31490Sstevel@tonic-gate * 31500Sstevel@tonic-gate * If a page isn't found on the unlocked sweep of the page_hash bucket, we 31510Sstevel@tonic-gate * don't lock and retry. This is ok, since the page scanner will eventually 31520Sstevel@tonic-gate * find any page we miss in free_vp_pages(). 31530Sstevel@tonic-gate */ 31540Sstevel@tonic-gate void 31550Sstevel@tonic-gate free_vp_pages(vnode_t *vp, u_offset_t off, size_t len) 31560Sstevel@tonic-gate { 31570Sstevel@tonic-gate page_t *pp; 31580Sstevel@tonic-gate u_offset_t eoff; 31590Sstevel@tonic-gate extern int swap_in_range(vnode_t *, u_offset_t, size_t); 31600Sstevel@tonic-gate 31610Sstevel@tonic-gate eoff = off + len; 31620Sstevel@tonic-gate 31630Sstevel@tonic-gate if (free_pages == 0) 31640Sstevel@tonic-gate return; 31650Sstevel@tonic-gate if (swap_in_range(vp, off, len)) 31660Sstevel@tonic-gate return; 31670Sstevel@tonic-gate 31680Sstevel@tonic-gate for (; off < eoff; off += PAGESIZE) { 31690Sstevel@tonic-gate 31700Sstevel@tonic-gate /* 31710Sstevel@tonic-gate * find the page using a fast, but inexact search. It'll be OK 31720Sstevel@tonic-gate * if a few pages slip through the cracks here. 31730Sstevel@tonic-gate */ 31740Sstevel@tonic-gate pp = page_exists(vp, off); 31750Sstevel@tonic-gate 31760Sstevel@tonic-gate /* 31770Sstevel@tonic-gate * If we didn't find the page (it may not exist), the page 31780Sstevel@tonic-gate * is free, looks still in use (shared), or we can't lock it, 31790Sstevel@tonic-gate * just give up. 31800Sstevel@tonic-gate */ 31810Sstevel@tonic-gate if (pp == NULL || 31820Sstevel@tonic-gate PP_ISFREE(pp) || 31830Sstevel@tonic-gate page_share_cnt(pp) > 0 || 31840Sstevel@tonic-gate !page_trylock(pp, SE_EXCL)) 31850Sstevel@tonic-gate continue; 31860Sstevel@tonic-gate 31870Sstevel@tonic-gate /* 31880Sstevel@tonic-gate * Once we have locked pp, verify that it's still the 31890Sstevel@tonic-gate * correct page and not already free 31900Sstevel@tonic-gate */ 31910Sstevel@tonic-gate ASSERT(PAGE_LOCKED_SE(pp, SE_EXCL)); 31920Sstevel@tonic-gate if (pp->p_vnode != vp || pp->p_offset != off || PP_ISFREE(pp)) { 31930Sstevel@tonic-gate page_unlock(pp); 31940Sstevel@tonic-gate continue; 31950Sstevel@tonic-gate } 31960Sstevel@tonic-gate 31970Sstevel@tonic-gate /* 31980Sstevel@tonic-gate * try to release the page... 31990Sstevel@tonic-gate */ 32000Sstevel@tonic-gate (void) page_release(pp, 1); 32010Sstevel@tonic-gate } 32020Sstevel@tonic-gate } 32030Sstevel@tonic-gate 32040Sstevel@tonic-gate /* 32050Sstevel@tonic-gate * Reclaim the given page from the free list. 32060Sstevel@tonic-gate * Returns 1 on success or 0 on failure. 32070Sstevel@tonic-gate * 32080Sstevel@tonic-gate * The page is unlocked if it can't be reclaimed (when freemem == 0). 32090Sstevel@tonic-gate * If `lock' is non-null, it will be dropped and re-acquired if 32100Sstevel@tonic-gate * the routine must wait while freemem is 0. 32110Sstevel@tonic-gate * 32120Sstevel@tonic-gate * As it turns out, boot_getpages() does this. It picks a page, 32130Sstevel@tonic-gate * based on where OBP mapped in some address, gets its pfn, searches 32140Sstevel@tonic-gate * the memsegs, locks the page, then pulls it off the free list! 32150Sstevel@tonic-gate */ 32160Sstevel@tonic-gate int 32170Sstevel@tonic-gate page_reclaim(page_t *pp, kmutex_t *lock) 32180Sstevel@tonic-gate { 32190Sstevel@tonic-gate struct pcf *p; 32200Sstevel@tonic-gate uint_t pcf_index; 32210Sstevel@tonic-gate struct cpu *cpup; 32220Sstevel@tonic-gate int enough; 32230Sstevel@tonic-gate uint_t i; 32240Sstevel@tonic-gate 32250Sstevel@tonic-gate ASSERT(lock != NULL ? MUTEX_HELD(lock) : 1); 32260Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp) && PP_ISFREE(pp)); 32270Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 32280Sstevel@tonic-gate 32290Sstevel@tonic-gate /* 32300Sstevel@tonic-gate * If `freemem' is 0, we cannot reclaim this page from the 32310Sstevel@tonic-gate * freelist, so release every lock we might hold: the page, 32320Sstevel@tonic-gate * and the `lock' before blocking. 32330Sstevel@tonic-gate * 32340Sstevel@tonic-gate * The only way `freemem' can become 0 while there are pages 32350Sstevel@tonic-gate * marked free (have their p->p_free bit set) is when the 32360Sstevel@tonic-gate * system is low on memory and doing a page_create(). In 32370Sstevel@tonic-gate * order to guarantee that once page_create() starts acquiring 32380Sstevel@tonic-gate * pages it will be able to get all that it needs since `freemem' 32390Sstevel@tonic-gate * was decreased by the requested amount. So, we need to release 32400Sstevel@tonic-gate * this page, and let page_create() have it. 32410Sstevel@tonic-gate * 32420Sstevel@tonic-gate * Since `freemem' being zero is not supposed to happen, just 32430Sstevel@tonic-gate * use the usual hash stuff as a starting point. If that bucket 32440Sstevel@tonic-gate * is empty, then assume the worst, and start at the beginning 32450Sstevel@tonic-gate * of the pcf array. If we always start at the beginning 32460Sstevel@tonic-gate * when acquiring more than one pcf lock, there won't be any 32470Sstevel@tonic-gate * deadlock problems. 32480Sstevel@tonic-gate */ 32490Sstevel@tonic-gate 32500Sstevel@tonic-gate /* TODO: Do we need to test kcage_freemem if PG_NORELOC(pp)? */ 32510Sstevel@tonic-gate 32520Sstevel@tonic-gate if (freemem <= throttlefree && !page_create_throttle(1l, 0)) { 32530Sstevel@tonic-gate pcf_acquire_all(); 32540Sstevel@tonic-gate goto page_reclaim_nomem; 32550Sstevel@tonic-gate } 32560Sstevel@tonic-gate 32570Sstevel@tonic-gate enough = 0; 32580Sstevel@tonic-gate pcf_index = PCF_INDEX(); 32590Sstevel@tonic-gate p = &pcf[pcf_index]; 32600Sstevel@tonic-gate p->pcf_touch = 1; 32610Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 32620Sstevel@tonic-gate if (p->pcf_count >= 1) { 32630Sstevel@tonic-gate enough = 1; 32640Sstevel@tonic-gate p->pcf_count--; 32650Sstevel@tonic-gate } 32660Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 32670Sstevel@tonic-gate 32680Sstevel@tonic-gate if (!enough) { 32690Sstevel@tonic-gate VM_STAT_ADD(page_reclaim_zero); 32700Sstevel@tonic-gate /* 32710Sstevel@tonic-gate * Check again. Its possible that some other thread 32720Sstevel@tonic-gate * could have been right behind us, and added one 32730Sstevel@tonic-gate * to a list somewhere. Acquire each of the pcf locks 32740Sstevel@tonic-gate * until we find a page. 32750Sstevel@tonic-gate */ 32760Sstevel@tonic-gate p = pcf; 32770Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 32780Sstevel@tonic-gate p->pcf_touch = 1; 32790Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 32800Sstevel@tonic-gate if (p->pcf_count >= 1) { 32810Sstevel@tonic-gate p->pcf_count -= 1; 32820Sstevel@tonic-gate enough = 1; 32830Sstevel@tonic-gate break; 32840Sstevel@tonic-gate } 32850Sstevel@tonic-gate p++; 32860Sstevel@tonic-gate } 32870Sstevel@tonic-gate 32880Sstevel@tonic-gate if (!enough) { 32890Sstevel@tonic-gate page_reclaim_nomem: 32900Sstevel@tonic-gate /* 32910Sstevel@tonic-gate * We really can't have page `pp'. 32920Sstevel@tonic-gate * Time for the no-memory dance with 32930Sstevel@tonic-gate * page_free(). This is just like 32940Sstevel@tonic-gate * page_create_wait(). Plus the added 32950Sstevel@tonic-gate * attraction of releasing whatever mutex 32960Sstevel@tonic-gate * we held when we were called with in `lock'. 32970Sstevel@tonic-gate * Page_unlock() will wakeup any thread 32980Sstevel@tonic-gate * waiting around for this page. 32990Sstevel@tonic-gate */ 33000Sstevel@tonic-gate if (lock) { 33010Sstevel@tonic-gate VM_STAT_ADD(page_reclaim_zero_locked); 33020Sstevel@tonic-gate mutex_exit(lock); 33030Sstevel@tonic-gate } 33040Sstevel@tonic-gate page_unlock(pp); 33050Sstevel@tonic-gate 33060Sstevel@tonic-gate /* 33070Sstevel@tonic-gate * get this before we drop all the pcf locks. 33080Sstevel@tonic-gate */ 33090Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 33100Sstevel@tonic-gate 33110Sstevel@tonic-gate p = pcf; 33120Sstevel@tonic-gate for (i = 0; i < PCF_FANOUT; i++) { 33130Sstevel@tonic-gate p->pcf_wait++; 33140Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 33150Sstevel@tonic-gate p++; 33160Sstevel@tonic-gate } 33170Sstevel@tonic-gate 33180Sstevel@tonic-gate freemem_wait++; 33190Sstevel@tonic-gate cv_wait(&freemem_cv, &new_freemem_lock); 33200Sstevel@tonic-gate freemem_wait--; 33210Sstevel@tonic-gate 33220Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 33230Sstevel@tonic-gate 33240Sstevel@tonic-gate if (lock) { 33250Sstevel@tonic-gate mutex_enter(lock); 33260Sstevel@tonic-gate } 33270Sstevel@tonic-gate return (0); 33280Sstevel@tonic-gate } 33290Sstevel@tonic-gate 33300Sstevel@tonic-gate /* 33310Sstevel@tonic-gate * There was a page to be found. 33320Sstevel@tonic-gate * The pcf accounting has been done, 33330Sstevel@tonic-gate * though none of the pcf_wait flags have been set, 33340Sstevel@tonic-gate * drop the locks and continue on. 33350Sstevel@tonic-gate */ 33360Sstevel@tonic-gate while (p >= pcf) { 33370Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 33380Sstevel@tonic-gate p--; 33390Sstevel@tonic-gate } 33400Sstevel@tonic-gate } 33410Sstevel@tonic-gate 33420Sstevel@tonic-gate /* 33430Sstevel@tonic-gate * freemem is not protected by any lock. Thus, we cannot 33440Sstevel@tonic-gate * have any assertion containing freemem here. 33450Sstevel@tonic-gate */ 33460Sstevel@tonic-gate freemem -= 1; 33470Sstevel@tonic-gate 33480Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_reclaim); 33490Sstevel@tonic-gate if (PP_ISAGED(pp)) { 33500Sstevel@tonic-gate page_list_sub(pp, PG_FREE_LIST); 33510Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_FREE, 33520Sstevel@tonic-gate "page_reclaim_free:pp %p", pp); 33530Sstevel@tonic-gate } else { 33540Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST); 33550Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_CACHE, 33560Sstevel@tonic-gate "page_reclaim_cache:pp %p", pp); 33570Sstevel@tonic-gate } 33580Sstevel@tonic-gate 33590Sstevel@tonic-gate /* 33600Sstevel@tonic-gate * clear the p_free & p_age bits since this page is no longer 33610Sstevel@tonic-gate * on the free list. Notice that there was a brief time where 33620Sstevel@tonic-gate * a page is marked as free, but is not on the list. 33630Sstevel@tonic-gate * 33640Sstevel@tonic-gate * Set the reference bit to protect against immediate pageout. 33650Sstevel@tonic-gate */ 33660Sstevel@tonic-gate PP_CLRFREE(pp); 33670Sstevel@tonic-gate PP_CLRAGED(pp); 33680Sstevel@tonic-gate page_set_props(pp, P_REF); 33690Sstevel@tonic-gate 33700Sstevel@tonic-gate CPU_STATS_ENTER_K(); 33710Sstevel@tonic-gate cpup = CPU; /* get cpup now that CPU cannot change */ 33720Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgrec, 1); 33730Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgfrec, 1); 33740Sstevel@tonic-gate CPU_STATS_EXIT_K(); 33750Sstevel@tonic-gate 33760Sstevel@tonic-gate return (1); 33770Sstevel@tonic-gate } 33780Sstevel@tonic-gate 33790Sstevel@tonic-gate 33800Sstevel@tonic-gate 33810Sstevel@tonic-gate /* 33820Sstevel@tonic-gate * Destroy identity of the page and put it back on 33830Sstevel@tonic-gate * the page free list. Assumes that the caller has 33840Sstevel@tonic-gate * acquired the "exclusive" lock on the page. 33850Sstevel@tonic-gate */ 33860Sstevel@tonic-gate void 33870Sstevel@tonic-gate page_destroy(page_t *pp, int dontfree) 33880Sstevel@tonic-gate { 33890Sstevel@tonic-gate ASSERT((PAGE_EXCL(pp) && 33900Sstevel@tonic-gate !page_iolock_assert(pp)) || panicstr); 33910Sstevel@tonic-gate 33920Sstevel@tonic-gate if (pp->p_szc != 0) { 33930Sstevel@tonic-gate if (pp->p_vnode == NULL || IS_SWAPFSVP(pp->p_vnode) || 33940Sstevel@tonic-gate pp->p_vnode == &kvp) { 33950Sstevel@tonic-gate panic("page_destroy: anon or kernel or no vnode " 33960Sstevel@tonic-gate "large page %p", (void *)pp); 33970Sstevel@tonic-gate } 33980Sstevel@tonic-gate page_demote_vp_pages(pp); 33990Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 34000Sstevel@tonic-gate } 34010Sstevel@tonic-gate 34020Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_DESTROY, "page_destroy:pp %p", pp); 34030Sstevel@tonic-gate 34040Sstevel@tonic-gate /* 34050Sstevel@tonic-gate * Unload translations, if any, then hash out the 34060Sstevel@tonic-gate * page to erase its identity. 34070Sstevel@tonic-gate */ 34080Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 34090Sstevel@tonic-gate page_hashout(pp, NULL); 34100Sstevel@tonic-gate 34110Sstevel@tonic-gate if (!dontfree) { 34120Sstevel@tonic-gate /* 34130Sstevel@tonic-gate * Acquire the "freemem_lock" for availrmem. 34140Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt 34150Sstevel@tonic-gate * and cowcnt since the page has an "exclusive" lock. 34160Sstevel@tonic-gate */ 34170Sstevel@tonic-gate if ((pp->p_lckcnt != 0) || (pp->p_cowcnt != 0)) { 34180Sstevel@tonic-gate mutex_enter(&freemem_lock); 34190Sstevel@tonic-gate if (pp->p_lckcnt != 0) { 34200Sstevel@tonic-gate availrmem++; 34210Sstevel@tonic-gate pp->p_lckcnt = 0; 34220Sstevel@tonic-gate } 34230Sstevel@tonic-gate if (pp->p_cowcnt != 0) { 34240Sstevel@tonic-gate availrmem += pp->p_cowcnt; 34250Sstevel@tonic-gate pp->p_cowcnt = 0; 34260Sstevel@tonic-gate } 34270Sstevel@tonic-gate mutex_exit(&freemem_lock); 34280Sstevel@tonic-gate } 34290Sstevel@tonic-gate /* 34300Sstevel@tonic-gate * Put the page on the "free" list. 34310Sstevel@tonic-gate */ 34320Sstevel@tonic-gate page_free(pp, 0); 34330Sstevel@tonic-gate } 34340Sstevel@tonic-gate } 34350Sstevel@tonic-gate 34360Sstevel@tonic-gate void 34370Sstevel@tonic-gate page_destroy_pages(page_t *pp) 34380Sstevel@tonic-gate { 34390Sstevel@tonic-gate 34400Sstevel@tonic-gate page_t *tpp, *rootpp = NULL; 34410Sstevel@tonic-gate pgcnt_t pgcnt = page_get_pagecnt(pp->p_szc); 34420Sstevel@tonic-gate pgcnt_t i, pglcks = 0; 34430Sstevel@tonic-gate uint_t szc = pp->p_szc; 34440Sstevel@tonic-gate int toxic = 0; 34450Sstevel@tonic-gate 34460Sstevel@tonic-gate ASSERT(pp->p_szc != 0 && pp->p_szc < page_num_pagesizes()); 34470Sstevel@tonic-gate 34480Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_destroy_pages); 34490Sstevel@tonic-gate 34500Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_DESTROY, "page_destroy_pages:pp %p", pp); 34510Sstevel@tonic-gate 34520Sstevel@tonic-gate if ((page_pptonum(pp) & (pgcnt - 1)) != 0) { 34530Sstevel@tonic-gate panic("page_destroy_pages: not root page %p", (void *)pp); 34540Sstevel@tonic-gate /*NOTREACHED*/ 34550Sstevel@tonic-gate } 34560Sstevel@tonic-gate 34570Sstevel@tonic-gate for (i = 0, tpp = pp; i < pgcnt; i++, tpp = page_next(tpp)) { 34580Sstevel@tonic-gate ASSERT((PAGE_EXCL(tpp) && 34590Sstevel@tonic-gate !page_iolock_assert(tpp)) || panicstr); 34600Sstevel@tonic-gate (void) hat_pageunload(tpp, HAT_FORCE_PGUNLOAD); 34610Sstevel@tonic-gate page_hashout(tpp, NULL); 34620Sstevel@tonic-gate ASSERT(tpp->p_offset == (u_offset_t)-1); 34630Sstevel@tonic-gate if (tpp->p_lckcnt != 0) { 34640Sstevel@tonic-gate pglcks++; 34650Sstevel@tonic-gate tpp->p_lckcnt = 0; 34660Sstevel@tonic-gate } else if (tpp->p_cowcnt != 0) { 34670Sstevel@tonic-gate pglcks += tpp->p_cowcnt; 34680Sstevel@tonic-gate tpp->p_cowcnt = 0; 34690Sstevel@tonic-gate } 34700Sstevel@tonic-gate ASSERT(!hat_page_getshare(tpp)); 34710Sstevel@tonic-gate ASSERT(tpp->p_vnode == NULL); 34720Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 34730Sstevel@tonic-gate 34740Sstevel@tonic-gate if (page_deteriorating(tpp)) 34750Sstevel@tonic-gate toxic = 1; 34760Sstevel@tonic-gate 34770Sstevel@tonic-gate PP_SETFREE(tpp); 34780Sstevel@tonic-gate page_clr_all_props(tpp); 34790Sstevel@tonic-gate PP_SETAGED(tpp); 34800Sstevel@tonic-gate ASSERT(tpp->p_next == tpp); 34810Sstevel@tonic-gate ASSERT(tpp->p_prev == tpp); 34820Sstevel@tonic-gate page_list_concat(&rootpp, &tpp); 34830Sstevel@tonic-gate } 34840Sstevel@tonic-gate 34850Sstevel@tonic-gate ASSERT(rootpp == pp); 34860Sstevel@tonic-gate if (pglcks != 0) { 34870Sstevel@tonic-gate mutex_enter(&freemem_lock); 34880Sstevel@tonic-gate availrmem += pglcks; 34890Sstevel@tonic-gate mutex_exit(&freemem_lock); 34900Sstevel@tonic-gate } 34910Sstevel@tonic-gate 34920Sstevel@tonic-gate if (toxic) { 34930Sstevel@tonic-gate page_free_toxic_pages(rootpp); 34940Sstevel@tonic-gate return; 34950Sstevel@tonic-gate } 34960Sstevel@tonic-gate page_list_add_pages(rootpp, 0); 34970Sstevel@tonic-gate page_create_putback(pgcnt); 34980Sstevel@tonic-gate } 34990Sstevel@tonic-gate 35000Sstevel@tonic-gate /* 35010Sstevel@tonic-gate * Similar to page_destroy(), but destroys pages which are 35020Sstevel@tonic-gate * locked and known to be on the page free list. Since 35030Sstevel@tonic-gate * the page is known to be free and locked, no one can access 35040Sstevel@tonic-gate * it. 35050Sstevel@tonic-gate * 35060Sstevel@tonic-gate * Also, the number of free pages does not change. 35070Sstevel@tonic-gate */ 35080Sstevel@tonic-gate void 35090Sstevel@tonic-gate page_destroy_free(page_t *pp) 35100Sstevel@tonic-gate { 35110Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 35120Sstevel@tonic-gate ASSERT(PP_ISFREE(pp)); 35130Sstevel@tonic-gate ASSERT(pp->p_vnode); 35140Sstevel@tonic-gate ASSERT(hat_page_getattr(pp, P_MOD | P_REF | P_RO) == 0); 35150Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 35160Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0); 35170Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 35180Sstevel@tonic-gate 35190Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_destroy_free); 35200Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST); 35210Sstevel@tonic-gate 35220Sstevel@tonic-gate page_hashout(pp, NULL); 35230Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL); 35240Sstevel@tonic-gate ASSERT(pp->p_offset == (u_offset_t)-1); 35250Sstevel@tonic-gate ASSERT(pp->p_hash == NULL); 35260Sstevel@tonic-gate 35270Sstevel@tonic-gate PP_SETAGED(pp); 35280Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 35290Sstevel@tonic-gate page_unlock(pp); 35300Sstevel@tonic-gate 35310Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 35320Sstevel@tonic-gate if (freemem_wait) { 35330Sstevel@tonic-gate cv_signal(&freemem_cv); 35340Sstevel@tonic-gate } 35350Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 35360Sstevel@tonic-gate } 35370Sstevel@tonic-gate 35380Sstevel@tonic-gate /* 35390Sstevel@tonic-gate * Rename the page "opp" to have an identity specified 35400Sstevel@tonic-gate * by [vp, off]. If a page already exists with this name 35410Sstevel@tonic-gate * it is locked and destroyed. Note that the page's 35420Sstevel@tonic-gate * translations are not unloaded during the rename. 35430Sstevel@tonic-gate * 35440Sstevel@tonic-gate * This routine is used by the anon layer to "steal" the 35450Sstevel@tonic-gate * original page and is not unlike destroying a page and 35460Sstevel@tonic-gate * creating a new page using the same page frame. 35470Sstevel@tonic-gate * 35480Sstevel@tonic-gate * XXX -- Could deadlock if caller 1 tries to rename A to B while 35490Sstevel@tonic-gate * caller 2 tries to rename B to A. 35500Sstevel@tonic-gate */ 35510Sstevel@tonic-gate void 35520Sstevel@tonic-gate page_rename(page_t *opp, vnode_t *vp, u_offset_t off) 35530Sstevel@tonic-gate { 35540Sstevel@tonic-gate page_t *pp; 35550Sstevel@tonic-gate int olckcnt = 0; 35560Sstevel@tonic-gate int ocowcnt = 0; 35570Sstevel@tonic-gate kmutex_t *phm; 35580Sstevel@tonic-gate ulong_t index; 35590Sstevel@tonic-gate 35600Sstevel@tonic-gate ASSERT(PAGE_EXCL(opp) && !page_iolock_assert(opp)); 35610Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 35620Sstevel@tonic-gate ASSERT(PP_ISFREE(opp) == 0); 35630Sstevel@tonic-gate 35640Sstevel@tonic-gate VM_STAT_ADD(page_rename_count); 35650Sstevel@tonic-gate 35660Sstevel@tonic-gate TRACE_3(TR_FAC_VM, TR_PAGE_RENAME, 35670Sstevel@tonic-gate "page rename:pp %p vp %p off %llx", opp, vp, off); 35680Sstevel@tonic-gate 3569*63Saguzovsk /* 3570*63Saguzovsk * CacheFS may call page_rename for a large NFS page 3571*63Saguzovsk * when both CacheFS and NFS mount points are used 3572*63Saguzovsk * by applications. Demote this large page before 3573*63Saguzovsk * renaming it, to ensure that there are no "partial" 3574*63Saguzovsk * large pages left lying around. 3575*63Saguzovsk */ 3576*63Saguzovsk if (opp->p_szc != 0) { 3577*63Saguzovsk vnode_t *ovp = opp->p_vnode; 3578*63Saguzovsk ASSERT(ovp != NULL); 3579*63Saguzovsk ASSERT(!IS_SWAPFSVP(ovp)); 3580*63Saguzovsk ASSERT(ovp != &kvp); 3581*63Saguzovsk page_demote_vp_pages(opp); 3582*63Saguzovsk ASSERT(opp->p_szc == 0); 3583*63Saguzovsk } 3584*63Saguzovsk 35850Sstevel@tonic-gate page_hashout(opp, NULL); 35860Sstevel@tonic-gate PP_CLRAGED(opp); 35870Sstevel@tonic-gate 35880Sstevel@tonic-gate /* 35890Sstevel@tonic-gate * Acquire the appropriate page hash lock, since 35900Sstevel@tonic-gate * we're going to rename the page. 35910Sstevel@tonic-gate */ 35920Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 35930Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 35940Sstevel@tonic-gate mutex_enter(phm); 35950Sstevel@tonic-gate top: 35960Sstevel@tonic-gate /* 35970Sstevel@tonic-gate * Look for an existing page with this name and destroy it if found. 35980Sstevel@tonic-gate * By holding the page hash lock all the way to the page_hashin() 35990Sstevel@tonic-gate * call, we are assured that no page can be created with this 36000Sstevel@tonic-gate * identity. In the case when the phm lock is dropped to undo any 36010Sstevel@tonic-gate * hat layer mappings, the existing page is held with an "exclusive" 36020Sstevel@tonic-gate * lock, again preventing another page from being created with 36030Sstevel@tonic-gate * this identity. 36040Sstevel@tonic-gate */ 36050Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 36060Sstevel@tonic-gate if (pp != NULL) { 36070Sstevel@tonic-gate VM_STAT_ADD(page_rename_exists); 36080Sstevel@tonic-gate 36090Sstevel@tonic-gate /* 36100Sstevel@tonic-gate * As it turns out, this is one of only two places where 36110Sstevel@tonic-gate * page_lock() needs to hold the passed in lock in the 36120Sstevel@tonic-gate * successful case. In all of the others, the lock could 36130Sstevel@tonic-gate * be dropped as soon as the attempt is made to lock 36140Sstevel@tonic-gate * the page. It is tempting to add yet another arguement, 36150Sstevel@tonic-gate * PL_KEEP or PL_DROP, to let page_lock know what to do. 36160Sstevel@tonic-gate */ 36170Sstevel@tonic-gate if (!page_lock(pp, SE_EXCL, phm, P_RECLAIM)) { 36180Sstevel@tonic-gate /* 36190Sstevel@tonic-gate * Went to sleep because the page could not 36200Sstevel@tonic-gate * be locked. We were woken up when the page 36210Sstevel@tonic-gate * was unlocked, or when the page was destroyed. 36220Sstevel@tonic-gate * In either case, `phm' was dropped while we 36230Sstevel@tonic-gate * slept. Hence we should not just roar through 36240Sstevel@tonic-gate * this loop. 36250Sstevel@tonic-gate */ 36260Sstevel@tonic-gate goto top; 36270Sstevel@tonic-gate } 36280Sstevel@tonic-gate 3629*63Saguzovsk /* 3630*63Saguzovsk * If an existing page is a large page, then demote 3631*63Saguzovsk * it to ensure that no "partial" large pages are 3632*63Saguzovsk * "created" after page_rename. An existing page 3633*63Saguzovsk * can be a CacheFS page, and can't belong to swapfs. 3634*63Saguzovsk */ 36350Sstevel@tonic-gate if (hat_page_is_mapped(pp)) { 36360Sstevel@tonic-gate /* 36370Sstevel@tonic-gate * Unload translations. Since we hold the 36380Sstevel@tonic-gate * exclusive lock on this page, the page 36390Sstevel@tonic-gate * can not be changed while we drop phm. 36400Sstevel@tonic-gate * This is also not a lock protocol violation, 36410Sstevel@tonic-gate * but rather the proper way to do things. 36420Sstevel@tonic-gate */ 36430Sstevel@tonic-gate mutex_exit(phm); 36440Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 3645*63Saguzovsk if (pp->p_szc != 0) { 3646*63Saguzovsk ASSERT(!IS_SWAPFSVP(vp)); 3647*63Saguzovsk ASSERT(vp != &kvp); 3648*63Saguzovsk page_demote_vp_pages(pp); 3649*63Saguzovsk ASSERT(pp->p_szc == 0); 3650*63Saguzovsk } 3651*63Saguzovsk mutex_enter(phm); 3652*63Saguzovsk } else if (pp->p_szc != 0) { 3653*63Saguzovsk ASSERT(!IS_SWAPFSVP(vp)); 3654*63Saguzovsk ASSERT(vp != &kvp); 3655*63Saguzovsk mutex_exit(phm); 3656*63Saguzovsk page_demote_vp_pages(pp); 3657*63Saguzovsk ASSERT(pp->p_szc == 0); 36580Sstevel@tonic-gate mutex_enter(phm); 36590Sstevel@tonic-gate } 36600Sstevel@tonic-gate page_hashout(pp, phm); 36610Sstevel@tonic-gate } 36620Sstevel@tonic-gate /* 36630Sstevel@tonic-gate * Hash in the page with the new identity. 36640Sstevel@tonic-gate */ 36650Sstevel@tonic-gate if (!page_hashin(opp, vp, off, phm)) { 36660Sstevel@tonic-gate /* 36670Sstevel@tonic-gate * We were holding phm while we searched for [vp, off] 36680Sstevel@tonic-gate * and only dropped phm if we found and locked a page. 36690Sstevel@tonic-gate * If we can't create this page now, then some thing 36700Sstevel@tonic-gate * is really broken. 36710Sstevel@tonic-gate */ 36720Sstevel@tonic-gate panic("page_rename: Can't hash in page: %p", (void *)pp); 36730Sstevel@tonic-gate /*NOTREACHED*/ 36740Sstevel@tonic-gate } 36750Sstevel@tonic-gate 36760Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 36770Sstevel@tonic-gate mutex_exit(phm); 36780Sstevel@tonic-gate 36790Sstevel@tonic-gate /* 36800Sstevel@tonic-gate * Now that we have dropped phm, lets get around to finishing up 36810Sstevel@tonic-gate * with pp. 36820Sstevel@tonic-gate */ 36830Sstevel@tonic-gate if (pp != NULL) { 36840Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 36850Sstevel@tonic-gate /* for now large pages should not end up here */ 36860Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 36870Sstevel@tonic-gate /* 36880Sstevel@tonic-gate * Save the locks for transfer to the new page and then 36890Sstevel@tonic-gate * clear them so page_free doesn't think they're important. 36900Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt and 36910Sstevel@tonic-gate * cowcnt since the page has an "exclusive" lock. 36920Sstevel@tonic-gate */ 36930Sstevel@tonic-gate olckcnt = pp->p_lckcnt; 36940Sstevel@tonic-gate ocowcnt = pp->p_cowcnt; 36950Sstevel@tonic-gate pp->p_lckcnt = pp->p_cowcnt = 0; 36960Sstevel@tonic-gate 36970Sstevel@tonic-gate /* 36980Sstevel@tonic-gate * Put the page on the "free" list after we drop 36990Sstevel@tonic-gate * the lock. The less work under the lock the better. 37000Sstevel@tonic-gate */ 37010Sstevel@tonic-gate /*LINTED: constant in conditional context*/ 37020Sstevel@tonic-gate VN_DISPOSE(pp, B_FREE, 0, kcred); 37030Sstevel@tonic-gate } 37040Sstevel@tonic-gate 37050Sstevel@tonic-gate /* 37060Sstevel@tonic-gate * Transfer the lock count from the old page (if any). 37070Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt and 37080Sstevel@tonic-gate * cowcnt since the page has an "exclusive" lock. 37090Sstevel@tonic-gate */ 37100Sstevel@tonic-gate opp->p_lckcnt += olckcnt; 37110Sstevel@tonic-gate opp->p_cowcnt += ocowcnt; 37120Sstevel@tonic-gate } 37130Sstevel@tonic-gate 37140Sstevel@tonic-gate /* 37150Sstevel@tonic-gate * low level routine to add page `pp' to the hash and vp chains for [vp, offset] 37160Sstevel@tonic-gate * 37170Sstevel@tonic-gate * Pages are normally inserted at the start of a vnode's v_pages list. 37180Sstevel@tonic-gate * If the vnode is VMODSORT and the page is modified, it goes at the end. 37190Sstevel@tonic-gate * This can happen when a modified page is relocated for DR. 37200Sstevel@tonic-gate * 37210Sstevel@tonic-gate * Returns 1 on success and 0 on failure. 37220Sstevel@tonic-gate */ 37230Sstevel@tonic-gate static int 37240Sstevel@tonic-gate page_do_hashin(page_t *pp, vnode_t *vp, u_offset_t offset) 37250Sstevel@tonic-gate { 37260Sstevel@tonic-gate page_t **listp; 37270Sstevel@tonic-gate page_t *tp; 37280Sstevel@tonic-gate ulong_t index; 37290Sstevel@tonic-gate 37300Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 37310Sstevel@tonic-gate ASSERT(vp != NULL); 37320Sstevel@tonic-gate ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 37330Sstevel@tonic-gate 37340Sstevel@tonic-gate /* 37350Sstevel@tonic-gate * Be sure to set these up before the page is inserted on the hash 37360Sstevel@tonic-gate * list. As soon as the page is placed on the list some other 37370Sstevel@tonic-gate * thread might get confused and wonder how this page could 37380Sstevel@tonic-gate * possibly hash to this list. 37390Sstevel@tonic-gate */ 37400Sstevel@tonic-gate pp->p_vnode = vp; 37410Sstevel@tonic-gate pp->p_offset = offset; 37420Sstevel@tonic-gate 37430Sstevel@tonic-gate /* 37440Sstevel@tonic-gate * record if this page is on a swap vnode 37450Sstevel@tonic-gate */ 37460Sstevel@tonic-gate if ((vp->v_flag & VISSWAP) != 0) 37470Sstevel@tonic-gate PP_SETSWAP(pp); 37480Sstevel@tonic-gate 37490Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, offset); 37500Sstevel@tonic-gate ASSERT(MUTEX_HELD(PAGE_HASH_MUTEX(index))); 37510Sstevel@tonic-gate listp = &page_hash[index]; 37520Sstevel@tonic-gate 37530Sstevel@tonic-gate /* 37540Sstevel@tonic-gate * If this page is already hashed in, fail this attempt to add it. 37550Sstevel@tonic-gate */ 37560Sstevel@tonic-gate for (tp = *listp; tp != NULL; tp = tp->p_hash) { 37570Sstevel@tonic-gate if (tp->p_vnode == vp && tp->p_offset == offset) { 37580Sstevel@tonic-gate pp->p_vnode = NULL; 37590Sstevel@tonic-gate pp->p_offset = (u_offset_t)(-1); 37600Sstevel@tonic-gate return (0); 37610Sstevel@tonic-gate } 37620Sstevel@tonic-gate } 37630Sstevel@tonic-gate pp->p_hash = *listp; 37640Sstevel@tonic-gate *listp = pp; 37650Sstevel@tonic-gate 37660Sstevel@tonic-gate /* 37670Sstevel@tonic-gate * Add the page to the vnode's list of pages 37680Sstevel@tonic-gate */ 37690Sstevel@tonic-gate if (vp->v_pages != NULL && IS_VMODSORT(vp) && hat_ismod(pp)) 37700Sstevel@tonic-gate listp = &vp->v_pages->p_vpprev->p_vpnext; 37710Sstevel@tonic-gate else 37720Sstevel@tonic-gate listp = &vp->v_pages; 37730Sstevel@tonic-gate 37740Sstevel@tonic-gate page_vpadd(listp, pp); 37750Sstevel@tonic-gate 37760Sstevel@tonic-gate return (1); 37770Sstevel@tonic-gate } 37780Sstevel@tonic-gate 37790Sstevel@tonic-gate /* 37800Sstevel@tonic-gate * Add page `pp' to both the hash and vp chains for [vp, offset]. 37810Sstevel@tonic-gate * 37820Sstevel@tonic-gate * Returns 1 on success and 0 on failure. 37830Sstevel@tonic-gate * If hold is passed in, it is not dropped. 37840Sstevel@tonic-gate */ 37850Sstevel@tonic-gate int 37860Sstevel@tonic-gate page_hashin(page_t *pp, vnode_t *vp, u_offset_t offset, kmutex_t *hold) 37870Sstevel@tonic-gate { 37880Sstevel@tonic-gate kmutex_t *phm = NULL; 37890Sstevel@tonic-gate kmutex_t *vphm; 37900Sstevel@tonic-gate int rc; 37910Sstevel@tonic-gate 37920Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 37930Sstevel@tonic-gate 37940Sstevel@tonic-gate TRACE_3(TR_FAC_VM, TR_PAGE_HASHIN, 37950Sstevel@tonic-gate "page_hashin:pp %p vp %p offset %llx", 37960Sstevel@tonic-gate pp, vp, offset); 37970Sstevel@tonic-gate 37980Sstevel@tonic-gate VM_STAT_ADD(hashin_count); 37990Sstevel@tonic-gate 38000Sstevel@tonic-gate if (hold != NULL) 38010Sstevel@tonic-gate phm = hold; 38020Sstevel@tonic-gate else { 38030Sstevel@tonic-gate VM_STAT_ADD(hashin_not_held); 38040Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, offset)); 38050Sstevel@tonic-gate mutex_enter(phm); 38060Sstevel@tonic-gate } 38070Sstevel@tonic-gate 38080Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 38090Sstevel@tonic-gate mutex_enter(vphm); 38100Sstevel@tonic-gate rc = page_do_hashin(pp, vp, offset); 38110Sstevel@tonic-gate mutex_exit(vphm); 38120Sstevel@tonic-gate if (hold == NULL) 38130Sstevel@tonic-gate mutex_exit(phm); 38140Sstevel@tonic-gate if (rc == 0) 38150Sstevel@tonic-gate VM_STAT_ADD(hashin_already); 38160Sstevel@tonic-gate return (rc); 38170Sstevel@tonic-gate } 38180Sstevel@tonic-gate 38190Sstevel@tonic-gate /* 38200Sstevel@tonic-gate * Remove page ``pp'' from the hash and vp chains and remove vp association. 38210Sstevel@tonic-gate * All mutexes must be held 38220Sstevel@tonic-gate */ 38230Sstevel@tonic-gate static void 38240Sstevel@tonic-gate page_do_hashout(page_t *pp) 38250Sstevel@tonic-gate { 38260Sstevel@tonic-gate page_t **hpp; 38270Sstevel@tonic-gate page_t *hp; 38280Sstevel@tonic-gate vnode_t *vp = pp->p_vnode; 38290Sstevel@tonic-gate 38300Sstevel@tonic-gate ASSERT(vp != NULL); 38310Sstevel@tonic-gate ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 38320Sstevel@tonic-gate 38330Sstevel@tonic-gate /* 38340Sstevel@tonic-gate * First, take pp off of its hash chain. 38350Sstevel@tonic-gate */ 38360Sstevel@tonic-gate hpp = &page_hash[PAGE_HASH_FUNC(vp, pp->p_offset)]; 38370Sstevel@tonic-gate 38380Sstevel@tonic-gate for (;;) { 38390Sstevel@tonic-gate hp = *hpp; 38400Sstevel@tonic-gate if (hp == pp) 38410Sstevel@tonic-gate break; 38420Sstevel@tonic-gate if (hp == NULL) { 38430Sstevel@tonic-gate panic("page_do_hashout"); 38440Sstevel@tonic-gate /*NOTREACHED*/ 38450Sstevel@tonic-gate } 38460Sstevel@tonic-gate hpp = &hp->p_hash; 38470Sstevel@tonic-gate } 38480Sstevel@tonic-gate *hpp = pp->p_hash; 38490Sstevel@tonic-gate 38500Sstevel@tonic-gate /* 38510Sstevel@tonic-gate * Now remove it from its associated vnode. 38520Sstevel@tonic-gate */ 38530Sstevel@tonic-gate if (vp->v_pages) 38540Sstevel@tonic-gate page_vpsub(&vp->v_pages, pp); 38550Sstevel@tonic-gate 38560Sstevel@tonic-gate pp->p_hash = NULL; 38570Sstevel@tonic-gate page_clr_all_props(pp); 38580Sstevel@tonic-gate PP_CLRSWAP(pp); 38590Sstevel@tonic-gate pp->p_vnode = NULL; 38600Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1; 38610Sstevel@tonic-gate } 38620Sstevel@tonic-gate 38630Sstevel@tonic-gate /* 38640Sstevel@tonic-gate * Remove page ``pp'' from the hash and vp chains and remove vp association. 38650Sstevel@tonic-gate * 38660Sstevel@tonic-gate * When `phm' is non-NULL it contains the address of the mutex protecting the 38670Sstevel@tonic-gate * hash list pp is on. It is not dropped. 38680Sstevel@tonic-gate */ 38690Sstevel@tonic-gate void 38700Sstevel@tonic-gate page_hashout(page_t *pp, kmutex_t *phm) 38710Sstevel@tonic-gate { 38720Sstevel@tonic-gate vnode_t *vp; 38730Sstevel@tonic-gate ulong_t index; 38740Sstevel@tonic-gate kmutex_t *nphm; 38750Sstevel@tonic-gate kmutex_t *vphm; 38760Sstevel@tonic-gate kmutex_t *sep; 38770Sstevel@tonic-gate 38780Sstevel@tonic-gate ASSERT(phm != NULL ? MUTEX_HELD(phm) : 1); 38790Sstevel@tonic-gate ASSERT(pp->p_vnode != NULL); 38800Sstevel@tonic-gate ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 38810Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(pp->p_vnode))); 38820Sstevel@tonic-gate 38830Sstevel@tonic-gate vp = pp->p_vnode; 38840Sstevel@tonic-gate 38850Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_HASHOUT, 38860Sstevel@tonic-gate "page_hashout:pp %p vp %p", pp, vp); 38870Sstevel@tonic-gate 38880Sstevel@tonic-gate /* Kernel probe */ 38890Sstevel@tonic-gate TNF_PROBE_2(page_unmap, "vm pagefault", /* CSTYLED */, 38900Sstevel@tonic-gate tnf_opaque, vnode, vp, 38910Sstevel@tonic-gate tnf_offset, offset, pp->p_offset); 38920Sstevel@tonic-gate 38930Sstevel@tonic-gate /* 38940Sstevel@tonic-gate * 38950Sstevel@tonic-gate */ 38960Sstevel@tonic-gate VM_STAT_ADD(hashout_count); 38970Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, pp->p_offset); 38980Sstevel@tonic-gate if (phm == NULL) { 38990Sstevel@tonic-gate VM_STAT_ADD(hashout_not_held); 39000Sstevel@tonic-gate nphm = PAGE_HASH_MUTEX(index); 39010Sstevel@tonic-gate mutex_enter(nphm); 39020Sstevel@tonic-gate } 39030Sstevel@tonic-gate ASSERT(phm ? phm == PAGE_HASH_MUTEX(index) : 1); 39040Sstevel@tonic-gate 39050Sstevel@tonic-gate 39060Sstevel@tonic-gate /* 39070Sstevel@tonic-gate * grab page vnode mutex and remove it... 39080Sstevel@tonic-gate */ 39090Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 39100Sstevel@tonic-gate mutex_enter(vphm); 39110Sstevel@tonic-gate 39120Sstevel@tonic-gate page_do_hashout(pp); 39130Sstevel@tonic-gate 39140Sstevel@tonic-gate mutex_exit(vphm); 39150Sstevel@tonic-gate if (phm == NULL) 39160Sstevel@tonic-gate mutex_exit(nphm); 39170Sstevel@tonic-gate 39180Sstevel@tonic-gate /* 39190Sstevel@tonic-gate * If the page was retired, update the pages_retired 39200Sstevel@tonic-gate * total and clear the page flag 39210Sstevel@tonic-gate */ 39220Sstevel@tonic-gate if (page_isretired(pp)) { 39230Sstevel@tonic-gate retired_page_removed(pp); 39240Sstevel@tonic-gate } 39250Sstevel@tonic-gate 39260Sstevel@tonic-gate /* 39270Sstevel@tonic-gate * Wake up processes waiting for this page. The page's 39280Sstevel@tonic-gate * identity has been changed, and is probably not the 39290Sstevel@tonic-gate * desired page any longer. 39300Sstevel@tonic-gate */ 39310Sstevel@tonic-gate sep = page_se_mutex(pp); 39320Sstevel@tonic-gate mutex_enter(sep); 39330Sstevel@tonic-gate if (CV_HAS_WAITERS(&pp->p_cv)) 39340Sstevel@tonic-gate cv_broadcast(&pp->p_cv); 39350Sstevel@tonic-gate mutex_exit(sep); 39360Sstevel@tonic-gate } 39370Sstevel@tonic-gate 39380Sstevel@tonic-gate /* 39390Sstevel@tonic-gate * Add the page to the front of a linked list of pages 39400Sstevel@tonic-gate * using the p_next & p_prev pointers for the list. 39410Sstevel@tonic-gate * The caller is responsible for protecting the list pointers. 39420Sstevel@tonic-gate */ 39430Sstevel@tonic-gate void 39440Sstevel@tonic-gate page_add(page_t **ppp, page_t *pp) 39450Sstevel@tonic-gate { 39460Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp) || (PAGE_SHARED(pp) && page_iolock_assert(pp))); 39470Sstevel@tonic-gate 39480Sstevel@tonic-gate page_add_common(ppp, pp); 39490Sstevel@tonic-gate } 39500Sstevel@tonic-gate 39510Sstevel@tonic-gate 39520Sstevel@tonic-gate 39530Sstevel@tonic-gate /* 39540Sstevel@tonic-gate * Common code for page_add() and mach_page_add() 39550Sstevel@tonic-gate */ 39560Sstevel@tonic-gate void 39570Sstevel@tonic-gate page_add_common(page_t **ppp, page_t *pp) 39580Sstevel@tonic-gate { 39590Sstevel@tonic-gate if (*ppp == NULL) { 39600Sstevel@tonic-gate pp->p_next = pp->p_prev = pp; 39610Sstevel@tonic-gate } else { 39620Sstevel@tonic-gate pp->p_next = *ppp; 39630Sstevel@tonic-gate pp->p_prev = (*ppp)->p_prev; 39640Sstevel@tonic-gate (*ppp)->p_prev = pp; 39650Sstevel@tonic-gate pp->p_prev->p_next = pp; 39660Sstevel@tonic-gate } 39670Sstevel@tonic-gate *ppp = pp; 39680Sstevel@tonic-gate } 39690Sstevel@tonic-gate 39700Sstevel@tonic-gate 39710Sstevel@tonic-gate /* 39720Sstevel@tonic-gate * Remove this page from a linked list of pages 39730Sstevel@tonic-gate * using the p_next & p_prev pointers for the list. 39740Sstevel@tonic-gate * 39750Sstevel@tonic-gate * The caller is responsible for protecting the list pointers. 39760Sstevel@tonic-gate */ 39770Sstevel@tonic-gate void 39780Sstevel@tonic-gate page_sub(page_t **ppp, page_t *pp) 39790Sstevel@tonic-gate { 39800Sstevel@tonic-gate ASSERT((PP_ISFREE(pp)) ? 1 : 39810Sstevel@tonic-gate (PAGE_EXCL(pp)) || (PAGE_SHARED(pp) && page_iolock_assert(pp))); 39820Sstevel@tonic-gate 39830Sstevel@tonic-gate if (*ppp == NULL || pp == NULL) { 39840Sstevel@tonic-gate panic("page_sub: bad arg(s): pp %p, *ppp %p", 39850Sstevel@tonic-gate (void *)pp, (void *)(*ppp)); 39860Sstevel@tonic-gate /*NOTREACHED*/ 39870Sstevel@tonic-gate } 39880Sstevel@tonic-gate 39890Sstevel@tonic-gate page_sub_common(ppp, pp); 39900Sstevel@tonic-gate } 39910Sstevel@tonic-gate 39920Sstevel@tonic-gate 39930Sstevel@tonic-gate /* 39940Sstevel@tonic-gate * Common code for page_sub() and mach_page_sub() 39950Sstevel@tonic-gate */ 39960Sstevel@tonic-gate void 39970Sstevel@tonic-gate page_sub_common(page_t **ppp, page_t *pp) 39980Sstevel@tonic-gate { 39990Sstevel@tonic-gate if (*ppp == pp) 40000Sstevel@tonic-gate *ppp = pp->p_next; /* go to next page */ 40010Sstevel@tonic-gate 40020Sstevel@tonic-gate if (*ppp == pp) 40030Sstevel@tonic-gate *ppp = NULL; /* page list is gone */ 40040Sstevel@tonic-gate else { 40050Sstevel@tonic-gate pp->p_prev->p_next = pp->p_next; 40060Sstevel@tonic-gate pp->p_next->p_prev = pp->p_prev; 40070Sstevel@tonic-gate } 40080Sstevel@tonic-gate pp->p_prev = pp->p_next = pp; /* make pp a list of one */ 40090Sstevel@tonic-gate } 40100Sstevel@tonic-gate 40110Sstevel@tonic-gate 40120Sstevel@tonic-gate /* 40130Sstevel@tonic-gate * Break page list cppp into two lists with npages in the first list. 40140Sstevel@tonic-gate * The tail is returned in nppp. 40150Sstevel@tonic-gate */ 40160Sstevel@tonic-gate void 40170Sstevel@tonic-gate page_list_break(page_t **oppp, page_t **nppp, pgcnt_t npages) 40180Sstevel@tonic-gate { 40190Sstevel@tonic-gate page_t *s1pp = *oppp; 40200Sstevel@tonic-gate page_t *s2pp; 40210Sstevel@tonic-gate page_t *e1pp, *e2pp; 40220Sstevel@tonic-gate long n = 0; 40230Sstevel@tonic-gate 40240Sstevel@tonic-gate if (s1pp == NULL) { 40250Sstevel@tonic-gate *nppp = NULL; 40260Sstevel@tonic-gate return; 40270Sstevel@tonic-gate } 40280Sstevel@tonic-gate if (npages == 0) { 40290Sstevel@tonic-gate *nppp = s1pp; 40300Sstevel@tonic-gate *oppp = NULL; 40310Sstevel@tonic-gate return; 40320Sstevel@tonic-gate } 40330Sstevel@tonic-gate for (n = 0, s2pp = *oppp; n < npages; n++) { 40340Sstevel@tonic-gate s2pp = s2pp->p_next; 40350Sstevel@tonic-gate } 40360Sstevel@tonic-gate /* Fix head and tail of new lists */ 40370Sstevel@tonic-gate e1pp = s2pp->p_prev; 40380Sstevel@tonic-gate e2pp = s1pp->p_prev; 40390Sstevel@tonic-gate s1pp->p_prev = e1pp; 40400Sstevel@tonic-gate e1pp->p_next = s1pp; 40410Sstevel@tonic-gate s2pp->p_prev = e2pp; 40420Sstevel@tonic-gate e2pp->p_next = s2pp; 40430Sstevel@tonic-gate 40440Sstevel@tonic-gate /* second list empty */ 40450Sstevel@tonic-gate if (s2pp == s1pp) { 40460Sstevel@tonic-gate *oppp = s1pp; 40470Sstevel@tonic-gate *nppp = NULL; 40480Sstevel@tonic-gate } else { 40490Sstevel@tonic-gate *oppp = s1pp; 40500Sstevel@tonic-gate *nppp = s2pp; 40510Sstevel@tonic-gate } 40520Sstevel@tonic-gate } 40530Sstevel@tonic-gate 40540Sstevel@tonic-gate /* 40550Sstevel@tonic-gate * Concatenate page list nppp onto the end of list ppp. 40560Sstevel@tonic-gate */ 40570Sstevel@tonic-gate void 40580Sstevel@tonic-gate page_list_concat(page_t **ppp, page_t **nppp) 40590Sstevel@tonic-gate { 40600Sstevel@tonic-gate page_t *s1pp, *s2pp, *e1pp, *e2pp; 40610Sstevel@tonic-gate 40620Sstevel@tonic-gate if (*nppp == NULL) { 40630Sstevel@tonic-gate return; 40640Sstevel@tonic-gate } 40650Sstevel@tonic-gate if (*ppp == NULL) { 40660Sstevel@tonic-gate *ppp = *nppp; 40670Sstevel@tonic-gate return; 40680Sstevel@tonic-gate } 40690Sstevel@tonic-gate s1pp = *ppp; 40700Sstevel@tonic-gate e1pp = s1pp->p_prev; 40710Sstevel@tonic-gate s2pp = *nppp; 40720Sstevel@tonic-gate e2pp = s2pp->p_prev; 40730Sstevel@tonic-gate s1pp->p_prev = e2pp; 40740Sstevel@tonic-gate e2pp->p_next = s1pp; 40750Sstevel@tonic-gate e1pp->p_next = s2pp; 40760Sstevel@tonic-gate s2pp->p_prev = e1pp; 40770Sstevel@tonic-gate } 40780Sstevel@tonic-gate 40790Sstevel@tonic-gate /* 40800Sstevel@tonic-gate * return the next page in the page list 40810Sstevel@tonic-gate */ 40820Sstevel@tonic-gate page_t * 40830Sstevel@tonic-gate page_list_next(page_t *pp) 40840Sstevel@tonic-gate { 40850Sstevel@tonic-gate return (pp->p_next); 40860Sstevel@tonic-gate } 40870Sstevel@tonic-gate 40880Sstevel@tonic-gate 40890Sstevel@tonic-gate /* 40900Sstevel@tonic-gate * Add the page to the front of the linked list of pages 40910Sstevel@tonic-gate * using p_vpnext/p_vpprev pointers for the list. 40920Sstevel@tonic-gate * 40930Sstevel@tonic-gate * The caller is responsible for protecting the lists. 40940Sstevel@tonic-gate */ 40950Sstevel@tonic-gate void 40960Sstevel@tonic-gate page_vpadd(page_t **ppp, page_t *pp) 40970Sstevel@tonic-gate { 40980Sstevel@tonic-gate if (*ppp == NULL) { 40990Sstevel@tonic-gate pp->p_vpnext = pp->p_vpprev = pp; 41000Sstevel@tonic-gate } else { 41010Sstevel@tonic-gate pp->p_vpnext = *ppp; 41020Sstevel@tonic-gate pp->p_vpprev = (*ppp)->p_vpprev; 41030Sstevel@tonic-gate (*ppp)->p_vpprev = pp; 41040Sstevel@tonic-gate pp->p_vpprev->p_vpnext = pp; 41050Sstevel@tonic-gate } 41060Sstevel@tonic-gate *ppp = pp; 41070Sstevel@tonic-gate } 41080Sstevel@tonic-gate 41090Sstevel@tonic-gate /* 41100Sstevel@tonic-gate * Remove this page from the linked list of pages 41110Sstevel@tonic-gate * using p_vpnext/p_vpprev pointers for the list. 41120Sstevel@tonic-gate * 41130Sstevel@tonic-gate * The caller is responsible for protecting the lists. 41140Sstevel@tonic-gate */ 41150Sstevel@tonic-gate void 41160Sstevel@tonic-gate page_vpsub(page_t **ppp, page_t *pp) 41170Sstevel@tonic-gate { 41180Sstevel@tonic-gate if (*ppp == NULL || pp == NULL) { 41190Sstevel@tonic-gate panic("page_vpsub: bad arg(s): pp %p, *ppp %p", 41200Sstevel@tonic-gate (void *)pp, (void *)(*ppp)); 41210Sstevel@tonic-gate /*NOTREACHED*/ 41220Sstevel@tonic-gate } 41230Sstevel@tonic-gate 41240Sstevel@tonic-gate if (*ppp == pp) 41250Sstevel@tonic-gate *ppp = pp->p_vpnext; /* go to next page */ 41260Sstevel@tonic-gate 41270Sstevel@tonic-gate if (*ppp == pp) 41280Sstevel@tonic-gate *ppp = NULL; /* page list is gone */ 41290Sstevel@tonic-gate else { 41300Sstevel@tonic-gate pp->p_vpprev->p_vpnext = pp->p_vpnext; 41310Sstevel@tonic-gate pp->p_vpnext->p_vpprev = pp->p_vpprev; 41320Sstevel@tonic-gate } 41330Sstevel@tonic-gate pp->p_vpprev = pp->p_vpnext = pp; /* make pp a list of one */ 41340Sstevel@tonic-gate } 41350Sstevel@tonic-gate 41360Sstevel@tonic-gate /* 41370Sstevel@tonic-gate * Lock a physical page into memory "long term". Used to support "lock 41380Sstevel@tonic-gate * in memory" functions. Accepts the page to be locked, and a cow variable 41390Sstevel@tonic-gate * to indicate whether a the lock will travel to the new page during 41400Sstevel@tonic-gate * a potential copy-on-write. 41410Sstevel@tonic-gate */ 41420Sstevel@tonic-gate int 41430Sstevel@tonic-gate page_pp_lock( 41440Sstevel@tonic-gate page_t *pp, /* page to be locked */ 41450Sstevel@tonic-gate int cow, /* cow lock */ 41460Sstevel@tonic-gate int kernel) /* must succeed -- ignore checking */ 41470Sstevel@tonic-gate { 41480Sstevel@tonic-gate int r = 0; /* result -- assume failure */ 41490Sstevel@tonic-gate 41500Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 41510Sstevel@tonic-gate 41520Sstevel@tonic-gate page_struct_lock(pp); 41530Sstevel@tonic-gate /* 41540Sstevel@tonic-gate * Acquire the "freemem_lock" for availrmem. 41550Sstevel@tonic-gate */ 41560Sstevel@tonic-gate if (cow) { 41570Sstevel@tonic-gate mutex_enter(&freemem_lock); 41580Sstevel@tonic-gate if ((availrmem > pages_pp_maximum) && 41590Sstevel@tonic-gate (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM)) { 41600Sstevel@tonic-gate availrmem--; 41610Sstevel@tonic-gate pages_locked++; 41620Sstevel@tonic-gate mutex_exit(&freemem_lock); 41630Sstevel@tonic-gate r = 1; 41640Sstevel@tonic-gate if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 41650Sstevel@tonic-gate cmn_err(CE_WARN, 41660Sstevel@tonic-gate "COW lock limit reached on pfn 0x%lx", 41670Sstevel@tonic-gate page_pptonum(pp)); 41680Sstevel@tonic-gate } 41690Sstevel@tonic-gate } else 41700Sstevel@tonic-gate mutex_exit(&freemem_lock); 41710Sstevel@tonic-gate } else { 41720Sstevel@tonic-gate if (pp->p_lckcnt) { 41730Sstevel@tonic-gate if (pp->p_lckcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 41740Sstevel@tonic-gate r = 1; 41750Sstevel@tonic-gate if (++pp->p_lckcnt == 41760Sstevel@tonic-gate (ushort_t)PAGE_LOCK_MAXIMUM) { 41770Sstevel@tonic-gate cmn_err(CE_WARN, "Page lock limit " 41780Sstevel@tonic-gate "reached on pfn 0x%lx", 41790Sstevel@tonic-gate page_pptonum(pp)); 41800Sstevel@tonic-gate } 41810Sstevel@tonic-gate } 41820Sstevel@tonic-gate } else { 41830Sstevel@tonic-gate if (kernel) { 41840Sstevel@tonic-gate /* availrmem accounting done by caller */ 41850Sstevel@tonic-gate ++pp->p_lckcnt; 41860Sstevel@tonic-gate r = 1; 41870Sstevel@tonic-gate } else { 41880Sstevel@tonic-gate mutex_enter(&freemem_lock); 41890Sstevel@tonic-gate if (availrmem > pages_pp_maximum) { 41900Sstevel@tonic-gate availrmem--; 41910Sstevel@tonic-gate pages_locked++; 41920Sstevel@tonic-gate ++pp->p_lckcnt; 41930Sstevel@tonic-gate r = 1; 41940Sstevel@tonic-gate } 41950Sstevel@tonic-gate mutex_exit(&freemem_lock); 41960Sstevel@tonic-gate } 41970Sstevel@tonic-gate } 41980Sstevel@tonic-gate } 41990Sstevel@tonic-gate page_struct_unlock(pp); 42000Sstevel@tonic-gate return (r); 42010Sstevel@tonic-gate } 42020Sstevel@tonic-gate 42030Sstevel@tonic-gate /* 42040Sstevel@tonic-gate * Decommit a lock on a physical page frame. Account for cow locks if 42050Sstevel@tonic-gate * appropriate. 42060Sstevel@tonic-gate */ 42070Sstevel@tonic-gate void 42080Sstevel@tonic-gate page_pp_unlock( 42090Sstevel@tonic-gate page_t *pp, /* page to be unlocked */ 42100Sstevel@tonic-gate int cow, /* expect cow lock */ 42110Sstevel@tonic-gate int kernel) /* this was a kernel lock */ 42120Sstevel@tonic-gate { 42130Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 42140Sstevel@tonic-gate 42150Sstevel@tonic-gate page_struct_lock(pp); 42160Sstevel@tonic-gate /* 42170Sstevel@tonic-gate * Acquire the "freemem_lock" for availrmem. 42180Sstevel@tonic-gate * If cowcnt or lcknt is already 0 do nothing; i.e., we 42190Sstevel@tonic-gate * could be called to unlock even if nothing is locked. This could 42200Sstevel@tonic-gate * happen if locked file pages were truncated (removing the lock) 42210Sstevel@tonic-gate * and the file was grown again and new pages faulted in; the new 42220Sstevel@tonic-gate * pages are unlocked but the segment still thinks they're locked. 42230Sstevel@tonic-gate */ 42240Sstevel@tonic-gate if (cow) { 42250Sstevel@tonic-gate if (pp->p_cowcnt) { 42260Sstevel@tonic-gate mutex_enter(&freemem_lock); 42270Sstevel@tonic-gate pp->p_cowcnt--; 42280Sstevel@tonic-gate availrmem++; 42290Sstevel@tonic-gate pages_locked--; 42300Sstevel@tonic-gate mutex_exit(&freemem_lock); 42310Sstevel@tonic-gate } 42320Sstevel@tonic-gate } else { 42330Sstevel@tonic-gate if (pp->p_lckcnt && --pp->p_lckcnt == 0) { 42340Sstevel@tonic-gate if (!kernel) { 42350Sstevel@tonic-gate mutex_enter(&freemem_lock); 42360Sstevel@tonic-gate availrmem++; 42370Sstevel@tonic-gate pages_locked--; 42380Sstevel@tonic-gate mutex_exit(&freemem_lock); 42390Sstevel@tonic-gate } 42400Sstevel@tonic-gate } 42410Sstevel@tonic-gate } 42420Sstevel@tonic-gate page_struct_unlock(pp); 42430Sstevel@tonic-gate } 42440Sstevel@tonic-gate 42450Sstevel@tonic-gate /* 42460Sstevel@tonic-gate * This routine reserves availrmem for npages; 42470Sstevel@tonic-gate * flags: KM_NOSLEEP or KM_SLEEP 42480Sstevel@tonic-gate * returns 1 on success or 0 on failure 42490Sstevel@tonic-gate */ 42500Sstevel@tonic-gate int 42510Sstevel@tonic-gate page_resv(pgcnt_t npages, uint_t flags) 42520Sstevel@tonic-gate { 42530Sstevel@tonic-gate mutex_enter(&freemem_lock); 42540Sstevel@tonic-gate while (availrmem < tune.t_minarmem + npages) { 42550Sstevel@tonic-gate if (flags & KM_NOSLEEP) { 42560Sstevel@tonic-gate mutex_exit(&freemem_lock); 42570Sstevel@tonic-gate return (0); 42580Sstevel@tonic-gate } 42590Sstevel@tonic-gate mutex_exit(&freemem_lock); 42600Sstevel@tonic-gate page_needfree(npages); 42610Sstevel@tonic-gate kmem_reap(); 42620Sstevel@tonic-gate delay(hz >> 2); 42630Sstevel@tonic-gate page_needfree(-(spgcnt_t)npages); 42640Sstevel@tonic-gate mutex_enter(&freemem_lock); 42650Sstevel@tonic-gate } 42660Sstevel@tonic-gate availrmem -= npages; 42670Sstevel@tonic-gate mutex_exit(&freemem_lock); 42680Sstevel@tonic-gate return (1); 42690Sstevel@tonic-gate } 42700Sstevel@tonic-gate 42710Sstevel@tonic-gate /* 42720Sstevel@tonic-gate * This routine unreserves availrmem for npages; 42730Sstevel@tonic-gate */ 42740Sstevel@tonic-gate void 42750Sstevel@tonic-gate page_unresv(pgcnt_t npages) 42760Sstevel@tonic-gate { 42770Sstevel@tonic-gate mutex_enter(&freemem_lock); 42780Sstevel@tonic-gate availrmem += npages; 42790Sstevel@tonic-gate mutex_exit(&freemem_lock); 42800Sstevel@tonic-gate } 42810Sstevel@tonic-gate 42820Sstevel@tonic-gate /* 42830Sstevel@tonic-gate * See Statement at the beginning of segvn_lockop() regarding 42840Sstevel@tonic-gate * the way we handle cowcnts and lckcnts. 42850Sstevel@tonic-gate * 42860Sstevel@tonic-gate * Transfer cowcnt on 'opp' to cowcnt on 'npp' if the vpage 42870Sstevel@tonic-gate * that breaks COW has PROT_WRITE. 42880Sstevel@tonic-gate * 42890Sstevel@tonic-gate * Note that, we may also break COW in case we are softlocking 42900Sstevel@tonic-gate * on read access during physio; 42910Sstevel@tonic-gate * in this softlock case, the vpage may not have PROT_WRITE. 42920Sstevel@tonic-gate * So, we need to transfer lckcnt on 'opp' to lckcnt on 'npp' 42930Sstevel@tonic-gate * if the vpage doesn't have PROT_WRITE. 42940Sstevel@tonic-gate * 42950Sstevel@tonic-gate * This routine is never called if we are stealing a page 42960Sstevel@tonic-gate * in anon_private. 42970Sstevel@tonic-gate * 42980Sstevel@tonic-gate * The caller subtracted from availrmem for read only mapping. 42990Sstevel@tonic-gate * if lckcnt is 1 increment availrmem. 43000Sstevel@tonic-gate */ 43010Sstevel@tonic-gate void 43020Sstevel@tonic-gate page_pp_useclaim( 43030Sstevel@tonic-gate page_t *opp, /* original page frame losing lock */ 43040Sstevel@tonic-gate page_t *npp, /* new page frame gaining lock */ 43050Sstevel@tonic-gate uint_t write_perm) /* set if vpage has PROT_WRITE */ 43060Sstevel@tonic-gate { 43070Sstevel@tonic-gate int payback = 0; 43080Sstevel@tonic-gate 43090Sstevel@tonic-gate ASSERT(PAGE_LOCKED(opp)); 43100Sstevel@tonic-gate ASSERT(PAGE_LOCKED(npp)); 43110Sstevel@tonic-gate 43120Sstevel@tonic-gate page_struct_lock(opp); 43130Sstevel@tonic-gate 43140Sstevel@tonic-gate ASSERT(npp->p_cowcnt == 0); 43150Sstevel@tonic-gate ASSERT(npp->p_lckcnt == 0); 43160Sstevel@tonic-gate 43170Sstevel@tonic-gate /* Don't use claim if nothing is locked (see page_pp_unlock above) */ 43180Sstevel@tonic-gate if ((write_perm && opp->p_cowcnt != 0) || 43190Sstevel@tonic-gate (!write_perm && opp->p_lckcnt != 0)) { 43200Sstevel@tonic-gate 43210Sstevel@tonic-gate if (write_perm) { 43220Sstevel@tonic-gate npp->p_cowcnt++; 43230Sstevel@tonic-gate ASSERT(opp->p_cowcnt != 0); 43240Sstevel@tonic-gate opp->p_cowcnt--; 43250Sstevel@tonic-gate } else { 43260Sstevel@tonic-gate 43270Sstevel@tonic-gate ASSERT(opp->p_lckcnt != 0); 43280Sstevel@tonic-gate 43290Sstevel@tonic-gate /* 43300Sstevel@tonic-gate * We didn't need availrmem decremented if p_lckcnt on 43310Sstevel@tonic-gate * original page is 1. Here, we are unlocking 43320Sstevel@tonic-gate * read-only copy belonging to original page and 43330Sstevel@tonic-gate * are locking a copy belonging to new page. 43340Sstevel@tonic-gate */ 43350Sstevel@tonic-gate if (opp->p_lckcnt == 1) 43360Sstevel@tonic-gate payback = 1; 43370Sstevel@tonic-gate 43380Sstevel@tonic-gate npp->p_lckcnt++; 43390Sstevel@tonic-gate opp->p_lckcnt--; 43400Sstevel@tonic-gate } 43410Sstevel@tonic-gate } 43420Sstevel@tonic-gate if (payback) { 43430Sstevel@tonic-gate mutex_enter(&freemem_lock); 43440Sstevel@tonic-gate availrmem++; 43450Sstevel@tonic-gate pages_useclaim--; 43460Sstevel@tonic-gate mutex_exit(&freemem_lock); 43470Sstevel@tonic-gate } 43480Sstevel@tonic-gate page_struct_unlock(opp); 43490Sstevel@tonic-gate } 43500Sstevel@tonic-gate 43510Sstevel@tonic-gate /* 43520Sstevel@tonic-gate * Simple claim adjust functions -- used to support changes in 43530Sstevel@tonic-gate * claims due to changes in access permissions. Used by segvn_setprot(). 43540Sstevel@tonic-gate */ 43550Sstevel@tonic-gate int 43560Sstevel@tonic-gate page_addclaim(page_t *pp) 43570Sstevel@tonic-gate { 43580Sstevel@tonic-gate int r = 0; /* result */ 43590Sstevel@tonic-gate 43600Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 43610Sstevel@tonic-gate 43620Sstevel@tonic-gate page_struct_lock(pp); 43630Sstevel@tonic-gate ASSERT(pp->p_lckcnt != 0); 43640Sstevel@tonic-gate 43650Sstevel@tonic-gate if (pp->p_lckcnt == 1) { 43660Sstevel@tonic-gate if (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 43670Sstevel@tonic-gate --pp->p_lckcnt; 43680Sstevel@tonic-gate r = 1; 43690Sstevel@tonic-gate if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 43700Sstevel@tonic-gate cmn_err(CE_WARN, 43710Sstevel@tonic-gate "COW lock limit reached on pfn 0x%lx", 43720Sstevel@tonic-gate page_pptonum(pp)); 43730Sstevel@tonic-gate } 43740Sstevel@tonic-gate } 43750Sstevel@tonic-gate } else { 43760Sstevel@tonic-gate mutex_enter(&freemem_lock); 43770Sstevel@tonic-gate if ((availrmem > pages_pp_maximum) && 43780Sstevel@tonic-gate (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM)) { 43790Sstevel@tonic-gate --availrmem; 43800Sstevel@tonic-gate ++pages_claimed; 43810Sstevel@tonic-gate mutex_exit(&freemem_lock); 43820Sstevel@tonic-gate --pp->p_lckcnt; 43830Sstevel@tonic-gate r = 1; 43840Sstevel@tonic-gate if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 43850Sstevel@tonic-gate cmn_err(CE_WARN, 43860Sstevel@tonic-gate "COW lock limit reached on pfn 0x%lx", 43870Sstevel@tonic-gate page_pptonum(pp)); 43880Sstevel@tonic-gate } 43890Sstevel@tonic-gate } else 43900Sstevel@tonic-gate mutex_exit(&freemem_lock); 43910Sstevel@tonic-gate } 43920Sstevel@tonic-gate page_struct_unlock(pp); 43930Sstevel@tonic-gate return (r); 43940Sstevel@tonic-gate } 43950Sstevel@tonic-gate 43960Sstevel@tonic-gate int 43970Sstevel@tonic-gate page_subclaim(page_t *pp) 43980Sstevel@tonic-gate { 43990Sstevel@tonic-gate int r = 0; 44000Sstevel@tonic-gate 44010Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 44020Sstevel@tonic-gate 44030Sstevel@tonic-gate page_struct_lock(pp); 44040Sstevel@tonic-gate ASSERT(pp->p_cowcnt != 0); 44050Sstevel@tonic-gate 44060Sstevel@tonic-gate if (pp->p_lckcnt) { 44070Sstevel@tonic-gate if (pp->p_lckcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 44080Sstevel@tonic-gate r = 1; 44090Sstevel@tonic-gate /* 44100Sstevel@tonic-gate * for availrmem 44110Sstevel@tonic-gate */ 44120Sstevel@tonic-gate mutex_enter(&freemem_lock); 44130Sstevel@tonic-gate availrmem++; 44140Sstevel@tonic-gate pages_claimed--; 44150Sstevel@tonic-gate mutex_exit(&freemem_lock); 44160Sstevel@tonic-gate 44170Sstevel@tonic-gate pp->p_cowcnt--; 44180Sstevel@tonic-gate 44190Sstevel@tonic-gate if (++pp->p_lckcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 44200Sstevel@tonic-gate cmn_err(CE_WARN, 44210Sstevel@tonic-gate "Page lock limit reached on pfn 0x%lx", 44220Sstevel@tonic-gate page_pptonum(pp)); 44230Sstevel@tonic-gate } 44240Sstevel@tonic-gate } 44250Sstevel@tonic-gate } else { 44260Sstevel@tonic-gate r = 1; 44270Sstevel@tonic-gate pp->p_cowcnt--; 44280Sstevel@tonic-gate pp->p_lckcnt++; 44290Sstevel@tonic-gate } 44300Sstevel@tonic-gate page_struct_unlock(pp); 44310Sstevel@tonic-gate return (r); 44320Sstevel@tonic-gate } 44330Sstevel@tonic-gate 44340Sstevel@tonic-gate int 44350Sstevel@tonic-gate page_addclaim_pages(page_t **ppa) 44360Sstevel@tonic-gate { 44370Sstevel@tonic-gate 44380Sstevel@tonic-gate pgcnt_t lckpgs = 0, pg_idx; 44390Sstevel@tonic-gate 44400Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_addclaim_pages); 44410Sstevel@tonic-gate 44420Sstevel@tonic-gate mutex_enter(&page_llock); 44430Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 44440Sstevel@tonic-gate 44450Sstevel@tonic-gate ASSERT(PAGE_LOCKED(ppa[pg_idx])); 44460Sstevel@tonic-gate ASSERT(ppa[pg_idx]->p_lckcnt != 0); 44470Sstevel@tonic-gate if (ppa[pg_idx]->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 44480Sstevel@tonic-gate mutex_exit(&page_llock); 44490Sstevel@tonic-gate return (0); 44500Sstevel@tonic-gate } 44510Sstevel@tonic-gate if (ppa[pg_idx]->p_lckcnt > 1) 44520Sstevel@tonic-gate lckpgs++; 44530Sstevel@tonic-gate } 44540Sstevel@tonic-gate 44550Sstevel@tonic-gate if (lckpgs != 0) { 44560Sstevel@tonic-gate mutex_enter(&freemem_lock); 44570Sstevel@tonic-gate if (availrmem >= pages_pp_maximum + lckpgs) { 44580Sstevel@tonic-gate availrmem -= lckpgs; 44590Sstevel@tonic-gate pages_claimed += lckpgs; 44600Sstevel@tonic-gate } else { 44610Sstevel@tonic-gate mutex_exit(&freemem_lock); 44620Sstevel@tonic-gate mutex_exit(&page_llock); 44630Sstevel@tonic-gate return (0); 44640Sstevel@tonic-gate } 44650Sstevel@tonic-gate mutex_exit(&freemem_lock); 44660Sstevel@tonic-gate } 44670Sstevel@tonic-gate 44680Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 44690Sstevel@tonic-gate ppa[pg_idx]->p_lckcnt--; 44700Sstevel@tonic-gate ppa[pg_idx]->p_cowcnt++; 44710Sstevel@tonic-gate } 44720Sstevel@tonic-gate mutex_exit(&page_llock); 44730Sstevel@tonic-gate return (1); 44740Sstevel@tonic-gate } 44750Sstevel@tonic-gate 44760Sstevel@tonic-gate int 44770Sstevel@tonic-gate page_subclaim_pages(page_t **ppa) 44780Sstevel@tonic-gate { 44790Sstevel@tonic-gate pgcnt_t ulckpgs = 0, pg_idx; 44800Sstevel@tonic-gate 44810Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_subclaim_pages); 44820Sstevel@tonic-gate 44830Sstevel@tonic-gate mutex_enter(&page_llock); 44840Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 44850Sstevel@tonic-gate 44860Sstevel@tonic-gate ASSERT(PAGE_LOCKED(ppa[pg_idx])); 44870Sstevel@tonic-gate ASSERT(ppa[pg_idx]->p_cowcnt != 0); 44880Sstevel@tonic-gate if (ppa[pg_idx]->p_lckcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 44890Sstevel@tonic-gate mutex_exit(&page_llock); 44900Sstevel@tonic-gate return (0); 44910Sstevel@tonic-gate } 44920Sstevel@tonic-gate if (ppa[pg_idx]->p_lckcnt != 0) 44930Sstevel@tonic-gate ulckpgs++; 44940Sstevel@tonic-gate } 44950Sstevel@tonic-gate 44960Sstevel@tonic-gate if (ulckpgs != 0) { 44970Sstevel@tonic-gate mutex_enter(&freemem_lock); 44980Sstevel@tonic-gate availrmem += ulckpgs; 44990Sstevel@tonic-gate pages_claimed -= ulckpgs; 45000Sstevel@tonic-gate mutex_exit(&freemem_lock); 45010Sstevel@tonic-gate } 45020Sstevel@tonic-gate 45030Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 45040Sstevel@tonic-gate ppa[pg_idx]->p_cowcnt--; 45050Sstevel@tonic-gate ppa[pg_idx]->p_lckcnt++; 45060Sstevel@tonic-gate 45070Sstevel@tonic-gate } 45080Sstevel@tonic-gate mutex_exit(&page_llock); 45090Sstevel@tonic-gate return (1); 45100Sstevel@tonic-gate } 45110Sstevel@tonic-gate 45120Sstevel@tonic-gate page_t * 45130Sstevel@tonic-gate page_numtopp(pfn_t pfnum, se_t se) 45140Sstevel@tonic-gate { 45150Sstevel@tonic-gate page_t *pp; 45160Sstevel@tonic-gate 45170Sstevel@tonic-gate retry: 45180Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 45190Sstevel@tonic-gate if (pp == NULL) { 45200Sstevel@tonic-gate return ((page_t *)NULL); 45210Sstevel@tonic-gate } 45220Sstevel@tonic-gate 45230Sstevel@tonic-gate /* 45240Sstevel@tonic-gate * Acquire the appropriate lock on the page. 45250Sstevel@tonic-gate */ 45260Sstevel@tonic-gate while (!page_lock(pp, se, (kmutex_t *)NULL, P_RECLAIM)) { 45270Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) 45280Sstevel@tonic-gate goto retry; 45290Sstevel@tonic-gate continue; 45300Sstevel@tonic-gate } 45310Sstevel@tonic-gate 45320Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) { 45330Sstevel@tonic-gate page_unlock(pp); 45340Sstevel@tonic-gate goto retry; 45350Sstevel@tonic-gate } 45360Sstevel@tonic-gate 45370Sstevel@tonic-gate return (pp); 45380Sstevel@tonic-gate } 45390Sstevel@tonic-gate 45400Sstevel@tonic-gate page_t * 45410Sstevel@tonic-gate page_numtopp_noreclaim(pfn_t pfnum, se_t se) 45420Sstevel@tonic-gate { 45430Sstevel@tonic-gate page_t *pp; 45440Sstevel@tonic-gate 45450Sstevel@tonic-gate retry: 45460Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 45470Sstevel@tonic-gate if (pp == NULL) { 45480Sstevel@tonic-gate return ((page_t *)NULL); 45490Sstevel@tonic-gate } 45500Sstevel@tonic-gate 45510Sstevel@tonic-gate /* 45520Sstevel@tonic-gate * Acquire the appropriate lock on the page. 45530Sstevel@tonic-gate */ 45540Sstevel@tonic-gate while (!page_lock(pp, se, (kmutex_t *)NULL, P_NO_RECLAIM)) { 45550Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) 45560Sstevel@tonic-gate goto retry; 45570Sstevel@tonic-gate continue; 45580Sstevel@tonic-gate } 45590Sstevel@tonic-gate 45600Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) { 45610Sstevel@tonic-gate page_unlock(pp); 45620Sstevel@tonic-gate goto retry; 45630Sstevel@tonic-gate } 45640Sstevel@tonic-gate 45650Sstevel@tonic-gate return (pp); 45660Sstevel@tonic-gate } 45670Sstevel@tonic-gate 45680Sstevel@tonic-gate /* 45690Sstevel@tonic-gate * This routine is like page_numtopp, but will only return page structs 45700Sstevel@tonic-gate * for pages which are ok for loading into hardware using the page struct. 45710Sstevel@tonic-gate */ 45720Sstevel@tonic-gate page_t * 45730Sstevel@tonic-gate page_numtopp_nowait(pfn_t pfnum, se_t se) 45740Sstevel@tonic-gate { 45750Sstevel@tonic-gate page_t *pp; 45760Sstevel@tonic-gate 45770Sstevel@tonic-gate retry: 45780Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 45790Sstevel@tonic-gate if (pp == NULL) { 45800Sstevel@tonic-gate return ((page_t *)NULL); 45810Sstevel@tonic-gate } 45820Sstevel@tonic-gate 45830Sstevel@tonic-gate /* 45840Sstevel@tonic-gate * Try to acquire the appropriate lock on the page. 45850Sstevel@tonic-gate */ 45860Sstevel@tonic-gate if (PP_ISFREE(pp)) 45870Sstevel@tonic-gate pp = NULL; 45880Sstevel@tonic-gate else { 45890Sstevel@tonic-gate if (!page_trylock(pp, se)) 45900Sstevel@tonic-gate pp = NULL; 45910Sstevel@tonic-gate else { 45920Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) { 45930Sstevel@tonic-gate page_unlock(pp); 45940Sstevel@tonic-gate goto retry; 45950Sstevel@tonic-gate } 45960Sstevel@tonic-gate if (PP_ISFREE(pp)) { 45970Sstevel@tonic-gate page_unlock(pp); 45980Sstevel@tonic-gate pp = NULL; 45990Sstevel@tonic-gate } 46000Sstevel@tonic-gate } 46010Sstevel@tonic-gate } 46020Sstevel@tonic-gate return (pp); 46030Sstevel@tonic-gate } 46040Sstevel@tonic-gate 46050Sstevel@tonic-gate /* 46060Sstevel@tonic-gate * Returns a count of dirty pages that are in the process 46070Sstevel@tonic-gate * of being written out. If 'cleanit' is set, try to push the page. 46080Sstevel@tonic-gate */ 46090Sstevel@tonic-gate pgcnt_t 46100Sstevel@tonic-gate page_busy(int cleanit) 46110Sstevel@tonic-gate { 46120Sstevel@tonic-gate page_t *page0 = page_first(); 46130Sstevel@tonic-gate page_t *pp = page0; 46140Sstevel@tonic-gate pgcnt_t nppbusy = 0; 46150Sstevel@tonic-gate u_offset_t off; 46160Sstevel@tonic-gate 46170Sstevel@tonic-gate do { 46180Sstevel@tonic-gate vnode_t *vp = pp->p_vnode; 46190Sstevel@tonic-gate 46200Sstevel@tonic-gate /* 46210Sstevel@tonic-gate * A page is a candidate for syncing if it is: 46220Sstevel@tonic-gate * 46230Sstevel@tonic-gate * (a) On neither the freelist nor the cachelist 46240Sstevel@tonic-gate * (b) Hashed onto a vnode 46250Sstevel@tonic-gate * (c) Not a kernel page 46260Sstevel@tonic-gate * (d) Dirty 46270Sstevel@tonic-gate * (e) Not part of a swapfile 46280Sstevel@tonic-gate * (f) a page which belongs to a real vnode; eg has a non-null 46290Sstevel@tonic-gate * v_vfsp pointer. 46300Sstevel@tonic-gate * (g) Backed by a filesystem which doesn't have a 46310Sstevel@tonic-gate * stubbed-out sync operation 46320Sstevel@tonic-gate */ 46330Sstevel@tonic-gate if (!PP_ISFREE(pp) && vp != NULL && vp != &kvp && 46340Sstevel@tonic-gate hat_ismod(pp) && !IS_SWAPVP(vp) && vp->v_vfsp != NULL && 46350Sstevel@tonic-gate vfs_can_sync(vp->v_vfsp)) { 46360Sstevel@tonic-gate nppbusy++; 46370Sstevel@tonic-gate vfs_syncprogress(); 46380Sstevel@tonic-gate 46390Sstevel@tonic-gate if (!cleanit) 46400Sstevel@tonic-gate continue; 46410Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) 46420Sstevel@tonic-gate continue; 46430Sstevel@tonic-gate 46440Sstevel@tonic-gate if (PP_ISFREE(pp) || vp == NULL || IS_SWAPVP(vp) || 46450Sstevel@tonic-gate pp->p_lckcnt != 0 || pp->p_cowcnt != 0 || 46460Sstevel@tonic-gate !(hat_pagesync(pp, 46470Sstevel@tonic-gate HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD) & P_MOD)) { 46480Sstevel@tonic-gate page_unlock(pp); 46490Sstevel@tonic-gate continue; 46500Sstevel@tonic-gate } 46510Sstevel@tonic-gate off = pp->p_offset; 46520Sstevel@tonic-gate VN_HOLD(vp); 46530Sstevel@tonic-gate page_unlock(pp); 46540Sstevel@tonic-gate (void) VOP_PUTPAGE(vp, off, PAGESIZE, 46550Sstevel@tonic-gate B_ASYNC | B_FREE, kcred); 46560Sstevel@tonic-gate VN_RELE(vp); 46570Sstevel@tonic-gate } 46580Sstevel@tonic-gate } while ((pp = page_next(pp)) != page0); 46590Sstevel@tonic-gate 46600Sstevel@tonic-gate return (nppbusy); 46610Sstevel@tonic-gate } 46620Sstevel@tonic-gate 46630Sstevel@tonic-gate void page_invalidate_pages(void); 46640Sstevel@tonic-gate 46650Sstevel@tonic-gate /* 46660Sstevel@tonic-gate * callback handler to vm sub-system 46670Sstevel@tonic-gate * 46680Sstevel@tonic-gate * callers make sure no recursive entries to this func. 46690Sstevel@tonic-gate */ 46700Sstevel@tonic-gate /*ARGSUSED*/ 46710Sstevel@tonic-gate boolean_t 46720Sstevel@tonic-gate callb_vm_cpr(void *arg, int code) 46730Sstevel@tonic-gate { 46740Sstevel@tonic-gate if (code == CB_CODE_CPR_CHKPT) 46750Sstevel@tonic-gate page_invalidate_pages(); 46760Sstevel@tonic-gate return (B_TRUE); 46770Sstevel@tonic-gate } 46780Sstevel@tonic-gate 46790Sstevel@tonic-gate /* 46800Sstevel@tonic-gate * Invalidate all pages of the system. 46810Sstevel@tonic-gate * It shouldn't be called until all user page activities are all stopped. 46820Sstevel@tonic-gate */ 46830Sstevel@tonic-gate void 46840Sstevel@tonic-gate page_invalidate_pages() 46850Sstevel@tonic-gate { 46860Sstevel@tonic-gate page_t *pp; 46870Sstevel@tonic-gate page_t *page0; 46880Sstevel@tonic-gate pgcnt_t nbusypages; 46890Sstevel@tonic-gate int retry = 0; 46900Sstevel@tonic-gate const int MAXRETRIES = 4; 46910Sstevel@tonic-gate #if defined(__sparc) 46920Sstevel@tonic-gate extern struct vnode prom_ppages; 46930Sstevel@tonic-gate #endif /* __sparc */ 46940Sstevel@tonic-gate 46950Sstevel@tonic-gate top: 46960Sstevel@tonic-gate /* 46970Sstevel@tonic-gate * Flush dirty pages and destory the clean ones. 46980Sstevel@tonic-gate */ 46990Sstevel@tonic-gate nbusypages = 0; 47000Sstevel@tonic-gate 47010Sstevel@tonic-gate pp = page0 = page_first(); 47020Sstevel@tonic-gate do { 47030Sstevel@tonic-gate struct vnode *vp; 47040Sstevel@tonic-gate u_offset_t offset; 47050Sstevel@tonic-gate int mod; 47060Sstevel@tonic-gate 47070Sstevel@tonic-gate /* 47080Sstevel@tonic-gate * skip the page if it has no vnode or the page associated 47090Sstevel@tonic-gate * with the kernel vnode or prom allocated kernel mem. 47100Sstevel@tonic-gate */ 47110Sstevel@tonic-gate #if defined(__sparc) 47120Sstevel@tonic-gate if ((vp = pp->p_vnode) == NULL || vp == &kvp || 47130Sstevel@tonic-gate vp == &prom_ppages) 47140Sstevel@tonic-gate #else /* x86 doesn't have prom or prom_ppage */ 47150Sstevel@tonic-gate if ((vp = pp->p_vnode) == NULL || vp == &kvp) 47160Sstevel@tonic-gate #endif /* __sparc */ 47170Sstevel@tonic-gate continue; 47180Sstevel@tonic-gate 47190Sstevel@tonic-gate /* 47200Sstevel@tonic-gate * skip the page which is already free invalidated. 47210Sstevel@tonic-gate */ 47220Sstevel@tonic-gate if (PP_ISFREE(pp) && PP_ISAGED(pp)) 47230Sstevel@tonic-gate continue; 47240Sstevel@tonic-gate 47250Sstevel@tonic-gate /* 47260Sstevel@tonic-gate * skip pages that are already locked or can't be "exclusively" 47270Sstevel@tonic-gate * locked or are already free. After we lock the page, check 47280Sstevel@tonic-gate * the free and age bits again to be sure it's not destroied 47290Sstevel@tonic-gate * yet. 47300Sstevel@tonic-gate * To achieve max. parallelization, we use page_trylock instead 47310Sstevel@tonic-gate * of page_lock so that we don't get block on individual pages 47320Sstevel@tonic-gate * while we have thousands of other pages to process. 47330Sstevel@tonic-gate */ 47340Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) { 47350Sstevel@tonic-gate nbusypages++; 47360Sstevel@tonic-gate continue; 47370Sstevel@tonic-gate } else if (PP_ISFREE(pp)) { 47380Sstevel@tonic-gate if (!PP_ISAGED(pp)) { 47390Sstevel@tonic-gate page_destroy_free(pp); 47400Sstevel@tonic-gate } else { 47410Sstevel@tonic-gate page_unlock(pp); 47420Sstevel@tonic-gate } 47430Sstevel@tonic-gate continue; 47440Sstevel@tonic-gate } 47450Sstevel@tonic-gate /* 47460Sstevel@tonic-gate * Is this page involved in some I/O? shared? 47470Sstevel@tonic-gate * 47480Sstevel@tonic-gate * The page_struct_lock need not be acquired to 47490Sstevel@tonic-gate * examine these fields since the page has an 47500Sstevel@tonic-gate * "exclusive" lock. 47510Sstevel@tonic-gate */ 47520Sstevel@tonic-gate if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) { 47530Sstevel@tonic-gate page_unlock(pp); 47540Sstevel@tonic-gate continue; 47550Sstevel@tonic-gate } 47560Sstevel@tonic-gate 47570Sstevel@tonic-gate if (vp->v_type == VCHR) { 47580Sstevel@tonic-gate panic("vp->v_type == VCHR"); 47590Sstevel@tonic-gate /*NOTREACHED*/ 47600Sstevel@tonic-gate } 47610Sstevel@tonic-gate 47620Sstevel@tonic-gate if (!page_try_demote_pages(pp)) { 47630Sstevel@tonic-gate page_unlock(pp); 47640Sstevel@tonic-gate continue; 47650Sstevel@tonic-gate } 47660Sstevel@tonic-gate 47670Sstevel@tonic-gate /* 47680Sstevel@tonic-gate * Check the modified bit. Leave the bits alone in hardware 47690Sstevel@tonic-gate * (they will be modified if we do the putpage). 47700Sstevel@tonic-gate */ 47710Sstevel@tonic-gate mod = (hat_pagesync(pp, HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD) 47720Sstevel@tonic-gate & P_MOD); 47730Sstevel@tonic-gate if (mod) { 47740Sstevel@tonic-gate offset = pp->p_offset; 47750Sstevel@tonic-gate /* 47760Sstevel@tonic-gate * Hold the vnode before releasing the page lock 47770Sstevel@tonic-gate * to prevent it from being freed and re-used by 47780Sstevel@tonic-gate * some other thread. 47790Sstevel@tonic-gate */ 47800Sstevel@tonic-gate VN_HOLD(vp); 47810Sstevel@tonic-gate page_unlock(pp); 47820Sstevel@tonic-gate /* 47830Sstevel@tonic-gate * No error return is checked here. Callers such as 47840Sstevel@tonic-gate * cpr deals with the dirty pages at the dump time 47850Sstevel@tonic-gate * if this putpage fails. 47860Sstevel@tonic-gate */ 47870Sstevel@tonic-gate (void) VOP_PUTPAGE(vp, offset, PAGESIZE, B_INVAL, 47880Sstevel@tonic-gate kcred); 47890Sstevel@tonic-gate VN_RELE(vp); 47900Sstevel@tonic-gate } else { 47910Sstevel@tonic-gate page_destroy(pp, 0); 47920Sstevel@tonic-gate } 47930Sstevel@tonic-gate } while ((pp = page_next(pp)) != page0); 47940Sstevel@tonic-gate if (nbusypages && retry++ < MAXRETRIES) { 47950Sstevel@tonic-gate delay(1); 47960Sstevel@tonic-gate goto top; 47970Sstevel@tonic-gate } 47980Sstevel@tonic-gate } 47990Sstevel@tonic-gate 48000Sstevel@tonic-gate /* 48010Sstevel@tonic-gate * Replace the page "old" with the page "new" on the page hash and vnode lists 48020Sstevel@tonic-gate * 48030Sstevel@tonic-gate * the replacemnt must be done in place, ie the equivalent sequence: 48040Sstevel@tonic-gate * 48050Sstevel@tonic-gate * vp = old->p_vnode; 48060Sstevel@tonic-gate * off = old->p_offset; 48070Sstevel@tonic-gate * page_do_hashout(old) 48080Sstevel@tonic-gate * page_do_hashin(new, vp, off) 48090Sstevel@tonic-gate * 48100Sstevel@tonic-gate * doesn't work, since 48110Sstevel@tonic-gate * 1) if old is the only page on the vnode, the v_pages list has a window 48120Sstevel@tonic-gate * where it looks empty. This will break file system assumptions. 48130Sstevel@tonic-gate * and 48140Sstevel@tonic-gate * 2) pvn_vplist_dirty() can't deal with pages moving on the v_pages list. 48150Sstevel@tonic-gate */ 48160Sstevel@tonic-gate static void 48170Sstevel@tonic-gate page_do_relocate_hash(page_t *new, page_t *old) 48180Sstevel@tonic-gate { 48190Sstevel@tonic-gate page_t **hash_list; 48200Sstevel@tonic-gate vnode_t *vp = old->p_vnode; 48210Sstevel@tonic-gate kmutex_t *sep; 48220Sstevel@tonic-gate 48230Sstevel@tonic-gate ASSERT(PAGE_EXCL(old)); 48240Sstevel@tonic-gate ASSERT(PAGE_EXCL(new)); 48250Sstevel@tonic-gate ASSERT(vp != NULL); 48260Sstevel@tonic-gate ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 48270Sstevel@tonic-gate ASSERT(MUTEX_HELD(PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, old->p_offset)))); 48280Sstevel@tonic-gate 48290Sstevel@tonic-gate /* 48300Sstevel@tonic-gate * First find old page on the page hash list 48310Sstevel@tonic-gate */ 48320Sstevel@tonic-gate hash_list = &page_hash[PAGE_HASH_FUNC(vp, old->p_offset)]; 48330Sstevel@tonic-gate 48340Sstevel@tonic-gate for (;;) { 48350Sstevel@tonic-gate if (*hash_list == old) 48360Sstevel@tonic-gate break; 48370Sstevel@tonic-gate if (*hash_list == NULL) { 48380Sstevel@tonic-gate panic("page_do_hashout"); 48390Sstevel@tonic-gate /*NOTREACHED*/ 48400Sstevel@tonic-gate } 48410Sstevel@tonic-gate hash_list = &(*hash_list)->p_hash; 48420Sstevel@tonic-gate } 48430Sstevel@tonic-gate 48440Sstevel@tonic-gate /* 48450Sstevel@tonic-gate * update new and replace old with new on the page hash list 48460Sstevel@tonic-gate */ 48470Sstevel@tonic-gate new->p_vnode = old->p_vnode; 48480Sstevel@tonic-gate new->p_offset = old->p_offset; 48490Sstevel@tonic-gate new->p_hash = old->p_hash; 48500Sstevel@tonic-gate *hash_list = new; 48510Sstevel@tonic-gate 48520Sstevel@tonic-gate if ((new->p_vnode->v_flag & VISSWAP) != 0) 48530Sstevel@tonic-gate PP_SETSWAP(new); 48540Sstevel@tonic-gate 48550Sstevel@tonic-gate /* 48560Sstevel@tonic-gate * replace old with new on the vnode's page list 48570Sstevel@tonic-gate */ 48580Sstevel@tonic-gate if (old->p_vpnext == old) { 48590Sstevel@tonic-gate new->p_vpnext = new; 48600Sstevel@tonic-gate new->p_vpprev = new; 48610Sstevel@tonic-gate } else { 48620Sstevel@tonic-gate new->p_vpnext = old->p_vpnext; 48630Sstevel@tonic-gate new->p_vpprev = old->p_vpprev; 48640Sstevel@tonic-gate new->p_vpnext->p_vpprev = new; 48650Sstevel@tonic-gate new->p_vpprev->p_vpnext = new; 48660Sstevel@tonic-gate } 48670Sstevel@tonic-gate if (vp->v_pages == old) 48680Sstevel@tonic-gate vp->v_pages = new; 48690Sstevel@tonic-gate 48700Sstevel@tonic-gate /* 48710Sstevel@tonic-gate * clear out the old page 48720Sstevel@tonic-gate */ 48730Sstevel@tonic-gate old->p_hash = NULL; 48740Sstevel@tonic-gate old->p_vpnext = NULL; 48750Sstevel@tonic-gate old->p_vpprev = NULL; 48760Sstevel@tonic-gate old->p_vnode = NULL; 48770Sstevel@tonic-gate PP_CLRSWAP(old); 48780Sstevel@tonic-gate old->p_offset = (u_offset_t)-1; 48790Sstevel@tonic-gate page_clr_all_props(old); 48800Sstevel@tonic-gate 48810Sstevel@tonic-gate /* 48820Sstevel@tonic-gate * Wake up processes waiting for this page. The page's 48830Sstevel@tonic-gate * identity has been changed, and is probably not the 48840Sstevel@tonic-gate * desired page any longer. 48850Sstevel@tonic-gate */ 48860Sstevel@tonic-gate sep = page_se_mutex(old); 48870Sstevel@tonic-gate mutex_enter(sep); 48880Sstevel@tonic-gate if (CV_HAS_WAITERS(&old->p_cv)) 48890Sstevel@tonic-gate cv_broadcast(&old->p_cv); 48900Sstevel@tonic-gate mutex_exit(sep); 48910Sstevel@tonic-gate } 48920Sstevel@tonic-gate 48930Sstevel@tonic-gate /* 48940Sstevel@tonic-gate * This function moves the identity of page "pp_old" to page "pp_new". 48950Sstevel@tonic-gate * Both pages must be locked on entry. "pp_new" is free, has no identity, 48960Sstevel@tonic-gate * and need not be hashed out from anywhere. 48970Sstevel@tonic-gate */ 48980Sstevel@tonic-gate void 48990Sstevel@tonic-gate page_relocate_hash(page_t *pp_new, page_t *pp_old) 49000Sstevel@tonic-gate { 49010Sstevel@tonic-gate vnode_t *vp = pp_old->p_vnode; 49020Sstevel@tonic-gate u_offset_t off = pp_old->p_offset; 49030Sstevel@tonic-gate kmutex_t *phm, *vphm; 49040Sstevel@tonic-gate 49050Sstevel@tonic-gate /* 49060Sstevel@tonic-gate * Rehash two pages 49070Sstevel@tonic-gate */ 49080Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp_old)); 49090Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp_new)); 49100Sstevel@tonic-gate ASSERT(vp != NULL); 49110Sstevel@tonic-gate ASSERT(pp_new->p_vnode == NULL); 49120Sstevel@tonic-gate 49130Sstevel@tonic-gate /* 49140Sstevel@tonic-gate * hashout then hashin while holding the mutexes 49150Sstevel@tonic-gate */ 49160Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, off)); 49170Sstevel@tonic-gate mutex_enter(phm); 49180Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 49190Sstevel@tonic-gate mutex_enter(vphm); 49200Sstevel@tonic-gate 49210Sstevel@tonic-gate page_do_relocate_hash(pp_new, pp_old); 49220Sstevel@tonic-gate 49230Sstevel@tonic-gate mutex_exit(vphm); 49240Sstevel@tonic-gate mutex_exit(phm); 49250Sstevel@tonic-gate 49260Sstevel@tonic-gate /* 49270Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt and 49280Sstevel@tonic-gate * cowcnt since the page has an "exclusive" lock. 49290Sstevel@tonic-gate */ 49300Sstevel@tonic-gate ASSERT(pp_new->p_lckcnt == 0); 49310Sstevel@tonic-gate ASSERT(pp_new->p_cowcnt == 0); 49320Sstevel@tonic-gate pp_new->p_lckcnt = pp_old->p_lckcnt; 49330Sstevel@tonic-gate pp_new->p_cowcnt = pp_old->p_cowcnt; 49340Sstevel@tonic-gate pp_old->p_lckcnt = pp_old->p_cowcnt = 0; 49350Sstevel@tonic-gate 49360Sstevel@tonic-gate /* The following comment preserved from page_flip(). */ 49370Sstevel@tonic-gate /* XXX - Do we need to protect fsdata? */ 49380Sstevel@tonic-gate pp_new->p_fsdata = pp_old->p_fsdata; 49390Sstevel@tonic-gate } 49400Sstevel@tonic-gate 49410Sstevel@tonic-gate /* 49420Sstevel@tonic-gate * Helper routine used to lock all remaining members of a 49430Sstevel@tonic-gate * large page. The caller is responsible for passing in a locked 49440Sstevel@tonic-gate * pp. If pp is a large page, then it succeeds in locking all the 49450Sstevel@tonic-gate * remaining constituent pages or it returns with only the 49460Sstevel@tonic-gate * original page locked. 49470Sstevel@tonic-gate * 49480Sstevel@tonic-gate * Returns 1 on success, 0 on failure. 49490Sstevel@tonic-gate * 49500Sstevel@tonic-gate * If success is returned this routine gurantees p_szc for all constituent 49510Sstevel@tonic-gate * pages of a large page pp belongs to can't change. To achieve this we 49520Sstevel@tonic-gate * recheck szc of pp after locking all constituent pages and retry if szc 49530Sstevel@tonic-gate * changed (it could only decrease). Since hat_page_demote() needs an EXCL 49540Sstevel@tonic-gate * lock on one of constituent pages it can't be running after all constituent 49550Sstevel@tonic-gate * pages are locked. hat_page_demote() with a lock on a constituent page 49560Sstevel@tonic-gate * outside of this large page (i.e. pp belonged to a larger large page) is 49570Sstevel@tonic-gate * already done with all constituent pages of pp since the root's p_szc is 49580Sstevel@tonic-gate * changed last. Thefore no need to synchronize with hat_page_demote() that 49590Sstevel@tonic-gate * locked a constituent page outside of pp's current large page. 49600Sstevel@tonic-gate */ 49610Sstevel@tonic-gate #ifdef DEBUG 49620Sstevel@tonic-gate uint32_t gpg_trylock_mtbf = 0; 49630Sstevel@tonic-gate #endif 49640Sstevel@tonic-gate 49650Sstevel@tonic-gate int 49660Sstevel@tonic-gate group_page_trylock(page_t *pp, se_t se) 49670Sstevel@tonic-gate { 49680Sstevel@tonic-gate page_t *tpp; 49690Sstevel@tonic-gate pgcnt_t npgs, i, j; 49700Sstevel@tonic-gate uint_t pszc = pp->p_szc; 49710Sstevel@tonic-gate 49720Sstevel@tonic-gate #ifdef DEBUG 49730Sstevel@tonic-gate if (gpg_trylock_mtbf && !(gethrtime() % gpg_trylock_mtbf)) { 49740Sstevel@tonic-gate return (0); 49750Sstevel@tonic-gate } 49760Sstevel@tonic-gate #endif 49770Sstevel@tonic-gate 49780Sstevel@tonic-gate if (pp != PP_GROUPLEADER(pp, pszc)) { 49790Sstevel@tonic-gate return (0); 49800Sstevel@tonic-gate } 49810Sstevel@tonic-gate 49820Sstevel@tonic-gate retry: 49830Sstevel@tonic-gate ASSERT(PAGE_LOCKED_SE(pp, se)); 49840Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 49850Sstevel@tonic-gate if (pszc == 0) { 49860Sstevel@tonic-gate return (1); 49870Sstevel@tonic-gate } 49880Sstevel@tonic-gate npgs = page_get_pagecnt(pszc); 49890Sstevel@tonic-gate tpp = pp + 1; 49900Sstevel@tonic-gate for (i = 1; i < npgs; i++, tpp++) { 49910Sstevel@tonic-gate if (!page_trylock(tpp, se)) { 49920Sstevel@tonic-gate tpp = pp + 1; 49930Sstevel@tonic-gate for (j = 1; j < i; j++, tpp++) { 49940Sstevel@tonic-gate page_unlock(tpp); 49950Sstevel@tonic-gate } 49960Sstevel@tonic-gate return (0); 49970Sstevel@tonic-gate } 49980Sstevel@tonic-gate } 49990Sstevel@tonic-gate if (pp->p_szc != pszc) { 50000Sstevel@tonic-gate ASSERT(pp->p_szc < pszc); 50010Sstevel@tonic-gate ASSERT(pp->p_vnode != NULL && pp->p_vnode != &kvp && 50020Sstevel@tonic-gate !IS_SWAPFSVP(pp->p_vnode)); 50030Sstevel@tonic-gate tpp = pp + 1; 50040Sstevel@tonic-gate for (i = 1; i < npgs; i++, tpp++) { 50050Sstevel@tonic-gate page_unlock(tpp); 50060Sstevel@tonic-gate } 50070Sstevel@tonic-gate pszc = pp->p_szc; 50080Sstevel@tonic-gate goto retry; 50090Sstevel@tonic-gate } 50100Sstevel@tonic-gate return (1); 50110Sstevel@tonic-gate } 50120Sstevel@tonic-gate 50130Sstevel@tonic-gate void 50140Sstevel@tonic-gate group_page_unlock(page_t *pp) 50150Sstevel@tonic-gate { 50160Sstevel@tonic-gate page_t *tpp; 50170Sstevel@tonic-gate pgcnt_t npgs, i; 50180Sstevel@tonic-gate 50190Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 50200Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 50210Sstevel@tonic-gate ASSERT(pp == PP_PAGEROOT(pp)); 50220Sstevel@tonic-gate npgs = page_get_pagecnt(pp->p_szc); 50230Sstevel@tonic-gate for (i = 1, tpp = pp + 1; i < npgs; i++, tpp++) { 50240Sstevel@tonic-gate page_unlock(tpp); 50250Sstevel@tonic-gate } 50260Sstevel@tonic-gate } 50270Sstevel@tonic-gate 50280Sstevel@tonic-gate /* 50290Sstevel@tonic-gate * returns 50300Sstevel@tonic-gate * 0 : on success and *nrelocp is number of relocated PAGESIZE pages 50310Sstevel@tonic-gate * ERANGE : this is not a base page 50320Sstevel@tonic-gate * EBUSY : failure to get locks on the page/pages 50330Sstevel@tonic-gate * ENOMEM : failure to obtain replacement pages 50340Sstevel@tonic-gate * EAGAIN : OBP has not yet completed its boot-time handoff to the kernel 50350Sstevel@tonic-gate * 50360Sstevel@tonic-gate * Return with all constituent members of target and replacement 50370Sstevel@tonic-gate * SE_EXCL locked. It is the callers responsibility to drop the 50380Sstevel@tonic-gate * locks. 50390Sstevel@tonic-gate */ 50400Sstevel@tonic-gate int 50410Sstevel@tonic-gate do_page_relocate( 50420Sstevel@tonic-gate page_t **target, 50430Sstevel@tonic-gate page_t **replacement, 50440Sstevel@tonic-gate int grouplock, 50450Sstevel@tonic-gate spgcnt_t *nrelocp, 50460Sstevel@tonic-gate lgrp_t *lgrp) 50470Sstevel@tonic-gate { 50480Sstevel@tonic-gate #ifdef DEBUG 50490Sstevel@tonic-gate page_t *first_repl; 50500Sstevel@tonic-gate #endif /* DEBUG */ 50510Sstevel@tonic-gate page_t *repl; 50520Sstevel@tonic-gate page_t *targ; 50530Sstevel@tonic-gate page_t *pl = NULL; 50540Sstevel@tonic-gate uint_t ppattr; 50550Sstevel@tonic-gate pfn_t pfn, repl_pfn; 50560Sstevel@tonic-gate uint_t szc; 50570Sstevel@tonic-gate spgcnt_t npgs, i; 50580Sstevel@tonic-gate int repl_contig = 0; 50590Sstevel@tonic-gate uint_t flags = 0; 50600Sstevel@tonic-gate spgcnt_t dofree = 0; 50610Sstevel@tonic-gate 50620Sstevel@tonic-gate *nrelocp = 0; 50630Sstevel@tonic-gate 50640Sstevel@tonic-gate #if defined(__sparc) 50650Sstevel@tonic-gate /* 50660Sstevel@tonic-gate * We need to wait till OBP has completed 50670Sstevel@tonic-gate * its boot-time handoff of its resources to the kernel 50680Sstevel@tonic-gate * before we allow page relocation 50690Sstevel@tonic-gate */ 50700Sstevel@tonic-gate if (page_relocate_ready == 0) { 50710Sstevel@tonic-gate return (EAGAIN); 50720Sstevel@tonic-gate } 50730Sstevel@tonic-gate #endif 50740Sstevel@tonic-gate 50750Sstevel@tonic-gate /* 50760Sstevel@tonic-gate * If this is not a base page, 50770Sstevel@tonic-gate * just return with 0x0 pages relocated. 50780Sstevel@tonic-gate */ 50790Sstevel@tonic-gate targ = *target; 50800Sstevel@tonic-gate ASSERT(PAGE_EXCL(targ)); 50810Sstevel@tonic-gate ASSERT(!PP_ISFREE(targ)); 50820Sstevel@tonic-gate szc = targ->p_szc; 50830Sstevel@tonic-gate ASSERT(szc < mmu_page_sizes); 50840Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_reloc[szc]); 50850Sstevel@tonic-gate pfn = targ->p_pagenum; 50860Sstevel@tonic-gate if (pfn != PFN_BASE(pfn, szc)) { 50870Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnoroot[szc]); 50880Sstevel@tonic-gate return (ERANGE); 50890Sstevel@tonic-gate } 50900Sstevel@tonic-gate 50910Sstevel@tonic-gate if ((repl = *replacement) != NULL && repl->p_szc >= szc) { 50920Sstevel@tonic-gate repl_pfn = repl->p_pagenum; 50930Sstevel@tonic-gate if (repl_pfn != PFN_BASE(repl_pfn, szc)) { 50940Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_reloc_replnoroot[szc]); 50950Sstevel@tonic-gate return (ERANGE); 50960Sstevel@tonic-gate } 50970Sstevel@tonic-gate repl_contig = 1; 50980Sstevel@tonic-gate } 50990Sstevel@tonic-gate 51000Sstevel@tonic-gate /* 51010Sstevel@tonic-gate * We must lock all members of this large page or we cannot 51020Sstevel@tonic-gate * relocate any part of it. 51030Sstevel@tonic-gate */ 51040Sstevel@tonic-gate if (grouplock != 0 && !group_page_trylock(targ, SE_EXCL)) { 51050Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnolock[targ->p_szc]); 51060Sstevel@tonic-gate return (EBUSY); 51070Sstevel@tonic-gate } 51080Sstevel@tonic-gate 51090Sstevel@tonic-gate /* 51100Sstevel@tonic-gate * reread szc it could have been decreased before 51110Sstevel@tonic-gate * group_page_trylock() was done. 51120Sstevel@tonic-gate */ 51130Sstevel@tonic-gate szc = targ->p_szc; 51140Sstevel@tonic-gate ASSERT(szc < mmu_page_sizes); 51150Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_reloc[szc]); 51160Sstevel@tonic-gate ASSERT(pfn == PFN_BASE(pfn, szc)); 51170Sstevel@tonic-gate 51180Sstevel@tonic-gate npgs = page_get_pagecnt(targ->p_szc); 51190Sstevel@tonic-gate 51200Sstevel@tonic-gate if (repl == NULL) { 51210Sstevel@tonic-gate dofree = npgs; /* Size of target page in MMU pages */ 51220Sstevel@tonic-gate if (!page_create_wait(dofree, 0)) { 51230Sstevel@tonic-gate if (grouplock != 0) { 51240Sstevel@tonic-gate group_page_unlock(targ); 51250Sstevel@tonic-gate } 51260Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnomem[szc]); 51270Sstevel@tonic-gate return (ENOMEM); 51280Sstevel@tonic-gate } 51290Sstevel@tonic-gate 51300Sstevel@tonic-gate /* 51310Sstevel@tonic-gate * seg kmem pages require that the target and replacement 51320Sstevel@tonic-gate * page be the same pagesize. 51330Sstevel@tonic-gate */ 51340Sstevel@tonic-gate flags = (targ->p_vnode == &kvp) ? PGR_SAMESZC : 0; 51350Sstevel@tonic-gate repl = page_get_replacement_page(targ, lgrp, flags); 51360Sstevel@tonic-gate if (repl == NULL) { 51370Sstevel@tonic-gate if (grouplock != 0) { 51380Sstevel@tonic-gate group_page_unlock(targ); 51390Sstevel@tonic-gate } 51400Sstevel@tonic-gate page_create_putback(dofree); 51410Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnomem[szc]); 51420Sstevel@tonic-gate return (ENOMEM); 51430Sstevel@tonic-gate } 51440Sstevel@tonic-gate } 51450Sstevel@tonic-gate #ifdef DEBUG 51460Sstevel@tonic-gate else { 51470Sstevel@tonic-gate ASSERT(PAGE_LOCKED(repl)); 51480Sstevel@tonic-gate } 51490Sstevel@tonic-gate #endif /* DEBUG */ 51500Sstevel@tonic-gate 51510Sstevel@tonic-gate #if defined(__sparc) 51520Sstevel@tonic-gate /* 51530Sstevel@tonic-gate * Let hat_page_relocate() complete the relocation if it's kernel page 51540Sstevel@tonic-gate */ 51550Sstevel@tonic-gate if (targ->p_vnode == &kvp) { 51560Sstevel@tonic-gate *replacement = repl; 51570Sstevel@tonic-gate if (hat_page_relocate(target, replacement, nrelocp) != 0) { 51580Sstevel@tonic-gate if (grouplock != 0) { 51590Sstevel@tonic-gate group_page_unlock(targ); 51600Sstevel@tonic-gate } 51610Sstevel@tonic-gate if (dofree) { 51620Sstevel@tonic-gate *replacement = NULL; 51630Sstevel@tonic-gate page_free_replacement_page(repl); 51640Sstevel@tonic-gate page_create_putback(dofree); 51650Sstevel@tonic-gate } 51660Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_krelocfail[szc]); 51670Sstevel@tonic-gate return (EAGAIN); 51680Sstevel@tonic-gate } 51690Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocok[szc]); 51700Sstevel@tonic-gate return (0); 51710Sstevel@tonic-gate } 51720Sstevel@tonic-gate #else 51730Sstevel@tonic-gate #if defined(lint) 51740Sstevel@tonic-gate dofree = dofree; 51750Sstevel@tonic-gate #endif 51760Sstevel@tonic-gate #endif 51770Sstevel@tonic-gate 51780Sstevel@tonic-gate #ifdef DEBUG 51790Sstevel@tonic-gate first_repl = repl; 51800Sstevel@tonic-gate #endif /* DEBUG */ 51810Sstevel@tonic-gate 51820Sstevel@tonic-gate for (i = 0; i < npgs; i++) { 51830Sstevel@tonic-gate ASSERT(PAGE_EXCL(targ)); 51840Sstevel@tonic-gate 51850Sstevel@tonic-gate (void) hat_pageunload(targ, HAT_FORCE_PGUNLOAD); 51860Sstevel@tonic-gate 51870Sstevel@tonic-gate ASSERT(hat_page_getshare(targ) == 0); 51880Sstevel@tonic-gate ASSERT(!PP_ISFREE(targ)); 51890Sstevel@tonic-gate ASSERT(targ->p_pagenum == (pfn + i)); 51900Sstevel@tonic-gate ASSERT(repl_contig == 0 || 51910Sstevel@tonic-gate repl->p_pagenum == (repl_pfn + i)); 51920Sstevel@tonic-gate 51930Sstevel@tonic-gate /* 51940Sstevel@tonic-gate * Copy the page contents and attributes then 51950Sstevel@tonic-gate * relocate the page in the page hash. 51960Sstevel@tonic-gate */ 51970Sstevel@tonic-gate ppcopy(targ, repl); 51980Sstevel@tonic-gate ppattr = hat_page_getattr(targ, (P_MOD | P_REF | P_RO)); 51990Sstevel@tonic-gate page_clr_all_props(repl); 52000Sstevel@tonic-gate page_set_props(repl, ppattr); 52010Sstevel@tonic-gate page_relocate_hash(repl, targ); 52020Sstevel@tonic-gate 52030Sstevel@tonic-gate ASSERT(hat_page_getshare(targ) == 0); 52040Sstevel@tonic-gate ASSERT(hat_page_getshare(repl) == 0); 52050Sstevel@tonic-gate /* 52060Sstevel@tonic-gate * Now clear the props on targ, after the 52070Sstevel@tonic-gate * page_relocate_hash(), they no longer 52080Sstevel@tonic-gate * have any meaning. 52090Sstevel@tonic-gate */ 52100Sstevel@tonic-gate page_clr_all_props(targ); 52110Sstevel@tonic-gate ASSERT(targ->p_next == targ); 52120Sstevel@tonic-gate ASSERT(targ->p_prev == targ); 52130Sstevel@tonic-gate page_list_concat(&pl, &targ); 52140Sstevel@tonic-gate 52150Sstevel@tonic-gate targ++; 52160Sstevel@tonic-gate if (repl_contig != 0) { 52170Sstevel@tonic-gate repl++; 52180Sstevel@tonic-gate } else { 52190Sstevel@tonic-gate repl = repl->p_next; 52200Sstevel@tonic-gate } 52210Sstevel@tonic-gate } 52220Sstevel@tonic-gate /* assert that we have come full circle with repl */ 52230Sstevel@tonic-gate ASSERT(repl_contig == 1 || first_repl == repl); 52240Sstevel@tonic-gate 52250Sstevel@tonic-gate *target = pl; 52260Sstevel@tonic-gate if (*replacement == NULL) { 52270Sstevel@tonic-gate ASSERT(first_repl == repl); 52280Sstevel@tonic-gate *replacement = repl; 52290Sstevel@tonic-gate } 52300Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocok[szc]); 52310Sstevel@tonic-gate *nrelocp = npgs; 52320Sstevel@tonic-gate return (0); 52330Sstevel@tonic-gate } 52340Sstevel@tonic-gate /* 52350Sstevel@tonic-gate * On success returns 0 and *nrelocp the number of PAGESIZE pages relocated. 52360Sstevel@tonic-gate */ 52370Sstevel@tonic-gate int 52380Sstevel@tonic-gate page_relocate( 52390Sstevel@tonic-gate page_t **target, 52400Sstevel@tonic-gate page_t **replacement, 52410Sstevel@tonic-gate int grouplock, 52420Sstevel@tonic-gate int freetarget, 52430Sstevel@tonic-gate spgcnt_t *nrelocp, 52440Sstevel@tonic-gate lgrp_t *lgrp) 52450Sstevel@tonic-gate { 52460Sstevel@tonic-gate spgcnt_t ret; 52470Sstevel@tonic-gate 52480Sstevel@tonic-gate /* do_page_relocate returns 0 on success or errno value */ 52490Sstevel@tonic-gate ret = do_page_relocate(target, replacement, grouplock, nrelocp, lgrp); 52500Sstevel@tonic-gate 52510Sstevel@tonic-gate if (ret != 0 || freetarget == 0) { 52520Sstevel@tonic-gate return (ret); 52530Sstevel@tonic-gate } 52540Sstevel@tonic-gate if (*nrelocp == 1) { 52550Sstevel@tonic-gate ASSERT(*target != NULL); 52560Sstevel@tonic-gate page_free(*target, 1); 52570Sstevel@tonic-gate } else { 52580Sstevel@tonic-gate page_t *tpp = *target; 52590Sstevel@tonic-gate uint_t szc = tpp->p_szc; 52600Sstevel@tonic-gate pgcnt_t npgs = page_get_pagecnt(szc); 52610Sstevel@tonic-gate ASSERT(npgs > 1); 52620Sstevel@tonic-gate ASSERT(szc != 0); 52630Sstevel@tonic-gate do { 52640Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 52650Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp)); 52660Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 52670Sstevel@tonic-gate PP_SETFREE(tpp); 52680Sstevel@tonic-gate PP_SETAGED(tpp); 52690Sstevel@tonic-gate npgs--; 52700Sstevel@tonic-gate } while ((tpp = tpp->p_next) != *target); 52710Sstevel@tonic-gate ASSERT(npgs == 0); 52720Sstevel@tonic-gate page_list_add_pages(*target, 0); 52730Sstevel@tonic-gate npgs = page_get_pagecnt(szc); 52740Sstevel@tonic-gate page_create_putback(npgs); 52750Sstevel@tonic-gate } 52760Sstevel@tonic-gate return (ret); 52770Sstevel@tonic-gate } 52780Sstevel@tonic-gate 52790Sstevel@tonic-gate /* 52800Sstevel@tonic-gate * it is up to the caller to deal with pcf accounting. 52810Sstevel@tonic-gate */ 52820Sstevel@tonic-gate void 52830Sstevel@tonic-gate page_free_replacement_page(page_t *pplist) 52840Sstevel@tonic-gate { 52850Sstevel@tonic-gate page_t *pp; 52860Sstevel@tonic-gate 52870Sstevel@tonic-gate while (pplist != NULL) { 52880Sstevel@tonic-gate /* 52890Sstevel@tonic-gate * pp_targ is a linked list. 52900Sstevel@tonic-gate */ 52910Sstevel@tonic-gate pp = pplist; 52920Sstevel@tonic-gate if (pp->p_szc == 0) { 52930Sstevel@tonic-gate page_sub(&pplist, pp); 52940Sstevel@tonic-gate page_clr_all_props(pp); 52950Sstevel@tonic-gate PP_SETFREE(pp); 52960Sstevel@tonic-gate PP_SETAGED(pp); 52970Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 52980Sstevel@tonic-gate page_unlock(pp); 52990Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_replacement_page[0]); 53000Sstevel@tonic-gate } else { 53010Sstevel@tonic-gate spgcnt_t curnpgs = page_get_pagecnt(pp->p_szc); 53020Sstevel@tonic-gate page_t *tpp; 53030Sstevel@tonic-gate page_list_break(&pp, &pplist, curnpgs); 53040Sstevel@tonic-gate tpp = pp; 53050Sstevel@tonic-gate do { 53060Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 53070Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp)); 53080Sstevel@tonic-gate page_clr_all_props(pp); 53090Sstevel@tonic-gate PP_SETFREE(tpp); 53100Sstevel@tonic-gate PP_SETAGED(tpp); 53110Sstevel@tonic-gate } while ((tpp = tpp->p_next) != pp); 53120Sstevel@tonic-gate page_list_add_pages(pp, 0); 53130Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_replacement_page[1]); 53140Sstevel@tonic-gate } 53150Sstevel@tonic-gate } 53160Sstevel@tonic-gate } 53170Sstevel@tonic-gate 53180Sstevel@tonic-gate /* 53190Sstevel@tonic-gate * Relocate target to non-relocatable replacement page. 53200Sstevel@tonic-gate */ 53210Sstevel@tonic-gate int 53220Sstevel@tonic-gate page_relocate_cage(page_t **target, page_t **replacement) 53230Sstevel@tonic-gate { 53240Sstevel@tonic-gate page_t *tpp, *rpp; 53250Sstevel@tonic-gate spgcnt_t pgcnt, npgs; 53260Sstevel@tonic-gate int result; 53270Sstevel@tonic-gate 53280Sstevel@tonic-gate tpp = *target; 53290Sstevel@tonic-gate 53300Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 53310Sstevel@tonic-gate ASSERT(tpp->p_szc == 0); 53320Sstevel@tonic-gate 53330Sstevel@tonic-gate pgcnt = btop(page_get_pagesize(tpp->p_szc)); 53340Sstevel@tonic-gate 53350Sstevel@tonic-gate do { 53360Sstevel@tonic-gate (void) page_create_wait(pgcnt, PG_WAIT | PG_NORELOC); 53370Sstevel@tonic-gate rpp = page_get_replacement_page(tpp, NULL, PGR_NORELOC); 53380Sstevel@tonic-gate if (rpp == NULL) { 53390Sstevel@tonic-gate page_create_putback(pgcnt); 53400Sstevel@tonic-gate kcage_cageout_wakeup(); 53410Sstevel@tonic-gate } 53420Sstevel@tonic-gate } while (rpp == NULL); 53430Sstevel@tonic-gate 53440Sstevel@tonic-gate ASSERT(PP_ISNORELOC(rpp)); 53450Sstevel@tonic-gate 53460Sstevel@tonic-gate result = page_relocate(&tpp, &rpp, 0, 1, &npgs, NULL); 53470Sstevel@tonic-gate 53480Sstevel@tonic-gate if (result == 0) { 53490Sstevel@tonic-gate *replacement = rpp; 53500Sstevel@tonic-gate if (pgcnt != npgs) 53510Sstevel@tonic-gate panic("page_relocate_cage: partial relocation"); 53520Sstevel@tonic-gate } 53530Sstevel@tonic-gate 53540Sstevel@tonic-gate return (result); 53550Sstevel@tonic-gate } 53560Sstevel@tonic-gate 53570Sstevel@tonic-gate /* 53580Sstevel@tonic-gate * Release the page lock on a page, place on cachelist 53590Sstevel@tonic-gate * tail if no longer mapped. Caller can let us know if 53600Sstevel@tonic-gate * the page is known to be clean. 53610Sstevel@tonic-gate */ 53620Sstevel@tonic-gate int 53630Sstevel@tonic-gate page_release(page_t *pp, int checkmod) 53640Sstevel@tonic-gate { 53650Sstevel@tonic-gate int status; 53660Sstevel@tonic-gate 53670Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp) && !PP_ISFREE(pp) && 53680Sstevel@tonic-gate (pp->p_vnode != NULL)); 53690Sstevel@tonic-gate 53700Sstevel@tonic-gate if (!hat_page_is_mapped(pp) && !IS_SWAPVP(pp->p_vnode) && 53710Sstevel@tonic-gate ((PAGE_SHARED(pp) && page_tryupgrade(pp)) || PAGE_EXCL(pp)) && 53720Sstevel@tonic-gate pp->p_lckcnt == 0 && pp->p_cowcnt == 0 && 53730Sstevel@tonic-gate !hat_page_is_mapped(pp)) { 53740Sstevel@tonic-gate 53750Sstevel@tonic-gate /* 53760Sstevel@tonic-gate * If page is modified, unlock it 53770Sstevel@tonic-gate * 53780Sstevel@tonic-gate * (p_nrm & P_MOD) bit has the latest stuff because: 53790Sstevel@tonic-gate * (1) We found that this page doesn't have any mappings 53800Sstevel@tonic-gate * _after_ holding SE_EXCL and 53810Sstevel@tonic-gate * (2) We didn't drop SE_EXCL lock after the check in (1) 53820Sstevel@tonic-gate */ 53830Sstevel@tonic-gate if (checkmod && hat_ismod(pp)) { 53840Sstevel@tonic-gate page_unlock(pp); 53850Sstevel@tonic-gate status = PGREL_MOD; 53860Sstevel@tonic-gate } else { 53870Sstevel@tonic-gate /*LINTED: constant in conditional context*/ 53880Sstevel@tonic-gate VN_DISPOSE(pp, B_FREE, 0, kcred); 53890Sstevel@tonic-gate status = PGREL_CLEAN; 53900Sstevel@tonic-gate } 53910Sstevel@tonic-gate } else { 53920Sstevel@tonic-gate page_unlock(pp); 53930Sstevel@tonic-gate status = PGREL_NOTREL; 53940Sstevel@tonic-gate } 53950Sstevel@tonic-gate return (status); 53960Sstevel@tonic-gate } 53970Sstevel@tonic-gate 53980Sstevel@tonic-gate int 53990Sstevel@tonic-gate page_try_demote_pages(page_t *pp) 54000Sstevel@tonic-gate { 54010Sstevel@tonic-gate page_t *tpp, *rootpp = pp; 54020Sstevel@tonic-gate pfn_t pfn = page_pptonum(pp); 54030Sstevel@tonic-gate spgcnt_t i, npgs; 54040Sstevel@tonic-gate uint_t szc = pp->p_szc; 54050Sstevel@tonic-gate vnode_t *vp = pp->p_vnode; 54060Sstevel@tonic-gate 54070Sstevel@tonic-gate ASSERT(PAGE_EXCL(rootpp)); 54080Sstevel@tonic-gate 54090Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[0]); 54100Sstevel@tonic-gate 54110Sstevel@tonic-gate if (rootpp->p_szc == 0) { 54120Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[1]); 54130Sstevel@tonic-gate return (1); 54140Sstevel@tonic-gate } 54150Sstevel@tonic-gate 54160Sstevel@tonic-gate if (vp != NULL && !IS_SWAPFSVP(vp) && vp != &kvp) { 54170Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[2]); 54180Sstevel@tonic-gate page_demote_vp_pages(rootpp); 54190Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 54200Sstevel@tonic-gate return (1); 54210Sstevel@tonic-gate } 54220Sstevel@tonic-gate 54230Sstevel@tonic-gate /* 54240Sstevel@tonic-gate * Adjust rootpp if passed in is not the base 54250Sstevel@tonic-gate * constituent page. 54260Sstevel@tonic-gate */ 54270Sstevel@tonic-gate npgs = page_get_pagecnt(rootpp->p_szc); 54280Sstevel@tonic-gate ASSERT(npgs > 1); 54290Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, npgs)) { 54300Sstevel@tonic-gate pfn = P2ALIGN(pfn, npgs); 54310Sstevel@tonic-gate rootpp = page_numtopp_nolock(pfn); 54320Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[3]); 54330Sstevel@tonic-gate ASSERT(rootpp->p_vnode != NULL); 54340Sstevel@tonic-gate ASSERT(rootpp->p_szc == szc); 54350Sstevel@tonic-gate } 54360Sstevel@tonic-gate 54370Sstevel@tonic-gate /* 54380Sstevel@tonic-gate * We can't demote kernel pages since we can't hat_unload() 54390Sstevel@tonic-gate * the mappings. 54400Sstevel@tonic-gate */ 54410Sstevel@tonic-gate if (rootpp->p_vnode == &kvp) 54420Sstevel@tonic-gate return (0); 54430Sstevel@tonic-gate 54440Sstevel@tonic-gate /* 54450Sstevel@tonic-gate * Attempt to lock all constituent pages except the page passed 54460Sstevel@tonic-gate * in since it's already locked. 54470Sstevel@tonic-gate */ 54480Sstevel@tonic-gate for (tpp = rootpp, i = 0; i < npgs; i++, tpp = page_next(tpp)) { 54490Sstevel@tonic-gate ASSERT(!PP_ISFREE(tpp)); 54500Sstevel@tonic-gate ASSERT(tpp->p_vnode != NULL); 54510Sstevel@tonic-gate 54520Sstevel@tonic-gate if (tpp != pp && !page_trylock(tpp, SE_EXCL)) 54530Sstevel@tonic-gate break; 54540Sstevel@tonic-gate ASSERT(tpp->p_szc == rootpp->p_szc); 54550Sstevel@tonic-gate ASSERT(page_pptonum(tpp) == page_pptonum(rootpp) + i); 54560Sstevel@tonic-gate (void) hat_pageunload(tpp, HAT_FORCE_PGUNLOAD); 54570Sstevel@tonic-gate } 54580Sstevel@tonic-gate 54590Sstevel@tonic-gate /* 54600Sstevel@tonic-gate * If we failed to lock them all then unlock what we have locked 54610Sstevel@tonic-gate * so far and bail. 54620Sstevel@tonic-gate */ 54630Sstevel@tonic-gate if (i < npgs) { 54640Sstevel@tonic-gate tpp = rootpp; 54650Sstevel@tonic-gate while (i-- > 0) { 54660Sstevel@tonic-gate if (tpp != pp) 54670Sstevel@tonic-gate page_unlock(tpp); 54680Sstevel@tonic-gate tpp = page_next(tpp); 54690Sstevel@tonic-gate } 54700Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[4]); 54710Sstevel@tonic-gate return (0); 54720Sstevel@tonic-gate } 54730Sstevel@tonic-gate 54740Sstevel@tonic-gate /* 54750Sstevel@tonic-gate * XXX probably p_szc clearing and page unlocking can be done within 54760Sstevel@tonic-gate * one loop but since this is rare code we can play very safe. 54770Sstevel@tonic-gate */ 54780Sstevel@tonic-gate for (tpp = rootpp, i = 0; i < npgs; i++, tpp = page_next(tpp)) { 54790Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 54800Sstevel@tonic-gate tpp->p_szc = 0; 54810Sstevel@tonic-gate } 54820Sstevel@tonic-gate 54830Sstevel@tonic-gate /* 54840Sstevel@tonic-gate * Unlock all pages except the page passed in. 54850Sstevel@tonic-gate */ 54860Sstevel@tonic-gate for (tpp = rootpp, i = 0; i < npgs; i++, tpp = page_next(tpp)) { 54870Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp)); 54880Sstevel@tonic-gate if (tpp != pp) 54890Sstevel@tonic-gate page_unlock(tpp); 54900Sstevel@tonic-gate } 54910Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[5]); 54920Sstevel@tonic-gate return (1); 54930Sstevel@tonic-gate } 54940Sstevel@tonic-gate 54950Sstevel@tonic-gate /* 54960Sstevel@tonic-gate * Called by page_free() and page_destroy() to demote the page size code 54970Sstevel@tonic-gate * (p_szc) to 0 (since we can't just put a single PAGESIZE page with non zero 54980Sstevel@tonic-gate * p_szc on free list, neither can we just clear p_szc of a single page_t 54990Sstevel@tonic-gate * within a large page since it will break other code that relies on p_szc 55000Sstevel@tonic-gate * being the same for all page_t's of a large page). Anonymous pages should 55010Sstevel@tonic-gate * never end up here because anon_map_getpages() cannot deal with p_szc 55020Sstevel@tonic-gate * changes after a single constituent page is locked. While anonymous or 55030Sstevel@tonic-gate * kernel large pages are demoted or freed the entire large page at a time 55040Sstevel@tonic-gate * with all constituent pages locked EXCL for the file system pages we 55050Sstevel@tonic-gate * have to be able to demote a large page (i.e. decrease all constituent pages 55060Sstevel@tonic-gate * p_szc) with only just an EXCL lock on one of constituent pages. The reason 55070Sstevel@tonic-gate * we can easily deal with anonymous page demotion the entire large page at a 55080Sstevel@tonic-gate * time is that those operation originate at address space level and concern 55090Sstevel@tonic-gate * the entire large page region with actual demotion only done when pages are 55100Sstevel@tonic-gate * not shared with any other processes (therefore we can always get EXCL lock 55110Sstevel@tonic-gate * on all anonymous constituent pages after clearing segment page 55120Sstevel@tonic-gate * cache). However file system pages can be truncated or invalidated at a 55130Sstevel@tonic-gate * PAGESIZE level from the file system side and end up in page_free() or 55140Sstevel@tonic-gate * page_destroy() (we also allow only part of the large page to be SOFTLOCKed 55150Sstevel@tonic-gate * and therfore pageout should be able to demote a large page by EXCL locking 55160Sstevel@tonic-gate * any constituent page that is not under SOFTLOCK). In those cases we cannot 55170Sstevel@tonic-gate * rely on being able to lock EXCL all constituent pages. 55180Sstevel@tonic-gate * 55190Sstevel@tonic-gate * To prevent szc changes on file system pages one has to lock all constituent 55200Sstevel@tonic-gate * pages at least SHARED (or call page_szc_lock()). The only subsystem that 55210Sstevel@tonic-gate * doesn't rely on locking all constituent pages (or using page_szc_lock()) to 55220Sstevel@tonic-gate * prevent szc changes is hat layer that uses its own page level mlist 55230Sstevel@tonic-gate * locks. hat assumes that szc doesn't change after mlist lock for a page is 55240Sstevel@tonic-gate * taken. Therefore we need to change szc under hat level locks if we only 55250Sstevel@tonic-gate * have an EXCL lock on a single constituent page and hat still references any 55260Sstevel@tonic-gate * of constituent pages. (Note we can't "ignore" hat layer by simply 55270Sstevel@tonic-gate * hat_pageunload() all constituent pages without having EXCL locks on all of 55280Sstevel@tonic-gate * constituent pages). We use hat_page_demote() call to safely demote szc of 55290Sstevel@tonic-gate * all constituent pages under hat locks when we only have an EXCL lock on one 55300Sstevel@tonic-gate * of constituent pages. 55310Sstevel@tonic-gate * 55320Sstevel@tonic-gate * This routine calls page_szc_lock() before calling hat_page_demote() to 55330Sstevel@tonic-gate * allow segvn in one special case not to lock all constituent pages SHARED 55340Sstevel@tonic-gate * before calling hat_memload_array() that relies on p_szc not changeing even 55350Sstevel@tonic-gate * before hat level mlist lock is taken. In that case segvn uses 55360Sstevel@tonic-gate * page_szc_lock() to prevent hat_page_demote() changeing p_szc values. 55370Sstevel@tonic-gate * 55380Sstevel@tonic-gate * Anonymous or kernel page demotion still has to lock all pages exclusively 55390Sstevel@tonic-gate * and do hat_pageunload() on all constituent pages before demoting the page 55400Sstevel@tonic-gate * therefore there's no need for anonymous or kernel page demotion to use 55410Sstevel@tonic-gate * hat_page_demote() mechanism. 55420Sstevel@tonic-gate * 55430Sstevel@tonic-gate * hat_page_demote() removes all large mappings that map pp and then decreases 55440Sstevel@tonic-gate * p_szc starting from the last constituent page of the large page. By working 55450Sstevel@tonic-gate * from the tail of a large page in pfn decreasing order allows one looking at 55460Sstevel@tonic-gate * the root page to know that hat_page_demote() is done for root's szc area. 55470Sstevel@tonic-gate * e.g. if a root page has szc 1 one knows it only has to lock all constituent 55480Sstevel@tonic-gate * pages within szc 1 area to prevent szc changes because hat_page_demote() 55490Sstevel@tonic-gate * that started on this page when it had szc > 1 is done for this szc 1 area. 55500Sstevel@tonic-gate * 55510Sstevel@tonic-gate * We are guranteed that all constituent pages of pp's large page belong to 55520Sstevel@tonic-gate * the same vnode with the consecutive offsets increasing in the direction of 55530Sstevel@tonic-gate * the pfn i.e. the identity of constituent pages can't change until their 55540Sstevel@tonic-gate * p_szc is decreased. Therefore it's safe for hat_page_demote() to remove 55550Sstevel@tonic-gate * large mappings to pp even though we don't lock any constituent page except 55560Sstevel@tonic-gate * pp (i.e. we won't unload e.g. kernel locked page). 55570Sstevel@tonic-gate */ 55580Sstevel@tonic-gate static void 55590Sstevel@tonic-gate page_demote_vp_pages(page_t *pp) 55600Sstevel@tonic-gate { 55610Sstevel@tonic-gate kmutex_t *mtx; 55620Sstevel@tonic-gate 55630Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 55640Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 55650Sstevel@tonic-gate ASSERT(pp->p_vnode != NULL); 55660Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(pp->p_vnode)); 55670Sstevel@tonic-gate ASSERT(pp->p_vnode != &kvp); 55680Sstevel@tonic-gate 55690Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_demote_pages[0]); 55700Sstevel@tonic-gate 55710Sstevel@tonic-gate mtx = page_szc_lock(pp); 55720Sstevel@tonic-gate if (mtx != NULL) { 55730Sstevel@tonic-gate hat_page_demote(pp); 55740Sstevel@tonic-gate mutex_exit(mtx); 55750Sstevel@tonic-gate } 55760Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 55770Sstevel@tonic-gate } 55780Sstevel@tonic-gate 55790Sstevel@tonic-gate /* 55800Sstevel@tonic-gate * Page retire operation. 55810Sstevel@tonic-gate * 55820Sstevel@tonic-gate * page_retire() 55830Sstevel@tonic-gate * Attempt to retire (throw away) page pp. We cannot do this if 55840Sstevel@tonic-gate * the page is dirty; if the page is clean, we can try. We return 0 on 55850Sstevel@tonic-gate * success, -1 on failure. This routine should be invoked by the platform's 55860Sstevel@tonic-gate * memory error detection code. 55870Sstevel@tonic-gate * 55880Sstevel@tonic-gate * pages_retired_limit_exceeded() 55890Sstevel@tonic-gate * We set a limit on the number of pages which may be retired. This 55900Sstevel@tonic-gate * is set to a percentage of total physical memory. This limit is 55910Sstevel@tonic-gate * enforced here. 55920Sstevel@tonic-gate */ 55930Sstevel@tonic-gate 55940Sstevel@tonic-gate static pgcnt_t retired_pgcnt = 0; 55950Sstevel@tonic-gate 55960Sstevel@tonic-gate /* 55970Sstevel@tonic-gate * routines to update the count of retired pages 55980Sstevel@tonic-gate */ 55990Sstevel@tonic-gate static void 56000Sstevel@tonic-gate page_retired(page_t *pp) 56010Sstevel@tonic-gate { 56020Sstevel@tonic-gate ASSERT(pp); 56030Sstevel@tonic-gate 56040Sstevel@tonic-gate page_settoxic(pp, PAGE_IS_RETIRED); 56050Sstevel@tonic-gate atomic_add_long(&retired_pgcnt, 1); 56060Sstevel@tonic-gate } 56070Sstevel@tonic-gate 56080Sstevel@tonic-gate static void 56090Sstevel@tonic-gate retired_page_removed(page_t *pp) 56100Sstevel@tonic-gate { 56110Sstevel@tonic-gate ASSERT(pp); 56120Sstevel@tonic-gate ASSERT(page_isretired(pp)); 56130Sstevel@tonic-gate ASSERT(retired_pgcnt > 0); 56140Sstevel@tonic-gate 56150Sstevel@tonic-gate page_clrtoxic(pp); 56160Sstevel@tonic-gate atomic_add_long(&retired_pgcnt, -1); 56170Sstevel@tonic-gate } 56180Sstevel@tonic-gate 56190Sstevel@tonic-gate 56200Sstevel@tonic-gate static int 56210Sstevel@tonic-gate pages_retired_limit_exceeded() 56220Sstevel@tonic-gate { 56230Sstevel@tonic-gate pgcnt_t retired_max; 56240Sstevel@tonic-gate 56250Sstevel@tonic-gate /* 56260Sstevel@tonic-gate * If the percentage is zero or is not set correctly, 56270Sstevel@tonic-gate * return TRUE so that pages are not retired. 56280Sstevel@tonic-gate */ 56290Sstevel@tonic-gate if (max_pages_retired_bps <= 0 || 56300Sstevel@tonic-gate max_pages_retired_bps >= 10000) 56310Sstevel@tonic-gate return (1); 56320Sstevel@tonic-gate 56330Sstevel@tonic-gate /* 56340Sstevel@tonic-gate * Calculate the maximum number of pages allowed to 56350Sstevel@tonic-gate * be retired as a percentage of total physical memory 56360Sstevel@tonic-gate * (Remember that we are using basis points, hence the 10000.) 56370Sstevel@tonic-gate */ 56380Sstevel@tonic-gate retired_max = (physmem * max_pages_retired_bps) / 10000; 56390Sstevel@tonic-gate 56400Sstevel@tonic-gate /* 56410Sstevel@tonic-gate * return 'TRUE' if we have already retired more 56420Sstevel@tonic-gate * than the legal limit 56430Sstevel@tonic-gate */ 56440Sstevel@tonic-gate return (retired_pgcnt >= retired_max); 56450Sstevel@tonic-gate } 56460Sstevel@tonic-gate 56470Sstevel@tonic-gate #define PAGE_RETIRE_SELOCK 0 56480Sstevel@tonic-gate #define PAGE_RETIRE_NORECLAIM 1 56490Sstevel@tonic-gate #define PAGE_RETIRE_LOCKED 2 56500Sstevel@tonic-gate #define PAGE_RETIRE_COW 3 56510Sstevel@tonic-gate #define PAGE_RETIRE_DIRTY 4 56520Sstevel@tonic-gate #define PAGE_RETIRE_LPAGE 5 56530Sstevel@tonic-gate #define PAGE_RETIRE_SUCCESS 6 56540Sstevel@tonic-gate #define PAGE_RETIRE_LIMIT 7 56550Sstevel@tonic-gate #define PAGE_RETIRE_NCODES 8 56560Sstevel@tonic-gate 56570Sstevel@tonic-gate typedef struct page_retire_op { 56580Sstevel@tonic-gate int pr_count; 56590Sstevel@tonic-gate short pr_unlock; 56600Sstevel@tonic-gate short pr_retval; 56610Sstevel@tonic-gate char *pr_message; 56620Sstevel@tonic-gate } page_retire_op_t; 56630Sstevel@tonic-gate 56640Sstevel@tonic-gate page_retire_op_t page_retire_ops[PAGE_RETIRE_NCODES] = { 56650Sstevel@tonic-gate { 0, 0, -1, "cannot lock page" }, 56660Sstevel@tonic-gate { 0, 0, -1, "cannot reclaim cached page" }, 56670Sstevel@tonic-gate { 0, 1, -1, "page is locked" }, 56680Sstevel@tonic-gate { 0, 1, -1, "copy-on-write page" }, 56690Sstevel@tonic-gate { 0, 1, -1, "page is dirty" }, 56700Sstevel@tonic-gate { 0, 1, -1, "cannot demote large page" }, 56710Sstevel@tonic-gate { 0, 0, 0, "page successfully retired" }, 56720Sstevel@tonic-gate { 0, 0, -1, "excess pages retired already" }, 56730Sstevel@tonic-gate }; 56740Sstevel@tonic-gate 56750Sstevel@tonic-gate static int 56760Sstevel@tonic-gate page_retire_done(page_t *pp, int code) 56770Sstevel@tonic-gate { 56780Sstevel@tonic-gate page_retire_op_t *prop = &page_retire_ops[code]; 56790Sstevel@tonic-gate 56800Sstevel@tonic-gate prop->pr_count++; 56810Sstevel@tonic-gate 56820Sstevel@tonic-gate if (prop->pr_unlock) 56830Sstevel@tonic-gate page_unlock(pp); 56840Sstevel@tonic-gate 56850Sstevel@tonic-gate if (page_retire_messages > 1) { 56860Sstevel@tonic-gate printf("page_retire(%p) pfn 0x%lx %s: %s\n", 56870Sstevel@tonic-gate (void *)pp, page_pptonum(pp), 56880Sstevel@tonic-gate prop->pr_retval == -1 ? "failed" : "succeeded", 56890Sstevel@tonic-gate prop->pr_message); 56900Sstevel@tonic-gate } 56910Sstevel@tonic-gate 56920Sstevel@tonic-gate return (prop->pr_retval); 56930Sstevel@tonic-gate } 56940Sstevel@tonic-gate 56950Sstevel@tonic-gate int 56960Sstevel@tonic-gate page_retire(page_t *pp, uchar_t flag) 56970Sstevel@tonic-gate { 56980Sstevel@tonic-gate uint64_t pa = ptob((uint64_t)page_pptonum(pp)); 56990Sstevel@tonic-gate 57000Sstevel@tonic-gate ASSERT(flag == PAGE_IS_FAILING || flag == PAGE_IS_TOXIC); 57010Sstevel@tonic-gate 57020Sstevel@tonic-gate /* 57030Sstevel@tonic-gate * DR operations change the association between a page_t 57040Sstevel@tonic-gate * and the physical page it represents. Check if the 57050Sstevel@tonic-gate * page is still bad. 57060Sstevel@tonic-gate */ 57070Sstevel@tonic-gate if (!page_isfaulty(pp)) { 57080Sstevel@tonic-gate page_clrtoxic(pp); 57090Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_SUCCESS)); 57100Sstevel@tonic-gate } 57110Sstevel@tonic-gate 57120Sstevel@tonic-gate /* 57130Sstevel@tonic-gate * We set the flag here so that even if we fail due 57140Sstevel@tonic-gate * to exceeding the limit for retired pages, the 57150Sstevel@tonic-gate * page will still be checked and either cleared 57160Sstevel@tonic-gate * or retired in page_free(). 57170Sstevel@tonic-gate */ 57180Sstevel@tonic-gate page_settoxic(pp, flag); 57190Sstevel@tonic-gate 57200Sstevel@tonic-gate if (flag == PAGE_IS_TOXIC) { 57210Sstevel@tonic-gate if (page_retire_messages) { 57220Sstevel@tonic-gate cmn_err(CE_NOTE, "Scheduling clearing of error on" 57230Sstevel@tonic-gate " page 0x%08x.%08x", 57240Sstevel@tonic-gate (uint32_t)(pa >> 32), (uint32_t)pa); 57250Sstevel@tonic-gate } 57260Sstevel@tonic-gate 57270Sstevel@tonic-gate } else { /* PAGE_IS_FAILING */ 57280Sstevel@tonic-gate if (pages_retired_limit_exceeded()) { 57290Sstevel@tonic-gate /* 57300Sstevel@tonic-gate * Return as we have already exceeded the 57310Sstevel@tonic-gate * maximum number of pages allowed to be 57320Sstevel@tonic-gate * retired 57330Sstevel@tonic-gate */ 57340Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_LIMIT)); 57350Sstevel@tonic-gate } 57360Sstevel@tonic-gate 57370Sstevel@tonic-gate if (page_retire_messages) { 57380Sstevel@tonic-gate cmn_err(CE_NOTE, "Scheduling removal of " 57390Sstevel@tonic-gate "page 0x%08x.%08x", 57400Sstevel@tonic-gate (uint32_t)(pa >> 32), (uint32_t)pa); 57410Sstevel@tonic-gate } 57420Sstevel@tonic-gate } 57430Sstevel@tonic-gate 57440Sstevel@tonic-gate if (PAGE_LOCKED(pp) || !page_trylock(pp, SE_EXCL)) 57450Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_SELOCK)); 57460Sstevel@tonic-gate 57470Sstevel@tonic-gate /* 57480Sstevel@tonic-gate * If this is a large page we first try and demote it 57490Sstevel@tonic-gate * to PAGESIZE pages and then dispose of the toxic page. 57500Sstevel@tonic-gate * On failure we will let the page free/destroy 57510Sstevel@tonic-gate * code handle it later since this is a mapped page. 57520Sstevel@tonic-gate * Note that free large pages can always be demoted. 57530Sstevel@tonic-gate * 57540Sstevel@tonic-gate */ 57550Sstevel@tonic-gate if (pp->p_szc != 0) { 57560Sstevel@tonic-gate if (PP_ISFREE(pp)) 57570Sstevel@tonic-gate (void) page_demote_free_pages(pp); 57580Sstevel@tonic-gate else 57590Sstevel@tonic-gate (void) page_try_demote_pages(pp); 57600Sstevel@tonic-gate 57610Sstevel@tonic-gate if (pp->p_szc != 0) 57620Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_LPAGE)); 57630Sstevel@tonic-gate } 57640Sstevel@tonic-gate 57650Sstevel@tonic-gate if (PP_ISFREE(pp)) { 57660Sstevel@tonic-gate if (!page_reclaim(pp, NULL)) 57670Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_NORECLAIM)); 57680Sstevel@tonic-gate /*LINTED: constant in conditional context*/ 57690Sstevel@tonic-gate VN_DISPOSE(pp, pp->p_vnode ? B_INVAL : B_FREE, 0, kcred) 57700Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_SUCCESS)); 57710Sstevel@tonic-gate } 57720Sstevel@tonic-gate 57730Sstevel@tonic-gate if (pp->p_lckcnt != 0) 57740Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_LOCKED)); 57750Sstevel@tonic-gate 57760Sstevel@tonic-gate if (pp->p_cowcnt != 0) 57770Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_COW)); 57780Sstevel@tonic-gate 57790Sstevel@tonic-gate /* 57800Sstevel@tonic-gate * Unload all translations to this page. No new translations 57810Sstevel@tonic-gate * can be created while we hold the exclusive lock on the page. 57820Sstevel@tonic-gate */ 57830Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 57840Sstevel@tonic-gate 57850Sstevel@tonic-gate if (hat_ismod(pp)) 57860Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_DIRTY)); 57870Sstevel@tonic-gate 57880Sstevel@tonic-gate /*LINTED: constant in conditional context*/ 57890Sstevel@tonic-gate VN_DISPOSE(pp, B_INVAL, 0, kcred); 57900Sstevel@tonic-gate 57910Sstevel@tonic-gate return (page_retire_done(pp, PAGE_RETIRE_SUCCESS)); 57920Sstevel@tonic-gate } 57930Sstevel@tonic-gate 57940Sstevel@tonic-gate /* 57950Sstevel@tonic-gate * Mark any existing pages for migration in the given range 57960Sstevel@tonic-gate */ 57970Sstevel@tonic-gate void 57980Sstevel@tonic-gate page_mark_migrate(struct seg *seg, caddr_t addr, size_t len, 57990Sstevel@tonic-gate struct anon_map *amp, ulong_t anon_index, vnode_t *vp, 58000Sstevel@tonic-gate u_offset_t vnoff, int rflag) 58010Sstevel@tonic-gate { 58020Sstevel@tonic-gate struct anon *ap; 58030Sstevel@tonic-gate vnode_t *curvp; 58040Sstevel@tonic-gate lgrp_t *from; 58050Sstevel@tonic-gate pgcnt_t i; 58060Sstevel@tonic-gate pgcnt_t nlocked; 58070Sstevel@tonic-gate u_offset_t off; 58080Sstevel@tonic-gate pfn_t pfn; 58090Sstevel@tonic-gate size_t pgsz; 58100Sstevel@tonic-gate size_t segpgsz; 58110Sstevel@tonic-gate pgcnt_t pages; 58120Sstevel@tonic-gate uint_t pszc; 58130Sstevel@tonic-gate page_t **ppa; 58140Sstevel@tonic-gate pgcnt_t ppa_nentries; 58150Sstevel@tonic-gate page_t *pp; 58160Sstevel@tonic-gate caddr_t va; 58170Sstevel@tonic-gate ulong_t an_idx; 58180Sstevel@tonic-gate anon_sync_obj_t cookie; 58190Sstevel@tonic-gate 58200Sstevel@tonic-gate ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 58210Sstevel@tonic-gate 58220Sstevel@tonic-gate /* 58230Sstevel@tonic-gate * Don't do anything if don't need to do lgroup optimizations 58240Sstevel@tonic-gate * on this system 58250Sstevel@tonic-gate */ 58260Sstevel@tonic-gate if (!lgrp_optimizations()) 58270Sstevel@tonic-gate return; 58280Sstevel@tonic-gate 58290Sstevel@tonic-gate /* 58300Sstevel@tonic-gate * Align address and length to (potentially large) page boundary 58310Sstevel@tonic-gate */ 58320Sstevel@tonic-gate segpgsz = page_get_pagesize(seg->s_szc); 58330Sstevel@tonic-gate addr = (caddr_t)P2ALIGN((uintptr_t)addr, segpgsz); 58340Sstevel@tonic-gate if (rflag) 58350Sstevel@tonic-gate len = P2ROUNDUP(len, segpgsz); 58360Sstevel@tonic-gate 58370Sstevel@tonic-gate /* 58380Sstevel@tonic-gate * Allocate page array to accomodate largest page size 58390Sstevel@tonic-gate */ 58400Sstevel@tonic-gate pgsz = page_get_pagesize(page_num_pagesizes() - 1); 58410Sstevel@tonic-gate ppa_nentries = btop(pgsz); 58420Sstevel@tonic-gate ppa = kmem_zalloc(ppa_nentries * sizeof (page_t *), KM_SLEEP); 58430Sstevel@tonic-gate 58440Sstevel@tonic-gate /* 58450Sstevel@tonic-gate * Do one (large) page at a time 58460Sstevel@tonic-gate */ 58470Sstevel@tonic-gate va = addr; 58480Sstevel@tonic-gate while (va < addr + len) { 58490Sstevel@tonic-gate /* 58500Sstevel@tonic-gate * Lookup (root) page for vnode and offset corresponding to 58510Sstevel@tonic-gate * this virtual address 58520Sstevel@tonic-gate * Try anonmap first since there may be copy-on-write 58530Sstevel@tonic-gate * pages, but initialize vnode pointer and offset using 58540Sstevel@tonic-gate * vnode arguments just in case there isn't an amp. 58550Sstevel@tonic-gate */ 58560Sstevel@tonic-gate curvp = vp; 58570Sstevel@tonic-gate off = vnoff + va - seg->s_base; 58580Sstevel@tonic-gate if (amp) { 58590Sstevel@tonic-gate ANON_LOCK_ENTER(&->a_rwlock, RW_READER); 58600Sstevel@tonic-gate an_idx = anon_index + seg_page(seg, va); 58610Sstevel@tonic-gate anon_array_enter(amp, an_idx, &cookie); 58620Sstevel@tonic-gate ap = anon_get_ptr(amp->ahp, an_idx); 58630Sstevel@tonic-gate if (ap) 58640Sstevel@tonic-gate swap_xlate(ap, &curvp, &off); 58650Sstevel@tonic-gate anon_array_exit(&cookie); 58660Sstevel@tonic-gate ANON_LOCK_EXIT(&->a_rwlock); 58670Sstevel@tonic-gate } 58680Sstevel@tonic-gate 58690Sstevel@tonic-gate pp = NULL; 58700Sstevel@tonic-gate if (curvp) 58710Sstevel@tonic-gate pp = page_lookup(curvp, off, SE_SHARED); 58720Sstevel@tonic-gate 58730Sstevel@tonic-gate /* 58740Sstevel@tonic-gate * If there isn't a page at this virtual address, 58750Sstevel@tonic-gate * skip to next page 58760Sstevel@tonic-gate */ 58770Sstevel@tonic-gate if (pp == NULL) { 58780Sstevel@tonic-gate va += PAGESIZE; 58790Sstevel@tonic-gate continue; 58800Sstevel@tonic-gate } 58810Sstevel@tonic-gate 58820Sstevel@tonic-gate /* 58830Sstevel@tonic-gate * Figure out which lgroup this page is in for kstats 58840Sstevel@tonic-gate */ 58850Sstevel@tonic-gate pfn = page_pptonum(pp); 58860Sstevel@tonic-gate from = lgrp_pfn_to_lgrp(pfn); 58870Sstevel@tonic-gate 58880Sstevel@tonic-gate /* 58890Sstevel@tonic-gate * Get page size, and round up and skip to next page boundary 58900Sstevel@tonic-gate * if unaligned address 58910Sstevel@tonic-gate */ 58920Sstevel@tonic-gate pszc = pp->p_szc; 58930Sstevel@tonic-gate pgsz = page_get_pagesize(pszc); 58940Sstevel@tonic-gate pages = btop(pgsz); 58950Sstevel@tonic-gate if (!IS_P2ALIGNED(va, pgsz) || 58960Sstevel@tonic-gate !IS_P2ALIGNED(pfn, pages) || 58970Sstevel@tonic-gate pgsz > segpgsz) { 58980Sstevel@tonic-gate pgsz = MIN(pgsz, segpgsz); 58990Sstevel@tonic-gate page_unlock(pp); 59000Sstevel@tonic-gate i = btop(P2END((uintptr_t)va, pgsz) - 59010Sstevel@tonic-gate (uintptr_t)va); 59020Sstevel@tonic-gate va = (caddr_t)P2END((uintptr_t)va, pgsz); 59030Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, i); 59040Sstevel@tonic-gate continue; 59050Sstevel@tonic-gate } 59060Sstevel@tonic-gate 59070Sstevel@tonic-gate /* 59080Sstevel@tonic-gate * Upgrade to exclusive lock on page 59090Sstevel@tonic-gate */ 59100Sstevel@tonic-gate if (!page_tryupgrade(pp)) { 59110Sstevel@tonic-gate page_unlock(pp); 59120Sstevel@tonic-gate va += pgsz; 59130Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, 59140Sstevel@tonic-gate btop(pgsz)); 59150Sstevel@tonic-gate continue; 59160Sstevel@tonic-gate } 59170Sstevel@tonic-gate 59180Sstevel@tonic-gate /* 59190Sstevel@tonic-gate * Remember pages locked exclusively and how many 59200Sstevel@tonic-gate */ 59210Sstevel@tonic-gate ppa[0] = pp; 59220Sstevel@tonic-gate nlocked = 1; 59230Sstevel@tonic-gate 59240Sstevel@tonic-gate /* 59250Sstevel@tonic-gate * Lock constituent pages if this is large page 59260Sstevel@tonic-gate */ 59270Sstevel@tonic-gate if (pages > 1) { 59280Sstevel@tonic-gate /* 59290Sstevel@tonic-gate * Lock all constituents except root page, since it 59300Sstevel@tonic-gate * should be locked already. 59310Sstevel@tonic-gate */ 59320Sstevel@tonic-gate for (i = 1; i < pages; i++) { 59330Sstevel@tonic-gate pp = page_next(pp); 59340Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) { 59350Sstevel@tonic-gate break; 59360Sstevel@tonic-gate } 59370Sstevel@tonic-gate if (PP_ISFREE(pp) || 59380Sstevel@tonic-gate pp->p_szc != pszc) { 59390Sstevel@tonic-gate /* 59400Sstevel@tonic-gate * hat_page_demote() raced in with us. 59410Sstevel@tonic-gate */ 59420Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(curvp)); 59430Sstevel@tonic-gate page_unlock(pp); 59440Sstevel@tonic-gate break; 59450Sstevel@tonic-gate } 59460Sstevel@tonic-gate ppa[nlocked] = pp; 59470Sstevel@tonic-gate nlocked++; 59480Sstevel@tonic-gate } 59490Sstevel@tonic-gate } 59500Sstevel@tonic-gate 59510Sstevel@tonic-gate /* 59520Sstevel@tonic-gate * If all constituent pages couldn't be locked, 59530Sstevel@tonic-gate * unlock pages locked so far and skip to next page. 59540Sstevel@tonic-gate */ 59550Sstevel@tonic-gate if (nlocked != pages) { 59560Sstevel@tonic-gate for (i = 0; i < nlocked; i++) 59570Sstevel@tonic-gate page_unlock(ppa[i]); 59580Sstevel@tonic-gate va += pgsz; 59590Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, 59600Sstevel@tonic-gate btop(pgsz)); 59610Sstevel@tonic-gate continue; 59620Sstevel@tonic-gate } 59630Sstevel@tonic-gate 59640Sstevel@tonic-gate /* 59650Sstevel@tonic-gate * hat_page_demote() can no longer happen 59660Sstevel@tonic-gate * since last cons page had the right p_szc after 59670Sstevel@tonic-gate * all cons pages were locked. all cons pages 59680Sstevel@tonic-gate * should now have the same p_szc. 59690Sstevel@tonic-gate */ 59700Sstevel@tonic-gate 59710Sstevel@tonic-gate /* 59720Sstevel@tonic-gate * All constituent pages locked successfully, so mark 59730Sstevel@tonic-gate * large page for migration and unload the mappings of 59740Sstevel@tonic-gate * constituent pages, so a fault will occur on any part of the 59750Sstevel@tonic-gate * large page 59760Sstevel@tonic-gate */ 59770Sstevel@tonic-gate PP_SETMIGRATE(ppa[0]); 59780Sstevel@tonic-gate for (i = 0; i < nlocked; i++) { 59790Sstevel@tonic-gate pp = ppa[i]; 59800Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 59810Sstevel@tonic-gate ASSERT(hat_page_getshare(pp) == 0); 59820Sstevel@tonic-gate page_unlock(pp); 59830Sstevel@tonic-gate } 59840Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_PGS, nlocked); 59850Sstevel@tonic-gate 59860Sstevel@tonic-gate va += pgsz; 59870Sstevel@tonic-gate } 59880Sstevel@tonic-gate kmem_free(ppa, ppa_nentries * sizeof (page_t *)); 59890Sstevel@tonic-gate } 59900Sstevel@tonic-gate 59910Sstevel@tonic-gate /* 59920Sstevel@tonic-gate * Migrate any pages that have been marked for migration in the given range 59930Sstevel@tonic-gate */ 59940Sstevel@tonic-gate void 59950Sstevel@tonic-gate page_migrate( 59960Sstevel@tonic-gate struct seg *seg, 59970Sstevel@tonic-gate caddr_t addr, 59980Sstevel@tonic-gate page_t **ppa, 59990Sstevel@tonic-gate pgcnt_t npages) 60000Sstevel@tonic-gate { 60010Sstevel@tonic-gate lgrp_t *from; 60020Sstevel@tonic-gate lgrp_t *to; 60030Sstevel@tonic-gate page_t *newpp; 60040Sstevel@tonic-gate page_t *pp; 60050Sstevel@tonic-gate pfn_t pfn; 60060Sstevel@tonic-gate size_t pgsz; 60070Sstevel@tonic-gate spgcnt_t page_cnt; 60080Sstevel@tonic-gate spgcnt_t i; 60090Sstevel@tonic-gate uint_t pszc; 60100Sstevel@tonic-gate 60110Sstevel@tonic-gate ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 60120Sstevel@tonic-gate 60130Sstevel@tonic-gate while (npages > 0) { 60140Sstevel@tonic-gate pp = *ppa; 60150Sstevel@tonic-gate pszc = pp->p_szc; 60160Sstevel@tonic-gate pgsz = page_get_pagesize(pszc); 60170Sstevel@tonic-gate page_cnt = btop(pgsz); 60180Sstevel@tonic-gate 60190Sstevel@tonic-gate /* 60200Sstevel@tonic-gate * Check to see whether this page is marked for migration 60210Sstevel@tonic-gate * 60220Sstevel@tonic-gate * Assume that root page of large page is marked for 60230Sstevel@tonic-gate * migration and none of the other constituent pages 60240Sstevel@tonic-gate * are marked. This really simplifies clearing the 60250Sstevel@tonic-gate * migrate bit by not having to clear it from each 60260Sstevel@tonic-gate * constituent page. 60270Sstevel@tonic-gate * 60280Sstevel@tonic-gate * note we don't want to relocate an entire large page if 60290Sstevel@tonic-gate * someone is only using one subpage. 60300Sstevel@tonic-gate */ 60310Sstevel@tonic-gate if (npages < page_cnt) 60320Sstevel@tonic-gate break; 60330Sstevel@tonic-gate 60340Sstevel@tonic-gate /* 60350Sstevel@tonic-gate * Is it marked for migration? 60360Sstevel@tonic-gate */ 60370Sstevel@tonic-gate if (!PP_ISMIGRATE(pp)) 60380Sstevel@tonic-gate goto next; 60390Sstevel@tonic-gate 60400Sstevel@tonic-gate /* 60410Sstevel@tonic-gate * Determine lgroups that page is being migrated between 60420Sstevel@tonic-gate */ 60430Sstevel@tonic-gate pfn = page_pptonum(pp); 60440Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, page_cnt)) { 60450Sstevel@tonic-gate break; 60460Sstevel@tonic-gate } 60470Sstevel@tonic-gate from = lgrp_pfn_to_lgrp(pfn); 60480Sstevel@tonic-gate to = lgrp_mem_choose(seg, addr, pgsz); 60490Sstevel@tonic-gate 60500Sstevel@tonic-gate /* 60510Sstevel@tonic-gate * Check to see whether we are trying to migrate page to lgroup 60520Sstevel@tonic-gate * where it is allocated already 60530Sstevel@tonic-gate */ 60540Sstevel@tonic-gate if (to == from) { 60550Sstevel@tonic-gate PP_CLRMIGRATE(pp); 60560Sstevel@tonic-gate goto next; 60570Sstevel@tonic-gate } 60580Sstevel@tonic-gate 60590Sstevel@tonic-gate /* 60600Sstevel@tonic-gate * Need to get exclusive lock's to migrate 60610Sstevel@tonic-gate */ 60620Sstevel@tonic-gate for (i = 0; i < page_cnt; i++) { 60630Sstevel@tonic-gate ASSERT(PAGE_LOCKED(ppa[i])); 60640Sstevel@tonic-gate if (page_pptonum(ppa[i]) != pfn + i || 60650Sstevel@tonic-gate ppa[i]->p_szc != pszc) { 60660Sstevel@tonic-gate break; 60670Sstevel@tonic-gate } 60680Sstevel@tonic-gate if (!page_tryupgrade(ppa[i])) { 60690Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, 60700Sstevel@tonic-gate LGRP_PM_FAIL_LOCK_PGS, 60710Sstevel@tonic-gate page_cnt); 60720Sstevel@tonic-gate break; 60730Sstevel@tonic-gate } 60740Sstevel@tonic-gate } 60750Sstevel@tonic-gate if (i != page_cnt) { 60760Sstevel@tonic-gate while (--i != -1) { 60770Sstevel@tonic-gate page_downgrade(ppa[i]); 60780Sstevel@tonic-gate } 60790Sstevel@tonic-gate goto next; 60800Sstevel@tonic-gate } 60810Sstevel@tonic-gate 60820Sstevel@tonic-gate (void) page_create_wait(page_cnt, PG_WAIT); 60830Sstevel@tonic-gate newpp = page_get_replacement_page(pp, to, PGR_SAMESZC); 60840Sstevel@tonic-gate if (newpp == NULL) { 60850Sstevel@tonic-gate page_create_putback(page_cnt); 60860Sstevel@tonic-gate for (i = 0; i < page_cnt; i++) { 60870Sstevel@tonic-gate page_downgrade(ppa[i]); 60880Sstevel@tonic-gate } 60890Sstevel@tonic-gate lgrp_stat_add(to->lgrp_id, LGRP_PM_FAIL_ALLOC_PGS, 60900Sstevel@tonic-gate page_cnt); 60910Sstevel@tonic-gate goto next; 60920Sstevel@tonic-gate } 60930Sstevel@tonic-gate ASSERT(newpp->p_szc == pszc); 60940Sstevel@tonic-gate /* 60950Sstevel@tonic-gate * Clear migrate bit and relocate page 60960Sstevel@tonic-gate */ 60970Sstevel@tonic-gate PP_CLRMIGRATE(pp); 60980Sstevel@tonic-gate if (page_relocate(&pp, &newpp, 0, 1, &page_cnt, to)) { 60990Sstevel@tonic-gate panic("page_migrate: page_relocate failed"); 61000Sstevel@tonic-gate } 61010Sstevel@tonic-gate ASSERT(page_cnt * PAGESIZE == pgsz); 61020Sstevel@tonic-gate 61030Sstevel@tonic-gate /* 61040Sstevel@tonic-gate * Keep stats for number of pages migrated from and to 61050Sstevel@tonic-gate * each lgroup 61060Sstevel@tonic-gate */ 61070Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PM_SRC_PGS, page_cnt); 61080Sstevel@tonic-gate lgrp_stat_add(to->lgrp_id, LGRP_PM_DEST_PGS, page_cnt); 61090Sstevel@tonic-gate /* 61100Sstevel@tonic-gate * update the page_t array we were passed in and 61110Sstevel@tonic-gate * unlink constituent pages of a large page. 61120Sstevel@tonic-gate */ 61130Sstevel@tonic-gate for (i = 0; i < page_cnt; ++i, ++pp) { 61140Sstevel@tonic-gate ASSERT(PAGE_EXCL(newpp)); 61150Sstevel@tonic-gate ASSERT(newpp->p_szc == pszc); 61160Sstevel@tonic-gate ppa[i] = newpp; 61170Sstevel@tonic-gate pp = newpp; 61180Sstevel@tonic-gate page_sub(&newpp, pp); 61190Sstevel@tonic-gate page_downgrade(pp); 61200Sstevel@tonic-gate } 61210Sstevel@tonic-gate ASSERT(newpp == NULL); 61220Sstevel@tonic-gate next: 61230Sstevel@tonic-gate addr += pgsz; 61240Sstevel@tonic-gate ppa += page_cnt; 61250Sstevel@tonic-gate npages -= page_cnt; 61260Sstevel@tonic-gate } 61270Sstevel@tonic-gate } 61280Sstevel@tonic-gate 61290Sstevel@tonic-gate /* 61300Sstevel@tonic-gate * initialize the vnode for retired pages 61310Sstevel@tonic-gate */ 61320Sstevel@tonic-gate static void 61330Sstevel@tonic-gate page_retired_init(void) 61340Sstevel@tonic-gate { 61350Sstevel@tonic-gate vn_setops(&retired_ppages, &retired_vnodeops); 61360Sstevel@tonic-gate } 61370Sstevel@tonic-gate 61380Sstevel@tonic-gate /* ARGSUSED */ 61390Sstevel@tonic-gate static void 61400Sstevel@tonic-gate retired_dispose(vnode_t *vp, page_t *pp, int flag, int dn, cred_t *cr) 61410Sstevel@tonic-gate { 61420Sstevel@tonic-gate panic("retired_dispose invoked"); 61430Sstevel@tonic-gate } 61440Sstevel@tonic-gate 61450Sstevel@tonic-gate /* ARGSUSED */ 61460Sstevel@tonic-gate static void 61470Sstevel@tonic-gate retired_inactive(vnode_t *vp, cred_t *cr) 61480Sstevel@tonic-gate {} 61490Sstevel@tonic-gate 61500Sstevel@tonic-gate void 61510Sstevel@tonic-gate page_unretire_pages(void) 61520Sstevel@tonic-gate { 61530Sstevel@tonic-gate page_t *pp; 61540Sstevel@tonic-gate kmutex_t *vphm; 61550Sstevel@tonic-gate vnode_t *vp; 61560Sstevel@tonic-gate page_t *rpages[UNRETIRE_PAGES]; 61570Sstevel@tonic-gate pgcnt_t i, npages, rmem; 61580Sstevel@tonic-gate uint64_t pa; 61590Sstevel@tonic-gate 61600Sstevel@tonic-gate rmem = 0; 61610Sstevel@tonic-gate 61620Sstevel@tonic-gate for (;;) { 61630Sstevel@tonic-gate /* 61640Sstevel@tonic-gate * We do this in 2 steps: 61650Sstevel@tonic-gate * 61660Sstevel@tonic-gate * 1. We walk the retired pages list and collect a list of 61670Sstevel@tonic-gate * pages that have the toxic field cleared. 61680Sstevel@tonic-gate * 61690Sstevel@tonic-gate * 2. We iterate through the page list and unretire each one. 61700Sstevel@tonic-gate * 61710Sstevel@tonic-gate * We have to do it in two steps on account of the mutexes that 61720Sstevel@tonic-gate * we need to acquire. 61730Sstevel@tonic-gate */ 61740Sstevel@tonic-gate 61750Sstevel@tonic-gate vp = &retired_ppages; 61760Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 61770Sstevel@tonic-gate mutex_enter(vphm); 61780Sstevel@tonic-gate 61790Sstevel@tonic-gate if ((pp = vp->v_pages) == NULL) { 61800Sstevel@tonic-gate mutex_exit(vphm); 61810Sstevel@tonic-gate break; 61820Sstevel@tonic-gate } 61830Sstevel@tonic-gate 61840Sstevel@tonic-gate i = 0; 61850Sstevel@tonic-gate do { 61860Sstevel@tonic-gate ASSERT(pp != NULL); 61870Sstevel@tonic-gate ASSERT(pp->p_vnode == vp); 61880Sstevel@tonic-gate 61890Sstevel@tonic-gate /* 61900Sstevel@tonic-gate * DR operations change the association between a page_t 61910Sstevel@tonic-gate * and the physical page it represents. Check if the 61920Sstevel@tonic-gate * page is still bad. If not, unretire it. 61930Sstevel@tonic-gate */ 61940Sstevel@tonic-gate if (!page_isfaulty(pp)) 61950Sstevel@tonic-gate rpages[i++] = pp; 61960Sstevel@tonic-gate 61970Sstevel@tonic-gate pp = pp->p_vpnext; 61980Sstevel@tonic-gate } while ((pp != vp->v_pages) && (i < UNRETIRE_PAGES)); 61990Sstevel@tonic-gate 62000Sstevel@tonic-gate mutex_exit(vphm); 62010Sstevel@tonic-gate 62020Sstevel@tonic-gate npages = i; 62030Sstevel@tonic-gate for (i = 0; i < npages; i++) { 62040Sstevel@tonic-gate pp = rpages[i]; 62050Sstevel@tonic-gate pa = ptob((uint64_t)page_pptonum(pp)); 62060Sstevel@tonic-gate 62070Sstevel@tonic-gate /* 62080Sstevel@tonic-gate * Need to upgrade the shared lock to an exclusive 62090Sstevel@tonic-gate * lock in order to hash out the page. 62100Sstevel@tonic-gate * 62110Sstevel@tonic-gate * The page could have been retired but the page lock 62120Sstevel@tonic-gate * may not have been downgraded yet. If so, skip this 62130Sstevel@tonic-gate * page. page_free() will call this function after the 62140Sstevel@tonic-gate * lock is downgraded. 62150Sstevel@tonic-gate */ 62160Sstevel@tonic-gate 62170Sstevel@tonic-gate if (!PAGE_SHARED(pp) || !page_tryupgrade(pp)) 62180Sstevel@tonic-gate continue; 62190Sstevel@tonic-gate 62200Sstevel@tonic-gate /* 62210Sstevel@tonic-gate * Both page_free() and DR call this function. They 62220Sstevel@tonic-gate * can potentially call this function at the same 62230Sstevel@tonic-gate * time and race with each other. 62240Sstevel@tonic-gate */ 62250Sstevel@tonic-gate if (!page_isretired(pp) || page_isfaulty(pp)) { 62260Sstevel@tonic-gate page_downgrade(pp); 62270Sstevel@tonic-gate continue; 62280Sstevel@tonic-gate } 62290Sstevel@tonic-gate 62300Sstevel@tonic-gate cmn_err(CE_NOTE, 62310Sstevel@tonic-gate "unretiring retired page 0x%08x.%08x", 62320Sstevel@tonic-gate (uint32_t)(pa >> 32), (uint32_t)pa); 62330Sstevel@tonic-gate 62340Sstevel@tonic-gate /* 62350Sstevel@tonic-gate * When a page is removed from the retired pages vnode, 62360Sstevel@tonic-gate * its toxic field is also cleared. So, we do not have 62370Sstevel@tonic-gate * to do that seperately here. 62380Sstevel@tonic-gate */ 62390Sstevel@tonic-gate page_hashout(pp, (kmutex_t *)NULL); 62400Sstevel@tonic-gate 62410Sstevel@tonic-gate /* 62420Sstevel@tonic-gate * This is a good page. So, free it. 62430Sstevel@tonic-gate */ 62440Sstevel@tonic-gate pp->p_vnode = NULL; 62450Sstevel@tonic-gate page_free(pp, 1); 62460Sstevel@tonic-gate rmem++; 62470Sstevel@tonic-gate } 62480Sstevel@tonic-gate 62490Sstevel@tonic-gate /* 62500Sstevel@tonic-gate * If the rpages array was filled up, then there could be more 62510Sstevel@tonic-gate * retired pages that are not faulty. We need to iterate 62520Sstevel@tonic-gate * again and unretire them. Otherwise, we are done. 62530Sstevel@tonic-gate */ 62540Sstevel@tonic-gate if (npages < UNRETIRE_PAGES) 62550Sstevel@tonic-gate break; 62560Sstevel@tonic-gate } 62570Sstevel@tonic-gate 62580Sstevel@tonic-gate mutex_enter(&freemem_lock); 62590Sstevel@tonic-gate availrmem += rmem; 62600Sstevel@tonic-gate mutex_exit(&freemem_lock); 62610Sstevel@tonic-gate } 62620Sstevel@tonic-gate 62630Sstevel@tonic-gate ulong_t mem_waiters = 0; 62640Sstevel@tonic-gate ulong_t max_count = 20; 62650Sstevel@tonic-gate #define MAX_DELAY 0x1ff 62660Sstevel@tonic-gate 62670Sstevel@tonic-gate /* 62680Sstevel@tonic-gate * Check if enough memory is available to proceed. 62690Sstevel@tonic-gate * Depending on system configuration and how much memory is 62700Sstevel@tonic-gate * reserved for swap we need to check against two variables. 62710Sstevel@tonic-gate * e.g. on systems with little physical swap availrmem can be 62720Sstevel@tonic-gate * more reliable indicator of how much memory is available. 62730Sstevel@tonic-gate * On systems with large phys swap freemem can be better indicator. 62740Sstevel@tonic-gate * If freemem drops below threshold level don't return an error 62750Sstevel@tonic-gate * immediately but wake up pageout to free memory and block. 62760Sstevel@tonic-gate * This is done number of times. If pageout is not able to free 62770Sstevel@tonic-gate * memory within certain time return an error. 62780Sstevel@tonic-gate * The same applies for availrmem but kmem_reap is used to 62790Sstevel@tonic-gate * free memory. 62800Sstevel@tonic-gate */ 62810Sstevel@tonic-gate int 62820Sstevel@tonic-gate page_mem_avail(pgcnt_t npages) 62830Sstevel@tonic-gate { 62840Sstevel@tonic-gate ulong_t count; 62850Sstevel@tonic-gate 62860Sstevel@tonic-gate #if defined(__i386) 62870Sstevel@tonic-gate if (freemem > desfree + npages && 62880Sstevel@tonic-gate availrmem > swapfs_reserve + npages && 62890Sstevel@tonic-gate btop(vmem_size(heap_arena, VMEM_FREE)) > tune.t_minarmem + 62900Sstevel@tonic-gate npages) 62910Sstevel@tonic-gate return (1); 62920Sstevel@tonic-gate #else 62930Sstevel@tonic-gate if (freemem > desfree + npages && 62940Sstevel@tonic-gate availrmem > swapfs_reserve + npages) 62950Sstevel@tonic-gate return (1); 62960Sstevel@tonic-gate #endif 62970Sstevel@tonic-gate 62980Sstevel@tonic-gate count = max_count; 62990Sstevel@tonic-gate atomic_add_long(&mem_waiters, 1); 63000Sstevel@tonic-gate 63010Sstevel@tonic-gate while (freemem < desfree + npages && --count) { 63020Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 63030Sstevel@tonic-gate if (delay_sig(hz + (mem_waiters & MAX_DELAY))) { 63040Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 63050Sstevel@tonic-gate return (0); 63060Sstevel@tonic-gate } 63070Sstevel@tonic-gate } 63080Sstevel@tonic-gate if (count == 0) { 63090Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 63100Sstevel@tonic-gate return (0); 63110Sstevel@tonic-gate } 63120Sstevel@tonic-gate 63130Sstevel@tonic-gate count = max_count; 63140Sstevel@tonic-gate while (availrmem < swapfs_reserve + npages && --count) { 63150Sstevel@tonic-gate kmem_reap(); 63160Sstevel@tonic-gate if (delay_sig(hz + (mem_waiters & MAX_DELAY))) { 63170Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 63180Sstevel@tonic-gate return (0); 63190Sstevel@tonic-gate } 63200Sstevel@tonic-gate } 63210Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 63220Sstevel@tonic-gate if (count == 0) 63230Sstevel@tonic-gate return (0); 63240Sstevel@tonic-gate 63250Sstevel@tonic-gate #if defined(__i386) 63260Sstevel@tonic-gate if (btop(vmem_size(heap_arena, VMEM_FREE)) < 63270Sstevel@tonic-gate tune.t_minarmem + npages) 63280Sstevel@tonic-gate return (0); 63290Sstevel@tonic-gate #endif 63300Sstevel@tonic-gate return (1); 63310Sstevel@tonic-gate } 63320Sstevel@tonic-gate 63330Sstevel@tonic-gate 63340Sstevel@tonic-gate /* 63350Sstevel@tonic-gate * Search the memory segments to locate the desired page. Within a 63360Sstevel@tonic-gate * segment, pages increase linearly with one page structure per 63370Sstevel@tonic-gate * physical page frame (size PAGESIZE). The search begins 63380Sstevel@tonic-gate * with the segment that was accessed last, to take advantage of locality. 63390Sstevel@tonic-gate * If the hint misses, we start from the beginning of the sorted memseg list 63400Sstevel@tonic-gate */ 63410Sstevel@tonic-gate 63420Sstevel@tonic-gate 63430Sstevel@tonic-gate /* 63440Sstevel@tonic-gate * Some data structures for pfn to pp lookup. 63450Sstevel@tonic-gate */ 63460Sstevel@tonic-gate ulong_t mhash_per_slot; 63470Sstevel@tonic-gate struct memseg *memseg_hash[N_MEM_SLOTS]; 63480Sstevel@tonic-gate 63490Sstevel@tonic-gate page_t * 63500Sstevel@tonic-gate page_numtopp_nolock(pfn_t pfnum) 63510Sstevel@tonic-gate { 63520Sstevel@tonic-gate static struct memseg *last_memseg_by_pfnum = NULL; 63530Sstevel@tonic-gate struct memseg *seg; 63540Sstevel@tonic-gate page_t *pp; 63550Sstevel@tonic-gate 63560Sstevel@tonic-gate /* 63570Sstevel@tonic-gate * XXX - Since page_numtopp_nolock is called in many places where 63580Sstevel@tonic-gate * the search fails more than it succeeds. It maybe worthwhile 63590Sstevel@tonic-gate * to put a check for pf_is_memory or a pfnum <= max_pfn (set at 63600Sstevel@tonic-gate * boot time). 63610Sstevel@tonic-gate * 63620Sstevel@tonic-gate * if (!pf_is_memory(pfnum) || (pfnum > max_pfn)) 63630Sstevel@tonic-gate * return (NULL); 63640Sstevel@tonic-gate */ 63650Sstevel@tonic-gate 63660Sstevel@tonic-gate MEMSEG_STAT_INCR(nsearch); 63670Sstevel@tonic-gate 63680Sstevel@tonic-gate /* Try last winner first */ 63690Sstevel@tonic-gate if (((seg = last_memseg_by_pfnum) != NULL) && 63700Sstevel@tonic-gate (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 63710Sstevel@tonic-gate MEMSEG_STAT_INCR(nlastwon); 63720Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 63730Sstevel@tonic-gate if (pp->p_pagenum == pfnum) 63740Sstevel@tonic-gate return ((page_t *)pp); 63750Sstevel@tonic-gate } 63760Sstevel@tonic-gate 63770Sstevel@tonic-gate /* Else Try hash */ 63780Sstevel@tonic-gate if (((seg = memseg_hash[MEMSEG_PFN_HASH(pfnum)]) != NULL) && 63790Sstevel@tonic-gate (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 63800Sstevel@tonic-gate MEMSEG_STAT_INCR(nhashwon); 63810Sstevel@tonic-gate last_memseg_by_pfnum = seg; 63820Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 63830Sstevel@tonic-gate if (pp->p_pagenum == pfnum) 63840Sstevel@tonic-gate return ((page_t *)pp); 63850Sstevel@tonic-gate } 63860Sstevel@tonic-gate 63870Sstevel@tonic-gate /* Else Brute force */ 63880Sstevel@tonic-gate for (seg = memsegs; seg != NULL; seg = seg->next) { 63890Sstevel@tonic-gate if (pfnum >= seg->pages_base && pfnum < seg->pages_end) { 63900Sstevel@tonic-gate last_memseg_by_pfnum = seg; 63910Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 63920Sstevel@tonic-gate return ((page_t *)pp); 63930Sstevel@tonic-gate } 63940Sstevel@tonic-gate } 63950Sstevel@tonic-gate last_memseg_by_pfnum = NULL; 63960Sstevel@tonic-gate MEMSEG_STAT_INCR(nnotfound); 63970Sstevel@tonic-gate return ((page_t *)NULL); 63980Sstevel@tonic-gate 63990Sstevel@tonic-gate } 64000Sstevel@tonic-gate 64010Sstevel@tonic-gate struct memseg * 64020Sstevel@tonic-gate page_numtomemseg_nolock(pfn_t pfnum) 64030Sstevel@tonic-gate { 64040Sstevel@tonic-gate struct memseg *seg; 64050Sstevel@tonic-gate page_t *pp; 64060Sstevel@tonic-gate 64070Sstevel@tonic-gate /* Try hash */ 64080Sstevel@tonic-gate if (((seg = memseg_hash[MEMSEG_PFN_HASH(pfnum)]) != NULL) && 64090Sstevel@tonic-gate (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 64100Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 64110Sstevel@tonic-gate if (pp->p_pagenum == pfnum) 64120Sstevel@tonic-gate return (seg); 64130Sstevel@tonic-gate } 64140Sstevel@tonic-gate 64150Sstevel@tonic-gate /* Else Brute force */ 64160Sstevel@tonic-gate for (seg = memsegs; seg != NULL; seg = seg->next) { 64170Sstevel@tonic-gate if (pfnum >= seg->pages_base && pfnum < seg->pages_end) { 64180Sstevel@tonic-gate return (seg); 64190Sstevel@tonic-gate } 64200Sstevel@tonic-gate } 64210Sstevel@tonic-gate return ((struct memseg *)NULL); 64220Sstevel@tonic-gate } 64230Sstevel@tonic-gate 64240Sstevel@tonic-gate /* 64250Sstevel@tonic-gate * Given a page and a count return the page struct that is 64260Sstevel@tonic-gate * n structs away from the current one in the global page 64270Sstevel@tonic-gate * list. 64280Sstevel@tonic-gate * 64290Sstevel@tonic-gate * This function wraps to the first page upon 64300Sstevel@tonic-gate * reaching the end of the memseg list. 64310Sstevel@tonic-gate */ 64320Sstevel@tonic-gate page_t * 64330Sstevel@tonic-gate page_nextn(page_t *pp, ulong_t n) 64340Sstevel@tonic-gate { 64350Sstevel@tonic-gate static struct memseg *last_page_next_memseg = NULL; 64360Sstevel@tonic-gate struct memseg *seg; 64370Sstevel@tonic-gate page_t *ppn; 64380Sstevel@tonic-gate 64390Sstevel@tonic-gate if (((seg = last_page_next_memseg) == NULL) || 64400Sstevel@tonic-gate (seg->pages_base == seg->pages_end) || 64410Sstevel@tonic-gate !(pp >= seg->pages && pp < seg->epages)) { 64420Sstevel@tonic-gate 64430Sstevel@tonic-gate for (seg = memsegs; seg; seg = seg->next) { 64440Sstevel@tonic-gate if (pp >= seg->pages && pp < seg->epages) 64450Sstevel@tonic-gate break; 64460Sstevel@tonic-gate } 64470Sstevel@tonic-gate 64480Sstevel@tonic-gate if (seg == NULL) { 64490Sstevel@tonic-gate /* Memory delete got in, return something valid. */ 64500Sstevel@tonic-gate /* TODO: fix me. */ 64510Sstevel@tonic-gate seg = memsegs; 64520Sstevel@tonic-gate pp = seg->pages; 64530Sstevel@tonic-gate } 64540Sstevel@tonic-gate } 64550Sstevel@tonic-gate 64560Sstevel@tonic-gate /* check for wraparound - possible if n is large */ 64570Sstevel@tonic-gate while ((ppn = (pp + n)) >= seg->epages || ppn < pp) { 64580Sstevel@tonic-gate n -= seg->epages - pp; 64590Sstevel@tonic-gate seg = seg->next; 64600Sstevel@tonic-gate if (seg == NULL) 64610Sstevel@tonic-gate seg = memsegs; 64620Sstevel@tonic-gate pp = seg->pages; 64630Sstevel@tonic-gate } 64640Sstevel@tonic-gate last_page_next_memseg = seg; 64650Sstevel@tonic-gate return (ppn); 64660Sstevel@tonic-gate } 64670Sstevel@tonic-gate 64680Sstevel@tonic-gate /* 64690Sstevel@tonic-gate * Initialize for a loop using page_next_scan_large(). 64700Sstevel@tonic-gate */ 64710Sstevel@tonic-gate page_t * 64720Sstevel@tonic-gate page_next_scan_init(void **cookie) 64730Sstevel@tonic-gate { 64740Sstevel@tonic-gate ASSERT(cookie != NULL); 64750Sstevel@tonic-gate *cookie = (void *)memsegs; 64760Sstevel@tonic-gate return ((page_t *)memsegs->pages); 64770Sstevel@tonic-gate } 64780Sstevel@tonic-gate 64790Sstevel@tonic-gate /* 64800Sstevel@tonic-gate * Return the next page in a scan of page_t's, assuming we want 64810Sstevel@tonic-gate * to skip over sub-pages within larger page sizes. 64820Sstevel@tonic-gate * 64830Sstevel@tonic-gate * The cookie is used to keep track of the current memseg. 64840Sstevel@tonic-gate */ 64850Sstevel@tonic-gate page_t * 64860Sstevel@tonic-gate page_next_scan_large( 64870Sstevel@tonic-gate page_t *pp, 64880Sstevel@tonic-gate ulong_t *n, 64890Sstevel@tonic-gate void **cookie) 64900Sstevel@tonic-gate { 64910Sstevel@tonic-gate struct memseg *seg = (struct memseg *)*cookie; 64920Sstevel@tonic-gate page_t *new_pp; 64930Sstevel@tonic-gate ulong_t cnt; 64940Sstevel@tonic-gate pfn_t pfn; 64950Sstevel@tonic-gate 64960Sstevel@tonic-gate 64970Sstevel@tonic-gate /* 64980Sstevel@tonic-gate * get the count of page_t's to skip based on the page size 64990Sstevel@tonic-gate */ 65000Sstevel@tonic-gate ASSERT(pp != NULL); 65010Sstevel@tonic-gate if (pp->p_szc == 0) { 65020Sstevel@tonic-gate cnt = 1; 65030Sstevel@tonic-gate } else { 65040Sstevel@tonic-gate pfn = page_pptonum(pp); 65050Sstevel@tonic-gate cnt = page_get_pagecnt(pp->p_szc); 65060Sstevel@tonic-gate cnt -= pfn & (cnt - 1); 65070Sstevel@tonic-gate } 65080Sstevel@tonic-gate *n += cnt; 65090Sstevel@tonic-gate new_pp = pp + cnt; 65100Sstevel@tonic-gate 65110Sstevel@tonic-gate /* 65120Sstevel@tonic-gate * Catch if we went past the end of the current memory segment. If so, 65130Sstevel@tonic-gate * just move to the next segment with pages. 65140Sstevel@tonic-gate */ 65150Sstevel@tonic-gate if (new_pp >= seg->epages) { 65160Sstevel@tonic-gate do { 65170Sstevel@tonic-gate seg = seg->next; 65180Sstevel@tonic-gate if (seg == NULL) 65190Sstevel@tonic-gate seg = memsegs; 65200Sstevel@tonic-gate } while (seg->pages == seg->epages); 65210Sstevel@tonic-gate new_pp = seg->pages; 65220Sstevel@tonic-gate *cookie = (void *)seg; 65230Sstevel@tonic-gate } 65240Sstevel@tonic-gate 65250Sstevel@tonic-gate return (new_pp); 65260Sstevel@tonic-gate } 65270Sstevel@tonic-gate 65280Sstevel@tonic-gate 65290Sstevel@tonic-gate /* 65300Sstevel@tonic-gate * Returns next page in list. Note: this function wraps 65310Sstevel@tonic-gate * to the first page in the list upon reaching the end 65320Sstevel@tonic-gate * of the list. Callers should be aware of this fact. 65330Sstevel@tonic-gate */ 65340Sstevel@tonic-gate 65350Sstevel@tonic-gate /* We should change this be a #define */ 65360Sstevel@tonic-gate 65370Sstevel@tonic-gate page_t * 65380Sstevel@tonic-gate page_next(page_t *pp) 65390Sstevel@tonic-gate { 65400Sstevel@tonic-gate return (page_nextn(pp, 1)); 65410Sstevel@tonic-gate } 65420Sstevel@tonic-gate 65430Sstevel@tonic-gate /* 65440Sstevel@tonic-gate * Special for routines processing an array of page_t. 65450Sstevel@tonic-gate */ 65460Sstevel@tonic-gate page_t * 65470Sstevel@tonic-gate page_nextn_raw(page_t *pp, ulong_t n) 65480Sstevel@tonic-gate { 65490Sstevel@tonic-gate return (pp+n); 65500Sstevel@tonic-gate } 65510Sstevel@tonic-gate 65520Sstevel@tonic-gate page_t * 65530Sstevel@tonic-gate page_first() 65540Sstevel@tonic-gate { 65550Sstevel@tonic-gate return ((page_t *)memsegs->pages); 65560Sstevel@tonic-gate } 65570Sstevel@tonic-gate 65580Sstevel@tonic-gate 65590Sstevel@tonic-gate /* 65600Sstevel@tonic-gate * This routine is called at boot with the initial memory configuration 65610Sstevel@tonic-gate * and when memory is added or removed. 65620Sstevel@tonic-gate */ 65630Sstevel@tonic-gate void 65640Sstevel@tonic-gate build_pfn_hash() 65650Sstevel@tonic-gate { 65660Sstevel@tonic-gate pfn_t cur; 65670Sstevel@tonic-gate pgcnt_t index; 65680Sstevel@tonic-gate struct memseg *pseg; 65690Sstevel@tonic-gate int i; 65700Sstevel@tonic-gate 65710Sstevel@tonic-gate /* 65720Sstevel@tonic-gate * Clear memseg_hash array. 65730Sstevel@tonic-gate * Since memory add/delete is designed to operate concurrently 65740Sstevel@tonic-gate * with normal operation, the hash rebuild must be able to run 65750Sstevel@tonic-gate * concurrently with page_numtopp_nolock(). To support this 65760Sstevel@tonic-gate * functionality, assignments to memseg_hash array members must 65770Sstevel@tonic-gate * be done atomically. 65780Sstevel@tonic-gate * 65790Sstevel@tonic-gate * NOTE: bzero() does not currently guarantee this for kernel 65800Sstevel@tonic-gate * threads, and cannot be used here. 65810Sstevel@tonic-gate */ 65820Sstevel@tonic-gate for (i = 0; i < N_MEM_SLOTS; i++) 65830Sstevel@tonic-gate memseg_hash[i] = NULL; 65840Sstevel@tonic-gate 65850Sstevel@tonic-gate hat_kpm_mseghash_clear(N_MEM_SLOTS); 65860Sstevel@tonic-gate 65870Sstevel@tonic-gate /* 65880Sstevel@tonic-gate * Physmax is the last valid pfn. 65890Sstevel@tonic-gate */ 65900Sstevel@tonic-gate mhash_per_slot = (physmax + 1) >> MEM_HASH_SHIFT; 65910Sstevel@tonic-gate for (pseg = memsegs; pseg != NULL; pseg = pseg->next) { 65920Sstevel@tonic-gate index = MEMSEG_PFN_HASH(pseg->pages_base); 65930Sstevel@tonic-gate cur = pseg->pages_base; 65940Sstevel@tonic-gate do { 65950Sstevel@tonic-gate if (index >= N_MEM_SLOTS) 65960Sstevel@tonic-gate index = MEMSEG_PFN_HASH(cur); 65970Sstevel@tonic-gate 65980Sstevel@tonic-gate if (memseg_hash[index] == NULL || 65990Sstevel@tonic-gate memseg_hash[index]->pages_base > pseg->pages_base) { 66000Sstevel@tonic-gate memseg_hash[index] = pseg; 66010Sstevel@tonic-gate hat_kpm_mseghash_update(index, pseg); 66020Sstevel@tonic-gate } 66030Sstevel@tonic-gate cur += mhash_per_slot; 66040Sstevel@tonic-gate index++; 66050Sstevel@tonic-gate } while (cur < pseg->pages_end); 66060Sstevel@tonic-gate } 66070Sstevel@tonic-gate } 66080Sstevel@tonic-gate 66090Sstevel@tonic-gate /* 66100Sstevel@tonic-gate * Return the pagenum for the pp 66110Sstevel@tonic-gate */ 66120Sstevel@tonic-gate pfn_t 66130Sstevel@tonic-gate page_pptonum(page_t *pp) 66140Sstevel@tonic-gate { 66150Sstevel@tonic-gate return (pp->p_pagenum); 66160Sstevel@tonic-gate } 66170Sstevel@tonic-gate 66180Sstevel@tonic-gate /* 66190Sstevel@tonic-gate * interface to the referenced and modified etc bits 66200Sstevel@tonic-gate * in the PSM part of the page struct 66210Sstevel@tonic-gate * when no locking is desired. 66220Sstevel@tonic-gate */ 66230Sstevel@tonic-gate void 66240Sstevel@tonic-gate page_set_props(page_t *pp, uint_t flags) 66250Sstevel@tonic-gate { 66260Sstevel@tonic-gate ASSERT((flags & ~(P_MOD | P_REF | P_RO)) == 0); 66270Sstevel@tonic-gate pp->p_nrm |= (uchar_t)flags; 66280Sstevel@tonic-gate } 66290Sstevel@tonic-gate 66300Sstevel@tonic-gate void 66310Sstevel@tonic-gate page_clr_all_props(page_t *pp) 66320Sstevel@tonic-gate { 66330Sstevel@tonic-gate pp->p_nrm = 0; 66340Sstevel@tonic-gate } 66350Sstevel@tonic-gate 66360Sstevel@tonic-gate /* 66370Sstevel@tonic-gate * The following functions is called from free_vp_pages() 66380Sstevel@tonic-gate * for an inexact estimate of a newly free'd page... 66390Sstevel@tonic-gate */ 66400Sstevel@tonic-gate ulong_t 66410Sstevel@tonic-gate page_share_cnt(page_t *pp) 66420Sstevel@tonic-gate { 66430Sstevel@tonic-gate return (hat_page_getshare(pp)); 66440Sstevel@tonic-gate } 66450Sstevel@tonic-gate 66460Sstevel@tonic-gate /* 66470Sstevel@tonic-gate * The following functions are used in handling memory 66480Sstevel@tonic-gate * errors. 66490Sstevel@tonic-gate */ 66500Sstevel@tonic-gate 66510Sstevel@tonic-gate int 66520Sstevel@tonic-gate page_istoxic(page_t *pp) 66530Sstevel@tonic-gate { 66540Sstevel@tonic-gate return ((pp->p_toxic & PAGE_IS_TOXIC) == PAGE_IS_TOXIC); 66550Sstevel@tonic-gate } 66560Sstevel@tonic-gate 66570Sstevel@tonic-gate int 66580Sstevel@tonic-gate page_isfailing(page_t *pp) 66590Sstevel@tonic-gate { 66600Sstevel@tonic-gate return ((pp->p_toxic & PAGE_IS_FAILING) == PAGE_IS_FAILING); 66610Sstevel@tonic-gate } 66620Sstevel@tonic-gate 66630Sstevel@tonic-gate int 66640Sstevel@tonic-gate page_isretired(page_t *pp) 66650Sstevel@tonic-gate { 66660Sstevel@tonic-gate return ((pp->p_toxic & PAGE_IS_RETIRED) == PAGE_IS_RETIRED); 66670Sstevel@tonic-gate } 66680Sstevel@tonic-gate 66690Sstevel@tonic-gate int 66700Sstevel@tonic-gate page_deteriorating(page_t *pp) 66710Sstevel@tonic-gate { 66720Sstevel@tonic-gate return ((pp->p_toxic & (PAGE_IS_TOXIC | PAGE_IS_FAILING)) != 0); 66730Sstevel@tonic-gate } 66740Sstevel@tonic-gate 66750Sstevel@tonic-gate void 66760Sstevel@tonic-gate page_settoxic(page_t *pp, uchar_t flag) 66770Sstevel@tonic-gate { 66780Sstevel@tonic-gate uchar_t new_flag = 0; 66790Sstevel@tonic-gate while ((new_flag & flag) != flag) { 66800Sstevel@tonic-gate uchar_t old_flag = pp->p_toxic; 66810Sstevel@tonic-gate new_flag = old_flag | flag; 66820Sstevel@tonic-gate (void) cas8(&pp->p_toxic, old_flag, new_flag); 66830Sstevel@tonic-gate new_flag = ((volatile page_t *)pp)->p_toxic; 66840Sstevel@tonic-gate } 66850Sstevel@tonic-gate } 66860Sstevel@tonic-gate 66870Sstevel@tonic-gate void 66880Sstevel@tonic-gate page_clrtoxic(page_t *pp) 66890Sstevel@tonic-gate { 66900Sstevel@tonic-gate /* 66910Sstevel@tonic-gate * We don't need to worry about atomicity on the 66920Sstevel@tonic-gate * p_toxic flag here as this is only called from 66930Sstevel@tonic-gate * page_free() while holding an exclusive lock on 66940Sstevel@tonic-gate * the page 66950Sstevel@tonic-gate */ 66960Sstevel@tonic-gate pp->p_toxic = PAGE_IS_OK; 66970Sstevel@tonic-gate } 66980Sstevel@tonic-gate 66990Sstevel@tonic-gate void 67000Sstevel@tonic-gate page_clrtoxic_flag(page_t *pp, uchar_t flag) 67010Sstevel@tonic-gate { 67020Sstevel@tonic-gate uchar_t new_flag = ((volatile page_t *)pp)->p_toxic; 67030Sstevel@tonic-gate while ((new_flag & flag) == flag) { 67040Sstevel@tonic-gate uchar_t old_flag = new_flag; 67050Sstevel@tonic-gate new_flag = old_flag & ~flag; 67060Sstevel@tonic-gate (void) cas8(&pp->p_toxic, old_flag, new_flag); 67070Sstevel@tonic-gate new_flag = ((volatile page_t *)pp)->p_toxic; 67080Sstevel@tonic-gate } 67090Sstevel@tonic-gate } 67100Sstevel@tonic-gate 67110Sstevel@tonic-gate int 67120Sstevel@tonic-gate page_isfaulty(page_t *pp) 67130Sstevel@tonic-gate { 67140Sstevel@tonic-gate return ((pp->p_toxic & PAGE_IS_FAULTY) == PAGE_IS_FAULTY); 67150Sstevel@tonic-gate } 67160Sstevel@tonic-gate 67170Sstevel@tonic-gate /* 67180Sstevel@tonic-gate * The following four functions are called from /proc code 67190Sstevel@tonic-gate * for the /proc/<pid>/xmap interface. 67200Sstevel@tonic-gate */ 67210Sstevel@tonic-gate int 67220Sstevel@tonic-gate page_isshared(page_t *pp) 67230Sstevel@tonic-gate { 67240Sstevel@tonic-gate return (hat_page_getshare(pp) > 1); 67250Sstevel@tonic-gate } 67260Sstevel@tonic-gate 67270Sstevel@tonic-gate int 67280Sstevel@tonic-gate page_isfree(page_t *pp) 67290Sstevel@tonic-gate { 67300Sstevel@tonic-gate return (PP_ISFREE(pp)); 67310Sstevel@tonic-gate } 67320Sstevel@tonic-gate 67330Sstevel@tonic-gate int 67340Sstevel@tonic-gate page_isref(page_t *pp) 67350Sstevel@tonic-gate { 67360Sstevel@tonic-gate return (hat_page_getattr(pp, P_REF)); 67370Sstevel@tonic-gate } 67380Sstevel@tonic-gate 67390Sstevel@tonic-gate int 67400Sstevel@tonic-gate page_ismod(page_t *pp) 67410Sstevel@tonic-gate { 67420Sstevel@tonic-gate return (hat_page_getattr(pp, P_MOD)); 67430Sstevel@tonic-gate } 6744