Skip to contentSkip to conversation

refactor SMP barriers for net/mlx

PATCH In review Feifei Wang 4 participants 36 messages Mar 18, 2021 – May 27, 2021 Network drivers

lore
Share & tools
lore
#0 lore raw

cover refactor SMP barriers for net/mlx

For net/mlx4 and net/mlx5, fix cache rebuild bug and replace SMP barriers with atomic fence.

Feifei Wang (4):
net/mlx4: fix rebuild bug for Memory Region cache net/mlx4: replace SMP barrier with C11 barriers net/mlx5: fix rebuild bug for Memory Region cache net/mlx5: replace SMP barriers with C11 barriers

 drivers/net/mlx4/mlx4_mr.c | 21 +++++++++----------
 drivers/net/mlx5/mlx5_mr.c | 41 ++++++++++++++++++--------------------
 2 files changed, 28 insertions(+), 34 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 net/mlx4: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local cache.

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region lkey value.

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this process.

Fixes 9797bfcce1c9 ("net/mlx4: add new memory region support")
---
 drivers/net/mlx4/mlx4_mr.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
drivers/net/mlx4/mlx4_mr.c +8 -11
index 6b2f0cf18..cfd7d4a9c 100644
@@ -946,20 +946,17 @@ mlx4_mr_mem_event_free_cb(struct rte_eth_dev *dev, const void *addr, size_t len)
 		rebuild = 1;
 	}
 	if (rebuild) {
-		mr_rebuild_dev_cache(dev);
-		/*
-		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If one of
-		 * freed memsegs is seen by other core, that means the memseg
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other core
-		 * before the core sees the newly allocated memory.
-		 */
 		++priv->mr.dev_gen;
 		DEBUG("broadcasting local cache flush, gen=%d",
-		      priv->mr.dev_gen);
+			priv->mr.dev_gen);
+
+		/* Flush local caches by propagating invalidation across cores.
+		 * rte_smp_wmb is to keep the order that dev_gen updated before
+		 * rebuilding global cache. Therefore, other core can flush their
+		 * local cache on time.
+		 */
 		rte_smp_wmb();
+		mr_rebuild_dev_cache(dev);
 	}
 	rte_rwlock_write_unlock(&priv->mr.rwlock);
 #ifdef RTE_LIBRTE_MLX4_DEBUG
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/4 net/mlx4: replace SMP barrier with C11 barriers

In reply to Feifei Wang

Replace SMP barrier with atomic thread fence.

---
 drivers/net/mlx4/mlx4_mr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/mlx4/mlx4_mr.c +2 -2
index cfd7d4a9c..503e8a7bb 100644
@@ -951,11 +951,11 @@ mlx4_mr_mem_event_free_cb(struct rte_eth_dev *dev, const void *addr, size_t len)
 			priv->mr.dev_gen);
 
 		/* Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb is to keep the order that dev_gen updated before
+		 * release-fence is to keep the order that dev_gen updated before
 		 * rebuilding global cache. Therefore, other core can flush their
 		 * local cache on time.
 		 */
-		rte_smp_wmb();
+		rte_atomic_thread_fence(__ATOMIC_RELEASE);
 		mr_rebuild_dev_cache(dev);
 	}
 	rte_rwlock_write_unlock(&priv->mr.rwlock);
Signature · 1 line
2.25.1
#3 lore raw

PATCH 3/4 net/mlx5: fix rebuild bug for Memory Region cache

In reply to Feifei Wang

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local cache.

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region lkey value.

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this process.

Fixes 974f1e7ef146 ("net/mlx5: add new memory region support")
---
 drivers/net/mlx5/mlx5_mr.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
drivers/net/mlx5/mlx5_mr.c +17 -20
index da4e91fc2..7ce1d3e64 100644
@@ -103,20 +103,18 @@ mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush, gen=%d",
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If one of
-		 * freed memsegs is seen by other core, that means the memseg
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other core
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that dev_gen updated before
+		 * rebuilding global cache. Therefore, other core can flush their
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
-		DEBUG("broadcasting local cache flush, gen=%d",
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data->port_id,
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If one of
-	 * freed memsegs is seen by other core, that means the memseg
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other core
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen updated before
+	 * rebuilding global cache. Therefore, other core can flush their
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
Signature · 1 line
2.25.1
#4 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity handle) and the "global" cache for all MR registered on the device.

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps [a] and [b],
and local caches flush is requested earlier. What problem does it solve? It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions being freed. Hence, the lookups for these MRs in caches should not occur.

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch on acquiring global cache lock. So, I see some potential performance impact.

With best regards,
Slava

Quoted 113 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, March 18, 2021 9:19
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>; Yongseok Koh <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>
Subject: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local cache.

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region lkey value.

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this process.

Fixes 974f1e7ef146 ("net/mlx5: add new memory region support")
---
 drivers/net/mlx5/mlx5_mr.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
drivers/net/mlx5/mlx5_mr.c index renamed from drivers/net/mlx5/mlx5_mr.c +0 -0

da4e91fc2..7ce1d3e64 100644

drivers/net/mlx5/mlx5_mr.c +4 -2
@@ -103,20 +103,18 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush, gen=%d",
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means the

memseg

drivers/net/mlx5/mlx5_mr.c +0 -3
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that dev_gen

updated before

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * rebuilding global cache. Therefore, other core can flush

their

drivers/net/mlx5/mlx5_mr.c +8 -10
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
-		DEBUG("broadcasting local cache flush, gen=%d",
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct rte_pci_device *pdev,
 void *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data->port_id,
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If one of
-	 * freed memsegs is seen by other core, that means the memseg
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other core
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen updated

before

drivers/net/mlx5/mlx5_mr.c +3 -4
+	 * rebuilding global cache. Therefore, other core can flush their
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
--

2.25.1

#5 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your attention.

Best Regards
Feifei

Quoted 10 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月12日 16:28
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; [email protected]
抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 19 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity handle) and the "global" cache for all MR registered on the device.

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

Quoted 6 lines

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it solve?
It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions being freed. Hence, the lookups for these MRs in caches should not occur.

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug cannot happen.

Quoted 5 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch on acquiring global cache lock. So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and performance.

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed. In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

However, this mechanism just ensures the performance unchanged for the first few mbufs. During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of time.

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually come back. Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of emergencies.

Best Regards
Feifei

Quoted 120 lines

With best regards,
Slava

Quoted 23 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, March 18, 2021 9:19
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>; Yongseok Koh <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>
Subject: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local cache.

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

Quoted 7 lines

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region lkey value.

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this

process.

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device *pdev,

Quoted 52 lines
Fixes 974f1e7ef146 ("net/mlx5: add new memory region support")
---
 drivers/net/mlx5/mlx5_mr.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
drivers/net/mlx5/mlx5_mr.c +0 -0

index
da4e91fc2..7ce1d3e64 100644

drivers/net/mlx5/mlx5_mr.c +4 -2
@@ -103,20 +103,18 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush, gen=%d",
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means the

memseg

drivers/net/mlx5/mlx5_mr.c +0 -3
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that dev_gen

updated before

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * rebuilding global cache. Therefore, other core can flush

their

drivers/net/mlx5/mlx5_mr.c +2 -3
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
-		DEBUG("broadcasting local cache flush, gen=%d",
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct rte_pci_device *pdev,

void

Quoted 33 lines
drivers/net/mlx5/mlx5_mr.c +6 -7
 *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data->port_id,
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If one of
-	 * freed memsegs is seen by other core, that means the memseg
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other core
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen updated

before

drivers/net/mlx5/mlx5_mr.c +3 -4
+	 * rebuilding global cache. Therefore, other core can flush their
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
--

2.25.1

#6 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 34 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 19 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity handle) and the "global" cache for all MR registered on the device.

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

Quoted 6 lines

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it solve? It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions being freed. Hence, the lookups for these MRs in caches should not occur.

Quoted 7 lines

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug cannot happen.

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to free. ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created there.

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being
sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in use. Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any mbufs in flight referencing to the addresses
being freed. No translation to MR being invalidated can happen.

Quoted 17 lines
Quoted 5 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and performance.

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed.
In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

Quoted 6 lines

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of time.

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local cache. And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked
on lock taken for cache update since point b) till point d).

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since c) till d) - quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 7 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually come back. Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of emergencies.

If we have some mbufs still allocated in memory being freed - there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong approaches.

With best regards,
Slava

Quoted 128 lines

Best Regards
Feifei

Quoted 19 lines

With best regards,
Slava

Quoted 16 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, March 18, 2021 9:19
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>; Yongseok Koh <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>
Subject: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local

cache.

Quoted 61 lines
Quoted 7 lines

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

Quoted 8 lines

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region lkey value.

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this

process.

Quoted 45 lines
Fixes 974f1e7ef146 ("net/mlx5: add new memory region support")
---
 drivers/net/mlx5/mlx5_mr.c | 37
+++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
drivers/net/mlx5/mlx5_mr.c +0 -0

index
da4e91fc2..7ce1d3e64 100644

drivers/net/mlx5/mlx5_mr.c +4 -2
@@ -103,20 +103,18 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush, gen=%d",
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means the

memseg

drivers/net/mlx5/mlx5_mr.c +0 -3
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that dev_gen

updated before

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * rebuilding global cache. Therefore, other core can flush

their

drivers/net/mlx5/mlx5_mr.c +1 -1
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device

Quoted 8 lines
drivers/net/mlx5/mlx5_mr.c +1 -2
-		DEBUG("broadcasting local cache flush, gen=%d",
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct rte_pci_device

*pdev,

Quoted 34 lines

void

Quoted 33 lines
drivers/net/mlx5/mlx5_mr.c +6 -7
 *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data->port_id,
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If one of
-	 * freed memsegs is seen by other core, that means the memseg
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other core
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen updated

before

drivers/net/mlx5/mlx5_mr.c +3 -4
+	 * rebuilding global cache. Therefore, other core can flush their
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
--

2.25.1

#7 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with this.

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 288 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 35 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 20 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity
handle) and the "global" cache for all MR registered on the device.

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

Quoted 6 lines

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it solve? It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions being freed. Hence, the lookups for these MRs in caches should not occur.

Quoted 7 lines

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug cannot happen.

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to free. ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created there.

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in use. Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any mbufs in flight referencing to the addresses being freed. No translation to MR being invalidated can happen.

Quoted 11 lines
Quoted 6 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 7 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed.
In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

Quoted 6 lines

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of time.

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local cache. And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point d).

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since c) till d) - quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 7 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually come back. Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

emergencies.

If we have some mbufs still allocated in memory being freed - there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong approaches.

With best regards,
Slava

Quoted 14 lines

Best Regards
Feifei

Quoted 10 lines

With best regards,
Slava

Quoted 7 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, March 18, 2021 9:19
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>; Yongseok Koh <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang

<[email protected]>;

Quoted 115 lines
Quoted 10 lines
Quoted 10 lines

[email protected]; Ruifeng Wang <[email protected]>
Subject: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local

cache.

Quoted 61 lines
Quoted 7 lines

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

Quoted 8 lines

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region lkey value.

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this

process.

Quoted 45 lines
Fixes 974f1e7ef146 ("net/mlx5: add new memory region support")
---
 drivers/net/mlx5/mlx5_mr.c | 37
+++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
(quoted diff) +0 -0

b/drivers/net/mlx5/mlx5_mr.c index
da4e91fc2..7ce1d3e64 100644

drivers/net/mlx5/mlx5_mr.c +4 -2
@@ -103,20 +103,18 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush, gen=%d",
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means the

memseg

drivers/net/mlx5/mlx5_mr.c +0 -3
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that dev_gen

updated before

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * rebuilding global cache. Therefore, other core can flush

their

drivers/net/mlx5/mlx5_mr.c +1 -1
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device

Quoted 8 lines
drivers/net/mlx5/mlx5_mr.c +1 -2
-		DEBUG("broadcasting local cache flush, gen=%d",
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct rte_pci_device

*pdev,

Quoted 34 lines

void

Quoted 33 lines
drivers/net/mlx5/mlx5_mr.c +6 -7
 *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data->port_id,
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If one of
-	 * freed memsegs is seen by other core, that means the memseg
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other core
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen updated

before

drivers/net/mlx5/mlx5_mr.c +3 -4
+	 * rebuilding global cache. Therefore, other core can flush their
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
--

2.25.1

#8 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order cannot be kept.

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei

Quoted 331 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]> 主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with this.

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 10 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 48 lines

Hi, Feifei

Please, see below

....

Quoted 42 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 20 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity
handle) and the "global" cache for all MR registered on the device.

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

Quoted 6 lines

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it solve? It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions being freed. Hence, the lookups for these MRs in caches should not occur.

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

cannot happen.

Quoted 15 lines

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to free. ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created there.

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 7 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in use. Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 71 lines

No translation to MR being invalidated can happen.

Quoted 11 lines
Quoted 6 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 7 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed.
In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

Quoted 6 lines

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of time.

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local cache. And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point d).

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since c) till d) - quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 8 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually come back. Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

Quoted 6 lines

emergencies.

If we have some mbufs still allocated in memory being freed - there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 67 lines

With best regards,
Slava

Quoted 14 lines

Best Regards
Feifei

Quoted 10 lines

With best regards,
Slava

Quoted 7 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, March 18, 2021 9:19
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>; Yongseok Koh <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang

<[email protected]>;

Quoted 48 lines
Quoted 10 lines
Quoted 10 lines

[email protected]; Ruifeng Wang <[email protected]>
Subject: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local

cache.

Quoted 37 lines
Quoted 7 lines

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

Quoted 8 lines

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region lkey value.

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this

process.

Quoted 21 lines
Fixes 974f1e7ef146 ("net/mlx5: add new memory region support")
---
 drivers/net/mlx5/mlx5_mr.c | 37
+++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
(quoted diff) +0 -0

b/drivers/net/mlx5/mlx5_mr.c index
da4e91fc2..7ce1d3e64 100644

drivers/net/mlx5/mlx5_mr.c +0 -0
@@ -103,20 +103,18 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh,

Quoted 8 lines
drivers/net/mlx5/mlx5_mr.c +4 -1
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush, gen=%d",
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation

across cores.

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct · line 110

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * rte_smp_wmb() is enough to synchronize this

event. If

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means

the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

memseg

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * has been allocated by allocator, which will come

after this

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * free call. Therefore, this store instruction

(incrementing

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * generation below) will be guaranteed to be seen

by other

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 5 lines

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that dev_gen

updated before

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * rebuilding global cache. Therefore, other core can

flush

Quoted 18 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

their

drivers/net/mlx5/mlx5_mr.c +1 -2
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
-		DEBUG("broadcasting local cache flush, gen=%d",
Quoted 12 lines
Quoted 7 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device

Quoted 7 lines
drivers/net/mlx5/mlx5_mr.c +1 -1
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct rte_pci_device

*pdev,

Quoted 4 lines

void

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -0
 *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data-

port_id,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr,

Quoted 10 lines
drivers/net/mlx5/mlx5_mr.c +5 -2
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If

one of

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device · line 415

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-	 * freed memsegs is seen by other core, that means the

memseg

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh,

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -3
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other

core

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device · line 416

Quoted 2 lines
drivers/net/mlx5/mlx5_mr.c +1 -1
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen

updated

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

before

drivers/net/mlx5/mlx5_mr.c +1 -0
+	 * rebuilding global cache. Therefore, other core can flush

their

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drivers/net/mlx5/mlx5_mr.c +2 -4
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
--

2.25.1

#9 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to ensure the correct order
- after altering dev_gen to make this change visible for other agents and to trigger local cache update

With best regards,
Slava

Quoted 364 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather than after it.
Otherwise, in the out-of-order platforms, current order cannot be kept.

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei

Quoted 17 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]> 主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with this.

Quoted 13 lines

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 48 lines
Quoted 48 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 38 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 20 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity
handle) and the "global" cache for all MR registered on the device.

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

Quoted 6 lines

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it solve? It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions being freed. Hence, the lookups for these MRs in caches should not occur.

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Quoted 11 lines
Quoted 5 lines
Quoted 5 lines

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

cannot happen.

Quoted 5 lines

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to free. ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 10 lines
Quoted 10 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 36 lines
Quoted 7 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in use. Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 28 lines

No translation to MR being invalidated can happen.

Quoted 6 lines
Quoted 6 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

Quoted 6 lines

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 13 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed.
In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of

time.

Quoted 44 lines
Quoted 44 lines

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local cache. And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point d).

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since c) till d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 8 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually come back. Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

Quoted 48 lines
Quoted 7 lines

emergencies.

If we have some mbufs still allocated in memory being freed - there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 40 lines

With best regards,
Slava

Quoted 14 lines

Best Regards
Feifei

Quoted 10 lines

With best regards,
Slava

Quoted 7 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, March 18, 2021 9:19
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>; Yongseok Koh <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang

<[email protected]>;

Quoted 21 lines
Quoted 10 lines
Quoted 10 lines

[email protected]; Ruifeng Wang <[email protected]>
Subject: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local

cache.

Quoted 10 lines
Quoted 10 lines

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region

lkey value.

Quoted 108 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 5 lines

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this

process.

Quoted 22 lines
Fixes 974f1e7ef146 ("net/mlx5: add new memory region

support")
Cc: [email protected]

---
 drivers/net/mlx5/mlx5_mr.c | 37
+++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
(quoted diff) +0 -0

b/drivers/net/mlx5/mlx5_mr.c index
da4e91fc2..7ce1d3e64 100644

drivers/net/mlx5/mlx5_mr.c +0 -0
@@ -103,20 +103,18 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh,

Quoted 8 lines
drivers/net/mlx5/mlx5_mr.c +4 -1
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush, gen=%d",
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation

across cores.

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct · line 110

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * rte_smp_wmb() is enough to synchronize this

event. If

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means

the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

memseg

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * has been allocated by allocator, which will come

after this

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * free call. Therefore, this store instruction

(incrementing

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * generation below) will be guaranteed to be seen

by other

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 5 lines

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that dev_gen

updated before

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * rebuilding global cache. Therefore, other core can

flush

Quoted 18 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

their

drivers/net/mlx5/mlx5_mr.c +1 -2
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
-		DEBUG("broadcasting local cache flush, gen=%d",
Quoted 12 lines
Quoted 7 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device

Quoted 7 lines
drivers/net/mlx5/mlx5_mr.c +1 -1
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct rte_pci_device

*pdev,

Quoted 4 lines

void

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -0
 *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data-

port_id,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr,

Quoted 10 lines
drivers/net/mlx5/mlx5_mr.c +5 -2
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If

one of

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device · line 415

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-	 * freed memsegs is seen by other core, that means the

memseg

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh,

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -3
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other

core

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device · line 416

Quoted 2 lines
drivers/net/mlx5/mlx5_mr.c +1 -1
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen

updated

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

before

drivers/net/mlx5/mlx5_mr.c +1 -0
+	 * rebuilding global cache. Therefore, other core can flush

their

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drivers/net/mlx5/mlx5_mr.c +2 -4
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
--

2.25.1

#10 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and write. and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release, have been done.

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated. 2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be flushed.

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible". The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure this.

Best Regards
Feifei

Quoted 395 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to ensure the correct
order
- after altering dev_gen to make this change visible for other agents and to
trigger local cache update

With best regards,
Slava

Quoted 18 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order cannot be kept.

Quoted 13 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 18 lines
Quoted 18 lines

<[email protected]>; nd <[email protected]>; nd <[email protected]> 主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with this.

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 50 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 43 lines
Quoted 43 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 33 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 25 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity
handle) and the "global" cache for all MR registered on the device.

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it solve? It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions

being freed.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 1 line

Hence, the lookups for these MRs in caches should not occur.

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

Quoted 4 lines
Quoted 4 lines

cannot happen.

Quoted 3 lines

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to

free.

Quoted 45 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 41 lines
Quoted 10 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 8 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in use. Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 22 lines

No translation to MR being invalidated can happen.

Quoted 6 lines
Quoted 6 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

Quoted 6 lines

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 7 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed.
In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

Quoted 50 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of

time.

Quoted 42 lines
Quoted 42 lines

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local cache. And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point d).

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 5 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually

