Skip to contentSkip to conversation

x86: use new text_poke_bp in ftrace

PATCH Superseded Petr Mladek 3 participants 16 messages Nov 8, 2013 – Nov 12, 2013

Subsystems (4)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86: use new text_poke_bp in ftrace

The commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching) uses the same technique that has been used in ftrace since 08d636b ("ftrace/x86: Have arch x86_64 use breakpoints instead of stop machine")

This patch set merge the two implementations and remove duplicities from the ftrace side. It adds a generic function to efficiently patch more instructions at once. It also makes the generic int3-based framework and the ftrace code a bit faster.

The first patch speed up the existing text_poke_bp.

The next 3 patches improve the generic int3-based framework to be usable with ftrace. All the changes are based on ideas already used in ftrace. They are needed to keep the functionality and efficiency.

The 5th patch speedups the original ftrace code but it is useful also with the generic functions.

The last three patches modifies different parts of the current x86-specific ftrace implementation and use the generic function there.

Changes in v2:

+ check for number of CPUs instead of enabling IRQs when syncing CPUs; suggested by Steven Rostedt, Paul E. McKenney, and Masami Hiramatsu
+ return error codes in text_poke_part and text_poke_bp; needed by ftrace
+ reverted changes in text_poke_bp; it patches only single address again
+ introduce text_poke_bp_iter for patching multiple addresses:
+ uses iterator and callbacks instead of copying data
+ checks old code before patching
+ returns error code and more info about error; needed by ftrace
+ provides recovery mechanism in case of errors
+ update ftrace.c to use the new text_poke_bp_iter
+ split notrace __probe_kernel_read into separate patch because it is useful for original ftrace code as well
+ rebased on current kernel tip and updated performance statistics; it started to be slower on idle machine after the commit commit c229828ca6bc62d6c654 (rcu: Throttle rcu_try_advance_all_cbs() execution)

Unfortunately, the size of the code is almost the same. But most of it is generic and can be reused.

Also I tried to switch between 7 tracers: blk, branch, function_graph, wakeup_rt, irqsoff, function, and nop. Every tracer has also been enabled and disabled. With 500 cycles, I got these times before the change:

    real    18m2.477s     18m8.654s     18m9.196s
    user    0m0.008s      0m0.008s	0m0.012s
    sys     0m17.316s     0m17.104s	0m17.300s

and after

    real    17m9.753s     17m12.272s    17m11.424s
    user    0m0.004s      0m0.004s	0m0.004s
    sys     0m18.244s     0m18.252s	0m18.308s

The patches are against kernel/git/tip/tip.git on top of the commit af7949e870d4632b Merge branch 'tools/kvm perf fixes'

Petr Mladek (8):
x86: speed up int3-based patching using less paranoid write
x86: return error code in text_poke_bp
x86: allow to call text_poke_bp during boot
x86: add generic function to modify more calls using int3 framework
x86: do not trace __probe_kernel_read
x86: modify ftrace function using the new int3-based framework
x86: patch all traced function calls using the int3-based framework
x86: enable/disable ftrace graph call using new int3-based framework

 arch/x86/include/asm/alternative.h |  39 ++-
 arch/x86/kernel/alternative.c      | 321 +++++++++++++++++++--
 arch/x86/kernel/ftrace.c           | 571 ++++++++-----------------------------
 arch/x86/kernel/traps.c            |  10 -
 include/linux/ftrace.h             |   6 -
 mm/maccess.c                       |   2 +-
 6 files changed, 462 insertions(+), 487 deletions(-)
Signature · 1 line
1.8.4
#1 lore raw

v2 cover x86: use new text_poke_bp in ftrace

The commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching) uses the same technique that has been used in ftrace since 08d636b ("ftrace/x86: Have arch x86_64 use breakpoints instead of stop machine")

This patch set merge the two implementations and remove duplicities from the ftrace side. It adds a generic function to efficiently patch more instructions at once. It also makes the generic int3-based framework and the ftrace code a bit faster.

The first patch speed up the existing text_poke_bp.

The next 3 patches improve the generic int3-based framework to be usable with ftrace. All the changes are based on ideas already used in ftrace. They are needed to keep the functionality and efficiency.

The 5th patch speedups the original ftrace code but it is useful also with the generic functions.

The last three patches modifies different parts of the current x86-specific ftrace implementation and use the generic function there.

Changes in v2:

+ check for number of CPUs instead of enabling IRQs when syncing CPUs; suggested by Steven Rostedt, Paul E. McKenney, and Masami Hiramatsu
+ return error codes in text_poke_part and text_poke_bp; needed by ftrace
+ reverted changes in text_poke_bp; it patches only single address again
+ introduce text_poke_bp_iter for patching multiple addresses:
+ uses iterator and callbacks instead of copying data
+ checks old code before patching
+ returns error code and more info about error; needed by ftrace
+ provides recovery mechanism in case of errors
+ update ftrace.c to use the new text_poke_bp_iter
+ split notrace __probe_kernel_read into separate patch because it is useful for original ftrace code as well
+ rebased on current kernel tip and updated performance statistics; it started to be slower on idle machine after the commit commit c229828ca6bc62d6c654 (rcu: Throttle rcu_try_advance_all_cbs() execution)

I tried to switch between 7 tracers: blk, branch, function_graph, wakeup_rt, irqsoff, function, and nop. Every tracer has also been enabled and disabled. With 500 cycles, I got these times before the change:

    real    18m2.477s     18m8.654s     18m9.196s
    user    0m0.008s      0m0.008s	0m0.012s
    sys     0m17.316s     0m17.104s	0m17.300s

and after

    real    17m9.753s     17m12.272s    17m11.424s
    user    0m0.004s      0m0.004s	0m0.004s
    sys     0m18.244s     0m18.252s	0m18.308s

Unfortunately, the size of the code is almost the same. But most of it is generic and can be reused.

The patches are against kernel/git/tip/tip.git on top of the commit af7949e870d4632b Merge branch 'tools/kvm perf fixes'

Petr Mladek (8):
x86: speed up int3-based patching using less paranoid write
x86: return error code in text_poke_bp
x86: allow to call text_poke_bp during boot
x86: add generic function to modify more calls using int3 framework
x86: do not trace __probe_kernel_read
x86: modify ftrace function using the new int3-based framework
x86: patch all traced function calls using the int3-based framework
x86: enable/disable ftrace graph call using new int3-based framework

 arch/x86/include/asm/alternative.h |  39 ++-
 arch/x86/kernel/alternative.c      | 321 +++++++++++++++++++--
 arch/x86/kernel/ftrace.c           | 571 ++++++++-----------------------------
 arch/x86/kernel/traps.c            |  10 -
 include/linux/ftrace.h             |   6 -
 mm/maccess.c                       |   2 +-
 6 files changed, 462 insertions(+), 487 deletions(-)
