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