Re: [PATCH] iommu/amd: fix sg->dma_address for sg->offset bigger than PAGE_SIZE

From: Alexander Duyck
Date: Tue Mar 12 2019 - 11:18:20 EST


On Tue, 2019-03-12 at 08:08 +0100, Stanislaw Gruszka wrote:
> On Mon, Mar 11, 2019 at 08:47:44AM -0700, Alexander Duyck wrote:
> > > drivers/iommu/amd_iommu.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
> > > index 6b0760dafb3e..949621f33624 100644
> > > --- a/drivers/iommu/amd_iommu.c
> > > +++ b/drivers/iommu/amd_iommu.c
> > > @@ -2604,7 +2604,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,
> > >
> > > /* Everything is mapped - write the right values into s->dma_address */
> > > for_each_sg(sglist, s, nelems, i) {
> > > - s->dma_address += address + s->offset;
> > > + s->dma_address += address + (s->offset & ~PAGE_MASK);
> > > s->dma_length = s->length;
> > > }
> > >
> >
> > You should add a comment calling out that this is needed because the
> > sg_phys(s) call above this is masked with PAGE_MASK. Then this makes
> > much more sense. Otherwise I would have assumed you needed either the
> > full offset or none.
>
> Would something like this
>
> /*
> * Everything is mapped - write the right values into s->dma_address.
> * Take into account s->offset can be bigger than page size and sg_phys(s)
> * address has to be aligned to page granularity.
> */
>
> be appropriate ?
>
> Stanislaw
>

No, that isn't a good description. If you take a look at the code a few
lines up you find:
phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);

Now if I am not mistaken the whole reason why you are having to make
the change here is because the application of PAGE_MASK in this line.
Basically what sg_phys() will do is take the address of the page,
convert it to a physical address and add the offset. However what the
mask is doing is limiting how much of that offset can be added. As a
result you have to add the remainder that was masked out. So maybe a
better comment would be something like:

/*
* Add in the remaining piece of the scatter-gather offset that was
* masked out when we were determining the physical address via
* (sg_phys(s) & PAGE_MASK) earlier.
*/