xref: /dragonfly/sys/vm/vm_pager.c (revision e65bc1c3)
1 /*
2  * (MPSAFE)
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  * $FreeBSD: src/sys/vm/vm_pager.c,v 1.54.2.2 2001/11/18 07:11:00 dillon Exp $
63  */
64 
65 /*
66  *	Paging space routine stubs.  Emulates a matchmaker-like interface
67  *	for builtin pagers.
68  */
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/vnode.h>
74 #include <sys/buf.h>
75 #include <sys/ucred.h>
76 #include <sys/dsched.h>
77 #include <sys/proc.h>
78 #include <sys/sysctl.h>
79 #include <sys/thread2.h>
80 
81 #include <vm/vm.h>
82 #include <vm/vm_param.h>
83 #include <vm/vm_kern.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 #include <sys/buf2.h>
90 
91 extern struct pagerops defaultpagerops;
92 extern struct pagerops swappagerops;
93 extern struct pagerops vnodepagerops;
94 extern struct pagerops devicepagerops;
95 extern struct pagerops physpagerops;
96 
97 int cluster_pbuf_freecnt = -1;	/* unlimited to begin with */
98 
99 static int dead_pager_getpage (vm_object_t, vm_page_t *, int);
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);
102 static void dead_pager_dealloc (vm_object_t);
103 
104 /*
105  * No requirements.
106  */
107 static int
108 dead_pager_getpage(vm_object_t obj, vm_page_t *mpp, int seqaccess)
109 {
110 	return VM_PAGER_FAIL;
111 }
112 
113 /*
114  * No requirements.
115  */
116 static void
117 dead_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
118 		    int *rtvals)
119 {
120 	int i;
121 
122 	for (i = 0; i < count; i++) {
123 		rtvals[i] = VM_PAGER_AGAIN;
124 	}
125 }
126 
127 /*
128  * No requirements.
129  */
130 static int
131 dead_pager_haspage(vm_object_t object, vm_pindex_t pindex)
132 {
133 	return FALSE;
134 }
135 
136 /*
137  * No requirements.
138  */
139 static void
140 dead_pager_dealloc(vm_object_t object)
141 {
142 	KKASSERT(object->swblock_count == 0);
143 	return;
144 }
145 
146 static struct pagerops deadpagerops = {
147 	dead_pager_dealloc,
148 	dead_pager_getpage,
149 	dead_pager_putpages,
150 	dead_pager_haspage
151 };
152 
153 struct pagerops *pagertab[] = {
154 	&defaultpagerops,	/* OBJT_DEFAULT */
155 	&swappagerops,		/* OBJT_SWAP */
156 	&vnodepagerops,		/* OBJT_VNODE */
157 	&devicepagerops,	/* OBJT_DEVICE */
158 	&physpagerops,		/* OBJT_PHYS */
159 	&deadpagerops		/* OBJT_DEAD */
160 };
161 
162 int npagers = NELEM(pagertab);
163 
164 /*
165  * Kernel address space for mapping pages.
166  * Used by pagers where KVAs are needed for IO.
167  *
168  * XXX needs to be large enough to support the number of pending async
169  * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
170  * (MAXPHYS == 64k) if you want to get the most efficiency.
171  */
172 #define PAGER_MAP_SIZE	(8 * 1024 * 1024)
173 
174 TAILQ_HEAD(swqueue, buf);
175 
176 int pager_map_size = PAGER_MAP_SIZE;
177 struct vm_map pager_map;
178 
179 static int bswneeded_raw;
180 static int bswneeded_kva;
181 static long nswbuf_raw;
182 static struct buf *swbuf_raw;
183 static vm_offset_t swapbkva;		/* swap buffers kva */
184 static struct swqueue bswlist_raw;	/* without kva */
185 static struct swqueue bswlist_kva;	/* with kva */
186 static struct spinlock bswspin = SPINLOCK_INITIALIZER(&bswspin);
187 static int pbuf_raw_count;
188 static int pbuf_kva_count;
189 
190 SYSCTL_INT(_vfs, OID_AUTO, pbuf_raw_count, CTLFLAG_RD, &pbuf_raw_count, 0,
191     "Kernel virtual address space reservations");
192 SYSCTL_INT(_vfs, OID_AUTO, pbuf_kva_count, CTLFLAG_RD, &pbuf_kva_count, 0,
193     "Kernel raw address space reservations");
194 
195 /*
196  * Initialize the swap buffer list.
197  *
198  * Called from the low level boot code only.
199  */
200 static void
201 vm_pager_init(void *arg __unused)
202 {
203 	TAILQ_INIT(&bswlist_raw);
204 	TAILQ_INIT(&bswlist_kva);
205 }
206 SYSINIT(vm_mem, SI_BOOT1_VM, SI_ORDER_SECOND, vm_pager_init, NULL)
207 
208 /*
209  * Called from the low level boot code only.
210  */
211 void
212 vm_pager_bufferinit(void)
213 {
214 	struct buf *bp;
215 	long i;
216 
217 	/*
218 	 * Reserve KVM space for pbuf data.
219 	 */
220 	swapbkva = kmem_alloc_pageable(&pager_map, nswbuf * MAXPHYS);
221 	if (!swapbkva)
222 		panic("Not enough pager_map VM space for physical buffers");
223 
224 	/*
225 	 * Initial pbuf setup.  These pbufs have KVA reservations.
226 	 */
227 	bp = swbuf;
228 	for (i = 0; i < nswbuf; ++i, ++bp) {
229 		bp->b_kvabase = (caddr_t)((intptr_t)i * MAXPHYS) + swapbkva;
230 		bp->b_kvasize = MAXPHYS;
231 		BUF_LOCKINIT(bp);
232 		buf_dep_init(bp);
233 		TAILQ_INSERT_HEAD(&bswlist_kva, bp, b_freelist);
234 		++pbuf_kva_count;
235 	}
236 
237 	/*
238 	 * Initial pbuf setup.  These pbufs do not have KVA reservations,
239 	 * so we can have a lot more of them.  These are typically used
240 	 * to massage low level buf/bio requests.
241 	 */
242 	nswbuf_raw = nbuf * 2;
243 	swbuf_raw = (void *)kmem_alloc(&kernel_map,
244 				round_page(nswbuf_raw * sizeof(struct buf)));
245 	bp = swbuf_raw;
246 	for (i = 0; i < nswbuf_raw; ++i, ++bp) {
247 		BUF_LOCKINIT(bp);
248 		buf_dep_init(bp);
249 		TAILQ_INSERT_HEAD(&bswlist_raw, bp, b_freelist);
250 		++pbuf_raw_count;
251 	}
252 
253 	/*
254 	 * Allow the clustering code to use half of our pbufs.
255 	 */
256 	cluster_pbuf_freecnt = nswbuf / 2;
257 }
258 
259 /*
260  * No requirements.
261  */
262 void
263 vm_pager_deallocate(vm_object_t object)
264 {
265 	(*pagertab[object->type]->pgo_dealloc) (object);
266 }
267 
268 /*
269  * vm_pager_get_pages() - inline, see vm/vm_pager.h
270  * vm_pager_put_pages() - inline, see vm/vm_pager.h
271  * vm_pager_has_page() - inline, see vm/vm_pager.h
272  * vm_pager_page_inserted() - inline, see vm/vm_pager.h
273  * vm_pager_page_removed() - inline, see vm/vm_pager.h
274  */
275 
276 #if 0
277 /*
278  *	vm_pager_sync:
279  *
280  *	Called by pageout daemon before going back to sleep.
281  *	Gives pagers a chance to clean up any completed async pageing
282  *	operations.
283  */
284 void
285 vm_pager_sync(void)
286 {
287 	struct pagerops **pgops;
288 
289 	for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++)
290 		if (pgops && ((*pgops)->pgo_sync != NULL))
291 			(*(*pgops)->pgo_sync) ();
292 }
293 
294 #endif
295 
296 /*
297  * Initialize a physical buffer.
298  *
299  * No requirements.
300  */
301 static void
302 initpbuf(struct buf *bp)
303 {
304 	bp->b_qindex = 0;		/* BQUEUE_NONE */
305 	bp->b_data = bp->b_kvabase;	/* NULL if pbuf sans kva */
306 	bp->b_flags = B_PAGING;
307 	bp->b_cmd = BUF_CMD_DONE;
308 	bp->b_error = 0;
309 	bp->b_bcount = 0;
310 	bp->b_bufsize = MAXPHYS;
311 	initbufbio(bp);
312 	xio_init(&bp->b_xio);
313 	BUF_LOCK(bp, LK_EXCLUSIVE);
314 }
315 
316 /*
317  * Allocate a physical buffer
318  *
319  *	There are a limited number (nswbuf) of physical buffers.  We need
320  *	to make sure that no single subsystem is able to hog all of them,
321  *	so each subsystem implements a counter which is typically initialized
322  *	to 1/2 nswbuf.  getpbuf() decrements this counter in allocation and
323  *	increments it on release, and blocks if the counter hits zero.  A
324  *	subsystem may initialize the counter to -1 to disable the feature,
325  *	but it must still be sure to match up all uses of getpbuf() with
326  *	relpbuf() using the same variable.
327  *
328  *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
329  *	relatively soon when the rest of the subsystems get smart about it. XXX
330  *
331  *	Physical buffers can be with or without KVA space reserved.  There
332  *	are severe limitations on the ones with KVA reserved, and fewer
333  *	limitations on the ones without.  getpbuf() gets one without,
334  *	getpbuf_kva() gets one with.
335  *
336  * No requirements.
337  */
338 struct buf *
339 getpbuf(int *pfreecnt)
340 {
341 	struct buf *bp;
342 
343 	spin_lock(&bswspin);
344 
345 	for (;;) {
346 		if (pfreecnt) {
347 			while (*pfreecnt == 0)
348 				ssleep(pfreecnt, &bswspin, 0, "wswbuf0", 0);
349 		}
350 
351 		/* get a bp from the swap buffer header pool */
352 		if ((bp = TAILQ_FIRST(&bswlist_raw)) != NULL)
353 			break;
354 		bswneeded_raw = 1;
355 		ssleep(&bswneeded_raw, &bswspin, 0, "wswbuf1", 0);
356 		/* loop in case someone else grabbed one */
357 	}
358 	TAILQ_REMOVE(&bswlist_raw, bp, b_freelist);
359 	--pbuf_raw_count;
360 	if (pfreecnt)
361 		--*pfreecnt;
362 
363 	spin_unlock(&bswspin);
364 
365 	initpbuf(bp);
366 	KKASSERT(dsched_is_clear_buf_priv(bp));
367 
368 	return (bp);
369 }
370 
371 struct buf *
372 getpbuf_kva(int *pfreecnt)
373 {
374 	struct buf *bp;
375 
376 	spin_lock(&bswspin);
377 
378 	for (;;) {
379 		if (pfreecnt) {
380 			while (*pfreecnt == 0)
381 				ssleep(pfreecnt, &bswspin, 0, "wswbuf0", 0);
382 		}
383 
384 		/* get a bp from the swap buffer header pool */
385 		if ((bp = TAILQ_FIRST(&bswlist_kva)) != NULL)
386 			break;
387 		bswneeded_kva = 1;
388 		ssleep(&bswneeded_kva, &bswspin, 0, "wswbuf1", 0);
389 		/* loop in case someone else grabbed one */
390 	}
391 	TAILQ_REMOVE(&bswlist_kva, bp, b_freelist);
392 	--pbuf_kva_count;
393 	if (pfreecnt)
394 		--*pfreecnt;
395 
396 	spin_unlock(&bswspin);
397 
398 	initpbuf(bp);
399 	KKASSERT(dsched_is_clear_buf_priv(bp));
400 
401 	return (bp);
402 }
403 
404 /*
405  * Allocate a physical buffer, if one is available.
406  *
407  *	Note that there is no NULL hack here - all subsystems using this
408  *	call understand how to use pfreecnt.
409  *
410  * No requirements.
411  */
412 struct buf *
413 trypbuf(int *pfreecnt)
414 {
415 	struct buf *bp;
416 
417 	spin_lock(&bswspin);
418 
419 	if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist_raw)) == NULL) {
420 		spin_unlock(&bswspin);
421 		return NULL;
422 	}
423 	TAILQ_REMOVE(&bswlist_raw, bp, b_freelist);
424 	--pbuf_raw_count;
425 	--*pfreecnt;
426 
427 	spin_unlock(&bswspin);
428 
429 	initpbuf(bp);
430 
431 	return bp;
432 }
433 
434 struct buf *
435 trypbuf_kva(int *pfreecnt)
436 {
437 	struct buf *bp;
438 
439 	spin_lock(&bswspin);
440 
441 	if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist_kva)) == NULL) {
442 		spin_unlock(&bswspin);
443 		return NULL;
444 	}
445 	TAILQ_REMOVE(&bswlist_kva, bp, b_freelist);
446 	--pbuf_kva_count;
447 	--*pfreecnt;
448 
449 	spin_unlock(&bswspin);
450 
451 	initpbuf(bp);
452 
453 	return bp;
454 }
455 
456 /*
457  * Release a physical buffer
458  *
459  *	NOTE: pfreecnt can be NULL, but this 'feature' will be removed
460  *	relatively soon when the rest of the subsystems get smart about it. XXX
461  *
462  * No requirements.
463  */
464 void
465 relpbuf(struct buf *bp, int *pfreecnt)
466 {
467 	int wake_bsw_kva = 0;
468 	int wake_bsw_raw = 0;
469 	int wake_freecnt = 0;
470 
471 	KKASSERT(bp->b_flags & B_PAGING);
472 	dsched_exit_buf(bp);
473 
474 	BUF_UNLOCK(bp);
475 
476 	spin_lock(&bswspin);
477 	if (bp->b_kvabase) {
478 		TAILQ_INSERT_HEAD(&bswlist_kva, bp, b_freelist);
479 		++pbuf_kva_count;
480 	} else {
481 		TAILQ_INSERT_HEAD(&bswlist_raw, bp, b_freelist);
482 		++pbuf_raw_count;
483 	}
484 	if (bswneeded_kva) {
485 		bswneeded_kva = 0;
486 		wake_bsw_kva = 1;
487 	}
488 	if (bswneeded_raw) {
489 		bswneeded_raw = 0;
490 		wake_bsw_raw = 1;
491 	}
492 	if (pfreecnt) {
493 		if (++*pfreecnt == 1)
494 			wake_freecnt = 1;
495 	}
496 	spin_unlock(&bswspin);
497 
498 	if (wake_bsw_kva)
499 		wakeup(&bswneeded_kva);
500 	if (wake_bsw_raw)
501 		wakeup(&bswneeded_raw);
502 	if (wake_freecnt)
503 		wakeup(pfreecnt);
504 }
505 
506 void
507 pbuf_adjcount(int *pfreecnt, int n)
508 {
509 	if (n) {
510 		spin_lock(&bswspin);
511 		*pfreecnt += n;
512 		spin_unlock(&bswspin);
513 		wakeup(pfreecnt);
514 	}
515 }
516