FreeBSD/Linux Kernel Cross Reference
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/eventhandler.h>
68 #include <sys/lock.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_extern.h>
87
88 static int
89 vm_contig_launder_page(vm_page_t m, vm_page_t *next, int tries)
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 if (!vm_pageout_page_lock(m, next) || m->hold_count != 0) {
99 vm_page_unlock(m);
100 return (EAGAIN);
101 }
102 object = m->object;
103 if (!VM_OBJECT_TRYLOCK(object) &&
104 (!vm_pageout_fallback_object_lock(m, next) || m->hold_count != 0)) {
105 vm_page_unlock(m);
106 VM_OBJECT_UNLOCK(object);
107 return (EAGAIN);
108 }
109 if ((m->oflags & VPO_BUSY) != 0 || m->busy != 0) {
110 if (tries == 0) {
111 vm_page_unlock(m);
112 VM_OBJECT_UNLOCK(object);
113 return (EAGAIN);
114 }
115 vm_page_sleep(m, "vpctw0");
116 VM_OBJECT_UNLOCK(object);
117 vm_page_lock_queues();
118 return (EBUSY);
119 }
120 vm_page_test_dirty(m);
121 if (m->dirty == 0)
122 pmap_remove_all(m);
123 if (m->dirty != 0) {
124 vm_page_unlock(m);
125 if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
126 VM_OBJECT_UNLOCK(object);
127 return (EAGAIN);
128 }
129 if (object->type == OBJT_VNODE) {
130 vm_page_unlock_queues();
131 vp = object->handle;
132 vm_object_reference_locked(object);
133 VM_OBJECT_UNLOCK(object);
134 (void) vn_start_write(vp, &mp, V_WAIT);
135 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
136 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
137 VM_OBJECT_LOCK(object);
138 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
139 VM_OBJECT_UNLOCK(object);
140 VOP_UNLOCK(vp, 0);
141 VFS_UNLOCK_GIANT(vfslocked);
142 vm_object_deallocate(object);
143 vn_finished_write(mp);
144 vm_page_lock_queues();
145 return (0);
146 } else if (object->type == OBJT_SWAP ||
147 object->type == OBJT_DEFAULT) {
148 vm_page_unlock_queues();
149 m_tmp = m;
150 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 0,
151 NULL, NULL);
152 VM_OBJECT_UNLOCK(object);
153 vm_page_lock_queues();
154 return (0);
155 }
156 } else {
157 vm_page_cache(m);
158 vm_page_unlock(m);
159 }
160 VM_OBJECT_UNLOCK(object);
161 return (EAGAIN);
162 }
163
164 static int
165 vm_contig_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high)
166 {
167 vm_page_t m, next;
168 vm_paddr_t pa;
169 int error;
170
171 TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
172 KASSERT(m->queue == queue,
173 ("vm_contig_launder: page %p's queue is not %d", m, queue));
174 if ((m->flags & PG_MARKER) != 0)
175 continue;
176 pa = VM_PAGE_TO_PHYS(m);
177 if (pa < low || pa + PAGE_SIZE > high)
178 continue;
179 error = vm_contig_launder_page(m, &next, tries);
180 if (error == 0)
181 return (TRUE);
182 if (error == EBUSY)
183 return (FALSE);
184 }
185 return (FALSE);
186 }
187
188 /*
189 * Increase the number of cached pages. The specified value, "tries",
190 * determines which categories of pages are cached:
191 *
192 * 0: All clean, inactive pages within the specified physical address range
193 * are cached. Will not sleep.
194 * 1: The vm_lowmem handlers are called. All inactive pages within
195 * the specified physical address range are cached. May sleep.
196 * 2: The vm_lowmem handlers are called. All inactive and active pages
197 * within the specified physical address range are cached. May sleep.
198 */
199 void
200 vm_contig_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
201 {
202 int actl, actmax, inactl, inactmax;
203
204 if (tries > 0) {
205 /*
206 * Decrease registered cache sizes. The vm_lowmem handlers
207 * may acquire locks and/or sleep, so they can only be invoked
208 * when "tries" is greater than zero.
209 */
210 EVENTHANDLER_INVOKE(vm_lowmem, 0);
211
212 /*
213 * We do this explicitly after the caches have been drained
214 * above.
215 */
216 uma_reclaim();
217 }
218 vm_page_lock_queues();
219 inactl = 0;
220 inactmax = cnt.v_inactive_count;
221 actl = 0;
222 actmax = tries < 2 ? 0 : cnt.v_active_count;
223 again:
224 if (inactl < inactmax && vm_contig_launder(PQ_INACTIVE, tries, low,
225 high)) {
226 inactl++;
227 goto again;
228 }
229 if (actl < actmax && vm_contig_launder(PQ_ACTIVE, tries, low, high)) {
230 actl++;
231 goto again;
232 }
233 vm_page_unlock_queues();
234 }
235
236 /*
237 * Allocates a region from the kernel address map and pages within the
238 * specified physical address range to the kernel object, creates a wired
239 * mapping from the region to these pages, and returns the region's starting
240 * virtual address. The allocated pages are not necessarily physically
241 * contiguous. If M_ZERO is specified through the given flags, then the pages
242 * are zeroed before they are mapped.
243 */
244 vm_offset_t
245 kmem_alloc_attr(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
246 vm_paddr_t high, vm_memattr_t memattr)
247 {
248 vm_object_t object = kernel_object;
249 vm_offset_t addr;
250 vm_ooffset_t end_offset, offset;
251 vm_page_t m;
252 int pflags, tries;
253
254 size = round_page(size);
255 vm_map_lock(map);
256 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
257 vm_map_unlock(map);
258 return (0);
259 }
260 offset = addr - VM_MIN_KERNEL_ADDRESS;
261 vm_object_reference(object);
262 vm_map_insert(map, object, offset, addr, addr + size, VM_PROT_ALL,
263 VM_PROT_ALL, 0);
264 if ((flags & (M_NOWAIT | M_USE_RESERVE)) == M_NOWAIT)
265 pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_NOBUSY;
266 else
267 pflags = VM_ALLOC_SYSTEM | VM_ALLOC_NOBUSY;
268 if (flags & M_ZERO)
269 pflags |= VM_ALLOC_ZERO;
270 VM_OBJECT_LOCK(object);
271 end_offset = offset + size;
272 for (; offset < end_offset; offset += PAGE_SIZE) {
273 tries = 0;
274 retry:
275 m = vm_page_alloc_contig(object, OFF_TO_IDX(offset), pflags, 1,
276 low, high, PAGE_SIZE, 0, memattr);
277 if (m == NULL) {
278 VM_OBJECT_UNLOCK(object);
279 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
280 vm_map_unlock(map);
281 vm_contig_grow_cache(tries, low, high);
282 vm_map_lock(map);
283 VM_OBJECT_LOCK(object);
284 tries++;
285 goto retry;
286 }
287 /*
288 * Since the pages that were allocated by any previous
289 * iterations of this loop are not busy, they can be
290 * freed by vm_object_page_remove(), which is called
291 * by vm_map_delete().
292 */
293 vm_map_delete(map, addr, addr + size);
294 vm_map_unlock(map);
295 return (0);
296 }
297 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
298 pmap_zero_page(m);
299 m->valid = VM_PAGE_BITS_ALL;
300 }
301 VM_OBJECT_UNLOCK(object);
302 vm_map_unlock(map);
303 vm_map_wire(map, addr, addr + size, VM_MAP_WIRE_SYSTEM |
304 VM_MAP_WIRE_NOHOLES);
305 return (addr);
306 }
307
308 /*
309 * Allocates a region from the kernel address map, inserts the
310 * given physically contiguous pages into the kernel object,
311 * creates a wired mapping from the region to the pages, and
312 * returns the region's starting virtual address. If M_ZERO is
313 * specified through the given flags, then the pages are zeroed
314 * before they are mapped.
315 */
316 vm_offset_t
317 kmem_alloc_contig(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
318 vm_paddr_t high, u_long alignment, u_long boundary,
319 vm_memattr_t memattr)
320 {
321 vm_object_t object = kernel_object;
322 vm_offset_t addr;
323 vm_ooffset_t offset;
324 vm_page_t end_m, m;
325 int pflags, tries;
326
327 size = round_page(size);
328 vm_map_lock(map);
329 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
330 vm_map_unlock(map);
331 return (0);
332 }
333 offset = addr - VM_MIN_KERNEL_ADDRESS;
334 vm_object_reference(object);
335 vm_map_insert(map, object, offset, addr, addr + size, VM_PROT_ALL,
336 VM_PROT_ALL, 0);
337 if ((flags & (M_NOWAIT | M_USE_RESERVE)) == M_NOWAIT)
338 pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_NOBUSY;
339 else
340 pflags = VM_ALLOC_SYSTEM | VM_ALLOC_NOBUSY;
341 if (flags & M_ZERO)
342 pflags |= VM_ALLOC_ZERO;
343 VM_OBJECT_LOCK(object);
344 tries = 0;
345 retry:
346 m = vm_page_alloc_contig(object, OFF_TO_IDX(offset), pflags,
347 atop(size), low, high, alignment, boundary, memattr);
348 if (m == NULL) {
349 VM_OBJECT_UNLOCK(object);
350 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
351 vm_map_unlock(map);
352 vm_contig_grow_cache(tries, low, high);
353 vm_map_lock(map);
354 VM_OBJECT_LOCK(object);
355 tries++;
356 goto retry;
357 }
358 vm_map_delete(map, addr, addr + size);
359 vm_map_unlock(map);
360 return (0);
361 }
362 end_m = m + atop(size);
363 for (; m < end_m; m++) {
364 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
365 pmap_zero_page(m);
366 m->valid = VM_PAGE_BITS_ALL;
367 }
368 VM_OBJECT_UNLOCK(object);
369 vm_map_unlock(map);
370 vm_map_wire(map, addr, addr + size, VM_MAP_WIRE_SYSTEM |
371 VM_MAP_WIRE_NOHOLES);
372 return (addr);
373 }
Cache object: 94bda5a1823fbefae74c5fdea21d960e
|