Skip to content

Commit

Permalink
bpf: eliminate rlimit-based memory accounting infra for bpf maps
Browse files Browse the repository at this point in the history
Remove rlimit-based accounting infrastructure code, which is not used
anymore.

To provide a backward compatibility, use an approximation of the
bpf map memory footprint as a "memlock" value, available to a user
via map info. The approximation is based on the maximal number of
elements and key and value sizes.

Signed-off-by: Roman Gushchin <guro@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
  • Loading branch information
rgushchin committed Nov 25, 2020
1 parent 13b4c7a commit dfaf7b3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 99 deletions.
12 changes: 0 additions & 12 deletions include/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,6 @@ struct bpf_map_ops {
const struct bpf_iter_seq_info *iter_seq_info;
};

struct bpf_map_memory {
u32 pages;
struct user_struct *user;
};

struct bpf_map {
/* The first two cachelines with read-mostly members of which some
* are also accessed in fast-path (e.g. ops, max_entries).
Expand All @@ -162,7 +157,6 @@ struct bpf_map {
u32 btf_key_type_id;
u32 btf_value_type_id;
struct btf *btf;
struct bpf_map_memory memory;
#ifdef CONFIG_MEMCG_KMEM
struct mem_cgroup *memcg;
#endif
Expand Down Expand Up @@ -1223,12 +1217,6 @@ void bpf_map_inc_with_uref(struct bpf_map *map);
struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
void bpf_map_put_with_uref(struct bpf_map *map);
void bpf_map_put(struct bpf_map *map);
int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size);
void bpf_map_charge_finish(struct bpf_map_memory *mem);
void bpf_map_charge_move(struct bpf_map_memory *dst,
struct bpf_map_memory *src);
void *bpf_map_area_alloc(u64 size, int numa_node);
void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
void bpf_map_area_free(void *base);
Expand Down
94 changes: 15 additions & 79 deletions kernel/bpf/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,77 +343,6 @@ void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
map->numa_node = bpf_map_attr_numa_node(attr);
}

static int bpf_charge_memlock(struct user_struct *user, u32 pages)
{
unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;

if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
atomic_long_sub(pages, &user->locked_vm);
return -EPERM;
}
return 0;
}

static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
{
if (user)
atomic_long_sub(pages, &user->locked_vm);
}

int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
{
u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
struct user_struct *user;
int ret;

if (size >= U32_MAX - PAGE_SIZE)
return -E2BIG;

user = get_current_user();
ret = bpf_charge_memlock(user, pages);
if (ret) {
free_uid(user);
return ret;
}

mem->pages = pages;
mem->user = user;

return 0;
}

void bpf_map_charge_finish(struct bpf_map_memory *mem)
{
bpf_uncharge_memlock(mem->user, mem->pages);
free_uid(mem->user);
}

void bpf_map_charge_move(struct bpf_map_memory *dst,
struct bpf_map_memory *src)
{
*dst = *src;

/* Make sure src will not be used for the redundant uncharging. */
memset(src, 0, sizeof(struct bpf_map_memory));
}

int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
{
int ret;

ret = bpf_charge_memlock(map->memory.user, pages);
if (ret)
return ret;
map->memory.pages += pages;
return ret;
}

void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
{
bpf_uncharge_memlock(map->memory.user, pages);
map->memory.pages -= pages;
}

static int bpf_map_alloc_id(struct bpf_map *map)
{
int id;
Expand Down Expand Up @@ -533,14 +462,11 @@ static void bpf_map_release_memcg(struct bpf_map *map)
static void bpf_map_free_deferred(struct work_struct *work)
{
struct bpf_map *map = container_of(work, struct bpf_map, work);
struct bpf_map_memory mem;

bpf_map_charge_move(&mem, &map->memory);
security_bpf_map_free(map);
bpf_map_release_memcg(map);
/* implementation dependent freeing */
map->ops->map_free(map);
bpf_map_charge_finish(&mem);
}

static void bpf_map_put_uref(struct bpf_map *map)
Expand Down Expand Up @@ -601,6 +527,19 @@ static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
}

#ifdef CONFIG_PROC_FS
/* Provides an approximation of the map's memory footprint.
* Used only to provide a backward compatibility and display
* a reasonable "memlock" info.
*/
static unsigned long bpf_map_memory_footprint(struct bpf_map *map)
{
unsigned long size;

size = round_up(map->key_size + bpf_map_value_size(map), 8);

return round_up(map->max_entries * size, PAGE_SIZE);
}

static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
{
const struct bpf_map *map = filp->private_data;
Expand All @@ -619,15 +558,15 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
"value_size:\t%u\n"
"max_entries:\t%u\n"
"map_flags:\t%#x\n"
"memlock:\t%llu\n"
"memlock:\t%lu\n"
"map_id:\t%u\n"
"frozen:\t%u\n",
map->map_type,
map->key_size,
map->value_size,
map->max_entries,
map->map_flags,
map->memory.pages * 1ULL << PAGE_SHIFT,
bpf_map_memory_footprint(map),
map->id,
READ_ONCE(map->frozen));
if (type) {
Expand Down Expand Up @@ -870,7 +809,6 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,
static int map_create(union bpf_attr *attr)
{
int numa_node = bpf_map_attr_numa_node(attr);
struct bpf_map_memory mem;
struct bpf_map *map;
int f_flags;
int err;
Expand Down Expand Up @@ -969,9 +907,7 @@ static int map_create(union bpf_attr *attr)
security_bpf_map_free(map);
free_map:
btf_put(map->btf);
bpf_map_charge_move(&mem, &map->memory);
map->ops->map_free(map);
bpf_map_charge_finish(&mem);
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/bpf_iter_bpf_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ int dump_bpf_map(struct bpf_iter__bpf_map *ctx)

BPF_SEQ_PRINTF(seq, "%8u %8ld %8ld %10lu\n", map->id, map->refcnt.counter,
map->usercnt.counter,
map->memory.user->locked_vm.counter);
0LLU);
return 0;
}
7 changes: 0 additions & 7 deletions tools/testing/selftests/bpf/progs/map_ptr_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@ __u32 g_line = 0;
return 0; \
})

struct bpf_map_memory {
__u32 pages;
} __attribute__((preserve_access_index));

struct bpf_map {
enum bpf_map_type map_type;
__u32 key_size;
__u32 value_size;
__u32 max_entries;
__u32 id;
struct bpf_map_memory memory;
} __attribute__((preserve_access_index));

static inline int check_bpf_map_fields(struct bpf_map *map, __u32 key_size,
Expand All @@ -47,7 +42,6 @@ static inline int check_bpf_map_fields(struct bpf_map *map, __u32 key_size,
VERIFY(map->value_size == value_size);
VERIFY(map->max_entries == max_entries);
VERIFY(map->id > 0);
VERIFY(map->memory.pages > 0);

return 1;
}
Expand All @@ -60,7 +54,6 @@ static inline int check_bpf_map_ptr(struct bpf_map *indirect,
VERIFY(indirect->value_size == direct->value_size);
VERIFY(indirect->max_entries == direct->max_entries);
VERIFY(indirect->id == direct->id);
VERIFY(indirect->memory.pages == direct->memory.pages);

return 1;
}
Expand Down

0 comments on commit dfaf7b3

Please sign in to comment.