1 /* $NetBSD: subr_extent.c,v 1.71 2008/03/17 17:05:54 ad Exp $ */
4 * Copyright (c) 1996, 1998, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Matthias Drochner.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * General purpose extent manager.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: subr_extent.c,v 1.71 2008/03/17 17:05:54 ad Exp $");
40 #include "opt_lockdebug.h"
42 #include <sys/param.h>
43 #include <sys/extent.h>
44 #include <sys/malloc.h>
47 #include <sys/systm.h>
50 #include <uvm/uvm_extern.h>
52 #define KMEM_IS_RUNNING (kmem_map != NULL)
53 #elif defined(_EXTENT_TESTING)
55 * user-land definitions, so it can fit into a testing harness.
57 #include <sys/param.h>
59 #include <sys/extent.h>
67 * Use multi-line #defines to avoid screwing up the kernel tags file;
68 * without this, ctags produces a tags file where panic() shows up
69 * in subr_extent.c rather than subr_prf.c.
72 malloc(s, t, flags) malloc(s)
76 cv_wait_sig(cv, lock) (EWOULDBLOCK)
78 pool_get(pool, flags) malloc((pool)->pr_size,0,0)
80 pool_put(pool, rp) free(rp,0)
83 #define mutex_init(a, b, c)
84 #define mutex_destroy(a)
85 #define mutex_enter(l)
87 #define cv_wait(cv, lock)
88 #define cv_broadcast(cv)
91 #define KMEM_IS_RUNNING (1)
93 #define MUTEX_DEFAULT (0)
96 static struct pool expool
;
99 * Macro to align to an arbitrary power-of-two boundary.
101 #define EXTENT_ALIGN(_start, _align, _skew) \
102 (((((_start) - (_skew)) + ((_align) - 1)) & (-(_align))) + (_skew))
105 * Create the extent_region pool.
112 pool_init(&expool
, sizeof(struct extent_region
), 0, 0, 0,
113 "extent", NULL
, IPL_VM
);
115 expool
.pr_size
= sizeof(struct extent_region
);
120 * Allocate an extent region descriptor. EXTENT MUST NOT BE LOCKED.
121 * We will handle any locking we may need.
123 static struct extent_region
*
124 extent_alloc_region_descriptor(struct extent
*ex
, int flags
)
126 struct extent_region
*rp
;
130 * If the kernel memory allocator is not yet running, we can't
131 * use it (obviously).
133 if (KMEM_IS_RUNNING
== 0)
134 flags
&= ~EX_MALLOCOK
;
137 * XXX Make a static, create-time flags word, so we don't
138 * XXX have to lock to read it!
140 mutex_enter(&ex
->ex_lock
);
141 exflags
= ex
->ex_flags
;
142 mutex_exit(&ex
->ex_lock
);
144 if (exflags
& EXF_FIXED
) {
145 struct extent_fixed
*fex
= (struct extent_fixed
*)ex
;
147 mutex_enter(&ex
->ex_lock
);
149 if ((rp
= LIST_FIRST(&fex
->fex_freelist
)) != NULL
) {
151 * Don't muck with flags after pulling it off
152 * the freelist; it may have been dynamically
153 * allocated, and kindly given to us. We
154 * need to remember that information.
156 LIST_REMOVE(rp
, er_link
);
157 mutex_exit(&ex
->ex_lock
);
160 if (flags
& EX_MALLOCOK
) {
161 mutex_exit(&ex
->ex_lock
);
164 if ((flags
& EX_WAITOK
) == 0) {
165 mutex_exit(&ex
->ex_lock
);
168 ex
->ex_flags
|= EXF_FLWANTED
;
169 if ((flags
& EX_CATCH
) != 0)
170 error
= cv_wait_sig(&ex
->ex_cv
, &ex
->ex_lock
);
172 cv_wait(&ex
->ex_cv
, &ex
->ex_lock
);
176 mutex_exit(&ex
->ex_lock
);
183 rp
= pool_get(&expool
, (flags
& EX_WAITOK
) ? PR_WAITOK
: 0);
186 rp
->er_flags
= ER_ALLOC
;
192 * Free an extent region descriptor. EXTENT _MUST_ BE LOCKED!
195 extent_free_region_descriptor(struct extent
*ex
, struct extent_region
*rp
)
198 if (ex
->ex_flags
& EXF_FIXED
) {
199 struct extent_fixed
*fex
= (struct extent_fixed
*)ex
;
202 * If someone's waiting for a region descriptor,
203 * be nice and give them this one, rather than
204 * just free'ing it back to the system.
206 if (rp
->er_flags
& ER_ALLOC
) {
207 if (ex
->ex_flags
& EXF_FLWANTED
) {
208 /* Clear all but ER_ALLOC flag. */
209 rp
->er_flags
= ER_ALLOC
;
210 LIST_INSERT_HEAD(&fex
->fex_freelist
, rp
,
214 pool_put(&expool
, rp
);
216 /* Clear all flags. */
218 LIST_INSERT_HEAD(&fex
->fex_freelist
, rp
, er_link
);
222 ex
->ex_flags
&= ~EXF_FLWANTED
;
223 cv_broadcast(&ex
->ex_cv
);
228 * We know it's dynamically allocated if we get here.
230 pool_put(&expool
, rp
);
234 * Allocate and initialize an extent map.
237 extent_create(const char *name
, u_long start
, u_long end
,
238 struct malloc_type
*mtype
, void *storage
, size_t storagesize
, int flags
)
242 size_t sz
= storagesize
;
243 struct extent_region
*rp
;
244 int fixed_extent
= (storage
!= NULL
);
251 /* Check arguments. */
253 panic("extent_create: name == NULL");
255 printf("extent_create: extent `%s', start 0x%lx, end 0x%lx\n",
257 panic("extent_create: end < start");
259 if (fixed_extent
&& (storagesize
< sizeof(struct extent_fixed
)))
260 panic("extent_create: fixed extent, bad storagesize 0x%lx",
261 (u_long
)storagesize
);
262 if (fixed_extent
== 0 && (storagesize
!= 0 || storage
!= NULL
))
263 panic("extent_create: storage provided for non-fixed");
266 /* Allocate extent descriptor. */
268 struct extent_fixed
*fex
;
270 memset(storage
, 0, storagesize
);
273 * Align all descriptors on "long" boundaries.
275 fex
= (struct extent_fixed
*)cp
;
276 ex
= (struct extent
*)fex
;
277 cp
+= ALIGN(sizeof(struct extent_fixed
));
278 sz
-= ALIGN(sizeof(struct extent_fixed
));
279 fex
->fex_storage
= storage
;
280 fex
->fex_storagesize
= storagesize
;
283 * In a fixed extent, we have to pre-allocate region
284 * descriptors and place them in the extent's freelist.
286 LIST_INIT(&fex
->fex_freelist
);
287 while (sz
>= ALIGN(sizeof(struct extent_region
))) {
288 rp
= (struct extent_region
*)cp
;
289 cp
+= ALIGN(sizeof(struct extent_region
));
290 sz
-= ALIGN(sizeof(struct extent_region
));
291 LIST_INSERT_HEAD(&fex
->fex_freelist
, rp
, er_link
);
294 ex
= (struct extent
*)malloc(sizeof(struct extent
),
295 mtype
, (flags
& EX_WAITOK
) ? M_WAITOK
: M_NOWAIT
);
300 /* Fill in the extent descriptor and return it to the caller. */
301 mutex_init(&ex
->ex_lock
, MUTEX_DEFAULT
, IPL_VM
);
302 cv_init(&ex
->ex_cv
, "extent");
303 LIST_INIT(&ex
->ex_regions
);
305 ex
->ex_start
= start
;
307 ex
->ex_mtype
= mtype
;
310 ex
->ex_flags
|= EXF_FIXED
;
311 if (flags
& EX_NOCOALESCE
)
312 ex
->ex_flags
|= EXF_NOCOALESCE
;
317 * Destroy an extent map.
318 * Since we're freeing the data, there can't be any references
319 * so we don't need any locking.
322 extent_destroy(struct extent
*ex
)
324 struct extent_region
*rp
, *orp
;
327 /* Check arguments. */
329 panic("extent_destroy: NULL extent");
332 /* Free all region descriptors in extent. */
333 for (rp
= LIST_FIRST(&ex
->ex_regions
); rp
!= NULL
; ) {
335 rp
= LIST_NEXT(rp
, er_link
);
336 LIST_REMOVE(orp
, er_link
);
337 extent_free_region_descriptor(ex
, orp
);
340 cv_destroy(&ex
->ex_cv
);
341 mutex_destroy(&ex
->ex_lock
);
343 /* If we're not a fixed extent, free the extent descriptor itself. */
344 if ((ex
->ex_flags
& EXF_FIXED
) == 0)
345 free(ex
, ex
->ex_mtype
);
349 * Insert a region descriptor into the sorted region list after the
350 * entry "after" or at the head of the list (if "after" is NULL).
351 * The region descriptor we insert is passed in "rp". We must
352 * allocate the region descriptor before calling this function!
353 * If we don't need the region descriptor, it will be freed here.
356 extent_insert_and_optimize(struct extent
*ex
, u_long start
, u_long size
,
357 int flags
, struct extent_region
*after
, struct extent_region
*rp
)
359 struct extent_region
*nextr
;
364 * We're the first in the region list. If there's
365 * a region after us, attempt to coalesce to save
366 * descriptor overhead.
368 if (((ex
->ex_flags
& EXF_NOCOALESCE
) == 0) &&
369 (LIST_FIRST(&ex
->ex_regions
) != NULL
) &&
370 ((start
+ size
) == LIST_FIRST(&ex
->ex_regions
)->er_start
)) {
372 * We can coalesce. Prepend us to the first region.
374 LIST_FIRST(&ex
->ex_regions
)->er_start
= start
;
375 extent_free_region_descriptor(ex
, rp
);
380 * Can't coalesce. Fill in the region descriptor
381 * in, and insert us at the head of the region list.
383 rp
->er_start
= start
;
384 rp
->er_end
= start
+ (size
- 1);
385 LIST_INSERT_HEAD(&ex
->ex_regions
, rp
, er_link
);
390 * If EXF_NOCOALESCE is set, coalescing is disallowed.
392 if (ex
->ex_flags
& EXF_NOCOALESCE
)
396 * Attempt to coalesce with the region before us.
398 if ((after
->er_end
+ 1) == start
) {
400 * We can coalesce. Append ourselves and make
403 after
->er_end
= start
+ (size
- 1);
408 * Attempt to coalesce with the region after us.
410 if ((LIST_NEXT(after
, er_link
) != NULL
) &&
411 ((start
+ size
) == LIST_NEXT(after
, er_link
)->er_start
)) {
413 * We can coalesce. Note that if we appended ourselves
414 * to the previous region, we exactly fit the gap, and
415 * can free the "next" region descriptor.
419 * Yup, we can free it up.
421 after
->er_end
= LIST_NEXT(after
, er_link
)->er_end
;
422 nextr
= LIST_NEXT(after
, er_link
);
423 LIST_REMOVE(nextr
, er_link
);
424 extent_free_region_descriptor(ex
, nextr
);
427 * Nope, just prepend us to the next region.
429 LIST_NEXT(after
, er_link
)->er_start
= start
;
432 extent_free_region_descriptor(ex
, rp
);
437 * We weren't able to coalesce with the next region, but
438 * we don't need to allocate a region descriptor if we
439 * appended ourselves to the previous region.
442 extent_free_region_descriptor(ex
, rp
);
449 * Fill in the region descriptor and insert ourselves
450 * into the region list.
452 rp
->er_start
= start
;
453 rp
->er_end
= start
+ (size
- 1);
454 LIST_INSERT_AFTER(after
, rp
, er_link
);
458 * Allocate a specific region in an extent map.
461 extent_alloc_region(struct extent
*ex
, u_long start
, u_long size
, int flags
)
463 struct extent_region
*rp
, *last
, *myrp
;
464 u_long end
= start
+ (size
- 1);
468 /* Check arguments. */
470 panic("extent_alloc_region: NULL extent");
472 printf("extent_alloc_region: extent `%s', size 0x%lx\n",
474 panic("extent_alloc_region: bad size");
478 "extent_alloc_region: extent `%s', start 0x%lx, size 0x%lx\n",
479 ex
->ex_name
, start
, size
);
480 panic("extent_alloc_region: overflow");
484 if (flags
& EX_WAITSPACE
) {
490 * Make sure the requested region lies within the
493 * We don't lock to check the range, because those values
494 * are never modified, and if another thread deletes the
495 * extent, we're screwed anyway.
497 if ((start
< ex
->ex_start
) || (end
> ex
->ex_end
)) {
499 printf("extent_alloc_region: extent `%s' (0x%lx - 0x%lx)\n",
500 ex
->ex_name
, ex
->ex_start
, ex
->ex_end
);
501 printf("extent_alloc_region: start 0x%lx, end 0x%lx\n",
503 panic("extent_alloc_region: region lies outside extent");
510 * Allocate the region descriptor. It will be freed later
511 * if we can coalesce with another region. Don't lock before
512 * here! This could block.
514 myrp
= extent_alloc_region_descriptor(ex
, flags
);
518 "extent_alloc_region: can't allocate region descriptor\n");
523 mutex_enter(&ex
->ex_lock
);
527 * Attempt to place ourselves in the desired area of the
528 * extent. We save ourselves some work by keeping the list sorted.
529 * In other words, if the start of the current region is greater
530 * than the end of our region, we don't have to search any further.
534 * Keep a pointer to the last region we looked at so
535 * that we don't have to traverse the list again when
536 * we insert ourselves. If "last" is NULL when we
537 * finally insert ourselves, we go at the head of the
538 * list. See extent_insert_and_optimize() for details.
542 LIST_FOREACH(rp
, &ex
->ex_regions
, er_link
) {
543 if (rp
->er_start
> end
) {
545 * We lie before this region and don't
552 * The current region begins before we end.
553 * Check for a conflict.
555 if (rp
->er_end
>= start
) {
557 * We conflict. If we can (and want to) wait,
560 if (flags
& EX_WAITSPACE
) {
561 if ((flags
& EX_CATCH
) != 0)
562 error
= cv_wait_sig(&ex
->ex_cv
,
565 cv_wait(&ex
->ex_cv
, &ex
->ex_lock
);
570 mutex_exit(&ex
->ex_lock
);
572 mutex_exit(&ex
->ex_lock
);
575 extent_free_region_descriptor(ex
, myrp
);
579 * We don't conflict, but this region lies before
580 * us. Keep a pointer to this region, and keep
587 * We don't conflict with any regions. "last" points
588 * to the region we fall after, or is NULL if we belong
589 * at the beginning of the region list. Insert ourselves.
591 extent_insert_and_optimize(ex
, start
, size
, flags
, last
, myrp
);
592 mutex_exit(&ex
->ex_lock
);
597 * Macro to check (x + y) <= z. This check is designed to fail
598 * if an overflow occurs.
600 #define LE_OV(x, y, z) ((((x) + (y)) >= (x)) && (((x) + (y)) <= (z)))
603 * Allocate a region in an extent map subregion.
605 * If EX_FAST is specified, we return the first fit in the map.
606 * Otherwise, we try to minimize fragmentation by finding the
607 * smallest gap that will hold the request.
609 * The allocated region is aligned to "alignment", which must be
613 extent_alloc_subregion1(struct extent
*ex
, u_long substart
, u_long subend
,
614 u_long size
, u_long alignment
, u_long skew
, u_long boundary
,
615 int flags
, u_long
*result
)
617 struct extent_region
*rp
, *myrp
, *last
, *bestlast
;
618 u_long newstart
, newend
, exend
, beststart
, bestovh
, ovh
;
626 * We don't lock to check these, because these values
627 * are never modified, and if another thread deletes the
628 * extent, we're screwed anyway.
631 panic("extent_alloc_subregion: NULL extent");
633 panic("extent_alloc_subregion: NULL result pointer");
634 if ((substart
< ex
->ex_start
) || (substart
> ex
->ex_end
) ||
635 (subend
> ex
->ex_end
) || (subend
< ex
->ex_start
)) {
636 printf("extent_alloc_subregion: extent `%s', ex_start 0x%lx, ex_end 0x%lx\n",
637 ex
->ex_name
, ex
->ex_start
, ex
->ex_end
);
638 printf("extent_alloc_subregion: substart 0x%lx, subend 0x%lx\n",
640 panic("extent_alloc_subregion: bad subregion");
642 if ((size
< 1) || ((size
- 1) > (subend
- substart
))) {
643 printf("extent_alloc_subregion: extent `%s', size 0x%lx\n",
645 panic("extent_alloc_subregion: bad size");
648 panic("extent_alloc_subregion: bad alignment");
649 if (boundary
&& (boundary
< size
)) {
651 "extent_alloc_subregion: extent `%s', size 0x%lx, "
652 "boundary 0x%lx\n", ex
->ex_name
, size
, boundary
);
653 panic("extent_alloc_subregion: bad boundary");
657 if (flags
& EX_WAITSPACE
) {
663 * Allocate the region descriptor. It will be freed later
664 * if we can coalesce with another region. Don't lock before
665 * here! This could block.
667 myrp
= extent_alloc_region_descriptor(ex
, flags
);
671 "extent_alloc_subregion: can't allocate region descriptor\n");
677 mutex_enter(&ex
->ex_lock
);
680 * Keep a pointer to the last region we looked at so
681 * that we don't have to traverse the list again when
682 * we insert ourselves. If "last" is NULL when we
683 * finally insert ourselves, we go at the head of the
684 * list. See extent_insert_and_optimize() for deatails.
689 * Keep track of size and location of the smallest
692 * Since the extent can be as large as the numeric range
693 * of the CPU (0 - 0xffffffff for 32-bit systems), the
694 * best overhead value can be the maximum unsigned integer.
695 * Thus, we initialize "bestovh" to 0, since we insert ourselves
696 * into the region list immediately on an exact match (which
697 * is the only case where "bestovh" would be set to 0).
704 * Keep track of end of free region. This is either the end of extent
705 * or the start of a region past the subend.
710 * For N allocated regions, we must make (N + 1)
711 * checks for unallocated space. The first chunk we
712 * check is the area from the beginning of the subregion
713 * to the first allocated region after that point.
715 newstart
= EXTENT_ALIGN(substart
, alignment
, skew
);
716 if (newstart
< ex
->ex_start
) {
719 "extent_alloc_subregion: extent `%s' (0x%lx - 0x%lx), alignment 0x%lx\n",
720 ex
->ex_name
, ex
->ex_start
, ex
->ex_end
, alignment
);
721 mutex_exit(&ex
->ex_lock
);
722 panic("extent_alloc_subregion: overflow after alignment");
724 extent_free_region_descriptor(ex
, myrp
);
725 mutex_exit(&ex
->ex_lock
);
731 * Find the first allocated region that begins on or after
732 * the subregion start, advancing the "last" pointer along
735 LIST_FOREACH(rp
, &ex
->ex_regions
, er_link
) {
736 if (rp
->er_start
>= newstart
)
742 * Relocate the start of our candidate region to the end of
743 * the last allocated region (if there was one overlapping
746 if (last
!= NULL
&& last
->er_end
>= newstart
)
747 newstart
= EXTENT_ALIGN((last
->er_end
+ 1), alignment
, skew
);
749 for (; rp
!= NULL
; rp
= LIST_NEXT(rp
, er_link
)) {
751 * If the region pasts the subend, bail out and see
752 * if we fit against the subend.
754 if (rp
->er_start
> subend
) {
755 exend
= rp
->er_start
;
760 * Check the chunk before "rp". Note that our
761 * comparison is safe from overflow conditions.
763 if (LE_OV(newstart
, size
, rp
->er_start
)) {
765 * Do a boundary check, if necessary. Note
766 * that a region may *begin* on the boundary,
767 * but it must end before the boundary.
770 newend
= newstart
+ (size
- 1);
773 * Calculate the next boundary after the start
776 dontcross
= EXTENT_ALIGN(newstart
+1, boundary
,
777 (flags
& EX_BOUNDZERO
) ? 0 : ex
->ex_start
)
781 printf("newstart=%lx newend=%lx ex_start=%lx ex_end=%lx boundary=%lx dontcross=%lx\n",
782 newstart
, newend
, ex
->ex_start
, ex
->ex_end
,
783 boundary
, dontcross
);
786 /* Check for overflow */
787 if (dontcross
< ex
->ex_start
)
788 dontcross
= ex
->ex_end
;
789 else if (newend
> dontcross
) {
791 * Candidate region crosses boundary.
792 * Throw away the leading part and see
795 newstart
= dontcross
+ 1;
796 newend
= newstart
+ (size
- 1);
797 dontcross
+= boundary
;
798 if (!LE_OV(newstart
, size
, rp
->er_start
))
803 * If we run past the end of
804 * the extent or the boundary
805 * overflows, then the request
808 if (newstart
+ size
- 1 > ex
->ex_end
||
809 dontcross
< newstart
)
814 * We would fit into this space. Calculate
815 * the overhead (wasted space). If we exactly
816 * fit, or we're taking the first fit, insert
817 * ourselves into the region list.
819 ovh
= rp
->er_start
- newstart
- size
;
820 if ((flags
& EX_FAST
) || (ovh
== 0))
824 * Don't exactly fit, but check to see
825 * if we're better than any current choice.
827 if ((bestovh
== 0) || (ovh
< bestovh
)) {
829 beststart
= newstart
;
836 * Skip past the current region and check again.
838 newstart
= EXTENT_ALIGN((rp
->er_end
+ 1), alignment
, skew
);
839 if (newstart
< rp
->er_end
) {
841 * Overflow condition. Don't error out, since
842 * we might have a chunk of space that we can
852 * The final check is from the current starting point to the
853 * end of the subregion. If there were no allocated regions,
854 * "newstart" is set to the beginning of the subregion, or
855 * just past the end of the last allocated region, adjusted
856 * for alignment in either case.
858 if (LE_OV(newstart
, (size
- 1), subend
)) {
860 * Do a boundary check, if necessary. Note
861 * that a region may *begin* on the boundary,
862 * but it must end before the boundary.
865 newend
= newstart
+ (size
- 1);
868 * Calculate the next boundary after the start
871 dontcross
= EXTENT_ALIGN(newstart
+1, boundary
,
872 (flags
& EX_BOUNDZERO
) ? 0 : ex
->ex_start
)
876 printf("newstart=%lx newend=%lx ex_start=%lx ex_end=%lx boundary=%lx dontcross=%lx\n",
877 newstart
, newend
, ex
->ex_start
, ex
->ex_end
,
878 boundary
, dontcross
);
881 /* Check for overflow */
882 if (dontcross
< ex
->ex_start
)
883 dontcross
= ex
->ex_end
;
884 else if (newend
> dontcross
) {
886 * Candidate region crosses boundary.
887 * Throw away the leading part and see
890 newstart
= dontcross
+ 1;
891 newend
= newstart
+ (size
- 1);
892 dontcross
+= boundary
;
893 if (!LE_OV(newstart
, (size
- 1), subend
))
898 * If we run past the end of
899 * the extent or the boundary
900 * overflows, then the request
903 if (newstart
+ size
- 1 > ex
->ex_end
||
904 dontcross
< newstart
)
909 * We would fit into this space. Calculate
910 * the overhead (wasted space). If we exactly
911 * fit, or we're taking the first fit, insert
912 * ourselves into the region list.
914 ovh
= exend
- newstart
- (size
- 1);
915 if ((flags
& EX_FAST
) || (ovh
== 0))
919 * Don't exactly fit, but check to see
920 * if we're better than any current choice.
922 if ((bestovh
== 0) || (ovh
< bestovh
)) {
924 beststart
= newstart
;
931 * One of the following two conditions have
934 * There is no chunk large enough to hold the request.
936 * If EX_FAST was not specified, there is not an
937 * exact match for the request.
939 * Note that if we reach this point and EX_FAST is
940 * set, then we know there is no space in the extent for
943 if (((flags
& EX_FAST
) == 0) && (bestovh
!= 0)) {
945 * We have a match that's "good enough".
947 newstart
= beststart
;
953 * No space currently available. Wait for it to free up,
956 if (flags
& EX_WAITSPACE
) {
957 if ((flags
& EX_CATCH
) != 0) {
958 error
= cv_wait_sig(&ex
->ex_cv
, &ex
->ex_lock
);
960 cv_wait(&ex
->ex_cv
, &ex
->ex_lock
);
965 mutex_exit(&ex
->ex_lock
);
967 mutex_exit(&ex
->ex_lock
);
971 extent_free_region_descriptor(ex
, myrp
);
976 * Insert ourselves into the region list.
978 extent_insert_and_optimize(ex
, newstart
, size
, flags
, last
, myrp
);
979 mutex_exit(&ex
->ex_lock
);
985 extent_alloc_subregion(struct extent
*ex
, u_long start
, u_long end
, u_long size
,
986 u_long alignment
, u_long boundary
, int flags
, u_long
*result
)
989 return (extent_alloc_subregion1(ex
, start
, end
, size
, alignment
,
990 0, boundary
, flags
, result
));
994 extent_alloc(struct extent
*ex
, u_long size
, u_long alignment
, u_long boundary
,
995 int flags
, u_long
*result
)
998 return (extent_alloc_subregion1(ex
, ex
->ex_start
, ex
->ex_end
,
999 size
, alignment
, 0, boundary
,
1004 extent_alloc1(struct extent
*ex
, u_long size
, u_long alignment
, u_long skew
,
1005 u_long boundary
, int flags
, u_long
*result
)
1008 return (extent_alloc_subregion1(ex
, ex
->ex_start
, ex
->ex_end
,
1009 size
, alignment
, skew
, boundary
,
1014 extent_free(struct extent
*ex
, u_long start
, u_long size
, int flags
)
1016 struct extent_region
*rp
, *nrp
= NULL
;
1017 u_long end
= start
+ (size
- 1);
1024 * We don't lock to check these, because these values
1025 * are never modified, and if another thread deletes the
1026 * extent, we're screwed anyway.
1029 panic("extent_free: NULL extent");
1030 if ((start
< ex
->ex_start
) || (end
> ex
->ex_end
)) {
1032 printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
1033 ex
->ex_name
, start
, size
);
1034 panic("extent_free: extent `%s', region not within extent",
1037 /* Check for an overflow. */
1040 printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
1041 ex
->ex_name
, start
, size
);
1042 panic("extent_free: overflow");
1047 * If we're allowing coalescing, we must allocate a region
1048 * descriptor now, since it might block.
1050 * XXX Make a static, create-time flags word, so we don't
1051 * XXX have to lock to read it!
1053 mutex_enter(&ex
->ex_lock
);
1054 coalesce
= (ex
->ex_flags
& EXF_NOCOALESCE
) == 0;
1055 mutex_exit(&ex
->ex_lock
);
1058 /* Allocate a region descriptor. */
1059 nrp
= extent_alloc_region_descriptor(ex
, flags
);
1064 mutex_enter(&ex
->ex_lock
);
1067 * Find region and deallocate. Several possibilities:
1069 * 1. (start == er_start) && (end == er_end):
1072 * 2. (start == er_start) && (end < er_end):
1075 * 3. (start > er_start) && (end == er_end):
1078 * 4. (start > er_start) && (end < er_end):
1079 * Fragment region. Requires descriptor alloc.
1081 * Cases 2, 3, and 4 require that the EXF_NOCOALESCE flag
1084 LIST_FOREACH(rp
, &ex
->ex_regions
, er_link
) {
1086 * Save ourselves some comparisons; does the current
1087 * region end before chunk to be freed begins? If so,
1088 * then we haven't found the appropriate region descriptor.
1090 if (rp
->er_end
< start
)
1094 * Save ourselves some traversal; does the current
1095 * region begin after the chunk to be freed ends? If so,
1096 * then we've already passed any possible region descriptors
1097 * that might have contained the chunk to be freed.
1099 if (rp
->er_start
> end
)
1103 if ((start
== rp
->er_start
) && (end
== rp
->er_end
)) {
1104 LIST_REMOVE(rp
, er_link
);
1105 extent_free_region_descriptor(ex
, rp
);
1110 * The following cases all require that EXF_NOCOALESCE
1117 if ((start
== rp
->er_start
) && (end
< rp
->er_end
)) {
1118 rp
->er_start
= (end
+ 1);
1123 if ((start
> rp
->er_start
) && (end
== rp
->er_end
)) {
1124 rp
->er_end
= (start
- 1);
1129 if ((start
> rp
->er_start
) && (end
< rp
->er_end
)) {
1130 /* Fill in new descriptor. */
1131 nrp
->er_start
= end
+ 1;
1132 nrp
->er_end
= rp
->er_end
;
1134 /* Adjust current descriptor. */
1135 rp
->er_end
= start
- 1;
1137 /* Insert new descriptor after current. */
1138 LIST_INSERT_AFTER(rp
, nrp
, er_link
);
1140 /* We used the new descriptor, so don't free it below */
1146 /* Region not found, or request otherwise invalid. */
1147 mutex_exit(&ex
->ex_lock
);
1149 printf("extent_free: start 0x%lx, end 0x%lx\n", start
, end
);
1150 panic("extent_free: region not found");
1154 extent_free_region_descriptor(ex
, nrp
);
1155 cv_broadcast(&ex
->ex_cv
);
1156 mutex_exit(&ex
->ex_lock
);
1161 extent_print(struct extent
*ex
)
1163 struct extent_region
*rp
;
1166 panic("extent_print: NULL extent");
1168 mutex_enter(&ex
->ex_lock
);
1170 printf("extent `%s' (0x%lx - 0x%lx), flags = 0x%x\n", ex
->ex_name
,
1171 ex
->ex_start
, ex
->ex_end
, ex
->ex_flags
);
1173 LIST_FOREACH(rp
, &ex
->ex_regions
, er_link
)
1174 printf(" 0x%lx - 0x%lx\n", rp
->er_start
, rp
->er_end
);
1176 mutex_exit(&ex
->ex_lock
);