Skip to contentSkip to conversation

eal: use _Static_assert() for RTE_BUILD_BUG_ON

RFC In review Stephen Hemminger 11 participants 83 messages Nov 11, 2023 – Feb 16, 2024 Network drivers

lore
Share & tools
lore
#0 lore raw

RFC 1/1 eal: use _Static_assert() for RTE_BUILD_BUG_ON

The method of doing sizeof a bad array element was an interesting hack but it has a couple of problems. First, it won't work if VLA checking is enabled. It doesn't enforce that the expression is constant.

Replace that with the _Static_assert builtin available in Gcc, Clang, and MSVC.

---
 lib/eal/include/rte_common.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_common.h +1 -1
index c1ba32d00e47..2c63206223b8 100644
@@ -495,7 +495,7 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 /**
  * Triggers an error at compilation time if the condition is true.
  */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(e) _Static_assert(!(e),  #e)
 
 /*********** Cache line related macros ********/
 
Signature · 1 line
2.39.2
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Saturday, 11 November 2023 18.22

The method of doing sizeof a bad array element was an interesting hack but it has a couple of problems. First, it won't work if VLA checking is enabled. It doesn't enforce that the expression is constant.

Quoted 5 lines

Replace that with the _Static_assert builtin available in Gcc, Clang, and MSVC.

---

Two souls, one thought...
I have been considering exactly the same, and thus strongly support this.

Quoted 3 lines

-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(e) _Static_assert(!(e), #e)

Please use static_assert instead of _Static_assert, as discussed with Bruce:

http://inbox.dpdk.org/dev/ZR%[email protected]/

#2 lore raw
Quoted 17 lines · Morten Brørup
Quoted 8 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Saturday, 11 November 2023 18.22

The method of doing sizeof a bad array element was an interesting hack but it has a couple of problems. First, it won't work if VLA checking is enabled. It doesn't enforce that the expression is constant.

Quoted 5 lines

Replace that with the _Static_assert builtin available in Gcc, Clang, and MSVC.

---

Two souls, one thought...
I have been considering exactly the same, and thus strongly support this.

Quoted 7 lines
Quoted 3 lines

-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(e) _Static_assert(!(e), #e)

Please use static_assert instead of _Static_assert, as discussed with Bruce:

http://inbox.dpdk.org/dev/ZR%[email protected]/

+1
At least in public headers use the macro static_assert for portability with C++.

Quoted 2 lines

one more time for good measure, this is an important change.

Acked-by on v1 1/1
#3 lore raw
In reply to Stephen Hemminger
Quoted 10 lines · Stephen Hemminger

The method of doing sizeof a bad array element was an interesting hack but it has a couple of problems. First, it won't work if VLA checking is enabled. It doesn't enforce that the expression is constant.

Replace that with the _Static_assert builtin available in Gcc, Clang, and MSVC.

---

#4 lore raw

v2 cover use static_assertion for build errors

In reply to Stephen Hemminger

This series fixes a couple places where expressions that could not be evaluated as constant early in compiler passes were used. And then converts RTE_BUILD_BUG_ON() with static_assert.

Stephen Hemminger (3):
event/opdl: fix non-constant compile time assertion net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()
eal: replace out of bounds VLA with static_assert

 drivers/event/opdl/opdl_ring.c | 5 ++++-
 drivers/net/sfc/sfc_ef100_tx.c | 7 +++++--
 lib/eal/include/rte_common.h   | 3 ++-
 3 files changed, 11 insertions(+), 4 deletions(-)
Signature · 1 line
2.39.2
#5 lore raw

PATCH v2 1/3 event/opdl: fix non-constant compile time assertion

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure library")
---
 drivers/event/opdl/opdl_ring.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/event/opdl/opdl_ring.c +4 -1
index 69392b56bbec..24e0bbe3222d 100644
@@ -31,6 +31,9 @@
 #define OPDL_OPA_MASK    (0xFF)
 #define OPDL_OPA_OFFSET  (0x38)
 
+/* Equivalent to rte_is_power_of_2() but as macro. */
+#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
+
 /* Types of dependency between stages */
 enum dep_type {
 	DEP_NONE = 0,  /* no dependency */
@@ -910,7 +913,7 @@ opdl_ring_create(const char *name, uint32_t num_slots, uint32_t slot_size,
 			RTE_CACHE_LINE_MASK) != 0);
 	RTE_BUILD_BUG_ON((offsetof(struct opdl_ring, slots) &
 			RTE_CACHE_LINE_MASK) != 0);
-	RTE_BUILD_BUG_ON(!rte_is_power_of_2(OPDL_DISCLAIMS_PER_LCORE));
+	RTE_BUILD_BUG_ON(!IS_POWER_OF_2(OPDL_DISCLAIMS_PER_LCORE));
 
 	/* Parameter checking */
 	if (name == NULL) {
Signature · 1 line
2.39.2
Acked-by on v2 1/3
#6 lore raw

Re: [PATCH v2 1/3] event/opdl: fix non-constant compile time assertion

Quoted 11 lines · Stephen Hemminger

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure library")
---
 drivers/event/opdl/opdl_ring.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Acked-by on v2 1/3
#7 lore raw

Re: [PATCH v2 1/3] event/opdl: fix non-constant compile time assertion

In reply to Stephen Hemminger
Quoted 9 lines · Stephen Hemminger

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure library")

---

#8 lore raw

PATCH v2 2/3 net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()

In reply to Stephen Hemminger

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluted in first pass of compiler. Replace RTE_MIN() with equivalent macro.

This will cause errors from checkpatch about multiple evaluations of same expression in macro but it is ok in this case.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")
---
 drivers/net/sfc/sfc_ef100_tx.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/sfc/sfc_ef100_tx.c +5 -2
index 1b6374775f07..f4bcadc1e8e0 100644
@@ -26,6 +26,10 @@
 #include "sfc_ef100.h"
 #include "sfc_nic_dma_dp.h"
 
+#ifndef MIN
+/* not typesafe but is a constant */
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#endif
 
 #define sfc_ef100_tx_err(_txq, ...) \
 	SFC_DP_LOG(SFC_KVARG_DATAPATH_EF100, ERR, &(_txq)->dp.dpq, __VA_ARGS__)
@@ -563,8 +567,7 @@ sfc_ef100_tx_pkt_descs_max(const struct rte_mbuf *m)
 		 * (split into many Tx descriptors).
 		 */
 		RTE_BUILD_BUG_ON(SFC_EF100_TX_SEND_DESC_LEN_MAX <
-				 RTE_MIN((unsigned int)EFX_MAC_PDU_MAX,
-				 SFC_MBUF_SEG_LEN_MAX));
+				 MIN((unsigned int)EFX_MAC_PDU_MAX, SFC_MBUF_SEG_LEN_MAX));
 	}
 
 	if (m->ol_flags & sfc_dp_mport_override) {
Signature · 1 line
2.39.2
Acked-by on v2 2/3
#9 lore raw

Re: [PATCH v2 2/3] net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()

Quoted 11 lines · Stephen Hemminger

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluted in first pass of compiler. Replace RTE_MIN() with equivalent macro.

This will cause errors from checkpatch about multiple evaluations of same expression in macro but it is ok in this case.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")

---

#10 lore raw

Re: [PATCH v2 2/3] net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()

In reply to Stephen Hemminger
Quoted 10 lines · On Mon, 13 Nov 2023 09:06:04 -0800 Stephen Hemminger…

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluted in first pass of compiler. Replace RTE_MIN() with equivalent macro.

This will cause errors from checkpatch about multiple evaluations of same expression in macro but it is ok in this case.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")

Building with clang finds another issue. ../drivers/net/sfc/sfc_rx.c:158:3: error: expected expression

                RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN != 0);

yet
lib/mbuf/rte_mbuf_core.h:#define RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN 0

#11 lore raw

Re: [PATCH v2 2/3] net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()

Quoted 14 lines · Stephen Hemminger
Quoted 10 lines · On Mon, 13 Nov 2023 09:06:04 -0800 Stephen Hemminger…

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluted in first pass of compiler. Replace RTE_MIN() with equivalent macro.

This will cause errors from checkpatch about multiple evaluations of same expression in macro but it is ok in this case.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")
Quoted 5 lines

Building with clang finds another issue. ../drivers/net/sfc/sfc_rx.c:158:3: error: expected expression

                RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN != 0);

yet
lib/mbuf/rte_mbuf_core.h:#define RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN 0

curious. do you have the gcc -E / clang -E preprocessed output for the expansion? wonder what it looks like.

#12 lore raw

Re: [PATCH v2 2/3] net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()

Quoted 15 lines · On Mon, 13 Nov 2023 14:28:55 -0800 Tyler Retzlaff…
Quoted 14 lines · Stephen Hemminger
Quoted 10 lines · On Mon, 13 Nov 2023 09:06:04 -0800 Stephen Hemminger…

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluted in first pass of compiler. Replace RTE_MIN() with equivalent macro.

This will cause errors from checkpatch about multiple evaluations of same expression in macro but it is ok in this case.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")
Quoted 8 lines
Quoted 5 lines

Building with clang finds another issue. ../drivers/net/sfc/sfc_rx.c:158:3: error: expected expression

                RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN != 0);

yet
lib/mbuf/rte_mbuf_core.h:#define RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN 0

curious. do you have the gcc -E / clang -E preprocessed output for the expansion? wonder what it looks like.

yet another one from clang. ../drivers/net/sfc/sfc_ef10_rx_ev.h:142:4: error: expected expression

                 RTE_BUILD_BUG_ON(ESE_FZ_L4_CLASS_TCP != ESE_DE_L4_CLASS_TCP);

drivers/common/sfc_efx/base/efx_regs_ef10.h:#define ESE_FZ_L4_CLASS_TCP 1
drivers/common/sfc_efx/base/efx_regs_ef10.h:#define ESE_DE_L4_CLASS_TCP 1

#13 lore raw

Re: [PATCH v2 2/3] net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()

In reply to Tyler Retzlaff
Quoted 15 lines · On Mon, 13 Nov 2023 14:28:55 -0800 Tyler Retzlaff…
Quoted 14 lines · Stephen Hemminger
Quoted 10 lines · On Mon, 13 Nov 2023 09:06:04 -0800 Stephen Hemminger…

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluted in first pass of compiler. Replace RTE_MIN() with equivalent macro.

This will cause errors from checkpatch about multiple evaluations of same expression in macro but it is ok in this case.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")
Quoted 8 lines
Quoted 5 lines

Building with clang finds another issue. ../drivers/net/sfc/sfc_rx.c:158:3: error: expected expression

                RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN != 0);

yet
lib/mbuf/rte_mbuf_core.h:#define RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN 0

curious. do you have the gcc -E / clang -E preprocessed output for the expansion? wonder what it looks like.

Building with clang and -E.

This code:

	switch (desc_flags & (EFX_PKT_IPV4 | EFX_CKSUM_IPV4)) {
	case (EFX_PKT_IPV4 | EFX_CKSUM_IPV4):
		mbuf_flags |= RTE_MBUF_F_RX_IP_CKSUM_GOOD;
		break;
	case EFX_PKT_IPV4:
		mbuf_flags |= RTE_MBUF_F_RX_IP_CKSUM_BAD;
		break;
	default:
		RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN != 0);
		SFC_ASSERT((mbuf_flags & RTE_MBUF_F_RX_IP_CKSUM_MASK) ==
			   RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN);
		break;
	}

Becomes:
switch (desc_flags & (0x0800 | 0x0040)) {
case (0x0800 | 0x0040):
mbuf_flags |= (1ULL << 7);
break;
case 0x0800:
mbuf_flags |= (1ULL << 4);
break;
default:
_Static_assert(!(0 != 0), "RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN != 0"); do {} while (0);

break;
}

