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