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_extern.h>
89 vm_contig_launder_page(vm_page_t m, vm_page_t *next, int tries)
97 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
98 if (!vm_pageout_page_lock(m, next) || m->hold_count != 0) {
103 if (!VM_OBJECT_TRYLOCK(object) &&
104 (!vm_pageout_fallback_object_lock(m, next) || m->hold_count != 0)) {
106 VM_OBJECT_UNLOCK(object);
109 if ((m->oflags & VPO_BUSY) != 0 || m->busy != 0) {
112 VM_OBJECT_UNLOCK(object);
115 vm_page_sleep(m, "vpctw0");
116 VM_OBJECT_UNLOCK(object);
117 vm_page_lock_queues();
120 vm_page_test_dirty(m);
125 if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
126 VM_OBJECT_UNLOCK(object);
129 if (object->type == OBJT_VNODE) {
130 vm_page_unlock_queues();
132 vm_object_reference_locked(object);
133 VM_OBJECT_UNLOCK(object);
134 (void) vn_start_write(vp, &mp, V_WAIT);
135 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
136 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
137 VM_OBJECT_LOCK(object);
138 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
139 VM_OBJECT_UNLOCK(object);
141 VFS_UNLOCK_GIANT(vfslocked);
142 vm_object_deallocate(object);
143 vn_finished_write(mp);
144 vm_page_lock_queues();
146 } else if (object->type == OBJT_SWAP ||
147 object->type == OBJT_DEFAULT) {
148 vm_page_unlock_queues();
150 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 0,
152 VM_OBJECT_UNLOCK(object);
153 vm_page_lock_queues();
160 VM_OBJECT_UNLOCK(object);
165 vm_contig_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high)
171 TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
172 KASSERT(m->queue == queue,
173 ("vm_contig_launder: page %p's queue is not %d", m, queue));
174 if ((m->flags & PG_MARKER) != 0)
176 pa = VM_PAGE_TO_PHYS(m);
177 if (pa < low || pa + PAGE_SIZE > high)
179 error = vm_contig_launder_page(m, &next, tries);
189 * Increase the number of cached pages. The specified value, "tries",
190 * determines which categories of pages are cached:
192 * 0: All clean, inactive pages within the specified physical address range
193 * are cached. Will not sleep.
194 * 1: The vm_lowmem handlers are called. All inactive pages within
195 * the specified physical address range are cached. May sleep.
196 * 2: The vm_lowmem handlers are called. All inactive and active pages
197 * within the specified physical address range are cached. May sleep.
200 vm_contig_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
202 int actl, actmax, inactl, inactmax;
206 * Decrease registered cache sizes. The vm_lowmem handlers
207 * may acquire locks and/or sleep, so they can only be invoked
208 * when "tries" is greater than zero.
210 EVENTHANDLER_INVOKE(vm_lowmem, 0);
213 * We do this explicitly after the caches have been drained
218 vm_page_lock_queues();
220 inactmax = cnt.v_inactive_count;
222 actmax = tries < 2 ? 0 : cnt.v_active_count;
224 if (inactl < inactmax && vm_contig_launder(PQ_INACTIVE, tries, low,
229 if (actl < actmax && vm_contig_launder(PQ_ACTIVE, tries, low, high)) {
233 vm_page_unlock_queues();
237 * Allocates a region from the kernel address map and pages within the
238 * specified physical address range to the kernel object, creates a wired
239 * mapping from the region to these pages, and returns the region's starting
240 * virtual address. The allocated pages are not necessarily physically
241 * contiguous. If M_ZERO is specified through the given flags, then the pages
242 * are zeroed before they are mapped.
245 kmem_alloc_attr(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
246 vm_paddr_t high, vm_memattr_t memattr)
248 vm_object_t object = kernel_object;
250 vm_ooffset_t end_offset, offset;
254 size = round_page(size);
256 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
260 offset = addr - VM_MIN_KERNEL_ADDRESS;
261 vm_object_reference(object);
262 vm_map_insert(map, object, offset, addr, addr + size, VM_PROT_ALL,
264 if ((flags & (M_NOWAIT | M_USE_RESERVE)) == M_NOWAIT)
265 pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_NOBUSY;
267 pflags = VM_ALLOC_SYSTEM | VM_ALLOC_NOBUSY;
269 pflags |= VM_ALLOC_ZERO;
270 VM_OBJECT_LOCK(object);
271 end_offset = offset + size;
272 for (; offset < end_offset; offset += PAGE_SIZE) {
275 m = vm_page_alloc_contig(object, OFF_TO_IDX(offset), pflags, 1,
276 low, high, PAGE_SIZE, 0, memattr);
278 VM_OBJECT_UNLOCK(object);
279 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
281 vm_contig_grow_cache(tries, low, high);
283 VM_OBJECT_LOCK(object);
288 * Since the pages that were allocated by any previous
289 * iterations of this loop are not busy, they can be
290 * freed by vm_object_page_remove(), which is called
291 * by vm_map_delete().
293 vm_map_delete(map, addr, addr + size);
297 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
299 m->valid = VM_PAGE_BITS_ALL;
301 VM_OBJECT_UNLOCK(object);
303 vm_map_wire(map, addr, addr + size, VM_MAP_WIRE_SYSTEM |
304 VM_MAP_WIRE_NOHOLES);
309 * Allocates a region from the kernel address map, inserts the
310 * given physically contiguous pages into the kernel object,
311 * creates a wired mapping from the region to the pages, and
312 * returns the region's starting virtual address. If M_ZERO is
313 * specified through the given flags, then the pages are zeroed
314 * before they are mapped.
317 kmem_alloc_contig(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
318 vm_paddr_t high, u_long alignment, u_long boundary,
319 vm_memattr_t memattr)
321 vm_object_t object = kernel_object;
327 size = round_page(size);
329 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
333 offset = addr - VM_MIN_KERNEL_ADDRESS;
334 vm_object_reference(object);
335 vm_map_insert(map, object, offset, addr, addr + size, VM_PROT_ALL,
337 if ((flags & (M_NOWAIT | M_USE_RESERVE)) == M_NOWAIT)
338 pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_NOBUSY;
340 pflags = VM_ALLOC_SYSTEM | VM_ALLOC_NOBUSY;
342 pflags |= VM_ALLOC_ZERO;
343 VM_OBJECT_LOCK(object);
346 m = vm_page_alloc_contig(object, OFF_TO_IDX(offset), pflags,
347 atop(size), low, high, alignment, boundary, memattr);
349 VM_OBJECT_UNLOCK(object);
350 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
352 vm_contig_grow_cache(tries, low, high);
354 VM_OBJECT_LOCK(object);
358 vm_map_delete(map, addr, addr + size);
362 end_m = m + atop(size);
363 for (; m < end_m; m++) {
364 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
366 m->valid = VM_PAGE_BITS_ALL;
368 VM_OBJECT_UNLOCK(object);
370 vm_map_wire(map, addr, addr + size, VM_MAP_WIRE_SYSTEM |
371 VM_MAP_WIRE_NOHOLES);