From nobody Thu Feb 12 06:34:18 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B55DEEAA7B for ; Thu, 14 Sep 2023 23:29:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230380AbjINX3V (ORCPT ); Thu, 14 Sep 2023 19:29:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230310AbjINX3L (ORCPT ); Thu, 14 Sep 2023 19:29:11 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C68D02717 for ; Thu, 14 Sep 2023 16:29:06 -0700 (PDT) Received: from workpc.. (109-252-153-31.dynamic.spd-mgts.ru [109.252.153.31]) (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: dmitry.osipenko) by madras.collabora.co.uk (Postfix) with ESMTPSA id 723CF6607346; Fri, 15 Sep 2023 00:29:04 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1694734145; bh=+WahLq2KPu3N5dEfC75pioBsE8NOud04/kmdVqiq2D0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GkYtGMcoGjBdC1njqnU7b0hRj4d1iM2gO9LPLH1wXIY0D+m+UIAxPG9045GhHrcIN 27jlC25R/AkUgwEzK5uO9hJFOmvV5HG+iLmLJH15XlTbUMHR6L5QCKtvfo12k6HBZL EPX8oHl4XNMoCa17uitl3RcYNSrFsji7kCA9DHffLt242F6qPJyZXfhJAaKPiYnBv9 KdnIgPHACW0AYoe8T4KzAZbB2nlvq7mbFWyiS3wupPsIvWQWuVxb/y1RHvL+VXUBZa mw25WxE7bu/0M53moE9sQ4kamVQkJHacAg0Dl9pQRqDYi/Yfe/uvTJFdI0Q/GbG8Tc qBgFP5HrmSowQ== From: Dmitry Osipenko To: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , =?UTF-8?q?Christian=20K=C3=B6nig?= , Qiang Yu , Steven Price , Boris Brezillon , Emma Anholt , Melissa Wen Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org Subject: [PATCH v17 06/18] drm/shmem-helper: Add and use pages_pin_count Date: Fri, 15 Sep 2023 02:27:09 +0300 Message-ID: <20230914232721.408581-7-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230914232721.408581-1-dmitry.osipenko@collabora.com> References: <20230914232721.408581-1-dmitry.osipenko@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add separate pages_pin_count for tracking of whether drm-shmem pages are moveable or not. With the addition of memory shrinker support to drm-shmem, the pages_use_count will no longer determine whether pages are hard-pinned in memory, but whether pages exist and are soft-pinned (and could be swapped out). The pages_pin_count > 1 will hard-pin pages in memory. Reviewed-by: Boris Brezillon Suggested-by: Boris Brezillon Signed-off-by: Dmitry Osipenko --- drivers/gpu/drm/drm_gem_shmem_helper.c | 24 ++++++++++++++++-------- include/drm/drm_gem_shmem_helper.h | 10 ++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_g= em_shmem_helper.c index 2cc0601865f6..286f0ca51309 100644 --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -234,18 +234,16 @@ static int drm_gem_shmem_pin_locked(struct drm_gem_sh= mem_object *shmem) =20 dma_resv_assert_held(shmem->base.resv); =20 + if (refcount_inc_not_zero(&shmem->pages_pin_count)) + return 0; + ret =3D drm_gem_shmem_get_pages_locked(shmem); + if (!ret) + refcount_set(&shmem->pages_pin_count, 1); =20 return ret; } =20 -static void drm_gem_shmem_unpin_locked(struct drm_gem_shmem_object *shmem) -{ - dma_resv_assert_held(shmem->base.resv); - - drm_gem_shmem_put_pages_locked(shmem); -} - /** * drm_gem_shmem_pin - Pin backing pages for a shmem GEM object * @shmem: shmem GEM object @@ -263,6 +261,9 @@ int drm_gem_shmem_pin(struct drm_gem_shmem_object *shme= m) =20 drm_WARN_ON(obj->dev, obj->import_attach); =20 + if (refcount_inc_not_zero(&shmem->pages_pin_count)) + return 0; + ret =3D dma_resv_lock_interruptible(shmem->base.resv, NULL); if (ret) return ret; @@ -286,8 +287,14 @@ void drm_gem_shmem_unpin(struct drm_gem_shmem_object *= shmem) =20 drm_WARN_ON(obj->dev, obj->import_attach); =20 + if (refcount_dec_not_one(&shmem->pages_pin_count)) + return; + dma_resv_lock(shmem->base.resv, NULL); - drm_gem_shmem_unpin_locked(shmem); + + if (refcount_dec_and_test(&shmem->pages_pin_count)) + drm_gem_shmem_put_pages_locked(shmem); + dma_resv_unlock(shmem->base.resv); } EXPORT_SYMBOL_GPL(drm_gem_shmem_unpin); @@ -632,6 +639,7 @@ void drm_gem_shmem_print_info(const struct drm_gem_shme= m_object *shmem, if (shmem->base.import_attach) return; =20 + drm_printf_indent(p, indent, "pages_pin_count=3D%u\n", refcount_read(&shm= em->pages_pin_count)); drm_printf_indent(p, indent, "pages_use_count=3D%u\n", shmem->pages_use_c= ount); drm_printf_indent(p, indent, "vmap_use_count=3D%u\n", shmem->vmap_use_cou= nt); drm_printf_indent(p, indent, "vaddr=3D%p\n", shmem->vaddr); diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem= _helper.h index 6ee4a4046980..268b3127d150 100644 --- a/include/drm/drm_gem_shmem_helper.h +++ b/include/drm/drm_gem_shmem_helper.h @@ -39,6 +39,16 @@ struct drm_gem_shmem_object { */ unsigned int pages_use_count; =20 + /** + * @pages_pin_count: + * + * Reference count on the pinned pages table. + * The pages allowed to be evicted and purged by memory + * shrinker only when the count is zero, otherwise pages + * are hard-pinned in memory. + */ + refcount_t pages_pin_count; + /** * @madv: State for madvise * --=20 2.41.0