Doing same with Gcc:
switch (desc_flags & (0x0800 | 0x0040)) {
case (0x0800 | 0x0040):
mbuf_flags |= (1ULL << 7);
break;
case 0x0800:
mbuf_flags |= (1ULL << 4);
break;
default:

# 158 "./drivers/net/sfc/sfc_rx.c" 3 4
_Static_assert
# 158 "./drivers/net/sfc/sfc_rx.c"
(!(0 != 0), "RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN != 0");
do {} while (0)

                                     ;
#14 lore raw

Re: [PATCH v2 2/3] net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()

Quoted 1 line · On Mon, 13 Nov 2023 16:16:35 -0800 Stephen Hemminger…
  _Static_assert(!(0 != 0), "RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN != 0");

Looks like a clang bug, or something about the other compiler flags because compiling just this part is fine.

#15 lore raw

RE: [PATCH v2 2/3] net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()

Quoted 10 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Tuesday, 14 November 2023 01.22

Quoted 1 line · On Mon, 13 Nov 2023 16:16:35 -0800 Stephen Hemminger…
  _Static_assert(!(0 != 0), "RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN != 0");

Looks like a clang bug, or something about the other compiler flags because compiling just this part is fine.

Ideas:

Perhaps this file is not compiled as C11, so _Static_assert is not recognized. Does that part compile in the same file? Or perhaps the C version or some compiler flags are modified (by a #pragma or similar) in the file.

#16 lore raw

PATCH v2 3/3 eal: replace out of bounds VLA with static_assert

In reply to Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

---
 lib/eal/include/rte_common.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/include/rte_common.h +2 -1
index c1ba32d00e47..bea7c0e57d5e 100644
@@ -16,6 +16,7 @@
 extern "C" {
 #endif
 
+#include <assert.h>
 #include <stdint.h>
 #include <limits.h>
 
@@ -495,7 +496,7 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 /**
  * Triggers an error at compilation time if the condition is true.
  */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(condition) static_assert(!(condition), #condition)
 
 /*********** Cache line related macros ********/
 
Signature · 1 line
2.39.2
Acked-by on v2 3/3
#17 lore raw

Re: [PATCH v2 3/3] eal: replace out of bounds VLA with static_assert

Quoted 27 lines · Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

---
 lib/eal/include/rte_common.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/include/rte_common.h +1 -0
index c1ba32d00e47..bea7c0e57d5e 100644
@@ -16,6 +16,7 @@
 extern "C" {
 #endif
 
+#include <assert.h>
 #include <stdint.h>
 #include <limits.h>
 

lib/eal/include/rte_common.h rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align) · line 500

Quoted 6 lines
lib/eal/include/rte_common.h +1 -1
@@ -495,7 +496,7 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 /**
  * Triggers an error at compilation time if the condition is true.
  */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(condition) static_assert(!(condition), #condition)
 

And as for the static_assert vs BUILD_BUG_ON debate, I think I'd go with using static_assert explicitly and only keeping BUILD_BUG_ON for backward compatbility. Reason being that you can give a reasonable error message with the assert.

#18 lore raw

Re: [PATCH v2 3/3] eal: replace out of bounds VLA with static_assert

Quoted 36 lines · Bruce Richardson
Quoted 27 lines · Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

---
 lib/eal/include/rte_common.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/include/rte_common.h +1 -0
index c1ba32d00e47..bea7c0e57d5e 100644
@@ -16,6 +16,7 @@
 extern "C" {
 #endif
 
+#include <assert.h>
 #include <stdint.h>
 #include <limits.h>
 

lib/eal/include/rte_common.h rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align) · line 500

Quoted 6 lines
lib/eal/include/rte_common.h +1 -1
@@ -495,7 +496,7 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 /**
  * Triggers an error at compilation time if the condition is true.
  */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(condition) static_assert(!(condition), #condition)
 
Quoted 4 lines

And as for the static_assert vs BUILD_BUG_ON debate, I think I'd go with using static_assert explicitly and only keeping BUILD_BUG_ON for backward compatbility. Reason being that you can give a reasonable error message with the assert.

my vote is for the same, i would prefer to just use standard constructs directly. i do wish the C11 version didn't require a message but regardless let's use the standard version.

additionally, can we have an addition to checkpatches.pl (if the above is agreed) that prevents further additions of RTE_BUILD_BUG_ON usage?

#19 lore raw

Re: [PATCH v2 3/3] eal: replace out of bounds VLA with static_assert

In reply to Stephen Hemminger

ping

i'd like to see this change go in asap since it is pre-requisite to turning on -Wvla which explicitly caught use of non-constant expressions in the RTE_BUILD_BUG_ON() hiding bugs.

thanks!

Quoted 37 lines · Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

---
 lib/eal/include/rte_common.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/include/rte_common.h +2 -1
index c1ba32d00e47..bea7c0e57d5e 100644
@@ -16,6 +16,7 @@
 extern "C" {
 #endif
 
+#include <assert.h>
 #include <stdint.h>
 #include <limits.h>
 
@@ -495,7 +496,7 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 /**
  * Triggers an error at compilation time if the condition is true.
  */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(condition) static_assert(!(condition), #condition)
 
 /*********** Cache line related macros ********/
 
Signature · 1 line
2.39.2
Reviewed-by
#20 lore raw

RE: [PATCH v2 3/3] eal: replace out of bounds VLA with static_assert

Quoted 42 lines

From: Tyler Retzlaff [mailto:[email protected]]
Sent: Friday, 16 February 2024 01.34

ping

i'd like to see this change go in asap since it is pre-requisite to turning on -Wvla which explicitly caught use of non-constant expressions
in the RTE_BUILD_BUG_ON() hiding bugs.

thanks!

Quoted 7 lines · Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

Review

Quoted 8 lines
---
 lib/eal/include/rte_common.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/lib/eal/include/rte_common.h

lib/eal/include/rte_common.h rte_is_aligned(const void * const __rte_restrict

Quoted 12 lines

index c1ba32d00e47..bea7c0e57d5e 100644

lib/eal/include/rte_common.h +1 -0
@@ -16,6 +16,7 @@
 extern "C" {
 #endif
 
+#include <assert.h>
 #include <stdint.h>
 #include <limits.h>
 
@@ -495,7 +496,7 @@ rte_is_aligned(const void * const __rte_restrict

ptr, const unsigned int align)

Quoted 12 lines

lib/eal/include/rte_common.h rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)

Quoted 4 lines
lib/eal/include/rte_common.h +0 -1
 /**
  * Triggers an error at compilation time if the condition is true.
  */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 -

2*!!(condition)]))

lib/eal/include/rte_common.h rte_is_aligned(const void * const __rte_restrict · line 499

Quoted 1 line
lib/eal/include/rte_common.h +1 -0
+#define RTE_BUILD_BUG_ON(condition) static_assert(!(condition),

#condition)

Quoted 5 lines
lib/eal/include/rte_common.h +0 -1
 /*********** Cache line related macros ********/
 
--

2.39.2

#21 lore raw

Re: [PATCH v2 3/3] eal: replace out of bounds VLA with static_assert

In reply to Tyler Retzlaff
Quoted 6 lines · Tyler Retzlaff

ping

i'd like to see this change go in asap since it is pre-requisite to turning on -Wvla which explicitly caught use of non-constant expressions in the RTE_BUILD_BUG_ON() hiding bugs.

That was the last thing I applied yesterday.
It ran through compile checks during the night.

I am reviewing a few details this morning before pushing.

Signature · 1 line
David Marchand
#22 lore raw

Re: [PATCH v2 3/3] eal: replace out of bounds VLA with static_assert

Quoted 5 lines · David Marchand
Quoted 3 lines · Tyler Retzlaff

ping

Quoted 8 lines
Quoted 3 lines

i'd like to see this change go in asap since it is pre-requisite to turning on -Wvla which explicitly caught use of non-constant expressions in the RTE_BUILD_BUG_ON() hiding bugs.

That was the last thing I applied yesterday.
It ran through compile checks during the night.

I am reviewing a few details this morning before pushing.

thank you!

Quoted 4 lines
Signature · 2 lines
-- 
David Marchand
Acked-by on the v2 series
#23 lore raw

Re: [PATCH v2 0/3] use static_assertion for build errors

In reply to Stephen Hemminger
Quoted 9 lines · Stephen Hemminger

This series fixes a couple places where expressions that could not be evaluated as constant early in compiler passes were used. And then converts RTE_BUILD_BUG_ON() with static_assert.

Stephen Hemminger (3):
event/opdl: fix non-constant compile time assertion net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()
eal: replace out of bounds VLA with static_assert

I am getting more build errors [1], [2].

[1] meson --buildtype=debug build

In file included from ../lib/eal/include/dev_driver.h:12,

                 from ../lib/ethdev/ethdev_driver.h:23,
                 from ../drivers/net/i40e/i40e_rxtx_vec_sse.c:6:

../drivers/net/i40e/i40e_rxtx_vec_sse.c: In function ‘descs_to_fdir_32b’:
../lib/eal/include/rte_common.h:499:51: error: expression in static assertion is not constant
499 | #define RTE_BUILD_BUG_ON(condition) static_assert(!(condition),
#condition)
| ^~~~~~~~~~~~
../drivers/net/i40e/i40e_rxtx_vec_sse.c:147:9: note: in expansion of macro ‘RTE_BUILD_BUG_ON’
147 | RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 <<

FDIR_ID_BIT_SHIFT));
      |         ^~~~~~~~~~~~~~~~

[2] CC=clang meson --buildtype=debugoptimized build

../lib/mempool/rte_mempool.c:749:2: error: static_assert expression is not an integral constant expression

RTE_BUILD_BUG_ON(CALC_CACHE_FLUSHTHRESH(RTE_MEMPOOL_CACHE_MAX_SIZE) >

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../lib/eal/include/rte_common.h:499:51: note: expanded from macro 'RTE_BUILD_BUG_ON'

#define RTE_BUILD_BUG_ON(condition) static_assert(!(condition), #condition)
                                                  ^~~~~~~~~~~~

1 error generated.

#24 lore raw

RE: [PATCH v2 0/3] use static_assertion for build errors

Quoted 14 lines

From: Ferruh Yigit [mailto:[email protected]]
Sent: Monday, 13 November 2023 19.14

Quoted 4 lines · Stephen Hemminger

This series fixes a couple places where expressions that could not be evaluated as constant early in compiler passes were used. And then converts RTE_BUILD_BUG_ON() with static_assert.

I am getting more build errors [1], [2].

[...]

Quoted 7 lines

[2] CC=clang meson --buildtype=debugoptimized build

../lib/mempool/rte_mempool.c:749:2: error: static_assert expression is not an integral constant expression

RTE_BUILD_BUG_ON(CALC_CACHE_FLUSHTHRESH(RTE_MEMPOOL_CACHE_MAX_SIZE) >

Quoted 7 lines

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../lib/eal/include/rte_common.h:499:51: note: expanded from macro 'RTE_BUILD_BUG_ON'
#define RTE_BUILD_BUG_ON(condition) static_assert(!(condition), #condition)

                                                  ^~~~~~~~~~~~

1 error generated.

Perhaps you can fix it in rte_mempool.c... the CACHE_FLUSHTHRESH_MULTIPLIER constant seems to be only used for the CALC_CACHE_FLUSHTHRESH() macro, so fix it like this:

-#define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
-#define CALC_CACHE_FLUSHTHRESH(c) \
- ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
+/* Cache flush threshold multiplier is 1.5 = 3/2. */ +#define CALC_CACHE_FLUSHTHRESH(c) (c * 3 / 2)

#25 lore raw

Re: [PATCH v2 0/3] use static_assertion for build errors

In reply to Ferruh Yigit
Quoted 23 lines · Ferruh Yigit
Quoted 9 lines · Stephen Hemminger

This series fixes a couple places where expressions that could not be evaluated as constant early in compiler passes were used. And then converts RTE_BUILD_BUG_ON() with static_assert.

Stephen Hemminger (3):
event/opdl: fix non-constant compile time assertion net/sfc: fix non-constant expression inr RTE_BUILD_BUG_ON()
eal: replace out of bounds VLA with static_assert

I am getting more build errors [1], [2].

[1] meson --buildtype=debug build

In file included from ../lib/eal/include/dev_driver.h:12,

                 from ../lib/ethdev/ethdev_driver.h:23,
                 from ../drivers/net/i40e/i40e_rxtx_vec_sse.c:6:
Quoted 10 lines

../drivers/net/i40e/i40e_rxtx_vec_sse.c: In function ‘descs_to_fdir_32b’:
../lib/eal/include/rte_common.h:499:51: error: expression in static assertion is not constant
499 | #define RTE_BUILD_BUG_ON(condition) static_assert(!(condition),
#condition)
| ^~~~~~~~~~~~
../drivers/net/i40e/i40e_rxtx_vec_sse.c:147:9: note: in expansion of macro ‘RTE_BUILD_BUG_ON’
147 | RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 <<
FDIR_ID_BIT_SHIFT));

this one is fixable by just making it a constant expression instead of a const variable.

- i40e_rxtx_vec_sse.c: const uint32_t FDIR_ID_BIT_SHIFT = 13;
+ i40e_rxtx_vec_sse.c: #define FDIR_ID_BIT_SHIFT (13u)

... whatever ...

+ i40e_rxtx_vec_sse.c: #undef FDIR_ID_BIT_SHIFT

ty

#26 lore raw

v3 cover replace uses of zero length array

In reply to Stephen Hemminger

Zero length arrays are a GNU extension that has been superseded by flex arrays.

https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

These are places found by cocci/zero_length_array.cocci

v3 - fix misuse of flex array in dpaxx found by clang

v2 - rebased on 23.11-rc3
more places started using zero length array

Stephen Hemminger (10):
member: replace zero length array with flex array
cryptodev: replace zero length array with flex array
security: replace zero length array with flex array
pipeline: replace zero length array with flex array net/nfp: replace zero length array with flex array net/enic: replace zero length array with flex array net/mlx5: replace zero length array with flex array
pdcp: replace zero length array with flex array net/cpfl: replace zero length array with flex array common/dpaxx: remove zero length array

 drivers/common/dpaax/caamflib/desc/ipsec.h | 6 ++----
 drivers/common/mlx5/mlx5_prm.h             | 2 +-
 drivers/net/cpfl/cpfl_flow_engine_fxp.c    | 2 +-
 drivers/net/enic/base/vnic_devcmd.h        | 2 +-
 drivers/net/mlx5/mlx5.h                    | 4 ++--
 drivers/net/mlx5/mlx5_flow.h               | 2 +-
 drivers/net/mlx5/mlx5_tx.h                 | 2 +-
 drivers/net/nfp/flower/nfp_flower_cmsg.h   | 2 +-
 lib/cryptodev/cryptodev_pmd.h              | 2 +-
 lib/member/rte_member_heap.h               | 2 +-
 lib/pdcp/pdcp_entity.h                     | 2 +-
 lib/pipeline/rte_swx_pipeline_internal.h   | 2 +-
 lib/security/rte_security_driver.h         | 2 +-
 13 files changed, 15 insertions(+), 17 deletions(-)
Signature · 1 line
2.42.0
#27 lore raw

PATCH v3 1/10 member: replace zero length array with flex array

Zero length arrays are GNU extension. Replace with standard flex array.

---
 lib/member/rte_member_heap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/member/rte_member_heap.h +1 -1
index 9c4a01aebe95..ab6319bc2de4 100644
@@ -26,7 +26,7 @@ struct hash {
 	uint16_t bkt_cnt;
 	uint16_t num_item;
 	uint32_t seed;
-	struct hash_bkt buckets[0];
+	struct hash_bkt buckets[];
 };
 
 struct node {
Signature · 1 line
2.42.0
#28 lore raw

PATCH v3 2/10 cryptodev: replace zero length array with flex array

In reply to Stephen Hemminger

Zero length arrays are GNU extension. Replace with standard flex array.

---
 lib/cryptodev/cryptodev_pmd.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/cryptodev/cryptodev_pmd.h +1 -1
index 3bb3d95c1338..0732b356883c 100644
@@ -153,7 +153,7 @@ struct rte_cryptodev_sym_session {
 
 	RTE_MARKER cacheline1 __rte_cache_min_aligned;
 	/**< Second cache line - start of the driver session data */
-	uint8_t driver_priv_data[0];
+	uint8_t driver_priv_data[];
 	/**< Driver specific session data, variable size */
 };
 
Signature · 1 line
2.42.0
#29 lore raw

PATCH v3 3/10 security: replace zero length array with flex array

In reply to Stephen Hemminger

Zero length arrays are GNU extension. Replace with standard flex array.

---
 lib/security/rte_security_driver.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/security/rte_security_driver.h +1 -1
index 62664dacdbb4..faa4074f1965 100644
@@ -33,7 +33,7 @@ struct rte_security_session {
 	/**< session private data IOVA address */
 
 	RTE_MARKER cacheline1 __rte_cache_min_aligned;
-	uint8_t driver_priv_data[0];
+	uint8_t driver_priv_data[];
 	/**< Private session material, variable size (depends on driver) */
 };
 
Signature · 1 line
2.42.0
#30 lore raw

PATCH v3 4/10 pipeline: replace zero length array with flex array

In reply to Stephen Hemminger

Zero length arrays are GNU extension. Replace with standard flex array.

---
 lib/pipeline/rte_swx_pipeline_internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/pipeline/rte_swx_pipeline_internal.h +1 -1
index a67b6e965de7..8ec12263b989 100644
@@ -213,7 +213,7 @@ TAILQ_HEAD(rss_tailq, rss);
 
 struct rss_runtime {
 	uint32_t key_size; /* key size in bytes. */
-	uint8_t key[0]; /* key. */
+	uint8_t key[]; /* key. */
 };
 
 /*
Signature · 1 line
2.42.0
#31 lore raw

PATCH v3 5/10 net/nfp: replace zero length array with flex array

In reply to Stephen Hemminger

Zero length arrays are GNU extension. Replace with standard flex array.

---
 drivers/net/nfp/flower/nfp_flower_cmsg.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/nfp/flower/nfp_flower_cmsg.h +1 -1
index c2938fb6f63c..f00d2e838d8f 100644
@@ -73,7 +73,7 @@ struct nfp_flower_cmsg_mac_repr {
 		uint8_t info;
 		uint8_t nbi_port;
 		uint8_t phys_port;
-	} ports[0];
+	} ports[];
 };
 
 /*
Signature · 1 line
2.42.0
#32 lore raw

PATCH v3 6/10 net/enic: replace zero length array with flex array

In reply to Stephen Hemminger

Zero length arrays are GNU extension. Replace with standard flex array.

---
 drivers/net/enic/base/vnic_devcmd.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/enic/base/vnic_devcmd.h +1 -1
index 253a791c3f65..f91cc3078d63 100644
@@ -765,7 +765,7 @@ struct vnic_devcmd_notify {
 struct vnic_devcmd_provinfo {
 	uint8_t oui[3];
 	uint8_t type;
-	uint8_t data[0];
+	uint8_t data[];
 };
 
 /*
Signature · 1 line
2.42.0
#33 lore raw

PATCH v3 7/10 net/mlx5: replace zero length array with flex array

In reply to Stephen Hemminger

Zero length arrays are GNU extension. Replace with standard flex array.

---
 drivers/common/mlx5/mlx5_prm.h | 2 +-
 drivers/net/mlx5/mlx5.h        | 4 ++--
 drivers/net/mlx5/mlx5_flow.h   | 2 +-
 drivers/net/mlx5/mlx5_tx.h     | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
drivers/common/mlx5/mlx5_prm.h +1 -1
index 9e22dce6da13..932b89bd79d3 100644
@@ -5181,7 +5181,7 @@ struct mlx5_ifc_flow_context_bits {
 	u8 reserved_at_e0[0x40];
 	u8 encrypt_decrypt_obj_id[0x20];
 	u8 reserved_at_140[0x16c0];
-	union mlx5_ifc_dest_format_flow_counter_list_auto_bits destination[0];
+	union mlx5_ifc_dest_format_flow_counter_list_auto_bits destination[];
 };
 
 struct mlx5_ifc_set_fte_in_bits {
drivers/net/mlx5/mlx5.h +2 -2
index f0d63a0ba5f5..89d13900fd3c 100644
@@ -1308,7 +1308,7 @@ struct mlx5_aso_ct_pool {
 	};
 	struct mlx5_aso_sq *sq; /* Async ASO SQ. */
 	struct mlx5_aso_sq *shared_sq; /* Shared ASO SQ. */
-	struct mlx5_aso_ct_action actions[0];
+	struct mlx5_aso_ct_action actions[];
 	/* CT action structures bulk. */
 };
 
@@ -1325,7 +1325,7 @@ struct mlx5_aso_ct_pools_mng {
 	rte_spinlock_t ct_sl; /* The ASO CT free list lock. */
 	rte_rwlock_t resize_rwl; /* The ASO CT pool resize lock. */
 	struct aso_ct_list free_cts; /* Free ASO CT objects list. */
-	struct mlx5_aso_sq aso_sqs[0]; /* ASO queue objects. */
+	struct mlx5_aso_sq aso_sqs[]; /* ASO queue objects. */
 };
 
 #ifdef PEDANTIC
drivers/net/mlx5/mlx5_flow.h +1 -1
index 6dde9de688b9..b35079b30a6e 100644
@@ -1257,7 +1257,7 @@ struct rte_flow_hw {
 	cnt_id_t cnt_id;
 	uint32_t mtr_id;
 	uint32_t rule_idx;
-	uint8_t rule[0]; /* HWS layer data struct. */
+	uint8_t rule[]; /* HWS layer data struct. */
 } __rte_packed;
 
 #ifdef PEDANTIC
drivers/net/mlx5/mlx5_tx.h +1 -1
index e59ce37667ba..2045e5174e6d 100644
@@ -171,7 +171,7 @@ struct mlx5_txq_data {
 	struct mlx5_txq_stats stats; /* TX queue counters. */
 	struct mlx5_txq_stats stats_reset; /* stats on last reset. */
 	struct mlx5_uar_data uar_data;
-	struct rte_mbuf *elts[0];
+	struct rte_mbuf *elts[];
 	/* Storage for queued packets, must be the last field. */
 } __rte_cache_aligned;
 
Signature · 1 line
2.42.0
#34 lore raw

PATCH v3 8/10 pdcp: replace zero length array with flex array

In reply to Stephen Hemminger

Zero length arrays are GNU extension. Replace with standard flex array.

---
 lib/pdcp/pdcp_entity.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/pdcp/pdcp_entity.h +1 -1
index 4fc6342a5ced..f854192e98dc 100644
@@ -185,7 +185,7 @@ struct entity_priv_dl_part {
 	/** Reorder packet buffer */
 	struct pdcp_reorder reorder;
 	/** Bitmap memory region */
-	uint8_t bitmap_mem[0];
+	uint8_t bitmap_mem[];
 };
 
 struct entity_priv_ul_part {
Signature · 1 line
2.42.0
#35 lore raw

PATCH v3 9/10 net/cpfl: replace zero length array with flex array

In reply to Stephen Hemminger

Zero length arrays are GNU extension. Replace with standard flex array.

---
 drivers/net/cpfl/cpfl_flow_engine_fxp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/cpfl/cpfl_flow_engine_fxp.c +1 -1
index 8a4e1419b4db..39a281fa61ee 100644
@@ -53,7 +53,7 @@ struct cpfl_rule_info_meta {
 	uint32_t pr_num;			/* number of pattern rules */
 	uint32_t mr_num;			/* number of modification rules */
 	uint32_t rule_num;			/* number of all rules */
-	struct cpfl_rule_info rules[0];
+	struct cpfl_rule_info rules[];
 };
 
 static uint32_t cpfl_fxp_mod_idx_alloc(struct cpfl_adapter_ext *ad);
Signature · 1 line
2.42.0
#36 lore raw

PATCH v3 10/10 common/dpaxx: remove zero length array

In reply to Stephen Hemminger

There is a place holder zero length array in this driver. But since the structure is embedded in other structures, it could not have been safely used anyway.
There doesn't appear to be any uses of it in the current code.

---
 drivers/common/dpaax/caamflib/desc/ipsec.h | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/common/dpaax/caamflib/desc/ipsec.h +2 -4
index 95fc3ea5ba3b..9d59b93292f9 100644
@@ -336,7 +336,6 @@ struct ipsec_encap_gcm {
  * @ip_hdr_len: optional IP Header length (in bytes)
  *  reserved - 16b
  *  Opt. IP Hdr Len - 16b
- * @ip_hdr: optional IP Header content (only for IPsec legacy mode)
  */
 struct ipsec_encap_pdb {
 	uint32_t options;
@@ -350,7 +349,6 @@ struct ipsec_encap_pdb {
 	};
 	uint32_t spi;
 	uint32_t ip_hdr_len;
-	uint8_t ip_hdr[0];
 };
 
 static inline unsigned int
@@ -776,7 +774,7 @@ cnstr_shdsc_ipsec_encap(uint32_t *descbuf, bool ps, bool swap,
 		PROGRAM_SET_36BIT_ADDR(p);
 	phdr = SHR_HDR(p, share, hdr, 0);
 	__rta_copy_ipsec_encap_pdb(p, pdb, cipherdata->algtype);
-	COPY_DATA(p, pdb->ip_hdr, pdb->ip_hdr_len);
+
 	SET_LABEL(p, hdr);
 	pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, BOTH|SHRD);
 	if (authdata->keylen)
@@ -913,7 +911,7 @@ cnstr_shdsc_ipsec_encap_des_aes_xcbc(uint32_t *descbuf,
 	PROGRAM_CNTXT_INIT(p, descbuf, 0);
 	phdr = SHR_HDR(p, share, hdr, 0);
 	__rta_copy_ipsec_encap_pdb(p, pdb, cipherdata->algtype);
-	COPY_DATA(p, pdb->ip_hdr, pdb->ip_hdr_len);
+
 	SET_LABEL(p, hdr);
 	pkeyjump = JUMP(p, keyjump, LOCAL_JUMP, ALL_TRUE, SHRD | SELF);
 	/*
Signature · 1 line
2.42.0
#37 lore raw

v3 cover use static_assert to catch build errors

In reply to Stephen Hemminger

This series fixes a couple places where expressions that could not be evaluated as constant early in compiler passes were used. And then converts RTE_BUILD_BUG_ON() with static_assert.

static_assert() is more picky about the expression has to be a constant, which also catches some existing undefined behavior that pre-existed.

Stephen Hemminger (5):
event/opdl: fix non-constant compile time assertion net/sfc: fix non-constant expression in RTE_BUILD_BUG_ON() net/i40e: avoid using const variable in assertion
mempool: avoid floating point expression in static assertion
eal: replace out of bounds VLA with static_assert

 drivers/event/opdl/opdl_ring.c       |  5 ++++-
 drivers/net/i40e/i40e_ethdev.h       |  1 +
 drivers/net/i40e/i40e_rxtx_vec_sse.c | 10 ++++------
 drivers/net/sfc/sfc_ef100_tx.c       |  7 +++++--
 lib/eal/include/rte_common.h         |  3 ++-
 lib/mempool/rte_mempool.c            |  4 +---
 6 files changed, 17 insertions(+), 13 deletions(-)
Signature · 1 line
2.43.0
#38 lore raw

PATCH v3 1/5 event/opdl: fix non-constant compile time assertion

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure library")
---
 drivers/event/opdl/opdl_ring.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/event/opdl/opdl_ring.c +4 -1
index 69392b56bbec..24e0bbe3222d 100644
@@ -31,6 +31,9 @@
 #define OPDL_OPA_MASK    (0xFF)
 #define OPDL_OPA_OFFSET  (0x38)
 
+/* Equivalent to rte_is_power_of_2() but as macro. */
+#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
+
 /* Types of dependency between stages */
 enum dep_type {
 	DEP_NONE = 0,  /* no dependency */
@@ -910,7 +913,7 @@ opdl_ring_create(const char *name, uint32_t num_slots, uint32_t slot_size,
 			RTE_CACHE_LINE_MASK) != 0);
 	RTE_BUILD_BUG_ON((offsetof(struct opdl_ring, slots) &
 			RTE_CACHE_LINE_MASK) != 0);
-	RTE_BUILD_BUG_ON(!rte_is_power_of_2(OPDL_DISCLAIMS_PER_LCORE));
+	RTE_BUILD_BUG_ON(!IS_POWER_OF_2(OPDL_DISCLAIMS_PER_LCORE));
 
 	/* Parameter checking */
 	if (name == NULL) {
Signature · 1 line
2.43.0
#39 lore raw

Re: [PATCH v3 1/5] event/opdl: fix non-constant compile time assertion

Quoted 19 lines · Stephen Hemminger

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure library")
---
  drivers/event/opdl/opdl_ring.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/event/opdl/opdl_ring.c +0 -0
index 69392b56bbec..24e0bbe3222d 100644

drivers/event/opdl/opdl_ring.c line 35

Quoted 5 lines
drivers/event/opdl/opdl_ring.c +2 -0
@@ -31,6 +31,9 @@
  #define OPDL_OPA_MASK    (0xFF)
  #define OPDL_OPA_OFFSET  (0x38)
 
+/* Equivalent to rte_is_power_of_2() but as macro. */
+#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)

IMHO adding it in specific driver is a wrong direction. I'm afraid it will result in duplication of such macros in code base without clear reason why.

May be it is better to add it with a proper name to EAL?

Quoted 13 lines
drivers/event/opdl/opdl_ring.c +2 -1
+
  /* Types of dependency between stages */
  enum dep_type {
  	DEP_NONE = 0,  /* no dependency */
@@ -910,7 +913,7 @@ opdl_ring_create(const char *name, uint32_t num_slots, uint32_t slot_size,
  			RTE_CACHE_LINE_MASK) != 0);
  	RTE_BUILD_BUG_ON((offsetof(struct opdl_ring, slots) &
  			RTE_CACHE_LINE_MASK) != 0);
-	RTE_BUILD_BUG_ON(!rte_is_power_of_2(OPDL_DISCLAIMS_PER_LCORE));
+	RTE_BUILD_BUG_ON(!IS_POWER_OF_2(OPDL_DISCLAIMS_PER_LCORE));
 
  	/* Parameter checking */
  	if (name == NULL) {
#40 lore raw

Re: [PATCH v3 1/5] event/opdl: fix non-constant compile time assertion

Quoted 29 lines · Andrew Rybchenko
Quoted 19 lines · Stephen Hemminger

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure library")
---
  drivers/event/opdl/opdl_ring.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/event/opdl/opdl_ring.c +0 -0
index 69392b56bbec..24e0bbe3222d 100644

drivers/event/opdl/opdl_ring.c line 34

Quoted 4 lines
drivers/event/opdl/opdl_ring.c +2 -0
@@ -31,6 +31,9 @@
  #define OPDL_OPA_MASK    (0xFF)
  #define OPDL_OPA_OFFSET  (0x38)
+/* Equivalent to rte_is_power_of_2() but as macro. */
+#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)

IMHO adding it in specific driver is a wrong direction. I'm afraid it will result in duplication of such macros in code base without clear reason why.

Quoted 2 lines

May be it is better to add it with a proper name to EAL?

+1
Even if it's a lower-case name, lets make it a macro in EAL so we can use it everywhere in asserts.

#41 lore raw

RE: [PATCH v3 1/5] event/opdl: fix non-constant compile time assertion

Quoted 35 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, 17 January 2024 10.27

Quoted 7 lines · Andrew Rybchenko
Quoted 6 lines · Stephen Hemminger

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure

library")

Quoted 9 lines

Review

Quoted 9 lines
---
  drivers/event/opdl/opdl_ring.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/event/opdl/opdl_ring.c

Quoted 13 lines

drivers/event/opdl/opdl_ring.c line 34

Quoted 8 lines

index 69392b56bbec..24e0bbe3222d 100644

drivers/event/opdl/opdl_ring.c +2 -0
@@ -31,6 +31,9 @@
  #define OPDL_OPA_MASK    (0xFF)
  #define OPDL_OPA_OFFSET  (0x38)
+/* Equivalent to rte_is_power_of_2() but as macro. */
+#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)

IMHO adding it in specific driver is a wrong direction. I'm afraid it will result in duplication of such macros in code base without clear reason why.

Quoted 6 lines
Quoted 2 lines

May be it is better to add it with a proper name to EAL?

+1
Even if it's a lower-case name, lets make it a macro in EAL so we can use
it everywhere in asserts.

Here's a thought... we could introduce a new code convention for this purpose. E.g. either:

1. Macros in lower case are guaranteed to access each parameter exactly once (and might not be usable for static_assert; they somewhat resemble inline functions), and macros in upper case may access parameters any number of times (and must be usable for static_assert).

Or:

2. Macros that access any parameter zero or multiple times must have their name postfixed _UNSAFE (or some other word).

#42 lore raw

PATCH v3 2/5 net/sfc: fix non-constant expression in RTE_BUILD_BUG_ON()

In reply to Stephen Hemminger

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluated in first pass of compiler. Replace RTE_MIN() with equivalent macro.

This will cause errors from checkpatch about multiple evaluations of same expression in macro but it is ok in this case.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")
---
 drivers/net/sfc/sfc_ef100_tx.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/sfc/sfc_ef100_tx.c +5 -2
index 1b6374775f07..f4bcadc1e8e0 100644
@@ -26,6 +26,10 @@
 #include "sfc_ef100.h"
 #include "sfc_nic_dma_dp.h"
 
+#ifndef MIN
+/* not typesafe but is a constant */
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#endif
 
 #define sfc_ef100_tx_err(_txq, ...) \
 	SFC_DP_LOG(SFC_KVARG_DATAPATH_EF100, ERR, &(_txq)->dp.dpq, __VA_ARGS__)
@@ -563,8 +567,7 @@ sfc_ef100_tx_pkt_descs_max(const struct rte_mbuf *m)
 		 * (split into many Tx descriptors).
 		 */
 		RTE_BUILD_BUG_ON(SFC_EF100_TX_SEND_DESC_LEN_MAX <
-				 RTE_MIN((unsigned int)EFX_MAC_PDU_MAX,
-				 SFC_MBUF_SEG_LEN_MAX));
+				 MIN((unsigned int)EFX_MAC_PDU_MAX, SFC_MBUF_SEG_LEN_MAX));
 	}
 
 	if (m->ol_flags & sfc_dp_mport_override) {
Signature · 1 line
2.43.0
#43 lore raw

Re: [PATCH v3 2/5] net/sfc: fix non-constant expression in RTE_BUILD_BUG_ON()

Quoted 8 lines · Stephen Hemminger

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluated in first pass of compiler. Replace RTE_MIN() with equivalent macro.

This will cause errors from checkpatch about multiple evaluations of same expression in macro but it is ok in this case.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")

I'm not sure that it is really a fix.

Quoted 12 lines
---
  drivers/net/sfc/sfc_ef100_tx.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/sfc/sfc_ef100_tx.c +0 -0
index 1b6374775f07..f4bcadc1e8e0 100644

drivers/net/sfc/sfc_ef100_tx.c line 32

Quoted 7 lines
drivers/net/sfc/sfc_ef100_tx.c +4 -0
@@ -26,6 +26,10 @@
  #include "sfc_ef100.h"
  #include "sfc_nic_dma_dp.h"
 
+#ifndef MIN
+/* not typesafe but is a constant */
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#endif

IMHO adding it in specific driver is a wrong direction. I'm afraid it will result in duplication of such macros in code base without clear reason why.

May be it is better to add it with a proper name to EAL?

Quoted 13 lines
drivers/net/sfc/sfc_ef100_tx.c +1 -2
  #define sfc_ef100_tx_err(_txq, ...) \
  	SFC_DP_LOG(SFC_KVARG_DATAPATH_EF100, ERR, &(_txq)->dp.dpq, __VA_ARGS__)
@@ -563,8 +567,7 @@ sfc_ef100_tx_pkt_descs_max(const struct rte_mbuf *m)
  		 * (split into many Tx descriptors).
  		 */
  		RTE_BUILD_BUG_ON(SFC_EF100_TX_SEND_DESC_LEN_MAX <
-				 RTE_MIN((unsigned int)EFX_MAC_PDU_MAX,
-				 SFC_MBUF_SEG_LEN_MAX));
+				 MIN((unsigned int)EFX_MAC_PDU_MAX, SFC_MBUF_SEG_LEN_MAX));
  	}
 
  	if (m->ol_flags & sfc_dp_mport_override) {
#44 lore raw

PATCH v3 3/5 net/i40e: avoid using const variable in assertion

In reply to Stephen Hemminger

Clang does not allow const variable in a static_assert expression.

---
 drivers/net/i40e/i40e_ethdev.h       |  1 +
 drivers/net/i40e/i40e_rxtx_vec_sse.c | 10 ++++------
 2 files changed, 5 insertions(+), 6 deletions(-)
drivers/net/i40e/i40e_ethdev.h +1 -0
index 1bbe7ad37600..445e1c0b381f 100644
@@ -278,6 +278,7 @@ enum i40e_flxpld_layer_idx {
 #define I40E_DEFAULT_DCB_APP_PRIO   3
 
 #define I40E_FDIR_PRG_PKT_CNT       128
+#define I40E_FDIR_ID_BIT_SHIFT	    13
 
 /*
  * Struct to store flow created.
drivers/net/i40e/i40e_rxtx_vec_sse.c +4 -6
index 9200a23ff662..2d4480a7651b 100644
@@ -143,10 +143,9 @@ descs_to_fdir_32b(volatile union i40e_rx_desc *rxdp, struct rte_mbuf **rx_pkt)
 	/* convert fdir_id_mask into a single bit, then shift as required for
 	 * correct location in the mbuf->olflags
 	 */
-	const uint32_t FDIR_ID_BIT_SHIFT = 13;
-	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << FDIR_ID_BIT_SHIFT));
+	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << I40E_FDIR_ID_BIT_SHIFT));
 	v_fd_id_mask = _mm_srli_epi32(v_fd_id_mask, 31);
-	v_fd_id_mask = _mm_slli_epi32(v_fd_id_mask, FDIR_ID_BIT_SHIFT);
+	v_fd_id_mask = _mm_slli_epi32(v_fd_id_mask, I40E_FDIR_ID_BIT_SHIFT);
 
 	/* The returned value must be combined into each mbuf. This is already
 	 * being done for RSS and VLAN mbuf olflags, so return bits to OR in.
@@ -205,10 +204,9 @@ descs_to_fdir_16b(__m128i fltstat, __m128i descs[4], struct rte_mbuf **rx_pkt)
 	descs[0] = _mm_blendv_epi8(descs[0], _mm_setzero_si128(), v_desc0_mask);
 
 	/* Shift to 1 or 0 bit per u32 lane, then to RTE_MBUF_F_RX_FDIR_ID offset */
-	const uint32_t FDIR_ID_BIT_SHIFT = 13;
-	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << FDIR_ID_BIT_SHIFT));
+	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << I40E_FDIR_ID_BIT_SHIFT));
 	__m128i v_mask_one_bit = _mm_srli_epi32(v_fdir_id_mask, 31);
-	return _mm_slli_epi32(v_mask_one_bit, FDIR_ID_BIT_SHIFT);
+	return _mm_slli_epi32(v_mask_one_bit, I40E_FDIR_ID_BIT_SHIFT);
 }
 #endif
 
