Skip to content

Commit 78f520b

Browse files
Sebastian Andrzej Siewiorkuba-moo
authored andcommitted
net: Use nested-BH locking for bpf_scratchpad.
bpf_scratchpad is a per-CPU variable and relies on disabled BH for its locking. Without per-CPU locking in local_bh_disable() on PREEMPT_RT this data structure requires explicit locking. Add a local_lock_t to the data structure and use local_lock_nested_bh() for locking. This change adds only lockdep coverage and does not alter the functional behaviour for !PREEMPT_RT. Cc: Alexei Starovoitov <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Hao Luo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: John Fastabend <[email protected]> Cc: KP Singh <[email protected]> Cc: Martin KaFai Lau <[email protected]> Cc: Song Liu <[email protected]> Cc: Stanislav Fomichev <[email protected]> Cc: Yonghong Song <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent d1542d4 commit 78f520b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

net/core/filter.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,9 +1658,12 @@ struct bpf_scratchpad {
16581658
__be32 diff[MAX_BPF_STACK / sizeof(__be32)];
16591659
u8 buff[MAX_BPF_STACK];
16601660
};
1661+
local_lock_t bh_lock;
16611662
};
16621663

1663-
static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1664+
static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp) = {
1665+
.bh_lock = INIT_LOCAL_LOCK(bh_lock),
1666+
};
16641667

16651668
static inline int __bpf_try_make_writable(struct sk_buff *skb,
16661669
unsigned int write_len)
@@ -2021,6 +2024,7 @@ BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
20212024
struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
20222025
u32 diff_size = from_size + to_size;
20232026
int i, j = 0;
2027+
__wsum ret;
20242028

20252029
/* This is quite flexible, some examples:
20262030
*
@@ -2034,12 +2038,15 @@ BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
20342038
diff_size > sizeof(sp->diff)))
20352039
return -EINVAL;
20362040

2041+
local_lock_nested_bh(&bpf_sp.bh_lock);
20372042
for (i = 0; i < from_size / sizeof(__be32); i++, j++)
20382043
sp->diff[j] = ~from[i];
20392044
for (i = 0; i < to_size / sizeof(__be32); i++, j++)
20402045
sp->diff[j] = to[i];
20412046

2042-
return csum_partial(sp->diff, diff_size, seed);
2047+
ret = csum_partial(sp->diff, diff_size, seed);
2048+
local_unlock_nested_bh(&bpf_sp.bh_lock);
2049+
return ret;
20432050
}
20442051

20452052
static const struct bpf_func_proto bpf_csum_diff_proto = {

0 commit comments

Comments
 (0)