]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bpf: Don't allocate per-cpu extra_elems for fd htab
authorHou Tao <houtao1@huawei.com>
Tue, 1 Apr 2025 06:22:49 +0000 (14:22 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 10 Apr 2025 03:12:54 +0000 (20:12 -0700)
The update of element in fd htab is in-place now, therefore, there is no
need to allocate per-cpu extra_elems, just remove it.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Hou Tao <houtao1@huawei.com>
Link: https://lore.kernel.org/r/20250401062250.543403-6-houtao@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/hashtab.c

index 097992efef05f544a0e4f16f6ed521d4b33ab7f7..2e18d7e50d9b62c00ee0c0f55e8dc14c9ae25b19 100644 (file)
@@ -206,9 +206,13 @@ static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
        return (struct htab_elem *) (htab->elems + i * (u64)htab->elem_size);
 }
 
+/* Both percpu and fd htab support in-place update, so no need for
+ * extra elem. LRU itself can remove the least used element, so
+ * there is no need for an extra elem during map_update.
+ */
 static bool htab_has_extra_elems(struct bpf_htab *htab)
 {
-       return !htab_is_percpu(htab) && !htab_is_lru(htab);
+       return !htab_is_percpu(htab) && !htab_is_lru(htab) && !is_fd_htab(htab);
 }
 
 static void htab_free_prealloced_timers_and_wq(struct bpf_htab *htab)
@@ -464,8 +468,6 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
 {
        bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
                       attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
-       bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
-                   attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
        /* percpu_lru means each cpu has its own LRU list.
         * it is different from BPF_MAP_TYPE_PERCPU_HASH where
         * the map's value itself is percpu.  percpu_lru has
@@ -560,10 +562,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
                if (err)
                        goto free_map_locked;
 
-               if (!percpu && !lru) {
-                       /* lru itself can remove the least used element, so
-                        * there is no need for an extra elem during map_update.
-                        */
+               if (htab_has_extra_elems(htab)) {
                        err = alloc_extra_elems(htab);
                        if (err)
                                goto free_prealloc;