uvm_pdaemon.c revision 1.87.14.2 1 1.87.14.2 ad /* $NetBSD: uvm_pdaemon.c,v 1.87.14.2 2007/07/21 19:21:56 ad Exp $ */
2 1.87.14.2 ad
3 1.87.14.2 ad /*
4 1.87.14.2 ad * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.87.14.2 ad * Copyright (c) 1991, 1993, The Regents of the University of California.
6 1.87.14.2 ad *
7 1.87.14.2 ad * All rights reserved.
8 1.87.14.2 ad *
9 1.87.14.2 ad * This code is derived from software contributed to Berkeley by
10 1.87.14.2 ad * The Mach Operating System project at Carnegie-Mellon University.
11 1.87.14.2 ad *
12 1.87.14.2 ad * Redistribution and use in source and binary forms, with or without
13 1.87.14.2 ad * modification, are permitted provided that the following conditions
14 1.87.14.2 ad * are met:
15 1.87.14.2 ad * 1. Redistributions of source code must retain the above copyright
16 1.87.14.2 ad * notice, this list of conditions and the following disclaimer.
17 1.87.14.2 ad * 2. Redistributions in binary form must reproduce the above copyright
18 1.87.14.2 ad * notice, this list of conditions and the following disclaimer in the
19 1.87.14.2 ad * documentation and/or other materials provided with the distribution.
20 1.87.14.2 ad * 3. All advertising materials mentioning features or use of this software
21 1.87.14.2 ad * must display the following acknowledgement:
22 1.87.14.2 ad * This product includes software developed by Charles D. Cranor,
23 1.87.14.2 ad * Washington University, the University of California, Berkeley and
24 1.87.14.2 ad * its contributors.
25 1.87.14.2 ad * 4. Neither the name of the University nor the names of its contributors
26 1.87.14.2 ad * may be used to endorse or promote products derived from this software
27 1.87.14.2 ad * without specific prior written permission.
28 1.87.14.2 ad *
29 1.87.14.2 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.87.14.2 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.87.14.2 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.87.14.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.87.14.2 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.87.14.2 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.87.14.2 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.87.14.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.87.14.2 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.87.14.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.87.14.2 ad * SUCH DAMAGE.
40 1.87.14.2 ad *
41 1.87.14.2 ad * @(#)vm_pageout.c 8.5 (Berkeley) 2/14/94
42 1.87.14.2 ad * from: Id: uvm_pdaemon.c,v 1.1.2.32 1998/02/06 05:26:30 chs Exp
43 1.87.14.2 ad *
44 1.87.14.2 ad *
45 1.87.14.2 ad * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46 1.87.14.2 ad * All rights reserved.
47 1.87.14.2 ad *
48 1.87.14.2 ad * Permission to use, copy, modify and distribute this software and
49 1.87.14.2 ad * its documentation is hereby granted, provided that both the copyright
50 1.87.14.2 ad * notice and this permission notice appear in all copies of the
51 1.87.14.2 ad * software, derivative works or modified versions, and any portions
52 1.87.14.2 ad * thereof, and that both notices appear in supporting documentation.
53 1.87.14.2 ad *
54 1.87.14.2 ad * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55 1.87.14.2 ad * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56 1.87.14.2 ad * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57 1.87.14.2 ad *
58 1.87.14.2 ad * Carnegie Mellon requests users of this software to return to
59 1.87.14.2 ad *
60 1.87.14.2 ad * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
61 1.87.14.2 ad * School of Computer Science
62 1.87.14.2 ad * Carnegie Mellon University
63 1.87.14.2 ad * Pittsburgh PA 15213-3890
64 1.87.14.2 ad *
65 1.87.14.2 ad * any improvements or extensions that they make and grant Carnegie the
66 1.87.14.2 ad * rights to redistribute these changes.
67 1.87.14.2 ad */
68 1.87.14.2 ad
69 1.87.14.2 ad /*
70 1.87.14.2 ad * uvm_pdaemon.c: the page daemon
71 1.87.14.2 ad */
72 1.87.14.2 ad
73 1.87.14.2 ad #include <sys/cdefs.h>
74 1.87.14.2 ad __KERNEL_RCSID(0, "$NetBSD: uvm_pdaemon.c,v 1.87.14.2 2007/07/21 19:21:56 ad Exp $");
75 1.87.14.2 ad
76 1.87.14.2 ad #include "opt_uvmhist.h"
77 1.87.14.2 ad #include "opt_readahead.h"
78 1.87.14.2 ad
79 1.87.14.2 ad #include <sys/param.h>
80 1.87.14.2 ad #include <sys/proc.h>
81 1.87.14.2 ad #include <sys/systm.h>
82 1.87.14.2 ad #include <sys/kernel.h>
83 1.87.14.2 ad #include <sys/pool.h>
84 1.87.14.2 ad #include <sys/buf.h>
85 1.87.14.2 ad
86 1.87.14.2 ad #include <uvm/uvm.h>
87 1.87.14.2 ad #include <uvm/uvm_pdpolicy.h>
88 1.87.14.2 ad
89 1.87.14.2 ad /*
90 1.87.14.2 ad * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
91 1.87.14.2 ad * in a pass thru the inactive list when swap is full. the value should be
92 1.87.14.2 ad * "small"... if it's too large we'll cycle the active pages thru the inactive
93 1.87.14.2 ad * queue too quickly to for them to be referenced and avoid being freed.
94 1.87.14.2 ad */
95 1.87.14.2 ad
96 1.87.14.2 ad #define UVMPD_NUMDIRTYREACTS 16
97 1.87.14.2 ad
98 1.87.14.2 ad
99 1.87.14.2 ad /*
100 1.87.14.2 ad * local prototypes
101 1.87.14.2 ad */
102 1.87.14.2 ad
103 1.87.14.2 ad static void uvmpd_scan(void);
104 1.87.14.2 ad static void uvmpd_scan_queue(void);
105 1.87.14.2 ad static void uvmpd_tune(void);
106 1.87.14.2 ad
107 1.87.14.2 ad /*
108 1.87.14.2 ad * XXX hack to avoid hangs when large processes fork.
109 1.87.14.2 ad */
110 1.87.14.2 ad int uvm_extrapages;
111 1.87.14.2 ad
112 1.87.14.2 ad /*
113 1.87.14.2 ad * uvm_wait: wait (sleep) for the page daemon to free some pages
114 1.87.14.2 ad *
115 1.87.14.2 ad * => should be called with all locks released
116 1.87.14.2 ad * => should _not_ be called by the page daemon (to avoid deadlock)
117 1.87.14.2 ad */
118 1.87.14.2 ad
119 1.87.14.2 ad void
120 1.87.14.2 ad uvm_wait(const char *wmsg)
121 1.87.14.2 ad {
122 1.87.14.2 ad int timo = 0;
123 1.87.14.2 ad int s = splbio();
124 1.87.14.2 ad
125 1.87.14.2 ad /*
126 1.87.14.2 ad * check for page daemon going to sleep (waiting for itself)
127 1.87.14.2 ad */
128 1.87.14.2 ad
129 1.87.14.2 ad if (curlwp == uvm.pagedaemon_lwp && uvmexp.paging == 0) {
130 1.87.14.2 ad /*
131 1.87.14.2 ad * now we have a problem: the pagedaemon wants to go to
132 1.87.14.2 ad * sleep until it frees more memory. but how can it
133 1.87.14.2 ad * free more memory if it is asleep? that is a deadlock.
134 1.87.14.2 ad * we have two options:
135 1.87.14.2 ad * [1] panic now
136 1.87.14.2 ad * [2] put a timeout on the sleep, thus causing the
137 1.87.14.2 ad * pagedaemon to only pause (rather than sleep forever)
138 1.87.14.2 ad *
139 1.87.14.2 ad * note that option [2] will only help us if we get lucky
140 1.87.14.2 ad * and some other process on the system breaks the deadlock
141 1.87.14.2 ad * by exiting or freeing memory (thus allowing the pagedaemon
142 1.87.14.2 ad * to continue). for now we panic if DEBUG is defined,
143 1.87.14.2 ad * otherwise we hope for the best with option [2] (better
144 1.87.14.2 ad * yet, this should never happen in the first place!).
145 1.87.14.2 ad */
146 1.87.14.2 ad
147 1.87.14.2 ad printf("pagedaemon: deadlock detected!\n");
148 1.87.14.2 ad timo = hz >> 3; /* set timeout */
149 1.87.14.2 ad #if defined(DEBUG)
150 1.87.14.2 ad /* DEBUG: panic so we can debug it */
151 1.87.14.2 ad panic("pagedaemon deadlock");
152 1.87.14.2 ad #endif
153 1.87.14.2 ad }
154 1.87.14.2 ad
155 1.87.14.2 ad mutex_enter(&uvm_pagedaemon_lock);
156 1.87.14.2 ad wakeup(&uvm.pagedaemon); /* wake the daemon! */
157 1.87.14.2 ad mtsleep(&uvmexp.free, PVM, wmsg, timo, &uvm_pagedaemon_lock);
158 1.87.14.2 ad mutex_exit(&uvm_pagedaemon_lock);
159 1.87.14.2 ad
160 1.87.14.2 ad splx(s);
161 1.87.14.2 ad }
162 1.87.14.2 ad
163 1.87.14.2 ad /*
164 1.87.14.2 ad * uvm_kick_pdaemon: perform checks to determine if we need to
165 1.87.14.2 ad * give the pagedaemon a nudge, and do so if necessary.
166 1.87.14.2 ad */
167 1.87.14.2 ad
168 1.87.14.2 ad void
169 1.87.14.2 ad uvm_kick_pdaemon(void)
170 1.87.14.2 ad {
171 1.87.14.2 ad
172 1.87.14.2 ad if (uvmexp.free + uvmexp.paging < uvmexp.freemin ||
173 1.87.14.2 ad (uvmexp.free + uvmexp.paging < uvmexp.freetarg &&
174 1.87.14.2 ad uvmpdpol_needsscan_p())) {
175 1.87.14.2 ad wakeup(&uvm.pagedaemon);
176 1.87.14.2 ad }
177 1.87.14.2 ad }
178 1.87.14.2 ad
179 1.87.14.2 ad /*
180 1.87.14.2 ad * uvmpd_tune: tune paging parameters
181 1.87.14.2 ad *
182 1.87.14.2 ad * => called when ever memory is added (or removed?) to the system
183 1.87.14.2 ad * => caller must call with page queues locked
184 1.87.14.2 ad */
185 1.87.14.2 ad
186 1.87.14.2 ad static void
187 1.87.14.2 ad uvmpd_tune(void)
188 1.87.14.2 ad {
189 1.87.14.2 ad UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist);
190 1.87.14.2 ad
191 1.87.14.2 ad uvmexp.freemin = uvmexp.npages / 20;
192 1.87.14.2 ad
193 1.87.14.2 ad /* between 16k and 256k */
194 1.87.14.2 ad /* XXX: what are these values good for? */
195 1.87.14.2 ad uvmexp.freemin = MAX(uvmexp.freemin, (16*1024) >> PAGE_SHIFT);
196 1.87.14.2 ad uvmexp.freemin = MIN(uvmexp.freemin, (256*1024) >> PAGE_SHIFT);
197 1.87.14.2 ad
198 1.87.14.2 ad /* Make sure there's always a user page free. */
199 1.87.14.2 ad if (uvmexp.freemin < uvmexp.reserve_kernel + 1)
200 1.87.14.2 ad uvmexp.freemin = uvmexp.reserve_kernel + 1;
201 1.87.14.2 ad
202 1.87.14.2 ad uvmexp.freetarg = (uvmexp.freemin * 4) / 3;
203 1.87.14.2 ad if (uvmexp.freetarg <= uvmexp.freemin)
204 1.87.14.2 ad uvmexp.freetarg = uvmexp.freemin + 1;
205 1.87.14.2 ad
206 1.87.14.2 ad uvmexp.freetarg += uvm_extrapages;
207 1.87.14.2 ad uvm_extrapages = 0;
208 1.87.14.2 ad
209 1.87.14.2 ad uvmexp.wiredmax = uvmexp.npages / 3;
210 1.87.14.2 ad UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
211 1.87.14.2 ad uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
212 1.87.14.2 ad }
213 1.87.14.2 ad
214 1.87.14.2 ad /*
215 1.87.14.2 ad * uvm_pageout: the main loop for the pagedaemon
216 1.87.14.2 ad */
217 1.87.14.2 ad
218 1.87.14.2 ad void
219 1.87.14.2 ad uvm_pageout(void *arg)
220 1.87.14.2 ad {
221 1.87.14.2 ad int bufcnt, npages = 0;
222 1.87.14.2 ad int extrapages = 0;
223 1.87.14.2 ad UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist);
224 1.87.14.2 ad
225 1.87.14.2 ad UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
226 1.87.14.2 ad
227 1.87.14.2 ad /*
228 1.87.14.2 ad * ensure correct priority and set paging parameters...
229 1.87.14.2 ad */
230 1.87.14.2 ad
231 1.87.14.2 ad uvm.pagedaemon_lwp = curlwp;
232 1.87.14.2 ad uvm_lock_pageq();
233 1.87.14.2 ad npages = uvmexp.npages;
234 1.87.14.2 ad uvmpd_tune();
235 1.87.14.2 ad uvm_unlock_pageq();
236 1.87.14.2 ad
237 1.87.14.2 ad /*
238 1.87.14.2 ad * main loop
239 1.87.14.2 ad */
240 1.87.14.2 ad
241 1.87.14.2 ad for (;;) {
242 1.87.14.2 ad mutex_enter(&uvm_pagedaemon_lock);
243 1.87.14.2 ad
244 1.87.14.2 ad UVMHIST_LOG(pdhist," <<SLEEPING>>",0,0,0,0);
245 1.87.14.2 ad mtsleep(&uvm.pagedaemon, PVM | PNORELOCK, "pgdaemon", 0,
246 1.87.14.2 ad &uvm_pagedaemon_lock);
247 1.87.14.2 ad uvmexp.pdwoke++;
248 1.87.14.2 ad UVMHIST_LOG(pdhist," <<WOKE UP>>",0,0,0,0);
249 1.87.14.2 ad
250 1.87.14.2 ad /*
251 1.87.14.2 ad * now lock page queues and recompute inactive count
252 1.87.14.2 ad */
253 1.87.14.2 ad
254 1.87.14.2 ad uvm_lock_pageq();
255 1.87.14.2 ad if (npages != uvmexp.npages || extrapages != uvm_extrapages) {
256 1.87.14.2 ad npages = uvmexp.npages;
257 1.87.14.2 ad extrapages = uvm_extrapages;
258 1.87.14.2 ad uvmpd_tune();
259 1.87.14.2 ad }
260 1.87.14.2 ad
261 1.87.14.2 ad uvmpdpol_tune();
262 1.87.14.2 ad
263 1.87.14.2 ad /*
264 1.87.14.2 ad * Estimate a hint. Note that bufmem are returned to
265 1.87.14.2 ad * system only when entire pool page is empty.
266 1.87.14.2 ad */
267 1.87.14.2 ad bufcnt = uvmexp.freetarg - uvmexp.free;
268 1.87.14.2 ad if (bufcnt < 0)
269 1.87.14.2 ad bufcnt = 0;
270 1.87.14.2 ad
271 1.87.14.2 ad UVMHIST_LOG(pdhist," free/ftarg=%d/%d",
272 1.87.14.2 ad uvmexp.free, uvmexp.freetarg, 0,0);
273 1.87.14.2 ad
274 1.87.14.2 ad /*
275 1.87.14.2 ad * scan if needed
276 1.87.14.2 ad */
277 1.87.14.2 ad
278 1.87.14.2 ad if (uvmexp.free + uvmexp.paging < uvmexp.freetarg ||
279 1.87.14.2 ad uvmpdpol_needsscan_p()) {
280 1.87.14.2 ad uvmpd_scan();
281 1.87.14.2 ad }
282 1.87.14.2 ad
283 1.87.14.2 ad /*
284 1.87.14.2 ad * if there's any free memory to be had,
285 1.87.14.2 ad * wake up any waiters.
286 1.87.14.2 ad */
287 1.87.14.2 ad
288 1.87.14.2 ad if (uvmexp.free > uvmexp.reserve_kernel ||
289 1.87.14.2 ad uvmexp.paging == 0) {
290 1.87.14.2 ad wakeup(&uvmexp.free);
291 1.87.14.2 ad }
292 1.87.14.2 ad
293 1.87.14.2 ad /*
294 1.87.14.2 ad * scan done. unlock page queues (the only lock we are holding)
295 1.87.14.2 ad */
296 1.87.14.2 ad
297 1.87.14.2 ad uvm_unlock_pageq();
298 1.87.14.2 ad
299 1.87.14.2 ad buf_drain(bufcnt << PAGE_SHIFT);
300 1.87.14.2 ad
301 1.87.14.2 ad /*
302 1.87.14.2 ad * drain pool resources now that we're not holding any locks
303 1.87.14.2 ad */
304 1.87.14.2 ad
305 1.87.14.2 ad pool_drain(0);
306 1.87.14.2 ad
307 1.87.14.2 ad /*
308 1.87.14.2 ad * free any cached u-areas we don't need
309 1.87.14.2 ad */
310 1.87.14.2 ad uvm_uarea_drain(true);
311 1.87.14.2 ad
312 1.87.14.2 ad }
313 1.87.14.2 ad /*NOTREACHED*/
314 1.87.14.2 ad }
315 1.87.14.2 ad
316 1.87.14.2 ad
317 1.87.14.2 ad /*
318 1.87.14.2 ad * uvm_aiodone_worker: a workqueue callback for the aiodone daemon.
319 1.87.14.2 ad */
320 1.87.14.2 ad
321 1.87.14.2 ad void
322 1.87.14.2 ad uvm_aiodone_worker(struct work *wk, void *dummy)
323 1.87.14.2 ad {
324 1.87.14.2 ad int free;
325 1.87.14.2 ad struct buf *bp = (void *)wk;
326 1.87.14.2 ad
327 1.87.14.2 ad KASSERT(&bp->b_work == wk);
328 1.87.14.2 ad
329 1.87.14.2 ad /*
330 1.87.14.2 ad * process an i/o that's done.
331 1.87.14.2 ad */
332 1.87.14.2 ad
333 1.87.14.2 ad free = uvmexp.free;
334 1.87.14.2 ad (*bp->b_iodone)(bp);
335 1.87.14.2 ad if (free <= uvmexp.reserve_kernel) {
336 1.87.14.2 ad mutex_spin_enter(&uvm_fpageqlock);
337 1.87.14.2 ad wakeup(&uvm.pagedaemon);
338 1.87.14.2 ad mutex_spin_exit(&uvm_fpageqlock);
339 1.87.14.2 ad } else {
340 1.87.14.2 ad mutex_enter(&uvm_pagedaemon_lock);
341 1.87.14.2 ad wakeup(&uvmexp.free);
342 1.87.14.2 ad mutex_exit(&uvm_pagedaemon_lock);
343 1.87.14.2 ad }
344 1.87.14.2 ad }
345 1.87.14.2 ad
346 1.87.14.2 ad /*
347 1.87.14.2 ad * uvmpd_trylockowner: trylock the page's owner.
348 1.87.14.2 ad *
349 1.87.14.2 ad * => called with pageq locked.
350 1.87.14.2 ad * => resolve orphaned O->A loaned page.
351 1.87.14.2 ad * => return the locked simplelock on success. otherwise, return NULL.
352 1.87.14.2 ad */
353 1.87.14.2 ad
354 1.87.14.2 ad struct simplelock *
355 1.87.14.2 ad uvmpd_trylockowner(struct vm_page *pg)
356 1.87.14.2 ad {
357 1.87.14.2 ad struct uvm_object *uobj = pg->uobject;
358 1.87.14.2 ad struct simplelock *slock;
359 1.87.14.2 ad
360 1.87.14.2 ad UVM_LOCK_ASSERT_PAGEQ();
361 1.87.14.2 ad if (uobj != NULL) {
362 1.87.14.2 ad slock = &uobj->vmobjlock;
363 1.87.14.2 ad } else {
364 1.87.14.2 ad struct vm_anon *anon = pg->uanon;
365 1.87.14.2 ad
366 1.87.14.2 ad KASSERT(anon != NULL);
367 1.87.14.2 ad slock = &anon->an_lock;
368 1.87.14.2 ad }
369 1.87.14.2 ad
370 1.87.14.2 ad if (!simple_lock_try(slock)) {
371 1.87.14.2 ad return NULL;
372 1.87.14.2 ad }
373 1.87.14.2 ad
374 1.87.14.2 ad if (uobj == NULL) {
375 1.87.14.2 ad
376 1.87.14.2 ad /*
377 1.87.14.2 ad * set PQ_ANON if it isn't set already.
378 1.87.14.2 ad */
379 1.87.14.2 ad
380 1.87.14.2 ad if ((pg->pqflags & PQ_ANON) == 0) {
381 1.87.14.2 ad KASSERT(pg->loan_count > 0);
382 1.87.14.2 ad pg->loan_count--;
383 1.87.14.2 ad pg->pqflags |= PQ_ANON;
384 1.87.14.2 ad /* anon now owns it */
385 1.87.14.2 ad }
386 1.87.14.2 ad }
387 1.87.14.2 ad
388 1.87.14.2 ad return slock;
389 1.87.14.2 ad }
390 1.87.14.2 ad
391 1.87.14.2 ad #if defined(VMSWAP)
392 1.87.14.2 ad struct swapcluster {
393 1.87.14.2 ad int swc_slot;
394 1.87.14.2 ad int swc_nallocated;
395 1.87.14.2 ad int swc_nused;
396 1.87.14.2 ad struct vm_page *swc_pages[howmany(MAXPHYS, MIN_PAGE_SIZE)];
397 1.87.14.2 ad };
398 1.87.14.2 ad
399 1.87.14.2 ad static void
400 1.87.14.2 ad swapcluster_init(struct swapcluster *swc)
401 1.87.14.2 ad {
402 1.87.14.2 ad
403 1.87.14.2 ad swc->swc_slot = 0;
404 1.87.14.2 ad }
405 1.87.14.2 ad
406 1.87.14.2 ad static int
407 1.87.14.2 ad swapcluster_allocslots(struct swapcluster *swc)
408 1.87.14.2 ad {
409 1.87.14.2 ad int slot;
410 1.87.14.2 ad int npages;
411 1.87.14.2 ad
412 1.87.14.2 ad if (swc->swc_slot != 0) {
413 1.87.14.2 ad return 0;
414 1.87.14.2 ad }
415 1.87.14.2 ad
416 1.87.14.2 ad /* Even with strange MAXPHYS, the shift
417 1.87.14.2 ad implicitly rounds down to a page. */
418 1.87.14.2 ad npages = MAXPHYS >> PAGE_SHIFT;
419 1.87.14.2 ad slot = uvm_swap_alloc(&npages, true);
420 1.87.14.2 ad if (slot == 0) {
421 1.87.14.2 ad return ENOMEM;
422 1.87.14.2 ad }
423 1.87.14.2 ad swc->swc_slot = slot;
424 1.87.14.2 ad swc->swc_nallocated = npages;
425 1.87.14.2 ad swc->swc_nused = 0;
426 1.87.14.2 ad
427 1.87.14.2 ad return 0;
428 1.87.14.2 ad }
429 1.87.14.2 ad
430 1.87.14.2 ad static int
431 1.87.14.2 ad swapcluster_add(struct swapcluster *swc, struct vm_page *pg)
432 1.87.14.2 ad {
433 1.87.14.2 ad int slot;
434 1.87.14.2 ad struct uvm_object *uobj;
435 1.87.14.2 ad
436 1.87.14.2 ad KASSERT(swc->swc_slot != 0);
437 1.87.14.2 ad KASSERT(swc->swc_nused < swc->swc_nallocated);
438 1.87.14.2 ad KASSERT((pg->pqflags & PQ_SWAPBACKED) != 0);
439 1.87.14.2 ad
440 1.87.14.2 ad slot = swc->swc_slot + swc->swc_nused;
441 1.87.14.2 ad uobj = pg->uobject;
442 1.87.14.2 ad if (uobj == NULL) {
443 1.87.14.2 ad LOCK_ASSERT(simple_lock_held(&pg->uanon->an_lock));
444 1.87.14.2 ad pg->uanon->an_swslot = slot;
445 1.87.14.2 ad } else {
446 1.87.14.2 ad int result;
447 1.87.14.2 ad
448 1.87.14.2 ad LOCK_ASSERT(simple_lock_held(&uobj->vmobjlock));
449 1.87.14.2 ad result = uao_set_swslot(uobj, pg->offset >> PAGE_SHIFT, slot);
450 1.87.14.2 ad if (result == -1) {
451 1.87.14.2 ad return ENOMEM;
452 1.87.14.2 ad }
453 1.87.14.2 ad }
454 1.87.14.2 ad swc->swc_pages[swc->swc_nused] = pg;
455 1.87.14.2 ad swc->swc_nused++;
456 1.87.14.2 ad
457 1.87.14.2 ad return 0;
458 1.87.14.2 ad }
459 1.87.14.2 ad
460 1.87.14.2 ad static void
461 1.87.14.2 ad swapcluster_flush(struct swapcluster *swc, bool now)
462 1.87.14.2 ad {
463 1.87.14.2 ad int slot;
464 1.87.14.2 ad int nused;
465 1.87.14.2 ad int nallocated;
466 1.87.14.2 ad int error;
467 1.87.14.2 ad
468 1.87.14.2 ad if (swc->swc_slot == 0) {
469 1.87.14.2 ad return;
470 1.87.14.2 ad }
471 1.87.14.2 ad KASSERT(swc->swc_nused <= swc->swc_nallocated);
472 1.87.14.2 ad
473 1.87.14.2 ad slot = swc->swc_slot;
474 1.87.14.2 ad nused = swc->swc_nused;
475 1.87.14.2 ad nallocated = swc->swc_nallocated;
476 1.87.14.2 ad
477 1.87.14.2 ad /*
478 1.87.14.2 ad * if this is the final pageout we could have a few
479 1.87.14.2 ad * unused swap blocks. if so, free them now.
480 1.87.14.2 ad */
481 1.87.14.2 ad
482 1.87.14.2 ad if (nused < nallocated) {
483 1.87.14.2 ad if (!now) {
484 1.87.14.2 ad return;
485 1.87.14.2 ad }
486 1.87.14.2 ad uvm_swap_free(slot + nused, nallocated - nused);
487 1.87.14.2 ad }
488 1.87.14.2 ad
489 1.87.14.2 ad /*
490 1.87.14.2 ad * now start the pageout.
491 1.87.14.2 ad */
492 1.87.14.2 ad
493 1.87.14.2 ad uvmexp.pdpageouts++;
494 1.87.14.2 ad error = uvm_swap_put(slot, swc->swc_pages, nused, 0);
495 1.87.14.2 ad KASSERT(error == 0);
496 1.87.14.2 ad
497 1.87.14.2 ad /*
498 1.87.14.2 ad * zero swslot to indicate that we are
499 1.87.14.2 ad * no longer building a swap-backed cluster.
500 1.87.14.2 ad */
501 1.87.14.2 ad
502 1.87.14.2 ad swc->swc_slot = 0;
503 1.87.14.2 ad }
504 1.87.14.2 ad
505 1.87.14.2 ad /*
506 1.87.14.2 ad * uvmpd_dropswap: free any swap allocated to this page.
507 1.87.14.2 ad *
508 1.87.14.2 ad * => called with owner locked.
509 1.87.14.2 ad * => return true if a page had an associated slot.
510 1.87.14.2 ad */
511 1.87.14.2 ad
512 1.87.14.2 ad static bool
513 1.87.14.2 ad uvmpd_dropswap(struct vm_page *pg)
514 1.87.14.2 ad {
515 1.87.14.2 ad bool result = false;
516 1.87.14.2 ad struct vm_anon *anon = pg->uanon;
517 1.87.14.2 ad
518 1.87.14.2 ad if ((pg->pqflags & PQ_ANON) && anon->an_swslot) {
519 1.87.14.2 ad uvm_swap_free(anon->an_swslot, 1);
520 1.87.14.2 ad anon->an_swslot = 0;
521 1.87.14.2 ad pg->flags &= ~PG_CLEAN;
522 1.87.14.2 ad result = true;
523 1.87.14.2 ad } else if (pg->pqflags & PQ_AOBJ) {
524 1.87.14.2 ad int slot = uao_set_swslot(pg->uobject,
525 1.87.14.2 ad pg->offset >> PAGE_SHIFT, 0);
526 1.87.14.2 ad if (slot) {
527 1.87.14.2 ad uvm_swap_free(slot, 1);
528 1.87.14.2 ad pg->flags &= ~PG_CLEAN;
529 1.87.14.2 ad result = true;
530 1.87.14.2 ad }
531 1.87.14.2 ad }
532 1.87.14.2 ad
533 1.87.14.2 ad return result;
534 1.87.14.2 ad }
535 1.87.14.2 ad
536 1.87.14.2 ad /*
537 1.87.14.2 ad * uvmpd_trydropswap: try to free any swap allocated to this page.
538 1.87.14.2 ad *
539 1.87.14.2 ad * => return true if a slot is successfully freed.
540 1.87.14.2 ad */
541 1.87.14.2 ad
542 1.87.14.2 ad bool
543 1.87.14.2 ad uvmpd_trydropswap(struct vm_page *pg)
544 1.87.14.2 ad {
545 1.87.14.2 ad struct simplelock *slock;
546 1.87.14.2 ad bool result;
547 1.87.14.2 ad
548 1.87.14.2 ad if ((pg->flags & PG_BUSY) != 0) {
549 1.87.14.2 ad return false;
550 1.87.14.2 ad }
551 1.87.14.2 ad
552 1.87.14.2 ad /*
553 1.87.14.2 ad * lock the page's owner.
554 1.87.14.2 ad */
555 1.87.14.2 ad
556 1.87.14.2 ad slock = uvmpd_trylockowner(pg);
557 1.87.14.2 ad if (slock == NULL) {
558 1.87.14.2 ad return false;
559 1.87.14.2 ad }
560 1.87.14.2 ad
561 1.87.14.2 ad /*
562 1.87.14.2 ad * skip this page if it's busy.
563 1.87.14.2 ad */
564 1.87.14.2 ad
565 1.87.14.2 ad if ((pg->flags & PG_BUSY) != 0) {
566 1.87.14.2 ad simple_unlock(slock);
567 1.87.14.2 ad return false;
568 1.87.14.2 ad }
569 1.87.14.2 ad
570 1.87.14.2 ad result = uvmpd_dropswap(pg);
571 1.87.14.2 ad
572 1.87.14.2 ad simple_unlock(slock);
573 1.87.14.2 ad
574 1.87.14.2 ad return result;
575 1.87.14.2 ad }
576 1.87.14.2 ad
577 1.87.14.2 ad #endif /* defined(VMSWAP) */
578 1.87.14.2 ad
579 1.87.14.2 ad /*
580 1.87.14.2 ad * uvmpd_scan_queue: scan an replace candidate list for pages
581 1.87.14.2 ad * to clean or free.
582 1.87.14.2 ad *
583 1.87.14.2 ad * => called with page queues locked
584 1.87.14.2 ad * => we work on meeting our free target by converting inactive pages
585 1.87.14.2 ad * into free pages.
586 1.87.14.2 ad * => we handle the building of swap-backed clusters
587 1.87.14.2 ad */
588 1.87.14.2 ad
589 1.87.14.2 ad static void
590 1.87.14.2 ad uvmpd_scan_queue(void)
591 1.87.14.2 ad {
592 1.87.14.2 ad struct vm_page *p;
593 1.87.14.2 ad struct uvm_object *uobj;
594 1.87.14.2 ad struct vm_anon *anon;
595 1.87.14.2 ad #if defined(VMSWAP)
596 1.87.14.2 ad struct swapcluster swc;
597 1.87.14.2 ad #endif /* defined(VMSWAP) */
598 1.87.14.2 ad int dirtyreacts;
599 1.87.14.2 ad struct simplelock *slock;
600 1.87.14.2 ad UVMHIST_FUNC("uvmpd_scan_queue"); UVMHIST_CALLED(pdhist);
601 1.87.14.2 ad
602 1.87.14.2 ad /*
603 1.87.14.2 ad * swslot is non-zero if we are building a swap cluster. we want
604 1.87.14.2 ad * to stay in the loop while we have a page to scan or we have
605 1.87.14.2 ad * a swap-cluster to build.
606 1.87.14.2 ad */
607 1.87.14.2 ad
608 1.87.14.2 ad #if defined(VMSWAP)
609 1.87.14.2 ad swapcluster_init(&swc);
610 1.87.14.2 ad #endif /* defined(VMSWAP) */
611 1.87.14.2 ad
612 1.87.14.2 ad dirtyreacts = 0;
613 1.87.14.2 ad uvmpdpol_scaninit();
614 1.87.14.2 ad
615 1.87.14.2 ad while (/* CONSTCOND */ 1) {
616 1.87.14.2 ad
617 1.87.14.2 ad /*
618 1.87.14.2 ad * see if we've met the free target.
619 1.87.14.2 ad */
620 1.87.14.2 ad
621 1.87.14.2 ad if (uvmexp.free + uvmexp.paging >= uvmexp.freetarg << 2 ||
622 1.87.14.2 ad dirtyreacts == UVMPD_NUMDIRTYREACTS) {
623 1.87.14.2 ad UVMHIST_LOG(pdhist," met free target: "
624 1.87.14.2 ad "exit loop", 0, 0, 0, 0);
625 1.87.14.2 ad break;
626 1.87.14.2 ad }
627 1.87.14.2 ad
628 1.87.14.2 ad p = uvmpdpol_selectvictim();
629 1.87.14.2 ad if (p == NULL) {
630 1.87.14.2 ad break;
631 1.87.14.2 ad }
632 1.87.14.2 ad KASSERT(uvmpdpol_pageisqueued_p(p));
633 1.87.14.2 ad KASSERT(p->wire_count == 0);
634 1.87.14.2 ad
635 1.87.14.2 ad /*
636 1.87.14.2 ad * we are below target and have a new page to consider.
637 1.87.14.2 ad */
638 1.87.14.2 ad
639 1.87.14.2 ad anon = p->uanon;
640 1.87.14.2 ad uobj = p->uobject;
641 1.87.14.2 ad
642 1.87.14.2 ad /*
643 1.87.14.2 ad * first we attempt to lock the object that this page
644 1.87.14.2 ad * belongs to. if our attempt fails we skip on to
645 1.87.14.2 ad * the next page (no harm done). it is important to
646 1.87.14.2 ad * "try" locking the object as we are locking in the
647 1.87.14.2 ad * wrong order (pageq -> object) and we don't want to
648 1.87.14.2 ad * deadlock.
649 1.87.14.2 ad *
650 1.87.14.2 ad * the only time we expect to see an ownerless page
651 1.87.14.2 ad * (i.e. a page with no uobject and !PQ_ANON) is if an
652 1.87.14.2 ad * anon has loaned a page from a uvm_object and the
653 1.87.14.2 ad * uvm_object has dropped the ownership. in that
654 1.87.14.2 ad * case, the anon can "take over" the loaned page
655 1.87.14.2 ad * and make it its own.
656 1.87.14.2 ad */
657 1.87.14.2 ad
658 1.87.14.2 ad slock = uvmpd_trylockowner(p);
659 1.87.14.2 ad if (slock == NULL) {
660 1.87.14.2 ad continue;
661 1.87.14.2 ad }
662 1.87.14.2 ad if (p->flags & PG_BUSY) {
663 1.87.14.2 ad simple_unlock(slock);
664 1.87.14.2 ad uvmexp.pdbusy++;
665 1.87.14.2 ad continue;
666 1.87.14.2 ad }
667 1.87.14.2 ad
668 1.87.14.2 ad /* does the page belong to an object? */
669 1.87.14.2 ad if (uobj != NULL) {
670 1.87.14.2 ad uvmexp.pdobscan++;
671 1.87.14.2 ad } else {
672 1.87.14.2 ad #if defined(VMSWAP)
673 1.87.14.2 ad KASSERT(anon != NULL);
674 1.87.14.2 ad uvmexp.pdanscan++;
675 1.87.14.2 ad #else /* defined(VMSWAP) */
676 1.87.14.2 ad panic("%s: anon", __func__);
677 1.87.14.2 ad #endif /* defined(VMSWAP) */
678 1.87.14.2 ad }
679 1.87.14.2 ad
680 1.87.14.2 ad
681 1.87.14.2 ad /*
682 1.87.14.2 ad * we now have the object and the page queues locked.
683 1.87.14.2 ad * if the page is not swap-backed, call the object's
684 1.87.14.2 ad * pager to flush and free the page.
685 1.87.14.2 ad */
686 1.87.14.2 ad
687 1.87.14.2 ad #if defined(READAHEAD_STATS)
688 1.87.14.2 ad if ((p->pqflags & PQ_READAHEAD) != 0) {
689 1.87.14.2 ad p->pqflags &= ~PQ_READAHEAD;
690 1.87.14.2 ad uvm_ra_miss.ev_count++;
691 1.87.14.2 ad }
692 1.87.14.2 ad #endif /* defined(READAHEAD_STATS) */
693 1.87.14.2 ad
694 1.87.14.2 ad if ((p->pqflags & PQ_SWAPBACKED) == 0) {
695 1.87.14.2 ad KASSERT(uobj != NULL);
696 1.87.14.2 ad uvm_unlock_pageq();
697 1.87.14.2 ad (void) (uobj->pgops->pgo_put)(uobj, p->offset,
698 1.87.14.2 ad p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE);
699 1.87.14.2 ad uvm_lock_pageq();
700 1.87.14.2 ad continue;
701 1.87.14.2 ad }
702 1.87.14.2 ad
703 1.87.14.2 ad /*
704 1.87.14.2 ad * the page is swap-backed. remove all the permissions
705 1.87.14.2 ad * from the page so we can sync the modified info
706 1.87.14.2 ad * without any race conditions. if the page is clean
707 1.87.14.2 ad * we can free it now and continue.
708 1.87.14.2 ad */
709 1.87.14.2 ad
710 1.87.14.2 ad pmap_page_protect(p, VM_PROT_NONE);
711 1.87.14.2 ad if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
712 1.87.14.2 ad p->flags &= ~(PG_CLEAN);
713 1.87.14.2 ad }
714 1.87.14.2 ad if (p->flags & PG_CLEAN) {
715 1.87.14.2 ad int slot;
716 1.87.14.2 ad int pageidx;
717 1.87.14.2 ad
718 1.87.14.2 ad pageidx = p->offset >> PAGE_SHIFT;
719 1.87.14.2 ad uvm_pagefree(p);
720 1.87.14.2 ad uvmexp.pdfreed++;
721 1.87.14.2 ad
722 1.87.14.2 ad /*
723 1.87.14.2 ad * for anons, we need to remove the page
724 1.87.14.2 ad * from the anon ourselves. for aobjs,
725 1.87.14.2 ad * pagefree did that for us.
726 1.87.14.2 ad */
727 1.87.14.2 ad
728 1.87.14.2 ad if (anon) {
729 1.87.14.2 ad KASSERT(anon->an_swslot != 0);
730 1.87.14.2 ad anon->an_page = NULL;
731 1.87.14.2 ad slot = anon->an_swslot;
732 1.87.14.2 ad } else {
733 1.87.14.2 ad slot = uao_find_swslot(uobj, pageidx);
734 1.87.14.2 ad }
735 1.87.14.2 ad simple_unlock(slock);
736 1.87.14.2 ad
737 1.87.14.2 ad if (slot > 0) {
738 1.87.14.2 ad /* this page is now only in swap. */
739 1.87.14.2 ad mutex_enter(&uvm_swap_data_lock);
740 1.87.14.2 ad KASSERT(uvmexp.swpgonly < uvmexp.swpginuse);
741 1.87.14.2 ad uvmexp.swpgonly++;
742 1.87.14.2 ad mutex_exit(&uvm_swap_data_lock);
743 1.87.14.2 ad }
744 1.87.14.2 ad continue;
745 1.87.14.2 ad }
746 1.87.14.2 ad
747 1.87.14.2 ad #if defined(VMSWAP)
748 1.87.14.2 ad /*
749 1.87.14.2 ad * this page is dirty, skip it if we'll have met our
750 1.87.14.2 ad * free target when all the current pageouts complete.
751 1.87.14.2 ad */
752 1.87.14.2 ad
753 1.87.14.2 ad if (uvmexp.free + uvmexp.paging > uvmexp.freetarg << 2) {
754 1.87.14.2 ad simple_unlock(slock);
755 1.87.14.2 ad continue;
756 1.87.14.2 ad }
757 1.87.14.2 ad
758 1.87.14.2 ad /*
759 1.87.14.2 ad * free any swap space allocated to the page since
760 1.87.14.2 ad * we'll have to write it again with its new data.
761 1.87.14.2 ad */
762 1.87.14.2 ad
763 1.87.14.2 ad uvmpd_dropswap(p);
764 1.87.14.2 ad
765 1.87.14.2 ad /*
766 1.87.14.2 ad * if all pages in swap are only in swap,
767 1.87.14.2 ad * the swap space is full and we can't page out
768 1.87.14.2 ad * any more swap-backed pages. reactivate this page
769 1.87.14.2 ad * so that we eventually cycle all pages through
770 1.87.14.2 ad * the inactive queue.
771 1.87.14.2 ad */
772 1.87.14.2 ad
773 1.87.14.2 ad if (uvm_swapisfull()) {
774 1.87.14.2 ad dirtyreacts++;
775 1.87.14.2 ad uvm_pageactivate(p);
776 1.87.14.2 ad simple_unlock(slock);
777 1.87.14.2 ad continue;
778 1.87.14.2 ad }
779 1.87.14.2 ad
780 1.87.14.2 ad /*
781 1.87.14.2 ad * start new swap pageout cluster (if necessary).
782 1.87.14.2 ad */
783 1.87.14.2 ad
784 1.87.14.2 ad if (swapcluster_allocslots(&swc)) {
785 1.87.14.2 ad simple_unlock(slock);
786 1.87.14.2 ad dirtyreacts++; /* XXX */
787 1.87.14.2 ad continue;
788 1.87.14.2 ad }
789 1.87.14.2 ad
790 1.87.14.2 ad /*
791 1.87.14.2 ad * at this point, we're definitely going reuse this
792 1.87.14.2 ad * page. mark the page busy and delayed-free.
793 1.87.14.2 ad * we should remove the page from the page queues
794 1.87.14.2 ad * so we don't ever look at it again.
795 1.87.14.2 ad * adjust counters and such.
796 1.87.14.2 ad */
797 1.87.14.2 ad
798 1.87.14.2 ad p->flags |= PG_BUSY;
799 1.87.14.2 ad UVM_PAGE_OWN(p, "scan_queue");
800 1.87.14.2 ad
801 1.87.14.2 ad p->flags |= PG_PAGEOUT;
802 1.87.14.2 ad uvmexp.paging++;
803 1.87.14.2 ad uvm_pagedequeue(p);
804 1.87.14.2 ad
805 1.87.14.2 ad uvmexp.pgswapout++;
806 1.87.14.2 ad uvm_unlock_pageq();
807 1.87.14.2 ad
808 1.87.14.2 ad /*
809 1.87.14.2 ad * add the new page to the cluster.
810 1.87.14.2 ad */
811 1.87.14.2 ad
812 1.87.14.2 ad if (swapcluster_add(&swc, p)) {
813 1.87.14.2 ad p->flags &= ~(PG_BUSY|PG_PAGEOUT);
814 1.87.14.2 ad UVM_PAGE_OWN(p, NULL);
815 1.87.14.2 ad uvm_lock_pageq();
816 1.87.14.2 ad uvmexp.paging--;
817 1.87.14.2 ad dirtyreacts++;
818 1.87.14.2 ad uvm_pageactivate(p);
819 1.87.14.2 ad simple_unlock(slock);
820 1.87.14.2 ad continue;
821 1.87.14.2 ad }
822 1.87.14.2 ad simple_unlock(slock);
823 1.87.14.2 ad
824 1.87.14.2 ad swapcluster_flush(&swc, false);
825 1.87.14.2 ad uvm_lock_pageq();
826 1.87.14.2 ad
827 1.87.14.2 ad /*
828 1.87.14.2 ad * the pageout is in progress. bump counters and set up
829 1.87.14.2 ad * for the next loop.
830 1.87.14.2 ad */
831 1.87.14.2 ad
832 1.87.14.2 ad uvmexp.pdpending++;
833 1.87.14.2 ad
834 1.87.14.2 ad #else /* defined(VMSWAP) */
835 1.87.14.2 ad uvm_pageactivate(p);
836 1.87.14.2 ad simple_unlock(slock);
837 1.87.14.2 ad #endif /* defined(VMSWAP) */
838 1.87.14.2 ad }
839 1.87.14.2 ad
840 1.87.14.2 ad #if defined(VMSWAP)
841 1.87.14.2 ad uvm_unlock_pageq();
842 1.87.14.2 ad swapcluster_flush(&swc, true);
843 1.87.14.2 ad uvm_lock_pageq();
844 1.87.14.2 ad #endif /* defined(VMSWAP) */
845 1.87.14.2 ad }
846 1.87.14.2 ad
847 1.87.14.2 ad /*
848 1.87.14.2 ad * uvmpd_scan: scan the page queues and attempt to meet our targets.
849 1.87.14.2 ad *
850 1.87.14.2 ad * => called with pageq's locked
851 1.87.14.2 ad */
852 1.87.14.2 ad
853 1.87.14.2 ad static void
854 1.87.14.2 ad uvmpd_scan(void)
855 1.87.14.2 ad {
856 1.87.14.2 ad int swap_shortage, pages_freed;
857 1.87.14.2 ad UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
858 1.87.14.2 ad
859 1.87.14.2 ad uvmexp.pdrevs++;
860 1.87.14.2 ad
861 1.87.14.2 ad #ifndef __SWAP_BROKEN
862 1.87.14.2 ad
863 1.87.14.2 ad /*
864 1.87.14.2 ad * swap out some processes if we are below our free target.
865 1.87.14.2 ad * we need to unlock the page queues for this.
866 1.87.14.2 ad */
867 1.87.14.2 ad
868 1.87.14.2 ad if (uvmexp.free < uvmexp.freetarg && uvmexp.nswapdev != 0 &&
869 1.87.14.2 ad uvm.swapout_enabled) {
870 1.87.14.2 ad uvmexp.pdswout++;
871 1.87.14.2 ad UVMHIST_LOG(pdhist," free %d < target %d: swapout",
872 1.87.14.2 ad uvmexp.free, uvmexp.freetarg, 0, 0);
873 1.87.14.2 ad uvm_unlock_pageq();
874 1.87.14.2 ad uvm_swapout_threads();
875 1.87.14.2 ad uvm_lock_pageq();
876 1.87.14.2 ad
877 1.87.14.2 ad }
878 1.87.14.2 ad #endif
879 1.87.14.2 ad
880 1.87.14.2 ad /*
881 1.87.14.2 ad * now we want to work on meeting our targets. first we work on our
882 1.87.14.2 ad * free target by converting inactive pages into free pages. then
883 1.87.14.2 ad * we work on meeting our inactive target by converting active pages
884 1.87.14.2 ad * to inactive ones.
885 1.87.14.2 ad */
886 1.87.14.2 ad
887 1.87.14.2 ad UVMHIST_LOG(pdhist, " starting 'free' loop",0,0,0,0);
888 1.87.14.2 ad
889 1.87.14.2 ad pages_freed = uvmexp.pdfreed;
890 1.87.14.2 ad uvmpd_scan_queue();
891 1.87.14.2 ad pages_freed = uvmexp.pdfreed - pages_freed;
892 1.87.14.2 ad
893 1.87.14.2 ad /*
894 1.87.14.2 ad * detect if we're not going to be able to page anything out
895 1.87.14.2 ad * until we free some swap resources from active pages.
896 1.87.14.2 ad */
897 1.87.14.2 ad
898 1.87.14.2 ad swap_shortage = 0;
899 1.87.14.2 ad if (uvmexp.free < uvmexp.freetarg &&
900 1.87.14.2 ad uvmexp.swpginuse >= uvmexp.swpgavail &&
901 1.87.14.2 ad !uvm_swapisfull() &&
902 1.87.14.2 ad pages_freed == 0) {
903 1.87.14.2 ad swap_shortage = uvmexp.freetarg - uvmexp.free;
904 1.87.14.2 ad }
905 1.87.14.2 ad
906 1.87.14.2 ad uvmpdpol_balancequeue(swap_shortage);
907 1.87.14.2 ad }
908 1.87.14.2 ad
909 1.87.14.2 ad /*
910 1.87.14.2 ad * uvm_reclaimable: decide whether to wait for pagedaemon.
911 1.87.14.2 ad *
912 1.87.14.2 ad * => return true if it seems to be worth to do uvm_wait.
913 1.87.14.2 ad *
914 1.87.14.2 ad * XXX should be tunable.
915 1.87.14.2 ad * XXX should consider pools, etc?
916 1.87.14.2 ad */
917 1.87.14.2 ad
918 1.87.14.2 ad bool
919 1.87.14.2 ad uvm_reclaimable(void)
920 1.87.14.2 ad {
921 1.87.14.2 ad int filepages;
922 1.87.14.2 ad int active, inactive;
923 1.87.14.2 ad
924 1.87.14.2 ad /*
925 1.87.14.2 ad * if swap is not full, no problem.
926 1.87.14.2 ad */
927 1.87.14.2 ad
928 1.87.14.2 ad if (!uvm_swapisfull()) {
929 1.87.14.2 ad return true;
930 1.87.14.2 ad }
931 1.87.14.2 ad
932 1.87.14.2 ad /*
933 1.87.14.2 ad * file-backed pages can be reclaimed even when swap is full.
934 1.87.14.2 ad * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
935 1.87.14.2 ad *
936 1.87.14.2 ad * XXX assume the worst case, ie. all wired pages are file-backed.
937 1.87.14.2 ad *
938 1.87.14.2 ad * XXX should consider about other reclaimable memory.
939 1.87.14.2 ad * XXX ie. pools, traditional buffer cache.
940 1.87.14.2 ad */
941 1.87.14.2 ad
942 1.87.14.2 ad filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired;
943 1.87.14.2 ad uvm_estimatepageable(&active, &inactive);
944 1.87.14.2 ad if (filepages >= MIN((active + inactive) >> 4,
945 1.87.14.2 ad 5 * 1024 * 1024 >> PAGE_SHIFT)) {
946 1.87.14.2 ad return true;
947 1.87.14.2 ad }
948 1.87.14.2 ad
949 1.87.14.2 ad /*
950 1.87.14.2 ad * kill the process, fail allocation, etc..
951 1.87.14.2 ad */
952 1.87.14.2 ad
953 1.87.14.2 ad return false;
954 1.87.14.2 ad }
955 1.87.14.2 ad
956 1.87.14.2 ad void
957 1.87.14.2 ad uvm_estimatepageable(int *active, int *inactive)
958 1.87.14.2 ad {
959 1.87.14.2 ad
960 1.87.14.2 ad uvmpdpol_estimatepageable(active, inactive);
961 1.87.14.2 ad }
962