anv: setup BO flags at state_pool/block_pool creation
[mesa.git] / src / intel / vulkan / anv_allocator.c
index 97bcb0170ba4e283c2aec55135203d34ced27523..8ed32b3c6731f5ecb919fbae2fc3eb3b1f879ced 100644 (file)
  * IN THE SOFTWARE.
  */
 
-#include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <limits.h>
 #include <assert.h>
-#include <linux/futex.h>
 #include <linux/memfd.h>
-#include <sys/time.h>
 #include <sys/mman.h>
-#include <sys/syscall.h>
 
 #include "anv_private.h"
 
 #include "util/hash_table.h"
+#include "util/simple_mtx.h"
 
 #ifdef HAVE_VALGRIND
 #define VG_NOACCESS_READ(__ptr) ({                       \
@@ -112,25 +109,6 @@ struct anv_mmap_cleanup {
 
 #define ANV_MMAP_CLEANUP_INIT ((struct anv_mmap_cleanup){0})
 
-static inline long
-sys_futex(void *addr1, int op, int val1,
-          struct timespec *timeout, void *addr2, int val3)
-{
-   return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
-}
-
-static inline int
-futex_wake(uint32_t *addr, int count)
-{
-   return sys_futex(addr, FUTEX_WAKE, count, NULL, NULL, 0);
-}
-
-static inline int
-futex_wait(uint32_t *addr, int32_t value)
-{
-   return sys_futex(addr, FUTEX_WAIT, value, NULL, NULL, 0);
-}
-
 static inline int
 memfd_create(const char *name, unsigned int flags)
 {
@@ -179,11 +157,22 @@ anv_free_list_pop(union anv_free_list *list, void **map, int32_t *offset)
 }
 
 static void
-anv_free_list_push(union anv_free_list *list, void *map, int32_t offset)
+anv_free_list_push(union anv_free_list *list, void *map, int32_t offset,
+                   uint32_t size, uint32_t count)
 {
    union anv_free_list current, old, new;
    int32_t *next_ptr = map + offset;
 
+   /* If we're returning more than one chunk, we need to build a chain to add
+    * to the list.  Fortunately, we can do this without any atomics since we
+    * own everything in the chain right now.  `offset` is left pointing to the
+    * head of our chain list while `next_ptr` points to the tail.
+    */
+   for (uint32_t i = 1; i < count; i++) {
+      VG_NOACCESS_WRITE(next_ptr, offset + i * size);
+      next_ptr = map + offset + i * size;
+   }
+
    old = *list;
    do {
       current = old;
@@ -245,22 +234,21 @@ anv_ptr_free_list_push(void **list, void *elem)
    } while (old != current);
 }
 
-static uint32_t
-anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state);
+static VkResult
+anv_block_pool_expand_range(struct anv_block_pool *pool,
+                            uint32_t center_bo_offset, uint32_t size);
 
 VkResult
 anv_block_pool_init(struct anv_block_pool *pool,
-                    struct anv_device *device, uint32_t block_size)
+                    struct anv_device *device,
+                    uint32_t initial_size,
+                    uint64_t bo_flags)
 {
    VkResult result;
 
-   assert(util_is_power_of_two(block_size));
-
    pool->device = device;
+   pool->bo_flags = bo_flags;
    anv_bo_init(&pool->bo, 0, 0);
-   pool->block_size = block_size;
-   pool->free_list = ANV_FREE_LIST_EMPTY;
-   pool->back_free_list = ANV_FREE_LIST_EMPTY;
 
    pool->fd = memfd_create("block pool", MFD_CLOEXEC);
    if (pool->fd == -1)
@@ -287,11 +275,14 @@ anv_block_pool_init(struct anv_block_pool *pool,
    pool->back_state.next = 0;
    pool->back_state.end = 0;
 
-   /* Immediately grow the pool so we'll have a backing bo. */
-   pool->state.end = anv_block_pool_grow(pool, &pool->state);
+   result = anv_block_pool_expand_range(pool, 0, initial_size);
+   if (result != VK_SUCCESS)
+      goto fail_mmap_cleanups;
 
    return VK_SUCCESS;
 
+ fail_mmap_cleanups:
+   u_vector_finish(&pool->mmap_cleanups);
  fail_fd:
    close(pool->fd);
 
@@ -329,6 +320,11 @@ anv_block_pool_expand_range(struct anv_block_pool *pool,
    assert(center_bo_offset >= pool->back_state.end);
    assert(size - center_bo_offset >= pool->state.end);
 
+   /* Assert that we don't go outside the bounds of the memfd */
+   assert(center_bo_offset <= BLOCK_POOL_MEMFD_CENTER);
+   assert(size - center_bo_offset <=
+          BLOCK_POOL_MEMFD_SIZE - BLOCK_POOL_MEMFD_CENTER);
+
    cleanup = u_vector_add(&pool->mmap_cleanups);
    if (!cleanup)
       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
@@ -345,12 +341,14 @@ anv_block_pool_expand_range(struct anv_block_pool *pool,
               MAP_SHARED | MAP_POPULATE, pool->fd,
               BLOCK_POOL_MEMFD_CENTER - center_bo_offset);
    if (map == MAP_FAILED)
-      return vk_errorf(VK_ERROR_MEMORY_MAP_FAILED, "mmap failed: %m");
+      return vk_errorf(pool->device->instance, pool->device,
+                       VK_ERROR_MEMORY_MAP_FAILED, "mmap failed: %m");
 
    gem_handle = anv_gem_userptr(pool->device, map, size);
    if (gem_handle == 0) {
       munmap(map, size);
-      return vk_errorf(VK_ERROR_TOO_MANY_OBJECTS, "userptr failed: %m");
+      return vk_errorf(pool->device->instance, pool->device,
+                       VK_ERROR_TOO_MANY_OBJECTS, "userptr failed: %m");
    }
 
    cleanup->map = map;
@@ -402,6 +400,7 @@ anv_block_pool_expand_range(struct anv_block_pool *pool,
     * hard work for us.
     */
    anv_bo_init(&pool->bo, gem_handle, size);
+   pool->bo.flags = pool->bo_flags;
    pool->bo.map = map;
 
    return VK_SUCCESS;
@@ -434,7 +433,6 @@ anv_block_pool_expand_range(struct anv_block_pool *pool,
 static uint32_t
 anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state)
 {
-   uint32_t size;
    VkResult result = VK_SUCCESS;
 
    pthread_mutex_lock(&pool->device->mutex);
@@ -459,9 +457,19 @@ anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state)
 
    uint32_t old_size = pool->bo.size;
 
-   if (old_size != 0 &&
-       back_used * 2 <= pool->center_bo_offset &&
-       front_used * 2 <= (old_size - pool->center_bo_offset)) {
+   /* The block pool is always initialized to a nonzero size and this function
+    * is always called after initialization.
+    */
+   assert(old_size > 0);
+
+   /* The back_used and front_used may actually be smaller than the actual
+    * requirement because they are based on the next pointers which are
+    * updated prior to calling this function.
+    */
+   uint32_t back_required = MAX2(back_used, pool->center_bo_offset);
+   uint32_t front_required = MAX2(front_used, old_size - pool->center_bo_offset);
+
+   if (back_used * 2 <= back_required && front_used * 2 <= front_required) {
       /* If we're in this case then this isn't the firsta allocation and we
        * already have enough space on both sides to hold double what we
        * have allocated.  There's nothing for us to do.
@@ -469,18 +477,11 @@ anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state)
       goto done;
    }
 
-   if (old_size == 0) {
-      /* This is the first allocation */
-      size = MAX2(32 * pool->block_size, PAGE_SIZE);
-   } else {
-      size = old_size * 2;
-   }
+   uint32_t size = old_size * 2;
+   while (size < back_required + front_required)
+      size *= 2;
 
-   /* We can't have a block pool bigger than 1GB because we use signed
-    * 32-bit offsets in the free list and we don't want overflow.  We
-    * should never need a block pool bigger than 1GB anyway.
-    */
-   assert(size <= (1u << 31));
+   assert(size > pool->bo.size);
 
    /* We compute a new center_bo_offset such that, when we double the size
     * of the pool, we maintain the ratio of how much is used by each side.
@@ -499,10 +500,8 @@ anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state)
        */
       center_bo_offset = ((uint64_t)size * back_used) / total_used;
 
-      /* Align down to a multiple of both the block size and page size */
-      uint32_t granularity = MAX2(pool->block_size, PAGE_SIZE);
-      assert(util_is_power_of_two(granularity));
-      center_bo_offset &= ~(granularity - 1);
+      /* Align down to a multiple of the page size */
+      center_bo_offset &= ~(PAGE_SIZE - 1);
 
       assert(center_bo_offset >= back_used);
 
@@ -515,13 +514,11 @@ anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state)
          center_bo_offset = size - pool->state.end;
    }
 
-   assert(center_bo_offset % pool->block_size == 0);
    assert(center_bo_offset % PAGE_SIZE == 0);
 
    result = anv_block_pool_expand_range(pool, center_bo_offset, size);
 
-   if (pool->device->instance->physicalDevice.has_exec_async)
-      pool->bo.flags |= EXEC_OBJECT_ASYNC;
+   pool->bo.flags = pool->bo_flags;
 
 done:
    pthread_mutex_unlock(&pool->device->mutex);
@@ -544,47 +541,43 @@ done:
 
 static uint32_t
 anv_block_pool_alloc_new(struct anv_block_pool *pool,
-                         struct anv_block_state *pool_state)
+                         struct anv_block_state *pool_state,
+                         uint32_t block_size)
 {
    struct anv_block_state state, old, new;
 
    while (1) {
-      state.u64 = __sync_fetch_and_add(&pool_state->u64, pool->block_size);
-      if (state.next < state.end) {
+      state.u64 = __sync_fetch_and_add(&pool_state->u64, block_size);
+      if (state.next + block_size <= state.end) {
          assert(pool->map);
          return state.next;
-      } else if (state.next == state.end) {
-         /* We allocated the first block outside the pool, we have to grow it.
-          * pool_state->next acts a mutex: threads who try to allocate now will
-          * get block indexes above the current limit and hit futex_wait
-          * below. */
-         new.next = state.next + pool->block_size;
-         new.end = anv_block_pool_grow(pool, pool_state);
-         assert(new.end >= new.next && new.end % pool->block_size == 0);
+      } else if (state.next <= state.end) {
+         /* We allocated the first block outside the pool so we have to grow
+          * the pool.  pool_state->next acts a mutex: threads who try to
+          * allocate now will get block indexes above the current limit and
+          * hit futex_wait below.
+          */
+         new.next = state.next + block_size;
+         do {
+            new.end = anv_block_pool_grow(pool, pool_state);
+         } while (new.end < new.next);
+
          old.u64 = __sync_lock_test_and_set(&pool_state->u64, new.u64);
          if (old.next != state.next)
             futex_wake(&pool_state->end, INT_MAX);
          return state.next;
       } else {
-         futex_wait(&pool_state->end, state.end);
+         futex_wait(&pool_state->end, state.end, NULL);
          continue;
       }
    }
 }
 
 int32_t
-anv_block_pool_alloc(struct anv_block_pool *pool)
+anv_block_pool_alloc(struct anv_block_pool *pool,
+                     uint32_t block_size)
 {
-   int32_t offset;
-
-   /* Try free list first. */
-   if (anv_free_list_pop(&pool->free_list, &pool->map, &offset)) {
-      assert(offset >= 0);
-      assert(pool->map);
-      return offset;
-   }
-
-   return anv_block_pool_alloc_new(pool, &pool->state);
+   return anv_block_pool_alloc_new(pool, &pool->state, block_size);
 }
 
 /* Allocates a block out of the back of the block pool.
@@ -597,18 +590,11 @@ anv_block_pool_alloc(struct anv_block_pool *pool)
  * gymnastics with the block pool's BO when doing relocations.
  */
 int32_t
-anv_block_pool_alloc_back(struct anv_block_pool *pool)
+anv_block_pool_alloc_back(struct anv_block_pool *pool,
+                          uint32_t block_size)
 {
-   int32_t offset;
-
-   /* Try free list first. */
-   if (anv_free_list_pop(&pool->back_free_list, &pool->map, &offset)) {
-      assert(offset < 0);
-      assert(pool->map);
-      return offset;
-   }
-
-   offset = anv_block_pool_alloc_new(pool, &pool->back_state);
+   int32_t offset = anv_block_pool_alloc_new(pool, &pool->back_state,
+                                             block_size);
 
    /* The offset we get out of anv_block_pool_alloc_new() is actually the
     * number of bytes downwards from the middle to the end of the block.
@@ -616,91 +602,178 @@ anv_block_pool_alloc_back(struct anv_block_pool *pool)
     * start of the block.
     */
    assert(offset >= 0);
-   return -(offset + pool->block_size);
-}
-
-void
-anv_block_pool_free(struct anv_block_pool *pool, int32_t offset)
-{
-   if (offset < 0) {
-      anv_free_list_push(&pool->back_free_list, pool->map, offset);
-   } else {
-      anv_free_list_push(&pool->free_list, pool->map, offset);
-   }
+   return -(offset + block_size);
 }
 
-void
+VkResult
 anv_state_pool_init(struct anv_state_pool *pool,
-                    struct anv_block_pool *block_pool)
+                    struct anv_device *device,
+                    uint32_t block_size,
+                    uint64_t bo_flags)
 {
-   pool->block_pool = block_pool;
+   VkResult result = anv_block_pool_init(&pool->block_pool, device,
+                                         block_size * 16,
+                                         bo_flags);
+   if (result != VK_SUCCESS)
+      return result;
+
+   assert(util_is_power_of_two(block_size));
+   pool->block_size = block_size;
+   pool->back_alloc_free_list = ANV_FREE_LIST_EMPTY;
    for (unsigned i = 0; i < ANV_STATE_BUCKETS; i++) {
       pool->buckets[i].free_list = ANV_FREE_LIST_EMPTY;
       pool->buckets[i].block.next = 0;
       pool->buckets[i].block.end = 0;
    }
    VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
+
+   return VK_SUCCESS;
 }
 
 void
 anv_state_pool_finish(struct anv_state_pool *pool)
 {
    VG(VALGRIND_DESTROY_MEMPOOL(pool));
+   anv_block_pool_finish(&pool->block_pool);
 }
 
 static uint32_t
 anv_fixed_size_state_pool_alloc_new(struct anv_fixed_size_state_pool *pool,
                                     struct anv_block_pool *block_pool,
-                                    uint32_t state_size)
+                                    uint32_t state_size,
+                                    uint32_t block_size)
 {
    struct anv_block_state block, old, new;
    uint32_t offset;
 
+   /* If our state is large, we don't need any sub-allocation from a block.
+    * Instead, we just grab whole (potentially large) blocks.
+    */
+   if (state_size >= block_size)
+      return anv_block_pool_alloc(block_pool, state_size);
+
  restart:
    block.u64 = __sync_fetch_and_add(&pool->block.u64, state_size);
 
    if (block.next < block.end) {
       return block.next;
    } else if (block.next == block.end) {
-      offset = anv_block_pool_alloc(block_pool);
+      offset = anv_block_pool_alloc(block_pool, block_size);
       new.next = offset + state_size;
-      new.end = offset + block_pool->block_size;
+      new.end = offset + block_size;
       old.u64 = __sync_lock_test_and_set(&pool->block.u64, new.u64);
       if (old.next != block.next)
          futex_wake(&pool->block.end, INT_MAX);
       return offset;
    } else {
-      futex_wait(&pool->block.end, block.end);
+      futex_wait(&pool->block.end, block.end, NULL);
       goto restart;
    }
 }
 
-static struct anv_state
-anv_state_pool_alloc_no_vg(struct anv_state_pool *pool,
-                           uint32_t size, uint32_t align)
+static uint32_t
+anv_state_pool_get_bucket(uint32_t size)
 {
-   unsigned size_log2 = ilog2_round_up(size < align ? align : size);
+   unsigned size_log2 = ilog2_round_up(size);
    assert(size_log2 <= ANV_MAX_STATE_SIZE_LOG2);
    if (size_log2 < ANV_MIN_STATE_SIZE_LOG2)
       size_log2 = ANV_MIN_STATE_SIZE_LOG2;
-   unsigned bucket = size_log2 - ANV_MIN_STATE_SIZE_LOG2;
+   return size_log2 - ANV_MIN_STATE_SIZE_LOG2;
+}
+
+static uint32_t
+anv_state_pool_get_bucket_size(uint32_t bucket)
+{
+   uint32_t size_log2 = bucket + ANV_MIN_STATE_SIZE_LOG2;
+   return 1 << size_log2;
+}
+
+static struct anv_state
+anv_state_pool_alloc_no_vg(struct anv_state_pool *pool,
+                           uint32_t size, uint32_t align)
+{
+   uint32_t bucket = anv_state_pool_get_bucket(MAX2(size, align));
 
    struct anv_state state;
-   state.alloc_size = 1 << size_log2;
+   state.alloc_size = anv_state_pool_get_bucket_size(bucket);
 
    /* Try free list first. */
    if (anv_free_list_pop(&pool->buckets[bucket].free_list,
-                         &pool->block_pool->map, &state.offset)) {
+                         &pool->block_pool.map, &state.offset)) {
       assert(state.offset >= 0);
       goto done;
    }
 
+   /* Try to grab a chunk from some larger bucket and split it up */
+   for (unsigned b = bucket + 1; b < ANV_STATE_BUCKETS; b++) {
+      int32_t chunk_offset;
+      if (anv_free_list_pop(&pool->buckets[b].free_list,
+                            &pool->block_pool.map, &chunk_offset)) {
+         unsigned chunk_size = anv_state_pool_get_bucket_size(b);
+
+         /* We've found a chunk that's larger than the requested state size.
+          * There are a couple of options as to what we do with it:
+          *
+          *    1) We could fully split the chunk into state.alloc_size sized
+          *       pieces.  However, this would mean that allocating a 16B
+          *       state could potentially split a 2MB chunk into 512K smaller
+          *       chunks.  This would lead to unnecessary fragmentation.
+          *
+          *    2) The classic "buddy allocator" method would have us split the
+          *       chunk in half and return one half.  Then we would split the
+          *       remaining half in half and return one half, and repeat as
+          *       needed until we get down to the size we want.  However, if
+          *       you are allocating a bunch of the same size state (which is
+          *       the common case), this means that every other allocation has
+          *       to go up a level and every fourth goes up two levels, etc.
+          *       This is not nearly as efficient as it could be if we did a
+          *       little more work up-front.
+          *
+          *    3) Split the difference between (1) and (2) by doing a
+          *       two-level split.  If it's bigger than some fixed block_size,
+          *       we split it into block_size sized chunks and return all but
+          *       one of them.  Then we split what remains into
+          *       state.alloc_size sized chunks and return all but one.
+          *
+          * We choose option (3).
+          */
+         if (chunk_size > pool->block_size &&
+             state.alloc_size < pool->block_size) {
+            assert(chunk_size % pool->block_size == 0);
+            /* We don't want to split giant chunks into tiny chunks.  Instead,
+             * break anything bigger than a block into block-sized chunks and
+             * then break it down into bucket-sized chunks from there.  Return
+             * all but the first block of the chunk to the block bucket.
+             */
+            const uint32_t block_bucket =
+               anv_state_pool_get_bucket(pool->block_size);
+            anv_free_list_push(&pool->buckets[block_bucket].free_list,
+                               pool->block_pool.map,
+                               chunk_offset + pool->block_size,
+                               pool->block_size,
+                               (chunk_size / pool->block_size) - 1);
+            chunk_size = pool->block_size;
+         }
+
+         assert(chunk_size % state.alloc_size == 0);
+         anv_free_list_push(&pool->buckets[bucket].free_list,
+                            pool->block_pool.map,
+                            chunk_offset + state.alloc_size,
+                            state.alloc_size,
+                            (chunk_size / state.alloc_size) - 1);
+
+         state.offset = chunk_offset;
+         goto done;
+      }
+   }
+
    state.offset = anv_fixed_size_state_pool_alloc_new(&pool->buckets[bucket],
-                                                      pool->block_pool,
-                                                      state.alloc_size);
+                                                      &pool->block_pool,
+                                                      state.alloc_size,
+                                                      pool->block_size);
 
 done:
