Skip to content

Commit 5bbc2b7

Browse files
x-y-zakpm00
authored andcommitted
selftests/mm: fix FORCE_READ to read input value correctly
FORCE_READ() converts input value x to its pointer type then reads from address x. This is wrong. If x is a non-pointer, it would be caught it easily. But all FORCE_READ() callers are trying to read from a pointer and FORCE_READ() basically reads a pointer to a pointer instead of the original typed pointer. Almost no access violation was found, except the one from split_huge_page_test. Fix it by implementing a simplified READ_ONCE() instead. Link: https://lkml.kernel.org/r/20250805175140.241656-1-ziy@nvidia.com Fixes: 3f6bfd4 ("selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));"") Signed-off-by: Zi Yan <ziy@nvidia.com> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Acked-by: David Hildenbrand <david@redhat.com> Reviewed-by: wang lian <lianux.mm@gmail.com> Reviewed-by: Wei Yang <richard.weiyang@gmail.com> Cc: Christian Brauner <brauner@kernel.org> Cc: Jann Horn <jannh@google.com> Cc: Kairui Song <ryncsn@gmail.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Mark Brown <broonie@kernel.org> Cc: SeongJae Park <sj@kernel.org> Cc: Shuah Khan <shuah@kernel.org> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Zi Yan <ziy@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 9614d8b commit 5bbc2b7

File tree

7 files changed

+14
-9
lines changed

7 files changed

+14
-9
lines changed

tools/testing/selftests/mm/cow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,8 +1554,8 @@ static void run_with_zeropage(non_anon_test_fn fn, const char *desc)
15541554
}
15551555

15561556
/* Read from the page to populate the shared zeropage. */
1557-
FORCE_READ(mem);
1558-
FORCE_READ(smem);
1557+
FORCE_READ(*mem);
1558+
FORCE_READ(*smem);
15591559

15601560
fn(mem, smem, pagesize);
15611561
munmap:

tools/testing/selftests/mm/guard-regions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static bool try_access_buf(char *ptr, bool write)
145145
if (write)
146146
*ptr = 'x';
147147
else
148-
FORCE_READ(ptr);
148+
FORCE_READ(*ptr);
149149
}
150150

151151
signal_jump_set = false;

tools/testing/selftests/mm/hugetlb-madvise.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ void read_fault_pages(void *addr, unsigned long nr_pages)
5050
unsigned long i;
5151

5252
for (i = 0; i < nr_pages; i++) {
53+
unsigned long *addr2 =
54+
((unsigned long *)(addr + (i * huge_page_size)));
5355
/* Prevent the compiler from optimizing out the entire loop: */
54-
FORCE_READ(((unsigned long *)(addr + (i * huge_page_size))));
56+
FORCE_READ(*addr2);
5557
}
5658
}
5759

tools/testing/selftests/mm/migration.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void *access_mem(void *ptr)
110110
* the memory access actually happens and prevents the compiler
111111
* from optimizing away this entire loop.
112112
*/
113-
FORCE_READ((uint64_t *)ptr);
113+
FORCE_READ(*(uint64_t *)ptr);
114114
}
115115

116116
return NULL;

tools/testing/selftests/mm/pagemap_ioctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ void zeropfn_tests(void)
15251525

15261526
ret = madvise(mem, hpage_size, MADV_HUGEPAGE);
15271527
if (!ret) {
1528-
FORCE_READ(mem);
1528+
FORCE_READ(*mem);
15291529

15301530
ret = pagemap_ioctl(mem, hpage_size, &vec, 1, 0,
15311531
0, PAGE_IS_PFNZERO, 0, 0, PAGE_IS_PFNZERO);

tools/testing/selftests/mm/split_huge_page_test.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,11 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,
439439
}
440440
madvise(*addr, fd_size, MADV_HUGEPAGE);
441441

442-
for (size_t i = 0; i < fd_size; i++)
443-
FORCE_READ((*addr + i));
442+
for (size_t i = 0; i < fd_size; i++) {
443+
char *addr2 = *addr + i;
444+
445+
FORCE_READ(*addr2);
446+
}
444447

445448
if (!check_huge_file(*addr, fd_size / pmd_pagesize, pmd_pagesize)) {
446449
ksft_print_msg("No large pagecache folio generated, please provide a filesystem supporting large folio\n");

tools/testing/selftests/mm/vm_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* anything with it in order to trigger a read page fault. We therefore must use
2424
* volatile to stop the compiler from optimising this away.
2525
*/
26-
#define FORCE_READ(x) (*(volatile typeof(x) *)x)
26+
#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))
2727

2828
extern unsigned int __page_size;
2929
extern unsigned int __page_shift;

0 commit comments

Comments
 (0)