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>
68 #include <sys/malloc.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)
98 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
99 vm_page_lock_assert(m, MA_OWNED);
101 if (!VM_OBJECT_TRYLOCK(object) &&
102 (!vm_pageout_fallback_object_lock(m, next) || m->hold_count != 0)) {
104 VM_OBJECT_UNLOCK(object);
107 if (vm_page_sleep_if_busy(m, TRUE, "vpctw0")) {
108 VM_OBJECT_UNLOCK(object);
109 vm_page_lock_queues();
112 vm_page_test_dirty(m);
117 if ((object->flags & OBJ_DEAD) != 0) {
118 VM_OBJECT_UNLOCK(object);
121 if (object->type == OBJT_VNODE) {
122 vm_page_unlock_queues();
124 vm_object_reference_locked(object);
125 VM_OBJECT_UNLOCK(object);
126 (void) vn_start_write(vp, &mp, V_WAIT);
127 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
128 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
129 VM_OBJECT_LOCK(object);
130 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
131 VM_OBJECT_UNLOCK(object);
133 VFS_UNLOCK_GIANT(vfslocked);
134 vm_object_deallocate(object);
135 vn_finished_write(mp);
136 vm_page_lock_queues();
138 } else if (object->type == OBJT_SWAP ||
139 object->type == OBJT_DEFAULT) {
140 vm_page_unlock_queues();
142 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 0,
144 VM_OBJECT_UNLOCK(object);
145 vm_page_lock_queues();
152 VM_OBJECT_UNLOCK(object);
157 vm_contig_launder(int queue, vm_paddr_t low, vm_paddr_t high)
163 TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
165 /* Skip marker pages */
166 if ((m->flags & PG_MARKER) != 0)
169 pa = VM_PAGE_TO_PHYS(m);
170 if (pa < low || pa + PAGE_SIZE > high)
173 if (!vm_pageout_page_lock(m, &next) || m->hold_count != 0) {
177 KASSERT(m->queue == queue,
178 ("vm_contig_launder: page %p's queue is not %d", m, queue));
179 error = vm_contig_launder_page(m, &next);
180 vm_page_lock_assert(m, MA_NOTOWNED);
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.
209 vm_contig_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
211 int actl, actmax, inactl, inactmax;
213 vm_page_lock_queues();
215 inactmax = tries < 1 ? 0 : cnt.v_inactive_count;
217 actmax = tries < 2 ? 0 : cnt.v_active_count;
219 if (inactl < inactmax && vm_contig_launder(PQ_INACTIVE, low, high)) {
223 if (actl < actmax && vm_contig_launder(PQ_ACTIVE, low, high)) {
227 vm_page_unlock_queues();
231 * Allocates a region from the kernel address map and pages within the
232 * specified physical address range to the kernel object, creates a wired
233 * mapping from the region to these pages, and returns the region's starting
234 * virtual address. The allocated pages are not necessarily physically
235 * contiguous. If M_ZERO is specified through the given flags, then the pages
236 * are zeroed before they are mapped.
239 kmem_alloc_attr(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
240 vm_paddr_t high, vm_memattr_t memattr)
242 vm_object_t object = kernel_object;
243 vm_offset_t addr, i, offset;
247 size = round_page(size);
249 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
253 offset = addr - VM_MIN_KERNEL_ADDRESS;
254 vm_object_reference(object);
255 vm_map_insert(map, object, offset, addr, addr + size, VM_PROT_ALL,
257 VM_OBJECT_LOCK(object);
258 for (i = 0; i < size; i += PAGE_SIZE) {
261 m = vm_phys_alloc_contig(1, low, high, PAGE_SIZE, 0);
263 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
264 VM_OBJECT_UNLOCK(object);
266 vm_contig_grow_cache(tries, low, high);
268 VM_OBJECT_LOCK(object);
274 m = vm_page_lookup(object, OFF_TO_IDX(offset +
278 VM_OBJECT_UNLOCK(object);
279 vm_map_delete(map, addr, addr + size);
283 if (memattr != VM_MEMATTR_DEFAULT)
284 pmap_page_set_memattr(m, memattr);
285 vm_page_insert(m, object, OFF_TO_IDX(offset + i));
286 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
288 m->valid = VM_PAGE_BITS_ALL;
290 VM_OBJECT_UNLOCK(object);
292 vm_map_wire(map, addr, addr + size, VM_MAP_WIRE_SYSTEM |
293 VM_MAP_WIRE_NOHOLES);
298 * Allocates a region from the kernel address map, inserts the
299 * given physically contiguous pages into the kernel object,
300 * creates a wired mapping from the region to the pages, and
301 * returns the region's starting virtual address. If M_ZERO is
302 * specified through the given flags, then the pages are zeroed
303 * before they are mapped.
306 contigmapping(vm_map_t map, vm_size_t size, vm_page_t m, vm_memattr_t memattr,
309 vm_object_t object = kernel_object;
310 vm_offset_t addr, tmp_addr;
313 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
317 vm_object_reference(object);
318 vm_map_insert(map, object, addr - VM_MIN_KERNEL_ADDRESS,
319 addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
321 VM_OBJECT_LOCK(object);
322 for (tmp_addr = addr; tmp_addr < addr + size; tmp_addr += PAGE_SIZE) {
323 if (memattr != VM_MEMATTR_DEFAULT)
324 pmap_page_set_memattr(m, memattr);
325 vm_page_insert(m, object,
326 OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
327 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
329 m->valid = VM_PAGE_BITS_ALL;
332 VM_OBJECT_UNLOCK(object);
333 vm_map_wire(map, addr, addr + size,
334 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
340 unsigned long size, /* should be size_t here and for malloc() */
341 struct malloc_type *type,
345 unsigned long alignment,
346 unsigned long boundary)
350 ret = (void *)kmem_alloc_contig(kernel_map, size, flags, low, high,
351 alignment, boundary, VM_MEMATTR_DEFAULT);
353 malloc_type_allocated(type, round_page(size));
358 kmem_alloc_contig(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
359 vm_paddr_t high, unsigned long alignment, unsigned long boundary,
360 vm_memattr_t memattr)
367 size = round_page(size);
368 npgs = size >> PAGE_SHIFT;
371 pages = vm_phys_alloc_contig(npgs, low, high, alignment, boundary);
373 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
374 vm_contig_grow_cache(tries, low, high);
380 ret = contigmapping(map, size, pages, memattr, flags);
382 vm_page_release_contig(pages, npgs);
388 contigfree(void *addr, unsigned long size, struct malloc_type *type)
391 kmem_free(kernel_map, (vm_offset_t)addr, size);
392 malloc_type_freed(type, round_page(size));