Skip to contentSkip to conversation

mlx5: use RCU lock in mlx5_eq_cq_get()

PATCH Superseded Cong Wang 4 participants 11 messages Feb 6, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 mlx5: use RCU lock in mlx5_eq_cq_get()

mlx5_eq_cq_get() is called in IRQ handler, the spinlock inside gets a lot of contentions when we test some heavy workload with 60 RX queues and 80 CPU's, and it is clearly shown in the flame graph.

In fact, radix_tree_lookup() is perfectly fine with RCU read lock, we don't have to take a spinlock on this hot path. It is pretty much similar to commit 291c566a2891
("net/mlx4_core: Fix racy CQ (Completion Queue) free"). Slow paths are still serialized with the spinlock, and with synchronize_irq() it should be safe to just move the fast path to RCU read lock.

This patch itself reduces the latency by about 50% with our workload.

Cc Saeed Mahameed <[email protected]>
Cc Tariq Toukan <[email protected]>
---
 drivers/net/ethernet/mellanox/mlx5/core/eq.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/eq.c +6 -6
index ee04aab65a9f..7092457705a2 100644
@@ -114,11 +114,11 @@ static struct mlx5_core_cq *mlx5_eq_cq_get(struct mlx5_eq *eq, u32 cqn)
 	struct mlx5_cq_table *table = &eq->cq_table;
 	struct mlx5_core_cq *cq = NULL;
 
-	spin_lock(&table->lock);
+	rcu_read_lock();
 	cq = radix_tree_lookup(&table->tree, cqn);
 	if (likely(cq))
 		mlx5_cq_hold(cq);
-	spin_unlock(&table->lock);
+	rcu_read_unlock();
 
 	return cq;
 }
@@ -371,9 +371,9 @@ int mlx5_eq_add_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
 	struct mlx5_cq_table *table = &eq->cq_table;
 	int err;
 
-	spin_lock_irq(&table->lock);
+	spin_lock(&table->lock);
 	err = radix_tree_insert(&table->tree, cq->cqn, cq);
-	spin_unlock_irq(&table->lock);
+	spin_unlock(&table->lock);
 
 	return err;
 }
@@ -383,9 +383,9 @@ int mlx5_eq_del_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
 	struct mlx5_cq_table *table = &eq->cq_table;
 	struct mlx5_core_cq *tmp;
 
-	spin_lock_irq(&table->lock);
+	spin_lock(&table->lock);
 	tmp = radix_tree_delete(&table->tree, cq->cqn);
-	spin_unlock_irq(&table->lock);
+	spin_unlock(&table->lock);
 
 	if (!tmp) {
 		mlx5_core_warn(eq->dev, "cq 0x%x not found in eq 0x%x tree\n", eq->eqn, cq->cqn);
Signature · 1 line
2.20.1
#1 lore raw
Quoted 26 lines · Cong Wang

mlx5_eq_cq_get() is called in IRQ handler, the spinlock inside gets a lot of contentions when we test some heavy workload with 60 RX queues and 80 CPU's, and it is clearly shown in the flame graph.

In fact, radix_tree_lookup() is perfectly fine with RCU read lock, we don't have to take a spinlock on this hot path. It is pretty much similar to commit 291c566a2891
("net/mlx4_core: Fix racy CQ (Completion Queue) free"). Slow paths are still serialized with the spinlock, and with synchronize_irq() it should be safe to just move the fast path to RCU read lock.

This patch itself reduces the latency by about 50% with our workload.

Cc Saeed Mahameed <[email protected]>
Cc Tariq Toukan <[email protected]>
---
  drivers/net/ethernet/mellanox/mlx5/core/eq.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/eq.c +0 -0
index ee04aab65a9f..7092457705a2 100644

drivers/net/ethernet/mellanox/mlx5/core/eq.c static struct mlx5_core_cq *mlx5_eq_cq_get(struct mlx5_eq *eq, u32 cqn) · line 121

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/eq.c +2 -2
@@ -114,11 +114,11 @@ static struct mlx5_core_cq *mlx5_eq_cq_get(struct mlx5_eq *eq, u32 cqn)
  	struct mlx5_cq_table *table = &eq->cq_table;
  	struct mlx5_core_cq *cq = NULL;
 
-	spin_lock(&table->lock);
+	rcu_read_lock();
  	cq = radix_tree_lookup(&table->tree, cqn);
  	if (likely(cq))
  		mlx5_cq_hold(cq);
-	spin_unlock(&table->lock);
+	rcu_read_unlock();

Thanks for you patch.

I think we can improve it further, by taking the if statement out of the critical section.

Other than that, patch LGTM.

Regards,
Tariq

Quoted 28 lines
drivers/net/ethernet/mellanox/mlx5/core/eq.c +4 -4
  	return cq;
  }
