[PATCH] arm: dma-mapping: don't call folio_next() beyond the requested region

Matthew Wilcox willy at infradead.org
Tue Aug 22 10:12:10 PDT 2023


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)



More information about the linux-arm-kernel mailing list