Skip to content

Commit 8ec3bf5

Browse files
Alexei StarovoitovMartin KaFai Lau
authored andcommitted
bpf: Add bpf_guard_preempt() convenience macro
Add bpf_guard_preempt() macro that uses newly introduced bpf_preempt_disable/enable() kfuncs to guard a critical section. Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent 52578f7 commit 8ec3bf5

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,28 @@ l_true: \
397397
, [as]"i"((dst_as << 16) | src_as));
398398
#endif
399399

400+
void bpf_preempt_disable(void) __weak __ksym;
401+
void bpf_preempt_enable(void) __weak __ksym;
402+
403+
typedef struct {
404+
} __bpf_preempt_t;
405+
406+
static inline __bpf_preempt_t __bpf_preempt_constructor(void)
407+
{
408+
__bpf_preempt_t ret = {};
409+
410+
bpf_preempt_disable();
411+
return ret;
412+
}
413+
static inline void __bpf_preempt_destructor(__bpf_preempt_t *t)
414+
{
415+
bpf_preempt_enable();
416+
}
417+
#define bpf_guard_preempt() \
418+
__bpf_preempt_t ___bpf_apply(preempt, __COUNTER__) \
419+
__attribute__((__unused__, __cleanup__(__bpf_preempt_destructor))) = \
420+
__bpf_preempt_constructor()
421+
400422
/* Description
401423
* Assert that a conditional expression is true.
402424
* Returns

tools/testing/selftests/bpf/progs/preempt_lock.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#include <bpf/bpf_helpers.h>
44
#include <bpf/bpf_tracing.h>
55
#include "bpf_misc.h"
6-
7-
void bpf_preempt_disable(void) __ksym;
8-
void bpf_preempt_enable(void) __ksym;
6+
#include "bpf_experimental.h"
97

108
SEC("?tc")
119
__failure __msg("1 bpf_preempt_enable is missing")
@@ -92,8 +90,7 @@ static __noinline void preempt_balance_subprog(void)
9290
SEC("?tc")
9391
__success int preempt_balance(struct __sk_buff *ctx)
9492
{
95-
bpf_preempt_disable();
96-
bpf_preempt_enable();
93+
bpf_guard_preempt();
9794
return 0;
9895
}
9996

0 commit comments

Comments
 (0)