kernel: Fix some boolean_t vs. int confusion.
[dragonfly.git] / sys / vm / vm_pager.c
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 boolean_t
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         &devicepagerops,        /* OBJT_MGTDEVICE */
159         &physpagerops,          /* OBJT_PHYS */
160         &deadpagerops           /* OBJT_DEAD */
161 };
162
163 int npagers = NELEM(pagertab);
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
175 TAILQ_HEAD(swqueue, buf);
176
177 int pager_map_size = PAGER_MAP_SIZE;
178 struct vm_map pager_map;
179
180 static int bswneeded_raw;
181 static int bswneeded_kva;
182 static long nswbuf_raw;
183 static struct buf *swbuf_raw;
184 static vm_offset_t swapbkva;            /* swap buffers kva */
185 static struct swqueue bswlist_raw;      /* without kva */
186 static struct swqueue bswlist_kva;      /* with kva */
187 static struct spinlock bswspin = SPINLOCK_INITIALIZER(&bswspin);
188 static int pbuf_raw_count;
189 static int pbuf_kva_count;
190
191 SYSCTL_INT(_vfs, OID_AUTO, pbuf_raw_count, CTLFLAG_RD, &pbuf_raw_count, 0,
192     "Kernel virtual address space reservations");
193 SYSCTL_INT(_vfs, OID_AUTO, pbuf_kva_count, CTLFLAG_RD, &pbuf_kva_count, 0,
194     "Kernel raw address space reservations");
195
196 /*
197  * Initialize the swap buffer list.
198  *
199  * Called from the low level boot code only.
200  */
201 static void
202 vm_pager_init(void *arg __unused)
203 {
204         TAILQ_INIT(&bswlist_raw);
205         TAILQ_INIT(&bswlist_kva);
206 }
207 SYSINIT(vm_mem, SI_BOOT1_VM, SI_ORDER_SECOND, vm_pager_init, NULL)
208
209 /*
210  * Called from the low level boot code only.
211  */
212 static
213 void
214 vm_pager_bufferinit(void *dummy __unused)
215 {
216         struct buf *bp;
217         long i;
218
219         /*
220          * Reserve KVM space for pbuf data.
221          */
222         swapbkva = kmem_alloc_pageable(&pager_map, nswbuf * MAXPHYS);
223         if (!swapbkva)
224                 panic("Not enough pager_map VM space for physical buffers");
225
226         /*
227          * Initial pbuf setup.  These pbufs have KVA reservations.
228          */
229         bp = swbuf;
230         for (i = 0; i < nswbuf; ++i, ++bp) {
231                 bp->b_kvabase = (caddr_t)((intptr_t)i * MAXPHYS) + swapbkva;
232                 bp->b_kvasize = MAXPHYS;
233                 BUF_LOCKINIT(bp);
234                 buf_dep_init(bp);
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.
243          */
244         nswbuf_raw = nbuf * 2;
245         swbuf_raw = (void *)kmem_alloc(&kernel_map,
246                                 round_page(nswbuf_raw * sizeof(struct buf)));
247         bp = swbuf_raw;
248         for (i = 0; i < nswbuf_raw; ++i, ++bp) {
249                 BUF_LOCKINIT(bp);
250                 buf_dep_init(bp);
251                 TAILQ_INSERT_HEAD(&bswlist_raw, bp, b_freelist);
252                 ++pbuf_raw_count;
253         }
254
255         /*
256          * Allow the clustering code to use half of our pbufs.
257          */
258         cluster_pbuf_freecnt = nswbuf / 2;
259 }
260
261 SYSINIT(do_vmpg, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, vm_pager_bufferinit, NULL);
262
263 /*
264  * No requirements.
265  */
266 void
267 vm_pager_deallocate(vm_object_t object)
268 {
269         (*pagertab[object->type]->pgo_dealloc) (object);
270 }
271
272 /*
273  * vm_pager_get_pages() - inline, see vm/vm_pager.h
274  * vm_pager_put_pages() - inline, see vm/vm_pager.h
275  * vm_pager_has_page() - inline, see vm/vm_pager.h
276  * vm_pager_page_inserted() - inline, see vm/vm_pager.h
277  * vm_pager_page_removed() - inline, see vm/vm_pager.h
278  */
279
280 /*
281  * Search the specified pager object list for an object with the
282  * specified handle.  If an object with the specified handle is found,
283  * increase its reference count and return it.  Otherwise, return NULL.
284  *
285  * The pager object list must be locked.
286  */
287 vm_object_t
288 vm_pager_object_lookup(struct pagerlst *pg_list, void *handle)
289 {
290         vm_object_t object;
291
292         TAILQ_FOREACH(object, pg_list, pager_object_list) {
293                 if (object->handle == handle) {
294                         VM_OBJECT_LOCK(object);
295                         if ((object->flags & OBJ_DEAD) == 0) {
296                                 vm_object_reference_locked(object);
297                                 VM_OBJECT_UNLOCK(object);
298                                 break;
299                         }
300                         VM_OBJECT_UNLOCK(object);
301                 }
302         }
303         return (object);
304 }
305
306 /*
307  * Initialize a physical buffer.
308  *
309  * No requirements.
310  */
311 static void
312 initpbuf(struct buf *bp)
313 {
314         bp->b_qindex = 0;               /* BQUEUE_NONE */
315         bp->b_data = bp->b_kvabase;     /* NULL if pbuf sans kva */
316         bp->b_flags = B_PAGING;
317         bp->b_cmd = BUF_CMD_DONE;
318         bp->b_error = 0;
319         bp->b_bcount = 0;
320         bp->b_bufsize = MAXPHYS;
321         initbufbio(bp);
322         xio_init(&bp->b_xio);
323         BUF_LOCK(bp, LK_EXCLUSIVE);
324 }
325
326 /*
327  * Allocate a physical buffer
328  *
329  *      There are a limited number (nswbuf) of physical buffers.  We need
330  *      to make sure that no single subsystem is able to hog all of them,
331  *      so each subsystem implements a counter which is typically initialized
332  *      to 1/2 nswbuf.  getpbuf() decrements this counter in allocation and
333  *      increments it on release, and blocks if the counter hits zero.  A
334  *      subsystem may initialize the counter to -1 to disable the feature,
335  *      but it must still be sure to match up all uses of getpbuf() with 
336  *      relpbuf() using the same variable.
337  *
338  *      NOTE: pfreecnt can be NULL, but this 'feature' will be removed
339  *      relatively soon when the rest of the subsystems get smart about it. XXX
340  *
341  *      Physical buffers can be with or without KVA space reserved.  There
342  *      are severe limitations on the ones with KVA reserved, and fewer
343  *      limitations on the ones without.  getpbuf() gets one without,
344  *      getpbuf_kva() gets one with.
345  *
346  * No requirements.
347  */
348 struct buf *
349 getpbuf(int *pfreecnt)
350 {
351         struct buf *bp;
352
353         spin_lock(&bswspin);
354
355         for (;;) {
356                 if (pfreecnt) {
357                         while (*pfreecnt == 0)
358                                 ssleep(pfreecnt, &bswspin, 0, "wswbuf0", 0);
359                 }
360
361                 /* get a bp from the swap buffer header pool */
362                 if ((bp = TAILQ_FIRST(&bswlist_raw)) != NULL)
363                         break;
364                 bswneeded_raw = 1;
365                 ssleep(&bswneeded_raw, &bswspin, 0, "wswbuf1", 0);
366                 /* loop in case someone else grabbed one */
367         }
368         TAILQ_REMOVE(&bswlist_raw, bp, b_freelist);
369         --pbuf_raw_count;
370         if (pfreecnt)
371                 --*pfreecnt;
372
373         spin_unlock(&bswspin);
374
375         initpbuf(bp);
376         KKASSERT(dsched_is_clear_buf_priv(bp));
377
378         return (bp);
379 }
380
381 struct buf *
382 getpbuf_kva(int *pfreecnt)
383 {
384         struct buf *bp;
385
386         spin_lock(&bswspin);
387
388         for (;;) {
389                 if (pfreecnt) {
390                         while (*pfreecnt == 0)
391                                 ssleep(pfreecnt, &bswspin, 0, "wswbuf0", 0);
392                 }
393
394                 /* get a bp from the swap buffer header pool */
395                 if ((bp = TAILQ_FIRST(&bswlist_kva)) != NULL)
396                         break;
397                 bswneeded_kva = 1;
398                 ssleep(&bswneeded_kva, &bswspin, 0, "wswbuf1", 0);
399                 /* loop in case someone else grabbed one */
400         }
401         TAILQ_REMOVE(&bswlist_kva, bp, b_freelist);
402         --pbuf_kva_count;
403         if (pfreecnt)
404                 --*pfreecnt;
405
406         spin_unlock(&bswspin);
407
408         initpbuf(bp);
409         KKASSERT(dsched_is_clear_buf_priv(bp));
410
411         return (bp);
412 }
413
414 /*
415  * Allocate a physical buffer, if one is available.
416  *
417  *      Note that there is no NULL hack here - all subsystems using this
418  *      call understand how to use pfreecnt.
419  *
420  * No requirements.
421  */
422 struct buf *
423 trypbuf(int *pfreecnt)
424 {
425         struct buf *bp;
426
427         spin_lock(&bswspin);
428
429         if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist_raw)) == NULL) {
430                 spin_unlock(&bswspin);
431                 return NULL;
432         }
433         TAILQ_REMOVE(&bswlist_raw, bp, b_freelist);
434         --pbuf_raw_count;
435         --*pfreecnt;
436
437         spin_unlock(&bswspin);
438
439         initpbuf(bp);
440
441         return bp;
442 }
443
444 struct buf *
445 trypbuf_kva(int *pfreecnt)
446 {
447         struct buf *bp;
448
449         spin_lock(&bswspin);
450
451         if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist_kva)) == NULL) {
452                 spin_unlock(&bswspin);
453                 return NULL;
454         }
455         TAILQ_REMOVE(&bswlist_kva, bp, b_freelist);
456         --pbuf_kva_count;
457         --*pfreecnt;
458
459         spin_unlock(&bswspin);
460
461         initpbuf(bp);
462
463         return bp;
464 }
465
466 /*
467  * Release a physical buffer
468  *
469  *      NOTE: pfreecnt can be NULL, but this 'feature' will be removed
470  *      relatively soon when the rest of the subsystems get smart about it. XXX
471  *
472  * No requirements.
473  */
474 void
475 relpbuf(struct buf *bp, int *pfreecnt)
476 {
477         int wake_bsw_kva = 0;
478         int wake_bsw_raw = 0;
479         int wake_freecnt = 0;
480
481         KKASSERT(bp->b_flags & B_PAGING);
482         dsched_exit_buf(bp);
483
484         BUF_UNLOCK(bp);
485
486         spin_lock(&bswspin);
487         if (bp->b_kvabase) {
488                 TAILQ_INSERT_HEAD(&bswlist_kva, bp, b_freelist);
489                 ++pbuf_kva_count;
490         } else {
491                 TAILQ_INSERT_HEAD(&bswlist_raw, bp, b_freelist);
492                 ++pbuf_raw_count;
493         }
494         if (bswneeded_kva) {
495                 bswneeded_kva = 0;
496                 wake_bsw_kva = 1;
497         }
498         if (bswneeded_raw) {
499                 bswneeded_raw = 0;
500                 wake_bsw_raw = 1;
501         }
502         if (pfreecnt) {
503                 if (++*pfreecnt == 1)
504                         wake_freecnt = 1;
505         }
506         spin_unlock(&bswspin);
507
508         if (wake_bsw_kva)
509                 wakeup(&bswneeded_kva);
510         if (wake_bsw_raw)
511                 wakeup(&bswneeded_raw);
512         if (wake_freecnt)
513                 wakeup(pfreecnt);
514 }
515
516 void
517 pbuf_adjcount(int *pfreecnt, int n)
518 {
519         if (n) {
520                 spin_lock(&bswspin);
521                 *pfreecnt += n;
522                 spin_unlock(&bswspin);
523                 wakeup(pfreecnt);
524         }
525 }