]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bpf: Disable migration before calling ops->map_free()
authorHou Tao <houtao1@huawei.com>
Wed, 8 Jan 2025 01:07:22 +0000 (09:07 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 9 Jan 2025 02:06:36 +0000 (18:06 -0800)
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 <houtao1@huawei.com>
Link: https://lore.kernel.org/r/20250108010728.207536-11-houtao@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/bpf_local_storage.c
kernel/bpf/hashtab.c
kernel/bpf/range_tree.c
kernel/bpf/syscall.c

index 615a3034baeb512096d96194ccff22b6fd58269b..12cf6382175e1e36e28dca255a39e5a9971eb074 100644 (file)
@@ -905,15 +905,11 @@ void bpf_local_storage_map_free(struct bpf_map *map,
                while ((selem = hlist_entry_safe(
                                rcu_dereference_raw(hlist_first_rcu(&b->list)),
                                struct bpf_local_storage_elem, map_node))) {
-                       if (busy_counter) {
-                               migrate_disable();
+                       if (busy_counter)
                                this_cpu_inc(*busy_counter);
-                       }
                        bpf_selem_unlink(selem, true);
-                       if (busy_counter) {
+                       if (busy_counter)
                                this_cpu_dec(*busy_counter);
-                               migrate_enable();
-                       }
                        cond_resched_rcu();
                }
                rcu_read_unlock();
index bccae537f9d222c73eeecffc1e8efabe63b38d3b..40095dda891d31846aef832e27c9f77759e23cf2 100644 (file)
@@ -1500,10 +1500,9 @@ static void delete_all_elements(struct bpf_htab *htab)
 {
        int i;
 
-       /* It's called from a worker thread, so disable migration here,
-        * since bpf_mem_cache_free() relies on that.
+       /* It's called from a worker thread and migration has been disabled,
+        * therefore, it is OK to invoke bpf_mem_cache_free() directly.
         */
-       migrate_disable();
        for (i = 0; i < htab->n_buckets; i++) {
                struct hlist_nulls_head *head = select_bucket(htab, i);
                struct hlist_nulls_node *n;
@@ -1515,7 +1514,6 @@ static void delete_all_elements(struct bpf_htab *htab)
                }
                cond_resched();
        }
-       migrate_enable();
 }
 
 static void htab_free_malloced_timers_and_wq(struct bpf_htab *htab)
index 5bdf9aadca3a62f5a4acb572f62edb7f420c4ed0..37b80a23ae1aed31c84f2676bd1214be6d7fbbff 100644 (file)
@@ -259,9 +259,7 @@ void range_tree_destroy(struct range_tree *rt)
 
        while ((rn = range_it_iter_first(rt, 0, -1U))) {
                range_it_remove(rn, rt);
-               migrate_disable();
                bpf_mem_free(&bpf_global_ma, rn);
-               migrate_enable();
        }
 }
 
index 4e88797fdbebbce8cc958782f7de7e36b2754b5c..5d2420492946130c9cc157ea970777a5571233c5 100644 (file)
@@ -835,8 +835,14 @@ static void bpf_map_free(struct bpf_map *map)
        struct btf_record *rec = map->record;
        struct btf *btf = map->btf;
 
-       /* implementation dependent freeing */
+       /* implementation dependent freeing. Disabling migration to simplify
+        * the free of values or special fields allocated from bpf memory
+        * allocator.
+        */
+       migrate_disable();
        map->ops->map_free(map);
+       migrate_enable();
+
        /* Delay freeing of btf_record for maps, as map_free
         * callback usually needs access to them. It is better to do it here
         * than require each callback to do the free itself manually.