Skip to content

Commit 8147dc7

Browse files
committed
ftrace: Add test to make sure compiled time sorts work
Now that ftrace function pointers are sorted at compile time, add a test that makes sure they are sorted at run time. This test is only run if it is configured in. Link: https://lkml.kernel.org/r/[email protected] Cc: Yinan Liu <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent 72b3942 commit 8147dc7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

kernel/trace/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,20 @@ config EVENT_TRACE_TEST_SYSCALLS
915915
TBD - enable a way to actually call the syscalls as we test their
916916
events
917917

918+
config FTRACE_SORT_STARTUP_TEST
919+
bool "Verify compile time sorting of ftrace functions"
920+
depends on DYNAMIC_FTRACE
921+
depends on BUILDTIME_TABLE_SORT
922+
help
923+
Sorting of the mcount_loc sections that is used to find the
924+
where the ftrace knows where to patch functions for tracing
925+
and other callbacks is done at compile time. But if the sort
926+
is not done correctly, it will cause non-deterministic failures.
927+
When this is set, the sorted sections will be verified that they
928+
are in deed sorted and will warn if they are not.
929+
930+
If unsure, say N
931+
918932
config RING_BUFFER_STARTUP_TEST
919933
bool "Ring buffer startup self test"
920934
depends on RING_BUFFER

kernel/trace/ftrace.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6388,6 +6388,27 @@ static int ftrace_cmp_ips(const void *a, const void *b)
63886388
return 0;
63896389
}
63906390

6391+
#ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
6392+
static void test_is_sorted(unsigned long *start, unsigned long count)
6393+
{
6394+
int i;
6395+
6396+
for (i = 1; i < count; i++) {
6397+
if (WARN(start[i - 1] > start[i],
6398+
"[%d] %pS at %lx is not sorted with %pS at %lx\n", i,
6399+
(void *)start[i - 1], start[i - 1],
6400+
(void *)start[i], start[i]))
6401+
break;
6402+
}
6403+
if (i == count)
6404+
pr_info("ftrace section at %px sorted properly\n", start);
6405+
}
6406+
#else
6407+
static void test_is_sorted(unsigned long *start, unsigned long count)
6408+
{
6409+
}
6410+
#endif
6411+
63916412
static int ftrace_process_locs(struct module *mod,
63926413
unsigned long *start,
63936414
unsigned long *end)
@@ -6414,6 +6435,8 @@ static int ftrace_process_locs(struct module *mod,
64146435
if (!IS_ENABLED(CONFIG_BUILDTIME_TABLE_SORT) || mod) {
64156436
sort(start, count, sizeof(*start),
64166437
ftrace_cmp_ips, NULL);
6438+
} else {
6439+
test_is_sorted(start, count);
64176440
}
64186441

64196442
start_pg = ftrace_allocate_pages(count);

0 commit comments

Comments
 (0)