uvm_aobj.c revision 1.16 1 /* $NetBSD: uvm_aobj.c,v 1.16 1999/03/24 03:45:27 cgd Exp $ */
2
3 /*
4 * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
5 * >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
6 */
7 /*
8 * Copyright (c) 1998 Chuck Silvers, Charles D. Cranor and
9 * Washington University.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Charles D. Cranor and
23 * Washington University.
24 * 4. The name of the author may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * from: Id: uvm_aobj.c,v 1.1.2.5 1998/02/06 05:14:38 chs Exp
39 */
40 /*
41 * uvm_aobj.c: anonymous memory uvm_object pager
42 *
43 * author: Chuck Silvers <chuq (at) chuq.com>
44 * started: Jan-1998
45 *
46 * - design mostly from Chuck Cranor
47 */
48
49
50
51 #include "opt_uvmhist.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/proc.h>
56 #include <sys/malloc.h>
57 #include <sys/pool.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_kern.h>
62
63 #include <uvm/uvm.h>
64
65 /*
66 * an aobj manages anonymous-memory backed uvm_objects. in addition
67 * to keeping the list of resident pages, it also keeps a list of
68 * allocated swap blocks. depending on the size of the aobj this list
69 * of allocated swap blocks is either stored in an array (small objects)
70 * or in a hash table (large objects).
71 */
72
73 /*
74 * local structures
75 */
76
77 /*
78 * for hash tables, we break the address space of the aobj into blocks
79 * of UAO_SWHASH_CLUSTER_SIZE pages. we require the cluster size to
80 * be a power of two.
81 */
82
83 #define UAO_SWHASH_CLUSTER_SHIFT 4
84 #define UAO_SWHASH_CLUSTER_SIZE (1 << UAO_SWHASH_CLUSTER_SHIFT)
85
86 /* get the "tag" for this page index */
87 #define UAO_SWHASH_ELT_TAG(PAGEIDX) \
88 ((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT)
89
90 /* given an ELT and a page index, find the swap slot */
91 #define UAO_SWHASH_ELT_PAGESLOT(ELT, PAGEIDX) \
92 ((ELT)->slots[(PAGEIDX) & (UAO_SWHASH_CLUSTER_SIZE - 1)])
93
94 /* given an ELT, return its pageidx base */
95 #define UAO_SWHASH_ELT_PAGEIDX_BASE(ELT) \
96 ((ELT)->tag << UAO_SWHASH_CLUSTER_SHIFT)
97
98 /*
99 * the swhash hash function
100 */
101 #define UAO_SWHASH_HASH(AOBJ, PAGEIDX) \
102 (&(AOBJ)->u_swhash[(((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT) \
103 & (AOBJ)->u_swhashmask)])
104
105 /*
106 * the swhash threshhold determines if we will use an array or a
107 * hash table to store the list of allocated swap blocks.
108 */
109
110 #define UAO_SWHASH_THRESHOLD (UAO_SWHASH_CLUSTER_SIZE * 4)
111 #define UAO_USES_SWHASH(AOBJ) \
112 ((AOBJ)->u_pages > UAO_SWHASH_THRESHOLD) /* use hash? */
113
114 /*
115 * the number of buckets in a swhash, with an upper bound
116 */
117 #define UAO_SWHASH_MAXBUCKETS 256
118 #define UAO_SWHASH_BUCKETS(AOBJ) \
119 (min((AOBJ)->u_pages >> UAO_SWHASH_CLUSTER_SHIFT, \
120 UAO_SWHASH_MAXBUCKETS))
121
122
123 /*
124 * uao_swhash_elt: when a hash table is being used, this structure defines
125 * the format of an entry in the bucket list.
126 */
127
128 struct uao_swhash_elt {
129 LIST_ENTRY(uao_swhash_elt) list; /* the hash list */
130 vaddr_t tag; /* our 'tag' */
131 int count; /* our number of active slots */
132 int slots[UAO_SWHASH_CLUSTER_SIZE]; /* the slots */
133 };
134
135 /*
136 * uao_swhash: the swap hash table structure
137 */
138
139 LIST_HEAD(uao_swhash, uao_swhash_elt);
140
141 /*
142 * uao_swhash_elt_pool: pool of uao_swhash_elt structures
143 */
144
145 struct pool uao_swhash_elt_pool;
146
147 /*
148 * uvm_aobj: the actual anon-backed uvm_object
149 *
150 * => the uvm_object is at the top of the structure, this allows
151 * (struct uvm_device *) == (struct uvm_object *)
152 * => only one of u_swslots and u_swhash is used in any given aobj
153 */
154
155 struct uvm_aobj {
156 struct uvm_object u_obj; /* has: lock, pgops, memq, #pages, #refs */
157 int u_pages; /* number of pages in entire object */
158 int u_flags; /* the flags (see uvm_aobj.h) */
159 int *u_swslots; /* array of offset->swapslot mappings */
160 /*
161 * hashtable of offset->swapslot mappings
162 * (u_swhash is an array of bucket heads)
163 */
164 struct uao_swhash *u_swhash;
165 u_long u_swhashmask; /* mask for hashtable */
166 LIST_ENTRY(uvm_aobj) u_list; /* global list of aobjs */
167 };
168
169 /*
170 * uvm_aobj_pool: pool of uvm_aobj structures
171 */
172
173 struct pool uvm_aobj_pool;
174
175 /*
176 * local functions
177 */
178
179 static void uao_init __P((void));
180 static struct uao_swhash_elt *uao_find_swhash_elt __P((struct uvm_aobj *,
181 int, boolean_t));
182 static int uao_find_swslot __P((struct uvm_aobj *,
183 int));
184 static boolean_t uao_flush __P((struct uvm_object *,
185 vaddr_t, vaddr_t,
186 int));
187 static void uao_free __P((struct uvm_aobj *));
188 static int uao_get __P((struct uvm_object *, vaddr_t,
189 vm_page_t *, int *, int,
190 vm_prot_t, int, int));
191 static boolean_t uao_releasepg __P((struct vm_page *,
192 struct vm_page **));
193
194
195
196 /*
197 * aobj_pager
198 *
199 * note that some functions (e.g. put) are handled elsewhere
200 */
201
202 struct uvm_pagerops aobj_pager = {
203 uao_init, /* init */
204 uao_reference, /* reference */
205 uao_detach, /* detach */
206 NULL, /* fault */
207 uao_flush, /* flush */
208 uao_get, /* get */
209 NULL, /* asyncget */
210 NULL, /* put (done by pagedaemon) */
211 NULL, /* cluster */
212 NULL, /* mk_pcluster */
213 uvm_shareprot, /* shareprot */
214 NULL, /* aiodone */
215 uao_releasepg /* releasepg */
216 };
217
218 /*
219 * uao_list: global list of active aobjs, locked by uao_list_lock
220 */
221
222 static LIST_HEAD(aobjlist, uvm_aobj) uao_list;
223 static simple_lock_data_t uao_list_lock;
224
225
226 /*
227 * functions
228 */
229
230 /*
231 * hash table/array related functions
232 */
233
234 /*
235 * uao_find_swhash_elt: find (or create) a hash table entry for a page
236 * offset.
237 *
238 * => the object should be locked by the caller
239 */
240
241 static struct uao_swhash_elt *
242 uao_find_swhash_elt(aobj, pageidx, create)
243 struct uvm_aobj *aobj;
244 int pageidx;
245 boolean_t create;
246 {
247 struct uao_swhash *swhash;
248 struct uao_swhash_elt *elt;
249 int page_tag;
250
251 swhash = UAO_SWHASH_HASH(aobj, pageidx); /* first hash to get bucket */
252 page_tag = UAO_SWHASH_ELT_TAG(pageidx); /* tag to search for */
253
254 /*
255 * now search the bucket for the requested tag
256 */
257 for (elt = swhash->lh_first; elt != NULL; elt = elt->list.le_next) {
258 if (elt->tag == page_tag)
259 return(elt);
260 }
261
262 /* fail now if we are not allowed to create a new entry in the bucket */
263 if (!create)
264 return NULL;
265
266
267 /*
268 * allocate a new entry for the bucket and init/insert it in
269 */
270 elt = pool_get(&uao_swhash_elt_pool, PR_WAITOK);
271 LIST_INSERT_HEAD(swhash, elt, list);
272 elt->tag = page_tag;
273 elt->count = 0;
274 memset(elt->slots, 0, sizeof(elt->slots));
275
276 return(elt);
277 }
278
279 /*
280 * uao_find_swslot: find the swap slot number for an aobj/pageidx
281 *
282 * => object must be locked by caller
283 */
284 __inline static int
285 uao_find_swslot(aobj, pageidx)
286 struct uvm_aobj *aobj;
287 int pageidx;
288 {
289
290 /*
291 * if noswap flag is set, then we never return a slot
292 */
293
294 if (aobj->u_flags & UAO_FLAG_NOSWAP)
295 return(0);
296
297 /*
298 * if hashing, look in hash table.
299 */
300
301 if (UAO_USES_SWHASH(aobj)) {
302 struct uao_swhash_elt *elt =
303 uao_find_swhash_elt(aobj, pageidx, FALSE);
304
305 if (elt)
306 return(UAO_SWHASH_ELT_PAGESLOT(elt, pageidx));
307 else
308 return(NULL);
309 }
310
311 /*
312 * otherwise, look in the array
313 */
314 return(aobj->u_swslots[pageidx]);
315 }
316
317 /*
318 * uao_set_swslot: set the swap slot for a page in an aobj.
319 *
320 * => setting a slot to zero frees the slot
321 * => object must be locked by caller
322 */
323 int
324 uao_set_swslot(uobj, pageidx, slot)
325 struct uvm_object *uobj;
326 int pageidx, slot;
327 {
328 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
329 int oldslot;
330 UVMHIST_FUNC("uao_set_swslot"); UVMHIST_CALLED(pdhist);
331 UVMHIST_LOG(pdhist, "aobj %p pageidx %d slot %d",
332 aobj, pageidx, slot, 0);
333
334 /*
335 * if noswap flag is set, then we can't set a slot
336 */
337
338 if (aobj->u_flags & UAO_FLAG_NOSWAP) {
339
340 if (slot == 0)
341 return(0); /* a clear is ok */
342
343 /* but a set is not */
344 printf("uao_set_swslot: uobj = %p\n", uobj);
345 panic("uao_set_swslot: attempt to set a slot on a NOSWAP object");
346 }
347
348 /*
349 * are we using a hash table? if so, add it in the hash.
350 */
351
352 if (UAO_USES_SWHASH(aobj)) {
353 /*
354 * Avoid allocating an entry just to free it again if
355 * the page had not swap slot in the first place, and
356 * we are freeing.
357 */
358 struct uao_swhash_elt *elt =
359 uao_find_swhash_elt(aobj, pageidx, slot ? TRUE : FALSE);
360 if (elt == NULL) {
361 #ifdef DIAGNOSTIC
362 if (slot)
363 panic("uao_set_swslot: didn't create elt");
364 #endif
365 return (0);
366 }
367
368 oldslot = UAO_SWHASH_ELT_PAGESLOT(elt, pageidx);
369 UAO_SWHASH_ELT_PAGESLOT(elt, pageidx) = slot;
370
371 /*
372 * now adjust the elt's reference counter and free it if we've
373 * dropped it to zero.
374 */
375
376 /* an allocation? */
377 if (slot) {
378 if (oldslot == 0)
379 elt->count++;
380 } else { /* freeing slot ... */
381 if (oldslot) /* to be safe */
382 elt->count--;
383
384 if (elt->count == 0) {
385 LIST_REMOVE(elt, list);
386 pool_put(&uao_swhash_elt_pool, elt);
387 }
388 }
389
390 } else {
391 /* we are using an array */
392 oldslot = aobj->u_swslots[pageidx];
393 aobj->u_swslots[pageidx] = slot;
394 }
395 return (oldslot);
396 }
397
398 /*
399 * end of hash/array functions
400 */
401
402 /*
403 * uao_free: free all resources held by an aobj, and then free the aobj
404 *
405 * => the aobj should be dead
406 */
407 static void
408 uao_free(aobj)
409 struct uvm_aobj *aobj;
410 {
411
412 if (UAO_USES_SWHASH(aobj)) {
413 int i, hashbuckets = aobj->u_swhashmask + 1;
414
415 /*
416 * free the swslots from each hash bucket,
417 * then the hash bucket, and finally the hash table itself.
418 */
419 for (i = 0; i < hashbuckets; i++) {
420 struct uao_swhash_elt *elt, *next;
421
422 for (elt = aobj->u_swhash[i].lh_first; elt != NULL;
423 elt = next) {
424 int j;
425
426 for (j = 0; j < UAO_SWHASH_CLUSTER_SIZE; j++)
427 {
428 int slot = elt->slots[j];
429
430 if (slot)
431 uvm_swap_free(slot, 1);
432 }
433
434 next = elt->list.le_next;
435 pool_put(&uao_swhash_elt_pool, elt);
436 }
437 }
438 FREE(aobj->u_swhash, M_UVMAOBJ);
439 } else {
440 int i;
441
442 /*
443 * free the array
444 */
445
446 for (i = 0; i < aobj->u_pages; i++)
447 {
448 int slot = aobj->u_swslots[i];
449
450 if (slot)
451 uvm_swap_free(slot, 1);
452 }
453 FREE(aobj->u_swslots, M_UVMAOBJ);
454 }
455
456 /*
457 * finally free the aobj itself
458 */
459 pool_put(&uvm_aobj_pool, aobj);
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 struct uvm_object *
475 uao_create(size, flags)
476 vsize_t size;
477 int flags;
478 {
479 static struct uvm_aobj kernel_object_store; /* home of kernel_object */
480 static int kobj_alloced = 0; /* not allocated yet */
481 int pages = round_page(size) >> PAGE_SHIFT;
482 struct uvm_aobj *aobj;
483
484 /*
485 * malloc a new aobj unless we are asked for the kernel object
486 */
487 if (flags & UAO_FLAG_KERNOBJ) { /* want kernel object? */
488 if (kobj_alloced)
489 panic("uao_create: kernel object already allocated");
490
491 /*
492 * XXXTHORPEJ: Need to call this now, so the pool gets
493 * initialized!
494 */
495 uao_init();
496
497 aobj = &kernel_object_store;
498 aobj->u_pages = pages;
499 aobj->u_flags = UAO_FLAG_NOSWAP; /* no swap to start */
500 /* we are special, we never die */
501 aobj->u_obj.uo_refs = UVM_OBJ_KERN;
502 kobj_alloced = UAO_FLAG_KERNOBJ;
503 } else if (flags & UAO_FLAG_KERNSWAP) {
504 aobj = &kernel_object_store;
505 if (kobj_alloced != UAO_FLAG_KERNOBJ)
506 panic("uao_create: asked to enable swap on kernel object");
507 kobj_alloced = UAO_FLAG_KERNSWAP;
508 } else { /* normal object */
509 aobj = pool_get(&uvm_aobj_pool, PR_WAITOK);
510 aobj->u_pages = pages;
511 aobj->u_flags = 0; /* normal object */
512 aobj->u_obj.uo_refs = 1; /* start with 1 reference */
513 }
514
515 /*
516 * allocate hash/array if necessary
517 *
518 * note: in the KERNSWAP case no need to worry about locking since
519 * we are still booting we should be the only thread around.
520 */
521 if (flags == 0 || (flags & UAO_FLAG_KERNSWAP) != 0) {
522 int mflags = (flags & UAO_FLAG_KERNSWAP) != 0 ?
523 M_NOWAIT : M_WAITOK;
524
525 /* allocate hash table or array depending on object size */
526 if (UAO_USES_SWHASH(aobj)) {
527 aobj->u_swhash = hashinit(UAO_SWHASH_BUCKETS(aobj),
528 M_UVMAOBJ, mflags, &aobj->u_swhashmask);
529 if (aobj->u_swhash == NULL)
530 panic("uao_create: hashinit swhash failed");
531 } else {
532 MALLOC(aobj->u_swslots, int *, pages * sizeof(int),
533 M_UVMAOBJ, mflags);
534 if (aobj->u_swslots == NULL)
535 panic("uao_create: malloc swslots failed");
536 memset(aobj->u_swslots, 0, pages * sizeof(int));
537 }
538
539 if (flags) {
540 aobj->u_flags &= ~UAO_FLAG_NOSWAP; /* clear noswap */
541 return(&aobj->u_obj);
542 /* done! */
543 }
544 }
545
546 /*
547 * init aobj fields
548 */
549 simple_lock_init(&aobj->u_obj.vmobjlock);
550 aobj->u_obj.pgops = &aobj_pager;
551 TAILQ_INIT(&aobj->u_obj.memq);
552 aobj->u_obj.uo_npages = 0;
553
554 /*
555 * now that aobj is ready, add it to the global list
556 * XXXCHS: uao_init hasn't been called'd in the KERNOBJ case,
557 * do we really need the kernel object on this list anyway?
558 */
559 simple_lock(&uao_list_lock);
560 LIST_INSERT_HEAD(&uao_list, aobj, u_list);
561 simple_unlock(&uao_list_lock);
562
563 /*
564 * done!
565 */
566 return(&aobj->u_obj);
567 }
568
569
570
571 /*
572 * uao_init: set up aobj pager subsystem
573 *
574 * => called at boot time from uvm_pager_init()
575 */
576 static void
577 uao_init()
578 {
579 static int uao_initialized;
580
581 if (uao_initialized)
582 return;
583 uao_initialized = TRUE;
584
585 LIST_INIT(&uao_list);
586 simple_lock_init(&uao_list_lock);
587
588 /*
589 * NOTE: Pages fror this pool must not come from a pageable
590 * kernel map!
591 */
592 pool_init(&uao_swhash_elt_pool, sizeof(struct uao_swhash_elt),
593 0, 0, 0, "uaoeltpl", 0, NULL, NULL, M_UVMAOBJ);
594
595 pool_init(&uvm_aobj_pool, sizeof(struct uvm_aobj), 0, 0, 0,
596 "aobjpl", 0,
597 pool_page_alloc_nointr, pool_page_free_nointr, M_UVMAOBJ);
598 }
599
600 /*
601 * uao_reference: add a ref to an aobj
602 *
603 * => aobj must be unlocked (we will lock it)
604 */
605 void
606 uao_reference(uobj)
607 struct uvm_object *uobj;
608 {
609 UVMHIST_FUNC("uao_reference"); UVMHIST_CALLED(maphist);
610
611 /*
612 * kernel_object already has plenty of references, leave it alone.
613 */
614
615 if (uobj->uo_refs == UVM_OBJ_KERN)
616 return;
617
618 simple_lock(&uobj->vmobjlock);
619 uobj->uo_refs++; /* bump! */
620 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
621 uobj, uobj->uo_refs,0,0);
622 simple_unlock(&uobj->vmobjlock);
623 }
624
625 /*
626 * uao_detach: drop a reference to an aobj
627 *
628 * => aobj must be unlocked, we will lock it
629 */
630 void
631 uao_detach(uobj)
632 struct uvm_object *uobj;
633 {
634 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
635 struct vm_page *pg;
636 boolean_t busybody;
637 UVMHIST_FUNC("uao_detach"); UVMHIST_CALLED(maphist);
638
639 /*
640 * detaching from kernel_object is a noop.
641 */
642 if (uobj->uo_refs == UVM_OBJ_KERN)
643 return;
644
645 simple_lock(&uobj->vmobjlock);
646
647 UVMHIST_LOG(maphist," (uobj=0x%x) ref=%d", uobj,uobj->uo_refs,0,0);
648 uobj->uo_refs--; /* drop ref! */
649 if (uobj->uo_refs) { /* still more refs? */
650 simple_unlock(&uobj->vmobjlock);
651 UVMHIST_LOG(maphist, "<- done (rc>0)", 0,0,0,0);
652 return;
653 }
654
655 /*
656 * remove the aobj from the global list.
657 */
658 simple_lock(&uao_list_lock);
659 LIST_REMOVE(aobj, u_list);
660 simple_unlock(&uao_list_lock);
661
662 /*
663 * free all the pages that aren't PG_BUSY, mark for release any that are.
664 */
665
666 busybody = FALSE;
667 for (pg = uobj->memq.tqh_first ; pg != NULL ; pg = pg->listq.tqe_next) {
668 int swslot;
669
670 if (pg->flags & PG_BUSY) {
671 pg->flags |= PG_RELEASED;
672 busybody = TRUE;
673 continue;
674 }
675
676
677 /* zap the mappings, free the swap slot, free the page */
678 pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
679
680 swslot = uao_set_swslot(&aobj->u_obj,
681 pg->offset >> PAGE_SHIFT, 0);
682 if (swslot) {
683 uvm_swap_free(swslot, 1);
684 }
685
686 uvm_lock_pageq();
687 uvm_pagefree(pg);
688 uvm_unlock_pageq();
689 }
690
691 /*
692 * if we found any busy pages, we're done for now.
693 * mark the aobj for death, releasepg will finish up for us.
694 */
695 if (busybody) {
696 aobj->u_flags |= UAO_FLAG_KILLME;
697 simple_unlock(&aobj->u_obj.vmobjlock);
698 return;
699 }
700
701 /*
702 * finally, free the rest.
703 */
704 uao_free(aobj);
705 }
706
707 /*
708 * uao_flush: uh, yea, sure it's flushed. really!
709 */
710 boolean_t
711 uao_flush(uobj, start, end, flags)
712 struct uvm_object *uobj;
713 vaddr_t start, end;
714 int flags;
715 {
716
717 /*
718 * anonymous memory doesn't "flush"
719 */
720 /*
721 * XXX
722 * deal with PGO_DEACTIVATE (for madvise(MADV_SEQUENTIAL))
723 * and PGO_FREE (for msync(MSINVALIDATE))
724 */
725 return TRUE;
726 }
727
728 /*
729 * uao_get: fetch me a page
730 *
731 * we have three cases:
732 * 1: page is resident -> just return the page.
733 * 2: page is zero-fill -> allocate a new page and zero it.
734 * 3: page is swapped out -> fetch the page from swap.
735 *
736 * cases 1 and 2 can be handled with PGO_LOCKED, case 3 cannot.
737 * so, if the "center" page hits case 3 (or any page, with PGO_ALLPAGES),
738 * then we will need to return VM_PAGER_UNLOCK.
739 *
740 * => prefer map unlocked (not required)
741 * => object must be locked! we will _unlock_ it before starting any I/O.
742 * => flags: PGO_ALLPAGES: get all of the pages
743 * PGO_LOCKED: fault data structures are locked
744 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
745 * => NOTE: caller must check for released pages!!
746 */
747 static int
748 uao_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
749 struct uvm_object *uobj;
750 vaddr_t offset;
751 struct vm_page **pps;
752 int *npagesp;
753 int centeridx, advice, flags;
754 vm_prot_t access_type;
755 {
756 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
757 vaddr_t current_offset;
758 vm_page_t ptmp;
759 int lcv, gotpages, maxpages, swslot, rv;
760 boolean_t done;
761 UVMHIST_FUNC("uao_get"); UVMHIST_CALLED(pdhist);
762
763 UVMHIST_LOG(pdhist, "aobj=%p offset=%d, flags=%d", aobj, offset, flags,0);
764
765 /*
766 * get number of pages
767 */
768
769 maxpages = *npagesp;
770
771 /*
772 * step 1: handled the case where fault data structures are locked.
773 */
774
775 if (flags & PGO_LOCKED) {
776
777 /*
778 * step 1a: get pages that are already resident. only do
779 * this if the data structures are locked (i.e. the first
780 * time through).
781 */
782
783 done = TRUE; /* be optimistic */
784 gotpages = 0; /* # of pages we got so far */
785
786 for (lcv = 0, current_offset = offset ; lcv < maxpages ;
787 lcv++, current_offset += PAGE_SIZE) {
788 /* do we care about this page? if not, skip it */
789 if (pps[lcv] == PGO_DONTCARE)
790 continue;
791
792 ptmp = uvm_pagelookup(uobj, current_offset);
793
794 /*
795 * if page is new, attempt to allocate the page, then
796 * zero-fill it.
797 */
798 if (ptmp == NULL && uao_find_swslot(aobj,
799 current_offset >> PAGE_SHIFT) == 0) {
800 ptmp = uvm_pagealloc(uobj, current_offset,
801 NULL);
802 if (ptmp) {
803 /* new page */
804 ptmp->flags &= ~(PG_BUSY|PG_FAKE);
805 ptmp->pqflags |= PQ_AOBJ;
806 UVM_PAGE_OWN(ptmp, NULL);
807 uvm_pagezero(ptmp);
808 }
809 }
810
811 /*
812 * to be useful must get a non-busy, non-released page
813 */
814 if (ptmp == NULL ||
815 (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
816 if (lcv == centeridx ||
817 (flags & PGO_ALLPAGES) != 0)
818 /* need to do a wait or I/O! */
819 done = FALSE;
820 continue;
821 }
822
823 /*
824 * useful page: busy/lock it and plug it in our
825 * result array
826 */
827 /* caller must un-busy this page */
828 ptmp->flags |= PG_BUSY;
829 UVM_PAGE_OWN(ptmp, "uao_get1");
830 pps[lcv] = ptmp;
831 gotpages++;
832
833 } /* "for" lcv loop */
834
835 /*
836 * step 1b: now we've either done everything needed or we
837 * to unlock and do some waiting or I/O.
838 */
839
840 UVMHIST_LOG(pdhist, "<- done (done=%d)", done, 0,0,0);
841
842 *npagesp = gotpages;
843 if (done)
844 /* bingo! */
845 return(VM_PAGER_OK);
846 else
847 /* EEK! Need to unlock and I/O */
848 return(VM_PAGER_UNLOCK);
849 }
850
851 /*
852 * step 2: get non-resident or busy pages.
853 * object is locked. data structures are unlocked.
854 */
855
856 for (lcv = 0, current_offset = offset ; lcv < maxpages ;
857 lcv++, current_offset += PAGE_SIZE) {
858 /*
859 * - skip over pages we've already gotten or don't want
860 * - skip over pages we don't _have_ to get
861 */
862 if (pps[lcv] != NULL ||
863 (lcv != centeridx && (flags & PGO_ALLPAGES) == 0))
864 continue;
865
866 /*
867 * we have yet to locate the current page (pps[lcv]). we
868 * first look for a page that is already at the current offset.
869 * if we find a page, we check to see if it is busy or
870 * released. if that is the case, then we sleep on the page
871 * until it is no longer busy or released and repeat the lookup.
872 * if the page we found is neither busy nor released, then we
873 * busy it (so we own it) and plug it into pps[lcv]. this
874 * 'break's the following while loop and indicates we are
875 * ready to move on to the next page in the "lcv" loop above.
876 *
877 * if we exit the while loop with pps[lcv] still set to NULL,
878 * then it means that we allocated a new busy/fake/clean page
879 * ptmp in the object and we need to do I/O to fill in the data.
880 */
881
882 /* top of "pps" while loop */
883 while (pps[lcv] == NULL) {
884 /* look for a resident page */
885 ptmp = uvm_pagelookup(uobj, current_offset);
886
887 /* not resident? allocate one now (if we can) */
888 if (ptmp == NULL) {
889
890 ptmp = uvm_pagealloc(uobj, current_offset,
891 NULL); /* alloc */
892
893 /* out of RAM? */
894 if (ptmp == NULL) {
895 simple_unlock(&uobj->vmobjlock);
896 UVMHIST_LOG(pdhist,
897 "sleeping, ptmp == NULL\n",0,0,0,0);
898 uvm_wait("uao_getpage");
899 simple_lock(&uobj->vmobjlock);
900 /* goto top of pps while loop */
901 continue;
902 }
903
904 /*
905 * safe with PQ's unlocked: because we just
906 * alloc'd the page
907 */
908 ptmp->pqflags |= PQ_AOBJ;
909
910 /*
911 * got new page ready for I/O. break pps while
912 * loop. pps[lcv] is still NULL.
913 */
914 break;
915 }
916
917 /* page is there, see if we need to wait on it */
918 if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
919 ptmp->flags |= PG_WANTED;
920 UVMHIST_LOG(pdhist,
921 "sleeping, ptmp->flags 0x%x\n",
922 ptmp->flags,0,0,0);
923 UVM_UNLOCK_AND_WAIT(ptmp, &uobj->vmobjlock, 0,
924 "uao_get", 0);
925 simple_lock(&uobj->vmobjlock);
926 continue; /* goto top of pps while loop */
927 }
928
929 /*
930 * if we get here then the page has become resident and
931 * unbusy between steps 1 and 2. we busy it now (so we
932 * own it) and set pps[lcv] (so that we exit the while
933 * loop).
934 */
935 /* we own it, caller must un-busy */
936 ptmp->flags |= PG_BUSY;
937 UVM_PAGE_OWN(ptmp, "uao_get2");
938 pps[lcv] = ptmp;
939 }
940
941 /*
942 * if we own the valid page at the correct offset, pps[lcv] will
943 * point to it. nothing more to do except go to the next page.
944 */
945 if (pps[lcv])
946 continue; /* next lcv */
947
948 /*
949 * we have a "fake/busy/clean" page that we just allocated.
950 * do the needed "i/o", either reading from swap or zeroing.
951 */
952 swslot = uao_find_swslot(aobj, current_offset >> PAGE_SHIFT);
953
954 /*
955 * just zero the page if there's nothing in swap.
956 */
957 if (swslot == 0)
958 {
959 /*
960 * page hasn't existed before, just zero it.
961 */
962 uvm_pagezero(ptmp);
963 }
964 else
965 {
966 UVMHIST_LOG(pdhist, "pagein from swslot %d",
967 swslot, 0,0,0);
968
969 /*
970 * page in the swapped-out page.
971 * unlock object for i/o, relock when done.
972 */
973 simple_unlock(&uobj->vmobjlock);
974 rv = uvm_swap_get(ptmp, swslot, PGO_SYNCIO);
975 simple_lock(&uobj->vmobjlock);
976
977 /*
978 * I/O done. check for errors.
979 */
980 if (rv != VM_PAGER_OK)
981 {
982 UVMHIST_LOG(pdhist, "<- done (error=%d)",
983 rv,0,0,0);
984 if (ptmp->flags & PG_WANTED)
985 /* object lock still held */
986 thread_wakeup(ptmp);
987 ptmp->flags &= ~(PG_WANTED|PG_BUSY);
988 UVM_PAGE_OWN(ptmp, NULL);
989 uvm_lock_pageq();
990 uvm_pagefree(ptmp);
991 uvm_unlock_pageq();
992 simple_unlock(&uobj->vmobjlock);
993 return (rv);
994 }
995 }
996
997 /*
998 * we got the page! clear the fake flag (indicates valid
999 * data now in page) and plug into our result array. note
1000 * that page is still busy.
1001 *
1002 * it is the callers job to:
1003 * => check if the page is released
1004 * => unbusy the page
1005 * => activate the page
1006 */
1007
1008 ptmp->flags &= ~PG_FAKE; /* data is valid ... */
1009 pmap_clear_modify(PMAP_PGARG(ptmp)); /* ... and clean */
1010 pps[lcv] = ptmp;
1011
1012 } /* lcv loop */
1013
1014 /*
1015 * finally, unlock object and return.
1016 */
1017
1018 simple_unlock(&uobj->vmobjlock);
1019 UVMHIST_LOG(pdhist, "<- done (OK)",0,0,0,0);
1020 return(VM_PAGER_OK);
1021 }
1022
1023 /*
1024 * uao_releasepg: handle released page in an aobj
1025 *
1026 * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
1027 * to dispose of.
1028 * => caller must handle PG_WANTED case
1029 * => called with page's object locked, pageq's unlocked
1030 * => returns TRUE if page's object is still alive, FALSE if we
1031 * killed the page's object. if we return TRUE, then we
1032 * return with the object locked.
1033 * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return
1034 * with the page queues locked [for pagedaemon]
1035 * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
1036 * => we kill the aobj if it is not referenced and we are suppose to
1037 * kill it ("KILLME").
1038 */
1039 static boolean_t uao_releasepg(pg, nextpgp)
1040 struct vm_page *pg;
1041 struct vm_page **nextpgp; /* OUT */
1042 {
1043 struct uvm_aobj *aobj = (struct uvm_aobj *) pg->uobject;
1044 int slot;
1045
1046 #ifdef DIAGNOSTIC
1047 if ((pg->flags & PG_RELEASED) == 0)
1048 panic("uao_releasepg: page not released!");
1049 #endif
1050
1051 /*
1052 * dispose of the page [caller handles PG_WANTED] and swap slot.
1053 */
1054 pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
1055 slot = uao_set_swslot(&aobj->u_obj, pg->offset >> PAGE_SHIFT, 0);
1056 if (slot)
1057 uvm_swap_free(slot, 1);
1058 uvm_lock_pageq();
1059 if (nextpgp)
1060 *nextpgp = pg->pageq.tqe_next; /* next page for daemon */
1061 uvm_pagefree(pg);
1062 if (!nextpgp)
1063 uvm_unlock_pageq(); /* keep locked for daemon */
1064
1065 /*
1066 * if we're not killing the object, we're done.
1067 */
1068 if ((aobj->u_flags & UAO_FLAG_KILLME) == 0)
1069 return TRUE;
1070
1071 #ifdef DIAGNOSTIC
1072 if (aobj->u_obj.uo_refs)
1073 panic("uvm_km_releasepg: kill flag set on referenced object!");
1074 #endif
1075
1076 /*
1077 * if there are still pages in the object, we're done for now.
1078 */
1079 if (aobj->u_obj.uo_npages != 0)
1080 return TRUE;
1081
1082 #ifdef DIAGNOSTIC
1083 if (aobj->u_obj.memq.tqh_first)
1084 panic("uvn_releasepg: pages in object with npages == 0");
1085 #endif
1086
1087 /*
1088 * finally, free the rest.
1089 */
1090 uao_free(aobj);
1091
1092 return FALSE;
1093 }
1094