1139825Simp/*- 21541Srgrimes * Copyright (c) 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * The Mach Operating System project at Carnegie-Mellon University. 71541Srgrimes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 4. Neither the name of the University nor the names of its contributors 171541Srgrimes * may be used to endorse or promote products derived from this software 181541Srgrimes * without specific prior written permission. 191541Srgrimes * 201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301541Srgrimes * SUCH DAMAGE. 311541Srgrimes * 321817Sdg * from: @(#)vm_pager.c 8.6 (Berkeley) 1/12/94 331541Srgrimes * 341541Srgrimes * 351541Srgrimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 361541Srgrimes * All rights reserved. 371541Srgrimes * 381541Srgrimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 395455Sdg * 401541Srgrimes * Permission to use, copy, modify and distribute this software and 411541Srgrimes * its documentation is hereby granted, provided that both the copyright 421541Srgrimes * notice and this permission notice appear in all copies of the 431541Srgrimes * software, derivative works or modified versions, and any portions 441541Srgrimes * thereof, and that both notices appear in supporting documentation. 455455Sdg * 465455Sdg * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 475455Sdg * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 481541Srgrimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 495455Sdg * 501541Srgrimes * Carnegie Mellon requests users of this software to return to 511541Srgrimes * 521541Srgrimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 531541Srgrimes * School of Computer Science 541541Srgrimes * Carnegie Mellon University 551541Srgrimes * Pittsburgh PA 15213-3890 561541Srgrimes * 571541Srgrimes * any improvements or extensions that they make and grant Carnegie the 581541Srgrimes * rights to redistribute these changes. 591541Srgrimes */ 601541Srgrimes 611541Srgrimes/* 621541Srgrimes * Paging space routine stubs. Emulates a matchmaker-like interface 631541Srgrimes * for builtin pagers. 641541Srgrimes */ 651541Srgrimes 66116226Sobrien#include <sys/cdefs.h> 67116226Sobrien__FBSDID("$FreeBSD: stable/10/sys/vm/vm_pager.c 311645 2017-01-07 12:04:30Z kib $"); 68116226Sobrien 691541Srgrimes#include <sys/param.h> 701541Srgrimes#include <sys/systm.h> 7141059Speter#include <sys/kernel.h> 7244739Sjulian#include <sys/vnode.h> 7360041Sphk#include <sys/bio.h> 741887Sdg#include <sys/buf.h> 7569517Sbde#include <sys/ucred.h> 7630354Sphk#include <sys/malloc.h> 77248084Sattilio#include <sys/rwlock.h> 781541Srgrimes 791541Srgrimes#include <vm/vm.h> 8012662Sdg#include <vm/vm_param.h> 81252330Sjeff#include <vm/vm_kern.h> 8212662Sdg#include <vm/vm_object.h> 831541Srgrimes#include <vm/vm_page.h> 849507Sdg#include <vm/vm_pager.h> 8512662Sdg#include <vm/vm_extern.h> 861541Srgrimes 8742957Sdillonint cluster_pbuf_freecnt = -1; /* unlimited to begin with */ 8842957Sdillon 8992727Salfredstatic int dead_pager_getpages(vm_object_t, vm_page_t *, int, int); 9092727Salfredstatic vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 91194766Skib vm_ooffset_t, struct ucred *); 9292727Salfredstatic void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 9392727Salfredstatic boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 9492727Salfredstatic void dead_pager_dealloc(vm_object_t); 9534611Sdyson 9643129Sdillonstatic int 9734611Sdysondead_pager_getpages(obj, ma, count, req) 9834611Sdyson vm_object_t obj; 9934611Sdyson vm_page_t *ma; 10034611Sdyson int count; 10134611Sdyson int req; 10234611Sdyson{ 10334611Sdyson return VM_PAGER_FAIL; 10434611Sdyson} 10534611Sdyson 10643129Sdillonstatic vm_object_t 107189004Srdivackydead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 108194766Skib vm_ooffset_t off, struct ucred *cred) 10934611Sdyson{ 110311645Skib 111311645Skib return (NULL); 11234611Sdyson} 11334611Sdyson 11443129Sdillonstatic void 115311645Skibdead_pager_putpages(vm_object_t object, vm_page_t *m, int count, 116311645Skib int flags, int *rtvals) 11734611Sdyson{ 11834611Sdyson int i; 11943129Sdillon 120311645Skib for (i = 0; i < count; i++) 12134611Sdyson rtvals[i] = VM_PAGER_AGAIN; 12234611Sdyson} 12334611Sdyson 12443129Sdillonstatic int 125311645Skibdead_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *prev, int *next) 12634611Sdyson{ 127311645Skib 128311645Skib if (prev != NULL) 12934611Sdyson *prev = 0; 130311645Skib if (next != NULL) 13134611Sdyson *next = 0; 132311645Skib return (FALSE); 13334611Sdyson} 13434611Sdyson 13543129Sdillonstatic void 136311645Skibdead_pager_dealloc(vm_object_t object) 13734611Sdyson{ 138311645Skib 13934611Sdyson} 14034611Sdyson 14145567Seivindstatic struct pagerops deadpagerops = { 142118466Sphk .pgo_alloc = dead_pager_alloc, 143118466Sphk .pgo_dealloc = dead_pager_dealloc, 144118466Sphk .pgo_getpages = dead_pager_getpages, 145118466Sphk .pgo_putpages = dead_pager_putpages, 146118466Sphk .pgo_haspage = dead_pager_haspage, 14734611Sdyson}; 14834611Sdyson 14942957Sdillonstruct pagerops *pagertab[] = { 1509507Sdg &defaultpagerops, /* OBJT_DEFAULT */ 1519507Sdg &swappagerops, /* OBJT_SWAP */ 1529507Sdg &vnodepagerops, /* OBJT_VNODE */ 1539507Sdg &devicepagerops, /* OBJT_DEVICE */ 15460757Speter &physpagerops, /* OBJT_PHYS */ 155195840Sjhb &deadpagerops, /* OBJT_DEAD */ 156235375Skib &sgpagerops, /* OBJT_SG */ 157235375Skib &mgtdevicepagerops, /* OBJT_MGTDEVICE */ 1581541Srgrimes}; 1591541Srgrimes 160141629Sphkstatic const int npagers = sizeof(pagertab) / sizeof(pagertab[0]); 16142957Sdillon 1621541Srgrimes/* 1631541Srgrimes * Kernel address space for mapping pages. 1641541Srgrimes * Used by pagers where KVAs are needed for IO. 1651541Srgrimes * 1661541Srgrimes * XXX needs to be large enough to support the number of pending async 1671541Srgrimes * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size 1681541Srgrimes * (MAXPHYS == 64k) if you want to get the most efficiency. 1691541Srgrimes */ 170252330Sjeffstruct mtx_padalign pbuf_mtx; 171252330Sjeffstatic TAILQ_HEAD(swqueue, buf) bswlist; 17212820Sphkstatic int bswneeded; 173252330Sjeffvm_offset_t swapbkva; /* swap buffers kva */ 1741541Srgrimes 1751541Srgrimesvoid 176311645Skibvm_pager_init(void) 1771541Srgrimes{ 1781541Srgrimes struct pagerops **pgops; 1791541Srgrimes 18091700Seivind TAILQ_INIT(&bswlist); 1811541Srgrimes /* 1821541Srgrimes * Initialize known pagers 1831541Srgrimes */ 1841541Srgrimes for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++) 185227529Skib if ((*pgops)->pgo_init != NULL) 186311645Skib (*(*pgops)->pgo_init)(); 1871541Srgrimes} 1881541Srgrimes 1891887Sdgvoid 190311645Skibvm_pager_bufferinit(void) 1911887Sdg{ 1921887Sdg struct buf *bp; 1931887Sdg int i; 1945455Sdg 19593818Sjhb mtx_init(&pbuf_mtx, "pbuf mutex", NULL, MTX_DEF); 1961887Sdg bp = swbuf; 1971887Sdg /* 1981887Sdg * Now set up swap and physical I/O buffer headers. 1991887Sdg */ 20032724Sdyson for (i = 0; i < nswbuf; i++, bp++) { 2011887Sdg TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist); 20248225Smckusick BUF_LOCKINIT(bp); 20348225Smckusick LIST_INIT(&bp->b_dep); 2041887Sdg bp->b_rcred = bp->b_wcred = NOCRED; 20540790Speter bp->b_xflags = 0; 2061887Sdg } 2071887Sdg 20842957Sdillon cluster_pbuf_freecnt = nswbuf / 2; 209149035Skan vnode_pbuf_freecnt = nswbuf / 2 + 1; 2101887Sdg} 2111887Sdg 2121541Srgrimes/* 2131541Srgrimes * Allocate an instance of a pager of the given type. 2141541Srgrimes * Size, protection and offset parameters are passed in for pagers that 2151541Srgrimes * need to perform page-level validation (e.g. the device pager). 2161541Srgrimes */ 2179507Sdgvm_object_t 21876827Salfredvm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size, 219194766Skib vm_prot_t prot, vm_ooffset_t off, struct ucred *cred) 2201541Srgrimes{ 22176827Salfred vm_object_t ret; 2221541Srgrimes struct pagerops *ops; 2231541Srgrimes 2249507Sdg ops = pagertab[type]; 2251541Srgrimes if (ops) 226311645Skib ret = (*ops->pgo_alloc)(handle, size, prot, off, cred); 22776827Salfred else 22876827Salfred ret = NULL; 22976827Salfred return (ret); 2301541Srgrimes} 2311541Srgrimes 232114774Salc/* 233114774Salc * The object must be locked. 234114774Salc */ 2351541Srgrimesvoid 236311645Skibvm_pager_deallocate(vm_object_t object) 2371541Srgrimes{ 238114774Salc 239248084Sattilio VM_OBJECT_ASSERT_WLOCKED(object); 2409507Sdg (*pagertab[object->type]->pgo_dealloc) (object); 2411541Srgrimes} 2421541Srgrimes 24342957Sdillon/* 24442957Sdillon * vm_pager_get_pages() - inline, see vm/vm_pager.h 24542957Sdillon * vm_pager_put_pages() - inline, see vm/vm_pager.h 24642957Sdillon * vm_pager_has_page() - inline, see vm/vm_pager.h 24742957Sdillon */ 2481549Srgrimes 249171737Salc/* 250171737Salc * Search the specified pager object list for an object with the 251171737Salc * specified handle. If an object with the specified handle is found, 252171737Salc * increase its reference count and return it. Otherwise, return NULL. 253171737Salc * 254171737Salc * The pager object list must be locked. 255171737Salc */ 2569507Sdgvm_object_t 257171737Salcvm_pager_object_lookup(struct pagerlst *pg_list, void *handle) 2581541Srgrimes{ 25979242Sdillon vm_object_t object; 2601541Srgrimes 261171737Salc TAILQ_FOREACH(object, pg_list, pager_object_list) { 262235829Savg if (object->handle == handle) { 263248084Sattilio VM_OBJECT_WLOCK(object); 264235829Savg if ((object->flags & OBJ_DEAD) == 0) { 265235829Savg vm_object_reference_locked(object); 266248084Sattilio VM_OBJECT_WUNLOCK(object); 267235829Savg break; 268235829Savg } 269248084Sattilio VM_OBJECT_WUNLOCK(object); 270171737Salc } 271171737Salc } 272171737Salc return (object); 2731541Srgrimes} 2741541Srgrimes 2751541Srgrimes/* 27620054Sdyson * initialize a physical buffer 27720054Sdyson */ 27820054Sdyson 27991700Seivind/* 28091700Seivind * XXX This probably belongs in vfs_bio.c 28191700Seivind */ 28220054Sdysonstatic void 28348274Speterinitpbuf(struct buf *bp) 28448274Speter{ 285311645Skib 286137723Sphk KASSERT(bp->b_bufobj == NULL, ("initpbuf with bufobj")); 287137723Sphk KASSERT(bp->b_vp == NULL, ("initpbuf with vp")); 28820054Sdyson bp->b_rcred = NOCRED; 28920054Sdyson bp->b_wcred = NOCRED; 29091700Seivind bp->b_qindex = 0; /* On no queue (QUEUE_NONE) */ 291311645Skib bp->b_saveaddr = (caddr_t)(MAXPHYS * (bp - swbuf)) + swapbkva; 292116605Sphk bp->b_data = bp->b_saveaddr; 293116605Sphk bp->b_kvabase = bp->b_saveaddr; 29420054Sdyson bp->b_kvasize = MAXPHYS; 29540790Speter bp->b_xflags = 0; 29648225Smckusick bp->b_flags = 0; 29758934Sphk bp->b_ioflags = 0; 29858345Sphk bp->b_iodone = NULL; 29948225Smckusick bp->b_error = 0; 300111463Sjeff BUF_LOCK(bp, LK_EXCLUSIVE, NULL); 30120054Sdyson} 30220054Sdyson 30320054Sdyson/* 3045455Sdg * allocate a physical buffer 30542957Sdillon * 30642957Sdillon * There are a limited number (nswbuf) of physical buffers. We need 30742957Sdillon * to make sure that no single subsystem is able to hog all of them, 30842957Sdillon * so each subsystem implements a counter which is typically initialized 30942957Sdillon * to 1/2 nswbuf. getpbuf() decrements this counter in allocation and 31042957Sdillon * increments it on release, and blocks if the counter hits zero. A 31142957Sdillon * subsystem may initialize the counter to -1 to disable the feature, 31242957Sdillon * but it must still be sure to match up all uses of getpbuf() with 31342957Sdillon * relpbuf() using the same variable. 31442957Sdillon * 31542957Sdillon * NOTE: pfreecnt can be NULL, but this 'feature' will be removed 31642957Sdillon * relatively soon when the rest of the subsystems get smart about it. XXX 3171887Sdg */ 3181887Sdgstruct buf * 319137722Sphkgetpbuf(int *pfreecnt) 3205455Sdg{ 3211887Sdg struct buf *bp; 3221887Sdg 32375474Salfred mtx_lock(&pbuf_mtx); 32448590Smckay for (;;) { 325311645Skib if (pfreecnt != NULL) { 32648590Smckay while (*pfreecnt == 0) { 32775474Salfred msleep(pfreecnt, &pbuf_mtx, PVM, "wswbuf0", 0); 32848590Smckay } 32942957Sdillon } 33042957Sdillon 33148590Smckay /* get a bp from the swap buffer header pool */ 33248590Smckay if ((bp = TAILQ_FIRST(&bswlist)) != NULL) 33348590Smckay break; 33448590Smckay 3351887Sdg bswneeded = 1; 33675474Salfred msleep(&bswneeded, &pbuf_mtx, PVM, "wswbuf1", 0); 33748590Smckay /* loop in case someone else grabbed one */ 3381887Sdg } 3391887Sdg TAILQ_REMOVE(&bswlist, bp, b_freelist); 34042957Sdillon if (pfreecnt) 34142957Sdillon --*pfreecnt; 34275474Salfred mtx_unlock(&pbuf_mtx); 34320054Sdyson initpbuf(bp); 344311645Skib return (bp); 3451887Sdg} 3461887Sdg 3471887Sdg/* 34842957Sdillon * allocate a physical buffer, if one is available. 34942957Sdillon * 35042957Sdillon * Note that there is no NULL hack here - all subsystems using this 35142957Sdillon * call understand how to use pfreecnt. 3521887Sdg */ 3531887Sdgstruct buf * 354137722Sphktrypbuf(int *pfreecnt) 3555455Sdg{ 3561887Sdg struct buf *bp; 3571887Sdg 35875474Salfred mtx_lock(&pbuf_mtx); 35942957Sdillon if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) { 36075474Salfred mtx_unlock(&pbuf_mtx); 3611887Sdg return NULL; 3621887Sdg } 3631887Sdg TAILQ_REMOVE(&bswlist, bp, b_freelist); 36442957Sdillon --*pfreecnt; 36575474Salfred mtx_unlock(&pbuf_mtx); 36620054Sdyson initpbuf(bp); 367311645Skib return (bp); 3681887Sdg} 3691887Sdg 3701887Sdg/* 3711887Sdg * release a physical buffer 37242957Sdillon * 37342957Sdillon * NOTE: pfreecnt can be NULL, but this 'feature' will be removed 37442957Sdillon * relatively soon when the rest of the subsystems get smart about it. XXX 3751887Sdg */ 3761887Sdgvoid 377137722Sphkrelpbuf(struct buf *bp, int *pfreecnt) 3781887Sdg{ 3791887Sdg 3801887Sdg if (bp->b_rcred != NOCRED) { 3811887Sdg crfree(bp->b_rcred); 3821887Sdg bp->b_rcred = NOCRED; 3831887Sdg } 3841887Sdg if (bp->b_wcred != NOCRED) { 3851887Sdg crfree(bp->b_wcred); 3861887Sdg bp->b_wcred = NOCRED; 3871887Sdg } 38842957Sdillon 389137723Sphk KASSERT(bp->b_vp == NULL, ("relpbuf with vp")); 390137723Sphk KASSERT(bp->b_bufobj == NULL, ("relpbuf with bufobj")); 391137723Sphk 39248225Smckusick BUF_UNLOCK(bp); 3935166Sdg 394113138Salc mtx_lock(&pbuf_mtx); 3951887Sdg TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist); 3961887Sdg 3971887Sdg if (bswneeded) { 3981887Sdg bswneeded = 0; 3999507Sdg wakeup(&bswneeded); 4001887Sdg } 40142957Sdillon if (pfreecnt) { 40242957Sdillon if (++*pfreecnt == 1) 40342957Sdillon wakeup(pfreecnt); 40442957Sdillon } 40575474Salfred mtx_unlock(&pbuf_mtx); 4061887Sdg} 407137721Sphk 408137721Sphk/* 409137721Sphk * Associate a p-buffer with a vnode. 410137721Sphk * 411137721Sphk * Also sets B_PAGING flag to indicate that vnode is not fully associated 412137721Sphk * with the buffer. i.e. the bp has not been linked into the vnode or 413137721Sphk * ref-counted. 414137721Sphk */ 415137721Sphkvoid 416137721Sphkpbgetvp(struct vnode *vp, struct buf *bp) 417137721Sphk{ 418137721Sphk 419137721Sphk KASSERT(bp->b_vp == NULL, ("pbgetvp: not free")); 420137723Sphk KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)")); 421137721Sphk 422137721Sphk bp->b_vp = vp; 423137721Sphk bp->b_flags |= B_PAGING; 424137721Sphk bp->b_bufobj = &vp->v_bufobj; 425137721Sphk} 426137721Sphk 427137721Sphk/* 428137725Sphk * Associate a p-buffer with a vnode. 429137725Sphk * 430137725Sphk * Also sets B_PAGING flag to indicate that vnode is not fully associated 431137725Sphk * with the buffer. i.e. the bp has not been linked into the vnode or 432137725Sphk * ref-counted. 433137725Sphk */ 434137725Sphkvoid 435137725Sphkpbgetbo(struct bufobj *bo, struct buf *bp) 436137725Sphk{ 437137725Sphk 438137725Sphk KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)")); 439137725Sphk KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)")); 440137725Sphk 441137725Sphk bp->b_flags |= B_PAGING; 442137725Sphk bp->b_bufobj = bo; 443137725Sphk} 444137725Sphk 445137725Sphk/* 446137721Sphk * Disassociate a p-buffer from a vnode. 447137721Sphk */ 448137721Sphkvoid 449137721Sphkpbrelvp(struct buf *bp) 450137721Sphk{ 451137721Sphk 452137721Sphk KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL")); 453137721Sphk KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj")); 454249218Sjeff KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0, 455249218Sjeff ("pbrelvp: pager buf on vnode list.")); 456137721Sphk 457137721Sphk bp->b_vp = NULL; 458137721Sphk bp->b_bufobj = NULL; 459137721Sphk bp->b_flags &= ~B_PAGING; 460137721Sphk} 461137725Sphk 462137725Sphk/* 463137725Sphk * Disassociate a p-buffer from a bufobj. 464137725Sphk */ 465137725Sphkvoid 466137725Sphkpbrelbo(struct buf *bp) 467137725Sphk{ 468137725Sphk 469137725Sphk KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode")); 470137725Sphk KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj")); 471249218Sjeff KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0, 472249218Sjeff ("pbrelbo: pager buf on vnode list.")); 473137725Sphk 474137725Sphk bp->b_bufobj = NULL; 475137725Sphk bp->b_flags &= ~B_PAGING; 476137725Sphk} 477