xref: /f-stack/freebsd/vm/vm_pagequeue.h (revision 22ce4aff)
1*22ce4affSfengbojiang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
3*22ce4affSfengbojiang  *
4*22ce4affSfengbojiang  * Copyright (c) 1991, 1993
5*22ce4affSfengbojiang  *	The Regents of the University of California.  All rights reserved.
6*22ce4affSfengbojiang  *
7*22ce4affSfengbojiang  * This code is derived from software contributed to Berkeley by
8*22ce4affSfengbojiang  * The Mach Operating System project at Carnegie-Mellon University.
9*22ce4affSfengbojiang  *
10*22ce4affSfengbojiang  * Redistribution and use in source and binary forms, with or without
11*22ce4affSfengbojiang  * modification, are permitted provided that the following conditions
12*22ce4affSfengbojiang  * are met:
13*22ce4affSfengbojiang  * 1. Redistributions of source code must retain the above copyright
14*22ce4affSfengbojiang  *    notice, this list of conditions and the following disclaimer.
15*22ce4affSfengbojiang  * 2. Redistributions in binary form must reproduce the above copyright
16*22ce4affSfengbojiang  *    notice, this list of conditions and the following disclaimer in the
17*22ce4affSfengbojiang  *    documentation and/or other materials provided with the distribution.
18*22ce4affSfengbojiang  * 3. Neither the name of the University nor the names of its contributors
19*22ce4affSfengbojiang  *    may be used to endorse or promote products derived from this software
20*22ce4affSfengbojiang  *    without specific prior written permission.
21*22ce4affSfengbojiang  *
22*22ce4affSfengbojiang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*22ce4affSfengbojiang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*22ce4affSfengbojiang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*22ce4affSfengbojiang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*22ce4affSfengbojiang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*22ce4affSfengbojiang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*22ce4affSfengbojiang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*22ce4affSfengbojiang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*22ce4affSfengbojiang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*22ce4affSfengbojiang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*22ce4affSfengbojiang  * SUCH DAMAGE.
33*22ce4affSfengbojiang  *
34*22ce4affSfengbojiang  *	from: @(#)vm_page.h	8.2 (Berkeley) 12/13/93
35*22ce4affSfengbojiang  *
36*22ce4affSfengbojiang  *
37*22ce4affSfengbojiang  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38*22ce4affSfengbojiang  * All rights reserved.
39*22ce4affSfengbojiang  *
40*22ce4affSfengbojiang  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41*22ce4affSfengbojiang  *
42*22ce4affSfengbojiang  * Permission to use, copy, modify and distribute this software and
43*22ce4affSfengbojiang  * its documentation is hereby granted, provided that both the copyright
44*22ce4affSfengbojiang  * notice and this permission notice appear in all copies of the
45*22ce4affSfengbojiang  * software, derivative works or modified versions, and any portions
46*22ce4affSfengbojiang  * thereof, and that both notices appear in supporting documentation.
47*22ce4affSfengbojiang  *
48*22ce4affSfengbojiang  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49*22ce4affSfengbojiang  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50*22ce4affSfengbojiang  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51*22ce4affSfengbojiang  *
52*22ce4affSfengbojiang  * Carnegie Mellon requests users of this software to return to
53*22ce4affSfengbojiang  *
54*22ce4affSfengbojiang  *  Software Distribution Coordinator  or  [email protected]
55*22ce4affSfengbojiang  *  School of Computer Science
56*22ce4affSfengbojiang  *  Carnegie Mellon University
57*22ce4affSfengbojiang  *  Pittsburgh PA 15213-3890
58*22ce4affSfengbojiang  *
59*22ce4affSfengbojiang  * any improvements or extensions that they make and grant Carnegie the
60*22ce4affSfengbojiang  * rights to redistribute these changes.
61*22ce4affSfengbojiang  *
62*22ce4affSfengbojiang  * $FreeBSD$
63*22ce4affSfengbojiang  */
64*22ce4affSfengbojiang 
65*22ce4affSfengbojiang #ifndef	_VM_PAGEQUEUE_
66*22ce4affSfengbojiang #define	_VM_PAGEQUEUE_
67*22ce4affSfengbojiang 
68*22ce4affSfengbojiang #ifdef _KERNEL
69*22ce4affSfengbojiang struct vm_pagequeue {
70*22ce4affSfengbojiang 	struct mtx	pq_mutex;
71*22ce4affSfengbojiang 	struct pglist	pq_pl;
72*22ce4affSfengbojiang 	int		pq_cnt;
73*22ce4affSfengbojiang 	const char	* const pq_name;
74*22ce4affSfengbojiang 	uint64_t	pq_pdpages;
75*22ce4affSfengbojiang } __aligned(CACHE_LINE_SIZE);
76*22ce4affSfengbojiang 
77*22ce4affSfengbojiang #ifndef VM_BATCHQUEUE_SIZE
78*22ce4affSfengbojiang #define	VM_BATCHQUEUE_SIZE	7
79*22ce4affSfengbojiang #endif
80*22ce4affSfengbojiang 
81*22ce4affSfengbojiang struct vm_batchqueue {
82*22ce4affSfengbojiang 	vm_page_t	bq_pa[VM_BATCHQUEUE_SIZE];
83*22ce4affSfengbojiang 	int		bq_cnt;
84*22ce4affSfengbojiang } __aligned(CACHE_LINE_SIZE);
85*22ce4affSfengbojiang 
86*22ce4affSfengbojiang #include <vm/uma.h>
87*22ce4affSfengbojiang #include <sys/_blockcount.h>
88*22ce4affSfengbojiang #include <sys/pidctrl.h>
89*22ce4affSfengbojiang struct sysctl_oid;
90*22ce4affSfengbojiang 
91*22ce4affSfengbojiang /*
92*22ce4affSfengbojiang  * One vm_domain per NUMA domain.  Contains pagequeues, free page structures,
93*22ce4affSfengbojiang  * and accounting.
94*22ce4affSfengbojiang  *
95*22ce4affSfengbojiang  * Lock Key:
96*22ce4affSfengbojiang  * f	vmd_free_mtx
97*22ce4affSfengbojiang  * p	vmd_pageout_mtx
98*22ce4affSfengbojiang  * d	vm_domainset_lock
99*22ce4affSfengbojiang  * a	atomic
100*22ce4affSfengbojiang  * c	const after boot
101*22ce4affSfengbojiang  * q	page queue lock
102*22ce4affSfengbojiang  *
103*22ce4affSfengbojiang  * A unique page daemon thread manages each vm_domain structure and is
104*22ce4affSfengbojiang  * responsible for ensuring that some free memory is available by freeing
105*22ce4affSfengbojiang  * inactive pages and aging active pages.  To decide how many pages to process,
106*22ce4affSfengbojiang  * it uses thresholds derived from the number of pages in the domain:
107*22ce4affSfengbojiang  *
108*22ce4affSfengbojiang  *  vmd_page_count
109*22ce4affSfengbojiang  *       ---
110*22ce4affSfengbojiang  *        |
111*22ce4affSfengbojiang  *        |-> vmd_inactive_target (~3%)
112*22ce4affSfengbojiang  *        |   - The active queue scan target is given by
113*22ce4affSfengbojiang  *        |     (vmd_inactive_target + vmd_free_target - vmd_free_count).
114*22ce4affSfengbojiang  *        |
115*22ce4affSfengbojiang  *        |
116*22ce4affSfengbojiang  *        |-> vmd_free_target (~2%)
117*22ce4affSfengbojiang  *        |   - Target for page reclamation.
118*22ce4affSfengbojiang  *        |
119*22ce4affSfengbojiang  *        |-> vmd_pageout_wakeup_thresh (~1.8%)
120*22ce4affSfengbojiang  *        |   - Threshold for waking up the page daemon.
121*22ce4affSfengbojiang  *        |
122*22ce4affSfengbojiang  *        |
123*22ce4affSfengbojiang  *        |-> vmd_free_min (~0.5%)
124*22ce4affSfengbojiang  *        |   - First low memory threshold.
125*22ce4affSfengbojiang  *        |   - Causes per-CPU caching to be lazily disabled in UMA.
126*22ce4affSfengbojiang  *        |   - vm_wait() sleeps below this threshold.
127*22ce4affSfengbojiang  *        |
128*22ce4affSfengbojiang  *        |-> vmd_free_severe (~0.25%)
129*22ce4affSfengbojiang  *        |   - Second low memory threshold.
130*22ce4affSfengbojiang  *        |   - Triggers aggressive UMA reclamation, disables delayed buffer
131*22ce4affSfengbojiang  *        |     writes.
132*22ce4affSfengbojiang  *        |
133*22ce4affSfengbojiang  *        |-> vmd_free_reserved (~0.13%)
134*22ce4affSfengbojiang  *        |   - Minimum for VM_ALLOC_NORMAL page allocations.
135*22ce4affSfengbojiang  *        |-> vmd_pageout_free_min (32 + 2 pages)
136*22ce4affSfengbojiang  *        |   - Minimum for waking a page daemon thread sleeping in vm_wait().
137*22ce4affSfengbojiang  *        |-> vmd_interrupt_free_min (2 pages)
138*22ce4affSfengbojiang  *        |   - Minimum for VM_ALLOC_SYSTEM page allocations.
139*22ce4affSfengbojiang  *       ---
140*22ce4affSfengbojiang  *
141*22ce4affSfengbojiang  *--
142*22ce4affSfengbojiang  * Free page count regulation:
143*22ce4affSfengbojiang  *
144*22ce4affSfengbojiang  * The page daemon attempts to ensure that the free page count is above the free
145*22ce4affSfengbojiang  * target.  It wakes up periodically (every 100ms) to input the current free
146*22ce4affSfengbojiang  * page shortage (free_target - free_count) to a PID controller, which in
147*22ce4affSfengbojiang  * response outputs the number of pages to attempt to reclaim.  The shortage's
148*22ce4affSfengbojiang  * current magnitude, rate of change, and cumulative value are together used to
149*22ce4affSfengbojiang  * determine the controller's output.  The page daemon target thus adapts
150*22ce4affSfengbojiang  * dynamically to the system's demand for free pages, resulting in less
151*22ce4affSfengbojiang  * burstiness than a simple hysteresis loop.
152*22ce4affSfengbojiang  *
153*22ce4affSfengbojiang  * When the free page count drops below the wakeup threshold,
154*22ce4affSfengbojiang  * vm_domain_allocate() proactively wakes up the page daemon.  This helps ensure
155*22ce4affSfengbojiang  * that the system responds promptly to a large instantaneous free page
156*22ce4affSfengbojiang  * shortage.
157*22ce4affSfengbojiang  *
158*22ce4affSfengbojiang  * The page daemon also attempts to ensure that some fraction of the system's
159*22ce4affSfengbojiang  * memory is present in the inactive (I) and laundry (L) page queues, so that it
160*22ce4affSfengbojiang  * can respond promptly to a sudden free page shortage.  In particular, the page
161*22ce4affSfengbojiang  * daemon thread aggressively scans active pages so long as the following
162*22ce4affSfengbojiang  * condition holds:
163*22ce4affSfengbojiang  *
164*22ce4affSfengbojiang  *         len(I) + len(L) + free_target - free_count < inactive_target
165*22ce4affSfengbojiang  *
166*22ce4affSfengbojiang  * Otherwise, when the inactive target is met, the page daemon periodically
167*22ce4affSfengbojiang  * scans a small portion of the active queue in order to maintain up-to-date
168*22ce4affSfengbojiang  * per-page access history.  Unreferenced pages in the active queue thus
169*22ce4affSfengbojiang  * eventually migrate to the inactive queue.
170*22ce4affSfengbojiang  *
171*22ce4affSfengbojiang  * The per-domain laundry thread periodically launders dirty pages based on the
172*22ce4affSfengbojiang  * number of clean pages freed by the page daemon since the last laundering.  If
173*22ce4affSfengbojiang  * the page daemon fails to meet its scan target (i.e., the PID controller
174*22ce4affSfengbojiang  * output) because of a shortage of clean inactive pages, the laundry thread
175*22ce4affSfengbojiang  * attempts to launder enough pages to meet the free page target.
176*22ce4affSfengbojiang  *
177*22ce4affSfengbojiang  *--
178*22ce4affSfengbojiang  * Page allocation priorities:
179*22ce4affSfengbojiang  *
180*22ce4affSfengbojiang  * The system defines three page allocation priorities: VM_ALLOC_NORMAL,
181*22ce4affSfengbojiang  * VM_ALLOC_SYSTEM and VM_ALLOC_INTERRUPT.  An interrupt-priority allocation can
182*22ce4affSfengbojiang  * claim any free page.  This priority is used in the pmap layer when attempting
183*22ce4affSfengbojiang  * to allocate a page for the kernel page tables; in such cases an allocation
184*22ce4affSfengbojiang  * failure will usually result in a kernel panic.  The system priority is used
185*22ce4affSfengbojiang  * for most other kernel memory allocations, for instance by UMA's slab
186*22ce4affSfengbojiang  * allocator or the buffer cache.  Such allocations will fail if the free count
187*22ce4affSfengbojiang  * is below interrupt_free_min.  All other allocations occur at the normal
188*22ce4affSfengbojiang  * priority, which is typically used for allocation of user pages, for instance
189*22ce4affSfengbojiang  * in the page fault handler or when allocating page table pages or pv_entry
190*22ce4affSfengbojiang  * structures for user pmaps.  Such allocations fail if the free count is below
191*22ce4affSfengbojiang  * the free_reserved threshold.
192*22ce4affSfengbojiang  *
193*22ce4affSfengbojiang  *--
194*22ce4affSfengbojiang  * Free memory shortages:
195*22ce4affSfengbojiang  *
196*22ce4affSfengbojiang  * The system uses the free_min and free_severe thresholds to apply
197*22ce4affSfengbojiang  * back-pressure and give the page daemon a chance to recover.  When a page
198*22ce4affSfengbojiang  * allocation fails due to a shortage and the allocating thread cannot handle
199*22ce4affSfengbojiang  * failure, it may call vm_wait() to sleep until free pages are available.
200*22ce4affSfengbojiang  * vm_domain_freecnt_inc() wakes sleeping threads once the free page count rises
201*22ce4affSfengbojiang  * above the free_min threshold; the page daemon and laundry threads are given
202*22ce4affSfengbojiang  * priority and will wake up once free_count reaches the (much smaller)
203*22ce4affSfengbojiang  * pageout_free_min threshold.
204*22ce4affSfengbojiang  *
205*22ce4affSfengbojiang  * On NUMA systems, the domainset iterators always prefer NUMA domains where the
206*22ce4affSfengbojiang  * free page count is above the free_min threshold.  This means that given the
207*22ce4affSfengbojiang  * choice between two NUMA domains, one above the free_min threshold and one
208*22ce4affSfengbojiang  * below, the former will be used to satisfy the allocation request regardless
209*22ce4affSfengbojiang  * of the domain selection policy.
210*22ce4affSfengbojiang  *
211*22ce4affSfengbojiang  * In addition to reclaiming memory from the page queues, the vm_lowmem event
212*22ce4affSfengbojiang  * fires every ten seconds so long as the system is under memory pressure (i.e.,
213*22ce4affSfengbojiang  * vmd_free_count < vmd_free_target).  This allows kernel subsystems to register
214*22ce4affSfengbojiang  * for notifications of free page shortages, upon which they may shrink their
215*22ce4affSfengbojiang  * caches.  Following a vm_lowmem event, UMA's caches are pruned to ensure that
216*22ce4affSfengbojiang  * they do not contain an excess of unused memory.  When a domain is below the
217*22ce4affSfengbojiang  * free_min threshold, UMA limits the population of per-CPU caches.  When a
218*22ce4affSfengbojiang  * domain falls below the free_severe threshold, UMA's caches are completely
219*22ce4affSfengbojiang  * drained.
220*22ce4affSfengbojiang  *
221*22ce4affSfengbojiang  * If the system encounters a global memory shortage, it may resort to the
222*22ce4affSfengbojiang  * out-of-memory (OOM) killer, which selects a process and delivers SIGKILL in a
223*22ce4affSfengbojiang  * last-ditch attempt to free up some pages.  Either of the two following
224*22ce4affSfengbojiang  * conditions will activate the OOM killer:
225*22ce4affSfengbojiang  *
226*22ce4affSfengbojiang  *  1. The page daemons collectively fail to reclaim any pages during their
227*22ce4affSfengbojiang  *     inactive queue scans.  After vm_pageout_oom_seq consecutive scans fail,
228*22ce4affSfengbojiang  *     the page daemon thread votes for an OOM kill, and an OOM kill is
229*22ce4affSfengbojiang  *     triggered when all page daemons have voted.  This heuristic is strict and
230*22ce4affSfengbojiang  *     may fail to trigger even when the system is effectively deadlocked.
231*22ce4affSfengbojiang  *
232*22ce4affSfengbojiang  *  2. Threads in the user fault handler are repeatedly unable to make progress
233*22ce4affSfengbojiang  *     while allocating a page to satisfy the fault.  After
234*22ce4affSfengbojiang  *     vm_pfault_oom_attempts page allocation failures with intervening
235*22ce4affSfengbojiang  *     vm_wait() calls, the faulting thread will trigger an OOM kill.
236*22ce4affSfengbojiang  */
237*22ce4affSfengbojiang struct vm_domain {
238*22ce4affSfengbojiang 	struct vm_pagequeue vmd_pagequeues[PQ_COUNT];
239*22ce4affSfengbojiang 	struct mtx_padalign vmd_free_mtx;
240*22ce4affSfengbojiang 	struct mtx_padalign vmd_pageout_mtx;
241*22ce4affSfengbojiang 	struct vm_pgcache {
242*22ce4affSfengbojiang 		int domain;
243*22ce4affSfengbojiang 		int pool;
244*22ce4affSfengbojiang 		uma_zone_t zone;
245*22ce4affSfengbojiang 	} vmd_pgcache[VM_NFREEPOOL];
246*22ce4affSfengbojiang 	struct vmem *vmd_kernel_arena;	/* (c) per-domain kva R/W arena. */
247*22ce4affSfengbojiang 	struct vmem *vmd_kernel_rwx_arena; /* (c) per-domain kva R/W/X arena. */
248*22ce4affSfengbojiang 	u_int vmd_domain;		/* (c) Domain number. */
249*22ce4affSfengbojiang 	u_int vmd_page_count;		/* (c) Total page count. */
250*22ce4affSfengbojiang 	long vmd_segs;			/* (c) bitmask of the segments */
251*22ce4affSfengbojiang 	u_int __aligned(CACHE_LINE_SIZE) vmd_free_count; /* (a,f) free page count */
252*22ce4affSfengbojiang 	u_int vmd_pageout_deficit;	/* (a) Estimated number of pages deficit */
253*22ce4affSfengbojiang 	uint8_t vmd_pad[CACHE_LINE_SIZE - (sizeof(u_int) * 2)];
254*22ce4affSfengbojiang 
255*22ce4affSfengbojiang 	/* Paging control variables, used within single threaded page daemon. */
256*22ce4affSfengbojiang 	struct pidctrl vmd_pid;		/* Pageout controller. */
257*22ce4affSfengbojiang 	boolean_t vmd_oom;
258*22ce4affSfengbojiang 	u_int vmd_inactive_threads;
259*22ce4affSfengbojiang 	u_int vmd_inactive_shortage;		/* Per-thread shortage. */
260*22ce4affSfengbojiang 	blockcount_t vmd_inactive_running;	/* Number of inactive threads. */
261*22ce4affSfengbojiang 	blockcount_t vmd_inactive_starting;	/* Number of threads started. */
262*22ce4affSfengbojiang 	volatile u_int vmd_addl_shortage;	/* Shortage accumulator. */
263*22ce4affSfengbojiang 	volatile u_int vmd_inactive_freed;	/* Successful inactive frees. */
264*22ce4affSfengbojiang 	volatile u_int vmd_inactive_us;		/* Microseconds for above. */
265*22ce4affSfengbojiang 	u_int vmd_inactive_pps;		/* Exponential decay frees/second. */
266*22ce4affSfengbojiang 	int vmd_oom_seq;
267*22ce4affSfengbojiang 	int vmd_last_active_scan;
268*22ce4affSfengbojiang 	struct vm_page vmd_markers[PQ_COUNT]; /* (q) markers for queue scans */
269*22ce4affSfengbojiang 	struct vm_page vmd_inacthead; /* marker for LRU-defeating insertions */
270*22ce4affSfengbojiang 	struct vm_page vmd_clock[2]; /* markers for active queue scan */
271*22ce4affSfengbojiang 
272*22ce4affSfengbojiang 	int vmd_pageout_wanted;		/* (a, p) pageout daemon wait channel */
273*22ce4affSfengbojiang 	int vmd_pageout_pages_needed;	/* (d) page daemon waiting for pages? */
274*22ce4affSfengbojiang 	bool vmd_minset;		/* (d) Are we in vm_min_domains? */
275*22ce4affSfengbojiang 	bool vmd_severeset;		/* (d) Are we in vm_severe_domains? */
276*22ce4affSfengbojiang 	enum {
277*22ce4affSfengbojiang 		VM_LAUNDRY_IDLE = 0,
278*22ce4affSfengbojiang 		VM_LAUNDRY_BACKGROUND,
279*22ce4affSfengbojiang 		VM_LAUNDRY_SHORTFALL
280*22ce4affSfengbojiang 	} vmd_laundry_request;
281*22ce4affSfengbojiang 
282*22ce4affSfengbojiang 	/* Paging thresholds and targets. */
283*22ce4affSfengbojiang 	u_int vmd_clean_pages_freed;	/* (q) accumulator for laundry thread */
284*22ce4affSfengbojiang 	u_int vmd_background_launder_target; /* (c) */
285*22ce4affSfengbojiang 	u_int vmd_free_reserved;	/* (c) pages reserved for deadlock */
286*22ce4affSfengbojiang 	u_int vmd_free_target;		/* (c) pages desired free */
287*22ce4affSfengbojiang 	u_int vmd_free_min;		/* (c) pages desired free */
288*22ce4affSfengbojiang 	u_int vmd_inactive_target;	/* (c) pages desired inactive */
289*22ce4affSfengbojiang 	u_int vmd_pageout_free_min;	/* (c) min pages reserved for kernel */
290*22ce4affSfengbojiang 	u_int vmd_pageout_wakeup_thresh;/* (c) min pages to wake pagedaemon */
291*22ce4affSfengbojiang 	u_int vmd_interrupt_free_min;	/* (c) reserved pages for int code */
292*22ce4affSfengbojiang 	u_int vmd_free_severe;		/* (c) severe page depletion point */
293*22ce4affSfengbojiang 
294*22ce4affSfengbojiang 	/* Name for sysctl etc. */
295*22ce4affSfengbojiang 	struct sysctl_oid *vmd_oid;
296*22ce4affSfengbojiang 	char vmd_name[sizeof(__XSTRING(MAXMEMDOM))];
297*22ce4affSfengbojiang } __aligned(CACHE_LINE_SIZE);
298*22ce4affSfengbojiang 
299*22ce4affSfengbojiang extern struct vm_domain vm_dom[MAXMEMDOM];
300*22ce4affSfengbojiang 
301*22ce4affSfengbojiang #define	VM_DOMAIN(n)		(&vm_dom[(n)])
302*22ce4affSfengbojiang #define	VM_DOMAIN_EMPTY(n)	(vm_dom[(n)].vmd_page_count == 0)
303*22ce4affSfengbojiang 
304*22ce4affSfengbojiang #define	vm_pagequeue_assert_locked(pq)	mtx_assert(&(pq)->pq_mutex, MA_OWNED)
305*22ce4affSfengbojiang #define	vm_pagequeue_lock(pq)		mtx_lock(&(pq)->pq_mutex)
306*22ce4affSfengbojiang #define	vm_pagequeue_lockptr(pq)	(&(pq)->pq_mutex)
307*22ce4affSfengbojiang #define	vm_pagequeue_trylock(pq)	mtx_trylock(&(pq)->pq_mutex)
308*22ce4affSfengbojiang #define	vm_pagequeue_unlock(pq)		mtx_unlock(&(pq)->pq_mutex)
309*22ce4affSfengbojiang 
310*22ce4affSfengbojiang #define	vm_domain_free_assert_locked(n)					\
311*22ce4affSfengbojiang 	    mtx_assert(vm_domain_free_lockptr((n)), MA_OWNED)
312*22ce4affSfengbojiang #define	vm_domain_free_assert_unlocked(n)				\
313*22ce4affSfengbojiang 	    mtx_assert(vm_domain_free_lockptr((n)), MA_NOTOWNED)
314*22ce4affSfengbojiang #define	vm_domain_free_lock(d)						\
315*22ce4affSfengbojiang 	    mtx_lock(vm_domain_free_lockptr((d)))
316*22ce4affSfengbojiang #define	vm_domain_free_lockptr(d)					\
317*22ce4affSfengbojiang 	    (&(d)->vmd_free_mtx)
318*22ce4affSfengbojiang #define	vm_domain_free_trylock(d)					\
319*22ce4affSfengbojiang 	    mtx_trylock(vm_domain_free_lockptr((d)))
320*22ce4affSfengbojiang #define	vm_domain_free_unlock(d)					\
321*22ce4affSfengbojiang 	    mtx_unlock(vm_domain_free_lockptr((d)))
322*22ce4affSfengbojiang 
323*22ce4affSfengbojiang #define	vm_domain_pageout_lockptr(d)					\
324*22ce4affSfengbojiang 	    (&(d)->vmd_pageout_mtx)
325*22ce4affSfengbojiang #define	vm_domain_pageout_assert_locked(n)				\
326*22ce4affSfengbojiang 	    mtx_assert(vm_domain_pageout_lockptr((n)), MA_OWNED)
327*22ce4affSfengbojiang #define	vm_domain_pageout_assert_unlocked(n)				\
328*22ce4affSfengbojiang 	    mtx_assert(vm_domain_pageout_lockptr((n)), MA_NOTOWNED)
329*22ce4affSfengbojiang #define	vm_domain_pageout_lock(d)					\
330*22ce4affSfengbojiang 	    mtx_lock(vm_domain_pageout_lockptr((d)))
331*22ce4affSfengbojiang #define	vm_domain_pageout_unlock(d)					\
332*22ce4affSfengbojiang 	    mtx_unlock(vm_domain_pageout_lockptr((d)))
333*22ce4affSfengbojiang 
334*22ce4affSfengbojiang static __inline void
vm_pagequeue_cnt_add(struct vm_pagequeue * pq,int addend)335*22ce4affSfengbojiang vm_pagequeue_cnt_add(struct vm_pagequeue *pq, int addend)
336*22ce4affSfengbojiang {
337*22ce4affSfengbojiang 
338*22ce4affSfengbojiang 	vm_pagequeue_assert_locked(pq);
339*22ce4affSfengbojiang 	pq->pq_cnt += addend;
340*22ce4affSfengbojiang }
341*22ce4affSfengbojiang #define	vm_pagequeue_cnt_inc(pq)	vm_pagequeue_cnt_add((pq), 1)
342*22ce4affSfengbojiang #define	vm_pagequeue_cnt_dec(pq)	vm_pagequeue_cnt_add((pq), -1)
343*22ce4affSfengbojiang 
344*22ce4affSfengbojiang static inline void
vm_pagequeue_remove(struct vm_pagequeue * pq,vm_page_t m)345*22ce4affSfengbojiang vm_pagequeue_remove(struct vm_pagequeue *pq, vm_page_t m)
346*22ce4affSfengbojiang {
347*22ce4affSfengbojiang 
348*22ce4affSfengbojiang 	TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
349*22ce4affSfengbojiang 	vm_pagequeue_cnt_dec(pq);
350*22ce4affSfengbojiang }
351*22ce4affSfengbojiang 
352*22ce4affSfengbojiang static inline void
vm_batchqueue_init(struct vm_batchqueue * bq)353*22ce4affSfengbojiang vm_batchqueue_init(struct vm_batchqueue *bq)
354*22ce4affSfengbojiang {
355*22ce4affSfengbojiang 
356*22ce4affSfengbojiang 	bq->bq_cnt = 0;
357*22ce4affSfengbojiang }
358*22ce4affSfengbojiang 
359*22ce4affSfengbojiang static inline bool
vm_batchqueue_insert(struct vm_batchqueue * bq,vm_page_t m)360*22ce4affSfengbojiang vm_batchqueue_insert(struct vm_batchqueue *bq, vm_page_t m)
361*22ce4affSfengbojiang {
362*22ce4affSfengbojiang 
363*22ce4affSfengbojiang 	if (bq->bq_cnt < nitems(bq->bq_pa)) {
364*22ce4affSfengbojiang 		bq->bq_pa[bq->bq_cnt++] = m;
365*22ce4affSfengbojiang 		return (true);
366*22ce4affSfengbojiang 	}
367*22ce4affSfengbojiang 	return (false);
368*22ce4affSfengbojiang }
369*22ce4affSfengbojiang 
370*22ce4affSfengbojiang static inline vm_page_t
vm_batchqueue_pop(struct vm_batchqueue * bq)371*22ce4affSfengbojiang vm_batchqueue_pop(struct vm_batchqueue *bq)
372*22ce4affSfengbojiang {
373*22ce4affSfengbojiang 
374*22ce4affSfengbojiang 	if (bq->bq_cnt == 0)
375*22ce4affSfengbojiang 		return (NULL);
376*22ce4affSfengbojiang 	return (bq->bq_pa[--bq->bq_cnt]);
377*22ce4affSfengbojiang }
378*22ce4affSfengbojiang 
379*22ce4affSfengbojiang void vm_domain_set(struct vm_domain *vmd);
380*22ce4affSfengbojiang void vm_domain_clear(struct vm_domain *vmd);
381*22ce4affSfengbojiang int vm_domain_allocate(struct vm_domain *vmd, int req, int npages);
382*22ce4affSfengbojiang 
383*22ce4affSfengbojiang /*
384*22ce4affSfengbojiang  *      vm_pagequeue_domain:
385*22ce4affSfengbojiang  *
386*22ce4affSfengbojiang  *      Return the memory domain the page belongs to.
387*22ce4affSfengbojiang  */
388*22ce4affSfengbojiang static inline struct vm_domain *
vm_pagequeue_domain(vm_page_t m)389*22ce4affSfengbojiang vm_pagequeue_domain(vm_page_t m)
390*22ce4affSfengbojiang {
391*22ce4affSfengbojiang 
392*22ce4affSfengbojiang 	return (VM_DOMAIN(vm_page_domain(m)));
393*22ce4affSfengbojiang }
394*22ce4affSfengbojiang 
395*22ce4affSfengbojiang /*
396*22ce4affSfengbojiang  * Return the number of pages we need to free-up or cache
397*22ce4affSfengbojiang  * A positive number indicates that we do not have enough free pages.
398*22ce4affSfengbojiang  */
399*22ce4affSfengbojiang static inline int
vm_paging_target(struct vm_domain * vmd)400*22ce4affSfengbojiang vm_paging_target(struct vm_domain *vmd)
401*22ce4affSfengbojiang {
402*22ce4affSfengbojiang 
403*22ce4affSfengbojiang 	return (vmd->vmd_free_target - vmd->vmd_free_count);
404*22ce4affSfengbojiang }
405*22ce4affSfengbojiang 
406*22ce4affSfengbojiang /*
407*22ce4affSfengbojiang  * Returns TRUE if the pagedaemon needs to be woken up.
408*22ce4affSfengbojiang  */
409*22ce4affSfengbojiang static inline int
vm_paging_needed(struct vm_domain * vmd,u_int free_count)410*22ce4affSfengbojiang vm_paging_needed(struct vm_domain *vmd, u_int free_count)
411*22ce4affSfengbojiang {
412*22ce4affSfengbojiang 
413*22ce4affSfengbojiang 	return (free_count < vmd->vmd_pageout_wakeup_thresh);
414*22ce4affSfengbojiang }
415*22ce4affSfengbojiang 
416*22ce4affSfengbojiang /*
417*22ce4affSfengbojiang  * Returns TRUE if the domain is below the min paging target.
418*22ce4affSfengbojiang  */
419*22ce4affSfengbojiang static inline int
vm_paging_min(struct vm_domain * vmd)420*22ce4affSfengbojiang vm_paging_min(struct vm_domain *vmd)
421*22ce4affSfengbojiang {
422*22ce4affSfengbojiang 
423*22ce4affSfengbojiang         return (vmd->vmd_free_min > vmd->vmd_free_count);
424*22ce4affSfengbojiang }
425*22ce4affSfengbojiang 
426*22ce4affSfengbojiang /*
427*22ce4affSfengbojiang  * Returns TRUE if the domain is below the severe paging target.
428*22ce4affSfengbojiang  */
429*22ce4affSfengbojiang static inline int
vm_paging_severe(struct vm_domain * vmd)430*22ce4affSfengbojiang vm_paging_severe(struct vm_domain *vmd)
431*22ce4affSfengbojiang {
432*22ce4affSfengbojiang 
433*22ce4affSfengbojiang         return (vmd->vmd_free_severe > vmd->vmd_free_count);
434*22ce4affSfengbojiang }
435*22ce4affSfengbojiang 
436*22ce4affSfengbojiang /*
437*22ce4affSfengbojiang  * Return the number of pages we need to launder.
438*22ce4affSfengbojiang  * A positive number indicates that we have a shortfall of clean pages.
439*22ce4affSfengbojiang  */
440*22ce4affSfengbojiang static inline int
vm_laundry_target(struct vm_domain * vmd)441*22ce4affSfengbojiang vm_laundry_target(struct vm_domain *vmd)
442*22ce4affSfengbojiang {
443*22ce4affSfengbojiang 
444*22ce4affSfengbojiang 	return (vm_paging_target(vmd));
445*22ce4affSfengbojiang }
446*22ce4affSfengbojiang 
447*22ce4affSfengbojiang void pagedaemon_wakeup(int domain);
448*22ce4affSfengbojiang 
449*22ce4affSfengbojiang static inline void
vm_domain_freecnt_inc(struct vm_domain * vmd,int adj)450*22ce4affSfengbojiang vm_domain_freecnt_inc(struct vm_domain *vmd, int adj)
451*22ce4affSfengbojiang {
452*22ce4affSfengbojiang 	u_int old, new;
453*22ce4affSfengbojiang 
454*22ce4affSfengbojiang 	old = atomic_fetchadd_int(&vmd->vmd_free_count, adj);
455*22ce4affSfengbojiang 	new = old + adj;
456*22ce4affSfengbojiang 	/*
457*22ce4affSfengbojiang 	 * Only update bitsets on transitions.  Notice we short-circuit the
458*22ce4affSfengbojiang 	 * rest of the checks if we're above min already.
459*22ce4affSfengbojiang 	 */
460*22ce4affSfengbojiang 	if (old < vmd->vmd_free_min && (new >= vmd->vmd_free_min ||
461*22ce4affSfengbojiang 	    (old < vmd->vmd_free_severe && new >= vmd->vmd_free_severe) ||
462*22ce4affSfengbojiang 	    (old < vmd->vmd_pageout_free_min &&
463*22ce4affSfengbojiang 	    new >= vmd->vmd_pageout_free_min)))
464*22ce4affSfengbojiang 		vm_domain_clear(vmd);
465*22ce4affSfengbojiang }
466*22ce4affSfengbojiang 
467*22ce4affSfengbojiang #endif	/* _KERNEL */
468*22ce4affSfengbojiang #endif				/* !_VM_PAGEQUEUE_ */
469