Skip to content

Commit 5d5de3a

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Only allocate one bpf_mem_cache for bpf_cpumask_ma
The size of bpf_cpumask is fixed, so there is no need to allocate many bpf_mem_caches for bpf_cpumask_ma, just one bpf_mem_cache is enough. Also add comments for bpf_mem_alloc_init() in bpf_mem_alloc.h to prevent future miuse. Signed-off-by: Hou Tao <houtao1@huawei.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Link: https://lore.kernel.org/r/20230216024821.2202916-1-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent dbd8d22 commit 5d5de3a

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

include/linux/bpf_mem_alloc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ struct bpf_mem_alloc {
1414
struct work_struct work;
1515
};
1616

17+
/* 'size != 0' is for bpf_mem_alloc which manages fixed-size objects.
18+
* Alloc and free are done with bpf_mem_cache_{alloc,free}().
19+
*
20+
* 'size = 0' is for bpf_mem_alloc which manages many fixed-size objects.
21+
* Alloc and free are done with bpf_mem_{alloc,free}() and the size of
22+
* the returned object is given by the size argument of bpf_mem_alloc().
23+
*/
1724
int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size, bool percpu);
1825
void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma);
1926

kernel/bpf/cpumask.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ __bpf_kfunc struct bpf_cpumask *bpf_cpumask_create(void)
5555
/* cpumask must be the first element so struct bpf_cpumask be cast to struct cpumask. */
5656
BUILD_BUG_ON(offsetof(struct bpf_cpumask, cpumask) != 0);
5757

58-
cpumask = bpf_mem_alloc(&bpf_cpumask_ma, sizeof(*cpumask));
58+
cpumask = bpf_mem_cache_alloc(&bpf_cpumask_ma);
5959
if (!cpumask)
6060
return NULL;
6161

@@ -123,7 +123,7 @@ __bpf_kfunc void bpf_cpumask_release(struct bpf_cpumask *cpumask)
123123

124124
if (refcount_dec_and_test(&cpumask->usage)) {
125125
migrate_disable();
126-
bpf_mem_free(&bpf_cpumask_ma, cpumask);
126+
bpf_mem_cache_free(&bpf_cpumask_ma, cpumask);
127127
migrate_enable();
128128
}
129129
}
@@ -468,7 +468,7 @@ static int __init cpumask_kfunc_init(void)
468468
},
469469
};
470470

471-
ret = bpf_mem_alloc_init(&bpf_cpumask_ma, 0, false);
471+
ret = bpf_mem_alloc_init(&bpf_cpumask_ma, sizeof(struct bpf_cpumask), false);
472472
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &cpumask_kfunc_set);
473473
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &cpumask_kfunc_set);
474474
return ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors,

0 commit comments

Comments
 (0)