Skip to contentSkip to conversation

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

PATCH In review Ruifeng Wang 5 participants 13 messages Jul 3, 2019 – Jul 9, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v4 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 ordering patches in general have no notable impact on LPM performance test on both Arm A72 platform and x86 E5 platform.

---
v4: updated test data
v3: updated test data
v2: no changes

 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 v4 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.

Besides, explicit structure alignment is used to address atomic operation building issue with older version clang.

---
v4: changed alignment attribute parameter
v3: no changes
v2: fixed clang building issue by supplying alignment attribute.

 lib/librte_lpm/rte_lpm.c | 32 +++++++++++++++++++++++++-------
 lib/librte_lpm/rte_lpm.h |  4 ++--
 2 files changed, 27 insertions(+), 9 deletions(-)
lib/librte_lpm/rte_lpm.c +25 -7
index 6ec450a08..baa6e7460 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,15 @@ 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_tbl24_entry = {
+						.valid = INVALID,
+						.depth = 0,
+						.valid_group = 0,
+					};
+					zero_tbl24_entry.next_hop = 0;
+				__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
@@ -1365,7 +1376,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 +1659,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 +1674,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..906ec4483 100644
@@ -88,7 +88,7 @@ struct rte_lpm_tbl_entry_v20 {
 	 */
 	uint8_t valid_group :1;
 	uint8_t depth       :6; /**< Rule depth. */
-};
+} __rte_aligned(sizeof(uint16_t));
 
 __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(sizeof(uint16_t));
 
 __extension__
 struct rte_lpm_tbl_entry {
Signature · 1 line
2.17.1
#2 lore raw

Re: [dpdk-dev] [PATCH v4 2/3] lib/lpm: memory orderings to avoid race conditions for v20

Hi Wang,

Quoted 66 lines · Ruifeng Wang

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.

Besides, explicit structure alignment is used to address atomic operation building issue with older version clang.

---
v4: changed alignment attribute parameter
v3: no changes
v2: fixed clang building issue by supplying alignment attribute.

  lib/librte_lpm/rte_lpm.c | 32 +++++++++++++++++++++++++-------
  lib/librte_lpm/rte_lpm.h |  4 ++--
  2 files changed, 27 insertions(+), 9 deletions(-)
lib/librte_lpm/rte_lpm.c +6 -3
index 6ec450a08..baa6e7460 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,15 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
 

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, · line 1332

Quoted 10 lines
lib/librte_lpm/rte_lpm.c +7 -1
  			if (lpm->tbl24[i].valid_group == 0 &&
  					lpm->tbl24[i].depth <= depth) {
-				lpm->tbl24[i].valid = INVALID;
+				struct rte_lpm_tbl_entry_v20
+					zero_tbl24_entry = {
+						.valid = INVALID,
+						.depth = 0,
+						.valid_group = 0,
+					};
+					zero_tbl24_entry.next_hop = 0;

Why don't you use just "struct rte_lpm_tbl_entry_v20 zero_tbl24_entry = {0} " like you do for _v1604?

Quoted 63 lines
lib/librte_lpm/rte_lpm.c +12 -3
+				__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
@@ -1365,7 +1376,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 +1659,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 +1674,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..906ec4483 100644
@@ -88,7 +88,7 @@ struct rte_lpm_tbl_entry_v20 {
  	 */
  	uint8_t valid_group :1;
  	uint8_t depth       :6; /**< Rule depth. */
-};
+} __rte_aligned(sizeof(uint16_t));
 
  __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(sizeof(uint16_t));
 
  __extension__
  struct rte_lpm_tbl_entry {
Signature · 2 lines
Regards,
Vladimir
#3 lore raw

Re: [dpdk-dev] [PATCH v4 2/3] lib/lpm: memory orderings to avoid race conditions for v20

Hi all,

пт, 5 июл. 2019 г. в 17:52, Medvedkin, Vladimir <
[email protected]>:

Quoted 85 lines

Hi Wang,

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

Quoted 35 lines · Ruifeng Wang

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.

Besides, explicit structure alignment is used to address atomic operation building issue with older version clang.

---
v4: changed alignment attribute parameter
v3: no changes
v2: fixed clang building issue by supplying alignment attribute.

  lib/librte_lpm/rte_lpm.c | 32 +++++++++++++++++++++++++-------
  lib/librte_lpm/rte_lpm.h |  4 ++--
  2 files changed, 27 insertions(+), 9 deletions(-)
lib/librte_lpm/rte_lpm.c +0 -0
index 6ec450a08..baa6e7460 100644
@@ -737,7 +737,8 @@ 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, uint32_t

Quoted 10 lines
lib/librte_lpm/rte_lpm.c +2 -1
                      /* 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,

lib/librte_lpm/rte_lpm.c add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,

Quoted 8 lines
lib/librte_lpm/rte_lpm.c +2 -1
                      .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. */

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

Quoted 2 lines
lib/librte_lpm/rte_lpm.c +0 -0
      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,

lib/librte_lpm/rte_lpm.c add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,

Quoted 9 lines
lib/librte_lpm/rte_lpm.c +2 -1
                              .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.

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

Quoted 1 line
lib/librte_lpm/rte_lpm.c +0 -0
@@ -1320,7 +1323,15 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm,

uint32_t ip_masked,

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, · line 1332

Quoted 11 lines
lib/librte_lpm/rte_lpm.c +7 -1
                      if (lpm->tbl24[i].valid_group == 0 &&
                                      lpm->tbl24[i].depth <= depth) {
-                             lpm->tbl24[i].valid = INVALID;
+                             struct rte_lpm_tbl_entry_v20
+                                     zero_tbl24_entry = {
+                                             .valid = INVALID,
+                                             .depth = 0,
+                                             .valid_group = 0,
+                                     };
+                                     zero_tbl24_entry.next_hop = 0;
Quoted 3 lines

Why don't you use just "struct rte_lpm_tbl_entry_v20 zero_tbl24_entry = {0} " like you do for _v1604?

Ah, sorry, just found what you mentioned earlier, this will cause meson build to fail.

Quoted 79 lines

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20 *lpm, · line 1333

Quoted 2 lines
lib/librte_lpm/rte_lpm.c +2 -0
+                             __atomic_store(&lpm->tbl24[i],
+                                     &zero_tbl24_entry,

__ATOMIC_RELEASE);

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,

Quoted 3 lines
lib/librte_lpm/rte_lpm.c +0 -0
                      } else if (lpm->tbl24[i].valid_group == 1) {
                              /*
                               * If TBL24 entry is extended, then there

has

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

Quoted 1 line
lib/librte_lpm/rte_lpm.c +0 -0
@@ -1365,7 +1376,8 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm,

uint32_t ip_masked,

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, · line 1333

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -1
                      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,

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,

Quoted 4 lines
lib/librte_lpm/rte_lpm.c +1 -0
+                                             __ATOMIC_RELEASE);
                      } else  if (lpm->tbl24[i].valid_group == 1) {
                              /*
                               * If TBL24 entry is extended, then there

has

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

Quoted 1 line
lib/librte_lpm/rte_lpm.c +0 -0
@@ -1647,8 +1659,11 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,

uint32_t ip_masked,

lib/librte_lpm/rte_lpm.c delete_depth_big_v20(struct rte_lpm_v20 *lpm, · line 1659

Quoted 1 line
lib/librte_lpm/rte_lpm.c +0 -0
      tbl8_recycle_index = tbl8_recycle_check_v20(lpm->tbl8,

tbl8_group_start);

lib/librte_lpm/rte_lpm.c delete_depth_big_v20(struct rte_lpm_v20 *lpm, · line 1649

Quoted 3 lines
lib/librte_lpm/rte_lpm.c +0 -1
      if (tbl8_recycle_index == -EINVAL) {
-             /* Set tbl24 before freeing tbl8 to avoid race condition.

*/

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

Quoted 9 lines
lib/librte_lpm/rte_lpm.c +4 -0
+             /* 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 +1674,11 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,

uint32_t ip_masked,

lib/librte_lpm/rte_lpm.c delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,

Quoted 4 lines
lib/librte_lpm/rte_lpm.c +0 -1
                      .depth = lpm->tbl8[tbl8_recycle_index].depth,
              };
 
-             /* Set tbl24 before freeing tbl8 to avoid race condition.

*/

Quoted 22 lines
lib/librte_lpm/rte_lpm.c +4 -0
+             /* 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 +1 -1
index 6f5704c5c..906ec4483 100644
@@ -88,7 +88,7 @@ struct rte_lpm_tbl_entry_v20 {
       */
      uint8_t valid_group :1;
      uint8_t depth       :6; /**< Rule depth. */
-};
+} __rte_aligned(sizeof(uint16_t));
 
  __extension__
  struct rte_lpm_tbl_entry {

lib/librte_lpm/rte_lpm.h struct rte_lpm_tbl_entry_v20 { · line 127

Quoted 8 lines
lib/librte_lpm/rte_lpm.h +1 -1
@@ -121,7 +121,7 @@ struct rte_lpm_tbl_entry_v20 {
              uint8_t group_idx;
              uint8_t next_hop;
      };
-};
+} __rte_aligned(sizeof(uint16_t));
 
  __extension__
  struct rte_lpm_tbl_entry {

--
Regards,
Vladimir

Signature · 2 lines
Regards,
Vladimir
#4 lore raw

PATCH v4 3/3 lib/lpm: use atomic store to avoid partial update

In reply to Ruifeng Wang

Compiler could generate non-atomic stores for whole table entry updating. This may cause incorrect nexthop to be returned, if the byte with valid flag is updated prior to the byte with next hot is updated.

Changed to use atomic store to update whole table entry.

---
v4: initial version

 lib/librte_lpm/rte_lpm.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
lib/librte_lpm/rte_lpm.c +24 -10
index baa6e7460..5d1dbd7e6 100644
@@ -767,7 +767,9 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to avoid
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
 				}
@@ -837,7 +839,9 @@ add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to avoid
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
 				}
