Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E5559C433F5 for ; Wed, 20 Apr 2022 12:42:32 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 45CF010F17C; Wed, 20 Apr 2022 12:42:32 +0000 (UTC) Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2044.outbound.protection.outlook.com [40.107.96.44]) by gabe.freedesktop.org (Postfix) with ESMTPS id E964F10F17C; Wed, 20 Apr 2022 12:42:30 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HXtXjYpfcD8mLf6vd6m954ToURZvbRVFOOy6Gu6ZEncf09y8aR4oJzAvB+iy8WTx09R4Au/XqdA4kbDuo7v2yBlOmTs+qxvt3ABwuzTVg3lGg9bHaVZ41/5tjFFNTFMb9YIrtLH0m3xw87CPzUmMv/Ph/kXWRtYEJ35Ss5k2FVyTOB1AeBskTgAyO3asbSkGYDEGE8tJDI4oSxu0k1M/4pOUBawNZGnnZjk5KHAfOlWUOHbbWxJ1itUr2lvUGnYRXXeDoHRqNa61ujIzwzjeT2GMsfp4nvJhgipYj1vPmy9kBC8v+bU+Bmuew3TOer/UdW1LSkBRjj9HgC+DlA1bag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Q8p6Q7j1ehyFTVNWl3pGXEVmDTOXYSil/+G2J1Wcct4=; b=PZAziMkKvo3Cqae2E8woJSsQ89dMJc3n7giD+B/vnWE98yqppQa+cSDX773TfSF2n+jDZs0SF/8Ty/ZJVOwA6s+VLsrPq8JGrhgX8nSh4R5sVN8PhFmcpKa3VTh7x7HPdLj149egfUqLpHpYIfRuniVUjou3u4fcOQnGJIKCyou0KX6VICUdOjcALG0jTalHZOAy3cD40HYuT7Rsau3j/TuukHryeA303Q0RU+xKRhMA+zeGhjFskQdQud0GCUYauqN+pfKQ7O9CoVlqN+GAhSCROXZp5noBIOKoTP4it+R0BkLeZxim5SPZDOMvHQfuRkwTkeTq+s3jUfjeUNLT3g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Q8p6Q7j1ehyFTVNWl3pGXEVmDTOXYSil/+G2J1Wcct4=; b=LF+67A58CVc/vMr9f+Rx7AjHReMoRtG8i+Sng09Z2PVEgLlrRaLw6GN19PO4w1QmT3MlhVcnXblUNzsDObxr8i2p6tIzFBtxzpCp9m1/pzwJAMySmoq3R523ZrclIy6bP+UqBvMFHPeDNX+l2n3H9+lREf63uw3Kq/Y2M0fBLZo= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from BN8PR12MB3587.namprd12.prod.outlook.com (2603:10b6:408:43::13) by SN1PR12MB2381.namprd12.prod.outlook.com (2603:10b6:802:2f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.14; Wed, 20 Apr 2022 12:42:28 +0000 Received: from BN8PR12MB3587.namprd12.prod.outlook.com ([fe80::a5fb:7137:5e64:cf8]) by BN8PR12MB3587.namprd12.prod.outlook.com ([fe80::a5fb:7137:5e64:cf8%5]) with mapi id 15.20.5164.026; Wed, 20 Apr 2022 12:42:28 +0000 Content-Type: multipart/alternative; boundary="------------2R6jqFgzNJ0RbutqKPIW5VOm" Message-ID: <71f3ac77-b6ed-e503-8e05-dcf9d2c2811a@amd.com> Date: Wed, 20 Apr 2022 14:42:17 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH] drm/ttm: fix ttm tt init fail when size exceeds kmalloc limit Content-Language: en-US To: "Wang, Yang(Kevin)" , =?UTF-8?Q?Christian_K=c3=b6nig?= , "dri-devel@lists.freedesktop.org" , "amd-gfx@lists.freedesktop.org" References: <20220420085612.1664787-1-KevinYang.Wang@amd.com> <00dfec71-cf38-d6d9-8775-0ae88f59224e@amd.com> <65ef352b-59c8-e802-5285-afc59e56fa9b@amd.com> <572ed15c-b47e-e7b3-850a-cb0d5259af04@gmail.com> From: =?UTF-8?Q?Christian_K=c3=b6nig?= In-Reply-To: X-ClientProxiedBy: AM7PR04CA0017.eurprd04.prod.outlook.com (2603:10a6:20b:110::27) To BN8PR12MB3587.namprd12.prod.outlook.com (2603:10b6:408:43::13) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c0ebb653-9142-4344-a0b2-08da22cb3a37 X-MS-TrafficTypeDiagnostic: SN1PR12MB2381:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1hV3pl56RA8Cv0rDoHWoUfPVSYkcLz2Oq7tYE8q1x0nhQzTt1wQXISgIjJEhyVPjc0lcMQ0Uqmjb8ZCfUJA+Ec2EB3T+NtVpwWng9helkkdWiiSzCM53doH9p3GLtRrVFFIh+8EIglNOHxtrqgF1Bs7s6KjwuCRuo1WODVItJStnF4W3WsErTckhWodtf0+eqRAHC2LY/GAO/K8CIf4W4i5rBHshu2hzkajrsF3ovlQgBKfzSu4q7AAsxwZDLNsRwKaz6TEffyqX8nSEV1wpcA39m8tBqbRVJIPIorKA2uEJjBQNLzoWVAZRGv1OsAkBDj+Mzhfa+jNqC+BHA0lQfEGVg7EUeOa+zEFNbKirO4IFkh5TpInUXcF34dctZPeKSNkh4Ln/Jzuni6fDXa9ELbTXBTJuOMXjZQYziQlvMbbndX6Wr9hkNBLxhnRH/07JFmzeC3acWoJpL9xpQz8XzKXM3U9BWYoyQch98DcOXyRrDtepTCFoj3PwtIxOGubfD3cLgmXW4rgvrjvS515lZkF1rHuDQ6Td+tjfpt7ZmDk4HNfagr0bJuPEn6a4KIuGaSgkX71XYZf9bbOAMoIbRRnP2BDnA0OAtFmDOO939XuZw7Om0wqkSV8hYaVGg9NarMv7S3VsEJFnkeee873cvTz3VzV0i5BX6elfogGDlDPSTaNcywYipPv0YXFWO/vMENmZ26Z4SpF4RBqp4x+nC1p2NICx6o1upf6B3usDf3nD53oTSlurk0aoyH1E3pdhF/+4FCHqZRiUMrRF7jCykViipFfNiKxgEWeEqeh0lTjExUCH59TWxQ/GeWTE04S4zfID8uK+RhKWb6PuleKscg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BN8PR12MB3587.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(36756003)(38100700002)(19627405001)(316002)(8676002)(66556008)(66946007)(31686004)(166002)(110136005)(66476007)(5660300002)(45080400002)(6512007)(33964004)(53546011)(2906002)(6506007)(83380400001)(6486002)(86362001)(508600001)(186003)(30864003)(6666004)(31696002)(26005)(66574015)(2616005)(8936002)(45980500001)(43740500002)(579004); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?WjRLbEh5OCtJS0lnUWtqVzlYYz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B?N2hzQjZ2cGM5eXVVOHdmbm9aMW1nUUJPQXYzMThCZnV6elRJZGZvYmdyb1pU?= =?utf-8?B?NGZkTlpibnhWdlZhalkzcTVNb3oxdmQ2Nklqa3BrSWZic1o2VTdXSWtEYnZp?= =?utf-8?B?NU55NlpDc1RkaW1jNGxFNXlsNmxveWlHSGZSZkd3LzJ6VU1pVGluN2t5QnZs?= =?utf-8?B?UUpMVHlVS1kvdS9GMGNVZkR3amRPK1N0cFRPVHJvSFRNdlJGVEhWcUFKWUVT?= =?utf-8?B?ZCs2VVpyWGhwekk2c1orV0hOMnB1K2hHMDdvbGFWV2ZtQlNJcklYQUZKNm5l?= =?utf-8?B?eDFpYU9oVWlpVVJJNzh0b2FrM1VUOGpUTFhaa1UrZURmQXp2K0xRSHMwdjBn?= =?utf-8?B?dlFJVytXbzdvWHJ0cXdXb0tRRGNHMG1pN0YvMU9TRzU2NGs1aUE3SkplYW5j?= =?utf-8?B?c2hTWlYwMEJLcTJhaE1PSnBYa1hkWDFXL0plMkhaTG9IUzRCU2twYTMybENM?= =?utf-8?B?VXFGMzhTSFg2dVQxMWppS1YxSFhKRnBLdE1NTmMxQ2V1REZoNG9oZz09?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c0ebb653-9142-4344-a0b2-08da22cb3a37 X-MS-Exchange-CrossTenant-AuthSource: BN8PR12MB3587.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2022 12:42:28.1619 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: YTV4wiOz0SCKSehwFkfSNN+M5FSWQ7QmEe6053NVz15aNO3VRbfnmAdJRwyl7M+f X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2381 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: amd-gfx-bounces@lists.freedesktop.org Sender: "amd-gfx" --------------2R6jqFgzNJ0RbutqKPIW5VOm Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi Kevin, yes and that is perfectly valid and expected behavior. There is absolutely no need to change anything in TTM here. What we could do is: 1) Change the test case to use something larger than 1TiB. 2) Change kvmalloc to allow GFP_ZERO allocations even in the vmalloc fallback path. Regards, Christian. Am 20.04.22 um 14:39 schrieb Wang, Yang(Kevin): > > [AMD Official Use Only] > > > Hi Chirs, > > yes, right, the amdgpu drive rwill use amdgpu_bo_validate_size() > function to verify bo size, > but when driver try to allocate VRAM domain bo fail, the amdgpu driver > will fall back to allocate domain = (GTT | VRAM)  bo. > please check following code, it will cause the 2nd time to allocate bo > fail during allocate 256Mb buffer to store dma address (via kvmalloc()). > >   initial_domain = (u32)(0xffffffff & args->in.domains); > retry: >         r = amdgpu_gem_object_create(adev, size, args->in.alignment, >                  initial_domain, >                  flags, ttm_bo_type_device, resv, &gobj); >         if (r && r != -ERESTARTSYS) { >                 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) { >     flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; >     goto retry; >                 } > >                 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) { >     initial_domain |= AMDGPU_GEM_DOMAIN_GTT; >     goto retry; >                 } > DRM_DEBUG("Failed to allocate GEM object (%llu, %d, %llu, %d)\n", >             size, initial_domain, args->in.alignment, r); >   } > > Best Regards, > Kevin > > ------------------------------------------------------------------------ > *From:* Christian König > *Sent:* Wednesday, April 20, 2022 7:55 PM > *To:* Wang, Yang(Kevin) ; Koenig, Christian > ; dri-devel@lists.freedesktop.org > ; amd-gfx@lists.freedesktop.org > > *Subject:* Re: [PATCH] drm/ttm: fix ttm tt init fail when size exceeds > kmalloc limit > Hi Kevin, > > no, the test case should already fail in amdgpu_bo_validate_size(). > > 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. > > 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. > > 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. > > Regards, > Christian. > > > Am 20.04.22 um 13:32 schrieb Wang, Yang(Kevin): >> >> [AMD Official Use Only] >> >> >> Hi Chris, >> >> you misunderstood background about this case. >> >> although 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. >> >> e.g: the case is failed in 2TB system ram machine, it should be >> allocated successful, but it is failed. >> >> allocate 1TB BO, the ttm should allocate 1TB/4k * 8 buffer to store >> allocate result (page address), this should not be failed usually. >> >> There is a similar fix in upstream kernel 5.18, before this fix >> entered the TTM code, this problem existed in TTM. >> >> kernel/git/torvalds/linux.git - Linux kernel source tree >> >> mm: allow !GFP_KERNEL allocations for kvmalloc >> >> Best Regards, >> Kevin >> >> ------------------------------------------------------------------------ >> *From:* Koenig, Christian >> >> *Sent:* Wednesday, April 20, 2022 6:53 PM >> *To:* Wang, Yang(Kevin) >> ; dri-devel@lists.freedesktop.org >> >> >> ; >> amd-gfx@lists.freedesktop.org >> >> *Subject:* Re: [PATCH] drm/ttm: fix ttm tt init fail when size >> exceeds kmalloc limit >> Am 20.04.22 um 11:07 schrieb Wang, Yang(Kevin): >>> >>> [AMD Official Use Only] >>> >>> >>> >>> ------------------------------------------------------------------------ >>> *From:* Koenig, Christian >>> >>> *Sent:* Wednesday, April 20, 2022 5:00 PM >>> *To:* Wang, Yang(Kevin) >>> ; dri-devel@lists.freedesktop.org >>> >>> >>> ; >>> amd-gfx@lists.freedesktop.org >>> >>> *Subject:* Re: [PATCH] drm/ttm: fix ttm tt init fail when size >>> exceeds kmalloc limit >>> Am 20.04.22 um 10:56 schrieb Yang Wang: >>> > if the __GFP_ZERO is set, the kvmalloc() can't fallback to use >>> vmalloc() >>> >>> Hui what? Why should kvmalloc() not be able to fallback to vmalloc() >>> when __GFP_ZERO is set? >>> >>> And even that is really the case then that sounds like a bug in >>> kvmalloc(). >>> >>> Regards, >>> Christian. >>> >>> [kevin]: >>> it is really test case from libdrm amdgpu test, which try to >>> allocate a big BO which will cause ttm tt init fail. >> >> >> LOL! Guys, this test case is intended to fail! >> * >> *The test consists of allocating a buffer so ridiculous large that it >> should never succeed and be rejected by the kernel driver. >> >> This patch here is a really clear NAK. >> >> Regards, >> Christian. >> >>> it may be a kvmalloc() bug, and this patch can as a workaround >>> in ttm before this issue fix. >>> >>> void *kvmalloc_node(size_t size, gfp_t flags, int node) >>> { >>> ... >>> if ((flags & GFP_KERNEL) != GFP_KERNEL) >>>         return kmalloc_node(size, flags, node); >>> ... >>> } >>> >>> Best Regards, >>> Kevin >>> >>> > to allocate memory, when request size is exceeds kmalloc limit, it >>> will >>> > cause allocate memory fail. >>> > >>> > e.g: when ttm want to create a BO with 1TB size, it maybe fail. >>> > >>> > Signed-off-by: Yang Wang >>> >>> > --- >>> >   drivers/gpu/drm/ttm/ttm_tt.c | 14 +++++++++++--- >>> >   1 file changed, 11 insertions(+), 3 deletions(-) >>> > >>> > diff --git a/drivers/gpu/drm/ttm/ttm_tt.c >>> b/drivers/gpu/drm/ttm/ttm_tt.c >>> > index 79c870a3bef8..9f2f3e576b8d 100644 >>> > --- a/drivers/gpu/drm/ttm/ttm_tt.c >>> > +++ b/drivers/gpu/drm/ttm/ttm_tt.c >>> > @@ -97,9 +97,12 @@ int ttm_tt_create(struct ttm_buffer_object *bo, >>> bool zero_alloc) >>> >   static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm) >>> >   { >>> >        ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*), >>> > -                     GFP_KERNEL | __GFP_ZERO); >>> > + GFP_KERNEL); >>> >        if (!ttm->pages) >>> >                return -ENOMEM; >>> > + >>> > +     memset(ttm->pages, 0, ttm->num_pages * sizeof(void *)); >>> > + >>> >        return 0; >>> >   } >>> > >>> > @@ -108,10 +111,12 @@ static int >>> ttm_dma_tt_alloc_page_directory(struct ttm_tt *ttm) >>> >        ttm->pages = kvmalloc_array(ttm->num_pages, >>> > sizeof(*ttm->pages) + >>> > sizeof(*ttm->dma_address), >>> > - GFP_KERNEL | __GFP_ZERO); >>> > + GFP_KERNEL); >>> >        if (!ttm->pages) >>> >                return -ENOMEM; >>> > >>> > +     memset(ttm->pages, 0, ttm->num_pages * (sizeof(*ttm->pages) >>> + sizeof(*ttm->dma_address))); >>> > + >>> >        ttm->dma_address = (void *)(ttm->pages + ttm->num_pages); >>> >        return 0; >>> >   } >>> > @@ -120,9 +125,12 @@ static int >>> ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm) >>> >   { >>> >        ttm->dma_address = kvmalloc_array(ttm->num_pages, >>> > sizeof(*ttm->dma_address), >>> > - GFP_KERNEL | __GFP_ZERO); >>> > + GFP_KERNEL); >>> >        if (!ttm->dma_address) >>> >                return -ENOMEM; >>> > + >>> > +     memset(ttm->dma_address, 0, ttm->num_pages * >>> sizeof(*ttm->dma_address)); >>> > + >>> >        return 0; >>> >   } >>> > >>> >> > --------------2R6jqFgzNJ0RbutqKPIW5VOm Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit Hi Kevin,

