]> git.proxmox.com Git - pve-kernel.git/blob - patches/kernel/0035-x86-unwind-Add-the-ORC-unwinder.patch
e2170a4a9210dddfb40163620386a646aaf0c2b3
[pve-kernel.git] / patches / kernel / 0035-x86-unwind-Add-the-ORC-unwinder.patch
1 From 906230051aedc4af1dce033db79e56eb61af2b73 Mon Sep 17 00:00:00 2001
2 From: Josh Poimboeuf <jpoimboe@redhat.com>
3 Date: Mon, 24 Jul 2017 18:36:57 -0500
4 Subject: [PATCH 035/233] x86/unwind: Add the ORC unwinder
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 CVE-2017-5754
10
11 Add the new ORC unwinder which is enabled by CONFIG_ORC_UNWINDER=y.
12 It plugs into the existing x86 unwinder framework.
13
14 It relies on objtool to generate the needed .orc_unwind and
15 .orc_unwind_ip sections.
16
17 For more details on why ORC is used instead of DWARF, see
18 Documentation/x86/orc-unwinder.txt - but the short version is
19 that it's a simplified, fundamentally more robust debugninfo
20 data structure, which also allows up to two orders of magnitude
21 faster lookups than the DWARF unwinder - which matters to
22 profiling workloads like perf.
23
24 Thanks to Andy Lutomirski for the performance improvement ideas:
25 splitting the ORC unwind table into two parallel arrays and creating a
26 fast lookup table to search a subset of the unwind table.
27
28 Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
29 Cc: Andy Lutomirski <luto@kernel.org>
30 Cc: Borislav Petkov <bp@alien8.de>
31 Cc: Brian Gerst <brgerst@gmail.com>
32 Cc: Denys Vlasenko <dvlasenk@redhat.com>
33 Cc: H. Peter Anvin <hpa@zytor.com>
34 Cc: Jiri Slaby <jslaby@suse.cz>
35 Cc: Linus Torvalds <torvalds@linux-foundation.org>
36 Cc: Mike Galbraith <efault@gmx.de>
37 Cc: Peter Zijlstra <peterz@infradead.org>
38 Cc: Thomas Gleixner <tglx@linutronix.de>
39 Cc: live-patching@vger.kernel.org
40 Link: http://lkml.kernel.org/r/0a6cbfb40f8da99b7a45a1a8302dc6aef16ec812.1500938583.git.jpoimboe@redhat.com
41 [ Extended the changelog. ]
42 Signed-off-by: Ingo Molnar <mingo@kernel.org>
43 (cherry picked from commit ee9f8fce99640811b2b8e79d0d1dbe8bab69ba67)
44 Signed-off-by: Andy Whitcroft <apw@canonical.com>
45 Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
46 (cherry picked from commit dccbf63d7a6cc431af23a86e28275a74904545cd)
47 Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
48 ---
49 Documentation/x86/orc-unwinder.txt | 179 ++++++++++++
50 arch/x86/kernel/Makefile | 8 +-
51 scripts/Makefile.build | 14 +-
52 arch/um/include/asm/unwind.h | 8 +
53 arch/x86/include/asm/module.h | 9 +
54 arch/x86/include/asm/orc_lookup.h | 46 +++
55 arch/x86/include/asm/orc_types.h | 2 +-
56 arch/x86/include/asm/unwind.h | 76 +++--
57 include/asm-generic/vmlinux.lds.h | 27 +-
58 arch/x86/kernel/module.c | 11 +-
59 arch/x86/kernel/setup.c | 3 +
60 arch/x86/kernel/unwind_frame.c | 39 +--
61 arch/x86/kernel/unwind_guess.c | 5 +
62 arch/x86/kernel/unwind_orc.c | 582 +++++++++++++++++++++++++++++++++++++
63 arch/x86/Kconfig | 1 +
64 arch/x86/Kconfig.debug | 25 ++
65 arch/x86/kernel/vmlinux.lds.S | 3 +
66 lib/Kconfig.debug | 3 +
67 18 files changed, 977 insertions(+), 64 deletions(-)
68 create mode 100644 Documentation/x86/orc-unwinder.txt
69 create mode 100644 arch/um/include/asm/unwind.h
70 create mode 100644 arch/x86/include/asm/orc_lookup.h
71 create mode 100644 arch/x86/kernel/unwind_orc.c
72
73 diff --git a/Documentation/x86/orc-unwinder.txt b/Documentation/x86/orc-unwinder.txt
74 new file mode 100644
75 index 000000000000..af0c9a4c65a6
76 --- /dev/null
77 +++ b/Documentation/x86/orc-unwinder.txt
78 @@ -0,0 +1,179 @@
79 +ORC unwinder
80 +============
81 +
82 +Overview
83 +--------
84 +
85 +The kernel CONFIG_ORC_UNWINDER option enables the ORC unwinder, which is
86 +similar in concept to a DWARF unwinder. The difference is that the
87 +format of the ORC data is much simpler than DWARF, which in turn allows
88 +the ORC unwinder to be much simpler and faster.
89 +
90 +The ORC data consists of unwind tables which are generated by objtool.
91 +They contain out-of-band data which is used by the in-kernel ORC
92 +unwinder. Objtool generates the ORC data by first doing compile-time
93 +stack metadata validation (CONFIG_STACK_VALIDATION). After analyzing
94 +all the code paths of a .o file, it determines information about the
95 +stack state at each instruction address in the file and outputs that
96 +information to the .orc_unwind and .orc_unwind_ip sections.
97 +
98 +The per-object ORC sections are combined at link time and are sorted and
99 +post-processed at boot time. The unwinder uses the resulting data to
100 +correlate instruction addresses with their stack states at run time.
101 +
102 +
103 +ORC vs frame pointers
104 +---------------------
105 +
106 +With frame pointers enabled, GCC adds instrumentation code to every
107 +function in the kernel. The kernel's .text size increases by about
108 +3.2%, resulting in a broad kernel-wide slowdown. Measurements by Mel
109 +Gorman [1] have shown a slowdown of 5-10% for some workloads.
110 +
111 +In contrast, the ORC unwinder has no effect on text size or runtime
112 +performance, because the debuginfo is out of band. So if you disable
113 +frame pointers and enable the ORC unwinder, you get a nice performance
114 +improvement across the board, and still have reliable stack traces.
115 +
116 +Ingo Molnar says:
117 +
118 + "Note that it's not just a performance improvement, but also an
119 + instruction cache locality improvement: 3.2% .text savings almost
120 + directly transform into a similarly sized reduction in cache
121 + footprint. That can transform to even higher speedups for workloads
122 + whose cache locality is borderline."
123 +
124 +Another benefit of ORC compared to frame pointers is that it can
125 +reliably unwind across interrupts and exceptions. Frame pointer based
126 +unwinds can sometimes skip the caller of the interrupted function, if it
127 +was a leaf function or if the interrupt hit before the frame pointer was
128 +saved.
129 +
130 +The main disadvantage of the ORC unwinder compared to frame pointers is
131 +that it needs more memory to store the ORC unwind tables: roughly 2-4MB
132 +depending on the kernel config.
133 +
134 +
135 +ORC vs DWARF
136 +------------
137 +
138 +ORC debuginfo's advantage over DWARF itself is that it's much simpler.
139 +It gets rid of the complex DWARF CFI state machine and also gets rid of
140 +the tracking of unnecessary registers. This allows the unwinder to be
141 +much simpler, meaning fewer bugs, which is especially important for
142 +mission critical oops code.
143 +
144 +The simpler debuginfo format also enables the unwinder to be much faster
145 +than DWARF, which is important for perf and lockdep. In a basic
146 +performance test by Jiri Slaby [2], the ORC unwinder was about 20x
147 +faster than an out-of-tree DWARF unwinder. (Note: That measurement was
148 +taken before some performance tweaks were added, which doubled
149 +performance, so the speedup over DWARF may be closer to 40x.)
150 +
151 +The ORC data format does have a few downsides compared to DWARF. ORC
152 +unwind tables take up ~50% more RAM (+1.3MB on an x86 defconfig kernel)
153 +than DWARF-based eh_frame tables.
154 +
155 +Another potential downside is that, as GCC evolves, it's conceivable
156 +that the ORC data may end up being *too* simple to describe the state of
157 +the stack for certain optimizations. But IMO this is unlikely because
158 +GCC saves the frame pointer for any unusual stack adjustments it does,
159 +so I suspect we'll really only ever need to keep track of the stack
160 +pointer and the frame pointer between call frames. But even if we do
161 +end up having to track all the registers DWARF tracks, at least we will
162 +still be able to control the format, e.g. no complex state machines.
163 +
164 +
165 +ORC unwind table generation
166 +---------------------------
167 +
168 +The ORC data is generated by objtool. With the existing compile-time
169 +stack metadata validation feature, objtool already follows all code
170 +paths, and so it already has all the information it needs to be able to
171 +generate ORC data from scratch. So it's an easy step to go from stack
172 +validation to ORC data generation.
173 +
174 +It should be possible to instead generate the ORC data with a simple
175 +tool which converts DWARF to ORC data. However, such a solution would
176 +be incomplete due to the kernel's extensive use of asm, inline asm, and
177 +special sections like exception tables.
178 +
179 +That could be rectified by manually annotating those special code paths
180 +using GNU assembler .cfi annotations in .S files, and homegrown
181 +annotations for inline asm in .c files. But asm annotations were tried
182 +in the past and were found to be unmaintainable. They were often
183 +incorrect/incomplete and made the code harder to read and keep updated.
184 +And based on looking at glibc code, annotating inline asm in .c files
185 +might be even worse.
186 +
187 +Objtool still needs a few annotations, but only in code which does
188 +unusual things to the stack like entry code. And even then, far fewer
189 +annotations are needed than what DWARF would need, so they're much more
190 +maintainable than DWARF CFI annotations.
191 +
192 +So the advantages of using objtool to generate ORC data are that it
193 +gives more accurate debuginfo, with very few annotations. It also
194 +insulates the kernel from toolchain bugs which can be very painful to
195 +deal with in the kernel since we often have to workaround issues in
196 +older versions of the toolchain for years.
197 +
198 +The downside is that the unwinder now becomes dependent on objtool's
199 +ability to reverse engineer GCC code flow. If GCC optimizations become
200 +too complicated for objtool to follow, the ORC data generation might
201 +stop working or become incomplete. (It's worth noting that livepatch
202 +already has such a dependency on objtool's ability to follow GCC code
203 +flow.)
204 +
205 +If newer versions of GCC come up with some optimizations which break
206 +objtool, we may need to revisit the current implementation. Some
207 +possible solutions would be asking GCC to make the optimizations more
208 +palatable, or having objtool use DWARF as an additional input, or
209 +creating a GCC plugin to assist objtool with its analysis. But for now,
210 +objtool follows GCC code quite well.
211 +
212 +
213 +Unwinder implementation details
214 +-------------------------------
215 +
216 +Objtool generates the ORC data by integrating with the compile-time
217 +stack metadata validation feature, which is described in detail in
218 +tools/objtool/Documentation/stack-validation.txt. After analyzing all
219 +the code paths of a .o file, it creates an array of orc_entry structs,
220 +and a parallel array of instruction addresses associated with those
221 +structs, and writes them to the .orc_unwind and .orc_unwind_ip sections
222 +respectively.
223 +
224 +The ORC data is split into the two arrays for performance reasons, to
225 +make the searchable part of the data (.orc_unwind_ip) more compact. The
226 +arrays are sorted in parallel at boot time.
227 +
228 +Performance is further improved by the use of a fast lookup table which
229 +is created at runtime. The fast lookup table associates a given address
230 +with a range of indices for the .orc_unwind table, so that only a small
231 +subset of the table needs to be searched.
232 +
233 +
234 +Etymology
235 +---------
236 +
237 +Orcs, fearsome creatures of medieval folklore, are the Dwarves' natural
238 +enemies. Similarly, the ORC unwinder was created in opposition to the
239 +complexity and slowness of DWARF.
240 +
241 +"Although Orcs rarely consider multiple solutions to a problem, they do
242 +excel at getting things done because they are creatures of action, not
243 +thought." [3] Similarly, unlike the esoteric DWARF unwinder, the
244 +veracious ORC unwinder wastes no time or siloconic effort decoding
245 +variable-length zero-extended unsigned-integer byte-coded
246 +state-machine-based debug information entries.
247 +
248 +Similar to how Orcs frequently unravel the well-intentioned plans of
249 +their adversaries, the ORC unwinder frequently unravels stacks with
250 +brutal, unyielding efficiency.
251 +
252 +ORC stands for Oops Rewind Capability.
253 +
254 +
255 +[1] https://lkml.kernel.org/r/20170602104048.jkkzssljsompjdwy@suse.de
256 +[2] https://lkml.kernel.org/r/d2ca5435-6386-29b8-db87-7f227c2b713a@suse.cz
257 +[3] http://dustin.wikidot.com/half-orcs-and-orcs
258 diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
259 index a01892bdd61a..287eac7d207f 100644
260 --- a/arch/x86/kernel/Makefile
261 +++ b/arch/x86/kernel/Makefile
262 @@ -126,11 +126,9 @@ obj-$(CONFIG_PERF_EVENTS) += perf_regs.o
263 obj-$(CONFIG_TRACING) += tracepoint.o
264 obj-$(CONFIG_SCHED_MC_PRIO) += itmt.o
265
266 -ifdef CONFIG_FRAME_POINTER
267 -obj-y += unwind_frame.o
268 -else
269 -obj-y += unwind_guess.o
270 -endif
271 +obj-$(CONFIG_ORC_UNWINDER) += unwind_orc.o
272 +obj-$(CONFIG_FRAME_POINTER_UNWINDER) += unwind_frame.o
273 +obj-$(CONFIG_GUESS_UNWINDER) += unwind_guess.o
274
275 ###
276 # 64 bit specific files
277 diff --git a/scripts/Makefile.build b/scripts/Makefile.build
278 index 273bc2228307..ab2c8ef43cdb 100644
279 --- a/scripts/Makefile.build
280 +++ b/scripts/Makefile.build
281 @@ -258,7 +258,8 @@ ifneq ($(SKIP_STACK_VALIDATION),1)
282
283 __objtool_obj := $(objtree)/tools/objtool/objtool
284
285 -objtool_args = check
286 +objtool_args = $(if $(CONFIG_ORC_UNWINDER),orc generate,check)
287 +
288 ifndef CONFIG_FRAME_POINTER
289 objtool_args += --no-fp
290 endif
291 @@ -276,6 +277,11 @@ objtool_obj = $(if $(patsubst y%,, \
292 endif # SKIP_STACK_VALIDATION
293 endif # CONFIG_STACK_VALIDATION
294
295 +# Rebuild all objects when objtool changes, or is enabled/disabled.
296 +objtool_dep = $(objtool_obj) \
297 + $(wildcard include/config/orc/unwinder.h \
298 + include/config/stack/validation.h)
299 +
300 define rule_cc_o_c
301 $(call echo-cmd,checksrc) $(cmd_checksrc) \
302 $(call cmd_and_fixdep,cc_o_c) \
303 @@ -298,14 +304,14 @@ cmd_undef_syms = echo
304 endif
305
306 # Built-in and composite module parts
307 -$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_obj) FORCE
308 +$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
309 $(call cmd,force_checksrc)
310 $(call cmd,force_check_kmsg)
311 $(call if_changed_rule,cc_o_c)
312
313 # Single-part modules are special since we need to mark them in $(MODVERDIR)
314
315 -$(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_obj) FORCE
316 +$(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
317 $(call cmd,force_checksrc)
318 $(call cmd,force_check_kmsg)
319 $(call if_changed_rule,cc_o_c)
320 @@ -401,7 +407,7 @@ cmd_modversions_S = \
321 endif
322 endif
323
324 -$(obj)/%.o: $(src)/%.S $(objtool_obj) FORCE
325 +$(obj)/%.o: $(src)/%.S $(objtool_dep) FORCE
326 $(call if_changed_rule,as_o_S)
327
328 targets += $(real-objs-y) $(real-objs-m) $(lib-y)
329 diff --git a/arch/um/include/asm/unwind.h b/arch/um/include/asm/unwind.h
330 new file mode 100644
331 index 000000000000..7ffa5437b761
332 --- /dev/null
333 +++ b/arch/um/include/asm/unwind.h
334 @@ -0,0 +1,8 @@
335 +#ifndef _ASM_UML_UNWIND_H
336 +#define _ASM_UML_UNWIND_H
337 +
338 +static inline void
339 +unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
340 + void *orc, size_t orc_size) {}
341 +
342 +#endif /* _ASM_UML_UNWIND_H */
343 diff --git a/arch/x86/include/asm/module.h b/arch/x86/include/asm/module.h
344 index e3b7819caeef..9eb7c718aaf8 100644
345 --- a/arch/x86/include/asm/module.h
346 +++ b/arch/x86/include/asm/module.h
347 @@ -2,6 +2,15 @@
348 #define _ASM_X86_MODULE_H
349
350 #include <asm-generic/module.h>
351 +#include <asm/orc_types.h>
352 +
353 +struct mod_arch_specific {
354 +#ifdef CONFIG_ORC_UNWINDER
355 + unsigned int num_orcs;
356 + int *orc_unwind_ip;
357 + struct orc_entry *orc_unwind;
358 +#endif
359 +};
360
361 #ifdef CONFIG_X86_64
362 /* X86_64 does not define MODULE_PROC_FAMILY */
363 diff --git a/arch/x86/include/asm/orc_lookup.h b/arch/x86/include/asm/orc_lookup.h
364 new file mode 100644
365 index 000000000000..91c8d868424d
366 --- /dev/null
367 +++ b/arch/x86/include/asm/orc_lookup.h
368 @@ -0,0 +1,46 @@
369 +/*
370 + * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
371 + *
372 + * This program is free software; you can redistribute it and/or
373 + * modify it under the terms of the GNU General Public License
374 + * as published by the Free Software Foundation; either version 2
375 + * of the License, or (at your option) any later version.
376 + *
377 + * This program is distributed in the hope that it will be useful,
378 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
379 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
380 + * GNU General Public License for more details.
381 + *
382 + * You should have received a copy of the GNU General Public License
383 + * along with this program; if not, see <http://www.gnu.org/licenses/>.
384 + */
385 +#ifndef _ORC_LOOKUP_H
386 +#define _ORC_LOOKUP_H
387 +
388 +/*
389 + * This is a lookup table for speeding up access to the .orc_unwind table.
390 + * Given an input address offset, the corresponding lookup table entry
391 + * specifies a subset of the .orc_unwind table to search.
392 + *
393 + * Each block represents the end of the previous range and the start of the
394 + * next range. An extra block is added to give the last range an end.
395 + *
396 + * The block size should be a power of 2 to avoid a costly 'div' instruction.
397 + *
398 + * A block size of 256 was chosen because it roughly doubles unwinder
399 + * performance while only adding ~5% to the ORC data footprint.
400 + */
401 +#define LOOKUP_BLOCK_ORDER 8
402 +#define LOOKUP_BLOCK_SIZE (1 << LOOKUP_BLOCK_ORDER)
403 +
404 +#ifndef LINKER_SCRIPT
405 +
406 +extern unsigned int orc_lookup[];
407 +extern unsigned int orc_lookup_end[];
408 +
409 +#define LOOKUP_START_IP (unsigned long)_stext
410 +#define LOOKUP_STOP_IP (unsigned long)_etext
411 +
412 +#endif /* LINKER_SCRIPT */
413 +
414 +#endif /* _ORC_LOOKUP_H */
415 diff --git a/arch/x86/include/asm/orc_types.h b/arch/x86/include/asm/orc_types.h
416 index 7dc777a6cb40..9c9dc579bd7d 100644
417 --- a/arch/x86/include/asm/orc_types.h
418 +++ b/arch/x86/include/asm/orc_types.h
419 @@ -88,7 +88,7 @@ struct orc_entry {
420 unsigned sp_reg:4;
421 unsigned bp_reg:4;
422 unsigned type:2;
423 -};
424 +} __packed;
425
426 /*
427 * This struct is used by asm and inline asm code to manually annotate the
428 diff --git a/arch/x86/include/asm/unwind.h b/arch/x86/include/asm/unwind.h
429 index e6676495b125..25b8d31a007d 100644
430 --- a/arch/x86/include/asm/unwind.h
431 +++ b/arch/x86/include/asm/unwind.h
432 @@ -12,11 +12,14 @@ struct unwind_state {
433 struct task_struct *task;
434 int graph_idx;
435 bool error;
436 -#ifdef CONFIG_FRAME_POINTER
437 +#if defined(CONFIG_ORC_UNWINDER)
438 + bool signal, full_regs;
439 + unsigned long sp, bp, ip;
440 + struct pt_regs *regs;
441 +#elif defined(CONFIG_FRAME_POINTER)
442 bool got_irq;
443 - unsigned long *bp, *orig_sp;
444 + unsigned long *bp, *orig_sp, ip;
445 struct pt_regs *regs;
446 - unsigned long ip;
447 #else
448 unsigned long *sp;
449 #endif
450 @@ -24,41 +27,30 @@ struct unwind_state {
451
452 void __unwind_start(struct unwind_state *state, struct task_struct *task,
453 struct pt_regs *regs, unsigned long *first_frame);
454 -
455 bool unwind_next_frame(struct unwind_state *state);
456 -
457 unsigned long unwind_get_return_address(struct unwind_state *state);
458 +unsigned long *unwind_get_return_address_ptr(struct unwind_state *state);
459
460 static inline bool unwind_done(struct unwind_state *state)
461 {
462 return state->stack_info.type == STACK_TYPE_UNKNOWN;
463 }
464
465 -static inline
466 -void unwind_start(struct unwind_state *state, struct task_struct *task,
467 - struct pt_regs *regs, unsigned long *first_frame)
468 -{
469 - first_frame = first_frame ? : get_stack_pointer(task, regs);
470 -
471 - __unwind_start(state, task, regs, first_frame);
472 -}
473 -
474 static inline bool unwind_error(struct unwind_state *state)
475 {
476 return state->error;
477 }
478
479 -#ifdef CONFIG_FRAME_POINTER
480 -
481 static inline
482 -unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
483 +void unwind_start(struct unwind_state *state, struct task_struct *task,
484 + struct pt_regs *regs, unsigned long *first_frame)
485 {
486 - if (unwind_done(state))
487 - return NULL;
488 + first_frame = first_frame ? : get_stack_pointer(task, regs);
489
490 - return state->regs ? &state->regs->ip : state->bp + 1;
491 + __unwind_start(state, task, regs, first_frame);
492 }
493
494 +#if defined(CONFIG_ORC_UNWINDER) || defined(CONFIG_FRAME_POINTER)
495 static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state)
496 {
497 if (unwind_done(state))
498 @@ -66,20 +58,46 @@ static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state)
499
500 return state->regs;
501 }
502 -
503 -#else /* !CONFIG_FRAME_POINTER */
504 -
505 -static inline
506 -unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
507 +#else
508 +static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state)
509 {
510 return NULL;
511 }
512 +#endif
513
514 -static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state)
515 +#ifdef CONFIG_ORC_UNWINDER
516 +void unwind_init(void);
517 +void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
518 + void *orc, size_t orc_size);
519 +#else
520 +static inline void unwind_init(void) {}
521 +static inline
522 +void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
523 + void *orc, size_t orc_size) {}
524 +#endif
525 +
526 +/*
527 + * This disables KASAN checking when reading a value from another task's stack,
528 + * since the other task could be running on another CPU and could have poisoned
529 + * the stack in the meantime.
530 + */
531 +#define READ_ONCE_TASK_STACK(task, x) \
532 +({ \
533 + unsigned long val; \
534 + if (task == current) \
535 + val = READ_ONCE(x); \
536 + else \
537 + val = READ_ONCE_NOCHECK(x); \
538 + val; \
539 +})
540 +
541 +static inline bool task_on_another_cpu(struct task_struct *task)
542 {
543 - return NULL;
544 +#ifdef CONFIG_SMP
545 + return task != current && task->on_cpu;
546 +#else
547 + return false;
548 +#endif
549 }
550
551 -#endif /* CONFIG_FRAME_POINTER */
552 -
553 #endif /* _ASM_X86_UNWIND_H */
554 diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
555 index e7e955d4ab9e..9fdb54a95976 100644
556 --- a/include/asm-generic/vmlinux.lds.h
557 +++ b/include/asm-generic/vmlinux.lds.h
558 @@ -686,6 +686,31 @@
559 #define BUG_TABLE
560 #endif
561
562 +#ifdef CONFIG_ORC_UNWINDER
563 +#define ORC_UNWIND_TABLE \
564 + . = ALIGN(4); \
565 + .orc_unwind_ip : AT(ADDR(.orc_unwind_ip) - LOAD_OFFSET) { \
566 + VMLINUX_SYMBOL(__start_orc_unwind_ip) = .; \
567 + KEEP(*(.orc_unwind_ip)) \
568 + VMLINUX_SYMBOL(__stop_orc_unwind_ip) = .; \
569 + } \
570 + . = ALIGN(6); \
571 + .orc_unwind : AT(ADDR(.orc_unwind) - LOAD_OFFSET) { \
572 + VMLINUX_SYMBOL(__start_orc_unwind) = .; \
573 + KEEP(*(.orc_unwind)) \
574 + VMLINUX_SYMBOL(__stop_orc_unwind) = .; \
575 + } \
576 + . = ALIGN(4); \
577 + .orc_lookup : AT(ADDR(.orc_lookup) - LOAD_OFFSET) { \
578 + VMLINUX_SYMBOL(orc_lookup) = .; \
579 + . += (((SIZEOF(.text) + LOOKUP_BLOCK_SIZE - 1) / \
580 + LOOKUP_BLOCK_SIZE) + 1) * 4; \
581 + VMLINUX_SYMBOL(orc_lookup_end) = .; \
582 + }
583 +#else
584 +#define ORC_UNWIND_TABLE
585 +#endif
586 +
587 #ifdef CONFIG_PM_TRACE
588 #define TRACEDATA \
589 . = ALIGN(4); \
590 @@ -872,7 +897,7 @@
591 DATA_DATA \
592 CONSTRUCTORS \
593 } \
594 - BUG_TABLE
595 + BUG_TABLE \
596
597 #define INIT_TEXT_SECTION(inittext_align) \
598 . = ALIGN(inittext_align); \
599 diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c
600 index f67bd3205df7..62e7d70aadd5 100644
601 --- a/arch/x86/kernel/module.c
602 +++ b/arch/x86/kernel/module.c
603 @@ -35,6 +35,7 @@
604 #include <asm/page.h>
605 #include <asm/pgtable.h>
606 #include <asm/setup.h>
607 +#include <asm/unwind.h>
608
609 #if 0
610 #define DEBUGP(fmt, ...) \
611 @@ -213,7 +214,7 @@ int module_finalize(const Elf_Ehdr *hdr,
612 struct module *me)
613 {
614 const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
615 - *para = NULL;
616 + *para = NULL, *orc = NULL, *orc_ip = NULL;
617 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
618
619 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
620 @@ -225,6 +226,10 @@ int module_finalize(const Elf_Ehdr *hdr,
621 locks = s;
622 if (!strcmp(".parainstructions", secstrings + s->sh_name))
623 para = s;
624 + if (!strcmp(".orc_unwind", secstrings + s->sh_name))
625 + orc = s;
626 + if (!strcmp(".orc_unwind_ip", secstrings + s->sh_name))
627 + orc_ip = s;
628 }
629
630 if (alt) {
631 @@ -248,6 +253,10 @@ int module_finalize(const Elf_Ehdr *hdr,
632 /* make jump label nops */
633 jump_label_apply_nops(me);
634
635 + if (orc && orc_ip)
636 + unwind_module_init(me, (void *)orc_ip->sh_addr, orc_ip->sh_size,
637 + (void *)orc->sh_addr, orc->sh_size);
638 +
639 return 0;
640 }
641
642 diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
643 index f964bfddfefd..dd6e8707e969 100644
644 --- a/arch/x86/kernel/setup.c
645 +++ b/arch/x86/kernel/setup.c
646 @@ -121,6 +121,7 @@
647 #include <asm/microcode.h>
648 #include <asm/mmu_context.h>
649 #include <asm/kaslr.h>
650 +#include <asm/unwind.h>
651
652 /*
653 * max_low_pfn_mapped: highest direct mapped pfn under 4GB
654 @@ -1325,6 +1326,8 @@ void __init setup_arch(char **cmdline_p)
655 if (efi_enabled(EFI_BOOT))
656 efi_apply_memmap_quirks();
657 #endif
658 +
659 + unwind_init();
660 }
661
662 #ifdef CONFIG_X86_32
663 diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c
664 index c29e5bc7e9c9..d145a0b1f529 100644
665 --- a/arch/x86/kernel/unwind_frame.c
666 +++ b/arch/x86/kernel/unwind_frame.c
667 @@ -10,20 +10,22 @@
668
669 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
670
671 -/*
672 - * This disables KASAN checking when reading a value from another task's stack,
673 - * since the other task could be running on another CPU and could have poisoned
674 - * the stack in the meantime.
675 - */
676 -#define READ_ONCE_TASK_STACK(task, x) \
677 -({ \
678 - unsigned long val; \
679 - if (task == current) \
680 - val = READ_ONCE(x); \
681 - else \
682 - val = READ_ONCE_NOCHECK(x); \
683 - val; \
684 -})
685 +unsigned long unwind_get_return_address(struct unwind_state *state)
686 +{
687 + if (unwind_done(state))
688 + return 0;
689 +
690 + return __kernel_text_address(state->ip) ? state->ip : 0;
691 +}
692 +EXPORT_SYMBOL_GPL(unwind_get_return_address);
693 +
694 +unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
695 +{
696 + if (unwind_done(state))
697 + return NULL;
698 +
699 + return state->regs ? &state->regs->ip : state->bp + 1;
700 +}
701
702 static void unwind_dump(struct unwind_state *state)
703 {
704 @@ -66,15 +68,6 @@ static void unwind_dump(struct unwind_state *state)
705 }
706 }
707
708 -unsigned long unwind_get_return_address(struct unwind_state *state)
709 -{
710 - if (unwind_done(state))
711 - return 0;
712 -
713 - return __kernel_text_address(state->ip) ? state->ip : 0;
714 -}
715 -EXPORT_SYMBOL_GPL(unwind_get_return_address);
716 -
717 static size_t regs_size(struct pt_regs *regs)
718 {
719 /* x86_32 regs from kernel mode are two words shorter: */
720 diff --git a/arch/x86/kernel/unwind_guess.c b/arch/x86/kernel/unwind_guess.c
721 index 039f36738e49..4f0e17b90463 100644
722 --- a/arch/x86/kernel/unwind_guess.c
723 +++ b/arch/x86/kernel/unwind_guess.c
724 @@ -19,6 +19,11 @@ unsigned long unwind_get_return_address(struct unwind_state *state)
725 }
726 EXPORT_SYMBOL_GPL(unwind_get_return_address);
727
728 +unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
729 +{
730 + return NULL;
731 +}
732 +
733 bool unwind_next_frame(struct unwind_state *state)
734 {
735 struct stack_info *info = &state->stack_info;
736 diff --git a/arch/x86/kernel/unwind_orc.c b/arch/x86/kernel/unwind_orc.c
737 new file mode 100644
738 index 000000000000..570b70d3f604
739 --- /dev/null
740 +++ b/arch/x86/kernel/unwind_orc.c
741 @@ -0,0 +1,582 @@
742 +#include <linux/module.h>
743 +#include <linux/sort.h>
744 +#include <asm/ptrace.h>
745 +#include <asm/stacktrace.h>
746 +#include <asm/unwind.h>
747 +#include <asm/orc_types.h>
748 +#include <asm/orc_lookup.h>
749 +#include <asm/sections.h>
750 +
751 +#define orc_warn(fmt, ...) \
752 + printk_deferred_once(KERN_WARNING pr_fmt("WARNING: " fmt), ##__VA_ARGS__)
753 +
754 +extern int __start_orc_unwind_ip[];
755 +extern int __stop_orc_unwind_ip[];
756 +extern struct orc_entry __start_orc_unwind[];
757 +extern struct orc_entry __stop_orc_unwind[];
758 +
759 +static DEFINE_MUTEX(sort_mutex);
760 +int *cur_orc_ip_table = __start_orc_unwind_ip;
761 +struct orc_entry *cur_orc_table = __start_orc_unwind;
762 +
763 +unsigned int lookup_num_blocks;
764 +bool orc_init;
765 +
766 +static inline unsigned long orc_ip(const int *ip)
767 +{
768 + return (unsigned long)ip + *ip;
769 +}
770 +
771 +static struct orc_entry *__orc_find(int *ip_table, struct orc_entry *u_table,
772 + unsigned int num_entries, unsigned long ip)
773 +{
774 + int *first = ip_table;
775 + int *last = ip_table + num_entries - 1;
776 + int *mid = first, *found = first;
777 +
778 + if (!num_entries)
779 + return NULL;
780 +
781 + /*
782 + * Do a binary range search to find the rightmost duplicate of a given
783 + * starting address. Some entries are section terminators which are
784 + * "weak" entries for ensuring there are no gaps. They should be
785 + * ignored when they conflict with a real entry.
786 + */
787 + while (first <= last) {
788 + mid = first + ((last - first) / 2);
789 +
790 + if (orc_ip(mid) <= ip) {
791 + found = mid;
792 + first = mid + 1;
793 + } else
794 + last = mid - 1;
795 + }
796 +
797 + return u_table + (found - ip_table);
798 +}
799 +
800 +#ifdef CONFIG_MODULES
801 +static struct orc_entry *orc_module_find(unsigned long ip)
802 +{
803 + struct module *mod;
804 +
805 + mod = __module_address(ip);
806 + if (!mod || !mod->arch.orc_unwind || !mod->arch.orc_unwind_ip)
807 + return NULL;
808 + return __orc_find(mod->arch.orc_unwind_ip, mod->arch.orc_unwind,
809 + mod->arch.num_orcs, ip);
810 +}
811 +#else
812 +static struct orc_entry *orc_module_find(unsigned long ip)
813 +{
814 + return NULL;
815 +}
816 +#endif
817 +
818 +static struct orc_entry *orc_find(unsigned long ip)
819 +{
820 + if (!orc_init)
821 + return NULL;
822 +
823 + /* For non-init vmlinux addresses, use the fast lookup table: */
824 + if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) {
825 + unsigned int idx, start, stop;
826 +
827 + idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE;
828 +
829 + if (unlikely((idx >= lookup_num_blocks-1))) {
830 + orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%lx\n",
831 + idx, lookup_num_blocks, ip);
832 + return NULL;
833 + }
834 +
835 + start = orc_lookup[idx];
836 + stop = orc_lookup[idx + 1] + 1;
837 +
838 + if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) ||
839 + (__start_orc_unwind + stop > __stop_orc_unwind))) {
840 + orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%lx\n",
841 + idx, lookup_num_blocks, start, stop, ip);
842 + return NULL;
843 + }
844 +
845 + return __orc_find(__start_orc_unwind_ip + start,
846 + __start_orc_unwind + start, stop - start, ip);
847 + }
848 +
849 + /* vmlinux .init slow lookup: */
850 + if (ip >= (unsigned long)_sinittext && ip < (unsigned long)_einittext)
851 + return __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
852 + __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
853 +
854 + /* Module lookup: */
855 + return orc_module_find(ip);
856 +}
857 +
858 +static void orc_sort_swap(void *_a, void *_b, int size)
859 +{
860 + struct orc_entry *orc_a, *orc_b;
861 + struct orc_entry orc_tmp;
862 + int *a = _a, *b = _b, tmp;
863 + int delta = _b - _a;
864 +
865 + /* Swap the .orc_unwind_ip entries: */
866 + tmp = *a;
867 + *a = *b + delta;
868 + *b = tmp - delta;
869 +
870 + /* Swap the corresponding .orc_unwind entries: */
871 + orc_a = cur_orc_table + (a - cur_orc_ip_table);
872 + orc_b = cur_orc_table + (b - cur_orc_ip_table);
873 + orc_tmp = *orc_a;
874 + *orc_a = *orc_b;
875 + *orc_b = orc_tmp;
876 +}
877 +
878 +static int orc_sort_cmp(const void *_a, const void *_b)
879 +{
880 + struct orc_entry *orc_a;
881 + const int *a = _a, *b = _b;
882 + unsigned long a_val = orc_ip(a);
883 + unsigned long b_val = orc_ip(b);
884 +
885 + if (a_val > b_val)
886 + return 1;
887 + if (a_val < b_val)
888 + return -1;
889 +
890 + /*
891 + * The "weak" section terminator entries need to always be on the left
892 + * to ensure the lookup code skips them in favor of real entries.
893 + * These terminator entries exist to handle any gaps created by
894 + * whitelisted .o files which didn't get objtool generation.
895 + */
896 + orc_a = cur_orc_table + (a - cur_orc_ip_table);
897 + return orc_a->sp_reg == ORC_REG_UNDEFINED ? -1 : 1;
898 +}
899 +
900 +#ifdef CONFIG_MODULES
901 +void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size,
902 + void *_orc, size_t orc_size)
903 +{
904 + int *orc_ip = _orc_ip;
905 + struct orc_entry *orc = _orc;
906 + unsigned int num_entries = orc_ip_size / sizeof(int);
907 +
908 + WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
909 + orc_size % sizeof(*orc) != 0 ||
910 + num_entries != orc_size / sizeof(*orc));
911 +
912 + /*
913 + * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
914 + * associate an .orc_unwind_ip table entry with its corresponding
915 + * .orc_unwind entry so they can both be swapped.
916 + */
917 + mutex_lock(&sort_mutex);
918 + cur_orc_ip_table = orc_ip;
919 + cur_orc_table = orc;
920 + sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
921 + mutex_unlock(&sort_mutex);
922 +
923 + mod->arch.orc_unwind_ip = orc_ip;
924 + mod->arch.orc_unwind = orc;
925 + mod->arch.num_orcs = num_entries;
926 +}
927 +#endif
928 +
929 +void __init unwind_init(void)
930 +{
931 + size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip;
932 + size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind;
933 + size_t num_entries = orc_ip_size / sizeof(int);
934 + struct orc_entry *orc;
935 + int i;
936 +
937 + if (!num_entries || orc_ip_size % sizeof(int) != 0 ||
938 + orc_size % sizeof(struct orc_entry) != 0 ||
939 + num_entries != orc_size / sizeof(struct orc_entry)) {
940 + orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
941 + return;
942 + }
943 +
944 + /* Sort the .orc_unwind and .orc_unwind_ip tables: */
945 + sort(__start_orc_unwind_ip, num_entries, sizeof(int), orc_sort_cmp,
946 + orc_sort_swap);
947 +
948 + /* Initialize the fast lookup table: */
949 + lookup_num_blocks = orc_lookup_end - orc_lookup;
950 + for (i = 0; i < lookup_num_blocks-1; i++) {
951 + orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
952 + num_entries,
953 + LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
954 + if (!orc) {
955 + orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
956 + return;
957 + }
958 +
959 + orc_lookup[i] = orc - __start_orc_unwind;
960 + }
961 +
962 + /* Initialize the ending block: */
963 + orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries,
964 + LOOKUP_STOP_IP);
965 + if (!orc) {
966 + orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
967 + return;
968 + }
969 + orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
970 +
971 + orc_init = true;
972 +}
973 +
974 +unsigned long unwind_get_return_address(struct unwind_state *state)
975 +{
976 + if (unwind_done(state))
977 + return 0;
978 +
979 + return __kernel_text_address(state->ip) ? state->ip : 0;
980 +}
981 +EXPORT_SYMBOL_GPL(unwind_get_return_address);
982 +
983 +unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
984 +{
985 + if (unwind_done(state))
986 + return NULL;
987 +
988 + if (state->regs)
989 + return &state->regs->ip;
990 +
991 + if (state->sp)
992 + return (unsigned long *)state->sp - 1;
993 +
994 + return NULL;
995 +}
996 +
997 +static bool stack_access_ok(struct unwind_state *state, unsigned long addr,
998 + size_t len)
999 +{
1000 + struct stack_info *info = &state->stack_info;
1001 +
1002 + /*
1003 + * If the address isn't on the current stack, switch to the next one.
1004 + *
1005 + * We may have to traverse multiple stacks to deal with the possibility
1006 + * that info->next_sp could point to an empty stack and the address
1007 + * could be on a subsequent stack.
1008 + */
1009 + while (!on_stack(info, (void *)addr, len))
1010 + if (get_stack_info(info->next_sp, state->task, info,
1011 + &state->stack_mask))
1012 + return false;
1013 +
1014 + return true;
1015 +}
1016 +
1017 +static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
1018 + unsigned long *val)
1019 +{
1020 + if (!stack_access_ok(state, addr, sizeof(long)))
1021 + return false;
1022 +
1023 + *val = READ_ONCE_TASK_STACK(state->task, *(unsigned long *)addr);
1024 + return true;
1025 +}
1026 +
1027 +#define REGS_SIZE (sizeof(struct pt_regs))
1028 +#define SP_OFFSET (offsetof(struct pt_regs, sp))
1029 +#define IRET_REGS_SIZE (REGS_SIZE - offsetof(struct pt_regs, ip))
1030 +#define IRET_SP_OFFSET (SP_OFFSET - offsetof(struct pt_regs, ip))
1031 +
1032 +static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
1033 + unsigned long *ip, unsigned long *sp, bool full)
1034 +{
1035 + size_t regs_size = full ? REGS_SIZE : IRET_REGS_SIZE;
1036 + size_t sp_offset = full ? SP_OFFSET : IRET_SP_OFFSET;
1037 + struct pt_regs *regs = (struct pt_regs *)(addr + regs_size - REGS_SIZE);
1038 +
1039 + if (IS_ENABLED(CONFIG_X86_64)) {
1040 + if (!stack_access_ok(state, addr, regs_size))
1041 + return false;
1042 +
1043 + *ip = regs->ip;
1044 + *sp = regs->sp;
1045 +
1046 + return true;
1047 + }
1048 +
1049 + if (!stack_access_ok(state, addr, sp_offset))
1050 + return false;
1051 +
1052 + *ip = regs->ip;
1053 +
1054 + if (user_mode(regs)) {
1055 + if (!stack_access_ok(state, addr + sp_offset,
1056 + REGS_SIZE - SP_OFFSET))
1057 + return false;
1058 +
1059 + *sp = regs->sp;
1060 + } else
1061 + *sp = (unsigned long)&regs->sp;
1062 +
1063 + return true;
1064 +}
1065 +
1066 +bool unwind_next_frame(struct unwind_state *state)
1067 +{
1068 + unsigned long ip_p, sp, orig_ip, prev_sp = state->sp;
1069 + enum stack_type prev_type = state->stack_info.type;
1070 + struct orc_entry *orc;
1071 + struct pt_regs *ptregs;
1072 + bool indirect = false;
1073 +
1074 + if (unwind_done(state))
1075 + return false;
1076 +
1077 + /* Don't let modules unload while we're reading their ORC data. */
1078 + preempt_disable();
1079 +
1080 + /* Have we reached the end? */
1081 + if (state->regs && user_mode(state->regs))
1082 + goto done;
1083 +
1084 + /*
1085 + * Find the orc_entry associated with the text address.
1086 + *
1087 + * Decrement call return addresses by one so they work for sibling
1088 + * calls and calls to noreturn functions.
1089 + */
1090 + orc = orc_find(state->signal ? state->ip : state->ip - 1);
1091 + if (!orc || orc->sp_reg == ORC_REG_UNDEFINED)
1092 + goto done;
1093 + orig_ip = state->ip;
1094 +
1095 + /* Find the previous frame's stack: */
1096 + switch (orc->sp_reg) {
1097 + case ORC_REG_SP:
1098 + sp = state->sp + orc->sp_offset;
1099 + break;
1100 +
1101 + case ORC_REG_BP:
1102 + sp = state->bp + orc->sp_offset;
1103 + break;
1104 +
1105 + case ORC_REG_SP_INDIRECT:
1106 + sp = state->sp + orc->sp_offset;
1107 + indirect = true;
1108 + break;
1109 +
1110 + case ORC_REG_BP_INDIRECT:
1111 + sp = state->bp + orc->sp_offset;
1112 + indirect = true;
1113 + break;
1114 +
1115 + case ORC_REG_R10:
1116 + if (!state->regs || !state->full_regs) {
1117 + orc_warn("missing regs for base reg R10 at ip %p\n",
1118 + (void *)state->ip);
1119 + goto done;
1120 + }
1121 + sp = state->regs->r10;
1122 + break;
1123 +
1124 + case ORC_REG_R13:
1125 + if (!state->regs || !state->full_regs) {
1126 + orc_warn("missing regs for base reg R13 at ip %p\n",
1127 + (void *)state->ip);
1128 + goto done;
1129 + }
1130 + sp = state->regs->r13;
1131 + break;
1132 +
1133 + case ORC_REG_DI:
1134 + if (!state->regs || !state->full_regs) {
1135 + orc_warn("missing regs for base reg DI at ip %p\n",
1136 + (void *)state->ip);
1137 + goto done;
1138 + }
1139 + sp = state->regs->di;
1140 + break;
1141 +
1142 + case ORC_REG_DX:
1143 + if (!state->regs || !state->full_regs) {
1144 + orc_warn("missing regs for base reg DX at ip %p\n",
1145 + (void *)state->ip);
1146 + goto done;
1147 + }
1148 + sp = state->regs->dx;
1149 + break;
1150 +
1151 + default:
1152 + orc_warn("unknown SP base reg %d for ip %p\n",
1153 + orc->sp_reg, (void *)state->ip);
1154 + goto done;
1155 + }
1156 +
1157 + if (indirect) {
1158 + if (!deref_stack_reg(state, sp, &sp))
1159 + goto done;
1160 + }
1161 +
1162 + /* Find IP, SP and possibly regs: */
1163 + switch (orc->type) {
1164 + case ORC_TYPE_CALL:
1165 + ip_p = sp - sizeof(long);
1166 +
1167 + if (!deref_stack_reg(state, ip_p, &state->ip))
1168 + goto done;
1169 +
1170 + state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
1171 + state->ip, (void *)ip_p);
1172 +
1173 + state->sp = sp;
1174 + state->regs = NULL;
1175 + state->signal = false;
1176 + break;
1177 +
1178 + case ORC_TYPE_REGS:
1179 + if (!deref_stack_regs(state, sp, &state->ip, &state->sp, true)) {
1180 + orc_warn("can't dereference registers at %p for ip %p\n",
1181 + (void *)sp, (void *)orig_ip);
1182 + goto done;
1183 + }
1184 +
1185 + state->regs = (struct pt_regs *)sp;
1186 + state->full_regs = true;
1187 + state->signal = true;
1188 + break;
1189 +
1190 + case ORC_TYPE_REGS_IRET:
1191 + if (!deref_stack_regs(state, sp, &state->ip, &state->sp, false)) {
1192 + orc_warn("can't dereference iret registers at %p for ip %p\n",
1193 + (void *)sp, (void *)orig_ip);
1194 + goto done;
1195 + }
1196 +
1197 + ptregs = container_of((void *)sp, struct pt_regs, ip);
1198 + if ((unsigned long)ptregs >= prev_sp &&
1199 + on_stack(&state->stack_info, ptregs, REGS_SIZE)) {
1200 + state->regs = ptregs;
1201 + state->full_regs = false;
1202 + } else
1203 + state->regs = NULL;
1204 +
1205 + state->signal = true;
1206 + break;
1207 +
1208 + default:
1209 + orc_warn("unknown .orc_unwind entry type %d\n", orc->type);
1210 + break;
1211 + }
1212 +
1213 + /* Find BP: */
1214 + switch (orc->bp_reg) {
1215 + case ORC_REG_UNDEFINED:
1216 + if (state->regs && state->full_regs)
1217 + state->bp = state->regs->bp;
1218 + break;
1219 +
1220 + case ORC_REG_PREV_SP:
1221 + if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
1222 + goto done;
1223 + break;
1224 +
1225 + case ORC_REG_BP:
1226 + if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
1227 + goto done;
1228 + break;
1229 +
1230 + default:
1231 + orc_warn("unknown BP base reg %d for ip %p\n",
1232 + orc->bp_reg, (void *)orig_ip);
1233 + goto done;
1234 + }
1235 +
1236 + /* Prevent a recursive loop due to bad ORC data: */
1237 + if (state->stack_info.type == prev_type &&
1238 + on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) &&
1239 + state->sp <= prev_sp) {
1240 + orc_warn("stack going in the wrong direction? ip=%p\n",
1241 + (void *)orig_ip);
1242 + goto done;
1243 + }
1244 +
1245 + preempt_enable();
1246 + return true;
1247 +
1248 +done:
1249 + preempt_enable();
1250 + state->stack_info.type = STACK_TYPE_UNKNOWN;
1251 + return false;
1252 +}
1253 +EXPORT_SYMBOL_GPL(unwind_next_frame);
1254 +
1255 +void __unwind_start(struct unwind_state *state, struct task_struct *task,
1256 + struct pt_regs *regs, unsigned long *first_frame)
1257 +{
1258 + memset(state, 0, sizeof(*state));
1259 + state->task = task;
1260 +
1261 + /*
1262 + * Refuse to unwind the stack of a task while it's executing on another
1263 + * CPU. This check is racy, but that's ok: the unwinder has other
1264 + * checks to prevent it from going off the rails.
1265 + */
1266 + if (task_on_another_cpu(task))
1267 + goto done;
1268 +
1269 + if (regs) {
1270 + if (user_mode(regs))
1271 + goto done;
1272 +
1273 + state->ip = regs->ip;
1274 + state->sp = kernel_stack_pointer(regs);
1275 + state->bp = regs->bp;
1276 + state->regs = regs;
1277 + state->full_regs = true;
1278 + state->signal = true;
1279 +
1280 + } else if (task == current) {
1281 + asm volatile("lea (%%rip), %0\n\t"
1282 + "mov %%rsp, %1\n\t"
1283 + "mov %%rbp, %2\n\t"
1284 + : "=r" (state->ip), "=r" (state->sp),
1285 + "=r" (state->bp));
1286 +
1287 + } else {
1288 + struct inactive_task_frame *frame = (void *)task->thread.sp;
1289 +
1290 + state->sp = task->thread.sp;
1291 + state->bp = READ_ONCE_NOCHECK(frame->bp);
1292 + state->ip = READ_ONCE_NOCHECK(frame->ret_addr);
1293 + }
1294 +
1295 + if (get_stack_info((unsigned long *)state->sp, state->task,
1296 + &state->stack_info, &state->stack_mask))
1297 + return;
1298 +
1299 + /*
1300 + * The caller can provide the address of the first frame directly
1301 + * (first_frame) or indirectly (regs->sp) to indicate which stack frame
1302 + * to start unwinding at. Skip ahead until we reach it.
1303 + */
1304 +
1305 + /* When starting from regs, skip the regs frame: */
1306 + if (regs) {
1307 + unwind_next_frame(state);
1308 + return;
1309 + }
1310 +
1311 + /* Otherwise, skip ahead to the user-specified starting frame: */
1312 + while (!unwind_done(state) &&
1313 + (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
1314 + state->sp <= (unsigned long)first_frame))
1315 + unwind_next_frame(state);
1316 +
1317 + return;
1318 +
1319 +done:
1320 + state->stack_info.type = STACK_TYPE_UNKNOWN;
1321 + return;
1322 +}
1323 +EXPORT_SYMBOL_GPL(__unwind_start);
1324 diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
1325 index 658fcf67862c..d6f45f6d1054 100644
1326 --- a/arch/x86/Kconfig
1327 +++ b/arch/x86/Kconfig
1328 @@ -158,6 +158,7 @@ config X86
1329 select HAVE_MEMBLOCK
1330 select HAVE_MEMBLOCK_NODE_MAP
1331 select HAVE_MIXED_BREAKPOINTS_REGS
1332 + select HAVE_MOD_ARCH_SPECIFIC
1333 select HAVE_NMI
1334 select HAVE_OPROFILE
1335 select HAVE_OPTPROBES
1336 diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug
1337 index 1fc519f3c49e..d5bca2ec8a74 100644
1338 --- a/arch/x86/Kconfig.debug
1339 +++ b/arch/x86/Kconfig.debug
1340 @@ -356,4 +356,29 @@ config PUNIT_ATOM_DEBUG
1341 The current power state can be read from
1342 /sys/kernel/debug/punit_atom/dev_power_state
1343
1344 +config ORC_UNWINDER
1345 + bool "ORC unwinder"
1346 + depends on X86_64
1347 + select STACK_VALIDATION
1348 + ---help---
1349 + This option enables the ORC (Oops Rewind Capability) unwinder for
1350 + unwinding kernel stack traces. It uses a custom data format which is
1351 + a simplified version of the DWARF Call Frame Information standard.
1352 +
1353 + This unwinder is more accurate across interrupt entry frames than the
1354 + frame pointer unwinder. It can also enable a 5-10% performance
1355 + improvement across the entire kernel if CONFIG_FRAME_POINTER is
1356 + disabled.
1357 +
1358 + Enabling this option will increase the kernel's runtime memory usage
1359 + by roughly 2-4MB, depending on your kernel config.
1360 +
1361 +config FRAME_POINTER_UNWINDER
1362 + def_bool y
1363 + depends on !ORC_UNWINDER && FRAME_POINTER
1364 +
1365 +config GUESS_UNWINDER
1366 + def_bool y
1367 + depends on !ORC_UNWINDER && !FRAME_POINTER
1368 +
1369 endmenu
1370 diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
1371 index c8a3b61be0aa..f05f00acac89 100644
1372 --- a/arch/x86/kernel/vmlinux.lds.S
1373 +++ b/arch/x86/kernel/vmlinux.lds.S
1374 @@ -24,6 +24,7 @@
1375 #include <asm/asm-offsets.h>
1376 #include <asm/thread_info.h>
1377 #include <asm/page_types.h>
1378 +#include <asm/orc_lookup.h>
1379 #include <asm/cache.h>
1380 #include <asm/boot.h>
1381
1382 @@ -148,6 +149,8 @@ SECTIONS
1383
1384 BUG_TABLE
1385
1386 + ORC_UNWIND_TABLE
1387 +
1388 . = ALIGN(PAGE_SIZE);
1389 __vvar_page = .;
1390
1391 diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
1392 index c617b9d1d6cb..0b4d1b3880b0 100644
1393 --- a/lib/Kconfig.debug
1394 +++ b/lib/Kconfig.debug
1395 @@ -374,6 +374,9 @@ config STACK_VALIDATION
1396 pointers (if CONFIG_FRAME_POINTER is enabled). This helps ensure
1397 that runtime stack traces are more reliable.
1398
1399 + This is also a prerequisite for generation of ORC unwind data, which
1400 + is needed for CONFIG_ORC_UNWINDER.
1401 +
1402 For more information, see
1403 tools/objtool/Documentation/stack-validation.txt.
1404
1405 --
1406 2.14.2
1407