Does FTRACE invalidate the CPU instruction cache after it has modify the code instructions in memory?

119 Views Asked by At

As is well known, the kernel uses "mcount" as a placeholder to redirect CPU instruction execution during FTRACE operation. Eg:

c1003000 <run_init_process>:
c1003000:       55                      push   %ebp
c1003001:       89 e5                   mov    %esp,%ebp
c1003003:       83 ec 04                sub    $0x4,%esp
c1003006:       e8 21 e2 5c 00          call   c15d122c <mcount>
c100300b:       b9 80 4f 83 c1          mov    $0xc1834f80,%ecx
c1003010:       64 8b 15 90 cf 95 c1    mov    %fs:0xc195cf90,%edx
c1003017:       a3 20 50 83 c1          mov    %eax,0xc1835020

From above, the instruction "call mcount" will be dynamically replace with some other instruction during FTRACE operation.

Question is how safe is the instruction replacement in the kernel memory - given that the CPU always preload certain number of instructions into its cache before execution. And it may happen that after loading the instruction, the FTRACE operation replaces the instruction in memory. But the CPU will still be executing the cached version, right? Or does FTRACE trigger a CPU instruction/data cache invalidation immediately after modifying the memory content? (Please provide kernel source code reference?)

Thanks.

PS: Reference: http://people.redhat.com/srostedt/ftrace-tutorial.odp (slide 36 and 37 showed the instructions operation in memory when FTRACE is enabled on the function)

1

There are 1 best solutions below

0
On BEST ANSWER

As briefly mentioned here:

http://lwn.net/Articles/556186/

FTRACE is using "stop_machine" architecture, and in this mode, when the CPU is modifying the memory of the tasks code area, all tasks are far and away from its execution activity, and thus the CPU cache is unlikely to store the code to be executed, thus it is fine to modify the code in memory.