<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jan 23, 2019 at 2:45 PM Rafael Antognolli <<a href="mailto:rafael.antognolli@intel.com">rafael.antognolli@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Accessing bo->map and then pool->center_bo_offset without a lock is<br>
racy. One way of avoiding such race condition is to store the bo->map +<br>
center_bo_offset into pool->map at the time the block pool is growing,<br>
which happens within a lock.<br>
<br>
Cc: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>><br>
Reported-by: Ian Romanick <<a href="mailto:idr@freedesktop.org" target="_blank">idr@freedesktop.org</a>><br>
Bugzilla: <a href="https://bugs.freedesktop.org/show_bug.cgi?id=109442" rel="noreferrer" target="_blank">https://bugs.freedesktop.org/show_bug.cgi?id=109442</a><br>
Fixes: fc3f58832015cbb177179e7f3420d3611479b4a9<br>
---<br>
 src/intel/vulkan/anv_allocator.c | 10 ++++++++--<br>
 src/intel/vulkan/anv_private.h   | 13 +++++++++++++<br>
 2 files changed, 21 insertions(+), 2 deletions(-)<br>
<br>
diff --git a/src/intel/vulkan/anv_allocator.c b/src/intel/vulkan/anv_allocator.c<br>
index 89f26789c85..0bfe55bf684 100644<br>
--- a/src/intel/vulkan/anv_allocator.c<br>
+++ b/src/intel/vulkan/anv_allocator.c<br>
@@ -437,6 +437,7 @@ anv_block_pool_init(struct anv_block_pool *pool,<br>
    pool->nbos = 0;<br>
    pool->size = 0;<br>
    pool->start_address = gen_canonical_address(start_address);<br>
+   pool->map = NULL;<br>
<br>
    /* This pointer will always point to the first BO in the list */<br>
    pool->bo = &pool->bos[0];<br>
@@ -575,6 +576,7 @@ anv_block_pool_expand_range(struct anv_block_pool *pool,<br>
<br>
    /* Now that we successfull allocated everything, we can write the new<br>
     * center_bo_offset back into pool. */<br>
+   pool->map = map + center_bo_offset;<br></blockquote><div><br></div><div>We should only set this when we are NOT using softpin.  Otherwise, we'll have a non-NULL map pointer that doesn't do what it looks like it does.</div><div><br></div><div>--Jason<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
    pool->center_bo_offset = center_bo_offset;<br>
<br>
    /* For block pool BOs we have to be a bit careful about where we place them<br>
@@ -670,8 +672,12 @@ anv_block_pool_get_bo(struct anv_block_pool *pool, int32_t *offset)<br>
 void*<br>
 anv_block_pool_map(struct anv_block_pool *pool, int32_t offset)<br>
 {<br>
-   struct anv_bo *bo = anv_block_pool_get_bo(pool, &offset);<br>
-   return bo->map + pool->center_bo_offset + offset;<br>
+   if (pool->bo_flags & EXEC_OBJECT_PINNED) {<br>
+      struct anv_bo *bo = anv_block_pool_get_bo(pool, &offset);<br>
+      return bo->map + offset;<br>
+   } else {<br>
+      return pool->map + offset;<br>
+   }<br>
 }<br>
<br>
 /** Grows and re-centers the block pool.<br>
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h<br>
index 3889065c93c..110b2ccf023 100644<br>
--- a/src/intel/vulkan/anv_private.h<br>
+++ b/src/intel/vulkan/anv_private.h<br>
@@ -663,6 +663,19 @@ struct anv_block_pool {<br>
     */<br>
    uint32_t center_bo_offset;<br>
<br>
+   /* Current memory map of the block pool.  This pointer may or may not<br>
+    * point to the actual beginning of the block pool memory.  If<br>
+    * anv_block_pool_alloc_back has ever been called, then this pointer<br>
+    * will point to the "center" position of the buffer and all offsets<br>
+    * (negative or positive) given out by the block pool alloc functions<br>
+    * will be valid relative to this pointer.<br>
+    *<br>
+    * In particular, map == bo.map + center_offset<br>
+    *<br>
+    * DO NOT access this pointer directly. Use anv_block_pool_map() instead,<br>
+    * since it will handle the softpin case as well, where this points to NULL.<br>
+    */<br>
+   void *map;<br>
    int fd;<br>
<br>
    /**<br>
-- <br>
2.17.2<br>
<br>
</blockquote></div></div>