xref: /freebsd/sys/vm/vm_pager.c (revision 7f9dff23d3092aa33ad45b2b63e52469b3c13a6e)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  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_pager.c	8.6 (Berkeley) 1/12/94
33  *
34  *
35  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36  * All rights reserved.
37  *
38  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39  *
40  * Permission to use, copy, modify and distribute this software and
41  * its documentation is hereby granted, provided that both the copyright
42  * notice and this permission notice appear in all copies of the
43  * software, derivative works or modified versions, and any portions
44  * thereof, and that both notices appear in supporting documentation.
45  *
46  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49  *
50  * Carnegie Mellon requests users of this software to return to
51  *
52  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53  *  School of Computer Science
54  *  Carnegie Mellon University
55  *  Pittsburgh PA 15213-3890
56  *
57  * any improvements or extensions that they make and grant Carnegie the
58  * rights to redistribute these changes.
59  */
60 
61 /*
62  *	Paging space routine stubs.  Emulates a matchmaker-like interface
63  *	for builtin pagers.
64  */
65 
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/vnode.h>
73 #include <sys/bio.h>
74 #include <sys/buf.h>
75 #include <sys/ucred.h>
76 #include <sys/malloc.h>
77 #include <sys/rwlock.h>
78 
79 #include <vm/vm.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_kern.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pager.h>
85 #include <vm/vm_extern.h>
86 
87 extern vm_page_t bogus_page;
88 
89 int cluster_pbuf_freecnt = -1;	/* unlimited to begin with */
90 
91 struct buf *swbuf;
92 
93 static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
94 static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
95     vm_ooffset_t, struct ucred *);
96 static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
97 static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
98 static void dead_pager_dealloc(vm_object_t);
99 
100 static int
101 dead_pager_getpages(vm_object_t obj, vm_page_t *ma, int count, int *rbehind,
102     int *rahead)
103 {
104 
105 	return (VM_PAGER_FAIL);
106 }
107 
108 static vm_object_t
109 dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
110     vm_ooffset_t off, struct ucred *cred)
111 {
112 	return NULL;
113 }
114 
115 static void
116 dead_pager_putpages(object, m, count, flags, rtvals)
117 	vm_object_t object;
118 	vm_page_t *m;
119 	int count;
120 	int flags;
121 	int *rtvals;
122 {
123 	int i;
124 
125 	for (i = 0; i < count; i++) {
126 		rtvals[i] = VM_PAGER_AGAIN;
127 	}
128 }
129 
130 static int
131 dead_pager_haspage(object, pindex, prev, next)
132 	vm_object_t object;
133 	vm_pindex_t pindex;
134 	int *prev;
135 	int *next;
136 {
137 	if (prev)
138 		*prev = 0;
139 	if (next)
140 		*next = 0;
141 	return FALSE;
142 }
143 
144 static void
145 dead_pager_dealloc(object)
146 	vm_object_t object;
147 {
148 	return;
149 }
150 
151 static struct pagerops deadpagerops = {
152 	.pgo_alloc = 	dead_pager_alloc,
153 	.pgo_dealloc =	dead_pager_dealloc,
154 	.pgo_getpages =	dead_pager_getpages,
155 	.pgo_putpages =	dead_pager_putpages,
156 	.pgo_haspage =	dead_pager_haspage,
157 };
158 
159 struct pagerops *pagertab[] = {
160 	&defaultpagerops,	/* OBJT_DEFAULT */
161 	&swappagerops,		/* OBJT_SWAP */
162 	&vnodepagerops,		/* OBJT_VNODE */
163 	&devicepagerops,	/* OBJT_DEVICE */
164 	&physpagerops,		/* OBJT_PHYS */
165 	&deadpagerops,		/* OBJT_DEAD */
166 	&sgpagerops,		/* OBJT_SG */
167 	&mgtdevicepagerops,	/* OBJT_MGTDEVICE */
168 };
169 
170 /*
171  * Kernel address space for mapping pages.
172  * Used by pagers where KVAs are needed for IO.
173  *
174  * XXX needs to be large enough to support the number of pending async
175  * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
176  * (MAXPHYS == 64k) if you want to get the most efficiency.
177  */
178 struct mtx_padalign pbuf_mtx;
179 static TAILQ_HEAD(swqueue, buf) bswlist;
180 static int bswneeded;
181 vm_offset_t swapbkva;		/* swap buffers kva */
182 
183 void
184 vm_pager_init()
185 {
186 	struct pagerops **pgops;
187 
188 	TAILQ_INIT(&bswlist);
189 	/*
190 	 * Initialize known pagers
191 	 */
192 	for (pgops = pagertab; pgops < &pagertab[nitems(pagertab)]; pgops++)
193 		if ((*pgops)->pgo_init != NULL)
194 			(*(*pgops)->pgo_init) ();
195 }
196 
197 void
198 vm_pager_bufferinit()
199 {
200 	struct buf *bp;
201 	int i;
202 
203 	mtx_init(&pbuf_mtx, "pbuf mutex", NULL, MTX_DEF);
204 	bp = swbuf;
205 	/*
206 	 * Now set up swap and physical I/O buffer headers.
207 	 */
208 	for (i = 0; i < nswbuf; i++, bp++) {
209 		TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
210 		BUF_LOCKINIT(bp);
211 		LIST_INIT(&bp->b_dep);
212 		bp->b_rcred = bp->b_wcred = NOCRED;
213 		bp->b_xflags = 0;
214 	}
215 
216 	cluster_pbuf_freecnt = nswbuf / 2;
217 	vnode_pbuf_freecnt = nswbuf / 2 + 1;
218 	vnode_async_pbuf_freecnt = nswbuf / 2;
219 }
220 
221 /*
222  * Allocate an instance of a pager of the given type.
223  * Size, protection and offset parameters are passed in for pagers that
224  * need to perform page-level validation (e.g. the device pager).
225  */
226 vm_object_t
227 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
228     vm_prot_t prot, vm_ooffset_t off, struct ucred *cred)
229 {
230 	vm_object_t ret;
231 	struct pagerops *ops;
232 
233 	ops = pagertab[type];
234 	if (ops)
235 		ret = (*ops->pgo_alloc) (handle, size, prot, off, cred);
236 	else
237 		ret = NULL;
238 	return (ret);
239 }
240 
241 /*
242  *	The object must be locked.
243  */
244 void
245 vm_pager_deallocate(object)
246 	vm_object_t object;
247 {
248 
249 	VM_OBJECT_ASSERT_WLOCKED(object);
250 	(*pagertab[object->type]->pgo_dealloc) (object);
251 }
252 
253 static void
254 vm_pager_assert_in(vm_object_t object, vm_page_t *m, int count)
255 {
256 #ifdef INVARIANTS
257 
258 	VM_OBJECT_ASSERT_WLOCKED(object);
259 	KASSERT(count > 0, ("%s: 0 count", __func__));
260 	/*
261 	 * All pages must be busied, not mapped, not fully valid,
262 	 * not dirty and belong to the proper object.
263 	 */
264 	for (int i = 0 ; i < count; i++) {
265 		if (m[i] == bogus_page)
266 			continue;
267 		vm_page_assert_xbusied(m[i]);
268 		KASSERT(!pmap_page_is_mapped(m[i]),
269 		    ("%s: page %p is mapped", __func__, m[i]));
270 		KASSERT(m[i]->valid != VM_PAGE_BITS_ALL,
271 		    ("%s: request for a valid page %p", __func__, m[i]));
272 		KASSERT(m[i]->dirty == 0,
273 		    ("%s: page %p is dirty", __func__, m[i]));
274 		KASSERT(m[i]->object == object,
275 		    ("%s: wrong object %p/%p", __func__, object, m[i]->object));
276 	}
277 #endif
278 }
279 
280 /*
281  * Page in the pages for the object using its associated pager.
282  * The requested page must be fully valid on successful return.
283  */
284 int
285 vm_pager_get_pages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
286     int *rahead)
287 {
288 #ifdef INVARIANTS
289 	vm_pindex_t pindex = m[0]->pindex;
290 #endif
291 	int r;
292 
293 	vm_pager_assert_in(object, m, count);
294 
295 	r = (*pagertab[object->type]->pgo_getpages)(object, m, count, rbehind,
296 	    rahead);
297 	if (r != VM_PAGER_OK)
298 		return (r);
299 
300 	for (int i = 0; i < count; i++) {
301 		/*
302 		 * If pager has replaced a page, assert that it had
303 		 * updated the array.
304 		 */
305 		KASSERT(m[i] == vm_page_lookup(object, pindex++),
306 		    ("%s: mismatch page %p pindex %ju", __func__,
307 		    m[i], (uintmax_t )pindex - 1));
308 		/*
309 		 * Zero out partially filled data.
310 		 */
311 		if (m[i]->valid != VM_PAGE_BITS_ALL)
312 			vm_page_zero_invalid(m[i], TRUE);
313 	}
314 	return (VM_PAGER_OK);
315 }
316 
317 int
318 vm_pager_get_pages_async(vm_object_t object, vm_page_t *m, int count,
319     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
320 {
321 
322 	vm_pager_assert_in(object, m, count);
323 
324 	return ((*pagertab[object->type]->pgo_getpages_async)(object, m,
325 	    count, rbehind, rahead, iodone, arg));
326 }
327 
328 /*
329  * vm_pager_put_pages() - inline, see vm/vm_pager.h
330  * vm_pager_has_page() - inline, see vm/vm_pager.h
331  */
332 
333 /*
334  * Search the specified pager object list for an object with the
335  * specified handle.  If an object with the specified handle is found,
336  * increase its reference count and return it.  Otherwise, return NULL.
337  *
338  * The pager object list must be locked.
339  */
340 vm_object_t
341 vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
342 {
343 	vm_object_t object;
344 
345 	TAILQ_FOREACH(object, pg_list, pager_object_list) {
346 		if (object->handle == handle) {
347 			VM_OBJECT_WLOCK(object);
348 			if ((object->flags & OBJ_DEAD) == 0) {
349 				vm_object_reference_locked(object);
350 				VM_OBJECT_WUNLOCK(object);
351 				break;
352 			}
353 			VM_OBJECT_WUNLOCK(object);
354 		}
355 	}
356 	return (object);
357 }
358 
359 /*
360  * initialize a physical buffer
361  */
362 
363 /*
364  * XXX This probably belongs in vfs_bio.c
365  */
366 static void
367 initpbuf(struct buf *bp)
368 {
369 	KASSERT(bp->b_bufobj == NULL, ("initpbuf with bufobj"));
370 	KASSERT(bp->b_vp == NULL, ("initpbuf with vp"));
371 	bp->b_rcred = NOCRED;
372 	bp->b_wcred = NOCRED;
373 	bp->b_qindex = 0;	/* On no queue (QUEUE_NONE) */
374 	bp->b_kvabase = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva;
375 	bp->b_data = bp->b_kvabase;
376 	bp->b_kvasize = MAXPHYS;
377 	bp->b_flags = 0;
378 	bp->b_xflags = 0;
379 	bp->b_ioflags = 0;
380 	bp->b_iodone = NULL;
381 	bp->b_error = 0;
382 	BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
383 	buf_track(bp, __func__);
384 }
385 
386 /*
387  * allocate a physical buffer
388  *
389  *	There are a limited number (nswbuf) of physical buffers.  We need
390  *	to make sure that no single subsystem is able to hog all of them,
391  *	so each subsystem implements a counter which is typically initialized
392  *	to 1/2 nswbuf.  getpbuf() decrements this counter in allocation and
393  *	increments it on release, and blocks if the counter hits zero.  A
394  *	subsystem may initialize the counter to -1 to disable the feature,
395  *	but it must still be sure to match up all uses of getpbuf() with
396  *	relpbuf() using the same variable.
397  *
398  *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
399  *	relatively soon when the rest of the subsystems get smart about it. XXX
400  */
401 struct buf *
402 getpbuf(int *pfreecnt)
403 {
404 	struct buf *bp;
405 
406 	mtx_lock(&pbuf_mtx);
407 
408 	for (;;) {
409 		if (pfreecnt) {
410 			while (*pfreecnt == 0) {
411 				msleep(pfreecnt, &pbuf_mtx, PVM, "wswbuf0", 0);
412 			}
413 		}
414 
415 		/* get a bp from the swap buffer header pool */
416 		if ((bp = TAILQ_FIRST(&bswlist)) != NULL)
417 			break;
418 
419 		bswneeded = 1;
420 		msleep(&bswneeded, &pbuf_mtx, PVM, "wswbuf1", 0);
421 		/* loop in case someone else grabbed one */
422 	}
423 	TAILQ_REMOVE(&bswlist, bp, b_freelist);
424 	if (pfreecnt)
425 		--*pfreecnt;
426 	mtx_unlock(&pbuf_mtx);
427 
428 	initpbuf(bp);
429 	return bp;
430 }
431 
432 /*
433  * allocate a physical buffer, if one is available.
434  *
435  *	Note that there is no NULL hack here - all subsystems using this
436  *	call understand how to use pfreecnt.
437  */
438 struct buf *
439 trypbuf(int *pfreecnt)
440 {
441 	struct buf *bp;
442 
443 	mtx_lock(&pbuf_mtx);
444 	if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) {
445 		mtx_unlock(&pbuf_mtx);
446 		return NULL;
447 	}
448 	TAILQ_REMOVE(&bswlist, bp, b_freelist);
449 
450 	--*pfreecnt;
451 
452 	mtx_unlock(&pbuf_mtx);
453 
454 	initpbuf(bp);
455 
456 	return bp;
457 }
458 
459 /*
460  * release a physical buffer
461  *
462  *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
463  *	relatively soon when the rest of the subsystems get smart about it. XXX
464  */
465 void
466 relpbuf(struct buf *bp, int *pfreecnt)
467 {
468 
469 	if (bp->b_rcred != NOCRED) {
470 		crfree(bp->b_rcred);
471 		bp->b_rcred = NOCRED;
472 	}
473 	if (bp->b_wcred != NOCRED) {
474 		crfree(bp->b_wcred);
475 		bp->b_wcred = NOCRED;
476 	}
477 
478 	KASSERT(bp->b_vp == NULL, ("relpbuf with vp"));
479 	KASSERT(bp->b_bufobj == NULL, ("relpbuf with bufobj"));
480 
481 	buf_track(bp, __func__);
482 	BUF_UNLOCK(bp);
483 
484 	mtx_lock(&pbuf_mtx);
485 	TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
486 
487 	if (bswneeded) {
488 		bswneeded = 0;
489 		wakeup(&bswneeded);
490 	}
491 	if (pfreecnt) {
492 		if (++*pfreecnt == 1)
493 			wakeup(pfreecnt);
494 	}
495 	mtx_unlock(&pbuf_mtx);
496 }
497 
498 /*
499  * Associate a p-buffer with a vnode.
500  *
501  * Also sets B_PAGING flag to indicate that vnode is not fully associated
502  * with the buffer.  i.e. the bp has not been linked into the vnode or
503  * ref-counted.
504  */
505 void
506 pbgetvp(struct vnode *vp, struct buf *bp)
507 {
508 
509 	KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
510 	KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
511 
512 	bp->b_vp = vp;
513 	bp->b_flags |= B_PAGING;
514 	bp->b_bufobj = &vp->v_bufobj;
515 }
516 
517 /*
518  * Associate a p-buffer with a vnode.
519  *
520  * Also sets B_PAGING flag to indicate that vnode is not fully associated
521  * with the buffer.  i.e. the bp has not been linked into the vnode or
522  * ref-counted.
523  */
524 void
525 pbgetbo(struct bufobj *bo, struct buf *bp)
526 {
527 
528 	KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
529 	KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
530 
531 	bp->b_flags |= B_PAGING;
532 	bp->b_bufobj = bo;
533 }
534 
535 /*
536  * Disassociate a p-buffer from a vnode.
537  */
538 void
539 pbrelvp(struct buf *bp)
540 {
541 
542 	KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
543 	KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
544 	KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
545 	    ("pbrelvp: pager buf on vnode list."));
546 
547 	bp->b_vp = NULL;
548 	bp->b_bufobj = NULL;
549 	bp->b_flags &= ~B_PAGING;
550 }
551 
552 /*
553  * Disassociate a p-buffer from a bufobj.
554  */
555 void
556 pbrelbo(struct buf *bp)
557 {
558 
559 	KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
560 	KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
561 	KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
562 	    ("pbrelbo: pager buf on vnode list."));
563 
564 	bp->b_bufobj = NULL;
565 	bp->b_flags &= ~B_PAGING;
566 }
567