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