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