@@ -965,7 +969,8 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 				continue;
 			}
@@ -1100,7 +1105,8 @@ add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 				continue;
 			}
@@ -1393,7 +1399,9 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
 					RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] = new_tbl8_entry;
+						__atomic_store(&lpm->tbl8[j],
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
 				}
 			}
 		}
@@ -1490,7 +1498,9 @@ delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
 					RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] = new_tbl8_entry;
+						__atomic_store(&lpm->tbl8[j],
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
 				}
 			}
 		}
@@ -1646,7 +1656,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}
 	}
 
@@ -1677,7 +1688,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);
 		tbl8_free_v20(lpm->tbl8, tbl8_group_start);
 	}
@@ -1730,7 +1742,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}
 	}
 
@@ -1761,7 +1774,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);
 		tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
 	}
Signature · 1 line
2.17.1
#5 lore raw

Re: [dpdk-dev] [PATCH v4 3/3] lib/lpm: use atomic store to avoid partial update

Hi Wang,

Quoted 115 lines · Ruifeng Wang

Compiler could generate non-atomic stores for whole table entry updating. This may cause incorrect nexthop to be returned, if the byte with valid flag is updated prior to the byte with next hot is updated.

Changed to use atomic store to update whole table entry.

---
v4: initial version

  lib/librte_lpm/rte_lpm.c | 34 ++++++++++++++++++++++++----------
  1 file changed, 24 insertions(+), 10 deletions(-)
