linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
@ 2024-06-06 13:58 Pankaj Raghav (Samsung)
  2024-06-06 14:21 ` Zi Yan
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Pankaj Raghav (Samsung) @ 2024-06-06 13:58 UTC (permalink / raw)
  To: Shuah Khan, Andrew Morton
  Cc: willy, linux-kernel, linux-mm, kernel, gost.dev, mcgrof,
	linux-kselftest, Zi Yan, Pankaj Raghav

From: Pankaj Raghav <p.raghav@samsung.com>

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.

Signed-off-by: Pankaj Raghav <p.raghav@samsung.com>
---
 tools/testing/selftests/mm/split_huge_page_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
index d3c7f5fb3e7b..c573a58f80ab 100644
--- a/tools/testing/selftests/mm/split_huge_page_test.c
+++ 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;
 
 	srand(time(NULL));
 

base-commit: d97496ca23a2d4ee80b7302849404859d9058bcd
-- 
2.44.1



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
  2024-06-06 13:58 [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable Pankaj Raghav (Samsung)
@ 2024-06-06 14:21 ` Zi Yan
  2024-06-06 14:35 ` Matthew Wilcox
  2024-06-06 15:56 ` David Hildenbrand
  2 siblings, 0 replies; 10+ messages in thread
From: Zi Yan @ 2024-06-06 14:21 UTC (permalink / raw)
  To: Pankaj Raghav (Samsung)
  Cc: Shuah Khan, Andrew Morton, willy, linux-kernel, linux-mm,
	gost.dev, mcgrof, linux-kselftest, Pankaj Raghav

