1/*- 2 * Copyright (c) 1995, David Greenman 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by David Greenman. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: stable/11/sys/vm/default_pager.c 315473 2017-03-18 05:38:10Z alc $"); 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/lock.h> 38#include <sys/proc.h> 39#include <sys/resourcevar.h> 40#include <sys/rwlock.h> 41 42#include <vm/vm.h> 43#include <vm/vm_object.h> 44#include <vm/vm_page.h> 45#include <vm/vm_pager.h> 46#include <vm/swap_pager.h> 47 48static vm_object_t default_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 49 vm_ooffset_t, struct ucred *); 50static void default_pager_dealloc(vm_object_t); 51static int default_pager_getpages(vm_object_t, vm_page_t *, int, 52 int *, int *); 53static void default_pager_putpages(vm_object_t, vm_page_t *, int, 54 boolean_t, int *); 55static boolean_t default_pager_haspage(vm_object_t, vm_pindex_t, int *, 56 int *); 57 58/* 59 * pagerops for OBJT_DEFAULT - "default pager". 60 * 61 * This pager handles anonymous (no handle) swap-backed memory, just 62 * like the swap pager. It allows several optimizations based on the 63 * fact that no pages of a default object can be swapped out. The 64 * most important optimization is in vm_fault(), where the pager is 65 * never asked for a non-resident page. Instead, a freshly allocated 66 * zeroed page is used. 67 * 68 * On the first request to page out a page from a default object, the 69 * object is converted to swap pager type. 70 */ 71struct pagerops defaultpagerops = { 72 .pgo_alloc = default_pager_alloc, 73 .pgo_dealloc = default_pager_dealloc, 74 .pgo_getpages = default_pager_getpages, 75 .pgo_putpages = default_pager_putpages, 76 .pgo_haspage = default_pager_haspage, 77}; 78 79/* 80 * Return an initialized object. 81 */ 82static vm_object_t 83default_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 84 vm_ooffset_t offset, struct ucred *cred) 85{ 86 vm_object_t object; 87 88 if (handle != NULL) 89 panic("default_pager_alloc: handle specified"); 90 if (cred != NULL) { 91 if (!swap_reserve_by_cred(size, cred)) 92 return (NULL); 93 crhold(cred); 94 } 95 object = vm_object_allocate(OBJT_DEFAULT, 96 OFF_TO_IDX(round_page(offset + size))); 97 if (cred != NULL) { 98 object->cred = cred; 99 object->charge = size; 100 } 101 return (object); 102} 103 104/* 105 * Deallocate resources associated with the object. 106 */ 107static void 108default_pager_dealloc(vm_object_t object) 109{ 110 111 /* Reserved swap is released by vm_object_destroy(). */ 112 object->type = OBJT_DEAD; 113} 114 115/* 116 * Load pages from backing store. 117 */ 118static int 119default_pager_getpages(vm_object_t object, vm_page_t *m, int count, 120 int *rbehind, int *rahead) 121{ 122 123 /* 124 * Since an OBJT_DEFAULT object is converted to OBJT_SWAP by the first 125 * call to the putpages method, this function will never be called on 126 * a vm_page with assigned swap. 127 */ 128 return (VM_PAGER_FAIL); 129} 130 131/* 132 * Store pages to backing store. 133 */ 134static void 135default_pager_putpages(vm_object_t object, vm_page_t *m, int count, 136 int flags, int *rtvals) 137{ 138 139 /* The swap pager will convert the object to OBJT_SWAP. */ 140 swappagerops.pgo_putpages(object, m, count, flags, rtvals); 141} 142 143/* 144 * Tell us whether the requested (object,index) is available from the object's 145 * backing store. 146 */ 147static boolean_t 148default_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 149 int *after) 150{ 151 152 /* An OBJT_DEFAULT object has no backing store. */ 153 return (FALSE); 154} 155 156