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