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