come back.

Quoted 162 lines
Quoted 52 lines
Quoted 10 lines
Quoted 3 lines

Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

emergencies.

If we have some mbufs still allocated in memory being freed - there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 41 lines

With best regards,
Slava

Quoted 14 lines

Best Regards
Feifei

Quoted 10 lines

With best regards,
Slava

Quoted 7 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, March 18, 2021 9:19
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>; Yongseok Koh <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang

<[email protected]>;

Quoted 22 lines
Quoted 10 lines
Quoted 10 lines

[email protected]; Ruifeng Wang <[email protected]>
Subject: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local

cache.

Quoted 11 lines
Quoted 11 lines

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region

lkey value.

Quoted 109 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 5 lines

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this

process.

Quoted 22 lines
Fixes 974f1e7ef146 ("net/mlx5: add new memory region

support")
Cc: [email protected]

---
 drivers/net/mlx5/mlx5_mr.c | 37
+++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
(quoted diff) +0 -0

b/drivers/net/mlx5/mlx5_mr.c index
da4e91fc2..7ce1d3e64 100644

drivers/net/mlx5/mlx5_mr.c +0 -0
@@ -103,20 +103,18 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct · line 112

Quoted 8 lines
drivers/net/mlx5/mlx5_mr.c +4 -1
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush, gen=%d",
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation

across cores.

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct · line 110

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * rte_smp_wmb() is enough to synchronize this

event. If

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means

the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

memseg

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * has been allocated by allocator, which will come

after this

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * free call. Therefore, this store instruction

(incrementing

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * generation below) will be guaranteed to be seen

by other

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 5 lines

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that dev_gen

updated before

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * rebuilding global cache. Therefore, other core can

flush

Quoted 18 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

their

drivers/net/mlx5/mlx5_mr.c +1 -2
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
-		DEBUG("broadcasting local cache flush, gen=%d",
Quoted 12 lines
Quoted 7 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device

Quoted 7 lines
drivers/net/mlx5/mlx5_mr.c +1 -1
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct rte_pci_device

*pdev,

Quoted 4 lines

void

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -0
 *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data-

port_id,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device · line 414

Quoted 10 lines
drivers/net/mlx5/mlx5_mr.c +5 -2
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If

one of

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device · line 415

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-	 * freed memsegs is seen by other core, that means the

memseg

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device · line 418

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -3
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other

core

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device · line 416

Quoted 2 lines
drivers/net/mlx5/mlx5_mr.c +1 -1
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen

updated

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 3 lines

before

drivers/net/mlx5/mlx5_mr.c +2 -0
+	 * rebuilding global cache. Therefore, other core can
+flush

their

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drivers/net/mlx5/mlx5_mr.c +2 -4
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
--

2.25.1

#11 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch?
For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Best Regards
Feifei

Quoted 439 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and write. and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release, have been done.

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be flushed.

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible".
The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure this.

Best Regards
Feifei

Quoted 47 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to ensure the
correct order
- after altering dev_gen to make this change visible for other agents
and to trigger local cache update

With best regards,
Slava

Quoted 25 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order cannot be kept.

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 2 lines
Quoted 2 lines

<[email protected]>; nd <[email protected]>; nd <[email protected]> 主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 78 lines
Quoted 16 lines
Quoted 16 lines

cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with this.

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 51 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 44 lines
Quoted 44 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 34 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 26 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity
handle) and the "global" cache for all MR registered on the device.

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it solve? It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions

being freed.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 1 line

Hence, the lookups for these MRs in caches should not occur.

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

Quoted 62 lines
Quoted 4 lines
Quoted 4 lines

cannot happen.

Quoted 3 lines

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to

free.

Quoted 57 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 18 lines
Quoted 10 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 8 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in use. Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

Quoted 30 lines

mbufs in flight referencing to the addresses being freed.

Quoted 29 lines

No translation to MR being invalidated can happen.

Quoted 6 lines
Quoted 6 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

Quoted 6 lines

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 14 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed. In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of

time.

Quoted 4 lines
Quoted 4 lines

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local

cache.

Quoted 150 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point d).

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually

come back.

Quoted 109 lines
Quoted 53 lines
Quoted 10 lines
Quoted 3 lines

Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

emergencies.

If we have some mbufs still allocated in memory being freed - there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 42 lines

With best regards,
Slava

Quoted 15 lines

Best Regards
Feifei

Quoted 11 lines

With best regards,
Slava

Quoted 8 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, March 18, 2021 9:19
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>; Yongseok Koh <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang

<[email protected]>;

Quoted 22 lines
Quoted 10 lines
Quoted 10 lines

[email protected]; Ruifeng Wang <[email protected]>
Subject: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local

cache.

Quoted 11 lines
Quoted 11 lines

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region

lkey value.

Quoted 55 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 5 lines

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this

process.

Quoted 22 lines
Fixes 974f1e7ef146 ("net/mlx5: add new memory region

support")
Cc: [email protected]

---
 drivers/net/mlx5/mlx5_mr.c | 37
+++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
(quoted diff) +0 -0

b/drivers/net/mlx5/mlx5_mr.c index
da4e91fc2..7ce1d3e64 100644

drivers/net/mlx5/mlx5_mr.c +0 -0
@@ -103,20 +103,18 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct · line 112

Quoted 8 lines
drivers/net/mlx5/mlx5_mr.c +4 -1
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush, gen=%d",
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation

across cores.

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct · line 110

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * rte_smp_wmb() is enough to synchronize this

event. If

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means

the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

memseg

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * has been allocated by allocator, which will come

after this

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * free call. Therefore, this store instruction

(incrementing

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * generation below) will be guaranteed to be seen

by other

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 5 lines

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that dev_gen

updated before

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * rebuilding global cache. Therefore, other core can

flush

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

their

drivers/net/mlx5/mlx5_mr.c +1 -1
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct

Quoted 8 lines
drivers/net/mlx5/mlx5_mr.c +1 -2
-		DEBUG("broadcasting local cache flush, gen=%d",
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct

rte_pci_device

Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
Quoted 6 lines
Quoted 5 lines

*pdev,

Quoted 4 lines

void

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -0
 *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data-

port_id,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct · line 414

Quoted 10 lines
drivers/net/mlx5/mlx5_mr.c +5 -2
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If

one of

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct · line 415

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-	 * freed memsegs is seen by other core, that means the

memseg

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct · line 418

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -3
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other

core

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct · line 416

Quoted 2 lines
drivers/net/mlx5/mlx5_mr.c +1 -1
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen

updated

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 3 lines

before

drivers/net/mlx5/mlx5_mr.c +2 -0
+	 * rebuilding global cache. Therefore, other core can
+flush

their

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drivers/net/mlx5/mlx5_mr.c +2 -4
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
--

2.25.1

#12 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb. We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to ensure this.

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

Also, regarding this:

Quoted 4 lines
Quoted 4 lines

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order cannot be kept.

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling, could you, please, elaborate? I'm not sure we should maintain an order (due to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

With best regards,
Slava

Quoted 479 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch?
For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Best Regards
Feifei

Quoted 10 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 8 lines

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and write. and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 10 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering
dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be flushed.

Quoted 5 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible".
The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 10 lines

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 16 lines
Quoted 16 lines

<[email protected]>; nd <[email protected]>; nd <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to ensure the
correct order
- after altering dev_gen to make this change visible for other
agents and to trigger local cache update

With best regards,
Slava

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd <[email protected]>;

nd

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order cannot be kept.

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Quoted 26 lines
Quoted 3 lines

Wang

Quoted 2 lines
Quoted 2 lines

<[email protected]>; nd <[email protected]>; nd <[email protected]> 主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with this.

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

Wang

Quoted 15 lines
Quoted 15 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 5 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 5 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity
handle) and the "global" cache for all MR registered on the

device.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it

solve?

Quoted 36 lines
Quoted 36 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions

being freed.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 1 line

Hence, the lookups for these MRs in caches should not occur.

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

Quoted 5 lines
Quoted 5 lines

cannot happen.

Quoted 4 lines

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to

free.

Quoted 15 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 11 lines
Quoted 11 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in

use.

Quoted 154 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 2 lines

Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 30 lines

No translation to MR being invalidated can happen.

Quoted 7 lines
Quoted 7 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

Quoted 6 lines

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 14 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed. In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of

Quoted 6 lines
Quoted 6 lines

time.

Quoted 5 lines
Quoted 5 lines

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local

cache.

Quoted 114 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point d).

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since
c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually

come back.

Quoted 73 lines
Quoted 53 lines
Quoted 10 lines
Quoted 3 lines

Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

emergencies.

If we have some mbufs still allocated in memory being freed - there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 42 lines

With best regards,
Slava

Quoted 15 lines

Best Regards
Feifei

Quoted 11 lines

With best regards,
Slava

Quoted 8 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, March 18, 2021 9:19
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>; Yongseok Koh <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang

<[email protected]>;

Quoted 22 lines
Quoted 10 lines
Quoted 10 lines

[email protected]; Ruifeng Wang <[email protected]>
Subject: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt.

However, if 'dev_gen' is updated after global cache is rebuilt, other cores may load a wrong memory region lkey value from old local

cache.

Quoted 11 lines
Quoted 11 lines

Timeslot main core worker core
1 rebuild global cache
2 load unchanged dev_gen
3 update dev_gen
4 look up old local cache

From the example above, we can see that though global cache is rebuilt, due to that dev_gen is not updated, the worker core may look up old cache table and receive a wrong memory region

lkey value.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 5 lines

To fix this, updating 'dev_gen' should be moved before rebuilding global cache to inform worker cores to flush their local cache when global cache start rebuilding. And wmb can ensure the sequence of this

process.

Quoted 13 lines
Fixes 974f1e7ef146 ("net/mlx5: add new memory region

support")
Cc: [email protected]

---
 drivers/net/mlx5/mlx5_mr.c | 37
+++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 6 lines
(quoted diff) +0 -0

b/drivers/net/mlx5/mlx5_mr.c index
da4e91fc2..7ce1d3e64 100644

drivers/net/mlx5/mlx5_mr.c +0 -0
@@ -103,20 +103,18 @@

mlx5_mr_mem_event_free_cb(struct

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/net/mlx5/mlx5_mr.c line 108

Quoted 7 lines
drivers/net/mlx5/mlx5_mr.c +2 -1
 mlx5_dev_ctx_shared *sh,
 		rebuild = 1;
 	}
 	if (rebuild) {
-		mlx5_mr_rebuild_cache(&sh->share_cache);
+		++sh->share_cache.dev_gen;
+		DEBUG("broadcasting local cache flush,

gen=%d",

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/net/mlx5/mlx5_mr.c line 112

Quoted 4 lines
drivers/net/mlx5/mlx5_mr.c +2 -0
+			sh->share_cache.dev_gen);
+
 		/*
 		 * Flush local caches by propagating invalidation

across cores.

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c line 110

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * rte_smp_wmb() is enough to synchronize this

event. If

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means

the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

memseg

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * has been allocated by allocator, which will come

after this

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * free call. Therefore, this store instruction

(incrementing

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * generation below) will be guaranteed to be seen

by other

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 3 lines

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * rte_smp_wmb() is to keep the order that

dev_gen

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines

updated before

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * rebuilding global cache. Therefore, other

core can

Quoted 56 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

flush

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

their

drivers/net/mlx5/mlx5_mr.c +1 -1
+		 * local cache on time.
 		 */
-		++sh->share_cache.dev_gen;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct

Quoted 8 lines
drivers/net/mlx5/mlx5_mr.c +1 -2
-		DEBUG("broadcasting local cache flush, gen=%d",
-		      sh->share_cache.dev_gen);
 		rte_smp_wmb();
+		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -407,20 +405,19 @@ mlx5_dma_unmap(struct

rte_pci_device

Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
Quoted 6 lines
Quoted 5 lines

*pdev,

Quoted 4 lines

void

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -0
 *addr,
 	mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
 	DEBUG("port %u remove MR(%p) from list", dev->data-

port_id,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct · line 414

Quoted 10 lines
drivers/net/mlx5/mlx5_mr.c +5 -2
 	      (void *)mr);
-	mlx5_mr_rebuild_cache(&sh->share_cache);
+
+	++sh->share_cache.dev_gen;
+	DEBUG("broadcasting local cache flush, gen=%d",
+		sh->share_cache.dev_gen);
+
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If

one of

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct · line 415

Quoted 1 line
drivers/net/mlx5/mlx5_mr.c +0 -1
-	 * freed memsegs is seen by other core, that means the

memseg

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct · line 418

Quoted 3 lines
drivers/net/mlx5/mlx5_mr.c +0 -3
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other

core

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct · line 416

Quoted 2 lines
drivers/net/mlx5/mlx5_mr.c +1 -1
-	 * before the core sees the newly allocated memory.
+	 * rte_smp_wmb() is to keep the order that dev_gen

updated

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 3 lines

before

drivers/net/mlx5/mlx5_mr.c +2 -0
+	 * rebuilding global cache. Therefore, other core can
+flush

their

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drivers/net/mlx5/mlx5_mr.c +2 -4
+	 * local cache on time.
 	 */
-	++sh->share_cache.dev_gen;
-	DEBUG("broadcasting local cache flush, gen=%d",
-	      sh->share_cache.dev_gen);
 	rte_smp_wmb();
+	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
--

2.25.1

#13 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your reply.

Quoted 9 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年5月6日 19:22
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 8 lines

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb.
We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to ensure this.

1. For the first problem why we should get rid of the last wmb and move it before dev_gen updated, I think our attention is how the wmb implements the synchronization between multiple agents.
Fig1
-----------------------------------------------------------------------------------------------------
Timeslot agent_1 agent_2
1 rebuild global cache
2 wmb
3 update dev_gen ----------------------- load changed dev_gen
4 rebuild local cache
-----------------------------------------------------------------------------------------------------

First, wmb is only for local thread to keep the order between local write-write :
Based on the picture above, for agent_1, wmb keeps the order that rebuilding global cache is always before updating dev_gen.

Second, agent_1 communicates with agent_2 by the global variable "dev_gen" :
If agent_1 updates dev_gen, agent_2 will load it and then it knows it should rebuild local cache

Finally, agent_2 rebuilds local cache according to whether agent_1 has rebuilt global cache, and agent_2 knows this information by the variable "dev_gen".
Fig2
-----------------------------------------------------------------------------------------------------
Timeslot agent_1 agent_2
1 update dev_gen
2 load changed dev_gen
3 rebuild local cache
4 rebuild global cache
5 wmb
-----------------------------------------------------------------------------------------------------

However, in arm platform, if wmb is after dev_gen updated, "dev_gen" may be updated before agent_1 rebuilding global cache, then agent_2 maybe receive error message and rebuild its local cache in advance.

To summarize, it is not important which time other agents can see the changed global variable "dev_gen".
(Actually, wmb after "dev_gen" cannot ensure changed "dev_gen" is committed to the global). It is more important that if other agents see the changed "dev_gen", they also can know global cache has been updated.

Quoted 3 lines

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

We just move the last wmb into the right place, and not delete it for performance.

Quoted 12 lines

Also, regarding this:

Quoted 1 line
Quoted 1 line

Another question suddenly occurred to me, in order to keep the > >

order that rebuilding global cache before updating ”dev_gen“, the > > wmb should be before updating "dev_gen" rather than after it.

Quoted 1 line
Quoted 1 line

Otherwise, in the out-of-order platforms, current order cannot be kept.

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

2. The second function of wmb before "dev_gen" updated is for performance according to our previous discussion.
According to Fig2, if there is no wmb between "global cache updated" and "dev_gen updated", "dev_gen" may update before global cache updated.

Then agent_2 may see the changed "dev_gen" and flush entire local cache in advance.

This entire flush can degrade the performance:
"the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in data path on acquiring global cache lock."

Furthermore, spinlock is just for global cache, not for dev_gen and local cache.

Quoted 7 lines

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling,
could you, please, elaborate? I'm not sure we should maintain an order (due
to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

At last, for my view, there are two functions that moving wmb before "dev_gen" for the write-write order:
--------------------------------
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen
--------------------------------
1. Achieve synchronization between multiple threads in the right way
2. Prevent other agents from flushing local cache early to ensure performance

Best Regards
Feifei

Quoted 347 lines

With best regards,
Slava

Quoted 16 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch?
For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Quoted 12 lines

Thanks very much for your attention.

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 8 lines
Quoted 8 lines

<[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and

write.

Quoted 27 lines
Quoted 5 lines

and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 10 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering
dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be flushed.

Quoted 6 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible".
The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 4 lines

Best Regards
Feifei

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd <[email protected]>;

nd

Quoted 21 lines
Quoted 15 lines
Quoted 15 lines

<[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to ensure
the correct order
- after altering dev_gen to make this change visible for other
agents and to trigger local cache update

With best regards,
Slava

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Quoted 13 lines

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd <[email protected]>;

nd

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order cannot be

kept.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 220 lines
Quoted 26 lines
Quoted 3 lines

Wang

Quoted 2 lines
Quoted 2 lines

<[email protected]>; nd <[email protected]>; nd <[email protected]> 主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with this.

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

Wang

Quoted 19 lines
Quoted 19 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 9 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity
handle) and the "global" cache for all MR registered on the

device.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

How cache update on memory freeing/unregistering happens:
- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it

solve?

Quoted 37 lines
Quoted 37 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions

being freed.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line

Hence, the lookups for these MRs in caches should not occur.

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

Quoted 5 lines
Quoted 5 lines

cannot happen.

Quoted 4 lines

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to

free.

Quoted 15 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 11 lines
Quoted 11 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in

use.

Quoted 103 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 2 lines

Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 30 lines

No translation to MR being invalidated can happen.

Quoted 7 lines
Quoted 7 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

Quoted 6 lines

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 14 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed. In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of

Quoted 6 lines
Quoted 6 lines

time.

Quoted 5 lines
Quoted 5 lines

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local

cache.

Quoted 63 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines

And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point d).

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since
c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually

come back.

Quoted 21 lines
Quoted 21 lines
Quoted 10 lines
Quoted 3 lines

Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

emergencies.

If we have some mbufs still allocated in memory being freed
- there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 10 lines

With best regards,
Slava

Quoted 6 lines

Best Regards
Feifei

Quoted 2 lines

With best regards,
Slava

#14 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

We should consider the locks in your scenario - it is crucial for the complete model description:

How agent_1 (in your terms) rebuilds global cache:

1a) lock()
1b) rebuild(global cache)
1c) update(dev_gen)
1d) wmb()
1e) unlock()

