Skip to content

Commit eb2de31

Browse files
anakryikoKernel Patches Daemon
authored andcommitted
bpf: consistently use bpf_rcu_lock_held() everywhere
We have many places which open-code what's now is bpf_rcu_lock_held() macro, so replace all those places with a clean and short macro invocation. For that, move bpf_rcu_lock_held() macro into include/linux/bpf.h. Signed-off-by: Andrii Nakryiko <[email protected]>
1 parent c9241b4 commit eb2de31

File tree

4 files changed

+14
-25
lines changed

4 files changed

+14
-25
lines changed

include/linux/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,9 @@ extern const struct bpf_prog_ops bpf_offload_prog_ops;
24172417
extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
24182418
extern const struct bpf_verifier_ops xdp_analyzer_ops;
24192419

2420+
#define bpf_rcu_lock_held() \
2421+
(rcu_read_lock_held() || rcu_read_lock_trace_held() || rcu_read_lock_bh_held())
2422+
24202423
struct bpf_prog *bpf_prog_get(u32 ufd);
24212424
struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
24222425
bool attach_drv);

include/linux/bpf_local_storage.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
#define BPF_LOCAL_STORAGE_CACHE_SIZE 16
2020

21-
#define bpf_rcu_lock_held() \
22-
(rcu_read_lock_held() || rcu_read_lock_trace_held() || \
23-
rcu_read_lock_bh_held())
2421
struct bpf_local_storage_map_bucket {
2522
struct hlist_head list;
2623
raw_spinlock_t lock;

kernel/bpf/hashtab.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,7 @@ static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
669669
struct htab_elem *l;
670670
u32 hash, key_size;
671671

672-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
673-
!rcu_read_lock_bh_held());
672+
WARN_ON_ONCE(!bpf_rcu_lock_held());
674673

675674
key_size = map->key_size;
676675

@@ -1098,8 +1097,7 @@ static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
10981097
/* unknown flags */
10991098
return -EINVAL;
11001099

1101-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1102-
!rcu_read_lock_bh_held());
1100+
WARN_ON_ONCE(!bpf_rcu_lock_held());
11031101

11041102
key_size = map->key_size;
11051103

@@ -1206,8 +1204,7 @@ static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value
12061204
/* unknown flags */
12071205
return -EINVAL;
12081206

1209-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1210-
!rcu_read_lock_bh_held());
1207+
WARN_ON_ONCE(!bpf_rcu_lock_held());
12111208

12121209
key_size = map->key_size;
12131210

@@ -1275,8 +1272,7 @@ static long htab_map_update_elem_in_place(struct bpf_map *map, void *key,
12751272
/* unknown flags */
12761273
return -EINVAL;
12771274

1278-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1279-
!rcu_read_lock_bh_held());
1275+
WARN_ON_ONCE(!bpf_rcu_lock_held());
12801276

12811277
key_size = map->key_size;
12821278

@@ -1338,8 +1334,7 @@ static long __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
13381334
/* unknown flags */
13391335
return -EINVAL;
13401336

1341-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1342-
!rcu_read_lock_bh_held());
1337+
WARN_ON_ONCE(!bpf_rcu_lock_held());
13431338

13441339
key_size = map->key_size;
13451340

@@ -1416,8 +1411,7 @@ static long htab_map_delete_elem(struct bpf_map *map, void *key)
14161411
u32 hash, key_size;
14171412
int ret;
14181413

1419-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1420-
!rcu_read_lock_bh_held());
1414+
WARN_ON_ONCE(!bpf_rcu_lock_held());
14211415

14221416
key_size = map->key_size;
14231417

@@ -1452,8 +1446,7 @@ static long htab_lru_map_delete_elem(struct bpf_map *map, void *key)
14521446
u32 hash, key_size;
14531447
int ret;
14541448

1455-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1456-
!rcu_read_lock_bh_held());
1449+
WARN_ON_ONCE(!bpf_rcu_lock_held());
14571450

14581451
key_size = map->key_size;
14591452

kernel/bpf/helpers.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
*/
4343
BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
4444
{
45-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
46-
!rcu_read_lock_bh_held());
45+
WARN_ON_ONCE(!bpf_rcu_lock_held());
4746
return (unsigned long) map->ops->map_lookup_elem(map, key);
4847
}
4948

@@ -59,8 +58,7 @@ const struct bpf_func_proto bpf_map_lookup_elem_proto = {
5958
BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
6059
void *, value, u64, flags)
6160
{
62-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
63-
!rcu_read_lock_bh_held());
61+
WARN_ON_ONCE(!bpf_rcu_lock_held());
6462
return map->ops->map_update_elem(map, key, value, flags);
6563
}
6664

@@ -77,8 +75,7 @@ const struct bpf_func_proto bpf_map_update_elem_proto = {
7775

7876
BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
7977
{
80-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
81-
!rcu_read_lock_bh_held());
78+
WARN_ON_ONCE(!bpf_rcu_lock_held());
8279
return map->ops->map_delete_elem(map, key);
8380
}
8481

@@ -134,8 +131,7 @@ const struct bpf_func_proto bpf_map_peek_elem_proto = {
134131

135132
BPF_CALL_3(bpf_map_lookup_percpu_elem, struct bpf_map *, map, void *, key, u32, cpu)
136133
{
137-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
138-
!rcu_read_lock_bh_held());
134+
WARN_ON_ONCE(!bpf_rcu_lock_held());
139135
return (unsigned long) map->ops->map_lookup_percpu_elem(map, key, cpu);
140136
}
141137

0 commit comments

Comments
 (0)