Skip to contentSkip to conversation

mm, cma: fix 32-bit warning

PATCH In review Arnd Bergmann 4 participants 8 messages Feb 24, 2025

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

PATCH 1/2 mm, cma: fix 32-bit warning

From: Arnd Bergmann <[email protected]>

clang warns about certain always-true conditions, like this one on 32-bit builds:

mm/cma.c:420:13: error: result of comparison of constant 4294967296 with expression of type 'phys_addr_t' (aka 'unsigned int') is always true [-Werror,-Wtautological-constant-out-of-range-compare]
420 | if (start < SZ_4G)

      |                     ~~~~~ ^ ~~~~~

Replace this one with an equivalent expression that does not cause a warning.

Fixes 4765deffa0f7 ("mm, cma: support multiple contiguous ranges, if requested")
---
 mm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/cma.c +1 -1
index 34a4df29af72..ef0206c0f16d 100644
@@ -417,7 +417,7 @@ int __init cma_declare_contiguous_multi(phys_addr_t total_size,
 	 * Create a list of ranges above 4G, largest range first.
 	 */
 	for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &start, &end, NULL) {
-		if (start < SZ_4G)
+		if (upper_32_bits(start) == 0)
 			continue;
 
 		start = ALIGN(start, align);
Signature · 1 line
2.39.5
#1 lore raw

PATCH 2/2 mm, cma: use literal printf format string

From: Arnd Bergmann <[email protected]>

Using a variable string as a printf format can be a security issue that clang warns about when extra warnings are enabled:

mm/cma.c:239:37: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
239 | snprintf(cma->name, CMA_MAX_NAME, name);
| ^~~~

This one does not appear to be a security issue since the string is not user controlled, but it's better to avoid the warning. Use "%s" as the format instead and just pass the name as the argument.

---
 mm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/cma.c +1 -1
index ef0206c0f16d..09322b8284bd 100644
@@ -236,7 +236,7 @@ static int __init cma_new_area(const char *name, phys_addr_t size,
 	cma_area_count++;
 
 	if (name)
-		snprintf(cma->name, CMA_MAX_NAME, name);
+		snprintf(cma->name, CMA_MAX_NAME, "%s", name);
 	else
 		snprintf(cma->name, CMA_MAX_NAME,  "cma%d\n", cma_area_count);
 
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 2/2] mm, cma: use literal printf format string

Quoted 9 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Using a variable string as a printf format can be a security issue that clang warns about when extra warnings are enabled:

mm/cma.c:239:37: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
239 | snprintf(cma->name, CMA_MAX_NAME, name);
| ^~~~

Quoted 9 lines

This one does not appear to be a security issue since the string is not user controlled, but it's better to avoid the warning. Use "%s" as the format instead and just pass the name as the argument.

---
 mm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

LGTM. Reviewed-by: Zi Yan <[email protected]>

Best Regards,
Yan, Zi

Acked-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] mm, cma: use literal printf format string

In reply to Arnd Bergmann
Quoted 23 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Using a variable string as a printf format can be a security issue that clang warns about when extra warnings are enabled:

mm/cma.c:239:37: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
239 | snprintf(cma->name, CMA_MAX_NAME, name);
| ^~~~

This one does not appear to be a security issue since the string is not user controlled, but it's better to avoid the warning. Use "%s" as the format instead and just pass the name as the argument.

---
  mm/cma.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/cma.c +0 -0
index ef0206c0f16d..09322b8284bd 100644

mm/cma.c static int __init cma_new_area(const char *name, phys_addr_t size, · line 242

Quoted 8 lines
mm/cma.c +1 -1
@@ -236,7 +236,7 @@ static int __init cma_new_area(const char *name, phys_addr_t size,
  	cma_area_count++;
 
  	if (name)
-		snprintf(cma->name, CMA_MAX_NAME, name);
+		snprintf(cma->name, CMA_MAX_NAME, "%s", name);
  	else
  		snprintf(cma->name, CMA_MAX_NAME,  "cma%d\n", cma_area_count);
 
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] mm, cma: use literal printf format string

In reply to Arnd Bergmann
Quoted 33 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Using a variable string as a printf format can be a security issue that clang warns about when extra warnings are enabled:

mm/cma.c:239:37: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
239 | snprintf(cma->name, CMA_MAX_NAME, name);
| ^~~~

This one does not appear to be a security issue since the string is not user controlled, but it's better to avoid the warning. Use "%s" as the format instead and just pass the name as the argument.

---
 mm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/cma.c +1 -2
index ef0206c0f16d..09322b8284bd 100644
@@ -236,7 +236,7 @@ static int __init cma_new_area(const char *name, phys_addr_t size,
        cma_area_count++;
 
        if (name)
-               snprintf(cma->name, CMA_MAX_NAME, name);
+               snprintf(cma->name, CMA_MAX_NAME, "%s", name);
        else
                snprintf(cma->name, CMA_MAX_NAME,  "cma%d\n", cma_area_count);
 
--
Quoted 2 lines

2.39.5

Yes, thanks - not sure why I didn't use "%s" there.

#5 lore raw
In reply to Arnd Bergmann
Quoted 9 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

clang warns about certain always-true conditions, like this one on 32-bit builds:

mm/cma.c:420:13: error: result of comparison of constant 4294967296 with expression of type 'phys_addr_t' (aka 'unsigned int') is always true [-Werror,-Wtautological-constant-out-of-range-compare]
420 | if (start < SZ_4G)

      |                     ~~~~~ ^ ~~~~~
Quoted 7 lines

Replace this one with an equivalent expression that does not cause a warning.

Fixes 4765deffa0f7 ("mm, cma: support multiple contiguous ranges, if requested")
---
 mm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

LGTM. Reviewed-by: Zi Yan <[email protected]>

Best Regards,
Yan, Zi

Acked-by on v1 1/2
#6 lore raw
In reply to Arnd Bergmann
Quoted 9 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

clang warns about certain always-true conditions, like this one on 32-bit builds:

mm/cma.c:420:13: error: result of comparison of constant 4294967296 with expression of type 'phys_addr_t' (aka 'unsigned int') is always true [-Werror,-Wtautological-constant-out-of-range-compare]
420 | if (start < SZ_4G)

       |                     ~~~~~ ^ ~~~~~

Hm, so in this case the whole loop is unnecessary.

In any case

Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v1 1/2
#7 lore raw
In reply to Arnd Bergmann
Quoted 32 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

clang warns about certain always-true conditions, like this one on 32-bit builds:

mm/cma.c:420:13: error: result of comparison of constant 4294967296 with expression of type 'phys_addr_t' (aka 'unsigned int') is always true [-Werror,-Wtautological-constant-out-of-range-compare]
420 | if (start < SZ_4G)

      |                     ~~~~~ ^ ~~~~~

Replace this one with an equivalent expression that does not cause a warning.

Fixes 4765deffa0f7 ("mm, cma: support multiple contiguous ranges, if requested")
---
 mm/cma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/cma.c +1 -2
index 34a4df29af72..ef0206c0f16d 100644
@@ -417,7 +417,7 @@ int __init cma_declare_contiguous_multi(phys_addr_t total_size,
         * Create a list of ranges above 4G, largest range first.
         */
        for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &start, &end, NULL) {
-               if (start < SZ_4G)
+               if (upper_32_bits(start) == 0)
                        continue;
 
                start = ALIGN(start, align);
--
Quoted 2 lines

2.39.5

Thanks for fixing these nits in my patch series - which, btw, needs more reviews, if anyone's reading this - please :)