[-- Attachment #1: Type: text/plain, Size: 760 bytes --]

On 6 Jun 2024, at 6:58, Pankaj Raghav (Samsung) wrote:

> From: Pankaj Raghav <p.raghav@samsung.com>
>
> 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.
>
> Signed-off-by: Pankaj Raghav <p.raghav@samsung.com>
> ---
>  tools/testing/selftests/mm/split_huge_page_test.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>

Acked-by: Zi Yan <ziy@nvidia.com>

Best Regards,
Yan, Zi

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 854 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
  2024-06-06 13:58 [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable Pankaj Raghav (Samsung)
  2024-06-06 14:21 ` Zi Yan
@ 2024-06-06 14:35 ` Matthew Wilcox
  2024-06-06 15:19   ` Zi Yan
  2024-06-06 15:28   ` Pankaj Raghav (Samsung)
  2024-06-06 15:56 ` David Hildenbrand
  2 siblings, 2 replies; 10+ messages in thread
From: Matthew Wilcox @ 2024-06-06 14:35 UTC (permalink / raw)
  To: Pankaj Raghav (Samsung)
  Cc: Shuah Khan, Andrew Morton, linux-kernel, linux-mm, gost.dev,
	mcgrof, linux-kselftest, Zi Yan, Pankaj Raghav

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.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
  2024-06-06 14:35 ` Matthew Wilcox
@ 2024-06-06 15:19   ` Zi Yan
  2024-06-06 15:28   ` Pankaj Raghav (Samsung)
  1 sibling, 0 replies; 10+ messages in thread
From: Zi Yan @ 2024-06-06 15:19 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Pankaj Raghav (Samsung),
	Shuah Khan, Andrew Morton, linux-kernel, linux-mm, gost.dev,
	mcgrof, linux-kselftest, Pankaj Raghav

[-- Attachment #1: Type: text/plain, Size: 957 bytes --]

On 6 Jun 2024, at 7:35, 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

That was me. :(

> 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

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 854 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
  2024-06-06 14:35 ` Matthew Wilcox
  2024-06-06 15:19   ` Zi Yan
@ 2024-06-06 15:28   ` Pankaj Raghav (Samsung)
  1 sibling, 0 replies; 10+ messages in thread
From: Pankaj Raghav (Samsung) @ 2024-06-06 15:28 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Shuah Khan, Andrew Morton, linux-kernel, linux-mm, gost.dev,
	mcgrof, linux-kselftest, Zi Yan, Pankaj Raghav

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
  2024-06-06 13:58 [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable Pankaj Raghav (Samsung)
  2024-06-06 14:21 ` Zi Yan
  2024-06-06 14:35 ` Matthew Wilcox
@ 2024-06-06 15:56 ` David Hildenbrand
  2024-06-06 15:57   ` David Hildenbrand
  2 siblings, 1 reply; 10+ messages in thread
From: David Hildenbrand @ 2024-06-06 15:56 UTC (permalink / raw)
  To: Pankaj Raghav (Samsung), Shuah Khan, Andrew Morton
  Cc: willy, linux-kernel, linux-mm, gost.dev, mcgrof, linux-kselftest,
	Zi Yan, Pankaj Raghav

On 06.06.24 15:58, Pankaj Raghav (Samsung) wrote:
> From: Pankaj Raghav <p.raghav@samsung.com>
> 
> 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.
> 
> Signed-off-by: Pankaj Raghav <p.raghav@samsung.com>
> ---
>   tools/testing/selftests/mm/split_huge_page_test.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
> index d3c7f5fb3e7b..c573a58f80ab 100644
> --- a/tools/testing/selftests/mm/split_huge_page_test.c
> +++ 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;
>   
>   	srand(time(NULL));
>   
> 
> base-commit: d97496ca23a2d4ee80b7302849404859d9058bcd

The rick we do in some other tests is:

char *tmp;

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


-- 
Cheers,

David / dhildenb



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
  2024-06-06 15:56 ` David Hildenbrand
@ 2024-06-06 15:57   ` David Hildenbrand
  2024-06-06 20:18     ` Pankaj Raghav (Samsung)
  0 siblings, 1 reply; 10+ messages in thread
From: David Hildenbrand @ 2024-06-06 15:57 UTC (permalink / raw)
  To: Pankaj Raghav (Samsung), Shuah Khan, Andrew Morton
  Cc: willy, linux-kernel, linux-mm, gost.dev, mcgrof, linux-kselftest,
	Zi Yan, Pankaj Raghav

On 06.06.24 17:56, David Hildenbrand wrote:
> On 06.06.24 15:58, Pankaj Raghav (Samsung) wrote:
>> From: Pankaj Raghav <p.raghav@samsung.com>
>>
>> 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.
>>
>> Signed-off-by: Pankaj Raghav <p.raghav@samsung.com>
>> ---
>>    tools/testing/selftests/mm/split_huge_page_test.c | 2 +-
>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
>> index d3c7f5fb3e7b..c573a58f80ab 100644
>> --- a/tools/testing/selftests/mm/split_huge_page_test.c
>> +++ 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;
>>    
>>    	srand(time(NULL));
>>    
>>
>> 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.

-- 
Cheers,

David / dhildenb



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
  2024-06-06 15:57   ` David Hildenbrand
@ 2024-06-06 20:18     ` Pankaj Raghav (Samsung)
  2024-06-06 20:21       ` David Hildenbrand
  0 siblings, 1 reply; 10+ messages in thread
From: Pankaj Raghav (Samsung) @ 2024-06-06 20:18 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Shuah Khan, Andrew Morton, willy, linux-kernel, linux-mm,
	gost.dev, mcgrof, linux-kselftest, Zi Yan, Pankaj Raghav

On Thu, Jun 06, 2024 at 05:57:21PM +0200, David Hildenbrand wrote:
> On 06.06.24 17:56, David Hildenbrand wrote:
> > On 06.06.24 15:58, Pankaj Raghav (Samsung) wrote:
> > > From: Pankaj Raghav <p.raghav@samsung.com>
> > > 
> > > 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.
> > > 
> > > Signed-off-by: Pankaj Raghav <p.raghav@samsung.com>
> > > ---
> > >    tools/testing/selftests/mm/split_huge_page_test.c | 2 +-
> > >    1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
> > > index d3c7f5fb3e7b..c573a58f80ab 100644
> > > --- a/tools/testing/selftests/mm/split_huge_page_test.c
> > > +++ 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;
> > >    	srand(time(NULL));
> > > 
> > > 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.
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 :)

diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
index d3c7f5fb3e7b..9c957703c1f7 100644
--- a/tools/testing/selftests/mm/split_huge_page_test.c
+++ b/tools/testing/selftests/mm/split_huge_page_test.c
@@ -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/20240606154428.672643-1-kernel@pankajraghav.com/


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
  2024-06-06 20:18     ` Pankaj Raghav (Samsung)
@ 2024-06-06 20:21       ` David Hildenbrand
  2024-06-06 20:30         ` Pankaj Raghav (Samsung)
  0 siblings, 1 reply; 10+ messages in thread
From: David Hildenbrand @ 2024-06-06 20:21 UTC (permalink / raw)
  To: Pankaj Raghav (Samsung)
  Cc: Shuah Khan, Andrew Morton, willy, linux-kernel, linux-mm,
	gost.dev, mcgrof, linux-kselftest, Zi Yan, Pankaj Raghav

On 06.06.24 22:18, Pankaj Raghav (Samsung) wrote:
> On Thu, Jun 06, 2024 at 05:57:21PM +0200, David Hildenbrand wrote:
>> On 06.06.24 17:56, David Hildenbrand wrote:
>>> On 06.06.24 15:58, Pankaj Raghav (Samsung) wrote:
>>>> From: Pankaj Raghav <p.raghav@samsung.com>
>>>>
>>>> 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.
>>>>
>>>> Signed-off-by: Pankaj Raghav <p.raghav@samsung.com>
>>>> ---
>>>>     tools/testing/selftests/mm/split_huge_page_test.c | 2 +-
>>>>     1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
>>>> index d3c7f5fb3e7b..c573a58f80ab 100644
>>>> --- a/tools/testing/selftests/mm/split_huge_page_test.c
>>>> +++ 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;
>>>>     	srand(time(NULL));
>>>>
>>>> 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.
> 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 :)
> 
> diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
> index d3c7f5fb3e7b..9c957703c1f7 100644
> --- a/tools/testing/selftests/mm/split_huge_page_test.c
> +++ b/tools/testing/selftests/mm/split_huge_page_test.c
> @@ -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.

You can then even the "__attribute__((unused))", because the compile 
must assume that it is used inside the asm statement.

-- 
Cheers,

David / dhildenb



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable
  2024-06-06 20:21       ` David Hildenbrand
@ 2024-06-06 20:30         ` Pankaj Raghav (Samsung)
  0 siblings, 0 replies; 10+ messages in thread
From: Pankaj Raghav (Samsung) @ 2024-06-06 20:30 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Shuah Khan, Andrew Morton, willy, linux-kernel, linux-mm,
	gost.dev, mcgrof, linux-kselftest, Zi Yan, Pankaj Raghav

On Thu, Jun 06, 2024 at 10:21:51PM +0200, David Hildenbrand wrote:
> On 06.06.24 22:18, Pankaj Raghav (Samsung) wrote:
> > On Thu, Jun 06, 2024 at 05:57:21PM +0200, David Hildenbrand wrote:
> > > On 06.06.24 17:56, David Hildenbrand wrote:
> > > > On 06.06.24 15:58, Pankaj Raghav (Samsung) wrote:
> > > > > From: Pankaj Raghav <p.raghav@samsung.com>
> > > > > 
> > > > > 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.
> > > > > 
> > > > > Signed-off-by: Pankaj Raghav <p.raghav@samsung.com>
> > > > > ---
> > > > >     tools/testing/selftests/mm/split_huge_page_test.c | 2 +-
> > > > >     1 file changed, 1 insertion(+), 1 deletion(-)
> > > > > 
> > > > > diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
> > > > > index d3c7f5fb3e7b..c573a58f80ab 100644
> > > > > --- a/tools/testing/selftests/mm/split_huge_page_test.c
> > > > > +++ 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;
> > > > >     	srand(time(NULL));
> > > > > 
> > > > > 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.
> > 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 :)
> > 
> > diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c
> > index d3c7f5fb3e7b..9c957703c1f7 100644
> > --- a/tools/testing/selftests/mm/split_huge_page_test.c
> > +++ b/tools/testing/selftests/mm/split_huge_page_test.c
> > @@ -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.
> 
> 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.
> 
> -- 
> Cheers,
> 
> David / dhildenb
> 


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-06-06 20:31 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-06 13:58 [PATCH] selftests/mm: use volatile keyword to not optimize mmap read variable Pankaj Raghav (Samsung)
2024-06-06 14:21 ` Zi Yan
2024-06-06 14:35 ` Matthew Wilcox
2024-06-06 15:19   ` Zi Yan
2024-06-06 15:28   ` Pankaj Raghav (Samsung)
2024-06-06 15:56 ` David Hildenbrand
2024-06-06 15:57   ` David Hildenbrand
2024-06-06 20:18     ` Pankaj Raghav (Samsung)
2024-06-06 20:21       ` David Hildenbrand
2024-06-06 20:30         ` Pankaj Raghav (Samsung)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox