Skip to content

Commit ea5b229

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Remove migrate_{disable|enable} in ->map_for_each_callback
BPF program may call bpf_for_each_map_elem(), and it will call the ->map_for_each_callback callback of related bpf map. Considering the running context of bpf program has already disabled migration, remove the unnecessary migrate_{disable|enable} pair in the implementations of ->map_for_each_callback. To ensure the guarantee will not be voilated later, also add cant_migrate() check in the implementations. Signed-off-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1b1a01d commit ea5b229

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

kernel/bpf/arraymap.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -735,13 +735,13 @@ static long bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback
735735
u64 ret = 0;
736736
void *val;
737737

738+
cant_migrate();
739+
738740
if (flags != 0)
739741
return -EINVAL;
740742

741743
is_percpu = map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
742744
array = container_of(map, struct bpf_array, map);
743-
if (is_percpu)
744-
migrate_disable();
745745
for (i = 0; i < map->max_entries; i++) {
746746
if (is_percpu)
747747
val = this_cpu_ptr(array->pptrs[i]);
@@ -756,8 +756,6 @@ static long bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback
756756
break;
757757
}
758758

759-
if (is_percpu)
760-
migrate_enable();
761759
return num_elems;
762760
}
763761

kernel/bpf/hashtab.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,17 +2208,18 @@ static long bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_
22082208
bool is_percpu;
22092209
u64 ret = 0;
22102210

2211+
cant_migrate();
2212+
22112213
if (flags != 0)
22122214
return -EINVAL;
22132215

22142216
is_percpu = htab_is_percpu(htab);
22152217

22162218
roundup_key_size = round_up(map->key_size, 8);
2217-
/* disable migration so percpu value prepared here will be the
2218-
* same as the one seen by the bpf program with bpf_map_lookup_elem().
2219+
/* migration has been disabled, so percpu value prepared here will be
2220+
* the same as the one seen by the bpf program with
2221+
* bpf_map_lookup_elem().
22192222
*/
2220-
if (is_percpu)
2221-
migrate_disable();
22222223
for (i = 0; i < htab->n_buckets; i++) {
22232224
b = &htab->buckets[i];
22242225
rcu_read_lock();
@@ -2244,8 +2245,6 @@ static long bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_
22442245
rcu_read_unlock();
22452246
}
22462247
out:
2247-
if (is_percpu)
2248-
migrate_enable();
22492248
return num_elems;
22502249
}
22512250

0 commit comments

Comments
 (0)