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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09BCAC36010 for ; Fri, 4 Apr 2025 05:37:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CB0EA6B0010; Fri, 4 Apr 2025 01:37:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C5EEB6B0011; Fri, 4 Apr 2025 01:37:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B00816B0026; Fri, 4 Apr 2025 01:37:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9033C6B0010 for ; Fri, 4 Apr 2025 01:37:03 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 89BBE160B6F for ; Fri, 4 Apr 2025 05:37:03 +0000 (UTC) X-FDA: 83295252726.08.B5CF0B9 Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by imf18.hostedemail.com (Postfix) with ESMTP id B39981C0003 for ; Fri, 4 Apr 2025 05:37:01 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=iXesTLYp; dmarc=pass (policy=none) header.from=igalia.com; spf=pass (imf18.hostedemail.com: domain of bhsharma@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=bhsharma@igalia.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743745021; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RJ6sJSRTRl9f2sZO8e6I1tdZqgXHwuW7syCheEXJI80=; b=Yab44hRkT1a6tiSDaQOBUmGWBHpSKc9f3o3kPVnRssrYhhQe0eZktkTYhpYxt1w95XmH0x ZWAbGUqC9OjO3cxlWh0QBOWZ6ePxauquv51w7wMwICNyBcIeij3cvRpg5bRZIKW/ItUW05 juUK5YdUmb20uyC8ZbamEdBzVcc4fJk= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=iXesTLYp; dmarc=pass (policy=none) header.from=igalia.com; spf=pass (imf18.hostedemail.com: domain of bhsharma@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=bhsharma@igalia.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743745021; a=rsa-sha256; cv=none; b=zPgt/60kZ/etXoXAtnJlhKdWtTOT7Tm/ZEvR9Rpel8WugcEwwVVuBah1qEzLPcOgfjvPyZ Yz0yLniPCgH2iXpUw8QlpLXcrjszAgYwfJvLS1i1m8MnnnEAeZP3xGZ9Mv2OkeT0RWlTdg 9asjbqZjFNYBS88uSFuuBLqrvtw3HC4= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:In-Reply-To:From: References:Cc:To:Subject:MIME-Version:Date:Message-ID:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=RJ6sJSRTRl9f2sZO8e6I1tdZqgXHwuW7syCheEXJI80=; b=iXesTLYpmAC7VnKZcqwzWpaBMr +BjlrXU93j2nnBgWxooZfjJgUfl5Evpo0stzq6D2PRnPg17LRd1iUGHQLn52CF5XjyGL5UNSFzHbf JPKPCo/VWr+iyn89AlTv0SBwY4vv/I1EN+lFPdhbg47mAxFgPtMYzLs6PTjGwr5S6eVfboNa31UMy eliDl/+0VjIcxwD8y7zhtGv1a/nu24osG1SG3saWbE8klvXekMID1DobWqI0xvINcpn9xVpQlL9vf ANjhs9vuySDsxA8cPjPQEkotsnRrFDO8Nt8xrPZgJox5OxJ18R6NFrkDms8qrPTemGH5d5iUIaUka xIAQ1tjg==; Received: from [223.233.74.223] (helo=[192.168.1.12]) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_128_GCM:128) (Exim) id 1u0Zjb-00BC68-IJ; Fri, 04 Apr 2025 07:36:55 +0200 Message-ID: <04038132-2edf-f13e-86f9-449cb7426104@igalia.com> Date: Fri, 4 Apr 2025 11:06:50 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH v2 1/3] exec: Dynamically allocate memory to store task's full name Content-Language: en-US To: Andrii Nakryiko , Bhupesh Cc: akpm@linux-foundation.org, kernel-dev@igalia.com, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, oliver.sang@intel.com, lkp@intel.com, laoar.shao@gmail.com, pmladek@suse.com, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, alexei.starovoitov@gmail.com, mirq-linux@rere.qmqm.pl, peterz@infradead.org, willy@infradead.org, david@redhat.com, viro@zeniv.linux.org.uk, keescook@chromium.org, ebiederm@xmission.com, brauner@kernel.org, jack@suse.cz, mingo@redhat.com, juri.lelli@redhat.com, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com References: <20250331121820.455916-1-bhupesh@igalia.com> <20250331121820.455916-2-bhupesh@igalia.com> From: Bhupesh Sharma In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-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vcC9FgBf+49Jl165ixJWgHfTVLvVgYlBS+nYKd4K PKm+cWGr 7QoKV89QDFQ0kA25S9RxzbWlwUYxfc8xQIFZB5ZecNS4Qg/3F21NS/VX9awJQV5rIwsaK5u0k8w7nfnURyLCVEe/MTa5pPhxR5ZICd1YX3bIjC1wPzM3lreTxS+PMwivBWFho402ogkAsQNBr3HujBA8fzuL99QzxyfdNriyf7KzgU60jcJqqWqVf0tog7ITRMTYdRZcAlFFKg9DUtz/ISBDEjxMu5cL12YJovsWpgAQVVnHvH0g646Hu8EwF7jf09fgOdR/o0j9lZ5+6bOkH0DykKiz1pDcRHqeIRaFFsCrid3m3ZSCGw5sZU5l4N/vX7//avTwheWGVtFhhhBkxzQM0xeyGNceF/lv5zFL4VleFgAQ6BZQetXJvwd/g2frFwJ4Q 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: List-Subscribe: List-Unsubscribe: On 4/3/25 9:47 PM, Andrii Nakryiko wrote: > On Mon, Mar 31, 2025 at 5:18 AM Bhupesh wrote: >> Provide a parallel implementation for get_task_comm() called >> get_task_full_name() which allows the dynamically allocated >> and filled-in task's full name to be passed to interested >> users such as 'gdb'. >> >> Currently while running 'gdb', the 'task->comm' value of a long >> task name is truncated due to the limitation of TASK_COMM_LEN. >> >> For example using gdb to debug a simple app currently which generate >> threads with long task names: >> # gdb ./threadnames -ex "run info thread" -ex "detach" -ex "quit" > log >> # cat log >> >> NameThatIsTooLo >> >> This patch does not touch 'TASK_COMM_LEN' at all, i.e. >> 'TASK_COMM_LEN' and the 16-byte design remains untouched. Which means >> that all the legacy / existing ABI, continue to work as before using >> '/proc/$pid/task/$tid/comm'. >> >> This patch only adds a parallel, dynamically-allocated >> 'task->full_name' which can be used by interested users >> via '/proc/$pid/task/$tid/full_name'. >> >> After this change, gdb is able to show full name of the task: >> # gdb ./threadnames -ex "run info thread" -ex "detach" -ex "quit" > log >> # cat log >> >> NameThatIsTooLongForComm[4662] >> >> Signed-off-by: Bhupesh >> --- >> fs/exec.c | 21 ++++++++++++++++++--- >> include/linux/sched.h | 9 +++++++++ >> 2 files changed, 27 insertions(+), 3 deletions(-) >> >> diff --git a/fs/exec.c b/fs/exec.c >> index f45859ad13ac..4219d77a519c 100644 >> --- a/fs/exec.c >> +++ b/fs/exec.c >> @@ -1208,6 +1208,9 @@ int begin_new_exec(struct linux_binprm * bprm) >> { >> struct task_struct *me = current; >> int retval; >> + va_list args; >> + char *name; >> + const char *fmt; >> >> /* Once we are committed compute the creds */ >> retval = bprm_creds_from_file(bprm); >> @@ -1348,11 +1351,22 @@ int begin_new_exec(struct linux_binprm * bprm) >> * detecting a concurrent rename and just want a terminated name. >> */ >> rcu_read_lock(); >> - __set_task_comm(me, smp_load_acquire(&bprm->file->f_path.dentry->d_name.name), >> - true); >> + fmt = smp_load_acquire(&bprm->file->f_path.dentry->d_name.name); >> + name = kvasprintf(GFP_KERNEL, fmt, args); > this `args` argument, it's not initialized anywhere, right? It's not > clear where it's coming from, but you are passing it directly into > kvasprintf(), I can't convince myself that this is correct. Can you > please explain what is happening here? > > Also, instead of allocating a buffer unconditionally, maybe check that > comm is longer than 16, and if not, just use the old-schoold 16-byte > comm array? Ok. As Kees also mentioned in his comment, I will try to do away with the allocation in the exec() hot-path in v3. >> + if (!name) >> + return -ENOMEM; >> + >> + me->full_name = name; >> + __set_task_comm(me, fmt, true); >> rcu_read_unlock(); >> } else { >> - __set_task_comm(me, kbasename(bprm->filename), true); >> + fmt = kbasename(bprm->filename); >> + name = kvasprintf(GFP_KERNEL, fmt, args); >> + if (!name) >> + return -ENOMEM; >> + >> + me->full_name = name; >> + __set_task_comm(me, fmt, true); >> } >> >> /* An exec changes our domain. We are no longer part of the thread >> @@ -1399,6 +1413,7 @@ int begin_new_exec(struct linux_binprm * bprm) >> return 0; >> >> out_unlock: >> + kfree(me->full_name); >> up_write(&me->signal->exec_update_lock); >> if (!bprm->cred) >> mutex_unlock(&me->signal->cred_guard_mutex); >> diff --git a/include/linux/sched.h b/include/linux/sched.h >> index 56ddeb37b5cd..053b52606652 100644 >> --- a/include/linux/sched.h >> +++ b/include/linux/sched.h >> @@ -1166,6 +1166,9 @@ struct task_struct { >> */ >> char comm[TASK_COMM_LEN]; >> >> + /* To store the full name if task comm is truncated. */ >> + char *full_name; >> + >> struct nameidata *nameidata; >> >> #ifdef CONFIG_SYSVIPC >> @@ -2007,6 +2010,12 @@ extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec >> buf; \ >> }) >> >> +#define get_task_full_name(buf, buf_size, tsk) ({ \ >> + BUILD_BUG_ON(sizeof(buf) < TASK_COMM_LEN); \ >> + strscpy_pad(buf, (tsk)->full_name, buf_size); \ >> + buf; \ >> +}) >> + >> #ifdef CONFIG_SMP >> static __always_inline void scheduler_ipi(void) >> { >> -- >> 2.38.1 >> Thanks.