1139825Simp/*- 29514Sdg * Copyright (c) 1995, David Greenman 39514Sdg * All rights reserved. 49514Sdg * 59514Sdg * Redistribution and use in source and binary forms, with or without 69514Sdg * modification, are permitted provided that the following conditions 79514Sdg * are met: 89514Sdg * 1. Redistributions of source code must retain the above copyright 99514Sdg * notice, this list of conditions and the following disclaimer. 109514Sdg * 2. Redistributions in binary form must reproduce the above copyright 119514Sdg * notice, this list of conditions and the following disclaimer in the 129514Sdg * documentation and/or other materials provided with the distribution. 139514Sdg * 3. All advertising materials mentioning features or use of this software 1458705Scharnier * must display the following acknowledgement: 159514Sdg * This product includes software developed by David Greenman. 169514Sdg * 4. The name of the author may not be used to endorse or promote products 179514Sdg * derived from this software without specific prior written permission. 189514Sdg * 199514Sdg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 209514Sdg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219514Sdg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229514Sdg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 239514Sdg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249514Sdg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259514Sdg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269514Sdg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279514Sdg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289514Sdg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299514Sdg * SUCH DAMAGE. 309514Sdg * 3142957Sdillon * The default pager is responsible for supplying backing store to unbacked 3242957Sdillon * storage. The backing store is usually swap so we just fall through to 3342957Sdillon * the swap routines. However, since swap metadata has not been assigned, 3442957Sdillon * the swap routines assign and manage the swap backing store through the 3542957Sdillon * vm_page->swapblk field. The object is only converted when the page is 3642957Sdillon * physically freed after having been cleaned and even then vm_page->swapblk 3742957Sdillon * is maintained whenever a resident page also has swap backing store. 389514Sdg */ 399514Sdg 40116226Sobrien#include <sys/cdefs.h> 41116226Sobrien__FBSDID("$FreeBSD: stable/10/sys/vm/default_pager.c 310363 2016-12-21 11:32:08Z kib $"); 42116226Sobrien 439513Sdg#include <sys/param.h> 449513Sdg#include <sys/systm.h> 4576827Salfred#include <sys/lock.h> 4679224Sdillon#include <sys/proc.h> 47194766Skib#include <sys/resourcevar.h> 48248084Sattilio#include <sys/rwlock.h> 499513Sdg 509513Sdg#include <vm/vm.h> 5112662Sdg#include <vm/vm_object.h> 5212662Sdg#include <vm/vm_page.h> 539513Sdg#include <vm/vm_pager.h> 549513Sdg#include <vm/swap_pager.h> 559513Sdg 5692727Salfredstatic vm_object_t default_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 57194766Skib vm_ooffset_t, struct ucred *); 5892727Salfredstatic void default_pager_dealloc(vm_object_t); 5992727Salfredstatic int default_pager_getpages(vm_object_t, vm_page_t *, int, int); 6092727Salfredstatic void default_pager_putpages(vm_object_t, vm_page_t *, int, 6192727Salfred boolean_t, int *); 6292727Salfredstatic boolean_t default_pager_haspage(vm_object_t, vm_pindex_t, int *, 6392727Salfred int *); 649513Sdg/* 659513Sdg * pagerops for OBJT_DEFAULT - "default pager". 66310363Skib * 67310363Skib * This pager handles anonymous (no handle) swap-backed memory, just 68310363Skib * like the swap pager. It allows several optimizations based on the 69310363Skib * fact that no pages of a default object can be swapped out. The 70310363Skib * most important optimization is in vm_fault(), where the pager is 71310363Skib * never asked for a non-resident page. Instead, a freshly allocated 72310363Skib * zeroed page is used. 73310363Skib * 74310363Skib * On the first request to page out a page from a default object, the 75310363Skib * object is converted to swap pager type. 769513Sdg */ 779513Sdgstruct pagerops defaultpagerops = { 78118466Sphk .pgo_alloc = default_pager_alloc, 79118466Sphk .pgo_dealloc = default_pager_dealloc, 80118466Sphk .pgo_getpages = default_pager_getpages, 81118466Sphk .pgo_putpages = default_pager_putpages, 82118466Sphk .pgo_haspage = default_pager_haspage, 839513Sdg}; 849513Sdg 859513Sdg/* 869513Sdg * no_pager_alloc just returns an initialized object. 879513Sdg */ 8812820Sphkstatic vm_object_t 8940286Sdgdefault_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 90194766Skib vm_ooffset_t offset, struct ucred *cred) 919513Sdg{ 92194766Skib vm_object_t object; 93194766Skib 949513Sdg if (handle != NULL) 959513Sdg panic("default_pager_alloc: handle specified"); 96194766Skib if (cred != NULL) { 97216128Strasz if (!swap_reserve_by_cred(size, cred)) 98194766Skib return (NULL); 99216128Strasz crhold(cred); 100194766Skib } 101194766Skib object = vm_object_allocate(OBJT_DEFAULT, 102194766Skib OFF_TO_IDX(round_page(offset + size))); 103194766Skib if (cred != NULL) { 104248084Sattilio VM_OBJECT_WLOCK(object); 105216128Strasz object->cred = cred; 106194766Skib object->charge = size; 107248084Sattilio VM_OBJECT_WUNLOCK(object); 108194766Skib } 109194766Skib return (object); 1109513Sdg} 1119513Sdg 11242957Sdillon/* 11342957Sdillon * deallocate resources associated with default objects. The default objects 11442957Sdillon * have no special resources allocated to them, but the vm_page's being used 11542957Sdillon * in this object might. Still, we do not have to do anything - we will free 11642957Sdillon * the swapblk in the underlying vm_page's when we free the vm_page or 11742957Sdillon * garbage collect the vm_page cache list. 11842957Sdillon */ 11912820Sphkstatic void 1209513Sdgdefault_pager_dealloc(object) 1219513Sdg vm_object_t object; 1229513Sdg{ 1239513Sdg /* 1249513Sdg * OBJT_DEFAULT objects have no special resources allocated to them. 1259513Sdg */ 126284100Sjhb object->type = OBJT_DEAD; 1279513Sdg} 1289513Sdg 1299513Sdg/* 13042957Sdillon * Load pages from backing store. Since OBJT_DEFAULT is converted to 13142957Sdillon * OBJT_SWAP at the time a swap-backed vm_page_t is freed, we will never 13242957Sdillon * see a vm_page with assigned swap here. 1339513Sdg */ 13412820Sphkstatic int 1359513Sdgdefault_pager_getpages(object, m, count, reqpage) 1369513Sdg vm_object_t object; 1379513Sdg vm_page_t *m; 1389513Sdg int count; 1399513Sdg int reqpage; 1409513Sdg{ 1419513Sdg return VM_PAGER_FAIL; 1429513Sdg} 1439513Sdg 14442957Sdillon/* 14542957Sdillon * Store pages to backing store. We should assign swap and initiate 14642957Sdillon * I/O. We do not actually convert the object to OBJT_SWAP here. The 14742957Sdillon * object will be converted when the written-out vm_page_t is moved from the 14842957Sdillon * cache to the free list. 14942957Sdillon */ 15043129Sdillonstatic void 151274375Skibdefault_pager_putpages(vm_object_t object, vm_page_t *m, int count, 152274375Skib int flags, int *rtvals) 1539513Sdg{ 154274375Skib 155274375Skib swappagerops.pgo_putpages(object, m, count, flags, rtvals); 1569513Sdg} 1579513Sdg 15842957Sdillon/* 15942957Sdillon * Tell us whether the backing store for the requested (object,index) is 16042957Sdillon * synchronized. i.e. tell us whether we can throw the page away and 16142957Sdillon * reload it later. So, for example, if we are in the process of writing 16242957Sdillon * the page to its backing store, or if no backing store has been assigned, 16342957Sdillon * it is not yet synchronized. 16442957Sdillon * 16542957Sdillon * It is possible to have fully-synchronized swap assigned without the 16642957Sdillon * object having been converted. We just call swap_pager_haspage() to 16742957Sdillon * deal with it since it must already deal with it plus deal with swap 16842957Sdillon * meta-data structures. 16942957Sdillon */ 17012820Sphkstatic boolean_t 17112767Sdysondefault_pager_haspage(object, pindex, before, after) 1729513Sdg vm_object_t object; 17312767Sdyson vm_pindex_t pindex; 1749513Sdg int *before; 1759513Sdg int *after; 1769513Sdg{ 1779513Sdg return FALSE; 1789513Sdg} 17915978Sdyson 180