0035-x86-unwind-Add-the-ORC-unwinder.patch 43 KB

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