Skip to contentSkip to conversation

selftests/mm: use volatile keyword to not optimize mmap read variable

PATCH Superseded Pankaj Raghav (Samsung) 4 participants 10 messages Jun 6, 2024

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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
index d3c7f5fb3e7b..c573a58f80ab 100644
@@ -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
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Pankaj Raghav (Samsung)

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.

Quoted 8 lines

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(-)

Best Regards,
Yan, Zi

#2 lore raw
In reply to Pankaj Raghav (Samsung)

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.c

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;

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.

reviewer
#3 lore raw
Quoted 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.c

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;

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

#4 lore raw
In reply to Matthew Wilcox
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.c

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;

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

#5 lore raw
In reply to Pankaj Raghav (Samsung)
Quoted 31 lines · Pankaj Raghav (Samsung)

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
index d3c7f5fb3e7b..c573a58f80ab 100644
@@ -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
#6 lore raw
Quoted 32 lines · David Hildenbrand
Quoted 31 lines · Pankaj Raghav (Samsung)

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
index d3c7f5fb3e7b..c573a58f80ab 100644
@@ -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: d97496ca23a2d4ee80b7302849404859d9058bcd

The rick we do in some other tests is:

char *tmp;

tmp = *whatever;
asm volatile("" : "+r" (tmp));

char tmp; of course. See cow.c as an example.

Signature · 3 lines
Cheers,

David / dhildenb
#7 lore raw
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() 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
index d3c7f5fb3e7b..c573a58f80ab 100644
@@ -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:

Quoted 6 lines
Quoted 4 lines

char *tmp;

tmp = *whatever;
asm volatile("" : "+r" (tmp));

char tmp; of course. See cow.c as 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
index d3c7f5fb3e7b..9c957703c1f7 100644
@@ -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);
+       asm volatile("" : "+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.

[1] https://lore.kernel.org/linux-mm/[email protected]/

#8 lore raw
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() 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
index d3c7f5fb3e7b..c573a58f80ab 100644
@@ -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:

Quoted 6 lines
Quoted 4 lines

char *tmp;

tmp = *whatever;
asm volatile("" : "+r" (tmp));

char tmp; of course. See cow.c as 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
index d3c7f5fb3e7b..9c957703c1f7 100644
@@ -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);
+       asm volatile("" : "+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
#9 lore raw
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() 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
index d3c7f5fb3e7b..c573a58f80ab 100644
@@ -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:

Quoted 6 lines
Quoted 4 lines

char *tmp;

tmp = *whatever;
asm volatile("" : "+r" (tmp));

char tmp; of course. See cow.c as 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
index d3c7f5fb3e7b..9c957703c1f7 100644
@@ -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);
+       asm volatile("" : "+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