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