-   state.map = pool->block_pool->map + state.offset;
+   state.map = pool->block_pool.map + state.offset;
    return state;
 }
 
@@ -715,17 +788,43 @@ anv_state_pool_alloc(struct anv_state_pool *pool, uint32_t size, uint32_t align)
    return state;
 }
 
+struct anv_state
+anv_state_pool_alloc_back(struct anv_state_pool *pool)
+{
+   struct anv_state state;
+   state.alloc_size = pool->block_size;
+
+   if (anv_free_list_pop(&pool->back_alloc_free_list,
+                         &pool->block_pool.map, &state.offset)) {
+      assert(state.offset < 0);
+      goto done;
+   }
+
+   state.offset = anv_block_pool_alloc_back(&pool->block_pool,
+                                            pool->block_size);
+
+done:
+   state.map = pool->block_pool.map + state.offset;
+   VG(VALGRIND_MEMPOOL_ALLOC(pool, state.map, state.alloc_size));
+   return state;
+}
+
 static void
 anv_state_pool_free_no_vg(struct anv_state_pool *pool, struct anv_state state)
 {
    assert(util_is_power_of_two(state.alloc_size));
-   unsigned size_log2 = ilog2_round_up(state.alloc_size);
-   assert(size_log2 >= ANV_MIN_STATE_SIZE_LOG2 &&
-          size_log2 <= ANV_MAX_STATE_SIZE_LOG2);
-   unsigned bucket = size_log2 - ANV_MIN_STATE_SIZE_LOG2;
+   unsigned bucket = anv_state_pool_get_bucket(state.alloc_size);
 
-   anv_free_list_push(&pool->buckets[bucket].free_list,
-                      pool->block_pool->map, state.offset);
+   if (state.offset < 0) {
+      assert(state.alloc_size == pool->block_size);
+      anv_free_list_push(&pool->back_alloc_free_list,
+                         pool->block_pool.map, state.offset,
+                         state.alloc_size, 1);
+   } else {
+      anv_free_list_push(&pool->buckets[bucket].free_list,
+                         pool->block_pool.map, state.offset,
+                         state.alloc_size, 1);
+   }
 }
 
 void
