linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Zheng Yejian <zhengyejian1@huawei.com>
Cc: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
	<laijs@cn.fujitsu.com>, <linux-kernel@vger.kernel.org>,
	<linux-trace-kernel@vger.kernel.org>
Subject: Re: [PATCH] tracing: Fix race when concurrently splice_read trace_pipe
Date: Fri, 11 Aug 2023 15:24:13 -0400	[thread overview]
Message-ID: <20230811152413.76d5b72e@gandalf.local.home> (raw)
In-Reply-To: <f7af687f-2376-fede-fa22-f776811c48f1@huawei.com>

On Fri, 11 Aug 2023 20:37:07 +0800
Zheng Yejian <zhengyejian1@huawei.com> wrote:

> On 2023/8/11 19:42, Masami Hiramatsu (Google) wrote:
> > On Thu, 10 Aug 2023 20:39:05 +0800
> > Zheng Yejian <zhengyejian1@huawei.com> wrote:
> >   
> >> When concurrently splice_read file trace_pipe and per_cpu/cpu*/trace_pipe,
> >> there are more data being read out than expected.
> >>
> >> The root cause is that in tracing_splice_read_pipe(), an entry is found
> >> outside locks, it may be read by multiple readers or consumed by other
> >> reader as starting printing it.
> >>
> >> To fix it, change to find entry after holding locks.
> >>
> >> Fixes: 7e53bd42d14c ("tracing: Consolidate protection of reader access to the ring buffer")
> >> Signed-off-by: Zheng Yejian <zhengyejian1@huawei.com>
> >> ---
> >>   kernel/trace/trace.c | 10 ++++++----
> >>   1 file changed, 6 insertions(+), 4 deletions(-)
> >>
> >> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> >> index b8870078ef58..f169d33b948f 100644
> >> --- a/kernel/trace/trace.c
> >> +++ b/kernel/trace/trace.c
> >> @@ -7054,14 +7054,16 @@ static ssize_t tracing_splice_read_pipe(struct file *filp,
> >>   	if (ret <= 0)
> >>   		goto out_err;
> >>   
> >> -	if (!iter->ent && !trace_find_next_entry_inc(iter)) {
> >> +	trace_event_read_lock();
> >> +	trace_access_lock(iter->cpu_file);
> >> +
> >> +	if (!trace_find_next_entry_inc(iter)) {  
> > 
> > It seems you skips '!iter->ent' check. Is there any reason for this change?  
> 
> IIUC, 'iter->ent' may be the entry that was found but not consumed
> in last call tracing_splice_read_pipe(), and in this call, 'iter->ent'
> may have being consumed, so we may should find a new 'iter->ent' before
> printing it in tracing_fill_pipe_page(), see following reduced codes:

And if it wasn't consumed? We just lost it?

> 
>    tracing_splice_read_pipe() {
>      if (!iter->ent && !trace_find_next_entry_inc(iter)) {  // 1. find 
> entry here
>          ... ...
>      }
>      tracing_fill_pipe_page() {
>        for (;;) {
>          ... ...
>          ret = print_trace_line(iter);  // 2. print entry
>          ... ...

You missed:

           count = trace_seq_used(&iter->seq) - save_len;
           if (rem < count) {
                rem = 0;
                iter->seq.seq.len = save_len;

Where the above just threw away what was printed in the above
"print_trace_line()", and it never went to console.

                break;
           }

-- Steve


>          if (!trace_find_next_entry_inc()) {  // 3. find next entry
>            ... ...
>            break;
>          }
>        }
> 
> --
> 
> Thanks,
> Zheng Yejian
> 
> > 
> > Thank you,
> >   
> >> +		trace_access_unlock(iter->cpu_file);
> >> +		trace_event_read_unlock();
> >>   		ret = -EFAULT;
> >>   		goto out_err;
> >>   	}
> >>   
> >> -	trace_event_read_lock();
> >> -	trace_access_lock(iter->cpu_file);
> >> -
> >>   	/* Fill as many pages as possible. */
> >>   	for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
> >>   		spd.pages[i] = alloc_page(GFP_KERNEL);
> >> -- 
> >> 2.25.1
> >>  
> > 
> >   


  reply	other threads:[~2023-08-11 19:24 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-10 12:39 [PATCH] tracing: Fix race when concurrently splice_read trace_pipe Zheng Yejian
2023-08-11 11:42 ` Masami Hiramatsu
2023-08-11 12:37   ` Zheng Yejian
2023-08-11 19:24     ` Steven Rostedt [this message]
2023-08-12  2:22       ` Zheng Yejian
2023-08-12 21:08         ` Steven Rostedt
2023-08-16 19:23         ` Steven Rostedt
2023-08-17 11:50           ` [RFC PATCH] tracing: Introduce pipe_cpumask to avoid race on trace_pipes Zheng Yejian
2023-08-17 14:13             ` Steven Rostedt
2023-08-18  1:38               ` Zheng Yejian
2023-08-18  1:43                 ` Steven Rostedt
2023-08-18  2:26             ` [PATCH v2] " Zheng Yejian
2023-08-18  5:03               ` Masami Hiramatsu
2023-08-18 13:41                 ` Steven Rostedt
2023-08-18 14:23                   ` Masami Hiramatsu
2023-08-18 15:53                     ` Steven Rostedt
2023-08-19  1:42                       ` Masami Hiramatsu
2023-08-20 13:18                         ` Masami Hiramatsu
2023-08-21  2:19                         ` Masami Hiramatsu
2023-08-21  2:33                           ` Steven Rostedt
2023-08-21  9:21                             ` Masami Hiramatsu
2023-08-21 15:17                         ` Steven Rostedt
2023-08-21 22:32                           ` Masami Hiramatsu
2023-08-11 19:25 ` [PATCH] tracing: Fix race when concurrently splice_read trace_pipe Steven Rostedt
2023-08-12  1:45   ` Zheng Yejian
2023-08-12 20:47     ` Masami Hiramatsu
2023-08-12  7:38   ` Zheng Yejian
2023-08-13  1:13     ` Steven Rostedt
2023-08-13 16:41       ` Linus Torvalds
2023-08-14 20:16         ` Steven Rostedt

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=20230811152413.76d5b72e@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=laijs@cn.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=zhengyejian1@huawei.com \
    /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;
as well as URLs for NNTP newsgroup(s).