How agent_2 checks:

2a) check(dev_gen) (assume positive - changed)
2b) clear(local_cache)
2c) miss(on empty local_cache) - > eventually it goes to mr_lookup_caches() 2d) lock()
2e) get(new MR)
2f) unlock()
2g) update(local cache with obtained new MR)

Hence, even if 1c) becomes visible in 2a) before 1b) committed (say, due to out-of-order Arch) - the agent 2 would be blocked on 2d) and scenario depicted on your Fig2 would not happen (agent_2 will wait before step 3 till agent 1 unlocks after its step 5).

With best regards,
Slava

Quoted 500 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Friday, May 7, 2021 9:36> To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your reply.

Quoted 10 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年5月6日 19:22
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 8 lines

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb.
We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to ensure this.

1. For the first problem why we should get rid of the last wmb and move it before dev_gen updated, I think our attention is how the wmb implements the synchronization between multiple agents.
Fig1
----------------------------------------------------------------------------------------------
-------
Timeslot agent_1 agent_2
1 rebuild global cache
2 wmb
3 update dev_gen ----------------------- load changed
dev_gen
4 rebuild local cache
----------------------------------------------------------------------------------------------
-------

First, wmb is only for local thread to keep the order between local write- write :
Based on the picture above, for agent_1, wmb keeps the order that rebuilding global cache is always before updating dev_gen.

Second, agent_1 communicates with agent_2 by the global variable "dev_gen" :
If agent_1 updates dev_gen, agent_2 will load it and then it knows it should rebuild local cache

Finally, agent_2 rebuilds local cache according to whether agent_1 has rebuilt global cache, and agent_2 knows this information by the variable "dev_gen".
Fig2
----------------------------------------------------------------------------------------------
-------
Timeslot agent_1 agent_2
1 update dev_gen
2 load changed dev_gen
3 rebuild local cache
4 rebuild global cache
5 wmb
----------------------------------------------------------------------------------------------
-------

However, in arm platform, if wmb is after dev_gen updated, "dev_gen" may be updated before agent_1 rebuilding global cache, then agent_2 maybe receive error message and rebuild its local cache in advance.

To summarize, it is not important which time other agents can see the changed global variable "dev_gen".
(Actually, wmb after "dev_gen" cannot ensure changed "dev_gen" is committed to the global).
It is more important that if other agents see the changed "dev_gen", they also can know global cache has been updated.

Quoted 3 lines

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

We just move the last wmb into the right place, and not delete it for performance.

Quoted 7 lines

Also, regarding this:

Quoted 4 lines
Quoted 1 line

Another question suddenly occurred to me, in order to keep the >

order that rebuilding global cache before updating ”dev_gen“, the > wmb should be before updating "dev_gen" rather than after it.

Quoted 1 line

Otherwise, in the out-of-order platforms, current order cannot be

kept.

Quoted 5 lines

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

2. The second function of wmb before "dev_gen" updated is for performance according to our previous discussion.
According to Fig2, if there is no wmb between "global cache updated" and "dev_gen updated", "dev_gen" may update before global cache updated.

Then agent_2 may see the changed "dev_gen" and flush entire local cache in advance.

This entire flush can degrade the performance:
"the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in data path on acquiring global cache lock."

Furthermore, spinlock is just for global cache, not for dev_gen and local cache.

Quoted 8 lines

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling,
could you, please, elaborate? I'm not sure we should maintain an
order (due to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

At last, for my view, there are two functions that moving wmb before "dev_gen"
for the write-write order:
--------------------------------
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen
--------------------------------
1. Achieve synchronization between multiple threads in the right way 2. Prevent other agents from flushing local cache early to ensure performance

Best Regards
Feifei

Quoted 25 lines

With best regards,
Slava

Quoted 22 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch?
For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Best Regards
Feifei

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 2 lines
Quoted 2 lines

<[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 23 lines
Quoted 7 lines
Quoted 7 lines

cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and

write.

Quoted 15 lines
Quoted 5 lines

and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 9 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering
dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be

flushed.

Quoted 18 lines
Quoted 18 lines
Quoted 7 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible".
The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 10 lines

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Quoted 26 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd <[email protected]>;

nd

Quoted 23 lines
Quoted 15 lines
Quoted 15 lines

<[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to ensure
the correct order
- after altering dev_gen to make this change visible for other
agents and to trigger local cache update

With best regards,
Slava

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 21 lines
Quoted 12 lines

nd

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order cannot be

kept.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with

this.

Quoted 50 lines
Quoted 50 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

Wang

Quoted 19 lines
Quoted 19 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 9 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity handle) and the "global" cache for all MR registered on the

device.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

How cache update on memory freeing/unregistering

happens:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it

Quoted 7 lines
Quoted 7 lines

solve?

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions

being freed.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Hence, the lookups for these MRs in caches should not

occur.

Quoted 143 lines
Quoted 143 lines
Quoted 31 lines
Quoted 31 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

Quoted 5 lines
Quoted 5 lines

cannot happen.

Quoted 4 lines

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to

free.

Quoted 15 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 11 lines
Quoted 11 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in

use.

Quoted 104 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 2 lines

Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 30 lines

No translation to MR being invalidated can happen.

Quoted 7 lines
Quoted 7 lines

For other side, the cache flush has negative effect - the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

Quoted 6 lines

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 14 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed. In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of

Quoted 6 lines
Quoted 6 lines

time.

Quoted 5 lines
Quoted 5 lines

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local

cache.

Quoted 64 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines

And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point d).

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since
c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually

come back.

Quoted 22 lines
Quoted 22 lines
Quoted 11 lines
Quoted 3 lines

Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

emergencies.

If we have some mbufs still allocated in memory being freed
- there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 10 lines

With best regards,
Slava

Quoted 6 lines

Best Regards
Feifei

Quoted 2 lines

With best regards,
Slava

#15 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks for your explanation.

Thus we can ignore the order between update global cache and update dev_gen due to R/W lock.

Furthermore, it is unnecessary to keep wmb, and the last wmb (1d) I think can be removed.
Two reasons for this:
1. wmb has only one function, this is for the local thread to keep the write-write order. It cannot ensure write operation above it can be seen by other threads.

2. rwunlock (1e) has a atomic_release operation in it, I think this release operation is same as the last wmb : keep order.

Best Regards
Feifei

Quoted 561 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年5月7日 18:15
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

We should consider the locks in your scenario - it is crucial for the complete model description:

How agent_1 (in your terms) rebuilds global cache:

1a) lock()
1b) rebuild(global cache)
1c) update(dev_gen)
1d) wmb()
1e) unlock()

How agent_2 checks:

2a) check(dev_gen) (assume positive - changed)
2b) clear(local_cache)
2c) miss(on empty local_cache) - > eventually it goes to mr_lookup_caches() 2d) lock()
2e) get(new MR)
2f) unlock()
2g) update(local cache with obtained new MR)

Hence, even if 1c) becomes visible in 2a) before 1b) committed (say, due to out-of-order Arch) - the agent 2 would be blocked on 2d) and scenario depicted on your Fig2 would not happen (agent_2 will wait before step 3 till agent 1 unlocks after its step 5).

With best regards,
Slava

Quoted 10 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Friday, May 7, 2021 9:36> To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 10 lines

Hi, Slava

Thanks very much for your reply.

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月6日 19:22
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 35 lines
Quoted 4 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 8 lines

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb.
We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to ensure this.

1. For the first problem why we should get rid of the last wmb and move it before dev_gen updated, I think our attention is how the wmb implements the synchronization between multiple agents. Fig1
----------------------------------------------------------------------
------------------------
-------
Timeslot agent_1 agent_2
1 rebuild global cache
2 wmb
3 update dev_gen ----------------------- load changed
dev_gen
4 rebuild local cache
----------------------------------------------------------------------
------------------------
-------

First, wmb is only for local thread to keep the order between local write- write :
Based on the picture above, for agent_1, wmb keeps the order that rebuilding global cache is always before updating dev_gen.

Quoted 7 lines

Second, agent_1 communicates with agent_2 by the global variable "dev_gen" :
If agent_1 updates dev_gen, agent_2 will load it and then it knows it should rebuild local cache

Finally, agent_2 rebuilds local cache according to whether agent_1 has rebuilt global cache, and agent_2 knows this information by the variable

"dev_gen".

Quoted 48 lines

Fig2
----------------------------------------------------------------------
------------------------
-------
Timeslot agent_1 agent_2
1 update dev_gen
2 load changed dev_gen
3 rebuild local cache
4 rebuild global cache
5 wmb
----------------------------------------------------------------------
------------------------
-------

However, in arm platform, if wmb is after dev_gen updated, "dev_gen" may be updated before agent_1 rebuilding global cache, then agent_2 maybe receive error message and rebuild its local cache in advance.

To summarize, it is not important which time other agents can see the changed global variable "dev_gen".
(Actually, wmb after "dev_gen" cannot ensure changed "dev_gen" is committed to the global).
It is more important that if other agents see the changed "dev_gen", they also can know global cache has been updated.

Quoted 3 lines

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

We just move the last wmb into the right place, and not delete it for performance.

Quoted 9 lines

Also, regarding this:

Quoted 5 lines
Quoted 1 line

Another question suddenly occurred to me, in order to keep the

order that rebuilding global cache before updating ”dev_gen“, the

Quoted 2 lines

wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order cannot

be

kept.

Quoted 5 lines

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

Quoted 4 lines

2. The second function of wmb before "dev_gen" updated is for performance according to our previous discussion. According to Fig2, if there is no wmb between "global cache updated" and "dev_gen updated", "dev_gen" may update before global cache

updated.

Quoted 34 lines

Then agent_2 may see the changed "dev_gen" and flush entire local cache in advance.

This entire flush can degrade the performance:
"the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in data path on acquiring global cache lock."

Furthermore, spinlock is just for global cache, not for dev_gen and local cache.

Quoted 8 lines

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling,
could you, please, elaborate? I'm not sure we should maintain an
order (due to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

At last, for my view, there are two functions that moving wmb before "dev_gen"
for the write-write order:
--------------------------------
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen
--------------------------------
1. Achieve synchronization between multiple threads in the right way 2. Prevent other agents from flushing local cache early to ensure performance

Quoted 12 lines

Best Regards
Feifei

Quoted 9 lines

With best regards,
Slava

Quoted 6 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 50 lines
Quoted 16 lines
Quoted 16 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch?
For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Best Regards
Feifei

Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 2 lines
Quoted 2 lines

<[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 24 lines
Quoted 7 lines
Quoted 7 lines

cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and

write.

Quoted 16 lines
Quoted 5 lines

and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 10 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering
dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be

Quoted 19 lines

flushed.

Quoted 18 lines
Quoted 18 lines
Quoted 7 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible".
The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 10 lines

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 22 lines
Quoted 22 lines

nd

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

<[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to
ensure the correct order
- after altering dev_gen to make this change visible for
other agents and to trigger local cache update

With best regards,
Slava

Quoted 6 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Quoted 13 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 10 lines
Quoted 10 lines

nd

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather

than after it.

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Otherwise, in the out-of-order platforms, current order cannot be

kept.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan

Azrad

Quoted 80 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

cache

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with

this.

Quoted 50 lines
Quoted 50 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

Wang

Quoted 19 lines
Quoted 19 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 9 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity handle) and the "global" cache for all MR registered on the

device.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

How cache update on memory freeing/unregistering

happens:

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it

solve?

Quoted 98 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions

being freed.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Hence, the lookups for these MRs in caches should not

occur.

Quoted 90 lines
Quoted 90 lines
Quoted 31 lines
Quoted 31 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

For your first point that, application can take charge of preventing MR freed memory being allocated to data path.

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

Quoted 5 lines
Quoted 5 lines

cannot happen.

Quoted 4 lines

Actually, this is the only correct way for application to operate. Let's suppose we have some memory area that application wants to

free.

Quoted 15 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 11 lines
Quoted 11 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in

use.

Quoted 51 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 2 lines

Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 30 lines

No translation to MR being invalidated can happen.

Quoted 7 lines
Quoted 7 lines

For other side, the cache flush has negative effect
- the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

Quoted 6 lines

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second point. I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 14 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast searching speed. In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global cache lock.

However, this mechanism just ensures the performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of

Quoted 6 lines
Quoted 6 lines

time.

Quoted 5 lines
Quoted 5 lines

Local cache should be updated to remove MRs no longer valid. But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local

cache.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point

d).

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since
c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually

come back.

Quoted 22 lines
Quoted 22 lines
Quoted 11 lines
Quoted 3 lines

Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

emergencies.

If we have some mbufs still allocated in memory being freed
- there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 10 lines

With best regards,
Slava

Quoted 6 lines

Best Regards
Feifei

Quoted 2 lines

With best regards,
Slava

#16 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below.

Quoted 10 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Saturday, May 8, 2021 6:13
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 6 lines

Hi, Slava

Thanks for your explanation.

Thus we can ignore the order between update global cache and update dev_gen due to R/W lock.

Yes, exactly.

Quoted 10 lines

Furthermore, it is unnecessary to keep wmb, and the last wmb (1d) I think can be removed.
Two reasons for this:
1. wmb has only one function, this is for the local thread to keep the write- write order. It cannot ensure write operation above it can be seen by other threads.

2. rwunlock (1e) has a atomic_release operation in it, I think this release operation is same as the last wmb : keep order.

Mmmm... In my understanding wmb ensures all memory writings are committed and visible by other agent. Without committing some writings might be visible and others not and we would get some inconsistent state. In other words - wmb here is rather for consistence, not for order.

With best regards,
Slava

Quoted 586 lines

Best Regards
Feifei

Quoted 206 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年5月7日 18:15
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

We should consider the locks in your scenario - it is crucial for the complete model description:

How agent_1 (in your terms) rebuilds global cache:

1a) lock()
1b) rebuild(global cache)
1c) update(dev_gen)
1d) wmb()
1e) unlock()

How agent_2 checks:

2a) check(dev_gen) (assume positive - changed) 2b) clear(local_cache)
2c) miss(on empty local_cache) - > eventually it goes to mr_lookup_caches()
2d) lock()
2e) get(new MR)
2f) unlock()
2g) update(local cache with obtained new MR)

Hence, even if 1c) becomes visible in 2a) before 1b) committed (say, due to out-of-order Arch) - the agent 2 would be blocked on 2d) and scenario depicted on your Fig2 would not happen (agent_2 will wait before step 3 till agent 1 unlocks after its step 5).

With best regards,
Slava

Quoted 10 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Friday, May 7, 2021 9:36> To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 10 lines

Hi, Slava

Thanks very much for your reply.

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月6日 19:22
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 37 lines
Quoted 4 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 8 lines

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb.
We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to ensure this.

1. For the first problem why we should get rid of the last wmb and move it before dev_gen updated, I think our attention is how the wmb implements the synchronization between multiple agents. Fig1
--------------------------------------------------------------------
--
------------------------
-------
Timeslot agent_1 agent_2
1 rebuild global cache
2 wmb
3 update dev_gen ----------------------- load changed
dev_gen
4 rebuild local cache
--------------------------------------------------------------------
--
------------------------
-------

First, wmb is only for local thread to keep the order between local write- write :
Based on the picture above, for agent_1, wmb keeps the order that rebuilding global cache is always before updating dev_gen.

Quoted 8 lines

Second, agent_1 communicates with agent_2 by the global variable "dev_gen" :
If agent_1 updates dev_gen, agent_2 will load it and then it knows it should rebuild local cache

Finally, agent_2 rebuilds local cache according to whether agent_1 has rebuilt global cache, and agent_2 knows this information by the variable

"dev_gen".

Quoted 52 lines

Fig2
--------------------------------------------------------------------
--
------------------------
-------
Timeslot agent_1 agent_2
1 update dev_gen
2 load changed dev_gen
3 rebuild local cache
4 rebuild global cache
5 wmb
--------------------------------------------------------------------
--
------------------------
-------

However, in arm platform, if wmb is after dev_gen updated, "dev_gen" may be updated before agent_1 rebuilding global cache, then agent_2 maybe receive error message and rebuild its local cache in advance.

To summarize, it is not important which time other agents can see the changed global variable "dev_gen".
(Actually, wmb after "dev_gen" cannot ensure changed "dev_gen" is committed to the global).
It is more important that if other agents see the changed "dev_gen", they also can know global cache has been updated.

Quoted 3 lines

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

We just move the last wmb into the right place, and not delete it for performance.

Quoted 11 lines

Also, regarding this:

Quoted 1 line
Quoted 1 line

Another question suddenly occurred to me, in order to keep

the

Quoted 5 lines

order that rebuilding global cache before updating ”dev_gen“, the

Quoted 2 lines

wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order

cannot be

kept.

Quoted 5 lines

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

Quoted 4 lines

2. The second function of wmb before "dev_gen" updated is for performance according to our previous discussion. According to Fig2, if there is no wmb between "global cache updated" and "dev_gen updated", "dev_gen" may update before global cache

updated.

Quoted 40 lines

Then agent_2 may see the changed "dev_gen" and flush entire local cache in advance.

This entire flush can degrade the performance:
"the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in data path on acquiring global cache lock."

Furthermore, spinlock is just for global cache, not for dev_gen and local cache.

