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/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 int
91 vm_contig_launder_page(vm_page_t m)
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 object = m->object;
100 if (!VM_OBJECT_TRYLOCK(object))
101 return (EAGAIN);
102 if (vm_page_sleep_if_busy(m, TRUE, "vpctw0")) {
103 VM_OBJECT_UNLOCK(object);
104 vm_page_lock_queues();
105 return (EBUSY);
106 }
107 vm_page_test_dirty(m);
108 if (m->dirty == 0 && m->hold_count == 0)
109 pmap_remove_all(m);
110 if (m->dirty) {
111 if ((object->flags & OBJ_DEAD) != 0) {
112 VM_OBJECT_UNLOCK(object);
113 return (EAGAIN);
114 }
115 if (object->type == OBJT_VNODE) {
116 vm_page_unlock_queues();
117 vp = object->handle;
118 vm_object_reference_locked(object);
119 VM_OBJECT_UNLOCK(object);
120 (void) vn_start_write(vp, &mp, V_WAIT);
121 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
122 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
123 VM_OBJECT_LOCK(object);
124 vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
125 VM_OBJECT_UNLOCK(object);
126 VOP_UNLOCK(vp, 0, curthread);
127 VFS_UNLOCK_GIANT(vfslocked);
128 vm_object_deallocate(object);
129 vn_finished_write(mp);
130 vm_page_lock_queues();
131 return (0);
132 } else if (object->type == OBJT_SWAP ||
133 object->type == OBJT_DEFAULT) {
134 m_tmp = m;
135 vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC);
136 VM_OBJECT_UNLOCK(object);
137 return (0);
138 }
139 } else if (m->hold_count == 0)
140 vm_page_cache(m);
141 VM_OBJECT_UNLOCK(object);
142 return (0);
143 }
144
145 static int
146 vm_contig_launder(int queue)
147 {
148 vm_page_t m, next;
149 int error;
150
151 for (m = TAILQ_FIRST(&vm_page_queues[queue].pl); m != NULL; m = next) {
152 next = TAILQ_NEXT(m, pageq);
153
154 /* Skip marker pages */
155 if ((m->flags & PG_MARKER) != 0)
156 continue;
157
158 KASSERT(VM_PAGE_INQUEUE2(m, queue),
159 ("vm_contig_launder: page %p's queue is not %d", m, queue));
160 error = vm_contig_launder_page(m);
161 if (error == 0)
162 return (TRUE);
163 if (error == EBUSY)
164 return (FALSE);
165 }
166 return (FALSE);
167 }
168
169 static void
170 vm_page_release_contigl(vm_page_t m, vm_pindex_t count)
171 {
172 while (count--) {
173 vm_page_free_toq(m);
174 m++;
175 }
176 }
177
178 static void
179 vm_page_release_contig(vm_page_t m, vm_pindex_t count)
180 {
181 vm_page_lock_queues();
182 vm_page_release_contigl(m, count);
183 vm_page_unlock_queues();
184 }
185
186 static void *
187 contigmalloc2(vm_page_t m, vm_pindex_t npages, int flags)
188 {
189 vm_object_t object = kernel_object;
190 vm_map_t map = kernel_map;
191 vm_offset_t addr, tmp_addr;
192 vm_pindex_t i;
193
194 /*
195 * Allocate kernel VM, unfree and assign the physical pages to
196 * it and return kernel VM pointer.
197 */
198 vm_map_lock(map);
199 if (vm_map_findspace(map, vm_map_min(map), npages << PAGE_SHIFT, &addr)
200 != KERN_SUCCESS) {
201 vm_map_unlock(map);
202 return (NULL);
203 }
204 vm_object_reference(object);
205 vm_map_insert(map, object, addr - VM_MIN_KERNEL_ADDRESS,
206 addr, addr + (npages << PAGE_SHIFT), VM_PROT_ALL, VM_PROT_ALL, 0);
207 vm_map_unlock(map);
208 tmp_addr = addr;
209 VM_OBJECT_LOCK(object);
210 for (i = 0; i < npages; i++) {
211 vm_page_insert(&m[i], object,
212 OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
213 if ((flags & M_ZERO) && !(m[i].flags & PG_ZERO))
214 pmap_zero_page(&m[i]);
215 tmp_addr += PAGE_SIZE;
216 }
217 VM_OBJECT_UNLOCK(object);
218 vm_map_wire(map, addr, addr + (npages << PAGE_SHIFT),
219 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
220 return ((void *)addr);
221 }
222
223 void *
224 contigmalloc(
225 unsigned long size, /* should be size_t here and for malloc() */
226 struct malloc_type *type,
227 int flags,
228 vm_paddr_t low,
229 vm_paddr_t high,
230 unsigned long alignment,
231 unsigned long boundary)
232 {
233 void * ret;
234 vm_page_t pages;
235 unsigned long npgs;
236 int actl, actmax, inactl, inactmax, tries;
237
238 npgs = round_page(size) >> PAGE_SHIFT;
239 tries = 0;
240 retry:
241 pages = vm_phys_alloc_contig(npgs, low, high, alignment, boundary);
242 if (pages == NULL) {
243 if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
244 vm_page_lock_queues();
245 inactl = 0;
246 inactmax = tries < 1 ? 0 : cnt.v_inactive_count;
247 actl = 0;
248 actmax = tries < 2 ? 0 : cnt.v_active_count;
249 again:
250 if (inactl < inactmax &&
251 vm_contig_launder(PQ_INACTIVE)) {
252 inactl++;
253 goto again;
254 }
255 if (actl < actmax &&
256 vm_contig_launder(PQ_ACTIVE)) {
257 actl++;
258 goto again;
259 }
260 vm_page_unlock_queues();
261 tries++;
262 goto retry;
263 }
264 ret = NULL;
265 } else {
266 ret = contigmalloc2(pages, npgs, flags);
267 if (ret == NULL)
268 vm_page_release_contig(pages, npgs);
269 }
270 malloc_type_allocated(type, ret == NULL ? 0 : npgs << PAGE_SHIFT);
271 return (ret);
272 }
273
274 void
275 contigfree(void *addr, unsigned long size, struct malloc_type *type)
276 {
277 vm_pindex_t npgs;
278
279 npgs = round_page(size) >> PAGE_SHIFT;
280 kmem_free(kernel_map, (vm_offset_t)addr, size);
281 malloc_type_freed(type, npgs << PAGE_SHIFT);
282 }
Cache object: 3946b74f6d0b6c0a0f6d9b4feba684ee
|