From: David Laight <david.laight.linux@gmail.com>
To: Zi Yan <ziy@nvidia.com>
Cc: wang lian <lianux.mm@gmail.com>,
akpm@linux-foundation.org, broonie@kernel.org, david@redhat.com,
lorenzo.stoakes@oracle.com, sj@kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, brauner@kernel.org,
gkwang@linx-info.com, jannh@google.com, Liam.Howlett@oracle.com,
ludovico.zy.wu@gmail.com, p1ucky0923@gmail.com,
richard.weiyang@gmail.com, ryncsn@gmail.com, shuah@kernel.org,
vbabka@suse.cz, zijing.zhang@proton.me
Subject: Re: [PATCH 1/2] selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));"
Date: Thu, 7 Aug 2025 13:16:11 +0100 [thread overview]
Message-ID: <20250807131611.430a097a@pumpkin> (raw)
In-Reply-To: <22169C82-5701-4ABB-811F-075D22CE6FCD@nvidia.com>
On Tue, 05 Aug 2025 10:26:17 -0400
Zi Yan <ziy@nvidia.com> wrote:
> On 17 Jul 2025, at 9:18, wang lian wrote:
>
> > Several mm selftests use the `asm volatile("" : "+r" (variable));`
> > construct to force a read of a variable, preventing the compiler from
> > optimizing away the memory access. This idiom is cryptic and duplicated
> > across multiple test files.
> >
> > Following a suggestion from David[1], this patch refactors this
> > common pattern into a FORCE_READ() macro
> >
> > [1] https://lore.kernel.org/lkml/4a3e0759-caa1-4cfa-bc3f-402593f1eee3@redhat.com/
> >
> > Signed-off-by: wang lian <lianux.mm@gmail.com>
> > Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> > ---
> > tools/testing/selftests/mm/cow.c | 30 +++++++++----------
> > tools/testing/selftests/mm/guard-regions.c | 7 -----
> > tools/testing/selftests/mm/hugetlb-madvise.c | 5 +---
> > tools/testing/selftests/mm/migration.c | 13 ++++----
> > tools/testing/selftests/mm/pagemap_ioctl.c | 4 +--
> > .../selftests/mm/split_huge_page_test.c | 4 +--
> > tools/testing/selftests/mm/vm_util.h | 7 +++++
> > 7 files changed, 31 insertions(+), 39 deletions(-)
> >
>
> <snip>
>
> > diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
> > index f0d9c035641d..05de1fc0005b 100644
> > --- a/tools/testing/selftests/mm/split_huge_page_test.c
> > +++ b/tools/testing/selftests/mm/split_huge_page_test.c
> > @@ -399,7 +399,6 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,
> > char **addr)
> > {
> > size_t i;
> > - int dummy = 0;
> > unsigned char buf[1024];
> >
> > srand(time(NULL));
> > @@ -441,8 +440,7 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,
> > madvise(*addr, fd_size, MADV_HUGEPAGE);
> >
> > for (size_t i = 0; i < fd_size; i++)
> > - dummy += *(*addr + i);
> > - asm volatile("" : "+r" (dummy));
> > + FORCE_READ((*addr + i));
>
> I encountered a segfault when running the test on x86_64.
> i is 4194297 and fd_size is 4194304.
> It seems that FORCE_READ() is reading (*addr + i) in 8 byte size
> and i is only 7 bytes away from the end of the memory address.
> This led to segfault.
>
> (*(volatile char*)(*addr + i)); works fine.
>
> Both gcc-12 and gcc-14 have the issue.
The definition of FORCE_READ in 6.16 is:
#define FORCE_READ(x) (*(volatile typeof(x) *)x)
this is clearly bogus.
'x' is a pointer - follow it through.
Possibly:
#define FORCE_READ(x) (*(volatile typeof(*(x)) *)(x))
is better,
But why not use READ_ONCE(*addr[i]) ?
David
>
> >
> > if (!check_huge_file(*addr, fd_size / pmd_pagesize, pmd_pagesize)) {
> > ksft_print_msg("No large pagecache folio generated, please provide a filesystem supporting large folio\n");
> > diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
> > index 2b154c287591..c20298ae98ea 100644
> > --- a/tools/testing/selftests/mm/vm_util.h
> > +++ b/tools/testing/selftests/mm/vm_util.h
> > @@ -18,6 +18,13 @@
> > #define PM_SWAP BIT_ULL(62)
> > #define PM_PRESENT BIT_ULL(63)
> >
> > +/*
> > + * Ignore the checkpatch warning, we must read from x but don't want to do
> > + * anything with it in order to trigger a read page fault. We therefore must use
> > + * volatile to stop the compiler from optimising this away.
> > + */
> > +#define FORCE_READ(x) (*(volatile typeof(x) *)x)
> > +
>
> Also, look at FORCE_READ again, it converts x to a pointer to x and
> deferences x as a point. It does not seem right to me.
>
> Best Regards,
> Yan, Zi
>
next prev parent reply other threads:[~2025-08-07 12:16 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-17 13:18 [PATCH v2 RESEND 0/2] selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));" and some cleanup wang lian
2025-07-17 13:18 ` [PATCH 1/2] selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));" wang lian
2025-07-17 14:49 ` David Hildenbrand
2025-07-21 11:49 ` wang lian
2025-07-18 0:08 ` Wei Yang
2025-07-21 11:51 ` wang lian
2025-07-18 15:11 ` Zi Yan
2025-08-05 14:26 ` Zi Yan
2025-08-07 12:16 ` David Laight [this message]
2025-08-07 12:21 ` Zi Yan
2025-07-17 13:18 ` [PATCH 2/2] selftests/mm: guard-regions: Use SKIP() instead of ksft_exit_skip() wang lian
2025-07-17 14:17 ` Mark Brown
2025-07-18 0:13 ` Wei Yang
2025-07-18 15:11 ` Zi Yan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250807131611.430a097a@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=broonie@kernel.org \
--cc=david@redhat.com \
--cc=gkwang@linx-info.com \
--cc=jannh@google.com \
--cc=lianux.mm@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=ludovico.zy.wu@gmail.com \
--cc=p1ucky0923@gmail.com \
--cc=richard.weiyang@gmail.com \
--cc=ryncsn@gmail.com \
--cc=shuah@kernel.org \
--cc=sj@kernel.org \
--cc=vbabka@suse.cz \
--cc=zijing.zhang@proton.me \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox