linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Petr Tesarik <ptesarik@suse.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Kees Cook <kees@kernel.org>, Jinbum Park <jinb.park7@gmail.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/1] mm/rodata_test: Use READ_ONCE() to read const variable
Date: Tue, 19 Nov 2024 12:38:30 +0100	[thread overview]
Message-ID: <20241119123830.7573897a@mordecai.tesarici.cz> (raw)
In-Reply-To: <20241119112009.1286675-1-ptesarik@suse.com>

Oops, this should have been a two-patch series. Will resend in a
minute. Sorry.

Petr T

On Tue, 19 Nov 2024 12:20:09 +0100
Petr Tesarik <ptesarik@suse.com> wrote:

> The C compiler may optimize away the memory read of a const variable if its
> value is known at compile time.
> 
> In particular, GCC14 with -O2 generates no code at all for test 1, and it
> generates the following x86_64 instructions for test 3:
> 
> 	cmpl	$195, 4(%rsp)
> 	je	.L14
> 
> That is, it replaces the read of rodata_test_data with an immediate value
> and compares it to the value of the local variable "zero".
> 
> Use READ_ONCE() to undo any such compiler optimizations and enforce a
> memory read.
> 
> Fixes: 2959a5f726f6 ("mm: add arch-independent testcases for RODATA")
> Signed-off-by: Petr Tesarik <ptesarik@suse.com>
> ---
>  mm/rodata_test.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/rodata_test.c b/mm/rodata_test.c
> index 6d783436951f..3b60425d80fe 100644
> --- a/mm/rodata_test.c
> +++ b/mm/rodata_test.c
> @@ -20,7 +20,7 @@ void rodata_test(void)
>  
>  	/* test 1: read the value */
>  	/* If this test fails, some previous testrun has clobbered the state */
> -	if (!rodata_test_data) {
> +	if (!READ_ONCE(rodata_test_data)) {
>  		pr_err("test 1 fails (start data)\n");
>  		return;
>  	}
> @@ -33,7 +33,7 @@ void rodata_test(void)
>  	}
>  
>  	/* test 3: check the value hasn't changed */
> -	if (rodata_test_data == zero) {
> +	if (READ_ONCE(rodata_test_data) == zero) {
>  		pr_err("test data was changed\n");
>  		return;
>  	}



      reply	other threads:[~2024-11-19 11:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-19 11:20 Petr Tesarik
2024-11-19 11:38 ` Petr Tesarik [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241119123830.7573897a@mordecai.tesarici.cz \
    --to=ptesarik@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=jinb.park7@gmail.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox