<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Arial,Helvetica,sans-serif;" dir="ltr">
<p>Hi Monk,</p>
<p><br>
</p>
<p>Correction:</p>
<p><br>
</p>
<p>Please remove the following line in the patch:</p>
<p><span style="color: rgb(33, 33, 33); font-size: 13.3333px;">+#include "stdio.h"</span><br>
</p>
<p><span style="color: rgb(33, 33, 33); font-size: 13.3333px;"><br>
</span></p>
<p><span style="color: rgb(33, 33, 33); font-size: 13.3333px;">This patch does not need to use stdio.h.</span></p>
<p><span style="color: rgb(33, 33, 33); font-size: 13.3333px;">With that fixed, </span><span style="color: rgb(33, 33, 33); font-family: Calibri, Arial, Helvetica, sans-serif;">Reviewed by: </span><span style="color: rgb(33, 33, 33); font-family: Calibri, Arial, Helvetica, sans-serif;"> Alex
 Xie <AlexBin.Xie@amd.com></span><br>
</p>
<br>
Thanks,
<div>Alex Bin Xie<br>
<br>
<div style="color: rgb(0, 0, 0);">
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Xie, AlexBin<br>
<b>Sent:</b> Monday, April 17, 2017 2:58 PM<br>
<b>To:</b> Liu, Monk; 'amd-gfx@lists.freedesktop.org'<br>
<b>Cc:</b> brahma_sw_dev<br>
<b>Subject:</b> Re: [PATCH] amdgpu:fix incorrect use on the remap_mutex</font>
<div> </div>
</div>
<div>
<div id="divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:#000000; font-family:Calibri,Arial,Helvetica,sans-serif">
<p>Reviewed by: <span> Alex Xie <AlexBin.Xie@amd.com></span></p>
<p><span><br>
</span></p>
<p><span>Please note that this is patch is not for all open libdrm.</span></p>
<p><span><br>
</span></p>
<p><span>There is no <span style="font-family:Calibri,Arial,Helvetica,sans-serif,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols; font-size:13.3333px">remap_mutex in master branch of all open libdrm.</span></span></p>
<p><span><br>
</span></p>
<p><span>Thanks,</span></p>
<p><span>Alex Bin Xie</span></p>
<br>
<div style="color:rgb(0,0,0)">
<div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="x_divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Liu, Monk<br>
<b>Sent:</b> Monday, April 17, 2017 11:12 AM<br>
<b>To:</b> 'amd-gfx@lists.freedesktop.org'<br>
<b>Cc:</b> brahma_sw_dev<br>
<b>Subject:</b> 答复: [PATCH] amdgpu:fix incorrect use on the remap_mutex</font>
<div> </div>
</div>
</div>
<font size="2"><span style="font-size:10pt">
<div class="PlainText">Anyone review it  ?<br>
<br>
-----邮件原件-----<br>
发件人: Liu, Monk <br>
发送时间: 2017年4月17日 14:16<br>
收件人: amd-gfx@lists.freedesktop.org<br>
主题: [PATCH] amdgpu:fix incorrect use on the remap_mutex<br>
<br>
[PATCH] amdgpu:fix incorrect use on the remap_mutex<br>
<br>
this patch fixed a multi-thread race issue by expand the protection range of remap_mutex to the whole LIST walk through, by this fix the CTS test:<br>
"dEQP-VK.api.object_management.multithreaded_per_thread_device.device_memory_smal"<br>
can always pass now,<br>
Previously it has 1/15 chance to fail with a high performance I7 CPU under virtualization envrionment.<br>
<br>
Change-Id: If88b9d35e79fe1cb6eb0e32c680bc4996c7915bc<br>
Signed-off-by: Monk Liu <Monk.Liu@amd.com><br>
---<br>
 amdgpu/amdgpu_bo.c    | 29 ++++++++++++++++++-----------<br>
 amdgpu/amdgpu_vamgr.c |  5 +++--<br>
 2 files changed, 21 insertions(+), 13 deletions(-)<br>
<br>
diff --git a/amdgpu/amdgpu_bo.c b/amdgpu/amdgpu_bo.c index 33cb705..8960728 100644<br>
--- a/amdgpu/amdgpu_bo.c<br>
+++ b/amdgpu/amdgpu_bo.c<br>
@@ -989,6 +989,8 @@ int amdgpu_bo_va_op_refcounted(amdgpu_device_handle dev,<br>
                         return -EINVAL;<br>
         }<br>
 <br>
