Skip to content

Commit 48edc1f

Browse files
Roman GushchinAlexei Starovoitov
authored andcommitted
bpf: Prepare for memcg-based memory accounting for bpf maps
Bpf maps can be updated from an interrupt context and in such case there is no process which can be charged. It makes the memory accounting of bpf maps non-trivial. Fortunately, after commit 4127c65 ("mm: kmem: enable kernel memcg accounting from interrupt contexts") and commit b87d8ce ("mm, memcg: rework remote charging API to support nesting") it's finally possible. To make the ownership model simple and consistent, when the map is created, the memory cgroup of the current process is recorded. All subsequent allocations related to the bpf map are charged to the same memory cgroup. It includes allocations made by any processes (even if they do belong to a different cgroup) and from interrupts. This commit introduces 3 new helpers, which will be used by following commits to enable the accounting of bpf maps memory: - bpf_map_kmalloc_node() - bpf_map_kzalloc() - bpf_map_alloc_percpu() They are wrapping popular memory allocation functions. They set the active memory cgroup to the map's memory cgroup and add __GFP_ACCOUNT to the passed gfp flags. Then they call into the corresponding memory allocation function and restore the original active memory cgroup. These helpers are supposed to use everywhere except the map creation path. During the map creation when the map structure is allocated by itself, it cannot be passed to those helpers. In those cases default memory allocation function will be used with the __GFP_ACCOUNT flag. Signed-off-by: Roman Gushchin <guro@fb.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20201201215900.3569844-7-guro@fb.com
1 parent ddf8503 commit 48edc1f

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <linux/module.h>
2121
#include <linux/kallsyms.h>
2222
#include <linux/capability.h>
23+
#include <linux/sched/mm.h>
24+
#include <linux/slab.h>
2325

2426
struct bpf_verifier_env;
2527
struct bpf_verifier_log;
@@ -37,6 +39,7 @@ struct bpf_iter_aux_info;
3739
struct bpf_local_storage;
3840
struct bpf_local_storage_map;
3941
struct kobject;
42+
struct mem_cgroup;
4043

4144
extern struct idr btf_idr;
4245
extern spinlock_t btf_idr_lock;
@@ -161,6 +164,9 @@ struct bpf_map {
161164
u32 btf_value_type_id;
162165
struct btf *btf;
163166
struct bpf_map_memory memory;
167+
#ifdef CONFIG_MEMCG_KMEM
168+
struct mem_cgroup *memcg;
169+
#endif
164170
char name[BPF_OBJ_NAME_LEN];
165171
u32 btf_vmlinux_value_type_id;
166172
bool bypass_spec_v1;
@@ -1240,6 +1246,34 @@ int generic_map_delete_batch(struct bpf_map *map,
12401246
struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
12411247
struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
12421248

1249+
#ifdef CONFIG_MEMCG_KMEM
1250+
void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1251+
int node);
1252+
void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
1253+
void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
1254+
size_t align, gfp_t flags);
1255+
#else
1256+
static inline void *
1257+
bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
1258+
int node)
1259+
{
1260+
return kmalloc_node(size, flags, node);
1261+
}
1262+
1263+
static inline void *
1264+
bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
1265+
{
1266+
return kzalloc(size, flags);
1267+
}
1268+
1269+
static inline void __percpu *
1270+
bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
1271+
gfp_t flags)
1272+
{
1273+
return __alloc_percpu_gfp(size, align, flags);
1274+
}
1275+
#endif
1276+
12431277
extern int sysctl_unprivileged_bpf_disabled;
12441278

12451279
static inline bool bpf_allow_ptr_leaks(void)

‎kernel/bpf/syscall.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/poll.h>
3232
#include <linux/bpf-netns.h>
3333
#include <linux/rcupdate_trace.h>
34+
#include <linux/memcontrol.h>
3435

3536
#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
3637
(map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
@@ -456,6 +457,65 @@ void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
456457
__release(&map_idr_lock);
457458
}
458459

460+
#ifdef CONFIG_MEMCG_KMEM
461+
static void bpf_map_save_memcg(struct bpf_map *map)
462+
{
463+
map->memcg = get_mem_cgroup_from_mm(current->mm);
464+
}
465+
466+
static void bpf_map_release_memcg(struct bpf_map *map)
467+
{
468+
mem_cgroup_put(map->memcg);
469+
}
470+
471+
void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
472+
int node)
473+
{
474+
struct mem_cgroup *old_memcg;
475+
void *ptr;
476+
477+
old_memcg = set_active_memcg(map->memcg);
478+
ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
479+
set_active_memcg(old_memcg);
480+
481+
return ptr;
482+
}
483+
484+
void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
485+
{
486+
struct mem_cgroup *old_memcg;
487+
void *ptr;
488+
489+
old_memcg = set_active_memcg(map->memcg);
490+
ptr = kzalloc(size, flags | __GFP_ACCOUNT);
491+
set_active_memcg(old_memcg);
492+
493+
return ptr;
494+
}
495+
496+
void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
497+
size_t align, gfp_t flags)
498+
{
499+
struct mem_cgroup *old_memcg;
500+
void __percpu *ptr;
501+
502+
old_memcg = set_active_memcg(map->memcg);
503+
ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
504+
set_active_memcg(old_memcg);
505+
506+
return ptr;
507+
}
508+
509+
#else
510+
static void bpf_map_save_memcg(struct bpf_map *map)
511+
{
512+
}
513+
514+
static void bpf_map_release_memcg(struct bpf_map *map)
515+
{
516+
}
517+
#endif
518+
459519
/* called from workqueue */
460520
static void bpf_map_free_deferred(struct work_struct *work)
461521
{
@@ -464,6 +524,7 @@ static void bpf_map_free_deferred(struct work_struct *work)
464524

465525
bpf_map_charge_move(&mem, &map->memory);
466526
security_bpf_map_free(map);
527+
bpf_map_release_memcg(map);
467528
/* implementation dependent freeing */
468529
map->ops->map_free(map);
469530
bpf_map_charge_finish(&mem);
@@ -875,6 +936,8 @@ static int map_create(union bpf_attr *attr)
875936
if (err)
876937
goto free_map_sec;
877938

939+
bpf_map_save_memcg(map);
940+
878941
err = bpf_map_new_fd(map, f_flags);
879942
if (err < 0) {
880943
/* failed to allocate fd.

0 commit comments

Comments
 (0)