lib/librte_lpm/rte_lpm.c +22 -9
index baa6e7460..5d1dbd7e6 100644
@@ -767,7 +767,9 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
  					 * Setting tbl8 entry in one go to avoid
  					 * race conditions
  					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
  					continue;
  				}
@@ -837,7 +839,9 @@ add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
  					 * Setting tbl8 entry in one go to avoid
  					 * race conditions
  					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
  					continue;
  				}
@@ -965,7 +969,8 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
  				 * Setting tbl8 entry in one go to avoid race
  				 * condition
  				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
  				continue;
  			}
@@ -1100,7 +1105,8 @@ add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
  				 * Setting tbl8 entry in one go to avoid race
  				 * condition
  				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
  				continue;
  			}
@@ -1393,7 +1399,9 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
  					RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
  					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] = new_tbl8_entry;
+						__atomic_store(&lpm->tbl8[j],
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
  				}
  			}
  		}
@@ -1490,7 +1498,9 @@ delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
  					RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
  					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] = new_tbl8_entry;
+						__atomic_store(&lpm->tbl8[j],
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
  				}
  			}
  		}
@@ -1646,7 +1656,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
  		 */
  		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
  			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
+						__ATOMIC_RELAXED);
  		}
  	}
 
@@ -1677,7 +1688,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
  		/* 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_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
+				__ATOMIC_RELAXED);
  		__atomic_thread_fence(__ATOMIC_RELEASE);
  		tbl8_free_v20(lpm->tbl8, tbl8_group_start);
  	}
@@ -1730,7 +1742,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
  		 */
  		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
  			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
+						__ATOMIC_RELAXED);
  		}
  	}
 

lib/librte_lpm/rte_lpm.c delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, · line 1779

