xref: /netbsd-src/sys/uvm/uvm_pager.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: uvm_pager.c,v 1.98 2010/06/22 18:34:50 rmind Exp $	*/
2 
3 /*
4  *
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * All rights reserved.
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  * 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.
22  *
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.
33  *
34  * from: Id: uvm_pager.c,v 1.1.2.23 1998/02/02 20:38:06 chuck Exp
35  */
36 
37 /*
38  * uvm_pager.c: generic functions used to assist the pagers.
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: uvm_pager.c,v 1.98 2010/06/22 18:34:50 rmind Exp $");
43 
44 #include "opt_uvmhist.h"
45 #include "opt_readahead.h"
46 #include "opt_pagermap.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/malloc.h>
52 #include <sys/vnode.h>
53 #include <sys/buf.h>
54 
55 #include <uvm/uvm.h>
56 
57 /*
58  * XXX
59  * this is needed until the device strategy interface
60  * is changed to do physically-addressed i/o.
61  */
62 
63 #ifndef PAGER_MAP_DEFAULT_SIZE
64 #define PAGER_MAP_DEFAULT_SIZE	(16 * 1024 * 1024)
65 #endif
66 
67 #ifndef PAGER_MAP_SIZE
68 #define PAGER_MAP_SIZE	PAGER_MAP_DEFAULT_SIZE
69 #endif
70 
71 size_t pager_map_size = PAGER_MAP_SIZE;
72 
73 /*
74  * list of uvm pagers in the system
75  */
76 
77 const struct uvm_pagerops * const uvmpagerops[] = {
78 	&aobj_pager,
79 	&uvm_deviceops,
80 	&uvm_vnodeops,
81 	&ubc_pager,
82 };
83 
84 /*
85  * the pager map: provides KVA for I/O
86  */
87 
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;
93 
94 /*
95  * uvm_pager_init: init pagers (at boot time)
96  */
97 
98 void
99 uvm_pager_init(void)
100 {
101 	u_int lcv;
102 	vaddr_t sva, eva;
103 
104 	/*
105 	 * init pager map
106 	 */
107 
108 	sva = 0;
109 	pager_map = uvm_km_suballoc(kernel_map, &sva, &eva, pager_map_size, 0,
110 	    false, NULL);
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,
114 	    UVM_KMF_VAONLY);
115 #if defined(DEBUG)
116 	if (emergva == 0)
117 		panic("emergva");
118 #endif
119 	emerginuse = false;
120 
121 	/*
122 	 * init ASYNC I/O queue
123 	 */
124 
125 	TAILQ_INIT(&uvm.aio_done);
126 
127 	/*
128 	 * call pager init functions
129 	 */
130 	for (lcv = 0 ; lcv < __arraycount(uvmpagerops); lcv++) {
131 		if (uvmpagerops[lcv]->pgo_init)
132 			uvmpagerops[lcv]->pgo_init();
133 	}
134 }
135 
136 /*
137  * uvm_pagermapin: map pages into KVA (pager_map) for I/O that needs mappings
138  *
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.
141  */
142 
143 vaddr_t
144 uvm_pagermapin(struct vm_page **pps, int npages, int flags)
145 {
146 	vsize_t size;
147 	vaddr_t kva;
148 	vaddr_t cva;
149 	struct vm_page *pp;
150 	vm_prot_t prot;
151 	const bool pdaemon = curlwp == uvm.pagedaemon_lwp;
152 	UVMHIST_FUNC("uvm_pagermapin"); UVMHIST_CALLED(maphist);
153 
154 	UVMHIST_LOG(maphist,"(pps=0x%x, npages=%d)", pps, npages,0,0);
155 
156 	/*
157 	 * compute protection.  outgoing I/O only needs read
158 	 * access to the page, whereas incoming needs read/write.
159 	 */
160 
161 	prot = VM_PROT_READ;
162 	if (flags & UVMPAGER_MAPIN_READ)
163 		prot |= VM_PROT_WRITE;
164 
165 ReStart:
166 	size = npages << PAGE_SHIFT;
167 	kva = 0;			/* let system choose VA */
168 
169 	if (uvm_map(pager_map, &kva, size, NULL, UVM_UNKNOWN_OFFSET, 0,
170 	    UVM_FLAG_NOMERGE | (pdaemon ? UVM_FLAG_NOWAIT : 0)) != 0) {
171 		if (pdaemon) {
172 			mutex_enter(&pager_map_wanted_lock);
173 			if (emerginuse) {
174 				UVM_UNLOCK_AND_WAIT(&emergva,
175 				    &pager_map_wanted_lock, false,
176 				    "emergva", 0);
177 				goto ReStart;
178 			}
179 			emerginuse = true;
180 			mutex_exit(&pager_map_wanted_lock);
181 			kva = emergva;
182 			/* The shift implicitly truncates to PAGE_SIZE */
183 			KASSERT(npages <= (MAXPHYS >> PAGE_SHIFT));
184 			goto enter;
185 		}
186 		if ((flags & UVMPAGER_MAPIN_WAITOK) == 0) {
187 			UVMHIST_LOG(maphist,"<- NOWAIT failed", 0,0,0,0);
188 			return(0);
189 		}
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,
194 		    "pager_map", 0);
195 		goto ReStart;
196 	}
197 
198 enter:
199 	/* got it */
200 	for (cva = kva ; size != 0 ; size -= PAGE_SIZE, cva += PAGE_SIZE) {
201 		pp = *pps++;
202 		KASSERT(pp);
203 		KASSERT(pp->flags & PG_BUSY);
204 		pmap_kenter_pa(cva, VM_PAGE_TO_PHYS(pp), prot, 0);
205 	}
206 	pmap_update(vm_map_pmap(pager_map));
207 
208 	UVMHIST_LOG(maphist, "<- done (KVA=0x%x)", kva,0,0,0);
209 	return(kva);
210 }
211 
212 /*
213  * uvm_pagermapout: remove pager_map mapping
214  *
215  * we remove our mappings by hand and then remove the mapping (waking
216  * up anyone wanting space).
217  */
218 
219 void
220 uvm_pagermapout(vaddr_t kva, int npages)
221 {
222 	vsize_t size = npages << PAGE_SHIFT;
223 	struct vm_map_entry *entries;
224 	UVMHIST_FUNC("uvm_pagermapout"); UVMHIST_CALLED(maphist);
225 
226 	UVMHIST_LOG(maphist, " (kva=0x%x, npages=%d)", kva, npages,0,0);
227 
228 	/*
229 	 * duplicate uvm_unmap, but add in pager_map_wanted handling.
230 	 */
231 
232 	pmap_kremove(kva, npages << PAGE_SHIFT);
233 	pmap_update(pmap_kernel());
234 
235 	if (kva == emergva) {
236 		mutex_enter(&pager_map_wanted_lock);
237 		emerginuse = false;
238 		wakeup(&emergva);
239 		mutex_exit(&pager_map_wanted_lock);
240 		return;
241 	}
242 
243 	vm_map_lock(pager_map);
244 	uvm_unmap_remove(pager_map, kva, kva + size, &entries, NULL, 0);
245 	mutex_enter(&pager_map_wanted_lock);
246 	if (pager_map_wanted) {
247 		pager_map_wanted = false;
248 		wakeup(pager_map);
249 	}
250 	mutex_exit(&pager_map_wanted_lock);
251 	vm_map_unlock(pager_map);
252 	if (entries)
253 		uvm_unmap_detach(entries, 0);
254 	UVMHIST_LOG(maphist,"<- done",0,0,0,0);
255 }
256 
257 /*
258  * interrupt-context iodone handler for single-buf i/os
259  * or the top-level buf of a nested-buf i/o.
260  */
261 
262 void
263 uvm_aio_biodone(struct buf *bp)
264 {
265 	/* reset b_iodone for when this is a single-buf i/o. */
266 	bp->b_iodone = uvm_aio_aiodone;
267 
268 	workqueue_enqueue(uvm.aiodone_queue, &bp->b_work, NULL);
269 }
270 
271 void
272 uvm_aio_aiodone_pages(struct vm_page **pgs, int npages, bool write, int error)
273 {
274 	struct uvm_object *uobj;
275 	struct vm_page *pg;
276 	kmutex_t *slock;
277 	int pageout_done;
278 	int swslot;
279 	int i;
280 	bool swap;
281 	UVMHIST_FUNC("uvm_aio_aiodone_pages"); UVMHIST_CALLED(ubchist);
282 
283 	swslot = 0;
284 	pageout_done = 0;
285 	slock = NULL;
286 	uobj = NULL;
287 	pg = pgs[0];
288 	swap = (pg->uanon != NULL && pg->uobject == NULL) ||
289 		(pg->pqflags & PQ_AOBJ) != 0;
290 	if (!swap) {
291 		uobj = pg->uobject;
292 		slock = &uobj->vmobjlock;
293 		mutex_enter(slock);
294 		mutex_enter(&uvm_pageqlock);
295 	} else {
296 #if defined(VMSWAP)
297 		if (error) {
298 			if (pg->uobject != NULL) {
299 				swslot = uao_find_swslot(pg->uobject,
300 				    pg->offset >> PAGE_SHIFT);
301 			} else {
302 				KASSERT(pg->uanon != NULL);
303 				swslot = pg->uanon->an_swslot;
304 			}
305 			KASSERT(swslot);
306 		}
307 #else /* defined(VMSWAP) */
308 		panic("%s: swap", __func__);
309 #endif /* defined(VMSWAP) */
310 	}
311 	for (i = 0; i < npages; i++) {
312 		pg = pgs[i];
313 		KASSERT(swap || pg->uobject == uobj);
314 		UVMHIST_LOG(ubchist, "pg %p", pg, 0,0,0);
315 
316 #if defined(VMSWAP)
317 		/*
318 		 * for swap i/os, lock each page's object (or anon)
319 		 * individually since each page may need a different lock.
320 		 */
321 
322 		if (swap) {
323 			if (pg->uobject != NULL) {
324 				slock = &pg->uobject->vmobjlock;
325 			} else {
326 				slock = &pg->uanon->an_lock;
327 			}
328 			mutex_enter(slock);
329 			mutex_enter(&uvm_pageqlock);
330 		}
331 #endif /* defined(VMSWAP) */
332 
333 		/*
334 		 * process errors.  for reads, just mark the page to be freed.
335 		 * for writes, if the error was ENOMEM, we assume this was
336 		 * a transient failure so we mark the page dirty so that
337 		 * we'll try to write it again later.  for all other write
338 		 * errors, we assume the error is permanent, thus the data
339 		 * in the page is lost.  bummer.
340 		 */
341 
342 		if (error) {
343 			int slot;
344 			if (!write) {
345 				pg->flags |= PG_RELEASED;
346 				continue;
347 			} else if (error == ENOMEM) {
348 				if (pg->flags & PG_PAGEOUT) {
349 					pg->flags &= ~PG_PAGEOUT;
350 					pageout_done++;
351 				}
352 				pg->flags &= ~PG_CLEAN;
353 				uvm_pageactivate(pg);
354 				slot = 0;
355 			} else
356 				slot = SWSLOT_BAD;
357 
358 #if defined(VMSWAP)
359 			if (swap) {
360 				if (pg->uobject != NULL) {
361 					int oldslot;
362 					oldslot = uao_set_swslot(pg->uobject,
363 						pg->offset >> PAGE_SHIFT, slot);
364 					KASSERT(oldslot == swslot + i);
365 				} else {
366 					KASSERT(pg->uanon->an_swslot ==
367 						swslot + i);
368 					pg->uanon->an_swslot = slot;
369 				}
370 			}
371 #endif /* defined(VMSWAP) */
372 		}
373 
374 		/*
375 		 * if the page is PG_FAKE, this must have been a read to
376 		 * initialize the page.  clear PG_FAKE and activate the page.
377 		 * we must also clear the pmap "modified" flag since it may
378 		 * still be set from the page's previous identity.
379 		 */
380 
381 		if (pg->flags & PG_FAKE) {
382 			KASSERT(!write);
383 			pg->flags &= ~PG_FAKE;
384 #if defined(READAHEAD_STATS)
385 			pg->pqflags |= PQ_READAHEAD;
386 			uvm_ra_total.ev_count++;
387 #endif /* defined(READAHEAD_STATS) */
388 			KASSERT((pg->flags & PG_CLEAN) != 0);
389 			uvm_pageenqueue(pg);
390 			pmap_clear_modify(pg);
391 		}
392 
393 		/*
394 		 * do accounting for pagedaemon i/o and arrange to free
395 		 * the pages instead of just unbusying them.
396 		 */
397 
398 		if (pg->flags & PG_PAGEOUT) {
399 			pg->flags &= ~PG_PAGEOUT;
400 			pageout_done++;
401 			uvmexp.pdfreed++;
402 			pg->flags |= PG_RELEASED;
403 		}
404 
405 #if defined(VMSWAP)
406 		/*
407 		 * for swap pages, unlock everything for this page now.
408 		 */
409 
410 		if (swap) {
411 			if (pg->uobject == NULL && pg->uanon->an_ref == 0 &&
412 			    (pg->flags & PG_RELEASED) != 0) {
413 				mutex_exit(&uvm_pageqlock);
414 				uvm_anon_release(pg->uanon);
415 			} else {
416 				uvm_page_unbusy(&pg, 1);
417 				mutex_exit(&uvm_pageqlock);
418 				mutex_exit(slock);
419 			}
420 		}
421 #endif /* defined(VMSWAP) */
422 	}
423 	uvm_pageout_done(pageout_done);
424 	if (!swap) {
425 		uvm_page_unbusy(pgs, npages);
426 		mutex_exit(&uvm_pageqlock);
427 		mutex_exit(slock);
428 	} else {
429 #if defined(VMSWAP)
430 		KASSERT(write);
431 
432 		/* these pages are now only in swap. */
433 		mutex_enter(&uvm_swap_data_lock);
434 		KASSERT(uvmexp.swpgonly + npages <= uvmexp.swpginuse);
435 		if (error != ENOMEM)
436 			uvmexp.swpgonly += npages;
437 		mutex_exit(&uvm_swap_data_lock);
438 		if (error) {
439 			if (error != ENOMEM)
440 				uvm_swap_markbad(swslot, npages);
441 			else
442 				uvm_swap_free(swslot, npages);
443 		}
444 		uvmexp.pdpending--;
445 #endif /* defined(VMSWAP) */
446 	}
447 }
448 
449 /*
450  * uvm_aio_aiodone: do iodone processing for async i/os.
451  * this should be called in thread context, not interrupt context.
452  */
453 
454 void
455 uvm_aio_aiodone(struct buf *bp)
456 {
457 	int npages = bp->b_bufsize >> PAGE_SHIFT;
458 	struct vm_page *pgs[npages];
459 	int i, error;
460 	bool write;
461 	UVMHIST_FUNC("uvm_aio_aiodone"); UVMHIST_CALLED(ubchist);
462 	UVMHIST_LOG(ubchist, "bp %p", bp, 0,0,0);
463 
464 	error = bp->b_error;
465 	write = (bp->b_flags & B_READ) == 0;
466 
467 	for (i = 0; i < npages; i++) {
468 		pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
469 		UVMHIST_LOG(ubchist, "pgs[%d] = %p", i, pgs[i],0,0);
470 	}
471 	uvm_pagermapout((vaddr_t)bp->b_data, npages);
472 
473 	uvm_aio_aiodone_pages(pgs, npages, write, error);
474 
475 	if (write && (bp->b_cflags & BC_AGE) != 0) {
476 		mutex_enter(bp->b_objlock);
477 		vwakeup(bp);
478 		mutex_exit(bp->b_objlock);
479 	}
480 	putiobuf(bp);
481 }
482 
483 /*
484  * uvm_pageratop: convert KVAs in the pager map back to their page
485  * structures.
486  */
487 
488 struct vm_page *
489 uvm_pageratop(vaddr_t kva)
490 {
491 	struct vm_page *pg;
492 	paddr_t pa;
493 	bool rv;
494 
495 	rv = pmap_extract(pmap_kernel(), kva, &pa);
496 	KASSERT(rv);
497 	pg = PHYS_TO_VM_PAGE(pa);
498 	KASSERT(pg != NULL);
499 	return (pg);
500 }
501