All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] swiotlb: add the missing dynamic swiotlb check in is_swiotlb_buffer
@ 2023-09-05  6:44 Christoph Hellwig
  2023-09-08 12:44 ` Petr Tesařík
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2023-09-05  6:44 UTC (permalink / raw)
  To: m.szyprowski, robin.murphy, petr.tesarik.ext; +Cc: iommu, Jonathan Corbet

Commit 1395706a1490 ("swiotlb: search the software IO TLB only if the
device makes use of it") claims to avoid the dynamic swiotlb lookup
unless dynamic allocations are actually used.  And while it adds all
the infrastructure for doing that, it misses the final bit of actually
checking the flag.  Actually add the check for this functionality to
work.

Fixes: 1395706a1490 ("swiotlb: search the software IO TLB only if the device makes use of it")
Reported-by: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 include/linux/swiotlb.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h
index b4536626f8ff35..a188025389836b 100644
--- a/include/linux/swiotlb.h
+++ b/include/linux/swiotlb.h
@@ -172,13 +172,17 @@ static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
 	if (!mem)
 		return false;
 
-	if (IS_ENABLED(CONFIG_SWIOTLB_DYNAMIC)) {
-		/* Pairs with smp_wmb() in swiotlb_find_slots() and
+#ifdef CONFIG_SWIOTLB_DYNAMIC
+	if (dev->dma_uses_io_tlb) {
+		/*
+		 * Pairs with smp_wmb() in swiotlb_find_slots() and
 		 * swiotlb_dyn_alloc(), which modify the RCU lists.
 		 */
 		smp_rmb();
 		return swiotlb_find_pool(dev, paddr);
 	}
+#endif
+
 	return paddr >= mem->defpool.start && paddr < mem->defpool.end;
 }
 
-- 
2.39.2


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

* Re: [PATCH] swiotlb: add the missing dynamic swiotlb check in is_swiotlb_buffer
  2023-09-05  6:44 [PATCH] swiotlb: add the missing dynamic swiotlb check in is_swiotlb_buffer Christoph Hellwig
@ 2023-09-08 12:44 ` Petr Tesařík
  2023-09-09  8:40   ` Christoph Hellwig
  0 siblings, 1 reply; 4+ messages in thread
From: Petr Tesařík @ 2023-09-08 12:44 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: m.szyprowski, robin.murphy, petr.tesarik.ext, iommu,
	Jonathan Corbet

On Tue,  5 Sep 2023 08:44:41 +0200
Christoph Hellwig <hch@lst.de> wrote:

> Commit 1395706a1490 ("swiotlb: search the software IO TLB only if the
> device makes use of it") claims to avoid the dynamic swiotlb lookup
> unless dynamic allocations are actually used.  And while it adds all
> the infrastructure for doing that, it misses the final bit of actually
> checking the flag.  Actually add the check for this functionality to
> work.
> 
> Fixes: 1395706a1490 ("swiotlb: search the software IO TLB only if the device makes use of it")
> Reported-by: Jonathan Corbet <corbet@lwn.net>
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  include/linux/swiotlb.h | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h
> index b4536626f8ff35..a188025389836b 100644
> --- a/include/linux/swiotlb.h
> +++ b/include/linux/swiotlb.h
> @@ -172,13 +172,17 @@ static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
>  	if (!mem)
>  		return false;
>  
> -	if (IS_ENABLED(CONFIG_SWIOTLB_DYNAMIC)) {
> -		/* Pairs with smp_wmb() in swiotlb_find_slots() and
> +#ifdef CONFIG_SWIOTLB_DYNAMIC
> +	if (dev->dma_uses_io_tlb) {
> +		/*
> +		 * Pairs with smp_wmb() in swiotlb_find_slots() and
>  		 * swiotlb_dyn_alloc(), which modify the RCU lists.
>  		 */
>  		smp_rmb();
>  		return swiotlb_find_pool(dev, paddr);
>  	}
> +#endif
> +

Not quite. This code is not SMP safe. I believe that:

1. the value must be read with a READ_ONCE(),
2. the memory barrier must precede the read operation.

Do you want to send a v2, or should I send my version instead?

Petr T

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

* Re: [PATCH] swiotlb: add the missing dynamic swiotlb check in is_swiotlb_buffer
  2023-09-08 12:44 ` Petr Tesařík
@ 2023-09-09  8:40   ` Christoph Hellwig
  2023-09-13 11:10     ` Petr Tesařík
  0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2023-09-09  8:40 UTC (permalink / raw)
  To: Petr Tesařík
  Cc: Christoph Hellwig, m.szyprowski, robin.murphy, petr.tesarik.ext,
	iommu, Jonathan Corbet

On Fri, Sep 08, 2023 at 02:44:18PM +0200, Petr Tesařík wrote:
> 
> Not quite. This code is not SMP safe. I believe that:
> 
> 1. the value must be read with a READ_ONCE(),
> 2. the memory barrier must precede the read operation.
> 
> Do you want to send a v2, or should I send my version instead?

As I'm travelling right now I'd love you to take care of it.  It's
probably a new thing and not a v2 so feel free to take full credit.


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

* Re: [PATCH] swiotlb: add the missing dynamic swiotlb check in is_swiotlb_buffer
  2023-09-09  8:40   ` Christoph Hellwig
@ 2023-09-13 11:10     ` Petr Tesařík
  0 siblings, 0 replies; 4+ messages in thread
From: Petr Tesařík @ 2023-09-13 11:10 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: m.szyprowski, robin.murphy, iommu, Jonathan Corbet

On Sat, 9 Sep 2023 10:40:45 +0200
Christoph Hellwig <hch@lst.de> wrote:

> On Fri, Sep 08, 2023 at 02:44:18PM +0200, Petr Tesařík wrote:
> > 
> > Not quite. This code is not SMP safe. I believe that:
> > 
> > 1. the value must be read with a READ_ONCE(),
> > 2. the memory barrier must precede the read operation.
> > 
> > Do you want to send a v2, or should I send my version instead?  
> 
> As I'm travelling right now I'd love you to take care of it.  It's
> probably a new thing and not a v2 so feel free to take full credit.

I'm sorry it took a bit longer than anticipated. I unexpectedly ran
into an apparent performance regression, but all looks good after I
fixed my test setup to compare apples with apples and oranges with
oranges. ;-)

Patch coming soon.

Petr T

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

end of thread, other threads:[~2023-09-13 11:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-05  6:44 [PATCH] swiotlb: add the missing dynamic swiotlb check in is_swiotlb_buffer Christoph Hellwig
2023-09-08 12:44 ` Petr Tesařík
2023-09-09  8:40   ` Christoph Hellwig
2023-09-13 11:10     ` Petr Tesařík

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.