mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Changwoo Min <changwoo@igalia.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>, Shuah Khan <shuah@kernel.org>,
	kernel-dev@igalia.com, bpf <bpf@vger.kernel.org>,
	sched-ext@lists.linux.dev, LKML <linux-kernel@vger.kernel.org>,
	"open list:KERNEL SELFTEST FRAMEWORK"
	<linux-kselftest@vger.kernel.org>
Subject: Re: [PATCH bpf-next v1 1/2] bpf: Introduce execution context detection kfuncs
Date: Sat, 24 Jan 2026 20:29:20 +0900	[thread overview]
Message-ID: <933f94c8-d74c-4d01-8e55-d8dab3d79f61@igalia.com> (raw)
In-Reply-To: <CAADnVQJTaYo0=aaTii6n+yXLuacLcc9XSj=PLgbq+-mn5Num9Q@mail.gmail.com>

Hi Alexei,

Thank you for the feedback.

On 1/24/26 1:37 AM, Alexei Starovoitov wrote:
> On Fri, Jan 23, 2026 at 12:43 AM Changwoo Min <changwoo@igalia.com> wrote:
>>
>> Introduce bpf_in_nmi(), bpf_in_hardirq(), bpf_in_serving_softirq(), and
>> bpf_in_task() kfuncs to allow BPF programs to query the current execution
>> context.
>>
>> While BPF programs can sometimes infer context based on the attach point,
>> certain programs (such as those in sched_ext) may be called from multiple
>> contexts. These kfuncs provide a reliable way for logic to branch based on
>> whether the CPU is currently handling an interrupt or executing in task
>> context.
>>
>> For example, this is particularly useful for sched_ext schedulers that need
>> to differentiate between task-to-task wake-ups and interrupt-to-task
>> wake-ups.
>>
>> As the names imply, these helpers wrap the kernel's internal in_nmi(),
>> in_hardirq(), in_serving_softirq(), and in_task() macros.
>>
>> Signed-off-by: Changwoo Min <changwoo@igalia.com>
>> ---
>>   kernel/bpf/helpers.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 44 insertions(+)
>>
>> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
>> index 637677815365..cb36bc7a80c6 100644
>> --- a/kernel/bpf/helpers.c
>> +++ b/kernel/bpf/helpers.c
>> @@ -4365,6 +4365,46 @@ __bpf_kfunc int bpf_dynptr_file_discard(struct bpf_dynptr *dynptr)
>>          return 0;
>>   }
>>
>> +/**
>> + * bpf_in_nmi - Test if the current execution context is in NMI context.
>> + *
>> + * Return: true if we are in NMI context, false otherwise.
>> + */
>> +__bpf_kfunc bool bpf_in_nmi(void)
>> +{
>> +       return in_nmi();
>> +}
>> +
>> +/**
>> + * bpf_in_hardirq - Test if the current execution context is in hard IRQ context.
>> + *
>> + * Return: true if we are in hard IRQ context, false otherwise.
>> + */
>> +__bpf_kfunc bool bpf_in_hardirq(void)
>> +{
>> +       return in_hardirq();
>> +}
>> +
>> +/**
>> + * bpf_in_serving_softirq - Test if the current execution context is in softirq context.
>> + *
>> + * Return: true if we are in softirq context, false otherwise.
>> + */
>> +__bpf_kfunc bool bpf_in_serving_softirq(void)
>> +{
>> +       return in_serving_softirq();
>> +}
>> +
>> +/**
>> + * bpf_in_task - Test if the current execution context is in task context.
>> + *
>> + * Return: true if we are in task context, false otherwise.
>> + */
>> +__bpf_kfunc bool bpf_in_task(void)
>> +{
>> +       return in_task();
>> +}
>> +
> 
> This functionality is already available.
> See bpf_in_interrupt() and get_preempt_count() in bpf_experimental.h.
> 
> No need to replicate that as kfuncs.

I see. While bpf_in_interrupt() provides a general check, certain use
cases in sched_ext require more granular differentiation -- specifically
between hardirq and softirq contexts to handle different wake-up paths
correctly.

> If something can be implemented as a bpf program it should stay as
> pure bpf code, since it's more flexible and faster this way.
> llvm together with JITs completely inline get_preempt_count().
> 

I agree. Implementing these checks as pure BPF code is more efficient
due to JIT inlining. I was initially hesitant to duplicate kernel-side
macros in BPF, but since the precedent is already established in
bpf_experimental.h, I will follow that pattern.

I will drop the kfunc implementations in Patch 1 and instead add
bpf_in_nmi(), bpf_in_hardirq(), bpf_in_serving_softirq(), and
bpf_in_task() to bpf_experimental.h by wrapping get_preempt_count().

I will also update the selftests in Patch 2 to use these new headers and
submit a v2 shortly.

Regards,
Changwoo Min

  reply	other threads:[~2026-01-24 11:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-23  8:42 [PATCH bpf-next v1 0/2] " Changwoo Min
2026-01-23  8:42 ` [PATCH bpf-next v1 1/2] " Changwoo Min
2026-01-23 16:37   ` Alexei Starovoitov
2026-01-24 11:29     ` Changwoo Min [this message]
2026-01-23  8:42 ` [PATCH bpf-next v1 2/2] selftests/bpf: Add tests for execution context kfuncs Changwoo Min
2026-01-23  9:08   ` bot+bpf-ci

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=933f94c8-d74c-4d01-8e55-d8dab3d79f61@igalia.com \
    --to=changwoo@igalia.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kernel-dev@igalia.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sched-ext@lists.linux.dev \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --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

all inboxes | Powered by JetHome®