Quoted 8 lines

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling,
could you, please, elaborate? I'm not sure we should maintain an
order (due to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

At last, for my view, there are two functions that moving wmb before "dev_gen"
for the write-write order:
--------------------------------
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen
--------------------------------
1. Achieve synchronization between multiple threads in the right way 2. Prevent other agents from flushing local cache early to ensure performance

Best Regards
Feifei

Quoted 3 lines

With best regards,
Slava

Quoted 29 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch?
For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 98 lines
Quoted 28 lines
Quoted 3 lines

Wang

Quoted 2 lines
Quoted 2 lines

<[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 24 lines
Quoted 7 lines
Quoted 7 lines

cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and

write.

Quoted 16 lines
Quoted 5 lines

and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 10 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering
dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be

Quoted 19 lines

flushed.

Quoted 18 lines
Quoted 18 lines
Quoted 7 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible".
The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 10 lines

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 23 lines
Quoted 23 lines

nd

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

<[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to
ensure the correct order
- after altering dev_gen to make this change visible for
other agents and to trigger local cache update

With best regards,
Slava

Quoted 7 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Quoted 14 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 11 lines
Quoted 11 lines

nd

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating "dev_gen" rather

than after it.

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Otherwise, in the out-of-order platforms, current order cannot be

kept.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Quoted 38 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan

Azrad

Quoted 31 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

cache

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with

this.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation again.

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan

Azrad

Quoted 53 lines
Quoted 53 lines
Quoted 38 lines
Quoted 38 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

Wang

Quoted 19 lines
Quoted 19 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 9 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Each Tx queue has its own dedicated "local" cache for MRs to convert buffer address in mbufs being transmitted to LKeys (HW-related entity handle) and the "global" cache for all MR registered on the

device.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

How cache update on memory freeing/unregistering

happens:

Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps [a] and [b], and local caches flush is requested earlier. What problem does it

solve?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

It is not supposed there are in datapath some mbufs referencing to the memory being freed. Application must ensure this and must not allocate new mbufs from this memory regions

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

being freed.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Hence, the lookups for these MRs in caches should not

occur.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For your first point that, application can take charge of preventing MR freed memory being allocated to

data path.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

cannot happen.

Quoted 2 lines

Actually, this is the only correct way for application to

operate.

Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 18 lines
Quoted 18 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Let's suppose we have some memory area that application wants to

free.

Quoted 15 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 11 lines
Quoted 11 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in

use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 2 lines

Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 10 lines

No translation to MR being invalidated can happen.

Quoted 8 lines
Quoted 8 lines

For other side, the cache flush has negative effect
- the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in datapatch

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second

point.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 4 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast

searching speed.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global

cache lock.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

However, this mechanism just ensures the performance unchanged for the first few mbufs. During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of

time.

Quoted 2 lines
Quoted 2 lines

Local cache should be updated to remove MRs no longer

valid.

Quoted 69 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local

cache.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point b) till point

d).

Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since
c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually

come back.

Quoted 21 lines
Quoted 21 lines
Quoted 11 lines
Quoted 3 lines

Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

emergencies.

If we have some mbufs still allocated in memory being freed
- there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 9 lines

With best regards,
Slava

Quoted 5 lines

Best Regards
Feifei

Quoted 1 line

With best regards, Slava

#17 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Please see below.

Quoted 39 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年5月11日 16:19
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below.

Quoted 10 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Saturday, May 8, 2021 6:13
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 6 lines

Hi, Slava

Thanks for your explanation.

Thus we can ignore the order between update global cache and update dev_gen due to R/W lock.

Yes, exactly.

Quoted 8 lines

Furthermore, it is unnecessary to keep wmb, and the last wmb (1d) I think can be removed.
Two reasons for this:
1. wmb has only one function, this is for the local thread to keep the write- write order. It cannot ensure write operation above it can be seen by other threads.

Quoted 8 lines
Quoted 2 lines

2. rwunlock (1e) has a atomic_release operation in it, I think this release operation is same as the last wmb : keep order.

Mmmm... In my understanding wmb ensures all memory writings are committed and visible by other agent. Without committing some writings might be visible and others not and we would get some inconsistent state. In other words - wmb here is rather for consistence, not for order.

If I understand correctly, your meaning is that if without wmb, maybe other agents observe changed "dev_gen", but they also observe unchanged "global" cache. This can be defined as memory inconsistent state.

Fig1
-----------------------------------------------------------------------------------------------------
Timeslot agent_1 agent_2
1 take_lock
2 update dev_gen
3 observe changed dev_gen
4 clear local cache
5 rebuild global cache wait_lock
6 free_lock
7 wmb take_lock
8 get(new MR)
9 free_lock
-----------------------------------------------------------------------------------------------------

1. However, in out-of-order platform, though adding a 'wmb at last', 'dev_gen' maybe updated before global cache rebuild, and then other agents can observe changed 'dev_ge' before rebuilding global cache.

Thus, though add a 'wmb at last', It is still unable to prevent other agents from observing some inconsistent state. As a result, 'wmb at last' fails to keep consistence.

2. On the other hand, due to lock, agent_2 will wait to take a lock until global cache rebuilt by agent_1, and this ensures agent_2 can get a correct new MR and update new local cache correctly.

In summary, 'wmb at last' cannot guarantee other agents to observe the consistent state. But lock can fix this error. So, the existence of wmb at last is redundant and we can remove it.

Best Regards
Feifei

Quoted 615 lines

With best regards,
Slava

Quoted 10 lines

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月7日 18:15
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 36 lines
Quoted 36 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

We should consider the locks in your scenario - it is crucial for the complete model description:

How agent_1 (in your terms) rebuilds global cache:

1a) lock()
1b) rebuild(global cache)
1c) update(dev_gen)
1d) wmb()
1e) unlock()

How agent_2 checks:

2a) check(dev_gen) (assume positive - changed) 2b) clear(local_cache)
2c) miss(on empty local_cache) - > eventually it goes to mr_lookup_caches()
2d) lock()
2e) get(new MR)
2f) unlock()
2g) update(local cache with obtained new MR)

Hence, even if 1c) becomes visible in 2a) before 1b) committed (say, due to out-of-order Arch) - the agent 2 would be blocked on 2d) and scenario depicted on your Fig2 would not happen (agent_2 will wait before step 3 till agent 1 unlocks after its step 5).

With best regards,
Slava

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Friday, May 7, 2021 9:36> To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 82 lines
Quoted 82 lines
Quoted 4 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 10 lines

Hi, Slava

Thanks very much for your reply.

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月6日 19:22
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 40 lines
Quoted 4 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 8 lines

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb.
We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to ensure this.

1. For the first problem why we should get rid of the last wmb and move it before dev_gen updated, I think our attention is how the wmb implements the synchronization between multiple agents. Fig1
------------------------------------------------------------------
--
--
------------------------
-------
Timeslot agent_1 agent_2
1 rebuild global cache
2 wmb
3 update dev_gen ----------------------- load changed
dev_gen
4 rebuild local cache
------------------------------------------------------------------
--
--
------------------------
-------

First, wmb is only for local thread to keep the order between local
write- write :
Based on the picture above, for agent_1, wmb keeps the order that rebuilding global cache is always before updating dev_gen.

Quoted 8 lines

Second, agent_1 communicates with agent_2 by the global variable "dev_gen" :
If agent_1 updates dev_gen, agent_2 will load it and then it knows it should rebuild local cache

Finally, agent_2 rebuilds local cache according to whether agent_1 has rebuilt global cache, and agent_2 knows this information by the variable

"dev_gen".

Quoted 18 lines

Fig2
------------------------------------------------------------------
--
--
------------------------
-------
Timeslot agent_1 agent_2
1 update dev_gen
2 load changed dev_gen
3 rebuild local cache
4 rebuild global cache
5 wmb
------------------------------------------------------------------
--
--
------------------------
-------

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

However, in arm platform, if wmb is after dev_gen updated, "dev_gen" may be updated before agent_1 rebuilding global cache, then agent_2 maybe receive error message and rebuild its local cache in

advance.

Quoted 101 lines
Quoted 101 lines
Quoted 33 lines

To summarize, it is not important which time other agents can see the changed global variable "dev_gen".
(Actually, wmb after "dev_gen" cannot ensure changed "dev_gen" is committed to the global).
It is more important that if other agents see the changed "dev_gen", they also can know global cache has been updated.

Quoted 3 lines

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

We just move the last wmb into the right place, and not delete it for performance.

Quoted 11 lines

Also, regarding this:

Quoted 1 line
Quoted 1 line

Another question suddenly occurred to me, in order to keep

the

Quoted 5 lines

order that rebuilding global cache before updating ”dev_gen“, the

Quoted 2 lines

wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order

cannot be

kept.

Quoted 5 lines

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

Quoted 4 lines

2. The second function of wmb before "dev_gen" updated is for performance according to our previous discussion. According to Fig2, if there is no wmb between "global cache updated" and "dev_gen updated", "dev_gen" may update before global cache

updated.

Quoted 34 lines

Then agent_2 may see the changed "dev_gen" and flush entire local cache in advance.

This entire flush can degrade the performance:
"the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in data path on acquiring global cache lock."

Furthermore, spinlock is just for global cache, not for dev_gen and local cache.

Quoted 8 lines

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling,
could you, please, elaborate? I'm not sure we should maintain
an order (due to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

At last, for my view, there are two functions that moving wmb before "dev_gen"
for the write-write order:
--------------------------------
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen
--------------------------------
1. Achieve synchronization between multiple threads in the right way 2. Prevent other agents from flushing local cache early to ensure performance

Quoted 12 lines

Best Regards
Feifei

Quoted 9 lines

With best regards,
Slava

Quoted 6 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch?
For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Best Regards
Feifei

Quoted 95 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 88 lines
Quoted 28 lines
Quoted 3 lines

Wang

Quoted 2 lines
Quoted 2 lines

<[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 24 lines
Quoted 7 lines
Quoted 7 lines

cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and

write.

Quoted 16 lines
Quoted 5 lines

and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 10 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering
dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be

Quoted 19 lines

flushed.

Quoted 18 lines
Quoted 18 lines
Quoted 7 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible".
The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 10 lines

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 37 lines
Quoted 26 lines

nd

Quoted 25 lines
Quoted 15 lines
Quoted 15 lines

<[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to
ensure the correct order
- after altering dev_gen to make this change visible for
other agents and to trigger local cache update

With best regards,
Slava

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 10 lines
Quoted 10 lines

nd

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating

"dev_gen"

Quoted 33 lines
Quoted 33 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

rather

than after it.

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Otherwise, in the out-of-order platforms, current order cannot be

kept.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>; Matan

Azrad

Quoted 15 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

cache

Hi, Slava

Thanks very much for your explanation.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with

this.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation

again.

Quoted 12 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 219 lines
Quoted 51 lines
Quoted 51 lines
Quoted 36 lines
Quoted 36 lines

Ruifeng

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

Wang

Quoted 19 lines
Quoted 19 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 9 lines
Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes. Do
we have some issue/bug with MR cache in practice?

This patch fixes the bug which is based on logical deduction, and it doesn't actually happen.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Each Tx queue has its own dedicated "local"
cache for MRs to convert buffer address in mbufs
being transmitted to LKeys (HW-related entity
handle) and the "global" cache for all MR
registered on the

device.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

How cache update on memory freeing/unregistering

happens:

Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

- acquire lock for global cache write access
- [a] remove relevant MRs from the global cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps [a]
and [b], and local caches flush is requested earlier.
What problem does it

solve?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

It is not supposed there are in datapath some
mbufs referencing to the memory being freed.
Application must ensure this and must not
allocate new mbufs from this memory regions

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

being freed.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Hence, the lookups for these MRs in caches
should not

occur.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

For your first point that, application can take charge of preventing MR freed memory being allocated to

data path.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines

Does it means that If there is an emergency of MR fragment, such as hotplug, the application must inform thedata path in advance, and this memory will not be allocated, and then the control path will free this memory? If application can do like this, I agree that this bug

cannot happen.

Quoted 3 lines

Actually, this is the only correct way for application to

operate.

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 19 lines
Quoted 19 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Let's suppose we have some memory area that application wants to

free.

Quoted 16 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 12 lines
Quoted 12 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in

use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 2 lines

Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 10 lines

No translation to MR being invalidated can happen.

Quoted 8 lines
Quoted 8 lines

For other side, the cache flush has negative
effect
- the local cache is getting empty and can't
provide translation for other valid (not being
removed) MRs, and the translation has to look up
in the global cache, that is locked now for
rebuilding, this causes the delays in datapatch

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

on acquiring global cache lock.

Quoted 1 line

So, I see some potential performance impact.

If above assumption is true, we can go to your second

point.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

I think this is a problem of the tradeoff between cache coherence and

performance.

Quoted 5 lines

I can understand your meaning that though global cache has been changed, we should keep the valid MR in local cache as long as possible to ensure the fast

searching speed.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In the meanwhile, the local cache can be rebuilt later to reduce its waiting time for acquiring the global

cache lock.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

However, this mechanism just ensures the performance unchanged for the first few mbufs. During the next mbufs lkey searching after 'dev_gen' updated, it is still necessary to update the local cache. And the performance can firstly reduce and then returns. Thus, no matter whether there is this patch or not, the performance will jitter in a certain period of

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

time.

Quoted 3 lines
Quoted 3 lines

Local cache should be updated to remove MRs no longer

valid.

Quoted 73 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local

cache.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point
b) till point

d).

Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
Quoted 56 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since
c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after
c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 1 line
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Finally, in conclusion, I tend to think that the bottom layer can do more things to ensure the correct execution of the program, which may have a negative impact on the performance in a short time, but in the long run, the performance will eventually

come back.

Quoted 23 lines
Quoted 23 lines
Quoted 6 lines
Quoted 4 lines

Furthermore, maybe we should pay attention to the performance in the stable period, and try our best to ensure the correctness of the program in case of

emergencies.

Quoted 7 lines

If we have some mbufs still allocated in memory being freed
- there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 9 lines

With best regards,
Slava

Quoted 5 lines

Best Regards
Feifei

Quoted 1 line

With best regards, Slava

#18 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

..ship..

Quoted 15 lines

If I understand correctly, your meaning is that if without wmb, maybe other agents observe changed "dev_gen", but they also observe unchanged "global" cache.
This can be defined as memory inconsistent state.

Fig1
----------------------------------------------------------------------------------------------
-------
Timeslot agent_1 agent_2
1 take_lock
2 update dev_gen
3 observe changed dev_gen
4 clear local cache

Quoted 7 lines

5 rebuild global cache wait_lock
6 free_lock
7 wmb take_lock
8 get(new MR)
9 free_lock
----------------------------------------------------------------------------------------------
-------

Yes, something like that.

Quoted 9 lines

1. However, in out-of-order platform, though adding a 'wmb at last', 'dev_gen' maybe updated before global cache rebuild, and then other agents can observe changed 'dev_ge'
before rebuilding global cache.

Thus, though add a 'wmb at last', It is still unable to prevent other agents from observing some inconsistent state. As a result, 'wmb at last' fails to keep consistence.

Quoted 8 lines

2. On the other hand, due to lock, agent_2 will wait to take a lock until global cache rebuilt by agent_1, and this ensures agent_2 can get a correct new MR and update new local cache correctly.

In summary, 'wmb at last' cannot guarantee other agents to observe the consistent state.
But lock can fix this error. So, the existence of wmb at last is redundant and we can remove it.

If dev_gen change is committed and cache's one is not yet - the agent_2 might see inconsistent state even inside the lock-protected section. Hence, we must commit all writes before leaving the locked section in agent_1.

Let’s suppose there is no wmb in agent_1 at all, and dev_gen is arbitrary committed
by CPU and MR cache data change is not. We leave the locked section in agent_1, agent_2 sees dev_gen changed, takes the lock and sees inconsistent MR-cache state due to not all changes made in agent_1 are committed. With wmb we have now in the existing code - there is no issue like that.

With best regards,
Slava

Quoted 643 lines

Best Regards
Feifei

Quoted 8 lines

With best regards,
Slava

Quoted 4 lines

Best Regards
Feifei

Quoted 47 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月7日 18:15
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 40 lines
Quoted 40 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

We should consider the locks in your scenario - it is crucial for the complete model description:

How agent_1 (in your terms) rebuilds global cache:

1a) lock()
1b) rebuild(global cache)
1c) update(dev_gen)
1d) wmb()
1e) unlock()

How agent_2 checks:

2a) check(dev_gen) (assume positive - changed) 2b) clear(local_cache)
2c) miss(on empty local_cache) - > eventually it goes to mr_lookup_caches()
2d) lock()
2e) get(new MR)
2f) unlock()
2g) update(local cache with obtained new MR)

Hence, even if 1c) becomes visible in 2a) before 1b) committed (say, due to out-of-order Arch) - the agent 2 would be blocked on 2d) and scenario depicted on your Fig2 would not happen (agent_2 will wait before step 3 till agent 1 unlocks after its step 5).

With best regards,
Slava

Quoted 4 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Friday, May 7, 2021 9:36> To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>;

Shahaf

Quoted 17 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your reply.

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月6日 19:22
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

Wang

Quoted 13 lines
Quoted 12 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb.
We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to ensure this.

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

1. For the first problem why we should get rid of the last wmb and move it before dev_gen updated, I think our attention is how the wmb implements the synchronization between multiple agents. Fig1
----------------------------------------------------------------
--
--
--

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

------------------------
-------
Timeslot agent_1 agent_2
1 rebuild global cache
2 wmb
3 update dev_gen ----------------------- load changed
dev_gen
4 rebuild local

cache

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 13 lines

----------------------------------------------------------------
--
--
--
------------------------
-------

First, wmb is only for local thread to keep the order between local
write- write :
Based on the picture above, for agent_1, wmb keeps the order that rebuilding global cache is always before updating dev_gen.

Quoted 8 lines

Second, agent_1 communicates with agent_2 by the global variable "dev_gen" :
If agent_1 updates dev_gen, agent_2 will load it and then it knows it should rebuild local cache

Finally, agent_2 rebuilds local cache according to whether agent_1 has rebuilt global cache, and agent_2 knows this information by the variable

"dev_gen".

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Fig2
----------------------------------------------------------------
--
--
--
------------------------
-------
Timeslot agent_1 agent_2
1 update dev_gen
2 load changed

dev_gen

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

3 rebuild local

cache

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

4 rebuild global cache
5 wmb
----------------------------------------------------------------
--
--
--
------------------------
-------

However, in arm platform, if wmb is after dev_gen updated,

"dev_gen"

Quoted 51 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

may be updated before agent_1 rebuilding global cache, then agent_2 maybe receive error message and rebuild its local cache in

advance.

Quoted 47 lines
Quoted 47 lines
Quoted 33 lines

To summarize, it is not important which time other agents can see the changed global variable "dev_gen". (Actually, wmb after "dev_gen" cannot ensure changed "dev_gen" is committed to the global).
It is more important that if other agents see the changed "dev_gen", they also can know global cache has been updated.

Quoted 3 lines

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

We just move the last wmb into the right place, and not delete it for performance.

Quoted 11 lines

Also, regarding this:

Quoted 1 line
Quoted 1 line

Another question suddenly occurred to me, in order to

keep the

Quoted 5 lines

order that rebuilding global cache before updating ”dev_gen“, the

Quoted 2 lines

wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order

cannot be

kept.

Quoted 5 lines

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

Quoted 4 lines

2. The second function of wmb before "dev_gen" updated is for performance according to our previous discussion. According to Fig2, if there is no wmb between "global cache updated" and "dev_gen updated", "dev_gen" may update before global cache

updated.

Quoted 9 lines

Then agent_2 may see the changed "dev_gen" and flush entire local cache in advance.

This entire flush can degrade the performance:
"the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in data path on acquiring global

cache lock."

Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 25 lines

Furthermore, spinlock is just for global cache, not for dev_gen and local cache.

