<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-01-06 21:31, Deng, Emily wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:PH0PR12MB5417148144D18CD9CDB70A3E8F112@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:"\@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;}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>
        <div class="WordSection1">
          <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">
                    Yang, Philip <a class="moz-txt-link-rfc2396E" href="mailto:Philip.Yang@amd.com"><Philip.Yang@amd.com></a>
                    <br>
                    <b>Sent:</b> Tuesday, January 7, 2025 7:51 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 partial
                    migrate issue<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 2025-01-02 19:06, Emily Deng
                wrote:<o:p></o:p></p>
            </div>
            <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
              <pre>For partial migrate from ram to vram, the migrate->cpages is not<o:p></o:p></pre>
              <pre>equal to migrate->npages, should use migrate->npages to check all needed<o:p></o:p></pre>
              <pre>migrate pages which could be copied or not.<o:p></o:p></pre>
              <pre><o:p> </o:p></pre>
              <pre>And only need to set those pages could be migrated to migrate->dst[i], or<o:p></o:p></pre>
              <pre>the migrate_vma_pages will migrate the wrong pages based on the migrate->dst[i].<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_migrate.c | 8 ++++----<o:p></o:p></pre>
              <pre> 1 file changed, 4 insertions(+), 4 deletions(-)<o:p></o:p></pre>
              <pre><o:p> </o:p></pre>
              <pre>diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c<o:p></o:p></pre>
              <pre>index 4b275937d05e..5c96c2d425e3 100644<o:p></o:p></pre>
              <pre>--- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c<o:p></o:p></pre>
              <pre>+++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c<o:p></o:p></pre>
              <pre>@@ -278,7 +278,7 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,<o:p></o:p></pre>
              <pre>                         struct migrate_vma *migrate, struct dma_fence **mfence,<o:p></o:p></pre>
              <pre>                         dma_addr_t *scratch, uint64_t ttm_res_offset)<o:p></o:p></pre>
              <pre> {<o:p></o:p></pre>
              <pre>-       uint64_t npages = migrate->cpages;<o:p></o:p></pre>
              <pre>+       uint64_t npages = migrate->npages;<o:p></o:p></pre>
            </blockquote>
            <p>As partial migration size is based on prange granularity,
              by default 2MB, maybe always migrate->cpages equal to
              migrate->npages, that's why we didn't trigger this bug.
              Wondering how do you catch this bug? This bug will leak
              svm_bo too, as svm_migrate_get_vram_page reference counter
              is incorrect.<o:p></o:p></p>
            <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
              <pre><o:p> </o:p></pre>
              <pre>        struct amdgpu_device *adev = node->adev;<o:p></o:p></pre>
              <pre>        struct device *dev = adev->dev;<o:p></o:p></pre>
              <pre>        struct amdgpu_res_cursor cursor;<o:p></o:p></pre>
              <pre>@@ -299,9 +299,6 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,<o:p></o:p></pre>
              <pre>                struct page *spage;<o:p></o:p></pre>
              <pre> <o:p></o:p></pre>
              <pre>                dst[i] = cursor.start + (j << PAGE_SHIFT);<o:p></o:p></pre>
              <pre>-               migrate->dst[i] = svm_migrate_addr_to_pfn(adev, dst[i]);<o:p></o:p></pre>
              <pre>-        svm_migrate_get_vram_page(prange, migrate->dst[i]);<o:p></o:p></pre>
              <pre>-               migrate->dst[i] = migrate_pfn(migrate->dst[i]);<o:p></o:p></pre>
              <pre> <o:p></o:p></pre>
              <pre>                spage = migrate_pfn_to_page(migrate->src[i]);<o:p></o:p></pre>
              <pre>                if (spage && !is_zone_device_page(spage)) {<o:p></o:p></pre>
              <pre>@@ -345,6 +342,9 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,<o:p></o:p></pre>
              <pre>                } else {<o:p></o:p></pre>
              <pre>                        j++;<o:p></o:p></pre>
              <pre>                }<o:p></o:p></pre>
              <pre>+               migrate->dst[i] = svm_migrate_addr_to_pfn(adev, dst[i]);<o:p></o:p></pre>
              <pre>+        svm_migrate_get_vram_page(prange, migrate->dst[i]);<o:p></o:p></pre>
              <pre>+               migrate->dst[i] = migrate_pfn(migrate->dst[i]);<o:p></o:p></pre>
            </blockquote>
            <p>This should move forward, to handle the corner case to
              migrate 1 page to the last page of VRAM res cursor.<o:p></o:p></p>
            <p><span style="font-size:11.0pt;font-family:"Arial",sans-serif">Sorry,
                don’t understand what is the corner case? And the place
                you put has not much different with mine, as it doesn’t
                have any jump or continue in the code between your</span></p>
          </div>
        </div>
      </div>
    </blockquote>
    migrate->dst[i] should set before this if condition (not after
    if) to setup sdma copy migrate->src to migrate->dst, if
    condition is true for the case migrating to the last page of VRAM
    res cusor, as the VRAM physical address is not contiguous.<br>
    <br>
    <p>        if (j >= (cursor.size >> PAGE_SHIFT) - 1
      && i < npages - 1) {</p>
    <p>Regards,</p>
    <p>Philip<br>
    </p>
    <blockquote type="cite" cite="mid:PH0PR12MB5417148144D18CD9CDB70A3E8F112@PH0PR12MB5417.namprd12.prod.outlook.com">
      <div>
        <div class="WordSection1">
          <div style="border:none;border-left:solid blue
            1.5pt;padding:0in 0in 0in 4.0pt">
            <p><span style="font-size:11.0pt;font-family:"Arial",sans-serif"><o:p></o:p></span></p>
            <p style="margin-left:11.0pt"><span style="font-size:11.0pt;font-family:"Arial",sans-serif">code
                place and mine. But it has “if (r) goto
                out_free_vram_pages;” to handle the error case. For
                error case, I think the
              </span>migrate->dst[i] also need to be set to null.<span style="font-size:11.0pt;font-family:"Arial",sans-serif"><o:p></o:p></span></p>
            <p>Please check this change, to add mpages accounting to
              break the loop earlier. <o:p>
              </o:p></p>
            <p><span style="font-size:11.0pt;font-family:"Arial",sans-serif">Good
                suggestion, I will add mpages in change.<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>
          <p style="margin-bottom:12.0pt">-       uint64_t npages =
            migrate->cpages;<br>
            +       uint64_t npages = migrate->npages;<br>
                    struct amdgpu_device *adev = node->adev;<br>
                    struct device *dev = adev->dev;<br>
                    struct amdgpu_res_cursor cursor;<br>
            +       uint64_t mpages = 0;<br>
                    dma_addr_t *src;<br>
                    uint64_t *dst;<br>
                    uint64_t i, j;<br>
            @@ -295,14 +296,9 @@ svm_migrate_copy_to_vram(struct
            kfd_node *node, struct svm_range *prange,<br>
             <br>
                    amdgpu_res_first(prange->ttm_res, ttm_res_offset,<br>
                                     npages << PAGE_SHIFT,
            &cursor);<br>
            -       for (i = j = 0; i < npages; i++) {<br>
            +       for (i = j = 0; i < npages && mpages <
            migrate->cpages; i++) {<br>
                            struct page *spage;<br>
             <br>
            -               dst[i] = cursor.start + (j <<
            PAGE_SHIFT);<br>
            -               migrate->dst[i] =
            svm_migrate_addr_to_pfn(adev, dst[i]);<br>
            -               svm_migrate_get_vram_page(prange,
            migrate->dst[i]);<br>
            -               migrate->dst[i] =
            migrate_pfn(migrate->dst[i]);<br>
            -<br>
                            spage =
            migrate_pfn_to_page(migrate->src[i]);<br>
                            if (spage &&
            !is_zone_device_page(spage)) {<br>
                                    src[i] = dma_map_page(dev, spage, 0,
            PAGE_SIZE,<br>
            @@ -322,6 +318,7 @@ svm_migrate_copy_to_vram(struct kfd_node
            *node, struct svm_range *prange,<br>
                                                            mfence);<br>
                                            if (r)<br>
                                                    goto
            out_free_vram_pages;<br>
            +                               mpages += j;<br>
                                            amdgpu_res_next(&cursor,
            (j + 1) << PAGE_SHIFT);<br>
                                            j = 0;<br>
                                    } else {<br>
            @@ -333,6 +330,11 @@ svm_migrate_copy_to_vram(struct
            kfd_node *node, struct svm_range *prange,<br>
                            pr_debug_ratelimited("dma mapping src to
            0x%llx, pfn 0x%lx\n",<br>
                                                 src[i] >>
            PAGE_SHIFT, page_to_pfn(spage));<br>
             <br>
            +               dst[i] = cursor.start + (j <<
            PAGE_SHIFT);<br>
            +               migrate->dst[i] =
            svm_migrate_addr_to_pfn(adev, dst[i]);<br>
            +               svm_migrate_get_vram_page(prange,
            migrate->dst[i]);<br>
            +               migrate->dst[i] =
            migrate_pfn(migrate->dst[i]);<br>
            +<br>
                            if (j >= (cursor.size >>
            PAGE_SHIFT) - 1 && i < npages - 1) {<br>
                                    r =
            svm_migrate_copy_memory_gart(adev, src + i - j,<br>
                                                                     dst
            + i - j, j + 1,<br>
            @@ -340,6 +342,7 @@ svm_migrate_copy_to_vram(struct kfd_node
            *node, struct svm_range *prange,<br>
                                                                    
            mfence);<br>
                                    if (r)<br>
                                            goto out_free_vram_pages;<br>
            +                       mpages += j + 1;<br>
                                    amdgpu_res_next(&cursor, (j + 1)
            * PAGE_SIZE);<br>
                                    j = 0;<br>
                            } else {<br>
            (END)<br>
            @@ -322,6 +318,7 @@ svm_migrate_copy_to_vram(struct kfd_node
            *node, struct svm_range *prange,<br>
                                                            mfence);<br>
                                            if (r)<br>
                                                    goto
            out_free_vram_pages;<br>
            +                               mpages += j;<br>
                                            amdgpu_res_next(&cursor,
            (j + 1) << PAGE_SHIFT);<br>
                                            j = 0;<br>
                                    } else {<br>
            @@ -333,6 +330,11 @@ svm_migrate_copy_to_vram(struct
            kfd_node *node, struct svm_range *prange,<br>
                            pr_debug_ratelimited("dma mapping src to
            0x%llx, pfn 0x%lx\n",<br>
                                                 src[i] >>
            PAGE_SHIFT, page_to_pfn(spage));<br>
             <br>
            +               dst[i] = cursor.start + (j <<
            PAGE_SHIFT);<br>
            +               migrate->dst[i] =
            svm_migrate_addr_to_pfn(adev, dst[i]);<br>
            +               svm_migrate_get_vram_page(prange,
            migrate->dst[i]);<br>
            +               migrate->dst[i] =
            migrate_pfn(migrate->dst[i]);<br>
            +<br>
                            if (j >= (cursor.size >>
            PAGE_SHIFT) - 1 && i < npages - 1) {<br>
                                    r =
            svm_migrate_copy_memory_gart(adev, src + i - j,<br>
                                                                     dst
            + i - j, j + 1,<br>
            @@ -340,6 +342,7 @@ svm_migrate_copy_to_vram(struct kfd_node
            *node, struct svm_range *prange,<br>
                                                                    
            mfence);<br>
                                    if (r)<br>
                                            goto out_free_vram_pages;<br>
            +                       mpages += j + 1;<br>
                                    amdgpu_res_next(&cursor, (j + 1)
            * PAGE_SIZE);<br>
                                    j = 0;<br>
                            } else {<o:p></o:p></p>
          <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
            <pre><o:p> </o:p></pre>
            <pre>        }<o:p></o:p></pre>
            <pre> <o:p></o:p></pre>
            <pre>        r = svm_migrate_copy_memory_gart(adev, src + i - j, dst + i - j, j,<o:p></o:p></pre>
          </blockquote>
        </div>
      </div>
    </blockquote>
  </body>
</html>