Skip to contentSkip to conversation

Documentation: livepatch: document reliable stacktrace

PATCH In review Mark Rutland 3 participants 3 messages Oct 23, 2020 – Oct 29, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Documentation: livepatch: document reliable stacktrace

Add documentation for reliable stacktrace. This is intended to describe the semantics and to be an aid for implementing architecture support for HAVE_RELIABLE_STACKTRACE.

Unwinding is a subtle area, and architectures vary greatly in both implementation and the set of concerns that affect them, so I've tried to avoid making this too specific to any given architecture. I've used examples from both x86_64 and arm64 to explain corner cases in more detail, but I've tried to keep the descriptions sufficient for those who are unfamiliar with the particular architecture.

I've tried to give rationale for all the recommendations/requirements, since that makes it easier to spot nearby issues, or when a check happens to catch a few things at once. I believe what I have written is sound, but as some of this was reverse-engineered I may have missed things worth noting.

I've made a few assumptions about preferred behaviour, notably:

* If you can reliably unwind through exceptions, you should (as x86_64 does).

* It's fine to omit ftrace_return_to_handler and other return trampolines so long as these are not subject to patching and the original return address is reported. Most architectures do this for ftrace_return_handler, but not other return trampolines.

* For cases where link register unreliability could result in duplicate entries in the trace or an inverted trace, I've assumed this should be treated as unreliable. This specific case shouldn't matter to livepatching, but I assume that that we want a reliable trace to have the correct order.

Cc Jiri Kosina <[email protected]>
Cc Joe Lawrence <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Mark Brown <[email protected]>
Cc Miroslav Benes <[email protected]>
Cc Petr Mladek <[email protected]>
---
 Documentation/livepatch/index.rst               |   1 +
 Documentation/livepatch/reliable-stacktrace.rst | 303 ++++++++++++++++++++++++
 2 files changed, 304 insertions(+)
 create mode 100644 Documentation/livepatch/reliable-stacktrace.rst
Documentation/livepatch/index.rst +1 -0
index 525944063be7a..43cce5fad705f 100644
@@ -13,6 +13,7 @@ Kernel Livepatching
     module-elf-format
     shadow-vars
     system-state
+    reliable-stacktrace
 
 .. only::  subproject and html
 
Documentation/livepatch/reliable-stacktrace.rst new file +303 -0

303 lines not on this page · raw message

Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 3 lines · Mark Rutland

Add documentation for reliable stacktrace. This is intended to describe the semantics and to be an aid for implementing architecture support for HAVE_RELIABLE_STACKTRACE.

First, thanks a lot for putting this document together.

I am not expert on stack unwinders and am not sure if some details should get corrected and added. I believe that it can be done by others more effectively.

Anyway, the document is well readable and provides a lot of useful information. I suggest only small change in the style, see below.

Quoted 20 lines
Documentation/livepatch/reliable-stacktrace.rst new file +15 -0
new file mode 100644
index 0000000000000..d296c93f6f0e0
+2. Requirements
+===============
+
+Architectures must implement one of the reliable stacktrace functions.
+Architectures using CONFIG_ARCH_STACKWALK should implement
+'arch_stack_walk_reliable', and other architectures should implement
+'save_stack_trace_tsk_reliable'.
+
+Principally, the reliable stacktrace function must ensure that either:
+
+* The trace includes all functions that the task may be returned to, and the
+  return code is zero to indicate that the trace is reliable.
+
+* The return code is non-zero to indicate that the trace is not reliable.
+

Documentation/livepatch/reliable-stacktrace.rst line 57

Quoted 10 lines
Documentation/livepatch/reliable-stacktrace.rst new file +10 -0
+.. note::
+   In some cases it is legitimate to omit specific functions from the trace,
+   but all other functions must be reported. These cases are described in
+   futher detail below.
+
+Secondly, the reliable stacktrace function should be robust to cases where the
+stack or other unwind state is corrupt or otherwise unreliable. The function
+should attempt to detect such cases and return a non-zero error code, and
+should not get stuck in an infinite loop or access memory in an unsafe way.
+Specific cases are described in further detail below.

Please, use imperative style when something is required for the reliability. For example, it means replacing all "should" with "must" in the above paragraph.

I perfectly understand why you used "should". I use it heavily as well. But we really must motivate people to handle all corner cases here. ;-)

Best Regards,
Petr

maintainer
#2 lore raw
In reply to Mark Rutland

Hi,

Quoted 3 lines · Mark Rutland

Add documentation for reliable stacktrace. This is intended to describe the semantics and to be an aid for implementing architecture support for HAVE_RELIABLE_STACKTRACE.

thanks a lot for doing the work!

Quoted 6 lines

Unwinding is a subtle area, and architectures vary greatly in both implementation and the set of concerns that affect them, so I've tried to avoid making this too specific to any given architecture. I've used examples from both x86_64 and arm64 to explain corner cases in more detail, but I've tried to keep the descriptions sufficient for those who are unfamiliar with the particular architecture.

Yes, I think it is a good approach. We can always add more details later, but it would probably cause more confusion for those unfamiliar.

Quoted 10 lines

I've tried to give rationale for all the recommendations/requirements, since that makes it easier to spot nearby issues, or when a check happens to catch a few things at once. I believe what I have written is sound, but as some of this was reverse-engineered I may have missed things worth noting.

I've made a few assumptions about preferred behaviour, notably:

* If you can reliably unwind through exceptions, you should (as x86_64 does).

Yes, it does. I think (and Josh will correct me if I am wrong here), that even at the beginning the intention was to improve the reliability of unwinding in general. Both x86_64 and s390x are the case. _reliable() interface only takes an advantage of that. As you pointed out in the document, unwinding through exceptions is not necessary. It can be reported as unreliable and we can deal with that later. But it is always better to do it if possible.

powerpc is an exception to the approach, because it implements its _reliable() API from the scratch.

Quoted 4 lines

* It's fine to omit ftrace_return_to_handler and other return trampolines so long as these are not subject to patching and the original return address is reported. Most architectures do this for ftrace_return_handler, but not other return trampolines.

Yes. Patching a trampoline is not something I can imagine, so that should not be a problem. But one never knows and we may run into a problem here easily. I don't remember if we even audited all the trampolines. And new ones are introduced all the time.

Quoted 5 lines

* For cases where link register unreliability could result in duplicate entries in the trace or an inverted trace, I've assumed this should be treated as unreliable. This specific case shouldn't matter to livepatching, but I assume that that we want a reliable trace to have the correct order.

Agreed.

Thanks
Miroslav