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>
                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();
 
 {
        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;
                }
                cond_resched();
        }
-       migrate_enable();
 }
 
 static void htab_free_malloced_timers_and_wq(struct bpf_htab *htab)
 
 
        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();
        }
 }
 
 
        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.