@@ -800,16 +899,19 @@ anv_state_stream_alloc(struct anv_state_stream *stream,
    assert(alignment <= PAGE_SIZE);
 
    uint32_t offset = align_u32(stream->next, alignment);
-   if (offset + size > stream->block_size) {
+   if (offset + size > stream->block.alloc_size) {
+      uint32_t block_size = stream->block_size;
+      if (block_size < size)
+         block_size = round_to_power_of_two(size);
+
       stream->block = anv_state_pool_alloc_no_vg(stream->state_pool,
-                                                 stream->block_size,
-                                                 PAGE_SIZE);
+                                                 block_size, PAGE_SIZE);
 
       struct anv_state_stream_block *sb = stream->block.map;
       VG_NOACCESS_WRITE(&sb->block, stream->block);
       VG_NOACCESS_WRITE(&sb->next, stream->block_list);
       stream->block_list = sb;
-      VG_NOACCESS_WRITE(&sb->_vg_ptr, NULL);
+      VG(VG_NOACCESS_WRITE(&sb->_vg_ptr, NULL));
 
       VG(VALGRIND_MAKE_MEM_NOACCESS(stream->block.map, stream->block_size));
 
@@ -817,7 +919,7 @@ anv_state_stream_alloc(struct anv_state_stream *stream,
       stream->next = sizeof(*sb);
 
       offset = align_u32(stream->next, alignment);
-      assert(offset + size <= stream->block_size);
+      assert(offset + size <= stream->block.alloc_size);
    }
 
    struct anv_state state = stream->block;
@@ -853,9 +955,11 @@ struct bo_pool_bo_link {
 };
 
 void
-anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device)
+anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device,
+                 uint64_t bo_flags)
 {
    pool->device = device;
+   pool->bo_flags = bo_flags;
    memset(pool->free_list, 0, sizeof(pool->free_list));
 
    VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
@@ -907,6 +1011,8 @@ anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo, uint32_t size)
    if (result != VK_SUCCESS)
       return result;
 
