Skip to contentSkip to conversation

kcmp: Comment get_file_raw_ptr() RCU usage

PATCH Superseded Jason Andryuk 2 participants 4 messages Feb 2, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kcmp: Comment get_file_raw_ptr() RCU usage

This usage of RCU appears wrong since the pointer is passed outside the RCU region. However, it is not dereferenced, so it is "okay". Leave a comment for the next reader.

Without a reference, these comparisons are racy, but even with their use inside an RCU region, the result could go stale.

---
I was looking for examples of task_lookup_fd_rcu()/files_lookup_fd_rcu() and found this. It differed from the example given in Documentation/filesystems/files.rst, so I was initially confused. A comment seemed appropriate to avoid confusion.

 kernel/kcmp.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kcmp.c +3 -0
index 5353edfad8e1..4fb23f242e0f 100644
@@ -63,6 +63,9 @@ get_file_raw_ptr(struct task_struct *task, unsigned int idx)
 {
 	struct file *file;
 
+	/* This RCU locking is only present to silence warnings.  The pointer
+	 * value is only used for comparison and not dereferenced, so it is
+	 * acceptable. */
 	rcu_read_lock();
 	file = task_lookup_fd_rcu(task, idx);
 	rcu_read_unlock();
Signature · 1 line
2.34.1
#1 lore raw
Quoted 22 lines · Jason Andryuk

This usage of RCU appears wrong since the pointer is passed outside the RCU region. However, it is not dereferenced, so it is "okay". Leave a comment for the next reader.

Without a reference, these comparisons are racy, but even with their use inside an RCU region, the result could go stale.

---
I was looking for examples of task_lookup_fd_rcu()/files_lookup_fd_rcu() and found this. It differed from the example given in Documentation/filesystems/files.rst, so I was initially confused. A comment seemed appropriate to avoid confusion.

 kernel/kcmp.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kcmp.c +0 -0
index 5353edfad8e1..4fb23f242e0f 100644

kernel/kcmp.c get_file_raw_ptr(struct task_struct *task, unsigned int idx) · line 71

Quoted 9 lines
kernel/kcmp.c +3 -0
@@ -63,6 +63,9 @@ get_file_raw_ptr(struct task_struct *task, unsigned int idx)
 {
 	struct file *file;
 
+	/* This RCU locking is only present to silence warnings.  The pointer
+	 * value is only used for comparison and not dereferenced, so it is
+	 * acceptable. */
 	rcu_read_lock();
 	file = task_lookup_fd_rcu(task, idx);
 	rcu_read_unlock();

They are not wrong, this is just such a bit weird semantics where we fetch the pointers and strictly speaking map them into numbers set to compare. But I agree that such tricks might confuse. How about

/*
* Fetching file pointers inside RCU read-lock section
* and reuse them as plain numbers is done in a sake
* of speed. But make sure never dereference them after. */

?

#2 lore raw
Quoted 38 lines · Cyrill Gorcunov
Quoted 22 lines · Jason Andryuk

This usage of RCU appears wrong since the pointer is passed outside the RCU region. However, it is not dereferenced, so it is "okay". Leave a comment for the next reader.

Without a reference, these comparisons are racy, but even with their use inside an RCU region, the result could go stale.

---
I was looking for examples of task_lookup_fd_rcu()/files_lookup_fd_rcu() and found this. It differed from the example given in Documentation/filesystems/files.rst, so I was initially confused. A comment seemed appropriate to avoid confusion.

 kernel/kcmp.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kcmp.c +0 -0
index 5353edfad8e1..4fb23f242e0f 100644

kernel/kcmp.c get_file_raw_ptr(struct task_struct *task, unsigned int idx) · line 71

Quoted 9 lines
kernel/kcmp.c +3 -0
@@ -63,6 +63,9 @@ get_file_raw_ptr(struct task_struct *task, unsigned int idx)
 {
      struct file *file;
 
+     /* This RCU locking is only present to silence warnings.  The pointer
+      * value is only used for comparison and not dereferenced, so it is
+      * acceptable. */
      rcu_read_lock();
      file = task_lookup_fd_rcu(task, idx);
      rcu_read_unlock();

They are not wrong, this is just such a bit weird semantics where we fetch the pointers and strictly speaking map them into numbers set to compare. But I agree that such tricks might confuse. How about

Quoted 5 lines

/*
* Fetching file pointers inside RCU read-lock section
* and reuse them as plain numbers is done in a sake
* of speed. But make sure never dereference them after. */

I would tweak it a little to "Fetch file pointers inside RCU read-lock section, but skip additional locking for speed. The pointer values will be used as integers, and must not be dereferenced."

One other idea I had was to switch the return value to "void *". That way it isn't a struct file, and it isn't readily dereference-able. But I wasn't sure if that would be overkill. What do you think?

Thanks,
Jason

#3 lore raw
Quoted 45 lines · Jason Andryuk
Quoted 38 lines · Cyrill Gorcunov
Quoted 22 lines · Jason Andryuk

This usage of RCU appears wrong since the pointer is passed outside the RCU region. However, it is not dereferenced, so it is "okay". Leave a comment for the next reader.

Without a reference, these comparisons are racy, but even with their use inside an RCU region, the result could go stale.

---
I was looking for examples of task_lookup_fd_rcu()/files_lookup_fd_rcu() and found this. It differed from the example given in Documentation/filesystems/files.rst, so I was initially confused. A comment seemed appropriate to avoid confusion.

 kernel/kcmp.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/kcmp.c +0 -0
index 5353edfad8e1..4fb23f242e0f 100644

kernel/kcmp.c get_file_raw_ptr(struct task_struct *task, unsigned int idx) · line 71

Quoted 9 lines
kernel/kcmp.c +3 -0
@@ -63,6 +63,9 @@ get_file_raw_ptr(struct task_struct *task, unsigned int idx)
 {
      struct file *file;
 
+     /* This RCU locking is only present to silence warnings.  The pointer
+      * value is only used for comparison and not dereferenced, so it is
+      * acceptable. */
      rcu_read_lock();
      file = task_lookup_fd_rcu(task, idx);
      rcu_read_unlock();

They are not wrong, this is just such a bit weird semantics where we fetch the pointers and strictly speaking map them into numbers set to compare. But I agree that such tricks might confuse. How about

Quoted 5 lines

/*
* Fetching file pointers inside RCU read-lock section
* and reuse them as plain numbers is done in a sake
* of speed. But make sure never dereference them after. */

Quoted 3 lines

I would tweak it a little to "Fetch file pointers inside RCU read-lock section, but skip additional locking for speed. The pointer values will be used as integers, and must not be dereferenced."

Up to you. Initially I use _raw_ptr suffix in function name trying to point out that the pointer obtained should not be considered in any way except a natural number. So I'm fine with any comment which helps readability.

Quoted 4 lines

One other idea I had was to switch the return value to "void *". That way it isn't a struct file, and it isn't readily dereference-able. But I wasn't sure if that would be overkill. What do you think?

Lets better stick with a comment I think. Thanks!