BPF List
 help / color / mirror / Atom feed
From: Leon Hwang <leon.hwang@linux.dev>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net,
	martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org,
	yonghong.song@linux.dev, kernel-patches-bot@fb.com,
	Leon Hwang <leon.hwang@linux.dev>
Subject: [PATCH bpf-next v3 0/2] selftests/bpf: Introduce experimental bpf_in_interrupt()
Date: Wed,  3 Sep 2025 22:04:36 +0800	[thread overview]
Message-ID: <20250903140438.59517-1-leon.hwang@linux.dev> (raw)

Filtering pid_tgid is meanlingless when the current task is preempted by
an interrupt.

To address this, introduce 'bpf_in_interrupt()' helper function, which
allows BPF programs to determine whether they are executing in interrupt
context.

'get_preempt_count()':

* On x86, '*(int *) bpf_this_cpu_ptr(&__preempt_count)'.
* On arm64, 'bpf_get_current_task_btf()->thread_info.preempt.count'.

Then 'bpf_in_interrupt()' will be:

* If !PREEMPT_RT, 'get_preempt_count() & (NMI_MASK | HARDIRQ_MASK
  | SOFTIRQ_MASK)'.
* If PREEMPT_RT, '(get_preempt_count() & (NMI_MASK | HARDIRQ_MASK))
  | (bpf_get_current_task_btf()->softirq_disable_cnt & SOFTIRQ_MASK)'.

'bpf_in_interrupt()' runs well when PREEMPT_RT is enabled. But it's
difficult for me to test it well because I'm not familiar with
PREEMPT_RT.

Changes:
v2 -> v3:
* Address comments from Alexei:
  * Move bpf_in_interrupt() to bpf_experimental.h.
  * Add support for arm64.
v2: https://lore.kernel.org/bpf/20250825131502.54269-1-leon.hwang@linux.dev/

v1 -> v2:
* Fix a build error reported by test bot.

Leon Hwang (2):
  selftests/bpf: Introduce experimental bpf_in_interrupt()
  selftests/bpf: Add case to test bpf_in_interrupt()

 .../testing/selftests/bpf/bpf_experimental.h  | 54 +++++++++++++++++++
 .../testing/selftests/bpf/prog_tests/timer.c  | 30 +++++++++++
 .../selftests/bpf/progs/timer_interrupt.c     | 48 +++++++++++++++++
 3 files changed, 132 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/timer_interrupt.c

--
2.51.0


             reply	other threads:[~2025-09-03 14:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-03 14:04 Leon Hwang [this message]
2025-09-03 14:04 ` [PATCH bpf-next v3 1/2] selftests/bpf: Introduce experimental bpf_in_interrupt() Leon Hwang
2025-09-03 14:04 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add case to test bpf_in_interrupt() Leon Hwang

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=20250903140438.59517-1-leon.hwang@linux.dev \
    --to=leon.hwang@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kernel-patches-bot@fb.com \
    --cc=martin.lau@linux.dev \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox