Skip to contentSkip to conversation

uprobes: avoid false lockdep splat in uprobe timer callback

PATCH Superseded Andrii Nakryiko 5 participants 7 messages Apr 3, 2025 – Apr 4, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 uprobes: avoid false lockdep splat in uprobe timer callback

Avoid a false-positive lockdep warning in PREEMPT_RT configuration when using write_seqcount_begin() in uprobe timer callback by using raw_write_* APIs. Uprobe's use of timer callback is guaranteed to not race with itself, and as such seqcount's insistence on having hardirqs disabled on the writer side is irrelevant. So switch to raw_ variants of seqcount API instead of disabling hardirqs unnecessarily.

Also, point out in the comments more explicitly why we use seqcount despite our reader side being rather simple and never retrying. We favor well-maintained kernel primitive in favor of open-coding our own memory barriers.

---
 kernel/events/uprobes.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
kernel/events/uprobes.c +11 -2
index 70c84b9d7be3..6d7e7da0fbbc 100644
@@ -1944,6 +1944,9 @@ static void free_ret_instance(struct uprobe_task *utask,
 	 * to-be-reused return instances for future uretprobes. If ri_timer()
 	 * happens to be running right now, though, we fallback to safety and
 	 * just perform RCU-delated freeing of ri.
+	 * Admittedly, this is a rather simple use of seqcount, but it nicely
+	 * abstracts away all the necessary memory barriers, so we use
+	 * a well-supported kernel primitive here.
 	 */
 	if (raw_seqcount_try_begin(&utask->ri_seqcount, seq)) {
 		/* immediate reuse of ri without RCU GP is OK */
@@ -2004,12 +2007,18 @@ static void ri_timer(struct timer_list *timer)
 	/* RCU protects return_instance from freeing. */
 	guard(rcu)();
 
-	write_seqcount_begin(&utask->ri_seqcount);
+	/* See free_ret_instance() for notes on seqcount use.
+	 * We also employ raw API variants to avoid lockdep false-positive
+	 * warning complaining about hardirqs not being disabled. We have
+	 * a guarantee that this timer callback won't race with itself, so no
+	 * need to disable hardirqs.
+	 */
+	raw_write_seqcount_begin(&utask->ri_seqcount);
 
 	for_each_ret_instance_rcu(ri, utask->return_instances)
 		hprobe_expire(&ri->hprobe, false);
 
-	write_seqcount_end(&utask->ri_seqcount);
+	raw_write_seqcount_end(&utask->ri_seqcount);
 }
 
 static struct uprobe_task *alloc_utask(void)
Signature · 1 line
2.47.1
#1 lore raw
Quoted 4 lines · Andrii Nakryiko

Avoid a false-positive lockdep warning in PREEMPT_RT configuration when using write_seqcount_begin() in uprobe timer callback by using raw_write_* APIs. Uprobe's use of timer callback is guaranteed to not race with itself, and as such seqcount's insistence on having hardirqs

preemption, not hardirqs

Quoted 7 lines

disabled on the writer side is irrelevant. So switch to raw_ variants of seqcount API instead of disabling hardirqs unnecessarily.

Also, point out in the comments more explicitly why we use seqcount despite our reader side being rather simple and never retrying. We favor well-maintained kernel primitive in favor of open-coding our own memory barriers.

Thank you.

Quoted 24 lines
---
 kernel/events/uprobes.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
kernel/events/uprobes.c +3 -0
index 70c84b9d7be3..6d7e7da0fbbc 100644
@@ -1944,6 +1944,9 @@ static void free_ret_instance(struct uprobe_task *utask,
 	 * to-be-reused return instances for future uretprobes. If ri_timer()
 	 * happens to be running right now, though, we fallback to safety and
 	 * just perform RCU-delated freeing of ri.
+	 * Admittedly, this is a rather simple use of seqcount, but it nicely
+	 * abstracts away all the necessary memory barriers, so we use
+	 * a well-supported kernel primitive here.
 	 */
 	if (raw_seqcount_try_begin(&utask->ri_seqcount, seq)) {
 		/* immediate reuse of ri without RCU GP is OK */

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer) · line 2011

Quoted 6 lines
kernel/events/uprobes.c +1 -1
@@ -2004,12 +2007,18 @@ static void ri_timer(struct timer_list *timer)
 	/* RCU protects return_instance from freeing. */
 	guard(rcu)();
 
-	write_seqcount_begin(&utask->ri_seqcount);
 
+	/* See free_ret_instance() for notes on seqcount use.

This is not a proper multi line comment.

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer) · line 2012

Quoted 2 lines
kernel/events/uprobes.c +2 -0
+	 * We also employ raw API variants to avoid lockdep false-positive
+	 * warning complaining about hardirqs not being disabled. We have

s/hardirqs/preemption. The warning is about missing disabled preemption.

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer) · line 2014

Quoted 2 lines
kernel/events/uprobes.c +2 -0
+	 * a guarantee that this timer callback won't race with itself, so no
+	 * need to disable hardirqs.

The timer can only be invoked once for a uprobe_task. Therefore there can only be one writer. The reader does not require an even sequence count so it is okay to remain preemptible on PREEMPT_RT.

Quoted 11 lines
kernel/events/uprobes.c +3 -1
+	 */
+	raw_write_seqcount_begin(&utask->ri_seqcount);
 
 	for_each_ret_instance_rcu(ri, utask->return_instances)
 		hprobe_expire(&ri->hprobe, false);
 
-	write_seqcount_end(&utask->ri_seqcount);
+	raw_write_seqcount_end(&utask->ri_seqcount);
 }
 

static struct uprobe_task *alloc_utask(void)

Sebastian

maintainer
#2 lore raw
Quoted 3 lines · On Thu, 3 Apr 2025 19:49:17 +0200 Sebastian Andrzej Siewior…

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer) · line 2010

