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 CCC10C43217 for ; Thu, 28 Oct 2021 01:43:31 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CB1F260E09 for ; Thu, 28 Oct 2021 01:43:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org CB1F260E09 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 5F5CF80007; Wed, 27 Oct 2021 21:43:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 57DFD6B0072; Wed, 27 Oct 2021 21:43:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46CF580007; Wed, 27 Oct 2021 21:43:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0194.hostedemail.com [216.40.44.194]) by kanga.kvack.org (Postfix) with ESMTP id 22C186B0071 for ; Wed, 27 Oct 2021 21:43:30 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A3AA08249980 for ; Thu, 28 Oct 2021 01:43:29 +0000 (UTC) X-FDA: 78744148938.09.53FA313 Received: from mail-io1-f44.google.com (mail-io1-f44.google.com [209.85.166.44]) by imf12.hostedemail.com (Postfix) with ESMTP id 4049010003E3 for ; Thu, 28 Oct 2021 01:43:29 +0000 (UTC) Received: by mail-io1-f44.google.com with SMTP id z144so4955099iof.0 for ; Wed, 27 Oct 2021 18:43:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=q2MWT3ltBpEutemWMnO5IV+Dli3w0cr7Jk2rYKN1WKo=; b=VYaQyXfWM6sOanx3t6qAvPWFfD338jdnTaM7L2F4EXwJrISOsX2ByFuKTHv7xNRkTG 7BWciBeSY2irXsWhnWRi6OVqJxQs7/ZO79rLcIDs/aiVFWgHfJlvuVpfUToWcVFBDw49 BCfecdSa2WUEjL+TN94OyEqsucM5rEaK9DgGgCJaOCYNRUwXGRjWaKZseUmGso3F9gxw di+uY+S3e+rK3xyX3eI/JiQ9ICTO+6tm+0hZ1D5dbOqVvxb2fQEWj7uSPsf/vcKdxhy1 StoSWoyz4zwvhiyUgYfxfRiYrspymffEwoasFZiknT2hQbvSdzCZRqpNBiWlqe1b6cj1 mybQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=q2MWT3ltBpEutemWMnO5IV+Dli3w0cr7Jk2rYKN1WKo=; b=01Z4U2kH77ZSNwnSYinHiUSSehxE29piaGI6NXpNM4PphNFL4K7X7Pxi217HgU8DWq bv2/75SB0gwItiOwUtxEcDRq2xcAmujvc7040iJEfh+iyqKNskUyGFKcIyG1l5+M4Vcj lelXqEcG3Se0g1o9QGRpoNZXbLR6tzmxKhMFhvlEIyoWr9B2aKy/iWz2criJbYcuYvku 8EcOgnQB59BiTtDqqzRIEbfQLDRBcDq8DpIDL49QVh4fCS9OgiotSPXOdbcW+3eUJ5/F Tvuq6GBq39HvCd0JZ15sKdsYDjJNABFh3HP0/WSRXldk3yhuc7erm0rYVfWygc0Upe5N YwDg== X-Gm-Message-State: AOAM531bmNgdTnKATIvJM7zJ+elfN0uac4nt5px5hOTiZYVCheaXrzqQ qUwfdo/eIvqaP4goyRusuKA8RuzVSyziwcReaW0= X-Google-Smtp-Source: ABdhPJwNQClWNir1jhV830HzS4FTBIIlwJORqskR1S8FYGa8OK7RlivKeHpt/M/ZKHGSsheR5GLY1mxrQk02MMgYFuc= X-Received: by 2002:a02:cb9c:: with SMTP id u28mr1005721jap.95.1635385408343; Wed, 27 Oct 2021 18:43:28 -0700 (PDT) MIME-Version: 1.0 References: <20211025083315.4752-1-laoar.shao@gmail.com> <20211025083315.4752-13-laoar.shao@gmail.com> <202110251431.F594652F@keescook> In-Reply-To: From: Yafang Shao Date: Thu, 28 Oct 2021 09:42:52 +0800 Message-ID: Subject: Re: [PATCH v6 12/12] kernel/kthread: show a warning if kthread's comm is truncated To: Petr Mladek Cc: Kees Cook , Andrew Morton , Steven Rostedt , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Peter Zijlstra , Al Viro , Valentin Schneider , Qiang Zhang , robdclark , christian , Dietmar Eggemann , Ingo Molnar , Juri Lelli , Vincent Guittot , David Miller , Jakub Kicinski , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin Lau , Song Liu , Yonghong Song , john fastabend , KP Singh , dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca, linux-rdma@vger.kernel.org, netdev , bpf , "linux-perf-use." , linux-fsdevel@vger.kernel.org, Linux MM , LKML , kernel test robot , kbuild test robot , Andrii Nakryiko Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: p84j6cq9ujrckkdasodwwqrfkkdyqkmd X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 4049010003E3 Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=VYaQyXfW; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.166.44 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-HE-Tag: 1635385409-874520 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 Thu, Oct 28, 2021 at 4:10 AM Petr Mladek 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 > > > 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. > 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 -- Thanks Yafang