1 /* $NetBSD: uvm_amap.c,v 1.87 2009/08/16 11:06:37 yamt Exp $ */
5 * Copyright (c) 1997 Charles D. Cranor and Washington University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
36 * uvm_amap.c: amap operations
40 * this file contains functions that perform operations on amaps. see
41 * uvm_amap.h for a brief explanation of the role of amaps in uvm.
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: uvm_amap.c,v 1.87 2009/08/16 11:06:37 yamt Exp $");
47 #include "opt_uvmhist.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/kernel.h>
55 #include <sys/atomic.h>
58 #include <uvm/uvm_swap.h>
61 * cache for allocation of vm_map structures. note that in order to
62 * avoid an endless loop, the amap cache's allocator cannot allocate
63 * memory from an amap (it currently goes through the kernel uobj, so
66 static struct pool_cache uvm_amap_cache
;
67 static kmutex_t amap_list_lock
;
68 static LIST_HEAD(, vm_amap
) amap_list
;
75 amap_list_insert(struct vm_amap
*amap
)
78 mutex_enter(&amap_list_lock
);
79 LIST_INSERT_HEAD(&amap_list
, amap
, am_list
);
80 mutex_exit(&amap_list_lock
);
84 amap_list_remove(struct vm_amap
*amap
)
87 mutex_enter(&amap_list_lock
);
88 LIST_REMOVE(amap
, am_list
);
89 mutex_exit(&amap_list_lock
);
93 amap_roundup_slots(int slots
)
96 return kmem_roundup_size(slots
* sizeof(int)) / sizeof(int);
101 * what is ppref? ppref is an _optional_ amap feature which is used
102 * to keep track of reference counts on a per-page basis. it is enabled
103 * when UVM_AMAP_PPREF is defined.
105 * when enabled, an array of ints is allocated for the pprefs. this
106 * array is allocated only when a partial reference is added to the
107 * map (either by unmapping part of the amap, or gaining a reference
108 * to only a part of an amap). if the malloc of the array fails
109 * (M_NOWAIT), then we set the array pointer to PPREF_NONE to indicate
110 * that we tried to do ppref's but couldn't alloc the array so just
111 * give up (after all, this is an optional feature!).
113 * the array is divided into page sized "chunks." for chunks of length 1,
114 * the chunk reference count plus one is stored in that chunk's slot.
115 * for chunks of length > 1 the first slot contains (the reference count
116 * plus one) * -1. [the negative value indicates that the length is
117 * greater than one.] the second slot of the chunk contains the length
118 * of the chunk. here is an example:
120 * actual REFS: 2 2 2 2 3 1 1 0 0 0 4 4 0 1 1 1
121 * ppref: -3 4 x x 4 -2 2 -1 3 x -5 2 1 -2 3 x
122 * <----------><-><----><-------><----><-><------->
125 * this allows us to allow one int to contain the ref count for the whole
126 * chunk. note that the "plus one" part is needed because a reference
127 * count of zero is neither positive or negative (need a way to tell
128 * if we've got one zero or a bunch of them).
130 * here are some in-line functions to help us.
134 * pp_getreflen: get the reference and length for a specific offset
136 * => ppref's amap must be locked
139 pp_getreflen(int *ppref
, int offset
, int *refp
, int *lenp
)
142 if (ppref
[offset
] > 0) { /* chunk size must be 1 */
143 *refp
= ppref
[offset
] - 1; /* don't forget to adjust */
146 *refp
= (ppref
[offset
] * -1) - 1;
147 *lenp
= ppref
[offset
+1];
152 * pp_setreflen: set the reference and length for a specific offset
154 * => ppref's amap must be locked
157 pp_setreflen(int *ppref
, int offset
, int ref
, int len
)
162 ppref
[offset
] = ref
+ 1;
164 ppref
[offset
] = (ref
+ 1) * -1;
165 ppref
[offset
+1] = len
;
168 #endif /* UVM_AMAP_PPREF */
171 * amap_alloc1: internal function that allocates an amap, but does not
174 * => lock on returned amap is init'd
176 static inline struct vm_amap
*
177 amap_alloc1(int slots
, int padslots
, int waitf
)
179 struct vm_amap
*amap
;
183 amap
= pool_cache_get(&uvm_amap_cache
,
184 ((waitf
& UVM_FLAG_NOWAIT
) != 0) ? PR_NOWAIT
: PR_WAITOK
);
188 kmflags
= ((waitf
& UVM_FLAG_NOWAIT
) != 0) ? KM_NOSLEEP
: KM_SLEEP
;
189 totalslots
= amap_roundup_slots(slots
+ padslots
);
190 mutex_init(&amap
->am_l
, MUTEX_DEFAULT
, IPL_NONE
);
193 #ifdef UVM_AMAP_PPREF
194 amap
->am_ppref
= NULL
;
196 amap
->am_maxslot
= totalslots
;
197 amap
->am_nslot
= slots
;
200 amap
->am_slots
= kmem_alloc(totalslots
* sizeof(int), kmflags
);
201 if (amap
->am_slots
== NULL
)
204 amap
->am_bckptr
= kmem_alloc(totalslots
* sizeof(int), kmflags
);
205 if (amap
->am_bckptr
== NULL
)
208 amap
->am_anon
= kmem_alloc(totalslots
* sizeof(struct vm_anon
*),
210 if (amap
->am_anon
== NULL
)
216 kmem_free(amap
->am_bckptr
, totalslots
* sizeof(int));
218 kmem_free(amap
->am_slots
, totalslots
* sizeof(int));
220 mutex_destroy(&amap
->am_l
);
221 pool_cache_put(&uvm_amap_cache
, amap
);
224 * XXX hack to tell the pagedaemon how many pages we need,
225 * since we can need more than it would normally free.
227 if ((waitf
& UVM_FLAG_NOWAIT
) != 0) {
228 extern u_int uvm_extrapages
;
229 atomic_add_int(&uvm_extrapages
,
230 ((sizeof(int) * 2 + sizeof(struct vm_anon
*)) *
231 totalslots
) >> PAGE_SHIFT
);
237 * amap_alloc: allocate an amap to manage "sz" bytes of anonymous VM
239 * => caller should ensure sz is a multiple of PAGE_SIZE
240 * => reference count to new amap is set to one
241 * => new amap is returned unlocked
245 amap_alloc(vaddr_t sz
, vaddr_t padsz
, int waitf
)
247 struct vm_amap
*amap
;
249 UVMHIST_FUNC("amap_alloc"); UVMHIST_CALLED(maphist
);
251 AMAP_B2SLOT(slots
, sz
);
252 AMAP_B2SLOT(padslots
, padsz
);
254 amap
= amap_alloc1(slots
, padslots
, waitf
);
256 memset(amap
->am_anon
, 0,
257 amap
->am_maxslot
* sizeof(struct vm_anon
*));
258 amap_list_insert(amap
);
261 UVMHIST_LOG(maphist
,"<- done, amap = 0x%x, sz=%d", amap
, sz
, 0, 0);
266 * uvm_amap_init: initialize the amap system.
272 mutex_init(&amap_list_lock
, MUTEX_DEFAULT
, IPL_NONE
);
274 pool_cache_bootstrap(&uvm_amap_cache
, sizeof(struct vm_amap
), 0, 0, 0,
275 "amappl", NULL
, IPL_NONE
, NULL
, NULL
, NULL
);
279 * amap_free: free an amap
281 * => the amap must be unlocked
282 * => the amap should have a zero reference count and be empty
285 amap_free(struct vm_amap
*amap
)
289 UVMHIST_FUNC("amap_free"); UVMHIST_CALLED(maphist
);
291 KASSERT(amap
->am_ref
== 0 && amap
->am_nused
== 0);
292 KASSERT((amap
->am_flags
& AMAP_SWAPOFF
) == 0);
293 KASSERT(!mutex_owned(&amap
->am_l
));
294 slots
= amap
->am_maxslot
;
295 kmem_free(amap
->am_slots
, slots
* sizeof(*amap
->am_slots
));
296 kmem_free(amap
->am_bckptr
, slots
* sizeof(*amap
->am_bckptr
));
297 kmem_free(amap
->am_anon
, slots
* sizeof(*amap
->am_anon
));
298 #ifdef UVM_AMAP_PPREF
299 if (amap
->am_ppref
&& amap
->am_ppref
!= PPREF_NONE
)
300 kmem_free(amap
->am_ppref
, slots
* sizeof(*amap
->am_ppref
));
302 mutex_destroy(&amap
->am_l
);
303 pool_cache_put(&uvm_amap_cache
, amap
);
304 UVMHIST_LOG(maphist
,"<- done, freed amap = 0x%x", amap
, 0, 0, 0);
308 * amap_extend: extend the size of an amap (if needed)
310 * => called from uvm_map when we want to extend an amap to cover
311 * a new mapping (rather than allocate a new one)
312 * => amap should be unlocked (we will lock it)
313 * => to safely extend an amap it should have a reference count of
314 * one (thus it can't be shared)
317 amap_extend(struct vm_map_entry
*entry
, vsize_t addsize
, int flags
)
319 struct vm_amap
*amap
= entry
->aref
.ar_amap
;
320 int slotoff
= entry
->aref
.ar_pageoff
;
321 int slotmapped
, slotadd
, slotneed
, slotadded
, slotalloc
;
322 int slotadj
, slotspace
;
324 #ifdef UVM_AMAP_PPREF
325 int *newppref
, *oldppref
;
327 int i
, *newsl
, *newbck
, *oldsl
, *oldbck
;
328 struct vm_anon
**newover
, **oldover
;
329 const km_flag_t kmflags
=
330 (flags
& AMAP_EXTEND_NOWAIT
) ? KM_NOSLEEP
: KM_SLEEP
;
332 UVMHIST_FUNC("amap_extend"); UVMHIST_CALLED(maphist
);
334 UVMHIST_LOG(maphist
, " (entry=0x%x, addsize=0x%x, flags=0x%x)",
335 entry
, addsize
, flags
, 0);
338 * first, determine how many slots we need in the amap. don't
339 * forget that ar_pageoff could be non-zero: this means that
340 * there are some unused slots before us in the amap.
344 KASSERT(amap_refs(amap
) == 1); /* amap can't be shared */
345 AMAP_B2SLOT(slotmapped
, entry
->end
- entry
->start
); /* slots mapped */
346 AMAP_B2SLOT(slotadd
, addsize
); /* slots to add */
347 if (flags
& AMAP_EXTEND_FORWARDS
) {
348 slotneed
= slotoff
+ slotmapped
+ slotadd
;
353 slotneed
= slotadd
+ slotmapped
;
354 slotadj
= slotadd
- slotoff
;
355 slotspace
= amap
->am_maxslot
- slotmapped
;
359 * case 1: we already have enough slots in the map and thus
360 * only need to bump the reference counts on the slots we are
364 if (flags
& AMAP_EXTEND_FORWARDS
) {
365 if (amap
->am_nslot
>= slotneed
) {
366 #ifdef UVM_AMAP_PPREF
367 if (amap
->am_ppref
&& amap
->am_ppref
!= PPREF_NONE
) {
368 amap_pp_adjref(amap
, slotoff
+ slotmapped
,
374 "<- done (case 1f), amap = 0x%x, sltneed=%d",
375 amap
, slotneed
, 0, 0);
381 entry
->aref
.ar_pageoff
= slotoff
;
382 #ifdef UVM_AMAP_PPREF
383 if (amap
->am_ppref
&& amap
->am_ppref
!= PPREF_NONE
) {
384 amap_pp_adjref(amap
, slotoff
, slotadd
, 1);
389 "<- done (case 1b), amap = 0x%x, sltneed=%d",
390 amap
, slotneed
, 0, 0);
396 * case 2: we pre-allocated slots for use and we just need to
397 * bump nslot up to take account for these slots.
400 if (amap
->am_maxslot
>= slotneed
) {
401 if (flags
& AMAP_EXTEND_FORWARDS
) {
402 #ifdef UVM_AMAP_PPREF
403 if (amap
->am_ppref
&& amap
->am_ppref
!= PPREF_NONE
) {
404 if ((slotoff
+ slotmapped
) < amap
->am_nslot
)
406 slotoff
+ slotmapped
,
408 (slotoff
+ slotmapped
)), 1);
409 pp_setreflen(amap
->am_ppref
, amap
->am_nslot
, 1,
410 slotneed
- amap
->am_nslot
);
413 amap
->am_nslot
= slotneed
;
417 * no need to zero am_anon since that was done at
418 * alloc time and we never shrink an allocation.
421 UVMHIST_LOG(maphist
,"<- done (case 2f), amap = 0x%x, "
422 "slotneed=%d", amap
, slotneed
, 0, 0);
425 #ifdef UVM_AMAP_PPREF
426 if (amap
->am_ppref
&& amap
->am_ppref
!= PPREF_NONE
) {
428 * Slide up the ref counts on the pages that
429 * are actually in use.
431 memmove(amap
->am_ppref
+ slotspace
,
432 amap
->am_ppref
+ slotoff
,
433 slotmapped
* sizeof(int));
435 * Mark the (adjusted) gap at the front as
436 * referenced/not referenced.
438 pp_setreflen(amap
->am_ppref
,
439 0, 0, slotspace
- slotadd
);
440 pp_setreflen(amap
->am_ppref
,
441 slotspace
- slotadd
, 1, slotadd
);
446 * Slide the anon pointers up and clear out
447 * the space we just made.
449 memmove(amap
->am_anon
+ slotspace
,
450 amap
->am_anon
+ slotoff
,
451 slotmapped
* sizeof(struct vm_anon
*));
452 memset(amap
->am_anon
+ slotoff
, 0,
453 (slotspace
- slotoff
) * sizeof(struct vm_anon
*));
456 * Slide the backpointers up, but don't bother
457 * wiping out the old slots.
459 memmove(amap
->am_bckptr
+ slotspace
,
460 amap
->am_bckptr
+ slotoff
,
461 slotmapped
* sizeof(int));
464 * Adjust all the useful active slot numbers.
466 for (i
= 0; i
< amap
->am_nused
; i
++)
467 amap
->am_slots
[i
] += (slotspace
- slotoff
);
470 * We just filled all the empty space in the
471 * front of the amap by activating a few new
474 amap
->am_nslot
= amap
->am_maxslot
;
475 entry
->aref
.ar_pageoff
= slotspace
- slotadd
;
478 UVMHIST_LOG(maphist
,"<- done (case 2b), amap = 0x%x, "
479 "slotneed=%d", amap
, slotneed
, 0, 0);
485 * case 3: we need to malloc a new amap and copy all the amap
486 * data over from old amap to the new one.
488 * note that the use of a kernel realloc() probably would not
489 * help here, since we wish to abort cleanly if one of the
490 * three (or four) mallocs fails.
493 amap_unlock(amap
); /* unlock in case we sleep in malloc */
495 if (slotneed
>= UVM_AMAP_LARGE
) {
499 slotalloc
= amap_roundup_slots(slotneed
);
500 #ifdef UVM_AMAP_PPREF
502 if (amap
->am_ppref
&& amap
->am_ppref
!= PPREF_NONE
)
503 newppref
= kmem_alloc(slotalloc
* sizeof(*newppref
), kmflags
);
505 newsl
= kmem_alloc(slotalloc
* sizeof(*newsl
), kmflags
);
506 newbck
= kmem_alloc(slotalloc
* sizeof(*newbck
), kmflags
);
507 newover
= kmem_alloc(slotalloc
* sizeof(*newover
), kmflags
);
508 if (newsl
== NULL
|| newbck
== NULL
|| newover
== NULL
) {
509 #ifdef UVM_AMAP_PPREF
510 if (newppref
!= NULL
) {
511 kmem_free(newppref
, slotalloc
* sizeof(*newppref
));
515 kmem_free(newsl
, slotalloc
* sizeof(*newsl
));
517 if (newbck
!= NULL
) {
518 kmem_free(newbck
, slotalloc
* sizeof(*newbck
));
520 if (newover
!= NULL
) {
521 kmem_free(newover
, slotalloc
* sizeof(*newover
));
526 KASSERT(amap
->am_maxslot
< slotneed
);
529 * now copy everything over to new malloc'd areas...
532 slotadded
= slotalloc
- amap
->am_nslot
;
533 if (!(flags
& AMAP_EXTEND_FORWARDS
))
534 slotspace
= slotalloc
- slotmapped
;
537 oldsl
= amap
->am_slots
;
538 if (flags
& AMAP_EXTEND_FORWARDS
)
539 memcpy(newsl
, oldsl
, sizeof(int) * amap
->am_nused
);
541 for (i
= 0; i
< amap
->am_nused
; i
++)
542 newsl
[i
] = oldsl
[i
] + slotspace
- slotoff
;
543 amap
->am_slots
= newsl
;
546 oldover
= amap
->am_anon
;
547 if (flags
& AMAP_EXTEND_FORWARDS
) {
548 memcpy(newover
, oldover
,
549 sizeof(struct vm_anon
*) * amap
->am_nslot
);
550 memset(newover
+ amap
->am_nslot
, 0,
551 sizeof(struct vm_anon
*) * slotadded
);
553 memcpy(newover
+ slotspace
, oldover
+ slotoff
,
554 sizeof(struct vm_anon
*) * slotmapped
);
556 sizeof(struct vm_anon
*) * slotspace
);
558 amap
->am_anon
= newover
;
561 oldbck
= amap
->am_bckptr
;
562 if (flags
& AMAP_EXTEND_FORWARDS
)
563 memcpy(newbck
, oldbck
, sizeof(int) * amap
->am_nslot
);
565 memcpy(newbck
+ slotspace
, oldbck
+ slotoff
,
566 sizeof(int) * slotmapped
);
567 amap
->am_bckptr
= newbck
;
569 #ifdef UVM_AMAP_PPREF
571 oldppref
= amap
->am_ppref
;
573 if (flags
& AMAP_EXTEND_FORWARDS
) {
574 memcpy(newppref
, oldppref
,
575 sizeof(int) * amap
->am_nslot
);
576 memset(newppref
+ amap
->am_nslot
, 0,
577 sizeof(int) * slotadded
);
579 memcpy(newppref
+ slotspace
, oldppref
+ slotoff
,
580 sizeof(int) * slotmapped
);
582 amap
->am_ppref
= newppref
;
583 if ((flags
& AMAP_EXTEND_FORWARDS
) &&
584 (slotoff
+ slotmapped
) < amap
->am_nslot
)
585 amap_pp_adjref(amap
, slotoff
+ slotmapped
,
586 (amap
->am_nslot
- (slotoff
+ slotmapped
)), 1);
587 if (flags
& AMAP_EXTEND_FORWARDS
)
588 pp_setreflen(newppref
, amap
->am_nslot
, 1,
589 slotneed
- amap
->am_nslot
);
591 pp_setreflen(newppref
, 0, 0,
592 slotalloc
- slotneed
);
593 pp_setreflen(newppref
, slotalloc
- slotneed
, 1,
594 slotneed
- slotmapped
);
598 amap
->am_ppref
= PPREF_NONE
;
602 /* update master values */
603 if (flags
& AMAP_EXTEND_FORWARDS
)
604 amap
->am_nslot
= slotneed
;
606 entry
->aref
.ar_pageoff
= slotspace
- slotadd
;
607 amap
->am_nslot
= slotalloc
;
609 oldnslots
= amap
->am_maxslot
;
610 amap
->am_maxslot
= slotalloc
;
613 kmem_free(oldsl
, oldnslots
* sizeof(*oldsl
));
614 kmem_free(oldbck
, oldnslots
* sizeof(*oldbck
));
615 kmem_free(oldover
, oldnslots
* sizeof(*oldover
));
616 #ifdef UVM_AMAP_PPREF
617 if (oldppref
&& oldppref
!= PPREF_NONE
)
618 kmem_free(oldppref
, oldnslots
* sizeof(*oldppref
));
620 UVMHIST_LOG(maphist
,"<- done (case 3), amap = 0x%x, slotneed=%d",
621 amap
, slotneed
, 0, 0);
626 * amap_share_protect: change protection of anons in a shared amap
628 * for shared amaps, given the current data structure layout, it is
629 * not possible for us to directly locate all maps referencing the
630 * shared anon (to change the protection). in order to protect data
631 * in shared maps we use pmap_page_protect(). [this is useful for IPC
632 * mechanisms like map entry passing that may want to write-protect
633 * all mappings of a shared amap.] we traverse am_anon or am_slots
634 * depending on the current state of the amap.
636 * => entry's map and amap must be locked by the caller
639 amap_share_protect(struct vm_map_entry
*entry
, vm_prot_t prot
)
641 struct vm_amap
*amap
= entry
->aref
.ar_amap
;
642 int slots
, lcv
, slot
, stop
;
644 KASSERT(mutex_owned(&amap
->am_l
));
646 AMAP_B2SLOT(slots
, (entry
->end
- entry
->start
));
647 stop
= entry
->aref
.ar_pageoff
+ slots
;
649 if (slots
< amap
->am_nused
) {
650 /* cheaper to traverse am_anon */
651 for (lcv
= entry
->aref
.ar_pageoff
; lcv
< stop
; lcv
++) {
652 if (amap
->am_anon
[lcv
] == NULL
)
654 if (amap
->am_anon
[lcv
]->an_page
!= NULL
)
655 pmap_page_protect(amap
->am_anon
[lcv
]->an_page
,
661 /* cheaper to traverse am_slots */
662 for (lcv
= 0 ; lcv
< amap
->am_nused
; lcv
++) {
663 slot
= amap
->am_slots
[lcv
];
664 if (slot
< entry
->aref
.ar_pageoff
|| slot
>= stop
)
666 if (amap
->am_anon
[slot
]->an_page
!= NULL
)
667 pmap_page_protect(amap
->am_anon
[slot
]->an_page
, prot
);
672 * amap_wipeout: wipeout all anon's in an amap; then free the amap!
674 * => called from amap_unref when the final reference to an amap is
675 * discarded (i.e. when reference count drops to 0)
676 * => the amap should be locked (by the caller)
680 amap_wipeout(struct vm_amap
*amap
)
683 struct vm_anon
*anon
;
684 UVMHIST_FUNC("amap_wipeout"); UVMHIST_CALLED(maphist
);
685 UVMHIST_LOG(maphist
,"(amap=0x%x)", amap
, 0,0,0);
687 KASSERT(amap
->am_ref
== 0);
689 if (__predict_false((amap
->am_flags
& AMAP_SWAPOFF
) != 0)) {
691 * amap_swap_off will call us again.
696 amap_list_remove(amap
);
699 for (lcv
= 0 ; lcv
< amap
->am_nused
; lcv
++) {
702 slot
= amap
->am_slots
[lcv
];
703 anon
= amap
->am_anon
[slot
];
704 KASSERT(anon
!= NULL
&& anon
->an_ref
!= 0);
706 mutex_enter(&anon
->an_lock
);
707 UVMHIST_LOG(maphist
," processing anon 0x%x, ref=%d", anon
,
709 refs
= --anon
->an_ref
;
710 mutex_exit(&anon
->an_lock
);
714 * we had the last reference to a vm_anon. free it.
720 if (curlwp
->l_cpu
->ci_schedstate
.spc_flags
& SPCF_SHOULDYIELD
)
725 * now we free the map
729 amap_free(amap
); /* will unlock and free amap */
730 UVMHIST_LOG(maphist
,"<- done!", 0,0,0,0);
734 * amap_copy: ensure that a map entry's "needs_copy" flag is false
735 * by copying the amap if necessary.
737 * => an entry with a null amap pointer will get a new (blank) one.
738 * => the map that the map entry belongs to must be locked by caller.
739 * => the amap currently attached to "entry" (if any) must be unlocked.
740 * => if canchunk is true, then we may clip the entry into a chunk
741 * => "startva" and "endva" are used only if canchunk is true. they are
742 * used to limit chunking (e.g. if you have a large space that you
743 * know you are going to need to allocate amaps for, there is no point
744 * in allowing that to be chunked)
748 amap_copy(struct vm_map
*map
, struct vm_map_entry
*entry
, int flags
,
749 vaddr_t startva
, vaddr_t endva
)
751 struct vm_amap
*amap
, *srcamap
;
754 const int waitf
= (flags
& AMAP_COPY_NOWAIT
) ? UVM_FLAG_NOWAIT
: 0;
755 const bool canchunk
= (flags
& AMAP_COPY_NOCHUNK
) == 0;
756 UVMHIST_FUNC("amap_copy"); UVMHIST_CALLED(maphist
);
757 UVMHIST_LOG(maphist
, " (map=%p, entry=%p, flags=%d)",
758 map
, entry
, flags
, 0);
760 KASSERT(map
!= kernel_map
); /* we use nointr pool */
763 * is there a map to copy? if not, create one from scratch.
766 if (entry
->aref
.ar_amap
== NULL
) {
769 * check to see if we have a large amap that we can
770 * chunk. we align startva/endva to chunk-sized
771 * boundaries and then clip to them.
774 if (canchunk
&& atop(entry
->end
- entry
->start
) >=
776 /* convert slots to bytes */
777 chunksize
= UVM_AMAP_CHUNK
<< PAGE_SHIFT
;
778 startva
= (startva
/ chunksize
) * chunksize
;
779 endva
= roundup(endva
, chunksize
);
780 UVMHIST_LOG(maphist
, " chunk amap ==> clip 0x%x->0x%x"
781 "to 0x%x->0x%x", entry
->start
, entry
->end
, startva
,
783 UVM_MAP_CLIP_START(map
, entry
, startva
, NULL
);
784 /* watch out for endva wrap-around! */
785 if (endva
>= startva
)
786 UVM_MAP_CLIP_END(map
, entry
, endva
, NULL
);
789 if ((flags
& AMAP_COPY_NOMERGE
) == 0 &&
790 uvm_mapent_trymerge(map
, entry
, UVM_MERGE_COPYING
)) {
794 UVMHIST_LOG(maphist
, "<- done [creating new amap 0x%x->0x%x]",
795 entry
->start
, entry
->end
, 0, 0);
796 entry
->aref
.ar_pageoff
= 0;
797 entry
->aref
.ar_amap
= amap_alloc(entry
->end
- entry
->start
, 0,
799 if (entry
->aref
.ar_amap
!= NULL
)
800 entry
->etype
&= ~UVM_ET_NEEDSCOPY
;
805 * first check and see if we are the only map entry
806 * referencing the amap we currently have. if so, then we can
807 * just take it over rather than copying it. note that we are
808 * reading am_ref with the amap unlocked... the value can only
809 * be one if we have the only reference to the amap (via our
810 * locked map). if we are greater than one we fall through to
811 * the next case (where we double check the value).
814 if (entry
->aref
.ar_amap
->am_ref
== 1) {
815 entry
->etype
&= ~UVM_ET_NEEDSCOPY
;
816 UVMHIST_LOG(maphist
, "<- done [ref cnt = 1, took it over]",
822 * looks like we need to copy the map.
825 UVMHIST_LOG(maphist
," amap=%p, ref=%d, must copy it",
826 entry
->aref
.ar_amap
, entry
->aref
.ar_amap
->am_ref
, 0, 0);
827 AMAP_B2SLOT(slots
, entry
->end
- entry
->start
);
828 amap
= amap_alloc1(slots
, 0, waitf
);
830 UVMHIST_LOG(maphist
, " amap_alloc1 failed", 0,0,0,0);
833 srcamap
= entry
->aref
.ar_amap
;
837 * need to double check reference count now that we've got the
838 * src amap locked down. the reference count could have
839 * changed while we were in malloc. if the reference count
840 * dropped down to one we take over the old map rather than
844 if (srcamap
->am_ref
== 1) { /* take it over? */
845 entry
->etype
&= ~UVM_ET_NEEDSCOPY
;
846 amap
->am_ref
--; /* drop final reference to map */
847 amap_free(amap
); /* dispose of new (unused) amap */
848 amap_unlock(srcamap
);
853 * we must copy it now.
856 UVMHIST_LOG(maphist
, " copying amap now",0, 0, 0, 0);
857 for (lcv
= 0 ; lcv
< slots
; lcv
++) {
859 srcamap
->am_anon
[entry
->aref
.ar_pageoff
+ lcv
];
860 if (amap
->am_anon
[lcv
] == NULL
)
862 mutex_enter(&amap
->am_anon
[lcv
]->an_lock
);
863 amap
->am_anon
[lcv
]->an_ref
++;
864 mutex_exit(&amap
->am_anon
[lcv
]->an_lock
);
865 amap
->am_bckptr
[lcv
] = amap
->am_nused
;
866 amap
->am_slots
[amap
->am_nused
] = lcv
;
869 memset(&amap
->am_anon
[lcv
], 0,
870 (amap
->am_maxslot
- lcv
) * sizeof(struct vm_anon
*));
873 * drop our reference to the old amap (srcamap) and unlock.
874 * we know that the reference count on srcamap is greater than
875 * one (we checked above), so there is no way we could drop
876 * the count to zero. [and no need to worry about freeing it]
880 if (srcamap
->am_ref
== 1 && (srcamap
->am_flags
& AMAP_SHARED
) != 0)
881 srcamap
->am_flags
&= ~AMAP_SHARED
; /* clear shared flag */
882 #ifdef UVM_AMAP_PPREF
883 if (srcamap
->am_ppref
&& srcamap
->am_ppref
!= PPREF_NONE
) {
884 amap_pp_adjref(srcamap
, entry
->aref
.ar_pageoff
,
885 (entry
->end
- entry
->start
) >> PAGE_SHIFT
, -1);
889 amap_unlock(srcamap
);
891 amap_list_insert(amap
);
897 entry
->aref
.ar_pageoff
= 0;
898 entry
->aref
.ar_amap
= amap
;
899 entry
->etype
&= ~UVM_ET_NEEDSCOPY
;
900 UVMHIST_LOG(maphist
, "<- done",0, 0, 0, 0);
904 * amap_cow_now: resolve all copy-on-write faults in an amap now for fork(2)
906 * called during fork(2) when the parent process has a wired map
907 * entry. in that case we want to avoid write-protecting pages
908 * in the parent's map (e.g. like what you'd do for a COW page)
909 * so we resolve the COW here.
911 * => assume parent's entry was wired, thus all pages are resident.
912 * => assume pages that are loaned out (loan_count) are already mapped
913 * read-only in all maps, and thus no need for us to worry about them
914 * => assume both parent and child vm_map's are locked
915 * => caller passes child's map/entry in to us
916 * => if we run out of memory we will unlock the amap and sleep _with_ the
917 * parent and child vm_map's locked(!). we have to do this since
918 * we are in the middle of a fork(2) and we can't let the parent
919 * map change until we are done copying all the map entrys.
920 * => XXXCDC: out of memory should cause fork to fail, but there is
921 * currently no easy way to do this (needs fix)
922 * => page queues must be unlocked (we may lock them)
926 amap_cow_now(struct vm_map
*map
, struct vm_map_entry
*entry
)
928 struct vm_amap
*amap
= entry
->aref
.ar_amap
;
930 struct vm_anon
*anon
, *nanon
;
931 struct vm_page
*pg
, *npg
;
934 * note that if we unlock the amap then we must ReStart the "lcv" for
935 * loop because some other process could reorder the anon's in the
936 * am_anon[] array on us while the lock is dropped.
941 for (lcv
= 0 ; lcv
< amap
->am_nused
; lcv
++) {
947 slot
= amap
->am_slots
[lcv
];
948 anon
= amap
->am_anon
[slot
];
949 mutex_enter(&anon
->an_lock
);
952 * If the anon has only one ref, we must have already copied it.
953 * This can happen if we needed to sleep waiting for memory
954 * in a previous run through this loop. The new page might
955 * even have been paged out, since the new page is not wired.
958 if (anon
->an_ref
== 1) {
959 KASSERT(anon
->an_page
!= NULL
|| anon
->an_swslot
!= 0);
960 mutex_exit(&anon
->an_lock
);
965 * The old page must be resident since the parent is wired.
970 KASSERT(pg
->wire_count
> 0);
973 * If the page is loaned then it must already be mapped
974 * read-only and we don't need to copy it.
977 if (pg
->loan_count
!= 0) {
978 mutex_exit(&anon
->an_lock
);
981 KASSERT(pg
->uanon
== anon
&& pg
->uobject
== NULL
);
984 * if the page is busy then we have to unlock, wait for
985 * it and then restart.
988 if (pg
->flags
& PG_BUSY
) {
989 pg
->flags
|= PG_WANTED
;
991 UVM_UNLOCK_AND_WAIT(pg
, &anon
->an_lock
, false,
997 * ok, time to do a copy-on-write to a new anon
1000 nanon
= uvm_analloc();
1002 npg
= uvm_pagealloc(NULL
, 0, nanon
, 0);
1004 npg
= NULL
; /* XXX: quiet gcc warning */
1005 if (nanon
== NULL
|| npg
== NULL
) {
1008 * XXXCDC: we should cause fork to fail, but we can't.
1013 mutex_exit(&nanon
->an_lock
);
1016 mutex_exit(&anon
->an_lock
);
1018 uvm_wait("cownowpage");
1023 * got it... now we can copy the data and replace anon
1024 * with our new one...
1027 uvm_pagecopy(pg
, npg
); /* old -> new */
1028 anon
->an_ref
--; /* can't drop to zero */
1029 amap
->am_anon
[slot
] = nanon
; /* replace */
1032 * drop PG_BUSY on new page ... since we have had its owner
1033 * locked the whole time it can't be PG_RELEASED or PG_WANTED.
1036 mutex_enter(&uvm_pageqlock
);
1037 uvm_pageactivate(npg
);
1038 mutex_exit(&uvm_pageqlock
);
1039 npg
->flags
&= ~(PG_BUSY
|PG_FAKE
);
1040 UVM_PAGE_OWN(npg
, NULL
);
1041 mutex_exit(&nanon
->an_lock
);
1042 mutex_exit(&anon
->an_lock
);
1048 * amap_splitref: split a single reference into two separate references
1050 * => called from uvm_map's clip routines
1051 * => origref's map should be locked
1052 * => origref->ar_amap should be unlocked (we will lock)
1055 amap_splitref(struct vm_aref
*origref
, struct vm_aref
*splitref
, vaddr_t offset
)
1058 struct vm_amap
*amap
;
1060 KASSERT(splitref
->ar_amap
== origref
->ar_amap
);
1061 AMAP_B2SLOT(leftslots
, offset
);
1062 KASSERT(leftslots
!= 0);
1064 amap
= origref
->ar_amap
;
1068 * now: amap is locked and we have a valid am_mapped array.
1070 KASSERT(amap
->am_nslot
- origref
->ar_pageoff
- leftslots
> 0);
1072 #ifdef UVM_AMAP_PPREF
1074 * establish ppref before we add a duplicate reference to the amap
1076 if (amap
->am_ppref
== NULL
)
1077 amap_pp_establish(amap
, origref
->ar_pageoff
);
1080 amap
->am_ref
++; /* not a share reference */
1081 splitref
->ar_pageoff
= origref
->ar_pageoff
+ leftslots
;
1086 #ifdef UVM_AMAP_PPREF
1089 * amap_pp_establish: add a ppref array to an amap, if possible
1091 * => amap locked by caller
1094 amap_pp_establish(struct vm_amap
*amap
, vaddr_t offset
)
1097 amap
->am_ppref
= kmem_alloc(amap
->am_maxslot
* sizeof(*amap
->am_ppref
),
1101 * if we fail then we just won't use ppref for this amap
1104 if (amap
->am_ppref
== NULL
) {
1105 amap
->am_ppref
= PPREF_NONE
; /* not using it */
1108 memset(amap
->am_ppref
, 0, sizeof(int) * amap
->am_maxslot
);
1109 pp_setreflen(amap
->am_ppref
, 0, 0, offset
);
1110 pp_setreflen(amap
->am_ppref
, offset
, amap
->am_ref
,
1111 amap
->am_nslot
- offset
);
1116 * amap_pp_adjref: adjust reference count to a part of an amap using the
1117 * per-page reference count array.
1119 * => map and amap locked by caller
1120 * => caller must check that ppref != PPREF_NONE before calling
1123 amap_pp_adjref(struct vm_amap
*amap
, int curslot
, vsize_t slotlen
, int adjval
)
1125 int stopslot
, *ppref
, lcv
, prevlcv
;
1126 int ref
, len
, prevref
, prevlen
;
1128 stopslot
= curslot
+ slotlen
;
1129 ppref
= amap
->am_ppref
;
1133 * first advance to the correct place in the ppref array,
1134 * fragment if needed.
1137 for (lcv
= 0 ; lcv
< curslot
; lcv
+= len
) {
1138 pp_getreflen(ppref
, lcv
, &ref
, &len
);
1139 if (lcv
+ len
> curslot
) { /* goes past start? */
1140 pp_setreflen(ppref
, lcv
, ref
, curslot
- lcv
);
1141 pp_setreflen(ppref
, curslot
, ref
, len
- (curslot
-lcv
));
1142 len
= curslot
- lcv
; /* new length of entry @ lcv */
1147 pp_getreflen(ppref
, prevlcv
, &prevref
, &prevlen
);
1149 /* Ensure that the "prevref == ref" test below always
1150 * fails, since we're starting from the beginning of
1151 * the ppref array; that is, there is no previous
1159 * now adjust reference counts in range. merge the first
1160 * changed entry with the last unchanged entry if possible.
1162 KASSERT(lcv
== curslot
);
1163 for (/* lcv already set */; lcv
< stopslot
; lcv
+= len
) {
1164 pp_getreflen(ppref
, lcv
, &ref
, &len
);
1165 if (lcv
+ len
> stopslot
) { /* goes past end? */
1166 pp_setreflen(ppref
, lcv
, ref
, stopslot
- lcv
);
1167 pp_setreflen(ppref
, stopslot
, ref
,
1168 len
- (stopslot
- lcv
));
1169 len
= stopslot
- lcv
;
1173 if (lcv
== prevlcv
+ prevlen
&& ref
== prevref
) {
1174 pp_setreflen(ppref
, prevlcv
, ref
, prevlen
+ len
);
1176 pp_setreflen(ppref
, lcv
, ref
, len
);
1179 amap_wiperange(amap
, lcv
, len
);
1185 * amap_wiperange: wipe out a range of an amap
1186 * [different from amap_wipeout because the amap is kept intact]
1188 * => both map and amap must be locked by caller.
1191 amap_wiperange(struct vm_amap
*amap
, int slotoff
, int slots
)
1193 int byanon
, lcv
, stop
, curslot
, ptr
, slotend
;
1194 struct vm_anon
*anon
;
1197 * we can either traverse the amap by am_anon or by am_slots depending
1198 * on which is cheaper. decide now.
1201 if (slots
< amap
->am_nused
) {
1204 stop
= slotoff
+ slots
;
1209 stop
= amap
->am_nused
;
1210 slotend
= slotoff
+ slots
;
1213 while (lcv
< stop
) {
1217 curslot
= lcv
++; /* lcv advances here */
1218 if (amap
->am_anon
[curslot
] == NULL
)
1221 curslot
= amap
->am_slots
[lcv
];
1222 if (curslot
< slotoff
|| curslot
>= slotend
) {
1223 lcv
++; /* lcv advances here */
1226 stop
--; /* drop stop, since anon will be removed */
1228 anon
= amap
->am_anon
[curslot
];
1231 * remove it from the amap
1234 amap
->am_anon
[curslot
] = NULL
;
1235 ptr
= amap
->am_bckptr
[curslot
];
1236 if (ptr
!= (amap
->am_nused
- 1)) {
1237 amap
->am_slots
[ptr
] =
1238 amap
->am_slots
[amap
->am_nused
- 1];
1239 amap
->am_bckptr
[amap
->am_slots
[ptr
]] =
1240 ptr
; /* back ptr. */
1245 * drop anon reference count
1248 mutex_enter(&anon
->an_lock
);
1249 refs
= --anon
->an_ref
;
1250 mutex_exit(&anon
->an_lock
);
1254 * we just eliminated the last reference to an anon.
1268 * amap_swap_off: pagein anonymous pages in amaps and drop swap slots.
1270 * => called with swap_syscall_lock held.
1271 * => note that we don't always traverse all anons.
1272 * eg. amaps being wiped out, released anons.
1273 * => return true if failed.
1277 amap_swap_off(int startslot
, int endslot
)
1280 struct vm_amap
*am_next
;
1281 struct vm_amap marker_prev
;
1282 struct vm_amap marker_next
;
1285 #if defined(DIAGNOSTIC)
1286 memset(&marker_prev
, 0, sizeof(marker_prev
));
1287 memset(&marker_next
, 0, sizeof(marker_next
));
1288 #endif /* defined(DIAGNOSTIC) */
1290 mutex_enter(&amap_list_lock
);
1291 for (am
= LIST_FIRST(&amap_list
); am
!= NULL
&& !rv
; am
= am_next
) {
1294 LIST_INSERT_BEFORE(am
, &marker_prev
, am_list
);
1295 LIST_INSERT_AFTER(am
, &marker_next
, am_list
);
1297 if (!amap_lock_try(am
)) {
1298 mutex_exit(&amap_list_lock
);
1300 mutex_enter(&amap_list_lock
);
1301 am_next
= LIST_NEXT(&marker_prev
, am_list
);
1302 if (am_next
== &marker_next
) {
1303 am_next
= LIST_NEXT(am_next
, am_list
);
1305 KASSERT(LIST_NEXT(am_next
, am_list
) ==
1308 LIST_REMOVE(&marker_prev
, am_list
);
1309 LIST_REMOVE(&marker_next
, am_list
);
1313 mutex_exit(&amap_list_lock
);
1315 if (am
->am_nused
<= 0) {
1320 for (i
= 0; i
< am
->am_nused
; i
++) {
1323 struct vm_anon
*anon
;
1325 slot
= am
->am_slots
[i
];
1326 anon
= am
->am_anon
[slot
];
1327 mutex_enter(&anon
->an_lock
);
1329 swslot
= anon
->an_swslot
;
1330 if (swslot
< startslot
|| endslot
<= swslot
) {
1331 mutex_exit(&anon
->an_lock
);
1335 am
->am_flags
|= AMAP_SWAPOFF
;
1338 rv
= uvm_anon_pagein(anon
);
1341 am
->am_flags
&= ~AMAP_SWAPOFF
;
1342 if (amap_refs(am
) == 0) {
1358 mutex_enter(&amap_list_lock
);
1359 KASSERT(LIST_NEXT(&marker_prev
, am_list
) == &marker_next
||
1360 LIST_NEXT(LIST_NEXT(&marker_prev
, am_list
), am_list
) ==
1362 am_next
= LIST_NEXT(&marker_next
, am_list
);
1363 LIST_REMOVE(&marker_prev
, am_list
);
1364 LIST_REMOVE(&marker_next
, am_list
);
1366 mutex_exit(&amap_list_lock
);
1371 #endif /* defined(VMSWAP) */
1374 * amap_lookup: look up a page in an amap
1376 * => amap should be locked by caller.
1379 amap_lookup(struct vm_aref
*aref
, vaddr_t offset
)
1383 struct vm_amap
*amap
= aref
->ar_amap
;
1384 UVMHIST_FUNC("amap_lookup"); UVMHIST_CALLED(maphist
);
1385 KASSERT(mutex_owned(&amap
->am_l
));
1387 AMAP_B2SLOT(slot
, offset
);
1388 slot
+= aref
->ar_pageoff
;
1389 KASSERT(slot
< amap
->am_nslot
);
1391 UVMHIST_LOG(maphist
, "<- done (amap=0x%x, offset=0x%x, result=0x%x)",
1392 amap
, offset
, amap
->am_anon
[slot
], 0);
1393 an
= amap
->am_anon
[slot
];
1394 KASSERT(an
== NULL
|| an
->an_ref
!= 0);
1399 * amap_lookups: look up a range of pages in an amap
1401 * => amap should be locked by caller.
1402 * => XXXCDC: this interface is biased toward array-based amaps. fix.
1405 amap_lookups(struct vm_aref
*aref
, vaddr_t offset
, struct vm_anon
**anons
,
1409 struct vm_amap
*amap
= aref
->ar_amap
;
1410 #if defined(DIAGNOSTIC)
1412 #endif /* defined(DIAGNOSTIC) */
1413 UVMHIST_FUNC("amap_lookups"); UVMHIST_CALLED(maphist
);
1414 KASSERT(mutex_owned(&amap
->am_l
));
1416 AMAP_B2SLOT(slot
, offset
);
1417 slot
+= aref
->ar_pageoff
;
1419 UVMHIST_LOG(maphist
, " slot=%d, npages=%d, nslot=%d", slot
, npages
,
1422 KASSERT((slot
+ (npages
- 1)) < amap
->am_nslot
);
1423 memcpy(anons
, &amap
->am_anon
[slot
], npages
* sizeof(struct vm_anon
*));
1425 #if defined(DIAGNOSTIC)
1426 for (i
= 0; i
< npages
; i
++) {
1427 struct vm_anon
* const an
= anons
[i
];
1429 if (an
!= NULL
&& an
->an_ref
== 0) {
1430 panic("%s: ref=0 anon", __func__
);
1433 #endif /* defined(DIAGNOSTIC) */
1434 UVMHIST_LOG(maphist
, "<- done", 0, 0, 0, 0);
1439 * amap_add: add (or replace) a page to an amap
1441 * => caller must lock amap.
1442 * => if (replace) caller must lock anon because we might have to call
1443 * pmap_page_protect on the anon's page.
1446 amap_add(struct vm_aref
*aref
, vaddr_t offset
, struct vm_anon
*anon
,
1450 struct vm_amap
*amap
= aref
->ar_amap
;
1451 UVMHIST_FUNC("amap_add"); UVMHIST_CALLED(maphist
);
1452 KASSERT(mutex_owned(&amap
->am_l
));
1454 AMAP_B2SLOT(slot
, offset
);
1455 slot
+= aref
->ar_pageoff
;
1456 KASSERT(slot
< amap
->am_nslot
);
1459 KASSERT(amap
->am_anon
[slot
] != NULL
);
1460 if (amap
->am_anon
[slot
]->an_page
!= NULL
&&
1461 (amap
->am_flags
& AMAP_SHARED
) != 0) {
1462 pmap_page_protect(amap
->am_anon
[slot
]->an_page
,
1465 * XXX: suppose page is supposed to be wired somewhere?
1468 } else { /* !replace */
1469 KASSERT(amap
->am_anon
[slot
] == NULL
);
1470 amap
->am_bckptr
[slot
] = amap
->am_nused
;
1471 amap
->am_slots
[amap
->am_nused
] = slot
;
1474 amap
->am_anon
[slot
] = anon
;
1475 UVMHIST_LOG(maphist
,
1476 "<- done (amap=0x%x, offset=0x%x, anon=0x%x, rep=%d)",
1477 amap
, offset
, anon
, replace
);
1481 * amap_unadd: remove a page from an amap
1483 * => caller must lock amap
1486 amap_unadd(struct vm_aref
*aref
, vaddr_t offset
)
1489 struct vm_amap
*amap
= aref
->ar_amap
;
1490 UVMHIST_FUNC("amap_unadd"); UVMHIST_CALLED(maphist
);
1491 KASSERT(mutex_owned(&amap
->am_l
));
1493 AMAP_B2SLOT(slot
, offset
);
1494 slot
+= aref
->ar_pageoff
;
1495 KASSERT(slot
< amap
->am_nslot
);
1496 KASSERT(amap
->am_anon
[slot
] != NULL
);
1498 amap
->am_anon
[slot
] = NULL
;
1499 ptr
= amap
->am_bckptr
[slot
];
1501 if (ptr
!= (amap
->am_nused
- 1)) { /* swap to keep slots contig? */
1502 amap
->am_slots
[ptr
] = amap
->am_slots
[amap
->am_nused
- 1];
1503 amap
->am_bckptr
[amap
->am_slots
[ptr
]] = ptr
; /* back link */
1506 UVMHIST_LOG(maphist
, "<- done (amap=0x%x, slot=0x%x)", amap
, slot
,0, 0);
1510 * amap_ref: gain a reference to an amap
1512 * => amap must not be locked (we will lock)
1513 * => "offset" and "len" are in units of pages
1514 * => called at fork time to gain the child's reference
1517 amap_ref(struct vm_amap
*amap
, vaddr_t offset
, vsize_t len
, int flags
)
1519 UVMHIST_FUNC("amap_ref"); UVMHIST_CALLED(maphist
);
1522 if (flags
& AMAP_SHARED
)
1523 amap
->am_flags
|= AMAP_SHARED
;
1524 #ifdef UVM_AMAP_PPREF
1525 if (amap
->am_ppref
== NULL
&& (flags
& AMAP_REFALL
) == 0 &&
1526 len
!= amap
->am_nslot
)
1527 amap_pp_establish(amap
, offset
);
1530 #ifdef UVM_AMAP_PPREF
1531 if (amap
->am_ppref
&& amap
->am_ppref
!= PPREF_NONE
) {
1532 if (flags
& AMAP_REFALL
)
1533 amap_pp_adjref(amap
, 0, amap
->am_nslot
, 1);
1535 amap_pp_adjref(amap
, offset
, len
, 1);
1539 UVMHIST_LOG(maphist
,"<- done! amap=0x%x", amap
, 0, 0, 0);
1543 * amap_unref: remove a reference to an amap
1545 * => caller must remove all pmap-level references to this amap before
1546 * dropping the reference
1547 * => called from uvm_unmap_detach [only] ... note that entry is no
1548 * longer part of a map and thus has no need for locking
1549 * => amap must be unlocked (we will lock it).
1552 amap_unref(struct vm_amap
*amap
, vaddr_t offset
, vsize_t len
, bool all
)
1554 UVMHIST_FUNC("amap_unref"); UVMHIST_CALLED(maphist
);
1560 UVMHIST_LOG(maphist
," amap=0x%x refs=%d, nused=%d",
1561 amap
, amap
->am_ref
, amap
->am_nused
, 0);
1563 KASSERT(amap_refs(amap
) > 0);
1566 * if we are the last reference, free the amap and return.
1571 if (amap_refs(amap
) == 0) {
1572 amap_wipeout(amap
); /* drops final ref and frees */
1573 UVMHIST_LOG(maphist
,"<- done (was last ref)!", 0, 0, 0, 0);
1574 return; /* no need to unlock */
1578 * otherwise just drop the reference count(s)
1581 if (amap_refs(amap
) == 1 && (amap
->am_flags
& AMAP_SHARED
) != 0)
1582 amap
->am_flags
&= ~AMAP_SHARED
; /* clear shared flag */
1583 #ifdef UVM_AMAP_PPREF
1584 if (amap
->am_ppref
== NULL
&& all
== 0 && len
!= amap
->am_nslot
)
1585 amap_pp_establish(amap
, offset
);
1586 if (amap
->am_ppref
&& amap
->am_ppref
!= PPREF_NONE
) {
1588 amap_pp_adjref(amap
, 0, amap
->am_nslot
, -1);
1590 amap_pp_adjref(amap
, offset
, len
, -1);
1595 UVMHIST_LOG(maphist
,"<- done!", 0, 0, 0, 0);