Skip to content

Commit e02bd9e

Browse files
committed
bpf: Don't allocate per-cpu extra_elems for fd htab
JIRA: https://issues.redhat.com/browse/RHEL-110274 commit 6704b1e Author: Hou Tao <houtao1@huawei.com> Date: Tue Apr 1 14:22:49 2025 +0800 bpf: Don't allocate per-cpu extra_elems for fd htab 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> Signed-off-by: Viktor Malik <vmalik@redhat.com>
1 parent 3775771 commit e02bd9e

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

kernel/bpf/hashtab.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,13 @@ static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
225225
return (struct htab_elem *) (htab->elems + i * (u64)htab->elem_size);
226226
}
227227

228+
/* Both percpu and fd htab support in-place update, so no need for
229+
* extra elem. LRU itself can remove the least used element, so
230+
* there is no need for an extra elem during map_update.
231+
*/
228232
static bool htab_has_extra_elems(struct bpf_htab *htab)
229233
{
230-
return !htab_is_percpu(htab) && !htab_is_lru(htab);
234+
return !htab_is_percpu(htab) && !htab_is_lru(htab) && !is_fd_htab(htab);
231235
}
232236

233237
static void htab_free_prealloced_timers_and_wq(struct bpf_htab *htab)
@@ -483,8 +487,6 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
483487
{
484488
bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
485489
attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
486-
bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
487-
attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
488490
/* percpu_lru means each cpu has its own LRU list.
489491
* it is different from BPF_MAP_TYPE_PERCPU_HASH where
490492
* the map's value itself is percpu. percpu_lru has
@@ -590,10 +592,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
590592
if (err)
591593
goto free_map_locked;
592594

593-
if (!percpu && !lru) {
594-
/* lru itself can remove the least used element, so
595-
* there is no need for an extra elem during map_update.
596-
*/
595+
if (htab_has_extra_elems(htab)) {
597596
err = alloc_extra_elems(htab);
598597
if (err)
599598
goto free_prealloc;

0 commit comments

Comments
 (0)