Skip to content

Commit 33c2606

Browse files
rostedtmehmetb0
authored andcommitted
tracing: Have saved_cmdlines arrays all in one allocation
BugLink: https://bugs.launchpad.net/bugs/2097393 [ Upstream commit 0b18c85 ] The saved_cmdlines have three arrays for mapping PIDs to COMMs: - map_pid_to_cmdline[] - map_cmdline_to_pid[] - saved_cmdlines The map_pid_to_cmdline[] is PID_MAX_DEFAULT in size and holds the index into the other arrays. The map_cmdline_to_pid[] is a mapping back to the full pid as it can be larger than PID_MAX_DEFAULT. And the saved_cmdlines[] just holds the COMMs associated to the pids. Currently the map_pid_to_cmdline[] and saved_cmdlines[] are allocated together (in reality the saved_cmdlines is just in the memory of the rounding of the allocation of the structure as it is always allocated in powers of two). The map_cmdline_to_pid[] array is allocated separately. Since the rounding to a power of two is rather large (it allows for 8000 elements in saved_cmdlines), also include the map_cmdline_to_pid[] array. (This drops it to 6000 by default, which is still plenty for most use cases). This saves even more memory as the map_cmdline_to_pid[] array doesn't need to be allocated. Link: https://lore.kernel.org/linux-trace-kernel/20240212174011.068211d9@gandalf.local.home/ Link: https://lore.kernel.org/linux-trace-kernel/20240220140703.182330529@goodmis.org Cc: Mark Rutland <mark.rutland@arm.com> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Tim Chen <tim.c.chen@linux.intel.com> Cc: Vincent Donnefort <vdonnefort@google.com> Cc: Sven Schnelle <svens@linux.ibm.com> Cc: Mete Durlu <meted@linux.ibm.com> Fixes: 44dc5c4 ("tracing: Fix wasted memory in saved_cmdlines logic") Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Koichiro Den <koichiro.den@canonical.com>
1 parent 6a51c96 commit 33c2606

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

kernel/trace/trace.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,10 @@ struct saved_cmdlines_buffer {
23462346
};
23472347
static struct saved_cmdlines_buffer *savedcmd;
23482348

2349+
/* Holds the size of a cmdline and pid element */
2350+
#define SAVED_CMDLINE_MAP_ELEMENT_SIZE(s) \
2351+
(TASK_COMM_LEN + sizeof((s)->map_cmdline_to_pid[0]))
2352+
23492353
static inline char *get_saved_cmdlines(int idx)
23502354
{
23512355
return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
@@ -2360,7 +2364,6 @@ static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s)
23602364
{
23612365
int order = get_order(sizeof(*s) + s->cmdline_num * TASK_COMM_LEN);
23622366

2363-
kfree(s->map_cmdline_to_pid);
23642367
kmemleak_free(s);
23652368
free_pages((unsigned long)s, order);
23662369
}
@@ -2373,7 +2376,7 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val)
23732376
int order;
23742377

23752378
/* Figure out how much is needed to hold the given number of cmdlines */
2376-
orig_size = sizeof(*s) + val * TASK_COMM_LEN;
2379+
orig_size = sizeof(*s) + val * SAVED_CMDLINE_MAP_ELEMENT_SIZE(s);
23772380
order = get_order(orig_size);
23782381
size = 1 << (order + PAGE_SHIFT);
23792382
page = alloc_pages(GFP_KERNEL, order);
@@ -2385,16 +2388,11 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val)
23852388
memset(s, 0, sizeof(*s));
23862389

23872390
/* Round up to actual allocation */
2388-
val = (size - sizeof(*s)) / TASK_COMM_LEN;
2391+
val = (size - sizeof(*s)) / SAVED_CMDLINE_MAP_ELEMENT_SIZE(s);
23892392
s->cmdline_num = val;
23902393

2391-
s->map_cmdline_to_pid = kmalloc_array(val,
2392-
sizeof(*s->map_cmdline_to_pid),
2393-
GFP_KERNEL);
2394-
if (!s->map_cmdline_to_pid) {
2395-
free_saved_cmdlines_buffer(s);
2396-
return NULL;
2397-
}
2394+
/* Place map_cmdline_to_pid array right after saved_cmdlines */
2395+
s->map_cmdline_to_pid = (unsigned *)&s->saved_cmdlines[val * TASK_COMM_LEN];
23982396

23992397
s->cmdline_idx = 0;
24002398
memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,

0 commit comments

Comments
 (0)