Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings
@ 2026-09-23  2:31 Tianyi Chen
  2026-09-23  2:31 ` [PATCH v2 1/4] memblock tests: use physical addresses for region removal Tianyi Chen
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Tianyi Chen @ 2026-09-23  2:31 UTC (permalink / raw)
  To: Mike Rapoport; +Cc: linux-mm, linux-kernel

Fix the BUILD=32 physical-address truncation and sign-extension failures,
and address the remaining GCC warnings reported during review.

Changes in v2:
- Use memblock_phys_free() for all synthetic physical ranges in the basic
  API tests, not only the maximum-address check.
- Keep the dummy-memory address conversion fix unchanged.
- Use __pa() in tools virt_to_phys() to avoid a pointer-size warning and
  sign extension of 32-bit pointers with their top bit set.
- Initialize end_rgn to avoid GCC's maybe-uninitialized warning.

Clang BUILD=32 without NUMA also passes on v1 (189 checks). The earlier
cover letter only mentioned the NUMA-enabled configuration tested then.

Validated v2 with GCC 13.3.0 and Clang 18.1.3, retaining the default ASan
and UBSan flags and adding -Werror. Both compilers build without warnings
and pass ./main -v and ./main -m -v in each configuration:
- Default and NUMA=1.
- BUILD=32, with and without NUMA=1.
- 32BIT_PHYS_ADDR_T=1, with and without NUMA=1.
- BUILD=32 NUMA=1 MEMBLOCK_DEBUG=1.

Each run passes 189 checks without NUMA or 272 with NUMA, including both
allocation directions. A separate virt_to_phys() boundary check passes
with both compilers in 32- and 64-bit builds; GCC BUILD=32 failed it on
v1 for 0x80000000 due to sign extension.

v1: https://lore.kernel.org/r/20260916161105.2548281-1-hi@tychen.cc

Tianyi Chen (4):
  memblock tests: use physical addresses for region removal
  memblock tests: zero-extend simulated memory addresses
  tools: use __pa() in virt_to_phys()
  memblock: initialize the region merge end index

 mm/memblock.c                            |  2 +-
 tools/include/linux/mm.h                 |  2 +-
 tools/testing/memblock/tests/basic_api.c | 20 ++++++++++----------
 tools/testing/memblock/tests/common.c    |  6 +++---
 4 files changed, 15 insertions(+), 15 deletions(-)


base-commit: 5728d3523f12826a2b180c1c5738f74e33197e14
-- 
2.55.0



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/4] memblock tests: use physical addresses for region removal
  2026-09-23  2:31 [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings Tianyi Chen
@ 2026-09-23  2:31 ` Tianyi Chen
  2026-09-23  2:31 ` [PATCH v2 2/4] memblock tests: zero-extend simulated memory addresses Tianyi Chen
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Tianyi Chen @ 2026-09-23  2:31 UTC (permalink / raw)
  To: Mike Rapoport; +Cc: linux-mm, linux-kernel

With BUILD=32, phys_addr_t remains 64-bit while pointers are 32-bit.
Casting the address near PHYS_ADDR_MAX to void * truncates it before
memblock_free() converts it back with __pa(). The reserved region is
therefore left unchanged and memblock_free_near_max_check() fails.

Use memblock_phys_free() for all synthetic physical ranges in the basic
API tests. This also removes the integer-to-pointer cast warnings from
the other removal checks. Retain the checks that freeing past
PHYS_ADDR_MAX clips the removed region.

Fixes: 21a233f68afe ("memblock tests: add additional tests for basic api and memblock_alloc")
Assisted-by: LLM
Signed-off-by: Tianyi Chen <hi@tychen.cc>
---
 tools/testing/memblock/tests/basic_api.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/tools/testing/memblock/tests/basic_api.c b/tools/testing/memblock/tests/basic_api.c
index 01e836fba488..47bcdcc115a5 100644
--- a/tools/testing/memblock/tests/basic_api.c
+++ b/tools/testing/memblock/tests/basic_api.c
@@ -1730,7 +1730,7 @@ static int memblock_free_simple_check(void)
 	reset_memblock_regions();
 	memblock_reserve(r1.base, r1.size);
 	memblock_reserve(r2.base, r2.size);
-	memblock_free((void *)r1.base, r1.size);
+	memblock_phys_free(r1.base, r1.size);
 
 	ASSERT_EQ(rgn->base, r2.base);
 	ASSERT_EQ(rgn->size, r2.size);
@@ -1778,7 +1778,7 @@ static int memblock_free_absent_check(void)
 
 	reset_memblock_regions();
 	memblock_reserve(r1.base, r1.size);
-	memblock_free((void *)r2.base, r2.size);
+	memblock_phys_free(r2.base, r2.size);
 
 	ASSERT_EQ(rgn->base, r1.base);
 	ASSERT_EQ(rgn->size, r1.size);
@@ -1832,7 +1832,7 @@ static int memblock_free_overlap_top_check(void)
 
 	reset_memblock_regions();
 	memblock_reserve(r1.base, r1.size);
-	memblock_free((void *)r2.base, r2.size);
+	memblock_phys_free(r2.base, r2.size);
 
 	ASSERT_EQ(rgn->base, r2.base + r2.size);
 	ASSERT_EQ(rgn->size, total_size);
@@ -1881,7 +1881,7 @@ static int memblock_free_overlap_bottom_check(void)
 
 	reset_memblock_regions();
 	memblock_reserve(r1.base, r1.size);
-	memblock_free((void *)r2.base, r2.size);
+	memblock_phys_free(r2.base, r2.size);
 
 	ASSERT_EQ(rgn->base, r1.base);
 	ASSERT_EQ(rgn->size, total_size);
@@ -1938,7 +1938,7 @@ static int memblock_free_within_check(void)
 
 	reset_memblock_regions();
 	memblock_reserve(r1.base, r1.size);
-	memblock_free((void *)r2.base, r2.size);
+	memblock_phys_free(r2.base, r2.size);
 
 	ASSERT_EQ(rgn1->base, r1.base);
 	ASSERT_EQ(rgn1->size, r1_size);
@@ -1976,7 +1976,7 @@ static int memblock_free_only_region_check(void)
 
 	reset_memblock_regions();
 	memblock_reserve(r1.base, r1.size);
-	memblock_free((void *)r1.base, r1.size);
+	memblock_phys_free(r1.base, r1.size);
 
 	ASSERT_EQ(rgn->base, 0);
 	ASSERT_EQ(rgn->size, 0);
@@ -2003,7 +2003,7 @@ static int memblock_free_only_region_check(void)
  * Expect the total size of reserved memory to be updated and the counter to
  * not be updated.
  */
-static int memblock_free_near_max_check(void)
+static int memblock_phys_free_near_max_check(void)
 {
 	struct memblock_region *rgn;
 	phys_addr_t total_size;
@@ -2026,7 +2026,7 @@ static int memblock_free_near_max_check(void)
 
 	reset_memblock_regions();
 	memblock_reserve(r1.base, r1.size);
-	memblock_free((void *)r2.base, r2.size);
+	memblock_phys_free(r2.base, r2.size);
 
 	ASSERT_EQ(rgn->base, r1.base);
 	ASSERT_EQ(rgn->size, total_size);
@@ -2086,7 +2086,7 @@ static int memblock_free_overlap_two_check(void)
 	reset_memblock_regions();
 	memblock_reserve(r1.base, r1.size);
 	memblock_reserve(r2.base, r2.size);
-	memblock_free((void *)r3.base, r3.size);
+	memblock_phys_free(r3.base, r3.size);
 
 	ASSERT_EQ(rgn1->base, r1.base);
 	ASSERT_EQ(rgn1->size, new_r1_size);
@@ -2114,7 +2114,7 @@ static int memblock_free_checks(void)
 	memblock_free_overlap_bottom_check();
 	memblock_free_within_check();
 	memblock_free_only_region_check();
-	memblock_free_near_max_check();
+	memblock_phys_free_near_max_check();
 	memblock_free_overlap_two_check();
 
 	prefix_pop();
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/4] memblock tests: zero-extend simulated memory addresses
  2026-09-23  2:31 [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings Tianyi Chen
  2026-09-23  2:31 ` [PATCH v2 1/4] memblock tests: use physical addresses for region removal Tianyi Chen
