linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iommu/iova: avoid false sharing on fq_timer_on
@ 2019-08-28 13:13 Eric Dumazet
  2019-08-30 10:49 ` Joerg Roedel
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Dumazet @ 2019-08-28 13:13 UTC (permalink / raw)
  To: Joerg Roedel; +Cc: linux-kernel, iommu, Eric Dumazet, Eric Dumazet, Jinyu Qi

In commit 14bd9a607f90 ("iommu/iova: Separate atomic variables
to improve performance") Jinyu Qi identified that the atomic_cmpxchg()
in queue_iova() was causing a performance loss and moved critical fields
so that the false sharing would not impact them.

However, avoiding the false sharing in the first place seems easy.
We should attempt the atomic_cmpxchg() no more than 100 times
per second. Adding an atomic_read() will keep the cache
line mostly shared.

This false sharing came with commit 9a005a800ae8
("iommu/iova: Add flush timer").

Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Jinyu Qi <jinyuqi@huawei.com>
Cc: Joerg Roedel <jroedel@suse.de>
---
 drivers/iommu/iova.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c
index 3e1a8a6755723a927a7942a7429ab7e6c19a0027..41c605b0058f9615c2dbdd83f1de2404a9b1d255 100644
--- a/drivers/iommu/iova.c
+++ b/drivers/iommu/iova.c
@@ -577,7 +577,9 @@ void queue_iova(struct iova_domain *iovad,
 
 	spin_unlock_irqrestore(&fq->lock, flags);
 
-	if (atomic_cmpxchg(&iovad->fq_timer_on, 0, 1) == 0)
+	/* Avoid false sharing as much as possible. */
+	if (!atomic_read(&iovad->fq_timer_on) &&
+	    !atomic_cmpxchg(&iovad->fq_timer_on, 0, 1))
 		mod_timer(&iovad->fq_timer,
 			  jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
 }
-- 
2.23.0.187.g17f5b7556c-goog


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

* Re: [PATCH] iommu/iova: avoid false sharing on fq_timer_on
  2019-08-28 13:13 [PATCH] iommu/iova: avoid false sharing on fq_timer_on Eric Dumazet
@ 2019-08-30 10:49 ` Joerg Roedel
  2019-08-30 12:27   ` Robin Murphy
  0 siblings, 1 reply; 4+ messages in thread
From: Joerg Roedel @ 2019-08-30 10:49 UTC (permalink / raw)
  To: Eric Dumazet, Robin Murphy; +Cc: linux-kernel, iommu, Eric Dumazet, Jinyu Qi

Looks good to me, but adding Robin for his opinion.

On Wed, Aug 28, 2019 at 06:13:38AM -0700, Eric Dumazet wrote:
> In commit 14bd9a607f90 ("iommu/iova: Separate atomic variables
> to improve performance") Jinyu Qi identified that the atomic_cmpxchg()
> in queue_iova() was causing a performance loss and moved critical fields
> so that the false sharing would not impact them.
> 
> However, avoiding the false sharing in the first place seems easy.
> We should attempt the atomic_cmpxchg() no more than 100 times
> per second. Adding an atomic_read() will keep the cache
> line mostly shared.
> 
> This false sharing came with commit 9a005a800ae8
> ("iommu/iova: Add flush timer").
> 
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Cc: Jinyu Qi <jinyuqi@huawei.com>
> Cc: Joerg Roedel <jroedel@suse.de>
> ---
>  drivers/iommu/iova.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c
> index 3e1a8a6755723a927a7942a7429ab7e6c19a0027..41c605b0058f9615c2dbdd83f1de2404a9b1d255 100644
> --- a/drivers/iommu/iova.c
> +++ b/drivers/iommu/iova.c
> @@ -577,7 +577,9 @@ void queue_iova(struct iova_domain *iovad,
>  
>  	spin_unlock_irqrestore(&fq->lock, flags);
>  
> -	if (atomic_cmpxchg(&iovad->fq_timer_on, 0, 1) == 0)
> +	/* Avoid false sharing as much as possible. */
> +	if (!atomic_read(&iovad->fq_timer_on) &&
> +	    !atomic_cmpxchg(&iovad->fq_timer_on, 0, 1))
>  		mod_timer(&iovad->fq_timer,
>  			  jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
>  }
> -- 
> 2.23.0.187.g17f5b7556c-goog

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

* Re: [PATCH] iommu/iova: avoid false sharing on fq_timer_on
  2019-08-30 10:49 ` Joerg Roedel