Quoted 1 line
kernel/events/uprobes.c +1 -0
+	/* See free_ret_instance() for notes on seqcount use.  

This is not a proper multi line comment.

It's only proper in the networking code, but not the rest of the kernel.

-- Steve

#3 lore raw
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Thu, 3 Apr 2025 19:49:17 +0200 Sebastian Andrzej Siewior…

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer) · line 2010

Quoted 1 line
kernel/events/uprobes.c +1 -0
+	/* See free_ret_instance() for notes on seqcount use.  

This is not a proper multi line comment.

It's only proper in the networking code, but not the rest of the kernel.

I wasn't aware that uprobe is following networking standards here.

Quoted 1 line

-- Steve

Sebastian

maintainer
#4 lore raw
Quoted 11 lines · On Thu, 3 Apr 2025 19:56:19 +0200 Sebastian Andrzej Siewior…
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Thu, 3 Apr 2025 19:49:17 +0200 Sebastian Andrzej Siewior…

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer) · line 2010

Quoted 1 line
kernel/events/uprobes.c +1 -0
+	/* See free_ret_instance() for notes on seqcount use.    

This is not a proper multi line comment.

It's only proper in the networking code, but not the rest of the kernel.

I wasn't aware that uprobe is following networking standards here.

It's not, but I know that Andrii works a bit with the networking code.

-- Steve

#5 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 45 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Andrii Nakryiko

Avoid a false-positive lockdep warning in PREEMPT_RT configuration when using write_seqcount_begin() in uprobe timer callback by using raw_write_* APIs. Uprobe's use of timer callback is guaranteed to not race with itself, and as such seqcount's insistence on having hardirqs

preemption, not hardirqs

Quoted 7 lines

disabled on the writer side is irrelevant. So switch to raw_ variants of seqcount API instead of disabling hardirqs unnecessarily.

Also, point out in the comments more explicitly why we use seqcount despite our reader side being rather simple and never retrying. We favor well-maintained kernel primitive in favor of open-coding our own memory barriers.

Thank you.

Quoted 27 lines
---
 kernel/events/uprobes.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
kernel/events/uprobes.c +3 -0
index 70c84b9d7be3..6d7e7da0fbbc 100644
@@ -1944,6 +1944,9 @@ static void free_ret_instance(struct uprobe_task *utask,
       * to-be-reused return instances for future uretprobes. If ri_timer()
       * happens to be running right now, though, we fallback to safety and
       * just perform RCU-delated freeing of ri.
+      * Admittedly, this is a rather simple use of seqcount, but it nicely
+      * abstracts away all the necessary memory barriers, so we use
+      * a well-supported kernel primitive here.
       */
      if (raw_seqcount_try_begin(&utask->ri_seqcount, seq)) {
              /* immediate reuse of ri without RCU GP is OK */
@@ -2004,12 +2007,18 @@ static void ri_timer(struct timer_list *timer)
      /* RCU protects return_instance from freeing. */
      guard(rcu)();
 
Quoted 5 lines

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer)

