lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAADnVQJTaYo0=aaTii6n+yXLuacLcc9XSj=PLgbq+-mn5Num9Q@mail.gmail.com>
Date: Fri, 23 Jan 2026 08:37:57 -0800
From: Alexei Starovoitov <alexei.starovoitov@...il.com>
To: Changwoo Min <changwoo@...lia.com>
Cc: Alexei Starovoitov <ast@...nel.org>, Daniel Borkmann <daniel@...earbox.net>, 
	Andrii Nakryiko <andrii@...nel.org>, Martin KaFai Lau <martin.lau@...ux.dev>, 
	Eduard Zingerman <eddyz87@...il.com>, Song Liu <song@...nel.org>, 
	Yonghong Song <yonghong.song@...ux.dev>, John Fastabend <john.fastabend@...il.com>, 
	KP Singh <kpsingh@...nel.org>, Stanislav Fomichev <sdf@...ichev.me>, Hao Luo <haoluo@...gle.com>, 
	Jiri Olsa <jolsa@...nel.org>, Shuah Khan <shuah@...nel.org>, kernel-dev@...lia.com, 
	bpf <bpf@...r.kernel.org>, sched-ext@...ts.linux.dev, 
	LKML <linux-kernel@...r.kernel.org>, 
	"open list:KERNEL SELFTEST FRAMEWORK" <linux-kselftest@...r.kernel.org>
Subject: Re: [PATCH bpf-next v1 1/2] bpf: Introduce execution context
 detection kfuncs

On Fri, Jan 23, 2026 at 12:43 AM Changwoo Min <changwoo@...lia.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@...lia.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.
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().

pw-bot: cr

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