xref: /freebsd/sys/vm/device_pager.c (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990 University of Utah.
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/lock.h>
41 #include <sys/proc.h>
42 #include <sys/mutex.h>
43 #include <sys/mman.h>
44 #include <sys/rwlock.h>
45 #include <sys/sx.h>
46 #include <sys/user.h>
47 #include <sys/vmmeter.h>
48 
49 #include <vm/vm.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pager.h>
54 #include <vm/vm_phys.h>
55 #include <vm/uma.h>
56 
57 static void dev_pager_init(void);
58 static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
59     vm_ooffset_t, struct ucred *);
60 static void dev_pager_dealloc(vm_object_t);
61 static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
62 static void dev_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
63 static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
64 static void dev_pager_free_page(vm_object_t object, vm_page_t m);
65 static int dev_pager_populate(vm_object_t object, vm_pindex_t pidx,
66     int fault_type, vm_prot_t, vm_pindex_t *first, vm_pindex_t *last);
67 
68 /* list of device pager objects */
69 static struct pagerlst dev_pager_object_list;
70 /* protect list manipulation */
71 static struct mtx dev_pager_mtx;
72 
73 const struct pagerops devicepagerops = {
74 	.pgo_kvme_type = KVME_TYPE_DEVICE,
75 	.pgo_init =	dev_pager_init,
76 	.pgo_alloc =	dev_pager_alloc,
77 	.pgo_dealloc =	dev_pager_dealloc,
78 	.pgo_getpages =	dev_pager_getpages,
79 	.pgo_putpages =	dev_pager_putpages,
80 	.pgo_haspage =	dev_pager_haspage,
81 };
82 
83 const struct pagerops mgtdevicepagerops = {
84 	.pgo_kvme_type = KVME_TYPE_MGTDEVICE,
85 	.pgo_alloc =	dev_pager_alloc,
86 	.pgo_dealloc =	dev_pager_dealloc,
87 	.pgo_getpages =	dev_pager_getpages,
88 	.pgo_putpages =	dev_pager_putpages,
89 	.pgo_haspage =	dev_pager_haspage,
90 	.pgo_populate =	dev_pager_populate,
91 };
92 
93 static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
94     vm_ooffset_t foff, struct ucred *cred, u_short *color);
95 static void old_dev_pager_dtor(void *handle);
96 static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
97     int prot, vm_page_t *mres);
98 
99 static const struct cdev_pager_ops old_dev_pager_ops = {
100 	.cdev_pg_ctor =	old_dev_pager_ctor,
101 	.cdev_pg_dtor =	old_dev_pager_dtor,
102 	.cdev_pg_fault = old_dev_pager_fault
103 };
104 
105 static void
106 dev_pager_init(void)
107 {
108 
109 	TAILQ_INIT(&dev_pager_object_list);
110 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
111 }
112 
113 vm_object_t
114 cdev_pager_lookup(void *handle)
115 {
116 	vm_object_t object;
117 
118 again:
119 	mtx_lock(&dev_pager_mtx);
120 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
121 	if (object != NULL && object->un_pager.devp.dev == NULL) {
122 		msleep(&object->un_pager.devp.dev, &dev_pager_mtx,
123 		    PVM | PDROP, "cdplkp", 0);
124 		vm_object_deallocate(object);
125 		goto again;
126 	}
127 	mtx_unlock(&dev_pager_mtx);
128 	return (object);
129 }
130 
131 vm_object_t
132 cdev_pager_allocate(void *handle, enum obj_type tp,
133     const struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot,
134     vm_ooffset_t foff, struct ucred *cred)
135 {
136 	vm_object_t object;
137 	vm_pindex_t pindex;
138 
139 	if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE)
140 		return (NULL);
141 	KASSERT(tp == OBJT_MGTDEVICE || ops->cdev_pg_populate == NULL,
142 	    ("populate on unmanaged device pager"));
143 
144 	/*
145 	 * Offset should be page aligned.
146 	 */
147 	if (foff & PAGE_MASK)
148 		return (NULL);
149 
150 	/*
151 	 * Treat the mmap(2) file offset as an unsigned value for a
152 	 * device mapping.  This, in effect, allows a user to pass all
153 	 * possible off_t values as the mapping cookie to the driver.  At
154 	 * this point, we know that both foff and size are a multiple
155 	 * of the page size.  Do a check to avoid wrap.
156 	 */
157 	size = round_page(size);
158 	pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
159 	if (pindex > OBJ_MAX_SIZE || pindex < OFF_TO_IDX(foff) ||
160 	    pindex < OFF_TO_IDX(size))
161 		return (NULL);
162 
163 again:
164 	mtx_lock(&dev_pager_mtx);
165 
166 	/*
167 	 * Look up pager, creating as necessary.
168 	 */
169 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
170 	if (object == NULL) {
171 		vm_object_t object1;
172 
173 		/*
174 		 * Allocate object and associate it with the pager.  Initialize
175 		 * the object's pg_color based upon the physical address of the
176 		 * device's memory.
177 		 */
178 		mtx_unlock(&dev_pager_mtx);
179 		object1 = vm_object_allocate(tp, pindex);
180 		mtx_lock(&dev_pager_mtx);
181 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
182 		if (object != NULL) {
183 			object1->type = OBJT_DEAD;
184 			vm_object_deallocate(object1);
185 			object1 = NULL;
186 			if (object->un_pager.devp.dev == NULL) {
187 				msleep(&object->un_pager.devp.dev,
188 				    &dev_pager_mtx, PVM | PDROP, "cdplkp", 0);
189 				vm_object_deallocate(object);
190 				goto again;
191 			}
192 
193 			/*
194 			 * We raced with other thread while allocating object.
195 			 */
196 			if (pindex > object->size)
197 				object->size = pindex;
198 			KASSERT(object->type == tp,
199 			    ("Inconsistent device pager type %p %d",
200 			    object, tp));
201 			KASSERT(object->un_pager.devp.ops == ops,
202 			    ("Inconsistent devops %p %p", object, ops));
203 		} else {
204 			u_short color;
205 
206 			object = object1;
207 			object1 = NULL;
208 			object->handle = handle;
209 			object->un_pager.devp.ops = ops;
210 			TAILQ_INIT(&object->un_pager.devp.devp_pglist);
211 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
212 			    pager_object_list);
213 			mtx_unlock(&dev_pager_mtx);
214 			if (ops->cdev_pg_populate != NULL)
215 				vm_object_set_flag(object, OBJ_POPULATE);
216 			if (ops->cdev_pg_ctor(handle, size, prot, foff,
217 			    cred, &color) != 0) {
218 				mtx_lock(&dev_pager_mtx);
219 				TAILQ_REMOVE(&dev_pager_object_list, object,
220 				    pager_object_list);
221 				wakeup(&object->un_pager.devp.dev);
222 				mtx_unlock(&dev_pager_mtx);
223 				object->type = OBJT_DEAD;
224 				vm_object_deallocate(object);
225 				object = NULL;
226 				mtx_lock(&dev_pager_mtx);
227 			} else {
228 				mtx_lock(&dev_pager_mtx);
229 				object->flags |= OBJ_COLORED;
230 				object->pg_color = color;
231 				object->un_pager.devp.dev = handle;
232 				wakeup(&object->un_pager.devp.dev);
233 			}
234 		}
235 		MPASS(object1 == NULL);
236 	} else {
237 		if (object->un_pager.devp.dev == NULL) {
238 			msleep(&object->un_pager.devp.dev,
239 			    &dev_pager_mtx, PVM | PDROP, "cdplkp", 0);
240 			vm_object_deallocate(object);
241 			goto again;
242 		}
243 		if (pindex > object->size)
244 			object->size = pindex;
245 		KASSERT(object->type == tp,
246 		    ("Inconsistent device pager type %p %d", object, tp));
247 	}
248 	mtx_unlock(&dev_pager_mtx);
249 	return (object);
250 }
251 
252 static vm_object_t
253 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
254     vm_ooffset_t foff, struct ucred *cred)
255 {
256 
257 	return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
258 	    size, prot, foff, cred));
259 }
260 
261 void
262 cdev_pager_free_page(vm_object_t object, vm_page_t m)
263 {
264 
265 	if (object->type == OBJT_MGTDEVICE)
266 		cdev_mgtdev_pager_free_page(object, m);
267 	else if (object->type == OBJT_DEVICE)
268 		dev_pager_free_page(object, m);
269 	else
270 		KASSERT(false,
271 		    ("Invalid device type obj %p m %p", object, m));
272 }
273 
274 void
275 cdev_mgtdev_pager_free_page(vm_object_t object, vm_page_t m)
276 {
277 
278 	VM_OBJECT_ASSERT_WLOCKED(object);
279 	KASSERT((object->type == OBJT_MGTDEVICE &&
280 	    (m->oflags & VPO_UNMANAGED) == 0),
281 	    ("Unmanaged device or page obj %p m %p", object, m));
282 	pmap_remove_all(m);
283 	(void)vm_page_remove(m);
284 }
285 
286 static void
287 dev_pager_free_page(vm_object_t object, vm_page_t m)
288 {
289 
290 	VM_OBJECT_ASSERT_WLOCKED(object);
291 	KASSERT((object->type == OBJT_DEVICE &&
292 	    (m->oflags & VPO_UNMANAGED) != 0),
293 	    ("Managed device or page obj %p m %p", object, m));
294 	TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q);
295 	vm_page_putfake(m);
296 }
297 
298 static void
299 dev_pager_dealloc(vm_object_t object)
300 {
301 	vm_page_t m;
302 
303 	VM_OBJECT_WUNLOCK(object);
304 	object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev);
305 
306 	mtx_lock(&dev_pager_mtx);
307 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
308 	mtx_unlock(&dev_pager_mtx);
309 	VM_OBJECT_WLOCK(object);
310 
311 	if (object->type == OBJT_DEVICE) {
312 		/*
313 		 * Free up our fake pages.
314 		 */
315 		while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist))
316 		    != NULL) {
317 			if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0)
318 				continue;
319 
320 			dev_pager_free_page(object, m);
321 		}
322 	}
323 	object->handle = NULL;
324 	object->type = OBJT_DEAD;
325 }
326 
327 static int
328 dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
329     int *rahead)
330 {
331 	int error;
332 
333 	/* Since our haspage reports zero after/before, the count is 1. */
334 	KASSERT(count == 1, ("%s: count %d", __func__, count));
335 	if (object->un_pager.devp.ops->cdev_pg_fault == NULL)
336 		return (VM_PAGER_FAIL);
337 	VM_OBJECT_WLOCK(object);
338 	error = object->un_pager.devp.ops->cdev_pg_fault(object,
339 	    IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]);
340 
341 	VM_OBJECT_ASSERT_WLOCKED(object);
342 
343 	if (error == VM_PAGER_OK) {
344 		KASSERT((object->type == OBJT_DEVICE &&
345 		     (ma[0]->oflags & VPO_UNMANAGED) != 0) ||
346 		    (object->type == OBJT_MGTDEVICE &&
347 		     (ma[0]->oflags & VPO_UNMANAGED) == 0),
348 		    ("Wrong page type %p %p", ma[0], object));
349 		if (object->type == OBJT_DEVICE) {
350 			TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
351 			    ma[0], plinks.q);
352 		}
353 		if (rbehind)
354 			*rbehind = 0;
355 		if (rahead)
356 			*rahead = 0;
357 	}
358 	VM_OBJECT_WUNLOCK(object);
359 
360 	return (error);
361 }
362 
363 static int
364 dev_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
365     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
366 {
367 
368 	VM_OBJECT_ASSERT_WLOCKED(object);
369 	if (object->un_pager.devp.ops->cdev_pg_populate == NULL)
370 		return (VM_PAGER_FAIL);
371 	return (object->un_pager.devp.ops->cdev_pg_populate(object, pidx,
372 	    fault_type, max_prot, first, last));
373 }
374 
375 static int
376 old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
377     vm_page_t *mres)
378 {
379 	vm_paddr_t paddr;
380 	vm_page_t m_paddr, page;
381 	struct cdev *dev;
382 	struct cdevsw *csw;
383 	struct file *fpop;
384 	struct thread *td;
385 	vm_memattr_t memattr, memattr1;
386 	int ref, ret;
387 
388 	memattr = object->memattr;
389 
390 	VM_OBJECT_WUNLOCK(object);
391 
392 	dev = object->handle;
393 	csw = dev_refthread(dev, &ref);
394 	if (csw == NULL) {
395 		VM_OBJECT_WLOCK(object);
396 		return (VM_PAGER_FAIL);
397 	}
398 	td = curthread;
399 	fpop = td->td_fpop;
400 	td->td_fpop = NULL;
401 	ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
402 	td->td_fpop = fpop;
403 	dev_relthread(dev, ref);
404 	if (ret != 0) {
405 		printf(
406 	    "WARNING: dev_pager_getpage: map function returns error %d", ret);
407 		VM_OBJECT_WLOCK(object);
408 		return (VM_PAGER_FAIL);
409 	}
410 
411 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
412 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
413 	    (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) {
414 		/*
415 		 * For the /dev/mem d_mmap routine to return the
416 		 * correct memattr, pmap_page_get_memattr() needs to
417 		 * be called, which we do there.
418 		 */
419 		if ((csw->d_flags & D_MEM) == 0) {
420 			printf("WARNING: Device driver %s has set "
421 			    "\"memattr\" inconsistently (drv %u pmap %u).\n",
422 			    csw->d_name, memattr, memattr1);
423 		}
424 		memattr = memattr1;
425 	}
426 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
427 		/*
428 		 * If the passed in result page is a fake page, update it with
429 		 * the new physical address.
430 		 */
431 		page = *mres;
432 		VM_OBJECT_WLOCK(object);
433 		vm_page_updatefake(page, paddr, memattr);
434 	} else {
435 		/*
436 		 * Replace the passed in reqpage page with our own fake page and
437 		 * free up the all of the original pages.
438 		 */
439 		page = vm_page_getfake(paddr, memattr);
440 		VM_OBJECT_WLOCK(object);
441 		vm_page_replace(page, object, (*mres)->pindex, *mres);
442 		*mres = page;
443 	}
444 	vm_page_valid(page);
445 	return (VM_PAGER_OK);
446 }
447 
448 static void
449 dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
450     int *rtvals)
451 {
452 
453 	panic("dev_pager_putpage called");
454 }
455 
456 static boolean_t
457 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
458     int *after)
459 {
460 
461 	if (before != NULL)
462 		*before = 0;
463 	if (after != NULL)
464 		*after = 0;
465 	return (TRUE);
466 }
467 
468 static int
469 old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
470     vm_ooffset_t foff, struct ucred *cred, u_short *color)
471 {
472 	struct cdev *dev;
473 	struct cdevsw *csw;
474 	vm_memattr_t dummy;
475 	vm_ooffset_t off;
476 	vm_paddr_t paddr;
477 	unsigned int npages;
478 	int ref;
479 
480 	/*
481 	 * Make sure this device can be mapped.
482 	 */
483 	dev = handle;
484 	csw = dev_refthread(dev, &ref);
485 	if (csw == NULL)
486 		return (ENXIO);
487 
488 	/*
489 	 * Check that the specified range of the device allows the desired
490 	 * protection.
491 	 *
492 	 * XXX assumes VM_PROT_* == PROT_*
493 	 */
494 	npages = OFF_TO_IDX(size);
495 	paddr = 0; /* Make paddr initialized for the case of size == 0. */
496 	for (off = foff; npages--; off += PAGE_SIZE) {
497 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
498 			dev_relthread(dev, ref);
499 			return (EINVAL);
500 		}
501 	}
502 
503 	dev_ref(dev);
504 	dev_relthread(dev, ref);
505 	*color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
506 	return (0);
507 }
508 
509 static void
510 old_dev_pager_dtor(void *handle)
511 {
512 
513 	dev_rel(handle);
514 }
515