vm_pager.c revision 42977
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 * $Id: vm_pager.c,v 1.41 1999/01/21 08:29:12 dillon Exp $ 65 */ 66 67/* 68 * Paging space routine stubs. Emulates a matchmaker-like interface 69 * for builtin pagers. 70 */ 71 72#include <sys/param.h> 73#include <sys/systm.h> 74#include <sys/kernel.h> 75#include <sys/buf.h> 76#include <sys/ucred.h> 77#include <sys/malloc.h> 78 79#include <vm/vm.h> 80#include <vm/vm_param.h> 81#include <vm/vm_prot.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 87MALLOC_DEFINE(M_VMPGDATA, "VM pgdata", "XXX: VM pager private data"); 88 89extern struct pagerops defaultpagerops; 90extern struct pagerops swappagerops; 91extern struct pagerops vnodepagerops; 92extern struct pagerops devicepagerops; 93 94int cluster_pbuf_freecnt = -1; /* unlimited to begin with */ 95 96static int dead_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 97static vm_object_t dead_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t, 98 vm_ooffset_t)); 99static int dead_pager_putpages __P((vm_object_t, vm_page_t *, int, int, int *)); 100static boolean_t dead_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *)); 101static void dead_pager_dealloc __P((vm_object_t)); 102 103int 104dead_pager_getpages(obj, ma, count, req) 105 vm_object_t obj; 106 vm_page_t *ma; 107 int count; 108 int req; 109{ 110 return VM_PAGER_FAIL; 111} 112 113vm_object_t 114dead_pager_alloc(handle, size, prot, off) 115 void *handle; 116 vm_ooffset_t size; 117 vm_prot_t prot; 118 vm_ooffset_t off; 119{ 120 return NULL; 121} 122 123int 124dead_pager_putpages(object, m, count, flags, rtvals) 125 vm_object_t object; 126 vm_page_t *m; 127 int count; 128 int flags; 129 int *rtvals; 130{ 131 int i; 132 for (i = 0; i < count; i++) { 133 rtvals[i] = VM_PAGER_AGAIN; 134 } 135 return VM_PAGER_AGAIN; 136} 137 138int 139dead_pager_haspage(object, pindex, prev, next) 140 vm_object_t object; 141 vm_pindex_t pindex; 142 int *prev; 143 int *next; 144{ 145 if (prev) 146 *prev = 0; 147 if (next) 148 *next = 0; 149 return FALSE; 150} 151 152void 153dead_pager_dealloc(object) 154 vm_object_t object; 155{ 156 return; 157} 158 159struct pagerops deadpagerops = { 160 NULL, 161 dead_pager_alloc, 162 dead_pager_dealloc, 163 dead_pager_getpages, 164 dead_pager_putpages, 165 dead_pager_haspage, 166 NULL 167}; 168 169struct pagerops *pagertab[] = { 170 &defaultpagerops, /* OBJT_DEFAULT */ 171 &swappagerops, /* OBJT_SWAP */ 172 &vnodepagerops, /* OBJT_VNODE */ 173 &devicepagerops, /* OBJT_DEVICE */ 174 &deadpagerops /* OBJT_DEAD */ 175}; 176 177int npagers = sizeof(pagertab) / sizeof(pagertab[0]); 178 179/* 180 * Kernel address space for mapping pages. 181 * Used by pagers where KVAs are needed for IO. 182 * 183 * XXX needs to be large enough to support the number of pending async 184 * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size 185 * (MAXPHYS == 64k) if you want to get the most efficiency. 186 */ 187#define PAGER_MAP_SIZE (8 * 1024 * 1024) 188 189int pager_map_size = PAGER_MAP_SIZE; 190vm_map_t pager_map; 191static int bswneeded; 192static vm_offset_t swapbkva; /* swap buffers kva */ 193 194void 195vm_pager_init() 196{ 197 struct pagerops **pgops; 198 199 /* 200 * Initialize known pagers 201 */ 202 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++) 203 if (pgops && ((*pgops)->pgo_init != NULL)) 204 (*(*pgops)->pgo_init) (); 205} 206 207void 208vm_pager_bufferinit() 209{ 210 struct buf *bp; 211 int i; 212 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 bp->b_rcred = bp->b_wcred = NOCRED; 220 bp->b_xflags = 0; 221 } 222 223 cluster_pbuf_freecnt = nswbuf / 2; 224 225 swapbkva = kmem_alloc_pageable(pager_map, nswbuf * MAXPHYS); 226 if (!swapbkva) 227 panic("Not enough pager_map VM space for physical buffers"); 228} 229 230/* 231 * Allocate an instance of a pager of the given type. 232 * Size, protection and offset parameters are passed in for pagers that 233 * need to perform page-level validation (e.g. the device pager). 234 */ 235vm_object_t 236vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size, vm_prot_t prot, 237 vm_ooffset_t off) 238{ 239 struct pagerops *ops; 240 241 ops = pagertab[type]; 242 if (ops) 243 return ((*ops->pgo_alloc) (handle, size, prot, off)); 244 return (NULL); 245} 246 247void 248vm_pager_deallocate(object) 249 vm_object_t object; 250{ 251 (*pagertab[object->type]->pgo_dealloc) (object); 252} 253 254/* 255 * vm_pager_get_pages() - inline, see vm/vm_pager.h 256 * vm_pager_put_pages() - inline, see vm/vm_pager.h 257 * vm_pager_has_page() - inline, see vm/vm_pager.h 258 * vm_pager_page_inserted() - inline, see vm/vm_pager.h 259 * vm_pager_page_removed() - inline, see vm/vm_pager.h 260 */ 261 262#if 0 263/* 264 * vm_pager_sync: 265 * 266 * Called by pageout daemon before going back to sleep. 267 * Gives pagers a chance to clean up any completed async pageing 268 * operations. 269 */ 270void 271vm_pager_sync() 272{ 273 struct pagerops **pgops; 274 275 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++) 276 if (pgops && ((*pgops)->pgo_sync != NULL)) 277 (*(*pgops)->pgo_sync) (); 278} 279 280#endif 281 282vm_offset_t 283vm_pager_map_page(m) 284 vm_page_t m; 285{ 286 vm_offset_t kva; 287 288 kva = kmem_alloc_wait(pager_map, PAGE_SIZE); 289 pmap_kenter(kva, VM_PAGE_TO_PHYS(m)); 290 return (kva); 291} 292 293void 294vm_pager_unmap_page(kva) 295 vm_offset_t kva; 296{ 297 pmap_kremove(kva); 298 kmem_free_wakeup(pager_map, kva, PAGE_SIZE); 299} 300 301vm_object_t 302vm_pager_object_lookup(pg_list, handle) 303 register struct pagerlst *pg_list; 304 void *handle; 305{ 306 register vm_object_t object; 307 308 for (object = TAILQ_FIRST(pg_list); object != NULL; object = TAILQ_NEXT(object,pager_object_list)) 309 if (object->handle == handle) 310 return (object); 311 return (NULL); 312} 313 314/* 315 * initialize a physical buffer 316 */ 317 318static void 319initpbuf(struct buf *bp) { 320 bzero(bp, sizeof *bp); 321 bp->b_rcred = NOCRED; 322 bp->b_wcred = NOCRED; 323 bp->b_qindex = QUEUE_NONE; 324 bp->b_data = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva; 325 bp->b_kvabase = bp->b_data; 326 bp->b_kvasize = MAXPHYS; 327 bp->b_xflags = 0; 328} 329 330/* 331 * allocate a physical buffer 332 * 333 * There are a limited number (nswbuf) of physical buffers. We need 334 * to make sure that no single subsystem is able to hog all of them, 335 * so each subsystem implements a counter which is typically initialized 336 * to 1/2 nswbuf. getpbuf() decrements this counter in allocation and 337 * increments it on release, and blocks if the counter hits zero. A 338 * subsystem may initialize the counter to -1 to disable the feature, 339 * but it must still be sure to match up all uses of getpbuf() with 340 * relpbuf() using the same variable. 341 * 342 * NOTE: pfreecnt can be NULL, but this 'feature' will be removed 343 * relatively soon when the rest of the subsystems get smart about it. XXX 344 */ 345struct buf * 346getpbuf(pfreecnt) 347 int *pfreecnt; 348{ 349 int s; 350 struct buf *bp; 351 352 s = splvm(); 353 354 if (pfreecnt) { 355 while (*pfreecnt == 0) { 356 tsleep(pfreecnt, PVM, "wswbuf0", 0); 357 } 358 } 359 360 /* get a bp from the swap buffer header pool */ 361 while ((bp = TAILQ_FIRST(&bswlist)) == NULL) { 362 bswneeded = 1; 363 tsleep(&bswneeded, PVM, "wswbuf1", 0); 364 } 365 TAILQ_REMOVE(&bswlist, bp, b_freelist); 366 if (pfreecnt) 367 --*pfreecnt; 368 splx(s); 369 370 initpbuf(bp); 371 return bp; 372} 373 374/* 375 * allocate a physical buffer, if one is available. 376 * 377 * Note that there is no NULL hack here - all subsystems using this 378 * call understand how to use pfreecnt. 379 */ 380struct buf * 381trypbuf(pfreecnt) 382 int *pfreecnt; 383{ 384 int s; 385 struct buf *bp; 386 387 s = splvm(); 388 if (*pfreecnt == 0 || (bp = TAILQ_FIRST(&bswlist)) == NULL) { 389 splx(s); 390 return NULL; 391 } 392 TAILQ_REMOVE(&bswlist, bp, b_freelist); 393 394 --*pfreecnt; 395 396 splx(s); 397 398 initpbuf(bp); 399 400 return bp; 401} 402 403/* 404 * release a physical buffer 405 * 406 * NOTE: pfreecnt can be NULL, but this 'feature' will be removed 407 * relatively soon when the rest of the subsystems get smart about it. XXX 408 */ 409void 410relpbuf(bp, pfreecnt) 411 struct buf *bp; 412 int *pfreecnt; 413{ 414 int s; 415 416 s = splvm(); 417 418 if (bp->b_rcred != NOCRED) { 419 crfree(bp->b_rcred); 420 bp->b_rcred = NOCRED; 421 } 422 if (bp->b_wcred != NOCRED) { 423 crfree(bp->b_wcred); 424 bp->b_wcred = NOCRED; 425 } 426 427 if (bp->b_vp) 428 pbrelvp(bp); 429 430 if (bp->b_flags & B_WANTED) 431 wakeup(bp); 432 433 TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist); 434 435 if (bswneeded) { 436 bswneeded = 0; 437 wakeup(&bswneeded); 438 } 439 if (pfreecnt) { 440 if (++*pfreecnt == 1) 441 wakeup(pfreecnt); 442 } 443 splx(s); 444} 445