xref: /freebsd/sys/vm/vm_pager.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. 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  *	from: @(#)vm_pager.c	8.6 (Berkeley) 1/12/94
37  *
38  *
39  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40  * All rights reserved.
41  *
42  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  */
64 
65 /*
66  *	Paging space routine stubs.  Emulates a matchmaker-like interface
67  *	for builtin pagers.
68  */
69 
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
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 
82 #include <vm/vm.h>
83 #include <vm/vm_param.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_pager.h>
87 #include <vm/vm_extern.h>
88 
89 MALLOC_DEFINE(M_VMPGDATA, "VM pgdata", "XXX: VM pager private data");
90 
91 int cluster_pbuf_freecnt = -1;	/* unlimited to begin with */
92 
93 static int dead_pager_getpages(vm_object_t, vm_page_t *, int, int);
94 static vm_object_t dead_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
95 	vm_ooffset_t);
96 static void dead_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
97 static boolean_t dead_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
98 static void dead_pager_dealloc(vm_object_t);
99 
100 static int
101 dead_pager_getpages(obj, ma, count, req)
102 	vm_object_t obj;
103 	vm_page_t *ma;
104 	int count;
105 	int req;
106 {
107 	return VM_PAGER_FAIL;
108 }
109 
110 static vm_object_t
111 dead_pager_alloc(handle, size, prot, off)
112 	void *handle;
113 	vm_ooffset_t size;
114 	vm_prot_t prot;
115 	vm_ooffset_t off;
116 {
117 	return NULL;
118 }
119 
120 static void
121 dead_pager_putpages(object, m, count, flags, rtvals)
122 	vm_object_t object;
123 	vm_page_t *m;
124 	int count;
125 	int flags;
126 	int *rtvals;
127 {
128 	int i;
129 
130 	for (i = 0; i < count; i++) {
131 		rtvals[i] = VM_PAGER_AGAIN;
132 	}
133 }
134 
135 static int
136 dead_pager_haspage(object, pindex, prev, next)
137 	vm_object_t object;
138 	vm_pindex_t pindex;
139 	int *prev;
140 	int *next;
141 {
142 	if (prev)
143 		*prev = 0;
144 	if (next)
145 		*next = 0;
146 	return FALSE;
147 }
148 
149 static void
150 dead_pager_dealloc(object)
151 	vm_object_t object;
152 {
153 	return;
154 }
155 
156 static struct pagerops deadpagerops = {
157 	.pgo_alloc = 	dead_pager_alloc,
158 	.pgo_dealloc =	dead_pager_dealloc,
159 	.pgo_getpages =	dead_pager_getpages,
160 	.pgo_putpages =	dead_pager_putpages,
161 	.pgo_haspage =	dead_pager_haspage,
162 };
163 
164 struct pagerops *pagertab[] = {
165 	&defaultpagerops,	/* OBJT_DEFAULT */
166 	&swappagerops,		/* OBJT_SWAP */
167 	&vnodepagerops,		/* OBJT_VNODE */
168 	&devicepagerops,	/* OBJT_DEVICE */
169 	&physpagerops,		/* OBJT_PHYS */
170 	&deadpagerops		/* OBJT_DEAD */
171 };
172 
173 int npagers = sizeof(pagertab) / sizeof(pagertab[0]);
174 
175 /*
176  * Kernel address space for mapping pages.
177  * Used by pagers where KVAs are needed for IO.
178  *
179  * XXX needs to be large enough to support the number of pending async
180  * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
181  * (MAXPHYS == 64k) if you want to get the most efficiency.
182  */
183 #define PAGER_MAP_SIZE	(8 * 1024 * 1024)
184 
185 int pager_map_size = PAGER_MAP_SIZE;
186 vm_map_t pager_map;
187 static int bswneeded;
188 static vm_offset_t swapbkva;		/* swap buffers kva */
189 struct mtx pbuf_mtx;
190 static TAILQ_HEAD(swqueue, buf) bswlist;
191 
192 void
193 vm_pager_init()
194 {
195 	struct pagerops **pgops;
196 
197 	TAILQ_INIT(&bswlist);
198 	/*
199 	 * Initialize known pagers
200 	 */
201 	for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
202 		if (pgops && ((*pgops)->pgo_init != NULL))
203 			(*(*pgops)->pgo_init) ();
204 }
205 
206 void
207 vm_pager_bufferinit()
208 {
209 	struct buf *bp;
210 	int i;
211 
212 	mtx_init(&pbuf_mtx, "pbuf mutex", NULL, MTX_DEF);
213 	bp = swbuf;
214 	/*
215 	 * Now set up swap and physical I/O buffer headers.
216 	 */
217 	for (i = 0; i < nswbuf; i++, bp++) {
218 		TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
219 		BUF_LOCKINIT(bp);
220 		LIST_INIT(&bp->b_dep);
221 		bp->b_rcred = bp->b_wcred = NOCRED;
222 		bp->b_xflags = 0;
223 	}
224 
225 	cluster_pbuf_freecnt = nswbuf / 2;
226 
227 	swapbkva = kmem_alloc_nofault(pager_map, nswbuf * MAXPHYS);
228 	if (!swapbkva)
229 		panic("Not enough pager_map VM space for physical buffers");
230 }
231 
232 /*
233  * Allocate an instance of a pager of the given type.
234  * Size, protection and offset parameters are passed in for pagers that
235  * need to perform page-level validation (e.g. the device pager).
236  */
237 vm_object_t
238 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size,
239 		  vm_prot_t prot, vm_ooffset_t off)
240 {
241 	vm_object_t ret;
242 	struct pagerops *ops;
243 
244 	ops = pagertab[type];
245 	if (ops)
246 		ret = (*ops->pgo_alloc) (handle, size, prot, off);
247 	else
248 		ret = NULL;
249 	return (ret);
250 }
251 
252 /*
253  *	The object must be locked.
254  */
255 void
256 vm_pager_deallocate(object)
257 	vm_object_t object;
258 {
259 
260 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
261 	(*pagertab[object->type]->pgo_dealloc) (object);
262 }
263 
264 /*
265  * vm_pager_get_pages() - inline, see vm/vm_pager.h
266  * vm_pager_put_pages() - inline, see vm/vm_pager.h
267  * vm_pager_has_page() - inline, see vm/vm_pager.h
268  * vm_pager_page_inserted() - inline, see vm/vm_pager.h
269  * vm_pager_page_removed() - inline, see vm/vm_pager.h
270  */
271 
272 vm_offset_t
273 vm_pager_map_page(m)
274 	vm_page_t m;
275 {
276 	vm_offset_t kva;
277 
278 	kva = kmem_alloc_wait(pager_map, PAGE_SIZE);
279 	pmap_qenter(kva, &m, 1);
280 	return (kva);
281 }
282 
283 void
284 vm_pager_unmap_page(kva)
285 	vm_offset_t kva;
286 {
287 
288 	pmap_qremove(kva, 1);
289 	kmem_free_wakeup(pager_map, kva, PAGE_SIZE);
290 }
291 
292 vm_object_t
293 vm_pager_object_lookup(pg_list, handle)
294 	struct pagerlst *pg_list;
295 	void *handle;
296 {
297 	vm_object_t object;
298 
299 	TAILQ_FOREACH(object, pg_list, pager_object_list)
300 		if (object->handle == handle)
301 			return (object);
302 	return (NULL);
303 }
304 
305 /*
306  * initialize a physical buffer
307  */
308 
309 /*
310  * XXX This probably belongs in vfs_bio.c
311  */
312 static void
313 initpbuf(struct buf *bp)
314 {
315 	bp->b_rcred = NOCRED;
316 	bp->b_wcred = NOCRED;
317 	bp->b_qindex = 0;	/* On no queue (QUEUE_NONE) */
318 	bp->b_saveaddr = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva;
319 	bp->b_data = bp->b_saveaddr;
320 	bp->b_kvabase = bp->b_saveaddr;
321 	bp->b_kvasize = MAXPHYS;
322 	bp->b_xflags = 0;
323 	bp->b_flags = 0;
324 	bp->b_ioflags = 0;
325 	bp->b_iodone = NULL;
326 	bp->b_error = 0;
327 	bp->b_magic = B_MAGIC_BIO;
328 	bp->b_op = &buf_ops_bio;
329 	BUF_LOCK(bp, LK_EXCLUSIVE, NULL);
330 }
331 
332 /*
333  * allocate a physical buffer
334  *
335  *	There are a limited number (nswbuf) of physical buffers.  We need
336  *	to make sure that no single subsystem is able to hog all of them,
337  *	so each subsystem implements a counter which is typically initialized
338  *	to 1/2 nswbuf.  getpbuf() decrements this counter in allocation and
339  *	increments it on release, and blocks if the counter hits zero.  A
340  *	subsystem may initialize the counter to -1 to disable the feature,
341  *	but it must still be sure to match up all uses of getpbuf() with
342  *	relpbuf() using the same variable.
343  *
344  *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
345  *	relatively soon when the rest of the subsystems get smart about it. XXX
346  */
347 struct buf *
348 getpbuf(pfreecnt)
349 	int *pfreecnt;
350 {
351 	int s;
352 	struct buf *bp;
353 
354 	s = splvm();
355 	mtx_lock(&pbuf_mtx);
356 
357 	for (;;) {
358 		if (pfreecnt) {
359 			while (*pfreecnt == 0) {
360 				msleep(pfreecnt, &pbuf_mtx, PVM, "wswbuf0", 0);
361 			}
362 		}
363 
364 		/* get a bp from the swap buffer header pool */
365 		if ((bp = TAILQ_FIRST(&bswlist)) != NULL)
366 			break;
367 
368 		bswneeded = 1;
369 		msleep(&bswneeded, &pbuf_mtx, PVM, "wswbuf1", 0);
370 		/* loop in case someone else grabbed one */
371 	}
372 	TAILQ_REMOVE(&bswlist, bp, b_freelist);
373 	if (pfreecnt)
374 		--*pfreecnt;
375 	mtx_unlock(&pbuf_mtx);
376 	splx(s);
377 
378 	initpbuf(bp);
379 	return bp;
380 }
381 
382 /*
383  * allocate a physical buffer, if one is available.
384  *
385  *	Note that there is no NULL hack here - all subsystems using this
386  *	call understand how to use pfreecnt.
387  */
388 struct buf *
389 trypbuf(pfreecnt)
390 	int *pfreecnt;
391 {
392 	int s;
393 	struct buf *bp;
394 
395 	s = splvm();
396 	mtx_lock(&pbuf_mtx);
397 	if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) {
398 		mtx_unlock(&pbuf_mtx);
399 		splx(s);
400 		return NULL;
401 	}
402 	TAILQ_REMOVE(&bswlist, bp, b_freelist);
403 
404 	--*pfreecnt;
405 
406 	mtx_unlock(&pbuf_mtx);
407 	splx(s);
408 
409 	initpbuf(bp);
410 
411 	return bp;
412 }
413 
414 /*
415  * release a physical buffer
416  *
417  *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
418  *	relatively soon when the rest of the subsystems get smart about it. XXX
419  */
420 void
421 relpbuf(bp, pfreecnt)
422 	struct buf *bp;
423 	int *pfreecnt;
424 {
425 	int s;
426 
427 	s = splvm();
428 
429 	if (bp->b_rcred != NOCRED) {
430 		crfree(bp->b_rcred);
431 		bp->b_rcred = NOCRED;
432 	}
433 	if (bp->b_wcred != NOCRED) {
434 		crfree(bp->b_wcred);
435 		bp->b_wcred = NOCRED;
436 	}
437 
438 	if (bp->b_vp)
439 		pbrelvp(bp);
440 
441 	BUF_UNLOCK(bp);
442 
443 	mtx_lock(&pbuf_mtx);
444 	TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist);
445 
446 	if (bswneeded) {
447 		bswneeded = 0;
448 		wakeup(&bswneeded);
449 	}
450 	if (pfreecnt) {
451 		if (++*pfreecnt == 1)
452 			wakeup(pfreecnt);
453 	}
454 	mtx_unlock(&pbuf_mtx);
455 	splx(s);
456 }
457