From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6135EC433DF for ; Thu, 18 Jun 2020 16:08:36 +0000 (UTC) Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1430A20776 for ; Thu, 18 Jun 2020 16:08:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1430A20776 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=iommu-bounces@lists.linux-foundation.org Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id F0D6088A01; Thu, 18 Jun 2020 16:08:34 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id IzRL8nRctC2D; Thu, 18 Jun 2020 16:08:33 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by whitealder.osuosl.org (Postfix) with ESMTP id 800E288930; Thu, 18 Jun 2020 16:08:33 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 64D35C07FF; Thu, 18 Jun 2020 16:08:33 +0000 (UTC) Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists.linuxfoundation.org (Postfix) with ESMTP id E7299C016E for ; Thu, 18 Jun 2020 16:08:31 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 8C713221F8 for ; Thu, 18 Jun 2020 16:08:31 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id e83fxbYOIoOT for ; Thu, 18 Jun 2020 16:08:30 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by silver.osuosl.org (Postfix) with ESMTP id F281A214EB for ; Thu, 18 Jun 2020 16:08:29 +0000 (UTC) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 68354101E; Thu, 18 Jun 2020 09:08:29 -0700 (PDT) Received: from [10.57.9.128] (unknown [10.57.9.128]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6E6CF3F6CF; Thu, 18 Jun 2020 09:08:22 -0700 (PDT) Subject: Re: [PATCH v6 01/36] drm: prime: add common helper to check scatterlist contiguity To: Marek Szyprowski , dri-devel@lists.freedesktop.org, iommu@lists.linux-foundation.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org References: <20200618153956.29558-1-m.szyprowski@samsung.com> <20200618153956.29558-2-m.szyprowski@samsung.com> From: Robin Murphy Message-ID: <968a920c-83b7-a0e5-8d0d-fa8486816b01@arm.com> Date: Thu, 18 Jun 2020 17:07:46 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: <20200618153956.29558-2-m.szyprowski@samsung.com> Content-Language: en-GB Cc: Thomas Zimmermann , Bartlomiej Zolnierkiewicz , David Airlie , Maarten Lankhorst , Maxime Ripard , Daniel Vetter , Christoph Hellwig , linux-arm-kernel@lists.infradead.org X-BeenThere: iommu@lists.linux-foundation.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Development issues for Linux IOMMU support List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: iommu-bounces@lists.linux-foundation.org Sender: "iommu" On 2020-06-18 16:39, Marek Szyprowski wrote: > It is a common operation done by DRM drivers to check the contiguity > of the DMA-mapped buffer described by a scatterlist in the > sg_table object. Let's add a common helper for this operation. > > Signed-off-by: Marek Szyprowski > --- > drivers/gpu/drm/drm_gem_cma_helper.c | 23 +++-------------------- > drivers/gpu/drm/drm_prime.c | 26 ++++++++++++++++++++++++++ > include/drm/drm_prime.h | 2 ++ > 3 files changed, 31 insertions(+), 20 deletions(-) > > diff --git a/drivers/gpu/drm/drm_gem_cma_helper.c b/drivers/gpu/drm/drm_gem_cma_helper.c > index 06a5b9ee1fe0..41566a15dabd 100644 > --- a/drivers/gpu/drm/drm_gem_cma_helper.c > +++ b/drivers/gpu/drm/drm_gem_cma_helper.c > @@ -471,26 +471,9 @@ drm_gem_cma_prime_import_sg_table(struct drm_device *dev, > { > struct drm_gem_cma_object *cma_obj; > > - if (sgt->nents != 1) { > - /* check if the entries in the sg_table are contiguous */ > - dma_addr_t next_addr = sg_dma_address(sgt->sgl); > - struct scatterlist *s; > - unsigned int i; > - > - for_each_sg(sgt->sgl, s, sgt->nents, i) { > - /* > - * sg_dma_address(s) is only valid for entries > - * that have sg_dma_len(s) != 0 > - */ > - if (!sg_dma_len(s)) > - continue; > - > - if (sg_dma_address(s) != next_addr) > - return ERR_PTR(-EINVAL); > - > - next_addr = sg_dma_address(s) + sg_dma_len(s); > - } > - } > + /* check if the entries in the sg_table are contiguous */ > + if (drm_prime_get_contiguous_size(sgt) < attach->dmabuf->size) > + return ERR_PTR(-EINVAL); > > /* Create a CMA GEM buffer. */ > cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size); > diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c > index bbfc713bfdc3..0784969894c1 100644 > --- a/drivers/gpu/drm/drm_prime.c > +++ b/drivers/gpu/drm/drm_prime.c > @@ -825,6 +825,32 @@ struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_page > } > EXPORT_SYMBOL(drm_prime_pages_to_sg); > > +/** > + * drm_prime_get_contiguous_size - returns the contiguous size of the buffer > + * @sgt: sg_table describing the buffer to check > + * > + * This helper calculates the contiguous size in the DMA address space > + * of the the buffer described by the provided sg_table. > + * > + * This is useful for implementing > + * &drm_gem_object_funcs.gem_prime_import_sg_table. > + */ > +unsigned long drm_prime_get_contiguous_size(struct sg_table *sgt) > +{ > + dma_addr_t expected = sg_dma_address(sgt->sgl); > + struct sg_dma_page_iter dma_iter; > + unsigned long size = 0; > + > + for_each_sgtable_dma_page(sgt, &dma_iter, 0) { > + if (sg_page_iter_dma_address(&dma_iter) != expected) > + break; > + expected += PAGE_SIZE; > + size += PAGE_SIZE; Hmm, in many cases this is likely to be far less efficient than simply using for_each_sgtable_dma() and sg_dma_len() equivalent to the original implementation, and there doesn't seem to be any good reason for that. Plus AFAICS it could potentially let false-positives through if someone were to pass in a table with non-page-aligned lengths (I assume that's expected never to happen, but still...) Robin. > + } > + return size; > +} > +EXPORT_SYMBOL(drm_prime_get_contiguous_size); > + > /** > * drm_gem_prime_export - helper library implementation of the export callback > * @obj: GEM object to export > diff --git a/include/drm/drm_prime.h b/include/drm/drm_prime.h > index 9af7422b44cf..47ef11614627 100644 > --- a/include/drm/drm_prime.h > +++ b/include/drm/drm_prime.h > @@ -92,6 +92,8 @@ struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_page > struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj, > int flags); > > +unsigned long drm_prime_get_contiguous_size(struct sg_table *sgt); > + > /* helper functions for importing */ > struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev, > struct dma_buf *dma_buf, > _______________________________________________ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu