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