Skip to content

Commit 9e40ef5

Browse files
rostedtgregkh
authored andcommitted
tracing: Map all PIDs to command lines
commit 785e3c0 upstream. The default max PID is set by PID_MAX_DEFAULT, and the tracing infrastructure uses this number to map PIDs to the comm names of the tasks, such output of the trace can show names from the recorded PIDs in the ring buffer. This mapping is also exported to user space via the "saved_cmdlines" file in the tracefs directory. But currently the mapping expects the PIDs to be less than PID_MAX_DEFAULT, which is the default maximum and not the real maximum. Recently, systemd will increases the maximum value of a PID on the system, and when tasks are traced that have a PID higher than PID_MAX_DEFAULT, its comm is not recorded. This leads to the entire trace to have "<...>" as the comm name, which is pretty useless. Instead, keep the array mapping the size of PID_MAX_DEFAULT, but instead of just mapping the index to the comm, map a mask of the PID (PID_MAX_DEFAULT - 1) to the comm, and find the full PID from the map_cmdline_to_pid array (that already exists). This bug goes back to the beginning of ftrace, but hasn't been an issue until user space started increasing the maximum value of PIDs. Link: https://lkml.kernel.org/r/20210427113207.3c601884@gandalf.local.home Cc: stable@vger.kernel.org Fixes: bc0c38d ("ftrace: latency tracer infrastructure") Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ea6803f commit 9e40ef5

1 file changed

Lines changed: 15 additions & 26 deletions

File tree

‎kernel/trace/trace.c‎

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,14 +2387,13 @@ static void tracing_stop_tr(struct trace_array *tr)
23872387

23882388
static int trace_save_cmdline(struct task_struct *tsk)
23892389
{
2390-
unsigned pid, idx;
2390+
unsigned tpid, idx;
23912391

23922392
/* treat recording of idle task as a success */
23932393
if (!tsk->pid)
23942394
return 1;
23952395

2396-
if (unlikely(tsk->pid > PID_MAX_DEFAULT))
2397-
return 0;
2396+
tpid = tsk->pid & (PID_MAX_DEFAULT - 1);
23982397

23992398
/*
24002399
* It's not the end of the world if we don't get
@@ -2405,26 +2404,15 @@ static int trace_save_cmdline(struct task_struct *tsk)
24052404
if (!arch_spin_trylock(&trace_cmdline_lock))
24062405
return 0;
24072406

2408-
idx = savedcmd->map_pid_to_cmdline[tsk->pid];
2407+
idx = savedcmd->map_pid_to_cmdline[tpid];
24092408
if (idx == NO_CMDLINE_MAP) {
24102409
idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
24112410

2412-
/*
2413-
* Check whether the cmdline buffer at idx has a pid
2414-
* mapped. We are going to overwrite that entry so we
2415-
* need to clear the map_pid_to_cmdline. Otherwise we
2416-
* would read the new comm for the old pid.
2417-
*/
2418-
pid = savedcmd->map_cmdline_to_pid[idx];
2419-
if (pid != NO_CMDLINE_MAP)
2420-
savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
2421-
2422-
savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
2423-
savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
2424-
2411+
savedcmd->map_pid_to_cmdline[tpid] = idx;
24252412
savedcmd->cmdline_idx = idx;
24262413
}
24272414

2415+
savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
24282416
set_cmdline(idx, tsk->comm);
24292417

24302418
arch_spin_unlock(&trace_cmdline_lock);
@@ -2435,6 +2423,7 @@ static int trace_save_cmdline(struct task_struct *tsk)
24352423
static void __trace_find_cmdline(int pid, char comm[])
24362424
{
24372425
unsigned map;
2426+
int tpid;
24382427

24392428
if (!pid) {
24402429
strcpy(comm, "<idle>");
@@ -2446,16 +2435,16 @@ static void __trace_find_cmdline(int pid, char comm[])
24462435
return;
24472436
}
24482437

2449-
if (pid > PID_MAX_DEFAULT) {
2450-
strcpy(comm, "<...>");
2451-
return;
2438+
tpid = pid & (PID_MAX_DEFAULT - 1);
2439+
map = savedcmd->map_pid_to_cmdline[tpid];
2440+
if (map != NO_CMDLINE_MAP) {
2441+
tpid = savedcmd->map_cmdline_to_pid[map];
2442+
if (tpid == pid) {
2443+
strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
2444+
return;
2445+
}
24522446
}
2453-
2454-
map = savedcmd->map_pid_to_cmdline[pid];
2455-
if (map != NO_CMDLINE_MAP)
2456-
strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
2457-
else
2458-
strcpy(comm, "<...>");
2447+
strcpy(comm, "<...>");
24592448
}
24602449

24612450
void trace_find_cmdline(int pid, char comm[])

0 commit comments

Comments
 (0)