subr_extent.c revision 1.76.4.3 1 /* $NetBSD: subr_extent.c,v 1.76.4.3 2017/08/28 17:53:07 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1998, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Matthias Drochner.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 *
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.
30 */
31
32 /*
33 * General purpose extent manager.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: subr_extent.c,v 1.76.4.3 2017/08/28 17:53:07 skrll Exp $");
38
39 #ifdef _KERNEL
40 #ifdef _KERNEL_OPT
41 #include "opt_lockdebug.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/extent.h>
46 #include <sys/kmem.h>
47 #include <sys/pool.h>
48 #include <sys/time.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51
52 #include <uvm/uvm_extern.h>
53
54 #elif defined(_EXTENT_TESTING)
55
56 /*
57 * user-land definitions, so it can fit into a testing harness.
58 */
59 #include <sys/param.h>
60 #include <sys/pool.h>
61 #include <sys/extent.h>
62
63 #include <errno.h>
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <string.h>
67
68 static inline void no_op(void) { return; }
69
70 /*
71 * Use multi-line #defines to avoid screwing up the kernel tags file;
72 * without this, ctags produces a tags file where panic() shows up
73 * in subr_extent.c rather than subr_prf.c.
74 */
75 #define \
76 kmem_alloc(s, flags) malloc(s)
77 #define \
78 kmem_free(p, s) free(p)
79 #define \
80 cv_wait_sig(cv, lock) (EWOULDBLOCK)
81 #define \
82 pool_get(pool, flags) kmem_alloc((pool)->pr_size,0)
83 #define \
84 pool_put(pool, rp) kmem_free(rp,0)
85 #define \
86 panic(a ...) printf(a)
87 #define mutex_init(a, b, c) no_op()
88 #define mutex_destroy(a) no_op()
89 #define mutex_enter(l) no_op()
90 #define mutex_exit(l) no_op()
91 #define cv_wait(cv, lock) no_op()
92 #define cv_broadcast(cv) no_op()
93 #define cv_init(a, b) no_op()
94 #define cv_destroy(a) no_op()
95 #define KMEM_IS_RUNNING (1)
96 #define IPL_VM (0)
97 #define MUTEX_DEFAULT (0)
98 #define KASSERT(exp)
99 #endif
100
101 static struct pool expool;
102
103 /*
104 * Macro to align to an arbitrary power-of-two boundary.
105 */
106 #define EXTENT_ALIGN(_start, _align, _skew) \
107 (((((_start) - (_skew)) + ((_align) - 1)) & (-(_align))) + (_skew))
108
109 /*
110 * Create the extent_region pool.
111 */
112 void
113 extent_init(void)
114 {
115
116 #if defined(_KERNEL)
117 pool_init(&expool, sizeof(struct extent_region), 0, 0, 0,
118 "extent", NULL, IPL_VM);
119 #else
120 expool.pr_size = sizeof(struct extent_region);
121 #endif
122 }
123
124 /*
125 * Allocate an extent region descriptor. EXTENT MUST NOT BE LOCKED.
126 * We will handle any locking we may need.
127 */
128 static struct extent_region *
129 extent_alloc_region_descriptor(struct extent *ex, int flags)
130 {
131 struct extent_region *rp;
132 int error;
133
134 if (ex->ex_flags & EXF_FIXED) {
135 struct extent_fixed *fex = (struct extent_fixed *)ex;
136
137 if (!(ex->ex_flags & EXF_EARLY))
138 mutex_enter(&ex->ex_lock);
139 for (;;) {
140 if ((rp = LIST_FIRST(&fex->fex_freelist)) != NULL) {
141 /*
142 * Don't muck with flags after pulling it off
143 * the freelist; it may have been dynamically
144 * allocated, and kindly given to us. We
145 * need to remember that information.
146 */
147 LIST_REMOVE(rp, er_link);
148 if (!(ex->ex_flags & EXF_EARLY))
149 mutex_exit(&ex->ex_lock);
150 return (rp);
151 }
152 if (flags & EX_MALLOCOK) {
153 if (!(ex->ex_flags & EXF_EARLY))
154 mutex_exit(&ex->ex_lock);
155 goto alloc;
156 }
157 if ((flags & EX_WAITOK) == 0) {
158 if (!(ex->ex_flags & EXF_EARLY))
159 mutex_exit(&ex->ex_lock);
160 return (NULL);
161 }
162 KASSERT(mutex_owned(&ex->ex_lock));
163 ex->ex_flwanted = true;
164 if ((flags & EX_CATCH) != 0)
165 error = cv_wait_sig(&ex->ex_cv, &ex->ex_lock);
166 else {
167 cv_wait(&ex->ex_cv, &ex->ex_lock);
168 error = 0;
169 }
170 if (error != 0) {
171 mutex_exit(&ex->ex_lock);
172 return (NULL);
173 }
174 }
175 }
176
177 alloc:
178 rp = pool_get(&expool, (flags & EX_WAITOK) ? PR_WAITOK : 0);
179
180 if (rp != NULL)
181 rp->er_flags = ER_ALLOC;
182
183 return (rp);
184 }
185
186 /*
187 * Free an extent region descriptor. EXTENT _MUST_ BE LOCKED!
188 */
189 static void
190 extent_free_region_descriptor(struct extent *ex, struct extent_region *rp)
191 {
192
193 if (ex->ex_flags & EXF_FIXED) {
194 struct extent_fixed *fex = (struct extent_fixed *)ex;
195
196 /*
197 * If someone's waiting for a region descriptor,
198 * be nice and give them this one, rather than
199 * just free'ing it back to the system.
200 */
201 if (rp->er_flags & ER_ALLOC) {
202 if (ex->ex_flwanted) {
203 /* Clear all but ER_ALLOC flag. */
204 rp->er_flags = ER_ALLOC;
205 LIST_INSERT_HEAD(&fex->fex_freelist, rp,
206 er_link);
207 goto wake_em_up;
208 } else
209 pool_put(&expool, rp);
210 } else {
211 /* Clear all flags. */
212 rp->er_flags = 0;
213 LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
214 }
215
216 wake_em_up:
217 if (!(ex->ex_flags & EXF_EARLY)) {
218 ex->ex_flwanted = false;
219 cv_broadcast(&ex->ex_cv);
220 }
221 return;
222 }
223
224 /*
225 * We know it's dynamically allocated if we get here.
226 */
227 pool_put(&expool, rp);
228 }
229
230 /*
231 * Allocate and initialize an extent map.
232 */
233 struct extent *
234 extent_create(const char *name, u_long start, u_long end,
235 void *storage, size_t storagesize, int flags)
236 {
237 struct extent *ex;
238 char *cp = storage;
239 size_t sz = storagesize;
240 struct extent_region *rp;
241 int fixed_extent = (storage != NULL);
242
243 #ifndef _KERNEL
244 extent_init();
245 #endif
246
247 #ifdef DIAGNOSTIC
248 /* Check arguments. */
249 if (name == NULL)
250 panic("extent_create: name == NULL");
251 if (end < start) {
252 printf("extent_create: extent `%s', start 0x%lx, end 0x%lx\n",
253 name, start, end);
254 panic("extent_create: end < start");
255 }
256 if (fixed_extent && (storagesize < sizeof(struct extent_fixed)))
257 panic("extent_create: fixed extent, bad storagesize 0x%lx",
258 (u_long)storagesize);
259 if (fixed_extent == 0 && (storagesize != 0 || storage != NULL))
260 panic("extent_create: storage provided for non-fixed");
261 #endif
262
263 /* Allocate extent descriptor. */
264 if (fixed_extent) {
265 struct extent_fixed *fex;
266
267 memset(storage, 0, storagesize);
268
269 /*
270 * Align all descriptors on "long" boundaries.
271 */
272 fex = (struct extent_fixed *)cp;
273 ex = (struct extent *)fex;
274 cp += ALIGN(sizeof(struct extent_fixed));
275 sz -= ALIGN(sizeof(struct extent_fixed));
276 fex->fex_storage = storage;
277 fex->fex_storagesize = storagesize;
278
279 /*
280 * In a fixed extent, we have to pre-allocate region
281 * descriptors and place them in the extent's freelist.
282 */
283 LIST_INIT(&fex->fex_freelist);
284 while (sz >= ALIGN(sizeof(struct extent_region))) {
285 rp = (struct extent_region *)cp;
286 cp += ALIGN(sizeof(struct extent_region));
287 sz -= ALIGN(sizeof(struct extent_region));
288 LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
289 }
290 } else {
291 ex = kmem_alloc(sizeof(*ex),
292 (flags & EX_WAITOK) ? KM_SLEEP : KM_NOSLEEP);
293 if (ex == NULL)
294 return (NULL);
295 }
296
297 /* Fill in the extent descriptor and return it to the caller. */
298 mutex_init(&ex->ex_lock, MUTEX_DEFAULT, IPL_VM);
299 cv_init(&ex->ex_cv, "extent");
300 LIST_INIT(&ex->ex_regions);
301 ex->ex_name = name;
302 ex->ex_start = start;
303 ex->ex_end = end;
304 ex->ex_flags = 0;
305 ex->ex_flwanted = false;
306 if (fixed_extent)
307 ex->ex_flags |= EXF_FIXED;
308 if (flags & EX_NOCOALESCE)
309 ex->ex_flags |= EXF_NOCOALESCE;
310 if (flags & EX_EARLY)
311 ex->ex_flags |= EXF_EARLY;
312 return (ex);
313 }
314
315 /*
316 * Destroy an extent map.
317 * Since we're freeing the data, there can't be any references
318 * so we don't need any locking.
319 */
320 void
321 extent_destroy(struct extent *ex)
322 {
323 struct extent_region *rp, *orp;
324
325 #ifdef DIAGNOSTIC
326 /* Check arguments. */
327 if (ex == NULL)
328 panic("extent_destroy: NULL extent");
329 #endif
330
331 /* Free all region descriptors in extent. */
332 for (rp = LIST_FIRST(&ex->ex_regions); rp != NULL; ) {
333 orp = rp;
334 rp = LIST_NEXT(rp, er_link);
335 LIST_REMOVE(orp, er_link);
336 extent_free_region_descriptor(ex, orp);
337 }
338
339 cv_destroy(&ex->ex_cv);
340 mutex_destroy(&ex->ex_lock);
341
342 /* If we're not a fixed extent, free the extent descriptor itself. */
343 if ((ex->ex_flags & EXF_FIXED) == 0)
344 kmem_free(ex, sizeof(*ex));
345 }
346
347 /*
348 * Insert a region descriptor into the sorted region list after the
349 * entry "after" or at the head of the list (if "after" is NULL).
350 * The region descriptor we insert is passed in "rp". We must
351 * allocate the region descriptor before calling this function!
352 * If we don't need the region descriptor, it will be freed here.
353 */
354 static void
355 extent_insert_and_optimize(struct extent *ex, u_long start, u_long size,
356 int flags, struct extent_region *after, struct extent_region *rp)
357 {
358 struct extent_region *nextr;
359 int appended = 0;
360
361 if (after == NULL) {
362 /*
363 * We're the first in the region list. If there's
364 * a region after us, attempt to coalesce to save
365 * descriptor overhead.
366 */
367 if (((ex->ex_flags & EXF_NOCOALESCE) == 0) &&
368 (LIST_FIRST(&ex->ex_regions) != NULL) &&
369 ((start + size) == LIST_FIRST(&ex->ex_regions)->er_start)) {
370 /*
371 * We can coalesce. Prepend us to the first region.
372 */
373 LIST_FIRST(&ex->ex_regions)->er_start = start;
374 extent_free_region_descriptor(ex, rp);
375 return;
376 }
377
378 /*
379 * Can't coalesce. Fill in the region descriptor
380 * in, and insert us at the head of the region list.
381 */
382 rp->er_start = start;
383 rp->er_end = start + (size - 1);
384 LIST_INSERT_HEAD(&ex->ex_regions, rp, er_link);
385 return;
386 }
387
388 /*
389 * If EXF_NOCOALESCE is set, coalescing is disallowed.
390 */
391 if (ex->ex_flags & EXF_NOCOALESCE)
392 goto cant_coalesce;
393
394 /*
395 * Attempt to coalesce with the region before us.
396 */
397 if ((after->er_end + 1) == start) {
398 /*
399 * We can coalesce. Append ourselves and make
400 * note of it.
401 */
402 after->er_end = start + (size - 1);
403 appended = 1;
404 }
405
406 /*
407 * Attempt to coalesce with the region after us.
408 */
409 if ((LIST_NEXT(after, er_link) != NULL) &&
410 ((start + size) == LIST_NEXT(after, er_link)->er_start)) {
411 /*
412 * We can coalesce. Note that if we appended ourselves
413 * to the previous region, we exactly fit the gap, and
414 * can free the "next" region descriptor.
415 */
416 if (appended) {
417 /*
418 * Yup, we can free it up.
419 */
420 after->er_end = LIST_NEXT(after, er_link)->er_end;
421 nextr = LIST_NEXT(after, er_link);
422 LIST_REMOVE(nextr, er_link);
423 extent_free_region_descriptor(ex, nextr);
424 } else {
425 /*
426 * Nope, just prepend us to the next region.
427 */
428 LIST_NEXT(after, er_link)->er_start = start;
429 }
430
431 extent_free_region_descriptor(ex, rp);
432 return;
433 }
434
435 /*
436 * We weren't able to coalesce with the next region, but
437 * we don't need to allocate a region descriptor if we
438 * appended ourselves to the previous region.
439 */
440 if (appended) {
441 extent_free_region_descriptor(ex, rp);
442 return;
443 }
444
445 cant_coalesce:
446
447 /*
448 * Fill in the region descriptor and insert ourselves
449 * into the region list.
450 */
451 rp->er_start = start;
452 rp->er_end = start + (size - 1);
453 LIST_INSERT_AFTER(after, rp, er_link);
454 }
455
456 /*
457 * Allocate a specific region in an extent map.
458 */
459 int
460 extent_alloc_region(struct extent *ex, u_long start, u_long size, int flags)
461 {
462 struct extent_region *rp, *last, *myrp;
463 u_long end = start + (size - 1);
464 int error;
465
466 #ifdef DIAGNOSTIC
467 /* Check arguments. */
468 if (ex == NULL)
469 panic("extent_alloc_region: NULL extent");
470 if (size < 1) {
471 printf("extent_alloc_region: extent `%s', size 0x%lx\n",
472 ex->ex_name, size);
473 panic("extent_alloc_region: bad size");
474 }
475 if (end < start) {
476 printf(
477 "extent_alloc_region: extent `%s', start 0x%lx, size 0x%lx\n",
478 ex->ex_name, start, size);
479 panic("extent_alloc_region: overflow");
480 }
481 #endif
482 #ifdef LOCKDEBUG
483 if (flags & EX_WAITSPACE) {
484 ASSERT_SLEEPABLE();
485 }
486 #endif
487
488 /*
489 * Make sure the requested region lies within the
490 * extent.
491 *
492 * We don't lock to check the range, because those values
493 * are never modified, and if another thread deletes the
494 * extent, we're screwed anyway.
495 */
496 if ((start < ex->ex_start) || (end > ex->ex_end)) {
497 #ifdef DIAGNOSTIC
498 printf("extent_alloc_region: extent `%s' (0x%lx - 0x%lx)\n",
499 ex->ex_name, ex->ex_start, ex->ex_end);
500 printf("extent_alloc_region: start 0x%lx, end 0x%lx\n",
501 start, end);
502 panic("extent_alloc_region: region lies outside extent");
503 #else
504 return (EINVAL);
505 #endif
506 }
507
508 /*
509 * Allocate the region descriptor. It will be freed later
510 * if we can coalesce with another region. Don't lock before
511 * here! This could block.
512 */
513 myrp = extent_alloc_region_descriptor(ex, flags);
514 if (myrp == NULL) {
515 #ifdef DIAGNOSTIC
516 printf(
517 "extent_alloc_region: can't allocate region descriptor\n");
518 #endif
519 return (ENOMEM);
520 }
521
522 if (!(ex->ex_flags & EXF_EARLY))
523 mutex_enter(&ex->ex_lock);
524 alloc_start:
525
526 /*
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.
531 */
532
533 /*
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.
539 */
540 last = NULL;
541
542 LIST_FOREACH(rp, &ex->ex_regions, er_link) {
543 if (rp->er_start > end) {
544 /*
545 * We lie before this region and don't
546 * conflict.
547 */
548 break;
549 }
550
551 /*
552 * The current region begins before we end.
553 * Check for a conflict.
554 */
555 if (rp->er_end >= start) {
556 /*
557 * We conflict. If we can (and want to) wait,
558 * do so.
559 */
560 if (flags & EX_WAITSPACE) {
561 KASSERT(!(ex->ex_flags & EXF_EARLY));
562 if ((flags & EX_CATCH) != 0)
563 error = cv_wait_sig(&ex->ex_cv,
564 &ex->ex_lock);
565 else {
566 cv_wait(&ex->ex_cv, &ex->ex_lock);
567 error = 0;
568 }
569 if (error == 0)
570 goto alloc_start;
571 mutex_exit(&ex->ex_lock);
572 } else {
573 if (!(ex->ex_flags & EXF_EARLY))
574 mutex_exit(&ex->ex_lock);
575 error = EAGAIN;
576 }
577 extent_free_region_descriptor(ex, myrp);
578 return error;
579 }
580 /*
581 * We don't conflict, but this region lies before
582 * us. Keep a pointer to this region, and keep
583 * trying.
584 */
585 last = rp;
586 }
587
588 /*
589 * We don't conflict with any regions. "last" points
590 * to the region we fall after, or is NULL if we belong
591 * at the beginning of the region list. Insert ourselves.
592 */
593 extent_insert_and_optimize(ex, start, size, flags, last, myrp);
594 if (!(ex->ex_flags & EXF_EARLY))
595 mutex_exit(&ex->ex_lock);
596 return (0);
597 }
598
599 /*
600 * Macro to check (x + y) <= z. This check is designed to fail
601 * if an overflow occurs.
602 */
603 #define LE_OV(x, y, z) ((((x) + (y)) >= (x)) && (((x) + (y)) <= (z)))
604
605 /*
606 * Allocate a region in an extent map subregion.
607 *
608 * If EX_FAST is specified, we return the first fit in the map.
609 * Otherwise, we try to minimize fragmentation by finding the
610 * smallest gap that will hold the request.
611 *
612 * The allocated region is aligned to "alignment", which must be
613 * a power of 2.
614 */
615 int
616 extent_alloc_subregion1(struct extent *ex, u_long substart, u_long subend,
617 u_long size, u_long alignment, u_long skew, u_long boundary,
618 int flags, u_long *result)
619 {
620 struct extent_region *rp, *myrp, *last, *bestlast;
621 u_long newstart, newend, exend, beststart, bestovh, ovh;
622 u_long dontcross;
623 int error;
624
625 #ifdef DIAGNOSTIC
626 /*
627 * Check arguments.
628 *
629 * We don't lock to check these, because these values
630 * are never modified, and if another thread deletes the
631 * extent, we're screwed anyway.
632 */
633 if (ex == NULL)
634 panic("extent_alloc_subregion: NULL extent");
635 if (result == NULL)
636 panic("extent_alloc_subregion: NULL result pointer");
637 if ((substart < ex->ex_start) || (substart > ex->ex_end) ||
638 (subend > ex->ex_end) || (subend < ex->ex_start)) {
639 printf("extent_alloc_subregion: extent `%s', ex_start 0x%lx, ex_end 0x%lx\n",
640 ex->ex_name, ex->ex_start, ex->ex_end);
641 printf("extent_alloc_subregion: substart 0x%lx, subend 0x%lx\n",
642 substart, subend);
643 panic("extent_alloc_subregion: bad subregion");
644 }
645 if ((size < 1) || ((size - 1) > (subend - substart))) {
646 printf("extent_alloc_subregion: extent `%s', size 0x%lx\n",
647 ex->ex_name, size);
648 panic("extent_alloc_subregion: bad size");
649 }
650 if (alignment == 0)
651 panic("extent_alloc_subregion: bad alignment");
652 if (boundary && (boundary < size)) {
653 printf(
654 "extent_alloc_subregion: extent `%s', size 0x%lx, "
655 "boundary 0x%lx\n", ex->ex_name, size, boundary);
656 panic("extent_alloc_subregion: bad boundary");
657 }
658 #endif
659 #ifdef LOCKDEBUG
660 if (flags & EX_WAITSPACE) {
661 ASSERT_SLEEPABLE();
662 }
663 #endif
664
665 /*
666 * Allocate the region descriptor. It will be freed later
667 * if we can coalesce with another region. Don't lock before
668 * here! This could block.
669 */
670 myrp = extent_alloc_region_descriptor(ex, flags);
671 if (myrp == NULL) {
672 #ifdef DIAGNOSTIC
673 printf(
674 "extent_alloc_subregion: can't allocate region descriptor\n");
675 #endif
676 return (ENOMEM);
677 }
678
679 alloc_start:
680 mutex_enter(&ex->ex_lock);
681
682 /*
683 * Keep a pointer to the last region we looked at so
684 * that we don't have to traverse the list again when
685 * we insert ourselves. If "last" is NULL when we
686 * finally insert ourselves, we go at the head of the
687 * list. See extent_insert_and_optimize() for deatails.
688 */
689 last = NULL;
690
691 /*
692 * Keep track of size and location of the smallest
693 * chunk we fit in.
694 *
695 * Since the extent can be as large as the numeric range
696 * of the CPU (0 - 0xffffffff for 32-bit systems), the
697 * best overhead value can be the maximum unsigned integer.
698 * Thus, we initialize "bestovh" to 0, since we insert ourselves
699 * into the region list immediately on an exact match (which
700 * is the only case where "bestovh" would be set to 0).
701 */
702 bestovh = 0;
703 beststart = 0;
704 bestlast = NULL;
705
706 /*
707 * Keep track of end of free region. This is either the end of extent
708 * or the start of a region past the subend.
709 */
710 exend = ex->ex_end;
711
712 /*
713 * For N allocated regions, we must make (N + 1)
714 * checks for unallocated space. The first chunk we
715 * check is the area from the beginning of the subregion
716 * to the first allocated region after that point.
717 */
718 newstart = EXTENT_ALIGN(substart, alignment, skew);
719 if (newstart < ex->ex_start) {
720 #ifdef DIAGNOSTIC
721 printf(
722 "extent_alloc_subregion: extent `%s' (0x%lx - 0x%lx), alignment 0x%lx\n",
723 ex->ex_name, ex->ex_start, ex->ex_end, alignment);
724 mutex_exit(&ex->ex_lock);
725 panic("extent_alloc_subregion: overflow after alignment");
726 #else
727 extent_free_region_descriptor(ex, myrp);
728 mutex_exit(&ex->ex_lock);
729 return (EINVAL);
730 #endif
731 }
732
733 /*
734 * Find the first allocated region that begins on or after
735 * the subregion start, advancing the "last" pointer along
736 * the way.
737 */
738 LIST_FOREACH(rp, &ex->ex_regions, er_link) {
739 if (rp->er_start >= newstart)
740 break;
741 last = rp;
742 }
743
744 /*
745 * Relocate the start of our candidate region to the end of
746 * the last allocated region (if there was one overlapping
747 * our subrange).
748 */
749 if (last != NULL && last->er_end >= newstart)
750 newstart = EXTENT_ALIGN((last->er_end + 1), alignment, skew);
751
752 for (; rp != NULL; rp = LIST_NEXT(rp, er_link)) {
753 /*
754 * If the region pasts the subend, bail out and see
755 * if we fit against the subend.
756 */
757 if (rp->er_start > subend) {
758 exend = rp->er_start;
759 break;
760 }
761
762 /*
763 * Check the chunk before "rp". Note that our
764 * comparison is safe from overflow conditions.
765 */
766 if (LE_OV(newstart, size, rp->er_start)) {
767 /*
768 * Do a boundary check, if necessary. Note
769 * that a region may *begin* on the boundary,
770 * but it must end before the boundary.
771 */
772 if (boundary) {
773 newend = newstart + (size - 1);
774
775 /*
776 * Calculate the next boundary after the start
777 * of this region.
778 */
779 dontcross = EXTENT_ALIGN(newstart+1, boundary,
780 (flags & EX_BOUNDZERO) ? 0 : ex->ex_start)
781 - 1;
782
783 #if 0
784 printf("newstart=%lx newend=%lx ex_start=%lx ex_end=%lx boundary=%lx dontcross=%lx\n",
785 newstart, newend, ex->ex_start, ex->ex_end,
786 boundary, dontcross);
787 #endif
788
789 /* Check for overflow */
790 if (dontcross < ex->ex_start)
791 dontcross = ex->ex_end;
792 else if (newend > dontcross) {
793 /*
794 * Candidate region crosses boundary.
795 * Throw away the leading part and see
796 * if we still fit.
797 */
798 newstart = dontcross + 1;
799 newend = newstart + (size - 1);
800 dontcross += boundary;
801 if (!LE_OV(newstart, size, rp->er_start))
802 goto skip;
803 }
804
805 /*
806 * If we run past the end of
807 * the extent or the boundary
808 * overflows, then the request
809 * can't fit.
810 */
811 if (newstart + size - 1 > ex->ex_end ||
812 dontcross < newstart)
813 goto fail;
814 }
815
816 /*
817 * We would fit into this space. Calculate
818 * the overhead (wasted space). If we exactly
819 * fit, or we're taking the first fit, insert
820 * ourselves into the region list.
821 */
822 ovh = rp->er_start - newstart - size;
823 if ((flags & EX_FAST) || (ovh == 0))
824 goto found;
825
826 /*
827 * Don't exactly fit, but check to see
828 * if we're better than any current choice.
829 */
830 if ((bestovh == 0) || (ovh < bestovh)) {
831 bestovh = ovh;
832 beststart = newstart;
833 bestlast = last;
834 }
835 }
836
837 skip:
838 /*
839 * Skip past the current region and check again.
840 */
841 newstart = EXTENT_ALIGN((rp->er_end + 1), alignment, skew);
842 if (newstart < rp->er_end) {
843 /*
844 * Overflow condition. Don't error out, since
845 * we might have a chunk of space that we can
846 * use.
847 */
848 goto fail;
849 }
850
851 last = rp;
852 }
853
854 /*
855 * The final check is from the current starting point to the
856 * end of the subregion. If there were no allocated regions,
857 * "newstart" is set to the beginning of the subregion, or
858 * just past the end of the last allocated region, adjusted
859 * for alignment in either case.
860 */
861 if (LE_OV(newstart, (size - 1), subend)) {
862 /*
863 * Do a boundary check, if necessary. Note
864 * that a region may *begin* on the boundary,
865 * but it must end before the boundary.
866 */
867 if (boundary) {
868 newend = newstart + (size - 1);
869
870 /*
871 * Calculate the next boundary after the start
872 * of this region.
873 */
874 dontcross = EXTENT_ALIGN(newstart+1, boundary,
875 (flags & EX_BOUNDZERO) ? 0 : ex->ex_start)
876 - 1;
877
878 #if 0
879 printf("newstart=%lx newend=%lx ex_start=%lx ex_end=%lx boundary=%lx dontcross=%lx\n",
880 newstart, newend, ex->ex_start, ex->ex_end,
881 boundary, dontcross);
882 #endif
883
884 /* Check for overflow */
885 if (dontcross < ex->ex_start)
886 dontcross = ex->ex_end;
887 else if (newend > dontcross) {
888 /*
889 * Candidate region crosses boundary.
890 * Throw away the leading part and see
891 * if we still fit.
892 */
893 newstart = dontcross + 1;
894 newend = newstart + (size - 1);
895 dontcross += boundary;
896 if (!LE_OV(newstart, (size - 1), subend))
897 goto fail;
898 }
899
900 /*
901 * If we run past the end of
902 * the extent or the boundary
903 * overflows, then the request
904 * can't fit.
905 */
906 if (newstart + size - 1 > ex->ex_end ||
907 dontcross < newstart)
908 goto fail;
909 }
910
911 /*
912 * We would fit into this space. Calculate
913 * the overhead (wasted space). If we exactly
914 * fit, or we're taking the first fit, insert
915 * ourselves into the region list.
916 */
917 ovh = exend - newstart - (size - 1);
918 if ((flags & EX_FAST) || (ovh == 0))
919 goto found;
920
921 /*
922 * Don't exactly fit, but check to see
923 * if we're better than any current choice.
924 */
925 if ((bestovh == 0) || (ovh < bestovh)) {
926 bestovh = ovh;
927 beststart = newstart;
928 bestlast = last;
929 }
930 }
931
932 fail:
933 /*
934 * One of the following two conditions have
935 * occurred:
936 *
937 * There is no chunk large enough to hold the request.
938 *
939 * If EX_FAST was not specified, there is not an
940 * exact match for the request.
941 *
942 * Note that if we reach this point and EX_FAST is
943 * set, then we know there is no space in the extent for
944 * the request.
945 */
946 if (((flags & EX_FAST) == 0) && (bestovh != 0)) {
947 /*
948 * We have a match that's "good enough".
949 */
950 newstart = beststart;
951 last = bestlast;
952 goto found;
953 }
954
955 /*
956 * No space currently available. Wait for it to free up,
957 * if possible.
958 */
959 if (flags & EX_WAITSPACE) {
960 if ((flags & EX_CATCH) != 0) {
961 error = cv_wait_sig(&ex->ex_cv, &ex->ex_lock);
962 } else {
963 cv_wait(&ex->ex_cv, &ex->ex_lock);
964 error = 0;
965 }
966 if (error == 0)
967 goto alloc_start;
968 mutex_exit(&ex->ex_lock);
969 } else {
970 mutex_exit(&ex->ex_lock);
971 error = EAGAIN;
972 }
973
974 extent_free_region_descriptor(ex, myrp);
975 return error;
976
977 found:
978 /*
979 * Insert ourselves into the region list.
980 */
981 extent_insert_and_optimize(ex, newstart, size, flags, last, myrp);
982 mutex_exit(&ex->ex_lock);
983 *result = newstart;
984 return (0);
985 }
986
987 int
988 extent_alloc_subregion(struct extent *ex, u_long start, u_long end, u_long size,
989 u_long alignment, u_long boundary, int flags, u_long *result)
990 {
991
992 return (extent_alloc_subregion1(ex, start, end, size, alignment,
993 0, boundary, flags, result));
994 }
995
996 int
997 extent_alloc(struct extent *ex, u_long size, u_long alignment, u_long boundary,
998 int flags, u_long *result)
999 {
1000
1001 return (extent_alloc_subregion1(ex, ex->ex_start, ex->ex_end,
1002 size, alignment, 0, boundary,
1003 flags, result));
1004 }
1005
1006 int
1007 extent_alloc1(struct extent *ex, u_long size, u_long alignment, u_long skew,
1008 u_long boundary, int flags, u_long *result)
1009 {
1010
1011 return (extent_alloc_subregion1(ex, ex->ex_start, ex->ex_end,
1012 size, alignment, skew, boundary,
1013 flags, result));
1014 }
1015
1016 int
1017 extent_free(struct extent *ex, u_long start, u_long size, int flags)
1018 {
1019 struct extent_region *rp, *nrp = NULL;
1020 u_long end = start + (size - 1);
1021
1022 #ifdef DIAGNOSTIC
1023 /*
1024 * Check arguments.
1025 *
1026 * We don't lock to check these, because these values
1027 * are never modified, and if another thread deletes the
1028 * extent, we're screwed anyway.
1029 */
1030 if (ex == NULL)
1031 panic("extent_free: NULL extent");
1032 if ((start < ex->ex_start) || (end > ex->ex_end)) {
1033 extent_print(ex);
1034 printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
1035 ex->ex_name, start, size);
1036 panic("extent_free: extent `%s', region not within extent",
1037 ex->ex_name);
1038 }
1039 /* Check for an overflow. */
1040 if (end < start) {
1041 extent_print(ex);
1042 printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
1043 ex->ex_name, start, size);
1044 panic("extent_free: overflow");
1045 }
1046 #endif
1047
1048 /*
1049 * If we're allowing coalescing, we must allocate a region
1050 * descriptor now, since it might block.
1051 */
1052 const bool coalesce = (ex->ex_flags & EXF_NOCOALESCE) == 0;
1053
1054 if (coalesce) {
1055 /* Allocate a region descriptor. */
1056 nrp = extent_alloc_region_descriptor(ex, flags);
1057 if (nrp == NULL)
1058 return (ENOMEM);
1059 }
1060
1061 if (!(ex->ex_flags & EXF_EARLY))
1062 mutex_enter(&ex->ex_lock);
1063
1064 /*
1065 * Find region and deallocate. Several possibilities:
1066 *
1067 * 1. (start == er_start) && (end == er_end):
1068 * Free descriptor.
1069 *
1070 * 2. (start == er_start) && (end < er_end):
1071 * Adjust er_start.
1072 *
1073 * 3. (start > er_start) && (end == er_end):
1074 * Adjust er_end.
1075 *
1076 * 4. (start > er_start) && (end < er_end):
1077 * Fragment region. Requires descriptor alloc.
1078 *
1079 * Cases 2, 3, and 4 require that the EXF_NOCOALESCE flag
1080 * is not set.
1081 */
1082 LIST_FOREACH(rp, &ex->ex_regions, er_link) {
1083 /*
1084 * Save ourselves some comparisons; does the current
1085 * region end before chunk to be freed begins? If so,
1086 * then we haven't found the appropriate region descriptor.
1087 */
1088 if (rp->er_end < start)
1089 continue;
1090
1091 /*
1092 * Save ourselves some traversal; does the current
1093 * region begin after the chunk to be freed ends? If so,
1094 * then we've already passed any possible region descriptors
1095 * that might have contained the chunk to be freed.
1096 */
1097 if (rp->er_start > end)
1098 break;
1099
1100 /* Case 1. */
1101 if ((start == rp->er_start) && (end == rp->er_end)) {
1102 LIST_REMOVE(rp, er_link);
1103 extent_free_region_descriptor(ex, rp);
1104 goto done;
1105 }
1106
1107 /*
1108 * The following cases all require that EXF_NOCOALESCE
1109 * is not set.
1110 */
1111 if (!coalesce)
1112 continue;
1113
1114 /* Case 2. */
1115 if ((start == rp->er_start) && (end < rp->er_end)) {
1116 rp->er_start = (end + 1);
1117 goto done;
1118 }
1119
1120 /* Case 3. */
1121 if ((start > rp->er_start) && (end == rp->er_end)) {
1122 rp->er_end = (start - 1);
1123 goto done;
1124 }
1125
1126 /* Case 4. */
1127 if ((start > rp->er_start) && (end < rp->er_end)) {
1128 /* Fill in new descriptor. */
1129 nrp->er_start = end + 1;
1130 nrp->er_end = rp->er_end;
1131
1132 /* Adjust current descriptor. */
1133 rp->er_end = start - 1;
1134
1135 /* Insert new descriptor after current. */
1136 LIST_INSERT_AFTER(rp, nrp, er_link);
1137
1138 /* We used the new descriptor, so don't free it below */
1139 nrp = NULL;
1140 goto done;
1141 }
1142 }
1143
1144 /* Region not found, or request otherwise invalid. */
1145 if (!(ex->ex_flags & EXF_EARLY))
1146 mutex_exit(&ex->ex_lock);
1147 extent_print(ex);
1148 printf("extent_free: start 0x%lx, end 0x%lx\n", start, end);
1149 panic("extent_free: region not found");
1150
1151 done:
1152 if (nrp != NULL)
1153 extent_free_region_descriptor(ex, nrp);
1154 if (!(ex->ex_flags & EXF_EARLY)) {
1155 cv_broadcast(&ex->ex_cv);
1156 mutex_exit(&ex->ex_lock);
1157 }
1158 return (0);
1159 }
1160
1161 void
1162 extent_print(struct extent *ex)
1163 {
1164 struct extent_region *rp;
1165
1166 if (ex == NULL)
1167 panic("extent_print: NULL extent");
1168
1169 mutex_enter(&ex->ex_lock);
1170
1171 printf("extent `%s' (0x%lx - 0x%lx), flags = 0x%x\n", ex->ex_name,
1172 ex->ex_start, ex->ex_end, ex->ex_flags);
1173
1174 LIST_FOREACH(rp, &ex->ex_regions, er_link)
1175 printf(" 0x%lx - 0x%lx\n", rp->er_start, rp->er_end);
1176
1177 mutex_exit(&ex->ex_lock);
1178 }
1179