kernel -- vm_page_dontneed: Fix interaction with vm_page_madvise and pagedaemon
[dragonfly.git] / sys / vm / vm_page.c
CommitLineData
984263bc 1/*
9ad0147b
MD
2 * (MPSAFE)
3 *
984263bc
MD
4 * Copyright (c) 1991 Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
984263bc
MD
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91
35 * $FreeBSD: src/sys/vm/vm_page.c,v 1.147.2.18 2002/03/10 05:03:19 alc Exp $
36 */
37
38/*
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
41 *
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43 *
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
49 *
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53 *
54 * Carnegie Mellon requests users of this software to return to
55 *
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
60 *
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
63 */
984263bc 64/*
de71fd3f
MD
65 * Resident memory management module. The module manipulates 'VM pages'.
66 * A VM page is the core building block for memory management.
984263bc
MD
67 */
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/malloc.h>
72#include <sys/proc.h>
73#include <sys/vmmeter.h>
74#include <sys/vnode.h>
cd3c66bd 75#include <sys/kernel.h>
984263bc
MD
76
77#include <vm/vm.h>
78#include <vm/vm_param.h>
79#include <sys/lock.h>
80#include <vm/vm_kern.h>
81#include <vm/pmap.h>
82#include <vm/vm_map.h>
83#include <vm/vm_object.h>
84#include <vm/vm_page.h>
85#include <vm/vm_pageout.h>
86#include <vm/vm_pager.h>
87#include <vm/vm_extern.h>
096e95c0 88#include <vm/swap_pager.h>
984263bc 89
8e5e6f1b
AH
90#include <machine/md_var.h>
91
bb6811be 92#include <vm/vm_page2.h>
bb6811be 93
906c754c
MD
94#define VMACTION_HSIZE 256
95#define VMACTION_HMASK (VMACTION_HSIZE - 1)
96
de71fd3f
MD
97static void vm_page_queue_init(void);
98static void vm_page_free_wakeup(void);
99static vm_page_t vm_page_select_cache(vm_object_t, vm_pindex_t);
74232d8e 100static vm_page_t _vm_page_list_find2(int basequeue, int index);
984263bc 101
de71fd3f 102struct vpgqueues vm_page_queues[PQ_COUNT]; /* Array of tailq lists */
984263bc 103
906c754c
MD
104LIST_HEAD(vm_page_action_list, vm_page_action);
105struct vm_page_action_list action_list[VMACTION_HSIZE];
cd3c66bd 106static volatile int vm_pages_waiting;
906c754c
MD
107
108
1f804340
MD
109RB_GENERATE2(vm_page_rb_tree, vm_page, rb_entry, rb_vm_page_compare,
110 vm_pindex_t, pindex);
111
984263bc 112static void
de71fd3f
MD
113vm_page_queue_init(void)
114{
984263bc
MD
115 int i;
116
de71fd3f 117 for (i = 0; i < PQ_L2_SIZE; i++)
12e4aaff 118 vm_page_queues[PQ_FREE+i].cnt = &vmstats.v_free_count;
de71fd3f
MD
119 for (i = 0; i < PQ_L2_SIZE; i++)
120 vm_page_queues[PQ_CACHE+i].cnt = &vmstats.v_cache_count;
984263bc 121
de71fd3f 122 vm_page_queues[PQ_INACTIVE].cnt = &vmstats.v_inactive_count;
12e4aaff
MD
123 vm_page_queues[PQ_ACTIVE].cnt = &vmstats.v_active_count;
124 vm_page_queues[PQ_HOLD].cnt = &vmstats.v_active_count;
de71fd3f
MD
125 /* PQ_NONE has no queue */
126
127 for (i = 0; i < PQ_COUNT; i++)
984263bc 128 TAILQ_INIT(&vm_page_queues[i].pl);
906c754c
MD
129
130 for (i = 0; i < VMACTION_HSIZE; i++)
131 LIST_INIT(&action_list[i]);
984263bc
MD
132}
133
de71fd3f
MD
134/*
135 * note: place in initialized data section? Is this necessary?
136 */
984263bc 137long first_page = 0;
de71fd3f 138int vm_page_array_size = 0;
984263bc 139int vm_page_zero_count = 0;
de71fd3f 140vm_page_t vm_page_array = 0;
984263bc
MD
141
142/*
de71fd3f 143 * (low level boot)
984263bc 144 *
de71fd3f
MD
145 * Sets the page size, perhaps based upon the memory size.
146 * Must be called before any use of page-size dependent functions.
984263bc
MD
147 */
148void
149vm_set_page_size(void)
150{
12e4aaff
MD
151 if (vmstats.v_page_size == 0)
152 vmstats.v_page_size = PAGE_SIZE;
153 if (((vmstats.v_page_size - 1) & vmstats.v_page_size) != 0)
984263bc
MD
154 panic("vm_set_page_size: page size not a power of two");
155}
156
157/*
de71fd3f 158 * (low level boot)
984263bc 159 *
de71fd3f
MD
160 * Add a new page to the freelist for use by the system. New pages
161 * are added to both the head and tail of the associated free page
162 * queue in a bottom-up fashion, so both zero'd and non-zero'd page
163 * requests pull 'recent' adds (higher physical addresses) first.
161399b3 164 *
654a39f0 165 * Must be called in a critical section.
984263bc 166 */
3d39a00b 167static vm_page_t
6ef943a3 168vm_add_new_page(vm_paddr_t pa)
984263bc 169{
161399b3 170 struct vpgqueues *vpq;
de71fd3f 171 vm_page_t m;
984263bc 172
12e4aaff
MD
173 ++vmstats.v_page_count;
174 ++vmstats.v_free_count;
984263bc
MD
175 m = PHYS_TO_VM_PAGE(pa);
176 m->phys_addr = pa;
177 m->flags = 0;
178 m->pc = (pa >> PAGE_SHIFT) & PQ_L2_MASK;
179 m->queue = m->pc + PQ_FREE;
26bcc0c0 180 KKASSERT(m->dirty == 0);
de71fd3f 181
161399b3
MD
182 vpq = &vm_page_queues[m->queue];
183 if (vpq->flipflop)
184 TAILQ_INSERT_TAIL(&vpq->pl, m, pageq);
185 else
186 TAILQ_INSERT_HEAD(&vpq->pl, m, pageq);
187 vpq->flipflop = 1 - vpq->flipflop;
de71fd3f 188
984263bc
MD
189 vm_page_queues[m->queue].lcnt++;
190 return (m);
191}
192
193/*
de71fd3f 194 * (low level boot)
984263bc 195 *
de71fd3f 196 * Initializes the resident memory module.
984263bc 197 *
da23a592
MD
198 * Preallocates memory for critical VM structures and arrays prior to
199 * kernel_map becoming available.
26bcc0c0 200 *
da23a592
MD
201 * Memory is allocated from (virtual2_start, virtual2_end) if available,
202 * otherwise memory is allocated from (virtual_start, virtual_end).
203 *
204 * On x86-64 (virtual_start, virtual_end) is only 2GB and may not be
205 * large enough to hold vm_page_array & other structures for machines with
206 * large amounts of ram, so we want to use virtual2* when available.
984263bc 207 */
da23a592
MD
208void
209vm_page_startup(void)
984263bc 210{
da23a592 211 vm_offset_t vaddr = virtual2_start ? virtual2_start : virtual_start;
984263bc 212 vm_offset_t mapped;
6ef943a3
MD
213 vm_size_t npages;
214 vm_paddr_t page_range;
215 vm_paddr_t new_end;
984263bc 216 int i;
6ef943a3 217 vm_paddr_t pa;
984263bc 218 int nblocks;
6ef943a3 219 vm_paddr_t last_pa;
6ef943a3
MD
220 vm_paddr_t end;
221 vm_paddr_t biggestone, biggestsize;
6ef943a3 222 vm_paddr_t total;
984263bc
MD
223
224 total = 0;
225 biggestsize = 0;
226 biggestone = 0;
227 nblocks = 0;
228 vaddr = round_page(vaddr);
229
230 for (i = 0; phys_avail[i + 1]; i += 2) {
aecf2182
MD
231 phys_avail[i] = round_page64(phys_avail[i]);
232 phys_avail[i + 1] = trunc_page64(phys_avail[i + 1]);
984263bc
MD
233 }
234
235 for (i = 0; phys_avail[i + 1]; i += 2) {
6ef943a3 236 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
984263bc
MD
237
238 if (size > biggestsize) {
239 biggestone = i;
240 biggestsize = size;
241 }
242 ++nblocks;
243 total += size;
244 }
245
246 end = phys_avail[biggestone+1];
1f804340 247 end = trunc_page(end);
984263bc
MD
248
249 /*
250 * Initialize the queue headers for the free queue, the active queue
251 * and the inactive queue.
252 */
253
254 vm_page_queue_init();
255
6abe3bd0
AH
256 /* VKERNELs don't support minidumps and as such don't need vm_page_dump */
257#if !defined(_KERNEL_VIRTUAL)
984263bc 258 /*
8e5e6f1b
AH
259 * Allocate a bitmap to indicate that a random physical page
260 * needs to be included in a minidump.
261 *
262 * The amd64 port needs this to indicate which direct map pages
263 * need to be dumped, via calls to dump_add_page()/dump_drop_page().
264 *
265 * However, i386 still needs this workspace internally within the
266 * minidump code. In theory, they are not needed on i386, but are
267 * included should the sf_buf code decide to use them.
268 */
269 page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE;
270 vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
271 end -= vm_page_dump_size;
272 vm_page_dump = (void *)pmap_map(&vaddr, end, end + vm_page_dump_size,
273 VM_PROT_READ | VM_PROT_WRITE);
274 bzero((void *)vm_page_dump, vm_page_dump_size);
6abe3bd0 275#endif
8e5e6f1b
AH
276
277 /*
984263bc
MD
278 * Compute the number of pages of memory that will be available for
279 * use (taking into account the overhead of a page structure per
280 * page).
281 */
984263bc 282 first_page = phys_avail[0] / PAGE_SIZE;
984263bc 283 page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page;
1f804340 284 npages = (total - (page_range * sizeof(struct vm_page))) / PAGE_SIZE;
de71fd3f 285
984263bc
MD
286 /*
287 * Initialize the mem entry structures now, and put them in the free
288 * queue.
289 */
984263bc 290 new_end = trunc_page(end - page_range * sizeof(struct vm_page));
8e5e6f1b 291 mapped = pmap_map(&vaddr, new_end, end,
984263bc 292 VM_PROT_READ | VM_PROT_WRITE);
8e5e6f1b
AH
293 vm_page_array = (vm_page_t)mapped;
294
0e6594a8 295#if defined(__x86_64__) && !defined(_KERNEL_VIRTUAL)
8e5e6f1b
AH
296 /*
297 * since pmap_map on amd64 returns stuff out of a direct-map region,
298 * we have to manually add these pages to the minidump tracking so
299 * that they can be dumped, including the vm_page_array.
300 */
301 for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
302 dump_add_page(pa);
8fdd3267 303#endif
984263bc
MD
304
305 /*
306 * Clear all of the page structures
307 */
308 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
309 vm_page_array_size = page_range;
310
311 /*
161399b3 312 * Construct the free queue(s) in ascending order (by physical
984263bc
MD
313 * address) so that the first 16MB of physical memory is allocated
314 * last rather than first. On large-memory machines, this avoids
315 * the exhaustion of low physical memory before isa_dmainit has run.
316 */
12e4aaff
MD
317 vmstats.v_page_count = 0;
318 vmstats.v_free_count = 0;
984263bc
MD
319 for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) {
320 pa = phys_avail[i];
321 if (i == biggestone)
322 last_pa = new_end;
323 else
324 last_pa = phys_avail[i + 1];
325 while (pa < last_pa && npages-- > 0) {
326 vm_add_new_page(pa);
327 pa += PAGE_SIZE;
328 }
329 }
da23a592
MD
330 if (virtual2_start)
331 virtual2_start = vaddr;
332 else
333 virtual_start = vaddr;
984263bc
MD
334}
335
336/*
1f804340
MD
337 * Scan comparison function for Red-Black tree scans. An inclusive
338 * (start,end) is expected. Other fields are not used.
984263bc 339 */
1f804340
MD
340int
341rb_vm_page_scancmp(struct vm_page *p, void *data)
984263bc 342{
1f804340 343 struct rb_vm_page_scan_info *info = data;
984263bc 344
1f804340
MD
345 if (p->pindex < info->start_pindex)
346 return(-1);
347 if (p->pindex > info->end_pindex)
348 return(1);
349 return(0);
350}
351
352int
353rb_vm_page_compare(struct vm_page *p1, struct vm_page *p2)
354{
355 if (p1->pindex < p2->pindex)
356 return(-1);
357 if (p1->pindex > p2->pindex)
358 return(1);
359 return(0);
984263bc
MD
360}
361
de71fd3f 362/*
573fb415
MD
363 * Holding a page keeps it from being reused. Other parts of the system
364 * can still disassociate the page from its current object and free it, or
365 * perform read or write I/O on it and/or otherwise manipulate the page,
366 * but if the page is held the VM system will leave the page and its data
367 * intact and not reuse the page for other purposes until the last hold
368 * reference is released. (see vm_page_wire() if you want to prevent the
369 * page from being disassociated from its object too).
370 *
371 * The caller must hold vm_token.
372 *
373 * The caller must still validate the contents of the page and, if necessary,
374 * wait for any pending I/O (e.g. vm_page_sleep_busy() loop) to complete
375 * before manipulating the page.
376 */
377void
378vm_page_hold(vm_page_t m)
379{
380 ASSERT_LWKT_TOKEN_HELD(&vm_token);
381 ++m->hold_count;
382}
383
384/*
de71fd3f
MD
385 * The opposite of vm_page_hold(). A page can be freed while being held,
386 * which places it on the PQ_HOLD queue. We must call vm_page_free_toq()
387 * in this case to actually free it once the hold count drops to 0.
388 *
573fb415
MD
389 * The caller must hold vm_token if non-blocking operation is desired,
390 * but otherwise does not need to.
de71fd3f 391 */
984263bc 392void
573fb415 393vm_page_unhold(vm_page_t m)
984263bc 394{
573fb415
MD
395 lwkt_gettoken(&vm_token);
396 --m->hold_count;
397 KASSERT(m->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
398 if (m->hold_count == 0 && m->queue == PQ_HOLD) {
399 vm_page_busy(m);
400 vm_page_free_toq(m);
97edb3b6 401 }
573fb415 402 lwkt_reltoken(&vm_token);
984263bc
MD
403}
404
405/*
573fb415 406 * Inserts the given vm_page into the object and object list.
984263bc 407 *
de71fd3f
MD
408 * The pagetables are not updated but will presumably fault the page
409 * in if necessary, or if a kernel page the caller will at some point
410 * enter the page into the kernel's pmap. We are not allowed to block
411 * here so we *can't* do this anyway.
984263bc 412 *
de71fd3f 413 * This routine may not block.
573fb415 414 * This routine must be called with the vm_token held.
398c240d 415 * This routine must be called with the vm_object held.
654a39f0 416 * This routine must be called with a critical section held.
984263bc 417 */
984263bc
MD
418void
419vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
420{
573fb415 421 ASSERT_LWKT_TOKEN_HELD(&vm_token);
984263bc
MD
422 if (m->object != NULL)
423 panic("vm_page_insert: already inserted");
424
425 /*
426 * Record the object/offset pair in this page
427 */
984263bc
MD
428 m->object = object;
429 m->pindex = pindex;
430
431 /*
1f804340 432 * Insert it into the object.
984263bc 433 */
1f804340 434 vm_page_rb_tree_RB_INSERT(&object->rb_memq, m);
984263bc
MD
435 object->generation++;
436
437 /*
438 * show that the object has one more resident page.
439 */
984263bc
MD
440 object->resident_page_count++;
441
442 /*
50a55c46
MD
443 * Add the pv_list_cout of the page when its inserted in
444 * the object
445 */
446 object->agg_pv_list_count = object->agg_pv_list_count + m->md.pv_list_count;
447
448 /*
984263bc
MD
449 * Since we are inserting a new and possibly dirty page,
450 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags.
451 */
17cde63e 452 if ((m->valid & m->dirty) || (m->flags & PG_WRITEABLE))
984263bc 453 vm_object_set_writeable_dirty(object);
096e95c0
MD
454
455 /*
456 * Checks for a swap assignment and sets PG_SWAPPED if appropriate.
457 */
458 swap_pager_page_inserted(m);
984263bc
MD
459}
460
461/*
9765affa
MD
462 * Removes the given vm_page_t from the global (object,index) hash table
463 * and from the object's memq.
984263bc 464 *
de71fd3f
MD
465 * The underlying pmap entry (if any) is NOT removed here.
466 * This routine may not block.
9765affa 467 *
9ad0147b
MD
468 * The page must be BUSY and will remain BUSY on return.
469 * No other requirements.
9765affa 470 *
9ad0147b
MD
471 * NOTE: FreeBSD side effect was to unbusy the page on return. We leave
472 * it busy.
984263bc 473 */
984263bc
MD
474void
475vm_page_remove(vm_page_t m)
476{
477 vm_object_t object;
478
9ad0147b 479 lwkt_gettoken(&vm_token);
654a39f0 480 if (m->object == NULL) {
9ad0147b 481 lwkt_reltoken(&vm_token);
984263bc 482 return;
654a39f0 483 }
984263bc 484
de71fd3f 485 if ((m->flags & PG_BUSY) == 0)
984263bc 486 panic("vm_page_remove: page not busy");
984263bc 487
984263bc
MD
488 object = m->object;
489
398c240d
VS
490 vm_object_hold(object);
491
984263bc 492 /*
1f804340 493 * Remove the page from the object and update the object.
984263bc 494 */
1f804340 495 vm_page_rb_tree_RB_REMOVE(&object->rb_memq, m);
984263bc 496 object->resident_page_count--;
50a55c46 497 object->agg_pv_list_count = object->agg_pv_list_count - m->md.pv_list_count;
984263bc 498 object->generation++;
984263bc 499 m->object = NULL;
1f804340 500
398c240d
VS
501 vm_object_drop(object);
502
9ad0147b 503 lwkt_reltoken(&vm_token);
984263bc
MD
504}
505
506/*
de71fd3f
MD
507 * Locate and return the page at (object, pindex), or NULL if the
508 * page could not be found.
509 *
77912481 510 * The caller must hold vm_token.
984263bc 511 */
984263bc
MD
512vm_page_t
513vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
514{
515 vm_page_t m;
984263bc
MD
516
517 /*
518 * Search the hash table for this object/offset pair
519 */
77912481 520 ASSERT_LWKT_TOKEN_HELD(&vm_token);
1f804340 521 m = vm_page_rb_tree_RB_LOOKUP(&object->rb_memq, pindex);
1f804340
MD
522 KKASSERT(m == NULL || (m->object == object && m->pindex == pindex));
523 return(m);
984263bc
MD
524}
525
526/*
de71fd3f 527 * vm_page_rename()
984263bc 528 *
de71fd3f
MD
529 * Move the given memory entry from its current object to the specified
530 * target object/offset.
984263bc 531 *
de71fd3f
MD
532 * The object must be locked.
533 * This routine may not block.
984263bc 534 *
de71fd3f 535 * Note: This routine will raise itself to splvm(), the caller need not.
984263bc 536 *
de71fd3f
MD
537 * Note: Swap associated with the page must be invalidated by the move. We
538 * have to do this for several reasons: (1) we aren't freeing the
539 * page, (2) we are dirtying the page, (3) the VM system is probably
540 * moving the page from object A to B, and will then later move
541 * the backing store from A to B and we can't have a conflict.
984263bc 542 *
de71fd3f
MD
543 * Note: We *always* dirty the page. It is necessary both for the
544 * fact that we moved it, and because we may be invalidating
545 * swap. If the page is on the cache, we have to deactivate it
546 * or vm_page_dirty() will panic. Dirty pages are not allowed
547 * on the cache.
984263bc 548 */
984263bc
MD
549void
550vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
551{
9ad0147b 552 lwkt_gettoken(&vm_token);
398c240d 553 vm_object_hold(new_object);
984263bc
MD
554 vm_page_remove(m);
555 vm_page_insert(m, new_object, new_pindex);
556 if (m->queue - m->pc == PQ_CACHE)
557 vm_page_deactivate(m);
558 vm_page_dirty(m);
9765affa 559 vm_page_wakeup(m);
398c240d 560 vm_object_drop(new_object);
9ad0147b 561 lwkt_reltoken(&vm_token);
984263bc
MD
562}
563
564/*
de71fd3f
MD
565 * vm_page_unqueue() without any wakeup. This routine is used when a page
566 * is being moved between queues or otherwise is to remain BUSYied by the
567 * caller.
984263bc 568 *
573fb415 569 * The caller must hold vm_token
de71fd3f 570 * This routine may not block.
984263bc 571 */
984263bc
MD
572void
573vm_page_unqueue_nowakeup(vm_page_t m)
574{
575 int queue = m->queue;
576 struct vpgqueues *pq;
de71fd3f 577
573fb415 578 ASSERT_LWKT_TOKEN_HELD(&vm_token);
984263bc
MD
579 if (queue != PQ_NONE) {
580 pq = &vm_page_queues[queue];
581 m->queue = PQ_NONE;
582 TAILQ_REMOVE(&pq->pl, m, pageq);
583 (*pq->cnt)--;
584 pq->lcnt--;
585 }
586}
587
588/*
de71fd3f
MD
589 * vm_page_unqueue() - Remove a page from its queue, wakeup the pagedemon
590 * if necessary.
984263bc 591 *
573fb415 592 * The caller must hold vm_token
de71fd3f 593 * This routine may not block.
984263bc 594 */
984263bc
MD
595void
596vm_page_unqueue(vm_page_t m)
597{
598 int queue = m->queue;
599 struct vpgqueues *pq;
de71fd3f 600
573fb415 601 ASSERT_LWKT_TOKEN_HELD(&vm_token);
984263bc
MD
602 if (queue != PQ_NONE) {
603 m->queue = PQ_NONE;
604 pq = &vm_page_queues[queue];
605 TAILQ_REMOVE(&pq->pl, m, pageq);
606 (*pq->cnt)--;
607 pq->lcnt--;
20479584
MD
608 if ((queue - m->pc) == PQ_CACHE || (queue - m->pc) == PQ_FREE)
609 pagedaemon_wakeup();
984263bc
MD
610 }
611}
612
984263bc 613/*
de71fd3f 614 * vm_page_list_find()
984263bc 615 *
de71fd3f 616 * Find a page on the specified queue with color optimization.
984263bc 617 *
de71fd3f
MD
618 * The page coloring optimization attempts to locate a page that does
619 * not overload other nearby pages in the object in the cpu's L1 or L2
620 * caches. We need this optimization because cpu caches tend to be
621 * physical caches, while object spaces tend to be virtual.
984263bc 622 *
573fb415 623 * Must be called with vm_token held.
de71fd3f 624 * This routine may not block.
984263bc 625 *
de71fd3f
MD
626 * Note that this routine is carefully inlined. A non-inlined version
627 * is available for outside callers but the only critical path is
628 * from within this source file.
984263bc 629 */
74232d8e 630static __inline
984263bc 631vm_page_t
74232d8e
MD
632_vm_page_list_find(int basequeue, int index, boolean_t prefer_zero)
633{
634 vm_page_t m;
635
636 if (prefer_zero)
637 m = TAILQ_LAST(&vm_page_queues[basequeue+index].pl, pglist);
638 else
639 m = TAILQ_FIRST(&vm_page_queues[basequeue+index].pl);
640 if (m == NULL)
641 m = _vm_page_list_find2(basequeue, index);
642 return(m);
643}
644
645static vm_page_t
646_vm_page_list_find2(int basequeue, int index)
984263bc
MD
647{
648 int i;
649 vm_page_t m = NULL;
650 struct vpgqueues *pq;
651
652 pq = &vm_page_queues[basequeue];
653
654 /*
655 * Note that for the first loop, index+i and index-i wind up at the
656 * same place. Even though this is not totally optimal, we've already
657 * blown it by missing the cache case so we do not care.
658 */
659
660 for(i = PQ_L2_SIZE / 2; i > 0; --i) {
661 if ((m = TAILQ_FIRST(&pq[(index + i) & PQ_L2_MASK].pl)) != NULL)
662 break;
663
664 if ((m = TAILQ_FIRST(&pq[(index - i) & PQ_L2_MASK].pl)) != NULL)
665 break;
666 }
667 return(m);
668}
669
573fb415
MD
670/*
671 * Must be called with vm_token held if the caller desired non-blocking
672 * operation and a stable result.
673 */
74232d8e
MD
674vm_page_t
675vm_page_list_find(int basequeue, int index, boolean_t prefer_zero)
676{
677 return(_vm_page_list_find(basequeue, index, prefer_zero));
678}
679
984263bc 680/*
de71fd3f
MD
681 * Find a page on the cache queue with color optimization. As pages
682 * might be found, but not applicable, they are deactivated. This
683 * keeps us from using potentially busy cached pages.
984263bc 684 *
de71fd3f 685 * This routine may not block.
573fb415 686 * Must be called with vm_token held.
984263bc
MD
687 */
688vm_page_t
689vm_page_select_cache(vm_object_t object, vm_pindex_t pindex)
690{
691 vm_page_t m;
692
573fb415 693 ASSERT_LWKT_TOKEN_HELD(&vm_token);
984263bc 694 while (TRUE) {
659c6a07 695 m = _vm_page_list_find(
984263bc
MD
696 PQ_CACHE,
697 (pindex + object->pg_color) & PQ_L2_MASK,
698 FALSE
699 );
700 if (m && ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy ||
701 m->hold_count || m->wire_count)) {
a491077e 702 /* cache page found busy */
984263bc 703 vm_page_deactivate(m);
a491077e
MD
704#ifdef INVARIANTS
705 kprintf("Warning: busy page %p found in cache\n", m);
706#endif
984263bc
MD
707 continue;
708 }
709 return m;
710 }
de71fd3f 711 /* not reached */
984263bc
MD
712}
713
714/*
de71fd3f
MD
715 * Find a free or zero page, with specified preference. We attempt to
716 * inline the nominal case and fall back to _vm_page_select_free()
717 * otherwise.
984263bc 718 *
654a39f0 719 * This routine must be called with a critical section held.
de71fd3f 720 * This routine may not block.
984263bc 721 */
984263bc
MD
722static __inline vm_page_t
723vm_page_select_free(vm_object_t object, vm_pindex_t pindex, boolean_t prefer_zero)
724{
725 vm_page_t m;
726
659c6a07 727 m = _vm_page_list_find(
984263bc
MD
728 PQ_FREE,
729 (pindex + object->pg_color) & PQ_L2_MASK,
730 prefer_zero
731 );
732 return(m);
733}
734
735/*
de71fd3f 736 * vm_page_alloc()
984263bc 737 *
de71fd3f
MD
738 * Allocate and return a memory cell associated with this VM object/offset
739 * pair.
984263bc
MD
740 *
741 * page_req classes:
de71fd3f 742 *
dc1fd4b3 743 * VM_ALLOC_NORMAL allow use of cache pages, nominal free drain
39208dbe 744 * VM_ALLOC_QUICK like normal but cannot use cache
dc1fd4b3
MD
745 * VM_ALLOC_SYSTEM greater free drain
746 * VM_ALLOC_INTERRUPT allow free list to be completely drained
747 * VM_ALLOC_ZERO advisory request for pre-zero'd page
984263bc 748 *
de71fd3f
MD
749 * The object must be locked.
750 * This routine may not block.
9765affa 751 * The returned page will be marked PG_BUSY
984263bc 752 *
de71fd3f
MD
753 * Additional special handling is required when called from an interrupt
754 * (VM_ALLOC_INTERRUPT). We are not allowed to mess with the page cache
755 * in this case.
984263bc 756 */
984263bc
MD
757vm_page_t
758vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int page_req)
759{
760 vm_page_t m = NULL;
984263bc 761
ba9d3e52
AH
762 lwkt_gettoken(&vm_token);
763
cfd17028 764 KKASSERT(object != NULL);
984263bc
MD
765 KASSERT(!vm_page_lookup(object, pindex),
766 ("vm_page_alloc: page already allocated"));
dc1fd4b3 767 KKASSERT(page_req &
39208dbe
MD
768 (VM_ALLOC_NORMAL|VM_ALLOC_QUICK|
769 VM_ALLOC_INTERRUPT|VM_ALLOC_SYSTEM));
984263bc
MD
770
771 /*
4ecf7cc9
MD
772 * Certain system threads (pageout daemon, buf_daemon's) are
773 * allowed to eat deeper into the free page list.
984263bc 774 */
4ecf7cc9 775 if (curthread->td_flags & TDF_SYSTHREAD)
dc1fd4b3 776 page_req |= VM_ALLOC_SYSTEM;
984263bc 777
984263bc 778loop:
dc1fd4b3
MD
779 if (vmstats.v_free_count > vmstats.v_free_reserved ||
780 ((page_req & VM_ALLOC_INTERRUPT) && vmstats.v_free_count > 0) ||
781 ((page_req & VM_ALLOC_SYSTEM) && vmstats.v_cache_count == 0 &&
782 vmstats.v_free_count > vmstats.v_interrupt_free_min)
783 ) {
984263bc 784 /*
dc1fd4b3 785 * The free queue has sufficient free pages to take one out.
984263bc 786 */
dc1fd4b3 787 if (page_req & VM_ALLOC_ZERO)
984263bc
MD
788 m = vm_page_select_free(object, pindex, TRUE);
789 else
790 m = vm_page_select_free(object, pindex, FALSE);
dc1fd4b3 791 } else if (page_req & VM_ALLOC_NORMAL) {
984263bc 792 /*
dc1fd4b3
MD
793 * Allocatable from the cache (non-interrupt only). On
794 * success, we must free the page and try again, thus
795 * ensuring that vmstats.v_*_free_min counters are replenished.
984263bc 796 */
dc1fd4b3
MD
797#ifdef INVARIANTS
798 if (curthread->td_preempted) {
086c1d7e 799 kprintf("vm_page_alloc(): warning, attempt to allocate"
dc1fd4b3
MD
800 " cache page from preempting interrupt\n");
801 m = NULL;
802 } else {
803 m = vm_page_select_cache(object, pindex);
804 }
805#else
806 m = vm_page_select_cache(object, pindex);
807#endif
984263bc 808 /*
9765affa 809 * On success move the page into the free queue and loop.
984263bc 810 */
dc1fd4b3
MD
811 if (m != NULL) {
812 KASSERT(m->dirty == 0,
813 ("Found dirty cache page %p", m));
814 vm_page_busy(m);
815 vm_page_protect(m, VM_PROT_NONE);
816 vm_page_free(m);
817 goto loop;
818 }
819
820 /*
821 * On failure return NULL
822 */
9ad0147b 823 lwkt_reltoken(&vm_token);
984263bc 824#if defined(DIAGNOSTIC)
dc1fd4b3 825 if (vmstats.v_cache_count > 0)
086c1d7e 826 kprintf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", vmstats.v_cache_count);
984263bc 827#endif
dc1fd4b3
MD
828 vm_pageout_deficit++;
829 pagedaemon_wakeup();
830 return (NULL);
984263bc
MD
831 } else {
832 /*
dc1fd4b3 833 * No pages available, wakeup the pageout daemon and give up.
984263bc 834 */
9ad0147b 835 lwkt_reltoken(&vm_token);
984263bc
MD
836 vm_pageout_deficit++;
837 pagedaemon_wakeup();
838 return (NULL);
839 }
840
841 /*
9765affa
MD
842 * Good page found. The page has not yet been busied. We are in
843 * a critical section.
984263bc 844 */
dc1fd4b3 845 KASSERT(m != NULL, ("vm_page_alloc(): missing page on free queue\n"));
26bcc0c0
MD
846 KASSERT(m->dirty == 0,
847 ("vm_page_alloc: free/cache page %p was dirty", m));
984263bc
MD
848
849 /*
850 * Remove from free queue
851 */
984263bc
MD
852 vm_page_unqueue_nowakeup(m);
853
854 /*
9765affa
MD
855 * Initialize structure. Only the PG_ZERO flag is inherited. Set
856 * the page PG_BUSY
984263bc 857 */
984263bc
MD
858 if (m->flags & PG_ZERO) {
859 vm_page_zero_count--;
860 m->flags = PG_ZERO | PG_BUSY;
861 } else {
862 m->flags = PG_BUSY;
863 }
864 m->wire_count = 0;
865 m->hold_count = 0;
866 m->act_count = 0;
867 m->busy = 0;
868 m->valid = 0;
984263bc
MD
869
870 /*
080c00e6 871 * vm_page_insert() is safe while holding vm_token. Note also that
984263bc
MD
872 * inserting a page here does not insert it into the pmap (which
873 * could cause us to block allocating memory). We cannot block
874 * anywhere.
875 */
984263bc
MD
876 vm_page_insert(m, object, pindex);
877
878 /*
879 * Don't wakeup too often - wakeup the pageout daemon when
880 * we would be nearly out of memory.
881 */
20479584 882 pagedaemon_wakeup();
984263bc 883
9ad0147b 884 lwkt_reltoken(&vm_token);
9765affa
MD
885
886 /*
887 * A PG_BUSY page is returned.
888 */
984263bc
MD
889 return (m);
890}
891
892/*
163f8d24
MD
893 * Wait for sufficient free memory for nominal heavy memory use kernel
894 * operations.
895 */
896void
897vm_wait_nominal(void)
898{
899 while (vm_page_count_min(0))
900 vm_wait(0);
901}
902
903/*
12052253
MD
904 * Test if vm_wait_nominal() would block.
905 */
906int
907vm_test_nominal(void)
908{
909 if (vm_page_count_min(0))
910 return(1);
911 return(0);
912}
913
914/*
de71fd3f
MD
915 * Block until free pages are available for allocation, called in various
916 * places before memory allocations.
cd3c66bd
MD
917 *
918 * The caller may loop if vm_page_count_min() == FALSE so we cannot be
919 * more generous then that.
984263bc 920 */
984263bc 921void
4ecf7cc9 922vm_wait(int timo)
984263bc 923{
cd3c66bd
MD
924 /*
925 * never wait forever
926 */
927 if (timo == 0)
928 timo = hz;
9ad0147b 929 lwkt_gettoken(&vm_token);
cd3c66bd 930
bc6dffab 931 if (curthread == pagethread) {
cd3c66bd
MD
932 /*
933 * The pageout daemon itself needs pages, this is bad.
934 */
935 if (vm_page_count_min(0)) {
936 vm_pageout_pages_needed = 1;
937 tsleep(&vm_pageout_pages_needed, 0, "VMWait", timo);
938 }
984263bc 939 } else {
cd3c66bd
MD
940 /*
941 * Wakeup the pageout daemon if necessary and wait.
942 */
943 if (vm_page_count_target()) {
944 if (vm_pages_needed == 0) {
945 vm_pages_needed = 1;
946 wakeup(&vm_pages_needed);
947 }
948 ++vm_pages_waiting; /* SMP race ok */
949 tsleep(&vmstats.v_free_count, 0, "vmwait", timo);
984263bc 950 }
984263bc 951 }
9ad0147b 952 lwkt_reltoken(&vm_token);
984263bc
MD
953}
954
955/*
de71fd3f
MD
956 * Block until free pages are available for allocation
957 *
cd3c66bd 958 * Called only from vm_fault so that processes page faulting can be
de71fd3f 959 * easily tracked.
984263bc 960 */
984263bc
MD
961void
962vm_waitpfault(void)
963{
cd3c66bd
MD
964 /*
965 * Wakeup the pageout daemon if necessary and wait.
966 */
967 if (vm_page_count_target()) {
968 lwkt_gettoken(&vm_token);
969 if (vm_page_count_target()) {
970 if (vm_pages_needed == 0) {
971 vm_pages_needed = 1;
972 wakeup(&vm_pages_needed);
973 }
974 ++vm_pages_waiting; /* SMP race ok */
975 tsleep(&vmstats.v_free_count, 0, "pfault", hz);
976 }
977 lwkt_reltoken(&vm_token);
984263bc 978 }
984263bc
MD
979}
980
981/*
de71fd3f
MD
982 * Put the specified page on the active list (if appropriate). Ensure
983 * that act_count is at least ACT_INIT but do not otherwise mess with it.
984263bc 984 *
de71fd3f
MD
985 * The page queues must be locked.
986 * This routine may not block.
984263bc
MD
987 */
988void
989vm_page_activate(vm_page_t m)
990{
9ad0147b 991 lwkt_gettoken(&vm_token);
984263bc
MD
992 if (m->queue != PQ_ACTIVE) {
993 if ((m->queue - m->pc) == PQ_CACHE)
12e4aaff 994 mycpu->gd_cnt.v_reactivated++;
984263bc
MD
995
996 vm_page_unqueue(m);
997
998 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
999 m->queue = PQ_ACTIVE;
1000 vm_page_queues[PQ_ACTIVE].lcnt++;
de71fd3f
MD
1001 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl,
1002 m, pageq);
984263bc
MD
1003 if (m->act_count < ACT_INIT)
1004 m->act_count = ACT_INIT;
12e4aaff 1005 vmstats.v_active_count++;
984263bc
MD
1006 }
1007 } else {
1008 if (m->act_count < ACT_INIT)
1009 m->act_count = ACT_INIT;
1010 }
9ad0147b 1011 lwkt_reltoken(&vm_token);
984263bc
MD
1012}
1013
1014/*
de71fd3f
MD
1015 * Helper routine for vm_page_free_toq() and vm_page_cache(). This
1016 * routine is called when a page has been added to the cache or free
1017 * queues.
984263bc 1018 *
de71fd3f
MD
1019 * This routine may not block.
1020 * This routine must be called at splvm()
984263bc
MD
1021 */
1022static __inline void
1023vm_page_free_wakeup(void)
1024{
1025 /*
cd3c66bd
MD
1026 * If the pageout daemon itself needs pages, then tell it that
1027 * there are some free.
984263bc
MD
1028 */
1029 if (vm_pageout_pages_needed &&
de71fd3f
MD
1030 vmstats.v_cache_count + vmstats.v_free_count >=
1031 vmstats.v_pageout_free_min
1032 ) {
984263bc
MD
1033 wakeup(&vm_pageout_pages_needed);
1034 vm_pageout_pages_needed = 0;
1035 }
de71fd3f 1036
984263bc 1037 /*
cd3c66bd
MD
1038 * Wakeup processes that are waiting on memory.
1039 *
1040 * NOTE: vm_paging_target() is the pageout daemon's target, while
1041 * vm_page_count_target() is somewhere inbetween. We want
1042 * to wake processes up prior to the pageout daemon reaching
1043 * its target to provide some hysteresis.
984263bc 1044 */
cd3c66bd
MD
1045 if (vm_pages_waiting) {
1046 if (!vm_page_count_target()) {
1047 /*
1048 * Plenty of pages are free, wakeup everyone.
1049 */
1050 vm_pages_waiting = 0;
1051 wakeup(&vmstats.v_free_count);
1052 ++mycpu->gd_cnt.v_ppwakeups;
1053 } else if (!vm_page_count_min(0)) {
1054 /*
1055 * Some pages are free, wakeup someone.
1056 */
1057 int wcount = vm_pages_waiting;
1058 if (wcount > 0)
1059 --wcount;
1060 vm_pages_waiting = wcount;
1061 wakeup_one(&vmstats.v_free_count);
1062 ++mycpu->gd_cnt.v_ppwakeups;
1063 }
984263bc
MD
1064 }
1065}
1066
1067/*
1068 * vm_page_free_toq:
1069 *
9765affa
MD
1070 * Returns the given page to the PQ_FREE list, disassociating it with
1071 * any VM object.
1072 *
1073 * The vm_page must be PG_BUSY on entry. PG_BUSY will be released on
1074 * return (the page will have been freed). No particular spl is required
1075 * on entry.
984263bc 1076 *
984263bc
MD
1077 * This routine may not block.
1078 */
984263bc
MD
1079void
1080vm_page_free_toq(vm_page_t m)
1081{
984263bc 1082 struct vpgqueues *pq;
984263bc 1083
9ad0147b 1084 lwkt_gettoken(&vm_token);
12e4aaff 1085 mycpu->gd_cnt.v_tfree++;
984263bc 1086
17cde63e
MD
1087 KKASSERT((m->flags & PG_MAPPED) == 0);
1088
984263bc 1089 if (m->busy || ((m->queue - m->pc) == PQ_FREE)) {
086c1d7e 1090 kprintf(
984263bc
MD
1091 "vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n",
1092 (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0,
1093 m->hold_count);
1094 if ((m->queue - m->pc) == PQ_FREE)
1095 panic("vm_page_free: freeing free page");
1096 else
1097 panic("vm_page_free: freeing busy page");
1098 }
1099
1100 /*
1101 * unqueue, then remove page. Note that we cannot destroy
1102 * the page here because we do not want to call the pager's
1103 * callback routine until after we've put the page on the
1104 * appropriate free queue.
1105 */
984263bc
MD
1106 vm_page_unqueue_nowakeup(m);
1107 vm_page_remove(m);
1108
1109 /*
f2d22ebf
MD
1110 * No further management of fictitious pages occurs beyond object
1111 * and queue removal.
984263bc 1112 */
984263bc 1113 if ((m->flags & PG_FICTITIOUS) != 0) {
9765affa 1114 vm_page_wakeup(m);
9ad0147b 1115 lwkt_reltoken(&vm_token);
984263bc
MD
1116 return;
1117 }
1118
1119 m->valid = 0;
1120 vm_page_undirty(m);
1121
1122 if (m->wire_count != 0) {
1123 if (m->wire_count > 1) {
de71fd3f
MD
1124 panic(
1125 "vm_page_free: invalid wire count (%d), pindex: 0x%lx",
1126 m->wire_count, (long)m->pindex);
984263bc 1127 }
73c351d1 1128 panic("vm_page_free: freeing wired page");
984263bc
MD
1129 }
1130
1131 /*
984263bc
MD
1132 * Clear the UNMANAGED flag when freeing an unmanaged page.
1133 */
984263bc 1134 if (m->flags & PG_UNMANAGED) {
d0aa00e8 1135 vm_page_flag_clear(m, PG_UNMANAGED);
984263bc
MD
1136 }
1137
1138 if (m->hold_count != 0) {
d0aa00e8 1139 vm_page_flag_clear(m, PG_ZERO);
984263bc 1140 m->queue = PQ_HOLD;
de71fd3f 1141 } else {
984263bc 1142 m->queue = PQ_FREE + m->pc;
de71fd3f 1143 }
984263bc
MD
1144 pq = &vm_page_queues[m->queue];
1145 pq->lcnt++;
1146 ++(*pq->cnt);
1147
1148 /*
1149 * Put zero'd pages on the end ( where we look for zero'd pages
1150 * first ) and non-zerod pages at the head.
1151 */
984263bc
MD
1152 if (m->flags & PG_ZERO) {
1153 TAILQ_INSERT_TAIL(&pq->pl, m, pageq);
1154 ++vm_page_zero_count;
1155 } else {
1156 TAILQ_INSERT_HEAD(&pq->pl, m, pageq);
1157 }
9765affa 1158 vm_page_wakeup(m);
984263bc 1159 vm_page_free_wakeup();
9ad0147b 1160 lwkt_reltoken(&vm_token);
984263bc
MD
1161}
1162
1163/*
bb6811be
MD
1164 * vm_page_free_fromq_fast()
1165 *
1166 * Remove a non-zero page from one of the free queues; the page is removed for
1167 * zeroing, so do not issue a wakeup.
1168 *
1169 * MPUNSAFE
1170 */
1171vm_page_t
1172vm_page_free_fromq_fast(void)
1173{
1174 static int qi;
1175 vm_page_t m;
1176 int i;
1177
9ad0147b 1178 lwkt_gettoken(&vm_token);
bb6811be
MD
1179 for (i = 0; i < PQ_L2_SIZE; ++i) {
1180 m = vm_page_list_find(PQ_FREE, qi, FALSE);
1181 qi = (qi + PQ_PRIME2) & PQ_L2_MASK;
1182 if (m && (m->flags & PG_ZERO) == 0) {
080c00e6 1183 KKASSERT(m->busy == 0 && (m->flags & PG_BUSY) == 0);
bb6811be
MD
1184 vm_page_unqueue_nowakeup(m);
1185 vm_page_busy(m);
1186 break;
1187 }
1188 m = NULL;
1189 }
9ad0147b 1190 lwkt_reltoken(&vm_token);
bb6811be
MD
1191 return (m);
1192}
1193
1194/*
de71fd3f
MD
1195 * vm_page_unmanage()
1196 *
1197 * Prevent PV management from being done on the page. The page is
1198 * removed from the paging queues as if it were wired, and as a
1199 * consequence of no longer being managed the pageout daemon will not
1200 * touch it (since there is no way to locate the pte mappings for the
1201 * page). madvise() calls that mess with the pmap will also no longer
1202 * operate on the page.
1203 *
1204 * Beyond that the page is still reasonably 'normal'. Freeing the page
1205 * will clear the flag.
1206 *
1207 * This routine is used by OBJT_PHYS objects - objects using unswappable
1208 * physical memory as backing store rather then swap-backed memory and
1209 * will eventually be extended to support 4MB unmanaged physical
1210 * mappings.
654a39f0
MD
1211 *
1212 * Must be called with a critical section held.
573fb415 1213 * Must be called with vm_token held.
984263bc 1214 */
984263bc
MD
1215void
1216vm_page_unmanage(vm_page_t m)
1217{
573fb415 1218 ASSERT_LWKT_TOKEN_HELD(&vm_token);
984263bc
MD
1219 if ((m->flags & PG_UNMANAGED) == 0) {
1220 if (m->wire_count == 0)
1221 vm_page_unqueue(m);
1222 }
1223 vm_page_flag_set(m, PG_UNMANAGED);
984263bc
MD
1224}
1225
1226/*
de71fd3f
MD
1227 * Mark this page as wired down by yet another map, removing it from
1228 * paging queues as necessary.
984263bc 1229 *
de71fd3f
MD
1230 * The page queues must be locked.
1231 * This routine may not block.
984263bc
MD
1232 */
1233void
1234vm_page_wire(vm_page_t m)
1235{
984263bc
MD
1236 /*
1237 * Only bump the wire statistics if the page is not already wired,
1238 * and only unqueue the page if it is on some queue (if it is unmanaged
f2d22ebf
MD
1239 * it is already off the queues). Don't do anything with fictitious
1240 * pages because they are always wired.
984263bc 1241 */
9ad0147b 1242 lwkt_gettoken(&vm_token);
f2d22ebf
MD
1243 if ((m->flags & PG_FICTITIOUS) == 0) {
1244 if (m->wire_count == 0) {
1245 if ((m->flags & PG_UNMANAGED) == 0)
1246 vm_page_unqueue(m);
1247 vmstats.v_wire_count++;
1248 }
1249 m->wire_count++;
1250 KASSERT(m->wire_count != 0,
17cde63e 1251 ("vm_page_wire: wire_count overflow m=%p", m));
984263bc 1252 }
9ad0147b 1253 lwkt_reltoken(&vm_token);
984263bc
MD
1254}
1255
1256/*
de71fd3f
MD
1257 * Release one wiring of this page, potentially enabling it to be paged again.
1258 *
1259 * Many pages placed on the inactive queue should actually go
1260 * into the cache, but it is difficult to figure out which. What
1261 * we do instead, if the inactive target is well met, is to put
1262 * clean pages at the head of the inactive queue instead of the tail.
1263 * This will cause them to be moved to the cache more quickly and
1264 * if not actively re-referenced, freed more quickly. If we just
1265 * stick these pages at the end of the inactive queue, heavy filesystem
1266 * meta-data accesses can cause an unnecessary paging load on memory bound
1267 * processes. This optimization causes one-time-use metadata to be
1268 * reused more quickly.
1269 *
1270 * BUT, if we are in a low-memory situation we have no choice but to
1271 * put clean pages on the cache queue.
1272 *
1273 * A number of routines use vm_page_unwire() to guarantee that the page
1274 * will go into either the inactive or active queues, and will NEVER
1275 * be placed in the cache - for example, just after dirtying a page.
1276 * dirty pages in the cache are not allowed.
1277 *
1278 * The page queues must be locked.
1279 * This routine may not block.
984263bc
MD
1280 */
1281void
1282vm_page_unwire(vm_page_t m, int activate)
1283{
9ad0147b 1284 lwkt_gettoken(&vm_token);
f2d22ebf
MD
1285 if (m->flags & PG_FICTITIOUS) {
1286 /* do nothing */
1287 } else if (m->wire_count <= 0) {
1288 panic("vm_page_unwire: invalid wire count: %d", m->wire_count);
1289 } else {
1290 if (--m->wire_count == 0) {
1291 --vmstats.v_wire_count;
984263bc
MD
1292 if (m->flags & PG_UNMANAGED) {
1293 ;
1294 } else if (activate) {
f2d22ebf
MD
1295 TAILQ_INSERT_TAIL(
1296 &vm_page_queues[PQ_ACTIVE].pl, m, pageq);
984263bc
MD
1297 m->queue = PQ_ACTIVE;
1298 vm_page_queues[PQ_ACTIVE].lcnt++;
12e4aaff 1299 vmstats.v_active_count++;
984263bc
MD
1300 } else {
1301 vm_page_flag_clear(m, PG_WINATCFLS);
f2d22ebf
MD
1302 TAILQ_INSERT_TAIL(
1303 &vm_page_queues[PQ_INACTIVE].pl, m, pageq);
984263bc
MD
1304 m->queue = PQ_INACTIVE;
1305 vm_page_queues[PQ_INACTIVE].lcnt++;
12e4aaff 1306 vmstats.v_inactive_count++;
e527fb6b 1307 ++vm_swapcache_inactive_heuristic;
984263bc
MD
1308 }
1309 }
984263bc 1310 }
9ad0147b 1311 lwkt_reltoken(&vm_token);
984263bc
MD
1312}
1313
1314
1315/*
1316 * Move the specified page to the inactive queue. If the page has
1317 * any associated swap, the swap is deallocated.
1318 *
1319 * Normally athead is 0 resulting in LRU operation. athead is set
1320 * to 1 if we want this page to be 'as if it were placed in the cache',
1321 * except without unmapping it from the process address space.
1322 *
1323 * This routine may not block.
573fb415 1324 * The caller must hold vm_token.
984263bc
MD
1325 */
1326static __inline void
1327_vm_page_deactivate(vm_page_t m, int athead)
1328{
984263bc
MD
1329 /*
1330 * Ignore if already inactive.
1331 */
1332 if (m->queue == PQ_INACTIVE)
1333 return;
1334
984263bc
MD
1335 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1336 if ((m->queue - m->pc) == PQ_CACHE)
12e4aaff 1337 mycpu->gd_cnt.v_reactivated++;
984263bc
MD
1338 vm_page_flag_clear(m, PG_WINATCFLS);
1339 vm_page_unqueue(m);
e527fb6b
MD
1340 if (athead) {
1341 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl,
1342 m, pageq);
1343 } else {
1344 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl,
1345 m, pageq);
1346 ++vm_swapcache_inactive_heuristic;
1347 }
984263bc
MD
1348 m->queue = PQ_INACTIVE;
1349 vm_page_queues[PQ_INACTIVE].lcnt++;
12e4aaff 1350 vmstats.v_inactive_count++;
984263bc 1351 }
984263bc
MD
1352}
1353
573fb415
MD
1354/*
1355 * Attempt to deactivate a page.
1356 *
1357 * No requirements.
1358 */
984263bc
MD
1359void
1360vm_page_deactivate(vm_page_t m)
1361{
573fb415
MD
1362 lwkt_gettoken(&vm_token);
1363 _vm_page_deactivate(m, 0);
1364 lwkt_reltoken(&vm_token);
984263bc
MD
1365}
1366
1367/*
573fb415 1368 * Attempt to move a page to PQ_CACHE.
984263bc 1369 * Returns 0 on failure, 1 on success
573fb415
MD
1370 *
1371 * No requirements.
984263bc
MD
1372 */
1373int
1374vm_page_try_to_cache(vm_page_t m)
1375{
9ad0147b 1376 lwkt_gettoken(&vm_token);
984263bc
MD
1377 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1378 (m->flags & (PG_BUSY|PG_UNMANAGED))) {
9ad0147b 1379 lwkt_reltoken(&vm_token);
984263bc
MD
1380 return(0);
1381 }
a491077e 1382 vm_page_busy(m);
984263bc 1383 vm_page_test_dirty(m);
654a39f0 1384 if (m->dirty) {
50e32333 1385 vm_page_wakeup(m);
9ad0147b 1386 lwkt_reltoken(&vm_token);
984263bc 1387 return(0);
654a39f0 1388 }
984263bc 1389 vm_page_cache(m);
9ad0147b 1390 lwkt_reltoken(&vm_token);
984263bc
MD
1391 return(1);
1392}
1393
1394/*
de71fd3f
MD
1395 * Attempt to free the page. If we cannot free it, we do nothing.
1396 * 1 is returned on success, 0 on failure.
573fb415
MD
1397 *
1398 * No requirements.
984263bc 1399 */
984263bc
MD
1400int
1401vm_page_try_to_free(vm_page_t m)
1402{
9ad0147b 1403 lwkt_gettoken(&vm_token);
984263bc
MD
1404 if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1405 (m->flags & (PG_BUSY|PG_UNMANAGED))) {
9ad0147b 1406 lwkt_reltoken(&vm_token);
984263bc
MD
1407 return(0);
1408 }
1409 vm_page_test_dirty(m);
654a39f0 1410 if (m->dirty) {
9ad0147b 1411 lwkt_reltoken(&vm_token);
984263bc 1412 return(0);
654a39f0 1413 }
984263bc
MD
1414 vm_page_busy(m);
1415 vm_page_protect(m, VM_PROT_NONE);
1416 vm_page_free(m);
9ad0147b 1417 lwkt_reltoken(&vm_token);
984263bc
MD
1418 return(1);
1419}
1420
984263bc
MD
1421/*
1422 * vm_page_cache
1423 *
1424 * Put the specified page onto the page cache queue (if appropriate).
1425 *
573fb415 1426 * The caller must hold vm_token.
984263bc 1427 * This routine may not block.
a491077e
MD
1428 * The page must be busy, and this routine will release the busy and
1429 * possibly even free the page.
984263bc
MD
1430 */
1431void
1432vm_page_cache(vm_page_t m)
1433{
573fb415 1434 ASSERT_LWKT_TOKEN_HELD(&vm_token);
984263bc 1435
a491077e
MD
1436 if ((m->flags & PG_UNMANAGED) || m->busy ||
1437 m->wire_count || m->hold_count) {
086c1d7e 1438 kprintf("vm_page_cache: attempting to cache busy/held page\n");
a491077e 1439 vm_page_wakeup(m);
984263bc
MD
1440 return;
1441 }
c9ec86b3
MD
1442
1443 /*
1444 * Already in the cache (and thus not mapped)
1445 */
17cde63e
MD
1446 if ((m->queue - m->pc) == PQ_CACHE) {
1447 KKASSERT((m->flags & PG_MAPPED) == 0);
a491077e 1448 vm_page_wakeup(m);
984263bc 1449 return;
17cde63e 1450 }
984263bc
MD
1451
1452 /*
c9ec86b3
MD
1453 * Caller is required to test m->dirty, but note that the act of
1454 * removing the page from its maps can cause it to become dirty
1455 * on an SMP system due to another cpu running in usermode.
984263bc 1456 */
c9ec86b3 1457 if (m->dirty) {
984263bc
MD
1458 panic("vm_page_cache: caching a dirty page, pindex: %ld",
1459 (long)m->pindex);
1460 }
c9ec86b3
MD
1461
1462 /*
1463 * Remove all pmaps and indicate that the page is not
17cde63e
MD
1464 * writeable or mapped. Our vm_page_protect() call may
1465 * have blocked (especially w/ VM_PROT_NONE), so recheck
1466 * everything.
c9ec86b3
MD
1467 */
1468 vm_page_protect(m, VM_PROT_NONE);
a491077e 1469 if ((m->flags & (PG_UNMANAGED|PG_MAPPED)) || m->busy ||
17cde63e 1470 m->wire_count || m->hold_count) {
a491077e 1471 vm_page_wakeup(m);
17cde63e 1472 } else if (m->dirty) {
c9ec86b3 1473 vm_page_deactivate(m);
a491077e 1474 vm_page_wakeup(m);
c9ec86b3
MD
1475 } else {
1476 vm_page_unqueue_nowakeup(m);
1477 m->queue = PQ_CACHE + m->pc;
1478 vm_page_queues[m->queue].lcnt++;
1479 TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m, pageq);
1480 vmstats.v_cache_count++;
a491077e 1481 vm_page_wakeup(m);
c9ec86b3
MD
1482 vm_page_free_wakeup();
1483 }
984263bc
MD
1484}
1485
1486/*
de71fd3f
MD
1487 * vm_page_dontneed()
1488 *
1489 * Cache, deactivate, or do nothing as appropriate. This routine
1490 * is typically used by madvise() MADV_DONTNEED.
1491 *
1492 * Generally speaking we want to move the page into the cache so
1493 * it gets reused quickly. However, this can result in a silly syndrome
1494 * due to the page recycling too quickly. Small objects will not be
1495 * fully cached. On the otherhand, if we move the page to the inactive
1496 * queue we wind up with a problem whereby very large objects
1497 * unnecessarily blow away our inactive and cache queues.
1498 *
1499 * The solution is to move the pages based on a fixed weighting. We
1500 * either leave them alone, deactivate them, or move them to the cache,
1501 * where moving them to the cache has the highest weighting.
1502 * By forcing some pages into other queues we eventually force the
1503 * system to balance the queues, potentially recovering other unrelated
1504 * space from active. The idea is to not force this to happen too
1505 * often.
573fb415
MD
1506 *
1507 * No requirements.
984263bc 1508 */
984263bc
MD
1509void
1510vm_page_dontneed(vm_page_t m)
1511{
1512 static int dnweight;
1513 int dnw;
1514 int head;
1515
1516 dnw = ++dnweight;
1517
1518 /*
1519 * occassionally leave the page alone
1520 */
9ad0147b 1521 lwkt_gettoken(&vm_token);
984263bc
MD
1522 if ((dnw & 0x01F0) == 0 ||
1523 m->queue == PQ_INACTIVE ||
1524 m->queue - m->pc == PQ_CACHE
1525 ) {
1526 if (m->act_count >= ACT_INIT)
1527 --m->act_count;
9ad0147b 1528 lwkt_reltoken(&vm_token);
984263bc
MD
1529 return;
1530 }
1531
31da5e4d
VS
1532 /*
1533 * If vm_page_dontneed() is inactivating a page, it must clear
1534 * the referenced flag; otherwise the pagedaemon will see references
1535 * on the page in the inactive queue and reactivate it. Until the
1536 * page can move to the cache queue, madvise's job is not done.
1537 */
1538 vm_page_flag_clear(m, PG_REFERENCED);
1539 pmap_clear_reference(m);
1540
984263bc
MD
1541 if (m->dirty == 0)
1542 vm_page_test_dirty(m);
1543
1544 if (m->dirty || (dnw & 0x0070) == 0) {
1545 /*
1546 * Deactivate the page 3 times out of 32.
1547 */
1548 head = 0;
1549 } else {
1550 /*
1551 * Cache the page 28 times out of every 32. Note that
1552 * the page is deactivated instead of cached, but placed
1553 * at the head of the queue instead of the tail.
1554 */
1555 head = 1;
1556 }
1557 _vm_page_deactivate(m, head);
9ad0147b 1558 lwkt_reltoken(&vm_token);
984263bc
MD
1559}
1560
1561/*
06ecca5a
MD
1562 * Grab a page, blocking if it is busy and allocating a page if necessary.
1563 * A busy page is returned or NULL.
984263bc 1564 *
dc1fd4b3 1565 * If VM_ALLOC_RETRY is specified VM_ALLOC_NORMAL must also be specified.
06ecca5a 1566 * If VM_ALLOC_RETRY is not specified
dc1fd4b3 1567 *
06ecca5a
MD
1568 * This routine may block, but if VM_ALLOC_RETRY is not set then NULL is
1569 * always returned if we had blocked.
1570 * This routine will never return NULL if VM_ALLOC_RETRY is set.
1571 * This routine may not be called from an interrupt.
1572 * The returned page may not be entirely valid.
1573 *
1574 * This routine may be called from mainline code without spl protection and
1575 * be guarenteed a busied page associated with the object at the specified
1576 * index.
573fb415
MD
1577 *
1578 * No requirements.
984263bc
MD
1579 */
1580vm_page_t
1581vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
1582{
984263bc 1583 vm_page_t m;
654a39f0 1584 int generation;
984263bc 1585
dc1fd4b3
MD
1586 KKASSERT(allocflags &
1587 (VM_ALLOC_NORMAL|VM_ALLOC_INTERRUPT|VM_ALLOC_SYSTEM));
9ad0147b 1588 lwkt_gettoken(&vm_token);
398c240d 1589 vm_object_hold(object);
984263bc
MD
1590retrylookup:
1591 if ((m = vm_page_lookup(object, pindex)) != NULL) {
1592 if (m->busy || (m->flags & PG_BUSY)) {
1593 generation = object->generation;
1594
984263bc
MD
1595 while ((object->generation == generation) &&
1596 (m->busy || (m->flags & PG_BUSY))) {
1597 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
377d4740 1598 tsleep(m, 0, "pgrbwt", 0);
984263bc 1599 if ((allocflags & VM_ALLOC_RETRY) == 0) {
06ecca5a
MD
1600 m = NULL;
1601 goto done;
984263bc
MD
1602 }
1603 }
984263bc
MD
1604 goto retrylookup;
1605 } else {
1606 vm_page_busy(m);
06ecca5a 1607 goto done;
984263bc
MD
1608 }
1609 }
984263bc
MD
1610 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY);
1611 if (m == NULL) {
4ecf7cc9 1612 vm_wait(0);
984263bc 1613 if ((allocflags & VM_ALLOC_RETRY) == 0)
06ecca5a 1614 goto done;
984263bc
MD
1615 goto retrylookup;
1616 }
06ecca5a 1617done:
398c240d 1618 vm_object_drop(object);
9ad0147b 1619 lwkt_reltoken(&vm_token);
06ecca5a 1620 return(m);
984263bc
MD
1621}
1622
1623/*
1624 * Mapping function for valid bits or for dirty bits in
1625 * a page. May not block.
1626 *
1627 * Inputs are required to range within a page.
573fb415
MD
1628 *
1629 * No requirements.
1630 * Non blocking.
984263bc 1631 */
573fb415 1632int
984263bc
MD
1633vm_page_bits(int base, int size)
1634{
1635 int first_bit;
1636 int last_bit;
1637
1638 KASSERT(
1639 base + size <= PAGE_SIZE,
1640 ("vm_page_bits: illegal base/size %d/%d", base, size)
1641 );
1642
1643 if (size == 0) /* handle degenerate case */
1644 return(0);
1645
1646 first_bit = base >> DEV_BSHIFT;
1647 last_bit = (base + size - 1) >> DEV_BSHIFT;
1648
1649 return ((2 << last_bit) - (1 << first_bit));
1650}
1651
1652/*
de71fd3f
MD
1653 * Sets portions of a page valid and clean. The arguments are expected
1654 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
1655 * of any partial chunks touched by the range. The invalid portion of
1656 * such chunks will be zero'd.
984263bc 1657 *
c7841cbe
MD
1658 * NOTE: When truncating a buffer vnode_pager_setsize() will automatically
1659 * align base to DEV_BSIZE so as not to mark clean a partially
1660 * truncated device block. Otherwise the dirty page status might be
1661 * lost.
1662 *
de71fd3f 1663 * This routine may not block.
984263bc 1664 *
de71fd3f 1665 * (base + size) must be less then or equal to PAGE_SIZE.
984263bc 1666 */
1a54183b
MD
1667static void
1668_vm_page_zero_valid(vm_page_t m, int base, int size)
984263bc 1669{
984263bc
MD
1670 int frag;
1671 int endoff;
1672
1673 if (size == 0) /* handle degenerate case */
1674 return;
1675
1676 /*
1677 * If the base is not DEV_BSIZE aligned and the valid
1678 * bit is clear, we have to zero out a portion of the
1679 * first block.
1680 */
1681
1682 if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
1683 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0
1684 ) {
1685 pmap_zero_page_area(
1686 VM_PAGE_TO_PHYS(m),
1687 frag,
1688 base - frag
1689 );
1690 }
1691
1692 /*
1693 * If the ending offset is not DEV_BSIZE aligned and the
1694 * valid bit is clear, we have to zero out a portion of
1695 * the last block.
1696 */
1697
1698 endoff = base + size;
1699
1700 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
1701 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0
1702 ) {
1703 pmap_zero_page_area(
1704 VM_PAGE_TO_PHYS(m),
1705 endoff,
1706 DEV_BSIZE - (endoff & (DEV_BSIZE - 1))
1707 );
1708 }
1a54183b 1709}
984263bc 1710
1a54183b
MD
1711/*
1712 * Set valid, clear dirty bits. If validating the entire
1713 * page we can safely clear the pmap modify bit. We also
1714 * use this opportunity to clear the PG_NOSYNC flag. If a process
1715 * takes a write fault on a MAP_NOSYNC memory area the flag will
1716 * be set again.
1717 *
1718 * We set valid bits inclusive of any overlap, but we can only
1719 * clear dirty bits for DEV_BSIZE chunks that are fully within
1720 * the range.
573fb415
MD
1721 *
1722 * Page must be busied?
1723 * No other requirements.
1a54183b
MD
1724 */
1725void
1726vm_page_set_valid(vm_page_t m, int base, int size)
1727{
1728 _vm_page_zero_valid(m, base, size);
1729 m->valid |= vm_page_bits(base, size);
1730}
984263bc 1731
cb1cf930
MD
1732
1733/*
1734 * Set valid bits and clear dirty bits.
1735 *
1736 * NOTE: This function does not clear the pmap modified bit.
1737 * Also note that e.g. NFS may use a byte-granular base
1738 * and size.
573fb415 1739 *
9a0cb7b1
MD
1740 * WARNING: Page must be busied? But vfs_clean_one_page() will call
1741 * this without necessarily busying the page (via bdwrite()).
1742 * So for now vm_token must also be held.
1743 *
573fb415 1744 * No other requirements.
cb1cf930 1745 */
1a54183b
MD
1746void
1747vm_page_set_validclean(vm_page_t m, int base, int size)
1748{
1749 int pagebits;
1750
1751 _vm_page_zero_valid(m, base, size);
984263bc
MD
1752 pagebits = vm_page_bits(base, size);
1753 m->valid |= pagebits;
984263bc
MD
1754 m->dirty &= ~pagebits;
1755 if (base == 0 && size == PAGE_SIZE) {
cb1cf930 1756 /*pmap_clear_modify(m);*/
984263bc
MD
1757 vm_page_flag_clear(m, PG_NOSYNC);
1758 }
1759}
1760
cb1cf930 1761/*
0a8aee15 1762 * Set valid & dirty. Used by buwrite()
573fb415 1763 *
9a0cb7b1
MD
1764 * WARNING: Page must be busied? But vfs_dirty_one_page() will
1765 * call this function in buwrite() so for now vm_token must
1766 * be held.
1767 *
573fb415 1768 * No other requirements.
0a8aee15
MD
1769 */
1770void
1771vm_page_set_validdirty(vm_page_t m, int base, int size)
1772{
1773 int pagebits;
1774
1775 pagebits = vm_page_bits(base, size);
1776 m->valid |= pagebits;
1777 m->dirty |= pagebits;
d89ce96a
MD
1778 if (m->object)
1779 vm_object_set_writeable_dirty(m->object);
0a8aee15
MD
1780}
1781
1782/*
cb1cf930
MD
1783 * Clear dirty bits.
1784 *
1785 * NOTE: This function does not clear the pmap modified bit.
1786 * Also note that e.g. NFS may use a byte-granular base
1787 * and size.
573fb415
MD
1788 *
1789 * Page must be busied?
1790 * No other requirements.
cb1cf930 1791 */
984263bc
MD
1792void
1793vm_page_clear_dirty(vm_page_t m, int base, int size)
1794{
1795 m->dirty &= ~vm_page_bits(base, size);
1a54183b 1796 if (base == 0 && size == PAGE_SIZE) {
cb1cf930 1797 /*pmap_clear_modify(m);*/
1a54183b
MD
1798 vm_page_flag_clear(m, PG_NOSYNC);
1799 }
984263bc
MD
1800}
1801
1802/*
17cde63e
MD
1803 * Make the page all-dirty.
1804 *
1805 * Also make sure the related object and vnode reflect the fact that the
1806 * object may now contain a dirty page.
573fb415
MD
1807 *
1808 * Page must be busied?
1809 * No other requirements.
17cde63e
MD
1810 */
1811void
1812vm_page_dirty(vm_page_t m)
1813{
1814#ifdef INVARIANTS
1815 int pqtype = m->queue - m->pc;
1816#endif
1817 KASSERT(pqtype != PQ_CACHE && pqtype != PQ_FREE,
1818 ("vm_page_dirty: page in free/cache queue!"));
1819 if (m->dirty != VM_PAGE_BITS_ALL) {
1820 m->dirty = VM_PAGE_BITS_ALL;
1821 if (m->object)
1822 vm_object_set_writeable_dirty(m->object);
1823 }
1824}
1825
1826/*
de71fd3f
MD
1827 * Invalidates DEV_BSIZE'd chunks within a page. Both the
1828 * valid and dirty bits for the effected areas are cleared.
984263bc 1829 *
573fb415
MD
1830 * Page must be busied?
1831 * Does not block.
1832 * No other requirements.
984263bc
MD
1833 */
1834void
1835vm_page_set_invalid(vm_page_t m, int base, int size)
1836{
1837 int bits;
1838
1839 bits = vm_page_bits(base, size);
1840 m->valid &= ~bits;
1841 m->dirty &= ~bits;
1842 m->object->generation++;
1843}
1844
1845/*
de71fd3f
MD
1846 * The kernel assumes that the invalid portions of a page contain
1847 * garbage, but such pages can be mapped into memory by user code.
1848 * When this occurs, we must zero out the non-valid portions of the
1849 * page so user code sees what it expects.
984263bc 1850 *
de71fd3f
MD
1851 * Pages are most often semi-valid when the end of a file is mapped
1852 * into memory and the file's size is not page aligned.
573fb415
MD
1853 *
1854 * Page must be busied?
1855 * No other requirements.
984263bc 1856 */
984263bc
MD
1857void
1858vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
1859{
1860 int b;
1861 int i;
1862
1863 /*
1864 * Scan the valid bits looking for invalid sections that
1865 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the
1866 * valid bit may be set ) have already been zerod by
1867 * vm_page_set_validclean().
1868 */
984263bc
MD
1869 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
1870 if (i == (PAGE_SIZE / DEV_BSIZE) ||
1871 (m->valid & (1 << i))
1872 ) {
1873 if (i > b) {
1874 pmap_zero_page_area(
1875 VM_PAGE_TO_PHYS(m),
1876 b << DEV_BSHIFT,
1877 (i - b) << DEV_BSHIFT
1878 );
1879 }
1880 b = i + 1;
1881 }
1882 }
1883
1884 /*
1885 * setvalid is TRUE when we can safely set the zero'd areas
1886 * as being valid. We can do this if there are no cache consistency
1887 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS.
1888 */
984263bc
MD
1889 if (setvalid)
1890 m->valid = VM_PAGE_BITS_ALL;
1891}
1892
1893/*
de71fd3f
MD
1894 * Is a (partial) page valid? Note that the case where size == 0
1895 * will return FALSE in the degenerate case where the page is entirely
1896 * invalid, and TRUE otherwise.
984263bc 1897 *
573fb415
MD
1898 * Does not block.
1899 * No other requirements.
984263bc 1900 */
984263bc
MD
1901int
1902vm_page_is_valid(vm_page_t m, int base, int size)
1903{
1904 int bits = vm_page_bits(base, size);
1905
1906 if (m->valid && ((m->valid & bits) == bits))
1907 return 1;
1908 else
1909 return 0;
1910}
1911
1912/*
1913 * update dirty bits from pmap/mmu. May not block.
573fb415
MD
1914 *
1915 * Caller must hold vm_token if non-blocking operation desired.
1916 * No other requirements.
984263bc 1917 */
984263bc
MD
1918void
1919vm_page_test_dirty(vm_page_t m)
1920{
1921 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) {
1922 vm_page_dirty(m);
1923 }
1924}
1925
10192bae 1926/*
906c754c
MD
1927 * Register an action, associating it with its vm_page
1928 */
1929void
1930vm_page_register_action(vm_page_action_t action, vm_page_event_t event)
1931{
1932 struct vm_page_action_list *list;
1933 int hv;
1934
1935 hv = (int)((intptr_t)action->m >> 8) & VMACTION_HMASK;
1936 list = &action_list[hv];
1937
1938 lwkt_gettoken(&vm_token);
1939 vm_page_flag_set(action->m, PG_ACTIONLIST);
1940 action->event = event;
1941 LIST_INSERT_HEAD(list, action, entry);
1942 lwkt_reltoken(&vm_token);
1943}
1944
1945/*
1946 * Unregister an action, disassociating it from its related vm_page
1947 */
1948void
1949vm_page_unregister_action(vm_page_action_t action)
1950{
1951 struct vm_page_action_list *list;
1952 int hv;
1953
1954 lwkt_gettoken(&vm_token);
1955 if (action->event != VMEVENT_NONE) {
1956 action->event = VMEVENT_NONE;
1957 LIST_REMOVE(action, entry);
1958
1959 hv = (int)((intptr_t)action->m >> 8) & VMACTION_HMASK;
1960 list = &action_list[hv];
1961 if (LIST_EMPTY(list))
1962 vm_page_flag_clear(action->m, PG_ACTIONLIST);
1963 }
1964 lwkt_reltoken(&vm_token);
1965}
1966
1967/*
10192bae
MD
1968 * Issue an event on a VM page. Corresponding action structures are
1969 * removed from the page's list and called.
906c754c
MD
1970 *
1971 * If the vm_page has no more pending action events we clear its
1972 * PG_ACTIONLIST flag.
10192bae
MD
1973 */
1974void
1975vm_page_event_internal(vm_page_t m, vm_page_event_t event)
1976{
906c754c
MD
1977 struct vm_page_action_list *list;
1978 struct vm_page_action *scan;
1979 struct vm_page_action *next;
1980 int hv;
1981 int all;
10192bae 1982
906c754c
MD
1983 hv = (int)((intptr_t)m >> 8) & VMACTION_HMASK;
1984 list = &action_list[hv];
1985 all = 1;
1986
1987 lwkt_gettoken(&vm_token);
1988 LIST_FOREACH_MUTABLE(scan, list, entry, next) {
1989 if (scan->m == m) {
1990 if (scan->event == event) {
1991 scan->event = VMEVENT_NONE;
1992 LIST_REMOVE(scan, entry);
1993 scan->func(m, scan);
1994 /* XXX */
1995 } else {
1996 all = 0;
1997 }
10192bae
MD
1998 }
1999 }
906c754c
MD
2000 if (all)
2001 vm_page_flag_clear(m, PG_ACTIONLIST);
2002 lwkt_reltoken(&vm_token);
10192bae
MD
2003}
2004
984263bc
MD
2005#include "opt_ddb.h"
2006#ifdef DDB
2007#include <sys/kernel.h>
2008
2009#include <ddb/ddb.h>
2010
2011DB_SHOW_COMMAND(page, vm_page_print_page_info)
2012{
12e4aaff
MD
2013 db_printf("vmstats.v_free_count: %d\n", vmstats.v_free_count);
2014 db_printf("vmstats.v_cache_count: %d\n", vmstats.v_cache_count);
2015 db_printf("vmstats.v_inactive_count: %d\n", vmstats.v_inactive_count);
2016 db_printf("vmstats.v_active_count: %d\n", vmstats.v_active_count);
2017 db_printf("vmstats.v_wire_count: %d\n", vmstats.v_wire_count);
2018 db_printf("vmstats.v_free_reserved: %d\n", vmstats.v_free_reserved);
2019 db_printf("vmstats.v_free_min: %d\n", vmstats.v_free_min);
2020 db_printf("vmstats.v_free_target: %d\n", vmstats.v_free_target);
2021 db_printf("vmstats.v_cache_min: %d\n", vmstats.v_cache_min);
2022 db_printf("vmstats.v_inactive_target: %d\n", vmstats.v_inactive_target);
984263bc
MD
2023}
2024
2025DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
2026{
2027 int i;
2028 db_printf("PQ_FREE:");
2029 for(i=0;i<PQ_L2_SIZE;i++) {
2030 db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt);
2031 }
2032 db_printf("\n");
2033
2034 db_printf("PQ_CACHE:");
2035 for(i=0;i<PQ_L2_SIZE;i++) {
2036 db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt);
2037 }
2038 db_printf("\n");
2039
2040 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
2041 vm_page_queues[PQ_ACTIVE].lcnt,
2042 vm_page_queues[PQ_INACTIVE].lcnt);
2043}
2044#endif /* DDB */