Skip to contentSkip to conversation

eal: fix side effects in align mul macros

PATCH Superseded [email protected] 5 participants 19 messages May 9, 2021 – Jul 14, 2023 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 eal: fix side effects in align mul macros

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside the macro multiple times. For example:

	RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);

Here rte_rdtsc() call is expanded multiple times in the macro causing it to return different values that leads to unintended side effects.

Also, update common_autotest to detect macro side effects.

Fixes f56e551485d5 ("eal: add macro to align value to the nearest multiple")

---
Retainded original author signoff, split patches as some of the changes involved in modifying drivers.

 app/test/test_common.c       | 27 ++++++++++++++++++++++++++-
 lib/eal/include/rte_common.h | 28 +++++++++++++++++++---------
 2 files changed, 45 insertions(+), 10 deletions(-)
app/test/test_common.c +26 -1
index 12bd1cad90..0dbb87e741 100644
@@ -18,6 +18,13 @@
 	{printf(x "() test failed!\n");\
 	return -1;}
 
+static uintptr_t callcount;
+static uintptr_t
+inc_callcount(void)
+{
+	return callcount++;
+}
+
 /* this is really a sanity check */
 static int
 test_macros(int __rte_unused unused_parm)
@@ -29,7 +36,19 @@ test_macros(int __rte_unused unused_parm)
 	{printf(#x "() test failed!\n");\
 	return -1;}
 
+#define TEST_SIDE_EFFECT_2(macro, type1, type2)                                \
+	do {                                                                   \
+		callcount = 0;                                                 \
+		(void)macro((type1)inc_callcount(), (type2)inc_callcount());   \
+		if (callcount != 2) {                                          \
+			printf(#macro " has side effects: callcount=%u\n",     \
+			       (unsigned int)callcount);                       \
+			ret = -1;                                              \
+		}                                                              \
+	} while (0)
+
 	uintptr_t unused = 0;
+	int ret = 0;
 
 	RTE_SET_USED(unused);
 
@@ -47,7 +66,13 @@ test_macros(int __rte_unused unused_parm)
 	if (strncmp(RTE_STR(test), "test", sizeof("test")))
 		FAIL_MACRO(RTE_STR);
 
-	return 0;
+	TEST_SIDE_EFFECT_2(RTE_PTR_ADD, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_DIFF, void *, void *);
+	TEST_SIDE_EFFECT_2(RTE_PTR_SUB, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_CEIL, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_FLOOR, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_NEAR, unsigned int, unsigned int);
+	return ret;
 }
 
 static int
lib/eal/include/rte_common.h +19 -9
index d5a32c66a5..a142596587 100644
@@ -329,27 +329,37 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * value will be of the same type as the first parameter and will be no lower
  * than the first parameter.
  */
