From: Zhen Ni <zhen.ni@easystack.cn>
To: akpm@linux-foundation.org, brauner@kernel.org, oleg@redhat.com,
tglx@linutronix.de, frederic@kernel.org,
richard.weiyang@gmail.com, zhangpeng.00@bytedance.com
Cc: linux-kernel@vger.kernel.org, Zhen Ni <zhen.ni@easystack.cn>
Subject: [PATCH] kernel/fork: Optimize by avoiding unnecessary locking for kernel threads
Date: Mon, 2 Dec 2024 17:10:55 +0800 [thread overview]
Message-ID: <20241202091055.49551-1-zhen.ni@easystack.cn> (raw)
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
next reply other threads:[~2024-12-02 9:11 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-02 9:10 Zhen Ni [this message]
2024-12-02 17:31 ` [PATCH] kernel/fork: Optimize by avoiding unnecessary locking for kernel threads Oleg Nesterov
-- strict thread matches above, loose matches on Subject: below --
2024-11-20 8:48 Zhen Ni
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241202091055.49551-1-zhen.ni@easystack.cn \
--to=zhen.ni@easystack.cn \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=frederic@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=oleg@redhat.com \
--cc=richard.weiyang@gmail.com \
--cc=tglx@linutronix.de \
--cc=zhangpeng.00@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox