xref: /dragonfly/sys/vm/vm_contig.c (revision 43b4d1bd)
1 /*
2  * Copyright (c) 2003, 2004 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Hiten Pandya <hmp@backplane.com>.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 /*
36  * Copyright (c) 1991 Regents of the University of California.
37  * All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * The Mach Operating System project at Carnegie-Mellon University.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	from: @(#)vm_page.c	7.4 (Berkeley) 5/7/91
67  * $DragonFly: src/sys/vm/vm_contig.c,v 1.9 2004/08/17 18:57:36 dillon Exp $
68  */
69 
70 /*
71  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
72  * All rights reserved.
73  *
74  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
75  *
76  * Permission to use, copy, modify and distribute this software and
77  * its documentation is hereby granted, provided that both the copyright
78  * notice and this permission notice appear in all copies of the
79  * software, derivative works or modified versions, and any portions
80  * thereof, and that both notices appear in supporting documentation.
81  *
82  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
83  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
84  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
85  *
86  * Carnegie Mellon requests users of this software to return to
87  *
88  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
89  *  School of Computer Science
90  *  Carnegie Mellon University
91  *  Pittsburgh PA 15213-3890
92  *
93  * any improvements or extensions that they make and grant Carnegie the
94  * rights to redistribute these changes.
95  */
96 
97 /*
98  * Contiguous memory allocation API.
99  */
100 
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/malloc.h>
104 #include <sys/proc.h>
105 #include <sys/lock.h>
106 #include <sys/vmmeter.h>
107 #include <sys/vnode.h>
108 
109 #include <vm/vm.h>
110 #include <vm/vm_param.h>
111 #include <vm/vm_kern.h>
112 #include <vm/pmap.h>
113 #include <vm/vm_map.h>
114 #include <vm/vm_object.h>
115 #include <vm/vm_page.h>
116 #include <vm/vm_pageout.h>
117 #include <vm/vm_pager.h>
118 #include <vm/vm_extern.h>
119 #include <vm/vm_page2.h>
120 
121 /*
122  * vm_contig_pg_clean:
123  *
124  * Do a thorough cleanup of the specified 'queue', which can be either
125  * PQ_ACTIVE or PQ_INACTIVE by doing a walkthrough.  If the page is not
126  * marked dirty, it is shoved into the page cache, provided no one has
127  * currently aqcuired it, otherwise localized action per object type
128  * is taken for cleanup:
129  *
130  * 	In the OBJT_VNODE case, the whole page range is cleaned up
131  * 	using the vm_object_page_clean() routine, by specyfing a
132  * 	start and end of '0'.
133  *
134  * 	Otherwise if the object is of any other type, the generic
135  * 	pageout (daemon) flush routine is invoked.
136  */
137 static int
138 vm_contig_pg_clean(int queue)
139 {
140 	vm_object_t object;
141 	vm_page_t m, m_tmp, next;
142 
143 	for (m = TAILQ_FIRST(&vm_page_queues[queue].pl); m != NULL; m = next) {
144 		KASSERT(m->queue == queue,
145 			("vm_contig_clean: page %p's queue is not %d", m, queue));
146 
147 		next = TAILQ_NEXT(m, pageq);
148 
149 		if (vm_page_sleep_busy(m, TRUE, "vpctw0"))
150 			return (TRUE);
151 
152 		vm_page_test_dirty(m);
153 		if (m->dirty) {
154 			object = m->object;
155 			if (object->type == OBJT_VNODE) {
156 				vn_lock(object->handle, NULL,
157 					LK_EXCLUSIVE | LK_RETRY, curthread);
158 				vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
159 				VOP_UNLOCK(((struct vnode *)object->handle),
160 					    NULL, 0, curthread);
161 				return (TRUE);
162 			} else if (object->type == OBJT_SWAP ||
163 					object->type == OBJT_DEFAULT) {
164 				m_tmp = m;
165 				vm_pageout_flush(&m_tmp, 1, 0);
166 				return (TRUE);
167 			}
168 		}
169 
170 		if ((m->dirty == 0) && (m->busy == 0) && (m->hold_count == 0))
171 			vm_page_cache(m);
172 	}
173 
174 	return (FALSE);
175 }
176 
177 /*
178  * vm_contig_pg_alloc:
179  *
180  * Allocate contiguous pages from the VM.  This function does not
181  * map the allocated pages into the kernel map, otherwise it is
182  * impossible to make large allocations (i.e. >2G).
183  *
184  * Malloc()'s data structures have been used for collection of
185  * statistics and for allocations of less than a page.
186  *
187  */
188 int
189 vm_contig_pg_alloc(
190 	unsigned long size,
191 	vm_paddr_t low,
192 	vm_paddr_t high,
193 	unsigned long alignment,
194 	unsigned long boundary)
195 {
196 	int i, s, start, pass;
197 	vm_offset_t phys;
198 	vm_page_t pga = vm_page_array;
199 
200 	size = round_page(size);
201 	if (size == 0)
202 		panic("vm_contig_pg_alloc: size must not be 0");
203 	if ((alignment & (alignment - 1)) != 0)
204 		panic("vm_contig_pg_alloc: alignment must be a power of 2");
205 	if ((boundary & (boundary - 1)) != 0)
206 		panic("vm_contig_pg_alloc: boundary must be a power of 2");
207 
208 	start = 0;
209 	for (pass = 0; pass <= 1; pass++) {
210 		s = splvm();
211 again:
212 		/*
213 		 * Find first page in array that is free, within range, aligned, and
214 		 * such that the boundary won't be crossed.
215 		 */
216 		for (i = start; i < vmstats.v_page_count; i++) {
217 			int pqtype;
218 			phys = VM_PAGE_TO_PHYS(&pga[i]);
219 			pqtype = pga[i].queue - pga[i].pc;
220 			if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) &&
221 			    (phys >= low) && (phys < high) &&
222 			    ((phys & (alignment - 1)) == 0) &&
223 			    (((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0))
224 				break;
225 		}
226 
227 		/*
228 		 * If we cannot find the page in the given range, or we have
229 		 * crossed the boundary, call the vm_contig_pg_clean() function
230 		 * for flushing out the queues, and returning it back to
231 		 * normal state.
232 		 */
233 		if ((i == vmstats.v_page_count) ||
234 			((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) {
235 
236 again1:
237 			if (vm_contig_pg_clean(PQ_INACTIVE))
238 				goto again1;
239 			if (vm_contig_pg_clean(PQ_ACTIVE))
240 				goto again1;
241 
242 			splx(s);
243 			continue;	/* next pass */
244 		}
245 		start = i;
246 
247 		/*
248 		 * Check successive pages for contiguous and free.
249 		 */
250 		for (i = start + 1; i < (start + size / PAGE_SIZE); i++) {
251 			int pqtype;
252 			pqtype = pga[i].queue - pga[i].pc;
253 			if ((VM_PAGE_TO_PHYS(&pga[i]) !=
254 			    (VM_PAGE_TO_PHYS(&pga[i - 1]) + PAGE_SIZE)) ||
255 			    ((pqtype != PQ_FREE) && (pqtype != PQ_CACHE))) {
256 				start++;
257 				goto again;
258 			}
259 		}
260 
261 		for (i = start; i < (start + size / PAGE_SIZE); i++) {
262 			int pqtype;
263 			vm_page_t m = &pga[i];
264 
265 			pqtype = m->queue - m->pc;
266 			if (pqtype == PQ_CACHE) {
267 				vm_page_busy(m);
268 				vm_page_free(m);
269 			}
270 			vm_page_unqueue_nowakeup(m);
271 			m->valid = VM_PAGE_BITS_ALL;
272 			if (m->flags & PG_ZERO)
273 				vm_page_zero_count--;
274 			/* Don't clear the PG_ZERO flag, we'll need it later. */
275 			m->flags &= PG_ZERO;
276 			KASSERT(m->dirty == 0,
277 				("vm_contig_pg_alloc: page %p was dirty", m));
278 			m->wire_count = 0;
279 			m->busy = 0;
280 			m->object = NULL;
281 		}
282 
283 		/*
284 		 * Our job is done, return the index page of vm_page_array.
285 		 */
286 
287 		splx(s);
288 		return (start); /* aka &pga[start] */
289 	}
290 
291 	/*
292 	 * Failed.
293 	 */
294 	splx(s);
295 	return (-1);
296 }
297 
298 /*
299  * vm_contig_pg_free:
300  *
301  * Remove pages previously allocated by vm_contig_pg_alloc, and
302  * assume all references to the pages have been removed, and that
303  * it is OK to add them back to the free list.
304  */
305 void
306 vm_contig_pg_free(int start, u_long size)
307 {
308 	vm_page_t pga = vm_page_array;
309 	int i;
310 
311 	size = round_page(size);
312 	if (size == 0)
313 		panic("vm_contig_pg_free: size must not be 0");
314 
315 	for (i = start; i < (start + size / PAGE_SIZE); i++) {
316 		vm_page_free(&pga[i]);
317 	}
318 }
319 
320 /*
321  * vm_contig_pg_kmap:
322  *
323  * Map previously allocated (vm_contig_pg_alloc) range of pages from
324  * vm_page_array[] into the KVA.  Once mapped, the pages are part of
325  * the Kernel, and are to free'ed with kmem_free(kernel_map, addr, size).
326  */
327 vm_offset_t
328 vm_contig_pg_kmap(int start, u_long size, vm_map_t map, int flags)
329 {
330 	vm_offset_t addr, tmp_addr;
331 	vm_page_t pga = vm_page_array;
332 	int i, s, count;
333 
334 	size = round_page(size);
335 	if (size == 0)
336 		panic("vm_contig_pg_kmap: size must not be 0");
337 
338 	s = splvm();	/* XXX: is this really needed? */
339 
340 	/*
341 	 * We've found a contiguous chunk that meets our requirements.
342 	 * Allocate KVM, and assign phys pages and return a kernel VM
343 	 * pointer.
344 	 */
345 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
346 	vm_map_lock(map);
347 	if (vm_map_findspace(map, vm_map_min(map), size, 1, &addr) !=
348 	    KERN_SUCCESS) {
349 		/*
350 		 * XXX We almost never run out of kernel virtual
351 		 * space, so we don't make the allocated memory
352 		 * above available.
353 		 */
354 		vm_map_unlock(map);
355 		vm_map_entry_release(count);
356  		splx(s);
357 		return (0);
358 	}
359 	vm_object_reference(kernel_object);
360 	vm_map_insert(map, &count,
361 	    kernel_object, addr - VM_MIN_KERNEL_ADDRESS,
362 	    addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
363 	vm_map_unlock(map);
364 	vm_map_entry_release(count);
365 
366 	tmp_addr = addr;
367 	for (i = start; i < (start + size / PAGE_SIZE); i++) {
368 		vm_page_t m = &pga[i];
369 		vm_page_insert(m, kernel_object,
370 			OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
371 		if ((flags & M_ZERO) && !(m->flags & PG_ZERO))
372 			pmap_zero_page(VM_PAGE_TO_PHYS(m));
373 		m->flags = 0;
374 		tmp_addr += PAGE_SIZE;
375  	}
376 	vm_map_wire(map, addr, addr + size, 0);
377 
378 	splx(s);
379 	return (addr);
380 }
381 
382 void *
383 contigmalloc(
384 	unsigned long size,	/* should be size_t here and for malloc() */
385 	struct malloc_type *type,
386 	int flags,
387 	vm_paddr_t low,
388 	vm_paddr_t high,
389 	unsigned long alignment,
390 	unsigned long boundary)
391 {
392 	return contigmalloc_map(size, type, flags, low, high, alignment,
393 			boundary, kernel_map);
394 }
395 
396 void *
397 contigmalloc_map(
398 	unsigned long size,	/* should be size_t here and for malloc() */
399 	struct malloc_type *type,
400 	int flags,
401 	vm_paddr_t low,
402 	vm_paddr_t high,
403 	unsigned long alignment,
404 	unsigned long boundary,
405 	vm_map_t map)
406 {
407 	int index;
408 	void *rv;
409 
410 	index = vm_contig_pg_alloc(size, low, high, alignment, boundary);
411 	if (index < 0) {
412 		printf("contigmalloc_map: failed in index < 0 case!");
413 		return NULL;
414 	}
415 
416 	rv = (void *) vm_contig_pg_kmap(index, size, map, flags);
417 	if (!rv)
418 		vm_contig_pg_free(index, size);
419 
420 	return rv;
421 }
422 
423 void
424 contigfree(void *addr, unsigned long size, struct malloc_type *type)
425 {
426 	kmem_free(kernel_map, (vm_offset_t)addr, size);
427 }
428 
429 vm_offset_t
430 vm_page_alloc_contig(
431 	vm_offset_t size,
432 	vm_paddr_t low,
433 	vm_paddr_t high,
434 	vm_offset_t alignment)
435 {
436 	return ((vm_offset_t)contigmalloc_map(size, M_DEVBUF, M_NOWAIT, low,
437 				high, alignment, 0ul, kernel_map));
438 }
439