Skip to contentSkip to conversation

uprobes: get RCU trace lock before list iteration

PATCH In review Breno Leitao 3 participants 5 messages Nov 7, 2024 – Nov 8, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 uprobes: get RCU trace lock before list iteration

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!!

Fixes cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
---
 kernel/events/uprobes.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/events/uprobes.c +2 -0
index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644
@@ -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,

Signature · 1 line
Breno Leitao <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · 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:

Quoted 7 lines

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

Fixes cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
---
 kernel/events/uprobes.c | 2 ++
 1 file changed, 2 insertions(+)

LGTM, thanks

Quoted 28 lines
kernel/events/uprobes.c +2 -1
index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644
@@ -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 <[email protected]>

maintainer
#2 lore raw
In reply to Breno Leitao
Quoted 20 lines · 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!!

Fixes cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
---
 kernel/events/uprobes.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/events/uprobes.c +0 -0
index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644

kernel/events/uprobes.c static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm) · line 1107

Quoted 5 lines
kernel/events/uprobes.c +1 -0
@@ -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?

kernel/events/uprobes.c +1 -1
index a76ddc5fc982..a5405e9ef9f5 100644
@@ -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;
#3 lore raw
Quoted 31 lines · Peter Zijlstra
Quoted 20 lines · 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!!

Fixes cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
---
 kernel/events/uprobes.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/events/uprobes.c +0 -0
index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644

kernel/events/uprobes.c static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm) · line 1107

Quoted 5 lines
kernel/events/uprobes.c +1 -0
@@ -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?

Quoted 2 lines

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.

kernel/events/uprobes.c static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm) · line 1107

Quoted 12 lines
kernel/events/uprobes.c +1 -1
index a76ddc5fc982..a5405e9ef9f5 100644
@@ -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) {
Quoted 3 lines
kernel/events/uprobes.c +0 -0
                ret = consumer_filter(uc, mm);
                if (ret)
                        break;
#4 lore raw
Quoted 32 lines · Andrii Nakryiko
Quoted 31 lines · Peter Zijlstra
Quoted 20 lines · 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!!

Fixes cc01bd044e6a ("uprobes: travers uprobe's consumer list locklessly under SRCU protection")
---
 kernel/events/uprobes.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/events/uprobes.c +0 -0
index fa04b14a7d72353adc440742016b813da6c812d2..afdaa45a43ac3948f7983175eda808c989e8738a 100644

kernel/events/uprobes.c static bool filter_chain(struct uprobe *uprobe, struct mm_struct *mm) · line 1107

Quoted 5 lines
kernel/events/uprobes.c +1 -0
@@ -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?

Quoted 6 lines
Quoted 2 lines

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