Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/vm/vm_fault.c
104157 views
1
/*-
2
* SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU)
3
*
4
* Copyright (c) 1991, 1993
5
* The Regents of the University of California. All rights reserved.
6
* Copyright (c) 1994 John S. Dyson
7
* All rights reserved.
8
* Copyright (c) 1994 David Greenman
9
* All rights reserved.
10
*
11
*
12
* This code is derived from software contributed to Berkeley by
13
* The Mach Operating System project at Carnegie-Mellon University.
14
*
15
* Redistribution and use in source and binary forms, with or without
16
* modification, are permitted provided that the following conditions
17
* are met:
18
* 1. Redistributions of source code must retain the above copyright
19
* notice, this list of conditions and the following disclaimer.
20
* 2. Redistributions in binary form must reproduce the above copyright
21
* notice, this list of conditions and the following disclaimer in the
22
* documentation and/or other materials provided with the distribution.
23
* 3. All advertising materials mentioning features or use of this software
24
* must display the following acknowledgement:
25
* This product includes software developed by the University of
26
* California, Berkeley and its contributors.
27
* 4. Neither the name of the University nor the names of its contributors
28
* may be used to endorse or promote products derived from this software
29
* without specific prior written permission.
30
*
31
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41
* SUCH DAMAGE.
42
*
43
*
44
* Copyright (c) 1987, 1990 Carnegie-Mellon University.
45
* All rights reserved.
46
*
47
* Authors: Avadis Tevanian, Jr., Michael Wayne Young
48
*
49
* Permission to use, copy, modify and distribute this software and
50
* its documentation is hereby granted, provided that both the copyright
51
* notice and this permission notice appear in all copies of the
52
* software, derivative works or modified versions, and any portions
53
* thereof, and that both notices appear in supporting documentation.
54
*
55
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57
* FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58
*
59
* Carnegie Mellon requests users of this software to return to
60
*
61
* Software Distribution Coordinator or [email protected]
62
* School of Computer Science
63
* Carnegie Mellon University
64
* Pittsburgh PA 15213-3890
65
*
66
* any improvements or extensions that they make and grant Carnegie the
67
* rights to redistribute these changes.
68
*/
69
70
/*
71
* Page fault handling module.
72
*/
73
74
#include "opt_ktrace.h"
75
#include "opt_vm.h"
76
77
#include <sys/systm.h>
78
#include <sys/kernel.h>
79
#include <sys/lock.h>
80
#include <sys/mman.h>
81
#include <sys/mutex.h>
82
#include <sys/pctrie.h>
83
#include <sys/proc.h>
84
#include <sys/racct.h>
85
#include <sys/refcount.h>
86
#include <sys/resourcevar.h>
87
#include <sys/rwlock.h>
88
#include <sys/sched.h>
89
#include <sys/sf_buf.h>
90
#include <sys/signalvar.h>
91
#include <sys/sysctl.h>
92
#include <sys/sysent.h>
93
#include <sys/vmmeter.h>
94
#include <sys/vnode.h>
95
#ifdef KTRACE
96
#include <sys/ktrace.h>
97
#endif
98
99
#include <vm/vm.h>
100
#include <vm/vm_param.h>
101
#include <vm/pmap.h>
102
#include <vm/vm_map.h>
103
#include <vm/vm_object.h>
104
#include <vm/vm_page.h>
105
#include <vm/vm_pageout.h>
106
#include <vm/vm_kern.h>
107
#include <vm/vm_pager.h>
108
#include <vm/vm_radix.h>
109
#include <vm/vm_extern.h>
110
#include <vm/vm_reserv.h>
111
112
#define PFBAK 4
113
#define PFFOR 4
114
115
#define VM_FAULT_READ_DEFAULT (1 + VM_FAULT_READ_AHEAD_INIT)
116
117
#define VM_FAULT_DONTNEED_MIN 1048576
118
119
struct faultstate {
120
/* Fault parameters. */
121
vm_offset_t vaddr;
122
vm_page_t *m_hold;
123
vm_prot_t fault_type;
124
vm_prot_t prot;
125
int fault_flags;
126
boolean_t wired;
127
128
/* Control state. */
129
struct timeval oom_start_time;
130
bool oom_started;
131
int nera;
132
bool can_read_lock;
133
134
/* Page reference for cow. */
135
vm_page_t m_cow;
136
137
/* Current object. */
138
vm_object_t object;
139
vm_pindex_t pindex;
140
vm_page_t m;
141
bool m_needs_zeroing;
142
143
/* Top-level map object. */
144
vm_object_t first_object;
145
vm_pindex_t first_pindex;
146
vm_page_t first_m;
147
148
/* Map state. */
149
vm_map_t map;
150
vm_map_entry_t entry;
151
int map_generation;
152
bool lookup_still_valid;
153
154
/* Vnode if locked. */
155
struct vnode *vp;
156
};
157
158
/*
159
* Return codes for internal fault routines.
160
*/
161
enum fault_status {
162
FAULT_SUCCESS = 10000, /* Return success to user. */
163
FAULT_FAILURE, /* Return failure to user. */
164
FAULT_CONTINUE, /* Continue faulting. */
165
FAULT_RESTART, /* Restart fault. */
166
FAULT_OUT_OF_BOUNDS, /* Invalid address for pager. */
167
FAULT_HARD, /* Performed I/O. */
168
FAULT_SOFT, /* Found valid page. */
169
FAULT_PROTECTION_FAILURE, /* Invalid access. */
170
};
171
172
enum fault_next_status {
173
FAULT_NEXT_GOTOBJ = 1,
174
FAULT_NEXT_NOOBJ,
175
FAULT_NEXT_RESTART,
176
};
177
178
static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
179
int ahead);
180
static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
181
int backward, int forward, bool obj_locked);
182
183
static int vm_pfault_oom_attempts = 3;
184
SYSCTL_INT(_vm, OID_AUTO, pfault_oom_attempts, CTLFLAG_RWTUN,
185
&vm_pfault_oom_attempts, 0,
186
"Number of page allocation attempts in page fault handler before it "
187
"triggers OOM handling");
188
189
static int vm_pfault_oom_wait = 10;
190
SYSCTL_INT(_vm, OID_AUTO, pfault_oom_wait, CTLFLAG_RWTUN,
191
&vm_pfault_oom_wait, 0,
192
"Number of seconds to wait for free pages before retrying "
193
"the page fault handler");
194
195
static inline void
196
vm_fault_page_release(vm_page_t *mp)
197
{
198
vm_page_t m;
199
200
m = *mp;
201
if (m != NULL) {
202
/*
203
* We are likely to loop around again and attempt to busy
204
* this page. Deactivating it leaves it available for
205
* pageout while optimizing fault restarts.
206
*/
207
vm_page_deactivate(m);
208
if (vm_page_xbusied(m))
209
vm_page_xunbusy(m);
210
else
211
vm_page_sunbusy(m);
212
*mp = NULL;
213
}
214
}
215
216
static inline void
217
vm_fault_page_free(vm_page_t *mp)
218
{
219
vm_page_t m;
220
221
m = *mp;
222
if (m != NULL) {
223
VM_OBJECT_ASSERT_WLOCKED(m->object);
224
if (!vm_page_wired(m))
225
vm_page_free(m);
226
else
227
vm_page_xunbusy(m);
228
*mp = NULL;
229
}
230
}
231
232
/*
233
* Return true if a vm_pager_get_pages() call is needed in order to check
234
* whether the pager might have a particular page, false if it can be determined
235
* immediately that the pager can not have a copy. For swap objects, this can
236
* be checked quickly.
237
*/
238
static inline bool
239
vm_fault_object_needs_getpages(vm_object_t object)
240
{
241
VM_OBJECT_ASSERT_LOCKED(object);
242
243
return ((object->flags & OBJ_SWAP) == 0 ||
244
!pctrie_is_empty(&object->un_pager.swp.swp_blks));
245
}
246
247
static inline void
248
vm_fault_unlock_map(struct faultstate *fs)
249
{
250
251
if (fs->lookup_still_valid) {
252
vm_map_lookup_done(fs->map, fs->entry);
253
fs->lookup_still_valid = false;
254
}
255
}
256
257
static void
258
vm_fault_unlock_vp(struct faultstate *fs)
259
{
260
261
if (fs->vp != NULL) {
262
vput(fs->vp);
263
fs->vp = NULL;
264
}
265
}
266
267
static bool
268
vm_fault_might_be_cow(struct faultstate *fs)
269
{
270
return (fs->object != fs->first_object);
271
}
272
273
static void
274
vm_fault_deallocate(struct faultstate *fs)
275
{
276
277
fs->m_needs_zeroing = true;
278
vm_fault_page_release(&fs->m_cow);
279
vm_fault_page_release(&fs->m);
280
vm_object_pip_wakeup(fs->object);
281
if (vm_fault_might_be_cow(fs)) {
282
VM_OBJECT_WLOCK(fs->first_object);
283
vm_fault_page_free(&fs->first_m);
284
VM_OBJECT_WUNLOCK(fs->first_object);
285
vm_object_pip_wakeup(fs->first_object);
286
}
287
vm_object_deallocate(fs->first_object);
288
vm_fault_unlock_map(fs);
289
vm_fault_unlock_vp(fs);
290
}
291
292
static void
293
vm_fault_unlock_and_deallocate(struct faultstate *fs)
294
{
295
296
VM_OBJECT_UNLOCK(fs->object);
297
vm_fault_deallocate(fs);
298
}
299
300
static void
301
vm_fault_dirty(struct faultstate *fs, vm_page_t m)
302
{
303
bool need_dirty;
304
305
if (((fs->prot & VM_PROT_WRITE) == 0 &&
306
(fs->fault_flags & VM_FAULT_DIRTY) == 0) ||
307
(m->oflags & VPO_UNMANAGED) != 0)
308
return;
309
310
VM_PAGE_OBJECT_BUSY_ASSERT(m);
311
312
need_dirty = ((fs->fault_type & VM_PROT_WRITE) != 0 &&
313
(fs->fault_flags & VM_FAULT_WIRE) == 0) ||
314
(fs->fault_flags & VM_FAULT_DIRTY) != 0;
315
316
vm_object_set_writeable_dirty(m->object);
317
318
/*
319
* If the fault is a write, we know that this page is being
320
* written NOW so dirty it explicitly to save on
321
* pmap_is_modified() calls later.
322
*
323
* Also, since the page is now dirty, we can possibly tell
324
* the pager to release any swap backing the page.
325
*/
326
if (need_dirty && vm_page_set_dirty(m) == 0) {
327
/*
328
* If this is a NOSYNC mmap we do not want to set PGA_NOSYNC
329
* if the page is already dirty to prevent data written with
330
* the expectation of being synced from not being synced.
331
* Likewise if this entry does not request NOSYNC then make
332
* sure the page isn't marked NOSYNC. Applications sharing
333
* data should use the same flags to avoid ping ponging.
334
*/
335
if ((fs->entry->eflags & MAP_ENTRY_NOSYNC) != 0)
336
vm_page_aflag_set(m, PGA_NOSYNC);
337
else
338
vm_page_aflag_clear(m, PGA_NOSYNC);
339
}
340
341
}
342
343
static bool
344
vm_fault_is_read(const struct faultstate *fs)
345
{
346
return ((fs->prot & VM_PROT_WRITE) == 0 &&
347
(fs->fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) == 0);
348
}
349
350
/*
351
* Unlocks fs.first_object and fs.map on success.
352
*/
353
static enum fault_status
354
vm_fault_soft_fast(struct faultstate *fs)
355
{
356
vm_page_t m, m_map;
357
#if VM_NRESERVLEVEL > 0
358
vm_page_t m_super;
359
int flags;
360
#endif
361
int psind;
362
vm_offset_t vaddr;
363
364
MPASS(fs->vp == NULL);
365
366
/*
367
* If we fail, vast majority of the time it is because the page is not
368
* there to begin with. Opportunistically perform the lookup and
369
* subsequent checks without the object lock, revalidate later.
370
*
371
* Note: a busy page can be mapped for read|execute access.
372
*/
373
m = vm_page_lookup_unlocked(fs->first_object, fs->first_pindex);
374
if (m == NULL || !vm_page_all_valid(m) ||
375
((fs->prot & VM_PROT_WRITE) != 0 && vm_page_busied(m))) {
376
VM_OBJECT_WLOCK(fs->first_object);
377
return (FAULT_FAILURE);
378
}
379
380
vaddr = fs->vaddr;
381
382
VM_OBJECT_RLOCK(fs->first_object);
383
384
/*
385
* Now that we stabilized the state, revalidate the page is in the shape
386
* we encountered above.
387
*/
388
389
if (m->object != fs->first_object || m->pindex != fs->first_pindex)
390
goto fail;
391
392
vm_object_busy(fs->first_object);
393
394
if (!vm_page_all_valid(m) ||
395
((fs->prot & VM_PROT_WRITE) != 0 && vm_page_busied(m)))
396
goto fail_busy;
397
398
m_map = m;
399
psind = 0;
400
#if VM_NRESERVLEVEL > 0
401
if ((m->flags & PG_FICTITIOUS) == 0 &&
402
(m_super = vm_reserv_to_superpage(m)) != NULL) {
403
psind = m_super->psind;
404
KASSERT(psind > 0,
405
("psind %d of m_super %p < 1", psind, m_super));
406
flags = PS_ALL_VALID;
407
if ((fs->prot & VM_PROT_WRITE) != 0) {
408
/*
409
* Create a superpage mapping allowing write access
410
* only if none of the constituent pages are busy and
411
* all of them are already dirty (except possibly for
412
* the page that was faulted on).
413
*/
414
flags |= PS_NONE_BUSY;
415
if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
416
flags |= PS_ALL_DIRTY;
417
}
418
while (rounddown2(vaddr, pagesizes[psind]) < fs->entry->start ||
419
roundup2(vaddr + 1, pagesizes[psind]) > fs->entry->end ||
420
(vaddr & (pagesizes[psind] - 1)) !=
421
(VM_PAGE_TO_PHYS(m) & (pagesizes[psind] - 1)) ||
422
!vm_page_ps_test(m_super, psind, flags, m) ||
423
!pmap_ps_enabled(fs->map->pmap)) {
424
psind--;
425
if (psind == 0)
426
break;
427
m_super += rounddown2(m - m_super,
428
atop(pagesizes[psind]));
429
KASSERT(m_super->psind >= psind,
430
("psind %d of m_super %p < %d", m_super->psind,
431
m_super, psind));
432
}
433
if (psind > 0) {
434
m_map = m_super;
435
vaddr = rounddown2(vaddr, pagesizes[psind]);
436
/* Preset the modified bit for dirty superpages. */
437
if ((flags & PS_ALL_DIRTY) != 0)
438
fs->fault_type |= VM_PROT_WRITE;
439
}
440
}
441
#endif
442
if (pmap_enter(fs->map->pmap, vaddr, m_map, fs->prot, fs->fault_type |
443
PMAP_ENTER_NOSLEEP | (fs->wired ? PMAP_ENTER_WIRED : 0), psind) !=
444
KERN_SUCCESS)
445
goto fail_busy;
446
if (fs->m_hold != NULL) {
447
(*fs->m_hold) = m;
448
vm_page_wire(m);
449
}
450
if (psind == 0 && !fs->wired)
451
vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
452
VM_OBJECT_RUNLOCK(fs->first_object);
453
vm_fault_dirty(fs, m);
454
vm_object_unbusy(fs->first_object);
455
vm_map_lookup_done(fs->map, fs->entry);
456
curthread->td_ru.ru_minflt++;
457
return (FAULT_SUCCESS);
458
fail_busy:
459
vm_object_unbusy(fs->first_object);
460
fail:
461
if (!VM_OBJECT_TRYUPGRADE(fs->first_object)) {
462
VM_OBJECT_RUNLOCK(fs->first_object);
463
VM_OBJECT_WLOCK(fs->first_object);
464
}
465
return (FAULT_FAILURE);
466
}
467
468
static void
469
vm_fault_restore_map_lock(struct faultstate *fs)
470
{
471
472
VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
473
MPASS(blockcount_read(&fs->first_object->paging_in_progress) > 0);
474
475
if (!vm_map_trylock_read(fs->map)) {
476
VM_OBJECT_WUNLOCK(fs->first_object);
477
vm_map_lock_read(fs->map);
478
VM_OBJECT_WLOCK(fs->first_object);
479
}
480
fs->lookup_still_valid = true;
481
}
482
483
static void
484
vm_fault_populate_check_page(vm_page_t m)
485
{
486
487
/*
488
* Check each page to ensure that the pager is obeying the
489
* interface: the page must be installed in the object, fully
490
* valid, and exclusively busied.
491
*/
492
MPASS(m != NULL);
493
MPASS(vm_page_all_valid(m));
494
MPASS(vm_page_xbusied(m));
495
}
496
497
static void
498
vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
499
vm_pindex_t last)
500
{
501
struct pctrie_iter pages;
502
vm_page_t m;
503
504
VM_OBJECT_ASSERT_WLOCKED(object);
505
MPASS(first <= last);
506
vm_page_iter_limit_init(&pages, object, last + 1);
507
VM_RADIX_FORALL_FROM(m, &pages, first) {
508
vm_fault_populate_check_page(m);
509
vm_page_deactivate(m);
510
vm_page_xunbusy(m);
511
}
512
KASSERT(pages.index == last, ("%s: pindex mismatch", __func__));
513
}
514
515
static enum fault_status
516
vm_fault_populate(struct faultstate *fs)
517
{
518
vm_offset_t vaddr;
519
vm_page_t m;
520
vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
521
int bdry_idx, i, npages, psind, rv;
522
enum fault_status res;
523
524
MPASS(fs->object == fs->first_object);
525
VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
526
MPASS(blockcount_read(&fs->first_object->paging_in_progress) > 0);
527
MPASS(fs->first_object->backing_object == NULL);
528
MPASS(fs->lookup_still_valid);
529
530
pager_first = OFF_TO_IDX(fs->entry->offset);
531
pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
532
vm_fault_unlock_map(fs);
533
vm_fault_unlock_vp(fs);
534
535
res = FAULT_SUCCESS;
536
537
/*
538
* Call the pager (driver) populate() method.
539
*
540
* There is no guarantee that the method will be called again
541
* if the current fault is for read, and a future fault is
542
* for write. Report the entry's maximum allowed protection
543
* to the driver.
544
*/
545
rv = vm_pager_populate(fs->first_object, fs->first_pindex,
546
fs->fault_type, fs->entry->max_protection, &pager_first,
547
&pager_last);
548
549
VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
550
if (rv == VM_PAGER_BAD) {
551
/*
552
* VM_PAGER_BAD is the backdoor for a pager to request
553
* normal fault handling.
554
*/
555
vm_fault_restore_map_lock(fs);
556
if (fs->map->timestamp != fs->map_generation)
557
return (FAULT_RESTART);
558
return (FAULT_CONTINUE);
559
}
560
if (rv != VM_PAGER_OK)
561
return (FAULT_FAILURE); /* AKA SIGSEGV */
562
563
/* Ensure that the driver is obeying the interface. */
564
MPASS(pager_first <= pager_last);
565
MPASS(fs->first_pindex <= pager_last);
566
MPASS(fs->first_pindex >= pager_first);
567
MPASS(pager_last < fs->first_object->size);
568
569
vm_fault_restore_map_lock(fs);
570
bdry_idx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(fs->entry);
571
if (fs->map->timestamp != fs->map_generation) {
572
if (bdry_idx == 0) {
573
vm_fault_populate_cleanup(fs->first_object, pager_first,
574
pager_last);
575
} else {
576
m = vm_page_lookup(fs->first_object, pager_first);
577
if (m != fs->m)
578
vm_page_xunbusy(m);
579
}
580
return (FAULT_RESTART);
581
}
582
583
/*
584
* The map is unchanged after our last unlock. Process the fault.
585
*
586
* First, the special case of largepage mappings, where
587
* populate only busies the first page in superpage run.
588
*/
589
if (bdry_idx != 0) {
590
KASSERT(PMAP_HAS_LARGEPAGES,
591
("missing pmap support for large pages"));
592
m = vm_page_lookup(fs->first_object, pager_first);
593
vm_fault_populate_check_page(m);
594
VM_OBJECT_WUNLOCK(fs->first_object);
595
vaddr = fs->entry->start + IDX_TO_OFF(pager_first) -
596
fs->entry->offset;
597
/* assert alignment for entry */
598
KASSERT((vaddr & (pagesizes[bdry_idx] - 1)) == 0,
599
("unaligned superpage start %#jx pager_first %#jx offset %#jx vaddr %#jx",
600
(uintmax_t)fs->entry->start, (uintmax_t)pager_first,
601
(uintmax_t)fs->entry->offset, (uintmax_t)vaddr));
602
KASSERT((VM_PAGE_TO_PHYS(m) & (pagesizes[bdry_idx] - 1)) == 0,
603
("unaligned superpage m %p %#jx", m,
604
(uintmax_t)VM_PAGE_TO_PHYS(m)));
605
rv = pmap_enter(fs->map->pmap, vaddr, m, fs->prot,
606
fs->fault_type | (fs->wired ? PMAP_ENTER_WIRED : 0) |
607
PMAP_ENTER_LARGEPAGE, bdry_idx);
608
VM_OBJECT_WLOCK(fs->first_object);
609
vm_page_xunbusy(m);
610
if (rv != KERN_SUCCESS) {
611
res = FAULT_FAILURE;
612
goto out;
613
}
614
if ((fs->fault_flags & VM_FAULT_WIRE) != 0) {
615
for (i = 0; i < atop(pagesizes[bdry_idx]); i++)
616
vm_page_wire(m + i);
617
}
618
if (fs->m_hold != NULL) {
619
*fs->m_hold = m + (fs->first_pindex - pager_first);
620
vm_page_wire(*fs->m_hold);
621
}
622
goto out;
623
}
624
625
/*
626
* The range [pager_first, pager_last] that is given to the
627
* pager is only a hint. The pager may populate any range
628
* within the object that includes the requested page index.
629
* In case the pager expanded the range, clip it to fit into
630
* the map entry.
631
*/
632
map_first = OFF_TO_IDX(fs->entry->offset);
633
if (map_first > pager_first) {
634
vm_fault_populate_cleanup(fs->first_object, pager_first,
635
map_first - 1);
636
pager_first = map_first;
637
}
638
map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
639
if (map_last < pager_last) {
640
vm_fault_populate_cleanup(fs->first_object, map_last + 1,
641
pager_last);
642
pager_last = map_last;
643
}
644
for (pidx = pager_first; pidx <= pager_last; pidx += npages) {
645
m = vm_page_lookup(fs->first_object, pidx);
646
vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
647
KASSERT(m != NULL && m->pindex == pidx,
648
("%s: pindex mismatch", __func__));
649
psind = m->psind;
650
while (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
651
pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
652
!pmap_ps_enabled(fs->map->pmap)))
653
psind--;
654
655
npages = atop(pagesizes[psind]);
656
for (i = 0; i < npages; i++) {
657
vm_fault_populate_check_page(&m[i]);
658
vm_fault_dirty(fs, &m[i]);
659
}
660
VM_OBJECT_WUNLOCK(fs->first_object);
661
rv = pmap_enter(fs->map->pmap, vaddr, m, fs->prot, fs->fault_type |
662
(fs->wired ? PMAP_ENTER_WIRED : 0), psind);
663
664
/*
665
* pmap_enter() may fail for a superpage mapping if additional
666
* protection policies prevent the full mapping.
667
* For example, this will happen on amd64 if the entire
668
* address range does not share the same userspace protection
669
* key. Revert to single-page mappings if this happens.
670
*/
671
MPASS(rv == KERN_SUCCESS ||
672
(psind > 0 && rv == KERN_PROTECTION_FAILURE));
673
if (__predict_false(psind > 0 &&
674
rv == KERN_PROTECTION_FAILURE)) {
675
MPASS(!fs->wired);
676
for (i = 0; i < npages; i++) {
677
rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
678
&m[i], fs->prot, fs->fault_type, 0);
679
MPASS(rv == KERN_SUCCESS);
680
}
681
}
682
683
VM_OBJECT_WLOCK(fs->first_object);
684
for (i = 0; i < npages; i++) {
685
if ((fs->fault_flags & VM_FAULT_WIRE) != 0 &&
686
m[i].pindex == fs->first_pindex)
687
vm_page_wire(&m[i]);
688
else
689
vm_page_activate(&m[i]);
690
if (fs->m_hold != NULL &&
691
m[i].pindex == fs->first_pindex) {
692
(*fs->m_hold) = &m[i];
693
vm_page_wire(&m[i]);
694
}
695
vm_page_xunbusy(&m[i]);
696
}
697
}
698
out:
699
curthread->td_ru.ru_majflt++;
700
return (res);
701
}
702
703
static int prot_fault_translation;
704
SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RWTUN,
705
&prot_fault_translation, 0,
706
"Control signal to deliver on protection fault");
707
708
/* compat definition to keep common code for signal translation */
709
#define UCODE_PAGEFLT 12
710
#ifdef T_PAGEFLT
711
_Static_assert(UCODE_PAGEFLT == T_PAGEFLT, "T_PAGEFLT");
712
#endif
713
714
/*
715
* vm_fault_trap:
716
*
717
* Helper for the machine-dependent page fault trap handlers, wrapping
718
* vm_fault(). Issues ktrace(2) tracepoints for the faults.
719
*
720
* If the fault cannot be handled successfully by updating the
721
* required mapping, and the faulted instruction cannot be restarted,
722
* the signal number and si_code values are returned for trapsignal()
723
* to deliver.
724
*
725
* Returns Mach error codes, but callers should only check for
726
* KERN_SUCCESS.
727
*/
728
int
729
vm_fault_trap(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
730
int fault_flags, int *signo, int *ucode)
731
{
732
int result;
733
734
MPASS(signo == NULL || ucode != NULL);
735
#ifdef KTRACE
736
if (map != kernel_map && KTRPOINT(curthread, KTR_FAULT))
737
ktrfault(vaddr, fault_type);
738
#endif
739
result = vm_fault(map, trunc_page(vaddr), fault_type, fault_flags,
740
NULL);
741
KASSERT(result == KERN_SUCCESS || result == KERN_FAILURE ||
742
result == KERN_INVALID_ADDRESS ||
743
result == KERN_RESOURCE_SHORTAGE ||
744
result == KERN_PROTECTION_FAILURE ||
745
result == KERN_OUT_OF_BOUNDS,
746
("Unexpected Mach error %d from vm_fault()", result));
747
#ifdef KTRACE
748
if (map != kernel_map && KTRPOINT(curthread, KTR_FAULTEND))
749
ktrfaultend(result);
750
#endif
751
if (result != KERN_SUCCESS && signo != NULL) {
752
switch (result) {
753
case KERN_FAILURE:
754
case KERN_INVALID_ADDRESS:
755
*signo = SIGSEGV;
756
*ucode = SEGV_MAPERR;
757
break;
758
case KERN_RESOURCE_SHORTAGE:
759
*signo = SIGBUS;
760
*ucode = BUS_OOMERR;
761
break;
762
case KERN_OUT_OF_BOUNDS:
763
*signo = SIGBUS;
764
*ucode = BUS_OBJERR;
765
break;
766
case KERN_PROTECTION_FAILURE:
767
if (prot_fault_translation == 0) {
768
/*
769
* Autodetect. This check also covers
770
* the images without the ABI-tag ELF
771
* note.
772
*/
773
if (SV_CURPROC_ABI() == SV_ABI_FREEBSD &&
774
curproc->p_osrel >= P_OSREL_SIGSEGV) {
775
*signo = SIGSEGV;
776
*ucode = SEGV_ACCERR;
777
} else {
778
*signo = SIGBUS;
779
*ucode = UCODE_PAGEFLT;
780
}
781
} else if (prot_fault_translation == 1) {
782
/* Always compat mode. */
783
*signo = SIGBUS;
784
*ucode = UCODE_PAGEFLT;
785
} else {
786
/* Always SIGSEGV mode. */
787
*signo = SIGSEGV;
788
*ucode = SEGV_ACCERR;
789
}
790
break;
791
default:
792
KASSERT(0, ("Unexpected Mach error %d from vm_fault()",
793
result));
794
break;
795
}
796
}
797
return (result);
798
}
799
800
static bool
801
vm_fault_object_ensure_wlocked(struct faultstate *fs)
802
{
803
if (fs->object == fs->first_object)
804
VM_OBJECT_ASSERT_WLOCKED(fs->object);
805
806
if (!fs->can_read_lock) {
807
VM_OBJECT_ASSERT_WLOCKED(fs->object);
808
return (true);
809
}
810
811
if (VM_OBJECT_WOWNED(fs->object))
812
return (true);
813
814
if (VM_OBJECT_TRYUPGRADE(fs->object))
815
return (true);
816
817
return (false);
818
}
819
820
static enum fault_status
821
vm_fault_lock_vnode(struct faultstate *fs, bool objlocked)
822
{
823
struct vnode *vp;
824
int error, locked;
825
826
if (fs->object->type != OBJT_VNODE)
827
return (FAULT_CONTINUE);
828
vp = fs->object->handle;
829
if (vp == fs->vp) {
830
ASSERT_VOP_LOCKED(vp, "saved vnode is not locked");
831
return (FAULT_CONTINUE);
832
}
833
834
/*
835
* Perform an unlock in case the desired vnode changed while
836
* the map was unlocked during a retry.
837
*/
838
vm_fault_unlock_vp(fs);
839
840
locked = VOP_ISLOCKED(vp);
841
if (locked != LK_EXCLUSIVE)
842
locked = LK_SHARED;
843
844
/*
845
* We must not sleep acquiring the vnode lock while we have
846
* the page exclusive busied or the object's
847
* paging-in-progress count incremented. Otherwise, we could
848
* deadlock.
849
*/
850
error = vget(vp, locked | LK_CANRECURSE | LK_NOWAIT);
851
if (error == 0) {
852
fs->vp = vp;
853
return (FAULT_CONTINUE);
854
}
855
856
vhold(vp);
857
if (objlocked)
858
vm_fault_unlock_and_deallocate(fs);
859
else
860
vm_fault_deallocate(fs);
861
error = vget(vp, locked | LK_RETRY | LK_CANRECURSE);
862
vdrop(vp);
863
fs->vp = vp;
864
KASSERT(error == 0, ("vm_fault: vget failed %d", error));
865
return (FAULT_RESTART);
866
}
867
868
/*
869
* Calculate the desired readahead. Handle drop-behind.
870
*
871
* Returns the number of readahead blocks to pass to the pager.
872
*/
873
static int
874
vm_fault_readahead(struct faultstate *fs)
875
{
876
int era, nera;
877
u_char behavior;
878
879
KASSERT(fs->lookup_still_valid, ("map unlocked"));
880
era = fs->entry->read_ahead;
881
behavior = vm_map_entry_behavior(fs->entry);
882
if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
883
nera = 0;
884
} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
885
nera = VM_FAULT_READ_AHEAD_MAX;
886
if (fs->vaddr == fs->entry->next_read)
887
vm_fault_dontneed(fs, fs->vaddr, nera);
888
} else if (fs->vaddr == fs->entry->next_read) {
889
/*
890
* This is a sequential fault. Arithmetically
891
* increase the requested number of pages in
892
* the read-ahead window. The requested
893
* number of pages is "# of sequential faults
894
* x (read ahead min + 1) + read ahead min"
895
*/
896
nera = VM_FAULT_READ_AHEAD_MIN;
897
if (era > 0) {
898
nera += era + 1;
899
if (nera > VM_FAULT_READ_AHEAD_MAX)
900
nera = VM_FAULT_READ_AHEAD_MAX;
901
}
902
if (era == VM_FAULT_READ_AHEAD_MAX)
903
vm_fault_dontneed(fs, fs->vaddr, nera);
904
} else {
905
/*
906
* This is a non-sequential fault.
907
*/
908
nera = 0;
909
}
910
if (era != nera) {
911
/*
912
* A read lock on the map suffices to update
913
* the read ahead count safely.
914
*/
915
fs->entry->read_ahead = nera;
916
}
917
918
return (nera);
919
}
920
921
static int
922
vm_fault_lookup(struct faultstate *fs)
923
{
924
int result;
925
926
KASSERT(!fs->lookup_still_valid,
927
("vm_fault_lookup: Map already locked."));
928
result = vm_map_lookup(&fs->map, fs->vaddr, fs->fault_type |
929
VM_PROT_FAULT_LOOKUP, &fs->entry, &fs->first_object,
930
&fs->first_pindex, &fs->prot, &fs->wired);
931
if (result != KERN_SUCCESS) {
932
vm_fault_unlock_vp(fs);
933
return (result);
934
}
935
936
fs->map_generation = fs->map->timestamp;
937
938
if (fs->entry->eflags & MAP_ENTRY_NOFAULT) {
939
panic("%s: fault on nofault entry, addr: %#lx",
940
__func__, (u_long)fs->vaddr);
941
}
942
943
if (fs->entry->eflags & MAP_ENTRY_IN_TRANSITION &&
944
fs->entry->wiring_thread != curthread) {
945
vm_map_unlock_read(fs->map);
946
vm_map_lock(fs->map);
947
if (vm_map_lookup_entry(fs->map, fs->vaddr, &fs->entry) &&
948
(fs->entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
949
vm_fault_unlock_vp(fs);
950
fs->entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
951
vm_map_unlock_and_wait(fs->map, 0);
952
} else
953
vm_map_unlock(fs->map);
954
return (KERN_RESOURCE_SHORTAGE);
955
}
956
957
MPASS((fs->entry->eflags & MAP_ENTRY_GUARD) == 0);
958
959
if (fs->wired)
960
fs->fault_type = fs->prot | (fs->fault_type & VM_PROT_COPY);
961
else
962
KASSERT((fs->fault_flags & VM_FAULT_WIRE) == 0,
963
("!fs->wired && VM_FAULT_WIRE"));
964
fs->lookup_still_valid = true;
965
966
return (KERN_SUCCESS);
967
}
968
969
static int
970
vm_fault_relookup(struct faultstate *fs)
971
{
972
vm_object_t retry_object;
973
vm_pindex_t retry_pindex;
974
vm_prot_t retry_prot;
975
int result;
976
977
if (!vm_map_trylock_read(fs->map))
978
return (KERN_RESTART);
979
980
fs->lookup_still_valid = true;
981
if (fs->map->timestamp == fs->map_generation)
982
return (KERN_SUCCESS);
983
984
result = vm_map_lookup_locked(&fs->map, fs->vaddr, fs->fault_type,
985
&fs->entry, &retry_object, &retry_pindex, &retry_prot,
986
&fs->wired);
987
if (result != KERN_SUCCESS) {
988
/*
989
* If retry of map lookup would have blocked then
990
* retry fault from start.
991
*/
992
if (result == KERN_FAILURE)
993
return (KERN_RESTART);
994
return (result);
995
}
996
if (retry_object != fs->first_object ||
997
retry_pindex != fs->first_pindex)
998
return (KERN_RESTART);
999
1000
/*
1001
* Check whether the protection has changed or the object has
1002
* been copied while we left the map unlocked. Changing from
1003
* read to write permission is OK - we leave the page
1004
* write-protected, and catch the write fault. Changing from
1005
* write to read permission means that we can't mark the page
1006
* write-enabled after all.
1007
*/
1008
fs->prot &= retry_prot;
1009
fs->fault_type &= retry_prot;
1010
if (fs->prot == 0)
1011
return (KERN_RESTART);
1012
1013
/* Reassert because wired may have changed. */
1014
KASSERT(fs->wired || (fs->fault_flags & VM_FAULT_WIRE) == 0,
1015
("!wired && VM_FAULT_WIRE"));
1016
1017
return (KERN_SUCCESS);
1018
}
1019
1020
static bool
1021
vm_fault_can_cow_rename(struct faultstate *fs)
1022
{
1023
return (
1024
/* Only one shadow object and no other refs. */
1025
fs->object->shadow_count == 1 && fs->object->ref_count == 1 &&
1026
/* No other ways to look the object up. */
1027
fs->object->handle == NULL && (fs->object->flags & OBJ_ANON) != 0);
1028
}
1029
1030
static void
1031
vm_fault_cow(struct faultstate *fs)
1032
{
1033
bool is_first_object_locked, rename_cow;
1034
1035
KASSERT(vm_fault_might_be_cow(fs),
1036
("source and target COW objects are identical"));
1037
1038
/*
1039
* This allows pages to be virtually copied from a backing_object
1040
* into the first_object, where the backing object has no other
1041
* refs to it, and cannot gain any more refs. Instead of a bcopy,
1042
* we just move the page from the backing object to the first
1043
* object. Note that we must mark the page dirty in the first
1044
* object so that it will go out to swap when needed.
1045
*/
1046
is_first_object_locked = false;
1047
rename_cow = false;
1048
1049
if (vm_fault_can_cow_rename(fs) && vm_page_xbusied(fs->m)) {
1050
/*
1051
* Check that we don't chase down the shadow chain and
1052
* we can acquire locks. Recheck the conditions for
1053
* rename after the shadow chain is stable after the
1054
* object locking.
1055
*/
1056
is_first_object_locked = VM_OBJECT_TRYWLOCK(fs->first_object);
1057
if (is_first_object_locked &&
1058
fs->object == fs->first_object->backing_object) {
1059
if (VM_OBJECT_TRYWLOCK(fs->object)) {
1060
rename_cow = vm_fault_can_cow_rename(fs);
1061
if (!rename_cow)
1062
VM_OBJECT_WUNLOCK(fs->object);
1063
}
1064
}
1065
}
1066
1067
if (rename_cow) {
1068
vm_page_assert_xbusied(fs->m);
1069
1070
/*
1071
* Remove but keep xbusy for replace. fs->m is moved into
1072
* fs->first_object and left busy while fs->first_m is
1073
* conditionally freed.
1074
*/
1075
vm_page_remove_xbusy(fs->m);
1076
vm_page_replace(fs->m, fs->first_object, fs->first_pindex,
1077
fs->first_m);
1078
vm_page_dirty(fs->m);
1079
#if VM_NRESERVLEVEL > 0
1080
/*
1081
* Rename the reservation.
1082
*/
1083
vm_reserv_rename(fs->m, fs->first_object, fs->object,
1084
OFF_TO_IDX(fs->first_object->backing_object_offset));
1085
#endif
1086
VM_OBJECT_WUNLOCK(fs->object);
1087
VM_OBJECT_WUNLOCK(fs->first_object);
1088
fs->first_m = fs->m;
1089
fs->m = NULL;
1090
VM_CNT_INC(v_cow_optim);
1091
} else {
1092
if (is_first_object_locked)
1093
VM_OBJECT_WUNLOCK(fs->first_object);
1094
/*
1095
* Oh, well, lets copy it.
1096
*/
1097
pmap_copy_page(fs->m, fs->first_m);
1098
if (fs->wired && (fs->fault_flags & VM_FAULT_WIRE) == 0) {
1099
vm_page_wire(fs->first_m);
1100
vm_page_unwire(fs->m, PQ_INACTIVE);
1101
}
1102
/*
1103
* Save the COW page to be released after pmap_enter is
1104
* complete. The new copy will be marked valid when we're ready
1105
* to map it.
1106
*/
1107
fs->m_cow = fs->m;
1108
fs->m = NULL;
1109
1110
/*
1111
* Typically, the shadow object is either private to this
1112
* address space (OBJ_ONEMAPPING) or its pages are read only.
1113
* In the highly unusual case where the pages of a shadow object
1114
* are read/write shared between this and other address spaces,
1115
* we need to ensure that any pmap-level mappings to the
1116
* original, copy-on-write page from the backing object are
1117
* removed from those other address spaces.
1118
*
1119
* The flag check is racy, but this is tolerable: if
1120
* OBJ_ONEMAPPING is cleared after the check, the busy state
1121
* ensures that new mappings of m_cow can't be created.
1122
* pmap_enter() will replace an existing mapping in the current
1123
* address space. If OBJ_ONEMAPPING is set after the check,
1124
* removing mappings will at worse trigger some unnecessary page
1125
* faults.
1126
*
1127
* In the fs->m shared busy case, the xbusy state of
1128
* fs->first_m prevents new mappings of fs->m from
1129
* being created because a parallel fault on this
1130
* shadow chain should wait for xbusy on fs->first_m.
1131
*/
1132
if ((fs->first_object->flags & OBJ_ONEMAPPING) == 0)
1133
pmap_remove_all(fs->m_cow);
1134
}
1135
1136
vm_object_pip_wakeup(fs->object);
1137
1138
/*
1139
* Only use the new page below...
1140
*/
1141
fs->object = fs->first_object;
1142
fs->pindex = fs->first_pindex;
1143
fs->m = fs->first_m;
1144
VM_CNT_INC(v_cow_faults);
1145
curthread->td_cow++;
1146
}
1147
1148
static enum fault_next_status
1149
vm_fault_next(struct faultstate *fs)
1150
{
1151
vm_object_t next_object;
1152
1153
if (fs->object == fs->first_object || !fs->can_read_lock)
1154
VM_OBJECT_ASSERT_WLOCKED(fs->object);
1155
else
1156
VM_OBJECT_ASSERT_LOCKED(fs->object);
1157
1158
/*
1159
* The requested page does not exist at this object/
1160
* offset. Remove the invalid page from the object,
1161
* waking up anyone waiting for it, and continue on to
1162
* the next object. However, if this is the top-level
1163
* object, we must leave the busy page in place to
1164
* prevent another process from rushing past us, and
1165
* inserting the page in that object at the same time
1166
* that we are.
1167
*/
1168
if (fs->object == fs->first_object) {
1169
fs->first_m = fs->m;
1170
fs->m = NULL;
1171
} else if (fs->m != NULL) {
1172
if (!vm_fault_object_ensure_wlocked(fs)) {
1173
fs->can_read_lock = false;
1174
vm_fault_unlock_and_deallocate(fs);
1175
return (FAULT_NEXT_RESTART);
1176
}
1177
vm_fault_page_free(&fs->m);
1178
}
1179
1180
/*
1181
* Move on to the next object. Lock the next object before
1182
* unlocking the current one.
1183
*/
1184
next_object = fs->object->backing_object;
1185
if (next_object == NULL)
1186
return (FAULT_NEXT_NOOBJ);
1187
MPASS(fs->first_m != NULL);
1188
KASSERT(fs->object != next_object, ("object loop %p", next_object));
1189
if (fs->can_read_lock)
1190
VM_OBJECT_RLOCK(next_object);
1191
else
1192
VM_OBJECT_WLOCK(next_object);
1193
vm_object_pip_add(next_object, 1);
1194
if (fs->object != fs->first_object)
1195
vm_object_pip_wakeup(fs->object);
1196
fs->pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1197
VM_OBJECT_UNLOCK(fs->object);
1198
fs->object = next_object;
1199
1200
return (FAULT_NEXT_GOTOBJ);
1201
}
1202
1203
static void
1204
vm_fault_zerofill(struct faultstate *fs)
1205
{
1206
1207
/*
1208
* If there's no object left, fill the page in the top
1209
* object with zeros.
1210
*/
1211
if (vm_fault_might_be_cow(fs)) {
1212
vm_object_pip_wakeup(fs->object);
1213
fs->object = fs->first_object;
1214
fs->pindex = fs->first_pindex;
1215
}
1216
MPASS(fs->first_m != NULL);
1217
MPASS(fs->m == NULL);
1218
fs->m = fs->first_m;
1219
fs->first_m = NULL;
1220
1221
/*
1222
* Zero the page if necessary and mark it valid.
1223
*/
1224
if (fs->m_needs_zeroing) {
1225
pmap_zero_page(fs->m);
1226
} else {
1227
#ifdef INVARIANTS
1228
if (vm_check_pg_zero) {
1229
struct sf_buf *sf;
1230
unsigned long *p;
1231
int i;
1232
1233
sched_pin();
1234
sf = sf_buf_alloc(fs->m, SFB_CPUPRIVATE);
1235
p = (unsigned long *)sf_buf_kva(sf);
1236
for (i = 0; i < PAGE_SIZE / sizeof(*p); i++, p++) {
1237
KASSERT(*p == 0,
1238
("zerocheck failed page %p PG_ZERO %d %jx",
1239
fs->m, i, (uintmax_t)*p));
1240
}
1241
sf_buf_free(sf);
1242
sched_unpin();
1243
}
1244
#endif
1245
VM_CNT_INC(v_ozfod);
1246
}
1247
VM_CNT_INC(v_zfod);
1248
vm_page_valid(fs->m);
1249
}
1250
1251
/*
1252
* Initiate page fault after timeout. Returns true if caller should
1253
* do vm_waitpfault() after the call.
1254
*/
1255
static bool
1256
vm_fault_allocate_oom(struct faultstate *fs)
1257
{
1258
struct timeval now;
1259
1260
vm_fault_unlock_and_deallocate(fs);
1261
if (vm_pfault_oom_attempts < 0)
1262
return (true);
1263
if (!fs->oom_started) {
1264
fs->oom_started = true;
1265
getmicrotime(&fs->oom_start_time);
1266
return (true);
1267
}
1268
1269
getmicrotime(&now);
1270
timevalsub(&now, &fs->oom_start_time);
1271
if (now.tv_sec < vm_pfault_oom_attempts * vm_pfault_oom_wait)
1272
return (true);
1273
1274
if (bootverbose)
1275
printf(
1276
"proc %d (%s) failed to alloc page on fault, starting OOM\n",
1277
curproc->p_pid, curproc->p_comm);
1278
vm_pageout_oom(VM_OOM_MEM_PF);
1279
fs->oom_started = false;
1280
return (false);
1281
}
1282
1283
/*
1284
* Allocate a page directly or via the object populate method.
1285
*/
1286
static enum fault_status
1287
vm_fault_allocate(struct faultstate *fs, struct pctrie_iter *pages)
1288
{
1289
struct domainset *dset;
1290
enum fault_status res;
1291
1292
if ((fs->object->flags & OBJ_SIZEVNLOCK) != 0) {
1293
res = vm_fault_lock_vnode(fs, true);
1294
MPASS(res == FAULT_CONTINUE || res == FAULT_RESTART);
1295
if (res == FAULT_RESTART)
1296
return (res);
1297
}
1298
1299
if (fs->pindex >= fs->object->size) {
1300
vm_fault_unlock_and_deallocate(fs);
1301
return (FAULT_OUT_OF_BOUNDS);
1302
}
1303
1304
if (fs->object == fs->first_object &&
1305
(fs->first_object->flags & OBJ_POPULATE) != 0 &&
1306
fs->first_object->shadow_count == 0) {
1307
res = vm_fault_populate(fs);
1308
switch (res) {
1309
case FAULT_SUCCESS:
1310
case FAULT_FAILURE:
1311
case FAULT_RESTART:
1312
vm_fault_unlock_and_deallocate(fs);
1313
return (res);
1314
case FAULT_CONTINUE:
1315
pctrie_iter_reset(pages);
1316
/*
1317
* Pager's populate() method
1318
* returned VM_PAGER_BAD.
1319
*/
1320
break;
1321
default:
1322
panic("inconsistent return codes");
1323
}
1324
}
1325
1326
/*
1327
* Allocate a new page for this object/offset pair.
1328
*
1329
* If the process has a fatal signal pending, prioritize the allocation
1330
* with the expectation that the process will exit shortly and free some
1331
* pages. In particular, the signal may have been posted by the page
1332
* daemon in an attempt to resolve an out-of-memory condition.
1333
*
1334
* The unlocked read of the p_flag is harmless. At worst, the P_KILLED
1335
* might be not observed here, and allocation fails, causing a restart
1336
* and new reading of the p_flag.
1337
*/
1338
dset = fs->object->domain.dr_policy;
1339
if (dset == NULL)
1340
dset = curthread->td_domain.dr_policy;
1341
if (!vm_page_count_severe_set(&dset->ds_mask) || P_KILLED(curproc)) {
1342
#if VM_NRESERVLEVEL > 0
1343
vm_object_color(fs->object, atop(fs->vaddr) - fs->pindex);
1344
#endif
1345
if (!vm_pager_can_alloc_page(fs->object, fs->pindex)) {
1346
vm_fault_unlock_and_deallocate(fs);
1347
return (FAULT_FAILURE);
1348
}
1349
fs->m = vm_page_alloc_iter(fs->object, fs->pindex,
1350
P_KILLED(curproc) ? VM_ALLOC_SYSTEM : 0, pages);
1351
}
1352
if (fs->m == NULL) {
1353
if (vm_fault_allocate_oom(fs))
1354
vm_waitpfault(dset, vm_pfault_oom_wait * hz);
1355
return (FAULT_RESTART);
1356
}
1357
fs->m_needs_zeroing = (fs->m->flags & PG_ZERO) == 0;
1358
fs->oom_started = false;
1359
1360
return (FAULT_CONTINUE);
1361
}
1362
1363
/*
1364
* Call the pager to retrieve the page if there is a chance
1365
* that the pager has it, and potentially retrieve additional
1366
* pages at the same time.
1367
*/
1368
static enum fault_status
1369
vm_fault_getpages(struct faultstate *fs, int *behindp, int *aheadp)
1370
{
1371
vm_offset_t e_end, e_start;
1372
int ahead, behind, cluster_offset, rv;
1373
enum fault_status status;
1374
u_char behavior;
1375
1376
/*
1377
* Prepare for unlocking the map. Save the map
1378
* entry's start and end addresses, which are used to
1379
* optimize the size of the pager operation below.
1380
* Even if the map entry's addresses change after
1381
* unlocking the map, using the saved addresses is
1382
* safe.
1383
*/
1384
e_start = fs->entry->start;
1385
e_end = fs->entry->end;
1386
behavior = vm_map_entry_behavior(fs->entry);
1387
1388
/*
1389
* If the pager for the current object might have
1390
* the page, then determine the number of additional
1391
* pages to read and potentially reprioritize
1392
* previously read pages for earlier reclamation.
1393
* These operations should only be performed once per
1394
* page fault. Even if the current pager doesn't
1395
* have the page, the number of additional pages to
1396
* read will apply to subsequent objects in the
1397
* shadow chain.
1398
*/
1399
if (fs->nera == -1 && !P_KILLED(curproc))
1400
fs->nera = vm_fault_readahead(fs);
1401
1402
/*
1403
* Release the map lock before locking the vnode or
1404
* sleeping in the pager. (If the current object has
1405
* a shadow, then an earlier iteration of this loop
1406
* may have already unlocked the map.)
1407
*/
1408
vm_fault_unlock_map(fs);
1409
1410
status = vm_fault_lock_vnode(fs, false);
1411
MPASS(status == FAULT_CONTINUE || status == FAULT_RESTART);
1412
if (status == FAULT_RESTART)
1413
return (status);
1414
KASSERT(fs->vp == NULL || !vm_map_is_system(fs->map),
1415
("vm_fault: vnode-backed object mapped by system map"));
1416
1417
/*
1418
* Page in the requested page and hint the pager,
1419
* that it may bring up surrounding pages.
1420
*/
1421
if (fs->nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
1422
P_KILLED(curproc)) {
1423
behind = 0;
1424
ahead = 0;
1425
} else {
1426
/* Is this a sequential fault? */
1427
if (fs->nera > 0) {
1428
behind = 0;
1429
ahead = fs->nera;
1430
} else {
1431
/*
1432
* Request a cluster of pages that is
1433
* aligned to a VM_FAULT_READ_DEFAULT
1434
* page offset boundary within the
1435
* object. Alignment to a page offset
1436
* boundary is more likely to coincide
1437
* with the underlying file system
1438
* block than alignment to a virtual
1439
* address boundary.
1440
*/
1441
cluster_offset = fs->pindex % VM_FAULT_READ_DEFAULT;
1442
behind = ulmin(cluster_offset,
1443
atop(fs->vaddr - e_start));
1444
ahead = VM_FAULT_READ_DEFAULT - 1 - cluster_offset;
1445
}
1446
ahead = ulmin(ahead, atop(e_end - fs->vaddr) - 1);
1447
}
1448
*behindp = behind;
1449
*aheadp = ahead;
1450
rv = vm_pager_get_pages(fs->object, &fs->m, 1, behindp, aheadp);
1451
if (rv == VM_PAGER_OK)
1452
return (FAULT_HARD);
1453
if (rv == VM_PAGER_ERROR)
1454
printf("vm_fault: pager read error, pid %d (%s)\n",
1455
curproc->p_pid, curproc->p_comm);
1456
/*
1457
* If an I/O error occurred or the requested page was
1458
* outside the range of the pager, clean up and return
1459
* an error.
1460
*/
1461
if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
1462
VM_OBJECT_WLOCK(fs->object);
1463
vm_fault_page_free(&fs->m);
1464
vm_fault_unlock_and_deallocate(fs);
1465
return (FAULT_OUT_OF_BOUNDS);
1466
}
1467
KASSERT(rv == VM_PAGER_FAIL,
1468
("%s: unexpected pager error %d", __func__, rv));
1469
return (FAULT_CONTINUE);
1470
}
1471
1472
/*
1473
* Wait/Retry if the page is busy. We have to do this if the page is
1474
* either exclusive or shared busy because the vm_pager may be using
1475
* read busy for pageouts (and even pageins if it is the vnode pager),
1476
* and we could end up trying to pagein and pageout the same page
1477
* simultaneously.
1478
*
1479
* We allow the busy case on a read fault if the page is valid. We
1480
* cannot under any circumstances mess around with a shared busied
1481
* page except, perhaps, to pmap it. This is controlled by the
1482
* VM_ALLOC_SBUSY bit in the allocflags argument.
1483
*/
1484
static void
1485
vm_fault_busy_sleep(struct faultstate *fs, int allocflags)
1486
{
1487
/*
1488
* Reference the page before unlocking and
1489
* sleeping so that the page daemon is less
1490
* likely to reclaim it.
1491
*/
1492
vm_page_aflag_set(fs->m, PGA_REFERENCED);
1493
if (vm_fault_might_be_cow(fs)) {
1494
vm_fault_page_release(&fs->first_m);
1495
vm_object_pip_wakeup(fs->first_object);
1496
}
1497
vm_object_pip_wakeup(fs->object);
1498
vm_fault_unlock_map(fs);
1499
if (!vm_page_busy_sleep(fs->m, "vmpfw", allocflags))
1500
VM_OBJECT_UNLOCK(fs->object);
1501
VM_CNT_INC(v_intrans);
1502
vm_object_deallocate(fs->first_object);
1503
}
1504
1505
/*
1506
* Handle page lookup, populate, allocate, page-in for the current
1507
* object.
1508
*
1509
* The object is locked on entry and will remain locked with a return
1510
* code of FAULT_CONTINUE so that fault may follow the shadow chain.
1511
* Otherwise, the object will be unlocked upon return.
1512
*/
1513
static enum fault_status
1514
vm_fault_object(struct faultstate *fs, int *behindp, int *aheadp)
1515
{
1516
struct pctrie_iter pages;
1517
enum fault_status res;
1518
bool dead;
1519
1520
if (fs->object == fs->first_object || !fs->can_read_lock)
1521
VM_OBJECT_ASSERT_WLOCKED(fs->object);
1522
else
1523
VM_OBJECT_ASSERT_LOCKED(fs->object);
1524
1525
/*
1526
* If the object is marked for imminent termination, we retry
1527
* here, since the collapse pass has raced with us. Otherwise,
1528
* if we see terminally dead object, return fail.
1529
*/
1530
if ((fs->object->flags & OBJ_DEAD) != 0) {
1531
dead = fs->object->type == OBJT_DEAD;
1532
vm_fault_unlock_and_deallocate(fs);
1533
if (dead)
1534
return (FAULT_PROTECTION_FAILURE);
1535
pause("vmf_de", 1);
1536
return (FAULT_RESTART);
1537
}
1538
1539
/*
1540
* See if the page is resident.
1541
*/
1542
vm_page_iter_init(&pages, fs->object);
1543
fs->m = vm_radix_iter_lookup(&pages, fs->pindex);
1544
if (fs->m != NULL) {
1545
/*
1546
* If the found page is valid, will be either shadowed
1547
* or mapped read-only, and will not be renamed for
1548
* COW, then busy it in shared mode. This allows
1549
* other faults needing this page to proceed in
1550
* parallel.
1551
*
1552
* Unlocked check for validity, rechecked after busy
1553
* is obtained.
1554
*/
1555
if (vm_page_all_valid(fs->m) &&
1556
/*
1557
* No write permissions for the new fs->m mapping,
1558
* or the first object has only one mapping, so
1559
* other writeable COW mappings of fs->m cannot
1560
* appear under us.
1561
*/
1562
(vm_fault_is_read(fs) || vm_fault_might_be_cow(fs)) &&
1563
/*
1564
* fs->m cannot be renamed from object to
1565
* first_object. These conditions will be
1566
* re-checked with proper synchronization in
1567
* vm_fault_cow().
1568
*/
1569
(!vm_fault_can_cow_rename(fs) ||
1570
fs->object != fs->first_object->backing_object)) {
1571
if (!vm_page_trysbusy(fs->m)) {
1572
vm_fault_busy_sleep(fs, VM_ALLOC_SBUSY);
1573
return (FAULT_RESTART);
1574
}
1575
1576
/*
1577
* Now make sure that racily checked
1578
* conditions are still valid.
1579
*/
1580
if (__predict_true(vm_page_all_valid(fs->m) &&
1581
(vm_fault_is_read(fs) ||
1582
vm_fault_might_be_cow(fs)))) {
1583
VM_OBJECT_UNLOCK(fs->object);
1584
return (FAULT_SOFT);
1585
}
1586
1587
vm_page_sunbusy(fs->m);
1588
}
1589
1590
if (!vm_page_tryxbusy(fs->m)) {
1591
vm_fault_busy_sleep(fs, 0);
1592
return (FAULT_RESTART);
1593
}
1594
1595
/*
1596
* The page is marked busy for other processes and the
1597
* pagedaemon. If it is still completely valid we are
1598
* done.
1599
*/
1600
if (vm_page_all_valid(fs->m)) {
1601
VM_OBJECT_UNLOCK(fs->object);
1602
return (FAULT_SOFT);
1603
}
1604
}
1605
1606
/*
1607
* Page is not resident. If the pager might contain the page
1608
* or this is the beginning of the search, allocate a new
1609
* page.
1610
*/
1611
if (fs->m == NULL && (vm_fault_object_needs_getpages(fs->object) ||
1612
fs->object == fs->first_object)) {
1613
if (!vm_fault_object_ensure_wlocked(fs)) {
1614
fs->can_read_lock = false;
1615
vm_fault_unlock_and_deallocate(fs);
1616
return (FAULT_RESTART);
1617
}
1618
res = vm_fault_allocate(fs, &pages);
1619
if (res != FAULT_CONTINUE)
1620
return (res);
1621
}
1622
1623
/*
1624
* Check to see if the pager can possibly satisfy this fault.
1625
* If not, skip to the next object without dropping the lock to
1626
* preserve atomicity of shadow faults.
1627
*/
1628
if (vm_fault_object_needs_getpages(fs->object)) {
1629
/*
1630
* At this point, we have either allocated a new page
1631
* or found an existing page that is only partially
1632
* valid.
1633
*
1634
* We hold a reference on the current object and the
1635
* page is exclusive busied. The exclusive busy
1636
* prevents simultaneous faults and collapses while
1637
* the object lock is dropped.
1638
*/
1639
VM_OBJECT_UNLOCK(fs->object);
1640
res = vm_fault_getpages(fs, behindp, aheadp);
1641
if (res == FAULT_CONTINUE)
1642
VM_OBJECT_WLOCK(fs->object);
1643
} else {
1644
res = FAULT_CONTINUE;
1645
}
1646
return (res);
1647
}
1648
1649
/*
1650
* vm_fault:
1651
*
1652
* Handle a page fault occurring at the given address, requiring the
1653
* given permissions, in the map specified. If successful, the page
1654
* is inserted into the associated physical map, and optionally
1655
* referenced and returned in *m_hold.
1656
*
1657
* The given address should be truncated to the proper page address.
1658
*
1659
* KERN_SUCCESS is returned if the page fault is handled; otherwise, a
1660
* Mach error code explaining why the fault is fatal is returned.
1661
*
1662
* The map in question must be alive, either being the map for the current
1663
* process, or the owner process hold count has been incremented to prevent
1664
* exit().
1665
*
1666
* If the thread private TDP_NOFAULTING flag is set, any fault results
1667
* in immediate protection failure. Otherwise the fault is processed,
1668
* and caller may hold no locks.
1669
*/
1670
int
1671
vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
1672
int fault_flags, vm_page_t *m_hold)
1673
{
1674
struct pctrie_iter pages;
1675
struct faultstate fs;
1676
int ahead, behind, faultcount, rv;
1677
enum fault_status res;
1678
enum fault_next_status res_next;
1679
bool hardfault;
1680
1681
VM_CNT_INC(v_vm_faults);
1682
1683
if ((curthread->td_pflags & TDP_NOFAULTING) != 0)
1684
return (KERN_PROTECTION_FAILURE);
1685
1686
fs.vp = NULL;
1687
fs.vaddr = vaddr;
1688
fs.m_hold = m_hold;
1689
fs.fault_flags = fault_flags;
1690
fs.map = map;
1691
fs.lookup_still_valid = false;
1692
fs.m_needs_zeroing = true;
1693
fs.oom_started = false;
1694
fs.nera = -1;
1695
fs.can_read_lock = true;
1696
faultcount = 0;
1697
hardfault = false;
1698
1699
RetryFault:
1700
fs.fault_type = fault_type;
1701
1702
/*
1703
* Find the backing store object and offset into it to begin the
1704
* search.
1705
*/
1706
rv = vm_fault_lookup(&fs);
1707
if (rv != KERN_SUCCESS) {
1708
if (rv == KERN_RESOURCE_SHORTAGE)
1709
goto RetryFault;
1710
return (rv);
1711
}
1712
1713
/*
1714
* Try to avoid lock contention on the top-level object through
1715
* special-case handling of some types of page faults, specifically,
1716
* those that are mapping an existing page from the top-level object.
1717
* Under this condition, a read lock on the object suffices, allowing
1718
* multiple page faults of a similar type to run in parallel.
1719
*/
1720
if (fs.vp == NULL /* avoid locked vnode leak */ &&
1721
(fs.entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) == 0 &&
1722
(fs.fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0) {
1723
res = vm_fault_soft_fast(&fs);
1724
if (res == FAULT_SUCCESS) {
1725
VM_OBJECT_ASSERT_UNLOCKED(fs.first_object);
1726
return (KERN_SUCCESS);
1727
}
1728
VM_OBJECT_ASSERT_WLOCKED(fs.first_object);
1729
} else {
1730
vm_page_iter_init(&pages, fs.first_object);
1731
VM_OBJECT_WLOCK(fs.first_object);
1732
}
1733
1734
/*
1735
* Make a reference to this object to prevent its disposal while we
1736
* are messing with it. Once we have the reference, the map is free
1737
* to be diddled. Since objects reference their shadows (and copies),
1738
* they will stay around as well.
1739
*
1740
* Bump the paging-in-progress count to prevent size changes (e.g.
1741
* truncation operations) during I/O.
1742
*/
1743
vm_object_reference_locked(fs.first_object);
1744
vm_object_pip_add(fs.first_object, 1);
1745
1746
fs.m_cow = fs.m = fs.first_m = NULL;
1747
1748
/*
1749
* Search for the page at object/offset.
1750
*/
1751
fs.object = fs.first_object;
1752
fs.pindex = fs.first_pindex;
1753
1754
if ((fs.entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK) != 0) {
1755
res = vm_fault_allocate(&fs, &pages);
1756
switch (res) {
1757
case FAULT_RESTART:
1758
goto RetryFault;
1759
case FAULT_SUCCESS:
1760
return (KERN_SUCCESS);
1761
case FAULT_FAILURE:
1762
return (KERN_FAILURE);
1763
case FAULT_OUT_OF_BOUNDS:
1764
return (KERN_OUT_OF_BOUNDS);
1765
case FAULT_CONTINUE:
1766
break;
1767
default:
1768
panic("vm_fault: Unhandled status %d", res);
1769
}
1770
}
1771
1772
while (TRUE) {
1773
KASSERT(fs.m == NULL,
1774
("page still set %p at loop start", fs.m));
1775
1776
res = vm_fault_object(&fs, &behind, &ahead);
1777
switch (res) {
1778
case FAULT_SOFT:
1779
goto found;
1780
case FAULT_HARD:
1781
faultcount = behind + 1 + ahead;
1782
hardfault = true;
1783
goto found;
1784
case FAULT_RESTART:
1785
goto RetryFault;
1786
case FAULT_SUCCESS:
1787
return (KERN_SUCCESS);
1788
case FAULT_FAILURE:
1789
return (KERN_FAILURE);
1790
case FAULT_OUT_OF_BOUNDS:
1791
return (KERN_OUT_OF_BOUNDS);
1792
case FAULT_PROTECTION_FAILURE:
1793
return (KERN_PROTECTION_FAILURE);
1794
case FAULT_CONTINUE:
1795
break;
1796
default:
1797
panic("vm_fault: Unhandled status %d", res);
1798
}
1799
1800
/*
1801
* The page was not found in the current object. Try to
1802
* traverse into a backing object or zero fill if none is
1803
* found.
1804
*/
1805
res_next = vm_fault_next(&fs);
1806
if (res_next == FAULT_NEXT_RESTART)
1807
goto RetryFault;
1808
else if (res_next == FAULT_NEXT_GOTOBJ)
1809
continue;
1810
MPASS(res_next == FAULT_NEXT_NOOBJ);
1811
if ((fs.fault_flags & VM_FAULT_NOFILL) != 0) {
1812
if (fs.first_object == fs.object)
1813
vm_fault_page_free(&fs.first_m);
1814
vm_fault_unlock_and_deallocate(&fs);
1815
return (KERN_OUT_OF_BOUNDS);
1816
}
1817
VM_OBJECT_UNLOCK(fs.object);
1818
vm_fault_zerofill(&fs);
1819
/* Don't try to prefault neighboring pages. */
1820
faultcount = 1;
1821
break;
1822
}
1823
1824
found:
1825
/*
1826
* A valid page has been found and busied. The object lock
1827
* must no longer be held if the page was busied.
1828
*
1829
* Regardless of the busy state of fs.m, fs.first_m is always
1830
* exclusively busied after the first iteration of the loop
1831
* calling vm_fault_object(). This is an ordering point for
1832
* the parallel faults occuring in on the same page.
1833
*/
1834
vm_page_assert_busied(fs.m);
1835
VM_OBJECT_ASSERT_UNLOCKED(fs.object);
1836
1837
/*
1838
* If the page is being written, but isn't already owned by the
1839
* top-level object, we have to copy it into a new page owned by the
1840
* top-level object.
1841
*/
1842
if (vm_fault_might_be_cow(&fs)) {
1843
/*
1844
* We only really need to copy if we want to write it.
1845
*/
1846
if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1847
vm_fault_cow(&fs);
1848
/*
1849
* We only try to prefault read-only mappings to the
1850
* neighboring pages when this copy-on-write fault is
1851
* a hard fault. In other cases, trying to prefault
1852
* is typically wasted effort.
1853
*/
1854
if (faultcount == 0)
1855
faultcount = 1;
1856
1857
} else {
1858
fs.prot &= ~VM_PROT_WRITE;
1859
}
1860
}
1861
1862
/*
1863
* We must verify that the maps have not changed since our last
1864
* lookup.
1865
*/
1866
if (!fs.lookup_still_valid) {
1867
rv = vm_fault_relookup(&fs);
1868
if (rv != KERN_SUCCESS) {
1869
vm_fault_deallocate(&fs);
1870
if (rv == KERN_RESTART)
1871
goto RetryFault;
1872
return (rv);
1873
}
1874
}
1875
VM_OBJECT_ASSERT_UNLOCKED(fs.object);
1876
1877
/*
1878
* If the page was filled by a pager, save the virtual address that
1879
* should be faulted on next under a sequential access pattern to the
1880
* map entry. A read lock on the map suffices to update this address
1881
* safely.
1882
*/
1883
if (hardfault)
1884
fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1885
1886
/*
1887
* If the page to be mapped was copied from a backing object, we defer
1888
* marking it valid until here, where the fault handler is guaranteed to
1889
* succeed. Otherwise we can end up with a shadowed, mapped page in the
1890
* backing object, which violates an invariant of vm_object_collapse()
1891
* that shadowed pages are not mapped.
1892
*/
1893
if (fs.m_cow != NULL) {
1894
KASSERT(vm_page_none_valid(fs.m),
1895
("vm_fault: page %p is already valid", fs.m_cow));
1896
vm_page_valid(fs.m);
1897
}
1898
1899
/*
1900
* Page must be completely valid or it is not fit to
1901
* map into user space. vm_pager_get_pages() ensures this.
1902
*/
1903
vm_page_assert_busied(fs.m);
1904
KASSERT(vm_page_all_valid(fs.m),
1905
("vm_fault: page %p partially invalid", fs.m));
1906
1907
vm_fault_dirty(&fs, fs.m);
1908
1909
/*
1910
* Put this page into the physical map. We had to do the unlock above
1911
* because pmap_enter() may sleep. We don't put the page
1912
* back on the active queue until later so that the pageout daemon
1913
* won't find it (yet).
1914
*/
1915
pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot,
1916
fs.fault_type | (fs.wired ? PMAP_ENTER_WIRED : 0), 0);
1917
if (faultcount != 1 && (fs.fault_flags & VM_FAULT_WIRE) == 0 &&
1918
fs.wired == 0)
1919
vm_fault_prefault(&fs, vaddr,
1920
faultcount > 0 ? behind : PFBAK,
1921
faultcount > 0 ? ahead : PFFOR, false);
1922
1923
/*
1924
* If the page is not wired down, then put it where the pageout daemon
1925
* can find it.
1926
*/
1927
if ((fs.fault_flags & VM_FAULT_WIRE) != 0)
1928
vm_page_wire(fs.m);
1929
else
1930
vm_page_activate(fs.m);
1931
if (fs.m_hold != NULL) {
1932
(*fs.m_hold) = fs.m;
1933
vm_page_wire(fs.m);
1934
}
1935
1936
KASSERT(fs.first_object == fs.object || vm_page_xbusied(fs.first_m),
1937
("first_m must be xbusy"));
1938
if (vm_page_xbusied(fs.m))
1939
vm_page_xunbusy(fs.m);
1940
else
1941
vm_page_sunbusy(fs.m);
1942
fs.m = NULL;
1943
1944
/*
1945
* Unlock everything, and return
1946
*/
1947
vm_fault_deallocate(&fs);
1948
if (hardfault) {
1949
VM_CNT_INC(v_io_faults);
1950
curthread->td_ru.ru_majflt++;
1951
#ifdef RACCT
1952
if (racct_enable && fs.object->type == OBJT_VNODE) {
1953
PROC_LOCK(curproc);
1954
if ((fs.fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1955
racct_add_force(curproc, RACCT_WRITEBPS,
1956
PAGE_SIZE + behind * PAGE_SIZE);
1957
racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1958
} else {
1959
racct_add_force(curproc, RACCT_READBPS,
1960
PAGE_SIZE + ahead * PAGE_SIZE);
1961
racct_add_force(curproc, RACCT_READIOPS, 1);
1962
}
1963
PROC_UNLOCK(curproc);
1964
}
1965
#endif
1966
} else
1967
curthread->td_ru.ru_minflt++;
1968
1969
return (KERN_SUCCESS);
1970
}
1971
1972
/*
1973
* Speed up the reclamation of pages that precede the faulting pindex within
1974
* the first object of the shadow chain. Essentially, perform the equivalent
1975
* to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1976
* the faulting pindex by the cluster size when the pages read by vm_fault()
1977
* cross a cluster-size boundary. The cluster size is the greater of the
1978
* smallest superpage size and VM_FAULT_DONTNEED_MIN.
1979
*
1980
* When "fs->first_object" is a shadow object, the pages in the backing object
1981
* that precede the faulting pindex are deactivated by vm_fault(). So, this
1982
* function must only be concerned with pages in the first object.
1983
*/
1984
static void
1985
vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1986
{
1987
struct pctrie_iter pages;
1988
vm_map_entry_t entry;
1989
vm_object_t first_object;
1990
vm_offset_t end, start;
1991
vm_page_t m;
1992
vm_size_t size;
1993
1994
VM_OBJECT_ASSERT_UNLOCKED(fs->object);
1995
first_object = fs->first_object;
1996
/* Neither fictitious nor unmanaged pages can be reclaimed. */
1997
if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1998
VM_OBJECT_RLOCK(first_object);
1999
size = VM_FAULT_DONTNEED_MIN;
2000
if (MAXPAGESIZES > 1 && size < pagesizes[1])
2001
size = pagesizes[1];
2002
end = rounddown2(vaddr, size);
2003
if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
2004
(entry = fs->entry)->start < end) {
2005
if (end - entry->start < size)
2006
start = entry->start;
2007
else
2008
start = end - size;
2009
pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
2010
vm_page_iter_limit_init(&pages, first_object,
2011
OFF_TO_IDX(entry->offset) +
2012
atop(end - entry->start));
2013
VM_RADIX_FOREACH_FROM(m, &pages,
2014
OFF_TO_IDX(entry->offset) +
2015
atop(start - entry->start)) {
2016
if (!vm_page_all_valid(m) ||
2017
vm_page_busied(m))
2018
continue;
2019
2020
/*
2021
* Don't clear PGA_REFERENCED, since it would
2022
* likely represent a reference by a different
2023
* process.
2024
*
2025
* Typically, at this point, prefetched pages
2026
* are still in the inactive queue. Only
2027
* pages that triggered page faults are in the
2028
* active queue. The test for whether the page
2029
* is in the inactive queue is racy; in the
2030
* worst case we will requeue the page
2031
* unnecessarily.
2032
*/
2033
if (!vm_page_inactive(m))
2034
vm_page_deactivate(m);
2035
}
2036
}
2037
VM_OBJECT_RUNLOCK(first_object);
2038
}
2039
}
2040
2041
/*
2042
* vm_fault_prefault provides a quick way of clustering
2043
* pagefaults into a processes address space. It is a "cousin"
2044
* of vm_map_pmap_enter, except it runs at page fault time instead
2045
* of mmap time.
2046
*/
2047
static void
2048
vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
2049
int backward, int forward, bool obj_locked)
2050
{
2051
pmap_t pmap;
2052
vm_map_entry_t entry;
2053
vm_object_t backing_object, lobject;
2054
vm_offset_t addr, starta;
2055
vm_pindex_t pindex;
2056
vm_page_t m;
2057
vm_prot_t prot;
2058
int i;
2059
2060
pmap = fs->map->pmap;
2061
if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
2062
return;
2063
2064
entry = fs->entry;
2065
2066
if (addra < backward * PAGE_SIZE) {
2067
starta = entry->start;
2068
} else {
2069
starta = addra - backward * PAGE_SIZE;
2070
if (starta < entry->start)
2071
starta = entry->start;
2072
}
2073
prot = entry->protection;
2074
2075
/*
2076
* If pmap_enter() has enabled write access on a nearby mapping, then
2077
* don't attempt promotion, because it will fail.
2078
*/
2079
if ((fs->prot & VM_PROT_WRITE) != 0)
2080
prot |= VM_PROT_NO_PROMOTE;
2081
2082
/*
2083
* Generate the sequence of virtual addresses that are candidates for
2084
* prefaulting in an outward spiral from the faulting virtual address,
2085
* "addra". Specifically, the sequence is "addra - PAGE_SIZE", "addra
2086
* + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
2087
* If the candidate address doesn't have a backing physical page, then
2088
* the loop immediately terminates.
2089
*/
2090
for (i = 0; i < 2 * imax(backward, forward); i++) {
2091
addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
2092
PAGE_SIZE);
2093
if (addr > addra + forward * PAGE_SIZE)
2094
addr = 0;
2095
2096
if (addr < starta || addr >= entry->end)
2097
continue;
2098
2099
if (!pmap_is_prefaultable(pmap, addr))
2100
continue;
2101
2102
pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2103
lobject = entry->object.vm_object;
2104
if (!obj_locked)
2105
VM_OBJECT_RLOCK(lobject);
2106
while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
2107
!vm_fault_object_needs_getpages(lobject) &&
2108
(backing_object = lobject->backing_object) != NULL) {
2109
KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
2110
0, ("vm_fault_prefault: unaligned object offset"));
2111
pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2112
VM_OBJECT_RLOCK(backing_object);
2113
if (!obj_locked || lobject != entry->object.vm_object)
2114
VM_OBJECT_RUNLOCK(lobject);
2115
lobject = backing_object;
2116
}
2117
if (m == NULL) {
2118
if (!obj_locked || lobject != entry->object.vm_object)
2119
VM_OBJECT_RUNLOCK(lobject);
2120
break;
2121
}
2122
if (vm_page_all_valid(m) &&
2123
(m->flags & PG_FICTITIOUS) == 0)
2124
pmap_enter_quick(pmap, addr, m, prot);
2125
if (!obj_locked || lobject != entry->object.vm_object)
2126
VM_OBJECT_RUNLOCK(lobject);
2127
}
2128
}
2129
2130
/*
2131
* Hold each of the physical pages that are mapped by the specified
2132
* range of virtual addresses, ["addr", "addr" + "len"), if those
2133
* mappings are valid and allow the specified types of access, "prot".
2134
* If all of the implied pages are successfully held, then the number
2135
* of held pages is assigned to *ppages_count, together with pointers
2136
* to those pages in the array "ma". The returned value is zero.
2137
*
2138
* However, if any of the pages cannot be held, an error is returned,
2139
* and no pages are held.
2140
* Error values:
2141
* ENOMEM - the range is not valid
2142
* EINVAL - the provided vm_page array is too small to hold all pages
2143
* EAGAIN - a page was not mapped, and the thread is in nofaulting mode
2144
* EFAULT - a page with requested permissions cannot be mapped
2145
* (more detailed result from vm_fault() is lost)
2146
*/
2147
int
2148
vm_fault_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
2149
vm_prot_t prot, vm_page_t *ma, int max_count, int *ppages_count)
2150
{
2151
vm_offset_t end, va;
2152
vm_page_t *mp;
2153
int count, error;
2154
boolean_t pmap_failed;
2155
2156
if (len == 0) {
2157
*ppages_count = 0;
2158
return (0);
2159
}
2160
end = round_page(addr + len);
2161
addr = trunc_page(addr);
2162
2163
if (!vm_map_range_valid(map, addr, end))
2164
return (ENOMEM);
2165
2166
if (atop(end - addr) > max_count)
2167
return (EINVAL);
2168
count = atop(end - addr);
2169
2170
/*
2171
* Most likely, the physical pages are resident in the pmap, so it is
2172
* faster to try pmap_extract_and_hold() first.
2173
*/
2174
pmap_failed = FALSE;
2175
for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
2176
*mp = pmap_extract_and_hold(map->pmap, va, prot);
2177
if (*mp == NULL)
2178
pmap_failed = TRUE;
2179
else if ((prot & VM_PROT_WRITE) != 0 &&
2180
(*mp)->dirty != VM_PAGE_BITS_ALL) {
2181
/*
2182
* Explicitly dirty the physical page. Otherwise, the
2183
* caller's changes may go unnoticed because they are
2184
* performed through an unmanaged mapping or by a DMA
2185
* operation.
2186
*
2187
* The object lock is not held here.
2188
* See vm_page_clear_dirty_mask().
2189
*/
2190
vm_page_dirty(*mp);
2191
}
2192
}
2193
if (pmap_failed) {
2194
/*
2195
* One or more pages could not be held by the pmap. Either no
2196
* page was mapped at the specified virtual address or that
2197
* mapping had insufficient permissions. Attempt to fault in
2198
* and hold these pages.
2199
*
2200
* If vm_fault_disable_pagefaults() was called,
2201
* i.e., TDP_NOFAULTING is set, we must not sleep nor
2202
* acquire MD VM locks, which means we must not call
2203
* vm_fault(). Some (out of tree) callers mark
2204
* too wide a code area with vm_fault_disable_pagefaults()
2205
* already, use the VM_PROT_QUICK_NOFAULT flag to request
2206
* the proper behaviour explicitly.
2207
*/
2208
if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
2209
(curthread->td_pflags & TDP_NOFAULTING) != 0) {
2210
error = EAGAIN;
2211
goto fail;
2212
}
2213
for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
2214
if (*mp == NULL && vm_fault(map, va, prot,
2215
VM_FAULT_NORMAL, mp) != KERN_SUCCESS) {
2216
error = EFAULT;
2217
goto fail;
2218
}
2219
}
2220
}
2221
*ppages_count = count;
2222
return (0);
2223
fail:
2224
for (mp = ma; mp < ma + count; mp++)
2225
if (*mp != NULL)
2226
vm_page_unwire(*mp, PQ_INACTIVE);
2227
return (error);
2228
}
2229
2230
/*
2231
* Hold each of the physical pages that are mapped by the specified range of
2232
* virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
2233
* and allow the specified types of access, "prot". If all of the implied
2234
* pages are successfully held, then the number of held pages is returned
2235
* together with pointers to those pages in the array "ma". However, if any
2236
* of the pages cannot be held, -1 is returned.
2237
*/
2238
int
2239
vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
2240
vm_prot_t prot, vm_page_t *ma, int max_count)
2241
{
2242
int error, pages_count;
2243
2244
error = vm_fault_hold_pages(map, addr, len, prot, ma,
2245
max_count, &pages_count);
2246
if (error != 0) {
2247
if (error == EINVAL)
2248
panic("vm_fault_quick_hold_pages: count > max_count");
2249
return (-1);
2250
}
2251
return (pages_count);
2252
}
2253
2254
/*
2255
* Routine:
2256
* vm_fault_copy_entry
2257
* Function:
2258
* Create new object backing dst_entry with private copy of all
2259
* underlying pages. When src_entry is equal to dst_entry, function
2260
* implements COW for wired-down map entry. Otherwise, it forks
2261
* wired entry into dst_map.
2262
*
2263
* In/out conditions:
2264
* The source and destination maps must be locked for write.
2265
* The source map entry must be wired down (or be a sharing map
2266
* entry corresponding to a main map entry that is wired down).
2267
*/
2268
void
2269
vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map __unused,
2270
vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
2271
vm_ooffset_t *fork_charge)
2272
{
2273
struct pctrie_iter pages;
2274
vm_object_t backing_object, dst_object, object, src_object;
2275
vm_pindex_t dst_pindex, pindex, src_pindex;
2276
vm_prot_t access, prot;
2277
vm_offset_t vaddr;
2278
vm_page_t dst_m;
2279
vm_page_t src_m;
2280
bool upgrade;
2281
2282
upgrade = src_entry == dst_entry;
2283
KASSERT(upgrade || dst_entry->object.vm_object == NULL,
2284
("vm_fault_copy_entry: vm_object not NULL"));
2285
2286
/*
2287
* If not an upgrade, then enter the mappings in the pmap as
2288
* read and/or execute accesses. Otherwise, enter them as
2289
* write accesses.
2290
*
2291
* A writeable large page mapping is only created if all of
2292
* the constituent small page mappings are modified. Marking
2293
* PTEs as modified on inception allows promotion to happen
2294
* without taking potentially large number of soft faults.
2295
*/
2296
access = prot = dst_entry->protection;
2297
if (!upgrade)
2298
access &= ~VM_PROT_WRITE;
2299
2300
src_object = src_entry->object.vm_object;
2301
src_pindex = OFF_TO_IDX(src_entry->offset);
2302
2303
if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2304
dst_object = src_object;
2305
vm_object_reference(dst_object);
2306
} else {
2307
/*
2308
* Create the top-level object for the destination entry.
2309
* Doesn't actually shadow anything - we copy the pages
2310
* directly.
2311
*/
2312
dst_object = vm_object_allocate_anon(atop(dst_entry->end -
2313
dst_entry->start), NULL, NULL);
2314
#if VM_NRESERVLEVEL > 0
2315
dst_object->flags |= OBJ_COLORED;
2316
dst_object->pg_color = atop(dst_entry->start);
2317
#endif
2318
dst_object->domain = src_object->domain;
2319
2320
dst_entry->object.vm_object = dst_object;
2321
dst_entry->offset = 0;
2322
dst_entry->eflags &= ~MAP_ENTRY_VN_EXEC;
2323
}
2324
2325
VM_OBJECT_WLOCK(dst_object);
2326
if (fork_charge != NULL) {
2327
KASSERT(dst_entry->cred == NULL,
2328
("vm_fault_copy_entry: leaked swp charge"));
2329
dst_object->cred = curthread->td_ucred;
2330
crhold(dst_object->cred);
2331
*fork_charge += ptoa(dst_object->size);
2332
} else if ((dst_object->flags & OBJ_SWAP) != 0 &&
2333
dst_object->cred == NULL) {
2334
KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
2335
dst_entry));
2336
dst_object->cred = dst_entry->cred;
2337
dst_entry->cred = NULL;
2338
}
2339
2340
/*
2341
* Loop through all of the virtual pages within the entry's
2342
* range, copying each page from the source object to the
2343
* destination object. Since the source is wired, those pages
2344
* must exist. In contrast, the destination is pageable.
2345
* Since the destination object doesn't share any backing storage
2346
* with the source object, all of its pages must be dirtied,
2347
* regardless of whether they can be written.
2348
*/
2349
vm_page_iter_init(&pages, dst_object);
2350
for (vaddr = dst_entry->start, dst_pindex = 0;
2351
vaddr < dst_entry->end;
2352
vaddr += PAGE_SIZE, dst_pindex++) {
2353
again:
2354
/*
2355
* Find the page in the source object, and copy it in.
2356
* Because the source is wired down, the page will be
2357
* in memory.
2358
*/
2359
if (src_object != dst_object)
2360
VM_OBJECT_RLOCK(src_object);
2361
object = src_object;
2362
pindex = src_pindex + dst_pindex;
2363
while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
2364
(backing_object = object->backing_object) != NULL) {
2365
/*
2366
* Unless the source mapping is read-only or
2367
* it is presently being upgraded from
2368
* read-only, the first object in the shadow
2369
* chain should provide all of the pages. In
2370
* other words, this loop body should never be
2371
* executed when the source mapping is already
2372
* read/write.
2373
*/
2374
KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
2375
upgrade,
2376
("vm_fault_copy_entry: main object missing page"));
2377
2378
VM_OBJECT_RLOCK(backing_object);
2379
pindex += OFF_TO_IDX(object->backing_object_offset);
2380
if (object != dst_object)
2381
VM_OBJECT_RUNLOCK(object);
2382
object = backing_object;
2383
}
2384
KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
2385
2386
if (object != dst_object) {
2387
/*
2388
* Allocate a page in the destination object.
2389
*/
2390
pindex = (src_object == dst_object ? src_pindex : 0) +
2391
dst_pindex;
2392
dst_m = vm_page_alloc_iter(dst_object, pindex,
2393
VM_ALLOC_NORMAL, &pages);
2394
if (dst_m == NULL) {
2395
VM_OBJECT_WUNLOCK(dst_object);
2396
VM_OBJECT_RUNLOCK(object);
2397
vm_wait(dst_object);
2398
VM_OBJECT_WLOCK(dst_object);
2399
pctrie_iter_reset(&pages);
2400
goto again;
2401
}
2402
2403
/*
2404
* See the comment in vm_fault_cow().
2405
*/
2406
if (src_object == dst_object &&
2407
(object->flags & OBJ_ONEMAPPING) == 0)
2408
pmap_remove_all(src_m);
2409
pmap_copy_page(src_m, dst_m);
2410
2411
/*
2412
* The object lock does not guarantee that "src_m" will
2413
* transition from invalid to valid, but it does ensure
2414
* that "src_m" will not transition from valid to
2415
* invalid.
2416
*/
2417
dst_m->dirty = dst_m->valid = src_m->valid;
2418
VM_OBJECT_RUNLOCK(object);
2419
} else {
2420
dst_m = src_m;
2421
if (vm_page_busy_acquire(
2422
dst_m, VM_ALLOC_WAITFAIL) == 0) {
2423
pctrie_iter_reset(&pages);
2424
goto again;
2425
}
2426
if (dst_m->pindex >= dst_object->size) {
2427
/*
2428
* We are upgrading. Index can occur
2429
* out of bounds if the object type is
2430
* vnode and the file was truncated.
2431
*/
2432
vm_page_xunbusy(dst_m);
2433
break;
2434
}
2435
}
2436
2437
/*
2438
* Enter it in the pmap. If a wired, copy-on-write
2439
* mapping is being replaced by a write-enabled
2440
* mapping, then wire that new mapping.
2441
*
2442
* The page can be invalid if the user called
2443
* msync(MS_INVALIDATE) or truncated the backing vnode
2444
* or shared memory object. In this case, do not
2445
* insert it into pmap, but still do the copy so that
2446
* all copies of the wired map entry have similar
2447
* backing pages.
2448
*/
2449
if (vm_page_all_valid(dst_m)) {
2450
VM_OBJECT_WUNLOCK(dst_object);
2451
pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
2452
access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
2453
VM_OBJECT_WLOCK(dst_object);
2454
}
2455
2456
/*
2457
* Mark it no longer busy, and put it on the active list.
2458
*/
2459
if (upgrade) {
2460
if (src_m != dst_m) {
2461
vm_page_unwire(src_m, PQ_INACTIVE);
2462
vm_page_wire(dst_m);
2463
} else {
2464
KASSERT(vm_page_wired(dst_m),
2465
("dst_m %p is not wired", dst_m));
2466
}
2467
} else {
2468
vm_page_activate(dst_m);
2469
}
2470
vm_page_xunbusy(dst_m);
2471
}
2472
VM_OBJECT_WUNLOCK(dst_object);
2473
if (upgrade) {
2474
dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
2475
vm_object_deallocate(src_object);
2476
}
2477
}
2478
2479
/*
2480
* Block entry into the machine-independent layer's page fault handler by
2481
* the calling thread. Subsequent calls to vm_fault() by that thread will
2482
* return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
2483
* spurious page faults.
2484
*/
2485
int
2486
vm_fault_disable_pagefaults(void)
2487
{
2488
2489
return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
2490
}
2491
2492
void
2493
vm_fault_enable_pagefaults(int save)
2494
{
2495
2496
curthread_pflags_restore(save);
2497
}
2498
2499