linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: Bhupesh Sharma <bhsharma@igalia.com>
Cc: Bhupesh <bhupesh@igalia.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	 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,
	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: Re: [PATCH v2 1/3] exec: Dynamically allocate memory to store task's full name
Date: Sun, 6 Apr 2025 10:28:36 +0800	[thread overview]
Message-ID: <CALOAHbDE3ToDc0knbUtGu0on9n9uUiWfKZEb-bgm1mW57VTZvg@mail.gmail.com> (raw)
In-Reply-To: <6beead5a-8c21-af57-0304-1bf825588481@igalia.com>

On Fri, Apr 4, 2025 at 2:35 PM Bhupesh Sharma <bhsharma@igalia.com> wrote:
>
>
> On 4/1/25 7:37 AM, Yafang Shao wrote:
> > On Mon, Mar 31, 2025 at 8:18 PM Bhupesh <bhupesh@igalia.com> 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 <bhupesh@igalia.com>
> >> ---
> >>   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;
> >> +
> > Adding another field to store the task name isn’t ideal. What about
> > combining them into a single field, as Linus suggested [0]?
> >
> > [0]. https://lore.kernel.org/all/CAHk-=wjAmmHUg6vho1KjzQi2=psR30+CogFd4aXrThr2gsiS4g@mail.gmail.com/
> >
>
> Thanks for sharing Linus's suggestion. I went through the suggested
> changes in the related threads and came up with the following set of points:
>
> 1. struct task_struct would contain both 'comm' and 'full_name',

Correct.

> 2. Remove the task_lock() inside __get_task_comm(),

This has been implemented in the patch series titled "Improve the copy
of task comm". For details, please refer to:
https://lore.kernel.org/linux-mm/20240828030321.20688-1-laoar.shao@gmail.com/.

> 3. Users of task->comm will be affected in the following ways:

Correct.

>      (a). Printing with '%s' and tsk->comm would just continue to
> work,but will get a longer max string.
>      (b). For users of memcpy.*->comm\>', we should change 'memcpy()' to
> 'copy_comm()' which would look like:
>
>          memcpy(dst, src, TASK_COMM_LEN);
>          dst[TASK_COMM_LEN-1] = 0;
>
>     (c). Users which use "sizeof(->comm)" will continue to get the old value because of the hacky union.

Using a separate pointer rather than a union could simplify the
implementation. I’m open to introducing a new pointer if you believe
it’s the better approach.

>
> Am I missing something here. Please let me know your views.


-- 
Regards
Yafang

  reply	other threads:[~2025-04-06  2:29 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-31 12:18 [PATCH v2 0/3] Dynamically allocate memory to store task's full name Bhupesh
2025-03-31 12:18 ` [PATCH v2 1/3] exec: " Bhupesh
2025-04-01  2:07   ` Yafang Shao
2025-04-04  6:35     ` Bhupesh Sharma
2025-04-06  2:28       ` Yafang Shao [this message]
2025-04-09 11:13         ` Bhupesh Sharma
2025-04-01  4:02   ` Harry Yoo
2025-04-04  5:31     ` Bhupesh Sharma
2025-04-03 16:17   ` Andrii Nakryiko
2025-04-04  5:36     ` Bhupesh Sharma
2025-04-03 16:30   ` Kees Cook
2025-04-04  6:48     ` Bhupesh Sharma
2025-04-04 17:24       ` Kees Cook
2025-04-09 11:31         ` Bhupesh Sharma
2025-03-31 12:18 ` [PATCH v2 2/3] fs/proc: Pass 'task->full_name' via 'proc_task_name()' Bhupesh
2025-03-31 12:18 ` [PATCH v2 3/3] kthread: Use 'task_struct->full_name' to store kthread's full name Bhupesh
2025-04-03 16:24   ` Kees Cook
2025-04-04  6:38     ` 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=CALOAHbDE3ToDc0knbUtGu0on9n9uUiWfKZEb-bgm1mW57VTZvg@mail.gmail.com \
    --to=laoar.shao@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=arnaldo.melo@gmail.com \
    --cc=bhsharma@igalia.com \
    --cc=bhupesh@igalia.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=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-perf-users@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=torvalds@linux-foundation.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;
as well as URLs for NNTP newsgroup(s).