Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp549937rdb; Fri, 26 Jan 2024 03:57:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IEO3MSxlUQh2pMg9rNUUMwdg1JYnfEMR1gLrl9OO1xSldPAA5ByZ8s66J17YUzInYrEiSO8 X-Received: by 2002:a05:6a21:99a9:b0:19a:b7e5:7f05 with SMTP id ve41-20020a056a2199a900b0019ab7e57f05mr1148133pzb.124.1706270264292; Fri, 26 Jan 2024 03:57:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706270264; cv=pass; d=google.com; s=arc-20160816; b=zRwf8Omkd9BAmOYz3G/8A8G0aI3SxpGomtHgdeAxvj1i8O8Evd9ZMLCi0YntFymqlq PvP7PkUPL0X2XUljFg1A/fZBpd8EYEFb8+6oxnaAR6j9amt85KCBlALkN8UDahKq7I5j KRjwX6ZtnryfB0GT6RXkLn7ui+hvwut0hGFfjMx2MeA8EM6SyKahpD/kURC/CyWhMqJU dJ9FpM7MDsoEpIxIJzWcOl29cwgyG8rIfHqV2s5jdNSlBkKoq9AcjpR3j5Fv5A3ZjWLA BhBhyJtdH3PvF7D5WNvaQLkkOcV4f7SEaGKIgPauefs0z40pDMbR4vVEIxmWOMg+Zxre +A/g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=/LhsHaVZtlB/5FVXUe+EuM9FGbxN3tv/2W2Ta8tVnMA=; fh=SK54qfQNgoknvsVpfqWk59YanMevPo3MsvKqcWWyc8o=; b=SEYD8sWZkQkcSfNy3FhDcP07ILdvtb5Nan4lxFXnYAlBE1jq5YYY7SDKTdex7A8Va5 w+qrEcTPlGZlJCGC/jUig8j2n6K598DNODQBizj646/w7muXeUfcu1ZHQPNm74uuebQ4 2M1CmqVraISD+ldEj7Qx74A10qkXbjHPlfHLo0IUOqZ1z+NQKldN8Q9/2+MjJmzjnp2Z EhgIj8EO2dd9Bg1VtrTP9fDi5sug0oKgx4NPmpPSgascWRLSYMwQMk6qxj3MqVy7nVn4 DETwP58dvkWHBnSK1CY5CclEaFZj065UEHkuamgL7Nq0vB0cYuM1bca3/bKtlYQ2SUE1 N+Kg== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-40044-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40044-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id cl18-20020a056a02099200b005cfbe6ebf60si1037358pgb.775.2024.01.26.03.57.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 03:57:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40044-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-40044-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40044-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 4EFD1B285E9 for ; Fri, 26 Jan 2024 11:26:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 66AAE1428D; Fri, 26 Jan 2024 11:26:50 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2D7F213FE4 for ; Fri, 26 Jan 2024 11:26:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706268409; cv=none; b=CQHLZik480/hgP5IKz+KLk5WoVZ0sLAbAbq3CEaPSuLu/6tMw5ribdmz/EACxozFbtDtGJb5b/dRt8t69tk+/VXzrIued4KFn7w7QgSO+AZothcf6JGgC21Hr9HOVpQgKi+FlYu1bZ6/rZu2E1Oa9GGCi9Fjn6a0bJYp6HBjrz8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706268409; c=relaxed/simple; bh=gu0PhGkylvtrDWf8V2mRv6VBhqsE4IZZB78F3hEY5lA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=a1M4taCTmEtXOvRHJIVUqKCH2dCKPHqckz24JdxNOR3U3SKADHH1nS2pMYfPYqNUKmAstadZObTlHfjhtrTUIlS0wm5/oWpccALfuI2NeSTiYPii4NlE9OHxdg5CxYs/DXkw1iMt/yCQCo+B7l2gB7gmTCWfFHZGuki0JqqjZBM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 463E21FB; Fri, 26 Jan 2024 03:27:24 -0800 (PST) Received: from [10.1.196.86] (unknown [10.1.196.86]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7BAFD3F762; Fri, 26 Jan 2024 03:26:37 -0800 (PST) Message-ID: Date: Fri, 26 Jan 2024 11:26:36 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v19 18/30] drm/panfrost: Explicitly get and put drm-shmem pages To: Boris Brezillon Cc: Dmitry Osipenko , David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , =?UTF-8?Q?Christian_K=C3=B6nig?= , Qiang Yu , Emma Anholt , Melissa Wen , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org References: <20240105184624.508603-1-dmitry.osipenko@collabora.com> <20240105184624.508603-19-dmitry.osipenko@collabora.com> <7144dd9b-62d1-4968-9b94-0313e2475f7e@arm.com> <20240126103924.0b911a4f@collabora.com> Content-Language: en-GB From: Steven Price In-Reply-To: <20240126103924.0b911a4f@collabora.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 26/01/2024 09:39, Boris Brezillon wrote: > On Thu, 25 Jan 2024 16:47:24 +0000 > Steven Price wrote: > >> On 05/01/2024 18:46, Dmitry Osipenko wrote: >>> To simplify the drm-shmem refcnt handling, we're moving away from >>> the implicit get_pages() that is used by get_pages_sgt(). From now on >>> drivers will have to pin pages while they use sgt. Panfrost's shrinker >>> doesn't support swapping out BOs, hence pages are pinned and sgt is valid >>> as long as pages' use-count > 0. >>> >>> In Panfrost, panfrost_gem_mapping, which is the object representing a >>> GPU mapping of a BO, owns a pages ref. This guarantees that any BO being >>> mapped GPU side has its pages retained till the mapping is destroyed. >>> >>> Since pages are no longer guaranteed to stay pinned for the BO lifetime, >>> and MADVISE(DONT_NEED) flagging remains after the GEM handle has been >>> destroyed, we need to add an extra 'is_purgeable' check in >>> panfrost_gem_purge(), to make sure we're not trying to purge a BO that >>> already had its pages released. >>> >>> Signed-off-by: Dmitry Osipenko >> >> Reviewed-by: Steven Price >> >> Although I don't like the condition in panfrost_gem_mapping_release() >> for drm_gem_shmem_put_pages() and assigning NULL to bo->sgts - it feels >> very fragile. See below. >> >>> --- >>> drivers/gpu/drm/panfrost/panfrost_gem.c | 63 ++++++++++++++----- >>> .../gpu/drm/panfrost/panfrost_gem_shrinker.c | 6 ++ >>> 2 files changed, 52 insertions(+), 17 deletions(-) >>> >>> diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c >>> index f268bd5c2884..7edfc12f7c1f 100644 >>> --- a/drivers/gpu/drm/panfrost/panfrost_gem.c >>> +++ b/drivers/gpu/drm/panfrost/panfrost_gem.c >>> @@ -35,20 +35,6 @@ static void panfrost_gem_free_object(struct drm_gem_object *obj) >>> */ >>> WARN_ON_ONCE(!list_empty(&bo->mappings.list)); >>> >>> - if (bo->sgts) { >>> - int i; >>> - int n_sgt = bo->base.base.size / SZ_2M; >>> - >>> - for (i = 0; i < n_sgt; i++) { >>> - if (bo->sgts[i].sgl) { >>> - dma_unmap_sgtable(pfdev->dev, &bo->sgts[i], >>> - DMA_BIDIRECTIONAL, 0); >>> - sg_free_table(&bo->sgts[i]); >>> - } >>> - } >>> - kvfree(bo->sgts); >>> - } >>> - >>> drm_gem_shmem_free(&bo->base); >>> } >>> >>> @@ -85,11 +71,40 @@ panfrost_gem_teardown_mapping(struct panfrost_gem_mapping *mapping) >>> >>> static void panfrost_gem_mapping_release(struct kref *kref) >>> { >>> - struct panfrost_gem_mapping *mapping; >>> - >>> - mapping = container_of(kref, struct panfrost_gem_mapping, refcount); >>> + struct panfrost_gem_mapping *mapping = >>> + container_of(kref, struct panfrost_gem_mapping, refcount); >>> + struct panfrost_gem_object *bo = mapping->obj; >>> + struct panfrost_device *pfdev = bo->base.base.dev->dev_private; >>> >>> panfrost_gem_teardown_mapping(mapping); >>> + >>> + /* On heap BOs, release the sgts created in the fault handler path. */ >>> + if (bo->sgts) { >>> + int i, n_sgt = bo->base.base.size / SZ_2M; >>> + >>> + for (i = 0; i < n_sgt; i++) { >>> + if (bo->sgts[i].sgl) { >>> + dma_unmap_sgtable(pfdev->dev, &bo->sgts[i], >>> + DMA_BIDIRECTIONAL, 0); >>> + sg_free_table(&bo->sgts[i]); >>> + } >>> + } >>> + kvfree(bo->sgts); >>> + } >>> + >>> + /* Pages ref is owned by the panfrost_gem_mapping object. We must >>> + * release our pages ref (if any), before releasing the object >>> + * ref. >>> + * Non-heap BOs acquired the pages at panfrost_gem_mapping creation >>> + * time, and heap BOs may have acquired pages if the fault handler >>> + * was called, in which case bo->sgts should be non-NULL. >>> + */ >>> + if (!bo->base.base.import_attach && (!bo->is_heap || bo->sgts) && >>> + bo->base.madv >= 0) { >>> + drm_gem_shmem_put_pages(&bo->base); >>> + bo->sgts = NULL; >> >> The assignment of NULL here really ought to be unconditional - it isn't >> a valid pointer because of the kvfree() above. > > Fair enough. How about we drop the '|| bo->sgts' and add an > drm_gem_shmem_put_pages() to the above if (bo->sgts) block, where we'll > also assign bo->sgts to NULL? Yes that would be good. >> >> I also feel that the big condition above suggests there's a need for a >> better state machine to keep track of what's going on. > > I'm planning to extend drm_gem_shmem to support the alloc-on-fault use > case that all Mali GPUs seem to rely on (lima, panfrost and soon > panthor would use those helpers). The idea is to: > > - make the allocation non-blocking, so we can kill the blocking > allocation in the dma signalling path (basically what intel does) > - allow dynamic extension of the pages array using an xarray instead of > a plain array > > Hopefully this makes the state tracking a lot easier, and we can also > get rid of the hack we have in panfrost/lima where we manipulate > drm_gem_shmem_object refcounts directly. That sounds great - it would definitely be good to get rid of the refcount hack, it confuses me everytime ;) Thanks, Steve >> >> But having said that I do think this series as a whole is an >> improvement, it's nice to get the shrinker code generic. And sadly I >> don't have an immediate idea for cleaning this up, hence my R-b. >> >> Steve >> >>> + } >>> + >>> drm_gem_object_put(&mapping->obj->base.base); >>> panfrost_mmu_ctx_put(mapping->mmu); >>> kfree(mapping); >>> @@ -125,6 +140,20 @@ int panfrost_gem_open(struct drm_gem_object *obj, struct drm_file *file_priv) >>> if (!mapping) >>> return -ENOMEM; >>> >>> + if (!bo->is_heap && !bo->base.base.import_attach) { >>> + /* Pages ref is owned by the panfrost_gem_mapping object. >>> + * For non-heap BOs, we request pages at mapping creation >>> + * time, such that the panfrost_mmu_map() call, further down in >>> + * this function, is guaranteed to have pages_use_count > 0 >>> + * when drm_gem_shmem_get_pages_sgt() is called. >>> + */ >>> + ret = drm_gem_shmem_get_pages(&bo->base); >>> + if (ret) { >>> + kfree(mapping); >>> + return ret; >>> + } >>> + } >>> + >>> INIT_LIST_HEAD(&mapping->node); >>> kref_init(&mapping->refcount); >>> drm_gem_object_get(obj); >>> diff --git a/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c b/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c >>> index 02b60ea1433a..d4fb0854cf2f 100644 >>> --- a/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c >>> +++ b/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c >>> @@ -50,6 +50,12 @@ static bool panfrost_gem_purge(struct drm_gem_object *obj) >>> if (!dma_resv_trylock(shmem->base.resv)) >>> goto unlock_mappings; >>> >>> + /* BO might have become unpurgeable if the last pages_use_count ref >>> + * was dropped, but the BO hasn't been destroyed yet. >>> + */ >>> + if (!drm_gem_shmem_is_purgeable(shmem)) >>> + goto unlock_mappings; >>> + >>> panfrost_gem_teardown_mappings_locked(bo); >>> drm_gem_shmem_purge_locked(&bo->base); >>> ret = true; >> >