Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
From: Matthew Wilcox
Date: Thu Jun 06 2024 - 10:59:22 EST
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.