Skip to contentSkip to conversation

lib/lpm: memory orderings to avoid race conditions for v1604

PATCH Superseded Ruifeng Wang 2 participants 5 messages Jun 19, 2019 – Jun 20, 2019

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 lib/lpm: memory orderings to avoid race conditions for v1604

When a tbl8 group is getting attached to a tbl24 entry, lookup might fail even though the entry is configured in the table.

For ex: consider a LPM table configured with 10.10.10.1/24. When a new entry 10.10.10.32/28 is being added, a new tbl8 group is allocated and tbl24 entry is changed to point to the tbl8 group. If the tbl24 entry is written without the tbl8 group entries updated, a lookup on 10.10.10.9 will return failure.

Correct memory orderings are required to ensure that the store to tbl24 does not happen before the stores to tbl8 group entries complete.

The orderings have impact on LPM performance test.
On Arm A72 platform, delete operation has 2.7% degradation, while add / lookup has no notable performance change.
On x86 E5 platform, add operation has 4.3% degradation, delete operation has 2.2% - 10.2% degradation, lookup has no performance change.

---
v2: no changes
v1: initial version

 lib/librte_lpm/rte_lpm.c | 32 +++++++++++++++++++++++++-------
 lib/librte_lpm/rte_lpm.h |  4 ++++
 2 files changed, 29 insertions(+), 7 deletions(-)
lib/librte_lpm/rte_lpm.c +25 -7
index 6b7b28a2e..6ec450a08 100644
@@ -806,7 +806,8 @@ add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
 			/* Setting tbl24 entry in one go to avoid race
 			 * conditions
 			 */
-			lpm->tbl24[i] = new_tbl24_entry;
+			__atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
+					__ATOMIC_RELEASE);
 
 			continue;
 		}
@@ -1017,7 +1018,11 @@ add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
 			.depth = 0,
 		};
 
-		lpm->tbl24[tbl24_index] = new_tbl24_entry;
+		/* The tbl24 entry must be written only after the
+		 * tbl8 entries are written.
+		 */
+		__atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
+				__ATOMIC_RELEASE);
 
 	} /* If valid entry but not extended calculate the index into Table8. */
 	else if (lpm->tbl24[tbl24_index].valid_group == 0) {
@@ -1063,7 +1068,11 @@ add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
 				.depth = 0,
 		};
 
-		lpm->tbl24[tbl24_index] = new_tbl24_entry;
+		/* The tbl24 entry must be written only after the
+		 * tbl8 entries are written.
+		 */
+		__atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
+				__ATOMIC_RELEASE);
 
 	} else { /*
 		* If it is valid, extended entry calculate the index into tbl8.
@@ -1391,6 +1400,7 @@ delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
 	/* Calculate the range and index into Table24. */
 	tbl24_range = depth_to_range(depth);
 	tbl24_index = (ip_masked >> 8);
+	struct rte_lpm_tbl_entry zero_tbl24_entry = {0};
 
 	/*
 	 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
@@ -1405,7 +1415,8 @@ delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
 
 			if (lpm->tbl24[i].valid_group == 0 &&
 					lpm->tbl24[i].depth <= depth) {
-				lpm->tbl24[i].valid = INVALID;
+				__atomic_store(&lpm->tbl24[i],
+					&zero_tbl24_entry, __ATOMIC_RELEASE);
 			} else if (lpm->tbl24[i].valid_group == 1) {
 				/*
 				 * If TBL24 entry is extended, then there has
@@ -1450,7 +1461,8 @@ delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
 
 			if (lpm->tbl24[i].valid_group == 0 &&
 					lpm->tbl24[i].depth <= depth) {
-				lpm->tbl24[i] = new_tbl24_entry;
+				__atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
+						__ATOMIC_RELEASE);
 			} else  if (lpm->tbl24[i].valid_group == 1) {
 				/*
 				 * If TBL24 entry is extended, then there has
@@ -1713,8 +1725,11 @@ delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
 	tbl8_recycle_index = tbl8_recycle_check_v1604(lpm->tbl8, tbl8_group_start);
 
 	if (tbl8_recycle_index == -EINVAL) {
-		/* Set tbl24 before freeing tbl8 to avoid race condition. */
+		/* Set tbl24 before freeing tbl8 to avoid race condition.
+		 * Prevent the free of the tbl8 group from hoisting.
+		 */
 		lpm->tbl24[tbl24_index].valid = 0;
+		__atomic_thread_fence(__ATOMIC_RELEASE);
 		tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
 	} else if (tbl8_recycle_index > -1) {
 		/* Update tbl24 entry. */
@@ -1725,8 +1740,11 @@ delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
 			.depth = lpm->tbl8[tbl8_recycle_index].depth,
 		};
 
