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