Message ID | 20211025083315.4752-13-laoar.shao@gmail.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | extend task comm from 16 to 24 | expand |
Context | Check | Description |
---|---|---|
netdev/tree_selection | success | Not a local patch |
bpf/vmtest-bpf-next | success | VM_Test |
bpf/vmtest-bpf-next-PR | success | PR summary |
bpf/vmtest-bpf | success | VM_Test |
bpf/vmtest-bpf-PR | success | PR summary |
On Mon, Oct 25, 2021 at 08:33:15AM +0000, Yafang Shao wrote: > Show a warning if task comm is truncated. Below is the result > of my test case: > > truncated kthread comm:I-am-a-kthread-with-lon, pid:14 by 6 characters > > Suggested-by: Petr Mladek <pmladek@suse.com> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > Reviewed-by: Kees Cook <keescook@chromium.org> > Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> > Cc: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com> > Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com> > Cc: Peter Zijlstra <peterz@infradead.org> > Cc: Steven Rostedt <rostedt@goodmis.org> > Cc: Al Viro <viro@zeniv.linux.org.uk> > Cc: Kees Cook <keescook@chromium.org> > Cc: Petr Mladek <pmladek@suse.com> > --- > kernel/kthread.c | 7 ++++++- > 1 file changed, 6 insertions(+), 1 deletion(-) > > diff --git a/kernel/kthread.c b/kernel/kthread.c > index 5b37a8567168..46b924c92078 100644 > --- a/kernel/kthread.c > +++ b/kernel/kthread.c > @@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), > if (!IS_ERR(task)) { > static const struct sched_param param = { .sched_priority = 0 }; > char name[TASK_COMM_LEN]; > + int len; > > /* > * task is already visible to other tasks, so updating > * COMM must be protected. > */ > - vsnprintf(name, sizeof(name), namefmt, args); > + len = vsnprintf(name, sizeof(name), namefmt, args); > + if (len >= TASK_COMM_LEN) { And since this failure case is slow-path, we could improve the warning as other had kind of suggested earlier with something like this instead: char *full_comm; full_comm = kvasprintf(GFP_KERNEL, namefmt, args); pr_warn("truncated kthread comm '%s' to '%s' (pid:%d)\n", full_comm, name); kfree(full_comm); } > set_task_comm(task, name); > /* > * root may have changed our (kthreadd's) priority or CPU mask. > -- > 2.17.1 >
On Tue, Oct 26, 2021 at 5:35 AM Kees Cook <keescook@chromium.org> wrote: > > On Mon, Oct 25, 2021 at 08:33:15AM +0000, Yafang Shao wrote: > > Show a warning if task comm is truncated. Below is the result > > of my test case: > > > > truncated kthread comm:I-am-a-kthread-with-lon, pid:14 by 6 characters > > > > Suggested-by: Petr Mladek <pmladek@suse.com> > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > Reviewed-by: Kees Cook <keescook@chromium.org> > > Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> > > Cc: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com> > > Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com> > > Cc: Peter Zijlstra <peterz@infradead.org> > > Cc: Steven Rostedt <rostedt@goodmis.org> > > Cc: Al Viro <viro@zeniv.linux.org.uk> > > Cc: Kees Cook <keescook@chromium.org> > > Cc: Petr Mladek <pmladek@suse.com> > > --- > > kernel/kthread.c | 7 ++++++- > > 1 file changed, 6 insertions(+), 1 deletion(-) > > > > diff --git a/kernel/kthread.c b/kernel/kthread.c > > index 5b37a8567168..46b924c92078 100644 > > --- a/kernel/kthread.c > > +++ b/kernel/kthread.c > > @@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), > > if (!IS_ERR(task)) { > > static const struct sched_param param = { .sched_priority = 0 }; > > char name[TASK_COMM_LEN]; > > + int len; > > > > /* > > * task is already visible to other tasks, so updating > > * COMM must be protected. > > */ > > - vsnprintf(name, sizeof(name), namefmt, args); > > + len = vsnprintf(name, sizeof(name), namefmt, args); > > + if (len >= TASK_COMM_LEN) { > > And since this failure case is slow-path, we could improve the warning > as other had kind of suggested earlier with something like this instead: > It Makes sense to me. I will do it as you suggested. > char *full_comm; > > full_comm = kvasprintf(GFP_KERNEL, namefmt, args); > pr_warn("truncated kthread comm '%s' to '%s' (pid:%d)\n", > full_comm, name); > > kfree(full_comm); > } > > set_task_comm(task, name); > > /* > > * root may have changed our (kthreadd's) priority or CPU mask. > > -- > > 2.17.1 > > > > -- > Kees Cook
On Mon 2021-10-25 14:35:42, Kees Cook wrote: > On Mon, Oct 25, 2021 at 08:33:15AM +0000, Yafang Shao wrote: > > Show a warning if task comm is truncated. Below is the result > > of my test case: > > > > truncated kthread comm:I-am-a-kthread-with-lon, pid:14 by 6 characters > > > > Suggested-by: Petr Mladek <pmladek@suse.com> > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > Reviewed-by: Kees Cook <keescook@chromium.org> > > Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> > > Cc: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com> > > Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com> > > Cc: Peter Zijlstra <peterz@infradead.org> > > Cc: Steven Rostedt <rostedt@goodmis.org> > > Cc: Al Viro <viro@zeniv.linux.org.uk> > > Cc: Kees Cook <keescook@chromium.org> > > Cc: Petr Mladek <pmladek@suse.com> > > --- > > kernel/kthread.c | 7 ++++++- > > 1 file changed, 6 insertions(+), 1 deletion(-) > > > > diff --git a/kernel/kthread.c b/kernel/kthread.c > > index 5b37a8567168..46b924c92078 100644 > > --- a/kernel/kthread.c > > +++ b/kernel/kthread.c > > @@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), > > if (!IS_ERR(task)) { > > static const struct sched_param param = { .sched_priority = 0 }; > > char name[TASK_COMM_LEN]; > > + int len; > > > > /* > > * task is already visible to other tasks, so updating > > * COMM must be protected. > > */ > > - vsnprintf(name, sizeof(name), namefmt, args); > > + len = vsnprintf(name, sizeof(name), namefmt, args); > > + if (len >= TASK_COMM_LEN) { > > And since this failure case is slow-path, we could improve the warning > as other had kind of suggested earlier with something like this instead: > > char *full_comm; > > full_comm = kvasprintf(GFP_KERNEL, namefmt, args); You need to use va_copy()/va_end() if you want to use the same va_args twice. For example, see how kvasprintf() is implemented. It calls vsnprintf() twice and it uses va_copy()/va_end() around the the first call. kvasprintf() could also return NULL if there is not enough memory. > pr_warn("truncated kthread comm '%s' to '%s' (pid:%d)\n", > full_comm, name); BTW: Is this message printed during normal boot? I did not tried the patchset myself. We should add this warning only if there is a good solution how to avoid the truncated names. And we should me sure that the most common kthreads/workqueues do not trigger it. It would be ugly to print many warnings during boot if people could not get rid of them easily. > kfree(full_comm); > } > > set_task_comm(task, name); > > /* > > * root may have changed our (kthreadd's) priority or CPU mask. Best Regards, Petr
On Thu, Oct 28, 2021 at 4:10 AM Petr Mladek <pmladek@suse.com> wrote: > > On Mon 2021-10-25 14:35:42, Kees Cook wrote: > > On Mon, Oct 25, 2021 at 08:33:15AM +0000, Yafang Shao wrote: > > > Show a warning if task comm is truncated. Below is the result > > > of my test case: > > > > > > truncated kthread comm:I-am-a-kthread-with-lon, pid:14 by 6 characters > > > > > > Suggested-by: Petr Mladek <pmladek@suse.com> > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > > Reviewed-by: Kees Cook <keescook@chromium.org> > > > Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> > > > Cc: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com> > > > Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com> > > > Cc: Peter Zijlstra <peterz@infradead.org> > > > Cc: Steven Rostedt <rostedt@goodmis.org> > > > Cc: Al Viro <viro@zeniv.linux.org.uk> > > > Cc: Kees Cook <keescook@chromium.org> > > > Cc: Petr Mladek <pmladek@suse.com> > > > --- > > > kernel/kthread.c | 7 ++++++- > > > 1 file changed, 6 insertions(+), 1 deletion(-) > > > > > > diff --git a/kernel/kthread.c b/kernel/kthread.c > > > index 5b37a8567168..46b924c92078 100644 > > > --- a/kernel/kthread.c > > > +++ b/kernel/kthread.c > > > @@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), > > > if (!IS_ERR(task)) { > > > static const struct sched_param param = { .sched_priority = 0 }; > > > char name[TASK_COMM_LEN]; > > > + int len; > > > > > > /* > > > * task is already visible to other tasks, so updating > > > * COMM must be protected. > > > */ > > > - vsnprintf(name, sizeof(name), namefmt, args); > > > + len = vsnprintf(name, sizeof(name), namefmt, args); > > > + if (len >= TASK_COMM_LEN) { > > > > And since this failure case is slow-path, we could improve the warning > > as other had kind of suggested earlier with something like this instead: > > > > char *full_comm; > > > > full_comm = kvasprintf(GFP_KERNEL, namefmt, args); > > You need to use va_copy()/va_end() if you want to use the same va_args > twice. > > For example, see how kvasprintf() is implemented. It calls > vsnprintf() twice and it uses va_copy()/va_end() around the the first call. > Does it mean that if we want to call vsnprintf() three times, we must use va_copy()/va_end() around the first call and the second call ? IOW, if we call vsnprintf() multiple times, all the calls except the last call should be protected by va_copy()/va_end(). Actually I don't quite understand why we should do it like this. I will try to understand it, and appreciate it if you could explain it in detail. BTW, can we use va_copy()/va_end() in vsnprintf(), then the caller doesn't need to care how many times it will call vsnprintf(). > kvasprintf() could also return NULL if there is not enough memory. Right. We need to do the NULL check. > > > pr_warn("truncated kthread comm '%s' to '%s' (pid:%d)\n", > > full_comm, name); > > BTW: Is this message printed during normal boot? I did not tried the > patchset myself. > Yes, it will be printed at boot time. > We should add this warning only if there is a good solution how to > avoid the truncated names. And we should me sure that the most common > kthreads/workqueues do not trigger it. It would be ugly to print many > warnings during boot if people could not get rid of them easily. > As we have extended task comm to 24, there's no such warning printed for the existing kthreads/workqueues. IOW, it will only print for the newly introduced one if it has a long name. That means this printing is under control. > > kfree(full_comm); > > } > > > set_task_comm(task, name); > > > /* > > > * root may have changed our (kthreadd's) priority or CPU mask. > > Best Regards, > Petr
On Thu, Oct 28, 2021 at 4:10 AM Petr Mladek <pmladek@suse.com> wrote: > > On Mon 2021-10-25 14:35:42, Kees Cook wrote: > > On Mon, Oct 25, 2021 at 08:33:15AM +0000, Yafang Shao wrote: > > > Show a warning if task comm is truncated. Below is the result > > > of my test case: > > > > > > truncated kthread comm:I-am-a-kthread-with-lon, pid:14 by 6 characters > > > > > > Suggested-by: Petr Mladek <pmladek@suse.com> > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com> > > > Reviewed-by: Kees Cook <keescook@chromium.org> > > > Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> > > > Cc: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com> > > > Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com> > > > Cc: Peter Zijlstra <peterz@infradead.org> > > > Cc: Steven Rostedt <rostedt@goodmis.org> > > > Cc: Al Viro <viro@zeniv.linux.org.uk> > > > Cc: Kees Cook <keescook@chromium.org> > > > Cc: Petr Mladek <pmladek@suse.com> > > > --- > > > kernel/kthread.c | 7 ++++++- > > > 1 file changed, 6 insertions(+), 1 deletion(-) > > > > > > diff --git a/kernel/kthread.c b/kernel/kthread.c > > > index 5b37a8567168..46b924c92078 100644 > > > --- a/kernel/kthread.c > > > +++ b/kernel/kthread.c > > > @@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), > > > if (!IS_ERR(task)) { > > > static const struct sched_param param = { .sched_priority = 0 }; > > > char name[TASK_COMM_LEN]; > > > + int len; > > > > > > /* > > > * task is already visible to other tasks, so updating > > > * COMM must be protected. > > > */ > > > - vsnprintf(name, sizeof(name), namefmt, args); > > > + len = vsnprintf(name, sizeof(name), namefmt, args); > > > + if (len >= TASK_COMM_LEN) { > > > > And since this failure case is slow-path, we could improve the warning > > as other had kind of suggested earlier with something like this instead: > > > > char *full_comm; > > > > full_comm = kvasprintf(GFP_KERNEL, namefmt, args); > > You need to use va_copy()/va_end() if you want to use the same va_args > twice. > Now I understand it. So the patch will be: diff --git a/kernel/kthread.c b/kernel/kthread.c index 5b37a8567168..c1ff67283725 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -399,12 +399,29 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), if (!IS_ERR(task)) { static const struct sched_param param = { .sched_priority = 0 }; char name[TASK_COMM_LEN]; + char *full_comm; + va_list aq; + int len; /* * task is already visible to other tasks, so updating * COMM must be protected. */ - vsnprintf(name, sizeof(name), namefmt, args); + va_copy(aq, args); + len = vsnprintf(name, sizeof(name), namefmt, aq); + va_end(aq); + if (len >= TASK_COMM_LEN) { + full_comm = kvasprintf(GFP_KERNEL, namefmt, args); + if (full_comm) { + pr_warn("truncated kthread comm '%s' to '%s' (pid:%d)\n", + full_comm, name, task->pid); + kfree(full_comm); + } else { + pr_warn("truncated kthread comm '%s' (pid:%d) by %d characters\n", + name, task->pid, len - TASK_COMM_LEN + 1); + + } + } set_task_comm(task, name); /* * root may have changed our (kthreadd's) priority or CPU mask. That seems a little overkill to me. I prefer to keep the v6 as-is. > For example, see how kvasprintf() is implemented. It calls > vsnprintf() twice and it uses va_copy()/va_end() around the the first call. > > kvasprintf() could also return NULL if there is not enough memory. > > > pr_warn("truncated kthread comm '%s' to '%s' (pid:%d)\n", > > full_comm, name); > > BTW: Is this message printed during normal boot? I did not tried the > patchset myself. > > We should add this warning only if there is a good solution how to > avoid the truncated names. And we should me sure that the most common > kthreads/workqueues do not trigger it. It would be ugly to print many > warnings during boot if people could not get rid of them easily. > > > kfree(full_comm); > > } > > > set_task_comm(task, name); > > > /* > > > * root may have changed our (kthreadd's) priority or CPU mask. > > Best Regards, > Petr
diff --git a/kernel/kthread.c b/kernel/kthread.c index 5b37a8567168..46b924c92078 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), if (!IS_ERR(task)) { static const struct sched_param param = { .sched_priority = 0 }; char name[TASK_COMM_LEN]; + int len; /* * task is already visible to other tasks, so updating * COMM must be protected. */ - vsnprintf(name, sizeof(name), namefmt, args); + len = vsnprintf(name, sizeof(name), namefmt, args); + if (len >= TASK_COMM_LEN) { + pr_warn("truncated kthread comm:%s, pid:%d by %d characters\n", + name, task->pid, len - TASK_COMM_LEN + 1); + } set_task_comm(task, name); /* * root may have changed our (kthreadd's) priority or CPU mask.