[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <ZOTsamBusDJu7wY0@casper.infradead.org>
Date: Tue, 22 Aug 2023 18:12:10 +0100
From: Matthew Wilcox <willy@...radead.org>
To: "Russell King (Oracle)" <linux@...linux.org.uk>
Cc: Marek Szyprowski <m.szyprowski@...sung.com>,
linux-arm-kernel@...ts.infradead.org, linux-kernel@...r.kernel.org,
Andrew Morton <akpm@...ux-foundation.org>,
Mike Rapoport <rppt@...nel.org>
Subject: Re: [PATCH] arm: dma-mapping: don't call folio_next() beyond the
requested region
On Thu, Aug 10, 2023 at 12:06:09PM +0100, Russell King (Oracle) wrote:
> However, consider what happens with the above when offset is larger
> than the first folio size. To show this, let's rewrite it:
Hmm. I thought 'off' had to be smaller than PAGE_SIZE.
> So, in all, to me it looks like this conversion is basically wrong, and it
> needs to be something like:
>
> size_t left = size;
>
> while (off >= folio_size(folio)) {
> off -= folio_size(folio);
> folio = folio_next(folio);
> }
We can jump straight to the first folio without iterating over the
folios in between. Like so:
static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
size_t size, enum dma_data_direction dir)
{
phys_addr_t paddr = page_to_phys(page) + off;
...
if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
struct folio *folio = pfn_folio(paddr / PAGE_SIZE);
size_t offset = offset_in_folio(folio, paddr);
for (;;) {
size_t sz = folio_size(folio) - offset;
if (size < sz)
break;
if (!offset)
set_bit(PG_dcache_clean, &folio->flags);
offset = 0;
size -= sz;
if (!size)
break;
folio = folio_next(folio);
}
}
Advantages:
* No more signed arithmetic
* Not even an intended arithmetic overflow
* Only one call to folio_size() per loop
* Folded the first conditional into the loop
Disadvantages:
* Some maintainers don't like a for (;;) loop, or a two-exit loop.
(we could remove the for (;;) by moving 'sz' outside the loop and
recalculating it at the end of the loop)
Powered by blists - more mailing lists