Commit 80ee81e0 authored by Roman Gushchin's avatar Roman Gushchin Committed by Alexei Starovoitov

bpf: Eliminate rlimit-based memory accounting infra for bpf maps

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: default avatarRoman Gushchin <guro@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20201201215900.3569844-33-guro@fb.com
parent ab31be37
...@@ -138,11 +138,6 @@ struct bpf_map_ops { ...@@ -138,11 +138,6 @@ struct bpf_map_ops {
const struct bpf_iter_seq_info *iter_seq_info; const struct bpf_iter_seq_info *iter_seq_info;
}; };
struct bpf_map_memory {
u32 pages;
struct user_struct *user;
};
struct bpf_map { struct bpf_map {
/* The first two cachelines with read-mostly members of which some /* The first two cachelines with read-mostly members of which some
* are also accessed in fast-path (e.g. ops, max_entries). * are also accessed in fast-path (e.g. ops, max_entries).
...@@ -163,7 +158,6 @@ struct bpf_map { ...@@ -163,7 +158,6 @@ struct bpf_map {
u32 btf_key_type_id; u32 btf_key_type_id;
u32 btf_value_type_id; u32 btf_value_type_id;
struct btf *btf; struct btf *btf;
struct bpf_map_memory memory;
#ifdef CONFIG_MEMCG_KMEM #ifdef CONFIG_MEMCG_KMEM
struct mem_cgroup *memcg; struct mem_cgroup *memcg;
#endif #endif
...@@ -1224,12 +1218,6 @@ void bpf_map_inc_with_uref(struct bpf_map *map); ...@@ -1224,12 +1218,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); 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_with_uref(struct bpf_map *map);
void bpf_map_put(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_alloc(u64 size, int numa_node);
void *bpf_map_area_mmapable_alloc(u64 size, int numa_node); void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
void bpf_map_area_free(void *base); void bpf_map_area_free(void *base);
......
...@@ -128,7 +128,7 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) ...@@ -128,7 +128,7 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
return map; return map;
} }
static u32 bpf_map_value_size(struct bpf_map *map) static u32 bpf_map_value_size(const struct bpf_map *map)
{ {
if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
...@@ -346,77 +346,6 @@ void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) ...@@ -346,77 +346,6 @@ void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
map->numa_node = bpf_map_attr_numa_node(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) static int bpf_map_alloc_id(struct bpf_map *map)
{ {
int id; int id;
...@@ -524,14 +453,11 @@ static void bpf_map_release_memcg(struct bpf_map *map) ...@@ -524,14 +453,11 @@ static void bpf_map_release_memcg(struct bpf_map *map)
static void bpf_map_free_deferred(struct work_struct *work) static void bpf_map_free_deferred(struct work_struct *work)
{ {
struct bpf_map *map = container_of(work, struct bpf_map, 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); security_bpf_map_free(map);
bpf_map_release_memcg(map); bpf_map_release_memcg(map);
/* implementation dependent freeing */ /* implementation dependent freeing */
map->ops->map_free(map); map->ops->map_free(map);
bpf_map_charge_finish(&mem);
} }
static void bpf_map_put_uref(struct bpf_map *map) static void bpf_map_put_uref(struct bpf_map *map)
...@@ -592,6 +518,19 @@ static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) ...@@ -592,6 +518,19 @@ static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
} }
#ifdef CONFIG_PROC_FS #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(const 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) static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
{ {
const struct bpf_map *map = filp->private_data; const struct bpf_map *map = filp->private_data;
...@@ -610,7 +549,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) ...@@ -610,7 +549,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
"value_size:\t%u\n" "value_size:\t%u\n"
"max_entries:\t%u\n" "max_entries:\t%u\n"
"map_flags:\t%#x\n" "map_flags:\t%#x\n"
"memlock:\t%llu\n" "memlock:\t%lu\n"
"map_id:\t%u\n" "map_id:\t%u\n"
"frozen:\t%u\n", "frozen:\t%u\n",
map->map_type, map->map_type,
...@@ -618,7 +557,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) ...@@ -618,7 +557,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
map->value_size, map->value_size,
map->max_entries, map->max_entries,
map->map_flags, map->map_flags,
map->memory.pages * 1ULL << PAGE_SHIFT, bpf_map_memory_footprint(map),
map->id, map->id,
READ_ONCE(map->frozen)); READ_ONCE(map->frozen));
if (type) { if (type) {
...@@ -861,7 +800,6 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf, ...@@ -861,7 +800,6 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,
static int map_create(union bpf_attr *attr) static int map_create(union bpf_attr *attr)
{ {
int numa_node = bpf_map_attr_numa_node(attr); int numa_node = bpf_map_attr_numa_node(attr);
struct bpf_map_memory mem;
struct bpf_map *map; struct bpf_map *map;
int f_flags; int f_flags;
int err; int err;
...@@ -960,9 +898,7 @@ static int map_create(union bpf_attr *attr) ...@@ -960,9 +898,7 @@ static int map_create(union bpf_attr *attr)
security_bpf_map_free(map); security_bpf_map_free(map);
free_map: free_map:
btf_put(map->btf); btf_put(map->btf);
bpf_map_charge_move(&mem, &map->memory);
map->ops->map_free(map); map->ops->map_free(map);
bpf_map_charge_finish(&mem);
return err; return err;
} }
......
...@@ -23,6 +23,6 @@ int dump_bpf_map(struct bpf_iter__bpf_map *ctx) ...@@ -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, BPF_SEQ_PRINTF(seq, "%8u %8ld %8ld %10lu\n", map->id, map->refcnt.counter,
map->usercnt.counter, map->usercnt.counter,
map->memory.user->locked_vm.counter); 0LLU);
return 0; return 0;
} }
...@@ -26,17 +26,12 @@ __u32 g_line = 0; ...@@ -26,17 +26,12 @@ __u32 g_line = 0;
return 0; \ return 0; \
}) })
struct bpf_map_memory {
__u32 pages;
} __attribute__((preserve_access_index));
struct bpf_map { struct bpf_map {
enum bpf_map_type map_type; enum bpf_map_type map_type;
__u32 key_size; __u32 key_size;
__u32 value_size; __u32 value_size;
__u32 max_entries; __u32 max_entries;
__u32 id; __u32 id;
struct bpf_map_memory memory;
} __attribute__((preserve_access_index)); } __attribute__((preserve_access_index));
static inline int check_bpf_map_fields(struct bpf_map *map, __u32 key_size, static inline int check_bpf_map_fields(struct bpf_map *map, __u32 key_size,
...@@ -47,7 +42,6 @@ static inline int check_bpf_map_fields(struct bpf_map *map, __u32 key_size, ...@@ -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->value_size == value_size);
VERIFY(map->max_entries == max_entries); VERIFY(map->max_entries == max_entries);
VERIFY(map->id > 0); VERIFY(map->id > 0);
VERIFY(map->memory.pages > 0);
return 1; return 1;
} }
...@@ -60,7 +54,6 @@ static inline int check_bpf_map_ptr(struct bpf_map *indirect, ...@@ -60,7 +54,6 @@ static inline int check_bpf_map_ptr(struct bpf_map *indirect,
VERIFY(indirect->value_size == direct->value_size); VERIFY(indirect->value_size == direct->value_size);
VERIFY(indirect->max_entries == direct->max_entries); VERIFY(indirect->max_entries == direct->max_entries);
VERIFY(indirect->id == direct->id); VERIFY(indirect->id == direct->id);
VERIFY(indirect->memory.pages == direct->memory.pages);
return 1; return 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment