17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53cff2f43Sstans * Common Development and Distribution License (the "License"). 63cff2f43Sstans * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22954021b7SJustin Frank * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 317c478bd9Sstevel@tonic-gate * The Regents of the University of California 327c478bd9Sstevel@tonic-gate * All Rights Reserved 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 367c478bd9Sstevel@tonic-gate * contributors. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * VM - physical page management. 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <sys/types.h> 447c478bd9Sstevel@tonic-gate #include <sys/t_lock.h> 457c478bd9Sstevel@tonic-gate #include <sys/param.h> 467c478bd9Sstevel@tonic-gate #include <sys/systm.h> 477c478bd9Sstevel@tonic-gate #include <sys/errno.h> 487c478bd9Sstevel@tonic-gate #include <sys/time.h> 497c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 507c478bd9Sstevel@tonic-gate #include <sys/vm.h> 517c478bd9Sstevel@tonic-gate #include <sys/vtrace.h> 527c478bd9Sstevel@tonic-gate #include <sys/swap.h> 537c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 547c478bd9Sstevel@tonic-gate #include <sys/tuneable.h> 557c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 567c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 577c478bd9Sstevel@tonic-gate #include <sys/callb.h> 587c478bd9Sstevel@tonic-gate #include <sys/debug.h> 597c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h> 607c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h> 617c478bd9Sstevel@tonic-gate #include <sys/mem_config.h> 627c478bd9Sstevel@tonic-gate #include <sys/mem_cage.h> 637c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 647c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 657c478bd9Sstevel@tonic-gate #include <sys/strlog.h> 667c478bd9Sstevel@tonic-gate #include <sys/mman.h> 677c478bd9Sstevel@tonic-gate #include <sys/ontrap.h> 687c478bd9Sstevel@tonic-gate #include <sys/lgrp.h> 697c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate #include <vm/hat.h> 727c478bd9Sstevel@tonic-gate #include <vm/anon.h> 737c478bd9Sstevel@tonic-gate #include <vm/page.h> 747c478bd9Sstevel@tonic-gate #include <vm/seg.h> 757c478bd9Sstevel@tonic-gate #include <vm/pvn.h> 767c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 777c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h> 780209230bSgjelinek #include <sys/vm_usage.h> 797c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h> 80cee1d74bSjfrank #include <sys/ddi.h> 81cee1d74bSjfrank #include <sys/modctl.h> 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate static int nopageage = 0; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate static pgcnt_t max_page_get; /* max page_get request size in pages */ 867c478bd9Sstevel@tonic-gate pgcnt_t total_pages = 0; /* total number of pages (used by /proc) */ 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * freemem_lock protects all freemem variables: 907c478bd9Sstevel@tonic-gate * availrmem. Also this lock protects the globals which track the 917c478bd9Sstevel@tonic-gate * availrmem changes for accurate kernel footprint calculation. 927c478bd9Sstevel@tonic-gate * See below for an explanation of these 937c478bd9Sstevel@tonic-gate * globals. 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate kmutex_t freemem_lock; 967c478bd9Sstevel@tonic-gate pgcnt_t availrmem; 977c478bd9Sstevel@tonic-gate pgcnt_t availrmem_initial; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * These globals track availrmem changes to get a more accurate 1017c478bd9Sstevel@tonic-gate * estimate of tke kernel size. Historically pp_kernel is used for 1027c478bd9Sstevel@tonic-gate * kernel size and is based on availrmem. But availrmem is adjusted for 1037c478bd9Sstevel@tonic-gate * locked pages in the system not just for kernel locked pages. 1047c478bd9Sstevel@tonic-gate * These new counters will track the pages locked through segvn and 1057c478bd9Sstevel@tonic-gate * by explicit user locking. 1067c478bd9Sstevel@tonic-gate * 107da6c28aaSamw * pages_locked : How many pages are locked because of user specified 1087c478bd9Sstevel@tonic-gate * locking through mlock or plock. 1097c478bd9Sstevel@tonic-gate * 1107c478bd9Sstevel@tonic-gate * pages_useclaim,pages_claimed : These two variables track the 111da6c28aaSamw * claim adjustments because of the protection changes on a segvn segment. 1127c478bd9Sstevel@tonic-gate * 1137c478bd9Sstevel@tonic-gate * All these globals are protected by the same lock which protects availrmem. 1147c478bd9Sstevel@tonic-gate */ 115a98e9dbfSaguzovsk pgcnt_t pages_locked = 0; 116a98e9dbfSaguzovsk pgcnt_t pages_useclaim = 0; 117a98e9dbfSaguzovsk pgcnt_t pages_claimed = 0; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate /* 1217c478bd9Sstevel@tonic-gate * new_freemem_lock protects freemem, freemem_wait & freemem_cv. 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate static kmutex_t new_freemem_lock; 1247c478bd9Sstevel@tonic-gate static uint_t freemem_wait; /* someone waiting for freemem */ 1257c478bd9Sstevel@tonic-gate static kcondvar_t freemem_cv; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * The logical page free list is maintained as two lists, the 'free' 1297c478bd9Sstevel@tonic-gate * and the 'cache' lists. 1307c478bd9Sstevel@tonic-gate * The free list contains those pages that should be reused first. 1317c478bd9Sstevel@tonic-gate * 1327c478bd9Sstevel@tonic-gate * The implementation of the lists is machine dependent. 1337c478bd9Sstevel@tonic-gate * page_get_freelist(), page_get_cachelist(), 1347c478bd9Sstevel@tonic-gate * page_list_sub(), and page_list_add() 1357c478bd9Sstevel@tonic-gate * form the interface to the machine dependent implementation. 1367c478bd9Sstevel@tonic-gate * 1377c478bd9Sstevel@tonic-gate * Pages with p_free set are on the cache list. 1387c478bd9Sstevel@tonic-gate * Pages with p_free and p_age set are on the free list, 1397c478bd9Sstevel@tonic-gate * 1407c478bd9Sstevel@tonic-gate * A page may be locked while on either list. 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate /* 1447c478bd9Sstevel@tonic-gate * free list accounting stuff. 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * 1477c478bd9Sstevel@tonic-gate * Spread out the value for the number of pages on the 1487c478bd9Sstevel@tonic-gate * page free and page cache lists. If there is just one 1497c478bd9Sstevel@tonic-gate * value, then it must be under just one lock. 1507c478bd9Sstevel@tonic-gate * The lock contention and cache traffic are a real bother. 1517c478bd9Sstevel@tonic-gate * 1527c478bd9Sstevel@tonic-gate * When we acquire and then drop a single pcf lock 1537c478bd9Sstevel@tonic-gate * we can start in the middle of the array of pcf structures. 1547c478bd9Sstevel@tonic-gate * If we acquire more than one pcf lock at a time, we need to 1557c478bd9Sstevel@tonic-gate * start at the front to avoid deadlocking. 1567c478bd9Sstevel@tonic-gate * 1577c478bd9Sstevel@tonic-gate * pcf_count holds the number of pages in each pool. 1587c478bd9Sstevel@tonic-gate * 1597c478bd9Sstevel@tonic-gate * pcf_block is set when page_create_get_something() has asked the 1607c478bd9Sstevel@tonic-gate * PSM page freelist and page cachelist routines without specifying 1617c478bd9Sstevel@tonic-gate * a color and nothing came back. This is used to block anything 1627c478bd9Sstevel@tonic-gate * else from moving pages from one list to the other while the 1637c478bd9Sstevel@tonic-gate * lists are searched again. If a page is freeed while pcf_block is 1647c478bd9Sstevel@tonic-gate * set, then pcf_reserve is incremented. pcgs_unblock() takes care 1657c478bd9Sstevel@tonic-gate * of clearning pcf_block, doing the wakeups, etc. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate 16806fb6a36Sdv #define MAX_PCF_FANOUT NCPU 16906fb6a36Sdv static uint_t pcf_fanout = 1; /* Will get changed at boot time */ 17006fb6a36Sdv static uint_t pcf_fanout_mask = 0; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate struct pcf { 1737c478bd9Sstevel@tonic-gate kmutex_t pcf_lock; /* protects the structure */ 174f2b37d75Sfr uint_t pcf_count; /* page count */ 1757c478bd9Sstevel@tonic-gate uint_t pcf_wait; /* number of waiters */ 1767c478bd9Sstevel@tonic-gate uint_t pcf_block; /* pcgs flag to page_free() */ 1777c478bd9Sstevel@tonic-gate uint_t pcf_reserve; /* pages freed after pcf_block set */ 17806fb6a36Sdv uint_t pcf_fill[10]; /* to line up on the caches */ 1797c478bd9Sstevel@tonic-gate }; 1807c478bd9Sstevel@tonic-gate 18106fb6a36Sdv /* 18206fb6a36Sdv * PCF_INDEX hash needs to be dynamic (every so often the hash changes where 18306fb6a36Sdv * it will hash the cpu to). This is done to prevent a drain condition 18406fb6a36Sdv * from happening. This drain condition will occur when pcf_count decrement 18506fb6a36Sdv * occurs on cpu A and the increment of pcf_count always occurs on cpu B. An 18606fb6a36Sdv * example of this shows up with device interrupts. The dma buffer is allocated 18706fb6a36Sdv * by the cpu requesting the IO thus the pcf_count is decremented based on that. 18806fb6a36Sdv * When the memory is returned by the interrupt thread, the pcf_count will be 18906fb6a36Sdv * incremented based on the cpu servicing the interrupt. 19006fb6a36Sdv */ 19106fb6a36Sdv static struct pcf pcf[MAX_PCF_FANOUT]; 19206fb6a36Sdv #define PCF_INDEX() ((int)(((long)CPU->cpu_seqid) + \ 19306fb6a36Sdv (randtick() >> 24)) & (pcf_fanout_mask)) 19406fb6a36Sdv 19506fb6a36Sdv static int pcf_decrement_bucket(pgcnt_t); 19606fb6a36Sdv static int pcf_decrement_multiple(pgcnt_t *, pgcnt_t, int); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate kmutex_t pcgs_lock; /* serializes page_create_get_ */ 1997c478bd9Sstevel@tonic-gate kmutex_t pcgs_cagelock; /* serializes NOSLEEP cage allocs */ 2007c478bd9Sstevel@tonic-gate kmutex_t pcgs_wait_lock; /* used for delay in pcgs */ 2017c478bd9Sstevel@tonic-gate static kcondvar_t pcgs_cv; /* cv for delay in pcgs */ 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate #ifdef VM_STATS 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* 2067c478bd9Sstevel@tonic-gate * No locks, but so what, they are only statistics. 2077c478bd9Sstevel@tonic-gate */ 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate static struct page_tcnt { 2107c478bd9Sstevel@tonic-gate int pc_free_cache; /* free's into cache list */ 2117c478bd9Sstevel@tonic-gate int pc_free_dontneed; /* free's with dontneed */ 2127c478bd9Sstevel@tonic-gate int pc_free_pageout; /* free's from pageout */ 2137c478bd9Sstevel@tonic-gate int pc_free_free; /* free's into free list */ 2147c478bd9Sstevel@tonic-gate int pc_free_pages; /* free's into large page free list */ 2157c478bd9Sstevel@tonic-gate int pc_destroy_pages; /* large page destroy's */ 2167c478bd9Sstevel@tonic-gate int pc_get_cache; /* get's from cache list */ 2177c478bd9Sstevel@tonic-gate int pc_get_free; /* get's from free list */ 2187c478bd9Sstevel@tonic-gate int pc_reclaim; /* reclaim's */ 2197c478bd9Sstevel@tonic-gate int pc_abortfree; /* abort's of free pages */ 2207c478bd9Sstevel@tonic-gate int pc_find_hit; /* find's that find page */ 2217c478bd9Sstevel@tonic-gate int pc_find_miss; /* find's that don't find page */ 2227c478bd9Sstevel@tonic-gate int pc_destroy_free; /* # of free pages destroyed */ 2237c478bd9Sstevel@tonic-gate #define PC_HASH_CNT (4*PAGE_HASHAVELEN) 2247c478bd9Sstevel@tonic-gate int pc_find_hashlen[PC_HASH_CNT+1]; 2257c478bd9Sstevel@tonic-gate int pc_addclaim_pages; 2267c478bd9Sstevel@tonic-gate int pc_subclaim_pages; 2277c478bd9Sstevel@tonic-gate int pc_free_replacement_page[2]; 2287c478bd9Sstevel@tonic-gate int pc_try_demote_pages[6]; 2297c478bd9Sstevel@tonic-gate int pc_demote_pages[2]; 2307c478bd9Sstevel@tonic-gate } pagecnt; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate uint_t hashin_count; 2337c478bd9Sstevel@tonic-gate uint_t hashin_not_held; 2347c478bd9Sstevel@tonic-gate uint_t hashin_already; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate uint_t hashout_count; 2377c478bd9Sstevel@tonic-gate uint_t hashout_not_held; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate uint_t page_create_count; 2407c478bd9Sstevel@tonic-gate uint_t page_create_not_enough; 2417c478bd9Sstevel@tonic-gate uint_t page_create_not_enough_again; 2427c478bd9Sstevel@tonic-gate uint_t page_create_zero; 2437c478bd9Sstevel@tonic-gate uint_t page_create_hashout; 2447c478bd9Sstevel@tonic-gate uint_t page_create_page_lock_failed; 2457c478bd9Sstevel@tonic-gate uint_t page_create_trylock_failed; 2467c478bd9Sstevel@tonic-gate uint_t page_create_found_one; 2477c478bd9Sstevel@tonic-gate uint_t page_create_hashin_failed; 2487c478bd9Sstevel@tonic-gate uint_t page_create_dropped_phm; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate uint_t page_create_new; 2517c478bd9Sstevel@tonic-gate uint_t page_create_exists; 2527c478bd9Sstevel@tonic-gate uint_t page_create_putbacks; 2537c478bd9Sstevel@tonic-gate uint_t page_create_overshoot; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate uint_t page_reclaim_zero; 2567c478bd9Sstevel@tonic-gate uint_t page_reclaim_zero_locked; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate uint_t page_rename_exists; 2597c478bd9Sstevel@tonic-gate uint_t page_rename_count; 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate uint_t page_lookup_cnt[20]; 2627c478bd9Sstevel@tonic-gate uint_t page_lookup_nowait_cnt[10]; 2637c478bd9Sstevel@tonic-gate uint_t page_find_cnt; 2647c478bd9Sstevel@tonic-gate uint_t page_exists_cnt; 2657c478bd9Sstevel@tonic-gate uint_t page_exists_forreal_cnt; 2667c478bd9Sstevel@tonic-gate uint_t page_lookup_dev_cnt; 2677c478bd9Sstevel@tonic-gate uint_t get_cachelist_cnt; 2687c478bd9Sstevel@tonic-gate uint_t page_create_cnt[10]; 26978b03d3aSkchow uint_t alloc_pages[9]; 2707c478bd9Sstevel@tonic-gate uint_t page_exphcontg[19]; 2717c478bd9Sstevel@tonic-gate uint_t page_create_large_cnt[10]; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * Collects statistics. 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate #define PAGE_HASH_SEARCH(index, pp, vp, off) { \ 2777c478bd9Sstevel@tonic-gate uint_t mylen = 0; \ 2787c478bd9Sstevel@tonic-gate \ 2797c478bd9Sstevel@tonic-gate for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash, mylen++) { \ 2807c478bd9Sstevel@tonic-gate if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \ 2817c478bd9Sstevel@tonic-gate break; \ 2827c478bd9Sstevel@tonic-gate } \ 2837c478bd9Sstevel@tonic-gate if ((pp) != NULL) \ 2847c478bd9Sstevel@tonic-gate pagecnt.pc_find_hit++; \ 2857c478bd9Sstevel@tonic-gate else \ 2867c478bd9Sstevel@tonic-gate pagecnt.pc_find_miss++; \ 2877c478bd9Sstevel@tonic-gate if (mylen > PC_HASH_CNT) \ 2887c478bd9Sstevel@tonic-gate mylen = PC_HASH_CNT; \ 2897c478bd9Sstevel@tonic-gate pagecnt.pc_find_hashlen[mylen]++; \ 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate #else /* VM_STATS */ 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * Don't collect statistics 2967c478bd9Sstevel@tonic-gate */ 2977c478bd9Sstevel@tonic-gate #define PAGE_HASH_SEARCH(index, pp, vp, off) { \ 2987c478bd9Sstevel@tonic-gate for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash) { \ 2997c478bd9Sstevel@tonic-gate if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \ 3007c478bd9Sstevel@tonic-gate break; \ 3017c478bd9Sstevel@tonic-gate } \ 3027c478bd9Sstevel@tonic-gate } 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate #endif /* VM_STATS */ 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate #ifdef DEBUG 3097c478bd9Sstevel@tonic-gate #define MEMSEG_SEARCH_STATS 3107c478bd9Sstevel@tonic-gate #endif 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate #ifdef MEMSEG_SEARCH_STATS 3137c478bd9Sstevel@tonic-gate struct memseg_stats { 3147c478bd9Sstevel@tonic-gate uint_t nsearch; 3157c478bd9Sstevel@tonic-gate uint_t nlastwon; 3167c478bd9Sstevel@tonic-gate uint_t nhashwon; 3177c478bd9Sstevel@tonic-gate uint_t nnotfound; 3187c478bd9Sstevel@tonic-gate } memseg_stats; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate #define MEMSEG_STAT_INCR(v) \ 3217c478bd9Sstevel@tonic-gate atomic_add_32(&memseg_stats.v, 1) 3227c478bd9Sstevel@tonic-gate #else 3237c478bd9Sstevel@tonic-gate #define MEMSEG_STAT_INCR(x) 3247c478bd9Sstevel@tonic-gate #endif 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate struct memseg *memsegs; /* list of memory segments */ 3277c478bd9Sstevel@tonic-gate 3282be2af34Smec /* 3292be2af34Smec * /etc/system tunable to control large page allocation hueristic. 3302be2af34Smec * 3312be2af34Smec * Setting to LPAP_LOCAL will heavily prefer the local lgroup over remote lgroup 3322be2af34Smec * for large page allocation requests. If a large page is not readily 3332be2af34Smec * avaliable on the local freelists we will go through additional effort 3342be2af34Smec * to create a large page, potentially moving smaller pages around to coalesce 3352be2af34Smec * larger pages in the local lgroup. 3362be2af34Smec * Default value of LPAP_DEFAULT will go to remote freelists if large pages 3372be2af34Smec * are not readily available in the local lgroup. 3382be2af34Smec */ 3392be2af34Smec enum lpap { 3402be2af34Smec LPAP_DEFAULT, /* default large page allocation policy */ 3412be2af34Smec LPAP_LOCAL /* local large page allocation policy */ 3422be2af34Smec }; 3432be2af34Smec 3442be2af34Smec enum lpap lpg_alloc_prefer = LPAP_DEFAULT; 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate static void page_init_mem_config(void); 3477c478bd9Sstevel@tonic-gate static int page_do_hashin(page_t *, vnode_t *, u_offset_t); 3487c478bd9Sstevel@tonic-gate static void page_do_hashout(page_t *); 3498b464eb8Smec static void page_capture_init(); 3508b464eb8Smec int page_capture_take_action(page_t *, uint_t, void *); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate static void page_demote_vp_pages(page_t *); 3537c478bd9Sstevel@tonic-gate 35406fb6a36Sdv 35506fb6a36Sdv void 35606fb6a36Sdv pcf_init(void) 35706fb6a36Sdv 35806fb6a36Sdv { 35906fb6a36Sdv if (boot_ncpus != -1) { 36006fb6a36Sdv pcf_fanout = boot_ncpus; 36106fb6a36Sdv } else { 36206fb6a36Sdv pcf_fanout = max_ncpus; 36306fb6a36Sdv } 36406fb6a36Sdv #ifdef sun4v 36506fb6a36Sdv /* 36606fb6a36Sdv * Force at least 4 buckets if possible for sun4v. 36706fb6a36Sdv */ 36806fb6a36Sdv pcf_fanout = MAX(pcf_fanout, 4); 36906fb6a36Sdv #endif /* sun4v */ 37006fb6a36Sdv 37106fb6a36Sdv /* 37206fb6a36Sdv * Round up to the nearest power of 2. 37306fb6a36Sdv */ 37406fb6a36Sdv pcf_fanout = MIN(pcf_fanout, MAX_PCF_FANOUT); 37506fb6a36Sdv if (!ISP2(pcf_fanout)) { 37606fb6a36Sdv pcf_fanout = 1 << highbit(pcf_fanout); 37706fb6a36Sdv 37806fb6a36Sdv if (pcf_fanout > MAX_PCF_FANOUT) { 37906fb6a36Sdv pcf_fanout = 1 << (highbit(MAX_PCF_FANOUT) - 1); 38006fb6a36Sdv } 38106fb6a36Sdv } 38206fb6a36Sdv pcf_fanout_mask = pcf_fanout - 1; 38306fb6a36Sdv } 38406fb6a36Sdv 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * vm subsystem related initialization 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate void 3897c478bd9Sstevel@tonic-gate vm_init(void) 3907c478bd9Sstevel@tonic-gate { 3917c478bd9Sstevel@tonic-gate boolean_t callb_vm_cpr(void *, int); 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate (void) callb_add(callb_vm_cpr, 0, CB_CL_CPR_VM, "vm"); 3947c478bd9Sstevel@tonic-gate page_init_mem_config(); 395db874c57Selowe page_retire_init(); 3960209230bSgjelinek vm_usage_init(); 3978b464eb8Smec page_capture_init(); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate /* 4017c478bd9Sstevel@tonic-gate * This function is called at startup and when memory is added or deleted. 4027c478bd9Sstevel@tonic-gate */ 4037c478bd9Sstevel@tonic-gate void 4047c478bd9Sstevel@tonic-gate init_pages_pp_maximum() 4057c478bd9Sstevel@tonic-gate { 4067c478bd9Sstevel@tonic-gate static pgcnt_t p_min; 4077c478bd9Sstevel@tonic-gate static pgcnt_t pages_pp_maximum_startup; 4087c478bd9Sstevel@tonic-gate static pgcnt_t avrmem_delta; 4097c478bd9Sstevel@tonic-gate static int init_done; 4107c478bd9Sstevel@tonic-gate static int user_set; /* true if set in /etc/system */ 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate if (init_done == 0) { 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* If the user specified a value, save it */ 4157c478bd9Sstevel@tonic-gate if (pages_pp_maximum != 0) { 4167c478bd9Sstevel@tonic-gate user_set = 1; 4177c478bd9Sstevel@tonic-gate pages_pp_maximum_startup = pages_pp_maximum; 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate /* 4217c478bd9Sstevel@tonic-gate * Setting of pages_pp_maximum is based first time 4227c478bd9Sstevel@tonic-gate * on the value of availrmem just after the start-up 4237c478bd9Sstevel@tonic-gate * allocations. To preserve this relationship at run 4247c478bd9Sstevel@tonic-gate * time, use a delta from availrmem_initial. 4257c478bd9Sstevel@tonic-gate */ 4267c478bd9Sstevel@tonic-gate ASSERT(availrmem_initial >= availrmem); 4277c478bd9Sstevel@tonic-gate avrmem_delta = availrmem_initial - availrmem; 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate /* The allowable floor of pages_pp_maximum */ 4307c478bd9Sstevel@tonic-gate p_min = tune.t_minarmem + 100; 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate /* Make sure we don't come through here again. */ 4337c478bd9Sstevel@tonic-gate init_done = 1; 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate /* 4367c478bd9Sstevel@tonic-gate * Determine pages_pp_maximum, the number of currently available 4377c478bd9Sstevel@tonic-gate * pages (availrmem) that can't be `locked'. If not set by 4387c478bd9Sstevel@tonic-gate * the user, we set it to 4% of the currently available memory 4397c478bd9Sstevel@tonic-gate * plus 4MB. 4407c478bd9Sstevel@tonic-gate * But we also insist that it be greater than tune.t_minarmem; 4417c478bd9Sstevel@tonic-gate * otherwise a process could lock down a lot of memory, get swapped 4427c478bd9Sstevel@tonic-gate * out, and never have enough to get swapped back in. 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate if (user_set) 4457c478bd9Sstevel@tonic-gate pages_pp_maximum = pages_pp_maximum_startup; 4467c478bd9Sstevel@tonic-gate else 4477c478bd9Sstevel@tonic-gate pages_pp_maximum = ((availrmem_initial - avrmem_delta) / 25) 4487c478bd9Sstevel@tonic-gate + btop(4 * 1024 * 1024); 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate if (pages_pp_maximum <= p_min) { 4517c478bd9Sstevel@tonic-gate pages_pp_maximum = p_min; 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate void 4567c478bd9Sstevel@tonic-gate set_max_page_get(pgcnt_t target_total_pages) 4577c478bd9Sstevel@tonic-gate { 4587c478bd9Sstevel@tonic-gate max_page_get = target_total_pages / 2; 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate static pgcnt_t pending_delete; 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4647c478bd9Sstevel@tonic-gate static void 4657c478bd9Sstevel@tonic-gate page_mem_config_post_add( 4667c478bd9Sstevel@tonic-gate void *arg, 4677c478bd9Sstevel@tonic-gate pgcnt_t delta_pages) 4687c478bd9Sstevel@tonic-gate { 4697c478bd9Sstevel@tonic-gate set_max_page_get(total_pages - pending_delete); 4707c478bd9Sstevel@tonic-gate init_pages_pp_maximum(); 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4747c478bd9Sstevel@tonic-gate static int 4757c478bd9Sstevel@tonic-gate page_mem_config_pre_del( 4767c478bd9Sstevel@tonic-gate void *arg, 4777c478bd9Sstevel@tonic-gate pgcnt_t delta_pages) 4787c478bd9Sstevel@tonic-gate { 4797c478bd9Sstevel@tonic-gate pgcnt_t nv; 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate nv = atomic_add_long_nv(&pending_delete, (spgcnt_t)delta_pages); 4827c478bd9Sstevel@tonic-gate set_max_page_get(total_pages - nv); 4837c478bd9Sstevel@tonic-gate return (0); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4877c478bd9Sstevel@tonic-gate static void 4887c478bd9Sstevel@tonic-gate page_mem_config_post_del( 4897c478bd9Sstevel@tonic-gate void *arg, 4907c478bd9Sstevel@tonic-gate pgcnt_t delta_pages, 4917c478bd9Sstevel@tonic-gate int cancelled) 4927c478bd9Sstevel@tonic-gate { 4937c478bd9Sstevel@tonic-gate pgcnt_t nv; 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate nv = atomic_add_long_nv(&pending_delete, -(spgcnt_t)delta_pages); 4967c478bd9Sstevel@tonic-gate set_max_page_get(total_pages - nv); 4977c478bd9Sstevel@tonic-gate if (!cancelled) 4987c478bd9Sstevel@tonic-gate init_pages_pp_maximum(); 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate static kphysm_setup_vector_t page_mem_config_vec = { 5027c478bd9Sstevel@tonic-gate KPHYSM_SETUP_VECTOR_VERSION, 5037c478bd9Sstevel@tonic-gate page_mem_config_post_add, 5047c478bd9Sstevel@tonic-gate page_mem_config_pre_del, 5057c478bd9Sstevel@tonic-gate page_mem_config_post_del, 5067c478bd9Sstevel@tonic-gate }; 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate static void 5097c478bd9Sstevel@tonic-gate page_init_mem_config(void) 5107c478bd9Sstevel@tonic-gate { 5117c478bd9Sstevel@tonic-gate int ret; 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate ret = kphysm_setup_func_register(&page_mem_config_vec, (void *)NULL); 5147c478bd9Sstevel@tonic-gate ASSERT(ret == 0); 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate /* 5187c478bd9Sstevel@tonic-gate * Evenly spread out the PCF counters for large free pages 5197c478bd9Sstevel@tonic-gate */ 5207c478bd9Sstevel@tonic-gate static void 5217c478bd9Sstevel@tonic-gate page_free_large_ctr(pgcnt_t npages) 5227c478bd9Sstevel@tonic-gate { 5237c478bd9Sstevel@tonic-gate static struct pcf *p = pcf; 5247c478bd9Sstevel@tonic-gate pgcnt_t lump; 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate freemem += npages; 5277c478bd9Sstevel@tonic-gate 52806fb6a36Sdv lump = roundup(npages, pcf_fanout) / pcf_fanout; 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate while (npages > 0) { 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate ASSERT(!p->pcf_block); 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate if (lump < npages) { 5357c478bd9Sstevel@tonic-gate p->pcf_count += (uint_t)lump; 5367c478bd9Sstevel@tonic-gate npages -= lump; 5377c478bd9Sstevel@tonic-gate } else { 5387c478bd9Sstevel@tonic-gate p->pcf_count += (uint_t)npages; 5397c478bd9Sstevel@tonic-gate npages = 0; 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate ASSERT(!p->pcf_wait); 5437c478bd9Sstevel@tonic-gate 54406fb6a36Sdv if (++p > &pcf[pcf_fanout - 1]) 5457c478bd9Sstevel@tonic-gate p = pcf; 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate ASSERT(npages == 0); 5497c478bd9Sstevel@tonic-gate } 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* 552da6c28aaSamw * Add a physical chunk of memory to the system free lists during startup. 5537c478bd9Sstevel@tonic-gate * Platform specific startup() allocates the memory for the page structs. 5547c478bd9Sstevel@tonic-gate * 5557c478bd9Sstevel@tonic-gate * num - number of page structures 5567c478bd9Sstevel@tonic-gate * base - page number (pfn) to be associated with the first page. 5577c478bd9Sstevel@tonic-gate * 5587c478bd9Sstevel@tonic-gate * Since we are doing this during startup (ie. single threaded), we will 5597c478bd9Sstevel@tonic-gate * use shortcut routines to avoid any locking overhead while putting all 5607c478bd9Sstevel@tonic-gate * these pages on the freelists. 5617c478bd9Sstevel@tonic-gate * 5627c478bd9Sstevel@tonic-gate * NOTE: Any changes performed to page_free(), must also be performed to 5637c478bd9Sstevel@tonic-gate * add_physmem() since this is how we initialize all page_t's at 5647c478bd9Sstevel@tonic-gate * boot time. 5657c478bd9Sstevel@tonic-gate */ 5667c478bd9Sstevel@tonic-gate void 5677c478bd9Sstevel@tonic-gate add_physmem( 5687c478bd9Sstevel@tonic-gate page_t *pp, 5697c478bd9Sstevel@tonic-gate pgcnt_t num, 5707c478bd9Sstevel@tonic-gate pfn_t pnum) 5717c478bd9Sstevel@tonic-gate { 5727c478bd9Sstevel@tonic-gate page_t *root = NULL; 5737c478bd9Sstevel@tonic-gate uint_t szc = page_num_pagesizes() - 1; 5747c478bd9Sstevel@tonic-gate pgcnt_t large = page_get_pagecnt(szc); 5757c478bd9Sstevel@tonic-gate pgcnt_t cnt = 0; 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_INIT, 5786e4dd838Smec "add_physmem:pp %p num %lu", pp, num); 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate /* 5817c478bd9Sstevel@tonic-gate * Arbitrarily limit the max page_get request 5827c478bd9Sstevel@tonic-gate * to 1/2 of the page structs we have. 5837c478bd9Sstevel@tonic-gate */ 5847c478bd9Sstevel@tonic-gate total_pages += num; 5857c478bd9Sstevel@tonic-gate set_max_page_get(total_pages); 5867c478bd9Sstevel@tonic-gate 587e21bae1bSkchow PLCNT_MODIFY_MAX(pnum, (long)num); 588e21bae1bSkchow 5897c478bd9Sstevel@tonic-gate /* 5907c478bd9Sstevel@tonic-gate * The physical space for the pages array 5917c478bd9Sstevel@tonic-gate * representing ram pages has already been 5927c478bd9Sstevel@tonic-gate * allocated. Here we initialize each lock 5937c478bd9Sstevel@tonic-gate * in the page structure, and put each on 5947c478bd9Sstevel@tonic-gate * the free list 5957c478bd9Sstevel@tonic-gate */ 596affbd3ccSkchow for (; num; pp++, pnum++, num--) { 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate /* 5997c478bd9Sstevel@tonic-gate * this needs to fill in the page number 6007c478bd9Sstevel@tonic-gate * and do any other arch specific initialization 6017c478bd9Sstevel@tonic-gate */ 6027c478bd9Sstevel@tonic-gate add_physmem_cb(pp, pnum); 6037c478bd9Sstevel@tonic-gate 60407b65a64Saguzovsk pp->p_lckcnt = 0; 60507b65a64Saguzovsk pp->p_cowcnt = 0; 60607b65a64Saguzovsk pp->p_slckcnt = 0; 60707b65a64Saguzovsk 6087c478bd9Sstevel@tonic-gate /* 6097c478bd9Sstevel@tonic-gate * Initialize the page lock as unlocked, since nobody 6107c478bd9Sstevel@tonic-gate * can see or access this page yet. 6117c478bd9Sstevel@tonic-gate */ 6127c478bd9Sstevel@tonic-gate pp->p_selock = 0; 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate /* 6157c478bd9Sstevel@tonic-gate * Initialize IO lock 6167c478bd9Sstevel@tonic-gate */ 6177c478bd9Sstevel@tonic-gate page_iolock_init(pp); 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate /* 6207c478bd9Sstevel@tonic-gate * initialize other fields in the page_t 6217c478bd9Sstevel@tonic-gate */ 6227c478bd9Sstevel@tonic-gate PP_SETFREE(pp); 6232f0fcb93SJason Beloro page_clr_all_props(pp, 0); 6247c478bd9Sstevel@tonic-gate PP_SETAGED(pp); 6257c478bd9Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1; 6267c478bd9Sstevel@tonic-gate pp->p_next = pp; 6277c478bd9Sstevel@tonic-gate pp->p_prev = pp; 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate /* 6307c478bd9Sstevel@tonic-gate * Simple case: System doesn't support large pages. 6317c478bd9Sstevel@tonic-gate */ 6327c478bd9Sstevel@tonic-gate if (szc == 0) { 6337c478bd9Sstevel@tonic-gate pp->p_szc = 0; 6347c478bd9Sstevel@tonic-gate page_free_at_startup(pp); 6357c478bd9Sstevel@tonic-gate continue; 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate /* 6397c478bd9Sstevel@tonic-gate * Handle unaligned pages, we collect them up onto 6407c478bd9Sstevel@tonic-gate * the root page until we have a full large page. 6417c478bd9Sstevel@tonic-gate */ 6427c478bd9Sstevel@tonic-gate if (!IS_P2ALIGNED(pnum, large)) { 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate /* 6457c478bd9Sstevel@tonic-gate * If not in a large page, 6467c478bd9Sstevel@tonic-gate * just free as small page. 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate if (root == NULL) { 6497c478bd9Sstevel@tonic-gate pp->p_szc = 0; 6507c478bd9Sstevel@tonic-gate page_free_at_startup(pp); 6517c478bd9Sstevel@tonic-gate continue; 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate /* 6557c478bd9Sstevel@tonic-gate * Link a constituent page into the large page. 6567c478bd9Sstevel@tonic-gate */ 6577c478bd9Sstevel@tonic-gate pp->p_szc = szc; 6587c478bd9Sstevel@tonic-gate page_list_concat(&root, &pp); 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate /* 6617c478bd9Sstevel@tonic-gate * When large page is fully formed, free it. 6627c478bd9Sstevel@tonic-gate */ 6637c478bd9Sstevel@tonic-gate if (++cnt == large) { 6647c478bd9Sstevel@tonic-gate page_free_large_ctr(cnt); 6657c478bd9Sstevel@tonic-gate page_list_add_pages(root, PG_LIST_ISINIT); 6667c478bd9Sstevel@tonic-gate root = NULL; 6677c478bd9Sstevel@tonic-gate cnt = 0; 6687c478bd9Sstevel@tonic-gate } 6697c478bd9Sstevel@tonic-gate continue; 6707c478bd9Sstevel@tonic-gate } 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate /* 6737c478bd9Sstevel@tonic-gate * At this point we have a page number which 6747c478bd9Sstevel@tonic-gate * is aligned. We assert that we aren't already 6757c478bd9Sstevel@tonic-gate * in a different large page. 6767c478bd9Sstevel@tonic-gate */ 6777c478bd9Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(pnum, large)); 6787c478bd9Sstevel@tonic-gate ASSERT(root == NULL && cnt == 0); 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate /* 6817c478bd9Sstevel@tonic-gate * If insufficient number of pages left to form 6827c478bd9Sstevel@tonic-gate * a large page, just free the small page. 6837c478bd9Sstevel@tonic-gate */ 6847c478bd9Sstevel@tonic-gate if (num < large) { 6857c478bd9Sstevel@tonic-gate pp->p_szc = 0; 6867c478bd9Sstevel@tonic-gate page_free_at_startup(pp); 6877c478bd9Sstevel@tonic-gate continue; 6887c478bd9Sstevel@tonic-gate } 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate /* 6917c478bd9Sstevel@tonic-gate * Otherwise start a new large page. 6927c478bd9Sstevel@tonic-gate */ 6937c478bd9Sstevel@tonic-gate pp->p_szc = szc; 6947c478bd9Sstevel@tonic-gate cnt++; 6957c478bd9Sstevel@tonic-gate root = pp; 6967c478bd9Sstevel@tonic-gate } 6977c478bd9Sstevel@tonic-gate ASSERT(root == NULL && cnt == 0); 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate /* 7017c478bd9Sstevel@tonic-gate * Find a page representing the specified [vp, offset]. 7027c478bd9Sstevel@tonic-gate * If we find the page but it is intransit coming in, 7037c478bd9Sstevel@tonic-gate * it will have an "exclusive" lock and we wait for 7047c478bd9Sstevel@tonic-gate * the i/o to complete. A page found on the free list 7057c478bd9Sstevel@tonic-gate * is always reclaimed and then locked. On success, the page 7067c478bd9Sstevel@tonic-gate * is locked, its data is valid and it isn't on the free 7077c478bd9Sstevel@tonic-gate * list, while a NULL is returned if the page doesn't exist. 7087c478bd9Sstevel@tonic-gate */ 7097c478bd9Sstevel@tonic-gate page_t * 7107c478bd9Sstevel@tonic-gate page_lookup(vnode_t *vp, u_offset_t off, se_t se) 7117c478bd9Sstevel@tonic-gate { 7127c478bd9Sstevel@tonic-gate return (page_lookup_create(vp, off, se, NULL, NULL, 0)); 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate /* 7167c478bd9Sstevel@tonic-gate * Find a page representing the specified [vp, offset]. 7177c478bd9Sstevel@tonic-gate * We either return the one we found or, if passed in, 7187c478bd9Sstevel@tonic-gate * create one with identity of [vp, offset] of the 719da6c28aaSamw * pre-allocated page. If we find existing page but it is 7207c478bd9Sstevel@tonic-gate * intransit coming in, it will have an "exclusive" lock 7217c478bd9Sstevel@tonic-gate * and we wait for the i/o to complete. A page found on 7227c478bd9Sstevel@tonic-gate * the free list is always reclaimed and then locked. 7237c478bd9Sstevel@tonic-gate * On success, the page is locked, its data is valid and 7247c478bd9Sstevel@tonic-gate * it isn't on the free list, while a NULL is returned 7257c478bd9Sstevel@tonic-gate * if the page doesn't exist and newpp is NULL; 7267c478bd9Sstevel@tonic-gate */ 7277c478bd9Sstevel@tonic-gate page_t * 7287c478bd9Sstevel@tonic-gate page_lookup_create( 7297c478bd9Sstevel@tonic-gate vnode_t *vp, 7307c478bd9Sstevel@tonic-gate u_offset_t off, 7317c478bd9Sstevel@tonic-gate se_t se, 7327c478bd9Sstevel@tonic-gate page_t *newpp, 7337c478bd9Sstevel@tonic-gate spgcnt_t *nrelocp, 7347c478bd9Sstevel@tonic-gate int flags) 7357c478bd9Sstevel@tonic-gate { 7367c478bd9Sstevel@tonic-gate page_t *pp; 7377c478bd9Sstevel@tonic-gate kmutex_t *phm; 7387c478bd9Sstevel@tonic-gate ulong_t index; 7397c478bd9Sstevel@tonic-gate uint_t hash_locked; 7407c478bd9Sstevel@tonic-gate uint_t es; 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 7437c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[0]); 7447c478bd9Sstevel@tonic-gate ASSERT(newpp ? PAGE_EXCL(newpp) : 1); 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate /* 7477c478bd9Sstevel@tonic-gate * Acquire the appropriate page hash lock since 7487c478bd9Sstevel@tonic-gate * we have to search the hash list. Pages that 7497c478bd9Sstevel@tonic-gate * hash to this list can't change identity while 7507c478bd9Sstevel@tonic-gate * this lock is held. 7517c478bd9Sstevel@tonic-gate */ 7527c478bd9Sstevel@tonic-gate hash_locked = 0; 7537c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 7547c478bd9Sstevel@tonic-gate phm = NULL; 7557c478bd9Sstevel@tonic-gate top: 7567c478bd9Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 7577c478bd9Sstevel@tonic-gate if (pp != NULL) { 7587c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[1]); 7597c478bd9Sstevel@tonic-gate es = (newpp != NULL) ? 1 : 0; 7607c478bd9Sstevel@tonic-gate es |= flags; 7617c478bd9Sstevel@tonic-gate if (!hash_locked) { 7627c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[2]); 7637c478bd9Sstevel@tonic-gate if (!page_try_reclaim_lock(pp, se, es)) { 7647c478bd9Sstevel@tonic-gate /* 7657c478bd9Sstevel@tonic-gate * On a miss, acquire the phm. Then 7667c478bd9Sstevel@tonic-gate * next time, page_lock() will be called, 7677c478bd9Sstevel@tonic-gate * causing a wait if the page is busy. 7687c478bd9Sstevel@tonic-gate * just looping with page_trylock() would 7697c478bd9Sstevel@tonic-gate * get pretty boring. 7707c478bd9Sstevel@tonic-gate */ 7717c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[3]); 7727c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 7737c478bd9Sstevel@tonic-gate mutex_enter(phm); 7747c478bd9Sstevel@tonic-gate hash_locked = 1; 7757c478bd9Sstevel@tonic-gate goto top; 7767c478bd9Sstevel@tonic-gate } 7777c478bd9Sstevel@tonic-gate } else { 7787c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[4]); 7797c478bd9Sstevel@tonic-gate if (!page_lock_es(pp, se, phm, P_RECLAIM, es)) { 7807c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[5]); 7817c478bd9Sstevel@tonic-gate goto top; 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate /* 7867c478bd9Sstevel@tonic-gate * Since `pp' is locked it can not change identity now. 7877c478bd9Sstevel@tonic-gate * Reconfirm we locked the correct page. 7887c478bd9Sstevel@tonic-gate * 7897c478bd9Sstevel@tonic-gate * Both the p_vnode and p_offset *must* be cast volatile 7907c478bd9Sstevel@tonic-gate * to force a reload of their values: The PAGE_HASH_SEARCH 7917c478bd9Sstevel@tonic-gate * macro will have stuffed p_vnode and p_offset into 7927c478bd9Sstevel@tonic-gate * registers before calling page_trylock(); another thread, 7937c478bd9Sstevel@tonic-gate * actually holding the hash lock, could have changed the 7947c478bd9Sstevel@tonic-gate * page's identity in memory, but our registers would not 7957c478bd9Sstevel@tonic-gate * be changed, fooling the reconfirmation. If the hash 7967c478bd9Sstevel@tonic-gate * lock was held during the search, the casting would 7977c478bd9Sstevel@tonic-gate * not be needed. 7987c478bd9Sstevel@tonic-gate */ 7997c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[6]); 8007c478bd9Sstevel@tonic-gate if (((volatile struct vnode *)(pp->p_vnode) != vp) || 8017c478bd9Sstevel@tonic-gate ((volatile u_offset_t)(pp->p_offset) != off)) { 8027c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[7]); 8037c478bd9Sstevel@tonic-gate if (hash_locked) { 8047c478bd9Sstevel@tonic-gate panic("page_lookup_create: lost page %p", 8057c478bd9Sstevel@tonic-gate (void *)pp); 8067c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 8077c478bd9Sstevel@tonic-gate } 8087c478bd9Sstevel@tonic-gate page_unlock(pp); 8097c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 8107c478bd9Sstevel@tonic-gate mutex_enter(phm); 8117c478bd9Sstevel@tonic-gate hash_locked = 1; 8127c478bd9Sstevel@tonic-gate goto top; 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate /* 8167c478bd9Sstevel@tonic-gate * If page_trylock() was called, then pp may still be on 8177c478bd9Sstevel@tonic-gate * the cachelist (can't be on the free list, it would not 8187c478bd9Sstevel@tonic-gate * have been found in the search). If it is on the 8197c478bd9Sstevel@tonic-gate * cachelist it must be pulled now. To pull the page from 8207c478bd9Sstevel@tonic-gate * the cachelist, it must be exclusively locked. 8217c478bd9Sstevel@tonic-gate * 8227c478bd9Sstevel@tonic-gate * The other big difference between page_trylock() and 8237c478bd9Sstevel@tonic-gate * page_lock(), is that page_lock() will pull the 8247c478bd9Sstevel@tonic-gate * page from whatever free list (the cache list in this 8257c478bd9Sstevel@tonic-gate * case) the page is on. If page_trylock() was used 8267c478bd9Sstevel@tonic-gate * above, then we have to do the reclaim ourselves. 8277c478bd9Sstevel@tonic-gate */ 8287c478bd9Sstevel@tonic-gate if ((!hash_locked) && (PP_ISFREE(pp))) { 8297c478bd9Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0); 8307c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[8]); 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate /* 8337c478bd9Sstevel@tonic-gate * page_relcaim will insure that we 8347c478bd9Sstevel@tonic-gate * have this page exclusively 8357c478bd9Sstevel@tonic-gate */ 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate if (!page_reclaim(pp, NULL)) { 8387c478bd9Sstevel@tonic-gate /* 8397c478bd9Sstevel@tonic-gate * Page_reclaim dropped whatever lock 8407c478bd9Sstevel@tonic-gate * we held. 8417c478bd9Sstevel@tonic-gate */ 8427c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[9]); 8437c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 8447c478bd9Sstevel@tonic-gate mutex_enter(phm); 8457c478bd9Sstevel@tonic-gate hash_locked = 1; 8467c478bd9Sstevel@tonic-gate goto top; 8477c478bd9Sstevel@tonic-gate } else if (se == SE_SHARED && newpp == NULL) { 8487c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[10]); 8497c478bd9Sstevel@tonic-gate page_downgrade(pp); 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate } 8527c478bd9Sstevel@tonic-gate 8537c478bd9Sstevel@tonic-gate if (hash_locked) { 8547c478bd9Sstevel@tonic-gate mutex_exit(phm); 8557c478bd9Sstevel@tonic-gate } 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate if (newpp != NULL && pp->p_szc < newpp->p_szc && 8587c478bd9Sstevel@tonic-gate PAGE_EXCL(pp) && nrelocp != NULL) { 8597c478bd9Sstevel@tonic-gate ASSERT(nrelocp != NULL); 8607c478bd9Sstevel@tonic-gate (void) page_relocate(&pp, &newpp, 1, 1, nrelocp, 8617c478bd9Sstevel@tonic-gate NULL); 8627c478bd9Sstevel@tonic-gate if (*nrelocp > 0) { 8637c478bd9Sstevel@tonic-gate VM_STAT_COND_ADD(*nrelocp == 1, 8647c478bd9Sstevel@tonic-gate page_lookup_cnt[11]); 8657c478bd9Sstevel@tonic-gate VM_STAT_COND_ADD(*nrelocp > 1, 8667c478bd9Sstevel@tonic-gate page_lookup_cnt[12]); 8677c478bd9Sstevel@tonic-gate pp = newpp; 8687c478bd9Sstevel@tonic-gate se = SE_EXCL; 8697c478bd9Sstevel@tonic-gate } else { 8707c478bd9Sstevel@tonic-gate if (se == SE_SHARED) { 8717c478bd9Sstevel@tonic-gate page_downgrade(pp); 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[13]); 8747c478bd9Sstevel@tonic-gate } 8757c478bd9Sstevel@tonic-gate } else if (newpp != NULL && nrelocp != NULL) { 8767c478bd9Sstevel@tonic-gate if (PAGE_EXCL(pp) && se == SE_SHARED) { 8777c478bd9Sstevel@tonic-gate page_downgrade(pp); 8787c478bd9Sstevel@tonic-gate } 8797c478bd9Sstevel@tonic-gate VM_STAT_COND_ADD(pp->p_szc < newpp->p_szc, 8807c478bd9Sstevel@tonic-gate page_lookup_cnt[14]); 8817c478bd9Sstevel@tonic-gate VM_STAT_COND_ADD(pp->p_szc == newpp->p_szc, 8827c478bd9Sstevel@tonic-gate page_lookup_cnt[15]); 8837c478bd9Sstevel@tonic-gate VM_STAT_COND_ADD(pp->p_szc > newpp->p_szc, 8847c478bd9Sstevel@tonic-gate page_lookup_cnt[16]); 8857c478bd9Sstevel@tonic-gate } else if (newpp != NULL && PAGE_EXCL(pp)) { 8867c478bd9Sstevel@tonic-gate se = SE_EXCL; 8877c478bd9Sstevel@tonic-gate } 8887c478bd9Sstevel@tonic-gate } else if (!hash_locked) { 8897c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[17]); 8907c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 8917c478bd9Sstevel@tonic-gate mutex_enter(phm); 8927c478bd9Sstevel@tonic-gate hash_locked = 1; 8937c478bd9Sstevel@tonic-gate goto top; 8947c478bd9Sstevel@tonic-gate } else if (newpp != NULL) { 8957c478bd9Sstevel@tonic-gate /* 8967c478bd9Sstevel@tonic-gate * If we have a preallocated page then 8977c478bd9Sstevel@tonic-gate * insert it now and basically behave like 8987c478bd9Sstevel@tonic-gate * page_create. 8997c478bd9Sstevel@tonic-gate */ 9007c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[18]); 9017c478bd9Sstevel@tonic-gate /* 9027c478bd9Sstevel@tonic-gate * Since we hold the page hash mutex and 9037c478bd9Sstevel@tonic-gate * just searched for this page, page_hashin 9047c478bd9Sstevel@tonic-gate * had better not fail. If it does, that 9057c478bd9Sstevel@tonic-gate * means some thread did not follow the 9067c478bd9Sstevel@tonic-gate * page hash mutex rules. Panic now and 9077c478bd9Sstevel@tonic-gate * get it over with. As usual, go down 9087c478bd9Sstevel@tonic-gate * holding all the locks. 9097c478bd9Sstevel@tonic-gate */ 9107c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 9117c478bd9Sstevel@tonic-gate if (!page_hashin(newpp, vp, off, phm)) { 9127c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 9137c478bd9Sstevel@tonic-gate panic("page_lookup_create: hashin failed %p %p %llx %p", 9147c478bd9Sstevel@tonic-gate (void *)newpp, (void *)vp, off, (void *)phm); 9157c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 9167c478bd9Sstevel@tonic-gate } 9177c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 9187c478bd9Sstevel@tonic-gate mutex_exit(phm); 9197c478bd9Sstevel@tonic-gate phm = NULL; 9207c478bd9Sstevel@tonic-gate page_set_props(newpp, P_REF); 9217c478bd9Sstevel@tonic-gate page_io_lock(newpp); 9227c478bd9Sstevel@tonic-gate pp = newpp; 9237c478bd9Sstevel@tonic-gate se = SE_EXCL; 9247c478bd9Sstevel@tonic-gate } else { 9257c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_cnt[19]); 9267c478bd9Sstevel@tonic-gate mutex_exit(phm); 9277c478bd9Sstevel@tonic-gate } 9287c478bd9Sstevel@tonic-gate 9297c478bd9Sstevel@tonic-gate ASSERT(pp ? PAGE_LOCKED_SE(pp, se) : 1); 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate ASSERT(pp ? ((PP_ISFREE(pp) == 0) && (PP_ISAGED(pp) == 0)) : 1); 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate return (pp); 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate /* 9377c478bd9Sstevel@tonic-gate * Search the hash list for the page representing the 9387c478bd9Sstevel@tonic-gate * specified [vp, offset] and return it locked. Skip 9397c478bd9Sstevel@tonic-gate * free pages and pages that cannot be locked as requested. 9407c478bd9Sstevel@tonic-gate * Used while attempting to kluster pages. 9417c478bd9Sstevel@tonic-gate */ 9427c478bd9Sstevel@tonic-gate page_t * 9437c478bd9Sstevel@tonic-gate page_lookup_nowait(vnode_t *vp, u_offset_t off, se_t se) 9447c478bd9Sstevel@tonic-gate { 9457c478bd9Sstevel@tonic-gate page_t *pp; 9467c478bd9Sstevel@tonic-gate kmutex_t *phm; 9477c478bd9Sstevel@tonic-gate ulong_t index; 9487c478bd9Sstevel@tonic-gate uint_t locked; 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 9517c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[0]); 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 9547c478bd9Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 9557c478bd9Sstevel@tonic-gate locked = 0; 9567c478bd9Sstevel@tonic-gate if (pp == NULL) { 9577c478bd9Sstevel@tonic-gate top: 9587c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[1]); 9597c478bd9Sstevel@tonic-gate locked = 1; 9607c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 9617c478bd9Sstevel@tonic-gate mutex_enter(phm); 9627c478bd9Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate if (pp == NULL || PP_ISFREE(pp)) { 9667c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[2]); 9677c478bd9Sstevel@tonic-gate pp = NULL; 9687c478bd9Sstevel@tonic-gate } else { 9697c478bd9Sstevel@tonic-gate if (!page_trylock(pp, se)) { 9707c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[3]); 9717c478bd9Sstevel@tonic-gate pp = NULL; 9727c478bd9Sstevel@tonic-gate } else { 9737c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[4]); 9747c478bd9Sstevel@tonic-gate /* 9757c478bd9Sstevel@tonic-gate * See the comment in page_lookup() 9767c478bd9Sstevel@tonic-gate */ 9777c478bd9Sstevel@tonic-gate if (((volatile struct vnode *)(pp->p_vnode) != vp) || 9787c478bd9Sstevel@tonic-gate ((u_offset_t)(pp->p_offset) != off)) { 9797c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[5]); 9807c478bd9Sstevel@tonic-gate if (locked) { 9817c478bd9Sstevel@tonic-gate panic("page_lookup_nowait %p", 9827c478bd9Sstevel@tonic-gate (void *)pp); 9837c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 9847c478bd9Sstevel@tonic-gate } 9857c478bd9Sstevel@tonic-gate page_unlock(pp); 9867c478bd9Sstevel@tonic-gate goto top; 9877c478bd9Sstevel@tonic-gate } 9887c478bd9Sstevel@tonic-gate if (PP_ISFREE(pp)) { 9897c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[6]); 9907c478bd9Sstevel@tonic-gate page_unlock(pp); 9917c478bd9Sstevel@tonic-gate pp = NULL; 9927c478bd9Sstevel@tonic-gate } 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate } 9957c478bd9Sstevel@tonic-gate if (locked) { 9967c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_lookup_nowait_cnt[7]); 9977c478bd9Sstevel@tonic-gate mutex_exit(phm); 9987c478bd9Sstevel@tonic-gate } 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate ASSERT(pp ? PAGE_LOCKED_SE(pp, se) : 1); 10017c478bd9Sstevel@tonic-gate 10027c478bd9Sstevel@tonic-gate return (pp); 10037c478bd9Sstevel@tonic-gate } 10047c478bd9Sstevel@tonic-gate 10057c478bd9Sstevel@tonic-gate /* 10067c478bd9Sstevel@tonic-gate * Search the hash list for a page with the specified [vp, off] 10077c478bd9Sstevel@tonic-gate * that is known to exist and is already locked. This routine 10087c478bd9Sstevel@tonic-gate * is typically used by segment SOFTUNLOCK routines. 10097c478bd9Sstevel@tonic-gate */ 10107c478bd9Sstevel@tonic-gate page_t * 10117c478bd9Sstevel@tonic-gate page_find(vnode_t *vp, u_offset_t off) 10127c478bd9Sstevel@tonic-gate { 10137c478bd9Sstevel@tonic-gate page_t *pp; 10147c478bd9Sstevel@tonic-gate kmutex_t *phm; 10157c478bd9Sstevel@tonic-gate ulong_t index; 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 10187c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_find_cnt); 10197c478bd9Sstevel@tonic-gate 10207c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 10217c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 10227c478bd9Sstevel@tonic-gate 10237c478bd9Sstevel@tonic-gate mutex_enter(phm); 10247c478bd9Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 10257c478bd9Sstevel@tonic-gate mutex_exit(phm); 10267c478bd9Sstevel@tonic-gate 10274fc2445aSelowe ASSERT(pp == NULL || PAGE_LOCKED(pp) || panicstr); 10287c478bd9Sstevel@tonic-gate return (pp); 10297c478bd9Sstevel@tonic-gate } 10307c478bd9Sstevel@tonic-gate 10317c478bd9Sstevel@tonic-gate /* 10327c478bd9Sstevel@tonic-gate * Determine whether a page with the specified [vp, off] 10337c478bd9Sstevel@tonic-gate * currently exists in the system. Obviously this should 10347c478bd9Sstevel@tonic-gate * only be considered as a hint since nothing prevents the 10357c478bd9Sstevel@tonic-gate * page from disappearing or appearing immediately after 10367c478bd9Sstevel@tonic-gate * the return from this routine. Subsequently, we don't 10377c478bd9Sstevel@tonic-gate * even bother to lock the list. 10387c478bd9Sstevel@tonic-gate */ 10397c478bd9Sstevel@tonic-gate page_t * 10407c478bd9Sstevel@tonic-gate page_exists(vnode_t *vp, u_offset_t off) 10417c478bd9Sstevel@tonic-gate { 10427c478bd9Sstevel@tonic-gate page_t *pp; 10437c478bd9Sstevel@tonic-gate ulong_t index; 10447c478bd9Sstevel@tonic-gate 10457c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 10467c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exists_cnt); 10477c478bd9Sstevel@tonic-gate 10487c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 10497c478bd9Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 10507c478bd9Sstevel@tonic-gate 10517c478bd9Sstevel@tonic-gate return (pp); 10527c478bd9Sstevel@tonic-gate } 10537c478bd9Sstevel@tonic-gate 10547c478bd9Sstevel@tonic-gate /* 10557c478bd9Sstevel@tonic-gate * Determine if physically contiguous pages exist for [vp, off] - [vp, off + 10567c478bd9Sstevel@tonic-gate * page_size(szc)) range. if they exist and ppa is not NULL fill ppa array 10577c478bd9Sstevel@tonic-gate * with these pages locked SHARED. If necessary reclaim pages from 10587c478bd9Sstevel@tonic-gate * freelist. Return 1 if contiguous pages exist and 0 otherwise. 10597c478bd9Sstevel@tonic-gate * 10607c478bd9Sstevel@tonic-gate * If we fail to lock pages still return 1 if pages exist and contiguous. 10617c478bd9Sstevel@tonic-gate * But in this case return value is just a hint. ppa array won't be filled. 10627c478bd9Sstevel@tonic-gate * Caller should initialize ppa[0] as NULL to distinguish return value. 10637c478bd9Sstevel@tonic-gate * 10647c478bd9Sstevel@tonic-gate * Returns 0 if pages don't exist or not physically contiguous. 10657c478bd9Sstevel@tonic-gate * 10667c478bd9Sstevel@tonic-gate * This routine doesn't work for anonymous(swapfs) pages. 10677c478bd9Sstevel@tonic-gate */ 10687c478bd9Sstevel@tonic-gate int 10697c478bd9Sstevel@tonic-gate page_exists_physcontig(vnode_t *vp, u_offset_t off, uint_t szc, page_t *ppa[]) 10707c478bd9Sstevel@tonic-gate { 10717c478bd9Sstevel@tonic-gate pgcnt_t pages; 10727c478bd9Sstevel@tonic-gate pfn_t pfn; 10737c478bd9Sstevel@tonic-gate page_t *rootpp; 10747c478bd9Sstevel@tonic-gate pgcnt_t i; 10757c478bd9Sstevel@tonic-gate pgcnt_t j; 10767c478bd9Sstevel@tonic-gate u_offset_t save_off = off; 10777c478bd9Sstevel@tonic-gate ulong_t index; 10787c478bd9Sstevel@tonic-gate kmutex_t *phm; 10797c478bd9Sstevel@tonic-gate page_t *pp; 10807c478bd9Sstevel@tonic-gate uint_t pszc; 10817c478bd9Sstevel@tonic-gate int loopcnt = 0; 10827c478bd9Sstevel@tonic-gate 10837c478bd9Sstevel@tonic-gate ASSERT(szc != 0); 10847c478bd9Sstevel@tonic-gate ASSERT(vp != NULL); 10857c478bd9Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(vp)); 1086ad23a2dbSjohansen ASSERT(!VN_ISKAS(vp)); 10877c478bd9Sstevel@tonic-gate 10887c478bd9Sstevel@tonic-gate again: 10897c478bd9Sstevel@tonic-gate if (++loopcnt > 3) { 10907c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[0]); 10917c478bd9Sstevel@tonic-gate return (0); 10927c478bd9Sstevel@tonic-gate } 10937c478bd9Sstevel@tonic-gate 10947c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 10957c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 10967c478bd9Sstevel@tonic-gate 10977c478bd9Sstevel@tonic-gate mutex_enter(phm); 10987c478bd9Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 10997c478bd9Sstevel@tonic-gate mutex_exit(phm); 11007c478bd9Sstevel@tonic-gate 11017c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[1]); 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate if (pp == NULL) { 11047c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[2]); 11057c478bd9Sstevel@tonic-gate return (0); 11067c478bd9Sstevel@tonic-gate } 11077c478bd9Sstevel@tonic-gate 11087c478bd9Sstevel@tonic-gate pages = page_get_pagecnt(szc); 11097c478bd9Sstevel@tonic-gate rootpp = pp; 11107c478bd9Sstevel@tonic-gate pfn = rootpp->p_pagenum; 11117c478bd9Sstevel@tonic-gate 11127c478bd9Sstevel@tonic-gate if ((pszc = pp->p_szc) >= szc && ppa != NULL) { 11137c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[3]); 11147c478bd9Sstevel@tonic-gate if (!page_trylock(pp, SE_SHARED)) { 11157c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[4]); 11167c478bd9Sstevel@tonic-gate return (1); 11177c478bd9Sstevel@tonic-gate } 1118*9853d9e8SJason Beloro /* 1119*9853d9e8SJason Beloro * Also check whether p_pagenum was modified by DR. 1120*9853d9e8SJason Beloro */ 11217c478bd9Sstevel@tonic-gate if (pp->p_szc != pszc || pp->p_vnode != vp || 1122*9853d9e8SJason Beloro pp->p_offset != off || pp->p_pagenum != pfn) { 11237c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[5]); 11247c478bd9Sstevel@tonic-gate page_unlock(pp); 11257c478bd9Sstevel@tonic-gate off = save_off; 11267c478bd9Sstevel@tonic-gate goto again; 11277c478bd9Sstevel@tonic-gate } 11287c478bd9Sstevel@tonic-gate /* 11297c478bd9Sstevel@tonic-gate * szc was non zero and vnode and offset matched after we 11307c478bd9Sstevel@tonic-gate * locked the page it means it can't become free on us. 11317c478bd9Sstevel@tonic-gate */ 11327c478bd9Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 11337c478bd9Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, pages)) { 11347c478bd9Sstevel@tonic-gate page_unlock(pp); 11357c478bd9Sstevel@tonic-gate return (0); 11367c478bd9Sstevel@tonic-gate } 11377c478bd9Sstevel@tonic-gate ppa[0] = pp; 11387c478bd9Sstevel@tonic-gate pp++; 11397c478bd9Sstevel@tonic-gate off += PAGESIZE; 11407c478bd9Sstevel@tonic-gate pfn++; 11417c478bd9Sstevel@tonic-gate for (i = 1; i < pages; i++, pp++, off += PAGESIZE, pfn++) { 11427c478bd9Sstevel@tonic-gate if (!page_trylock(pp, SE_SHARED)) { 11437c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[6]); 11447c478bd9Sstevel@tonic-gate pp--; 11457c478bd9Sstevel@tonic-gate while (i-- > 0) { 11467c478bd9Sstevel@tonic-gate page_unlock(pp); 11477c478bd9Sstevel@tonic-gate pp--; 11487c478bd9Sstevel@tonic-gate } 11497c478bd9Sstevel@tonic-gate ppa[0] = NULL; 11507c478bd9Sstevel@tonic-gate return (1); 11517c478bd9Sstevel@tonic-gate } 11527c478bd9Sstevel@tonic-gate if (pp->p_szc != pszc) { 11537c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[7]); 11547c478bd9Sstevel@tonic-gate page_unlock(pp); 11557c478bd9Sstevel@tonic-gate pp--; 11567c478bd9Sstevel@tonic-gate while (i-- > 0) { 11577c478bd9Sstevel@tonic-gate page_unlock(pp); 11587c478bd9Sstevel@tonic-gate pp--; 11597c478bd9Sstevel@tonic-gate } 11607c478bd9Sstevel@tonic-gate ppa[0] = NULL; 11617c478bd9Sstevel@tonic-gate off = save_off; 11627c478bd9Sstevel@tonic-gate goto again; 11637c478bd9Sstevel@tonic-gate } 11647c478bd9Sstevel@tonic-gate /* 11657c478bd9Sstevel@tonic-gate * szc the same as for previous already locked pages 11667c478bd9Sstevel@tonic-gate * with right identity. Since this page had correct 11677c478bd9Sstevel@tonic-gate * szc after we locked it can't get freed or destroyed 11687c478bd9Sstevel@tonic-gate * and therefore must have the expected identity. 11697c478bd9Sstevel@tonic-gate */ 11707c478bd9Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 11717c478bd9Sstevel@tonic-gate if (pp->p_vnode != vp || 11727c478bd9Sstevel@tonic-gate pp->p_offset != off) { 11737c478bd9Sstevel@tonic-gate panic("page_exists_physcontig: " 11747c478bd9Sstevel@tonic-gate "large page identity doesn't match"); 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate ppa[i] = pp; 11777c478bd9Sstevel@tonic-gate ASSERT(pp->p_pagenum == pfn); 11787c478bd9Sstevel@tonic-gate } 11797c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[8]); 11807c478bd9Sstevel@tonic-gate ppa[pages] = NULL; 11817c478bd9Sstevel@tonic-gate return (1); 11827c478bd9Sstevel@tonic-gate } else if (pszc >= szc) { 11837c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[9]); 11847c478bd9Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, pages)) { 11857c478bd9Sstevel@tonic-gate return (0); 11867c478bd9Sstevel@tonic-gate } 11877c478bd9Sstevel@tonic-gate return (1); 11887c478bd9Sstevel@tonic-gate } 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, pages)) { 11917c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[10]); 11927c478bd9Sstevel@tonic-gate return (0); 11937c478bd9Sstevel@tonic-gate } 11947c478bd9Sstevel@tonic-gate 11957c478bd9Sstevel@tonic-gate if (page_numtomemseg_nolock(pfn) != 11967c478bd9Sstevel@tonic-gate page_numtomemseg_nolock(pfn + pages - 1)) { 11977c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[11]); 11987c478bd9Sstevel@tonic-gate return (0); 11997c478bd9Sstevel@tonic-gate } 12007c478bd9Sstevel@tonic-gate 12017c478bd9Sstevel@tonic-gate /* 12027c478bd9Sstevel@tonic-gate * We loop up 4 times across pages to promote page size. 12037c478bd9Sstevel@tonic-gate * We're extra cautious to promote page size atomically with respect 12047c478bd9Sstevel@tonic-gate * to everybody else. But we can probably optimize into 1 loop if 12057c478bd9Sstevel@tonic-gate * this becomes an issue. 12067c478bd9Sstevel@tonic-gate */ 12077c478bd9Sstevel@tonic-gate 12087c478bd9Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++, off += PAGESIZE, pfn++) { 12097c478bd9Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) { 12107c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[12]); 12117c478bd9Sstevel@tonic-gate break; 12127c478bd9Sstevel@tonic-gate } 1213*9853d9e8SJason Beloro /* 1214*9853d9e8SJason Beloro * Check whether p_pagenum was modified by DR. 1215*9853d9e8SJason Beloro */ 1216*9853d9e8SJason Beloro if (pp->p_pagenum != pfn) { 1217*9853d9e8SJason Beloro page_unlock(pp); 1218*9853d9e8SJason Beloro break; 1219*9853d9e8SJason Beloro } 12207c478bd9Sstevel@tonic-gate if (pp->p_vnode != vp || 12217c478bd9Sstevel@tonic-gate pp->p_offset != off) { 12227c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[13]); 12237c478bd9Sstevel@tonic-gate page_unlock(pp); 12247c478bd9Sstevel@tonic-gate break; 12257c478bd9Sstevel@tonic-gate } 12267c478bd9Sstevel@tonic-gate if (pp->p_szc >= szc) { 12277c478bd9Sstevel@tonic-gate ASSERT(i == 0); 12287c478bd9Sstevel@tonic-gate page_unlock(pp); 12297c478bd9Sstevel@tonic-gate off = save_off; 12307c478bd9Sstevel@tonic-gate goto again; 12317c478bd9Sstevel@tonic-gate } 12327c478bd9Sstevel@tonic-gate } 12337c478bd9Sstevel@tonic-gate 12347c478bd9Sstevel@tonic-gate if (i != pages) { 12357c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[14]); 12367c478bd9Sstevel@tonic-gate --pp; 12377c478bd9Sstevel@tonic-gate while (i-- > 0) { 12387c478bd9Sstevel@tonic-gate page_unlock(pp); 12397c478bd9Sstevel@tonic-gate --pp; 12407c478bd9Sstevel@tonic-gate } 12417c478bd9Sstevel@tonic-gate return (0); 12427c478bd9Sstevel@tonic-gate } 12437c478bd9Sstevel@tonic-gate 12447c478bd9Sstevel@tonic-gate pp = rootpp; 12457c478bd9Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++) { 12467c478bd9Sstevel@tonic-gate if (PP_ISFREE(pp)) { 12477c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[15]); 12487c478bd9Sstevel@tonic-gate ASSERT(!PP_ISAGED(pp)); 12497c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 12507c478bd9Sstevel@tonic-gate if (!page_reclaim(pp, NULL)) { 12517c478bd9Sstevel@tonic-gate break; 12527c478bd9Sstevel@tonic-gate } 12537c478bd9Sstevel@tonic-gate } else { 12547c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc < szc); 12557c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[16]); 12567c478bd9Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 12577c478bd9Sstevel@tonic-gate } 12587c478bd9Sstevel@tonic-gate } 12597c478bd9Sstevel@tonic-gate if (i < pages) { 12607c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[17]); 12617c478bd9Sstevel@tonic-gate /* 12627c478bd9Sstevel@tonic-gate * page_reclaim failed because we were out of memory. 12637c478bd9Sstevel@tonic-gate * drop the rest of the locks and return because this page 12647c478bd9Sstevel@tonic-gate * must be already reallocated anyway. 12657c478bd9Sstevel@tonic-gate */ 12667c478bd9Sstevel@tonic-gate pp = rootpp; 12677c478bd9Sstevel@tonic-gate for (j = 0; j < pages; j++, pp++) { 12687c478bd9Sstevel@tonic-gate if (j != i) { 12697c478bd9Sstevel@tonic-gate page_unlock(pp); 12707c478bd9Sstevel@tonic-gate } 12717c478bd9Sstevel@tonic-gate } 12727c478bd9Sstevel@tonic-gate return (0); 12737c478bd9Sstevel@tonic-gate } 12747c478bd9Sstevel@tonic-gate 12757c478bd9Sstevel@tonic-gate off = save_off; 12767c478bd9Sstevel@tonic-gate pp = rootpp; 12777c478bd9Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++, off += PAGESIZE) { 12787c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 12797c478bd9Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 12807c478bd9Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 12817c478bd9Sstevel@tonic-gate ASSERT(pp->p_vnode == vp); 12827c478bd9Sstevel@tonic-gate ASSERT(pp->p_offset == off); 12837c478bd9Sstevel@tonic-gate pp->p_szc = szc; 12847c478bd9Sstevel@tonic-gate } 12857c478bd9Sstevel@tonic-gate pp = rootpp; 12867c478bd9Sstevel@tonic-gate for (i = 0; i < pages; i++, pp++) { 12877c478bd9Sstevel@tonic-gate if (ppa == NULL) { 12887c478bd9Sstevel@tonic-gate page_unlock(pp); 12897c478bd9Sstevel@tonic-gate } else { 12907c478bd9Sstevel@tonic-gate ppa[i] = pp; 12917c478bd9Sstevel@tonic-gate page_downgrade(ppa[i]); 12927c478bd9Sstevel@tonic-gate } 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate if (ppa != NULL) { 12957c478bd9Sstevel@tonic-gate ppa[pages] = NULL; 12967c478bd9Sstevel@tonic-gate } 12977c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exphcontg[18]); 12987c478bd9Sstevel@tonic-gate ASSERT(vp->v_pages != NULL); 12997c478bd9Sstevel@tonic-gate return (1); 13007c478bd9Sstevel@tonic-gate } 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate /* 13037c478bd9Sstevel@tonic-gate * Determine whether a page with the specified [vp, off] 13047c478bd9Sstevel@tonic-gate * currently exists in the system and if so return its 13057c478bd9Sstevel@tonic-gate * size code. Obviously this should only be considered as 13067c478bd9Sstevel@tonic-gate * a hint since nothing prevents the page from disappearing 13077c478bd9Sstevel@tonic-gate * or appearing immediately after the return from this routine. 13087c478bd9Sstevel@tonic-gate */ 13097c478bd9Sstevel@tonic-gate int 13107c478bd9Sstevel@tonic-gate page_exists_forreal(vnode_t *vp, u_offset_t off, uint_t *szc) 13117c478bd9Sstevel@tonic-gate { 13127c478bd9Sstevel@tonic-gate page_t *pp; 13137c478bd9Sstevel@tonic-gate kmutex_t *phm; 13147c478bd9Sstevel@tonic-gate ulong_t index; 13157c478bd9Sstevel@tonic-gate int rc = 0; 13167c478bd9Sstevel@tonic-gate 13177c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 13187c478bd9Sstevel@tonic-gate ASSERT(szc != NULL); 13197c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_exists_forreal_cnt); 13207c478bd9Sstevel@tonic-gate 13217c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 13227c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 13237c478bd9Sstevel@tonic-gate 13247c478bd9Sstevel@tonic-gate mutex_enter(phm); 13257c478bd9Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 13267c478bd9Sstevel@tonic-gate if (pp != NULL) { 13277c478bd9Sstevel@tonic-gate *szc = pp->p_szc; 13287c478bd9Sstevel@tonic-gate rc = 1; 13297c478bd9Sstevel@tonic-gate } 13307c478bd9Sstevel@tonic-gate mutex_exit(phm); 13317c478bd9Sstevel@tonic-gate return (rc); 13327c478bd9Sstevel@tonic-gate } 13337c478bd9Sstevel@tonic-gate 13347c478bd9Sstevel@tonic-gate /* wakeup threads waiting for pages in page_create_get_something() */ 13357c478bd9Sstevel@tonic-gate void 13367c478bd9Sstevel@tonic-gate wakeup_pcgs(void) 13377c478bd9Sstevel@tonic-gate { 13387c478bd9Sstevel@tonic-gate if (!CV_HAS_WAITERS(&pcgs_cv)) 13397c478bd9Sstevel@tonic-gate return; 13407c478bd9Sstevel@tonic-gate cv_broadcast(&pcgs_cv); 13417c478bd9Sstevel@tonic-gate } 13427c478bd9Sstevel@tonic-gate 13437c478bd9Sstevel@tonic-gate /* 13447c478bd9Sstevel@tonic-gate * 'freemem' is used all over the kernel as an indication of how many 13457c478bd9Sstevel@tonic-gate * pages are free (either on the cache list or on the free page list) 13467c478bd9Sstevel@tonic-gate * in the system. In very few places is a really accurate 'freemem' 13477c478bd9Sstevel@tonic-gate * needed. To avoid contention of the lock protecting a the 13487c478bd9Sstevel@tonic-gate * single freemem, it was spread out into NCPU buckets. Set_freemem 13497c478bd9Sstevel@tonic-gate * sets freemem to the total of all NCPU buckets. It is called from 13507c478bd9Sstevel@tonic-gate * clock() on each TICK. 13517c478bd9Sstevel@tonic-gate */ 13527c478bd9Sstevel@tonic-gate void 13537c478bd9Sstevel@tonic-gate set_freemem() 13547c478bd9Sstevel@tonic-gate { 13557c478bd9Sstevel@tonic-gate struct pcf *p; 13567c478bd9Sstevel@tonic-gate ulong_t t; 13577c478bd9Sstevel@tonic-gate uint_t i; 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate t = 0; 13607c478bd9Sstevel@tonic-gate p = pcf; 136106fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 13627c478bd9Sstevel@tonic-gate t += p->pcf_count; 13637c478bd9Sstevel@tonic-gate p++; 13647c478bd9Sstevel@tonic-gate } 13657c478bd9Sstevel@tonic-gate freemem = t; 13667c478bd9Sstevel@tonic-gate 13677c478bd9Sstevel@tonic-gate /* 13687c478bd9Sstevel@tonic-gate * Don't worry about grabbing mutex. It's not that 13697c478bd9Sstevel@tonic-gate * critical if we miss a tick or two. This is 13707c478bd9Sstevel@tonic-gate * where we wakeup possible delayers in 13717c478bd9Sstevel@tonic-gate * page_create_get_something(). 13727c478bd9Sstevel@tonic-gate */ 13737c478bd9Sstevel@tonic-gate wakeup_pcgs(); 13747c478bd9Sstevel@tonic-gate } 13757c478bd9Sstevel@tonic-gate 13767c478bd9Sstevel@tonic-gate ulong_t 13777c478bd9Sstevel@tonic-gate get_freemem() 13787c478bd9Sstevel@tonic-gate { 13797c478bd9Sstevel@tonic-gate struct pcf *p; 13807c478bd9Sstevel@tonic-gate ulong_t t; 13817c478bd9Sstevel@tonic-gate uint_t i; 13827c478bd9Sstevel@tonic-gate 13837c478bd9Sstevel@tonic-gate t = 0; 13847c478bd9Sstevel@tonic-gate p = pcf; 138506fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 13867c478bd9Sstevel@tonic-gate t += p->pcf_count; 13877c478bd9Sstevel@tonic-gate p++; 13887c478bd9Sstevel@tonic-gate } 13897c478bd9Sstevel@tonic-gate /* 13907c478bd9Sstevel@tonic-gate * We just calculated it, might as well set it. 13917c478bd9Sstevel@tonic-gate */ 13927c478bd9Sstevel@tonic-gate freemem = t; 13937c478bd9Sstevel@tonic-gate return (t); 13947c478bd9Sstevel@tonic-gate } 13957c478bd9Sstevel@tonic-gate 13967c478bd9Sstevel@tonic-gate /* 13977c478bd9Sstevel@tonic-gate * Acquire all of the page cache & free (pcf) locks. 13987c478bd9Sstevel@tonic-gate */ 13997c478bd9Sstevel@tonic-gate void 14007c478bd9Sstevel@tonic-gate pcf_acquire_all() 14017c478bd9Sstevel@tonic-gate { 14027c478bd9Sstevel@tonic-gate struct pcf *p; 14037c478bd9Sstevel@tonic-gate uint_t i; 14047c478bd9Sstevel@tonic-gate 14057c478bd9Sstevel@tonic-gate p = pcf; 140606fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 14077c478bd9Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 14087c478bd9Sstevel@tonic-gate p++; 14097c478bd9Sstevel@tonic-gate } 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate /* 14137c478bd9Sstevel@tonic-gate * Release all the pcf_locks. 14147c478bd9Sstevel@tonic-gate */ 14157c478bd9Sstevel@tonic-gate void 14167c478bd9Sstevel@tonic-gate pcf_release_all() 14177c478bd9Sstevel@tonic-gate { 14187c478bd9Sstevel@tonic-gate struct pcf *p; 14197c478bd9Sstevel@tonic-gate uint_t i; 14207c478bd9Sstevel@tonic-gate 14217c478bd9Sstevel@tonic-gate p = pcf; 142206fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 14237c478bd9Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 14247c478bd9Sstevel@tonic-gate p++; 14257c478bd9Sstevel@tonic-gate } 14267c478bd9Sstevel@tonic-gate } 14277c478bd9Sstevel@tonic-gate 14287c478bd9Sstevel@tonic-gate /* 14297c478bd9Sstevel@tonic-gate * Inform the VM system that we need some pages freed up. 14307c478bd9Sstevel@tonic-gate * Calls must be symmetric, e.g.: 14317c478bd9Sstevel@tonic-gate * 14327c478bd9Sstevel@tonic-gate * page_needfree(100); 14337c478bd9Sstevel@tonic-gate * wait a bit; 14347c478bd9Sstevel@tonic-gate * page_needfree(-100); 14357c478bd9Sstevel@tonic-gate */ 14367c478bd9Sstevel@tonic-gate void 14377c478bd9Sstevel@tonic-gate page_needfree(spgcnt_t npages) 14387c478bd9Sstevel@tonic-gate { 14397c478bd9Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 14407c478bd9Sstevel@tonic-gate needfree += npages; 14417c478bd9Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 14427c478bd9Sstevel@tonic-gate } 14437c478bd9Sstevel@tonic-gate 14447c478bd9Sstevel@tonic-gate /* 14457c478bd9Sstevel@tonic-gate * Throttle for page_create(): try to prevent freemem from dropping 14467c478bd9Sstevel@tonic-gate * below throttlefree. We can't provide a 100% guarantee because 14477c478bd9Sstevel@tonic-gate * KM_NOSLEEP allocations, page_reclaim(), and various other things 14487c478bd9Sstevel@tonic-gate * nibble away at the freelist. However, we can block all PG_WAIT 14497c478bd9Sstevel@tonic-gate * allocations until memory becomes available. The motivation is 14507c478bd9Sstevel@tonic-gate * that several things can fall apart when there's no free memory: 14517c478bd9Sstevel@tonic-gate * 14527c478bd9Sstevel@tonic-gate * (1) If pageout() needs memory to push a page, the system deadlocks. 14537c478bd9Sstevel@tonic-gate * 14547c478bd9Sstevel@tonic-gate * (2) By (broken) specification, timeout(9F) can neither fail nor 14557c478bd9Sstevel@tonic-gate * block, so it has no choice but to panic the system if it 14567c478bd9Sstevel@tonic-gate * cannot allocate a callout structure. 14577c478bd9Sstevel@tonic-gate * 14587c478bd9Sstevel@tonic-gate * (3) Like timeout(), ddi_set_callback() cannot fail and cannot block; 14597c478bd9Sstevel@tonic-gate * it panics if it cannot allocate a callback structure. 14607c478bd9Sstevel@tonic-gate * 14617c478bd9Sstevel@tonic-gate * (4) Untold numbers of third-party drivers have not yet been hardened 14627c478bd9Sstevel@tonic-gate * against KM_NOSLEEP and/or allocb() failures; they simply assume 14637c478bd9Sstevel@tonic-gate * success and panic the system with a data fault on failure. 14647c478bd9Sstevel@tonic-gate * (The long-term solution to this particular problem is to ship 14657c478bd9Sstevel@tonic-gate * hostile fault-injecting DEBUG kernels with the DDK.) 14667c478bd9Sstevel@tonic-gate * 14677c478bd9Sstevel@tonic-gate * It is theoretically impossible to guarantee success of non-blocking 14687c478bd9Sstevel@tonic-gate * allocations, but in practice, this throttle is very hard to break. 14697c478bd9Sstevel@tonic-gate */ 14707c478bd9Sstevel@tonic-gate static int 14717c478bd9Sstevel@tonic-gate page_create_throttle(pgcnt_t npages, int flags) 14727c478bd9Sstevel@tonic-gate { 14737c478bd9Sstevel@tonic-gate ulong_t fm; 14747c478bd9Sstevel@tonic-gate uint_t i; 14757c478bd9Sstevel@tonic-gate pgcnt_t tf; /* effective value of throttlefree */ 14767c478bd9Sstevel@tonic-gate 14777c478bd9Sstevel@tonic-gate /* 14787c478bd9Sstevel@tonic-gate * Never deny pages when: 14797c478bd9Sstevel@tonic-gate * - it's a thread that cannot block [NOMEMWAIT()] 14807c478bd9Sstevel@tonic-gate * - the allocation cannot block and must not fail 14817c478bd9Sstevel@tonic-gate * - the allocation cannot block and is pageout dispensated 14827c478bd9Sstevel@tonic-gate */ 14837c478bd9Sstevel@tonic-gate if (NOMEMWAIT() || 14847c478bd9Sstevel@tonic-gate ((flags & (PG_WAIT | PG_PANIC)) == PG_PANIC) || 14857c478bd9Sstevel@tonic-gate ((flags & (PG_WAIT | PG_PUSHPAGE)) == PG_PUSHPAGE)) 14867c478bd9Sstevel@tonic-gate return (1); 14877c478bd9Sstevel@tonic-gate 14887c478bd9Sstevel@tonic-gate /* 14897c478bd9Sstevel@tonic-gate * If the allocation can't block, we look favorably upon it 14907c478bd9Sstevel@tonic-gate * unless we're below pageout_reserve. In that case we fail 14917c478bd9Sstevel@tonic-gate * the allocation because we want to make sure there are a few 14927c478bd9Sstevel@tonic-gate * pages available for pageout. 14937c478bd9Sstevel@tonic-gate */ 14947c478bd9Sstevel@tonic-gate if ((flags & PG_WAIT) == 0) 14957c478bd9Sstevel@tonic-gate return (freemem >= npages + pageout_reserve); 14967c478bd9Sstevel@tonic-gate 14977c478bd9Sstevel@tonic-gate /* Calculate the effective throttlefree value */ 14987c478bd9Sstevel@tonic-gate tf = throttlefree - 14997c478bd9Sstevel@tonic-gate ((flags & PG_PUSHPAGE) ? pageout_reserve : 0); 15007c478bd9Sstevel@tonic-gate 15017c478bd9Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 15027c478bd9Sstevel@tonic-gate 150378b03d3aSkchow for (;;) { 150478b03d3aSkchow fm = 0; 15057c478bd9Sstevel@tonic-gate pcf_acquire_all(); 15067c478bd9Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 150706fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 15087c478bd9Sstevel@tonic-gate fm += pcf[i].pcf_count; 15097c478bd9Sstevel@tonic-gate pcf[i].pcf_wait++; 15107c478bd9Sstevel@tonic-gate mutex_exit(&pcf[i].pcf_lock); 15117c478bd9Sstevel@tonic-gate } 15127c478bd9Sstevel@tonic-gate freemem = fm; 151378b03d3aSkchow if (freemem >= npages + tf) { 151478b03d3aSkchow mutex_exit(&new_freemem_lock); 151578b03d3aSkchow break; 151678b03d3aSkchow } 15177c478bd9Sstevel@tonic-gate needfree += npages; 15187c478bd9Sstevel@tonic-gate freemem_wait++; 15197c478bd9Sstevel@tonic-gate cv_wait(&freemem_cv, &new_freemem_lock); 15207c478bd9Sstevel@tonic-gate freemem_wait--; 15217c478bd9Sstevel@tonic-gate needfree -= npages; 15227c478bd9Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 15237c478bd9Sstevel@tonic-gate } 15247c478bd9Sstevel@tonic-gate return (1); 15257c478bd9Sstevel@tonic-gate } 15267c478bd9Sstevel@tonic-gate 15277c478bd9Sstevel@tonic-gate /* 1528da6c28aaSamw * page_create_wait() is called to either coalesce pages from the 15297c478bd9Sstevel@tonic-gate * different pcf buckets or to wait because there simply are not 15307c478bd9Sstevel@tonic-gate * enough pages to satisfy the caller's request. 15317c478bd9Sstevel@tonic-gate * 15327c478bd9Sstevel@tonic-gate * Sadly, this is called from platform/vm/vm_machdep.c 15337c478bd9Sstevel@tonic-gate */ 15347c478bd9Sstevel@tonic-gate int 153506fb6a36Sdv page_create_wait(pgcnt_t npages, uint_t flags) 15367c478bd9Sstevel@tonic-gate { 15377c478bd9Sstevel@tonic-gate pgcnt_t total; 15387c478bd9Sstevel@tonic-gate uint_t i; 15397c478bd9Sstevel@tonic-gate struct pcf *p; 15407c478bd9Sstevel@tonic-gate 15417c478bd9Sstevel@tonic-gate /* 15427c478bd9Sstevel@tonic-gate * Wait until there are enough free pages to satisfy our 15437c478bd9Sstevel@tonic-gate * entire request. 15447c478bd9Sstevel@tonic-gate * We set needfree += npages before prodding pageout, to make sure 15457c478bd9Sstevel@tonic-gate * it does real work when npages > lotsfree > freemem. 15467c478bd9Sstevel@tonic-gate */ 15477c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_not_enough); 15487c478bd9Sstevel@tonic-gate 15497c478bd9Sstevel@tonic-gate ASSERT(!kcage_on ? !(flags & PG_NORELOC) : 1); 15507c478bd9Sstevel@tonic-gate checkagain: 15517c478bd9Sstevel@tonic-gate if ((flags & PG_NORELOC) && 15527c478bd9Sstevel@tonic-gate kcage_freemem < kcage_throttlefree + npages) 15537c478bd9Sstevel@tonic-gate (void) kcage_create_throttle(npages, flags); 15547c478bd9Sstevel@tonic-gate 15557c478bd9Sstevel@tonic-gate if (freemem < npages + throttlefree) 15567c478bd9Sstevel@tonic-gate if (!page_create_throttle(npages, flags)) 15577c478bd9Sstevel@tonic-gate return (0); 15587c478bd9Sstevel@tonic-gate 155906fb6a36Sdv if (pcf_decrement_bucket(npages) || 156006fb6a36Sdv pcf_decrement_multiple(&total, npages, 0)) 156106fb6a36Sdv return (1); 15627c478bd9Sstevel@tonic-gate 15637c478bd9Sstevel@tonic-gate /* 15647c478bd9Sstevel@tonic-gate * All of the pcf locks are held, there are not enough pages 15657c478bd9Sstevel@tonic-gate * to satisfy the request (npages < total). 15667c478bd9Sstevel@tonic-gate * Be sure to acquire the new_freemem_lock before dropping 15677c478bd9Sstevel@tonic-gate * the pcf locks. This prevents dropping wakeups in page_free(). 15687c478bd9Sstevel@tonic-gate * The order is always pcf_lock then new_freemem_lock. 15697c478bd9Sstevel@tonic-gate * 15707c478bd9Sstevel@tonic-gate * Since we hold all the pcf locks, it is a good time to set freemem. 15717c478bd9Sstevel@tonic-gate * 15727c478bd9Sstevel@tonic-gate * If the caller does not want to wait, return now. 15737c478bd9Sstevel@tonic-gate * Else turn the pageout daemon loose to find something 15747c478bd9Sstevel@tonic-gate * and wait till it does. 15757c478bd9Sstevel@tonic-gate * 15767c478bd9Sstevel@tonic-gate */ 15777c478bd9Sstevel@tonic-gate freemem = total; 15787c478bd9Sstevel@tonic-gate 15797c478bd9Sstevel@tonic-gate if ((flags & PG_WAIT) == 0) { 15807c478bd9Sstevel@tonic-gate pcf_release_all(); 15817c478bd9Sstevel@tonic-gate 15827c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_NOMEM, 15837c478bd9Sstevel@tonic-gate "page_create_nomem:npages %ld freemem %ld", npages, freemem); 15847c478bd9Sstevel@tonic-gate return (0); 15857c478bd9Sstevel@tonic-gate } 15867c478bd9Sstevel@tonic-gate 15877c478bd9Sstevel@tonic-gate ASSERT(proc_pageout != NULL); 15887c478bd9Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 15897c478bd9Sstevel@tonic-gate 15907c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SLEEP_START, 15917c478bd9Sstevel@tonic-gate "page_create_sleep_start: freemem %ld needfree %ld", 15927c478bd9Sstevel@tonic-gate freemem, needfree); 15937c478bd9Sstevel@tonic-gate 15947c478bd9Sstevel@tonic-gate /* 15957c478bd9Sstevel@tonic-gate * We are going to wait. 15967c478bd9Sstevel@tonic-gate * We currently hold all of the pcf_locks, 15977c478bd9Sstevel@tonic-gate * get the new_freemem_lock (it protects freemem_wait), 15987c478bd9Sstevel@tonic-gate * before dropping the pcf_locks. 15997c478bd9Sstevel@tonic-gate */ 16007c478bd9Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 16017c478bd9Sstevel@tonic-gate 16027c478bd9Sstevel@tonic-gate p = pcf; 160306fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 16047c478bd9Sstevel@tonic-gate p->pcf_wait++; 16057c478bd9Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 16067c478bd9Sstevel@tonic-gate p++; 16077c478bd9Sstevel@tonic-gate } 16087c478bd9Sstevel@tonic-gate 16097c478bd9Sstevel@tonic-gate needfree += npages; 16107c478bd9Sstevel@tonic-gate freemem_wait++; 16117c478bd9Sstevel@tonic-gate 16127c478bd9Sstevel@tonic-gate cv_wait(&freemem_cv, &new_freemem_lock); 16137c478bd9Sstevel@tonic-gate 16147c478bd9Sstevel@tonic-gate freemem_wait--; 16157c478bd9Sstevel@tonic-gate needfree -= npages; 16167c478bd9Sstevel@tonic-gate 16177c478bd9Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 16187c478bd9Sstevel@tonic-gate 16197c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SLEEP_END, 16207c478bd9Sstevel@tonic-gate "page_create_sleep_end: freemem %ld needfree %ld", 16217c478bd9Sstevel@tonic-gate freemem, needfree); 16227c478bd9Sstevel@tonic-gate 16237c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_not_enough_again); 16247c478bd9Sstevel@tonic-gate goto checkagain; 16257c478bd9Sstevel@tonic-gate } 16267c478bd9Sstevel@tonic-gate /* 16277c478bd9Sstevel@tonic-gate * A routine to do the opposite of page_create_wait(). 16287c478bd9Sstevel@tonic-gate */ 16297c478bd9Sstevel@tonic-gate void 16307c478bd9Sstevel@tonic-gate page_create_putback(spgcnt_t npages) 16317c478bd9Sstevel@tonic-gate { 16327c478bd9Sstevel@tonic-gate struct pcf *p; 16337c478bd9Sstevel@tonic-gate pgcnt_t lump; 16347c478bd9Sstevel@tonic-gate uint_t *which; 16357c478bd9Sstevel@tonic-gate 16367c478bd9Sstevel@tonic-gate /* 16377c478bd9Sstevel@tonic-gate * When a contiguous lump is broken up, we have to 16387c478bd9Sstevel@tonic-gate * deal with lots of pages (min 64) so lets spread 16397c478bd9Sstevel@tonic-gate * the wealth around. 16407c478bd9Sstevel@tonic-gate */ 164106fb6a36Sdv lump = roundup(npages, pcf_fanout) / pcf_fanout; 16427c478bd9Sstevel@tonic-gate freemem += npages; 16437c478bd9Sstevel@tonic-gate 164406fb6a36Sdv for (p = pcf; (npages > 0) && (p < &pcf[pcf_fanout]); p++) { 16457c478bd9Sstevel@tonic-gate which = &p->pcf_count; 16467c478bd9Sstevel@tonic-gate 16477c478bd9Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 16487c478bd9Sstevel@tonic-gate 16497c478bd9Sstevel@tonic-gate if (p->pcf_block) { 16507c478bd9Sstevel@tonic-gate which = &p->pcf_reserve; 16517c478bd9Sstevel@tonic-gate } 16527c478bd9Sstevel@tonic-gate 16537c478bd9Sstevel@tonic-gate if (lump < npages) { 16547c478bd9Sstevel@tonic-gate *which += (uint_t)lump; 16557c478bd9Sstevel@tonic-gate npages -= lump; 16567c478bd9Sstevel@tonic-gate } else { 16577c478bd9Sstevel@tonic-gate *which += (uint_t)npages; 16587c478bd9Sstevel@tonic-gate npages = 0; 16597c478bd9Sstevel@tonic-gate } 16607c478bd9Sstevel@tonic-gate 16617c478bd9Sstevel@tonic-gate if (p->pcf_wait) { 16627c478bd9Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 16637c478bd9Sstevel@tonic-gate /* 16647c478bd9Sstevel@tonic-gate * Check to see if some other thread 16657c478bd9Sstevel@tonic-gate * is actually waiting. Another bucket 16667c478bd9Sstevel@tonic-gate * may have woken it up by now. If there 16677c478bd9Sstevel@tonic-gate * are no waiters, then set our pcf_wait 16687c478bd9Sstevel@tonic-gate * count to zero to avoid coming in here 16697c478bd9Sstevel@tonic-gate * next time. 16707c478bd9Sstevel@tonic-gate */ 16717c478bd9Sstevel@tonic-gate if (freemem_wait) { 16727c478bd9Sstevel@tonic-gate if (npages > 1) { 16737c478bd9Sstevel@tonic-gate cv_broadcast(&freemem_cv); 16747c478bd9Sstevel@tonic-gate } else { 16757c478bd9Sstevel@tonic-gate cv_signal(&freemem_cv); 16767c478bd9Sstevel@tonic-gate } 16777c478bd9Sstevel@tonic-gate p->pcf_wait--; 16787c478bd9Sstevel@tonic-gate } else { 16797c478bd9Sstevel@tonic-gate p->pcf_wait = 0; 16807c478bd9Sstevel@tonic-gate } 16817c478bd9Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 16827c478bd9Sstevel@tonic-gate } 16837c478bd9Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 16847c478bd9Sstevel@tonic-gate } 16857c478bd9Sstevel@tonic-gate ASSERT(npages == 0); 16867c478bd9Sstevel@tonic-gate } 16877c478bd9Sstevel@tonic-gate 16887c478bd9Sstevel@tonic-gate /* 16897c478bd9Sstevel@tonic-gate * A helper routine for page_create_get_something. 16907c478bd9Sstevel@tonic-gate * The indenting got to deep down there. 16917c478bd9Sstevel@tonic-gate * Unblock the pcf counters. Any pages freed after 16927c478bd9Sstevel@tonic-gate * pcf_block got set are moved to pcf_count and 16937c478bd9Sstevel@tonic-gate * wakeups (cv_broadcast() or cv_signal()) are done as needed. 16947c478bd9Sstevel@tonic-gate */ 16957c478bd9Sstevel@tonic-gate static void 16967c478bd9Sstevel@tonic-gate pcgs_unblock(void) 16977c478bd9Sstevel@tonic-gate { 16987c478bd9Sstevel@tonic-gate int i; 16997c478bd9Sstevel@tonic-gate struct pcf *p; 17007c478bd9Sstevel@tonic-gate 17017c478bd9Sstevel@tonic-gate /* Update freemem while we're here. */ 17027c478bd9Sstevel@tonic-gate freemem = 0; 17037c478bd9Sstevel@tonic-gate p = pcf; 170406fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 17057c478bd9Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 17067c478bd9Sstevel@tonic-gate ASSERT(p->pcf_count == 0); 17077c478bd9Sstevel@tonic-gate p->pcf_count = p->pcf_reserve; 17087c478bd9Sstevel@tonic-gate p->pcf_block = 0; 17097c478bd9Sstevel@tonic-gate freemem += p->pcf_count; 17107c478bd9Sstevel@tonic-gate if (p->pcf_wait) { 17117c478bd9Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 17127c478bd9Sstevel@tonic-gate if (freemem_wait) { 17137c478bd9Sstevel@tonic-gate if (p->pcf_reserve > 1) { 17147c478bd9Sstevel@tonic-gate cv_broadcast(&freemem_cv); 17157c478bd9Sstevel@tonic-gate p->pcf_wait = 0; 17167c478bd9Sstevel@tonic-gate } else { 17177c478bd9Sstevel@tonic-gate cv_signal(&freemem_cv); 17187c478bd9Sstevel@tonic-gate p->pcf_wait--; 17197c478bd9Sstevel@tonic-gate } 17207c478bd9Sstevel@tonic-gate } else { 17217c478bd9Sstevel@tonic-gate p->pcf_wait = 0; 17227c478bd9Sstevel@tonic-gate } 17237c478bd9Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 17247c478bd9Sstevel@tonic-gate } 17257c478bd9Sstevel@tonic-gate p->pcf_reserve = 0; 17267c478bd9Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 17277c478bd9Sstevel@tonic-gate p++; 17287c478bd9Sstevel@tonic-gate } 17297c478bd9Sstevel@tonic-gate } 17307c478bd9Sstevel@tonic-gate 17317c478bd9Sstevel@tonic-gate /* 17327c478bd9Sstevel@tonic-gate * Called from page_create_va() when both the cache and free lists 17337c478bd9Sstevel@tonic-gate * have been checked once. 17347c478bd9Sstevel@tonic-gate * 17357c478bd9Sstevel@tonic-gate * Either returns a page or panics since the accounting was done 17367c478bd9Sstevel@tonic-gate * way before we got here. 17377c478bd9Sstevel@tonic-gate * 17387c478bd9Sstevel@tonic-gate * We don't come here often, so leave the accounting on permanently. 17397c478bd9Sstevel@tonic-gate */ 17407c478bd9Sstevel@tonic-gate 17417c478bd9Sstevel@tonic-gate #define MAX_PCGS 100 17427c478bd9Sstevel@tonic-gate 17437c478bd9Sstevel@tonic-gate #ifdef DEBUG 17447c478bd9Sstevel@tonic-gate #define PCGS_TRIES 100 17457c478bd9Sstevel@tonic-gate #else /* DEBUG */ 17467c478bd9Sstevel@tonic-gate #define PCGS_TRIES 10 17477c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 17487c478bd9Sstevel@tonic-gate 17497c478bd9Sstevel@tonic-gate #ifdef VM_STATS 17507c478bd9Sstevel@tonic-gate uint_t pcgs_counts[PCGS_TRIES]; 17517c478bd9Sstevel@tonic-gate uint_t pcgs_too_many; 17527c478bd9Sstevel@tonic-gate uint_t pcgs_entered; 17537c478bd9Sstevel@tonic-gate uint_t pcgs_entered_noreloc; 17547c478bd9Sstevel@tonic-gate uint_t pcgs_locked; 17557c478bd9Sstevel@tonic-gate uint_t pcgs_cagelocked; 17567c478bd9Sstevel@tonic-gate #endif /* VM_STATS */ 17577c478bd9Sstevel@tonic-gate 17587c478bd9Sstevel@tonic-gate static page_t * 17597c478bd9Sstevel@tonic-gate page_create_get_something(vnode_t *vp, u_offset_t off, struct seg *seg, 17607c478bd9Sstevel@tonic-gate caddr_t vaddr, uint_t flags) 17617c478bd9Sstevel@tonic-gate { 17627c478bd9Sstevel@tonic-gate uint_t count; 17637c478bd9Sstevel@tonic-gate page_t *pp; 17647c478bd9Sstevel@tonic-gate uint_t locked, i; 17657c478bd9Sstevel@tonic-gate struct pcf *p; 17667c478bd9Sstevel@tonic-gate lgrp_t *lgrp; 17677c478bd9Sstevel@tonic-gate int cagelocked = 0; 17687c478bd9Sstevel@tonic-gate 17697c478bd9Sstevel@tonic-gate VM_STAT_ADD(pcgs_entered); 17707c478bd9Sstevel@tonic-gate 17717c478bd9Sstevel@tonic-gate /* 17727c478bd9Sstevel@tonic-gate * Tap any reserve freelists: if we fail now, we'll die 17737c478bd9Sstevel@tonic-gate * since the page(s) we're looking for have already been 17747c478bd9Sstevel@tonic-gate * accounted for. 17757c478bd9Sstevel@tonic-gate */ 17767c478bd9Sstevel@tonic-gate flags |= PG_PANIC; 17777c478bd9Sstevel@tonic-gate 17787c478bd9Sstevel@tonic-gate if ((flags & PG_NORELOC) != 0) { 17797c478bd9Sstevel@tonic-gate VM_STAT_ADD(pcgs_entered_noreloc); 17807c478bd9Sstevel@tonic-gate /* 17817c478bd9Sstevel@tonic-gate * Requests for free pages from critical threads 17827c478bd9Sstevel@tonic-gate * such as pageout still won't throttle here, but 17837c478bd9Sstevel@tonic-gate * we must try again, to give the cageout thread 17847c478bd9Sstevel@tonic-gate * another chance to catch up. Since we already 17857c478bd9Sstevel@tonic-gate * accounted for the pages, we had better get them 17867c478bd9Sstevel@tonic-gate * this time. 17877c478bd9Sstevel@tonic-gate * 17887c478bd9Sstevel@tonic-gate * N.B. All non-critical threads acquire the pcgs_cagelock 17897c478bd9Sstevel@tonic-gate * to serialize access to the freelists. This implements a 17907c478bd9Sstevel@tonic-gate * turnstile-type synchornization to avoid starvation of 17917c478bd9Sstevel@tonic-gate * critical requests for PG_NORELOC memory by non-critical 17927c478bd9Sstevel@tonic-gate * threads: all non-critical threads must acquire a 'ticket' 17937c478bd9Sstevel@tonic-gate * before passing through, which entails making sure 17947c478bd9Sstevel@tonic-gate * kcage_freemem won't fall below minfree prior to grabbing 17957c478bd9Sstevel@tonic-gate * pages from the freelists. 17967c478bd9Sstevel@tonic-gate */ 17977c478bd9Sstevel@tonic-gate if (kcage_create_throttle(1, flags) == KCT_NONCRIT) { 17987c478bd9Sstevel@tonic-gate mutex_enter(&pcgs_cagelock); 17997c478bd9Sstevel@tonic-gate cagelocked = 1; 18007c478bd9Sstevel@tonic-gate VM_STAT_ADD(pcgs_cagelocked); 18017c478bd9Sstevel@tonic-gate } 18027c478bd9Sstevel@tonic-gate } 18037c478bd9Sstevel@tonic-gate 18047c478bd9Sstevel@tonic-gate /* 18057c478bd9Sstevel@tonic-gate * Time to get serious. 18067c478bd9Sstevel@tonic-gate * We failed to get a `correctly colored' page from both the 18077c478bd9Sstevel@tonic-gate * free and cache lists. 18087c478bd9Sstevel@tonic-gate * We escalate in stage. 18097c478bd9Sstevel@tonic-gate * 18107c478bd9Sstevel@tonic-gate * First try both lists without worring about color. 18117c478bd9Sstevel@tonic-gate * 18127c478bd9Sstevel@tonic-gate * Then, grab all page accounting locks (ie. pcf[]) and 18137c478bd9Sstevel@tonic-gate * steal any pages that they have and set the pcf_block flag to 18147c478bd9Sstevel@tonic-gate * stop deletions from the lists. This will help because 18157c478bd9Sstevel@tonic-gate * a page can get added to the free list while we are looking 18167c478bd9Sstevel@tonic-gate * at the cache list, then another page could be added to the cache 18177c478bd9Sstevel@tonic-gate * list allowing the page on the free list to be removed as we 18187c478bd9Sstevel@tonic-gate * move from looking at the cache list to the free list. This 18197c478bd9Sstevel@tonic-gate * could happen over and over. We would never find the page 18207c478bd9Sstevel@tonic-gate * we have accounted for. 18217c478bd9Sstevel@tonic-gate * 18227c478bd9Sstevel@tonic-gate * Noreloc pages are a subset of the global (relocatable) page pool. 18237c478bd9Sstevel@tonic-gate * They are not tracked separately in the pcf bins, so it is 18247c478bd9Sstevel@tonic-gate * impossible to know when doing pcf accounting if the available 18257c478bd9Sstevel@tonic-gate * page(s) are noreloc pages or not. When looking for a noreloc page 18267c478bd9Sstevel@tonic-gate * it is quite easy to end up here even if the global (relocatable) 18277c478bd9Sstevel@tonic-gate * page pool has plenty of free pages but the noreloc pool is empty. 18287c478bd9Sstevel@tonic-gate * 18297c478bd9Sstevel@tonic-gate * When the noreloc pool is empty (or low), additional noreloc pages 18307c478bd9Sstevel@tonic-gate * are created by converting pages from the global page pool. This 18317c478bd9Sstevel@tonic-gate * process will stall during pcf accounting if the pcf bins are 18327c478bd9Sstevel@tonic-gate * already locked. Such is the case when a noreloc allocation is 18337c478bd9Sstevel@tonic-gate * looping here in page_create_get_something waiting for more noreloc 18347c478bd9Sstevel@tonic-gate * pages to appear. 18357c478bd9Sstevel@tonic-gate * 18367c478bd9Sstevel@tonic-gate * Short of adding a new field to the pcf bins to accurately track 18377c478bd9Sstevel@tonic-gate * the number of free noreloc pages, we instead do not grab the 18387c478bd9Sstevel@tonic-gate * pcgs_lock, do not set the pcf blocks and do not timeout when 18397c478bd9Sstevel@tonic-gate * allocating a noreloc page. This allows noreloc allocations to 18407c478bd9Sstevel@tonic-gate * loop without blocking global page pool allocations. 18417c478bd9Sstevel@tonic-gate * 18427c478bd9Sstevel@tonic-gate * NOTE: the behaviour of page_create_get_something has not changed 18437c478bd9Sstevel@tonic-gate * for the case of global page pool allocations. 18447c478bd9Sstevel@tonic-gate */ 18457c478bd9Sstevel@tonic-gate 18467c478bd9Sstevel@tonic-gate flags &= ~PG_MATCH_COLOR; 18477c478bd9Sstevel@tonic-gate locked = 0; 184807ad560dSkchow #if defined(__i386) || defined(__amd64) 1849843e1988Sjohnlev flags = page_create_update_flags_x86(flags); 18507c478bd9Sstevel@tonic-gate #endif 18517c478bd9Sstevel@tonic-gate 18527c478bd9Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, vaddr, PAGESIZE); 18537c478bd9Sstevel@tonic-gate 18547c478bd9Sstevel@tonic-gate for (count = 0; kcage_on || count < MAX_PCGS; count++) { 18557c478bd9Sstevel@tonic-gate pp = page_get_freelist(vp, off, seg, vaddr, PAGESIZE, 18567c478bd9Sstevel@tonic-gate flags, lgrp); 18577c478bd9Sstevel@tonic-gate if (pp == NULL) { 18587c478bd9Sstevel@tonic-gate pp = page_get_cachelist(vp, off, seg, vaddr, 18596e4dd838Smec flags, lgrp); 18607c478bd9Sstevel@tonic-gate } 18617c478bd9Sstevel@tonic-gate if (pp == NULL) { 18627c478bd9Sstevel@tonic-gate /* 18637c478bd9Sstevel@tonic-gate * Serialize. Don't fight with other pcgs(). 18647c478bd9Sstevel@tonic-gate */ 18657c478bd9Sstevel@tonic-gate if (!locked && (!kcage_on || !(flags & PG_NORELOC))) { 18667c478bd9Sstevel@tonic-gate mutex_enter(&pcgs_lock); 18677c478bd9Sstevel@tonic-gate VM_STAT_ADD(pcgs_locked); 18687c478bd9Sstevel@tonic-gate locked = 1; 18697c478bd9Sstevel@tonic-gate p = pcf; 187006fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 18717c478bd9Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 18727c478bd9Sstevel@tonic-gate ASSERT(p->pcf_block == 0); 18737c478bd9Sstevel@tonic-gate p->pcf_block = 1; 18747c478bd9Sstevel@tonic-gate p->pcf_reserve = p->pcf_count; 18757c478bd9Sstevel@tonic-gate p->pcf_count = 0; 18767c478bd9Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 18777c478bd9Sstevel@tonic-gate p++; 18787c478bd9Sstevel@tonic-gate } 18797c478bd9Sstevel@tonic-gate freemem = 0; 18807c478bd9Sstevel@tonic-gate } 18817c478bd9Sstevel@tonic-gate 18827c478bd9Sstevel@tonic-gate if (count) { 18837c478bd9Sstevel@tonic-gate /* 18847c478bd9Sstevel@tonic-gate * Since page_free() puts pages on 18857c478bd9Sstevel@tonic-gate * a list then accounts for it, we 18867c478bd9Sstevel@tonic-gate * just have to wait for page_free() 18877c478bd9Sstevel@tonic-gate * to unlock any page it was working 18887c478bd9Sstevel@tonic-gate * with. The page_lock()-page_reclaim() 18897c478bd9Sstevel@tonic-gate * path falls in the same boat. 18907c478bd9Sstevel@tonic-gate * 18917c478bd9Sstevel@tonic-gate * We don't need to check on the 18927c478bd9Sstevel@tonic-gate * PG_WAIT flag, we have already 18937c478bd9Sstevel@tonic-gate * accounted for the page we are 18947c478bd9Sstevel@tonic-gate * looking for in page_create_va(). 18957c478bd9Sstevel@tonic-gate * 18967c478bd9Sstevel@tonic-gate * We just wait a moment to let any 18977c478bd9Sstevel@tonic-gate * locked pages on the lists free up, 18987c478bd9Sstevel@tonic-gate * then continue around and try again. 18997c478bd9Sstevel@tonic-gate * 19007c478bd9Sstevel@tonic-gate * Will be awakened by set_freemem(). 19017c478bd9Sstevel@tonic-gate */ 19027c478bd9Sstevel@tonic-gate mutex_enter(&pcgs_wait_lock); 19037c478bd9Sstevel@tonic-gate cv_wait(&pcgs_cv, &pcgs_wait_lock); 19047c478bd9Sstevel@tonic-gate mutex_exit(&pcgs_wait_lock); 19057c478bd9Sstevel@tonic-gate } 19067c478bd9Sstevel@tonic-gate } else { 19077c478bd9Sstevel@tonic-gate #ifdef VM_STATS 19087c478bd9Sstevel@tonic-gate if (count >= PCGS_TRIES) { 19097c478bd9Sstevel@tonic-gate VM_STAT_ADD(pcgs_too_many); 19107c478bd9Sstevel@tonic-gate } else { 19117c478bd9Sstevel@tonic-gate VM_STAT_ADD(pcgs_counts[count]); 19127c478bd9Sstevel@tonic-gate } 19137c478bd9Sstevel@tonic-gate #endif 19147c478bd9Sstevel@tonic-gate if (locked) { 19157c478bd9Sstevel@tonic-gate pcgs_unblock(); 19167c478bd9Sstevel@tonic-gate mutex_exit(&pcgs_lock); 19177c478bd9Sstevel@tonic-gate } 19187c478bd9Sstevel@tonic-gate if (cagelocked) 19197c478bd9Sstevel@tonic-gate mutex_exit(&pcgs_cagelock); 19207c478bd9Sstevel@tonic-gate return (pp); 19217c478bd9Sstevel@tonic-gate } 19227c478bd9Sstevel@tonic-gate } 19237c478bd9Sstevel@tonic-gate /* 19247c478bd9Sstevel@tonic-gate * we go down holding the pcf locks. 19257c478bd9Sstevel@tonic-gate */ 19267c478bd9Sstevel@tonic-gate panic("no %spage found %d", 19277c478bd9Sstevel@tonic-gate ((flags & PG_NORELOC) ? "non-reloc " : ""), count); 19287c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 19297c478bd9Sstevel@tonic-gate } 19307c478bd9Sstevel@tonic-gate 19317c478bd9Sstevel@tonic-gate /* 19327c478bd9Sstevel@tonic-gate * Create enough pages for "bytes" worth of data starting at 19337c478bd9Sstevel@tonic-gate * "off" in "vp". 19347c478bd9Sstevel@tonic-gate * 19357c478bd9Sstevel@tonic-gate * Where flag must be one of: 19367c478bd9Sstevel@tonic-gate * 19377c478bd9Sstevel@tonic-gate * PG_EXCL: Exclusive create (fail if any page already 19387c478bd9Sstevel@tonic-gate * exists in the page cache) which does not 19397c478bd9Sstevel@tonic-gate * wait for memory to become available. 19407c478bd9Sstevel@tonic-gate * 19417c478bd9Sstevel@tonic-gate * PG_WAIT: Non-exclusive create which can wait for 19427c478bd9Sstevel@tonic-gate * memory to become available. 19437c478bd9Sstevel@tonic-gate * 19447c478bd9Sstevel@tonic-gate * PG_PHYSCONTIG: Allocate physically contiguous pages. 19457c478bd9Sstevel@tonic-gate * (Not Supported) 19467c478bd9Sstevel@tonic-gate * 19477c478bd9Sstevel@tonic-gate * A doubly linked list of pages is returned to the caller. Each page 19487c478bd9Sstevel@tonic-gate * on the list has the "exclusive" (p_selock) lock and "iolock" (p_iolock) 19497c478bd9Sstevel@tonic-gate * lock. 19507c478bd9Sstevel@tonic-gate * 19517c478bd9Sstevel@tonic-gate * Unable to change the parameters to page_create() in a minor release, 19527c478bd9Sstevel@tonic-gate * we renamed page_create() to page_create_va(), changed all known calls 19537c478bd9Sstevel@tonic-gate * from page_create() to page_create_va(), and created this wrapper. 19547c478bd9Sstevel@tonic-gate * 19557c478bd9Sstevel@tonic-gate * Upon a major release, we should break compatibility by deleting this 19567c478bd9Sstevel@tonic-gate * wrapper, and replacing all the strings "page_create_va", with "page_create". 19577c478bd9Sstevel@tonic-gate * 19587c478bd9Sstevel@tonic-gate * NOTE: There is a copy of this interface as page_create_io() in 19597c478bd9Sstevel@tonic-gate * i86/vm/vm_machdep.c. Any bugs fixed here should be applied 19607c478bd9Sstevel@tonic-gate * there. 19617c478bd9Sstevel@tonic-gate */ 19627c478bd9Sstevel@tonic-gate page_t * 19637c478bd9Sstevel@tonic-gate page_create(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags) 19647c478bd9Sstevel@tonic-gate { 19657c478bd9Sstevel@tonic-gate caddr_t random_vaddr; 19667c478bd9Sstevel@tonic-gate struct seg kseg; 19677c478bd9Sstevel@tonic-gate 19687c478bd9Sstevel@tonic-gate #ifdef DEBUG 19697c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "Using deprecated interface page_create: caller %p", 19707c478bd9Sstevel@tonic-gate (void *)caller()); 19717c478bd9Sstevel@tonic-gate #endif 19727c478bd9Sstevel@tonic-gate 19737c478bd9Sstevel@tonic-gate random_vaddr = (caddr_t)(((uintptr_t)vp >> 7) ^ 19747c478bd9Sstevel@tonic-gate (uintptr_t)(off >> PAGESHIFT)); 19757c478bd9Sstevel@tonic-gate kseg.s_as = &kas; 19767c478bd9Sstevel@tonic-gate 19777c478bd9Sstevel@tonic-gate return (page_create_va(vp, off, bytes, flags, &kseg, random_vaddr)); 19787c478bd9Sstevel@tonic-gate } 19797c478bd9Sstevel@tonic-gate 19807c478bd9Sstevel@tonic-gate #ifdef DEBUG 19817c478bd9Sstevel@tonic-gate uint32_t pg_alloc_pgs_mtbf = 0; 19827c478bd9Sstevel@tonic-gate #endif 19837c478bd9Sstevel@tonic-gate 19847c478bd9Sstevel@tonic-gate /* 19857c478bd9Sstevel@tonic-gate * Used for large page support. It will attempt to allocate 19867c478bd9Sstevel@tonic-gate * a large page(s) off the freelist. 19877c478bd9Sstevel@tonic-gate * 19887c478bd9Sstevel@tonic-gate * Returns non zero on failure. 19897c478bd9Sstevel@tonic-gate */ 19907c478bd9Sstevel@tonic-gate int 1991e44bd21cSsusans page_alloc_pages(struct vnode *vp, struct seg *seg, caddr_t addr, 19922cb27123Saguzovsk page_t **basepp, page_t *ppa[], uint_t szc, int anypgsz, int pgflags) 19937c478bd9Sstevel@tonic-gate { 19947c478bd9Sstevel@tonic-gate pgcnt_t npgs, curnpgs, totpgs; 19957c478bd9Sstevel@tonic-gate size_t pgsz; 19967c478bd9Sstevel@tonic-gate page_t *pplist = NULL, *pp; 19977c478bd9Sstevel@tonic-gate int err = 0; 19987c478bd9Sstevel@tonic-gate lgrp_t *lgrp; 19997c478bd9Sstevel@tonic-gate 20007c478bd9Sstevel@tonic-gate ASSERT(szc != 0 && szc <= (page_num_pagesizes() - 1)); 20012cb27123Saguzovsk ASSERT(pgflags == 0 || pgflags == PG_LOCAL); 20027c478bd9Sstevel@tonic-gate 20032be2af34Smec /* 20042be2af34Smec * Check if system heavily prefers local large pages over remote 20052be2af34Smec * on systems with multiple lgroups. 20062be2af34Smec */ 20072be2af34Smec if (lpg_alloc_prefer == LPAP_LOCAL && nlgrps > 1) { 20082be2af34Smec pgflags = PG_LOCAL; 20092be2af34Smec } 20102be2af34Smec 20117c478bd9Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[0]); 20127c478bd9Sstevel@tonic-gate 20137c478bd9Sstevel@tonic-gate #ifdef DEBUG 20147c478bd9Sstevel@tonic-gate if (pg_alloc_pgs_mtbf && !(gethrtime() % pg_alloc_pgs_mtbf)) { 20157c478bd9Sstevel@tonic-gate return (ENOMEM); 20167c478bd9Sstevel@tonic-gate } 20177c478bd9Sstevel@tonic-gate #endif 20187c478bd9Sstevel@tonic-gate 20197c478bd9Sstevel@tonic-gate /* 20207c478bd9Sstevel@tonic-gate * One must be NULL but not both. 20217c478bd9Sstevel@tonic-gate * And one must be non NULL but not both. 20227c478bd9Sstevel@tonic-gate */ 20237c478bd9Sstevel@tonic-gate ASSERT(basepp != NULL || ppa != NULL); 20247c478bd9Sstevel@tonic-gate ASSERT(basepp == NULL || ppa == NULL); 20257c478bd9Sstevel@tonic-gate 202678b03d3aSkchow #if defined(__i386) || defined(__amd64) 202778b03d3aSkchow while (page_chk_freelist(szc) == 0) { 202878b03d3aSkchow VM_STAT_ADD(alloc_pages[8]); 202978b03d3aSkchow if (anypgsz == 0 || --szc == 0) 203078b03d3aSkchow return (ENOMEM); 203178b03d3aSkchow } 203278b03d3aSkchow #endif 203378b03d3aSkchow 203478b03d3aSkchow pgsz = page_get_pagesize(szc); 203578b03d3aSkchow totpgs = curnpgs = npgs = pgsz >> PAGESHIFT; 203678b03d3aSkchow 203778b03d3aSkchow ASSERT(((uintptr_t)addr & (pgsz - 1)) == 0); 203878b03d3aSkchow 20397c478bd9Sstevel@tonic-gate (void) page_create_wait(npgs, PG_WAIT); 20407c478bd9Sstevel@tonic-gate 20417c478bd9Sstevel@tonic-gate while (npgs && szc) { 20427c478bd9Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, addr, pgsz); 20432cb27123Saguzovsk if (pgflags == PG_LOCAL) { 20442cb27123Saguzovsk pp = page_get_freelist(vp, 0, seg, addr, pgsz, 20452cb27123Saguzovsk pgflags, lgrp); 20462cb27123Saguzovsk if (pp == NULL) { 20472cb27123Saguzovsk pp = page_get_freelist(vp, 0, seg, addr, pgsz, 20482cb27123Saguzovsk 0, lgrp); 20492cb27123Saguzovsk } 20502cb27123Saguzovsk } else { 20512cb27123Saguzovsk pp = page_get_freelist(vp, 0, seg, addr, pgsz, 20522cb27123Saguzovsk 0, lgrp); 20532cb27123Saguzovsk } 20547c478bd9Sstevel@tonic-gate if (pp != NULL) { 20557c478bd9Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[1]); 20567c478bd9Sstevel@tonic-gate page_list_concat(&pplist, &pp); 20577c478bd9Sstevel@tonic-gate ASSERT(npgs >= curnpgs); 20587c478bd9Sstevel@tonic-gate npgs -= curnpgs; 20597c478bd9Sstevel@tonic-gate } else if (anypgsz) { 20607c478bd9Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[2]); 20617c478bd9Sstevel@tonic-gate szc--; 20627c478bd9Sstevel@tonic-gate pgsz = page_get_pagesize(szc); 20637c478bd9Sstevel@tonic-gate curnpgs = pgsz >> PAGESHIFT; 20647c478bd9Sstevel@tonic-gate } else { 20657c478bd9Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[3]); 20667c478bd9Sstevel@tonic-gate ASSERT(npgs == totpgs); 20677c478bd9Sstevel@tonic-gate page_create_putback(npgs); 20687c478bd9Sstevel@tonic-gate return (ENOMEM); 20697c478bd9Sstevel@tonic-gate } 20707c478bd9Sstevel@tonic-gate } 20717c478bd9Sstevel@tonic-gate if (szc == 0) { 20727c478bd9Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[4]); 20737c478bd9Sstevel@tonic-gate ASSERT(npgs != 0); 20747c478bd9Sstevel@tonic-gate page_create_putback(npgs); 20757c478bd9Sstevel@tonic-gate err = ENOMEM; 20767c478bd9Sstevel@tonic-gate } else if (basepp != NULL) { 20777c478bd9Sstevel@tonic-gate ASSERT(npgs == 0); 20787c478bd9Sstevel@tonic-gate ASSERT(ppa == NULL); 20797c478bd9Sstevel@tonic-gate *basepp = pplist; 20807c478bd9Sstevel@tonic-gate } 20817c478bd9Sstevel@tonic-gate 20827c478bd9Sstevel@tonic-gate npgs = totpgs - npgs; 20837c478bd9Sstevel@tonic-gate pp = pplist; 20847c478bd9Sstevel@tonic-gate 20857c478bd9Sstevel@tonic-gate /* 20867c478bd9Sstevel@tonic-gate * Clear the free and age bits. Also if we were passed in a ppa then 20877c478bd9Sstevel@tonic-gate * fill it in with all the constituent pages from the large page. But 20887c478bd9Sstevel@tonic-gate * if we failed to allocate all the pages just free what we got. 20897c478bd9Sstevel@tonic-gate */ 20907c478bd9Sstevel@tonic-gate while (npgs != 0) { 20917c478bd9Sstevel@tonic-gate ASSERT(PP_ISFREE(pp)); 20927c478bd9Sstevel@tonic-gate ASSERT(PP_ISAGED(pp)); 20937c478bd9Sstevel@tonic-gate if (ppa != NULL || err != 0) { 20947c478bd9Sstevel@tonic-gate if (err == 0) { 20957c478bd9Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[5]); 20967c478bd9Sstevel@tonic-gate PP_CLRFREE(pp); 20977c478bd9Sstevel@tonic-gate PP_CLRAGED(pp); 20987c478bd9Sstevel@tonic-gate page_sub(&pplist, pp); 20997c478bd9Sstevel@tonic-gate *ppa++ = pp; 21007c478bd9Sstevel@tonic-gate npgs--; 21017c478bd9Sstevel@tonic-gate } else { 21027c478bd9Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[6]); 21037c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc != 0); 21047c478bd9Sstevel@tonic-gate curnpgs = page_get_pagecnt(pp->p_szc); 21057c478bd9Sstevel@tonic-gate page_list_break(&pp, &pplist, curnpgs); 21067c478bd9Sstevel@tonic-gate page_list_add_pages(pp, 0); 21077c478bd9Sstevel@tonic-gate page_create_putback(curnpgs); 21087c478bd9Sstevel@tonic-gate ASSERT(npgs >= curnpgs); 21097c478bd9Sstevel@tonic-gate npgs -= curnpgs; 21107c478bd9Sstevel@tonic-gate } 21117c478bd9Sstevel@tonic-gate pp = pplist; 21127c478bd9Sstevel@tonic-gate } else { 21137c478bd9Sstevel@tonic-gate VM_STAT_ADD(alloc_pages[7]); 21147c478bd9Sstevel@tonic-gate PP_CLRFREE(pp); 21157c478bd9Sstevel@tonic-gate PP_CLRAGED(pp); 21167c478bd9Sstevel@tonic-gate pp = pp->p_next; 21177c478bd9Sstevel@tonic-gate npgs--; 21187c478bd9Sstevel@tonic-gate } 21197c478bd9Sstevel@tonic-gate } 21207c478bd9Sstevel@tonic-gate return (err); 21217c478bd9Sstevel@tonic-gate } 21227c478bd9Sstevel@tonic-gate 21237c478bd9Sstevel@tonic-gate /* 21247c478bd9Sstevel@tonic-gate * Get a single large page off of the freelists, and set it up for use. 21257c478bd9Sstevel@tonic-gate * Number of bytes requested must be a supported page size. 21267c478bd9Sstevel@tonic-gate * 21277c478bd9Sstevel@tonic-gate * Note that this call may fail even if there is sufficient 21287c478bd9Sstevel@tonic-gate * memory available or PG_WAIT is set, so the caller must 21297c478bd9Sstevel@tonic-gate * be willing to fallback on page_create_va(), block and retry, 21307c478bd9Sstevel@tonic-gate * or fail the requester. 21317c478bd9Sstevel@tonic-gate */ 21327c478bd9Sstevel@tonic-gate page_t * 21337c478bd9Sstevel@tonic-gate page_create_va_large(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags, 21347c478bd9Sstevel@tonic-gate struct seg *seg, caddr_t vaddr, void *arg) 21357c478bd9Sstevel@tonic-gate { 213606fb6a36Sdv pgcnt_t npages; 21377c478bd9Sstevel@tonic-gate page_t *pp; 21387c478bd9Sstevel@tonic-gate page_t *rootpp; 21397c478bd9Sstevel@tonic-gate lgrp_t *lgrp; 21407c478bd9Sstevel@tonic-gate lgrp_id_t *lgrpid = (lgrp_id_t *)arg; 21417c478bd9Sstevel@tonic-gate 21427c478bd9Sstevel@tonic-gate ASSERT(vp != NULL); 21437c478bd9Sstevel@tonic-gate 21447c478bd9Sstevel@tonic-gate ASSERT((flags & ~(PG_EXCL | PG_WAIT | 21456e4dd838Smec PG_NORELOC | PG_PANIC | PG_PUSHPAGE)) == 0); 21467c478bd9Sstevel@tonic-gate /* but no others */ 21477c478bd9Sstevel@tonic-gate 21487c478bd9Sstevel@tonic-gate ASSERT((flags & PG_EXCL) == PG_EXCL); 21497c478bd9Sstevel@tonic-gate 21507c478bd9Sstevel@tonic-gate npages = btop(bytes); 21517c478bd9Sstevel@tonic-gate 21527c478bd9Sstevel@tonic-gate if (!kcage_on || panicstr) { 21537c478bd9Sstevel@tonic-gate /* 21547c478bd9Sstevel@tonic-gate * Cage is OFF, or we are single threaded in 21557c478bd9Sstevel@tonic-gate * panic, so make everything a RELOC request. 21567c478bd9Sstevel@tonic-gate */ 21577c478bd9Sstevel@tonic-gate flags &= ~PG_NORELOC; 21587c478bd9Sstevel@tonic-gate } 21597c478bd9Sstevel@tonic-gate 21607c478bd9Sstevel@tonic-gate /* 21617c478bd9Sstevel@tonic-gate * Make sure there's adequate physical memory available. 21627c478bd9Sstevel@tonic-gate * Note: PG_WAIT is ignored here. 21637c478bd9Sstevel@tonic-gate */ 21647c478bd9Sstevel@tonic-gate if (freemem <= throttlefree + npages) { 21657c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[1]); 21667c478bd9Sstevel@tonic-gate return (NULL); 21677c478bd9Sstevel@tonic-gate } 21687c478bd9Sstevel@tonic-gate 21697c478bd9Sstevel@tonic-gate /* 21707c478bd9Sstevel@tonic-gate * If cage is on, dampen draw from cage when available 21717c478bd9Sstevel@tonic-gate * cage space is low. 21727c478bd9Sstevel@tonic-gate */ 21737c478bd9Sstevel@tonic-gate if ((flags & (PG_NORELOC | PG_WAIT)) == (PG_NORELOC | PG_WAIT) && 21747c478bd9Sstevel@tonic-gate kcage_freemem < kcage_throttlefree + npages) { 21757c478bd9Sstevel@tonic-gate 21767c478bd9Sstevel@tonic-gate /* 21777c478bd9Sstevel@tonic-gate * The cage is on, the caller wants PG_NORELOC 21787c478bd9Sstevel@tonic-gate * pages and available cage memory is very low. 21797c478bd9Sstevel@tonic-gate * Call kcage_create_throttle() to attempt to 21807c478bd9Sstevel@tonic-gate * control demand on the cage. 21817c478bd9Sstevel@tonic-gate */ 21827c478bd9Sstevel@tonic-gate if (kcage_create_throttle(npages, flags) == KCT_FAILURE) { 21837c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[2]); 21847c478bd9Sstevel@tonic-gate return (NULL); 21857c478bd9Sstevel@tonic-gate } 21867c478bd9Sstevel@tonic-gate } 21877c478bd9Sstevel@tonic-gate 218806fb6a36Sdv if (!pcf_decrement_bucket(npages) && 218906fb6a36Sdv !pcf_decrement_multiple(NULL, npages, 1)) { 219006fb6a36Sdv VM_STAT_ADD(page_create_large_cnt[4]); 219106fb6a36Sdv return (NULL); 21927c478bd9Sstevel@tonic-gate } 21937c478bd9Sstevel@tonic-gate 21947c478bd9Sstevel@tonic-gate /* 21957c478bd9Sstevel@tonic-gate * This is where this function behaves fundamentally differently 21967c478bd9Sstevel@tonic-gate * than page_create_va(); since we're intending to map the page 21977c478bd9Sstevel@tonic-gate * with a single TTE, we have to get it as a physically contiguous 21987c478bd9Sstevel@tonic-gate * hardware pagesize chunk. If we can't, we fail. 21997c478bd9Sstevel@tonic-gate */ 22007c478bd9Sstevel@tonic-gate if (lgrpid != NULL && *lgrpid >= 0 && *lgrpid <= lgrp_alloc_max && 22016e4dd838Smec LGRP_EXISTS(lgrp_table[*lgrpid])) 22027c478bd9Sstevel@tonic-gate lgrp = lgrp_table[*lgrpid]; 22037c478bd9Sstevel@tonic-gate else 22047c478bd9Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, vaddr, bytes); 22057c478bd9Sstevel@tonic-gate 22067c478bd9Sstevel@tonic-gate if ((rootpp = page_get_freelist(&kvp, off, seg, vaddr, 22077c478bd9Sstevel@tonic-gate bytes, flags & ~PG_MATCH_COLOR, lgrp)) == NULL) { 22087c478bd9Sstevel@tonic-gate page_create_putback(npages); 22097c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[5]); 22107c478bd9Sstevel@tonic-gate return (NULL); 22117c478bd9Sstevel@tonic-gate } 22127c478bd9Sstevel@tonic-gate 22137c478bd9Sstevel@tonic-gate /* 22147c478bd9Sstevel@tonic-gate * if we got the page with the wrong mtype give it back this is a 22157c478bd9Sstevel@tonic-gate * workaround for CR 6249718. When CR 6249718 is fixed we never get 22167c478bd9Sstevel@tonic-gate * inside "if" and the workaround becomes just a nop 22177c478bd9Sstevel@tonic-gate */ 22187c478bd9Sstevel@tonic-gate if (kcage_on && (flags & PG_NORELOC) && !PP_ISNORELOC(rootpp)) { 22197c478bd9Sstevel@tonic-gate page_list_add_pages(rootpp, 0); 22207c478bd9Sstevel@tonic-gate page_create_putback(npages); 22217c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[6]); 22227c478bd9Sstevel@tonic-gate return (NULL); 22237c478bd9Sstevel@tonic-gate } 22247c478bd9Sstevel@tonic-gate 22257c478bd9Sstevel@tonic-gate /* 22267c478bd9Sstevel@tonic-gate * If satisfying this request has left us with too little 22277c478bd9Sstevel@tonic-gate * memory, start the wheels turning to get some back. The 22287c478bd9Sstevel@tonic-gate * first clause of the test prevents waking up the pageout 22297c478bd9Sstevel@tonic-gate * daemon in situations where it would decide that there's 22307c478bd9Sstevel@tonic-gate * nothing to do. 22317c478bd9Sstevel@tonic-gate */ 22327c478bd9Sstevel@tonic-gate if (nscan < desscan && freemem < minfree) { 22337c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL, 22347c478bd9Sstevel@tonic-gate "pageout_cv_signal:freemem %ld", freemem); 22357c478bd9Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 22367c478bd9Sstevel@tonic-gate } 22377c478bd9Sstevel@tonic-gate 22387c478bd9Sstevel@tonic-gate pp = rootpp; 22397c478bd9Sstevel@tonic-gate while (npages--) { 22407c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 22417c478bd9Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL); 22427c478bd9Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 22437c478bd9Sstevel@tonic-gate PP_CLRFREE(pp); 22447c478bd9Sstevel@tonic-gate PP_CLRAGED(pp); 22457c478bd9Sstevel@tonic-gate if (!page_hashin(pp, vp, off, NULL)) 22467c478bd9Sstevel@tonic-gate panic("page_create_large: hashin failed: page %p", 22477c478bd9Sstevel@tonic-gate (void *)pp); 22487c478bd9Sstevel@tonic-gate page_io_lock(pp); 22497c478bd9Sstevel@tonic-gate off += PAGESIZE; 22507c478bd9Sstevel@tonic-gate pp = pp->p_next; 22517c478bd9Sstevel@tonic-gate } 22527c478bd9Sstevel@tonic-gate 22537c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_large_cnt[0]); 22547c478bd9Sstevel@tonic-gate return (rootpp); 22557c478bd9Sstevel@tonic-gate } 22567c478bd9Sstevel@tonic-gate 22577c478bd9Sstevel@tonic-gate page_t * 22587c478bd9Sstevel@tonic-gate page_create_va(vnode_t *vp, u_offset_t off, size_t bytes, uint_t flags, 22597c478bd9Sstevel@tonic-gate struct seg *seg, caddr_t vaddr) 22607c478bd9Sstevel@tonic-gate { 22617c478bd9Sstevel@tonic-gate page_t *plist = NULL; 22627c478bd9Sstevel@tonic-gate pgcnt_t npages; 22637c478bd9Sstevel@tonic-gate pgcnt_t found_on_free = 0; 22647c478bd9Sstevel@tonic-gate pgcnt_t pages_req; 22657c478bd9Sstevel@tonic-gate page_t *npp = NULL; 22667c478bd9Sstevel@tonic-gate struct pcf *p; 22677c478bd9Sstevel@tonic-gate lgrp_t *lgrp; 22687c478bd9Sstevel@tonic-gate 22697c478bd9Sstevel@tonic-gate TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_START, 22706e4dd838Smec "page_create_start:vp %p off %llx bytes %lu flags %x", 22716e4dd838Smec vp, off, bytes, flags); 22727c478bd9Sstevel@tonic-gate 22737c478bd9Sstevel@tonic-gate ASSERT(bytes != 0 && vp != NULL); 22747c478bd9Sstevel@tonic-gate 22757c478bd9Sstevel@tonic-gate if ((flags & PG_EXCL) == 0 && (flags & PG_WAIT) == 0) { 22767c478bd9Sstevel@tonic-gate panic("page_create: invalid flags"); 22777c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 22787c478bd9Sstevel@tonic-gate } 22797c478bd9Sstevel@tonic-gate ASSERT((flags & ~(PG_EXCL | PG_WAIT | 22807c478bd9Sstevel@tonic-gate PG_NORELOC | PG_PANIC | PG_PUSHPAGE)) == 0); 22817c478bd9Sstevel@tonic-gate /* but no others */ 22827c478bd9Sstevel@tonic-gate 22837c478bd9Sstevel@tonic-gate pages_req = npages = btopr(bytes); 22847c478bd9Sstevel@tonic-gate /* 22857c478bd9Sstevel@tonic-gate * Try to see whether request is too large to *ever* be 22867c478bd9Sstevel@tonic-gate * satisfied, in order to prevent deadlock. We arbitrarily 22877c478bd9Sstevel@tonic-gate * decide to limit maximum size requests to max_page_get. 22887c478bd9Sstevel@tonic-gate */ 22897c478bd9Sstevel@tonic-gate if (npages >= max_page_get) { 22907c478bd9Sstevel@tonic-gate if ((flags & PG_WAIT) == 0) { 22917c478bd9Sstevel@tonic-gate TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_TOOBIG, 22927c478bd9Sstevel@tonic-gate "page_create_toobig:vp %p off %llx npages " 22937c478bd9Sstevel@tonic-gate "%lu max_page_get %lu", 22947c478bd9Sstevel@tonic-gate vp, off, npages, max_page_get); 22957c478bd9Sstevel@tonic-gate return (NULL); 22967c478bd9Sstevel@tonic-gate } else { 22977c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 22987c478bd9Sstevel@tonic-gate "Request for too much kernel memory " 22997c478bd9Sstevel@tonic-gate "(%lu bytes), will hang forever", bytes); 23007c478bd9Sstevel@tonic-gate for (;;) 23017c478bd9Sstevel@tonic-gate delay(1000000000); 23027c478bd9Sstevel@tonic-gate } 23037c478bd9Sstevel@tonic-gate } 23047c478bd9Sstevel@tonic-gate 23057c478bd9Sstevel@tonic-gate if (!kcage_on || panicstr) { 23067c478bd9Sstevel@tonic-gate /* 23077c478bd9Sstevel@tonic-gate * Cage is OFF, or we are single threaded in 23087c478bd9Sstevel@tonic-gate * panic, so make everything a RELOC request. 23097c478bd9Sstevel@tonic-gate */ 23107c478bd9Sstevel@tonic-gate flags &= ~PG_NORELOC; 23117c478bd9Sstevel@tonic-gate } 23127c478bd9Sstevel@tonic-gate 23137c478bd9Sstevel@tonic-gate if (freemem <= throttlefree + npages) 23147c478bd9Sstevel@tonic-gate if (!page_create_throttle(npages, flags)) 23157c478bd9Sstevel@tonic-gate return (NULL); 23167c478bd9Sstevel@tonic-gate 23177c478bd9Sstevel@tonic-gate /* 23187c478bd9Sstevel@tonic-gate * If cage is on, dampen draw from cage when available 23197c478bd9Sstevel@tonic-gate * cage space is low. 23207c478bd9Sstevel@tonic-gate */ 23217c478bd9Sstevel@tonic-gate if ((flags & PG_NORELOC) && 23226e4dd838Smec kcage_freemem < kcage_throttlefree + npages) { 23237c478bd9Sstevel@tonic-gate 23247c478bd9Sstevel@tonic-gate /* 23257c478bd9Sstevel@tonic-gate * The cage is on, the caller wants PG_NORELOC 23267c478bd9Sstevel@tonic-gate * pages and available cage memory is very low. 23277c478bd9Sstevel@tonic-gate * Call kcage_create_throttle() to attempt to 23287c478bd9Sstevel@tonic-gate * control demand on the cage. 23297c478bd9Sstevel@tonic-gate */ 23307c478bd9Sstevel@tonic-gate if (kcage_create_throttle(npages, flags) == KCT_FAILURE) 23317c478bd9Sstevel@tonic-gate return (NULL); 23327c478bd9Sstevel@tonic-gate } 23337c478bd9Sstevel@tonic-gate 23347c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[0]); 23357c478bd9Sstevel@tonic-gate 233606fb6a36Sdv if (!pcf_decrement_bucket(npages)) { 23377c478bd9Sstevel@tonic-gate /* 23387c478bd9Sstevel@tonic-gate * Have to look harder. If npages is greater than 2339da6c28aaSamw * one, then we might have to coalesce the counters. 23407c478bd9Sstevel@tonic-gate * 23417c478bd9Sstevel@tonic-gate * Go wait. We come back having accounted 23427c478bd9Sstevel@tonic-gate * for the memory. 23437c478bd9Sstevel@tonic-gate */ 23447c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[1]); 23457c478bd9Sstevel@tonic-gate if (!page_create_wait(npages, flags)) { 23467c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[2]); 23477c478bd9Sstevel@tonic-gate return (NULL); 23487c478bd9Sstevel@tonic-gate } 23497c478bd9Sstevel@tonic-gate } 23507c478bd9Sstevel@tonic-gate 23517c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SUCCESS, 23526e4dd838Smec "page_create_success:vp %p off %llx", vp, off); 23537c478bd9Sstevel@tonic-gate 23547c478bd9Sstevel@tonic-gate /* 23557c478bd9Sstevel@tonic-gate * If satisfying this request has left us with too little 23567c478bd9Sstevel@tonic-gate * memory, start the wheels turning to get some back. The 23577c478bd9Sstevel@tonic-gate * first clause of the test prevents waking up the pageout 23587c478bd9Sstevel@tonic-gate * daemon in situations where it would decide that there's 23597c478bd9Sstevel@tonic-gate * nothing to do. 23607c478bd9Sstevel@tonic-gate */ 23617c478bd9Sstevel@tonic-gate if (nscan < desscan && freemem < minfree) { 23627c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL, 23636e4dd838Smec "pageout_cv_signal:freemem %ld", freemem); 23647c478bd9Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 23657c478bd9Sstevel@tonic-gate } 23667c478bd9Sstevel@tonic-gate 23677c478bd9Sstevel@tonic-gate /* 23687c478bd9Sstevel@tonic-gate * Loop around collecting the requested number of pages. 23697c478bd9Sstevel@tonic-gate * Most of the time, we have to `create' a new page. With 23707c478bd9Sstevel@tonic-gate * this in mind, pull the page off the free list before 23717c478bd9Sstevel@tonic-gate * getting the hash lock. This will minimize the hash 23727c478bd9Sstevel@tonic-gate * lock hold time, nesting, and the like. If it turns 23737c478bd9Sstevel@tonic-gate * out we don't need the page, we put it back at the end. 23747c478bd9Sstevel@tonic-gate */ 23757c478bd9Sstevel@tonic-gate while (npages--) { 23767c478bd9Sstevel@tonic-gate page_t *pp; 23777c478bd9Sstevel@tonic-gate kmutex_t *phm = NULL; 23787c478bd9Sstevel@tonic-gate ulong_t index; 23797c478bd9Sstevel@tonic-gate 23807c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 23817c478bd9Sstevel@tonic-gate top: 23827c478bd9Sstevel@tonic-gate ASSERT(phm == NULL); 23837c478bd9Sstevel@tonic-gate ASSERT(index == PAGE_HASH_FUNC(vp, off)); 23847c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 23857c478bd9Sstevel@tonic-gate 23867c478bd9Sstevel@tonic-gate if (npp == NULL) { 23877c478bd9Sstevel@tonic-gate /* 23887c478bd9Sstevel@tonic-gate * Try to get a page from the freelist (ie, 23897c478bd9Sstevel@tonic-gate * a page with no [vp, off] tag). If that 23907c478bd9Sstevel@tonic-gate * fails, use the cachelist. 23917c478bd9Sstevel@tonic-gate * 23927c478bd9Sstevel@tonic-gate * During the first attempt at both the free 23937c478bd9Sstevel@tonic-gate * and cache lists we try for the correct color. 23947c478bd9Sstevel@tonic-gate */ 23957c478bd9Sstevel@tonic-gate /* 23967c478bd9Sstevel@tonic-gate * XXXX-how do we deal with virtual indexed 23977c478bd9Sstevel@tonic-gate * caches and and colors? 23987c478bd9Sstevel@tonic-gate */ 23997c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_cnt[4]); 24007c478bd9Sstevel@tonic-gate /* 24017c478bd9Sstevel@tonic-gate * Get lgroup to allocate next page of shared memory 24027c478bd9Sstevel@tonic-gate * from and use it to specify where to allocate 24037c478bd9Sstevel@tonic-gate * the physical memory 24047c478bd9Sstevel@tonic-gate */ 24057c478bd9Sstevel@tonic-gate lgrp = lgrp_mem_choose(seg, vaddr, PAGESIZE); 24067c478bd9Sstevel@tonic-gate npp = page_get_freelist(vp, off, seg, vaddr, PAGESIZE, 24077c478bd9Sstevel@tonic-gate flags | PG_MATCH_COLOR, lgrp); 24087c478bd9Sstevel@tonic-gate if (npp == NULL) { 24097c478bd9Sstevel@tonic-gate npp = page_get_cachelist(vp, off, seg, 24107c478bd9Sstevel@tonic-gate vaddr, flags | PG_MATCH_COLOR, lgrp); 24117c478bd9Sstevel@tonic-gate if (npp == NULL) { 24127c478bd9Sstevel@tonic-gate npp = page_create_get_something(vp, 24137c478bd9Sstevel@tonic-gate off, seg, vaddr, 24147c478bd9Sstevel@tonic-gate flags & ~PG_MATCH_COLOR); 24157c478bd9Sstevel@tonic-gate } 24167c478bd9Sstevel@tonic-gate 24177c478bd9Sstevel@tonic-gate if (PP_ISAGED(npp) == 0) { 24187c478bd9Sstevel@tonic-gate /* 24197c478bd9Sstevel@tonic-gate * Since this page came from the 24207c478bd9Sstevel@tonic-gate * cachelist, we must destroy the 24217c478bd9Sstevel@tonic-gate * old vnode association. 24227c478bd9Sstevel@tonic-gate */ 24237c478bd9Sstevel@tonic-gate page_hashout(npp, NULL); 24247c478bd9Sstevel@tonic-gate } 24257c478bd9Sstevel@tonic-gate } 24267c478bd9Sstevel@tonic-gate } 24277c478bd9Sstevel@tonic-gate 24287c478bd9Sstevel@tonic-gate /* 24297c478bd9Sstevel@tonic-gate * We own this page! 24307c478bd9Sstevel@tonic-gate */ 24317c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(npp)); 24327c478bd9Sstevel@tonic-gate ASSERT(npp->p_vnode == NULL); 24337c478bd9Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(npp)); 24347c478bd9Sstevel@tonic-gate PP_CLRFREE(npp); 24357c478bd9Sstevel@tonic-gate PP_CLRAGED(npp); 24367c478bd9Sstevel@tonic-gate 24377c478bd9Sstevel@tonic-gate /* 24387c478bd9Sstevel@tonic-gate * Here we have a page in our hot little mits and are 24397c478bd9Sstevel@tonic-gate * just waiting to stuff it on the appropriate lists. 24407c478bd9Sstevel@tonic-gate * Get the mutex and check to see if it really does 24417c478bd9Sstevel@tonic-gate * not exist. 24427c478bd9Sstevel@tonic-gate */ 24437c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 24447c478bd9Sstevel@tonic-gate mutex_enter(phm); 24457c478bd9Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 24467c478bd9Sstevel@tonic-gate if (pp == NULL) { 24477c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_new); 24487c478bd9Sstevel@tonic-gate pp = npp; 24497c478bd9Sstevel@tonic-gate npp = NULL; 24507c478bd9Sstevel@tonic-gate if (!page_hashin(pp, vp, off, phm)) { 24517c478bd9Sstevel@tonic-gate /* 24527c478bd9Sstevel@tonic-gate * Since we hold the page hash mutex and 24537c478bd9Sstevel@tonic-gate * just searched for this page, page_hashin 24547c478bd9Sstevel@tonic-gate * had better not fail. If it does, that 24557c478bd9Sstevel@tonic-gate * means somethread did not follow the 24567c478bd9Sstevel@tonic-gate * page hash mutex rules. Panic now and 24577c478bd9Sstevel@tonic-gate * get it over with. As usual, go down 24587c478bd9Sstevel@tonic-gate * holding all the locks. 24597c478bd9Sstevel@tonic-gate */ 24607c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 24617c478bd9Sstevel@tonic-gate panic("page_create: " 24627c478bd9Sstevel@tonic-gate "hashin failed %p %p %llx %p", 24637c478bd9Sstevel@tonic-gate (void *)pp, (void *)vp, off, (void *)phm); 24647c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 24657c478bd9Sstevel@tonic-gate } 24667c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 24677c478bd9Sstevel@tonic-gate mutex_exit(phm); 24687c478bd9Sstevel@tonic-gate phm = NULL; 24697c478bd9Sstevel@tonic-gate 24707c478bd9Sstevel@tonic-gate /* 24717c478bd9Sstevel@tonic-gate * Hat layer locking need not be done to set 24727c478bd9Sstevel@tonic-gate * the following bits since the page is not hashed 24737c478bd9Sstevel@tonic-gate * and was on the free list (i.e., had no mappings). 24747c478bd9Sstevel@tonic-gate * 24757c478bd9Sstevel@tonic-gate * Set the reference bit to protect 24767c478bd9Sstevel@tonic-gate * against immediate pageout 24777c478bd9Sstevel@tonic-gate * 24787c478bd9Sstevel@tonic-gate * XXXmh modify freelist code to set reference 24797c478bd9Sstevel@tonic-gate * bit so we don't have to do it here. 24807c478bd9Sstevel@tonic-gate */ 24817c478bd9Sstevel@tonic-gate page_set_props(pp, P_REF); 24827c478bd9Sstevel@tonic-gate found_on_free++; 24837c478bd9Sstevel@tonic-gate } else { 24847c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_exists); 24857c478bd9Sstevel@tonic-gate if (flags & PG_EXCL) { 24867c478bd9Sstevel@tonic-gate /* 24877c478bd9Sstevel@tonic-gate * Found an existing page, and the caller 24887c478bd9Sstevel@tonic-gate * wanted all new pages. Undo all of the work 24897c478bd9Sstevel@tonic-gate * we have done. 24907c478bd9Sstevel@tonic-gate */ 24917c478bd9Sstevel@tonic-gate mutex_exit(phm); 24927c478bd9Sstevel@tonic-gate phm = NULL; 24937c478bd9Sstevel@tonic-gate while (plist != NULL) { 24947c478bd9Sstevel@tonic-gate pp = plist; 24957c478bd9Sstevel@tonic-gate page_sub(&plist, pp); 24967c478bd9Sstevel@tonic-gate page_io_unlock(pp); 24977c478bd9Sstevel@tonic-gate /* large pages should not end up here */ 24987c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 24997c478bd9Sstevel@tonic-gate /*LINTED: constant in conditional ctx*/ 25007c478bd9Sstevel@tonic-gate VN_DISPOSE(pp, B_INVAL, 0, kcred); 25017c478bd9Sstevel@tonic-gate } 25027c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_found_one); 25037c478bd9Sstevel@tonic-gate goto fail; 25047c478bd9Sstevel@tonic-gate } 25057c478bd9Sstevel@tonic-gate ASSERT(flags & PG_WAIT); 25067c478bd9Sstevel@tonic-gate if (!page_lock(pp, SE_EXCL, phm, P_NO_RECLAIM)) { 25077c478bd9Sstevel@tonic-gate /* 25087c478bd9Sstevel@tonic-gate * Start all over again if we blocked trying 25097c478bd9Sstevel@tonic-gate * to lock the page. 25107c478bd9Sstevel@tonic-gate */ 25117c478bd9Sstevel@tonic-gate mutex_exit(phm); 25127c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_page_lock_failed); 25137c478bd9Sstevel@tonic-gate phm = NULL; 25147c478bd9Sstevel@tonic-gate goto top; 25157c478bd9Sstevel@tonic-gate } 25167c478bd9Sstevel@tonic-gate mutex_exit(phm); 25177c478bd9Sstevel@tonic-gate phm = NULL; 25187c478bd9Sstevel@tonic-gate 25197c478bd9Sstevel@tonic-gate if (PP_ISFREE(pp)) { 25207c478bd9Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0); 25217c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_get_cache); 25227c478bd9Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST); 25237c478bd9Sstevel@tonic-gate PP_CLRFREE(pp); 25247c478bd9Sstevel@tonic-gate found_on_free++; 25257c478bd9Sstevel@tonic-gate } 25267c478bd9Sstevel@tonic-gate } 25277c478bd9Sstevel@tonic-gate 25287c478bd9Sstevel@tonic-gate /* 25297c478bd9Sstevel@tonic-gate * Got a page! It is locked. Acquire the i/o 25307c478bd9Sstevel@tonic-gate * lock since we are going to use the p_next and 25317c478bd9Sstevel@tonic-gate * p_prev fields to link the requested pages together. 25327c478bd9Sstevel@tonic-gate */ 25337c478bd9Sstevel@tonic-gate page_io_lock(pp); 25347c478bd9Sstevel@tonic-gate page_add(&plist, pp); 25357c478bd9Sstevel@tonic-gate plist = plist->p_next; 25367c478bd9Sstevel@tonic-gate off += PAGESIZE; 25377c478bd9Sstevel@tonic-gate vaddr += PAGESIZE; 25387c478bd9Sstevel@tonic-gate } 25397c478bd9Sstevel@tonic-gate 25407c478bd9Sstevel@tonic-gate ASSERT((flags & PG_EXCL) ? (found_on_free == pages_req) : 1); 25417c478bd9Sstevel@tonic-gate fail: 25427c478bd9Sstevel@tonic-gate if (npp != NULL) { 25437c478bd9Sstevel@tonic-gate /* 25447c478bd9Sstevel@tonic-gate * Did not need this page after all. 25457c478bd9Sstevel@tonic-gate * Put it back on the free list. 25467c478bd9Sstevel@tonic-gate */ 25477c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_putbacks); 25487c478bd9Sstevel@tonic-gate PP_SETFREE(npp); 25497c478bd9Sstevel@tonic-gate PP_SETAGED(npp); 25507c478bd9Sstevel@tonic-gate npp->p_offset = (u_offset_t)-1; 25517c478bd9Sstevel@tonic-gate page_list_add(npp, PG_FREE_LIST | PG_LIST_TAIL); 25527c478bd9Sstevel@tonic-gate page_unlock(npp); 25537c478bd9Sstevel@tonic-gate 25547c478bd9Sstevel@tonic-gate } 25557c478bd9Sstevel@tonic-gate 25567c478bd9Sstevel@tonic-gate ASSERT(pages_req >= found_on_free); 25577c478bd9Sstevel@tonic-gate 25587c478bd9Sstevel@tonic-gate { 25597c478bd9Sstevel@tonic-gate uint_t overshoot = (uint_t)(pages_req - found_on_free); 25607c478bd9Sstevel@tonic-gate 25617c478bd9Sstevel@tonic-gate if (overshoot) { 25627c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_create_overshoot); 256306fb6a36Sdv p = &pcf[PCF_INDEX()]; 25647c478bd9Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 25657c478bd9Sstevel@tonic-gate if (p->pcf_block) { 25667c478bd9Sstevel@tonic-gate p->pcf_reserve += overshoot; 25677c478bd9Sstevel@tonic-gate } else { 25687c478bd9Sstevel@tonic-gate p->pcf_count += overshoot; 25697c478bd9Sstevel@tonic-gate if (p->pcf_wait) { 25707c478bd9Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 25717c478bd9Sstevel@tonic-gate if (freemem_wait) { 25727c478bd9Sstevel@tonic-gate cv_signal(&freemem_cv); 25737c478bd9Sstevel@tonic-gate p->pcf_wait--; 25747c478bd9Sstevel@tonic-gate } else { 25757c478bd9Sstevel@tonic-gate p->pcf_wait = 0; 25767c478bd9Sstevel@tonic-gate } 25777c478bd9Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 25787c478bd9Sstevel@tonic-gate } 25797c478bd9Sstevel@tonic-gate } 25807c478bd9Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 25817c478bd9Sstevel@tonic-gate /* freemem is approximate, so this test OK */ 25827c478bd9Sstevel@tonic-gate if (!p->pcf_block) 25837c478bd9Sstevel@tonic-gate freemem += overshoot; 25847c478bd9Sstevel@tonic-gate } 25857c478bd9Sstevel@tonic-gate } 25867c478bd9Sstevel@tonic-gate 25877c478bd9Sstevel@tonic-gate return (plist); 25887c478bd9Sstevel@tonic-gate } 25897c478bd9Sstevel@tonic-gate 25907c478bd9Sstevel@tonic-gate /* 25917c478bd9Sstevel@tonic-gate * One or more constituent pages of this large page has been marked 25927c478bd9Sstevel@tonic-gate * toxic. Simply demote the large page to PAGESIZE pages and let 25937c478bd9Sstevel@tonic-gate * page_free() handle it. This routine should only be called by 25947c478bd9Sstevel@tonic-gate * large page free routines (page_free_pages() and page_destroy_pages(). 25957c478bd9Sstevel@tonic-gate * All pages are locked SE_EXCL and have already been marked free. 25967c478bd9Sstevel@tonic-gate */ 25977c478bd9Sstevel@tonic-gate static void 25987c478bd9Sstevel@tonic-gate page_free_toxic_pages(page_t *rootpp) 25997c478bd9Sstevel@tonic-gate { 26007c478bd9Sstevel@tonic-gate page_t *tpp; 26017c478bd9Sstevel@tonic-gate pgcnt_t i, pgcnt = page_get_pagecnt(rootpp->p_szc); 26027c478bd9Sstevel@tonic-gate uint_t szc = rootpp->p_szc; 26037c478bd9Sstevel@tonic-gate 26047c478bd9Sstevel@tonic-gate for (i = 0, tpp = rootpp; i < pgcnt; i++, tpp = tpp->p_next) { 26057c478bd9Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 26067c478bd9Sstevel@tonic-gate ASSERT((PAGE_EXCL(tpp) && 26077c478bd9Sstevel@tonic-gate !page_iolock_assert(tpp)) || panicstr); 26087c478bd9Sstevel@tonic-gate tpp->p_szc = 0; 26097c478bd9Sstevel@tonic-gate } 26107c478bd9Sstevel@tonic-gate 26117c478bd9Sstevel@tonic-gate while (rootpp != NULL) { 26127c478bd9Sstevel@tonic-gate tpp = rootpp; 26137c478bd9Sstevel@tonic-gate page_sub(&rootpp, tpp); 26147c478bd9Sstevel@tonic-gate ASSERT(PP_ISFREE(tpp)); 26157c478bd9Sstevel@tonic-gate PP_CLRFREE(tpp); 26167c478bd9Sstevel@tonic-gate page_free(tpp, 1); 26177c478bd9Sstevel@tonic-gate } 26187c478bd9Sstevel@tonic-gate } 26197c478bd9Sstevel@tonic-gate 26207c478bd9Sstevel@tonic-gate /* 26217c478bd9Sstevel@tonic-gate * Put page on the "free" list. 26227c478bd9Sstevel@tonic-gate * The free list is really two lists maintained by 26237c478bd9Sstevel@tonic-gate * the PSM of whatever machine we happen to be on. 26247c478bd9Sstevel@tonic-gate */ 26257c478bd9Sstevel@tonic-gate void 26267c478bd9Sstevel@tonic-gate page_free(page_t *pp, int dontneed) 26277c478bd9Sstevel@tonic-gate { 26287c478bd9Sstevel@tonic-gate struct pcf *p; 26297c478bd9Sstevel@tonic-gate uint_t pcf_index; 26307c478bd9Sstevel@tonic-gate 26317c478bd9Sstevel@tonic-gate ASSERT((PAGE_EXCL(pp) && 26327c478bd9Sstevel@tonic-gate !page_iolock_assert(pp)) || panicstr); 26337c478bd9Sstevel@tonic-gate 26347c478bd9Sstevel@tonic-gate if (PP_ISFREE(pp)) { 26357c478bd9Sstevel@tonic-gate panic("page_free: page %p is free", (void *)pp); 26367c478bd9Sstevel@tonic-gate } 26377c478bd9Sstevel@tonic-gate 26387c478bd9Sstevel@tonic-gate if (pp->p_szc != 0) { 26397c478bd9Sstevel@tonic-gate if (pp->p_vnode == NULL || IS_SWAPFSVP(pp->p_vnode) || 2640ad23a2dbSjohansen PP_ISKAS(pp)) { 26417c478bd9Sstevel@tonic-gate panic("page_free: anon or kernel " 26427c478bd9Sstevel@tonic-gate "or no vnode large page %p", (void *)pp); 26437c478bd9Sstevel@tonic-gate } 26447c478bd9Sstevel@tonic-gate page_demote_vp_pages(pp); 26457c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 26467c478bd9Sstevel@tonic-gate } 26477c478bd9Sstevel@tonic-gate 26487c478bd9Sstevel@tonic-gate /* 26497c478bd9Sstevel@tonic-gate * The page_struct_lock need not be acquired to examine these 26507c478bd9Sstevel@tonic-gate * fields since the page has an "exclusive" lock. 26517c478bd9Sstevel@tonic-gate */ 265207b65a64Saguzovsk if (hat_page_is_mapped(pp) || pp->p_lckcnt != 0 || pp->p_cowcnt != 0 || 265307b65a64Saguzovsk pp->p_slckcnt != 0) { 265407b65a64Saguzovsk panic("page_free pp=%p, pfn=%lx, lckcnt=%d, cowcnt=%d " 26558793b36bSNick Todd "slckcnt = %d", (void *)pp, page_pptonum(pp), pp->p_lckcnt, 265607b65a64Saguzovsk pp->p_cowcnt, pp->p_slckcnt); 26577c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 26587c478bd9Sstevel@tonic-gate } 26597c478bd9Sstevel@tonic-gate 26607c478bd9Sstevel@tonic-gate ASSERT(!hat_page_getshare(pp)); 26617c478bd9Sstevel@tonic-gate 26627c478bd9Sstevel@tonic-gate PP_SETFREE(pp); 26637c478bd9Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL || !IS_VMODSORT(pp->p_vnode) || 26647c478bd9Sstevel@tonic-gate !hat_ismod(pp)); 26652f0fcb93SJason Beloro page_clr_all_props(pp, 0); 26667c478bd9Sstevel@tonic-gate ASSERT(!hat_page_getshare(pp)); 26677c478bd9Sstevel@tonic-gate 26687c478bd9Sstevel@tonic-gate /* 26697c478bd9Sstevel@tonic-gate * Now we add the page to the head of the free list. 26707c478bd9Sstevel@tonic-gate * But if this page is associated with a paged vnode 26717c478bd9Sstevel@tonic-gate * then we adjust the head forward so that the page is 26727c478bd9Sstevel@tonic-gate * effectively at the end of the list. 26737c478bd9Sstevel@tonic-gate */ 26747c478bd9Sstevel@tonic-gate if (pp->p_vnode == NULL) { 26757c478bd9Sstevel@tonic-gate /* 26767c478bd9Sstevel@tonic-gate * Page has no identity, put it on the free list. 26777c478bd9Sstevel@tonic-gate */ 26787c478bd9Sstevel@tonic-gate PP_SETAGED(pp); 26797c478bd9Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1; 26807c478bd9Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 26817c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_free); 26827c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_FREE, 26837c478bd9Sstevel@tonic-gate "page_free_free:pp %p", pp); 26847c478bd9Sstevel@tonic-gate } else { 26857c478bd9Sstevel@tonic-gate PP_CLRAGED(pp); 26867c478bd9Sstevel@tonic-gate 26877c478bd9Sstevel@tonic-gate if (!dontneed || nopageage) { 26887c478bd9Sstevel@tonic-gate /* move it to the tail of the list */ 26897c478bd9Sstevel@tonic-gate page_list_add(pp, PG_CACHE_LIST | PG_LIST_TAIL); 26907c478bd9Sstevel@tonic-gate 26917c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_cache); 26927c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_CACHE_TAIL, 26937c478bd9Sstevel@tonic-gate "page_free_cache_tail:pp %p", pp); 26947c478bd9Sstevel@tonic-gate } else { 26957c478bd9Sstevel@tonic-gate page_list_add(pp, PG_CACHE_LIST | PG_LIST_HEAD); 26967c478bd9Sstevel@tonic-gate 26977c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_dontneed); 26987c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_CACHE_HEAD, 26997c478bd9Sstevel@tonic-gate "page_free_cache_head:pp %p", pp); 27007c478bd9Sstevel@tonic-gate } 27017c478bd9Sstevel@tonic-gate } 27027c478bd9Sstevel@tonic-gate page_unlock(pp); 27037c478bd9Sstevel@tonic-gate 27047c478bd9Sstevel@tonic-gate /* 27057c478bd9Sstevel@tonic-gate * Now do the `freemem' accounting. 27067c478bd9Sstevel@tonic-gate */ 27077c478bd9Sstevel@tonic-gate pcf_index = PCF_INDEX(); 27087c478bd9Sstevel@tonic-gate p = &pcf[pcf_index]; 27097c478bd9Sstevel@tonic-gate 27107c478bd9Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 27117c478bd9Sstevel@tonic-gate if (p->pcf_block) { 27127c478bd9Sstevel@tonic-gate p->pcf_reserve += 1; 27137c478bd9Sstevel@tonic-gate } else { 27147c478bd9Sstevel@tonic-gate p->pcf_count += 1; 27157c478bd9Sstevel@tonic-gate if (p->pcf_wait) { 27167c478bd9Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 27177c478bd9Sstevel@tonic-gate /* 27187c478bd9Sstevel@tonic-gate * Check to see if some other thread 27197c478bd9Sstevel@tonic-gate * is actually waiting. Another bucket 27207c478bd9Sstevel@tonic-gate * may have woken it up by now. If there 27217c478bd9Sstevel@tonic-gate * are no waiters, then set our pcf_wait 27227c478bd9Sstevel@tonic-gate * count to zero to avoid coming in here 27237c478bd9Sstevel@tonic-gate * next time. Also, since only one page 27247c478bd9Sstevel@tonic-gate * was put on the free list, just wake 27257c478bd9Sstevel@tonic-gate * up one waiter. 27267c478bd9Sstevel@tonic-gate */ 27277c478bd9Sstevel@tonic-gate if (freemem_wait) { 27287c478bd9Sstevel@tonic-gate cv_signal(&freemem_cv); 27297c478bd9Sstevel@tonic-gate p->pcf_wait--; 27307c478bd9Sstevel@tonic-gate } else { 27317c478bd9Sstevel@tonic-gate p->pcf_wait = 0; 27327c478bd9Sstevel@tonic-gate } 27337c478bd9Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 27347c478bd9Sstevel@tonic-gate } 27357c478bd9Sstevel@tonic-gate } 27367c478bd9Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 27377c478bd9Sstevel@tonic-gate 27387c478bd9Sstevel@tonic-gate /* freemem is approximate, so this test OK */ 27397c478bd9Sstevel@tonic-gate if (!p->pcf_block) 27407c478bd9Sstevel@tonic-gate freemem += 1; 27417c478bd9Sstevel@tonic-gate } 27427c478bd9Sstevel@tonic-gate 27437c478bd9Sstevel@tonic-gate /* 27447c478bd9Sstevel@tonic-gate * Put page on the "free" list during intial startup. 27457c478bd9Sstevel@tonic-gate * This happens during initial single threaded execution. 27467c478bd9Sstevel@tonic-gate */ 27477c478bd9Sstevel@tonic-gate void 27487c478bd9Sstevel@tonic-gate page_free_at_startup(page_t *pp) 27497c478bd9Sstevel@tonic-gate { 27507c478bd9Sstevel@tonic-gate struct pcf *p; 27517c478bd9Sstevel@tonic-gate uint_t pcf_index; 27527c478bd9Sstevel@tonic-gate 27537c478bd9Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_HEAD | PG_LIST_ISINIT); 27547c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_free); 27557c478bd9Sstevel@tonic-gate 27567c478bd9Sstevel@tonic-gate /* 27577c478bd9Sstevel@tonic-gate * Now do the `freemem' accounting. 27587c478bd9Sstevel@tonic-gate */ 27597c478bd9Sstevel@tonic-gate pcf_index = PCF_INDEX(); 27607c478bd9Sstevel@tonic-gate p = &pcf[pcf_index]; 27617c478bd9Sstevel@tonic-gate 27627c478bd9Sstevel@tonic-gate ASSERT(p->pcf_block == 0); 27637c478bd9Sstevel@tonic-gate ASSERT(p->pcf_wait == 0); 27647c478bd9Sstevel@tonic-gate p->pcf_count += 1; 27657c478bd9Sstevel@tonic-gate 27667c478bd9Sstevel@tonic-gate /* freemem is approximate, so this is OK */ 27677c478bd9Sstevel@tonic-gate freemem += 1; 27687c478bd9Sstevel@tonic-gate } 27697c478bd9Sstevel@tonic-gate 27707c478bd9Sstevel@tonic-gate void 27717c478bd9Sstevel@tonic-gate page_free_pages(page_t *pp) 27727c478bd9Sstevel@tonic-gate { 27737c478bd9Sstevel@tonic-gate page_t *tpp, *rootpp = NULL; 27747c478bd9Sstevel@tonic-gate pgcnt_t pgcnt = page_get_pagecnt(pp->p_szc); 27757c478bd9Sstevel@tonic-gate pgcnt_t i; 27767c478bd9Sstevel@tonic-gate uint_t szc = pp->p_szc; 27777c478bd9Sstevel@tonic-gate 27787c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_pages); 27797c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_FREE_FREE, 27807c478bd9Sstevel@tonic-gate "page_free_free:pp %p", pp); 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc != 0 && pp->p_szc < page_num_pagesizes()); 27837c478bd9Sstevel@tonic-gate if ((page_pptonum(pp) & (pgcnt - 1)) != 0) { 27847c478bd9Sstevel@tonic-gate panic("page_free_pages: not root page %p", (void *)pp); 27857c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 27867c478bd9Sstevel@tonic-gate } 27877c478bd9Sstevel@tonic-gate 2788affbd3ccSkchow for (i = 0, tpp = pp; i < pgcnt; i++, tpp++) { 27897c478bd9Sstevel@tonic-gate ASSERT((PAGE_EXCL(tpp) && 27907c478bd9Sstevel@tonic-gate !page_iolock_assert(tpp)) || panicstr); 27917c478bd9Sstevel@tonic-gate if (PP_ISFREE(tpp)) { 27927c478bd9Sstevel@tonic-gate panic("page_free_pages: page %p is free", (void *)tpp); 27937c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 27947c478bd9Sstevel@tonic-gate } 27957c478bd9Sstevel@tonic-gate if (hat_page_is_mapped(tpp) || tpp->p_lckcnt != 0 || 279607b65a64Saguzovsk tpp->p_cowcnt != 0 || tpp->p_slckcnt != 0) { 27977c478bd9Sstevel@tonic-gate panic("page_free_pages %p", (void *)tpp); 27987c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 27997c478bd9Sstevel@tonic-gate } 28007c478bd9Sstevel@tonic-gate 28017c478bd9Sstevel@tonic-gate ASSERT(!hat_page_getshare(tpp)); 28027c478bd9Sstevel@tonic-gate ASSERT(tpp->p_vnode == NULL); 28037c478bd9Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 28047c478bd9Sstevel@tonic-gate 28057c478bd9Sstevel@tonic-gate PP_SETFREE(tpp); 28062f0fcb93SJason Beloro page_clr_all_props(tpp, 0); 28077c478bd9Sstevel@tonic-gate PP_SETAGED(tpp); 28087c478bd9Sstevel@tonic-gate tpp->p_offset = (u_offset_t)-1; 28097c478bd9Sstevel@tonic-gate ASSERT(tpp->p_next == tpp); 28107c478bd9Sstevel@tonic-gate ASSERT(tpp->p_prev == tpp); 28117c478bd9Sstevel@tonic-gate page_list_concat(&rootpp, &tpp); 28127c478bd9Sstevel@tonic-gate } 28137c478bd9Sstevel@tonic-gate ASSERT(rootpp == pp); 28147c478bd9Sstevel@tonic-gate 28157c478bd9Sstevel@tonic-gate page_list_add_pages(rootpp, 0); 28167c478bd9Sstevel@tonic-gate page_create_putback(pgcnt); 28177c478bd9Sstevel@tonic-gate } 28187c478bd9Sstevel@tonic-gate 28197c478bd9Sstevel@tonic-gate int free_pages = 1; 28207c478bd9Sstevel@tonic-gate 28217c478bd9Sstevel@tonic-gate /* 28227c478bd9Sstevel@tonic-gate * This routine attempts to return pages to the cachelist via page_release(). 28237c478bd9Sstevel@tonic-gate * It does not *have* to be successful in all cases, since the pageout scanner 28247c478bd9Sstevel@tonic-gate * will catch any pages it misses. It does need to be fast and not introduce 28257c478bd9Sstevel@tonic-gate * too much overhead. 28267c478bd9Sstevel@tonic-gate * 28277c478bd9Sstevel@tonic-gate * If a page isn't found on the unlocked sweep of the page_hash bucket, we 28287c478bd9Sstevel@tonic-gate * don't lock and retry. This is ok, since the page scanner will eventually 28297c478bd9Sstevel@tonic-gate * find any page we miss in free_vp_pages(). 28307c478bd9Sstevel@tonic-gate */ 28317c478bd9Sstevel@tonic-gate void 28327c478bd9Sstevel@tonic-gate free_vp_pages(vnode_t *vp, u_offset_t off, size_t len) 28337c478bd9Sstevel@tonic-gate { 28347c478bd9Sstevel@tonic-gate page_t *pp; 28357c478bd9Sstevel@tonic-gate u_offset_t eoff; 28367c478bd9Sstevel@tonic-gate extern int swap_in_range(vnode_t *, u_offset_t, size_t); 28377c478bd9Sstevel@tonic-gate 28387c478bd9Sstevel@tonic-gate eoff = off + len; 28397c478bd9Sstevel@tonic-gate 28407c478bd9Sstevel@tonic-gate if (free_pages == 0) 28417c478bd9Sstevel@tonic-gate return; 28427c478bd9Sstevel@tonic-gate if (swap_in_range(vp, off, len)) 28437c478bd9Sstevel@tonic-gate return; 28447c478bd9Sstevel@tonic-gate 28457c478bd9Sstevel@tonic-gate for (; off < eoff; off += PAGESIZE) { 28467c478bd9Sstevel@tonic-gate 28477c478bd9Sstevel@tonic-gate /* 28487c478bd9Sstevel@tonic-gate * find the page using a fast, but inexact search. It'll be OK 28497c478bd9Sstevel@tonic-gate * if a few pages slip through the cracks here. 28507c478bd9Sstevel@tonic-gate */ 28517c478bd9Sstevel@tonic-gate pp = page_exists(vp, off); 28527c478bd9Sstevel@tonic-gate 28537c478bd9Sstevel@tonic-gate /* 28547c478bd9Sstevel@tonic-gate * If we didn't find the page (it may not exist), the page 28557c478bd9Sstevel@tonic-gate * is free, looks still in use (shared), or we can't lock it, 28567c478bd9Sstevel@tonic-gate * just give up. 28577c478bd9Sstevel@tonic-gate */ 28587c478bd9Sstevel@tonic-gate if (pp == NULL || 28597c478bd9Sstevel@tonic-gate PP_ISFREE(pp) || 28607c478bd9Sstevel@tonic-gate page_share_cnt(pp) > 0 || 28617c478bd9Sstevel@tonic-gate !page_trylock(pp, SE_EXCL)) 28627c478bd9Sstevel@tonic-gate continue; 28637c478bd9Sstevel@tonic-gate 28647c478bd9Sstevel@tonic-gate /* 28657c478bd9Sstevel@tonic-gate * Once we have locked pp, verify that it's still the 28667c478bd9Sstevel@tonic-gate * correct page and not already free 28677c478bd9Sstevel@tonic-gate */ 28687c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED_SE(pp, SE_EXCL)); 28697c478bd9Sstevel@tonic-gate if (pp->p_vnode != vp || pp->p_offset != off || PP_ISFREE(pp)) { 28707c478bd9Sstevel@tonic-gate page_unlock(pp); 28717c478bd9Sstevel@tonic-gate continue; 28727c478bd9Sstevel@tonic-gate } 28737c478bd9Sstevel@tonic-gate 28747c478bd9Sstevel@tonic-gate /* 28757c478bd9Sstevel@tonic-gate * try to release the page... 28767c478bd9Sstevel@tonic-gate */ 28777c478bd9Sstevel@tonic-gate (void) page_release(pp, 1); 28787c478bd9Sstevel@tonic-gate } 28797c478bd9Sstevel@tonic-gate } 28807c478bd9Sstevel@tonic-gate 28817c478bd9Sstevel@tonic-gate /* 28827c478bd9Sstevel@tonic-gate * Reclaim the given page from the free list. 28836e4dd838Smec * If pp is part of a large pages, only the given constituent page is reclaimed 28846e4dd838Smec * and the large page it belonged to will be demoted. This can only happen 28856e4dd838Smec * if the page is not on the cachelist. 28866e4dd838Smec * 28877c478bd9Sstevel@tonic-gate * Returns 1 on success or 0 on failure. 28887c478bd9Sstevel@tonic-gate * 28897c478bd9Sstevel@tonic-gate * The page is unlocked if it can't be reclaimed (when freemem == 0). 28907c478bd9Sstevel@tonic-gate * If `lock' is non-null, it will be dropped and re-acquired if 28917c478bd9Sstevel@tonic-gate * the routine must wait while freemem is 0. 28927c478bd9Sstevel@tonic-gate * 28937c478bd9Sstevel@tonic-gate * As it turns out, boot_getpages() does this. It picks a page, 28947c478bd9Sstevel@tonic-gate * based on where OBP mapped in some address, gets its pfn, searches 28957c478bd9Sstevel@tonic-gate * the memsegs, locks the page, then pulls it off the free list! 28967c478bd9Sstevel@tonic-gate */ 28977c478bd9Sstevel@tonic-gate int 28987c478bd9Sstevel@tonic-gate page_reclaim(page_t *pp, kmutex_t *lock) 28997c478bd9Sstevel@tonic-gate { 29007c478bd9Sstevel@tonic-gate struct pcf *p; 29017c478bd9Sstevel@tonic-gate struct cpu *cpup; 29026e4dd838Smec int enough; 29037c478bd9Sstevel@tonic-gate uint_t i; 29047c478bd9Sstevel@tonic-gate 29057c478bd9Sstevel@tonic-gate ASSERT(lock != NULL ? MUTEX_HELD(lock) : 1); 29067c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp) && PP_ISFREE(pp)); 2907db874c57Selowe 29087c478bd9Sstevel@tonic-gate /* 29097c478bd9Sstevel@tonic-gate * If `freemem' is 0, we cannot reclaim this page from the 29107c478bd9Sstevel@tonic-gate * freelist, so release every lock we might hold: the page, 29117c478bd9Sstevel@tonic-gate * and the `lock' before blocking. 29127c478bd9Sstevel@tonic-gate * 29137c478bd9Sstevel@tonic-gate * The only way `freemem' can become 0 while there are pages 29147c478bd9Sstevel@tonic-gate * marked free (have their p->p_free bit set) is when the 29157c478bd9Sstevel@tonic-gate * system is low on memory and doing a page_create(). In 29167c478bd9Sstevel@tonic-gate * order to guarantee that once page_create() starts acquiring 29177c478bd9Sstevel@tonic-gate * pages it will be able to get all that it needs since `freemem' 29187c478bd9Sstevel@tonic-gate * was decreased by the requested amount. So, we need to release 29197c478bd9Sstevel@tonic-gate * this page, and let page_create() have it. 29207c478bd9Sstevel@tonic-gate * 29217c478bd9Sstevel@tonic-gate * Since `freemem' being zero is not supposed to happen, just 29227c478bd9Sstevel@tonic-gate * use the usual hash stuff as a starting point. If that bucket 29237c478bd9Sstevel@tonic-gate * is empty, then assume the worst, and start at the beginning 29247c478bd9Sstevel@tonic-gate * of the pcf array. If we always start at the beginning 29257c478bd9Sstevel@tonic-gate * when acquiring more than one pcf lock, there won't be any 29267c478bd9Sstevel@tonic-gate * deadlock problems. 29277c478bd9Sstevel@tonic-gate */ 29287c478bd9Sstevel@tonic-gate 29297c478bd9Sstevel@tonic-gate /* TODO: Do we need to test kcage_freemem if PG_NORELOC(pp)? */ 29307c478bd9Sstevel@tonic-gate 29316e4dd838Smec if (freemem <= throttlefree && !page_create_throttle(1l, 0)) { 29327c478bd9Sstevel@tonic-gate pcf_acquire_all(); 29337c478bd9Sstevel@tonic-gate goto page_reclaim_nomem; 29347c478bd9Sstevel@tonic-gate } 29357c478bd9Sstevel@tonic-gate 293606fb6a36Sdv enough = pcf_decrement_bucket(1); 29377c478bd9Sstevel@tonic-gate 29386e4dd838Smec if (!enough) { 29397c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_reclaim_zero); 29407c478bd9Sstevel@tonic-gate /* 29417c478bd9Sstevel@tonic-gate * Check again. Its possible that some other thread 29427c478bd9Sstevel@tonic-gate * could have been right behind us, and added one 29437c478bd9Sstevel@tonic-gate * to a list somewhere. Acquire each of the pcf locks 29447c478bd9Sstevel@tonic-gate * until we find a page. 29457c478bd9Sstevel@tonic-gate */ 29467c478bd9Sstevel@tonic-gate p = pcf; 294706fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 29487c478bd9Sstevel@tonic-gate mutex_enter(&p->pcf_lock); 29496e4dd838Smec if (p->pcf_count >= 1) { 29506e4dd838Smec p->pcf_count -= 1; 29515797d5ddSDavid Valin /* 29525797d5ddSDavid Valin * freemem is not protected by any lock. Thus, 29535797d5ddSDavid Valin * we cannot have any assertion containing 29545797d5ddSDavid Valin * freemem here. 29555797d5ddSDavid Valin */ 29565797d5ddSDavid Valin freemem -= 1; 29576e4dd838Smec enough = 1; 29586e4dd838Smec break; 29597c478bd9Sstevel@tonic-gate } 29607c478bd9Sstevel@tonic-gate p++; 29617c478bd9Sstevel@tonic-gate } 29627c478bd9Sstevel@tonic-gate 29636e4dd838Smec if (!enough) { 29647c478bd9Sstevel@tonic-gate page_reclaim_nomem: 29657c478bd9Sstevel@tonic-gate /* 29667c478bd9Sstevel@tonic-gate * We really can't have page `pp'. 29677c478bd9Sstevel@tonic-gate * Time for the no-memory dance with 29687c478bd9Sstevel@tonic-gate * page_free(). This is just like 29697c478bd9Sstevel@tonic-gate * page_create_wait(). Plus the added 29707c478bd9Sstevel@tonic-gate * attraction of releasing whatever mutex 29717c478bd9Sstevel@tonic-gate * we held when we were called with in `lock'. 29727c478bd9Sstevel@tonic-gate * Page_unlock() will wakeup any thread 29737c478bd9Sstevel@tonic-gate * waiting around for this page. 29747c478bd9Sstevel@tonic-gate */ 29757c478bd9Sstevel@tonic-gate if (lock) { 29767c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_reclaim_zero_locked); 29777c478bd9Sstevel@tonic-gate mutex_exit(lock); 29787c478bd9Sstevel@tonic-gate } 29797c478bd9Sstevel@tonic-gate page_unlock(pp); 29807c478bd9Sstevel@tonic-gate 29817c478bd9Sstevel@tonic-gate /* 29827c478bd9Sstevel@tonic-gate * get this before we drop all the pcf locks. 29837c478bd9Sstevel@tonic-gate */ 29847c478bd9Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 29857c478bd9Sstevel@tonic-gate 29867c478bd9Sstevel@tonic-gate p = pcf; 298706fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 29887c478bd9Sstevel@tonic-gate p->pcf_wait++; 29897c478bd9Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 29907c478bd9Sstevel@tonic-gate p++; 29917c478bd9Sstevel@tonic-gate } 29927c478bd9Sstevel@tonic-gate 29937c478bd9Sstevel@tonic-gate freemem_wait++; 29947c478bd9Sstevel@tonic-gate cv_wait(&freemem_cv, &new_freemem_lock); 29957c478bd9Sstevel@tonic-gate freemem_wait--; 29967c478bd9Sstevel@tonic-gate 29977c478bd9Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 29987c478bd9Sstevel@tonic-gate 29997c478bd9Sstevel@tonic-gate if (lock) { 30007c478bd9Sstevel@tonic-gate mutex_enter(lock); 30017c478bd9Sstevel@tonic-gate } 30027c478bd9Sstevel@tonic-gate return (0); 30037c478bd9Sstevel@tonic-gate } 30047c478bd9Sstevel@tonic-gate 30057c478bd9Sstevel@tonic-gate /* 30067c478bd9Sstevel@tonic-gate * The pcf accounting has been done, 30077c478bd9Sstevel@tonic-gate * though none of the pcf_wait flags have been set, 30087c478bd9Sstevel@tonic-gate * drop the locks and continue on. 30097c478bd9Sstevel@tonic-gate */ 30107c478bd9Sstevel@tonic-gate while (p >= pcf) { 30117c478bd9Sstevel@tonic-gate mutex_exit(&p->pcf_lock); 30127c478bd9Sstevel@tonic-gate p--; 30137c478bd9Sstevel@tonic-gate } 30147c478bd9Sstevel@tonic-gate } 30157c478bd9Sstevel@tonic-gate 30167c478bd9Sstevel@tonic-gate 30177c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_reclaim); 30186e4dd838Smec 30196e4dd838Smec /* 30206e4dd838Smec * page_list_sub will handle the case where pp is a large page. 30216e4dd838Smec * It's possible that the page was promoted while on the freelist 30226e4dd838Smec */ 30237c478bd9Sstevel@tonic-gate if (PP_ISAGED(pp)) { 30246e4dd838Smec page_list_sub(pp, PG_FREE_LIST); 30257c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_FREE, 30267c478bd9Sstevel@tonic-gate "page_reclaim_free:pp %p", pp); 30277c478bd9Sstevel@tonic-gate } else { 30287c478bd9Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST); 30297c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_UNFREE_CACHE, 30307c478bd9Sstevel@tonic-gate "page_reclaim_cache:pp %p", pp); 30317c478bd9Sstevel@tonic-gate } 30327c478bd9Sstevel@tonic-gate 30337c478bd9Sstevel@tonic-gate /* 30347c478bd9Sstevel@tonic-gate * clear the p_free & p_age bits since this page is no longer 30357c478bd9Sstevel@tonic-gate * on the free list. Notice that there was a brief time where 30367c478bd9Sstevel@tonic-gate * a page is marked as free, but is not on the list. 30377c478bd9Sstevel@tonic-gate * 30387c478bd9Sstevel@tonic-gate * Set the reference bit to protect against immediate pageout. 30397c478bd9Sstevel@tonic-gate */ 30406e4dd838Smec PP_CLRFREE(pp); 30416e4dd838Smec PP_CLRAGED(pp); 30426e4dd838Smec page_set_props(pp, P_REF); 30437c478bd9Sstevel@tonic-gate 30447c478bd9Sstevel@tonic-gate CPU_STATS_ENTER_K(); 30457c478bd9Sstevel@tonic-gate cpup = CPU; /* get cpup now that CPU cannot change */ 30467c478bd9Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgrec, 1); 30477c478bd9Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, vm, pgfrec, 1); 30487c478bd9Sstevel@tonic-gate CPU_STATS_EXIT_K(); 30496e4dd838Smec ASSERT(pp->p_szc == 0); 30507c478bd9Sstevel@tonic-gate 30517c478bd9Sstevel@tonic-gate return (1); 30527c478bd9Sstevel@tonic-gate } 30537c478bd9Sstevel@tonic-gate 30547c478bd9Sstevel@tonic-gate /* 30557c478bd9Sstevel@tonic-gate * Destroy identity of the page and put it back on 30567c478bd9Sstevel@tonic-gate * the page free list. Assumes that the caller has 30577c478bd9Sstevel@tonic-gate * acquired the "exclusive" lock on the page. 30587c478bd9Sstevel@tonic-gate */ 30597c478bd9Sstevel@tonic-gate void 30607c478bd9Sstevel@tonic-gate page_destroy(page_t *pp, int dontfree) 30617c478bd9Sstevel@tonic-gate { 30627c478bd9Sstevel@tonic-gate ASSERT((PAGE_EXCL(pp) && 30637c478bd9Sstevel@tonic-gate !page_iolock_assert(pp)) || panicstr); 306407b65a64Saguzovsk ASSERT(pp->p_slckcnt == 0 || panicstr); 30657c478bd9Sstevel@tonic-gate 30667c478bd9Sstevel@tonic-gate if (pp->p_szc != 0) { 30677c478bd9Sstevel@tonic-gate if (pp->p_vnode == NULL || IS_SWAPFSVP(pp->p_vnode) || 3068ad23a2dbSjohansen PP_ISKAS(pp)) { 30697c478bd9Sstevel@tonic-gate panic("page_destroy: anon or kernel or no vnode " 30707c478bd9Sstevel@tonic-gate "large page %p", (void *)pp); 30717c478bd9Sstevel@tonic-gate } 30727c478bd9Sstevel@tonic-gate page_demote_vp_pages(pp); 30737c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 30747c478bd9Sstevel@tonic-gate } 30757c478bd9Sstevel@tonic-gate 30767c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_DESTROY, "page_destroy:pp %p", pp); 30777c478bd9Sstevel@tonic-gate 30787c478bd9Sstevel@tonic-gate /* 30797c478bd9Sstevel@tonic-gate * Unload translations, if any, then hash out the 30807c478bd9Sstevel@tonic-gate * page to erase its identity. 30817c478bd9Sstevel@tonic-gate */ 30827c478bd9Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 30837c478bd9Sstevel@tonic-gate page_hashout(pp, NULL); 30847c478bd9Sstevel@tonic-gate 30857c478bd9Sstevel@tonic-gate if (!dontfree) { 30867c478bd9Sstevel@tonic-gate /* 30877c478bd9Sstevel@tonic-gate * Acquire the "freemem_lock" for availrmem. 30887c478bd9Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt 30897c478bd9Sstevel@tonic-gate * and cowcnt since the page has an "exclusive" lock. 3090552507c5SGangadhar Mylapuram * We are doing a modified version of page_pp_unlock here. 30917c478bd9Sstevel@tonic-gate */ 30927c478bd9Sstevel@tonic-gate if ((pp->p_lckcnt != 0) || (pp->p_cowcnt != 0)) { 30937c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 30947c478bd9Sstevel@tonic-gate if (pp->p_lckcnt != 0) { 30957c478bd9Sstevel@tonic-gate availrmem++; 3096552507c5SGangadhar Mylapuram pages_locked--; 30977c478bd9Sstevel@tonic-gate pp->p_lckcnt = 0; 30987c478bd9Sstevel@tonic-gate } 30997c478bd9Sstevel@tonic-gate if (pp->p_cowcnt != 0) { 31007c478bd9Sstevel@tonic-gate availrmem += pp->p_cowcnt; 3101552507c5SGangadhar Mylapuram pages_locked -= pp->p_cowcnt; 31027c478bd9Sstevel@tonic-gate pp->p_cowcnt = 0; 31037c478bd9Sstevel@tonic-gate } 31047c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 31057c478bd9Sstevel@tonic-gate } 31067c478bd9Sstevel@tonic-gate /* 31077c478bd9Sstevel@tonic-gate * Put the page on the "free" list. 31087c478bd9Sstevel@tonic-gate */ 31097c478bd9Sstevel@tonic-gate page_free(pp, 0); 31107c478bd9Sstevel@tonic-gate } 31117c478bd9Sstevel@tonic-gate } 31127c478bd9Sstevel@tonic-gate 31137c478bd9Sstevel@tonic-gate void 31147c478bd9Sstevel@tonic-gate page_destroy_pages(page_t *pp) 31157c478bd9Sstevel@tonic-gate { 31167c478bd9Sstevel@tonic-gate 31177c478bd9Sstevel@tonic-gate page_t *tpp, *rootpp = NULL; 31187c478bd9Sstevel@tonic-gate pgcnt_t pgcnt = page_get_pagecnt(pp->p_szc); 31197c478bd9Sstevel@tonic-gate pgcnt_t i, pglcks = 0; 31207c478bd9Sstevel@tonic-gate uint_t szc = pp->p_szc; 31217c478bd9Sstevel@tonic-gate 31227c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc != 0 && pp->p_szc < page_num_pagesizes()); 31237c478bd9Sstevel@tonic-gate 31247c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_destroy_pages); 31257c478bd9Sstevel@tonic-gate 31267c478bd9Sstevel@tonic-gate TRACE_1(TR_FAC_VM, TR_PAGE_DESTROY, "page_destroy_pages:pp %p", pp); 31277c478bd9Sstevel@tonic-gate 31287c478bd9Sstevel@tonic-gate if ((page_pptonum(pp) & (pgcnt - 1)) != 0) { 31297c478bd9Sstevel@tonic-gate panic("page_destroy_pages: not root page %p", (void *)pp); 31307c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 31317c478bd9Sstevel@tonic-gate } 31327c478bd9Sstevel@tonic-gate 3133affbd3ccSkchow for (i = 0, tpp = pp; i < pgcnt; i++, tpp++) { 31347c478bd9Sstevel@tonic-gate ASSERT((PAGE_EXCL(tpp) && 31357c478bd9Sstevel@tonic-gate !page_iolock_assert(tpp)) || panicstr); 313607b65a64Saguzovsk ASSERT(tpp->p_slckcnt == 0 || panicstr); 31377c478bd9Sstevel@tonic-gate (void) hat_pageunload(tpp, HAT_FORCE_PGUNLOAD); 31387c478bd9Sstevel@tonic-gate page_hashout(tpp, NULL); 31397c478bd9Sstevel@tonic-gate ASSERT(tpp->p_offset == (u_offset_t)-1); 31407c478bd9Sstevel@tonic-gate if (tpp->p_lckcnt != 0) { 31417c478bd9Sstevel@tonic-gate pglcks++; 31427c478bd9Sstevel@tonic-gate tpp->p_lckcnt = 0; 31437c478bd9Sstevel@tonic-gate } else if (tpp->p_cowcnt != 0) { 31447c478bd9Sstevel@tonic-gate pglcks += tpp->p_cowcnt; 31457c478bd9Sstevel@tonic-gate tpp->p_cowcnt = 0; 31467c478bd9Sstevel@tonic-gate } 31477c478bd9Sstevel@tonic-gate ASSERT(!hat_page_getshare(tpp)); 31487c478bd9Sstevel@tonic-gate ASSERT(tpp->p_vnode == NULL); 31497c478bd9Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 31507c478bd9Sstevel@tonic-gate 31517c478bd9Sstevel@tonic-gate PP_SETFREE(tpp); 31522f0fcb93SJason Beloro page_clr_all_props(tpp, 0); 31537c478bd9Sstevel@tonic-gate PP_SETAGED(tpp); 31547c478bd9Sstevel@tonic-gate ASSERT(tpp->p_next == tpp); 31557c478bd9Sstevel@tonic-gate ASSERT(tpp->p_prev == tpp); 31567c478bd9Sstevel@tonic-gate page_list_concat(&rootpp, &tpp); 31577c478bd9Sstevel@tonic-gate } 31587c478bd9Sstevel@tonic-gate 31597c478bd9Sstevel@tonic-gate ASSERT(rootpp == pp); 31607c478bd9Sstevel@tonic-gate if (pglcks != 0) { 31617c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 31627c478bd9Sstevel@tonic-gate availrmem += pglcks; 31637c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 31647c478bd9Sstevel@tonic-gate } 31657c478bd9Sstevel@tonic-gate 31667c478bd9Sstevel@tonic-gate page_list_add_pages(rootpp, 0); 31677c478bd9Sstevel@tonic-gate page_create_putback(pgcnt); 31687c478bd9Sstevel@tonic-gate } 31697c478bd9Sstevel@tonic-gate 31707c478bd9Sstevel@tonic-gate /* 31717c478bd9Sstevel@tonic-gate * Similar to page_destroy(), but destroys pages which are 31727c478bd9Sstevel@tonic-gate * locked and known to be on the page free list. Since 31737c478bd9Sstevel@tonic-gate * the page is known to be free and locked, no one can access 31747c478bd9Sstevel@tonic-gate * it. 31757c478bd9Sstevel@tonic-gate * 31767c478bd9Sstevel@tonic-gate * Also, the number of free pages does not change. 31777c478bd9Sstevel@tonic-gate */ 31787c478bd9Sstevel@tonic-gate void 31797c478bd9Sstevel@tonic-gate page_destroy_free(page_t *pp) 31807c478bd9Sstevel@tonic-gate { 31817c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 31827c478bd9Sstevel@tonic-gate ASSERT(PP_ISFREE(pp)); 31837c478bd9Sstevel@tonic-gate ASSERT(pp->p_vnode); 31847c478bd9Sstevel@tonic-gate ASSERT(hat_page_getattr(pp, P_MOD | P_REF | P_RO) == 0); 31857c478bd9Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 31867c478bd9Sstevel@tonic-gate ASSERT(PP_ISAGED(pp) == 0); 31877c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 31887c478bd9Sstevel@tonic-gate 31897c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_destroy_free); 31907c478bd9Sstevel@tonic-gate page_list_sub(pp, PG_CACHE_LIST); 31917c478bd9Sstevel@tonic-gate 31927c478bd9Sstevel@tonic-gate page_hashout(pp, NULL); 31937c478bd9Sstevel@tonic-gate ASSERT(pp->p_vnode == NULL); 31947c478bd9Sstevel@tonic-gate ASSERT(pp->p_offset == (u_offset_t)-1); 31957c478bd9Sstevel@tonic-gate ASSERT(pp->p_hash == NULL); 31967c478bd9Sstevel@tonic-gate 31977c478bd9Sstevel@tonic-gate PP_SETAGED(pp); 31987c478bd9Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 31997c478bd9Sstevel@tonic-gate page_unlock(pp); 32007c478bd9Sstevel@tonic-gate 32017c478bd9Sstevel@tonic-gate mutex_enter(&new_freemem_lock); 32027c478bd9Sstevel@tonic-gate if (freemem_wait) { 32037c478bd9Sstevel@tonic-gate cv_signal(&freemem_cv); 32047c478bd9Sstevel@tonic-gate } 32057c478bd9Sstevel@tonic-gate mutex_exit(&new_freemem_lock); 32067c478bd9Sstevel@tonic-gate } 32077c478bd9Sstevel@tonic-gate 32087c478bd9Sstevel@tonic-gate /* 32097c478bd9Sstevel@tonic-gate * Rename the page "opp" to have an identity specified 32107c478bd9Sstevel@tonic-gate * by [vp, off]. If a page already exists with this name 32117c478bd9Sstevel@tonic-gate * it is locked and destroyed. Note that the page's 32127c478bd9Sstevel@tonic-gate * translations are not unloaded during the rename. 32137c478bd9Sstevel@tonic-gate * 32147c478bd9Sstevel@tonic-gate * This routine is used by the anon layer to "steal" the 32157c478bd9Sstevel@tonic-gate * original page and is not unlike destroying a page and 32167c478bd9Sstevel@tonic-gate * creating a new page using the same page frame. 32177c478bd9Sstevel@tonic-gate * 32187c478bd9Sstevel@tonic-gate * XXX -- Could deadlock if caller 1 tries to rename A to B while 32197c478bd9Sstevel@tonic-gate * caller 2 tries to rename B to A. 32207c478bd9Sstevel@tonic-gate */ 32217c478bd9Sstevel@tonic-gate void 32227c478bd9Sstevel@tonic-gate page_rename(page_t *opp, vnode_t *vp, u_offset_t off) 32237c478bd9Sstevel@tonic-gate { 32247c478bd9Sstevel@tonic-gate page_t *pp; 32257c478bd9Sstevel@tonic-gate int olckcnt = 0; 32267c478bd9Sstevel@tonic-gate int ocowcnt = 0; 32277c478bd9Sstevel@tonic-gate kmutex_t *phm; 32287c478bd9Sstevel@tonic-gate ulong_t index; 32297c478bd9Sstevel@tonic-gate 32307c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(opp) && !page_iolock_assert(opp)); 32317c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 32327c478bd9Sstevel@tonic-gate ASSERT(PP_ISFREE(opp) == 0); 32337c478bd9Sstevel@tonic-gate 32347c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_rename_count); 32357c478bd9Sstevel@tonic-gate 32367c478bd9Sstevel@tonic-gate TRACE_3(TR_FAC_VM, TR_PAGE_RENAME, 32376e4dd838Smec "page rename:pp %p vp %p off %llx", opp, vp, off); 32387c478bd9Sstevel@tonic-gate 323937fbc076Saguzovsk /* 324037fbc076Saguzovsk * CacheFS may call page_rename for a large NFS page 324137fbc076Saguzovsk * when both CacheFS and NFS mount points are used 324237fbc076Saguzovsk * by applications. Demote this large page before 324337fbc076Saguzovsk * renaming it, to ensure that there are no "partial" 324437fbc076Saguzovsk * large pages left lying around. 324537fbc076Saguzovsk */ 324637fbc076Saguzovsk if (opp->p_szc != 0) { 324737fbc076Saguzovsk vnode_t *ovp = opp->p_vnode; 324837fbc076Saguzovsk ASSERT(ovp != NULL); 324937fbc076Saguzovsk ASSERT(!IS_SWAPFSVP(ovp)); 3250ad23a2dbSjohansen ASSERT(!VN_ISKAS(ovp)); 325137fbc076Saguzovsk page_demote_vp_pages(opp); 325237fbc076Saguzovsk ASSERT(opp->p_szc == 0); 325337fbc076Saguzovsk } 325437fbc076Saguzovsk 32557c478bd9Sstevel@tonic-gate page_hashout(opp, NULL); 32567c478bd9Sstevel@tonic-gate PP_CLRAGED(opp); 32577c478bd9Sstevel@tonic-gate 32587c478bd9Sstevel@tonic-gate /* 32597c478bd9Sstevel@tonic-gate * Acquire the appropriate page hash lock, since 32607c478bd9Sstevel@tonic-gate * we're going to rename the page. 32617c478bd9Sstevel@tonic-gate */ 32627c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, off); 32637c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(index); 32647c478bd9Sstevel@tonic-gate mutex_enter(phm); 32657c478bd9Sstevel@tonic-gate top: 32667c478bd9Sstevel@tonic-gate /* 32677c478bd9Sstevel@tonic-gate * Look for an existing page with this name and destroy it if found. 32687c478bd9Sstevel@tonic-gate * By holding the page hash lock all the way to the page_hashin() 32697c478bd9Sstevel@tonic-gate * call, we are assured that no page can be created with this 32707c478bd9Sstevel@tonic-gate * identity. In the case when the phm lock is dropped to undo any 32717c478bd9Sstevel@tonic-gate * hat layer mappings, the existing page is held with an "exclusive" 32727c478bd9Sstevel@tonic-gate * lock, again preventing another page from being created with 32737c478bd9Sstevel@tonic-gate * this identity. 32747c478bd9Sstevel@tonic-gate */ 32757c478bd9Sstevel@tonic-gate PAGE_HASH_SEARCH(index, pp, vp, off); 32767c478bd9Sstevel@tonic-gate if (pp != NULL) { 32777c478bd9Sstevel@tonic-gate VM_STAT_ADD(page_rename_exists); 32787c478bd9Sstevel@tonic-gate 32797c478bd9Sstevel@tonic-gate /* 32807c478bd9Sstevel@tonic-gate * As it turns out, this is one of only two places where 32817c478bd9Sstevel@tonic-gate * page_lock() needs to hold the passed in lock in the 32827c478bd9Sstevel@tonic-gate * successful case. In all of the others, the lock could 32837c478bd9Sstevel@tonic-gate * be dropped as soon as the attempt is made to lock 32847c478bd9Sstevel@tonic-gate * the page. It is tempting to add yet another arguement, 32857c478bd9Sstevel@tonic-gate * PL_KEEP or PL_DROP, to let page_lock know what to do. 32867c478bd9Sstevel@tonic-gate */ 32877c478bd9Sstevel@tonic-gate if (!page_lock(pp, SE_EXCL, phm, P_RECLAIM)) { 32887c478bd9Sstevel@tonic-gate /* 32897c478bd9Sstevel@tonic-gate * Went to sleep because the page could not 32907c478bd9Sstevel@tonic-gate * be locked. We were woken up when the page 32917c478bd9Sstevel@tonic-gate * was unlocked, or when the page was destroyed. 32927c478bd9Sstevel@tonic-gate * In either case, `phm' was dropped while we 32937c478bd9Sstevel@tonic-gate * slept. Hence we should not just roar through 32947c478bd9Sstevel@tonic-gate * this loop. 32957c478bd9Sstevel@tonic-gate */ 32967c478bd9Sstevel@tonic-gate goto top; 32977c478bd9Sstevel@tonic-gate } 32987c478bd9Sstevel@tonic-gate 329937fbc076Saguzovsk /* 330037fbc076Saguzovsk * If an existing page is a large page, then demote 330137fbc076Saguzovsk * it to ensure that no "partial" large pages are 330237fbc076Saguzovsk * "created" after page_rename. An existing page 330337fbc076Saguzovsk * can be a CacheFS page, and can't belong to swapfs. 330437fbc076Saguzovsk */ 33057c478bd9Sstevel@tonic-gate if (hat_page_is_mapped(pp)) { 33067c478bd9Sstevel@tonic-gate /* 33077c478bd9Sstevel@tonic-gate * Unload translations. Since we hold the 33087c478bd9Sstevel@tonic-gate * exclusive lock on this page, the page 33097c478bd9Sstevel@tonic-gate * can not be changed while we drop phm. 33107c478bd9Sstevel@tonic-gate * This is also not a lock protocol violation, 33117c478bd9Sstevel@tonic-gate * but rather the proper way to do things. 33127c478bd9Sstevel@tonic-gate */ 33137c478bd9Sstevel@tonic-gate mutex_exit(phm); 33147c478bd9Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 331537fbc076Saguzovsk if (pp->p_szc != 0) { 331637fbc076Saguzovsk ASSERT(!IS_SWAPFSVP(vp)); 3317ad23a2dbSjohansen ASSERT(!VN_ISKAS(vp)); 331837fbc076Saguzovsk page_demote_vp_pages(pp); 331937fbc076Saguzovsk ASSERT(pp->p_szc == 0); 332037fbc076Saguzovsk } 332137fbc076Saguzovsk mutex_enter(phm); 332237fbc076Saguzovsk } else if (pp->p_szc != 0) { 332337fbc076Saguzovsk ASSERT(!IS_SWAPFSVP(vp)); 3324ad23a2dbSjohansen ASSERT(!VN_ISKAS(vp)); 332537fbc076Saguzovsk mutex_exit(phm); 332637fbc076Saguzovsk page_demote_vp_pages(pp); 332737fbc076Saguzovsk ASSERT(pp->p_szc == 0); 33287c478bd9Sstevel@tonic-gate mutex_enter(phm); 33297c478bd9Sstevel@tonic-gate } 33307c478bd9Sstevel@tonic-gate page_hashout(pp, phm); 33317c478bd9Sstevel@tonic-gate } 33327c478bd9Sstevel@tonic-gate /* 33337c478bd9Sstevel@tonic-gate * Hash in the page with the new identity. 33347c478bd9Sstevel@tonic-gate */ 33357c478bd9Sstevel@tonic-gate if (!page_hashin(opp, vp, off, phm)) { 33367c478bd9Sstevel@tonic-gate /* 33377c478bd9Sstevel@tonic-gate * We were holding phm while we searched for [vp, off] 33387c478bd9Sstevel@tonic-gate * and only dropped phm if we found and locked a page. 33397c478bd9Sstevel@tonic-gate * If we can't create this page now, then some thing 33407c478bd9Sstevel@tonic-gate * is really broken. 33417c478bd9Sstevel@tonic-gate */ 33427c478bd9Sstevel@tonic-gate panic("page_rename: Can't hash in page: %p", (void *)pp); 33437c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 33447c478bd9Sstevel@tonic-gate } 33457c478bd9Sstevel@tonic-gate 33467c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(phm)); 33477c478bd9Sstevel@tonic-gate mutex_exit(phm); 33487c478bd9Sstevel@tonic-gate 33497c478bd9Sstevel@tonic-gate /* 33507c478bd9Sstevel@tonic-gate * Now that we have dropped phm, lets get around to finishing up 33517c478bd9Sstevel@tonic-gate * with pp. 33527c478bd9Sstevel@tonic-gate */ 33537c478bd9Sstevel@tonic-gate if (pp != NULL) { 33547c478bd9Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(pp)); 33557c478bd9Sstevel@tonic-gate /* for now large pages should not end up here */ 33567c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 33577c478bd9Sstevel@tonic-gate /* 33587c478bd9Sstevel@tonic-gate * Save the locks for transfer to the new page and then 33597c478bd9Sstevel@tonic-gate * clear them so page_free doesn't think they're important. 33607c478bd9Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt and 33617c478bd9Sstevel@tonic-gate * cowcnt since the page has an "exclusive" lock. 33627c478bd9Sstevel@tonic-gate */ 33637c478bd9Sstevel@tonic-gate olckcnt = pp->p_lckcnt; 33647c478bd9Sstevel@tonic-gate ocowcnt = pp->p_cowcnt; 33657c478bd9Sstevel@tonic-gate pp->p_lckcnt = pp->p_cowcnt = 0; 33667c478bd9Sstevel@tonic-gate 33677c478bd9Sstevel@tonic-gate /* 33687c478bd9Sstevel@tonic-gate * Put the page on the "free" list after we drop 33697c478bd9Sstevel@tonic-gate * the lock. The less work under the lock the better. 33707c478bd9Sstevel@tonic-gate */ 33717c478bd9Sstevel@tonic-gate /*LINTED: constant in conditional context*/ 33727c478bd9Sstevel@tonic-gate VN_DISPOSE(pp, B_FREE, 0, kcred); 33737c478bd9Sstevel@tonic-gate } 33747c478bd9Sstevel@tonic-gate 33757c478bd9Sstevel@tonic-gate /* 33767c478bd9Sstevel@tonic-gate * Transfer the lock count from the old page (if any). 33777c478bd9Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt and 33787c478bd9Sstevel@tonic-gate * cowcnt since the page has an "exclusive" lock. 33797c478bd9Sstevel@tonic-gate */ 33807c478bd9Sstevel@tonic-gate opp->p_lckcnt += olckcnt; 33817c478bd9Sstevel@tonic-gate opp->p_cowcnt += ocowcnt; 33827c478bd9Sstevel@tonic-gate } 33837c478bd9Sstevel@tonic-gate 33847c478bd9Sstevel@tonic-gate /* 33857c478bd9Sstevel@tonic-gate * low level routine to add page `pp' to the hash and vp chains for [vp, offset] 33867c478bd9Sstevel@tonic-gate * 33877c478bd9Sstevel@tonic-gate * Pages are normally inserted at the start of a vnode's v_pages list. 33887c478bd9Sstevel@tonic-gate * If the vnode is VMODSORT and the page is modified, it goes at the end. 33897c478bd9Sstevel@tonic-gate * This can happen when a modified page is relocated for DR. 33907c478bd9Sstevel@tonic-gate * 33917c478bd9Sstevel@tonic-gate * Returns 1 on success and 0 on failure. 33927c478bd9Sstevel@tonic-gate */ 33937c478bd9Sstevel@tonic-gate static int 33947c478bd9Sstevel@tonic-gate page_do_hashin(page_t *pp, vnode_t *vp, u_offset_t offset) 33957c478bd9Sstevel@tonic-gate { 33967c478bd9Sstevel@tonic-gate page_t **listp; 33977c478bd9Sstevel@tonic-gate page_t *tp; 33987c478bd9Sstevel@tonic-gate ulong_t index; 33997c478bd9Sstevel@tonic-gate 34007c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 34017c478bd9Sstevel@tonic-gate ASSERT(vp != NULL); 34027c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 34037c478bd9Sstevel@tonic-gate 34047c478bd9Sstevel@tonic-gate /* 34057c478bd9Sstevel@tonic-gate * Be sure to set these up before the page is inserted on the hash 34067c478bd9Sstevel@tonic-gate * list. As soon as the page is placed on the list some other 34077c478bd9Sstevel@tonic-gate * thread might get confused and wonder how this page could 34087c478bd9Sstevel@tonic-gate * possibly hash to this list. 34097c478bd9Sstevel@tonic-gate */ 34107c478bd9Sstevel@tonic-gate pp->p_vnode = vp; 34117c478bd9Sstevel@tonic-gate pp->p_offset = offset; 34127c478bd9Sstevel@tonic-gate 34137c478bd9Sstevel@tonic-gate /* 34147c478bd9Sstevel@tonic-gate * record if this page is on a swap vnode 34157c478bd9Sstevel@tonic-gate */ 34167c478bd9Sstevel@tonic-gate if ((vp->v_flag & VISSWAP) != 0) 34177c478bd9Sstevel@tonic-gate PP_SETSWAP(pp); 34187c478bd9Sstevel@tonic-gate 34197c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, offset); 34207c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(PAGE_HASH_MUTEX(index))); 34217c478bd9Sstevel@tonic-gate listp = &page_hash[index]; 34227c478bd9Sstevel@tonic-gate 34237c478bd9Sstevel@tonic-gate /* 34247c478bd9Sstevel@tonic-gate * If this page is already hashed in, fail this attempt to add it. 34257c478bd9Sstevel@tonic-gate */ 34267c478bd9Sstevel@tonic-gate for (tp = *listp; tp != NULL; tp = tp->p_hash) { 34277c478bd9Sstevel@tonic-gate if (tp->p_vnode == vp && tp->p_offset == offset) { 34287c478bd9Sstevel@tonic-gate pp->p_vnode = NULL; 34297c478bd9Sstevel@tonic-gate pp->p_offset = (u_offset_t)(-1); 34307c478bd9Sstevel@tonic-gate return (0); 34317c478bd9Sstevel@tonic-gate } 34327c478bd9Sstevel@tonic-gate } 34337c478bd9Sstevel@tonic-gate pp->p_hash = *listp; 34347c478bd9Sstevel@tonic-gate *listp = pp; 34357c478bd9Sstevel@tonic-gate 34367c478bd9Sstevel@tonic-gate /* 34377c478bd9Sstevel@tonic-gate * Add the page to the vnode's list of pages 34387c478bd9Sstevel@tonic-gate */ 34397c478bd9Sstevel@tonic-gate if (vp->v_pages != NULL && IS_VMODSORT(vp) && hat_ismod(pp)) 34407c478bd9Sstevel@tonic-gate listp = &vp->v_pages->p_vpprev->p_vpnext; 34417c478bd9Sstevel@tonic-gate else 34427c478bd9Sstevel@tonic-gate listp = &vp->v_pages; 34437c478bd9Sstevel@tonic-gate 34447c478bd9Sstevel@tonic-gate page_vpadd(listp, pp); 34457c478bd9Sstevel@tonic-gate 34467c478bd9Sstevel@tonic-gate return (1); 34477c478bd9Sstevel@tonic-gate } 34487c478bd9Sstevel@tonic-gate 34497c478bd9Sstevel@tonic-gate /* 34507c478bd9Sstevel@tonic-gate * Add page `pp' to both the hash and vp chains for [vp, offset]. 34517c478bd9Sstevel@tonic-gate * 34527c478bd9Sstevel@tonic-gate * Returns 1 on success and 0 on failure. 34537c478bd9Sstevel@tonic-gate * If hold is passed in, it is not dropped. 34547c478bd9Sstevel@tonic-gate */ 34557c478bd9Sstevel@tonic-gate int 34567c478bd9Sstevel@tonic-gate page_hashin(page_t *pp, vnode_t *vp, u_offset_t offset, kmutex_t *hold) 34577c478bd9Sstevel@tonic-gate { 34587c478bd9Sstevel@tonic-gate kmutex_t *phm = NULL; 34597c478bd9Sstevel@tonic-gate kmutex_t *vphm; 34607c478bd9Sstevel@tonic-gate int rc; 34617c478bd9Sstevel@tonic-gate 34627c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp))); 3463c7531c7fSPrakash Sangappa ASSERT(pp->p_fsdata == 0 || panicstr); 34647c478bd9Sstevel@tonic-gate 34657c478bd9Sstevel@tonic-gate TRACE_3(TR_FAC_VM, TR_PAGE_HASHIN, 34666e4dd838Smec "page_hashin:pp %p vp %p offset %llx", 34676e4dd838Smec pp, vp, offset); 34687c478bd9Sstevel@tonic-gate 34697c478bd9Sstevel@tonic-gate VM_STAT_ADD(hashin_count); 34707c478bd9Sstevel@tonic-gate 34717c478bd9Sstevel@tonic-gate if (hold != NULL) 34727c478bd9Sstevel@tonic-gate phm = hold; 34737c478bd9Sstevel@tonic-gate else { 34747c478bd9Sstevel@tonic-gate VM_STAT_ADD(hashin_not_held); 34757c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, offset)); 34767c478bd9Sstevel@tonic-gate mutex_enter(phm); 34777c478bd9Sstevel@tonic-gate } 34787c478bd9Sstevel@tonic-gate 34797c478bd9Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 34807c478bd9Sstevel@tonic-gate mutex_enter(vphm); 34817c478bd9Sstevel@tonic-gate rc = page_do_hashin(pp, vp, offset); 34827c478bd9Sstevel@tonic-gate mutex_exit(vphm); 34837c478bd9Sstevel@tonic-gate if (hold == NULL) 34847c478bd9Sstevel@tonic-gate mutex_exit(phm); 34857c478bd9Sstevel@tonic-gate if (rc == 0) 34867c478bd9Sstevel@tonic-gate VM_STAT_ADD(hashin_already); 34877c478bd9Sstevel@tonic-gate return (rc); 34887c478bd9Sstevel@tonic-gate } 34897c478bd9Sstevel@tonic-gate 34907c478bd9Sstevel@tonic-gate /* 34917c478bd9Sstevel@tonic-gate * Remove page ``pp'' from the hash and vp chains and remove vp association. 34927c478bd9Sstevel@tonic-gate * All mutexes must be held 34937c478bd9Sstevel@tonic-gate */ 34947c478bd9Sstevel@tonic-gate static void 34957c478bd9Sstevel@tonic-gate page_do_hashout(page_t *pp) 34967c478bd9Sstevel@tonic-gate { 34977c478bd9Sstevel@tonic-gate page_t **hpp; 34987c478bd9Sstevel@tonic-gate page_t *hp; 34997c478bd9Sstevel@tonic-gate vnode_t *vp = pp->p_vnode; 35007c478bd9Sstevel@tonic-gate 35017c478bd9Sstevel@tonic-gate ASSERT(vp != NULL); 35027c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 35037c478bd9Sstevel@tonic-gate 35047c478bd9Sstevel@tonic-gate /* 35057c478bd9Sstevel@tonic-gate * First, take pp off of its hash chain. 35067c478bd9Sstevel@tonic-gate */ 35077c478bd9Sstevel@tonic-gate hpp = &page_hash[PAGE_HASH_FUNC(vp, pp->p_offset)]; 35087c478bd9Sstevel@tonic-gate 35097c478bd9Sstevel@tonic-gate for (;;) { 35107c478bd9Sstevel@tonic-gate hp = *hpp; 35117c478bd9Sstevel@tonic-gate if (hp == pp) 35127c478bd9Sstevel@tonic-gate break; 35137c478bd9Sstevel@tonic-gate if (hp == NULL) { 35147c478bd9Sstevel@tonic-gate panic("page_do_hashout"); 35157c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 35167c478bd9Sstevel@tonic-gate } 35177c478bd9Sstevel@tonic-gate hpp = &hp->p_hash; 35187c478bd9Sstevel@tonic-gate } 35197c478bd9Sstevel@tonic-gate *hpp = pp->p_hash; 35207c478bd9Sstevel@tonic-gate 35217c478bd9Sstevel@tonic-gate /* 35227c478bd9Sstevel@tonic-gate * Now remove it from its associated vnode. 35237c478bd9Sstevel@tonic-gate */ 35247c478bd9Sstevel@tonic-gate if (vp->v_pages) 35257c478bd9Sstevel@tonic-gate page_vpsub(&vp->v_pages, pp); 35267c478bd9Sstevel@tonic-gate 35277c478bd9Sstevel@tonic-gate pp->p_hash = NULL; 35282f0fcb93SJason Beloro page_clr_all_props(pp, 1); 35297c478bd9Sstevel@tonic-gate PP_CLRSWAP(pp); 35307c478bd9Sstevel@tonic-gate pp->p_vnode = NULL; 35317c478bd9Sstevel@tonic-gate pp->p_offset = (u_offset_t)-1; 3532c7531c7fSPrakash Sangappa pp->p_fsdata = 0; 35337c478bd9Sstevel@tonic-gate } 35347c478bd9Sstevel@tonic-gate 35357c478bd9Sstevel@tonic-gate /* 35367c478bd9Sstevel@tonic-gate * Remove page ``pp'' from the hash and vp chains and remove vp association. 35377c478bd9Sstevel@tonic-gate * 35387c478bd9Sstevel@tonic-gate * When `phm' is non-NULL it contains the address of the mutex protecting the 35397c478bd9Sstevel@tonic-gate * hash list pp is on. It is not dropped. 35407c478bd9Sstevel@tonic-gate */ 35417c478bd9Sstevel@tonic-gate void 35427c478bd9Sstevel@tonic-gate page_hashout(page_t *pp, kmutex_t *phm) 35437c478bd9Sstevel@tonic-gate { 35447c478bd9Sstevel@tonic-gate vnode_t *vp; 35457c478bd9Sstevel@tonic-gate ulong_t index; 35467c478bd9Sstevel@tonic-gate kmutex_t *nphm; 35477c478bd9Sstevel@tonic-gate kmutex_t *vphm; 35487c478bd9Sstevel@tonic-gate kmutex_t *sep; 35497c478bd9Sstevel@tonic-gate 35507c478bd9Sstevel@tonic-gate ASSERT(phm != NULL ? MUTEX_HELD(phm) : 1); 35517c478bd9Sstevel@tonic-gate ASSERT(pp->p_vnode != NULL); 35527c478bd9Sstevel@tonic-gate ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 35537c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(pp->p_vnode))); 35547c478bd9Sstevel@tonic-gate 35557c478bd9Sstevel@tonic-gate vp = pp->p_vnode; 35567c478bd9Sstevel@tonic-gate 35577c478bd9Sstevel@tonic-gate TRACE_2(TR_FAC_VM, TR_PAGE_HASHOUT, 35586e4dd838Smec "page_hashout:pp %p vp %p", pp, vp); 35597c478bd9Sstevel@tonic-gate 35607c478bd9Sstevel@tonic-gate /* Kernel probe */ 35617c478bd9Sstevel@tonic-gate TNF_PROBE_2(page_unmap, "vm pagefault", /* CSTYLED */, 35627c478bd9Sstevel@tonic-gate tnf_opaque, vnode, vp, 35637c478bd9Sstevel@tonic-gate tnf_offset, offset, pp->p_offset); 35647c478bd9Sstevel@tonic-gate 35657c478bd9Sstevel@tonic-gate /* 35667c478bd9Sstevel@tonic-gate * 35677c478bd9Sstevel@tonic-gate */ 35687c478bd9Sstevel@tonic-gate VM_STAT_ADD(hashout_count); 35697c478bd9Sstevel@tonic-gate index = PAGE_HASH_FUNC(vp, pp->p_offset); 35707c478bd9Sstevel@tonic-gate if (phm == NULL) { 35717c478bd9Sstevel@tonic-gate VM_STAT_ADD(hashout_not_held); 35727c478bd9Sstevel@tonic-gate nphm = PAGE_HASH_MUTEX(index); 35737c478bd9Sstevel@tonic-gate mutex_enter(nphm); 35747c478bd9Sstevel@tonic-gate } 35757c478bd9Sstevel@tonic-gate ASSERT(phm ? phm == PAGE_HASH_MUTEX(index) : 1); 35767c478bd9Sstevel@tonic-gate 35777c478bd9Sstevel@tonic-gate 35787c478bd9Sstevel@tonic-gate /* 35797c478bd9Sstevel@tonic-gate * grab page vnode mutex and remove it... 35807c478bd9Sstevel@tonic-gate */ 35817c478bd9Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 35827c478bd9Sstevel@tonic-gate mutex_enter(vphm); 35837c478bd9Sstevel@tonic-gate 35847c478bd9Sstevel@tonic-gate page_do_hashout(pp); 35857c478bd9Sstevel@tonic-gate 35867c478bd9Sstevel@tonic-gate mutex_exit(vphm); 35877c478bd9Sstevel@tonic-gate if (phm == NULL) 35887c478bd9Sstevel@tonic-gate mutex_exit(nphm); 35897c478bd9Sstevel@tonic-gate 35907c478bd9Sstevel@tonic-gate /* 35917c478bd9Sstevel@tonic-gate * Wake up processes waiting for this page. The page's 35927c478bd9Sstevel@tonic-gate * identity has been changed, and is probably not the 35937c478bd9Sstevel@tonic-gate * desired page any longer. 35947c478bd9Sstevel@tonic-gate */ 35957c478bd9Sstevel@tonic-gate sep = page_se_mutex(pp); 35967c478bd9Sstevel@tonic-gate mutex_enter(sep); 359742787a71Sstans pp->p_selock &= ~SE_EWANTED; 35987c478bd9Sstevel@tonic-gate if (CV_HAS_WAITERS(&pp->p_cv)) 35997c478bd9Sstevel@tonic-gate cv_broadcast(&pp->p_cv); 36007c478bd9Sstevel@tonic-gate mutex_exit(sep); 36017c478bd9Sstevel@tonic-gate } 36027c478bd9Sstevel@tonic-gate 36037c478bd9Sstevel@tonic-gate /* 36047c478bd9Sstevel@tonic-gate * Add the page to the front of a linked list of pages 36057c478bd9Sstevel@tonic-gate * using the p_next & p_prev pointers for the list. 36067c478bd9Sstevel@tonic-gate * The caller is responsible for protecting the list pointers. 36077c478bd9Sstevel@tonic-gate */ 36087c478bd9Sstevel@tonic-gate void 36097c478bd9Sstevel@tonic-gate page_add(page_t **ppp, page_t *pp) 36107c478bd9Sstevel@tonic-gate { 36117c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp) || (PAGE_SHARED(pp) && page_iolock_assert(pp))); 36127c478bd9Sstevel@tonic-gate 36137c478bd9Sstevel@tonic-gate page_add_common(ppp, pp); 36147c478bd9Sstevel@tonic-gate } 36157c478bd9Sstevel@tonic-gate 36167c478bd9Sstevel@tonic-gate 36177c478bd9Sstevel@tonic-gate 36187c478bd9Sstevel@tonic-gate /* 36197c478bd9Sstevel@tonic-gate * Common code for page_add() and mach_page_add() 36207c478bd9Sstevel@tonic-gate */ 36217c478bd9Sstevel@tonic-gate void 36227c478bd9Sstevel@tonic-gate page_add_common(page_t **ppp, page_t *pp) 36237c478bd9Sstevel@tonic-gate { 36247c478bd9Sstevel@tonic-gate if (*ppp == NULL) { 36257c478bd9Sstevel@tonic-gate pp->p_next = pp->p_prev = pp; 36267c478bd9Sstevel@tonic-gate } else { 36277c478bd9Sstevel@tonic-gate pp->p_next = *ppp; 36287c478bd9Sstevel@tonic-gate pp->p_prev = (*ppp)->p_prev; 36297c478bd9Sstevel@tonic-gate (*ppp)->p_prev = pp; 36307c478bd9Sstevel@tonic-gate pp->p_prev->p_next = pp; 36317c478bd9Sstevel@tonic-gate } 36327c478bd9Sstevel@tonic-gate *ppp = pp; 36337c478bd9Sstevel@tonic-gate } 36347c478bd9Sstevel@tonic-gate 36357c478bd9Sstevel@tonic-gate 36367c478bd9Sstevel@tonic-gate /* 36377c478bd9Sstevel@tonic-gate * Remove this page from a linked list of pages 36387c478bd9Sstevel@tonic-gate * using the p_next & p_prev pointers for the list. 36397c478bd9Sstevel@tonic-gate * 36407c478bd9Sstevel@tonic-gate * The caller is responsible for protecting the list pointers. 36417c478bd9Sstevel@tonic-gate */ 36427c478bd9Sstevel@tonic-gate void 36437c478bd9Sstevel@tonic-gate page_sub(page_t **ppp, page_t *pp) 36447c478bd9Sstevel@tonic-gate { 36457c478bd9Sstevel@tonic-gate ASSERT((PP_ISFREE(pp)) ? 1 : 36467c478bd9Sstevel@tonic-gate (PAGE_EXCL(pp)) || (PAGE_SHARED(pp) && page_iolock_assert(pp))); 36477c478bd9Sstevel@tonic-gate 36487c478bd9Sstevel@tonic-gate if (*ppp == NULL || pp == NULL) { 36497c478bd9Sstevel@tonic-gate panic("page_sub: bad arg(s): pp %p, *ppp %p", 36507c478bd9Sstevel@tonic-gate (void *)pp, (void *)(*ppp)); 36517c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 36527c478bd9Sstevel@tonic-gate } 36537c478bd9Sstevel@tonic-gate 36547c478bd9Sstevel@tonic-gate page_sub_common(ppp, pp); 36557c478bd9Sstevel@tonic-gate } 36567c478bd9Sstevel@tonic-gate 36577c478bd9Sstevel@tonic-gate 36587c478bd9Sstevel@tonic-gate /* 36597c478bd9Sstevel@tonic-gate * Common code for page_sub() and mach_page_sub() 36607c478bd9Sstevel@tonic-gate */ 36617c478bd9Sstevel@tonic-gate void 36627c478bd9Sstevel@tonic-gate page_sub_common(page_t **ppp, page_t *pp) 36637c478bd9Sstevel@tonic-gate { 36647c478bd9Sstevel@tonic-gate if (*ppp == pp) 36657c478bd9Sstevel@tonic-gate *ppp = pp->p_next; /* go to next page */ 36667c478bd9Sstevel@tonic-gate 36677c478bd9Sstevel@tonic-gate if (*ppp == pp) 36687c478bd9Sstevel@tonic-gate *ppp = NULL; /* page list is gone */ 36697c478bd9Sstevel@tonic-gate else { 36707c478bd9Sstevel@tonic-gate pp->p_prev->p_next = pp->p_next; 36717c478bd9Sstevel@tonic-gate pp->p_next->p_prev = pp->p_prev; 36727c478bd9Sstevel@tonic-gate } 36737c478bd9Sstevel@tonic-gate pp->p_prev = pp->p_next = pp; /* make pp a list of one */ 36747c478bd9Sstevel@tonic-gate } 36757c478bd9Sstevel@tonic-gate 36767c478bd9Sstevel@tonic-gate 36777c478bd9Sstevel@tonic-gate /* 36787c478bd9Sstevel@tonic-gate * Break page list cppp into two lists with npages in the first list. 36797c478bd9Sstevel@tonic-gate * The tail is returned in nppp. 36807c478bd9Sstevel@tonic-gate */ 36817c478bd9Sstevel@tonic-gate void 36827c478bd9Sstevel@tonic-gate page_list_break(page_t **oppp, page_t **nppp, pgcnt_t npages) 36837c478bd9Sstevel@tonic-gate { 36847c478bd9Sstevel@tonic-gate page_t *s1pp = *oppp; 36857c478bd9Sstevel@tonic-gate page_t *s2pp; 36867c478bd9Sstevel@tonic-gate page_t *e1pp, *e2pp; 36877c478bd9Sstevel@tonic-gate long n = 0; 36887c478bd9Sstevel@tonic-gate 36897c478bd9Sstevel@tonic-gate if (s1pp == NULL) { 36907c478bd9Sstevel@tonic-gate *nppp = NULL; 36917c478bd9Sstevel@tonic-gate return; 36927c478bd9Sstevel@tonic-gate } 36937c478bd9Sstevel@tonic-gate if (npages == 0) { 36947c478bd9Sstevel@tonic-gate *nppp = s1pp; 36957c478bd9Sstevel@tonic-gate *oppp = NULL; 36967c478bd9Sstevel@tonic-gate return; 36977c478bd9Sstevel@tonic-gate } 36987c478bd9Sstevel@tonic-gate for (n = 0, s2pp = *oppp; n < npages; n++) { 36997c478bd9Sstevel@tonic-gate s2pp = s2pp->p_next; 37007c478bd9Sstevel@tonic-gate } 37017c478bd9Sstevel@tonic-gate /* Fix head and tail of new lists */ 37027c478bd9Sstevel@tonic-gate e1pp = s2pp->p_prev; 37037c478bd9Sstevel@tonic-gate e2pp = s1pp->p_prev; 37047c478bd9Sstevel@tonic-gate s1pp->p_prev = e1pp; 37057c478bd9Sstevel@tonic-gate e1pp->p_next = s1pp; 37067c478bd9Sstevel@tonic-gate s2pp->p_prev = e2pp; 37077c478bd9Sstevel@tonic-gate e2pp->p_next = s2pp; 37087c478bd9Sstevel@tonic-gate 37097c478bd9Sstevel@tonic-gate /* second list empty */ 37107c478bd9Sstevel@tonic-gate if (s2pp == s1pp) { 37117c478bd9Sstevel@tonic-gate *oppp = s1pp; 37127c478bd9Sstevel@tonic-gate *nppp = NULL; 37137c478bd9Sstevel@tonic-gate } else { 37147c478bd9Sstevel@tonic-gate *oppp = s1pp; 37157c478bd9Sstevel@tonic-gate *nppp = s2pp; 37167c478bd9Sstevel@tonic-gate } 37177c478bd9Sstevel@tonic-gate } 37187c478bd9Sstevel@tonic-gate 37197c478bd9Sstevel@tonic-gate /* 37207c478bd9Sstevel@tonic-gate * Concatenate page list nppp onto the end of list ppp. 37217c478bd9Sstevel@tonic-gate */ 37227c478bd9Sstevel@tonic-gate void 37237c478bd9Sstevel@tonic-gate page_list_concat(page_t **ppp, page_t **nppp) 37247c478bd9Sstevel@tonic-gate { 37257c478bd9Sstevel@tonic-gate page_t *s1pp, *s2pp, *e1pp, *e2pp; 37267c478bd9Sstevel@tonic-gate 37277c478bd9Sstevel@tonic-gate if (*nppp == NULL) { 37287c478bd9Sstevel@tonic-gate return; 37297c478bd9Sstevel@tonic-gate } 37307c478bd9Sstevel@tonic-gate if (*ppp == NULL) { 37317c478bd9Sstevel@tonic-gate *ppp = *nppp; 37327c478bd9Sstevel@tonic-gate return; 37337c478bd9Sstevel@tonic-gate } 37347c478bd9Sstevel@tonic-gate s1pp = *ppp; 37357c478bd9Sstevel@tonic-gate e1pp = s1pp->p_prev; 37367c478bd9Sstevel@tonic-gate s2pp = *nppp; 37377c478bd9Sstevel@tonic-gate e2pp = s2pp->p_prev; 37387c478bd9Sstevel@tonic-gate s1pp->p_prev = e2pp; 37397c478bd9Sstevel@tonic-gate e2pp->p_next = s1pp; 37407c478bd9Sstevel@tonic-gate e1pp->p_next = s2pp; 37417c478bd9Sstevel@tonic-gate s2pp->p_prev = e1pp; 37427c478bd9Sstevel@tonic-gate } 37437c478bd9Sstevel@tonic-gate 37447c478bd9Sstevel@tonic-gate /* 37457c478bd9Sstevel@tonic-gate * return the next page in the page list 37467c478bd9Sstevel@tonic-gate */ 37477c478bd9Sstevel@tonic-gate page_t * 37487c478bd9Sstevel@tonic-gate page_list_next(page_t *pp) 37497c478bd9Sstevel@tonic-gate { 37507c478bd9Sstevel@tonic-gate return (pp->p_next); 37517c478bd9Sstevel@tonic-gate } 37527c478bd9Sstevel@tonic-gate 37537c478bd9Sstevel@tonic-gate 37547c478bd9Sstevel@tonic-gate /* 37557c478bd9Sstevel@tonic-gate * Add the page to the front of the linked list of pages 37567c478bd9Sstevel@tonic-gate * using p_vpnext/p_vpprev pointers for the list. 37577c478bd9Sstevel@tonic-gate * 37587c478bd9Sstevel@tonic-gate * The caller is responsible for protecting the lists. 37597c478bd9Sstevel@tonic-gate */ 37607c478bd9Sstevel@tonic-gate void 37617c478bd9Sstevel@tonic-gate page_vpadd(page_t **ppp, page_t *pp) 37627c478bd9Sstevel@tonic-gate { 37637c478bd9Sstevel@tonic-gate if (*ppp == NULL) { 37647c478bd9Sstevel@tonic-gate pp->p_vpnext = pp->p_vpprev = pp; 37657c478bd9Sstevel@tonic-gate } else { 37667c478bd9Sstevel@tonic-gate pp->p_vpnext = *ppp; 37677c478bd9Sstevel@tonic-gate pp->p_vpprev = (*ppp)->p_vpprev; 37687c478bd9Sstevel@tonic-gate (*ppp)->p_vpprev = pp; 37697c478bd9Sstevel@tonic-gate pp->p_vpprev->p_vpnext = pp; 37707c478bd9Sstevel@tonic-gate } 37717c478bd9Sstevel@tonic-gate *ppp = pp; 37727c478bd9Sstevel@tonic-gate } 37737c478bd9Sstevel@tonic-gate 37747c478bd9Sstevel@tonic-gate /* 37757c478bd9Sstevel@tonic-gate * Remove this page from the linked list of pages 37767c478bd9Sstevel@tonic-gate * using p_vpnext/p_vpprev pointers for the list. 37777c478bd9Sstevel@tonic-gate * 37787c478bd9Sstevel@tonic-gate * The caller is responsible for protecting the lists. 37797c478bd9Sstevel@tonic-gate */ 37807c478bd9Sstevel@tonic-gate void 37817c478bd9Sstevel@tonic-gate page_vpsub(page_t **ppp, page_t *pp) 37827c478bd9Sstevel@tonic-gate { 37837c478bd9Sstevel@tonic-gate if (*ppp == NULL || pp == NULL) { 37847c478bd9Sstevel@tonic-gate panic("page_vpsub: bad arg(s): pp %p, *ppp %p", 37857c478bd9Sstevel@tonic-gate (void *)pp, (void *)(*ppp)); 37867c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 37877c478bd9Sstevel@tonic-gate } 37887c478bd9Sstevel@tonic-gate 37897c478bd9Sstevel@tonic-gate if (*ppp == pp) 37907c478bd9Sstevel@tonic-gate *ppp = pp->p_vpnext; /* go to next page */ 37917c478bd9Sstevel@tonic-gate 37927c478bd9Sstevel@tonic-gate if (*ppp == pp) 37937c478bd9Sstevel@tonic-gate *ppp = NULL; /* page list is gone */ 37947c478bd9Sstevel@tonic-gate else { 37957c478bd9Sstevel@tonic-gate pp->p_vpprev->p_vpnext = pp->p_vpnext; 37967c478bd9Sstevel@tonic-gate pp->p_vpnext->p_vpprev = pp->p_vpprev; 37977c478bd9Sstevel@tonic-gate } 37987c478bd9Sstevel@tonic-gate pp->p_vpprev = pp->p_vpnext = pp; /* make pp a list of one */ 37997c478bd9Sstevel@tonic-gate } 38007c478bd9Sstevel@tonic-gate 38017c478bd9Sstevel@tonic-gate /* 38027c478bd9Sstevel@tonic-gate * Lock a physical page into memory "long term". Used to support "lock 38037c478bd9Sstevel@tonic-gate * in memory" functions. Accepts the page to be locked, and a cow variable 38047c478bd9Sstevel@tonic-gate * to indicate whether a the lock will travel to the new page during 38057c478bd9Sstevel@tonic-gate * a potential copy-on-write. 38067c478bd9Sstevel@tonic-gate */ 38077c478bd9Sstevel@tonic-gate int 38087c478bd9Sstevel@tonic-gate page_pp_lock( 38097c478bd9Sstevel@tonic-gate page_t *pp, /* page to be locked */ 38107c478bd9Sstevel@tonic-gate int cow, /* cow lock */ 38117c478bd9Sstevel@tonic-gate int kernel) /* must succeed -- ignore checking */ 38127c478bd9Sstevel@tonic-gate { 38137c478bd9Sstevel@tonic-gate int r = 0; /* result -- assume failure */ 38147c478bd9Sstevel@tonic-gate 38157c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 38167c478bd9Sstevel@tonic-gate 38177c478bd9Sstevel@tonic-gate page_struct_lock(pp); 38187c478bd9Sstevel@tonic-gate /* 38197c478bd9Sstevel@tonic-gate * Acquire the "freemem_lock" for availrmem. 38207c478bd9Sstevel@tonic-gate */ 38217c478bd9Sstevel@tonic-gate if (cow) { 38227c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 38237c478bd9Sstevel@tonic-gate if ((availrmem > pages_pp_maximum) && 38247c478bd9Sstevel@tonic-gate (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM)) { 38257c478bd9Sstevel@tonic-gate availrmem--; 38267c478bd9Sstevel@tonic-gate pages_locked++; 38277c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 38287c478bd9Sstevel@tonic-gate r = 1; 38297c478bd9Sstevel@tonic-gate if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 38307c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 38317c478bd9Sstevel@tonic-gate "COW lock limit reached on pfn 0x%lx", 38327c478bd9Sstevel@tonic-gate page_pptonum(pp)); 38337c478bd9Sstevel@tonic-gate } 38347c478bd9Sstevel@tonic-gate } else 38357c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 38367c478bd9Sstevel@tonic-gate } else { 38377c478bd9Sstevel@tonic-gate if (pp->p_lckcnt) { 38387c478bd9Sstevel@tonic-gate if (pp->p_lckcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 38397c478bd9Sstevel@tonic-gate r = 1; 38407c478bd9Sstevel@tonic-gate if (++pp->p_lckcnt == 38417c478bd9Sstevel@tonic-gate (ushort_t)PAGE_LOCK_MAXIMUM) { 38427c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "Page lock limit " 38437c478bd9Sstevel@tonic-gate "reached on pfn 0x%lx", 38447c478bd9Sstevel@tonic-gate page_pptonum(pp)); 38457c478bd9Sstevel@tonic-gate } 38467c478bd9Sstevel@tonic-gate } 38477c478bd9Sstevel@tonic-gate } else { 38487c478bd9Sstevel@tonic-gate if (kernel) { 38497c478bd9Sstevel@tonic-gate /* availrmem accounting done by caller */ 38507c478bd9Sstevel@tonic-gate ++pp->p_lckcnt; 38517c478bd9Sstevel@tonic-gate r = 1; 38527c478bd9Sstevel@tonic-gate } else { 38537c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 38547c478bd9Sstevel@tonic-gate if (availrmem > pages_pp_maximum) { 38557c478bd9Sstevel@tonic-gate availrmem--; 38567c478bd9Sstevel@tonic-gate pages_locked++; 38577c478bd9Sstevel@tonic-gate ++pp->p_lckcnt; 38587c478bd9Sstevel@tonic-gate r = 1; 38597c478bd9Sstevel@tonic-gate } 38607c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 38617c478bd9Sstevel@tonic-gate } 38627c478bd9Sstevel@tonic-gate } 38637c478bd9Sstevel@tonic-gate } 38647c478bd9Sstevel@tonic-gate page_struct_unlock(pp); 38657c478bd9Sstevel@tonic-gate return (r); 38667c478bd9Sstevel@tonic-gate } 38677c478bd9Sstevel@tonic-gate 38687c478bd9Sstevel@tonic-gate /* 38697c478bd9Sstevel@tonic-gate * Decommit a lock on a physical page frame. Account for cow locks if 38707c478bd9Sstevel@tonic-gate * appropriate. 38717c478bd9Sstevel@tonic-gate */ 38727c478bd9Sstevel@tonic-gate void 38737c478bd9Sstevel@tonic-gate page_pp_unlock( 38747c478bd9Sstevel@tonic-gate page_t *pp, /* page to be unlocked */ 38757c478bd9Sstevel@tonic-gate int cow, /* expect cow lock */ 38767c478bd9Sstevel@tonic-gate int kernel) /* this was a kernel lock */ 38777c478bd9Sstevel@tonic-gate { 38787c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 38797c478bd9Sstevel@tonic-gate 38807c478bd9Sstevel@tonic-gate page_struct_lock(pp); 38817c478bd9Sstevel@tonic-gate /* 38827c478bd9Sstevel@tonic-gate * Acquire the "freemem_lock" for availrmem. 38837c478bd9Sstevel@tonic-gate * If cowcnt or lcknt is already 0 do nothing; i.e., we 38847c478bd9Sstevel@tonic-gate * could be called to unlock even if nothing is locked. This could 38857c478bd9Sstevel@tonic-gate * happen if locked file pages were truncated (removing the lock) 38867c478bd9Sstevel@tonic-gate * and the file was grown again and new pages faulted in; the new 38877c478bd9Sstevel@tonic-gate * pages are unlocked but the segment still thinks they're locked. 38887c478bd9Sstevel@tonic-gate */ 38897c478bd9Sstevel@tonic-gate if (cow) { 38907c478bd9Sstevel@tonic-gate if (pp->p_cowcnt) { 38917c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 38927c478bd9Sstevel@tonic-gate pp->p_cowcnt--; 38937c478bd9Sstevel@tonic-gate availrmem++; 38947c478bd9Sstevel@tonic-gate pages_locked--; 38957c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 38967c478bd9Sstevel@tonic-gate } 38977c478bd9Sstevel@tonic-gate } else { 38987c478bd9Sstevel@tonic-gate if (pp->p_lckcnt && --pp->p_lckcnt == 0) { 38997c478bd9Sstevel@tonic-gate if (!kernel) { 39007c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 39017c478bd9Sstevel@tonic-gate availrmem++; 39027c478bd9Sstevel@tonic-gate pages_locked--; 39037c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 39047c478bd9Sstevel@tonic-gate } 39057c478bd9Sstevel@tonic-gate } 39067c478bd9Sstevel@tonic-gate } 39077c478bd9Sstevel@tonic-gate page_struct_unlock(pp); 39087c478bd9Sstevel@tonic-gate } 39097c478bd9Sstevel@tonic-gate 39107c478bd9Sstevel@tonic-gate /* 39117c478bd9Sstevel@tonic-gate * This routine reserves availrmem for npages; 39127c478bd9Sstevel@tonic-gate * flags: KM_NOSLEEP or KM_SLEEP 39137c478bd9Sstevel@tonic-gate * returns 1 on success or 0 on failure 39147c478bd9Sstevel@tonic-gate */ 39157c478bd9Sstevel@tonic-gate int 39167c478bd9Sstevel@tonic-gate page_resv(pgcnt_t npages, uint_t flags) 39177c478bd9Sstevel@tonic-gate { 39187c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 39197c478bd9Sstevel@tonic-gate while (availrmem < tune.t_minarmem + npages) { 39207c478bd9Sstevel@tonic-gate if (flags & KM_NOSLEEP) { 39217c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 39227c478bd9Sstevel@tonic-gate return (0); 39237c478bd9Sstevel@tonic-gate } 39247c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 39257c478bd9Sstevel@tonic-gate page_needfree(npages); 39267c478bd9Sstevel@tonic-gate kmem_reap(); 39277c478bd9Sstevel@tonic-gate delay(hz >> 2); 39287c478bd9Sstevel@tonic-gate page_needfree(-(spgcnt_t)npages); 39297c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 39307c478bd9Sstevel@tonic-gate } 39317c478bd9Sstevel@tonic-gate availrmem -= npages; 39327c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 39337c478bd9Sstevel@tonic-gate return (1); 39347c478bd9Sstevel@tonic-gate } 39357c478bd9Sstevel@tonic-gate 39367c478bd9Sstevel@tonic-gate /* 39377c478bd9Sstevel@tonic-gate * This routine unreserves availrmem for npages; 39387c478bd9Sstevel@tonic-gate */ 39397c478bd9Sstevel@tonic-gate void 39407c478bd9Sstevel@tonic-gate page_unresv(pgcnt_t npages) 39417c478bd9Sstevel@tonic-gate { 39427c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 39437c478bd9Sstevel@tonic-gate availrmem += npages; 39447c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 39457c478bd9Sstevel@tonic-gate } 39467c478bd9Sstevel@tonic-gate 39477c478bd9Sstevel@tonic-gate /* 39487c478bd9Sstevel@tonic-gate * See Statement at the beginning of segvn_lockop() regarding 39497c478bd9Sstevel@tonic-gate * the way we handle cowcnts and lckcnts. 39507c478bd9Sstevel@tonic-gate * 39517c478bd9Sstevel@tonic-gate * Transfer cowcnt on 'opp' to cowcnt on 'npp' if the vpage 39527c478bd9Sstevel@tonic-gate * that breaks COW has PROT_WRITE. 39537c478bd9Sstevel@tonic-gate * 39547c478bd9Sstevel@tonic-gate * Note that, we may also break COW in case we are softlocking 39557c478bd9Sstevel@tonic-gate * on read access during physio; 39567c478bd9Sstevel@tonic-gate * in this softlock case, the vpage may not have PROT_WRITE. 39577c478bd9Sstevel@tonic-gate * So, we need to transfer lckcnt on 'opp' to lckcnt on 'npp' 39587c478bd9Sstevel@tonic-gate * if the vpage doesn't have PROT_WRITE. 39597c478bd9Sstevel@tonic-gate * 39607c478bd9Sstevel@tonic-gate * This routine is never called if we are stealing a page 39617c478bd9Sstevel@tonic-gate * in anon_private. 39627c478bd9Sstevel@tonic-gate * 39637c478bd9Sstevel@tonic-gate * The caller subtracted from availrmem for read only mapping. 39647c478bd9Sstevel@tonic-gate * if lckcnt is 1 increment availrmem. 39657c478bd9Sstevel@tonic-gate */ 39667c478bd9Sstevel@tonic-gate void 39677c478bd9Sstevel@tonic-gate page_pp_useclaim( 39687c478bd9Sstevel@tonic-gate page_t *opp, /* original page frame losing lock */ 39697c478bd9Sstevel@tonic-gate page_t *npp, /* new page frame gaining lock */ 39707c478bd9Sstevel@tonic-gate uint_t write_perm) /* set if vpage has PROT_WRITE */ 39717c478bd9Sstevel@tonic-gate { 39727c478bd9Sstevel@tonic-gate int payback = 0; 39737c478bd9Sstevel@tonic-gate 39747c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(opp)); 39757c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(npp)); 39767c478bd9Sstevel@tonic-gate 39777c478bd9Sstevel@tonic-gate page_struct_lock(opp); 39787c478bd9Sstevel@tonic-gate 39797c478bd9Sstevel@tonic-gate ASSERT(npp->p_cowcnt == 0); 39807c478bd9Sstevel@tonic-gate ASSERT(npp->p_lckcnt == 0); 39817c478bd9Sstevel@tonic-gate 39827c478bd9Sstevel@tonic-gate /* Don't use claim if nothing is locked (see page_pp_unlock above) */ 39837c478bd9Sstevel@tonic-gate if ((write_perm && opp->p_cowcnt != 0) || 39847c478bd9Sstevel@tonic-gate (!write_perm && opp->p_lckcnt != 0)) { 39857c478bd9Sstevel@tonic-gate 39867c478bd9Sstevel@tonic-gate if (write_perm) { 39877c478bd9Sstevel@tonic-gate npp->p_cowcnt++; 39887c478bd9Sstevel@tonic-gate ASSERT(opp->p_cowcnt != 0); 39897c478bd9Sstevel@tonic-gate opp->p_cowcnt--; 39907c478bd9Sstevel@tonic-gate } else { 39917c478bd9Sstevel@tonic-gate 39927c478bd9Sstevel@tonic-gate ASSERT(opp->p_lckcnt != 0); 39937c478bd9Sstevel@tonic-gate 39947c478bd9Sstevel@tonic-gate /* 39957c478bd9Sstevel@tonic-gate * We didn't need availrmem decremented if p_lckcnt on 39967c478bd9Sstevel@tonic-gate * original page is 1. Here, we are unlocking 39977c478bd9Sstevel@tonic-gate * read-only copy belonging to original page and 39987c478bd9Sstevel@tonic-gate * are locking a copy belonging to new page. 39997c478bd9Sstevel@tonic-gate */ 40007c478bd9Sstevel@tonic-gate if (opp->p_lckcnt == 1) 40017c478bd9Sstevel@tonic-gate payback = 1; 40027c478bd9Sstevel@tonic-gate 40037c478bd9Sstevel@tonic-gate npp->p_lckcnt++; 40047c478bd9Sstevel@tonic-gate opp->p_lckcnt--; 40057c478bd9Sstevel@tonic-gate } 40067c478bd9Sstevel@tonic-gate } 40077c478bd9Sstevel@tonic-gate if (payback) { 40087c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 40097c478bd9Sstevel@tonic-gate availrmem++; 40107c478bd9Sstevel@tonic-gate pages_useclaim--; 40117c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 40127c478bd9Sstevel@tonic-gate } 40137c478bd9Sstevel@tonic-gate page_struct_unlock(opp); 40147c478bd9Sstevel@tonic-gate } 40157c478bd9Sstevel@tonic-gate 40167c478bd9Sstevel@tonic-gate /* 40177c478bd9Sstevel@tonic-gate * Simple claim adjust functions -- used to support changes in 40187c478bd9Sstevel@tonic-gate * claims due to changes in access permissions. Used by segvn_setprot(). 40197c478bd9Sstevel@tonic-gate */ 40207c478bd9Sstevel@tonic-gate int 40217c478bd9Sstevel@tonic-gate page_addclaim(page_t *pp) 40227c478bd9Sstevel@tonic-gate { 40237c478bd9Sstevel@tonic-gate int r = 0; /* result */ 40247c478bd9Sstevel@tonic-gate 40257c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 40267c478bd9Sstevel@tonic-gate 40277c478bd9Sstevel@tonic-gate page_struct_lock(pp); 40287c478bd9Sstevel@tonic-gate ASSERT(pp->p_lckcnt != 0); 40297c478bd9Sstevel@tonic-gate 40307c478bd9Sstevel@tonic-gate if (pp->p_lckcnt == 1) { 40317c478bd9Sstevel@tonic-gate if (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 40327c478bd9Sstevel@tonic-gate --pp->p_lckcnt; 40337c478bd9Sstevel@tonic-gate r = 1; 40347c478bd9Sstevel@tonic-gate if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 40357c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 40367c478bd9Sstevel@tonic-gate "COW lock limit reached on pfn 0x%lx", 40377c478bd9Sstevel@tonic-gate page_pptonum(pp)); 40387c478bd9Sstevel@tonic-gate } 40397c478bd9Sstevel@tonic-gate } 40407c478bd9Sstevel@tonic-gate } else { 40417c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 40427c478bd9Sstevel@tonic-gate if ((availrmem > pages_pp_maximum) && 40437c478bd9Sstevel@tonic-gate (pp->p_cowcnt < (ushort_t)PAGE_LOCK_MAXIMUM)) { 40447c478bd9Sstevel@tonic-gate --availrmem; 40457c478bd9Sstevel@tonic-gate ++pages_claimed; 40467c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 40477c478bd9Sstevel@tonic-gate --pp->p_lckcnt; 40487c478bd9Sstevel@tonic-gate r = 1; 40497c478bd9Sstevel@tonic-gate if (++pp->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 40507c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 40517c478bd9Sstevel@tonic-gate "COW lock limit reached on pfn 0x%lx", 40527c478bd9Sstevel@tonic-gate page_pptonum(pp)); 40537c478bd9Sstevel@tonic-gate } 40547c478bd9Sstevel@tonic-gate } else 40557c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 40567c478bd9Sstevel@tonic-gate } 40577c478bd9Sstevel@tonic-gate page_struct_unlock(pp); 40587c478bd9Sstevel@tonic-gate return (r); 40597c478bd9Sstevel@tonic-gate } 40607c478bd9Sstevel@tonic-gate 40617c478bd9Sstevel@tonic-gate int 40627c478bd9Sstevel@tonic-gate page_subclaim(page_t *pp) 40637c478bd9Sstevel@tonic-gate { 40647c478bd9Sstevel@tonic-gate int r = 0; 40657c478bd9Sstevel@tonic-gate 40667c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 40677c478bd9Sstevel@tonic-gate 40687c478bd9Sstevel@tonic-gate page_struct_lock(pp); 40697c478bd9Sstevel@tonic-gate ASSERT(pp->p_cowcnt != 0); 40707c478bd9Sstevel@tonic-gate 40717c478bd9Sstevel@tonic-gate if (pp->p_lckcnt) { 40727c478bd9Sstevel@tonic-gate if (pp->p_lckcnt < (ushort_t)PAGE_LOCK_MAXIMUM) { 40737c478bd9Sstevel@tonic-gate r = 1; 40747c478bd9Sstevel@tonic-gate /* 40757c478bd9Sstevel@tonic-gate * for availrmem 40767c478bd9Sstevel@tonic-gate */ 40777c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 40787c478bd9Sstevel@tonic-gate availrmem++; 40797c478bd9Sstevel@tonic-gate pages_claimed--; 40807c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 40817c478bd9Sstevel@tonic-gate 40827c478bd9Sstevel@tonic-gate pp->p_cowcnt--; 40837c478bd9Sstevel@tonic-gate 40847c478bd9Sstevel@tonic-gate if (++pp->p_lckcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 40857c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 40867c478bd9Sstevel@tonic-gate "Page lock limit reached on pfn 0x%lx", 40877c478bd9Sstevel@tonic-gate page_pptonum(pp)); 40887c478bd9Sstevel@tonic-gate } 40897c478bd9Sstevel@tonic-gate } 40907c478bd9Sstevel@tonic-gate } else { 40917c478bd9Sstevel@tonic-gate r = 1; 40927c478bd9Sstevel@tonic-gate pp->p_cowcnt--; 40937c478bd9Sstevel@tonic-gate pp->p_lckcnt++; 40947c478bd9Sstevel@tonic-gate } 40957c478bd9Sstevel@tonic-gate page_struct_unlock(pp); 40967c478bd9Sstevel@tonic-gate return (r); 40977c478bd9Sstevel@tonic-gate } 40987c478bd9Sstevel@tonic-gate 40997c478bd9Sstevel@tonic-gate int 41007c478bd9Sstevel@tonic-gate page_addclaim_pages(page_t **ppa) 41017c478bd9Sstevel@tonic-gate { 41027c478bd9Sstevel@tonic-gate 41037c478bd9Sstevel@tonic-gate pgcnt_t lckpgs = 0, pg_idx; 41047c478bd9Sstevel@tonic-gate 41057c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_addclaim_pages); 41067c478bd9Sstevel@tonic-gate 41077c478bd9Sstevel@tonic-gate mutex_enter(&page_llock); 41087c478bd9Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 41097c478bd9Sstevel@tonic-gate 41107c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(ppa[pg_idx])); 41117c478bd9Sstevel@tonic-gate ASSERT(ppa[pg_idx]->p_lckcnt != 0); 41127c478bd9Sstevel@tonic-gate if (ppa[pg_idx]->p_cowcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 41137c478bd9Sstevel@tonic-gate mutex_exit(&page_llock); 41147c478bd9Sstevel@tonic-gate return (0); 41157c478bd9Sstevel@tonic-gate } 41167c478bd9Sstevel@tonic-gate if (ppa[pg_idx]->p_lckcnt > 1) 41177c478bd9Sstevel@tonic-gate lckpgs++; 41187c478bd9Sstevel@tonic-gate } 41197c478bd9Sstevel@tonic-gate 41207c478bd9Sstevel@tonic-gate if (lckpgs != 0) { 41217c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 41227c478bd9Sstevel@tonic-gate if (availrmem >= pages_pp_maximum + lckpgs) { 41237c478bd9Sstevel@tonic-gate availrmem -= lckpgs; 41247c478bd9Sstevel@tonic-gate pages_claimed += lckpgs; 41257c478bd9Sstevel@tonic-gate } else { 41267c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 41277c478bd9Sstevel@tonic-gate mutex_exit(&page_llock); 41287c478bd9Sstevel@tonic-gate return (0); 41297c478bd9Sstevel@tonic-gate } 41307c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 41317c478bd9Sstevel@tonic-gate } 41327c478bd9Sstevel@tonic-gate 41337c478bd9Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 41347c478bd9Sstevel@tonic-gate ppa[pg_idx]->p_lckcnt--; 41357c478bd9Sstevel@tonic-gate ppa[pg_idx]->p_cowcnt++; 41367c478bd9Sstevel@tonic-gate } 41377c478bd9Sstevel@tonic-gate mutex_exit(&page_llock); 41387c478bd9Sstevel@tonic-gate return (1); 41397c478bd9Sstevel@tonic-gate } 41407c478bd9Sstevel@tonic-gate 41417c478bd9Sstevel@tonic-gate int 41427c478bd9Sstevel@tonic-gate page_subclaim_pages(page_t **ppa) 41437c478bd9Sstevel@tonic-gate { 41447c478bd9Sstevel@tonic-gate pgcnt_t ulckpgs = 0, pg_idx; 41457c478bd9Sstevel@tonic-gate 41467c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_subclaim_pages); 41477c478bd9Sstevel@tonic-gate 41487c478bd9Sstevel@tonic-gate mutex_enter(&page_llock); 41497c478bd9Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 41507c478bd9Sstevel@tonic-gate 41517c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(ppa[pg_idx])); 41527c478bd9Sstevel@tonic-gate ASSERT(ppa[pg_idx]->p_cowcnt != 0); 41537c478bd9Sstevel@tonic-gate if (ppa[pg_idx]->p_lckcnt == (ushort_t)PAGE_LOCK_MAXIMUM) { 41547c478bd9Sstevel@tonic-gate mutex_exit(&page_llock); 41557c478bd9Sstevel@tonic-gate return (0); 41567c478bd9Sstevel@tonic-gate } 41577c478bd9Sstevel@tonic-gate if (ppa[pg_idx]->p_lckcnt != 0) 41587c478bd9Sstevel@tonic-gate ulckpgs++; 41597c478bd9Sstevel@tonic-gate } 41607c478bd9Sstevel@tonic-gate 41617c478bd9Sstevel@tonic-gate if (ulckpgs != 0) { 41627c478bd9Sstevel@tonic-gate mutex_enter(&freemem_lock); 41637c478bd9Sstevel@tonic-gate availrmem += ulckpgs; 41647c478bd9Sstevel@tonic-gate pages_claimed -= ulckpgs; 41657c478bd9Sstevel@tonic-gate mutex_exit(&freemem_lock); 41667c478bd9Sstevel@tonic-gate } 41677c478bd9Sstevel@tonic-gate 41687c478bd9Sstevel@tonic-gate for (pg_idx = 0; ppa[pg_idx] != NULL; pg_idx++) { 41697c478bd9Sstevel@tonic-gate ppa[pg_idx]->p_cowcnt--; 41707c478bd9Sstevel@tonic-gate ppa[pg_idx]->p_lckcnt++; 41717c478bd9Sstevel@tonic-gate 41727c478bd9Sstevel@tonic-gate } 41737c478bd9Sstevel@tonic-gate mutex_exit(&page_llock); 41747c478bd9Sstevel@tonic-gate return (1); 41757c478bd9Sstevel@tonic-gate } 41767c478bd9Sstevel@tonic-gate 41777c478bd9Sstevel@tonic-gate page_t * 41787c478bd9Sstevel@tonic-gate page_numtopp(pfn_t pfnum, se_t se) 41797c478bd9Sstevel@tonic-gate { 41807c478bd9Sstevel@tonic-gate page_t *pp; 41817c478bd9Sstevel@tonic-gate 41827c478bd9Sstevel@tonic-gate retry: 41837c478bd9Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 41847c478bd9Sstevel@tonic-gate if (pp == NULL) { 41857c478bd9Sstevel@tonic-gate return ((page_t *)NULL); 41867c478bd9Sstevel@tonic-gate } 41877c478bd9Sstevel@tonic-gate 41887c478bd9Sstevel@tonic-gate /* 41897c478bd9Sstevel@tonic-gate * Acquire the appropriate lock on the page. 41907c478bd9Sstevel@tonic-gate */ 41917c478bd9Sstevel@tonic-gate while (!page_lock(pp, se, (kmutex_t *)NULL, P_RECLAIM)) { 41927c478bd9Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) 41937c478bd9Sstevel@tonic-gate goto retry; 41947c478bd9Sstevel@tonic-gate continue; 41957c478bd9Sstevel@tonic-gate } 41967c478bd9Sstevel@tonic-gate 41977c478bd9Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) { 41987c478bd9Sstevel@tonic-gate page_unlock(pp); 41997c478bd9Sstevel@tonic-gate goto retry; 42007c478bd9Sstevel@tonic-gate } 42017c478bd9Sstevel@tonic-gate 42027c478bd9Sstevel@tonic-gate return (pp); 42037c478bd9Sstevel@tonic-gate } 42047c478bd9Sstevel@tonic-gate 42057c478bd9Sstevel@tonic-gate page_t * 42067c478bd9Sstevel@tonic-gate page_numtopp_noreclaim(pfn_t pfnum, se_t se) 42077c478bd9Sstevel@tonic-gate { 42087c478bd9Sstevel@tonic-gate page_t *pp; 42097c478bd9Sstevel@tonic-gate 42107c478bd9Sstevel@tonic-gate retry: 42117c478bd9Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 42127c478bd9Sstevel@tonic-gate if (pp == NULL) { 42137c478bd9Sstevel@tonic-gate return ((page_t *)NULL); 42147c478bd9Sstevel@tonic-gate } 42157c478bd9Sstevel@tonic-gate 42167c478bd9Sstevel@tonic-gate /* 42177c478bd9Sstevel@tonic-gate * Acquire the appropriate lock on the page. 42187c478bd9Sstevel@tonic-gate */ 42197c478bd9Sstevel@tonic-gate while (!page_lock(pp, se, (kmutex_t *)NULL, P_NO_RECLAIM)) { 42207c478bd9Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) 42217c478bd9Sstevel@tonic-gate goto retry; 42227c478bd9Sstevel@tonic-gate continue; 42237c478bd9Sstevel@tonic-gate } 42247c478bd9Sstevel@tonic-gate 42257c478bd9Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) { 42267c478bd9Sstevel@tonic-gate page_unlock(pp); 42277c478bd9Sstevel@tonic-gate goto retry; 42287c478bd9Sstevel@tonic-gate } 42297c478bd9Sstevel@tonic-gate 42307c478bd9Sstevel@tonic-gate return (pp); 42317c478bd9Sstevel@tonic-gate } 42327c478bd9Sstevel@tonic-gate 42337c478bd9Sstevel@tonic-gate /* 42347c478bd9Sstevel@tonic-gate * This routine is like page_numtopp, but will only return page structs 42357c478bd9Sstevel@tonic-gate * for pages which are ok for loading into hardware using the page struct. 42367c478bd9Sstevel@tonic-gate */ 42377c478bd9Sstevel@tonic-gate page_t * 42387c478bd9Sstevel@tonic-gate page_numtopp_nowait(pfn_t pfnum, se_t se) 42397c478bd9Sstevel@tonic-gate { 42407c478bd9Sstevel@tonic-gate page_t *pp; 42417c478bd9Sstevel@tonic-gate 42427c478bd9Sstevel@tonic-gate retry: 42437c478bd9Sstevel@tonic-gate pp = page_numtopp_nolock(pfnum); 42447c478bd9Sstevel@tonic-gate if (pp == NULL) { 42457c478bd9Sstevel@tonic-gate return ((page_t *)NULL); 42467c478bd9Sstevel@tonic-gate } 42477c478bd9Sstevel@tonic-gate 42487c478bd9Sstevel@tonic-gate /* 42497c478bd9Sstevel@tonic-gate * Try to acquire the appropriate lock on the page. 42507c478bd9Sstevel@tonic-gate */ 42517c478bd9Sstevel@tonic-gate if (PP_ISFREE(pp)) 42527c478bd9Sstevel@tonic-gate pp = NULL; 42537c478bd9Sstevel@tonic-gate else { 42547c478bd9Sstevel@tonic-gate if (!page_trylock(pp, se)) 42557c478bd9Sstevel@tonic-gate pp = NULL; 42567c478bd9Sstevel@tonic-gate else { 42577c478bd9Sstevel@tonic-gate if (page_pptonum(pp) != pfnum) { 42587c478bd9Sstevel@tonic-gate page_unlock(pp); 42597c478bd9Sstevel@tonic-gate goto retry; 42607c478bd9Sstevel@tonic-gate } 42617c478bd9Sstevel@tonic-gate if (PP_ISFREE(pp)) { 42627c478bd9Sstevel@tonic-gate page_unlock(pp); 42637c478bd9Sstevel@tonic-gate pp = NULL; 42647c478bd9Sstevel@tonic-gate } 42657c478bd9Sstevel@tonic-gate } 42667c478bd9Sstevel@tonic-gate } 42677c478bd9Sstevel@tonic-gate return (pp); 42687c478bd9Sstevel@tonic-gate } 42697c478bd9Sstevel@tonic-gate 42707c478bd9Sstevel@tonic-gate /* 42717c478bd9Sstevel@tonic-gate * Returns a count of dirty pages that are in the process 42727c478bd9Sstevel@tonic-gate * of being written out. If 'cleanit' is set, try to push the page. 42737c478bd9Sstevel@tonic-gate */ 42747c478bd9Sstevel@tonic-gate pgcnt_t 42757c478bd9Sstevel@tonic-gate page_busy(int cleanit) 42767c478bd9Sstevel@tonic-gate { 42777c478bd9Sstevel@tonic-gate page_t *page0 = page_first(); 42787c478bd9Sstevel@tonic-gate page_t *pp = page0; 42797c478bd9Sstevel@tonic-gate pgcnt_t nppbusy = 0; 42807c478bd9Sstevel@tonic-gate u_offset_t off; 42817c478bd9Sstevel@tonic-gate 42827c478bd9Sstevel@tonic-gate do { 42837c478bd9Sstevel@tonic-gate vnode_t *vp = pp->p_vnode; 42847c478bd9Sstevel@tonic-gate 42857c478bd9Sstevel@tonic-gate /* 42867c478bd9Sstevel@tonic-gate * A page is a candidate for syncing if it is: 42877c478bd9Sstevel@tonic-gate * 42887c478bd9Sstevel@tonic-gate * (a) On neither the freelist nor the cachelist 42897c478bd9Sstevel@tonic-gate * (b) Hashed onto a vnode 42907c478bd9Sstevel@tonic-gate * (c) Not a kernel page 42917c478bd9Sstevel@tonic-gate * (d) Dirty 42927c478bd9Sstevel@tonic-gate * (e) Not part of a swapfile 42937c478bd9Sstevel@tonic-gate * (f) a page which belongs to a real vnode; eg has a non-null 42947c478bd9Sstevel@tonic-gate * v_vfsp pointer. 42957c478bd9Sstevel@tonic-gate * (g) Backed by a filesystem which doesn't have a 42967c478bd9Sstevel@tonic-gate * stubbed-out sync operation 42977c478bd9Sstevel@tonic-gate */ 4298ad23a2dbSjohansen if (!PP_ISFREE(pp) && vp != NULL && !VN_ISKAS(vp) && 42997c478bd9Sstevel@tonic-gate hat_ismod(pp) && !IS_SWAPVP(vp) && vp->v_vfsp != NULL && 43007c478bd9Sstevel@tonic-gate vfs_can_sync(vp->v_vfsp)) { 43017c478bd9Sstevel@tonic-gate nppbusy++; 43027c478bd9Sstevel@tonic-gate vfs_syncprogress(); 43037c478bd9Sstevel@tonic-gate 43047c478bd9Sstevel@tonic-gate if (!cleanit) 43057c478bd9Sstevel@tonic-gate continue; 43067c478bd9Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) 43077c478bd9Sstevel@tonic-gate continue; 43087c478bd9Sstevel@tonic-gate 43097c478bd9Sstevel@tonic-gate if (PP_ISFREE(pp) || vp == NULL || IS_SWAPVP(vp) || 43107c478bd9Sstevel@tonic-gate pp->p_lckcnt != 0 || pp->p_cowcnt != 0 || 43117c478bd9Sstevel@tonic-gate !(hat_pagesync(pp, 43127c478bd9Sstevel@tonic-gate HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD) & P_MOD)) { 43137c478bd9Sstevel@tonic-gate page_unlock(pp); 43147c478bd9Sstevel@tonic-gate continue; 43157c478bd9Sstevel@tonic-gate } 43167c478bd9Sstevel@tonic-gate off = pp->p_offset; 43177c478bd9Sstevel@tonic-gate VN_HOLD(vp); 43187c478bd9Sstevel@tonic-gate page_unlock(pp); 43197c478bd9Sstevel@tonic-gate (void) VOP_PUTPAGE(vp, off, PAGESIZE, 4320da6c28aaSamw B_ASYNC | B_FREE, kcred, NULL); 43217c478bd9Sstevel@tonic-gate VN_RELE(vp); 43227c478bd9Sstevel@tonic-gate } 43237c478bd9Sstevel@tonic-gate } while ((pp = page_next(pp)) != page0); 43247c478bd9Sstevel@tonic-gate 43257c478bd9Sstevel@tonic-gate return (nppbusy); 43267c478bd9Sstevel@tonic-gate } 43277c478bd9Sstevel@tonic-gate 43287c478bd9Sstevel@tonic-gate void page_invalidate_pages(void); 43297c478bd9Sstevel@tonic-gate 43307c478bd9Sstevel@tonic-gate /* 43317c478bd9Sstevel@tonic-gate * callback handler to vm sub-system 43327c478bd9Sstevel@tonic-gate * 43337c478bd9Sstevel@tonic-gate * callers make sure no recursive entries to this func. 43347c478bd9Sstevel@tonic-gate */ 43357c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 43367c478bd9Sstevel@tonic-gate boolean_t 43377c478bd9Sstevel@tonic-gate callb_vm_cpr(void *arg, int code) 43387c478bd9Sstevel@tonic-gate { 43397c478bd9Sstevel@tonic-gate if (code == CB_CODE_CPR_CHKPT) 43407c478bd9Sstevel@tonic-gate page_invalidate_pages(); 43417c478bd9Sstevel@tonic-gate return (B_TRUE); 43427c478bd9Sstevel@tonic-gate } 43437c478bd9Sstevel@tonic-gate 43447c478bd9Sstevel@tonic-gate /* 43457c478bd9Sstevel@tonic-gate * Invalidate all pages of the system. 43467c478bd9Sstevel@tonic-gate * It shouldn't be called until all user page activities are all stopped. 43477c478bd9Sstevel@tonic-gate */ 43487c478bd9Sstevel@tonic-gate void 43497c478bd9Sstevel@tonic-gate page_invalidate_pages() 43507c478bd9Sstevel@tonic-gate { 43517c478bd9Sstevel@tonic-gate page_t *pp; 43527c478bd9Sstevel@tonic-gate page_t *page0; 43537c478bd9Sstevel@tonic-gate pgcnt_t nbusypages; 43547c478bd9Sstevel@tonic-gate int retry = 0; 43557c478bd9Sstevel@tonic-gate const int MAXRETRIES = 4; 43567c478bd9Sstevel@tonic-gate #if defined(__sparc) 43577c478bd9Sstevel@tonic-gate extern struct vnode prom_ppages; 43587c478bd9Sstevel@tonic-gate #endif /* __sparc */ 43597c478bd9Sstevel@tonic-gate 43607c478bd9Sstevel@tonic-gate top: 43617c478bd9Sstevel@tonic-gate /* 43628b464eb8Smec * Flush dirty pages and destroy the clean ones. 43637c478bd9Sstevel@tonic-gate */ 43647c478bd9Sstevel@tonic-gate nbusypages = 0; 43657c478bd9Sstevel@tonic-gate 43667c478bd9Sstevel@tonic-gate pp = page0 = page_first(); 43677c478bd9Sstevel@tonic-gate do { 43687c478bd9Sstevel@tonic-gate struct vnode *vp; 43697c478bd9Sstevel@tonic-gate u_offset_t offset; 43707c478bd9Sstevel@tonic-gate int mod; 43717c478bd9Sstevel@tonic-gate 43727c478bd9Sstevel@tonic-gate /* 43737c478bd9Sstevel@tonic-gate * skip the page if it has no vnode or the page associated 43747c478bd9Sstevel@tonic-gate * with the kernel vnode or prom allocated kernel mem. 43757c478bd9Sstevel@tonic-gate */ 43767c478bd9Sstevel@tonic-gate #if defined(__sparc) 4377ad23a2dbSjohansen if ((vp = pp->p_vnode) == NULL || VN_ISKAS(vp) || 43787c478bd9Sstevel@tonic-gate vp == &prom_ppages) 43797c478bd9Sstevel@tonic-gate #else /* x86 doesn't have prom or prom_ppage */ 4380ad23a2dbSjohansen if ((vp = pp->p_vnode) == NULL || VN_ISKAS(vp)) 43817c478bd9Sstevel@tonic-gate #endif /* __sparc */ 43827c478bd9Sstevel@tonic-gate continue; 43837c478bd9Sstevel@tonic-gate 43847c478bd9Sstevel@tonic-gate /* 43857c478bd9Sstevel@tonic-gate * skip the page which is already free invalidated. 43867c478bd9Sstevel@tonic-gate */ 43877c478bd9Sstevel@tonic-gate if (PP_ISFREE(pp) && PP_ISAGED(pp)) 43887c478bd9Sstevel@tonic-gate continue; 43897c478bd9Sstevel@tonic-gate 43907c478bd9Sstevel@tonic-gate /* 43917c478bd9Sstevel@tonic-gate * skip pages that are already locked or can't be "exclusively" 43927c478bd9Sstevel@tonic-gate * locked or are already free. After we lock the page, check 43932e0ea4c4SMichael Corcoran * the free and age bits again to be sure it's not destroyed 43947c478bd9Sstevel@tonic-gate * yet. 43957c478bd9Sstevel@tonic-gate * To achieve max. parallelization, we use page_trylock instead 43967c478bd9Sstevel@tonic-gate * of page_lock so that we don't get block on individual pages 43977c478bd9Sstevel@tonic-gate * while we have thousands of other pages to process. 43987c478bd9Sstevel@tonic-gate */ 43997c478bd9Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) { 44007c478bd9Sstevel@tonic-gate nbusypages++; 44017c478bd9Sstevel@tonic-gate continue; 44027c478bd9Sstevel@tonic-gate } else if (PP_ISFREE(pp)) { 44037c478bd9Sstevel@tonic-gate if (!PP_ISAGED(pp)) { 44047c478bd9Sstevel@tonic-gate page_destroy_free(pp); 44057c478bd9Sstevel@tonic-gate } else { 44067c478bd9Sstevel@tonic-gate page_unlock(pp); 44077c478bd9Sstevel@tonic-gate } 44087c478bd9Sstevel@tonic-gate continue; 44097c478bd9Sstevel@tonic-gate } 44107c478bd9Sstevel@tonic-gate /* 44117c478bd9Sstevel@tonic-gate * Is this page involved in some I/O? shared? 44127c478bd9Sstevel@tonic-gate * 44137c478bd9Sstevel@tonic-gate * The page_struct_lock need not be acquired to 44147c478bd9Sstevel@tonic-gate * examine these fields since the page has an 44157c478bd9Sstevel@tonic-gate * "exclusive" lock. 44167c478bd9Sstevel@tonic-gate */ 44177c478bd9Sstevel@tonic-gate if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) { 44187c478bd9Sstevel@tonic-gate page_unlock(pp); 44197c478bd9Sstevel@tonic-gate continue; 44207c478bd9Sstevel@tonic-gate } 44217c478bd9Sstevel@tonic-gate 44227c478bd9Sstevel@tonic-gate if (vp->v_type == VCHR) { 44237c478bd9Sstevel@tonic-gate panic("vp->v_type == VCHR"); 44247c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 44257c478bd9Sstevel@tonic-gate } 44267c478bd9Sstevel@tonic-gate 44277c478bd9Sstevel@tonic-gate if (!page_try_demote_pages(pp)) { 44287c478bd9Sstevel@tonic-gate page_unlock(pp); 44297c478bd9Sstevel@tonic-gate continue; 44307c478bd9Sstevel@tonic-gate } 44317c478bd9Sstevel@tonic-gate 44327c478bd9Sstevel@tonic-gate /* 44337c478bd9Sstevel@tonic-gate * Check the modified bit. Leave the bits alone in hardware 44347c478bd9Sstevel@tonic-gate * (they will be modified if we do the putpage). 44357c478bd9Sstevel@tonic-gate */ 44367c478bd9Sstevel@tonic-gate mod = (hat_pagesync(pp, HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD) 44376e4dd838Smec & P_MOD); 44387c478bd9Sstevel@tonic-gate if (mod) { 44397c478bd9Sstevel@tonic-gate offset = pp->p_offset; 44407c478bd9Sstevel@tonic-gate /* 44417c478bd9Sstevel@tonic-gate * Hold the vnode before releasing the page lock 44427c478bd9Sstevel@tonic-gate * to prevent it from being freed and re-used by 44437c478bd9Sstevel@tonic-gate * some other thread. 44447c478bd9Sstevel@tonic-gate */ 44457c478bd9Sstevel@tonic-gate VN_HOLD(vp); 44467c478bd9Sstevel@tonic-gate page_unlock(pp); 44477c478bd9Sstevel@tonic-gate /* 44487c478bd9Sstevel@tonic-gate * No error return is checked here. Callers such as 44497c478bd9Sstevel@tonic-gate * cpr deals with the dirty pages at the dump time 44507c478bd9Sstevel@tonic-gate * if this putpage fails. 44517c478bd9Sstevel@tonic-gate */ 44527c478bd9Sstevel@tonic-gate (void) VOP_PUTPAGE(vp, offset, PAGESIZE, B_INVAL, 4453da6c28aaSamw kcred, NULL); 44547c478bd9Sstevel@tonic-gate VN_RELE(vp); 44557c478bd9Sstevel@tonic-gate } else { 44562e0ea4c4SMichael Corcoran /*LINTED: constant in conditional context*/ 44572e0ea4c4SMichael Corcoran VN_DISPOSE(pp, B_INVAL, 0, kcred); 44587c478bd9Sstevel@tonic-gate } 44597c478bd9Sstevel@tonic-gate } while ((pp = page_next(pp)) != page0); 44607c478bd9Sstevel@tonic-gate if (nbusypages && retry++ < MAXRETRIES) { 44617c478bd9Sstevel@tonic-gate delay(1); 44627c478bd9Sstevel@tonic-gate goto top; 44637c478bd9Sstevel@tonic-gate } 44647c478bd9Sstevel@tonic-gate } 44657c478bd9Sstevel@tonic-gate 44667c478bd9Sstevel@tonic-gate /* 44677c478bd9Sstevel@tonic-gate * Replace the page "old" with the page "new" on the page hash and vnode lists 44687c478bd9Sstevel@tonic-gate * 4469da6c28aaSamw * the replacement must be done in place, ie the equivalent sequence: 44707c478bd9Sstevel@tonic-gate * 44717c478bd9Sstevel@tonic-gate * vp = old->p_vnode; 44727c478bd9Sstevel@tonic-gate * off = old->p_offset; 44737c478bd9Sstevel@tonic-gate * page_do_hashout(old) 44747c478bd9Sstevel@tonic-gate * page_do_hashin(new, vp, off) 44757c478bd9Sstevel@tonic-gate * 44767c478bd9Sstevel@tonic-gate * doesn't work, since 44777c478bd9Sstevel@tonic-gate * 1) if old is the only page on the vnode, the v_pages list has a window 44787c478bd9Sstevel@tonic-gate * where it looks empty. This will break file system assumptions. 44797c478bd9Sstevel@tonic-gate * and 44807c478bd9Sstevel@tonic-gate * 2) pvn_vplist_dirty() can't deal with pages moving on the v_pages list. 44817c478bd9Sstevel@tonic-gate */ 44827c478bd9Sstevel@tonic-gate static void 44837c478bd9Sstevel@tonic-gate page_do_relocate_hash(page_t *new, page_t *old) 44847c478bd9Sstevel@tonic-gate { 44857c478bd9Sstevel@tonic-gate page_t **hash_list; 44867c478bd9Sstevel@tonic-gate vnode_t *vp = old->p_vnode; 44877c478bd9Sstevel@tonic-gate kmutex_t *sep; 44887c478bd9Sstevel@tonic-gate 44897c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(old)); 44907c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(new)); 44917c478bd9Sstevel@tonic-gate ASSERT(vp != NULL); 44927c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(page_vnode_mutex(vp))); 44937c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, old->p_offset)))); 44947c478bd9Sstevel@tonic-gate 44957c478bd9Sstevel@tonic-gate /* 44967c478bd9Sstevel@tonic-gate * First find old page on the page hash list 44977c478bd9Sstevel@tonic-gate */ 44987c478bd9Sstevel@tonic-gate hash_list = &page_hash[PAGE_HASH_FUNC(vp, old->p_offset)]; 44997c478bd9Sstevel@tonic-gate 45007c478bd9Sstevel@tonic-gate for (;;) { 45017c478bd9Sstevel@tonic-gate if (*hash_list == old) 45027c478bd9Sstevel@tonic-gate break; 45037c478bd9Sstevel@tonic-gate if (*hash_list == NULL) { 45047c478bd9Sstevel@tonic-gate panic("page_do_hashout"); 45057c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 45067c478bd9Sstevel@tonic-gate } 45077c478bd9Sstevel@tonic-gate hash_list = &(*hash_list)->p_hash; 45087c478bd9Sstevel@tonic-gate } 45097c478bd9Sstevel@tonic-gate 45107c478bd9Sstevel@tonic-gate /* 45117c478bd9Sstevel@tonic-gate * update new and replace old with new on the page hash list 45127c478bd9Sstevel@tonic-gate */ 45137c478bd9Sstevel@tonic-gate new->p_vnode = old->p_vnode; 45147c478bd9Sstevel@tonic-gate new->p_offset = old->p_offset; 45157c478bd9Sstevel@tonic-gate new->p_hash = old->p_hash; 45167c478bd9Sstevel@tonic-gate *hash_list = new; 45177c478bd9Sstevel@tonic-gate 45187c478bd9Sstevel@tonic-gate if ((new->p_vnode->v_flag & VISSWAP) != 0) 45197c478bd9Sstevel@tonic-gate PP_SETSWAP(new); 45207c478bd9Sstevel@tonic-gate 45217c478bd9Sstevel@tonic-gate /* 45227c478bd9Sstevel@tonic-gate * replace old with new on the vnode's page list 45237c478bd9Sstevel@tonic-gate */ 45247c478bd9Sstevel@tonic-gate if (old->p_vpnext == old) { 45257c478bd9Sstevel@tonic-gate new->p_vpnext = new; 45267c478bd9Sstevel@tonic-gate new->p_vpprev = new; 45277c478bd9Sstevel@tonic-gate } else { 45287c478bd9Sstevel@tonic-gate new->p_vpnext = old->p_vpnext; 45297c478bd9Sstevel@tonic-gate new->p_vpprev = old->p_vpprev; 45307c478bd9Sstevel@tonic-gate new->p_vpnext->p_vpprev = new; 45317c478bd9Sstevel@tonic-gate new->p_vpprev->p_vpnext = new; 45327c478bd9Sstevel@tonic-gate } 45337c478bd9Sstevel@tonic-gate if (vp->v_pages == old) 45347c478bd9Sstevel@tonic-gate vp->v_pages = new; 45357c478bd9Sstevel@tonic-gate 45367c478bd9Sstevel@tonic-gate /* 45377c478bd9Sstevel@tonic-gate * clear out the old page 45387c478bd9Sstevel@tonic-gate */ 45397c478bd9Sstevel@tonic-gate old->p_hash = NULL; 45407c478bd9Sstevel@tonic-gate old->p_vpnext = NULL; 45417c478bd9Sstevel@tonic-gate old->p_vpprev = NULL; 45427c478bd9Sstevel@tonic-gate old->p_vnode = NULL; 45437c478bd9Sstevel@tonic-gate PP_CLRSWAP(old); 45447c478bd9Sstevel@tonic-gate old->p_offset = (u_offset_t)-1; 45452f0fcb93SJason Beloro page_clr_all_props(old, 1); 45467c478bd9Sstevel@tonic-gate 45477c478bd9Sstevel@tonic-gate /* 45487c478bd9Sstevel@tonic-gate * Wake up processes waiting for this page. The page's 45497c478bd9Sstevel@tonic-gate * identity has been changed, and is probably not the 45507c478bd9Sstevel@tonic-gate * desired page any longer. 45517c478bd9Sstevel@tonic-gate */ 45527c478bd9Sstevel@tonic-gate sep = page_se_mutex(old); 45537c478bd9Sstevel@tonic-gate mutex_enter(sep); 455442787a71Sstans old->p_selock &= ~SE_EWANTED; 45557c478bd9Sstevel@tonic-gate if (CV_HAS_WAITERS(&old->p_cv)) 45567c478bd9Sstevel@tonic-gate cv_broadcast(&old->p_cv); 45577c478bd9Sstevel@tonic-gate mutex_exit(sep); 45587c478bd9Sstevel@tonic-gate } 45597c478bd9Sstevel@tonic-gate 45607c478bd9Sstevel@tonic-gate /* 45617c478bd9Sstevel@tonic-gate * This function moves the identity of page "pp_old" to page "pp_new". 45627c478bd9Sstevel@tonic-gate * Both pages must be locked on entry. "pp_new" is free, has no identity, 45637c478bd9Sstevel@tonic-gate * and need not be hashed out from anywhere. 45647c478bd9Sstevel@tonic-gate */ 45657c478bd9Sstevel@tonic-gate void 45667c478bd9Sstevel@tonic-gate page_relocate_hash(page_t *pp_new, page_t *pp_old) 45677c478bd9Sstevel@tonic-gate { 45687c478bd9Sstevel@tonic-gate vnode_t *vp = pp_old->p_vnode; 45697c478bd9Sstevel@tonic-gate u_offset_t off = pp_old->p_offset; 45707c478bd9Sstevel@tonic-gate kmutex_t *phm, *vphm; 45717c478bd9Sstevel@tonic-gate 45727c478bd9Sstevel@tonic-gate /* 45737c478bd9Sstevel@tonic-gate * Rehash two pages 45747c478bd9Sstevel@tonic-gate */ 45757c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp_old)); 45767c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp_new)); 45777c478bd9Sstevel@tonic-gate ASSERT(vp != NULL); 45787c478bd9Sstevel@tonic-gate ASSERT(pp_new->p_vnode == NULL); 45797c478bd9Sstevel@tonic-gate 45807c478bd9Sstevel@tonic-gate /* 45817c478bd9Sstevel@tonic-gate * hashout then hashin while holding the mutexes 45827c478bd9Sstevel@tonic-gate */ 45837c478bd9Sstevel@tonic-gate phm = PAGE_HASH_MUTEX(PAGE_HASH_FUNC(vp, off)); 45847c478bd9Sstevel@tonic-gate mutex_enter(phm); 45857c478bd9Sstevel@tonic-gate vphm = page_vnode_mutex(vp); 45867c478bd9Sstevel@tonic-gate mutex_enter(vphm); 45877c478bd9Sstevel@tonic-gate 45887c478bd9Sstevel@tonic-gate page_do_relocate_hash(pp_new, pp_old); 45897c478bd9Sstevel@tonic-gate 4590c7531c7fSPrakash Sangappa /* The following comment preserved from page_flip(). */ 4591c7531c7fSPrakash Sangappa pp_new->p_fsdata = pp_old->p_fsdata; 4592c7531c7fSPrakash Sangappa pp_old->p_fsdata = 0; 45937c478bd9Sstevel@tonic-gate mutex_exit(vphm); 45947c478bd9Sstevel@tonic-gate mutex_exit(phm); 45957c478bd9Sstevel@tonic-gate 45967c478bd9Sstevel@tonic-gate /* 45977c478bd9Sstevel@tonic-gate * The page_struct_lock need not be acquired for lckcnt and 45987c478bd9Sstevel@tonic-gate * cowcnt since the page has an "exclusive" lock. 45997c478bd9Sstevel@tonic-gate */ 46007c478bd9Sstevel@tonic-gate ASSERT(pp_new->p_lckcnt == 0); 46017c478bd9Sstevel@tonic-gate ASSERT(pp_new->p_cowcnt == 0); 46027c478bd9Sstevel@tonic-gate pp_new->p_lckcnt = pp_old->p_lckcnt; 46037c478bd9Sstevel@tonic-gate pp_new->p_cowcnt = pp_old->p_cowcnt; 46047c478bd9Sstevel@tonic-gate pp_old->p_lckcnt = pp_old->p_cowcnt = 0; 46057c478bd9Sstevel@tonic-gate 46067c478bd9Sstevel@tonic-gate } 46077c478bd9Sstevel@tonic-gate 46087c478bd9Sstevel@tonic-gate /* 46097c478bd9Sstevel@tonic-gate * Helper routine used to lock all remaining members of a 46107c478bd9Sstevel@tonic-gate * large page. The caller is responsible for passing in a locked 46117c478bd9Sstevel@tonic-gate * pp. If pp is a large page, then it succeeds in locking all the 46127c478bd9Sstevel@tonic-gate * remaining constituent pages or it returns with only the 46137c478bd9Sstevel@tonic-gate * original page locked. 46147c478bd9Sstevel@tonic-gate * 46157c478bd9Sstevel@tonic-gate * Returns 1 on success, 0 on failure. 46167c478bd9Sstevel@tonic-gate * 4617da6c28aaSamw * If success is returned this routine guarantees p_szc for all constituent 46187c478bd9Sstevel@tonic-gate * pages of a large page pp belongs to can't change. To achieve this we 46197c478bd9Sstevel@tonic-gate * recheck szc of pp after locking all constituent pages and retry if szc 46207c478bd9Sstevel@tonic-gate * changed (it could only decrease). Since hat_page_demote() needs an EXCL 46217c478bd9Sstevel@tonic-gate * lock on one of constituent pages it can't be running after all constituent 46227c478bd9Sstevel@tonic-gate * pages are locked. hat_page_demote() with a lock on a constituent page 46237c478bd9Sstevel@tonic-gate * outside of this large page (i.e. pp belonged to a larger large page) is 46247c478bd9Sstevel@tonic-gate * already done with all constituent pages of pp since the root's p_szc is 4625da6c28aaSamw * changed last. Therefore no need to synchronize with hat_page_demote() that 46267c478bd9Sstevel@tonic-gate * locked a constituent page outside of pp's current large page. 46277c478bd9Sstevel@tonic-gate */ 46287c478bd9Sstevel@tonic-gate #ifdef DEBUG 46297c478bd9Sstevel@tonic-gate uint32_t gpg_trylock_mtbf = 0; 46307c478bd9Sstevel@tonic-gate #endif 46317c478bd9Sstevel@tonic-gate 46327c478bd9Sstevel@tonic-gate int 46337c478bd9Sstevel@tonic-gate group_page_trylock(page_t *pp, se_t se) 46347c478bd9Sstevel@tonic-gate { 46357c478bd9Sstevel@tonic-gate page_t *tpp; 46367c478bd9Sstevel@tonic-gate pgcnt_t npgs, i, j; 46377c478bd9Sstevel@tonic-gate uint_t pszc = pp->p_szc; 46387c478bd9Sstevel@tonic-gate 46397c478bd9Sstevel@tonic-gate #ifdef DEBUG 46407c478bd9Sstevel@tonic-gate if (gpg_trylock_mtbf && !(gethrtime() % gpg_trylock_mtbf)) { 46417c478bd9Sstevel@tonic-gate return (0); 46427c478bd9Sstevel@tonic-gate } 46437c478bd9Sstevel@tonic-gate #endif 46447c478bd9Sstevel@tonic-gate 46457c478bd9Sstevel@tonic-gate if (pp != PP_GROUPLEADER(pp, pszc)) { 46467c478bd9Sstevel@tonic-gate return (0); 46477c478bd9Sstevel@tonic-gate } 46487c478bd9Sstevel@tonic-gate 46497c478bd9Sstevel@tonic-gate retry: 46507c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED_SE(pp, se)); 46517c478bd9Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 46527c478bd9Sstevel@tonic-gate if (pszc == 0) { 46537c478bd9Sstevel@tonic-gate return (1); 46547c478bd9Sstevel@tonic-gate } 46557c478bd9Sstevel@tonic-gate npgs = page_get_pagecnt(pszc); 46567c478bd9Sstevel@tonic-gate tpp = pp + 1; 46577c478bd9Sstevel@tonic-gate for (i = 1; i < npgs; i++, tpp++) { 46587c478bd9Sstevel@tonic-gate if (!page_trylock(tpp, se)) { 46597c478bd9Sstevel@tonic-gate tpp = pp + 1; 46607c478bd9Sstevel@tonic-gate for (j = 1; j < i; j++, tpp++) { 46617c478bd9Sstevel@tonic-gate page_unlock(tpp); 46627c478bd9Sstevel@tonic-gate } 46637c478bd9Sstevel@tonic-gate return (0); 46647c478bd9Sstevel@tonic-gate } 46657c478bd9Sstevel@tonic-gate } 46667c478bd9Sstevel@tonic-gate if (pp->p_szc != pszc) { 46677c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc < pszc); 4668ad23a2dbSjohansen ASSERT(pp->p_vnode != NULL && !PP_ISKAS(pp) && 46697c478bd9Sstevel@tonic-gate !IS_SWAPFSVP(pp->p_vnode)); 46707c478bd9Sstevel@tonic-gate tpp = pp + 1; 46717c478bd9Sstevel@tonic-gate for (i = 1; i < npgs; i++, tpp++) { 46727c478bd9Sstevel@tonic-gate page_unlock(tpp); 46737c478bd9Sstevel@tonic-gate } 46747c478bd9Sstevel@tonic-gate pszc = pp->p_szc; 46757c478bd9Sstevel@tonic-gate goto retry; 46767c478bd9Sstevel@tonic-gate } 46777c478bd9Sstevel@tonic-gate return (1); 46787c478bd9Sstevel@tonic-gate } 46797c478bd9Sstevel@tonic-gate 46807c478bd9Sstevel@tonic-gate void 46817c478bd9Sstevel@tonic-gate group_page_unlock(page_t *pp) 46827c478bd9Sstevel@tonic-gate { 46837c478bd9Sstevel@tonic-gate page_t *tpp; 46847c478bd9Sstevel@tonic-gate pgcnt_t npgs, i; 46857c478bd9Sstevel@tonic-gate 46867c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp)); 46877c478bd9Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 46887c478bd9Sstevel@tonic-gate ASSERT(pp == PP_PAGEROOT(pp)); 46897c478bd9Sstevel@tonic-gate npgs = page_get_pagecnt(pp->p_szc); 46907c478bd9Sstevel@tonic-gate for (i = 1, tpp = pp + 1; i < npgs; i++, tpp++) { 46917c478bd9Sstevel@tonic-gate page_unlock(tpp); 46927c478bd9Sstevel@tonic-gate } 46937c478bd9Sstevel@tonic-gate } 46947c478bd9Sstevel@tonic-gate 46957c478bd9Sstevel@tonic-gate /* 46967c478bd9Sstevel@tonic-gate * returns 46977c478bd9Sstevel@tonic-gate * 0 : on success and *nrelocp is number of relocated PAGESIZE pages 46987c478bd9Sstevel@tonic-gate * ERANGE : this is not a base page 46997c478bd9Sstevel@tonic-gate * EBUSY : failure to get locks on the page/pages 47007c478bd9Sstevel@tonic-gate * ENOMEM : failure to obtain replacement pages 47017c478bd9Sstevel@tonic-gate * EAGAIN : OBP has not yet completed its boot-time handoff to the kernel 47028b464eb8Smec * EIO : An error occurred while trying to copy the page data 47037c478bd9Sstevel@tonic-gate * 47047c478bd9Sstevel@tonic-gate * Return with all constituent members of target and replacement 47057c478bd9Sstevel@tonic-gate * SE_EXCL locked. It is the callers responsibility to drop the 47067c478bd9Sstevel@tonic-gate * locks. 47077c478bd9Sstevel@tonic-gate */ 47087c478bd9Sstevel@tonic-gate int 47097c478bd9Sstevel@tonic-gate do_page_relocate( 47107c478bd9Sstevel@tonic-gate page_t **target, 47117c478bd9Sstevel@tonic-gate page_t **replacement, 47127c478bd9Sstevel@tonic-gate int grouplock, 47137c478bd9Sstevel@tonic-gate spgcnt_t *nrelocp, 47147c478bd9Sstevel@tonic-gate lgrp_t *lgrp) 47157c478bd9Sstevel@tonic-gate { 47167c478bd9Sstevel@tonic-gate page_t *first_repl; 47177c478bd9Sstevel@tonic-gate page_t *repl; 47187c478bd9Sstevel@tonic-gate page_t *targ; 47197c478bd9Sstevel@tonic-gate page_t *pl = NULL; 47207c478bd9Sstevel@tonic-gate uint_t ppattr; 47217c478bd9Sstevel@tonic-gate pfn_t pfn, repl_pfn; 47227c478bd9Sstevel@tonic-gate uint_t szc; 47237c478bd9Sstevel@tonic-gate spgcnt_t npgs, i; 47247c478bd9Sstevel@tonic-gate int repl_contig = 0; 47257c478bd9Sstevel@tonic-gate uint_t flags = 0; 47267c478bd9Sstevel@tonic-gate spgcnt_t dofree = 0; 47277c478bd9Sstevel@tonic-gate 47287c478bd9Sstevel@tonic-gate *nrelocp = 0; 47297c478bd9Sstevel@tonic-gate 47307c478bd9Sstevel@tonic-gate #if defined(__sparc) 47317c478bd9Sstevel@tonic-gate /* 47327c478bd9Sstevel@tonic-gate * We need to wait till OBP has completed 47337c478bd9Sstevel@tonic-gate * its boot-time handoff of its resources to the kernel 47347c478bd9Sstevel@tonic-gate * before we allow page relocation 47357c478bd9Sstevel@tonic-gate */ 47367c478bd9Sstevel@tonic-gate if (page_relocate_ready == 0) { 47377c478bd9Sstevel@tonic-gate return (EAGAIN); 47387c478bd9Sstevel@tonic-gate } 47397c478bd9Sstevel@tonic-gate #endif 47407c478bd9Sstevel@tonic-gate 47417c478bd9Sstevel@tonic-gate /* 47427c478bd9Sstevel@tonic-gate * If this is not a base page, 47437c478bd9Sstevel@tonic-gate * just return with 0x0 pages relocated. 47447c478bd9Sstevel@tonic-gate */ 47457c478bd9Sstevel@tonic-gate targ = *target; 47467c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(targ)); 47477c478bd9Sstevel@tonic-gate ASSERT(!PP_ISFREE(targ)); 47487c478bd9Sstevel@tonic-gate szc = targ->p_szc; 47497c478bd9Sstevel@tonic-gate ASSERT(szc < mmu_page_sizes); 47507c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_reloc[szc]); 47517c478bd9Sstevel@tonic-gate pfn = targ->p_pagenum; 47527c478bd9Sstevel@tonic-gate if (pfn != PFN_BASE(pfn, szc)) { 47537c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnoroot[szc]); 47547c478bd9Sstevel@tonic-gate return (ERANGE); 47557c478bd9Sstevel@tonic-gate } 47567c478bd9Sstevel@tonic-gate 47577c478bd9Sstevel@tonic-gate if ((repl = *replacement) != NULL && repl->p_szc >= szc) { 47587c478bd9Sstevel@tonic-gate repl_pfn = repl->p_pagenum; 47597c478bd9Sstevel@tonic-gate if (repl_pfn != PFN_BASE(repl_pfn, szc)) { 47607c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_reloc_replnoroot[szc]); 47617c478bd9Sstevel@tonic-gate return (ERANGE); 47627c478bd9Sstevel@tonic-gate } 47637c478bd9Sstevel@tonic-gate repl_contig = 1; 47647c478bd9Sstevel@tonic-gate } 47657c478bd9Sstevel@tonic-gate 47667c478bd9Sstevel@tonic-gate /* 47677c478bd9Sstevel@tonic-gate * We must lock all members of this large page or we cannot 47687c478bd9Sstevel@tonic-gate * relocate any part of it. 47697c478bd9Sstevel@tonic-gate */ 47707c478bd9Sstevel@tonic-gate if (grouplock != 0 && !group_page_trylock(targ, SE_EXCL)) { 47717c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnolock[targ->p_szc]); 47727c478bd9Sstevel@tonic-gate return (EBUSY); 47737c478bd9Sstevel@tonic-gate } 47747c478bd9Sstevel@tonic-gate 47757c478bd9Sstevel@tonic-gate /* 47767c478bd9Sstevel@tonic-gate * reread szc it could have been decreased before 47777c478bd9Sstevel@tonic-gate * group_page_trylock() was done. 47787c478bd9Sstevel@tonic-gate */ 47797c478bd9Sstevel@tonic-gate szc = targ->p_szc; 47807c478bd9Sstevel@tonic-gate ASSERT(szc < mmu_page_sizes); 47817c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_reloc[szc]); 47827c478bd9Sstevel@tonic-gate ASSERT(pfn == PFN_BASE(pfn, szc)); 47837c478bd9Sstevel@tonic-gate 47847c478bd9Sstevel@tonic-gate npgs = page_get_pagecnt(targ->p_szc); 47857c478bd9Sstevel@tonic-gate 47867c478bd9Sstevel@tonic-gate if (repl == NULL) { 47877c478bd9Sstevel@tonic-gate dofree = npgs; /* Size of target page in MMU pages */ 47887c478bd9Sstevel@tonic-gate if (!page_create_wait(dofree, 0)) { 47897c478bd9Sstevel@tonic-gate if (grouplock != 0) { 47907c478bd9Sstevel@tonic-gate group_page_unlock(targ); 47917c478bd9Sstevel@tonic-gate } 47927c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnomem[szc]); 47937c478bd9Sstevel@tonic-gate return (ENOMEM); 47947c478bd9Sstevel@tonic-gate } 47957c478bd9Sstevel@tonic-gate 47967c478bd9Sstevel@tonic-gate /* 47977c478bd9Sstevel@tonic-gate * seg kmem pages require that the target and replacement 47987c478bd9Sstevel@tonic-gate * page be the same pagesize. 47997c478bd9Sstevel@tonic-gate */ 4800ad23a2dbSjohansen flags = (VN_ISKAS(targ->p_vnode)) ? PGR_SAMESZC : 0; 48017c478bd9Sstevel@tonic-gate repl = page_get_replacement_page(targ, lgrp, flags); 48027c478bd9Sstevel@tonic-gate if (repl == NULL) { 48037c478bd9Sstevel@tonic-gate if (grouplock != 0) { 48047c478bd9Sstevel@tonic-gate group_page_unlock(targ); 48057c478bd9Sstevel@tonic-gate } 48067c478bd9Sstevel@tonic-gate page_create_putback(dofree); 48077c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocnomem[szc]); 48087c478bd9Sstevel@tonic-gate return (ENOMEM); 48097c478bd9Sstevel@tonic-gate } 48107c478bd9Sstevel@tonic-gate } 48117c478bd9Sstevel@tonic-gate #ifdef DEBUG 48127c478bd9Sstevel@tonic-gate else { 48137c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(repl)); 48147c478bd9Sstevel@tonic-gate } 48157c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 48167c478bd9Sstevel@tonic-gate 48177c478bd9Sstevel@tonic-gate #if defined(__sparc) 48187c478bd9Sstevel@tonic-gate /* 48197c478bd9Sstevel@tonic-gate * Let hat_page_relocate() complete the relocation if it's kernel page 48207c478bd9Sstevel@tonic-gate */ 4821ad23a2dbSjohansen if (VN_ISKAS(targ->p_vnode)) { 48227c478bd9Sstevel@tonic-gate *replacement = repl; 48237c478bd9Sstevel@tonic-gate if (hat_page_relocate(target, replacement, nrelocp) != 0) { 48247c478bd9Sstevel@tonic-gate if (grouplock != 0) { 48257c478bd9Sstevel@tonic-gate group_page_unlock(targ); 48267c478bd9Sstevel@tonic-gate } 48277c478bd9Sstevel@tonic-gate if (dofree) { 48287c478bd9Sstevel@tonic-gate *replacement = NULL; 48297c478bd9Sstevel@tonic-gate page_free_replacement_page(repl); 48307c478bd9Sstevel@tonic-gate page_create_putback(dofree); 48317c478bd9Sstevel@tonic-gate } 48327c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_krelocfail[szc]); 48337c478bd9Sstevel@tonic-gate return (EAGAIN); 48347c478bd9Sstevel@tonic-gate } 48357c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocok[szc]); 48367c478bd9Sstevel@tonic-gate return (0); 48377c478bd9Sstevel@tonic-gate } 48387c478bd9Sstevel@tonic-gate #else 48397c478bd9Sstevel@tonic-gate #if defined(lint) 48407c478bd9Sstevel@tonic-gate dofree = dofree; 48417c478bd9Sstevel@tonic-gate #endif 48427c478bd9Sstevel@tonic-gate #endif 48437c478bd9Sstevel@tonic-gate 48447c478bd9Sstevel@tonic-gate first_repl = repl; 48457c478bd9Sstevel@tonic-gate 48467c478bd9Sstevel@tonic-gate for (i = 0; i < npgs; i++) { 48477c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(targ)); 484807b65a64Saguzovsk ASSERT(targ->p_slckcnt == 0); 484907b65a64Saguzovsk ASSERT(repl->p_slckcnt == 0); 48507c478bd9Sstevel@tonic-gate 48517c478bd9Sstevel@tonic-gate (void) hat_pageunload(targ, HAT_FORCE_PGUNLOAD); 48527c478bd9Sstevel@tonic-gate 48537c478bd9Sstevel@tonic-gate ASSERT(hat_page_getshare(targ) == 0); 48547c478bd9Sstevel@tonic-gate ASSERT(!PP_ISFREE(targ)); 48557c478bd9Sstevel@tonic-gate ASSERT(targ->p_pagenum == (pfn + i)); 48567c478bd9Sstevel@tonic-gate ASSERT(repl_contig == 0 || 48577c478bd9Sstevel@tonic-gate repl->p_pagenum == (repl_pfn + i)); 48587c478bd9Sstevel@tonic-gate 48597c478bd9Sstevel@tonic-gate /* 48607c478bd9Sstevel@tonic-gate * Copy the page contents and attributes then 48617c478bd9Sstevel@tonic-gate * relocate the page in the page hash. 48627c478bd9Sstevel@tonic-gate */ 48638b464eb8Smec if (ppcopy(targ, repl) == 0) { 48648b464eb8Smec targ = *target; 48658b464eb8Smec repl = first_repl; 48668b464eb8Smec VM_STAT_ADD(vmm_vmstats.ppr_copyfail); 48678b464eb8Smec if (grouplock != 0) { 48688b464eb8Smec group_page_unlock(targ); 48698b464eb8Smec } 48708b464eb8Smec if (dofree) { 48718b464eb8Smec *replacement = NULL; 48728b464eb8Smec page_free_replacement_page(repl); 48738b464eb8Smec page_create_putback(dofree); 48748b464eb8Smec } 48758b464eb8Smec return (EIO); 48768b464eb8Smec } 48778b464eb8Smec 48788b464eb8Smec targ++; 48798b464eb8Smec if (repl_contig != 0) { 48808b464eb8Smec repl++; 48818b464eb8Smec } else { 48828b464eb8Smec repl = repl->p_next; 48838b464eb8Smec } 48848b464eb8Smec } 48858b464eb8Smec 48868b464eb8Smec repl = first_repl; 48878b464eb8Smec targ = *target; 48888b464eb8Smec 48898b464eb8Smec for (i = 0; i < npgs; i++) { 48907c478bd9Sstevel@tonic-gate ppattr = hat_page_getattr(targ, (P_MOD | P_REF | P_RO)); 48912f0fcb93SJason Beloro page_clr_all_props(repl, 0); 48927c478bd9Sstevel@tonic-gate page_set_props(repl, ppattr); 48937c478bd9Sstevel@tonic-gate page_relocate_hash(repl, targ); 48947c478bd9Sstevel@tonic-gate 48957c478bd9Sstevel@tonic-gate ASSERT(hat_page_getshare(targ) == 0); 48967c478bd9Sstevel@tonic-gate ASSERT(hat_page_getshare(repl) == 0); 48977c478bd9Sstevel@tonic-gate /* 48987c478bd9Sstevel@tonic-gate * Now clear the props on targ, after the 48997c478bd9Sstevel@tonic-gate * page_relocate_hash(), they no longer 49007c478bd9Sstevel@tonic-gate * have any meaning. 49017c478bd9Sstevel@tonic-gate */ 49022f0fcb93SJason Beloro page_clr_all_props(targ, 0); 49037c478bd9Sstevel@tonic-gate ASSERT(targ->p_next == targ); 49047c478bd9Sstevel@tonic-gate ASSERT(targ->p_prev == targ); 49057c478bd9Sstevel@tonic-gate page_list_concat(&pl, &targ); 49067c478bd9Sstevel@tonic-gate 49077c478bd9Sstevel@tonic-gate targ++; 49087c478bd9Sstevel@tonic-gate if (repl_contig != 0) { 49097c478bd9Sstevel@tonic-gate repl++; 49107c478bd9Sstevel@tonic-gate } else { 49117c478bd9Sstevel@tonic-gate repl = repl->p_next; 49127c478bd9Sstevel@tonic-gate } 49137c478bd9Sstevel@tonic-gate } 49147c478bd9Sstevel@tonic-gate /* assert that we have come full circle with repl */ 49157c478bd9Sstevel@tonic-gate ASSERT(repl_contig == 1 || first_repl == repl); 49167c478bd9Sstevel@tonic-gate 49177c478bd9Sstevel@tonic-gate *target = pl; 49187c478bd9Sstevel@tonic-gate if (*replacement == NULL) { 49197c478bd9Sstevel@tonic-gate ASSERT(first_repl == repl); 49207c478bd9Sstevel@tonic-gate *replacement = repl; 49217c478bd9Sstevel@tonic-gate } 49227c478bd9Sstevel@tonic-gate VM_STAT_ADD(vmm_vmstats.ppr_relocok[szc]); 49237c478bd9Sstevel@tonic-gate *nrelocp = npgs; 49247c478bd9Sstevel@tonic-gate return (0); 49257c478bd9Sstevel@tonic-gate } 49267c478bd9Sstevel@tonic-gate /* 49277c478bd9Sstevel@tonic-gate * On success returns 0 and *nrelocp the number of PAGESIZE pages relocated. 49287c478bd9Sstevel@tonic-gate */ 49297c478bd9Sstevel@tonic-gate int 49307c478bd9Sstevel@tonic-gate page_relocate( 49317c478bd9Sstevel@tonic-gate page_t **target, 49327c478bd9Sstevel@tonic-gate page_t **replacement, 49337c478bd9Sstevel@tonic-gate int grouplock, 49347c478bd9Sstevel@tonic-gate int freetarget, 49357c478bd9Sstevel@tonic-gate spgcnt_t *nrelocp, 49367c478bd9Sstevel@tonic-gate lgrp_t *lgrp) 49377c478bd9Sstevel@tonic-gate { 49387c478bd9Sstevel@tonic-gate spgcnt_t ret; 49397c478bd9Sstevel@tonic-gate 49407c478bd9Sstevel@tonic-gate /* do_page_relocate returns 0 on success or errno value */ 49417c478bd9Sstevel@tonic-gate ret = do_page_relocate(target, replacement, grouplock, nrelocp, lgrp); 49427c478bd9Sstevel@tonic-gate 49437c478bd9Sstevel@tonic-gate if (ret != 0 || freetarget == 0) { 49447c478bd9Sstevel@tonic-gate return (ret); 49457c478bd9Sstevel@tonic-gate } 49467c478bd9Sstevel@tonic-gate if (*nrelocp == 1) { 49477c478bd9Sstevel@tonic-gate ASSERT(*target != NULL); 49487c478bd9Sstevel@tonic-gate page_free(*target, 1); 49497c478bd9Sstevel@tonic-gate } else { 49507c478bd9Sstevel@tonic-gate page_t *tpp = *target; 49517c478bd9Sstevel@tonic-gate uint_t szc = tpp->p_szc; 49527c478bd9Sstevel@tonic-gate pgcnt_t npgs = page_get_pagecnt(szc); 49537c478bd9Sstevel@tonic-gate ASSERT(npgs > 1); 49547c478bd9Sstevel@tonic-gate ASSERT(szc != 0); 49557c478bd9Sstevel@tonic-gate do { 49567c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 49577c478bd9Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp)); 49587c478bd9Sstevel@tonic-gate ASSERT(tpp->p_szc == szc); 49597c478bd9Sstevel@tonic-gate PP_SETFREE(tpp); 49607c478bd9Sstevel@tonic-gate PP_SETAGED(tpp); 49617c478bd9Sstevel@tonic-gate npgs--; 49627c478bd9Sstevel@tonic-gate } while ((tpp = tpp->p_next) != *target); 49637c478bd9Sstevel@tonic-gate ASSERT(npgs == 0); 49647c478bd9Sstevel@tonic-gate page_list_add_pages(*target, 0); 49657c478bd9Sstevel@tonic-gate npgs = page_get_pagecnt(szc); 49667c478bd9Sstevel@tonic-gate page_create_putback(npgs); 49677c478bd9Sstevel@tonic-gate } 49687c478bd9Sstevel@tonic-gate return (ret); 49697c478bd9Sstevel@tonic-gate } 49707c478bd9Sstevel@tonic-gate 49717c478bd9Sstevel@tonic-gate /* 49727c478bd9Sstevel@tonic-gate * it is up to the caller to deal with pcf accounting. 49737c478bd9Sstevel@tonic-gate */ 49747c478bd9Sstevel@tonic-gate void 49757c478bd9Sstevel@tonic-gate page_free_replacement_page(page_t *pplist) 49767c478bd9Sstevel@tonic-gate { 49777c478bd9Sstevel@tonic-gate page_t *pp; 49787c478bd9Sstevel@tonic-gate 49797c478bd9Sstevel@tonic-gate while (pplist != NULL) { 49807c478bd9Sstevel@tonic-gate /* 49817c478bd9Sstevel@tonic-gate * pp_targ is a linked list. 49827c478bd9Sstevel@tonic-gate */ 49837c478bd9Sstevel@tonic-gate pp = pplist; 49847c478bd9Sstevel@tonic-gate if (pp->p_szc == 0) { 49857c478bd9Sstevel@tonic-gate page_sub(&pplist, pp); 49862f0fcb93SJason Beloro page_clr_all_props(pp, 0); 49877c478bd9Sstevel@tonic-gate PP_SETFREE(pp); 49887c478bd9Sstevel@tonic-gate PP_SETAGED(pp); 49897c478bd9Sstevel@tonic-gate page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL); 49907c478bd9Sstevel@tonic-gate page_unlock(pp); 49917c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_replacement_page[0]); 49927c478bd9Sstevel@tonic-gate } else { 49937c478bd9Sstevel@tonic-gate spgcnt_t curnpgs = page_get_pagecnt(pp->p_szc); 49947c478bd9Sstevel@tonic-gate page_t *tpp; 49957c478bd9Sstevel@tonic-gate page_list_break(&pp, &pplist, curnpgs); 49967c478bd9Sstevel@tonic-gate tpp = pp; 49977c478bd9Sstevel@tonic-gate do { 49987c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 49997c478bd9Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp)); 50002f0fcb93SJason Beloro page_clr_all_props(tpp, 0); 50017c478bd9Sstevel@tonic-gate PP_SETFREE(tpp); 50027c478bd9Sstevel@tonic-gate PP_SETAGED(tpp); 50037c478bd9Sstevel@tonic-gate } while ((tpp = tpp->p_next) != pp); 50047c478bd9Sstevel@tonic-gate page_list_add_pages(pp, 0); 50057c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_free_replacement_page[1]); 50067c478bd9Sstevel@tonic-gate } 50077c478bd9Sstevel@tonic-gate } 50087c478bd9Sstevel@tonic-gate } 50097c478bd9Sstevel@tonic-gate 50107c478bd9Sstevel@tonic-gate /* 50117c478bd9Sstevel@tonic-gate * Relocate target to non-relocatable replacement page. 50127c478bd9Sstevel@tonic-gate */ 50137c478bd9Sstevel@tonic-gate int 50147c478bd9Sstevel@tonic-gate page_relocate_cage(page_t **target, page_t **replacement) 50157c478bd9Sstevel@tonic-gate { 50167c478bd9Sstevel@tonic-gate page_t *tpp, *rpp; 50177c478bd9Sstevel@tonic-gate spgcnt_t pgcnt, npgs; 50187c478bd9Sstevel@tonic-gate int result; 50197c478bd9Sstevel@tonic-gate 50207c478bd9Sstevel@tonic-gate tpp = *target; 50217c478bd9Sstevel@tonic-gate 50227c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 50237c478bd9Sstevel@tonic-gate ASSERT(tpp->p_szc == 0); 50247c478bd9Sstevel@tonic-gate 50257c478bd9Sstevel@tonic-gate pgcnt = btop(page_get_pagesize(tpp->p_szc)); 50267c478bd9Sstevel@tonic-gate 50277c478bd9Sstevel@tonic-gate do { 50287c478bd9Sstevel@tonic-gate (void) page_create_wait(pgcnt, PG_WAIT | PG_NORELOC); 50297c478bd9Sstevel@tonic-gate rpp = page_get_replacement_page(tpp, NULL, PGR_NORELOC); 50307c478bd9Sstevel@tonic-gate if (rpp == NULL) { 50317c478bd9Sstevel@tonic-gate page_create_putback(pgcnt); 50327c478bd9Sstevel@tonic-gate kcage_cageout_wakeup(); 50337c478bd9Sstevel@tonic-gate } 50347c478bd9Sstevel@tonic-gate } while (rpp == NULL); 50357c478bd9Sstevel@tonic-gate 50367c478bd9Sstevel@tonic-gate ASSERT(PP_ISNORELOC(rpp)); 50377c478bd9Sstevel@tonic-gate 50387c478bd9Sstevel@tonic-gate result = page_relocate(&tpp, &rpp, 0, 1, &npgs, NULL); 50397c478bd9Sstevel@tonic-gate 50407c478bd9Sstevel@tonic-gate if (result == 0) { 50417c478bd9Sstevel@tonic-gate *replacement = rpp; 50427c478bd9Sstevel@tonic-gate if (pgcnt != npgs) 50437c478bd9Sstevel@tonic-gate panic("page_relocate_cage: partial relocation"); 50447c478bd9Sstevel@tonic-gate } 50457c478bd9Sstevel@tonic-gate 50467c478bd9Sstevel@tonic-gate return (result); 50477c478bd9Sstevel@tonic-gate } 50487c478bd9Sstevel@tonic-gate 50497c478bd9Sstevel@tonic-gate /* 50507c478bd9Sstevel@tonic-gate * Release the page lock on a page, place on cachelist 50517c478bd9Sstevel@tonic-gate * tail if no longer mapped. Caller can let us know if 50527c478bd9Sstevel@tonic-gate * the page is known to be clean. 50537c478bd9Sstevel@tonic-gate */ 50547c478bd9Sstevel@tonic-gate int 50557c478bd9Sstevel@tonic-gate page_release(page_t *pp, int checkmod) 50567c478bd9Sstevel@tonic-gate { 50577c478bd9Sstevel@tonic-gate int status; 50587c478bd9Sstevel@tonic-gate 50597c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(pp) && !PP_ISFREE(pp) && 50606e4dd838Smec (pp->p_vnode != NULL)); 50617c478bd9Sstevel@tonic-gate 50627c478bd9Sstevel@tonic-gate if (!hat_page_is_mapped(pp) && !IS_SWAPVP(pp->p_vnode) && 50637c478bd9Sstevel@tonic-gate ((PAGE_SHARED(pp) && page_tryupgrade(pp)) || PAGE_EXCL(pp)) && 50647c478bd9Sstevel@tonic-gate pp->p_lckcnt == 0 && pp->p_cowcnt == 0 && 50657c478bd9Sstevel@tonic-gate !hat_page_is_mapped(pp)) { 50667c478bd9Sstevel@tonic-gate 50677c478bd9Sstevel@tonic-gate /* 50687c478bd9Sstevel@tonic-gate * If page is modified, unlock it 50697c478bd9Sstevel@tonic-gate * 50707c478bd9Sstevel@tonic-gate * (p_nrm & P_MOD) bit has the latest stuff because: 50717c478bd9Sstevel@tonic-gate * (1) We found that this page doesn't have any mappings 50727c478bd9Sstevel@tonic-gate * _after_ holding SE_EXCL and 50737c478bd9Sstevel@tonic-gate * (2) We didn't drop SE_EXCL lock after the check in (1) 50747c478bd9Sstevel@tonic-gate */ 50757c478bd9Sstevel@tonic-gate if (checkmod && hat_ismod(pp)) { 50767c478bd9Sstevel@tonic-gate page_unlock(pp); 50777c478bd9Sstevel@tonic-gate status = PGREL_MOD; 50787c478bd9Sstevel@tonic-gate } else { 50797c478bd9Sstevel@tonic-gate /*LINTED: constant in conditional context*/ 50807c478bd9Sstevel@tonic-gate VN_DISPOSE(pp, B_FREE, 0, kcred); 50817c478bd9Sstevel@tonic-gate status = PGREL_CLEAN; 50827c478bd9Sstevel@tonic-gate } 50837c478bd9Sstevel@tonic-gate } else { 50847c478bd9Sstevel@tonic-gate page_unlock(pp); 50857c478bd9Sstevel@tonic-gate status = PGREL_NOTREL; 50867c478bd9Sstevel@tonic-gate } 50877c478bd9Sstevel@tonic-gate return (status); 50887c478bd9Sstevel@tonic-gate } 50897c478bd9Sstevel@tonic-gate 5090db874c57Selowe /* 5091db874c57Selowe * Given a constituent page, try to demote the large page on the freelist. 5092db874c57Selowe * 5093db874c57Selowe * Returns nonzero if the page could be demoted successfully. Returns with 5094db874c57Selowe * the constituent page still locked. 5095db874c57Selowe */ 5096db874c57Selowe int 5097db874c57Selowe page_try_demote_free_pages(page_t *pp) 5098db874c57Selowe { 5099db874c57Selowe page_t *rootpp = pp; 5100db874c57Selowe pfn_t pfn = page_pptonum(pp); 5101db874c57Selowe spgcnt_t npgs; 5102db874c57Selowe uint_t szc = pp->p_szc; 5103db874c57Selowe 5104db874c57Selowe ASSERT(PP_ISFREE(pp)); 5105db874c57Selowe ASSERT(PAGE_EXCL(pp)); 5106db874c57Selowe 5107db874c57Selowe /* 5108db874c57Selowe * Adjust rootpp and lock it, if `pp' is not the base 5109db874c57Selowe * constituent page. 5110db874c57Selowe */ 5111db874c57Selowe npgs = page_get_pagecnt(pp->p_szc); 5112db874c57Selowe if (npgs == 1) { 5113db874c57Selowe return (0); 5114db874c57Selowe } 5115db874c57Selowe 5116db874c57Selowe if (!IS_P2ALIGNED(pfn, npgs)) { 5117db874c57Selowe pfn = P2ALIGN(pfn, npgs); 5118db874c57Selowe rootpp = page_numtopp_nolock(pfn); 5119db874c57Selowe } 5120db874c57Selowe 5121db874c57Selowe if (pp != rootpp && !page_trylock(rootpp, SE_EXCL)) { 5122db874c57Selowe return (0); 5123db874c57Selowe } 5124db874c57Selowe 5125db874c57Selowe if (rootpp->p_szc != szc) { 5126db874c57Selowe if (pp != rootpp) 5127db874c57Selowe page_unlock(rootpp); 5128db874c57Selowe return (0); 5129db874c57Selowe } 5130db874c57Selowe 5131db874c57Selowe page_demote_free_pages(rootpp); 5132db874c57Selowe 5133db874c57Selowe if (pp != rootpp) 5134db874c57Selowe page_unlock(rootpp); 5135db874c57Selowe 5136db874c57Selowe ASSERT(PP_ISFREE(pp)); 5137db874c57Selowe ASSERT(PAGE_EXCL(pp)); 5138db874c57Selowe return (1); 5139db874c57Selowe } 5140db874c57Selowe 5141db874c57Selowe /* 5142db874c57Selowe * Given a constituent page, try to demote the large page. 5143db874c57Selowe * 5144db874c57Selowe * Returns nonzero if the page could be demoted successfully. Returns with 5145db874c57Selowe * the constituent page still locked. 5146db874c57Selowe */ 51477c478bd9Sstevel@tonic-gate int 51487c478bd9Sstevel@tonic-gate page_try_demote_pages(page_t *pp) 51497c478bd9Sstevel@tonic-gate { 51507c478bd9Sstevel@tonic-gate page_t *tpp, *rootpp = pp; 51517c478bd9Sstevel@tonic-gate pfn_t pfn = page_pptonum(pp); 51527c478bd9Sstevel@tonic-gate spgcnt_t i, npgs; 51537c478bd9Sstevel@tonic-gate uint_t szc = pp->p_szc; 51547c478bd9Sstevel@tonic-gate vnode_t *vp = pp->p_vnode; 51557c478bd9Sstevel@tonic-gate 5156db874c57Selowe ASSERT(PAGE_EXCL(pp)); 51577c478bd9Sstevel@tonic-gate 51587c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[0]); 51597c478bd9Sstevel@tonic-gate 5160db874c57Selowe if (pp->p_szc == 0) { 51617c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[1]); 51627c478bd9Sstevel@tonic-gate return (1); 51637c478bd9Sstevel@tonic-gate } 51647c478bd9Sstevel@tonic-gate 5165ad23a2dbSjohansen if (vp != NULL && !IS_SWAPFSVP(vp) && !VN_ISKAS(vp)) { 51667c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[2]); 5167db874c57Selowe page_demote_vp_pages(pp); 51687c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 51697c478bd9Sstevel@tonic-gate return (1); 51707c478bd9Sstevel@tonic-gate } 51717c478bd9Sstevel@tonic-gate 51727c478bd9Sstevel@tonic-gate /* 5173db874c57Selowe * Adjust rootpp if passed in is not the base 51747c478bd9Sstevel@tonic-gate * constituent page. 51757c478bd9Sstevel@tonic-gate */ 5176db874c57Selowe npgs = page_get_pagecnt(pp->p_szc); 51777c478bd9Sstevel@tonic-gate ASSERT(npgs > 1); 51787c478bd9Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, npgs)) { 51797c478bd9Sstevel@tonic-gate pfn = P2ALIGN(pfn, npgs); 51807c478bd9Sstevel@tonic-gate rootpp = page_numtopp_nolock(pfn); 51817c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[3]); 51827c478bd9Sstevel@tonic-gate ASSERT(rootpp->p_vnode != NULL); 51837c478bd9Sstevel@tonic-gate ASSERT(rootpp->p_szc == szc); 51847c478bd9Sstevel@tonic-gate } 51857c478bd9Sstevel@tonic-gate 51867c478bd9Sstevel@tonic-gate /* 51877c478bd9Sstevel@tonic-gate * We can't demote kernel pages since we can't hat_unload() 51887c478bd9Sstevel@tonic-gate * the mappings. 51897c478bd9Sstevel@tonic-gate */ 5190ad23a2dbSjohansen if (VN_ISKAS(rootpp->p_vnode)) 51917c478bd9Sstevel@tonic-gate return (0); 51927c478bd9Sstevel@tonic-gate 51937c478bd9Sstevel@tonic-gate /* 51947c478bd9Sstevel@tonic-gate * Attempt to lock all constituent pages except the page passed 51957c478bd9Sstevel@tonic-gate * in since it's already locked. 51967c478bd9Sstevel@tonic-gate */ 5197affbd3ccSkchow for (tpp = rootpp, i = 0; i < npgs; i++, tpp++) { 51987c478bd9Sstevel@tonic-gate ASSERT(!PP_ISFREE(tpp)); 51997c478bd9Sstevel@tonic-gate ASSERT(tpp->p_vnode != NULL); 52007c478bd9Sstevel@tonic-gate 52017c478bd9Sstevel@tonic-gate if (tpp != pp && !page_trylock(tpp, SE_EXCL)) 52027c478bd9Sstevel@tonic-gate break; 52037c478bd9Sstevel@tonic-gate ASSERT(tpp->p_szc == rootpp->p_szc); 52047c478bd9Sstevel@tonic-gate ASSERT(page_pptonum(tpp) == page_pptonum(rootpp) + i); 52057c478bd9Sstevel@tonic-gate } 52067c478bd9Sstevel@tonic-gate 52077c478bd9Sstevel@tonic-gate /* 5208db874c57Selowe * If we failed to lock them all then unlock what we have 5209db874c57Selowe * locked so far and bail. 52107c478bd9Sstevel@tonic-gate */ 52117c478bd9Sstevel@tonic-gate if (i < npgs) { 52127c478bd9Sstevel@tonic-gate tpp = rootpp; 52137c478bd9Sstevel@tonic-gate while (i-- > 0) { 52147c478bd9Sstevel@tonic-gate if (tpp != pp) 52157c478bd9Sstevel@tonic-gate page_unlock(tpp); 5216affbd3ccSkchow tpp++; 52177c478bd9Sstevel@tonic-gate } 52187c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[4]); 52197c478bd9Sstevel@tonic-gate return (0); 52207c478bd9Sstevel@tonic-gate } 52217c478bd9Sstevel@tonic-gate 5222affbd3ccSkchow for (tpp = rootpp, i = 0; i < npgs; i++, tpp++) { 52237c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(tpp)); 522407b65a64Saguzovsk ASSERT(tpp->p_slckcnt == 0); 5225db874c57Selowe (void) hat_pageunload(tpp, HAT_FORCE_PGUNLOAD); 52267c478bd9Sstevel@tonic-gate tpp->p_szc = 0; 52277c478bd9Sstevel@tonic-gate } 52287c478bd9Sstevel@tonic-gate 52297c478bd9Sstevel@tonic-gate /* 52307c478bd9Sstevel@tonic-gate * Unlock all pages except the page passed in. 52317c478bd9Sstevel@tonic-gate */ 5232affbd3ccSkchow for (tpp = rootpp, i = 0; i < npgs; i++, tpp++) { 52337c478bd9Sstevel@tonic-gate ASSERT(!hat_page_is_mapped(tpp)); 52347c478bd9Sstevel@tonic-gate if (tpp != pp) 52357c478bd9Sstevel@tonic-gate page_unlock(tpp); 52367c478bd9Sstevel@tonic-gate } 5237db874c57Selowe 52387c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_try_demote_pages[5]); 52397c478bd9Sstevel@tonic-gate return (1); 52407c478bd9Sstevel@tonic-gate } 52417c478bd9Sstevel@tonic-gate 52427c478bd9Sstevel@tonic-gate /* 52437c478bd9Sstevel@tonic-gate * Called by page_free() and page_destroy() to demote the page size code 52447c478bd9Sstevel@tonic-gate * (p_szc) to 0 (since we can't just put a single PAGESIZE page with non zero 52457c478bd9Sstevel@tonic-gate * p_szc on free list, neither can we just clear p_szc of a single page_t 52467c478bd9Sstevel@tonic-gate * within a large page since it will break other code that relies on p_szc 52477c478bd9Sstevel@tonic-gate * being the same for all page_t's of a large page). Anonymous pages should 52487c478bd9Sstevel@tonic-gate * never end up here because anon_map_getpages() cannot deal with p_szc 52497c478bd9Sstevel@tonic-gate * changes after a single constituent page is locked. While anonymous or 52507c478bd9Sstevel@tonic-gate * kernel large pages are demoted or freed the entire large page at a time 52517c478bd9Sstevel@tonic-gate * with all constituent pages locked EXCL for the file system pages we 52527c478bd9Sstevel@tonic-gate * have to be able to demote a large page (i.e. decrease all constituent pages 52537c478bd9Sstevel@tonic-gate * p_szc) with only just an EXCL lock on one of constituent pages. The reason 52547c478bd9Sstevel@tonic-gate * we can easily deal with anonymous page demotion the entire large page at a 52557c478bd9Sstevel@tonic-gate * time is that those operation originate at address space level and concern 52567c478bd9Sstevel@tonic-gate * the entire large page region with actual demotion only done when pages are 52577c478bd9Sstevel@tonic-gate * not shared with any other processes (therefore we can always get EXCL lock 52587c478bd9Sstevel@tonic-gate * on all anonymous constituent pages after clearing segment page 52597c478bd9Sstevel@tonic-gate * cache). However file system pages can be truncated or invalidated at a 52607c478bd9Sstevel@tonic-gate * PAGESIZE level from the file system side and end up in page_free() or 52617c478bd9Sstevel@tonic-gate * page_destroy() (we also allow only part of the large page to be SOFTLOCKed 5262da6c28aaSamw * and therefore pageout should be able to demote a large page by EXCL locking 52637c478bd9Sstevel@tonic-gate * any constituent page that is not under SOFTLOCK). In those cases we cannot 52647c478bd9Sstevel@tonic-gate * rely on being able to lock EXCL all constituent pages. 52657c478bd9Sstevel@tonic-gate * 52667c478bd9Sstevel@tonic-gate * To prevent szc changes on file system pages one has to lock all constituent 52677c478bd9Sstevel@tonic-gate * pages at least SHARED (or call page_szc_lock()). The only subsystem that 52687c478bd9Sstevel@tonic-gate * doesn't rely on locking all constituent pages (or using page_szc_lock()) to 52697c478bd9Sstevel@tonic-gate * prevent szc changes is hat layer that uses its own page level mlist 52707c478bd9Sstevel@tonic-gate * locks. hat assumes that szc doesn't change after mlist lock for a page is 52717c478bd9Sstevel@tonic-gate * taken. Therefore we need to change szc under hat level locks if we only 52727c478bd9Sstevel@tonic-gate * have an EXCL lock on a single constituent page and hat still references any 52737c478bd9Sstevel@tonic-gate * of constituent pages. (Note we can't "ignore" hat layer by simply 52747c478bd9Sstevel@tonic-gate * hat_pageunload() all constituent pages without having EXCL locks on all of 52757c478bd9Sstevel@tonic-gate * constituent pages). We use hat_page_demote() call to safely demote szc of 52767c478bd9Sstevel@tonic-gate * all constituent pages under hat locks when we only have an EXCL lock on one 52777c478bd9Sstevel@tonic-gate * of constituent pages. 52787c478bd9Sstevel@tonic-gate * 52797c478bd9Sstevel@tonic-gate * This routine calls page_szc_lock() before calling hat_page_demote() to 52807c478bd9Sstevel@tonic-gate * allow segvn in one special case not to lock all constituent pages SHARED 5281da6c28aaSamw * before calling hat_memload_array() that relies on p_szc not changing even 52827c478bd9Sstevel@tonic-gate * before hat level mlist lock is taken. In that case segvn uses 5283da6c28aaSamw * page_szc_lock() to prevent hat_page_demote() changing p_szc values. 52847c478bd9Sstevel@tonic-gate * 52857c478bd9Sstevel@tonic-gate * Anonymous or kernel page demotion still has to lock all pages exclusively 52867c478bd9Sstevel@tonic-gate * and do hat_pageunload() on all constituent pages before demoting the page 52877c478bd9Sstevel@tonic-gate * therefore there's no need for anonymous or kernel page demotion to use 52887c478bd9Sstevel@tonic-gate * hat_page_demote() mechanism. 52897c478bd9Sstevel@tonic-gate * 52907c478bd9Sstevel@tonic-gate * hat_page_demote() removes all large mappings that map pp and then decreases 52917c478bd9Sstevel@tonic-gate * p_szc starting from the last constituent page of the large page. By working 52927c478bd9Sstevel@tonic-gate * from the tail of a large page in pfn decreasing order allows one looking at 52937c478bd9Sstevel@tonic-gate * the root page to know that hat_page_demote() is done for root's szc area. 52947c478bd9Sstevel@tonic-gate * e.g. if a root page has szc 1 one knows it only has to lock all constituent 52957c478bd9Sstevel@tonic-gate * pages within szc 1 area to prevent szc changes because hat_page_demote() 52967c478bd9Sstevel@tonic-gate * that started on this page when it had szc > 1 is done for this szc 1 area. 52977c478bd9Sstevel@tonic-gate * 5298da6c28aaSamw * We are guaranteed that all constituent pages of pp's large page belong to 52997c478bd9Sstevel@tonic-gate * the same vnode with the consecutive offsets increasing in the direction of 53007c478bd9Sstevel@tonic-gate * the pfn i.e. the identity of constituent pages can't change until their 53017c478bd9Sstevel@tonic-gate * p_szc is decreased. Therefore it's safe for hat_page_demote() to remove 53027c478bd9Sstevel@tonic-gate * large mappings to pp even though we don't lock any constituent page except 53037c478bd9Sstevel@tonic-gate * pp (i.e. we won't unload e.g. kernel locked page). 53047c478bd9Sstevel@tonic-gate */ 53057c478bd9Sstevel@tonic-gate static void 53067c478bd9Sstevel@tonic-gate page_demote_vp_pages(page_t *pp) 53077c478bd9Sstevel@tonic-gate { 53087c478bd9Sstevel@tonic-gate kmutex_t *mtx; 53097c478bd9Sstevel@tonic-gate 53107c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(pp)); 53117c478bd9Sstevel@tonic-gate ASSERT(!PP_ISFREE(pp)); 53127c478bd9Sstevel@tonic-gate ASSERT(pp->p_vnode != NULL); 53137c478bd9Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(pp->p_vnode)); 5314ad23a2dbSjohansen ASSERT(!PP_ISKAS(pp)); 53157c478bd9Sstevel@tonic-gate 53167c478bd9Sstevel@tonic-gate VM_STAT_ADD(pagecnt.pc_demote_pages[0]); 53177c478bd9Sstevel@tonic-gate 53187c478bd9Sstevel@tonic-gate mtx = page_szc_lock(pp); 53197c478bd9Sstevel@tonic-gate if (mtx != NULL) { 53207c478bd9Sstevel@tonic-gate hat_page_demote(pp); 53217c478bd9Sstevel@tonic-gate mutex_exit(mtx); 53227c478bd9Sstevel@tonic-gate } 53237c478bd9Sstevel@tonic-gate ASSERT(pp->p_szc == 0); 53247c478bd9Sstevel@tonic-gate } 53257c478bd9Sstevel@tonic-gate 53267c478bd9Sstevel@tonic-gate /* 53277c478bd9Sstevel@tonic-gate * Mark any existing pages for migration in the given range 53287c478bd9Sstevel@tonic-gate */ 53297c478bd9Sstevel@tonic-gate void 53307c478bd9Sstevel@tonic-gate page_mark_migrate(struct seg *seg, caddr_t addr, size_t len, 53317c478bd9Sstevel@tonic-gate struct anon_map *amp, ulong_t anon_index, vnode_t *vp, 53327c478bd9Sstevel@tonic-gate u_offset_t vnoff, int rflag) 53337c478bd9Sstevel@tonic-gate { 53347c478bd9Sstevel@tonic-gate struct anon *ap; 53357c478bd9Sstevel@tonic-gate vnode_t *curvp; 53367c478bd9Sstevel@tonic-gate lgrp_t *from; 53377c478bd9Sstevel@tonic-gate pgcnt_t i; 53387c478bd9Sstevel@tonic-gate pgcnt_t nlocked; 53397c478bd9Sstevel@tonic-gate u_offset_t off; 53407c478bd9Sstevel@tonic-gate pfn_t pfn; 53417c478bd9Sstevel@tonic-gate size_t pgsz; 53427c478bd9Sstevel@tonic-gate size_t segpgsz; 53437c478bd9Sstevel@tonic-gate pgcnt_t pages; 53447c478bd9Sstevel@tonic-gate uint_t pszc; 53457c478bd9Sstevel@tonic-gate page_t **ppa; 53467c478bd9Sstevel@tonic-gate pgcnt_t ppa_nentries; 53477c478bd9Sstevel@tonic-gate page_t *pp; 53487c478bd9Sstevel@tonic-gate caddr_t va; 53497c478bd9Sstevel@tonic-gate ulong_t an_idx; 53507c478bd9Sstevel@tonic-gate anon_sync_obj_t cookie; 53517c478bd9Sstevel@tonic-gate 53527c478bd9Sstevel@tonic-gate ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 53537c478bd9Sstevel@tonic-gate 53547c478bd9Sstevel@tonic-gate /* 53557c478bd9Sstevel@tonic-gate * Don't do anything if don't need to do lgroup optimizations 53567c478bd9Sstevel@tonic-gate * on this system 53577c478bd9Sstevel@tonic-gate */ 53587c478bd9Sstevel@tonic-gate if (!lgrp_optimizations()) 53597c478bd9Sstevel@tonic-gate return; 53607c478bd9Sstevel@tonic-gate 53617c478bd9Sstevel@tonic-gate /* 53627c478bd9Sstevel@tonic-gate * Align address and length to (potentially large) page boundary 53637c478bd9Sstevel@tonic-gate */ 53647c478bd9Sstevel@tonic-gate segpgsz = page_get_pagesize(seg->s_szc); 53657c478bd9Sstevel@tonic-gate addr = (caddr_t)P2ALIGN((uintptr_t)addr, segpgsz); 53667c478bd9Sstevel@tonic-gate if (rflag) 53677c478bd9Sstevel@tonic-gate len = P2ROUNDUP(len, segpgsz); 53687c478bd9Sstevel@tonic-gate 53697c478bd9Sstevel@tonic-gate /* 5370da6c28aaSamw * Allocate page array to accommodate largest page size 53717c478bd9Sstevel@tonic-gate */ 53727c478bd9Sstevel@tonic-gate pgsz = page_get_pagesize(page_num_pagesizes() - 1); 53737c478bd9Sstevel@tonic-gate ppa_nentries = btop(pgsz); 53747c478bd9Sstevel@tonic-gate ppa = kmem_zalloc(ppa_nentries * sizeof (page_t *), KM_SLEEP); 53757c478bd9Sstevel@tonic-gate 53767c478bd9Sstevel@tonic-gate /* 53777c478bd9Sstevel@tonic-gate * Do one (large) page at a time 53787c478bd9Sstevel@tonic-gate */ 53797c478bd9Sstevel@tonic-gate va = addr; 53807c478bd9Sstevel@tonic-gate while (va < addr + len) { 53817c478bd9Sstevel@tonic-gate /* 53827c478bd9Sstevel@tonic-gate * Lookup (root) page for vnode and offset corresponding to 53837c478bd9Sstevel@tonic-gate * this virtual address 53847c478bd9Sstevel@tonic-gate * Try anonmap first since there may be copy-on-write 53857c478bd9Sstevel@tonic-gate * pages, but initialize vnode pointer and offset using 53867c478bd9Sstevel@tonic-gate * vnode arguments just in case there isn't an amp. 53877c478bd9Sstevel@tonic-gate */ 53887c478bd9Sstevel@tonic-gate curvp = vp; 53897c478bd9Sstevel@tonic-gate off = vnoff + va - seg->s_base; 53907c478bd9Sstevel@tonic-gate if (amp) { 53917c478bd9Sstevel@tonic-gate ANON_LOCK_ENTER(&->a_rwlock, RW_READER); 53927c478bd9Sstevel@tonic-gate an_idx = anon_index + seg_page(seg, va); 53937c478bd9Sstevel@tonic-gate anon_array_enter(amp, an_idx, &cookie); 53947c478bd9Sstevel@tonic-gate ap = anon_get_ptr(amp->ahp, an_idx); 53957c478bd9Sstevel@tonic-gate if (ap) 53967c478bd9Sstevel@tonic-gate swap_xlate(ap, &curvp, &off); 53977c478bd9Sstevel@tonic-gate anon_array_exit(&cookie); 53987c478bd9Sstevel@tonic-gate ANON_LOCK_EXIT(&->a_rwlock); 53997c478bd9Sstevel@tonic-gate } 54007c478bd9Sstevel@tonic-gate 54017c478bd9Sstevel@tonic-gate pp = NULL; 54027c478bd9Sstevel@tonic-gate if (curvp) 54037c478bd9Sstevel@tonic-gate pp = page_lookup(curvp, off, SE_SHARED); 54047c478bd9Sstevel@tonic-gate 54057c478bd9Sstevel@tonic-gate /* 54067c478bd9Sstevel@tonic-gate * If there isn't a page at this virtual address, 54077c478bd9Sstevel@tonic-gate * skip to next page 54087c478bd9Sstevel@tonic-gate */ 54097c478bd9Sstevel@tonic-gate if (pp == NULL) { 54107c478bd9Sstevel@tonic-gate va += PAGESIZE; 54117c478bd9Sstevel@tonic-gate continue; 54127c478bd9Sstevel@tonic-gate } 54137c478bd9Sstevel@tonic-gate 54147c478bd9Sstevel@tonic-gate /* 54157c478bd9Sstevel@tonic-gate * Figure out which lgroup this page is in for kstats 54167c478bd9Sstevel@tonic-gate */ 54177c478bd9Sstevel@tonic-gate pfn = page_pptonum(pp); 54187c478bd9Sstevel@tonic-gate from = lgrp_pfn_to_lgrp(pfn); 54197c478bd9Sstevel@tonic-gate 54207c478bd9Sstevel@tonic-gate /* 54217c478bd9Sstevel@tonic-gate * Get page size, and round up and skip to next page boundary 54227c478bd9Sstevel@tonic-gate * if unaligned address 54237c478bd9Sstevel@tonic-gate */ 54247c478bd9Sstevel@tonic-gate pszc = pp->p_szc; 54257c478bd9Sstevel@tonic-gate pgsz = page_get_pagesize(pszc); 54267c478bd9Sstevel@tonic-gate pages = btop(pgsz); 54277c478bd9Sstevel@tonic-gate if (!IS_P2ALIGNED(va, pgsz) || 54287c478bd9Sstevel@tonic-gate !IS_P2ALIGNED(pfn, pages) || 54297c478bd9Sstevel@tonic-gate pgsz > segpgsz) { 54307c478bd9Sstevel@tonic-gate pgsz = MIN(pgsz, segpgsz); 54317c478bd9Sstevel@tonic-gate page_unlock(pp); 54327c478bd9Sstevel@tonic-gate i = btop(P2END((uintptr_t)va, pgsz) - 54337c478bd9Sstevel@tonic-gate (uintptr_t)va); 54347c478bd9Sstevel@tonic-gate va = (caddr_t)P2END((uintptr_t)va, pgsz); 54357c478bd9Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, i); 54367c478bd9Sstevel@tonic-gate continue; 54377c478bd9Sstevel@tonic-gate } 54387c478bd9Sstevel@tonic-gate 54397c478bd9Sstevel@tonic-gate /* 54407c478bd9Sstevel@tonic-gate * Upgrade to exclusive lock on page 54417c478bd9Sstevel@tonic-gate */ 54427c478bd9Sstevel@tonic-gate if (!page_tryupgrade(pp)) { 54437c478bd9Sstevel@tonic-gate page_unlock(pp); 54447c478bd9Sstevel@tonic-gate va += pgsz; 54457c478bd9Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, 54467c478bd9Sstevel@tonic-gate btop(pgsz)); 54477c478bd9Sstevel@tonic-gate continue; 54487c478bd9Sstevel@tonic-gate } 54497c478bd9Sstevel@tonic-gate 54507c478bd9Sstevel@tonic-gate /* 54517c478bd9Sstevel@tonic-gate * Remember pages locked exclusively and how many 54527c478bd9Sstevel@tonic-gate */ 54537c478bd9Sstevel@tonic-gate ppa[0] = pp; 54547c478bd9Sstevel@tonic-gate nlocked = 1; 54557c478bd9Sstevel@tonic-gate 54567c478bd9Sstevel@tonic-gate /* 54577c478bd9Sstevel@tonic-gate * Lock constituent pages if this is large page 54587c478bd9Sstevel@tonic-gate */ 54597c478bd9Sstevel@tonic-gate if (pages > 1) { 54607c478bd9Sstevel@tonic-gate /* 54617c478bd9Sstevel@tonic-gate * Lock all constituents except root page, since it 54627c478bd9Sstevel@tonic-gate * should be locked already. 54637c478bd9Sstevel@tonic-gate */ 54647c478bd9Sstevel@tonic-gate for (i = 1; i < pages; i++) { 5465affbd3ccSkchow pp++; 54667c478bd9Sstevel@tonic-gate if (!page_trylock(pp, SE_EXCL)) { 54677c478bd9Sstevel@tonic-gate break; 54687c478bd9Sstevel@tonic-gate } 54697c478bd9Sstevel@tonic-gate if (PP_ISFREE(pp) || 54707c478bd9Sstevel@tonic-gate pp->p_szc != pszc) { 54717c478bd9Sstevel@tonic-gate /* 54727c478bd9Sstevel@tonic-gate * hat_page_demote() raced in with us. 54737c478bd9Sstevel@tonic-gate */ 54747c478bd9Sstevel@tonic-gate ASSERT(!IS_SWAPFSVP(curvp)); 54757c478bd9Sstevel@tonic-gate page_unlock(pp); 54767c478bd9Sstevel@tonic-gate break; 54777c478bd9Sstevel@tonic-gate } 54787c478bd9Sstevel@tonic-gate ppa[nlocked] = pp; 54797c478bd9Sstevel@tonic-gate nlocked++; 54807c478bd9Sstevel@tonic-gate } 54817c478bd9Sstevel@tonic-gate } 54827c478bd9Sstevel@tonic-gate 54837c478bd9Sstevel@tonic-gate /* 54847c478bd9Sstevel@tonic-gate * If all constituent pages couldn't be locked, 54857c478bd9Sstevel@tonic-gate * unlock pages locked so far and skip to next page. 54867c478bd9Sstevel@tonic-gate */ 54877c478bd9Sstevel@tonic-gate if (nlocked != pages) { 54887c478bd9Sstevel@tonic-gate for (i = 0; i < nlocked; i++) 54897c478bd9Sstevel@tonic-gate page_unlock(ppa[i]); 54907c478bd9Sstevel@tonic-gate va += pgsz; 54917c478bd9Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_FAIL_PGS, 54927c478bd9Sstevel@tonic-gate btop(pgsz)); 54937c478bd9Sstevel@tonic-gate continue; 54947c478bd9Sstevel@tonic-gate } 54957c478bd9Sstevel@tonic-gate 54967c478bd9Sstevel@tonic-gate /* 54977c478bd9Sstevel@tonic-gate * hat_page_demote() can no longer happen 54987c478bd9Sstevel@tonic-gate * since last cons page had the right p_szc after 54997c478bd9Sstevel@tonic-gate * all cons pages were locked. all cons pages 55007c478bd9Sstevel@tonic-gate * should now have the same p_szc. 55017c478bd9Sstevel@tonic-gate */ 55027c478bd9Sstevel@tonic-gate 55037c478bd9Sstevel@tonic-gate /* 55047c478bd9Sstevel@tonic-gate * All constituent pages locked successfully, so mark 55057c478bd9Sstevel@tonic-gate * large page for migration and unload the mappings of 55067c478bd9Sstevel@tonic-gate * constituent pages, so a fault will occur on any part of the 55077c478bd9Sstevel@tonic-gate * large page 55087c478bd9Sstevel@tonic-gate */ 55097c478bd9Sstevel@tonic-gate PP_SETMIGRATE(ppa[0]); 55107c478bd9Sstevel@tonic-gate for (i = 0; i < nlocked; i++) { 55117c478bd9Sstevel@tonic-gate pp = ppa[i]; 55127c478bd9Sstevel@tonic-gate (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 55137c478bd9Sstevel@tonic-gate ASSERT(hat_page_getshare(pp) == 0); 55147c478bd9Sstevel@tonic-gate page_unlock(pp); 55157c478bd9Sstevel@tonic-gate } 55167c478bd9Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PMM_PGS, nlocked); 55177c478bd9Sstevel@tonic-gate 55187c478bd9Sstevel@tonic-gate va += pgsz; 55197c478bd9Sstevel@tonic-gate } 55207c478bd9Sstevel@tonic-gate kmem_free(ppa, ppa_nentries * sizeof (page_t *)); 55217c478bd9Sstevel@tonic-gate } 55227c478bd9Sstevel@tonic-gate 55237c478bd9Sstevel@tonic-gate /* 55247c478bd9Sstevel@tonic-gate * Migrate any pages that have been marked for migration in the given range 55257c478bd9Sstevel@tonic-gate */ 55267c478bd9Sstevel@tonic-gate void 55277c478bd9Sstevel@tonic-gate page_migrate( 55287c478bd9Sstevel@tonic-gate struct seg *seg, 55297c478bd9Sstevel@tonic-gate caddr_t addr, 55307c478bd9Sstevel@tonic-gate page_t **ppa, 55317c478bd9Sstevel@tonic-gate pgcnt_t npages) 55327c478bd9Sstevel@tonic-gate { 55337c478bd9Sstevel@tonic-gate lgrp_t *from; 55347c478bd9Sstevel@tonic-gate lgrp_t *to; 55357c478bd9Sstevel@tonic-gate page_t *newpp; 55367c478bd9Sstevel@tonic-gate page_t *pp; 55377c478bd9Sstevel@tonic-gate pfn_t pfn; 55387c478bd9Sstevel@tonic-gate size_t pgsz; 55397c478bd9Sstevel@tonic-gate spgcnt_t page_cnt; 55407c478bd9Sstevel@tonic-gate spgcnt_t i; 55417c478bd9Sstevel@tonic-gate uint_t pszc; 55427c478bd9Sstevel@tonic-gate 55437c478bd9Sstevel@tonic-gate ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock)); 55447c478bd9Sstevel@tonic-gate 55457c478bd9Sstevel@tonic-gate while (npages > 0) { 55467c478bd9Sstevel@tonic-gate pp = *ppa; 55477c478bd9Sstevel@tonic-gate pszc = pp->p_szc; 55487c478bd9Sstevel@tonic-gate pgsz = page_get_pagesize(pszc); 55497c478bd9Sstevel@tonic-gate page_cnt = btop(pgsz); 55507c478bd9Sstevel@tonic-gate 55517c478bd9Sstevel@tonic-gate /* 55527c478bd9Sstevel@tonic-gate * Check to see whether this page is marked for migration 55537c478bd9Sstevel@tonic-gate * 55547c478bd9Sstevel@tonic-gate * Assume that root page of large page is marked for 55557c478bd9Sstevel@tonic-gate * migration and none of the other constituent pages 55567c478bd9Sstevel@tonic-gate * are marked. This really simplifies clearing the 55577c478bd9Sstevel@tonic-gate * migrate bit by not having to clear it from each 55587c478bd9Sstevel@tonic-gate * constituent page. 55597c478bd9Sstevel@tonic-gate * 55607c478bd9Sstevel@tonic-gate * note we don't want to relocate an entire large page if 55617c478bd9Sstevel@tonic-gate * someone is only using one subpage. 55627c478bd9Sstevel@tonic-gate */ 55637c478bd9Sstevel@tonic-gate if (npages < page_cnt) 55647c478bd9Sstevel@tonic-gate break; 55657c478bd9Sstevel@tonic-gate 55667c478bd9Sstevel@tonic-gate /* 55677c478bd9Sstevel@tonic-gate * Is it marked for migration? 55687c478bd9Sstevel@tonic-gate */ 55697c478bd9Sstevel@tonic-gate if (!PP_ISMIGRATE(pp)) 55707c478bd9Sstevel@tonic-gate goto next; 55717c478bd9Sstevel@tonic-gate 55727c478bd9Sstevel@tonic-gate /* 55737c478bd9Sstevel@tonic-gate * Determine lgroups that page is being migrated between 55747c478bd9Sstevel@tonic-gate */ 55757c478bd9Sstevel@tonic-gate pfn = page_pptonum(pp); 55767c478bd9Sstevel@tonic-gate if (!IS_P2ALIGNED(pfn, page_cnt)) { 55777c478bd9Sstevel@tonic-gate break; 55787c478bd9Sstevel@tonic-gate } 55797c478bd9Sstevel@tonic-gate from = lgrp_pfn_to_lgrp(pfn); 55807c478bd9Sstevel@tonic-gate to = lgrp_mem_choose(seg, addr, pgsz); 55817c478bd9Sstevel@tonic-gate 55827c478bd9Sstevel@tonic-gate /* 55837c478bd9Sstevel@tonic-gate * Need to get exclusive lock's to migrate 55847c478bd9Sstevel@tonic-gate */ 55857c478bd9Sstevel@tonic-gate for (i = 0; i < page_cnt; i++) { 55867c478bd9Sstevel@tonic-gate ASSERT(PAGE_LOCKED(ppa[i])); 55877c478bd9Sstevel@tonic-gate if (page_pptonum(ppa[i]) != pfn + i || 55887c478bd9Sstevel@tonic-gate ppa[i]->p_szc != pszc) { 55897c478bd9Sstevel@tonic-gate break; 55907c478bd9Sstevel@tonic-gate } 55917c478bd9Sstevel@tonic-gate if (!page_tryupgrade(ppa[i])) { 55927c478bd9Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, 55937c478bd9Sstevel@tonic-gate LGRP_PM_FAIL_LOCK_PGS, 55947c478bd9Sstevel@tonic-gate page_cnt); 55957c478bd9Sstevel@tonic-gate break; 55967c478bd9Sstevel@tonic-gate } 55976bc16138Sjj 55986bc16138Sjj /* 55996bc16138Sjj * Check to see whether we are trying to migrate 56006bc16138Sjj * page to lgroup where it is allocated already. 56016bc16138Sjj * If so, clear the migrate bit and skip to next 56026bc16138Sjj * page. 56036bc16138Sjj */ 56046bc16138Sjj if (i == 0 && to == from) { 56056bc16138Sjj PP_CLRMIGRATE(ppa[0]); 56066bc16138Sjj page_downgrade(ppa[0]); 56076bc16138Sjj goto next; 56086bc16138Sjj } 56097c478bd9Sstevel@tonic-gate } 56106bc16138Sjj 56116bc16138Sjj /* 56126bc16138Sjj * If all constituent pages couldn't be locked, 56136bc16138Sjj * unlock pages locked so far and skip to next page. 56146bc16138Sjj */ 56157c478bd9Sstevel@tonic-gate if (i != page_cnt) { 56167c478bd9Sstevel@tonic-gate while (--i != -1) { 56177c478bd9Sstevel@tonic-gate page_downgrade(ppa[i]); 56187c478bd9Sstevel@tonic-gate } 56197c478bd9Sstevel@tonic-gate goto next; 56207c478bd9Sstevel@tonic-gate } 56217c478bd9Sstevel@tonic-gate 56227c478bd9Sstevel@tonic-gate (void) page_create_wait(page_cnt, PG_WAIT); 56237c478bd9Sstevel@tonic-gate newpp = page_get_replacement_page(pp, to, PGR_SAMESZC); 56247c478bd9Sstevel@tonic-gate if (newpp == NULL) { 56257c478bd9Sstevel@tonic-gate page_create_putback(page_cnt); 56267c478bd9Sstevel@tonic-gate for (i = 0; i < page_cnt; i++) { 56277c478bd9Sstevel@tonic-gate page_downgrade(ppa[i]); 56287c478bd9Sstevel@tonic-gate } 56297c478bd9Sstevel@tonic-gate lgrp_stat_add(to->lgrp_id, LGRP_PM_FAIL_ALLOC_PGS, 56307c478bd9Sstevel@tonic-gate page_cnt); 56317c478bd9Sstevel@tonic-gate goto next; 56327c478bd9Sstevel@tonic-gate } 56337c478bd9Sstevel@tonic-gate ASSERT(newpp->p_szc == pszc); 56347c478bd9Sstevel@tonic-gate /* 56357c478bd9Sstevel@tonic-gate * Clear migrate bit and relocate page 56367c478bd9Sstevel@tonic-gate */ 56377c478bd9Sstevel@tonic-gate PP_CLRMIGRATE(pp); 56387c478bd9Sstevel@tonic-gate if (page_relocate(&pp, &newpp, 0, 1, &page_cnt, to)) { 56397c478bd9Sstevel@tonic-gate panic("page_migrate: page_relocate failed"); 56407c478bd9Sstevel@tonic-gate } 56417c478bd9Sstevel@tonic-gate ASSERT(page_cnt * PAGESIZE == pgsz); 56427c478bd9Sstevel@tonic-gate 56437c478bd9Sstevel@tonic-gate /* 56447c478bd9Sstevel@tonic-gate * Keep stats for number of pages migrated from and to 56457c478bd9Sstevel@tonic-gate * each lgroup 56467c478bd9Sstevel@tonic-gate */ 56477c478bd9Sstevel@tonic-gate lgrp_stat_add(from->lgrp_id, LGRP_PM_SRC_PGS, page_cnt); 56487c478bd9Sstevel@tonic-gate lgrp_stat_add(to->lgrp_id, LGRP_PM_DEST_PGS, page_cnt); 56497c478bd9Sstevel@tonic-gate /* 56507c478bd9Sstevel@tonic-gate * update the page_t array we were passed in and 56517c478bd9Sstevel@tonic-gate * unlink constituent pages of a large page. 56527c478bd9Sstevel@tonic-gate */ 56537c478bd9Sstevel@tonic-gate for (i = 0; i < page_cnt; ++i, ++pp) { 56547c478bd9Sstevel@tonic-gate ASSERT(PAGE_EXCL(newpp)); 56557c478bd9Sstevel@tonic-gate ASSERT(newpp->p_szc == pszc); 56567c478bd9Sstevel@tonic-gate ppa[i] = newpp; 56577c478bd9Sstevel@tonic-gate pp = newpp; 56587c478bd9Sstevel@tonic-gate page_sub(&newpp, pp); 56597c478bd9Sstevel@tonic-gate page_downgrade(pp); 56607c478bd9Sstevel@tonic-gate } 56617c478bd9Sstevel@tonic-gate ASSERT(newpp == NULL); 56627c478bd9Sstevel@tonic-gate next: 56637c478bd9Sstevel@tonic-gate addr += pgsz; 56647c478bd9Sstevel@tonic-gate ppa += page_cnt; 56657c478bd9Sstevel@tonic-gate npages -= page_cnt; 56667c478bd9Sstevel@tonic-gate } 56677c478bd9Sstevel@tonic-gate } 56687c478bd9Sstevel@tonic-gate 56697c478bd9Sstevel@tonic-gate ulong_t mem_waiters = 0; 56707c478bd9Sstevel@tonic-gate ulong_t max_count = 20; 56717c478bd9Sstevel@tonic-gate #define MAX_DELAY 0x1ff 56727c478bd9Sstevel@tonic-gate 56737c478bd9Sstevel@tonic-gate /* 56747c478bd9Sstevel@tonic-gate * Check if enough memory is available to proceed. 56757c478bd9Sstevel@tonic-gate * Depending on system configuration and how much memory is 56767c478bd9Sstevel@tonic-gate * reserved for swap we need to check against two variables. 56777c478bd9Sstevel@tonic-gate * e.g. on systems with little physical swap availrmem can be 56787c478bd9Sstevel@tonic-gate * more reliable indicator of how much memory is available. 56797c478bd9Sstevel@tonic-gate * On systems with large phys swap freemem can be better indicator. 56807c478bd9Sstevel@tonic-gate * If freemem drops below threshold level don't return an error 56817c478bd9Sstevel@tonic-gate * immediately but wake up pageout to free memory and block. 56827c478bd9Sstevel@tonic-gate * This is done number of times. If pageout is not able to free 56837c478bd9Sstevel@tonic-gate * memory within certain time return an error. 56847c478bd9Sstevel@tonic-gate * The same applies for availrmem but kmem_reap is used to 56857c478bd9Sstevel@tonic-gate * free memory. 56867c478bd9Sstevel@tonic-gate */ 56877c478bd9Sstevel@tonic-gate int 56887c478bd9Sstevel@tonic-gate page_mem_avail(pgcnt_t npages) 56897c478bd9Sstevel@tonic-gate { 56907c478bd9Sstevel@tonic-gate ulong_t count; 56917c478bd9Sstevel@tonic-gate 56927c478bd9Sstevel@tonic-gate #if defined(__i386) 56937c478bd9Sstevel@tonic-gate if (freemem > desfree + npages && 56947c478bd9Sstevel@tonic-gate availrmem > swapfs_reserve + npages && 56957c478bd9Sstevel@tonic-gate btop(vmem_size(heap_arena, VMEM_FREE)) > tune.t_minarmem + 56967c478bd9Sstevel@tonic-gate npages) 56977c478bd9Sstevel@tonic-gate return (1); 56987c478bd9Sstevel@tonic-gate #else 56997c478bd9Sstevel@tonic-gate if (freemem > desfree + npages && 57007c478bd9Sstevel@tonic-gate availrmem > swapfs_reserve + npages) 57017c478bd9Sstevel@tonic-gate return (1); 57027c478bd9Sstevel@tonic-gate #endif 57037c478bd9Sstevel@tonic-gate 57047c478bd9Sstevel@tonic-gate count = max_count; 57057c478bd9Sstevel@tonic-gate atomic_add_long(&mem_waiters, 1); 57067c478bd9Sstevel@tonic-gate 57077c478bd9Sstevel@tonic-gate while (freemem < desfree + npages && --count) { 57087c478bd9Sstevel@tonic-gate cv_signal(&proc_pageout->p_cv); 57097c478bd9Sstevel@tonic-gate if (delay_sig(hz + (mem_waiters & MAX_DELAY))) { 57107c478bd9Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 57117c478bd9Sstevel@tonic-gate return (0); 57127c478bd9Sstevel@tonic-gate } 57137c478bd9Sstevel@tonic-gate } 57147c478bd9Sstevel@tonic-gate if (count == 0) { 57157c478bd9Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 57167c478bd9Sstevel@tonic-gate return (0); 57177c478bd9Sstevel@tonic-gate } 57187c478bd9Sstevel@tonic-gate 57197c478bd9Sstevel@tonic-gate count = max_count; 57207c478bd9Sstevel@tonic-gate while (availrmem < swapfs_reserve + npages && --count) { 57217c478bd9Sstevel@tonic-gate kmem_reap(); 57227c478bd9Sstevel@tonic-gate if (delay_sig(hz + (mem_waiters & MAX_DELAY))) { 57237c478bd9Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 57247c478bd9Sstevel@tonic-gate return (0); 57257c478bd9Sstevel@tonic-gate } 57267c478bd9Sstevel@tonic-gate } 57277c478bd9Sstevel@tonic-gate atomic_add_long(&mem_waiters, -1); 57287c478bd9Sstevel@tonic-gate if (count == 0) 57297c478bd9Sstevel@tonic-gate return (0); 57307c478bd9Sstevel@tonic-gate 57317c478bd9Sstevel@tonic-gate #if defined(__i386) 57327c478bd9Sstevel@tonic-gate if (btop(vmem_size(heap_arena, VMEM_FREE)) < 57337c478bd9Sstevel@tonic-gate tune.t_minarmem + npages) 57347c478bd9Sstevel@tonic-gate return (0); 57357c478bd9Sstevel@tonic-gate #endif 57367c478bd9Sstevel@tonic-gate return (1); 57377c478bd9Sstevel@tonic-gate } 57387c478bd9Sstevel@tonic-gate 57393cff2f43Sstans #define MAX_CNT 60 /* max num of iterations */ 57403cff2f43Sstans /* 57413cff2f43Sstans * Reclaim/reserve availrmem for npages. 57423cff2f43Sstans * If there is not enough memory start reaping seg, kmem caches. 57433cff2f43Sstans * Start pageout scanner (via page_needfree()). 57443cff2f43Sstans * Exit after ~ MAX_CNT s regardless of how much memory has been released. 57453cff2f43Sstans * Note: There is no guarantee that any availrmem will be freed as 57463cff2f43Sstans * this memory typically is locked (kernel heap) or reserved for swap. 57473cff2f43Sstans * Also due to memory fragmentation kmem allocator may not be able 57483cff2f43Sstans * to free any memory (single user allocated buffer will prevent 57493cff2f43Sstans * freeing slab or a page). 57503cff2f43Sstans */ 57513cff2f43Sstans int 57523cff2f43Sstans page_reclaim_mem(pgcnt_t npages, pgcnt_t epages, int adjust) 57533cff2f43Sstans { 57543cff2f43Sstans int i = 0; 57553cff2f43Sstans int ret = 0; 57563cff2f43Sstans pgcnt_t deficit; 57573cff2f43Sstans pgcnt_t old_availrmem; 57583cff2f43Sstans 57593cff2f43Sstans mutex_enter(&freemem_lock); 57603cff2f43Sstans old_availrmem = availrmem - 1; 57613cff2f43Sstans while ((availrmem < tune.t_minarmem + npages + epages) && 57623cff2f43Sstans (old_availrmem < availrmem) && (i++ < MAX_CNT)) { 57633cff2f43Sstans old_availrmem = availrmem; 57643cff2f43Sstans deficit = tune.t_minarmem + npages + epages - availrmem; 57653cff2f43Sstans mutex_exit(&freemem_lock); 57663cff2f43Sstans page_needfree(deficit); 57673cff2f43Sstans kmem_reap(); 57683cff2f43Sstans delay(hz); 57693cff2f43Sstans page_needfree(-(spgcnt_t)deficit); 57703cff2f43Sstans mutex_enter(&freemem_lock); 57713cff2f43Sstans } 57723cff2f43Sstans 57733cff2f43Sstans if (adjust && (availrmem >= tune.t_minarmem + npages + epages)) { 57743cff2f43Sstans availrmem -= npages; 57753cff2f43Sstans ret = 1; 57763cff2f43Sstans } 57773cff2f43Sstans 57783cff2f43Sstans mutex_exit(&freemem_lock); 57793cff2f43Sstans 57803cff2f43Sstans return (ret); 57813cff2f43Sstans } 57827c478bd9Sstevel@tonic-gate 57837c478bd9Sstevel@tonic-gate /* 57847c478bd9Sstevel@tonic-gate * Search the memory segments to locate the desired page. Within a 57857c478bd9Sstevel@tonic-gate * segment, pages increase linearly with one page structure per 57867c478bd9Sstevel@tonic-gate * physical page frame (size PAGESIZE). The search begins 57877c478bd9Sstevel@tonic-gate * with the segment that was accessed last, to take advantage of locality. 57887c478bd9Sstevel@tonic-gate * If the hint misses, we start from the beginning of the sorted memseg list 57897c478bd9Sstevel@tonic-gate */ 57907c478bd9Sstevel@tonic-gate 57917c478bd9Sstevel@tonic-gate 57927c478bd9Sstevel@tonic-gate /* 57937c478bd9Sstevel@tonic-gate * Some data structures for pfn to pp lookup. 57947c478bd9Sstevel@tonic-gate */ 57957c478bd9Sstevel@tonic-gate ulong_t mhash_per_slot; 57967c478bd9Sstevel@tonic-gate struct memseg *memseg_hash[N_MEM_SLOTS]; 57977c478bd9Sstevel@tonic-gate 57987c478bd9Sstevel@tonic-gate page_t * 57997c478bd9Sstevel@tonic-gate page_numtopp_nolock(pfn_t pfnum) 58007c478bd9Sstevel@tonic-gate { 58017c478bd9Sstevel@tonic-gate struct memseg *seg; 58027c478bd9Sstevel@tonic-gate page_t *pp; 58032af6eb52SMichael Corcoran vm_cpu_data_t *vc; 58047c478bd9Sstevel@tonic-gate 58052af6eb52SMichael Corcoran /* 58062af6eb52SMichael Corcoran * We need to disable kernel preemption while referencing the 58072af6eb52SMichael Corcoran * cpu_vm_data field in order to prevent us from being switched to 58082af6eb52SMichael Corcoran * another cpu and trying to reference it after it has been freed. 58092af6eb52SMichael Corcoran * This will keep us on cpu and prevent it from being removed while 58102af6eb52SMichael Corcoran * we are still on it. 5811*9853d9e8SJason Beloro * 5812*9853d9e8SJason Beloro * We may be caching a memseg in vc_pnum_memseg/vc_pnext_memseg 5813*9853d9e8SJason Beloro * which is being resued by DR who will flush those references 5814*9853d9e8SJason Beloro * before modifying the reused memseg. See memseg_cpu_vm_flush(). 58152af6eb52SMichael Corcoran */ 58162af6eb52SMichael Corcoran kpreempt_disable(); 58172af6eb52SMichael Corcoran vc = CPU->cpu_vm_data; 5818affbd3ccSkchow ASSERT(vc != NULL); 58197c478bd9Sstevel@tonic-gate 58207c478bd9Sstevel@tonic-gate MEMSEG_STAT_INCR(nsearch); 58217c478bd9Sstevel@tonic-gate 58227c478bd9Sstevel@tonic-gate /* Try last winner first */ 5823affbd3ccSkchow if (((seg = vc->vc_pnum_memseg) != NULL) && 58246e4dd838Smec (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 58257c478bd9Sstevel@tonic-gate MEMSEG_STAT_INCR(nlastwon); 58267c478bd9Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 58272af6eb52SMichael Corcoran if (pp->p_pagenum == pfnum) { 58282af6eb52SMichael Corcoran kpreempt_enable(); 58297c478bd9Sstevel@tonic-gate return ((page_t *)pp); 58302af6eb52SMichael Corcoran } 58317c478bd9Sstevel@tonic-gate } 58327c478bd9Sstevel@tonic-gate 58337c478bd9Sstevel@tonic-gate /* Else Try hash */ 58347c478bd9Sstevel@tonic-gate if (((seg = memseg_hash[MEMSEG_PFN_HASH(pfnum)]) != NULL) && 58356e4dd838Smec (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 58367c478bd9Sstevel@tonic-gate MEMSEG_STAT_INCR(nhashwon); 5837affbd3ccSkchow vc->vc_pnum_memseg = seg; 58387c478bd9Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 58392af6eb52SMichael Corcoran if (pp->p_pagenum == pfnum) { 58402af6eb52SMichael Corcoran kpreempt_enable(); 58417c478bd9Sstevel@tonic-gate return ((page_t *)pp); 58422af6eb52SMichael Corcoran } 58437c478bd9Sstevel@tonic-gate } 58447c478bd9Sstevel@tonic-gate 58457c478bd9Sstevel@tonic-gate /* Else Brute force */ 58467c478bd9Sstevel@tonic-gate for (seg = memsegs; seg != NULL; seg = seg->next) { 58477c478bd9Sstevel@tonic-gate if (pfnum >= seg->pages_base && pfnum < seg->pages_end) { 5848affbd3ccSkchow vc->vc_pnum_memseg = seg; 58497c478bd9Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 5850*9853d9e8SJason Beloro if (pp->p_pagenum == pfnum) { 5851*9853d9e8SJason Beloro kpreempt_enable(); 5852*9853d9e8SJason Beloro return ((page_t *)pp); 5853*9853d9e8SJason Beloro } 58547c478bd9Sstevel@tonic-gate } 58557c478bd9Sstevel@tonic-gate } 5856affbd3ccSkchow vc->vc_pnum_memseg = NULL; 58572af6eb52SMichael Corcoran kpreempt_enable(); 58587c478bd9Sstevel@tonic-gate MEMSEG_STAT_INCR(nnotfound); 58597c478bd9Sstevel@tonic-gate return ((page_t *)NULL); 58607c478bd9Sstevel@tonic-gate 58617c478bd9Sstevel@tonic-gate } 58627c478bd9Sstevel@tonic-gate 58637c478bd9Sstevel@tonic-gate struct memseg * 58647c478bd9Sstevel@tonic-gate page_numtomemseg_nolock(pfn_t pfnum) 58657c478bd9Sstevel@tonic-gate { 58667c478bd9Sstevel@tonic-gate struct memseg *seg; 58677c478bd9Sstevel@tonic-gate page_t *pp; 58687c478bd9Sstevel@tonic-gate 5869*9853d9e8SJason Beloro /* 5870*9853d9e8SJason Beloro * We may be caching a memseg in vc_pnum_memseg/vc_pnext_memseg 5871*9853d9e8SJason Beloro * which is being resued by DR who will flush those references 5872*9853d9e8SJason Beloro * before modifying the reused memseg. See memseg_cpu_vm_flush(). 5873*9853d9e8SJason Beloro */ 5874*9853d9e8SJason Beloro kpreempt_disable(); 58757c478bd9Sstevel@tonic-gate /* Try hash */ 58767c478bd9Sstevel@tonic-gate if (((seg = memseg_hash[MEMSEG_PFN_HASH(pfnum)]) != NULL) && 58776e4dd838Smec (pfnum >= seg->pages_base) && (pfnum < seg->pages_end)) { 58787c478bd9Sstevel@tonic-gate pp = seg->pages + (pfnum - seg->pages_base); 5879*9853d9e8SJason Beloro if (pp->p_pagenum == pfnum) { 5880*9853d9e8SJason Beloro kpreempt_enable(); 58817c478bd9Sstevel@tonic-gate return (seg); 5882*9853d9e8SJason Beloro } 58837c478bd9Sstevel@tonic-gate } 58847c478bd9Sstevel@tonic-gate 58857c478bd9Sstevel@tonic-gate /* Else Brute force */ 58867c478bd9Sstevel@tonic-gate for (seg = memsegs; seg != NULL; seg = seg->next) { 58877c478bd9Sstevel@tonic-gate if (pfnum >= seg->pages_base && pfnum < seg->pages_end) { 5888*9853d9e8SJason Beloro pp = seg->pages + (pfnum - seg->pages_base); 5889*9853d9e8SJason Beloro if (pp->p_pagenum == pfnum) { 5890*9853d9e8SJason Beloro kpreempt_enable(); 5891*9853d9e8SJason Beloro return (seg); 5892*9853d9e8SJason Beloro } 58937c478bd9Sstevel@tonic-gate } 58947c478bd9Sstevel@tonic-gate } 5895*9853d9e8SJason Beloro kpreempt_enable(); 58967c478bd9Sstevel@tonic-gate return ((struct memseg *)NULL); 58977c478bd9Sstevel@tonic-gate } 58987c478bd9Sstevel@tonic-gate 58997c478bd9Sstevel@tonic-gate /* 59007c478bd9Sstevel@tonic-gate * Given a page and a count return the page struct that is 59017c478bd9Sstevel@tonic-gate * n structs away from the current one in the global page 59027c478bd9Sstevel@tonic-gate * list. 59037c478bd9Sstevel@tonic-gate * 59047c478bd9Sstevel@tonic-gate * This function wraps to the first page upon 59057c478bd9Sstevel@tonic-gate * reaching the end of the memseg list. 59067c478bd9Sstevel@tonic-gate */ 59077c478bd9Sstevel@tonic-gate page_t * 59087c478bd9Sstevel@tonic-gate page_nextn(page_t *pp, ulong_t n) 59097c478bd9Sstevel@tonic-gate { 59107c478bd9Sstevel@tonic-gate struct memseg *seg; 59117c478bd9Sstevel@tonic-gate page_t *ppn; 59122af6eb52SMichael Corcoran vm_cpu_data_t *vc; 59132af6eb52SMichael Corcoran 59142af6eb52SMichael Corcoran /* 59152af6eb52SMichael Corcoran * We need to disable kernel preemption while referencing the 59162af6eb52SMichael Corcoran * cpu_vm_data field in order to prevent us from being switched to 59172af6eb52SMichael Corcoran * another cpu and trying to reference it after it has been freed. 59182af6eb52SMichael Corcoran * This will keep us on cpu and prevent it from being removed while 59192af6eb52SMichael Corcoran * we are still on it. 5920*9853d9e8SJason Beloro * 5921*9853d9e8SJason Beloro * We may be caching a memseg in vc_pnum_memseg/vc_pnext_memseg 5922*9853d9e8SJason Beloro * which is being resued by DR who will flush those references 5923*9853d9e8SJason Beloro * before modifying the reused memseg. See memseg_cpu_vm_flush(). 59242af6eb52SMichael Corcoran */ 59252af6eb52SMichael Corcoran kpreempt_disable(); 59262af6eb52SMichael Corcoran vc = (vm_cpu_data_t *)CPU->cpu_vm_data; 59277c478bd9Sstevel@tonic-gate 5928affbd3ccSkchow ASSERT(vc != NULL); 5929affbd3ccSkchow 5930affbd3ccSkchow if (((seg = vc->vc_pnext_memseg) == NULL) || 59317c478bd9Sstevel@tonic-gate (seg->pages_base == seg->pages_end) || 59327c478bd9Sstevel@tonic-gate !(pp >= seg->pages && pp < seg->epages)) { 59337c478bd9Sstevel@tonic-gate 59347c478bd9Sstevel@tonic-gate for (seg = memsegs; seg; seg = seg->next) { 59357c478bd9Sstevel@tonic-gate if (pp >= seg->pages && pp < seg->epages) 59367c478bd9Sstevel@tonic-gate break; 59377c478bd9Sstevel@tonic-gate } 59387c478bd9Sstevel@tonic-gate 59397c478bd9Sstevel@tonic-gate if (seg == NULL) { 59407c478bd9Sstevel@tonic-gate /* Memory delete got in, return something valid. */ 59417c478bd9Sstevel@tonic-gate /* TODO: fix me. */ 59427c478bd9Sstevel@tonic-gate seg = memsegs; 59437c478bd9Sstevel@tonic-gate pp = seg->pages; 59447c478bd9Sstevel@tonic-gate } 59457c478bd9Sstevel@tonic-gate } 59467c478bd9Sstevel@tonic-gate 59477c478bd9Sstevel@tonic-gate /* check for wraparound - possible if n is large */ 59487c478bd9Sstevel@tonic-gate while ((ppn = (pp + n)) >= seg->epages || ppn < pp) { 59497c478bd9Sstevel@tonic-gate n -= seg->epages - pp; 59507c478bd9Sstevel@tonic-gate seg = seg->next; 59517c478bd9Sstevel@tonic-gate if (seg == NULL) 59527c478bd9Sstevel@tonic-gate seg = memsegs; 59537c478bd9Sstevel@tonic-gate pp = seg->pages; 59547c478bd9Sstevel@tonic-gate } 5955affbd3ccSkchow vc->vc_pnext_memseg = seg; 59562af6eb52SMichael Corcoran kpreempt_enable(); 59577c478bd9Sstevel@tonic-gate return (ppn); 59587c478bd9Sstevel@tonic-gate } 59597c478bd9Sstevel@tonic-gate 59607c478bd9Sstevel@tonic-gate /* 59617c478bd9Sstevel@tonic-gate * Initialize for a loop using page_next_scan_large(). 59627c478bd9Sstevel@tonic-gate */ 59637c478bd9Sstevel@tonic-gate page_t * 59647c478bd9Sstevel@tonic-gate page_next_scan_init(void **cookie) 59657c478bd9Sstevel@tonic-gate { 59667c478bd9Sstevel@tonic-gate ASSERT(cookie != NULL); 59677c478bd9Sstevel@tonic-gate *cookie = (void *)memsegs; 59687c478bd9Sstevel@tonic-gate return ((page_t *)memsegs->pages); 59697c478bd9Sstevel@tonic-gate } 59707c478bd9Sstevel@tonic-gate 59717c478bd9Sstevel@tonic-gate /* 59727c478bd9Sstevel@tonic-gate * Return the next page in a scan of page_t's, assuming we want 59737c478bd9Sstevel@tonic-gate * to skip over sub-pages within larger page sizes. 59747c478bd9Sstevel@tonic-gate * 59757c478bd9Sstevel@tonic-gate * The cookie is used to keep track of the current memseg. 59767c478bd9Sstevel@tonic-gate */ 59777c478bd9Sstevel@tonic-gate page_t * 59787c478bd9Sstevel@tonic-gate page_next_scan_large( 59797c478bd9Sstevel@tonic-gate page_t *pp, 59807c478bd9Sstevel@tonic-gate ulong_t *n, 59817c478bd9Sstevel@tonic-gate void **cookie) 59827c478bd9Sstevel@tonic-gate { 59837c478bd9Sstevel@tonic-gate struct memseg *seg = (struct memseg *)*cookie; 59847c478bd9Sstevel@tonic-gate page_t *new_pp; 59857c478bd9Sstevel@tonic-gate ulong_t cnt; 59867c478bd9Sstevel@tonic-gate pfn_t pfn; 59877c478bd9Sstevel@tonic-gate 59887c478bd9Sstevel@tonic-gate 59897c478bd9Sstevel@tonic-gate /* 59907c478bd9Sstevel@tonic-gate * get the count of page_t's to skip based on the page size 59917c478bd9Sstevel@tonic-gate */ 59927c478bd9Sstevel@tonic-gate ASSERT(pp != NULL); 59937c478bd9Sstevel@tonic-gate if (pp->p_szc == 0) { 59947c478bd9Sstevel@tonic-gate cnt = 1; 59957c478bd9Sstevel@tonic-gate } else { 59967c478bd9Sstevel@tonic-gate pfn = page_pptonum(pp); 59977c478bd9Sstevel@tonic-gate cnt = page_get_pagecnt(pp->p_szc); 59987c478bd9Sstevel@tonic-gate cnt -= pfn & (cnt - 1); 59997c478bd9Sstevel@tonic-gate } 60007c478bd9Sstevel@tonic-gate *n += cnt; 60017c478bd9Sstevel@tonic-gate new_pp = pp + cnt; 60027c478bd9Sstevel@tonic-gate 60037c478bd9Sstevel@tonic-gate /* 60047c478bd9Sstevel@tonic-gate * Catch if we went past the end of the current memory segment. If so, 60057c478bd9Sstevel@tonic-gate * just move to the next segment with pages. 60067c478bd9Sstevel@tonic-gate */ 6007*9853d9e8SJason Beloro if (new_pp >= seg->epages || seg->pages_base == seg->pages_end) { 60087c478bd9Sstevel@tonic-gate do { 60097c478bd9Sstevel@tonic-gate seg = seg->next; 60107c478bd9Sstevel@tonic-gate if (seg == NULL) 60117c478bd9Sstevel@tonic-gate seg = memsegs; 6012*9853d9e8SJason Beloro } while (seg->pages_base == seg->pages_end); 60137c478bd9Sstevel@tonic-gate new_pp = seg->pages; 60147c478bd9Sstevel@tonic-gate *cookie = (void *)seg; 60157c478bd9Sstevel@tonic-gate } 60167c478bd9Sstevel@tonic-gate 60177c478bd9Sstevel@tonic-gate return (new_pp); 60187c478bd9Sstevel@tonic-gate } 60197c478bd9Sstevel@tonic-gate 60207c478bd9Sstevel@tonic-gate 60217c478bd9Sstevel@tonic-gate /* 60227c478bd9Sstevel@tonic-gate * Returns next page in list. Note: this function wraps 60237c478bd9Sstevel@tonic-gate * to the first page in the list upon reaching the end 60247c478bd9Sstevel@tonic-gate * of the list. Callers should be aware of this fact. 60257c478bd9Sstevel@tonic-gate */ 60267c478bd9Sstevel@tonic-gate 60277c478bd9Sstevel@tonic-gate /* We should change this be a #define */ 60287c478bd9Sstevel@tonic-gate 60297c478bd9Sstevel@tonic-gate page_t * 60307c478bd9Sstevel@tonic-gate page_next(page_t *pp) 60317c478bd9Sstevel@tonic-gate { 60327c478bd9Sstevel@tonic-gate return (page_nextn(pp, 1)); 60337c478bd9Sstevel@tonic-gate } 60347c478bd9Sstevel@tonic-gate 60357c478bd9Sstevel@tonic-gate page_t * 60367c478bd9Sstevel@tonic-gate page_first() 60377c478bd9Sstevel@tonic-gate { 60387c478bd9Sstevel@tonic-gate return ((page_t *)memsegs->pages); 60397c478bd9Sstevel@tonic-gate } 60407c478bd9Sstevel@tonic-gate 60417c478bd9Sstevel@tonic-gate 60427c478bd9Sstevel@tonic-gate /* 60437c478bd9Sstevel@tonic-gate * This routine is called at boot with the initial memory configuration 60447c478bd9Sstevel@tonic-gate * and when memory is added or removed. 60457c478bd9Sstevel@tonic-gate */ 60467c478bd9Sstevel@tonic-gate void 60477c478bd9Sstevel@tonic-gate build_pfn_hash() 60487c478bd9Sstevel@tonic-gate { 60497c478bd9Sstevel@tonic-gate pfn_t cur; 60507c478bd9Sstevel@tonic-gate pgcnt_t index; 60517c478bd9Sstevel@tonic-gate struct memseg *pseg; 60527c478bd9Sstevel@tonic-gate int i; 60537c478bd9Sstevel@tonic-gate 60547c478bd9Sstevel@tonic-gate /* 60557c478bd9Sstevel@tonic-gate * Clear memseg_hash array. 60567c478bd9Sstevel@tonic-gate * Since memory add/delete is designed to operate concurrently 60577c478bd9Sstevel@tonic-gate * with normal operation, the hash rebuild must be able to run 60587c478bd9Sstevel@tonic-gate * concurrently with page_numtopp_nolock(). To support this 60597c478bd9Sstevel@tonic-gate * functionality, assignments to memseg_hash array members must 60607c478bd9Sstevel@tonic-gate * be done atomically. 60617c478bd9Sstevel@tonic-gate * 60627c478bd9Sstevel@tonic-gate * NOTE: bzero() does not currently guarantee this for kernel 60637c478bd9Sstevel@tonic-gate * threads, and cannot be used here. 60647c478bd9Sstevel@tonic-gate */ 60657c478bd9Sstevel@tonic-gate for (i = 0; i < N_MEM_SLOTS; i++) 60667c478bd9Sstevel@tonic-gate memseg_hash[i] = NULL; 60677c478bd9Sstevel@tonic-gate 60687c478bd9Sstevel@tonic-gate hat_kpm_mseghash_clear(N_MEM_SLOTS); 60697c478bd9Sstevel@tonic-gate 60707c478bd9Sstevel@tonic-gate /* 60717c478bd9Sstevel@tonic-gate * Physmax is the last valid pfn. 60727c478bd9Sstevel@tonic-gate */ 60737c478bd9Sstevel@tonic-gate mhash_per_slot = (physmax + 1) >> MEM_HASH_SHIFT; 60747c478bd9Sstevel@tonic-gate for (pseg = memsegs; pseg != NULL; pseg = pseg->next) { 60757c478bd9Sstevel@tonic-gate index = MEMSEG_PFN_HASH(pseg->pages_base); 60767c478bd9Sstevel@tonic-gate cur = pseg->pages_base; 60777c478bd9Sstevel@tonic-gate do { 60787c478bd9Sstevel@tonic-gate if (index >= N_MEM_SLOTS) 60797c478bd9Sstevel@tonic-gate index = MEMSEG_PFN_HASH(cur); 60807c478bd9Sstevel@tonic-gate 60817c478bd9Sstevel@tonic-gate if (memseg_hash[index] == NULL || 60827c478bd9Sstevel@tonic-gate memseg_hash[index]->pages_base > pseg->pages_base) { 60837c478bd9Sstevel@tonic-gate memseg_hash[index] = pseg; 60847c478bd9Sstevel@tonic-gate hat_kpm_mseghash_update(index, pseg); 60857c478bd9Sstevel@tonic-gate } 60867c478bd9Sstevel@tonic-gate cur += mhash_per_slot; 60877c478bd9Sstevel@tonic-gate index++; 60887c478bd9Sstevel@tonic-gate } while (cur < pseg->pages_end); 60897c478bd9Sstevel@tonic-gate } 60907c478bd9Sstevel@tonic-gate } 60917c478bd9Sstevel@tonic-gate 60927c478bd9Sstevel@tonic-gate /* 60937c478bd9Sstevel@tonic-gate * Return the pagenum for the pp 60947c478bd9Sstevel@tonic-gate */ 60957c478bd9Sstevel@tonic-gate pfn_t 60967c478bd9Sstevel@tonic-gate page_pptonum(page_t *pp) 60977c478bd9Sstevel@tonic-gate { 60987c478bd9Sstevel@tonic-gate return (pp->p_pagenum); 60997c478bd9Sstevel@tonic-gate } 61007c478bd9Sstevel@tonic-gate 61017c478bd9Sstevel@tonic-gate /* 61027c478bd9Sstevel@tonic-gate * interface to the referenced and modified etc bits 61037c478bd9Sstevel@tonic-gate * in the PSM part of the page struct 61047c478bd9Sstevel@tonic-gate * when no locking is desired. 61057c478bd9Sstevel@tonic-gate */ 61067c478bd9Sstevel@tonic-gate void 61077c478bd9Sstevel@tonic-gate page_set_props(page_t *pp, uint_t flags) 61087c478bd9Sstevel@tonic-gate { 61097c478bd9Sstevel@tonic-gate ASSERT((flags & ~(P_MOD | P_REF | P_RO)) == 0); 61107c478bd9Sstevel@tonic-gate pp->p_nrm |= (uchar_t)flags; 61117c478bd9Sstevel@tonic-gate } 61127c478bd9Sstevel@tonic-gate 61132f0fcb93SJason Beloro extern void mach_sync_icache_pp(page_t *); 61142f0fcb93SJason Beloro #pragma weak mach_sync_icache_pp 61152f0fcb93SJason Beloro 61162f0fcb93SJason Beloro /* 61172f0fcb93SJason Beloro * Flush I-cache if the page is being reassigned. The hashout flag is 61182f0fcb93SJason Beloro * set when a page has been removed from a hash chain (i.e. vnode 61192f0fcb93SJason Beloro * pages). If the page stays on the hash chain there is a chance it 61202f0fcb93SJason Beloro * will be re-used, therefore there is no need to flush the 61212f0fcb93SJason Beloro * I-cache. However, if the page is being removed from a hash chain 61222f0fcb93SJason Beloro * then the page can be used for any new purpose, and the I-cache must 61232f0fcb93SJason Beloro * be flushed. 61242f0fcb93SJason Beloro */ 61252f0fcb93SJason Beloro /* ARGSUSED */ 61267c478bd9Sstevel@tonic-gate void 61272f0fcb93SJason Beloro page_clr_all_props(page_t *pp, int hashout) 61287c478bd9Sstevel@tonic-gate { 61292f0fcb93SJason Beloro if (&mach_sync_icache_pp != NULL && hashout) { 61302f0fcb93SJason Beloro mach_sync_icache_pp(pp); 61312f0fcb93SJason Beloro } 61327c478bd9Sstevel@tonic-gate pp->p_nrm = 0; 61337c478bd9Sstevel@tonic-gate } 61347c478bd9Sstevel@tonic-gate 61357c478bd9Sstevel@tonic-gate /* 6136db874c57Selowe * Clear p_lckcnt and p_cowcnt, adjusting freemem if required. 61377c478bd9Sstevel@tonic-gate */ 61387c478bd9Sstevel@tonic-gate int 6139db874c57Selowe page_clear_lck_cow(page_t *pp, int adjust) 61407c478bd9Sstevel@tonic-gate { 6141db874c57Selowe int f_amount; 61427c478bd9Sstevel@tonic-gate 6143db874c57Selowe ASSERT(PAGE_EXCL(pp)); 61447c478bd9Sstevel@tonic-gate 61457c478bd9Sstevel@tonic-gate /* 6146db874c57Selowe * The page_struct_lock need not be acquired here since 6147db874c57Selowe * we require the caller hold the page exclusively locked. 61487c478bd9Sstevel@tonic-gate */ 6149db874c57Selowe f_amount = 0; 6150db874c57Selowe if (pp->p_lckcnt) { 6151db874c57Selowe f_amount = 1; 6152db874c57Selowe pp->p_lckcnt = 0; 6153db874c57Selowe } 6154db874c57Selowe if (pp->p_cowcnt) { 6155db874c57Selowe f_amount += pp->p_cowcnt; 6156db874c57Selowe pp->p_cowcnt = 0; 6157db874c57Selowe } 61587c478bd9Sstevel@tonic-gate 6159db874c57Selowe if (adjust && f_amount) { 6160db874c57Selowe mutex_enter(&freemem_lock); 6161db874c57Selowe availrmem += f_amount; 6162db874c57Selowe mutex_exit(&freemem_lock); 61637c478bd9Sstevel@tonic-gate } 61647c478bd9Sstevel@tonic-gate 6165db874c57Selowe return (f_amount); 61667c478bd9Sstevel@tonic-gate } 61677c478bd9Sstevel@tonic-gate 61687c478bd9Sstevel@tonic-gate /* 6169db874c57Selowe * The following functions is called from free_vp_pages() 6170db874c57Selowe * for an inexact estimate of a newly free'd page... 61717c478bd9Sstevel@tonic-gate */ 6172db874c57Selowe ulong_t 6173db874c57Selowe page_share_cnt(page_t *pp) 6174db874c57Selowe { 6175db874c57Selowe return (hat_page_getshare(pp)); 6176db874c57Selowe } 6177db874c57Selowe 61787c478bd9Sstevel@tonic-gate int 61797c478bd9Sstevel@tonic-gate page_isshared(page_t *pp) 61807c478bd9Sstevel@tonic-gate { 618105d3dc4bSpaulsan return (hat_page_checkshare(pp, 1)); 61827c478bd9Sstevel@tonic-gate } 61837c478bd9Sstevel@tonic-gate 61847c478bd9Sstevel@tonic-gate int 61857c478bd9Sstevel@tonic-gate page_isfree(page_t *pp) 61867c478bd9Sstevel@tonic-gate { 61877c478bd9Sstevel@tonic-gate return (PP_ISFREE(pp)); 61887c478bd9Sstevel@tonic-gate } 61897c478bd9Sstevel@tonic-gate 61907c478bd9Sstevel@tonic-gate int 61917c478bd9Sstevel@tonic-gate page_isref(page_t *pp) 61927c478bd9Sstevel@tonic-gate { 61937c478bd9Sstevel@tonic-gate return (hat_page_getattr(pp, P_REF)); 61947c478bd9Sstevel@tonic-gate } 61957c478bd9Sstevel@tonic-gate 61967c478bd9Sstevel@tonic-gate int 61977c478bd9Sstevel@tonic-gate page_ismod(page_t *pp) 61987c478bd9Sstevel@tonic-gate { 61997c478bd9Sstevel@tonic-gate return (hat_page_getattr(pp, P_MOD)); 62007c478bd9Sstevel@tonic-gate } 62018b464eb8Smec 62028b464eb8Smec /* 62038b464eb8Smec * The following code all currently relates to the page capture logic: 62048b464eb8Smec * 62058b464eb8Smec * This logic is used for cases where there is a desire to claim a certain 62068b464eb8Smec * physical page in the system for the caller. As it may not be possible 62078b464eb8Smec * to capture the page immediately, the p_toxic bits are used in the page 62088b464eb8Smec * structure to indicate that someone wants to capture this page. When the 62098b464eb8Smec * page gets unlocked, the toxic flag will be noted and an attempt to capture 62108b464eb8Smec * the page will be made. If it is successful, the original callers callback 62118b464eb8Smec * will be called with the page to do with it what they please. 62128b464eb8Smec * 62138b464eb8Smec * There is also an async thread which wakes up to attempt to capture 62148b464eb8Smec * pages occasionally which have the capture bit set. All of the pages which 62158b464eb8Smec * need to be captured asynchronously have been inserted into the 62168b464eb8Smec * page_capture_hash and thus this thread walks that hash list. Items in the 62178b464eb8Smec * hash have an expiration time so this thread handles that as well by removing 62188b464eb8Smec * the item from the hash if it has expired. 62198b464eb8Smec * 62208b464eb8Smec * Some important things to note are: 62218b464eb8Smec * - if the PR_CAPTURE bit is set on a page, then the page is in the 62228b464eb8Smec * page_capture_hash. The page_capture_hash_head.pchh_mutex is needed 62238b464eb8Smec * to set and clear this bit, and while the lock is held is the only time 62248b464eb8Smec * you can add or remove an entry from the hash. 62258b464eb8Smec * - the PR_CAPTURE bit can only be set and cleared while holding the 62268b464eb8Smec * page_capture_hash_head.pchh_mutex 62278b464eb8Smec * - the t_flag field of the thread struct is used with the T_CAPTURING 62288b464eb8Smec * flag to prevent recursion while dealing with large pages. 62298b464eb8Smec * - pages which need to be retired never expire on the page_capture_hash. 62308b464eb8Smec */ 62318b464eb8Smec 62328b464eb8Smec static void page_capture_thread(void); 62338b464eb8Smec static kthread_t *pc_thread_id; 62348b464eb8Smec kcondvar_t pc_cv; 62358b464eb8Smec static kmutex_t pc_thread_mutex; 62368b464eb8Smec static clock_t pc_thread_shortwait; 62378b464eb8Smec static clock_t pc_thread_longwait; 6238a98e9dbfSaguzovsk static int pc_thread_retry; 62398b464eb8Smec 62408b464eb8Smec struct page_capture_callback pc_cb[PC_NUM_CALLBACKS]; 62418b464eb8Smec 62428b464eb8Smec /* Note that this is a circular linked list */ 62438b464eb8Smec typedef struct page_capture_hash_bucket { 62448b464eb8Smec page_t *pp; 62458b464eb8Smec uint_t szc; 62468b464eb8Smec uint_t flags; 62478b464eb8Smec clock_t expires; /* lbolt at which this request expires. */ 62488b464eb8Smec void *datap; /* Cached data passed in for callback */ 62498b464eb8Smec struct page_capture_hash_bucket *next; 62508b464eb8Smec struct page_capture_hash_bucket *prev; 62518b464eb8Smec } page_capture_hash_bucket_t; 62528b464eb8Smec 62538b464eb8Smec /* 62548b464eb8Smec * Each hash bucket will have it's own mutex and two lists which are: 62558b464eb8Smec * active (0): represents requests which have not been processed by 62568b464eb8Smec * the page_capture async thread yet. 62578b464eb8Smec * walked (1): represents requests which have been processed by the 62588b464eb8Smec * page_capture async thread within it's given walk of this bucket. 62598b464eb8Smec * 62608b464eb8Smec * These are all needed so that we can synchronize all async page_capture 62618b464eb8Smec * events. When the async thread moves to a new bucket, it will append the 62628b464eb8Smec * walked list to the active list and walk each item one at a time, moving it 62638b464eb8Smec * from the active list to the walked list. Thus if there is an async request 62648b464eb8Smec * outstanding for a given page, it will always be in one of the two lists. 62658b464eb8Smec * New requests will always be added to the active list. 62668b464eb8Smec * If we were not able to capture a page before the request expired, we'd free 62678b464eb8Smec * up the request structure which would indicate to page_capture that there is 62688b464eb8Smec * no longer a need for the given page, and clear the PR_CAPTURE flag if 62698b464eb8Smec * possible. 62708b464eb8Smec */ 62718b464eb8Smec typedef struct page_capture_hash_head { 62728b464eb8Smec kmutex_t pchh_mutex; 62738b464eb8Smec uint_t num_pages; 62748b464eb8Smec page_capture_hash_bucket_t lists[2]; /* sentinel nodes */ 62758b464eb8Smec } page_capture_hash_head_t; 62768b464eb8Smec 62778b464eb8Smec #ifdef DEBUG 62788b464eb8Smec #define NUM_PAGE_CAPTURE_BUCKETS 4 62798b464eb8Smec #else 62808b464eb8Smec #define NUM_PAGE_CAPTURE_BUCKETS 64 62818b464eb8Smec #endif 62828b464eb8Smec 62838b464eb8Smec page_capture_hash_head_t page_capture_hash[NUM_PAGE_CAPTURE_BUCKETS]; 62848b464eb8Smec 62858b464eb8Smec /* for now use a very simple hash based upon the size of a page struct */ 62868b464eb8Smec #define PAGE_CAPTURE_HASH(pp) \ 62878b464eb8Smec ((int)(((uintptr_t)pp >> 7) & (NUM_PAGE_CAPTURE_BUCKETS - 1))) 62888b464eb8Smec 62898b464eb8Smec extern pgcnt_t swapfs_minfree; 62908b464eb8Smec 62918b464eb8Smec int page_trycapture(page_t *pp, uint_t szc, uint_t flags, void *datap); 62928b464eb8Smec 62938b464eb8Smec /* 62948b464eb8Smec * a callback function is required for page capture requests. 62958b464eb8Smec */ 62968b464eb8Smec void 62978b464eb8Smec page_capture_register_callback(uint_t index, clock_t duration, 62988b464eb8Smec int (*cb_func)(page_t *, void *, uint_t)) 62998b464eb8Smec { 63008b464eb8Smec ASSERT(pc_cb[index].cb_active == 0); 63018b464eb8Smec ASSERT(cb_func != NULL); 63028b464eb8Smec rw_enter(&pc_cb[index].cb_rwlock, RW_WRITER); 63038b464eb8Smec pc_cb[index].duration = duration; 63048b464eb8Smec pc_cb[index].cb_func = cb_func; 63058b464eb8Smec pc_cb[index].cb_active = 1; 63068b464eb8Smec rw_exit(&pc_cb[index].cb_rwlock); 63078b464eb8Smec } 63088b464eb8Smec 63098b464eb8Smec void 63108b464eb8Smec page_capture_unregister_callback(uint_t index) 63118b464eb8Smec { 63128b464eb8Smec int i, j; 63138b464eb8Smec struct page_capture_hash_bucket *bp1; 63148b464eb8Smec struct page_capture_hash_bucket *bp2; 63158b464eb8Smec struct page_capture_hash_bucket *head = NULL; 63168b464eb8Smec uint_t flags = (1 << index); 63178b464eb8Smec 63188b464eb8Smec rw_enter(&pc_cb[index].cb_rwlock, RW_WRITER); 63198b464eb8Smec ASSERT(pc_cb[index].cb_active == 1); 63208b464eb8Smec pc_cb[index].duration = 0; /* Paranoia */ 63218b464eb8Smec pc_cb[index].cb_func = NULL; /* Paranoia */ 63228b464eb8Smec pc_cb[index].cb_active = 0; 63238b464eb8Smec rw_exit(&pc_cb[index].cb_rwlock); 63248b464eb8Smec 63258b464eb8Smec /* 63268b464eb8Smec * Just move all the entries to a private list which we can walk 63278b464eb8Smec * through without the need to hold any locks. 63288b464eb8Smec * No more requests can get added to the hash lists for this consumer 63298b464eb8Smec * as the cb_active field for the callback has been cleared. 63308b464eb8Smec */ 63318b464eb8Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 63328b464eb8Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 63338b464eb8Smec for (j = 0; j < 2; j++) { 63348b464eb8Smec bp1 = page_capture_hash[i].lists[j].next; 63358b464eb8Smec /* walk through all but first (sentinel) element */ 63368b464eb8Smec while (bp1 != &page_capture_hash[i].lists[j]) { 63378b464eb8Smec bp2 = bp1; 63388b464eb8Smec if (bp2->flags & flags) { 63398b464eb8Smec bp1 = bp2->next; 63408b464eb8Smec bp1->prev = bp2->prev; 63418b464eb8Smec bp2->prev->next = bp1; 63428b464eb8Smec bp2->next = head; 63438b464eb8Smec head = bp2; 63448b464eb8Smec /* 63458b464eb8Smec * Clear the PR_CAPTURE bit as we 63468b464eb8Smec * hold appropriate locks here. 63478b464eb8Smec */ 63488b464eb8Smec page_clrtoxic(head->pp, PR_CAPTURE); 63498b464eb8Smec page_capture_hash[i].num_pages--; 63508b464eb8Smec continue; 63518b464eb8Smec } 63528b464eb8Smec bp1 = bp1->next; 63538b464eb8Smec } 63548b464eb8Smec } 63558b464eb8Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 63568b464eb8Smec } 63578b464eb8Smec 63588b464eb8Smec while (head != NULL) { 63598b464eb8Smec bp1 = head; 63608b464eb8Smec head = head->next; 63618b464eb8Smec kmem_free(bp1, sizeof (*bp1)); 63628b464eb8Smec } 63638b464eb8Smec } 63648b464eb8Smec 63658b464eb8Smec 63668b464eb8Smec /* 63678b464eb8Smec * Find pp in the active list and move it to the walked list if it 63688b464eb8Smec * exists. 63698b464eb8Smec * Note that most often pp should be at the front of the active list 63708b464eb8Smec * as it is currently used and thus there is no other sort of optimization 63718b464eb8Smec * being done here as this is a linked list data structure. 63728b464eb8Smec * Returns 1 on successful move or 0 if page could not be found. 63738b464eb8Smec */ 63748b464eb8Smec static int 63758b464eb8Smec page_capture_move_to_walked(page_t *pp) 63768b464eb8Smec { 63778b464eb8Smec page_capture_hash_bucket_t *bp; 63788b464eb8Smec int index; 63798b464eb8Smec 63808b464eb8Smec index = PAGE_CAPTURE_HASH(pp); 63818b464eb8Smec 63828b464eb8Smec mutex_enter(&page_capture_hash[index].pchh_mutex); 63838b464eb8Smec bp = page_capture_hash[index].lists[0].next; 63848b464eb8Smec while (bp != &page_capture_hash[index].lists[0]) { 63858b464eb8Smec if (bp->pp == pp) { 63868b464eb8Smec /* Remove from old list */ 63878b464eb8Smec bp->next->prev = bp->prev; 63888b464eb8Smec bp->prev->next = bp->next; 63898b464eb8Smec 63908b464eb8Smec /* Add to new list */ 63918b464eb8Smec bp->next = page_capture_hash[index].lists[1].next; 63928b464eb8Smec bp->prev = &page_capture_hash[index].lists[1]; 63938b464eb8Smec page_capture_hash[index].lists[1].next = bp; 63948b464eb8Smec bp->next->prev = bp; 63958b464eb8Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 63968b464eb8Smec 63978b464eb8Smec return (1); 63988b464eb8Smec } 63998b464eb8Smec bp = bp->next; 64008b464eb8Smec } 64018b464eb8Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 64028b464eb8Smec return (0); 64038b464eb8Smec } 64048b464eb8Smec 64058b464eb8Smec /* 64068b464eb8Smec * Add a new entry to the page capture hash. The only case where a new 64078b464eb8Smec * entry is not added is when the page capture consumer is no longer registered. 64088b464eb8Smec * In this case, we'll silently not add the page to the hash. We know that 64098b464eb8Smec * page retire will always be registered for the case where we are currently 64108b464eb8Smec * unretiring a page and thus there are no conflicts. 64118b464eb8Smec */ 64128b464eb8Smec static void 64138b464eb8Smec page_capture_add_hash(page_t *pp, uint_t szc, uint_t flags, void *datap) 64148b464eb8Smec { 64158b464eb8Smec page_capture_hash_bucket_t *bp1; 64168b464eb8Smec page_capture_hash_bucket_t *bp2; 64178b464eb8Smec int index; 64188b464eb8Smec int cb_index; 64198b464eb8Smec int i; 64208b464eb8Smec #ifdef DEBUG 64218b464eb8Smec page_capture_hash_bucket_t *tp1; 64228b464eb8Smec int l; 64238b464eb8Smec #endif 64248b464eb8Smec 64258b464eb8Smec ASSERT(!(flags & CAPTURE_ASYNC)); 64268b464eb8Smec 64278b464eb8Smec bp1 = kmem_alloc(sizeof (struct page_capture_hash_bucket), KM_SLEEP); 64288b464eb8Smec 64298b464eb8Smec bp1->pp = pp; 64308b464eb8Smec bp1->szc = szc; 64318b464eb8Smec bp1->flags = flags; 64328b464eb8Smec bp1->datap = datap; 64338b464eb8Smec 64348b464eb8Smec for (cb_index = 0; cb_index < PC_NUM_CALLBACKS; cb_index++) { 64358b464eb8Smec if ((flags >> cb_index) & 1) { 64368b464eb8Smec break; 64378b464eb8Smec } 64388b464eb8Smec } 64398b464eb8Smec 64408b464eb8Smec ASSERT(cb_index != PC_NUM_CALLBACKS); 64418b464eb8Smec 64428b464eb8Smec rw_enter(&pc_cb[cb_index].cb_rwlock, RW_READER); 64438b464eb8Smec if (pc_cb[cb_index].cb_active) { 64448b464eb8Smec if (pc_cb[cb_index].duration == -1) { 64458b464eb8Smec bp1->expires = (clock_t)-1; 64468b464eb8Smec } else { 64478b464eb8Smec bp1->expires = lbolt + pc_cb[cb_index].duration; 64488b464eb8Smec } 64498b464eb8Smec } else { 64508b464eb8Smec /* There's no callback registered so don't add to the hash */ 64518b464eb8Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 64528b464eb8Smec kmem_free(bp1, sizeof (*bp1)); 64538b464eb8Smec return; 64548b464eb8Smec } 64558b464eb8Smec 64568b464eb8Smec index = PAGE_CAPTURE_HASH(pp); 64578b464eb8Smec 64588b464eb8Smec /* 64598b464eb8Smec * Only allow capture flag to be modified under this mutex. 64608b464eb8Smec * Prevents multiple entries for same page getting added. 64618b464eb8Smec */ 64628b464eb8Smec mutex_enter(&page_capture_hash[index].pchh_mutex); 64638b464eb8Smec 64648b464eb8Smec /* 64658b464eb8Smec * if not already on the hash, set capture bit and add to the hash 64668b464eb8Smec */ 64678b464eb8Smec if (!(pp->p_toxic & PR_CAPTURE)) { 64688b464eb8Smec #ifdef DEBUG 64698b464eb8Smec /* Check for duplicate entries */ 64708b464eb8Smec for (l = 0; l < 2; l++) { 64718b464eb8Smec tp1 = page_capture_hash[index].lists[l].next; 64728b464eb8Smec while (tp1 != &page_capture_hash[index].lists[l]) { 64738b464eb8Smec if (tp1->pp == pp) { 64748b464eb8Smec panic("page pp 0x%p already on hash " 64758793b36bSNick Todd "at 0x%p\n", 64768793b36bSNick Todd (void *)pp, (void *)tp1); 64778b464eb8Smec } 64788b464eb8Smec tp1 = tp1->next; 64798b464eb8Smec } 64808b464eb8Smec } 64818b464eb8Smec 64828b464eb8Smec #endif 64838b464eb8Smec page_settoxic(pp, PR_CAPTURE); 64848b464eb8Smec bp1->next = page_capture_hash[index].lists[0].next; 64858b464eb8Smec bp1->prev = &page_capture_hash[index].lists[0]; 64868b464eb8Smec bp1->next->prev = bp1; 64878b464eb8Smec page_capture_hash[index].lists[0].next = bp1; 64888b464eb8Smec page_capture_hash[index].num_pages++; 6489cee1d74bSjfrank if (flags & CAPTURE_RETIRE) { 6490704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States page_retire_incr_pend_count(datap); 6491cee1d74bSjfrank } 64928b464eb8Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 64938b464eb8Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 64948b464eb8Smec cv_signal(&pc_cv); 64958b464eb8Smec return; 64968b464eb8Smec } 64978b464eb8Smec 64988b464eb8Smec /* 64998b464eb8Smec * A page retire request will replace any other request. 65008b464eb8Smec * A second physmem request which is for a different process than 65018b464eb8Smec * the currently registered one will be dropped as there is 65028b464eb8Smec * no way to hold the private data for both calls. 65038b464eb8Smec * In the future, once there are more callers, this will have to 65048b464eb8Smec * be worked out better as there needs to be private storage for 65058b464eb8Smec * at least each type of caller (maybe have datap be an array of 65068b464eb8Smec * *void's so that we can index based upon callers index). 65078b464eb8Smec */ 65088b464eb8Smec 65098b464eb8Smec /* walk hash list to update expire time */ 65108b464eb8Smec for (i = 0; i < 2; i++) { 65118b464eb8Smec bp2 = page_capture_hash[index].lists[i].next; 65128b464eb8Smec while (bp2 != &page_capture_hash[index].lists[i]) { 65138b464eb8Smec if (bp2->pp == pp) { 65148b464eb8Smec if (flags & CAPTURE_RETIRE) { 65158b464eb8Smec if (!(bp2->flags & CAPTURE_RETIRE)) { 6516704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States page_retire_incr_pend_count( 6517704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States datap); 65188b464eb8Smec bp2->flags = flags; 65198b464eb8Smec bp2->expires = bp1->expires; 65208b464eb8Smec bp2->datap = datap; 65218b464eb8Smec } 65228b464eb8Smec } else { 65238b464eb8Smec ASSERT(flags & CAPTURE_PHYSMEM); 65248b464eb8Smec if (!(bp2->flags & CAPTURE_RETIRE) && 65258b464eb8Smec (datap == bp2->datap)) { 65268b464eb8Smec bp2->expires = bp1->expires; 65278b464eb8Smec } 65288b464eb8Smec } 65298b464eb8Smec mutex_exit(&page_capture_hash[index]. 65308b464eb8Smec pchh_mutex); 65318b464eb8Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 65328b464eb8Smec kmem_free(bp1, sizeof (*bp1)); 65338b464eb8Smec return; 65348b464eb8Smec } 65358b464eb8Smec bp2 = bp2->next; 65368b464eb8Smec } 65378b464eb8Smec } 65388b464eb8Smec 65398b464eb8Smec /* 65408b464eb8Smec * the PR_CAPTURE flag is protected by the page_capture_hash mutexes 65418b464eb8Smec * and thus it either has to be set or not set and can't change 65428b464eb8Smec * while holding the mutex above. 65438b464eb8Smec */ 65448793b36bSNick Todd panic("page_capture_add_hash, PR_CAPTURE flag set on pp %p\n", 65458793b36bSNick Todd (void *)pp); 65468b464eb8Smec } 65478b464eb8Smec 65488b464eb8Smec /* 65498b464eb8Smec * We have a page in our hands, lets try and make it ours by turning 65508b464eb8Smec * it into a clean page like it had just come off the freelists. 65518b464eb8Smec * 65528b464eb8Smec * Returns 0 on success, with the page still EXCL locked. 65538b464eb8Smec * On failure, the page will be unlocked, and returns EAGAIN 65548b464eb8Smec */ 65558b464eb8Smec static int 65568b464eb8Smec page_capture_clean_page(page_t *pp) 65578b464eb8Smec { 65588b464eb8Smec page_t *newpp; 65598b464eb8Smec int skip_unlock = 0; 65608b464eb8Smec spgcnt_t count; 65618b464eb8Smec page_t *tpp; 65628b464eb8Smec int ret = 0; 65638b464eb8Smec int extra; 65648b464eb8Smec 65658b464eb8Smec ASSERT(PAGE_EXCL(pp)); 65668b464eb8Smec ASSERT(!PP_RETIRED(pp)); 65678b464eb8Smec ASSERT(curthread->t_flag & T_CAPTURING); 65688b464eb8Smec 65698b464eb8Smec if (PP_ISFREE(pp)) { 65706e4dd838Smec if (!page_reclaim(pp, NULL)) { 65718b464eb8Smec skip_unlock = 1; 65728b464eb8Smec ret = EAGAIN; 65738b464eb8Smec goto cleanup; 65748b464eb8Smec } 65756e4dd838Smec ASSERT(pp->p_szc == 0); 65768b464eb8Smec if (pp->p_vnode != NULL) { 65778b464eb8Smec /* 65788b464eb8Smec * Since this page came from the 65798b464eb8Smec * cachelist, we must destroy the 65808b464eb8Smec * old vnode association. 65818b464eb8Smec */ 65828b464eb8Smec page_hashout(pp, NULL); 65838b464eb8Smec } 65848b464eb8Smec goto cleanup; 65858b464eb8Smec } 65868b464eb8Smec 65878b464eb8Smec /* 65888b464eb8Smec * If we know page_relocate will fail, skip it 65898b464eb8Smec * It could still fail due to a UE on another page but we 65908b464eb8Smec * can't do anything about that. 65918b464eb8Smec */ 65928b464eb8Smec if (pp->p_toxic & PR_UE) { 65938b464eb8Smec goto skip_relocate; 65948b464eb8Smec } 65958b464eb8Smec 65968b464eb8Smec /* 65978b464eb8Smec * It's possible that pages can not have a vnode as fsflush comes 65988b464eb8Smec * through and cleans up these pages. It's ugly but that's how it is. 65998b464eb8Smec */ 66008b464eb8Smec if (pp->p_vnode == NULL) { 66018b464eb8Smec goto skip_relocate; 66028b464eb8Smec } 66038b464eb8Smec 66048b464eb8Smec /* 66058b464eb8Smec * Page was not free, so lets try to relocate it. 66068b464eb8Smec * page_relocate only works with root pages, so if this is not a root 66078b464eb8Smec * page, we need to demote it to try and relocate it. 66088b464eb8Smec * Unfortunately this is the best we can do right now. 66098b464eb8Smec */ 66108b464eb8Smec newpp = NULL; 66118b464eb8Smec if ((pp->p_szc > 0) && (pp != PP_PAGEROOT(pp))) { 66128b464eb8Smec if (page_try_demote_pages(pp) == 0) { 66138b464eb8Smec ret = EAGAIN; 66148b464eb8Smec goto cleanup; 66158b464eb8Smec } 66168b464eb8Smec } 66178b464eb8Smec ret = page_relocate(&pp, &newpp, 1, 0, &count, NULL); 66188b464eb8Smec if (ret == 0) { 66198b464eb8Smec page_t *npp; 66208b464eb8Smec /* unlock the new page(s) */ 66218b464eb8Smec while (count-- > 0) { 66228b464eb8Smec ASSERT(newpp != NULL); 66238b464eb8Smec npp = newpp; 66248b464eb8Smec page_sub(&newpp, npp); 66258b464eb8Smec page_unlock(npp); 66268b464eb8Smec } 66278b464eb8Smec ASSERT(newpp == NULL); 66288b464eb8Smec /* 66298b464eb8Smec * Check to see if the page we have is too large. 66308b464eb8Smec * If so, demote it freeing up the extra pages. 66318b464eb8Smec */ 66328b464eb8Smec if (pp->p_szc > 0) { 66338b464eb8Smec /* For now demote extra pages to szc == 0 */ 66348b464eb8Smec extra = page_get_pagecnt(pp->p_szc) - 1; 66358b464eb8Smec while (extra > 0) { 66368b464eb8Smec tpp = pp->p_next; 66378b464eb8Smec page_sub(&pp, tpp); 66388b464eb8Smec tpp->p_szc = 0; 66398b464eb8Smec page_free(tpp, 1); 66408b464eb8Smec extra--; 66418b464eb8Smec } 66428b464eb8Smec /* Make sure to set our page to szc 0 as well */ 66438b464eb8Smec ASSERT(pp->p_next == pp && pp->p_prev == pp); 66448b464eb8Smec pp->p_szc = 0; 66458b464eb8Smec } 66468b464eb8Smec goto cleanup; 66478b464eb8Smec } else if (ret == EIO) { 66488b464eb8Smec ret = EAGAIN; 66498b464eb8Smec goto cleanup; 66508b464eb8Smec } else { 66518b464eb8Smec /* 66528b464eb8Smec * Need to reset return type as we failed to relocate the page 66538b464eb8Smec * but that does not mean that some of the next steps will not 66548b464eb8Smec * work. 66558b464eb8Smec */ 66568b464eb8Smec ret = 0; 66578b464eb8Smec } 66588b464eb8Smec 66598b464eb8Smec skip_relocate: 66608b464eb8Smec 66618b464eb8Smec if (pp->p_szc > 0) { 66628b464eb8Smec if (page_try_demote_pages(pp) == 0) { 66638b464eb8Smec ret = EAGAIN; 66648b464eb8Smec goto cleanup; 66658b464eb8Smec } 66668b464eb8Smec } 66678b464eb8Smec 66688b464eb8Smec ASSERT(pp->p_szc == 0); 66698b464eb8Smec 66708b464eb8Smec if (hat_ismod(pp)) { 66718b464eb8Smec ret = EAGAIN; 66728b464eb8Smec goto cleanup; 66738b464eb8Smec } 6674ad23a2dbSjohansen if (PP_ISKAS(pp)) { 66758b464eb8Smec ret = EAGAIN; 66768b464eb8Smec goto cleanup; 66778b464eb8Smec } 66788b464eb8Smec if (pp->p_lckcnt || pp->p_cowcnt) { 66798b464eb8Smec ret = EAGAIN; 66808b464eb8Smec goto cleanup; 66818b464eb8Smec } 66828b464eb8Smec 66838b464eb8Smec (void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD); 66848b464eb8Smec ASSERT(!hat_page_is_mapped(pp)); 66858b464eb8Smec 66868b464eb8Smec if (hat_ismod(pp)) { 66878b464eb8Smec /* 66888b464eb8Smec * This is a semi-odd case as the page is now modified but not 66898b464eb8Smec * mapped as we just unloaded the mappings above. 66908b464eb8Smec */ 66918b464eb8Smec ret = EAGAIN; 66928b464eb8Smec goto cleanup; 66938b464eb8Smec } 66948b464eb8Smec if (pp->p_vnode != NULL) { 66958b464eb8Smec page_hashout(pp, NULL); 66968b464eb8Smec } 66978b464eb8Smec 66988b464eb8Smec /* 66998b464eb8Smec * At this point, the page should be in a clean state and 67008b464eb8Smec * we can do whatever we want with it. 67018b464eb8Smec */ 67028b464eb8Smec 67038b464eb8Smec cleanup: 67048b464eb8Smec if (ret != 0) { 67058b464eb8Smec if (!skip_unlock) { 67068b464eb8Smec page_unlock(pp); 67078b464eb8Smec } 67088b464eb8Smec } else { 67098b464eb8Smec ASSERT(pp->p_szc == 0); 67108b464eb8Smec ASSERT(PAGE_EXCL(pp)); 67118b464eb8Smec 67128b464eb8Smec pp->p_next = pp; 67138b464eb8Smec pp->p_prev = pp; 67148b464eb8Smec } 67158b464eb8Smec return (ret); 67168b464eb8Smec } 67178b464eb8Smec 67188b464eb8Smec /* 67198b464eb8Smec * Various callers of page_trycapture() can have different restrictions upon 67208b464eb8Smec * what memory they have access to. 67218b464eb8Smec * Returns 0 on success, with the following error codes on failure: 67228b464eb8Smec * EPERM - The requested page is long term locked, and thus repeated 67238b464eb8Smec * requests to capture this page will likely fail. 67248b464eb8Smec * ENOMEM - There was not enough free memory in the system to safely 67258b464eb8Smec * map the requested page. 67268b464eb8Smec * ENOENT - The requested page was inside the kernel cage, and the 67278b464eb8Smec * PHYSMEM_CAGE flag was not set. 67288b464eb8Smec */ 67298b464eb8Smec int 67308b464eb8Smec page_capture_pre_checks(page_t *pp, uint_t flags) 67318b464eb8Smec { 67328b464eb8Smec #if defined(__sparc) 67338b464eb8Smec extern struct vnode prom_ppages; 67348b464eb8Smec #endif /* __sparc */ 67358b464eb8Smec 67368b464eb8Smec ASSERT(pp != NULL); 67378b464eb8Smec 67388b464eb8Smec #if defined(__sparc) 67398b464eb8Smec if (pp->p_vnode == &prom_ppages) { 67408b464eb8Smec return (EPERM); 67418b464eb8Smec } 67428b464eb8Smec 6743a98e9dbfSaguzovsk if (PP_ISNORELOC(pp) && !(flags & CAPTURE_GET_CAGE) && 6744a98e9dbfSaguzovsk (flags & CAPTURE_PHYSMEM)) { 67458b464eb8Smec return (ENOENT); 67468b464eb8Smec } 67478b464eb8Smec 67488b464eb8Smec if (PP_ISNORELOCKERNEL(pp)) { 67498b464eb8Smec return (EPERM); 67508b464eb8Smec } 67518b464eb8Smec #else 6752ad23a2dbSjohansen if (PP_ISKAS(pp)) { 67538b464eb8Smec return (EPERM); 67548b464eb8Smec } 67558b464eb8Smec #endif /* __sparc */ 67568b464eb8Smec 6757a98e9dbfSaguzovsk /* only physmem currently has the restrictions checked below */ 6758a98e9dbfSaguzovsk if (!(flags & CAPTURE_PHYSMEM)) { 6759a98e9dbfSaguzovsk return (0); 6760a98e9dbfSaguzovsk } 6761a98e9dbfSaguzovsk 67628b464eb8Smec if (availrmem < swapfs_minfree) { 67638b464eb8Smec /* 67648b464eb8Smec * We won't try to capture this page as we are 67658b464eb8Smec * running low on memory. 67668b464eb8Smec */ 67678b464eb8Smec return (ENOMEM); 67688b464eb8Smec } 67698b464eb8Smec return (0); 67708b464eb8Smec } 67718b464eb8Smec 67728b464eb8Smec /* 67738b464eb8Smec * Once we have a page in our mits, go ahead and complete the capture 67748b464eb8Smec * operation. 67758b464eb8Smec * Returns 1 on failure where page is no longer needed 67768b464eb8Smec * Returns 0 on success 67778b464eb8Smec * Returns -1 if there was a transient failure. 67788b464eb8Smec * Failure cases must release the SE_EXCL lock on pp (usually via page_free). 67798b464eb8Smec */ 67808b464eb8Smec int 67818b464eb8Smec page_capture_take_action(page_t *pp, uint_t flags, void *datap) 67828b464eb8Smec { 67838b464eb8Smec int cb_index; 67848b464eb8Smec int ret = 0; 67858b464eb8Smec page_capture_hash_bucket_t *bp1; 67868b464eb8Smec page_capture_hash_bucket_t *bp2; 67878b464eb8Smec int index; 67888b464eb8Smec int found = 0; 67898b464eb8Smec int i; 67908b464eb8Smec 67918b464eb8Smec ASSERT(PAGE_EXCL(pp)); 67928b464eb8Smec ASSERT(curthread->t_flag & T_CAPTURING); 67938b464eb8Smec 67948b464eb8Smec for (cb_index = 0; cb_index < PC_NUM_CALLBACKS; cb_index++) { 67958b464eb8Smec if ((flags >> cb_index) & 1) { 67968b464eb8Smec break; 67978b464eb8Smec } 67988b464eb8Smec } 67998b464eb8Smec ASSERT(cb_index < PC_NUM_CALLBACKS); 68008b464eb8Smec 68018b464eb8Smec /* 68028b464eb8Smec * Remove the entry from the page_capture hash, but don't free it yet 68038b464eb8Smec * as we may need to put it back. 68048b464eb8Smec * Since we own the page at this point in time, we should find it 68058b464eb8Smec * in the hash if this is an ASYNC call. If we don't it's likely 68068b464eb8Smec * that the page_capture_async() thread decided that this request 68078b464eb8Smec * had expired, in which case we just continue on. 68088b464eb8Smec */ 68098b464eb8Smec if (flags & CAPTURE_ASYNC) { 68108b464eb8Smec 68118b464eb8Smec index = PAGE_CAPTURE_HASH(pp); 68128b464eb8Smec 68138b464eb8Smec mutex_enter(&page_capture_hash[index].pchh_mutex); 68148b464eb8Smec for (i = 0; i < 2 && !found; i++) { 68158b464eb8Smec bp1 = page_capture_hash[index].lists[i].next; 68168b464eb8Smec while (bp1 != &page_capture_hash[index].lists[i]) { 68178b464eb8Smec if (bp1->pp == pp) { 68188b464eb8Smec bp1->next->prev = bp1->prev; 68198b464eb8Smec bp1->prev->next = bp1->next; 68208b464eb8Smec page_capture_hash[index].num_pages--; 68218b464eb8Smec page_clrtoxic(pp, PR_CAPTURE); 68228b464eb8Smec found = 1; 68238b464eb8Smec break; 68248b464eb8Smec } 68258b464eb8Smec bp1 = bp1->next; 68268b464eb8Smec } 68278b464eb8Smec } 68288b464eb8Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 68298b464eb8Smec } 68308b464eb8Smec 68318b464eb8Smec /* Synchronize with the unregister func. */ 68328b464eb8Smec rw_enter(&pc_cb[cb_index].cb_rwlock, RW_READER); 68338b464eb8Smec if (!pc_cb[cb_index].cb_active) { 68348b464eb8Smec page_free(pp, 1); 68358b464eb8Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 68368b464eb8Smec if (found) { 68378b464eb8Smec kmem_free(bp1, sizeof (*bp1)); 68388b464eb8Smec } 68398b464eb8Smec return (1); 68408b464eb8Smec } 68418b464eb8Smec 68428b464eb8Smec /* 68438b464eb8Smec * We need to remove the entry from the page capture hash and turn off 68448b464eb8Smec * the PR_CAPTURE bit before calling the callback. We'll need to cache 68458b464eb8Smec * the entry here, and then based upon the return value, cleanup 68468b464eb8Smec * appropriately or re-add it to the hash, making sure that someone else 68478b464eb8Smec * hasn't already done so. 68488b464eb8Smec * It should be rare for the callback to fail and thus it's ok for 68498b464eb8Smec * the failure path to be a bit complicated as the success path is 68508b464eb8Smec * cleaner and the locking rules are easier to follow. 68518b464eb8Smec */ 68528b464eb8Smec 68538b464eb8Smec ret = pc_cb[cb_index].cb_func(pp, datap, flags); 68548b464eb8Smec 68558b464eb8Smec rw_exit(&pc_cb[cb_index].cb_rwlock); 68568b464eb8Smec 68578b464eb8Smec /* 68588b464eb8Smec * If this was an ASYNC request, we need to cleanup the hash if the 68598b464eb8Smec * callback was successful or if the request was no longer valid. 68608b464eb8Smec * For non-ASYNC requests, we return failure to map and the caller 68618b464eb8Smec * will take care of adding the request to the hash. 68628b464eb8Smec * Note also that the callback itself is responsible for the page 68638b464eb8Smec * at this point in time in terms of locking ... The most common 68648b464eb8Smec * case for the failure path should just be a page_free. 68658b464eb8Smec */ 68668b464eb8Smec if (ret >= 0) { 68678b464eb8Smec if (found) { 6868cee1d74bSjfrank if (bp1->flags & CAPTURE_RETIRE) { 6869704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States page_retire_decr_pend_count(datap); 6870cee1d74bSjfrank } 68718b464eb8Smec kmem_free(bp1, sizeof (*bp1)); 68728b464eb8Smec } 68738b464eb8Smec return (ret); 68748b464eb8Smec } 68758b464eb8Smec if (!found) { 68768b464eb8Smec return (ret); 68778b464eb8Smec } 68788b464eb8Smec 68798b464eb8Smec ASSERT(flags & CAPTURE_ASYNC); 68808b464eb8Smec 68818b464eb8Smec /* 68828b464eb8Smec * Check for expiration time first as we can just free it up if it's 68838b464eb8Smec * expired. 68848b464eb8Smec */ 68858b464eb8Smec if (lbolt > bp1->expires && bp1->expires != -1) { 68868b464eb8Smec kmem_free(bp1, sizeof (*bp1)); 68878b464eb8Smec return (ret); 68888b464eb8Smec } 68898b464eb8Smec 68908b464eb8Smec /* 68918b464eb8Smec * The callback failed and there used to be an entry in the hash for 68928b464eb8Smec * this page, so we need to add it back to the hash. 68938b464eb8Smec */ 68948b464eb8Smec mutex_enter(&page_capture_hash[index].pchh_mutex); 68958b464eb8Smec if (!(pp->p_toxic & PR_CAPTURE)) { 68968b464eb8Smec /* just add bp1 back to head of walked list */ 68978b464eb8Smec page_settoxic(pp, PR_CAPTURE); 68988b464eb8Smec bp1->next = page_capture_hash[index].lists[1].next; 68998b464eb8Smec bp1->prev = &page_capture_hash[index].lists[1]; 69008b464eb8Smec bp1->next->prev = bp1; 69018b464eb8Smec page_capture_hash[index].lists[1].next = bp1; 69028b464eb8Smec page_capture_hash[index].num_pages++; 69038b464eb8Smec mutex_exit(&page_capture_hash[index].pchh_mutex); 69048b464eb8Smec return (ret); 69058b464eb8Smec } 69068b464eb8Smec 69078b464eb8Smec /* 69088b464eb8Smec * Otherwise there was a new capture request added to list 69098b464eb8Smec * Need to make sure that our original data is represented if 69108b464eb8Smec * appropriate. 69118b464eb8Smec */ 69128b464eb8Smec for (i = 0; i < 2; i++) { 69138b464eb8Smec bp2 = page_capture_hash[index].lists[i].next; 69148b464eb8Smec while (bp2 != &page_capture_hash[index].lists[i]) { 69158b464eb8Smec if (bp2->pp == pp) { 69168b464eb8Smec if (bp1->flags & CAPTURE_RETIRE) { 69178b464eb8Smec if (!(bp2->flags & CAPTURE_RETIRE)) { 69188b464eb8Smec bp2->szc = bp1->szc; 69198b464eb8Smec bp2->flags = bp1->flags; 69208b464eb8Smec bp2->expires = bp1->expires; 69218b464eb8Smec bp2->datap = bp1->datap; 69228b464eb8Smec } 69238b464eb8Smec } else { 69248b464eb8Smec ASSERT(bp1->flags & CAPTURE_PHYSMEM); 69258b464eb8Smec if (!(bp2->flags & CAPTURE_RETIRE)) { 69268b464eb8Smec bp2->szc = bp1->szc; 69278b464eb8Smec bp2->flags = bp1->flags; 69288b464eb8Smec bp2->expires = bp1->expires; 69298b464eb8Smec bp2->datap = bp1->datap; 69308b464eb8Smec } 69318b464eb8Smec } 69328b464eb8Smec mutex_exit(&page_capture_hash[index]. 69338b464eb8Smec pchh_mutex); 69348b464eb8Smec kmem_free(bp1, sizeof (*bp1)); 69358b464eb8Smec return (ret); 69368b464eb8Smec } 69378b464eb8Smec bp2 = bp2->next; 69388b464eb8Smec } 69398b464eb8Smec } 69408793b36bSNick Todd panic("PR_CAPTURE set but not on hash for pp 0x%p\n", (void *)pp); 69418b464eb8Smec /*NOTREACHED*/ 69428b464eb8Smec } 69438b464eb8Smec 69448b464eb8Smec /* 69458b464eb8Smec * Try to capture the given page for the caller specified in the flags 69468b464eb8Smec * parameter. The page will either be captured and handed over to the 69478b464eb8Smec * appropriate callback, or will be queued up in the page capture hash 69488b464eb8Smec * to be captured asynchronously. 69498b464eb8Smec * If the current request is due to an async capture, the page must be 69508b464eb8Smec * exclusively locked before calling this function. 69518b464eb8Smec * Currently szc must be 0 but in the future this should be expandable to 69528b464eb8Smec * other page sizes. 69538b464eb8Smec * Returns 0 on success, with the following error codes on failure: 69548b464eb8Smec * EPERM - The requested page is long term locked, and thus repeated 69558b464eb8Smec * requests to capture this page will likely fail. 69568b464eb8Smec * ENOMEM - There was not enough free memory in the system to safely 69578b464eb8Smec * map the requested page. 69588b464eb8Smec * ENOENT - The requested page was inside the kernel cage, and the 69598b464eb8Smec * CAPTURE_GET_CAGE flag was not set. 69608b464eb8Smec * EAGAIN - The requested page could not be capturead at this point in 69618b464eb8Smec * time but future requests will likely work. 69628b464eb8Smec * EBUSY - The requested page is retired and the CAPTURE_GET_RETIRED flag 69638b464eb8Smec * was not set. 69648b464eb8Smec */ 69658b464eb8Smec int 69668b464eb8Smec page_itrycapture(page_t *pp, uint_t szc, uint_t flags, void *datap) 69678b464eb8Smec { 69688b464eb8Smec int ret; 69698b464eb8Smec int cb_index; 69708b464eb8Smec 69718b464eb8Smec if (flags & CAPTURE_ASYNC) { 69728b464eb8Smec ASSERT(PAGE_EXCL(pp)); 69738b464eb8Smec goto async; 69748b464eb8Smec } 69758b464eb8Smec 69768b464eb8Smec /* Make sure there's enough availrmem ... */ 69778b464eb8Smec ret = page_capture_pre_checks(pp, flags); 69788b464eb8Smec if (ret != 0) { 69798b464eb8Smec return (ret); 69808b464eb8Smec } 69818b464eb8Smec 69828b464eb8Smec if (!page_trylock(pp, SE_EXCL)) { 69838b464eb8Smec for (cb_index = 0; cb_index < PC_NUM_CALLBACKS; cb_index++) { 69848b464eb8Smec if ((flags >> cb_index) & 1) { 69858b464eb8Smec break; 69868b464eb8Smec } 69878b464eb8Smec } 69888b464eb8Smec ASSERT(cb_index < PC_NUM_CALLBACKS); 69898b464eb8Smec ret = EAGAIN; 69908b464eb8Smec /* Special case for retired pages */ 69918b464eb8Smec if (PP_RETIRED(pp)) { 69928b464eb8Smec if (flags & CAPTURE_GET_RETIRED) { 69938b464eb8Smec if (!page_unretire_pp(pp, PR_UNR_TEMP)) { 69948b464eb8Smec /* 69958b464eb8Smec * Need to set capture bit and add to 69968b464eb8Smec * hash so that the page will be 69978b464eb8Smec * retired when freed. 69988b464eb8Smec */ 69998b464eb8Smec page_capture_add_hash(pp, szc, 70008b464eb8Smec CAPTURE_RETIRE, NULL); 70018b464eb8Smec ret = 0; 70028b464eb8Smec goto own_page; 70038b464eb8Smec } 70048b464eb8Smec } else { 70058b464eb8Smec return (EBUSY); 70068b464eb8Smec } 70078b464eb8Smec } 70088b464eb8Smec page_capture_add_hash(pp, szc, flags, datap); 70098b464eb8Smec return (ret); 70108b464eb8Smec } 70118b464eb8Smec 70128b464eb8Smec async: 70138b464eb8Smec ASSERT(PAGE_EXCL(pp)); 70148b464eb8Smec 70158b464eb8Smec /* Need to check for physmem async requests that availrmem is sane */ 70168b464eb8Smec if ((flags & (CAPTURE_ASYNC | CAPTURE_PHYSMEM)) == 70178b464eb8Smec (CAPTURE_ASYNC | CAPTURE_PHYSMEM) && 70188b464eb8Smec (availrmem < swapfs_minfree)) { 70198b464eb8Smec page_unlock(pp); 70208b464eb8Smec return (ENOMEM); 70218b464eb8Smec } 70228b464eb8Smec 70238b464eb8Smec ret = page_capture_clean_page(pp); 70248b464eb8Smec 70258b464eb8Smec if (ret != 0) { 70268b464eb8Smec /* We failed to get the page, so lets add it to the hash */ 70278b464eb8Smec if (!(flags & CAPTURE_ASYNC)) { 70288b464eb8Smec page_capture_add_hash(pp, szc, flags, datap); 70298b464eb8Smec } 70308b464eb8Smec return (ret); 70318b464eb8Smec } 70328b464eb8Smec 70338b464eb8Smec own_page: 70348b464eb8Smec ASSERT(PAGE_EXCL(pp)); 70358b464eb8Smec ASSERT(pp->p_szc == 0); 70368b464eb8Smec 70378b464eb8Smec /* Call the callback */ 70388b464eb8Smec ret = page_capture_take_action(pp, flags, datap); 70398b464eb8Smec 70408b464eb8Smec if (ret == 0) { 70418b464eb8Smec return (0); 70428b464eb8Smec } 70438b464eb8Smec 70448b464eb8Smec /* 70458b464eb8Smec * Note that in the failure cases from page_capture_take_action, the 70468b464eb8Smec * EXCL lock will have already been dropped. 70478b464eb8Smec */ 70488b464eb8Smec if ((ret == -1) && (!(flags & CAPTURE_ASYNC))) { 70498b464eb8Smec page_capture_add_hash(pp, szc, flags, datap); 70508b464eb8Smec } 70518b464eb8Smec return (EAGAIN); 70528b464eb8Smec } 70538b464eb8Smec 70548b464eb8Smec int 70558b464eb8Smec page_trycapture(page_t *pp, uint_t szc, uint_t flags, void *datap) 70568b464eb8Smec { 70578b464eb8Smec int ret; 70588b464eb8Smec 70598b464eb8Smec curthread->t_flag |= T_CAPTURING; 70608b464eb8Smec ret = page_itrycapture(pp, szc, flags, datap); 70618b464eb8Smec curthread->t_flag &= ~T_CAPTURING; /* xor works as we know its set */ 70628b464eb8Smec return (ret); 70638b464eb8Smec } 70648b464eb8Smec 70658b464eb8Smec /* 70668b464eb8Smec * When unlocking a page which has the PR_CAPTURE bit set, this routine 70678b464eb8Smec * gets called to try and capture the page. 70688b464eb8Smec */ 70698b464eb8Smec void 70708b464eb8Smec page_unlock_capture(page_t *pp) 70718b464eb8Smec { 70728b464eb8Smec page_capture_hash_bucket_t *bp; 70738b464eb8Smec int index; 70748b464eb8Smec int i; 70758b464eb8Smec uint_t szc; 70768b464eb8Smec uint_t flags = 0; 70778b464eb8Smec void *datap; 70788b464eb8Smec kmutex_t *mp; 70798b464eb8Smec extern vnode_t retired_pages; 70808b464eb8Smec 70818b464eb8Smec /* 70828b464eb8Smec * We need to protect against a possible deadlock here where we own 70838b464eb8Smec * the vnode page hash mutex and want to acquire it again as there 70848b464eb8Smec * are locations in the code, where we unlock a page while holding 70858b464eb8Smec * the mutex which can lead to the page being captured and eventually 70868b464eb8Smec * end up here. As we may be hashing out the old page and hashing into 70878b464eb8Smec * the retire vnode, we need to make sure we don't own them. 70888b464eb8Smec * Other callbacks who do hash operations also need to make sure that 70898b464eb8Smec * before they hashin to a vnode that they do not currently own the 70908b464eb8Smec * vphm mutex otherwise there will be a panic. 70918b464eb8Smec */ 70928b464eb8Smec if (mutex_owned(page_vnode_mutex(&retired_pages))) { 709322addef7Smec page_unlock_nocapture(pp); 70948b464eb8Smec return; 70958b464eb8Smec } 70968b464eb8Smec if (pp->p_vnode != NULL && mutex_owned(page_vnode_mutex(pp->p_vnode))) { 709722addef7Smec page_unlock_nocapture(pp); 70988b464eb8Smec return; 70998b464eb8Smec } 71008b464eb8Smec 71018b464eb8Smec index = PAGE_CAPTURE_HASH(pp); 71028b464eb8Smec 71038b464eb8Smec mp = &page_capture_hash[index].pchh_mutex; 71048b464eb8Smec mutex_enter(mp); 71058b464eb8Smec for (i = 0; i < 2; i++) { 71068b464eb8Smec bp = page_capture_hash[index].lists[i].next; 71078b464eb8Smec while (bp != &page_capture_hash[index].lists[i]) { 71088b464eb8Smec if (bp->pp == pp) { 71098b464eb8Smec szc = bp->szc; 71108b464eb8Smec flags = bp->flags | CAPTURE_ASYNC; 71118b464eb8Smec datap = bp->datap; 71128b464eb8Smec mutex_exit(mp); 71138b464eb8Smec (void) page_trycapture(pp, szc, flags, datap); 71148b464eb8Smec return; 71158b464eb8Smec } 71168b464eb8Smec bp = bp->next; 71178b464eb8Smec } 71188b464eb8Smec } 71198b464eb8Smec 71208b464eb8Smec /* Failed to find page in hash so clear flags and unlock it. */ 71218b464eb8Smec page_clrtoxic(pp, PR_CAPTURE); 71228b464eb8Smec page_unlock(pp); 71238b464eb8Smec 71248b464eb8Smec mutex_exit(mp); 71258b464eb8Smec } 71268b464eb8Smec 71278b464eb8Smec void 71288b464eb8Smec page_capture_init() 71298b464eb8Smec { 71308b464eb8Smec int i; 71318b464eb8Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 71328b464eb8Smec page_capture_hash[i].lists[0].next = 71338b464eb8Smec &page_capture_hash[i].lists[0]; 71348b464eb8Smec page_capture_hash[i].lists[0].prev = 71358b464eb8Smec &page_capture_hash[i].lists[0]; 71368b464eb8Smec page_capture_hash[i].lists[1].next = 71378b464eb8Smec &page_capture_hash[i].lists[1]; 71388b464eb8Smec page_capture_hash[i].lists[1].prev = 71398b464eb8Smec &page_capture_hash[i].lists[1]; 71408b464eb8Smec } 71418b464eb8Smec 71428b464eb8Smec pc_thread_shortwait = 23 * hz; 71438b464eb8Smec pc_thread_longwait = 1201 * hz; 7144a98e9dbfSaguzovsk pc_thread_retry = 3; 71458b464eb8Smec mutex_init(&pc_thread_mutex, NULL, MUTEX_DEFAULT, NULL); 71468b464eb8Smec cv_init(&pc_cv, NULL, CV_DEFAULT, NULL); 71478b464eb8Smec pc_thread_id = thread_create(NULL, 0, page_capture_thread, NULL, 0, &p0, 71488b464eb8Smec TS_RUN, minclsyspri); 71498b464eb8Smec } 71508b464eb8Smec 71518b464eb8Smec /* 71528b464eb8Smec * It is necessary to scrub any failing pages prior to reboot in order to 71538b464eb8Smec * prevent a latent error trap from occurring on the next boot. 71548b464eb8Smec */ 71558b464eb8Smec void 71568b464eb8Smec page_retire_mdboot() 71578b464eb8Smec { 71588b464eb8Smec page_t *pp; 71598b464eb8Smec int i, j; 71608b464eb8Smec page_capture_hash_bucket_t *bp; 71618b464eb8Smec 71628b464eb8Smec /* walk lists looking for pages to scrub */ 71638b464eb8Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 71648b464eb8Smec if (page_capture_hash[i].num_pages == 0) 71658b464eb8Smec continue; 71668b464eb8Smec 71678b464eb8Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 71688b464eb8Smec 71698b464eb8Smec for (j = 0; j < 2; j++) { 71708b464eb8Smec bp = page_capture_hash[i].lists[j].next; 71718b464eb8Smec while (bp != &page_capture_hash[i].lists[j]) { 71728b464eb8Smec pp = bp->pp; 7173954021b7SJustin Frank if (PP_TOXIC(pp)) { 7174954021b7SJustin Frank if (page_trylock(pp, SE_EXCL)) { 7175954021b7SJustin Frank PP_CLRFREE(pp); 7176954021b7SJustin Frank pagescrub(pp, 0, PAGESIZE); 7177954021b7SJustin Frank page_unlock(pp); 7178954021b7SJustin Frank } 71798b464eb8Smec } 71808b464eb8Smec bp = bp->next; 71818b464eb8Smec } 71828b464eb8Smec } 71838b464eb8Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 71848b464eb8Smec } 71858b464eb8Smec } 71868b464eb8Smec 71878b464eb8Smec /* 71888b464eb8Smec * Walk the page_capture_hash trying to capture pages and also cleanup old 71898b464eb8Smec * entries which have expired. 71908b464eb8Smec */ 71918b464eb8Smec void 71928b464eb8Smec page_capture_async() 71938b464eb8Smec { 71948b464eb8Smec page_t *pp; 71958b464eb8Smec int i; 71968b464eb8Smec int ret; 71978b464eb8Smec page_capture_hash_bucket_t *bp1, *bp2; 71988b464eb8Smec uint_t szc; 71998b464eb8Smec uint_t flags; 72008b464eb8Smec void *datap; 72018b464eb8Smec 72028b464eb8Smec /* If there are outstanding pages to be captured, get to work */ 72038b464eb8Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) { 72048b464eb8Smec if (page_capture_hash[i].num_pages == 0) 72058b464eb8Smec continue; 72068b464eb8Smec /* Append list 1 to list 0 and then walk through list 0 */ 72078b464eb8Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 72088b464eb8Smec bp1 = &page_capture_hash[i].lists[1]; 72098b464eb8Smec bp2 = bp1->next; 72108b464eb8Smec if (bp1 != bp2) { 72118b464eb8Smec bp1->prev->next = page_capture_hash[i].lists[0].next; 72128b464eb8Smec bp2->prev = &page_capture_hash[i].lists[0]; 72138b464eb8Smec page_capture_hash[i].lists[0].next->prev = bp1->prev; 72148b464eb8Smec page_capture_hash[i].lists[0].next = bp2; 72158b464eb8Smec bp1->next = bp1; 72168b464eb8Smec bp1->prev = bp1; 72178b464eb8Smec } 72188b464eb8Smec 72198b464eb8Smec /* list[1] will be empty now */ 72208b464eb8Smec 72218b464eb8Smec bp1 = page_capture_hash[i].lists[0].next; 72228b464eb8Smec while (bp1 != &page_capture_hash[i].lists[0]) { 72238b464eb8Smec /* Check expiration time */ 72248b464eb8Smec if ((lbolt > bp1->expires && bp1->expires != -1) || 72258b464eb8Smec page_deleted(bp1->pp)) { 72268b464eb8Smec page_capture_hash[i].lists[0].next = bp1->next; 72278b464eb8Smec bp1->next->prev = 72288b464eb8Smec &page_capture_hash[i].lists[0]; 72298b464eb8Smec page_capture_hash[i].num_pages--; 72308b464eb8Smec 72318b464eb8Smec /* 72328b464eb8Smec * We can safely remove the PR_CAPTURE bit 72338b464eb8Smec * without holding the EXCL lock on the page 72348b464eb8Smec * as the PR_CAPTURE bit requres that the 72358b464eb8Smec * page_capture_hash[].pchh_mutex be held 72368b464eb8Smec * to modify it. 72378b464eb8Smec */ 72388b464eb8Smec page_clrtoxic(bp1->pp, PR_CAPTURE); 72398b464eb8Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 72408b464eb8Smec kmem_free(bp1, sizeof (*bp1)); 72418b464eb8Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 72428b464eb8Smec bp1 = page_capture_hash[i].lists[0].next; 72438b464eb8Smec continue; 72448b464eb8Smec } 72458b464eb8Smec pp = bp1->pp; 72468b464eb8Smec szc = bp1->szc; 72478b464eb8Smec flags = bp1->flags; 72488b464eb8Smec datap = bp1->datap; 72498b464eb8Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 72508b464eb8Smec if (page_trylock(pp, SE_EXCL)) { 72518b464eb8Smec ret = page_trycapture(pp, szc, 72528b464eb8Smec flags | CAPTURE_ASYNC, datap); 72538b464eb8Smec } else { 72548b464eb8Smec ret = 1; /* move to walked hash */ 72558b464eb8Smec } 72568b464eb8Smec 72578b464eb8Smec if (ret != 0) { 72588b464eb8Smec /* Move to walked hash */ 72598b464eb8Smec (void) page_capture_move_to_walked(pp); 72608b464eb8Smec } 72618b464eb8Smec mutex_enter(&page_capture_hash[i].pchh_mutex); 72628b464eb8Smec bp1 = page_capture_hash[i].lists[0].next; 72638b464eb8Smec } 72648b464eb8Smec 72658b464eb8Smec mutex_exit(&page_capture_hash[i].pchh_mutex); 72668b464eb8Smec } 72678b464eb8Smec } 72688b464eb8Smec 7269cee1d74bSjfrank /* 7270cee1d74bSjfrank * This function is called by the page_capture_thread, and is needed in 7271cee1d74bSjfrank * in order to initiate aio cleanup, so that pages used in aio 7272cee1d74bSjfrank * will be unlocked and subsequently retired by page_capture_thread. 7273cee1d74bSjfrank */ 7274cee1d74bSjfrank static int 7275cee1d74bSjfrank do_aio_cleanup(void) 7276cee1d74bSjfrank { 7277cee1d74bSjfrank proc_t *procp; 7278cee1d74bSjfrank int (*aio_cleanup_dr_delete_memory)(proc_t *); 7279cee1d74bSjfrank int cleaned = 0; 7280cee1d74bSjfrank 7281cee1d74bSjfrank if (modload("sys", "kaio") == -1) { 7282cee1d74bSjfrank cmn_err(CE_WARN, "do_aio_cleanup: cannot load kaio"); 7283cee1d74bSjfrank return (0); 7284cee1d74bSjfrank } 7285cee1d74bSjfrank /* 7286cee1d74bSjfrank * We use the aio_cleanup_dr_delete_memory function to 7287cee1d74bSjfrank * initiate the actual clean up; this function will wake 7288cee1d74bSjfrank * up the per-process aio_cleanup_thread. 7289cee1d74bSjfrank */ 7290cee1d74bSjfrank aio_cleanup_dr_delete_memory = (int (*)(proc_t *)) 7291cee1d74bSjfrank modgetsymvalue("aio_cleanup_dr_delete_memory", 0); 7292cee1d74bSjfrank if (aio_cleanup_dr_delete_memory == NULL) { 7293cee1d74bSjfrank cmn_err(CE_WARN, 7294cee1d74bSjfrank "aio_cleanup_dr_delete_memory not found in kaio"); 7295cee1d74bSjfrank return (0); 7296cee1d74bSjfrank } 7297cee1d74bSjfrank mutex_enter(&pidlock); 7298cee1d74bSjfrank for (procp = practive; (procp != NULL); procp = procp->p_next) { 7299cee1d74bSjfrank mutex_enter(&procp->p_lock); 7300cee1d74bSjfrank if (procp->p_aio != NULL) { 7301cee1d74bSjfrank /* cleanup proc's outstanding kaio */ 7302cee1d74bSjfrank cleaned += (*aio_cleanup_dr_delete_memory)(procp); 7303cee1d74bSjfrank } 7304cee1d74bSjfrank mutex_exit(&procp->p_lock); 7305cee1d74bSjfrank } 7306cee1d74bSjfrank mutex_exit(&pidlock); 7307cee1d74bSjfrank return (cleaned); 7308cee1d74bSjfrank } 7309cee1d74bSjfrank 7310cee1d74bSjfrank /* 7311cee1d74bSjfrank * helper function for page_capture_thread 7312cee1d74bSjfrank */ 7313cee1d74bSjfrank static void 7314cee1d74bSjfrank page_capture_handle_outstanding(void) 7315cee1d74bSjfrank { 7316cee1d74bSjfrank int ntry; 7317cee1d74bSjfrank 7318704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States /* Reap pages before attempting capture pages */ 7319704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States kmem_reap(); 7320704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States 7321704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States if ((page_retire_pend_count() > page_retire_pend_kas_count()) && 7322704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States hat_supported(HAT_DYNAMIC_ISM_UNMAP, (void *)0)) { 7323cee1d74bSjfrank /* 7324c2d79585SVijay Balakrishna, SG-RPE * Note: Purging only for platforms that support 7325c2d79585SVijay Balakrishna, SG-RPE * ISM hat_pageunload() - mainly SPARC. On x86/x64 7326c2d79585SVijay Balakrishna, SG-RPE * platforms ISM pages SE_SHARED locked until destroyed. 7327cee1d74bSjfrank */ 7328a98e9dbfSaguzovsk 7329a98e9dbfSaguzovsk /* disable and purge seg_pcache */ 7330a98e9dbfSaguzovsk (void) seg_p_disable(); 7331a98e9dbfSaguzovsk for (ntry = 0; ntry < pc_thread_retry; ntry++) { 7332a98e9dbfSaguzovsk if (!page_retire_pend_count()) 7333a98e9dbfSaguzovsk break; 7334a98e9dbfSaguzovsk if (do_aio_cleanup()) { 7335a98e9dbfSaguzovsk /* 7336a98e9dbfSaguzovsk * allow the apps cleanup threads 7337a98e9dbfSaguzovsk * to run 7338a98e9dbfSaguzovsk */ 7339a98e9dbfSaguzovsk delay(pc_thread_shortwait); 7340cee1d74bSjfrank } 7341cee1d74bSjfrank page_capture_async(); 7342cee1d74bSjfrank } 7343a98e9dbfSaguzovsk /* reenable seg_pcache */ 7344a98e9dbfSaguzovsk seg_p_enable(); 7345704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States 7346704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States /* completed what can be done. break out */ 7347704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States return; 7348cee1d74bSjfrank } 7349704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States 7350704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States /* 7351704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States * For kernel pages and/or unsupported HAT_DYNAMIC_ISM_UNMAP, reap 7352704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States * and then attempt to capture. 7353704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States */ 7354704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States seg_preap(); 7355704b9682SChristopher Baumbauer - Sun Microsystems - San Diego United States page_capture_async(); 7356cee1d74bSjfrank } 7357cee1d74bSjfrank 73588b464eb8Smec /* 73598b464eb8Smec * The page_capture_thread loops forever, looking to see if there are 73608b464eb8Smec * pages still waiting to be captured. 73618b464eb8Smec */ 73628b464eb8Smec static void 73638b464eb8Smec page_capture_thread(void) 73648b464eb8Smec { 73658b464eb8Smec callb_cpr_t c; 73668b464eb8Smec int outstanding; 73678b464eb8Smec int i; 73688b464eb8Smec 73698b464eb8Smec CALLB_CPR_INIT(&c, &pc_thread_mutex, callb_generic_cpr, "page_capture"); 73708b464eb8Smec 73718b464eb8Smec mutex_enter(&pc_thread_mutex); 73728b464eb8Smec for (;;) { 73738b464eb8Smec outstanding = 0; 73748b464eb8Smec for (i = 0; i < NUM_PAGE_CAPTURE_BUCKETS; i++) 73758b464eb8Smec outstanding += page_capture_hash[i].num_pages; 73768b464eb8Smec if (outstanding) { 7377cee1d74bSjfrank page_capture_handle_outstanding(); 73788b464eb8Smec CALLB_CPR_SAFE_BEGIN(&c); 73798b464eb8Smec (void) cv_timedwait(&pc_cv, &pc_thread_mutex, 73808b464eb8Smec lbolt + pc_thread_shortwait); 73818b464eb8Smec CALLB_CPR_SAFE_END(&c, &pc_thread_mutex); 73828b464eb8Smec } else { 73838b464eb8Smec CALLB_CPR_SAFE_BEGIN(&c); 73848b464eb8Smec (void) cv_timedwait(&pc_cv, &pc_thread_mutex, 73858b464eb8Smec lbolt + pc_thread_longwait); 73868b464eb8Smec CALLB_CPR_SAFE_END(&c, &pc_thread_mutex); 73878b464eb8Smec } 73888b464eb8Smec } 73898b464eb8Smec /*NOTREACHED*/ 73908b464eb8Smec } 739106fb6a36Sdv /* 739206fb6a36Sdv * Attempt to locate a bucket that has enough pages to satisfy the request. 739306fb6a36Sdv * The initial check is done without the lock to avoid unneeded contention. 739406fb6a36Sdv * The function returns 1 if enough pages were found, else 0 if it could not 739506fb6a36Sdv * find enough pages in a bucket. 739606fb6a36Sdv */ 739706fb6a36Sdv static int 739806fb6a36Sdv pcf_decrement_bucket(pgcnt_t npages) 739906fb6a36Sdv { 740006fb6a36Sdv struct pcf *p; 740106fb6a36Sdv struct pcf *q; 740206fb6a36Sdv int i; 740306fb6a36Sdv 740406fb6a36Sdv p = &pcf[PCF_INDEX()]; 740506fb6a36Sdv q = &pcf[pcf_fanout]; 740606fb6a36Sdv for (i = 0; i < pcf_fanout; i++) { 740706fb6a36Sdv if (p->pcf_count > npages) { 740806fb6a36Sdv /* 740906fb6a36Sdv * a good one to try. 741006fb6a36Sdv */ 741106fb6a36Sdv mutex_enter(&p->pcf_lock); 741206fb6a36Sdv if (p->pcf_count > npages) { 741306fb6a36Sdv p->pcf_count -= (uint_t)npages; 741406fb6a36Sdv /* 741506fb6a36Sdv * freemem is not protected by any lock. 741606fb6a36Sdv * Thus, we cannot have any assertion 741706fb6a36Sdv * containing freemem here. 741806fb6a36Sdv */ 741906fb6a36Sdv freemem -= npages; 742006fb6a36Sdv mutex_exit(&p->pcf_lock); 742106fb6a36Sdv return (1); 742206fb6a36Sdv } 742306fb6a36Sdv mutex_exit(&p->pcf_lock); 742406fb6a36Sdv } 742506fb6a36Sdv p++; 742606fb6a36Sdv if (p >= q) { 742706fb6a36Sdv p = pcf; 742806fb6a36Sdv } 742906fb6a36Sdv } 743006fb6a36Sdv return (0); 743106fb6a36Sdv } 743206fb6a36Sdv 743306fb6a36Sdv /* 743406fb6a36Sdv * Arguments: 743506fb6a36Sdv * pcftotal_ret: If the value is not NULL and we have walked all the 743606fb6a36Sdv * buckets but did not find enough pages then it will 743706fb6a36Sdv * be set to the total number of pages in all the pcf 743806fb6a36Sdv * buckets. 743906fb6a36Sdv * npages: Is the number of pages we have been requested to 744006fb6a36Sdv * find. 744106fb6a36Sdv * unlock: If set to 0 we will leave the buckets locked if the 744206fb6a36Sdv * requested number of pages are not found. 744306fb6a36Sdv * 744406fb6a36Sdv * Go and try to satisfy the page request from any number of buckets. 744506fb6a36Sdv * This can be a very expensive operation as we have to lock the buckets 744606fb6a36Sdv * we are checking (and keep them locked), starting at bucket 0. 744706fb6a36Sdv * 744806fb6a36Sdv * The function returns 1 if enough pages were found, else 0 if it could not 744906fb6a36Sdv * find enough pages in the buckets. 745006fb6a36Sdv * 745106fb6a36Sdv */ 745206fb6a36Sdv static int 745306fb6a36Sdv pcf_decrement_multiple(pgcnt_t *pcftotal_ret, pgcnt_t npages, int unlock) 745406fb6a36Sdv { 745506fb6a36Sdv struct pcf *p; 745606fb6a36Sdv pgcnt_t pcftotal; 745706fb6a36Sdv int i; 745806fb6a36Sdv 745906fb6a36Sdv p = pcf; 746006fb6a36Sdv /* try to collect pages from several pcf bins */ 746106fb6a36Sdv for (pcftotal = 0, i = 0; i < pcf_fanout; i++) { 746206fb6a36Sdv mutex_enter(&p->pcf_lock); 746306fb6a36Sdv pcftotal += p->pcf_count; 746406fb6a36Sdv if (pcftotal >= npages) { 746506fb6a36Sdv /* 746606fb6a36Sdv * Wow! There are enough pages laying around 746706fb6a36Sdv * to satisfy the request. Do the accounting, 746806fb6a36Sdv * drop the locks we acquired, and go back. 746906fb6a36Sdv * 747006fb6a36Sdv * freemem is not protected by any lock. So, 747106fb6a36Sdv * we cannot have any assertion containing 747206fb6a36Sdv * freemem. 747306fb6a36Sdv */ 747406fb6a36Sdv freemem -= npages; 747506fb6a36Sdv while (p >= pcf) { 747606fb6a36Sdv if (p->pcf_count <= npages) { 747706fb6a36Sdv npages -= p->pcf_count; 747806fb6a36Sdv p->pcf_count = 0; 747906fb6a36Sdv } else { 748006fb6a36Sdv p->pcf_count -= (uint_t)npages; 748106fb6a36Sdv npages = 0; 748206fb6a36Sdv } 748306fb6a36Sdv mutex_exit(&p->pcf_lock); 748406fb6a36Sdv p--; 748506fb6a36Sdv } 748606fb6a36Sdv ASSERT(npages == 0); 748706fb6a36Sdv return (1); 748806fb6a36Sdv } 748906fb6a36Sdv p++; 749006fb6a36Sdv } 749106fb6a36Sdv if (unlock) { 749206fb6a36Sdv /* failed to collect pages - release the locks */ 749306fb6a36Sdv while (--p >= pcf) { 749406fb6a36Sdv mutex_exit(&p->pcf_lock); 749506fb6a36Sdv } 749606fb6a36Sdv } 749706fb6a36Sdv if (pcftotal_ret != NULL) 749806fb6a36Sdv *pcftotal_ret = pcftotal; 749906fb6a36Sdv return (0); 750006fb6a36Sdv } 7501