llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang][PPC] support for -mprofile-kernel

Open nickdesaulniers opened this issue 4 years ago • 14 comments

From an LKML thread, it seems like the Linux kernel makes use of -mprofile-kernel. https://lore.kernel.org/llvm/[email protected]/ cc @nemanjai @nemanja-ibm

nickdesaulniers avatar Aug 09 '22 17:08 nickdesaulniers

@llvm/issue-subscribers-backend-powerpc

llvmbot avatar Aug 09 '22 17:08 llvmbot

This script is what's used to feature test for this flag.

nickdesaulniers avatar Aug 09 '22 17:08 nickdesaulniers

So this is specific to PowerPC? I suppose only the kernel build on PowerPC uses it so that is why this is specific to PowerPC but Clang doesn't support it on any architecture. Do you happen to have the documentation from GCC for what this option does? It doesn't seem to come up in my searches in the GCC 11.2.0 manual nor in my searches on Google. If this is an undocumented GCC option, then we can't really implement it in Clang and we need to find a documented alternative for the kernel build.

nemanjai avatar Aug 09 '22 17:08 nemanjai

In the Linux kernel sources, I only see references to -mprofile-kernel in the PPC port.

If this is an undocumented GCC option https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65372

From that bug, the command line help text prints:

Call mcount for profiling before a function prologue

https://kamaleshbabulal.wordpress.com/2016/02/22/linux-kernel-livepatch-mprofile-kernel/ has some more info.

I'm guessing it's easy to find what it's doing in godbolt. Seems like it needs -pg do call _mcount, and is only supported on 64b targets. https://godbolt.org/z/37ooxnh3P

nickdesaulniers avatar Aug 09 '22 18:08 nickdesaulniers

It is unfortunate that -mprofile-kernel is still not documented, but we rely on it heavily for a lot of the ftrace features, including kernel live patching. I doubt the kernel gets much coverage for the older mcount ABI for ppc64le.

From the kernel perspective, -mprofile-kernel largely boils down to the two instruction sequence mflr r0; bl _mcount emitted at function entry, after the optional two-instruction sequence for TOC setup.

Not to replace documentation, but another good reference for this is the writeup from Michael Ellerman: https://mpe.github.io/posts/2016/05/23/kernel-live-patching-for-ppc64le/

rnav avatar Aug 10 '22 08:08 rnav

I am unsure whether we need to add more variants for mcount. GCC's PowerPC port has some support for -fpatchable-function-entry= now: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99888 This is the new way for live patching IIUC.

MaskRay avatar Aug 11 '22 06:08 MaskRay

We now have support for -fpatchable-function-entry in the Linux kernel: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0f71dcfb4aef6043da6cc509e7a7f6a3ae87c12d

rnav avatar Sep 14 '23 08:09 rnav

@rnav does ftrace work with -fpatchable-function-entry with clang for ppc?

arch/powerpc/tools/gcc-check-fpatchable-function-entry.sh looks like it's invoked with $(CC) which I would expect should work.

Has it been tested with clang though?

nickdesaulniers avatar Sep 14 '23 16:09 nickdesaulniers

clang does not support that option on powerpc: clang: error: unsupported option '-fpatchable-function-entry=2' for target 'powerpc64le-unknown-linux-gnu'

Note that gcc needed changes to ensure that the nops are generated after the global entry point for ppc64, as seen in the gcc bug linked above.

rnav avatar Sep 15 '23 05:09 rnav

@maryammo I know that you had done some work to make -fpatchable-function-entry work. Do you know how much work it would be to enable it for ELFv2? @stefanp-ibm You were also involved in evaluating it on ELFv2 IIRC. Perhaps you might be able to help here as well?

nemanjai avatar Sep 21 '23 20:09 nemanjai

I believe that enabling -fpatchable-function-entry=N,M on ELFv2 is quite doable. We would have to adopt the same limitations as GCC (ie. The maximum value of N and M is 65535. On PowerPC with the ELFv2 ABI, for a function with dual entry points, the supported values for M are 0, 2, 6 and 14. )

I do also remember that there were a couple of limitations that required more work. For example, there were issues that we found when we tried to use -fpatchable-function-entry with -ffunction-sections. I don't remember if that issue was when we were looking at both AIX and Linux or just AIX.

stefanp-synopsys avatar Sep 21 '23 21:09 stefanp-synopsys

One issue was any call to the function local entry will bypass the hotpatch block.

maryammo avatar Sep 22 '23 14:09 maryammo

As an update on gcc side of things, there has been some new discussion about how gcc implements -fpatchable-function-entry on powerpc:

  • https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99888#c15
  • https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112980

rnav avatar Jan 29 '24 05:01 rnav

https://github.com/llvm/llvm-project/pull/92997 is created for this issue. I don't have a ppc/ppc64 server to check the end-to-end functionality of the patchable function entry. For now, the code generation is like other platforms, like X86/RISCV on Linux. If any issue found in the compiler code generation, please let me know.

chenzheng1030 avatar May 22 '24 06:05 chenzheng1030