Quoted 7 lines
lib/librte_lpm/rte_lpm.c +2 -1
@@ -1761,7 +1774,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
  		/* 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_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
+				__ATOMIC_RELAXED);
  		__atomic_thread_fence(__ATOMIC_RELEASE);

Do you really need __atomic_thread_fence after atomic_store?

Quoted 2 lines
lib/librte_lpm/rte_lpm.c +0 -0
  		tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
  	}
Signature · 2 lines
Regards,
Vladimir
#6 lore raw

Re: [dpdk-dev] [PATCH v4 3/3] lib/lpm: use atomic store to avoid partial update

Hi Vladimir,

Quoted 154 lines

-----Original Message-----
From: Medvedkin, Vladimir <[email protected]>
Sent: Saturday, July 6, 2019 00:53
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 v4 3/3] lib/lpm: use atomic store to avoid partial update

Hi Wang,

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

Quoted 20 lines · Ruifeng Wang

Compiler could generate non-atomic stores for whole table entry updating. This may cause incorrect nexthop to be returned, if the byte with valid flag is updated prior to the byte with next hot is updated.

Changed to use atomic store to update whole table entry.

---
v4: initial version

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

baa6e7460..5d1dbd7e6 100644

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

uint32_t ip, uint8_t depth,

lib/librte_lpm/rte_lpm.c add_depth_small_v1604(struct rte_lpm *lpm,

Quoted 11 lines
lib/librte_lpm/rte_lpm.c +3 -1
  					 * Setting tbl8 entry in one go to avoid
  					 * race conditions
  					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
  					continue;
  				}
@@ -837,7 +839,9 @@ add_depth_small_v1604(struct rte_lpm *lpm,

uint32_t ip, uint8_t depth,

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

Quoted 11 lines
lib/librte_lpm/rte_lpm.c +3 -1
  					 * Setting tbl8 entry in one go to avoid
  					 * race conditions
  					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
  					continue;
  				}
@@ -965,7 +969,8 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm,

uint32_t ip_masked, uint8_t depth,

lib/librte_lpm/rte_lpm.c add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -1
  				 * Setting tbl8 entry in one go to avoid race
  				 * condition
  				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],

&new_tbl8_entry,

lib/librte_lpm/rte_lpm.c add_depth_big_v1604(struct rte_lpm *lpm,

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -0
+						__ATOMIC_RELAXED);
 
  				continue;
  			}
@@ -1100,7 +1105,8 @@ 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, uint32_t ip_masked, uint8_t depth,

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -1
  				 * Setting tbl8 entry in one go to avoid race
  				 * condition
  				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],

&new_tbl8_entry,

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -0
+						__ATOMIC_RELAXED);
 
  				continue;
  			}
@@ -1393,7 +1399,9 @@ delete_depth_small_v20(struct rte_lpm_v20

*lpm, uint32_t ip_masked,

Quoted 1 line
	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {

lib/librte_lpm/rte_lpm.c

Quoted 3 lines

if (lpm->tbl8[j].depth <= depth)

lib/librte_lpm/rte_lpm.c +0 -1
-						lpm->tbl8[j] =

new_tbl8_entry;

lib/librte_lpm/rte_lpm.c delete_depth_small_v1604(struct rte_lpm *lpm,

Quoted 8 lines
lib/librte_lpm/rte_lpm.c +1 -0
+						__atomic_store(&lpm-

tbl8[j],

lib/librte_lpm/rte_lpm.c +2 -0
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
  				}
  			}
  		}
@@ -1490,7 +1498,9 @@ delete_depth_small_v1604(struct rte_lpm *lpm,

uint32_t ip_masked,

Quoted 1 line
	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {

lib/librte_lpm/rte_lpm.c

Quoted 3 lines

if (lpm->tbl8[j].depth <= depth)

lib/librte_lpm/rte_lpm.c +0 -1
-						lpm->tbl8[j] =

new_tbl8_entry;

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

Quoted 8 lines
lib/librte_lpm/rte_lpm.c +1 -0
+						__atomic_store(&lpm-

tbl8[j],

lib/librte_lpm/rte_lpm.c +2 -0
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
  				}
  			}
  		}
@@ -1646,7 +1656,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,

uint32_t ip_masked,

lib/librte_lpm/rte_lpm.c delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -1
  		 */
  		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
  			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],

&new_tbl8_entry,

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

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -0
+						__ATOMIC_RELAXED);
  		}
  	}
 
@@ -1677,7 +1688,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,

uint32_t ip_masked,