Signature · 1 line
2.43.0
#45 lore raw

PATCH v3 4/5 mempool: avoid floating point expression in static assertion

In reply to Stephen Hemminger

Clang does not handle casts in static_assert() expressions. It doesn't like use of floating point to calculate threshold. Use a different expression with same effect; yes this will cause checkpatch nag.

---
 lib/mempool/rte_mempool.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
lib/mempool/rte_mempool.c +1 -3
index b7a19bea7185..ba3a54cfc298 100644
@@ -50,9 +50,7 @@ static void
 mempool_event_callback_invoke(enum rte_mempool_event event,
 			      struct rte_mempool *mp);
 
-#define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
-#define CALC_CACHE_FLUSHTHRESH(c)	\
-	((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
+#define CALC_CACHE_FLUSHTHRESH(c) ((c) + (c) / 2)
 
 #if defined(RTE_ARCH_X86)
 /*
Signature · 1 line
2.43.0
#46 lore raw

Re: [PATCH v3 4/5] mempool: avoid floating point expression in static assertion

Quoted 15 lines · Stephen Hemminger

Clang does not handle casts in static_assert() expressions. It doesn't like use of floating point to calculate threshold. Use a different expression with same effect; yes this will cause checkpatch nag.

---
  lib/mempool/rte_mempool.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
lib/mempool/rte_mempool.c +0 -0
index b7a19bea7185..ba3a54cfc298 100644

lib/mempool/rte_mempool.c static void · line 53

Quoted 4 lines
lib/mempool/rte_mempool.c +0 -1
@@ -50,9 +50,7 @@ static void
  mempool_event_callback_invoke(enum rte_mempool_event event,
  			      struct rte_mempool *mp);
 
-#define CACHE_FLUSHTHRESH_MULTIPLIER 1.5

drivers/net/mlx5/mlx5_rxq.c:1447: * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a

lib/mempool/rte_mempool.c static void · line 53

Quoted 3 lines
lib/mempool/rte_mempool.c +1 -2
-#define CALC_CACHE_FLUSHTHRESH(c)	\
-	((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
+#define CALC_CACHE_FLUSHTHRESH(c) ((c) + (c) / 2)

Maybe ((c) * 3 / 2) to avoid double usage of arg in macro?

Quoted 3 lines
lib/mempool/rte_mempool.c +0 -0
  #if defined(RTE_ARCH_X86)
  /*
#47 lore raw

PATCH v3 5/5 eal: replace out of bounds VLA with static_assert

In reply to Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

Need to add brackets {} around the static_assert() to workaround a bug in clang. Clang was not handling static_assert() in a switch case properly.

---
 lib/eal/include/rte_common.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/include/rte_common.h +2 -1
index c1ba32d00e47..413bed23cb73 100644
@@ -16,6 +16,7 @@
 extern "C" {
 #endif
 
+#include <assert.h>
 #include <stdint.h>
 #include <limits.h>
 
@@ -495,7 +496,7 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 /**
  * Triggers an error at compilation time if the condition is true.
  */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(condition) { static_assert(!(condition), #condition); }
 
 /*********** Cache line related macros ********/
 
Signature · 1 line
2.43.0
Acked-by on v3 5/5
#48 lore raw

Re: [PATCH v3 5/5] eal: replace out of bounds VLA with static_assert

Quoted 7 lines · Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

Quoted 7 lines

Need to add brackets {} around the static_assert() to workaround a bug in clang. Clang was not handling static_assert() in a switch case properly.

Does it imply any limitations on Gcc / Clang versions to be used? Is it documented somewhere in DPDK?

#49 lore raw

Re: [PATCH v3 5/5] eal: replace out of bounds VLA with static_assert

Quoted 12 lines · On Wed, 17 Jan 2024 10:52:40 +0300 Andrew Rybchenko…
Quoted 11 lines · Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

Need to add brackets {} around the static_assert() to workaround a bug in clang. Clang was not handling static_assert() in a switch case properly.

Quoted 8 lines
Quoted 3 lines

Does it imply any limitations on Gcc / Clang versions to be used? Is it documented somewhere in DPDK?

No new language versions problems. There are already direct usages of static_assert() in some drivers.

#50 lore raw

Re: [PATCH v3 5/5] eal: replace out of bounds VLA with static_assert

In reply to Stephen Hemminger
Quoted 31 lines · Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

Need to add brackets {} around the static_assert() to workaround a bug in clang. Clang was not handling static_assert() in a switch case properly.

---
  lib/eal/include/rte_common.h | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/include/rte_common.h +1 -0
index c1ba32d00e47..413bed23cb73 100644
@@ -16,6 +16,7 @@
  extern "C" {
  #endif
 
+#include <assert.h>
  #include <stdint.h>
  #include <limits.h>
 

lib/eal/include/rte_common.h rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align) · line 502

Quoted 8 lines
lib/eal/include/rte_common.h +1 -1
@@ -495,7 +496,7 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
  /**
   * Triggers an error at compilation time if the condition is true.
   */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(condition) { static_assert(!(condition), #condition); }
 
  /*********** Cache line related macros ********/
 

Should one use RTE_BUILD_BUG_ON() or static_assert() in new DPDK code?

If static_assert() occasionally needs a work-around, it sounds like keeping using RTE_BUILD_BUG_ON() everywhere is the better choice.

#51 lore raw

Re: [PATCH v3 5/5] eal: replace out of bounds VLA with static_assert

Quoted 5 lines · On Wed, 17 Jan 2024 08:53:44 +0100 Mattias Rönnblom <[email protected]>…
Quoted 5 lines

* Triggers an error at compilation time if the condition is true. */

lib/eal/include/rte_common.h +1 -1
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(condition) { static_assert(!(condition), #condition); }
Quoted 7 lines
Quoted 2 lines

/*********** Cache line related macros ********/

Should one use RTE_BUILD_BUG_ON() or static_assert() in new DPDK code?

If static_assert() occasionally needs a work-around, it sounds like keeping using RTE_BUILD_BUG_ON() everywhere is the better choice.

Either choice is the same. Keeping the macro instead of directly using static_assert will reduce the effort to change when the next C standard introduces something different.

But using static_assert() can allow for a better warning message.

#52 lore raw

v4 cover use static assert to cathc build errors

In reply to Stephen Hemminger

This series fixes a couple places where expressions that could not be evaluated as constant early in compiler passes were used. Then converts RTE_BUILD_BUG_ON() with static_assert.

static_assert() is more picky about the expression has to be a constant, which also catches some existing undefined behavior that pre-existed.

v4 - incorporate review feedback
add RTE_MIN_T and RTE_MAX_T macros

Stephen Hemminger (6):
eal: introduce RTE_MIN_T() and RTE_MAX_T() macros event/opdl: fix non-constant compile time assertion net/sfc: fix non-constant expression in RTE_BUILD_BUG_ON() net/i40e: avoid using const variable in assertion
mempool: avoid floating point expression in static assertion
eal: replace out of bounds VLA with static_assert

 drivers/event/opdl/opdl_ring.c       |  2 +-
 drivers/net/i40e/i40e_ethdev.h       |  1 +
 drivers/net/i40e/i40e_rxtx_vec_sse.c | 10 ++++------
 drivers/net/mlx5/mlx5_rxq.c          |  2 +-
 drivers/net/sfc/sfc_ef100_tx.c       |  4 +---
 lib/eal/include/rte_common.h         | 19 ++++++++++++++++++-
 lib/mempool/rte_mempool.c            |  7 ++++---
 7 files changed, 30 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
#53 lore raw

PATCH v4 1/6 eal: introduce RTE_MIN_T() and RTE_MAX_T() macros

These macros work like RTE_MIN and RTE_MAX but take an explicit type. Necessary when being used in static assertions since RTE_MIN and RTE_MAX use temporary variables which confuses compilers constant expression checks. These macros could also be useful in other scenarios when bounded range is useful.

Naming is chosen to be similar to Linux kernel conventions.

---
 lib/eal/include/rte_common.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/eal/include/rte_common.h +16 -0
index c1ba32d00e47..33680e818bfb 100644
@@ -585,6 +585,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a < _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the minimum of two numbers
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MIN_T(a, b, t) \
+	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
+
 /**
  * Macro to return the maximum of two numbers
  */
@@ -595,6 +603,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a > _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the maxiimum of two numbers
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MAX_T(a, b, t) \
+	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
+
 /*********** Other general functions / macros ********/
 
 #ifndef offsetof
Signature · 1 line
2.43.0
Acked-by on v4 1/6
#54 lore raw

RE: [PATCH v4 1/6] eal: introduce RTE_MIN_T() and RTE_MAX_T() macros

Quoted 39 lines

These macros work like RTE_MIN and RTE_MAX but take an explicit type. Necessary when being used in static assertions since RTE_MIN and RTE_MAX use temporary variables which confuses compilers constant expression checks. These macros could also be useful in other scenarios when bounded range is useful.

Naming is chosen to be similar to Linux kernel conventions.

---
 lib/eal/include/rte_common.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/eal/include/rte_common.h +10 -0
index c1ba32d00e47..33680e818bfb 100644
@@ -585,6 +585,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a < _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the minimum of two numbers
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MIN_T(a, b, t) \
+	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
+
 /**
  * Macro to return the maximum of two numbers
  */
@@ -595,6 +603,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a > _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the maxiimum of two numbers

lib/eal/include/rte_common.h __extension__ typedef uint64_t RTE_MARKER64[0];

Quoted 10 lines
lib/eal/include/rte_common.h +6 -1
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MAX_T(a, b, t) \
+	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
+
 /*********** Other general functions / macros ********/
 
 #ifndef offsetof
--
Quoted 1 line

2.43.0

Acked-by on v4 1/6
#55 lore raw

Re: [PATCH v4 1/6] eal: introduce RTE_MIN_T() and RTE_MAX_T() macros

In reply to Stephen Hemminger
Quoted 9 lines · Stephen Hemminger

These macros work like RTE_MIN and RTE_MAX but take an explicit type. Necessary when being used in static assertions since RTE_MIN and RTE_MAX use temporary variables which confuses compilers constant expression checks. These macros could also be useful in other scenarios when bounded range is useful.

Naming is chosen to be similar to Linux kernel conventions.

Acked-by on v4 1/6
#56 lore raw

Re: [PATCH v4 1/6] eal: introduce RTE_MIN_T() and RTE_MAX_T() macros

In reply to Stephen Hemminger
Quoted 7 lines · Stephen Hemminger

These macros work like RTE_MIN and RTE_MAX but take an explicit type. Necessary when being used in static assertions since RTE_MIN and RTE_MAX use temporary variables which confuses compilers constant expression checks. These macros could also be useful in other scenarios when bounded range is useful.

Naming is chosen to be similar to Linux kernel conventions.

parameter ordering seems weird, also Linux kernel copied? just curious more than anything. if not i would put 't' as the first parameter.

Quoted 42 lines
---
 lib/eal/include/rte_common.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/eal/include/rte_common.h +16 -0
index c1ba32d00e47..33680e818bfb 100644
@@ -585,6 +585,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a < _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the minimum of two numbers
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MIN_T(a, b, t) \
+	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
+
 /**
  * Macro to return the maximum of two numbers
  */
@@ -595,6 +603,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a > _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the maxiimum of two numbers
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MAX_T(a, b, t) \
+	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
+
 /*********** Other general functions / macros ********/
 
 #ifndef offsetof
Signature · 1 line
2.43.0
#57 lore raw

Re: [PATCH v4 1/6] eal: introduce RTE_MIN_T() and RTE_MAX_T() macros

Quoted 7 lines · On Fri, 19 Jan 2024 12:58:47 -0800 Tyler Retzlaff…
Quoted 6 lines · Stephen Hemminger

These macros work like RTE_MIN and RTE_MAX but take an explicit type. Necessary when being used in static assertions since RTE_MIN and RTE_MAX use temporary variables which confuses compilers constant expression checks. These macros could also be useful in other scenarios when bounded range is useful.

Quoted 6 lines
Quoted 1 line

Naming is chosen to be similar to Linux kernel conventions.

parameter ordering seems weird, also Linux kernel copied? just curious more than anything. if not i would put 't' as the first parameter.

Hmm, kernel version takes type first will use that.

#58 lore raw

PATCH v4 2/6 event/opdl: fix non-constant compile time assertion

In reply to Stephen Hemminger

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with the macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure library")
---
 drivers/event/opdl/opdl_ring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/event/opdl/opdl_ring.c +1 -1
index 69392b56bbec..da5ea02d1928 100644
@@ -910,7 +910,7 @@ opdl_ring_create(const char *name, uint32_t num_slots, uint32_t slot_size,
 			RTE_CACHE_LINE_MASK) != 0);
 	RTE_BUILD_BUG_ON((offsetof(struct opdl_ring, slots) &
 			RTE_CACHE_LINE_MASK) != 0);
-	RTE_BUILD_BUG_ON(!rte_is_power_of_2(OPDL_DISCLAIMS_PER_LCORE));
+	RTE_BUILD_BUG_ON(!RTE_IS_POWER_OF_2(OPDL_DISCLAIMS_PER_LCORE));
 
 	/* Parameter checking */
 	if (name == NULL) {
Signature · 1 line
2.43.0
Acked-by on v4 2/6
#59 lore raw

Re: [PATCH v4 2/6] event/opdl: fix non-constant compile time assertion

Quoted 10 lines · Stephen Hemminger

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with the macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure library")
#60 lore raw

PATCH v4 3/6 net/sfc: fix non-constant expression in RTE_BUILD_BUG_ON()

In reply to Stephen Hemminger

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluated in first pass of compiler. Replace RTE_MIN() with equivalent macro.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")
---
 drivers/net/sfc/sfc_ef100_tx.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/sfc/sfc_ef100_tx.c +1 -3
index 1b6374775f07..c49d004113d3 100644
@@ -26,7 +26,6 @@
 #include "sfc_ef100.h"
 #include "sfc_nic_dma_dp.h"
 
-
 #define sfc_ef100_tx_err(_txq, ...) \
 	SFC_DP_LOG(SFC_KVARG_DATAPATH_EF100, ERR, &(_txq)->dp.dpq, __VA_ARGS__)
 
@@ -563,8 +562,7 @@ sfc_ef100_tx_pkt_descs_max(const struct rte_mbuf *m)
 		 * (split into many Tx descriptors).
 		 */
 		RTE_BUILD_BUG_ON(SFC_EF100_TX_SEND_DESC_LEN_MAX <
-				 RTE_MIN((unsigned int)EFX_MAC_PDU_MAX,
-				 SFC_MBUF_SEG_LEN_MAX));
+				 RTE_MIN_T(EFX_MAC_PDU_MAX, SFC_MBUF_SEG_LEN_MAX, uint32_t));
 	}
 
 	if (m->ol_flags & sfc_dp_mport_override) {
Signature · 1 line
2.43.0
Reviewed-by on v4 3/6
#61 lore raw

Re: [PATCH v4 3/6] net/sfc: fix non-constant expression in RTE_BUILD_BUG_ON()

Quoted 8 lines · Stephen Hemminger

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluated in first pass of compiler. Replace RTE_MIN() with equivalent macro.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")

One nit below, anyway:

drivers/net/sfc/sfc_ef100_tx.c line 29

Quoted 13 lines
---
  drivers/net/sfc/sfc_ef100_tx.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/sfc/sfc_ef100_tx.c +0 -1
index 1b6374775f07..c49d004113d3 100644
@@ -26,7 +26,6 @@
  #include "sfc_ef100.h"
  #include "sfc_nic_dma_dp.h"
 
-

unrelated change

Quoted 13 lines
drivers/net/sfc/sfc_ef100_tx.c +1 -2
  #define sfc_ef100_tx_err(_txq, ...) \
  	SFC_DP_LOG(SFC_KVARG_DATAPATH_EF100, ERR, &(_txq)->dp.dpq, __VA_ARGS__)
 
@@ -563,8 +562,7 @@ sfc_ef100_tx_pkt_descs_max(const struct rte_mbuf *m)
  		 * (split into many Tx descriptors).
  		 */
  		RTE_BUILD_BUG_ON(SFC_EF100_TX_SEND_DESC_LEN_MAX <
-				 RTE_MIN((unsigned int)EFX_MAC_PDU_MAX,
-				 SFC_MBUF_SEG_LEN_MAX));
+				 RTE_MIN_T(EFX_MAC_PDU_MAX, SFC_MBUF_SEG_LEN_MAX, uint32_t));
  	}
 
  	if (m->ol_flags & sfc_dp_mport_override) {
#62 lore raw

PATCH v4 4/6 net/i40e: avoid using const variable in assertion

In reply to Stephen Hemminger

Clang does not allow const variable in a static_assert expression.

---
 drivers/net/i40e/i40e_ethdev.h       |  1 +
 drivers/net/i40e/i40e_rxtx_vec_sse.c | 10 ++++------
 2 files changed, 5 insertions(+), 6 deletions(-)
drivers/net/i40e/i40e_ethdev.h +1 -0
index 1bbe7ad37600..445e1c0b381f 100644
@@ -278,6 +278,7 @@ enum i40e_flxpld_layer_idx {
 #define I40E_DEFAULT_DCB_APP_PRIO   3
 
 #define I40E_FDIR_PRG_PKT_CNT       128
+#define I40E_FDIR_ID_BIT_SHIFT	    13
 
 /*
  * Struct to store flow created.
drivers/net/i40e/i40e_rxtx_vec_sse.c +4 -6
index 9200a23ff662..2d4480a7651b 100644
@@ -143,10 +143,9 @@ descs_to_fdir_32b(volatile union i40e_rx_desc *rxdp, struct rte_mbuf **rx_pkt)
 	/* convert fdir_id_mask into a single bit, then shift as required for
 	 * correct location in the mbuf->olflags
 	 */
-	const uint32_t FDIR_ID_BIT_SHIFT = 13;
-	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << FDIR_ID_BIT_SHIFT));
+	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << I40E_FDIR_ID_BIT_SHIFT));
 	v_fd_id_mask = _mm_srli_epi32(v_fd_id_mask, 31);
-	v_fd_id_mask = _mm_slli_epi32(v_fd_id_mask, FDIR_ID_BIT_SHIFT);
+	v_fd_id_mask = _mm_slli_epi32(v_fd_id_mask, I40E_FDIR_ID_BIT_SHIFT);
 
 	/* The returned value must be combined into each mbuf. This is already
 	 * being done for RSS and VLAN mbuf olflags, so return bits to OR in.
@@ -205,10 +204,9 @@ descs_to_fdir_16b(__m128i fltstat, __m128i descs[4], struct rte_mbuf **rx_pkt)
 	descs[0] = _mm_blendv_epi8(descs[0], _mm_setzero_si128(), v_desc0_mask);
 
 	/* Shift to 1 or 0 bit per u32 lane, then to RTE_MBUF_F_RX_FDIR_ID offset */
-	const uint32_t FDIR_ID_BIT_SHIFT = 13;
-	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << FDIR_ID_BIT_SHIFT));
+	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << I40E_FDIR_ID_BIT_SHIFT));
 	__m128i v_mask_one_bit = _mm_srli_epi32(v_fdir_id_mask, 31);
