xref: /freebsd/sys/vm/device_pager.c (revision 38e3125d6df98919983ed800471673ff0e5c2337)
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/vm_radix.h>
56 #include <vm/uma.h>
57 
58 static void dev_pager_init(void);
59 static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
60     vm_ooffset_t, struct ucred *);
61 static void dev_pager_dealloc(vm_object_t);
62 static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
63 static void dev_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
64 static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
65 static void dev_pager_free_page(vm_object_t object, vm_page_t m);
66 static int dev_pager_populate(vm_object_t object, vm_pindex_t pidx,
67     int fault_type, vm_prot_t, vm_pindex_t *first, vm_pindex_t *last);
68 
69 /* list of device pager objects */
70 static struct pagerlst dev_pager_object_list;
71 /* protect list manipulation */
72 static struct mtx dev_pager_mtx;
73 
74 const struct pagerops devicepagerops = {
75 	.pgo_kvme_type = KVME_TYPE_DEVICE,
76 	.pgo_init =	dev_pager_init,
77 	.pgo_alloc =	dev_pager_alloc,
78 	.pgo_dealloc =	dev_pager_dealloc,
79 	.pgo_getpages =	dev_pager_getpages,
80 	.pgo_putpages =	dev_pager_putpages,
81 	.pgo_haspage =	dev_pager_haspage,
82 };
83 
84 const struct pagerops mgtdevicepagerops = {
85 	.pgo_kvme_type = KVME_TYPE_MGTDEVICE,
86 	.pgo_alloc =	dev_pager_alloc,
87 	.pgo_dealloc =	dev_pager_dealloc,
88 	.pgo_getpages =	dev_pager_getpages,
89 	.pgo_putpages =	dev_pager_putpages,
90 	.pgo_haspage =	dev_pager_haspage,
91 	.pgo_populate =	dev_pager_populate,
92 };
93 
94 static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
95     vm_ooffset_t foff, struct ucred *cred, u_short *color);
96 static void old_dev_pager_dtor(void *handle);
97 static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
98     int prot, vm_page_t *mres);
99 
100 static const struct cdev_pager_ops old_dev_pager_ops = {
101 	.cdev_pg_ctor =	old_dev_pager_ctor,
102 	.cdev_pg_dtor =	old_dev_pager_dtor,
103 	.cdev_pg_fault = old_dev_pager_fault
104 };
105 
106 static void
dev_pager_init(void)107 dev_pager_init(void)
108 {
109 
110 	TAILQ_INIT(&dev_pager_object_list);
111 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
112 }
113 
114 vm_object_t
cdev_pager_lookup(void * handle)115 cdev_pager_lookup(void *handle)
116 {
117 	vm_object_t object;
118 
119 again:
120 	mtx_lock(&dev_pager_mtx);
121 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
122 	if (object != NULL && object->un_pager.devp.handle == NULL) {
123 		msleep(&object->un_pager.devp.handle, &dev_pager_mtx,
124 		    PVM | PDROP, "cdplkp", 0);
125 		vm_object_deallocate(object);
126 		goto again;
127 	}
128 	mtx_unlock(&dev_pager_mtx);
129 	return (object);
130 }
131 
132 vm_object_t
cdev_pager_allocate(void * handle,enum obj_type tp,const struct cdev_pager_ops * ops,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred)133 cdev_pager_allocate(void *handle, enum obj_type tp,
134     const struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot,
135     vm_ooffset_t foff, struct ucred *cred)
136 {
137 	vm_object_t object;
138 	vm_pindex_t pindex;
139 
140 	if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE)
141 		return (NULL);
142 	KASSERT(tp == OBJT_MGTDEVICE || ops->cdev_pg_populate == NULL,
143 	    ("populate on unmanaged device pager"));
144 
145 	/*
146 	 * Offset should be page aligned.
147 	 */
148 	if (foff & PAGE_MASK)
149 		return (NULL);
150 
151 	/*
152 	 * Treat the mmap(2) file offset as an unsigned value for a
153 	 * device mapping.  This, in effect, allows a user to pass all
154 	 * possible off_t values as the mapping cookie to the driver.  At
155 	 * this point, we know that both foff and size are a multiple
156 	 * of the page size.  Do a check to avoid wrap.
157 	 */
158 	size = round_page(size);
159 	pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
160 	if (pindex > OBJ_MAX_SIZE || pindex < OFF_TO_IDX(foff) ||
161 	    pindex < OFF_TO_IDX(size))
162 		return (NULL);
163 
164 again:
165 	mtx_lock(&dev_pager_mtx);
166 
167 	/*
168 	 * Look up pager, creating as necessary.
169 	 */
170 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
171 	if (object == NULL) {
172 		vm_object_t object1;
173 
174 		/*
175 		 * Allocate object and associate it with the pager.  Initialize
176 		 * the object's pg_color based upon the physical address of the
177 		 * device's memory.
178 		 */
179 		mtx_unlock(&dev_pager_mtx);
180 		object1 = vm_object_allocate(tp, pindex);
181 		mtx_lock(&dev_pager_mtx);
182 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
183 		if (object != NULL) {
184 			object1->type = OBJT_DEAD;
185 			vm_object_deallocate(object1);
186 			object1 = NULL;
187 			if (object->un_pager.devp.handle == NULL) {
188 				msleep(&object->un_pager.devp.handle,
189 				    &dev_pager_mtx, PVM | PDROP, "cdplkp", 0);
190 				vm_object_deallocate(object);
191 				goto again;
192 			}
193 
194 			/*
195 			 * We raced with other thread while allocating object.
196 			 */
197 			if (pindex > object->size)
198 				object->size = pindex;
199 			KASSERT(object->type == tp,
200 			    ("Inconsistent device pager type %p %d",
201 			    object, tp));
202 			KASSERT(object->un_pager.devp.ops == ops,
203 			    ("Inconsistent devops %p %p", object, ops));
204 		} else {
205 			u_short color;
206 
207 			object = object1;
208 			object1 = NULL;
209 			object->handle = handle;
210 			object->un_pager.devp.ops = ops;
211 			TAILQ_INIT(&object->un_pager.devp.devp_pglist);
212 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
213 			    pager_object_list);
214 			mtx_unlock(&dev_pager_mtx);
215 			if (ops->cdev_pg_populate != NULL)
216 				vm_object_set_flag(object, OBJ_POPULATE);
217 			if (ops->cdev_pg_ctor(handle, size, prot, foff,
218 			    cred, &color) != 0) {
219 				mtx_lock(&dev_pager_mtx);
220 				TAILQ_REMOVE(&dev_pager_object_list, object,
221 				    pager_object_list);
222 				wakeup(&object->un_pager.devp.handle);
223 				mtx_unlock(&dev_pager_mtx);
224 				object->type = OBJT_DEAD;
225 				vm_object_deallocate(object);
226 				object = NULL;
227 				mtx_lock(&dev_pager_mtx);
228 			} else {
229 				mtx_lock(&dev_pager_mtx);
230 				object->flags |= OBJ_COLORED;
231 				object->pg_color = color;
232 				object->un_pager.devp.handle = handle;
233 				wakeup(&object->un_pager.devp.handle);
234 			}
235 		}
236 		MPASS(object1 == NULL);
237 	} else {
238 		if (object->un_pager.devp.handle == NULL) {
239 			msleep(&object->un_pager.devp.handle,
240 			    &dev_pager_mtx, PVM | PDROP, "cdplkp", 0);
241 			vm_object_deallocate(object);
242 			goto again;
243 		}
244 		if (pindex > object->size)
245 			object->size = pindex;
246 		KASSERT(object->type == tp,
247 		    ("Inconsistent device pager type %p %d", object, tp));
248 	}
249 	mtx_unlock(&dev_pager_mtx);
250 	return (object);
251 }
252 
253 static vm_object_t
dev_pager_alloc(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred)254 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
255     vm_ooffset_t foff, struct ucred *cred)
256 {
257 
258 	return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
259 	    size, prot, foff, cred));
260 }
261 
262 void
cdev_pager_free_page(vm_object_t object,vm_page_t m)263 cdev_pager_free_page(vm_object_t object, vm_page_t m)
264 {
265 
266 	if (object->type == OBJT_MGTDEVICE) {
267 		struct pctrie_iter pages;
268 
269 		vm_page_iter_init(&pages, object);
270 		vm_page_iter_lookup(&pages, m->pindex);
271 		cdev_mgtdev_pager_free_page(&pages);
272 	} else if (object->type == OBJT_DEVICE)
273 		dev_pager_free_page(object, m);
274 	else
275 		KASSERT(false,
276 		    ("Invalid device type obj %p m %p", object, m));
277 }
278 
279 void
cdev_mgtdev_pager_free_page(struct pctrie_iter * pages)280 cdev_mgtdev_pager_free_page(struct pctrie_iter *pages)
281 {
282 	pmap_remove_all(vm_radix_iter_page(pages));
283 	vm_page_iter_remove(pages);
284 }
285 
286 void
cdev_mgtdev_pager_free_pages(vm_object_t object)287 cdev_mgtdev_pager_free_pages(vm_object_t object)
288 {
289 	struct pctrie_iter pages;
290 	vm_page_t m;
291 
292 	vm_page_iter_init(&pages, object);
293 	VM_OBJECT_WLOCK(object);
294 retry:
295 	for (m = vm_page_iter_lookup_ge(&pages, 0); m != NULL;
296 	    m = vm_radix_iter_step(&pages)) {
297 		if (!vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL)) {
298 			pctrie_iter_reset(&pages);
299 			goto retry;
300 		}
301 		cdev_mgtdev_pager_free_page(&pages);
302 	}
303 	VM_OBJECT_WUNLOCK(object);
304 }
305 
306 static void
dev_pager_free_page(vm_object_t object,vm_page_t m)307 dev_pager_free_page(vm_object_t object, vm_page_t m)
308 {
309 
310 	VM_OBJECT_ASSERT_WLOCKED(object);
311 	KASSERT((object->type == OBJT_DEVICE &&
312 	    (m->oflags & VPO_UNMANAGED) != 0),
313 	    ("Managed device or page obj %p m %p", object, m));
314 	TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q);
315 	vm_page_putfake(m);
316 }
317 
318 static void
dev_pager_dealloc(vm_object_t object)319 dev_pager_dealloc(vm_object_t object)
320 {
321 	vm_page_t m;
322 
323 	VM_OBJECT_WUNLOCK(object);
324 	object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.handle);
325 
326 	mtx_lock(&dev_pager_mtx);
327 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
328 	mtx_unlock(&dev_pager_mtx);
329 	VM_OBJECT_WLOCK(object);
330 
331 	if (object->type == OBJT_DEVICE) {
332 		/*
333 		 * Free up our fake pages.
334 		 */
335 		while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist))
336 		    != NULL) {
337 			if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0)
338 				continue;
339 
340 			dev_pager_free_page(object, m);
341 		}
342 	}
343 	object->handle = NULL;
344 	object->type = OBJT_DEAD;
345 }
346 
347 static int
dev_pager_getpages(vm_object_t object,vm_page_t * ma,int count,int * rbehind,int * rahead)348 dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
349     int *rahead)
350 {
351 	int error;
352 
353 	/* Since our haspage reports zero after/before, the count is 1. */
354 	KASSERT(count == 1, ("%s: count %d", __func__, count));
355 	if (object->un_pager.devp.ops->cdev_pg_fault == NULL)
356 		return (VM_PAGER_FAIL);
357 	VM_OBJECT_WLOCK(object);
358 	error = object->un_pager.devp.ops->cdev_pg_fault(object,
359 	    IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]);
360 
361 	VM_OBJECT_ASSERT_WLOCKED(object);
362 
363 	if (error == VM_PAGER_OK) {
364 		KASSERT((object->type == OBJT_DEVICE &&
365 		     (ma[0]->oflags & VPO_UNMANAGED) != 0) ||
366 		    (object->type == OBJT_MGTDEVICE &&
367 		     (ma[0]->oflags & VPO_UNMANAGED) == 0),
368 		    ("Wrong page type %p %p", ma[0], object));
369 		if (object->type == OBJT_DEVICE) {
370 			TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
371 			    ma[0], plinks.q);
372 		}
373 		if (rbehind)
374 			*rbehind = 0;
375 		if (rahead)
376 			*rahead = 0;
377 	}
378 	VM_OBJECT_WUNLOCK(object);
379 
380 	return (error);
381 }
382 
383 static int
dev_pager_populate(vm_object_t object,vm_pindex_t pidx,int fault_type,vm_prot_t max_prot,vm_pindex_t * first,vm_pindex_t * last)384 dev_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
385     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
386 {
387 
388 	VM_OBJECT_ASSERT_WLOCKED(object);
389 	if (object->un_pager.devp.ops->cdev_pg_populate == NULL)
390 		return (VM_PAGER_FAIL);
391 	return (object->un_pager.devp.ops->cdev_pg_populate(object, pidx,
392 	    fault_type, max_prot, first, last));
393 }
394 
395 static int
old_dev_pager_fault(vm_object_t object,vm_ooffset_t offset,int prot,vm_page_t * mres)396 old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
397     vm_page_t *mres)
398 {
399 	vm_paddr_t paddr;
400 	vm_page_t m_paddr, page;
401 	struct cdev *dev;
402 	struct cdevsw *csw;
403 	struct file *fpop;
404 	struct thread *td;
405 	vm_memattr_t memattr, memattr1;
406 	int ref, ret;
407 
408 	memattr = object->memattr;
409 
410 	VM_OBJECT_WUNLOCK(object);
411 
412 	dev = object->handle;
413 	csw = dev_refthread(dev, &ref);
414 	if (csw == NULL) {
415 		VM_OBJECT_WLOCK(object);
416 		return (VM_PAGER_FAIL);
417 	}
418 	td = curthread;
419 	fpop = td->td_fpop;
420 	td->td_fpop = NULL;
421 	ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
422 	td->td_fpop = fpop;
423 	dev_relthread(dev, ref);
424 	if (ret != 0) {
425 		printf(
426 	    "WARNING: dev_pager_getpage: map function returns error %d", ret);
427 		VM_OBJECT_WLOCK(object);
428 		return (VM_PAGER_FAIL);
429 	}
430 
431 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
432 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
433 	    (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) {
434 		/*
435 		 * For the /dev/mem d_mmap routine to return the
436 		 * correct memattr, pmap_page_get_memattr() needs to
437 		 * be called, which we do there.
438 		 */
439 		if ((csw->d_flags & D_MEM) == 0) {
440 			printf("WARNING: Device driver %s has set "
441 			    "\"memattr\" inconsistently (drv %u pmap %u).\n",
442 			    csw->d_name, memattr, memattr1);
443 		}
444 		memattr = memattr1;
445 	}
446 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
447 		/*
448 		 * If the passed in result page is a fake page, update it with
449 		 * the new physical address.
450 		 */
451 		page = *mres;
452 		VM_OBJECT_WLOCK(object);
453 		vm_page_updatefake(page, paddr, memattr);
454 	} else {
455 		/*
456 		 * Replace the passed in reqpage page with our own fake page and
457 		 * free up the all of the original pages.
458 		 */
459 		page = vm_page_getfake(paddr, memattr);
460 		VM_OBJECT_WLOCK(object);
461 		vm_page_replace(page, object, (*mres)->pindex, *mres);
462 		*mres = page;
463 	}
464 	vm_page_valid(page);
465 	return (VM_PAGER_OK);
466 }
467 
468 static void
dev_pager_putpages(vm_object_t object,vm_page_t * m,int count,int flags,int * rtvals)469 dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
470     int *rtvals)
471 {
472 
473 	panic("dev_pager_putpage called");
474 }
475 
476 static boolean_t
dev_pager_haspage(vm_object_t object,vm_pindex_t pindex,int * before,int * after)477 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
478     int *after)
479 {
480 
481 	if (before != NULL)
482 		*before = 0;
483 	if (after != NULL)
484 		*after = 0;
485 	return (TRUE);
486 }
487 
488 static int
old_dev_pager_ctor(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred,u_short * color)489 old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
490     vm_ooffset_t foff, struct ucred *cred, u_short *color)
491 {
492 	struct cdev *dev;
493 	struct cdevsw *csw;
494 	vm_memattr_t dummy;
495 	vm_ooffset_t off;
496 	vm_paddr_t paddr;
497 	unsigned int npages;
498 	int ref;
499 
500 	/*
501 	 * Make sure this device can be mapped.
502 	 */
503 	dev = handle;
504 	csw = dev_refthread(dev, &ref);
505 	if (csw == NULL)
506 		return (ENXIO);
507 
508 	/*
509 	 * Check that the specified range of the device allows the desired
510 	 * protection.
511 	 *
512 	 * XXX assumes VM_PROT_* == PROT_*
513 	 */
514 	npages = OFF_TO_IDX(size);
515 	paddr = 0; /* Make paddr initialized for the case of size == 0. */
516 	for (off = foff; npages--; off += PAGE_SIZE) {
517 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
518 			dev_relthread(dev, ref);
519 			return (EINVAL);
520 		}
521 	}
522 
523 	dev_ref(dev);
524 	dev_relthread(dev, ref);
525 	*color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
526 	return (0);
527 }
528 
529 static void
old_dev_pager_dtor(void * handle)530 old_dev_pager_dtor(void *handle)
531 {
532 
533 	dev_rel(handle);
534 }
535