Quoted 8 lines

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling,
could you, please, elaborate? I'm not sure we should
maintain an order (due to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

At last, for my view, there are two functions that moving wmb before "dev_gen"
for the write-write order:
--------------------------------
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen
-------------------------------- 1. Achieve synchronization between multiple threads in the right way 2.
Prevent other agents from flushing local cache early to ensure performance

Quoted 12 lines

Best Regards
Feifei

Quoted 9 lines

With best regards,
Slava

Quoted 6 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch? For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan

Azrad

Quoted 87 lines
Quoted 87 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 84 lines
Quoted 29 lines
Quoted 4 lines

Wang

Quoted 3 lines
Quoted 3 lines

<[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 24 lines
Quoted 7 lines
Quoted 7 lines

cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and

write.

Quoted 16 lines
Quoted 5 lines

and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 10 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering
dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be

Quoted 19 lines

flushed.

Quoted 18 lines
Quoted 18 lines
Quoted 7 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible". The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 10 lines

Best Regards
Feifei

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 32 lines
Quoted 26 lines

nd

Quoted 25 lines
Quoted 15 lines
Quoted 15 lines

<[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen, to
ensure the correct order
- after altering dev_gen to make this change visible
for other agents and to trigger local cache update

With best regards,
Slava

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 5 lines
Quoted 5 lines

nd

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 23 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating

"dev_gen"

Quoted 17 lines
Quoted 17 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

rather

than after it.

Quoted 15 lines
Quoted 15 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Otherwise, in the out-of-order platforms, current order cannot be

kept.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>;

Matan

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 31 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 3 lines

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

this.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation

again.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan

Quoted 21 lines
Quoted 4 lines

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

Ruifeng

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

Wang

Quoted 14 lines
Quoted 14 lines

<[email protected]>; nd <[email protected]> 主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Please, see below

....

Quoted 4 lines

Hi, Feifei

Sorry, I do not follow what this patch fixes.
Do we have some issue/bug with MR cache in

practice?

Quoted 120 lines
Quoted 120 lines
Quoted 38 lines
Quoted 38 lines
Quoted 21 lines
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

This patch fixes the bug which is based on
logical deduction, and it doesn't actually happen.

Each Tx queue has its own dedicated "local"
cache for MRs to convert buffer address in
mbufs being transmitted to LKeys (HW-related
entity
handle) and the "global" cache for all MR
registered on the

device.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

How cache update on memory
freeing/unregistering

happens:

Quoted 14 lines
Quoted 14 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

- acquire lock for global cache write access
- [a] remove relevant MRs from the global
cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps
[a] and [b], and local caches flush is requested earlier.
What problem does it

solve?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

It is not supposed there are in datapath some
mbufs referencing to the memory being freed.
Application must ensure this and must not
allocate new mbufs from this memory regions

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

being freed.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Hence, the lookups for these MRs in caches
should not

occur.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

For your first point that, application can take
charge of preventing MR freed memory being
allocated to

data path.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Does it means that If there is an emergency of
MR fragment, such as hotplug, the application
must inform thedata path in advance, and this
memory will not be allocated, and then the
control path will free this memory? If
application can do like this, I agree that this
bug

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

cannot happen.

Quoted 3 lines

Actually, this is the only correct way for application to

operate.

Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
Quoted 19 lines
Quoted 19 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Let's suppose we have some memory area that application wants to

free.

Quoted 16 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If we have some mbufs allocated from this area, it means that we have memory pool created

there.

Quoted 12 lines
Quoted 12 lines

What application should do:
- notify all its components/agents the memory area is going to be freed
- all components/agents free the mbufs they might own
- PMD might not support freeing for some mbufs (for example being sent and awaiting for completion), so app should just wait
- wait till all mbufs are returned to the memory pool (by monitoring available obj == pool size)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Otherwise - it is dangerous to free the memory. There are just some mbufs still allocated, it is regardless to buf address to MR translation. We just can't free the memory - the mapping will be destroyed and might cause the segmentation fault by SW or some HW issues on DMA access to unmapped memory. It is very generic safety approach - do not free the memory that is still in

use.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 2 lines

Hence, at the moment of freeing and unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 11 lines

No translation to MR being invalidated can happen.

Quoted 9 lines

For other side, the cache flush has negative
effect
- the local cache is getting empty and can't
provide translation for other valid (not being
removed) MRs, and the translation has to look
up in the global cache, that is locked now for
rebuilding, this causes the delays in
datapatch

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

on acquiring global cache lock.
So, I see some potential performance impact.

If above assumption is true, we can go to your
second

point.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

I think this is a problem of the tradeoff
between cache coherence and

performance.

Quoted 5 lines

I can understand your meaning that though global
cache has been changed, we should keep the valid
MR in local cache as long as possible to ensure
the fast

searching speed.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In the meanwhile, the local cache can be rebuilt
later to reduce its waiting time for acquiring
the global

Quoted 7 lines
Quoted 7 lines

cache lock.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

However, this mechanism just ensures the
performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen'
updated, it is still necessary to update the local cache.
And the performance can firstly reduce and then

returns.

Quoted 80 lines
Quoted 80 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Thus, no matter whether there is this patch or
not, the performance will jitter in a certain
period of

time.

Quoted 3 lines
Quoted 3 lines

Local cache should be updated to remove MRs no longer

valid.

Quoted 72 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not valid MRX in local

cache.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

And there are traffic in the datapath for MR0 and MR1, and no traffic for MRX anymore.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0, MR1, MRX will be removed on translation in datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will be blocked on lock taken for cache update since point
b) till point

d).

Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs non-existing in local cache (not happens for MR0 and MR1, must not happen for MRX), and probability should be minor. And lock might happen since
c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after b),
2) lock MIGHT happen, for cache miss ONLY, after
c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 1 line
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Finally, in conclusion, I tend to think that the
bottom layer can do more things to ensure the
correct execution of the program, which may have
a negative impact on the performance in a short
time, but in the long run, the performance will
eventually

come back.

Quoted 22 lines
Quoted 22 lines
Quoted 6 lines
Quoted 4 lines

Furthermore, maybe we should pay attention to
the performance in the stable period, and try
our best to ensure the correctness of the
program in case of

emergencies.

Quoted 7 lines

If we have some mbufs still allocated in memory being freed
- there is nothing to say about correctness, it is totally incorrect. In my opinion, we should not think how to mitigate this incorrect behavior, we should not encourage application developers to follow the wrong

approaches.

Quoted 8 lines

With best regards, Slava

Quoted 5 lines

Best Regards
Feifei

With best regards, Slava

#19 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Please see below.

Quoted 62 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年5月12日 19:08
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

..ship..

Quoted 16 lines

If I understand correctly, your meaning is that if without wmb, maybe other agents observe changed "dev_gen", but they also observe unchanged "global" cache.
This can be defined as memory inconsistent state.

Fig1
----------------------------------------------------------------------
------------------------
-------
Timeslot agent_1 agent_2
1 take_lock
2 update dev_gen
3 observe changed dev_gen
4 clear local cache

Quoted 8 lines

5 rebuild global cache wait_lock
6 free_lock
7 wmb take_lock
8 get(new MR)
9 free_lock
----------------------------------------------------------------------
------------------------
-------

Yes, something like that.

Quoted 9 lines

1. However, in out-of-order platform, though adding a 'wmb at last', 'dev_gen' maybe updated before global cache rebuild, and then other agents can observe changed 'dev_ge'
before rebuilding global cache.

Thus, though add a 'wmb at last', It is still unable to prevent other agents from observing some inconsistent state. As a result, 'wmb at last' fails to keep consistence.

Quoted 8 lines

2. On the other hand, due to lock, agent_2 will wait to take a lock until global cache rebuilt by agent_1, and this ensures agent_2 can get a correct new MR and update new local cache correctly.

In summary, 'wmb at last' cannot guarantee other agents to observe the consistent state.
But lock can fix this error. So, the existence of wmb at last is redundant and we can remove it.

If dev_gen change is committed and cache's one is not yet - the agent_2 might see inconsistent state even inside the lock-protected section. Hence, we must commit all writes before leaving the locked section in agent_1.

Quoted 6 lines

Let’s suppose there is no wmb in agent_1 at all, and dev_gen is arbitrary committed by CPU and MR cache data change is not. We leave the locked section in agent_1, agent_2 sees dev_gen changed, takes the lock and sees inconsistent MR-cache state due to not all changes made in agent_1 are committed. With wmb we have now in the existing code - there is no issue like that.

I can understand you worry that if there is no 'wmb at last', when agent_1 leave the locked section, agent_2 still may observe unchanged global cache.

However, when agent_2 take a lock and get(new MR) in time slot 7(Fig.1), it means agent_1 has finished updating global cache and lock is freed.
Besides, if agent_2 can take a lock, it also shows agent_2 has observed the changed global cache.

This is because there is a store- release in rte_rwlock_read_unlock, store-release ensures all store operation before 'release' can be committed if store operation after 'release' is observed by other agents.

Thus, in our case, if agent_2 can observe updated 'rwl->cnt'(lock) and take a lock, it also can observe updated 'dev_gen' and 'global cache'.

As a result, wmb can be removed due to store-release in R/W unlock.

Best Regards
Feifei

Quoted 670 lines

With best regards,
Slava

Quoted 72 lines

Best Regards
Feifei

Quoted 8 lines

With best regards,
Slava

Quoted 4 lines

Best Regards
Feifei

Quoted 48 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月7日 18:15
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 41 lines
Quoted 41 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

We should consider the locks in your scenario - it is crucial for the complete model description:

How agent_1 (in your terms) rebuilds global cache:

1a) lock()
1b) rebuild(global cache)
1c) update(dev_gen)
1d) wmb()
1e) unlock()

How agent_2 checks:

2a) check(dev_gen) (assume positive - changed) 2b) clear(local_cache)
2c) miss(on empty local_cache) - > eventually it goes to mr_lookup_caches()
2d) lock()
2e) get(new MR)
2f) unlock()
2g) update(local cache with obtained new MR)

Hence, even if 1c) becomes visible in 2a) before 1b) committed (say, due to out-of-order Arch) - the agent 2 would be blocked on
2d) and scenario depicted on your Fig2 would not happen (agent_2 will wait before step 3 till agent 1 unlocks after its step 5).

With best regards,
Slava

Quoted 4 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Friday, May 7, 2021 9:36> To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>;

Shahaf

Quoted 11 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your reply.

Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月6日 19:22
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

Wang

Quoted 16 lines
Quoted 4 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 8 lines

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb.
We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to ensure this.

1. For the first problem why we should get rid of the last wmb and move it before dev_gen updated, I think our attention is how the wmb implements the synchronization between multiple

agents.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Fig1
--------------------------------------------------------------
--
--

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

--
--
------------------------
-------
Timeslot agent_1 agent_2
1 rebuild global cache
2 wmb
3 update dev_gen ----------------------- load

changed

Quoted 50 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

dev_gen
4 rebuild local

cache

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 14 lines

--------------------------------------------------------------
--
--
--
--
------------------------
-------

First, wmb is only for local thread to keep the order between local
write- write :
Based on the picture above, for agent_1, wmb keeps the order that rebuilding global cache is always before updating dev_gen.

Quoted 8 lines

Second, agent_1 communicates with agent_2 by the global variable "dev_gen" :
If agent_1 updates dev_gen, agent_2 will load it and then it knows it should rebuild local cache

Finally, agent_2 rebuilds local cache according to whether agent_1 has rebuilt global cache, and agent_2 knows this information by the variable

"dev_gen".

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Fig2
--------------------------------------------------------------
--
--
--
--
------------------------
-------
Timeslot agent_1 agent_2
1 update dev_gen
2 load changed

dev_gen

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

3 rebuild local

cache

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

4 rebuild global cache
5 wmb
--------------------------------------------------------------
--
--
--
--
------------------------
-------

Quoted 42 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

However, in arm platform, if wmb is after dev_gen updated,

"dev_gen"

Quoted 40 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

may be updated before agent_1 rebuilding global cache, then agent_2 maybe receive error message and rebuild its local cache in

advance.

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines

To summarize, it is not important which time other agents can see the changed global variable "dev_gen". (Actually, wmb after "dev_gen" cannot ensure changed "dev_gen" is committed to the global).
It is more important that if other agents see the changed "dev_gen", they also can know global cache has been updated.

Quoted 3 lines

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

We just move the last wmb into the right place, and not delete it for performance.

Quoted 11 lines

Also, regarding this:

Quoted 1 line
Quoted 1 line

Another question suddenly occurred to me, in order to

keep the

Quoted 5 lines

order that rebuilding global cache before updating ”dev_gen“, the

Quoted 2 lines

wmb should be before updating "dev_gen" rather than after it. Otherwise, in the out-of-order platforms, current order

cannot be

kept.

Quoted 5 lines

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

2. The second function of wmb before "dev_gen" updated is for performance according to our previous discussion. According to Fig2, if there is no wmb between "global cache

updated"

Quoted 66 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

and "dev_gen updated", "dev_gen" may update before global cache

updated.

Quoted 4 lines

Then agent_2 may see the changed "dev_gen" and flush entire local cache in advance.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

This entire flush can degrade the performance:
"the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in data path on acquiring global

cache lock."

Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 25 lines

Furthermore, spinlock is just for global cache, not for dev_gen and local cache.

Quoted 8 lines

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling,
could you, please, elaborate? I'm not sure we should
maintain an order (due to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

At last, for my view, there are two functions that moving wmb before "dev_gen"
for the write-write order:
--------------------------------
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen
-------------------------------- 1. Achieve synchronization between multiple threads in the right way 2.
Prevent other agents from flushing local cache early to ensure performance

Quoted 13 lines

Best Regards
Feifei

Quoted 10 lines

With best regards,
Slava

Quoted 7 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch? For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Quoted 57 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan

Azrad

Quoted 49 lines
Quoted 49 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 46 lines
Quoted 46 lines
Quoted 4 lines

Wang

Quoted 3 lines
Quoted 3 lines

<[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 24 lines
Quoted 7 lines
Quoted 7 lines

cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and

write.

Quoted 16 lines
Quoted 5 lines

and it cannot tell the CPU when it should commit the changes.

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 10 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be

flushed.

Quoted 16 lines
Quoted 16 lines
Quoted 7 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible". The "visible" means when updating guard variable "dev_gen" is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 8 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan

Azrad

Quoted 114 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 32 lines
Quoted 26 lines

nd

Quoted 25 lines
Quoted 15 lines
Quoted 15 lines

<[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering dev_gen,
to ensure the correct order
- after altering dev_gen to make this change visible
for other agents and to trigger local cache update

With best regards,
Slava

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 5 lines
Quoted 5 lines

nd

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating

"dev_gen"

Quoted 17 lines
Quoted 17 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

rather

than after it.

Quoted 15 lines
Quoted 15 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Otherwise, in the out-of-order platforms, current order cannot be

kept.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>;

Matan

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 46 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 3 lines

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are returned to the memory pool, and then it can free this mbufs, I agree with

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

this.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

As a result, I will remove the bug fix patch from this series and just replace the smp barrier with C11 thread fence. Thanks very much for your patient explanation

again.

Quoted 12 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>; Matan

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Ruifeng

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Wang

Quoted 8 lines
Quoted 8 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug
for Memory Region cache

Hi, Feifei

Please, see below

Quoted 39 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

....

Hi, Feifei

Sorry, I do not follow what this patch fixes.
Do we have some issue/bug with MR cache in

practice?

Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 21 lines
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

This patch fixes the bug which is based on
logical deduction, and it doesn't actually happen.

Each Tx queue has its own dedicated "local"
cache for MRs to convert buffer address in
mbufs being transmitted to LKeys (HW-related
entity
handle) and the "global" cache for all MR
registered on the

device.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush request
- lookup in local cache
- if not found:
- acquire lock for global cache read access
- lookup in global cache
- release lock for global cache

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

How cache update on memory
freeing/unregistering

happens:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

- acquire lock for global cache write access
- [a] remove relevant MRs from the global
cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch swaps
[a] and [b], and local caches flush is requested

earlier.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

What problem does it

solve?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

It is not supposed there are in datapath
some mbufs referencing to the memory being

freed.

Quoted 88 lines
Quoted 88 lines
Quoted 88 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Application must ensure this and must not
allocate new mbufs from this memory regions

being freed.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Hence, the lookups for these MRs in caches
should not

occur.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

For your first point that, application can
take charge of preventing MR freed memory
being allocated to

data path.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Does it means that If there is an emergency of
MR fragment, such as hotplug, the application
must inform thedata path in advance, and this
memory will not be allocated, and then the
control path will free this memory? If
application can do like this, I agree that
this bug

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

cannot happen.

Quoted 3 lines

Actually, this is the only correct way for
application to

operate.

Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 19 lines
Quoted 19 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Let's suppose we have some memory area that
application wants to

free.

Quoted 16 lines
Quoted 3 lines
Quoted 3 lines

ALL references to this area must be removed. If
we have some mbufs allocated from this area, it
means that we have memory pool created

there.

Quoted 12 lines
Quoted 12 lines

What application should do:
- notify all its components/agents the memory
area is going to be freed
- all components/agents free the mbufs they
might own
- PMD might not support freeing for some mbufs
(for example being sent and awaiting for
completion), so app should just wait
- wait till all mbufs are returned to the memory
pool (by monitoring available obj == pool size)

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Otherwise - it is dangerous to free the memory.
There are just some mbufs still allocated, it is
regardless to buf address to MR translation. We
just can't free the memory - the mapping will be
destroyed and might cause the segmentation fault
by SW or some HW issues on DMA access to
unmapped memory. It is very generic safety
approach - do not free the memory that is still
in

use.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 2 lines

Hence, at the moment of freeing and
unregistering the MR, there MUST BE NO any

mbufs in flight referencing to the addresses being freed.

Quoted 12 lines

No translation to MR being invalidated can happen.

For other side, the cache flush has negative
effect
- the local cache is getting empty and can't
provide translation for other valid (not
being
removed) MRs, and the translation has to
look up in the global cache, that is locked
now for rebuilding, this causes the delays
in datapatch

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

on acquiring global cache lock.
So, I see some potential performance impact.

If above assumption is true, we can go to your
second

point.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

I think this is a problem of the tradeoff
between cache coherence and
performance.

I can understand your meaning that though
global cache has been changed, we should keep
the valid MR in local cache as long as
possible to ensure the fast

searching speed.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In the meanwhile, the local cache can be
rebuilt later to reduce its waiting time for
acquiring the global

cache lock.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 53 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

However, this mechanism just ensures the
performance unchanged for the first few mbufs.
During the next mbufs lkey searching after 'dev_gen'
updated, it is still necessary to update the local cache.
And the performance can firstly reduce and
then

returns.

Quoted 46 lines
Quoted 46 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

Thus, no matter whether there is this patch or
not, the performance will jitter in a certain
period of

time.

Quoted 3 lines
Quoted 3 lines

Local cache should be updated to remove MRs no
longer

valid.

Quoted 38 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not
valid MRX in local

cache.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

And there are traffic in the datapath for MR0
and MR1, and no traffic for MRX anymore.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0,
MR1, MRX will be removed on translation in
datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will
be blocked on lock taken for cache update since
point
b) till point

d).

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs
non-existing in local cache (not happens for MR0
and MR1, must not happen for MRX), and
probability should be minor. And lock might
happen since
c) till
d)
- quite short period of time

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation after

b),

Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

2) lock MIGHT happen, for cache miss ONLY,
after
c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much shorter.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Finally, in conclusion, I tend to think that
the bottom layer can do more things to ensure
the correct execution of the program, which
may have a negative impact on the performance
in a short time, but in the long run, the
performance will eventually

come back.

Quoted 22 lines
Quoted 22 lines
Quoted 13 lines

Furthermore, maybe we should pay attention to
the performance in the stable period, and try
our best to ensure the correctness of the
program in case of
emergencies.

If we have some mbufs still allocated in memory
being freed
- there is nothing to say about correctness, it
is totally incorrect. In my opinion, we should
not think how to mitigate this incorrect
behavior, we should not encourage application
developers to follow the wrong

approaches.

Quoted 8 lines

With best regards, Slava

Best Regards
Feifei

With best regards, Slava

#20 lore raw