-	return _mm_slli_epi32(v_mask_one_bit, FDIR_ID_BIT_SHIFT);
+	return _mm_slli_epi32(v_mask_one_bit, I40E_FDIR_ID_BIT_SHIFT);
 }
 #endif
 
Signature · 1 line
2.43.0
Acked-by on v4 4/6
#64 lore raw

RE: [PATCH v4 4/6] net/i40e: avoid using const variable in assertion

In reply to Stephen Hemminger
Quoted 43 lines

Clang does not allow const variable in a static_assert expression.

---
 drivers/net/i40e/i40e_ethdev.h       |  1 +
 drivers/net/i40e/i40e_rxtx_vec_sse.c | 10 ++++------
 2 files changed, 5 insertions(+), 6 deletions(-)
drivers/net/i40e/i40e_ethdev.h +1 -0
index 1bbe7ad37600..445e1c0b381f 100644
@@ -278,6 +278,7 @@ enum i40e_flxpld_layer_idx {
 #define I40E_DEFAULT_DCB_APP_PRIO   3
 
 #define I40E_FDIR_PRG_PKT_CNT       128
+#define I40E_FDIR_ID_BIT_SHIFT	    13
 
 /*
  * Struct to store flow created.
drivers/net/i40e/i40e_rxtx_vec_sse.c +2 -3
index 9200a23ff662..2d4480a7651b 100644
@@ -143,10 +143,9 @@ descs_to_fdir_32b(volatile union i40e_rx_desc *rxdp, struct rte_mbuf **rx_pkt)
 	/* convert fdir_id_mask into a single bit, then shift as required for
 	 * correct location in the mbuf->olflags
 	 */
-	const uint32_t FDIR_ID_BIT_SHIFT = 13;
-	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << FDIR_ID_BIT_SHIFT));
+	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << I40E_FDIR_ID_BIT_SHIFT));
 	v_fd_id_mask = _mm_srli_epi32(v_fd_id_mask, 31);
