Skip to content

Commit 48a97ff

Browse files
anakryikoborkmann
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]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 39e9d5f commit 48a97ff

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
@@ -2381,6 +2381,9 @@ bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
23812381
bool bpf_jit_bypass_spec_v1(void);
23822382
bool bpf_jit_bypass_spec_v4(void);
23832383

2384+
#define bpf_rcu_lock_held() \
2385+
(rcu_read_lock_held() || rcu_read_lock_trace_held() || rcu_read_lock_bh_held())
2386+
23842387
#ifdef CONFIG_BPF_SYSCALL
23852388
DECLARE_PER_CPU(int, bpf_prog_active);
23862389
extern struct mutex bpf_stats_enabled_mutex;

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
@@ -657,8 +657,7 @@ static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
657657
struct htab_elem *l;
658658
u32 hash, key_size;
659659

660-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
661-
!rcu_read_lock_bh_held());
660+
WARN_ON_ONCE(!bpf_rcu_lock_held());
662661

663662
key_size = map->key_size;
664663

@@ -1086,8 +1085,7 @@ static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
10861085
/* unknown flags */
10871086
return -EINVAL;
10881087

1089-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1090-
!rcu_read_lock_bh_held());
1088+
WARN_ON_ONCE(!bpf_rcu_lock_held());
10911089

10921090
key_size = map->key_size;
10931091

@@ -1194,8 +1192,7 @@ static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value
11941192
/* unknown flags */
11951193
return -EINVAL;
11961194

1197-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1198-
!rcu_read_lock_bh_held());
1195+
WARN_ON_ONCE(!bpf_rcu_lock_held());
11991196

12001197
key_size = map->key_size;
12011198

@@ -1263,8 +1260,7 @@ static long htab_map_update_elem_in_place(struct bpf_map *map, void *key,
12631260
/* unknown flags */
12641261
return -EINVAL;
12651262

1266-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1267-
!rcu_read_lock_bh_held());
1263+
WARN_ON_ONCE(!bpf_rcu_lock_held());
12681264

12691265
key_size = map->key_size;
12701266

@@ -1326,8 +1322,7 @@ static long __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
13261322
/* unknown flags */
13271323
return -EINVAL;
13281324

1329-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1330-
!rcu_read_lock_bh_held());
1325+
WARN_ON_ONCE(!bpf_rcu_lock_held());
13311326

13321327
key_size = map->key_size;
13331328

@@ -1404,8 +1399,7 @@ static long htab_map_delete_elem(struct bpf_map *map, void *key)
14041399
u32 hash, key_size;
14051400
int ret;
14061401

1407-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1408-
!rcu_read_lock_bh_held());
1402+
WARN_ON_ONCE(!bpf_rcu_lock_held());
14091403

14101404
key_size = map->key_size;
14111405

@@ -1440,8 +1434,7 @@ static long htab_lru_map_delete_elem(struct bpf_map *map, void *key)
14401434
u32 hash, key_size;
14411435
int ret;
14421436

1443-
WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1444-
!rcu_read_lock_bh_held());
1437+
WARN_ON_ONCE(!bpf_rcu_lock_held());
14451438

14461439
key_size = map->key_size;
14471440

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)