-		/* Set tbl24 before freeing tbl8 to avoid race condition. */
+		/* Set tbl24 before freeing tbl8 to avoid race condition.
+		 * Prevent the free of the tbl8 group from hoisting.
+		 */
 		lpm->tbl24[tbl24_index] = new_tbl24_entry;
+		__atomic_thread_fence(__ATOMIC_RELEASE);
 		tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
 	}
 #undef group_idx
lib/librte_lpm/rte_lpm.h +4 -0
index b886f54b4..6f5704c5c 100644
@@ -354,6 +354,10 @@ rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
 	ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
 	tbl_entry = *ptbl;
 
+	/* Memory ordering is not required in lookup. Because dataflow
+	 * dependency exists, compiler or HW won't be able to re-order
+	 * the operations.
+	 */
 	/* Copy tbl8 entry (only if needed) */
 	if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
 			RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 2/3 lib/lpm: memory orderings to avoid race conditions for v20

When a tbl8 group is getting attached to a tbl24 entry, lookup might fail even though the entry is configured in the table.

For ex: consider a LPM table configured with 10.10.10.1/24. When a new entry 10.10.10.32/28 is being added, a new tbl8 group is allocated and tbl24 entry is changed to point to the tbl8 group. If the tbl24 entry is written without the tbl8 group entries updated, a lookup on 10.10.10.9 will return failure.

Correct memory orderings are required to ensure that the store to tbl24 does not happen before the stores to tbl8 group entries complete.

---
v2: fixed clang building issue by supplying alignment attribute.
v1: initail version

 lib/librte_lpm/rte_lpm.c | 31 ++++++++++++++++++++++++-------
 lib/librte_lpm/rte_lpm.h |  4 ++--
 2 files changed, 26 insertions(+), 9 deletions(-)
lib/librte_lpm/rte_lpm.c +24 -7
index 6ec450a08..0addff5d4 100644
@@ -737,7 +737,8 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
 			/* Setting tbl24 entry in one go to avoid race
 			 * conditions
 			 */
-			lpm->tbl24[i] = new_tbl24_entry;
+			__atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
+					__ATOMIC_RELEASE);
 
 			continue;
 		}
@@ -892,7 +893,8 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
 			.depth = 0,
 		};
 
-		lpm->tbl24[tbl24_index] = new_tbl24_entry;
+		__atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
+				__ATOMIC_RELEASE);
 
 	} /* If valid entry but not extended calculate the index into Table8. */
 	else if (lpm->tbl24[tbl24_index].valid_group == 0) {
@@ -938,7 +940,8 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
 				.depth = 0,
 		};
 
