<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 2025-02-10 02:51, Deng, Emily wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:PH0PR12MB54174DCAA0C2AF44C320C1098FF22@PH0PR12MB5417.namprd12.prod.outlook.com">
      
      <meta name="Generator" content="Microsoft Word 15 (filtered
        medium)">
      <style>@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:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}@font-face
        {font-family:"\@DengXian";
        panose-1:2 1 6 0 3 1 1 1 1 1;}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;}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]-->
      <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>
        <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 <a class="moz-txt-link-rfc2396E" href="mailto:Xiaogang.Chen@amd.com"><Xiaogang.Chen@amd.com></a>
                      <br>
                      <b>Sent:</b> Monday, February 10, 2025 10:18 AM<br>
                      <b>To:</b> Deng, Emily <a class="moz-txt-link-rfc2396E" href="mailto:Emily.Deng@amd.com"><Emily.Deng@amd.com></a>;
                      <a class="moz-txt-link-abbreviated" href="mailto:amd-gfx@lists.freedesktop.org">amd-gfx@lists.freedesktop.org</a><br>
                      <b>Subject:</b> Re: [PATCH] drm/amdkfd: Fix the
                      deadlock in svm_range_restore_work<o:p></o:p></span></p>
                </div>
              </div>
              <p class="MsoNormal"><o:p> </o:p></p>
              <p><o:p> </o:p></p>
              <div>
                <p class="MsoNormal">On 2/7/2025 9:02 PM, Deng, Emily
                  wrote:<o:p></o:p></p>
              </div>
              <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
                <pre>[AMD Official Use Only - AMD Internal Distribution Only]<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>[AMD Official Use Only - AMD Internal Distribution Only]<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>Ping.......<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>Emily Deng<o:p></o:p></pre>
                <pre>Best Wishes<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre><o:p> </o:p></pre>
                <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
                  <pre>-----Original Message-----<o:p></o:p></pre>
                  <pre>From: Emily Deng <a href="mailto:Emily.Deng@amd.com" moz-do-not-send="true"><Emily.Deng@amd.com></a><o:p></o:p></pre>
                  <pre>Sent: Friday, February 7, 2025 6:28 PM<o:p></o:p></pre>
                  <pre>To: <a href="mailto:amd-gfx@lists.freedesktop.org" moz-do-not-send="true" class="moz-txt-link-freetext">amd-gfx@lists.freedesktop.org</a><o:p></o:p></pre>
                  <pre>Cc: Deng, Emily <a href="mailto:Emily.Deng@amd.com" moz-do-not-send="true"><Emily.Deng@amd.com></a><o:p></o:p></pre>
                  <pre>Subject: [PATCH] drm/amdkfd: Fix the deadlock in svm_range_restore_work<o:p></o:p></pre>
                  <pre><o:p> </o:p></pre>
                  <pre>It will hit deadlock in svm_range_restore_work ramdonly.<o:p></o:p></pre>
                  <pre>Detail as below:<o:p></o:p></pre>
                  <pre>1.svm_range_restore_work<o:p></o:p></pre>
                  <pre>      ->svm_range_list_lock_and_flush_work<o:p></o:p></pre>
                  <pre>      ->mmap_write_lock<o:p></o:p></pre>
                  <pre>2.svm_range_restore_work<o:p></o:p></pre>
                  <pre>      ->svm_range_validate_and_map<o:p></o:p></pre>
                  <pre>      ->amdgpu_vm_update_range<o:p></o:p></pre>
                  <pre>      ->amdgpu_vm_ptes_update<o:p></o:p></pre>
                  <pre>      ->amdgpu_vm_pt_alloc<o:p></o:p></pre>
                  <pre>      ->svm_range_evict_svm_bo_worker<o:p></o:p></pre>
                </blockquote>
              </blockquote>
              <p>svm_range_evict_svm_bo_worker is a function running by
                a kernel task from default system_wq. It is not the task
                that runs svm_range_restore_work which is from
                system_freezable_wq. The second task may need wait the
                first task to release mmap_write_lock, but there is no
                cycle lock dependency.<o:p></o:p></p>
              <p>Can you explain more how deadlock happened? If a
                deadlock exists between two tasks there are should be at
                least two locks used by both tasks.<o:p></o:p></p>
              <p>Regards<o:p></o:p></p>
              <p style="margin-left:11.0pt">Xiaogang <o:p></o:p></p>
              <p style="margin-left:5.5pt">In Step 2, during
                the amdgpu_vm_pt_alloc process, the system encounters
                insufficient memory and triggers an eviction. This
                initiates the svm_range_evict_svm_bo_worker task, and
                waits for the eviction_fence to be signaled. However,
                the svm_range_evict_svm_bo_worker cannot acquire
                the mmap_read_lock(mm), preventing it from signaling
                the eviction_fence. As a
                result, amdgpu_vm_pt_alloc remains incomplete and cannot
                release the mmap_write_lock(mm).
                <o:p></o:p></p>
              <p style="margin-left:5.5pt">Which means
                the svm_range_restore_work task holds
                the mmap_write_lock(mm) and is stuck waiting for
                the eviction_fence to be signaled
                by svm_range_evict_svm_bo_worker.
                However, svm_range_evict_svm_bo_worker is itself
                blocked, unable to acquire the mmap_read_lock(mm). This
                creates a deadlock.</p>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    <p>The deadlock situation should not happen as
      svm_range_restore_work is only used for xnack off case, there is
      no VRAM over commitment with KFD amdgpu_amdkfd_reserve_mem_limit.
      We reserved VRAM ESTIMATE_PT_SIZE for page table allocation to
      prevent this situation.</p>
    <p>Regards,</p>
    <p>Philip<br>
    </p>
    <blockquote type="cite" cite="mid:PH0PR12MB54174DCAA0C2AF44C320C1098FF22@PH0PR12MB5417.namprd12.prod.outlook.com">
      <div>
        <div>
          <div class="WordSection1">
            <div style="border:none;border-left:solid blue
              1.5pt;padding:0in 0in 0in 4.0pt">
              <p style="margin-left:5.5pt"><o:p></o:p></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">
              <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
                <pre>      ->mmap_read_lock(deadlock here, because already get mmap_write_lock)<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>How to fix?<o:p></o:p></pre>
                <pre>Downgrade the write lock to read lock.<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>Signed-off-by: Emily Deng <a href="mailto:Emily.Deng@amd.com" moz-do-not-send="true"><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 | 3 ++-<o:p></o:p></pre>
                <pre>1 file changed, 2 insertions(+), 1 deletion(-)<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>diff --git 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>index bd3e20d981e0..c907e2de3dde 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>@@ -1841,6 +1841,7 @@ static void svm_range_restore_work(struct work_struct<o:p></o:p></pre>
                <pre>*work)<o:p></o:p></pre>
                <pre>      mutex_lock(&process_info->lock);<o:p></o:p></pre>
                <pre>      svm_range_list_lock_and_flush_work(svms, mm);<o:p></o:p></pre>
                <pre>      mutex_lock(&svms->lock);<o:p></o:p></pre>
                <pre>+      mmap_write_downgrade(mm);<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>      evicted_ranges = atomic_read(&svms->evicted_ranges);<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>@@ -1890,7 +1891,7 @@ static void svm_range_restore_work(struct work_struct<o:p></o:p></pre>
                <pre>*work)<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>out_reschedule:<o:p></o:p></pre>
                <pre>      mutex_unlock(&svms->lock);<o:p></o:p></pre>
                <pre>-      mmap_write_unlock(mm);<o:p></o:p></pre>
                <pre>+      mmap_read_unlock(mm);<o:p></o:p></pre>
                <pre>      mutex_unlock(&process_info->lock);<o:p></o:p></pre>
                <pre><o:p> </o:p></pre>
                <pre>      /* If validation failed, reschedule another attempt */<o:p></o:p></pre>
                <pre>--<o:p></o:p></pre>
                <pre>2.34.1<o:p></o:p></pre>
              </blockquote>
              <pre><o:p> </o:p></pre>
            </blockquote>
          </div>
        </div>
      </div>
    </blockquote>
  </body>
</html>