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 993BCC433FE for ; Fri, 29 Oct 2021 07:45:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 489336108F for ; Fri, 29 Oct 2021 07:45:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 489336108F 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 D2E916B0071; Fri, 29 Oct 2021 03:45:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDE57940007; Fri, 29 Oct 2021 03:45:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B87976B0073; Fri, 29 Oct 2021 03:45:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0199.hostedemail.com [216.40.44.199]) by kanga.kvack.org (Postfix) with ESMTP id 9221B6B0071 for ; Fri, 29 Oct 2021 03:45:38 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 299202DEB9 for ; Fri, 29 Oct 2021 07:45:38 +0000 (UTC) X-FDA: 78748690356.12.30C49EC Received: from mail-io1-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) by imf29.hostedemail.com (Postfix) with ESMTP id ADCB3900013E for ; Fri, 29 Oct 2021 07:45:37 +0000 (UTC) Received: by mail-io1-f54.google.com with SMTP id h81so2701157iof.6 for ; Fri, 29 Oct 2021 00:45:37 -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=YvS3OEojn6WlgoM5Pok9+XWG3Yikban/dCaVMqXh6O4=; b=j0ZYEwfg/RFP3vZ0NF45LNbXn0FXnpRrSZKuYBz/v3Ti0BTmeyp9N7VQZHJ0MOpaHc SyOFdeQmIvmzNxRqDvY36MWKL4TN4GyGqlch3Oic3ZzzQtFJzSqAx6Sncn84ySZo0K/U CDVKvtPGDJyopdDodidwBiFnONnN0Y+zPuafOQ6zzwcj0xmqQruYxufDtrGqgVTm197X YD7WbaChlI8ZDicXCuH4XD1MpdTyJ50sTFdy4MOyQqgKK1CXkRLKfxaPvxQqnar8Ew15 4rzhCfQnHV9Kq+c95oj4HnQu6n4EutKUy+lqQDB3VSiBsBk12fvwsyMW/u3N12jhBu3l Yg8A== 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=YvS3OEojn6WlgoM5Pok9+XWG3Yikban/dCaVMqXh6O4=; b=3mi0YfsCioRRyYKvpub3o0LEywdvHdKhQpkBXVMTvvd5mUqb9vgn0lzTrvDs/z8lTY hDCrhaWUZbLPYhc8eLJ1KbLclv4er5o8SdC5fJaIHxzzbh0kkZX5yhh3yhjf5hO7VqTg QU9X2XkvfrRJGN4YH2C9kmDjhcd30806xVA79wFfFhievIfsnyU4wdljvJWz6cKeAKjw rOdj132UAJrrPDqG/8IOX3qnh8hdqYdAxCmD2LHNfrlql6IeBf3V0hJ1jNE0DJggX4MN NLqikxt6DX+GaFgh6P33JCyhq31HHpqO6bF8DpsKMcNci5odpJNg8f1JoWIek8E6e3Ys GslA== X-Gm-Message-State: AOAM533s6IWmbT/oWIWWN5U6jKU5Wfy7qZmIaDSBvva+ngT14AZ+LO7V rb35LJdcdSwr10w5w6vfGg8aWAhPBT+YBuiXH28= X-Google-Smtp-Source: ABdhPJyETE/qLAF0grfwPy1l00eM4REeeLtmeoBWL6wOt59OXeZvcrCFeFwAw5oh/9q3xbShCHKFcImikiHaH3niayk= X-Received: by 2002:a05:6602:27d4:: with SMTP id l20mr6774818ios.94.1635493536891; Fri, 29 Oct 2021 00:45:36 -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: Fri, 29 Oct 2021 15:44:47 +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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: ADCB3900013E X-Stat-Signature: rq7gyke88mfyjruh9nuru8nqn1kds7ck Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=j0ZYEwfg; spf=pass (imf29.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.166.54 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1635493537-738097 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. > 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 -- Thanks Yafang