Skip to content

Commit 4b7e7cd

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Disable migration before calling ops->map_free()
The freeing of all map elements may invoke bpf_obj_free_fields() to free the special fields in the map value. Since these special fields may be allocated from bpf memory allocator, migrate_{disable|enable} pairs are necessary for the freeing of these special fields. To simplify reasoning about when migrate_disable() is needed for the freeing of these special fields, let the caller to guarantee migration is disabled before invoking bpf_obj_free_fields(). Therefore, disabling migration before calling ops->map_free() to simplify the freeing of map values or special fields allocated from bpf memory allocator. After disabling migration in bpf_map_free(), there is no need for additional migration_{disable|enable} pairs in these ->map_free() callbacks. Remove these redundant invocations. The migrate_{disable|enable} pairs in the underlying implementation of bpf_obj_free_fields() will be removed by the following patch. Signed-off-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 090d7f2 commit 4b7e7cd

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

kernel/bpf/bpf_local_storage.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,15 +905,11 @@ void bpf_local_storage_map_free(struct bpf_map *map,
905905
while ((selem = hlist_entry_safe(
906906
rcu_dereference_raw(hlist_first_rcu(&b->list)),
907907
struct bpf_local_storage_elem, map_node))) {
908-
if (busy_counter) {
909-
migrate_disable();
908+
if (busy_counter)
910909
this_cpu_inc(*busy_counter);
911-
}
912910
bpf_selem_unlink(selem, true);
913-
if (busy_counter) {
911+
if (busy_counter)
914912
this_cpu_dec(*busy_counter);
915-
migrate_enable();
916-
}
917913
cond_resched_rcu();
918914
}
919915
rcu_read_unlock();

kernel/bpf/hashtab.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,10 +1500,9 @@ static void delete_all_elements(struct bpf_htab *htab)
15001500
{
15011501
int i;
15021502

1503-
/* It's called from a worker thread, so disable migration here,
1504-
* since bpf_mem_cache_free() relies on that.
1503+
/* It's called from a worker thread and migration has been disabled,
1504+
* therefore, it is OK to invoke bpf_mem_cache_free() directly.
15051505
*/
1506-
migrate_disable();
15071506
for (i = 0; i < htab->n_buckets; i++) {
15081507
struct hlist_nulls_head *head = select_bucket(htab, i);
15091508
struct hlist_nulls_node *n;
@@ -1515,7 +1514,6 @@ static void delete_all_elements(struct bpf_htab *htab)
15151514
}
15161515
cond_resched();
15171516
}
1518-
migrate_enable();
15191517
}
15201518

15211519
static void htab_free_malloced_timers_and_wq(struct bpf_htab *htab)

kernel/bpf/range_tree.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,7 @@ void range_tree_destroy(struct range_tree *rt)
259259

260260
while ((rn = range_it_iter_first(rt, 0, -1U))) {
261261
range_it_remove(rn, rt);
262-
migrate_disable();
263262
bpf_mem_free(&bpf_global_ma, rn);
264-
migrate_enable();
265263
}
266264
}
267265

kernel/bpf/syscall.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,14 @@ static void bpf_map_free(struct bpf_map *map)
835835
struct btf_record *rec = map->record;
836836
struct btf *btf = map->btf;
837837

838-
/* implementation dependent freeing */
838+
/* implementation dependent freeing. Disabling migration to simplify
839+
* the free of values or special fields allocated from bpf memory
840+
* allocator.
841+
*/
842+
migrate_disable();
839843
map->ops->map_free(map);
844+
migrate_enable();
845+
840846
/* Delay freeing of btf_record for maps, as map_free
841847
* callback usually needs access to them. It is better to do it here
842848
* than require each callback to do the free itself manually.

0 commit comments

Comments
 (0)