Signature · 1 line
1.8.4
#2 lore raw

PATCH v2 1/8 x86: speed up int3-based patching using less paranoid write

In reply to Petr Mladek

This change is inspired by the int3-based patching code used in ftrace. See the commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching).

When trying to use text_poke_bp in ftrace, the result was slower than the original implementation.

It turned out that one difference was in text_poke vs. ftrace_write. text_poke did many extra operations to make sure that the change was atomic.

In fact, we do not need this luxury in text_poke_bp because the modified code is guarded by the int3 handler. The int3 interrupt itself is added and removed by an atomic operation because we modify only one byte.

This patch adds text_poke_part which is inspired by ftrace_write. Then it is used in text_poke_bp instead of the paranoid text_poke.

For example, I tried to switch between 7 tracers: blk, branch, function_graph, wakeup_rt, irqsoff, function, and nop. Every tracer has also been enabled and disabled. With 100 cycles, I got these times with text_poke:

    real    25m7.380s     25m13.44s     25m9.072s
    user    0m0.004s      0m0.004s      0m0.004s
    sys     0m3.480s      0m3.508s      0m3.420s

and with text_poke_part:

    real    3m23.335s     3m24.422s     3m26.686s
    user    0m0.004s      0m0.004s	0m0.004s
    sys     0m3.724s      0m3.772s	0m3.588s
---
 arch/x86/kernel/alternative.c | 49 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 44 insertions(+), 5 deletions(-)
arch/x86/kernel/alternative.c +44 -5
index df94598..0586dc1 100644
@@ -8,6 +8,7 @@
 #include <linux/kprobes.h>
 #include <linux/mm.h>
 #include <linux/vmalloc.h>
+#include <linux/uaccess.h>
 #include <linux/memory.h>
 #include <linux/stop_machine.h>
 #include <linux/slab.h>
@@ -586,6 +587,44 @@ void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
 	return addr;
 }
 
+/**
+ * text_poke_part - Update part of the instruction on a live kernel when using
+ * 		    int3 breakpoint
+ * @addr:   address to modify
+ * @opcode: source of the copy
+ * @len:    length to copy
+ *
+ * If we patch instructions using int3 interrupt, we do not need to be so
+ * careful about an atomic write. Adding and removing the interrupt is atomic
+ * because we modify only one byte. The rest of the instruction could be
+ * modified in several steps because it is guarded by the interrupt handler.
+ * Hence we could use faster code here. See also text_poke_bp below for more
+ * details.
+ *
+ * Note: Must be called under text_mutex.
+ */
+static int text_poke_part(void *addr, const void *opcode, size_t len)
+{
+	/*
+	 * On x86_64, kernel text mappings are mapped read-only with
+	 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
+	 * of the kernel text mapping to modify the kernel text.
+	 *
+	 * For 32bit kernels, these mappings are same and we can use
+	 * kernel identity mapping to modify code.
+	 */
+	if (((unsigned long)addr >= (unsigned long)_text) &&
+	    ((unsigned long)addr < (unsigned long)_etext))
+		addr = __va(__pa_symbol(addr));
+
+	if (probe_kernel_write(addr, opcode, len))
+		return -EPERM;
+
+	sync_core();
+
+	return 0;
+}
+
 static void do_sync_core(void *info)
 {
 	sync_core();
@@ -648,15 +687,15 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	 */
 	smp_wmb();
 
-	text_poke(addr, &int3, sizeof(int3));
+	text_poke_part(addr, &int3, sizeof(int3));
 
 	on_each_cpu(do_sync_core, NULL, 1);
 
 	if (len - sizeof(int3) > 0) {
 		/* patch all but the first byte */
-		text_poke((char *)addr + sizeof(int3),
-			  (const char *) opcode + sizeof(int3),
-			  len - sizeof(int3));
+		text_poke_part((char *)addr + sizeof(int3),
+			       (const char *) opcode + sizeof(int3),
+			       len - sizeof(int3));
 		/*
 		 * According to Intel, this core syncing is very likely
 		 * not necessary and we'd be safe even without it. But
@@ -666,7 +705,7 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	}
 
 	/* patch the first byte */
-	text_poke(addr, opcode, sizeof(int3));
+	text_poke_part(addr, opcode, sizeof(int3));
 
 	on_each_cpu(do_sync_core, NULL, 1);
 
Signature · 1 line
1.8.4
#3 lore raw

Re: [PATCH v2 1/8] x86: speed up int3-based patching using less paranoid write

Quoted 10 lines · Petr Mladek

This change is inspired by the int3-based patching code used in ftrace. See the commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching).

When trying to use text_poke_bp in ftrace, the result was slower than the original implementation.

It turned out that one difference was in text_poke vs. ftrace_write. text_poke did many extra operations to make sure that the change was atomic.

AFAIK, the main reason why text_poke is used is avoiding RODATA protection (by alias mapping).

Quoted 56 lines

In fact, we do not need this luxury in text_poke_bp because the modified code is guarded by the int3 handler. The int3 interrupt itself is added and removed by an atomic operation because we modify only one byte.

This patch adds text_poke_part which is inspired by ftrace_write. Then it is used in text_poke_bp instead of the paranoid text_poke.

For example, I tried to switch between 7 tracers: blk, branch, function_graph, wakeup_rt, irqsoff, function, and nop. Every tracer has also been enabled and disabled. With 100 cycles, I got these times with text_poke:

    real    25m7.380s     25m13.44s     25m9.072s
    user    0m0.004s      0m0.004s      0m0.004s
    sys     0m3.480s      0m3.508s      0m3.420s

and with text_poke_part:

    real    3m23.335s     3m24.422s     3m26.686s
    user    0m0.004s      0m0.004s	0m0.004s
    sys     0m3.724s      0m3.772s	0m3.588s
---
 arch/x86/kernel/alternative.c | 49 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 44 insertions(+), 5 deletions(-)
arch/x86/kernel/alternative.c +13 -0
index df94598..0586dc1 100644
@@ -8,6 +8,7 @@
 #include <linux/kprobes.h>
 #include <linux/mm.h>
 #include <linux/vmalloc.h>
+#include <linux/uaccess.h>
 #include <linux/memory.h>
 #include <linux/stop_machine.h>
 #include <linux/slab.h>
@@ -586,6 +587,44 @@ void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
 	return addr;
 }
 
