Skip to contentSkip to conversation

selftest/mm: fix pointer comparison in mremap_test

PATCH Superseded Ankit Khushwaha 7 participants 11 messages Nov 6, 2025 – Nov 10, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftest/mm: fix pointer comparison in mremap_test

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

---
 tools/testing/selftests/mm/mremap_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/mremap_test.c +1 -1
index a95c0663a011..5ae0400176af 100644
@@ -1032,7 +1032,7 @@ static long long remap_region(struct config c, unsigned int threshold_mb,
 	/* Don't destroy existing mappings unless expected to overlap */
 	while (!is_remap_region_valid(addr, c.region_size) && !c.overlapping) {
 		/* Check for unsigned overflow */
-		if (addr + c.dest_alignment < addr) {
+		if ((unsigned long long) addr + c.dest_alignment < (unsigned long long) addr) {
 			ksft_print_msg("Couldn't find a valid region to remap to\n");
 			ret = -1;
 			goto clean_up_src;

--
2.51.0

#1 lore raw
Quoted 9 lines · Ankit Khushwaha

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

With which compiler are you seeing this?

Signature · 3 lines
Cheers

David
#2 lore raw
Quoted 4 lines · David Hildenbrand (Red Hat)
Quoted 3 lines · Ankit Khushwaha

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

Quoted 8 lines
Quoted 6 lines

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

With which compiler are you seeing this?

Hi David,

clang version 20.1.8 (Fedora 20.1.8-4.fc42) raised this warning.

To reproduce:
make -C tools/testing/selftests/mm CC=clang

Thanks,

Signature · 1 line
Ankit
#3 lore raw
Quoted 12 lines · Ankit Khushwaha
Quoted 11 lines · David Hildenbrand (Red Hat)
Quoted 9 lines · Ankit Khushwaha

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

Quoted 8 lines
Quoted 1 line

With which compiler are you seeing this?

Hi David,

clang version 20.1.8 (Fedora 20.1.8-4.fc42) raised this warning.

To reproduce:
make -C tools/testing/selftests/mm CC=clang

FYI That doesn't work:

$ make -C tools/testing/selftests/mm CC=clang
make: Entering directory '/data/kerndev/kernels/mm/tools/testing/selftests/mm'
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: gcc (GCC) 15.2.1 20250813
  You are using:           clang version 20.1.8
  CC [M]  page_frag_test.o
clang: error: unknown argument: '-fmin-function-alignment=16'
clang: error: unknown argument: '-fconserve-stack'
clang: error: unsupported option '-mrecord-mcount' for target 'x86_64-unknown-linux-gnu'
make[4]: *** [/usr/lib/modules/6.17.2-arch1-1/build/scripts/Makefile.build:287: page_frag_test.o] Error 1
make[3]: *** [/usr/lib/modules/6.17.2-arch1-1/build/Makefile:2011: .] Error 2
make[2]: *** [Makefile:248: __sub-make] Error 2
make[1]: *** [Makefile:15: all] Error 2
make: *** [../lib.mk:146: gen_mods_dir] Error 2
make: Leaving directory '/data/kerndev/kernels/mm/tools/testing/selftests/mm'
[649ms][2][review/loz-v2][~/kerndev/kernels/mm]$ cd tools/testing/selftests/mm

Even if I rebuild the entire kernel using clang via LLVM=1 it doesn't work.

The following KIND OF works:

$ make -C tools/testing/selftests/mm clean
$ make tools/testing/selftests/mm LLVM=1

But I still get:

warning: the compiler differs from the one used to build the kernel
The kernel was built by: gcc (GCC) 15.2.1 20250813
You are using: clang version 20.1.8
CC [M] page_frag_test.o
clang: error: unknown argument: '-fmin-function-alignment=16'
clang: error: unknown argument: '-fconserve-stack'
clang: error: unsupported option '-mrecord-mcount' for target 'x86_64-unknown-linux-gnu' make[4]: *** [/usr/lib/modules/6.17.2-arch1-1/build/scripts/Makefile.build:287: page_frag_test.o] Error 1 make[3]: *** [/usr/lib/modules/6.17.2-arch1-1/build/Makefile:2011: .] Error 2
make[2]: *** [Makefile:248: __sub-make] Error 2
make[1]: *** [Makefile:15: all] Error 2
make: *** [../lib.mk:146: gen_mods_dir] Error 2
make: Leaving directory '/data/kerndev/kernels/mm/tools/testing/selftests/mm'

Errors, presumably unless you build _the entire kernel_ using LLVM=1 :)

Quoted 4 lines

Thanks,
--
Ankit

It may be worth looking at how to make this behave better because this seems silly if clang will pick up additional warnings.

Cheers, Lorenzo

maintainer Acked-by
#4 lore raw
In reply to Ankit Khushwaha
Quoted 12 lines · Ankit Khushwaha
Quoted 11 lines · David Hildenbrand (Red Hat)
Quoted 9 lines · Ankit Khushwaha

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

Quoted 8 lines
Quoted 1 line

With which compiler are you seeing this?

Hi David,

clang version 20.1.8 (Fedora 20.1.8-4.fc42) raised this warning.

To reproduce:
make -C tools/testing/selftests/mm CC=clang

Thanks, and thanks to Lorenzo for the details.

Signature · 3 lines
Cheers

David
maintainer
#5 lore raw
Quoted 17 lines · On Fri, 7 Nov 2025 10:27:27 +0100 "David Hildenbrand (Red Hat)"…
Quoted 16 lines · Ankit Khushwaha
Quoted 4 lines · David Hildenbrand (Red Hat)
Quoted 3 lines · Ankit Khushwaha

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

Quoted 8 lines
Quoted 6 lines

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

With which compiler are you seeing this?

Hi David,

Quoted 8 lines
Quoted 4 lines

clang version 20.1.8 (Fedora 20.1.8-4.fc42) raised this warning.

To reproduce:
make -C tools/testing/selftests/mm CC=clang

Thanks, and thanks to Lorenzo for the details.

I must say, applying this would be an unhappy life event.

if (void* + ulong < void*)

makes perfect sense in a world which permits void* arithmetic (ie, ours). So what the heck is clang doing??

If we do

	void *addr2 = addr + c.dest_alignment;
	if (addr2 < addr)
		...

then which statement warns, and why?

#6 lore raw
Quoted 5 lines · Andrew Morton
	void *addr2 = addr + c.dest_alignment;
	if (addr2 < addr)
		...

Hi Andrew,
i have tried this and clang is raising no warning. I will send v2 patch with this changes.

Thanks
-- Ankit

reviewer
#7 lore raw
In reply to Andrew Morton

(added Nathan for clang advice)

Quoted 42 lines · Andrew Morton
Quoted 17 lines · On Fri, 7 Nov 2025 10:27:27 +0100 "David Hildenbrand (Red Hat)"…
Quoted 16 lines · Ankit Khushwaha
Quoted 4 lines · David Hildenbrand (Red Hat)
Quoted 3 lines · Ankit Khushwaha

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

Quoted 8 lines
Quoted 6 lines

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

With which compiler are you seeing this?

Hi David,

Quoted 8 lines
Quoted 4 lines

clang version 20.1.8 (Fedora 20.1.8-4.fc42) raised this warning.

To reproduce:
make -C tools/testing/selftests/mm CC=clang

Thanks, and thanks to Lorenzo for the details.

I must say, applying this would be an unhappy life event.

if (void* + ulong < void*)

makes perfect sense in a world which permits void* arithmetic (ie, ours). So what the heck is clang doing??

If we do

	void *addr2 = addr + c.dest_alignment;
	if (addr2 < addr)
		...

then which statement warns, and why?

Signature · 2 lines
Sincerely yours,
Mike.
#8 lore raw
Quoted 37 lines · Mike Rapoport

(added Nathan for clang advice)

Quoted 34 lines · Andrew Morton
Quoted 17 lines · On Fri, 7 Nov 2025 10:27:27 +0100 "David Hildenbrand (Red Hat)"…
Quoted 16 lines · Ankit Khushwaha
Quoted 4 lines · David Hildenbrand (Red Hat)
Quoted 3 lines · Ankit Khushwaha

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

Quoted 8 lines
Quoted 6 lines

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

With which compiler are you seeing this?

Hi David,

Quoted 8 lines
Quoted 4 lines

clang version 20.1.8 (Fedora 20.1.8-4.fc42) raised this warning.

To reproduce:
make -C tools/testing/selftests/mm CC=clang

Thanks, and thanks to Lorenzo for the details.

I must say, applying this would be an unhappy life event.

if (void* + ulong < void*)

makes perfect sense in a world which permits void* arithmetic (ie, ours). So what the heck is clang doing??

My (not very informed) guess would be something about undefined behavior because pointer arithmetic is strictly speaking only valid within an array, so void* + ulong is also still in the same array, and thus can't become smaller by an overflow, because overflow can't happen if we're still within the same valid array...

But I don't know if this strictness is only applied to the warning itself or to the actual compilation too (does it eliminate everything as dead code then?)

Quoted 7 lines
Quoted 7 lines

If we do

	void *addr2 = addr + c.dest_alignment;
	if (addr2 < addr)
		...

then which statement warns, and why?

As the answer was that nothing warns, I'd think it just isn't able to warn if it's no longer part of the same statement. Whether it also means it's not eliminated as dead code anymore, dunno.

#9 lore raw
Quoted 9 lines · Vlastimil Babka
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

...

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

I must say, applying this would be an unhappy life event.

if (void* + ulong < void*)

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

makes perfect sense in a world which permits void* arithmetic (ie, ours). So what the heck is clang doing??

My (not very informed) guess would be something about undefined behavior because pointer arithmetic is strictly speaking only valid within an array, so void* + ulong is also still in the same array, and thus can't become smaller by an overflow, because overflow can't happen if we're still within the same valid array...

It is indeed due to undefined behavior but more so that without -fwrapv-pointer (set via -fno-strict-overflow for the kernel build), the addition of an unsigned index and a pointer cannot wrap. This warning is a result of the following change in clang-20:

https://github.com/llvm/llvm-project/commit/6d34cfac53b993a6cdf3d6669e017eac3a2296c8
https://godbolt.org/z/hvMoPYb17

which I made sure respected the value of -fwrapv-pointer in

https://github.com/llvm/llvm-project/commit/f0dcf3240dffe3767c7f3a2e2da5b92ae9fd1bef

But it looks like the mm selftests do not build with -fno-strict-overflow. Maybe it should?

Quoted 2 lines

But I don't know if this strictness is only applied to the warning itself or to the actual compilation too (does it eliminate everything as dead code then?)

Yes, it would turn that

  if (addr + c.dest_alignment < addr) {

into just

  if (false) {

based on the above Godbolt link.

Quoted 11 lines
Quoted 7 lines
Quoted 7 lines

If we do

	void *addr2 = addr + c.dest_alignment;
	if (addr2 < addr)
		...

then which statement warns, and why?

As the answer was that nothing warns, I'd think it just isn't able to warn if it's no longer part of the same statement. Whether it also means it's not eliminated as dead code anymore, dunno.

Based on the above Godbolt link, it appears it will be optimized the same way, just without the warning letting you know something is up.

Cheers,
Nathan

Reviewed-by on v1 1/1
#10 lore raw
In reply to Ankit Khushwaha
Quoted 9 lines · Ankit Khushwaha

Pointer arthemitic with 'void * addr' and 'unsigned long long dest_alignment' triggers following warning:

mremap_test.c:1035:31: warning: pointer comparison always evaluates to false [-Wtautological-compare]
1035 | if (addr + c.dest_alignment < addr) {
| ^

typecasting 'addr' to 'unsigned long long' to fix pointer comparison.

Thanks for raising!

Quoted 2 lines

See below, but the fix seems ok despite the existing code being really horrible
+ in need of rework:

Quoted 9 lines
---
 tools/testing/selftests/mm/mremap_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/mremap_test.c +0 -0
index a95c0663a011..5ae0400176af 100644

tools/testing/selftests/mm/mremap_test.c static long long remap_region(struct config c, unsigned int threshold_mb, · line 1035

Quoted 5 lines
tools/testing/selftests/mm/mremap_test.c +1 -1
@@ -1032,7 +1032,7 @@ static long long remap_region(struct config c, unsigned int threshold_mb,
 	/* Don't destroy existing mappings unless expected to overlap */
 	while (!is_remap_region_valid(addr, c.region_size) && !c.overlapping) {
 		/* Check for unsigned overflow */
-		if (addr + c.dest_alignment < addr) {
+		if ((unsigned long long) addr + c.dest_alignment < (unsigned long long) addr) {

Hmm this is odd though. gcc (and presumably for compatibility clang) treat void
* arithmetic as if void * was of size 1, so on 64-bit systems this should be:

64-bit value + 64-bit value < 64-bit value

Which is a valid check for overflow right?

On 32-bit it seems even more so:

32-bit value + 64-bit value < 64-bit value

So I'm not sure why this warning is happening.

Maybe clang is doing some very clever checks to determine this is false.

In practice I don't see how this could happen given userland pointers are already restricted on upper bits.

BUT at the same time this is something that the rest of the test code does so it seems fine to take this.

Also, (to be clear - it's not your fault Ankit):

This (existing code) is horrible, I don't know why we're using unsigned long long _anyway_ here, the whole function is appalling.

Your fix is in line with the existing code but is still quite horrible (again _not your fault_ :). So on the basis that it shuts this error up

I guess we can take this patch as the fix is equally as gross as the existing code, but can we please:

a. do away with the unsigned long long nonsense if possible (presumably to try
to detect overflow on 32-bit systems, but I do not understand why we are bothering it seems totally overengineered and pointless for arbitrary mremap tests)

b. For the love of all spiders, cats and other beautiful things on planet earth,
let's please store a local unsigned long variable for the address and convert from void * _once_ :)

Quoted 6 lines
tools/testing/selftests/mm/mremap_test.c +0 -1
 			ksft_print_msg("Couldn't find a valid region to remap to\n");
 			ret = -1;
 			goto clean_up_src;
--

2.51.0

Thanks, Lorenzo