+   new_bo.flags = pool->bo_flags;
+
    assert(new_bo.size == pow2_size);
 
    new_bo.map = anv_gem_mmap(pool->device, new_bo.gem_handle, 0, pow2_size, 0);
@@ -1036,7 +1142,10 @@ anv_scratch_pool_alloc(struct anv_device *device, struct anv_scratch_pool *pool,
     *
     * so nothing will ever touch the top page.
     */
-   bo->bo.flags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+   assert(!(bo->bo.flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS));
+
+   if (device->instance->physicalDevice.has_exec_async)
+      bo->bo.flags |= EXEC_OBJECT_ASYNC;
 
    /* Set the exists last because it may be read by other threads */
    __sync_synchronize();
@@ -1063,7 +1172,7 @@ anv_bo_cache_init(struct anv_bo_cache *cache)
 
    if (pthread_mutex_init(&cache->mutex, NULL)) {
       _mesa_hash_table_destroy(cache->bo_map, NULL);
-      return vk_errorf(VK_ERROR_OUT_OF_HOST_MEMORY,
+      return vk_errorf(NULL, NULL, VK_ERROR_OUT_OF_HOST_MEMORY,
                        "pthread_mutex_init failed: %m");
    }
 
@@ -1092,7 +1201,7 @@ anv_bo_cache_lookup_locked(struct anv_bo_cache *cache, uint32_t gem_handle)
    return bo;
 }
 
-static struct anv_bo *
+UNUSED static struct anv_bo *
 anv_bo_cache_lookup(struct anv_bo_cache *cache, uint32_t gem_handle)
 {
    pthread_mutex_lock(&cache->mutex);
@@ -1143,40 +1252,25 @@ anv_bo_cache_alloc(struct anv_device *device,
 VkResult
 anv_bo_cache_import(struct anv_device *device,
                     struct anv_bo_cache *cache,
-                    int fd, uint64_t size, struct anv_bo **bo_out)
+                    int fd, struct anv_bo **bo_out)
 {
    pthread_mutex_lock(&cache->mutex);
 
-   /* The kernel is going to give us whole pages anyway */
-   size = align_u64(size, 4096);
-
    uint32_t gem_handle = anv_gem_fd_to_handle(device, fd);
    if (!gem_handle) {
       pthread_mutex_unlock(&cache->mutex);
-      return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX);
+      return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
    }
 
    struct anv_cached_bo *bo = anv_bo_cache_lookup_locked(cache, gem_handle);
    if (bo) {
-      if (bo->bo.size != size) {
-         pthread_mutex_unlock(&cache->mutex);
-         return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX);
-      }
       __sync_fetch_and_add(&bo->refcount, 1);
    } else {
-      /* For security purposes, we reject BO imports where the size does not
-       * match exactly.  This prevents a malicious client from passing a
-       * buffer to a trusted client, lying about the size, and telling the
-       * trusted client to try and texture from an image that goes
-       * out-of-bounds.  This sort of thing could lead to GPU hangs or worse
-       * in the trusted client.  The trusted client can protect itself against
-       * this sort of attack but only if it can trust the buffer size.
-       */
-      off_t import_size = lseek(fd, 0, SEEK_END);
-      if (import_size == (off_t)-1 || import_size != size) {
+      off_t size = lseek(fd, 0, SEEK_END);
+      if (size == (off_t)-1) {
          anv_gem_close(device, gem_handle);
          pthread_mutex_unlock(&cache->mutex);
-         return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHX);
+         return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
       }
 
       bo = vk_alloc(&device->alloc, sizeof(struct anv_cached_bo), 8,
@@ -1191,28 +1285,10 @@ anv_bo_cache_import(struct anv_device *device,
 
       anv_bo_init(&bo->bo, gem_handle, size);
 
-      if (device->instance->physicalDevice.supports_48bit_addresses)
-         bo->bo.flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
-
-      if (device->instance->physicalDevice.has_exec_async)
-         bo->bo.flags |= EXEC_OBJECT_ASYNC;
-
       _mesa_hash_table_insert(cache->bo_map, (void *)(uintptr_t)gem_handle, bo);
    }
 
    pthread_mutex_unlock(&cache->mutex);
-
-   /* From the Vulkan spec:
-    *
-    *    "Importing memory from a file descriptor transfers ownership of
-    *    the file descriptor from the application to the Vulkan
-    *    implementation. The application must not perform any operations on
-    *    the file descriptor after a successful import."
-    *
-    * If the import fails, we leave the file descriptor open.
-    */
-   close(fd);
-
    *bo_out = &bo->bo;
 
    return VK_SUCCESS;