+/**
+ * text_poke_part - Update part of the instruction on a live kernel when using
+ * 		    int3 breakpoint
+ * @addr:   address to modify
+ * @opcode: source of the copy
+ * @len:    length to copy
+ *
+ * If we patch instructions using int3 interrupt, we do not need to be so
+ * careful about an atomic write. Adding and removing the interrupt is atomic
+ * because we modify only one byte. The rest of the instruction could be
+ * modified in several steps because it is guarded by the interrupt handler.
+ * Hence we could use faster code here. See also text_poke_bp below for more

arch/x86/kernel/alternative.c void *__kprobes text_poke(void *addr, const void *opcode, size_t len) · line 611

Quoted 10 lines
arch/x86/kernel/alternative.c +10 -0
+ * details.
+ *
+ * Note: Must be called under text_mutex.
+ */
+static int text_poke_part(void *addr, const void *opcode, size_t len)
+{
+	/*
+	 * On x86_64, kernel text mappings are mapped read-only with
+	 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
+	 * of the kernel text mapping to modify the kernel text.

Hmm, I couldn't find the guarantee that __va(__pa_symbol(x)) gives us a raw(writable) text pages always.
Even it is true, it seems wired that we can still rewrite such protected page without any spacial page remapping operation.

arch/x86/kernel/alternative.c void *__kprobes text_poke(void *addr, const void *opcode, size_t len) · line 618

Quoted 7 lines
arch/x86/kernel/alternative.c +7 -0
+	 *
+	 * For 32bit kernels, these mappings are same and we can use
+	 * kernel identity mapping to modify code.
+	 */
+	if (((unsigned long)addr >= (unsigned long)_text) &&
+	    ((unsigned long)addr < (unsigned long)_etext))
+		addr = __va(__pa_symbol(addr));

You should also warn or return error if the given addr is not in the kernel text.

Quoted 13 lines
arch/x86/kernel/alternative.c +9 -0
+
+	if (probe_kernel_write(addr, opcode, len))
+		return -EPERM;
+
+	sync_core();
+
+	return 0;
+}
+
 static void do_sync_core(void *info)
 {
 	sync_core();

arch/x86/kernel/alternative.c void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) · line 690

Quoted 5 lines
arch/x86/kernel/alternative.c +1 -1
@@ -648,15 +687,15 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	 */
 	smp_wmb();
 
-	text_poke(addr, &int3, sizeof(int3));
+	text_poke_part(addr, &int3, sizeof(int3));

Anyway, text_poke itself will cause a BUG if it hits an error, but text_poke_part() seems silently failing. It should handle the error correctly (or call BUG()).

arch/x86/kernel/alternative.c void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) · line 698

Quoted 11 lines
arch/x86/kernel/alternative.c +3 -3
 	on_each_cpu(do_sync_core, NULL, 1);
 
 	if (len - sizeof(int3) > 0) {
 		/* patch all but the first byte */
-		text_poke((char *)addr + sizeof(int3),
-			  (const char *) opcode + sizeof(int3),
-			  len - sizeof(int3));
+		text_poke_part((char *)addr + sizeof(int3),
+			       (const char *) opcode + sizeof(int3),
+			       len - sizeof(int3));

Here we should check an error too.

arch/x86/kernel/alternative.c void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) · line 708

Quoted 9 lines
arch/x86/kernel/alternative.c +1 -1
 		/*
 		 * According to Intel, this core syncing is very likely
 		 * not necessary and we'd be safe even without it. But
@@ -666,7 +705,7 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	}
 
 	/* patch the first byte */
-	text_poke(addr, opcode, sizeof(int3));
+	text_poke_part(addr, opcode, sizeof(int3));

Ditto.

arch/x86/kernel/alternative.c void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) · line 709

Quoted 2 lines
arch/x86/kernel/alternative.c +0 -0
 	on_each_cpu(do_sync_core, NULL, 1);

Thank you,

Signature · 4 lines
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
maintainer
#4 lore raw

Re: [PATCH v2 1/8] x86: speed up int3-based patching using less paranoid write

Quoted 8 lines · On Fri, 08 Nov 2013 21:04:26 +0900 Masami Hiramatsu…
Quoted 7 lines · Petr Mladek

This change is inspired by the int3-based patching code used in ftrace. See the commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching).

When trying to use text_poke_bp in ftrace, the result was slower than the original implementation.

Quoted 6 lines
Quoted 3 lines

It turned out that one difference was in text_poke vs. ftrace_write. text_poke did many extra operations to make sure that the change was atomic.

AFAIK, the main reason why text_poke is used is avoiding RODATA protection (by alias mapping).

That is correct, and the reason ftrace didn't do that is because it would be quite expensive to map 22,000 addresses for each change.

-- Steve

#5 lore raw

Re: [PATCH v2 1/8] x86: speed up int3-based patching using less paranoid write

Steven Rostedt píše v Pá 08. 11. 2013 v 07:43 -0500:

Quoted 15 lines
Quoted 12 lines · On Fri, 08 Nov 2013 21:04:26 +0900 Masami Hiramatsu…
Quoted 10 lines · Petr Mladek

This change is inspired by the int3-based patching code used in ftrace. See the commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching).

When trying to use text_poke_bp in ftrace, the result was slower than the original implementation.

It turned out that one difference was in text_poke vs. ftrace_write. text_poke did many extra operations to make sure that the change was atomic.

Quoted 5 lines
Quoted 2 lines

AFAIK, the main reason why text_poke is used is avoiding RODATA protection (by alias mapping).

That is correct, and the reason ftrace didn't do that is because it would be quite expensive to map 22,000 addresses for each change.

I see. I am going to prepare v3 with the following changes:

+ use text_poke in text_poke_bp back again because it is not effective to make the pages rw only for a single address; the alias mapping is faster here

+ use the faster text_poke_part only in text_poke_bp_iter; the caller of this function will be responsible for making the code rw

+ I will rework the error handling as suggested in the other mails; I will try to add some also for text_poke.

Thanks a lot for hints.

Best Regards,
Petr

maintainer
#6 lore raw

Re: [PATCH v2 1/8] x86: speed up int3-based patching using less paranoid write

In reply to Masami Hiramatsu
Quoted 7 lines · On Fri, 08 Nov 2013 21:04:26 +0900 Masami Hiramatsu…
Quoted 7 lines

static void do_sync_core(void *info)
{

arch/x86/kernel/alternative.c +0 -0
 	sync_core();
@@ -648,15 +687,15 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	 */
 	smp_wmb();
 
Quoted 6 lines

arch/x86/kernel/alternative.c void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) · line 690

Quoted 2 lines
arch/x86/kernel/alternative.c +1 -1
-	text_poke(addr, &int3, sizeof(int3));
+	text_poke_part(addr, &int3, sizeof(int3));