@ 2026-09-23  2:31 ` Tianyi Chen
  2026-09-23  2:31 ` [PATCH v2 3/4] tools: use __pa() in virt_to_phys() Tianyi Chen
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Tianyi Chen @ 2026-09-23  2:31 UTC (permalink / raw)
  To: Mike Rapoport; +Cc: linux-mm, linux-kernel

Converting a 32-bit pointer directly to the 64-bit phys_addr_t can
sign-extend it when BUILD=32 is used. A dummy allocation above 2 GiB
then gets registered at an address different from its unsigned pointer
value. The low-address tests compare that value against the registered
range and fail once the earlier maximum-address free test is fixed.

Convert through uintptr_t when registering and reporting dummy memory.
This preserves the pointer bits and agrees with the simulator __pa()
conversion and the allocation tests.

Fixes: 284d950dd6b0 ("memblock tests: Add simulation of physical memory")
Assisted-by: LLM
Signed-off-by: Tianyi Chen <hi@tychen.cc>
---
 tools/testing/memblock/tests/common.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/testing/memblock/tests/common.c b/tools/testing/memblock/tests/common.c
index ac032610a56e..2a248973eb4c 100644
--- a/tools/testing/memblock/tests/common.c
+++ b/tools/testing/memblock/tests/common.c
@@ -67,7 +67,7 @@ static inline void fill_memblock(void)
 void setup_memblock(void)
 {
 	reset_memblock_regions();
-	memblock_add((phys_addr_t)memory_block.base, MEM_SIZE);
+	memblock_add((phys_addr_t)(uintptr_t)memory_block.base, MEM_SIZE);
 	fill_memblock();
 }
 
