Skip to content

Commit b3783b1

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
bpf: Introduce bpf_in_interrupt kfunc
Filtering pid_tgid is meaningless when the current task is preempted by an interrupt. To address this, introduce the bpf_in_interrupt kfunc, which allows BPF programs to determine whether they are executing in interrupt context. This enables programs to avoid applying pid_tgid filtering when running in such contexts. Signed-off-by: Leon Hwang <[email protected]>
1 parent b62323c commit b3783b1

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

kernel/bpf/helpers.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3714,6 +3714,14 @@ __bpf_kfunc int bpf_strstr(const char *s1__ign, const char *s2__ign)
37143714
return bpf_strnstr(s1__ign, s2__ign, XATTR_SIZE_MAX);
37153715
}
37163716

3717+
/**
3718+
* bpf_in_interrupt - Check whether it's in interrupt context
3719+
*/
3720+
__bpf_kfunc int bpf_in_interrupt(void)
3721+
{
3722+
return in_interrupt();
3723+
}
3724+
37173725
__bpf_kfunc_end_defs();
37183726

37193727
BTF_KFUNCS_START(generic_btf_ids)
@@ -3754,6 +3762,7 @@ BTF_ID_FLAGS(func, bpf_throw)
37543762
#ifdef CONFIG_BPF_EVENTS
37553763
BTF_ID_FLAGS(func, bpf_send_signal_task, KF_TRUSTED_ARGS)
37563764
#endif
3765+
BTF_ID_FLAGS(func, bpf_in_interrupt, KF_FASTCALL)
37573766
BTF_KFUNCS_END(generic_btf_ids)
37583767

37593768
static const struct btf_kfunc_id_set generic_kfunc_set = {

kernel/bpf/verifier.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12259,6 +12259,7 @@ enum special_kfunc_type {
1225912259
KF_bpf_res_spin_lock_irqsave,
1226012260
KF_bpf_res_spin_unlock_irqrestore,
1226112261
KF___bpf_trap,
12262+
KF_bpf_in_interrupt,
1226212263
};
1226312264

1226412265
BTF_ID_LIST(special_kfunc_list)
@@ -12327,6 +12328,7 @@ BTF_ID(func, bpf_res_spin_unlock)
1232712328
BTF_ID(func, bpf_res_spin_lock_irqsave)
1232812329
BTF_ID(func, bpf_res_spin_unlock_irqrestore)
1232912330
BTF_ID(func, __bpf_trap)
12331+
BTF_ID(func, bpf_in_interrupt)
1233012332

1233112333
static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta)
1233212334
{
@@ -21977,6 +21979,15 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2197721979
desc->func_id == special_kfunc_list[KF_bpf_rdonly_cast]) {
2197821980
insn_buf[0] = BPF_MOV64_REG(BPF_REG_0, BPF_REG_1);
2197921981
*cnt = 1;
21982+
} else if (desc->func_id == special_kfunc_list[KF_bpf_in_interrupt]) {
21983+
#ifdef CONFIG_X86_64
21984+
insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)&__preempt_count);
21985+
insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
21986+
insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);
21987+
insn_buf[3] = BPF_ALU32_IMM(BPF_AND, BPF_REG_0, NMI_MASK | HARDIRQ_MASK |
21988+
(IS_ENABLED(CONFIG_PREEMPT_RT) ? 0 : SOFTIRQ_MASK));
21989+
*cnt = 4;
21990+
#endif
2198021991
}
2198121992

2198221993
if (env->insn_aux_data[insn_idx].arg_prog) {

0 commit comments

Comments
 (0)