xref: /freebsd/sys/vm/vm_pagequeue.h (revision 3982006ed5587cfd83cc1955186e0aa4b92b3fcd)
1 /*-
2  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * The Mach Operating System project at Carnegie-Mellon University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)vm_page.h	8.2 (Berkeley) 12/13/93
35  *
36  *
37  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38  * All rights reserved.
39  *
40  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41  *
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.
47  *
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.
51  *
52  * Carnegie Mellon requests users of this software to return to
53  *
54  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55  *  School of Computer Science
56  *  Carnegie Mellon University
57  *  Pittsburgh PA 15213-3890
58  *
59  * any improvements or extensions that they make and grant Carnegie the
60  * rights to redistribute these changes.
61  *
62  * $FreeBSD$
63  */
64 
65 #ifndef	_VM_PAGEQUEUE_
66 #define	_VM_PAGEQUEUE_
67 
68 #ifdef _KERNEL
69 struct vm_pagequeue {
70 	struct mtx	pq_mutex;
71 	struct pglist	pq_pl;
72 	int		pq_cnt;
73 	const char	* const pq_name;
74 } __aligned(CACHE_LINE_SIZE);
75 
76 
77 struct vm_domain {
78 	struct vm_pagequeue vmd_pagequeues[PQ_COUNT];
79 	struct mtx_padalign vmd_free_mtx;
80 	struct vmem *vmd_kernel_arena;
81 	u_int vmd_domain;		/* Domain number. */
82 	u_int vmd_page_count;
83 	long vmd_segs;			/* bitmask of the segments */
84 
85 	/* Paging control variables, locked by domain_free_mtx. */
86 	u_int vmd_free_count;
87 	boolean_t vmd_oom;
88 	int vmd_oom_seq;
89 	int vmd_last_active_scan;
90 	struct vm_page vmd_laundry_marker;
91 	struct vm_page vmd_marker; /* marker for pagedaemon private use */
92 	struct vm_page vmd_inacthead; /* marker for LRU-defeating insertions */
93 
94 	int vmd_pageout_pages_needed;	/* page daemon waiting for pages? */
95 	int vmd_pageout_deficit;	/* Estimated number of pages deficit */
96 	bool vmd_pageout_wanted;	/* pageout daemon wait channel */
97 	bool vmd_minset;		/* Are we in vm_min_domains? */
98 	bool vmd_severeset;		/* Are we in vm_severe_domains? */
99 	int vmd_inactq_scans;
100 	enum {
101 		VM_LAUNDRY_IDLE = 0,
102 		VM_LAUNDRY_BACKGROUND,
103 		VM_LAUNDRY_SHORTFALL
104 	} vmd_laundry_request;
105 
106 	/* Paging thresholds. */
107 	u_int vmd_background_launder_target;
108 	u_int vmd_free_reserved;	/* (c) pages reserved for deadlock */
109 	u_int vmd_free_target;		/* (c) pages desired free */
110 	u_int vmd_free_min;		/* (c) pages desired free */
111 	u_int vmd_inactive_target;	/* (c) pages desired inactive */
112 	u_int vmd_pageout_free_min;	/* (c) min pages reserved for kernel */
113 	u_int vmd_pageout_wakeup_thresh;/* (c) min pages to wake pagedaemon */
114 	u_int vmd_interrupt_free_min;	/* (c) reserved pages for int code */
115 	u_int vmd_free_severe;		/* (c) severe page depletion point */
116 } __aligned(CACHE_LINE_SIZE);
117 
118 extern struct vm_domain vm_dom[MAXMEMDOM];
119 
120 #define	VM_DOMAIN(n)	(&vm_dom[(n)])
121 
122 #define	vm_pagequeue_assert_locked(pq)	mtx_assert(&(pq)->pq_mutex, MA_OWNED)
123 #define	vm_pagequeue_lock(pq)		mtx_lock(&(pq)->pq_mutex)
124 #define	vm_pagequeue_lockptr(pq)	(&(pq)->pq_mutex)
125 #define	vm_pagequeue_unlock(pq)		mtx_unlock(&(pq)->pq_mutex)
126 
127 #define	vm_domain_free_assert_locked(n)					\
128 	    mtx_assert(vm_domain_free_lockptr((n)), MA_OWNED)
129 #define	vm_domain_free_assert_unlocked(n)				\
130 	    mtx_assert(vm_domain_free_lockptr((n)), MA_NOTOWNED)
131 #define	vm_domain_free_lock(d)						\
132 	    mtx_lock(vm_domain_free_lockptr((d)))
133 #define	vm_domain_free_lockptr(d)					\
134 	    (&(d)->vmd_free_mtx)
135 #define	vm_domain_free_unlock(d)					\
136 	    mtx_unlock(vm_domain_free_lockptr((d)))
137 
138 static __inline void
139 vm_pagequeue_cnt_add(struct vm_pagequeue *pq, int addend)
140 {
141 
142 #ifdef notyet
143 	vm_pagequeue_assert_locked(pq);
144 #endif
145 	pq->pq_cnt += addend;
146 }
147 #define	vm_pagequeue_cnt_inc(pq)	vm_pagequeue_cnt_add((pq), 1)
148 #define	vm_pagequeue_cnt_dec(pq)	vm_pagequeue_cnt_add((pq), -1)
149 
150 void vm_domain_set(struct vm_domain *vmd);
151 int vm_domain_available(struct vm_domain *vmd, int req, int npages);
152 
153 /*
154  *      vm_pagequeue_domain:
155  *
156  *      Return the memory domain the page belongs to.
157  */
158 static inline struct vm_domain *
159 vm_pagequeue_domain(vm_page_t m)
160 {
161 
162 	return (VM_DOMAIN(vm_phys_domain(m)));
163 }
164 
165 /*
166  * Return the number of pages we need to free-up or cache
167  * A positive number indicates that we do not have enough free pages.
168  */
169 static inline int
170 vm_paging_target(struct vm_domain *vmd)
171 {
172 
173 	return (vmd->vmd_free_target - vmd->vmd_free_count);
174 }
175 
176 /*
177  * Returns TRUE if the pagedaemon needs to be woken up.
178  */
179 static inline int
180 vm_paging_needed(struct vm_domain *vmd, u_int free_count)
181 {
182 
183 	return (free_count < vmd->vmd_pageout_wakeup_thresh);
184 }
185 
186 /*
187  * Returns TRUE if the domain is below the min paging target.
188  */
189 static inline int
190 vm_paging_min(struct vm_domain *vmd)
191 {
192 
193         return (vmd->vmd_free_min > vmd->vmd_free_count);
194 }
195 
196 /*
197  * Returns TRUE if the domain is below the severe paging target.
198  */
199 static inline int
200 vm_paging_severe(struct vm_domain *vmd)
201 {
202 
203         return (vmd->vmd_free_severe > vmd->vmd_free_count);
204 }
205 
206 /*
207  * Return the number of pages we need to launder.
208  * A positive number indicates that we have a shortfall of clean pages.
209  */
210 static inline int
211 vm_laundry_target(struct vm_domain *vmd)
212 {
213 
214 	return (vm_paging_target(vmd));
215 }
216 
217 static inline u_int
218 vm_domain_freecnt_adj(struct vm_domain *vmd, int adj)
219 {
220 	u_int ret;
221 
222 	vm_domain_free_assert_locked(vmd);
223 	ret = vmd->vmd_free_count += adj;
224 	if ((!vmd->vmd_minset && vm_paging_min(vmd)) ||
225 	    (!vmd->vmd_severeset && vm_paging_severe(vmd)))
226 		vm_domain_set(vmd);
227 
228 	return (ret);
229 }
230 
231 
232 #endif	/* _KERNEL */
233 #endif				/* !_VM_PAGEQUEUE_ */
234