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 7DE27C43217 for ; Tue, 9 Nov 2021 17:34:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 68C7161381 for ; Tue, 9 Nov 2021 17:34:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241180AbhKIRhc (ORCPT ); Tue, 9 Nov 2021 12:37:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241153AbhKIRh1 (ORCPT ); Tue, 9 Nov 2021 12:37:27 -0500 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4AD0C0613B9 for ; Tue, 9 Nov 2021 09:34:40 -0800 (PST) Received: by mail-pg1-x52c.google.com with SMTP id g184so19153633pgc.6 for ; Tue, 09 Nov 2021 09:34:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=YtCMrd8soCJwqQdYqfS1Uxj+jvIWsqJW9Sfp+I5znK0=; b=RnI334WgYGo1s2xg0PnNwh/LwyjwkbeECYSptgpgWOUMErGKHmsDK/ZLGV9pEMwl/O ekYV7g3Qyj7iroFAawbog28LZq8j9E8ozPsy6LbXEeNYDfaWne0pF0iY2RKw20oFgoi+ iUWeYVTxBO9NJJ4Hp1KyWtYKI60Ou3f9tZSZ4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=YtCMrd8soCJwqQdYqfS1Uxj+jvIWsqJW9Sfp+I5znK0=; b=qC/9d/BHpSWN0xFrvU1VUotsHCA+W9/eOOKQe98Jv5B3c5eIk8+mNW2IXyxPWdU3v7 8V6bngnM6WExygTzZaGYr+Eb1VK95qoXmH7oTE55mgIOZgYUIEgr8soYPjZ2Z+kBjBZ0 FEus3BH+/MD1IKPPWnijBWoiDli/tDMKRtI/bRX041HBpMhbMVzcd3B94rT6MsJMdFs2 d9IQX6JSHdAyrZBzQ8YA62gp+rc6HWKkLNECUME2+cJkXfylx2PCm+CzVwThPe7zzChR 5gT3e699iB5SdlNCUXneaKQxM0ZqKHQeIBa/fKCPei8l0Y6Rd45cC4lCI0tdTkmzVRX+ DhHg== X-Gm-Message-State: AOAM533IBTmPueXtH9psoQUfoVJr7Zwnv0g3hhWSKZ0j2VsPpPVJRMt9 HEyXVJgy53qaS/Zf2oqDGSqglA== X-Google-Smtp-Source: ABdhPJzSUauLeZJaFQUg6w2y5hqN0XLLDLzRlcMe5/CcpZD5KWsYDIDxVT3fGawLe51+TwjHa4Pryg== X-Received: by 2002:a63:e710:: with SMTP id b16mr7200956pgi.38.1636479280328; Tue, 09 Nov 2021 09:34:40 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q9sm20769593pfj.88.2021.11.09.09.34.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Nov 2021 09:34:39 -0800 (PST) Date: Tue, 9 Nov 2021 09:34:39 -0800 From: Kees Cook To: Yafang Shao Cc: akpm@linux-foundation.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, Petr Mladek , Steven Rostedt , Mathieu Desnoyers , Arnaldo Carvalho de Melo , Alexei Starovoitov , Andrii Nakryiko , Michal Miroslaw , Peter Zijlstra , Matthew Wilcox , David Hildenbrand , Al Viro Subject: Re: [PATCH] kthread: dynamically allocate memory to store kthread's full name Message-ID: <202111090930.75BBF4678@keescook> References: <20211108084142.4692-1-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211108084142.4692-1-laoar.shao@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-perf-users@vger.kernel.org On Mon, Nov 08, 2021 at 08:41:42AM +0000, Yafang Shao wrote: > When I was implementing a new per-cpu kthread cfs_migration, I found the > comm of it "cfs_migration/%u" is truncated due to the limitation of > TASK_COMM_LEN. For example, the comm of the percpu thread on CPU10~19 are > all with the same name "cfs_migration/1", which will confuse the user. This > issue is not critical, because we can get the corresponding CPU from the > task's Cpus_allowed. But for kthreads correspoinding to other hardware > devices, it is not easy to get the detailed device info from task comm, > for example, > > jbd2/nvme0n1p2- > xfs-reclaim/sdf > > Currently there are so many truncated kthreads: > > rcu_tasks_kthre > rcu_tasks_rude_ > rcu_tasks_trace > poll_mpt3sas0_s > ext4-rsv-conver > xfs-reclaim/sd{a, b, c, ...} > xfs-blockgc/sd{a, b, c, ...} > xfs-inodegc/sd{a, b, c, ...} > audit_send_repl > ecryptfs-kthrea > vfio-irqfd-clea > jbd2/nvme0n1p2- > ... > > We can shorten these names to work around this problem, but it may be > not applied to all of the truncated kthreads. Take 'jbd2/nvme0n1p2-' for > example, it is a nice name, and it is not a good idea to shorten it. > > One possible way to fix this issue is extending the task comm size, but > as task->comm is used in lots of places, that may cause some potential > buffer overflows. Another more conservative approach is introducing a new > pointer to store kthread's full name if it is truncated, which won't > introduce too much overhead as it is in the non-critical path. Finally we > make a dicision to use the second approach. See also the discussions in > this thread: > https://lore.kernel.org/lkml/20211101060419.4682-1-laoar.shao@gmail.com/ > > After this change, the full name of these truncated kthreads will be > displayed via /proc/[pid]/comm: > > rcu_tasks_kthread > rcu_tasks_rude_kthread > rcu_tasks_trace_kthread > poll_mpt3sas0_statu > ext4-rsv-conversion > xfs-reclaim/sdf1 > xfs-blockgc/sdf1 > xfs-inodegc/sdf1 > audit_send_reply > ecryptfs-kthread > vfio-irqfd-cleanup > jbd2/nvme0n1p2-8 > > Suggested-by: Petr Mladek > Suggested-by: Steven Rostedt > Signed-off-by: Yafang Shao > Cc: Mathieu Desnoyers > Cc: Arnaldo Carvalho de Melo > Cc: Alexei Starovoitov > Cc: Andrii Nakryiko > Cc: Michal Miroslaw > Cc: Peter Zijlstra > Cc: Steven Rostedt > Cc: Matthew Wilcox > Cc: David Hildenbrand > Cc: Al Viro > Cc: Kees Cook > Cc: Petr Mladek > --- > TODO: will cleanup worker comm in the next step. > > --- > fs/proc/array.c | 3 +++ > include/linux/kthread.h | 1 + > kernel/kthread.c | 32 +++++++++++++++++++++++++++++++- > 3 files changed, 35 insertions(+), 1 deletion(-) > > diff --git a/fs/proc/array.c b/fs/proc/array.c > index 49be8c8ef555..860e4deafa65 100644 > --- a/fs/proc/array.c > +++ b/fs/proc/array.c > @@ -92,6 +92,7 @@ > #include > #include > #include > +#include > > #include > #include "internal.h" > @@ -102,6 +103,8 @@ void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) > > if (p->flags & PF_WQ_WORKER) > wq_worker_comm(tcomm, sizeof(tcomm), p); > + else if (p->flags & PF_KTHREAD) > + get_kthread_comm(tcomm, sizeof(tcomm), p); > else > __get_task_comm(tcomm, sizeof(tcomm), p); > > diff --git a/include/linux/kthread.h b/include/linux/kthread.h > index 346b0f269161..2a5c04494663 100644 > --- a/include/linux/kthread.h > +++ b/include/linux/kthread.h > @@ -33,6 +33,7 @@ struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), > unsigned int cpu, > const char *namefmt); > > +void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk); > void set_kthread_struct(struct task_struct *p); > > void kthread_set_per_cpu(struct task_struct *k, int cpu); > diff --git a/kernel/kthread.c b/kernel/kthread.c > index 5b37a8567168..ce8258231eea 100644 > --- a/kernel/kthread.c > +++ b/kernel/kthread.c > @@ -60,6 +60,8 @@ struct kthread { > #ifdef CONFIG_BLK_CGROUP > struct cgroup_subsys_state *blkcg_css; > #endif > + /* To store the full name if task comm is truncated. */ > + char *full_name; > }; > > enum KTHREAD_BITS { > @@ -93,6 +95,18 @@ static inline struct kthread *__to_kthread(struct task_struct *p) > return kthread; > } > > +void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk) > +{ > + struct kthread *kthread = to_kthread(tsk); > + > + if (!kthread || !kthread->full_name) { > + __get_task_comm(buf, buf_size, tsk); > + return; > + } > + > + strscpy_pad(buf, kthread->full_name, buf_size); > +} > + > void set_kthread_struct(struct task_struct *p) > { > struct kthread *kthread; > @@ -121,6 +135,7 @@ void free_kthread_struct(struct task_struct *k) > #ifdef CONFIG_BLK_CGROUP > WARN_ON_ONCE(kthread && kthread->blkcg_css); > #endif > + kfree(kthread->full_name); > kfree(kthread); > } > > @@ -399,12 +414,27 @@ 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]; > + 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) { > + struct kthread *kthread = to_kthread(task); > + char *full_name; > + > + full_name = kvasprintf(GFP_KERNEL, namefmt, args); > + if (!full_name) { > + kfree(create); > + return ERR_PTR(-ENOMEM); I'm not a fan of this out-of-line free/return. Why not just leave it truncated when out of memory? For example just do: struct kthread *kthread = to_kthread(task); kthread->full_name = kvasprintf(GFP_KERNEL, namefmt, args); > + } > + kthread->full_name = full_name; > + } > set_task_comm(task, name); > /* > * root may have changed our (kthreadd's) priority or CPU mask. > -- > 2.17.1 > -- Kees Cook