Re: [PATCH 2/2] swiotlb: Enforce page alignment in swiotlb_alloc()

From: Petr Tesařík
Date: Mon Jan 29 2024 - 02:43:41 EST


On Mon, 29 Jan 2024 07:08:53 +0100
Christoph Hellwig <hch@xxxxxx> wrote:

> On Fri, Jan 26, 2024 at 03:19:56PM +0000, Will Deacon wrote:
> > diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
> > index 25febb9e670c..92433ea9f2d2 100644
> > --- a/kernel/dma/swiotlb.c
> > +++ b/kernel/dma/swiotlb.c
> > @@ -1647,6 +1647,12 @@ struct page *swiotlb_alloc(struct device *dev, size_t size)
> > return NULL;
> >
> > tlb_addr = slot_addr(pool->start, index);
> > + if (!PAGE_ALIGNED(tlb_addr)) {
> > + dev_WARN_ONCE(dev, 1, "Cannot return 'struct page *' for non page-aligned swiotlb addr 0x%pa.\n",
> > + &tlb_addr);
> > + swiotlb_release_slots(dev, tlb_addr);
> > + return NULL;
> > + }
> >
> > return pfn_to_page(PFN_DOWN(tlb_addr));
>
> So PFN_DOWN aligns the address and thus per se converting the unaligned
> address isn't a problem. That being said swiotlb obviously should never
> allocate unaligned addresses, but the placement of this check feels
> odd to me. Also because it only catches swiotlb_alloc and not the
> map side.

We may have to rethink how alignment constraints are interpreted. See
also my reply to PATCH 1/2.

> Maybe just throw a WARN_ON_ONCE into slot_addr() ?

Yes.

Or, what if I write a KUnit test suite for swiotlb to combat this
constant stream of various regressions?

Petr T