From patchwork Wed Aug 23 19:18:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13363061 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 95E6DC3DA66 for ; Wed, 23 Aug 2023 19:19:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B50BC280085; Wed, 23 Aug 2023 15:19:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B010028007C; Wed, 23 Aug 2023 15:19:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F05B280085; Wed, 23 Aug 2023 15:19:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 81C9028007C for ; Wed, 23 Aug 2023 15:19:16 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2AE0D1C9208 for ; Wed, 23 Aug 2023 19:19:16 +0000 (UTC) X-FDA: 81156332712.08.ECF4C7A Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf07.hostedemail.com (Postfix) with ESMTP id 8D2ED40010 for ; Wed, 23 Aug 2023 19:19:13 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=cv7bFkx4; dmarc=none; spf=none (imf07.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692818354; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=TMNmrd+rOYSZliS9Ed1c+i0bae31CLWzR0pXsj6pnmY=; b=ZLPHbOfCwmb6H8qep1lUEamv5R1sixjOcex8TblJv6yGxSsCDqi5aHGm135YIVqEf6FrAY AS0bOhJRlNHNhfVkI8bBiWA9o/ckV3eYzZM93M46Pr9vrTQoO3OUeMTYNjP8xRptXSgJ9s Yk30LkmDApldSj6sSJutFiIunUm67PM= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=cv7bFkx4; dmarc=none; spf=none (imf07.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692818354; a=rsa-sha256; cv=none; b=gI37GS9PFQQ7ufT1+WKwaIlBl/Sk1Xh6K8DDQoG5u69BsIiYrJQ/ldEDJA8jOHealYtyMP UWfv9ATYefk7QjistMI6GHRexAyFgd7lJmwOV/508vzrCcb+A6nId27nMZ2rPXr+djlMTV XTotFYKddvDyy4c+cZjqhimABvThMyw= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:In-Reply-To:References; bh=TMNmrd+rOYSZliS9Ed1c+i0bae31CLWzR0pXsj6pnmY=; b=cv7bFkx4oMF9MyUNm420+PgiyM a6c9YB1XyQpZqjYTDEunwzYMNHKMKl/MFftdGgF/21e0dKVSY6MxcvVbOV7HIAUgfQGSnWshgobi9 KhpdUlmWvfzaarmu18S1BY8CIISjU+7vi9/t2WLZDmsdMY7kDvcJkTTJlo7HHxzjGAl9dJAiWJL8u 5sQ8G2TiIXHFliU7m6pGGtJZOhmOg7pt6lWrpFdJnth+BQb2ZzFbAwFsu671RMgPii0Xnxhna0/UF szik4bxrhwrklmO4LKRylslyvZrBIoZsNl/P7s4Fp78oaEdLAQMZ20hHUfdGcCtQBEZdaxTIs9u8N qc424BMQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qYtNa-006WxW-Ab; Wed, 23 Aug 2023 19:18:58 +0000 From: "Matthew Wilcox (Oracle)" To: Russell King , Andrew Morton Cc: "Matthew Wilcox (Oracle)" , Marek Szyprowski , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org Subject: [PATCH] Fix folio conversion in __dma_page_dev_to_cpu() Date: Wed, 23 Aug 2023 20:18:52 +0100 Message-Id: <20230823191852.1556561-1-willy@infradead.org> X-Mailer: git-send-email 2.37.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 8D2ED40010 X-Stat-Signature: qswtqf5muqjyrxwqoixmexcgqt5pj8ic X-HE-Tag: 1692818353-859086 X-HE-Meta: U2FsdGVkX1/I6n7D8Mg4qIl5Y5OyHH1woSu18zj6khR5AxzCIhR6XP4TsIcTzgmYdgVr2ooIWv7LJNRnvQq8n5IP5uMfOZjINpTkTNzrHQBjZtW4yPvdQX8vi/x/yNsIw0gbRpL/jbux4TYYmT5yD0f3hlKD7e6t0c4SwgP/K+rgKCtMuowQloRy48l9pDYJY7haF3acPsCpc53hDmJPQTVhC5Uqt9/9fUFe49Cda1JnAWy7tO1ClMf0PVzFN2y0Fc1Ac3UXXotvMVv38ILHyyUfCMy7LsiR5OlTkWRlVU5XoYfKqxLYLT203rl9TzMLoi/fL04YYOIjpNrWylCsCtfpiY1VOLv24ggIwara//KPmtOQaiBmhZfJdrRsELamOu5DqwG5ZPxfPJW4LxjuZ9B8vHVlv0Fgg/0gxraYC480nJh/Gb4HwfwE2uZS3TN2Z323pEIEyu6OhVj1/ITZCTvGO0f/wW/cfio71fommUlAMic7YcSwf/zPQjczcrN1Z4yLpRiL6+9W5j0R+MPJkbPBhD9Bp9nRoXuJzeUo33BIbv2mIgZSpDt4JWnSf3Y1/La9Oty302NW+ppAU5hTY7saj22EW8tSgKEsnaO69RjNsPbOnRccoLufm+Rgx64aB/DEym2SnJA1WfZqMTaE7pTBUYDFsZbsnBAPjqzSU8bqQSSA+pCSN9ZWTfLvCxp4Q8PfWr6mrdjs79faR9otHVAufhz0Fstg/xHVaasDfM6RCOvmcuTyoETkZPLPpYCxNpABaCykjWE0HGL46co9GlWvWv8rnOAuVDQklx56BH5agkDovdqVeDJOFG195cWUXL00+1do/ONSMUrDrg//HouWgtpIxQ97K6te0om3XTkvJGI/v0fWNwdzRPX4B1z82Z/f/qkUV28LfWirHfbxY9/1Nrz2Zu52kQlNZmwlmU7xxmKTbfUoHUVtVPQ8539rFh8c+IUo3OAMoQ6ADk7 z8wCUDpb 64qymH32L8CcoAC+801zoNQR9A01AkiFxqioIvsW0uuOTW9ZTRIe9rcmxT1/nuhz3UO5I7YNpxaLfIwIIN1zIBxHeRBtB2MRKC1X6JKbNffQjv8VJrSQe2Vab6FemG+d3jRTWUDAETyyiRJWI3/tE1FvRnU9YNDCbmpc5 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Russell and Marek pointed out some assumptions I was making about how sg lists work; eg that they are limited to 2GB and that the initial offset lies within the first page (or at least within the first folio that a page belongs to). While I think those assumptions are true, it's not too hard to write a version which does not have those assumptions and also calculates folio_size() only once per loop iteration. --- arch/arm/mm/dma-mapping.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index 0474840224d9..5409225b4abc 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c @@ -695,7 +695,6 @@ static void __dma_page_cpu_to_dev(struct page *page, unsigned long off, static void __dma_page_dev_to_cpu(struct page *page, unsigned long off, size_t size, enum dma_data_direction dir) { - struct folio *folio = page_folio(page); phys_addr_t paddr = page_to_phys(page) + off; /* FIXME: non-speculating: not required */ @@ -710,18 +709,19 @@ static void __dma_page_dev_to_cpu(struct page *page, unsigned long off, * Mark the D-cache clean for these pages to avoid extra flushing. */ if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) { - ssize_t left = size; + struct folio *folio = pfn_folio(paddr / PAGE_SIZE); size_t offset = offset_in_folio(folio, paddr); - if (offset) { - left -= folio_size(folio) - offset; - folio = folio_next(folio); - } + for (;;) { + size_t sz = folio_size(folio) - offset; - while (left >= (ssize_t)folio_size(folio)) { - left -= folio_size(folio); - set_bit(PG_dcache_clean, &folio->flags); - if (!left) + if (size < sz) + break; + if (!offset) + set_bit(PG_dcache_clean, &folio->flags); + offset = 0; + size -= sz; + if (!size) break; folio = folio_next(folio); }