Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2425044pxb; Fri, 29 Oct 2021 00:46:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJygC3Fy7AkjReTM1/VdGaE/LlZCJ3gkRNzjW2r73BYFvkiPVjlvUy8/z6bkw7kr+wJUjelC X-Received: by 2002:a17:90a:7e13:: with SMTP id i19mr9721604pjl.120.1635493608501; Fri, 29 Oct 2021 00:46:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635493608; cv=none; d=google.com; s=arc-20160816; b=h88SD5eAJfFn8wqQK8N4mejVJ2eYUvKQ7ZQFB9yV6ZX46hHBHeb1tBQ3+vF1nWlZVB v4pt4vurwJ4uAP9Gc/F8+wX6WcjQc1923vS2lMU5e4zLTF3n3X9FuJZYaESzrVc4YPuZ iyxR8k1GGczoiOII5OuOVq5+Ti9xRSxPxCRKoJpDNzy2qEIy3QfMJ8aGjAfbv0DOoAMy +4cw48d+4sTCNYcq3xy/qho7qkzihG2jMHenIgnlnModhnz1SwAmbLqLlnUa6gKApN2O mSXiJboCwrWxUb5Ic4Da7dF19EdsN+oFGcEEOyPuETMyIomV1xr0fIaCTcQXFlN87LsH RaKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=YvS3OEojn6WlgoM5Pok9+XWG3Yikban/dCaVMqXh6O4=; b=NdnoszbHQQxVXf5bn4LyxYRMNhBC7dWtGuCQzKf3N1HpO3db54BvO2qth2MtYEdk/H ooijM4gSIE3fLo26aC9fsmcGOI4eCgHgwVpIca3MN4uc82YlKGIr8RAUryJuxs0MXePk e0prpT9N8MrLRcpmpZTSnip495Ze/S2SKVAQb0PV61Kkuu3kLKe8bkFz+Dg+uFfdjFky svB5EMAGWTOJ27wDi7KdOH0x+QGfEQ108ziUEr8BxVqP1U6KGiIUj961iCRq8YlsSIp6 zDRDF5w2bR9ZCau1XLsH1TZnO8gHBveIOOlkt279xkguUTAlK5uZFYgMtxToFRk+cGTO iHMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=j0ZYEwfg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r11si5678617plr.376.2021.10.29.00.46.35; Fri, 29 Oct 2021 00:46:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=j0ZYEwfg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232236AbhJ2HsG (ORCPT + 99 others); Fri, 29 Oct 2021 03:48:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230247AbhJ2HsF (ORCPT ); Fri, 29 Oct 2021 03:48:05 -0400 Received: from mail-io1-xd35.google.com (mail-io1-xd35.google.com [IPv6:2607:f8b0:4864:20::d35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A40AEC061570; Fri, 29 Oct 2021 00:45:37 -0700 (PDT) Received: by mail-io1-xd35.google.com with SMTP id n67so11489652iod.9; 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=Dy+L2AIyx8TPFQn9HjDpaganSZ4/9hXjfefaJa5ZQ+Ao0OlHtHnsgU2/ANFCz027DB OzcpXx7w5abhPDzg6FKagaZ0pci1jadnof3uroSqKjOP1qV0ReILwvxbYRmioikToxOR CBwpMeVdB2gIg80oLTSZSHbVGvPX6NH5Ql2YL+sQTzEId9hGtr39ssnZPmNCs8nCQpfl 1uNJQ1bRaCyjWOvcE4ovJ5buqaIcRwO2ySeuD6ST7//Wnrw35DabK/wTIfQwCRtRT8Qe run0JpMf41CZOxGtnqbQux9wy8skHBdexwjzlbDkTEJScEvBlt4xjjJ4UCzVax+weyJw jITg== X-Gm-Message-State: AOAM530kgGMKBstBxKTuI/V7O59d1ylMJumuc/VM7XjstAdqzehCCevV 6DJp11l0OzhHkDqGwo+SFs0Pxb9375Pbo9YkjiI= 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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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