[Mesa-dev] [PATCH] anv: Put batch buffers in their own address space

Jason Ekstrand jason at jlekstrand.net
Sun Aug 26 12:58:15 UTC 2018


This protects the batches a bit from being overwritten by random OOB
memor access.  Maybe not worth merging but it's somewhat interesting for
debugging.
---
 src/intel/vulkan/anv_allocator.c | 11 +++++------
 src/intel/vulkan/anv_device.c    |  3 ++-
 src/intel/vulkan/anv_private.h   |  8 ++++++--
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/intel/vulkan/anv_allocator.c b/src/intel/vulkan/anv_allocator.c
index f62d48ae3fe..5dd72071d30 100644
--- a/src/intel/vulkan/anv_allocator.c
+++ b/src/intel/vulkan/anv_allocator.c
@@ -967,10 +967,11 @@ struct bo_pool_bo_link {
 
 void
 anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device,
-                 uint64_t bo_flags)
+                 uint64_t bo_flags, uint64_t start_address)
 {
    pool->device = device;
    pool->bo_flags = bo_flags;
+   pool->next_address = start_address;
    memset(pool->free_list, 0, sizeof(pool->free_list));
 
    VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
@@ -1025,18 +1026,17 @@ anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo, uint32_t size)
 
    new_bo.flags = pool->bo_flags;
 
-   if (!anv_vma_alloc(pool->device, &new_bo))
-      return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
-
    assert(new_bo.size == pow2_size);
 
    new_bo.map = anv_gem_mmap(pool->device, new_bo.gem_handle, 0, pow2_size, 0);
    if (new_bo.map == MAP_FAILED) {
       anv_gem_close(pool->device, new_bo.gem_handle);
-      anv_vma_free(pool->device, &new_bo);
       return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
    }
 
+   new_bo.offset = pool->next_address;
+   pool->next_address += new_bo.size;
+
    *bo = new_bo;
 
    VG(VALGRIND_MEMPOOL_ALLOC(pool, bo->map, size));
@@ -1078,7 +1078,6 @@ anv_scratch_pool_finish(struct anv_device *device, struct anv_scratch_pool *pool
       for (unsigned i = 0; i < 16; i++) {
          struct anv_scratch_bo *bo = &pool->bos[i][s];
          if (bo->exists > 0) {
-            anv_vma_free(device, &bo->bo);
             anv_gem_close(device, bo->bo.gem_handle);
          }
       }
diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index 85b9456a795..2a026399877 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -1732,7 +1732,8 @@ VkResult anv_CreateDevice(
       (physical_device->has_exec_capture ? EXEC_OBJECT_CAPTURE : 0) |
       (physical_device->use_softpin ? EXEC_OBJECT_PINNED : 0);
 
-   anv_bo_pool_init(&device->batch_bo_pool, device, bo_flags);
+   anv_bo_pool_init(&device->batch_bo_pool, device, bo_flags,
+                    BATCH_BO_POOL_MIN_ADDRESS);
 
    result = anv_bo_cache_init(&device->bo_cache);
    if (result != VK_SUCCESS)
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index 5598d2bf5be..7fe1f2a7f77 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -118,7 +118,9 @@ struct gen_l3_config;
 #define SURFACE_STATE_POOL_MAX_ADDRESS     0x00017fffffffULL
 #define INSTRUCTION_STATE_POOL_MIN_ADDRESS 0x000180000000ULL /* 6 GiB */
 #define INSTRUCTION_STATE_POOL_MAX_ADDRESS 0x0001bfffffffULL
-#define HIGH_HEAP_MIN_ADDRESS              0x0001c0000000ULL /* 7 GiB */
+#define BATCH_BO_POOL_MIN_ADDRESS          0x0001c0000000ULL /* 7 GiB */
+#define BATCH_BO_POOL_MAX_ADDRESS          0x0001ffffffffULL
+#define HIGH_HEAP_MIN_ADDRESS              0x000200000000ULL /* 8 GiB */
 #define HIGH_HEAP_MAX_ADDRESS              0xfffeffffffffULL
 
 #define LOW_HEAP_SIZE               \
@@ -760,13 +762,15 @@ struct anv_state anv_state_stream_alloc(struct anv_state_stream *stream,
 struct anv_bo_pool {
    struct anv_device *device;
 
+   uint64_t next_address;
+
    uint64_t bo_flags;
 
    void *free_list[16];
 };
 
 void anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device,
-                      uint64_t bo_flags);
+                      uint64_t bo_flags, uint64_t start_address);
 void anv_bo_pool_finish(struct anv_bo_pool *pool);
 VkResult anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo,
                            uint32_t size);
-- 
2.17.1



More information about the mesa-dev mailing list