1

function_graph: Use a simple LRU for fgraph_array index number

Since the fgraph_array index is used for the bitmap on the shadow
stack, it may leave some entries after a function_graph instance is
removed. Thus if another instance reuses the fgraph_array index soon
after releasing it, the fgraph may confuse to call the newer callback
for the entries which are pushed by the older instance.
To avoid reusing the fgraph_array index soon after releasing, introduce
a simple LRU table for managing the index number. This will reduce the
possibility of this confusion.

Link: https://lore.kernel.org/linux-trace-kernel/171509103267.162236.6885097397289135378.stgit@devnote2
Link: https://lore.kernel.org/linux-trace-kernel/20240603190823.147421545@goodmis.org

Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Florent Revest <revest@chromium.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>
Cc: bpf <bpf@vger.kernel.org>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Alan Maguire <alan.maguire@oracle.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Guo Ren <guoren@kernel.org>
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
This commit is contained in:
Masami Hiramatsu (Google) 2024-06-03 15:07:18 -04:00 committed by Steven Rostedt (Google)
parent df3ec5da6a
commit 6d4786592a

View File

@ -124,10 +124,48 @@ enum {
DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph);
int ftrace_graph_active;
static int fgraph_array_cnt;
static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE];
/* LRU index table for fgraph_array */
static int fgraph_lru_table[FGRAPH_ARRAY_SIZE];
static int fgraph_lru_next;
static int fgraph_lru_last;
/* Initialize fgraph_lru_table with unused index */
static void fgraph_lru_init(void)
{
int i;
for (i = 0; i < FGRAPH_ARRAY_SIZE; i++)
fgraph_lru_table[i] = i;
}
/* Release the used index to the LRU table */
static int fgraph_lru_release_index(int idx)
{
if (idx < 0 || idx >= FGRAPH_ARRAY_SIZE ||
WARN_ON_ONCE(fgraph_lru_table[fgraph_lru_last] != -1))
return -1;
fgraph_lru_table[fgraph_lru_last] = idx;
fgraph_lru_last = (fgraph_lru_last + 1) % FGRAPH_ARRAY_SIZE;
return 0;
}
/* Allocate a new index from LRU table */
static int fgraph_lru_alloc_index(void)
{
int idx = fgraph_lru_table[fgraph_lru_next];
/* No id is available */
if (idx == -1)
return -1;
fgraph_lru_table[fgraph_lru_next] = -1;
fgraph_lru_next = (fgraph_lru_next + 1) % FGRAPH_ARRAY_SIZE;
return idx;
}
/* Get the FRAME_OFFSET from the word from the @offset on ret_stack */
static inline int get_frame_offset(struct task_struct *t, int offset)
{
@ -374,7 +412,7 @@ int function_graph_enter(unsigned long ret, unsigned long func,
if (offset < 0)
goto out;
for (i = 0; i < fgraph_array_cnt; i++) {
for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) {
struct fgraph_ops *gops = fgraph_array[i];
if (gops == &fgraph_stub)
@ -925,7 +963,7 @@ int register_ftrace_graph(struct fgraph_ops *gops)
{
int command = 0;
int ret = 0;
int i;
int i = -1;
mutex_lock(&ftrace_lock);
@ -933,21 +971,16 @@ int register_ftrace_graph(struct fgraph_ops *gops)
/* The array must always have real data on it */
for (i = 0; i < FGRAPH_ARRAY_SIZE; i++)
fgraph_array[i] = &fgraph_stub;
fgraph_lru_init();
}
/* Look for an available spot */
for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) {
if (fgraph_array[i] == &fgraph_stub)
break;
}
if (i >= FGRAPH_ARRAY_SIZE) {
i = fgraph_lru_alloc_index();
if (i < 0 || WARN_ON_ONCE(fgraph_array[i] != &fgraph_stub)) {
ret = -ENOSPC;
goto out;
}
fgraph_array[i] = gops;
if (i + 1 > fgraph_array_cnt)
fgraph_array_cnt = i + 1;
gops->idx = i;
ftrace_graph_active++;
@ -975,6 +1008,7 @@ error:
fgraph_array[i] = &fgraph_stub;
ftrace_graph_active--;
gops->saved_func = NULL;
fgraph_lru_release_index(i);
}
out:
mutex_unlock(&ftrace_lock);
@ -984,25 +1018,20 @@ out:
void unregister_ftrace_graph(struct fgraph_ops *gops)
{
int command = 0;
int i;
mutex_lock(&ftrace_lock);
if (unlikely(!ftrace_graph_active))
goto out;
if (unlikely(gops->idx < 0 || gops->idx >= fgraph_array_cnt))
if (unlikely(gops->idx < 0 || gops->idx >= FGRAPH_ARRAY_SIZE ||
fgraph_array[gops->idx] != gops))
goto out;
WARN_ON_ONCE(fgraph_array[gops->idx] != gops);
if (fgraph_lru_release_index(gops->idx) < 0)
goto out;
fgraph_array[gops->idx] = &fgraph_stub;
if (gops->idx + 1 == fgraph_array_cnt) {
i = gops->idx;
while (i >= 0 && fgraph_array[i] == &fgraph_stub)
i--;
fgraph_array_cnt = i + 1;
}
ftrace_graph_active--;