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/mutex.h>
71 #include <sys/kernel.h>
72 #include <sys/linker_set.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)
96 if (!VM_OBJECT_TRYLOCK(object))
98 if (vm_page_sleep_if_busy(m, TRUE, "vpctw0")) {
99 VM_OBJECT_UNLOCK(object);
100 vm_page_lock_queues();
103 vm_page_test_dirty(m);
104 if (m->dirty == 0 && m->hold_count == 0)
107 if (object->type == OBJT_VNODE) {
108 vm_page_unlock_queues();
110 VM_OBJECT_UNLOCK(object);
111 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
112 VM_OBJECT_LOCK(object);
113 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
114 VM_OBJECT_UNLOCK(object);
115 VOP_UNLOCK(vp, 0, curthread);
116 vm_page_lock_queues();
118 } else if (object->type == OBJT_SWAP ||
119 object->type == OBJT_DEFAULT) {
121 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC);
122 VM_OBJECT_UNLOCK(object);
125 } else if (m->hold_count == 0)
127 VM_OBJECT_UNLOCK(object);
132 vm_contig_launder(int queue)
137 for (m = TAILQ_FIRST(&vm_page_queues[queue].pl); m != NULL; m = next) {
138 next = TAILQ_NEXT(m, pageq);
139 KASSERT(m->queue == queue,
140 ("vm_contig_launder: page %p's queue is not %d", m, queue));
141 error = vm_contig_launder_page(m);
151 * This interface is for merging with malloc() someday.
152 * Even if we never implement compaction so that contiguous allocation
153 * works after initialization time, malloc()'s data structures are good
154 * for statistics and for allocations of less than a page.
158 unsigned long size, /* should be size_t here and for malloc() */
159 struct malloc_type *type,
163 unsigned long alignment,
164 unsigned long boundary,
170 vm_offset_t addr, tmp_addr;
172 int inactl, actl, inactmax, actmax;
173 vm_page_t pga = vm_page_array;
175 size = round_page(size);
177 panic("contigmalloc1: size must not be 0");
178 if ((alignment & (alignment - 1)) != 0)
179 panic("contigmalloc1: alignment must be a power of 2");
180 if ((boundary & (boundary - 1)) != 0)
181 panic("contigmalloc1: boundary must be a power of 2");
184 for (pass = 2; pass >= 0; pass--) {
185 vm_page_lock_queues();
187 mtx_lock_spin(&vm_page_queue_free_mtx);
190 * Find first page in array that is free, within range,
191 * aligned, and such that the boundary won't be crossed.
193 for (i = start; i < cnt.v_page_count; i++) {
194 phys = VM_PAGE_TO_PHYS(&pga[i]);
195 pqtype = pga[i].queue - pga[i].pc;
196 if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) &&
197 (phys >= low) && (phys < high) &&
198 ((phys & (alignment - 1)) == 0) &&
199 (((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0))
204 * If the above failed or we will exceed the upper bound, fail.
206 if ((i == cnt.v_page_count) ||
207 ((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) {
208 mtx_unlock_spin(&vm_page_queue_free_mtx);
210 * Instead of racing to empty the inactive/active
211 * queues, give up, even with more left to free,
212 * if we try more than the initial amount of pages.
214 * There's no point attempting this on the last pass.
218 inactmax = vm_page_queues[PQ_INACTIVE].lcnt;
219 actmax = vm_page_queues[PQ_ACTIVE].lcnt;
221 if (inactl < inactmax &&
222 vm_contig_launder(PQ_INACTIVE)) {
227 vm_contig_launder(PQ_ACTIVE)) {
232 vm_page_unlock_queues();
238 * Check successive pages for contiguous and free.
240 for (i = start + 1; i < (start + size / PAGE_SIZE); i++) {
241 pqtype = pga[i].queue - pga[i].pc;
242 if ((VM_PAGE_TO_PHYS(&pga[i]) !=
243 (VM_PAGE_TO_PHYS(&pga[i - 1]) + PAGE_SIZE)) ||
244 ((pqtype != PQ_FREE) && (pqtype != PQ_CACHE))) {
249 mtx_unlock_spin(&vm_page_queue_free_mtx);
250 for (i = start; i < (start + size / PAGE_SIZE); i++) {
251 vm_page_t m = &pga[i];
253 if ((m->queue - m->pc) == PQ_CACHE) {
254 if (m->hold_count != 0) {
259 if (!VM_OBJECT_TRYLOCK(object)) {
263 if ((m->flags & PG_BUSY) || m->busy != 0) {
264 VM_OBJECT_UNLOCK(object);
269 VM_OBJECT_UNLOCK(object);
272 mtx_lock_spin(&vm_page_queue_free_mtx);
273 for (i = start; i < (start + size / PAGE_SIZE); i++) {
274 pqtype = pga[i].queue - pga[i].pc;
275 if (pqtype != PQ_FREE) {
280 for (i = start; i < (start + size / PAGE_SIZE); i++) {
281 vm_page_t m = &pga[i];
282 vm_pageq_remove_nowakeup(m);
283 m->valid = VM_PAGE_BITS_ALL;
284 if (m->flags & PG_ZERO)
285 vm_page_zero_count--;
286 /* Don't clear the PG_ZERO flag, we'll need it later. */
287 m->flags = PG_UNMANAGED | (m->flags & PG_ZERO);
288 KASSERT(m->dirty == 0,
289 ("contigmalloc1: page %p was dirty", m));
293 mtx_unlock_spin(&vm_page_queue_free_mtx);
294 vm_page_unlock_queues();
296 * We've found a contiguous chunk that meets are requirements.
297 * Allocate kernel VM, unfree and assign the physical pages to
298 * it and return kernel VM pointer.
301 if (vm_map_findspace(map, vm_map_min(map), size, &addr) !=
304 * XXX We almost never run out of kernel virtual
305 * space, so we don't make the allocated memory
311 vm_object_reference(kernel_object);
312 vm_map_insert(map, kernel_object, addr - VM_MIN_KERNEL_ADDRESS,
313 addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
317 VM_OBJECT_LOCK(kernel_object);
318 for (i = start; i < (start + size / PAGE_SIZE); i++) {
319 vm_page_t m = &pga[i];
320 vm_page_insert(m, kernel_object,
321 OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
322 if ((flags & M_ZERO) && !(m->flags & PG_ZERO))
324 tmp_addr += PAGE_SIZE;
326 VM_OBJECT_UNLOCK(kernel_object);
327 vm_map_wire(map, addr, addr + size,
328 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
330 return ((void *)addr);
336 vm_page_release_contigl(vm_page_t m, vm_pindex_t count)
345 vm_page_release_contig(vm_page_t m, vm_pindex_t count)
347 vm_page_lock_queues();
348 vm_page_release_contigl(m, count);
349 vm_page_unlock_queues();
353 vm_contig_unqueue_free(vm_page_t m)
357 mtx_lock_spin(&vm_page_queue_free_mtx);
358 if ((m->queue - m->pc) == PQ_FREE)
359 vm_pageq_remove_nowakeup(m);
362 mtx_unlock_spin(&vm_page_queue_free_mtx);
365 m->valid = VM_PAGE_BITS_ALL;
366 if (m->flags & PG_ZERO)
367 vm_page_zero_count--;
368 /* Don't clear the PG_ZERO flag; we'll need it later. */
369 m->flags = PG_UNMANAGED | (m->flags & PG_ZERO);
370 KASSERT(m->dirty == 0,
371 ("contigmalloc2: page %p was dirty", m));
378 vm_page_alloc_contig(vm_pindex_t npages, vm_paddr_t low, vm_paddr_t high,
379 vm_offset_t alignment, vm_offset_t boundary)
384 vm_page_t pga = vm_page_array;
385 int i, pass, pqtype, start;
387 size = npages << PAGE_SHIFT;
389 panic("vm_page_alloc_contig: size must not be 0");
390 if ((alignment & (alignment - 1)) != 0)
391 panic("vm_page_alloc_contig: alignment must be a power of 2");
392 if ((boundary & (boundary - 1)) != 0)
393 panic("vm_page_alloc_contig: boundary must be a power of 2");
395 for (pass = 0; pass < 2; pass++) {
396 start = vm_page_array_size - npages + 1;
397 vm_page_lock_queues();
401 * Find last page in array that is free, within range,
402 * aligned, and such that the boundary won't be crossed.
404 for (i = start; i >= 0; i--) {
405 phys = VM_PAGE_TO_PHYS(&pga[i]);
406 pqtype = pga[i].queue - pga[i].pc;
408 if (pqtype != PQ_FREE && pqtype != PQ_CACHE)
410 } else if (pqtype != PQ_FREE && pqtype != PQ_CACHE &&
411 pga[i].queue != PQ_ACTIVE &&
412 pga[i].queue != PQ_INACTIVE)
414 if (phys >= low && phys + size <= high &&
415 ((phys & (alignment - 1)) == 0) &&
416 ((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0)
419 /* There are no candidates at all. */
421 vm_page_unlock_queues();
426 * Check successive pages for contiguous and free.
428 for (i = start + npages - 1; i > start; i--) {
429 pqtype = pga[i].queue - pga[i].pc;
430 if (VM_PAGE_TO_PHYS(&pga[i]) !=
431 VM_PAGE_TO_PHYS(&pga[i - 1]) + PAGE_SIZE) {
432 start = i - npages + 1;
436 if (pqtype != PQ_FREE && pqtype != PQ_CACHE) {
437 start = i - npages + 1;
440 } else if (pqtype != PQ_FREE && pqtype != PQ_CACHE &&
441 pga[i].queue != PQ_ACTIVE &&
442 pga[i].queue != PQ_INACTIVE) {
443 start = i - npages + 1;
447 for (i = start + npages - 1; i >= start; i--) {
448 vm_page_t m = &pga[i];
451 pqtype = m->queue - m->pc;
452 if (pass != 0 && pqtype != PQ_FREE &&
453 pqtype != PQ_CACHE) {
457 if (vm_contig_launder_page(m) != 0)
459 pqtype = m->queue - m->pc;
460 if (pqtype == PQ_FREE ||
465 vm_page_release_contigl(&pga[i + 1],
466 start + npages - 1 - i);
467 start = i - npages + 1;
471 if (pqtype == PQ_CACHE) {
472 if (m->hold_count != 0) {
473 start = i - npages + 1;
477 if (!VM_OBJECT_TRYLOCK(object)) {
478 start = i - npages + 1;
481 if ((m->flags & PG_BUSY) || m->busy != 0) {
482 VM_OBJECT_UNLOCK(object);
483 start = i - npages + 1;
487 VM_OBJECT_UNLOCK(object);
490 * There is no good API for freeing a page
491 * directly to PQ_NONE on our behalf, so spin.
493 if (vm_contig_unqueue_free(m) != 0)
496 vm_page_unlock_queues();
498 * We've found a contiguous chunk that meets are requirements.
500 return (&pga[start]);
506 contigmalloc2(vm_page_t m, vm_pindex_t npages, int flags)
508 vm_object_t object = kernel_object;
509 vm_map_t map = kernel_map;
510 vm_offset_t addr, tmp_addr;
514 * Allocate kernel VM, unfree and assign the physical pages to
515 * it and return kernel VM pointer.
518 if (vm_map_findspace(map, vm_map_min(map), npages << PAGE_SHIFT, &addr)
523 vm_object_reference(object);
524 vm_map_insert(map, object, addr - VM_MIN_KERNEL_ADDRESS,
525 addr, addr + (npages << PAGE_SHIFT), VM_PROT_ALL, VM_PROT_ALL, 0);
528 VM_OBJECT_LOCK(object);
529 for (i = 0; i < npages; i++) {
530 vm_page_insert(&m[i], object,
531 OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
532 if ((flags & M_ZERO) && !(m->flags & PG_ZERO))
533 pmap_zero_page(&m[i]);
534 tmp_addr += PAGE_SIZE;
536 VM_OBJECT_UNLOCK(object);
537 vm_map_wire(map, addr, addr + (npages << PAGE_SHIFT),
538 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
539 return ((void *)addr);
542 static int vm_old_contigmalloc = 0;
543 SYSCTL_INT(_vm, OID_AUTO, old_contigmalloc,
544 CTLFLAG_RW, &vm_old_contigmalloc, 0, "Use the old contigmalloc algorithm");
545 TUNABLE_INT("vm.old_contigmalloc", &vm_old_contigmalloc);
549 unsigned long size, /* should be size_t here and for malloc() */
550 struct malloc_type *type,
554 unsigned long alignment,
555 unsigned long boundary)
561 npgs = round_page(size) >> PAGE_SHIFT;
563 if (vm_old_contigmalloc) {
564 ret = contigmalloc1(size, type, flags, low, high, alignment,
565 boundary, kernel_map);
567 pages = vm_page_alloc_contig(npgs, low, high,
568 alignment, boundary);
572 ret = contigmalloc2(pages, npgs, flags);
574 vm_page_release_contig(pages, npgs);
579 malloc_type_allocated(type, ret == NULL ? 0 : npgs << PAGE_SHIFT);
584 contigfree(void *addr, unsigned long size, struct malloc_type *type)
588 npgs = round_page(size) >> PAGE_SHIFT;
589 kmem_free(kernel_map, (vm_offset_t)addr, size);
590 malloc_type_freed(type, npgs << PAGE_SHIFT);