selftests/mm: use volatile keyword to not optimize mmap read variable
Subsystems (4)
PATCH 1/1 selftests/mm: use volatile keyword to not optimize mmap read variable
From: Pankaj Raghav <[email protected]>
create_pagecache_thp_and_fd() in split_huge_page_test.c used the variable dummy to perform mmap read.
However, this test was skipped even on XFS which has large folio support. The issue was compiler (gcc 13.2.0) was optimizing out the dummy variable, therefore, not creating huge page in the page cache.
Add volatile keyword to force compiler not to optimize out the loop where we read from the mmaped addr.
--- tools/testing/selftests/mm/split_huge_page_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/mm/split_huge_page_test.c +1 -1
@@ -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 ;srand (time (NULL ));
base-commit: d97496ca23a2d4ee80b7302849404859d9058bcd
Signature · 1 line
2.44.1
Quoted 9 lines · Pankaj Raghav (Samsung)
From: Pankaj Raghav <[email protected]>
create_pagecache_thp_and_fd()insplit_huge_page_test.cused the variable dummy to perform mmap read.However, this test was skipped even on XFS which has large folio support. The issue was compiler (gcc 13.2.0) was optimizing out the dummy variable, therefore, not creating huge page in the page cache.
Quoted 8 lines
Add volatile keyword to force compiler not to optimize out the loop where we read from the mmaped addr.
Signed-off-by Pankaj Raghav <[email protected]>--- tools/testing/selftests/mm/split_huge_page_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Best Regards,
Yan, Zi
tools/testing/selftests/mm/split_huge_page_test.c int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd, · line 303
Quoted 7 lines · Pankaj Raghav (Samsung)
+++
b/tools/testing/selftests/mm/split_huge_page_test.ctools/testing/selftests/mm/split_huge_page_test.c +1
-1@@ -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.
Zi Yan
reviewerQuoted 10 lines · Matthew Wilcox
tools/testing/selftests/mm/split_huge_page_test.c int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd, · line 303
Quoted 7 lines · Pankaj Raghav (Samsung)
+++
b/tools/testing/selftests/mm/split_huge_page_test.ctools/testing/selftests/mm/split_huge_page_test.c +1
-1@@ -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
That was me. :(
Quoted 4 lines
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.
It is better to just move “dummy” like you suggested instead of adding more keywords to fix it.
-
Best Regards,
Yan, Zi
Quoted 14 lines · Matthew Wilcox
tools/testing/selftests/mm/split_huge_page_test.c int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd, · line 303
Quoted 7 lines · Pankaj Raghav (Samsung)
+++
b/tools/testing/selftests/mm/split_huge_page_test.ctools/testing/selftests/mm/split_huge_page_test.c +1
-1@@ -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
Quoted 31 lines · Pankaj Raghav (Samsung)
From: Pankaj Raghav <[email protected]>
create_pagecache_thp_and_fd()insplit_huge_page_test.cused the variable dummy to perform mmap read.However, this test was skipped even on XFS which has large folio support. The issue was compiler (gcc 13.2.0) was optimizing out the dummy variable, therefore, not creating huge page in the page cache.
Add volatile keyword to force compiler not to optimize out the loop where we read from the mmaped addr.
Signed-off-by Pankaj Raghav <[email protected]>--- tools/testing/selftests/mm/split_huge_page_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/split_huge_page_test.c +1
-1@@ -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 ;srand (time (NULL ));
Quoted 1 line
base-commit:
d97496ca23a2d4ee80b7302849404859d9058bcd
The rick we do in some other tests is:
char *tmp;
tmp = *whatever;
asm volatile("" : "+r" (tmp));
Signature · 3 lines
Cheers, David / dhildenb
Quoted 32 lines · David Hildenbrand
Quoted 31 lines · Pankaj Raghav (Samsung)
From: Pankaj Raghav <[email protected]>
create_pagecache_thp_and_fd()insplit_huge_page_test.cused the variable dummy to perform mmap read.However, this test was skipped even on XFS which has large folio support. The issue was compiler (gcc 13.2.0) was optimizing out the dummy variable, therefore, not creating huge page in the page cache.
Add volatile keyword to force compiler not to optimize out the loop where we read from the mmaped addr.
Signed-off-by Pankaj Raghav <[email protected]>--- tools/testing/selftests/mm/split_huge_page_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/split_huge_page_test.c +1
-1@@ -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 ;srand (time (NULL ));
Quoted 8 lines
Quoted 1 line
base-commit:
d97496ca23a2d4ee80b7302849404859d9058bcdThe rick we do in some other tests is:
char *tmp;
tmp = *whatever;
asmvolatile("" : "+r" (tmp));
char tmp; of course. See cow.c as an example.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 35 lines · David Hildenbrand
Quoted 34 lines · David Hildenbrand
Quoted 29 lines · Pankaj Raghav (Samsung)
From: Pankaj Raghav <[email protected]>
create_pagecache_thp_and_fd()insplit_huge_page_test.cused the variable dummy to perform mmap read.However, this test was skipped even on XFS which has large folio support. The issue was compiler (gcc 13.2.0) was optimizing out the dummy variable, therefore, not creating huge page in the page cache.
Add volatile keyword to force compiler not to optimize out the loop where we read from the mmaped addr.
Signed-off-by Pankaj Raghav <[email protected]>--- tools/testing/selftests/mm/split_huge_page_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/split_huge_page_test.c +1
-1@@ -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 ;srand (time (NULL ));Quoted 1 line
base-commit:
d97496ca23a2d4ee80b7302849404859d9058bcdThe rick we do in some other tests is:
Quoted 6 lines
Quoted 4 lines
char *tmp;
tmp = *whatever;
asmvolatile("" : "+r" (tmp));char tmp; of course. See
cow.cas an example.
Thanks David! I remember also seeing this when I grepped for volatile in the selftests directory.
Willy gave the idea of making it as a global variable [1]. But your trick also works :)
tools/testing/selftests/mm/split_huge_page_test.c +1 -0
@@ -341,6 +341,7 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,for (size_t i =0 ; i < fd_size; i++) dummy += *(*addr + i); + asmvolatile ("" :"+r" (dummy));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" );
I am fine with either solutions. But using the trick asm volatile is more cleaner than making it a global variable IMO and makes it more uniform across the other mm tests.
Let me know what others think.
Quoted 61 lines · Pankaj Raghav (Samsung)
Quoted 35 lines · David Hildenbrand
Quoted 34 lines · David Hildenbrand
Quoted 29 lines · Pankaj Raghav (Samsung)
From: Pankaj Raghav <[email protected]>
create_pagecache_thp_and_fd()insplit_huge_page_test.cused the variable dummy to perform mmap read.However, this test was skipped even on XFS which has large folio support. The issue was compiler (gcc 13.2.0) was optimizing out the dummy variable, therefore, not creating huge page in the page cache.
Add volatile keyword to force compiler not to optimize out the loop where we read from the mmaped addr.
Signed-off-by Pankaj Raghav <[email protected]>--- tools/testing/selftests/mm/split_huge_page_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/split_huge_page_test.c +1
-1@@ -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 ;srand (time (NULL ));Quoted 1 line
base-commit:
d97496ca23a2d4ee80b7302849404859d9058bcdThe rick we do in some other tests is:
Quoted 6 lines
Quoted 4 lines
char *tmp;
tmp = *whatever;
asmvolatile("" : "+r" (tmp));char tmp; of course. See
cow.cas an example.Thanks David! I remember also seeing this when I grepped for volatile in the selftests directory.
Willy gave the idea of making it as a global variable [1]. But your trick also works :)
tools/testing/selftests/mm/split_huge_page_test.c +1
-0@@ -341,6 +341,7 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,for (size_t i =0 ; i < fd_size; i++) dummy += *(*addr + i); + asmvolatile ("" :"+r" (dummy));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" );
Quoted 3 lines
I am fine with either solutions. But using the trick asm volatile is more cleaner than making it a global variable IMO and makes it more uniform across the other mm tests.
You can then even the "__attribute__((unused))", because the compile must assume that it is used inside the asm statement.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 60 lines · David Hildenbrand
Quoted 59 lines · Pankaj Raghav (Samsung)
Quoted 35 lines · David Hildenbrand
Quoted 34 lines · David Hildenbrand
Quoted 29 lines · Pankaj Raghav (Samsung)
From: Pankaj Raghav <[email protected]>
create_pagecache_thp_and_fd()insplit_huge_page_test.cused the variable dummy to perform mmap read.However, this test was skipped even on XFS which has large folio support. The issue was compiler (gcc 13.2.0) was optimizing out the dummy variable, therefore, not creating huge page in the page cache.
Add volatile keyword to force compiler not to optimize out the loop where we read from the mmaped addr.
Signed-off-by Pankaj Raghav <[email protected]>--- tools/testing/selftests/mm/split_huge_page_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/mm/split_huge_page_test.c +1
-1@@ -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 ;srand (time (NULL ));Quoted 1 line
base-commit:
d97496ca23a2d4ee80b7302849404859d9058bcdThe rick we do in some other tests is:
Quoted 6 lines
Quoted 4 lines
char *tmp;
tmp = *whatever;
asmvolatile("" : "+r" (tmp));char tmp; of course. See
cow.cas an example.Thanks David! I remember also seeing this when I grepped for volatile in the selftests directory.
Willy gave the idea of making it as a global variable [1]. But your trick also works :)
tools/testing/selftests/mm/split_huge_page_test.c +1
-0@@ -341,6 +341,7 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,for (size_t i =0 ; i < fd_size; i++) dummy += *(*addr + i); + asmvolatile ("" :"+r" (dummy));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" );
Quoted 6 lines
Quoted 3 lines
I am fine with either solutions. But using the trick asm volatile is more cleaner than making it a global variable IMO and makes it more uniform across the other mm tests.
You can then even the "__attribute__((unused))", because the compile must assume that it is used inside the asm statement.
Yup! I will send a new version. Thanks David.
Quoted 6 lines
Signature · 4 lines
-- Cheers, David / dhildenb