LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: akpm@linux-foundation.org, pmladek@suse.com,
	peterz@infradead.org, valentin.schneider@arm.com,
	keescook@chromium.org, mathieu.desnoyers@efficios.com,
	qiang.zhang@windriver.com, robdclark@chromium.org,
	viro@zeniv.linux.org.uk, christian@brauner.io,
	dietmar.eggemann@arm.com
Cc: linux-kernel@vger.kernel.org, Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH 4/5] kernel: increase the size of kthread's comm
Date: Wed, 29 Sep 2021 11:50:35 +0000	[thread overview]
Message-ID: <20210929115036.4851-5-laoar.shao@gmail.com> (raw)
In-Reply-To: <20210929115036.4851-1-laoar.shao@gmail.com>

Some of the kthreads' comm are trucated due to the limitation of
TASK_COMM_LEN, for example,

  rcu_tasks_kthre
  rcu_tasks_rude_
  rcu_tasks_trace
  ecryptfs-kthrea
  vfio-irqfd-clea
  ext4-rsv-conver
  jbd2/nvme0n1p2-
  ...

Besides the in-tree kthreads listed above, the out-of-tree kthreads may
also be trucated, for example,

  rtase_work_queu
  nvidia-modeset/
  UVM global queu
  UVM deferred re
  ...

That is not expected by the author of these kthreads.

This patch increases the size of ktread's comm from 16 to 24, which is
the same with workqueue's, to improve this situation. After this cahnge,
the name of kthread can be fully displayed in /proc/[pid]/comm,
for example,

  rcu_tasks_kthread
  rcu_tasks_rude_kthread
  rcu_tasks_trace_kthread
  ecryptfs-kthread
  vfio-irqfd-cleanup
  ext4-rsv-conversion
  jbd2/nvme0n1p2-8
  ...

Because there're only a few of kthreads, so it won't increase too much
memory.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 fs/exec.c             | 5 ++++-
 include/linux/sched.h | 2 ++
 kernel/fork.c         | 9 ++++++++-
 kernel/kthread.c      | 2 +-
 4 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 021c9dc727bc..4bf0501b7766 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1222,9 +1222,12 @@ EXPORT_SYMBOL_GPL(__get_task_comm);
 
 void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
 {
+	size_t size;
+
 	task_lock(tsk);
 	trace_task_rename(tsk, buf);
-	strscpy(tsk->comm, buf, TASK_COMM_LEN);
+	size = tsk->flags & PF_KTHREAD ? KTHREAD_COMM_LEN : TASK_COMM_LEN;
+	strscpy(tsk->comm, buf, size);
 	task_unlock(tsk);
 	perf_event_comm(tsk, exec);
 }
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 959eaef248fc..6b336eba4ff6 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -276,6 +276,8 @@ struct task_group;
 
 /* Task command name length: */
 #define TASK_COMM_LEN			16
+/* KTHREAD_COMM_LEN must be >= TASK_COMM_LEN */
+#define KTHREAD_COMM_LEN		24
 
 extern void scheduler_tick(void);
 
diff --git a/kernel/fork.c b/kernel/fork.c
index 227aec240501..a2939353383d 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -723,7 +723,14 @@ static void mmdrop_async(struct mm_struct *mm)
 
 static int task_comm_alloc(struct task_struct *p)
 {
-	p->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
+	size_t size;
+
+	/*
+	 * PF_KTHREAD may be cleared in exec, but the allocated memory can
+	 * be safely freed.
+	 */
+	size = p->flags & PF_KTHREAD ? KTHREAD_COMM_LEN : TASK_COMM_LEN;
+	p->comm = kzalloc(size, GFP_KERNEL);
 	if (!p->comm)
 		return -ENOMEM;
 
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 5b37a8567168..6def951c605a 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -398,7 +398,7 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
 	task = create->result;
 	if (!IS_ERR(task)) {
 		static const struct sched_param param = { .sched_priority = 0 };
-		char name[TASK_COMM_LEN];
+		char name[KTHREAD_COMM_LEN];
 
 		/*
 		 * task is already visible to other tasks, so updating
-- 
2.17.1


  parent reply	other threads:[~2021-09-29 11:51 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-29 11:50 [PATCH 0/5] kthread: " Yafang Shao
2021-09-29 11:50 ` [PATCH 1/5] kernel: replace sizeof(task->comm) with TASK_COMM_LEN Yafang Shao
2021-09-29 18:09   ` Kees Cook
2021-09-30 12:27     ` Yafang Shao
2021-10-03  3:31   ` Al Viro
2021-10-03 14:14     ` Yafang Shao
2021-09-29 11:50 ` [PATCH 2/5] kernel/fork: allocate task->comm dynamicly Yafang Shao
2021-09-29 13:08   ` Yafang Shao
2021-09-29 18:11   ` Kees Cook
2021-09-30 12:41     ` Yafang Shao
2021-09-30 14:51       ` Petr Mladek
2021-10-01 11:58         ` Yafang Shao
2021-09-29 11:50 ` [PATCH 3/5] kernel/sched: improve the BUILD_BUG_ON() in get_task_comm() Yafang Shao
2021-09-29 18:12   ` Kees Cook
2021-09-30 12:43     ` Yafang Shao
2021-09-29 11:50 ` Yafang Shao [this message]
2021-09-29 18:19   ` [PATCH 4/5] kernel: increase the size of kthread's comm Kees Cook
2021-09-30 12:53     ` Yafang Shao
2021-09-29 11:50 ` [PATCH 5/5] kernel/kthread: show a warning if kthread's comm is still trucated Yafang Shao
2021-09-29 18:20   ` Kees Cook
2021-09-30 12:54     ` Yafang Shao
2021-09-30 15:17   ` Petr Mladek
2021-10-01 11:59     ` Yafang Shao
2021-10-03  3:41 ` [PATCH 0/5] kthread: increase the size of kthread's comm Al Viro
2021-10-03 14:20   ` Yafang Shao

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=20210929115036.4851-5-laoar.shao@gmail.com \
    --to=laoar.shao@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=christian@brauner.io \
    --cc=dietmar.eggemann@arm.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=qiang.zhang@windriver.com \
    --cc=robdclark@chromium.org \
    --cc=valentin.schneider@arm.com \
    --cc=viro@zeniv.linux.org.uk \
    --subject='Re: [PATCH 4/5] kernel: increase the size of kthread'\''s comm' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).