Anyway, text_poke itself will cause a BUG if it hits an error, but text_poke_part() seems silently failing. It should handle the error correctly (or call BUG()).

text_poke_part() shouldn't do a bug on error, but this code probably should.

	if (text_poke_part(addr, &int3, sizeof(int3)))
		BUG();

Note, I would avoid doing:

	BUG_ON(text_poke_part(...));

as we don't want "side effects" in the BUG() macros. You may do:

	ret = text_poke_part(...);
	BUG_ON(ret);

Which has the added benefit of the implicit "unlikely" in the BUG_ON() macro.

Or we can have do_sync_core() be able to handle errors and have their callers handle them too.

-- Steve

#7 lore raw

PATCH v2 2/8 x86: return error code in text_poke_bp

In reply to Petr Mladek

We would like to use text_poke_bp in the dynamic ftrace which want to know about errors. For example, it informs about them in the ftrace log.

Let's return the error code instead of the address. The address was just copied from the first parameter, so it was no extra information. The return value has not been used anywhere yet.

There is a question whether we should recover the original opcode when the second or third text_poke_part fails in text_poke_bp. Well, the errors were ignored until now. It did not cause any real life problems. There is really small chance that the first byte (int3) can be written and the other parts of the code can not be modified. It is probably not worth the extra complexity.

---
 arch/x86/include/asm/alternative.h |  3 ++-
 arch/x86/kernel/alternative.c      | 18 +++++++++++++-----
 2 files changed, 15 insertions(+), 6 deletions(-)
arch/x86/include/asm/alternative.h +2 -1
index 0a3f9c9..f2343d8 100644
@@ -226,6 +226,7 @@ extern void *text_poke_early(void *addr, const void *opcode, size_t len);
  */
 extern void *text_poke(void *addr, const void *opcode, size_t len);
 extern int poke_int3_handler(struct pt_regs *regs);
-extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
+extern int text_poke_bp(void *addr, const void *opcode, size_t len,
+			void *handler);
 
 #endif /* _ASM_X86_ALTERNATIVE_H */
arch/x86/kernel/alternative.c +13 -5
index 0586dc1..c459e62 100644
@@ -673,9 +673,10 @@ int poke_int3_handler(struct pt_regs *regs)
  *
  * Note: must be called under text_mutex.
  */
