Skip to content

Commit 1d2865b

Browse files
paulburtongregkh
authored andcommitted
tracing: Resize tgid_map to pid_max, not PID_MAX_DEFAULT
commit 4030a6e upstream. Currently tgid_map is sized at PID_MAX_DEFAULT entries, which means that on systems where pid_max is configured higher than PID_MAX_DEFAULT the ftrace record-tgid option doesn't work so well. Any tasks with PIDs higher than PID_MAX_DEFAULT are simply not recorded in tgid_map, and don't show up in the saved_tgids file. In particular since systemd v243 & above configure pid_max to its highest possible 1<<22 value by default on 64 bit systems this renders the record-tgids option of little use. Increase the size of tgid_map to the configured pid_max instead, allowing it to cover the full range of PIDs up to the maximum value of PID_MAX_LIMIT if the system is configured that way. On 64 bit systems with pid_max == PID_MAX_LIMIT this will increase the size of tgid_map from 256KiB to 16MiB. Whilst this 64x increase in memory overhead sounds significant 64 bit systems are presumably best placed to accommodate it, and since tgid_map is only allocated when the record-tgid option is actually used presumably the user would rather it spends sufficient memory to actually record the tgids they expect. The size of tgid_map could also increase for CONFIG_BASE_SMALL=y configurations, but these seem unlikely to be systems upon which people are both configuring a large pid_max and running ftrace with record-tgid anyway. Of note is that we only allocate tgid_map once, the first time that the record-tgid option is enabled. Therefore its size is only set once, to the value of pid_max at the time the record-tgid option is first enabled. If a user increases pid_max after that point, the saved_tgids file will not contain entries for any tasks with pids beyond the earlier value of pid_max. Link: https://lkml.kernel.org/r/20210701172407.889626-2-paulburton@google.com Fixes: d914ba3 ("tracing: Add support for recording tgid of tasks") Cc: Ingo Molnar <mingo@redhat.com> Cc: Joel Fernandes <joelaf@google.com> Cc: <stable@vger.kernel.org> Signed-off-by: Paul Burton <paulburton@google.com> [ Fixed comment coding style ] Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3816490 commit 1d2865b

1 file changed

Lines changed: 47 additions & 16 deletions

File tree

‎kernel/trace/trace.c‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,8 +2184,15 @@ void tracing_reset_all_online_cpus(void)
21842184
}
21852185
}
21862186

2187+
/*
2188+
* The tgid_map array maps from pid to tgid; i.e. the value stored at index i
2189+
* is the tgid last observed corresponding to pid=i.
2190+
*/
21872191
static int *tgid_map;
21882192

2193+
/* The maximum valid index into tgid_map. */
2194+
static size_t tgid_map_max;
2195+
21892196
#define SAVED_CMDLINES_DEFAULT 128
21902197
#define NO_CMDLINE_MAP UINT_MAX
21912198
static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
@@ -2458,24 +2465,41 @@ void trace_find_cmdline(int pid, char comm[])
24582465
preempt_enable();
24592466
}
24602467

2468+
static int *trace_find_tgid_ptr(int pid)
2469+
{
2470+
/*
2471+
* Pairs with the smp_store_release in set_tracer_flag() to ensure that
2472+
* if we observe a non-NULL tgid_map then we also observe the correct
2473+
* tgid_map_max.
2474+
*/
2475+
int *map = smp_load_acquire(&tgid_map);
2476+
2477+
if (unlikely(!map || pid > tgid_map_max))
2478+
return NULL;
2479+
2480+
return &map[pid];
2481+
}
2482+
24612483
int trace_find_tgid(int pid)
24622484
{
2463-
if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT))
2464-
return 0;
2485+
int *ptr = trace_find_tgid_ptr(pid);
24652486

2466-
return tgid_map[pid];
2487+
return ptr ? *ptr : 0;
24672488
}
24682489

24692490
static int trace_save_tgid(struct task_struct *tsk)
24702491
{
2492+
int *ptr;
2493+
24712494
/* treat recording of idle task as a success */
24722495
if (!tsk->pid)
24732496
return 1;
24742497

2475-
if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT))
2498+
ptr = trace_find_tgid_ptr(tsk->pid);
2499+
if (!ptr)
24762500
return 0;
24772501

2478-
tgid_map[tsk->pid] = tsk->tgid;
2502+
*ptr = tsk->tgid;
24792503
return 1;
24802504
}
24812505

@@ -5171,6 +5195,8 @@ int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
51715195

51725196
int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
51735197
{
5198+
int *map;
5199+
51745200
if ((mask == TRACE_ITER_RECORD_TGID) ||
51755201
(mask == TRACE_ITER_RECORD_CMD))
51765202
lockdep_assert_held(&event_mutex);
@@ -5193,10 +5219,19 @@ int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
51935219
trace_event_enable_cmd_record(enabled);
51945220

51955221
if (mask == TRACE_ITER_RECORD_TGID) {
5196-
if (!tgid_map)
5197-
tgid_map = kvcalloc(PID_MAX_DEFAULT + 1,
5198-
sizeof(*tgid_map),
5199-
GFP_KERNEL);
5222+
if (!tgid_map) {
5223+
tgid_map_max = pid_max;
5224+
map = kvcalloc(tgid_map_max + 1, sizeof(*tgid_map),
5225+
GFP_KERNEL);
5226+
5227+
/*
5228+
* Pairs with smp_load_acquire() in
5229+
* trace_find_tgid_ptr() to ensure that if it observes
5230+
* the tgid_map we just allocated then it also observes
5231+
* the corresponding tgid_map_max value.
5232+
*/
5233+
smp_store_release(&tgid_map, map);
5234+
}
52005235
if (!tgid_map) {
52015236
tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
52025237
return -ENOMEM;
@@ -5610,18 +5645,14 @@ static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
56105645
{
56115646
int pid = ++(*pos);
56125647

5613-
if (pid > PID_MAX_DEFAULT)
5614-
return NULL;
5615-
5616-
return &tgid_map[pid];
5648+
return trace_find_tgid_ptr(pid);
56175649
}
56185650

56195651
static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
56205652
{
5621-
if (!tgid_map || *pos > PID_MAX_DEFAULT)
5622-
return NULL;
5653+
int pid = *pos;
56235654

5624-
return &tgid_map[*pos];
5655+
return trace_find_tgid_ptr(pid);
56255656
}
56265657

56275658
static void saved_tgids_stop(struct seq_file *m, void *v)

0 commit comments

Comments
 (0)