linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ARM: ftrace: Ensure code modifications are synchronised across all cpus
@ 2012-12-06 18:11 Jon Medhurst (Tixy)
  2012-12-06 19:19 ` Steven Rostedt
  0 siblings, 1 reply; 35+ messages in thread
From: Jon Medhurst (Tixy) @ 2012-12-06 18:11 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: Russell King, Steven Rostedt, Ingo Molnar, Frederic Weisbecker,
	Rabin Vincent, linux-kernel

When the generic ftrace implementation modifies code for trace-points it
uses stop_machine() to call ftrace_modify_all_code() on one CPU. This
ultimately calls the ARM specific function ftrace_modify_code() which
updates the instruction and then does flush_icache_range(). As this
cache flushing only operates on the local CPU then other cores may end
up executing the old instruction if it's still in their icaches.

This may or may not cause problems for the use of ftrace on kernels
compiled for ARM instructions. However, Thumb2 instructions can straddle
two cache lines so its possible for half the old instruction to be in
the cache and half the new one, leading to the CPU executing garbage.

This patch fixes this situation by providing an arch-specific
implementation of arch_ftrace_update_code() which ensures that after one
core has modified all the code, the other cores invalidate their icaches
before continuing.

Signed-off-by: Jon Medhurst <tixy@linaro.org>
---
 arch/arm/kernel/ftrace.c |   34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c
index 34e5664..38b670c 100644
--- a/arch/arm/kernel/ftrace.c
+++ b/arch/arm/kernel/ftrace.c
@@ -14,6 +14,7 @@
 
 #include <linux/ftrace.h>
 #include <linux/uaccess.h>
+#include <linux/stop_machine.h>
 
 #include <asm/cacheflush.h>
 #include <asm/opcodes.h>
@@ -156,6 +157,39 @@ int ftrace_make_nop(struct module *mod,
 	return ret;
 }
 
+struct afmc_data {
+	int command;
+	atomic_t cpu;
+	atomic_t done;
+};
+
+static int __arch_ftrace_modify_code(void *data)
+{
+	struct afmc_data *afmcd = data;
+
+	if (atomic_inc_return(&afmcd->cpu) == num_online_cpus()) {
+		/* Last cpu to get into this function does the actual work */
+		ftrace_modify_all_code(afmcd->command);
+		wmb();
+		atomic_set(&afmcd->done, true);
+	} else {
+		/* Other cpus wait for the code modifications to be done */
+		rmb();
+		while (!atomic_read(&afmcd->done))
+			cpu_relax();
+		/* Ensure icache is consistent with the code changes */
+		__flush_icache_all();
+	}
+
+	return 0;
+}
+
+void arch_ftrace_update_code(int command)
+{
+	struct afmc_data afmcd = { command };
+	stop_machine(__arch_ftrace_modify_code, &afmcd, cpu_online_mask);
+}
+
 int __init ftrace_dyn_arch_init(void *data)
 {
 	*(unsigned long *)data = 0;
-- 
1.7.10.4




^ permalink raw reply related	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2012-12-10 16:45 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-06 18:11 [PATCH] ARM: ftrace: Ensure code modifications are synchronised across all cpus Jon Medhurst (Tixy)
2012-12-06 19:19 ` Steven Rostedt
2012-12-07  9:22   ` Jon Medhurst (Tixy)
2012-12-07 14:03     ` Steven Rostedt
2012-12-07 14:55       ` Jon Medhurst (Tixy)
2012-12-07 15:28         ` Steven Rostedt
2012-12-07 15:40           ` Jon Medhurst (Tixy)
2012-12-07 16:09             ` Steven Rostedt
2012-12-07 16:23           ` Russell King - ARM Linux
2012-12-07 16:36             ` Steven Rostedt
2012-12-07 16:45               ` Russell King - ARM Linux
2012-12-07 17:13                 ` Steven Rostedt
2012-12-07 17:45                   ` Jon Medhurst (Tixy)
2012-12-07 18:06                     ` Steven Rostedt
2012-12-07 18:17                       ` Steven Rostedt
2012-12-07 18:18                       ` Jon Medhurst (Tixy)
2012-12-10 10:04                     ` Will Deacon
2012-12-10 13:02                       ` Steven Rostedt
2012-12-10 13:33                         ` Will Deacon
2012-12-10 13:40                         ` Jamie Lokier
2012-12-10 14:56                           ` Will Deacon
2012-12-10 13:57                         ` Russell King - ARM Linux
2012-12-10 14:06                           ` Steven Rostedt
2012-12-10 14:07                             ` Russell King - ARM Linux
2012-12-10 14:46                               ` Steven Rostedt
2012-12-10 15:25                                 ` Russell King - ARM Linux
2012-12-10 16:31                                   ` Steven Rostedt
2012-12-10 16:45                       ` Jon Medhurst (Tixy)
2012-12-07 18:13                   ` Russell King - ARM Linux
2012-12-07 18:43                     ` Steven Rostedt
2012-12-07 19:02                       ` Will Deacon
2012-12-07 20:01                         ` Steven Rostedt
2012-12-10 11:04                         ` Jon Medhurst (Tixy)
2012-12-10 11:24                           ` Will Deacon
2012-12-10 14:02                             ` Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).