| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 2 | * Copyright (c) 1991 Regents of the University of California. | |
| 3 | * All rights reserved. | |
| 4 | * Copyright (c) 1994 John S. Dyson | |
| 5 | * All rights reserved. | |
| 6 | * Copyright (c) 1994 David Greenman | |
| 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 the University of | |
| 23 | * California, Berkeley and its contributors. | |
| 24 | * 4. Neither the name of the University nor the names of its contributors | |
| 25 | * may be used to endorse or promote products derived from this software | |
| 26 | * without specific prior written permission. | |
| 27 | * | |
| 28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 38 | * SUCH DAMAGE. | |
| 39 | * | |
| 40 | * from: @(#)vm_pageout.c 7.4 (Berkeley) 5/7/91 | |
| 41 | * | |
| 42 | * | |
| 43 | * Copyright (c) 1987, 1990 Carnegie-Mellon University. | |
| 44 | * All rights reserved. | |
| 45 | * | |
| 46 | * Authors: Avadis Tevanian, Jr., Michael Wayne Young | |
| 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@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 | * $FreeBSD: src/sys/vm/vm_pageout.c,v 1.151.2.15 2002/12/29 18:21:04 dillon Exp $ | |
| 4ecf7cc9 | 69 | * $DragonFly: src/sys/vm/vm_pageout.c,v 1.36 2008/07/01 02:02:56 dillon Exp $ |
| 984263bc MD |
70 | */ |
| 71 | ||
| 72 | /* | |
| 73 | * The proverbial page-out daemon. | |
| 74 | */ | |
| 75 | ||
| 76 | #include "opt_vm.h" | |
| 77 | #include <sys/param.h> | |
| 78 | #include <sys/systm.h> | |
| 79 | #include <sys/kernel.h> | |
| 80 | #include <sys/proc.h> | |
| 81 | #include <sys/kthread.h> | |
| 82 | #include <sys/resourcevar.h> | |
| 83 | #include <sys/signalvar.h> | |
| 84 | #include <sys/vnode.h> | |
| 85 | #include <sys/vmmeter.h> | |
| 86 | #include <sys/sysctl.h> | |
| 87 | ||
| 88 | #include <vm/vm.h> | |
| 89 | #include <vm/vm_param.h> | |
| 90 | #include <sys/lock.h> | |
| 91 | #include <vm/vm_object.h> | |
| 92 | #include <vm/vm_page.h> | |
| 93 | #include <vm/vm_map.h> | |
| 94 | #include <vm/vm_pageout.h> | |
| 95 | #include <vm/vm_pager.h> | |
| 96 | #include <vm/swap_pager.h> | |
| 97 | #include <vm/vm_extern.h> | |
| 5fd012e0 MD |
98 | |
| 99 | #include <sys/thread2.h> | |
| 12e4aaff | 100 | #include <vm/vm_page2.h> |
| 984263bc MD |
101 | |
| 102 | /* | |
| 103 | * System initialization | |
| 104 | */ | |
| 105 | ||
| 106 | /* the kernel process "vm_pageout"*/ | |
| 1388df65 RG |
107 | static void vm_pageout (void); |
| 108 | static int vm_pageout_clean (vm_page_t); | |
| 20479584 | 109 | static int vm_pageout_scan (int pass); |
| 1388df65 | 110 | static int vm_pageout_free_page_calc (vm_size_t count); |
| bc6dffab | 111 | struct thread *pagethread; |
| 984263bc MD |
112 | |
| 113 | static struct kproc_desc page_kp = { | |
| 114 | "pagedaemon", | |
| 115 | vm_pageout, | |
| bc6dffab | 116 | &pagethread |
| 984263bc MD |
117 | }; |
| 118 | SYSINIT(pagedaemon, SI_SUB_KTHREAD_PAGE, SI_ORDER_FIRST, kproc_start, &page_kp) | |
| 119 | ||
| 120 | #if !defined(NO_SWAPPING) | |
| 121 | /* the kernel process "vm_daemon"*/ | |
| 1388df65 | 122 | static void vm_daemon (void); |
| bc6dffab | 123 | static struct thread *vmthread; |
| 984263bc MD |
124 | |
| 125 | static struct kproc_desc vm_kp = { | |
| 126 | "vmdaemon", | |
| 127 | vm_daemon, | |
| bc6dffab | 128 | &vmthread |
| 984263bc MD |
129 | }; |
| 130 | SYSINIT(vmdaemon, SI_SUB_KTHREAD_VM, SI_ORDER_FIRST, kproc_start, &vm_kp) | |
| 131 | #endif | |
| 132 | ||
| 133 | ||
| 134 | int vm_pages_needed=0; /* Event on which pageout daemon sleeps */ | |
| 135 | int vm_pageout_deficit=0; /* Estimated number of pages deficit */ | |
| 136 | int vm_pageout_pages_needed=0; /* flag saying that the pageout daemon needs pages */ | |
| 137 | ||
| 138 | #if !defined(NO_SWAPPING) | |
| 139 | static int vm_pageout_req_swapout; /* XXX */ | |
| 140 | static int vm_daemon_needed; | |
| 141 | #endif | |
| 984263bc MD |
142 | static int vm_max_launder = 32; |
| 143 | static int vm_pageout_stats_max=0, vm_pageout_stats_interval = 0; | |
| 144 | static int vm_pageout_full_stats_interval = 0; | |
| 145 | static int vm_pageout_stats_free_max=0, vm_pageout_algorithm=0; | |
| 146 | static int defer_swap_pageouts=0; | |
| 147 | static int disable_swap_pageouts=0; | |
| 148 | ||
| 149 | #if defined(NO_SWAPPING) | |
| 150 | static int vm_swap_enabled=0; | |
| 151 | static int vm_swap_idle_enabled=0; | |
| 152 | #else | |
| 153 | static int vm_swap_enabled=1; | |
| 154 | static int vm_swap_idle_enabled=0; | |
| 155 | #endif | |
| 156 | ||
| 157 | SYSCTL_INT(_vm, VM_PAGEOUT_ALGORITHM, pageout_algorithm, | |
| 158 | CTLFLAG_RW, &vm_pageout_algorithm, 0, "LRU page mgmt"); | |
| 159 | ||
| 160 | SYSCTL_INT(_vm, OID_AUTO, max_launder, | |
| 161 | CTLFLAG_RW, &vm_max_launder, 0, "Limit dirty flushes in pageout"); | |
| 162 | ||
| 163 | SYSCTL_INT(_vm, OID_AUTO, pageout_stats_max, | |
| 164 | CTLFLAG_RW, &vm_pageout_stats_max, 0, "Max pageout stats scan length"); | |
| 165 | ||
| 166 | SYSCTL_INT(_vm, OID_AUTO, pageout_full_stats_interval, | |
| 167 | CTLFLAG_RW, &vm_pageout_full_stats_interval, 0, "Interval for full stats scan"); | |
| 168 | ||
| 169 | SYSCTL_INT(_vm, OID_AUTO, pageout_stats_interval, | |
| 170 | CTLFLAG_RW, &vm_pageout_stats_interval, 0, "Interval for partial stats scan"); | |
| 171 | ||
| 172 | SYSCTL_INT(_vm, OID_AUTO, pageout_stats_free_max, | |
| 173 | CTLFLAG_RW, &vm_pageout_stats_free_max, 0, "Not implemented"); | |
| 174 | ||
| 175 | #if defined(NO_SWAPPING) | |
| 176 | SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, | |
| 177 | CTLFLAG_RD, &vm_swap_enabled, 0, ""); | |
| 178 | SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, | |
| 179 | CTLFLAG_RD, &vm_swap_idle_enabled, 0, ""); | |
| 180 | #else | |
| 181 | SYSCTL_INT(_vm, VM_SWAPPING_ENABLED, swap_enabled, | |
| 182 | CTLFLAG_RW, &vm_swap_enabled, 0, "Enable entire process swapout"); | |
| 183 | SYSCTL_INT(_vm, OID_AUTO, swap_idle_enabled, | |
| 184 | CTLFLAG_RW, &vm_swap_idle_enabled, 0, "Allow swapout on idle criteria"); | |
| 185 | #endif | |
| 186 | ||
| 187 | SYSCTL_INT(_vm, OID_AUTO, defer_swapspace_pageouts, | |
| 188 | CTLFLAG_RW, &defer_swap_pageouts, 0, "Give preference to dirty pages in mem"); | |
| 189 | ||
| 190 | SYSCTL_INT(_vm, OID_AUTO, disable_swapspace_pageouts, | |
| 191 | CTLFLAG_RW, &disable_swap_pageouts, 0, "Disallow swapout of dirty pages"); | |
| 192 | ||
| 193 | static int pageout_lock_miss; | |
| 194 | SYSCTL_INT(_vm, OID_AUTO, pageout_lock_miss, | |
| 195 | CTLFLAG_RD, &pageout_lock_miss, 0, "vget() lock misses during pageout"); | |
| 196 | ||
| 46311ac2 MD |
197 | int vm_load; |
| 198 | SYSCTL_INT(_vm, OID_AUTO, vm_load, | |
| 199 | CTLFLAG_RD, &vm_load, 0, "load on the VM system"); | |
| 200 | int vm_load_enable = 1; | |
| 201 | SYSCTL_INT(_vm, OID_AUTO, vm_load_enable, | |
| 202 | CTLFLAG_RW, &vm_load_enable, 0, "enable vm_load rate limiting"); | |
| 203 | #ifdef INVARIANTS | |
| 204 | int vm_load_debug; | |
| 205 | SYSCTL_INT(_vm, OID_AUTO, vm_load_debug, | |
| 206 | CTLFLAG_RW, &vm_load_debug, 0, "debug vm_load"); | |
| 207 | #endif | |
| 208 | ||
| 984263bc MD |
209 | #define VM_PAGEOUT_PAGE_COUNT 16 |
| 210 | int vm_pageout_page_count = VM_PAGEOUT_PAGE_COUNT; | |
| 211 | ||
| 212 | int vm_page_max_wired; /* XXX max # of wired pages system-wide */ | |
| 213 | ||
| 214 | #if !defined(NO_SWAPPING) | |
| 1388df65 RG |
215 | typedef void freeer_fcn_t (vm_map_t, vm_object_t, vm_pindex_t, int); |
| 216 | static void vm_pageout_map_deactivate_pages (vm_map_t, vm_pindex_t); | |
| 984263bc | 217 | static freeer_fcn_t vm_pageout_object_deactivate_pages; |
| 1388df65 | 218 | static void vm_req_vmdaemon (void); |
| 984263bc MD |
219 | #endif |
| 220 | static void vm_pageout_page_stats(void); | |
| 221 | ||
| 222 | /* | |
| 20479584 | 223 | * Update vm_load to slow down faulting processes. |
| 46311ac2 MD |
224 | */ |
| 225 | void | |
| 226 | vm_fault_ratecheck(void) | |
| 227 | { | |
| 228 | if (vm_pages_needed) { | |
| 229 | if (vm_load < 1000) | |
| 230 | ++vm_load; | |
| 231 | } else { | |
| 232 | if (vm_load > 0) | |
| 233 | --vm_load; | |
| 234 | } | |
| 235 | } | |
| 236 | ||
| 237 | /* | |
| 984263bc MD |
238 | * vm_pageout_clean: |
| 239 | * | |
| 06ecca5a MD |
240 | * Clean the page and remove it from the laundry. The page must not be |
| 241 | * busy on-call. | |
| 984263bc MD |
242 | * |
| 243 | * We set the busy bit to cause potential page faults on this page to | |
| 244 | * block. Note the careful timing, however, the busy bit isn't set till | |
| 245 | * late and we cannot do anything that will mess with the page. | |
| 246 | */ | |
| 247 | ||
| 248 | static int | |
| 57e43348 | 249 | vm_pageout_clean(vm_page_t m) |
| 984263bc | 250 | { |
| 5f910b2f | 251 | vm_object_t object; |
| 984263bc MD |
252 | vm_page_t mc[2*vm_pageout_page_count]; |
| 253 | int pageout_count; | |
| 254 | int ib, is, page_base; | |
| 255 | vm_pindex_t pindex = m->pindex; | |
| 256 | ||
| 257 | object = m->object; | |
| 258 | ||
| 259 | /* | |
| 260 | * It doesn't cost us anything to pageout OBJT_DEFAULT or OBJT_SWAP | |
| 261 | * with the new swapper, but we could have serious problems paging | |
| 262 | * out other object types if there is insufficient memory. | |
| 263 | * | |
| 264 | * Unfortunately, checking free memory here is far too late, so the | |
| 265 | * check has been moved up a procedural level. | |
| 266 | */ | |
| 267 | ||
| 268 | /* | |
| 269 | * Don't mess with the page if it's busy, held, or special | |
| 270 | */ | |
| 271 | if ((m->hold_count != 0) || | |
| 272 | ((m->busy != 0) || (m->flags & (PG_BUSY|PG_UNMANAGED)))) { | |
| 273 | return 0; | |
| 274 | } | |
| 275 | ||
| 276 | mc[vm_pageout_page_count] = m; | |
| 277 | pageout_count = 1; | |
| 278 | page_base = vm_pageout_page_count; | |
| 279 | ib = 1; | |
| 280 | is = 1; | |
| 281 | ||
| 282 | /* | |
| 283 | * Scan object for clusterable pages. | |
| 284 | * | |
| 285 | * We can cluster ONLY if: ->> the page is NOT | |
| 286 | * clean, wired, busy, held, or mapped into a | |
| 287 | * buffer, and one of the following: | |
| 288 | * 1) The page is inactive, or a seldom used | |
| 289 | * active page. | |
| 290 | * -or- | |
| 291 | * 2) we force the issue. | |
| 292 | * | |
| 293 | * During heavy mmap/modification loads the pageout | |
| 294 | * daemon can really fragment the underlying file | |
| 295 | * due to flushing pages out of order and not trying | |
| 296 | * align the clusters (which leave sporatic out-of-order | |
| 297 | * holes). To solve this problem we do the reverse scan | |
| 298 | * first and attempt to align our cluster, then do a | |
| 299 | * forward scan if room remains. | |
| 300 | */ | |
| 301 | ||
| 302 | more: | |
| 303 | while (ib && pageout_count < vm_pageout_page_count) { | |
| 304 | vm_page_t p; | |
| 305 | ||
| 306 | if (ib > pindex) { | |
| 307 | ib = 0; | |
| 308 | break; | |
| 309 | } | |
| 310 | ||
| 311 | if ((p = vm_page_lookup(object, pindex - ib)) == NULL) { | |
| 312 | ib = 0; | |
| 313 | break; | |
| 314 | } | |
| 315 | if (((p->queue - p->pc) == PQ_CACHE) || | |
| 316 | (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { | |
| 317 | ib = 0; | |
| 318 | break; | |
| 319 | } | |
| 320 | vm_page_test_dirty(p); | |
| 321 | if ((p->dirty & p->valid) == 0 || | |
| 322 | p->queue != PQ_INACTIVE || | |
| 323 | p->wire_count != 0 || /* may be held by buf cache */ | |
| 324 | p->hold_count != 0) { /* may be undergoing I/O */ | |
| 325 | ib = 0; | |
| 326 | break; | |
| 327 | } | |
| 328 | mc[--page_base] = p; | |
| 329 | ++pageout_count; | |
| 330 | ++ib; | |
| 331 | /* | |
| 332 | * alignment boundry, stop here and switch directions. Do | |
| 333 | * not clear ib. | |
| 334 | */ | |
| 335 | if ((pindex - (ib - 1)) % vm_pageout_page_count == 0) | |
| 336 | break; | |
| 337 | } | |
| 338 | ||
| 339 | while (pageout_count < vm_pageout_page_count && | |
| 340 | pindex + is < object->size) { | |
| 341 | vm_page_t p; | |
| 342 | ||
| 343 | if ((p = vm_page_lookup(object, pindex + is)) == NULL) | |
| 344 | break; | |
| 345 | if (((p->queue - p->pc) == PQ_CACHE) || | |
| 346 | (p->flags & (PG_BUSY|PG_UNMANAGED)) || p->busy) { | |
| 347 | break; | |
| 348 | } | |
| 349 | vm_page_test_dirty(p); | |
| 350 | if ((p->dirty & p->valid) == 0 || | |
| 351 | p->queue != PQ_INACTIVE || | |
| 352 | p->wire_count != 0 || /* may be held by buf cache */ | |
| 353 | p->hold_count != 0) { /* may be undergoing I/O */ | |
| 354 | break; | |
| 355 | } | |
| 356 | mc[page_base + pageout_count] = p; | |
| 357 | ++pageout_count; | |
| 358 | ++is; | |
| 359 | } | |
| 360 | ||
| 361 | /* | |
| 362 | * If we exhausted our forward scan, continue with the reverse scan | |
| 363 | * when possible, even past a page boundry. This catches boundry | |
| 364 | * conditions. | |
| 365 | */ | |
| 366 | if (ib && pageout_count < vm_pageout_page_count) | |
| 367 | goto more; | |
| 368 | ||
| 369 | /* | |
| 370 | * we allow reads during pageouts... | |
| 371 | */ | |
| 372 | return vm_pageout_flush(&mc[page_base], pageout_count, 0); | |
| 373 | } | |
| 374 | ||
| 375 | /* | |
| 376 | * vm_pageout_flush() - launder the given pages | |
| 377 | * | |
| 378 | * The given pages are laundered. Note that we setup for the start of | |
| 379 | * I/O ( i.e. busy the page ), mark it read-only, and bump the object | |
| 380 | * reference count all in here rather then in the parent. If we want | |
| 381 | * the parent to do more sophisticated things we may have to change | |
| 382 | * the ordering. | |
| 383 | */ | |
| 984263bc | 384 | int |
| 57e43348 | 385 | vm_pageout_flush(vm_page_t *mc, int count, int flags) |
| 984263bc | 386 | { |
| 5f910b2f | 387 | vm_object_t object; |
| 984263bc MD |
388 | int pageout_status[count]; |
| 389 | int numpagedout = 0; | |
| 390 | int i; | |
| 391 | ||
| 392 | /* | |
| 17cde63e MD |
393 | * Initiate I/O. Bump the vm_page_t->busy counter. |
| 394 | */ | |
| 395 | for (i = 0; i < count; i++) { | |
| 396 | KASSERT(mc[i]->valid == VM_PAGE_BITS_ALL, ("vm_pageout_flush page %p index %d/%d: partially invalid page", mc[i], i, count)); | |
| 397 | vm_page_io_start(mc[i]); | |
| 398 | } | |
| 399 | ||
| 400 | /* | |
| 4530a3aa MD |
401 | * We must make the pages read-only. This will also force the |
| 402 | * modified bit in the related pmaps to be cleared. The pager | |
| 403 | * cannot clear the bit for us since the I/O completion code | |
| 404 | * typically runs from an interrupt. The act of making the page | |
| 405 | * read-only handles the case for us. | |
| 984263bc | 406 | */ |
| 984263bc | 407 | for (i = 0; i < count; i++) { |
| 984263bc MD |
408 | vm_page_protect(mc[i], VM_PROT_READ); |
| 409 | } | |
| 410 | ||
| 411 | object = mc[0]->object; | |
| 412 | vm_object_pip_add(object, count); | |
| 413 | ||
| 414 | vm_pager_put_pages(object, mc, count, | |
| c439ad8f | 415 | (flags | ((object == &kernel_object) ? VM_PAGER_PUT_SYNC : 0)), |
| 984263bc MD |
416 | pageout_status); |
| 417 | ||
| 418 | for (i = 0; i < count; i++) { | |
| 419 | vm_page_t mt = mc[i]; | |
| 420 | ||
| 421 | switch (pageout_status[i]) { | |
| 422 | case VM_PAGER_OK: | |
| 423 | numpagedout++; | |
| 424 | break; | |
| 425 | case VM_PAGER_PEND: | |
| 426 | numpagedout++; | |
| 427 | break; | |
| 428 | case VM_PAGER_BAD: | |
| 429 | /* | |
| 430 | * Page outside of range of object. Right now we | |
| 431 | * essentially lose the changes by pretending it | |
| 432 | * worked. | |
| 433 | */ | |
| 434 | pmap_clear_modify(mt); | |
| 435 | vm_page_undirty(mt); | |
| 436 | break; | |
| 437 | case VM_PAGER_ERROR: | |
| 438 | case VM_PAGER_FAIL: | |
| 439 | /* | |
| c84c24da MD |
440 | * A page typically cannot be paged out when we |
| 441 | * have run out of swap. We leave the page | |
| 442 | * marked inactive and will try to page it out | |
| 443 | * again later. | |
| 444 | * | |
| 445 | * Starvation of the active page list is used to | |
| 446 | * determine when the system is massively memory | |
| 447 | * starved. | |
| 984263bc | 448 | */ |
| 984263bc MD |
449 | break; |
| 450 | case VM_PAGER_AGAIN: | |
| 451 | break; | |
| 452 | } | |
| 453 | ||
| 454 | /* | |
| 455 | * If the operation is still going, leave the page busy to | |
| 456 | * block all other accesses. Also, leave the paging in | |
| 457 | * progress indicator set so that we don't attempt an object | |
| 458 | * collapse. | |
| 93afe6be MD |
459 | * |
| 460 | * For any pages which have completed synchronously, | |
| 461 | * deactivate the page if we are under a severe deficit. | |
| 462 | * Do not try to enter them into the cache, though, they | |
| 463 | * might still be read-heavy. | |
| 984263bc MD |
464 | */ |
| 465 | if (pageout_status[i] != VM_PAGER_PEND) { | |
| 466 | vm_object_pip_wakeup(object); | |
| 467 | vm_page_io_finish(mt); | |
| 93afe6be MD |
468 | if (vm_page_count_severe()) |
| 469 | vm_page_deactivate(mt); | |
| 470 | #if 0 | |
| 984263bc MD |
471 | if (!vm_page_count_severe() || !vm_page_try_to_cache(mt)) |
| 472 | vm_page_protect(mt, VM_PROT_READ); | |
| 93afe6be | 473 | #endif |
| 984263bc MD |
474 | } |
| 475 | } | |
| 476 | return numpagedout; | |
| 477 | } | |
| 478 | ||
| 479 | #if !defined(NO_SWAPPING) | |
| 480 | /* | |
| 481 | * vm_pageout_object_deactivate_pages | |
| 482 | * | |
| 483 | * deactivate enough pages to satisfy the inactive target | |
| 484 | * requirements or if vm_page_proc_limit is set, then | |
| 485 | * deactivate all of the pages in the object and its | |
| 486 | * backing_objects. | |
| 487 | * | |
| 488 | * The object and map must be locked. | |
| 489 | */ | |
| 1f804340 MD |
490 | static int vm_pageout_object_deactivate_pages_callback(vm_page_t, void *); |
| 491 | ||
| 984263bc | 492 | static void |
| 57e43348 | 493 | vm_pageout_object_deactivate_pages(vm_map_t map, vm_object_t object, |
| 06ecca5a | 494 | vm_pindex_t desired, int map_remove_only) |
| 984263bc | 495 | { |
| 1f804340 | 496 | struct rb_vm_page_scan_info info; |
| 984263bc | 497 | int remove_mode; |
| 984263bc MD |
498 | |
| 499 | if (object->type == OBJT_DEVICE || object->type == OBJT_PHYS) | |
| 500 | return; | |
| 501 | ||
| 502 | while (object) { | |
| 503 | if (pmap_resident_count(vm_map_pmap(map)) <= desired) | |
| 504 | return; | |
| 505 | if (object->paging_in_progress) | |
| 506 | return; | |
| 507 | ||
| 508 | remove_mode = map_remove_only; | |
| 509 | if (object->shadow_count > 1) | |
| 510 | remove_mode = 1; | |
| 06ecca5a MD |
511 | |
| 512 | /* | |
| 513 | * scan the objects entire memory queue. spl protection is | |
| 514 | * required to avoid an interrupt unbusy/free race against | |
| 515 | * our busy check. | |
| 516 | */ | |
| 5fd012e0 | 517 | crit_enter(); |
| 1f804340 MD |
518 | info.limit = remove_mode; |
| 519 | info.map = map; | |
| 520 | info.desired = desired; | |
| 521 | vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL, | |
| 522 | vm_pageout_object_deactivate_pages_callback, | |
| 523 | &info | |
| 524 | ); | |
| 525 | crit_exit(); | |
| 526 | object = object->backing_object; | |
| 527 | } | |
| 528 | } | |
| 529 | ||
| 530 | static int | |
| 531 | vm_pageout_object_deactivate_pages_callback(vm_page_t p, void *data) | |
| 532 | { | |
| 533 | struct rb_vm_page_scan_info *info = data; | |
| 534 | int actcount; | |
| 984263bc | 535 | |
| 1f804340 MD |
536 | if (pmap_resident_count(vm_map_pmap(info->map)) <= info->desired) { |
| 537 | return(-1); | |
| 538 | } | |
| 539 | mycpu->gd_cnt.v_pdpages++; | |
| 540 | if (p->wire_count != 0 || p->hold_count != 0 || p->busy != 0 || | |
| 541 | (p->flags & (PG_BUSY|PG_UNMANAGED)) || | |
| 542 | !pmap_page_exists_quick(vm_map_pmap(info->map), p)) { | |
| 543 | return(0); | |
| 544 | } | |
| 984263bc | 545 | |
| 1f804340 MD |
546 | actcount = pmap_ts_referenced(p); |
| 547 | if (actcount) { | |
| 548 | vm_page_flag_set(p, PG_REFERENCED); | |
| 549 | } else if (p->flags & PG_REFERENCED) { | |
| 550 | actcount = 1; | |
| 551 | } | |
| 552 | ||
| 553 | if ((p->queue != PQ_ACTIVE) && | |
| 554 | (p->flags & PG_REFERENCED)) { | |
| 555 | vm_page_activate(p); | |
| 556 | p->act_count += actcount; | |
| 557 | vm_page_flag_clear(p, PG_REFERENCED); | |
| 558 | } else if (p->queue == PQ_ACTIVE) { | |
| 559 | if ((p->flags & PG_REFERENCED) == 0) { | |
| 560 | p->act_count -= min(p->act_count, ACT_DECLINE); | |
| 561 | if (!info->limit && (vm_pageout_algorithm || (p->act_count == 0))) { | |
| 17cde63e | 562 | vm_page_busy(p); |
| 984263bc | 563 | vm_page_protect(p, VM_PROT_NONE); |
| 17cde63e | 564 | vm_page_wakeup(p); |
| 1f804340 MD |
565 | vm_page_deactivate(p); |
| 566 | } else { | |
| 567 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); | |
| 568 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); | |
| 984263bc | 569 | } |
| 1f804340 MD |
570 | } else { |
| 571 | vm_page_activate(p); | |
| 572 | vm_page_flag_clear(p, PG_REFERENCED); | |
| 573 | if (p->act_count < (ACT_MAX - ACT_ADVANCE)) | |
| 574 | p->act_count += ACT_ADVANCE; | |
| 575 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); | |
| 576 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, p, pageq); | |
| 984263bc | 577 | } |
| 1f804340 | 578 | } else if (p->queue == PQ_INACTIVE) { |
| 17cde63e | 579 | vm_page_busy(p); |
| 1f804340 | 580 | vm_page_protect(p, VM_PROT_NONE); |
| 17cde63e | 581 | vm_page_wakeup(p); |
| 984263bc | 582 | } |
| 1f804340 | 583 | return(0); |
| 984263bc MD |
584 | } |
| 585 | ||
| 586 | /* | |
| 587 | * deactivate some number of pages in a map, try to do it fairly, but | |
| 588 | * that is really hard to do. | |
| 589 | */ | |
| 590 | static void | |
| 57e43348 | 591 | vm_pageout_map_deactivate_pages(vm_map_t map, vm_pindex_t desired) |
| 984263bc MD |
592 | { |
| 593 | vm_map_entry_t tmpe; | |
| 594 | vm_object_t obj, bigobj; | |
| 595 | int nothingwired; | |
| 596 | ||
| df4f70a6 | 597 | if (lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT)) { |
| 984263bc MD |
598 | return; |
| 599 | } | |
| 600 | ||
| 601 | bigobj = NULL; | |
| 602 | nothingwired = TRUE; | |
| 603 | ||
| 604 | /* | |
| 605 | * first, search out the biggest object, and try to free pages from | |
| 606 | * that. | |
| 607 | */ | |
| 608 | tmpe = map->header.next; | |
| 609 | while (tmpe != &map->header) { | |
| 1b874851 MD |
610 | switch(tmpe->maptype) { |
| 611 | case VM_MAPTYPE_NORMAL: | |
| 612 | case VM_MAPTYPE_VPAGETABLE: | |
| 984263bc MD |
613 | obj = tmpe->object.vm_object; |
| 614 | if ((obj != NULL) && (obj->shadow_count <= 1) && | |
| 615 | ((bigobj == NULL) || | |
| 616 | (bigobj->resident_page_count < obj->resident_page_count))) { | |
| 617 | bigobj = obj; | |
| 618 | } | |
| 1b874851 MD |
619 | break; |
| 620 | default: | |
| 621 | break; | |
| 984263bc MD |
622 | } |
| 623 | if (tmpe->wired_count > 0) | |
| 624 | nothingwired = FALSE; | |
| 625 | tmpe = tmpe->next; | |
| 626 | } | |
| 627 | ||
| 628 | if (bigobj) | |
| 629 | vm_pageout_object_deactivate_pages(map, bigobj, desired, 0); | |
| 630 | ||
| 631 | /* | |
| 632 | * Next, hunt around for other pages to deactivate. We actually | |
| 633 | * do this search sort of wrong -- .text first is not the best idea. | |
| 634 | */ | |
| 635 | tmpe = map->header.next; | |
| 636 | while (tmpe != &map->header) { | |
| 637 | if (pmap_resident_count(vm_map_pmap(map)) <= desired) | |
| 638 | break; | |
| 1b874851 MD |
639 | switch(tmpe->maptype) { |
| 640 | case VM_MAPTYPE_NORMAL: | |
| 641 | case VM_MAPTYPE_VPAGETABLE: | |
| 984263bc MD |
642 | obj = tmpe->object.vm_object; |
| 643 | if (obj) | |
| 644 | vm_pageout_object_deactivate_pages(map, obj, desired, 0); | |
| 1b874851 MD |
645 | break; |
| 646 | default: | |
| 647 | break; | |
| 984263bc MD |
648 | } |
| 649 | tmpe = tmpe->next; | |
| 650 | }; | |
| 651 | ||
| 652 | /* | |
| 653 | * Remove all mappings if a process is swapped out, this will free page | |
| 654 | * table pages. | |
| 655 | */ | |
| 656 | if (desired == 0 && nothingwired) | |
| 657 | pmap_remove(vm_map_pmap(map), | |
| 88181b08 | 658 | VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS); |
| 984263bc | 659 | vm_map_unlock(map); |
| 984263bc MD |
660 | } |
| 661 | #endif | |
| 662 | ||
| 663 | /* | |
| a11aaa81 MD |
664 | * Don't try to be fancy - being fancy can lead to vnode deadlocks. We |
| 665 | * only do it for OBJT_DEFAULT and OBJT_SWAP objects which we know can | |
| 666 | * be trivially freed. | |
| 984263bc | 667 | */ |
| 984263bc | 668 | void |
| 95813af0 MD |
669 | vm_pageout_page_free(vm_page_t m) |
| 670 | { | |
| 984263bc MD |
671 | vm_object_t object = m->object; |
| 672 | int type = object->type; | |
| 673 | ||
| 674 | if (type == OBJT_SWAP || type == OBJT_DEFAULT) | |
| 675 | vm_object_reference(object); | |
| 676 | vm_page_busy(m); | |
| 677 | vm_page_protect(m, VM_PROT_NONE); | |
| 678 | vm_page_free(m); | |
| 679 | if (type == OBJT_SWAP || type == OBJT_DEFAULT) | |
| 680 | vm_object_deallocate(object); | |
| 681 | } | |
| 682 | ||
| 683 | /* | |
| 20479584 | 684 | * vm_pageout_scan does the dirty work for the pageout daemon. |
| 984263bc | 685 | */ |
| 8fa76237 MD |
686 | struct vm_pageout_scan_info { |
| 687 | struct proc *bigproc; | |
| 688 | vm_offset_t bigsize; | |
| 689 | }; | |
| 690 | ||
| 691 | static int vm_pageout_scan_callback(struct proc *p, void *data); | |
| 692 | ||
| 20479584 | 693 | static int |
| 984263bc MD |
694 | vm_pageout_scan(int pass) |
| 695 | { | |
| 8fa76237 | 696 | struct vm_pageout_scan_info info; |
| 984263bc MD |
697 | vm_page_t m, next; |
| 698 | struct vm_page marker; | |
| fa1ae1e3 | 699 | struct vnode *vpfailed; /* warning, allowed to be stale */ |
| 20479584 MD |
700 | int maxscan, pcount; |
| 701 | int recycle_count; | |
| 702 | int inactive_shortage, active_shortage; | |
| 51db7ca2 | 703 | int inactive_original_shortage; |
| 984263bc MD |
704 | vm_object_t object; |
| 705 | int actcount; | |
| 706 | int vnodes_skipped = 0; | |
| 707 | int maxlaunder; | |
| 984263bc MD |
708 | |
| 709 | /* | |
| 710 | * Do whatever cleanup that the pmap code can. | |
| 711 | */ | |
| 712 | pmap_collect(); | |
| 713 | ||
| 984263bc | 714 | /* |
| 20479584 MD |
715 | * Calculate our target for the number of free+cache pages we |
| 716 | * want to get to. This is higher then the number that causes | |
| 717 | * allocations to stall (severe) in order to provide hysteresis, | |
| 718 | * and if we don't make it all the way but get to the minimum | |
| 719 | * we're happy. | |
| 984263bc | 720 | */ |
| 20479584 | 721 | inactive_shortage = vm_paging_target() + vm_pageout_deficit; |
| 51db7ca2 | 722 | inactive_original_shortage = inactive_shortage; |
| 20479584 | 723 | vm_pageout_deficit = 0; |
| 984263bc MD |
724 | |
| 725 | /* | |
| 726 | * Initialize our marker | |
| 727 | */ | |
| 728 | bzero(&marker, sizeof(marker)); | |
| 729 | marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; | |
| 730 | marker.queue = PQ_INACTIVE; | |
| 731 | marker.wire_count = 1; | |
| 732 | ||
| 733 | /* | |
| 734 | * Start scanning the inactive queue for pages we can move to the | |
| 735 | * cache or free. The scan will stop when the target is reached or | |
| 736 | * we have scanned the entire inactive queue. Note that m->act_count | |
| 737 | * is not used to form decisions for the inactive queue, only for the | |
| 738 | * active queue. | |
| 739 | * | |
| 740 | * maxlaunder limits the number of dirty pages we flush per scan. | |
| 741 | * For most systems a smaller value (16 or 32) is more robust under | |
| 742 | * extreme memory and disk pressure because any unnecessary writes | |
| 743 | * to disk can result in extreme performance degredation. However, | |
| 744 | * systems with excessive dirty pages (especially when MAP_NOSYNC is | |
| 745 | * used) will die horribly with limited laundering. If the pageout | |
| 746 | * daemon cannot clean enough pages in the first pass, we let it go | |
| 747 | * all out in succeeding passes. | |
| 748 | */ | |
| 749 | if ((maxlaunder = vm_max_launder) <= 1) | |
| 750 | maxlaunder = 1; | |
| 751 | if (pass) | |
| 752 | maxlaunder = 10000; | |
| 753 | ||
| 06ecca5a | 754 | /* |
| 5fd012e0 MD |
755 | * We will generally be in a critical section throughout the |
| 756 | * scan, but we can release it temporarily when we are sitting on a | |
| 757 | * non-busy page without fear. this is required to prevent an | |
| 758 | * interrupt from unbusying or freeing a page prior to our busy | |
| 759 | * check, leaving us on the wrong queue or checking the wrong | |
| 760 | * page. | |
| 06ecca5a | 761 | */ |
| 5fd012e0 | 762 | crit_enter(); |
| 984263bc | 763 | rescan0: |
| fa1ae1e3 | 764 | vpfailed = NULL; |
| 12e4aaff | 765 | maxscan = vmstats.v_inactive_count; |
| 984263bc | 766 | for (m = TAILQ_FIRST(&vm_page_queues[PQ_INACTIVE].pl); |
| 20479584 | 767 | m != NULL && maxscan-- > 0 && inactive_shortage > 0; |
| 06ecca5a MD |
768 | m = next |
| 769 | ) { | |
| 12e4aaff | 770 | mycpu->gd_cnt.v_pdpages++; |
| 984263bc | 771 | |
| 06ecca5a MD |
772 | /* |
| 773 | * Give interrupts a chance | |
| 774 | */ | |
| 5fd012e0 MD |
775 | crit_exit(); |
| 776 | crit_enter(); | |
| 984263bc | 777 | |
| 06ecca5a MD |
778 | /* |
| 779 | * It's easier for some of the conditions below to just loop | |
| 780 | * and catch queue changes here rather then check everywhere | |
| 781 | * else. | |
| 782 | */ | |
| 783 | if (m->queue != PQ_INACTIVE) | |
| 784 | goto rescan0; | |
| 984263bc MD |
785 | next = TAILQ_NEXT(m, pageq); |
| 786 | ||
| 787 | /* | |
| 788 | * skip marker pages | |
| 789 | */ | |
| 790 | if (m->flags & PG_MARKER) | |
| 791 | continue; | |
| 792 | ||
| 793 | /* | |
| 794 | * A held page may be undergoing I/O, so skip it. | |
| 795 | */ | |
| 796 | if (m->hold_count) { | |
| 984263bc MD |
797 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 798 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); | |
| e527fb6b | 799 | ++vm_swapcache_inactive_heuristic; |
| 984263bc MD |
800 | continue; |
| 801 | } | |
| 06ecca5a | 802 | |
| 984263bc MD |
803 | /* |
| 804 | * Dont mess with busy pages, keep in the front of the | |
| 805 | * queue, most likely are being paged out. | |
| 806 | */ | |
| 807 | if (m->busy || (m->flags & PG_BUSY)) { | |
| 984263bc MD |
808 | continue; |
| 809 | } | |
| 810 | ||
| 984263bc | 811 | if (m->object->ref_count == 0) { |
| 06ecca5a MD |
812 | /* |
| 813 | * If the object is not being used, we ignore previous | |
| 814 | * references. | |
| 815 | */ | |
| 984263bc MD |
816 | vm_page_flag_clear(m, PG_REFERENCED); |
| 817 | pmap_clear_reference(m); | |
| 818 | ||
| 984263bc | 819 | } else if (((m->flags & PG_REFERENCED) == 0) && |
| 06ecca5a MD |
820 | (actcount = pmap_ts_referenced(m))) { |
| 821 | /* | |
| 822 | * Otherwise, if the page has been referenced while | |
| 823 | * in the inactive queue, we bump the "activation | |
| 824 | * count" upwards, making it less likely that the | |
| 825 | * page will be added back to the inactive queue | |
| 826 | * prematurely again. Here we check the page tables | |
| 827 | * (or emulated bits, if any), given the upper level | |
| 828 | * VM system not knowing anything about existing | |
| 829 | * references. | |
| 830 | */ | |
| 984263bc MD |
831 | vm_page_activate(m); |
| 832 | m->act_count += (actcount + ACT_ADVANCE); | |
| 833 | continue; | |
| 834 | } | |
| 835 | ||
| 836 | /* | |
| 837 | * If the upper level VM system knows about any page | |
| 838 | * references, we activate the page. We also set the | |
| 839 | * "activation count" higher than normal so that we will less | |
| 840 | * likely place pages back onto the inactive queue again. | |
| 841 | */ | |
| 842 | if ((m->flags & PG_REFERENCED) != 0) { | |
| 843 | vm_page_flag_clear(m, PG_REFERENCED); | |
| 844 | actcount = pmap_ts_referenced(m); | |
| 845 | vm_page_activate(m); | |
| 846 | m->act_count += (actcount + ACT_ADVANCE + 1); | |
| 847 | continue; | |
| 848 | } | |
| 849 | ||
| 850 | /* | |
| 851 | * If the upper level VM system doesn't know anything about | |
| 852 | * the page being dirty, we have to check for it again. As | |
| 853 | * far as the VM code knows, any partially dirty pages are | |
| 854 | * fully dirty. | |
| 41a01a4d MD |
855 | * |
| 856 | * Pages marked PG_WRITEABLE may be mapped into the user | |
| 857 | * address space of a process running on another cpu. A | |
| 858 | * user process (without holding the MP lock) running on | |
| 859 | * another cpu may be able to touch the page while we are | |
| 17cde63e MD |
860 | * trying to remove it. vm_page_cache() will handle this |
| 861 | * case for us. | |
| 984263bc MD |
862 | */ |
| 863 | if (m->dirty == 0) { | |
| 864 | vm_page_test_dirty(m); | |
| 865 | } else { | |
| 866 | vm_page_dirty(m); | |
| 867 | } | |
| 868 | ||
| 984263bc | 869 | if (m->valid == 0) { |
| 41a01a4d MD |
870 | /* |
| 871 | * Invalid pages can be easily freed | |
| 872 | */ | |
| 984263bc | 873 | vm_pageout_page_free(m); |
| 12e4aaff | 874 | mycpu->gd_cnt.v_dfree++; |
| 20479584 | 875 | --inactive_shortage; |
| 984263bc MD |
876 | } else if (m->dirty == 0) { |
| 877 | /* | |
| 41a01a4d MD |
878 | * Clean pages can be placed onto the cache queue. |
| 879 | * This effectively frees them. | |
| 984263bc MD |
880 | */ |
| 881 | vm_page_cache(m); | |
| 20479584 | 882 | --inactive_shortage; |
| 984263bc MD |
883 | } else if ((m->flags & PG_WINATCFLS) == 0 && pass == 0) { |
| 884 | /* | |
| 885 | * Dirty pages need to be paged out, but flushing | |
| 886 | * a page is extremely expensive verses freeing | |
| 887 | * a clean page. Rather then artificially limiting | |
| 888 | * the number of pages we can flush, we instead give | |
| 889 | * dirty pages extra priority on the inactive queue | |
| 890 | * by forcing them to be cycled through the queue | |
| 891 | * twice before being flushed, after which the | |
| 892 | * (now clean) page will cycle through once more | |
| 893 | * before being freed. This significantly extends | |
| 894 | * the thrash point for a heavily loaded machine. | |
| 895 | */ | |
| 984263bc MD |
896 | vm_page_flag_set(m, PG_WINATCFLS); |
| 897 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); | |
| 898 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); | |
| e527fb6b | 899 | ++vm_swapcache_inactive_heuristic; |
| 984263bc MD |
900 | } else if (maxlaunder > 0) { |
| 901 | /* | |
| 902 | * We always want to try to flush some dirty pages if | |
| 903 | * we encounter them, to keep the system stable. | |
| 904 | * Normally this number is small, but under extreme | |
| 905 | * pressure where there are insufficient clean pages | |
| 906 | * on the inactive queue, we may have to go all out. | |
| 907 | */ | |
| 908 | int swap_pageouts_ok; | |
| 909 | struct vnode *vp = NULL; | |
| 910 | ||
| 911 | object = m->object; | |
| 912 | ||
| 913 | if ((object->type != OBJT_SWAP) && (object->type != OBJT_DEFAULT)) { | |
| 914 | swap_pageouts_ok = 1; | |
| 915 | } else { | |
| 916 | swap_pageouts_ok = !(defer_swap_pageouts || disable_swap_pageouts); | |
| 917 | swap_pageouts_ok |= (!disable_swap_pageouts && defer_swap_pageouts && | |
| 20479584 | 918 | vm_page_count_min(0)); |
| 984263bc MD |
919 | |
| 920 | } | |
| 921 | ||
| 922 | /* | |
| 923 | * We don't bother paging objects that are "dead". | |
| 924 | * Those objects are in a "rundown" state. | |
| 925 | */ | |
| 926 | if (!swap_pageouts_ok || (object->flags & OBJ_DEAD)) { | |
| 984263bc MD |
927 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 928 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); | |
| e527fb6b | 929 | ++vm_swapcache_inactive_heuristic; |
| 984263bc MD |
930 | continue; |
| 931 | } | |
| 932 | ||
| 933 | /* | |
| 934 | * The object is already known NOT to be dead. It | |
| 935 | * is possible for the vget() to block the whole | |
| 936 | * pageout daemon, but the new low-memory handling | |
| 937 | * code should prevent it. | |
| 938 | * | |
| 939 | * The previous code skipped locked vnodes and, worse, | |
| 940 | * reordered pages in the queue. This results in | |
| 941 | * completely non-deterministic operation because, | |
| 942 | * quite often, a vm_fault has initiated an I/O and | |
| 943 | * is holding a locked vnode at just the point where | |
| 944 | * the pageout daemon is woken up. | |
| 945 | * | |
| 946 | * We can't wait forever for the vnode lock, we might | |
| 947 | * deadlock due to a vn_read() getting stuck in | |
| 948 | * vm_wait while holding this vnode. We skip the | |
| 949 | * vnode if we can't get it in a reasonable amount | |
| 950 | * of time. | |
| fa1ae1e3 MD |
951 | * |
| 952 | * vpfailed is used to (try to) avoid the case where | |
| 953 | * a large number of pages are associated with a | |
| 954 | * locked vnode, which could cause the pageout daemon | |
| 955 | * to stall for an excessive amount of time. | |
| 984263bc | 956 | */ |
| 984263bc | 957 | if (object->type == OBJT_VNODE) { |
| fa1ae1e3 | 958 | int flags; |
| 984263bc | 959 | |
| fa1ae1e3 MD |
960 | vp = object->handle; |
| 961 | flags = LK_EXCLUSIVE | LK_NOOBJ; | |
| 962 | if (vp == vpfailed) | |
| 963 | flags |= LK_NOWAIT; | |
| 964 | else | |
| 965 | flags |= LK_TIMELOCK; | |
| 966 | if (vget(vp, flags) != 0) { | |
| 967 | vpfailed = vp; | |
| 984263bc MD |
968 | ++pageout_lock_miss; |
| 969 | if (object->flags & OBJ_MIGHTBEDIRTY) | |
| 970 | vnodes_skipped++; | |
| 971 | continue; | |
| 972 | } | |
| 973 | ||
| 974 | /* | |
| 975 | * The page might have been moved to another | |
| 976 | * queue during potential blocking in vget() | |
| 977 | * above. The page might have been freed and | |
| 978 | * reused for another vnode. The object might | |
| 979 | * have been reused for another vnode. | |
| 980 | */ | |
| 981 | if (m->queue != PQ_INACTIVE || | |
| 982 | m->object != object || | |
| 983 | object->handle != vp) { | |
| 984 | if (object->flags & OBJ_MIGHTBEDIRTY) | |
| 985 | vnodes_skipped++; | |
| 986 | vput(vp); | |
| 987 | continue; | |
| 988 | } | |
| 989 | ||
| 990 | /* | |
| 991 | * The page may have been busied during the | |
| 992 | * blocking in vput(); We don't move the | |
| 993 | * page back onto the end of the queue so that | |
| 994 | * statistics are more correct if we don't. | |
| 995 | */ | |
| 996 | if (m->busy || (m->flags & PG_BUSY)) { | |
| 997 | vput(vp); | |
| 998 | continue; | |
| 999 | } | |
| 1000 | ||
| 1001 | /* | |
| 1002 | * If the page has become held it might | |
| 1003 | * be undergoing I/O, so skip it | |
| 1004 | */ | |
| 1005 | if (m->hold_count) { | |
| 984263bc MD |
1006 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); |
| 1007 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); | |
| e527fb6b | 1008 | ++vm_swapcache_inactive_heuristic; |
| 984263bc MD |
1009 | if (object->flags & OBJ_MIGHTBEDIRTY) |
| 1010 | vnodes_skipped++; | |
| 1011 | vput(vp); | |
| 1012 | continue; | |
| 1013 | } | |
| 1014 | } | |
| 1015 | ||
| 1016 | /* | |
| 1017 | * If a page is dirty, then it is either being washed | |
| 1018 | * (but not yet cleaned) or it is still in the | |
| 1019 | * laundry. If it is still in the laundry, then we | |
| 1020 | * start the cleaning operation. | |
| 1021 | * | |
| 1022 | * This operation may cluster, invalidating the 'next' | |
| 1023 | * pointer. To prevent an inordinate number of | |
| 1024 | * restarts we use our marker to remember our place. | |
| 1025 | * | |
| 20479584 MD |
1026 | * decrement inactive_shortage on success to account |
| 1027 | * for the (future) cleaned page. Otherwise we | |
| 1028 | * could wind up laundering or cleaning too many | |
| 1029 | * pages. | |
| 984263bc | 1030 | */ |
| 984263bc | 1031 | TAILQ_INSERT_AFTER(&vm_page_queues[PQ_INACTIVE].pl, m, &marker, pageq); |
| 984263bc | 1032 | if (vm_pageout_clean(m) != 0) { |
| 20479584 | 1033 | --inactive_shortage; |
| 984263bc | 1034 | --maxlaunder; |
| c84c24da | 1035 | } |
| 984263bc MD |
1036 | next = TAILQ_NEXT(&marker, pageq); |
| 1037 | TAILQ_REMOVE(&vm_page_queues[PQ_INACTIVE].pl, &marker, pageq); | |
| 984263bc MD |
1038 | if (vp != NULL) |
| 1039 | vput(vp); | |
| 1040 | } | |
| 1041 | } | |
| 1042 | ||
| 1043 | /* | |
| 20479584 MD |
1044 | * We want to move pages from the active queue to the inactive |
| 1045 | * queue to get the inactive queue to the inactive target. If | |
| 1046 | * we still have a page shortage from above we try to directly free | |
| 1047 | * clean pages instead of moving them. | |
| 06ecca5a | 1048 | * |
| 20479584 MD |
1049 | * If we do still have a shortage we keep track of the number of |
| 1050 | * pages we free or cache (recycle_count) as a measure of thrashing | |
| 1051 | * between the active and inactive queues. | |
| 1052 | * | |
| 51db7ca2 MD |
1053 | * If we were able to completely satisfy the free+cache targets |
| 1054 | * from the inactive pool we limit the number of pages we move | |
| 1055 | * from the active pool to the inactive pool to 2x the pages we | |
| e6e9a0c3 MD |
1056 | * had removed from the inactive pool (with a minimum of 1/5 the |
| 1057 | * inactive target). If we were not able to completely satisfy | |
| 1058 | * the free+cache targets we go for the whole target aggressively. | |
| 20479584 MD |
1059 | * |
| 1060 | * NOTE: Both variables can end up negative. | |
| 1061 | * NOTE: We are still in a critical section. | |
| 984263bc | 1062 | */ |
| 20479584 | 1063 | active_shortage = vmstats.v_inactive_target - vmstats.v_inactive_count; |
| e6e9a0c3 MD |
1064 | if (inactive_original_shortage < vmstats.v_inactive_target / 10) |
| 1065 | inactive_original_shortage = vmstats.v_inactive_target / 10; | |
| 51db7ca2 MD |
1066 | if (inactive_shortage <= 0 && |
| 1067 | active_shortage > inactive_original_shortage * 2) { | |
| 1068 | active_shortage = inactive_original_shortage * 2; | |
| 1069 | } | |
| 20479584 | 1070 | |
| 12e4aaff | 1071 | pcount = vmstats.v_active_count; |
| 20479584 | 1072 | recycle_count = 0; |
| 984263bc MD |
1073 | m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); |
| 1074 | ||
| 20479584 MD |
1075 | while ((m != NULL) && (pcount-- > 0) && |
| 1076 | (inactive_shortage > 0 || active_shortage > 0) | |
| 1077 | ) { | |
| 06ecca5a MD |
1078 | /* |
| 1079 | * Give interrupts a chance. | |
| 1080 | */ | |
| 5fd012e0 MD |
1081 | crit_exit(); |
| 1082 | crit_enter(); | |
| 984263bc MD |
1083 | |
| 1084 | /* | |
| 06ecca5a | 1085 | * If the page was ripped out from under us, just stop. |
| 984263bc | 1086 | */ |
| 06ecca5a | 1087 | if (m->queue != PQ_ACTIVE) |
| 984263bc | 1088 | break; |
| 984263bc | 1089 | next = TAILQ_NEXT(m, pageq); |
| 06ecca5a | 1090 | |
| 984263bc MD |
1091 | /* |
| 1092 | * Don't deactivate pages that are busy. | |
| 1093 | */ | |
| 1094 | if ((m->busy != 0) || | |
| 1095 | (m->flags & PG_BUSY) || | |
| 1096 | (m->hold_count != 0)) { | |
| 984263bc MD |
1097 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1098 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); | |
| 984263bc MD |
1099 | m = next; |
| 1100 | continue; | |
| 1101 | } | |
| 1102 | ||
| 1103 | /* | |
| 1104 | * The count for pagedaemon pages is done after checking the | |
| 1105 | * page for eligibility... | |
| 1106 | */ | |
| 12e4aaff | 1107 | mycpu->gd_cnt.v_pdpages++; |
| 984263bc MD |
1108 | |
| 1109 | /* | |
| 20479584 MD |
1110 | * Check to see "how much" the page has been used and clear |
| 1111 | * the tracking access bits. If the object has no references | |
| 1112 | * don't bother paying the expense. | |
| 984263bc MD |
1113 | */ |
| 1114 | actcount = 0; | |
| 1115 | if (m->object->ref_count != 0) { | |
| 20479584 MD |
1116 | if (m->flags & PG_REFERENCED) |
| 1117 | ++actcount; | |
| 984263bc MD |
1118 | actcount += pmap_ts_referenced(m); |
| 1119 | if (actcount) { | |
| 1120 | m->act_count += ACT_ADVANCE + actcount; | |
| 1121 | if (m->act_count > ACT_MAX) | |
| 1122 | m->act_count = ACT_MAX; | |
| 1123 | } | |
| 1124 | } | |
| 984263bc MD |
1125 | vm_page_flag_clear(m, PG_REFERENCED); |
| 1126 | ||
| 1127 | /* | |
| 20479584 | 1128 | * actcount is only valid if the object ref_count is non-zero. |
| 984263bc | 1129 | */ |
| 20479584 | 1130 | if (actcount && m->object->ref_count != 0) { |
| 984263bc MD |
1131 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1132 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); | |
| 984263bc MD |
1133 | } else { |
| 1134 | m->act_count -= min(m->act_count, ACT_DECLINE); | |
| 1135 | if (vm_pageout_algorithm || | |
| 1136 | m->object->ref_count == 0 || | |
| 20479584 MD |
1137 | m->act_count < pass + 1 |
| 1138 | ) { | |
| 1139 | /* | |
| 1140 | * Deactivate the page. If we had a | |
| 1141 | * shortage from our inactive scan try to | |
| 1142 | * free (cache) the page instead. | |
| e6e9a0c3 MD |
1143 | * |
| 1144 | * Don't just blindly cache the page if | |
| 1145 | * we do not have a shortage from the | |
| 1146 | * inactive scan, that could lead to | |
| 1147 | * gigabytes being moved. | |
| 20479584 MD |
1148 | */ |
| 1149 | --active_shortage; | |
| 1150 | if (inactive_shortage > 0 || | |
| 1151 | m->object->ref_count == 0) { | |
| 1152 | if (inactive_shortage > 0) | |
| 1153 | ++recycle_count; | |
| 17cde63e | 1154 | vm_page_busy(m); |
| 984263bc | 1155 | vm_page_protect(m, VM_PROT_NONE); |
| 17cde63e | 1156 | vm_page_wakeup(m); |
| e6e9a0c3 MD |
1157 | if (m->dirty == 0 && |
| 1158 | inactive_shortage > 0) { | |
| 20479584 | 1159 | --inactive_shortage; |
| 984263bc | 1160 | vm_page_cache(m); |
| c84c24da | 1161 | } else { |
| 984263bc | 1162 | vm_page_deactivate(m); |
| c84c24da | 1163 | } |
| 984263bc MD |
1164 | } else { |
| 1165 | vm_page_deactivate(m); | |
| 1166 | } | |
| 1167 | } else { | |
| 984263bc MD |
1168 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1169 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); | |
| 984263bc MD |
1170 | } |
| 1171 | } | |
| 1172 | m = next; | |
| 1173 | } | |
| 1174 | ||
| 984263bc MD |
1175 | /* |
| 1176 | * We try to maintain some *really* free pages, this allows interrupt | |
| 1177 | * code to be guaranteed space. Since both cache and free queues | |
| 1178 | * are considered basically 'free', moving pages from cache to free | |
| 1179 | * does not effect other calculations. | |
| 06ecca5a | 1180 | * |
| 5fd012e0 | 1181 | * NOTE: we are still in a critical section. |
| c84c24da MD |
1182 | * |
| 1183 | * Pages moved from PQ_CACHE to totally free are not counted in the | |
| 1184 | * pages_freed counter. | |
| 984263bc | 1185 | */ |
| 12e4aaff | 1186 | while (vmstats.v_free_count < vmstats.v_free_reserved) { |
| 984263bc MD |
1187 | static int cache_rover = 0; |
| 1188 | m = vm_page_list_find(PQ_CACHE, cache_rover, FALSE); | |
| 20479584 | 1189 | if (m == NULL) |
| 984263bc MD |
1190 | break; |
| 1191 | if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || | |
| 1192 | m->busy || | |
| 1193 | m->hold_count || | |
| 1194 | m->wire_count) { | |
| 1195 | #ifdef INVARIANTS | |
| 086c1d7e | 1196 | kprintf("Warning: busy page %p found in cache\n", m); |
| 984263bc MD |
1197 | #endif |
| 1198 | vm_page_deactivate(m); | |
| 1199 | continue; | |
| 1200 | } | |
| 17cde63e MD |
1201 | KKASSERT((m->flags & PG_MAPPED) == 0); |
| 1202 | KKASSERT(m->dirty == 0); | |
| 984263bc MD |
1203 | cache_rover = (cache_rover + PQ_PRIME2) & PQ_L2_MASK; |
| 1204 | vm_pageout_page_free(m); | |
| 12e4aaff | 1205 | mycpu->gd_cnt.v_dfree++; |
| 984263bc | 1206 | } |
| 06ecca5a | 1207 | |
| 5fd012e0 | 1208 | crit_exit(); |
| 984263bc MD |
1209 | |
| 1210 | #if !defined(NO_SWAPPING) | |
| 1211 | /* | |
| 1212 | * Idle process swapout -- run once per second. | |
| 1213 | */ | |
| 1214 | if (vm_swap_idle_enabled) { | |
| 1215 | static long lsec; | |
| 1216 | if (time_second != lsec) { | |
| 1217 | vm_pageout_req_swapout |= VM_SWAP_IDLE; | |
| 1218 | vm_req_vmdaemon(); | |
| 1219 | lsec = time_second; | |
| 1220 | } | |
| 1221 | } | |
| 1222 | #endif | |
| 1223 | ||
| 1224 | /* | |
| 1225 | * If we didn't get enough free pages, and we have skipped a vnode | |
| 1226 | * in a writeable object, wakeup the sync daemon. And kick swapout | |
| 1227 | * if we did not get enough free pages. | |
| 1228 | */ | |
| 1229 | if (vm_paging_target() > 0) { | |
| 20479584 | 1230 | if (vnodes_skipped && vm_page_count_min(0)) |
| 418ff780 | 1231 | speedup_syncer(); |
| 984263bc MD |
1232 | #if !defined(NO_SWAPPING) |
| 1233 | if (vm_swap_enabled && vm_page_count_target()) { | |
| 1234 | vm_req_vmdaemon(); | |
| 1235 | vm_pageout_req_swapout |= VM_SWAP_NORMAL; | |
| 1236 | } | |
| 1237 | #endif | |
| 1238 | } | |
| 1239 | ||
| 1240 | /* | |
| 20479584 MD |
1241 | * Handle catastrophic conditions. Under good conditions we should |
| 1242 | * be at the target, well beyond our minimum. If we could not even | |
| 1243 | * reach our minimum the system is under heavy stress. | |
| 1244 | * | |
| 1245 | * Determine whether we have run out of memory. This occurs when | |
| 1246 | * swap_pager_full is TRUE and the only pages left in the page | |
| 1247 | * queues are dirty. We will still likely have page shortages. | |
| c84c24da MD |
1248 | * |
| 1249 | * - swap_pager_full is set if insufficient swap was | |
| 1250 | * available to satisfy a requested pageout. | |
| 1251 | * | |
| 20479584 MD |
1252 | * - the inactive queue is bloated (4 x size of active queue), |
| 1253 | * meaning it is unable to get rid of dirty pages and. | |
| c84c24da | 1254 | * |
| 20479584 MD |
1255 | * - vm_page_count_min() without counting pages recycled from the |
| 1256 | * active queue (recycle_count) means we could not recover | |
| 1257 | * enough pages to meet bare minimum needs. This test only | |
| 1258 | * works if the inactive queue is bloated. | |
| c84c24da | 1259 | * |
| 20479584 MD |
1260 | * - due to a positive inactive_shortage we shifted the remaining |
| 1261 | * dirty pages from the active queue to the inactive queue | |
| 1262 | * trying to find clean ones to free. | |
| 984263bc | 1263 | */ |
| 20479584 | 1264 | if (swap_pager_full && vm_page_count_min(recycle_count)) |
| c84c24da | 1265 | kprintf("Warning: system low on memory+swap!\n"); |
| 20479584 MD |
1266 | if (swap_pager_full && vm_page_count_min(recycle_count) && |
| 1267 | vmstats.v_inactive_count > vmstats.v_active_count * 4 && | |
| 1268 | inactive_shortage > 0) { | |
| 1269 | /* | |
| 1270 | * Kill something. | |
| 1271 | */ | |
| 8fa76237 MD |
1272 | info.bigproc = NULL; |
| 1273 | info.bigsize = 0; | |
| 1274 | allproc_scan(vm_pageout_scan_callback, &info); | |
| 1275 | if (info.bigproc != NULL) { | |
| 1276 | killproc(info.bigproc, "out of swap space"); | |
| 1277 | info.bigproc->p_nice = PRIO_MIN; | |
| 08f2f1bb SS |
1278 | info.bigproc->p_usched->resetpriority( |
| 1279 | FIRST_LWP_IN_PROC(info.bigproc)); | |
| 12e4aaff | 1280 | wakeup(&vmstats.v_free_count); |
| 8fa76237 | 1281 | PRELE(info.bigproc); |
| 984263bc MD |
1282 | } |
| 1283 | } | |
| 20479584 | 1284 | return(inactive_shortage); |
| 984263bc MD |
1285 | } |
| 1286 | ||
| 8fa76237 MD |
1287 | static int |
| 1288 | vm_pageout_scan_callback(struct proc *p, void *data) | |
| 1289 | { | |
| 1290 | struct vm_pageout_scan_info *info = data; | |
| 1291 | vm_offset_t size; | |
| 1292 | ||
| 1293 | /* | |
| 20479584 MD |
1294 | * Never kill system processes or init. If we have configured swap |
| 1295 | * then try to avoid killing low-numbered pids. | |
| 8fa76237 MD |
1296 | */ |
| 1297 | if ((p->p_flag & P_SYSTEM) || (p->p_pid == 1) || | |
| 1298 | ((p->p_pid < 48) && (vm_swap_size != 0))) { | |
| 1299 | return (0); | |
| 1300 | } | |
| 1301 | ||
| 1302 | /* | |
| 1303 | * if the process is in a non-running type state, | |
| 1304 | * don't touch it. | |
| 1305 | */ | |
| 20479584 | 1306 | if (p->p_stat != SACTIVE && p->p_stat != SSTOP) |
| 8fa76237 | 1307 | return (0); |
| 8fa76237 MD |
1308 | |
| 1309 | /* | |
| 20479584 MD |
1310 | * Get the approximate process size. Note that anonymous pages |
| 1311 | * with backing swap will be counted twice, but there should not | |
| 1312 | * be too many such pages due to the stress the VM system is | |
| 1313 | * under at this point. | |
| 8fa76237 | 1314 | */ |
| 20479584 | 1315 | size = vmspace_anonymous_count(p->p_vmspace) + |
| 8fa76237 MD |
1316 | vmspace_swap_count(p->p_vmspace); |
| 1317 | ||
| 1318 | /* | |
| 1319 | * If the this process is bigger than the biggest one | |
| 1320 | * remember it. | |
| 1321 | */ | |
| 20479584 | 1322 | if (info->bigsize < size) { |
| 8fa76237 MD |
1323 | if (info->bigproc) |
| 1324 | PRELE(info->bigproc); | |
| 1325 | PHOLD(p); | |
| 1326 | info->bigproc = p; | |
| 1327 | info->bigsize = size; | |
| 1328 | } | |
| 1329 | return(0); | |
| 1330 | } | |
| 1331 | ||
| 984263bc MD |
1332 | /* |
| 1333 | * This routine tries to maintain the pseudo LRU active queue, | |
| 1334 | * so that during long periods of time where there is no paging, | |
| 1335 | * that some statistic accumulation still occurs. This code | |
| 1336 | * helps the situation where paging just starts to occur. | |
| 1337 | */ | |
| 1338 | static void | |
| 57e43348 | 1339 | vm_pageout_page_stats(void) |
| 984263bc | 1340 | { |
| 984263bc MD |
1341 | vm_page_t m,next; |
| 1342 | int pcount,tpcount; /* Number of pages to check */ | |
| 1343 | static int fullintervalcount = 0; | |
| 1344 | int page_shortage; | |
| 984263bc MD |
1345 | |
| 1346 | page_shortage = | |
| 12e4aaff MD |
1347 | (vmstats.v_inactive_target + vmstats.v_cache_max + vmstats.v_free_min) - |
| 1348 | (vmstats.v_free_count + vmstats.v_inactive_count + vmstats.v_cache_count); | |
| 984263bc MD |
1349 | |
| 1350 | if (page_shortage <= 0) | |
| 1351 | return; | |
| 1352 | ||
| 5fd012e0 | 1353 | crit_enter(); |
| 984263bc | 1354 | |
| 12e4aaff | 1355 | pcount = vmstats.v_active_count; |
| 984263bc MD |
1356 | fullintervalcount += vm_pageout_stats_interval; |
| 1357 | if (fullintervalcount < vm_pageout_full_stats_interval) { | |
| 12e4aaff | 1358 | tpcount = (vm_pageout_stats_max * vmstats.v_active_count) / vmstats.v_page_count; |
| 984263bc MD |
1359 | if (pcount > tpcount) |
| 1360 | pcount = tpcount; | |
| 1361 | } else { | |
| 1362 | fullintervalcount = 0; | |
| 1363 | } | |
| 1364 | ||
| 1365 | m = TAILQ_FIRST(&vm_page_queues[PQ_ACTIVE].pl); | |
| 1366 | while ((m != NULL) && (pcount-- > 0)) { | |
| 1367 | int actcount; | |
| 1368 | ||
| 1369 | if (m->queue != PQ_ACTIVE) { | |
| 1370 | break; | |
| 1371 | } | |
| 1372 | ||
| 1373 | next = TAILQ_NEXT(m, pageq); | |
| 1374 | /* | |
| 1375 | * Don't deactivate pages that are busy. | |
| 1376 | */ | |
| 1377 | if ((m->busy != 0) || | |
| 1378 | (m->flags & PG_BUSY) || | |
| 1379 | (m->hold_count != 0)) { | |
| 984263bc MD |
1380 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1381 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); | |
| 984263bc MD |
1382 | m = next; |
| 1383 | continue; | |
| 1384 | } | |
| 1385 | ||
| 1386 | actcount = 0; | |
| 1387 | if (m->flags & PG_REFERENCED) { | |
| 1388 | vm_page_flag_clear(m, PG_REFERENCED); | |
| 1389 | actcount += 1; | |
| 1390 | } | |
| 1391 | ||
| 1392 | actcount += pmap_ts_referenced(m); | |
| 1393 | if (actcount) { | |
| 1394 | m->act_count += ACT_ADVANCE + actcount; | |
| 1395 | if (m->act_count > ACT_MAX) | |
| 1396 | m->act_count = ACT_MAX; | |
| 984263bc MD |
1397 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1398 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); | |
| 984263bc MD |
1399 | } else { |
| 1400 | if (m->act_count == 0) { | |
| 1401 | /* | |
| 1402 | * We turn off page access, so that we have | |
| 1403 | * more accurate RSS stats. We don't do this | |
| 1404 | * in the normal page deactivation when the | |
| 1405 | * system is loaded VM wise, because the | |
| 1406 | * cost of the large number of page protect | |
| 1407 | * operations would be higher than the value | |
| 1408 | * of doing the operation. | |
| 1409 | */ | |
| 17cde63e | 1410 | vm_page_busy(m); |
| 984263bc | 1411 | vm_page_protect(m, VM_PROT_NONE); |
| 17cde63e | 1412 | vm_page_wakeup(m); |
| 984263bc MD |
1413 | vm_page_deactivate(m); |
| 1414 | } else { | |
| 1415 | m->act_count -= min(m->act_count, ACT_DECLINE); | |
| 984263bc MD |
1416 | TAILQ_REMOVE(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); |
| 1417 | TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); | |
| 984263bc MD |
1418 | } |
| 1419 | } | |
| 1420 | ||
| 1421 | m = next; | |
| 1422 | } | |
| 5fd012e0 | 1423 | crit_exit(); |
| 984263bc MD |
1424 | } |
| 1425 | ||
| 1426 | static int | |
| 57e43348 | 1427 | vm_pageout_free_page_calc(vm_size_t count) |
| 984263bc | 1428 | { |
| 12e4aaff | 1429 | if (count < vmstats.v_page_count) |
| 984263bc MD |
1430 | return 0; |
| 1431 | /* | |
| 1432 | * free_reserved needs to include enough for the largest swap pager | |
| 1433 | * structures plus enough for any pv_entry structs when paging. | |
| 1434 | */ | |
| 12e4aaff MD |
1435 | if (vmstats.v_page_count > 1024) |
| 1436 | vmstats.v_free_min = 4 + (vmstats.v_page_count - 1024) / 200; | |
| 984263bc | 1437 | else |
| 12e4aaff MD |
1438 | vmstats.v_free_min = 4; |
| 1439 | vmstats.v_pageout_free_min = (2*MAXBSIZE)/PAGE_SIZE + | |
| 1440 | vmstats.v_interrupt_free_min; | |
| 1441 | vmstats.v_free_reserved = vm_pageout_page_count + | |
| 1442 | vmstats.v_pageout_free_min + (count / 768) + PQ_L2_SIZE; | |
| 1443 | vmstats.v_free_severe = vmstats.v_free_min / 2; | |
| 1444 | vmstats.v_free_min += vmstats.v_free_reserved; | |
| 1445 | vmstats.v_free_severe += vmstats.v_free_reserved; | |
| 984263bc MD |
1446 | return 1; |
| 1447 | } | |
| 1448 | ||
| 1449 | ||
| 1450 | /* | |
| 20479584 | 1451 | * vm_pageout is the high level pageout daemon. |
| 984263bc MD |
1452 | */ |
| 1453 | static void | |
| 57e43348 | 1454 | vm_pageout(void) |
| 984263bc MD |
1455 | { |
| 1456 | int pass; | |
| 20479584 | 1457 | int inactive_shortage; |
| 984263bc MD |
1458 | |
| 1459 | /* | |
| 1460 | * Initialize some paging parameters. | |
| 1461 | */ | |
| 4ecf7cc9 | 1462 | curthread->td_flags |= TDF_SYSTHREAD; |
| 984263bc | 1463 | |
| 12e4aaff MD |
1464 | vmstats.v_interrupt_free_min = 2; |
| 1465 | if (vmstats.v_page_count < 2000) | |
| 984263bc MD |
1466 | vm_pageout_page_count = 8; |
| 1467 | ||
| 12e4aaff | 1468 | vm_pageout_free_page_calc(vmstats.v_page_count); |
| 20479584 | 1469 | |
| 984263bc MD |
1470 | /* |
| 1471 | * v_free_target and v_cache_min control pageout hysteresis. Note | |
| 1472 | * that these are more a measure of the VM cache queue hysteresis | |
| 1473 | * then the VM free queue. Specifically, v_free_target is the | |
| 1474 | * high water mark (free+cache pages). | |
| 1475 | * | |
| 1476 | * v_free_reserved + v_cache_min (mostly means v_cache_min) is the | |
| 1477 | * low water mark, while v_free_min is the stop. v_cache_min must | |
| 1478 | * be big enough to handle memory needs while the pageout daemon | |
| 1479 | * is signalled and run to free more pages. | |
| 1480 | */ | |
| 12e4aaff MD |
1481 | if (vmstats.v_free_count > 6144) |
| 1482 | vmstats.v_free_target = 4 * vmstats.v_free_min + vmstats.v_free_reserved; | |
| 984263bc | 1483 | else |
| 12e4aaff | 1484 | vmstats.v_free_target = 2 * vmstats.v_free_min + vmstats.v_free_reserved; |
| 984263bc | 1485 | |
| 0e8bd897 MD |
1486 | /* |
| 1487 | * NOTE: With the new buffer cache b_act_count we want the default | |
| 1488 | * inactive target to be a percentage of available memory. | |
| 1489 | * | |
| 1490 | * The inactive target essentially determines the minimum | |
| 1491 | * number of 'temporary' pages capable of caching one-time-use | |
| 1492 | * files when the VM system is otherwise full of pages | |
| 1493 | * belonging to multi-time-use files or active program data. | |
| 51db7ca2 MD |
1494 | * |
| 1495 | * NOTE: The inactive target is aggressively persued only if the | |
| 1496 | * inactive queue becomes too small. If the inactive queue | |
| 1497 | * is large enough to satisfy page movement to free+cache | |
| 1498 | * then it is repopulated more slowly from the active queue. | |
| e15708fc | 1499 | * This allows a general inactive_target default to be set. |
| 51db7ca2 MD |
1500 | * |
| 1501 | * There is an issue here for processes which sit mostly idle | |
| 1502 | * 'overnight', such as sshd, tcsh, and X. Any movement from | |
| 1503 | * the active queue will eventually cause such pages to | |
| 1504 | * recycle eventually causing a lot of paging in the morning. | |
| 1505 | * To reduce the incidence of this pages cycled out of the | |
| 1506 | * buffer cache are moved directly to the inactive queue if | |
| e15708fc MD |
1507 | * they were only used once or twice. |
| 1508 | * | |
| 1509 | * The vfs.vm_cycle_point sysctl can be used to adjust this. | |
| 1510 | * Increasing the value (up to 64) increases the number of | |
| 1511 | * buffer recyclements which go directly to the inactive queue. | |
| 0e8bd897 | 1512 | */ |
| 12e4aaff MD |
1513 | if (vmstats.v_free_count > 2048) { |
| 1514 | vmstats.v_cache_min = vmstats.v_free_target; | |
| 1515 | vmstats.v_cache_max = 2 * vmstats.v_cache_min; | |
| 984263bc | 1516 | } else { |
| 12e4aaff MD |
1517 | vmstats.v_cache_min = 0; |
| 1518 | vmstats.v_cache_max = 0; | |
| 984263bc | 1519 | } |
| e15708fc | 1520 | vmstats.v_inactive_target = vmstats.v_free_count / 4; |
| 984263bc MD |
1521 | |
| 1522 | /* XXX does not really belong here */ | |
| 1523 | if (vm_page_max_wired == 0) | |
| 12e4aaff | 1524 | vm_page_max_wired = vmstats.v_free_count / 3; |
| 984263bc MD |
1525 | |
| 1526 | if (vm_pageout_stats_max == 0) | |
| 12e4aaff | 1527 | vm_pageout_stats_max = vmstats.v_free_target; |
| 984263bc MD |
1528 | |
| 1529 | /* | |
| 1530 | * Set interval in seconds for stats scan. | |
| 1531 | */ | |
| 1532 | if (vm_pageout_stats_interval == 0) | |
| 1533 | vm_pageout_stats_interval = 5; | |
| 1534 | if (vm_pageout_full_stats_interval == 0) | |
| 1535 | vm_pageout_full_stats_interval = vm_pageout_stats_interval * 4; | |
| 1536 | ||
| 1537 | ||
| 1538 | /* | |
| 1539 | * Set maximum free per pass | |
| 1540 | */ | |
| 1541 | if (vm_pageout_stats_free_max == 0) | |
| 1542 | vm_pageout_stats_free_max = 5; | |
| 1543 | ||
| 1544 | swap_pager_swap_init(); | |
| 1545 | pass = 0; | |
| 20479584 | 1546 | |
| 984263bc MD |
1547 | /* |
| 1548 | * The pageout daemon is never done, so loop forever. | |
| 1549 | */ | |
| 1550 | while (TRUE) { | |
| 1551 | int error; | |
| 984263bc | 1552 | |
| 12d8aca7 MD |
1553 | /* |
| 1554 | * Wait for an action request | |
| 1555 | */ | |
| 1556 | crit_enter(); | |
| 20479584 | 1557 | if (vm_pages_needed == 0) { |
| 984263bc | 1558 | error = tsleep(&vm_pages_needed, |
| 20479584 MD |
1559 | 0, "psleep", |
| 1560 | vm_pageout_stats_interval * hz); | |
| 1561 | if (error && vm_pages_needed == 0) { | |
| 984263bc MD |
1562 | vm_pageout_page_stats(); |
| 1563 | continue; | |
| 1564 | } | |
| 20479584 | 1565 | vm_pages_needed = 1; |
| 984263bc | 1566 | } |
| 12d8aca7 | 1567 | crit_exit(); |
| 984263bc | 1568 | |
| 20479584 MD |
1569 | /* |
| 1570 | * If we have enough free memory, wakeup waiters. | |
| 12d8aca7 | 1571 | * (This is optional here) |
| 20479584 MD |
1572 | */ |
| 1573 | crit_enter(); | |
| 1574 | if (!vm_page_count_min(0)) | |
| 1575 | wakeup(&vmstats.v_free_count); | |
| 1576 | mycpu->gd_cnt.v_pdwakeups++; | |
| 5fd012e0 | 1577 | crit_exit(); |
| 20479584 MD |
1578 | |
| 1579 | /* | |
| 12d8aca7 MD |
1580 | * Scan for pageout. Try to avoid thrashing the system |
| 1581 | * with activity. | |
| 20479584 | 1582 | */ |
| 12d8aca7 | 1583 | inactive_shortage = vm_pageout_scan(pass); |
| 20479584 MD |
1584 | if (inactive_shortage > 0) { |
| 1585 | ++pass; | |
| 1586 | if (swap_pager_full) { | |
| 1587 | /* | |
| 1588 | * Running out of memory, catastrophic back-off | |
| 1589 | * to one-second intervals. | |
| 1590 | */ | |
| 1591 | tsleep(&vm_pages_needed, 0, "pdelay", hz); | |
| 1592 | } else if (pass < 10 && vm_pages_needed > 1) { | |
| 1593 | /* | |
| 1594 | * Normal operation, additional processes | |
| 1595 | * have already kicked us. Retry immediately. | |
| 1596 | */ | |
| 1597 | } else if (pass < 10) { | |
| 1598 | /* | |
| 1599 | * Normal operation, fewer processes. Delay | |
| 1600 | * a bit but allow wakeups. | |
| 1601 | */ | |
| 1602 | vm_pages_needed = 0; | |
| 1603 | tsleep(&vm_pages_needed, 0, "pdelay", hz / 10); | |
| 1604 | vm_pages_needed = 1; | |
| 1605 | } else { | |
| 1606 | /* | |
| 1607 | * We've taken too many passes, forced delay. | |
| 1608 | */ | |
| 1609 | tsleep(&vm_pages_needed, 0, "pdelay", hz / 10); | |
| 1610 | } | |
| 1611 | } else { | |
| 12d8aca7 MD |
1612 | /* |
| 1613 | * Interlocked wakeup of waiters (non-optional) | |
| 1614 | */ | |
| 20479584 | 1615 | pass = 0; |
| 12d8aca7 MD |
1616 | if (vm_pages_needed && !vm_page_count_min(0)) { |
| 1617 | wakeup(&vmstats.v_free_count); | |
| 1618 | vm_pages_needed = 0; | |
| 1619 | } | |
| 20479584 | 1620 | } |
| 984263bc MD |
1621 | } |
| 1622 | } | |
| 1623 | ||
| 20479584 MD |
1624 | /* |
| 1625 | * Called after allocating a page out of the cache or free queue | |
| 1626 | * to possibly wake the pagedaemon up to replentish our supply. | |
| 1627 | * | |
| 1628 | * We try to generate some hysteresis by waking the pagedaemon up | |
| 1629 | * when our free+cache pages go below the severe level. The pagedaemon | |
| 1630 | * tries to get the count back up to at least the minimum, and through | |
| 1631 | * to the target level if possible. | |
| 1632 | * | |
| 1633 | * If the pagedaemon is already active bump vm_pages_needed as a hint | |
| 1634 | * that there are even more requests pending. | |
| 1635 | */ | |
| 984263bc | 1636 | void |
| 57e43348 | 1637 | pagedaemon_wakeup(void) |
| 984263bc | 1638 | { |
| 20479584 MD |
1639 | if (vm_page_count_severe() && curthread != pagethread) { |
| 1640 | if (vm_pages_needed == 0) { | |
| 1641 | vm_pages_needed = 1; | |
| 1642 | wakeup(&vm_pages_needed); | |
| 1643 | } else if (vm_page_count_min(0)) { | |
| 1644 | ++vm_pages_needed; | |
| 1645 | } | |
| 984263bc MD |
1646 | } |
| 1647 | } | |
| 1648 | ||
| 1649 | #if !defined(NO_SWAPPING) | |
| 1650 | static void | |
| 57e43348 | 1651 | vm_req_vmdaemon(void) |
| 984263bc MD |
1652 | { |
| 1653 | static int lastrun = 0; | |
| 1654 | ||
| 1655 | if ((ticks > (lastrun + hz)) || (ticks < lastrun)) { | |
| 1656 | wakeup(&vm_daemon_needed); | |
| 1657 | lastrun = ticks; | |
| 1658 | } | |
| 1659 | } | |
| 1660 | ||
| 8fa76237 MD |
1661 | static int vm_daemon_callback(struct proc *p, void *data __unused); |
| 1662 | ||
| 984263bc | 1663 | static void |
| 57e43348 | 1664 | vm_daemon(void) |
| 984263bc | 1665 | { |
| 984263bc | 1666 | while (TRUE) { |
| 377d4740 | 1667 | tsleep(&vm_daemon_needed, 0, "psleep", 0); |
| 984263bc MD |
1668 | if (vm_pageout_req_swapout) { |
| 1669 | swapout_procs(vm_pageout_req_swapout); | |
| 1670 | vm_pageout_req_swapout = 0; | |
| 1671 | } | |
| 1672 | /* | |
| 1673 | * scan the processes for exceeding their rlimits or if | |
| 1674 | * process is swapped out -- deactivate pages | |
| 1675 | */ | |
| 8fa76237 MD |
1676 | allproc_scan(vm_daemon_callback, NULL); |
| 1677 | } | |
| 1678 | } | |
| 984263bc | 1679 | |
| 8fa76237 MD |
1680 | static int |
| 1681 | vm_daemon_callback(struct proc *p, void *data __unused) | |
| 1682 | { | |
| 1683 | vm_pindex_t limit, size; | |
| 984263bc | 1684 | |
| 8fa76237 MD |
1685 | /* |
| 1686 | * if this is a system process or if we have already | |
| 1687 | * looked at this process, skip it. | |
| 1688 | */ | |
| 1689 | if (p->p_flag & (P_SYSTEM | P_WEXIT)) | |
| 1690 | return (0); | |
| 984263bc | 1691 | |
| 8fa76237 MD |
1692 | /* |
| 1693 | * if the process is in a non-running type state, | |
| 1694 | * don't touch it. | |
| 1695 | */ | |
| 164b8401 | 1696 | if (p->p_stat != SACTIVE && p->p_stat != SSTOP) |
| 8fa76237 | 1697 | return (0); |
| 984263bc | 1698 | |
| 8fa76237 MD |
1699 | /* |
| 1700 | * get a limit | |
| 1701 | */ | |
| 1702 | limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, | |
| 1703 | p->p_rlimit[RLIMIT_RSS].rlim_max)); | |
| 1704 | ||
| 1705 | /* | |
| 1706 | * let processes that are swapped out really be | |
| 1707 | * swapped out. Set the limit to nothing to get as | |
| 1708 | * many pages out to swap as possible. | |
| 1709 | */ | |
| 1710 | if (p->p_flag & P_SWAPPEDOUT) | |
| 1711 | limit = 0; | |
| 1712 | ||
| 1713 | size = vmspace_resident_count(p->p_vmspace); | |
| 1714 | if (limit >= 0 && size >= limit) { | |
| 1715 | vm_pageout_map_deactivate_pages( | |
| 1716 | &p->p_vmspace->vm_map, limit); | |
| 984263bc | 1717 | } |
| 8fa76237 | 1718 | return (0); |
| 984263bc | 1719 | } |
| 8fa76237 | 1720 | |
| 984263bc | 1721 | #endif |