Skip to contentSkip to conversation

cxl: core: Initialize eiw and eig to fix potential uninitialized use

PATCH In review Purva Yeshi 5 participants 6 messages Apr 19, 2025 – May 13, 2025 CXL

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxl: core: Initialize eiw and eig to fix potential uninitialized use

Fix Smatch-detected issue:

drivers/cxl/core/region.c:1292 check_interleave_cap()
error: uninitialized symbol 'eiw'. drivers/cxl/core/region.c:1297 check_interleave_cap()
error: uninitialized symbol 'eig'. drivers/cxl/core/region.c:1299 check_interleave_cap()
error: uninitialized symbol 'eig'.

Smatch reports possible uninitialized usage of these variables if the helper functions ways_to_eiw() or granularity_to_eig() fail to assign values under certain conditions.

Initialize the eiw and eig variables to zero when they are declared in check_interleave_cap() to prevent potential use of uninitialized values.

---
 drivers/cxl/core/region.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +2 -2
index c3f4dc244df7..edf8636f190a 100644
@@ -1266,8 +1266,8 @@ static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
 	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
 	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
 	unsigned int interleave_mask;
-	u8 eiw;
-	u16 eig;
+	u8 eiw = 0;
+	u16 eig = 0;
 	int high_pos, low_pos;
 
 	if (!test_bit(iw, &cxlhdm->iw_cap_mask))
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 9 lines · Purva Yeshi

Fix Smatch-detected issue:

drivers/cxl/core/region.c:1292 check_interleave_cap()
error: uninitialized symbol 'eiw'. drivers/cxl/core/region.c:1297 check_interleave_cap()
error: uninitialized symbol 'eig'. drivers/cxl/core/region.c:1299 check_interleave_cap()
error: uninitialized symbol 'eig'.

Quoted 7 lines

Smatch reports possible uninitialized usage of these variables if the helper functions ways_to_eiw() or granularity_to_eig() fail to assign values under certain conditions.

Initialize the eiw and eig variables to zero when they are declared in check_interleave_cap() to prevent potential use of uninitialized values.

I also met them during checking cxl drivers code with code inspection tools. But they are not real issues, because both iw and ig are checked before calling check_interleave_cap(). That means check_interleave_cap() will always get a eiw and a eig correctly.

they are checked in cxl_port_setup_targets() in auto-assembly case, otherwise checked in interleave_ways_store() and interleave_guranularity_store().

I am not sure if we need this patch, let's see if other reviewers have more comments.

Ming

Quoted 21 lines
---
 drivers/cxl/core/region.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +2 -2
index c3f4dc244df7..edf8636f190a 100644
@@ -1266,8 +1266,8 @@ static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
 	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
 	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
 	unsigned int interleave_mask;
-	u8 eiw;
-	u16 eig;
+	u8 eiw = 0;
+	u16 eig = 0;
 	int high_pos, low_pos;
 
 	if (!test_bit(iw, &cxlhdm->iw_cap_mask))
#2 lore raw
Quoted 14 lines · Li Ming
Quoted 13 lines · Purva Yeshi

Fix Smatch-detected issue:

drivers/cxl/core/region.c:1292 check_interleave_cap()
error: uninitialized symbol 'eiw'. drivers/cxl/core/region.c:1297 check_interleave_cap()
error: uninitialized symbol 'eig'. drivers/cxl/core/region.c:1299 check_interleave_cap()
error: uninitialized symbol 'eig'.

Smatch reports possible uninitialized usage of these variables if the helper functions ways_to_eiw() or granularity_to_eig() fail to assign values under certain conditions.

Quoted 8 lines
Quoted 3 lines

Initialize the eiw and eig variables to zero when they are declared in check_interleave_cap() to prevent potential use of uninitialized values.

I also met them during checking cxl drivers code with code inspection tools. But they are not real issues, because both iw and ig are checked before calling check_interleave_cap(). That means check_interleave_cap() will always get a eiw and a eig correctly.

they are checked in cxl_port_setup_targets() in auto-assembly case, otherwise checked in interleave_ways_store() and interleave_guranularity_store().

unlikely, but things can change and if we know about this now we should just fix it to avoid someone tripping over it in the future

~Gregory

Reviewed-by on v1 1/1
#3 lore raw
In reply to Purva Yeshi
Quoted 13 lines · Purva Yeshi

Fix Smatch-detected issue:

drivers/cxl/core/region.c:1292 check_interleave_cap()
error: uninitialized symbol 'eiw'. drivers/cxl/core/region.c:1297 check_interleave_cap()
error: uninitialized symbol 'eig'. drivers/cxl/core/region.c:1299 check_interleave_cap()
error: uninitialized symbol 'eig'.

Smatch reports possible uninitialized usage of these variables if the helper functions ways_to_eiw() or granularity_to_eig() fail to assign values under certain conditions.

Quoted 5 lines

Initialize the eiw and eig variables to zero when they are declared in check_interleave_cap() to prevent potential use of uninitialized values.

Quoted 22 lines
---
 drivers/cxl/core/region.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +2 -2
index c3f4dc244df7..edf8636f190a 100644
@@ -1266,8 +1266,8 @@ static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
 	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
 	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
 	unsigned int interleave_mask;