lib/librte_lpm/rte_lpm.c delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -1
  		/* 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_store(&lpm->tbl24[tbl24_index],

&new_tbl24_entry,

lib/librte_lpm/rte_lpm.c delete_depth_big_v1604(struct rte_lpm *lpm,

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -0
+				__ATOMIC_RELAXED);
  		__atomic_thread_fence(__ATOMIC_RELEASE);
  		tbl8_free_v20(lpm->tbl8, tbl8_group_start);
  	}
@@ -1730,7 +1742,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,

uint32_t ip_masked,

lib/librte_lpm/rte_lpm.c delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -1
  		 */
  		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
  			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],

&new_tbl8_entry,

lib/librte_lpm/rte_lpm.c delete_depth_big_v1604(struct rte_lpm *lpm,

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -0
+						__ATOMIC_RELAXED);
  		}
  	}
 
@@ -1761,7 +1774,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,

uint32_t ip_masked,

lib/librte_lpm/rte_lpm.c delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,

Quoted 5 lines
lib/librte_lpm/rte_lpm.c +1 -1
  		/* 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_store(&lpm->tbl24[tbl24_index],

&new_tbl24_entry,

Quoted 3 lines

lib/librte_lpm/rte_lpm.c delete_depth_big_v1604(struct rte_lpm *lpm, · line 1779

Quoted 2 lines
lib/librte_lpm/rte_lpm.c +1 -0
+				__ATOMIC_RELAXED);
  		__atomic_thread_fence(__ATOMIC_RELEASE);

Do you really need __atomic_thread_fence after atomic_store?

Yes. The __atomic_thread_fence here is to prevent the free of the tbl8 group below from hoisting. The order of tbl24 update and tbl8 update needs to be kept.

Quoted 6 lines

lib/librte_lpm/rte_lpm.c delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, · line 1781

Quoted 2 lines
lib/librte_lpm/rte_lpm.c +0 -0
  		tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
  	}

--
Regards,
Vladimir

#7 lore raw

Re: [dpdk-dev] [PATCH v4 3/3] lib/lpm: use atomic store to avoid partial update

In reply to Ruifeng Wang
Quoted 4 lines

Compiler could generate non-atomic stores for whole table entry updating. This may cause incorrect nexthop to be returned, if the byte with valid flag is updated prior to the byte with next hot is updated.

                                                           ^^^^^^^

Should be nexthop

Quoted 116 lines

Changed to use atomic store to update whole table entry.

---
v4: initial version

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

baa6e7460..5d1dbd7e6 100644

lib/librte_lpm/rte_lpm.c +0 -0
@@ -767,7 +767,9 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to

avoid

lib/librte_lpm/rte_lpm.c +3 -1
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
 				}
@@ -837,7 +839,9 @@ add_depth_small_v1604(struct rte_lpm *lpm,
 uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to

avoid

lib/librte_lpm/rte_lpm.c +5 -2
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
 				}
@@ -965,7 +969,8 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
lib/librte_lpm/rte_lpm.c +2 -1
 				continue;
 			}
@@ -1100,7 +1105,8 @@ add_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
lib/librte_lpm/rte_lpm.c +3 -1
 				continue;
 			}
@@ -1393,7 +1399,9 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked,
 
 	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] =
 new_tbl8_entry;
+						__atomic_store(&lpm->tbl8[j],
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
lib/librte_lpm/rte_lpm.c +3 -1
 				}
 			}
 		}
@@ -1490,7 +1498,9 @@ delete_depth_small_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 
 	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] =
 new_tbl8_entry;
+						__atomic_store(&lpm->tbl8[j],
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
lib/librte_lpm/rte_lpm.c +2 -1
 				}
 			}
 		}
@@ -1646,7 +1656,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}
lib/librte_lpm/rte_lpm.c +2 -1
 	}
@@ -1677,7 +1688,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index],
 &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);
Quoted 1 line

tbl8_free_v20(lpm->tbl8, tbl8_group_start);

tbl8_alloc_v20/tbl8_free_v20 need to be updated to use __atomic_store

Quoted 23 lines
lib/librte_lpm/rte_lpm.c +2 -1
 	}
@@ -1730,7 +1742,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}
lib/librte_lpm/rte_lpm.c +2 -1
 	}
@@ -1761,7 +1774,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index],
 &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);
Quoted 1 line

tbl8_free_v1604(lpm->tbl8, tbl8_group_start);

tbl8_alloc_v1604 /tbl8_free_v1604 need to be updated to use __atomic_store

Quoted 3 lines

}
--
2.17.1

#8 lore raw

Re: [dpdk-dev] [PATCH v4 3/3] lib/lpm: use atomic store to avoid partial update

Hi Honnappa,

Quoted 124 lines

-----Original Message-----
From: Honnappa Nagarahalli <[email protected]>
Sent: Monday, July 8, 2019 12:57
To: Ruifeng Wang (Arm Technology China) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; Gavin Hu (Arm Technology China) <[email protected]>; nd <[email protected]>; Ruifeng Wang (Arm Technology China) <[email protected]>; Honnappa Nagarahalli <[email protected]>; nd <[email protected]>
Subject: RE: [PATCH v4 3/3] lib/lpm: use atomic store to avoid partial update

Quoted 4 lines

Compiler could generate non-atomic stores for whole table entry updating. This may cause incorrect nexthop to be returned, if the byte with valid flag is updated prior to the byte with next hot is updated.

                                                           ^^^^^^^ Should be nexthop

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20

Quoted 65 lines

Changed to use atomic store to update whole table entry.

---
v4: initial version

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

baa6e7460..5d1dbd7e6 100644

lib/librte_lpm/rte_lpm.c +3 -1
@@ -767,7 +767,9 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to avoid
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
lib/librte_lpm/rte_lpm.c +3 -1
 				}
@@ -837,7 +839,9 @@ add_depth_small_v1604(struct rte_lpm *lpm,
 uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to avoid
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
lib/librte_lpm/rte_lpm.c +2 -1
 				}
@@ -965,7 +969,8 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
lib/librte_lpm/rte_lpm.c +2 -1
 				continue;
 			}
@@ -1100,7 +1105,8 @@ add_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
lib/librte_lpm/rte_lpm.c +0 -0
 				continue;
 			}
@@ -1393,7 +1399,9 @@ delete_depth_small_v20(struct rte_lpm_v20

*lpm,

Quoted 41 lines
lib/librte_lpm/rte_lpm.c +1 -1
 uint32_t ip_masked,
 
 	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] =
 new_tbl8_entry;
+						__atomic_store(&lpm-

tbl8[j],

lib/librte_lpm/rte_lpm.c +3 -1
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
 				}
 			}
 		}
@@ -1490,7 +1498,9 @@ delete_depth_small_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 
 	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] =
 new_tbl8_entry;
+						__atomic_store(&lpm-

tbl8[j],

lib/librte_lpm/rte_lpm.c +4 -1
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
 				}
 			}
 		}
@@ -1646,7 +1656,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}

}

Quoted 13 lines
Quoted 11 lines
lib/librte_lpm/rte_lpm.c +2 -1
@@ -1677,7 +1688,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index],
 &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);

tbl8_free_v20(lpm->tbl8, tbl8_group_start);

tbl8_alloc_v20/tbl8_free_v20 need to be updated to use __atomic_store

tbl8_alloc_v20/tbl8_free_v20 updates a single field of table entry. The process is already atomic. Do we really need to use __atomic_store?

Quoted 13 lines
Quoted 13 lines
lib/librte_lpm/rte_lpm.c +2 -1
 	}
@@ -1730,7 +1742,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}

}

Quoted 13 lines
Quoted 11 lines
lib/librte_lpm/rte_lpm.c +2 -1
@@ -1761,7 +1774,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index],
 &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);

tbl8_free_v1604(lpm->tbl8, tbl8_group_start);

tbl8_alloc_v1604 /tbl8_free_v1604 need to be updated to use __atomic_store

Ditto.

Quoted 4 lines
Quoted 4 lines

}
--
2.17.1

#9 lore raw

Re: [dpdk-dev] [PATCH v4 3/3] lib/lpm: use atomic store to avoid partial update

Quoted 129 lines
Quoted 116 lines
Quoted 5 lines

Compiler could generate non-atomic stores for whole table entry updating. This may cause incorrect nexthop to be returned, if the byte with valid flag is updated prior to the byte with next hot is updated.

                                                           ^^^^^^^

Should be nexthop

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20

Quoted 66 lines

Changed to use atomic store to update whole table entry.

---
v4: initial version

 lib/librte_lpm/rte_lpm.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
lib/librte_lpm/rte_lpm.c +0 -0

index
baa6e7460..5d1dbd7e6 100644

lib/librte_lpm/rte_lpm.c +3 -1
@@ -767,7 +767,9 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to avoid
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
lib/librte_lpm/rte_lpm.c +3 -1
 				}
@@ -837,7 +839,9 @@ add_depth_small_v1604(struct rte_lpm *lpm,
 uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to avoid
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
lib/librte_lpm/rte_lpm.c +2 -1
 				}
@@ -965,7 +969,8 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
lib/librte_lpm/rte_lpm.c +2 -1
 				continue;
 			}
@@ -1100,7 +1105,8 @@ add_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
lib/librte_lpm/rte_lpm.c +0 -0
 				continue;
 			}
@@ -1393,7 +1399,9 @@ delete_depth_small_v20(struct rte_lpm_v20

*lpm,

Quoted 41 lines
lib/librte_lpm/rte_lpm.c +1 -1
 uint32_t ip_masked,
 
 	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] =
 new_tbl8_entry;
+						__atomic_store(&lpm-

tbl8[j],

lib/librte_lpm/rte_lpm.c +3 -1
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
 				}
 			}
 		}
@@ -1490,7 +1498,9 @@ delete_depth_small_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 
 	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] =
 new_tbl8_entry;
+						__atomic_store(&lpm-

tbl8[j],

lib/librte_lpm/rte_lpm.c +4 -1
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
 				}
 			}
 		}
@@ -1646,7 +1656,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}

}

Quoted 13 lines
Quoted 11 lines
lib/librte_lpm/rte_lpm.c +2 -1
@@ -1677,7 +1688,8 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index],
 &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);

tbl8_free_v20(lpm->tbl8, tbl8_group_start);

tbl8_alloc_v20/tbl8_free_v20 need to be updated to use __atomic_store

Quoted 2 lines

tbl8_alloc_v20/tbl8_free_v20 updates a single field of table entry. The process is already atomic. Do we really need to use __atomic_store?

I thought we agreed that all the tbl8 stores will use __atomic_store.
IMO, it is better to use C11 atomic built-ins entirely, at least for the data structures used in reader-writer scenario. Otherwise, the code does not follow C11 memory model completely. (I do not know what to call such a model).

Quoted 33 lines
Quoted 14 lines
Quoted 14 lines
lib/librte_lpm/rte_lpm.c +2 -1
 	}
@@ -1730,7 +1742,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}

}

Quoted 13 lines
Quoted 11 lines
lib/librte_lpm/rte_lpm.c +2 -1
@@ -1761,7 +1774,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index],
 &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);

tbl8_free_v1604(lpm->tbl8, tbl8_group_start);

tbl8_alloc_v1604 /tbl8_free_v1604 need to be updated to use __atomic_store

Ditto.

Quoted 4 lines
Quoted 4 lines

}
--
2.17.1

#10 lore raw

Re: [dpdk-dev] [PATCH v4 3/3] lib/lpm: use atomic store to avoid partial update

Quoted 142 lines

-----Original Message-----
From: Honnappa Nagarahalli <[email protected]>
Sent: Tuesday, July 9, 2019 12:43
To: Ruifeng Wang (Arm Technology China) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; Gavin Hu (Arm Technology China) <[email protected]>; Honnappa Nagarahalli <[email protected]>; nd <[email protected]>; nd <[email protected]>
Subject: RE: [PATCH v4 3/3] lib/lpm: use atomic store to avoid partial update

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Compiler could generate non-atomic stores for whole table entry

updating.

Quoted 127 lines
Quoted 127 lines
Quoted 2 lines

This may cause incorrect nexthop to be returned, if the byte with valid flag is updated prior to the byte with next hot is updated.

                                                           ^^^^^^^

Should be nexthop

lib/librte_lpm/rte_lpm.c delete_depth_small_v20(struct rte_lpm_v20

Quoted 66 lines

Changed to use atomic store to update whole table entry.

---
v4: initial version

 lib/librte_lpm/rte_lpm.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
lib/librte_lpm/rte_lpm.c +0 -0

index
baa6e7460..5d1dbd7e6 100644

lib/librte_lpm/rte_lpm.c +3 -1
@@ -767,7 +767,9 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to avoid
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
lib/librte_lpm/rte_lpm.c +3 -1
 				}
@@ -837,7 +839,9 @@ add_depth_small_v1604(struct rte_lpm *lpm,
 uint32_t ip, uint8_t depth,
 					 * Setting tbl8 entry in one go to avoid
 					 * race conditions
 					 */
