<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:DengXian;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Aptos;}
@font-face
        {font-family:"\@DengXian";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:12.0pt;
        font-family:"Aptos",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;}
span.EmailStyle21
        {mso-style-type:personal-reply;
        font-family:"Arial",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;
        mso-ligatures:none;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<p style="font-family:Calibri;font-size:10pt;color:#0000FF;margin:5pt;font-style:normal;font-weight:normal;text-decoration:none;" align="Left">
[AMD Official Use Only - AMD Internal Distribution Only]<br>
</p>
<br>
<div>
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Chen, Xiaogang <Xiaogang.Chen@amd.com>
<br>
<b>Sent:</b> Friday, March 7, 2025 1:01 AM<br>
<b>To:</b> Deng, Emily <Emily.Deng@amd.com>; amd-gfx@lists.freedesktop.org<br>
<b>Subject:</b> Re: [PATCH v4] drm/amdgpu: Fix the race condition for draining retry fault<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p>Thanks for catch up and fix this race condition. It looks good to me. One minor thing below:<o:p></o:p></p>
<div>
<p class="MsoNormal">On 3/6/2025 12:03 AM, Emily Deng wrote:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<pre>Issue:<o:p></o:p></pre>
<pre>In the scenario where svm_range_restore_pages is called, but svm->checkpoint_ts<o:p></o:p></pre>
<pre> has not been set and the retry fault has not been drained, svm_range_unmap_from_cpu<o:p></o:p></pre>
<pre>is triggered and calls svm_range_free. Meanwhile, svm_range_restore_pages<o:p></o:p></pre>
<pre>continues execution and reaches svm_range_from_addr. This results in<o:p></o:p></pre>
<pre>a "failed to find prange..." error, causing the page recovery to fail.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>How to fix:<o:p></o:p></pre>
<pre>Move the timestamp check code under the protection of svm->lock.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>v2:<o:p></o:p></pre>
<pre>Make sure all right locks are released before go out.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>v3:<o:p></o:p></pre>
<pre>Directly goto out_unlock_svms, and return -EAGAIN.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>v4:<o:p></o:p></pre>
<pre>Refine code.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>Signed-off-by: Emily Deng <a href="mailto:Emily.Deng@amd.com"><Emily.Deng@amd.com></a><o:p></o:p></pre>
<pre>---<o:p></o:p></pre>
<pre> drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 30 +++++++++++++++-------------<o:p></o:p></pre>
<pre> 1 file changed, 16 insertions(+), 14 deletions(-)<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c<o:p></o:p></pre>
<pre>index d04725583f19..83ac14bf7a7a 100644<o:p></o:p></pre>
<pre>--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c<o:p></o:p></pre>
<pre>+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c<o:p></o:p></pre>
<pre>@@ -3008,19 +3008,6 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,<o:p></o:p></pre>
<pre>                goto out;<o:p></o:p></pre>
<pre>        }<o:p></o:p></pre>
<pre> <o:p></o:p></pre>
<pre>-       /* check if this page fault time stamp is before svms->checkpoint_ts */<o:p></o:p></pre>
<pre>-       if (svms->checkpoint_ts[gpuidx] != 0) {<o:p></o:p></pre>
<pre>-               if (amdgpu_ih_ts_after_or_equal(ts,  svms->checkpoint_ts[gpuidx])) {<o:p></o:p></pre>
<pre>-                       pr_debug("draining retry fault, drop fault 0x%llx\n", addr);<o:p></o:p></pre>
<pre>-                       r = 0;<o:p></o:p></pre>
<pre>-                       goto out;<o:p></o:p></pre>
<pre>-               } else<o:p></o:p></pre>
<pre>-                       /* ts is after svms->checkpoint_ts now, reset svms->checkpoint_ts<o:p></o:p></pre>
<pre>-                        * to zero to avoid following ts wrap around give wrong comparing<o:p></o:p></pre>
<pre>-                        */<o:p></o:p></pre>
<pre>-                svms->checkpoint_ts[gpuidx] = 0;<o:p></o:p></pre>
<pre>-       }<o:p></o:p></pre>
<pre>-<o:p></o:p></pre>
<pre>        if (!p->xnack_enabled) {<o:p></o:p></pre>
<pre>                pr_debug("XNACK not enabled for pasid 0x%x\n", pasid);<o:p></o:p></pre>
<pre>                r = -EFAULT;<o:p></o:p></pre>
<pre>@@ -3040,6 +3027,20 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,<o:p></o:p></pre>
<pre>        mmap_read_lock(mm);<o:p></o:p></pre>
<pre> retry_write_locked:<o:p></o:p></pre>
<pre>        mutex_lock(&svms->lock);<o:p></o:p></pre>
<pre>+<o:p></o:p></pre>
<pre>+       /* check if this page fault time stamp is before svms->checkpoint_ts */<o:p></o:p></pre>
<pre>+       if (svms->checkpoint_ts[gpuidx] != 0) {<o:p></o:p></pre>
<pre>+               if (amdgpu_ih_ts_after_or_equal(ts,  svms->checkpoint_ts[gpuidx])) {<o:p></o:p></pre>
<pre>+                       pr_debug("draining retry fault, drop fault 0x%llx\n", addr);<o:p></o:p></pre>
<pre>+                       r = -EAGAIN;<o:p></o:p></pre>
</blockquote>
<p>We drop page fault because it is stale, not mean to handle it again. if return -EAGAIN we do amdgpu_gmc_filter_faults_remove. If after unmap, user map same range again we should treat page fault happened at same range as new one. <o:p></o:p></p>
<p>Regards<o:p></o:p></p>
<p>Xiaogang<o:p></o:p></p>
<p><span style="font-size:11.0pt;font-family:"Arial",sans-serif">Sorry, I didn't quite catch that. So, you think we shouldn't remove the fault from amdgpu_gmc_filter_faults_remove?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-ligatures:standardcontextual">Emily Deng<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;mso-ligatures:standardcontextual">Best Wishes<o:p></o:p></span></p>
</div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Arial",sans-serif"><o:p> </o:p></span></p>
<p><span style="font-size:11.0pt;font-family:"Arial",sans-serif"><o:p> </o:p></span></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<pre><o:p> </o:p></pre>
<pre>+                       goto out_unlock_svms;<o:p></o:p></pre>
<pre>+               } else<o:p></o:p></pre>
<pre>+                       /* ts is after svms->checkpoint_ts now, reset svms->checkpoint_ts<o:p></o:p></pre>
<pre>+                        * to zero to avoid following ts wrap around give wrong comparing<o:p></o:p></pre>
<pre>+                        */<o:p></o:p></pre>
<pre>+                svms->checkpoint_ts[gpuidx] = 0;<o:p></o:p></pre>
<pre>+       }<o:p></o:p></pre>
<pre>+<o:p></o:p></pre>
<pre>        prange = svm_range_from_addr(svms, addr, NULL);<o:p></o:p></pre>
<pre>        if (!prange) {<o:p></o:p></pre>
<pre>                pr_debug("failed to find prange svms 0x%p address [0x%llx]\n",<o:p></o:p></pre>
<pre>@@ -3165,7 +3166,8 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,<o:p></o:p></pre>
<pre>        mutex_unlock(&svms->lock);<o:p></o:p></pre>
<pre>        mmap_read_unlock(mm);<o:p></o:p></pre>
<pre> <o:p></o:p></pre>
<pre>-       svm_range_count_fault(node, p, gpuidx);<o:p></o:p></pre>
<pre>+       if (r != -EAGAIN)<o:p></o:p></pre>
<pre>+               svm_range_count_fault(node, p, gpuidx);<o:p></o:p></pre>
<pre> <o:p></o:p></pre>
<pre>        mmput(mm);<o:p></o:p></pre>
<pre> out:<o:p></o:p></pre>
</blockquote>
</div>
</div>
</body>
</html>