orc-unwinder.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ============
  3. ORC unwinder
  4. ============
  5. Overview
  6. ========
  7. The kernel CONFIG_UNWINDER_ORC option enables the ORC unwinder, which is
  8. similar in concept to a DWARF unwinder. The difference is that the
  9. format of the ORC data is much simpler than DWARF, which in turn allows
  10. the ORC unwinder to be much simpler and faster.
  11. The ORC data consists of unwind tables which are generated by objtool.
  12. They contain out-of-band data which is used by the in-kernel ORC
  13. unwinder. Objtool generates the ORC data by first doing compile-time
  14. stack metadata validation (CONFIG_STACK_VALIDATION). After analyzing
  15. all the code paths of a .o file, it determines information about the
  16. stack state at each instruction address in the file and outputs that
  17. information to the .orc_unwind and .orc_unwind_ip sections.
  18. The per-object ORC sections are combined at link time and are sorted and
  19. post-processed at boot time. The unwinder uses the resulting data to
  20. correlate instruction addresses with their stack states at run time.
  21. ORC vs frame pointers
  22. =====================
  23. With frame pointers enabled, GCC adds instrumentation code to every
  24. function in the kernel. The kernel's .text size increases by about
  25. 3.2%, resulting in a broad kernel-wide slowdown. Measurements by Mel
  26. Gorman [1]_ have shown a slowdown of 5-10% for some workloads.
  27. In contrast, the ORC unwinder has no effect on text size or runtime
  28. performance, because the debuginfo is out of band. So if you disable
  29. frame pointers and enable the ORC unwinder, you get a nice performance
  30. improvement across the board, and still have reliable stack traces.
  31. Ingo Molnar says:
  32. "Note that it's not just a performance improvement, but also an
  33. instruction cache locality improvement: 3.2% .text savings almost
  34. directly transform into a similarly sized reduction in cache
  35. footprint. That can transform to even higher speedups for workloads
  36. whose cache locality is borderline."
  37. Another benefit of ORC compared to frame pointers is that it can
  38. reliably unwind across interrupts and exceptions. Frame pointer based
  39. unwinds can sometimes skip the caller of the interrupted function, if it
  40. was a leaf function or if the interrupt hit before the frame pointer was
  41. saved.
  42. The main disadvantage of the ORC unwinder compared to frame pointers is
  43. that it needs more memory to store the ORC unwind tables: roughly 2-4MB
  44. depending on the kernel config.
  45. ORC vs DWARF
  46. ============
  47. ORC debuginfo's advantage over DWARF itself is that it's much simpler.
  48. It gets rid of the complex DWARF CFI state machine and also gets rid of
  49. the tracking of unnecessary registers. This allows the unwinder to be
  50. much simpler, meaning fewer bugs, which is especially important for
  51. mission critical oops code.
  52. The simpler debuginfo format also enables the unwinder to be much faster
  53. than DWARF, which is important for perf and lockdep. In a basic
  54. performance test by Jiri Slaby [2]_, the ORC unwinder was about 20x
  55. faster than an out-of-tree DWARF unwinder. (Note: That measurement was
  56. taken before some performance tweaks were added, which doubled
  57. performance, so the speedup over DWARF may be closer to 40x.)
  58. The ORC data format does have a few downsides compared to DWARF. ORC
  59. unwind tables take up ~50% more RAM (+1.3MB on an x86 defconfig kernel)
  60. than DWARF-based eh_frame tables.
  61. Another potential downside is that, as GCC evolves, it's conceivable
  62. that the ORC data may end up being *too* simple to describe the state of
  63. the stack for certain optimizations. But IMO this is unlikely because
  64. GCC saves the frame pointer for any unusual stack adjustments it does,
  65. so I suspect we'll really only ever need to keep track of the stack
  66. pointer and the frame pointer between call frames. But even if we do
  67. end up having to track all the registers DWARF tracks, at least we will
  68. still be able to control the format, e.g. no complex state machines.
  69. ORC unwind table generation
  70. ===========================
  71. The ORC data is generated by objtool. With the existing compile-time
  72. stack metadata validation feature, objtool already follows all code
  73. paths, and so it already has all the information it needs to be able to
  74. generate ORC data from scratch. So it's an easy step to go from stack
  75. validation to ORC data generation.
  76. It should be possible to instead generate the ORC data with a simple
  77. tool which converts DWARF to ORC data. However, such a solution would
  78. be incomplete due to the kernel's extensive use of asm, inline asm, and
  79. special sections like exception tables.
  80. That could be rectified by manually annotating those special code paths
  81. using GNU assembler .cfi annotations in .S files, and homegrown
  82. annotations for inline asm in .c files. But asm annotations were tried
  83. in the past and were found to be unmaintainable. They were often
  84. incorrect/incomplete and made the code harder to read and keep updated.
  85. And based on looking at glibc code, annotating inline asm in .c files
  86. might be even worse.
  87. Objtool still needs a few annotations, but only in code which does
  88. unusual things to the stack like entry code. And even then, far fewer
  89. annotations are needed than what DWARF would need, so they're much more
  90. maintainable than DWARF CFI annotations.
  91. So the advantages of using objtool to generate ORC data are that it
  92. gives more accurate debuginfo, with very few annotations. It also
  93. insulates the kernel from toolchain bugs which can be very painful to
  94. deal with in the kernel since we often have to workaround issues in
  95. older versions of the toolchain for years.
  96. The downside is that the unwinder now becomes dependent on objtool's
  97. ability to reverse engineer GCC code flow. If GCC optimizations become
  98. too complicated for objtool to follow, the ORC data generation might
  99. stop working or become incomplete. (It's worth noting that livepatch
  100. already has such a dependency on objtool's ability to follow GCC code
  101. flow.)
  102. If newer versions of GCC come up with some optimizations which break
  103. objtool, we may need to revisit the current implementation. Some
  104. possible solutions would be asking GCC to make the optimizations more
  105. palatable, or having objtool use DWARF as an additional input, or
  106. creating a GCC plugin to assist objtool with its analysis. But for now,
  107. objtool follows GCC code quite well.
  108. Unwinder implementation details
  109. ===============================
  110. Objtool generates the ORC data by integrating with the compile-time
  111. stack metadata validation feature, which is described in detail in
  112. tools/objtool/Documentation/objtool.txt. After analyzing all
  113. the code paths of a .o file, it creates an array of orc_entry structs,
  114. and a parallel array of instruction addresses associated with those
  115. structs, and writes them to the .orc_unwind and .orc_unwind_ip sections
  116. respectively.
  117. The ORC data is split into the two arrays for performance reasons, to
  118. make the searchable part of the data (.orc_unwind_ip) more compact. The
  119. arrays are sorted in parallel at boot time.
  120. Performance is further improved by the use of a fast lookup table which
  121. is created at runtime. The fast lookup table associates a given address
  122. with a range of indices for the .orc_unwind table, so that only a small
  123. subset of the table needs to be searched.
  124. Etymology
  125. =========
  126. Orcs, fearsome creatures of medieval folklore, are the Dwarves' natural
  127. enemies. Similarly, the ORC unwinder was created in opposition to the
  128. complexity and slowness of DWARF.
  129. "Although Orcs rarely consider multiple solutions to a problem, they do
  130. excel at getting things done because they are creatures of action, not
  131. thought." [3]_ Similarly, unlike the esoteric DWARF unwinder, the
  132. veracious ORC unwinder wastes no time or siloconic effort decoding
  133. variable-length zero-extended unsigned-integer byte-coded
  134. state-machine-based debug information entries.
  135. Similar to how Orcs frequently unravel the well-intentioned plans of
  136. their adversaries, the ORC unwinder frequently unravels stacks with
  137. brutal, unyielding efficiency.
  138. ORC stands for Oops Rewind Capability.
  139. .. [1] https://lore.kernel.org/r/20170602104048.jkkzssljsompjdwy@suse.de
  140. .. [2] https://lore.kernel.org/r/d2ca5435-6386-29b8-db87-7f227c2b713a@suse.cz
  141. .. [3] http://dustin.wikidot.com/half-orcs-and-orcs