vm_pager.c revision 279688
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	from: @(#)vm_pager.c	8.6 (Berkeley) 1/12/94
33 *
34 *
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
37 *
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39 *
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
45 *
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 *
50 * Carnegie Mellon requests users of this software to return to
51 *
52 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53 *  School of Computer Science
54 *  Carnegie Mellon University
55 *  Pittsburgh PA 15213-3890
56 *
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
59 */
60
61/*
62 *	Paging space routine stubs.  Emulates a matchmaker-like interface
63 *	for builtin pagers.
64 */
65
66#include <sys/cdefs.h>
67__FBSDID("$FreeBSD: head/sys/vm/vm_pager.c 279688 2015-03-06 14:15:30Z glebius $");
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/kernel.h>
72#include <sys/vnode.h>
73#include <sys/bio.h>
74#include <sys/buf.h>
75#include <sys/ucred.h>
76#include <sys/malloc.h>
77#include <sys/rwlock.h>
78
79#include <vm/vm.h>
80#include <vm/vm_param.h>
81#include <vm/vm_kern.h>
82#include <vm/vm_object.h>
83#include <vm/vm_page.h>
84#include <vm/vm_pager.h>
85#include <vm/vm_extern.h>
86
87int cluster_pbuf_freecnt = -1;	/* unlimited to begin with */
88
89static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int);
90static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
91    vm_ooffset_t, struct ucred *);
92static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
93static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
94static void dead_pager_dealloc(vm_object_t);
95
96static int
97dead_pager_getpages(obj, ma, count, req)
98	vm_object_t obj;
99	vm_page_t *ma;
100	int count;
101	int req;
102{
103	return VM_PAGER_FAIL;
104}
105
106static vm_object_t
107dead_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
108    vm_ooffset_t off, struct ucred *cred)
109{
110	return NULL;
111}
112
113static void
114dead_pager_putpages(object, m, count, flags, rtvals)
115	vm_object_t object;
116	vm_page_t *m;
117	int count;
118	int flags;
119	int *rtvals;
120{
121	int i;
122
123	for (i = 0; i < count; i++) {
124		rtvals[i] = VM_PAGER_AGAIN;
125	}
126}
127
128static int
129dead_pager_haspage(object, pindex, prev, next)
130	vm_object_t object;
131	vm_pindex_t pindex;
132	int *prev;
133	int *next;
134{
135	if (prev)
136		*prev = 0;
137	if (next)
138		*next = 0;
139	return FALSE;
140}
141
142static void
143dead_pager_dealloc(object)
144	vm_object_t object;
145{
146	return;
147}
148
149static struct pagerops deadpagerops = {
150	.pgo_alloc = 	dead_pager_alloc,
151	.pgo_dealloc =	dead_pager_dealloc,
152	.pgo_getpages =	dead_pager_getpages,
153	.pgo_putpages =	dead_pager_putpages,
154	.pgo_haspage =	dead_pager_haspage,
155};
156
157struct pagerops *pagertab[] = {
158	&defaultpagerops,	/* OBJT_DEFAULT */
159	&swappagerops,		/* OBJT_SWAP */
160	&vnodepagerops,		/* OBJT_VNODE */
161	&devicepagerops,	/* OBJT_DEVICE */
162	&physpagerops,		/* OBJT_PHYS */
163	&deadpagerops,		/* OBJT_DEAD */
164	&sgpagerops,		/* OBJT_SG */
165	&mgtdevicepagerops,	/* OBJT_MGTDEVICE */
166};
167
168static const int npagers = sizeof(pagertab) / sizeof(pagertab[0]);
169
170/*
171 * Kernel address space for mapping pages.
172 * Used by pagers where KVAs are needed for IO.
173 *
174 * XXX needs to be large enough to support the number of pending async
175 * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
176 * (MAXPHYS == 64k) if you want to get the most efficiency.
177 */
178struct mtx_padalign pbuf_mtx;
179static TAILQ_HEAD(swqueue, buf) bswlist;
180static int bswneeded;
181vm_offset_t swapbkva;		/* swap buffers kva */
182
183void
184vm_pager_init()
185{
186	struct pagerops **pgops;
187
188	TAILQ_INIT(&bswlist);
189	/*
190	 * Initialize known pagers
191	 */
192	for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
193		if ((*pgops)->pgo_init != NULL)
194			(*(*pgops)->pgo_init) ();
195}
196
197void
198vm_pager_bufferinit()
199{
200	struct buf *bp;
201	int i;
202
203	mtx_init(&pbuf_mtx, "pbuf mutex", NULL, MTX_DEF);
204	bp = swbuf;
205	/*
206	 * Now set up swap and physical I/O buffer headers.
207	 */
208	for (i = 0; i < nswbuf; i++, bp++) {
209		TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
210		BUF_LOCKINIT(bp);
211		LIST_INIT(&bp->b_dep);
212		bp->b_rcred = bp->b_wcred = NOCRED;
213		bp->b_xflags = 0;
214	}
215
216	cluster_pbuf_freecnt = nswbuf / 2;
217	vnode_pbuf_freecnt = nswbuf / 2 + 1;
218	vnode_async_pbuf_freecnt = nswbuf / 2;
219}
220
221/*
222 * Allocate an instance of a pager of the given type.
223 * Size, protection and offset parameters are passed in for pagers that
224 * need to perform page-level validation (e.g. the device pager).
225 */
226vm_object_t
227vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
228    vm_prot_t prot, vm_ooffset_t off, struct ucred *cred)
229{
230	vm_object_t ret;
231	struct pagerops *ops;
232
233	ops = pagertab[type];
234	if (ops)
235		ret = (*ops->pgo_alloc) (handle, size, prot, off, cred);
236	else
237		ret = NULL;
238	return (ret);
239}
240
241/*
242 *	The object must be locked.
243 */
244void
245vm_pager_deallocate(object)
246	vm_object_t object;
247{
248
249	VM_OBJECT_ASSERT_WLOCKED(object);
250	(*pagertab[object->type]->pgo_dealloc) (object);
251}
252
253/*
254 * vm_pager_get_pages() - inline, see vm/vm_pager.h
255 * vm_pager_put_pages() - inline, see vm/vm_pager.h
256 * vm_pager_has_page() - inline, see vm/vm_pager.h
257 */
258
259/*
260 * Search the specified pager object list for an object with the
261 * specified handle.  If an object with the specified handle is found,
262 * increase its reference count and return it.  Otherwise, return NULL.
263 *
264 * The pager object list must be locked.
265 */
266vm_object_t
267vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
268{
269	vm_object_t object;
270
271	TAILQ_FOREACH(object, pg_list, pager_object_list) {
272		if (object->handle == handle) {
273			VM_OBJECT_WLOCK(object);
274			if ((object->flags & OBJ_DEAD) == 0) {
275				vm_object_reference_locked(object);
276				VM_OBJECT_WUNLOCK(object);
277				break;
278			}
279			VM_OBJECT_WUNLOCK(object);
280		}
281	}
282	return (object);
283}
284
285/*
286 * Free the non-requested pages from the given array.
287 */
288void
289vm_pager_free_nonreq(vm_object_t object, vm_page_t ma[], int reqpage,
290    int npages)
291{
292	int i;
293	boolean_t object_locked;
294
295	VM_OBJECT_ASSERT_UNLOCKED(object);
296	object_locked = FALSE;
297	for (i = 0; i < npages; ++i) {
298		if (i != reqpage) {
299			if (!object_locked) {
300				VM_OBJECT_WLOCK(object);
301				object_locked = TRUE;
302			}
303			vm_page_lock(ma[i]);
304			vm_page_free(ma[i]);
305			vm_page_unlock(ma[i]);
306		}
307	}
308	if (object_locked)
309		VM_OBJECT_WUNLOCK(object);
310}
311
312/*
313 * initialize a physical buffer
314 */
315
316/*
317 * XXX This probably belongs in vfs_bio.c
318 */
319static void
320initpbuf(struct buf *bp)
321{
322	KASSERT(bp->b_bufobj == NULL, ("initpbuf with bufobj"));
323	KASSERT(bp->b_vp == NULL, ("initpbuf with vp"));
324	bp->b_rcred = NOCRED;
325	bp->b_wcred = NOCRED;
326	bp->b_qindex = 0;	/* On no queue (QUEUE_NONE) */
327	bp->b_saveaddr = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva;
328	bp->b_data = bp->b_saveaddr;
329	bp->b_kvabase = bp->b_saveaddr;
330	bp->b_kvasize = MAXPHYS;
331	bp->b_xflags = 0;
332	bp->b_flags = 0;
333	bp->b_ioflags = 0;
334	bp->b_iodone = NULL;
335	bp->b_error = 0;
336	BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
337}
338
339/*
340 * allocate a physical buffer
341 *
342 *	There are a limited number (nswbuf) of physical buffers.  We need
343 *	to make sure that no single subsystem is able to hog all of them,
344 *	so each subsystem implements a counter which is typically initialized
345 *	to 1/2 nswbuf.  getpbuf() decrements this counter in allocation and
346 *	increments it on release, and blocks if the counter hits zero.  A
347 *	subsystem may initialize the counter to -1 to disable the feature,
348 *	but it must still be sure to match up all uses of getpbuf() with
349 *	relpbuf() using the same variable.
350 *
351 *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
352 *	relatively soon when the rest of the subsystems get smart about it. XXX
353 */
354struct buf *
355getpbuf(int *pfreecnt)
356{
357	struct buf *bp;
358
359	mtx_lock(&pbuf_mtx);
360
361	for (;;) {
362		if (pfreecnt) {
363			while (*pfreecnt == 0) {
364				msleep(pfreecnt, &pbuf_mtx, PVM, "wswbuf0", 0);
365			}
366		}
367
368		/* get a bp from the swap buffer header pool */
369		if ((bp = TAILQ_FIRST(&bswlist)) != NULL)
370			break;
371
372		bswneeded = 1;
373		msleep(&bswneeded, &pbuf_mtx, PVM, "wswbuf1", 0);
374		/* loop in case someone else grabbed one */
375	}
376	TAILQ_REMOVE(&bswlist, bp, b_freelist);
377	if (pfreecnt)
378		--*pfreecnt;
379	mtx_unlock(&pbuf_mtx);
380
381	initpbuf(bp);
382	return bp;
383}
384
385/*
386 * allocate a physical buffer, if one is available.
387 *
388 *	Note that there is no NULL hack here - all subsystems using this
389 *	call understand how to use pfreecnt.
390 */
391struct buf *
392trypbuf(int *pfreecnt)
393{
394	struct buf *bp;
395
396	mtx_lock(&pbuf_mtx);
397	if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) {
398		mtx_unlock(&pbuf_mtx);
399		return NULL;
400	}
401	TAILQ_REMOVE(&bswlist, bp, b_freelist);
402
403	--*pfreecnt;
404
405	mtx_unlock(&pbuf_mtx);
406
407	initpbuf(bp);
408
409	return bp;
410}
411
412/*
413 * release a physical buffer
414 *
415 *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
416 *	relatively soon when the rest of the subsystems get smart about it. XXX
417 */
418void
419relpbuf(struct buf *bp, int *pfreecnt)
420{
421
422	if (bp->b_rcred != NOCRED) {
423		crfree(bp->b_rcred);
424		bp->b_rcred = NOCRED;
425	}
426	if (bp->b_wcred != NOCRED) {
427		crfree(bp->b_wcred);
428		bp->b_wcred = NOCRED;
429	}
430
431	KASSERT(bp->b_vp == NULL, ("relpbuf with vp"));
432	KASSERT(bp->b_bufobj == NULL, ("relpbuf with bufobj"));
433
434	BUF_UNLOCK(bp);
435
436	mtx_lock(&pbuf_mtx);
437	TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
438
439	if (bswneeded) {
440		bswneeded = 0;
441		wakeup(&bswneeded);
442	}
443	if (pfreecnt) {
444		if (++*pfreecnt == 1)
445			wakeup(pfreecnt);
446	}
447	mtx_unlock(&pbuf_mtx);
448}
449
450/*
451 * Associate a p-buffer with a vnode.
452 *
453 * Also sets B_PAGING flag to indicate that vnode is not fully associated
454 * with the buffer.  i.e. the bp has not been linked into the vnode or
455 * ref-counted.
456 */
457void
458pbgetvp(struct vnode *vp, struct buf *bp)
459{
460
461	KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
462	KASSERT(bp->b_bufobj == NULL, ("pbgetvp: not free (bufobj)"));
463
464	bp->b_vp = vp;
465	bp->b_flags |= B_PAGING;
466	bp->b_bufobj = &vp->v_bufobj;
467}
468
469/*
470 * Associate a p-buffer with a vnode.
471 *
472 * Also sets B_PAGING flag to indicate that vnode is not fully associated
473 * with the buffer.  i.e. the bp has not been linked into the vnode or
474 * ref-counted.
475 */
476void
477pbgetbo(struct bufobj *bo, struct buf *bp)
478{
479
480	KASSERT(bp->b_vp == NULL, ("pbgetbo: not free (vnode)"));
481	KASSERT(bp->b_bufobj == NULL, ("pbgetbo: not free (bufobj)"));
482
483	bp->b_flags |= B_PAGING;
484	bp->b_bufobj = bo;
485}
486
487/*
488 * Disassociate a p-buffer from a vnode.
489 */
490void
491pbrelvp(struct buf *bp)
492{
493
494	KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
495	KASSERT(bp->b_bufobj != NULL, ("pbrelvp: NULL bufobj"));
496	KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
497	    ("pbrelvp: pager buf on vnode list."));
498
499	bp->b_vp = NULL;
500	bp->b_bufobj = NULL;
501	bp->b_flags &= ~B_PAGING;
502}
503
504/*
505 * Disassociate a p-buffer from a bufobj.
506 */
507void
508pbrelbo(struct buf *bp)
509{
510
511	KASSERT(bp->b_vp == NULL, ("pbrelbo: vnode"));
512	KASSERT(bp->b_bufobj != NULL, ("pbrelbo: NULL bufobj"));
513	KASSERT((bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) == 0,
514	    ("pbrelbo: pager buf on vnode list."));
515
516	bp->b_bufobj = NULL;
517	bp->b_flags &= ~B_PAGING;
518}
519