]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_pager.c
ping: use the monotonic clock to measure durations
[FreeBSD/FreeBSD.git] / sys / vm / vm_pager.c
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 __FBSDID("$FreeBSD$");
70
71 #include "opt_param.h"
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/vnode.h>
77 #include <sys/bio.h>
78 #include <sys/buf.h>
79 #include <sys/ucred.h>
80 #include <sys/malloc.h>
81 #include <sys/rwlock.h>
82
83 #include <vm/vm.h>
84 #include <vm/vm_param.h>
85 #include <vm/vm_kern.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vm_extern.h>
90 #include <vm/uma.h>
91
92 uma_zone_t pbuf_zone;
93 static int      pbuf_init(void *, int, int);
94 static int      pbuf_ctor(void *, int, void *, int);
95 static void     pbuf_dtor(void *, int, void *);
96
97 static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
98 static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
99     vm_ooffset_t, struct ucred *);
100 static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
101 static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
102 static void dead_pager_dealloc(vm_object_t);
103
104 static int
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
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
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 int
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
142 dead_pager_dealloc(vm_object_t object)
143 {
144
145 }
146
147 static struct pagerops deadpagerops = {
148         .pgo_alloc =    dead_pager_alloc,
149         .pgo_dealloc =  dead_pager_dealloc,
150         .pgo_getpages = dead_pager_getpages,
151         .pgo_putpages = dead_pager_putpages,
152         .pgo_haspage =  dead_pager_haspage,
153 };
154
155 struct pagerops *pagertab[] = {
156         &defaultpagerops,       /* OBJT_DEFAULT */
157         &swappagerops,          /* OBJT_SWAP */
158         &vnodepagerops,         /* OBJT_VNODE */
159         &devicepagerops,        /* OBJT_DEVICE */
160         &physpagerops,          /* OBJT_PHYS */
161         &deadpagerops,          /* OBJT_DEAD */
162         &sgpagerops,            /* OBJT_SG */
163         &mgtdevicepagerops,     /* OBJT_MGTDEVICE */
164 };
165
166 void
167 vm_pager_init(void)
168 {
169         struct pagerops **pgops;
170
171         /*
172          * Initialize known pagers
173          */
174         for (pgops = pagertab; pgops < &pagertab[nitems(pagertab)]; pgops++)
175                 if ((*pgops)->pgo_init != NULL)
176                         (*(*pgops)->pgo_init)();
177 }
178
179 static int nswbuf_max;
180
181 void
182 vm_pager_bufferinit(void)
183 {
184
185         /* Main zone for paging bufs. */
186         pbuf_zone = uma_zcreate("pbuf", sizeof(struct buf),
187             pbuf_ctor, pbuf_dtor, pbuf_init, NULL, UMA_ALIGN_CACHE,
188             UMA_ZONE_VM | UMA_ZONE_NOFREE);
189         /* Few systems may still use this zone directly, so it needs a limit. */
190         nswbuf_max += uma_zone_set_max(pbuf_zone, NSWBUF_MIN);
191 }
192
193 uma_zone_t
194 pbuf_zsecond_create(char *name, int max)
195 {
196         uma_zone_t zone;
197
198         zone = uma_zsecond_create(name, pbuf_ctor, pbuf_dtor, NULL, NULL,
199             pbuf_zone);
200         /*
201          * uma_prealloc() rounds up to items per slab. If we would prealloc
202          * immediately on every pbuf_zsecond_create(), we may accumulate too
203          * much of difference between hard limit and prealloced items, which
204          * means wasted memory.
205          */
206         if (nswbuf_max > 0)
207                 nswbuf_max += uma_zone_set_max(zone, max);
208         else
209                 uma_prealloc(pbuf_zone, uma_zone_set_max(zone, max));
210
211         return (zone);
212 }
213
214 static void
215 pbuf_prealloc(void *arg __unused)
216 {
217
218         uma_prealloc(pbuf_zone, nswbuf_max);
219         nswbuf_max = -1;
220 }
221
222 SYSINIT(pbuf, SI_SUB_KTHREAD_BUF, SI_ORDER_ANY, pbuf_prealloc, NULL);
223
224 /*
225  * Allocate an instance of a pager of the given type.
226  * Size, protection and offset parameters are passed in for pagers that
227  * need to perform page-level validation (e.g. the device pager).
228  */
229 vm_object_t
230 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
231     vm_prot_t prot, vm_ooffset_t off, struct ucred *cred)
232 {
233         vm_object_t ret;
234         struct pagerops *ops;
235
236         ops = pagertab[type];
237         if (ops)
238                 ret = (*ops->pgo_alloc)(handle, size, prot, off, cred);
239         else
240                 ret = NULL;
241         return (ret);
242 }
243
244 /*
245  *      The object must be locked.
246  */
247 void
248 vm_pager_deallocate(vm_object_t object)
249 {
250
251         VM_OBJECT_ASSERT_WLOCKED(object);
252         (*pagertab[object->type]->pgo_dealloc) (object);
253 }
254
255 static void
256 vm_pager_assert_in(vm_object_t object, vm_page_t *m, int count)
257 {
258 #ifdef INVARIANTS
259
260         VM_OBJECT_ASSERT_WLOCKED(object);
261         KASSERT(count > 0, ("%s: 0 count", __func__));
262         /*
263          * All pages must be busied, not mapped, not fully valid,
264          * not dirty and belong to the proper object.
265          */
266         for (int i = 0 ; i < count; i++) {
267                 if (m[i] == bogus_page)
268                         continue;
269                 vm_page_assert_xbusied(m[i]);
270                 KASSERT(!pmap_page_is_mapped(m[i]),
271                     ("%s: page %p is mapped", __func__, m[i]));
272                 KASSERT(m[i]->valid != VM_PAGE_BITS_ALL,
273                     ("%s: request for a valid page %p", __func__, m[i]));
274                 KASSERT(m[i]->dirty == 0,
275                     ("%s: page %p is dirty", __func__, m[i]));
276                 KASSERT(m[i]->object == object,
277                     ("%s: wrong object %p/%p", __func__, object, m[i]->object));
278         }
279 #endif
280 }
281
282 /*
283  * Page in the pages for the object using its associated pager.
284  * The requested page must be fully valid on successful return.
285  */
286 int
287 vm_pager_get_pages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
288     int *rahead)
289 {
290 #ifdef INVARIANTS
291         vm_pindex_t pindex = m[0]->pindex;
292 #endif
293         int r;
294
295         vm_pager_assert_in(object, m, count);
296
297         r = (*pagertab[object->type]->pgo_getpages)(object, m, count, rbehind,
298             rahead);
299         if (r != VM_PAGER_OK)
300                 return (r);
301
302         for (int i = 0; i < count; i++) {
303                 /*
304                  * If pager has replaced a page, assert that it had
305                  * updated the array.
306                  */
307                 KASSERT(m[i] == vm_page_lookup(object, pindex++),
308                     ("%s: mismatch page %p pindex %ju", __func__,
309                     m[i], (uintmax_t )pindex - 1));
310                 /*
311                  * Zero out partially filled data.
312                  */
313                 if (m[i]->valid != VM_PAGE_BITS_ALL)
314                         vm_page_zero_invalid(m[i], TRUE);
315         }
316         return (VM_PAGER_OK);
317 }
318
319 int
320 vm_pager_get_pages_async(vm_object_t object, vm_page_t *m, int count,
321     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
322 {
323
324         vm_pager_assert_in(object, m, count);
325
326         return ((*pagertab[object->type]->pgo_getpages_async)(object, m,
327             count, rbehind, rahead, iodone, arg));
328 }
329
330 /*
331  * vm_pager_put_pages() - inline, see vm/vm_pager.h
332  * vm_pager_has_page() - inline, see vm/vm_pager.h
333  */
334
335 /*
336  * Search the specified pager object list for an object with the
337  * specified handle.  If an object with the specified handle is found,
338  * increase its reference count and return it.  Otherwise, return NULL.
339  *
340  * The pager object list must be locked.
341  */
342 vm_object_t
343 vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
344 {
345         vm_object_t object;
346
347         TAILQ_FOREACH(object, pg_list, pager_object_list) {
348                 if (object->handle == handle) {
349                         VM_OBJECT_WLOCK(object);
350                         if ((object->flags & OBJ_DEAD) == 0) {
351                                 vm_object_reference_locked(object);
352                                 VM_OBJECT_WUNLOCK(object);
353                                 break;
354                         }
355                         VM_OBJECT_WUNLOCK(object);
356                 }
357         }
358         return (object);
359 }
360
361 static int
362 pbuf_ctor(void *mem, int size, void *arg, int flags)
363 {
364         struct buf *bp = mem;
365
366         bp->b_vp = NULL;
367         bp->b_bufobj = NULL;
368
369         /* copied from initpbuf() */
370         bp->b_rcred = NOCRED;
371         bp->b_wcred = NOCRED;
372         bp->b_qindex = 0;       /* On no queue (QUEUE_NONE) */
373         bp->b_data = bp->b_kvabase;
374         bp->b_xflags = 0;
375         bp->b_flags = 0;
376         bp->b_ioflags = 0;
377         bp->b_iodone = NULL;
378         bp->b_error = 0;
379         BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
380
381         return (0);
382 }
383
384 static void
385 pbuf_dtor(void *mem, int size, void *arg)
386 {
387         struct buf *bp = mem;
388
389         if (bp->b_rcred != NOCRED) {
390                 crfree(bp->b_rcred);
391                 bp->b_rcred = NOCRED;
392         }
393         if (bp->b_wcred != NOCRED) {
394                 crfree(bp->b_wcred);
395                 bp->b_wcred = NOCRED;
396         }
397
398         BUF_UNLOCK(bp);
399 }
400
401 static int
402 pbuf_init(void *mem, int size, int flags)
403 {
404         struct buf *bp = mem;
405
406         bp->b_kvabase = (void *)kva_alloc(MAXPHYS);
407         if (bp->b_kvabase == NULL)
408                 return (ENOMEM);
409         bp->b_kvasize = MAXPHYS;
410         BUF_LOCKINIT(bp);
411         LIST_INIT(&bp->b_dep);
412         bp->b_rcred = bp->b_wcred = NOCRED;
413         bp->b_xflags = 0;
414
415         return (0);
416 }
417
418 /*
419  * Associate a p-buffer with a vnode.
420  *
421  * Also sets B_PAGING flag to indicate that vnode is not fully associated
422  * with the buffer.  i.e. the bp has not been linked into the vnode or
423  * ref-counted.
424  */
425 void
426 pbgetvp(struct vnode *vp, struct buf *bp)
427 {
428
429         KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
430         KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
431
432         bp->b_vp = vp;
433         bp->b_flags |= B_PAGING;
434         bp->b_bufobj = &vp->v_bufobj;
435 }
436
437 /*
438  * Associate a p-buffer with a vnode.
439  *
440  * Also sets B_PAGING flag to indicate that vnode is not fully associated
441  * with the buffer.  i.e. the bp has not been linked into the vnode or
442  * ref-counted.
443  */
444 void
445 pbgetbo(struct bufobj *bo, struct buf *bp)
446 {
447
448         KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
449         KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
450
451         bp->b_flags |= B_PAGING;
452         bp->b_bufobj = bo;
453 }
454
455 /*
456  * Disassociate a p-buffer from a vnode.
457  */
458 void
459 pbrelvp(struct buf *bp)
460 {
461
462         KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
463         KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
464         KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
465             ("pbrelvp: pager buf on vnode list."));
466
467         bp->b_vp = NULL;
468         bp->b_bufobj = NULL;
469         bp->b_flags &= ~B_PAGING;
470 }
471
472 /*
473  * Disassociate a p-buffer from a bufobj.
474  */
475 void
476 pbrelbo(struct buf *bp)
477 {
478
479         KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
480         KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
481         KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
482             ("pbrelbo: pager buf on vnode list."));
483
484         bp->b_bufobj = NULL;
485         bp->b_flags &= ~B_PAGING;
486 }