xref: /freebsd/sys/vm/sg_pager.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Hudson River Trading LLC
5  * Written by: John H. Baldwin <jhb@FreeBSD.org>
6  * All rights reserved.
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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * This pager manages OBJT_SG objects.  These objects are backed by
35  * a scatter/gather list of physical address ranges.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/rwlock.h>
42 #include <sys/sglist.h>
43 #include <sys/user.h>
44 #include <sys/vmmeter.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_pager.h>
51 #include <vm/vm_phys.h>
52 #include <vm/uma.h>
53 
54 static vm_object_t sg_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
55     vm_ooffset_t, struct ucred *);
56 static void sg_pager_dealloc(vm_object_t);
57 static int sg_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
58 static void sg_pager_putpages(vm_object_t, vm_page_t *, int,
59 		boolean_t, int *);
60 static boolean_t sg_pager_haspage(vm_object_t, vm_pindex_t, int *,
61 		int *);
62 
63 const struct pagerops sgpagerops = {
64 	.pgo_kvme_type = KVME_TYPE_SG,
65 	.pgo_alloc =	sg_pager_alloc,
66 	.pgo_dealloc =	sg_pager_dealloc,
67 	.pgo_getpages =	sg_pager_getpages,
68 	.pgo_putpages =	sg_pager_putpages,
69 	.pgo_haspage =	sg_pager_haspage,
70 };
71 
72 static vm_object_t
73 sg_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
74     vm_ooffset_t foff, struct ucred *cred)
75 {
76 	struct sglist *sg;
77 	vm_object_t object;
78 	vm_pindex_t npages, pindex;
79 	int i;
80 
81 	/*
82 	 * Offset should be page aligned.
83 	 */
84 	if (foff & PAGE_MASK)
85 		return (NULL);
86 
87 	/*
88 	 * The scatter/gather list must only include page-aligned
89 	 * ranges.
90 	 */
91 	npages = 0;
92 	sg = handle;
93 	for (i = 0; i < sg->sg_nseg; i++) {
94 		if ((sg->sg_segs[i].ss_paddr % PAGE_SIZE) != 0 ||
95 		    (sg->sg_segs[i].ss_len % PAGE_SIZE) != 0)
96 			return (NULL);
97 		npages += sg->sg_segs[i].ss_len / PAGE_SIZE;
98 	}
99 
100 	/*
101 	 * The scatter/gather list has a fixed size.  Refuse requests
102 	 * to map beyond that.
103 	 */
104 	size = round_page(size);
105 	pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
106 	if (pindex > npages || pindex < OFF_TO_IDX(foff) ||
107 	    pindex < OFF_TO_IDX(size))
108 		return (NULL);
109 
110 	/*
111 	 * Allocate a new object and associate it with the
112 	 * scatter/gather list.  It is ok for our purposes to have
113 	 * multiple VM objects associated with the same scatter/gather
114 	 * list because scatter/gather lists are static.  This is also
115 	 * simpler than ensuring a unique object per scatter/gather
116 	 * list.
117 	 */
118 	object = vm_object_allocate(OBJT_SG, npages);
119 	object->handle = sglist_hold(sg);
120 	TAILQ_INIT(&object->un_pager.sgp.sgp_pglist);
121 	return (object);
122 }
123 
124 static void
125 sg_pager_dealloc(vm_object_t object)
126 {
127 	struct sglist *sg;
128 	vm_page_t m;
129 
130 	/*
131 	 * Free up our fake pages.
132 	 */
133 	while ((m = TAILQ_FIRST(&object->un_pager.sgp.sgp_pglist)) != 0) {
134 		if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0)
135 			continue;
136 		TAILQ_REMOVE(&object->un_pager.sgp.sgp_pglist, m, plinks.q);
137 		vm_page_putfake(m);
138 	}
139 
140 	sg = object->handle;
141 	sglist_free(sg);
142 	object->handle = NULL;
143 	object->type = OBJT_DEAD;
144 }
145 
146 static int
147 sg_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
148     int *rahead)
149 {
150 	struct sglist *sg;
151 	vm_page_t m_paddr, page;
152 	vm_pindex_t offset;
153 	vm_paddr_t paddr;
154 	vm_memattr_t memattr;
155 	size_t space;
156 	int i;
157 
158 	/* Since our haspage reports zero after/before, the count is 1. */
159 	KASSERT(count == 1, ("%s: count %d", __func__, count));
160 	/* Handle is stable while paging is in progress. */
161 	sg = object->handle;
162 	memattr = object->memattr;
163 	offset = m[0]->pindex;
164 
165 	/*
166 	 * Lookup the physical address of the requested page.  An initial
167 	 * value of '1' instead of '0' is used so we can assert that the
168 	 * page is found since '0' can be a valid page-aligned physical
169 	 * address.
170 	 */
171 	space = 0;
172 	paddr = 1;
173 	for (i = 0; i < sg->sg_nseg; i++) {
174 		if (space + sg->sg_segs[i].ss_len <= (offset * PAGE_SIZE)) {
175 			space += sg->sg_segs[i].ss_len;
176 			continue;
177 		}
178 		paddr = sg->sg_segs[i].ss_paddr + offset * PAGE_SIZE - space;
179 		break;
180 	}
181 	KASSERT(paddr != 1, ("invalid SG page index"));
182 
183 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
184 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
185 	    pmap_page_get_memattr(m_paddr) != memattr) {
186 		memattr = pmap_page_get_memattr(m_paddr);
187 		printf(
188 	    "WARNING: A device driver has set \"memattr\" inconsistently.\n");
189 	}
190 
191 	/* Return a fake page for the requested page. */
192 	KASSERT(!(m[0]->flags & PG_FICTITIOUS),
193 	    ("backing page for SG is fake"));
194 
195 	/* Construct a new fake page. */
196 	page = vm_page_getfake(paddr, memattr);
197 	VM_OBJECT_WLOCK(object);
198 	TAILQ_INSERT_TAIL(&object->un_pager.sgp.sgp_pglist, page, plinks.q);
199 	vm_page_replace(page, object, offset, m[0]);
200 	VM_OBJECT_WUNLOCK(object);
201 	m[0] = page;
202 	vm_page_valid(page);
203 
204 	if (rbehind)
205 		*rbehind = 0;
206 	if (rahead)
207 		*rahead = 0;
208 
209 	return (VM_PAGER_OK);
210 }
211 
212 static void
213 sg_pager_putpages(vm_object_t object, vm_page_t *m, int count,
214     boolean_t sync, int *rtvals)
215 {
216 
217 	panic("sg_pager_putpage called");
218 }
219 
220 static boolean_t
221 sg_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
222     int *after)
223 {
224 
225 	if (before != NULL)
226 		*before = 0;
227 	if (after != NULL)
228 		*after = 0;
229 	return (TRUE);
230 }
231