From patchwork Tue Mar 14 02:26:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Osipenko X-Patchwork-Id: 69259 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1524745wrd; Mon, 13 Mar 2023 19:36:42 -0700 (PDT) X-Google-Smtp-Source: AK7set9oXbR1bVrfXCoWdpJzbPhFBsdV9ZBUc+NKIjM36uZDZ5bRh1TXSezijErnCN4p9zkomYXn X-Received: by 2002:a05:6a20:669e:b0:d4:72ab:df31 with SMTP id o30-20020a056a20669e00b000d472abdf31mr4725482pzh.11.1678761402052; Mon, 13 Mar 2023 19:36:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678761402; cv=none; d=google.com; s=arc-20160816; b=u2S4MqaHCHVK/bn0HRgx1hoedxmfdGcte9bq3/3IDo/Ou6unjP4xVEj/7uzR19ivbP mB04jlCZATLbcwPvmNTe9qpTqHVeI9cfeRG4ajmnxzTzJUj3xn7GJxDgw7ftcP3gOv69 BUtqQEg+7uX6DE54cNf4wy7HQ3t8+dVwkLep1z3R5TLsELTsB2ih2EM8rxxAfEySgnEJ z4H1UItlKEEOU1VeFDU7HIkDW39PlWYKHF3uS0/vXdt2YkrikPAyPLgnUaLmI7UTpoNA zMuwfeLm+0vwqUnkkvo2q4km/IvRN/45i//d1aYzkNLEPnQeLRXAea4sxhraRmURs4tQ t54A== 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=Pmuu64wpk3D/ceT0SbKXbI0w8/xz74cnWKOdbh05Rd4=; b=LcZm3z+yaKZm50q7a+2hWGMtq37qZ4NCBBKl9knSbWuykXyql1kGMk196e6kEDppcT owP2MjaAURMfu6MYrCxzRLslgX11bIQMFbYcX+JT+MViHjI2rLBlWfn/nMwP6gE4tprN Fvf127nWsjqEqDG7unUYNyPbnt0b6QEluQY7HLOGKbByDSd80njMWylCHv1xNBRlHbEG svKEIelsecrlJNqbp79Y4DBygWBIrXVg6icvsYemG8mp2qKrw9YAhTJqWYxZ0vWyLToH hROHkIlUdrDayJpc1ihdkNAz4wBByRqnFHnj7GUgr/D3aEqXsZ/xf5mpGSJrei0Le6Zq SSHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=RMfz9vO3; 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 j28-20020a63551c000000b0050bc14fc7absi1045334pgb.106.2023.03.13.19.36.29; Mon, 13 Mar 2023 19:36:42 -0700 (PDT) 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=RMfz9vO3; 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 S230077AbjCNC2S (ORCPT + 99 others); Mon, 13 Mar 2023 22:28:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229925AbjCNC2N (ORCPT ); Mon, 13 Mar 2023 22:28:13 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B3CD359D for ; Mon, 13 Mar 2023 19:28:11 -0700 (PDT) Received: from workpc.. (109-252-120-116.nat.spd-mgts.ru [109.252.120.116]) (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 1513B6602173; Tue, 14 Mar 2023 02:28:07 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1678760890; bh=vUKXk81qka0zQY/A/PT9otvyoQLzz6m+Uz83dLMP8L0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RMfz9vO3gE6S3hbW7psQPq/LLg4CPAVLTPe6KGgNJgHk9cSWmaPhyrF6z2og6SZLh 9nZ5PqMRxNZQsp57/9uvE/XzFOVAeNFjpPuUF+74WpgZpHMuqHWaLgorTeSdtcnE16 T4PFdmLgSinaj4anSbi2RVcCFp2gL1sKT/gp7NXrcaPRQ2kX4SreyhuzYsZH5J5yUs EwuN74fUwck7e8Y3aK/KJXPTTJCTCapDCtVfiHbkqr3ay3KwY5FnXJxsymTHah0E25 VheNWgRojMueqnPdqQx9ICtB4r2KfxycBL89a0kEZ/MddMCFLz4h/T4zjzMT8sClVG MScr6ffX7APug== 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 v13 02/10] drm/shmem-helper: Factor out pages alloc/release from drm_gem_shmem_get/put_pages() Date: Tue, 14 Mar 2023 05:26:51 +0300 Message-Id: <20230314022659.1816246-3-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230314022659.1816246-1-dmitry.osipenko@collabora.com> References: <20230314022659.1816246-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, URIBL_BLOCKED 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?1760308916066497549?= X-GMAIL-MSGID: =?utf-8?q?1760308916066497549?= 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 8fc2a3277486..4da9c9c39b9a 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);