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