cxl: Align interleave decode/encode helpers with their callers
Alison Schofield
maintainerPATCH 1/1 cxl: Align interleave decode/encode helpers with their callers
The interleave conversion helpers translate between encoded HDM interleave values and the granularity and way values used by the driver.
These helpers have been a recurring source of static analysis complaints that expose type mismatches and potentially uninitialized outputs. Fix those issues in the helpers so callers inherit the consistent behavior automatically.
The decode and encode helpers have different interface issues.
The decode helpers return values through unsigned int pointers, but the decoded values are ultimately represented as int throughout the driver. Align the helper interfaces with their callers by changing the out-parameters to int * and updating the handful of affected locals to match.
The encode helpers leave their out-parameters unchanged on error. That means callers that ignore the return value may observe uninitialized encoded values. Initialize the outputs so failed conversions leave defined values. This issue was originally reported by Purva and the helper-side fix was suggested by Dan [1].
Tidy up a related, pre-existing, printk format specifier mismatch in cxl_validate_translation_params().
No functional change for valid interleave parameters.
[1] https://lore.kernel.org/linux-cxl/[email protected]/
--- drivers/cxl/acpi.c | 10 +++++----- drivers/cxl/core/region.c | 4 ++-- drivers/cxl/cxl.h | 6 ++++-- 3 files changed, 11 insertions(+), 9 deletions(-)
drivers/cxl/acpi.c +5 -5
@@ -101,8 +101,8 @@ static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg,struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;struct device *dev = ctx->dev;struct cxl_cxims_data *cximsd; -unsigned int hbig, nr_maps; -int rc; +unsigned int nr_maps; +int hbig, rc; rc =eig_to_granularity (cxims->hbig, &hbig);if (rc)
@@ -160,7 +160,7 @@ static int cxl_acpi_cfmws_verify(struct device *dev, struct acpi_cedt_cfmws *cfmws) {int rc, expected_len; -unsigned int ways; +int ways;if (cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_MODULO && cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_XOR ) {
@@ -405,7 +405,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,struct cxl_cxims_context cxims_ctx;struct device *dev = ctx->dev;struct cxl_decoder *cxld; -unsigned int ways, i, ig; +int ways, i, ig;int rc; rc =cxl_acpi_cfmws_verify (dev, cfmws);
@@ -464,7 +464,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,if (rc <0 )return rc;if (!cxlrd->platform_data) { -dev_err (dev,"No CXIMS for HBIG %u\n" , ig); +dev_err (dev,"No CXIMS for HBIG %d\n" , ig);return -EINVAL ; } }
drivers/cxl/core/region.c +2 -2
@@ -3060,7 +3060,7 @@ int cxl_validate_translation_params(u8 eiw, u16 eig, int pos)return -EINVAL ; }if (pos <0 || pos >= ways) { -pr_debug ("%s: invalid pos=%d for ways=%u\n" , __func__, pos, +pr_debug ("%s: invalid pos=%d for ways=%d\n" , __func__, pos, ways);return -EINVAL ; }
@@ -3106,7 +3106,7 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_dpa_offset, "cxl_translate");int cxl_calculate_position (u64 hpa_offset,u8 eiw,u16 eig) { -unsigned int ways =0 ; +int ways =0 ;u64 shifted, rem;int pos, ret;
drivers/cxl/cxl.h +4 -2
@@ -91,7 +91,7 @@ static inline int cxl_hdm_decoder_count(u32 cap_hdr) }/* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */ -static inline int eig_to_granularity (u16 eig,unsigned int *granularity) +static inline int eig_to_granularity (u16 eig,int *granularity) {if (eig >CXL_DECODER_MAX_ENCODED_IG )return -EINVAL ;
@@ -100,7 +100,7 @@ static inline int eig_to_granularity(u16 eig, unsigned int *granularity) }/* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */ -static inline int eiw_to_ways (u8 eiw,unsigned int *ways) +static inline int eiw_to_ways (u8 eiw,int *ways) {switch (eiw) { case 0 ... 4:
@@ -118,6 +118,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 ;
@@ -127,6 +128,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)) {
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
Signature · 1 line
2.37.3
Quoted 26 lines · Alison Schofield
The interleave conversion helpers translate between encoded HDM interleave values and the granularity and way values used by the driver.
These helpers have been a recurring source of static analysis complaints that expose type mismatches and potentially uninitialized outputs. Fix those issues in the helpers so callers inherit the consistent behavior automatically.
The decode and encode helpers have different interface issues.
The decode helpers return values through unsigned int pointers, but the decoded values are ultimately represented as int throughout the driver. Align the helper interfaces with their callers by changing the out-parameters to int * and updating the handful of affected locals to match.
The encode helpers leave their out-parameters unchanged on error. That means callers that ignore the return value may observe uninitialized encoded values. Initialize the outputs so failed conversions leave defined values. This issue was originally reported by Purva and the helper-side fix was suggested by Dan [1].
Tidy up a related, pre-existing, printk format specifier mismatch in
cxl_validate_translation_params().
Quoted 7 lines
No functional change for valid interleave parameters.
[1] https://lore.kernel.org/linux-cxl/[email protected]/
Reported-by Purva Yeshi <[email protected]>Suggested-by Dan Williams <[email protected]>Signed-off-by Alison Schofield <[email protected]>
Quoted 110 lines
--- drivers/cxl/acpi.c | 10 +++++----- drivers/cxl/core/region.c | 4 ++-- drivers/cxl/cxl.h | 6 ++++-- 3 files changed, 11 insertions(+), 9 deletions(-)drivers/cxl/acpi.c +5
-5@@ -101,8 +101,8 @@ static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg,struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;struct device *dev = ctx->dev;struct cxl_cxims_data *cximsd; -unsigned int hbig, nr_maps; -int rc; +unsigned int nr_maps; +int hbig, rc; rc =eig_to_granularity (cxims->hbig, &hbig);if (rc)@@ -160,7 +160,7 @@ static int cxl_acpi_cfmws_verify(struct device *dev, struct acpi_cedt_cfmws *cfmws) {int rc, expected_len; -unsigned int ways; +int ways;if (cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_MODULO && cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_XOR ) {@@ -405,7 +405,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,struct cxl_cxims_context cxims_ctx;struct device *dev = ctx->dev;struct cxl_decoder *cxld; -unsigned int ways, i, ig; +int ways, i, ig;int rc; rc =cxl_acpi_cfmws_verify (dev, cfmws);@@ -464,7 +464,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,if (rc <0 )return rc;if (!cxlrd->platform_data) { -dev_err (dev,"No CXIMS for HBIG %u\n" , ig); +dev_err (dev,"No CXIMS for HBIG %d\n" , ig);return -EINVAL ; } }drivers/cxl/core/region.c +2
-2@@ -3060,7 +3060,7 @@ int cxl_validate_translation_params(u8 eiw, u16 eig, int pos)return -EINVAL ; }if (pos <0 || pos >= ways) { -pr_debug ("%s: invalid pos=%d for ways=%u\n" , __func__, pos, +pr_debug ("%s: invalid pos=%d for ways=%d\n" , __func__, pos, ways);return -EINVAL ; }@@ -3106,7 +3106,7 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_dpa_offset, "cxl_translate");int cxl_calculate_position (u64 hpa_offset,u8 eiw,u16 eig) { -unsigned int ways =0 ; +int ways =0 ;u64 shifted, rem;int pos, ret;drivers/cxl/cxl.h +4
-2@@ -91,7 +91,7 @@ static inline int cxl_hdm_decoder_count(u32 cap_hdr) }/* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */ -static inline int eig_to_granularity (u16 eig,unsigned int *granularity) +static inline int eig_to_granularity (u16 eig,int *granularity) {if (eig >CXL_DECODER_MAX_ENCODED_IG )return -EINVAL ;@@ -100,7 +100,7 @@ static inline int eig_to_granularity(u16 eig, unsigned int *granularity) }/* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */ -static inline int eiw_to_ways (u8 eiw,unsigned int *ways) +static inline int eiw_to_ways (u8 eiw,int *ways) {switch (eiw) { case 0 ... 4:@@ -118,6 +118,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 ;@@ -127,6 +128,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)) {base-commit:
254f49634ee16a731174d2ae34bc50bd5f45e731
Quoted 26 lines · Alison Schofield
The interleave conversion helpers translate between encoded HDM interleave values and the granularity and way values used by the driver.
These helpers have been a recurring source of static analysis complaints that expose type mismatches and potentially uninitialized outputs. Fix those issues in the helpers so callers inherit the consistent behavior automatically.
The decode and encode helpers have different interface issues.
The decode helpers return values through unsigned int pointers, but the decoded values are ultimately represented as int throughout the driver. Align the helper interfaces with their callers by changing the out-parameters to int * and updating the handful of affected locals to match.
The encode helpers leave their out-parameters unchanged on error. That means callers that ignore the return value may observe uninitialized encoded values. Initialize the outputs so failed conversions leave defined values. This issue was originally reported by Purva and the helper-side fix was suggested by Dan [1].
Tidy up a related, pre-existing, printk format specifier mismatch in
cxl_validate_translation_params().
Quoted 7 lines
No functional change for valid interleave parameters.
[1] https://lore.kernel.org/linux-cxl/[email protected]/
Reported-by Purva Yeshi <[email protected]>Suggested-by Dan Williams <[email protected]>Signed-off-by Alison Schofield <[email protected]>
Since the types are changed from 'unsigned int' to 'int', maybe we should drop the second parameter of these helpers in the future?
A negative return value indicates failure, other value is valid.
Quoted 110 lines
--- drivers/cxl/acpi.c | 10 +++++----- drivers/cxl/core/region.c | 4 ++-- drivers/cxl/cxl.h | 6 ++++-- 3 files changed, 11 insertions(+), 9 deletions(-)drivers/cxl/acpi.c +5
-5@@ -101,8 +101,8 @@ static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg,struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;struct device *dev = ctx->dev;struct cxl_cxims_data *cximsd; -unsigned int hbig, nr_maps; -int rc; +unsigned int nr_maps; +int hbig, rc; rc =eig_to_granularity (cxims->hbig, &hbig);if (rc)@@ -160,7 +160,7 @@ static int cxl_acpi_cfmws_verify(struct device *dev, struct acpi_cedt_cfmws *cfmws) {int rc, expected_len; -unsigned int ways; +int ways;if (cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_MODULO && cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_XOR ) {@@ -405,7 +405,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,struct cxl_cxims_context cxims_ctx;struct device *dev = ctx->dev;struct cxl_decoder *cxld; -unsigned int ways, i, ig; +int ways, i, ig;int rc; rc =cxl_acpi_cfmws_verify (dev, cfmws);@@ -464,7 +464,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,if (rc <0 )return rc;if (!cxlrd->platform_data) { -dev_err (dev,"No CXIMS for HBIG %u\n" , ig); +dev_err (dev,"No CXIMS for HBIG %d\n" , ig);return -EINVAL ; } }drivers/cxl/core/region.c +2
-2@@ -3060,7 +3060,7 @@ int cxl_validate_translation_params(u8 eiw, u16 eig, int pos)return -EINVAL ; }if (pos <0 || pos >= ways) { -pr_debug ("%s: invalid pos=%d for ways=%u\n" , __func__, pos, +pr_debug ("%s: invalid pos=%d for ways=%d\n" , __func__, pos, ways);return -EINVAL ; }@@ -3106,7 +3106,7 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_dpa_offset, "cxl_translate");int cxl_calculate_position (u64 hpa_offset,u8 eiw,u16 eig) { -unsigned int ways =0 ; +int ways =0 ;u64 shifted, rem;int pos, ret;drivers/cxl/cxl.h +4
-2@@ -91,7 +91,7 @@ static inline int cxl_hdm_decoder_count(u32 cap_hdr) }/* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */ -static inline int eig_to_granularity (u16 eig,unsigned int *granularity) +static inline int eig_to_granularity (u16 eig,int *granularity) {if (eig >CXL_DECODER_MAX_ENCODED_IG )return -EINVAL ;@@ -100,7 +100,7 @@ static inline int eig_to_granularity(u16 eig, unsigned int *granularity) }/* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */ -static inline int eiw_to_ways (u8 eiw,unsigned int *ways) +static inline int eiw_to_ways (u8 eiw,int *ways) {switch (eiw) { case 0 ... 4:@@ -118,6 +118,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 ;@@ -127,6 +128,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)) {base-commit:
254f49634ee16a731174d2ae34bc50bd5f45e731
Alison Schofield
maintainerQuoted 36 lines · Li Ming
Quoted 26 lines · Alison Schofield
The interleave conversion helpers translate between encoded HDM interleave values and the granularity and way values used by the driver.
These helpers have been a recurring source of static analysis complaints that expose type mismatches and potentially uninitialized outputs. Fix those issues in the helpers so callers inherit the consistent behavior automatically.
The decode and encode helpers have different interface issues.
The decode helpers return values through unsigned int pointers, but the decoded values are ultimately represented as int throughout the driver. Align the helper interfaces with their callers by changing the out-parameters to int * and updating the handful of affected locals to match.
The encode helpers leave their out-parameters unchanged on error. That means callers that ignore the return value may observe uninitialized encoded values. Initialize the outputs so failed conversions leave defined values. This issue was originally reported by Purva and the helper-side fix was suggested by Dan [1].
Tidy up a related, pre-existing, printk format specifier mismatch in
cxl_validate_translation_params().Quoted 7 lines
No functional change for valid interleave parameters.
[1] https://lore.kernel.org/linux-cxl/[email protected]/
Reported-by Purva Yeshi <[email protected]>Suggested-by Dan Williams <[email protected]>Signed-off-by Alison Schofield <[email protected]>
Quoted 6 lines
Reviewed-by Li Ming <[email protected]>Since the types are changed from 'unsigned int' to 'int', maybe we should drop the second parameter of these helpers in the future?
A negative return value indicates failure, other value is valid.
Hi Ming,
Good catch and good idea. Thanks!
I'd like to address it now, probably turning this into a 2 patch series.
Not exactly as you suggested, see if you agree w this:
I'll convert the two decode helpers eig_to_granularity() and eiw_to_ways(), to return the decoded value directly, while keeping out params for the encode helpers ways_to_eiw(), granularity_to_eig().
Decode converts cleanly: results are small positives, so a negative return is clear, and no caller is made worse. ie ones that must check already do.
Encode I'd leave as is. Those helpers run on two kinds of input: raw sysfs values, where the return value must be checked, and already-validated values, where the return code is legitimately ignored. Keeping the output param, together with the *eiw = 0 / *eig = 0 initialization this patch adds, lets both kinds of caller share the encode helpers.
I'll go ahead and code it up so you can see if you like it when you see it.
-- Alison
Quoted 102 lines
Quoted 102 lines
--- drivers/cxl/acpi.c | 10 +++++----- drivers/cxl/core/region.c | 4 ++-- drivers/cxl/cxl.h | 6 ++++-- 3 files changed, 11 insertions(+), 9 deletions(-)drivers/cxl/acpi.c +5
-5@@ -101,8 +101,8 @@ static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg,struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;struct device *dev = ctx->dev;struct cxl_cxims_data *cximsd; -unsigned int hbig, nr_maps; -int rc; +unsigned int nr_maps; +int hbig, rc; rc =eig_to_granularity (cxims->hbig, &hbig);if (rc)@@ -160,7 +160,7 @@ static int cxl_acpi_cfmws_verify(struct device *dev, struct acpi_cedt_cfmws *cfmws) {int rc, expected_len; -unsigned int ways; +int ways;if (cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_MODULO && cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_XOR ) {@@ -405,7 +405,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,struct cxl_cxims_context cxims_ctx;struct device *dev = ctx->dev;struct cxl_decoder *cxld; -unsigned int ways, i, ig; +int ways, i, ig;int rc; rc =cxl_acpi_cfmws_verify (dev, cfmws);@@ -464,7 +464,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,if (rc <0 )return rc;if (!cxlrd->platform_data) { -dev_err (dev,"No CXIMS for HBIG %u\n" , ig); +dev_err (dev,"No CXIMS for HBIG %d\n" , ig);return -EINVAL ; } }drivers/cxl/core/region.c +2
-2@@ -3060,7 +3060,7 @@ int cxl_validate_translation_params(u8 eiw, u16 eig, int pos)return -EINVAL ; }if (pos <0 || pos >= ways) { -pr_debug ("%s: invalid pos=%d for ways=%u\n" , __func__, pos, +pr_debug ("%s: invalid pos=%d for ways=%d\n" , __func__, pos, ways);return -EINVAL ; }@@ -3106,7 +3106,7 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_dpa_offset, "cxl_translate");int cxl_calculate_position (u64 hpa_offset,u8 eiw,u16 eig) { -unsigned int ways =0 ; +int ways =0 ;u64 shifted, rem;int pos, ret;drivers/cxl/cxl.h +4
-2@@ -91,7 +91,7 @@ static inline int cxl_hdm_decoder_count(u32 cap_hdr) }/* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */ -static inline int eig_to_granularity (u16 eig,unsigned int *granularity) +static inline int eig_to_granularity (u16 eig,int *granularity) {if (eig >CXL_DECODER_MAX_ENCODED_IG )return -EINVAL ;@@ -100,7 +100,7 @@ static inline int eig_to_granularity(u16 eig, unsigned int *granularity) }/* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */ -static inline int eiw_to_ways (u8 eiw,unsigned int *ways) +static inline int eiw_to_ways (u8 eiw,int *ways) {switch (eiw) { case 0 ... 4:@@ -118,6 +118,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 ;@@ -127,6 +128,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)) {base-commit:
254f49634ee16a731174d2ae34bc50bd5f45e731
Li Ming
reviewerQuoted 55 lines · Alison Schofield
Quoted 34 lines · Li Ming
Quoted 26 lines · Alison Schofield
The interleave conversion helpers translate between encoded HDM interleave values and the granularity and way values used by the driver.
These helpers have been a recurring source of static analysis complaints that expose type mismatches and potentially uninitialized outputs. Fix those issues in the helpers so callers inherit the consistent behavior automatically.
The decode and encode helpers have different interface issues.
The decode helpers return values through unsigned int pointers, but the decoded values are ultimately represented as int throughout the driver. Align the helper interfaces with their callers by changing the out-parameters to int * and updating the handful of affected locals to match.
The encode helpers leave their out-parameters unchanged on error. That means callers that ignore the return value may observe uninitialized encoded values. Initialize the outputs so failed conversions leave defined values. This issue was originally reported by Purva and the helper-side fix was suggested by Dan [1].
Tidy up a related, pre-existing, printk format specifier mismatch in
cxl_validate_translation_params().Quoted 7 lines
No functional change for valid interleave parameters.
[1] https://lore.kernel.org/linux-cxl/[email protected]/
Reported-by Purva Yeshi <[email protected]>Suggested-by Dan Williams <[email protected]>Signed-off-by Alison Schofield <[email protected]>Quoted 6 lines
Reviewed-by Li Ming <[email protected]>Since the types are changed from 'unsigned int' to 'int', maybe we should drop the second parameter of these helpers in the future?
A negative return value indicates failure, other value is valid.
Hi Ming,
Good catch and good idea. Thanks!
I'd like to address it now, probably turning this into a 2 patch series.Not exactly as you suggested, see if you agree w this:
I'll convert the two decode helpers
eig_to_granularity()andeiw_to_ways(), to return the decoded value directly, while keeping out params for the encode helpersways_to_eiw(),granularity_to_eig().Decode converts cleanly: results are small positives, so a negative return is clear, and no caller is made worse. ie ones that must check already do.
Quoted 8 lines
Encode I'd leave as is. Those helpers run on two kinds of input: raw sysfs values, where the return value must be checked, and already-validated values, where the return code is legitimately ignored. Keeping the output param, together with the *eiw = 0 / *eig = 0 initialization this patch adds, lets both kinds of caller share the encode helpers.
I'll go ahead and code it up so you can see if you like it when you see it.
Hi Alison,
These changes look good to me. Thanks.
Ming
Quoted 3 lines
-- Alison
Alison Schofield
maintainerQuoted 62 lines · Li Ming
Quoted 61 lines · Alison Schofield
Quoted 34 lines · Li Ming
Quoted 26 lines · Alison Schofield
The interleave conversion helpers translate between encoded HDM interleave values and the granularity and way values used by the driver.
These helpers have been a recurring source of static analysis complaints that expose type mismatches and potentially uninitialized outputs. Fix those issues in the helpers so callers inherit the consistent behavior automatically.
The decode and encode helpers have different interface issues.
The decode helpers return values through unsigned int pointers, but the decoded values are ultimately represented as int throughout the driver. Align the helper interfaces with their callers by changing the out-parameters to int * and updating the handful of affected locals to match.
The encode helpers leave their out-parameters unchanged on error. That means callers that ignore the return value may observe uninitialized encoded values. Initialize the outputs so failed conversions leave defined values. This issue was originally reported by Purva and the helper-side fix was suggested by Dan [1].
Tidy up a related, pre-existing, printk format specifier mismatch in
cxl_validate_translation_params().Quoted 7 lines
No functional change for valid interleave parameters.
[1] https://lore.kernel.org/linux-cxl/[email protected]/
Reported-by Purva Yeshi <[email protected]>Suggested-by Dan Williams <[email protected]>Signed-off-by Alison Schofield <[email protected]>Quoted 6 lines
Reviewed-by Li Ming <[email protected]>Since the types are changed from 'unsigned int' to 'int', maybe we should drop the second parameter of these helpers in the future?
A negative return value indicates failure, other value is valid.
Hi Ming,
Good catch and good idea. Thanks!
I'd like to address it now, probably turning this into a 2 patch series.Not exactly as you suggested, see if you agree w this:
I'll convert the two decode helpers
eig_to_granularity()andeiw_to_ways(), to return the decoded value directly, while keeping out params for the encode helpersways_to_eiw(),granularity_to_eig().Decode converts cleanly: results are small positives, so a negative return is clear, and no caller is made worse. ie ones that must check already do.
Encode I'd leave as is. Those helpers run on two kinds of input: raw sysfs values, where the return value must be checked, and already-validated values, where the return code is legitimately ignored. Keeping the output param, together with the *eiw = 0 / *eig = 0 initialization this patch adds, lets both kinds of caller share the encode helpers.
Quoted 6 lines
Quoted 2 lines
I'll go ahead and code it up so you can see if you like it when you see it.
Hi Alison,
These changes look good to me. Thanks.
Dave's applied this patch for the 7.2 merge window, and that does fix reporters issue, so forget about my mention of a 2 patch series.
I'll just come back around with a second change for the next merge window.
-- Alison
Quoted 8 lines
Ming
Quoted 3 lines
-- Alison
Quoted 26 lines · Alison Schofield
The interleave conversion helpers translate between encoded HDM interleave values and the granularity and way values used by the driver.
These helpers have been a recurring source of static analysis complaints that expose type mismatches and potentially uninitialized outputs. Fix those issues in the helpers so callers inherit the consistent behavior automatically.
The decode and encode helpers have different interface issues.
The decode helpers return values through unsigned int pointers, but the decoded values are ultimately represented as int throughout the driver. Align the helper interfaces with their callers by changing the out-parameters to int * and updating the handful of affected locals to match.
The encode helpers leave their out-parameters unchanged on error. That means callers that ignore the return value may observe uninitialized encoded values. Initialize the outputs so failed conversions leave defined values. This issue was originally reported by Purva and the helper-side fix was suggested by Dan [1].
Tidy up a related, pre-existing, printk format specifier mismatch in
cxl_validate_translation_params().
Quoted 7 lines
No functional change for valid interleave parameters.
[1] https://lore.kernel.org/linux-cxl/[email protected]/
Reported-by Purva Yeshi <[email protected]>Suggested-by Dan Williams <[email protected]>Signed-off-by Alison Schofield <[email protected]>
Applied to cxl/next661c092f9839
Quoted 110 lines
--- drivers/cxl/acpi.c | 10 +++++----- drivers/cxl/core/region.c | 4 ++-- drivers/cxl/cxl.h | 6 ++++-- 3 files changed, 11 insertions(+), 9 deletions(-)drivers/cxl/acpi.c +5
-5@@ -101,8 +101,8 @@ static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg,struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;struct device *dev = ctx->dev;struct cxl_cxims_data *cximsd; -unsigned int hbig, nr_maps; -int rc; +unsigned int nr_maps; +int hbig, rc; rc =eig_to_granularity (cxims->hbig, &hbig);if (rc)@@ -160,7 +160,7 @@ static int cxl_acpi_cfmws_verify(struct device *dev, struct acpi_cedt_cfmws *cfmws) {int rc, expected_len; -unsigned int ways; +int ways;if (cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_MODULO && cfmws->interleave_arithmetic !=ACPI_CEDT_CFMWS_ARITHMETIC_XOR ) {@@ -405,7 +405,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,struct cxl_cxims_context cxims_ctx;struct device *dev = ctx->dev;struct cxl_decoder *cxld; -unsigned int ways, i, ig; +int ways, i, ig;int rc; rc =cxl_acpi_cfmws_verify (dev, cfmws);@@ -464,7 +464,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,if (rc <0 )return rc;if (!cxlrd->platform_data) { -dev_err (dev,"No CXIMS for HBIG %u\n" , ig); +dev_err (dev,"No CXIMS for HBIG %d\n" , ig);return -EINVAL ; } }drivers/cxl/core/region.c +2
-2@@ -3060,7 +3060,7 @@ int cxl_validate_translation_params(u8 eiw, u16 eig, int pos)return -EINVAL ; }if (pos <0 || pos >= ways) { -pr_debug ("%s: invalid pos=%d for ways=%u\n" , __func__, pos, +pr_debug ("%s: invalid pos=%d for ways=%d\n" , __func__, pos, ways);return -EINVAL ; }@@ -3106,7 +3106,7 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_dpa_offset, "cxl_translate");int cxl_calculate_position (u64 hpa_offset,u8 eiw,u16 eig) { -unsigned int ways =0 ; +int ways =0 ;u64 shifted, rem;int pos, ret;drivers/cxl/cxl.h +4
-2@@ -91,7 +91,7 @@ static inline int cxl_hdm_decoder_count(u32 cap_hdr) }/* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */ -static inline int eig_to_granularity (u16 eig,unsigned int *granularity) +static inline int eig_to_granularity (u16 eig,int *granularity) {if (eig >CXL_DECODER_MAX_ENCODED_IG )return -EINVAL ;@@ -100,7 +100,7 @@ static inline int eig_to_granularity(u16 eig, unsigned int *granularity) }/* Encode defined in CXL ECN "3, 6, 12 and 16-way memory Interleaving" */ -static inline int eiw_to_ways (u8 eiw,unsigned int *ways) +static inline int eiw_to_ways (u8 eiw,int *ways) {switch (eiw) { case 0 ... 4:@@ -118,6 +118,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 ;@@ -127,6 +128,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)) {base-commit:
254f49634ee16a731174d2ae34bc50bd5f45e731