2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon 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 * 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.
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
32 * from: @(#)vm_pager.c 8.6 (Berkeley) 1/12/94
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
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.
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.
50 * Carnegie Mellon requests users of this software to return to
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
62 * Paging space routine stubs. Emulates a matchmaker-like interface
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/vnode.h>
75 #include <sys/ucred.h>
76 #include <sys/malloc.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>
85 MALLOC_DEFINE(M_VMPGDATA, "VM pgdata", "XXX: VM pager private data");
87 int cluster_pbuf_freecnt = -1; /* unlimited to begin with */
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,
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);
97 dead_pager_getpages(obj, ma, count, req)
103 return VM_PAGER_FAIL;
107 dead_pager_alloc(handle, size, prot, off)
117 dead_pager_putpages(object, m, count, flags, rtvals)
126 for (i = 0; i < count; i++) {
127 rtvals[i] = VM_PAGER_AGAIN;
132 dead_pager_haspage(object, pindex, prev, next)
146 dead_pager_dealloc(object)
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,
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 */
169 static const int npagers = sizeof(pagertab) / sizeof(pagertab[0]);
172 * Kernel address space for mapping pages.
173 * Used by pagers where KVAs are needed for IO.
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.
180 static int bswneeded;
181 static vm_offset_t swapbkva; /* swap buffers kva */
183 static TAILQ_HEAD(swqueue, buf) bswlist;
188 struct pagerops **pgops;
190 TAILQ_INIT(&bswlist);
192 * Initialize known pagers
194 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
195 if (pgops && ((*pgops)->pgo_init != NULL))
196 (*(*pgops)->pgo_init) ();
200 vm_pager_bufferinit()
205 mtx_init(&pbuf_mtx, "pbuf mutex", NULL, MTX_DEF);
208 * Now set up swap and physical I/O buffer headers.
210 for (i = 0; i < nswbuf; i++, bp++) {
211 TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
213 LIST_INIT(&bp->b_dep);
214 bp->b_rcred = bp->b_wcred = NOCRED;
218 cluster_pbuf_freecnt = nswbuf / 2;
220 swapbkva = kmem_alloc_nofault(pager_map, nswbuf * MAXPHYS);
222 panic("Not enough pager_map VM space for physical buffers");
226 * Allocate an instance of a pager of the given type.
227 * Size, protection and offset parameters are passed in for pagers that
228 * need to perform page-level validation (e.g. the device pager).
231 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
232 vm_prot_t prot, vm_ooffset_t off)
235 struct pagerops *ops;
237 ops = pagertab[type];
239 ret = (*ops->pgo_alloc) (handle, size, prot, off);
246 * The object must be locked.
249 vm_pager_deallocate(object)
253 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
254 (*pagertab[object->type]->pgo_dealloc) (object);
258 * vm_pager_get_pages() - inline, see vm/vm_pager.h
259 * vm_pager_put_pages() - inline, see vm/vm_pager.h
260 * vm_pager_has_page() - inline, see vm/vm_pager.h
264 vm_pager_object_lookup(pg_list, handle)
265 struct pagerlst *pg_list;
270 TAILQ_FOREACH(object, pg_list, pager_object_list)
271 if (object->handle == handle)
277 * initialize a physical buffer
281 * XXX This probably belongs in vfs_bio.c
284 initpbuf(struct buf *bp)
286 KASSERT(bp->b_bufobj == NULL, ("initpbuf with bufobj"));
287 KASSERT(bp->b_vp == NULL, ("initpbuf with vp"));
288 bp->b_rcred = NOCRED;
289 bp->b_wcred = NOCRED;
290 bp->b_qindex = 0; /* On no queue (QUEUE_NONE) */
291 bp->b_saveaddr = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva;
292 bp->b_data = bp->b_saveaddr;
293 bp->b_kvabase = bp->b_saveaddr;
294 bp->b_kvasize = MAXPHYS;
300 BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
304 * allocate a physical buffer
306 * There are a limited number (nswbuf) of physical buffers. We need
307 * to make sure that no single subsystem is able to hog all of them,
308 * so each subsystem implements a counter which is typically initialized
309 * to 1/2 nswbuf. getpbuf() decrements this counter in allocation and
310 * increments it on release, and blocks if the counter hits zero. A
311 * subsystem may initialize the counter to -1 to disable the feature,
312 * but it must still be sure to match up all uses of getpbuf() with
313 * relpbuf() using the same variable.
315 * NOTE: pfreecnt can be NULL, but this 'feature' will be removed
316 * relatively soon when the rest of the subsystems get smart about it. XXX
319 getpbuf(int *pfreecnt)
327 while (*pfreecnt == 0) {
328 msleep(pfreecnt, &pbuf_mtx, PVM, "wswbuf0", 0);
332 /* get a bp from the swap buffer header pool */
333 if ((bp = TAILQ_FIRST(&bswlist)) != NULL)
337 msleep(&bswneeded, &pbuf_mtx, PVM, "wswbuf1", 0);
338 /* loop in case someone else grabbed one */
340 TAILQ_REMOVE(&bswlist, bp, b_freelist);
343 mtx_unlock(&pbuf_mtx);
350 * allocate a physical buffer, if one is available.
352 * Note that there is no NULL hack here - all subsystems using this
353 * call understand how to use pfreecnt.
356 trypbuf(int *pfreecnt)
361 if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) {
362 mtx_unlock(&pbuf_mtx);
365 TAILQ_REMOVE(&bswlist, bp, b_freelist);
369 mtx_unlock(&pbuf_mtx);
377 * release a physical buffer
379 * NOTE: pfreecnt can be NULL, but this 'feature' will be removed
380 * relatively soon when the rest of the subsystems get smart about it. XXX
383 relpbuf(struct buf *bp, int *pfreecnt)
386 if (bp->b_rcred != NOCRED) {
388 bp->b_rcred = NOCRED;
390 if (bp->b_wcred != NOCRED) {
392 bp->b_wcred = NOCRED;
395 KASSERT(bp->b_vp == NULL, ("relpbuf with vp"));
396 KASSERT(bp->b_bufobj == NULL, ("relpbuf with bufobj"));
401 TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
408 if (++*pfreecnt == 1)
411 mtx_unlock(&pbuf_mtx);
415 * Associate a p-buffer with a vnode.
417 * Also sets B_PAGING flag to indicate that vnode is not fully associated
418 * with the buffer. i.e. the bp has not been linked into the vnode or
422 pbgetvp(struct vnode *vp, struct buf *bp)
425 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
426 KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
429 bp->b_flags |= B_PAGING;
430 bp->b_bufobj = &vp->v_bufobj;
434 * Associate a p-buffer with a vnode.
436 * Also sets B_PAGING flag to indicate that vnode is not fully associated
437 * with the buffer. i.e. the bp has not been linked into the vnode or
441 pbgetbo(struct bufobj *bo, struct buf *bp)
444 KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
445 KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
447 bp->b_flags |= B_PAGING;
452 * Disassociate a p-buffer from a vnode.
455 pbrelvp(struct buf *bp)
458 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
459 KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
462 BO_LOCK(bp->b_bufobj);
463 if (TAILQ_NEXT(bp, b_bobufs) != NULL) {
465 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
470 BO_UNLOCK(bp->b_bufobj);
473 bp->b_flags &= ~B_PAGING;
477 * Disassociate a p-buffer from a bufobj.
480 pbrelbo(struct buf *bp)
483 KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
484 KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
487 BO_LOCK(bp->b_bufobj);
488 if (TAILQ_NEXT(bp, b_bobufs) != NULL) {
490 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
495 BO_UNLOCK(bp->b_bufobj);
497 bp->b_flags &= ~B_PAGING;