vma.c revision 01e04c3f
101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2018 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#include <stdlib.h>
2501e04c3fSmrg
2601e04c3fSmrg#include "util/u_math.h"
2701e04c3fSmrg#include "util/vma.h"
2801e04c3fSmrg
2901e04c3fSmrgstruct util_vma_hole {
3001e04c3fSmrg   struct list_head link;
3101e04c3fSmrg   uint64_t offset;
3201e04c3fSmrg   uint64_t size;
3301e04c3fSmrg};
3401e04c3fSmrg
3501e04c3fSmrg#define util_vma_foreach_hole(_hole, _heap) \
3601e04c3fSmrg   list_for_each_entry(struct util_vma_hole, _hole, &(_heap)->holes, link)
3701e04c3fSmrg
3801e04c3fSmrg#define util_vma_foreach_hole_safe(_hole, _heap) \
3901e04c3fSmrg   list_for_each_entry_safe(struct util_vma_hole, _hole, &(_heap)->holes, link)
4001e04c3fSmrg
4101e04c3fSmrgvoid
4201e04c3fSmrgutil_vma_heap_init(struct util_vma_heap *heap,
4301e04c3fSmrg                   uint64_t start, uint64_t size)
4401e04c3fSmrg{
4501e04c3fSmrg   list_inithead(&heap->holes);
4601e04c3fSmrg   util_vma_heap_free(heap, start, size);
4701e04c3fSmrg}
4801e04c3fSmrg
4901e04c3fSmrgvoid
5001e04c3fSmrgutil_vma_heap_finish(struct util_vma_heap *heap)
5101e04c3fSmrg{
5201e04c3fSmrg   util_vma_foreach_hole_safe(hole, heap)
5301e04c3fSmrg      free(hole);
5401e04c3fSmrg}
5501e04c3fSmrg
5601e04c3fSmrg#ifndef NDEBUG
5701e04c3fSmrgstatic void
5801e04c3fSmrgutil_vma_heap_validate(struct util_vma_heap *heap)
5901e04c3fSmrg{
6001e04c3fSmrg   uint64_t prev_offset = 0;
6101e04c3fSmrg   util_vma_foreach_hole(hole, heap) {
6201e04c3fSmrg      assert(hole->offset > 0);
6301e04c3fSmrg      assert(hole->size > 0);
6401e04c3fSmrg
6501e04c3fSmrg      if (&hole->link == heap->holes.next) {
6601e04c3fSmrg         /* This must be the top-most hole.  Assert that, if it overflows, it
6701e04c3fSmrg          * overflows to 0, i.e. 2^64.
6801e04c3fSmrg          */
6901e04c3fSmrg         assert(hole->size + hole->offset == 0 ||
7001e04c3fSmrg                hole->size + hole->offset > hole->offset);
7101e04c3fSmrg      } else {
7201e04c3fSmrg         /* This is not the top-most hole so it must not overflow and, in
7301e04c3fSmrg          * fact, must be strictly lower than the top-most hole.  If
7401e04c3fSmrg          * hole->size + hole->offset == prev_offset, then we failed to join
7501e04c3fSmrg          * holes during a util_vma_heap_free.
7601e04c3fSmrg          */
7701e04c3fSmrg         assert(hole->size + hole->offset > hole->offset &&
7801e04c3fSmrg                hole->size + hole->offset < prev_offset);
7901e04c3fSmrg      }
8001e04c3fSmrg      prev_offset = hole->offset;
8101e04c3fSmrg   }
8201e04c3fSmrg}
8301e04c3fSmrg#else
8401e04c3fSmrg#define util_vma_heap_validate(heap)
8501e04c3fSmrg#endif
8601e04c3fSmrg
8701e04c3fSmrguint64_t
8801e04c3fSmrgutil_vma_heap_alloc(struct util_vma_heap *heap,
8901e04c3fSmrg                    uint64_t size, uint64_t alignment)
9001e04c3fSmrg{
9101e04c3fSmrg   /* The caller is expected to reject zero-size allocations */
9201e04c3fSmrg   assert(size > 0);
9301e04c3fSmrg   assert(alignment > 0);
9401e04c3fSmrg
9501e04c3fSmrg   util_vma_heap_validate(heap);
9601e04c3fSmrg
9701e04c3fSmrg   util_vma_foreach_hole_safe(hole, heap) {
9801e04c3fSmrg      if (size > hole->size)
9901e04c3fSmrg         continue;
10001e04c3fSmrg
10101e04c3fSmrg      /* Compute the offset as the highest address where a chunk of the given
10201e04c3fSmrg       * size can be without going over the top of the hole.
10301e04c3fSmrg       *
10401e04c3fSmrg       * This calculation is known to not overflow because we know that
10501e04c3fSmrg       * hole->size + hole->offset can only overflow to 0 and size > 0.
10601e04c3fSmrg       */
10701e04c3fSmrg      uint64_t offset = (hole->size - size) + hole->offset;
10801e04c3fSmrg
10901e04c3fSmrg      /* Align the offset.  We align down and not up because we are allocating
11001e04c3fSmrg       * from the top of the hole and not the bottom.
11101e04c3fSmrg       */
11201e04c3fSmrg      offset = (offset / alignment) * alignment;
11301e04c3fSmrg
11401e04c3fSmrg      if (offset < hole->offset)
11501e04c3fSmrg         continue;
11601e04c3fSmrg
11701e04c3fSmrg      if (offset == hole->offset && size == hole->size) {
11801e04c3fSmrg         /* Just get rid of the hole. */
11901e04c3fSmrg         list_del(&hole->link);
12001e04c3fSmrg         free(hole);
12101e04c3fSmrg         util_vma_heap_validate(heap);
12201e04c3fSmrg         return offset;
12301e04c3fSmrg      }
12401e04c3fSmrg
12501e04c3fSmrg      assert(offset - hole->offset <= hole->size - size);
12601e04c3fSmrg      uint64_t waste = (hole->size - size) - (offset - hole->offset);
12701e04c3fSmrg      if (waste == 0) {
12801e04c3fSmrg         /* We allocated at the top.  Shrink the hole down. */
12901e04c3fSmrg         hole->size -= size;
13001e04c3fSmrg         util_vma_heap_validate(heap);
13101e04c3fSmrg         return offset;
13201e04c3fSmrg      }
13301e04c3fSmrg
13401e04c3fSmrg      if (offset == hole->offset) {
13501e04c3fSmrg         /* We allocated at the bottom. Shrink the hole up. */
13601e04c3fSmrg         hole->offset += size;
13701e04c3fSmrg         hole->size -= size;
13801e04c3fSmrg         util_vma_heap_validate(heap);
13901e04c3fSmrg         return offset;
14001e04c3fSmrg      }
14101e04c3fSmrg
14201e04c3fSmrg      /* We allocated in the middle.  We need to split the old hole into two
14301e04c3fSmrg       * holes, one high and one low.
14401e04c3fSmrg       */
14501e04c3fSmrg      struct util_vma_hole *high_hole = calloc(1, sizeof(*hole));
14601e04c3fSmrg      high_hole->offset = offset + size;
14701e04c3fSmrg      high_hole->size = waste;
14801e04c3fSmrg
14901e04c3fSmrg      /* Adjust the hole to be the amount of space left at he bottom of the
15001e04c3fSmrg       * original hole.
15101e04c3fSmrg       */
15201e04c3fSmrg      hole->size = offset - hole->offset;
15301e04c3fSmrg
15401e04c3fSmrg      /* Place the new hole before the old hole so that the list is in order
15501e04c3fSmrg       * from high to low.
15601e04c3fSmrg       */
15701e04c3fSmrg      list_addtail(&high_hole->link, &hole->link);
15801e04c3fSmrg
15901e04c3fSmrg      util_vma_heap_validate(heap);
16001e04c3fSmrg
16101e04c3fSmrg      return offset;
16201e04c3fSmrg   }
16301e04c3fSmrg
16401e04c3fSmrg   /* Failed to allocate */
16501e04c3fSmrg   return 0;
16601e04c3fSmrg}
16701e04c3fSmrg
16801e04c3fSmrgvoid
16901e04c3fSmrgutil_vma_heap_free(struct util_vma_heap *heap,
17001e04c3fSmrg                   uint64_t offset, uint64_t size)
17101e04c3fSmrg{
17201e04c3fSmrg   /* An offset of 0 is reserved for allocation failure.  It is not a valid
17301e04c3fSmrg    * address and cannot be freed.
17401e04c3fSmrg    */
17501e04c3fSmrg   assert(offset > 0);
17601e04c3fSmrg
17701e04c3fSmrg   /* Freeing something with a size of 0 is also not valid. */
17801e04c3fSmrg   assert(size > 0);
17901e04c3fSmrg
18001e04c3fSmrg   /* It's possible for offset + size to wrap around if we touch the top of
18101e04c3fSmrg    * the 64-bit address space, but we cannot go any higher than 2^64.
18201e04c3fSmrg    */
18301e04c3fSmrg   assert(offset + size == 0 || offset + size > offset);
18401e04c3fSmrg
18501e04c3fSmrg   util_vma_heap_validate(heap);
18601e04c3fSmrg
18701e04c3fSmrg   /* Find immediately higher and lower holes if they exist. */
18801e04c3fSmrg   struct util_vma_hole *high_hole = NULL, *low_hole = NULL;
18901e04c3fSmrg   util_vma_foreach_hole(hole, heap) {
19001e04c3fSmrg      if (hole->offset <= offset) {
19101e04c3fSmrg         low_hole = hole;
19201e04c3fSmrg         break;
19301e04c3fSmrg      }
19401e04c3fSmrg      high_hole = hole;
19501e04c3fSmrg   }
19601e04c3fSmrg
19701e04c3fSmrg   if (high_hole)
19801e04c3fSmrg      assert(offset + size <= high_hole->offset);
19901e04c3fSmrg   bool high_adjacent = high_hole && offset + size == high_hole->offset;
20001e04c3fSmrg
20101e04c3fSmrg   if (low_hole) {
20201e04c3fSmrg      assert(low_hole->offset + low_hole->size > low_hole->offset);
20301e04c3fSmrg      assert(low_hole->offset + low_hole->size <= offset);
20401e04c3fSmrg   }
20501e04c3fSmrg   bool low_adjacent = low_hole && low_hole->offset + low_hole->size == offset;
20601e04c3fSmrg
20701e04c3fSmrg   if (low_adjacent && high_adjacent) {
20801e04c3fSmrg      /* Merge the two holes */
20901e04c3fSmrg      low_hole->size += size + high_hole->size;
21001e04c3fSmrg      list_del(&high_hole->link);
21101e04c3fSmrg      free(high_hole);
21201e04c3fSmrg   } else if (low_adjacent) {
21301e04c3fSmrg      /* Merge into the low hole */
21401e04c3fSmrg      low_hole->size += size;
21501e04c3fSmrg   } else if (high_adjacent) {
21601e04c3fSmrg      /* Merge into the high hole */
21701e04c3fSmrg      high_hole->offset = offset;
21801e04c3fSmrg      high_hole->size += size;
21901e04c3fSmrg   } else {
22001e04c3fSmrg      /* Neither hole is adjacent; make a new one */
22101e04c3fSmrg      struct util_vma_hole *hole = calloc(1, sizeof(*hole));
22201e04c3fSmrg
22301e04c3fSmrg      hole->offset = offset;
22401e04c3fSmrg      hole->size = size;
22501e04c3fSmrg
22601e04c3fSmrg      /* Add it after the high hole so we maintain high-to-low ordering */
22701e04c3fSmrg      if (high_hole)
22801e04c3fSmrg         list_add(&hole->link, &high_hole->link);
22901e04c3fSmrg      else
23001e04c3fSmrg         list_add(&hole->link, &heap->holes);
23101e04c3fSmrg   }
23201e04c3fSmrg
23301e04c3fSmrg   util_vma_heap_validate(heap);
23401e04c3fSmrg}
235