yes and that is perfectly valid and expected behavior. There is absolutely no need to change anything in TTM here.

What we could do is:
1) Change the test case to use something larger than 1TiB.
2) Change kvmalloc to allow GFP_ZERO allocations even in the vmalloc fallback path.

Regards,
Christian.

Am 20.04.22 um 14:39 schrieb Wang, Yang(Kevin):

[AMD Official Use Only]


Hi Chirs,

yes, right, the amdgpu drive rwill use amdgpu_bo_validate_size() function to verify bo size,
but when driver try to allocate VRAM domain bo fail, the amdgpu driver will fall back to allocate domain = (GTT | VRAM)  bo.
please check following code, it will cause the 2nd time to allocate bo fail during allocate 256Mb buffer to store dma address (via kvmalloc()).

        initial_domain = (u32)(0xffffffff & args->in.domains);
retry:
        r = amdgpu_gem_object_create(adev, size, args->in.alignment,
                                     initial_domain,
                                     flags, ttm_bo_type_device, resv, &gobj);
        if (r && r != -ERESTARTSYS) {
                if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
                        flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
                        goto retry;
                }

                if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
                        initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
                        goto retry;
                }
                DRM_DEBUG("Failed to allocate GEM object (%llu, %d, %llu, %d)\n",
                                size, initial_domain, args->in.alignment, r);
        }

