From patchwork Sun Feb 5 12:51:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 52901 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1764123wrn; Sun, 5 Feb 2023 05:01:39 -0800 (PST) X-Google-Smtp-Source: AK7set+SFxbAUSchEaGTCyIMNWMSqIMp8xfzLRCjcWdNZIGItFy6LnjPnsZYQ1cQjNEmJXfKXoFz X-Received: by 2002:a50:9341:0:b0:4aa:a76a:c428 with SMTP id n1-20020a509341000000b004aaa76ac428mr2348494eda.6.1675602099181; Sun, 05 Feb 2023 05:01:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675602099; cv=none; d=google.com; s=arc-20160816; b=sN0PFWTyBJkDzR0EVKCQs17ksjQ0qckpXycA+2w5mjayRsQBtmihBQmRDZ6tPDrgxf xKEdYTHtd6JGNWya+nCzBI9i5r61Q2ULND/B6kIvNpBjv8xdxt+kOfHMWQHN9G7CcwXx iyrVCRHg1HQMTfZh+FdySy2whbPp2ZIKco/Xjw5pqhzzunBa+giYvR+8uh6m3OlMefio jgX8hP++fhecBKQwV+aFRb8yWq0rzIevRIAoeThk0V6DBiieNra6A3GCjM6WAYrVVuzr 3qb8PqGcZagykJZvj/3GHIqCBzfBzV8y/mAtHBN4/9nIYR9gkp0okNxCC1neV6ycpqdh FReg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=l/Spi1k880iaAss7oJ70ellzbl0eFFC3K9Tt6MGhvj4=; b=v5dIpDKyqgS15OXaXl3lxiTFtH3FGKoFEpq1FNnWrTkHIoi42ZjqIbNHWGNnfpIcvy OlR5lj1d2XhU8EGDi4FVNqiUL4UU+dNCqAML9qniKI5DPjcmJgIwToObQrMWM8CzrJig 2ScR+ZGVJVSCx3h7mMBICeDZfppShH3sAUjIYlgXv06gKaMB6kvAlwXSHLi5/GOLtgEb JVrmdoGZ6xYgPK3dbjyVtyMiiirxYK3JRq7TBn3XtAy3r54ujY7Bzc7BirKvxnhfYvVJ y6m/nKYZaDP3+bSBarA9rS5B3GKly2OZsgVvYUcMioimx+Z2YxK3r8qycldzof/VlWTS WbIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=Xt+nlv7Y; 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=asahilina.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r7-20020aa7cfc7000000b0049dfd2a39a8si8029822edy.153.2023.02.05.05.01.14; Sun, 05 Feb 2023 05:01:39 -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=@asahilina.net header.s=default header.b=Xt+nlv7Y; 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=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229570AbjBEMwK (ORCPT + 99 others); Sun, 5 Feb 2023 07:52:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229517AbjBEMwI (ORCPT ); Sun, 5 Feb 2023 07:52:08 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B54E65A9 for ; Sun, 5 Feb 2023 04:52:07 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (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: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id 09FF9423B9; Sun, 5 Feb 2023 12:52:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1675601525; bh=q0LNgqsfGLVJB6RizLXffwvd+oFT2Fg0IE4QerrJfd0=; h=From:To:Cc:Subject:Date; b=Xt+nlv7YZI4F8qZkOLOserjbvP/Ase1HK8RipD68mIthYaXCMAxz7k/GADvHZb97d B9WafGbp6VQaZJKHeX2iyuWMoli/QAWFzvq2TR5lmUPf/JSD8sLJTF5PFb24VbFcBl tKcqJwdpiOYgsBIErlgFnF3mTswqmzQ4ZxYdN9jN0vkW8bHRq7PU1/H5e82mFpyrrs NM1qKf207KL9Pml9f44U+wnlulDshDVQyjBpJe6k0Jom7hKA0TLIYc7UQBgS//eF0P 32iFI9OqXft7piz1QRkz4JB6kfcpcpHizh1Qdy9RGnZ46V7J3hf+eKZG8XfLWCbjNG XmXeTjql3ZVbA== From: Asahi Lina To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter Cc: =?utf-8?q?Noralf_Tr=C3=B8nnes?= , Rob Herring , Alyssa Rosenzweig , dri-devel@lists.freedesktop.org, asahi@lists.linux.dev, linux-kernel@vger.kernel.org, Asahi Lina Subject: [PATCH] drm/shmem-helper: Fix locking for drm_gem_shmem_get_pages_sgt() Date: Sun, 5 Feb 2023 21:51:24 +0900 Message-Id: <20230205125124.2260-1-lina@asahilina.net> X-Mailer: git-send-email 2.35.1 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?1756996146544976738?= X-GMAIL-MSGID: =?utf-8?q?1756996146544976738?= Other functions touching shmem->sgt take the pages lock, so do that here too. drm_gem_shmem_get_pages() & co take the same lock, so move to the _locked() variants to avoid recursive locking. Discovered while auditing locking to write the Rust abstractions. Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects") Fixes: 4fa3d66f132b ("drm/shmem: Do dma_unmap_sg before purging pages") Signed-off-by: Asahi Lina Reviewed-by: Javier Martinez Canillas --- drivers/gpu/drm/drm_gem_shmem_helper.c | 54 ++++++++++++++++---------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c index b602cd72a120..2c559b310cad 100644 --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -681,23 +681,7 @@ struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem) } EXPORT_SYMBOL_GPL(drm_gem_shmem_get_sg_table); -/** - * drm_gem_shmem_get_pages_sgt - Pin pages, dma map them, and return a - * scatter/gather table for a shmem GEM object. - * @shmem: shmem GEM object - * - * This function returns a scatter/gather table suitable for driver usage. If - * the sg table doesn't exist, the pages are pinned, dma-mapped, and a sg - * table created. - * - * This is the main function for drivers to get at backing storage, and it hides - * and difference between dma-buf imported and natively allocated objects. - * drm_gem_shmem_get_sg_table() should not be directly called by drivers. - * - * Returns: - * A pointer to the scatter/gather table of pinned pages or errno on failure. - */ -struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem) +static struct sg_table *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_object *shmem) { struct drm_gem_object *obj = &shmem->base; int ret; @@ -708,7 +692,7 @@ struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem) WARN_ON(obj->import_attach); - ret = drm_gem_shmem_get_pages(shmem); + ret = drm_gem_shmem_get_pages_locked(shmem); if (ret) return ERR_PTR(ret); @@ -730,10 +714,40 @@ struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem) sg_free_table(sgt); kfree(sgt); err_put_pages: - drm_gem_shmem_put_pages(shmem); + drm_gem_shmem_put_pages_locked(shmem); return ERR_PTR(ret); } -EXPORT_SYMBOL_GPL(drm_gem_shmem_get_pages_sgt); + +/** + * drm_gem_shmem_get_pages_sgt - Pin pages, dma map them, and return a + * scatter/gather table for a shmem GEM object. + * @shmem: shmem GEM object + * + * This function returns a scatter/gather table suitable for driver usage. If + * the sg table doesn't exist, the pages are pinned, dma-mapped, and a sg + * table created. + * + * This is the main function for drivers to get at backing storage, and it hides + * and difference between dma-buf imported and natively allocated objects. + * drm_gem_shmem_get_sg_table() should not be directly called by drivers. + * + * Returns: + * A pointer to the scatter/gather table of pinned pages or errno on failure. + */ +struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem) +{ + int ret; + struct sg_table *sgt; + + ret = mutex_lock_interruptible(&shmem->pages_lock); + if (ret) + return ERR_PTR(ret); + sgt = drm_gem_shmem_get_pages_sgt_locked(shmem); + mutex_unlock(&shmem->pages_lock); + + return sgt; +} +EXPORT_SYMBOL(drm_gem_shmem_get_pages_sgt); /** * drm_gem_shmem_prime_import_sg_table - Produce a shmem GEM object from