[PATCH 2/3] arm64: perf: Improve compat perf_callchain_user() for clang leaf functions

From: Douglas Anderson
Date: Fri May 07 2021 - 16:55:48 EST


It turns out that even when you compile code with clang with
"-fno-omit-frame-pointer" that it won't generate a frame pointer for
leaf functions (those that don't call any sub-functions). Presumably
clang does this to reduce the overhead of frame pointers. In a leaf
function you don't really need frame pointers since the Link Register
(LR) is guaranteed to always point to the caller.

Clang's optimization here is a bit of a pain for us, though. A human
might have an easy time figuring out if a function is a leaf function
or not and in theory we could have a way to lookup a given PC to find
out if it's in a leaf function. Unfortunately we haven't passed the
Turing test and we don't have any auxiliary data to help us.

If we just ignore this problem then the end result isn't terrible. It
just turns out that the _callers_ of leaf functions won't be logged. I
guess that's OK, but it could lead to some confusing traces.

Another option (the one proposed in this patch) is to always log the
first LR when we're tracing, assuming that we hadn't already decided
to log it for some other reason.

Good things about always logging the LR:
* clang leaf functions are handled better.
* if PC is right at the start of a function (even on non-clang) it's
handled better.

Bad things about the LR:
* We could log a "bogus" PC in the trace.

I believe that the most common "bogus" PC that would be logged would
be a PC somewhere in the top function being traced. As an example, if
we have this function:

non_leaf():
1. Setup the frame pointer
2. Call example()
3. Do something slow
4. Do something else slow
5. Call example2()
6. Return

If the PC was in the middle of "Do something else slow" and then we
tried to trace, our stack trace would look like this:
Top: a) A PC in the middle of "Do something else slow".
b) The return address that example() used, probably in "Do
something slow"
c) The caller of non_leaf()

Specifically you can see that there would be two PCs logged for
non_leaf().

To me it feels like this is a net-win. It also should be noted that
the consumer of our trace records probably _does_ have more
information than we do. It could fairly easily ignore this info.

Signed-off-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
---

arch/arm64/kernel/perf_callchain.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/arch/arm64/kernel/perf_callchain.c b/arch/arm64/kernel/perf_callchain.c
index e5ce5f7965d1..b3cd9f371469 100644
--- a/arch/arm64/kernel/perf_callchain.c
+++ b/arch/arm64/kernel/perf_callchain.c
@@ -326,6 +326,20 @@ static void compat_perf_callchain_user(struct perf_callchain_entry_ctx *entry,
while ((entry->nr < entry->max_stack) && fp && !(fp & 0x3)) {
err = compat_perf_trace_1(&fp, &pc, leaf_lr);

+ /*
+ * If this is the first trace and it didn't find the LR then
+ * let's throw it in the trace first. This isn't perfect but
+ * is the best we can do for handling clang leaf functions (or
+ * the case where we're right at the start of the function
+ * before the new frame has been pushed). In the worst case
+ * this can cause us to throw an extra entry that will be some
+ * location in the same function as the PC. That's not
+ * amazing but shouldn't really hurt. It seems better than
+ * throwing away the LR.
+ */
+ if (leaf_lr && leaf_lr != pc)
+ perf_callchain_store(entry, leaf_lr & ~BIT(0));
+
/* Bail out on any type of error */
if (err)
break;
--
2.31.1.607.g51e8a6a459-goog