9ed43f37d45bebe5b13dac8b47cfe13ed6765295
[dragonfly.git] / sys / vm / vm_contig.c
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.8 2004/07/16 05:04:36 hmp 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(object->handle, NULL, 0, curthread);
160                                 return (TRUE);
161                         } else if (object->type == OBJT_SWAP ||
162                                         object->type == OBJT_DEFAULT) {
163                                 m_tmp = m;
164                                 vm_pageout_flush(&m_tmp, 1, 0);
165                                 return (TRUE);
166                         }
167                 }
168                 
169                 if ((m->dirty == 0) && (m->busy == 0) && (m->hold_count == 0))
170                         vm_page_cache(m);
171         }
172
173         return (FALSE);
174 }
175
176 /*
177  * vm_contig_pg_alloc:
178  *
179  * Allocate contiguous pages from the VM.  This function does not
180  * map the allocated pages into the kernel map, otherwise it is
181  * impossible to make large allocations (i.e. >2G).
182  *
183  * Malloc()'s data structures have been used for collection of
184  * statistics and for allocations of less than a page.
185  *
186  */
187 int
188 vm_contig_pg_alloc(
189         unsigned long size,
190         vm_paddr_t low,
191         vm_paddr_t high,
192         unsigned long alignment,
193         unsigned long boundary)
194 {
195         int i, s, start, pass;
196         vm_offset_t phys;
197         vm_page_t pga = vm_page_array;
198
199         size = round_page(size);
200         if (size == 0)
201                 panic("vm_contig_pg_alloc: size must not be 0");
202         if ((alignment & (alignment - 1)) != 0)
203                 panic("vm_contig_pg_alloc: alignment must be a power of 2");
204         if ((boundary & (boundary - 1)) != 0)
205                 panic("vm_contig_pg_alloc: boundary must be a power of 2");
206
207         start = 0;
208         for (pass = 0; pass <= 1; pass++) {
209                 s = splvm();
210 again:
211                 /*
212                  * Find first page in array that is free, within range, aligned, and
213                  * such that the boundary won't be crossed.
214                  */
215                 for (i = start; i < vmstats.v_page_count; i++) {
216                         int pqtype;
217                         phys = VM_PAGE_TO_PHYS(&pga[i]);
218                         pqtype = pga[i].queue - pga[i].pc;
219                         if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) &&
220                             (phys >= low) && (phys < high) &&
221                             ((phys & (alignment - 1)) == 0) &&
222                             (((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0))
223                                 break;
224                 }
225
226                 /*
227                  * If we cannot find the page in the given range, or we have
228                  * crossed the boundary, call the vm_contig_pg_clean() function
229                  * for flushing out the queues, and returning it back to
230                  * normal state.
231                  */
232                 if ((i == vmstats.v_page_count) ||
233                         ((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) {
234
235 again1:
236                         if (vm_contig_pg_clean(PQ_INACTIVE))
237                                 goto again1;
238                         if (vm_contig_pg_clean(PQ_ACTIVE))
239                                 goto again1;
240
241                         splx(s);
242                         continue;       /* next pass */
243                 }
244                 start = i;
245
246                 /*
247                  * Check successive pages for contiguous and free.
248                  */
249                 for (i = start + 1; i < (start + size / PAGE_SIZE); i++) {
250                         int pqtype;
251                         pqtype = pga[i].queue - pga[i].pc;
252                         if ((VM_PAGE_TO_PHYS(&pga[i]) !=
253                             (VM_PAGE_TO_PHYS(&pga[i - 1]) + PAGE_SIZE)) ||
254                             ((pqtype != PQ_FREE) && (pqtype != PQ_CACHE))) {
255                                 start++;
256                                 goto again;
257                         }
258                 }
259
260                 for (i = start; i < (start + size / PAGE_SIZE); i++) {
261                         int pqtype;
262                         vm_page_t m = &pga[i];
263
264                         pqtype = m->queue - m->pc;
265                         if (pqtype == PQ_CACHE) {
266                                 vm_page_busy(m);
267                                 vm_page_free(m);
268                         }
269                         vm_page_unqueue_nowakeup(m);
270                         m->valid = VM_PAGE_BITS_ALL;
271                         if (m->flags & PG_ZERO)
272                                 vm_page_zero_count--;
273                         /* Don't clear the PG_ZERO flag, we'll need it later. */
274                         m->flags &= PG_ZERO;
275                         KASSERT(m->dirty == 0,
276                                 ("vm_contig_pg_alloc: page %p was dirty", m));
277                         m->wire_count = 0;
278                         m->busy = 0;
279                         m->object = NULL;
280                 }
281
282                 /*
283                  * Our job is done, return the index page of vm_page_array.
284                  */
285
286                 splx(s);
287                 return (start); /* aka &pga[start] */
288         }
289
290         /*
291          * Failed.
292          */
293         splx(s);
294         return (-1);
295 }
296
297 /*
298  * vm_contig_pg_free:
299  *
300  * Remove pages previously allocated by vm_contig_pg_alloc, and
301  * assume all references to the pages have been removed, and that
302  * it is OK to add them back to the free list.
303  */
304 void
305 vm_contig_pg_free(int start, u_long size)
306 {
307         vm_page_t pga = vm_page_array;
308         int i;
309         
310         size = round_page(size);
311         if (size == 0)
312                 panic("vm_contig_pg_free: size must not be 0");
313
314         for (i = start; i < (start + size / PAGE_SIZE); i++) {
315                 vm_page_free(&pga[i]);
316         }
317 }
318
319 /*
320  * vm_contig_pg_kmap:
321  *
322  * Map previously allocated (vm_contig_pg_alloc) range of pages from
323  * vm_page_array[] into the KVA.  Once mapped, the pages are part of
324  * the Kernel, and are to free'ed with kmem_free(kernel_map, addr, size).
325  */
326 vm_offset_t
327 vm_contig_pg_kmap(int start, u_long size, vm_map_t map, int flags)
328 {
329         vm_offset_t addr, tmp_addr;
330         vm_page_t pga = vm_page_array;
331         int i, s, count;
332
333         size = round_page(size);
334         if (size == 0)
335                 panic("vm_contig_pg_kmap: size must not be 0");
336
337         s = splvm();    /* XXX: is this really needed? */
338
339         /*
340          * We've found a contiguous chunk that meets our requirements.
341          * Allocate KVM, and assign phys pages and return a kernel VM
342          * pointer.
343          */
344         count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
345         vm_map_lock(map);
346         if (vm_map_findspace(map, vm_map_min(map), size, 1, &addr) !=
347             KERN_SUCCESS) {
348                 /*
349                  * XXX We almost never run out of kernel virtual
350                  * space, so we don't make the allocated memory
351                  * above available.
352                  */
353                 vm_map_unlock(map);
354                 vm_map_entry_release(count);
355                 splx(s);
356                 return (0);
357         }
358         vm_object_reference(kernel_object);
359         vm_map_insert(map, &count, 
360             kernel_object, addr - VM_MIN_KERNEL_ADDRESS,
361             addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
362         vm_map_unlock(map);
363         vm_map_entry_release(count);
364
365         tmp_addr = addr;
366         for (i = start; i < (start + size / PAGE_SIZE); i++) {
367                 vm_page_t m = &pga[i];
368                 vm_page_insert(m, kernel_object,
369                         OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
370                 if ((flags & M_ZERO) && !(m->flags & PG_ZERO))
371                         pmap_zero_page(VM_PAGE_TO_PHYS(m));
372                 m->flags = 0;
373                 tmp_addr += PAGE_SIZE;
374         }
375         vm_map_wire(map, addr, addr + size, 0);
376
377         splx(s);
378         return (addr);
379 }
380
381 void *
382 contigmalloc(
383         unsigned long size,     /* should be size_t here and for malloc() */
384         struct malloc_type *type,
385         int flags,
386         vm_paddr_t low,
387         vm_paddr_t high,
388         unsigned long alignment,
389         unsigned long boundary)
390 {
391         return contigmalloc_map(size, type, flags, low, high, alignment,
392                         boundary, kernel_map);
393 }
394
395 void *
396 contigmalloc_map(
397         unsigned long size,     /* should be size_t here and for malloc() */
398         struct malloc_type *type,
399         int flags,
400         vm_paddr_t low,
401         vm_paddr_t high,
402         unsigned long alignment,
403         unsigned long boundary,
404         vm_map_t map)
405 {
406         int index;
407         void *rv;
408
409         index = vm_contig_pg_alloc(size, low, high, alignment, boundary);
410         if (index < 0) {
411                 printf("contigmalloc_map: failed in index < 0 case!");
412                 return NULL;
413         }
414
415         rv = (void *) vm_contig_pg_kmap(index, size, map, flags);
416         if (!rv)
417                 vm_contig_pg_free(index, size);
418         
419         return rv;
420 }
421
422 void
423 contigfree(void *addr, unsigned long size, struct malloc_type *type)
424 {
425         kmem_free(kernel_map, (vm_offset_t)addr, size);
426 }
427
428 vm_offset_t
429 vm_page_alloc_contig(
430         vm_offset_t size,
431         vm_paddr_t low,
432         vm_paddr_t high,
433         vm_offset_t alignment)
434 {
435         return ((vm_offset_t)contigmalloc_map(size, M_DEVBUF, M_NOWAIT, low,
436                                 high, alignment, 0ul, kernel_map));
437 }