Skip to contentSkip to conversation

ftrace: setup correct FTRACE_FL_REGS flags for module

PATCH New Chengming Zhou 1 participant 2 messages Jul 28, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ftrace: setup correct FTRACE_FL_REGS flags for module

When module loaded and enabled, we will use __ftrace_replace_code for module if any ftrace_ops referenced it found. But we will get wrong ftrace_addr for module rec in ftrace_get_addr_new, because rec->flags has not been setup correctly. It can cause the callback function of a ftrace_ops has FTRACE_OPS_FL_SAVE_REGS to be called with pt_regs set to NULL.
So setup correct FTRACE_FL_REGS flags for rec when we call referenced_filters to find ftrace_ops references it.

---
 kernel/trace/ftrace.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
kernel/trace/ftrace.c +7 -4
index fca01a168ae5..a3d141f53c9e 100644
@@ -6190,8 +6190,11 @@ static int referenced_filters(struct dyn_ftrace *rec)
 	int cnt = 0;
 
 	for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
-		if (ops_references_rec(ops, rec))
-		    cnt++;
+		if (ops_references_rec(ops, rec)) {
+			cnt++;
+			if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
+				rec->flags |= FTRACE_FL_REGS;
+		}
 	}
 
 	return cnt;
@@ -6372,8 +6375,8 @@ void ftrace_module_enable(struct module *mod)
 		if (ftrace_start_up)
 			cnt += referenced_filters(rec);
 
-		/* This clears FTRACE_FL_DISABLED */
-		rec->flags = cnt;
+		rec->flags &= ~FTRACE_FL_DISABLED;
+		rec->flags += cnt;
 
 		if (ftrace_start_up && cnt) {
 			int failed = __ftrace_replace_code(rec, 1);
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/2 ftrace: skip special ftrace_ops when enable module

When enable module, we find all ftrace_ops referenced it on the ftrace_ops_list. But FTRACE_OPS_FL_DIRECT and FTRACE_OPS_FL_IPMODIFY flag are special, and should not be set automatically. So warn and skip ftrace_ops that have these two flags.
Also check if only one ftrace_ops references the module, in which case we can use trampoline as an optimization.

---
 kernel/trace/ftrace.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/trace/ftrace.c +8 -0
index a3d141f53c9e..8c1d9e3daa67 100644
@@ -6191,9 +6191,17 @@ static int referenced_filters(struct dyn_ftrace *rec)
 
 	for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
 		if (ops_references_rec(ops, rec)) {
+			if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
+				continue;
+			if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
+				continue;
 			cnt++;
 			if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
 				rec->flags |= FTRACE_FL_REGS;
+			if (cnt == 1 && ops->trampoline)
+				rec->flags |= FTRACE_FL_TRAMP;
+			else
+				rec->flags &= ~FTRACE_FL_TRAMP;
 		}
 	}
 
Signature · 1 line
2.20.1