uvm_pager.c revision 1.40 1 1.40 mrg /* $NetBSD: uvm_pager.c,v 1.40 2001/02/04 10:55:12 mrg Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg *
5 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
6 1.1 mrg * All rights reserved.
7 1.1 mrg *
8 1.1 mrg * Redistribution and use in source and binary forms, with or without
9 1.1 mrg * modification, are permitted provided that the following conditions
10 1.1 mrg * are met:
11 1.1 mrg * 1. Redistributions of source code must retain the above copyright
12 1.1 mrg * notice, this list of conditions and the following disclaimer.
13 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer in the
15 1.1 mrg * documentation and/or other materials provided with the distribution.
16 1.1 mrg * 3. All advertising materials mentioning features or use of this software
17 1.1 mrg * must display the following acknowledgement:
18 1.1 mrg * This product includes software developed by Charles D. Cranor and
19 1.1 mrg * Washington University.
20 1.1 mrg * 4. The name of the author may not be used to endorse or promote products
21 1.1 mrg * derived from this software without specific prior written permission.
22 1.1 mrg *
23 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 mrg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 mrg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 mrg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 mrg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 mrg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.3 mrg *
34 1.3 mrg * from: Id: uvm_pager.c,v 1.1.2.23 1998/02/02 20:38:06 chuck Exp
35 1.1 mrg */
36 1.1 mrg
37 1.5 mrg #include "opt_uvmhist.h"
38 1.5 mrg
39 1.1 mrg /*
40 1.1 mrg * uvm_pager.c: generic functions used to assist the pagers.
41 1.1 mrg */
42 1.1 mrg
43 1.1 mrg #include <sys/param.h>
44 1.1 mrg #include <sys/systm.h>
45 1.1 mrg #include <sys/proc.h>
46 1.1 mrg #include <sys/malloc.h>
47 1.35 chs #include <sys/pool.h>
48 1.35 chs #include <sys/vnode.h>
49 1.1 mrg
50 1.1 mrg #define UVM_PAGER
51 1.1 mrg #include <uvm/uvm.h>
52 1.1 mrg
53 1.35 chs struct pool *uvm_aiobuf_pool;
54 1.35 chs
55 1.1 mrg /*
56 1.1 mrg * list of uvm pagers in the system
57 1.1 mrg */
58 1.1 mrg
59 1.1 mrg extern struct uvm_pagerops uvm_deviceops;
60 1.1 mrg extern struct uvm_pagerops uvm_vnodeops;
61 1.35 chs extern struct uvm_pagerops ubc_pager;
62 1.1 mrg
63 1.1 mrg struct uvm_pagerops *uvmpagerops[] = {
64 1.10 thorpej &aobj_pager,
65 1.6 mrg &uvm_deviceops,
66 1.6 mrg &uvm_vnodeops,
67 1.35 chs &ubc_pager,
68 1.1 mrg };
69 1.1 mrg
70 1.1 mrg /*
71 1.1 mrg * the pager map: provides KVA for I/O
72 1.1 mrg */
73 1.1 mrg
74 1.1 mrg vm_map_t pager_map; /* XXX */
75 1.1 mrg simple_lock_data_t pager_map_wanted_lock;
76 1.1 mrg boolean_t pager_map_wanted; /* locked by pager map */
77 1.35 chs static vaddr_t emergva;
78 1.35 chs static boolean_t emerginuse;
79 1.1 mrg
80 1.1 mrg /*
81 1.1 mrg * uvm_pager_init: init pagers (at boot time)
82 1.1 mrg */
83 1.1 mrg
84 1.6 mrg void
85 1.6 mrg uvm_pager_init()
86 1.6 mrg {
87 1.6 mrg int lcv;
88 1.1 mrg
89 1.6 mrg /*
90 1.6 mrg * init pager map
91 1.6 mrg */
92 1.6 mrg
93 1.35 chs pager_map = uvm_km_suballoc(kernel_map, &uvm.pager_sva, &uvm.pager_eva,
94 1.35 chs PAGER_MAP_SIZE, 0, FALSE, NULL);
95 1.35 chs simple_lock_init(&pager_map_wanted_lock);
96 1.35 chs pager_map_wanted = FALSE;
97 1.35 chs emergva = uvm_km_valloc(kernel_map, MAXBSIZE);
98 1.35 chs emerginuse = FALSE;
99 1.6 mrg
100 1.6 mrg /*
101 1.6 mrg * init ASYNC I/O queue
102 1.6 mrg */
103 1.6 mrg
104 1.6 mrg TAILQ_INIT(&uvm.aio_done);
105 1.1 mrg
106 1.6 mrg /*
107 1.6 mrg * call pager init functions
108 1.6 mrg */
109 1.6 mrg for (lcv = 0 ; lcv < sizeof(uvmpagerops)/sizeof(struct uvm_pagerops *);
110 1.6 mrg lcv++) {
111 1.6 mrg if (uvmpagerops[lcv]->pgo_init)
112 1.6 mrg uvmpagerops[lcv]->pgo_init();
113 1.6 mrg }
114 1.1 mrg }
115 1.1 mrg
116 1.1 mrg /*
117 1.1 mrg * uvm_pagermapin: map pages into KVA (pager_map) for I/O that needs mappings
118 1.1 mrg *
119 1.1 mrg * we basically just map in a blank map entry to reserve the space in the
120 1.1 mrg * map and then use pmap_enter() to put the mappings in by hand.
121 1.1 mrg */
122 1.1 mrg
123 1.9 eeh vaddr_t
124 1.35 chs uvm_pagermapin(pps, npages, flags)
125 1.6 mrg struct vm_page **pps;
126 1.6 mrg int npages;
127 1.29 thorpej int flags;
128 1.1 mrg {
129 1.9 eeh vsize_t size;
130 1.9 eeh vaddr_t kva;
131 1.9 eeh vaddr_t cva;
132 1.6 mrg struct vm_page *pp;
133 1.29 thorpej vm_prot_t prot;
134 1.6 mrg UVMHIST_FUNC("uvm_pagermapin"); UVMHIST_CALLED(maphist);
135 1.1 mrg
136 1.35 chs UVMHIST_LOG(maphist,"(pps=0x%x, npages=%d)", pps, npages,0,0);
137 1.29 thorpej
138 1.29 thorpej /*
139 1.29 thorpej * compute protection. outgoing I/O only needs read
140 1.29 thorpej * access to the page, whereas incoming needs read/write.
141 1.29 thorpej */
142 1.29 thorpej
143 1.29 thorpej prot = VM_PROT_READ;
144 1.29 thorpej if (flags & UVMPAGER_MAPIN_READ)
145 1.29 thorpej prot |= VM_PROT_WRITE;
146 1.1 mrg
147 1.1 mrg ReStart:
148 1.12 chs size = npages << PAGE_SHIFT;
149 1.29 thorpej kva = 0; /* let system choose VA */
150 1.1 mrg
151 1.6 mrg if (uvm_map(pager_map, &kva, size, NULL,
152 1.33 thorpej UVM_UNKNOWN_OFFSET, 0, UVM_FLAG_NOMERGE) != KERN_SUCCESS) {
153 1.35 chs if (curproc == uvm.pagedaemon_proc) {
154 1.35 chs simple_lock(&pager_map_wanted_lock);
155 1.35 chs if (emerginuse) {
156 1.35 chs UVM_UNLOCK_AND_WAIT(&emergva,
157 1.35 chs &pager_map_wanted_lock, FALSE,
158 1.35 chs "emergva", 0);
159 1.35 chs goto ReStart;
160 1.35 chs }
161 1.35 chs emerginuse = TRUE;
162 1.35 chs simple_unlock(&pager_map_wanted_lock);
163 1.35 chs kva = emergva;
164 1.35 chs KASSERT(npages <= MAXBSIZE >> PAGE_SHIFT);
165 1.35 chs goto enter;
166 1.35 chs }
167 1.29 thorpej if ((flags & UVMPAGER_MAPIN_WAITOK) == 0) {
168 1.6 mrg UVMHIST_LOG(maphist,"<- NOWAIT failed", 0,0,0,0);
169 1.29 thorpej return(0);
170 1.6 mrg }
171 1.6 mrg simple_lock(&pager_map_wanted_lock);
172 1.6 mrg pager_map_wanted = TRUE;
173 1.6 mrg UVMHIST_LOG(maphist, " SLEEPING on pager_map",0,0,0,0);
174 1.6 mrg UVM_UNLOCK_AND_WAIT(pager_map, &pager_map_wanted_lock, FALSE,
175 1.35 chs "pager_map", 0);
176 1.6 mrg goto ReStart;
177 1.6 mrg }
178 1.1 mrg
179 1.35 chs enter:
180 1.6 mrg /* got it */
181 1.6 mrg for (cva = kva ; size != 0 ; size -= PAGE_SIZE, cva += PAGE_SIZE) {
182 1.6 mrg pp = *pps++;
183 1.40 mrg KASSERT(pp);
184 1.38 chs KASSERT(pp->flags & PG_BUSY);
185 1.6 mrg pmap_enter(vm_map_pmap(pager_map), cva, VM_PAGE_TO_PHYS(pp),
186 1.38 chs prot, PMAP_WIRED | ((pp->flags & PG_FAKE) ? prot :
187 1.38 chs VM_PROT_READ));
188 1.6 mrg }
189 1.1 mrg
190 1.6 mrg UVMHIST_LOG(maphist, "<- done (KVA=0x%x)", kva,0,0,0);
191 1.6 mrg return(kva);
192 1.1 mrg }
193 1.1 mrg
194 1.1 mrg /*
195 1.1 mrg * uvm_pagermapout: remove pager_map mapping
196 1.1 mrg *
197 1.1 mrg * we remove our mappings by hand and then remove the mapping (waking
198 1.1 mrg * up anyone wanting space).
199 1.1 mrg */
200 1.1 mrg
201 1.6 mrg void
202 1.6 mrg uvm_pagermapout(kva, npages)
203 1.9 eeh vaddr_t kva;
204 1.6 mrg int npages;
205 1.6 mrg {
206 1.12 chs vsize_t size = npages << PAGE_SHIFT;
207 1.6 mrg vm_map_entry_t entries;
208 1.6 mrg UVMHIST_FUNC("uvm_pagermapout"); UVMHIST_CALLED(maphist);
209 1.35 chs
210 1.6 mrg UVMHIST_LOG(maphist, " (kva=0x%x, npages=%d)", kva, npages,0,0);
211 1.1 mrg
212 1.6 mrg /*
213 1.6 mrg * duplicate uvm_unmap, but add in pager_map_wanted handling.
214 1.6 mrg */
215 1.6 mrg
216 1.35 chs if (kva == emergva) {
217 1.35 chs simple_lock(&pager_map_wanted_lock);
218 1.35 chs emerginuse = FALSE;
219 1.35 chs wakeup(&emergva);
220 1.35 chs simple_unlock(&pager_map_wanted_lock);
221 1.35 chs entries = NULL;
222 1.35 chs goto remove;
223 1.35 chs }
224 1.35 chs
225 1.6 mrg vm_map_lock(pager_map);
226 1.11 chuck (void) uvm_unmap_remove(pager_map, kva, kva + size, &entries);
227 1.6 mrg simple_lock(&pager_map_wanted_lock);
228 1.6 mrg if (pager_map_wanted) {
229 1.6 mrg pager_map_wanted = FALSE;
230 1.6 mrg wakeup(pager_map);
231 1.6 mrg }
232 1.6 mrg simple_unlock(&pager_map_wanted_lock);
233 1.6 mrg vm_map_unlock(pager_map);
234 1.35 chs remove:
235 1.35 chs pmap_remove(pmap_kernel(), kva, kva + (npages << PAGE_SHIFT));
236 1.6 mrg if (entries)
237 1.6 mrg uvm_unmap_detach(entries, 0);
238 1.1 mrg
239 1.6 mrg UVMHIST_LOG(maphist,"<- done",0,0,0,0);
240 1.1 mrg }
241 1.1 mrg
242 1.1 mrg /*
243 1.1 mrg * uvm_mk_pcluster
244 1.1 mrg *
245 1.1 mrg * generic "make 'pager put' cluster" function. a pager can either
246 1.1 mrg * [1] set pgo_mk_pcluster to NULL (never cluster), [2] set it to this
247 1.1 mrg * generic function, or [3] set it to a pager specific function.
248 1.1 mrg *
249 1.1 mrg * => caller must lock object _and_ pagequeues (since we need to look
250 1.1 mrg * at active vs. inactive bits, etc.)
251 1.1 mrg * => caller must make center page busy and write-protect it
252 1.1 mrg * => we mark all cluster pages busy for the caller
253 1.1 mrg * => the caller must unbusy all pages (and check wanted/released
254 1.1 mrg * status if it drops the object lock)
255 1.1 mrg * => flags:
256 1.1 mrg * PGO_ALLPAGES: all pages in object are valid targets
257 1.1 mrg * !PGO_ALLPAGES: use "lo" and "hi" to limit range of cluster
258 1.1 mrg * PGO_DOACTCLUST: include active pages in cluster.
259 1.1 mrg * NOTE: the caller should clear PG_CLEANCHK bits if PGO_DOACTCLUST.
260 1.1 mrg * PG_CLEANCHK is only a hint, but clearing will help reduce
261 1.1 mrg * the number of calls we make to the pmap layer.
262 1.1 mrg */
263 1.1 mrg
264 1.6 mrg struct vm_page **
265 1.6 mrg uvm_mk_pcluster(uobj, pps, npages, center, flags, mlo, mhi)
266 1.6 mrg struct uvm_object *uobj; /* IN */
267 1.6 mrg struct vm_page **pps, *center; /* IN/OUT, IN */
268 1.6 mrg int *npages, flags; /* IN/OUT, IN */
269 1.26 kleink voff_t mlo, mhi; /* IN (if !PGO_ALLPAGES) */
270 1.1 mrg {
271 1.6 mrg struct vm_page **ppsp, *pclust;
272 1.26 kleink voff_t lo, hi, curoff;
273 1.35 chs int center_idx, forward, incr;
274 1.6 mrg UVMHIST_FUNC("uvm_mk_pcluster"); UVMHIST_CALLED(maphist);
275 1.6 mrg
276 1.6 mrg /*
277 1.6 mrg * center page should already be busy and write protected. XXX:
278 1.6 mrg * suppose page is wired? if we lock, then a process could
279 1.6 mrg * fault/block on it. if we don't lock, a process could write the
280 1.6 mrg * pages in the middle of an I/O. (consider an msync()). let's
281 1.6 mrg * lock it for now (better to delay than corrupt data?).
282 1.6 mrg */
283 1.6 mrg
284 1.6 mrg /*
285 1.6 mrg * get cluster boundaries, check sanity, and apply our limits as well.
286 1.6 mrg */
287 1.6 mrg
288 1.6 mrg uobj->pgops->pgo_cluster(uobj, center->offset, &lo, &hi);
289 1.6 mrg if ((flags & PGO_ALLPAGES) == 0) {
290 1.6 mrg if (lo < mlo)
291 1.6 mrg lo = mlo;
292 1.6 mrg if (hi > mhi)
293 1.6 mrg hi = mhi;
294 1.6 mrg }
295 1.35 chs if ((hi - lo) >> PAGE_SHIFT > *npages) { /* pps too small, bail out! */
296 1.1 mrg #ifdef DIAGNOSTIC
297 1.35 chs printf("uvm_mk_pcluster uobj %p npages %d lo 0x%llx hi 0x%llx "
298 1.35 chs "flags 0x%x\n", uobj, *npages, (long long)lo,
299 1.35 chs (long long)hi, flags);
300 1.1 mrg #endif
301 1.6 mrg pps[0] = center;
302 1.6 mrg *npages = 1;
303 1.6 mrg return(pps);
304 1.6 mrg }
305 1.6 mrg
306 1.6 mrg /*
307 1.6 mrg * now determine the center and attempt to cluster around the
308 1.6 mrg * edges
309 1.6 mrg */
310 1.6 mrg
311 1.12 chs center_idx = (center->offset - lo) >> PAGE_SHIFT;
312 1.6 mrg pps[center_idx] = center; /* plug in the center page */
313 1.6 mrg ppsp = &pps[center_idx];
314 1.6 mrg *npages = 1;
315 1.35 chs
316 1.6 mrg /*
317 1.6 mrg * attempt to cluster around the left [backward], and then
318 1.6 mrg * the right side [forward].
319 1.6 mrg */
320 1.6 mrg
321 1.6 mrg for (forward = 0 ; forward <= 1 ; forward++) {
322 1.35 chs incr = forward ? PAGE_SIZE : -PAGE_SIZE;
323 1.35 chs curoff = center->offset + incr;
324 1.6 mrg for ( ;(forward == 0 && curoff >= lo) ||
325 1.12 chs (forward && curoff < hi);
326 1.35 chs curoff += incr) {
327 1.6 mrg
328 1.6 mrg pclust = uvm_pagelookup(uobj, curoff); /* lookup page */
329 1.35 chs if (pclust == NULL) {
330 1.6 mrg break; /* no page */
331 1.35 chs }
332 1.39 thorpej
333 1.39 thorpej if ((flags & PGO_DOACTCLUST) == 0) {
334 1.39 thorpej /* dont want mapped pages at all */
335 1.39 thorpej break;
336 1.39 thorpej }
337 1.39 thorpej
338 1.39 thorpej /*
339 1.39 thorpej * get an up-to-date view of the "clean" bit.
340 1.39 thorpej * note this isn't 100% accurate, but it doesn't
341 1.39 thorpej * have to be. if it's not quite right, the
342 1.39 thorpej * worst that happens is we don't cluster as
343 1.39 thorpej * aggressively. we'll sync-it-for-sure before
344 1.39 thorpej * we free the page, and clean it if necessary.
345 1.39 thorpej */
346 1.39 thorpej if ((pclust->flags & PG_CLEANCHK) == 0) {
347 1.39 thorpej if ((pclust->flags & (PG_CLEAN|PG_BUSY))
348 1.39 thorpej == PG_CLEAN &&
349 1.39 thorpej pmap_is_modified(pclust))
350 1.39 thorpej pclust->flags &= ~PG_CLEAN;
351 1.39 thorpej
352 1.39 thorpej /* now checked */
353 1.39 thorpej pclust->flags |= PG_CLEANCHK;
354 1.6 mrg }
355 1.35 chs
356 1.6 mrg /* is page available for cleaning and does it need it */
357 1.35 chs if ((pclust->flags & (PG_CLEAN|PG_BUSY)) != 0) {
358 1.6 mrg break; /* page is already clean or is busy */
359 1.35 chs }
360 1.6 mrg
361 1.6 mrg /* yes! enroll the page in our array */
362 1.6 mrg pclust->flags |= PG_BUSY; /* busy! */
363 1.6 mrg UVM_PAGE_OWN(pclust, "uvm_mk_pcluster");
364 1.35 chs
365 1.6 mrg /* XXX: protect wired page? see above comment. */
366 1.23 chs pmap_page_protect(pclust, VM_PROT_READ);
367 1.6 mrg if (!forward) {
368 1.6 mrg ppsp--; /* back up one page */
369 1.6 mrg *ppsp = pclust;
370 1.6 mrg } else {
371 1.6 mrg /* move forward one page */
372 1.6 mrg ppsp[*npages] = pclust;
373 1.6 mrg }
374 1.35 chs (*npages)++;
375 1.6 mrg }
376 1.6 mrg }
377 1.6 mrg
378 1.6 mrg /*
379 1.6 mrg * done! return the cluster array to the caller!!!
380 1.6 mrg */
381 1.1 mrg
382 1.6 mrg UVMHIST_LOG(maphist, "<- done",0,0,0,0);
383 1.6 mrg return(ppsp);
384 1.1 mrg }
385 1.1 mrg
386 1.1 mrg /*
387 1.1 mrg * uvm_pager_put: high level pageout routine
388 1.1 mrg *
389 1.1 mrg * we want to pageout page "pg" to backing store, clustering if
390 1.1 mrg * possible.
391 1.1 mrg *
392 1.1 mrg * => page queues must be locked by caller
393 1.1 mrg * => if page is not swap-backed, then "uobj" points to the object
394 1.1 mrg * backing it. this object should be locked by the caller.
395 1.1 mrg * => if page is swap-backed, then "uobj" should be NULL.
396 1.1 mrg * => "pg" should be PG_BUSY (by caller), and !PG_CLEAN
397 1.1 mrg * for swap-backed memory, "pg" can be NULL if there is no page
398 1.1 mrg * of interest [sometimes the case for the pagedaemon]
399 1.1 mrg * => "ppsp_ptr" should point to an array of npages vm_page pointers
400 1.1 mrg * for possible cluster building
401 1.1 mrg * => flags (first two for non-swap-backed pages)
402 1.1 mrg * PGO_ALLPAGES: all pages in uobj are valid targets
403 1.1 mrg * PGO_DOACTCLUST: include "PQ_ACTIVE" pages as valid targets
404 1.1 mrg * PGO_SYNCIO: do SYNC I/O (no async)
405 1.1 mrg * PGO_PDFREECLUST: pagedaemon: drop cluster on successful I/O
406 1.1 mrg * => start/stop: if (uobj && !PGO_ALLPAGES) limit targets to this range
407 1.1 mrg * if (!uobj) start is the (daddr_t) of the starting swapblk
408 1.1 mrg * => return state:
409 1.1 mrg * 1. we return the VM_PAGER status code of the pageout
410 1.1 mrg * 2. we return with the page queues unlocked
411 1.1 mrg * 3. if (uobj != NULL) [!swap_backed] we return with
412 1.1 mrg * uobj locked _only_ if PGO_PDFREECLUST is set
413 1.1 mrg * AND result != VM_PAGER_PEND. in all other cases
414 1.1 mrg * we return with uobj unlocked. [this is a hack
415 1.1 mrg * that allows the pagedaemon to save one lock/unlock
416 1.1 mrg * pair in the !swap_backed case since we have to
417 1.1 mrg * lock the uobj to drop the cluster anyway]
418 1.1 mrg * 4. on errors we always drop the cluster. thus, if we return
419 1.1 mrg * !PEND, !OK, then the caller only has to worry about
420 1.1 mrg * un-busying the main page (not the cluster pages).
421 1.1 mrg * 5. on success, if !PGO_PDFREECLUST, we return the cluster
422 1.1 mrg * with all pages busy (caller must un-busy and check
423 1.1 mrg * wanted/released flags).
424 1.1 mrg */
425 1.1 mrg
426 1.6 mrg int
427 1.6 mrg uvm_pager_put(uobj, pg, ppsp_ptr, npages, flags, start, stop)
428 1.6 mrg struct uvm_object *uobj; /* IN */
429 1.6 mrg struct vm_page *pg, ***ppsp_ptr;/* IN, IN/OUT */
430 1.6 mrg int *npages; /* IN/OUT */
431 1.6 mrg int flags; /* IN */
432 1.26 kleink voff_t start, stop; /* IN, IN */
433 1.6 mrg {
434 1.6 mrg int result;
435 1.6 mrg daddr_t swblk;
436 1.6 mrg struct vm_page **ppsp = *ppsp_ptr;
437 1.35 chs UVMHIST_FUNC("uvm_pager_put"); UVMHIST_CALLED(ubchist);
438 1.6 mrg
439 1.6 mrg /*
440 1.6 mrg * note that uobj is null if we are doing a swap-backed pageout.
441 1.6 mrg * note that uobj is !null if we are doing normal object pageout.
442 1.6 mrg * note that the page queues must be locked to cluster.
443 1.6 mrg */
444 1.6 mrg
445 1.6 mrg if (uobj) { /* if !swap-backed */
446 1.6 mrg
447 1.6 mrg /*
448 1.6 mrg * attempt to build a cluster for pageout using its
449 1.6 mrg * make-put-cluster function (if it has one).
450 1.6 mrg */
451 1.6 mrg
452 1.6 mrg if (uobj->pgops->pgo_mk_pcluster) {
453 1.6 mrg ppsp = uobj->pgops->pgo_mk_pcluster(uobj, ppsp,
454 1.6 mrg npages, pg, flags, start, stop);
455 1.6 mrg *ppsp_ptr = ppsp; /* update caller's pointer */
456 1.6 mrg } else {
457 1.6 mrg ppsp[0] = pg;
458 1.6 mrg *npages = 1;
459 1.25 chs }
460 1.25 chs
461 1.6 mrg swblk = 0; /* XXX: keep gcc happy */
462 1.1 mrg
463 1.6 mrg } else {
464 1.1 mrg
465 1.6 mrg /*
466 1.6 mrg * for swap-backed pageout, the caller (the pagedaemon) has
467 1.6 mrg * already built the cluster for us. the starting swap
468 1.6 mrg * block we are writing to has been passed in as "start."
469 1.6 mrg * "pg" could be NULL if there is no page we are especially
470 1.6 mrg * interested in (in which case the whole cluster gets dropped
471 1.6 mrg * in the event of an error or a sync "done").
472 1.6 mrg */
473 1.6 mrg swblk = (daddr_t) start;
474 1.6 mrg /* ppsp and npages should be ok */
475 1.6 mrg }
476 1.1 mrg
477 1.6 mrg /* now that we've clustered we can unlock the page queues */
478 1.6 mrg uvm_unlock_pageq();
479 1.1 mrg
480 1.6 mrg /*
481 1.6 mrg * now attempt the I/O. if we have a failure and we are
482 1.6 mrg * clustered, we will drop the cluster and try again.
483 1.6 mrg */
484 1.1 mrg
485 1.1 mrg ReTry:
486 1.6 mrg if (uobj) {
487 1.6 mrg /* object is locked */
488 1.35 chs result = uobj->pgops->pgo_put(uobj, ppsp, *npages, flags);
489 1.35 chs UVMHIST_LOG(ubchist, "put -> %d", result, 0,0,0);
490 1.6 mrg /* object is now unlocked */
491 1.6 mrg } else {
492 1.6 mrg /* nothing locked */
493 1.35 chs result = uvm_swap_put(swblk, ppsp, *npages, flags);
494 1.6 mrg /* nothing locked */
495 1.6 mrg }
496 1.6 mrg
497 1.6 mrg /*
498 1.6 mrg * we have attempted the I/O.
499 1.6 mrg *
500 1.6 mrg * if the I/O was a success then:
501 1.6 mrg * if !PGO_PDFREECLUST, we return the cluster to the
502 1.6 mrg * caller (who must un-busy all pages)
503 1.6 mrg * else we un-busy cluster pages for the pagedaemon
504 1.6 mrg *
505 1.6 mrg * if I/O is pending (async i/o) then we return the pending code.
506 1.6 mrg * [in this case the async i/o done function must clean up when
507 1.6 mrg * i/o is done...]
508 1.6 mrg */
509 1.6 mrg
510 1.6 mrg if (result == VM_PAGER_PEND || result == VM_PAGER_OK) {
511 1.6 mrg if (result == VM_PAGER_OK && (flags & PGO_PDFREECLUST)) {
512 1.6 mrg /*
513 1.6 mrg * drop cluster and relock object (only if I/O is
514 1.6 mrg * not pending)
515 1.6 mrg */
516 1.6 mrg if (uobj)
517 1.6 mrg /* required for dropcluster */
518 1.6 mrg simple_lock(&uobj->vmobjlock);
519 1.6 mrg if (*npages > 1 || pg == NULL)
520 1.6 mrg uvm_pager_dropcluster(uobj, pg, ppsp, npages,
521 1.25 chs PGO_PDFREECLUST);
522 1.6 mrg /* if (uobj): object still locked, as per
523 1.6 mrg * return-state item #3 */
524 1.6 mrg }
525 1.6 mrg return (result);
526 1.6 mrg }
527 1.6 mrg
528 1.6 mrg /*
529 1.25 chs * a pager error occured.
530 1.25 chs * for transient errors, drop to a cluster of 1 page ("pg")
531 1.25 chs * and try again. for hard errors, don't bother retrying.
532 1.6 mrg */
533 1.6 mrg
534 1.6 mrg if (*npages > 1 || pg == NULL) {
535 1.25 chs if (uobj) {
536 1.6 mrg simple_lock(&uobj->vmobjlock);
537 1.25 chs }
538 1.25 chs uvm_pager_dropcluster(uobj, pg, ppsp, npages, PGO_REALLOCSWAP);
539 1.25 chs
540 1.25 chs /*
541 1.25 chs * for failed swap-backed pageouts with a "pg",
542 1.25 chs * we need to reset pg's swslot to either:
543 1.25 chs * "swblk" (for transient errors, so we can retry),
544 1.25 chs * or 0 (for hard errors).
545 1.25 chs */
546 1.25 chs
547 1.25 chs if (uobj == NULL && pg != NULL) {
548 1.25 chs int nswblk = (result == VM_PAGER_AGAIN) ? swblk : 0;
549 1.25 chs if (pg->pqflags & PQ_ANON) {
550 1.25 chs simple_lock(&pg->uanon->an_lock);
551 1.25 chs pg->uanon->an_swslot = nswblk;
552 1.25 chs simple_unlock(&pg->uanon->an_lock);
553 1.25 chs } else {
554 1.25 chs simple_lock(&pg->uobject->vmobjlock);
555 1.25 chs uao_set_swslot(pg->uobject,
556 1.25 chs pg->offset >> PAGE_SHIFT,
557 1.25 chs nswblk);
558 1.25 chs simple_unlock(&pg->uobject->vmobjlock);
559 1.25 chs }
560 1.25 chs }
561 1.25 chs if (result == VM_PAGER_AGAIN) {
562 1.25 chs
563 1.25 chs /*
564 1.25 chs * for transient failures, free all the swslots that
565 1.25 chs * we're not going to retry with.
566 1.25 chs */
567 1.25 chs
568 1.25 chs if (uobj == NULL) {
569 1.25 chs if (pg) {
570 1.25 chs uvm_swap_free(swblk + 1, *npages - 1);
571 1.25 chs } else {
572 1.25 chs uvm_swap_free(swblk, *npages);
573 1.25 chs }
574 1.25 chs }
575 1.25 chs if (pg) {
576 1.25 chs ppsp[0] = pg;
577 1.25 chs *npages = 1;
578 1.25 chs goto ReTry;
579 1.25 chs }
580 1.25 chs } else if (uobj == NULL) {
581 1.25 chs
582 1.25 chs /*
583 1.25 chs * for hard errors on swap-backed pageouts,
584 1.25 chs * mark the swslots as bad. note that we do not
585 1.25 chs * free swslots that we mark bad.
586 1.25 chs */
587 1.25 chs
588 1.25 chs uvm_swap_markbad(swblk, *npages);
589 1.25 chs }
590 1.6 mrg }
591 1.6 mrg
592 1.6 mrg /*
593 1.6 mrg * a pager error occured (even after dropping the cluster, if there
594 1.35 chs * was one). give up! the caller only has one page ("pg")
595 1.6 mrg * to worry about.
596 1.6 mrg */
597 1.6 mrg
598 1.6 mrg if (uobj && (flags & PGO_PDFREECLUST) != 0)
599 1.6 mrg simple_lock(&uobj->vmobjlock);
600 1.6 mrg return(result);
601 1.1 mrg }
602 1.1 mrg
603 1.1 mrg /*
604 1.1 mrg * uvm_pager_dropcluster: drop a cluster we have built (because we
605 1.1 mrg * got an error, or, if PGO_PDFREECLUST we are un-busying the
606 1.1 mrg * cluster pages on behalf of the pagedaemon).
607 1.1 mrg *
608 1.1 mrg * => uobj, if non-null, is a non-swap-backed object that is
609 1.1 mrg * locked by the caller. we return with this object still
610 1.1 mrg * locked.
611 1.1 mrg * => page queues are not locked
612 1.1 mrg * => pg is our page of interest (the one we clustered around, can be null)
613 1.1 mrg * => ppsp/npages is our current cluster
614 1.1 mrg * => flags: PGO_PDFREECLUST: pageout was a success: un-busy cluster
615 1.1 mrg * pages on behalf of the pagedaemon.
616 1.1 mrg * PGO_REALLOCSWAP: drop previously allocated swap slots for
617 1.1 mrg * clustered swap-backed pages (except for "pg" if !NULL)
618 1.1 mrg * "swblk" is the start of swap alloc (e.g. for ppsp[0])
619 1.1 mrg * [only meaningful if swap-backed (uobj == NULL)]
620 1.1 mrg */
621 1.1 mrg
622 1.21 thorpej void
623 1.25 chs uvm_pager_dropcluster(uobj, pg, ppsp, npages, flags)
624 1.21 thorpej struct uvm_object *uobj; /* IN */
625 1.21 thorpej struct vm_page *pg, **ppsp; /* IN, IN/OUT */
626 1.21 thorpej int *npages; /* IN/OUT */
627 1.21 thorpej int flags;
628 1.1 mrg {
629 1.6 mrg int lcv;
630 1.6 mrg boolean_t obj_is_alive;
631 1.7 chuck struct uvm_object *saved_uobj;
632 1.1 mrg
633 1.6 mrg /*
634 1.6 mrg * drop all pages but "pg"
635 1.6 mrg */
636 1.1 mrg
637 1.6 mrg for (lcv = 0 ; lcv < *npages ; lcv++) {
638 1.1 mrg
639 1.35 chs /* skip "pg" or empty slot */
640 1.35 chs if (ppsp[lcv] == pg || ppsp[lcv] == NULL)
641 1.6 mrg continue;
642 1.1 mrg
643 1.6 mrg /*
644 1.6 mrg * if swap-backed, gain lock on object that owns page. note
645 1.6 mrg * that PQ_ANON bit can't change as long as we are holding
646 1.6 mrg * the PG_BUSY bit (so there is no need to lock the page
647 1.6 mrg * queues to test it).
648 1.6 mrg *
649 1.6 mrg * once we have the lock, dispose of the pointer to swap, if
650 1.6 mrg * requested
651 1.6 mrg */
652 1.6 mrg if (!uobj) {
653 1.6 mrg if (ppsp[lcv]->pqflags & PQ_ANON) {
654 1.6 mrg simple_lock(&ppsp[lcv]->uanon->an_lock);
655 1.6 mrg if (flags & PGO_REALLOCSWAP)
656 1.6 mrg /* zap swap block */
657 1.6 mrg ppsp[lcv]->uanon->an_swslot = 0;
658 1.6 mrg } else {
659 1.6 mrg simple_lock(&ppsp[lcv]->uobject->vmobjlock);
660 1.6 mrg if (flags & PGO_REALLOCSWAP)
661 1.6 mrg uao_set_swslot(ppsp[lcv]->uobject,
662 1.12 chs ppsp[lcv]->offset >> PAGE_SHIFT, 0);
663 1.6 mrg }
664 1.6 mrg }
665 1.6 mrg
666 1.6 mrg /* did someone want the page while we had it busy-locked? */
667 1.35 chs if (ppsp[lcv]->flags & PG_WANTED) {
668 1.6 mrg /* still holding obj lock */
669 1.22 thorpej wakeup(ppsp[lcv]);
670 1.35 chs }
671 1.6 mrg
672 1.6 mrg /* if page was released, release it. otherwise un-busy it */
673 1.6 mrg if (ppsp[lcv]->flags & PG_RELEASED) {
674 1.6 mrg
675 1.6 mrg if (ppsp[lcv]->pqflags & PQ_ANON) {
676 1.6 mrg /* so that anfree will free */
677 1.6 mrg ppsp[lcv]->flags &= ~(PG_BUSY);
678 1.6 mrg UVM_PAGE_OWN(ppsp[lcv], NULL);
679 1.6 mrg
680 1.23 chs pmap_page_protect(ppsp[lcv], VM_PROT_NONE);
681 1.13 chs simple_unlock(&ppsp[lcv]->uanon->an_lock);
682 1.6 mrg /* kills anon and frees pg */
683 1.13 chs uvm_anfree(ppsp[lcv]->uanon);
684 1.6 mrg
685 1.6 mrg continue;
686 1.6 mrg }
687 1.6 mrg
688 1.6 mrg /*
689 1.6 mrg * pgo_releasepg will dump the page for us
690 1.6 mrg */
691 1.1 mrg
692 1.1 mrg #ifdef DIAGNOSTIC
693 1.6 mrg if (ppsp[lcv]->uobject->pgops->pgo_releasepg == NULL)
694 1.6 mrg panic("uvm_pager_dropcluster: no releasepg "
695 1.6 mrg "function");
696 1.1 mrg #endif
697 1.7 chuck saved_uobj = ppsp[lcv]->uobject;
698 1.6 mrg obj_is_alive =
699 1.7 chuck saved_uobj->pgops->pgo_releasepg(ppsp[lcv], NULL);
700 1.6 mrg
701 1.1 mrg #ifdef DIAGNOSTIC
702 1.6 mrg /* for normal objects, "pg" is still PG_BUSY by us,
703 1.6 mrg * so obj can't die */
704 1.6 mrg if (uobj && !obj_is_alive)
705 1.6 mrg panic("uvm_pager_dropcluster: object died "
706 1.6 mrg "with active page");
707 1.1 mrg #endif
708 1.7 chuck /* only unlock the object if it is still alive... */
709 1.7 chuck if (obj_is_alive && saved_uobj != uobj)
710 1.7 chuck simple_unlock(&saved_uobj->vmobjlock);
711 1.7 chuck
712 1.7 chuck /*
713 1.7 chuck * XXXCDC: suppose uobj died in the pgo_releasepg?
714 1.7 chuck * how pass that
715 1.7 chuck * info up to caller. we are currently ignoring it...
716 1.7 chuck */
717 1.7 chuck
718 1.7 chuck continue; /* next page */
719 1.1 mrg
720 1.6 mrg } else {
721 1.35 chs ppsp[lcv]->flags &= ~(PG_BUSY|PG_WANTED|PG_FAKE);
722 1.6 mrg UVM_PAGE_OWN(ppsp[lcv], NULL);
723 1.6 mrg }
724 1.6 mrg
725 1.6 mrg /*
726 1.6 mrg * if we are operating on behalf of the pagedaemon and we
727 1.6 mrg * had a successful pageout update the page!
728 1.6 mrg */
729 1.6 mrg if (flags & PGO_PDFREECLUST) {
730 1.23 chs pmap_clear_reference(ppsp[lcv]);
731 1.23 chs pmap_clear_modify(ppsp[lcv]);
732 1.6 mrg ppsp[lcv]->flags |= PG_CLEAN;
733 1.6 mrg }
734 1.6 mrg
735 1.6 mrg /* if anonymous cluster, unlock object and move on */
736 1.6 mrg if (!uobj) {
737 1.6 mrg if (ppsp[lcv]->pqflags & PQ_ANON)
738 1.6 mrg simple_unlock(&ppsp[lcv]->uanon->an_lock);
739 1.6 mrg else
740 1.6 mrg simple_unlock(&ppsp[lcv]->uobject->vmobjlock);
741 1.6 mrg }
742 1.35 chs }
743 1.35 chs }
744 1.35 chs
745 1.35 chs /*
746 1.35 chs * interrupt-context iodone handler for nested i/o bufs.
747 1.35 chs *
748 1.35 chs * => must be at splbio().
749 1.35 chs */
750 1.35 chs
751 1.35 chs void
752 1.35 chs uvm_aio_biodone1(bp)
753 1.35 chs struct buf *bp;
754 1.35 chs {
755 1.35 chs struct buf *mbp = bp->b_private;
756 1.35 chs
757 1.35 chs KASSERT(mbp != bp);
758 1.35 chs if (bp->b_flags & B_ERROR) {
759 1.35 chs mbp->b_flags |= B_ERROR;
760 1.35 chs mbp->b_error = bp->b_error;
761 1.35 chs }
762 1.35 chs mbp->b_resid -= bp->b_bcount;
763 1.35 chs pool_put(&bufpool, bp);
764 1.35 chs if (mbp->b_resid == 0) {
765 1.35 chs biodone(mbp);
766 1.35 chs }
767 1.35 chs }
768 1.35 chs
769 1.35 chs /*
770 1.35 chs * interrupt-context iodone handler for single-buf i/os
771 1.35 chs * or the top-level buf of a nested-buf i/o.
772 1.35 chs *
773 1.35 chs * => must be at splbio().
774 1.35 chs */
775 1.35 chs
776 1.35 chs void
777 1.35 chs uvm_aio_biodone(bp)
778 1.35 chs struct buf *bp;
779 1.35 chs {
780 1.35 chs /* reset b_iodone for when this is a single-buf i/o. */
781 1.35 chs bp->b_iodone = uvm_aio_aiodone;
782 1.35 chs
783 1.35 chs simple_lock(&uvm.aiodoned_lock); /* locks uvm.aio_done */
784 1.35 chs TAILQ_INSERT_TAIL(&uvm.aio_done, bp, b_freelist);
785 1.35 chs wakeup(&uvm.aiodoned);
786 1.35 chs simple_unlock(&uvm.aiodoned_lock);
787 1.35 chs }
788 1.35 chs
789 1.35 chs /*
790 1.35 chs * uvm_aio_aiodone: do iodone processing for async i/os.
791 1.35 chs * this should be called in thread context, not interrupt context.
792 1.35 chs */
793 1.35 chs
794 1.35 chs void
795 1.35 chs uvm_aio_aiodone(bp)
796 1.35 chs struct buf *bp;
797 1.35 chs {
798 1.35 chs int npages = bp->b_bufsize >> PAGE_SHIFT;
799 1.35 chs struct vm_page *pg, *pgs[npages];
800 1.35 chs struct uvm_object *uobj;
801 1.35 chs int s, i;
802 1.35 chs boolean_t release, write, swap;
803 1.35 chs UVMHIST_FUNC("uvm_aio_aiodone"); UVMHIST_CALLED(ubchist);
804 1.35 chs UVMHIST_LOG(ubchist, "bp %p", bp, 0,0,0);
805 1.35 chs
806 1.35 chs release = (bp->b_flags & (B_ERROR|B_READ)) == (B_ERROR|B_READ);
807 1.35 chs write = (bp->b_flags & B_READ) == 0;
808 1.35 chs /* XXXUBC B_NOCACHE is for swap pager, should be done differently */
809 1.36 chs if (write && !(bp->b_flags & B_NOCACHE) && bioops.io_pageiodone) {
810 1.36 chs (*bioops.io_pageiodone)(bp);
811 1.35 chs }
812 1.35 chs
813 1.35 chs uobj = NULL;
814 1.35 chs for (i = 0; i < npages; i++) {
815 1.35 chs pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
816 1.35 chs UVMHIST_LOG(ubchist, "pgs[%d] = %p", i, pgs[i],0,0);
817 1.35 chs }
818 1.35 chs uvm_pagermapout((vaddr_t)bp->b_data, npages);
819 1.35 chs for (i = 0; i < npages; i++) {
820 1.35 chs pg = pgs[i];
821 1.35 chs
822 1.35 chs if (i == 0) {
823 1.35 chs swap = (pg->pqflags & PQ_SWAPBACKED) != 0;
824 1.35 chs if (!swap) {
825 1.35 chs uobj = pg->uobject;
826 1.35 chs simple_lock(&uobj->vmobjlock);
827 1.35 chs }
828 1.35 chs }
829 1.35 chs KASSERT(swap || pg->uobject == uobj);
830 1.35 chs if (swap) {
831 1.35 chs if (pg->pqflags & PQ_ANON) {
832 1.35 chs simple_lock(&pg->uanon->an_lock);
833 1.35 chs } else {
834 1.35 chs simple_lock(&pg->uobject->vmobjlock);
835 1.35 chs }
836 1.35 chs }
837 1.35 chs
838 1.35 chs /*
839 1.35 chs * if this is a read and we got an error, mark the pages
840 1.35 chs * PG_RELEASED so that uvm_page_unbusy() will free them.
841 1.35 chs */
842 1.35 chs
843 1.35 chs if (release) {
844 1.35 chs pg->flags |= PG_RELEASED;
845 1.35 chs continue;
846 1.35 chs }
847 1.35 chs KASSERT(!write || (pgs[i]->flags & PG_FAKE) == 0);
848 1.35 chs
849 1.35 chs /*
850 1.35 chs * if this is a read and the page is PG_FAKE
851 1.35 chs * or this was a write, mark the page PG_CLEAN and not PG_FAKE.
852 1.35 chs */
853 1.35 chs
854 1.35 chs if (pgs[i]->flags & PG_FAKE || write) {
855 1.35 chs pmap_clear_reference(pgs[i]);
856 1.35 chs pmap_clear_modify(pgs[i]);
857 1.35 chs pgs[i]->flags |= PG_CLEAN;
858 1.35 chs pgs[i]->flags &= ~PG_FAKE;
859 1.37 chs }
860 1.37 chs if (pg->wire_count == 0) {
861 1.37 chs uvm_pageactivate(pg);
862 1.35 chs }
863 1.35 chs if (swap) {
864 1.35 chs if (pg->pqflags & PQ_ANON) {
865 1.35 chs simple_unlock(&pg->uanon->an_lock);
866 1.35 chs } else {
867 1.35 chs simple_unlock(&pg->uobject->vmobjlock);
868 1.35 chs }
869 1.35 chs }
870 1.35 chs }
871 1.35 chs uvm_page_unbusy(pgs, npages);
872 1.35 chs if (!swap) {
873 1.35 chs simple_unlock(&uobj->vmobjlock);
874 1.35 chs }
875 1.35 chs
876 1.35 chs s = splbio();
877 1.35 chs if (write && (bp->b_flags & B_AGE) != 0) {
878 1.35 chs vwakeup(bp);
879 1.35 chs }
880 1.35 chs pool_put(&bufpool, bp);
881 1.35 chs splx(s);
882 1.35 chs }
883 1.35 chs
884 1.35 chs /*
885 1.35 chs * translate unix errno values to VM_PAGER_*.
886 1.35 chs */
887 1.35 chs
888 1.35 chs int
889 1.35 chs uvm_errno2vmerror(errno)
890 1.35 chs int errno;
891 1.35 chs {
892 1.35 chs switch (errno) {
893 1.35 chs case 0:
894 1.35 chs return VM_PAGER_OK;
895 1.35 chs case EINVAL:
896 1.35 chs return VM_PAGER_BAD;
897 1.35 chs case EINPROGRESS:
898 1.35 chs return VM_PAGER_PEND;
899 1.35 chs case EIO:
900 1.35 chs return VM_PAGER_ERROR;
901 1.35 chs case EAGAIN:
902 1.35 chs return VM_PAGER_AGAIN;
903 1.35 chs case EBUSY:
904 1.35 chs return VM_PAGER_UNLOCK;
905 1.35 chs default:
906 1.35 chs return VM_PAGER_ERROR;
907 1.6 mrg }
908 1.1 mrg }
909