1 | /* $NetBSD: uvm_aobj.c,v 1.124 2016/07/28 07:52:06 martin Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 1998 Chuck Silvers, Charles D. Cranor and |
5 | * Washington University. |
6 | * All rights reserved. |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions |
10 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * |
28 | * from: Id: uvm_aobj.c,v 1.1.2.5 1998/02/06 05:14:38 chs Exp |
29 | */ |
30 | |
31 | /* |
32 | * uvm_aobj.c: anonymous memory uvm_object pager |
33 | * |
34 | * author: Chuck Silvers <chuq@chuq.com> |
35 | * started: Jan-1998 |
36 | * |
37 | * - design mostly from Chuck Cranor |
38 | */ |
39 | |
40 | #include <sys/cdefs.h> |
41 | __KERNEL_RCSID(0, "$NetBSD: uvm_aobj.c,v 1.124 2016/07/28 07:52:06 martin Exp $" ); |
42 | |
43 | #ifdef _KERNEL_OPT |
44 | #include "opt_uvmhist.h" |
45 | #endif |
46 | |
47 | #include <sys/param.h> |
48 | #include <sys/systm.h> |
49 | #include <sys/kernel.h> |
50 | #include <sys/kmem.h> |
51 | #include <sys/pool.h> |
52 | #include <sys/atomic.h> |
53 | |
54 | #include <uvm/uvm.h> |
55 | |
56 | /* |
57 | * An anonymous UVM object (aobj) manages anonymous-memory. In addition to |
58 | * keeping the list of resident pages, it may also keep a list of allocated |
59 | * swap blocks. Depending on the size of the object, this list is either |
60 | * stored in an array (small objects) or in a hash table (large objects). |
61 | * |
62 | * Lock order |
63 | * |
64 | * uao_list_lock -> |
65 | * uvm_object::vmobjlock |
66 | */ |
67 | |
68 | /* |
69 | * Note: for hash tables, we break the address space of the aobj into blocks |
70 | * of UAO_SWHASH_CLUSTER_SIZE pages, which shall be a power of two. |
71 | */ |
72 | |
73 | #define UAO_SWHASH_CLUSTER_SHIFT 4 |
74 | #define UAO_SWHASH_CLUSTER_SIZE (1 << UAO_SWHASH_CLUSTER_SHIFT) |
75 | |
76 | /* Get the "tag" for this page index. */ |
77 | #define UAO_SWHASH_ELT_TAG(idx) ((idx) >> UAO_SWHASH_CLUSTER_SHIFT) |
78 | #define UAO_SWHASH_ELT_PAGESLOT_IDX(idx) \ |
79 | ((idx) & (UAO_SWHASH_CLUSTER_SIZE - 1)) |
80 | |
81 | /* Given an ELT and a page index, find the swap slot. */ |
82 | #define UAO_SWHASH_ELT_PAGESLOT(elt, idx) \ |
83 | ((elt)->slots[UAO_SWHASH_ELT_PAGESLOT_IDX(idx)]) |
84 | |
85 | /* Given an ELT, return its pageidx base. */ |
86 | #define UAO_SWHASH_ELT_PAGEIDX_BASE(ELT) \ |
87 | ((elt)->tag << UAO_SWHASH_CLUSTER_SHIFT) |
88 | |
89 | /* The hash function. */ |
90 | #define UAO_SWHASH_HASH(aobj, idx) \ |
91 | (&(aobj)->u_swhash[(((idx) >> UAO_SWHASH_CLUSTER_SHIFT) \ |
92 | & (aobj)->u_swhashmask)]) |
93 | |
94 | /* |
95 | * The threshold which determines whether we will use an array or a |
96 | * hash table to store the list of allocated swap blocks. |
97 | */ |
98 | #define UAO_SWHASH_THRESHOLD (UAO_SWHASH_CLUSTER_SIZE * 4) |
99 | #define UAO_USES_SWHASH(aobj) \ |
100 | ((aobj)->u_pages > UAO_SWHASH_THRESHOLD) |
101 | |
102 | /* The number of buckets in a hash, with an upper bound. */ |
103 | #define UAO_SWHASH_MAXBUCKETS 256 |
104 | #define UAO_SWHASH_BUCKETS(aobj) \ |
105 | (MIN((aobj)->u_pages >> UAO_SWHASH_CLUSTER_SHIFT, UAO_SWHASH_MAXBUCKETS)) |
106 | |
107 | /* |
108 | * uao_swhash_elt: when a hash table is being used, this structure defines |
109 | * the format of an entry in the bucket list. |
110 | */ |
111 | |
112 | struct uao_swhash_elt { |
113 | LIST_ENTRY(uao_swhash_elt) list; /* the hash list */ |
114 | voff_t tag; /* our 'tag' */ |
115 | int count; /* our number of active slots */ |
116 | int slots[UAO_SWHASH_CLUSTER_SIZE]; /* the slots */ |
117 | }; |
118 | |
119 | /* |
120 | * uao_swhash: the swap hash table structure |
121 | */ |
122 | |
123 | LIST_HEAD(uao_swhash, uao_swhash_elt); |
124 | |
125 | /* |
126 | * uao_swhash_elt_pool: pool of uao_swhash_elt structures. |
127 | * Note: pages for this pool must not come from a pageable kernel map. |
128 | */ |
129 | static struct pool uao_swhash_elt_pool __cacheline_aligned; |
130 | |
131 | /* |
132 | * uvm_aobj: the actual anon-backed uvm_object |
133 | * |
134 | * => the uvm_object is at the top of the structure, this allows |
135 | * (struct uvm_aobj *) == (struct uvm_object *) |
136 | * => only one of u_swslots and u_swhash is used in any given aobj |
137 | */ |
138 | |
139 | struct uvm_aobj { |
140 | struct uvm_object u_obj; /* has: lock, pgops, memq, #pages, #refs */ |
141 | pgoff_t u_pages; /* number of pages in entire object */ |
142 | int u_flags; /* the flags (see uvm_aobj.h) */ |
143 | int *u_swslots; /* array of offset->swapslot mappings */ |
144 | /* |
145 | * hashtable of offset->swapslot mappings |
146 | * (u_swhash is an array of bucket heads) |
147 | */ |
148 | struct uao_swhash *u_swhash; |
149 | u_long u_swhashmask; /* mask for hashtable */ |
150 | LIST_ENTRY(uvm_aobj) u_list; /* global list of aobjs */ |
151 | int u_freelist; /* freelist to allocate pages from */ |
152 | }; |
153 | |
154 | static void uao_free(struct uvm_aobj *); |
155 | static int uao_get(struct uvm_object *, voff_t, struct vm_page **, |
156 | int *, int, vm_prot_t, int, int); |
157 | static int uao_put(struct uvm_object *, voff_t, voff_t, int); |
158 | |
159 | #if defined(VMSWAP) |
160 | static struct uao_swhash_elt *uao_find_swhash_elt |
161 | (struct uvm_aobj *, int, bool); |
162 | |
163 | static bool uao_pagein(struct uvm_aobj *, int, int); |
164 | static bool uao_pagein_page(struct uvm_aobj *, int); |
165 | #endif /* defined(VMSWAP) */ |
166 | |
167 | static struct vm_page *uao_pagealloc(struct uvm_object *, voff_t, int); |
168 | |
169 | /* |
170 | * aobj_pager |
171 | * |
172 | * note that some functions (e.g. put) are handled elsewhere |
173 | */ |
174 | |
175 | const struct uvm_pagerops = { |
176 | .pgo_reference = uao_reference, |
177 | .pgo_detach = uao_detach, |
178 | .pgo_get = uao_get, |
179 | .pgo_put = uao_put, |
180 | }; |
181 | |
182 | /* |
183 | * uao_list: global list of active aobjs, locked by uao_list_lock |
184 | */ |
185 | |
186 | static LIST_HEAD(aobjlist, uvm_aobj) uao_list __cacheline_aligned; |
187 | static kmutex_t uao_list_lock __cacheline_aligned; |
188 | |
189 | /* |
190 | * hash table/array related functions |
191 | */ |
192 | |
193 | #if defined(VMSWAP) |
194 | |
195 | /* |
196 | * uao_find_swhash_elt: find (or create) a hash table entry for a page |
197 | * offset. |
198 | * |
199 | * => the object should be locked by the caller |
200 | */ |
201 | |
202 | static struct uao_swhash_elt * |
203 | uao_find_swhash_elt(struct uvm_aobj *aobj, int pageidx, bool create) |
204 | { |
205 | struct uao_swhash *swhash; |
206 | struct uao_swhash_elt *elt; |
207 | voff_t page_tag; |
208 | |
209 | swhash = UAO_SWHASH_HASH(aobj, pageidx); |
210 | page_tag = UAO_SWHASH_ELT_TAG(pageidx); |
211 | |
212 | /* |
213 | * now search the bucket for the requested tag |
214 | */ |
215 | |
216 | LIST_FOREACH(elt, swhash, list) { |
217 | if (elt->tag == page_tag) { |
218 | return elt; |
219 | } |
220 | } |
221 | if (!create) { |
222 | return NULL; |
223 | } |
224 | |
225 | /* |
226 | * allocate a new entry for the bucket and init/insert it in |
227 | */ |
228 | |
229 | elt = pool_get(&uao_swhash_elt_pool, PR_NOWAIT); |
230 | if (elt == NULL) { |
231 | return NULL; |
232 | } |
233 | LIST_INSERT_HEAD(swhash, elt, list); |
234 | elt->tag = page_tag; |
235 | elt->count = 0; |
236 | memset(elt->slots, 0, sizeof(elt->slots)); |
237 | return elt; |
238 | } |
239 | |
240 | /* |
241 | * uao_find_swslot: find the swap slot number for an aobj/pageidx |
242 | * |
243 | * => object must be locked by caller |
244 | */ |
245 | |
246 | int |
247 | uao_find_swslot(struct uvm_object *uobj, int pageidx) |
248 | { |
249 | struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; |
250 | struct uao_swhash_elt *elt; |
251 | |
252 | /* |
253 | * if noswap flag is set, then we never return a slot |
254 | */ |
255 | |
256 | if (aobj->u_flags & UAO_FLAG_NOSWAP) |
257 | return 0; |
258 | |
259 | /* |
260 | * if hashing, look in hash table. |
261 | */ |
262 | |
263 | if (UAO_USES_SWHASH(aobj)) { |
264 | elt = uao_find_swhash_elt(aobj, pageidx, false); |
265 | return elt ? UAO_SWHASH_ELT_PAGESLOT(elt, pageidx) : 0; |
266 | } |
267 | |
268 | /* |
269 | * otherwise, look in the array |
270 | */ |
271 | |
272 | return aobj->u_swslots[pageidx]; |
273 | } |
274 | |
275 | /* |
276 | * uao_set_swslot: set the swap slot for a page in an aobj. |
277 | * |
278 | * => setting a slot to zero frees the slot |
279 | * => object must be locked by caller |
280 | * => we return the old slot number, or -1 if we failed to allocate |
281 | * memory to record the new slot number |
282 | */ |
283 | |
284 | int |
285 | uao_set_swslot(struct uvm_object *uobj, int pageidx, int slot) |
286 | { |
287 | struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; |
288 | struct uao_swhash_elt *elt; |
289 | int oldslot; |
290 | UVMHIST_FUNC("uao_set_swslot" ); UVMHIST_CALLED(pdhist); |
291 | UVMHIST_LOG(pdhist, "aobj %p pageidx %d slot %d" , |
292 | aobj, pageidx, slot, 0); |
293 | |
294 | KASSERT(mutex_owned(uobj->vmobjlock) || uobj->uo_refs == 0); |
295 | |
296 | /* |
297 | * if noswap flag is set, then we can't set a non-zero slot. |
298 | */ |
299 | |
300 | if (aobj->u_flags & UAO_FLAG_NOSWAP) { |
301 | KASSERTMSG(slot == 0, "uao_set_swslot: no swap object" ); |
302 | return 0; |
303 | } |
304 | |
305 | /* |
306 | * are we using a hash table? if so, add it in the hash. |
307 | */ |
308 | |
309 | if (UAO_USES_SWHASH(aobj)) { |
310 | |
311 | /* |
312 | * Avoid allocating an entry just to free it again if |
313 | * the page had not swap slot in the first place, and |
314 | * we are freeing. |
315 | */ |
316 | |
317 | elt = uao_find_swhash_elt(aobj, pageidx, slot != 0); |
318 | if (elt == NULL) { |
319 | return slot ? -1 : 0; |
320 | } |
321 | |
322 | oldslot = UAO_SWHASH_ELT_PAGESLOT(elt, pageidx); |
323 | UAO_SWHASH_ELT_PAGESLOT(elt, pageidx) = slot; |
324 | |
325 | /* |
326 | * now adjust the elt's reference counter and free it if we've |
327 | * dropped it to zero. |
328 | */ |
329 | |
330 | if (slot) { |
331 | if (oldslot == 0) |
332 | elt->count++; |
333 | } else { |
334 | if (oldslot) |
335 | elt->count--; |
336 | |
337 | if (elt->count == 0) { |
338 | LIST_REMOVE(elt, list); |
339 | pool_put(&uao_swhash_elt_pool, elt); |
340 | } |
341 | } |
342 | } else { |
343 | /* we are using an array */ |
344 | oldslot = aobj->u_swslots[pageidx]; |
345 | aobj->u_swslots[pageidx] = slot; |
346 | } |
347 | return oldslot; |
348 | } |
349 | |
350 | #endif /* defined(VMSWAP) */ |
351 | |
352 | /* |
353 | * end of hash/array functions |
354 | */ |
355 | |
356 | /* |
357 | * uao_free: free all resources held by an aobj, and then free the aobj |
358 | * |
359 | * => the aobj should be dead |
360 | */ |
361 | |
362 | static void |
363 | uao_free(struct uvm_aobj *aobj) |
364 | { |
365 | struct uvm_object *uobj = &aobj->u_obj; |
366 | |
367 | KASSERT(mutex_owned(uobj->vmobjlock)); |
368 | uao_dropswap_range(uobj, 0, 0); |
369 | mutex_exit(uobj->vmobjlock); |
370 | |
371 | #if defined(VMSWAP) |
372 | if (UAO_USES_SWHASH(aobj)) { |
373 | |
374 | /* |
375 | * free the hash table itself. |
376 | */ |
377 | |
378 | hashdone(aobj->u_swhash, HASH_LIST, aobj->u_swhashmask); |
379 | } else { |
380 | |
381 | /* |
382 | * free the array itsself. |
383 | */ |
384 | |
385 | kmem_free(aobj->u_swslots, aobj->u_pages * sizeof(int)); |
386 | } |
387 | #endif /* defined(VMSWAP) */ |
388 | |
389 | /* |
390 | * finally free the aobj itself |
391 | */ |
392 | |
393 | uvm_obj_destroy(uobj, true); |
394 | kmem_free(aobj, sizeof(struct uvm_aobj)); |
395 | } |
396 | |
397 | /* |
398 | * pager functions |
399 | */ |
400 | |
401 | /* |
402 | * uao_create: create an aobj of the given size and return its uvm_object. |
403 | * |
404 | * => for normal use, flags are always zero |
405 | * => for the kernel object, the flags are: |
406 | * UAO_FLAG_KERNOBJ - allocate the kernel object (can only happen once) |
407 | * UAO_FLAG_KERNSWAP - enable swapping of kernel object (" ") |
408 | */ |
409 | |
410 | struct uvm_object * |
411 | uao_create(vsize_t size, int flags) |
412 | { |
413 | static struct uvm_aobj kernel_object_store; |
414 | static kmutex_t kernel_object_lock; |
415 | static int kobj_alloced __diagused = 0; |
416 | pgoff_t pages = round_page(size) >> PAGE_SHIFT; |
417 | struct uvm_aobj *aobj; |
418 | int refs; |
419 | |
420 | /* |
421 | * Allocate a new aobj, unless kernel object is requested. |
422 | */ |
423 | |
424 | if (flags & UAO_FLAG_KERNOBJ) { |
425 | KASSERT(!kobj_alloced); |
426 | aobj = &kernel_object_store; |
427 | aobj->u_pages = pages; |
428 | aobj->u_flags = UAO_FLAG_NOSWAP; |
429 | refs = UVM_OBJ_KERN; |
430 | kobj_alloced = UAO_FLAG_KERNOBJ; |
431 | } else if (flags & UAO_FLAG_KERNSWAP) { |
432 | KASSERT(kobj_alloced == UAO_FLAG_KERNOBJ); |
433 | aobj = &kernel_object_store; |
434 | kobj_alloced = UAO_FLAG_KERNSWAP; |
435 | refs = 0xdeadbeaf; /* XXX: gcc */ |
436 | } else { |
437 | aobj = kmem_alloc(sizeof(struct uvm_aobj), KM_SLEEP); |
438 | aobj->u_pages = pages; |
439 | aobj->u_flags = 0; |
440 | refs = 1; |
441 | } |
442 | |
443 | /* |
444 | * no freelist by default |
445 | */ |
446 | |
447 | aobj->u_freelist = VM_NFREELIST; |
448 | |
449 | /* |
450 | * allocate hash/array if necessary |
451 | * |
452 | * note: in the KERNSWAP case no need to worry about locking since |
453 | * we are still booting we should be the only thread around. |
454 | */ |
455 | |
456 | if (flags == 0 || (flags & UAO_FLAG_KERNSWAP) != 0) { |
457 | #if defined(VMSWAP) |
458 | const int kernswap = (flags & UAO_FLAG_KERNSWAP) != 0; |
459 | |
460 | /* allocate hash table or array depending on object size */ |
461 | if (UAO_USES_SWHASH(aobj)) { |
462 | aobj->u_swhash = hashinit(UAO_SWHASH_BUCKETS(aobj), |
463 | HASH_LIST, kernswap ? false : true, |
464 | &aobj->u_swhashmask); |
465 | if (aobj->u_swhash == NULL) |
466 | panic("uao_create: hashinit swhash failed" ); |
467 | } else { |
468 | aobj->u_swslots = kmem_zalloc(pages * sizeof(int), |
469 | kernswap ? KM_NOSLEEP : KM_SLEEP); |
470 | if (aobj->u_swslots == NULL) |
471 | panic("uao_create: swslots allocation failed" ); |
472 | } |
473 | #endif /* defined(VMSWAP) */ |
474 | |
475 | if (flags) { |
476 | aobj->u_flags &= ~UAO_FLAG_NOSWAP; /* clear noswap */ |
477 | return &aobj->u_obj; |
478 | } |
479 | } |
480 | |
481 | /* |
482 | * Initialise UVM object. |
483 | */ |
484 | |
485 | const bool kernobj = (flags & UAO_FLAG_KERNOBJ) != 0; |
486 | uvm_obj_init(&aobj->u_obj, &aobj_pager, !kernobj, refs); |
487 | if (__predict_false(kernobj)) { |
488 | /* Initialisation only once, for UAO_FLAG_KERNOBJ. */ |
489 | mutex_init(&kernel_object_lock, MUTEX_DEFAULT, IPL_NONE); |
490 | uvm_obj_setlock(&aobj->u_obj, &kernel_object_lock); |
491 | } |
492 | |
493 | /* |
494 | * now that aobj is ready, add it to the global list |
495 | */ |
496 | |
497 | mutex_enter(&uao_list_lock); |
498 | LIST_INSERT_HEAD(&uao_list, aobj, u_list); |
499 | mutex_exit(&uao_list_lock); |
500 | return(&aobj->u_obj); |
501 | } |
502 | |
503 | /* |
504 | * uao_set_pgfl: allocate pages only from the specified freelist. |
505 | * |
506 | * => must be called before any pages are allocated for the object. |
507 | * => reset by setting it to VM_NFREELIST, meaning any freelist. |
508 | */ |
509 | |
510 | void |
511 | uao_set_pgfl(struct uvm_object *uobj, int freelist) |
512 | { |
513 | struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; |
514 | |
515 | KASSERTMSG((0 <= freelist), "invalid freelist %d" , freelist); |
516 | KASSERTMSG((freelist <= VM_NFREELIST), "invalid freelist %d" , |
517 | freelist); |
518 | |
519 | aobj->u_freelist = freelist; |
520 | } |
521 | |
522 | /* |
523 | * uao_pagealloc: allocate a page for aobj. |
524 | */ |
525 | |
526 | static inline struct vm_page * |
527 | uao_pagealloc(struct uvm_object *uobj, voff_t offset, int flags) |
528 | { |
529 | struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; |
530 | |
531 | if (__predict_true(aobj->u_freelist == VM_NFREELIST)) |
532 | return uvm_pagealloc(uobj, offset, NULL, flags); |
533 | else |
534 | return uvm_pagealloc_strat(uobj, offset, NULL, flags, |
535 | UVM_PGA_STRAT_ONLY, aobj->u_freelist); |
536 | } |
537 | |
538 | /* |
539 | * uao_init: set up aobj pager subsystem |
540 | * |
541 | * => called at boot time from uvm_pager_init() |
542 | */ |
543 | |
544 | void |
545 | uao_init(void) |
546 | { |
547 | static int uao_initialized; |
548 | |
549 | if (uao_initialized) |
550 | return; |
551 | uao_initialized = true; |
552 | LIST_INIT(&uao_list); |
553 | mutex_init(&uao_list_lock, MUTEX_DEFAULT, IPL_NONE); |
554 | pool_init(&uao_swhash_elt_pool, sizeof(struct uao_swhash_elt), |
555 | 0, 0, 0, "uaoeltpl" , NULL, IPL_VM); |
556 | } |
557 | |
558 | /* |
559 | * uao_reference: hold a reference to an anonymous UVM object. |
560 | */ |
561 | void |
562 | uao_reference(struct uvm_object *uobj) |
563 | { |
564 | /* Kernel object is persistent. */ |
565 | if (UVM_OBJ_IS_KERN_OBJECT(uobj)) { |
566 | return; |
567 | } |
568 | atomic_inc_uint(&uobj->uo_refs); |
569 | } |
570 | |
571 | /* |
572 | * uao_detach: drop a reference to an anonymous UVM object. |
573 | */ |
574 | void |
575 | uao_detach(struct uvm_object *uobj) |
576 | { |
577 | struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; |
578 | struct vm_page *pg; |
579 | |
580 | UVMHIST_FUNC("uao_detach" ); UVMHIST_CALLED(maphist); |
581 | |
582 | /* |
583 | * Detaching from kernel object is a NOP. |
584 | */ |
585 | |
586 | if (UVM_OBJ_IS_KERN_OBJECT(uobj)) |
587 | return; |
588 | |
589 | /* |
590 | * Drop the reference. If it was the last one, destroy the object. |
591 | */ |
592 | |
593 | UVMHIST_LOG(maphist," (uobj=0x%x) ref=%d" , uobj,uobj->uo_refs,0,0); |
594 | if (atomic_dec_uint_nv(&uobj->uo_refs) > 0) { |
595 | UVMHIST_LOG(maphist, "<- done (rc>0)" , 0,0,0,0); |
596 | return; |
597 | } |
598 | |
599 | /* |
600 | * Remove the aobj from the global list. |
601 | */ |
602 | |
603 | mutex_enter(&uao_list_lock); |
604 | LIST_REMOVE(aobj, u_list); |
605 | mutex_exit(&uao_list_lock); |
606 | |
607 | /* |
608 | * Free all the pages left in the aobj. For each page, when the |
609 | * page is no longer busy (and thus after any disk I/O that it is |
610 | * involved in is complete), release any swap resources and free |
611 | * the page itself. |
612 | */ |
613 | |
614 | mutex_enter(uobj->vmobjlock); |
615 | mutex_enter(&uvm_pageqlock); |
616 | while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL) { |
617 | pmap_page_protect(pg, VM_PROT_NONE); |
618 | if (pg->flags & PG_BUSY) { |
619 | pg->flags |= PG_WANTED; |
620 | mutex_exit(&uvm_pageqlock); |
621 | UVM_UNLOCK_AND_WAIT(pg, uobj->vmobjlock, false, |
622 | "uao_det" , 0); |
623 | mutex_enter(uobj->vmobjlock); |
624 | mutex_enter(&uvm_pageqlock); |
625 | continue; |
626 | } |
627 | uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT); |
628 | uvm_pagefree(pg); |
629 | } |
630 | mutex_exit(&uvm_pageqlock); |
631 | |
632 | /* |
633 | * Finally, free the anonymous UVM object itself. |
634 | */ |
635 | |
636 | uao_free(aobj); |
637 | } |
638 | |
639 | /* |
640 | * uao_put: flush pages out of a uvm object |
641 | * |
642 | * => object should be locked by caller. we may _unlock_ the object |
643 | * if (and only if) we need to clean a page (PGO_CLEANIT). |
644 | * XXXJRT Currently, however, we don't. In the case of cleaning |
645 | * XXXJRT a page, we simply just deactivate it. Should probably |
646 | * XXXJRT handle this better, in the future (although "flushing" |
647 | * XXXJRT anonymous memory isn't terribly important). |
648 | * => if PGO_CLEANIT is not set, then we will neither unlock the object |
649 | * or block. |
650 | * => if PGO_ALLPAGE is set, then all pages in the object are valid targets |
651 | * for flushing. |
652 | * => NOTE: we rely on the fact that the object's memq is a TAILQ and |
653 | * that new pages are inserted on the tail end of the list. thus, |
654 | * we can make a complete pass through the object in one go by starting |
655 | * at the head and working towards the tail (new pages are put in |
656 | * front of us). |
657 | * => NOTE: we are allowed to lock the page queues, so the caller |
658 | * must not be holding the lock on them [e.g. pagedaemon had |
659 | * better not call us with the queues locked] |
660 | * => we return 0 unless we encountered some sort of I/O error |
661 | * XXXJRT currently never happens, as we never directly initiate |
662 | * XXXJRT I/O |
663 | * |
664 | * note on page traversal: |
665 | * we can traverse the pages in an object either by going down the |
666 | * linked list in "uobj->memq", or we can go over the address range |
667 | * by page doing hash table lookups for each address. depending |
668 | * on how many pages are in the object it may be cheaper to do one |
669 | * or the other. we set "by_list" to true if we are using memq. |
670 | * if the cost of a hash lookup was equal to the cost of the list |
671 | * traversal we could compare the number of pages in the start->stop |
672 | * range to the total number of pages in the object. however, it |
673 | * seems that a hash table lookup is more expensive than the linked |
674 | * list traversal, so we multiply the number of pages in the |
675 | * start->stop range by a penalty which we define below. |
676 | */ |
677 | |
678 | static int |
679 | uao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags) |
680 | { |
681 | struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; |
682 | struct vm_page *pg, *nextpg, curmp, endmp; |
683 | bool by_list; |
684 | voff_t curoff; |
685 | UVMHIST_FUNC("uao_put" ); UVMHIST_CALLED(maphist); |
686 | |
687 | KASSERT(mutex_owned(uobj->vmobjlock)); |
688 | |
689 | curoff = 0; |
690 | if (flags & PGO_ALLPAGES) { |
691 | start = 0; |
692 | stop = aobj->u_pages << PAGE_SHIFT; |
693 | by_list = true; /* always go by the list */ |
694 | } else { |
695 | start = trunc_page(start); |
696 | if (stop == 0) { |
697 | stop = aobj->u_pages << PAGE_SHIFT; |
698 | } else { |
699 | stop = round_page(stop); |
700 | } |
701 | if (stop > (aobj->u_pages << PAGE_SHIFT)) { |
702 | printf("uao_flush: strange, got an out of range " |
703 | "flush (fixed)\n" ); |
704 | stop = aobj->u_pages << PAGE_SHIFT; |
705 | } |
706 | by_list = (uobj->uo_npages <= |
707 | ((stop - start) >> PAGE_SHIFT) * UVM_PAGE_TREE_PENALTY); |
708 | } |
709 | UVMHIST_LOG(maphist, |
710 | " flush start=0x%lx, stop=0x%x, by_list=%d, flags=0x%x" , |
711 | start, stop, by_list, flags); |
712 | |
713 | /* |
714 | * Don't need to do any work here if we're not freeing |
715 | * or deactivating pages. |
716 | */ |
717 | |
718 | if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) { |
719 | mutex_exit(uobj->vmobjlock); |
720 | return 0; |
721 | } |
722 | |
723 | /* |
724 | * Initialize the marker pages. See the comment in |
725 | * genfs_putpages() also. |
726 | */ |
727 | |
728 | curmp.flags = PG_MARKER; |
729 | endmp.flags = PG_MARKER; |
730 | |
731 | /* |
732 | * now do it. note: we must update nextpg in the body of loop or we |
733 | * will get stuck. we need to use nextpg if we'll traverse the list |
734 | * because we may free "pg" before doing the next loop. |
735 | */ |
736 | |
737 | if (by_list) { |
738 | TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq.queue); |
739 | nextpg = TAILQ_FIRST(&uobj->memq); |
740 | } else { |
741 | curoff = start; |
742 | nextpg = NULL; /* Quell compiler warning */ |
743 | } |
744 | |
745 | /* locked: uobj */ |
746 | for (;;) { |
747 | if (by_list) { |
748 | pg = nextpg; |
749 | if (pg == &endmp) |
750 | break; |
751 | nextpg = TAILQ_NEXT(pg, listq.queue); |
752 | if (pg->flags & PG_MARKER) |
753 | continue; |
754 | if (pg->offset < start || pg->offset >= stop) |
755 | continue; |
756 | } else { |
757 | if (curoff < stop) { |
758 | pg = uvm_pagelookup(uobj, curoff); |
759 | curoff += PAGE_SIZE; |
760 | } else |
761 | break; |
762 | if (pg == NULL) |
763 | continue; |
764 | } |
765 | |
766 | /* |
767 | * wait and try again if the page is busy. |
768 | */ |
769 | |
770 | if (pg->flags & PG_BUSY) { |
771 | if (by_list) { |
772 | TAILQ_INSERT_BEFORE(pg, &curmp, listq.queue); |
773 | } |
774 | pg->flags |= PG_WANTED; |
775 | UVM_UNLOCK_AND_WAIT(pg, uobj->vmobjlock, 0, |
776 | "uao_put" , 0); |
777 | mutex_enter(uobj->vmobjlock); |
778 | if (by_list) { |
779 | nextpg = TAILQ_NEXT(&curmp, listq.queue); |
780 | TAILQ_REMOVE(&uobj->memq, &curmp, |
781 | listq.queue); |
782 | } else |
783 | curoff -= PAGE_SIZE; |
784 | continue; |
785 | } |
786 | |
787 | switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) { |
788 | |
789 | /* |
790 | * XXX In these first 3 cases, we always just |
791 | * XXX deactivate the page. We may want to |
792 | * XXX handle the different cases more specifically |
793 | * XXX in the future. |
794 | */ |
795 | |
796 | case PGO_CLEANIT|PGO_FREE: |
797 | case PGO_CLEANIT|PGO_DEACTIVATE: |
798 | case PGO_DEACTIVATE: |
799 | deactivate_it: |
800 | mutex_enter(&uvm_pageqlock); |
801 | /* skip the page if it's wired */ |
802 | if (pg->wire_count == 0) { |
803 | uvm_pagedeactivate(pg); |
804 | } |
805 | mutex_exit(&uvm_pageqlock); |
806 | break; |
807 | |
808 | case PGO_FREE: |
809 | /* |
810 | * If there are multiple references to |
811 | * the object, just deactivate the page. |
812 | */ |
813 | |
814 | if (uobj->uo_refs > 1) |
815 | goto deactivate_it; |
816 | |
817 | /* |
818 | * free the swap slot and the page. |
819 | */ |
820 | |
821 | pmap_page_protect(pg, VM_PROT_NONE); |
822 | |
823 | /* |
824 | * freeing swapslot here is not strictly necessary. |
825 | * however, leaving it here doesn't save much |
826 | * because we need to update swap accounting anyway. |
827 | */ |
828 | |
829 | uao_dropswap(uobj, pg->offset >> PAGE_SHIFT); |
830 | mutex_enter(&uvm_pageqlock); |
831 | uvm_pagefree(pg); |
832 | mutex_exit(&uvm_pageqlock); |
833 | break; |
834 | |
835 | default: |
836 | panic("%s: impossible" , __func__); |
837 | } |
838 | } |
839 | if (by_list) { |
840 | TAILQ_REMOVE(&uobj->memq, &endmp, listq.queue); |
841 | } |
842 | mutex_exit(uobj->vmobjlock); |
843 | return 0; |
844 | } |
845 | |
846 | /* |
847 | * uao_get: fetch me a page |
848 | * |
849 | * we have three cases: |
850 | * 1: page is resident -> just return the page. |
851 | * 2: page is zero-fill -> allocate a new page and zero it. |
852 | * 3: page is swapped out -> fetch the page from swap. |
853 | * |
854 | * cases 1 and 2 can be handled with PGO_LOCKED, case 3 cannot. |
855 | * so, if the "center" page hits case 3 (or any page, with PGO_ALLPAGES), |
856 | * then we will need to return EBUSY. |
857 | * |
858 | * => prefer map unlocked (not required) |
859 | * => object must be locked! we will _unlock_ it before starting any I/O. |
860 | * => flags: PGO_ALLPAGES: get all of the pages |
861 | * PGO_LOCKED: fault data structures are locked |
862 | * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx] |
863 | * => NOTE: caller must check for released pages!! |
864 | */ |
865 | |
866 | static int |
867 | uao_get(struct uvm_object *uobj, voff_t offset, struct vm_page **pps, |
868 | int *npagesp, int centeridx, vm_prot_t access_type, int advice, int flags) |
869 | { |
870 | voff_t current_offset; |
871 | struct vm_page *ptmp = NULL; /* Quell compiler warning */ |
872 | int lcv, gotpages, maxpages, swslot, pageidx; |
873 | bool done; |
874 | UVMHIST_FUNC("uao_get" ); UVMHIST_CALLED(pdhist); |
875 | |
876 | UVMHIST_LOG(pdhist, "aobj=%p offset=%d, flags=%d" , |
877 | (struct uvm_aobj *)uobj, offset, flags,0); |
878 | |
879 | /* |
880 | * get number of pages |
881 | */ |
882 | |
883 | maxpages = *npagesp; |
884 | |
885 | /* |
886 | * step 1: handled the case where fault data structures are locked. |
887 | */ |
888 | |
889 | if (flags & PGO_LOCKED) { |
890 | |
891 | /* |
892 | * step 1a: get pages that are already resident. only do |
893 | * this if the data structures are locked (i.e. the first |
894 | * time through). |
895 | */ |
896 | |
897 | done = true; /* be optimistic */ |
898 | gotpages = 0; /* # of pages we got so far */ |
899 | for (lcv = 0, current_offset = offset ; lcv < maxpages ; |
900 | lcv++, current_offset += PAGE_SIZE) { |
901 | /* do we care about this page? if not, skip it */ |
902 | if (pps[lcv] == PGO_DONTCARE) |
903 | continue; |
904 | ptmp = uvm_pagelookup(uobj, current_offset); |
905 | |
906 | /* |
907 | * if page is new, attempt to allocate the page, |
908 | * zero-fill'd. |
909 | */ |
910 | |
911 | if (ptmp == NULL && uao_find_swslot(uobj, |
912 | current_offset >> PAGE_SHIFT) == 0) { |
913 | ptmp = uao_pagealloc(uobj, current_offset, |
914 | UVM_FLAG_COLORMATCH|UVM_PGA_ZERO); |
915 | if (ptmp) { |
916 | /* new page */ |
917 | ptmp->flags &= ~(PG_FAKE); |
918 | ptmp->pqflags |= PQ_AOBJ; |
919 | goto gotpage; |
920 | } |
921 | } |
922 | |
923 | /* |
924 | * to be useful must get a non-busy page |
925 | */ |
926 | |
927 | if (ptmp == NULL || (ptmp->flags & PG_BUSY) != 0) { |
928 | if (lcv == centeridx || |
929 | (flags & PGO_ALLPAGES) != 0) |
930 | /* need to do a wait or I/O! */ |
931 | done = false; |
932 | continue; |
933 | } |
934 | |
935 | /* |
936 | * useful page: busy/lock it and plug it in our |
937 | * result array |
938 | */ |
939 | |
940 | /* caller must un-busy this page */ |
941 | ptmp->flags |= PG_BUSY; |
942 | UVM_PAGE_OWN(ptmp, "uao_get1" ); |
943 | gotpage: |
944 | pps[lcv] = ptmp; |
945 | gotpages++; |
946 | } |
947 | |
948 | /* |
949 | * step 1b: now we've either done everything needed or we |
950 | * to unlock and do some waiting or I/O. |
951 | */ |
952 | |
953 | UVMHIST_LOG(pdhist, "<- done (done=%d)" , done, 0,0,0); |
954 | *npagesp = gotpages; |
955 | if (done) |
956 | return 0; |
957 | else |
958 | return EBUSY; |
959 | } |
960 | |
961 | /* |
962 | * step 2: get non-resident or busy pages. |
963 | * object is locked. data structures are unlocked. |
964 | */ |
965 | |
966 | if ((flags & PGO_SYNCIO) == 0) { |
967 | goto done; |
968 | } |
969 | |
970 | for (lcv = 0, current_offset = offset ; lcv < maxpages ; |
971 | lcv++, current_offset += PAGE_SIZE) { |
972 | |
973 | /* |
974 | * - skip over pages we've already gotten or don't want |
975 | * - skip over pages we don't _have_ to get |
976 | */ |
977 | |
978 | if (pps[lcv] != NULL || |
979 | (lcv != centeridx && (flags & PGO_ALLPAGES) == 0)) |
980 | continue; |
981 | |
982 | pageidx = current_offset >> PAGE_SHIFT; |
983 | |
984 | /* |
985 | * we have yet to locate the current page (pps[lcv]). we |
986 | * first look for a page that is already at the current offset. |
987 | * if we find a page, we check to see if it is busy or |
988 | * released. if that is the case, then we sleep on the page |
989 | * until it is no longer busy or released and repeat the lookup. |
990 | * if the page we found is neither busy nor released, then we |
991 | * busy it (so we own it) and plug it into pps[lcv]. this |
992 | * 'break's the following while loop and indicates we are |
993 | * ready to move on to the next page in the "lcv" loop above. |
994 | * |
995 | * if we exit the while loop with pps[lcv] still set to NULL, |
996 | * then it means that we allocated a new busy/fake/clean page |
997 | * ptmp in the object and we need to do I/O to fill in the data. |
998 | */ |
999 | |
1000 | /* top of "pps" while loop */ |
1001 | while (pps[lcv] == NULL) { |
1002 | /* look for a resident page */ |
1003 | ptmp = uvm_pagelookup(uobj, current_offset); |
1004 | |
1005 | /* not resident? allocate one now (if we can) */ |
1006 | if (ptmp == NULL) { |
1007 | |
1008 | ptmp = uao_pagealloc(uobj, current_offset, 0); |
1009 | |
1010 | /* out of RAM? */ |
1011 | if (ptmp == NULL) { |
1012 | mutex_exit(uobj->vmobjlock); |
1013 | UVMHIST_LOG(pdhist, |
1014 | "sleeping, ptmp == NULL\n" ,0,0,0,0); |
1015 | uvm_wait("uao_getpage" ); |
1016 | mutex_enter(uobj->vmobjlock); |
1017 | continue; |
1018 | } |
1019 | |
1020 | /* |
1021 | * safe with PQ's unlocked: because we just |
1022 | * alloc'd the page |
1023 | */ |
1024 | |
1025 | ptmp->pqflags |= PQ_AOBJ; |
1026 | |
1027 | /* |
1028 | * got new page ready for I/O. break pps while |
1029 | * loop. pps[lcv] is still NULL. |
1030 | */ |
1031 | |
1032 | break; |
1033 | } |
1034 | |
1035 | /* page is there, see if we need to wait on it */ |
1036 | if ((ptmp->flags & PG_BUSY) != 0) { |
1037 | ptmp->flags |= PG_WANTED; |
1038 | UVMHIST_LOG(pdhist, |
1039 | "sleeping, ptmp->flags 0x%x\n" , |
1040 | ptmp->flags,0,0,0); |
1041 | UVM_UNLOCK_AND_WAIT(ptmp, uobj->vmobjlock, |
1042 | false, "uao_get" , 0); |
1043 | mutex_enter(uobj->vmobjlock); |
1044 | continue; |
1045 | } |
1046 | |
1047 | /* |
1048 | * if we get here then the page has become resident and |
1049 | * unbusy between steps 1 and 2. we busy it now (so we |
1050 | * own it) and set pps[lcv] (so that we exit the while |
1051 | * loop). |
1052 | */ |
1053 | |
1054 | /* we own it, caller must un-busy */ |
1055 | ptmp->flags |= PG_BUSY; |
1056 | UVM_PAGE_OWN(ptmp, "uao_get2" ); |
1057 | pps[lcv] = ptmp; |
1058 | } |
1059 | |
1060 | /* |
1061 | * if we own the valid page at the correct offset, pps[lcv] will |
1062 | * point to it. nothing more to do except go to the next page. |
1063 | */ |
1064 | |
1065 | if (pps[lcv]) |
1066 | continue; /* next lcv */ |
1067 | |
1068 | /* |
1069 | * we have a "fake/busy/clean" page that we just allocated. |
1070 | * do the needed "i/o", either reading from swap or zeroing. |
1071 | */ |
1072 | |
1073 | swslot = uao_find_swslot(uobj, pageidx); |
1074 | |
1075 | /* |
1076 | * just zero the page if there's nothing in swap. |
1077 | */ |
1078 | |
1079 | if (swslot == 0) { |
1080 | |
1081 | /* |
1082 | * page hasn't existed before, just zero it. |
1083 | */ |
1084 | |
1085 | uvm_pagezero(ptmp); |
1086 | } else { |
1087 | #if defined(VMSWAP) |
1088 | int error; |
1089 | |
1090 | UVMHIST_LOG(pdhist, "pagein from swslot %d" , |
1091 | swslot, 0,0,0); |
1092 | |
1093 | /* |
1094 | * page in the swapped-out page. |
1095 | * unlock object for i/o, relock when done. |
1096 | */ |
1097 | |
1098 | mutex_exit(uobj->vmobjlock); |
1099 | error = uvm_swap_get(ptmp, swslot, PGO_SYNCIO); |
1100 | mutex_enter(uobj->vmobjlock); |
1101 | |
1102 | /* |
1103 | * I/O done. check for errors. |
1104 | */ |
1105 | |
1106 | if (error != 0) { |
1107 | UVMHIST_LOG(pdhist, "<- done (error=%d)" , |
1108 | error,0,0,0); |
1109 | if (ptmp->flags & PG_WANTED) |
1110 | wakeup(ptmp); |
1111 | |
1112 | /* |
1113 | * remove the swap slot from the aobj |
1114 | * and mark the aobj as having no real slot. |
1115 | * don't free the swap slot, thus preventing |
1116 | * it from being used again. |
1117 | */ |
1118 | |
1119 | swslot = uao_set_swslot(uobj, pageidx, |
1120 | SWSLOT_BAD); |
1121 | if (swslot > 0) { |
1122 | uvm_swap_markbad(swslot, 1); |
1123 | } |
1124 | |
1125 | mutex_enter(&uvm_pageqlock); |
1126 | uvm_pagefree(ptmp); |
1127 | mutex_exit(&uvm_pageqlock); |
1128 | mutex_exit(uobj->vmobjlock); |
1129 | return error; |
1130 | } |
1131 | #else /* defined(VMSWAP) */ |
1132 | panic("%s: pagein" , __func__); |
1133 | #endif /* defined(VMSWAP) */ |
1134 | } |
1135 | |
1136 | if ((access_type & VM_PROT_WRITE) == 0) { |
1137 | ptmp->flags |= PG_CLEAN; |
1138 | pmap_clear_modify(ptmp); |
1139 | } |
1140 | |
1141 | /* |
1142 | * we got the page! clear the fake flag (indicates valid |
1143 | * data now in page) and plug into our result array. note |
1144 | * that page is still busy. |
1145 | * |
1146 | * it is the callers job to: |
1147 | * => check if the page is released |
1148 | * => unbusy the page |
1149 | * => activate the page |
1150 | */ |
1151 | |
1152 | ptmp->flags &= ~PG_FAKE; |
1153 | pps[lcv] = ptmp; |
1154 | } |
1155 | |
1156 | /* |
1157 | * finally, unlock object and return. |
1158 | */ |
1159 | |
1160 | done: |
1161 | mutex_exit(uobj->vmobjlock); |
1162 | UVMHIST_LOG(pdhist, "<- done (OK)" ,0,0,0,0); |
1163 | return 0; |
1164 | } |
1165 | |
1166 | #if defined(VMSWAP) |
1167 | |
1168 | /* |
1169 | * uao_dropswap: release any swap resources from this aobj page. |
1170 | * |
1171 | * => aobj must be locked or have a reference count of 0. |
1172 | */ |
1173 | |
1174 | void |
1175 | uao_dropswap(struct uvm_object *uobj, int pageidx) |
1176 | { |
1177 | int slot; |
1178 | |
1179 | slot = uao_set_swslot(uobj, pageidx, 0); |
1180 | if (slot) { |
1181 | uvm_swap_free(slot, 1); |
1182 | } |
1183 | } |
1184 | |
1185 | /* |
1186 | * page in every page in every aobj that is paged-out to a range of swslots. |
1187 | * |
1188 | * => nothing should be locked. |
1189 | * => returns true if pagein was aborted due to lack of memory. |
1190 | */ |
1191 | |
1192 | bool |
1193 | uao_swap_off(int startslot, int endslot) |
1194 | { |
1195 | struct uvm_aobj *aobj; |
1196 | |
1197 | /* |
1198 | * Walk the list of all anonymous UVM objects. Grab the first. |
1199 | */ |
1200 | mutex_enter(&uao_list_lock); |
1201 | if ((aobj = LIST_FIRST(&uao_list)) == NULL) { |
1202 | mutex_exit(&uao_list_lock); |
1203 | return false; |
1204 | } |
1205 | uao_reference(&aobj->u_obj); |
1206 | |
1207 | do { |
1208 | struct uvm_aobj *nextaobj; |
1209 | bool rv; |
1210 | |
1211 | /* |
1212 | * Prefetch the next object and immediately hold a reference |
1213 | * on it, so neither the current nor the next entry could |
1214 | * disappear while we are iterating. |
1215 | */ |
1216 | if ((nextaobj = LIST_NEXT(aobj, u_list)) != NULL) { |
1217 | uao_reference(&nextaobj->u_obj); |
1218 | } |
1219 | mutex_exit(&uao_list_lock); |
1220 | |
1221 | /* |
1222 | * Page in all pages in the swap slot range. |
1223 | */ |
1224 | mutex_enter(aobj->u_obj.vmobjlock); |
1225 | rv = uao_pagein(aobj, startslot, endslot); |
1226 | mutex_exit(aobj->u_obj.vmobjlock); |
1227 | |
1228 | /* Drop the reference of the current object. */ |
1229 | uao_detach(&aobj->u_obj); |
1230 | if (rv) { |
1231 | if (nextaobj) { |
1232 | uao_detach(&nextaobj->u_obj); |
1233 | } |
1234 | return rv; |
1235 | } |
1236 | |
1237 | aobj = nextaobj; |
1238 | mutex_enter(&uao_list_lock); |
1239 | } while (aobj); |
1240 | |
1241 | mutex_exit(&uao_list_lock); |
1242 | return false; |
1243 | } |
1244 | |
1245 | /* |
1246 | * page in any pages from aobj in the given range. |
1247 | * |
1248 | * => aobj must be locked and is returned locked. |
1249 | * => returns true if pagein was aborted due to lack of memory. |
1250 | */ |
1251 | static bool |
1252 | uao_pagein(struct uvm_aobj *aobj, int startslot, int endslot) |
1253 | { |
1254 | bool rv; |
1255 | |
1256 | if (UAO_USES_SWHASH(aobj)) { |
1257 | struct uao_swhash_elt *elt; |
1258 | int buck; |
1259 | |
1260 | restart: |
1261 | for (buck = aobj->u_swhashmask; buck >= 0; buck--) { |
1262 | for (elt = LIST_FIRST(&aobj->u_swhash[buck]); |
1263 | elt != NULL; |
1264 | elt = LIST_NEXT(elt, list)) { |
1265 | int i; |
1266 | |
1267 | for (i = 0; i < UAO_SWHASH_CLUSTER_SIZE; i++) { |
1268 | int slot = elt->slots[i]; |
1269 | |
1270 | /* |
1271 | * if the slot isn't in range, skip it. |
1272 | */ |
1273 | |
1274 | if (slot < startslot || |
1275 | slot >= endslot) { |
1276 | continue; |
1277 | } |
1278 | |
1279 | /* |
1280 | * process the page, |
1281 | * the start over on this object |
1282 | * since the swhash elt |
1283 | * may have been freed. |
1284 | */ |
1285 | |
1286 | rv = uao_pagein_page(aobj, |
1287 | UAO_SWHASH_ELT_PAGEIDX_BASE(elt) + i); |
1288 | if (rv) { |
1289 | return rv; |
1290 | } |
1291 | goto restart; |
1292 | } |
1293 | } |
1294 | } |
1295 | } else { |
1296 | int i; |
1297 | |
1298 | for (i = 0; i < aobj->u_pages; i++) { |
1299 | int slot = aobj->u_swslots[i]; |
1300 | |
1301 | /* |
1302 | * if the slot isn't in range, skip it |
1303 | */ |
1304 | |
1305 | if (slot < startslot || slot >= endslot) { |
1306 | continue; |
1307 | } |
1308 | |
1309 | /* |
1310 | * process the page. |
1311 | */ |
1312 | |
1313 | rv = uao_pagein_page(aobj, i); |
1314 | if (rv) { |
1315 | return rv; |
1316 | } |
1317 | } |
1318 | } |
1319 | |
1320 | return false; |
1321 | } |
1322 | |
1323 | /* |
1324 | * uao_pagein_page: page in a single page from an anonymous UVM object. |
1325 | * |
1326 | * => Returns true if pagein was aborted due to lack of memory. |
1327 | * => Object must be locked and is returned locked. |
1328 | */ |
1329 | |
1330 | static bool |
1331 | uao_pagein_page(struct uvm_aobj *aobj, int pageidx) |
1332 | { |
1333 | struct uvm_object *uobj = &aobj->u_obj; |
1334 | struct vm_page *pg; |
1335 | int rv, npages; |
1336 | |
1337 | pg = NULL; |
1338 | npages = 1; |
1339 | |
1340 | KASSERT(mutex_owned(uobj->vmobjlock)); |
1341 | rv = uao_get(uobj, pageidx << PAGE_SHIFT, &pg, &npages, |
1342 | 0, VM_PROT_READ | VM_PROT_WRITE, 0, PGO_SYNCIO); |
1343 | |
1344 | /* |
1345 | * relock and finish up. |
1346 | */ |
1347 | |
1348 | mutex_enter(uobj->vmobjlock); |
1349 | switch (rv) { |
1350 | case 0: |
1351 | break; |
1352 | |
1353 | case EIO: |
1354 | case ERESTART: |
1355 | |
1356 | /* |
1357 | * nothing more to do on errors. |
1358 | * ERESTART can only mean that the anon was freed, |
1359 | * so again there's nothing to do. |
1360 | */ |
1361 | |
1362 | return false; |
1363 | |
1364 | default: |
1365 | return true; |
1366 | } |
1367 | |
1368 | /* |
1369 | * ok, we've got the page now. |
1370 | * mark it as dirty, clear its swslot and un-busy it. |
1371 | */ |
1372 | uao_dropswap(&aobj->u_obj, pageidx); |
1373 | |
1374 | /* |
1375 | * make sure it's on a page queue. |
1376 | */ |
1377 | mutex_enter(&uvm_pageqlock); |
1378 | if (pg->wire_count == 0) |
1379 | uvm_pageenqueue(pg); |
1380 | mutex_exit(&uvm_pageqlock); |
1381 | |
1382 | if (pg->flags & PG_WANTED) { |
1383 | wakeup(pg); |
1384 | } |
1385 | pg->flags &= ~(PG_WANTED|PG_BUSY|PG_CLEAN|PG_FAKE); |
1386 | UVM_PAGE_OWN(pg, NULL); |
1387 | |
1388 | return false; |
1389 | } |
1390 | |
1391 | /* |
1392 | * uao_dropswap_range: drop swapslots in the range. |
1393 | * |
1394 | * => aobj must be locked and is returned locked. |
1395 | * => start is inclusive. end is exclusive. |
1396 | */ |
1397 | |
1398 | void |
1399 | uao_dropswap_range(struct uvm_object *uobj, voff_t start, voff_t end) |
1400 | { |
1401 | struct uvm_aobj *aobj = (struct uvm_aobj *)uobj; |
1402 | int swpgonlydelta = 0; |
1403 | |
1404 | KASSERT(mutex_owned(uobj->vmobjlock)); |
1405 | |
1406 | if (end == 0) { |
1407 | end = INT64_MAX; |
1408 | } |
1409 | |
1410 | if (UAO_USES_SWHASH(aobj)) { |
1411 | int i, hashbuckets = aobj->u_swhashmask + 1; |
1412 | voff_t taghi; |
1413 | voff_t taglo; |
1414 | |
1415 | taglo = UAO_SWHASH_ELT_TAG(start); |
1416 | taghi = UAO_SWHASH_ELT_TAG(end); |
1417 | |
1418 | for (i = 0; i < hashbuckets; i++) { |
1419 | struct uao_swhash_elt *elt, *next; |
1420 | |
1421 | for (elt = LIST_FIRST(&aobj->u_swhash[i]); |
1422 | elt != NULL; |
1423 | elt = next) { |
1424 | int startidx, endidx; |
1425 | int j; |
1426 | |
1427 | next = LIST_NEXT(elt, list); |
1428 | |
1429 | if (elt->tag < taglo || taghi < elt->tag) { |
1430 | continue; |
1431 | } |
1432 | |
1433 | if (elt->tag == taglo) { |
1434 | startidx = |
1435 | UAO_SWHASH_ELT_PAGESLOT_IDX(start); |
1436 | } else { |
1437 | startidx = 0; |
1438 | } |
1439 | |
1440 | if (elt->tag == taghi) { |
1441 | endidx = |
1442 | UAO_SWHASH_ELT_PAGESLOT_IDX(end); |
1443 | } else { |
1444 | endidx = UAO_SWHASH_CLUSTER_SIZE; |
1445 | } |
1446 | |
1447 | for (j = startidx; j < endidx; j++) { |
1448 | int slot = elt->slots[j]; |
1449 | |
1450 | KASSERT(uvm_pagelookup(&aobj->u_obj, |
1451 | (UAO_SWHASH_ELT_PAGEIDX_BASE(elt) |
1452 | + j) << PAGE_SHIFT) == NULL); |
1453 | if (slot > 0) { |
1454 | uvm_swap_free(slot, 1); |
1455 | swpgonlydelta++; |
1456 | KASSERT(elt->count > 0); |
1457 | elt->slots[j] = 0; |
1458 | elt->count--; |
1459 | } |
1460 | } |
1461 | |
1462 | if (elt->count == 0) { |
1463 | LIST_REMOVE(elt, list); |
1464 | pool_put(&uao_swhash_elt_pool, elt); |
1465 | } |
1466 | } |
1467 | } |
1468 | } else { |
1469 | int i; |
1470 | |
1471 | if (aobj->u_pages < end) { |
1472 | end = aobj->u_pages; |
1473 | } |
1474 | for (i = start; i < end; i++) { |
1475 | int slot = aobj->u_swslots[i]; |
1476 | |
1477 | if (slot > 0) { |
1478 | uvm_swap_free(slot, 1); |
1479 | swpgonlydelta++; |
1480 | } |
1481 | } |
1482 | } |
1483 | |
1484 | /* |
1485 | * adjust the counter of pages only in swap for all |
1486 | * the swap slots we've freed. |
1487 | */ |
1488 | |
1489 | if (swpgonlydelta > 0) { |
1490 | mutex_enter(&uvm_swap_data_lock); |
1491 | KASSERT(uvmexp.swpgonly >= swpgonlydelta); |
1492 | uvmexp.swpgonly -= swpgonlydelta; |
1493 | mutex_exit(&uvm_swap_data_lock); |
1494 | } |
1495 | } |
1496 | |
1497 | #endif /* defined(VMSWAP) */ |
1498 | |