Best Regards,
Kevin


From: Christian König <ckoenig.leichtzumerken@gmail.com>
Sent: Wednesday, April 20, 2022 7:55 PM
To: Wang, Yang(Kevin) <KevinYang.Wang@amd.com>; Koenig, Christian <Christian.Koenig@amd.com>; dri-devel@lists.freedesktop.org <dri-devel@lists.freedesktop.org>; amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org>
Subject: Re: [PATCH] drm/ttm: fix ttm tt init fail when size exceeds kmalloc limit
 
Hi Kevin,

no, the test case should already fail in amdgpu_bo_validate_size().

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.

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.

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.

Regards,
Christian.


Am 20.04.22 um 13:32 schrieb Wang, Yang(Kevin):

[AMD Official Use Only]


Hi Chris,

you misunderstood background about this case.

although 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.

e.g: the case is failed in 2TB system ram machine, it should be allocated successful, but it is failed.

allocate 1TB BO, the ttm should allocate 1TB/4k * 8 buffer to store allocate result (page address), this should not be failed usually.

There is a similar fix in upstream kernel 5.18, before this fix entered the TTM code, this problem existed in TTM.

mm: allow !GFP_KERNEL allocations for kvmalloc

Best Regards,
Kevin


From: Koenig, Christian <Christian.Koenig@amd.com>
Sent: Wednesday, April 20, 2022 6:53 PM
To: Wang, Yang(Kevin) <KevinYang.Wang@amd.com>; dri-devel@lists.freedesktop.org <dri-devel@lists.freedesktop.org>; amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org>
Subject: Re: [PATCH] drm/ttm: fix ttm tt init fail when size exceeds kmalloc limit
 
