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.
Signed-off-by: Ankit Khushwaha ankitkhushwaha.linux@gmail.com --- tools/testing/selftests/mm/mremap_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selftests/mm/mremap_test.c index a95c0663a011..5ae0400176af 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -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
On 06.11.25 11:49, Ankit Khushwaha wrote:
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?
On Thu, Nov 06, 2025 at 12:18:57PM +0100, David Hildenbrand (Red Hat) wrote:
On 06.11.25 11:49, Ankit Khushwaha wrote:
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?
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,
On Thu, Nov 06, 2025 at 05:32:47PM +0530, Ankit Khushwaha wrote:
On Thu, Nov 06, 2025 at 12:18:57PM +0100, David Hildenbrand (Red Hat) wrote:
On 06.11.25 11:49, Ankit Khushwaha wrote:
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?
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 :)
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
On 06.11.25 13:02, Ankit Khushwaha wrote:
On Thu, Nov 06, 2025 at 12:18:57PM +0100, David Hildenbrand (Red Hat) wrote:
On 06.11.25 11:49, Ankit Khushwaha wrote:
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?
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.
Acked-by: David Hildenbrand (Red Hat) david@kernel.org
On Fri, 7 Nov 2025 10:27:27 +0100 "David Hildenbrand (Red Hat)" david@kernel.org wrote:
On 06.11.25 13:02, Ankit Khushwaha wrote:
On Thu, Nov 06, 2025 at 12:18:57PM +0100, David Hildenbrand (Red Hat) wrote:
On 06.11.25 11:49, Ankit Khushwaha wrote:
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?
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.
Acked-by: David Hildenbrand (Red Hat) david@kernel.org
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?
(added Nathan for clang advice)
On Fri, Nov 07, 2025 at 04:08:55PM -0800, Andrew Morton wrote:
On Fri, 7 Nov 2025 10:27:27 +0100 "David Hildenbrand (Red Hat)" david@kernel.org wrote:
On 06.11.25 13:02, Ankit Khushwaha wrote:
On Thu, Nov 06, 2025 at 12:18:57PM +0100, David Hildenbrand (Red Hat) wrote:
On 06.11.25 11:49, Ankit Khushwaha wrote:
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?
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.
Acked-by: David Hildenbrand (Red Hat) david@kernel.org
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?
On 11/9/25 07:54, Mike Rapoport wrote:
(added Nathan for clang advice)
On Fri, Nov 07, 2025 at 04:08:55PM -0800, Andrew Morton wrote:
On Fri, 7 Nov 2025 10:27:27 +0100 "David Hildenbrand (Red Hat)" david@kernel.org wrote:
On 06.11.25 13:02, Ankit Khushwaha wrote:
On Thu, Nov 06, 2025 at 12:18:57PM +0100, David Hildenbrand (Red Hat) wrote:
On 06.11.25 11:49, Ankit Khushwaha wrote:
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?
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.
Acked-by: David Hildenbrand (Red Hat) david@kernel.org
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?)
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.
On Sun, Nov 09, 2025 at 08:11:09PM +0100, Vlastimil Babka wrote:
> 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.
...
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...
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/6d34cfac53b993a6cdf3d6669e017eac... https://godbolt.org/z/hvMoPYb17
which I made sure respected the value of -fwrapv-pointer in
https://github.com/llvm/llvm-project/commit/f0dcf3240dffe3767c7f3a2e2da5b92a...
But it looks like the mm selftests do not build with -fno-strict-overflow. Maybe it should?
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.
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
On Thu, Nov 06, 2025 at 04:19:17PM +0530, Ankit Khushwaha wrote:
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!
Signed-off-by: Ankit Khushwaha ankitkhushwaha.linux@gmail.com
See below, but the fix seems ok despite the existing code being really horrible + in need of rework:
Reviewed-by: Lorenzo Stoakes lorenzo.stoakes@oracle.com
tools/testing/selftests/mm/mremap_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selftests/mm/mremap_test.c index a95c0663a011..5ae0400176af 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -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_ :)
ksft_print_msg("Couldn't find a valid region to remap to\n"); ret = -1; goto clean_up_src;-- 2.51.0
Thanks, Lorenzo
linux-kselftest-mirror@lists.linaro.org