From patchwork Mon Feb 27 06:02:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Osipenko X-Patchwork-Id: 61736 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2255215wrd; Sun, 26 Feb 2023 22:05:36 -0800 (PST) X-Google-Smtp-Source: AK7set9x9aNIVdAlzEIYsTkr6+qhUcLNuDknJDlnQ+6tlgdKNlWoYS3sGFJu1V5sWWGb2hFu0WN7 X-Received: by 2002:a17:906:af18:b0:88a:8e57:f063 with SMTP id lx24-20020a170906af1800b0088a8e57f063mr36917254ejb.62.1677477936259; Sun, 26 Feb 2023 22:05:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677477936; cv=none; d=google.com; s=arc-20160816; b=yshnBVbVGABqzk5fF5NNyunVWA+aJ2HUZlp1oo7xoj9yyB83gclIQZ83TyNfXJbHoX HinvpeW8c/K7KLy3EfjZbZuLj/bEPV+WXedSSLb6/Li8rjJU/R6Otwi9jewna5qFgDUi S3zIeJxBMt4ibLTYefYrOzRchJdpNGaGRpH56clygPhr5sF9PwKFVEWUqkU6tkeHp9HI nKgoQo1pmTgELF/4JowIKDJhH6rNGt2La9EI6LjaAHAt5eNDZPOFFUo/L4ekhdRCchAl AKb3A2ewD0zslvODn57A2Q7ikmYnLbaH7cYG0cE93cYOijNLgjsareKupOiQ4+2yPQDH qq+w== 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=RUsnSB6gctGHyJhl/AONGet8G3hE907IN0JLBSQOg6w=; b=lp802Nd8EdM4OCva//eS4e0YmBiwHSAnL/2KzT5fMFRXxY48TrZnJTD40CnxAVZ5ws +DSAIdkE+wMdSCfz3ME9qncBA7xEEPDWvH2IXedaXrvT1i4+CA58T3WGNTeAfLrrQgB+ 1cc6AOQtv59WmksZW66+EZ5vVRkxZQtCW+JiDZQKP9+4I6qwTJqje3nUWgjKGak3xYBa i8yFsMOquLJXoRzagWAtNpcyFH5psc9LjcNrXeqGYBfr40AK+Z1xkmhWp0LrRWvyNsEd Lx4pioAg0gCoPTcjWtDA3EnCiwyi80UXWdHu8mTSgQjzi4CEX/onXwG1JY6D9vPxnhQp aDjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=PlUavREP; 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=REJECT sp=REJECT 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 y5-20020a17090614c500b008b981b82f65si5519434ejc.42.2023.02.26.22.05.13; Sun, 26 Feb 2023 22:05:36 -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=PlUavREP; 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=REJECT sp=REJECT dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229829AbjB0GDL (ORCPT + 99 others); Mon, 27 Feb 2023 01:03:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229751AbjB0GDJ (ORCPT ); Mon, 27 Feb 2023 01:03:09 -0500 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 54D0659C8 for ; Sun, 26 Feb 2023 22:03:06 -0800 (PST) Received: from workpc.. (109-252-117-89.nat.spd-mgts.ru [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 9FA95660230B; Mon, 27 Feb 2023 06:03:03 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1677477785; bh=KhM572I8tzRrRh298eDvHlO8Q85Aa16RJA/7aZZtuz4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PlUavREPluaDDwURNs2F6u2wC0CkF6TwY8x2nKBjIDrUNV+iWfxj3eBxsqMTAJZp6 JhN+kpcqjbkLA2MOkLpebu4BwWgKVzU40hRkIxVrWq2yWuZDjEZzsmWSkYi1/+n6kc 7C0dMwW8cDPSx0TbM7uXATyvLMlESIm62s0i9+9lU0Rt0UhWlt92E63ki7i4Yg/QwR HAEeyJpD0f4HRKYRgwPCGPMOWUS/OxwdhPtMD6H5u7LYXQEepWORqQ3432xTb+LwPp imQAvc1MV6vXAK06k+QxnQp3H0jzE8udd2XfE4qyYPDxK8cKqmE0Uqosxq1WuRa2ZC Fi8UGKS8Rtfyg== 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 Subject: [PATCH v11 01/10] drm/shmem-helper: Factor out pages alloc/release from drm_gem_shmem_get/put_pages() Date: Mon, 27 Feb 2023 09:02:10 +0300 Message-Id: <20230227060219.904986-2-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230227060219.904986-1-dmitry.osipenko@collabora.com> References: <20230227060219.904986-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?1758963104375819682?= X-GMAIL-MSGID: =?utf-8?q?1758963104375819682?= 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 ad01c43ff26e..49ab49454783 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);