Skip to content

Commit 7468048

Browse files
Alexei Starovoitovborkmann
authored andcommitted
bpf: Further refactor alloc_bulk().
In certain scenarios alloc_bulk() might be taking free objects mainly from free_by_rcu_ttrace list. In such case get_memcg() and set_active_memcg() are redundant, but they show up in perf profile. Split the loop and only set memcg when allocating from slab. No performance difference in this patch alone, but it helps in combination with further patches. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Hou Tao <houtao1@huawei.com> Link: https://lore.kernel.org/bpf/20230706033447.54696-7-alexei.starovoitov@gmail.com
1 parent 18e027b commit 7468048

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

kernel/bpf/memalloc.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@ static void alloc_bulk(struct bpf_mem_cache *c, int cnt, int node)
196196
void *obj;
197197
int i;
198198

199-
memcg = get_memcg(c);
200-
old_memcg = set_active_memcg(memcg);
201199
for (i = 0; i < cnt; i++) {
202200
/*
203201
* free_by_rcu_ttrace is only manipulated by irq work refill_work().
@@ -212,16 +210,24 @@ static void alloc_bulk(struct bpf_mem_cache *c, int cnt, int node)
212210
* numa node and it is not a guarantee.
213211
*/
214212
obj = __llist_del_first(&c->free_by_rcu_ttrace);
215-
if (!obj) {
216-
/* Allocate, but don't deplete atomic reserves that typical
217-
* GFP_ATOMIC would do. irq_work runs on this cpu and kmalloc
218-
* will allocate from the current numa node which is what we
219-
* want here.
220-
*/
221-
obj = __alloc(c, node, GFP_NOWAIT | __GFP_NOWARN | __GFP_ACCOUNT);
222-
if (!obj)
223-
break;
224-
}
213+
if (!obj)
214+
break;
215+
add_obj_to_free_list(c, obj);
216+
}
217+
if (i >= cnt)
218+
return;
219+
220+
memcg = get_memcg(c);
221+
old_memcg = set_active_memcg(memcg);
222+
for (; i < cnt; i++) {
223+
/* Allocate, but don't deplete atomic reserves that typical
224+
* GFP_ATOMIC would do. irq_work runs on this cpu and kmalloc
225+
* will allocate from the current numa node which is what we
226+
* want here.
227+
*/
228+
obj = __alloc(c, node, GFP_NOWAIT | __GFP_NOWARN | __GFP_ACCOUNT);
229+
if (!obj)
230+
break;
225231
add_obj_to_free_list(c, obj);
226232
}
227233
set_active_memcg(old_memcg);

0 commit comments

Comments
 (0)