@@ -371,9 +371,9 @@ int mlx5_eq_add_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
  	struct mlx5_cq_table *table = &eq->cq_table;
  	int err;
 
-	spin_lock_irq(&table->lock);
+	spin_lock(&table->lock);
  	err = radix_tree_insert(&table->tree, cq->cqn, cq);
-	spin_unlock_irq(&table->lock);
+	spin_unlock(&table->lock);
 
  	return err;
  }
@@ -383,9 +383,9 @@ int mlx5_eq_del_cq(struct mlx5_eq *eq, struct mlx5_core_cq *cq)
  	struct mlx5_cq_table *table = &eq->cq_table;
  	struct mlx5_core_cq *tmp;
 
-	spin_lock_irq(&table->lock);
+	spin_lock(&table->lock);
  	tmp = radix_tree_delete(&table->tree, cq->cqn);
-	spin_unlock_irq(&table->lock);
+	spin_unlock(&table->lock);
 
  	if (!tmp) {
  		mlx5_core_warn(eq->dev, "cq 0x%x not found in eq 0x%x tree\n", eq->eqn, cq->cqn);
#2 lore raw
Quoted 7 lines · Tariq Toukan
Quoted 5 lines · Cong Wang

mlx5_eq_cq_get() is called in IRQ handler, the spinlock inside gets a lot of contentions when we test some heavy workload with 60 RX queues and 80 CPU's, and it is clearly shown in the flame graph.

Hi Cong,

The patch is ok to me, but i really doubt that you can hit a contention on latest upstream driver, since we already have spinlock per EQ, which means spinlock per core, each EQ (core) msix handler can only access one spinlock (its own), so I am surprised how you got the contention, Maybe you are not running on latest upstream driver ?

what is the workload ?

Quoted 38 lines
Quoted 25 lines

In fact, radix_tree_lookup() is perfectly fine with RCU read lock, we don't have to take a spinlock on this hot path. It is pretty much
similar to commit 291c566a2891
("net/mlx4_core: Fix racy CQ (Completion Queue) free"). Slow paths are still serialized with the spinlock, and with synchronize_irq() it should be safe to just move the fast path to RCU read lock.

This patch itself reduces the latency by about 50% with our workload.

Cc Saeed Mahameed <[email protected]>
Cc Tariq Toukan <[email protected]>
---
  drivers/net/ethernet/mellanox/mlx5/core/eq.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/eq.c +0 -0
index ee04aab65a9f..7092457705a2 100644
@@ -114,11 +114,11 @@ static struct mlx5_core_cq
 *mlx5_eq_cq_get(struct mlx5_eq *eq, u32 cqn)

drivers/net/ethernet/mellanox/mlx5/core/eq.c static struct mlx5_core_cq · line 122

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/eq.c +2 -2
  	struct mlx5_cq_table *table = &eq->cq_table;
  	struct mlx5_core_cq *cq = NULL;
 
-	spin_lock(&table->lock);
+	rcu_read_lock();
  	cq = radix_tree_lookup(&table->tree, cqn);
  	if (likely(cq))
  		mlx5_cq_hold(cq);
-	spin_unlock(&table->lock);
+	rcu_read_unlock();

Thanks for you patch.

Quoted 4 lines

I think we can improve it further, by taking the if statement out of the
critical section.

No, mlx5_cq_hold must stay under RCU read, otherwise cq might get freed before the irq gets a change to increment ref count on it.

another way to do it is not to do any refcounting in the irq handler and fence cq removal via synchronize_irq(eq->irqn) on mlx5_eq_del_cq. But let's keep one approach (refcounting), synchronize_irq/rcu can be heavy sometimes especially on RDMA workloads with many create/destroy cq in loops.

Quoted 36 lines

Other than that, patch LGTM.

Regards,
Tariq

Quoted 31 lines
drivers/net/ethernet/mellanox/mlx5/core/eq.c +2 -2
  	return cq;
  }
@@ -371,9 +371,9 @@ int mlx5_eq_add_cq(struct mlx5_eq *eq, struct
 mlx5_core_cq *cq)
  	struct mlx5_cq_table *table = &eq->cq_table;
  	int err;
 