-					lpm->tbl8[j] = new_tbl8_entry;
+					__atomic_store(&lpm->tbl8[j],
+						&new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
 					continue;
lib/librte_lpm/rte_lpm.c +2 -1
 				}
@@ -965,7 +969,8 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm,
 uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
lib/librte_lpm/rte_lpm.c +2 -1
 				continue;
 			}
@@ -1100,7 +1105,8 @@ add_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked, uint8_t depth,
 				 * Setting tbl8 entry in one go to avoid race
 				 * condition
 				 */
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 
lib/librte_lpm/rte_lpm.c +0 -0
 				continue;
 			}
@@ -1393,7 +1399,9 @@ delete_depth_small_v20(struct rte_lpm_v20

*lpm,

Quoted 51 lines
lib/librte_lpm/rte_lpm.c +1 -1
 uint32_t ip_masked,
 
 	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] =
 new_tbl8_entry;
+						__atomic_store(&lpm-

tbl8[j],

lib/librte_lpm/rte_lpm.c +3 -1
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
 				}
 			}
 		}
@@ -1490,7 +1498,9 @@ delete_depth_small_v1604(struct rte_lpm
 *lpm, uint32_t ip_masked,
 
 	RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
 					if (lpm->tbl8[j].depth <= depth)
-						lpm->tbl8[j] =
 new_tbl8_entry;
+						__atomic_store(&lpm-

tbl8[j],

lib/librte_lpm/rte_lpm.c +4 -1
+							&new_tbl8_entry,
+							__ATOMIC_RELAXED);
 				}
 			}
 		}