Am 20.04.22 um 11:07 schrieb Wang, Yang(Kevin):

[AMD Official Use Only]




From: Koenig, Christian <Christian.Koenig@amd.com>
Sent: Wednesday, April 20, 2022 5:00 PM
To: Wang, Yang(Kevin) <KevinYang.Wang@amd.com>; dri-devel@lists.freedesktop.org <dri-devel@lists.freedesktop.org>; amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org>
Subject: Re: [PATCH] drm/ttm: fix ttm tt init fail when size exceeds kmalloc limit
 
Am 20.04.22 um 10:56 schrieb Yang Wang:
> if the __GFP_ZERO is set, the kvmalloc() can't fallback to use vmalloc()

Hui what? Why should kvmalloc() not be able to fallback to vmalloc()
when __GFP_ZERO is set?

And even that is really the case then that sounds like a bug in kvmalloc().

Regards,
Christian.

[kevin]:
it is really test case from libdrm amdgpu test, which try to allocate a big BO which will cause ttm tt init fail.


LOL! Guys, this test case is intended to fail!

The test consists of allocating a buffer so ridiculous large that it should never succeed and be rejected by the kernel driver.

This patch here is a really clear NAK.

Regards,
Christian.

it may be a kvmalloc() bug, and this patch can as a workaround in ttm before this issue fix.

