uvm_pdaemon.c revision 1.40 1 /* $NetBSD: uvm_pdaemon.c,v 1.40 2001/11/06 06:28:22 simonb 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 #include "opt_uvmhist.h"
70
71 /*
72 * uvm_pdaemon.c: the page daemon
73 */
74
75 #include <sys/param.h>
76 #include <sys/proc.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/pool.h>
80 #include <sys/buf.h>
81 #include <sys/vnode.h>
82
83 #include <uvm/uvm.h>
84
85 /*
86 * UVMPD_NUMDIRTYREACTS is how many dirty pages the pagedeamon will reactivate
87 * in a pass thru the inactive list when swap is full. the value should be
88 * "small"... if it's too large we'll cycle the active pages thru the inactive
89 * queue too quickly to for them to be referenced and avoid being freed.
90 */
91
92 #define UVMPD_NUMDIRTYREACTS 16
93
94
95 /*
96 * local prototypes
97 */
98
99 void uvmpd_scan __P((void));
100 boolean_t uvmpd_scan_inactive __P((struct pglist *));
101 void uvmpd_tune __P((void));
102
103 /*
104 * uvm_wait: wait (sleep) for the page daemon to free some pages
105 *
106 * => should be called with all locks released
107 * => should _not_ be called by the page daemon (to avoid deadlock)
108 */
109
110 void
111 uvm_wait(wmsg)
112 const char *wmsg;
113 {
114 int timo = 0;
115 int s = splbio();
116
117 /*
118 * check for page daemon going to sleep (waiting for itself)
119 */
120
121 if (curproc == uvm.pagedaemon_proc && uvmexp.paging == 0) {
122 /*
123 * now we have a problem: the pagedaemon wants to go to
124 * sleep until it frees more memory. but how can it
125 * free more memory if it is asleep? that is a deadlock.
126 * we have two options:
127 * [1] panic now
128 * [2] put a timeout on the sleep, thus causing the
129 * pagedaemon to only pause (rather than sleep forever)
130 *
131 * note that option [2] will only help us if we get lucky
132 * and some other process on the system breaks the deadlock
133 * by exiting or freeing memory (thus allowing the pagedaemon
134 * to continue). for now we panic if DEBUG is defined,
135 * otherwise we hope for the best with option [2] (better
136 * yet, this should never happen in the first place!).
137 */
138
139 printf("pagedaemon: deadlock detected!\n");
140 timo = hz >> 3; /* set timeout */
141 #if defined(DEBUG)
142 /* DEBUG: panic so we can debug it */
143 panic("pagedaemon deadlock");
144 #endif
145 }
146
147 simple_lock(&uvm.pagedaemon_lock);
148 wakeup(&uvm.pagedaemon); /* wake the daemon! */
149 UVM_UNLOCK_AND_WAIT(&uvmexp.free, &uvm.pagedaemon_lock, FALSE, wmsg,
150 timo);
151
152 splx(s);
153 }
154
155
156 /*
157 * uvmpd_tune: tune paging parameters
158 *
159 * => called when ever memory is added (or removed?) to the system
160 * => caller must call with page queues locked
161 */
162
163 void
164 uvmpd_tune(void)
165 {
166 UVMHIST_FUNC("uvmpd_tune"); UVMHIST_CALLED(pdhist);
167
168 uvmexp.freemin = uvmexp.npages / 20;
169
170 /* between 16k and 256k */
171 /* XXX: what are these values good for? */
172 uvmexp.freemin = MAX(uvmexp.freemin, (16*1024) >> PAGE_SHIFT);
173 uvmexp.freemin = MIN(uvmexp.freemin, (256*1024) >> PAGE_SHIFT);
174
175 /* Make sure there's always a user page free. */
176 if (uvmexp.freemin < uvmexp.reserve_kernel + 1)
177 uvmexp.freemin = uvmexp.reserve_kernel + 1;
178
179 uvmexp.freetarg = (uvmexp.freemin * 4) / 3;
180 if (uvmexp.freetarg <= uvmexp.freemin)
181 uvmexp.freetarg = uvmexp.freemin + 1;
182
183 /* uvmexp.inactarg: computed in main daemon loop */
184
185 uvmexp.wiredmax = uvmexp.npages / 3;
186 UVMHIST_LOG(pdhist, "<- done, freemin=%d, freetarg=%d, wiredmax=%d",
187 uvmexp.freemin, uvmexp.freetarg, uvmexp.wiredmax, 0);
188 }
189
190 /*
191 * uvm_pageout: the main loop for the pagedaemon
192 */
193
194 void
195 uvm_pageout(void *arg)
196 {
197 int npages = 0;
198 UVMHIST_FUNC("uvm_pageout"); UVMHIST_CALLED(pdhist);
199
200 UVMHIST_LOG(pdhist,"<starting uvm pagedaemon>", 0, 0, 0, 0);
201
202 /*
203 * ensure correct priority and set paging parameters...
204 */
205
206 uvm.pagedaemon_proc = curproc;
207 uvm_lock_pageq();
208 npages = uvmexp.npages;
209 uvmpd_tune();
210 uvm_unlock_pageq();
211
212 /*
213 * main loop
214 */
215
216 for (;;) {
217 simple_lock(&uvm.pagedaemon_lock);
218
219 UVMHIST_LOG(pdhist," <<SLEEPING>>",0,0,0,0);
220 UVM_UNLOCK_AND_WAIT(&uvm.pagedaemon,
221 &uvm.pagedaemon_lock, FALSE, "pgdaemon", 0);
222 uvmexp.pdwoke++;
223 UVMHIST_LOG(pdhist," <<WOKE UP>>",0,0,0,0);
224
225 /*
226 * now lock page queues and recompute inactive count
227 */
228
229 uvm_lock_pageq();
230 if (npages != uvmexp.npages) { /* check for new pages? */
231 npages = uvmexp.npages;
232 uvmpd_tune();
233 }
234
235 uvmexp.inactarg = (uvmexp.active + uvmexp.inactive) / 3;
236 if (uvmexp.inactarg <= uvmexp.freetarg) {
237 uvmexp.inactarg = uvmexp.freetarg + 1;
238 }
239
240 UVMHIST_LOG(pdhist," free/ftarg=%d/%d, inact/itarg=%d/%d",
241 uvmexp.free, uvmexp.freetarg, uvmexp.inactive,
242 uvmexp.inactarg);
243
244 /*
245 * scan if needed
246 */
247
248 if (uvmexp.free + uvmexp.paging < uvmexp.freetarg ||
249 uvmexp.inactive < uvmexp.inactarg) {
250 uvmpd_scan();
251 }
252
253 /*
254 * if there's any free memory to be had,
255 * wake up any waiters.
256 */
257
258 if (uvmexp.free > uvmexp.reserve_kernel ||
259 uvmexp.paging == 0) {
260 wakeup(&uvmexp.free);
261 }
262
263 /*
264 * scan done. unlock page queues (the only lock we are holding)
265 */
266
267 uvm_unlock_pageq();
268
269 /*
270 * drain pool resources now that we're not holding any locks
271 */
272
273 pool_drain(0);
274 }
275 /*NOTREACHED*/
276 }
277
278
279 /*
280 * uvm_aiodone_daemon: main loop for the aiodone daemon.
281 */
282
283 void
284 uvm_aiodone_daemon(void *arg)
285 {
286 int s, free;
287 struct buf *bp, *nbp;
288 UVMHIST_FUNC("uvm_aiodoned"); UVMHIST_CALLED(pdhist);
289
290 for (;;) {
291
292 /*
293 * carefully attempt to go to sleep (without losing "wakeups"!).
294 * we need splbio because we want to make sure the aio_done list
295 * is totally empty before we go to sleep.
296 */
297
298 s = splbio();
299 simple_lock(&uvm.aiodoned_lock);
300 if (TAILQ_FIRST(&uvm.aio_done) == NULL) {
301 UVMHIST_LOG(pdhist," <<SLEEPING>>",0,0,0,0);
302 UVM_UNLOCK_AND_WAIT(&uvm.aiodoned,
303 &uvm.aiodoned_lock, FALSE, "aiodoned", 0);
304 UVMHIST_LOG(pdhist," <<WOKE UP>>",0,0,0,0);
305
306 /* relock aiodoned_lock, still at splbio */
307 simple_lock(&uvm.aiodoned_lock);
308 }
309
310 /*
311 * check for done aio structures
312 */
313
314 bp = TAILQ_FIRST(&uvm.aio_done);
315 if (bp) {
316 TAILQ_INIT(&uvm.aio_done);
317 }
318
319 simple_unlock(&uvm.aiodoned_lock);
320 splx(s);
321
322 /*
323 * process each i/o that's done.
324 */
325
326 free = uvmexp.free;
327 while (bp != NULL) {
328 nbp = TAILQ_NEXT(bp, b_freelist);
329 (*bp->b_iodone)(bp);
330 bp = nbp;
331 }
332 if (free <= uvmexp.reserve_kernel) {
333 s = uvm_lock_fpageq();
334 wakeup(&uvm.pagedaemon);
335 uvm_unlock_fpageq(s);
336 } else {
337 simple_lock(&uvm.pagedaemon_lock);
338 wakeup(&uvmexp.free);
339 simple_unlock(&uvm.pagedaemon_lock);
340 }
341 }
342 }
343
344 /*
345 * uvmpd_scan_inactive: scan an inactive list for pages to clean or free.
346 *
347 * => called with page queues locked
348 * => we work on meeting our free target by converting inactive pages
349 * into free pages.
350 * => we handle the building of swap-backed clusters
351 * => we return TRUE if we are exiting because we met our target
352 */
353
354 boolean_t
355 uvmpd_scan_inactive(pglst)
356 struct pglist *pglst;
357 {
358 int error;
359 struct vm_page *p, *nextpg;
360 struct uvm_object *uobj;
361 struct vm_anon *anon;
362 struct vm_page *swpps[MAXBSIZE >> PAGE_SHIFT];
363 struct simplelock *slock;
364 int swnpages, swcpages;
365 int swslot;
366 int dirtyreacts, t, result;
367 UVMHIST_FUNC("uvmpd_scan_inactive"); UVMHIST_CALLED(pdhist);
368
369 /*
370 * swslot is non-zero if we are building a swap cluster. we want
371 * to stay in the loop while we have a page to scan or we have
372 * a swap-cluster to build.
373 */
374
375 swslot = 0;
376 swnpages = swcpages = 0;
377 dirtyreacts = 0;
378 for (p = TAILQ_FIRST(pglst); p != NULL || swslot != 0; p = nextpg) {
379 uobj = NULL;
380 anon = NULL;
381 if (p) {
382
383 /*
384 * see if we've met the free target.
385 */
386
387 if (uvmexp.free + uvmexp.paging >=
388 uvmexp.freetarg << 2 ||
389 dirtyreacts == UVMPD_NUMDIRTYREACTS) {
390 UVMHIST_LOG(pdhist," met free target: "
391 "exit loop", 0, 0, 0, 0);
392
393 if (swslot == 0) {
394 /* exit now if no swap-i/o pending */
395 break;
396 }
397
398 /* set p to null to signal final swap i/o */
399 p = NULL;
400 nextpg = NULL;
401 }
402 }
403 if (p) { /* if (we have a new page to consider) */
404
405 /*
406 * we are below target and have a new page to consider.
407 */
408
409 uvmexp.pdscans++;
410 nextpg = TAILQ_NEXT(p, pageq);
411
412 /*
413 * move referenced pages back to active queue and
414 * skip to next page.
415 */
416
417 if (pmap_clear_reference(p)) {
418 uvm_pageactivate(p);
419 uvmexp.pdreact++;
420 continue;
421 }
422 anon = p->uanon;
423 uobj = p->uobject;
424
425 /*
426 * enforce the minimum thresholds on different
427 * types of memory usage. if reusing the current
428 * page would reduce that type of usage below its
429 * minimum, reactivate the page instead and move
430 * on to the next page.
431 */
432
433 t = uvmexp.active + uvmexp.inactive + uvmexp.free;
434 if (anon &&
435 uvmexp.anonpages <= (t * uvmexp.anonmin) >> 8) {
436 uvm_pageactivate(p);
437 uvmexp.pdreanon++;
438 continue;
439 }
440 if (uobj && UVM_OBJ_IS_VTEXT(uobj) &&
441 uvmexp.vtextpages <= (t * uvmexp.vtextmin) >> 8) {
442 uvm_pageactivate(p);
443 uvmexp.pdrevtext++;
444 continue;
445 }
446 if (uobj && UVM_OBJ_IS_VNODE(uobj) &&
447 !UVM_OBJ_IS_VTEXT(uobj) &&
448 uvmexp.vnodepages <= (t * uvmexp.vnodemin) >> 8) {
449 uvm_pageactivate(p);
450 uvmexp.pdrevnode++;
451 continue;
452 }
453
454 /*
455 * first we attempt to lock the object that this page
456 * belongs to. if our attempt fails we skip on to
457 * the next page (no harm done). it is important to
458 * "try" locking the object as we are locking in the
459 * wrong order (pageq -> object) and we don't want to
460 * deadlock.
461 *
462 * the only time we expect to see an ownerless page
463 * (i.e. a page with no uobject and !PQ_ANON) is if an
464 * anon has loaned a page from a uvm_object and the
465 * uvm_object has dropped the ownership. in that
466 * case, the anon can "take over" the loaned page
467 * and make it its own.
468 */
469
470 /* is page part of an anon or ownerless ? */
471 if ((p->pqflags & PQ_ANON) || uobj == NULL) {
472 KASSERT(anon != NULL);
473 slock = &anon->an_lock;
474 if (!simple_lock_try(slock)) {
475 /* lock failed, skip this page */
476 continue;
477 }
478
479 /*
480 * if the page is ownerless, claim it in the
481 * name of "anon"!
482 */
483
484 if ((p->pqflags & PQ_ANON) == 0) {
485 KASSERT(p->loan_count > 0);
486 p->loan_count--;
487 p->pqflags |= PQ_ANON;
488 /* anon now owns it */
489 }
490 if (p->flags & PG_BUSY) {
491 simple_unlock(slock);
492 uvmexp.pdbusy++;
493 continue;
494 }
495 uvmexp.pdanscan++;
496 } else {
497 KASSERT(uobj != NULL);
498 slock = &uobj->vmobjlock;
499 if (!simple_lock_try(slock)) {
500 continue;
501 }
502 if (p->flags & PG_BUSY) {
503 simple_unlock(slock);
504 uvmexp.pdbusy++;
505 continue;
506 }
507 uvmexp.pdobscan++;
508 }
509
510
511 /*
512 * we now have the object and the page queues locked.
513 * if the page is not swap-backed, call the object's
514 * pager to flush and free the page.
515 */
516
517 if ((p->pqflags & PQ_SWAPBACKED) == 0) {
518 uvm_unlock_pageq();
519 error = (uobj->pgops->pgo_put)(uobj, p->offset,
520 p->offset + PAGE_SIZE,
521 PGO_CLEANIT|PGO_FREE);
522 uvm_lock_pageq();
523 if (nextpg &&
524 (nextpg->flags & PQ_INACTIVE) == 0) {
525 nextpg = TAILQ_FIRST(pglst);
526 }
527 continue;
528 }
529
530 /*
531 * the page is swap-backed. remove all the permissions
532 * from the page so we can sync the modified info
533 * without any race conditions. if the page is clean
534 * we can free it now and continue.
535 */
536
537 pmap_page_protect(p, VM_PROT_NONE);
538 if ((p->flags & PG_CLEAN) && pmap_clear_modify(p)) {
539 p->flags &= ~(PG_CLEAN);
540 }
541 if (p->flags & PG_CLEAN) {
542 uvm_pagefree(p);
543 uvmexp.pdfreed++;
544
545 /*
546 * for anons, we need to remove the page
547 * from the anon ourselves. for aobjs,
548 * pagefree did that for us.
549 */
550
551 if (anon) {
552 KASSERT(anon->an_swslot != 0);
553 anon->u.an_page = NULL;
554 }
555 simple_unlock(slock);
556 continue;
557 }
558
559 /*
560 * this page is dirty, skip it if we'll have met our
561 * free target when all the current pageouts complete.
562 */
563
564 if (uvmexp.free + uvmexp.paging >
565 uvmexp.freetarg << 2) {
566 simple_unlock(slock);
567 continue;
568 }
569
570 /*
571 * free any swap space allocated to the page since
572 * we'll have to write it again with its new data.
573 */
574
575 if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
576 uvm_swap_free(anon->an_swslot, 1);
577 anon->an_swslot = 0;
578 } else if (p->pqflags & PQ_AOBJ) {
579 uao_dropswap(uobj, p->offset >> PAGE_SHIFT);
580 }
581
582 /*
583 * if all pages in swap are only in swap,
584 * the swap space is full and we can't page out
585 * any more swap-backed pages. reactivate this page
586 * so that we eventually cycle all pages through
587 * the inactive queue.
588 */
589
590 KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
591 if (uvmexp.swpgonly == uvmexp.swpages) {
592 dirtyreacts++;
593 uvm_pageactivate(p);
594 simple_unlock(slock);
595 continue;
596 }
597
598 /*
599 * start new swap pageout cluster (if necessary).
600 */
601
602 if (swslot == 0) {
603 swnpages = MAXBSIZE >> PAGE_SHIFT;
604 swslot = uvm_swap_alloc(&swnpages, TRUE);
605 if (swslot == 0) {
606 simple_unlock(slock);
607 continue;
608 }
609 swcpages = 0;
610 }
611
612 /*
613 * at this point, we're definitely going reuse this
614 * page. mark the page busy and delayed-free.
615 * we should remove the page from the page queues
616 * so we don't ever look at it again.
617 * adjust counters and such.
618 */
619
620 p->flags |= PG_BUSY;
621 UVM_PAGE_OWN(p, "scan_inactive");
622
623 p->flags |= PG_PAGEOUT;
624 uvmexp.paging++;
625 uvm_pagedequeue(p);
626
627 uvmexp.pgswapout++;
628
629 /*
630 * add the new page to the cluster.
631 */
632
633 if (anon) {
634 anon->an_swslot = swslot + swcpages;
635 simple_unlock(slock);
636 } else {
637 result = uao_set_swslot(uobj,
638 p->offset >> PAGE_SHIFT, swslot + swcpages);
639 if (result == -1) {
640 p->flags &= ~(PG_BUSY|PG_PAGEOUT);
641 UVM_PAGE_OWN(p, NULL);
642 uvmexp.paging--;
643 uvm_pageactivate(p);
644 simple_unlock(slock);
645 continue;
646 }
647 simple_unlock(slock);
648 }
649 swpps[swcpages] = p;
650 swcpages++;
651
652 /*
653 * if the cluster isn't full, look for more pages
654 * before starting the i/o.
655 */
656
657 if (swcpages < swnpages) {
658 continue;
659 }
660 }
661
662 /*
663 * if this is the final pageout we could have a few
664 * unused swap blocks. if so, free them now.
665 */
666
667 if (swcpages < swnpages) {
668 uvm_swap_free(swslot + swcpages, (swnpages - swcpages));
669 }
670
671 /*
672 * now start the pageout.
673 */
674
675 uvm_unlock_pageq();
676 uvmexp.pdpageouts++;
677 error = uvm_swap_put(swslot, swpps, swcpages, 0);
678 KASSERT(error == 0);
679 uvm_lock_pageq();
680
681 /*
682 * zero swslot to indicate that we are
683 * no longer building a swap-backed cluster.
684 */
685
686 swslot = 0;
687
688 /*
689 * the pageout is in progress. bump counters and set up
690 * for the next loop.
691 */
692
693 uvmexp.pdpending++;
694 if (nextpg && (nextpg->pqflags & PQ_INACTIVE) == 0) {
695 nextpg = TAILQ_FIRST(pglst);
696 }
697 }
698 return (error);
699 }
700
701 /*
702 * uvmpd_scan: scan the page queues and attempt to meet our targets.
703 *
704 * => called with pageq's locked
705 */
706
707 void
708 uvmpd_scan(void)
709 {
710 int inactive_shortage, swap_shortage, pages_freed;
711 struct vm_page *p, *nextpg;
712 struct uvm_object *uobj;
713 struct vm_anon *anon;
714 UVMHIST_FUNC("uvmpd_scan"); UVMHIST_CALLED(pdhist);
715
716 uvmexp.pdrevs++;
717 uobj = NULL;
718 anon = NULL;
719
720 #ifndef __SWAP_BROKEN
721
722 /*
723 * swap out some processes if we are below our free target.
724 * we need to unlock the page queues for this.
725 */
726
727 if (uvmexp.free < uvmexp.freetarg && uvmexp.nswapdev != 0) {
728 uvmexp.pdswout++;
729 UVMHIST_LOG(pdhist," free %d < target %d: swapout",
730 uvmexp.free, uvmexp.freetarg, 0, 0);
731 uvm_unlock_pageq();
732 uvm_swapout_threads();
733 uvm_lock_pageq();
734
735 }
736 #endif
737
738 /*
739 * now we want to work on meeting our targets. first we work on our
740 * free target by converting inactive pages into free pages. then
741 * we work on meeting our inactive target by converting active pages
742 * to inactive ones.
743 */
744
745 UVMHIST_LOG(pdhist, " starting 'free' loop",0,0,0,0);
746
747 /*
748 * alternate starting queue between swap and object based on the
749 * low bit of uvmexp.pdrevs (which we bump by one each call).
750 */
751
752 pages_freed = uvmexp.pdfreed;
753 (void) uvmpd_scan_inactive(&uvm.page_inactive);
754 pages_freed = uvmexp.pdfreed - pages_freed;
755
756 /*
757 * we have done the scan to get free pages. now we work on meeting
758 * our inactive target.
759 */
760
761 inactive_shortage = uvmexp.inactarg - uvmexp.inactive;
762
763 /*
764 * detect if we're not going to be able to page anything out
765 * until we free some swap resources from active pages.
766 */
767
768 swap_shortage = 0;
769 if (uvmexp.free < uvmexp.freetarg &&
770 uvmexp.swpginuse == uvmexp.swpages &&
771 uvmexp.swpgonly < uvmexp.swpages &&
772 pages_freed == 0) {
773 swap_shortage = uvmexp.freetarg - uvmexp.free;
774 }
775
776 UVMHIST_LOG(pdhist, " loop 2: inactive_shortage=%d swap_shortage=%d",
777 inactive_shortage, swap_shortage,0,0);
778 for (p = TAILQ_FIRST(&uvm.page_active);
779 p != NULL && (inactive_shortage > 0 || swap_shortage > 0);
780 p = nextpg) {
781 nextpg = TAILQ_NEXT(p, pageq);
782 if (p->flags & PG_BUSY) {
783 continue;
784 }
785
786 /*
787 * lock the page's owner.
788 */
789 /* is page anon owned or ownerless? */
790 if ((p->pqflags & PQ_ANON) || p->uobject == NULL) {
791 anon = p->uanon;
792 KASSERT(anon != NULL);
793 if (!simple_lock_try(&anon->an_lock)) {
794 continue;
795 }
796
797 /* take over the page? */
798 if ((p->pqflags & PQ_ANON) == 0) {
799 KASSERT(p->loan_count > 0);
800 p->loan_count--;
801 p->pqflags |= PQ_ANON;
802 }
803 } else {
804 uobj = p->uobject;
805 if (!simple_lock_try(&uobj->vmobjlock)) {
806 continue;
807 }
808 }
809
810 /*
811 * skip this page if it's busy.
812 */
813
814 if ((p->flags & PG_BUSY) != 0) {
815 if (p->pqflags & PQ_ANON)
816 simple_unlock(&anon->an_lock);
817 else
818 simple_unlock(&uobj->vmobjlock);
819 continue;
820 }
821
822 /*
823 * if there's a shortage of swap, free any swap allocated
824 * to this page so that other pages can be paged out.
825 */
826
827 if (swap_shortage > 0) {
828 if ((p->pqflags & PQ_ANON) && anon->an_swslot) {
829 uvm_swap_free(anon->an_swslot, 1);
830 anon->an_swslot = 0;
831 p->flags &= ~PG_CLEAN;
832 swap_shortage--;
833 } else if (p->pqflags & PQ_AOBJ) {
834 int slot = uao_set_swslot(uobj,
835 p->offset >> PAGE_SHIFT, 0);
836 if (slot) {
837 uvm_swap_free(slot, 1);
838 p->flags &= ~PG_CLEAN;
839 swap_shortage--;
840 }
841 }
842 }
843
844 /*
845 * if there's a shortage of inactive pages, deactivate.
846 */
847
848 if (inactive_shortage > 0) {
849 /* no need to check wire_count as pg is "active" */
850 uvm_pagedeactivate(p);
851 uvmexp.pddeact++;
852 inactive_shortage--;
853 }
854
855 /*
856 * we're done with this page.
857 */
858
859 if (p->pqflags & PQ_ANON)
860 simple_unlock(&anon->an_lock);
861 else
862 simple_unlock(&uobj->vmobjlock);
863 }
864 }
865