@@ -1646,7 +1656,8 @@ delete_depth_big_v20(struct rte_lpm_v20
 *lpm, uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}
lib/librte_lpm/rte_lpm.c +2 -1
 	}
@@ -1677,7 +1688,8 @@ delete_depth_big_v20(struct rte_lpm_v20
 *lpm, uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index],
 &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);
Quoted 1 line

tbl8_free_v20(lpm->tbl8, tbl8_group_start);

tbl8_alloc_v20/tbl8_free_v20 need to be updated to use __atomic_store

Quoted 8 lines
Quoted 2 lines

tbl8_alloc_v20/tbl8_free_v20 updates a single field of table entry. The process is already atomic. Do we really need to use __atomic_store?

I thought we agreed that all the tbl8 stores will use __atomic_store. IMO, it is better to use C11 atomic built-ins entirely, at least for the data structures used in reader-writer scenario. Otherwise, the code does not follow C11 memory model completely. (I do not know what to call such a model).

OK, change will be made in next version.

Quoted 33 lines
Quoted 33 lines
Quoted 14 lines
Quoted 14 lines
lib/librte_lpm/rte_lpm.c +2 -1
 	}
@@ -1730,7 +1742,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 		 */
 		for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
 			if (lpm->tbl8[i].depth <= depth)
