1 /* $NetBSD: uvm_pager.c,v 1.96 2009/08/05 14:11:32 pooka Exp $ */
5 * Copyright (c) 1997 Charles D. Cranor and Washington University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * from: Id: uvm_pager.c,v 1.1.2.23 1998/02/02 20:38:06 chuck Exp
38 * uvm_pager.c: generic functions used to assist the pagers.
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: uvm_pager.c,v 1.96 2009/08/05 14:11:32 pooka Exp $");
44 #include "opt_uvmhist.h"
45 #include "opt_readahead.h"
46 #include "opt_pagermap.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/vnode.h>
59 * this is needed until the device strategy interface
60 * is changed to do physically-addressed i/o.
63 #ifndef PAGER_MAP_DEFAULT_SIZE
64 #define PAGER_MAP_DEFAULT_SIZE (16 * 1024 * 1024)
67 #ifndef PAGER_MAP_SIZE
68 #define PAGER_MAP_SIZE PAGER_MAP_DEFAULT_SIZE
71 size_t pager_map_size
= PAGER_MAP_SIZE
;
74 * list of uvm pagers in the system
77 const struct uvm_pagerops
* const uvmpagerops
[] = {
85 * the pager map: provides KVA for I/O
88 struct vm_map
*pager_map
; /* XXX */
89 kmutex_t pager_map_wanted_lock
;
90 bool pager_map_wanted
; /* locked by pager map */
91 static vaddr_t emergva
;
92 static bool emerginuse
;
95 * uvm_pager_init: init pagers (at boot time)
109 pager_map
= uvm_km_suballoc(kernel_map
, &sva
, &eva
, pager_map_size
, 0,
111 mutex_init(&pager_map_wanted_lock
, MUTEX_DEFAULT
, IPL_NONE
);
112 pager_map_wanted
= false;
113 emergva
= uvm_km_alloc(kernel_map
, round_page(MAXPHYS
), 0,
122 * init ASYNC I/O queue
125 TAILQ_INIT(&uvm
.aio_done
);
128 * call pager init functions
130 for (lcv
= 0 ; lcv
< __arraycount(uvmpagerops
); lcv
++) {
131 if (uvmpagerops
[lcv
]->pgo_init
)
132 uvmpagerops
[lcv
]->pgo_init();
137 * uvm_pagermapin: map pages into KVA (pager_map) for I/O that needs mappings
139 * we basically just map in a blank map entry to reserve the space in the
140 * map and then use pmap_enter() to put the mappings in by hand.
144 uvm_pagermapin(struct vm_page
**pps
, int npages
, int flags
)
151 const bool pdaemon
= curlwp
== uvm
.pagedaemon_lwp
;
152 UVMHIST_FUNC("uvm_pagermapin"); UVMHIST_CALLED(maphist
);
154 UVMHIST_LOG(maphist
,"(pps=0x%x, npages=%d)", pps
, npages
,0,0);
157 * compute protection. outgoing I/O only needs read
158 * access to the page, whereas incoming needs read/write.
162 if (flags
& UVMPAGER_MAPIN_READ
)
163 prot
|= VM_PROT_WRITE
;
166 size
= npages
<< PAGE_SHIFT
;
167 kva
= 0; /* let system choose VA */
169 if (uvm_map(pager_map
, &kva
, size
, NULL
, UVM_UNKNOWN_OFFSET
, 0,
170 UVM_FLAG_NOMERGE
| (pdaemon
? UVM_FLAG_NOWAIT
: 0)) != 0) {
172 mutex_enter(&pager_map_wanted_lock
);
174 UVM_UNLOCK_AND_WAIT(&emergva
,
175 &pager_map_wanted_lock
, false,
180 mutex_exit(&pager_map_wanted_lock
);
182 /* The shift implicitly truncates to PAGE_SIZE */
183 KASSERT(npages
<= (MAXPHYS
>> PAGE_SHIFT
));
186 if ((flags
& UVMPAGER_MAPIN_WAITOK
) == 0) {
187 UVMHIST_LOG(maphist
,"<- NOWAIT failed", 0,0,0,0);
190 mutex_enter(&pager_map_wanted_lock
);
191 pager_map_wanted
= true;
192 UVMHIST_LOG(maphist
, " SLEEPING on pager_map",0,0,0,0);
193 UVM_UNLOCK_AND_WAIT(pager_map
, &pager_map_wanted_lock
, false,
200 for (cva
= kva
; size
!= 0 ; size
-= PAGE_SIZE
, cva
+= PAGE_SIZE
) {
203 KASSERT(pp
->flags
& PG_BUSY
);
204 pmap_kenter_pa(cva
, VM_PAGE_TO_PHYS(pp
), prot
, 0);
206 pmap_update(vm_map_pmap(pager_map
));
208 UVMHIST_LOG(maphist
, "<- done (KVA=0x%x)", kva
,0,0,0);
213 * uvm_pagermapout: remove pager_map mapping
215 * we remove our mappings by hand and then remove the mapping (waking
216 * up anyone wanting space).
220 uvm_pagermapout(vaddr_t kva
, int npages
)
222 vsize_t size
= npages
<< PAGE_SHIFT
;
223 struct vm_map_entry
*entries
;
224 UVMHIST_FUNC("uvm_pagermapout"); UVMHIST_CALLED(maphist
);
226 UVMHIST_LOG(maphist
, " (kva=0x%x, npages=%d)", kva
, npages
,0,0);
229 * duplicate uvm_unmap, but add in pager_map_wanted handling.
232 pmap_kremove(kva
, npages
<< PAGE_SHIFT
);
233 if (kva
== emergva
) {
234 mutex_enter(&pager_map_wanted_lock
);
237 mutex_exit(&pager_map_wanted_lock
);
241 vm_map_lock(pager_map
);
242 uvm_unmap_remove(pager_map
, kva
, kva
+ size
, &entries
, NULL
, 0);
243 mutex_enter(&pager_map_wanted_lock
);
244 if (pager_map_wanted
) {
245 pager_map_wanted
= false;
248 mutex_exit(&pager_map_wanted_lock
);
249 vm_map_unlock(pager_map
);
251 uvm_unmap_detach(entries
, 0);
252 pmap_update(pmap_kernel());
253 UVMHIST_LOG(maphist
,"<- done",0,0,0,0);
257 * interrupt-context iodone handler for single-buf i/os
258 * or the top-level buf of a nested-buf i/o.
262 uvm_aio_biodone(struct buf
*bp
)
264 /* reset b_iodone for when this is a single-buf i/o. */
265 bp
->b_iodone
= uvm_aio_aiodone
;
267 workqueue_enqueue(uvm
.aiodone_queue
, &bp
->b_work
, NULL
);
271 uvm_aio_aiodone_pages(struct vm_page
**pgs
, int npages
, bool write
, int error
)
273 struct uvm_object
*uobj
;
280 UVMHIST_FUNC("uvm_aio_aiodone_pages"); UVMHIST_CALLED(ubchist
);
287 swap
= (pg
->uanon
!= NULL
&& pg
->uobject
== NULL
) ||
288 (pg
->pqflags
& PQ_AOBJ
) != 0;
291 slock
= &uobj
->vmobjlock
;
293 mutex_enter(&uvm_pageqlock
);
297 if (pg
->uobject
!= NULL
) {
298 swslot
= uao_find_swslot(pg
->uobject
,
299 pg
->offset
>> PAGE_SHIFT
);
301 KASSERT(pg
->uanon
!= NULL
);
302 swslot
= pg
->uanon
->an_swslot
;
306 #else /* defined(VMSWAP) */
307 panic("%s: swap", __func__
);
308 #endif /* defined(VMSWAP) */
310 for (i
= 0; i
< npages
; i
++) {
312 KASSERT(swap
|| pg
->uobject
== uobj
);
313 UVMHIST_LOG(ubchist
, "pg %p", pg
, 0,0,0);
317 * for swap i/os, lock each page's object (or anon)
318 * individually since each page may need a different lock.
322 if (pg
->uobject
!= NULL
) {
323 slock
= &pg
->uobject
->vmobjlock
;
325 slock
= &pg
->uanon
->an_lock
;
328 mutex_enter(&uvm_pageqlock
);
330 #endif /* defined(VMSWAP) */
333 * process errors. for reads, just mark the page to be freed.
334 * for writes, if the error was ENOMEM, we assume this was
335 * a transient failure so we mark the page dirty so that
336 * we'll try to write it again later. for all other write
337 * errors, we assume the error is permanent, thus the data
338 * in the page is lost. bummer.
344 pg
->flags
|= PG_RELEASED
;
346 } else if (error
== ENOMEM
) {
347 if (pg
->flags
& PG_PAGEOUT
) {
348 pg
->flags
&= ~PG_PAGEOUT
;
351 pg
->flags
&= ~PG_CLEAN
;
352 uvm_pageactivate(pg
);
359 if (pg
->uobject
!= NULL
) {
361 oldslot
= uao_set_swslot(pg
->uobject
,
362 pg
->offset
>> PAGE_SHIFT
, slot
);
363 KASSERT(oldslot
== swslot
+ i
);
365 KASSERT(pg
->uanon
->an_swslot
==
367 pg
->uanon
->an_swslot
= slot
;
370 #endif /* defined(VMSWAP) */
374 * if the page is PG_FAKE, this must have been a read to
375 * initialize the page. clear PG_FAKE and activate the page.
376 * we must also clear the pmap "modified" flag since it may
377 * still be set from the page's previous identity.
380 if (pg
->flags
& PG_FAKE
) {
382 pg
->flags
&= ~PG_FAKE
;
383 #if defined(READAHEAD_STATS)
384 pg
->pqflags
|= PQ_READAHEAD
;
385 uvm_ra_total
.ev_count
++;
386 #endif /* defined(READAHEAD_STATS) */
387 KASSERT((pg
->flags
& PG_CLEAN
) != 0);
389 pmap_clear_modify(pg
);
393 * do accounting for pagedaemon i/o and arrange to free
394 * the pages instead of just unbusying them.
397 if (pg
->flags
& PG_PAGEOUT
) {
398 pg
->flags
&= ~PG_PAGEOUT
;
401 pg
->flags
|= PG_RELEASED
;
406 * for swap pages, unlock everything for this page now.
410 if (pg
->uobject
== NULL
&& pg
->uanon
->an_ref
== 0 &&
411 (pg
->flags
& PG_RELEASED
) != 0) {
412 mutex_exit(&uvm_pageqlock
);
413 uvm_anon_release(pg
->uanon
);
415 uvm_page_unbusy(&pg
, 1);
416 mutex_exit(&uvm_pageqlock
);
420 #endif /* defined(VMSWAP) */
422 uvm_pageout_done(pageout_done
);
424 uvm_page_unbusy(pgs
, npages
);
425 mutex_exit(&uvm_pageqlock
);
431 /* these pages are now only in swap. */
432 mutex_enter(&uvm_swap_data_lock
);
433 KASSERT(uvmexp
.swpgonly
+ npages
<= uvmexp
.swpginuse
);
435 uvmexp
.swpgonly
+= npages
;
436 mutex_exit(&uvm_swap_data_lock
);
439 uvm_swap_markbad(swslot
, npages
);
441 uvm_swap_free(swslot
, npages
);
444 #endif /* defined(VMSWAP) */
449 * uvm_aio_aiodone: do iodone processing for async i/os.
450 * this should be called in thread context, not interrupt context.
454 uvm_aio_aiodone(struct buf
*bp
)
456 int npages
= bp
->b_bufsize
>> PAGE_SHIFT
;
457 struct vm_page
*pgs
[npages
];
460 UVMHIST_FUNC("uvm_aio_aiodone"); UVMHIST_CALLED(ubchist
);
461 UVMHIST_LOG(ubchist
, "bp %p", bp
, 0,0,0);
464 write
= (bp
->b_flags
& B_READ
) == 0;
466 for (i
= 0; i
< npages
; i
++) {
467 pgs
[i
] = uvm_pageratop((vaddr_t
)bp
->b_data
+ (i
<< PAGE_SHIFT
));
468 UVMHIST_LOG(ubchist
, "pgs[%d] = %p", i
, pgs
[i
],0,0);
470 uvm_pagermapout((vaddr_t
)bp
->b_data
, npages
);
472 uvm_aio_aiodone_pages(pgs
, npages
, write
, error
);
474 if (write
&& (bp
->b_cflags
& BC_AGE
) != 0) {
475 mutex_enter(bp
->b_objlock
);
477 mutex_exit(bp
->b_objlock
);
483 * uvm_pageratop: convert KVAs in the pager map back to their page
488 uvm_pageratop(vaddr_t kva
)
494 rv
= pmap_extract(pmap_kernel(), kva
, &pa
);
496 pg
= PHYS_TO_VM_PAGE(pa
);