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