-		lpm->tbl24[tbl24_index] = new_tbl24_entry;
+		__atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
+				__ATOMIC_RELEASE);
 
 	} else { /*
 		* If it is valid, extended entry calculate the index into tbl8.
@@ -1320,7 +1323,14 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
 
 			if (lpm->tbl24[i].valid_group == 0 &&
 					lpm->tbl24[i].depth <= depth) {
-				lpm->tbl24[i].valid = INVALID;
+				struct rte_lpm_tbl_entry_v20 zero_tbl_entry = {
+						.valid = INVALID,
+						.depth = 0,
+						.valid_group = 0,
+					};
+					zero_tbl_entry.next_hop = 0;
+				__atomic_store(&lpm->tbl24[i], &zero_tbl_entry,
+						__ATOMIC_RELEASE);
 			} else if (lpm->tbl24[i].valid_group == 1) {
 				/*
 				 * If TBL24 entry is extended, then there has
@@ -1365,7 +1375,8 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
 
 			if (lpm->tbl24[i].valid_group == 0 &&
 					lpm->tbl24[i].depth <= depth) {
-				lpm->tbl24[i] = new_tbl24_entry;
+				__atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
+						__ATOMIC_RELEASE);
 			} else  if (lpm->tbl24[i].valid_group == 1) {
 				/*
 				 * If TBL24 entry is extended, then there has
@@ -1647,8 +1658,11 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
 	tbl8_recycle_index = tbl8_recycle_check_v20(lpm->tbl8, tbl8_group_start);
 
 	if (tbl8_recycle_index == -EINVAL) {
-		/* Set tbl24 before freeing tbl8 to avoid race condition. */
+		/* Set tbl24 before freeing tbl8 to avoid race condition.
+		 * Prevent the free of the tbl8 group from hoisting.
+		 */
 		lpm->tbl24[tbl24_index].valid = 0;
+		__atomic_thread_fence(__ATOMIC_RELEASE);
 		tbl8_free_v20(lpm->tbl8, tbl8_group_start);
 	} else if (tbl8_recycle_index > -1) {
 		/* Update tbl24 entry. */
@@ -1659,8 +1673,11 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
 			.depth = lpm->tbl8[tbl8_recycle_index].depth,
 		};
 
-		/* Set tbl24 before freeing tbl8 to avoid race condition. */
+		/* Set tbl24 before freeing tbl8 to avoid race condition.
+		 * Prevent the free of the tbl8 group from hoisting.
+		 */
 		lpm->tbl24[tbl24_index] = new_tbl24_entry;
+		__atomic_thread_fence(__ATOMIC_RELEASE);
 		tbl8_free_v20(lpm->tbl8, tbl8_group_start);
 	}
 
lib/librte_lpm/rte_lpm.h +2 -2
index 6f5704c5c..98c70ecbe 100644
@@ -88,7 +88,7 @@ struct rte_lpm_tbl_entry_v20 {
 	 */
 	uint8_t valid_group :1;
 	uint8_t depth       :6; /**< Rule depth. */
-};
+} __rte_aligned(2);
 
 __extension__
 struct rte_lpm_tbl_entry {
@@ -121,7 +121,7 @@ struct rte_lpm_tbl_entry_v20 {
 		uint8_t group_idx;
 		uint8_t next_hop;
 	};
-};
+} __rte_aligned(2);
 
 __extension__
 struct rte_lpm_tbl_entry {
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 3/3 lib/lpm: remove unnecessary inline

In reply to Ruifeng Wang

Tests showed that the 'inline' keyword caused performance drop on some x86 platforms after the memory ordering patches applied. By removing the 'inline' keyword, the performance was recovered as before on x86 and no impact to arm64 platforms.

---
v2: initail version to recover rte_lpm_add() performance

 lib/librte_lpm/rte_lpm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/librte_lpm/rte_lpm.c +2 -2
index 0addff5d4..c97b602e6 100644
@@ -778,7 +778,7 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
 	return 0;
 }
 
-static inline int32_t
+static int32_t
 add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
 		uint32_t next_hop)
 {
@@ -975,7 +975,7 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
 	return 0;
 }
 
