2022-09-29 15:38:03

by Niklas Schnelle

[permalink] [raw]
Subject: [PATCH v3 4/5] iommu/s390: Fix incorrect aperture check

The domain->geometry.aperture_end specifies the last valid address treat
it as such when checking if a DMA address is valid.

Reviewed-by: Pierre Morel <[email protected]>
Reviewed-by: Matthew Rosato <[email protected]>
Signed-off-by: Niklas Schnelle <[email protected]>
---
drivers/iommu/s390-iommu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c
index ed0e64f478cf..6d4a9c7db32c 100644
--- a/drivers/iommu/s390-iommu.c
+++ b/drivers/iommu/s390-iommu.c
@@ -210,7 +210,7 @@ static int s390_iommu_update_trans(struct s390_domain *s390_domain,
int rc = 0;

if (dma_addr < s390_domain->domain.geometry.aperture_start ||
- dma_addr + size > s390_domain->domain.geometry.aperture_end)
+ dma_addr + size > s390_domain->domain.geometry.aperture_end + 1)
return -EINVAL;

nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
--
2.34.1


2022-09-29 16:12:29

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH v3 4/5] iommu/s390: Fix incorrect aperture check

On Thu, Sep 29, 2022 at 05:33:01PM +0200, Niklas Schnelle wrote:
> The domain->geometry.aperture_end specifies the last valid address treat
> it as such when checking if a DMA address is valid.
>
> Reviewed-by: Pierre Morel <[email protected]>
> Reviewed-by: Matthew Rosato <[email protected]>
> Signed-off-by: Niklas Schnelle <[email protected]>
> ---
> drivers/iommu/s390-iommu.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c
> index ed0e64f478cf..6d4a9c7db32c 100644
> --- a/drivers/iommu/s390-iommu.c
> +++ b/drivers/iommu/s390-iommu.c
> @@ -210,7 +210,7 @@ static int s390_iommu_update_trans(struct s390_domain *s390_domain,
> int rc = 0;
>
> if (dma_addr < s390_domain->domain.geometry.aperture_start ||
> - dma_addr + size > s390_domain->domain.geometry.aperture_end)
> + dma_addr + size > s390_domain->domain.geometry.aperture_end + 1)

The reason the iommu layer uses 'last' (= start + size - 1) not 'end'
is to allow for the very last byte of the range to be used.

Meaning (start + size) == 0 in some cases due to the overflow.

Generally when working with lasts's I prefer people write code in a
way that doesn't trigger the overflow, because there are some
complicated C rules about integer promotion that can mean the desired
overflow silently doesn't happen in obscure cases - especially if
unsigned long != u64

So, I'd write this as:

(dma_addr + size - 1) > s390_domain->domain.geometry.aperture_end

Jason

2022-09-30 08:19:40

by Niklas Schnelle

[permalink] [raw]
Subject: Re: [PATCH v3 4/5] iommu/s390: Fix incorrect aperture check

On Thu, 2022-09-29 at 12:58 -0300, Jason Gunthorpe wrote:
> On Thu, Sep 29, 2022 at 05:33:01PM +0200, Niklas Schnelle wrote:
> > The domain->geometry.aperture_end specifies the last valid address treat
> > it as such when checking if a DMA address is valid.
> >
> > Reviewed-by: Pierre Morel <[email protected]>
> > Reviewed-by: Matthew Rosato <[email protected]>
> > Signed-off-by: Niklas Schnelle <[email protected]>
> > ---
> > drivers/iommu/s390-iommu.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c
> > index ed0e64f478cf..6d4a9c7db32c 100644
> > --- a/drivers/iommu/s390-iommu.c
> > +++ b/drivers/iommu/s390-iommu.c
> > @@ -210,7 +210,7 @@ static int s390_iommu_update_trans(struct s390_domain *s390_domain,
> > int rc = 0;
> >
> > if (dma_addr < s390_domain->domain.geometry.aperture_start ||
> > - dma_addr + size > s390_domain->domain.geometry.aperture_end)
> > + dma_addr + size > s390_domain->domain.geometry.aperture_end + 1)
>
> The reason the iommu layer uses 'last' (= start + size - 1) not 'end'
> is to allow for the very last byte of the range to be used.
>
> Meaning (start + size) == 0 in some cases due to the overflow.
>
> Generally when working with lasts's I prefer people write code in a
> way that doesn't trigger the overflow, because there are some
> complicated C rules about integer promotion that can mean the desired
> overflow silently doesn't happen in obscure cases - especially if
> unsigned long != u64
>
> So, I'd write this as:
>
> (dma_addr + size - 1) > s390_domain->domain.geometry.aperture_end
>
> Jason

Makes sense. Thanks.