-				lpm->tbl8[i] = new_tbl8_entry;
+				__atomic_store(&lpm->tbl8[i],
 &new_tbl8_entry,
+						__ATOMIC_RELAXED);
 		}

}

Quoted 13 lines
Quoted 11 lines
lib/librte_lpm/rte_lpm.c +2 -1
@@ -1761,7 +1774,8 @@ delete_depth_big_v1604(struct rte_lpm *lpm,
 uint32_t ip_masked,
 		/* 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_store(&lpm->tbl24[tbl24_index],
 &new_tbl24_entry,
+				__ATOMIC_RELAXED);
 		__atomic_thread_fence(__ATOMIC_RELEASE);

tbl8_free_v1604(lpm->tbl8, tbl8_group_start);

tbl8_alloc_v1604 /tbl8_free_v1604 need to be updated to use __atomic_store

Ditto.

Quoted 4 lines
Quoted 4 lines

}
--
2.17.1

#11 lore raw
In reply to Ruifeng Wang

03/07/2019 07:44, Ruifeng Wang:

Quoted 3 lines

v4: updated test data
v3: updated test data
v2: no changes

Ruifeng, it is not easy to follow your changes because you don't use --in-reply-to when sending new versions. So they are not in the same mail thread. It would also help to make a cover letter and make every versions a child of the very first cover letter.

Please pass the word to you colleagues in Arm, it seems to be a common miss in the process. Thanks

#12 lore raw
Quoted 17 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Friday, July 5, 2019 04:25
To: Ruifeng Wang (Arm Technology China) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Honnappa Nagarahalli <[email protected]>; Gavin Hu (Arm Technology China) <[email protected]>; nd <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v4 1/3] lib/lpm: memory orderings to avoid race conditions for v1604

03/07/2019 07:44, Ruifeng Wang:

Quoted 3 lines

v4: updated test data
v3: updated test data
v2: no changes

Quoted 9 lines

Ruifeng, it is not easy to follow your changes because you don't use --in- reply-to when sending new versions. So they are not in the same mail thread. It would also help to make a cover letter and make every versions a child of the very first cover letter.

Please pass the word to you colleagues in Arm, it seems to be a common miss in the process.
Thanks

Thanks for reminding. We'll review Contributor's Guidelines and stick to it.