-	v_fd_id_mask = _mm_slli_epi32(v_fd_id_mask, FDIR_ID_BIT_SHIFT);
+	v_fd_id_mask = _mm_slli_epi32(v_fd_id_mask, I40E_FDIR_ID_BIT_SHIFT);
 
 	/* The returned value must be combined into each mbuf. This is already
 	 * being done for RSS and VLAN mbuf olflags, so return bits to OR in.
@@ -205,10 +204,9 @@ descs_to_fdir_16b(__m128i fltstat, __m128i descs[4], struct rte_mbuf **rx_pkt)
 	descs[0] = _mm_blendv_epi8(descs[0], _mm_setzero_si128(), v_desc0_mask);
 
 	/* Shift to 1 or 0 bit per u32 lane, then to RTE_MBUF_F_RX_FDIR_ID offset */

drivers/net/i40e/i40e_rxtx_vec_sse.c descs_to_fdir_16b(__m128i fltstat, __m128i descs[4], struct rte_mbuf **rx_pkt)

Quoted 10 lines
drivers/net/i40e/i40e_rxtx_vec_sse.c +2 -4
-	const uint32_t FDIR_ID_BIT_SHIFT = 13;
-	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << FDIR_ID_BIT_SHIFT));
+	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << I40E_FDIR_ID_BIT_SHIFT));
 	__m128i v_mask_one_bit = _mm_srli_epi32(v_fdir_id_mask, 31);