@ 2019-08-30 12:27   ` Robin Murphy
  2019-08-30 13:22     ` Joerg Roedel
  0 siblings, 1 reply; 4+ messages in thread
From: Robin Murphy @ 2019-08-30 12:27 UTC (permalink / raw)
  To: Joerg Roedel, Eric Dumazet
  Cc: linux-kernel, iommu, Eric Dumazet, Jinyu Qi, Will Deacon

On 30/08/2019 11:49, Joerg Roedel wrote:
> Looks good to me, but adding Robin for his opinion.

Sounds reasonable to me too - that should also be true for the majority 
of Arm systems that we know of. Will suggested that atomic_try_cmpxchg() 
might be relevant, but AFAICS that's backwards compared to what we want 
to do here, which I guess is more of an "atomic_unlikely_cmpxchg".

Acked-by: Robin Murphy <robin.murphy@arm.com>

Cheers,
Robin.

> On Wed, Aug 28, 2019 at 06:13:38AM -0700, Eric Dumazet wrote:
>> In commit 14bd9a607f90 ("iommu/iova: Separate atomic variables
>> to improve performance") Jinyu Qi identified that the atomic_cmpxchg()
>> in queue_iova() was causing a performance loss and moved critical fields
>> so that the false sharing would not impact them.
>>
>> However, avoiding the false sharing in the first place seems easy.
>> We should attempt the atomic_cmpxchg() no more than 100 times
>> per second. Adding an atomic_read() will keep the cache
>> line mostly shared.
>>
>> This false sharing came with commit 9a005a800ae8
>> ("iommu/iova: Add flush timer").
>>
>> Signed-off-by: Eric Dumazet <edumazet@google.com>
>> Cc: Jinyu Qi <jinyuqi@huawei.com>
>> Cc: Joerg Roedel <jroedel@suse.de>
>> ---
>>   drivers/iommu/iova.c | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c
>> index 3e1a8a6755723a927a7942a7429ab7e6c19a0027..41c605b0058f9615c2dbdd83f1de2404a9b1d255 100644
>> --- a/drivers/iommu/iova.c
>> +++ b/drivers/iommu/iova.c
>> @@ -577,7 +577,9 @@ void queue_iova(struct iova_domain *iovad,
>>   
>>   	spin_unlock_irqrestore(&fq->lock, flags);
>>   
>> -	if (atomic_cmpxchg(&iovad->fq_timer_on, 0, 1) == 0)
>> +	/* Avoid false sharing as much as possible. */
>> +	if (!atomic_read(&iovad->fq_timer_on) &&
>> +	    !atomic_cmpxchg(&iovad->fq_timer_on, 0, 1))
>>   		mod_timer(&iovad->fq_timer,
>>   			  jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
>>   }
>> -- 
>> 2.23.0.187.g17f5b7556c-goog

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

* Re: [PATCH] iommu/iova: avoid false sharing on fq_timer_on
  2019-08-30 12:27   ` Robin Murphy
@ 2019-08-30 13:22     ` Joerg Roedel
  0 siblings, 0 replies; 4+ messages in thread
From: Joerg Roedel @ 2019-08-30 13:22 UTC (permalink / raw)
  To: Robin Murphy
  Cc: Eric Dumazet, linux-kernel, iommu, Eric Dumazet, Jinyu Qi, Will Deacon

On Fri, Aug 30, 2019 at 01:27:25PM +0100, Robin Murphy wrote:
> On 30/08/2019 11:49, Joerg Roedel wrote:
> > Looks good to me, but adding Robin for his opinion.
> 
> Sounds reasonable to me too - that should also be true for the majority of
> Arm systems that we know of. Will suggested that atomic_try_cmpxchg() might
> be relevant, but AFAICS that's backwards compared to what we want to do
> here, which I guess is more of an "atomic_unlikely_cmpxchg".
> 
> Acked-by: Robin Murphy <robin.murphy@arm.com>

Great, thanks for looking into it, Robin.

Applied now, thanks Eric.

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

end of thread, other threads:[~2019-08-30 13:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-28 13:13 [PATCH] iommu/iova: avoid false sharing on fq_timer_on Eric Dumazet
2019-08-30 10:49 ` Joerg Roedel
2019-08-30 12:27   ` Robin Murphy
2019-08-30 13:22     ` Joerg Roedel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).