-void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
+int text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 {
 	unsigned char int3 = 0xcc;
+	int ret;
 
 	bp_int3_handler = handler;
 	bp_int3_addr = (u8 *)addr + sizeof(int3);
@@ -687,15 +688,19 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	 */
 	smp_wmb();
 
-	text_poke_part(addr, &int3, sizeof(int3));
+	ret = text_poke_part(addr, &int3, sizeof(int3));
+	if (unlikely(ret))
+		goto fail;
 
 	on_each_cpu(do_sync_core, NULL, 1);
 
 	if (len - sizeof(int3) > 0) {
 		/* patch all but the first byte */
-		text_poke_part((char *)addr + sizeof(int3),
+		ret = text_poke_part((char *)addr + sizeof(int3),
 			       (const char *) opcode + sizeof(int3),
 			       len - sizeof(int3));
+		if (unlikely(ret))
+			goto fail;
 		/*
 		 * According to Intel, this core syncing is very likely
 		 * not necessary and we'd be safe even without it. But
@@ -705,13 +710,16 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	}
 
 	/* patch the first byte */
-	text_poke_part(addr, opcode, sizeof(int3));
+	ret = text_poke_part(addr, opcode, sizeof(int3));
+	if (unlikely(ret))
+		goto fail;
 
 	on_each_cpu(do_sync_core, NULL, 1);
 
+fail:
 	bp_patching_in_progress = false;
 	smp_wmb();
 
-	return addr;
+	return ret;
 }
 
Signature · 1 line
1.8.4
#8 lore raw

Re: [PATCH v2 2/8] x86: return error code in text_poke_bp

Quoted 6 lines · Petr Mladek

We would like to use text_poke_bp in the dynamic ftrace which want to know about errors. For example, it informs about them in the ftrace log.

Let's return the error code instead of the address. The address was just copied from the first parameter, so it was no extra information. The return value has not been used anywhere yet.

Ah, OK. This change is what I'd like to see. :)

Quoted 6 lines

There is a question whether we should recover the original opcode when the second or third text_poke_part fails in text_poke_bp. Well, the errors were ignored until now. It did not cause any real life problems. There is really small chance that the first byte (int3) can be written and the other parts of the code can not be modified. It is probably not worth the extra complexity.

Since all the text_poke user must hold text_mutex, that kind of racing must not happen. I guess, if you hit that case, you'd better call BUG_ON() or you may get a GPF...

Thank you,

Quoted 78 lines
---
 arch/x86/include/asm/alternative.h |  3 ++-
 arch/x86/kernel/alternative.c      | 18 +++++++++++++-----
 2 files changed, 15 insertions(+), 6 deletions(-)
arch/x86/include/asm/alternative.h +2 -1
index 0a3f9c9..f2343d8 100644
@@ -226,6 +226,7 @@ extern void *text_poke_early(void *addr, const void *opcode, size_t len);
  */
 extern void *text_poke(void *addr, const void *opcode, size_t len);
 extern int poke_int3_handler(struct pt_regs *regs);
-extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
+extern int text_poke_bp(void *addr, const void *opcode, size_t len,
+			void *handler);
 
 #endif /* _ASM_X86_ALTERNATIVE_H */
arch/x86/kernel/alternative.c +13 -5
index 0586dc1..c459e62 100644
@@ -673,9 +673,10 @@ int poke_int3_handler(struct pt_regs *regs)
  *
  * Note: must be called under text_mutex.
  */
-void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
+int text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 {
 	unsigned char int3 = 0xcc;
+	int ret;
 
 	bp_int3_handler = handler;
 	bp_int3_addr = (u8 *)addr + sizeof(int3);
@@ -687,15 +688,19 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	 */
 	smp_wmb();
 
-	text_poke_part(addr, &int3, sizeof(int3));
+	ret = text_poke_part(addr, &int3, sizeof(int3));
+	if (unlikely(ret))
+		goto fail;
 
 	on_each_cpu(do_sync_core, NULL, 1);
 
 	if (len - sizeof(int3) > 0) {
 		/* patch all but the first byte */
-		text_poke_part((char *)addr + sizeof(int3),
+		ret = text_poke_part((char *)addr + sizeof(int3),
 			       (const char *) opcode + sizeof(int3),
 			       len - sizeof(int3));
+		if (unlikely(ret))
+			goto fail;
 		/*
 		 * According to Intel, this core syncing is very likely
 		 * not necessary and we'd be safe even without it. But
@@ -705,13 +710,16 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	}
 
 	/* patch the first byte */
-	text_poke_part(addr, opcode, sizeof(int3));
+	ret = text_poke_part(addr, opcode, sizeof(int3));
+	if (unlikely(ret))
+		goto fail;
 
 	on_each_cpu(do_sync_core, NULL, 1);
 
+fail:
 	bp_patching_in_progress = false;
 	smp_wmb();
 
-	return addr;
+	return ret;
 }
 
Signature · 4 lines
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
maintainer
#9 lore raw

Re: [PATCH v2 2/8] x86: return error code in text_poke_bp

In reply to Petr Mladek
Quoted 7 lines · On Fri, 8 Nov 2013 10:12:06 +0100 Petr Mladek <[email protected]> wrote:

We would like to use text_poke_bp in the dynamic ftrace which want to know about errors. For example, it informs about them in the ftrace log.

Let's return the error code instead of the address. The address was just copied from the first parameter, so it was no extra information. The return value has not been used anywhere yet.

Quoted 6 lines

There is a question whether we should recover the original opcode when the second or third text_poke_part fails in text_poke_bp. Well, the errors were ignored until now. It did not cause any real life problems. There is really small chance that the first byte (int3) can be written and the other parts of the code can not be modified. It is probably not worth the extra complexity.

I agree with Masami. If the first part succeeds, still check the other parts, and if they don't succeed, then just do a BUG().

Quoted 37 lines
---
 arch/x86/include/asm/alternative.h |  3 ++-
 arch/x86/kernel/alternative.c      | 18 +++++++++++++-----
 2 files changed, 15 insertions(+), 6 deletions(-)
arch/x86/include/asm/alternative.h +2 -1
index 0a3f9c9..f2343d8 100644
@@ -226,6 +226,7 @@ extern void *text_poke_early(void *addr, const void *opcode, size_t len);
  */
 extern void *text_poke(void *addr, const void *opcode, size_t len);
 extern int poke_int3_handler(struct pt_regs *regs);
-extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
+extern int text_poke_bp(void *addr, const void *opcode, size_t len,
+			void *handler);
 
 #endif /* _ASM_X86_ALTERNATIVE_H */
arch/x86/kernel/alternative.c +2 -1
index 0586dc1..c459e62 100644
@@ -673,9 +673,10 @@ int poke_int3_handler(struct pt_regs *regs)
  *
  * Note: must be called under text_mutex.
  */
-void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
+int text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 {
 	unsigned char int3 = 0xcc;
+	int ret;
 
 	bp_int3_handler = handler;
 	bp_int3_addr = (u8 *)addr + sizeof(int3);

arch/x86/kernel/alternative.c void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) · line 693

Quoted 7 lines
arch/x86/kernel/alternative.c +3 -1
@@ -687,15 +688,19 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	 */
 	smp_wmb();
 
-	text_poke_part(addr, &int3, sizeof(int3));
+	ret = text_poke_part(addr, &int3, sizeof(int3));
+	if (unlikely(ret))
+		goto fail;

Ah! you have it return an error code. May need to do that for all users of text_poke_part().

-- Steve

Quoted 33 lines
arch/x86/kernel/alternative.c +8 -3
 	on_each_cpu(do_sync_core, NULL, 1);
 
 	if (len - sizeof(int3) > 0) {
 		/* patch all but the first byte */
-		text_poke_part((char *)addr + sizeof(int3),
+		ret = text_poke_part((char *)addr + sizeof(int3),
 			       (const char *) opcode + sizeof(int3),
 			       len - sizeof(int3));
+		if (unlikely(ret))
+			goto fail;
 		/*
 		 * According to Intel, this core syncing is very likely
 		 * not necessary and we'd be safe even without it. But
@@ -705,13 +710,16 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	}
 
 	/* patch the first byte */
-	text_poke_part(addr, opcode, sizeof(int3));
+	ret = text_poke_part(addr, opcode, sizeof(int3));
+	if (unlikely(ret))
+		goto fail;
 
 	on_each_cpu(do_sync_core, NULL, 1);
 
+fail:
 	bp_patching_in_progress = false;
 	smp_wmb();
 
-	return addr;
+	return ret;
 }
 
#10 lore raw

PATCH v2 3/8 x86: allow to call text_poke_bp during boot

In reply to Petr Mladek

We would like to use text_poke_bp in ftrace. It might be called also during boot when there is only one CPU and we do not need to sync the others.

The check is must to have because there are also disabled interrupts during the boot. Then the call would cause a deadlock, see the warning in "smp_call_function_many", kernel/smp.c:371.

Note that the check might need an update when we add support for zero CPUS or complex numbers, e.g 5i-3 CPUs.

The change is inspired by the code in arch/x86/kernel/ftrace.c.

---
 arch/x86/kernel/alternative.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
arch/x86/kernel/alternative.c +14 -3
index c459e62..c28c108 100644
@@ -630,6 +630,17 @@ static void do_sync_core(void *info)
 	sync_core();
 }
 
+static void run_sync(void)
+{
+	/*
+	 * We do not need to sync other cores during boot when there is only one
+	 * CPU enabled. In fact, we must not because there are also disabled
+	 * interrupts. The call would fail because of a potential deadlock.
+	 */
+	if (num_online_cpus() != 1)
+		on_each_cpu(do_sync_core, NULL, 1);
+}
+
 static bool bp_patching_in_progress;
 static void *bp_int3_handler, *bp_int3_addr;
 
@@ -692,7 +703,7 @@ int text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	if (unlikely(ret))
 		goto fail;
 
-	on_each_cpu(do_sync_core, NULL, 1);
+	run_sync();
 
 	if (len - sizeof(int3) > 0) {
 		/* patch all but the first byte */
@@ -706,7 +717,7 @@ int text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 		 * not necessary and we'd be safe even without it. But
 		 * better safe than sorry (plus there's not only Intel).
 		 */
-		on_each_cpu(do_sync_core, NULL, 1);
+		run_sync();
 	}
 
 	/* patch the first byte */
@@ -714,7 +725,7 @@ int text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 	if (unlikely(ret))
 		goto fail;
 
-	on_each_cpu(do_sync_core, NULL, 1);
+	run_sync();
 
 fail:
 	bp_patching_in_progress = false;
Signature · 1 line
1.8.4
#11 lore raw

PATCH v2 4/8 x86: add generic function to modify more calls using int3 framework

In reply to Petr Mladek

The commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching) uses the same technique that has been used in ftrace since 08d636b ("ftrace/x86: Have arch x86_64 use breakpoints instead of stop machine")

When trying to use text_poke_bp in dynamic ftrace, the process was significantly slower than the original code. For example, I tried to switch between 7 tracers:
blk, branch, function_graph, wakeup_rt, irqsoff, function, and nop. Every tracer has also been enabled and disabled. With 500 cycles, I got these times with the original code:

    real    16m14.390s    16m15.200s    16m19.632s
    user    0m0.028s      0m0.024s	0m0.028s
    sys     0m23.788s     0m23.812s	0m23.804s

and with text_poke_bp:

    real    29m45.785s    29m47.504s    29m44.016
    user    0m0.004s      0m0.004s	0m0.004s
    sys     0m15.776s     0m16.088s     0m16.192s

It turned out that most of the extra time was spent in the call:

on_each_cpu(do_sync_core, NULL, 1)

It is needed to reset the speculative queue of decoded instructions on each CPU. It is relatively expensive operation. The original code reduced the usage by patching all instructions in parallel.

This commits adds text_poke_bp_iter that provides generic interface for patching more instructions in parallel. It is basically a mix of text_poke_bp and ftrace_replace_code. The list of addresses and opcodes is passed via an iterator and callbacks.

We need to iterate several times but we do not need both opcodes and the address in all stages. They are passed via callback to avoid an unnecessary computation.

The function checks the current code before patching. It might be possible to leave this check in the ftrace code but such paranoia is useful in general. It helps to keep the system consistent. And the old opcode is needed anyway. When something goes wrong, it is used to get back to a valid state. Note that an error might happen even when adding interrupt to a particular address.

The optional "finish" callback can be used to update state of the patched entry. For example, it is needed to update the related ftrace record.

Some opcodes might already be in the final state and do not need patching. It would be possible to detect this by comparing the old and new opcode but there might be more a effective way. For example, ftrace could tell this by ftrace_test_record. We pass this information via the get_opcode callbacks. They return a valid opcode when a change is needed and NULL otherwise. Note that we should call the "finish" callback even then the code is not really modified.

Finally, ftrace wants to know more information about a potential failure. The error code is passed via the return value. The failing address and the number of the failed record is passed via the iterator structure.

---
 arch/x86/include/asm/alternative.h |  36 ++++++
 arch/x86/kernel/alternative.c      | 251 +++++++++++++++++++++++++++++++++++--
 2 files changed, 275 insertions(+), 12 deletions(-)
arch/x86/include/asm/alternative.h +36 -0
index f2343d8..60c0520 100644
@@ -229,4 +229,40 @@ extern int poke_int3_handler(struct pt_regs *regs);
 extern int text_poke_bp(void *addr, const void *opcode, size_t len,
 			void *handler);
 
+struct text_poke_bp_iter {
+	/* information used to start iteration from the beginning */
+	void *init;
+	/* length of the patched instruction */
+	size_t len;
+	/* details about failure if any */
+	int fail_count;
+	void *fail_addr;
+	/* iteration over entries */
+	void *(*start)(void *init);
+	void *(*next)(void *prev);
+	/* callback to get patched address */
+	void *(*get_addr)(void *cur);
+	/*
+	 * Callbacks to get the patched code. They could return NULL if no
+	 * patching is needed; This is useful for example in ftrace.
+	 */
+	const void *(*get_opcode)(void *cur);
+	const void *(*get_old_opcode)(void *cur);
+	/*
+	 * Optional function that is called when the patching of the given
+	 * has finished. It might be NULL if no postprocess is needed.
+	 */
+	int (*finish)(void *cur);
+	/*
+	 * Helper function for int3 handler. It decides whether the given IP
+	 * is being patched or not.
+	 *
+	 * Try to implement it as fast as possible. It affects performance
+	 * of the system when the patching is in progress.
+	 */
+	void *(*is_handled)(const unsigned long ip);
+};
+
+extern int text_poke_bp_list(struct text_poke_bp_iter *iter);
+
 #endif /* _ASM_X86_ALTERNATIVE_H */
arch/x86/kernel/alternative.c +239 -12

299 lines not on this page · raw message

Signature · 1 line
1.8.4
#12 lore raw

PATCH v2 5/8 x86: do not trace __probe_kernel_read

In reply to Petr Mladek

probe_kernel_read is used when modifying function calls in ftrace_replace_code, see arch/x86/kernel/ftrace.c. On x86, the code is replaced using int3 guard. All functions are patched in parallel to reduce an expensive synchronization of all CPUs. The result is that all affected functions are called via ftrace_int3_handler during the process.

ftrace_int3_handler is relatively slow because it has to check whether it is responsible for the handled IP. Therefore we should not modify functions that used during patching. It would slowdown the patching.

I tried to switch between 7 tracers: blk, branch, function_graph, wakeup_rt, irqsoff, function, and nop. Every tracer has also been enabled and disabled. With 500 cycles, I got these times before this commit:

    real    18m2.477s     18m8.654s     18m9.196s
    user    0m0.008s      0m0.008s      0m0.012s
    sys     0m17.316s     0m17.104s     0m17.300s

and after this commit:

    real    16m14.390s    16m15.200s    16m19.632s
    user    0m0.028s      0m0.024s	0m0.028s
    sys     0m23.788s     0m23.812s	0m23.804s
---
 mm/maccess.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/maccess.c +1 -1
index d53adf9..bed9ee8 100644
@@ -18,7 +18,7 @@
 long __weak probe_kernel_read(void *dst, const void *src, size_t size)
     __attribute__((alias("__probe_kernel_read")));
 
-long __probe_kernel_read(void *dst, const void *src, size_t size)
+long notrace __probe_kernel_read(void *dst, const void *src, size_t size)
 {
 	long ret;
 	mm_segment_t old_fs = get_fs();
Signature · 1 line
1.8.4
#13 lore raw

PATCH v2 6/8 x86: modify ftrace function using the new int3-based framework

In reply to Petr Mladek

The commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching) uses the same technique that has been used in ftrace since 08d636b ("ftrace/x86: Have arch x86_64 use breakpoints instead of stop machine")

It would be great to use the new generic implementation and clean up the x86 ftrace code a bit.

Let's start with ftrace_modify_code. It does basically the same as text_poke_bp. In addition, it checks whether the replaced code is the expected one to avoid problems with modules. The checking code can be split from ftrace_modify_code_direct.

Note that we do not longer need to set modifying_ftrace_code in ftrace_update_ftrace_func. The int3 interrupt will be handled by poke_int3_handler.

---
 arch/x86/kernel/ftrace.c | 106 ++++++++++++++++++++---------------------------
 1 file changed, 46 insertions(+), 60 deletions(-)
arch/x86/kernel/ftrace.c +46 -60
index 42a392a..5ade40e 100644
@@ -52,6 +52,33 @@ union ftrace_code_union {
 	} __attribute__((packed));
 };
 
+/*
+ * Note: Due to modules and __init, code can
+ *  disappear and change, we need to protect against faulting
+ *  as well as code changing. We do this by using the
+ *  probe_kernel_* functions.
+ */
+static int
+ftrace_check_code(unsigned long ip, unsigned const char *expected)
+{
+	unsigned char actual[MCOUNT_INSN_SIZE];
+
+	if (probe_kernel_read(actual, (void *)ip, MCOUNT_INSN_SIZE))
+		return -EFAULT;
+
+	if (memcmp(actual, expected, MCOUNT_INSN_SIZE) != 0) {
+		WARN_ONCE(1,
+			  "ftrace check failed: %x %x%x%x%x vs. %x %x%x%x%x\n",
+			  actual[0],
+			  actual[1], actual[2], actual[3], actual[4],
+			  expected[0],
+			  expected[1], expected[2], expected[3], expected[4]);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static int ftrace_calc_offset(long ip, long addr)
 {
 	return (int)(addr - ip);
@@ -103,28 +130,12 @@ static int
 ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code,
 		   unsigned const char *new_code)
 {
-	unsigned char replaced[MCOUNT_INSN_SIZE];
-
-	/*
-	 * Note: Due to modules and __init, code can
-	 *  disappear and change, we need to protect against faulting
-	 *  as well as code changing. We do this by using the
-	 *  probe_kernel_* functions.
-	 *
-	 * No real locking needed, this code is run through
-	 * kstop_machine, or before SMP starts.
-	 */
-
-	/* read the text we want to modify */
-	if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
-		return -EFAULT;
+	int ret;
 
-	/* Make sure it is what we expect it to be */
-	if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
-		return -EINVAL;
+	ret = ftrace_check_code(ip, old_code);
 
 	/* replace the text with the new text */
-	if (do_ftrace_mod_code(ip, new_code))
+	if (!ret && do_ftrace_mod_code(ip, new_code))
 		return -EPERM;
 
 	sync_core();
@@ -132,6 +143,22 @@ ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code,
 	return 0;
 }
 
+static int
+ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
+		   unsigned const char *new_code)
+{
+	int ret;
+
+	ret = ftrace_check_code(ip, old_code);
+
+	/* replace the text with the new text */
+	if (!ret)
+		ret = text_poke_bp((void *)ip, new_code, MCOUNT_INSN_SIZE,
+				   (void *)ip + MCOUNT_INSN_SIZE);
+
+	return ret;
+}
+
 int ftrace_make_nop(struct module *mod,
 		    struct dyn_ftrace *rec, unsigned long addr)
 {
@@ -202,10 +229,6 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  */
 atomic_t modifying_ftrace_code __read_mostly;
 
-static int
-ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
-		   unsigned const char *new_code);
-
 /*
  * Should never be called:
  *  As it is only called by __ftrace_replace_code() which is called by
@@ -230,9 +253,6 @@ int ftrace_update_ftrace_func(ftrace_func_t func)
 	memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
 	new = ftrace_call_replace(ip, (unsigned long)func);
 
-	/* See comment above by declaration of modifying_ftrace_code */
-	atomic_inc(&modifying_ftrace_code);
-
 	ret = ftrace_modify_code(ip, old, new);
 
 	/* Also update the regs callback function */
@@ -243,8 +263,6 @@ int ftrace_update_ftrace_func(ftrace_func_t func)
 		ret = ftrace_modify_code(ip, old, new);
 	}
 
-	atomic_dec(&modifying_ftrace_code);
-
 	return ret;
 }
 
@@ -609,38 +627,6 @@ void ftrace_replace_code(int enable)
 	}
 }
 
