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: releng/9.2/sys/vm/vm_contig.c 240757 2012-09-20 17:02:04Z alc $");
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/eventhandler.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/mount.h>
71 #include <sys/mutex.h>
72 #include <sys/proc.h>
73 #include <sys/kernel.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 int
91 vm_contig_launder_page(vm_page_t m, vm_page_t *next, int tries)
92 {
93 vm_object_t object;
94 vm_page_t m_tmp;
95 struct vnode *vp;
96 struct mount *mp;
97 int vfslocked;
98
99 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
100 if (!vm_pageout_page_lock(m, next) || m->hold_count != 0) {
101 vm_page_unlock(m);
102 return (EAGAIN);
103 }
104 object = m->object;
105 if (!VM_OBJECT_TRYLOCK(object) &&
106 (!vm_pageout_fallback_object_lock(m, next) || m->hold_count != 0)) {
107 vm_page_unlock(m);
108 VM_OBJECT_UNLOCK(object);
109 return (EAGAIN);
110 }
111 if ((m->oflags & VPO_BUSY) != 0 || m->busy != 0) {
112 if (tries == 0) {
113 vm_page_unlock(m);
114 VM_OBJECT_UNLOCK(object);
115 return (EAGAIN);
116 }
117 vm_page_sleep(m, "vpctw0");
118 VM_OBJECT_UNLOCK(object);
119 vm_page_lock_queues();
120 return (EBUSY);
121 }
122 vm_page_test_dirty(m);
123 if (m->dirty == 0)
124 pmap_remove_all(m);
125 if (m->dirty != 0) {
126 vm_page_unlock(m);
127 if (tries == 0 || (object->flags & OBJ_DEAD) != 0) {
128 VM_OBJECT_UNLOCK(object);
129 return (EAGAIN);
130 }
131 if (object->type == OBJT_VNODE) {
132 vm_page_unlock_queues();
133 vp = object->handle;
134 vm_object_reference_locked(object);
135 VM_OBJECT_UNLOCK(object);
136 (void) vn_start_write(vp, &mp, V_WAIT);
137 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
138 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
139 VM_OBJECT_LOCK(object);
140 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
141 VM_OBJECT_UNLOCK(object);
142 VOP_UNLOCK(vp, 0);
143 VFS_UNLOCK_GIANT(vfslocked);
144 vm_object_deallocate(object);
145 vn_finished_write(mp);
146 vm_page_lock_queues();
147 return (0);
148 } else if (object->type == OBJT_SWAP ||
149 object->type == OBJT_DEFAULT) {
150 vm_page_unlock_queues();
151 m_tmp = m;
152 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC, 0,
153 NULL, NULL);
154 VM_OBJECT_UNLOCK(object);
155 vm_page_lock_queues();
156 return (0);
157 }
158 } else {
159 vm_page_cache(m);
160 vm_page_unlock(m);
161 }
162 VM_OBJECT_UNLOCK(object);
163 return (EAGAIN);
164 }
165
166 static int
167 vm_contig_launder(int queue, int tries, vm_paddr_t low, vm_paddr_t high)
168 {
169 vm_page_t m, next;
170 vm_paddr_t pa;
171 int error;
172
173 TAILQ_FOREACH_SAFE(m, &vm_page_queues[queue].pl, pageq, next) {
174 KASSERT(m->queue == queue,
175 ("vm_contig_launder: page %p's queue is not %d", m, queue));
176 if ((m->flags & PG_MARKER) != 0)
177 continue;
178 pa = VM_PAGE_TO_PHYS(m);
179 if (pa < low || pa + PAGE_SIZE > high)
180 continue;
181 error = vm_contig_launder_page(m, &next, tries);
182 if (error == 0)
183 return (TRUE);
184 if (error == EBUSY)
185 return (FALSE);
186 }
187 return (FALSE);
188 }
189
190 /*
191 * Frees the given physically contiguous pages.
192 *
193 * N.B.: Any pages with PG_ZERO set must, in fact, be zero filled.
194 */
195 static void
196 vm_page_release_contig(vm_page_t m, vm_pindex_t count)
197 {
198
199 while (count--) {
200 /* Leave PG_ZERO unchanged. */
201 vm_page_free_toq(m);
202 m++;
203 }
204 }
205
206 /*
207 * Increase the number of cached pages. The specified value, "tries",
208 * determines which categories of pages are cached:
209 *
210 * 0: All clean, inactive pages within the specified physical address range
211 * are cached. Will not sleep.
212 * 1: The vm_lowmem handlers are called. All inactive pages within
213 * the specified physical address range are cached. May sleep.
214 * 2: The vm_lowmem handlers are called. All inactive and active pages
215 * within the specified physical address range are cached. May sleep.
216 */
217 void
218 vm_contig_grow_cache(int tries, vm_paddr_t low, vm_paddr_t high)
219 {
220 int actl, actmax, inactl, inactmax;
221
222 if (tries > 0) {
223 /*
224 * Decrease registered cache sizes. The vm_lowmem handlers
225 * may acquire locks and/or sleep, so they can only be invoked
226 * when "tries" is greater than zero.
227 */
228 EVENTHANDLER_INVOKE(vm_lowmem, 0);
229
230 /*
231 * We do this explicitly after the caches have been drained
232 * above.
233 */
234 uma_reclaim();
235 }
236 vm_page_lock_queues();
237 inactl = 0;
238 inactmax = cnt.v_inactive_count;
239 actl = 0;
240 actmax = tries < 2 ? 0 : cnt.v_active_count;
241 again:
242 if (inactl < inactmax && vm_contig_launder(PQ_INACTIVE, tries, low,
243 high)) {
244 inactl++;
245 goto again;
246 }
247 if (actl < actmax && vm_contig_launder(PQ_ACTIVE, tries, low, high)) {
248 actl++;
249 goto again;
250 }
251 vm_page_unlock_queues();
252 }
253
254 /*
255 * Allocates a region from the kernel address map and pages within the
256 * specified physical address range to the kernel object, creates a wired
257 * mapping from the region to these pages, and returns the region's starting
258 * virtual address. The allocated pages are not necessarily physically
259 * contiguous. If M_ZERO is specified through the given flags, then the pages
260 * are zeroed before they are mapped.
261 */
262 vm_offset_t
263 kmem_alloc_attr(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
264 vm_paddr_t high, vm_memattr_t memattr)
265 {
266 vm_object_t object = kernel_object;
267 vm_offset_t addr, i, offset;
268 vm_page_t m;
269 int tries;
270
271 size = round_page(size);
272 vm_map_lock(map);
273 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
274 vm_map_unlock(map);
275 return (0);
276 }
277 offset = addr - VM_MIN_KERNEL_ADDRESS;
278 vm_object_reference(object);
279 vm_map_insert(map, object, offset, addr, addr + size, VM_PROT_ALL,
280 VM_PROT_ALL, 0);
281 VM_OBJECT_LOCK(object);
282 for (i = 0; i < size; i += PAGE_SIZE) {
283 tries = 0;
284 retry:
285 m = vm_phys_alloc_contig(1, low, high, PAGE_SIZE, 0);
286 if (m == NULL) {
287 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
288 VM_OBJECT_UNLOCK(object);
289 vm_map_unlock(map);
290 vm_contig_grow_cache(tries, low, high);
291 vm_map_lock(map);
292 VM_OBJECT_LOCK(object);
293 tries++;
294 goto retry;
295 }
296 while (i != 0) {
297 i -= PAGE_SIZE;
298 m = vm_page_lookup(object, OFF_TO_IDX(offset +
299 i));
300 vm_page_free(m);
301 }
302 VM_OBJECT_UNLOCK(object);
303 vm_map_delete(map, addr, addr + size);
304 vm_map_unlock(map);
305 return (0);
306 }
307 if (memattr != VM_MEMATTR_DEFAULT)
308 pmap_page_set_memattr(m, memattr);
309 vm_page_insert(m, object, OFF_TO_IDX(offset + i));
310 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
311 pmap_zero_page(m);
312 m->valid = VM_PAGE_BITS_ALL;
313 }
314 VM_OBJECT_UNLOCK(object);
315 vm_map_unlock(map);
316 vm_map_wire(map, addr, addr + size, VM_MAP_WIRE_SYSTEM |
317 VM_MAP_WIRE_NOHOLES);
318 return (addr);
319 }
320
321 /*
322 * Allocates a region from the kernel address map, inserts the
323 * given physically contiguous pages into the kernel object,
324 * creates a wired mapping from the region to the pages, and
325 * returns the region's starting virtual address. If M_ZERO is
326 * specified through the given flags, then the pages are zeroed
327 * before they are mapped.
328 */
329 static vm_offset_t
330 contigmapping(vm_map_t map, vm_size_t size, vm_page_t m, vm_memattr_t memattr,
331 int flags)
332 {
333 vm_object_t object = kernel_object;
334 vm_offset_t addr, tmp_addr;
335
336 vm_map_lock(map);
337 if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
338 vm_map_unlock(map);
339 return (0);
340 }
341 vm_object_reference(object);
342 vm_map_insert(map, object, addr - VM_MIN_KERNEL_ADDRESS,
343 addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
344 vm_map_unlock(map);
345 VM_OBJECT_LOCK(object);
346 for (tmp_addr = addr; tmp_addr < addr + size; tmp_addr += PAGE_SIZE) {
347 if (memattr != VM_MEMATTR_DEFAULT)
348 pmap_page_set_memattr(m, memattr);
349 vm_page_insert(m, object,
350 OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
351 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
352 pmap_zero_page(m);
353 m->valid = VM_PAGE_BITS_ALL;
354 m++;
355 }
356 VM_OBJECT_UNLOCK(object);
357 vm_map_wire(map, addr, addr + size,
358 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
359 return (addr);
360 }
361
362 void *
363 contigmalloc(
364 unsigned long size, /* should be size_t here and for malloc() */
365 struct malloc_type *type,
366 int flags,
367 vm_paddr_t low,
368 vm_paddr_t high,
369 unsigned long alignment,
370 unsigned long boundary)
371 {
372 void *ret;
373
374 ret = (void *)kmem_alloc_contig(kernel_map, size, flags, low, high,
375 alignment, boundary, VM_MEMATTR_DEFAULT);
376 if (ret != NULL)
377 malloc_type_allocated(type, round_page(size));
378 return (ret);
379 }
380
381 vm_offset_t
382 kmem_alloc_contig(vm_map_t map, vm_size_t size, int flags, vm_paddr_t low,
383 vm_paddr_t high, unsigned long alignment, unsigned long boundary,
384 vm_memattr_t memattr)
385 {
386 vm_offset_t ret;
387 vm_page_t pages;
388 unsigned long npgs;
389 int tries;
390
391 size = round_page(size);
392 npgs = size >> PAGE_SHIFT;
393 tries = 0;
394 retry:
395 pages = vm_phys_alloc_contig(npgs, low, high, alignment, boundary);
396 if (pages == NULL) {
397 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
398 vm_contig_grow_cache(tries, low, high);
399 tries++;
400 goto retry;
401 }
402 ret = 0;
403 } else {
404 ret = contigmapping(map, size, pages, memattr, flags);
405 if (ret == 0)
406 vm_page_release_contig(pages, npgs);
407 }
408 return (ret);
409 }
410
411 void
412 contigfree(void *addr, unsigned long size, struct malloc_type *type)
413 {
414
415 kmem_free(kernel_map, (vm_offset_t)addr, size);
416 malloc_type_freed(type, round_page(size));
417 }
Cache object: e0c5458673e4bc20047c07acd2afa51d
|