Received: by 2002:a05:7412:98c1:b0:fa:551:50a7 with SMTP id kc1csp318893rdb; Fri, 5 Jan 2024 10:48:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPs91D0Qe9emKuJGBvHKyfPkml9Zyls18nAane7ufPqnsxmk+uWaHD2yau/dcEop+LI+VV X-Received: by 2002:a05:6402:1392:b0:552:2b83:42e3 with SMTP id b18-20020a056402139200b005522b8342e3mr1553411edv.41.1704480526235; Fri, 05 Jan 2024 10:48:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704480526; cv=none; d=google.com; s=arc-20160816; b=ciSu7KopL+Z818ouzeuhlZW/6DuzHDoZX3QgEodoHnzTgKzJyavW5Vh31InLdJbvz0 1Ixaj4v9+jh7fh2C2IJmvmKWAU3lL7Gbc0Y7weBYqFxzbTnq3hYdUqve9ATjRvHCZV9x z8Wr98ryujIfaaT5OjZDbw5p7VHrorcBF0vYo0c0u8fgFfw7ddcTcffy4LppSAAgylvj Hha1gDhd2YOvqfu4DPyahzjvyOZh/N5pF0Fc1FXRcXX4vVx5lEMSyjmmE6yO1dxgUKCI nIk8/mNvr/3mAoJCWiUZp5S0NhwIkVMtqTmSdYsUUNHZe3LI1skTPqTd8dEZmpWM/VVa BYDA== ARC-Message-Signature: i=1; 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=rshXXrNkTdp+TOKM1cZl8KNztU+aLVks6xaydr0XYpQ=; fh=5cel2jD5h+yPMXVwxbomyVhwojHUqATy6nFjd4aOh4o=; b=JE0Qyt23V2iwlhaol/5CdQ0cW+IViKAWRGX335vb7ThkXTXZXczgdqRqZiXFY/AEEK 8Bw7z98ANYnMrTJ/4QoFA1AuShJivkOtbBxgAm81b7lTfmwFT/kr5V1dPt9cTh+ghtiz qsjtYtzYqCzUhG+GjInJWQAZOB8/G/TzZZ+lXnheQag+wc5Ttgi3FNileY24DrqyyTuI lVAP6/JXg2/156hdcS5imaPzZ6LZ1tETmeeVZzdDJw9PLmGqkMa0ThYPbTO1ufQCdaQv ovF8lREL+jgN7A8mkewcwL9CElF771w3DNfePSaebkSSfDmusrwz/n5vnUwjpJlp2efe kC7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=cUKmatPY; spf=pass (google.com: domain of linux-kernel+bounces-18245-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18245-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id di11-20020a056402318b00b0054cfed181d5si877736edb.595.2024.01.05.10.48.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:48:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18245-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=cUKmatPY; spf=pass (google.com: domain of linux-kernel+bounces-18245-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18245-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 060311F223B3 for ; Fri, 5 Jan 2024 18:48:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6AD61374C2; Fri, 5 Jan 2024 18:46:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="cUKmatPY" X-Original-To: linux-kernel@vger.kernel.org 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 3D3E836B10 for ; Fri, 5 Jan 2024 18:46:57 +0000 (UTC) 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=1704480415; bh=4r4O0Txfi6sopJfIVmmSRlOl7CynM2fVl56/NWlOz5k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cUKmatPYuxSLewnyK0wnLHgKnMj/IflthxaD4ptKKDHilcL8uZyPR/xzePklx18st GR87LT57II64QsIW767D76QP8T8WVqmuijhRMXOC3Usx7kZQiax2/ee2baw8IeoAHY TU+T2K/xp2F/d8kT2AIFeTYOpV0QKx6aXV9iLo9qVB005R3PV+eA7vwt+xRtrg0h83 SIopLsPwN7iYLLyEltax6KzukYL6/Ct5mFSWqTIYpupFGhLk04re3USQNiI1UCg10J HGR2iX3OvNj5Y6mWz74uTWF9hMykFXqSPuDF8OMZxVT0GZKnTO7nPKPLklU8qAygTm He7O0zVAbn0nQ== Received: from workpc.. (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: dmitry.osipenko) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 76A473782043; Fri, 5 Jan 2024 18:46:54 +0000 (UTC) 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 v19 07/30] drm/shmem-helper: Add and use pages_pin_count Date: Fri, 5 Jan 2024 21:46:01 +0300 Message-ID: <20240105184624.508603-8-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240105184624.508603-1-dmitry.osipenko@collabora.com> References: <20240105184624.508603-1-dmitry.osipenko@collabora.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Acked-by: Maxime Ripard Reviewed-by: Boris Brezillon Suggested-by: Boris Brezillon Signed-off-by: Dmitry Osipenko --- drivers/gpu/drm/drm_gem_shmem_helper.c | 25 +++++++++++++++++-------- include/drm/drm_gem_shmem_helper.h | 11 +++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c index 1f0a66386415..55b9dd3d4b18 100644 --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -156,6 +156,7 @@ void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem) drm_gem_shmem_put_pages_locked(shmem); drm_WARN_ON(obj->dev, shmem->pages_use_count); + drm_WARN_ON(obj->dev, refcount_read(&shmem->pages_pin_count)); dma_resv_unlock(shmem->base.resv); } @@ -234,18 +235,16 @@ static int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem) dma_resv_assert_held(shmem->base.resv); + if (refcount_inc_not_zero(&shmem->pages_pin_count)) + return 0; + ret = drm_gem_shmem_get_pages_locked(shmem); + if (!ret) + refcount_set(&shmem->pages_pin_count, 1); return ret; } -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 +262,9 @@ int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem) drm_WARN_ON(obj->dev, obj->import_attach); + if (refcount_inc_not_zero(&shmem->pages_pin_count)) + return 0; + ret = dma_resv_lock_interruptible(shmem->base.resv, NULL); if (ret) return ret; @@ -286,8 +288,14 @@ void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem) drm_WARN_ON(obj->dev, obj->import_attach); + 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 +640,7 @@ void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem, if (shmem->base.import_attach) return; + drm_printf_indent(p, indent, "pages_pin_count=%u\n", refcount_read(&shmem->pages_pin_count)); drm_printf_indent(p, indent, "pages_use_count=%u\n", shmem->pages_use_count); drm_printf_indent(p, indent, "vmap_use_count=%u\n", shmem->vmap_use_count); drm_printf_indent(p, indent, "vaddr=%p\n", shmem->vaddr); diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem_helper.h index 9e83212becbb..c708a9f45cbd 100644 --- a/include/drm/drm_gem_shmem_helper.h +++ b/include/drm/drm_gem_shmem_helper.h @@ -39,6 +39,17 @@ struct drm_gem_shmem_object { */ unsigned int pages_use_count; + /** + * @pages_pin_count: + * + * Reference count on the pinned pages table. + * + * Pages are hard-pinned and reside in memory if count + * greater than zero. Otherwise, when count is zero, the pages are + * allowed to be evicted and purged by memory shrinker. + */ + refcount_t pages_pin_count; + /** * @madv: State for madvise * -- 2.43.0