]> git.ipfire.org Git - thirdparty/gcc.git/commit
Add direct support for Linux kernel __fentry__ patching
authorAndi Kleen <ak@linux.intel.com>
Fri, 26 Sep 2014 04:06:40 +0000 (04:06 +0000)
committerAndi Kleen <ak@gcc.gnu.org>
Fri, 26 Sep 2014 04:06:40 +0000 (04:06 +0000)
commitecc81e33123d7ac9c11742161e128858d844b99d
treefd0848cafa95e3618b8829b766aae35e59770c01
parent6eb0ae29ec058a56434c43ebd8c8b3741bf6475c
Add direct support for Linux kernel __fentry__ patching

The Linux kernel dynamically patches in __fentry__ calls in and
out at runtime. This allows using function tracing for debugging
in production kernels without (significant) performance penalty.

For this it needs a table pointing to each __fentry__ call.

The way it is currently implemented is that a special
perl script scans the object file, generates the table in a special
section. When the kernel boots up it nops the calls, and
then later patches in the calls again as needed.

The recordmcount.pl script in the kernel works, but it seems
cleaner and faster to support the code generation of the patch table
directly in gcc.

This also allows to nop the calls directly at code generation
time, which allows to skip a patching step at kernel boot.
I also expect that a patchable production tracing facility is also useful
for other applications.

For example it could be used in ftracer
(https://github.com/andikleen/ftracer)

Having a nop area at the beginning of each function can be also
also useful for other things. For example it can be used to patch
functions at runtime to point to different functions, to do
binary updates without restarting the program (like ksplice or
similar)

This patch implements two new options for the i386 target:

-mrecord-mcount
Generate a __mcount_loc section entry for each __fentry__ or mcount
call. The section is compatible with the kernel convention
and the data is put into a section loaded at runtime.

-mnop-mcount
Generate the mcount/__fentry__ call as 5 byte nop that can be
patched in later. The nop is generated as a single instruction,
as the Linux kernel run time patching relies on this.

Limitations:
- I didn't implement -mnop-mcount for -fPIC. This
would need a good single instruction 6 byte NOP and it seems a
bit pointless, as the patching would prevent text sharing.
- I didn't implement noping for targets that pass a variable
to mcount.
- The facility could be useful on architectures too. Currently
the mcount code is target specific, so I made it a i386 option.

gcc/:

2014-09-25  Andi Kleen  <ak@linux.intel.com>

* config/i386/i386.c (x86_print_call_or_nop): New function.
(x86_function_profiler): Support -mnop-mcount and
-mrecord-mcount.
* config/i386/i386.opt (-mnop-mcount, -mrecord-mcount): Add
* doc/invoke.texi: Document -mnop-mcount, -mrecord-mcount.

gcc/testsuite:

2014-09-25  Andi Kleen  <ak@linux.intel.com>

* gcc.target/i386/nop-mcount.c: New file.
* gcc.target/i386/record-mcount.c: New file.

From-SVN: r215629
gcc/ChangeLog
gcc/config/i386/i386.c
gcc/config/i386/i386.opt
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/i386/nop-mcount.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/record-mcount.c [new file with mode: 0644]