uvm_bio.c revision 1.23 1 /* $NetBSD: uvm_bio.c,v 1.23 2002/01/19 16:55:20 chs Exp $ */
2
3 /*
4 * Copyright (c) 1998 Chuck Silvers.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 /*
33 * uvm_bio.c: buffered i/o vnode mapping cache
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: uvm_bio.c,v 1.23 2002/01/19 16:55:20 chs Exp $");
38
39 #include "opt_uvmhist.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/proc.h>
47
48 #include <uvm/uvm.h>
49
50 /*
51 * global data structures
52 */
53
54 /*
55 * local functions
56 */
57
58 int ubc_fault __P((struct uvm_faultinfo *, vaddr_t, struct vm_page **, int,
59 int, vm_fault_t, vm_prot_t, int));
60 struct ubc_map *ubc_find_mapping __P((struct uvm_object *, voff_t));
61
62 /*
63 * local data structues
64 */
65
66 #define UBC_HASH(uobj, offset) \
67 (((((u_long)(uobj)) >> 8) + (((u_long)(offset)) >> PAGE_SHIFT)) & \
68 ubc_object.hashmask)
69
70 #define UBC_QUEUE(offset) \
71 (&ubc_object.inactive[(((u_long)(offset)) >> ubc_winshift) & \
72 (UBC_NQUEUES - 1)])
73
74 #define UBC_UMAP_ADDR(u) \
75 (vaddr_t)(ubc_object.kva + (((u) - ubc_object.umap) << ubc_winshift))
76
77
78 #define UMAP_PAGES_LOCKED 0x0001
79 #define UMAP_MAPPING_CACHED 0x0002
80
81 struct ubc_map
82 {
83 struct uvm_object * uobj; /* mapped object */
84 voff_t offset; /* offset into uobj */
85 voff_t writeoff; /* overwrite offset */
86 vsize_t writelen; /* overwrite len */
87 int refcount; /* refcount on mapping */
88 int flags; /* extra state */
89
90 LIST_ENTRY(ubc_map) hash; /* hash table */
91 TAILQ_ENTRY(ubc_map) inactive; /* inactive queue */
92 };
93
94 static struct ubc_object
95 {
96 struct uvm_object uobj; /* glue for uvm_map() */
97 char *kva; /* where ubc_object is mapped */
98 struct ubc_map *umap; /* array of ubc_map's */
99
100 LIST_HEAD(, ubc_map) *hash; /* hashtable for cached ubc_map's */
101 u_long hashmask; /* mask for hashtable */
102
103 TAILQ_HEAD(ubc_inactive_head, ubc_map) *inactive;
104 /* inactive queues for ubc_map's */
105
106 } ubc_object;
107
108 struct uvm_pagerops ubc_pager =
109 {
110 NULL, /* init */
111 NULL, /* reference */
112 NULL, /* detach */
113 ubc_fault, /* fault */
114 /* ... rest are NULL */
115 };
116
117 int ubc_nwins = UBC_NWINS;
118 int ubc_winshift = UBC_WINSHIFT;
119 int ubc_winsize;
120 #ifdef PMAP_PREFER
121 int ubc_nqueues;
122 boolean_t ubc_release_unmap = FALSE;
123 #define UBC_NQUEUES ubc_nqueues
124 #define UBC_RELEASE_UNMAP ubc_release_unmap
125 #else
126 #define UBC_NQUEUES 1
127 #define UBC_RELEASE_UNMAP FALSE
128 #endif
129
130 /*
131 * ubc_init
132 *
133 * init pager private data structures.
134 */
135
136 void
137 ubc_init(void)
138 {
139 struct ubc_map *umap;
140 vaddr_t va;
141 int i;
142
143 /*
144 * Make sure ubc_winshift is sane.
145 */
146 if (ubc_winshift < PAGE_SHIFT)
147 ubc_winshift = PAGE_SHIFT;
148
149 /*
150 * init ubc_object.
151 * alloc and init ubc_map's.
152 * init inactive queues.
153 * alloc and init hashtable.
154 * map in ubc_object.
155 */
156
157 simple_lock_init(&ubc_object.uobj.vmobjlock);
158 ubc_object.uobj.pgops = &ubc_pager;
159 TAILQ_INIT(&ubc_object.uobj.memq);
160 ubc_object.uobj.uo_npages = 0;
161 ubc_object.uobj.uo_refs = UVM_OBJ_KERN;
162
163 ubc_object.umap = malloc(ubc_nwins * sizeof(struct ubc_map),
164 M_TEMP, M_NOWAIT);
165 if (ubc_object.umap == NULL)
166 panic("ubc_init: failed to allocate ubc_map");
167 memset(ubc_object.umap, 0, ubc_nwins * sizeof(struct ubc_map));
168
169 if (ubc_winshift < PAGE_SHIFT) {
170 ubc_winshift = PAGE_SHIFT;
171 }
172 va = (vaddr_t)1L;
173 #ifdef PMAP_PREFER
174 PMAP_PREFER(0, &va);
175 ubc_nqueues = va >> ubc_winshift;
176 if (ubc_nqueues == 0) {
177 ubc_nqueues = 1;
178 }
179 if (ubc_nqueues != 1) {
180 ubc_release_unmap = TRUE;
181 }
182 #endif
183 ubc_winsize = 1 << ubc_winshift;
184 ubc_object.inactive = malloc(UBC_NQUEUES *
185 sizeof(struct ubc_inactive_head), M_TEMP, M_NOWAIT);
186 if (ubc_object.inactive == NULL)
187 panic("ubc_init: failed to allocate inactive queue heads");
188 for (i = 0; i < UBC_NQUEUES; i++) {
189 TAILQ_INIT(&ubc_object.inactive[i]);
190 }
191 for (i = 0; i < ubc_nwins; i++) {
192 umap = &ubc_object.umap[i];
193 TAILQ_INSERT_TAIL(&ubc_object.inactive[i & (UBC_NQUEUES - 1)],
194 umap, inactive);
195 }
196
197 ubc_object.hash = hashinit(ubc_nwins, HASH_LIST, M_TEMP, M_NOWAIT,
198 &ubc_object.hashmask);
199 for (i = 0; i <= ubc_object.hashmask; i++) {
200 LIST_INIT(&ubc_object.hash[i]);
201 }
202
203 if (uvm_map(kernel_map, (vaddr_t *)&ubc_object.kva,
204 ubc_nwins << ubc_winshift, &ubc_object.uobj, 0, (vsize_t)va,
205 UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
206 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
207 panic("ubc_init: failed to map ubc_object\n");
208 }
209 UVMHIST_INIT(ubchist, 300);
210 }
211
212 /*
213 * ubc_fault: fault routine for ubc mapping
214 */
215
216 int
217 ubc_fault(ufi, ign1, ign2, ign3, ign4, fault_type, access_type, flags)
218 struct uvm_faultinfo *ufi;
219 vaddr_t ign1;
220 struct vm_page **ign2;
221 int ign3, ign4;
222 vm_fault_t fault_type;
223 vm_prot_t access_type;
224 int flags;
225 {
226 struct uvm_object *uobj;
227 struct vnode *vp;
228 struct ubc_map *umap;
229 vaddr_t va, eva, ubc_offset, slot_offset;
230 int i, error, npages;
231 struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT], *pg;
232 vm_prot_t prot;
233 UVMHIST_FUNC("ubc_fault"); UVMHIST_CALLED(ubchist);
234
235 /*
236 * no need to try with PGO_LOCKED...
237 * we don't need to have the map locked since we know that
238 * no one will mess with it until our reference is released.
239 */
240
241 if (flags & PGO_LOCKED) {
242 uvmfault_unlockall(ufi, NULL, &ubc_object.uobj, NULL);
243 flags &= ~PGO_LOCKED;
244 }
245
246 va = ufi->orig_rvaddr;
247 ubc_offset = va - (vaddr_t)ubc_object.kva;
248
249 UVMHIST_LOG(ubchist, "va 0x%lx ubc_offset 0x%lx at %d",
250 va, ubc_offset, access_type,0);
251
252 umap = &ubc_object.umap[ubc_offset >> ubc_winshift];
253 KASSERT(umap->refcount != 0);
254 slot_offset = ubc_offset & (ubc_winsize - 1);
255
256 /* no umap locking needed since we have a ref on the umap */
257 uobj = umap->uobj;
258 vp = (struct vnode *)uobj;
259 KASSERT(vp != NULL);
260
261 npages = MIN(ubc_winsize - slot_offset,
262 (round_page(MAX(vp->v_size, umap->offset +
263 umap->writeoff + umap->writelen)) -
264 umap->offset)) >> PAGE_SHIFT;
265
266 again:
267 memset(pgs, 0, sizeof (pgs));
268 simple_lock(&uobj->vmobjlock);
269
270 UVMHIST_LOG(ubchist, "slot_offset 0x%x writeoff 0x%x writelen 0x%x "
271 "v_size 0x%x", slot_offset, umap->writeoff, umap->writelen,
272 vp->v_size);
273 UVMHIST_LOG(ubchist, "getpages vp %p offset 0x%x npages %d",
274 uobj, umap->offset + slot_offset, npages, 0);
275
276 flags |= PGO_PASTEOF;
277 error = VOP_GETPAGES(vp, umap->offset + slot_offset, pgs, &npages, 0,
278 access_type, 0, flags);
279 UVMHIST_LOG(ubchist, "getpages error %d npages %d", error, npages,0,0);
280
281 if (error == EAGAIN) {
282 tsleep(&lbolt, PVM, "ubc_fault", 0);
283 goto again;
284 }
285 if (error) {
286 return error;
287 }
288
289 va = ufi->orig_rvaddr;
290 eva = ufi->orig_rvaddr + (npages << PAGE_SHIFT);
291
292 /*
293 * for virtually-indexed, virtually-tagged caches we should avoid
294 * creating writable mappings when we don't absolutely need them,
295 * since the "compatible alias" trick doesn't work on such caches.
296 * otherwise, we can always map the pages writable.
297 */
298
299 #ifdef PMAP_CACHE_VIVT
300 prot = VM_PROT_READ | access_type;
301 #else
302 prot = VM_PROT_READ | VM_PROT_WRITE;
303 #endif
304 UVMHIST_LOG(ubchist, "va 0x%lx eva 0x%lx", va, eva, 0,0);
305 simple_lock(&uobj->vmobjlock);
306 uvm_lock_pageq();
307 for (i = 0; va < eva; i++, va += PAGE_SIZE) {
308 UVMHIST_LOG(ubchist, "pgs[%d] = %p", i, pgs[i],0,0);
309 pg = pgs[i];
310
311 if (pg == NULL || pg == PGO_DONTCARE) {
312 continue;
313 }
314 if (pg->flags & PG_WANTED) {
315 wakeup(pg);
316 }
317 KASSERT((pg->flags & PG_FAKE) == 0);
318 if (pg->flags & PG_RELEASED) {
319 uvm_pagefree(pg);
320 continue;
321 }
322 KASSERT(access_type == VM_PROT_READ ||
323 (pg->flags & PG_RDONLY) == 0);
324 pmap_enter(ufi->orig_map->pmap, va, VM_PAGE_TO_PHYS(pg),
325 prot, access_type);
326 uvm_pageactivate(pg);
327 pg->flags &= ~(PG_BUSY);
328 UVM_PAGE_OWN(pg, NULL);
329 }
330 uvm_unlock_pageq();
331 simple_unlock(&uobj->vmobjlock);
332 pmap_update(ufi->orig_map->pmap);
333 return 0;
334 }
335
336 /*
337 * local functions
338 */
339
340 struct ubc_map *
341 ubc_find_mapping(uobj, offset)
342 struct uvm_object *uobj;
343 voff_t offset;
344 {
345 struct ubc_map *umap;
346
347 LIST_FOREACH(umap, &ubc_object.hash[UBC_HASH(uobj, offset)], hash) {
348 if (umap->uobj == uobj && umap->offset == offset) {
349 return umap;
350 }
351 }
352 return NULL;
353 }
354
355
356 /*
357 * ubc interface functions
358 */
359
360 /*
361 * ubc_alloc: allocate a file mapping window
362 */
363
364 void *
365 ubc_alloc(uobj, offset, lenp, flags)
366 struct uvm_object *uobj;
367 voff_t offset;
368 vsize_t *lenp;
369 int flags;
370 {
371 struct vnode *vp = (struct vnode *)uobj;
372 vaddr_t slot_offset, va;
373 struct ubc_map *umap;
374 voff_t umap_offset;
375 int error;
376 UVMHIST_FUNC("ubc_alloc"); UVMHIST_CALLED(ubchist);
377
378 UVMHIST_LOG(ubchist, "uobj %p offset 0x%lx len 0x%lx filesize 0x%x",
379 uobj, offset, *lenp, vp->v_size);
380
381 umap_offset = (offset & ~((voff_t)ubc_winsize - 1));
382 slot_offset = (vaddr_t)(offset & ((voff_t)ubc_winsize - 1));
383 *lenp = MIN(*lenp, ubc_winsize - slot_offset);
384
385 /*
386 * the vnode is always locked here, so we don't need to add a ref.
387 */
388
389 again:
390 simple_lock(&ubc_object.uobj.vmobjlock);
391 umap = ubc_find_mapping(uobj, umap_offset);
392 if (umap == NULL) {
393 umap = TAILQ_FIRST(UBC_QUEUE(offset));
394 if (umap == NULL) {
395 simple_unlock(&ubc_object.uobj.vmobjlock);
396 tsleep(&lbolt, PVM, "ubc_alloc", 0);
397 goto again;
398 }
399
400 /*
401 * remove from old hash (if any), add to new hash.
402 */
403
404 if (umap->uobj != NULL) {
405 LIST_REMOVE(umap, hash);
406 }
407 umap->uobj = uobj;
408 umap->offset = umap_offset;
409 LIST_INSERT_HEAD(&ubc_object.hash[UBC_HASH(uobj, umap_offset)],
410 umap, hash);
411 va = UBC_UMAP_ADDR(umap);
412 if (umap->flags & UMAP_MAPPING_CACHED) {
413 umap->flags &= ~UMAP_MAPPING_CACHED;
414 pmap_remove(pmap_kernel(), va, va + ubc_winsize);
415 pmap_update(pmap_kernel());
416 }
417 } else {
418 va = UBC_UMAP_ADDR(umap);
419 }
420
421 if (umap->refcount == 0) {
422 TAILQ_REMOVE(UBC_QUEUE(offset), umap, inactive);
423 }
424
425 #ifdef DIAGNOSTIC
426 if ((flags & UBC_WRITE) && (umap->writeoff || umap->writelen)) {
427 panic("ubc_fault: concurrent writes vp %p", uobj);
428 }
429 #endif
430 if (flags & UBC_WRITE) {
431 umap->writeoff = slot_offset;
432 umap->writelen = *lenp;
433 }
434
435 umap->refcount++;
436 simple_unlock(&ubc_object.uobj.vmobjlock);
437 UVMHIST_LOG(ubchist, "umap %p refs %d va %p flags 0x%x",
438 umap, umap->refcount, va, flags);
439
440 if (flags & UBC_FAULTBUSY) {
441 int npages = (*lenp + PAGE_SIZE - 1) >> PAGE_SHIFT;
442 struct vm_page *pgs[npages];
443 int gpflags = PGO_SYNCIO|PGO_OVERWRITE|PGO_PASTEOF;
444 int i;
445
446 if (umap->flags & UMAP_MAPPING_CACHED) {
447 umap->flags &= ~UMAP_MAPPING_CACHED;
448 pmap_remove(pmap_kernel(), va, va + ubc_winsize);
449 }
450 memset(pgs, 0, sizeof(pgs));
451 simple_lock(&uobj->vmobjlock);
452 error = VOP_GETPAGES(vp, trunc_page(offset), pgs, &npages, 0,
453 VM_PROT_READ|VM_PROT_WRITE, 0, gpflags);
454 UVMHIST_LOG(ubchist, "faultbusy getpages %d", error,0,0,0);
455 if (error) {
456 goto out;
457 }
458 for (i = 0; i < npages; i++) {
459 pmap_kenter_pa(va + slot_offset + (i << PAGE_SHIFT),
460 VM_PAGE_TO_PHYS(pgs[i]),
461 VM_PROT_READ | VM_PROT_WRITE);
462 }
463 pmap_update(pmap_kernel());
464 umap->flags |= UMAP_PAGES_LOCKED;
465 }
466
467 out:
468 return (void *)(va + slot_offset);
469 }
470
471 /*
472 * ubc_release: free a file mapping window.
473 */
474
475 void
476 ubc_release(va, flags)
477 void *va;
478 int flags;
479 {
480 struct ubc_map *umap;
481 struct uvm_object *uobj;
482 vaddr_t umapva;
483 boolean_t unmapped;
484 UVMHIST_FUNC("ubc_release"); UVMHIST_CALLED(ubchist);
485
486 UVMHIST_LOG(ubchist, "va %p", va,0,0,0);
487 umap = &ubc_object.umap[((char *)va - ubc_object.kva) >> ubc_winshift];
488 umapva = UBC_UMAP_ADDR(umap);
489 uobj = umap->uobj;
490 KASSERT(uobj != NULL);
491
492 if (umap->flags & UMAP_PAGES_LOCKED) {
493 int slot_offset = umap->writeoff;
494 int endoff = umap->writeoff + umap->writelen;
495 int zerolen = round_page(endoff) - endoff;
496 int npages = (int)(round_page(umap->writeoff + umap->writelen)
497 - trunc_page(umap->writeoff)) >> PAGE_SHIFT;
498 struct vm_page *pgs[npages];
499 paddr_t pa;
500 int i;
501 boolean_t rv;
502
503 if (zerolen) {
504 memset((char *)umapva + endoff, 0, zerolen);
505 }
506 umap->flags &= ~UMAP_PAGES_LOCKED;
507 uvm_lock_pageq();
508 for (i = 0; i < npages; i++) {
509 rv = pmap_extract(pmap_kernel(),
510 umapva + slot_offset + (i << PAGE_SHIFT), &pa);
511 KASSERT(rv);
512 pgs[i] = PHYS_TO_VM_PAGE(pa);
513 pgs[i]->flags &= ~(PG_FAKE|PG_CLEAN);
514 uvm_pageactivate(pgs[i]);
515 }
516 uvm_unlock_pageq();
517 pmap_kremove(umapva, ubc_winsize);
518 pmap_update(pmap_kernel());
519 uvm_page_unbusy(pgs, npages);
520 unmapped = TRUE;
521 } else {
522 unmapped = FALSE;
523 }
524
525 simple_lock(&ubc_object.uobj.vmobjlock);
526 umap->writeoff = 0;
527 umap->writelen = 0;
528 umap->refcount--;
529 if (umap->refcount == 0) {
530 if (UBC_RELEASE_UNMAP &&
531 (((struct vnode *)uobj)->v_flag & VTEXT)) {
532
533 /*
534 * if this file is the executable image of
535 * some process, that process will likely have
536 * the file mapped at an alignment other than
537 * what PMAP_PREFER() would like. we'd like
538 * to have process text be able to use the
539 * cache even if someone is also reading the
540 * file, so invalidate mappings of such files
541 * as soon as possible.
542 */
543
544 pmap_remove(pmap_kernel(), umapva,
545 umapva + ubc_winsize);
546 umap->flags &= ~UMAP_MAPPING_CACHED;
547 pmap_update(pmap_kernel());
548 LIST_REMOVE(umap, hash);
549 umap->uobj = NULL;
550 TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap,
551 inactive);
552 } else {
553 if (!unmapped) {
554 umap->flags |= UMAP_MAPPING_CACHED;
555 }
556 TAILQ_INSERT_TAIL(UBC_QUEUE(umap->offset), umap,
557 inactive);
558 }
559 }
560 UVMHIST_LOG(ubchist, "umap %p refs %d", umap, umap->refcount,0,0);
561 simple_unlock(&ubc_object.uobj.vmobjlock);
562 }
563
564
565 /*
566 * removing a range of mappings from the ubc mapping cache.
567 */
568
569 void
570 ubc_flush(uobj, start, end)
571 struct uvm_object *uobj;
572 voff_t start, end;
573 {
574 struct ubc_map *umap;
575 vaddr_t va;
576 UVMHIST_FUNC("ubc_flush"); UVMHIST_CALLED(ubchist);
577
578 UVMHIST_LOG(ubchist, "uobj %p start 0x%lx end 0x%lx",
579 uobj, start, end,0);
580
581 simple_lock(&ubc_object.uobj.vmobjlock);
582 for (umap = ubc_object.umap;
583 umap < &ubc_object.umap[ubc_nwins];
584 umap++) {
585
586 if (umap->uobj != uobj || umap->offset < start ||
587 (umap->offset >= end && end != 0) ||
588 umap->refcount > 0) {
589 continue;
590 }
591
592 /*
593 * remove from hash,
594 * move to head of inactive queue.
595 */
596
597 va = (vaddr_t)(ubc_object.kva +
598 ((umap - ubc_object.umap) << ubc_winshift));
599 pmap_remove(pmap_kernel(), va, va + ubc_winsize);
600
601 LIST_REMOVE(umap, hash);
602 umap->uobj = NULL;
603 TAILQ_REMOVE(UBC_QUEUE(umap->offset), umap, inactive);
604 TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap, inactive);
605 }
606 pmap_update(pmap_kernel());
607 simple_unlock(&ubc_object.uobj.vmobjlock);
608 }
609