@@ -88,7 +88,7 @@ void setup_numa_memblock(const unsigned int node_fracs[])
 	int flags;
 
 	reset_memblock_regions();
-	base = (phys_addr_t)memory_block.base;
+	base = (phys_addr_t)(uintptr_t)memory_block.base;
 	flags = (movable_node_is_enabled()) ? MEMBLOCK_NONE : MEMBLOCK_HOTPLUG;
 
 	for (int i = 0; i < NUMA_NODES; i++) {
@@ -115,7 +115,7 @@ void dummy_physical_memory_cleanup(void)
 
 phys_addr_t dummy_physical_memory_base(void)
 {
-	return (phys_addr_t)memory_block.base;
+	return (phys_addr_t)(uintptr_t)memory_block.base;
 }
 
 phys_addr_t dummy_physical_memory_low_limit(void)
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 3/4] tools: use __pa() in virt_to_phys()
  2026-09-23  2:31 [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings Tianyi Chen
  2026-09-23  2:31 ` [PATCH v2 1/4] memblock tests: use physical addresses for region removal Tianyi Chen
  2026-09-23  2:31 ` [PATCH v2 2/4] memblock tests: zero-extend simulated memory addresses Tianyi Chen
@ 2026-09-23  2:31 ` Tianyi Chen
  2026-09-23  2:31 ` [PATCH v2 4/4] memblock: initialize the region merge end index Tianyi Chen
  2026-09-24  7:57 ` [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings Mike Rapoport
  4 siblings, 0 replies; 6+ messages in thread
From: Tianyi Chen @ 2026-09-23  2:31 UTC (permalink / raw)
  To: Mike Rapoport; +Cc: linux-mm, linux-kernel

With 32-bit pointers and a 64-bit phys_addr_t, GCC warns about the
pointer-to-integer cast in virt_to_phys(). It can also sign-extend
pointers whose top bit is set, turning 0x80000000 into 0xffffffff80000000.

Use __pa() to convert through unsigned long before widening the result.
This preserves the unsigned pointer value and matches the other address
conversion helpers.

Reported-by: Mike Rapoport <rppt@kernel.org>
Link: https://lore.kernel.org/r/179007180279.508753.4595268983388589280.b4-review@b4
Assisted-by: LLM
Signed-off-by: Tianyi Chen <hi@tychen.cc>
---
 tools/include/linux/mm.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/include/linux/mm.h b/tools/include/linux/mm.h
index 84b5954f66c3..480526de8d65 100644
--- a/tools/include/linux/mm.h
+++ b/tools/include/linux/mm.h
@@ -30,7 +30,7 @@ static inline void *phys_to_virt(unsigned long address)
 #define virt_to_phys virt_to_phys
 static inline phys_addr_t virt_to_phys(volatile void *address)
 {
-	return (phys_addr_t)address;
+	return __pa(address);
 }
 
 static inline void totalram_pages_inc(void)
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 4/4] memblock: initialize the region merge end index
  2026-09-23  2:31 [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings Tianyi Chen
                   ` (2 preceding siblings ...)
  2026-09-23  2:31 ` [PATCH v2 3/4] tools: use __pa() in virt_to_phys() Tianyi Chen
@ 2026-09-23  2:31 ` Tianyi Chen
  2026-09-24  7:57 ` [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings Mike Rapoport
  4 siblings, 0 replies; 6+ messages in thread
From: Tianyi Chen @ 2026-09-23  2:31 UTC (permalink / raw)
  To: Mike Rapoport; +Cc: linux-mm, linux-kernel

GCC 13 reports that end_rgn may be used uninitialized when building the
memblock simulator with BUILD=32 and its default sanitizer flags.

Every insertion sets end_rgn, and the nr_new check prevents merging when
there were no insertions, so this is not an uninitialized read. Initialize
the index to zero to avoid the warning without disabling the diagnostic.

Reported-by: Mike Rapoport <rppt@kernel.org>
Link: https://lore.kernel.org/r/179007180279.508753.4595268983388589280.b4-review@b4
Assisted-by: LLM
Signed-off-by: Tianyi Chen <hi@tychen.cc>
---
 mm/memblock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/memblock.c b/mm/memblock.c
index 22d14a637f24..aee1fe36130b 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -614,7 +614,7 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
 	bool insert = false;
 	phys_addr_t obase = base;
 	phys_addr_t end = base + memblock_cap_size(base, &size);
-	int idx, nr_new, start_rgn = -1, end_rgn;
+	int idx, nr_new, start_rgn = -1, end_rgn = 0;
 	struct memblock_region *rgn;
 
 	if (!size)
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings
  2026-09-23  2:31 [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings Tianyi Chen
                   ` (3 preceding siblings ...)
  2026-09-23  2:31 ` [PATCH v2 4/4] memblock: initialize the region merge end index Tianyi Chen
@ 2026-09-24  7:57 ` Mike Rapoport
  4 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport @ 2026-09-24  7:57 UTC (permalink / raw)
  To: Tianyi Chen; +Cc: linux-mm, linux-kernel

On Wed, 23 Sep 2026 11:31:22 +0900, Tianyi Chen wrote:
> memblock tests: fix BUILD=32 address conversions and warnings
> 
> Fix the BUILD=32 physical-address truncation and sign-extension failures,
> and address the remaining GCC warnings reported during review.
> 
> Changes in v2:
> - Use memblock_phys_free() for all synthetic physical ranges in the basic
>   API tests, not only the maximum-address check.
> - Keep the dummy-memory address conversion fix unchanged.
> - Use __pa() in tools virt_to_phys() to avoid a pointer-size warning and
>   sign extension of 32-bit pointers with their top bit set.
> - Initialize end_rgn to avoid GCC's maybe-uninitialized warning.
> 
> [...]

Applied to for-next branch of memblock.git tree, thanks!

[1/4] memblock tests: use physical addresses for region removal
      commit: 9357596afac61154e7f54048c0add9f1293b9eba
[2/4] memblock tests: zero-extend simulated memory addresses
      commit: 6957416ae43de1830174f527b5b8cf737401a8dd
[3/4] tools: use __pa() in virt_to_phys()
      commit: a10f3a08d248fcb3006f587faf42437837fd12c6
[4/4] memblock: initialize the region merge end index
      commit: ba94f68b754985f77c3f6d3354df74952a1e0752

tree: https://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock
branch: for-next

--
Sincerely yours,
Mike.




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-09-24  7:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-23  2:31 [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings Tianyi Chen
2026-09-23  2:31 ` [PATCH v2 1/4] memblock tests: use physical addresses for region removal Tianyi Chen
2026-09-23  2:31 ` [PATCH v2 2/4] memblock tests: zero-extend simulated memory addresses Tianyi Chen
2026-09-23  2:31 ` [PATCH v2 3/4] tools: use __pa() in virt_to_phys() Tianyi Chen
2026-09-23  2:31 ` [PATCH v2 4/4] memblock: initialize the region merge end index Tianyi Chen
2026-09-24  7:57 ` [PATCH v2 0/4] memblock tests: fix BUILD=32 address conversions and warnings Mike Rapoport

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox