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 507b65a64Saguzovsk * Common Development and Distribution License (the "License"). 607b65a64Saguzovsk * 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 /* 22*23d9e5acSMichael Corcoran * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 267c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 307c478bd9Sstevel@tonic-gate * The Regents of the University of California 317c478bd9Sstevel@tonic-gate * All Rights Reserved 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 347c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 357c478bd9Sstevel@tonic-gate * contributors. 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #ifndef _VM_ANON_H 397c478bd9Sstevel@tonic-gate #define _VM_ANON_H 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <sys/cred.h> 420209230bSgjelinek #include <sys/zone.h> 437c478bd9Sstevel@tonic-gate #include <vm/seg.h> 447c478bd9Sstevel@tonic-gate #include <vm/vpage.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #ifdef __cplusplus 477c478bd9Sstevel@tonic-gate extern "C" { 487c478bd9Sstevel@tonic-gate #endif 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate /* 517c478bd9Sstevel@tonic-gate * VM - Anonymous pages. 527c478bd9Sstevel@tonic-gate */ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate typedef unsigned long anoff_t; /* anon offsets */ 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * Each anonymous page, either in memory or in swap, has an anon structure. 587c478bd9Sstevel@tonic-gate * The structure (slot) provides a level of indirection between anonymous pages 597c478bd9Sstevel@tonic-gate * and their backing store. 607c478bd9Sstevel@tonic-gate * 617c478bd9Sstevel@tonic-gate * (an_vp, an_off) names the vnode of the anonymous page for this slot. 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * (an_pvp, an_poff) names the location of the physical backing store 647c478bd9Sstevel@tonic-gate * for the page this slot represents. If the name is null there is no 657c478bd9Sstevel@tonic-gate * associated physical store. The physical backing store location can 667c478bd9Sstevel@tonic-gate * change while the slot is in use. 677c478bd9Sstevel@tonic-gate * 687c478bd9Sstevel@tonic-gate * an_hash is a hash list of anon slots. The list is hashed by 697c478bd9Sstevel@tonic-gate * (an_vp, an_off) of the associated anonymous page and provides a 707c478bd9Sstevel@tonic-gate * method of going from the name of an anonymous page to its 717c478bd9Sstevel@tonic-gate * associated anon slot. 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * an_refcnt holds a reference count which is the number of separate 747c478bd9Sstevel@tonic-gate * copies that will need to be created in case of copy-on-write. 757c478bd9Sstevel@tonic-gate * A refcnt > 0 protects the existence of the slot. The refcnt is 767c478bd9Sstevel@tonic-gate * initialized to 1 when the anon slot is created in anon_alloc(). 777c478bd9Sstevel@tonic-gate * If a client obtains an anon slot and allows multiple threads to 787c478bd9Sstevel@tonic-gate * share it, then it is the client's responsibility to insure that 797c478bd9Sstevel@tonic-gate * it does not allow one thread to try to reference the slot at the 807c478bd9Sstevel@tonic-gate * same time as another is trying to decrement the last count and 817c478bd9Sstevel@tonic-gate * destroy the anon slot. E.g., the seg_vn segment type protects 827c478bd9Sstevel@tonic-gate * against this with higher level locks. 837c478bd9Sstevel@tonic-gate */ 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate struct anon { 867c478bd9Sstevel@tonic-gate struct vnode *an_vp; /* vnode of anon page */ 877c478bd9Sstevel@tonic-gate struct vnode *an_pvp; /* vnode of physical backing store */ 887c478bd9Sstevel@tonic-gate anoff_t an_off; /* offset of anon page */ 897c478bd9Sstevel@tonic-gate anoff_t an_poff; /* offset in vnode */ 907c478bd9Sstevel@tonic-gate struct anon *an_hash; /* hash table of anon slots */ 917c478bd9Sstevel@tonic-gate int an_refcnt; /* # of people sharing slot */ 927c478bd9Sstevel@tonic-gate }; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate #ifdef _KERNEL 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * The swapinfo_lock protects: 977c478bd9Sstevel@tonic-gate * swapinfo list 987c478bd9Sstevel@tonic-gate * individual swapinfo structures 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate * The anoninfo_lock protects: 1017c478bd9Sstevel@tonic-gate * anoninfo counters 1027c478bd9Sstevel@tonic-gate * 1037c478bd9Sstevel@tonic-gate * The anonhash_lock protects: 1047c478bd9Sstevel@tonic-gate * anon hash lists 1057c478bd9Sstevel@tonic-gate * anon slot fields 1067c478bd9Sstevel@tonic-gate * 1077c478bd9Sstevel@tonic-gate * Fields in the anon slot which are read-only for the life of the slot 1087c478bd9Sstevel@tonic-gate * (an_vp, an_off) do not require the anonhash_lock be held to access them. 1097c478bd9Sstevel@tonic-gate * If you access a field without the anonhash_lock held you must be holding 1107c478bd9Sstevel@tonic-gate * the slot with an_refcnt to make sure it isn't destroyed. 1117c478bd9Sstevel@tonic-gate * To write (an_pvp, an_poff) in a given slot you must also hold the 1127c478bd9Sstevel@tonic-gate * p_iolock of the anonymous page for slot. 1137c478bd9Sstevel@tonic-gate */ 1147c478bd9Sstevel@tonic-gate extern kmutex_t anoninfo_lock; 1157c478bd9Sstevel@tonic-gate extern kmutex_t swapinfo_lock; 116*23d9e5acSMichael Corcoran extern pad_mutex_t *anonhash_lock; 1177c478bd9Sstevel@tonic-gate extern pad_mutex_t anon_array_lock[]; 1187c478bd9Sstevel@tonic-gate extern kcondvar_t anon_array_cv[]; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate /* 1217c478bd9Sstevel@tonic-gate * Global hash table to provide a function from (vp, off) -> ap 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate extern size_t anon_hash_size; 1247c478bd9Sstevel@tonic-gate extern struct anon **anon_hash; 1257c478bd9Sstevel@tonic-gate #define ANON_HASH_SIZE anon_hash_size 1267c478bd9Sstevel@tonic-gate #define ANON_HASHAVELEN 4 1277c478bd9Sstevel@tonic-gate #define ANON_HASH(VP, OFF) \ 1287c478bd9Sstevel@tonic-gate ((((uintptr_t)(VP) >> 7) ^ ((OFF) >> PAGESHIFT)) & (ANON_HASH_SIZE - 1)) 1297c478bd9Sstevel@tonic-gate 130*23d9e5acSMichael Corcoran #define AH_LOCK_SIZE (2 << NCPU_LOG2) 131*23d9e5acSMichael Corcoran 132*23d9e5acSMichael Corcoran #define AH_MUTEX(vp, off) \ 133*23d9e5acSMichael Corcoran (&anonhash_lock[(ANON_HASH((vp), (off)) & \ 134*23d9e5acSMichael Corcoran (AH_LOCK_SIZE - 1))].pad_mutex) 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate /* 1397c478bd9Sstevel@tonic-gate * Declaration for the Global counters to accurately 1407c478bd9Sstevel@tonic-gate * track the kernel foot print in memory. 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate extern pgcnt_t pages_locked; 1437c478bd9Sstevel@tonic-gate extern pgcnt_t pages_claimed; 1447c478bd9Sstevel@tonic-gate extern pgcnt_t pages_useclaim; 1457c478bd9Sstevel@tonic-gate extern pgcnt_t obp_pages; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate /* 1487c478bd9Sstevel@tonic-gate * Anonymous backing store accounting structure for swapctl. 1497c478bd9Sstevel@tonic-gate * 1507c478bd9Sstevel@tonic-gate * ani_max = maximum amount of swap space 1517c478bd9Sstevel@tonic-gate * (including potentially available physical memory) 1527c478bd9Sstevel@tonic-gate * ani_free = amount of unallocated anonymous memory 1537c478bd9Sstevel@tonic-gate * (some of which might be reserved and including 1547c478bd9Sstevel@tonic-gate * potentially available physical memory) 1557c478bd9Sstevel@tonic-gate * ani_resv = amount of claimed (reserved) anonymous memory 1567c478bd9Sstevel@tonic-gate * 1577c478bd9Sstevel@tonic-gate * The swap data can be aquired more efficiently through the 1587c478bd9Sstevel@tonic-gate * kstats interface. 1597c478bd9Sstevel@tonic-gate * Total slots currently available for reservation = 1607c478bd9Sstevel@tonic-gate * MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree) 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate struct anoninfo { 1637c478bd9Sstevel@tonic-gate pgcnt_t ani_max; 1647c478bd9Sstevel@tonic-gate pgcnt_t ani_free; 1657c478bd9Sstevel@tonic-gate pgcnt_t ani_resv; 1667c478bd9Sstevel@tonic-gate }; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32 1697c478bd9Sstevel@tonic-gate struct anoninfo32 { 1707c478bd9Sstevel@tonic-gate size32_t ani_max; 1717c478bd9Sstevel@tonic-gate size32_t ani_free; 1727c478bd9Sstevel@tonic-gate size32_t ani_resv; 1737c478bd9Sstevel@tonic-gate }; 1747c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32 */ 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * Define the NCPU pool of the ani_free counters. Update the counter 1787c478bd9Sstevel@tonic-gate * of the cpu on which the thread is running and in every clock intr 1797c478bd9Sstevel@tonic-gate * sync anoninfo.ani_free with the current total off all the NCPU entries. 1807c478bd9Sstevel@tonic-gate */ 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate typedef struct ani_free { 1837c478bd9Sstevel@tonic-gate kmutex_t ani_lock; 1847c478bd9Sstevel@tonic-gate pgcnt_t ani_count; 1857c478bd9Sstevel@tonic-gate uchar_t pad[64 - sizeof (kmutex_t) - sizeof (pgcnt_t)]; 1867c478bd9Sstevel@tonic-gate /* XXX 64 = cacheline size */ 1877c478bd9Sstevel@tonic-gate } ani_free_t; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate #define ANI_MAX_POOL 128 1907c478bd9Sstevel@tonic-gate extern ani_free_t ani_free_pool[]; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate #define ANI_ADD(inc) { \ 1937c478bd9Sstevel@tonic-gate ani_free_t *anifp; \ 1947c478bd9Sstevel@tonic-gate int index; \ 1957c478bd9Sstevel@tonic-gate index = (CPU->cpu_id & (ANI_MAX_POOL - 1)); \ 1967c478bd9Sstevel@tonic-gate anifp = &ani_free_pool[index]; \ 1977c478bd9Sstevel@tonic-gate mutex_enter(&anifp->ani_lock); \ 1987c478bd9Sstevel@tonic-gate anifp->ani_count += inc; \ 1997c478bd9Sstevel@tonic-gate mutex_exit(&anifp->ani_lock); \ 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate /* 2037c478bd9Sstevel@tonic-gate * Anon array pointers are allocated in chunks. Each chunk 2047c478bd9Sstevel@tonic-gate * has PAGESIZE/sizeof(u_long *) of anon pointers. 2057c478bd9Sstevel@tonic-gate * There are two levels of arrays for anon array pointers larger 2067c478bd9Sstevel@tonic-gate * than a chunk. The first level points to anon array chunks. 2077c478bd9Sstevel@tonic-gate * The second level consists of chunks of anon pointers. 2087c478bd9Sstevel@tonic-gate * 2097c478bd9Sstevel@tonic-gate * If anon array is smaller than a chunk then the whole anon array 2107c478bd9Sstevel@tonic-gate * is created (memory is allocated for whole anon array). 2117c478bd9Sstevel@tonic-gate * If anon array is larger than a chunk only first level array is 2127c478bd9Sstevel@tonic-gate * allocated. Then other arrays (chunks) are allocated only when 2137c478bd9Sstevel@tonic-gate * they are initialized with anon pointers. 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate struct anon_hdr { 2167c478bd9Sstevel@tonic-gate kmutex_t serial_lock; /* serialize array chunk allocation */ 2177c478bd9Sstevel@tonic-gate pgcnt_t size; /* number of pointers to (anon) pages */ 2187c478bd9Sstevel@tonic-gate void **array_chunk; /* pointers to anon pointers or chunks of */ 2197c478bd9Sstevel@tonic-gate /* anon pointers */ 2207c478bd9Sstevel@tonic-gate int flags; /* ANON_ALLOC_FORCE force preallocation of */ 2217c478bd9Sstevel@tonic-gate /* whole anon array */ 2227c478bd9Sstevel@tonic-gate }; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate #ifdef _LP64 2257c478bd9Sstevel@tonic-gate #define ANON_PTRSHIFT 3 2267c478bd9Sstevel@tonic-gate #define ANON_PTRMASK ~7 2277c478bd9Sstevel@tonic-gate #else 2287c478bd9Sstevel@tonic-gate #define ANON_PTRSHIFT 2 2297c478bd9Sstevel@tonic-gate #define ANON_PTRMASK ~3 2307c478bd9Sstevel@tonic-gate #endif 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate #define ANON_CHUNK_SIZE (PAGESIZE >> ANON_PTRSHIFT) 2337c478bd9Sstevel@tonic-gate #define ANON_CHUNK_SHIFT (PAGESHIFT - ANON_PTRSHIFT) 2347c478bd9Sstevel@tonic-gate #define ANON_CHUNK_OFF (ANON_CHUNK_SIZE - 1) 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate /* 2377c478bd9Sstevel@tonic-gate * Anon flags. 2387c478bd9Sstevel@tonic-gate */ 2397c478bd9Sstevel@tonic-gate #define ANON_SLEEP 0x0 /* ok to block */ 2407c478bd9Sstevel@tonic-gate #define ANON_NOSLEEP 0x1 /* non-blocking call */ 2417c478bd9Sstevel@tonic-gate #define ANON_ALLOC_FORCE 0x2 /* force single level anon array */ 2427c478bd9Sstevel@tonic-gate #define ANON_GROWDOWN 0x4 /* anon array should grow downward */ 2437c478bd9Sstevel@tonic-gate 244c6939658Ssl struct kshmid; 245c6939658Ssl 2467c478bd9Sstevel@tonic-gate /* 2477c478bd9Sstevel@tonic-gate * The anon_map structure is used by various clients of the anon layer to 2487c478bd9Sstevel@tonic-gate * manage anonymous memory. When anonymous memory is shared, 2497c478bd9Sstevel@tonic-gate * then the different clients sharing it will point to the 2507c478bd9Sstevel@tonic-gate * same anon_map structure. Also, if a segment is unmapped 2517c478bd9Sstevel@tonic-gate * in the middle where an anon_map structure exists, the 2527c478bd9Sstevel@tonic-gate * newly created segment will also share the anon_map structure, 2537c478bd9Sstevel@tonic-gate * although the two segments will use different ranges of the 2547c478bd9Sstevel@tonic-gate * anon array. When mappings are private (or shared with 2557c478bd9Sstevel@tonic-gate * a reference count of 1), an unmap operation will free up 2567c478bd9Sstevel@tonic-gate * a range of anon slots in the array given by the anon_map 2577c478bd9Sstevel@tonic-gate * structure. Because of fragmentation due to this unmapping, 2587c478bd9Sstevel@tonic-gate * we have to store the size of the anon array in the anon_map 2597c478bd9Sstevel@tonic-gate * structure so that we can free everything when the referernce 2607c478bd9Sstevel@tonic-gate * count goes to zero. 2617c478bd9Sstevel@tonic-gate * 2627c478bd9Sstevel@tonic-gate * A new rangelock scheme is introduced to make the anon layer scale. 2637c478bd9Sstevel@tonic-gate * A reader/writer lock per anon_amp and an array of system-wide hash 2647c478bd9Sstevel@tonic-gate * locks, anon_array_lock[] are introduced to replace serial_lock and 2657c478bd9Sstevel@tonic-gate * anonmap lock. The writer lock is held when we want to singlethreaD 2667c478bd9Sstevel@tonic-gate * the reference to the anon array pointers or when references to 2677c478bd9Sstevel@tonic-gate * anon_map's members, whereas reader lock and anon_array_lock are 2687c478bd9Sstevel@tonic-gate * held to allows multiple threads to reference different part of 2697c478bd9Sstevel@tonic-gate * anon array. A global set of condition variables, anon_array_cv, 2707c478bd9Sstevel@tonic-gate * are used with anon_array_lock[] to make the hold time of the locks 2717c478bd9Sstevel@tonic-gate * short. 2727c478bd9Sstevel@tonic-gate * 2737c478bd9Sstevel@tonic-gate * szc is used to calculate the index of hash locks and cv's. We 2747c478bd9Sstevel@tonic-gate * could've just used seg->s_szc if not for the possible sharing of 2757c478bd9Sstevel@tonic-gate * anon_amp between SYSV shared memory and ISM, so now we introduce 2767c478bd9Sstevel@tonic-gate * szc in the anon_map structure. For MAP_SHARED, the amp->szc is either 2777c478bd9Sstevel@tonic-gate * 0 (base page size) or page_num_pagesizes() - 1, while MAP_PRIVATE 2787c478bd9Sstevel@tonic-gate * the amp->szc could be anything in [0, page_num_pagesizes() - 1]. 2797c478bd9Sstevel@tonic-gate */ 280a98e9dbfSaguzovsk typedef struct anon_map { 2817c478bd9Sstevel@tonic-gate krwlock_t a_rwlock; /* protect anon_map and anon array */ 2827c478bd9Sstevel@tonic-gate size_t size; /* size in bytes mapped by the anon array */ 2837c478bd9Sstevel@tonic-gate struct anon_hdr *ahp; /* anon array header pointer, containing */ 2847c478bd9Sstevel@tonic-gate /* anon pointer array(s) */ 2857c478bd9Sstevel@tonic-gate size_t swresv; /* swap space reserved for this anon_map */ 28607b65a64Saguzovsk ulong_t refcnt; /* reference count on this structure */ 2877c478bd9Sstevel@tonic-gate ushort_t a_szc; /* max szc among shared processes */ 2887c478bd9Sstevel@tonic-gate void *locality; /* lgroup locality info */ 289c6939658Ssl struct kshmid *a_sp; /* kshmid if amp backs sysV, or NULL */ 290a98e9dbfSaguzovsk int a_purgewait; /* somebody waits for slocks to go away */ 291a98e9dbfSaguzovsk kcondvar_t a_purgecv; /* cv for waiting for slocks to go away */ 292a98e9dbfSaguzovsk kmutex_t a_purgemtx; /* mutex for anonmap_purge() */ 293a98e9dbfSaguzovsk spgcnt_t a_softlockcnt; /* number of pages locked in pcache */ 294a98e9dbfSaguzovsk kmutex_t a_pmtx; /* protects amp's pcache list */ 295a98e9dbfSaguzovsk pcache_link_t a_phead; /* head of amp's pcache list */ 296a98e9dbfSaguzovsk } amp_t; 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate #ifdef _KERNEL 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate #define ANON_BUSY 0x1 3017c478bd9Sstevel@tonic-gate #define ANON_ISBUSY(slot) (*(slot) & ANON_BUSY) 3027c478bd9Sstevel@tonic-gate #define ANON_SETBUSY(slot) (*(slot) |= ANON_BUSY) 3037c478bd9Sstevel@tonic-gate #define ANON_CLRBUSY(slot) (*(slot) &= ~ANON_BUSY) 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate #define ANON_MAP_SHIFT 6 /* log2(sizeof (struct anon_map)) */ 3067c478bd9Sstevel@tonic-gate #define ANON_ARRAY_SHIFT 7 /* log2(ANON_LOCKSIZE) */ 3077c478bd9Sstevel@tonic-gate #define ANON_LOCKSIZE 128 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate #define ANON_LOCK_ENTER(lock, type) rw_enter((lock), (type)) 3107c478bd9Sstevel@tonic-gate #define ANON_LOCK_EXIT(lock) rw_exit((lock)) 311a98e9dbfSaguzovsk #define ANON_LOCK_HELD(lock) RW_LOCK_HELD((lock)) 312a98e9dbfSaguzovsk #define ANON_READ_HELD(lock) RW_READ_HELD((lock)) 313a98e9dbfSaguzovsk #define ANON_WRITE_HELD(lock) RW_WRITE_HELD((lock)) 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate #define ANON_ARRAY_HASH(amp, idx)\ 3167c478bd9Sstevel@tonic-gate ((((idx) + ((idx) >> ANON_ARRAY_SHIFT) +\ 3177c478bd9Sstevel@tonic-gate ((idx) >> (ANON_ARRAY_SHIFT << 1)) +\ 3187c478bd9Sstevel@tonic-gate ((idx) >> (ANON_ARRAY_SHIFT + (ANON_ARRAY_SHIFT << 1)))) ^\ 3197c478bd9Sstevel@tonic-gate ((uintptr_t)(amp) >> ANON_MAP_SHIFT)) & (ANON_LOCKSIZE - 1)) 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate typedef struct anon_sync_obj { 3227c478bd9Sstevel@tonic-gate kmutex_t *sync_mutex; 3237c478bd9Sstevel@tonic-gate kcondvar_t *sync_cv; 3247c478bd9Sstevel@tonic-gate ulong_t *sync_data; 3257c478bd9Sstevel@tonic-gate } anon_sync_obj_t; 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate /* 3287c478bd9Sstevel@tonic-gate * Anonymous backing store accounting structure for kernel. 3297c478bd9Sstevel@tonic-gate * ani_max = total reservable slots on physical (disk-backed) swap 3307c478bd9Sstevel@tonic-gate * ani_phys_resv = total phys slots reserved for use by clients 3317c478bd9Sstevel@tonic-gate * ani_mem_resv = total mem slots reserved for use by clients 3327c478bd9Sstevel@tonic-gate * ani_free = # unallocated physical slots + # of reserved unallocated 3337c478bd9Sstevel@tonic-gate * memory slots 3347c478bd9Sstevel@tonic-gate */ 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate /* 3377c478bd9Sstevel@tonic-gate * Initial total swap slots available for reservation 3387c478bd9Sstevel@tonic-gate */ 3397c478bd9Sstevel@tonic-gate #define TOTAL_AVAILABLE_SWAP \ 3407c478bd9Sstevel@tonic-gate (k_anoninfo.ani_max + MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate /* 3437c478bd9Sstevel@tonic-gate * Swap slots currently available for reservation 3447c478bd9Sstevel@tonic-gate */ 345a98e9dbfSaguzovsk #define CURRENT_TOTAL_AVAILABLE_SWAP \ 3467c478bd9Sstevel@tonic-gate ((k_anoninfo.ani_max - k_anoninfo.ani_phys_resv) + \ 347a98e9dbfSaguzovsk MAX((spgcnt_t)(availrmem - swapfs_minfree), 0)) 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate struct k_anoninfo { 3507c478bd9Sstevel@tonic-gate pgcnt_t ani_max; /* total reservable slots on phys */ 3517c478bd9Sstevel@tonic-gate /* (disk) swap */ 3527c478bd9Sstevel@tonic-gate pgcnt_t ani_free; /* # of unallocated phys and mem slots */ 3537c478bd9Sstevel@tonic-gate pgcnt_t ani_phys_resv; /* # of reserved phys (disk) slots */ 3547c478bd9Sstevel@tonic-gate pgcnt_t ani_mem_resv; /* # of reserved mem slots */ 3557c478bd9Sstevel@tonic-gate pgcnt_t ani_locked_swap; /* # of swap slots locked in reserved */ 3567c478bd9Sstevel@tonic-gate /* mem swap */ 3577c478bd9Sstevel@tonic-gate }; 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate extern struct k_anoninfo k_anoninfo; 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate extern void anon_init(void); 3627c478bd9Sstevel@tonic-gate extern struct anon *anon_alloc(struct vnode *, anoff_t); 3637c478bd9Sstevel@tonic-gate extern void anon_dup(struct anon_hdr *, ulong_t, 3647c478bd9Sstevel@tonic-gate struct anon_hdr *, ulong_t, size_t); 3657c478bd9Sstevel@tonic-gate extern void anon_dup_fill_holes(struct anon_hdr *, ulong_t, 3667c478bd9Sstevel@tonic-gate struct anon_hdr *, ulong_t, size_t, uint_t, int); 3677c478bd9Sstevel@tonic-gate extern int anon_fill_cow_holes(struct seg *, caddr_t, struct anon_hdr *, 3687c478bd9Sstevel@tonic-gate ulong_t, struct vnode *, u_offset_t, size_t, uint_t, 3697c478bd9Sstevel@tonic-gate uint_t, struct vpage [], struct cred *); 3707c478bd9Sstevel@tonic-gate extern void anon_free(struct anon_hdr *, ulong_t, size_t); 3717c478bd9Sstevel@tonic-gate extern void anon_free_pages(struct anon_hdr *, ulong_t, size_t, uint_t); 3722ba723d8Smec extern void anon_disclaim(struct anon_map *, ulong_t, size_t); 3737c478bd9Sstevel@tonic-gate extern int anon_getpage(struct anon **, uint_t *, struct page **, 3747c478bd9Sstevel@tonic-gate size_t, struct seg *, caddr_t, enum seg_rw, struct cred *); 3757c478bd9Sstevel@tonic-gate extern int swap_getconpage(struct vnode *, u_offset_t, size_t, 37607b65a64Saguzovsk uint_t *, page_t *[], size_t, page_t *, uint_t *, 3777c478bd9Sstevel@tonic-gate spgcnt_t *, struct seg *, caddr_t, 3787c478bd9Sstevel@tonic-gate enum seg_rw, struct cred *); 3797c478bd9Sstevel@tonic-gate extern int anon_map_getpages(struct anon_map *, ulong_t, 3807c478bd9Sstevel@tonic-gate uint_t, struct seg *, caddr_t, uint_t, 3817c478bd9Sstevel@tonic-gate uint_t *, page_t *[], uint_t *, 3822cb27123Saguzovsk struct vpage [], enum seg_rw, int, int, int, struct cred *); 3837c478bd9Sstevel@tonic-gate extern int anon_map_privatepages(struct anon_map *, ulong_t, 3847c478bd9Sstevel@tonic-gate uint_t, struct seg *, caddr_t, uint_t, 3852cb27123Saguzovsk page_t *[], struct vpage [], int, int, struct cred *); 3867c478bd9Sstevel@tonic-gate extern struct page *anon_private(struct anon **, struct seg *, 3877c478bd9Sstevel@tonic-gate caddr_t, uint_t, struct page *, 3887c478bd9Sstevel@tonic-gate int, struct cred *); 3897c478bd9Sstevel@tonic-gate extern struct page *anon_zero(struct seg *, caddr_t, 3907c478bd9Sstevel@tonic-gate struct anon **, struct cred *); 3917c478bd9Sstevel@tonic-gate extern int anon_map_createpages(struct anon_map *, ulong_t, 3927c478bd9Sstevel@tonic-gate size_t, struct page **, 3937c478bd9Sstevel@tonic-gate struct seg *, caddr_t, 3947c478bd9Sstevel@tonic-gate enum seg_rw, struct cred *); 3957c478bd9Sstevel@tonic-gate extern int anon_map_demotepages(struct anon_map *, ulong_t, 3967c478bd9Sstevel@tonic-gate struct seg *, caddr_t, uint_t, 3977c478bd9Sstevel@tonic-gate struct vpage [], struct cred *); 39807b65a64Saguzovsk extern void anon_shmap_free_pages(struct anon_map *, ulong_t, size_t); 3992cb27123Saguzovsk extern int anon_resvmem(size_t, boolean_t, zone_t *, int); 4000209230bSgjelinek extern void anon_unresvmem(size_t, zone_t *); 4012cb27123Saguzovsk extern struct anon_map *anonmap_alloc(size_t, size_t, int); 4027c478bd9Sstevel@tonic-gate extern void anonmap_free(struct anon_map *); 403a98e9dbfSaguzovsk extern void anonmap_purge(struct anon_map *); 404a98e9dbfSaguzovsk extern void anon_swap_free(struct anon *, struct page *); 4057c478bd9Sstevel@tonic-gate extern void anon_decref(struct anon *); 4067c478bd9Sstevel@tonic-gate extern int non_anon(struct anon_hdr *, ulong_t, u_offset_t *, size_t *); 4077c478bd9Sstevel@tonic-gate extern pgcnt_t anon_pages(struct anon_hdr *, ulong_t, pgcnt_t); 4087c478bd9Sstevel@tonic-gate extern int anon_swap_adjust(pgcnt_t); 4097c478bd9Sstevel@tonic-gate extern void anon_swap_restore(pgcnt_t); 4107c478bd9Sstevel@tonic-gate extern struct anon_hdr *anon_create(pgcnt_t, int); 4117c478bd9Sstevel@tonic-gate extern void anon_release(struct anon_hdr *, pgcnt_t); 4127c478bd9Sstevel@tonic-gate extern struct anon *anon_get_ptr(struct anon_hdr *, ulong_t); 4137c478bd9Sstevel@tonic-gate extern ulong_t *anon_get_slot(struct anon_hdr *, ulong_t); 4147c478bd9Sstevel@tonic-gate extern struct anon *anon_get_next_ptr(struct anon_hdr *, ulong_t *); 4157c478bd9Sstevel@tonic-gate extern int anon_set_ptr(struct anon_hdr *, ulong_t, struct anon *, int); 4167c478bd9Sstevel@tonic-gate extern int anon_copy_ptr(struct anon_hdr *, ulong_t, 4177c478bd9Sstevel@tonic-gate struct anon_hdr *, ulong_t, pgcnt_t, int); 4187c478bd9Sstevel@tonic-gate extern pgcnt_t anon_grow(struct anon_hdr *, ulong_t *, pgcnt_t, pgcnt_t, int); 4197c478bd9Sstevel@tonic-gate extern void anon_array_enter(struct anon_map *, ulong_t, 4207c478bd9Sstevel@tonic-gate anon_sync_obj_t *); 42187015465Scwb extern int anon_array_try_enter(struct anon_map *, ulong_t, 42287015465Scwb anon_sync_obj_t *); 4237c478bd9Sstevel@tonic-gate extern void anon_array_exit(anon_sync_obj_t *); 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate /* 4267c478bd9Sstevel@tonic-gate * anon_resv checks to see if there is enough swap space to fulfill a 4277c478bd9Sstevel@tonic-gate * request and if so, reserves the appropriate anonymous memory resources. 4287c478bd9Sstevel@tonic-gate * anon_checkspace just checks to see if there is space to fulfill the request, 4297c478bd9Sstevel@tonic-gate * without taking any resources. Both return 1 if successful and 0 if not. 4300209230bSgjelinek * 4310209230bSgjelinek * Macros are provided as anon reservation is usually charged to the zone of 4320209230bSgjelinek * the current process. In some cases (such as anon reserved by tmpfs), a 4330209230bSgjelinek * zone pointer is needed to charge the appropriate zone. 4347c478bd9Sstevel@tonic-gate */ 4350209230bSgjelinek #define anon_unresv(size) anon_unresvmem(size, curproc->p_zone) 4360209230bSgjelinek #define anon_unresv_zone(size, zone) anon_unresvmem(size, zone) 4372cb27123Saguzovsk #define anon_resv(size) \ 4382cb27123Saguzovsk anon_resvmem((size), 1, curproc->p_zone, 1) 4392cb27123Saguzovsk #define anon_resv_zone(size, zone) anon_resvmem((size), 1, zone, 1) 4408c06a490Sjj #define anon_checkspace(size, zone) anon_resvmem((size), 0, zone, 0) 4412cb27123Saguzovsk #define anon_try_resv_zone(size, zone) anon_resvmem((size), 1, zone, 0) 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate /* 4447c478bd9Sstevel@tonic-gate * Flags to anon_private 4457c478bd9Sstevel@tonic-gate */ 4467c478bd9Sstevel@tonic-gate #define STEAL_PAGE 0x1 /* page can be stolen */ 4477c478bd9Sstevel@tonic-gate #define LOCK_PAGE 0x2 /* page must be ``logically'' locked */ 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * SEGKP ANON pages that are locked are assumed to be LWP stack pages 4517c478bd9Sstevel@tonic-gate * and thus count towards the user pages locked count. 4527c478bd9Sstevel@tonic-gate * This value is protected by the same lock as availrmem. 4537c478bd9Sstevel@tonic-gate */ 4547c478bd9Sstevel@tonic-gate extern pgcnt_t anon_segkp_pages_locked; 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate extern int anon_debug; 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate #ifdef ANON_DEBUG 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate #define A_ANON 0x01 4617c478bd9Sstevel@tonic-gate #define A_RESV 0x02 4627c478bd9Sstevel@tonic-gate #define A_MRESV 0x04 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate /* vararg-like debugging macro. */ 4657c478bd9Sstevel@tonic-gate #define ANON_PRINT(f, printf_args) \ 4667c478bd9Sstevel@tonic-gate if (anon_debug & f) \ 4677c478bd9Sstevel@tonic-gate printf printf_args 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate #else /* ANON_DEBUG */ 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate #define ANON_PRINT(f, printf_args) 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate #endif /* ANON_DEBUG */ 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate #ifdef __cplusplus 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate #endif 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate #endif /* _VM_ANON_H */ 482