uvm_pdaemon.c revision 1.77 1 /* $NetBSD: uvm_pdaemon.c,v 1.77 2006/09/15 15:51:13 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.77 2006/09/15 15:51:13 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 #include <uvm/uvm_pdpolicy.h>
89
90 /*
91 * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedaemon will reactivate
92 * in a pass thru the inactive list when swap is full. the value should be
93 * "small"... if it's too large we'll cycle the active pages thru the inactive
94 * queue too quickly to for them to be referenced and avoid being freed.
95 */
96
97 #define UVMPD_NUMDIRTYREACTS 16
98
99
100 /*
101 * local prototypes
102 */
103
104 static void uvmpd_scan(void);
105 static void uvmpd_scan_queue(void);
106 static void uvmpd_tune(void);
107
108 /*
109 * XXX hack to avoid hangs when large processes fork.
110 */
111 int uvm_extrapages;
112
113 /*
114 * uvm_wait: wait (sleep) for the page daemon to free some pages
115 *
116 * => should be called with all locks released
117 * => should _not_ be called by the page daemon (to avoid deadlock)
118 */
119
120 void
121 uvm_wait(const char *wmsg)
122 {
123 int timo = 0;
124 int s = splbio();
125
126 /*
127 * check for page daemon going to sleep (waiting for itself)
128 */
129
130 if (curproc == uvm.pagedaemon_proc && uvmexp.paging == 0) {
131 /*
132 * now we have a problem: the pagedaemon wants to go to
133 * sleep until it frees more memory. but how can it
134 * free more memory if it is asleep? that is a deadlock.
135 * we have two options:
136 * [1] panic now
137 * [2] put a timeout on the sleep, thus causing the
138 * pagedaemon to only pause (rather than sleep forever)
139 *
140 * note that option [2] will only help us if we get lucky
141 * and some other process on the system breaks the deadlock
142 * by exiting or freeing memory (thus allowing the pagedaemon
143 * to continue). for now we panic if DEBUG is defined,
144 * otherwise we hope for the best with option [2] (better
145 * yet, this should never happen in the first place!).
146 */
147
148 printf("pagedaemon: deadlock detected!\n");
149 timo = hz >> 3; /* set timeout */
150 #if defined(DEBUG)
151 /* DEBUG: panic so we can debug it */
152 panic("pagedaemon deadlock");
153 #endif
154 }
155
156 simple_lock(&uvm.pagedaemon_lock);
157 wakeup(&uvm.pagedaemon); /* wake the daemon! */
158 UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvm.pagedaemon_lock, FALSE, wmsg,
159 timo);
160
161 splx(s);
162 }
163
164 /*
165 * uvm_kick_pdaemon: perform checks to determine if we need to
166 * give the pagedaemon a nudge, and do so if necessary.
167 */
168
169 void
170 uvm_kick_pdaemon(void)
171 {
172
173 if (uvmexp.free + uvmexp.paging < uvmexp.freemin ||
174 (uvmexp.free + uvmexp.paging < uvmexp.freetarg &&
175 uvmpdpol_needsscan_p())) {
176 wakeup(&uvm.pagedaemon);
177 }
178 }
179
180 /*
181 * uvmpd_tune: tune paging parameters
182 *
183 * => called when ever memory is added (or removed?) to the system
184 * => caller must call with page queues locked
185 */
186
187 static void
188 uvmpd_tune(void)
189 {
190 UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist);
191
192 uvmexp.freemin = uvmexp.npages / 20;
193
194 /* between 16k and 256k */
195 /* XXX: what are these values good for? */
196 uvmexp.freemin = MAX(uvmexp.freemin, (16*1024) >> PAGE_SHIFT);
197 uvmexp.freemin = MIN(uvmexp.freemin, (256*1024) >> PAGE_SHIFT);
198
199 /* Make sure there's always a user page free. */
200 if (uvmexp.freemin < uvmexp.reserve_kernel + 1)
201 uvmexp.freemin = uvmexp.reserve_kernel + 1;
202
203 uvmexp.freetarg = (uvmexp.freemin * 4) / 3;
204 if (uvmexp.freetarg <= uvmexp.freemin)
205 uvmexp.freetarg = uvmexp.freemin + 1;
206
207 uvmexp.freetarg += uvm_extrapages;
208 uvm_extrapages = 0;
209
210 uvmexp.wiredmax = uvmexp.npages / 3;
211 UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
212 uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
213 }
214
215 /*
216 * uvm_pageout: the main loop for the pagedaemon
217 */
218
219 void
220 uvm_pageout(void *arg)
221 {
222 int bufcnt, npages = 0;
223 int extrapages = 0;
224 UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist);
225
226 UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
227
228 /*
229 * ensure correct priority and set paging parameters...
230 */
231
232 uvm.pagedaemon_proc = curproc;
233 uvm_lock_pageq();
234 npages = uvmexp.npages;
235 uvmpd_tune();
236 uvm_unlock_pageq();
237
238 /*
239 * main loop
240 */
241
242 for (;;) {
243 simple_lock(&uvm.pagedaemon_lock);
244
245 UVMHIST_LOG(pdhist," <<SLEEPING>>",0,0,0,0);
246 UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
247 &uvm.pagedaemon_lock, FALSE, "pgdaemon", 0);
248 uvmexp.pdwoke++;
249 UVMHIST_LOG(pdhist," <<WOKE UP>>",0,0,0,0);
250
251 /*
252 * now lock page queues and recompute inactive count
253 */
254
255 uvm_lock_pageq();
256 if (npages != uvmexp.npages || extrapages != uvm_extrapages) {
257 npages = uvmexp.npages;
258 extrapages = uvm_extrapages;
259 uvmpd_tune();
260 }
261
262 uvmpdpol_tune();
263
264 /*
265 * Estimate a hint. Note that bufmem are returned to
266 * system only when entire pool page is empty.
267 */
268 bufcnt = uvmexp.freetarg - uvmexp.free;
269 if (bufcnt < 0)
270 bufcnt = 0;
271
272 UVMHIST_LOG(pdhist," free/ftarg=%d/%d",
273 uvmexp.free, uvmexp.freetarg, 0,0);
274
275 /*
276 * scan if needed
277 */
278
279 if (uvmexp.free + uvmexp.paging < uvmexp.freetarg ||
280 uvmpdpol_needsscan_p()) {
281 uvmpd_scan();
282 }
283
284 /*
285 * if there's any free memory to be had,
286 * wake up any waiters.
287 */
288
289 if (uvmexp.free > uvmexp.reserve_kernel ||
290 uvmexp.paging == 0) {
291 wakeup(&uvmexp.free);
292 }
293
294 /*
295 * scan done. unlock page queues (the only lock we are holding)
296 */
297
298 uvm_unlock_pageq();
299
300 buf_drain(bufcnt << PAGE_SHIFT);
301
302 /*
303 * drain pool resources now that we're not holding any locks
304 */
305
306 pool_drain(0);
307
308 /*
309 * free any cached u-areas we don't need
310 */
311 uvm_uarea_drain(TRUE);
312
313 }
314 /*NOTREACHED*/
315 }
316
317
318 /*
319 * uvm_aiodone_daemon: main loop for the aiodone daemon.
320 */
321
322 void
323 uvm_aiodone_daemon(void *arg)
324 {
325 int s, free;
326 struct buf *bp, *nbp;
327 UVMHIST_FUNC("uvm_aiodoned"); UVMHIST_CALLED(pdhist);
328
329 for (;;) {
330
331 /*
332 * carefully attempt to go to sleep (without losing "wakeups"!).
333 * we need splbio because we want to make sure the aio_done list
334 * is totally empty before we go to sleep.
335 */
336
337 s = splbio();
338 simple_lock(&uvm.aiodoned_lock);
339 if (TAILQ_FIRST(&uvm.aio_done) == NULL) {
340 UVMHIST_LOG(pdhist," <<SLEEPING>>",0,0,0,0);
341 UVM_UNLOCK_AND_WAIT(&uvm.aiodoned,
342 &uvm.aiodoned_lock, FALSE, "aiodoned", 0);
343 UVMHIST_LOG(pdhist," <<WOKE UP>>",0,0,0,0);
344
345 /* relock aiodoned_lock, still at splbio */
346 simple_lock(&uvm.aiodoned_lock);
347 }
348
349 /*
350 * check for done aio structures
351 */
352
353 bp = TAILQ_FIRST(&uvm.aio_done);
354 if (bp) {
355 TAILQ_INIT(&uvm.aio_done);
356 }
357
358 simple_unlock(&uvm.aiodoned_lock);
359 splx(s);
360
361 /*
362 * process each i/o that's done.
363 */
364
365 free = uvmexp.free;
366 while (bp != NULL) {
367 nbp = TAILQ_NEXT(bp, b_freelist);
368 (*bp->b_iodone)(bp);
369 bp = nbp;
370 }
371 if (free <= uvmexp.reserve_kernel) {
372 s = uvm_lock_fpageq();
373 wakeup(&uvm.pagedaemon);
374 uvm_unlock_fpageq(s);
375 } else {
376 simple_lock(&uvm.pagedaemon_lock);
377 wakeup(&uvmexp.free);
378 simple_unlock(&uvm.pagedaemon_lock);
379 }
380 }
381 }
382
383 /*
384 * uvmpd_trylockowner: trylock the page's owner.
385 *
386 * => called with pageq locked.
387 * => resolve orphaned O->A loaned page.
388 * => return the locked simplelock on success. otherwise, return NULL.
389 */
390
391 struct simplelock *
392 uvmpd_trylockowner(struct vm_page *pg)
393 {
394 struct uvm_object *uobj = pg->uobject;
395 struct simplelock *slock;
396
397 UVM_LOCK_ASSERT_PAGEQ();
398 if (uobj != NULL) {
399 slock = &uobj->vmobjlock;
400 } else {
401 struct vm_anon *anon = pg->uanon;
402
403 KASSERT(anon != NULL);
404 slock = &anon->an_lock;
405 }
406
407 if (!simple_lock_try(slock)) {
408 return NULL;
409 }
410
411 if (uobj == NULL) {
412
413 /*
414 * set PQ_ANON if it isn't set already.
415 */
416
417 if ((pg->pqflags & PQ_ANON) == 0) {
418 KASSERT(pg->loan_count > 0);
419 pg->loan_count--;
420 pg->pqflags |= PQ_ANON;
421 /* anon now owns it */
422 }
423 }
424
425 return slock;
426 }
427
428 #if defined(VMSWAP)
429 struct swapcluster {
430 int swc_slot;
431 int swc_nallocated;
432 int swc_nused;
433 struct vm_page *swc_pages[howmany(MAXPHYS, MIN_PAGE_SIZE)];
434 };
435
436 static void
437 swapcluster_init(struct swapcluster *swc)
438 {
439
440 swc->swc_slot = 0;
441 }
442
443 static int
444 swapcluster_allocslots(struct swapcluster *swc)
445 {
446 int slot;
447 int npages;
448
449 if (swc->swc_slot != 0) {
450 return 0;
451 }
452
453 /* Even with strange MAXPHYS, the shift
454 implicitly rounds down to a page. */
455 npages = MAXPHYS >> PAGE_SHIFT;
456 slot = uvm_swap_alloc(&npages, TRUE);
457 if (slot == 0) {
458 return ENOMEM;
459 }
460 swc->swc_slot = slot;
461 swc->swc_nallocated = npages;
462 swc->swc_nused = 0;
463
464 return 0;
465 }
466
467 static int
468 swapcluster_add(struct swapcluster *swc, struct vm_page *pg)
469 {
470 int slot;
471 struct uvm_object *uobj;
472
473 KASSERT(swc->swc_slot != 0);
474 KASSERT(swc->swc_nused < swc->swc_nallocated);
475 KASSERT((pg->pqflags & PQ_SWAPBACKED) != 0);
476
477 slot = swc->swc_slot + swc->swc_nused;
478 uobj = pg->uobject;
479 if (uobj == NULL) {
480 LOCK_ASSERT(simple_lock_held(&pg->uanon->an_lock));
481 pg->uanon->an_swslot = slot;
482 } else {
483 int result;
484
485 LOCK_ASSERT(simple_lock_held(&uobj->vmobjlock));
486 result = uao_set_swslot(uobj, pg->offset >> PAGE_SHIFT, slot);
487 if (result == -1) {
488 return ENOMEM;
489 }
490 }
491 swc->swc_pages[swc->swc_nused] = pg;
492 swc->swc_nused++;
493
494 return 0;
495 }
496
497 static void
498 swapcluster_flush(struct swapcluster *swc, boolean_t now)
499 {
500 int slot;
501 int nused;
502 int nallocated;
503 int error;
504
505 if (swc->swc_slot == 0) {
506 return;
507 }
508 KASSERT(swc->swc_nused <= swc->swc_nallocated);
509
510 slot = swc->swc_slot;
511 nused = swc->swc_nused;
512 nallocated = swc->swc_nallocated;
513
514 /*
515 * if this is the final pageout we could have a few
516 * unused swap blocks. if so, free them now.
517 */
518
519 if (nused < nallocated) {
520 if (!now) {
521 return;
522 }
523 uvm_swap_free(slot + nused, nallocated - nused);
524 }
525
526 /*
527 * now start the pageout.
528 */
529
530 uvmexp.pdpageouts++;
531 error = uvm_swap_put(slot, swc->swc_pages, nused, 0);
532 KASSERT(error == 0);
533
534 /*
535 * zero swslot to indicate that we are
536 * no longer building a swap-backed cluster.
537 */
538
539 swc->swc_slot = 0;
540 }
541
542 /*
543 * uvmpd_dropswap: free any swap allocated to this page.
544 *
545 * => called with owner locked.
546 * => return TRUE if a page had an associated slot.
547 */
548
549 static boolean_t
550 uvmpd_dropswap(struct vm_page *pg)
551 {
552 boolean_t result = FALSE;
553 struct vm_anon *anon = pg->uanon;
554
555 if ((pg->pqflags & PQ_ANON) && anon->an_swslot) {
556 uvm_swap_free(anon->an_swslot, 1);
557 anon->an_swslot = 0;
558 pg->flags &= ~PG_CLEAN;
559 result = TRUE;
560 } else if (pg->pqflags & PQ_AOBJ) {
561 int slot = uao_set_swslot(pg->uobject,
562 pg->offset >> PAGE_SHIFT, 0);
563 if (slot) {
564 uvm_swap_free(slot, 1);
565 pg->flags &= ~PG_CLEAN;
566 result = TRUE;
567 }
568 }
569
570 return result;
571 }
572
573 /*
574 * uvmpd_trydropswap: try to free any swap allocated to this page.
575 *
576 * => return TRUE if a slot is successfully freed.
577 */
578
579 boolean_t
580 uvmpd_trydropswap(struct vm_page *pg)
581 {
582 struct simplelock *slock;
583 boolean_t result;
584
585 if ((pg->flags & PG_BUSY) != 0) {
586 return FALSE;
587 }
588
589 /*
590 * lock the page's owner.
591 */
592
593 slock = uvmpd_trylockowner(pg);
594 if (slock == NULL) {
595 return FALSE;
596 }
597
598 /*
599 * skip this page if it's busy.
600 */
601
602 if ((pg->flags & PG_BUSY) != 0) {
603 simple_unlock(slock);
604 return FALSE;
605 }
606
607 result = uvmpd_dropswap(pg);
608
609 simple_unlock(slock);
610
611 return result;
612 }
613
614 #endif /* defined(VMSWAP) */
615
616 /*
617 * uvmpd_scan_queue: scan an replace candidate list for pages
618 * to clean or free.
619 *
620 * => called with page queues locked
621 * => we work on meeting our free target by converting inactive pages
622 * into free pages.
623 * => we handle the building of swap-backed clusters
624 */
625
626 static void
627 uvmpd_scan_queue(void)
628 {
629 struct vm_page *p;
630 struct uvm_object *uobj;
631 struct vm_anon *anon;
632 #if defined(VMSWAP)
633 struct swapcluster swc;
634 #endif /* defined(VMSWAP) */
635 int dirtyreacts;
636 struct simplelock *slock;
637 UVMHIST_FUNC("uvmpd_scan_queue"); UVMHIST_CALLED(pdhist);
638
639 /*
640 * swslot is non-zero if we are building a swap cluster. we want
641 * to stay in the loop while we have a page to scan or we have
642 * a swap-cluster to build.
643 */
644
645 #if defined(VMSWAP)
646 swapcluster_init(&swc);
647 #endif /* defined(VMSWAP) */
648
649 dirtyreacts = 0;
650 uvmpdpol_scaninit();
651
652 while (/* CONSTCOND */ 1) {
653
654 /*
655 * see if we've met the free target.
656 */
657
658 if (uvmexp.free + uvmexp.paging >= uvmexp.freetarg << 2 ||
659 dirtyreacts == UVMPD_NUMDIRTYREACTS) {
660 UVMHIST_LOG(pdhist," met free target: "
661 "exit loop", 0, 0, 0, 0);
662 break;
663 }
664
665 p = uvmpdpol_selectvictim();
666 if (p == NULL) {
667 break;
668 }
669 KASSERT(uvmpdpol_pageisqueued_p(p));
670 KASSERT(p->wire_count == 0);
671
672 /*
673 * we are below target and have a new page to consider.
674 */
675
676 anon = p->uanon;
677 uobj = p->uobject;
678
679 /*
680 * first we attempt to lock the object that this page
681 * belongs to. if our attempt fails we skip on to
682 * the next page (no harm done). it is important to
683 * "try" locking the object as we are locking in the
684 * wrong order (pageq -> object) and we don't want to
685 * deadlock.
686 *
687 * the only time we expect to see an ownerless page
688 * (i.e. a page with no uobject and !PQ_ANON) is if an
689 * anon has loaned a page from a uvm_object and the
690 * uvm_object has dropped the ownership. in that
691 * case, the anon can "take over" the loaned page
692 * and make it its own.
693 */
694
695 slock = uvmpd_trylockowner(p);
696 if (slock == NULL) {
697 continue;
698 }
699 if (p->flags & PG_BUSY) {
700 simple_unlock(slock);
701 uvmexp.pdbusy++;
702 continue;
703 }
704
705 /* does the page belong to an object? */
706 if (uobj != NULL) {
707 uvmexp.pdobscan++;
708 } else {
709 #if defined(VMSWAP)
710 KASSERT(anon != NULL);
711 uvmexp.pdanscan++;
712 #else /* defined(VMSWAP) */
713 panic("%s: anon", __func__);
714 #endif /* defined(VMSWAP) */
715 }
716
717
718 /*
719 * we now have the object and the page queues locked.
720 * if the page is not swap-backed, call the object's
721 * pager to flush and free the page.
722 */
723
724 #if defined(READAHEAD_STATS)
725 if ((p->pqflags & PQ_READAHEAD) != 0) {
726 p->pqflags &= ~PQ_READAHEAD;
727 uvm_ra_miss.ev_count++;
728 }
729 #endif /* defined(READAHEAD_STATS) */
730
731 if ((p->pqflags & PQ_SWAPBACKED) == 0) {
732 uvm_unlock_pageq();
733 (void) (uobj->pgops->pgo_put)(uobj, p->offset,
734 p->offset + PAGE_SIZE, PGO_CLEANIT|PGO_FREE);
735 uvm_lock_pageq();
736 continue;
737 }
738
739 /*
740 * the page is swap-backed. remove all the permissions
741 * from the page so we can sync the modified info
742 * without any race conditions. if the page is clean
743 * we can free it now and continue.
744 */
745
746 pmap_page_protect(p, VM_PROT_NONE);
747 if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
748 p->flags &= ~(PG_CLEAN);
749 }
750 if (p->flags & PG_CLEAN) {
751 int slot;
752 int pageidx;
753
754 pageidx = p->offset >> PAGE_SHIFT;
755 uvm_pagefree(p);
756 uvmexp.pdfreed++;
757
758 /*
759 * for anons, we need to remove the page
760 * from the anon ourselves. for aobjs,
761 * pagefree did that for us.
762 */
763
764 if (anon) {
765 KASSERT(anon->an_swslot != 0);
766 anon->an_page = NULL;
767 slot = anon->an_swslot;
768 } else {
769 slot = uao_find_swslot(uobj, pageidx);
770 }
771 simple_unlock(slock);
772
773 if (slot > 0) {
774 /* this page is now only in swap. */
775 simple_lock(&uvm.swap_data_lock);
776 KASSERT(uvmexp.swpgonly < uvmexp.swpginuse);
777 uvmexp.swpgonly++;
778 simple_unlock(&uvm.swap_data_lock);
779 }
780 continue;
781 }
782
783 #if defined(VMSWAP)
784 /*
785 * this page is dirty, skip it if we'll have met our
786 * free target when all the current pageouts complete.
787 */
788
789 if (uvmexp.free + uvmexp.paging > uvmexp.freetarg << 2) {
790 simple_unlock(slock);
791 continue;
792 }
793
794 /*
795 * free any swap space allocated to the page since
796 * we'll have to write it again with its new data.
797 */
798
799 uvmpd_dropswap(p);
800
801 /*
802 * if all pages in swap are only in swap,
803 * the swap space is full and we can't page out
804 * any more swap-backed pages. reactivate this page
805 * so that we eventually cycle all pages through
806 * the inactive queue.
807 */
808
809 if (uvm_swapisfull()) {
810 dirtyreacts++;
811 uvm_pageactivate(p);
812 simple_unlock(slock);
813 continue;
814 }
815
816 /*
817 * start new swap pageout cluster (if necessary).
818 */
819
820 if (swapcluster_allocslots(&swc)) {
821 simple_unlock(slock);
822 dirtyreacts++; /* XXX */
823 continue;
824 }
825
826 /*
827 * at this point, we're definitely going reuse this
828 * page. mark the page busy and delayed-free.
829 * we should remove the page from the page queues
830 * so we don't ever look at it again.
831 * adjust counters and such.
832 */
833
834 p->flags |= PG_BUSY;
835 UVM_PAGE_OWN(p, "scan_queue");
836
837 p->flags |= PG_PAGEOUT;
838 uvmexp.paging++;
839 uvm_pagedequeue(p);
840
841 uvmexp.pgswapout++;
842 uvm_unlock_pageq();
843
844 /*
845 * add the new page to the cluster.
846 */
847
848 if (swapcluster_add(&swc, p)) {
849 p->flags &= ~(PG_BUSY|PG_PAGEOUT);
850 UVM_PAGE_OWN(p, NULL);
851 uvm_lock_pageq();
852 uvmexp.paging--;
853 dirtyreacts++;
854 uvm_pageactivate(p);
855 simple_unlock(slock);
856 continue;
857 }
858 simple_unlock(slock);
859
860 swapcluster_flush(&swc, FALSE);
861 uvm_lock_pageq();
862
863 /*
864 * the pageout is in progress. bump counters and set up
865 * for the next loop.
866 */
867
868 uvmexp.pdpending++;
869
870 #else /* defined(VMSWAP) */
871 uvm_pageactivate(p);
872 simple_unlock(slock);
873 #endif /* defined(VMSWAP) */
874 }
875
876 #if defined(VMSWAP)
877 uvm_unlock_pageq();
878 swapcluster_flush(&swc, TRUE);
879 uvm_lock_pageq();
880 #endif /* defined(VMSWAP) */
881 }
882
883 /*
884 * uvmpd_scan: scan the page queues and attempt to meet our targets.
885 *
886 * => called with pageq's locked
887 */
888
889 static void
890 uvmpd_scan(void)
891 {
892 int swap_shortage, pages_freed;
893 UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
894
895 uvmexp.pdrevs++;
896
897 #ifndef __SWAP_BROKEN
898
899 /*
900 * swap out some processes if we are below our free target.
901 * we need to unlock the page queues for this.
902 */
903
904 if (uvmexp.free < uvmexp.freetarg && uvmexp.nswapdev != 0) {
905 uvmexp.pdswout++;
906 UVMHIST_LOG(pdhist," free %d < target %d: swapout",
907 uvmexp.free, uvmexp.freetarg, 0, 0);
908 uvm_unlock_pageq();
909 uvm_swapout_threads();
910 uvm_lock_pageq();
911
912 }
913 #endif
914
915 /*
916 * now we want to work on meeting our targets. first we work on our
917 * free target by converting inactive pages into free pages. then
918 * we work on meeting our inactive target by converting active pages
919 * to inactive ones.
920 */
921
922 UVMHIST_LOG(pdhist, " starting 'free' loop",0,0,0,0);
923
924 pages_freed = uvmexp.pdfreed;
925 uvmpd_scan_queue();
926 pages_freed = uvmexp.pdfreed - pages_freed;
927
928 /*
929 * detect if we're not going to be able to page anything out
930 * until we free some swap resources from active pages.
931 */
932
933 swap_shortage = 0;
934 if (uvmexp.free < uvmexp.freetarg &&
935 uvmexp.swpginuse >= uvmexp.swpgavail &&
936 !uvm_swapisfull() &&
937 pages_freed == 0) {
938 swap_shortage = uvmexp.freetarg - uvmexp.free;
939 }
940
941 uvmpdpol_balancequeue(swap_shortage);
942 }
943
944 /*
945 * uvm_reclaimable: decide whether to wait for pagedaemon.
946 *
947 * => return TRUE if it seems to be worth to do uvm_wait.
948 *
949 * XXX should be tunable.
950 * XXX should consider pools, etc?
951 */
952
953 boolean_t
954 uvm_reclaimable(void)
955 {
956 int filepages;
957 int active, inactive;
958
959 /*
960 * if swap is not full, no problem.
961 */
962
963 if (!uvm_swapisfull()) {
964 return TRUE;
965 }
966
967 /*
968 * file-backed pages can be reclaimed even when swap is full.
969 * if we have more than 1/16 of pageable memory or 5MB, try to reclaim.
970 *
971 * XXX assume the worst case, ie. all wired pages are file-backed.
972 *
973 * XXX should consider about other reclaimable memory.
974 * XXX ie. pools, traditional buffer cache.
975 */
976
977 filepages = uvmexp.filepages + uvmexp.execpages - uvmexp.wired;
978 uvm_estimatepageable(&active, &inactive);
979 if (filepages >= MIN((active + inactive) >> 4,
980 5 * 1024 * 1024 >> PAGE_SHIFT)) {
981 return TRUE;
982 }
983
984 /*
985 * kill the process, fail allocation, etc..
986 */
987
988 return FALSE;
989 }
990
991 void
992 uvm_estimatepageable(int *active, int *inactive)
993 {
994
995 uvmpdpol_estimatepageable(active, inactive);
996 }
997