* [PATCH] kernel/fork: Optimize by avoiding unnecessary locking for kernel threads
@ 2024-12-02 9:10 Zhen Ni
2024-12-02 17:31 ` Oleg Nesterov
0 siblings, 1 reply; 3+ messages in thread
From: Zhen Ni @ 2024-12-02 9:10 UTC (permalink / raw)
To: akpm, brauner, oleg, tglx, frederic, richard.weiyang,
zhangpeng.00
Cc: linux-kernel, Zhen Ni
Improves the function by checking if the task is a kernel thread
(PF_KTHREAD) before acquiring the task lock. Kernel threads do not
have an associated executable file, so the function now returns NULL
immediately in such cases. This change reduces unnecessary locking
overhead and simplifies the code logic, while maintaining consistent
behavior for regular tasks.
Signed-off-by: Zhen Ni <zhen.ni@easystack.cn>
---
kernel/fork.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/kernel/fork.c b/kernel/fork.c
index e58d27c05788..9fadde6c7c98 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1500,12 +1500,13 @@ struct file *get_task_exe_file(struct task_struct *task)
struct file *exe_file = NULL;
struct mm_struct *mm;
+ if (task->flags & PF_KTHREAD)
+ return NULL;
+
task_lock(task);
mm = task->mm;
- if (mm) {
- if (!(task->flags & PF_KTHREAD))
- exe_file = get_mm_exe_file(mm);
- }
+ if (mm)
+ exe_file = get_mm_exe_file(mm);
task_unlock(task);
return exe_file;
}
--
2.20.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] kernel/fork: Optimize by avoiding unnecessary locking for kernel threads
2024-12-02 9:10 [PATCH] kernel/fork: Optimize by avoiding unnecessary locking for kernel threads Zhen Ni
@ 2024-12-02 17:31 ` Oleg Nesterov
0 siblings, 0 replies; 3+ messages in thread
From: Oleg Nesterov @ 2024-12-02 17:31 UTC (permalink / raw)
To: Zhen Ni
Cc: akpm, brauner, tglx, frederic, richard.weiyang, zhangpeng.00,
linux-kernel
Already in -mm tree, please see
https://lore.kernel.org/all/20241119143526.704986-1-mjguzik@gmail.com/
Oleg.
On 12/02, Zhen Ni wrote:
>
> Improves the function by checking if the task is a kernel thread
> (PF_KTHREAD) before acquiring the task lock. Kernel threads do not
> have an associated executable file, so the function now returns NULL
> immediately in such cases. This change reduces unnecessary locking
> overhead and simplifies the code logic, while maintaining consistent
> behavior for regular tasks.
>
> Signed-off-by: Zhen Ni <zhen.ni@easystack.cn>
> ---
> kernel/fork.c | 9 +++++----
> 1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/fork.c b/kernel/fork.c
> index e58d27c05788..9fadde6c7c98 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -1500,12 +1500,13 @@ struct file *get_task_exe_file(struct task_struct *task)
> struct file *exe_file = NULL;
> struct mm_struct *mm;
>
> + if (task->flags & PF_KTHREAD)
> + return NULL;
> +
> task_lock(task);
> mm = task->mm;
> - if (mm) {
> - if (!(task->flags & PF_KTHREAD))
> - exe_file = get_mm_exe_file(mm);
> - }
> + if (mm)
> + exe_file = get_mm_exe_file(mm);
> task_unlock(task);
> return exe_file;
> }
> --
> 2.20.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH] kernel/fork: Optimize by avoiding unnecessary locking for kernel threads
@ 2024-11-20 8:48 Zhen Ni
0 siblings, 0 replies; 3+ messages in thread
From: Zhen Ni @ 2024-11-20 8:48 UTC (permalink / raw)
To: akpm, brauner, oleg, tglx, frederic, richard.weiyang,
zhangpeng.00
Cc: linux-kernel, Zhen Ni
Improves the function by checking if the task is a kernel thread
(PF_KTHREAD) before acquiring the task lock. Kernel threads do not
have an associated executable file, so the function now returns NULL
immediately in such cases. This change reduces unnecessary locking
overhead and simplifies the code logic, while maintaining consistent
behavior for regular tasks.
Signed-off-by: Zhen Ni <zhen.ni@easystack.cn>
---
kernel/fork.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/kernel/fork.c b/kernel/fork.c
index e58d27c05788..9fadde6c7c98 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1500,12 +1500,13 @@ struct file *get_task_exe_file(struct task_struct *task)
struct file *exe_file = NULL;
struct mm_struct *mm;
+ if (task->flags & PF_KTHREAD)
+ return NULL;
+
task_lock(task);
mm = task->mm;
- if (mm) {
- if (!(task->flags & PF_KTHREAD))
- exe_file = get_mm_exe_file(mm);
- }
+ if (mm)
+ exe_file = get_mm_exe_file(mm);
task_unlock(task);
return exe_file;
}
--
2.20.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-12-02 17:32 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-02 9:10 [PATCH] kernel/fork: Optimize by avoiding unnecessary locking for kernel threads Zhen Ni
2024-12-02 17:31 ` Oleg Nesterov
-- strict thread matches above, loose matches on Subject: below --
2024-11-20 8:48 Zhen Ni
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.