Skip to contentSkip to conversation

mlx4: fix "initializer element not constant" compiler error

PATCH Applied Jacob Keller 3 participants 3 messages Mar 27, 2020 – Mar 30, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mlx4: fix "initializer element not constant" compiler error

A recent commit e8937681797c ("devlink: prepare to support region operations") used the region_cr_space_str and region_fw_health_str variables as initializers for the devlink_region_ops structures.

This can result in compiler errors:
drivers/net/ethernet/mellanox//mlx4/crdump.c:45:10: error: initializer element is not constant
.name = region_cr_space_str,

           ^

drivers/net/ethernet/mellanox//mlx4/crdump.c:45:10: note: (near initialization for ‘region_cr_space_ops.name’) drivers/net/ethernet/mellanox//mlx4/crdump.c:50:10: error: initializer element is not constant
.name = region_fw_health_str,

The variables were made to be "const char * const", indicating that both the pointer and data were constant. This was enough to resolve this on recent GCC (gcc (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1) for this author).

Unfortunately this is not enough for older compilers to realize that the variable can be treated as a constant expression.

Fix this by introducing macros for the string and use those instead of the variable name in the region ops structures.

Fixes e8937681797c ("devlink: prepare to support region operations")
---
 drivers/net/ethernet/mellanox/mlx4/crdump.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/net/ethernet/mellanox/mlx4/crdump.c +7 -4
index 2700628f1689..73eae80e1cb7 100644
@@ -38,16 +38,19 @@
 #define CR_ENABLE_BIT_OFFSET		0xF3F04
 #define MAX_NUM_OF_DUMPS_TO_STORE	(8)
 
-static const char * const region_cr_space_str = "cr-space";
-static const char * const region_fw_health_str = "fw-health";
+#define REGION_CR_SPACE "cr-space"
+#define REGION_FW_HEALTH "fw-health"
+
+static const char * const region_cr_space_str = REGION_CR_SPACE;
+static const char * const region_fw_health_str = REGION_FW_HEALTH;
 
 static const struct devlink_region_ops region_cr_space_ops = {
-	.name = region_cr_space_str,
+	.name = REGION_CR_SPACE,
 	.destructor = &kvfree,
 };
 
 static const struct devlink_region_ops region_fw_health_ops = {
-	.name = region_fw_health_str,
+	.name = REGION_FW_HEALTH,
 	.destructor = &kvfree,
 };
 
Signature · 1 line
2.24.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Jacob Keller

A recent commit e8937681797c ("devlink: prepare to support region operations") used the region_cr_space_str and region_fw_health_str variables as initializers for the devlink_region_ops structures.

This can result in compiler errors:
drivers/net/ethernet/mellanox//mlx4/crdump.c:45:10: error: initializer element is not constant
.name = region_cr_space_str,

           ^

drivers/net/ethernet/mellanox//mlx4/crdump.c:45:10: note: (near initialization for ‘region_cr_space_ops.name’) drivers/net/ethernet/mellanox//mlx4/crdump.c:50:10: error: initializer element is not constant
.name = region_fw_health_str,

The variables were made to be "const char * const", indicating that both the pointer and data were constant. This was enough to resolve this on recent GCC (gcc (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1) for this author).

Unfortunately this is not enough for older compilers to realize that the variable can be treated as a constant expression.

Fix this by introducing macros for the string and use those instead of the variable name in the region ops structures.

Quoted 7 lines
Fixes e8937681797c ("devlink: prepare to support region operations")
---
 drivers/net/ethernet/mellanox/mlx4/crdump.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Thanks,

maintainer Applied
#2 lore raw
In reply to Jacob Keller

From: Jacob Keller <[email protected]>
Date: Fri, 27 Mar 2020 14:08:35 -0700

Quoted 22 lines

A recent commit e8937681797c ("devlink: prepare to support region operations") used the region_cr_space_str and region_fw_health_str variables as initializers for the devlink_region_ops structures.

This can result in compiler errors:
drivers/net/ethernet/mellanox//mlx4/crdump.c:45:10: error: initializer element is not constant
.name = region_cr_space_str,

           ^

drivers/net/ethernet/mellanox//mlx4/crdump.c:45:10: note: (near initialization for ‘region_cr_space_ops.name’) drivers/net/ethernet/mellanox//mlx4/crdump.c:50:10: error: initializer element is not constant
.name = region_fw_health_str,

The variables were made to be "const char * const", indicating that both the pointer and data were constant. This was enough to resolve this on recent GCC (gcc (GCC) 9.2.1 20190827 (Red Hat 9.2.1-1) for this author).

Unfortunately this is not enough for older compilers to realize that the variable can be treated as a constant expression.

Quoted 6 lines

Fix this by introducing macros for the string and use those instead of the variable name in the region ops structures.

Fixes e8937681797c ("devlink: prepare to support region operations")

Applied.