VFS messaging/interfacing work stage 8/99: Major reworking of the vnode
[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.10 2004/10/12 19:21:16 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
120 #include <sys/thread2.h>
121 #include <vm/vm_page2.h>
122
123 /*
124  * vm_contig_pg_clean:
125  * 
126  * Do a thorough cleanup of the specified 'queue', which can be either
127  * PQ_ACTIVE or PQ_INACTIVE by doing a walkthrough.  If the page is not
128  * marked dirty, it is shoved into the page cache, provided no one has
129  * currently aqcuired it, otherwise localized action per object type
130  * is taken for cleanup:
131  *
132  *      In the OBJT_VNODE case, the whole page range is cleaned up
133  *      using the vm_object_page_clean() routine, by specyfing a
134  *      start and end of '0'.
135  *
136  *      Otherwise if the object is of any other type, the generic
137  *      pageout (daemon) flush routine is invoked.
138  *
139  * We must be in a critical section.
140  */
141 static int
142 vm_contig_pg_clean(int queue)
143 {
144         vm_object_t object;
145         vm_page_t m, m_tmp, next;
146
147         for (m = TAILQ_FIRST(&vm_page_queues[queue].pl); m != NULL; m = next) {
148                 KASSERT(m->queue == queue,
149                         ("vm_contig_clean: page %p's queue is not %d", m, queue));
150                 
151                 next = TAILQ_NEXT(m, pageq);
152                 
153                 if (vm_page_sleep_busy(m, TRUE, "vpctw0"))
154                         return (TRUE);
155                 
156                 vm_page_test_dirty(m);
157                 if (m->dirty) {
158                         object = m->object;
159                         if (object->type == OBJT_VNODE) {
160                                 vn_lock(object->handle, 
161                                         LK_EXCLUSIVE | LK_RETRY, curthread);
162                                 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
163                                 VOP_UNLOCK(((struct vnode *)object->handle),
164                                             0, curthread);
165                                 return (TRUE);
166                         } else if (object->type == OBJT_SWAP ||
167                                         object->type == OBJT_DEFAULT) {
168                                 m_tmp = m;
169                                 vm_pageout_flush(&m_tmp, 1, 0);
170                                 return (TRUE);
171                         }
172                 }
173                 
174                 if ((m->dirty == 0) && (m->busy == 0) && (m->hold_count == 0))
175                         vm_page_cache(m);
176         }
177
178         return (FALSE);
179 }
180
181 /*
182  * vm_contig_pg_alloc:
183  *
184  * Allocate contiguous pages from the VM.  This function does not
185  * map the allocated pages into the kernel map, otherwise it is
186  * impossible to make large allocations (i.e. >2G).
187  *
188  * Malloc()'s data structures have been used for collection of
189  * statistics and for allocations of less than a page.
190  *
191  */
192 int
193 vm_contig_pg_alloc(
194         unsigned long size,
195         vm_paddr_t low,
196         vm_paddr_t high,
197         unsigned long alignment,
198         unsigned long boundary)
199 {
200         int i, start, pass;
201         vm_offset_t phys;
202         vm_page_t pga = vm_page_array;
203
204         size = round_page(size);
205         if (size == 0)
206                 panic("vm_contig_pg_alloc: size must not be 0");
207         if ((alignment & (alignment - 1)) != 0)
208                 panic("vm_contig_pg_alloc: alignment must be a power of 2");
209         if ((boundary & (boundary - 1)) != 0)
210                 panic("vm_contig_pg_alloc: boundary must be a power of 2");
211
212         start = 0;
213         for (pass = 0; pass <= 1; pass++) {
214                 crit_enter();
215 again:
216                 /*
217                  * Find first page in array that is free, within range, aligned, and
218                  * such that the boundary won't be crossed.
219                  */
220                 for (i = start; i < vmstats.v_page_count; i++) {
221                         int pqtype;
222                         phys = VM_PAGE_TO_PHYS(&pga[i]);
223                         pqtype = pga[i].queue - pga[i].pc;
224                         if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) &&
225                             (phys >= low) && (phys < high) &&
226                             ((phys & (alignment - 1)) == 0) &&
227                             (((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0))
228                                 break;
229                 }
230
231                 /*
232                  * If we cannot find the page in the given range, or we have
233                  * crossed the boundary, call the vm_contig_pg_clean() function
234                  * for flushing out the queues, and returning it back to
235                  * normal state.
236                  */
237                 if ((i == vmstats.v_page_count) ||
238                         ((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) {
239
240 again1:
241                         if (vm_contig_pg_clean(PQ_INACTIVE))
242                                 goto again1;
243                         if (vm_contig_pg_clean(PQ_ACTIVE))
244                                 goto again1;
245
246                         crit_exit();
247                         continue;       /* next pass */
248                 }
249                 start = i;
250
251                 /*
252                  * Check successive pages for contiguous and free.
253                  *
254                  * (still in critical section)
255                  */
256                 for (i = start + 1; i < (start + size / PAGE_SIZE); i++) {
257                         int pqtype;
258                         pqtype = pga[i].queue - pga[i].pc;
259                         if ((VM_PAGE_TO_PHYS(&pga[i]) !=
260                             (VM_PAGE_TO_PHYS(&pga[i - 1]) + PAGE_SIZE)) ||
261                             ((pqtype != PQ_FREE) && (pqtype != PQ_CACHE))) {
262                                 start++;
263                                 goto again;
264                         }
265                 }
266
267                 /*
268                  * (still in critical section)
269                  */
270                 for (i = start; i < (start + size / PAGE_SIZE); i++) {
271                         int pqtype;
272                         vm_page_t m = &pga[i];
273
274                         pqtype = m->queue - m->pc;
275                         if (pqtype == PQ_CACHE) {
276                                 vm_page_busy(m);
277                                 vm_page_free(m);
278                         }
279                         vm_page_unqueue_nowakeup(m);
280                         m->valid = VM_PAGE_BITS_ALL;
281                         if (m->flags & PG_ZERO)
282                                 vm_page_zero_count--;
283                         /* Don't clear the PG_ZERO flag, we'll need it later. */
284                         m->flags &= PG_ZERO;
285                         KASSERT(m->dirty == 0,
286                                 ("vm_contig_pg_alloc: page %p was dirty", m));
287                         m->wire_count = 0;
288                         m->busy = 0;
289                         m->object = NULL;
290                 }
291
292                 /*
293                  * Our job is done, return the index page of vm_page_array.
294                  */
295                 crit_exit();
296                 return (start); /* aka &pga[start] */
297         }
298
299         /*
300          * Failed.
301          */
302         crit_exit();
303         return (-1);
304 }
305
306 /*
307  * vm_contig_pg_free:
308  *
309  * Remove pages previously allocated by vm_contig_pg_alloc, and
310  * assume all references to the pages have been removed, and that
311  * it is OK to add them back to the free list.
312  */
313 void
314 vm_contig_pg_free(int start, u_long size)
315 {
316         vm_page_t pga = vm_page_array;
317         int i;
318         
319         size = round_page(size);
320         if (size == 0)
321                 panic("vm_contig_pg_free: size must not be 0");
322
323         for (i = start; i < (start + size / PAGE_SIZE); i++) {
324                 vm_page_free(&pga[i]);
325         }
326 }
327
328 /*
329  * vm_contig_pg_kmap:
330  *
331  * Map previously allocated (vm_contig_pg_alloc) range of pages from
332  * vm_page_array[] into the KVA.  Once mapped, the pages are part of
333  * the Kernel, and are to free'ed with kmem_free(kernel_map, addr, size).
334  */
335 vm_offset_t
336 vm_contig_pg_kmap(int start, u_long size, vm_map_t map, int flags)
337 {
338         vm_offset_t addr, tmp_addr;
339         vm_page_t pga = vm_page_array;
340         int i, count;
341
342         size = round_page(size);
343         if (size == 0)
344                 panic("vm_contig_pg_kmap: size must not be 0");
345
346         crit_enter();
347
348         /*
349          * We've found a contiguous chunk that meets our requirements.
350          * Allocate KVM, and assign phys pages and return a kernel VM
351          * pointer.
352          */
353         count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
354         vm_map_lock(map);
355         if (vm_map_findspace(map, vm_map_min(map), size, 1, &addr) !=
356             KERN_SUCCESS) {
357                 /*
358                  * XXX We almost never run out of kernel virtual
359                  * space, so we don't make the allocated memory
360                  * above available.
361                  */
362                 vm_map_unlock(map);
363                 vm_map_entry_release(count);
364                 crit_exit();
365                 return (0);
366         }
367         vm_object_reference(kernel_object);
368         vm_map_insert(map, &count, 
369             kernel_object, addr - VM_MIN_KERNEL_ADDRESS,
370             addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
371         vm_map_unlock(map);
372         vm_map_entry_release(count);
373
374         tmp_addr = addr;
375         for (i = start; i < (start + size / PAGE_SIZE); i++) {
376                 vm_page_t m = &pga[i];
377                 vm_page_insert(m, kernel_object,
378                         OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
379                 if ((flags & M_ZERO) && !(m->flags & PG_ZERO))
380                         pmap_zero_page(VM_PAGE_TO_PHYS(m));
381                 m->flags = 0;
382                 tmp_addr += PAGE_SIZE;
383         }
384         vm_map_wire(map, addr, addr + size, 0);
385
386         crit_exit();
387         return (addr);
388 }
389
390 void *
391 contigmalloc(
392         unsigned long size,     /* should be size_t here and for malloc() */
393         struct malloc_type *type,
394         int flags,
395         vm_paddr_t low,
396         vm_paddr_t high,
397         unsigned long alignment,
398         unsigned long boundary)
399 {
400         return contigmalloc_map(size, type, flags, low, high, alignment,
401                         boundary, kernel_map);
402 }
403
404 void *
405 contigmalloc_map(
406         unsigned long size,     /* should be size_t here and for malloc() */
407         struct malloc_type *type,
408         int flags,
409         vm_paddr_t low,
410         vm_paddr_t high,
411         unsigned long alignment,
412         unsigned long boundary,
413         vm_map_t map)
414 {
415         int index;
416         void *rv;
417
418         index = vm_contig_pg_alloc(size, low, high, alignment, boundary);
419         if (index < 0) {
420                 printf("contigmalloc_map: failed in index < 0 case!");
421                 return NULL;
422         }
423
424         rv = (void *) vm_contig_pg_kmap(index, size, map, flags);
425         if (!rv)
426                 vm_contig_pg_free(index, size);
427         
428         return rv;
429 }
430
431 void
432 contigfree(void *addr, unsigned long size, struct malloc_type *type)
433 {
434         kmem_free(kernel_map, (vm_offset_t)addr, size);
435 }
436
437 vm_offset_t
438 vm_page_alloc_contig(
439         vm_offset_t size,
440         vm_paddr_t low,
441         vm_paddr_t high,
442         vm_offset_t alignment)
443 {
444         return ((vm_offset_t)contigmalloc_map(size, M_DEVBUF, M_NOWAIT, low,
445                                 high, alignment, 0ul, kernel_map));
446 }