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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91
41 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42 * All rights reserved.
44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
56 * Carnegie Mellon requests users of this software to return to
58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
67 #include <sys/param.h>
68 #include <sys/systm.h>
70 #include <sys/malloc.h>
71 #include <sys/mutex.h>
73 #include <sys/vmmeter.h>
74 #include <sys/vnode.h>
77 #include <vm/vm_param.h>
78 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_object.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_pageout.h>
84 #include <vm/vm_pager.h>
85 #include <vm/vm_extern.h>
88 vm_contig_launder(int queue)
91 vm_page_t m, m_tmp, next;
93 for (m = TAILQ_FIRST(&vm_page_queues[queue].pl); m != NULL; m = next) {
94 next = TAILQ_NEXT(m, pageq);
95 KASSERT(m->queue == queue,
96 ("vm_contig_launder: page %p's queue is not %d", m, queue));
97 if (vm_page_sleep_if_busy(m, TRUE, "vpctw0")) {
98 vm_page_lock_queues();
101 vm_page_test_dirty(m);
104 if (object->type == OBJT_VNODE) {
105 vm_page_unlock_queues();
106 vn_lock(object->handle,
107 LK_EXCLUSIVE | LK_RETRY, curthread);
108 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
109 VOP_UNLOCK(object->handle, 0, curthread);
110 vm_page_lock_queues();
112 } else if (object->type == OBJT_SWAP ||
113 object->type == OBJT_DEFAULT) {
115 vm_pageout_flush(&m_tmp, 1, 0);
118 } else if (m->busy == 0 && m->hold_count == 0)
125 * This interface is for merging with malloc() someday.
126 * Even if we never implement compaction so that contiguous allocation
127 * works after initialization time, malloc()'s data structures are good
128 * for statistics and for allocations of less than a page.
132 unsigned long size, /* should be size_t here and for malloc() */
133 struct malloc_type *type,
137 unsigned long alignment,
138 unsigned long boundary,
142 vm_offset_t addr, phys, tmp_addr;
144 vm_page_t pga = vm_page_array;
146 size = round_page(size);
148 panic("contigmalloc1: size must not be 0");
149 if ((alignment & (alignment - 1)) != 0)
150 panic("contigmalloc1: alignment must be a power of 2");
151 if ((boundary & (boundary - 1)) != 0)
152 panic("contigmalloc1: boundary must be a power of 2");
155 for (pass = 0; pass <= 1; pass++) {
157 vm_page_lock_queues();
160 * Find first page in array that is free, within range, aligned, and
161 * such that the boundary won't be crossed.
163 for (i = start; i < cnt.v_page_count; i++) {
165 phys = VM_PAGE_TO_PHYS(&pga[i]);
166 pqtype = pga[i].queue - pga[i].pc;
167 if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) &&
168 (phys >= low) && (phys < high) &&
169 ((phys & (alignment - 1)) == 0) &&
170 (((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0))
175 * If the above failed or we will exceed the upper bound, fail.
177 if ((i == cnt.v_page_count) ||
178 ((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) {
180 if (vm_contig_launder(PQ_INACTIVE))
182 if (vm_contig_launder(PQ_ACTIVE))
184 vm_page_unlock_queues();
191 * Check successive pages for contiguous and free.
193 for (i = start + 1; i < (start + size / PAGE_SIZE); i++) {
195 pqtype = pga[i].queue - pga[i].pc;
196 if ((VM_PAGE_TO_PHYS(&pga[i]) !=
197 (VM_PAGE_TO_PHYS(&pga[i - 1]) + PAGE_SIZE)) ||
198 ((pqtype != PQ_FREE) && (pqtype != PQ_CACHE))) {
203 for (i = start; i < (start + size / PAGE_SIZE); i++) {
204 vm_page_t m = &pga[i];
206 if ((m->queue - m->pc) == PQ_CACHE) {
210 mtx_lock_spin(&vm_page_queue_free_mtx);
211 vm_pageq_remove_nowakeup(m);
212 m->valid = VM_PAGE_BITS_ALL;
213 if (m->flags & PG_ZERO)
214 vm_page_zero_count--;
216 KASSERT(m->dirty == 0, ("contigmalloc1: page %p was dirty", m));
220 mtx_unlock_spin(&vm_page_queue_free_mtx);
222 vm_page_unlock_queues();
224 * We've found a contiguous chunk that meets are requirements.
225 * Allocate kernel VM, unfree and assign the physical pages to it and
226 * return kernel VM pointer.
229 if (vm_map_findspace(map, vm_map_min(map), size, &addr) !=
232 * XXX We almost never run out of kernel virtual
233 * space, so we don't make the allocated memory
240 vm_object_reference(kernel_object);
241 vm_map_insert(map, kernel_object, addr - VM_MIN_KERNEL_ADDRESS,
242 addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
246 for (i = start; i < (start + size / PAGE_SIZE); i++) {
247 vm_page_t m = &pga[i];
248 vm_page_insert(m, kernel_object,
249 OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
250 tmp_addr += PAGE_SIZE;
252 vm_map_wire(map, addr, addr + size, FALSE);
255 return ((void *)addr);
262 unsigned long size, /* should be size_t here and for malloc() */
263 struct malloc_type *type,
267 unsigned long alignment,
268 unsigned long boundary)
273 ret = contigmalloc1(size, type, flags, low, high, alignment, boundary,
280 contigfree(void *addr, unsigned long size, struct malloc_type *type)
283 kmem_free(kernel_map, (vm_offset_t)addr, size);
287 vm_page_alloc_contig(
291 vm_offset_t alignment)
296 ret = ((vm_offset_t)contigmalloc1(size, M_DEVBUF, M_NOWAIT, low, high,
297 alignment, 0ul, kernel_map));