2 * Copyright (c) 1991 Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91
36 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
37 * All rights reserved.
39 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41 * Permission to use, copy, modify and distribute this software and
42 * its documentation is hereby granted, provided that both the copyright
43 * notice and this permission notice appear in all copies of the
44 * software, derivative works or modified versions, and any portions
45 * thereof, and that both notices appear in supporting documentation.
47 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
48 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
49 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51 * Carnegie Mellon requests users of this software to return to
53 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
54 * School of Computer Science
55 * Carnegie Mellon University
56 * Pittsburgh PA 15213-3890
58 * any improvements or extensions that they make and grant Carnegie the
59 * rights to redistribute these changes.
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/eventhandler.h>
69 #include <sys/mount.h>
70 #include <sys/mutex.h>
72 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
74 #include <sys/vmmeter.h>
75 #include <sys/vnode.h>
78 #include <vm/vm_param.h>
79 #include <vm/vm_kern.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pageout.h>
85 #include <vm/vm_pager.h>
86 #include <vm/vm_phys.h>
87 #include <vm/vm_extern.h>
90 vm_contig_launder_page(vm_page_t m, vm_page_t *next, int tries)
98 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
99 if (!vm_pageout_page_lock(m, next) || m->hold_count != 0) {
104 if (!VM_OBJECT_TRYLOCK(object) &&
105 (!vm_pageout_fallback_object_lock(m, next) || m->hold_count != 0)) {
107 VM_OBJECT_UNLOCK(object);
110 if ((m->oflags & VPO_BUSY) != 0 || m->busy != 0) {
113 VM_OBJECT_UNLOCK(object);
116 vm_page_sleep(m, "vpctw0");
117 VM_OBJECT_UNLOCK(object);
118 vm_page_lock_queues();
121 vm_page_test_dirty(m);
126 if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
127 VM_OBJECT_UNLOCK(object);
130 if (object->type == OBJT_VNODE) {
131 vm_page_unlock_queues();
133 vm_object_reference_locked(object);
134 VM_OBJECT_UNLOCK(object);
135 (void) vn_start_write(vp, &mp, V_WAIT);
136 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
137 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
138 VM_OBJECT_LOCK(object);
139 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
140 VM_OBJECT_UNLOCK(object);
142 VFS_UNLOCK_GIANT(vfslocked);
143 vm_object_deallocate(object);
144 vn_finished_write(mp);
145 vm_page_lock_queues();
147 } else if (object->type == OBJT_SWAP ||
148 object->type == OBJT_DEFAULT) {
149 vm_page_unlock_queues();
151 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 0,
153 VM_OBJECT_UNLOCK(object);
154 vm_page_lock_queues();
161 VM_OBJECT_UNLOCK(object);
166 vm_contig_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high)
172 TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
173 KASSERT(m->queue == queue,
174 ("vm_contig_launder: page %p's queue is not %d", m, queue));
175 if ((m->flags & PG_MARKER) != 0)
177 pa = VM_PAGE_TO_PHYS(m);
178 if (pa < low || pa + PAGE_SIZE > high)
180 error = vm_contig_launder_page(m, &next, tries);
190 * Frees the given physically contiguous pages.
192 * N.B.: Any pages with PG_ZERO set must, in fact, be zero filled.
195 vm_page_release_contig(vm_page_t m, vm_pindex_t count)
199 /* Leave PG_ZERO unchanged. */
206 * Increase the number of cached pages. The specified value, "tries",
207 * determines which categories of pages are cached:
209 * 0: All clean, inactive pages within the specified physical address range
210 * are cached. Will not sleep.
211 * 1: The vm_lowmem handlers are called. All inactive pages within
212 * the specified physical address range are cached. May sleep.
213 * 2: The vm_lowmem handlers are called. All inactive and active pages
214 * within the specified physical address range are cached. May sleep.
217 vm_contig_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
219 int actl, actmax, inactl, inactmax;
223 * Decrease registered cache sizes. The vm_lowmem handlers
224 * may acquire locks and/or sleep, so they can only be invoked
225 * when "tries" is greater than zero.
227 EVENTHANDLER_INVOKE(vm_lowmem, 0);
230 * We do this explicitly after the caches have been drained
235 vm_page_lock_queues();
237 inactmax = cnt.v_inactive_count;
239 actmax = tries < 2 ? 0 : cnt.v_active_count;
241 if (inactl < inactmax && vm_contig_launder(PQ_INACTIVE, tries, low,
246 if (actl < actmax && vm_contig_launder(PQ_ACTIVE, tries, low, high)) {
250 vm_page_unlock_queues();
254 * Allocates a region from the kernel address map and pages within the
255 * specified physical address range to the kernel object, creates a wired
256 * mapping from the region to these pages, and returns the region's starting
257 * virtual address. The allocated pages are not necessarily physically
258 * contiguous. If M_ZERO is specified through the given flags, then the pages
259 * are zeroed before they are mapped.
262 kmem_alloc_attr(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
263 vm_paddr_t high, vm_memattr_t memattr)
265 vm_object_t object = kernel_object;
266 vm_offset_t addr, i, offset;
270 size = round_page(size);
272 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
276 offset = addr - VM_MIN_KERNEL_ADDRESS;
277 vm_object_reference(object);
278 vm_map_insert(map, object, offset, addr, addr + size, VM_PROT_ALL,
280 VM_OBJECT_LOCK(object);
281 for (i = 0; i < size; i += PAGE_SIZE) {
284 m = vm_phys_alloc_contig(1, low, high, PAGE_SIZE, 0);
286 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
287 VM_OBJECT_UNLOCK(object);
289 vm_contig_grow_cache(tries, low, high);
291 VM_OBJECT_LOCK(object);
297 m = vm_page_lookup(object, OFF_TO_IDX(offset +
301 VM_OBJECT_UNLOCK(object);
302 vm_map_delete(map, addr, addr + size);
306 if (memattr != VM_MEMATTR_DEFAULT)
307 pmap_page_set_memattr(m, memattr);
308 vm_page_insert(m, object, OFF_TO_IDX(offset + i));
309 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
311 m->valid = VM_PAGE_BITS_ALL;
313 VM_OBJECT_UNLOCK(object);
315 vm_map_wire(map, addr, addr + size, VM_MAP_WIRE_SYSTEM |
316 VM_MAP_WIRE_NOHOLES);
321 * Allocates a region from the kernel address map, inserts the
322 * given physically contiguous pages into the kernel object,
323 * creates a wired mapping from the region to the pages, and
324 * returns the region's starting virtual address. If M_ZERO is
325 * specified through the given flags, then the pages are zeroed
326 * before they are mapped.
329 contigmapping(vm_map_t map, vm_size_t size, vm_page_t m, vm_memattr_t memattr,
332 vm_object_t object = kernel_object;
333 vm_offset_t addr, tmp_addr;
336 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
340 vm_object_reference(object);
341 vm_map_insert(map, object, addr - VM_MIN_KERNEL_ADDRESS,
342 addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
344 VM_OBJECT_LOCK(object);
345 for (tmp_addr = addr; tmp_addr < addr + size; tmp_addr += PAGE_SIZE) {
346 if (memattr != VM_MEMATTR_DEFAULT)
347 pmap_page_set_memattr(m, memattr);
348 vm_page_insert(m, object,
349 OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
350 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
352 m->valid = VM_PAGE_BITS_ALL;
355 VM_OBJECT_UNLOCK(object);
356 vm_map_wire(map, addr, addr + size,
357 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
362 kmem_alloc_contig(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
363 vm_paddr_t high, unsigned long alignment, unsigned long boundary,
364 vm_memattr_t memattr)
371 size = round_page(size);
372 npgs = size >> PAGE_SHIFT;
375 pages = vm_phys_alloc_contig(npgs, low, high, alignment, boundary);
377 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
378 vm_contig_grow_cache(tries, low, high);
384 ret = contigmapping(map, size, pages, memattr, flags);
386 vm_page_release_contig(pages, npgs);