From: Douglas Anderson <dianders@chromium.org> To: Catalin Marinas <catalin.marinas@arm.com>, Will Deacon <will@kernel.org> Cc: Nick Desaulniers <ndesaulniers@google.com>, Seth LaForge <sethml@google.com>, Ricky Liang <jcliang@chromium.org>, Douglas Anderson <dianders@chromium.org>, Alexander Shishkin <alexander.shishkin@linux.intel.com>, Arnaldo Carvalho de Melo <acme@kernel.org>, Ingo Molnar <mingo@redhat.com>, Jiri Olsa <jolsa@redhat.com>, Mark Rutland <mark.rutland@arm.com>, Namhyung Kim <namhyung@kernel.org>, Nathan Chancellor <nathan@kernel.org>, Peter Zijlstra <peterz@infradead.org>, clang-built-linux@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 2/3] arm64: perf: Improve compat perf_callchain_user() for clang leaf functions Date: Fri, 7 May 2021 13:55:12 -0700 [thread overview] Message-ID: <20210507135509.2.Ib54050e4091679cc31b04d52d7ef200f99faaae5@changeid> (raw) In-Reply-To: <20210507205513.640780-1-dianders@chromium.org> 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@chromium.org> --- 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
WARNING: multiple messages have this Message-ID (diff)
From: Douglas Anderson <dianders@chromium.org> To: Catalin Marinas <catalin.marinas@arm.com>, Will Deacon <will@kernel.org> Cc: Nick Desaulniers <ndesaulniers@google.com>, Seth LaForge <sethml@google.com>, Ricky Liang <jcliang@chromium.org>, Douglas Anderson <dianders@chromium.org>, Alexander Shishkin <alexander.shishkin@linux.intel.com>, Arnaldo Carvalho de Melo <acme@kernel.org>, Ingo Molnar <mingo@redhat.com>, Jiri Olsa <jolsa@redhat.com>, Mark Rutland <mark.rutland@arm.com>, Namhyung Kim <namhyung@kernel.org>, Nathan Chancellor <nathan@kernel.org>, Peter Zijlstra <peterz@infradead.org>, clang-built-linux@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 2/3] arm64: perf: Improve compat perf_callchain_user() for clang leaf functions Date: Fri, 7 May 2021 13:55:12 -0700 [thread overview] Message-ID: <20210507135509.2.Ib54050e4091679cc31b04d52d7ef200f99faaae5@changeid> (raw) In-Reply-To: <20210507205513.640780-1-dianders@chromium.org> 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@chromium.org> --- 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 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2021-05-07 20:55 UTC|newest] Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-05-07 20:55 [PATCH 0/3] arm64: perf: Make compat tracing better Douglas Anderson 2021-05-07 20:55 ` Douglas Anderson 2021-05-07 20:55 ` [PATCH 1/3] arm64: perf: perf_callchain_user() compat support for clang/non-APCS-gcc-arm Douglas Anderson 2021-05-07 20:55 ` Douglas Anderson 2021-05-07 20:55 ` Douglas Anderson [this message] 2021-05-07 20:55 ` [PATCH 2/3] arm64: perf: Improve compat perf_callchain_user() for clang leaf functions Douglas Anderson 2021-06-07 9:14 ` James Clark 2021-06-07 9:14 ` James Clark 2021-06-07 20:57 ` Doug Anderson 2021-06-07 20:57 ` Doug Anderson 2021-05-07 20:55 ` [PATCH 3/3] arm64: perf: Add a config option saying 32-bit thumb code uses R11 for FP Douglas Anderson 2021-05-07 20:55 ` Douglas Anderson 2021-05-25 15:04 ` [PATCH 0/3] arm64: perf: Make compat tracing better Doug Anderson 2021-05-25 15:04 ` Doug Anderson 2021-06-02 17:55 ` Will Deacon 2021-06-02 17:55 ` Will Deacon 2021-06-07 20:34 ` Doug Anderson 2021-06-07 20:34 ` Doug Anderson
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20210507135509.2.Ib54050e4091679cc31b04d52d7ef200f99faaae5@changeid \ --to=dianders@chromium.org \ --cc=acme@kernel.org \ --cc=alexander.shishkin@linux.intel.com \ --cc=catalin.marinas@arm.com \ --cc=clang-built-linux@googlegroups.com \ --cc=jcliang@chromium.org \ --cc=jolsa@redhat.com \ --cc=linux-arm-kernel@lists.infradead.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-perf-users@vger.kernel.org \ --cc=mark.rutland@arm.com \ --cc=mingo@redhat.com \ --cc=namhyung@kernel.org \ --cc=nathan@kernel.org \ --cc=ndesaulniers@google.com \ --cc=peterz@infradead.org \ --cc=sethml@google.com \ --cc=will@kernel.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.