linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] uprobes: get RCU trace lock before list iteration
@ 2024-11-07 17:14 Breno Leitao
  2024-11-07 18:14 ` Andrii Nakryiko
  2024-11-08  9:00 ` Peter Zijlstra
  0 siblings, 2 replies; 5+ messages in thread
From: Breno Leitao @ 2024-11-07 17:14 UTC (permalink / raw)
  To: Masami Hiramatsu, Oleg Nesterov, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	Andrii Nakryiko
  Cc: linux-perf-users, linux-kernel, linux-trace-kernel, kernel-team,
	Breno Leitao

Acquire RCU trace lock in filter_chain() to protect
list_for_each_entry_rcu() iteration, protecting the list iteration in a
RCU read section.

Prior to this fix, list_for_each_entry_srcu() was called without holding
the required lock, triggering warnings when RCU_PROVING is enabled:

	kernel/events/uprobes.c:937 RCU-list traversed without holding the required lock!!

Signed-off-by: Breno Leitao <leitao@debian.org>
Fixes: cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
---
 kernel/events/uprobes.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -1103,11 +1103,13 @@ static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm)
 	bool ret = false;
 
 	down_read(&uprobe->consumer_rwsem);
+	rcu_read_lock_trace();
 	list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) {
 		ret = consumer_filter(uc, mm);
 		if (ret)
 			break;
 	}
+	rcu_read_unlock_trace();
 	up_read(&uprobe->consumer_rwsem);
 
 	return ret;

---
base-commit: 5b913f5d7d7fe0f567dea8605f21da6eaa1735fb
change-id: 20241107-rcu_probe-bef660d84990

Best regards,
-- 
Breno Leitao <leitao@debian.org>


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

* Re: [PATCH] uprobes: get RCU trace lock before list iteration
  2024-11-07 17:14 [PATCH] uprobes: get RCU trace lock before list iteration Breno Leitao
@ 2024-11-07 18:14 ` Andrii Nakryiko
  2024-11-08  9:00 ` Peter Zijlstra
  1 sibling, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2024-11-07 18:14 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Masami Hiramatsu, Oleg Nesterov, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	Andrii Nakryiko, linux-perf-users, linux-kernel,
	linux-trace-kernel, kernel-team

On Thu, Nov 7, 2024 at 9:16 AM Breno Leitao <leitao@debian.org> wrote:
>
> Acquire RCU trace lock in filter_chain() to protect
> list_for_each_entry_rcu() iteration, protecting the list iteration in a
> RCU read section.
>
> Prior to this fix, list_for_each_entry_srcu() was called without holding
> the required lock, triggering warnings when RCU_PROVING is enabled:
>
>         kernel/events/uprobes.c:937 RCU-list traversed without holding the required lock!!
>
> Signed-off-by: Breno Leitao <leitao@debian.org>
> Fixes: cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
> ---
>  kernel/events/uprobes.c | 2 ++
>  1 file changed, 2 insertions(+)

LGTM, thanks

Reviewed-by: Andrii Nakryiko <andrii@kernel.org>

>
> diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644
> --- a/kernel/events/uprobes.c
> +++ b/kernel/events/uprobes.c
> @@ -1103,11 +1103,13 @@ static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm)
>         bool ret = false;
>
>         down_read(&uprobe->consumer_rwsem);
> +       rcu_read_lock_trace();
>         list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) {
>                 ret = consumer_filter(uc, mm);
>                 if (ret)
>                         break;
>         }
> +       rcu_read_unlock_trace();
>         up_read(&uprobe->consumer_rwsem);
>
>         return ret;
>
> ---
> base-commit: 5b913f5d7d7fe0f567dea8605f21da6eaa1735fb
> change-id: 20241107-rcu_probe-bef660d84990
>
> Best regards,
> --
> Breno Leitao <leitao@debian.org>
>

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

* Re: [PATCH] uprobes: get RCU trace lock before list iteration
  2024-11-07 17:14 [PATCH] uprobes: get RCU trace lock before list iteration Breno Leitao
  2024-11-07 18:14 ` Andrii Nakryiko
@ 2024-11-08  9:00 ` Peter Zijlstra
  2024-11-08 17:28   ` Andrii Nakryiko
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Zijlstra @ 2024-11-08  9:00 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Masami Hiramatsu, Oleg Nesterov, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	Andrii Nakryiko, linux-perf-users, linux-kernel,
	linux-trace-kernel, kernel-team

On Thu, Nov 07, 2024 at 09:14:45AM -0800, Breno Leitao wrote:
> Acquire RCU trace lock in filter_chain() to protect
> list_for_each_entry_rcu() iteration, protecting the list iteration in a
> RCU read section.
> 
> Prior to this fix, list_for_each_entry_srcu() was called without holding
> the required lock, triggering warnings when RCU_PROVING is enabled:
> 
> 	kernel/events/uprobes.c:937 RCU-list traversed without holding the required lock!!
> 
> Signed-off-by: Breno Leitao <leitao@debian.org>
> Fixes: cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
> ---
>  kernel/events/uprobes.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644
> --- a/kernel/events/uprobes.c
> +++ b/kernel/events/uprobes.c
> @@ -1103,11 +1103,13 @@ static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm)
>  	bool ret = false;
>  
>  	down_read(&uprobe->consumer_rwsem);
> +	rcu_read_lock_trace();
>  	list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) {

Maybe I'm confused, but isn't uprobe->consumer list protected by
uprobe->consumer_rwsem, which we hold for reading?

That is, AFAICT this is a false positive and we should be doing this
instead, no?


diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index a76ddc5fc982..a5405e9ef9f5 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -1104,7 +1104,7 @@ static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm)
 	bool ret = false;
 
 	down_read(&uprobe->consumer_rwsem);
-	list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) {
+	list_for_each_entry(uc, &uprobe->consumers, cons_node) {
 		ret = consumer_filter(uc, mm);
 		if (ret)
 			break;

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

* Re: [PATCH] uprobes: get RCU trace lock before list iteration
  2024-11-08  9:00 ` Peter Zijlstra
