The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_pager.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: releng/8.0/sys/vm/vm_pager.c 195840 2009-07-24 13:50:29Z jhb $");
   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, struct ucred *);
   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, struct ucred *cred)
  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         &sgpagerops             /* OBJT_SG */
  165 };
  166 
  167 static const int npagers = sizeof(pagertab) / sizeof(pagertab[0]);
  168 
  169 /*
  170  * Kernel address space for mapping pages.
  171  * Used by pagers where KVAs are needed for IO.
  172  *
  173  * XXX needs to be large enough to support the number of pending async
  174  * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
  175  * (MAXPHYS == 64k) if you want to get the most efficiency.
  176  */
  177 vm_map_t pager_map;
  178 static int bswneeded;
  179 static vm_offset_t swapbkva;            /* swap buffers kva */
  180 struct mtx pbuf_mtx;
  181 static TAILQ_HEAD(swqueue, buf) bswlist;
  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[npagers]; pgops++)
  193                 if (pgops && ((*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 
  219         swapbkva = kmem_alloc_nofault(pager_map, nswbuf * MAXPHYS);
  220         if (!swapbkva)
  221                 panic("Not enough pager_map VM space for physical buffers");
  222 }
  223 
  224 /*
  225  * Allocate an instance of a pager of the given type.
  226  * Size, protection and offset parameters are passed in for pagers that
  227  * need to perform page-level validation (e.g. the device pager).
  228  */
  229 vm_object_t
  230 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
  231     vm_prot_t prot, vm_ooffset_t off, struct ucred *cred)
  232 {
  233         vm_object_t ret;
  234         struct pagerops *ops;
  235 
  236         ops = pagertab[type];
  237         if (ops)
  238                 ret = (*ops->pgo_alloc) (handle, size, prot, off, cred);
  239         else
  240                 ret = NULL;
  241         return (ret);
  242 }
  243 
  244 /*
  245  *      The object must be locked.
  246  */
  247 void
  248 vm_pager_deallocate(object)
  249         vm_object_t object;
  250 {
  251 
  252         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  253         (*pagertab[object->type]->pgo_dealloc) (object);
  254 }
  255 
  256 /*
  257  * vm_pager_get_pages() - inline, see vm/vm_pager.h
  258  * vm_pager_put_pages() - inline, see vm/vm_pager.h
  259  * vm_pager_has_page() - inline, see vm/vm_pager.h
  260  */
  261 
  262 /*
  263  * Search the specified pager object list for an object with the
  264  * specified handle.  If an object with the specified handle is found,
  265  * increase its reference count and return it.  Otherwise, return NULL.
  266  *
  267  * The pager object list must be locked.
  268  */
  269 vm_object_t
  270 vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
  271 {
  272         vm_object_t object;
  273 
  274         TAILQ_FOREACH(object, pg_list, pager_object_list) {
  275                 VM_OBJECT_LOCK(object);
  276                 if (object->handle == handle &&
  277                     (object->flags & OBJ_DEAD) == 0) {
  278                         vm_object_reference_locked(object);
  279                         VM_OBJECT_UNLOCK(object);
  280                         break;
  281                 }
  282                 VM_OBJECT_UNLOCK(object);
  283         }
  284         return (object);
  285 }
  286 
  287 /*
  288  * initialize a physical buffer
  289  */
  290 
  291 /*
  292  * XXX This probably belongs in vfs_bio.c
  293  */
  294 static void
  295 initpbuf(struct buf *bp)
  296 {
  297         KASSERT(bp->b_bufobj == NULL, ("initpbuf with bufobj"));
  298         KASSERT(bp->b_vp == NULL, ("initpbuf with vp"));
  299         bp->b_rcred = NOCRED;
  300         bp->b_wcred = NOCRED;
  301         bp->b_qindex = 0;       /* On no queue (QUEUE_NONE) */
  302         bp->b_saveaddr = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva;
  303         bp->b_data = bp->b_saveaddr;
  304         bp->b_kvabase = bp->b_saveaddr;
  305         bp->b_kvasize = MAXPHYS;
  306         bp->b_xflags = 0;
  307         bp->b_flags = 0;
  308         bp->b_ioflags = 0;
  309         bp->b_iodone = NULL;
  310         bp->b_error = 0;
  311         BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
  312 }
  313 
  314 /*
  315  * allocate a physical buffer
  316  *
  317  *      There are a limited number (nswbuf) of physical buffers.  We need
  318  *      to make sure that no single subsystem is able to hog all of them,
  319  *      so each subsystem implements a counter which is typically initialized
  320  *      to 1/2 nswbuf.  getpbuf() decrements this counter in allocation and
  321  *      increments it on release, and blocks if the counter hits zero.  A
  322  *      subsystem may initialize the counter to -1 to disable the feature,
  323  *      but it must still be sure to match up all uses of getpbuf() with 
  324  *      relpbuf() using the same variable.
  325  *
  326  *      NOTE: pfreecnt can be NULL, but this 'feature' will be removed
  327  *      relatively soon when the rest of the subsystems get smart about it. XXX
  328  */
  329 struct buf *
  330 getpbuf(int *pfreecnt)
  331 {
  332         struct buf *bp;
  333 
  334         mtx_lock(&pbuf_mtx);
  335 
  336         for (;;) {
  337                 if (pfreecnt) {
  338                         while (*pfreecnt == 0) {
  339                                 msleep(pfreecnt, &pbuf_mtx, PVM, "wswbuf0", 0);
  340                         }
  341                 }
  342 
  343                 /* get a bp from the swap buffer header pool */
  344                 if ((bp = TAILQ_FIRST(&bswlist)) != NULL)
  345                         break;
  346 
  347                 bswneeded = 1;
  348                 msleep(&bswneeded, &pbuf_mtx, PVM, "wswbuf1", 0);
  349                 /* loop in case someone else grabbed one */
  350         }
  351         TAILQ_REMOVE(&bswlist, bp, b_freelist);
  352         if (pfreecnt)
  353                 --*pfreecnt;
  354         mtx_unlock(&pbuf_mtx);
  355 
  356         initpbuf(bp);
  357         return bp;
  358 }
  359 
  360 /*
  361  * allocate a physical buffer, if one is available.
  362  *
  363  *      Note that there is no NULL hack here - all subsystems using this
  364  *      call understand how to use pfreecnt.
  365  */
  366 struct buf *
  367 trypbuf(int *pfreecnt)
  368 {
  369         struct buf *bp;
  370 
  371         mtx_lock(&pbuf_mtx);
  372         if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) {
  373                 mtx_unlock(&pbuf_mtx);
  374                 return NULL;
  375         }
  376         TAILQ_REMOVE(&bswlist, bp, b_freelist);
  377 
  378         --*pfreecnt;
  379 
  380         mtx_unlock(&pbuf_mtx);
  381 
  382         initpbuf(bp);
  383 
  384         return bp;
  385 }
  386 
  387 /*
  388  * release a physical buffer
  389  *
  390  *      NOTE: pfreecnt can be NULL, but this 'feature' will be removed
  391  *      relatively soon when the rest of the subsystems get smart about it. XXX
  392  */
  393 void
  394 relpbuf(struct buf *bp, int *pfreecnt)
  395 {
  396 
  397         if (bp->b_rcred != NOCRED) {
  398                 crfree(bp->b_rcred);
  399                 bp->b_rcred = NOCRED;
  400         }
  401         if (bp->b_wcred != NOCRED) {
  402                 crfree(bp->b_wcred);
  403                 bp->b_wcred = NOCRED;
  404         }
  405 
  406         KASSERT(bp->b_vp == NULL, ("relpbuf with vp"));
  407         KASSERT(bp->b_bufobj == NULL, ("relpbuf with bufobj"));
  408 
  409         BUF_UNLOCK(bp);
  410 
  411         mtx_lock(&pbuf_mtx);
  412         TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
  413 
  414         if (bswneeded) {
  415                 bswneeded = 0;
  416                 wakeup(&bswneeded);
  417         }
  418         if (pfreecnt) {
  419                 if (++*pfreecnt == 1)
  420                         wakeup(pfreecnt);
  421         }
  422         mtx_unlock(&pbuf_mtx);
  423 }
  424 
  425 /*
  426  * Associate a p-buffer with a vnode.
  427  *
  428  * Also sets B_PAGING flag to indicate that vnode is not fully associated
  429  * with the buffer.  i.e. the bp has not been linked into the vnode or
  430  * ref-counted.
  431  */
  432 void
  433 pbgetvp(struct vnode *vp, struct buf *bp)
  434 {
  435 
  436         KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
  437         KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
  438 
  439         bp->b_vp = vp;
  440         bp->b_flags |= B_PAGING;
  441         bp->b_bufobj = &vp->v_bufobj;
  442 }
  443 
  444 /*
  445  * Associate a p-buffer with a vnode.
  446  *
  447  * Also sets B_PAGING flag to indicate that vnode is not fully associated
  448  * with the buffer.  i.e. the bp has not been linked into the vnode or
  449  * ref-counted.
  450  */
  451 void
  452 pbgetbo(struct bufobj *bo, struct buf *bp)
  453 {
  454 
  455         KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
  456         KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
  457 
  458         bp->b_flags |= B_PAGING;
  459         bp->b_bufobj = bo;
  460 }
  461 
  462 /*
  463  * Disassociate a p-buffer from a vnode.
  464  */
  465 void
  466 pbrelvp(struct buf *bp)
  467 {
  468 
  469         KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
  470         KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
  471 
  472         /* XXX REMOVE ME */
  473         BO_LOCK(bp->b_bufobj);
  474         if (TAILQ_NEXT(bp, b_bobufs) != NULL) {
  475                 panic(
  476                     "relpbuf(): b_vp was probably reassignbuf()d %p %x",
  477                     bp,
  478                     (int)bp->b_flags
  479                 );
  480         }
  481         BO_UNLOCK(bp->b_bufobj);
  482         bp->b_vp = NULL;
  483         bp->b_bufobj = NULL;
  484         bp->b_flags &= ~B_PAGING;
  485 }
  486 
  487 /*
  488  * Disassociate a p-buffer from a bufobj.
  489  */
  490 void
  491 pbrelbo(struct buf *bp)
  492 {
  493 
  494         KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
  495         KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
  496 
  497         /* XXX REMOVE ME */
  498         BO_LOCK(bp->b_bufobj);
  499         if (TAILQ_NEXT(bp, b_bobufs) != NULL) {
  500                 panic(
  501                     "relpbuf(): b_vp was probably reassignbuf()d %p %x",
  502                     bp,
  503                     (int)bp->b_flags
  504                 );
  505         }
  506         BO_UNLOCK(bp->b_bufobj);
  507         bp->b_bufobj = NULL;
  508         bp->b_flags &= ~B_PAGING;
  509 }

Cache object: 69fa53a6ce1dcfa3bd75c0926a3dc1c0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.