Skip to content

Commit 2d92ef7

Browse files
author
Alexei Starovoitov
committed
Merge branch 'selftests-bpf-introduce-experimental-bpf_in_interrupt'
Leon Hwang says: ==================== selftests/bpf: Introduce experimental bpf_in_interrupt() 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/[email protected]/ v1 -> v2: * Fix a build error reported by test bot. ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 929adf8 + 88a3bde commit 2d92ef7

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,4 +599,58 @@ extern void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it) __weak __ksym;
599599
extern int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__str,
600600
struct bpf_dynptr *value_p) __weak __ksym;
601601

602+
#define PREEMPT_BITS 8
603+
#define SOFTIRQ_BITS 8
604+
#define HARDIRQ_BITS 4
605+
#define NMI_BITS 4
606+
607+
#define PREEMPT_SHIFT 0
608+
#define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
609+
#define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
610+
#define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS)
611+
612+
#define __IRQ_MASK(x) ((1UL << (x))-1)
613+
614+
#define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
615+
#define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
616+
#define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT)
617+
618+
extern bool CONFIG_PREEMPT_RT __kconfig __weak;
619+
#ifdef bpf_target_x86
620+
extern const int __preempt_count __ksym;
621+
#endif
622+
623+
struct task_struct___preempt_rt {
624+
int softirq_disable_cnt;
625+
} __attribute__((preserve_access_index));
626+
627+
static inline int get_preempt_count(void)
628+
{
629+
#if defined(bpf_target_x86)
630+
return *(int *) bpf_this_cpu_ptr(&__preempt_count);
631+
#elif defined(bpf_target_arm64)
632+
return bpf_get_current_task_btf()->thread_info.preempt.count;
633+
#endif
634+
return 0;
635+
}
636+
637+
/* Description
638+
* Report whether it is in interrupt context. Only works on the following archs:
639+
* * x86
640+
* * arm64
641+
*/
642+
static inline int bpf_in_interrupt(void)
643+
{
644+
struct task_struct___preempt_rt *tsk;
645+
int pcnt;
646+
647+
pcnt = get_preempt_count();
648+
if (!CONFIG_PREEMPT_RT)
649+
return pcnt & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_MASK);
650+
651+
tsk = (void *) bpf_get_current_task_btf();
652+
return (pcnt & (NMI_MASK | HARDIRQ_MASK)) |
653+
(tsk->softirq_disable_cnt & SOFTIRQ_MASK);
654+
}
655+
602656
#endif

tools/testing/selftests/bpf/prog_tests/timer.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <test_progs.h>
44
#include "timer.skel.h"
55
#include "timer_failure.skel.h"
6+
#include "timer_interrupt.skel.h"
67

78
#define NUM_THR 8
89

@@ -95,3 +96,32 @@ void serial_test_timer(void)
9596

9697
RUN_TESTS(timer_failure);
9798
}
99+
100+
void test_timer_interrupt(void)
101+
{
102+
struct timer_interrupt *skel = NULL;
103+
int err, prog_fd;
104+
LIBBPF_OPTS(bpf_test_run_opts, opts);
105+
106+
skel = timer_interrupt__open_and_load();
107+
if (!ASSERT_OK_PTR(skel, "timer_interrupt__open_and_load"))
108+
return;
109+
110+
err = timer_interrupt__attach(skel);
111+
if (!ASSERT_OK(err, "timer_interrupt__attach"))
112+
goto out;
113+
114+
prog_fd = bpf_program__fd(skel->progs.test_timer_interrupt);
115+
err = bpf_prog_test_run_opts(prog_fd, &opts);
116+
if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
117+
goto out;
118+
119+
usleep(50);
120+
121+
ASSERT_EQ(skel->bss->in_interrupt, 0, "in_interrupt");
122+
if (skel->bss->preempt_count)
123+
ASSERT_NEQ(skel->bss->in_interrupt_cb, 0, "in_interrupt_cb");
124+
125+
out:
126+
timer_interrupt__destroy(skel);
127+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <vmlinux.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include "bpf_experimental.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
#define CLOCK_MONOTONIC 1
10+
11+
int preempt_count;
12+
int in_interrupt;
13+
int in_interrupt_cb;
14+
15+
struct elem {
16+
struct bpf_timer t;
17+
};
18+
19+
struct {
20+
__uint(type, BPF_MAP_TYPE_ARRAY);
21+
__uint(max_entries, 1);
22+
__type(key, int);
23+
__type(value, struct elem);
24+
} array SEC(".maps");
25+
26+
static int timer_in_interrupt(void *map, int *key, struct bpf_timer *timer)
27+
{
28+
preempt_count = get_preempt_count();
29+
in_interrupt_cb = bpf_in_interrupt();
30+
return 0;
31+
}
32+
33+
SEC("fentry/bpf_fentry_test1")
34+
int BPF_PROG(test_timer_interrupt)
35+
{
36+
struct bpf_timer *timer;
37+
int key = 0;
38+
39+
timer = bpf_map_lookup_elem(&array, &key);
40+
if (!timer)
41+
return 0;
42+
43+
in_interrupt = bpf_in_interrupt();
44+
bpf_timer_init(timer, &array, CLOCK_MONOTONIC);
45+
bpf_timer_set_callback(timer, timer_in_interrupt);
46+
bpf_timer_start(timer, 0, 0);
47+
return 0;
48+
}

0 commit comments

Comments
 (0)