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