kernel - Remove dsched
[dragonfly.git] / sys / vm / vm_pager.c
CommitLineData
984263bc 1/*
99ad9bc4
MD
2 * (MPSAFE)
3 *
984263bc
MD
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.
dc71b7ab 18 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
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>
8c72e3d5 76#include <sys/dsched.h>
984263bc 77#include <sys/proc.h>
9a82e536 78#include <sys/sysctl.h>
cdd46d2e 79#include <sys/thread2.h>
984263bc
MD
80
81#include <vm/vm.h>
82#include <vm/vm_param.h>
9a82e536 83#include <vm/vm_kern.h>
984263bc
MD
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
3020e3be
MD
89#include <sys/buf2.h>
90
984263bc
MD
91extern struct pagerops defaultpagerops;
92extern struct pagerops swappagerops;
93extern struct pagerops vnodepagerops;
94extern struct pagerops devicepagerops;
95extern struct pagerops physpagerops;
96
97int cluster_pbuf_freecnt = -1; /* unlimited to begin with */
98
1b9d3514 99static int dead_pager_getpage (vm_object_t, vm_page_t *, int);
1388df65 100static void dead_pager_putpages (vm_object_t, vm_page_t *, int, int, int *);
1b9d3514 101static boolean_t dead_pager_haspage (vm_object_t, vm_pindex_t);
1388df65 102static void dead_pager_dealloc (vm_object_t);
984263bc 103
99ad9bc4
MD
104/*
105 * No requirements.
106 */
984263bc 107static int
1b9d3514 108dead_pager_getpage(vm_object_t obj, vm_page_t *mpp, int seqaccess)
984263bc
MD
109{
110 return VM_PAGER_FAIL;
111}
112
99ad9bc4
MD
113/*
114 * No requirements.
115 */
984263bc 116static void
57e43348 117dead_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
5a648714 118 int *rtvals)
984263bc
MD
119{
120 int i;
121
122 for (i = 0; i < count; i++) {
123 rtvals[i] = VM_PAGER_AGAIN;
124 }
125}
126
99ad9bc4
MD
127/*
128 * No requirements.
129 */
99ebfb7c 130static boolean_t
1b9d3514 131dead_pager_haspage(vm_object_t object, vm_pindex_t pindex)
984263bc 132{
984263bc
MD
133 return FALSE;
134}
135
99ad9bc4
MD
136/*
137 * No requirements.
138 */
984263bc 139static void
57e43348 140dead_pager_dealloc(vm_object_t object)
984263bc 141{
8d292090 142 KKASSERT(object->swblock_count == 0);
984263bc
MD
143 return;
144}
145
146static struct pagerops deadpagerops = {
984263bc 147 dead_pager_dealloc,
1b9d3514 148 dead_pager_getpage,
984263bc 149 dead_pager_putpages,
107e9bcc 150 dead_pager_haspage
984263bc
MD
151};
152
153struct pagerops *pagertab[] = {
154 &defaultpagerops, /* OBJT_DEFAULT */
155 &swappagerops, /* OBJT_SWAP */
156 &vnodepagerops, /* OBJT_VNODE */
157 &devicepagerops, /* OBJT_DEVICE */
f6a0c819 158 &devicepagerops, /* OBJT_MGTDEVICE */
984263bc
MD
159 &physpagerops, /* OBJT_PHYS */
160 &deadpagerops /* OBJT_DEAD */
161};
162
c157ff7a 163int npagers = NELEM(pagertab);
984263bc
MD
164
165/*
166 * Kernel address space for mapping pages.
167 * Used by pagers where KVAs are needed for IO.
168 *
169 * XXX needs to be large enough to support the number of pending async
170 * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size
171 * (MAXPHYS == 64k) if you want to get the most efficiency.
172 */
173#define PAGER_MAP_SIZE (8 * 1024 * 1024)
174
9a82e536
MD
175TAILQ_HEAD(swqueue, buf);
176
984263bc 177int pager_map_size = PAGER_MAP_SIZE;
e4846942
MD
178struct vm_map pager_map;
179
9a82e536
MD
180static int bswneeded_raw;
181static int bswneeded_kva;
74d62460 182static long nswbuf_raw;
9a82e536 183static struct buf *swbuf_raw;
984263bc 184static vm_offset_t swapbkva; /* swap buffers kva */
9a82e536
MD
185static struct swqueue bswlist_raw; /* without kva */
186static struct swqueue bswlist_kva; /* with kva */
ba87a4ab 187static struct spinlock bswspin = SPINLOCK_INITIALIZER(&bswspin, "bswspin");
9a82e536
MD
188static int pbuf_raw_count;
189static int pbuf_kva_count;
190
093e85dc
SG
191SYSCTL_INT(_vfs, OID_AUTO, pbuf_raw_count, CTLFLAG_RD, &pbuf_raw_count, 0,
192 "Kernel virtual address space reservations");
193SYSCTL_INT(_vfs, OID_AUTO, pbuf_kva_count, CTLFLAG_RD, &pbuf_kva_count, 0,
194 "Kernel raw address space reservations");
984263bc 195
99ad9bc4
MD
196/*
197 * Initialize the swap buffer list.
198 *
199 * Called from the low level boot code only.
200 */
107e9bcc
MD
201static void
202vm_pager_init(void *arg __unused)
984263bc 203{
9a82e536
MD
204 TAILQ_INIT(&bswlist_raw);
205 TAILQ_INIT(&bswlist_kva);
984263bc 206}
f3f3eadb 207SYSINIT(vm_mem, SI_BOOT1_VM, SI_ORDER_SECOND, vm_pager_init, NULL);
984263bc 208
99ad9bc4
MD
209/*
210 * Called from the low level boot code only.
211 */
ca667d3c 212static
984263bc 213void
ca667d3c 214vm_pager_bufferinit(void *dummy __unused)
984263bc
MD
215{
216 struct buf *bp;
74d62460 217 long i;
984263bc 218
984263bc 219 /*
312dcd01
MD
220 * Reserve KVM space for pbuf data.
221 */
e4846942 222 swapbkva = kmem_alloc_pageable(&pager_map, nswbuf * MAXPHYS);
312dcd01
MD
223 if (!swapbkva)
224 panic("Not enough pager_map VM space for physical buffers");
225
226 /*
9a82e536 227 * Initial pbuf setup. These pbufs have KVA reservations.
984263bc 228 */
312dcd01
MD
229 bp = swbuf;
230 for (i = 0; i < nswbuf; ++i, ++bp) {
973c11b9 231 bp->b_kvabase = (caddr_t)((intptr_t)i * MAXPHYS) + swapbkva;
312dcd01 232 bp->b_kvasize = MAXPHYS;
984263bc 233 BUF_LOCKINIT(bp);
408357d8 234 buf_dep_init(bp);
9a82e536
MD
235 TAILQ_INSERT_HEAD(&bswlist_kva, bp, b_freelist);
236 ++pbuf_kva_count;
237 }
238
239 /*
240 * Initial pbuf setup. These pbufs do not have KVA reservations,
241 * so we can have a lot more of them. These are typically used
242 * to massage low level buf/bio requests.
06c66eb2
MD
243 *
244 * NOTE: We use KM_NOTLBSYNC here to reduce unnecessary IPIs
245 * during startup, which can really slow down emulated
246 * systems.
9a82e536
MD
247 */
248 nswbuf_raw = nbuf * 2;
06c66eb2
MD
249 swbuf_raw = (void *)kmem_alloc3(&kernel_map,
250 round_page(nswbuf_raw * sizeof(struct buf)),
251 KM_NOTLBSYNC);
252 smp_invltlb();
9a82e536
MD
253 bp = swbuf_raw;
254 for (i = 0; i < nswbuf_raw; ++i, ++bp) {
255 BUF_LOCKINIT(bp);
256 buf_dep_init(bp);
257 TAILQ_INSERT_HEAD(&bswlist_raw, bp, b_freelist);
258 ++pbuf_raw_count;
984263bc
MD
259 }
260
312dcd01
MD
261 /*
262 * Allow the clustering code to use half of our pbufs.
263 */
984263bc 264 cluster_pbuf_freecnt = nswbuf / 2;
984263bc
MD
265}
266
ca667d3c
MD
267SYSINIT(do_vmpg, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, vm_pager_bufferinit, NULL);
268
99ad9bc4
MD
269/*
270 * No requirements.
271 */
984263bc 272void
57e43348 273vm_pager_deallocate(vm_object_t object)
984263bc
MD
274{
275 (*pagertab[object->type]->pgo_dealloc) (object);
276}
277
984263bc
MD
278/*
279 * vm_pager_get_pages() - inline, see vm/vm_pager.h
280 * vm_pager_put_pages() - inline, see vm/vm_pager.h
281 * vm_pager_has_page() - inline, see vm/vm_pager.h
282 * vm_pager_page_inserted() - inline, see vm/vm_pager.h
283 * vm_pager_page_removed() - inline, see vm/vm_pager.h
284 */
285
984263bc 286/*
f2c2051e
JH
287 * Search the specified pager object list for an object with the
288 * specified handle. If an object with the specified handle is found,
289 * increase its reference count and return it. Otherwise, return NULL.
984263bc 290 *
f2c2051e 291 * The pager object list must be locked.
984263bc 292 */
f2c2051e
JH
293vm_object_t
294vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
984263bc 295{
f2c2051e
JH
296 vm_object_t object;
297
298 TAILQ_FOREACH(object, pg_list, pager_object_list) {
299 if (object->handle == handle) {
300 VM_OBJECT_LOCK(object);
301 if ((object->flags & OBJ_DEAD) == 0) {
302 vm_object_reference_locked(object);
303 VM_OBJECT_UNLOCK(object);
304 break;
305 }
306 VM_OBJECT_UNLOCK(object);
307 }
308 }
309 return (object);
984263bc
MD
310}
311
984263bc 312/*
312dcd01 313 * Initialize a physical buffer.
99ad9bc4
MD
314 *
315 * No requirements.
984263bc 316 */
984263bc
MD
317static void
318initpbuf(struct buf *bp)
319{
9a82e536
MD
320 bp->b_qindex = 0; /* BQUEUE_NONE */
321 bp->b_data = bp->b_kvabase; /* NULL if pbuf sans kva */
a8f169e2 322 bp->b_flags = B_PAGING;
10f3fee5 323 bp->b_cmd = BUF_CMD_DONE;
984263bc 324 bp->b_error = 0;
9a71d53f
MD
325 bp->b_bcount = 0;
326 bp->b_bufsize = MAXPHYS;
81b5c339 327 initbufbio(bp);
54f51aeb 328 xio_init(&bp->b_xio);
984263bc
MD
329 BUF_LOCK(bp, LK_EXCLUSIVE);
330}
331
332/*
a50fe973 333 * Allocate a physical buffer
984263bc
MD
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
a50fe973 346 *
9a82e536
MD
347 * Physical buffers can be with or without KVA space reserved. There
348 * are severe limitations on the ones with KVA reserved, and fewer
349 * limitations on the ones without. getpbuf() gets one without,
350 * getpbuf_kva() gets one with.
351 *
99ad9bc4 352 * No requirements.
984263bc
MD
353 */
354struct buf *
57e43348 355getpbuf(int *pfreecnt)
984263bc 356{
984263bc
MD
357 struct buf *bp;
358
287a8577 359 spin_lock(&bswspin);
984263bc
MD
360
361 for (;;) {
362 if (pfreecnt) {
a50fe973 363 while (*pfreecnt == 0)
e590ee86 364 ssleep(pfreecnt, &bswspin, 0, "wswbuf0", 0);
984263bc
MD
365 }
366
367 /* get a bp from the swap buffer header pool */
9a82e536 368 if ((bp = TAILQ_FIRST(&bswlist_raw)) != NULL)
984263bc 369 break;
9a82e536
MD
370 bswneeded_raw = 1;
371 ssleep(&bswneeded_raw, &bswspin, 0, "wswbuf1", 0);
984263bc
MD
372 /* loop in case someone else grabbed one */
373 }
9a82e536
MD
374 TAILQ_REMOVE(&bswlist_raw, bp, b_freelist);
375 --pbuf_raw_count;
984263bc
MD
376 if (pfreecnt)
377 --*pfreecnt;
a50fe973 378
287a8577 379 spin_unlock(&bswspin);
984263bc
MD
380
381 initpbuf(bp);
9a82e536
MD
382
383 return (bp);
384}
385
386struct buf *
387getpbuf_kva(int *pfreecnt)
388{
389 struct buf *bp;
390
287a8577 391 spin_lock(&bswspin);
9a82e536
MD
392
393 for (;;) {
394 if (pfreecnt) {
395 while (*pfreecnt == 0)
396 ssleep(pfreecnt, &bswspin, 0, "wswbuf0", 0);
397 }
398
399 /* get a bp from the swap buffer header pool */
400 if ((bp = TAILQ_FIRST(&bswlist_kva)) != NULL)
401 break;
402 bswneeded_kva = 1;
403 ssleep(&bswneeded_kva, &bswspin, 0, "wswbuf1", 0);
404 /* loop in case someone else grabbed one */
405 }
406 TAILQ_REMOVE(&bswlist_kva, bp, b_freelist);
407 --pbuf_kva_count;
408 if (pfreecnt)
409 --*pfreecnt;
410
287a8577 411 spin_unlock(&bswspin);
9a82e536
MD
412
413 initpbuf(bp);
9a82e536
MD
414
415 return (bp);
984263bc
MD
416}
417
418/*
a50fe973 419 * Allocate a physical buffer, if one is available.
984263bc
MD
420 *
421 * Note that there is no NULL hack here - all subsystems using this
422 * call understand how to use pfreecnt.
a50fe973 423 *
99ad9bc4 424 * No requirements.
984263bc
MD
425 */
426struct buf *
57e43348 427trypbuf(int *pfreecnt)
984263bc 428{
984263bc
MD
429 struct buf *bp;
430
287a8577 431 spin_lock(&bswspin);
a50fe973 432
9a82e536 433 if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist_raw)) == NULL) {
287a8577 434 spin_unlock(&bswspin);
9a82e536
MD
435 return NULL;
436 }
437 TAILQ_REMOVE(&bswlist_raw, bp, b_freelist);
438 --pbuf_raw_count;
439 --*pfreecnt;
440
287a8577 441 spin_unlock(&bswspin);
9a82e536
MD
442
443 initpbuf(bp);
444
445 return bp;
446}
447
448struct buf *
449trypbuf_kva(int *pfreecnt)
450{
451 struct buf *bp;
452
287a8577 453 spin_lock(&bswspin);
9a82e536
MD
454
455 if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist_kva)) == NULL) {
287a8577 456 spin_unlock(&bswspin);
984263bc
MD
457 return NULL;
458 }
9a82e536
MD
459 TAILQ_REMOVE(&bswlist_kva, bp, b_freelist);
460 --pbuf_kva_count;
984263bc
MD
461 --*pfreecnt;
462
287a8577 463 spin_unlock(&bswspin);
984263bc
MD
464
465 initpbuf(bp);
466
467 return bp;
468}
469
470/*
a50fe973 471 * Release a physical buffer
984263bc
MD
472 *
473 * NOTE: pfreecnt can be NULL, but this 'feature' will be removed
474 * relatively soon when the rest of the subsystems get smart about it. XXX
a50fe973 475 *
99ad9bc4 476 * No requirements.
984263bc
MD
477 */
478void
57e43348 479relpbuf(struct buf *bp, int *pfreecnt)
984263bc 480{
9a82e536
MD
481 int wake_bsw_kva = 0;
482 int wake_bsw_raw = 0;
a50fe973 483 int wake_freecnt = 0;
984263bc 484
a8f169e2 485 KKASSERT(bp->b_flags & B_PAGING);
3573cf7b 486 dsched_buf_exit(bp);
984263bc 487
a50fe973 488 BUF_UNLOCK(bp);
7757d1c2 489
287a8577 490 spin_lock(&bswspin);
9a82e536
MD
491 if (bp->b_kvabase) {
492 TAILQ_INSERT_HEAD(&bswlist_kva, bp, b_freelist);
493 ++pbuf_kva_count;
494 } else {
495 TAILQ_INSERT_HEAD(&bswlist_raw, bp, b_freelist);
496 ++pbuf_raw_count;
497 }
498 if (bswneeded_kva) {
499 bswneeded_kva = 0;
500 wake_bsw_kva = 1;
501 }
502 if (bswneeded_raw) {
503 bswneeded_raw = 0;
504 wake_bsw_raw = 1;
984263bc
MD
505 }
506 if (pfreecnt) {
507 if (++*pfreecnt == 1)
a50fe973 508 wake_freecnt = 1;
984263bc 509 }
287a8577 510 spin_unlock(&bswspin);
a50fe973 511
9a82e536
MD
512 if (wake_bsw_kva)
513 wakeup(&bswneeded_kva);
514 if (wake_bsw_raw)
515 wakeup(&bswneeded_raw);
a50fe973
MD
516 if (wake_freecnt)
517 wakeup(pfreecnt);
984263bc 518}
7f86d367
MD
519
520void
521pbuf_adjcount(int *pfreecnt, int n)
522{
523 if (n) {
524 spin_lock(&bswspin);
525 *pfreecnt += n;
526 spin_unlock(&bswspin);
527 wakeup(pfreecnt);
528 }
529}