void *kvmalloc_node(size_t size, gfp_t flags, int node) 
{
...
        if ((flags & GFP_KERNEL) != GFP_KERNEL)
                return kmalloc_node(size, flags, node);
...
}

Best Regards,
Kevin

> to allocate memory, when request size is exceeds kmalloc limit, it will
> cause allocate memory fail.
>
> e.g: when ttm want to create a BO with 1TB size, it maybe fail.
>
> Signed-off-by: Yang Wang <KevinYang.Wang@amd.com>
> ---
>   drivers/gpu/drm/ttm/ttm_tt.c | 14 +++++++++++---
>   1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c
> index 79c870a3bef8..9f2f3e576b8d 100644
> --- a/drivers/gpu/drm/ttm/ttm_tt.c
> +++ b/drivers/gpu/drm/ttm/ttm_tt.c
> @@ -97,9 +97,12 @@ int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
>   static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
>   {
>        ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
> -                     GFP_KERNEL | __GFP_ZERO);
> +                                 GFP_KERNEL);
>        if (!ttm->pages)
>                return -ENOMEM;
> +
> +     memset(ttm->pages, 0, ttm->num_pages * sizeof(void *));
> +
>        return 0;
>   }
>  
> @@ -108,10 +111,12 @@ static int ttm_dma_tt_alloc_page_directory(struct ttm_tt *ttm)
>        ttm->pages = kvmalloc_array(ttm->num_pages,
>                                    sizeof(*ttm->pages) +
>                                    sizeof(*ttm->dma_address),
> -                                 GFP_KERNEL | __GFP_ZERO);
> +                                 GFP_KERNEL);
>        if (!ttm->pages)
>                return -ENOMEM;
>  
> +     memset(ttm->pages, 0, ttm->num_pages * (sizeof(*ttm->pages) + sizeof(*ttm->dma_address)));
> +
>        ttm->dma_address = (void *)(ttm->pages + ttm->num_pages);
>        return 0;
>   }
> @@ -120,9 +125,12 @@ static int ttm_sg_tt_alloc_page_directory(struct ttm_tt *ttm)
>   {
>        ttm->dma_address = kvmalloc_array(ttm->num_pages,
>                                          sizeof(*ttm->dma_address),
> -                                       GFP_KERNEL | __GFP_ZERO);
> +                                       GFP_KERNEL);
>        if (!ttm->dma_address)
>                return -ENOMEM;
> +
> +     memset(ttm->dma_address, 0, ttm->num_pages * sizeof(*ttm->dma_address));
> +
>        return 0;
>   }
>  




--------------2R6jqFgzNJ0RbutqKPIW5VOm--