All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
@ 2022-05-13 15:39 ` Niklas Schnelle
  0 siblings, 0 replies; 10+ messages in thread
From: Niklas Schnelle @ 2022-05-13 15:39 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Christoph Hellwig, Jason Gunthorpe, Robin Murphy, Matthew Rosato,
	linux-kernel, iommu

In __iommu_dma_alloc_noncontiguous() the value returned by
iommu_map_sg_atomic() is checked for being smaller than size. Before
commit ad8f36e4b6b1 ("iommu: return full error code from
iommu_map_sg[_atomic]()") this simply checked if the requested size was
successfully mapped.

After that commit iommu_map_sg_atomic() may also return a negative
error value. In principle this too would be covered by the existing
check. There is one problem however, as size is of type size_t while the
return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
converted to size_t and negative error values end up as very large
positive values making the check succeed. Fix this by making the return
type visible with a local variable and add an explicit cast to ssize_t.

Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
Cc: stable@vger.kernel.org
Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
---
 drivers/iommu/dma-iommu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 09f6e1c0f9c0..b4fcf1d92994 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -776,6 +776,7 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
 	unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
 	struct page **pages;
 	dma_addr_t iova;
+	ssize_t mapped;
 
 	if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
 	    iommu_deferred_attach(dev, domain))
@@ -813,8 +814,8 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
 			arch_dma_prep_coherent(sg_page(sg), sg->length);
 	}
 
-	if (iommu_map_sg_atomic(domain, iova, sgt->sgl, sgt->orig_nents, ioprot)
-			< size)
+	mapped = iommu_map_sg_atomic(domain, iova, sgt->sgl, sgt->orig_nents, ioprot);
+	if (mapped < (ssize_t)size)
 		goto out_free_sg;
 
 	sgt->sgl->dma_address = iova;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
@ 2022-05-13 15:39 ` Niklas Schnelle
  0 siblings, 0 replies; 10+ messages in thread
From: Niklas Schnelle @ 2022-05-13 15:39 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: linux-kernel, iommu, Jason Gunthorpe, Robin Murphy, Christoph Hellwig

In __iommu_dma_alloc_noncontiguous() the value returned by
iommu_map_sg_atomic() is checked for being smaller than size. Before
commit ad8f36e4b6b1 ("iommu: return full error code from
iommu_map_sg[_atomic]()") this simply checked if the requested size was
successfully mapped.

After that commit iommu_map_sg_atomic() may also return a negative
error value. In principle this too would be covered by the existing
check. There is one problem however, as size is of type size_t while the
return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
converted to size_t and negative error values end up as very large
positive values making the check succeed. Fix this by making the return
type visible with a local variable and add an explicit cast to ssize_t.

Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
Cc: stable@vger.kernel.org
Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
---
 drivers/iommu/dma-iommu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 09f6e1c0f9c0..b4fcf1d92994 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -776,6 +776,7 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
 	unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
 	struct page **pages;
 	dma_addr_t iova;
+	ssize_t mapped;
 
 	if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
 	    iommu_deferred_attach(dev, domain))
@@ -813,8 +814,8 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
 			arch_dma_prep_coherent(sg_page(sg), sg->length);
 	}
 
-	if (iommu_map_sg_atomic(domain, iova, sgt->sgl, sgt->orig_nents, ioprot)
-			< size)
+	mapped = iommu_map_sg_atomic(domain, iova, sgt->sgl, sgt->orig_nents, ioprot);
+	if (mapped < (ssize_t)size)
 		goto out_free_sg;
 
 	sgt->sgl->dma_address = iova;
-- 
2.32.0

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
  2022-05-13 15:39 ` Niklas Schnelle
@ 2022-05-17  8:36   ` Christoph Hellwig
  -1 siblings, 0 replies; 10+ messages in thread
From: Christoph Hellwig @ 2022-05-17  8:36 UTC (permalink / raw)
  To: Niklas Schnelle
  Cc: Joerg Roedel, Will Deacon, Christoph Hellwig, Jason Gunthorpe,
	Robin Murphy, Matthew Rosato, linux-kernel, iommu