-static inline int32_t
+static int32_t
 add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
 		uint32_t next_hop)
 {
Signature · 1 line
2.17.1
#3 lore raw

Re: [dpdk-dev] [PATCH v2 3/3] lib/lpm: remove unnecessary inline

Hi Wang,

1. It is better to explicitly use __rte_noinline with this function because my gcc still inlines it even without the inline qualifier.

2. The same should be applied to _v20 functions.

3. Please try running the tests again and show the results.

4. Make this patch the first in a series.

Quoted 36 lines · Ruifeng Wang

Tests showed that the 'inline' keyword caused performance drop on some x86 platforms after the memory ordering patches applied. By removing the 'inline' keyword, the performance was recovered as before on x86 and no impact to arm64 platforms.

---
v2: initail version to recover rte_lpm_add() performance

  lib/librte_lpm/rte_lpm.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
lib/librte_lpm/rte_lpm.c +2 -2
index 0addff5d4..c97b602e6 100644
@@ -778,7 +778,7 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
  	return 0;
  }
 
-static inline int32_t
+static int32_t
  add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
  		uint32_t next_hop)
  {
@@ -975,7 +975,7 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
  	return 0;
  }
 
-static inline int32_t
+static int32_t
  add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
  		uint32_t next_hop)
  {
Signature · 2 lines
Regards,
Vladimir
#4 lore raw

Re: [dpdk-dev] [PATCH v2 3/3] lib/lpm: remove unnecessary inline

Hi Vladimir,

Quoted 10 lines

-----Original Message-----
From: Medvedkin, Vladimir <[email protected]>
Sent: Wednesday, June 19, 2019 20:51
To: Ruifeng Wang (Arm Technology China) <[email protected]>; [email protected]
Cc: [email protected]; Honnappa Nagarahalli <[email protected]>; Gavin Hu (Arm Technology China) <[email protected]>; nd <[email protected]>
Subject: Re: [PATCH v2 3/3] lib/lpm: remove unnecessary inline

Quoted 8 lines

Hi Wang,

1. It is better to explicitly use __rte_noinline with this function because my gcc still inlines it even without the inline qualifier.

2. The same should be applied to _v20 functions.

3. Please try running the tests again and show the results.

For x86, I can do test with E5 platform that we have.

Quoted 3 lines

4. Make this patch the first in a series.

Thanks. Will come back with new patch set.

Quoted 44 lines · Ruifeng Wang

lib/librte_lpm/rte_lpm.c add_depth_small_v20(struct rte_lpm_v20 *lpm,

Quoted 19 lines

Tests showed that the 'inline' keyword caused performance drop on some x86 platforms after the memory ordering patches applied.
By removing the 'inline' keyword, the performance was recovered as before on x86 and no impact to arm64 platforms.

---
v2: initail version to recover rte_lpm_add() performance

  lib/librte_lpm/rte_lpm.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
lib/librte_lpm/rte_lpm.c index renamed from lib/librte_lpm/rte_lpm.c +0 -0

0addff5d4..c97b602e6 100644

lib/librte_lpm/rte_lpm.c +0 -0
@@ -778,7 +778,7 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm,

uint32_t ip, uint8_t depth,

lib/librte_lpm/rte_lpm.c add_depth_big_v20(struct rte_lpm_v20 *lpm,

Quoted 9 lines
lib/librte_lpm/rte_lpm.c +1 -1
  	return 0;
  }
 
-static inline int32_t
+static int32_t
  add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
  		uint32_t next_hop)
  {
@@ -975,7 +975,7 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm,

uint32_t ip_masked, uint8_t depth,

lib/librte_lpm/rte_lpm.c add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,

Quoted 6 lines
lib/librte_lpm/rte_lpm.c +1 -1
  	return 0;
  }
 
-static inline int32_t
+static int32_t
  add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t

depth,

lib/librte_lpm/rte_lpm.c add_depth_big_v20(struct rte_lpm_v20 *lpm, · line 981

Quoted 2 lines
lib/librte_lpm/rte_lpm.c +0 -0
  		uint32_t next_hop)
  {

--
Regards,
Vladimir