2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)vm_page.h 8.2 (Berkeley) 12/13/93
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
65 #ifndef _VM_PAGEQUEUE_
66 #define _VM_PAGEQUEUE_
73 const char * const pq_name;
74 } __aligned(CACHE_LINE_SIZE);
76 #include <sys/pidctrl.h>
80 * One vm_domain per-numa domain. Contains pagequeues, free page structures,
91 struct vm_pagequeue vmd_pagequeues[PQ_COUNT];
92 struct mtx_padalign vmd_free_mtx;
93 struct mtx_padalign vmd_pageout_mtx;
94 struct vmem *vmd_kernel_arena; /* (c) per-domain kva arena. */
95 u_int vmd_domain; /* (c) Domain number. */
96 u_int vmd_page_count; /* (c) Total page count. */
97 long vmd_segs; /* (c) bitmask of the segments */
98 u_int __aligned(CACHE_LINE_SIZE) vmd_free_count; /* (a,f) free page count */
99 u_int vmd_pageout_deficit; /* (a) Estimated number of pages deficit */
100 uint8_t vmd_pad[CACHE_LINE_SIZE - (sizeof(u_int) * 2)];
102 /* Paging control variables, used within single threaded page daemon. */
103 struct pidctrl vmd_pid; /* Pageout controller. */
106 int vmd_last_active_scan;
107 struct vm_page vmd_laundry_marker;
108 struct vm_page vmd_marker; /* marker for pagedaemon private use */
109 struct vm_page vmd_inacthead; /* marker for LRU-defeating insertions */
111 int vmd_pageout_wanted; /* (a, p) pageout daemon wait channel */
112 int vmd_pageout_pages_needed; /* (d) page daemon waiting for pages? */
113 bool vmd_minset; /* (d) Are we in vm_min_domains? */
114 bool vmd_severeset; /* (d) Are we in vm_severe_domains? */
115 int vmd_inactq_scans;
118 VM_LAUNDRY_BACKGROUND,
120 } vmd_laundry_request;
122 /* Paging thresholds. */
123 u_int vmd_background_launder_target;
124 u_int vmd_free_reserved; /* (c) pages reserved for deadlock */
125 u_int vmd_free_target; /* (c) pages desired free */
126 u_int vmd_free_min; /* (c) pages desired free */
127 u_int vmd_inactive_target; /* (c) pages desired inactive */
128 u_int vmd_pageout_free_min; /* (c) min pages reserved for kernel */
129 u_int vmd_pageout_wakeup_thresh;/* (c) min pages to wake pagedaemon */
130 u_int vmd_interrupt_free_min; /* (c) reserved pages for int code */
131 u_int vmd_free_severe; /* (c) severe page depletion point */
133 /* Name for sysctl etc. */
134 struct sysctl_oid *vmd_oid;
135 char vmd_name[sizeof(__XSTRING(MAXMEMDOM))];
136 } __aligned(CACHE_LINE_SIZE);
138 extern struct vm_domain vm_dom[MAXMEMDOM];
140 #define VM_DOMAIN(n) (&vm_dom[(n)])
142 #define vm_pagequeue_assert_locked(pq) mtx_assert(&(pq)->pq_mutex, MA_OWNED)
143 #define vm_pagequeue_lock(pq) mtx_lock(&(pq)->pq_mutex)
144 #define vm_pagequeue_lockptr(pq) (&(pq)->pq_mutex)
145 #define vm_pagequeue_unlock(pq) mtx_unlock(&(pq)->pq_mutex)
147 #define vm_domain_free_assert_locked(n) \
148 mtx_assert(vm_domain_free_lockptr((n)), MA_OWNED)
149 #define vm_domain_free_assert_unlocked(n) \
150 mtx_assert(vm_domain_free_lockptr((n)), MA_NOTOWNED)
151 #define vm_domain_free_lock(d) \
152 mtx_lock(vm_domain_free_lockptr((d)))
153 #define vm_domain_free_lockptr(d) \
155 #define vm_domain_free_unlock(d) \
156 mtx_unlock(vm_domain_free_lockptr((d)))
158 #define vm_domain_pageout_lockptr(d) \
159 (&(d)->vmd_pageout_mtx)
160 #define vm_domain_pageout_assert_locked(n) \
161 mtx_assert(vm_domain_pageout_lockptr((n)), MA_OWNED)
162 #define vm_domain_pageout_assert_unlocked(n) \
163 mtx_assert(vm_domain_pageout_lockptr((n)), MA_NOTOWNED)
164 #define vm_domain_pageout_lock(d) \
165 mtx_lock(vm_domain_pageout_lockptr((d)))
166 #define vm_domain_pageout_unlock(d) \
167 mtx_unlock(vm_domain_pageout_lockptr((d)))
170 vm_pagequeue_cnt_add(struct vm_pagequeue *pq, int addend)
174 vm_pagequeue_assert_locked(pq);
176 pq->pq_cnt += addend;
178 #define vm_pagequeue_cnt_inc(pq) vm_pagequeue_cnt_add((pq), 1)
179 #define vm_pagequeue_cnt_dec(pq) vm_pagequeue_cnt_add((pq), -1)
181 void vm_domain_set(struct vm_domain *vmd);
182 void vm_domain_clear(struct vm_domain *vmd);
183 int vm_domain_available(struct vm_domain *vmd, int req, int npages);
186 * vm_pagequeue_domain:
188 * Return the memory domain the page belongs to.
190 static inline struct vm_domain *
191 vm_pagequeue_domain(vm_page_t m)
194 return (VM_DOMAIN(vm_phys_domain(m)));
198 * Return the number of pages we need to free-up or cache
199 * A positive number indicates that we do not have enough free pages.
202 vm_paging_target(struct vm_domain *vmd)
205 return (vmd->vmd_free_target - vmd->vmd_free_count);
209 * Returns TRUE if the pagedaemon needs to be woken up.
212 vm_paging_needed(struct vm_domain *vmd, u_int free_count)
215 return (free_count < vmd->vmd_pageout_wakeup_thresh);
219 * Returns TRUE if the domain is below the min paging target.
222 vm_paging_min(struct vm_domain *vmd)
225 return (vmd->vmd_free_min > vmd->vmd_free_count);
229 * Returns TRUE if the domain is below the severe paging target.
232 vm_paging_severe(struct vm_domain *vmd)
235 return (vmd->vmd_free_severe > vmd->vmd_free_count);
239 * Return the number of pages we need to launder.
240 * A positive number indicates that we have a shortfall of clean pages.
243 vm_laundry_target(struct vm_domain *vmd)
246 return (vm_paging_target(vmd));
249 void pagedaemon_wakeup(int domain);
252 vm_domain_freecnt_inc(struct vm_domain *vmd, int adj)
256 old = atomic_fetchadd_int(&vmd->vmd_free_count, adj);
259 * Only update bitsets on transitions. Notice we short-circuit the
260 * rest of the checks if we're above min already.
262 if (old < vmd->vmd_free_min && (new >= vmd->vmd_free_min ||
263 (old < vmd->vmd_free_severe && new >= vmd->vmd_free_severe) ||
264 (old < vmd->vmd_pageout_free_min &&
265 new >= vmd->vmd_pageout_free_min)))
266 vm_domain_clear(vmd);
270 vm_domain_freecnt_dec(struct vm_domain *vmd, int adj)
274 old = atomic_fetchadd_int(&vmd->vmd_free_count, -adj);
276 KASSERT(new >= 0, ("vm_domain_freecnt_dec: free count underflow"));
277 if (vm_paging_needed(vmd, new) && !vm_paging_needed(vmd, old))
278 pagedaemon_wakeup(vmd->vmd_domain);
279 /* Only update bitsets on transitions. */
280 if ((old >= vmd->vmd_free_min && new < vmd->vmd_free_min) ||
281 (old >= vmd->vmd_free_severe && new < vmd->vmd_free_severe))
287 #endif /* !_VM_PAGEQUEUE_ */