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