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
92#include <linux/string_helpers.h>92#include <linux/string_helpers.h>
93#include <linux/user_namespace.h>93#include <linux/user_namespace.h>
94#include <linux/fs_struct.h>94#include <linux/fs_struct.h>
95#include <linux/kthread.h>
95 96
96#include <asm/processor.h>97#include <asm/processor.h>
97#include "internal.h"98#include "internal.h"
@@ -102,6 +103,8 @@ void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) · 4 unmodified lines
102 103
103 if (p->flags & PF_WQ_WORKER)104 if (p->flags & PF_WQ_WORKER)
104 wq_worker_comm(tcomm, sizeof(tcomm), p);105 wq_worker_comm(tcomm, sizeof(tcomm), p);
106 else if (p->flags & PF_KTHREAD)
107 get_kthread_comm(tcomm, sizeof(tcomm), p);
105 else108 else
106 __get_task_comm(tcomm, sizeof(tcomm), p);109 __get_task_comm(tcomm, sizeof(tcomm), p);
107 110
include/linux/kthread.h+1−0
@@ -33,6 +33,7 @@ struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), · 32 unmodified lines
33 unsigned int cpu,33 unsigned int cpu,
34 const char *namefmt);34 const char *namefmt);
35 35
36void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
36void set_kthread_struct(struct task_struct *p);37void set_kthread_struct(struct task_struct *p);
37 38
38void kthread_set_per_cpu(struct task_struct *k, int cpu);39void kthread_set_per_cpu(struct task_struct *k, int cpu);
kernel/kthread.c+30−2
@@ -60,6 +60,8 @@ struct kthread { · 59 unmodified lines
60#ifdef CONFIG_BLK_CGROUP60#ifdef CONFIG_BLK_CGROUP
61 struct cgroup_subsys_state *blkcg_css;61 struct cgroup_subsys_state *blkcg_css;
62#endif62#endif
63 /* To store the full name if task comm is truncated. */
64 char *full_name;
63};65};
64 66
65enum KTHREAD_BITS {67enum KTHREAD_BITS {
@@ -93,6 +95,18 @@ static inline struct kthread *__to_kthread(struct task_struct *p) · 27 unmodified lines
93 return kthread;95 return kthread;
94}96}
95 97
98void 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
96void set_kthread_struct(struct task_struct *p)110void set_kthread_struct(struct task_struct *p)
97{111{
98 struct kthread *kthread;112 struct kthread *kthread;
@@ -118,9 +132,13 @@ void free_kthread_struct(struct task_struct *k) · 19 unmodified lines
118 * or if kmalloc() in kthread() failed.132 * or if kmalloc() in kthread() failed.
119 */133 */
120 kthread = to_kthread(k);134 kthread = to_kthread(k);
135 if (!kthread)
136 return;
137
121#ifdef CONFIG_BLK_CGROUP138#ifdef CONFIG_BLK_CGROUP
122 WARN_ON_ONCE(kthread && kthread->blkcg_css);139 WARN_ON_ONCE(kthread->blkcg_css);
123#endif140#endif
141 kfree(kthread->full_name);
124 kfree(kthread);142 kfree(kthread);
125}143}
126 144
@@ -406,12 +424,22 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), · 279 unmodified lines
406 task = create->result;424 task = create->result;
407 if (!IS_ERR(task)) {425 if (!IS_ERR(task)) {
408 char name[TASK_COMM_LEN];426 char name[TASK_COMM_LEN];
427 va_list aq;
428 int len;
409 429
410 /*430 /*
411 * task is already visible to other tasks, so updating431 * task is already visible to other tasks, so updating
412 * COMM must be protected.432 * COMM must be protected.
413 */433 */
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 }
415 set_task_comm(task, name);443 set_task_comm(task, name);
416 }444 }
417 kfree(create);445 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