Quoted 3 lines
kernel/events/uprobes.c +1 -1
-     write_seqcount_begin(&utask->ri_seqcount);
 
+     /* See free_ret_instance() for notes on seqcount use.

This is not a proper multi line comment.

yep, will fix; no, uprobe is not networking, this style is just ingrained in my brain from working in BPF code base for a while

Quoted 5 lines

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer) · line 2012

Quoted 3 lines
kernel/events/uprobes.c +2 -0
+      * We also employ raw API variants to avoid lockdep false-positive
+      * warning complaining about hardirqs not being disabled. We have

s/hardirqs/preemption. The warning is about missing disabled preemption.

Right, sorry, the this_cpu_read(hardirqs_enabled) part of the check in lockdep_assert_preemption_disabled() made too strong an impression on me :) Will fix.

Quoted 21 lines

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer) · line 2014

Quoted 3 lines
kernel/events/uprobes.c +2 -0
+      * a guarantee that this timer callback won't race with itself, so no
+      * need to disable hardirqs.

The timer can only be invoked once for a uprobe_task. Therefore there can only be one writer. The reader does not require an even sequence count so it is okay to remain preemptible on PREEMPT_RT.

Quoted 11 lines
kernel/events/uprobes.c +3 -1
+      */
+     raw_write_seqcount_begin(&utask->ri_seqcount);
 
      for_each_ret_instance_rcu(ri, utask->return_instances)
              hprobe_expire(&ri->hprobe, false);
 
-     write_seqcount_end(&utask->ri_seqcount);
+     raw_write_seqcount_end(&utask->ri_seqcount);
 }
 

static struct uprobe_task *alloc_utask(void)

Sebastian

#6 lore raw
Quoted 47 lines · Andrii Nakryiko
Quoted 45 lines · Sebastian Andrzej Siewior
Quoted 4 lines · Andrii Nakryiko

Avoid a false-positive lockdep warning in PREEMPT_RT configuration when using write_seqcount_begin() in uprobe timer callback by using raw_write_* APIs. Uprobe's use of timer callback is guaranteed to not race with itself, and as such seqcount's insistence on having hardirqs

preemption, not hardirqs

Quoted 7 lines

disabled on the writer side is irrelevant. So switch to raw_ variants of seqcount API instead of disabling hardirqs unnecessarily.

Also, point out in the comments more explicitly why we use seqcount despite our reader side being rather simple and never retrying. We favor well-maintained kernel primitive in favor of open-coding our own memory barriers.

Thank you.

Quoted 27 lines
---
 kernel/events/uprobes.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
kernel/events/uprobes.c +3 -0
index 70c84b9d7be3..6d7e7da0fbbc 100644
@@ -1944,6 +1944,9 @@ static void free_ret_instance(struct uprobe_task *utask,
       * to-be-reused return instances for future uretprobes. If ri_timer()
       * happens to be running right now, though, we fallback to safety and
       * just perform RCU-delated freeing of ri.
+      * Admittedly, this is a rather simple use of seqcount, but it nicely
+      * abstracts away all the necessary memory barriers, so we use
+      * a well-supported kernel primitive here.
       */
      if (raw_seqcount_try_begin(&utask->ri_seqcount, seq)) {
              /* immediate reuse of ri without RCU GP is OK */
@@ -2004,12 +2007,18 @@ static void ri_timer(struct timer_list *timer)
      /* RCU protects return_instance from freeing. */
      guard(rcu)();
 
Quoted 8 lines
Quoted 5 lines

kernel/events/uprobes.c static void ri_timer(struct timer_list *timer)

Quoted 3 lines
kernel/events/uprobes.c +1 -1
-     write_seqcount_begin(&utask->ri_seqcount);
 
+     /* See free_ret_instance() for notes on seqcount use.

This is not a proper multi line comment.

yep, will fix; no, uprobe is not networking, this style is just ingrained in my brain from working in BPF code base for a while

... and this example underlines why we've been asking the networking folks for years to use the standard Linux kernel coding style for comments, instead of creating this pointless noise & inconsistency.

Thanks,

Ingo