Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp1086201pxb; Thu, 17 Feb 2022 23:51:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJwKQ+HSJNpoipDBIzzn9EjCQf28IBWYP/EYRaEWEtwY6TeQvpEQl66ZhPXB+mucX+lSZoF8 X-Received: by 2002:a17:903:1210:b0:14e:e194:2f2c with SMTP id l16-20020a170903121000b0014ee1942f2cmr6418929plh.130.1645170670857; Thu, 17 Feb 2022 23:51:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645170670; cv=none; d=google.com; s=arc-20160816; b=sSP0TCOgMxBFuPn/IdJvdVPO462aeeVTj1zFlAz+8blZULEhx+A7npe4dVCKHM1nF4 KyJp4acQk4GEt4k3Jc9wQSynslAX9MWIsOlk8qcaeqwQEq4E5X8txa1ubLFUIB4bcCnK FgBAi6wT8AToscCG8Bl7Gw6KoeyzmqkQqC7SofPT2O8l0ZucF4vz8MvDSSIXWprCMWUq Hy54T/G9F4anod1xftUx811am32DiTQfona3VIUbolAGJ9K2zfbRLKt3hupGZ2pXDwyV fVB9k8sqwfXCKNLz9zOn4Sj36/LERJjsEH7TMpAxV1lQ8aJzHoCh+FbO1B7OtpirhL6v kYFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=U5olLpLCoghEHY1TCG3d3Y+Izm3Dlu4bh0SXakm9Uoo=; b=yhQ8JwNVjPjkabF1vbeEuh8QkpF13W1ogaVU1N+xPHJmwYuqEg0DNoV2hvANGHXQTF 6jL0l4OMgWcHpLZedPRZDiEnV7VxWTWvPjMwXU8TS2lA6PWV5ZfHYgIBbxWrkoz6h66w 1T13fYc+0nxkmrAbV+YQePFlheMPqmJB29KwrgWcY1aTifX6WA/Gwb8SzeEcC9gJOokU yHT65Ts5YQcFZ2KA9/hkoOBHJT3vRg/+8M8mZ6OYPXYF7EjamxH7SH1GzvIGgtlVWIG8 bBceMKnQIWKzghaW4HV0dZS83ilJBieN8iif40YMzpniltk28qMn/MIRq/YTwfbW0exQ C8OA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=F2w8q2qm; 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 a24si10457756pgl.439.2022.02.17.23.50.54; Thu, 17 Feb 2022 23:51:10 -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=F2w8q2qm; 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 S231804AbiBRHqW (ORCPT + 99 others); Fri, 18 Feb 2022 02:46:22 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:58770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229750AbiBRHqV (ORCPT ); Fri, 18 Feb 2022 02:46:21 -0500 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57E3E5E178; Thu, 17 Feb 2022 23:46:04 -0800 (PST) Received: by mail-ed1-x52d.google.com with SMTP id b13so14085449edn.0; Thu, 17 Feb 2022 23:46:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=U5olLpLCoghEHY1TCG3d3Y+Izm3Dlu4bh0SXakm9Uoo=; b=F2w8q2qmgX4O8EXvRpOJqfH1BIxaJaYjl2r31xfBPIGHUyGiwgy4+5a3SUfMU9kIPg 1SM5oG2xKBaUVU94NTAcwBCucXC8I1wwuvU1iq7kTzxc7m5GEa/8B7gBzYbFU2B6p2+B 6H4aIZ5glaNVXa/x3LMgNcbvYDKHOMLDI1iKGXKlIOyNQuDSQRh2908h4pLkrqpf66vh VLPcydVUQM+0oKmmX88FpobYVo8Y8ETD/+kILmSqxT0MCCq0nOEo1EV5tZVxLgnCeDE1 xpzQYZn33nE2f4FrNnodtoXBs/GG7nn5NzM7SDWoBG7r+FdLLkxHUOnDB42VUAkVxkZ8 eIGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=U5olLpLCoghEHY1TCG3d3Y+Izm3Dlu4bh0SXakm9Uoo=; b=gsGzhdOIta5hSydMSY3aEfj52dQZ2rtwyvTXN/6Fe2DFe5CtNAG4Ng6/O2SnVWiaSZ +AmJIAvT6ujcCfyyzIfveICqXVLMLVumbUhFVU5prjsczaSYEIcjAgL7uQRhJhQ1YEcM 3Gjo2L+Ne14EeuRHhEHOjDAMM/AnR3klrhY/7brA58WKLw7ZkzK/dWDes648N5ToD6c4 s3ZbDXc6isOiZWrlwCMTmLHQJKoCo1ts+ttx7KXNkjLNbRnXdwCJ38NG784egMoZtL8m NGccaoAD2j9f1k2885vY2FfzvkJTIOgAEKqliQfPIYWlCh/vlBcwzwANAxbtscv+aSgM zo1w== X-Gm-Message-State: AOAM5322hxfkTF+F46eJUWrd6kK97FsMoX5Xrxs2whKUyZN8Vcy/dR0F YU+Vo39qrQjBPfReiRnI9wY= X-Received: by 2002:a05:6402:11cb:b0:3fd:abfa:1eef with SMTP id j11-20020a05640211cb00b003fdabfa1eefmr6721618edw.341.1645170362684; Thu, 17 Feb 2022 23:46:02 -0800 (PST) Received: from [192.168.178.21] (p57b0bff8.dip0.t-ipconnect.de. [87.176.191.248]) by smtp.gmail.com with ESMTPSA id z8sm1905551ejc.197.2022.02.17.23.46.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Feb 2022 23:46:02 -0800 (PST) Message-ID: Date: Fri, 18 Feb 2022 08:46:00 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH] drm/amdgpu: check vm bo eviction valuable at last Content-Language: en-US To: Qiang Yu , =?UTF-8?Q?Christian_K=c3=b6nig?= Cc: 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 References: <20220217090440.4468-1-qiang.yu@amd.com> <5d3fdd2c-e74a-49f4-2b28-32c06483236f@amd.com> <6711073b-8771-5750-33f7-b72333b411c6@amd.com> From: =?UTF-8?Q?Christian_K=c3=b6nig?= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, 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 Am 18.02.22 um 04:08 schrieb Qiang Yu: > On Thu, Feb 17, 2022 at 8:22 PM Christian König > wrote: >> Am 17.02.22 um 11:58 schrieb Qiang Yu: >>> On Thu, Feb 17, 2022 at 6:39 PM Christian König >>> wrote: >>>> >>>> Am 17.02.22 um 11:13 schrieb Qiang Yu: >>>>> On Thu, Feb 17, 2022 at 5:46 PM Christian König >>>>> wrote: >>>>>> Am 17.02.22 um 10:40 schrieb Qiang Yu: >>>>>>> On Thu, Feb 17, 2022 at 5:15 PM Christian König >>>>>>> wrote: >>>>>>>> Am 17.02.22 um 10:04 schrieb Qiang Yu: >>>>>>>>> Workstation application ANSA/META get this error dmesg: >>>>>>>>> [drm:amdgpu_gem_va_ioctl [amdgpu]] *ERROR* Couldn't update BO_VA (-16) >>>>>>>>> >>>>>>>>> This is caused by: >>>>>>>>> 1. create a 256MB buffer in invisible VRAM >>>>>>>>> 2. CPU map the buffer and access it causes vm_fault and try to move >>>>>>>>> it to visible VRAM >>>>>>>>> 3. force visible VRAM space and traverse all VRAM bos to check if >>>>>>>>> evicting this bo is valuable >>>>>>>>> 4. when checking a VM bo (in invisible VRAM), amdgpu_vm_evictable() >>>>>>>>> will set amdgpu_vm->evicting, but latter due to not in visible >>>>>>>>> VRAM, won't really evict it so not add it to amdgpu_vm->evicted >>>>>>>>> 5. before next CS to clear the amdgpu_vm->evicting, user VM ops >>>>>>>>> ioctl will pass amdgpu_vm_ready() (check amdgpu_vm->evicted) >>>>>>>>> but fail in amdgpu_vm_bo_update_mapping() (check >>>>>>>>> amdgpu_vm->evicting) and get this error log >>>>>>>>> >>>>>>>>> This error won't affect functionality as next CS will finish the >>>>>>>>> waiting VM ops. But we'd better make the amdgpu_vm->evicting >>>>>>>>> correctly reflact the vm status and clear the error log. >>>>>>>> Well NAK, that is intentional behavior. >>>>>>>> >>>>>>>> The VM page tables where considered for eviction, so setting the flag is >>>>>>>> correct even when the page tables later on are not actually evicted. >>>>>>>> >>>>>>> But this will unnecessarily stop latter user VM ops in ioctl before CS >>>>>>> even when the VM bos are not evicted. >>>>>>> Won't this have any negative effect when could do better? >>>>>> No, this will have a positive effect. See the VM was already considered >>>>>> for eviction because it is idle. >>>>>> >>>>>> Updating it immediately doesn't necessarily make sense, we should wait >>>>>> with that until its next usage. >>>>>> >>>>>> Additional to that this patch doesn't really fix the problem, it just >>>>>> mitigates it. >>>>>> >>>>>> Eviction can fail later on for a couple of reasons and we absolutely >>>>>> need to check the flag instead of the list in amdgpu_vm_ready(). >>>>> The flag only for both flag and list? Looks like should be both as >>>>> the list indicate some vm page table need to be updated and could >>>>> delay the user update with the same logic as you described above. >>>> I think checking the flag should be enough. The issue is that the list >>>> was there initially, but to avoid race conditions we added the flag with >>>> separate lock protection later on. >>>> >>> But list and flag does not align always, there are cases like >>> list-empty/flag-set (this problem) and list-non-empty/flag-unset (non-vm bo >>> eviction). If only check flag list-non-empty/flag-unset change behavior. >> Yeah, but I think that the flag unset list-non-empty case would be >> correctly handled if we only test the flag. >> >> In other words we can update the page tables as long as they are not >> partially or fully evicted and that's not the case when non-vm BOs are >> evicted. >> > This sounds like two standard for the same thing, because this problem > does not evict page tables too. But I see your point is: > There's a difference that this problem's case can make sure vm is idle, > and we prefer to delay vm updates when vm is idle. > > If so, why not just stop user vm update by checking vm busy in > amdgpu_gem_va_ioctl() to skip amdgpu_gem_va_update_vm()? That's exactly what amdgpu_gem_va_update_vm() is doing by calling amdgpu_vm_ready(). The problem is that amdgpu_vm_ready() looks at the wrong thing. > Then we can keep the evicting flag accurate (after solving your > concern for this patch that eviction may fail latter by further delay > 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. 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/drivers/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(struct amdgpu_device *adev, struct ttm_tt *ttm, >>>>>>>>> return flags; >>>>>>>>> } >>>>>>>>> >>>>>>>>> -/* >>>>>>>>> - * amdgpu_ttm_bo_eviction_valuable - Check to see if we can 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_buffer_object *bo, >>>>>>>>> - const struct ttm_place *place) >>>>>>>>> +static bool amdgpu_ttm_mem_eviction_valuable(struct ttm_buffer_object *bo, >>>>>>>>> + const struct ttm_place *place) >>>>>>>>> { >>>>>>>>> unsigned long num_pages = bo->resource->num_pages; >>>>>>>>> struct amdgpu_res_cursor cursor; >>>>>>>>> - struct dma_resv_list *flist; >>>>>>>>> - struct dma_fence *f; >>>>>>>>> - int i; >>>>>>>>> - >>>>>>>>> - /* Swapout? */ >>>>>>>>> - if (bo->resource->mem_type == TTM_PL_SYSTEM) >>>>>>>>> - return true; >>>>>>>>> - >>>>>>>>> - if (bo->type == 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 needs all its BOs to >>>>>>>>> - * be resident to run successfully >>>>>>>>> - */ >>>>>>>>> - flist = dma_resv_shared_list(bo->base.resv); >>>>>>>>> - if (flist) { >>>>>>>>> - for (i = 0; i < flist->shared_count; ++i) { >>>>>>>>> - f = 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 can 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_buffer_object *bo, >>>>>>>>> + const struct ttm_place *place) >>>>>>>>> +{ >>>>>>>>> + struct dma_resv_list *flist; >>>>>>>>> + struct dma_fence *f; >>>>>>>>> + int i; >>>>>>>>> + >>>>>>>>> + /* Swapout? */ >>>>>>>>> + if (bo->resource->mem_type == 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 needs all its BOs to >>>>>>>>> + * be resident to run successfully >>>>>>>>> + */ >>>>>>>>> + flist = dma_resv_shared_list(bo->base.resv); >>>>>>>>> + if (flist) { >>>>>>>>> + for (i = 0; i < flist->shared_count; ++i) { >>>>>>>>> + f = 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 mark VM evicting. */ >>>>>>>>> + if (bo->type == ttm_bo_type_kernel) >>>>>>>>> + return amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo)); >>>>>>>>> + >>>>>>>>> + return true; >>>>>>>>> } >>>>>>>>> >>>>>>>>> static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,