-	return _mm_slli_epi32(v_mask_one_bit, FDIR_ID_BIT_SHIFT);
+	return _mm_slli_epi32(v_mask_one_bit, I40E_FDIR_ID_BIT_SHIFT);
 }
 #endif
 
--
Quoted 1 line

2.43.0

#65 lore raw

PATCH v4 5/6 mempool: avoid floating point expression in static assertion

In reply to Stephen Hemminger

Clang does not handle casts in static_assert() expressions. It doesn't like use of floating point to calculate threshold. Use a different expression with same effect.

Modify comment in mlx5 so that developers don't go searching for old value.

---
 drivers/net/mlx5/mlx5_rxq.c | 2 +-
 lib/mempool/rte_mempool.c   | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
drivers/net/mlx5/mlx5_rxq.c +1 -1
index 1bb036afebb3..7d972b6d927c 100644
@@ -1444,7 +1444,7 @@ mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
 	/*
 	 * rte_mempool_create_empty() has sanity check to refuse large cache
 	 * size compared to the number of elements.
-	 * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
+	 * CALC_CACHE_FLUSHTHRESH() is defined in a C file, so using a
 	 * constant number 2 instead.
 	 */
 	obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
lib/mempool/rte_mempool.c +4 -3
index b7a19bea7185..12390a2c8155 100644
@@ -50,9 +50,10 @@ static void
 mempool_event_callback_invoke(enum rte_mempool_event event,
 			      struct rte_mempool *mp);
 
