[PATCH] D116070: [X86] Enable ibt-seal optimization when LTO is used in Kernel

Joao Moreira via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 20 18:02:02 PST 2021


joaomoreira created this revision.
joaomoreira added reviewers: craig.topper, pengfei, xiangzhangllvm, oren_ben_simhon, hjl.tools, gftg.
Herald added subscribers: dexonsmith, dang, hiraditya, inglorion.
joaomoreira requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Intel's CET/IBT requires every indirect branch target to be an ENDBR instruction. Because of that, the compiler needs to correctly emit these instruction on function's prologues. Because this is a security feature, it is desirable that only actual indirect-branch-targeted functions are emitted with ENDBRs. While it is possible to identify address-taken functions through LTO, minimizing these ENDBR instructions remains a hard task for user-space binaries because exported functions may end being reachable through PLT entries, that will use an indirect branch for such. Because this cannot be determined during compilation-time, the compiler currently emits ENDBRs to every non-local-linkage function.

Despite the challenge presented for user-space, the kernel landscape is different as no PLTs are used. With the intent of providing the most fit ENDBR emission for the kernel, kernel developers proposed an optimization named "ibt-seal" which replaces the ENDBRs for NOPs directly in the binary. The discussion of this feature can be seen in [1].

This diff brings the enablement of the flag -mibt-seal, which in combination with LTO enforces a different policy for ENDBR placement in when the code-model is set to "kernel". In this scenario, the compiler will only emit ENDBRs to address taken functions, ignoring non-address taken functions that are don't have local linkage.

A comparison between an LTO-compiled kernel binaries without and with the -mibt-seal feature enabled shows that when -mibt-seal was used, the number of ENDBRs in the vmlinux.o binary patched by objtool decreased from 44383 to 33192, and that the number of superfluous ENDBR instructions nopped-out decreased from 11730 to 540.

The 540 missed superfluous ENDBRs need to be investigated further, but hypotheses are: assembly code not being taken care of by the compiler, kernel exported symbols mechanisms creating bogus address taken situations or even these being removed due to other binary optimizations like kernel's static_calls. For now, I assume that the large drop in the number of ENDBR instructions already justifies the feature being merged.

[1] - https://lkml.org/lkml/2021/11/22/591


https://reviews.llvm.org/D116070

Files:
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  llvm/lib/Target/X86/X86IndirectBranchTracking.cpp
  llvm/test/CodeGen/X86/ibtseal-kernel.ll
  llvm/test/CodeGen/X86/ibtseal-large.ll
  llvm/test/CodeGen/X86/ibtseal-small.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116070.395531.patch
Type: text/x-patch
Size: 8012 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211221/c950e514/attachment.bin>


More information about the llvm-commits mailing list