uvm_bio.c revision 1.104 1 /* $NetBSD: uvm_bio.c,v 1.104 2020/02/23 15:46:43 ad 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 object mapping cache
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: uvm_bio.c,v 1.104 2020/02/23 15:46:43 ad Exp $");
38
39 #include "opt_uvmhist.h"
40 #include "opt_ubc.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kmem.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48
49 #include <uvm/uvm.h>
50
51 #ifdef PMAP_DIRECT
52 # define UBC_USE_PMAP_DIRECT
53 #endif
54
55 /*
56 * local functions
57 */
58
59 static int ubc_fault(struct uvm_faultinfo *, vaddr_t, struct vm_page **,
60 int, int, vm_prot_t, int);
61 static struct ubc_map *ubc_find_mapping(struct uvm_object *, voff_t);
62 #ifdef UBC_USE_PMAP_DIRECT
63 static int __noinline ubc_uiomove_direct(struct uvm_object *, struct uio *, vsize_t,
64 int, int);
65 static void __noinline ubc_zerorange_direct(struct uvm_object *, off_t, size_t, int);
66
67 bool ubc_direct = false; /* XXX */
68 #endif
69
70 /*
71 * local data structues
72 */
73
74 #define UBC_HASH(uobj, offset) \
75 (((((u_long)(uobj)) >> 8) + (((u_long)(offset)) >> PAGE_SHIFT)) & \
76 ubc_object.hashmask)
77
78 #define UBC_QUEUE(offset) \
79 (&ubc_object.inactive[(((u_long)(offset)) >> ubc_winshift) & \
80 (UBC_NQUEUES - 1)])
81
82 #define UBC_UMAP_ADDR(u) \
83 (vaddr_t)(ubc_object.kva + (((u) - ubc_object.umap) << ubc_winshift))
84
85
86 #define UMAP_PAGES_LOCKED 0x0001
87 #define UMAP_MAPPING_CACHED 0x0002
88
89 struct ubc_map {
90 struct uvm_object * uobj; /* mapped object */
91 voff_t offset; /* offset into uobj */
92 voff_t writeoff; /* write offset */
93 vsize_t writelen; /* write len */
94 int refcount; /* refcount on mapping */
95 int flags; /* extra state */
96 int advice;
97
98 LIST_ENTRY(ubc_map) hash; /* hash table */
99 TAILQ_ENTRY(ubc_map) inactive; /* inactive queue */
100 LIST_ENTRY(ubc_map) list; /* per-object list */
101 };
102
103 TAILQ_HEAD(ubc_inactive_head, ubc_map);
104 static struct ubc_object {
105 struct uvm_object uobj; /* glue for uvm_map() */
106 char *kva; /* where ubc_object is mapped */
107 struct ubc_map *umap; /* array of ubc_map's */
108
109 LIST_HEAD(, ubc_map) *hash; /* hashtable for cached ubc_map's */
110 u_long hashmask; /* mask for hashtable */
111
112 struct ubc_inactive_head *inactive;
113 /* inactive queues for ubc_map's */
114 } ubc_object;
115
116 const struct uvm_pagerops ubc_pager = {
117 .pgo_fault = ubc_fault,
118 /* ... rest are NULL */
119 };
120
121 int ubc_nwins = UBC_NWINS;
122 int ubc_winshift __read_mostly = UBC_WINSHIFT;
123 int ubc_winsize __read_mostly;
124 #if defined(PMAP_PREFER)
125 int ubc_nqueues;
126 #define UBC_NQUEUES ubc_nqueues
127 #else
128 #define UBC_NQUEUES 1
129 #endif
130
131 #if defined(UBC_STATS)
132
133 #define UBC_EVCNT_DEFINE(name) \
134 struct evcnt ubc_evcnt_##name = \
135 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "ubc", #name); \
136 EVCNT_ATTACH_STATIC(ubc_evcnt_##name);
137 #define UBC_EVCNT_INCR(name) ubc_evcnt_##name.ev_count++
138
139 #else /* defined(UBC_STATS) */
140
141 #define UBC_EVCNT_DEFINE(name) /* nothing */
142 #define UBC_EVCNT_INCR(name) /* nothing */
143
144 #endif /* defined(UBC_STATS) */
145
146 UBC_EVCNT_DEFINE(wincachehit)
147 UBC_EVCNT_DEFINE(wincachemiss)
148 UBC_EVCNT_DEFINE(faultbusy)
149
150 /*
151 * ubc_init
152 *
153 * init pager private data structures.
154 */
155
156 void
157 ubc_init(void)
158 {
159 /*
160 * Make sure ubc_winshift is sane.
161 */
162 if (ubc_winshift < PAGE_SHIFT)
163 ubc_winshift = PAGE_SHIFT;
164 ubc_winsize = 1 << ubc_winshift;
165
166 /*
167 * init ubc_object.
168 * alloc and init ubc_map's.
169 * init inactive queues.
170 * alloc and init hashtable.
171 * map in ubc_object.
172 */
173
174 uvm_obj_init(&ubc_object.uobj, &ubc_pager, true, UVM_OBJ_KERN);
175
176 ubc_object.umap = kmem_zalloc(ubc_nwins * sizeof(struct ubc_map),
177 KM_SLEEP);
178 if (ubc_object.umap == NULL)
179 panic("ubc_init: failed to allocate ubc_map");
180
181 vaddr_t va = (vaddr_t)1L;
182 #ifdef PMAP_PREFER
183 PMAP_PREFER(0, &va, 0, 0); /* kernel is never topdown */
184 ubc_nqueues = va >> ubc_winshift;
185 if (ubc_nqueues == 0) {
186 ubc_nqueues = 1;
187 }
188 #endif
189 ubc_object.inactive = kmem_alloc(UBC_NQUEUES *
190 sizeof(struct ubc_inactive_head), KM_SLEEP);
191 for (int i = 0; i < UBC_NQUEUES; i++) {
192 TAILQ_INIT(&ubc_object.inactive[i]);
193 }
194 for (int i = 0; i < ubc_nwins; i++) {
195 struct ubc_map *umap;
196 umap = &ubc_object.umap[i];
197 TAILQ_INSERT_TAIL(&ubc_object.inactive[i & (UBC_NQUEUES - 1)],
198 umap, inactive);
199 }
200
201 ubc_object.hash = hashinit(ubc_nwins, HASH_LIST, true,
202 &ubc_object.hashmask);
203 for (int i = 0; i <= ubc_object.hashmask; i++) {
204 LIST_INIT(&ubc_object.hash[i]);
205 }
206
207 if (uvm_map(kernel_map, (vaddr_t *)&ubc_object.kva,
208 ubc_nwins << ubc_winshift, &ubc_object.uobj, 0, (vsize_t)va,
209 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_NONE,
210 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
211 panic("ubc_init: failed to map ubc_object");
212 }
213 }
214
215 void
216 ubchist_init(void)
217 {
218
219 UVMHIST_INIT(ubchist, 300);
220 }
221
222 /*
223 * ubc_fault_page: helper of ubc_fault to handle a single page.
224 *
225 * => Caller has UVM object locked.
226 * => Caller will perform pmap_update().
227 */
228
229 static inline int
230 ubc_fault_page(const struct uvm_faultinfo *ufi, const struct ubc_map *umap,
231 struct vm_page *pg, vm_prot_t prot, vm_prot_t access_type, vaddr_t va)
232 {
233 vm_prot_t mask;
234 int error;
235 bool rdonly;
236
237 KASSERT(rw_write_held(pg->uobject->vmobjlock));
238
239 if (pg->flags & PG_WANTED) {
240 wakeup(pg);
241 }
242 KASSERT((pg->flags & PG_FAKE) == 0);
243 if (pg->flags & PG_RELEASED) {
244 uvm_pagefree(pg);
245 return 0;
246 }
247 if (pg->loan_count != 0) {
248
249 /*
250 * Avoid unneeded loan break, if possible.
251 */
252
253 if ((access_type & VM_PROT_WRITE) == 0) {
254 prot &= ~VM_PROT_WRITE;
255 }
256 if (prot & VM_PROT_WRITE) {
257 struct vm_page *newpg;
258
259 newpg = uvm_loanbreak(pg);
260 if (newpg == NULL) {
261 uvm_page_unbusy(&pg, 1);
262 return ENOMEM;
263 }
264 pg = newpg;
265 }
266 }
267
268 /*
269 * Note that a page whose backing store is partially allocated
270 * is marked as PG_RDONLY.
271 *
272 * it's a responsibility of ubc_alloc's caller to allocate backing
273 * blocks before writing to the window.
274 */
275
276 KASSERT((pg->flags & PG_RDONLY) == 0 ||
277 (access_type & VM_PROT_WRITE) == 0 ||
278 pg->offset < umap->writeoff ||
279 pg->offset + PAGE_SIZE > umap->writeoff + umap->writelen);
280
281 rdonly = uvm_pagereadonly_p(pg);
282 mask = rdonly ? ~VM_PROT_WRITE : VM_PROT_ALL;
283
284 error = pmap_enter(ufi->orig_map->pmap, va, VM_PAGE_TO_PHYS(pg),
285 prot & mask, PMAP_CANFAIL | (access_type & mask));
286
287 uvm_pagelock(pg);
288 uvm_pageactivate(pg);
289 uvm_pageunlock(pg);
290 pg->flags &= ~(PG_BUSY|PG_WANTED);
291 UVM_PAGE_OWN(pg, NULL);
292
293 return error;
294 }
295
296 /*
297 * ubc_fault: fault routine for ubc mapping
298 */
299
300 static int
301 ubc_fault(struct uvm_faultinfo *ufi, vaddr_t ign1, struct vm_page **ign2,
302 int ign3, int ign4, vm_prot_t access_type, int flags)
303 {
304 struct uvm_object *uobj;
305 struct ubc_map *umap;
306 vaddr_t va, eva, ubc_offset, slot_offset;
307 struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT];
308 int i, error, npages;
309 vm_prot_t prot;
310
311 UVMHIST_FUNC("ubc_fault"); UVMHIST_CALLED(ubchist);
312
313 /*
314 * no need to try with PGO_LOCKED...
315 * we don't need to have the map locked since we know that
316 * no one will mess with it until our reference is released.
317 */
318
319 if (flags & PGO_LOCKED) {
320 uvmfault_unlockall(ufi, NULL, &ubc_object.uobj);
321 flags &= ~PGO_LOCKED;
322 }
323
324 va = ufi->orig_rvaddr;
325 ubc_offset = va - (vaddr_t)ubc_object.kva;
326 umap = &ubc_object.umap[ubc_offset >> ubc_winshift];
327 KASSERT(umap->refcount != 0);
328 KASSERT((umap->flags & UMAP_PAGES_LOCKED) == 0);
329 slot_offset = ubc_offset & (ubc_winsize - 1);
330
331 /*
332 * some platforms cannot write to individual bytes atomically, so
333 * software has to do read/modify/write of larger quantities instead.
334 * this means that the access_type for "write" operations
335 * can be VM_PROT_READ, which confuses us mightily.
336 *
337 * deal with this by resetting access_type based on the info
338 * that ubc_alloc() stores for us.
339 */
340
341 access_type = umap->writelen ? VM_PROT_WRITE : VM_PROT_READ;
342 UVMHIST_LOG(ubchist, "va 0x%jx ubc_offset 0x%jx access_type %jd",
343 va, ubc_offset, access_type, 0);
344
345 if ((access_type & VM_PROT_WRITE) != 0) {
346 #ifndef PRIxOFF /* XXX */
347 #define PRIxOFF "jx" /* XXX */
348 #endif /* XXX */
349 KASSERTMSG((trunc_page(umap->writeoff) <= slot_offset),
350 "out of range write: slot=%#"PRIxVSIZE" off=%#"PRIxOFF,
351 slot_offset, (intmax_t)umap->writeoff);
352 KASSERTMSG((slot_offset < umap->writeoff + umap->writelen),
353 "out of range write: slot=%#"PRIxVADDR
354 " off=%#"PRIxOFF" len=%#"PRIxVSIZE,
355 slot_offset, (intmax_t)umap->writeoff, umap->writelen);
356 }
357
358 /* no umap locking needed since we have a ref on the umap */
359 uobj = umap->uobj;
360
361 if ((access_type & VM_PROT_WRITE) == 0) {
362 npages = (ubc_winsize - slot_offset) >> PAGE_SHIFT;
363 } else {
364 npages = (round_page(umap->offset + umap->writeoff +
365 umap->writelen) - (umap->offset + slot_offset))
366 >> PAGE_SHIFT;
367 flags |= PGO_PASTEOF;
368 }
369
370 again:
371 memset(pgs, 0, sizeof (pgs));
372 rw_enter(uobj->vmobjlock, RW_WRITER);
373
374 UVMHIST_LOG(ubchist, "slot_offset 0x%jx writeoff 0x%jx writelen 0x%jx ",
375 slot_offset, umap->writeoff, umap->writelen, 0);
376 UVMHIST_LOG(ubchist, "getpages uobj %#jx offset 0x%jx npages %jd",
377 (uintptr_t)uobj, umap->offset + slot_offset, npages, 0);
378
379 error = (*uobj->pgops->pgo_get)(uobj, umap->offset + slot_offset, pgs,
380 &npages, 0, access_type, umap->advice, flags | PGO_NOBLOCKALLOC |
381 PGO_NOTIMESTAMP);
382 UVMHIST_LOG(ubchist, "getpages error %jd npages %jd", error, npages, 0,
383 0);
384
385 if (error == EAGAIN) {
386 kpause("ubc_fault", false, hz >> 2, NULL);
387 goto again;
388 }
389 if (error) {
390 return error;
391 }
392
393 /*
394 * For virtually-indexed, virtually-tagged caches we should avoid
395 * creating writable mappings when we do not absolutely need them,
396 * since the "compatible alias" trick does not work on such caches.
397 * Otherwise, we can always map the pages writable.
398 */
399
400 #ifdef PMAP_CACHE_VIVT
401 prot = VM_PROT_READ | access_type;
402 #else
403 prot = VM_PROT_READ | VM_PROT_WRITE;
404 #endif
405
406 va = ufi->orig_rvaddr;
407 eva = ufi->orig_rvaddr + (npages << PAGE_SHIFT);
408
409 UVMHIST_LOG(ubchist, "va 0x%jx eva 0x%jx", va, eva, 0, 0);
410
411 /*
412 * Note: normally all returned pages would have the same UVM object.
413 * However, layered file-systems and e.g. tmpfs, may return pages
414 * which belong to underlying UVM object. In such case, lock is
415 * shared amongst the objects.
416 */
417 rw_enter(uobj->vmobjlock, RW_WRITER);
418 for (i = 0; va < eva; i++, va += PAGE_SIZE) {
419 struct vm_page *pg;
420
421 UVMHIST_LOG(ubchist, "pgs[%jd] = %#jx", i, (uintptr_t)pgs[i],
422 0, 0);
423 pg = pgs[i];
424
425 if (pg == NULL || pg == PGO_DONTCARE) {
426 continue;
427 }
428 KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
429 error = ubc_fault_page(ufi, umap, pg, prot, access_type, va);
430 if (error) {
431 /*
432 * Flush (there might be pages entered), drop the lock,
433 * and perform uvm_wait(). Note: page will re-fault.
434 */
435 pmap_update(ufi->orig_map->pmap);
436 rw_exit(uobj->vmobjlock);
437 uvm_wait("ubc_fault");
438 rw_enter(uobj->vmobjlock, RW_WRITER);
439 }
440 }
441 /* Must make VA visible before the unlock. */
442 pmap_update(ufi->orig_map->pmap);
443 rw_exit(uobj->vmobjlock);
444
445 return 0;
446 }
447
448 /*
449 * local functions
450 */
451
452 static struct ubc_map *
453 ubc_find_mapping(struct uvm_object *uobj, voff_t offset)
454 {
455 struct ubc_map *umap;
456
457 LIST_FOREACH(umap, &ubc_object.hash[UBC_HASH(uobj, offset)], hash) {
458 if (umap->uobj == uobj && umap->offset == offset) {
459 return umap;
460 }
461 }
462 return NULL;
463 }
464
465
466 /*
467 * ubc interface functions
468 */
469
470 /*
471 * ubc_alloc: allocate a file mapping window
472 */
473
474 static void * __noinline
475 ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
476 int flags)
477 {
478 vaddr_t slot_offset, va;
479 struct ubc_map *umap;
480 voff_t umap_offset;
481 int error;
482 UVMHIST_FUNC("ubc_alloc"); UVMHIST_CALLED(ubchist);
483
484 UVMHIST_LOG(ubchist, "uobj %#jx offset 0x%jx len 0x%jx",
485 (uintptr_t)uobj, offset, *lenp, 0);
486
487 KASSERT(*lenp > 0);
488 umap_offset = (offset & ~((voff_t)ubc_winsize - 1));
489 slot_offset = (vaddr_t)(offset & ((voff_t)ubc_winsize - 1));
490 *lenp = MIN(*lenp, ubc_winsize - slot_offset);
491
492 rw_enter(ubc_object.uobj.vmobjlock, RW_WRITER);
493 again:
494 /*
495 * The UVM object is already referenced.
496 * Lock order: UBC object -> ubc_map::uobj.
497 */
498 umap = ubc_find_mapping(uobj, umap_offset);
499 if (umap == NULL) {
500 struct uvm_object *oobj;
501
502 UBC_EVCNT_INCR(wincachemiss);
503 umap = TAILQ_FIRST(UBC_QUEUE(offset));
504 if (umap == NULL) {
505 rw_exit(ubc_object.uobj.vmobjlock);
506 kpause("ubc_alloc", false, hz >> 2, NULL);
507 rw_enter(ubc_object.uobj.vmobjlock, RW_WRITER);
508 goto again;
509 }
510
511 va = UBC_UMAP_ADDR(umap);
512 oobj = umap->uobj;
513
514 /*
515 * Remove from old hash (if any), add to new hash.
516 */
517
518 if (oobj != NULL) {
519 /*
520 * Mapping must be removed before the list entry,
521 * since there is a race with ubc_purge().
522 */
523 if (umap->flags & UMAP_MAPPING_CACHED) {
524 umap->flags &= ~UMAP_MAPPING_CACHED;
525 rw_enter(oobj->vmobjlock, RW_WRITER);
526 pmap_remove(pmap_kernel(), va,
527 va + ubc_winsize);
528 pmap_update(pmap_kernel());
529 rw_exit(oobj->vmobjlock);
530 }
531 LIST_REMOVE(umap, hash);
532 LIST_REMOVE(umap, list);
533 } else {
534 KASSERT((umap->flags & UMAP_MAPPING_CACHED) == 0);
535 }
536 umap->uobj = uobj;
537 umap->offset = umap_offset;
538 LIST_INSERT_HEAD(&ubc_object.hash[UBC_HASH(uobj, umap_offset)],
539 umap, hash);
540 LIST_INSERT_HEAD(&uobj->uo_ubc, umap, list);
541 } else {
542 UBC_EVCNT_INCR(wincachehit);
543 va = UBC_UMAP_ADDR(umap);
544 }
545
546 if (umap->refcount == 0) {
547 TAILQ_REMOVE(UBC_QUEUE(offset), umap, inactive);
548 }
549
550 if (flags & UBC_WRITE) {
551 KASSERTMSG(umap->writeoff == 0 && umap->writelen == 0,
552 "ubc_alloc: concurrent writes to uobj %p", uobj);
553 umap->writeoff = slot_offset;
554 umap->writelen = *lenp;
555 }
556
557 umap->refcount++;
558 umap->advice = advice;
559 rw_exit(ubc_object.uobj.vmobjlock);
560 UVMHIST_LOG(ubchist, "umap %#jx refs %jd va %#jx flags 0x%jx",
561 (uintptr_t)umap, umap->refcount, (uintptr_t)va, flags);
562
563 if (flags & UBC_FAULTBUSY) {
564 // XXX add offset from slot_offset?
565 int npages = (*lenp + PAGE_SIZE - 1) >> PAGE_SHIFT;
566 struct vm_page *pgs[npages];
567 int gpflags =
568 PGO_SYNCIO|PGO_OVERWRITE|PGO_PASTEOF|PGO_NOBLOCKALLOC|
569 PGO_NOTIMESTAMP;
570 int i;
571 KDASSERT(flags & UBC_WRITE);
572 KASSERT(umap->refcount == 1);
573
574 UBC_EVCNT_INCR(faultbusy);
575 again_faultbusy:
576 rw_enter(uobj->vmobjlock, RW_WRITER);
577 if (umap->flags & UMAP_MAPPING_CACHED) {
578 umap->flags &= ~UMAP_MAPPING_CACHED;
579 pmap_remove(pmap_kernel(), va, va + ubc_winsize);
580 }
581 memset(pgs, 0, sizeof(pgs));
582
583 error = (*uobj->pgops->pgo_get)(uobj, trunc_page(offset), pgs,
584 &npages, 0, VM_PROT_READ | VM_PROT_WRITE, advice, gpflags);
585 UVMHIST_LOG(ubchist, "faultbusy getpages %jd", error, 0, 0, 0);
586 if (error) {
587 /*
588 * Flush: the mapping above might have been removed.
589 */
590 pmap_update(pmap_kernel());
591 goto out;
592 }
593 for (i = 0; i < npages; i++) {
594 struct vm_page *pg = pgs[i];
595
596 KASSERT(pg->uobject == uobj);
597 if (pg->loan_count != 0) {
598 rw_enter(uobj->vmobjlock, RW_WRITER);
599 if (pg->loan_count != 0) {
600 pg = uvm_loanbreak(pg);
601 }
602 if (pg == NULL) {
603 pmap_kremove(va, ubc_winsize);
604 pmap_update(pmap_kernel());
605 uvm_page_unbusy(pgs, npages);
606 rw_exit(uobj->vmobjlock);
607 uvm_wait("ubc_alloc");
608 goto again_faultbusy;
609 }
610 rw_exit(uobj->vmobjlock);
611 pgs[i] = pg;
612 }
613 pmap_kenter_pa(va + slot_offset + (i << PAGE_SHIFT),
614 VM_PAGE_TO_PHYS(pg),
615 VM_PROT_READ | VM_PROT_WRITE, 0);
616 }
617 pmap_update(pmap_kernel());
618 umap->flags |= UMAP_PAGES_LOCKED;
619 } else {
620 KASSERT((umap->flags & UMAP_PAGES_LOCKED) == 0);
621 }
622
623 out:
624 return (void *)(va + slot_offset);
625 }
626
627 /*
628 * ubc_release: free a file mapping window.
629 */
630
631 static void __noinline
632 ubc_release(void *va, int flags)
633 {
634 struct ubc_map *umap;
635 struct uvm_object *uobj;
636 vaddr_t umapva;
637 bool unmapped;
638 UVMHIST_FUNC("ubc_release"); UVMHIST_CALLED(ubchist);
639
640 UVMHIST_LOG(ubchist, "va %#jx", (uintptr_t)va, 0, 0, 0);
641 umap = &ubc_object.umap[((char *)va - ubc_object.kva) >> ubc_winshift];
642 umapva = UBC_UMAP_ADDR(umap);
643 uobj = umap->uobj;
644 KASSERT(uobj != NULL);
645
646 if (umap->flags & UMAP_PAGES_LOCKED) {
647 const voff_t slot_offset = umap->writeoff;
648 const voff_t endoff = umap->writeoff + umap->writelen;
649 const voff_t zerolen = round_page(endoff) - endoff;
650 const u_int npages = (round_page(endoff) -
651 trunc_page(slot_offset)) >> PAGE_SHIFT;
652 struct vm_page *pgs[npages];
653
654 KASSERT((umap->flags & UMAP_MAPPING_CACHED) == 0);
655 if (zerolen) {
656 memset((char *)umapva + endoff, 0, zerolen);
657 }
658 umap->flags &= ~UMAP_PAGES_LOCKED;
659 rw_enter(uobj->vmobjlock, RW_WRITER);
660 for (u_int i = 0; i < npages; i++) {
661 paddr_t pa;
662 bool rv __diagused;
663
664 rv = pmap_extract(pmap_kernel(),
665 umapva + slot_offset + (i << PAGE_SHIFT), &pa);
666 KASSERT(rv);
667 pgs[i] = PHYS_TO_VM_PAGE(pa);
668 pgs[i]->flags &= ~PG_FAKE;
669 KASSERTMSG(uvm_pagegetdirty(pgs[i]) ==
670 UVM_PAGE_STATUS_DIRTY,
671 "page %p not dirty", pgs[i]);
672 KASSERT(pgs[i]->loan_count == 0);
673 uvm_pagelock(pgs[i]);
674 uvm_pageactivate(pgs[i]);
675 uvm_pageunlock(pgs[i]);
676 }
677 pmap_kremove(umapva, ubc_winsize);
678 pmap_update(pmap_kernel());
679 uvm_page_unbusy(pgs, npages);
680 rw_exit(uobj->vmobjlock);
681 unmapped = true;
682 } else {
683 unmapped = false;
684 }
685
686 rw_enter(ubc_object.uobj.vmobjlock, RW_WRITER);
687 umap->writeoff = 0;
688 umap->writelen = 0;
689 umap->refcount--;
690 if (umap->refcount == 0) {
691 if (flags & UBC_UNMAP) {
692 /*
693 * Invalidate any cached mappings if requested.
694 * This is typically used to avoid leaving
695 * incompatible cache aliases around indefinitely.
696 */
697 rw_enter(uobj->vmobjlock, RW_WRITER);
698 pmap_remove(pmap_kernel(), umapva,
699 umapva + ubc_winsize);
700 pmap_update(pmap_kernel());
701 rw_exit(uobj->vmobjlock);
702
703 umap->flags &= ~UMAP_MAPPING_CACHED;
704 LIST_REMOVE(umap, hash);
705 LIST_REMOVE(umap, list);
706 umap->uobj = NULL;
707 TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap,
708 inactive);
709 } else {
710 if (!unmapped) {
711 umap->flags |= UMAP_MAPPING_CACHED;
712 }
713 TAILQ_INSERT_TAIL(UBC_QUEUE(umap->offset), umap,
714 inactive);
715 }
716 }
717 UVMHIST_LOG(ubchist, "umap %#jx refs %jd", (uintptr_t)umap,
718 umap->refcount, 0, 0);
719 rw_exit(ubc_object.uobj.vmobjlock);
720 }
721
722 /*
723 * ubc_uiomove: move data to/from an object.
724 */
725
726 int
727 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo, int advice,
728 int flags)
729 {
730 const bool overwrite = (flags & UBC_FAULTBUSY) != 0;
731 voff_t off;
732 int error;
733
734 KASSERT(todo <= uio->uio_resid);
735 KASSERT(((flags & UBC_WRITE) != 0 && uio->uio_rw == UIO_WRITE) ||
736 ((flags & UBC_READ) != 0 && uio->uio_rw == UIO_READ));
737
738 #ifdef UBC_USE_PMAP_DIRECT
739 if (ubc_direct) {
740 return ubc_uiomove_direct(uobj, uio, todo, advice, flags);
741 }
742 #endif
743
744 off = uio->uio_offset;
745 error = 0;
746 while (todo > 0) {
747 vsize_t bytelen = todo;
748 void *win;
749
750 win = ubc_alloc(uobj, off, &bytelen, advice, flags);
751 if (error == 0) {
752 error = uiomove(win, bytelen, uio);
753 }
754 if (error != 0 && overwrite) {
755 /*
756 * if we haven't initialized the pages yet,
757 * do it now. it's safe to use memset here
758 * because we just mapped the pages above.
759 */
760 printf("%s: error=%d\n", __func__, error);
761 memset(win, 0, bytelen);
762 }
763 ubc_release(win, flags);
764 off += bytelen;
765 todo -= bytelen;
766 if (error != 0 && (flags & UBC_PARTIALOK) != 0) {
767 break;
768 }
769 }
770
771 return error;
772 }
773
774 /*
775 * ubc_zerorange: set a range of bytes in an object to zero.
776 */
777
778 void
779 ubc_zerorange(struct uvm_object *uobj, off_t off, size_t len, int flags)
780 {
781
782 #ifdef UBC_USE_PMAP_DIRECT
783 if (ubc_direct) {
784 ubc_zerorange_direct(uobj, off, len, flags);
785 return;
786 }
787 #endif
788
789 /*
790 * XXXUBC invent kzero() and use it
791 */
792
793 while (len) {
794 void *win;
795 vsize_t bytelen = len;
796
797 win = ubc_alloc(uobj, off, &bytelen, UVM_ADV_NORMAL, UBC_WRITE);
798 memset(win, 0, bytelen);
799 ubc_release(win, flags);
800
801 off += bytelen;
802 len -= bytelen;
803 }
804 }
805
806 #ifdef UBC_USE_PMAP_DIRECT
807 /* Copy data using direct map */
808
809 /*
810 * ubc_alloc_direct: allocate a file mapping window using direct map
811 */
812 static int __noinline
813 ubc_alloc_direct(struct uvm_object *uobj, voff_t offset, vsize_t *lenp,
814 int advice, int flags, struct vm_page **pgs, int *npages)
815 {
816 voff_t pgoff;
817 int error;
818 int gpflags = flags | PGO_NOTIMESTAMP | PGO_SYNCIO | PGO_ALLPAGES;
819 int access_type = VM_PROT_READ;
820 UVMHIST_FUNC("ubc_alloc_direct"); UVMHIST_CALLED(ubchist);
821
822 if (flags & UBC_WRITE) {
823 if (flags & UBC_FAULTBUSY)
824 gpflags |= PGO_OVERWRITE;
825 #if 0
826 KASSERT(!UVM_OBJ_NEEDS_WRITEFAULT(uobj));
827 #endif
828
829 /*
830 * Tell genfs_getpages() we already have the journal lock,
831 * allow allocation past current EOF.
832 */
833 gpflags |= PGO_JOURNALLOCKED | PGO_PASTEOF;
834 access_type |= VM_PROT_WRITE;
835 } else {
836 /* Don't need the empty blocks allocated, PG_RDONLY is okay */
837 gpflags |= PGO_NOBLOCKALLOC;
838 }
839
840 pgoff = (offset & PAGE_MASK);
841 *lenp = MIN(*lenp, ubc_winsize - pgoff);
842
843 again:
844 *npages = (*lenp + pgoff + PAGE_SIZE - 1) >> PAGE_SHIFT;
845 KASSERT((*npages * PAGE_SIZE) <= ubc_winsize);
846 KASSERT(*lenp + pgoff <= ubc_winsize);
847 memset(pgs, 0, *npages * sizeof(pgs[0]));
848
849 rw_enter(uobj->vmobjlock, RW_WRITER);
850 error = (*uobj->pgops->pgo_get)(uobj, trunc_page(offset), pgs,
851 npages, 0, access_type, advice, gpflags);
852 UVMHIST_LOG(ubchist, "alloc_direct getpages %jd", error, 0, 0, 0);
853 if (error) {
854 if (error == EAGAIN) {
855 kpause("ubc_alloc_directg", false, hz >> 2, NULL);
856 goto again;
857 }
858 return error;
859 }
860
861 rw_enter(uobj->vmobjlock, RW_WRITER);
862 for (int i = 0; i < *npages; i++) {
863 struct vm_page *pg = pgs[i];
864
865 KASSERT(pg != NULL);
866 KASSERT(pg != PGO_DONTCARE);
867 KASSERT((pg->flags & PG_FAKE) == 0 || (gpflags & PGO_OVERWRITE));
868 KASSERT(pg->uobject->vmobjlock == uobj->vmobjlock);
869
870 /* Avoid breaking loan if possible, only do it on write */
871 if ((flags & UBC_WRITE) && pg->loan_count != 0) {
872 pg = uvm_loanbreak(pg);
873 if (pg == NULL) {
874 uvm_page_unbusy(pgs, *npages);
875 rw_exit(uobj->vmobjlock);
876 uvm_wait("ubc_alloc_directl");
877 goto again;
878 }
879 pgs[i] = pg;
880 }
881
882 /* Page must be writable by now */
883 KASSERT((pg->flags & PG_RDONLY) == 0 || (flags & UBC_WRITE) == 0);
884 }
885 rw_exit(uobj->vmobjlock);
886
887 return 0;
888 }
889
890 static void __noinline
891 ubc_direct_release(struct uvm_object *uobj,
892 int flags, struct vm_page **pgs, int npages)
893 {
894 rw_enter(uobj->vmobjlock, RW_WRITER);
895 for (int i = 0; i < npages; i++) {
896 struct vm_page *pg = pgs[i];
897
898 uvm_pagelock(pg);
899 uvm_pageactivate(pg);
900 uvm_pageunlock(pg);
901
902 /*
903 * Page was changed, no longer fake and neither clean.
904 * There's no managed mapping in the direct case, so
905 * mark the page dirty manually.
906 */
907 if (flags & UBC_WRITE) {
908 pg->flags &= ~PG_FAKE;
909 KASSERTMSG(uvm_pagegetdirty(pg) ==
910 UVM_PAGE_STATUS_DIRTY,
911 "page %p not dirty", pg);
912 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
913 }
914 }
915 uvm_page_unbusy(pgs, npages);
916 rw_exit(uobj->vmobjlock);
917 }
918
919 static int
920 ubc_uiomove_process(void *win, size_t len, void *arg)
921 {
922 struct uio *uio = (struct uio *)arg;
923
924 return uiomove(win, len, uio);
925 }
926
927 static int
928 ubc_zerorange_process(void *win, size_t len, void *arg)
929 {
930 memset(win, 0, len);
931 return 0;
932 }
933
934 static int __noinline
935 ubc_uiomove_direct(struct uvm_object *uobj, struct uio *uio, vsize_t todo, int advice,
936 int flags)
937 {
938 const bool overwrite = (flags & UBC_FAULTBUSY) != 0;
939 voff_t off;
940 int error, npages;
941 struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT];
942
943 KASSERT(todo <= uio->uio_resid);
944 KASSERT(((flags & UBC_WRITE) != 0 && uio->uio_rw == UIO_WRITE) ||
945 ((flags & UBC_READ) != 0 && uio->uio_rw == UIO_READ));
946
947 off = uio->uio_offset;
948 error = 0;
949 while (todo > 0) {
950 vsize_t bytelen = todo;
951
952 error = ubc_alloc_direct(uobj, off, &bytelen, advice, flags,
953 pgs, &npages);
954 if (error != 0) {
955 /* can't do anything, failed to get the pages */
956 break;
957 }
958
959 if (error == 0) {
960 error = uvm_direct_process(pgs, npages, off, bytelen,
961 ubc_uiomove_process, uio);
962 }
963 if (error != 0 && overwrite) {
964 /*
965 * if we haven't initialized the pages yet,
966 * do it now. it's safe to use memset here
967 * because we just mapped the pages above.
968 */
969 printf("%s: error=%d\n", __func__, error);
970 (void) uvm_direct_process(pgs, npages, off, bytelen,
971 ubc_zerorange_process, NULL);
972 }
973
974 ubc_direct_release(uobj, flags, pgs, npages);
975
976 off += bytelen;
977 todo -= bytelen;
978
979 if (error != 0 && ISSET(flags, UBC_PARTIALOK)) {
980 break;
981 }
982 }
983
984 return error;
985 }
986
987 static void __noinline
988 ubc_zerorange_direct(struct uvm_object *uobj, off_t off, size_t todo, int flags)
989 {
990 int error, npages;
991 struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT];
992
993 flags |= UBC_WRITE;
994
995 error = 0;
996 while (todo > 0) {
997 vsize_t bytelen = todo;
998
999 error = ubc_alloc_direct(uobj, off, &bytelen, UVM_ADV_NORMAL,
1000 flags, pgs, &npages);
1001 if (error != 0) {
1002 /* can't do anything, failed to get the pages */
1003 break;
1004 }
1005
1006 error = uvm_direct_process(pgs, npages, off, bytelen,
1007 ubc_zerorange_process, NULL);
1008
1009 ubc_direct_release(uobj, flags, pgs, npages);
1010
1011 off += bytelen;
1012 todo -= bytelen;
1013 }
1014 }
1015
1016 #endif /* UBC_USE_PMAP_DIRECT */
1017
1018 /*
1019 * ubc_purge: disassociate ubc_map structures from an empty uvm_object.
1020 */
1021
1022 void
1023 ubc_purge(struct uvm_object *uobj)
1024 {
1025 struct ubc_map *umap;
1026 vaddr_t va;
1027
1028 KASSERT(uobj->uo_npages == 0);
1029
1030 /*
1031 * Safe to check without lock held, as ubc_alloc() removes
1032 * the mapping and list entry in the correct order.
1033 */
1034 if (__predict_true(LIST_EMPTY(&uobj->uo_ubc))) {
1035 return;
1036 }
1037 rw_enter(ubc_object.uobj.vmobjlock, RW_WRITER);
1038 while ((umap = LIST_FIRST(&uobj->uo_ubc)) != NULL) {
1039 KASSERT(umap->refcount == 0);
1040 for (va = 0; va < ubc_winsize; va += PAGE_SIZE) {
1041 KASSERT(!pmap_extract(pmap_kernel(),
1042 va + UBC_UMAP_ADDR(umap), NULL));
1043 }
1044 LIST_REMOVE(umap, list);
1045 LIST_REMOVE(umap, hash);
1046 umap->flags &= ~UMAP_MAPPING_CACHED;
1047 umap->uobj = NULL;
1048 }
1049 rw_exit(ubc_object.uobj.vmobjlock);
1050 }
1051