<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    Hi Kevin,<br>
    <br>
    no, the test case should already fail in amdgpu_bo_validate_size().<br>
    <br>
    If we have a system with 2TiB of memory where the test case could
    succeed then we should increase the requested size to something
    larger.<br>
    <br>
    And if the underlying core Linux kernel functions don't allow
    allocations as large as the requested one we should *NEVER* ever add
    workarounds like this.<br>
    <br>
    It is perfectly expected that this test case is not able to fulfill
    the desired allocation. That it fails during kvmalloc is
    unfortunate, but not a show stopper.<br>
    <br>
    Regards,<br>
    Christian.<br>
    <br>
    <br>
    <div class="moz-cite-prefix">Am 20.04.22 um 13:32 schrieb Wang,
      Yang(Kevin):<br>
    </div>
    <blockquote type="cite"
cite="mid:CO6PR12MB547340CB065269B1731812B182F59@CO6PR12MB5473.namprd12.prod.outlook.com">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <style type="text/css" style="display:none;">P {margin-top:0;margin-bottom:0;}</style>
      <p
        style="font-family:Arial;font-size:10pt;color:#0000FF;margin:5pt;"
        align="Left">
        [AMD Official Use Only]<br>
      </p>
      <br>
      <div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="font-family: Calibri, Arial, Helvetica,
            sans-serif; font-size: 12pt; color: rgb(0, 0, 0);
            background-color: rgba(0, 0, 0, 0);">Hi Chris,</span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <br>
        </div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="font-family: Calibri, Arial, Helvetica,
            sans-serif; font-size: 12pt; color: rgb(0, 0, 0);
            background-color: rgba(0, 0, 0, 0);">you misunderstood
            background about this case.</span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="font-family: Calibri, Arial, Helvetica,
            sans-serif; font-size: 12pt; color: rgb(0, 0, 0);
            background-color: rgba(0, 0, 0, 0);"><br>
          </span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="font-family: Calibri, Arial, Helvetica,
            sans-serif; font-size: 12pt; color: rgb(0, 0, 0);
            background-color: rgba(0, 0, 0, 0);">a</span><span
            style="color: rgb(0, 0, 0); font-family: Calibri, Arial,
            Helvetica, sans-serif; font-size: 12pt; background-color:
            rgba(0, 0, 0, 0);">lthough we expect this test case to fail,
            it should fail at the location where the Bo actual memory is
            actually allocated. now the code logic will cause the
            failure to allocate memory to store DMA address.</span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="color: rgb(0, 0, 0); font-family: Calibri, Arial,
            Helvetica, sans-serif; font-size: 12pt; background-color:
            rgba(0, 0, 0, 0);"><br>
          </span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="color: rgb(0, 0, 0); font-family: Calibri, Arial,
            Helvetica, sans-serif; font-size: 12pt; background-color:
            rgba(0, 0, 0, 0);"><span style="background-color:rgb(255,
              255, 255);display:inline !important">e.g: </span>the case
            is failed in 2TB system ram machine, it should be allocated
            successful, but it is failed.</span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="color: rgb(0, 0, 0); font-family: Calibri, Arial,
            Helvetica, sans-serif; font-size: 12pt;"><br>
          </span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="color: rgb(0, 0, 0); font-family: Calibri, Arial,
            Helvetica, sans-serif; font-size: 12pt; background-color:
            rgba(0, 0, 0, 0);">allocate 1TB BO, the ttm should allocate
            1TB/4k * 8 buffer to store allocate result (page address),
            this should not be failed usually.</span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="color: rgb(0, 0, 0); font-size: 12pt;
            background-color: rgba(0, 0, 0, 0); font-family: Calibri,
            Arial, Helvetica, sans-serif;"><br>
          </span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="color: rgb(0, 0, 0); font-size: 12pt;
            background-color: rgba(0, 0, 0, 0); font-family: Calibri,
            Arial, Helvetica, sans-serif;">There is a similar fix in
            upstream kernel 5.18, b</span><span style="color: rgb(0, 0,
            0); font-size: 12pt; background-color: rgba(0, 0, 0, 0);
            font-family: Calibri, Arial, Helvetica, sans-serif;">efore
            this fix entered the TTM code, this problem existed in TTM.</span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="color: rgb(0, 0, 0); font-size: 12pt;
            background-color: rgba(0, 0, 0, 0); font-family: Calibri,
            Arial, Helvetica, sans-serif;"><br>
          </span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="color: rgb(0, 0, 0); font-size: 12pt;
            background-color: rgba(0, 0, 0, 0); font-family: Calibri,
            Arial, Helvetica, sans-serif;"><a