+       pthread_mutex_lock(&dev->remap_mutex);<br>
+<br>
         /* find the previous mapped va object and its bo and unmap it*/<br>
         if (ops == AMDGPU_VA_OP_MAP) {<br>
                 LIST_FOR_EACH_ENTRY(vahandle, &dev->remap_list, list) { @@ -998,15 +1000,15 @@ int amdgpu_bo_va_op_refcounted(amdgpu_device_handle dev,<br>
                                 /* the overlap va mapping which need to be unmapped first */<br>
                                 vao = vahandle;<br>
                                 r = amdgpu_bo_va_op(vao->bo, vao->offset, vao->size, vao->address, flags, AMDGPU_VA_OP_UNMAP);<br>
-                               if (r)<br>
+                               if (r) {<br>
+                                       pthread_mutex_unlock(&dev->remap_mutex);<br>
                                         return -EINVAL;<br>
+                               }<br>
 <br>
                                 /* Just drop the reference. */<br>
                                 amdgpu_bo_reference(&vao->bo, NULL);<br>
                                 /* remove the remap from list */<br>
-                               pthread_mutex_lock(&dev->remap_mutex);<br>
                                 list_del(&vao->list);<br>
-                               pthread_mutex_unlock(&dev->remap_mutex);<br>
                                 free(vao);<br>
                         }<br>
                 }<br>
@@ -1021,12 +1023,18 @@ int amdgpu_bo_va_op_refcounted(amdgpu_device_handle dev,<br>
                         }<br>
                 }<br>
                 if (vao) {<br>
-                       if (bo && (bo != vao->bo))<br>
+                       if (bo && (bo != vao->bo)) {<br>
+                               pthread_mutex_unlock(&dev->remap_mutex);<br>
                                 return -EINVAL;<br>
-               } else<br>
+                       }<br>
+               } else {<br>
+                       pthread_mutex_unlock(&dev->remap_mutex);<br>
                         return -EINVAL;<br>
-       } else<br>
+               }<br>
+       } else {<br>
+               pthread_mutex_unlock(&dev->remap_mutex);<br>
                 return -EINVAL;<br>
+       }<br>
 <br>
         /* we only allow null bo for unmap operation */<br>
         if (!bo)<br>
@@ -1039,26 +1047,25 @@ int amdgpu_bo_va_op_refcounted(amdgpu_device_handle dev,<br>
                         /* Just drop the reference. */<br>
                         amdgpu_bo_reference(&bo, NULL);<br>
                         /* remove the remap from list */<br>
-                       pthread_mutex_lock(&dev->remap_mutex);<br>
                         list_del(&vao->list);<br>
-                       pthread_mutex_unlock(&dev->remap_mutex);<br>
                         free(vao);<br>
                 } else if (ops == AMDGPU_VA_OP_MAP) {<br>
                         /* bump the refcount of bo! */<br>
                         atomic_inc(&bo->refcount);<br>
                         /* add the remap to list and vao should be NULL for map */<br>
                         vao = (struct amdgpu_va_remap*)calloc(1, sizeof(struct amdgpu_va_remap));<br>
-                       if (!vao)<br>
+                       if (!vao) {<br>
+                               pthread_mutex_unlock(&dev->remap_mutex);<br>
                                 return -ENOMEM;<br>
+                       }<br>
                         vao->address = addr;<br>
                         vao->size = size;<br>
                         vao->offset = offset;<br>
                         vao->bo = bo;<br>
-                       pthread_mutex_lock(&dev->remap_mutex);<br>
                         list_add(&vao->list, &dev->remap_list);<br>
-                       pthread_mutex_unlock(&dev->remap_mutex);<br>
                 }<br>
         }<br>
+       pthread_mutex_unlock(&dev->remap_mutex);<br>
         return r;<br>
 }<br>
 <br>
diff --git a/amdgpu/amdgpu_vamgr.c b/amdgpu/amdgpu_vamgr.c index b3f5397..c5a7f41 100644<br>
--- a/amdgpu/amdgpu_vamgr.c<br>
+++ b/amdgpu/amdgpu_vamgr.c<br>
@@ -32,6 +32,7 @@<br>
 #include "amdgpu_drm.h"<br>
 #include "amdgpu_internal.h"<br>
 #include "util_math.h"<br>
+#include "stdio.h"<br>
 <br>
 /* Devices share SVM range. So a global SVM VAM manager is needed. */  static struct amdgpu_bo_va_mgr vamgr_svm; @@ -488,6 +489,7 @@ int amdgpu_va_range_free(amdgpu_va_handle va_range_handle)<br>
         address = va_range_handle->address;<br>
         size    = va_range_handle->size;<br>
 <br>
+       pthread_mutex_lock(&dev->remap_mutex);<br>
         /* clean up previous mapping if it is used for virtual allocation */<br>
         LIST_FOR_EACH_ENTRY(vahandle, &dev->remap_list, list) {<br>
                 /* check whether the remap list alraedy have va that overlap with current request */ @@ -500,12 +502,11 @@ int amdgpu_va_range_free(amdgpu_va_handle va_range_handle)<br>
                         /* Just drop the reference. */<br>
                         amdgpu_bo_reference(&vahandle->bo, NULL);<br>
                         /* remove the remap from list */<br>
-                       pthread_mutex_lock(&dev->remap_mutex);<br>
                         list_del(&vahandle->list);<br>
-                       pthread_mutex_unlock(&dev->remap_mutex);<br>
                         free(vahandle);<br>
                 }<br>
         }<br>
+       pthread_mutex_unlock(&dev->remap_mutex);<br>
 <br>
         amdgpu_vamgr_free_va(va_range_handle->vamgr,<br>
                         va_range_handle->address,<br>
--<br>
2.7.4<br>
<br>
</div>
</span></font></div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>