History

kthread: dynamically allocate memory to store kthread's full name

When I was implementing a new per-cpu kthread cfs_migration, I found the comm of it "cfs_migration/%u" is truncated due to the limitation of TASK_COMM_LEN. For example, the comm of the percpu thread on CPU10~19 all have the same name "cfs_migration/1", which will confuse the user. This issue is not critical, because we can get the corresponding CPU from the task's Cpus_allowed. But for kthreads corresponding to other hardware devices, it is not easy to get the detailed device info from task comm, for example, jbd2/nvme0n1p2- xfs-reclaim/sdf Currently there are so many truncated kthreads: rcu_tasks_kthre rcu_tasks_rude_ rcu_tasks_trace poll_mpt3sas0_s ext4-rsv-conver xfs-reclaim/sd{a, b, c, ...} xfs-blockgc/sd{a, b, c, ...} xfs-inodegc/sd{a, b, c, ...} audit_send_repl ecryptfs-kthrea vfio-irqfd-clea jbd2/nvme0n1p2- ... We can shorten these names to work around this problem, but it may be not applied to all of the truncated kthreads. Take 'jbd2/nvme0n1p2-' for example, it is a nice name, and it is not a good idea to shorten it. One possible way to fix this issue is extending the task comm size, but as task->comm is used in lots of places, that may cause some potential buffer overflows. Another more conservative approach is introducing a new pointer to store kthread's full name if it is truncated, which won't introduce too much overhead as it is in the non-critical path. Finally we make a dicision to use the second approach. See also the discussions in this thread: https://lore.kernel.org/lkml/20211101060419.4682-1-laoar.shao@gmail.com/ After this change, the full name of these truncated kthreads will be displayed via /proc/[pid]/comm: rcu_tasks_kthread rcu_tasks_rude_kthread rcu_tasks_trace_kthread poll_mpt3sas0_statu ext4-rsv-conversion xfs-reclaim/sdf1 xfs-blockgc/sdf1 xfs-inodegc/sdf1 audit_send_reply ecryptfs-kthread vfio-irqfd-cleanup jbd2/nvme0n1p2-8

Yafang Shao committed 4 years ago

Commit d6986ce1 parent 3087c613 files · +34 −2Browse files

Unified · Split

3 files
  1. fs/proc/array.c +3 −0
  2. include/linux/kthread.h +1 −0
  3. kernel/kthread.c +30 −2
fs/proc/array.c+3−0
@@ -92,6 +92,7 @@ · 91 unmodified lines
9292 #include <linux/string_helpers.h>
9393 #include <linux/user_namespace.h>
9494 #include <linux/fs_struct.h>
95+#include <linux/kthread.h>
9596
9697 #include <asm/processor.h>
9798 #include "internal.h"
@@ -102,6 +103,8 @@ void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) · 4 unmodified lines
102103
103104 if (p->flags & PF_WQ_WORKER)
104105 wq_worker_comm(tcomm, sizeof(tcomm), p);
106+ else if (p->flags & PF_KTHREAD)
107+ get_kthread_comm(tcomm, sizeof(tcomm), p);
105108 else
106109 __get_task_comm(tcomm, sizeof(tcomm), p);
107110
include/linux/kthread.h+1−0
@@ -33,6 +33,7 @@ struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), · 32 unmodified lines
3333 unsigned int cpu,
3434 const char *namefmt);
3535
36+void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
3637 void set_kthread_struct(struct task_struct *p);
3738
3839 void kthread_set_per_cpu(struct task_struct *k, int cpu);
kernel/kthread.c+30−2
@@ -60,6 +60,8 @@ struct kthread { · 59 unmodified lines
6060 #ifdef CONFIG_BLK_CGROUP
6161 struct cgroup_subsys_state *blkcg_css;
6262 #endif
63+ /* To store the full name if task comm is truncated. */
64+ char *full_name;
6365 };
6466
6567 enum KTHREAD_BITS {
@@ -93,6 +95,18 @@ static inline struct kthread *__to_kthread(struct task_struct *p) · 27 unmodified lines
9395 return kthread;
9496 }
9597
98+void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk)
99+{
100+ struct kthread *kthread = to_kthread(tsk);
101+
102+ if (!kthread || !kthread->full_name) {
103+ __get_task_comm(buf, buf_size, tsk);
104+ return;
105+ }
106+
107+ strscpy_pad(buf, kthread->full_name, buf_size);
108+}
109+
96110 void set_kthread_struct(struct task_struct *p)
97111 {
98112 struct kthread *kthread;
@@ -118,9 +132,13 @@ void free_kthread_struct(struct task_struct *k) · 19 unmodified lines
118132 * or if kmalloc() in kthread() failed.
119133 */
120134 kthread = to_kthread(k);
135+ if (!kthread)
136+ return;
137+
121138 #ifdef CONFIG_BLK_CGROUP
122− WARN_ON_ONCE(kthread && kthread->blkcg_css);
139+ WARN_ON_ONCE(kthread->blkcg_css);
123140 #endif
141+ kfree(kthread->full_name);
124142 kfree(kthread);
125143 }
126144
@@ -406,12 +424,22 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), · 279 unmodified lines
406424 task = create->result;
407425 if (!IS_ERR(task)) {
408426 char name[TASK_COMM_LEN];
427+ va_list aq;
428+ int len;
409429
410430 /*
411431 * task is already visible to other tasks, so updating
412432 * COMM must be protected.
413433 */
414− vsnprintf(name, sizeof(name), namefmt, args);
434+ va_copy(aq, args);
435+ len = vsnprintf(name, sizeof(name), namefmt, aq);
436+ va_end(aq);
437+ if (len >= TASK_COMM_LEN) {
438+ struct kthread *kthread = to_kthread(task);
439+
440+ /* leave it truncated when out of memory. */
441+ kthread->full_name = kvasprintf(GFP_KERNEL, namefmt, args);
442+ }
415443 set_task_comm(task, name);
416444 }
417445 kfree(create);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t