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