Skip to content

Commit 88b72b3

Browse files
committed
proc: Consolidate task->comm formatting into proc_task_name()
proc shows task->comm in three places - comm, stat, status - and each is fetching and formatting task->comm slighly differently. This patch renames task_name() to proc_task_name(), makes it more generic, and updates all three paths to use it. This will enable expanding comm reporting for workqueue workers. Signed-off-by: Tejun Heo <[email protected]>
1 parent 8bf8959 commit 88b72b3

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

fs/proc/array.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
#include <asm/processor.h>
9696
#include "internal.h"
9797

98-
static inline void task_name(struct seq_file *m, struct task_struct *p)
98+
void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape)
9999
{
100100
char *buf;
101101
size_t size;
@@ -104,13 +104,17 @@ static inline void task_name(struct seq_file *m, struct task_struct *p)
104104

105105
get_task_comm(tcomm, p);
106106

107-
seq_puts(m, "Name:\t");
108-
109107
size = seq_get_buf(m, &buf);
110-
ret = string_escape_str(tcomm, buf, size, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
111-
seq_commit(m, ret < size ? ret : -1);
108+
if (escape) {
109+
ret = string_escape_str(tcomm, buf, size,
110+
ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
111+
if (ret >= size)
112+
ret = -1;
113+
} else {
114+
ret = strscpy(buf, tcomm, size);
115+
}
112116

113-
seq_putc(m, '\n');
117+
seq_commit(m, ret);
114118
}
115119

116120
/*
@@ -365,7 +369,10 @@ int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
365369
{
366370
struct mm_struct *mm = get_task_mm(task);
367371

368-
task_name(m, task);
372+
seq_puts(m, "Name:\t");
373+
proc_task_name(m, task, true);
374+
seq_putc(m, '\n');
375+
369376
task_state(m, ns, pid, task);
370377

371378
if (mm) {
@@ -400,7 +407,6 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
400407
u64 cutime, cstime, utime, stime;
401408
u64 cgtime, gtime;
402409
unsigned long rsslim = 0;
403-
char tcomm[sizeof(task->comm)];
404410
unsigned long flags;
405411

406412
state = *get_task_state(task);
@@ -427,8 +433,6 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
427433
}
428434
}
429435

430-
get_task_comm(tcomm, task);
431-
432436
sigemptyset(&sigign);
433437
sigemptyset(&sigcatch);
434438
cutime = cstime = utime = stime = 0;
@@ -495,7 +499,7 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
495499

496500
seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
497501
seq_puts(m, " (");
498-
seq_puts(m, tcomm);
502+
proc_task_name(m, task, false);
499503
seq_puts(m, ") ");
500504
seq_putc(m, state);
501505
seq_put_decimal_ll(m, " ", ppid);

fs/proc/base.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,9 +1565,8 @@ static int comm_show(struct seq_file *m, void *v)
15651565
if (!p)
15661566
return -ESRCH;
15671567

1568-
task_lock(p);
1569-
seq_printf(m, "%s\n", p->comm);
1570-
task_unlock(p);
1568+
proc_task_name(m, p, false);
1569+
seq_putc(m, '\n');
15711570

15721571
put_task_struct(p);
15731572

fs/proc/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ unsigned name_to_int(const struct qstr *qstr);
131131
*/
132132
extern const struct file_operations proc_tid_children_operations;
133133

134+
extern void proc_task_name(struct seq_file *m, struct task_struct *p,
135+
bool escape);
134136
extern int proc_tid_stat(struct seq_file *, struct pid_namespace *,
135137
struct pid *, struct task_struct *);
136138
extern int proc_tgid_stat(struct seq_file *, struct pid_namespace *,

0 commit comments

Comments
 (0)