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