Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable

From: Pankaj Raghav (Samsung)
Date: Thu Jun 06 2024 - 11:48:18 EST


On Thu, Jun 06, 2024 at 03:35:31PM +0100, Matthew Wilcox wrote:
> On Thu, Jun 06, 2024 at 01:58:35PM +0000, Pankaj Raghav (Samsung) wrote:
> > +++ b/tools/testing/selftests/mm/split_huge_page_test.c
> > @@ -300,7 +300,7 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,
> > char **addr)
> > {
> > size_t i;
> > - int __attribute__((unused)) dummy = 0;
> > + volatile int __attribute__((unused)) dummy = 0;
>
> The mistake made by whoever wrote this test was making 'dummy' a stack
> variable. That lets the compiler figure out that it's unused. If you
> make it a top-level variable (not static) so the compiler can't tell
> whether it's referenced by a different compilation unit, it can't make
> that deduction. And you don't need the stupid attibute or volatile on it.

I did cringe a little before adding the volatile keyword. While not a
fan of global variables, that might be better than all these keywords.

I will send a v2 right away! Thanks.

--
Pankaj