-#define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
-#define CALC_CACHE_FLUSHTHRESH(c)	\
-	((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
+/* Note: avoid using floating point since that compiler
+ * may not think that is constant.
+ */
+#define CALC_CACHE_FLUSHTHRESH(c) (((c) * 3) / 2)
 
 #if defined(RTE_ARCH_X86)
 /*
Signature · 1 line
2.43.0
Acked-by on v4 5/6
#66 lore raw

RE: [PATCH v4 5/6] mempool: avoid floating point expression in static assertion

Quoted 11 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Wednesday, 17 January 2024 19.20

Clang does not handle casts in static_assert() expressions. It doesn't like use of floating point to calculate threshold. Use a different expression with same effect.

Modify comment in mlx5 so that developers don't go searching for old value.

Reviewed-by
#67 lore raw

Re: [PATCH v4 5/6] mempool: avoid floating point expression in static assertion

Quoted 7 lines · Morten Brørup
Quoted 7 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Wednesday, 17 January 2024 19.20

Clang does not handle casts in static_assert() expressions. It doesn't like use of floating point to calculate threshold. Use a different expression with same effect.

Quoted 7 lines
Quoted 4 lines

Modify comment in mlx5 so that developers don't go searching for old value.

Acked-by on v4 5/6
#68 lore raw

RE: [PATCH v4 5/6] mempool: avoid floating point expression in static assertion

In reply to Stephen Hemminger
Quoted 35 lines

Clang does not handle casts in static_assert() expressions. It doesn't like use of floating point to calculate threshold. Use a different expression with same effect.

Modify comment in mlx5 so that developers don't go searching for old value.

---
 drivers/net/mlx5/mlx5_rxq.c | 2 +-
 lib/mempool/rte_mempool.c   | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
drivers/net/mlx5/mlx5_rxq.c +1 -1
index 1bb036afebb3..7d972b6d927c 100644
@@ -1444,7 +1444,7 @@ mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
 	/*
 	 * rte_mempool_create_empty() has sanity check to refuse large cache
 	 * size compared to the number of elements.
-	 * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
+	 * CALC_CACHE_FLUSHTHRESH() is defined in a C file, so using a
 	 * constant number 2 instead.
 	 */
 	obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
lib/mempool/rte_mempool.c +0 -0
index b7a19bea7185..12390a2c8155 100644
@@ -50,9 +50,10 @@ static void
 mempool_event_callback_invoke(enum rte_mempool_event event,
 			      struct rte_mempool *mp);
 

lib/mempool/rte_mempool.c static void · line 59

Quoted 10 lines
lib/mempool/rte_mempool.c +4 -3
-#define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
-#define CALC_CACHE_FLUSHTHRESH(c)	\
-	((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
+/* Note: avoid using floating point since that compiler
+ * may not think that is constant.
+ */
+#define CALC_CACHE_FLUSHTHRESH(c) (((c) * 3) / 2)
 
 #if defined(RTE_ARCH_X86)
 /*
Quoted 2 lines

--
2.43.0

#69 lore raw

PATCH v4 6/6 eal: replace out of bounds VLA with static_assert

In reply to Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

Need to add brackets {} around the static_assert() to workaround a bug in clang. Clang was not handling static_assert() in a switch case properly.

---
 lib/eal/include/rte_common.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/eal/include/rte_common.h +2 -1
index 33680e818bfb..f63119b444fa 100644
@@ -16,6 +16,7 @@
 extern "C" {
 #endif
 
+#include <assert.h>
 #include <stdint.h>
 #include <limits.h>
 
@@ -495,7 +496,7 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 /**
  * Triggers an error at compilation time if the condition is true.
  */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(condition) { static_assert(!(condition), #condition); }
 
 /*********** Cache line related macros ********/
 
Signature · 1 line
2.43.0
#70 lore raw

v5 cover use static_assert for build error reports

In reply to Stephen Hemminger

This series fixes a couple places where expressions that could not be evaluated as constant early in compiler passes were used. Then converts RTE_BUILD_BUG_ON() with static_assert.

static_assert() is more picky about the expression has to be a constant, which also catches some existing undefined behavior that pre-existed.

The series requires a couple of workarounds to deal with quirks in static_assert() in some toolchains.

v6 - minor cleanups
handle missing macro in old FreeBSD

Stephen Hemminger (6):
eal: introduce RTE_MIN_T() and RTE_MAX_T() macros event/opdl: fix non-constant compile time assertion net/sfc: fix non-constant expression in RTE_BUILD_BUG_ON() net/i40e: avoid using const variable in assertion
mempool: avoid floating point expression in static assertion
eal: replace out of bounds VLA with static_assert

 drivers/event/opdl/opdl_ring.c       |  2 +-
 drivers/net/i40e/i40e_ethdev.h       |  1 +
 drivers/net/i40e/i40e_rxtx_vec_sse.c | 10 ++++------
 drivers/net/mlx5/mlx5_rxq.c          |  2 +-
 drivers/net/sfc/sfc_ef100_tx.c       |  3 +--
 lib/eal/include/rte_common.h         | 27 ++++++++++++++++++++++++++-
 lib/mempool/rte_mempool.c            |  7 ++++---
 7 files changed, 38 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.0
#71 lore raw

PATCH v5 1/6 eal: introduce RTE_MIN_T() and RTE_MAX_T() macros

These macros work like RTE_MIN and RTE_MAX but take an explicit type. Necessary when being used in static assertions since RTE_MIN and RTE_MAX use temporary variables which confuses compilers constant expression checks. These macros could also be useful in other scenarios when bounded range is useful.

Naming is chosen to be similar to Linux kernel conventions.

---
 lib/eal/include/rte_common.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/eal/include/rte_common.h +16 -0
index c1ba32d00e47..33680e818bfb 100644
@@ -585,6 +585,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a < _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the minimum of two numbers
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MIN_T(a, b, t) \
+	((t)(a) < (t)(b) ? (t)(a) : (t)(b))
+
 /**
  * Macro to return the maximum of two numbers
  */
@@ -595,6 +603,14 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
 		_a > _b ? _a : _b; \
 	})
 
+/**
+ * Macro to return the maxiimum of two numbers
+ * does not use temporarys so not safe if a or b is expression
+ * but is guaranteed to be constant for use in static_assert()
+ */
+#define RTE_MAX_T(a, b, t) \
+	((t)(a) > (t)(b) ? (t)(a) : (t)(b))
+
 /*********** Other general functions / macros ********/
 
 #ifndef offsetof
Signature · 1 line
2.43.0
Acked-by on v5 1/6
#72 lore raw

Re: [PATCH v5 1/6] eal: introduce RTE_MIN_T() and RTE_MAX_T() macros

Quoted 6 lines · Stephen Hemminger

These macros work like RTE_MIN and RTE_MAX but take an explicit type. Necessary when being used in static assertions since RTE_MIN and RTE_MAX use temporary variables which confuses compilers constant expression checks. These macros could also be useful in other scenarios when bounded range is useful.

Quoted 6 lines

Naming is chosen to be similar to Linux kernel conventions.

---

#73 lore raw

PATCH v5 2/6 event/opdl: fix non-constant compile time assertion

In reply to Stephen Hemminger

RTE_BUILD_BUG_ON() was being used with a non-constant value. The inline function rte_is_power_of_2() is not constant since inline expansion happens later in the compile process. Replace it with the macro which will be constant.

Fixes 4236ce9bf5bf ("event/opdl: add OPDL ring infrastructure library")
---
 drivers/event/opdl/opdl_ring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/event/opdl/opdl_ring.c +1 -1
index 69392b56bbec..da5ea02d1928 100644
@@ -910,7 +910,7 @@ opdl_ring_create(const char *name, uint32_t num_slots, uint32_t slot_size,
 			RTE_CACHE_LINE_MASK) != 0);
 	RTE_BUILD_BUG_ON((offsetof(struct opdl_ring, slots) &
 			RTE_CACHE_LINE_MASK) != 0);
-	RTE_BUILD_BUG_ON(!rte_is_power_of_2(OPDL_DISCLAIMS_PER_LCORE));
+	RTE_BUILD_BUG_ON(!RTE_IS_POWER_OF_2(OPDL_DISCLAIMS_PER_LCORE));
 
 	/* Parameter checking */
 	if (name == NULL) {
Signature · 1 line
2.43.0
#74 lore raw

PATCH v5 3/6 net/sfc: fix non-constant expression in RTE_BUILD_BUG_ON()

In reply to Stephen Hemminger

The macro RTE_MIN has some hidden assignments to provide type safety which means the statement can not be fully evaluated in first pass of compiler. Replace RTE_MIN() with equivalent macro.

Fixes 4f936666d790 ("net/sfc: support TSO for EF100 native datapath")
---
 drivers/net/sfc/sfc_ef100_tx.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/sfc/sfc_ef100_tx.c +1 -2
index 1b6374775f07..c88ab964547e 100644
@@ -563,8 +563,7 @@ sfc_ef100_tx_pkt_descs_max(const struct rte_mbuf *m)
 		 * (split into many Tx descriptors).
 		 */
 		RTE_BUILD_BUG_ON(SFC_EF100_TX_SEND_DESC_LEN_MAX <
-				 RTE_MIN((unsigned int)EFX_MAC_PDU_MAX,
-				 SFC_MBUF_SEG_LEN_MAX));
+				 RTE_MIN_T(EFX_MAC_PDU_MAX, SFC_MBUF_SEG_LEN_MAX, uint32_t));
 	}
 
 	if (m->ol_flags & sfc_dp_mport_override) {
Signature · 1 line
2.43.0
#75 lore raw

PATCH v5 4/6 net/i40e: avoid using const variable in assertion

In reply to Stephen Hemminger

Clang does not allow const variable in a static_assert expression.

---
 drivers/net/i40e/i40e_ethdev.h       |  1 +
 drivers/net/i40e/i40e_rxtx_vec_sse.c | 10 ++++------
 2 files changed, 5 insertions(+), 6 deletions(-)
drivers/net/i40e/i40e_ethdev.h +1 -0
index 1bbe7ad37600..445e1c0b381f 100644
@@ -278,6 +278,7 @@ enum i40e_flxpld_layer_idx {
 #define I40E_DEFAULT_DCB_APP_PRIO   3
 
 #define I40E_FDIR_PRG_PKT_CNT       128
+#define I40E_FDIR_ID_BIT_SHIFT	    13
 
 /*
  * Struct to store flow created.
drivers/net/i40e/i40e_rxtx_vec_sse.c +4 -6
index 9200a23ff662..2d4480a7651b 100644
@@ -143,10 +143,9 @@ descs_to_fdir_32b(volatile union i40e_rx_desc *rxdp, struct rte_mbuf **rx_pkt)
 	/* convert fdir_id_mask into a single bit, then shift as required for
 	 * correct location in the mbuf->olflags
 	 */
-	const uint32_t FDIR_ID_BIT_SHIFT = 13;
-	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << FDIR_ID_BIT_SHIFT));
+	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << I40E_FDIR_ID_BIT_SHIFT));
 	v_fd_id_mask = _mm_srli_epi32(v_fd_id_mask, 31);
