uvm_pager.c revision 1.125 1 1.125 ad /* $NetBSD: uvm_pager.c,v 1.125 2020/04/19 21:53:38 ad Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg *
16 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 mrg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 mrg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 mrg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 mrg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 mrg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.3 mrg *
27 1.3 mrg * from: Id: uvm_pager.c,v 1.1.2.23 1998/02/02 20:38:06 chuck Exp
28 1.1 mrg */
29 1.1 mrg
30 1.1 mrg /*
31 1.1 mrg * uvm_pager.c: generic functions used to assist the pagers.
32 1.1 mrg */
33 1.54 lukem
34 1.54 lukem #include <sys/cdefs.h>
35 1.125 ad __KERNEL_RCSID(0, "$NetBSD: uvm_pager.c,v 1.125 2020/04/19 21:53:38 ad Exp $");
36 1.54 lukem
37 1.54 lukem #include "opt_uvmhist.h"
38 1.72 yamt #include "opt_readahead.h"
39 1.87 yamt #include "opt_pagermap.h"
40 1.1 mrg
41 1.1 mrg #include <sys/param.h>
42 1.1 mrg #include <sys/systm.h>
43 1.113 uwe #include <sys/atomic.h>
44 1.35 chs #include <sys/vnode.h>
45 1.93 pooka #include <sys/buf.h>
46 1.1 mrg
47 1.1 mrg #include <uvm/uvm.h>
48 1.1 mrg
49 1.87 yamt /*
50 1.87 yamt * XXX
51 1.87 yamt * this is needed until the device strategy interface
52 1.87 yamt * is changed to do physically-addressed i/o.
53 1.87 yamt */
54 1.87 yamt
55 1.87 yamt #ifndef PAGER_MAP_DEFAULT_SIZE
56 1.87 yamt #define PAGER_MAP_DEFAULT_SIZE (16 * 1024 * 1024)
57 1.87 yamt #endif
58 1.87 yamt
59 1.87 yamt #ifndef PAGER_MAP_SIZE
60 1.87 yamt #define PAGER_MAP_SIZE PAGER_MAP_DEFAULT_SIZE
61 1.87 yamt #endif
62 1.87 yamt
63 1.87 yamt size_t pager_map_size = PAGER_MAP_SIZE;
64 1.87 yamt
65 1.1 mrg /*
66 1.1 mrg * list of uvm pagers in the system
67 1.1 mrg */
68 1.1 mrg
69 1.89 yamt const struct uvm_pagerops * const uvmpagerops[] = {
70 1.10 thorpej &aobj_pager,
71 1.6 mrg &uvm_deviceops,
72 1.6 mrg &uvm_vnodeops,
73 1.35 chs &ubc_pager,
74 1.1 mrg };
75 1.1 mrg
76 1.1 mrg /*
77 1.1 mrg * the pager map: provides KVA for I/O
78 1.1 mrg */
79 1.1 mrg
80 1.47 chs struct vm_map *pager_map; /* XXX */
81 1.112 ad kmutex_t pager_map_wanted_lock __cacheline_aligned;
82 1.80 thorpej bool pager_map_wanted; /* locked by pager map */
83 1.35 chs static vaddr_t emergva;
84 1.105 matt static int emerg_ncolors;
85 1.80 thorpej static bool emerginuse;
86 1.1 mrg
87 1.105 matt void
88 1.105 matt uvm_pager_realloc_emerg(void)
89 1.105 matt {
90 1.105 matt vaddr_t new_emergva, old_emergva;
91 1.105 matt int old_emerg_ncolors;
92 1.105 matt
93 1.105 matt if (__predict_true(emergva != 0 && emerg_ncolors >= uvmexp.ncolors))
94 1.105 matt return;
95 1.105 matt
96 1.105 matt KASSERT(!emerginuse);
97 1.105 matt
98 1.105 matt new_emergva = uvm_km_alloc(kernel_map,
99 1.106 uebayasi round_page(MAXPHYS) + ptoa(uvmexp.ncolors), ptoa(uvmexp.ncolors),
100 1.105 matt UVM_KMF_VAONLY);
101 1.105 matt
102 1.105 matt KASSERT(new_emergva != 0);
103 1.105 matt
104 1.105 matt old_emergva = emergva;
105 1.105 matt old_emerg_ncolors = emerg_ncolors;
106 1.105 matt
107 1.105 matt /*
108 1.105 matt * don't support re-color in late boot anyway.
109 1.105 matt */
110 1.105 matt if (0) /* XXX */
111 1.105 matt mutex_enter(&pager_map_wanted_lock);
112 1.105 matt
113 1.105 matt emergva = new_emergva;
114 1.105 matt emerg_ncolors = uvmexp.ncolors;
115 1.105 matt wakeup(&old_emergva);
116 1.105 matt
117 1.105 matt if (0) /* XXX */
118 1.105 matt mutex_exit(&pager_map_wanted_lock);
119 1.105 matt
120 1.105 matt if (old_emergva)
121 1.105 matt uvm_km_free(kernel_map, old_emergva,
122 1.105 matt round_page(MAXPHYS) + ptoa(old_emerg_ncolors),
123 1.105 matt UVM_KMF_VAONLY);
124 1.105 matt }
125 1.105 matt
126 1.1 mrg /*
127 1.1 mrg * uvm_pager_init: init pagers (at boot time)
128 1.1 mrg */
129 1.1 mrg
130 1.6 mrg void
131 1.67 thorpej uvm_pager_init(void)
132 1.6 mrg {
133 1.59 thorpej u_int lcv;
134 1.50 chs vaddr_t sva, eva;
135 1.1 mrg
136 1.6 mrg /*
137 1.6 mrg * init pager map
138 1.6 mrg */
139 1.6 mrg
140 1.50 chs sva = 0;
141 1.87 yamt pager_map = uvm_km_suballoc(kernel_map, &sva, &eva, pager_map_size, 0,
142 1.81 thorpej false, NULL);
143 1.84 ad mutex_init(&pager_map_wanted_lock, MUTEX_DEFAULT, IPL_NONE);
144 1.81 thorpej pager_map_wanted = false;
145 1.105 matt
146 1.105 matt uvm_pager_realloc_emerg();
147 1.6 mrg
148 1.6 mrg /*
149 1.6 mrg * call pager init functions
150 1.6 mrg */
151 1.88 yamt for (lcv = 0 ; lcv < __arraycount(uvmpagerops); lcv++) {
152 1.6 mrg if (uvmpagerops[lcv]->pgo_init)
153 1.6 mrg uvmpagerops[lcv]->pgo_init();
154 1.6 mrg }
155 1.1 mrg }
156 1.1 mrg
157 1.124 ad #ifdef PMAP_DIRECT
158 1.124 ad /*
159 1.124 ad * uvm_pagermapdirect: map a single page via the pmap's direct segment
160 1.124 ad *
161 1.124 ad * this is an abuse of pmap_direct_process(), since the kva is being grabbed
162 1.124 ad * and no processing is taking place, but for now..
163 1.124 ad */
164 1.124 ad
165 1.124 ad static int
166 1.124 ad uvm_pagermapdirect(void *kva, size_t sz, void *cookie)
167 1.124 ad {
168 1.124 ad
169 1.124 ad KASSERT(sz == PAGE_SIZE);
170 1.124 ad *(vaddr_t *)cookie = (vaddr_t)kva;
171 1.124 ad return 0;
172 1.124 ad }
173 1.124 ad #endif
174 1.124 ad
175 1.1 mrg /*
176 1.1 mrg * uvm_pagermapin: map pages into KVA (pager_map) for I/O that needs mappings
177 1.1 mrg *
178 1.1 mrg * we basically just map in a blank map entry to reserve the space in the
179 1.1 mrg * map and then use pmap_enter() to put the mappings in by hand.
180 1.1 mrg */
181 1.1 mrg
182 1.9 eeh vaddr_t
183 1.67 thorpej uvm_pagermapin(struct vm_page **pps, int npages, int flags)
184 1.1 mrg {
185 1.9 eeh vsize_t size;
186 1.9 eeh vaddr_t kva;
187 1.9 eeh vaddr_t cva;
188 1.6 mrg struct vm_page *pp;
189 1.29 thorpej vm_prot_t prot;
190 1.104 matt const bool pdaemon = (curlwp == uvm.pagedaemon_lwp);
191 1.117 ad const u_int first_color = VM_PGCOLOR(*pps);
192 1.6 mrg UVMHIST_FUNC("uvm_pagermapin"); UVMHIST_CALLED(maphist);
193 1.1 mrg
194 1.123 rin UVMHIST_LOG(maphist,"(pps=%#jx, npages=%jd, first_color=%ju)",
195 1.111 pgoyette (uintptr_t)pps, npages, first_color, 0);
196 1.29 thorpej
197 1.124 ad #ifdef PMAP_DIRECT
198 1.124 ad /*
199 1.124 ad * for a single page the direct mapped segment can be used.
200 1.124 ad */
201 1.124 ad
202 1.124 ad if (npages == 1) {
203 1.124 ad int error __diagused;
204 1.124 ad KASSERT((pps[0]->flags & PG_BUSY) != 0);
205 1.124 ad error = pmap_direct_process(VM_PAGE_TO_PHYS(pps[0]), 0,
206 1.124 ad PAGE_SIZE, uvm_pagermapdirect, &kva);
207 1.124 ad KASSERT(error == 0);
208 1.124 ad UVMHIST_LOG(maphist, "<- done, direct (KVA=%#jx)", kva,0,0,0);
209 1.124 ad return kva;
210 1.124 ad }
211 1.124 ad #endif
212 1.124 ad
213 1.29 thorpej /*
214 1.29 thorpej * compute protection. outgoing I/O only needs read
215 1.29 thorpej * access to the page, whereas incoming needs read/write.
216 1.29 thorpej */
217 1.29 thorpej
218 1.29 thorpej prot = VM_PROT_READ;
219 1.29 thorpej if (flags & UVMPAGER_MAPIN_READ)
220 1.29 thorpej prot |= VM_PROT_WRITE;
221 1.1 mrg
222 1.1 mrg ReStart:
223 1.104 matt size = ptoa(npages);
224 1.29 thorpej kva = 0; /* let system choose VA */
225 1.1 mrg
226 1.104 matt if (uvm_map(pager_map, &kva, size, NULL, UVM_UNKNOWN_OFFSET,
227 1.104 matt first_color, UVM_FLAG_COLORMATCH | UVM_FLAG_NOMERGE
228 1.104 matt | (pdaemon ? UVM_FLAG_NOWAIT : 0)) != 0) {
229 1.75 yamt if (pdaemon) {
230 1.84 ad mutex_enter(&pager_map_wanted_lock);
231 1.35 chs if (emerginuse) {
232 1.90 ad UVM_UNLOCK_AND_WAIT(&emergva,
233 1.90 ad &pager_map_wanted_lock, false,
234 1.90 ad "emergva", 0);
235 1.35 chs goto ReStart;
236 1.35 chs }
237 1.81 thorpej emerginuse = true;
238 1.84 ad mutex_exit(&pager_map_wanted_lock);
239 1.104 matt kva = emergva + ptoa(first_color);
240 1.60 tls /* The shift implicitly truncates to PAGE_SIZE */
241 1.60 tls KASSERT(npages <= (MAXPHYS >> PAGE_SHIFT));
242 1.35 chs goto enter;
243 1.35 chs }
244 1.29 thorpej if ((flags & UVMPAGER_MAPIN_WAITOK) == 0) {
245 1.6 mrg UVMHIST_LOG(maphist,"<- NOWAIT failed", 0,0,0,0);
246 1.29 thorpej return(0);
247 1.6 mrg }
248 1.84 ad mutex_enter(&pager_map_wanted_lock);
249 1.81 thorpej pager_map_wanted = true;
250 1.6 mrg UVMHIST_LOG(maphist, " SLEEPING on pager_map",0,0,0,0);
251 1.90 ad UVM_UNLOCK_AND_WAIT(pager_map, &pager_map_wanted_lock, false,
252 1.90 ad "pager_map", 0);
253 1.6 mrg goto ReStart;
254 1.6 mrg }
255 1.1 mrg
256 1.35 chs enter:
257 1.6 mrg /* got it */
258 1.104 matt for (cva = kva; npages != 0; npages--, cva += PAGE_SIZE) {
259 1.6 mrg pp = *pps++;
260 1.40 mrg KASSERT(pp);
261 1.104 matt // KASSERT(!((VM_PAGE_TO_PHYS(pp) ^ cva) & uvmexp.colormask));
262 1.38 chs KASSERT(pp->flags & PG_BUSY);
263 1.97 cegger pmap_kenter_pa(cva, VM_PAGE_TO_PHYS(pp), prot, 0);
264 1.6 mrg }
265 1.49 chris pmap_update(vm_map_pmap(pager_map));
266 1.1 mrg
267 1.123 rin UVMHIST_LOG(maphist, "<- done (KVA=%#jx)", kva,0,0,0);
268 1.6 mrg return(kva);
269 1.1 mrg }
270 1.1 mrg
271 1.1 mrg /*
272 1.1 mrg * uvm_pagermapout: remove pager_map mapping
273 1.1 mrg *
274 1.1 mrg * we remove our mappings by hand and then remove the mapping (waking
275 1.1 mrg * up anyone wanting space).
276 1.1 mrg */
277 1.1 mrg
278 1.6 mrg void
279 1.67 thorpej uvm_pagermapout(vaddr_t kva, int npages)
280 1.6 mrg {
281 1.104 matt vsize_t size = ptoa(npages);
282 1.47 chs struct vm_map_entry *entries;
283 1.6 mrg UVMHIST_FUNC("uvm_pagermapout"); UVMHIST_CALLED(maphist);
284 1.35 chs
285 1.123 rin UVMHIST_LOG(maphist, " (kva=%#jx, npages=%jd)", kva, npages,0,0);
286 1.1 mrg
287 1.124 ad #ifdef PMAP_DIRECT
288 1.124 ad /*
289 1.124 ad * solitary pages are mapped directly.
290 1.124 ad */
291 1.124 ad
292 1.124 ad if (npages == 1) {
293 1.124 ad UVMHIST_LOG(maphist,"<- done, direct", 0,0,0,0);
294 1.124 ad return;
295 1.124 ad }
296 1.124 ad #endif
297 1.124 ad
298 1.6 mrg /*
299 1.6 mrg * duplicate uvm_unmap, but add in pager_map_wanted handling.
300 1.6 mrg */
301 1.6 mrg
302 1.104 matt pmap_kremove(kva, size);
303 1.98 rmind pmap_update(pmap_kernel());
304 1.98 rmind
305 1.104 matt if ((kva & ~ptoa(uvmexp.colormask)) == emergva) {
306 1.84 ad mutex_enter(&pager_map_wanted_lock);
307 1.106 uebayasi KASSERT(emerginuse);
308 1.81 thorpej emerginuse = false;
309 1.35 chs wakeup(&emergva);
310 1.84 ad mutex_exit(&pager_map_wanted_lock);
311 1.50 chs return;
312 1.35 chs }
313 1.35 chs
314 1.6 mrg vm_map_lock(pager_map);
315 1.108 para uvm_unmap_remove(pager_map, kva, kva + size, &entries, 0);
316 1.84 ad mutex_enter(&pager_map_wanted_lock);
317 1.6 mrg if (pager_map_wanted) {
318 1.81 thorpej pager_map_wanted = false;
319 1.6 mrg wakeup(pager_map);
320 1.6 mrg }
321 1.84 ad mutex_exit(&pager_map_wanted_lock);
322 1.6 mrg vm_map_unlock(pager_map);
323 1.6 mrg if (entries)
324 1.6 mrg uvm_unmap_detach(entries, 0);
325 1.6 mrg UVMHIST_LOG(maphist,"<- done",0,0,0,0);
326 1.1 mrg }
327 1.1 mrg
328 1.35 chs void
329 1.91 yamt uvm_aio_aiodone_pages(struct vm_page **pgs, int npages, bool write, int error)
330 1.35 chs {
331 1.35 chs struct uvm_object *uobj;
332 1.91 yamt struct vm_page *pg;
333 1.122 ad krwlock_t *slock;
334 1.107 yamt int pageout_done; /* number of PG_PAGEOUT pages processed */
335 1.91 yamt int swslot;
336 1.91 yamt int i;
337 1.91 yamt bool swap;
338 1.92 simonb UVMHIST_FUNC("uvm_aio_aiodone_pages"); UVMHIST_CALLED(ubchist);
339 1.50 chs
340 1.50 chs swslot = 0;
341 1.91 yamt pageout_done = 0;
342 1.50 chs slock = NULL;
343 1.91 yamt uobj = NULL;
344 1.55 chs pg = pgs[0];
345 1.55 chs swap = (pg->uanon != NULL && pg->uobject == NULL) ||
346 1.114 ad (pg->flags & PG_AOBJ) != 0;
347 1.50 chs if (!swap) {
348 1.55 chs uobj = pg->uobject;
349 1.101 rmind slock = uobj->vmobjlock;
350 1.122 ad rw_enter(slock, RW_WRITER);
351 1.71 yamt } else {
352 1.71 yamt #if defined(VMSWAP)
353 1.71 yamt if (error) {
354 1.71 yamt if (pg->uobject != NULL) {
355 1.71 yamt swslot = uao_find_swslot(pg->uobject,
356 1.71 yamt pg->offset >> PAGE_SHIFT);
357 1.77 christos } else {
358 1.77 christos KASSERT(pg->uanon != NULL);
359 1.71 yamt swslot = pg->uanon->an_swslot;
360 1.71 yamt }
361 1.71 yamt KASSERT(swslot);
362 1.50 chs }
363 1.71 yamt #else /* defined(VMSWAP) */
364 1.71 yamt panic("%s: swap", __func__);
365 1.71 yamt #endif /* defined(VMSWAP) */
366 1.50 chs }
367 1.35 chs for (i = 0; i < npages; i++) {
368 1.103 oki #if defined(VMSWAP)
369 1.102 yamt bool anon_disposed = false; /* XXX gcc */
370 1.103 oki #endif /* defined(VMSWAP) */
371 1.102 yamt
372 1.35 chs pg = pgs[i];
373 1.50 chs KASSERT(swap || pg->uobject == uobj);
374 1.111 pgoyette UVMHIST_LOG(ubchist, "pg %#jx", (uintptr_t)pg, 0,0,0);
375 1.50 chs
376 1.71 yamt #if defined(VMSWAP)
377 1.50 chs /*
378 1.50 chs * for swap i/os, lock each page's object (or anon)
379 1.50 chs * individually since each page may need a different lock.
380 1.50 chs */
381 1.35 chs
382 1.35 chs if (swap) {
383 1.55 chs if (pg->uobject != NULL) {
384 1.101 rmind slock = pg->uobject->vmobjlock;
385 1.55 chs } else {
386 1.101 rmind slock = pg->uanon->an_lock;
387 1.35 chs }
388 1.122 ad rw_enter(slock, RW_WRITER);
389 1.102 yamt anon_disposed = (pg->flags & PG_RELEASED) != 0;
390 1.102 yamt KASSERT(!anon_disposed || pg->uobject != NULL ||
391 1.102 yamt pg->uanon->an_ref == 0);
392 1.50 chs }
393 1.71 yamt #endif /* defined(VMSWAP) */
394 1.50 chs
395 1.120 ad if (write && uobj != NULL) {
396 1.120 ad KASSERT(radix_tree_get_tag(&uobj->uo_pages,
397 1.120 ad pg->offset >> PAGE_SHIFT, UVM_PAGE_WRITEBACK_TAG));
398 1.120 ad radix_tree_clear_tag(&uobj->uo_pages,
399 1.120 ad pg->offset >> PAGE_SHIFT, UVM_PAGE_WRITEBACK_TAG);
400 1.120 ad }
401 1.120 ad
402 1.50 chs /*
403 1.50 chs * process errors. for reads, just mark the page to be freed.
404 1.50 chs * for writes, if the error was ENOMEM, we assume this was
405 1.50 chs * a transient failure so we mark the page dirty so that
406 1.50 chs * we'll try to write it again later. for all other write
407 1.50 chs * errors, we assume the error is permanent, thus the data
408 1.50 chs * in the page is lost. bummer.
409 1.50 chs */
410 1.50 chs
411 1.50 chs if (error) {
412 1.61 pk int slot;
413 1.50 chs if (!write) {
414 1.50 chs pg->flags |= PG_RELEASED;
415 1.50 chs continue;
416 1.50 chs } else if (error == ENOMEM) {
417 1.50 chs if (pg->flags & PG_PAGEOUT) {
418 1.50 chs pg->flags &= ~PG_PAGEOUT;
419 1.90 ad pageout_done++;
420 1.50 chs }
421 1.120 ad uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
422 1.119 ad uvm_pagelock(pg);
423 1.50 chs uvm_pageactivate(pg);
424 1.119 ad uvm_pageunlock(pg);
425 1.61 pk slot = 0;
426 1.61 pk } else
427 1.61 pk slot = SWSLOT_BAD;
428 1.61 pk
429 1.71 yamt #if defined(VMSWAP)
430 1.61 pk if (swap) {
431 1.61 pk if (pg->uobject != NULL) {
432 1.109 martin int oldslot __diagused;
433 1.62 pk oldslot = uao_set_swslot(pg->uobject,
434 1.62 pk pg->offset >> PAGE_SHIFT, slot);
435 1.62 pk KASSERT(oldslot == swslot + i);
436 1.61 pk } else {
437 1.61 pk KASSERT(pg->uanon->an_swslot ==
438 1.61 pk swslot + i);
439 1.61 pk pg->uanon->an_swslot = slot;
440 1.61 pk }
441 1.50 chs }
442 1.71 yamt #endif /* defined(VMSWAP) */
443 1.50 chs }
444 1.50 chs
445 1.50 chs /*
446 1.50 chs * if the page is PG_FAKE, this must have been a read to
447 1.50 chs * initialize the page. clear PG_FAKE and activate the page.
448 1.50 chs */
449 1.50 chs
450 1.50 chs if (pg->flags & PG_FAKE) {
451 1.50 chs KASSERT(!write);
452 1.50 chs pg->flags &= ~PG_FAKE;
453 1.72 yamt #if defined(READAHEAD_STATS)
454 1.114 ad pg->flags |= PG_READAHEAD;
455 1.72 yamt uvm_ra_total.ev_count++;
456 1.72 yamt #endif /* defined(READAHEAD_STATS) */
457 1.120 ad KASSERT(uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_CLEAN);
458 1.119 ad uvm_pagelock(pg);
459 1.78 yamt uvm_pageenqueue(pg);
460 1.119 ad uvm_pageunlock(pg);
461 1.35 chs }
462 1.35 chs
463 1.35 chs /*
464 1.53 chs * do accounting for pagedaemon i/o and arrange to free
465 1.53 chs * the pages instead of just unbusying them.
466 1.35 chs */
467 1.35 chs
468 1.53 chs if (pg->flags & PG_PAGEOUT) {
469 1.50 chs pg->flags &= ~PG_PAGEOUT;
470 1.90 ad pageout_done++;
471 1.114 ad atomic_inc_uint(&uvmexp.pdfreed);
472 1.35 chs pg->flags |= PG_RELEASED;
473 1.35 chs }
474 1.35 chs
475 1.71 yamt #if defined(VMSWAP)
476 1.35 chs /*
477 1.50 chs * for swap pages, unlock everything for this page now.
478 1.35 chs */
479 1.35 chs
480 1.35 chs if (swap) {
481 1.102 yamt if (pg->uobject == NULL && anon_disposed) {
482 1.63 yamt uvm_anon_release(pg->uanon);
483 1.63 yamt } else {
484 1.63 yamt uvm_page_unbusy(&pg, 1);
485 1.122 ad rw_exit(slock);
486 1.63 yamt }
487 1.35 chs }
488 1.71 yamt #endif /* defined(VMSWAP) */
489 1.35 chs }
490 1.125 ad if (pageout_done != 0) {
491 1.125 ad uvm_pageout_done(pageout_done);
492 1.125 ad }
493 1.35 chs if (!swap) {
494 1.50 chs uvm_page_unbusy(pgs, npages);
495 1.122 ad rw_exit(slock);
496 1.50 chs } else {
497 1.71 yamt #if defined(VMSWAP)
498 1.53 chs KASSERT(write);
499 1.53 chs
500 1.53 chs /* these pages are now only in swap. */
501 1.110 christos if (error != ENOMEM) {
502 1.112 ad atomic_add_int(&uvmexp.swpgonly, npages);
503 1.110 christos }
504 1.50 chs if (error) {
505 1.61 pk if (error != ENOMEM)
506 1.61 pk uvm_swap_markbad(swslot, npages);
507 1.61 pk else
508 1.61 pk uvm_swap_free(swslot, npages);
509 1.50 chs }
510 1.116 ad atomic_dec_uint(&uvmexp.pdpending);
511 1.71 yamt #endif /* defined(VMSWAP) */
512 1.35 chs }
513 1.91 yamt }
514 1.91 yamt
515 1.91 yamt /*
516 1.91 yamt * uvm_aio_aiodone: do iodone processing for async i/os.
517 1.91 yamt * this should be called in thread context, not interrupt context.
518 1.91 yamt */
519 1.91 yamt
520 1.91 yamt void
521 1.91 yamt uvm_aio_aiodone(struct buf *bp)
522 1.91 yamt {
523 1.91 yamt int npages = bp->b_bufsize >> PAGE_SHIFT;
524 1.91 yamt struct vm_page *pgs[npages];
525 1.91 yamt int i, error;
526 1.91 yamt bool write;
527 1.91 yamt UVMHIST_FUNC("uvm_aio_aiodone"); UVMHIST_CALLED(ubchist);
528 1.111 pgoyette UVMHIST_LOG(ubchist, "bp %#jx", (uintptr_t)bp, 0,0,0);
529 1.91 yamt
530 1.91 yamt error = bp->b_error;
531 1.91 yamt write = (bp->b_flags & B_READ) == 0;
532 1.91 yamt
533 1.91 yamt for (i = 0; i < npages; i++) {
534 1.91 yamt pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
535 1.111 pgoyette UVMHIST_LOG(ubchist, "pgs[%jd] = %#jx", i,
536 1.111 pgoyette (uintptr_t)pgs[i], 0, 0);
537 1.91 yamt }
538 1.91 yamt uvm_pagermapout((vaddr_t)bp->b_data, npages);
539 1.91 yamt
540 1.91 yamt uvm_aio_aiodone_pages(pgs, npages, write, error);
541 1.91 yamt
542 1.90 ad if (write && (bp->b_cflags & BC_AGE) != 0) {
543 1.90 ad mutex_enter(bp->b_objlock);
544 1.35 chs vwakeup(bp);
545 1.90 ad mutex_exit(bp->b_objlock);
546 1.35 chs }
547 1.73 yamt putiobuf(bp);
548 1.1 mrg }
549 1.74 yamt
550 1.74 yamt /*
551 1.74 yamt * uvm_pageratop: convert KVAs in the pager map back to their page
552 1.74 yamt * structures.
553 1.74 yamt */
554 1.74 yamt
555 1.74 yamt struct vm_page *
556 1.74 yamt uvm_pageratop(vaddr_t kva)
557 1.74 yamt {
558 1.74 yamt struct vm_page *pg;
559 1.74 yamt paddr_t pa;
560 1.109 martin bool rv __diagused;
561 1.74 yamt
562 1.74 yamt rv = pmap_extract(pmap_kernel(), kva, &pa);
563 1.74 yamt KASSERT(rv);
564 1.74 yamt pg = PHYS_TO_VM_PAGE(pa);
565 1.74 yamt KASSERT(pg != NULL);
566 1.74 yamt return (pg);
567 1.74 yamt }
568