Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp3607532pxb; Mon, 21 Feb 2022 01:40:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxjQf71rIsj5fFA7f2QOhbaVzRESHXYA958vRVgiCz6PuFUJDfYPIlYV41Sp8irWmxKZqb X-Received: by 2002:a17:906:2a92:b0:6cd:4349:dc1a with SMTP id l18-20020a1709062a9200b006cd4349dc1amr15010196eje.648.1645436433091; Mon, 21 Feb 2022 01:40:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645436433; cv=none; d=google.com; s=arc-20160816; b=O5vwo+5FFzx2W4PPP0aHmgbkW/kG3uP+VHTzs5loXgwwZDTV2VYtKjelbgE5lb6S3u 5jKrUasyAzkefeXRkq6EahUJbY7eBwmrvMLTC3WmB0rwmEYqvWlB7zgA2Ol4p/86Id9p +qe81bPhkYyVuIG5aIBC8O5zrGXixq+E+OWd3+yNrQ1CXewCe8jPLS2qg9xEzbdbabcg W63A4F9TkCv4zM3hUYTxq6KBTIaE7VXpvGHpUlEewBi6ast+PhwPSRIICWsthLP7+NL7 lBA+BGdUHHH59b6bdBHp8+JwH0fCLI2yEp1UJAqTixUQo5Qo++E9XkcjXwpxucbniIfz cMgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=evzbx3hFkrG4LEm3hNJUfV0Qe4cnm6Z7bXfWh+oqgHA=; b=KG035OLB0FiKJDAJPPH8RQjOMkC1K03YTbHcGTHZ96GttfhQSLGpGv9z+wUUOd9QfK S6xOzPX8RWm1KpONm4RbF4WCeBSNGBFA8NRV4wQOEXt2uBh7RC0NxJlR6NW0yUMr5ve8 n6SGG0J/r+Z6bLCYkLUeU/UyYPCZGzS2rkocWcX8ruNoMByYnpD6TvrIrWz9yQQTv0UO IICVmepl0soNuI3Jh5kB/XolJN2yLfKZ7B5xDk3mF1bXiqkazWHHMRKA4i53a1QAT7ED JzMUW/Pos8O+MRoIiIX1f/1DMDjZlwSyRh/Err3oIF7ZMEIix3Vgy85OcE5xqyTOdJce CYJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=q1zGywxq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x9si7769935eje.747.2022.02.21.01.40.10; Mon, 21 Feb 2022 01:40:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=q1zGywxq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344241AbiBUD24 (ORCPT + 99 others); Sun, 20 Feb 2022 22:28:56 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:42982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236398AbiBUD2z (ORCPT ); Sun, 20 Feb 2022 22:28:55 -0500 Received: from mail-oo1-xc2b.google.com (mail-oo1-xc2b.google.com [IPv6:2607:f8b0:4864:20::c2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEFBA5132B; Sun, 20 Feb 2022 19:28:32 -0800 (PST) Received: by mail-oo1-xc2b.google.com with SMTP id s203-20020a4a3bd4000000b003191c2dcbe8so11539911oos.9; Sun, 20 Feb 2022 19:28:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=evzbx3hFkrG4LEm3hNJUfV0Qe4cnm6Z7bXfWh+oqgHA=; b=q1zGywxqEbABgk5qGo/5ZgnOjAUyxdDin3JN4tmRVsT/QUo5H2mlPReuWGyQPa6Pkq kIeqcgDUIuCKhgDSRwp3JN8vVyzGKfD6+Tqtn++FHpZow3PJkgW4RAbKPYF8eDlKWMMI eRForIbQFRMd6yIZ5rA8/5eCajF4y411hFjVKwxS9yLjPhsD5doEQiKqgdt6GdKmRycz mDRrhYjSCfzJe0AsySBagpk3OCmMOp7OzGFtL/K++ppJTfCIGYRAnyWAOpm7jDohJ2HH 4UnEmlGfThpXRTUjH1xPKj0SxwWtaarM957Vm23s1Vji0E3QzSuA4lW83aB/CpcBj9qz my/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=evzbx3hFkrG4LEm3hNJUfV0Qe4cnm6Z7bXfWh+oqgHA=; b=JLaMZePv6KLudNNlRY4xeC4IQjlyOfQV1GWM7j1IMFDmDg8YjRN3u/BFBz4Zr1V59Q Mz3TcZW4nfavDBcyLrDN2EWkXqHFDxFyw9Q0KOs3IR4ZsbtCr9IF48g7xPrYVmE6abzO jeHFzDo8gcWPbolP1S5mkYr4AJIINKBFJZMl/tijRfEx5v8szuNhzwy/FVZOA/VIjMQA +/niJvFIWwa+cUJvmLfRnW55s8yjHbOTzMPhvokL2cS7p0S2Gps1KkLBM9rDCFuxbG/2 Z2zrl1qpKJoNVgyuAhYMQGBKV5GRQmQGlbldOgRXA+sQZrVt7//ztLfxb68zu7gzX1ch kWUg== X-Gm-Message-State: AOAM533NkcRAcUxClokv2+FBWe3QtENY8usXcmKS/DGUPfBADDinqIBn +Pxl1OwHVzYbkptDF8zfET1/VCPaE+BTqk8aweU= X-Received: by 2002:a05:6870:148a:b0:d3:b909:926c with SMTP id k10-20020a056870148a00b000d3b909926cmr6698908oab.129.1645414111191; Sun, 20 Feb 2022 19:28:31 -0800 (PST) MIME-Version: 1.0 References: <20220217090440.4468-1-qiang.yu@amd.com> <5d3fdd2c-e74a-49f4-2b28-32c06483236f@amd.com> <6711073b-8771-5750-33f7-b72333b411c6@amd.com> <47c3a681-379e-18d4-86da-c48721081911@gmail.com> In-Reply-To: <47c3a681-379e-18d4-86da-c48721081911@gmail.com> From: Qiang Yu Date: Mon, 21 Feb 2022 11:28:20 +0800 Message-ID: Subject: Re: [PATCH] drm/amdgpu: check vm bo eviction valuable at last To: =?UTF-8?Q?Christian_K=C3=B6nig?= Cc: =?UTF-8?Q?Christian_K=C3=B6nig?= , David Airlie , "Pan, Xinhui" , Linux Kernel Mailing List , dri-devel , linaro-mm-sig@lists.linaro.org, Qiang Yu , amd-gfx@lists.freedesktop.org, Daniel Vetter , Alex Deucher , Sumit Semwal , linux-media@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 18, 2022 at 6:24 PM Christian K=C3=B6nig wrote: > > Am 18.02.22 um 11:16 schrieb Qiang Yu: > > [SNIP] > >>> If amdgpu_vm_ready() use evicting flag, it's still not equivalent to = check > >>> vm idle: true -> vm idle, false -> vm may be idle or busy. > >> Yeah, but why should that be relevant? > >> > >> The amdgpu_vm_ready() return if we can do page table updates or not. I= f > >> the VM is idle or not is only relevant for eviction. > >> > >> In other words any CS or page table update makes the VM busy, but that > >> only affects if the VM can be evicted or not. > >> > > My point is: we can't use amdgpu_vm_ready() to replace vm_is_busy(), so > > currently we update vm even when vm is busy. So why not use: Sorry, should be "vm is idle". > > if (!amdgpu_vm_ready() || vm_is_busy()) return; > > in amdgpu_gem_va_update_vm(), as you mentioned we prefer to not > > update vm when it's idle. > > Because updating the VM while it is busy is perfectly fine, we do it all > the time. > Yeah, as above, my typo. > We should just not update it when it is already idle and was considered > for eviction. "and", not "or"? > In this situation it makes most of the time sense to keep > it idle and postpone the update till the next command submission. > > >>>>> Then we can keep the evicting flag accurate (after solving your > >>>>> concern for this patch that eviction may fail latter by further del= ay > >>>>> the flag update after eviction success). > >>>> That won't work. See we need to mark the VM as evicted before we > >>>> actually evict them because otherwise somebody could use the VM in > >>>> parallel and add another fence to it. > >>>> > >>> I see, make this too accurate should cost too much like holding the > >>> eviction_lock when eviction. But just delay it in > >>> amdgpu_ttm_bo_eviction_valuable() > >>> could avoid most false positive case. > >> Partially correct. Another fundamental problem is that we can't hold t= he > >> eviction lock because that would result in lock inversion and potentia= l > >> deadlock. > >> > >> We could set the flag later on, but as I said before that when we set > >> the evicted flag when the VM is already idle is a desired effect. > >> > > As above, this confuse me as we can explicitly check vm idle when > > user update vm, why bother to embed it in evicting flag implicitly? > > Well as I said it's irrelevant for the update if the VM is idle or not. > > To summarize the rules once more: > 1. When VM page tables are used by CS or page tables updates it is > considered busy, e.g. not idle. > > 2. When we want to evict a VM it must be idle. As soon as we considered > this we should set the evicted flag to make sure to keep it idle as much > as possible. > > 3. When we want to update the page tables we just need to check if the > VM is idle or not. > But now we does not check vm idle directly in amdgpu_gem_va_update_vm(). If VM bo has not been considered for eviction, it could be either idle or b= usy. Just want to confirm if the fix should be only change amdgpu_vm_ready() to use evicting flag or besides using evicting flag, also check vm_idle() i= n amdgpu_gem_va_update_vm(). Regards, Qiang > 4. When a CS happens we don't have another chance and make the VM busy > again. And do all postponed page table updates. > Anyway, > Regards, > Christian. > > > > > Check vm idle need to hold resv lock. Read your patch for adding > > evicting flag is to update vm without resv lock. But user vm ops in > > amdgpu_gem_va_update_vm() do hold the resv lock, so the difference > > happens when calling amdgpu_vm_bo_update_mapping() from > > svm_range_(un)map_to_gpu(). So embed vm idle in evicting flag > > is for svm_range_(un)map_to_gpu() also do nothing when vm idle? > > > > > > > Regards, > > Qiang > > > >> Regards, > >> Christian. > >> > >>> Regards, > >>> Qiang > >>> > >>>> Regards, > >>>> Christian. > >>>> > >>>>> Regards, > >>>>> Qiang > >>>>> > >>>>> > >>>>>> Regards, > >>>>>> Christian. > >>>>>> > >>>>>>> Regards, > >>>>>>> Qiang > >>>>>>> > >>>>>>>> Regards, > >>>>>>>> Christian. > >>>>>>>> > >>>>>>>>> Regards, > >>>>>>>>> Qiang > >>>>>>>>> > >>>>>>>>>> Regards, > >>>>>>>>>> Christian. > >>>>>>>>>> > >>>>>>>>>>> Regards, > >>>>>>>>>>> Qiang > >>>>>>>>>>> > >>>>>>>>>>>> What we should rather do is to fix amdgpu_vm_ready() to take= a look at > >>>>>>>>>>>> the flag instead of the linked list. > >>>>>>>>>>>> > >>>>>>>>>>>> Regards, > >>>>>>>>>>>> Christian. > >>>>>>>>>>>> > >>>>>>>>>>>>> Signed-off-by: Qiang Yu > >>>>>>>>>>>>> --- > >>>>>>>>>>>>> drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 85 ++++++= ++++++++----------- > >>>>>>>>>>>>> 1 file changed, 47 insertions(+), 38 deletions(-) > >>>>>>>>>>>>> > >>>>>>>>>>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/driv= ers/gpu/drm/amd/amdgpu/amdgpu_ttm.c > >>>>>>>>>>>>> index 5a32ee66d8c8..88a27911054f 100644 > >>>>>>>>>>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c > >>>>>>>>>>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c > >>>>>>>>>>>>> @@ -1306,45 +1306,11 @@ uint64_t amdgpu_ttm_tt_pte_flags(st= ruct amdgpu_device *adev, struct ttm_tt *ttm, > >>>>>>>>>>>>> return flags; > >>>>>>>>>>>>> } > >>>>>>>>>>>>> > >>>>>>>>>>>>> -/* > >>>>>>>>>>>>> - * amdgpu_ttm_bo_eviction_valuable - Check to see if we ca= n evict a buffer > >>>>>>>>>>>>> - * object. > >>>>>>>>>>>>> - * > >>>>>>>>>>>>> - * Return true if eviction is sensible. Called by ttm_mem_= evict_first() on > >>>>>>>>>>>>> - * behalf of ttm_bo_mem_force_space() which tries to evict= buffer objects until > >>>>>>>>>>>>> - * it can find space for a new object and by ttm_bo_force_= list_clean() which is > >>>>>>>>>>>>> - * used to clean out a memory space. > >>>>>>>>>>>>> - */ > >>>>>>>>>>>>> -static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buf= fer_object *bo, > >>>>>>>>>>>>> - const struct ttm_= place *place) > >>>>>>>>>>>>> +static bool amdgpu_ttm_mem_eviction_valuable(struct ttm_bu= ffer_object *bo, > >>>>>>>>>>>>> + const struct ttm= _place *place) > >>>>>>>>>>>>> { > >>>>>>>>>>>>> unsigned long num_pages =3D bo->resource->num_p= ages; > >>>>>>>>>>>>> struct amdgpu_res_cursor cursor; > >>>>>>>>>>>>> - struct dma_resv_list *flist; > >>>>>>>>>>>>> - struct dma_fence *f; > >>>>>>>>>>>>> - int i; > >>>>>>>>>>>>> - > >>>>>>>>>>>>> - /* Swapout? */ > >>>>>>>>>>>>> - if (bo->resource->mem_type =3D=3D TTM_PL_SYSTEM) > >>>>>>>>>>>>> - return true; > >>>>>>>>>>>>> - > >>>>>>>>>>>>> - if (bo->type =3D=3D ttm_bo_type_kernel && > >>>>>>>>>>>>> - !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo))) > >>>>>>>>>>>>> - return false; > >>>>>>>>>>>>> - > >>>>>>>>>>>>> - /* If bo is a KFD BO, check if the bo belongs to the = current process. > >>>>>>>>>>>>> - * If true, then return false as any KFD process need= s all its BOs to > >>>>>>>>>>>>> - * be resident to run successfully > >>>>>>>>>>>>> - */ > >>>>>>>>>>>>> - flist =3D dma_resv_shared_list(bo->base.resv); > >>>>>>>>>>>>> - if (flist) { > >>>>>>>>>>>>> - for (i =3D 0; i < flist->shared_count; ++i) { > >>>>>>>>>>>>> - f =3D rcu_dereference_protected(flist= ->shared[i], > >>>>>>>>>>>>> - dma_resv_held(bo->base.resv))= ; > >>>>>>>>>>>>> - if (amdkfd_fence_check_mm(f, current-= >mm)) > >>>>>>>>>>>>> - return false; > >>>>>>>>>>>>> - } > >>>>>>>>>>>>> - } > >>>>>>>>>>>>> > >>>>>>>>>>>>> switch (bo->resource->mem_type) { > >>>>>>>>>>>>> case AMDGPU_PL_PREEMPT: > >>>>>>>>>>>>> @@ -1377,10 +1343,53 @@ static bool amdgpu_ttm_bo_eviction_= valuable(struct ttm_buffer_object *bo, > >>>>>>>>>>>>> return false; > >>>>>>>>>>>>> > >>>>>>>>>>>>> default: > >>>>>>>>>>>>> - break; > >>>>>>>>>>>>> + return ttm_bo_eviction_valuable(bo, place); > >>>>>>>>>>>>> } > >>>>>>>>>>>>> +} > >>>>>>>>>>>>> > >>>>>>>>>>>>> - return ttm_bo_eviction_valuable(bo, place); > >>>>>>>>>>>>> +/* > >>>>>>>>>>>>> + * amdgpu_ttm_bo_eviction_valuable - Check to see if we ca= n evict a buffer > >>>>>>>>>>>>> + * object. > >>>>>>>>>>>>> + * > >>>>>>>>>>>>> + * Return true if eviction is sensible. Called by ttm_mem_= evict_first() on > >>>>>>>>>>>>> + * behalf of ttm_bo_mem_force_space() which tries to evict= buffer objects until > >>>>>>>>>>>>> + * it can find space for a new object and by ttm_bo_force_= list_clean() which is > >>>>>>>>>>>>> + * used to clean out a memory space. > >>>>>>>>>>>>> + */ > >>>>>>>>>>>>> +static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buf= fer_object *bo, > >>>>>>>>>>>>> + const struct ttm_= place *place) > >>>>>>>>>>>>> +{ > >>>>>>>>>>>>> + struct dma_resv_list *flist; > >>>>>>>>>>>>> + struct dma_fence *f; > >>>>>>>>>>>>> + int i; > >>>>>>>>>>>>> + > >>>>>>>>>>>>> + /* Swapout? */ > >>>>>>>>>>>>> + if (bo->resource->mem_type =3D=3D TTM_PL_SYSTEM) > >>>>>>>>>>>>> + return true; > >>>>>>>>>>>>> + > >>>>>>>>>>>>> + /* If bo is a KFD BO, check if the bo belongs to the = current process. > >>>>>>>>>>>>> + * If true, then return false as any KFD process need= s all its BOs to > >>>>>>>>>>>>> + * be resident to run successfully > >>>>>>>>>>>>> + */ > >>>>>>>>>>>>> + flist =3D dma_resv_shared_list(bo->base.resv); > >>>>>>>>>>>>> + if (flist) { > >>>>>>>>>>>>> + for (i =3D 0; i < flist->shared_count; ++i) { > >>>>>>>>>>>>> + f =3D rcu_dereference_protected(flist= ->shared[i], > >>>>>>>>>>>>> + dma_resv_held(bo->base.resv))= ; > >>>>>>>>>>>>> + if (amdkfd_fence_check_mm(f, current-= >mm)) > >>>>>>>>>>>>> + return false; > >>>>>>>>>>>>> + } > >>>>>>>>>>>>> + } > >>>>>>>>>>>>> + > >>>>>>>>>>>>> + /* Check by different mem type. */ > >>>>>>>>>>>>> + if (!amdgpu_ttm_mem_eviction_valuable(bo, place)) > >>>>>>>>>>>>> + return false; > >>>>>>>>>>>>> + > >>>>>>>>>>>>> + /* VM bo should be checked at last because it will ma= rk VM evicting. */ > >>>>>>>>>>>>> + if (bo->type =3D=3D ttm_bo_type_kernel) > >>>>>>>>>>>>> + return amdgpu_vm_evictable(ttm_to_amdgpu_bo(b= o)); > >>>>>>>>>>>>> + > >>>>>>>>>>>>> + return true; > >>>>>>>>>>>>> } > >>>>>>>>>>>>> > >>>>>>>>>>>>> static void amdgpu_ttm_vram_mm_access(struct amdgpu= _device *adev, loff_t pos, >