@ 2024-11-08 17:28   ` Andrii Nakryiko
  2024-11-08 17:34     ` Breno Leitao
  0 siblings, 1 reply; 5+ messages in thread
From: Andrii Nakryiko @ 2024-11-08 17:28 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Breno Leitao, Masami Hiramatsu, Oleg Nesterov, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	Andrii Nakryiko, linux-perf-users, linux-kernel,
	linux-trace-kernel, kernel-team

On Fri, Nov 8, 2024 at 1:00 AM Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Thu, Nov 07, 2024 at 09:14:45AM -0800, Breno Leitao wrote:
> > Acquire RCU trace lock in filter_chain() to protect
> > list_for_each_entry_rcu() iteration, protecting the list iteration in a
> > RCU read section.
> >
> > Prior to this fix, list_for_each_entry_srcu() was called without holding
> > the required lock, triggering warnings when RCU_PROVING is enabled:
> >
> >       kernel/events/uprobes.c:937 RCU-list traversed without holding the required lock!!
> >
> > Signed-off-by: Breno Leitao <leitao@debian.org>
> > Fixes: cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
> > ---
> >  kernel/events/uprobes.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> > index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644
> > --- a/kernel/events/uprobes.c
> > +++ b/kernel/events/uprobes.c
> > @@ -1103,11 +1103,13 @@ static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm)
> >       bool ret = false;
> >
> >       down_read(&uprobe->consumer_rwsem);
> > +     rcu_read_lock_trace();
> >       list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) {
>
> Maybe I'm confused, but isn't uprobe->consumer list protected by
> uprobe->consumer_rwsem, which we hold for reading?
>
> That is, AFAICT this is a false positive and we should be doing this
> instead, no?

Yep, you are absolutely right. RCU-protected traversal is important
only for handler_chain() and handle_uretprobe_chain(). Here it's all
under lock, so no need for RCU protection.

>
>
> diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> index a76ddc5fc982..a5405e9ef9f5 100644
> --- a/kernel/events/uprobes.c
> +++ b/kernel/events/uprobes.c
> @@ -1104,7 +1104,7 @@ static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm)
>         bool ret = false;
>
>         down_read(&uprobe->consumer_rwsem);
> -       list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) {
> +       list_for_each_entry(uc, &uprobe->consumers, cons_node) {

Acked-by: Andrii Nakryiko <andrii@kernel.org>

>                 ret = consumer_filter(uc, mm);
>                 if (ret)
>                         break;

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

* Re: [PATCH] uprobes: get RCU trace lock before list iteration
  2024-11-08 17:28   ` Andrii Nakryiko
@ 2024-11-08 17:34     ` Breno Leitao
  0 siblings, 0 replies; 5+ messages in thread
From: Breno Leitao @ 2024-11-08 17:34 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Peter Zijlstra, Masami Hiramatsu, Oleg Nesterov, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter,
	Andrii Nakryiko, linux-perf-users, linux-kernel,
	linux-trace-kernel, kernel-team

On Fri, Nov 08, 2024 at 09:28:17AM -0800, Andrii Nakryiko wrote:
> On Fri, Nov 8, 2024 at 1:00 AM Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > On Thu, Nov 07, 2024 at 09:14:45AM -0800, Breno Leitao wrote:
> > > Acquire RCU trace lock in filter_chain() to protect
> > > list_for_each_entry_rcu() iteration, protecting the list iteration in a
> > > RCU read section.
> > >
> > > Prior to this fix, list_for_each_entry_srcu() was called without holding
> > > the required lock, triggering warnings when RCU_PROVING is enabled:
> > >
> > >       kernel/events/uprobes.c:937 RCU-list traversed without holding the required lock!!
> > >
> > > Signed-off-by: Breno Leitao <leitao@debian.org>
> > > Fixes: cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
> > > ---
> > >  kernel/events/uprobes.c | 2 ++
> > >  1 file changed, 2 insertions(+)
> > >
> > > diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
> > > index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644
> > > --- a/kernel/events/uprobes.c
> > > +++ b/kernel/events/uprobes.c
> > > @@ -1103,11 +1103,13 @@ static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm)
> > >       bool ret = false;
> > >
> > >       down_read(&uprobe->consumer_rwsem);
> > > +     rcu_read_lock_trace();
> > >       list_for_each_entry_rcu(uc, &uprobe->consumers, cons_node, rcu_read_lock_trace_held()) {
> >
> > Maybe I'm confused, but isn't uprobe->consumer list protected by
> > uprobe->consumer_rwsem, which we hold for reading?
> >
> > That is, AFAICT this is a false positive and we should be doing this
> > instead, no?
> 
> Yep, you are absolutely right. RCU-protected traversal is important
> only for handler_chain() and handle_uretprobe_chain(). Here it's all
> under lock, so no need for RCU protection.

Thanks. I will update

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

end of thread, other threads:[~2024-11-08 17:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-07 17:14 [PATCH] uprobes: get RCU trace lock before list iteration Breno Leitao
2024-11-07 18:14 ` Andrii Nakryiko
2024-11-08  9:00 ` Peter Zijlstra
2024-11-08 17:28   ` Andrii Nakryiko
2024-11-08 17:34     ` Breno Leitao

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