Re: [dpdk-dev] [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

.. snip..

Quoted 10 lines

I can understand you worry that if there is no 'wmb at last', when agent_1 leave the locked section, agent_2 still may observe unchanged global cache.

However, when agent_2 take a lock and get(new MR) in time slot 7(Fig.1), it means
agent_1 has finished updating global cache and lock is freed. Besides, if agent_2 can take a lock, it also shows agent_2 has observed the changed global cache.

Quoted 3 lines

This is because there is a store- release in rte_rwlock_read_unlock, store- release ensures all store operation before 'release' can be committed if store operation after 'release' is observed by other agents.

OK, I missed this implicit ordering hidden in the lock/unlock(), thank you for pointing me out that.

I checked the listings for rte_rwlock_write_unlock(), it is implemented with __atomic_store_n(&rwl->cnt, 0, __ATOMIC_RELEASE) and:
- on x86 there is nothing special in the code due to strict x86-64 ordering model
- on ARMv8 there is stlxr instruction, that implies the write barrier

Now you convinced me 😊, we can get rid of the explicit "wmb_at_last" in the code.
Please, provide the patch with clear commit message with details above,
it is important to mention:
- lock protection is involved, dev_gen and cache update ordering inside the protected section does not matter
- unlock() provides implicit write barrier due to atomic operation _ATOMIC_RELEASE

Also, in my opinion, there should be small comment in place of wmb being revomed, reminding we are in the protected section and unlock provides the implicit write barrier at the level visible by software.

Thank you for the meaningful discussion, I refreshed my knowledge of memory ordering models for different architectures 😊

With best regards,
Slava

Quoted 694 lines
Quoted 13 lines
Quoted 13 lines

Best Regards
Feifei

Quoted 8 lines

With best regards,
Slava

Quoted 4 lines

Best Regards
Feifei

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月7日 18:15
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 93 lines
Quoted 54 lines
Quoted 42 lines

Wang

Quoted 41 lines
Quoted 41 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

We should consider the locks in your scenario - it is crucial for the complete model description:

How agent_1 (in your terms) rebuilds global cache:

1a) lock()
1b) rebuild(global cache)
1c) update(dev_gen)
1d) wmb()
1e) unlock()

How agent_2 checks:

2a) check(dev_gen) (assume positive - changed) 2b) clear(local_cache)
2c) miss(on empty local_cache) - > eventually it goes to mr_lookup_caches()
2d) lock()
2e) get(new MR)
2f) unlock()
2g) update(local cache with obtained new MR)

Hence, even if 1c) becomes visible in 2a) before 1b) committed (say, due to out-of-order Arch) - the agent 2 would be blocked on
2d) and scenario depicted on your Fig2 would not happen (agent_2 will wait before step 3 till agent 1 unlocks after its step 5).

With best regards,
Slava

Quoted 4 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Friday, May 7, 2021 9:36> To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>;

Shahaf

Quoted 11 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your reply.

Quoted 25 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月6日 19:22
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

Wang

Quoted 17 lines
Quoted 4 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 8 lines

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb.
We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to ensure this.

1. For the first problem why we should get rid of the last wmb and move it before dev_gen updated, I think our attention is how the wmb implements the synchronization between multiple

agents.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Fig1
------------------------------------------------------------
--
--
--

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

--
--
------------------------
-------
Timeslot agent_1 agent_2
1 rebuild global cache
2 wmb
3 update dev_gen ----------------------- load

Quoted 64 lines

changed

Quoted 63 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

dev_gen
4 rebuild local

cache

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 15 lines

------------------------------------------------------------
--
--
--
--
--
------------------------
-------

First, wmb is only for local thread to keep the order between local
write- write :
Based on the picture above, for agent_1, wmb keeps the order that rebuilding global cache is always before updating dev_gen.

Quoted 8 lines

Second, agent_1 communicates with agent_2 by the global variable "dev_gen" :
If agent_1 updates dev_gen, agent_2 will load it and then it knows it should rebuild local cache

Finally, agent_2 rebuilds local cache according to whether agent_1 has rebuilt global cache, and agent_2 knows this information by the variable

"dev_gen".

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Fig2
------------------------------------------------------------
--
--

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

--
--
--
------------------------
-------
Timeslot agent_1 agent_2
1 update dev_gen
2 load changed

dev_gen

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

3 rebuild local

cache

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

4 rebuild global cache
5 wmb
------------------------------------------------------------
--
--
--
--
--
------------------------
-------

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

However, in arm platform, if wmb is after dev_gen updated,

"dev_gen"

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

may be updated before agent_1 rebuilding global cache, then agent_2 maybe receive error message and rebuild its local cache in

advance.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

To summarize, it is not important which time other agents can see the changed global variable "dev_gen". (Actually, wmb after "dev_gen" cannot ensure changed

"dev_gen"

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

is committed to the global).
It is more important that if other agents see the changed "dev_gen", they also can know global cache has been updated.

Quoted 3 lines

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

We just move the last wmb into the right place, and not delete it for performance.

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Also, regarding this:

Quoted 1 line
Quoted 1 line

Another question suddenly occurred to me, in order to

keep the

Quoted 4 lines

order that rebuilding global cache before updating ”dev_gen“, the

Quoted 1 line

wmb should be before updating "dev_gen" rather than after

it.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

Otherwise, in the out-of-order platforms, current

order cannot be

kept.

Quoted 5 lines

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

Quoted 96 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

2. The second function of wmb before "dev_gen" updated is for performance according to our previous discussion. According to Fig2, if there is no wmb between "global cache

updated"

Quoted 92 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

and "dev_gen updated", "dev_gen" may update before global cache

updated.

Quoted 4 lines

Then agent_2 may see the changed "dev_gen" and flush entire local cache in advance.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

This entire flush can degrade the performance:
"the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in data path on acquiring global

cache lock."

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
Quoted 25 lines

Furthermore, spinlock is just for global cache, not for dev_gen and local cache.

Quoted 8 lines

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling,
could you, please, elaborate? I'm not sure we should
maintain an order (due to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

At last, for my view, there are two functions that moving wmb before "dev_gen"
for the write-write order:
--------------------------------
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen -------------------------------- 1. Achieve synchronization between multiple threads in the right way 2. Prevent other agents from flushing local cache early to ensure performance

Quoted 14 lines

Best Regards
Feifei

Quoted 11 lines

With best regards,
Slava

Quoted 8 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch? For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>; Matan

Azrad

Quoted 17 lines
Quoted 17 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

<[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 14 lines
Quoted 14 lines
Quoted 4 lines

Wang

Quoted 3 lines
Quoted 3 lines

<[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

cache

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and

write.

Quoted 1 line
Quoted 1 line

and it cannot tell the CPU when it should commit the

changes.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 10 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

flushed.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible". The "visible" means when updating guard variable

"dev_gen"

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 1 line
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 92 lines
Quoted 92 lines
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 33 lines
Quoted 27 lines

nd

Quoted 26 lines
Quoted 16 lines
Quoted 16 lines

<[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering
dev_gen, to ensure the correct order
- after altering dev_gen to make this change
visible for other agents and to trigger local cache update

With best regards,
Slava

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 5 lines
Quoted 5 lines

nd

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Hi, Slava

Another question suddenly occurred to me, in order to keep the order that rebuilding global cache before updating ”dev_gen“, the wmb should be before updating

"dev_gen"

Quoted 17 lines
Quoted 17 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

rather

than after it.

Quoted 15 lines
Quoted 15 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Otherwise, in the out-of-order platforms, current order cannot be

kept.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>;

Matan

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 3 lines

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug
for Memory

Region

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs are
returned to the memory pool, and then it can
free this mbufs, I agree with

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

this.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

As a result, I will remove the bug fix patch
from this series and just replace the smp
barrier with
C11 thread fence. Thanks very much for your
patient explanation

again.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Best Regards
Feifei

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>;

Matan

Quoted 58 lines
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Ruifeng

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Wang

Quoted 8 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild
bug for Memory Region cache

Hi, Feifei

Please, see below

Quoted 43 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

....

Hi, Feifei

Sorry, I do not follow what this patch fixes.
Do we have some issue/bug with MR cache in

practice?

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

This patch fixes the bug which is based on
logical deduction, and it doesn't actually happen.

Each Tx queue has its own dedicated "local"
cache for MRs to convert buffer address in
mbufs being transmitted to LKeys
(HW-related entity
handle) and the "global" cache for all MR
registered on the

device.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush
request
- lookup in local cache
- if not found:
- acquire lock for global cache read
access
- lookup in global cache
- release lock for global cache

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

How cache update on memory
freeing/unregistering

happens:

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

- acquire lock for global cache write
access
- [a] remove relevant MRs from the global
cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch
swaps [a] and [b], and local caches flush
is requested

Quoted 65 lines

earlier.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

What problem does it

solve?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

It is not supposed there are in datapath
some mbufs referencing to the memory being

freed.

Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 11 lines
Quoted 11 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Application must ensure this and must not
allocate new mbufs from this memory
regions

being freed.

Quoted 2 lines
Quoted 2 lines

Hence, the lookups for these MRs in caches
should not

occur.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

For your first point that, application can
take charge of preventing MR freed memory
being allocated to

data path.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Does it means that If there is an emergency
of MR fragment, such as hotplug, the
application must inform thedata path in
advance, and this memory will not be
allocated, and then the control path will
free this memory? If application can do
like this, I agree that this bug

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

cannot happen.

Actually, this is the only correct way for
application to

operate.

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 21 lines
Quoted 21 lines
Quoted 2 lines
Quoted 2 lines

Let's suppose we have some memory area that
application wants to

free.

Quoted 18 lines
Quoted 4 lines

ALL references to this area must be removed.
If we have some mbufs allocated from this
area, it means that we have memory pool
created

there.

Quoted 13 lines

What application should do:
- notify all its components/agents the memory
area is going to be freed
- all components/agents free the mbufs they
might own
- PMD might not support freeing for some mbufs
(for example being sent and awaiting for
completion), so app should just wait
- wait till all mbufs are returned to the
memory pool (by monitoring available obj ==
pool size)

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Otherwise - it is dangerous to free the memory.
There are just some mbufs still allocated, it
is regardless to buf address to MR
translation. We just can't free the memory -
the mapping will be destroyed and might cause
the segmentation fault by SW or some HW issues
on DMA access to unmapped memory. It is very
generic safety approach - do not free the
memory that is still in

use.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Hence, at the moment of freeing and
unregistering the MR, there MUST BE NO any
mbufs in flight referencing to the addresses being

freed.

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

No translation to MR being invalidated can happen.

For other side, the cache flush has
negative effect
- the local cache is getting empty and
can't provide translation for other valid
(not being
removed) MRs, and the translation has to
look up in the global cache, that is
locked now for rebuilding, this causes the
delays in datapatch

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

on acquiring global cache lock.
So, I see some potential performance impact.

If above assumption is true, we can go to
your second

point.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

I think this is a problem of the tradeoff
between cache coherence and
performance.

I can understand your meaning that though
global cache has been changed, we should
keep the valid MR in local cache as long as
possible to ensure the fast

searching speed.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In the meanwhile, the local cache can be
rebuilt later to reduce its waiting time for
acquiring the global

cache lock.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

However, this mechanism just ensures the
performance unchanged for the first few mbufs.
During the next mbufs lkey searching after

'dev_gen'

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

updated, it is still necessary to update the local

cache.

Quoted 45 lines
Quoted 45 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

And the performance can firstly reduce and
then

returns.

Quoted 42 lines
Quoted 42 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

Thus, no matter whether there is this patch
or not, the performance will jitter in a
certain period of

time.

Quoted 3 lines

Local cache should be updated to remove MRs no
longer

valid.

Quoted 34 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and not
valid MRX in local

cache.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

And there are traffic in the datapath for MR0
and MR1, and no traffic for MRX anymore.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all MR0,
MR1, MRX will be removed on translation in
datapath
c) update global cache,
d) free lock
All the traffic for valid MR0, MR1 ALWAYS will
be blocked on lock taken for cache update
since point
b) till point

d).

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock
The traffic MIGHT be locked ONLY for MRs
non-existing in local cache (not happens for
MR0 and MR1, must not happen for MRX), and
probability should be minor. And lock might
happen since
c) till
d)
- quite short period of time

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR translation
after

b),

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

2) lock MIGHT happen, for cache miss ONLY,
after
c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be much

shorter.

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 8 lines
Quoted 8 lines

Finally, in conclusion, I tend to think that
the bottom layer can do more things to
ensure the correct execution of the program,
which may have a negative impact on the
performance in a short time, but in the long
run, the performance will eventually

come back.

Quoted 21 lines
Quoted 21 lines

Furthermore, maybe we should pay attention
to the performance in the stable period, and
try our best to ensure the correctness of
the program in case of
emergencies.

If we have some mbufs still allocated in
memory being freed
- there is nothing to say about correctness,
it is totally incorrect. In my opinion, we
should not think how to mitigate this
incorrect behavior, we should not encourage
application developers to follow the wrong
approaches.

With best regards, Slava

Best Regards Feifei

With best regards, Slava

#21 lore raw

[dpdk-dev] 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Quoted 36 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年5月13日 18:49
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
抄送: [email protected]; nd <[email protected]>; [email protected]; Ruifeng Wang <[email protected]>; nd <[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

.. snip..

Quoted 3 lines

I can understand you worry that if there is no 'wmb at last', when agent_1 leave the locked section, agent_2 still may observe unchanged

global cache.

Quoted 10 lines

However, when agent_2 take a lock and get(new MR) in time slot 7(Fig.1), it means
agent_1 has finished updating global cache and lock is freed. Besides, if agent_2 can take a lock, it also shows agent_2 has observed the changed global cache.

This is because there is a store- release in rte_rwlock_read_unlock, store- release ensures all store operation before 'release' can be committed if store operation after 'release' is observed by other agents.

OK, I missed this implicit ordering hidden in the lock/unlock(), thank you for pointing me out that.

I checked the listings for rte_rwlock_write_unlock(), it is implemented with __atomic_store_n(&rwl->cnt, 0, __ATOMIC_RELEASE) and:
- on x86 there is nothing special in the code due to strict x86-64 ordering model
- on ARMv8 there is stlxr instruction, that implies the write barrier

Quoted 8 lines

Now you convinced me 😊, we can get rid of the explicit "wmb_at_last" in the code.
Please, provide the patch with clear commit message with details above, it is important to mention:
- lock protection is involved, dev_gen and cache update ordering inside the protected section does not matter
- unlock() provides implicit write barrier due to atomic operation _ATOMIC_RELEASE

Ok, I will be careful with the commit message and include above details in it.

Quoted 5 lines

Also, in my opinion, there should be small comment in place of wmb being revomed, reminding we are in the protected section and unlock provides the implicit write barrier at the level visible by software.

Yes, it is necessary to add small comment in the place of wmb to explain why we do this.

Quoted 3 lines

Thank you for the meaningful discussion, I refreshed my knowledge of memory ordering models for different architectures 😊

Also thanks very much for this discussion, I think I learn a lot from it and enjoy exploring the unknown problems.
Looking forward to your review about the next version, thanks a lot 😊.

Best Regards
Feifei

Quoted 718 lines

With best regards,
Slava

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

Best Regards
Feifei

Quoted 8 lines

With best regards,
Slava

Quoted 4 lines

Best Regards
Feifei

Quoted 82 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月7日 18:15
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 75 lines
Quoted 75 lines
Quoted 42 lines

Wang

Quoted 41 lines
Quoted 41 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

We should consider the locks in your scenario - it is crucial for the complete model description:

How agent_1 (in your terms) rebuilds global cache:

1a) lock()
1b) rebuild(global cache)
1c) update(dev_gen)
1d) wmb()
1e) unlock()

How agent_2 checks:

2a) check(dev_gen) (assume positive - changed) 2b) clear(local_cache)
2c) miss(on empty local_cache) - > eventually it goes to mr_lookup_caches()
2d) lock()
2e) get(new MR)
2f) unlock()
2g) update(local cache with obtained new MR)

Hence, even if 1c) becomes visible in 2a) before 1b) committed (say, due to out-of-order Arch) - the agent 2 would be blocked on
2d) and scenario depicted on your Fig2 would not happen (agent_2 will wait before step 3 till agent 1 unlocks after its step 5).

With best regards,
Slava

Quoted 4 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Friday, May 7, 2021 9:36> To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>;

Shahaf

Quoted 18 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Thanks very much for your reply.

Quoted 6 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]> 发送时间: 2021年5月6日 19:22
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; [email protected];

Ruifeng

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

Wang

Quoted 12 lines
Quoted 12 lines

<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Feifei

Sorry, I do not follow why we should get rid of the last (after dev_gen update) wmb.
We've rebuilt the global cache, we should notify other agents it's happened and they should flush local caches. So, dev_gen change should be made visible to other agents to trigger this activity and the second wmb is here to

ensure this.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

1. For the first problem why we should get rid of the last wmb and move it before dev_gen updated, I think our attention is how the wmb implements the synchronization between multiple

agents.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Fig1
----------------------------------------------------------
--
--
--
--
--
--
------------------------
-------
Timeslot agent_1

agent_2

Quoted 17 lines
Quoted 17 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

1 rebuild global cache
2 wmb
3 update dev_gen ----------------------- load

changed

Quoted 13 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

dev_gen
4 rebuild local

cache

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

----------------------------------------------------------
--
--
--
--
--
--
------------------------
-------

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

First, wmb is only for local thread to keep the order between local
write- write :
Based on the picture above, for agent_1, wmb keeps the order that rebuilding global cache is always before updating

dev_gen.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

Second, agent_1 communicates with agent_2 by the global variable "dev_gen" :
If agent_1 updates dev_gen, agent_2 will load it and then it knows it should rebuild local cache

Finally, agent_2 rebuilds local cache according to whether agent_1 has rebuilt global cache, and agent_2 knows this information by the variable

"dev_gen".

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Fig2
----------------------------------------------------------
--
--
--
--
--
--
------------------------
-------
Timeslot agent_1

agent_2

Quoted 43 lines
Quoted 23 lines
Quoted 23 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

1 update dev_gen
2 load changed

dev_gen

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

3 rebuild local

cache

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

4 rebuild global cache
5 wmb
----------------------------------------------------------
--
--
--
--
--
--
------------------------
-------

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

However, in arm platform, if wmb is after dev_gen updated,

"dev_gen"

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

may be updated before agent_1 rebuilding global cache, then
agent_2 maybe receive error message and rebuild its local cache in

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

advance.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

To summarize, it is not important which time other agents can see the changed global variable "dev_gen". (Actually, wmb after "dev_gen" cannot ensure changed

"dev_gen"

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

is committed to the global).
It is more important that if other agents see the changed "dev_gen", they also can know global cache has been updated.

Quoted 3 lines

One more point, due to registering new/destroying existing MR involves FW (via kernel) calls, it takes so many CPU cycles that we could neglect wmb overhead at all.

We just move the last wmb into the right place, and not delete it for performance.

Quoted 3 lines

Also, regarding this:

Quoted 96 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line

Another question suddenly occurred to me, in order

to keep the

Quoted 5 lines

order that rebuilding global cache before updating ”dev_gen“, the

Quoted 2 lines

wmb should be before updating "dev_gen" rather than after

it.

Quoted 88 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line

Otherwise, in the out-of-order platforms, current

order cannot be

kept.

Quoted 5 lines

it is not clear why ordering is important - global cache update and dev_gen change happen under spinlock protection, so only the last wmb is meaningful.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

2. The second function of wmb before "dev_gen" updated is for performance according to our previous discussion. According to Fig2, if there is no wmb between "global cache

updated"

Quoted 74 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

and "dev_gen updated", "dev_gen" may update before global cache

updated.

Quoted 4 lines

Then agent_2 may see the changed "dev_gen" and flush entire local cache in advance.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

