2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 * from: @(#)vm_pager.c 8.6 (Berkeley) 1/12/94
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
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.
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.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
64 * Paging space routine stubs. Emulates a matchmaker-like interface
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
71 #include "opt_param.h"
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/vnode.h>
79 #include <sys/ucred.h>
80 #include <sys/malloc.h>
81 #include <sys/rwlock.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_kern.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_pager.h>
90 #include <vm/vm_extern.h>
94 static int pbuf_init(void *, int, int);
95 static int pbuf_ctor(void *, int, void *, int);
96 static void pbuf_dtor(void *, int, void *);
98 static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
99 static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
100 vm_ooffset_t, struct ucred *);
101 static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
102 static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
103 static void dead_pager_dealloc(vm_object_t);
104 static void dead_pager_getvp(vm_object_t, struct vnode **, bool *);
107 dead_pager_getpages(vm_object_t obj, vm_page_t *ma, int count, int *rbehind,
111 return (VM_PAGER_FAIL);
115 dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
116 vm_ooffset_t off, struct ucred *cred)
123 dead_pager_putpages(vm_object_t object, vm_page_t *m, int count,
124 int flags, int *rtvals)
128 for (i = 0; i < count; i++)
129 rtvals[i] = VM_PAGER_AGAIN;
133 dead_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *prev, int *next)
144 dead_pager_dealloc(vm_object_t object)
150 dead_pager_getvp(vm_object_t object, struct vnode **vpp, bool *vp_heldp)
153 * For OBJT_DEAD objects, v_writecount was handled in
154 * vnode_pager_dealloc().
158 static const struct pagerops deadpagerops = {
159 .pgo_kvme_type = KVME_TYPE_DEAD,
160 .pgo_alloc = dead_pager_alloc,
161 .pgo_dealloc = dead_pager_dealloc,
162 .pgo_getpages = dead_pager_getpages,
163 .pgo_putpages = dead_pager_putpages,
164 .pgo_haspage = dead_pager_haspage,
165 .pgo_getvp = dead_pager_getvp,
168 const struct pagerops *pagertab[] __read_mostly = {
169 [OBJT_DEFAULT] = &defaultpagerops,
170 [OBJT_SWAP] = &swappagerops,
171 [OBJT_VNODE] = &vnodepagerops,
172 [OBJT_DEVICE] = &devicepagerops,
173 [OBJT_PHYS] = &physpagerops,
174 [OBJT_DEAD] = &deadpagerops,
175 [OBJT_SG] = &sgpagerops,
176 [OBJT_MGTDEVICE] = &mgtdevicepagerops,
177 [OBJT_SWAP_TMPFS] = &swaptmpfspagerops,
183 const struct pagerops **pgops;
186 * Initialize known pagers
188 for (pgops = pagertab; pgops < &pagertab[nitems(pagertab)]; pgops++)
189 if ((*pgops)->pgo_init != NULL)
190 (*(*pgops)->pgo_init)();
193 static int nswbuf_max;
196 vm_pager_bufferinit(void)
199 /* Main zone for paging bufs. */
200 pbuf_zone = uma_zcreate("pbuf",
201 sizeof(struct buf) + PBUF_PAGES * sizeof(vm_page_t),
202 pbuf_ctor, pbuf_dtor, pbuf_init, NULL, UMA_ALIGN_CACHE,
204 /* Few systems may still use this zone directly, so it needs a limit. */
205 nswbuf_max += uma_zone_set_max(pbuf_zone, NSWBUF_MIN);
209 pbuf_zsecond_create(const char *name, int max)
213 zone = uma_zsecond_create(name, pbuf_ctor, pbuf_dtor, NULL, NULL,
216 * uma_prealloc() rounds up to items per slab. If we would prealloc
217 * immediately on every pbuf_zsecond_create(), we may accumulate too
218 * much of difference between hard limit and prealloced items, which
219 * means wasted memory.
222 nswbuf_max += uma_zone_set_max(zone, max);
224 uma_prealloc(pbuf_zone, uma_zone_set_max(zone, max));
230 pbuf_prealloc(void *arg __unused)
233 uma_prealloc(pbuf_zone, nswbuf_max);
237 SYSINIT(pbuf, SI_SUB_KTHREAD_BUF, SI_ORDER_ANY, pbuf_prealloc, NULL);
240 * Allocate an instance of a pager of the given type.
241 * Size, protection and offset parameters are passed in for pagers that
242 * need to perform page-level validation (e.g. the device pager).
245 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
246 vm_prot_t prot, vm_ooffset_t off, struct ucred *cred)
249 const struct pagerops *ops;
251 ops = pagertab[type];
253 ret = (*ops->pgo_alloc)(handle, size, prot, off, cred);
260 * The object must be locked.
263 vm_pager_deallocate(vm_object_t object)
266 VM_OBJECT_ASSERT_WLOCKED(object);
267 (*pagertab[object->type]->pgo_dealloc) (object);
271 vm_pager_assert_in(vm_object_t object, vm_page_t *m, int count)
276 * All pages must be consecutive, busied, not mapped, not fully valid,
277 * not dirty and belong to the proper object. Some pages may be the
278 * bogus page, but the first and last pages must be a real ones.
281 VM_OBJECT_ASSERT_UNLOCKED(object);
282 VM_OBJECT_ASSERT_PAGING(object);
283 KASSERT(count > 0, ("%s: 0 count", __func__));
284 for (int i = 0 ; i < count; i++) {
285 if (m[i] == bogus_page) {
286 KASSERT(i != 0 && i != count - 1,
287 ("%s: page %d is the bogus page", __func__, i));
290 vm_page_assert_xbusied(m[i]);
291 KASSERT(!pmap_page_is_mapped(m[i]),
292 ("%s: page %p is mapped", __func__, m[i]));
293 KASSERT(m[i]->valid != VM_PAGE_BITS_ALL,
294 ("%s: request for a valid page %p", __func__, m[i]));
295 KASSERT(m[i]->dirty == 0,
296 ("%s: page %p is dirty", __func__, m[i]));
297 KASSERT(m[i]->object == object,
298 ("%s: wrong object %p/%p", __func__, object, m[i]->object));
299 KASSERT(m[i]->pindex == m[0]->pindex + i,
300 ("%s: page %p isn't consecutive", __func__, m[i]));
306 * Page in the pages for the object using its associated pager.
307 * The requested page must be fully valid on successful return.
310 vm_pager_get_pages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
314 vm_pindex_t pindex = m[0]->pindex;
318 vm_pager_assert_in(object, m, count);
320 r = (*pagertab[object->type]->pgo_getpages)(object, m, count, rbehind,
322 if (r != VM_PAGER_OK)
325 for (int i = 0; i < count; i++) {
327 * If pager has replaced a page, assert that it had
331 VM_OBJECT_RLOCK(object);
332 KASSERT(m[i] == vm_page_lookup(object, pindex++),
333 ("%s: mismatch page %p pindex %ju", __func__,
334 m[i], (uintmax_t )pindex - 1));
335 VM_OBJECT_RUNLOCK(object);
338 * Zero out partially filled data.
340 if (m[i]->valid != VM_PAGE_BITS_ALL)
341 vm_page_zero_invalid(m[i], TRUE);
343 return (VM_PAGER_OK);
347 vm_pager_get_pages_async(vm_object_t object, vm_page_t *m, int count,
348 int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
351 vm_pager_assert_in(object, m, count);
353 return ((*pagertab[object->type]->pgo_getpages_async)(object, m,
354 count, rbehind, rahead, iodone, arg));
358 * vm_pager_put_pages() - inline, see vm/vm_pager.h
359 * vm_pager_has_page() - inline, see vm/vm_pager.h
363 * Search the specified pager object list for an object with the
364 * specified handle. If an object with the specified handle is found,
365 * increase its reference count and return it. Otherwise, return NULL.
367 * The pager object list must be locked.
370 vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
374 TAILQ_FOREACH(object, pg_list, pager_object_list) {
375 if (object->handle == handle) {
376 VM_OBJECT_WLOCK(object);
377 if ((object->flags & OBJ_DEAD) == 0) {
378 vm_object_reference_locked(object);
379 VM_OBJECT_WUNLOCK(object);
382 VM_OBJECT_WUNLOCK(object);
389 pbuf_ctor(void *mem, int size, void *arg, int flags)
391 struct buf *bp = mem;
396 /* copied from initpbuf() */
397 bp->b_rcred = NOCRED;
398 bp->b_wcred = NOCRED;
399 bp->b_qindex = 0; /* On no queue (QUEUE_NONE) */
400 bp->b_data = bp->b_kvabase;
402 bp->b_flags = B_MAXPHYS;
406 BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
412 pbuf_dtor(void *mem, int size, void *arg)
414 struct buf *bp = mem;
416 if (bp->b_rcred != NOCRED) {
418 bp->b_rcred = NOCRED;
420 if (bp->b_wcred != NOCRED) {
422 bp->b_wcred = NOCRED;
429 pbuf_init(void *mem, int size, int flags)
431 struct buf *bp = mem;
433 bp->b_kvabase = (void *)kva_alloc(ptoa(PBUF_PAGES));
434 if (bp->b_kvabase == NULL)
436 bp->b_kvasize = ptoa(PBUF_PAGES);
438 LIST_INIT(&bp->b_dep);
439 bp->b_rcred = bp->b_wcred = NOCRED;
446 * Associate a p-buffer with a vnode.
448 * Also sets B_PAGING flag to indicate that vnode is not fully associated
449 * with the buffer. i.e. the bp has not been linked into the vnode or
453 pbgetvp(struct vnode *vp, struct buf *bp)
456 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
457 KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
460 bp->b_flags |= B_PAGING;
461 bp->b_bufobj = &vp->v_bufobj;
465 * Associate a p-buffer with a vnode.
467 * Also sets B_PAGING flag to indicate that vnode is not fully associated
468 * with the buffer. i.e. the bp has not been linked into the vnode or
472 pbgetbo(struct bufobj *bo, struct buf *bp)
475 KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
476 KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
478 bp->b_flags |= B_PAGING;
483 * Disassociate a p-buffer from a vnode.
486 pbrelvp(struct buf *bp)
489 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
490 KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
491 KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
492 ("pbrelvp: pager buf on vnode list."));
496 bp->b_flags &= ~B_PAGING;
500 * Disassociate a p-buffer from a bufobj.
503 pbrelbo(struct buf *bp)
506 KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
507 KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
508 KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
509 ("pbrelbo: pager buf on vnode list."));
512 bp->b_flags &= ~B_PAGING;
516 vm_object_set_writeable_dirty(vm_object_t object)
518 pgo_set_writeable_dirty_t *method;
520 method = pagertab[object->type]->pgo_set_writeable_dirty;
526 vm_object_mightbedirty(vm_object_t object)
528 pgo_mightbedirty_t *method;
530 method = pagertab[object->type]->pgo_mightbedirty;
533 return (method(object));
537 * Return the kvme type of the given object.
538 * If vpp is not NULL, set it to the object's vm_object_vnode() or NULL.
541 vm_object_kvme_type(vm_object_t object, struct vnode **vpp)
543 VM_OBJECT_ASSERT_LOCKED(object);
546 *vpp = vm_object_vnode(object);
547 return (pagertab[object->type]->pgo_kvme_type);