1 /*-
2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
3 *
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: @(#)vm_pager.c 8.6 (Berkeley) 1/12/94
35 *
36 *
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
39 *
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41 *
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
47 *
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51 *
52 * Carnegie Mellon requests users of this software to return to
53 *
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
58 *
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
61 */
62
63 /*
64 * Paging space routine stubs. Emulates a matchmaker-like interface
65 * for builtin pagers.
66 */
67
68 #include <sys/cdefs.h>
69 #include "opt_param.h"
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/vnode.h>
75 #include <sys/bio.h>
76 #include <sys/buf.h>
77 #include <sys/ucred.h>
78 #include <sys/malloc.h>
79 #include <sys/rwlock.h>
80 #include <sys/user.h>
81
82 #include <vm/vm.h>
83 #include <vm/vm_param.h>
84 #include <vm/vm_kern.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_pager.h>
88 #include <vm/vm_extern.h>
89 #include <vm/uma.h>
90
91 uma_zone_t pbuf_zone;
92 static int pbuf_init(void *, int, int);
93 static int pbuf_ctor(void *, int, void *, int);
94 static void pbuf_dtor(void *, int, void *);
95
96 static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
97 static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
98 vm_ooffset_t, struct ucred *);
99 static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
100 static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
101 static void dead_pager_dealloc(vm_object_t);
102 static void dead_pager_getvp(vm_object_t, struct vnode **, bool *);
103
104 static int
dead_pager_getpages(vm_object_t obj,vm_page_t * ma,int count,int * rbehind,int * rahead)105 dead_pager_getpages(vm_object_t obj, vm_page_t *ma, int count, int *rbehind,
106 int *rahead)
107 {
108
109 return (VM_PAGER_FAIL);
110 }
111
112 static vm_object_t
dead_pager_alloc(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t off,struct ucred * cred)113 dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
114 vm_ooffset_t off, struct ucred *cred)
115 {
116
117 return (NULL);
118 }
119
120 static void
dead_pager_putpages(vm_object_t object,vm_page_t * m,int count,int flags,int * rtvals)121 dead_pager_putpages(vm_object_t object, vm_page_t *m, int count,
122 int flags, int *rtvals)
123 {
124 int i;
125
126 for (i = 0; i < count; i++)
127 rtvals[i] = VM_PAGER_AGAIN;
128 }
129
130 static boolean_t
dead_pager_haspage(vm_object_t object,vm_pindex_t pindex,int * prev,int * next)131 dead_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *prev, int *next)
132 {
133
134 if (prev != NULL)
135 *prev = 0;
136 if (next != NULL)
137 *next = 0;
138 return (FALSE);
139 }
140
141 static void
dead_pager_dealloc(vm_object_t object)142 dead_pager_dealloc(vm_object_t object)
143 {
144
145 }
146
147 static void
dead_pager_getvp(vm_object_t object,struct vnode ** vpp,bool * vp_heldp)148 dead_pager_getvp(vm_object_t object, struct vnode **vpp, bool *vp_heldp)
149 {
150 /*
151 * For OBJT_DEAD objects, v_writecount was handled in
152 * vnode_pager_dealloc().
153 */
154 }
155
156 static const struct pagerops deadpagerops = {
157 .pgo_kvme_type = KVME_TYPE_DEAD,
158 .pgo_alloc = dead_pager_alloc,
159 .pgo_dealloc = dead_pager_dealloc,
160 .pgo_getpages = dead_pager_getpages,
161 .pgo_putpages = dead_pager_putpages,
162 .pgo_haspage = dead_pager_haspage,
163 .pgo_getvp = dead_pager_getvp,
164 };
165
166 const struct pagerops *pagertab[16] __read_mostly = {
167 [OBJT_DEFAULT] = &defaultpagerops,
168 [OBJT_SWAP] = &swappagerops,
169 [OBJT_VNODE] = &vnodepagerops,
170 [OBJT_DEVICE] = &devicepagerops,
171 [OBJT_PHYS] = &physpagerops,
172 [OBJT_DEAD] = &deadpagerops,
173 [OBJT_SG] = &sgpagerops,
174 [OBJT_MGTDEVICE] = &mgtdevicepagerops,
175 };
176 static struct mtx pagertab_lock;
177
178 void
vm_pager_init(void)179 vm_pager_init(void)
180 {
181 const struct pagerops **pgops;
182 int i;
183
184 mtx_init(&pagertab_lock, "dynpag", NULL, MTX_DEF);
185
186 /*
187 * Initialize known pagers
188 */
189 for (i = 0; i < OBJT_FIRST_DYN; i++) {
190 pgops = &pagertab[i];
191 if ((*pgops)->pgo_init != NULL)
192 (*(*pgops)->pgo_init)();
193 }
194 }
195
196 static int nswbuf_max;
197
198 void
vm_pager_bufferinit(void)199 vm_pager_bufferinit(void)
200 {
201
202 /* Main zone for paging bufs. */
203 pbuf_zone = uma_zcreate("pbuf",
204 sizeof(struct buf) + PBUF_PAGES * sizeof(vm_page_t),
205 pbuf_ctor, pbuf_dtor, pbuf_init, NULL, UMA_ALIGN_CACHE,
206 UMA_ZONE_NOFREE);
207 /* Few systems may still use this zone directly, so it needs a limit. */
208 nswbuf_max += uma_zone_set_max(pbuf_zone, NSWBUF_MIN);
209 }
210
211 uma_zone_t
pbuf_zsecond_create(const char * name,int max)212 pbuf_zsecond_create(const char *name, int max)
213 {
214 uma_zone_t zone;
215
216 zone = uma_zsecond_create(name, pbuf_ctor, pbuf_dtor, NULL, NULL,
217 pbuf_zone);
218 /*
219 * uma_prealloc() rounds up to items per slab. If we would prealloc
220 * immediately on every pbuf_zsecond_create(), we may accumulate too
221 * much of difference between hard limit and prealloced items, which
222 * means wasted memory.
223 */
224 if (nswbuf_max > 0)
225 nswbuf_max += uma_zone_set_max(zone, max);
226 else
227 uma_prealloc(pbuf_zone, uma_zone_set_max(zone, max));
228
229 return (zone);
230 }
231
232 static void
pbuf_prealloc(void * arg __unused)233 pbuf_prealloc(void *arg __unused)
234 {
235
236 uma_prealloc(pbuf_zone, nswbuf_max);
237 nswbuf_max = -1;
238 }
239
240 SYSINIT(pbuf, SI_SUB_KTHREAD_BUF, SI_ORDER_ANY, pbuf_prealloc, NULL);
241
242 /*
243 * Allocate an instance of a pager of the given type.
244 * Size, protection and offset parameters are passed in for pagers that
245 * need to perform page-level validation (e.g. the device pager).
246 */
247 vm_object_t
vm_pager_allocate(objtype_t type,void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t off,struct ucred * cred)248 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
249 vm_prot_t prot, vm_ooffset_t off, struct ucred *cred)
250 {
251 vm_object_t object;
252
253 MPASS(type < nitems(pagertab));
254
255 object = (*pagertab[type]->pgo_alloc)(handle, size, prot, off, cred);
256 if (object != NULL)
257 object->type = type;
258 return (object);
259 }
260
261 /*
262 * The object must be locked.
263 */
264 void
vm_pager_deallocate(vm_object_t object)265 vm_pager_deallocate(vm_object_t object)
266 {
267
268 VM_OBJECT_ASSERT_WLOCKED(object);
269 MPASS(object->type < nitems(pagertab));
270 (*pagertab[object->type]->pgo_dealloc) (object);
271 }
272
273 static void
vm_pager_assert_in(vm_object_t object,vm_page_t * m,int count)274 vm_pager_assert_in(vm_object_t object, vm_page_t *m, int count)
275 {
276 #ifdef INVARIANTS
277
278 /*
279 * All pages must be consecutive, busied, not mapped, not fully valid,
280 * not dirty and belong to the proper object. Some pages may be the
281 * bogus page, but the first and last pages must be a real ones.
282 */
283
284 VM_OBJECT_ASSERT_UNLOCKED(object);
285 VM_OBJECT_ASSERT_PAGING(object);
286 KASSERT(count > 0, ("%s: 0 count", __func__));
287 for (int i = 0 ; i < count; i++) {
288 if (m[i] == bogus_page) {
289 KASSERT(i != 0 && i != count - 1,
290 ("%s: page %d is the bogus page", __func__, i));
291 continue;
292 }
293 vm_page_assert_xbusied(m[i]);
294 KASSERT(!pmap_page_is_mapped(m[i]),
295 ("%s: page %p is mapped", __func__, m[i]));
296 KASSERT(m[i]->valid != VM_PAGE_BITS_ALL,
297 ("%s: request for a valid page %p", __func__, m[i]));
298 KASSERT(m[i]->dirty == 0,
299 ("%s: page %p is dirty", __func__, m[i]));
300 KASSERT(m[i]->object == object,
301 ("%s: wrong object %p/%p", __func__, object, m[i]->object));
302 KASSERT(m[i]->pindex == m[0]->pindex + i,
303 ("%s: page %p isn't consecutive", __func__, m[i]));
304 }
305 #endif
306 }
307
308 /*
309 * Page in the pages for the object using its associated pager.
310 * The requested page must be fully valid on successful return.
311 */
312 int
vm_pager_get_pages(vm_object_t object,vm_page_t * m,int count,int * rbehind,int * rahead)313 vm_pager_get_pages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
314 int *rahead)
315 {
316 #ifdef INVARIANTS
317 vm_pindex_t pindex = m[0]->pindex;
318 #endif
319 int r;
320
321 MPASS(object->type < nitems(pagertab));
322 vm_pager_assert_in(object, m, count);
323
324 r = (*pagertab[object->type]->pgo_getpages)(object, m, count, rbehind,
325 rahead);
326 if (r != VM_PAGER_OK)
327 return (r);
328
329 for (int i = 0; i < count; i++) {
330 /*
331 * If pager has replaced a page, assert that it had
332 * updated the array.
333 */
334 #ifdef INVARIANTS
335 KASSERT(m[i] == vm_page_relookup(object, pindex++),
336 ("%s: mismatch page %p pindex %ju", __func__,
337 m[i], (uintmax_t )pindex - 1));
338 #endif
339
340 /*
341 * Zero out partially filled data.
342 */
343 if (m[i]->valid != VM_PAGE_BITS_ALL)
344 vm_page_zero_invalid(m[i], TRUE);
345 }
346 return (VM_PAGER_OK);
347 }
348
349 int
vm_pager_get_pages_async(vm_object_t object,vm_page_t * m,int count,int * rbehind,int * rahead,pgo_getpages_iodone_t iodone,void * arg)350 vm_pager_get_pages_async(vm_object_t object, vm_page_t *m, int count,
351 int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
352 {
353
354 MPASS(object->type < nitems(pagertab));
355 vm_pager_assert_in(object, m, count);
356
357 return ((*pagertab[object->type]->pgo_getpages_async)(object, m,
358 count, rbehind, rahead, iodone, arg));
359 }
360
361 /*
362 * vm_pager_put_pages() - inline, see vm/vm_pager.h
363 * vm_pager_has_page() - inline, see vm/vm_pager.h
364 */
365
366 /*
367 * Search the specified pager object list for an object with the
368 * specified handle. If an object with the specified handle is found,
369 * increase its reference count and return it. Otherwise, return NULL.
370 *
371 * The pager object list must be locked.
372 */
373 vm_object_t
vm_pager_object_lookup(struct pagerlst * pg_list,void * handle)374 vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
375 {
376 vm_object_t object;
377
378 TAILQ_FOREACH(object, pg_list, pager_object_list) {
379 if (object->handle == handle) {
380 VM_OBJECT_WLOCK(object);
381 if ((object->flags & OBJ_DEAD) == 0) {
382 vm_object_reference_locked(object);
383 VM_OBJECT_WUNLOCK(object);
384 break;
385 }
386 VM_OBJECT_WUNLOCK(object);
387 }
388 }
389 return (object);
390 }
391
392 int
vm_pager_alloc_dyn_type(struct pagerops * ops,int base_type)393 vm_pager_alloc_dyn_type(struct pagerops *ops, int base_type)
394 {
395 int res;
396
397 mtx_lock(&pagertab_lock);
398 MPASS(base_type == -1 ||
399 (base_type >= OBJT_DEFAULT && base_type < nitems(pagertab)));
400 for (res = OBJT_FIRST_DYN; res < nitems(pagertab); res++) {
401 if (pagertab[res] == NULL)
402 break;
403 }
404 if (res == nitems(pagertab)) {
405 mtx_unlock(&pagertab_lock);
406 return (-1);
407 }
408 if (base_type != -1) {
409 MPASS(pagertab[base_type] != NULL);
410 #define FIX(n) \
411 if (ops->pgo_##n == NULL) \
412 ops->pgo_##n = pagertab[base_type]->pgo_##n
413 FIX(init);
414 FIX(alloc);
415 FIX(dealloc);
416 FIX(getpages);
417 FIX(getpages_async);
418 FIX(putpages);
419 FIX(haspage);
420 FIX(populate);
421 FIX(pageunswapped);
422 FIX(update_writecount);
423 FIX(release_writecount);
424 FIX(set_writeable_dirty);
425 FIX(mightbedirty);
426 FIX(getvp);
427 FIX(freespace);
428 FIX(page_inserted);
429 FIX(page_removed);
430 FIX(can_alloc_page);
431 #undef FIX
432 }
433 pagertab[res] = ops; /* XXXKIB should be rel, but acq is too much */
434 mtx_unlock(&pagertab_lock);
435 return (res);
436 }
437
438 void
vm_pager_free_dyn_type(objtype_t type)439 vm_pager_free_dyn_type(objtype_t type)
440 {
441 MPASS(type >= OBJT_FIRST_DYN && type < nitems(pagertab));
442
443 mtx_lock(&pagertab_lock);
444 MPASS(pagertab[type] != NULL);
445 pagertab[type] = NULL;
446 mtx_unlock(&pagertab_lock);
447 }
448
449 static int
pbuf_ctor(void * mem,int size,void * arg,int flags)450 pbuf_ctor(void *mem, int size, void *arg, int flags)
451 {
452 struct buf *bp = mem;
453
454 bp->b_vp = NULL;
455 bp->b_bufobj = NULL;
456
457 /* copied from initpbuf() */
458 bp->b_rcred = NOCRED;
459 bp->b_wcred = NOCRED;
460 bp->b_qindex = 0; /* On no queue (QUEUE_NONE) */
461 bp->b_data = bp->b_kvabase;
462 bp->b_xflags = 0;
463 bp->b_flags = B_MAXPHYS;
464 bp->b_ioflags = 0;
465 bp->b_iodone = NULL;
466 bp->b_error = 0;
467 BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
468
469 return (0);
470 }
471
472 static void
pbuf_dtor(void * mem,int size,void * arg)473 pbuf_dtor(void *mem, int size, void *arg)
474 {
475 struct buf *bp = mem;
476
477 if (bp->b_rcred != NOCRED) {
478 crfree(bp->b_rcred);
479 bp->b_rcred = NOCRED;
480 }
481 if (bp->b_wcred != NOCRED) {
482 crfree(bp->b_wcred);
483 bp->b_wcred = NOCRED;
484 }
485
486 BUF_UNLOCK(bp);
487 }
488
489 static const char pbuf_wmesg[] = "pbufwait";
490
491 static int
pbuf_init(void * mem,int size,int flags)492 pbuf_init(void *mem, int size, int flags)
493 {
494 struct buf *bp = mem;
495
496 bp->b_kvabase = (void *)kva_alloc(ptoa(PBUF_PAGES));
497 if (bp->b_kvabase == NULL)
498 return (ENOMEM);
499 bp->b_kvasize = ptoa(PBUF_PAGES);
500 BUF_LOCKINIT(bp, pbuf_wmesg);
501 LIST_INIT(&bp->b_dep);
502 bp->b_rcred = bp->b_wcred = NOCRED;
503 bp->b_xflags = 0;
504
505 return (0);
506 }
507
508 /*
509 * Associate a p-buffer with a vnode.
510 *
511 * Also sets B_PAGING flag to indicate that vnode is not fully associated
512 * with the buffer. i.e. the bp has not been linked into the vnode or
513 * ref-counted.
514 */
515 void
pbgetvp(struct vnode * vp,struct buf * bp)516 pbgetvp(struct vnode *vp, struct buf *bp)
517 {
518
519 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
520 KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
521
522 bp->b_vp = vp;
523 bp->b_flags |= B_PAGING;
524 bp->b_bufobj = &vp->v_bufobj;
525 }
526
527 /*
528 * Associate a p-buffer with a vnode.
529 *
530 * Also sets B_PAGING flag to indicate that vnode is not fully associated
531 * with the buffer. i.e. the bp has not been linked into the vnode or
532 * ref-counted.
533 */
534 void
pbgetbo(struct bufobj * bo,struct buf * bp)535 pbgetbo(struct bufobj *bo, struct buf *bp)
536 {
537
538 KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
539 KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
540
541 bp->b_flags |= B_PAGING;
542 bp->b_bufobj = bo;
543 }
544
545 /*
546 * Disassociate a p-buffer from a vnode.
547 */
548 void
pbrelvp(struct buf * bp)549 pbrelvp(struct buf *bp)
550 {
551
552 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
553 KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
554 KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
555 ("pbrelvp: pager buf on vnode list."));
556
557 bp->b_vp = NULL;
558 bp->b_bufobj = NULL;
559 bp->b_flags &= ~B_PAGING;
560 }
561
562 /*
563 * Disassociate a p-buffer from a bufobj.
564 */
565 void
pbrelbo(struct buf * bp)566 pbrelbo(struct buf *bp)
567 {
568
569 KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
570 KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
571 KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
572 ("pbrelbo: pager buf on vnode list."));
573
574 bp->b_bufobj = NULL;
575 bp->b_flags &= ~B_PAGING;
576 }
577
578 void
vm_object_set_writeable_dirty(vm_object_t object)579 vm_object_set_writeable_dirty(vm_object_t object)
580 {
581 pgo_set_writeable_dirty_t *method;
582
583 MPASS(object->type < nitems(pagertab));
584
585 method = pagertab[object->type]->pgo_set_writeable_dirty;
586 if (method != NULL)
587 method(object);
588 }
589
590 bool
vm_object_mightbedirty(vm_object_t object)591 vm_object_mightbedirty(vm_object_t object)
592 {
593 pgo_mightbedirty_t *method;
594
595 MPASS(object->type < nitems(pagertab));
596
597 method = pagertab[object->type]->pgo_mightbedirty;
598 if (method == NULL)
599 return (false);
600 return (method(object));
601 }
602
603 /*
604 * Return the kvme type of the given object.
605 * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL.
606 */
607 int
vm_object_kvme_type(vm_object_t object,struct vnode ** vpp)608 vm_object_kvme_type(vm_object_t object, struct vnode **vpp)
609 {
610 VM_OBJECT_ASSERT_LOCKED(object);
611 MPASS(object->type < nitems(pagertab));
612
613 if (vpp != NULL)
614 *vpp = vm_object_vnode(object);
615 return (pagertab[object->type]->pgo_kvme_type);
616 }
617