xref: /openbmc/linux/Documentation/arch/x86/orc-unwinder.rst (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1*ff61f079SJonathan Corbet.. SPDX-License-Identifier: GPL-2.0
2*ff61f079SJonathan Corbet
3*ff61f079SJonathan Corbet============
4*ff61f079SJonathan CorbetORC unwinder
5*ff61f079SJonathan Corbet============
6*ff61f079SJonathan Corbet
7*ff61f079SJonathan CorbetOverview
8*ff61f079SJonathan Corbet========
9*ff61f079SJonathan Corbet
10*ff61f079SJonathan CorbetThe kernel CONFIG_UNWINDER_ORC option enables the ORC unwinder, which is
11*ff61f079SJonathan Corbetsimilar in concept to a DWARF unwinder.  The difference is that the
12*ff61f079SJonathan Corbetformat of the ORC data is much simpler than DWARF, which in turn allows
13*ff61f079SJonathan Corbetthe ORC unwinder to be much simpler and faster.
14*ff61f079SJonathan Corbet
15*ff61f079SJonathan CorbetThe ORC data consists of unwind tables which are generated by objtool.
16*ff61f079SJonathan CorbetThey contain out-of-band data which is used by the in-kernel ORC
17*ff61f079SJonathan Corbetunwinder.  Objtool generates the ORC data by first doing compile-time
18*ff61f079SJonathan Corbetstack metadata validation (CONFIG_STACK_VALIDATION).  After analyzing
19*ff61f079SJonathan Corbetall the code paths of a .o file, it determines information about the
20*ff61f079SJonathan Corbetstack state at each instruction address in the file and outputs that
21*ff61f079SJonathan Corbetinformation to the .orc_unwind and .orc_unwind_ip sections.
22*ff61f079SJonathan Corbet
23*ff61f079SJonathan CorbetThe per-object ORC sections are combined at link time and are sorted and
24*ff61f079SJonathan Corbetpost-processed at boot time.  The unwinder uses the resulting data to
25*ff61f079SJonathan Corbetcorrelate instruction addresses with their stack states at run time.
26*ff61f079SJonathan Corbet
27*ff61f079SJonathan Corbet
28*ff61f079SJonathan CorbetORC vs frame pointers
29*ff61f079SJonathan Corbet=====================
30*ff61f079SJonathan Corbet
31*ff61f079SJonathan CorbetWith frame pointers enabled, GCC adds instrumentation code to every
32*ff61f079SJonathan Corbetfunction in the kernel.  The kernel's .text size increases by about
33*ff61f079SJonathan Corbet3.2%, resulting in a broad kernel-wide slowdown.  Measurements by Mel
34*ff61f079SJonathan CorbetGorman [1]_ have shown a slowdown of 5-10% for some workloads.
35*ff61f079SJonathan Corbet
36*ff61f079SJonathan CorbetIn contrast, the ORC unwinder has no effect on text size or runtime
37*ff61f079SJonathan Corbetperformance, because the debuginfo is out of band.  So if you disable
38*ff61f079SJonathan Corbetframe pointers and enable the ORC unwinder, you get a nice performance
39*ff61f079SJonathan Corbetimprovement across the board, and still have reliable stack traces.
40*ff61f079SJonathan Corbet
41*ff61f079SJonathan CorbetIngo Molnar says:
42*ff61f079SJonathan Corbet
43*ff61f079SJonathan Corbet  "Note that it's not just a performance improvement, but also an
44*ff61f079SJonathan Corbet  instruction cache locality improvement: 3.2% .text savings almost
45*ff61f079SJonathan Corbet  directly transform into a similarly sized reduction in cache
46*ff61f079SJonathan Corbet  footprint. That can transform to even higher speedups for workloads
47*ff61f079SJonathan Corbet  whose cache locality is borderline."
48*ff61f079SJonathan Corbet
49*ff61f079SJonathan CorbetAnother benefit of ORC compared to frame pointers is that it can
50*ff61f079SJonathan Corbetreliably unwind across interrupts and exceptions.  Frame pointer based
51*ff61f079SJonathan Corbetunwinds can sometimes skip the caller of the interrupted function, if it
52*ff61f079SJonathan Corbetwas a leaf function or if the interrupt hit before the frame pointer was
53*ff61f079SJonathan Corbetsaved.
54*ff61f079SJonathan Corbet
55*ff61f079SJonathan CorbetThe main disadvantage of the ORC unwinder compared to frame pointers is
56*ff61f079SJonathan Corbetthat it needs more memory to store the ORC unwind tables: roughly 2-4MB
57*ff61f079SJonathan Corbetdepending on the kernel config.
58*ff61f079SJonathan Corbet
59*ff61f079SJonathan Corbet
60*ff61f079SJonathan CorbetORC vs DWARF
61*ff61f079SJonathan Corbet============
62*ff61f079SJonathan Corbet
63*ff61f079SJonathan CorbetORC debuginfo's advantage over DWARF itself is that it's much simpler.
64*ff61f079SJonathan CorbetIt gets rid of the complex DWARF CFI state machine and also gets rid of
65*ff61f079SJonathan Corbetthe tracking of unnecessary registers.  This allows the unwinder to be
66*ff61f079SJonathan Corbetmuch simpler, meaning fewer bugs, which is especially important for
67*ff61f079SJonathan Corbetmission critical oops code.
68*ff61f079SJonathan Corbet
69*ff61f079SJonathan CorbetThe simpler debuginfo format also enables the unwinder to be much faster
70*ff61f079SJonathan Corbetthan DWARF, which is important for perf and lockdep.  In a basic
71*ff61f079SJonathan Corbetperformance test by Jiri Slaby [2]_, the ORC unwinder was about 20x
72*ff61f079SJonathan Corbetfaster than an out-of-tree DWARF unwinder.  (Note: That measurement was
73*ff61f079SJonathan Corbettaken before some performance tweaks were added, which doubled
74*ff61f079SJonathan Corbetperformance, so the speedup over DWARF may be closer to 40x.)
75*ff61f079SJonathan Corbet
76*ff61f079SJonathan CorbetThe ORC data format does have a few downsides compared to DWARF.  ORC
77*ff61f079SJonathan Corbetunwind tables take up ~50% more RAM (+1.3MB on an x86 defconfig kernel)
78*ff61f079SJonathan Corbetthan DWARF-based eh_frame tables.
79*ff61f079SJonathan Corbet
80*ff61f079SJonathan CorbetAnother potential downside is that, as GCC evolves, it's conceivable
81*ff61f079SJonathan Corbetthat the ORC data may end up being *too* simple to describe the state of
82*ff61f079SJonathan Corbetthe stack for certain optimizations.  But IMO this is unlikely because
83*ff61f079SJonathan CorbetGCC saves the frame pointer for any unusual stack adjustments it does,
84*ff61f079SJonathan Corbetso I suspect we'll really only ever need to keep track of the stack
85*ff61f079SJonathan Corbetpointer and the frame pointer between call frames.  But even if we do
86*ff61f079SJonathan Corbetend up having to track all the registers DWARF tracks, at least we will
87*ff61f079SJonathan Corbetstill be able to control the format, e.g. no complex state machines.
88*ff61f079SJonathan Corbet
89*ff61f079SJonathan Corbet
90*ff61f079SJonathan CorbetORC unwind table generation
91*ff61f079SJonathan Corbet===========================
92*ff61f079SJonathan Corbet
93*ff61f079SJonathan CorbetThe ORC data is generated by objtool.  With the existing compile-time
94*ff61f079SJonathan Corbetstack metadata validation feature, objtool already follows all code
95*ff61f079SJonathan Corbetpaths, and so it already has all the information it needs to be able to
96*ff61f079SJonathan Corbetgenerate ORC data from scratch.  So it's an easy step to go from stack
97*ff61f079SJonathan Corbetvalidation to ORC data generation.
98*ff61f079SJonathan Corbet
99*ff61f079SJonathan CorbetIt should be possible to instead generate the ORC data with a simple
100*ff61f079SJonathan Corbettool which converts DWARF to ORC data.  However, such a solution would
101*ff61f079SJonathan Corbetbe incomplete due to the kernel's extensive use of asm, inline asm, and
102*ff61f079SJonathan Corbetspecial sections like exception tables.
103*ff61f079SJonathan Corbet
104*ff61f079SJonathan CorbetThat could be rectified by manually annotating those special code paths
105*ff61f079SJonathan Corbetusing GNU assembler .cfi annotations in .S files, and homegrown
106*ff61f079SJonathan Corbetannotations for inline asm in .c files.  But asm annotations were tried
107*ff61f079SJonathan Corbetin the past and were found to be unmaintainable.  They were often
108*ff61f079SJonathan Corbetincorrect/incomplete and made the code harder to read and keep updated.
109*ff61f079SJonathan CorbetAnd based on looking at glibc code, annotating inline asm in .c files
110*ff61f079SJonathan Corbetmight be even worse.
111*ff61f079SJonathan Corbet
112*ff61f079SJonathan CorbetObjtool still needs a few annotations, but only in code which does
113*ff61f079SJonathan Corbetunusual things to the stack like entry code.  And even then, far fewer
114*ff61f079SJonathan Corbetannotations are needed than what DWARF would need, so they're much more
115*ff61f079SJonathan Corbetmaintainable than DWARF CFI annotations.
116*ff61f079SJonathan Corbet
117*ff61f079SJonathan CorbetSo the advantages of using objtool to generate ORC data are that it
118*ff61f079SJonathan Corbetgives more accurate debuginfo, with very few annotations.  It also
119*ff61f079SJonathan Corbetinsulates the kernel from toolchain bugs which can be very painful to
120*ff61f079SJonathan Corbetdeal with in the kernel since we often have to workaround issues in
121*ff61f079SJonathan Corbetolder versions of the toolchain for years.
122*ff61f079SJonathan Corbet
123*ff61f079SJonathan CorbetThe downside is that the unwinder now becomes dependent on objtool's
124*ff61f079SJonathan Corbetability to reverse engineer GCC code flow.  If GCC optimizations become
125*ff61f079SJonathan Corbettoo complicated for objtool to follow, the ORC data generation might
126*ff61f079SJonathan Corbetstop working or become incomplete.  (It's worth noting that livepatch
127*ff61f079SJonathan Corbetalready has such a dependency on objtool's ability to follow GCC code
128*ff61f079SJonathan Corbetflow.)
129*ff61f079SJonathan Corbet
130*ff61f079SJonathan CorbetIf newer versions of GCC come up with some optimizations which break
131*ff61f079SJonathan Corbetobjtool, we may need to revisit the current implementation.  Some
132*ff61f079SJonathan Corbetpossible solutions would be asking GCC to make the optimizations more
133*ff61f079SJonathan Corbetpalatable, or having objtool use DWARF as an additional input, or
134*ff61f079SJonathan Corbetcreating a GCC plugin to assist objtool with its analysis.  But for now,
135*ff61f079SJonathan Corbetobjtool follows GCC code quite well.
136*ff61f079SJonathan Corbet
137*ff61f079SJonathan Corbet
138*ff61f079SJonathan CorbetUnwinder implementation details
139*ff61f079SJonathan Corbet===============================
140*ff61f079SJonathan Corbet
141*ff61f079SJonathan CorbetObjtool generates the ORC data by integrating with the compile-time
142*ff61f079SJonathan Corbetstack metadata validation feature, which is described in detail in
143*ff61f079SJonathan Corbettools/objtool/Documentation/objtool.txt.  After analyzing all
144*ff61f079SJonathan Corbetthe code paths of a .o file, it creates an array of orc_entry structs,
145*ff61f079SJonathan Corbetand a parallel array of instruction addresses associated with those
146*ff61f079SJonathan Corbetstructs, and writes them to the .orc_unwind and .orc_unwind_ip sections
147*ff61f079SJonathan Corbetrespectively.
148*ff61f079SJonathan Corbet
149*ff61f079SJonathan CorbetThe ORC data is split into the two arrays for performance reasons, to
150*ff61f079SJonathan Corbetmake the searchable part of the data (.orc_unwind_ip) more compact.  The
151*ff61f079SJonathan Corbetarrays are sorted in parallel at boot time.
152*ff61f079SJonathan Corbet
153*ff61f079SJonathan CorbetPerformance is further improved by the use of a fast lookup table which
154*ff61f079SJonathan Corbetis created at runtime.  The fast lookup table associates a given address
155*ff61f079SJonathan Corbetwith a range of indices for the .orc_unwind table, so that only a small
156*ff61f079SJonathan Corbetsubset of the table needs to be searched.
157*ff61f079SJonathan Corbet
158*ff61f079SJonathan Corbet
159*ff61f079SJonathan CorbetEtymology
160*ff61f079SJonathan Corbet=========
161*ff61f079SJonathan Corbet
162*ff61f079SJonathan CorbetOrcs, fearsome creatures of medieval folklore, are the Dwarves' natural
163*ff61f079SJonathan Corbetenemies.  Similarly, the ORC unwinder was created in opposition to the
164*ff61f079SJonathan Corbetcomplexity and slowness of DWARF.
165*ff61f079SJonathan Corbet
166*ff61f079SJonathan Corbet"Although Orcs rarely consider multiple solutions to a problem, they do
167*ff61f079SJonathan Corbetexcel at getting things done because they are creatures of action, not
168*ff61f079SJonathan Corbetthought." [3]_  Similarly, unlike the esoteric DWARF unwinder, the
169*ff61f079SJonathan Corbetveracious ORC unwinder wastes no time or siloconic effort decoding
170*ff61f079SJonathan Corbetvariable-length zero-extended unsigned-integer byte-coded
171*ff61f079SJonathan Corbetstate-machine-based debug information entries.
172*ff61f079SJonathan Corbet
173*ff61f079SJonathan CorbetSimilar to how Orcs frequently unravel the well-intentioned plans of
174*ff61f079SJonathan Corbettheir adversaries, the ORC unwinder frequently unravels stacks with
175*ff61f079SJonathan Corbetbrutal, unyielding efficiency.
176*ff61f079SJonathan Corbet
177*ff61f079SJonathan CorbetORC stands for Oops Rewind Capability.
178*ff61f079SJonathan Corbet
179*ff61f079SJonathan Corbet
180*ff61f079SJonathan Corbet.. [1] https://lore.kernel.org/r/20170602104048.jkkzssljsompjdwy@suse.de
181*ff61f079SJonathan Corbet.. [2] https://lore.kernel.org/r/d2ca5435-6386-29b8-db87-7f227c2b713a@suse.cz
182*ff61f079SJonathan Corbet.. [3] http://dustin.wikidot.com/half-orcs-and-orcs
183