From patchwork Sun Mar 5 22:10:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Osipenko X-Patchwork-Id: 64363 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1551138wrd; Sun, 5 Mar 2023 14:13:14 -0800 (PST) X-Google-Smtp-Source: AK7set+b4ofR61tDqZ0KQQsgC/gshpuaB7MT6n76ERwvRYCECnmvPdxL28RiBjmdhPjBG561J1nO X-Received: by 2002:aa7:c44e:0:b0:4b4:48e1:1eb4 with SMTP id n14-20020aa7c44e000000b004b448e11eb4mr7275642edr.38.1678054394655; Sun, 05 Mar 2023 14:13:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678054394; cv=none; d=google.com; s=arc-20160816; b=0yPSUzCnE8IKi504UGsNapEtnm0edZoZmHFupCymfrP6R3BYeRcC3E+79tUTY9xfDX Gm7YLNiA0sp3s+uubQw9y2w76X0s3OHatuLmzc8Q3fIVS/YFPQbIf9MYhWcuGz6Y00We d0HHtL+AWhfIpzPk1wCkLJ5/BH5bBprZA8XgGNnmkJvMgbLEJHOHzMHlG1edGx4ExfnO fYvFeCRgRmVA9PHUFGkYaxNDRkqVCgHpcA4OkL+Xv1mYPE1DgLLvGct2NjVixtT4dUv4 5juzKHt+ERpABBx3+tJaRzYXTob+BGKB0g6EC1GCKIfsyI44SXt7sG3RgNF50pUH/CKi FPdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gv7U80nlSjeaLZJ2p3Ep6ZtPoP813bx126mdprPACTc=; b=hRB6tcMpQP/1d1hIHut5h/9ORYvtrcdJfoUq2OR+sXS7uKOZsZy2Kuf9e42j5m/Fgm aNO06Eo+qX3hoLhFyPwYqTZjx36h5pUsQxZQdgXYE6vDvSpGWlzSfrd21/obD9dn+puX d7zUg3gSzULCDKnDUHMOcm24HZANWMEWfr1JVRAwqUGXJGLorsWF/v4jDF4Pk+qmy/QD w30lccdKKjyID2SJp+r9ArL/vEe1MQZHQOD0TpIDOcbcqgTZLT0HcysaKZ3djOFcwUQ+ lI6R7oDhfsNT2sfcU+GcvMTxFiBlZbdh8d59YPB74dsKvFZqf9fegAwMWy+dQAdVPBze WMuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=A0n+DdLp; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x17-20020a1709064a9100b008df5a5088ccsi1366168eju.94.2023.03.05.14.12.50; Sun, 05 Mar 2023 14:13:14 -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=@collabora.com header.s=mail header.b=A0n+DdLp; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229753AbjCEWLf (ORCPT + 99 others); Sun, 5 Mar 2023 17:11:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229699AbjCEWLd (ORCPT ); Sun, 5 Mar 2023 17:11:33 -0500 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8B6E13D4A for ; Sun, 5 Mar 2023 14:11:31 -0800 (PST) Received: from workpc.. (unknown [109.252.117.89]) (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 2C81666020F5; Sun, 5 Mar 2023 22:11:29 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1678054290; bh=CQuK/0mx8fcX8m0D0MEcKjqr9lA4ND+Aj+s1RvwAUnI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A0n+DdLpmEy1quKkcKSC6n1ldd/SmnjFrl1QWhN/JI68GF49XUzaSzm8L8yesda1b yg/DivhGtHRefuPBpUX/C+wLrCNrq0T3LpihR9yR0eALNVTgvxnNzplnEzUZ9KAqQu VDkuwdwF9CgFaP2BtHXkwBZIkYjlwMUa7pRFqoULi4kDI1tLXKurHKydsMGgr1pqYd cy+50oT+7mkj1b9hb0CHCerpT0tSpSqfkTaGgTervDAi+cBnuXuHBT7qyCoHxeXZfU pdqc2cNlY0+Th55OlUv/1B7r0WrebKCbersyZxU8Ma7vltUFY38jlJOs9I66RXBLlX laRWud9NzZzjg== From: Dmitry Osipenko To: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Daniel Almeida , Gustavo Padovan , Daniel Stone , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Qiang Yu , Steven Price , Alyssa Rosenzweig , Rob Herring Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org, intel-gfx@lists.freedesktop.org Subject: [PATCH v12 02/11] drm/shmem-helper: Factor out pages alloc/release from drm_gem_shmem_get/put_pages() Date: Mon, 6 Mar 2023 01:10:02 +0300 Message-Id: <20230305221011.1404672-3-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230305221011.1404672-1-dmitry.osipenko@collabora.com> References: <20230305221011.1404672-1-dmitry.osipenko@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759567564761566934?= X-GMAIL-MSGID: =?utf-8?q?1759567564761566934?= Factor out pages allocation from drm_gem_shmem_get_pages() into drm_gem_shmem_acquire_pages() function and similar for the put_pages() in a preparation for addition of shrinker support to drm-shmem. Once shrinker will be added, the pages_use_count>0 will no longer determine whether pages are pinned because pages could be swapped out by the shrinker and then pages_use_count will be greater than 0 in this case. We will add new pages_pin_count in a later patch. The new common drm_gem_shmem_acquire/release_pages() will be used by shrinker code for performing the page swapping. Signed-off-by: Dmitry Osipenko --- drivers/gpu/drm/drm_gem_shmem_helper.c | 67 +++++++++++++++++++++----- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c index 651ca7f380a2..a62c41336a7f 100644 --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -165,19 +165,26 @@ void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem) } EXPORT_SYMBOL_GPL(drm_gem_shmem_free); -static int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem) +static int +drm_gem_shmem_acquire_pages(struct drm_gem_shmem_object *shmem) { struct drm_gem_object *obj = &shmem->base; struct page **pages; - if (shmem->pages_use_count++ > 0) - return 0; + dma_resv_assert_held(shmem->base.resv); + + if (shmem->madv < 0) { + drm_WARN_ON(obj->dev, shmem->pages); + return -ENOMEM; + } + + if (drm_WARN_ON(obj->dev, !shmem->pages_use_count)) + return -EINVAL; pages = drm_gem_get_pages(obj); if (IS_ERR(pages)) { drm_dbg_kms(obj->dev, "Failed to get pages (%ld)\n", PTR_ERR(pages)); - shmem->pages_use_count = 0; return PTR_ERR(pages); } @@ -196,6 +203,48 @@ static int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem) return 0; } +static int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem) +{ + int err; + + dma_resv_assert_held(shmem->base.resv); + + if (shmem->madv < 0) + return -ENOMEM; + + if (shmem->pages_use_count++ > 0) + return 0; + + err = drm_gem_shmem_acquire_pages(shmem); + if (err) + goto err_zero_use; + + return 0; + +err_zero_use: + shmem->pages_use_count = 0; + + return err; +} + +static void +drm_gem_shmem_release_pages(struct drm_gem_shmem_object *shmem) +{ + struct drm_gem_object *obj = &shmem->base; + + dma_resv_assert_held(shmem->base.resv); + +#ifdef CONFIG_X86 + if (shmem->map_wc) + set_pages_array_wb(shmem->pages, obj->size >> PAGE_SHIFT); +#endif + + drm_gem_put_pages(obj, shmem->pages, + shmem->pages_mark_dirty_on_put, + shmem->pages_mark_accessed_on_put); + shmem->pages = NULL; +} + /* * drm_gem_shmem_put_pages - Decrease use count on the backing pages for a shmem GEM object * @shmem: shmem GEM object @@ -214,15 +263,7 @@ void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem) if (--shmem->pages_use_count > 0) return; -#ifdef CONFIG_X86 - if (shmem->map_wc) - set_pages_array_wb(shmem->pages, obj->size >> PAGE_SHIFT); -#endif - - drm_gem_put_pages(obj, shmem->pages, - shmem->pages_mark_dirty_on_put, - shmem->pages_mark_accessed_on_put); - shmem->pages = NULL; + drm_gem_shmem_release_pages(shmem); } EXPORT_SYMBOL(drm_gem_shmem_put_pages);