selftests/mm: fix format warnings on 32-bit architectures
Subsystems (4)
PATCH 1/1 selftests/mm: fix format warnings on 32-bit architectures
On 32-bit architectures size_t and uintptr_t are unsigned int and ssize_t is int, and with _FILE_OFFSET_BITS=64 off_t is long long, so printing them with %ld, %lu or %lx, or scanning /proc/self/maps into uintptr_t variables with %lx, warns:
hugetlb-read-hwpoison.c:32:58: warning: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type 'size_t' {aka 'unsigned int'} [-Wformat=]
Print size_t with %zu and %zx, casting the nonnegative ssize_t read totals to size_t, print off_t as unsigned long long, and scan the addresses into unsigned long variables.
split_huge_page_test also builds its debugfs commands from uint64_t addresses with %lx. write_debugfs() has no format attribute, so nothing warned, but on 32-bit the commands get the wrong addresses. Print them with PRIx64, add the attribute, and cast the pointers through uintptr_t, as casting them straight to uint64_t warns on 32-bit too.
---
Found with a 32-bit build of the selftests with a 64-bit time_t (gcc 16 and clang 21 with -m32 -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64); KernelCI's 32-bit arm builds show the same warnings. The changed files build without format warnings there now, and the x86_64 builds don't change. The other selftests get the same kind of fix in separate patches.
.../selftests/mm/hugetlb-read-hwpoison.c | 25 +++++++------- .../selftests/mm/hugetlb-soft-offline.c | 2 +- tools/testing/selftests/mm/mseal_test.c | 2 +- .../selftests/mm/split_huge_page_test.c | 34 +++++++++---------- 4 files changed, 32 insertions(+), 31 deletions(-)
tools/testing/selftests/mm/hugetlb-read-hwpoison.c +13 -12
@@ -29,15 +29,15 @@ static void report_status(enum test_status status, const char *test_name, {switch (status) {case TEST_PASSED : -ksft_test_result_pass ("%s chunk_size=0x%lx\n" , +ksft_test_result_pass ("%s chunk_size=0x%zx\n" , test_name, chunk_size);break ;case TEST_FAILED : -ksft_test_result_fail ("%s chunk_size=0x%lx\n" , +ksft_test_result_fail ("%s chunk_size=0x%zx\n" , test_name, chunk_size);break ;case TEST_SKIPPED : -ksft_test_result_skip ("%s chunk_size=0x%lx\n" , +ksft_test_result_skip ("%s chunk_size=0x%zx\n" , test_name, chunk_size);break ; }
@@ -62,7 +62,7 @@ static bool verify_chunk(char *buf, size_t len, char val)for (i =0 ; i < len; ++i) {if (buf[i] != val) { -ksft_print_msg ("check fail: buf[%lu] = %u != %u\n" , +ksft_print_msg ("check fail: buf[%zu] = %u != %u\n" , i, buf[i], val);return false; }
@@ -79,8 +79,9 @@ static bool seek_read_hugepage_filemap(int fd, size_t len, size_t wr_chunk_size,ssize_t total_ret_count =0 ;char val = offset / wr_chunk_size + offset % wr_chunk_size; -ksft_print_msg ("init val=%u with offset=0x%lx\n" , val, offset); -ksft_print_msg ("expect to read 0x%lx bytes of data in total\n" , +ksft_print_msg ("init val=%u with offset=0x%llx\n" , val, + (unsigned long long )offset); +ksft_print_msg ("expect to read 0x%zx bytes of data in total\n" , expected);if (lseek (fd, offset,SEEK_SET ) <0 ) {ksft_perror ("seek failed" );
@@ -102,8 +103,8 @@ static bool seek_read_hugepage_filemap(int fd, size_t len, size_t wr_chunk_size, total_ret_count += ret_count; } -ksft_print_msg ("actually read 0x%lx bytes of data in total\n" , - total_ret_count); +ksft_print_msg ("actually read 0x%zx bytes of data in total\n" , + (size_t )total_ret_count);return total_ret_count == expected; }
@@ -116,7 +117,7 @@ static bool read_hugepage_filemap(int fd, size_t len,ssize_t total_ret_count =0 ;char val =0 ; -ksft_print_msg ("expect to read 0x%lx bytes of data in total\n" , +ksft_print_msg ("expect to read 0x%zx bytes of data in total\n" , expected);while (total_ret_count < len) { ret_count =read (fd, buf, wr_chunk_size);
@@ -133,8 +134,8 @@ static bool read_hugepage_filemap(int fd, size_t len, total_ret_count += ret_count; } -ksft_print_msg ("actually read 0x%lx bytes of data in total\n" , - total_ret_count); +ksft_print_msg ("actually read 0x%zx bytes of data in total\n" , + (size_t )total_ret_count);return total_ret_count == expected; }
@@ -293,7 +294,7 @@ int main(void)signal (SIGBUS , sigbus_handler);for (i =0 ; i <ARRAY_SIZE (wr_chunk_sizes); ++i) { -ksft_print_msg ("Write/read chunk size=0x%lx\n" , +ksft_print_msg ("Write/read chunk size=0x%zx\n" , wr_chunk_sizes[i]); fd =create_hugetlbfs_file (&file_stat);
tools/testing/selftests/mm/hugetlb-soft-offline.c +1 -1
@@ -56,7 +56,7 @@ static int do_soft_offline(int fd, size_t len, int expect_errno) }memset (filemap,0xab , len); -ksft_print_msg ("Allocated %#lx bytes of hugetlb pages\n" , len); +ksft_print_msg ("Allocated %#zx bytes of hugetlb pages\n" , len); hwp_addr = filemap + len /2 ; ret =madvise (hwp_addr, pagesize,MADV_SOFT_OFFLINE );
tools/testing/selftests/mm/mseal_test.c +1 -1
@@ -24,7 +24,7 @@ static unsigned long get_vma_size(void *addr, int *prot)FILE *maps;char line[256 ];int size =0 ; -uintptr_t addr_start, addr_end; +unsigned long addr_start, addr_end;char protstr[5 ]; *prot =0 ;
tools/testing/selftests/mm/split_huge_page_test.c +17 -17
@@ -32,9 +32,9 @@ int *expected_orders;#define SMAP_PATH "/proc/self/smaps"#define INPUT_MAX 80 -#define PID_FMT "%d,0x%lx,0x%lx,%d" -#define PID_FMT_OFFSET "%d,0x%lx,0x%lx,%d,%d" -#define PATH_FMT "%s,0x%lx,0x%lx,%d" +#define PID_FMT "%d,0x%" PRIx64 ",0x%" PRIx64 ",%d" +#define PID_FMT_OFFSET "%d,0x%" PRIx64 ",0x%" PRIx64 ",%d,%d" +#define PATH_FMT "%s,0x%" PRIx64 ",0x%" PRIx64 ",%d"const char *pagemap_proc ="/proc/self/pagemap" ;const char *kpageflags_proc ="/proc/kpageflags" ;
@@ -70,7 +70,7 @@ static int check_after_split_folio_orders(char *vaddr_start, size_t len,return status; } -static void write_debugfs (const char *fmt, ...) +static __printf(1, 2) void write_debugfs(const char *fmt, ...) {char input[INPUT_MAX ];int ret;
@@ -132,12 +132,12 @@ static void verify_rss_anon_split_huge_page_all_zeroes(char *one_page, int nr_hpdisable_khugepaged (one_page, len);/* split all THPs */ -write_debugfs (PID_FMT ,getpid (), (uint64_t )one_page, - (uint64_t )one_page + len,0 ); +write_debugfs (PID_FMT ,getpid (), (uint64_t )(uintptr_t )one_page, + (uint64_t )(uintptr_t )one_page + len,0 );for (i =0 ; i < len; i++)if (one_page[i] != (char )0 ) -ksft_exit_fail_msg ("%ld byte corrupted\n" , i); +ksft_exit_fail_msg ("%zu byte corrupted\n" , i);if (!check_huge_anon (one_page, nr_hpages * pmd_pagesize,0 , pmd_pagesize))ksft_exit_fail_msg ("Still AnonHugePages not split\n" );
@@ -181,12 +181,12 @@ static void split_pmd_thp_to_order(int order)disable_khugepaged (one_page, len);/* split all THPs */ -write_debugfs (PID_FMT ,getpid (), (uint64_t )one_page, - (uint64_t )one_page + len, order); +write_debugfs (PID_FMT ,getpid (), (uint64_t )(uintptr_t )one_page, + (uint64_t )(uintptr_t )one_page + len, order);for (i =0 ; i < len; i++)if (one_page[i] != (char )i) -ksft_exit_fail_msg ("%ld byte corrupted\n" , i); +ksft_exit_fail_msg ("%zu byte corrupted\n" , i);memset (expected_orders,0 ,sizeof (int ) * (pmd_order +1 )); expected_orders[order] =4 << (pmd_order - order);
@@ -269,8 +269,8 @@ static void split_pte_mapped_thp(void) }/* Split all THPs through the remapped pages. */ -write_debugfs (PID_FMT ,getpid (), (uint64_t )page_area, - (uint64_t )page_area + page_area_size,0 ); +write_debugfs (PID_FMT ,getpid (), (uint64_t )(uintptr_t )page_area, + (uint64_t )(uintptr_t )page_area + page_area_size,0 );/* Corruption during mremap or split? */ for (i =0 ; i < page_area_size; i++) {
@@ -536,16 +536,16 @@ static void split_thp_in_pagecache_to_order_at(size_t fd_size,*/ if (offset ==-1 ) {for (split_addr = addr; split_addr < addr + fd_size; split_addr += pmd_pagesize) -write_debugfs (PID_FMT ,getpid (), (uint64_t )split_addr, - (uint64_t )split_addr + pagesize, order); +write_debugfs (PID_FMT ,getpid (), (uint64_t )(uintptr_t )split_addr, + (uint64_t )(uintptr_t )split_addr + pagesize, order); expected_orders[order] = fd_size / (pagesize << order); }else {int times = fd_size / pmd_pagesize;for (split_addr = addr; split_addr < addr + fd_size; split_addr += pmd_pagesize) -write_debugfs (PID_FMT_OFFSET ,getpid (), (uint64_t )split_addr, - (uint64_t )split_addr + pagesize, order, offset); +write_debugfs (PID_FMT_OFFSET ,getpid (), (uint64_t )(uintptr_t )split_addr, + (uint64_t )(uintptr_t )split_addr + pagesize, order, offset);for (i = order +1 ; i < pmd_order; i++) expected_orders[i] = times;
@@ -554,7 +554,7 @@ static void split_thp_in_pagecache_to_order_at(size_t fd_size,for (i =0 ; i < fd_size; i++)if (*(addr + i) != (char )i) { -ksft_print_msg ("%lu byte corrupted in the file\n" , i); +ksft_print_msg ("%zu byte corrupted in the file\n" , i); err =EXIT_FAILURE ; goto out; }
Signature · 1 line
2.53.0
Quoted 11 lines · Sasha Levin
On 32-bit architectures
size_tanduintptr_tare unsigned int andssize_tis int, and with _FILE_OFFSET_BITS=64off_tis long long, so printing them with %ld, %lu or %lx, or scanning/proc/self/mapsintouintptr_tvariables with %lx, warns:hugetlb-read-hwpoison.c:32:58: warning: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type '
size_t' {aka 'unsigned int'} [-Wformat=]size_twith %zu and %zx, casting the nonnegativessize_tread totals tosize_t, printoff_tas unsigned long long, and scan the addresses into unsigned long variables.
Quoted 7 lines
split_huge_page_testalso builds its debugfs commands fromuint64_taddresses with %lx.write_debugfs()has no format attribute, so nothing warned, but on 32-bit the commands get the wrong addresses. Print them with PRIx64, add the attribute, and cast the pointers throughuintptr_t, as casting them straight touint64_twarns on 32-bit too.Signed-off-by Sasha Levin <[email protected]>
All looks reasonable so:
Quoted 224 lines
---
Found with a 32-bit build of the selftests with a 64-bittime_t(gcc 16 and clang 21 with -m32 -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64); KernelCI's 32-bit arm builds show the same warnings. The changed files build without format warnings there now, and thex86_64builds don't change. The other selftests get the same kind of fix in separate patches..../selftests/mm/hugetlb-read-hwpoison.c | 25 +++++++------- .../selftests/mm/hugetlb-soft-offline.c | 2 +- tools/testing/selftests/mm/mseal_test.c | 2 +- .../selftests/mm/split_huge_page_test.c | 34 +++++++++---------- 4 files changed, 32 insertions(+), 31 deletions(-)tools/testing/selftests/mm/hugetlb-read-hwpoison.c +13
-12@@ -29,15 +29,15 @@ static void report_status(enum test_status status, const char *test_name, {switch (status) {case TEST_PASSED : -ksft_test_result_pass ("%s chunk_size=0x%lx\n" , +ksft_test_result_pass ("%s chunk_size=0x%zx\n" , test_name, chunk_size);break ;case TEST_FAILED : -ksft_test_result_fail ("%s chunk_size=0x%lx\n" , +ksft_test_result_fail ("%s chunk_size=0x%zx\n" , test_name, chunk_size);break ;case TEST_SKIPPED : -ksft_test_result_skip ("%s chunk_size=0x%lx\n" , +ksft_test_result_skip ("%s chunk_size=0x%zx\n" , test_name, chunk_size);break ; }@@ -62,7 +62,7 @@ static bool verify_chunk(char *buf, size_t len, char val)for (i =0 ; i < len; ++i) {if (buf[i] != val) { -ksft_print_msg ("check fail: buf[%lu] = %u != %u\n" , +ksft_print_msg ("check fail: buf[%zu] = %u != %u\n" , i, buf[i], val);return false; }@@ -79,8 +79,9 @@ static bool seek_read_hugepage_filemap(int fd, size_t len, size_t wr_chunk_size,ssize_t total_ret_count =0 ;char val = offset / wr_chunk_size + offset % wr_chunk_size; -ksft_print_msg ("init val=%u with offset=0x%lx\n" , val, offset); -ksft_print_msg ("expect to read 0x%lx bytes of data in total\n" , +ksft_print_msg ("init val=%u with offset=0x%llx\n" , val, + (unsigned long long )offset); +ksft_print_msg ("expect to read 0x%zx bytes of data in total\n" , expected);if (lseek (fd, offset,SEEK_SET ) <0 ) {ksft_perror ("seek failed" );@@ -102,8 +103,8 @@ static bool seek_read_hugepage_filemap(int fd, size_t len, size_t wr_chunk_size, total_ret_count += ret_count; } -ksft_print_msg ("actually read 0x%lx bytes of data in total\n" , - total_ret_count); +ksft_print_msg ("actually read 0x%zx bytes of data in total\n" , + (size_t )total_ret_count);return total_ret_count == expected; }@@ -116,7 +117,7 @@ static bool read_hugepage_filemap(int fd, size_t len,ssize_t total_ret_count =0 ;char val =0 ; -ksft_print_msg ("expect to read 0x%lx bytes of data in total\n" , +ksft_print_msg ("expect to read 0x%zx bytes of data in total\n" , expected);while (total_ret_count < len) { ret_count =read (fd, buf, wr_chunk_size);@@ -133,8 +134,8 @@ static bool read_hugepage_filemap(int fd, size_t len, total_ret_count += ret_count; } -ksft_print_msg ("actually read 0x%lx bytes of data in total\n" , - total_ret_count); +ksft_print_msg ("actually read 0x%zx bytes of data in total\n" , + (size_t )total_ret_count);return total_ret_count == expected; }@@ -293,7 +294,7 @@ int main(void)signal (SIGBUS , sigbus_handler);for (i =0 ; i <ARRAY_SIZE (wr_chunk_sizes); ++i) { -ksft_print_msg ("Write/read chunk size=0x%lx\n" , +ksft_print_msg ("Write/read chunk size=0x%zx\n" , wr_chunk_sizes[i]); fd =create_hugetlbfs_file (&file_stat);tools/testing/selftests/mm/hugetlb-soft-offline.c +1
-1@@ -56,7 +56,7 @@ static int do_soft_offline(int fd, size_t len, int expect_errno) }memset (filemap,0xab , len); -ksft_print_msg ("Allocated %#lx bytes of hugetlb pages\n" , len); +ksft_print_msg ("Allocated %#zx bytes of hugetlb pages\n" , len); hwp_addr = filemap + len /2 ; ret =madvise (hwp_addr, pagesize,MADV_SOFT_OFFLINE );tools/testing/selftests/mm/mseal_test.c +1
-1@@ -24,7 +24,7 @@ static unsigned long get_vma_size(void *addr, int *prot)FILE *maps;char line[256 ];int size =0 ; -uintptr_t addr_start, addr_end; +unsigned long addr_start, addr_end;char protstr[5 ]; *prot =0 ;tools/testing/selftests/mm/split_huge_page_test.c +17
-18@@ -32,9 +32,9 @@ int *expected_orders;#define SMAP_PATH "/proc/self/smaps"#define INPUT_MAX 80 -#define PID_FMT "%d,0x%lx,0x%lx,%d" -#define PID_FMT_OFFSET "%d,0x%lx,0x%lx,%d,%d" -#define PATH_FMT "%s,0x%lx,0x%lx,%d" +#define PID_FMT "%d,0x%" PRIx64 ",0x%" PRIx64 ",%d" +#define PID_FMT_OFFSET "%d,0x%" PRIx64 ",0x%" PRIx64 ",%d,%d" +#define PATH_FMT "%s,0x%" PRIx64 ",0x%" PRIx64 ",%d"const char *pagemap_proc ="/proc/self/pagemap" ;const char *kpageflags_proc ="/proc/kpageflags" ;@@ -70,7 +70,7 @@ static int check_after_split_folio_orders(char *vaddr_start, size_t len,return status; } -static void write_debugfs (const char *fmt, ...) +static __printf(1, 2) void write_debugfs(const char *fmt, ...) {char input[INPUT_MAX ];int ret;@@ -132,12 +132,12 @@ static void verify_rss_anon_split_huge_page_all_zeroes(char *one_page, int nr_hpdisable_khugepaged (one_page, len);/* split all THPs */ -write_debugfs (PID_FMT ,getpid (), (uint64_t )one_page, - (uint64_t )one_page + len,0 ); +write_debugfs (PID_FMT ,getpid (), (uint64_t )(uintptr_t )one_page, + (uint64_t )(uintptr_t )one_page + len,0 );for (i =0 ; i < len; i++)if (one_page[i] != (char )0 ) -ksft_exit_fail_msg ("%ld byte corrupted\n" , i); +ksft_exit_fail_msg ("%zu byte corrupted\n" , i);if (!check_huge_anon (one_page, nr_hpages * pmd_pagesize,0 , pmd_pagesize))ksft_exit_fail_msg ("Still AnonHugePages not split\n" );@@ -181,12 +181,12 @@ static void split_pmd_thp_to_order(int order)disable_khugepaged (one_page, len);/* split all THPs */ -write_debugfs (PID_FMT ,getpid (), (uint64_t )one_page, - (uint64_t )one_page + len, order); +write_debugfs (PID_FMT ,getpid (), (uint64_t )(uintptr_t )one_page, + (uint64_t )(uintptr_t )one_page + len, order);for (i =0 ; i < len; i++)if (one_page[i] != (char )i) -ksft_exit_fail_msg ("%ld byte corrupted\n" , i); +ksft_exit_fail_msg ("%zu byte corrupted\n" , i);memset (expected_orders,0 ,sizeof (int ) * (pmd_order +1 )); expected_orders[order] =4 << (pmd_order - order);@@ -269,8 +269,8 @@ static void split_pte_mapped_thp(void) }/* Split all THPs through the remapped pages. */ -write_debugfs (PID_FMT ,getpid (), (uint64_t )page_area, - (uint64_t )page_area + page_area_size,0 ); +write_debugfs (PID_FMT ,getpid (), (uint64_t )(uintptr_t )page_area, + (uint64_t )(uintptr_t )page_area + page_area_size,0 );/* Corruption during mremap or split? */ for (i =0 ; i < page_area_size; i++) {@@ -536,16 +536,16 @@ static void split_thp_in_pagecache_to_order_at(size_t fd_size,*/ if (offset ==-1 ) {for (split_addr = addr; split_addr < addr + fd_size; split_addr += pmd_pagesize) -write_debugfs (PID_FMT ,getpid (), (uint64_t )split_addr, - (uint64_t )split_addr + pagesize, order); +write_debugfs (PID_FMT ,getpid (), (uint64_t )(uintptr_t )split_addr, + (uint64_t )(uintptr_t )split_addr + pagesize, order); expected_orders[order] = fd_size / (pagesize << order); }else {int times = fd_size / pmd_pagesize;for (split_addr = addr; split_addr < addr + fd_size; split_addr += pmd_pagesize) -write_debugfs (PID_FMT_OFFSET ,getpid (), (uint64_t )split_addr, - (uint64_t )split_addr + pagesize, order, offset); +write_debugfs (PID_FMT_OFFSET ,getpid (), (uint64_t )(uintptr_t )split_addr, + (uint64_t )(uintptr_t )split_addr + pagesize, order, offset);for (i = order +1 ; i < pmd_order; i++) expected_orders[i] = times;@@ -554,7 +554,7 @@ static void split_thp_in_pagecache_to_order_at(size_t fd_size,for (i =0 ; i < fd_size; i++)if (*(addr + i) != (char )i) { -ksft_print_msg ("%lu byte corrupted in the file\n" , i); +ksft_print_msg ("%zu byte corrupted in the file\n" , i); err =EXIT_FAILURE ; goto out; } --
Quoted 2 lines
2.53.0
--
Cheers, Lorenzo
Quoted 32 lines · Sasha Levin
On 32-bit architectures
size_tanduintptr_tare unsigned int andssize_tis int, and with _FILE_OFFSET_BITS=64off_tis long long, so printing them with %ld, %lu or %lx, or scanning/proc/self/mapsintouintptr_tvariables with %lx, warns:hugetlb-read-hwpoison.c:32:58: warning: format '%lx' expects argument of type 'long unsigned int', but argument 3 has type '
size_t' {aka 'unsigned int'} [-Wformat=]size_twith %zu and %zx, casting the nonnegativessize_tread totals tosize_t, printoff_tas unsigned long long, and scan the addresses into unsigned long variables.
split_huge_page_testalso builds its debugfs commands fromuint64_taddresses with %lx.write_debugfs()has no format attribute, so nothing warned, but on 32-bit the commands get the wrong addresses. Print them with PRIx64, add the attribute, and cast the pointers throughuintptr_t, as casting them straight touint64_twarns on 32-bit too.Signed-off-by Sasha Levin <[email protected]>---
Found with a 32-bit build of the selftests with a 64-bittime_t(gcc 16 and clang 21 with -m32 -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64); KernelCI's 32-bit arm builds show the same warnings. The changed files build without format warnings there now, and thex86_64builds don't change. The other selftests get the same kind of fix in separate patches..../selftests/mm/hugetlb-read-hwpoison.c | 25 +++++++------- .../selftests/mm/hugetlb-soft-offline.c | 2 +- tools/testing/selftests/mm/mseal_test.c | 2 +- .../selftests/mm/split_huge_page_test.c | 34 +++++++++---------- 4 files changed, 32 insertions(+), 31 deletions(-)
<snip>
tools/testing/selftests/mm/split_huge_page_test.c static int check_after_split_folio_orders(char *vaddr_start, size_t len, · line 73
Quoted 6 lines
tools/testing/selftests/mm/split_huge_page_test.c +1
-1@@ -70,7 +70,7 @@ static int check_after_split_folio_orders(char *vaddr_start, size_t len,return status; } -static void write_debugfs (const char *fmt, ...) +static __printf(1, 2) void write_debugfs(const char *fmt, ...)
TIL, __printf() tells the compiler that write_debugfs() is doing printf_style arguments and the compiler will do the corresponding checks.
LGTM. Thanks.
Best Regards,
Yan, Zi