Skip to content

Commit eb81b5a

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 3cda5b7 commit eb81b5a

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
@@ -2181,8 +2181,15 @@ void tracing_reset_all_online_cpus(void)
21812181
}
21822182
}
21832183

2184+
/*
2185+
* The tgid_map array maps from pid to tgid; i.e. the value stored at index i
2186+
* is the tgid last observed corresponding to pid=i.
2187+
*/
21842188
static int *tgid_map;
21852189

2190+
/* The maximum valid index into tgid_map. */
2191+
static size_t tgid_map_max;
2192+
21862193
#define SAVED_CMDLINES_DEFAULT 128
21872194
#define NO_CMDLINE_MAP UINT_MAX
21882195
static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
@@ -2455,24 +2462,41 @@ void trace_find_cmdline(int pid, char comm[])
24552462
preempt_enable();
24562463
}
24572464

2465+
static int *trace_find_tgid_ptr(int pid)
2466+
{
2467+
/*
2468+
* Pairs with the smp_store_release in set_tracer_flag() to ensure that
2469+
* if we observe a non-NULL tgid_map then we also observe the correct
2470+
* tgid_map_max.
2471+
*/
2472+
int *map = smp_load_acquire(&tgid_map);
2473+
2474+
if (unlikely(!map || pid > tgid_map_max))
2475+
return NULL;
2476+
2477+
return &map[pid];
2478+
}
2479+
24582480
int trace_find_tgid(int pid)
24592481
{
2460-
if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT))
2461-
return 0;
2482+
int *ptr = trace_find_tgid_ptr(pid);
24622483

2463-
return tgid_map[pid];
2484+
return ptr ? *ptr : 0;
24642485
}
24652486

24662487
static int trace_save_tgid(struct task_struct *tsk)
24672488
{
2489+
int *ptr;
2490+
24682491
/* treat recording of idle task as a success */
24692492
if (!tsk->pid)
24702493
return 1;
24712494

2472-
if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT))
2495+
ptr = trace_find_tgid_ptr(tsk->pid);
2496+
if (!ptr)
24732497
return 0;
24742498

2475-
tgid_map[tsk->pid] = tsk->tgid;
2499+
*ptr = tsk->tgid;
24762500
return 1;
24772501
}
24782502

@@ -4847,6 +4871,8 @@ int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
48474871

48484872
int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
48494873
{
4874+
int *map;
4875+
48504876
if ((mask == TRACE_ITER_RECORD_TGID) ||
48514877
(mask == TRACE_ITER_RECORD_CMD))
48524878
lockdep_assert_held(&event_mutex);
@@ -4869,10 +4895,19 @@ int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
48694895
trace_event_enable_cmd_record(enabled);
48704896

48714897
if (mask == TRACE_ITER_RECORD_TGID) {
4872-
if (!tgid_map)
4873-
tgid_map = kvcalloc(PID_MAX_DEFAULT + 1,
4874-
sizeof(*tgid_map),
4875-
GFP_KERNEL);
4898+
if (!tgid_map) {
4899+
tgid_map_max = pid_max;
4900+
map = kvcalloc(tgid_map_max + 1, sizeof(*tgid_map),
4901+
GFP_KERNEL);
4902+
4903+
/*
4904+
* Pairs with smp_load_acquire() in
4905+
* trace_find_tgid_ptr() to ensure that if it observes
4906+
* the tgid_map we just allocated then it also observes
4907+
* the corresponding tgid_map_max value.
4908+
*/
4909+
smp_store_release(&tgid_map, map);
4910+
}
48764911
if (!tgid_map) {
48774912
tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
48784913
return -ENOMEM;
@@ -5286,18 +5321,14 @@ static void *saved_tgids_next(struct seq_file *m, void *v, loff_t *pos)
52865321
{
52875322
int pid = ++(*pos);
52885323

5289-
if (pid > PID_MAX_DEFAULT)
5290-
return NULL;
5291-
5292-
return &tgid_map[pid];
5324+
return trace_find_tgid_ptr(pid);
52935325
}
52945326

52955327
static void *saved_tgids_start(struct seq_file *m, loff_t *pos)
52965328
{
5297-
if (!tgid_map || *pos > PID_MAX_DEFAULT)
5298-
return NULL;
5329+
int pid = *pos;
52995330

5300-
return &tgid_map[*pos];
5331+
return trace_find_tgid_ptr(pid);
53015332
}
53025333

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

0 commit comments

Comments
 (0)