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