-	u8 eiw;
-	u16 eig;
+	u8 eiw = 0;
+	u16 eig = 0;
 	int high_pos, low_pos;
 
 	if (!test_bit(iw, &cxlhdm->iw_cap_mask))
Signature · 1 line
2.34.1
#4 lore raw
In reply to Purva Yeshi
Quoted 27 lines · Purva Yeshi

Fix Smatch-detected issue:

drivers/cxl/core/region.c:1292 check_interleave_cap()
error: uninitialized symbol 'eiw'. drivers/cxl/core/region.c:1297 check_interleave_cap()
error: uninitialized symbol 'eig'. drivers/cxl/core/region.c:1299 check_interleave_cap()
error: uninitialized symbol 'eig'.

Smatch reports possible uninitialized usage of these variables if the helper functions ways_to_eiw() or granularity_to_eig() fail to assign values under certain conditions.

Initialize the eiw and eig variables to zero when they are declared in check_interleave_cap() to prevent potential use of uninitialized values.

---
 drivers/cxl/core/region.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +0 -0
index c3f4dc244df7..edf8636f190a 100644

drivers/cxl/core/region.c static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig) · line 1270

Quoted 7 lines
drivers/cxl/core/region.c +2 -2
@@ -1266,8 +1266,8 @@ static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
 	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
 	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
 	unsigned int interleave_mask;
-	u8 eiw;
-	u16 eig;
+	u8 eiw = 0;
+	u16 eig = 0;

I appreciate that this quiets the warning, but it is leaving a trip hazard for future code refactoring. I would prefer to make this scenario harder to repeat in the future with something like:

drivers/cxl/cxl.h +2 -0
index be8a7dc77719..5f20919207ae 100644
@@ -107,6 +107,7 @@ static inline int eiw_to_ways(u8 eiw, unsigned int *ways)
 
 static inline int granularity_to_eig(int granularity, u16 *eig)
 {
+	*eig = 0;
 	if (granularity > SZ_16K || granularity < CXL_DECODER_MIN_GRANULARITY ||
 	    !is_power_of_2(granularity))
 		return -EINVAL;
@@ -116,6 +117,7 @@ static inline int granularity_to_eig(int granularity, u16 *eig)
 
 static inline int ways_to_eiw(unsigned int ways, u8 *eiw)
 {
+	*eiw = 0;
 	if (ways > 16)
 		return -EINVAL;
 	if (is_power_of_2(ways)) {
maintainer
#5 lore raw
Quoted 10 lines · Dan Williams
Quoted 9 lines · Purva Yeshi

Fix Smatch-detected issue:

drivers/cxl/core/region.c:1292 check_interleave_cap()
error: uninitialized symbol 'eiw'. drivers/cxl/core/region.c:1297 check_interleave_cap()
error: uninitialized symbol 'eig'. drivers/cxl/core/region.c:1299 check_interleave_cap()
error: uninitialized symbol 'eig'.

Quoted 8 lines
Quoted 8 lines

Smatch reports possible uninitialized usage of these variables if the helper functions ways_to_eiw() or granularity_to_eig() fail to assign values under certain conditions.

Initialize the eiw and eig variables to zero when they are declared in check_interleave_cap() to prevent potential use of uninitialized values.

Hi Purva,

We'd like to see this patch revised and resubmitted. It is a good cleanup!

Go ahead and send a v2 with Dan's suggestion as the solution. There's a couple of options for the tags. Here I think it is good to add this tag:

followed by your Signed-off-by tag.

Quoted 2 lines
Quoted 2 lines

---

You'll add a changelog in v2 here, below the ---, something like:
Changes in v2:
- init eiw and eig in their helper functions (Dan)

Thanks,
Alison

Quoted 40 lines
Quoted 8 lines
 drivers/cxl/core/region.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/cxl/core/region.c +0 -0
index c3f4dc244df7..edf8636f190a 100644

drivers/cxl/core/region.c static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig) · line 1270

Quoted 7 lines
drivers/cxl/core/region.c +2 -2
@@ -1266,8 +1266,8 @@ static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
 	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
 	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
 	unsigned int interleave_mask;
-	u8 eiw;
-	u16 eig;
+	u8 eiw = 0;
+	u16 eig = 0;

I appreciate that this quiets the warning, but it is leaving a trip hazard for future code refactoring. I would prefer to make this scenario harder to repeat in the future with something like:

drivers/cxl/cxl.h +2 -0
index be8a7dc77719..5f20919207ae 100644
@@ -107,6 +107,7 @@ static inline int eiw_to_ways(u8 eiw, unsigned int *ways)
 
 static inline int granularity_to_eig(int granularity, u16 *eig)
 {
+	*eig = 0;
 	if (granularity > SZ_16K || granularity < CXL_DECODER_MIN_GRANULARITY ||
 	    !is_power_of_2(granularity))
 		return -EINVAL;
@@ -116,6 +117,7 @@ static inline int granularity_to_eig(int granularity, u16 *eig)
 
 static inline int ways_to_eiw(unsigned int ways, u8 *eiw)
 {
+	*eiw = 0;
 	if (ways > 16)
 		return -EINVAL;
 	if (is_power_of_2(ways)) {