From patchwork Mon Mar 31 12:18:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bhupesh X-Patchwork-Id: 14033740 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 455D9C3600C for ; Mon, 31 Mar 2025 15:54:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CDAA7280009; Mon, 31 Mar 2025 11:54:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C89EF280002; Mon, 31 Mar 2025 11:54:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B29FB280009; Mon, 31 Mar 2025 11:54:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8E3DE280002 for ; Mon, 31 Mar 2025 11:54:04 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E923BBA51E for ; Mon, 31 Mar 2025 12:18:53 +0000 (UTC) X-FDA: 83281750146.23.68571AD Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by imf15.hostedemail.com (Postfix) with ESMTP id 332D8A0011 for ; Mon, 31 Mar 2025 12:18:51 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=GLoQ8Gs7; dmarc=pass (policy=none) header.from=igalia.com; spf=pass (imf15.hostedemail.com: domain of bhupesh@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=bhupesh@igalia.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743423532; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=m2o+Mi9B+LLlSr1F+IZ6AiiYjWbrFv3KIUCOssd5KE4=; b=UNbi2EdKzYIzUJ6A1ZuSqNI5I3QhSO4L3CfZqncc++Z6xFp3K5x14tQ/uQ/yxrQ556v4AV qnPO4ANWEITzISEVBW9Ni481XCkV7vHplV1ayxWKzbEhqXKF+Fg3G1HkTLJFgpKSdn5bP4 WrhIREt8wFHyaRNX/94qMsX+C8jfKuw= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=GLoQ8Gs7; dmarc=pass (policy=none) header.from=igalia.com; spf=pass (imf15.hostedemail.com: domain of bhupesh@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=bhupesh@igalia.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743423532; a=rsa-sha256; cv=none; b=vjp5pKokA5SZOSESXXzyEn5jGiCmEavg+5jfTXvV5tlq6BBzi/GJRYOUf6srmcsmwoxiRo ph1a3Bb0HuiZeKNofrE2tl4Bpoth129YBtyRto5o9TiwCvfR8VDDgjHg1CtNzBwEegrmdd uc4aI0RDY+P42v25ewW666xFbPT/kbM= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: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=m2o+Mi9B+LLlSr1F+IZ6AiiYjWbrFv3KIUCOssd5KE4=; b=GLoQ8Gs7Pv63GbZtpCeznhHQZD uGrw3V2kkzc05t3W9WL4nTZX5nrNHWFnf2oI+TulxjbbjiBuH13I6FIwJmAxh+qaikLRf3yKZ9e0z HD2HPTVbcLIBMPpKCCNDYZNLYwDAMk5n0eNwKKqtHRFqZkh5X0CfkPs48QBJytxuJywPJmCPFb4Ib u9XW3oOch4MwZynhdtcLpW7cqx8XuMgcjx6Yf01DOmYLiIcDbPGgJ9AAt1oCmCpLswGouy5i/3ZQ0 76c3H2ssIWvhu42wYkEIJ+CiP9csFpnYwT9QyddFHdCDAOMxhAdUHpNDFggAQynJsn7TfsVUjmVv4 e7mlHXnw==; Received: from [223.233.69.2] (helo=localhost.localdomain) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1tzE6G-009Btr-G1; Mon, 31 Mar 2025 14:18:44 +0200 From: Bhupesh To: akpm@linux-foundation.org Cc: bhupesh@igalia.com, 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, andrii.nakryiko@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 Subject: [PATCH v2 1/3] exec: Dynamically allocate memory to store task's full name Date: Mon, 31 Mar 2025 17:48:18 +0530 Message-Id: <20250331121820.455916-2-bhupesh@igalia.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20250331121820.455916-1-bhupesh@igalia.com> References: <20250331121820.455916-1-bhupesh@igalia.com> MIME-Version: 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jk7Tk9ISOhrvhu1zVqgUDfJV t5Jn7Yq+ W5JxkEMNKiX1BJU+D9PFzlGCHGpn1My1UgKoL/YiTeYiIm9foT3lkz98CXU9lGuBeK4pjE77QolBDslyECJHz62Ce2oAh4wc0VBzOeffUYudJwAYPqtYIoJlCEtIaJJfwPU4XnTIj1QM7lH8dU8XZavs0dQB7MCm3qGXinIuNK+R4Eron2yYgWwQnODFfo9D3Ko+EUfqPnrVK3SjxeClBXHdFX2+2pUxZekhX9RBjWgHuQxsdBNYB7z8KTx3s/3opA1Bnu0TIHkJhszUe0HIFClykllybSv8WIF64FTHBeARg4s2tiDI0Kn90cfc/ph4woStZm+0XqimBqZ5FcpjAXyGEEw== 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: 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); + 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) {