From: Bhupesh <bhupesh@igalia.com>
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,
linux-trace-kernel@vger.kernel.org
Subject: [PATCH v4 3/3] exec: Add support for 64 byte 'tsk->comm_ext'
Date: Wed, 21 May 2025 11:53:37 +0530 [thread overview]
Message-ID: <20250521062337.53262-4-bhupesh@igalia.com> (raw)
In-Reply-To: <20250521062337.53262-1-bhupesh@igalia.com>
Historically due to the 16-byte length of TASK_COMM_LEN, the
users of 'tsk->comm' are restricted to use a fixed-size target
buffer also of TASK_COMM_LEN for 'memcpy()' like use-cases.
To fix the same, Linus suggested in [1] that we can add the
following union inside 'task_struct':
union {
char comm[TASK_COMM_LEN];
char comm_ext[TASK_COMM_EXT_LEN];
};
and then modify '__set_task_comm()' to pass 'tsk->comm_ext'
to the existing users.
This would mean that:
(1) The old common pattern of just printing with '%s' and tsk->comm
would just continue to work (as it is):
pr_alert("BUG: Bad page state in process %s pfn:%05lx\n",
current->comm, page_to_pfn(page));
(2) And, the memcpy() users of 'tsk->comm' would need to be made more
stable by ensuring that the destination buffer always has a closing
NUL character (done already in the preceding patch in this series).
So, eventually:
- users who want the existing 'TASK_COMM_LEN' behavior will get it
(existing ABIs would continue to work),
- users who just print out 'tsk->comm' as a string will get the longer
new "extended comm",
- users who do 'sizeof(->comm)' will continue to get the old value
because of the union.
[1]. https://lore.kernel.org/all/CAHk-=wjAmmHUg6vho1KjzQi2=psR30+CogFd4aXrThr2gsiS4g@mail.gmail.com
Signed-off-by: Bhupesh <bhupesh@igalia.com>
---
fs/exec.c | 6 +++---
include/linux/sched.h | 8 ++++++--
2 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/fs/exec.c b/fs/exec.c
index 1f5fdd2e096e..3b39fbfc8fe4 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1077,11 +1077,11 @@ static int unshare_sighand(struct task_struct *me)
*/
void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
{
- size_t len = min(strlen(buf), sizeof(tsk->comm) - 1);
+ size_t len = min(strlen(buf), sizeof(tsk->comm_ext) - 1);
trace_task_rename(tsk, buf);
- memcpy(tsk->comm, buf, len);
- memset(&tsk->comm[len], 0, sizeof(tsk->comm) - len);
+ memcpy(tsk->comm_ext, buf, len);
+ memset(&tsk->comm_ext[len], 0, sizeof(tsk->comm_ext) - len);
perf_event_comm(tsk, exec);
}
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 704222114dcc..2605207170b4 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -316,6 +316,7 @@ struct user_event_mm;
*/
enum {
TASK_COMM_LEN = 16,
+ TASK_COMM_EXT_LEN = 64,
};
extern void sched_tick(void);
@@ -1165,7 +1166,10 @@ struct task_struct {
* - logic inside set_task_comm() will ensure it is always NUL-terminated and
* zero-padded
*/
- char comm[TASK_COMM_LEN];
+ union {
+ char comm[TASK_COMM_LEN];
+ char comm_ext[TASK_COMM_EXT_LEN];
+ };
struct nameidata *nameidata;
@@ -2005,7 +2009,7 @@ extern void __set_task_comm(struct task_struct *tsk, const char *from, bool exec
*/
#define get_task_comm(buf, tsk) ({ \
BUILD_BUG_ON(sizeof(buf) < TASK_COMM_LEN); \
- strscpy_pad(buf, (tsk)->comm); \
+ strscpy_pad(buf, (tsk)->comm_ext); \
buf; \
})
--
2.38.1
next prev parent reply other threads:[~2025-05-21 6:24 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-21 6:23 [PATCH v4 0/3] Add support for long task name Bhupesh
2025-05-21 6:23 ` [PATCH v4 1/3] exec: Remove obsolete comments Bhupesh
2025-05-22 6:18 ` Yafang Shao
2025-05-21 6:23 ` [PATCH v4 2/3] treewide: Switch memcpy() users of 'task->comm' to a more safer implementation Bhupesh
2025-05-22 6:15 ` Yafang Shao
2025-05-22 6:27 ` Yafang Shao
2025-05-22 19:44 ` Bhupesh Sharma
2025-05-21 6:23 ` Bhupesh [this message]
2025-05-23 3:48 ` [PATCH v4 3/3] exec: Add support for 64 byte 'tsk->comm_ext' Kees Cook
2025-05-23 12:31 ` Bhupesh Sharma
2025-05-23 20:55 ` Kees Cook
2025-05-26 11:13 ` Bhupesh Sharma
2025-06-30 7:58 ` Bhupesh Sharma
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250521062337.53262-4-bhupesh@igalia.com \
--to=bhupesh@igalia.com \
--cc=akpm@linux-foundation.org \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=arnaldo.melo@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=bsegall@google.com \
--cc=david@redhat.com \
--cc=ebiederm@xmission.com \
--cc=jack@suse.cz \
--cc=juri.lelli@redhat.com \
--cc=keescook@chromium.org \
--cc=kernel-dev@igalia.com \
--cc=laoar.shao@gmail.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=lkp@intel.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=mirq-linux@rere.qmqm.pl \
--cc=oliver.sang@intel.com \
--cc=peterz@infradead.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=viro@zeniv.linux.org.uk \
--cc=vschneid@redhat.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox