uvm_fault.c revision 1.235 1 /* $NetBSD: uvm_fault.c,v 1.235 2023/09/01 10:57:20 andvar Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp
28 */
29
30 /*
31 * uvm_fault.c: fault handler
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.235 2023/09/01 10:57:20 andvar Exp $");
36
37 #include "opt_uvmhist.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/atomic.h>
42 #include <sys/kernel.h>
43 #include <sys/mman.h>
44
45 #include <uvm/uvm.h>
46 #include <uvm/uvm_pdpolicy.h>
47 #include <uvm/uvm_rndsource.h>
48
49 /*
50 *
51 * a word on page faults:
52 *
53 * types of page faults we handle:
54 *
55 * CASE 1: upper layer faults CASE 2: lower layer faults
56 *
57 * CASE 1A CASE 1B CASE 2A CASE 2B
58 * read/write1 write>1 read/write +-cow_write/zero
59 * | | | |
60 * +--|--+ +--|--+ +-----+ + | + | +-----+
61 * amap | V | | ---------> new | | | | ^ |
62 * +-----+ +-----+ +-----+ + | + | +--|--+
63 * | | |
64 * +-----+ +-----+ +--|--+ | +--|--+
65 * uobj | d/c | | d/c | | V | +----+ |
66 * +-----+ +-----+ +-----+ +-----+
67 *
68 * d/c = don't care
69 *
70 * case [0]: layerless fault
71 * no amap or uobj is present. this is an error.
72 *
73 * case [1]: upper layer fault [anon active]
74 * 1A: [read] or [write with anon->an_ref == 1]
75 * I/O takes place in upper level anon and uobj is not touched.
76 * 1B: [write with anon->an_ref > 1]
77 * new anon is alloc'd and data is copied off ["COW"]
78 *
79 * case [2]: lower layer fault [uobj]
80 * 2A: [read on non-NULL uobj] or [write to non-copy_on_write area]
81 * I/O takes place directly in object.
82 * 2B: [write to copy_on_write] or [read on NULL uobj]
83 * data is "promoted" from uobj to a new anon.
84 * if uobj is null, then we zero fill.
85 *
86 * we follow the standard UVM locking protocol ordering:
87 *
88 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
89 * we hold a PG_BUSY page if we unlock for I/O
90 *
91 *
92 * the code is structured as follows:
93 *
94 * - init the "IN" params in the ufi structure
95 * ReFault: (ERESTART returned to the loop in uvm_fault_internal)
96 * - do lookups [locks maps], check protection, handle needs_copy
97 * - check for case 0 fault (error)
98 * - establish "range" of fault
99 * - if we have an amap lock it and extract the anons
100 * - if sequential advice deactivate pages behind us
101 * - at the same time check pmap for unmapped areas and anon for pages
102 * that we could map in (and do map it if found)
103 * - check object for resident pages that we could map in
104 * - if (case 2) goto Case2
105 * - >>> handle case 1
106 * - ensure source anon is resident in RAM
107 * - if case 1B alloc new anon and copy from source
108 * - map the correct page in
109 * Case2:
110 * - >>> handle case 2
111 * - ensure source page is resident (if uobj)
112 * - if case 2B alloc new anon and copy from source (could be zero
113 * fill if uobj == NULL)
114 * - map the correct page in
115 * - done!
116 *
117 * note on paging:
118 * if we have to do I/O we place a PG_BUSY page in the correct object,
119 * unlock everything, and do the I/O. when I/O is done we must reverify
120 * the state of the world before assuming that our data structures are
121 * valid. [because mappings could change while the map is unlocked]
122 *
123 * alternative 1: unbusy the page in question and restart the page fault
124 * from the top (ReFault). this is easy but does not take advantage
125 * of the information that we already have from our previous lookup,
126 * although it is possible that the "hints" in the vm_map will help here.
127 *
128 * alternative 2: the system already keeps track of a "version" number of
129 * a map. [i.e. every time you write-lock a map (e.g. to change a
130 * mapping) you bump the version number up by one...] so, we can save
131 * the version number of the map before we release the lock and start I/O.
132 * then when I/O is done we can relock and check the version numbers
133 * to see if anything changed. this might save us some over 1 because
134 * we don't have to unbusy the page and may be less compares(?).
135 *
136 * alternative 3: put in backpointers or a way to "hold" part of a map
137 * in place while I/O is in progress. this could be complex to
138 * implement (especially with structures like amap that can be referenced
139 * by multiple map entries, and figuring out what should wait could be
140 * complex as well...).
141 *
142 * we use alternative 2. given that we are multi-threaded now we may want
143 * to reconsider the choice.
144 */
145
146 /*
147 * local data structures
148 */
149
150 struct uvm_advice {
151 int advice;
152 int nback;
153 int nforw;
154 };
155
156 /*
157 * page range array:
158 * note: index in array must match "advice" value
159 * XXX: borrowed numbers from freebsd. do they work well for us?
160 */
161
162 static const struct uvm_advice uvmadvice[] = {
163 { UVM_ADV_NORMAL, 3, 4 },
164 { UVM_ADV_RANDOM, 0, 0 },
165 { UVM_ADV_SEQUENTIAL, 8, 7},
166 };
167
168 #define UVM_MAXRANGE 16 /* must be MAX() of nback+nforw+1 */
169
170 /*
171 * private prototypes
172 */
173
174 /*
175 * inline functions
176 */
177
178 /*
179 * uvmfault_anonflush: try and deactivate pages in specified anons
180 *
181 * => does not have to deactivate page if it is busy
182 */
183
184 static inline void
185 uvmfault_anonflush(struct vm_anon **anons, int n)
186 {
187 int lcv;
188 struct vm_page *pg;
189
190 for (lcv = 0; lcv < n; lcv++) {
191 if (anons[lcv] == NULL)
192 continue;
193 KASSERT(rw_lock_held(anons[lcv]->an_lock));
194 pg = anons[lcv]->an_page;
195 if (pg && (pg->flags & PG_BUSY) == 0) {
196 uvm_pagelock(pg);
197 uvm_pagedeactivate(pg);
198 uvm_pageunlock(pg);
199 }
200 }
201 }
202
203 /*
204 * normal functions
205 */
206
207 /*
208 * uvmfault_amapcopy: clear "needs_copy" in a map.
209 *
210 * => called with VM data structures unlocked (usually, see below)
211 * => we get a write lock on the maps and clear needs_copy for a VA
212 * => if we are out of RAM we sleep (waiting for more)
213 */
214
215 static void
216 uvmfault_amapcopy(struct uvm_faultinfo *ufi)
217 {
218 for (;;) {
219
220 /*
221 * no mapping? give up.
222 */
223
224 if (uvmfault_lookup(ufi, true) == false)
225 return;
226
227 /*
228 * copy if needed.
229 */
230
231 if (UVM_ET_ISNEEDSCOPY(ufi->entry))
232 amap_copy(ufi->map, ufi->entry, AMAP_COPY_NOWAIT,
233 ufi->orig_rvaddr, ufi->orig_rvaddr + 1);
234
235 /*
236 * didn't work? must be out of RAM. unlock and sleep.
237 */
238
239 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
240 uvmfault_unlockmaps(ufi, true);
241 uvm_wait("fltamapcopy");
242 continue;
243 }
244
245 /*
246 * got it! unlock and return.
247 */
248
249 uvmfault_unlockmaps(ufi, true);
250 return;
251 }
252 /*NOTREACHED*/
253 }
254
255 /*
256 * uvmfault_anonget: get data in an anon into a non-busy, non-released
257 * page in that anon.
258 *
259 * => Map, amap and thus anon should be locked by caller.
260 * => If we fail, we unlock everything and error is returned.
261 * => If we are successful, return with everything still locked.
262 * => We do not move the page on the queues [gets moved later]. If we
263 * allocate a new page [we_own], it gets put on the queues. Either way,
264 * the result is that the page is on the queues at return time
265 * => For pages which are on loan from a uvm_object (and thus are not owned
266 * by the anon): if successful, return with the owning object locked.
267 * The caller must unlock this object when it unlocks everything else.
268 */
269
270 int
271 uvmfault_anonget(struct uvm_faultinfo *ufi, struct vm_amap *amap,
272 struct vm_anon *anon)
273 {
274 struct vm_page *pg;
275 krw_t lock_type;
276 int error;
277
278 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
279 KASSERT(rw_lock_held(anon->an_lock));
280 KASSERT(anon->an_lock == amap->am_lock);
281
282 /* Increment the counters.*/
283 cpu_count(CPU_COUNT_FLTANGET, 1);
284 if (anon->an_page) {
285 curlwp->l_ru.ru_minflt++;
286 } else {
287 curlwp->l_ru.ru_majflt++;
288 }
289 error = 0;
290
291 /*
292 * Loop until we get the anon data, or fail.
293 */
294
295 for (;;) {
296 bool we_own, locked;
297 /*
298 * Note: 'we_own' will become true if we set PG_BUSY on a page.
299 */
300 we_own = false;
301 pg = anon->an_page;
302
303 /*
304 * If there is a resident page and it is loaned, then anon
305 * may not own it. Call out to uvm_anon_lockloanpg() to
306 * identify and lock the real owner of the page.
307 */
308
309 if (pg && pg->loan_count)
310 pg = uvm_anon_lockloanpg(anon);
311
312 /*
313 * Is page resident? Make sure it is not busy/released.
314 */
315
316 lock_type = rw_lock_op(anon->an_lock);
317 if (pg) {
318
319 /*
320 * at this point, if the page has a uobject [meaning
321 * we have it on loan], then that uobject is locked
322 * by us! if the page is busy, we drop all the
323 * locks (including uobject) and try again.
324 */
325
326 if ((pg->flags & PG_BUSY) == 0) {
327 UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
328 return 0;
329 }
330 cpu_count(CPU_COUNT_FLTPGWAIT, 1);
331
332 /*
333 * The last unlock must be an atomic unlock and wait
334 * on the owner of page.
335 */
336
337 if (pg->uobject) {
338 /* Owner of page is UVM object. */
339 uvmfault_unlockall(ufi, amap, NULL);
340 UVMHIST_LOG(maphist, " unlock+wait on uobj",0,
341 0,0,0);
342 uvm_pagewait(pg, pg->uobject->vmobjlock, "anonget1");
343 } else {
344 /* Owner of page is anon. */
345 uvmfault_unlockall(ufi, NULL, NULL);
346 UVMHIST_LOG(maphist, " unlock+wait on anon",0,
347 0,0,0);
348 uvm_pagewait(pg, anon->an_lock, "anonget2");
349 }
350 } else {
351 #if defined(VMSWAP)
352 /*
353 * No page, therefore allocate one. A write lock is
354 * required for this. If the caller didn't supply
355 * one, fail now and have them retry.
356 */
357
358 if (lock_type == RW_READER) {
359 return ENOLCK;
360 }
361 pg = uvm_pagealloc(NULL,
362 ufi != NULL ? ufi->orig_rvaddr : 0,
363 anon, ufi != NULL ? UVM_FLAG_COLORMATCH : 0);
364 if (pg == NULL) {
365 /* Out of memory. Wait a little. */
366 uvmfault_unlockall(ufi, amap, NULL);
367 cpu_count(CPU_COUNT_FLTNORAM, 1);
368 UVMHIST_LOG(maphist, " noram -- UVM_WAIT",0,
369 0,0,0);
370 if (!uvm_reclaimable()) {
371 return ENOMEM;
372 }
373 uvm_wait("flt_noram1");
374 } else {
375 /* PG_BUSY bit is set. */
376 we_own = true;
377 uvmfault_unlockall(ufi, amap, NULL);
378
379 /*
380 * Pass a PG_BUSY+PG_FAKE clean page into
381 * the uvm_swap_get() function with all data
382 * structures unlocked. Note that it is OK
383 * to read an_swslot here, because we hold
384 * PG_BUSY on the page.
385 */
386 cpu_count(CPU_COUNT_PAGEINS, 1);
387 error = uvm_swap_get(pg, anon->an_swslot,
388 PGO_SYNCIO);
389
390 /*
391 * We clean up after the I/O below in the
392 * 'we_own' case.
393 */
394 }
395 #else
396 panic("%s: no page", __func__);
397 #endif /* defined(VMSWAP) */
398 }
399
400 /*
401 * Re-lock the map and anon.
402 */
403
404 locked = uvmfault_relock(ufi);
405 if (locked || we_own) {
406 rw_enter(anon->an_lock, lock_type);
407 }
408
409 /*
410 * If we own the page (i.e. we set PG_BUSY), then we need
411 * to clean up after the I/O. There are three cases to
412 * consider:
413 *
414 * 1) Page was released during I/O: free anon and ReFault.
415 * 2) I/O not OK. Free the page and cause the fault to fail.
416 * 3) I/O OK! Activate the page and sync with the non-we_own
417 * case (i.e. drop anon lock if not locked).
418 */
419
420 if (we_own) {
421 KASSERT(lock_type == RW_WRITER);
422 #if defined(VMSWAP)
423 if (error) {
424
425 /*
426 * Remove the swap slot from the anon and
427 * mark the anon as having no real slot.
428 * Do not free the swap slot, thus preventing
429 * it from being used again.
430 */
431
432 if (anon->an_swslot > 0) {
433 uvm_swap_markbad(anon->an_swslot, 1);
434 }
435 anon->an_swslot = SWSLOT_BAD;
436
437 if ((pg->flags & PG_RELEASED) != 0) {
438 goto released;
439 }
440
441 /*
442 * Note: page was never !PG_BUSY, so it
443 * cannot be mapped and thus no need to
444 * pmap_page_protect() it.
445 */
446
447 uvm_pagefree(pg);
448
449 if (locked) {
450 uvmfault_unlockall(ufi, NULL, NULL);
451 }
452 rw_exit(anon->an_lock);
453 UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0);
454 return error;
455 }
456
457 if ((pg->flags & PG_RELEASED) != 0) {
458 released:
459 KASSERT(anon->an_ref == 0);
460
461 /*
462 * Released while we had unlocked amap.
463 */
464
465 if (locked) {
466 uvmfault_unlockall(ufi, NULL, NULL);
467 }
468 uvm_anon_release(anon);
469
470 if (error) {
471 UVMHIST_LOG(maphist,
472 "<- ERROR/RELEASED", 0,0,0,0);
473 return error;
474 }
475
476 UVMHIST_LOG(maphist, "<- RELEASED", 0,0,0,0);
477 return ERESTART;
478 }
479
480 /*
481 * We have successfully read the page, activate it.
482 */
483
484 uvm_pagelock(pg);
485 uvm_pageactivate(pg);
486 uvm_pagewakeup(pg);
487 uvm_pageunlock(pg);
488 pg->flags &= ~(PG_BUSY|PG_FAKE);
489 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_UNKNOWN);
490 UVM_PAGE_OWN(pg, NULL);
491 #else
492 panic("%s: we_own", __func__);
493 #endif /* defined(VMSWAP) */
494 }
495
496 /*
497 * We were not able to re-lock the map - restart the fault.
498 */
499
500 if (!locked) {
501 if (we_own) {
502 rw_exit(anon->an_lock);
503 }
504 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
505 return ERESTART;
506 }
507
508 /*
509 * Verify that no one has touched the amap and moved
510 * the anon on us.
511 */
512
513 if (ufi != NULL && amap_lookup(&ufi->entry->aref,
514 ufi->orig_rvaddr - ufi->entry->start) != anon) {
515
516 uvmfault_unlockall(ufi, amap, NULL);
517 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
518 return ERESTART;
519 }
520
521 /*
522 * Retry..
523 */
524
525 cpu_count(CPU_COUNT_FLTANRETRY, 1);
526 continue;
527 }
528 /*NOTREACHED*/
529 }
530
531 /*
532 * uvmfault_promote: promote data to a new anon. used for 1B and 2B.
533 *
534 * 1. allocate an anon and a page.
535 * 2. fill its contents.
536 * 3. put it into amap.
537 *
538 * => if we fail (result != 0) we unlock everything.
539 * => on success, return a new locked anon via 'nanon'.
540 * (*nanon)->an_page will be a resident, locked, dirty page.
541 * => it's caller's responsibility to put the promoted nanon->an_page to the
542 * page queue.
543 */
544
545 static int
546 uvmfault_promote(struct uvm_faultinfo *ufi,
547 struct vm_anon *oanon,
548 struct vm_page *uobjpage,
549 struct vm_anon **nanon, /* OUT: allocated anon */
550 struct vm_anon **spare)
551 {
552 struct vm_amap *amap = ufi->entry->aref.ar_amap;
553 struct uvm_object *uobj;
554 struct vm_anon *anon;
555 struct vm_page *pg;
556 struct vm_page *opg;
557 int error;
558 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
559
560 if (oanon) {
561 /* anon COW */
562 opg = oanon->an_page;
563 KASSERT(opg != NULL);
564 KASSERT(opg->uobject == NULL || opg->loan_count > 0);
565 } else if (uobjpage != PGO_DONTCARE) {
566 /* object-backed COW */
567 opg = uobjpage;
568 KASSERT(rw_lock_held(opg->uobject->vmobjlock));
569 } else {
570 /* ZFOD */
571 opg = NULL;
572 }
573 if (opg != NULL) {
574 uobj = opg->uobject;
575 } else {
576 uobj = NULL;
577 }
578
579 KASSERT(amap != NULL);
580 KASSERT(uobjpage != NULL);
581 KASSERT(rw_write_held(amap->am_lock));
582 KASSERT(oanon == NULL || amap->am_lock == oanon->an_lock);
583 KASSERT(uobj == NULL || rw_lock_held(uobj->vmobjlock));
584
585 if (*spare != NULL) {
586 anon = *spare;
587 *spare = NULL;
588 } else {
589 anon = uvm_analloc();
590 }
591 if (anon) {
592
593 /*
594 * The new anon is locked.
595 *
596 * if opg == NULL, we want a zero'd, dirty page,
597 * so have uvm_pagealloc() do that for us.
598 */
599
600 KASSERT(anon->an_lock == NULL);
601 anon->an_lock = amap->am_lock;
602 pg = uvm_pagealloc(NULL, ufi->orig_rvaddr, anon,
603 UVM_FLAG_COLORMATCH | (opg == NULL ? UVM_PGA_ZERO : 0));
604 if (pg == NULL) {
605 anon->an_lock = NULL;
606 }
607 } else {
608 pg = NULL;
609 }
610
611 /*
612 * out of memory resources?
613 */
614
615 if (pg == NULL) {
616 /* save anon for the next try. */
617 if (anon != NULL) {
618 *spare = anon;
619 }
620
621 /* unlock and fail ... */
622 uvmfault_unlockall(ufi, amap, uobj);
623 if (!uvm_reclaimable()) {
624 UVMHIST_LOG(maphist, "out of VM", 0,0,0,0);
625 cpu_count(CPU_COUNT_FLTNOANON, 1);
626 error = ENOMEM;
627 goto done;
628 }
629
630 UVMHIST_LOG(maphist, "out of RAM, waiting for more", 0,0,0,0);
631 cpu_count(CPU_COUNT_FLTNORAM, 1);
632 uvm_wait("flt_noram5");
633 error = ERESTART;
634 goto done;
635 }
636
637 /*
638 * copy the page [pg now dirty]
639 *
640 * Remove the pmap entry now for the old page at this address
641 * so that no thread can modify the new page while any thread
642 * might still see the old page.
643 */
644 if (opg) {
645 pmap_remove(vm_map_pmap(ufi->orig_map), ufi->orig_rvaddr,
646 ufi->orig_rvaddr + PAGE_SIZE);
647 pmap_update(vm_map_pmap(ufi->orig_map));
648 uvm_pagecopy(opg, pg);
649 }
650 KASSERT(uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_DIRTY);
651
652 amap_add(&ufi->entry->aref, ufi->orig_rvaddr - ufi->entry->start, anon,
653 oanon != NULL);
654
655 /*
656 * from this point on am_lock won't be dropped until the page is
657 * entered, so it's safe to unbusy the page up front.
658 *
659 * uvm_fault_{upper,lower}_done will activate or enqueue the page.
660 */
661
662 pg = anon->an_page;
663 pg->flags &= ~(PG_BUSY|PG_FAKE);
664 UVM_PAGE_OWN(pg, NULL);
665
666 *nanon = anon;
667 error = 0;
668 done:
669 return error;
670 }
671
672 /*
673 * Update statistics after fault resolution.
674 * - maxrss
675 */
676 void
677 uvmfault_update_stats(struct uvm_faultinfo *ufi)
678 {
679 struct vm_map *map;
680 struct vmspace *vm;
681 struct proc *p;
682 vsize_t res;
683
684 map = ufi->orig_map;
685
686 p = curproc;
687 KASSERT(p != NULL);
688 vm = p->p_vmspace;
689
690 if (&vm->vm_map != map)
691 return;
692
693 res = pmap_resident_count(map->pmap);
694 if (vm->vm_rssmax < res)
695 vm->vm_rssmax = res;
696 }
697
698 /*
699 * F A U L T - m a i n e n t r y p o i n t
700 */
701
702 /*
703 * uvm_fault: page fault handler
704 *
705 * => called from MD code to resolve a page fault
706 * => VM data structures usually should be unlocked. however, it is
707 * possible to call here with the main map locked if the caller
708 * gets a write lock, sets it recursive, and then calls us (c.f.
709 * uvm_map_pageable). this should be avoided because it keeps
710 * the map locked off during I/O.
711 * => MUST NEVER BE CALLED IN INTERRUPT CONTEXT
712 */
713
714 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
715 ~VM_PROT_WRITE : VM_PROT_ALL)
716
717 /* fault_flag values passed from uvm_fault_wire to uvm_fault_internal */
718 #define UVM_FAULT_WIRE (1 << 0)
719 #define UVM_FAULT_MAXPROT (1 << 1)
720
721 struct uvm_faultctx {
722
723 /*
724 * the following members are set up by uvm_fault_check() and
725 * read-only after that.
726 *
727 * note that narrow is used by uvm_fault_check() to change
728 * the behaviour after ERESTART.
729 *
730 * most of them might change after RESTART if the underlying
731 * map entry has been changed behind us. an exception is
732 * wire_paging, which does never change.
733 */
734 vm_prot_t access_type;
735 vaddr_t startva;
736 int npages;
737 int centeridx;
738 bool narrow; /* work on a single requested page only */
739 bool wire_mapping; /* request a PMAP_WIRED mapping
740 (UVM_FAULT_WIRE or VM_MAPENT_ISWIRED) */
741 bool wire_paging; /* request uvm_pagewire
742 (true for UVM_FAULT_WIRE) */
743 bool cow_now; /* VM_PROT_WRITE is actually requested
744 (ie. should break COW and page loaning) */
745
746 /*
747 * enter_prot is set up by uvm_fault_check() and clamped
748 * (ie. drop the VM_PROT_WRITE bit) in various places in case
749 * of !cow_now.
750 */
751 vm_prot_t enter_prot; /* prot at which we want to enter pages in */
752
753 /*
754 * the following member is for uvmfault_promote() and ERESTART.
755 */
756 struct vm_anon *anon_spare;
757
758 /*
759 * the following is actually a uvm_fault_lower() internal.
760 * it's here merely for debugging.
761 * (or due to the mechanical separation of the function?)
762 */
763 bool promote;
764
765 /*
766 * type of lock to acquire on objects in both layers.
767 */
768 krw_t lower_lock_type;
769 krw_t upper_lock_type;
770 };
771
772 static inline int uvm_fault_check(
773 struct uvm_faultinfo *, struct uvm_faultctx *,
774 struct vm_anon ***, bool);
775
776 static int uvm_fault_upper(
777 struct uvm_faultinfo *, struct uvm_faultctx *,
778 struct vm_anon **);
779 static inline int uvm_fault_upper_lookup(
780 struct uvm_faultinfo *, const struct uvm_faultctx *,
781 struct vm_anon **, struct vm_page **);
782 static inline void uvm_fault_upper_neighbor(
783 struct uvm_faultinfo *, const struct uvm_faultctx *,
784 vaddr_t, struct vm_page *, bool);
785 static inline int uvm_fault_upper_loan(
786 struct uvm_faultinfo *, struct uvm_faultctx *,
787 struct vm_anon *, struct uvm_object **);
788 static inline int uvm_fault_upper_promote(
789 struct uvm_faultinfo *, struct uvm_faultctx *,
790 struct uvm_object *, struct vm_anon *);
791 static inline int uvm_fault_upper_direct(
792 struct uvm_faultinfo *, struct uvm_faultctx *,
793 struct uvm_object *, struct vm_anon *);
794 static int uvm_fault_upper_enter(
795 struct uvm_faultinfo *, const struct uvm_faultctx *,
796 struct uvm_object *, struct vm_anon *,
797 struct vm_page *, struct vm_anon *);
798 static inline void uvm_fault_upper_done(
799 struct uvm_faultinfo *, const struct uvm_faultctx *,
800 struct vm_anon *, struct vm_page *);
801
802 static int uvm_fault_lower(
803 struct uvm_faultinfo *, struct uvm_faultctx *,
804 struct vm_page **);
805 static inline void uvm_fault_lower_lookup(
806 struct uvm_faultinfo *, const struct uvm_faultctx *,
807 struct vm_page **);
808 static inline void uvm_fault_lower_neighbor(
809 struct uvm_faultinfo *, const struct uvm_faultctx *,
810 vaddr_t, struct vm_page *);
811 static inline int uvm_fault_lower_io(
812 struct uvm_faultinfo *, struct uvm_faultctx *,
813 struct uvm_object **, struct vm_page **);
814 static inline int uvm_fault_lower_direct(
815 struct uvm_faultinfo *, struct uvm_faultctx *,
816 struct uvm_object *, struct vm_page *);
817 static inline int uvm_fault_lower_direct_loan(
818 struct uvm_faultinfo *, struct uvm_faultctx *,
819 struct uvm_object *, struct vm_page **,
820 struct vm_page **);
821 static inline int uvm_fault_lower_promote(
822 struct uvm_faultinfo *, struct uvm_faultctx *,
823 struct uvm_object *, struct vm_page *);
824 static int uvm_fault_lower_enter(
825 struct uvm_faultinfo *, const struct uvm_faultctx *,
826 struct uvm_object *,
827 struct vm_anon *, struct vm_page *);
828 static inline void uvm_fault_lower_done(
829 struct uvm_faultinfo *, const struct uvm_faultctx *,
830 struct uvm_object *, struct vm_page *);
831
832 int
833 uvm_fault_internal(struct vm_map *orig_map, vaddr_t vaddr,
834 vm_prot_t access_type, int fault_flag)
835 {
836 struct uvm_faultinfo ufi;
837 struct uvm_faultctx flt = {
838 .access_type = access_type,
839
840 /* don't look for neighborhood * pages on "wire" fault */
841 .narrow = (fault_flag & UVM_FAULT_WIRE) != 0,
842
843 /* "wire" fault causes wiring of both mapping and paging */
844 .wire_mapping = (fault_flag & UVM_FAULT_WIRE) != 0,
845 .wire_paging = (fault_flag & UVM_FAULT_WIRE) != 0,
846
847 /*
848 * default lock type to acquire on upper & lower layer
849 * objects: reader. this can be upgraded at any point
850 * during the fault from read -> write and uvm_faultctx
851 * changed to match, but is never downgraded write -> read.
852 */
853 #ifdef __HAVE_UNLOCKED_PMAP /* XXX temporary */
854 .upper_lock_type = RW_WRITER,
855 .lower_lock_type = RW_WRITER,
856 #else
857 .upper_lock_type = RW_READER,
858 .lower_lock_type = RW_READER,
859 #endif
860 };
861 const bool maxprot = (fault_flag & UVM_FAULT_MAXPROT) != 0;
862 struct vm_anon *anons_store[UVM_MAXRANGE], **anons;
863 struct vm_page *pages_store[UVM_MAXRANGE], **pages;
864 int error;
865
866 UVMHIST_FUNC(__func__);
867 UVMHIST_CALLARGS(maphist, "(map=%#jx, vaddr=%#jx, at=%jd, ff=%jd)",
868 (uintptr_t)orig_map, vaddr, access_type, fault_flag);
869
870 /* Don't count anything until user interaction is possible */
871 kpreempt_disable();
872 if (__predict_true(start_init_exec)) {
873 struct cpu_info *ci = curcpu();
874 CPU_COUNT(CPU_COUNT_NFAULT, 1);
875 /* Don't flood RNG subsystem with samples. */
876 if (++(ci->ci_faultrng) == 503) {
877 ci->ci_faultrng = 0;
878 rnd_add_uint32(&uvm_fault_rndsource,
879 sizeof(vaddr_t) == sizeof(uint32_t) ?
880 (uint32_t)vaddr : sizeof(vaddr_t) ==
881 sizeof(uint64_t) ?
882 (uint32_t)vaddr :
883 (uint32_t)ci->ci_counts[CPU_COUNT_NFAULT]);
884 }
885 }
886 kpreempt_enable();
887
888 /*
889 * init the IN parameters in the ufi
890 */
891
892 ufi.orig_map = orig_map;
893 ufi.orig_rvaddr = trunc_page(vaddr);
894 ufi.orig_size = PAGE_SIZE; /* can't get any smaller than this */
895
896 error = ERESTART;
897 while (error == ERESTART) { /* ReFault: */
898 anons = anons_store;
899 pages = pages_store;
900
901 error = uvm_fault_check(&ufi, &flt, &anons, maxprot);
902 if (error != 0)
903 continue;
904
905 error = uvm_fault_upper_lookup(&ufi, &flt, anons, pages);
906 if (error != 0)
907 continue;
908
909 if (pages[flt.centeridx] == PGO_DONTCARE)
910 error = uvm_fault_upper(&ufi, &flt, anons);
911 else {
912 struct uvm_object * const uobj =
913 ufi.entry->object.uvm_obj;
914
915 if (uobj && uobj->pgops->pgo_fault != NULL) {
916 /*
917 * invoke "special" fault routine.
918 */
919 rw_enter(uobj->vmobjlock, RW_WRITER);
920 /* locked: maps(read), amap(if there), uobj */
921 error = uobj->pgops->pgo_fault(&ufi,
922 flt.startva, pages, flt.npages,
923 flt.centeridx, flt.access_type,
924 PGO_LOCKED|PGO_SYNCIO);
925
926 /*
927 * locked: nothing, pgo_fault has unlocked
928 * everything
929 */
930
931 /*
932 * object fault routine responsible for
933 * pmap_update().
934 */
935
936 /*
937 * Wake up the pagedaemon if the fault method
938 * failed for lack of memory but some can be
939 * reclaimed.
940 */
941 if (error == ENOMEM && uvm_reclaimable()) {
942 uvm_wait("pgo_fault");
943 error = ERESTART;
944 }
945 } else {
946 error = uvm_fault_lower(&ufi, &flt, pages);
947 }
948 }
949 }
950
951 if (flt.anon_spare != NULL) {
952 flt.anon_spare->an_ref--;
953 KASSERT(flt.anon_spare->an_ref == 0);
954 KASSERT(flt.anon_spare->an_lock == NULL);
955 uvm_anfree(flt.anon_spare);
956 }
957 return error;
958 }
959
960 /*
961 * uvm_fault_check: check prot, handle needs-copy, etc.
962 *
963 * 1. lookup entry.
964 * 2. check protection.
965 * 3. adjust fault condition (mainly for simulated fault).
966 * 4. handle needs-copy (lazy amap copy).
967 * 5. establish range of interest for neighbor fault (aka pre-fault).
968 * 6. look up anons (if amap exists).
969 * 7. flush pages (if MADV_SEQUENTIAL)
970 *
971 * => called with nothing locked.
972 * => if we fail (result != 0) we unlock everything.
973 * => initialize/adjust many members of flt.
974 */
975
976 static int
977 uvm_fault_check(
978 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
979 struct vm_anon ***ranons, bool maxprot)
980 {
981 struct vm_amap *amap;
982 struct uvm_object *uobj;
983 vm_prot_t check_prot;
984 int nback, nforw;
985 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
986
987 /*
988 * lookup and lock the maps
989 */
990
991 if (uvmfault_lookup(ufi, false) == false) {
992 UVMHIST_LOG(maphist, "<- no mapping @ %#jx", ufi->orig_rvaddr,
993 0,0,0);
994 return EFAULT;
995 }
996 /* locked: maps(read) */
997
998 #ifdef DIAGNOSTIC
999 if ((ufi->map->flags & VM_MAP_PAGEABLE) == 0) {
1000 printf("Page fault on non-pageable map:\n");
1001 printf("ufi->map = %p\n", ufi->map);
1002 printf("ufi->orig_map = %p\n", ufi->orig_map);
1003 printf("ufi->orig_rvaddr = %#lx\n", (u_long) ufi->orig_rvaddr);
1004 panic("uvm_fault: (ufi->map->flags & VM_MAP_PAGEABLE) == 0");
1005 }
1006 #endif
1007
1008 /*
1009 * check protection
1010 */
1011
1012 check_prot = maxprot ?
1013 ufi->entry->max_protection : ufi->entry->protection;
1014 if ((check_prot & flt->access_type) != flt->access_type) {
1015 UVMHIST_LOG(maphist,
1016 "<- protection failure (prot=%#jx, access=%#jx)",
1017 ufi->entry->protection, flt->access_type, 0, 0);
1018 uvmfault_unlockmaps(ufi, false);
1019 return EFAULT;
1020 }
1021
1022 /*
1023 * "enter_prot" is the protection we want to enter the page in at.
1024 * for certain pages (e.g. copy-on-write pages) this protection can
1025 * be more strict than ufi->entry->protection. "wired" means either
1026 * the entry is wired or we are fault-wiring the pg.
1027 */
1028
1029 flt->enter_prot = ufi->entry->protection;
1030 if (VM_MAPENT_ISWIRED(ufi->entry)) {
1031 flt->wire_mapping = true;
1032 flt->wire_paging = true;
1033 flt->narrow = true;
1034 }
1035
1036 if (flt->wire_mapping) {
1037 flt->access_type = flt->enter_prot; /* full access for wired */
1038 flt->cow_now = (check_prot & VM_PROT_WRITE) != 0;
1039 } else {
1040 flt->cow_now = (flt->access_type & VM_PROT_WRITE) != 0;
1041 }
1042
1043 if (flt->wire_paging) {
1044 /* wiring pages requires a write lock. */
1045 flt->upper_lock_type = RW_WRITER;
1046 flt->lower_lock_type = RW_WRITER;
1047 }
1048
1049 flt->promote = false;
1050
1051 /*
1052 * handle "needs_copy" case. if we need to copy the amap we will
1053 * have to drop our readlock and relock it with a write lock. (we
1054 * need a write lock to change anything in a map entry [e.g.
1055 * needs_copy]).
1056 */
1057
1058 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
1059 if (flt->cow_now || (ufi->entry->object.uvm_obj == NULL)) {
1060 KASSERT(!maxprot);
1061 /* need to clear */
1062 UVMHIST_LOG(maphist,
1063 " need to clear needs_copy and refault",0,0,0,0);
1064 uvmfault_unlockmaps(ufi, false);
1065 uvmfault_amapcopy(ufi);
1066 cpu_count(CPU_COUNT_FLTAMCOPY, 1);
1067 return ERESTART;
1068
1069 } else {
1070
1071 /*
1072 * ensure that we pmap_enter page R/O since
1073 * needs_copy is still true
1074 */
1075
1076 flt->enter_prot &= ~VM_PROT_WRITE;
1077 }
1078 }
1079
1080 /*
1081 * identify the players
1082 */
1083
1084 amap = ufi->entry->aref.ar_amap; /* upper layer */
1085 uobj = ufi->entry->object.uvm_obj; /* lower layer */
1086
1087 /*
1088 * check for a case 0 fault. if nothing backing the entry then
1089 * error now.
1090 */
1091
1092 if (amap == NULL && uobj == NULL) {
1093 uvmfault_unlockmaps(ufi, false);
1094 UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
1095 return EFAULT;
1096 }
1097
1098 /*
1099 * for a case 2B fault waste no time on adjacent pages because
1100 * they are likely already entered.
1101 */
1102
1103 if (uobj != NULL && amap != NULL &&
1104 (flt->access_type & VM_PROT_WRITE) != 0) {
1105 /* wide fault (!narrow) */
1106 flt->narrow = true;
1107 }
1108
1109 /*
1110 * establish range of interest based on advice from mapper
1111 * and then clip to fit map entry. note that we only want
1112 * to do this the first time through the fault. if we
1113 * ReFault we will disable this by setting "narrow" to true.
1114 */
1115
1116 if (flt->narrow == false) {
1117
1118 /* wide fault (!narrow) */
1119 KASSERT(uvmadvice[ufi->entry->advice].advice ==
1120 ufi->entry->advice);
1121 nback = MIN(uvmadvice[ufi->entry->advice].nback,
1122 (ufi->orig_rvaddr - ufi->entry->start) >> PAGE_SHIFT);
1123 flt->startva = ufi->orig_rvaddr - (nback << PAGE_SHIFT);
1124 /*
1125 * note: "-1" because we don't want to count the
1126 * faulting page as forw
1127 */
1128 nforw = MIN(uvmadvice[ufi->entry->advice].nforw,
1129 ((ufi->entry->end - ufi->orig_rvaddr) >>
1130 PAGE_SHIFT) - 1);
1131 flt->npages = nback + nforw + 1;
1132 flt->centeridx = nback;
1133
1134 flt->narrow = true; /* ensure only once per-fault */
1135
1136 } else {
1137
1138 /* narrow fault! */
1139 nback = nforw = 0;
1140 flt->startva = ufi->orig_rvaddr;
1141 flt->npages = 1;
1142 flt->centeridx = 0;
1143
1144 }
1145 /* offset from entry's start to pgs' start */
1146 const voff_t eoff = flt->startva - ufi->entry->start;
1147
1148 /* locked: maps(read) */
1149 UVMHIST_LOG(maphist, " narrow=%jd, back=%jd, forw=%jd, startva=%#jx",
1150 flt->narrow, nback, nforw, flt->startva);
1151 UVMHIST_LOG(maphist, " entry=%#jx, amap=%#jx, obj=%#jx",
1152 (uintptr_t)ufi->entry, (uintptr_t)amap, (uintptr_t)uobj, 0);
1153
1154 /*
1155 * guess at the most suitable lock types to acquire.
1156 * if we've got an amap then lock it and extract current anons.
1157 */
1158
1159 if (amap) {
1160 if ((amap_flags(amap) & AMAP_SHARED) == 0) {
1161 /*
1162 * the amap isn't shared. get a writer lock to
1163 * avoid the cost of upgrading the lock later if
1164 * needed.
1165 *
1166 * XXX nice for PostgreSQL, but consider threads.
1167 */
1168 flt->upper_lock_type = RW_WRITER;
1169 } else if ((flt->access_type & VM_PROT_WRITE) != 0) {
1170 /*
1171 * assume we're about to COW.
1172 */
1173 flt->upper_lock_type = RW_WRITER;
1174 }
1175 amap_lock(amap, flt->upper_lock_type);
1176 amap_lookups(&ufi->entry->aref, eoff, *ranons, flt->npages);
1177 } else {
1178 if ((flt->access_type & VM_PROT_WRITE) != 0) {
1179 /*
1180 * we are about to dirty the object and that
1181 * requires a write lock.
1182 */
1183 flt->lower_lock_type = RW_WRITER;
1184 }
1185 *ranons = NULL; /* to be safe */
1186 }
1187
1188 /* locked: maps(read), amap(if there) */
1189 KASSERT(amap == NULL ||
1190 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1191
1192 /*
1193 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
1194 * now and then forget about them (for the rest of the fault).
1195 */
1196
1197 if (ufi->entry->advice == MADV_SEQUENTIAL && nback != 0) {
1198
1199 UVMHIST_LOG(maphist, " MADV_SEQUENTIAL: flushing backpages",
1200 0,0,0,0);
1201 /* flush back-page anons? */
1202 if (amap)
1203 uvmfault_anonflush(*ranons, nback);
1204
1205 /*
1206 * flush object? change lock type to RW_WRITER, to avoid
1207 * excessive competition between read/write locks if many
1208 * threads doing "sequential access".
1209 */
1210 if (uobj) {
1211 voff_t uoff;
1212
1213 flt->lower_lock_type = RW_WRITER;
1214 uoff = ufi->entry->offset + eoff;
1215 rw_enter(uobj->vmobjlock, RW_WRITER);
1216 (void) (uobj->pgops->pgo_put)(uobj, uoff, uoff +
1217 (nback << PAGE_SHIFT), PGO_DEACTIVATE);
1218 }
1219
1220 /* now forget about the backpages */
1221 if (amap)
1222 *ranons += nback;
1223 flt->startva += (nback << PAGE_SHIFT);
1224 flt->npages -= nback;
1225 flt->centeridx = 0;
1226 }
1227 /*
1228 * => startva is fixed
1229 * => npages is fixed
1230 */
1231 KASSERT(flt->startva <= ufi->orig_rvaddr);
1232 KASSERT(ufi->orig_rvaddr + ufi->orig_size <=
1233 flt->startva + (flt->npages << PAGE_SHIFT));
1234 return 0;
1235 }
1236
1237 /*
1238 * uvm_fault_upper_upgrade: upgrade upper lock, reader -> writer
1239 */
1240
1241 static inline int
1242 uvm_fault_upper_upgrade(struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1243 struct vm_amap *amap, struct uvm_object *uobj)
1244 {
1245 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1246
1247 KASSERT(amap != NULL);
1248 KASSERT(flt->upper_lock_type == rw_lock_op(amap->am_lock));
1249
1250 /*
1251 * fast path.
1252 */
1253
1254 if (__predict_true(flt->upper_lock_type == RW_WRITER)) {
1255 return 0;
1256 }
1257
1258 /*
1259 * otherwise try for the upgrade. if we don't get it, unlock
1260 * everything, restart the fault and next time around get a writer
1261 * lock.
1262 */
1263
1264 flt->upper_lock_type = RW_WRITER;
1265 if (__predict_false(!rw_tryupgrade(amap->am_lock))) {
1266 uvmfault_unlockall(ufi, amap, uobj);
1267 cpu_count(CPU_COUNT_FLTNOUP, 1);
1268 UVMHIST_LOG(maphist, " !upgrade upper", 0, 0,0,0);
1269 return ERESTART;
1270 }
1271 cpu_count(CPU_COUNT_FLTUP, 1);
1272 KASSERT(flt->upper_lock_type == rw_lock_op(amap->am_lock));
1273 return 0;
1274 }
1275
1276 /*
1277 * uvm_fault_upper_lookup: look up existing h/w mapping and amap.
1278 *
1279 * iterate range of interest:
1280 * 1. check if h/w mapping exists. if yes, we don't care
1281 * 2. check if anon exists. if not, page is lower.
1282 * 3. if anon exists, enter h/w mapping for neighbors.
1283 *
1284 * => called with amap locked (if exists).
1285 */
1286
1287 static int
1288 uvm_fault_upper_lookup(
1289 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1290 struct vm_anon **anons, struct vm_page **pages)
1291 {
1292 struct vm_amap *amap = ufi->entry->aref.ar_amap;
1293 int lcv;
1294 vaddr_t currva;
1295 bool shadowed __unused;
1296 bool entered;
1297 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1298
1299 /* locked: maps(read), amap(if there) */
1300 KASSERT(amap == NULL ||
1301 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1302
1303 /*
1304 * map in the backpages and frontpages we found in the amap in hopes
1305 * of preventing future faults. we also init the pages[] array as
1306 * we go.
1307 */
1308
1309 currva = flt->startva;
1310 shadowed = false;
1311 entered = false;
1312 for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
1313 /*
1314 * unmapped or center page. check if any anon at this level.
1315 */
1316 if (amap == NULL || anons[lcv] == NULL) {
1317 pages[lcv] = NULL;
1318 continue;
1319 }
1320
1321 /*
1322 * check for present page and map if possible.
1323 */
1324
1325 pages[lcv] = PGO_DONTCARE;
1326 if (lcv == flt->centeridx) { /* save center for later! */
1327 shadowed = true;
1328 continue;
1329 }
1330
1331 struct vm_anon *anon = anons[lcv];
1332 struct vm_page *pg = anon->an_page;
1333
1334 KASSERT(anon->an_lock == amap->am_lock);
1335
1336 /*
1337 * ignore loaned and busy pages.
1338 * don't play with VAs that are already mapped.
1339 */
1340
1341 if (pg && pg->loan_count == 0 && (pg->flags & PG_BUSY) == 0 &&
1342 !pmap_extract(ufi->orig_map->pmap, currva, NULL)) {
1343 uvm_fault_upper_neighbor(ufi, flt, currva,
1344 pg, anon->an_ref > 1);
1345 entered = true;
1346 }
1347 }
1348 if (entered) {
1349 pmap_update(ufi->orig_map->pmap);
1350 }
1351
1352 /* locked: maps(read), amap(if there) */
1353 KASSERT(amap == NULL ||
1354 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1355 /* (shadowed == true) if there is an anon at the faulting address */
1356 UVMHIST_LOG(maphist, " shadowed=%jd, will_get=%jd", shadowed,
1357 (ufi->entry->object.uvm_obj && shadowed != false),0,0);
1358
1359 return 0;
1360 }
1361
1362 /*
1363 * uvm_fault_upper_neighbor: enter single upper neighbor page.
1364 *
1365 * => called with amap and anon locked.
1366 */
1367
1368 static void
1369 uvm_fault_upper_neighbor(
1370 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1371 vaddr_t currva, struct vm_page *pg, bool readonly)
1372 {
1373 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1374
1375 /* locked: amap, anon */
1376
1377 KASSERT(pg->uobject == NULL);
1378 KASSERT(pg->uanon != NULL);
1379 KASSERT(rw_lock_op(pg->uanon->an_lock) == flt->upper_lock_type);
1380 KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);
1381
1382 /*
1383 * there wasn't a direct fault on the page, so avoid the cost of
1384 * activating it.
1385 */
1386
1387 if (!uvmpdpol_pageisqueued_p(pg) && pg->wire_count == 0) {
1388 uvm_pagelock(pg);
1389 uvm_pageenqueue(pg);
1390 uvm_pageunlock(pg);
1391 }
1392
1393 UVMHIST_LOG(maphist,
1394 " MAPPING: n anon: pm=%#jx, va=%#jx, pg=%#jx",
1395 (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
1396 cpu_count(CPU_COUNT_FLTNAMAP, 1);
1397
1398 /*
1399 * Since this page isn't the page that's actually faulting,
1400 * ignore pmap_enter() failures; it's not critical that we
1401 * enter these right now.
1402 */
1403
1404 (void) pmap_enter(ufi->orig_map->pmap, currva,
1405 VM_PAGE_TO_PHYS(pg),
1406 readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
1407 flt->enter_prot,
1408 PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0));
1409 }
1410
1411 /*
1412 * uvm_fault_upper: handle upper fault.
1413 *
1414 * 1. acquire anon lock.
1415 * 2. get anon. let uvmfault_anonget do the dirty work.
1416 * 3. handle loan.
1417 * 4. dispatch direct or promote handlers.
1418 */
1419
1420 static int
1421 uvm_fault_upper(
1422 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1423 struct vm_anon **anons)
1424 {
1425 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1426 struct vm_anon * const anon = anons[flt->centeridx];
1427 struct uvm_object *uobj;
1428 int error;
1429 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1430
1431 /* locked: maps(read), amap, anon */
1432 KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1433 KASSERT(anon->an_lock == amap->am_lock);
1434
1435 /*
1436 * handle case 1: fault on an anon in our amap
1437 */
1438
1439 UVMHIST_LOG(maphist, " case 1 fault: anon=%#jx",
1440 (uintptr_t)anon, 0, 0, 0);
1441
1442 /*
1443 * no matter if we have case 1A or case 1B we are going to need to
1444 * have the anon's memory resident. ensure that now.
1445 */
1446
1447 /*
1448 * let uvmfault_anonget do the dirty work.
1449 * if it fails (!OK) it will unlock everything for us.
1450 * if it succeeds, locks are still valid and locked.
1451 * also, if it is OK, then the anon's page is on the queues.
1452 * if the page is on loan from a uvm_object, then anonget will
1453 * lock that object for us if it does not fail.
1454 */
1455 retry:
1456 error = uvmfault_anonget(ufi, amap, anon);
1457 switch (error) {
1458 case 0:
1459 break;
1460
1461 case ERESTART:
1462 return ERESTART;
1463
1464 case EAGAIN:
1465 kpause("fltagain1", false, hz/2, NULL);
1466 return ERESTART;
1467
1468 case ENOLCK:
1469 /* it needs a write lock: retry */
1470 error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
1471 if (error != 0) {
1472 return error;
1473 }
1474 KASSERT(rw_write_held(amap->am_lock));
1475 goto retry;
1476
1477 default:
1478 return error;
1479 }
1480
1481 /*
1482 * uobj is non null if the page is on loan from an object (i.e. uobj)
1483 */
1484
1485 uobj = anon->an_page->uobject; /* locked by anonget if !NULL */
1486
1487 /* locked: maps(read), amap, anon, uobj(if one) */
1488 KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1489 KASSERT(anon->an_lock == amap->am_lock);
1490 KASSERT(uobj == NULL ||
1491 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
1492
1493 /*
1494 * special handling for loaned pages
1495 */
1496
1497 if (anon->an_page->loan_count) {
1498 error = uvm_fault_upper_loan(ufi, flt, anon, &uobj);
1499 if (error != 0)
1500 return error;
1501 }
1502
1503 /*
1504 * if we are case 1B then we will need to allocate a new blank
1505 * anon to transfer the data into. note that we have a lock
1506 * on anon, so no one can busy or release the page until we are done.
1507 * also note that the ref count can't drop to zero here because
1508 * it is > 1 and we are only dropping one ref.
1509 *
1510 * in the (hopefully very rare) case that we are out of RAM we
1511 * will unlock, wait for more RAM, and refault.
1512 *
1513 * if we are out of anon VM we kill the process (XXX: could wait?).
1514 */
1515
1516 if (flt->cow_now && anon->an_ref > 1) {
1517 flt->promote = true;
1518 error = uvm_fault_upper_promote(ufi, flt, uobj, anon);
1519 } else {
1520 error = uvm_fault_upper_direct(ufi, flt, uobj, anon);
1521 }
1522 return error;
1523 }
1524
1525 /*
1526 * uvm_fault_upper_loan: handle loaned upper page.
1527 *
1528 * 1. if not cow'ing now, simply adjust flt->enter_prot.
1529 * 2. if cow'ing now, and if ref count is 1, break loan.
1530 */
1531
1532 static int
1533 uvm_fault_upper_loan(
1534 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1535 struct vm_anon *anon, struct uvm_object **ruobj)
1536 {
1537 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1538 int error = 0;
1539 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1540
1541 if (!flt->cow_now) {
1542
1543 /*
1544 * for read faults on loaned pages we just cap the
1545 * protection at read-only.
1546 */
1547
1548 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1549
1550 } else {
1551 /*
1552 * note that we can't allow writes into a loaned page!
1553 *
1554 * if we have a write fault on a loaned page in an
1555 * anon then we need to look at the anon's ref count.
1556 * if it is greater than one then we are going to do
1557 * a normal copy-on-write fault into a new anon (this
1558 * is not a problem). however, if the reference count
1559 * is one (a case where we would normally allow a
1560 * write directly to the page) then we need to kill
1561 * the loan before we continue.
1562 */
1563
1564 /* >1 case is already ok */
1565 if (anon->an_ref == 1) {
1566 /* breaking loan requires a write lock. */
1567 error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
1568 if (error != 0) {
1569 return error;
1570 }
1571 KASSERT(rw_write_held(amap->am_lock));
1572
1573 error = uvm_loanbreak_anon(anon, *ruobj);
1574 if (error != 0) {
1575 uvmfault_unlockall(ufi, amap, *ruobj);
1576 uvm_wait("flt_noram2");
1577 return ERESTART;
1578 }
1579 /* if we were a loan receiver uobj is gone */
1580 if (*ruobj)
1581 *ruobj = NULL;
1582 }
1583 }
1584 return error;
1585 }
1586
1587 /*
1588 * uvm_fault_upper_promote: promote upper page.
1589 *
1590 * 1. call uvmfault_promote.
1591 * 2. enqueue page.
1592 * 3. deref.
1593 * 4. pass page to uvm_fault_upper_enter.
1594 */
1595
1596 static int
1597 uvm_fault_upper_promote(
1598 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1599 struct uvm_object *uobj, struct vm_anon *anon)
1600 {
1601 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1602 struct vm_anon * const oanon = anon;
1603 struct vm_page *pg;
1604 int error;
1605 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1606
1607 UVMHIST_LOG(maphist, " case 1B: COW fault",0,0,0,0);
1608 cpu_count(CPU_COUNT_FLT_ACOW, 1);
1609
1610 /* promoting requires a write lock. */
1611 error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
1612 if (error != 0) {
1613 return error;
1614 }
1615 KASSERT(rw_write_held(amap->am_lock));
1616
1617 error = uvmfault_promote(ufi, oanon, PGO_DONTCARE, &anon,
1618 &flt->anon_spare);
1619 switch (error) {
1620 case 0:
1621 break;
1622 case ERESTART:
1623 return ERESTART;
1624 default:
1625 return error;
1626 }
1627 pg = anon->an_page;
1628
1629 KASSERT(anon->an_lock == oanon->an_lock);
1630 KASSERT((pg->flags & (PG_BUSY | PG_FAKE)) == 0);
1631
1632 /* deref: can not drop to zero here by defn! */
1633 KASSERT(oanon->an_ref > 1);
1634 oanon->an_ref--;
1635
1636 /*
1637 * note: oanon is still locked, as is the new anon. we
1638 * need to check for this later when we unlock oanon; if
1639 * oanon != anon, we'll have to unlock anon, too.
1640 */
1641
1642 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
1643 }
1644
1645 /*
1646 * uvm_fault_upper_direct: handle direct fault.
1647 */
1648
1649 static int
1650 uvm_fault_upper_direct(
1651 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1652 struct uvm_object *uobj, struct vm_anon *anon)
1653 {
1654 struct vm_anon * const oanon = anon;
1655 struct vm_page *pg;
1656 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1657
1658 cpu_count(CPU_COUNT_FLT_ANON, 1);
1659 pg = anon->an_page;
1660 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */
1661 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1662
1663 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
1664 }
1665
1666 /*
1667 * uvm_fault_upper_enter: enter h/w mapping of upper page.
1668 */
1669
1670 static int
1671 uvm_fault_upper_enter(
1672 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1673 struct uvm_object *uobj, struct vm_anon *anon, struct vm_page *pg,
1674 struct vm_anon *oanon)
1675 {
1676 struct pmap *pmap = ufi->orig_map->pmap;
1677 vaddr_t va = ufi->orig_rvaddr;
1678 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1679 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1680
1681 /* locked: maps(read), amap, oanon, anon(if different from oanon) */
1682 KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1683 KASSERT(anon->an_lock == amap->am_lock);
1684 KASSERT(oanon->an_lock == amap->am_lock);
1685 KASSERT(uobj == NULL ||
1686 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
1687 KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);
1688
1689 /*
1690 * now map the page in.
1691 */
1692
1693 UVMHIST_LOG(maphist,
1694 " MAPPING: anon: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
1695 (uintptr_t)pmap, va, (uintptr_t)pg, flt->promote);
1696 if (pmap_enter(pmap, va, VM_PAGE_TO_PHYS(pg),
1697 flt->enter_prot, flt->access_type | PMAP_CANFAIL |
1698 (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {
1699
1700 /*
1701 * If pmap_enter() fails, it must not leave behind an existing
1702 * pmap entry. In particular, a now-stale entry for a different
1703 * page would leave the pmap inconsistent with the vm_map.
1704 * This is not to imply that pmap_enter() should remove an
1705 * existing mapping in such a situation (since that could create
1706 * different problems, eg. if the existing mapping is wired),
1707 * but rather that the pmap should be designed such that it
1708 * never needs to fail when the new mapping is replacing an
1709 * existing mapping and the new page has no existing mappings.
1710 *
1711 * XXX This can't be asserted safely any more because many
1712 * LWPs and/or many processes could simultaneously fault on
1713 * the same VA and some might succeed.
1714 */
1715
1716 /* KASSERT(!pmap_extract(pmap, va, NULL)); */
1717
1718 /*
1719 * ensure that the page is queued in the case that
1720 * we just promoted.
1721 */
1722
1723 uvm_pagelock(pg);
1724 uvm_pageenqueue(pg);
1725 uvm_pageunlock(pg);
1726
1727 /*
1728 * No need to undo what we did; we can simply think of
1729 * this as the pmap throwing away the mapping information.
1730 *
1731 * We do, however, have to go through the ReFault path,
1732 * as the map may change while we're asleep.
1733 */
1734
1735 uvmfault_unlockall(ufi, amap, uobj);
1736 if (!uvm_reclaimable()) {
1737 UVMHIST_LOG(maphist,
1738 "<- failed. out of VM",0,0,0,0);
1739 /* XXX instrumentation */
1740 return ENOMEM;
1741 }
1742 /* XXX instrumentation */
1743 uvm_wait("flt_pmfail1");
1744 return ERESTART;
1745 }
1746
1747 uvm_fault_upper_done(ufi, flt, anon, pg);
1748
1749 /*
1750 * done case 1! finish up by unlocking everything and returning success
1751 */
1752
1753 pmap_update(pmap);
1754 uvmfault_unlockall(ufi, amap, uobj);
1755 return 0;
1756 }
1757
1758 /*
1759 * uvm_fault_upper_done: queue upper center page.
1760 */
1761
1762 static void
1763 uvm_fault_upper_done(
1764 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1765 struct vm_anon *anon, struct vm_page *pg)
1766 {
1767 const bool wire_paging = flt->wire_paging;
1768
1769 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1770
1771 /*
1772 * ... update the page queues.
1773 */
1774
1775 if (wire_paging) {
1776 uvm_pagelock(pg);
1777 uvm_pagewire(pg);
1778 uvm_pageunlock(pg);
1779
1780 /*
1781 * since the now-wired page cannot be paged out,
1782 * release its swap resources for others to use.
1783 * and since an anon with no swap cannot be clean,
1784 * mark it dirty now.
1785 */
1786
1787 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
1788 uvm_anon_dropswap(anon);
1789 } else if (uvmpdpol_pageactivate_p(pg)) {
1790 /*
1791 * avoid re-activating the page unless needed,
1792 * to avoid false sharing on multiprocessor.
1793 */
1794
1795 uvm_pagelock(pg);
1796 uvm_pageactivate(pg);
1797 uvm_pageunlock(pg);
1798 }
1799 }
1800
1801 /*
1802 * uvm_fault_lower_upgrade: upgrade lower lock, reader -> writer
1803 */
1804
1805 static inline int
1806 uvm_fault_lower_upgrade(struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1807 struct vm_amap *amap, struct uvm_object *uobj, struct vm_page *uobjpage)
1808 {
1809
1810 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1811
1812 KASSERT(uobj != NULL);
1813 KASSERT(flt->lower_lock_type == rw_lock_op(uobj->vmobjlock));
1814
1815 /*
1816 * fast path.
1817 */
1818
1819 if (__predict_true(flt->lower_lock_type == RW_WRITER)) {
1820 return 0;
1821 }
1822
1823 /*
1824 * otherwise try for the upgrade. if we don't get it, unlock
1825 * everything, restart the fault and next time around get a writer
1826 * lock.
1827 */
1828
1829 flt->lower_lock_type = RW_WRITER;
1830 if (__predict_false(!rw_tryupgrade(uobj->vmobjlock))) {
1831 uvmfault_unlockall(ufi, amap, uobj);
1832 cpu_count(CPU_COUNT_FLTNOUP, 1);
1833 UVMHIST_LOG(maphist, " !upgrade lower", 0, 0,0,0);
1834 return ERESTART;
1835 }
1836 cpu_count(CPU_COUNT_FLTUP, 1);
1837 KASSERT(flt->lower_lock_type == rw_lock_op(uobj->vmobjlock));
1838 return 0;
1839 }
1840
1841 /*
1842 * uvm_fault_lower: handle lower fault.
1843 *
1844 * 1. check uobj
1845 * 1.1. if null, ZFOD.
1846 * 1.2. if not null, look up unmapped neighbor pages.
1847 * 2. for center page, check if promote.
1848 * 2.1. ZFOD always needs promotion.
1849 * 2.2. other uobjs, when entry is marked COW (usually MAP_PRIVATE vnode).
1850 * 3. if uobj is not ZFOD and page is not found, do i/o.
1851 * 4. dispatch either direct / promote fault.
1852 */
1853
1854 static int
1855 uvm_fault_lower(
1856 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1857 struct vm_page **pages)
1858 {
1859 struct vm_amap *amap __diagused = ufi->entry->aref.ar_amap;
1860 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1861 struct vm_page *uobjpage;
1862 int error;
1863 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1864
1865 /*
1866 * now, if the desired page is not shadowed by the amap and we have
1867 * a backing object that does not have a special fault routine, then
1868 * we ask (with pgo_get) the object for resident pages that we care
1869 * about and attempt to map them in. we do not let pgo_get block
1870 * (PGO_LOCKED).
1871 */
1872
1873 if (uobj == NULL) {
1874 /* zero fill; don't care neighbor pages */
1875 uobjpage = NULL;
1876 } else {
1877 uvm_fault_lower_lookup(ufi, flt, pages);
1878 uobjpage = pages[flt->centeridx];
1879 }
1880
1881 /*
1882 * note that at this point we are done with any front or back pages.
1883 * we are now going to focus on the center page (i.e. the one we've
1884 * faulted on). if we have faulted on the upper (anon) layer
1885 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
1886 * not touched it yet). if we have faulted on the bottom (uobj)
1887 * layer [i.e. case 2] and the page was both present and available,
1888 * then we've got a pointer to it as "uobjpage" and we've already
1889 * made it BUSY.
1890 */
1891
1892 /*
1893 * locked:
1894 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
1895 */
1896 KASSERT(amap == NULL ||
1897 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1898 KASSERT(uobj == NULL ||
1899 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
1900
1901 /*
1902 * note that uobjpage can not be PGO_DONTCARE at this point. we now
1903 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we
1904 * have a backing object, check and see if we are going to promote
1905 * the data up to an anon during the fault.
1906 */
1907
1908 if (uobj == NULL) {
1909 uobjpage = PGO_DONTCARE;
1910 flt->promote = true; /* always need anon here */
1911 } else {
1912 KASSERT(uobjpage != PGO_DONTCARE);
1913 flt->promote = flt->cow_now && UVM_ET_ISCOPYONWRITE(ufi->entry);
1914 }
1915 UVMHIST_LOG(maphist, " case 2 fault: promote=%jd, zfill=%jd",
1916 flt->promote, (uobj == NULL), 0,0);
1917
1918 /*
1919 * if uobjpage is not null then we do not need to do I/O to get the
1920 * uobjpage.
1921 *
1922 * if uobjpage is null, then we need to unlock and ask the pager to
1923 * get the data for us. once we have the data, we need to reverify
1924 * the state the world. we are currently not holding any resources.
1925 */
1926
1927 if (uobjpage) {
1928 /* update rusage counters */
1929 curlwp->l_ru.ru_minflt++;
1930 } else {
1931 error = uvm_fault_lower_io(ufi, flt, &uobj, &uobjpage);
1932 if (error != 0)
1933 return error;
1934 }
1935
1936 /*
1937 * locked:
1938 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
1939 */
1940 KASSERT(amap == NULL ||
1941 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1942 KASSERT(uobj == NULL ||
1943 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
1944
1945 /*
1946 * notes:
1947 * - at this point uobjpage can not be NULL
1948 * - at this point uobjpage can not be PG_RELEASED (since we checked
1949 * for it above)
1950 * - at this point uobjpage could be waited on (handle later)
1951 * - uobjpage can be from a different object if tmpfs (vnode vs UAO)
1952 */
1953
1954 KASSERT(uobjpage != NULL);
1955 KASSERT(uobj == NULL ||
1956 uobjpage->uobject->vmobjlock == uobj->vmobjlock);
1957 KASSERT(uobj == NULL || !UVM_OBJ_IS_CLEAN(uobjpage->uobject) ||
1958 uvm_pagegetdirty(uobjpage) == UVM_PAGE_STATUS_CLEAN);
1959
1960 if (!flt->promote) {
1961 error = uvm_fault_lower_direct(ufi, flt, uobj, uobjpage);
1962 } else {
1963 error = uvm_fault_lower_promote(ufi, flt, uobj, uobjpage);
1964 }
1965 return error;
1966 }
1967
1968 /*
1969 * uvm_fault_lower_lookup: look up on-memory uobj pages.
1970 *
1971 * 1. get on-memory pages.
1972 * 2. if failed, give up (get only center page later).
1973 * 3. if succeeded, enter h/w mapping of neighbor pages.
1974 */
1975
1976 static void
1977 uvm_fault_lower_lookup(
1978 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1979 struct vm_page **pages)
1980 {
1981 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1982 int lcv, gotpages;
1983 vaddr_t currva;
1984 bool entered;
1985 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1986
1987 rw_enter(uobj->vmobjlock, flt->lower_lock_type);
1988
1989 /*
1990 * Locked: maps(read), amap(if there), uobj
1991 */
1992
1993 cpu_count(CPU_COUNT_FLTLGET, 1);
1994 gotpages = flt->npages;
1995 (void) uobj->pgops->pgo_get(uobj,
1996 ufi->entry->offset + flt->startva - ufi->entry->start,
1997 pages, &gotpages, flt->centeridx,
1998 flt->access_type & MASK(ufi->entry), ufi->entry->advice,
1999 PGO_LOCKED);
2000
2001 KASSERT(rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
2002
2003 /*
2004 * check for pages to map, if we got any
2005 */
2006
2007 if (gotpages == 0) {
2008 pages[flt->centeridx] = NULL;
2009 return;
2010 }
2011
2012 entered = false;
2013 currva = flt->startva;
2014 for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
2015 struct vm_page *curpg;
2016
2017 curpg = pages[lcv];
2018 if (curpg == NULL || curpg == PGO_DONTCARE) {
2019 continue;
2020 }
2021
2022 /*
2023 * in the case of tmpfs, the pages might be from a different
2024 * uvm_object. just make sure that they have the same lock.
2025 */
2026
2027 KASSERT(curpg->uobject->vmobjlock == uobj->vmobjlock);
2028 KASSERT((curpg->flags & PG_BUSY) == 0);
2029
2030 /*
2031 * leave the centre page for later. don't screw with
2032 * existing mappings (needless & expensive).
2033 */
2034
2035 if (lcv == flt->centeridx) {
2036 UVMHIST_LOG(maphist, " got uobjpage (%#jx) "
2037 "with locked get", (uintptr_t)curpg, 0, 0, 0);
2038 } else if (!pmap_extract(ufi->orig_map->pmap, currva, NULL)) {
2039 uvm_fault_lower_neighbor(ufi, flt, currva, curpg);
2040 entered = true;
2041 }
2042 }
2043 if (entered) {
2044 pmap_update(ufi->orig_map->pmap);
2045 }
2046 }
2047
2048 /*
2049 * uvm_fault_lower_neighbor: enter h/w mapping of lower neighbor page.
2050 */
2051
2052 static void
2053 uvm_fault_lower_neighbor(
2054 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
2055 vaddr_t currva, struct vm_page *pg)
2056 {
2057 const bool readonly = uvm_pagereadonly_p(pg) || pg->loan_count > 0;
2058 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
2059
2060 /* locked: maps(read), amap(if there), uobj */
2061
2062 /*
2063 * calling pgo_get with PGO_LOCKED returns us pages which
2064 * are neither busy nor released, so we don't need to check
2065 * for this. we can just directly enter the pages.
2066 *
2067 * there wasn't a direct fault on the page, so avoid the cost of
2068 * activating it.
2069 */
2070
2071 if (!uvmpdpol_pageisqueued_p(pg) && pg->wire_count == 0) {
2072 uvm_pagelock(pg);
2073 uvm_pageenqueue(pg);
2074 uvm_pageunlock(pg);
2075 }
2076
2077 UVMHIST_LOG(maphist,
2078 " MAPPING: n obj: pm=%#jx, va=%#jx, pg=%#jx",
2079 (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
2080 cpu_count(CPU_COUNT_FLTNOMAP, 1);
2081
2082 /*
2083 * Since this page isn't the page that's actually faulting,
2084 * ignore pmap_enter() failures; it's not critical that we
2085 * enter these right now.
2086 * NOTE: page can't be waited on or PG_RELEASED because we've
2087 * held the lock the whole time we've had the handle.
2088 */
2089 KASSERT((pg->flags & PG_PAGEOUT) == 0);
2090 KASSERT((pg->flags & PG_RELEASED) == 0);
2091 KASSERT(!UVM_OBJ_IS_CLEAN(pg->uobject) ||
2092 uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_CLEAN);
2093 KASSERT((pg->flags & PG_BUSY) == 0);
2094 KASSERT(rw_lock_op(pg->uobject->vmobjlock) == flt->lower_lock_type);
2095
2096 const vm_prot_t mapprot =
2097 readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
2098 flt->enter_prot & MASK(ufi->entry);
2099 const u_int mapflags =
2100 PMAP_CANFAIL | (flt->wire_mapping ? (mapprot | PMAP_WIRED) : 0);
2101 (void) pmap_enter(ufi->orig_map->pmap, currva,
2102 VM_PAGE_TO_PHYS(pg), mapprot, mapflags);
2103 }
2104
2105 /*
2106 * uvm_fault_lower_io: get lower page from backing store.
2107 *
2108 * 1. unlock everything, because i/o will block.
2109 * 2. call pgo_get.
2110 * 3. if failed, recover.
2111 * 4. if succeeded, relock everything and verify things.
2112 */
2113
2114 static int
2115 uvm_fault_lower_io(
2116 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2117 struct uvm_object **ruobj, struct vm_page **ruobjpage)
2118 {
2119 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2120 struct uvm_object *uobj = *ruobj;
2121 struct vm_page *pg;
2122 bool locked;
2123 int gotpages;
2124 int error;
2125 voff_t uoff;
2126 vm_prot_t access_type;
2127 int advice;
2128 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
2129
2130 /* update rusage counters */
2131 curlwp->l_ru.ru_majflt++;
2132
2133 /* grab everything we need from the entry before we unlock */
2134 uoff = (ufi->orig_rvaddr - ufi->entry->start) + ufi->entry->offset;
2135 access_type = flt->access_type & MASK(ufi->entry);
2136 advice = ufi->entry->advice;
2137
2138 /* Locked: maps(read), amap(if there), uobj */
2139 KASSERT(rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
2140
2141 /* Upgrade to a write lock if needed. */
2142 error = uvm_fault_lower_upgrade(ufi, flt, amap, uobj, NULL);
2143 if (error != 0) {
2144 return error;
2145 }
2146 uvmfault_unlockall(ufi, amap, NULL);
2147
2148 /* Locked: uobj(write) */
2149 KASSERT(rw_write_held(uobj->vmobjlock));
2150
2151 cpu_count(CPU_COUNT_FLTGET, 1);
2152 gotpages = 1;
2153 pg = NULL;
2154 error = uobj->pgops->pgo_get(uobj, uoff, &pg, &gotpages,
2155 0, access_type, advice, PGO_SYNCIO);
2156 /* locked: pg(if no error) */
2157
2158 /*
2159 * recover from I/O
2160 */
2161
2162 if (error) {
2163 if (error == EAGAIN) {
2164 UVMHIST_LOG(maphist,
2165 " pgo_get says TRY AGAIN!",0,0,0,0);
2166 kpause("fltagain2", false, hz/2, NULL);
2167 return ERESTART;
2168 }
2169
2170 #if 0
2171 KASSERT(error != ERESTART);
2172 #else
2173 /* XXXUEBS don't re-fault? */
2174 if (error == ERESTART)
2175 error = EIO;
2176 #endif
2177
2178 UVMHIST_LOG(maphist, "<- pgo_get failed (code %jd)",
2179 error, 0,0,0);
2180 return error;
2181 }
2182
2183 /*
2184 * re-verify the state of the world by first trying to relock
2185 * the maps. always relock the object.
2186 */
2187
2188 locked = uvmfault_relock(ufi);
2189 if (locked && amap)
2190 amap_lock(amap, flt->upper_lock_type);
2191
2192 /* might be changed */
2193 uobj = pg->uobject;
2194
2195 rw_enter(uobj->vmobjlock, flt->lower_lock_type);
2196 KASSERT((pg->flags & PG_BUSY) != 0);
2197 KASSERT(flt->lower_lock_type == RW_WRITER);
2198
2199 uvm_pagelock(pg);
2200 uvm_pageactivate(pg);
2201 uvm_pageunlock(pg);
2202
2203 /* locked(locked): maps(read), amap(if !null), uobj, pg */
2204 /* locked(!locked): uobj, pg */
2205
2206 /*
2207 * verify that the page has not be released and re-verify
2208 * that amap slot is still free. if there is a problem,
2209 * we unlock and clean up.
2210 */
2211
2212 if ((pg->flags & PG_RELEASED) != 0 ||
2213 (locked && amap && amap_lookup(&ufi->entry->aref,
2214 ufi->orig_rvaddr - ufi->entry->start))) {
2215 if (locked)
2216 uvmfault_unlockall(ufi, amap, NULL);
2217 locked = false;
2218 }
2219
2220 /*
2221 * unbusy/release the page.
2222 */
2223
2224 if ((pg->flags & PG_RELEASED) == 0) {
2225 pg->flags &= ~PG_BUSY;
2226 uvm_pagelock(pg);
2227 uvm_pagewakeup(pg);
2228 uvm_pageunlock(pg);
2229 UVM_PAGE_OWN(pg, NULL);
2230 } else {
2231 cpu_count(CPU_COUNT_FLTPGRELE, 1);
2232 uvm_pagefree(pg);
2233 }
2234
2235 /*
2236 * didn't get the lock? retry.
2237 */
2238
2239 if (locked == false) {
2240 UVMHIST_LOG(maphist,
2241 " wasn't able to relock after fault: retry",
2242 0,0,0,0);
2243 rw_exit(uobj->vmobjlock);
2244 return ERESTART;
2245 }
2246
2247 /*
2248 * we have the data in pg. we are holding object lock (so the page
2249 * can't be released on us).
2250 */
2251
2252 /* locked: maps(read), amap(if !null), uobj */
2253
2254 *ruobj = uobj;
2255 *ruobjpage = pg;
2256 return 0;
2257 }
2258
2259 /*
2260 * uvm_fault_lower_direct: fault lower center page
2261 *
2262 * 1. adjust flt->enter_prot.
2263 * 2. if page is loaned, resolve.
2264 */
2265
2266 int
2267 uvm_fault_lower_direct(
2268 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2269 struct uvm_object *uobj, struct vm_page *uobjpage)
2270 {
2271 struct vm_page *pg;
2272 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
2273
2274 /*
2275 * we are not promoting. if the mapping is COW ensure that we
2276 * don't give more access than we should (e.g. when doing a read
2277 * fault on a COPYONWRITE mapping we want to map the COW page in
2278 * R/O even though the entry protection could be R/W).
2279 *
2280 * set "pg" to the page we want to map in (uobjpage, usually)
2281 */
2282
2283 cpu_count(CPU_COUNT_FLT_OBJ, 1);
2284 if (UVM_ET_ISCOPYONWRITE(ufi->entry) ||
2285 UVM_OBJ_NEEDS_WRITEFAULT(uobjpage->uobject))
2286 flt->enter_prot &= ~VM_PROT_WRITE;
2287 pg = uobjpage; /* map in the actual object */
2288
2289 KASSERT(uobjpage != PGO_DONTCARE);
2290
2291 /*
2292 * we are faulting directly on the page. be careful
2293 * about writing to loaned pages...
2294 */
2295
2296 if (uobjpage->loan_count) {
2297 uvm_fault_lower_direct_loan(ufi, flt, uobj, &pg, &uobjpage);
2298 }
2299 KASSERT(pg == uobjpage);
2300 KASSERT((pg->flags & PG_BUSY) == 0);
2301 return uvm_fault_lower_enter(ufi, flt, uobj, NULL, pg);
2302 }
2303
2304 /*
2305 * uvm_fault_lower_direct_loan: resolve loaned page.
2306 *
2307 * 1. if not cow'ing, adjust flt->enter_prot.
2308 * 2. if cow'ing, break loan.
2309 */
2310
2311 static int
2312 uvm_fault_lower_direct_loan(
2313 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2314 struct uvm_object *uobj, struct vm_page **rpg,
2315 struct vm_page **ruobjpage)
2316 {
2317 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2318 struct vm_page *pg;
2319 struct vm_page *uobjpage = *ruobjpage;
2320 int error;
2321 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
2322
2323 if (!flt->cow_now) {
2324 /* read fault: cap the protection at readonly */
2325 /* cap! */
2326 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
2327 } else {
2328 /*
2329 * write fault: must break the loan here. to do this
2330 * we need a write lock on the object.
2331 */
2332
2333 error = uvm_fault_lower_upgrade(ufi, flt, amap, uobj, uobjpage);
2334 if (error != 0) {
2335 return error;
2336 }
2337 KASSERT(rw_write_held(uobj->vmobjlock));
2338
2339 pg = uvm_loanbreak(uobjpage);
2340 if (pg == NULL) {
2341
2342 uvmfault_unlockall(ufi, amap, uobj);
2343 UVMHIST_LOG(maphist,
2344 " out of RAM breaking loan, waiting",
2345 0,0,0,0);
2346 cpu_count(CPU_COUNT_FLTNORAM, 1);
2347 uvm_wait("flt_noram4");
2348 return ERESTART;
2349 }
2350 *rpg = pg;
2351 *ruobjpage = pg;
2352
2353 /*
2354 * drop ownership of page while still holding object lock,
2355 * which won't be dropped until the page is entered.
2356 */
2357
2358 uvm_pagelock(pg);
2359 uvm_pagewakeup(pg);
2360 uvm_pageunlock(pg);
2361 pg->flags &= ~PG_BUSY;
2362 UVM_PAGE_OWN(pg, NULL);
2363 }
2364 return 0;
2365 }
2366
2367 /*
2368 * uvm_fault_lower_promote: promote lower page.
2369 *
2370 * 1. call uvmfault_promote.
2371 * 2. fill in data.
2372 * 3. if not ZFOD, dispose old page.
2373 */
2374
2375 int
2376 uvm_fault_lower_promote(
2377 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2378 struct uvm_object *uobj, struct vm_page *uobjpage)
2379 {
2380 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2381 struct vm_anon *anon;
2382 struct vm_page *pg;
2383 int error;
2384 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
2385
2386 KASSERT(amap != NULL);
2387
2388 /* promoting requires a write lock. */
2389 error = uvm_fault_upper_upgrade(ufi, flt, amap, uobj);
2390 if (error != 0) {
2391 return error;
2392 }
2393 KASSERT(rw_write_held(amap->am_lock));
2394 KASSERT(uobj == NULL ||
2395 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
2396
2397 /*
2398 * If we are going to promote the data to an anon we
2399 * allocate a blank anon here and plug it into our amap.
2400 */
2401 error = uvmfault_promote(ufi, NULL, uobjpage, &anon, &flt->anon_spare);
2402 switch (error) {
2403 case 0:
2404 break;
2405 case ERESTART:
2406 return ERESTART;
2407 default:
2408 return error;
2409 }
2410
2411 pg = anon->an_page;
2412
2413 /*
2414 * Fill in the data.
2415 */
2416
2417 if (uobjpage != PGO_DONTCARE) {
2418 cpu_count(CPU_COUNT_FLT_PRCOPY, 1);
2419
2420 /*
2421 * promote to shared amap? make sure all sharing
2422 * procs see it
2423 */
2424
2425 if ((amap_flags(amap) & AMAP_SHARED) != 0) {
2426 pmap_page_protect(uobjpage, VM_PROT_NONE);
2427 /*
2428 * XXX: PAGE MIGHT BE WIRED!
2429 */
2430 }
2431
2432 UVMHIST_LOG(maphist,
2433 " promote uobjpage %#jx to anon/page %#jx/%#jx",
2434 (uintptr_t)uobjpage, (uintptr_t)anon, (uintptr_t)pg, 0);
2435
2436 } else {
2437 cpu_count(CPU_COUNT_FLT_PRZERO, 1);
2438
2439 /*
2440 * Page is zero'd and marked dirty by
2441 * uvmfault_promote().
2442 */
2443
2444 UVMHIST_LOG(maphist," zero fill anon/page %#jx/%#jx",
2445 (uintptr_t)anon, (uintptr_t)pg, 0, 0);
2446 }
2447
2448 return uvm_fault_lower_enter(ufi, flt, uobj, anon, pg);
2449 }
2450
2451 /*
2452 * uvm_fault_lower_enter: enter h/w mapping of lower page or anon page promoted
2453 * from the lower page.
2454 */
2455
2456 int
2457 uvm_fault_lower_enter(
2458 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
2459 struct uvm_object *uobj,
2460 struct vm_anon *anon, struct vm_page *pg)
2461 {
2462 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2463 const bool readonly = uvm_pagereadonly_p(pg);
2464 int error;
2465 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
2466
2467 /*
2468 * Locked:
2469 *
2470 * maps(read), amap(if !null), uobj(if !null),
2471 * anon(if !null), pg(if anon), unlock_uobj(if !null)
2472 *
2473 * anon must be write locked (promotion). uobj can be either.
2474 *
2475 * Note: pg is either the uobjpage or the new page in the new anon.
2476 */
2477
2478 KASSERT(amap == NULL ||
2479 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
2480 KASSERT(uobj == NULL ||
2481 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
2482 KASSERT(anon == NULL || anon->an_lock == amap->am_lock);
2483
2484 /*
2485 * note that pg can't be PG_RELEASED or PG_BUSY since we did
2486 * not drop the object lock since the last time we checked.
2487 */
2488
2489 KASSERT((pg->flags & PG_RELEASED) == 0);
2490 KASSERT((pg->flags & PG_BUSY) == 0);
2491
2492 /*
2493 * all resources are present. we can now map it in and free our
2494 * resources.
2495 */
2496
2497 UVMHIST_LOG(maphist,
2498 " MAPPING: case2: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
2499 (uintptr_t)ufi->orig_map->pmap, ufi->orig_rvaddr,
2500 (uintptr_t)pg, flt->promote);
2501 KASSERTMSG((flt->access_type & VM_PROT_WRITE) == 0 || !readonly,
2502 "promote=%u cow_now=%u access_type=%x enter_prot=%x cow=%u "
2503 "entry=%p map=%p orig_rvaddr=%p pg=%p",
2504 flt->promote, flt->cow_now, flt->access_type, flt->enter_prot,
2505 UVM_ET_ISCOPYONWRITE(ufi->entry), ufi->entry, ufi->orig_map,
2506 (void *)ufi->orig_rvaddr, pg);
2507 KASSERT((flt->access_type & VM_PROT_WRITE) == 0 || !readonly);
2508 if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr,
2509 VM_PAGE_TO_PHYS(pg),
2510 readonly ? flt->enter_prot & ~VM_PROT_WRITE : flt->enter_prot,
2511 flt->access_type | PMAP_CANFAIL |
2512 (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {
2513
2514 /*
2515 * No need to undo what we did; we can simply think of
2516 * this as the pmap throwing away the mapping information.
2517 *
2518 * We do, however, have to go through the ReFault path,
2519 * as the map may change while we're asleep.
2520 */
2521
2522 /*
2523 * ensure that the page is queued in the case that
2524 * we just promoted the page.
2525 */
2526
2527 if (anon != NULL) {
2528 uvm_pagelock(pg);
2529 uvm_pageenqueue(pg);
2530 uvm_pagewakeup(pg);
2531 uvm_pageunlock(pg);
2532 }
2533
2534 uvmfault_unlockall(ufi, amap, uobj);
2535 if (!uvm_reclaimable()) {
2536 UVMHIST_LOG(maphist,
2537 "<- failed. out of VM",0,0,0,0);
2538 /* XXX instrumentation */
2539 error = ENOMEM;
2540 return error;
2541 }
2542 /* XXX instrumentation */
2543 uvm_wait("flt_pmfail2");
2544 return ERESTART;
2545 }
2546
2547 uvm_fault_lower_done(ufi, flt, uobj, pg);
2548 pmap_update(ufi->orig_map->pmap);
2549 uvmfault_unlockall(ufi, amap, uobj);
2550
2551 UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
2552 return 0;
2553 }
2554
2555 /*
2556 * uvm_fault_lower_done: queue lower center page.
2557 */
2558
2559 void
2560 uvm_fault_lower_done(
2561 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
2562 struct uvm_object *uobj, struct vm_page *pg)
2563 {
2564
2565 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
2566
2567 if (flt->wire_paging) {
2568 uvm_pagelock(pg);
2569 uvm_pagewire(pg);
2570 uvm_pageunlock(pg);
2571 if (pg->flags & PG_AOBJ) {
2572
2573 /*
2574 * since the now-wired page cannot be paged out,
2575 * release its swap resources for others to use.
2576 * since an aobj page with no swap cannot be clean,
2577 * mark it dirty now.
2578 *
2579 * use pg->uobject here. if the page is from a
2580 * tmpfs vnode, the pages are backed by its UAO and
2581 * not the vnode.
2582 */
2583
2584 KASSERT(uobj != NULL);
2585 KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
2586 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
2587 uao_dropswap(pg->uobject, pg->offset >> PAGE_SHIFT);
2588 }
2589 } else if (uvmpdpol_pageactivate_p(pg)) {
2590 /*
2591 * avoid re-activating the page unless needed,
2592 * to avoid false sharing on multiprocessor.
2593 */
2594
2595 uvm_pagelock(pg);
2596 uvm_pageactivate(pg);
2597 uvm_pageunlock(pg);
2598 }
2599 }
2600
2601
2602 /*
2603 * uvm_fault_wire: wire down a range of virtual addresses in a map.
2604 *
2605 * => map may be read-locked by caller, but MUST NOT be write-locked.
2606 * => if map is read-locked, any operations which may cause map to
2607 * be write-locked in uvm_fault() must be taken care of by
2608 * the caller. See uvm_map_pageable().
2609 */
2610
2611 int
2612 uvm_fault_wire(struct vm_map *map, vaddr_t start, vaddr_t end,
2613 vm_prot_t access_type, int maxprot)
2614 {
2615 vaddr_t va;
2616 int error;
2617
2618 /*
2619 * now fault it in a page at a time. if the fault fails then we have
2620 * to undo what we have done. note that in uvm_fault VM_PROT_NONE
2621 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
2622 */
2623
2624 /*
2625 * XXX work around overflowing a vaddr_t. this prevents us from
2626 * wiring the last page in the address space, though.
2627 */
2628 if (start > end) {
2629 return EFAULT;
2630 }
2631
2632 for (va = start; va < end; va += PAGE_SIZE) {
2633 error = uvm_fault_internal(map, va, access_type,
2634 (maxprot ? UVM_FAULT_MAXPROT : 0) | UVM_FAULT_WIRE);
2635 if (error) {
2636 if (va != start) {
2637 uvm_fault_unwire(map, start, va);
2638 }
2639 return error;
2640 }
2641 }
2642 return 0;
2643 }
2644
2645 /*
2646 * uvm_fault_unwire(): unwire range of virtual space.
2647 */
2648
2649 void
2650 uvm_fault_unwire(struct vm_map *map, vaddr_t start, vaddr_t end)
2651 {
2652 vm_map_lock_read(map);
2653 uvm_fault_unwire_locked(map, start, end);
2654 vm_map_unlock_read(map);
2655 }
2656
2657 /*
2658 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
2659 *
2660 * => map must be at least read-locked.
2661 */
2662
2663 void
2664 uvm_fault_unwire_locked(struct vm_map *map, vaddr_t start, vaddr_t end)
2665 {
2666 struct vm_map_entry *entry, *oentry;
2667 pmap_t pmap = vm_map_pmap(map);
2668 vaddr_t va;
2669 paddr_t pa;
2670 struct vm_page *pg;
2671
2672 /*
2673 * we assume that the area we are unwiring has actually been wired
2674 * in the first place. this means that we should be able to extract
2675 * the PAs from the pmap. we also lock out the page daemon so that
2676 * we can call uvm_pageunwire.
2677 */
2678
2679 /*
2680 * find the beginning map entry for the region.
2681 */
2682
2683 KASSERT(start >= vm_map_min(map));
2684 KASSERT(end <= vm_map_max(map));
2685 if (uvm_map_lookup_entry(map, start, &entry) == false)
2686 panic("uvm_fault_unwire_locked: address not in map");
2687
2688 oentry = NULL;
2689 for (va = start; va < end; va += PAGE_SIZE) {
2690
2691 /*
2692 * find the map entry for the current address.
2693 */
2694
2695 KASSERT(va >= entry->start);
2696 while (va >= entry->end) {
2697 KASSERT(entry->next != &map->header);
2698 KASSERT(entry->next->start <= entry->end);
2699 entry = entry->next;
2700 }
2701
2702 /*
2703 * lock it.
2704 */
2705
2706 if (entry != oentry) {
2707 if (oentry != NULL) {
2708 uvm_map_unlock_entry(oentry);
2709 }
2710 uvm_map_lock_entry(entry, RW_WRITER);
2711 oentry = entry;
2712 }
2713
2714 /*
2715 * if the entry is no longer wired, tell the pmap.
2716 */
2717
2718 if (!pmap_extract(pmap, va, &pa))
2719 continue;
2720
2721 if (VM_MAPENT_ISWIRED(entry) == 0)
2722 pmap_unwire(pmap, va);
2723
2724 pg = PHYS_TO_VM_PAGE(pa);
2725 if (pg) {
2726 uvm_pagelock(pg);
2727 uvm_pageunwire(pg);
2728 uvm_pageunlock(pg);
2729 }
2730 }
2731
2732 if (oentry != NULL) {
2733 uvm_map_unlock_entry(entry);
2734 }
2735 }
2736