-static int
-ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
-		   unsigned const char *new_code)
-{
-	int ret;
-
-	ret = add_break(ip, old_code);
-	if (ret)
-		goto out;
-
-	run_sync();
-
-	ret = add_update_code(ip, new_code);
-	if (ret)
-		goto fail_update;
-
-	run_sync();
-
-	ret = ftrace_write(ip, new_code, 1);
-	if (ret) {
-		ret = -EPERM;
-		goto out;
-	}
-	run_sync();
- out:
-	return ret;
-
- fail_update:
-	probe_kernel_write((void *)ip, &old_code[0], 1);
-	goto out;
-}
-
 void arch_ftrace_update_code(int command)
 {
 	/* See comment above by declaration of modifying_ftrace_code */
Signature · 1 line
1.8.4
#14 lore raw

PATCH v2 7/8 x86: patch all traced function calls using the int3-based framework

In reply to Petr Mladek

Let's continue with replacing the existing Int3-based framework in ftrace with the new generic function introduced by the commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching)

This time use it in ftrace_replace_code that modifies all the watched function calls.

If we use text_poke_bp in ftrace_make_nop, ftrace_make_call, ftrace_modify_call, it would be possible to get rid of the x86-specific ftrace_replace_code implementation and use the generic code.

This would be really lovely change. Unfortunately, the code would be slow because syncing on each CPU is relatively expensive operation. For example, I tried to switch between 7 tracers: blk, branch, function_graph, wakeup_rt, irqsoff, function, and nop. Every tracer has also been enabled and disabled. With 500 cycles, I got these times with the original code:

    real    16m14.390s    16m15.200s    16m19.632s
    user    0m0.028s      0m0.024s      0m0.028s
    sys     0m23.788s     0m23.812s     0m23.804s

It slowed down after using text_poke_bp for each record separately:

    real    29m45.785s    29m47.504s    29m44.016
    user    0m0.004s      0m0.004s      0m0.004s
    sys     0m15.776s     0m16.088s     0m16.192s

Hence, we implemented the more complex text_poke_bp_iter. When we use it, we get the times:

   real    17m9.753s     17m12.272s    17m11.424s
   user    0m0.004s      0m0.004s      0m0.004s
   sys     0m18.244s     0m18.252s     0m18.308s

It is slightly slower than the ftrace-specific code. Well, this is the cost for using a generic API that can be used also in other locations.

---

---
 arch/x86/kernel/alternative.c |   4 +-
 arch/x86/kernel/ftrace.c      | 428 +++++++-----------------------------------
 arch/x86/kernel/traps.c       |  10 -
 include/linux/ftrace.h        |   6 -
 4 files changed, 75 insertions(+), 373 deletions(-)
arch/x86/kernel/alternative.c +2 -2
index 95e8a7f..c7c7ad8 100644
@@ -603,7 +603,7 @@ void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
  *
  * Note: Must be called under text_mutex.
  */
-static int text_poke_part(void *addr, const void *opcode, size_t len)
+static int notrace text_poke_part(void *addr, const void *opcode, size_t len)
 {
 	/*
 	 * On x86_64, kernel text mappings are mapped read-only with
@@ -647,7 +647,7 @@ static void *bp_int3_handler, *bp_int3_addr;
 static size_t bp_int3_len;
 static void *(*bp_int3_is_handled)(const unsigned long ip);
 
-int poke_int3_handler(struct pt_regs *regs)
+int notrace poke_int3_handler(struct pt_regs *regs)
 {
 	/* bp_patching_in_progress */
 	smp_rmb();
arch/x86/kernel/ftrace.c +73 -355

515 lines not on this page · raw message

arch/x86/kernel/traps.c +0 -10
index 729aa77..16bf450 100644
@@ -50,7 +50,6 @@
 #include <asm/processor.h>
 #include <asm/debugreg.h>
 #include <linux/atomic.h>
-#include <asm/ftrace.h>
 #include <asm/traps.h>
 #include <asm/desc.h>
 #include <asm/i387.h>
@@ -319,15 +318,6 @@ dotraplinkage void __kprobes notrace do_int3(struct pt_regs *regs, long error_co
 {
 	enum ctx_state prev_state;
 
-#ifdef CONFIG_DYNAMIC_FTRACE
-	/*
-	 * ftrace must be first, everything else may cause a recursive crash.
-	 * See note by declaration of modifying_ftrace_code in ftrace.c
-	 */
-	if (unlikely(atomic_read(&modifying_ftrace_code)) &&
-	    ftrace_int3_handler(regs))
-		return;
-#endif
 	if (poke_int3_handler(regs))
 		return;
 
include/linux/ftrace.h +0 -6
index 9f15c00..40ec039 100644
@@ -383,12 +383,6 @@ struct ftrace_rec_iter *ftrace_rec_iter_start(void);
 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
 
-#define for_ftrace_rec_iter(iter)		\
-	for (iter = ftrace_rec_iter_start();	\
-	     iter;				\
-	     iter = ftrace_rec_iter_next(iter))
-
-
 int ftrace_update_record(struct dyn_ftrace *rec, int enable);
 int ftrace_test_record(struct dyn_ftrace *rec, int enable);
 void ftrace_run_stop_machine(int command);
Signature · 1 line
1.8.4
#15 lore raw

PATCH v2 8/8 x86: enable/disable ftrace graph call using new int3-based framework

In reply to Petr Mladek

One more change related to replacing the existing Int3-based framework with the new generic function introduced by the commit fd4363fff3d9 (x86: Introduce int3 (breakpoint)-based instruction patching)

ftrace_enable_ftrace_graph_caller and ftrace_disable_ftrace_graph_caller modified the jump target directly without using the int3 guard. It worked because writing the address was an atomic operation.

We do not really need to use the safe ftrace_modify_code here but it helps to remove another arch-specific code. The result is more consistent and better readable code which is might be worth the change.

---
 arch/x86/kernel/ftrace.c | 63 +++++++++++-------------------------------------
 1 file changed, 14 insertions(+), 49 deletions(-)
arch/x86/kernel/ftrace.c +14 -49
index 92fe8ca..20d2028 100644
@@ -47,7 +47,7 @@ int ftrace_arch_code_modify_post_process(void)
 union ftrace_code_union {
 	char code[MCOUNT_INSN_SIZE];
 	struct {
-		char e8;
+		char inst;
 		int offset;
 	} __attribute__((packed));
 };
@@ -88,7 +88,7 @@ static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
 {
 	static union ftrace_code_union calc;
 
-	calc.e8		= 0xe8;
+	calc.inst	= 0xe8;
 	calc.offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
 
 	/*
@@ -98,27 +98,11 @@ static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
 	return calc.code;
 }
 
-static inline int
-within(unsigned long addr, unsigned long start, unsigned long end)
+static void ftrace_jump_replace(union ftrace_code_union *calc,
+				unsigned long ip, unsigned long addr)
 {
-	return addr >= start && addr < end;
-}
-
-static int
-do_ftrace_mod_code(unsigned long ip, const void *new_code)
-{
-	/*
-	 * On x86_64, kernel text mappings are mapped read-only with
-	 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
-	 * of the kernel text mapping to modify the kernel text.
-	 *
-	 * For 32bit kernels, these mappings are same and we can use
-	 * kernel identity mapping to modify code.
-	 */
-	if (within(ip, (unsigned long)_text, (unsigned long)_etext))
-		ip = (unsigned long)__va(__pa_symbol(ip));
-
-	return probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE);
+	calc->inst	= 0xe9;
+	calc->offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
 }
 
 static const unsigned char *ftrace_nop_replace(void)
@@ -369,45 +353,26 @@ int __init ftrace_dyn_arch_init(void *data)
 #ifdef CONFIG_DYNAMIC_FTRACE
 extern void ftrace_graph_call(void);
 
-static int ftrace_mod_jmp(unsigned long ip,
-			  int old_offset, int new_offset)
-{
-	unsigned char code[MCOUNT_INSN_SIZE];
-
-	if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
-		return -EFAULT;
-
-	if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
-		return -EINVAL;
-
-	*(int *)(&code[1]) = new_offset;
-
-	if (do_ftrace_mod_code(ip, &code))
-		return -EPERM;
-
-	return 0;
-}
-
 int ftrace_enable_ftrace_graph_caller(void)
 {
 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
-	int old_offset, new_offset;
+	union ftrace_code_union old, new;
 
-	old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
-	new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
+	ftrace_jump_replace(&old, ip, (unsigned long)(&ftrace_stub));
+	ftrace_jump_replace(&new, ip, (unsigned long)(&ftrace_graph_caller));
 
-	return ftrace_mod_jmp(ip, old_offset, new_offset);
+	return ftrace_modify_code(ip, old.code, new.code);
 }
 
 int ftrace_disable_ftrace_graph_caller(void)
 {
 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
-	int old_offset, new_offset;
+	union ftrace_code_union old, new;
 
-	old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
-	new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
+	ftrace_jump_replace(&old, ip, (unsigned long)(&ftrace_graph_caller));
+	ftrace_jump_replace(&new, ip, (unsigned long)(&ftrace_stub));
 
-	return ftrace_mod_jmp(ip, old_offset, new_offset);
+	return ftrace_modify_code(ip, old.code, new.code);
 }
 
 #endif /* !CONFIG_DYNAMIC_FTRACE */
Signature · 1 line
1.8.4