-	spin_lock_irq(&table->lock);
+	spin_lock(&table->lock);
  	err = radix_tree_insert(&table->tree, cq->cqn, cq);
-	spin_unlock_irq(&table->lock);
+	spin_unlock(&table->lock);
 
  	return err;
  }
drivers/net/ethernet/mellanox/mlx5/core/eq.c +2 -2
@@ -383,9 +383,9 @@ int mlx5_eq_del_cq(struct mlx5_eq *eq, struct
 mlx5_core_cq *cq)
  	struct mlx5_cq_table *table = &eq->cq_table;
  	struct mlx5_core_cq *tmp;
 
-	spin_lock_irq(&table->lock);
+	spin_lock(&table->lock);
  	tmp = radix_tree_delete(&table->tree, cq->cqn);
-	spin_unlock_irq(&table->lock);
+	spin_unlock(&table->lock);
 
  	if (!tmp) {

mlx5_core_warn(eq->dev, "cq 0x%x not found in eq 0x%x
tree\n", eq->eqn, cq->cqn);

author
#3 lore raw
Quoted 7 lines · Saeed Mahameed

Hi Cong,

The patch is ok to me, but i really doubt that you can hit a contention on latest upstream driver, since we already have spinlock per EQ, which means spinlock per core, each EQ (core) msix handler can only access one spinlock (its own), so I am surprised how you got the contention, Maybe you are not running on latest upstream driver ?

We are running 4.14 stable release. Which commit changes the game here? We can consider to backport it unless it is complicated.

Also, if you don't like this patch, we are happy to carry it for our own, sometimes it isn't worth the time to push into upstream.

Quoted 3 lines

what is the workload ?

It's a memcached RPC performance test, that is all I can tell. (Apparently I have almost zero knowledge about memcached.)

Quoted 38 lines
Quoted 38 lines
Quoted 25 lines

In fact, radix_tree_lookup() is perfectly fine with RCU read lock, we don't have to take a spinlock on this hot path. It is pretty much
similar to commit 291c566a2891
("net/mlx4_core: Fix racy CQ (Completion Queue) free"). Slow paths are still serialized with the spinlock, and with synchronize_irq() it should be safe to just move the fast path to RCU read lock.

This patch itself reduces the latency by about 50% with our workload.

Cc Saeed Mahameed <[email protected]>
Cc Tariq Toukan <[email protected]>
---
  drivers/net/ethernet/mellanox/mlx5/core/eq.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/eq.c +0 -0
index ee04aab65a9f..7092457705a2 100644
@@ -114,11 +114,11 @@ static struct mlx5_core_cq
 *mlx5_eq_cq_get(struct mlx5_eq *eq, u32 cqn)

drivers/net/ethernet/mellanox/mlx5/core/eq.c static struct mlx5_core_cq · line 122

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/eq.c +2 -2
    struct mlx5_cq_table *table = &eq->cq_table;
    struct mlx5_core_cq *cq = NULL;
 
-   spin_lock(&table->lock);
+   rcu_read_lock();
    cq = radix_tree_lookup(&table->tree, cqn);
    if (likely(cq))
            mlx5_cq_hold(cq);
-   spin_unlock(&table->lock);
+   rcu_read_unlock();

Thanks for you patch.

Quoted 8 lines
Quoted 4 lines

I think we can improve it further, by taking the if statement out of the
critical section.

No, mlx5_cq_hold must stay under RCU read, otherwise cq might get freed before the irq gets a change to increment ref count on it.

Agreed.

Thanks.

#6 lore raw
In reply to Saeed Mahameed
Quoted 12 lines · Saeed Mahameed
Quoted 7 lines · Tariq Toukan
Quoted 5 lines · Cong Wang

mlx5_eq_cq_get() is called in IRQ handler, the spinlock inside gets a lot of contentions when we test some heavy workload with 60 RX queues and 80 CPU's, and it is clearly shown in the flame graph.

Hi Cong,

Quoted 7 lines

The patch is ok to me, but i really doubt that you can hit a contention on latest upstream driver, since we already have spinlock per EQ, which means spinlock per core, each EQ (core) msix handler can only access one spinlock (its own), so I am surprised how you got the contention, Maybe you are not running on latest upstream driver ?

what is the workload ?

Surprisingly (or not), atomic operations, even on _not_ contended cache lines can stall the cpu enough for perf tools to notice...

If the atomic operation can be trivially replaced by RCU, then do it by any mean.

#4 lore raw
In reply to Cong Wang
Quoted 14 lines · Cong Wang
Quoted 11 lines · Saeed Mahameed

Hi Cong,

The patch is ok to me, but i really doubt that you can hit a contention
on latest upstream driver, since we already have spinlock per EQ, which
means spinlock per core, each EQ (core) msix handler can only access
one spinlock (its own), so I am surprised how you got the contention,
Maybe you are not running on latest upstream driver ?

Quoted 3 lines

We are running 4.14 stable release. Which commit changes the game here? We can consider to backport it unless it is complicated.

Ok, so there is no issue upstream, you are just missing the following patch:

commit 02d92f7903647119e125b24f5470f96cee0d4b4b

Author: Saeed Mahameed <[email protected]>
Date:   Fri Jan 19 16:13:01 2018 -0800

net/mlx5: CQ Database per EQ

Before this patch the driver had one CQ database protected via one spinlock, this spinlock is meant to synchronize between CQ adding/removing and CQ IRQ interrupt handling.
[...]

Quoted 3 lines

Also, if you don't like this patch, we are happy to carry it for our own,
sometimes it isn't worth the time to push into upstream.

I Do like it and it always worth it to push upstream, we all get to learn cool new stuff.

Quoted 5 lines
Quoted 3 lines

what is the workload ?

It's a memcached RPC performance test, that is all I can tell.

cool, thanks, so the missing commit should fix your issue.

Quoted 51 lines

(Apparently I have almost zero knowledge about memcached.)

Quoted 48 lines
Quoted 47 lines
Quoted 29 lines

In fact, radix_tree_lookup() is perfectly fine with RCU read lock,
we don't have to take a spinlock on this hot path. It is pretty much
similar to commit 291c566a2891
("net/mlx4_core: Fix racy CQ (Completion Queue) free"). Slow paths
are still serialized with the spinlock, and with synchronize_irq()
it should be safe to just move the fast path to RCU read lock.

This patch itself reduces the latency by about 50% with our workload.

Cc Saeed Mahameed <[email protected]>
Cc Tariq Toukan <[email protected]>
---
  drivers/net/ethernet/mellanox/mlx5/core/eq.c | 12 ++++++-----
(quoted diff) +0 -1
-
  1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/eq.c +0 -0
index ee04aab65a9f..7092457705a2 100644
@@ -114,11 +114,11 @@ static struct mlx5_core_cq
 *mlx5_eq_cq_get(struct mlx5_eq *eq, u32 cqn)

drivers/net/ethernet/mellanox/mlx5/core/eq.c static struct mlx5_core_cq · line 122

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/eq.c +2 -2
    struct mlx5_cq_table *table = &eq->cq_table;
    struct mlx5_core_cq *cq = NULL;
 
-   spin_lock(&table->lock);
+   rcu_read_lock();
    cq = radix_tree_lookup(&table->tree, cqn);
    if (likely(cq))
            mlx5_cq_hold(cq);
-   spin_unlock(&table->lock);
+   rcu_read_unlock();

Thanks for you patch.

I think we can improve it further, by taking the if statement out of
the
critical section.

Quoted 7 lines
Quoted 4 lines

No, mlx5_cq_hold must stay under RCU read, otherwise cq might get freed
before the irq gets a change to increment ref count on it.

Agreed.

Cool, I will ack the patch..

Quoted 2 lines

Thanks.

Acked-by on v1 1/1
#8 lore raw
In reply to Cong Wang
Quoted 12 lines · Cong Wang

mlx5_eq_cq_get() is called in IRQ handler, the spinlock inside gets a lot of contentions when we test some heavy workload with 60 RX queues and 80 CPU's, and it is clearly shown in the flame graph.

In fact, radix_tree_lookup() is perfectly fine with RCU read lock, we don't have to take a spinlock on this hot path. It is pretty much similar to commit 291c566a2891
("net/mlx4_core: Fix racy CQ (Completion Queue) free"). Slow paths are still serialized with the spinlock, and with synchronize_irq() it should be safe to just move the fast path to RCU read lock.

Quoted 5 lines

This patch itself reduces the latency by about 50% with our workload.

Cc Saeed Mahameed <[email protected]>
Cc Tariq Toukan <[email protected]>
#7 lore raw
In reply to Eric Dumazet
Quoted 22 lines · Eric Dumazet
Quoted 20 lines · Saeed Mahameed
Quoted 6 lines · Tariq Toukan
Quoted 5 lines · Cong Wang

mlx5_eq_cq_get() is called in IRQ handler, the spinlock inside gets a lot of contentions when we test some heavy workload with 60 RX queues and 80 CPU's, and it is clearly shown in the flame graph.

Hi Cong,

The patch is ok to me, but i really doubt that you can hit a contention
on latest upstream driver, since we already have spinlock per EQ, which
means spinlock per core, each EQ (core) msix handler can only access
one spinlock (its own), so I am surprised how you got the contention,
Maybe you are not running on latest upstream driver ?

Quoted 10 lines
Quoted 1 line

what is the workload ?

Surprisingly (or not), atomic operations, even on _not_ contended cache lines can
stall the cpu enough for perf tools to notice...

If the atomic operation can be trivially replaced by RCU, then do it by any mean.

Totally agree, Thanks Eric.

#9 lore raw
In reply to Saeed Mahameed
Quoted 14 lines · Saeed Mahameed
Quoted 13 lines · Cong Wang

mlx5_eq_cq_get() is called in IRQ handler, the spinlock inside gets a lot of contentions when we test some heavy workload with 60 RX queues and 80 CPU's, and it is clearly shown in the flame graph.

In fact, radix_tree_lookup() is perfectly fine with RCU read lock, we don't have to take a spinlock on this hot path. It is pretty much
similar to commit 291c566a2891
("net/mlx4_core: Fix racy CQ (Completion Queue) free"). Slow paths are still serialized with the spinlock, and with synchronize_irq() it should be safe to just move the fast path to RCU read lock.

Quoted 9 lines
Quoted 6 lines

This patch itself reduces the latency by about 50% with our workload.

Cc Saeed Mahameed <[email protected]>
Cc Tariq Toukan <[email protected]>

Actually, the commit message needs some rework, since there is no contention upstream, Cong can you take care of this and post a V2 ?

Thanks,
Saeed.

author
#5 lore raw
In reply to Saeed Mahameed
Quoted 24 lines · Saeed Mahameed
Quoted 14 lines · Cong Wang
Quoted 11 lines · Saeed Mahameed

Hi Cong,

The patch is ok to me, but i really doubt that you can hit a contention
on latest upstream driver, since we already have spinlock per EQ, which
means spinlock per core, each EQ (core) msix handler can only access
one spinlock (its own), so I am surprised how you got the contention,
Maybe you are not running on latest upstream driver ?

Quoted 3 lines

We are running 4.14 stable release. Which commit changes the game here? We can consider to backport it unless it is complicated.

Ok, so there is no issue upstream, you are just missing the following patch:

commit 02d92f7903647119e125b24f5470f96cee0d4b4b

Quoted 8 lines
Author: Saeed Mahameed <[email protected]>
Date:   Fri Jan 19 16:13:01 2018 -0800

net/mlx5: CQ Database per EQ

Before this patch the driver had one CQ database protected via one spinlock, this spinlock is meant to synchronize between CQ adding/removing and CQ IRQ interrupt handling.

Thanks for pointing it out! I will evaluate if we should backport it to 4.14.

author
#10 lore raw
In reply to Saeed Mahameed
Quoted 23 lines · Saeed Mahameed
Quoted 21 lines · Saeed Mahameed
Quoted 19 lines · Cong Wang

mlx5_eq_cq_get() is called in IRQ handler, the spinlock inside gets a lot of contentions when we test some heavy workload with 60 RX queues and 80 CPU's, and it is clearly shown in the flame graph.

In fact, radix_tree_lookup() is perfectly fine with RCU read lock, we don't have to take a spinlock on this hot path. It is pretty much
similar to commit 291c566a2891
("net/mlx4_core: Fix racy CQ (Completion Queue) free"). Slow paths are still serialized with the spinlock, and with synchronize_irq() it should be safe to just move the fast path to RCU read lock.

This patch itself reduces the latency by about 50% with our workload.

Cc Saeed Mahameed <[email protected]>
Cc Tariq Toukan <[email protected]>
Quoted 5 lines
Quoted 2 lines

Actually, the commit message needs some rework, since there is no contention upstream, Cong can you take care of this and post a V2 ?

I can't verify if upstream has contention or not, but yeah, I can at least mention the commit 02d92f7903647119e125b24f547 in changelog.

Thanks.