-#define RTE_ALIGN_MUL_CEIL(v, mul) \
-	((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
+#define RTE_ALIGN_MUL_CEIL(v, mul)                                             \
+	__extension__({                                                        \
+		typeof(v) _vc = (v);                                           \
+		typeof(v) _mc = (mul);                                         \
+		((_vc + _mc - 1) / _mc) * _mc;                                 \
+	})
 
 /**
  * Macro to align a value to the multiple of given value. The resultant
  * value will be of the same type as the first parameter and will be no higher
  * than the first parameter.
  */
-#define RTE_ALIGN_MUL_FLOOR(v, mul) \
-	(((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
+#define RTE_ALIGN_MUL_FLOOR(v, mul)                                            \
+	__extension__({                                                        \
+		typeof(v) _vf = (v);                                           \
+		typeof(v) _mf = (mul);                                         \
+		(_vf / _mf) * _mf;                                             \
+	})
 
 /**
  * Macro to align value to the nearest multiple of the given value.
  * The resultant value might be greater than or less than the first parameter
  * whichever difference is the lowest.
  */
-#define RTE_ALIGN_MUL_NEAR(v, mul)				\
-	({							\
-		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
-		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
-		(ceil - (v)) > ((v) - floor) ? floor : ceil;	\
+#define RTE_ALIGN_MUL_NEAR(v, mul)                                             \
+	__extension__({                                                        \
+		typeof(v) _v = (v);                                            \
+		typeof(v) _m = (mul);                                          \
+		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(_v, _m);                 \
+		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(_v, _m);                   \
+		(ceil - _v) > (_v - floor) ? floor : ceil;                     \
 	})
 
 /**

--
2.17.1

#1 lore raw

PATCH 2/2 eal: fix side effects in ptr align macros

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside RTE_*_ALIGN macros. Update common_autotest to detect macro side effects. Workaround static arrays relying on RTE_ALIGN macros.

Fixes af75078fece3 ("first public release")
---
 app/test/test_common.c           |  6 ++++++
 drivers/net/e1000/e1000_ethdev.h |  7 ++++---
 drivers/net/ixgbe/ixgbe_ethdev.h |  6 ++++--
 drivers/net/txgbe/txgbe_ethdev.h |  6 ++++--
 lib/eal/include/rte_common.h     | 17 +++++++++++++----
 lib/ethdev/rte_eth_ctrl.h        |  5 +++--
 6 files changed, 34 insertions(+), 13 deletions(-)
app/test/test_common.c +6 -0
index 0dbb87e741..9efe3b10f9 100644
@@ -69,6 +69,12 @@ test_macros(int __rte_unused unused_parm)
 	TEST_SIDE_EFFECT_2(RTE_PTR_ADD, void *, size_t);
 	TEST_SIDE_EFFECT_2(RTE_PTR_DIFF, void *, void *);
 	TEST_SIDE_EFFECT_2(RTE_PTR_SUB, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_CEIL, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_FLOOR, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_CEIL, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_FLOOR, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_CEIL, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_FLOOR, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_NEAR, unsigned int, unsigned int);
drivers/net/e1000/e1000_ethdev.h +4 -3
index 3b4d9c3ee6..155d825d89 100644
@@ -332,9 +332,10 @@ struct igb_eth_syn_filter_ele {
 };
 
 #define IGB_FLEX_FILTER_MAXLEN	128	/**< bytes to use in flex filter. */
-#define IGB_FLEX_FILTER_MASK_SIZE	\
-	(RTE_ALIGN(IGB_FLEX_FILTER_MAXLEN, CHAR_BIT) / CHAR_BIT)
-					/**< mask bytes in flex filter. */
+#define IGB_FLEX_FILTER_MASK_SIZE                                              \
+	(RTE_ALIGN_FLOOR(IGB_FLEX_FILTER_MAXLEN + (CHAR_BIT - 1), CHAR_BIT) /  \
+	 CHAR_BIT)
+/**< mask bytes in flex filter. */
 
 /**
  * A structure used to define the flex filter entry
drivers/net/ixgbe/ixgbe_ethdev.h +4 -2
index a0ce18ca24..f2f8b943d2 100644
@@ -311,8 +311,10 @@ struct ixgbe_5tuple_filter {
 	uint16_t queue;       /* rx queue assigned to */
 };
 
-#define IXGBE_5TUPLE_ARRAY_SIZE \
-	(RTE_ALIGN(IXGBE_MAX_FTQF_FILTERS, (sizeof(uint32_t) * NBBY)) / \
+#define IXGBE_5TUPLE_ARRAY_SIZE                                                \
+	(RTE_ALIGN_FLOOR(IXGBE_MAX_FTQF_FILTERS + (sizeof(uint32_t) * NBBY) -  \
+				 1,                                            \
+			 (sizeof(uint32_t) * NBBY)) /                          \
 	 (sizeof(uint32_t) * NBBY))
 
 struct ixgbe_ethertype_filter {
drivers/net/txgbe/txgbe_ethdev.h +4 -2
index 3021933965..05537b34c7 100644
@@ -219,8 +219,10 @@ struct txgbe_5tuple_filter {
 	uint16_t queue;       /* rx queue assigned to */
 };
 
-#define TXGBE_5TUPLE_ARRAY_SIZE \
-	(RTE_ALIGN(TXGBE_MAX_FTQF_FILTERS, (sizeof(uint32_t) * NBBY)) / \
+#define TXGBE_5TUPLE_ARRAY_SIZE                                                \
+	(RTE_ALIGN_FLOOR(TXGBE_MAX_FTQF_FILTERS + (sizeof(uint32_t) * NBBY) -  \
+				 1,                                            \
+			 (sizeof(uint32_t) * NBBY)) /                          \
 	 (sizeof(uint32_t) * NBBY))
 
 struct txgbe_ethertype_filter {
lib/eal/include/rte_common.h +13 -4
index a142596587..6acd067b5c 100644
@@ -294,8 +294,13 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * point to an address no lower than the first parameter. Second parameter
  * must be a power-of-two value.
  */
-#define RTE_PTR_ALIGN_CEIL(ptr, align) \
-	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
+#define RTE_PTR_ALIGN_CEIL(ptr, align)                                         \
+	__extension__({                                                        \
+		typeof(ptr) _pc = (ptr);                                       \
+		typeof(align) _ac = (align);                                   \
+		RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(_pc, _ac - 1),    \
+				    _ac);                                      \
+	})
 
 /**
  * Macro to align a value to a given power-of-two. The resultant value
@@ -303,8 +308,12 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * than the first parameter. Second parameter must be a power-of-two
  * value.
  */
-#define RTE_ALIGN_CEIL(val, align) \
-	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
+#define RTE_ALIGN_CEIL(val, align)                                             \
+	__extension__({                                                        \
+		typeof(val) _vc = (val);                                       \
+		typeof(val) _ac = (typeof(val))(align);                        \
+		RTE_ALIGN_FLOOR((_vc + _ac - 1), _ac);                         \
+	})
 
 /**
  * Macro to align a pointer to a given power-of-two. The resultant
lib/ethdev/rte_eth_ctrl.h +3 -2
index 42652f9cce..863e56170b 100644
@@ -431,8 +431,9 @@ enum rte_fdir_mode {
 };
 
 #define UINT64_BIT (CHAR_BIT * sizeof(uint64_t))
-#define RTE_FLOW_MASK_ARRAY_SIZE \
-	(RTE_ALIGN(RTE_ETH_FLOW_MAX, UINT64_BIT)/UINT64_BIT)
+#define RTE_FLOW_MASK_ARRAY_SIZE                                               \
+	(RTE_ALIGN_FLOOR(RTE_ETH_FLOW_MAX + (UINT64_BIT - 1), UINT64_BIT) /    \
+	 UINT64_BIT)
 
 /**
  * A structure used to get the information of flow director filter.
Signature · 1 line
2.17.1
#2 lore raw

Re: [dpdk-dev] [PATCH 2/2] eal: fix side effects in ptr align macros

Quoted 11 lines · On Sun, 9 May 2021 22:48:02 +0530 <[email protected]> wrote:

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside RTE_*_ALIGN macros. Update common_autotest to detect macro side effects. Workaround static arrays relying on RTE_ALIGN macros.

Fixes af75078fece3 ("first public release")

Why not split these up? It looks like the Intel driver and common part could be separate?

#3 lore raw

Re: [dpdk-dev] [EXT] Re: [PATCH 2/2] eal: fix side effects in ptr align macros

Quoted 9 lines
Quoted 6 lines · On Sun, 9 May 2021 22:48:02 +0530 <[email protected]> wrote:

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside RTE_*_ALIGN macros. Update common_autotest to detect macro side effects. Workaround static arrays relying on RTE_ALIGN macros.

Quoted 8 lines
Quoted 5 lines
Fixes af75078fece3 ("first public release")

Why not split these up? It looks like the Intel driver and common part could be separate?

The common changes break intel/mlx5 driver compilation can't separate them.

#4 lore raw

Re: [dpdk-dev] [EXT] Re: [PATCH 2/2] eal: fix side effects in ptr align macros

Hello Pavan,

Quoted 13 lines · Pavan Nikhilesh Bhagavatula
Quoted 13 lines
Quoted 9 lines · On Sun, 9 May 2021 22:48:02 +0530 <[email protected]> wrote:

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside RTE_*_ALIGN macros. Update common_autotest to detect macro side effects. Workaround static arrays relying on RTE_ALIGN macros.

Fixes af75078fece3 ("first public release")
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Why not split these up? It looks like the Intel driver and common part could be separate?

The common changes break intel/mlx5 driver compilation can't separate them.

Thanks for working on this.
The CI still reports a build error in mlx5.

Signature · 1 line
David Marchand
#5 lore raw

PATCH v2 1/2 eal: fix side effects in align mul macros

In reply to [email protected]

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside the macro multiple times. For example:

	RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);

Here rte_rdtsc() call is expanded multiple times in the macro causing it to return different values that leads to unintended side effects.

Also, update common_autotest to detect macro side effects.

Fixes f56e551485d5 ("eal: add macro to align value to the nearest multiple")

---
v2 Changes:
- Fix mlx5 compilation.

 app/test/test_common.c       | 27 ++++++++++++++++++++++++++-
 lib/eal/include/rte_common.h | 28 +++++++++++++++++++---------
 2 files changed, 45 insertions(+), 10 deletions(-)
app/test/test_common.c +26 -1
index 12bd1cad90..0dbb87e741 100644
@@ -18,6 +18,13 @@
 	{printf(x "() test failed!\n");\
 	return -1;}
 
+static uintptr_t callcount;
+static uintptr_t
+inc_callcount(void)
+{
+	return callcount++;
+}
+
 /* this is really a sanity check */
 static int
 test_macros(int __rte_unused unused_parm)
@@ -29,7 +36,19 @@ test_macros(int __rte_unused unused_parm)
 	{printf(#x "() test failed!\n");\
 	return -1;}
 
+#define TEST_SIDE_EFFECT_2(macro, type1, type2)                                \
+	do {                                                                   \
+		callcount = 0;                                                 \
+		(void)macro((type1)inc_callcount(), (type2)inc_callcount());   \
+		if (callcount != 2) {                                          \
+			printf(#macro " has side effects: callcount=%u\n",     \
+			       (unsigned int)callcount);                       \
+			ret = -1;                                              \
+		}                                                              \
+	} while (0)
+
 	uintptr_t unused = 0;
+	int ret = 0;
 
 	RTE_SET_USED(unused);
 
@@ -47,7 +66,13 @@ test_macros(int __rte_unused unused_parm)
 	if (strncmp(RTE_STR(test), "test", sizeof("test")))
 		FAIL_MACRO(RTE_STR);
 
-	return 0;
+	TEST_SIDE_EFFECT_2(RTE_PTR_ADD, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_DIFF, void *, void *);
+	TEST_SIDE_EFFECT_2(RTE_PTR_SUB, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_CEIL, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_FLOOR, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_NEAR, unsigned int, unsigned int);
+	return ret;
 }
 
 static int
lib/eal/include/rte_common.h +19 -9
index d5a32c66a5..a142596587 100644
@@ -329,27 +329,37 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * value will be of the same type as the first parameter and will be no lower
  * than the first parameter.
  */
-#define RTE_ALIGN_MUL_CEIL(v, mul) \
-	((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
+#define RTE_ALIGN_MUL_CEIL(v, mul)                                             \
+	__extension__({                                                        \
+		typeof(v) _vc = (v);                                           \
+		typeof(v) _mc = (mul);                                         \
+		((_vc + _mc - 1) / _mc) * _mc;                                 \
+	})
 
 /**
  * Macro to align a value to the multiple of given value. The resultant
  * value will be of the same type as the first parameter and will be no higher
  * than the first parameter.
  */
-#define RTE_ALIGN_MUL_FLOOR(v, mul) \
-	(((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
+#define RTE_ALIGN_MUL_FLOOR(v, mul)                                            \
+	__extension__({                                                        \
+		typeof(v) _vf = (v);                                           \
+		typeof(v) _mf = (mul);                                         \
+		(_vf / _mf) * _mf;                                             \
+	})
 
 /**
  * Macro to align value to the nearest multiple of the given value.
  * The resultant value might be greater than or less than the first parameter
  * whichever difference is the lowest.
  */
-#define RTE_ALIGN_MUL_NEAR(v, mul)				\
-	({							\
-		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
-		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
-		(ceil - (v)) > ((v) - floor) ? floor : ceil;	\
+#define RTE_ALIGN_MUL_NEAR(v, mul)                                             \
+	__extension__({                                                        \
+		typeof(v) _v = (v);                                            \
+		typeof(v) _m = (mul);                                          \
+		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(_v, _m);                 \
+		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(_v, _m);                   \
+		(ceil - _v) > (_v - floor) ? floor : ceil;                     \
 	})
 
 /**

--
2.17.1

#6 lore raw

PATCH v2 2/2 eal: fix side effects in ptr align macros

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside RTE_*_ALIGN macros. Update common_autotest to detect macro side effects. Workaround static arrays relying on RTE_ALIGN macros.

Fixes af75078fece3 ("first public release")
---
 app/test/test_common.c           |  6 ++++++
 drivers/net/e1000/e1000_ethdev.h |  7 ++++---
 drivers/net/ixgbe/ixgbe_ethdev.h |  6 ++++--
 drivers/net/mlx5/mlx5_rxtx_vec.h |  7 ++++---
 drivers/net/txgbe/txgbe_ethdev.h |  6 ++++--
 lib/eal/include/rte_common.h     | 17 +++++++++++++----
 lib/ethdev/rte_eth_ctrl.h        |  5 +++--
 7 files changed, 38 insertions(+), 16 deletions(-)
app/test/test_common.c +6 -0
index 0dbb87e741..9efe3b10f9 100644
@@ -69,6 +69,12 @@ test_macros(int __rte_unused unused_parm)
 	TEST_SIDE_EFFECT_2(RTE_PTR_ADD, void *, size_t);
 	TEST_SIDE_EFFECT_2(RTE_PTR_DIFF, void *, void *);
 	TEST_SIDE_EFFECT_2(RTE_PTR_SUB, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_CEIL, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_FLOOR, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_CEIL, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_FLOOR, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_CEIL, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_FLOOR, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_NEAR, unsigned int, unsigned int);
drivers/net/e1000/e1000_ethdev.h +4 -3
index 3b4d9c3ee6..155d825d89 100644
@@ -332,9 +332,10 @@ struct igb_eth_syn_filter_ele {
 };
 
 #define IGB_FLEX_FILTER_MAXLEN	128	/**< bytes to use in flex filter. */
-#define IGB_FLEX_FILTER_MASK_SIZE	\
-	(RTE_ALIGN(IGB_FLEX_FILTER_MAXLEN, CHAR_BIT) / CHAR_BIT)
-					/**< mask bytes in flex filter. */
+#define IGB_FLEX_FILTER_MASK_SIZE                                              \
+	(RTE_ALIGN_FLOOR(IGB_FLEX_FILTER_MAXLEN + (CHAR_BIT - 1), CHAR_BIT) /  \
+	 CHAR_BIT)
+/**< mask bytes in flex filter. */
 
 /**
  * A structure used to define the flex filter entry
drivers/net/ixgbe/ixgbe_ethdev.h +4 -2
index a0ce18ca24..f2f8b943d2 100644
@@ -311,8 +311,10 @@ struct ixgbe_5tuple_filter {
 	uint16_t queue;       /* rx queue assigned to */
 };
 
-#define IXGBE_5TUPLE_ARRAY_SIZE \
-	(RTE_ALIGN(IXGBE_MAX_FTQF_FILTERS, (sizeof(uint32_t) * NBBY)) / \
+#define IXGBE_5TUPLE_ARRAY_SIZE                                                \
+	(RTE_ALIGN_FLOOR(IXGBE_MAX_FTQF_FILTERS + (sizeof(uint32_t) * NBBY) -  \
+				 1,                                            \
+			 (sizeof(uint32_t) * NBBY)) /                          \
 	 (sizeof(uint32_t) * NBBY))
 
 struct ixgbe_ethertype_filter {
drivers/net/mlx5/mlx5_rxtx_vec.h +4 -3
index 93b4f517bb..5d105db474 100644
@@ -42,8 +42,8 @@ S_ASSERT_RTE_MBUF(offsetof(struct rte_mbuf, hash) ==
 S_ASSERT_RTE_MBUF(offsetof(struct rte_mbuf, ol_flags) ==
 		  offsetof(struct rte_mbuf, rearm_data) + 8);
 S_ASSERT_RTE_MBUF(offsetof(struct rte_mbuf, rearm_data) ==
-		  RTE_ALIGN(offsetof(struct rte_mbuf, rearm_data), 16));
-
+		  RTE_ALIGN_FLOOR(offsetof(struct rte_mbuf, rearm_data) + 15,
+				  16));
 /* rxq_burst_v() */
 S_ASSERT_RTE_MBUF(offsetof(struct rte_mbuf, pkt_len) ==
 		  offsetof(struct rte_mbuf, rx_descriptor_fields1) + 4);
@@ -63,7 +63,8 @@ S_ASSERT_MLX5_CQE(offsetof(struct mlx5_cqe, vlan_info) ==
 S_ASSERT_MLX5_CQE(offsetof(struct mlx5_cqe, lro_num_seg) + 12 ==
 		  offsetof(struct mlx5_cqe, byte_cnt));
 S_ASSERT_MLX5_CQE(offsetof(struct mlx5_cqe, sop_drop_qpn) ==
-		  RTE_ALIGN(offsetof(struct mlx5_cqe, sop_drop_qpn), 8));
+		  RTE_ALIGN_FLOOR(offsetof(struct mlx5_cqe, sop_drop_qpn) + 7,
+				  8));
 S_ASSERT_MLX5_CQE(offsetof(struct mlx5_cqe, op_own) ==
 		  offsetof(struct mlx5_cqe, sop_drop_qpn) + 7);
 
drivers/net/txgbe/txgbe_ethdev.h +4 -2
index 3021933965..05537b34c7 100644
@@ -219,8 +219,10 @@ struct txgbe_5tuple_filter {
 	uint16_t queue;       /* rx queue assigned to */
 };
 
-#define TXGBE_5TUPLE_ARRAY_SIZE \
-	(RTE_ALIGN(TXGBE_MAX_FTQF_FILTERS, (sizeof(uint32_t) * NBBY)) / \
+#define TXGBE_5TUPLE_ARRAY_SIZE                                                \
+	(RTE_ALIGN_FLOOR(TXGBE_MAX_FTQF_FILTERS + (sizeof(uint32_t) * NBBY) -  \
+				 1,                                            \
+			 (sizeof(uint32_t) * NBBY)) /                          \
 	 (sizeof(uint32_t) * NBBY))
 
 struct txgbe_ethertype_filter {
lib/eal/include/rte_common.h +13 -4
index a142596587..6acd067b5c 100644
@@ -294,8 +294,13 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * point to an address no lower than the first parameter. Second parameter
  * must be a power-of-two value.
  */
-#define RTE_PTR_ALIGN_CEIL(ptr, align) \
-	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
+#define RTE_PTR_ALIGN_CEIL(ptr, align)                                         \
+	__extension__({                                                        \
+		typeof(ptr) _pc = (ptr);                                       \
+		typeof(align) _ac = (align);                                   \
+		RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(_pc, _ac - 1),    \
+				    _ac);                                      \
+	})
 
 /**
  * Macro to align a value to a given power-of-two. The resultant value
@@ -303,8 +308,12 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * than the first parameter. Second parameter must be a power-of-two
  * value.
  */
-#define RTE_ALIGN_CEIL(val, align) \
-	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
+#define RTE_ALIGN_CEIL(val, align)                                             \
+	__extension__({                                                        \
+		typeof(val) _vc = (val);                                       \
+		typeof(val) _ac = (typeof(val))(align);                        \
+		RTE_ALIGN_FLOOR((_vc + _ac - 1), _ac);                         \
+	})
 
 /**
  * Macro to align a pointer to a given power-of-two. The resultant
lib/ethdev/rte_eth_ctrl.h +3 -2
index 42652f9cce..863e56170b 100644
@@ -431,8 +431,9 @@ enum rte_fdir_mode {
 };
 
 #define UINT64_BIT (CHAR_BIT * sizeof(uint64_t))
-#define RTE_FLOW_MASK_ARRAY_SIZE \
-	(RTE_ALIGN(RTE_ETH_FLOW_MAX, UINT64_BIT)/UINT64_BIT)
+#define RTE_FLOW_MASK_ARRAY_SIZE                                               \
+	(RTE_ALIGN_FLOOR(RTE_ETH_FLOW_MAX + (UINT64_BIT - 1), UINT64_BIT) /    \
+	 UINT64_BIT)
 
 /**
  * A structure used to get the information of flow director filter.

--
2.17.1

#7 lore raw

PATCH v3 1/2 eal: fix side effects in align mul macros

In reply to [email protected]

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside the macro multiple times. For example:

	RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);

Here rte_rdtsc() call is expanded multiple times in the macro causing it to return different values that leads to unintended side effects.

Also, update common_autotest to detect macro side effects.

Fixes f56e551485d5 ("eal: add macro to align value to the nearest multiple")

---
v3 Changes:
- Fix examples/bbdev_app compilation.

v2 Changes:
- Fix mlx5 compilation.

 app/test/test_common.c       | 27 ++++++++++++++++++++++++++-
 lib/eal/include/rte_common.h | 28 +++++++++++++++++++---------
 2 files changed, 45 insertions(+), 10 deletions(-)
app/test/test_common.c +26 -1
index 12bd1cad90..0dbb87e741 100644
@@ -18,6 +18,13 @@
 	{printf(x "() test failed!\n");\
 	return -1;}
 
+static uintptr_t callcount;
+static uintptr_t
+inc_callcount(void)
+{
+	return callcount++;
+}
+
 /* this is really a sanity check */
 static int
 test_macros(int __rte_unused unused_parm)
@@ -29,7 +36,19 @@ test_macros(int __rte_unused unused_parm)
 	{printf(#x "() test failed!\n");\
 	return -1;}
 
+#define TEST_SIDE_EFFECT_2(macro, type1, type2)                                \
+	do {                                                                   \
+		callcount = 0;                                                 \
+		(void)macro((type1)inc_callcount(), (type2)inc_callcount());   \
+		if (callcount != 2) {                                          \
+			printf(#macro " has side effects: callcount=%u\n",     \
+			       (unsigned int)callcount);                       \
+			ret = -1;                                              \
+		}                                                              \
+	} while (0)
+
 	uintptr_t unused = 0;
+	int ret = 0;
 
 	RTE_SET_USED(unused);
 
@@ -47,7 +66,13 @@ test_macros(int __rte_unused unused_parm)
 	if (strncmp(RTE_STR(test), "test", sizeof("test")))
 		FAIL_MACRO(RTE_STR);
 
-	return 0;
+	TEST_SIDE_EFFECT_2(RTE_PTR_ADD, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_DIFF, void *, void *);
+	TEST_SIDE_EFFECT_2(RTE_PTR_SUB, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_CEIL, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_FLOOR, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_NEAR, unsigned int, unsigned int);
+	return ret;
 }
 
 static int
lib/eal/include/rte_common.h +19 -9
index d5a32c66a5..a142596587 100644
@@ -329,27 +329,37 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * value will be of the same type as the first parameter and will be no lower
  * than the first parameter.
  */
-#define RTE_ALIGN_MUL_CEIL(v, mul) \
-	((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
+#define RTE_ALIGN_MUL_CEIL(v, mul)                                             \
+	__extension__({                                                        \
+		typeof(v) _vc = (v);                                           \
+		typeof(v) _mc = (mul);                                         \
+		((_vc + _mc - 1) / _mc) * _mc;                                 \
+	})
 
 /**
  * Macro to align a value to the multiple of given value. The resultant
  * value will be of the same type as the first parameter and will be no higher
  * than the first parameter.
  */
-#define RTE_ALIGN_MUL_FLOOR(v, mul) \
-	(((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
+#define RTE_ALIGN_MUL_FLOOR(v, mul)                                            \
+	__extension__({                                                        \
+		typeof(v) _vf = (v);                                           \
+		typeof(v) _mf = (mul);                                         \
+		(_vf / _mf) * _mf;                                             \
+	})
 
 /**
  * Macro to align value to the nearest multiple of the given value.
  * The resultant value might be greater than or less than the first parameter
  * whichever difference is the lowest.
  */
-#define RTE_ALIGN_MUL_NEAR(v, mul)				\
-	({							\
-		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
-		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
-		(ceil - (v)) > ((v) - floor) ? floor : ceil;	\
+#define RTE_ALIGN_MUL_NEAR(v, mul)                                             \
+	__extension__({                                                        \
+		typeof(v) _v = (v);                                            \
+		typeof(v) _m = (mul);                                          \
+		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(_v, _m);                 \
+		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(_v, _m);                   \
+		(ceil - _v) > (_v - floor) ? floor : ceil;                     \
 	})
 
 /**

--
2.17.1

#8 lore raw

PATCH v3 2/2 eal: fix side effects in ptr align macros

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside RTE_*_ALIGN macros. Update common_autotest to detect macro side effects. Workaround static arrays relying on RTE_ALIGN macros.

Fixes af75078fece3 ("first public release")
---
 app/test/test_common.c           |  6 ++++++
 drivers/net/e1000/e1000_ethdev.h |  7 ++++---
 drivers/net/ixgbe/ixgbe_ethdev.h |  6 ++++--
 drivers/net/mlx5/mlx5_rxtx_vec.h |  7 ++++---
 drivers/net/txgbe/txgbe_ethdev.h |  6 ++++--
 examples/bbdev_app/main.c        |  2 +-
 lib/eal/include/rte_common.h     | 17 +++++++++++++----
 lib/ethdev/rte_eth_ctrl.h        |  5 +++--
 8 files changed, 39 insertions(+), 17 deletions(-)
app/test/test_common.c +6 -0
index 0dbb87e741..9efe3b10f9 100644
@@ -69,6 +69,12 @@ test_macros(int __rte_unused unused_parm)
 	TEST_SIDE_EFFECT_2(RTE_PTR_ADD, void *, size_t);
 	TEST_SIDE_EFFECT_2(RTE_PTR_DIFF, void *, void *);
 	TEST_SIDE_EFFECT_2(RTE_PTR_SUB, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_CEIL, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_FLOOR, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_CEIL, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_FLOOR, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_CEIL, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_FLOOR, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_NEAR, unsigned int, unsigned int);
drivers/net/e1000/e1000_ethdev.h +4 -3
index 3b4d9c3ee6..155d825d89 100644
@@ -332,9 +332,10 @@ struct igb_eth_syn_filter_ele {
 };
 
 #define IGB_FLEX_FILTER_MAXLEN	128	/**< bytes to use in flex filter. */
-#define IGB_FLEX_FILTER_MASK_SIZE	\
-	(RTE_ALIGN(IGB_FLEX_FILTER_MAXLEN, CHAR_BIT) / CHAR_BIT)
-					/**< mask bytes in flex filter. */
+#define IGB_FLEX_FILTER_MASK_SIZE                                              \
+	(RTE_ALIGN_FLOOR(IGB_FLEX_FILTER_MAXLEN + (CHAR_BIT - 1), CHAR_BIT) /  \
+	 CHAR_BIT)
+/**< mask bytes in flex filter. */
 
 /**
  * A structure used to define the flex filter entry
drivers/net/ixgbe/ixgbe_ethdev.h +4 -2
index a0ce18ca24..f2f8b943d2 100644
@@ -311,8 +311,10 @@ struct ixgbe_5tuple_filter {
 	uint16_t queue;       /* rx queue assigned to */
 };
 
-#define IXGBE_5TUPLE_ARRAY_SIZE \
-	(RTE_ALIGN(IXGBE_MAX_FTQF_FILTERS, (sizeof(uint32_t) * NBBY)) / \
+#define IXGBE_5TUPLE_ARRAY_SIZE                                                \
+	(RTE_ALIGN_FLOOR(IXGBE_MAX_FTQF_FILTERS + (sizeof(uint32_t) * NBBY) -  \
+				 1,                                            \
+			 (sizeof(uint32_t) * NBBY)) /                          \
 	 (sizeof(uint32_t) * NBBY))
 
 struct ixgbe_ethertype_filter {
drivers/net/mlx5/mlx5_rxtx_vec.h +4 -3
index 93b4f517bb..5d105db474 100644
@@ -42,8 +42,8 @@ S_ASSERT_RTE_MBUF(offsetof(struct rte_mbuf, hash) ==
 S_ASSERT_RTE_MBUF(offsetof(struct rte_mbuf, ol_flags) ==
 		  offsetof(struct rte_mbuf, rearm_data) + 8);
 S_ASSERT_RTE_MBUF(offsetof(struct rte_mbuf, rearm_data) ==
-		  RTE_ALIGN(offsetof(struct rte_mbuf, rearm_data), 16));
-
+		  RTE_ALIGN_FLOOR(offsetof(struct rte_mbuf, rearm_data) + 15,
+				  16));
 /* rxq_burst_v() */
 S_ASSERT_RTE_MBUF(offsetof(struct rte_mbuf, pkt_len) ==
 		  offsetof(struct rte_mbuf, rx_descriptor_fields1) + 4);
@@ -63,7 +63,8 @@ S_ASSERT_MLX5_CQE(offsetof(struct mlx5_cqe, vlan_info) ==
 S_ASSERT_MLX5_CQE(offsetof(struct mlx5_cqe, lro_num_seg) + 12 ==
 		  offsetof(struct mlx5_cqe, byte_cnt));
 S_ASSERT_MLX5_CQE(offsetof(struct mlx5_cqe, sop_drop_qpn) ==
-		  RTE_ALIGN(offsetof(struct mlx5_cqe, sop_drop_qpn), 8));
+		  RTE_ALIGN_FLOOR(offsetof(struct mlx5_cqe, sop_drop_qpn) + 7,
+				  8));
 S_ASSERT_MLX5_CQE(offsetof(struct mlx5_cqe, op_own) ==
 		  offsetof(struct mlx5_cqe, sop_drop_qpn) + 7);
 
drivers/net/txgbe/txgbe_ethdev.h +4 -2
index 3021933965..05537b34c7 100644
@@ -219,8 +219,10 @@ struct txgbe_5tuple_filter {
 	uint16_t queue;       /* rx queue assigned to */
 };
 
-#define TXGBE_5TUPLE_ARRAY_SIZE \
-	(RTE_ALIGN(TXGBE_MAX_FTQF_FILTERS, (sizeof(uint32_t) * NBBY)) / \
+#define TXGBE_5TUPLE_ARRAY_SIZE                                                \
+	(RTE_ALIGN_FLOOR(TXGBE_MAX_FTQF_FILTERS + (sizeof(uint32_t) * NBBY) -  \
+				 1,                                            \
+			 (sizeof(uint32_t) * NBBY)) /                          \
 	 (sizeof(uint32_t) * NBBY))
 
 struct txgbe_ethertype_filter {
examples/bbdev_app/main.c +1 -1
index 5251db0b16..3f8d2bb671 100644
@@ -45,7 +45,7 @@
 
 /* Hardcoded K value */
 #define K 40
-#define NCB (3 * RTE_ALIGN_CEIL(K + 4, 32))
+#define NCB (3 * RTE_ALIGN_FLOOR((K + 4) + 31, 32))
 
 #define CRC_24B_LEN 3
 
lib/eal/include/rte_common.h +13 -4
index a142596587..6acd067b5c 100644
@@ -294,8 +294,13 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * point to an address no lower than the first parameter. Second parameter
  * must be a power-of-two value.
  */
-#define RTE_PTR_ALIGN_CEIL(ptr, align) \
-	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
+#define RTE_PTR_ALIGN_CEIL(ptr, align)                                         \
+	__extension__({                                                        \
+		typeof(ptr) _pc = (ptr);                                       \
+		typeof(align) _ac = (align);                                   \
+		RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(_pc, _ac - 1),    \
+				    _ac);                                      \
+	})
 
 /**
  * Macro to align a value to a given power-of-two. The resultant value
@@ -303,8 +308,12 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * than the first parameter. Second parameter must be a power-of-two
  * value.
  */
-#define RTE_ALIGN_CEIL(val, align) \
-	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
+#define RTE_ALIGN_CEIL(val, align)                                             \
+	__extension__({                                                        \
+		typeof(val) _vc = (val);                                       \
+		typeof(val) _ac = (typeof(val))(align);                        \
+		RTE_ALIGN_FLOOR((_vc + _ac - 1), _ac);                         \
+	})
 
 /**
  * Macro to align a pointer to a given power-of-two. The resultant
lib/ethdev/rte_eth_ctrl.h +3 -2
index 42652f9cce..863e56170b 100644
@@ -431,8 +431,9 @@ enum rte_fdir_mode {
 };
 
 #define UINT64_BIT (CHAR_BIT * sizeof(uint64_t))
-#define RTE_FLOW_MASK_ARRAY_SIZE \
-	(RTE_ALIGN(RTE_ETH_FLOW_MAX, UINT64_BIT)/UINT64_BIT)
+#define RTE_FLOW_MASK_ARRAY_SIZE                                               \
+	(RTE_ALIGN_FLOOR(RTE_ETH_FLOW_MAX + (UINT64_BIT - 1), UINT64_BIT) /    \
+	 UINT64_BIT)
 
 /**
  * A structure used to get the information of flow director filter.
Signature · 1 line
2.17.1
#9 lore raw

Re: [dpdk-dev] [PATCH v3 2/2] eal: fix side effects in ptr align macros

Quoted 56 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Tuesday, May 11, 2021 03:40
To: [email protected]; Wang, Haiyue <[email protected]>; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Viacheslav Ovsiienko <[email protected]>; Jiawen Wu <[email protected]>; Jian Wang <[email protected]>; Chautru, Nicolas <[email protected]>; Thomas Monjalon <[email protected]>; Yigit, Ferruh <[email protected]>; Andrew Rybchenko <[email protected]>
Cc: [email protected]; Pavan Nikhilesh <[email protected]>; [email protected]
Subject: [dpdk-dev] [PATCH v3 2/2] eal: fix side effects in ptr align macros

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside RTE_*_ALIGN macros. Update common_autotest to detect macro side effects. Workaround static arrays relying on RTE_ALIGN macros.

Fixes af75078fece3 ("first public release")
---
 app/test/test_common.c           |  6 ++++++
 drivers/net/e1000/e1000_ethdev.h |  7 ++++---
 drivers/net/ixgbe/ixgbe_ethdev.h |  6 ++++--
 drivers/net/mlx5/mlx5_rxtx_vec.h |  7 ++++---
 drivers/net/txgbe/txgbe_ethdev.h |  6 ++++--
 examples/bbdev_app/main.c        |  2 +-
 lib/eal/include/rte_common.h     | 17 +++++++++++++----
 lib/ethdev/rte_eth_ctrl.h        |  5 +++--
 8 files changed, 39 insertions(+), 17 deletions(-)
app/test/test_common.c +6 -0
index 0dbb87e741..9efe3b10f9 100644
@@ -69,6 +69,12 @@ test_macros(int __rte_unused unused_parm)
 	TEST_SIDE_EFFECT_2(RTE_PTR_ADD, void *, size_t);
 	TEST_SIDE_EFFECT_2(RTE_PTR_DIFF, void *, void *);
 	TEST_SIDE_EFFECT_2(RTE_PTR_SUB, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_CEIL, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_FLOOR, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_CEIL, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_FLOOR, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_CEIL, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_FLOOR, unsigned int, unsigned int);
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_NEAR, unsigned int, unsigned int);
drivers/net/e1000/e1000_ethdev.h +0 -0
index 3b4d9c3ee6..155d825d89 100644
@@ -332,9 +332,10 @@ struct igb_eth_syn_filter_ele {
 };

drivers/net/e1000/e1000_ethdev.h struct igb_eth_syn_filter_ele { · line 339

Quoted 10 lines
drivers/net/e1000/e1000_ethdev.h +4 -3
 
 #define IGB_FLEX_FILTER_MAXLEN	128	/**< bytes to use in flex filter. */
-#define IGB_FLEX_FILTER_MASK_SIZE	\
-	(RTE_ALIGN(IGB_FLEX_FILTER_MAXLEN, CHAR_BIT) / CHAR_BIT)
-					/**< mask bytes in flex filter. */
+#define IGB_FLEX_FILTER_MASK_SIZE                                              \
+	(RTE_ALIGN_FLOOR(IGB_FLEX_FILTER_MAXLEN + (CHAR_BIT - 1), CHAR_BIT) /  \
+	 CHAR_BIT)
+/**< mask bytes in flex filter. */
 

Since:
RTE_ALIGN --> RTE_ALIGN_CEIL(val, align) --> RTE_ALIGN_FLOOR(...)

Why only change 'RTE_ALIGN_CEIL', but then call 'RTE_ALIGN_FLOOR' directly ? Sorry, can't get the point. : - (

Why not change 'RTE_ALIGN' if it has issue ?

Quoted 54 lines
lib/eal/include/rte_common.h +13 -4
index a142596587..6acd067b5c 100644
@@ -294,8 +294,13 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * point to an address no lower than the first parameter. Second parameter
  * must be a power-of-two value.
  */
-#define RTE_PTR_ALIGN_CEIL(ptr, align) \
-	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
+#define RTE_PTR_ALIGN_CEIL(ptr, align)                                         \
+	__extension__({                                                        \
+		typeof(ptr) _pc = (ptr);                                       \
+		typeof(align) _ac = (align);                                   \
+		RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(_pc, _ac - 1),    \
+				    _ac);                                      \
+	})
 
 /**
  * Macro to align a value to a given power-of-two. The resultant value
@@ -303,8 +308,12 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * than the first parameter. Second parameter must be a power-of-two
  * value.
  */
-#define RTE_ALIGN_CEIL(val, align) \
-	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
+#define RTE_ALIGN_CEIL(val, align)                                             \
+	__extension__({                                                        \
+		typeof(val) _vc = (val);                                       \
+		typeof(val) _ac = (typeof(val))(align);                        \
+		RTE_ALIGN_FLOOR((_vc + _ac - 1), _ac);                         \
+	})
 
 /**
  * Macro to align a pointer to a given power-of-two. The resultant
lib/ethdev/rte_eth_ctrl.h +3 -3
index 42652f9cce..863e56170b 100644
@@ -431,8 +431,9 @@ enum rte_fdir_mode {
 };
 
 #define UINT64_BIT (CHAR_BIT * sizeof(uint64_t))
-#define RTE_FLOW_MASK_ARRAY_SIZE \
-	(RTE_ALIGN(RTE_ETH_FLOW_MAX, UINT64_BIT)/UINT64_BIT)
+#define RTE_FLOW_MASK_ARRAY_SIZE                                               \
+	(RTE_ALIGN_FLOOR(RTE_ETH_FLOW_MAX + (UINT64_BIT - 1), UINT64_BIT) /    \
+	 UINT64_BIT)
 
 /**
  * A structure used to get the information of flow director filter.
--

2.17.1

#10 lore raw

Re: [dpdk-dev] [PATCH v3 2/2] eal: fix side effects in ptr align macros

Quoted 86 lines
Quoted 4 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Tuesday, May 11, 2021 03:40
To: [email protected]; Wang, Haiyue

<[email protected]>; Matan Azrad <[email protected]>;

Quoted 1 line

Shahaf Shuler <[email protected]>; Viacheslav Ovsiienko

<[email protected]>; Jiawen Wu

Quoted 1 line

<[email protected]>; Jian Wang <[email protected]>;

Chautru, Nicolas

Quoted 1 line

<[email protected]>; Thomas Monjalon

<[email protected]>; Yigit, Ferruh

Quoted 1 line

<[email protected]>; Andrew Rybchenko

<[email protected]>

Quoted 1 line
Cc [email protected]; Pavan Nikhilesh <[email protected]>;

[email protected]

Quoted 1 line

Subject: [dpdk-dev] [PATCH v3 2/2] eal: fix side effects in ptr align

macros

Quoted 28 lines

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside RTE_*_ALIGN macros. Update common_autotest to detect macro side effects. Workaround static arrays relying on RTE_ALIGN macros.

Fixes af75078fece3 ("first public release")
---
 app/test/test_common.c           |  6 ++++++
 drivers/net/e1000/e1000_ethdev.h |  7 ++++---
 drivers/net/ixgbe/ixgbe_ethdev.h |  6 ++++--
 drivers/net/mlx5/mlx5_rxtx_vec.h |  7 ++++---
 drivers/net/txgbe/txgbe_ethdev.h |  6 ++++--
 examples/bbdev_app/main.c        |  2 +-
 lib/eal/include/rte_common.h     | 17 +++++++++++++----
 lib/ethdev/rte_eth_ctrl.h        |  5 +++--
 8 files changed, 39 insertions(+), 17 deletions(-)
app/test/test_common.c +0 -0
index 0dbb87e741..9efe3b10f9 100644

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 76

Quoted 8 lines
app/test/test_common.c +5 -0
@@ -69,6 +69,12 @@ test_macros(int __rte_unused unused_parm)
 	TEST_SIDE_EFFECT_2(RTE_PTR_ADD, void *, size_t);
 	TEST_SIDE_EFFECT_2(RTE_PTR_DIFF, void *, void *);
 	TEST_SIDE_EFFECT_2(RTE_PTR_SUB, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_CEIL, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_FLOOR, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_CEIL, unsigned int, unsigned

int);

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 77

Quoted 1 line
app/test/test_common.c +1 -0
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_FLOOR, unsigned int,

unsigned int);

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 78

Quoted 1 line
app/test/test_common.c +0 -0
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_CEIL, unsigned int,

unsigned int);

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 79

Quoted 1 line
app/test/test_common.c +0 -0
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_FLOOR, unsigned int,

unsigned int);

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 80

Quoted 1 line
app/test/test_common.c +0 -0
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_NEAR, unsigned int,

unsigned int);

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/net/e1000/e1000_ethdev.h

drivers/net/e1000/e1000_ethdev.h struct igb_eth_syn_filter_ele { · line 334

Quoted 7 lines

index 3b4d9c3ee6..155d825d89 100644

drivers/net/e1000/e1000_ethdev.h +0 -0
@@ -332,9 +332,10 @@ struct igb_eth_syn_filter_ele {
 };
 
 #define IGB_FLEX_FILTER_MAXLEN	128	/**< bytes to use in flex

filter. */

drivers/net/e1000/e1000_ethdev.h struct igb_eth_syn_filter_ele { · line 336

Quoted 2 lines
drivers/net/e1000/e1000_ethdev.h +0 -2
-#define IGB_FLEX_FILTER_MASK_SIZE	\
-	(RTE_ALIGN(IGB_FLEX_FILTER_MAXLEN, CHAR_BIT) /

CHAR_BIT)

drivers/net/e1000/e1000_ethdev.h struct igb_eth_syn_filter_ele {

Quoted 3 lines
drivers/net/e1000/e1000_ethdev.h +2 -1
-					/**< mask bytes in flex filter. */
+#define IGB_FLEX_FILTER_MASK_SIZE                                              \
+	(RTE_ALIGN_FLOOR(IGB_FLEX_FILTER_MAXLEN + (CHAR_BIT -

1), CHAR_BIT) / \

drivers/net/e1000/e1000_ethdev.h struct igb_eth_syn_filter_ele { · line 339

Quoted 3 lines
drivers/net/e1000/e1000_ethdev.h +2 -0
+	 CHAR_BIT)
+/**< mask bytes in flex filter. */
 

Since:
RTE_ALIGN --> RTE_ALIGN_CEIL(val, align) --> RTE_ALIGN_FLOOR(...)

Quoted 4 lines

Why only change 'RTE_ALIGN_CEIL', but then call 'RTE_ALIGN_FLOOR' directly ?
Sorry, can't get the point. : - (

RTE_ALIGN_CEIL is prone to macro side effects since it uses align twice in the macro. To fix it RTE_ALIGN_CEIL first expands the val and align values to temporary values and then calls ALIGN_FLOOR.
Due to this we can no longer use ALIGN/ALIGN_CEIL in static array declarations

Example:
In file included from ../lib/eal/x86/include/rte_atomic.h:13,

                 from ../examples/bbdev_app/main.c:21:
../lib/eal/include/rte_common.h:312:15: error: braced-group within expression allowed only inside a function
  __extension__({                                                        \
               ^
../examples/bbdev_app/main.c:48:18: note: in expansion of macro 'RTE_ALIGN_CEIL'
 #define NCB (3 * RTE_ALIGN_CEIL(K + 4, 32))
                  ^~~~~~~~~~~~~~

../examples/bbdev_app/main.c:145:23: note: in expansion of macro 'NCB'
uint8_t llr_temp_buf[NCB];

To workaround this I had to call RTE_ALIGN_FLOOR for known constants directly.

Quoted 65 lines

Why not change 'RTE_ALIGN' if it has issue ?

Review

Quoted 2 lines
(quoted diff) +0 -0

b/lib/eal/include/rte_common.h

lib/eal/include/rte_common.h static void

Quoted 4 lines

index a142596587..6acd067b5c 100644

lib/eal/include/rte_common.h +0 -0
@@ -294,8 +294,13 @@ static void

__attribute__((destructor(RTE_PRIO(prio)), used)) func(void)

lib/eal/include/rte_common.h static void · line 294

Quoted 1 line
lib/eal/include/rte_common.h +0 -0
  * point to an address no lower than the first parameter. Second

parameter

lib/eal/include/rte_common.h static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)

Quoted 4 lines
lib/eal/include/rte_common.h +0 -2
  * must be a power-of-two value.
  */
-#define RTE_PTR_ALIGN_CEIL(ptr, align) \
-	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align)

- 1), align)

lib/eal/include/rte_common.h static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)

Quoted 5 lines
lib/eal/include/rte_common.h +5 -0
+#define RTE_PTR_ALIGN_CEIL(ptr, align)                                         \
+	__extension__({                                                        \
+		typeof(ptr) _pc = (ptr);                                       \
+		typeof(align) _ac = (align);                                   \
+
	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(_pc, _ac -

1), \

lib/eal/include/rte_common.h static void

Quoted 6 lines
lib/eal/include/rte_common.h +2 -0
+				    _ac);                                      \
+	})
 
 /**
  * Macro to align a value to a given power-of-two. The resultant value
@@ -303,8 +308,12 @@ static void

__attribute__((destructor(RTE_PRIO(prio)), used)) func(void)

lib/eal/include/rte_common.h static void · line 308

Quoted 1 line
lib/eal/include/rte_common.h +0 -0
  * than the first parameter. Second parameter must be a power-of-

two

Quoted 18 lines
lib/eal/include/rte_common.h +6 -2
  * value.
  */
-#define RTE_ALIGN_CEIL(val, align) \
-	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
+#define RTE_ALIGN_CEIL(val, align)                                             \
+	__extension__({                                                        \
+		typeof(val) _vc = (val);                                       \
+		typeof(val) _ac = (typeof(val))(align);                        \
+		RTE_ALIGN_FLOOR((_vc + _ac - 1), _ac);                         \
+	})
 
 /**
  * Macro to align a pointer to a given power-of-two. The resultant
lib/ethdev/rte_eth_ctrl.h +0 -0
index 42652f9cce..863e56170b 100644

lib/ethdev/rte_eth_ctrl.h enum rte_fdir_mode { · line 435

Quoted 7 lines
lib/ethdev/rte_eth_ctrl.h +2 -2
@@ -431,8 +431,9 @@ enum rte_fdir_mode {
 };
 
 #define UINT64_BIT (CHAR_BIT * sizeof(uint64_t))
-#define RTE_FLOW_MASK_ARRAY_SIZE \
-	(RTE_ALIGN(RTE_ETH_FLOW_MAX, UINT64_BIT)/UINT64_BIT)
+#define RTE_FLOW_MASK_ARRAY_SIZE                                               \
+	(RTE_ALIGN_FLOOR(RTE_ETH_FLOW_MAX + (UINT64_BIT - 1),

UINT64_BIT) / \

Quoted 6 lines
lib/ethdev/rte_eth_ctrl.h +1 -1
+	 UINT64_BIT)
 
 /**
  * A structure used to get the information of flow director filter.
--

2.17.1

Acked-by
#11 lore raw

Re: [dpdk-dev] [PATCH v3 2/2] eal: fix side effects in ptr align macros

Quoted 117 lines

-----Original Message-----
From: Pavan Nikhilesh Bhagavatula <[email protected]>
Sent: Tuesday, May 11, 2021 16:45
To: Wang, Haiyue <[email protected]>; [email protected]; Matan Azrad <[email protected]>; Shahaf Shuler <[email protected]>; Viacheslav Ovsiienko <[email protected]>; Jiawen Wu <[email protected]>; Jian Wang <[email protected]>; Chautru, Nicolas <[email protected]>; Thomas Monjalon <[email protected]>; Yigit, Ferruh <[email protected]>; Andrew Rybchenko <[email protected]>
Cc: [email protected]; [email protected]
Subject: RE: [dpdk-dev] [PATCH v3 2/2] eal: fix side effects in ptr align macros

Quoted 86 lines
Quoted 4 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Tuesday, May 11, 2021 03:40
To: [email protected]; Wang, Haiyue

<[email protected]>; Matan Azrad <[email protected]>;

Quoted 1 line

Shahaf Shuler <[email protected]>; Viacheslav Ovsiienko

<[email protected]>; Jiawen Wu

Quoted 1 line

<[email protected]>; Jian Wang <[email protected]>;

Chautru, Nicolas

Quoted 1 line

<[email protected]>; Thomas Monjalon

<[email protected]>; Yigit, Ferruh

Quoted 1 line

<[email protected]>; Andrew Rybchenko

<[email protected]>

Quoted 1 line
Cc [email protected]; Pavan Nikhilesh <[email protected]>;

[email protected]

Quoted 1 line

Subject: [dpdk-dev] [PATCH v3 2/2] eal: fix side effects in ptr align

macros

Quoted 28 lines

From: Pavan Nikhilesh <[email protected]>

Avoid expanding parameters inside RTE_*_ALIGN macros. Update common_autotest to detect macro side effects. Workaround static arrays relying on RTE_ALIGN macros.

Fixes af75078fece3 ("first public release")
---
 app/test/test_common.c           |  6 ++++++
 drivers/net/e1000/e1000_ethdev.h |  7 ++++---
 drivers/net/ixgbe/ixgbe_ethdev.h |  6 ++++--
 drivers/net/mlx5/mlx5_rxtx_vec.h |  7 ++++---
 drivers/net/txgbe/txgbe_ethdev.h |  6 ++++--
 examples/bbdev_app/main.c        |  2 +-
 lib/eal/include/rte_common.h     | 17 +++++++++++++----
 lib/ethdev/rte_eth_ctrl.h        |  5 +++--
 8 files changed, 39 insertions(+), 17 deletions(-)
app/test/test_common.c +0 -0
index 0dbb87e741..9efe3b10f9 100644

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 76

Quoted 8 lines
app/test/test_common.c +5 -0
@@ -69,6 +69,12 @@ test_macros(int __rte_unused unused_parm)
 	TEST_SIDE_EFFECT_2(RTE_PTR_ADD, void *, size_t);
 	TEST_SIDE_EFFECT_2(RTE_PTR_DIFF, void *, void *);
 	TEST_SIDE_EFFECT_2(RTE_PTR_SUB, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_CEIL, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_PTR_ALIGN_FLOOR, void *, size_t);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN, unsigned int, unsigned int);
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_CEIL, unsigned int, unsigned

int);

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 77

Quoted 1 line
app/test/test_common.c +1 -0
+	TEST_SIDE_EFFECT_2(RTE_ALIGN_FLOOR, unsigned int,

unsigned int);

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 78

Quoted 1 line
app/test/test_common.c +0 -0
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_CEIL, unsigned int,

unsigned int);

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 79

Quoted 1 line
app/test/test_common.c +0 -0
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_FLOOR, unsigned int,

unsigned int);

app/test/test_common.c test_macros(int __rte_unused unused_parm) · line 80

Quoted 1 line
app/test/test_common.c +0 -0
 	TEST_SIDE_EFFECT_2(RTE_ALIGN_MUL_NEAR, unsigned int,

unsigned int);

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/net/e1000/e1000_ethdev.h

drivers/net/e1000/e1000_ethdev.h struct igb_eth_syn_filter_ele { · line 334

Quoted 7 lines

index 3b4d9c3ee6..155d825d89 100644

drivers/net/e1000/e1000_ethdev.h +0 -0
@@ -332,9 +332,10 @@ struct igb_eth_syn_filter_ele {
 };
 
 #define IGB_FLEX_FILTER_MAXLEN	128	/**< bytes to use in flex

filter. */

drivers/net/e1000/e1000_ethdev.h struct igb_eth_syn_filter_ele { · line 336

Quoted 2 lines
drivers/net/e1000/e1000_ethdev.h +0 -2
-#define IGB_FLEX_FILTER_MASK_SIZE	\
-	(RTE_ALIGN(IGB_FLEX_FILTER_MAXLEN, CHAR_BIT) /

CHAR_BIT)

drivers/net/e1000/e1000_ethdev.h struct igb_eth_syn_filter_ele {

Quoted 3 lines
drivers/net/e1000/e1000_ethdev.h +2 -1
-					/**< mask bytes in flex filter. */
+#define IGB_FLEX_FILTER_MASK_SIZE                                              \
+	(RTE_ALIGN_FLOOR(IGB_FLEX_FILTER_MAXLEN + (CHAR_BIT -

1), CHAR_BIT) / \

drivers/net/e1000/e1000_ethdev.h struct igb_eth_syn_filter_ele { · line 339

Quoted 3 lines
drivers/net/e1000/e1000_ethdev.h +2 -0
+	 CHAR_BIT)
+/**< mask bytes in flex filter. */
 

Since:
RTE_ALIGN --> RTE_ALIGN_CEIL(val, align) --> RTE_ALIGN_FLOOR(...)

Quoted 4 lines

Why only change 'RTE_ALIGN_CEIL', but then call 'RTE_ALIGN_FLOOR' directly ?
Sorry, can't get the point. : - (

RTE_ALIGN_CEIL is prone to macro side effects since it uses align twice in the macro. To fix it RTE_ALIGN_CEIL first expands the val and align values to temporary values and then calls ALIGN_FLOOR.
Due to this we can no longer use ALIGN/ALIGN_CEIL in static array declarations

Example:
In file included from ../lib/eal/x86/include/rte_atomic.h:13,

                 from ../examples/bbdev_app/main.c:21:
../lib/eal/include/rte_common.h:312:15: error: braced-group within expression allowed only inside a
function
  __extension__({                                                        \
               ^
../examples/bbdev_app/main.c:48:18: note: in expansion of macro 'RTE_ALIGN_CEIL'
 #define NCB (3 * RTE_ALIGN_CEIL(K + 4, 32))
                  ^~~~~~~~~~~~~~
Quoted 5 lines

../examples/bbdev_app/main.c:145:23: note: in expansion of macro 'NCB'
uint8_t llr_temp_buf[NCB];

To workaround this I had to call RTE_ALIGN_FLOOR for known constants directly.

Clear now, thanks.

For Intel PMDs,

BTW, I'm wondering that we can explicitly define the macro with type style like RTE_ALIGN_CEIL_TYPE, so that people can notice the side effects directly, and choose the right ALIGN macro.

And can still use ALIGN/ALIGN_CEIL in static array declarations, which is very simple.

Just my thinking. ;-)

Quoted 65 lines
Quoted 65 lines

Why not change 'RTE_ALIGN' if it has issue ?

Review

Quoted 2 lines
(quoted diff) +0 -0

b/lib/eal/include/rte_common.h

lib/eal/include/rte_common.h static void

Quoted 4 lines

index a142596587..6acd067b5c 100644

lib/eal/include/rte_common.h +0 -0
@@ -294,8 +294,13 @@ static void

__attribute__((destructor(RTE_PRIO(prio)), used)) func(void)

lib/eal/include/rte_common.h static void · line 294

Quoted 1 line
lib/eal/include/rte_common.h +0 -0
  * point to an address no lower than the first parameter. Second

parameter

lib/eal/include/rte_common.h static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)

Quoted 4 lines
lib/eal/include/rte_common.h +0 -2
  * must be a power-of-two value.
  */
-#define RTE_PTR_ALIGN_CEIL(ptr, align) \
-	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align)

- 1), align)

lib/eal/include/rte_common.h static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)

Quoted 5 lines
lib/eal/include/rte_common.h +5 -0
+#define RTE_PTR_ALIGN_CEIL(ptr, align)                                         \
+	__extension__({                                                        \
+		typeof(ptr) _pc = (ptr);                                       \
+		typeof(align) _ac = (align);                                   \
+
	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(_pc, _ac -

1), \

lib/eal/include/rte_common.h static void

Quoted 6 lines
lib/eal/include/rte_common.h +2 -0
+				    _ac);                                      \
+	})
 
 /**
  * Macro to align a value to a given power-of-two. The resultant value
@@ -303,8 +308,12 @@ static void

__attribute__((destructor(RTE_PRIO(prio)), used)) func(void)

lib/eal/include/rte_common.h static void · line 308

Quoted 1 line
lib/eal/include/rte_common.h +0 -0
  * than the first parameter. Second parameter must be a power-of-

two

Quoted 18 lines
lib/eal/include/rte_common.h +6 -2
  * value.
  */
-#define RTE_ALIGN_CEIL(val, align) \
-	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
+#define RTE_ALIGN_CEIL(val, align)                                             \
+	__extension__({                                                        \
+		typeof(val) _vc = (val);                                       \
+		typeof(val) _ac = (typeof(val))(align);                        \
+		RTE_ALIGN_FLOOR((_vc + _ac - 1), _ac);                         \
+	})
 
 /**
  * Macro to align a pointer to a given power-of-two. The resultant
lib/ethdev/rte_eth_ctrl.h +0 -0
index 42652f9cce..863e56170b 100644

lib/ethdev/rte_eth_ctrl.h enum rte_fdir_mode { · line 435

Quoted 7 lines
lib/ethdev/rte_eth_ctrl.h +2 -2
@@ -431,8 +431,9 @@ enum rte_fdir_mode {
 };
 
 #define UINT64_BIT (CHAR_BIT * sizeof(uint64_t))
-#define RTE_FLOW_MASK_ARRAY_SIZE \
-	(RTE_ALIGN(RTE_ETH_FLOW_MAX, UINT64_BIT)/UINT64_BIT)
+#define RTE_FLOW_MASK_ARRAY_SIZE                                               \
+	(RTE_ALIGN_FLOOR(RTE_ETH_FLOW_MAX + (UINT64_BIT - 1),

UINT64_BIT) / \

Quoted 6 lines
lib/ethdev/rte_eth_ctrl.h +1 -1
+	 UINT64_BIT)
 
 /**
  * A structure used to get the information of flow director filter.
--

2.17.1

#12 lore raw

PATCH 1/1 eal: avoid issues in macro expansion of alignment

In reply to [email protected]

RTE_ALIGN_MUL_NEAR is a macro so the cycle arguement could get evaluated twice causing some potential skew. Fix by computing value once.

Suggested by patch to fix side effects.

Fixes 5cbd14b3e5f9 ("eal: roundup TSC frequency when estimating")
---
 lib/eal/common/eal_common_timer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/eal/common/eal_common_timer.c +6 -2
index 5686a5102b66..05614b0503cf 100644
@@ -42,10 +42,14 @@ estimate_tsc_freq(void)
 	RTE_LOG(WARNING, EAL, "WARNING: TSC frequency estimated roughly"
 		" - clock timings may be less accurate.\n");
 	/* assume that the rte_delay_us_sleep() will sleep for 1 second */
-	uint64_t start = rte_rdtsc();
+	uint64_t start, elapsed;
+
+	start = rte_rdtsc();
 	rte_delay_us_sleep(US_PER_S);
+	elapsed = rte_rdtsc() - start;
+
 	/* Round up to 10Mhz. 1E7 ~ 10Mhz */
-	return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);
+	return RTE_ALIGN_MUL_NEAR(elapsed, CYC_PER_10MHZ);
 }
 
 void
Signature · 1 line
2.39.2
#13 lore raw

PATCH v2 1/1 eal: avoid issues in macro expansion of alignment

RTE_ALIGN_MUL_NEAR is a macro so the cycle argument could get evaluated twice causing some potential skew. Fix by computing value once.

Suggested by patch to fix side effects.

Fixes 5cbd14b3e5f9 ("eal: roundup TSC frequency when estimating")

---
v2 - fix spelling error in commit message

 lib/eal/common/eal_common_timer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/eal/common/eal_common_timer.c +6 -2
index 5686a5102b66..05614b0503cf 100644
@@ -42,10 +42,14 @@ estimate_tsc_freq(void)
 	RTE_LOG(WARNING, EAL, "WARNING: TSC frequency estimated roughly"
 		" - clock timings may be less accurate.\n");
 	/* assume that the rte_delay_us_sleep() will sleep for 1 second */
-	uint64_t start = rte_rdtsc();
+	uint64_t start, elapsed;
+
+	start = rte_rdtsc();
 	rte_delay_us_sleep(US_PER_S);
+	elapsed = rte_rdtsc() - start;
+
 	/* Round up to 10Mhz. 1E7 ~ 10Mhz */
-	return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);
+	return RTE_ALIGN_MUL_NEAR(elapsed, CYC_PER_10MHZ);
 }
 
 void
Signature · 1 line
2.39.2
#14 lore raw

RE: [PATCH v2] eal: avoid issues in macro expansion of alignment

Quoted 41 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 4 July 2023 01.24

RTE_ALIGN_MUL_NEAR is a macro so the cycle argument could get evaluated twice causing some potential skew. Fix by computing value once.

Suggested by patch to fix side effects.

Fixes 5cbd14b3e5f9 ("eal: roundup TSC frequency when estimating")

---
v2 - fix spelling error in commit message

 lib/eal/common/eal_common_timer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/eal/common/eal_common_timer.c +6 -3
index 5686a5102b66..05614b0503cf 100644
@@ -42,10 +42,14 @@ estimate_tsc_freq(void)
 	RTE_LOG(WARNING, EAL, "WARNING: TSC frequency estimated roughly"
 		" - clock timings may be less accurate.\n");
 	/* assume that the rte_delay_us_sleep() will sleep for 1 second */
-	uint64_t start = rte_rdtsc();
+	uint64_t start, elapsed;
+
+	start = rte_rdtsc();
 	rte_delay_us_sleep(US_PER_S);
+	elapsed = rte_rdtsc() - start;
+
 	/* Round up to 10Mhz. 1E7 ~ 10Mhz */
-	return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);
+	return RTE_ALIGN_MUL_NEAR(elapsed, CYC_PER_10MHZ);
 }
 
 void
--
Quoted 1 line

2.39.2

Please fix the RTE_ALIGN_MUL_NEAR() macro instead. It already uses temporary variables with typeof() anyway.

Other macros might have similar behavior of using their parameters more than once, and could be improved too.

#15 lore raw

Re: [PATCH v2] eal: avoid issues in macro expansion of alignment

Quoted 39 lines · On Tue, 4 Jul 2023 10:43:40 +0200 Morten Brørup <[email protected]>…
Quoted 39 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 4 July 2023 01.24

RTE_ALIGN_MUL_NEAR is a macro so the cycle argument could get evaluated twice causing some potential skew. Fix by computing value once.

Suggested by patch to fix side effects.

Fixes 5cbd14b3e5f9 ("eal: roundup TSC frequency when estimating")

---
v2 - fix spelling error in commit message

 lib/eal/common/eal_common_timer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/eal/common/eal_common_timer.c +6 -2
index 5686a5102b66..05614b0503cf 100644
@@ -42,10 +42,14 @@ estimate_tsc_freq(void)
 	RTE_LOG(WARNING, EAL, "WARNING: TSC frequency estimated roughly"
 		" - clock timings may be less accurate.\n");
 	/* assume that the rte_delay_us_sleep() will sleep for 1 second */
-	uint64_t start = rte_rdtsc();
+	uint64_t start, elapsed;
+
+	start = rte_rdtsc();
 	rte_delay_us_sleep(US_PER_S);
+	elapsed = rte_rdtsc() - start;
+
 	/* Round up to 10Mhz. 1E7 ~ 10Mhz */
-	return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);
+	return RTE_ALIGN_MUL_NEAR(elapsed, CYC_PER_10MHZ);
 }
 
Quoted 8 lines
Quoted 3 lines
lib/eal/common/eal_common_timer.c +0 -1
 void
--

2.39.2

Please fix the RTE_ALIGN_MUL_NEAR() macro instead. It already uses temporary variables with typeof() anyway.

Other macros might have similar behavior of using their parameters more than once, and could be improved too.

It is already fixed, so this patch can be dropped.

#16 lore raw

RE: [PATCH v2] eal: avoid issues in macro expansion of alignment

Quoted 50 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 4 July 2023 18.01

Quoted 39 lines · On Tue, 4 Jul 2023 10:43:40 +0200 Morten Brørup <[email protected]>…
Quoted 39 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 4 July 2023 01.24

RTE_ALIGN_MUL_NEAR is a macro so the cycle argument could get evaluated twice causing some potential skew. Fix by computing value once.

Suggested by patch to fix side effects.

Fixes 5cbd14b3e5f9 ("eal: roundup TSC frequency when estimating")

---
v2 - fix spelling error in commit message

 lib/eal/common/eal_common_timer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/eal/common/eal_common_timer.c +6 -2
index 5686a5102b66..05614b0503cf 100644
@@ -42,10 +42,14 @@ estimate_tsc_freq(void)
 	RTE_LOG(WARNING, EAL, "WARNING: TSC frequency estimated roughly"
 		" - clock timings may be less accurate.\n");
 	/* assume that the rte_delay_us_sleep() will sleep for 1 second */
-	uint64_t start = rte_rdtsc();
+	uint64_t start, elapsed;
+
+	start = rte_rdtsc();
 	rte_delay_us_sleep(US_PER_S);
+	elapsed = rte_rdtsc() - start;
+
 	/* Round up to 10Mhz. 1E7 ~ 10Mhz */
-	return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);
+	return RTE_ALIGN_MUL_NEAR(elapsed, CYC_PER_10MHZ);
 }
 
Quoted 5 lines
Quoted 3 lines
lib/eal/common/eal_common_timer.c +0 -1
 void
--

2.39.2

Please fix the RTE_ALIGN_MUL_NEAR() macro instead. It already uses

Quoted 7 lines

temporary variables with typeof() anyway.

Quoted 2 lines

Other macros might have similar behavior of using their parameters

more than once, and could be improved too.

Quoted 1 line

It is already fixed, so this patch can be dropped.

The macro is not fixed in 23.07-rc2 [1], but evaluates the "v" parameter four times; first two times to calculate respectively "ceil" and "floor", and then two times in the trigraph to determine which way to round. The "mul" parameter is evaluated twice by the macro.

#define RTE_ALIGN_MUL_NEAR(v, mul)				\
	({							\
		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
		(ceil - (v)) > ((v) - floor) ? floor : ceil;	\
	})

[1]: https://elixir.bootlin.com/dpdk/v23.07-rc2/source/lib/eal/include/rte_common.h#L388

#17 lore raw

Re: [PATCH v2] eal: avoid issues in macro expansion of alignment

Quoted 60 lines · On Wed, 5 Jul 2023 00:16:50 +0200 Morten Brørup <[email protected]>…
Quoted 50 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 4 July 2023 18.01

Quoted 39 lines · On Tue, 4 Jul 2023 10:43:40 +0200 Morten Brørup <[email protected]>…
Quoted 39 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 4 July 2023 01.24

RTE_ALIGN_MUL_NEAR is a macro so the cycle argument could get evaluated twice causing some potential skew. Fix by computing value once.

Suggested by patch to fix side effects.

Fixes 5cbd14b3e5f9 ("eal: roundup TSC frequency when estimating")

---
v2 - fix spelling error in commit message

 lib/eal/common/eal_common_timer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/eal/common/eal_common_timer.c +6 -2
index 5686a5102b66..05614b0503cf 100644
@@ -42,10 +42,14 @@ estimate_tsc_freq(void)
 	RTE_LOG(WARNING, EAL, "WARNING: TSC frequency estimated roughly"
 		" - clock timings may be less accurate.\n");
 	/* assume that the rte_delay_us_sleep() will sleep for 1 second */
-	uint64_t start = rte_rdtsc();
+	uint64_t start, elapsed;
+
+	start = rte_rdtsc();
 	rte_delay_us_sleep(US_PER_S);
+	elapsed = rte_rdtsc() - start;
+
 	/* Round up to 10Mhz. 1E7 ~ 10Mhz */
-	return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);
+	return RTE_ALIGN_MUL_NEAR(elapsed, CYC_PER_10MHZ);
 }
 
Quoted 5 lines
Quoted 3 lines
lib/eal/common/eal_common_timer.c +0 -1
 void
--

2.39.2

Please fix the RTE_ALIGN_MUL_NEAR() macro instead. It already uses

Quoted 7 lines

temporary variables with typeof() anyway.

Quoted 2 lines

Other macros might have similar behavior of using their parameters

more than once, and could be improved too.

Quoted 1 line

It is already fixed, so this patch can be dropped.

The macro is not fixed in 23.07-rc2 [1], but evaluates the "v" parameter four times; first two times to calculate respectively "ceil" and "floor", and then two times in the trigraph to determine which way to round. The "mul" parameter is evaluated twice by the macro.

Quoted 9 lines
#define RTE_ALIGN_MUL_NEAR(v, mul)				\
	({							\
		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
		(ceil - (v)) > ((v) - floor) ? floor : ceil;	\
	})

[1]: https://elixir.bootlin.com/dpdk/v23.07-rc2/source/lib/eal/include/rte_common.h#L388

Still working on sorting this out. Discovered this new issue.

If I fix RTE_ALIGN_CEIL to only evaluate arguments once.

-#define RTE_ALIGN_CEIL(val, align) \
- RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
+#define RTE_ALIGN_CEIL(val, align) \
+ __extension__ ({ \
+ uintptr_t _align = align; \
+ RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (_align) - 1)), _align); \
+ })

Then compiler won't allow it to be used in initialization of globals like this.

In file included from ../lib/telemetry/rte_telemetry.h:14,
from ../lib/ethdev/rte_ethdev_telemetry.c:9:
../lib/eal/include/rte_common.h:350:23: error: braced-group within expression allowed only inside a function
350 | __extension__ ({ \
| ^
../lib/eal/include/rte_common.h:371:31: note: in expansion of macro ‘RTE_ALIGN_CEIL’
371 | #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)

      |                               ^~~~~~~~~~~~~~
../lib/ethdev/rte_eth_ctrl.h:435:10: note: in expansion of macro ‘RTE_ALIGN’
  435 |         (RTE_ALIGN(RTE_ETH_FLOW_MAX, UINT64_BIT)/UINT64_BIT)
      |          ^~~~~~~~~

../lib/ethdev/rte_eth_ctrl.h:452:34: note: in expansion of macro ‘RTE_FLOW_MASK_ARRAY_SIZE’
452 | uint64_t flow_types_mask[RTE_FLOW_MASK_ARRAY_SIZE];

#18 lore raw
In reply to [email protected]
Quoted 6 lines · On Tue, 11 May 2021 01:10:06 +0530 <[email protected]> wrote:

Avoid expanding parameters inside the macro multiple times. For example:

	RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);

Here rte_rdtsc() call is expanded multiple times in the macro causing it to return different values that leads to unintended side effects.

This was already fixed.