Skip to content

Commit e65a5c6

Browse files
Martin KaFai LauAlexei Starovoitov
authored andcommitted
bpf: Add a few bpf mem allocator functions
This patch adds a few bpf mem allocator functions which will be used in the bpf_local_storage in a later patch. bpf_mem_cache_alloc_flags(..., gfp_t flags) is added. When the flags == GFP_KERNEL, it will fallback to __alloc(..., GFP_KERNEL). bpf_local_storage knows its running context is sleepable (GFP_KERNEL) and provides a better guarantee on memory allocation. bpf_local_storage has some uncommon cases that its selem cannot be reused immediately. It handles its own rcu_head and goes through a rcu_trace gp and then free it. bpf_mem_cache_raw_free() is added for direct free purpose without leaking the LLIST_NODE_SZ internal knowledge. During free time, the 'struct bpf_mem_alloc *ma' is no longer available. However, the caller should know if it is percpu memory or not and it can call different raw_free functions. bpf_local_storage does not support percpu value, so only the non-percpu 'bpf_mem_cache_raw_free()' is added in this patch. Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e993607 commit e65a5c6

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

include/linux/bpf_mem_alloc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ void bpf_mem_free(struct bpf_mem_alloc *ma, void *ptr);
3131
/* kmem_cache_alloc/free equivalent: */
3232
void *bpf_mem_cache_alloc(struct bpf_mem_alloc *ma);
3333
void bpf_mem_cache_free(struct bpf_mem_alloc *ma, void *ptr);
34+
void bpf_mem_cache_raw_free(void *ptr);
35+
void *bpf_mem_cache_alloc_flags(struct bpf_mem_alloc *ma, gfp_t flags);
3436

3537
#endif /* _BPF_MEM_ALLOC_H */

kernel/bpf/memalloc.c

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,8 @@ static struct llist_node notrace *__llist_del_first(struct llist_head *head)
121121
return entry;
122122
}
123123

124-
static void *__alloc(struct bpf_mem_cache *c, int node)
124+
static void *__alloc(struct bpf_mem_cache *c, int node, gfp_t flags)
125125
{
126-
/* Allocate, but don't deplete atomic reserves that typical
127-
* GFP_ATOMIC would do. irq_work runs on this cpu and kmalloc
128-
* will allocate from the current numa node which is what we
129-
* want here.
130-
*/
131-
gfp_t flags = GFP_NOWAIT | __GFP_NOWARN | __GFP_ACCOUNT;
132-
133126
if (c->percpu_size) {
134127
void **obj = kmalloc_node(c->percpu_size, flags, node);
135128
void *pptr = __alloc_percpu_gfp(c->unit_size, 8, flags);
@@ -185,7 +178,12 @@ static void alloc_bulk(struct bpf_mem_cache *c, int cnt, int node)
185178
*/
186179
obj = __llist_del_first(&c->free_by_rcu);
187180
if (!obj) {
188-
obj = __alloc(c, node);
181+
/* Allocate, but don't deplete atomic reserves that typical
182+
* GFP_ATOMIC would do. irq_work runs on this cpu and kmalloc
183+
* will allocate from the current numa node which is what we
184+
* want here.
185+
*/
186+
obj = __alloc(c, node, GFP_NOWAIT | __GFP_NOWARN | __GFP_ACCOUNT);
189187
if (!obj)
190188
break;
191189
}
@@ -676,3 +674,46 @@ void notrace bpf_mem_cache_free(struct bpf_mem_alloc *ma, void *ptr)
676674

677675
unit_free(this_cpu_ptr(ma->cache), ptr);
678676
}
677+
678+
/* Directly does a kfree() without putting 'ptr' back to the free_llist
679+
* for reuse and without waiting for a rcu_tasks_trace gp.
680+
* The caller must first go through the rcu_tasks_trace gp for 'ptr'
681+
* before calling bpf_mem_cache_raw_free().
682+
* It could be used when the rcu_tasks_trace callback does not have
683+
* a hold on the original bpf_mem_alloc object that allocated the
684+
* 'ptr'. This should only be used in the uncommon code path.
685+
* Otherwise, the bpf_mem_alloc's free_llist cannot be refilled
686+
* and may affect performance.
687+
*/
688+
void bpf_mem_cache_raw_free(void *ptr)
689+
{
690+
if (!ptr)
691+
return;
692+
693+
kfree(ptr - LLIST_NODE_SZ);
694+
}
695+
696+
/* When flags == GFP_KERNEL, it signals that the caller will not cause
697+
* deadlock when using kmalloc. bpf_mem_cache_alloc_flags() will use
698+
* kmalloc if the free_llist is empty.
699+
*/
700+
void notrace *bpf_mem_cache_alloc_flags(struct bpf_mem_alloc *ma, gfp_t flags)
701+
{
702+
struct bpf_mem_cache *c;
703+
void *ret;
704+
705+
c = this_cpu_ptr(ma->cache);
706+
707+
ret = unit_alloc(c);
708+
if (!ret && flags == GFP_KERNEL) {
709+
struct mem_cgroup *memcg, *old_memcg;
710+
711+
memcg = get_memcg(c);
712+
old_memcg = set_active_memcg(memcg);
713+
ret = __alloc(c, NUMA_NO_NODE, GFP_KERNEL | __GFP_NOWARN | __GFP_ACCOUNT);
714+
set_active_memcg(old_memcg);
715+
mem_cgroup_put(memcg);
716+
}
717+
718+
return !ret ? NULL : ret + LLIST_NODE_SZ;
719+
}

0 commit comments

Comments
 (0)