]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/vm/vm_contig.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / vm / vm_contig.c
1 /*-
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  *      from: @(#)vm_page.c     7.4 (Berkeley) 5/7/91
33  */
34
35 /*-
36  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
37  * All rights reserved.
38  *
39  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
40  *
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.
46  *
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.
50  *
51  * Carnegie Mellon requests users of this software to return to
52  *
53  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
54  *  School of Computer Science
55  *  Carnegie Mellon University
56  *  Pittsburgh PA 15213-3890
57  *
58  * any improvements or extensions that they make and grant Carnegie the
59  * rights to redistribute these changes.
60  */
61
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/lock.h>
68 #include <sys/malloc.h>
69 #include <sys/mount.h>
70 #include <sys/mutex.h>
71 #include <sys/proc.h>
72 #include <sys/kernel.h>
73 #include <sys/linker_set.h>
74 #include <sys/sysctl.h>
75 #include <sys/vmmeter.h>
76 #include <sys/vnode.h>
77
78 #include <vm/vm.h>
79 #include <vm/vm_param.h>
80 #include <vm/vm_kern.h>
81 #include <vm/pmap.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pageout.h>
86 #include <vm/vm_pager.h>
87 #include <vm/vm_phys.h>
88 #include <vm/vm_extern.h>
89
90 static void vm_contig_grow_cache(int tries);
91
92 static int
93 vm_contig_launder_page(vm_page_t m, vm_page_t *next)
94 {
95         vm_object_t object;
96         vm_page_t m_tmp;
97         struct vnode *vp;
98         struct mount *mp;
99         int vfslocked;
100
101         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
102         object = m->object;
103         if (!VM_OBJECT_TRYLOCK(object) &&
104             !vm_pageout_fallback_object_lock(m, next)) {
105                 VM_OBJECT_UNLOCK(object);
106                 return (EAGAIN);
107         }
108         if (vm_page_sleep_if_busy(m, TRUE, "vpctw0")) {
109                 VM_OBJECT_UNLOCK(object);
110                 vm_page_lock_queues();
111                 return (EBUSY);
112         }
113         vm_page_test_dirty(m);
114         if (m->dirty == 0 && m->hold_count == 0)
115                 pmap_remove_all(m);
116         if (m->dirty) {
117                 if ((object->flags & OBJ_DEAD) != 0) {
118                         VM_OBJECT_UNLOCK(object);
119                         return (EAGAIN);
120                 }
121                 if (object->type == OBJT_VNODE) {
122                         vm_page_unlock_queues();
123                         vp = object->handle;
124                         vm_object_reference_locked(object);
125                         VM_OBJECT_UNLOCK(object);
126                         (void) vn_start_write(vp, &mp, V_WAIT);
127                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
128                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
129                         VM_OBJECT_LOCK(object);
130                         vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
131                         VM_OBJECT_UNLOCK(object);
132                         VOP_UNLOCK(vp, 0);
133                         VFS_UNLOCK_GIANT(vfslocked);
134                         vm_object_deallocate(object);
135                         vn_finished_write(mp);
136                         vm_page_lock_queues();
137                         return (0);
138                 } else if (object->type == OBJT_SWAP ||
139                            object->type == OBJT_DEFAULT) {
140                         m_tmp = m;
141                         vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC);
142                         VM_OBJECT_UNLOCK(object);
143                         return (0);
144                 }
145         } else if (m->hold_count == 0)
146                 vm_page_cache(m);
147         VM_OBJECT_UNLOCK(object);
148         return (0);
149 }
150
151 static int
152 vm_contig_launder(int queue)
153 {
154         vm_page_t m, next;
155         int error;
156
157         TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
158
159                 /* Skip marker pages */
160                 if ((m->flags & PG_MARKER) != 0)
161                         continue;
162
163                 KASSERT(VM_PAGE_INQUEUE2(m, queue),
164                     ("vm_contig_launder: page %p's queue is not %d", m, queue));
165                 error = vm_contig_launder_page(m, &next);
166                 if (error == 0)
167                         return (TRUE);
168                 if (error == EBUSY)
169                         return (FALSE);
170         }
171         return (FALSE);
172 }
173
174 /*
175  *      Frees the given physically contiguous pages.
176  *
177  *      N.B.: Any pages with PG_ZERO set must, in fact, be zero filled.
178  */
179 static void
180 vm_page_release_contig(vm_page_t m, vm_pindex_t count)
181 {
182
183         while (count--) {
184                 /* Leave PG_ZERO unchanged. */
185                 vm_page_free_toq(m);
186                 m++;
187         }
188 }
189
190 /*
191  * Increase the number of cached pages.
192  */
193 static void
194 vm_contig_grow_cache(int tries)
195 {
196         int actl, actmax, inactl, inactmax;
197
198         vm_page_lock_queues();
199         inactl = 0;
200         inactmax = tries < 1 ? 0 : cnt.v_inactive_count;
201         actl = 0;
202         actmax = tries < 2 ? 0 : cnt.v_active_count;
203 again:
204         if (inactl < inactmax && vm_contig_launder(PQ_INACTIVE)) {
205                 inactl++;
206                 goto again;
207         }
208         if (actl < actmax && vm_contig_launder(PQ_ACTIVE)) {
209                 actl++;
210                 goto again;
211         }
212         vm_page_unlock_queues();
213 }
214
215 /*
216  * Allocates a region from the kernel address map and pages within the
217  * specified physical address range to the kernel object, creates a wired
218  * mapping from the region to these pages, and returns the region's starting
219  * virtual address.  The allocated pages are not necessarily physically
220  * contiguous.  If M_ZERO is specified through the given flags, then the pages
221  * are zeroed before they are mapped.
222  */
223 vm_offset_t
224 kmem_alloc_attr(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
225     vm_paddr_t high, vm_memattr_t memattr)
226 {
227         vm_object_t object = kernel_object;
228         vm_offset_t addr, i, offset;
229         vm_page_t m;
230         int tries;
231
232         size = round_page(size);
233         vm_map_lock(map);
234         if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
235                 vm_map_unlock(map);
236                 return (0);
237         }
238         offset = addr - VM_MIN_KERNEL_ADDRESS;
239         vm_object_reference(object);
240         vm_map_insert(map, object, offset, addr, addr + size, VM_PROT_ALL,
241             VM_PROT_ALL, 0);
242         VM_OBJECT_LOCK(object);
243         for (i = 0; i < size; i += PAGE_SIZE) {
244                 tries = 0;
245 retry:
246                 m = vm_phys_alloc_contig(1, low, high, PAGE_SIZE, 0);
247                 if (m == NULL) {
248                         if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
249                                 VM_OBJECT_UNLOCK(object);
250                                 vm_map_unlock(map);
251                                 vm_contig_grow_cache(tries);
252                                 vm_map_lock(map);
253                                 VM_OBJECT_LOCK(object);
254                                 goto retry;
255                         }
256                         while (i != 0) {
257                                 i -= PAGE_SIZE;
258                                 m = vm_page_lookup(object, OFF_TO_IDX(offset +
259                                     i));
260                                 vm_page_lock_queues();
261                                 vm_page_free(m);
262                                 vm_page_unlock_queues();
263                         }
264                         VM_OBJECT_UNLOCK(object);
265                         vm_map_delete(map, addr, addr + size);
266                         vm_map_unlock(map);
267                         return (0);
268                 }
269                 if (memattr != VM_MEMATTR_DEFAULT)
270                         pmap_page_set_memattr(m, memattr);
271                 vm_page_insert(m, object, OFF_TO_IDX(offset + i));
272                 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
273                         pmap_zero_page(m);
274                 m->valid = VM_PAGE_BITS_ALL;
275         }
276         VM_OBJECT_UNLOCK(object);
277         vm_map_unlock(map);
278         vm_map_wire(map, addr, addr + size, VM_MAP_WIRE_SYSTEM |
279             VM_MAP_WIRE_NOHOLES);
280         return (addr);
281 }
282
283 /*
284  *      Allocates a region from the kernel address map, inserts the
285  *      given physically contiguous pages into the kernel object,
286  *      creates a wired mapping from the region to the pages, and
287  *      returns the region's starting virtual address.  If M_ZERO is
288  *      specified through the given flags, then the pages are zeroed
289  *      before they are mapped.
290  */
291 static vm_offset_t
292 contigmapping(vm_map_t map, vm_size_t size, vm_page_t m, vm_memattr_t memattr,
293     int flags)
294 {
295         vm_object_t object = kernel_object;
296         vm_offset_t addr, tmp_addr;
297  
298         vm_map_lock(map);
299         if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
300                 vm_map_unlock(map);
301                 return (0);
302         }
303         vm_object_reference(object);
304         vm_map_insert(map, object, addr - VM_MIN_KERNEL_ADDRESS,
305             addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
306         vm_map_unlock(map);
307         VM_OBJECT_LOCK(object);
308         for (tmp_addr = addr; tmp_addr < addr + size; tmp_addr += PAGE_SIZE) {
309                 if (memattr != VM_MEMATTR_DEFAULT)
310                         pmap_page_set_memattr(m, memattr);
311                 vm_page_insert(m, object,
312                     OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
313                 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
314                         pmap_zero_page(m);
315                 m->valid = VM_PAGE_BITS_ALL;
316                 m++;
317         }
318         VM_OBJECT_UNLOCK(object);
319         vm_map_wire(map, addr, addr + size,
320             VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
321         return (addr);
322 }
323
324 void *
325 contigmalloc(
326         unsigned long size,     /* should be size_t here and for malloc() */
327         struct malloc_type *type,
328         int flags,
329         vm_paddr_t low,
330         vm_paddr_t high,
331         unsigned long alignment,
332         unsigned long boundary)
333 {
334         void *ret;
335
336         ret = (void *)kmem_alloc_contig(kernel_map, size, flags, low, high,
337             alignment, boundary, VM_MEMATTR_DEFAULT);
338         if (ret != NULL)
339                 malloc_type_allocated(type, round_page(size));
340         return (ret);
341 }
342
343 vm_offset_t
344 kmem_alloc_contig(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
345     vm_paddr_t high, unsigned long alignment, unsigned long boundary,
346     vm_memattr_t memattr)
347 {
348         vm_offset_t ret;
349         vm_page_t pages;
350         unsigned long npgs;
351         int tries;
352
353         size = round_page(size);
354         npgs = size >> PAGE_SHIFT;
355         tries = 0;
356 retry:
357         pages = vm_phys_alloc_contig(npgs, low, high, alignment, boundary);
358         if (pages == NULL) {
359                 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
360                         vm_contig_grow_cache(tries);
361                         tries++;
362                         goto retry;
363                 }
364                 ret = 0;
365         } else {
366                 ret = contigmapping(map, size, pages, memattr, flags);
367                 if (ret == 0)
368                         vm_page_release_contig(pages, npgs);
369         }
370         return (ret);
371 }
372
373 void
374 contigfree(void *addr, unsigned long size, struct malloc_type *type)
375 {
376
377         kmem_free(kernel_map, (vm_offset_t)addr, size);
378         malloc_type_freed(type, round_page(size));
379 }