This entire flush can degrade the performance:
"the local cache is getting empty and can't provide translation for other valid (not being removed) MRs, and the translation has to look up in the global cache, that is locked now for rebuilding, this causes the delays in data path on acquiring global

cache lock."

Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 25 lines

Furthermore, spinlock is just for global cache, not for dev_gen and local cache.

Quoted 8 lines

To summarize, in my opinion:
- if you see some issue with ordering of global cache update/dev_gen signalling,
could you, please, elaborate? I'm not sure we should
maintain an order (due to spinlock protection)
- the last rte_smp_wmb() after dev_gen incrementing should be kept intact

At last, for my view, there are two functions that moving wmb before "dev_gen"
for the write-write order:
--------------------------------
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen -------------------------------- 1. Achieve synchronization between multiple threads in the right way 2. Prevent other agents from flushing local cache early to ensure performance

Quoted 14 lines

Best Regards
Feifei

Quoted 11 lines

With best regards,
Slava

Quoted 8 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Thursday, May 6, 2021 5:52
To: Slava Ovsiienko <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; nd <[email protected]>; [email protected]; Ruifeng

Wang

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

<[email protected]>; nd <[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory Region cache

Hi, Slava

Would you have more comments about this patch? For my sight, only one wmb before "dev_gen" updating is enough to synchronize.

Thanks very much for your attention.

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 16:42
收件人: Slava Ovsiienko <[email protected]>;

Matan

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 411 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Ruifeng

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

Wang

Quoted 3 lines
Quoted 3 lines

<[email protected]>; nd <[email protected]>
主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild bug for Memory

Region

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

cache

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

Hi, Slava

I think the second wmb can be removed.
As I know, wmb is just a barrier to keep the order between write and

write.

Quoted 1 line
Quoted 1 line

and it cannot tell the CPU when it should commit the

changes.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines

It is usually used before guard variable to keep the order that updating guard variable after some changes, which you want to release,

have been done.

Quoted 10 lines

For example, for the wmb after global cache update/before altering dev_gen, it can ensure the order that updating global cache before altering dev_gen:
1)If other agent load the changed "dev_gen", it can know the global cache has been updated.
2)If other agents load the unchanged, "dev_gen", it means the global cache has not been updated, and the local cache will not be

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

flushed.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

As a result, we use wmb and guard variable "dev_gen" to ensure the global cache updating is "visible". The "visible" means when updating guard variable

"dev_gen"

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

is known by other agents, they also can confirm global cache has been updated in the meanwhile. Thus, just one wmb before altering dev_gen can ensure

this.

Quoted 1 line
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Best Regards
Feifei

Quoted 4 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年4月20日 15:54
收件人: Feifei Wang <[email protected]>; Matan

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 91 lines
Quoted 91 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 32 lines
Quoted 26 lines

nd

Quoted 25 lines
Quoted 15 lines
Quoted 15 lines

<[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild bug
for Memory Region cache

Hi, Feifei

In my opinion, there should be 2 barriers:
- after global cache update/before altering
dev_gen, to ensure the correct order
- after altering dev_gen to make this change
visible for other agents and to trigger local
cache update

With best regards, Slava

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, April 20, 2021 10:30
To: Slava Ovsiienko <[email protected]>;
Matan Azrad <[email protected]>; Shahaf Shuler
<[email protected]>
Cc: [email protected]; nd <[email protected]>;
[email protected]; Ruifeng

Wang

Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>;

Quoted 5 lines
Quoted 5 lines

nd

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

<[email protected]>
Subject: 回复: [PATCH v1 3/4] net/mlx5: fix
rebuild bug for Memory Region cache

Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Hi, Slava

Another question suddenly occurred to me, in
order to keep the order that rebuilding global
cache before updating ”dev_gen“, the wmb should
be before updating

"dev_gen"

Quoted 17 lines
Quoted 17 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

rather

than after it.

Quoted 15 lines
Quoted 15 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Otherwise, in the out-of-order platforms,
current order cannot be

kept.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thus, we should change the code as:
a) rebuild global cache;
b) rte_smp_wmb();
c) updating dev_gen

Best Regards
Feifei
-----邮件原件-----
发件人: Feifei Wang
发送时间: 2021年4月20日 13:54
收件人: Slava Ovsiienko <[email protected]>;

Matan

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 3 lines

Ruifeng

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Wang

Quoted 1 line

<[email protected]>; nd <[email protected]>; nd

<[email protected]>

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 2 lines
Quoted 2 lines

主题: 回复: [PATCH v1 3/4] net/mlx5: fix rebuild
bug for Memory

Region

Quoted 9 lines
Quoted 9 lines

cache

Hi, Slava

Thanks very much for your explanation.

I can understand the app can wait all mbufs
are returned to the memory pool, and then it
can free this mbufs, I agree with

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

this.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

As a result, I will remove the bug fix patch
from this series and just replace the smp
barrier with
C11 thread fence. Thanks very much for your
patient explanation

again.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Best Regards
Feifei

-----邮件原件-----
发件人: Slava Ovsiienko
<[email protected]>
发送时间: 2021年4月20日 2:51
收件人: Feifei Wang <[email protected]>;

Matan

Quoted 59 lines
Quoted 15 lines
Quoted 15 lines
Quoted 4 lines

Azrad

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

<[email protected]>; Shahaf Shuler
<[email protected]>
抄送: [email protected]; nd <[email protected]>;

[email protected];

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Ruifeng

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Wang
<[email protected]>; nd <[email protected]>
主题: RE: [PATCH v1 3/4] net/mlx5: fix rebuild
bug for Memory Region cache

Hi, Feifei

Please, see below

Quoted 44 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

....

Hi, Feifei

Sorry, I do not follow what this patch fixes.
Do we have some issue/bug with MR cache
in

practice?

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

This patch fixes the bug which is based on
logical deduction, and it doesn't actually happen.

Each Tx queue has its own dedicated "local"
cache for MRs to convert buffer address
in mbufs being transmitted to LKeys
(HW-related entity
handle) and the "global" cache for all
MR registered on the

device.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

AFAIK, how conversion happens in datapath:
- check the local queue cache flush
request
- lookup in local cache
- if not found:
- acquire lock for global cache read
access
- lookup in global cache
- release lock for global cache

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

How cache update on memory
freeing/unregistering

happens:

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

- acquire lock for global cache write
access
- [a] remove relevant MRs from the
global cache
- [b] set local caches flush request
- free global cache lock

If I understand correctly, your patch
swaps [a] and [b], and local caches
flush is requested

Quoted 67 lines

earlier.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

What problem does it

solve?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

It is not supposed there are in datapath
some mbufs referencing to the memory
being

freed.

Quoted 60 lines
Quoted 60 lines
Quoted 60 lines
Quoted 11 lines
Quoted 11 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Application must ensure this and must
not allocate new mbufs from this memory
regions

being freed.

Quoted 2 lines

Hence, the lookups for these MRs in
caches should not

occur.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

For your first point that, application can
take charge of preventing MR freed memory
being allocated to

data path.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Does it means that If there is an
emergency of MR fragment, such as hotplug,
the application must inform thedata path
in advance, and this memory will not be
allocated, and then the control path will
free this memory? If application can do
like this, I agree that this bug

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

cannot happen.

Actually, this is the only correct way for
application to

operate.

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 21 lines
Quoted 21 lines
Quoted 2 lines

Let's suppose we have some memory area that
application wants to

free.

Quoted 18 lines

ALL references to this area must be removed.
If we have some mbufs allocated from this
area, it means that we have memory pool
created
there.

What application should do:
- notify all its components/agents the
memory area is going to be freed
- all components/agents free the mbufs they
might own
- PMD might not support freeing for some
mbufs (for example being sent and awaiting
for completion), so app should just wait
- wait till all mbufs are returned to the
memory pool (by monitoring available obj ==
pool size)

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Otherwise - it is dangerous to free the memory.
There are just some mbufs still allocated,
it is regardless to buf address to MR
translation. We just can't free the memory -
the mapping will be destroyed and might
cause the segmentation fault by SW or some
HW issues on DMA access to unmapped memory.
It is very generic safety approach - do not
free the memory that is still in

use.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Hence, at the moment of freeing and
unregistering the MR, there MUST BE NO any
mbufs in flight referencing to the addresses
being

freed.

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

No translation to MR being invalidated can happen.

For other side, the cache flush has
negative effect
- the local cache is getting empty and
can't provide translation for other
valid (not being
removed) MRs, and the translation has to
look up in the global cache, that is
locked now for rebuilding, this causes
the delays in datapatch

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

on acquiring global cache lock.
So, I see some potential performance impact.

If above assumption is true, we can go to
your second

point.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

I think this is a problem of the tradeoff
between cache coherence and
performance.

I can understand your meaning that though
global cache has been changed, we should
keep the valid MR in local cache as long
as possible to ensure the fast

searching speed.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In the meanwhile, the local cache can be
rebuilt later to reduce its waiting time
for acquiring the global

cache lock.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

However, this mechanism just ensures the
performance unchanged for the first few mbufs.
During the next mbufs lkey searching after

'dev_gen'

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

updated, it is still necessary to update
the local

cache.

Quoted 44 lines
Quoted 44 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

And the performance can firstly reduce and
then

returns.

Quoted 41 lines
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Thus, no matter whether there is this
patch or not, the performance will jitter
in a certain period of
time.

Local cache should be updated to remove MRs
no longer

valid.

Quoted 33 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

But we just flush the entire cache.
Let's suppose we have valid MR0, MR1, and
not valid MRX in local

cache.

Quoted 3 lines
Quoted 3 lines

And there are traffic in the datapath for
MR0 and MR1, and no traffic for MRX anymore.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

1) If we do as you propose:
a) take a lock
b) request flush local cache first - all
MR0, MR1, MRX will be removed on translation
in datapath
c) update global cache,
d) free lock All the traffic for valid MR0,
MR1 ALWAYS will be blocked on lock taken for
cache update since point
b) till point

d).

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

2) If we do as it is implemented now:
a) take a lock
b) update global cache
c) request flush local cache
d) free lock The traffic MIGHT be locked
ONLY for MRs non-existing in local cache
(not happens for
MR0 and MR1, must not happen for MRX), and
probability should be minor. And lock might
happen since
c) till
d)
- quite short period of time

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Summary, the difference between 1) and 2)

Lock probability:
- 1) lock ALWAYS happen for ANY MR
translation after

b),

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

2) lock MIGHT happen, for cache miss ONLY,
after
c)

Lock duration:
- 1) lock since b) till d),
2) lock since c) till d), that seems to be
much

shorter.

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 9 lines

Finally, in conclusion, I tend to think
that the bottom layer can do more things
to ensure the correct execution of the
program, which may have a negative impact
on the performance in a short time, but in
the long run, the performance will
eventually

come back.

Quoted 21 lines

Furthermore, maybe we should pay attention
to the performance in the stable period,
and try our best to ensure the correctness
of the program in case of
emergencies.

If we have some mbufs still allocated in
memory being freed
- there is nothing to say about correctness,
it is totally incorrect. In my opinion, we
should not think how to mitigate this
incorrect behavior, we should not encourage
application developers to follow the wrong
approaches.

With best regards, Slava

Best Regards Feifei

With best regards, Slava

#22 lore raw

PATCH 4/4 net/mlx5: replace SMP barriers with C11 barriers

In reply to Feifei Wang

Replace SMP barrier with atomic thread fence.

---
 drivers/net/mlx5/mlx5_mr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/mlx5/mlx5_mr.c +4 -4
index 7ce1d3e64..650fe9093 100644
@@ -109,11 +109,11 @@ mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh,
 
 		/*
 		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is to keep the order that dev_gen updated before
+		 * release-fence is to keep the order that dev_gen updated before
 		 * rebuilding global cache. Therefore, other core can flush their
 		 * local cache on time.
 		 */
-		rte_smp_wmb();
+		rte_atomic_thread_fence(__ATOMIC_RELEASE);
 		mlx5_mr_rebuild_cache(&sh->share_cache);
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
@@ -412,11 +412,11 @@ mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr,
 
 	/*
 	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is to keep the order that dev_gen updated before
+	 * release-fence is to keep the order that dev_gen updated before
 	 * rebuilding global cache. Therefore, other core can flush their
 	 * local cache on time.
 	 */
-	rte_smp_wmb();
+	rte_atomic_thread_fence(__ATOMIC_RELEASE);
 	mlx5_mr_rebuild_cache(&sh->share_cache);
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
Signature · 1 line
2.25.1
Reviewed-by on the v1 series
#23 lore raw
In reply to Feifei Wang

Looks good to me. Thank you for fixing this bug for us.

Quoted 21 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Feifei Wang
Sent: Thursday, March 18, 2021 3:19
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>
Subject: [dpdk-dev] [PATCH v1 0/4] refactor SMP barriers for net/mlx

For net/mlx4 and net/mlx5, fix cache rebuild bug and replace SMP barriers with atomic fence.

Feifei Wang (4):
net/mlx4: fix rebuild bug for Memory Region cache net/mlx4: replace SMP barrier with C11 barriers net/mlx5: fix rebuild bug for Memory Region cache net/mlx5: replace SMP barriers with C11 barriers

 drivers/net/mlx4/mlx4_mr.c | 21 +++++++++----------
 drivers/net/mlx5/mlx5_mr.c | 41 ++++++++++++++++++--------------------
 2 files changed, 28 insertions(+), 34 deletions(-)

--
2.25.1

#24 lore raw

v2 cover remove wmb for net/mlx

In reply to Feifei Wang

For net/mlx4 and net/mlx5, remove unnecessary wmb for Memory Region cache.

v2:
1. keep the order of dev_gen and global cache (Slava Ovsiienko)
2. remove the wmb at last instead of moving it forward
3. remove atomic_thread_fence patches

Feifei Wang (2):
net/mlx4: remove unnecessary wmb for Memory Region cache net/mlx5: remove unnecessary wmb for Memory Region cache

 drivers/net/mlx4/mlx4_mr.c | 13 +++++--------
 drivers/net/mlx5/mlx5_mr.c | 26 ++++++++++----------------
 2 files changed, 15 insertions(+), 24 deletions(-)
Signature · 1 line
2.25.1
#25 lore raw

PATCH v2 1/2 net/mlx4: remove unnecessary wmb for Memory Region cache

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt. It is unnecessary to add write memory barrier (wmb) before or after its updating for synchronization.

This is due to MR cache's R/W lock can maintain synchronization between threads:
1. dev_gen and global cache update ordering inside the lock protected section does not matter. Because other threads cannot take the lock until global cache has been updated. Thus, in out of order platform, even if other agents firstly observed updated dev_gen but global does not update, they also needs to wait the lock. As a result, it is unnecessary to add a wmb between rebuiling global cache and updating dev_gen to keep the order of rebuilding global cache and updating dev_gen.

2. Store-Release of unlock can provide the implicit wmb at the level visible by software. This makes 'rebuiling global cache' and 'updating dev_gen' be observed before local_cache starts to be updated by other agents. Thus, wmb after 'updating dev_gen' can be removed.

---
 drivers/net/mlx4/mlx4_mr.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/net/mlx4/mlx4_mr.c +5 -8
index 6b2f0cf187..9a396f5729 100644
@@ -948,18 +948,15 @@ mlx4_mr_mem_event_free_cb(struct rte_eth_dev *dev, const void *addr, size_t len)
 	if (rebuild) {
 		mr_rebuild_dev_cache(dev);
 		/*
-		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If one of
-		 * freed memsegs is seen by other core, that means the memseg
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other core
-		 * before the core sees the newly allocated memory.
+		 * No wmb is needed after updating dev_gen due to store-release of
+		 * unlock can provide the implicit wmb at the level visible by
+		 * software. This makes rebuilt global cache and updated dev_gen
+		 * be observed when local_cache starts to be updating by other
+		 * agents.
 		 */
 		++priv->mr.dev_gen;
 		DEBUG("broadcasting local cache flush, gen=%d",
 		      priv->mr.dev_gen);
-		rte_smp_wmb();
 	}
 	rte_rwlock_write_unlock(&priv->mr.rwlock);
 #ifdef RTE_LIBRTE_MLX4_DEBUG
Signature · 1 line
2.25.1
#26 lore raw

PATCH v2 2/2 net/mlx5: remove unnecessary wmb for Memory Region cache

In reply to Feifei Wang

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt. It is unnecessary to add write memory barrier (wmb) before or after its updating for synchronization.

This is due to MR cache's R/W lock can maintain synchronization between threads:
1. dev_gen and global cache update ordering inside the lock protected section does not matter. Because other threads cannot take the lock until global cache has been updated. Thus, in out of order platform, even if other agents firstly observed updated dev_gen but global does not update, they also needs to wait the lock. As a result, it is unnecessary to add a wmb between rebuiling global cache and updating dev_gen to keep the order of rebuilding global cache and updating dev_gen.

2. Store-Release of unlock can provide the implicit wmb at the level visible by software. This makes 'rebuiling global cache' and 'updating dev_gen' be observed before local_cache starts to be updated by other agents. Thus, wmb after 'updating dev_gen' can be removed.

---
 drivers/net/mlx5/mlx5_mr.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
drivers/net/mlx5/mlx5_mr.c +10 -16
index e791b6338d..85e5865050 100644
@@ -107,18 +107,15 @@ mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh,
 	if (rebuild) {
 		mlx5_mr_rebuild_cache(&sh->share_cache);
 		/*
-		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If one of
-		 * freed memsegs is seen by other core, that means the memseg
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other core
-		 * before the core sees the newly allocated memory.
+		 * No wmb is needed after updating dev_gen due to store-release of
+		 * unlock can provide the implicit wmb at the level visible by
+		 * software. This makes rebuilt global cache and updated dev_gen
+		 * be observed when local_cache starts to be updating by other
+		 * agents.
 		 */
 		++sh->share_cache.dev_gen;
 		DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
 		      sh->share_cache.dev_gen);
-		rte_smp_wmb();
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -411,18 +408,15 @@ mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr,
 	      (void *)mr);
 	mlx5_mr_rebuild_cache(&sh->share_cache);
 	/*
-	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If one of
-	 * freed memsegs is seen by other core, that means the memseg
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other core
-	 * before the core sees the newly allocated memory.
+	 * No wmb is needed after updating dev_gen due to store-release of
+	 * unlock can provide the implicit wmb at the level visible by
+	 * software. This makes rebuilt global cache and updated dev_gen
+	 * be observed when local_cache starts to be updating by other
+	 * agents.
 	 */
 	++sh->share_cache.dev_gen;
 	DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
 	      sh->share_cache.dev_gen);
-	rte_smp_wmb();
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
Signature · 1 line
2.25.1
#27 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

Hi, Feifei

Thanks you for the patch.
Please, see my notes below about typos and minor commit message rewording.