-	v_fd_id_mask = _mm_slli_epi32(v_fd_id_mask, FDIR_ID_BIT_SHIFT);
+	v_fd_id_mask = _mm_slli_epi32(v_fd_id_mask, I40E_FDIR_ID_BIT_SHIFT);
 
 	/* The returned value must be combined into each mbuf. This is already
 	 * being done for RSS and VLAN mbuf olflags, so return bits to OR in.
@@ -205,10 +204,9 @@ descs_to_fdir_16b(__m128i fltstat, __m128i descs[4], struct rte_mbuf **rx_pkt)
 	descs[0] = _mm_blendv_epi8(descs[0], _mm_setzero_si128(), v_desc0_mask);
 
 	/* Shift to 1 or 0 bit per u32 lane, then to RTE_MBUF_F_RX_FDIR_ID offset */
-	const uint32_t FDIR_ID_BIT_SHIFT = 13;
-	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << FDIR_ID_BIT_SHIFT));
+	RTE_BUILD_BUG_ON(RTE_MBUF_F_RX_FDIR_ID != (1 << I40E_FDIR_ID_BIT_SHIFT));
 	__m128i v_mask_one_bit = _mm_srli_epi32(v_fdir_id_mask, 31);
-	return _mm_slli_epi32(v_mask_one_bit, FDIR_ID_BIT_SHIFT);
+	return _mm_slli_epi32(v_mask_one_bit, I40E_FDIR_ID_BIT_SHIFT);
 }
 #endif
 
Signature · 1 line
2.43.0
#76 lore raw

PATCH v5 5/6 mempool: avoid floating point expression in static assertion

In reply to Stephen Hemminger

Clang does not handle casts in static_assert() expressions. It doesn't like use of floating point to calculate threshold. Use a different expression with same effect.

Modify comment in mlx5 so that developers don't go searching for old value.

---
 drivers/net/mlx5/mlx5_rxq.c | 2 +-
 lib/mempool/rte_mempool.c   | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
drivers/net/mlx5/mlx5_rxq.c +1 -1
index 1bb036afebb3..ca2eeedc9de3 100644
@@ -1444,7 +1444,7 @@ mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
 	/*
 	 * rte_mempool_create_empty() has sanity check to refuse large cache
 	 * size compared to the number of elements.
-	 * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
+	 * CALC_CACHE_FLUSHTHRESH() is defined in a C file, so using a
 	 * constant number 2 instead.
 	 */
 	obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
lib/mempool/rte_mempool.c +4 -3
index b7a19bea7185..12390a2c8155 100644
@@ -50,9 +50,10 @@ static void
 mempool_event_callback_invoke(enum rte_mempool_event event,
 			      struct rte_mempool *mp);
 
-#define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
-#define CALC_CACHE_FLUSHTHRESH(c)	\
-	((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
+/* Note: avoid using floating point since that compiler
+ * may not think that is constant.
+ */
+#define CALC_CACHE_FLUSHTHRESH(c) (((c) * 3) / 2)
 
 #if defined(RTE_ARCH_X86)
 /*
Signature · 1 line
2.43.0
Reviewed-by on v5 5/6
#77 lore raw

RE: [PATCH v5 5/6] mempool: avoid floating point expression in static assertion

Quoted 7 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 18 January 2024 17.51

Clang does not handle casts in static_assert() expressions. It doesn't like use of floating point to calculate threshold. Use a different expression with same effect.

Quoted 7 lines

Modify comment in mlx5 so that developers don't go searching for old value.

---

Acked-by on v5 5/6
#78 lore raw

RE: [PATCH v5 5/6] mempool: avoid floating point expression in static assertion

In reply to Stephen Hemminger
Quoted 17 lines

-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Thursday, January 18, 2024 6:51 PM
To: [email protected]
Cc: Stephen Hemminger <[email protected]>; Konstantin Ananyev <[email protected]>; Andrew Rybchenko <[email protected]>; Dariusz Sosnowski <[email protected]>; Slava Ovsiienko <[email protected]>; Ori Kam <[email protected]>; Suanming Mou <[email protected]>; Matan Azrad <[email protected]>; Morten Brørup <[email protected]>
Subject: [PATCH v5 5/6] mempool: avoid floating point expression in static assertion

Clang does not handle casts in static_assert() expressions. It doesn't like use of floating point to calculate threshold. Use a different expression with same effect.

Quoted 5 lines

Modify comment in mlx5 so that developers don't go searching for old value.

#79 lore raw

PATCH v5 6/6 eal: replace out of bounds VLA with static_assert

In reply to Stephen Hemminger

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

Add workaround for clang static_assert in switch,
and missing static_assert in older FreeBSD.

---
 lib/eal/include/rte_common.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
lib/eal/include/rte_common.h +10 -1
index 33680e818bfb..aa066125b6cd 100644
@@ -16,6 +16,7 @@
 extern "C" {
 #endif
 
+#include <assert.h>
 #include <stdint.h>
 #include <limits.h>
 
@@ -492,10 +493,18 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 
 /*********** Macros for compile type checks ********/
 
+/* Workaround for toolchain issues with missing C11 macro in FreeBSD */
+#if !defined(static_assert) && !defined(__cplusplus)
+#define	static_assert	_Static_assert
+#endif
+
 /**
  * Triggers an error at compilation time if the condition is true.
+ *
+ * The do { } while(0) exists to workaround a bug in clang (#55821)
+ * where it would not handle _Static_assert in a switch case.
  */
-#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
 
 /*********** Cache line related macros ********/
 
Signature · 1 line
2.43.0
Reviewed-by on v5 6/6
#80 lore raw

RE: [PATCH v5 6/6] eal: replace out of bounds VLA with static_assert

Quoted 10 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 18 January 2024 17.51

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

Quoted 7 lines

Add workaround for clang static_assert in switch,
and missing static_assert in older FreeBSD.

---

Acked-by
#81 lore raw

Re: [PATCH v5 6/6] eal: replace out of bounds VLA with static_assert

Quoted 13 lines · Morten Brørup
Quoted 13 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 18 January 2024 17.51

Both Gcc, clang and MSVC have better way to do compile time assertions rather than using out of bounds array access. The old method would fail if -Wvla is enabled because compiler can't determine size in that code. Also, the use of new _Static_assert will catch broken code that is passing non-constant expression to RTE_BUILD_BUG_ON().

Add workaround for clang static_assert in switch,
and missing static_assert in older FreeBSD.

Quoted 7 lines
Quoted 4 lines

---

#82 lore raw

Re: [PATCH v5 0/6] use static_assert for build error reports

In reply to Stephen Hemminger
Quoted 23 lines · Stephen Hemminger

This series fixes a couple places where expressions that could not be evaluated as constant early in compiler passes were used. Then converts RTE_BUILD_BUG_ON() with static_assert.

static_assert() is more picky about the expression has to be a constant, which also catches some existing undefined behavior that pre-existed.

The series requires a couple of workarounds to deal with quirks in static_assert() in some toolchains.

v6 - minor cleanups
handle missing macro in old FreeBSD

Stephen Hemminger (6):
eal: introduce RTE_MIN_T() and RTE_MAX_T() macros event/opdl: fix non-constant compile time assertion net/sfc: fix non-constant expression in RTE_BUILD_BUG_ON() net/i40e: avoid using const variable in assertion
mempool: avoid floating point expression in static assertion
eal: replace out of bounds VLA with static_assert

Quoted 8 lines
 drivers/event/opdl/opdl_ring.c       |  2 +-
 drivers/net/i40e/i40e_ethdev.h       |  1 +
 drivers/net/i40e/i40e_rxtx_vec_sse.c | 10 ++++------
 drivers/net/mlx5/mlx5_rxq.c          |  2 +-
 drivers/net/sfc/sfc_ef100_tx.c       |  3 +--
 lib/eal/include/rte_common.h         | 27 ++++++++++++++++++++++++++-
 lib/mempool/rte_mempool.c            |  7 ++++---
 7 files changed, 38 insertions(+), 14 deletions(-)

Added a small RN and applied, thanks Stephen.

Signature · 1 line
David Marchand