mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Steven Rostedt <rostedt@goodmis.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Linux Trace Kernel <linux-trace-kernel@vger.kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>
Subject: Re: [PATCH] ring-buffer: Fix a race in rb_time_cmpxchg() for 32 bit archs
Date: Tue, 12 Dec 2023 14:39:29 -0500	[thread overview]
Message-ID: <572ab085-ce40-4110-9296-99b136582eba@efficios.com> (raw)
In-Reply-To: <20231212115301.7a9c9a64@gandalf.local.home>

On 2023-12-12 11:53, Steven Rostedt wrote:
> From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
> 
> Mathieu Desnoyers pointed out an issue in the rb_time_cmpxchg() for 32 bit
> architectures. That is:
> 
>   static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
>   {
> 	unsigned long cnt, top, bottom, msb;
> 	unsigned long cnt2, top2, bottom2, msb2;
> 	u64 val;
> 
> 	/* The cmpxchg always fails if it interrupted an update */
> 	 if (!__rb_time_read(t, &val, &cnt2))
> 		 return false;
> 
> 	 if (val != expect)
> 		 return false;
> 
> <<<< interrupted here!
> 
> 	 cnt = local_read(&t->cnt);
> 
> The problem is that the synchronization counter in the rb_time_t is read
> *after* the value of the timestamp is read. That means if an interrupt
> were to come in between the value being read and the counter being read,
> it can change the value and the counter and the interrupted process would
> be clueless about it!
> 
> The counter needs to be read first and then the value. That way it is easy
> to tell if the value is stale or not. If the counter hasn't been updated,
> then the value is still good.
> 
> Link: https://lore.kernel.org/linux-trace-kernel/20231211201324.652870-1-mathieu.desnoyers@efficios.com/
> 
> Cc: stable@vger.kernel.org
> Fixes: 10464b4aa605e ("ring-buffer: Add rb_time_t 64 bit operations for speeding up 32 bit")
> Reported-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

> ---
>   kernel/trace/ring_buffer.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
> index 1d9caee7f542..e110cde685ea 100644
> --- a/kernel/trace/ring_buffer.c
> +++ b/kernel/trace/ring_buffer.c
> @@ -706,6 +706,9 @@ static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
>   	unsigned long cnt2, top2, bottom2, msb2;
>   	u64 val;
>   
> +	/* Any interruptions in this function should cause a failure */
> +	cnt = local_read(&t->cnt);
> +
>   	/* The cmpxchg always fails if it interrupted an update */
>   	 if (!__rb_time_read(t, &val, &cnt2))
>   		 return false;
> @@ -713,7 +716,6 @@ static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
>   	 if (val != expect)
>   		 return false;
>   
> -	 cnt = local_read(&t->cnt);
>   	 if ((cnt & 3) != cnt2)
>   		 return false;
>   

-- 
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com


      reply	other threads:[~2023-12-12 19:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-12 16:53 Steven Rostedt
2023-12-12 19:39 ` Mathieu Desnoyers [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=572ab085-ce40-4110-9296-99b136582eba@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.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

all inboxes | Powered by JetHome®