xref: /freebsd/sys/vm/vm_pager.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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 
78 #include <vm/vm.h>
79 #include <vm/vm_param.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_pager.h>
83 #include <vm/vm_extern.h>
84 
85 MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "XXX: VM pager private data");
86 
87 int cluster_pbuf_freecnt = -1;	/* unlimited to begin with */
88 
89 static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int);
90 static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
91 	vm_ooffset_t);
92 static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
93 static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
94 static void dead_pager_dealloc(vm_object_t);
95 
96 static int
97 dead_pager_getpages(obj, ma, count, req)
98 	vm_object_t obj;
99 	vm_page_t *ma;
100 	int count;
101 	int req;
102 {
103 	return VM_PAGER_FAIL;
104 }
105 
106 static vm_object_t
107 dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
108     vm_ooffset_t off)
109 {
110 	return NULL;
111 }
112 
113 static void
114 dead_pager_putpages(object, m, count, flags, rtvals)
115 	vm_object_t object;
116 	vm_page_t *m;
117 	int count;
118 	int flags;
119 	int *rtvals;
120 {
121 	int i;
122 
123 	for (i = 0; i < count; i++) {
124 		rtvals[i] = VM_PAGER_AGAIN;
125 	}
126 }
127 
128 static int
129 dead_pager_haspage(object, pindex, prev, next)
130 	vm_object_t object;
131 	vm_pindex_t pindex;
132 	int *prev;
133 	int *next;
134 {
135 	if (prev)
136 		*prev = 0;
137 	if (next)
138 		*next = 0;
139 	return FALSE;
140 }
141 
142 static void
143 dead_pager_dealloc(object)
144 	vm_object_t object;
145 {
146 	return;
147 }
148 
149 static struct pagerops deadpagerops = {
150 	.pgo_alloc = 	dead_pager_alloc,
151 	.pgo_dealloc =	dead_pager_dealloc,
152 	.pgo_getpages =	dead_pager_getpages,
153 	.pgo_putpages =	dead_pager_putpages,
154 	.pgo_haspage =	dead_pager_haspage,
155 };
156 
157 struct pagerops *pagertab[] = {
158 	&defaultpagerops,	/* OBJT_DEFAULT */
159 	&swappagerops,		/* OBJT_SWAP */
160 	&vnodepagerops,		/* OBJT_VNODE */
161 	&devicepagerops,	/* OBJT_DEVICE */
162 	&physpagerops,		/* OBJT_PHYS */
163 	&deadpagerops		/* OBJT_DEAD */
164 };
165 
166 static const int npagers = sizeof(pagertab) / sizeof(pagertab[0]);
167 
168 /*
169  * Kernel address space for mapping pages.
170  * Used by pagers where KVAs are needed for IO.
171  *
172  * XXX needs to be large enough to support the number of pending async
173  * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
174  * (MAXPHYS == 64k) if you want to get the most efficiency.
175  */
176 vm_map_t pager_map;
177 static int bswneeded;
178 static vm_offset_t swapbkva;		/* swap buffers kva */
179 struct mtx pbuf_mtx;
180 static TAILQ_HEAD(swqueue, buf) bswlist;
181 
182 void
183 vm_pager_init()
184 {
185 	struct pagerops **pgops;
186 
187 	TAILQ_INIT(&bswlist);
188 	/*
189 	 * Initialize known pagers
190 	 */
191 	for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
192 		if (pgops && ((*pgops)->pgo_init != NULL))
193 			(*(*pgops)->pgo_init) ();
194 }
195 
196 void
197 vm_pager_bufferinit()
198 {
199 	struct buf *bp;
200 	int i;
201 
202 	mtx_init(&pbuf_mtx, "pbuf mutex", NULL, MTX_DEF);
203 	bp = swbuf;
204 	/*
205 	 * Now set up swap and physical I/O buffer headers.
206 	 */
207 	for (i = 0; i < nswbuf; i++, bp++) {
208 		TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
209 		BUF_LOCKINIT(bp);
210 		LIST_INIT(&bp->b_dep);
211 		bp->b_rcred = bp->b_wcred = NOCRED;
212 		bp->b_xflags = 0;
213 	}
214 
215 	cluster_pbuf_freecnt = nswbuf / 2;
216 	vnode_pbuf_freecnt = nswbuf / 2 + 1;
217 
218 	swapbkva = kmem_alloc_nofault(pager_map, nswbuf * MAXPHYS);
219 	if (!swapbkva)
220 		panic("Not enough pager_map VM space for physical buffers");
221 }
222 
223 /*
224  * Allocate an instance of a pager of the given type.
225  * Size, protection and offset parameters are passed in for pagers that
226  * need to perform page-level validation (e.g. the device pager).
227  */
228 vm_object_t
229 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
230 		  vm_prot_t prot, vm_ooffset_t off)
231 {
232 	vm_object_t ret;
233 	struct pagerops *ops;
234 
235 	ops = pagertab[type];
236 	if (ops)
237 		ret = (*ops->pgo_alloc) (handle, size, prot, off);
238 	else
239 		ret = NULL;
240 	return (ret);
241 }
242 
243 /*
244  *	The object must be locked.
245  */
246 void
247 vm_pager_deallocate(object)
248 	vm_object_t object;
249 {
250 
251 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
252 	(*pagertab[object->type]->pgo_dealloc) (object);
253 }
254 
255 /*
256  * vm_pager_get_pages() - inline, see vm/vm_pager.h
257  * vm_pager_put_pages() - inline, see vm/vm_pager.h
258  * vm_pager_has_page() - inline, see vm/vm_pager.h
259  */
260 
261 /*
262  * Search the specified pager object list for an object with the
263  * specified handle.  If an object with the specified handle is found,
264  * increase its reference count and return it.  Otherwise, return NULL.
265  *
266  * The pager object list must be locked.
267  */
268 vm_object_t
269 vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
270 {
271 	vm_object_t object;
272 
273 	TAILQ_FOREACH(object, pg_list, pager_object_list) {
274 		VM_OBJECT_LOCK(object);
275 		if (object->handle == handle &&
276 		    (object->flags & OBJ_DEAD) == 0) {
277 			vm_object_reference_locked(object);
278 			VM_OBJECT_UNLOCK(object);
279 			break;
280 		}
281 		VM_OBJECT_UNLOCK(object);
282 	}
283 	return (object);
284 }
285 
286 /*
287  * initialize a physical buffer
288  */
289 
290 /*
291  * XXX This probably belongs in vfs_bio.c
292  */
293 static void
294 initpbuf(struct buf *bp)
295 {
296 	KASSERT(bp->b_bufobj == NULL, ("initpbuf with bufobj"));
297 	KASSERT(bp->b_vp == NULL, ("initpbuf with vp"));
298 	bp->b_rcred = NOCRED;
299 	bp->b_wcred = NOCRED;
300 	bp->b_qindex = 0;	/* On no queue (QUEUE_NONE) */
301 	bp->b_saveaddr = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva;
302 	bp->b_data = bp->b_saveaddr;
303 	bp->b_kvabase = bp->b_saveaddr;
304 	bp->b_kvasize = MAXPHYS;
305 	bp->b_xflags = 0;
306 	bp->b_flags = 0;
307 	bp->b_ioflags = 0;
308 	bp->b_iodone = NULL;
309 	bp->b_error = 0;
310 	BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
311 }
312 
313 /*
314  * allocate a physical buffer
315  *
316  *	There are a limited number (nswbuf) of physical buffers.  We need
317  *	to make sure that no single subsystem is able to hog all of them,
318  *	so each subsystem implements a counter which is typically initialized
319  *	to 1/2 nswbuf.  getpbuf() decrements this counter in allocation and
320  *	increments it on release, and blocks if the counter hits zero.  A
321  *	subsystem may initialize the counter to -1 to disable the feature,
322  *	but it must still be sure to match up all uses of getpbuf() with
323  *	relpbuf() using the same variable.
324  *
325  *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
326  *	relatively soon when the rest of the subsystems get smart about it. XXX
327  */
328 struct buf *
329 getpbuf(int *pfreecnt)
330 {
331 	struct buf *bp;
332 
333 	mtx_lock(&pbuf_mtx);
334 
335 	for (;;) {
336 		if (pfreecnt) {
337 			while (*pfreecnt == 0) {
338 				msleep(pfreecnt, &pbuf_mtx, PVM, "wswbuf0", 0);
339 			}
340 		}
341 
342 		/* get a bp from the swap buffer header pool */
343 		if ((bp = TAILQ_FIRST(&bswlist)) != NULL)
344 			break;
345 
346 		bswneeded = 1;
347 		msleep(&bswneeded, &pbuf_mtx, PVM, "wswbuf1", 0);
348 		/* loop in case someone else grabbed one */
349 	}
350 	TAILQ_REMOVE(&bswlist, bp, b_freelist);
351 	if (pfreecnt)
352 		--*pfreecnt;
353 	mtx_unlock(&pbuf_mtx);
354 
355 	initpbuf(bp);
356 	return bp;
357 }
358 
359 /*
360  * allocate a physical buffer, if one is available.
361  *
362  *	Note that there is no NULL hack here - all subsystems using this
363  *	call understand how to use pfreecnt.
364  */
365 struct buf *
366 trypbuf(int *pfreecnt)
367 {
368 	struct buf *bp;
369 
370 	mtx_lock(&pbuf_mtx);
371 	if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) {
372 		mtx_unlock(&pbuf_mtx);
373 		return NULL;
374 	}
375 	TAILQ_REMOVE(&bswlist, bp, b_freelist);
376 
377 	--*pfreecnt;
378 
379 	mtx_unlock(&pbuf_mtx);
380 
381 	initpbuf(bp);
382 
383 	return bp;
384 }
385 
386 /*
387  * release a physical buffer
388  *
389  *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
390  *	relatively soon when the rest of the subsystems get smart about it. XXX
391  */
392 void
393 relpbuf(struct buf *bp, int *pfreecnt)
394 {
395 
396 	if (bp->b_rcred != NOCRED) {
397 		crfree(bp->b_rcred);
398 		bp->b_rcred = NOCRED;
399 	}
400 	if (bp->b_wcred != NOCRED) {
401 		crfree(bp->b_wcred);
402 		bp->b_wcred = NOCRED;
403 	}
404 
405 	KASSERT(bp->b_vp == NULL, ("relpbuf with vp"));
406 	KASSERT(bp->b_bufobj == NULL, ("relpbuf with bufobj"));
407 
408 	BUF_UNLOCK(bp);
409 
410 	mtx_lock(&pbuf_mtx);
411 	TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
412 
413 	if (bswneeded) {
414 		bswneeded = 0;
415 		wakeup(&bswneeded);
416 	}
417 	if (pfreecnt) {
418 		if (++*pfreecnt == 1)
419 			wakeup(pfreecnt);
420 	}
421 	mtx_unlock(&pbuf_mtx);
422 }
423 
424 /*
425  * Associate a p-buffer with a vnode.
426  *
427  * Also sets B_PAGING flag to indicate that vnode is not fully associated
428  * with the buffer.  i.e. the bp has not been linked into the vnode or
429  * ref-counted.
430  */
431 void
432 pbgetvp(struct vnode *vp, struct buf *bp)
433 {
434 
435 	KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
436 	KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
437 
438 	bp->b_vp = vp;
439 	bp->b_flags |= B_PAGING;
440 	bp->b_bufobj = &vp->v_bufobj;
441 }
442 
443 /*
444  * Associate a p-buffer with a vnode.
445  *
446  * Also sets B_PAGING flag to indicate that vnode is not fully associated
447  * with the buffer.  i.e. the bp has not been linked into the vnode or
448  * ref-counted.
449  */
450 void
451 pbgetbo(struct bufobj *bo, struct buf *bp)
452 {
453 
454 	KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
455 	KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
456 
457 	bp->b_flags |= B_PAGING;
458 	bp->b_bufobj = bo;
459 }
460 
461 /*
462  * Disassociate a p-buffer from a vnode.
463  */
464 void
465 pbrelvp(struct buf *bp)
466 {
467 
468 	KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
469 	KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
470 
471 	/* XXX REMOVE ME */
472 	BO_LOCK(bp->b_bufobj);
473 	if (TAILQ_NEXT(bp, b_bobufs) != NULL) {
474 		panic(
475 		    "relpbuf(): b_vp was probably reassignbuf()d %p %x",
476 		    bp,
477 		    (int)bp->b_flags
478 		);
479 	}
480 	BO_UNLOCK(bp->b_bufobj);
481 	bp->b_vp = NULL;
482 	bp->b_bufobj = NULL;
483 	bp->b_flags &= ~B_PAGING;
484 }
485 
486 /*
487  * Disassociate a p-buffer from a bufobj.
488  */
489 void
490 pbrelbo(struct buf *bp)
491 {
492 
493 	KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
494 	KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
495 
496 	/* XXX REMOVE ME */
497 	BO_LOCK(bp->b_bufobj);
498 	if (TAILQ_NEXT(bp, b_bobufs) != NULL) {
499 		panic(
500 		    "relpbuf(): b_vp was probably reassignbuf()d %p %x",
501 		    bp,
502 		    (int)bp->b_flags
503 		);
504 	}
505 	BO_UNLOCK(bp->b_bufobj);
506 	bp->b_bufobj = NULL;
507 	bp->b_flags &= ~B_PAGING;
508 }
509