Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1903249pxk; Tue, 1 Sep 2020 10:25:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqf6QuNoVv1WgjgcbSTpsJEBtuftb04OjqCBriaoHLCLG53IUv2cUaPEi4Y47qxR59rkyN X-Received: by 2002:a17:906:a84f:: with SMTP id dx15mr2439071ejb.377.1598981132492; Tue, 01 Sep 2020 10:25:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598981132; cv=none; d=google.com; s=arc-20160816; b=WqBdNcpNc0E7UX/2z63ptCKk9blxUqN92oatdUyUTDJTwcVVHikzqESrVY1KmW1Bnk XDw3SoSTGQ7KNwXNqeIuCH9xcQK1j91hJS0l+YMv713CakeyUWNsWPRqaGyZ15bx7wXz lHEleVCQKdG57Dsf4rKPGMSWl4YYfxXcV012NGBnGo++PZVsWmj+q9gVE1TUYxJp0PbG BEECImV2ul7mEbpC1E8iFXSb2JA6z6gf54sjEFiH5LJXXxed+2Jxznm2HcFiKeBju42c OClzKVco0WaL5HPNHV7bFQjZKI+/0a0tEVlKsrwa6vbMUqj5asKjGit9B0TN2N69wEPZ hhIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=3Wfn9Ala9UkBIgR6/NtzLiBqlrcVmRy06G8wjI1lFDg=; b=LEauyCTzJxm0/PnTzqOdF5Va/mA56ZFYv/eZmldebH77zu+KU9xxAIDN3i0k3wrf89 1NzIXd6g+eAu6EqrITUT2vyzobrYyS5bEzfWOauDq1DOHd1ffz5bA0ySGonTz1LTlxr+ D8eZuhpysFyhpl5bWZmuf4XWOWiqqgIkqxVhnJczho8SVBSk0KQGn6CCXVaA7TeLGnA9 hfpAanDXhUSDMcdbRn7b15f+nRzBg2RV4qwmmC8umtF3BrP/hdgLcnazbEBIW4NtzXNt ZaOfwl3bBYBdqp7ern4UueAFIYD6TvEycoe6kdNbNG9aBYojzJ66IKJI+6pr3yIiEiAw KHag== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jo8si1011355ejb.524.2020.09.01.10.25.08; Tue, 01 Sep 2020 10:25:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732694AbgIARYa (ORCPT + 99 others); Tue, 1 Sep 2020 13:24:30 -0400 Received: from foss.arm.com ([217.140.110.172]:47282 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728454AbgIARYV (ORCPT ); Tue, 1 Sep 2020 13:24:21 -0400 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 266601FB; Tue, 1 Sep 2020 10:24:21 -0700 (PDT) Received: from [10.57.40.122] (unknown [10.57.40.122]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 27F3B3F71F; Tue, 1 Sep 2020 10:24:18 -0700 (PDT) Subject: Re: [PATCH v9 02/32] drm: prime: use sgtable iterators in drm_prime_sg_to_page_addr_arrays() To: Marek Szyprowski , dri-devel@lists.freedesktop.org, iommu@lists.linux-foundation.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Cc: Christoph Hellwig , Bartlomiej Zolnierkiewicz , linux-arm-kernel@lists.infradead.org, David Airlie , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann References: <20200826063316.23486-1-m.szyprowski@samsung.com> <20200826063316.23486-3-m.szyprowski@samsung.com> From: Robin Murphy Message-ID: <484582f2-b027-8406-b6ff-f50c0d1ba7b0@arm.com> Date: Tue, 1 Sep 2020 18:24:17 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20200826063316.23486-3-m.szyprowski@samsung.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-08-26 07:32, Marek Szyprowski wrote: > Replace the current hand-crafted code for extracting pages and DMA > addresses from the given scatterlist by the much more robust > code based on the generic scatterlist iterators and recently > introduced sg_table-based wrappers. The resulting code is simple and > easy to understand, so the comment describing the old code is no > longer needed. Is removing the WARN_ON()s intentional? It certainly seems like it would be a genuine driver bug if the caller asked for addresses but didn't allocate appropriately-sized arrays. Might be worth noting either way. I'm also assuming this isn't called in performance-critical paths with massive lists such that the two separate iterations might have a noticeable impact. Nits aside, Reviewed-by: Robin Murphy > Signed-off-by: Marek Szyprowski > Reviewed-by: Andrzej Hajda > --- > drivers/gpu/drm/drm_prime.c | 49 ++++++++++++------------------------- > 1 file changed, 15 insertions(+), 34 deletions(-) > > diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c > index 4ed5ed1f078c..5d181bf60a44 100644 > --- a/drivers/gpu/drm/drm_prime.c > +++ b/drivers/gpu/drm/drm_prime.c > @@ -990,45 +990,26 @@ EXPORT_SYMBOL(drm_gem_prime_import); > int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages, > dma_addr_t *addrs, int max_entries) > { > - unsigned count; > - struct scatterlist *sg; > - struct page *page; > - u32 page_len, page_index; > - dma_addr_t addr; > - u32 dma_len, dma_index; > - > - /* > - * Scatterlist elements contains both pages and DMA addresses, but > - * one shoud not assume 1:1 relation between them. The sg->length is > - * the size of the physical memory chunk described by the sg->page, > - * while sg_dma_len(sg) is the size of the DMA (IO virtual) chunk > - * described by the sg_dma_address(sg). > - */ > - page_index = 0; > - dma_index = 0; > - for_each_sg(sgt->sgl, sg, sgt->nents, count) { > - page_len = sg->length; > - page = sg_page(sg); > - dma_len = sg_dma_len(sg); > - addr = sg_dma_address(sg); > - > - while (pages && page_len > 0) { > - if (WARN_ON(page_index >= max_entries)) > + struct sg_dma_page_iter dma_iter; > + struct sg_page_iter page_iter; > + struct page **p = pages; > + dma_addr_t *a = addrs; > + > + if (pages) { > + for_each_sgtable_page(sgt, &page_iter, 0) { > + if (p - pages >= max_entries) > return -1; > - pages[page_index] = page; > - page++; > - page_len -= PAGE_SIZE; > - page_index++; > + *p++ = sg_page_iter_page(&page_iter); > } > - while (addrs && dma_len > 0) { > - if (WARN_ON(dma_index >= max_entries)) > + } > + if (addrs) { > + for_each_sgtable_dma_page(sgt, &dma_iter, 0) { > + if (a - addrs >= max_entries) > return -1; > - addrs[dma_index] = addr; > - addr += PAGE_SIZE; > - dma_len -= PAGE_SIZE; > - dma_index++; > + *a++ = sg_page_iter_dma_address(&dma_iter); > } > } > + > return 0; > } > EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays); >