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