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