href="https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v5.18-rc3&id=a421ef303008b0ceee2cfc625c3246fa7654b0ca"
              id="LPlnk660272" moz-do-not-send="true">kernel/git/torvalds/linux.git
              - Linux kernel source tree</a><br>
          </span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span class="commit-subject" style="font-size: 12pt; margin:
            1.5em 0em 0.5em; padding: 0em; color: rgb(0, 0, 0);
            font-family: Calibri, Arial, Helvetica, sans-serif;
            background-color: rgb(255, 255, 255);">mm: allow !GFP_KERNEL
            allocations for kvmalloc</span><br>
        </div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <div><br>
          </div>
          <span style="font-family: Calibri, Arial, Helvetica,
            sans-serif; font-size: 12pt; color: rgb(0, 0, 0);
            background-color: rgba(0, 0, 0, 0);">Best Regards,</span></div>
        <div style="font-family: Calibri, Arial, Helvetica, sans-serif;
          font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
          <span style="font-family: Calibri, Arial, Helvetica,
            sans-serif; font-size: 12pt; color: rgb(0, 0, 0);
            background-color: rgba(0, 0, 0, 0);">Kevin</span><br>
          <div><br>
          </div>
        </div>
        <hr tabindex="-1" style="display:inline-block; width:98%">
        <div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt"
            face="Calibri, sans-serif" color="#000000"><b>From:</b>
            Koenig, Christian <a class="moz-txt-link-rfc2396E" href="mailto:Christian.Koenig@amd.com"><Christian.Koenig@amd.com></a><br>
            <b>Sent:</b> Wednesday, April 20, 2022 6:53 PM<br>
            <b>To:</b> Wang, Yang(Kevin) <a class="moz-txt-link-rfc2396E" href="mailto:KevinYang.Wang@amd.com"><KevinYang.Wang@amd.com></a>;
            <a class="moz-txt-link-abbreviated" href="mailto:dri-devel@lists.freedesktop.org">dri-devel@lists.freedesktop.org</a>
            <a class="moz-txt-link-rfc2396E" href="mailto:dri-devel@lists.freedesktop.org"><dri-devel@lists.freedesktop.org></a>;
            <a class="moz-txt-link-abbreviated" href="mailto:amd-gfx@lists.freedesktop.org">amd-gfx@lists.freedesktop.org</a>
            <a class="moz-txt-link-rfc2396E" href="mailto:amd-gfx@lists.freedesktop.org"><amd-gfx@lists.freedesktop.org></a><br>
            <b>Subject:</b> Re: [PATCH] drm/ttm: fix ttm tt init fail
            when size exceeds kmalloc limit</font>
          <div> </div>
        </div>
        <div>Am 20.04.22 um 11:07 schrieb Wang, Yang(Kevin):<br>
          <blockquote type="cite">
            <p style="margin-top: 0px; margin-bottom:
              0px;font-family:Arial; font-size:10pt; color:#0000FF;
              margin:5pt" align="Left">
              [AMD Official Use Only]<br>
            </p>
            <br>
            <div>
              <div
                style="font-family:Calibri,Arial,Helvetica,sans-serif;
                font-size:12pt; color:rgb(0,0,0)">
                <br>
              </div>
              <hr tabindex="-1" style="display:inline-block; width:98%">
              <div id="x_divRplyFwdMsg" dir="ltr"><font
                  style="font-size:11pt" face="Calibri, sans-serif"
                  color="#000000"><b>From:</b> Koenig, Christian
                  <a href="mailto:Christian.Koenig@amd.com"
                    data-auth="NotApplicable"
                    class="x_moz-txt-link-rfc2396E"
                    moz-do-not-send="true">
                    <Christian.Koenig@amd.com></a><br>
                  <b>Sent:</b> Wednesday, April 20, 2022 5:00 PM<br>
                  <b>To:</b> Wang, Yang(Kevin) <a
                    href="mailto:KevinYang.Wang@amd.com"
                    data-auth="NotApplicable"
                    class="x_moz-txt-link-rfc2396E"
                    moz-do-not-send="true">
                    <KevinYang.Wang@amd.com></a>; <a
                    href="mailto:dri-devel@lists.freedesktop.org"
                    data-auth="NotApplicable"
                    class="x_moz-txt-link-abbreviated
                    moz-txt-link-freetext" moz-do-not-send="true">
                    dri-devel@lists.freedesktop.org</a> <a
                    href="mailto:dri-devel@lists.freedesktop.org"
                    data-auth="NotApplicable"
                    class="x_moz-txt-link-rfc2396E"
                    moz-do-not-send="true">
                    <dri-devel@lists.freedesktop.org></a>; <a
                    href="mailto:amd-gfx@lists.freedesktop.org"
                    data-auth="NotApplicable"
                    class="x_moz-txt-link-abbreviated
                    moz-txt-link-freetext" moz-do-not-send="true">
                    amd-gfx@lists.freedesktop.org</a> <a
                    href="mailto:amd-gfx@lists.freedesktop.org"
                    data-auth="NotApplicable"
                    class="x_moz-txt-link-rfc2396E"
                    moz-do-not-send="true">
                    <amd-gfx@lists.freedesktop.org></a><br>
                  <b>Subject:</b> Re: [PATCH] drm/ttm: fix ttm tt init
                  fail when size exceeds kmalloc limit</font>
                <div> </div>
              </div>
              <div class="x_BodyFragment"><font size="2"><span
                    style="font-size:11pt">
                    <div class="x_PlainText x_elementToProof">Am
                      20.04.22 um 10:56 schrieb Yang Wang:<br>
                      > if the __GFP_ZERO is set, the kvmalloc()
                      can't fallback to use vmalloc()<br>
                      <br>
                      Hui what? Why should kvmalloc() not be able to
                      fallback to vmalloc() <br>
                      when __GFP_ZERO is set?<br>
                      <br>
                      And even that is really the case then that sounds
                      like a bug in kvmalloc().</div>
                    <div class="x_PlainText x_elementToProof"><br>
                      Regards,<br>
                      Christian.</div>
                    <div class="x_PlainText x_elementToProof"><br>
                      <div class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">[kevin]:</div>
                    </div>
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">it is really
                        test case from libdrm amdgpu test, which try to
                        allocate a big BO which will cause ttm tt init
                        fail.</span><br>
                    </div>
                  </span></font></div>
            </div>
          </blockquote>
          <br>
          <br>
          LOL! Guys, this test case is intended to fail!<br>
          <b><br>
          </b>The test consists of allocating a buffer so ridiculous
          large that it should never succeed and be rejected by the
          kernel driver.<br>
          <br>
          This patch here is a really clear NAK.<br>
          <br>
          Regards,<br>
          Christian.<br>
          <br>
          <blockquote type="cite">
            <div>
              <div class="x_BodyFragment"><font size="2"><span
                    style="font-size:11pt">
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">it may be a
                        kvmalloc() bug, and this patch can as a
                        workaround in<span
                          style="background-color:rgb(255,255,255);
                          display:inline!important"> ttm</span> before
                        this issue fix.</span></div>
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)"><br>
                      </span></div>
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">void
                        *kvmalloc_node(size_t size, gfp_t flags, int
                        node) <br>
                      </span></div>
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">{</span></div>
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">...</span></div>
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">        if
                        ((flags & GFP_KERNEL) != GFP_KERNEL)<br>
                      </span></div>
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">             
                          return kmalloc_node(size, flags, node);<br>
                      </span></div>
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">...</span></div>
                    <div class="x_PlainText x_elementToProof"><span
                        class="x_PlainText" style="margin:0px;
                        background-color:rgb(255,255,255)">}</span></div>
                    <div class="x_PlainText x_elementToProof"><br>
                    </div>
                    <div class="x_PlainText x_elementToProof">Best
                      Regards,</div>
                    <div class="x_PlainText x_elementToProof">Kevin<br>
                      <br>
                      > to allocate memory, when request size is
                      exceeds kmalloc limit, it will<br>
                      > cause allocate memory fail.<br>
                      ><br>
                      > e.g: when ttm want to create a BO with 1TB
                      size, it maybe fail.<br>
                      ><br>
                      > Signed-off-by: Yang Wang <a
                        href="mailto:KevinYang.Wang@amd.com"
                        data-auth="NotApplicable"
                        class="x_moz-txt-link-rfc2396E"
                        moz-do-not-send="true">
                        <KevinYang.Wang@amd.com></a><br>
                      > ---<br>
                      >   drivers/gpu/drm/ttm/ttm_tt.c | 14
                      +++++++++++---<br>
                      >   1 file changed, 11 insertions(+), 3
                      deletions(-)<br>
                      ><br>
                      > diff --git a/drivers/gpu/drm/ttm/ttm_tt.c
                      b/drivers/gpu/drm/ttm/ttm_tt.c<br>
                      > index 79c870a3bef8..9f2f3e576b8d 100644<br>
                      > --- a/drivers/gpu/drm/ttm/ttm_tt.c<br>
                      > +++ b/drivers/gpu/drm/ttm/ttm_tt.c<br>
                      > @@ -97,9 +97,12 @@ int ttm_tt_create(struct
                      ttm_buffer_object *bo, bool zero_alloc)<br>
                      >   static int
                      ttm_tt_alloc_page_directory(struct ttm_tt *ttm)<br>
                      >   {<br>
                      >        ttm->pages =
                      kvmalloc_array(ttm->num_pages, sizeof(void*),<br>
                      > -                     GFP_KERNEL |
                      __GFP_ZERO);<br>
                      > +                                
                      GFP_KERNEL);<br>
                      >        if (!ttm->pages)<br>
                      >                return -ENOMEM;<br>
                      > +<br>
                      > +     memset(ttm->pages, 0,
                      ttm->num_pages * sizeof(void *));<br>
                      > +<br>
                      >        return 0;<br>
                      >   }<br>
                      >   <br>
                      > @@ -108,10 +111,12 @@ static int
                      ttm_dma_tt_alloc_page_directory(struct ttm_tt
                      *ttm)<br>
                      >        ttm->pages =
                      kvmalloc_array(ttm->num_pages,<br>
                      >                                   
                      sizeof(*ttm->pages) +<br>
                      >                                   
                      sizeof(*ttm->dma_address),<br>
                      > -                                 GFP_KERNEL
                      | __GFP_ZERO);<br>
                      > +                                
                      GFP_KERNEL);<br>
                      >        if (!ttm->pages)<br>
                      >                return -ENOMEM;<br>
                      >   <br>
                      > +     memset(ttm->pages, 0,
                      ttm->num_pages * (sizeof(*ttm->pages) +
                      sizeof(*ttm->dma_address)));<br>
                      > +<br>
                      >        ttm->dma_address = (void
                      *)(ttm->pages + ttm->num_pages);<br>
                      >        return 0;<br>
                      >   }<br>
                      > @@ -120,9 +125,12 @@ static int
                      ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm)<br>
                      >   {<br>
                      >        ttm->dma_address =
                      kvmalloc_array(ttm->num_pages,<br>
                      >                                         
                      sizeof(*ttm->dma_address),<br>
                      > -                                      
                      GFP_KERNEL | __GFP_ZERO);<br>
                      > +                                      
                      GFP_KERNEL);<br>
                      >        if (!ttm->dma_address)<br>
                      >                return -ENOMEM;<br>
                      > +<br>
                      > +     memset(ttm->dma_address, 0,
                      ttm->num_pages * sizeof(*ttm->dma_address));<br>
                      > +<br>
                      >        return 0;<br>
                      >   }<br>
                      >   <br>
                      <br>
                    </div>
                  </span></font></div>
            </div>
          </blockquote>
          <br>
        </div>
      </div>
    </blockquote>
    <br>
  </body>
</html>