From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D6D8C433EF for ; Wed, 27 Oct 2021 20:10:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B20F760234 for ; Wed, 27 Oct 2021 20:10:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B20F760234 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 283816B0072; Wed, 27 Oct 2021 16:10:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 20C706B0073; Wed, 27 Oct 2021 16:10:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0AD9C940007; Wed, 27 Oct 2021 16:10:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0193.hostedemail.com [216.40.44.193]) by kanga.kvack.org (Postfix) with ESMTP id D46DE6B0072 for ; Wed, 27 Oct 2021 16:10:55 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 59EA739F3B for ; Wed, 27 Oct 2021 20:10:55 +0000 (UTC) X-FDA: 78743310870.37.D8793BA Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf20.hostedemail.com (Postfix) with ESMTP id D4C70D000641 for ; Wed, 27 Oct 2021 20:10:48 +0000 (UTC) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 58FB51FD3D; Wed, 27 Oct 2021 20:10:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1635365453; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=hW3UyAAqPRUl0z16nqyHfbiF0xI1dmNAdSdUTNu03Zk=; b=Ug1WIxJfxXX71Y2crVlfwZij9ycoD7jFrvvYrRhlcKGq6/XKBfMVPvnQNsg4w5IX0Xl4f3 lshS7w512IK/QUcRi8V4QC9g25SmmuGDf473cZpqGHYIB1MyUcSsrAOJKGGLngr6Tv77cK MkkXnJvG+M/1ktmedNiHywPCvdZ6KSU= Received: from suse.cz (unknown [10.100.224.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 31FD9A3B84; Wed, 27 Oct 2021 20:10:52 +0000 (UTC) Date: Wed, 27 Oct 2021 22:10:49 +0200 From: Petr Mladek To: Kees Cook Cc: Yafang Shao , akpm@linux-foundation.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Andrii Nakryiko Subject: Re: [PATCH v6 12/12] kernel/kthread: show a warning if kthread's comm is truncated Message-ID: References: <20211025083315.4752-1-laoar.shao@gmail.com> <20211025083315.4752-13-laoar.shao@gmail.com> <202110251431.F594652F@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202110251431.F594652F@keescook> X-Stat-Signature: yhdia1k8t7xfbf8u4wp75xik4bwie9rc X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: D4C70D000641 Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=suse.com header.s=susede1 header.b=Ug1WIxJf; dmarc=pass (policy=quarantine) header.from=suse.com; spf=pass (imf20.hostedemail.com: domain of pmladek@suse.com designates 195.135.220.29 as permitted sender) smtp.mailfrom=pmladek@suse.com X-HE-Tag: 1635365448-387847 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 > > Signed-off-by: Yafang Shao > > Reviewed-by: Kees Cook > > Cc: Mathieu Desnoyers > > Cc: Arnaldo Carvalho de Melo > > Cc: Andrii Nakryiko > > Cc: Peter Zijlstra > > Cc: Steven Rostedt > > Cc: Al Viro > > Cc: Kees Cook > > Cc: Petr Mladek > > --- > > 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