All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ben Gamari <ben@smart-cactus.org>
To: bpf@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: Sampling of non-C-like stacks with eBPF and perf_events?
Date: Fri, 17 Dec 2021 11:45:32 -0500	[thread overview]
Message-ID: <87o85ftc3p.fsf@smart-cactus.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 2184 bytes --]

Hi all,

I have recently been exploring the possibility of using a
BPF_PROG_TYPE_PERF_EVENT program to implement stack sampling for
languages which do not use the platform's %sp for their stack pointer
(in my case, GHC/Haskell [1], which on x86-64 uses %rbp for its stack
pointer). Specifically, the idea is to use a sampling perf_events
session with an eBPF overflow handler which locates the
currently-running thread's stack and records it in the sample ringbuffer
(see [2] for my current attempt). At this point I only care about
user-space samples.

However, I quickly ran up against the fact that perf_event's stack
sampling logic (namely perf_output_sample_ustack) is called from an IRQ
context. This appears to preclude use of a sleepable BPF program, which
would be necessary to use bpf_copy_from_user. Indeed, the fact that the
usual stack sampling logic uses copy_from_user_inatomic rather than
copy_from_user suggests that this isn't a safe context for sleeping.

So, I'm at this point a bit unclear on how to proceed. I can see a few
possible directions forward, although none are particularly enticing:

* Add a bpf_copy_from_user_atomic helper, which can be called from a
  non-sleepable context like a perf_events overflow handler. This would
  take the same set_fs() and pagefault_disable() precautions as
  perf_output_sample_ustack to ensure that the access is safe and aborts
  on fault.

* Introduce a new BPF program type,
  BPF_PROG_TYPE_PERF_EVENT_STACK_LOCATOR, which can be invoked by
  perf_output_sample_ustack to locate the stack to be sampled.

Do either of these ideas sound upstreamable? Perhaps there are other
ideas on how to attack this general problem? I do not believe Haskell is
alone in its struggle with the current inflexibility of stack sampling;
the JVM introduced framepointer support specifically to allow callgraph
sampling; however, dedicating a register and code to this seems like an
unfortunate compromise, especially on x86-64 where registers are already
fairly precious.

Any thoughts or suggestions would be greatly appreciated.

Cheers,

- Ben


[1] https://www.haskell.org/ghc/
[2] https://gitlab.haskell.org/bgamari/hs-bpf-prof/

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

             reply	other threads:[~2021-12-17 16:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-17 16:45 Ben Gamari [this message]
2022-01-22  0:04 ` Sampling of non-C-like stacks with eBPF and perf_events? Alexei Starovoitov
2022-01-23 18:45   ` Ian Rogers
2022-01-24 13:48     ` Gabriele

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=87o85ftc3p.fsf@smart-cactus.org \
    --to=ben@smart-cactus.org \
    --cc=bpf@vger.kernel.org \
    --cc=linux-perf-users@vger.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: link
Be 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.