On Fri, May 13, 2022 at 05:39:48PM +0200, Niklas Schnelle wrote:
> In __iommu_dma_alloc_noncontiguous() the value returned by
> iommu_map_sg_atomic() is checked for being smaller than size. Before
> commit ad8f36e4b6b1 ("iommu: return full error code from
> iommu_map_sg[_atomic]()") this simply checked if the requested size was
> successfully mapped.
> 
> After that commit iommu_map_sg_atomic() may also return a negative
> error value. In principle this too would be covered by the existing
> check. There is one problem however, as size is of type size_t while the
> return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
> converted to size_t and negative error values end up as very large
> positive values making the check succeed. Fix this by making the return
> type visible with a local variable and add an explicit cast to ssize_t.
> 
> Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>

I don't see what the point of the newly added local variable is here.
Just casting size should be all that is needed as far as I can tell.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
@ 2022-05-17  8:36   ` Christoph Hellwig
  0 siblings, 0 replies; 10+ messages in thread
From: Christoph Hellwig @ 2022-05-17  8:36 UTC (permalink / raw)
  To: Niklas Schnelle
  Cc: Will Deacon, linux-kernel, iommu, Jason Gunthorpe, Robin Murphy,
	Christoph Hellwig

On Fri, May 13, 2022 at 05:39:48PM +0200, Niklas Schnelle wrote:
> In __iommu_dma_alloc_noncontiguous() the value returned by
> iommu_map_sg_atomic() is checked for being smaller than size. Before
> commit ad8f36e4b6b1 ("iommu: return full error code from
> iommu_map_sg[_atomic]()") this simply checked if the requested size was
> successfully mapped.
> 
> After that commit iommu_map_sg_atomic() may also return a negative
> error value. In principle this too would be covered by the existing
> check. There is one problem however, as size is of type size_t while the
> return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
> converted to size_t and negative error values end up as very large
> positive values making the check succeed. Fix this by making the return
> type visible with a local variable and add an explicit cast to ssize_t.
> 
> Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>

I don't see what the point of the newly added local variable is here.
Just casting size should be all that is needed as far as I can tell.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
  2022-05-17  8:36   ` Christoph Hellwig
@ 2022-05-17 10:17     ` Niklas Schnelle
  -1 siblings, 0 replies; 10+ messages in thread
From: Niklas Schnelle @ 2022-05-17 10:17 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Will Deacon, linux-kernel, iommu, Jason Gunthorpe, Robin Murphy

On Tue, 2022-05-17 at 10:36 +0200, Christoph Hellwig wrote:
> On Fri, May 13, 2022 at 05:39:48PM +0200, Niklas Schnelle wrote:
> > In __iommu_dma_alloc_noncontiguous() the value returned by
> > iommu_map_sg_atomic() is checked for being smaller than size. Before
> > commit ad8f36e4b6b1 ("iommu: return full error code from
> > iommu_map_sg[_atomic]()") this simply checked if the requested size was
> > successfully mapped.
> > 
> > After that commit iommu_map_sg_atomic() may also return a negative
> > error value. In principle this too would be covered by the existing
> > check. There is one problem however, as size is of type size_t while the
> > return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
> > converted to size_t and negative error values end up as very large
> > positive values making the check succeed. Fix this by making the return
> > type visible with a local variable and add an explicit cast to ssize_t.
> > 
> > Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
> 
> I don't see what the point of the newly added local variable is here.
> Just casting size should be all that is needed as far as I can tell.

No technical reason just found it easier to read and more descriptive.
I'll sent a v2 with just the cast, it does simplify the commit message.

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
@ 2022-05-17 10:17     ` Niklas Schnelle
  0 siblings, 0 replies; 10+ messages in thread
From: Niklas Schnelle @ 2022-05-17 10:17 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Joerg Roedel, Will Deacon, Jason Gunthorpe, Robin Murphy,
	Matthew Rosato, linux-kernel, iommu

On Tue, 2022-05-17 at 10:36 +0200, Christoph Hellwig wrote:
> On Fri, May 13, 2022 at 05:39:48PM +0200, Niklas Schnelle wrote:
> > In __iommu_dma_alloc_noncontiguous() the value returned by
> > iommu_map_sg_atomic() is checked for being smaller than size. Before
> > commit ad8f36e4b6b1 ("iommu: return full error code from
> > iommu_map_sg[_atomic]()") this simply checked if the requested size was
> > successfully mapped.
> > 
> > After that commit iommu_map_sg_atomic() may also return a negative
> > error value. In principle this too would be covered by the existing
> > check. There is one problem however, as size is of type size_t while the
> > return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
> > converted to size_t and negative error values end up as very large
> > positive values making the check succeed. Fix this by making the return
> > type visible with a local variable and add an explicit cast to ssize_t.
> > 
> > Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
> 
> I don't see what the point of the newly added local variable is here.
> Just casting size should be all that is needed as far as I can tell.

No technical reason just found it easier to read and more descriptive.
I'll sent a v2 with just the cast, it does simplify the commit message.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
  2022-05-17 10:17     ` Niklas Schnelle
@ 2022-05-17 10:18       ` Robin Murphy
  -1 siblings, 0 replies; 10+ messages in thread
From: Robin Murphy @ 2022-05-17 10:18 UTC (permalink / raw)
  To: Niklas Schnelle, Christoph Hellwig
  Cc: linux-kernel, iommu, Jason Gunthorpe, Will Deacon

On 2022-05-17 11:17, Niklas Schnelle wrote:
> On Tue, 2022-05-17 at 10:36 +0200, Christoph Hellwig wrote:
>> On Fri, May 13, 2022 at 05:39:48PM +0200, Niklas Schnelle wrote:
>>> In __iommu_dma_alloc_noncontiguous() the value returned by
>>> iommu_map_sg_atomic() is checked for being smaller than size. Before
>>> commit ad8f36e4b6b1 ("iommu: return full error code from
>>> iommu_map_sg[_atomic]()") this simply checked if the requested size was
>>> successfully mapped.
>>>
>>> After that commit iommu_map_sg_atomic() may also return a negative
>>> error value. In principle this too would be covered by the existing
>>> check. There is one problem however, as size is of type size_t while the
>>> return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
>>> converted to size_t and negative error values end up as very large
>>> positive values making the check succeed. Fix this by making the return
>>> type visible with a local variable and add an explicit cast to ssize_t.
>>>
>>> Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
>>> Cc: stable@vger.kernel.org
>>> Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
>>
>> I don't see what the point of the newly added local variable is here.
>> Just casting size should be all that is needed as far as I can tell.
> 
> No technical reason just found it easier to read and more descriptive.
> I'll sent a v2 with just the cast, it does simplify the commit message.

Note that this is already fixed upstream, though:

https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git/commit/?h=core&id=a3884774d731f03d3a3dd4fb70ec2d9341ceb39d

Robin.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
@ 2022-05-17 10:18       ` Robin Murphy
  0 siblings, 0 replies; 10+ messages in thread
From: Robin Murphy @ 2022-05-17 10:18 UTC (permalink / raw)
  To: Niklas Schnelle, Christoph Hellwig
  Cc: Joerg Roedel, Will Deacon, Jason Gunthorpe, Matthew Rosato,
	linux-kernel, iommu

On 2022-05-17 11:17, Niklas Schnelle wrote:
> On Tue, 2022-05-17 at 10:36 +0200, Christoph Hellwig wrote:
>> On Fri, May 13, 2022 at 05:39:48PM +0200, Niklas Schnelle wrote:
>>> In __iommu_dma_alloc_noncontiguous() the value returned by
>>> iommu_map_sg_atomic() is checked for being smaller than size. Before
>>> commit ad8f36e4b6b1 ("iommu: return full error code from
>>> iommu_map_sg[_atomic]()") this simply checked if the requested size was
>>> successfully mapped.
>>>
>>> After that commit iommu_map_sg_atomic() may also return a negative
>>> error value. In principle this too would be covered by the existing
>>> check. There is one problem however, as size is of type size_t while the
>>> return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
>>> converted to size_t and negative error values end up as very large
>>> positive values making the check succeed. Fix this by making the return
>>> type visible with a local variable and add an explicit cast to ssize_t.
>>>
>>> Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
>>> Cc: stable@vger.kernel.org
>>> Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
>>
>> I don't see what the point of the newly added local variable is here.
>> Just casting size should be all that is needed as far as I can tell.
> 
> No technical reason just found it easier to read and more descriptive.
> I'll sent a v2 with just the cast, it does simplify the commit message.

Note that this is already fixed upstream, though:

https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git/commit/?h=core&id=a3884774d731f03d3a3dd4fb70ec2d9341ceb39d

Robin.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
  2022-05-17 10:18       ` Robin Murphy
@ 2022-05-17 10:32         ` Niklas Schnelle
  -1 siblings, 0 replies; 10+ messages in thread
From: Niklas Schnelle @ 2022-05-17 10:32 UTC (permalink / raw)
  To: Robin Murphy, Christoph Hellwig
  Cc: linux-kernel, iommu, Jason Gunthorpe, Will Deacon

On Tue, 2022-05-17 at 11:18 +0100, Robin Murphy wrote:
> On 2022-05-17 11:17, Niklas Schnelle wrote:
> > On Tue, 2022-05-17 at 10:36 +0200, Christoph Hellwig wrote:
> > > On Fri, May 13, 2022 at 05:39:48PM +0200, Niklas Schnelle wrote:
> > > > In __iommu_dma_alloc_noncontiguous() the value returned by
> > > > iommu_map_sg_atomic() is checked for being smaller than size. Before
> > > > commit ad8f36e4b6b1 ("iommu: return full error code from
> > > > iommu_map_sg[_atomic]()") this simply checked if the requested size was
> > > > successfully mapped.
> > > > 
> > > > After that commit iommu_map_sg_atomic() may also return a negative
> > > > error value. In principle this too would be covered by the existing
> > > > check. There is one problem however, as size is of type size_t while the
> > > > return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
> > > > converted to size_t and negative error values end up as very large
> > > > positive values making the check succeed. Fix this by making the return
> > > > type visible with a local variable and add an explicit cast to ssize_t.
> > > > 
> > > > Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
> > > > Cc: stable@vger.kernel.org
> > > > Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
> > > 
> > > I don't see what the point of the newly added local variable is here.
> > > Just casting size should be all that is needed as far as I can tell.
> > 
> > No technical reason just found it easier to read and more descriptive.
> > I'll sent a v2 with just the cast, it does simplify the commit message.
> 
> Note that this is already fixed upstream, though:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git/commit/?h=core&id=a3884774d731f03d3a3dd4fb70ec2d9341ceb39d
> 
> Robin.

Ah oh well then nevermind and you can of course also ignore the v2 I
sent out a minute ago.

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic()
@ 2022-05-17 10:32         ` Niklas Schnelle
  0 siblings, 0 replies; 10+ messages in thread
From: Niklas Schnelle @ 2022-05-17 10:32 UTC (permalink / raw)
  To: Robin Murphy, Christoph Hellwig
  Cc: Joerg Roedel, Will Deacon, Jason Gunthorpe, Matthew Rosato,
	linux-kernel, iommu

On Tue, 2022-05-17 at 11:18 +0100, Robin Murphy wrote:
> On 2022-05-17 11:17, Niklas Schnelle wrote:
> > On Tue, 2022-05-17 at 10:36 +0200, Christoph Hellwig wrote:
> > > On Fri, May 13, 2022 at 05:39:48PM +0200, Niklas Schnelle wrote:
> > > > In __iommu_dma_alloc_noncontiguous() the value returned by
> > > > iommu_map_sg_atomic() is checked for being smaller than size. Before
> > > > commit ad8f36e4b6b1 ("iommu: return full error code from
> > > > iommu_map_sg[_atomic]()") this simply checked if the requested size was
> > > > successfully mapped.
> > > > 
> > > > After that commit iommu_map_sg_atomic() may also return a negative
> > > > error value. In principle this too would be covered by the existing
> > > > check. There is one problem however, as size is of type size_t while the
> > > > return type of iommu_map_sg_atomic() is now of type ssize_t the latter gets
> > > > converted to size_t and negative error values end up as very large
> > > > positive values making the check succeed. Fix this by making the return
> > > > type visible with a local variable and add an explicit cast to ssize_t.
> > > > 
> > > > Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()")
> > > > Cc: stable@vger.kernel.org
> > > > Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com>
> > > 
> > > I don't see what the point of the newly added local variable is here.
> > > Just casting size should be all that is needed as far as I can tell.
> > 
> > No technical reason just found it easier to read and more descriptive.
> > I'll sent a v2 with just the cast, it does simplify the commit message.
> 
> Note that this is already fixed upstream, though:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git/commit/?h=core&id=a3884774d731f03d3a3dd4fb70ec2d9341ceb39d
> 
> Robin.

Ah oh well then nevermind and you can of course also ignore the v2 I
sent out a minute ago.


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-05-17 10:35 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-13 15:39 [PATCH] iommu/dma: Fix check for error return from iommu_map_sg_atomic() Niklas Schnelle
2022-05-13 15:39 ` Niklas Schnelle
2022-05-17  8:36 ` Christoph Hellwig
2022-05-17  8:36   ` Christoph Hellwig
2022-05-17 10:17   ` Niklas Schnelle
2022-05-17 10:17     ` Niklas Schnelle
2022-05-17 10:18     ` Robin Murphy
2022-05-17 10:18       ` Robin Murphy
2022-05-17 10:32       ` Niklas Schnelle
2022-05-17 10:32         ` Niklas Schnelle

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.