Skip to contentSkip to conversation

bpf: remove unnecessary loop in task_file_seq_get_next()

PATCH Applied Dan Carpenter 5 participants 5 messages Jul 4, 2024 – Jul 8, 2024

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

PATCH 1/1 bpf: remove unnecessary loop in task_file_seq_get_next()

After commit 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU") this loop always iterates exactly one time. Delete the for statement and pull the code in a tab.

---
 kernel/bpf/task_iter.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/bpf/task_iter.c +3 -6
index ec4e97c61eef..02aa9db8d796 100644
@@ -261,6 +261,7 @@ task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
 	u32 saved_tid = info->tid;
 	struct task_struct *curr_task;
 	unsigned int curr_fd = info->fd;
+	struct file *f;
 
 	/* If this function returns a non-NULL file object,
 	 * it held a reference to the task/file.
@@ -286,12 +287,8 @@ task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
 	}
 
 	rcu_read_lock();
-	for (;; curr_fd++) {
-		struct file *f;
-		f = task_lookup_next_fdget_rcu(curr_task, &curr_fd);
-		if (!f)
-			break;
-
+	f = task_lookup_next_fdget_rcu(curr_task, &curr_fd);
+	if (f) {
 		/* set info->fd */
 		info->fd = curr_fd;
 		rcu_read_unlock();
Signature · 1 line
2.43.0
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Dan Carpenter

After commit 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU") this loop always iterates exactly one time. Delete the for statement and pull the code in a tab.

lgtm

jirka

Quoted 34 lines
---
 kernel/bpf/task_iter.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/bpf/task_iter.c +3 -6
index ec4e97c61eef..02aa9db8d796 100644
@@ -261,6 +261,7 @@ task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
 	u32 saved_tid = info->tid;
 	struct task_struct *curr_task;
 	unsigned int curr_fd = info->fd;
+	struct file *f;
 
 	/* If this function returns a non-NULL file object,
 	 * it held a reference to the task/file.
@@ -286,12 +287,8 @@ task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
 	}
 
 	rcu_read_lock();
-	for (;; curr_fd++) {
-		struct file *f;
-		f = task_lookup_next_fdget_rcu(curr_task, &curr_fd);
-		if (!f)
-			break;
-
+	f = task_lookup_next_fdget_rcu(curr_task, &curr_fd);
+	if (f) {
 		/* set info->fd */
 		info->fd = curr_fd;
 		rcu_read_unlock();
Signature · 1 line
2.43.0
#4 lore raw
In reply to Dan Carpenter
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 8 lines · On Thu, 4 Jul 2024 10:19:19 -0500 you wrote:

After commit 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU") this loop always iterates exactly one time. Delete the for statement and pull the code in a tab.

---
 kernel/bpf/task_iter.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

Here is the summary with links:
- [bpf-next] bpf: remove unnecessary loop in task_file_seq_get_next()

    https://git.kernel.org/bpf/bpf-next/c/bc239eb271e5

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html