Skip to content

Commit cf04f2d

Browse files
committed
ftrace: Still disable enabled records marked as disabled
Weak functions started causing havoc as they showed up in the "available_filter_functions" and this confused people as to why some functions marked as "notrace" were listed, but when enabled they did nothing. This was because weak functions can still have fentry calls, and these addresses get added to the "available_filter_functions" file. kallsyms is what converts those addresses to names, and since the weak functions are not listed in kallsyms, it would just pick the function before that. To solve this, there was a trick to detect weak functions listed, and these records would be marked as DISABLED so that they do not get enabled and are mostly ignored. As the processing of the list of all functions to figure out what is weak or not can take a long time, this process is put off into a kernel thread and run in parallel with the rest of start up. Now the issue happens whet function tracing is enabled via the kernel command line. As it starts very early in boot up, it can be enabled before the records that are weak are marked to be disabled. This causes an issue in the accounting, as the weak records are enabled by the command line function tracing, but after boot up, they are not disabled. The ftrace records have several accounting flags and a ref count. The DISABLED flag is just one. If the record is enabled before it is marked DISABLED it will get an ENABLED flag and also have its ref counter incremented. After it is marked for DISABLED, neither the ENABLED flag nor the ref counter is cleared. There's sanity checks on the records that are performed after an ftrace function is registered or unregistered, and this detected that there were records marked as ENABLED with ref counter that should not have been. Note, the module loading code uses the DISABLED flag as well to keep its functions from being modified while its being loaded and some of these flags may get set in this process. So changing the verification code to ignore DISABLED records is a no go, as it still needs to verify that the module records are working too. Also, the weak functions still are calling a trampoline. Even though they should never be called, it is dangerous to leave these weak functions calling a trampoline that is freed, so they should still be set back to nops. There's two places that need to not skip records that have the ENABLED and the DISABLED flags set. That is where the ftrace_ops is processed and sets the records ref counts, and then later when the function itself is to be updated, and the ENABLED flag gets removed. Add a helper function "skip_record()" that returns true if the record has the DISABLED flag set but not the ENABLED flag. Link: https://lkml.kernel.org/r/[email protected] Cc: Masami Hiramatsu <[email protected]> Cc: Andrew Morton <[email protected]> Cc: [email protected] Fixes: b39181f ("ftrace: Add FTRACE_MCOUNT_MAX_OFFSET to avoid adding weak function") Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent e5d2718 commit cf04f2d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

kernel/trace/ftrace.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,18 @@ ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_ex
16441644
static struct ftrace_ops *
16451645
ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
16461646

1647+
static bool skip_record(struct dyn_ftrace *rec)
1648+
{
1649+
/*
1650+
* At boot up, weak functions are set to disable. Function tracing
1651+
* can be enabled before they are, and they still need to be disabled now.
1652+
* If the record is disabled, still continue if it is marked as already
1653+
* enabled (this is needed to keep the accounting working).
1654+
*/
1655+
return rec->flags & FTRACE_FL_DISABLED &&
1656+
!(rec->flags & FTRACE_FL_ENABLED);
1657+
}
1658+
16471659
static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
16481660
int filter_hash,
16491661
bool inc)
@@ -1693,7 +1705,7 @@ static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
16931705
int in_hash = 0;
16941706
int match = 0;
16951707

1696-
if (rec->flags & FTRACE_FL_DISABLED)
1708+
if (skip_record(rec))
16971709
continue;
16981710

16991711
if (all) {
@@ -2126,7 +2138,7 @@ static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
21262138

21272139
ftrace_bug_type = FTRACE_BUG_UNKNOWN;
21282140

2129-
if (rec->flags & FTRACE_FL_DISABLED)
2141+
if (skip_record(rec))
21302142
return FTRACE_UPDATE_IGNORE;
21312143

21322144
/*
@@ -2241,7 +2253,7 @@ static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
22412253
if (update) {
22422254
/* If there's no more users, clear all flags */
22432255
if (!ftrace_rec_count(rec))
2244-
rec->flags = 0;
2256+
rec->flags &= FTRACE_FL_DISABLED;
22452257
else
22462258
/*
22472259
* Just disable the record, but keep the ops TRAMP
@@ -2634,7 +2646,7 @@ void __weak ftrace_replace_code(int mod_flags)
26342646

26352647
do_for_each_ftrace_rec(pg, rec) {
26362648

2637-
if (rec->flags & FTRACE_FL_DISABLED)
2649+
if (skip_record(rec))
26382650
continue;
26392651

26402652
failed = __ftrace_replace_code(rec, enable);

0 commit comments

Comments
 (0)