[v3] tracing: add cond_resched to ftrace_replace_code()

Message ID 20181204194044.9506-1-anders.roxell@linaro.org
State New
Headers show
Series
  • [v3] tracing: add cond_resched to ftrace_replace_code()
Related show

Commit Message

Anders Roxell Dec. 4, 2018, 7:40 p.m.
When running in qemu on an kernel built with allmodconfig and debug
options (in particular kcov and ubsan) enabled, ftrace_replace_code
function call take minutes. The ftrace selftest calls
ftrace_replace_code to look >40000 through
ftrace_make_call/ftrace_make_nop, and these end up calling
__aarch64_insn_write/aarch64_insn_patch_text_nosync.

Microseconds add up because this is called in a loop for each dyn_ftrace
record, and this triggers the softlockup watchdog unless we let it sleep
occasionally.

Rework so that we call cond_resched() if !irqs_disabled() && !preempt_count().

Suggested-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Signed-off-by: Anders Roxell <anders.roxell@linaro.org>

---
 kernel/trace/ftrace.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

-- 
2.19.2

Comments

Steven Rostedt Dec. 4, 2018, 10:32 p.m. | #1
On Tue,  4 Dec 2018 20:40:44 +0100
Anders Roxell <anders.roxell@linaro.org> wrote:

> When running in qemu on an kernel built with allmodconfig and debug

> options (in particular kcov and ubsan) enabled, ftrace_replace_code

> function call take minutes. The ftrace selftest calls

> ftrace_replace_code to look >40000 through

> ftrace_make_call/ftrace_make_nop, and these end up calling

> __aarch64_insn_write/aarch64_insn_patch_text_nosync.

> 

> Microseconds add up because this is called in a loop for each dyn_ftrace

> record, and this triggers the softlockup watchdog unless we let it sleep

> occasionally.

> 

> Rework so that we call cond_resched() if !irqs_disabled() && !preempt_count().


This isn't urgent is it? That is, it doesn't need a stable tag?

-- Steve
Anders Roxell Dec. 5, 2018, 10:50 a.m. | #2
On Tue, 4 Dec 2018 at 23:33, Steven Rostedt <rostedt@goodmis.org> wrote:
>

> On Tue,  4 Dec 2018 20:40:44 +0100

> Anders Roxell <anders.roxell@linaro.org> wrote:

>

> > When running in qemu on an kernel built with allmodconfig and debug

> > options (in particular kcov and ubsan) enabled, ftrace_replace_code

> > function call take minutes. The ftrace selftest calls

> > ftrace_replace_code to look >40000 through

> > ftrace_make_call/ftrace_make_nop, and these end up calling

> > __aarch64_insn_write/aarch64_insn_patch_text_nosync.

> >

> > Microseconds add up because this is called in a loop for each dyn_ftrace

> > record, and this triggers the softlockup watchdog unless we let it sleep

> > occasionally.

> >

> > Rework so that we call cond_resched() if !irqs_disabled() && !preempt_count().

>

> This isn't urgent is it?


No, it is not urgent. I'm trying to get allmodconfig to boot on arm64.

> That is, it doesn't need a stable tag?


Also I don't think its a regression, since clearly nobody has ever been
able to boot an arm64 allmodconfig kernel or anything close to that.

Cheers,
Anders

Patch

diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index c375e33239f7..7c9f49a5a1ab 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -2419,11 +2419,19 @@  void __weak ftrace_replace_code(int enable)
 {
 	struct dyn_ftrace *rec;
 	struct ftrace_page *pg;
+	bool schedulable;
 	int failed;
 
 	if (unlikely(ftrace_disabled))
 		return;
 
+	/*
+	 * Some archs call this function with interrupts or preemption
+	 * disabled. However, for other archs that can preempt, this can cause
+	 * an tremendous unneeded latency.
+	 */
+	schedulable = !irqs_disabled() && !preempt_count();
+
 	do_for_each_ftrace_rec(pg, rec) {
 
 		if (rec->flags & FTRACE_FL_DISABLED)
@@ -2435,6 +2443,8 @@  void __weak ftrace_replace_code(int enable)
 			/* Stop processing */
 			return;
 		}
+		if (schedulable)
+			cond_resched();
 	} while_for_each_ftrace_rec();
 }