Quoted 10 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Monday, May 17, 2021 13:00
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>; Ruifeng Wang <[email protected]>
Subject: [PATCH v2 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

Quoted 4 lines

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt. It is unnecessary to add write memory barrier (wmb) before or after its updating for synchronization.

Would it be better "to trigger all cores to flush their local caches once the global MR cache has been rebuilt" ?

Quoted 2 lines

This is due to MR cache's R/W lock can maintain synchronization between threads:

I would add empty line here.

Quoted 5 lines

1. dev_gen and global cache update ordering inside the lock protected section does not matter. Because other threads cannot take the lock until global cache has been updated. Thus, in out of order platform, even if other agents firstly observed updated dev_gen but global does not update, they also needs to wait the lock. As a result, it is unnecessary to add a wmb

Type: "need" (no S) -> "have to" would be better ?

Quoted 1 line

between rebuiling global cache and updating dev_gen to keep the order

rebuiling -> rebuilDing
And let's reword a little bit?
"wmb between global cache rebuilding and updating the dev_gen to keep the memory store order."

Quoted 3 lines

rebuilding global cache and updating dev_gen.

2. Store-Release of unlock can provide the implicit wmb at the level visible by

can provide -> provides

Quoted 1 line

software. This makes 'rebuiling global cache' and 'updating dev_gen' be

Typo: rebuiling -> rebuilDing

Quoted 36 lines

observed before local_cache starts to be updated by other agents. Thus, wmb after 'updating dev_gen' can be removed.

---
 drivers/net/mlx5/mlx5_mr.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
drivers/net/mlx5/mlx5_mr.c index renamed from drivers/net/mlx5/mlx5_mr.c +0 -0

e791b6338d..85e5865050 100644

drivers/net/mlx5/mlx5_mr.c +0 -2
@@ -107,18 +107,15 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 	if (rebuild) {
 		mlx5_mr_rebuild_cache(&sh->share_cache);
 		/*
-		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means the

memseg

drivers/net/mlx5/mlx5_mr.c +0 -3
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * No wmb is needed after updating dev_gen due to store-

release of

drivers/net/mlx5/mlx5_mr.c +2 -0
+		 * unlock can provide the implicit wmb at the level visible by
+		 * software. This makes rebuilt global cache and updated

dev_gen

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * be observed when local_cache starts to be updating by

other

drivers/net/mlx5/mlx5_mr.c

Quoted 2 lines
drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * agents.
 		 */

Let's make comment a less wordy (and try to keep source code concise), what about this? "No explicit wmb is needed after updating dev_gen due to store-release ordering in unlock that provides the implicit barrier at the software visible level."

Quoted 14 lines
drivers/net/mlx5/mlx5_mr.c +0 -3
 		++sh->share_cache.dev_gen;
 		DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
 		      sh->share_cache.dev_gen);
-		rte_smp_wmb();
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -411,18 +408,15 @@ mlx5_dma_unmap(struct rte_pci_device *pdev,
 void *addr,
 	      (void *)mr);
 	mlx5_mr_rebuild_cache(&sh->share_cache);
 	/*
-	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If one of

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device *pdev, · line 416

Quoted 10 lines
drivers/net/mlx5/mlx5_mr.c +5 -5
-	 * freed memsegs is seen by other core, that means the memseg
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other core
-	 * before the core sees the newly allocated memory.
+	 * No wmb is needed after updating dev_gen due to store-release of
+	 * unlock can provide the implicit wmb at the level visible by
+	 * software. This makes rebuilt global cache and updated dev_gen
+	 * be observed when local_cache starts to be updating by other
+	 * agents.

The same as previous comment above.

Please, apply the same comments to the mlx4 patch:
http://patches.dpdk.org/project/dpdk/patch/[email protected]/

With best regards,
Slava

#28 lore raw

[dpdk-dev] 回复: [PATCH v2 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

Hi, Slava

Quoted 10 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年5月17日 22:15
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; Ruifeng Wang <[email protected]>
主题: RE: [PATCH v2 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

Quoted 5 lines

Hi, Feifei

Thanks you for the patch.
Please, see my notes below about typos and minor commit message rewording.

Thanks very much for your very careful reviewing.
I will apply these in the next version.

Quoted 11 lines
Quoted 11 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Monday, May 17, 2021 13:00
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>; Ruifeng Wang <[email protected]>
Subject: [PATCH v2 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

Quoted 7 lines
Quoted 4 lines

'dev_gen' is a variable to inform other cores to flush their local cache when global cache is rebuilt. It is unnecessary to add write memory barrier (wmb) before or after its updating for synchronization.

Would it be better "to trigger all cores to flush their local caches once the global MR cache has been rebuilt" ?

1.Yes, I think this can be more clear.

Quoted 4 lines
Quoted 3 lines

This is due to MR cache's R/W lock can maintain synchronization between
threads:

I would add empty line here.

2.Done.

Quoted 8 lines
Quoted 6 lines

1. dev_gen and global cache update ordering inside the lock protected section does not matter. Because other threads cannot take the lock until global cache has been updated. Thus, in out of order platform, even if other agents firstly observed updated dev_gen but global does not update, they also needs to wait the lock. As a result, it is unnecessary to add a wmb

Type: "need" (no S) -> "have to" would be better ?

3.Done.

Quoted 3 lines
Quoted 1 line

between rebuiling global cache and updating dev_gen to keep the order

rebuiling -> rebuilding

4.Done.

Quoted 4 lines

And let's reword a little bit?
"wmb between global cache rebuilding and updating the dev_gen to keep the memory store order."

5.Done.

Quoted 6 lines
Quoted 4 lines

rebuilding global cache and updating dev_gen.

2. Store-Release of unlock can provide the implicit wmb at the level visible by

can provide -> provides

6.Done.

Quoted 3 lines
Quoted 2 lines

software. This makes 'rebuiling global cache' and 'updating dev_gen' be

Typo: rebuiling -> rebuilding

7.Done.

Quoted 38 lines
Quoted 38 lines

observed before local_cache starts to be updated by other agents. Thus, wmb after 'updating dev_gen' can be removed.

---
 drivers/net/mlx5/mlx5_mr.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
drivers/net/mlx5/mlx5_mr.c +0 -0

index
e791b6338d..85e5865050 100644

drivers/net/mlx5/mlx5_mr.c +0 -2
@@ -107,18 +107,15 @@ mlx5_mr_mem_event_free_cb(struct
 mlx5_dev_ctx_shared *sh,
 	if (rebuild) {
 		mlx5_mr_rebuild_cache(&sh->share_cache);
 		/*
-		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If

one of

drivers/net/mlx5/mlx5_mr.c +0 -1
-		 * freed memsegs is seen by other core, that means the

memseg

drivers/net/mlx5/mlx5_mr.c +0 -3
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other

core

drivers/net/mlx5/mlx5_mr.c +1 -1
-		 * before the core sees the newly allocated memory.
+		 * No wmb is needed after updating dev_gen due to store-

release of

drivers/net/mlx5/mlx5_mr.c +2 -0
+		 * unlock can provide the implicit wmb at the level visible by
+		 * software. This makes rebuilt global cache and updated

dev_gen

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * be observed when local_cache starts to be updating by
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c

Quoted 3 lines

other

drivers/net/mlx5/mlx5_mr.c +1 -0
+		 * agents.
 		 */

Let's make comment a less wordy (and try to keep source code concise), what about this?
"No explicit wmb is needed after updating dev_gen due to store-release ordering in unlock that provides the implicit barrier at the software visible level."

8.That's better than before. A concise comment works better in the code.

Quoted 19 lines

drivers/net/mlx5/mlx5_mr.c mlx5_dma_unmap(struct rte_pci_device *pdev,

Quoted 9 lines
drivers/net/mlx5/mlx5_mr.c +0 -1
 		++sh->share_cache.dev_gen;
 		DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
 		      sh->share_cache.dev_gen);
-		rte_smp_wmb();
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -411,18 +408,15 @@ mlx5_dma_unmap(struct rte_pci_device *pdev,

void

Quoted 9 lines
drivers/net/mlx5/mlx5_mr.c +0 -5
 *addr,
 	      (void *)mr);
 	mlx5_mr_rebuild_cache(&sh->share_cache);
 	/*
-	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If one of
-	 * freed memsegs is seen by other core, that means the memseg
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
Quoted 8 lines

drivers/net/mlx5/mlx5_mr.c mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh, · line 114

Quoted 7 lines
drivers/net/mlx5/mlx5_mr.c +5 -2
-	 * generation below) will be guaranteed to be seen by other core
-	 * before the core sees the newly allocated memory.
+	 * No wmb is needed after updating dev_gen due to store-release of
+	 * unlock can provide the implicit wmb at the level visible by
+	 * software. This makes rebuilt global cache and updated dev_gen
+	 * be observed when local_cache starts to be updating by other
+	 * agents.

The same as previous comment above.

9.Done.

Quoted 5 lines

Please, apply the same comments to the mlx4 patch:
http://patches.dpdk.org/project/dpdk/patch/20210517100002.19905-2-
[email protected]/

10.Done.

Best Regards
Feifei

Quoted 2 lines

With best regards,
Slava

#29 lore raw

v3 cover remove wmb for net/mlx

In reply to Feifei Wang

For net/mlx4 and net/mlx5, remove unnecessary wmb for Memory Region cache.

v2:
1. keep the order of dev_gen and global cache (Slava Ovsiienko)
2. remove the wmb at last instead of moving it forward
3. remove atomic_thread_fence patches

v3:
1. commit message rewording (Slava Ovsiienko)

Feifei Wang (2):
net/mlx4: remove unnecessary wmb for Memory Region cache net/mlx5: remove unnecessary wmb for Memory Region cache

 drivers/net/mlx4/mlx4_mr.c | 11 +++--------
 drivers/net/mlx5/mlx5_mr.c | 22 ++++++----------------
 2 files changed, 9 insertions(+), 24 deletions(-)
Signature · 1 line
2.25.1
#30 lore raw

PATCH v3 1/2 net/mlx4: remove unnecessary wmb for Memory Region cache

'dev_gen' is a variable to trigger all cores to flush their local caches once the global MR cache has been rebuilt.

This is due to MR cache's R/W lock can maintain synchronization between threads:

1. dev_gen and global cache updating ordering inside the lock protected section does not matter. Because other threads cannot take the lock until global cache has been updated. Thus, in out of order platform, even if other agents firstly observe updated dev_gen but global does not update, they still have to wait the lock. As a result, it is unnecessary to add a wmb between global cache rebuilding and updating the dev_gen to keep the memory store order.

2. Store-Release of unlock provides the implicit wmb at the level visible by software. This makes 'rebuilding global cache' and 'updating dev_gen' be observed before local_cache starts to be updated by other agents. Thus, wmb after 'updating dev_gen' can be removed.

---
 drivers/net/mlx4/mlx4_mr.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/net/mlx4/mlx4_mr.c +3 -8
index 6b2f0cf187..2274b5df19 100644
@@ -948,18 +948,13 @@ mlx4_mr_mem_event_free_cb(struct rte_eth_dev *dev, const void *addr, size_t len)
 	if (rebuild) {
 		mr_rebuild_dev_cache(dev);
 		/*
-		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If one of
-		 * freed memsegs is seen by other core, that means the memseg
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other core
-		 * before the core sees the newly allocated memory.
+		 * No explicit wmb is needed after updating dev_gen due to
+		 * store-release ordering in unlock that provides the
+		 * implicit barrier at the software visible level.
 		 */
 		++priv->mr.dev_gen;
 		DEBUG("broadcasting local cache flush, gen=%d",
 		      priv->mr.dev_gen);
-		rte_smp_wmb();
 	}
 	rte_rwlock_write_unlock(&priv->mr.rwlock);
 #ifdef RTE_LIBRTE_MLX4_DEBUG
Signature · 1 line
2.25.1
Acked-by on v3 1/2
#31 lore raw

Re: [dpdk-dev] [PATCH v3 1/2] net/mlx4: remove unnecessary wmb for Memory Region cache

Quoted 24 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Feifei Wang
Sent: Tuesday, May 18, 2021 11:51
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>; Ruifeng Wang <[email protected]>
Subject: [dpdk-dev] [PATCH v3 1/2] net/mlx4: remove unnecessary wmb for Memory Region cache

'dev_gen' is a variable to trigger all cores to flush their local caches once the global MR cache has been rebuilt.

This is due to MR cache's R/W lock can maintain synchronization between threads:

1. dev_gen and global cache updating ordering inside the lock protected section does not matter. Because other threads cannot take the lock until global cache has been updated. Thus, in out of order platform, even if other agents firstly observe updated dev_gen but global does not update, they still have to wait the lock. As a result, it is unnecessary to add a wmb between global cache rebuilding and updating the dev_gen to keep the memory store order.

Quoted 8 lines

2. Store-Release of unlock provides the implicit wmb at the level visible by software. This makes 'rebuilding global cache' and 'updating dev_gen' be observed before local_cache starts to be updated by other agents. Thus, wmb after 'updating dev_gen' can be removed.

#32 lore raw

PATCH v3 2/2 net/mlx5: remove unnecessary wmb for Memory Region cache

In reply to Feifei Wang

'dev_gen' is a variable to trigger all cores to flush their local caches once the global MR cache has been rebuilt.

This is due to MR cache's R/W lock can maintain synchronization between threads:

1. dev_gen and global cache updating ordering inside the lock protected section does not matter. Because other threads cannot take the lock until global cache has been updated. Thus, in out of order platform, even if other agents firstly observe updated dev_gen but global does not update, they also have to wait the lock. As a result, it is unnecessary to add a wmb between global cache rebuilding and updating the dev_gen to keep the memory store order.

2. Store-Release of unlock provides the implicit wmb at the level visible by software. This makes 'rebuilding global cache' and 'updating dev_gen' be observed before local_cache starts to be updated by other agents. Thus, wmb after 'updating dev_gen' can be removed.

---
 drivers/net/mlx5/mlx5_mr.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
drivers/net/mlx5/mlx5_mr.c +6 -16
index e791b6338d..0c5403e493 100644
@@ -107,18 +107,13 @@ mlx5_mr_mem_event_free_cb(struct mlx5_dev_ctx_shared *sh,
 	if (rebuild) {
 		mlx5_mr_rebuild_cache(&sh->share_cache);
 		/*
-		 * Flush local caches by propagating invalidation across cores.
-		 * rte_smp_wmb() is enough to synchronize this event. If one of
-		 * freed memsegs is seen by other core, that means the memseg
-		 * has been allocated by allocator, which will come after this
-		 * free call. Therefore, this store instruction (incrementing
-		 * generation below) will be guaranteed to be seen by other core
-		 * before the core sees the newly allocated memory.
+		 * No explicit wmb is needed after updating dev_gen due to
+		 * store-release ordering in unlock that provides the
+		 * implicit barrier at the software visible level.
 		 */
 		++sh->share_cache.dev_gen;
 		DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
 		      sh->share_cache.dev_gen);
-		rte_smp_wmb();
 	}
 	rte_rwlock_write_unlock(&sh->share_cache.rwlock);
 }
@@ -411,18 +406,13 @@ mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr,
 	      (void *)mr);
 	mlx5_mr_rebuild_cache(&sh->share_cache);
 	/*
-	 * Flush local caches by propagating invalidation across cores.
-	 * rte_smp_wmb() is enough to synchronize this event. If one of
-	 * freed memsegs is seen by other core, that means the memseg
-	 * has been allocated by allocator, which will come after this
-	 * free call. Therefore, this store instruction (incrementing
-	 * generation below) will be guaranteed to be seen by other core
-	 * before the core sees the newly allocated memory.
+	 * No explicit wmb is needed after updating dev_gen due to
+	 * store-release ordering in unlock that provides the
+	 * implicit barrier at the software visible level.
 	 */
 	++sh->share_cache.dev_gen;
 	DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
 	      sh->share_cache.dev_gen);
-	rte_smp_wmb();
 	rte_rwlock_read_unlock(&sh->share_cache.rwlock);
 	return 0;
 }
Signature · 1 line
2.25.1
Acked-by on v3 2/2
#33 lore raw

Re: [dpdk-dev] [PATCH v3 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

Quoted 24 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, May 18, 2021 11:51
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>; Ruifeng Wang <[email protected]>
Subject: [PATCH v3 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

'dev_gen' is a variable to trigger all cores to flush their local caches once the global MR cache has been rebuilt.

This is due to MR cache's R/W lock can maintain synchronization between threads:

1. dev_gen and global cache updating ordering inside the lock protected section does not matter. Because other threads cannot take the lock until global cache has been updated. Thus, in out of order platform, even if other agents firstly observe updated dev_gen but global does not update, they also have to wait the lock. As a result, it is unnecessary to add a wmb between global cache rebuilding and updating the dev_gen to keep the memory store order.

Quoted 8 lines

2. Store-Release of unlock provides the implicit wmb at the level visible by software. This makes 'rebuilding global cache' and 'updating dev_gen' be observed before local_cache starts to be updated by other agents. Thus, wmb after 'updating dev_gen' can be removed.

Thanks a lot for patience and cooperation.
With best regards,
Slava

#34 lore raw

[dpdk-dev] 回复: [PATCH v3 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

Also thanks for your patient communication and explanation. A wonderful discussion~

Best Regards
Feifei

Quoted 48 lines

-----邮件原件-----
发件人: Slava Ovsiienko <[email protected]>
发送时间: 2021年5月18日 18:18
收件人: Feifei Wang <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]> 抄送: [email protected]; nd <[email protected]>; Ruifeng Wang <[email protected]>
主题: RE: [PATCH v3 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

Quoted 25 lines

-----Original Message-----
From: Feifei Wang <[email protected]>
Sent: Tuesday, May 18, 2021 11:51
To: Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Slava Ovsiienko <[email protected]>
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>; Ruifeng Wang <[email protected]>
Subject: [PATCH v3 2/2] net/mlx5: remove unnecessary wmb for Memory Region cache

'dev_gen' is a variable to trigger all cores to flush their local caches once the global MR cache has been rebuilt.

This is due to MR cache's R/W lock can maintain synchronization between
threads:

1. dev_gen and global cache updating ordering inside the lock protected section does not matter. Because other threads cannot take the lock until global cache has been updated. Thus, in out of order platform, even if other agents firstly observe updated dev_gen but global does not update, they also have to wait the lock. As a result, it is unnecessary to add a wmb between global cache rebuilding and updating the dev_gen to keep the memory store order.

Quoted 8 lines

2. Store-Release of unlock provides the implicit wmb at the level visible by software. This makes 'rebuilding global cache' and 'updating dev_gen' be observed before local_cache starts to be updated by other agents. Thus, wmb after 'updating dev_gen' can be removed.

Thanks a lot for patience and cooperation.
With best regards,
Slava

#35 lore raw

Re: [dpdk-dev] [PATCH v3 0/2] remove wmb for net/mlx

In reply to Feifei Wang

Hi,

Quoted 21 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Feifei Wang
Sent: Tuesday, May 18, 2021 11:51 AM
Cc: [email protected]; [email protected]; Feifei Wang <[email protected]>
Subject: [dpdk-dev] [PATCH v3 0/2] remove wmb for net/mlx

For net/mlx4 and net/mlx5, remove unnecessary wmb for Memory Region cache.

v2:
1. keep the order of dev_gen and global cache (Slava Ovsiienko) 2. remove the wmb at last instead of moving it forward 3. remove atomic_thread_fence patches

v3:
1. commit message rewording (Slava Ovsiienko)

Feifei Wang (2):
net/mlx4: remove unnecessary wmb for Memory Region cache net/mlx5: remove unnecessary wmb for Memory Region cache

Quoted 6 lines

drivers/net/mlx4/mlx4_mr.c | 11 +++-------- drivers/net/mlx5/mlx5_mr.c |
22 ++++++----------------

 2 files changed, 9 insertions(+), 24 deletions(-)

--
2.25.1

Series applied to next-net-mlx,

Kindest regards,
Raslan Darawsheh