1e2068d0bSJeff Roberson /*- 2e2068d0bSJeff Roberson * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 3e2068d0bSJeff Roberson * 4e2068d0bSJeff Roberson * Copyright (c) 1991, 1993 5e2068d0bSJeff Roberson * The Regents of the University of California. All rights reserved. 6e2068d0bSJeff Roberson * 7e2068d0bSJeff Roberson * This code is derived from software contributed to Berkeley by 8e2068d0bSJeff Roberson * The Mach Operating System project at Carnegie-Mellon University. 9e2068d0bSJeff Roberson * 10e2068d0bSJeff Roberson * Redistribution and use in source and binary forms, with or without 11e2068d0bSJeff Roberson * modification, are permitted provided that the following conditions 12e2068d0bSJeff Roberson * are met: 13e2068d0bSJeff Roberson * 1. Redistributions of source code must retain the above copyright 14e2068d0bSJeff Roberson * notice, this list of conditions and the following disclaimer. 15e2068d0bSJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 16e2068d0bSJeff Roberson * notice, this list of conditions and the following disclaimer in the 17e2068d0bSJeff Roberson * documentation and/or other materials provided with the distribution. 18e2068d0bSJeff Roberson * 3. Neither the name of the University nor the names of its contributors 19e2068d0bSJeff Roberson * may be used to endorse or promote products derived from this software 20e2068d0bSJeff Roberson * without specific prior written permission. 21e2068d0bSJeff Roberson * 22e2068d0bSJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23e2068d0bSJeff Roberson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24e2068d0bSJeff Roberson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25e2068d0bSJeff Roberson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26e2068d0bSJeff Roberson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27e2068d0bSJeff Roberson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28e2068d0bSJeff Roberson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29e2068d0bSJeff Roberson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30e2068d0bSJeff Roberson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31e2068d0bSJeff Roberson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32e2068d0bSJeff Roberson * SUCH DAMAGE. 33e2068d0bSJeff Roberson * 34e2068d0bSJeff Roberson * from: @(#)vm_page.h 8.2 (Berkeley) 12/13/93 35e2068d0bSJeff Roberson * 36e2068d0bSJeff Roberson * 37e2068d0bSJeff Roberson * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38e2068d0bSJeff Roberson * All rights reserved. 39e2068d0bSJeff Roberson * 40e2068d0bSJeff Roberson * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41e2068d0bSJeff Roberson * 42e2068d0bSJeff Roberson * Permission to use, copy, modify and distribute this software and 43e2068d0bSJeff Roberson * its documentation is hereby granted, provided that both the copyright 44e2068d0bSJeff Roberson * notice and this permission notice appear in all copies of the 45e2068d0bSJeff Roberson * software, derivative works or modified versions, and any portions 46e2068d0bSJeff Roberson * thereof, and that both notices appear in supporting documentation. 47e2068d0bSJeff Roberson * 48e2068d0bSJeff Roberson * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49e2068d0bSJeff Roberson * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50e2068d0bSJeff Roberson * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51e2068d0bSJeff Roberson * 52e2068d0bSJeff Roberson * Carnegie Mellon requests users of this software to return to 53e2068d0bSJeff Roberson * 54e2068d0bSJeff Roberson * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55e2068d0bSJeff Roberson * School of Computer Science 56e2068d0bSJeff Roberson * Carnegie Mellon University 57e2068d0bSJeff Roberson * Pittsburgh PA 15213-3890 58e2068d0bSJeff Roberson * 59e2068d0bSJeff Roberson * any improvements or extensions that they make and grant Carnegie the 60e2068d0bSJeff Roberson * rights to redistribute these changes. 61e2068d0bSJeff Roberson * 62e2068d0bSJeff Roberson * $FreeBSD$ 63e2068d0bSJeff Roberson */ 64e2068d0bSJeff Roberson 65e2068d0bSJeff Roberson #ifndef _VM_PAGEQUEUE_ 66e2068d0bSJeff Roberson #define _VM_PAGEQUEUE_ 67e2068d0bSJeff Roberson 68e2068d0bSJeff Roberson #ifdef _KERNEL 69e2068d0bSJeff Roberson struct vm_pagequeue { 70e2068d0bSJeff Roberson struct mtx pq_mutex; 71e2068d0bSJeff Roberson struct pglist pq_pl; 72e2068d0bSJeff Roberson int pq_cnt; 73e2068d0bSJeff Roberson const char * const pq_name; 74899fe184SMark Johnston uint64_t pq_pdpages; 75e2068d0bSJeff Roberson } __aligned(CACHE_LINE_SIZE); 76e2068d0bSJeff Roberson 775cd29d0fSMark Johnston #ifndef VM_BATCHQUEUE_SIZE 785cd29d0fSMark Johnston #define VM_BATCHQUEUE_SIZE 7 795cd29d0fSMark Johnston #endif 805cd29d0fSMark Johnston 815cd29d0fSMark Johnston struct vm_batchqueue { 825cd29d0fSMark Johnston vm_page_t bq_pa[VM_BATCHQUEUE_SIZE]; 835cd29d0fSMark Johnston int bq_cnt; 845cd29d0fSMark Johnston } __aligned(CACHE_LINE_SIZE); 855cd29d0fSMark Johnston 86c33e3a64SJeff Roberson #include <vm/uma.h> 875cd29d0fSMark Johnston #include <sys/pidctrl.h> 885f8cd1c0SJeff Roberson struct sysctl_oid; 89e2068d0bSJeff Roberson 9030fbfddaSJeff Roberson /* 91*9c770a27SMark Johnston * One vm_domain per NUMA domain. Contains pagequeues, free page structures, 9230fbfddaSJeff Roberson * and accounting. 9330fbfddaSJeff Roberson * 9430fbfddaSJeff Roberson * Lock Key: 9530fbfddaSJeff Roberson * f vmd_free_mtx 9630fbfddaSJeff Roberson * p vmd_pageout_mtx 9730fbfddaSJeff Roberson * d vm_domainset_lock 9830fbfddaSJeff Roberson * a atomic 9930fbfddaSJeff Roberson * c const after boot 10060684862SMark Johnston * q page queue lock 101*9c770a27SMark Johnston * 102*9c770a27SMark Johnston * A unique page daemon thread manages each vm_domain structure and is 103*9c770a27SMark Johnston * responsible for ensuring that some free memory is available by freeing 104*9c770a27SMark Johnston * inactive pages and aging active pages. To decide how many pages to process, 105*9c770a27SMark Johnston * it uses thresholds derived from the number of pages in the domain: 106*9c770a27SMark Johnston * 107*9c770a27SMark Johnston * vmd_page_count 108*9c770a27SMark Johnston * --- 109*9c770a27SMark Johnston * | 110*9c770a27SMark Johnston * |-> vmd_inactive_target (~3%) 111*9c770a27SMark Johnston * | - The active queue scan target is given by 112*9c770a27SMark Johnston * | (vmd_inactive_target + vmd_free_target - vmd_free_count). 113*9c770a27SMark Johnston * | 114*9c770a27SMark Johnston * | 115*9c770a27SMark Johnston * |-> vmd_free_target (~2%) 116*9c770a27SMark Johnston * | - Target for page reclamation. 117*9c770a27SMark Johnston * | 118*9c770a27SMark Johnston * |-> vmd_pageout_wakeup_thresh (~1.8%) 119*9c770a27SMark Johnston * | - Threshold for waking up the page daemon. 120*9c770a27SMark Johnston * | 121*9c770a27SMark Johnston * | 122*9c770a27SMark Johnston * |-> vmd_free_min (~0.5%) 123*9c770a27SMark Johnston * | - First low memory threshold. 124*9c770a27SMark Johnston * | - Causes per-CPU caching to be lazily disabled in UMA. 125*9c770a27SMark Johnston * | - vm_wait() sleeps below this threshold. 126*9c770a27SMark Johnston * | 127*9c770a27SMark Johnston * |-> vmd_free_severe (~0.25%) 128*9c770a27SMark Johnston * | - Second low memory threshold. 129*9c770a27SMark Johnston * | - Triggers aggressive UMA reclamation, disables delayed buffer 130*9c770a27SMark Johnston * | writes. 131*9c770a27SMark Johnston * | 132*9c770a27SMark Johnston * |-> vmd_free_reserved (~0.13%) 133*9c770a27SMark Johnston * | - Minimum for VM_ALLOC_NORMAL page allocations. 134*9c770a27SMark Johnston * |-> vmd_pageout_free_min (32 + 2 pages) 135*9c770a27SMark Johnston * | - Minimum for waking a page daemon thread sleeping in vm_wait(). 136*9c770a27SMark Johnston * |-> vmd_interrupt_free_min (2 pages) 137*9c770a27SMark Johnston * | - Minimum for VM_ALLOC_SYSTEM page allocations. 138*9c770a27SMark Johnston * --- 139*9c770a27SMark Johnston * 140*9c770a27SMark Johnston *-- 141*9c770a27SMark Johnston * Free page count regulation: 142*9c770a27SMark Johnston * 143*9c770a27SMark Johnston * The page daemon attempts to ensure that the free page count is above the free 144*9c770a27SMark Johnston * target. It wakes up periodically (every 100ms) to input the current free 145*9c770a27SMark Johnston * page shortage (free_target - free_count) to a PID controller, which in 146*9c770a27SMark Johnston * response outputs the number of pages to attempt to reclaim. The shortage's 147*9c770a27SMark Johnston * current magnitude, rate of change, and cumulative value are together used to 148*9c770a27SMark Johnston * determine the controller's output. The page daemon target thus adapts 149*9c770a27SMark Johnston * dynamically to the system's demand for free pages, resulting in less 150*9c770a27SMark Johnston * burstiness than a simple hysteresis loop. 151*9c770a27SMark Johnston * 152*9c770a27SMark Johnston * When the free page count drops below the wakeup threshold, 153*9c770a27SMark Johnston * vm_domain_allocate() proactively wakes up the page daemon. This helps ensure 154*9c770a27SMark Johnston * that the system responds promptly to a large instantaneous free page 155*9c770a27SMark Johnston * shortage. 156*9c770a27SMark Johnston * 157*9c770a27SMark Johnston * The page daemon also attempts to ensure that some fraction of the system's 158*9c770a27SMark Johnston * memory is present in the inactive (I) and laundry (L) page queues, so that it 159*9c770a27SMark Johnston * can respond promptly to a sudden free page shortage. In particular, the page 160*9c770a27SMark Johnston * daemon thread aggressively scans active pages so long as the following 161*9c770a27SMark Johnston * condition holds: 162*9c770a27SMark Johnston * 163*9c770a27SMark Johnston * len(I) + len(L) + free_target - free_count < inactive_target 164*9c770a27SMark Johnston * 165*9c770a27SMark Johnston * Otherwise, when the inactive target is met, the page daemon periodically 166*9c770a27SMark Johnston * scans a small portion of the active queue in order to maintain up-to-date 167*9c770a27SMark Johnston * per-page access history. Unreferenced pages in the active queue thus 168*9c770a27SMark Johnston * eventually migrate to the inactive queue. 169*9c770a27SMark Johnston * 170*9c770a27SMark Johnston * The per-domain laundry thread periodically launders dirty pages based on the 171*9c770a27SMark Johnston * number of clean pages freed by the page daemon since the last laundering. If 172*9c770a27SMark Johnston * the page daemon fails to meet its scan target (i.e., the PID controller 173*9c770a27SMark Johnston * output) because of a shortage of clean inactive pages, the laundry thread 174*9c770a27SMark Johnston * attempts to launder enough pages to meet the free page target. 175*9c770a27SMark Johnston * 176*9c770a27SMark Johnston *-- 177*9c770a27SMark Johnston * Page allocation priorities: 178*9c770a27SMark Johnston * 179*9c770a27SMark Johnston * The system defines three page allocation priorities: VM_ALLOC_NORMAL, 180*9c770a27SMark Johnston * VM_ALLOC_SYSTEM and VM_ALLOC_INTERRUPT. An interrupt-priority allocation can 181*9c770a27SMark Johnston * claim any free page. This priority is used in the pmap layer when attempting 182*9c770a27SMark Johnston * to allocate a page for the kernel page tables; in such cases an allocation 183*9c770a27SMark Johnston * failure will usually result in a kernel panic. The system priority is used 184*9c770a27SMark Johnston * for most other kernel memory allocations, for instance by UMA's slab 185*9c770a27SMark Johnston * allocator or the buffer cache. Such allocations will fail if the free count 186*9c770a27SMark Johnston * is below interrupt_free_min. All other allocations occur at the normal 187*9c770a27SMark Johnston * priority, which is typically used for allocation of user pages, for instance 188*9c770a27SMark Johnston * in the page fault handler or when allocating page table pages or pv_entry 189*9c770a27SMark Johnston * structures for user pmaps. Such allocations fail if the free count is below 190*9c770a27SMark Johnston * the free_reserved threshold. 191*9c770a27SMark Johnston * 192*9c770a27SMark Johnston *-- 193*9c770a27SMark Johnston * Free memory shortages: 194*9c770a27SMark Johnston * 195*9c770a27SMark Johnston * The system uses the free_min and free_severe thresholds to apply 196*9c770a27SMark Johnston * back-pressure and give the page daemon a chance to recover. When a page 197*9c770a27SMark Johnston * allocation fails due to a shortage and the allocating thread cannot handle 198*9c770a27SMark Johnston * failure, it may call vm_wait() to sleep until free pages are available. 199*9c770a27SMark Johnston * vm_domain_freecnt_inc() wakes sleeping threads once the free page count rises 200*9c770a27SMark Johnston * above the free_min threshold; the page daemon and laundry threads are given 201*9c770a27SMark Johnston * priority and will wake up once free_count reaches the (much smaller) 202*9c770a27SMark Johnston * pageout_free_min threshold. 203*9c770a27SMark Johnston * 204*9c770a27SMark Johnston * On NUMA systems, the domainset iterators always prefer NUMA domains where the 205*9c770a27SMark Johnston * free page count is above the free_min threshold. This means that given the 206*9c770a27SMark Johnston * choice between two NUMA domains, one above the free_min threshold and one 207*9c770a27SMark Johnston * below, the former will be used to satisfy the allocation request regardless 208*9c770a27SMark Johnston * of the domain selection policy. 209*9c770a27SMark Johnston * 210*9c770a27SMark Johnston * In addition to reclaiming memory from the page queues, the vm_lowmem event 211*9c770a27SMark Johnston * fires every ten seconds so long as the system is under memory pressure (i.e., 212*9c770a27SMark Johnston * vmd_free_count < vmd_free_target). This allows kernel subsystems to register 213*9c770a27SMark Johnston * for notifications of free page shortages, upon which they may shrink their 214*9c770a27SMark Johnston * caches. Following a vm_lowmem event, UMA's caches are pruned to ensure that 215*9c770a27SMark Johnston * they do not contain an excess of unused memory. When a domain is below the 216*9c770a27SMark Johnston * free_min threshold, UMA limits the population of per-CPU caches. When a 217*9c770a27SMark Johnston * domain falls below the free_severe threshold, UMA's caches are completely 218*9c770a27SMark Johnston * drained. 219*9c770a27SMark Johnston * 220*9c770a27SMark Johnston * If the system encounters a global memory shortage, it may resort to the 221*9c770a27SMark Johnston * out-of-memory (OOM) killer, which selects a process and delivers SIGKILL in a 222*9c770a27SMark Johnston * last-ditch attempt to free up some pages. Either of the two following 223*9c770a27SMark Johnston * conditions will activate the OOM killer: 224*9c770a27SMark Johnston * 225*9c770a27SMark Johnston * 1. The page daemons collectively fail to reclaim any pages during their 226*9c770a27SMark Johnston * inactive queue scans. After vm_pageout_oom_seq consecutive scans fail, 227*9c770a27SMark Johnston * the page daemon thread votes for an OOM kill, and an OOM kill is 228*9c770a27SMark Johnston * triggered when all page daemons have voted. This heuristic is strict and 229*9c770a27SMark Johnston * may fail to trigger even when the system is effectively deadlocked. 230*9c770a27SMark Johnston * 231*9c770a27SMark Johnston * 2. Threads in the user fault handler are repeatedly unable to make progress 232*9c770a27SMark Johnston * while allocating a page to satisfy the fault. After 233*9c770a27SMark Johnston * vm_pfault_oom_attempts page allocation failures with intervening 234*9c770a27SMark Johnston * vm_wait() calls, the faulting thread will trigger an OOM kill. 23530fbfddaSJeff Roberson */ 236e2068d0bSJeff Roberson struct vm_domain { 237e2068d0bSJeff Roberson struct vm_pagequeue vmd_pagequeues[PQ_COUNT]; 238e2068d0bSJeff Roberson struct mtx_padalign vmd_free_mtx; 23930fbfddaSJeff Roberson struct mtx_padalign vmd_pageout_mtx; 240d9a73522SMark Johnston struct vm_pgcache { 241d9a73522SMark Johnston int domain; 242d9a73522SMark Johnston int pool; 243d9a73522SMark Johnston uma_zone_t zone; 244d9a73522SMark Johnston } vmd_pgcache[VM_NFREEPOOL]; 2450766f278SJonathan T. Looney struct vmem *vmd_kernel_arena; /* (c) per-domain kva R/W arena. */ 2460766f278SJonathan T. Looney struct vmem *vmd_kernel_rwx_arena; /* (c) per-domain kva R/W/X arena. */ 24730fbfddaSJeff Roberson u_int vmd_domain; /* (c) Domain number. */ 24830fbfddaSJeff Roberson u_int vmd_page_count; /* (c) Total page count. */ 24930fbfddaSJeff Roberson long vmd_segs; /* (c) bitmask of the segments */ 25030fbfddaSJeff Roberson u_int __aligned(CACHE_LINE_SIZE) vmd_free_count; /* (a,f) free page count */ 25130fbfddaSJeff Roberson u_int vmd_pageout_deficit; /* (a) Estimated number of pages deficit */ 25230fbfddaSJeff Roberson uint8_t vmd_pad[CACHE_LINE_SIZE - (sizeof(u_int) * 2)]; 253e2068d0bSJeff Roberson 25430fbfddaSJeff Roberson /* Paging control variables, used within single threaded page daemon. */ 2555f8cd1c0SJeff Roberson struct pidctrl vmd_pid; /* Pageout controller. */ 256e2068d0bSJeff Roberson boolean_t vmd_oom; 257e2068d0bSJeff Roberson int vmd_oom_seq; 258e2068d0bSJeff Roberson int vmd_last_active_scan; 2595cd29d0fSMark Johnston struct vm_page vmd_markers[PQ_COUNT]; /* (q) markers for queue scans */ 260e2068d0bSJeff Roberson struct vm_page vmd_inacthead; /* marker for LRU-defeating insertions */ 2615cd29d0fSMark Johnston struct vm_page vmd_clock[2]; /* markers for active queue scan */ 262e2068d0bSJeff Roberson 26330fbfddaSJeff Roberson int vmd_pageout_wanted; /* (a, p) pageout daemon wait channel */ 26430fbfddaSJeff Roberson int vmd_pageout_pages_needed; /* (d) page daemon waiting for pages? */ 26530fbfddaSJeff Roberson bool vmd_minset; /* (d) Are we in vm_min_domains? */ 26630fbfddaSJeff Roberson bool vmd_severeset; /* (d) Are we in vm_severe_domains? */ 267e2068d0bSJeff Roberson enum { 268e2068d0bSJeff Roberson VM_LAUNDRY_IDLE = 0, 269e2068d0bSJeff Roberson VM_LAUNDRY_BACKGROUND, 270e2068d0bSJeff Roberson VM_LAUNDRY_SHORTFALL 271e2068d0bSJeff Roberson } vmd_laundry_request; 272e2068d0bSJeff Roberson 27360684862SMark Johnston /* Paging thresholds and targets. */ 27460684862SMark Johnston u_int vmd_clean_pages_freed; /* (q) accumulator for laundry thread */ 27560684862SMark Johnston u_int vmd_background_launder_target; /* (c) */ 276e2068d0bSJeff Roberson u_int vmd_free_reserved; /* (c) pages reserved for deadlock */ 277e2068d0bSJeff Roberson u_int vmd_free_target; /* (c) pages desired free */ 278e2068d0bSJeff Roberson u_int vmd_free_min; /* (c) pages desired free */ 279e2068d0bSJeff Roberson u_int vmd_inactive_target; /* (c) pages desired inactive */ 280e2068d0bSJeff Roberson u_int vmd_pageout_free_min; /* (c) min pages reserved for kernel */ 281e2068d0bSJeff Roberson u_int vmd_pageout_wakeup_thresh;/* (c) min pages to wake pagedaemon */ 282e2068d0bSJeff Roberson u_int vmd_interrupt_free_min; /* (c) reserved pages for int code */ 283e2068d0bSJeff Roberson u_int vmd_free_severe; /* (c) severe page depletion point */ 2845f8cd1c0SJeff Roberson 2855f8cd1c0SJeff Roberson /* Name for sysctl etc. */ 2865f8cd1c0SJeff Roberson struct sysctl_oid *vmd_oid; 2875f8cd1c0SJeff Roberson char vmd_name[sizeof(__XSTRING(MAXMEMDOM))]; 288e2068d0bSJeff Roberson } __aligned(CACHE_LINE_SIZE); 289e2068d0bSJeff Roberson 290e2068d0bSJeff Roberson extern struct vm_domain vm_dom[MAXMEMDOM]; 291e2068d0bSJeff Roberson 292e2068d0bSJeff Roberson #define VM_DOMAIN(n) (&vm_dom[(n)]) 29330c5525bSAndrew Gallatin #define VM_DOMAIN_EMPTY(n) (vm_dom[(n)].vmd_page_count == 0) 294e2068d0bSJeff Roberson 295e2068d0bSJeff Roberson #define vm_pagequeue_assert_locked(pq) mtx_assert(&(pq)->pq_mutex, MA_OWNED) 296e2068d0bSJeff Roberson #define vm_pagequeue_lock(pq) mtx_lock(&(pq)->pq_mutex) 297e2068d0bSJeff Roberson #define vm_pagequeue_lockptr(pq) (&(pq)->pq_mutex) 2985cd29d0fSMark Johnston #define vm_pagequeue_trylock(pq) mtx_trylock(&(pq)->pq_mutex) 299e2068d0bSJeff Roberson #define vm_pagequeue_unlock(pq) mtx_unlock(&(pq)->pq_mutex) 300e2068d0bSJeff Roberson 301e2068d0bSJeff Roberson #define vm_domain_free_assert_locked(n) \ 302e2068d0bSJeff Roberson mtx_assert(vm_domain_free_lockptr((n)), MA_OWNED) 303e2068d0bSJeff Roberson #define vm_domain_free_assert_unlocked(n) \ 304e2068d0bSJeff Roberson mtx_assert(vm_domain_free_lockptr((n)), MA_NOTOWNED) 305e2068d0bSJeff Roberson #define vm_domain_free_lock(d) \ 306e2068d0bSJeff Roberson mtx_lock(vm_domain_free_lockptr((d))) 307e2068d0bSJeff Roberson #define vm_domain_free_lockptr(d) \ 308e2068d0bSJeff Roberson (&(d)->vmd_free_mtx) 3095cd29d0fSMark Johnston #define vm_domain_free_trylock(d) \ 3105cd29d0fSMark Johnston mtx_trylock(vm_domain_free_lockptr((d))) 311e2068d0bSJeff Roberson #define vm_domain_free_unlock(d) \ 312e2068d0bSJeff Roberson mtx_unlock(vm_domain_free_lockptr((d))) 313e2068d0bSJeff Roberson 31430fbfddaSJeff Roberson #define vm_domain_pageout_lockptr(d) \ 31530fbfddaSJeff Roberson (&(d)->vmd_pageout_mtx) 31630fbfddaSJeff Roberson #define vm_domain_pageout_assert_locked(n) \ 31730fbfddaSJeff Roberson mtx_assert(vm_domain_pageout_lockptr((n)), MA_OWNED) 31830fbfddaSJeff Roberson #define vm_domain_pageout_assert_unlocked(n) \ 31930fbfddaSJeff Roberson mtx_assert(vm_domain_pageout_lockptr((n)), MA_NOTOWNED) 32030fbfddaSJeff Roberson #define vm_domain_pageout_lock(d) \ 32130fbfddaSJeff Roberson mtx_lock(vm_domain_pageout_lockptr((d))) 32230fbfddaSJeff Roberson #define vm_domain_pageout_unlock(d) \ 32330fbfddaSJeff Roberson mtx_unlock(vm_domain_pageout_lockptr((d))) 32430fbfddaSJeff Roberson 325e2068d0bSJeff Roberson static __inline void 326e2068d0bSJeff Roberson vm_pagequeue_cnt_add(struct vm_pagequeue *pq, int addend) 327e2068d0bSJeff Roberson { 328e2068d0bSJeff Roberson 329e2068d0bSJeff Roberson vm_pagequeue_assert_locked(pq); 330e2068d0bSJeff Roberson pq->pq_cnt += addend; 331e2068d0bSJeff Roberson } 332e2068d0bSJeff Roberson #define vm_pagequeue_cnt_inc(pq) vm_pagequeue_cnt_add((pq), 1) 333e2068d0bSJeff Roberson #define vm_pagequeue_cnt_dec(pq) vm_pagequeue_cnt_add((pq), -1) 334e2068d0bSJeff Roberson 3355cd29d0fSMark Johnston static inline void 3368b90607fSMark Johnston vm_pagequeue_remove(struct vm_pagequeue *pq, vm_page_t m) 3378b90607fSMark Johnston { 3388b90607fSMark Johnston 3398b90607fSMark Johnston TAILQ_REMOVE(&pq->pq_pl, m, plinks.q); 3408b90607fSMark Johnston vm_pagequeue_cnt_dec(pq); 3418b90607fSMark Johnston } 3428b90607fSMark Johnston 3438b90607fSMark Johnston static inline void 3445cd29d0fSMark Johnston vm_batchqueue_init(struct vm_batchqueue *bq) 3455cd29d0fSMark Johnston { 3465cd29d0fSMark Johnston 3475cd29d0fSMark Johnston bq->bq_cnt = 0; 3485cd29d0fSMark Johnston } 3495cd29d0fSMark Johnston 3505cd29d0fSMark Johnston static inline bool 3515cd29d0fSMark Johnston vm_batchqueue_insert(struct vm_batchqueue *bq, vm_page_t m) 3525cd29d0fSMark Johnston { 3535cd29d0fSMark Johnston 3545cd29d0fSMark Johnston if (bq->bq_cnt < nitems(bq->bq_pa)) { 3555cd29d0fSMark Johnston bq->bq_pa[bq->bq_cnt++] = m; 3565cd29d0fSMark Johnston return (true); 3575cd29d0fSMark Johnston } 3585cd29d0fSMark Johnston return (false); 3595cd29d0fSMark Johnston } 3605cd29d0fSMark Johnston 3615cd29d0fSMark Johnston static inline vm_page_t 3625cd29d0fSMark Johnston vm_batchqueue_pop(struct vm_batchqueue *bq) 3635cd29d0fSMark Johnston { 3645cd29d0fSMark Johnston 3655cd29d0fSMark Johnston if (bq->bq_cnt == 0) 3665cd29d0fSMark Johnston return (NULL); 3675cd29d0fSMark Johnston return (bq->bq_pa[--bq->bq_cnt]); 3685cd29d0fSMark Johnston } 3695cd29d0fSMark Johnston 370e2068d0bSJeff Roberson void vm_domain_set(struct vm_domain *vmd); 37130fbfddaSJeff Roberson void vm_domain_clear(struct vm_domain *vmd); 3725c930c89SJeff Roberson int vm_domain_allocate(struct vm_domain *vmd, int req, int npages); 373e2068d0bSJeff Roberson 374e2068d0bSJeff Roberson /* 375e2068d0bSJeff Roberson * vm_pagequeue_domain: 376e2068d0bSJeff Roberson * 377e2068d0bSJeff Roberson * Return the memory domain the page belongs to. 378e2068d0bSJeff Roberson */ 379e2068d0bSJeff Roberson static inline struct vm_domain * 380e2068d0bSJeff Roberson vm_pagequeue_domain(vm_page_t m) 381e2068d0bSJeff Roberson { 382e2068d0bSJeff Roberson 383e2068d0bSJeff Roberson return (VM_DOMAIN(vm_phys_domain(m))); 384e2068d0bSJeff Roberson } 385e2068d0bSJeff Roberson 386e2068d0bSJeff Roberson /* 387e2068d0bSJeff Roberson * Return the number of pages we need to free-up or cache 388e2068d0bSJeff Roberson * A positive number indicates that we do not have enough free pages. 389e2068d0bSJeff Roberson */ 390e2068d0bSJeff Roberson static inline int 391e2068d0bSJeff Roberson vm_paging_target(struct vm_domain *vmd) 392e2068d0bSJeff Roberson { 393e2068d0bSJeff Roberson 394e2068d0bSJeff Roberson return (vmd->vmd_free_target - vmd->vmd_free_count); 395e2068d0bSJeff Roberson } 396e2068d0bSJeff Roberson 397e2068d0bSJeff Roberson /* 398e2068d0bSJeff Roberson * Returns TRUE if the pagedaemon needs to be woken up. 399e2068d0bSJeff Roberson */ 400e2068d0bSJeff Roberson static inline int 401e2068d0bSJeff Roberson vm_paging_needed(struct vm_domain *vmd, u_int free_count) 402e2068d0bSJeff Roberson { 403e2068d0bSJeff Roberson 404e2068d0bSJeff Roberson return (free_count < vmd->vmd_pageout_wakeup_thresh); 405e2068d0bSJeff Roberson } 406e2068d0bSJeff Roberson 407e2068d0bSJeff Roberson /* 408e2068d0bSJeff Roberson * Returns TRUE if the domain is below the min paging target. 409e2068d0bSJeff Roberson */ 410e2068d0bSJeff Roberson static inline int 411e2068d0bSJeff Roberson vm_paging_min(struct vm_domain *vmd) 412e2068d0bSJeff Roberson { 413e2068d0bSJeff Roberson 414e2068d0bSJeff Roberson return (vmd->vmd_free_min > vmd->vmd_free_count); 415e2068d0bSJeff Roberson } 416e2068d0bSJeff Roberson 417e2068d0bSJeff Roberson /* 418e2068d0bSJeff Roberson * Returns TRUE if the domain is below the severe paging target. 419e2068d0bSJeff Roberson */ 420e2068d0bSJeff Roberson static inline int 421e2068d0bSJeff Roberson vm_paging_severe(struct vm_domain *vmd) 422e2068d0bSJeff Roberson { 423e2068d0bSJeff Roberson 424e2068d0bSJeff Roberson return (vmd->vmd_free_severe > vmd->vmd_free_count); 425e2068d0bSJeff Roberson } 426e2068d0bSJeff Roberson 427e2068d0bSJeff Roberson /* 428e2068d0bSJeff Roberson * Return the number of pages we need to launder. 429e2068d0bSJeff Roberson * A positive number indicates that we have a shortfall of clean pages. 430e2068d0bSJeff Roberson */ 431e2068d0bSJeff Roberson static inline int 432e2068d0bSJeff Roberson vm_laundry_target(struct vm_domain *vmd) 433e2068d0bSJeff Roberson { 434e2068d0bSJeff Roberson 435e2068d0bSJeff Roberson return (vm_paging_target(vmd)); 436e2068d0bSJeff Roberson } 437e2068d0bSJeff Roberson 43830fbfddaSJeff Roberson void pagedaemon_wakeup(int domain); 43930fbfddaSJeff Roberson 44030fbfddaSJeff Roberson static inline void 44130fbfddaSJeff Roberson vm_domain_freecnt_inc(struct vm_domain *vmd, int adj) 442e2068d0bSJeff Roberson { 44330fbfddaSJeff Roberson u_int old, new; 444e2068d0bSJeff Roberson 44530fbfddaSJeff Roberson old = atomic_fetchadd_int(&vmd->vmd_free_count, adj); 44630fbfddaSJeff Roberson new = old + adj; 44730fbfddaSJeff Roberson /* 44830fbfddaSJeff Roberson * Only update bitsets on transitions. Notice we short-circuit the 44930fbfddaSJeff Roberson * rest of the checks if we're above min already. 45030fbfddaSJeff Roberson */ 45130fbfddaSJeff Roberson if (old < vmd->vmd_free_min && (new >= vmd->vmd_free_min || 45230fbfddaSJeff Roberson (old < vmd->vmd_free_severe && new >= vmd->vmd_free_severe) || 45330fbfddaSJeff Roberson (old < vmd->vmd_pageout_free_min && 45430fbfddaSJeff Roberson new >= vmd->vmd_pageout_free_min))) 45530fbfddaSJeff Roberson vm_domain_clear(vmd); 45630fbfddaSJeff Roberson } 45730fbfddaSJeff Roberson 458e2068d0bSJeff Roberson #endif /* _KERNEL */ 459e2068d0bSJeff Roberson #endif /* !_VM_PAGEQUEUE_ */ 460