Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1609151rdb; Thu, 25 Jan 2024 00:11:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzJru9jo6z81mtKnfC0ZN+AGw+2I/sNhI9kJWvVdG/XHHhXT3Zkhuhy1Yb4ISSzlzt939V X-Received: by 2002:a17:902:a5c9:b0:1d7:57a3:4de0 with SMTP id t9-20020a170902a5c900b001d757a34de0mr528605plq.94.1706170269780; Thu, 25 Jan 2024 00:11:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706170269; cv=pass; d=google.com; s=arc-20160816; b=miHTi/AvWHJ+5UazbFn1fqDlFvBSWod0jjC6F8mSEnNU+jJmWlJyDyvpjigF5w84/g 7ahAZKqXvHm5tVrUkxjpnI6TrKfl4F+kSFMHgHJxwY7FMRBZyKix2Lx4nOGIPlmSHaN9 uskmZk7cUAZX3KkhfN6PZdRiPhNkH7BCbms1RZ4jd/mVKkxyCGYTe+mApXaXGieh7STe VvcZpSHX6bahB2fAf15BHxFP3aQpr346omwKzU5r2GablhluGeKR2PcUFGmNVBDlIQf9 ttw86/iVgJ/iGk1rhv4kZeZrTTb5GwLcofZsNNqXEtLfWUKTwc8rRqXEf5pgDpZ7jvpb k/Lg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:organization:references :in-reply-to:message-id:subject:cc:to:from:date:dkim-signature; bh=Dc9EzLjDN8Lidv8qDj5bf6Li62zCizph2K13XkSamrI=; fh=7/8YbX67hBmP28Tlg+8TabviGOWcDJYLtr8c3ZgQhbw=; b=TWDvtPeP70vZ+Ey3CW7y1qgU1EwnbFLWXsx+bvf9HveetDaBLP/fE0cTDe4i5Gum2w 7Il3Xomk6SLkD4Ox+SQ9jrQzzlzblUPKAjfTdJSJpz+6ltV811OvlpzudMI+gXmBLiM+ XiYTJfV3+hSbXRcCQP7zAmxy4+nc5O70aAgHhBbGKQ3kTjRTuzmChzEpt0mD3NQhC3YI erjEpWwCGAy/mJC8T1wfDq04IpiF2LRRIiQYS4j4DZ9lHECkOb8fPPNdGQNM8Cv+n2yq kS0nU3+7OkjJ1i/0Sfl7d+NzdZfUNsU/u1KEuoDlqYlJjdoH9i6vbNBuc5ClN/uVJIVW GV9g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=bL1MfMsE; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-38135-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38135-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id u18-20020a170902e81200b001d78b8bf305si1848460plg.182.2024.01.25.00.11.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 00:11:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-38135-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=bL1MfMsE; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-38135-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38135-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 2EE9F289AC5 for ; Thu, 25 Jan 2024 08:10:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 464B617593; Thu, 25 Jan 2024 08:10:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="bL1MfMsE" Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD8ED17580 for ; Thu, 25 Jan 2024 08:10:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706170216; cv=none; b=euJmS/xz2Ldnx8k5a2wKcnnKP/U3Oeq1g3EyQFY0Qb033VDPs96QAGgJx78hsa9OpFIFghE+we3wIljV196oYje4YMBPMIcjAFr7nvATnymtKmrHiMUHku5L+h20nel+R48+aFHldoZOVpwQIlTjhK7lOQpex8jKPp4u4bsPWGI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706170216; c=relaxed/simple; bh=YRb+Ukyjfck2mZNlu30x2+P6GcmpGkv7SrsqXba6R74=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RYrtPD7R7qUvgUGTM8SMFbOo3kMjuQqedVmI+OAZCvj93487kRhclDndRFkrr+iDKkiyGUM+mtOB5JFkezEGWWWzCNz9TbtR2uQdukO7PAUJ3tQGo1ZCKXWgt8Q7AWABzYAb9yC0EVRgJuzYuAoJlO8oVOkbD4ma/9ldPoi0jhE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=bL1MfMsE; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1706170212; bh=YRb+Ukyjfck2mZNlu30x2+P6GcmpGkv7SrsqXba6R74=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=bL1MfMsEpbV/TlkfPY/kiTrDi7jzXOpa8gw7YXNDcxv0jgzZpDnWkFTaYB+BFcX/Q tfG+N9jFlB8gSoW2xKD08sG5tA+KJ+M9o3KNXWiV7zQ/ecDNze6/tHh1/2q5Ihx4D1 oj6mgRfgqPBA+D84kjsBU4nWBKnREh8t2YwRx5jUlybrrepPLg4/hSXc9qCerQ3smg 5IDGDP5X2tZT4LILq85YdIBnowAzMbgv4TobKGUXz9YQuRBWqVeyBtCRiX42Hhe9KS OKf+/XMDnLKCSNjsaVb0sozVrO2djkKvNUpDb02UfEv6F6GWWrF/7pNhGwlRsjONSr PRaJnv8i/hpcg== Received: from localhost (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 6058F3781FC7; Thu, 25 Jan 2024 08:10:11 +0000 (UTC) Date: Thu, 25 Jan 2024 09:10:09 +0100 From: Boris Brezillon To: Dmitry Osipenko , Steven Price Cc: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Christian =?UTF-8?B?S8O2bmln?= , Qiang Yu , Emma Anholt , Melissa Wen , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org Subject: Re: [PATCH v19 18/30] drm/panfrost: Explicitly get and put drm-shmem pages Message-ID: <20240125091009.01425434@collabora.com> In-Reply-To: <20240105184624.508603-19-dmitry.osipenko@collabora.com> References: <20240105184624.508603-1-dmitry.osipenko@collabora.com> <20240105184624.508603-19-dmitry.osipenko@collabora.com> Organization: Collabora X-Mailer: Claws Mail 4.2.0 (GTK 3.24.38; x86_64-redhat-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 5 Jan 2024 21:46:12 +0300 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: Boris Brezillon But I'd like to have Steve's review as well on that one. > --- > 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; > + } > + > 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;