Commit a1d1f079 authored by Daniel Borkmann's avatar Daniel Borkmann

Merge branch 'bpf-btf-id'

Martin KaFai Lau says:

====================
This series introduces BTF ID which is exposed through
the new BPF_BTF_GET_FD_BY_ID cmd, new "struct bpf_btf_info"
and new members in the "struct bpf_map_info".

Please see individual patch for details.
====================
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parents 53a7bdfb cd8b8928
...@@ -44,5 +44,7 @@ const struct btf_type *btf_type_id_size(const struct btf *btf, ...@@ -44,5 +44,7 @@ const struct btf_type *btf_type_id_size(const struct btf *btf,
u32 *ret_size); u32 *ret_size);
void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj,
struct seq_file *m); struct seq_file *m);
int btf_get_fd_by_id(u32 id);
u32 btf_id(const struct btf *btf);
#endif #endif
...@@ -96,6 +96,7 @@ enum bpf_cmd { ...@@ -96,6 +96,7 @@ enum bpf_cmd {
BPF_PROG_QUERY, BPF_PROG_QUERY,
BPF_RAW_TRACEPOINT_OPEN, BPF_RAW_TRACEPOINT_OPEN,
BPF_BTF_LOAD, BPF_BTF_LOAD,
BPF_BTF_GET_FD_BY_ID,
}; };
enum bpf_map_type { enum bpf_map_type {
...@@ -344,6 +345,7 @@ union bpf_attr { ...@@ -344,6 +345,7 @@ union bpf_attr {
__u32 start_id; __u32 start_id;
__u32 prog_id; __u32 prog_id;
__u32 map_id; __u32 map_id;
__u32 btf_id;
}; };
__u32 next_id; __u32 next_id;
__u32 open_flags; __u32 open_flags;
...@@ -2130,6 +2132,15 @@ struct bpf_map_info { ...@@ -2130,6 +2132,15 @@ struct bpf_map_info {
__u32 ifindex; __u32 ifindex;
__u64 netns_dev; __u64 netns_dev;
__u64 netns_ino; __u64 netns_ino;
__u32 btf_id;
__u32 btf_key_id;
__u32 btf_value_id;
} __attribute__((aligned(8)));
struct bpf_btf_info {
__aligned_u64 btf;
__u32 btf_size;
__u32 id;
} __attribute__((aligned(8))); } __attribute__((aligned(8)));
/* User bpf_sock_addr struct to access socket fields and sockaddr struct passed /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <linux/file.h> #include <linux/file.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/idr.h>
#include <linux/bpf_verifier.h> #include <linux/bpf_verifier.h>
#include <linux/btf.h> #include <linux/btf.h>
...@@ -179,6 +180,9 @@ ...@@ -179,6 +180,9 @@
i < btf_type_vlen(struct_type); \ i < btf_type_vlen(struct_type); \
i++, member++) i++, member++)
static DEFINE_IDR(btf_idr);
static DEFINE_SPINLOCK(btf_idr_lock);
struct btf { struct btf {
union { union {
struct btf_header *hdr; struct btf_header *hdr;
...@@ -193,6 +197,8 @@ struct btf { ...@@ -193,6 +197,8 @@ struct btf {
u32 types_size; u32 types_size;
u32 data_size; u32 data_size;
refcount_t refcnt; refcount_t refcnt;
u32 id;
struct rcu_head rcu;
}; };
enum verifier_phase { enum verifier_phase {
...@@ -598,6 +604,42 @@ static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t) ...@@ -598,6 +604,42 @@ static int btf_add_type(struct btf_verifier_env *env, struct btf_type *t)
return 0; return 0;
} }
static int btf_alloc_id(struct btf *btf)
{
int id;
idr_preload(GFP_KERNEL);
spin_lock_bh(&btf_idr_lock);
id = idr_alloc_cyclic(&btf_idr, btf, 1, INT_MAX, GFP_ATOMIC);
if (id > 0)
btf->id = id;
spin_unlock_bh(&btf_idr_lock);
idr_preload_end();
if (WARN_ON_ONCE(!id))
return -ENOSPC;
return id > 0 ? 0 : id;
}
static void btf_free_id(struct btf *btf)
{
unsigned long flags;
/*
* In map-in-map, calling map_delete_elem() on outer
* map will call bpf_map_put on the inner map.
* It will then eventually call btf_free_id()
* on the inner map. Some of the map_delete_elem()
* implementation may have irq disabled, so
* we need to use the _irqsave() version instead
* of the _bh() version.
*/
spin_lock_irqsave(&btf_idr_lock, flags);
idr_remove(&btf_idr, btf->id);
spin_unlock_irqrestore(&btf_idr_lock, flags);
}
static void btf_free(struct btf *btf) static void btf_free(struct btf *btf)
{ {
kvfree(btf->types); kvfree(btf->types);
...@@ -607,15 +649,19 @@ static void btf_free(struct btf *btf) ...@@ -607,15 +649,19 @@ static void btf_free(struct btf *btf)
kfree(btf); kfree(btf);
} }
static void btf_get(struct btf *btf) static void btf_free_rcu(struct rcu_head *rcu)
{ {
refcount_inc(&btf->refcnt); struct btf *btf = container_of(rcu, struct btf, rcu);
btf_free(btf);
} }
void btf_put(struct btf *btf) void btf_put(struct btf *btf)
{ {
if (btf && refcount_dec_and_test(&btf->refcnt)) if (btf && refcount_dec_and_test(&btf->refcnt)) {
btf_free(btf); btf_free_id(btf);
call_rcu(&btf->rcu, btf_free_rcu);
}
} }
static int env_resolve_init(struct btf_verifier_env *env) static int env_resolve_init(struct btf_verifier_env *env)
...@@ -1977,7 +2023,7 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size, ...@@ -1977,7 +2023,7 @@ static struct btf *btf_parse(void __user *btf_data, u32 btf_data_size,
if (!err) { if (!err) {
btf_verifier_env_free(env); btf_verifier_env_free(env);
btf_get(btf); refcount_set(&btf->refcnt, 1);
return btf; return btf;
} }
...@@ -2006,10 +2052,15 @@ const struct file_operations btf_fops = { ...@@ -2006,10 +2052,15 @@ const struct file_operations btf_fops = {
.release = btf_release, .release = btf_release,
}; };
static int __btf_new_fd(struct btf *btf)
{
return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
}
int btf_new_fd(const union bpf_attr *attr) int btf_new_fd(const union bpf_attr *attr)
{ {
struct btf *btf; struct btf *btf;
int fd; int ret;
btf = btf_parse(u64_to_user_ptr(attr->btf), btf = btf_parse(u64_to_user_ptr(attr->btf),
attr->btf_size, attr->btf_log_level, attr->btf_size, attr->btf_log_level,
...@@ -2018,12 +2069,23 @@ int btf_new_fd(const union bpf_attr *attr) ...@@ -2018,12 +2069,23 @@ int btf_new_fd(const union bpf_attr *attr)
if (IS_ERR(btf)) if (IS_ERR(btf))
return PTR_ERR(btf); return PTR_ERR(btf);
fd = anon_inode_getfd("btf", &btf_fops, btf, ret = btf_alloc_id(btf);
O_RDONLY | O_CLOEXEC); if (ret) {
if (fd < 0) btf_free(btf);
return ret;
}
/*
* The BTF ID is published to the userspace.
* All BTF free must go through call_rcu() from
* now on (i.e. free by calling btf_put()).
*/
ret = __btf_new_fd(btf);
if (ret < 0)
btf_put(btf); btf_put(btf);
return fd; return ret;
} }
struct btf *btf_get_by_fd(int fd) struct btf *btf_get_by_fd(int fd)
...@@ -2042,7 +2104,7 @@ struct btf *btf_get_by_fd(int fd) ...@@ -2042,7 +2104,7 @@ struct btf *btf_get_by_fd(int fd)
} }
btf = f.file->private_data; btf = f.file->private_data;
btf_get(btf); refcount_inc(&btf->refcnt);
fdput(f); fdput(f);
return btf; return btf;
...@@ -2052,13 +2114,55 @@ int btf_get_info_by_fd(const struct btf *btf, ...@@ -2052,13 +2114,55 @@ int btf_get_info_by_fd(const struct btf *btf,
const union bpf_attr *attr, const union bpf_attr *attr,
union bpf_attr __user *uattr) union bpf_attr __user *uattr)
{ {
void __user *udata = u64_to_user_ptr(attr->info.info); struct bpf_btf_info __user *uinfo;
u32 copy_len = min_t(u32, btf->data_size, struct bpf_btf_info info = {};
attr->info.info_len); u32 info_copy, btf_copy;
void __user *ubtf;
u32 uinfo_len;
uinfo = u64_to_user_ptr(attr->info.info);
uinfo_len = attr->info.info_len;
if (copy_to_user(udata, btf->data, copy_len) || info_copy = min_t(u32, uinfo_len, sizeof(info));
put_user(btf->data_size, &uattr->info.info_len)) if (copy_from_user(&info, uinfo, info_copy))
return -EFAULT;
info.id = btf->id;
ubtf = u64_to_user_ptr(info.btf);
btf_copy = min_t(u32, btf->data_size, info.btf_size);
if (copy_to_user(ubtf, btf->data, btf_copy))
return -EFAULT;
info.btf_size = btf->data_size;
if (copy_to_user(uinfo, &info, info_copy) ||
put_user(info_copy, &uattr->info.info_len))
return -EFAULT; return -EFAULT;
return 0; return 0;
} }
int btf_get_fd_by_id(u32 id)
{
struct btf *btf;
int fd;
rcu_read_lock();
btf = idr_find(&btf_idr, id);
if (!btf || !refcount_inc_not_zero(&btf->refcnt))
btf = ERR_PTR(-ENOENT);
rcu_read_unlock();
if (IS_ERR(btf))
return PTR_ERR(btf);
fd = __btf_new_fd(btf);
if (fd < 0)
btf_put(btf);
return fd;
}
u32 btf_id(const struct btf *btf)
{
return btf->id;
}
...@@ -252,7 +252,6 @@ static void bpf_map_free_deferred(struct work_struct *work) ...@@ -252,7 +252,6 @@ static void bpf_map_free_deferred(struct work_struct *work)
bpf_map_uncharge_memlock(map); bpf_map_uncharge_memlock(map);
security_bpf_map_free(map); security_bpf_map_free(map);
btf_put(map->btf);
/* implementation dependent freeing */ /* implementation dependent freeing */
map->ops->map_free(map); map->ops->map_free(map);
} }
...@@ -273,6 +272,7 @@ static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) ...@@ -273,6 +272,7 @@ static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
if (atomic_dec_and_test(&map->refcnt)) { if (atomic_dec_and_test(&map->refcnt)) {
/* bpf_map_free_id() must be called first */ /* bpf_map_free_id() must be called first */
bpf_map_free_id(map, do_idr_lock); bpf_map_free_id(map, do_idr_lock);
btf_put(map->btf);
INIT_WORK(&map->work, bpf_map_free_deferred); INIT_WORK(&map->work, bpf_map_free_deferred);
schedule_work(&map->work); schedule_work(&map->work);
} }
...@@ -2002,6 +2002,12 @@ static int bpf_map_get_info_by_fd(struct bpf_map *map, ...@@ -2002,6 +2002,12 @@ static int bpf_map_get_info_by_fd(struct bpf_map *map,
info.map_flags = map->map_flags; info.map_flags = map->map_flags;
memcpy(info.name, map->name, sizeof(map->name)); memcpy(info.name, map->name, sizeof(map->name));
if (map->btf) {
info.btf_id = btf_id(map->btf);
info.btf_key_id = map->btf_key_id;
info.btf_value_id = map->btf_value_id;
}
if (bpf_map_is_dev_bound(map)) { if (bpf_map_is_dev_bound(map)) {
err = bpf_map_offload_info_fill(&info, map); err = bpf_map_offload_info_fill(&info, map);
if (err) if (err)
...@@ -2015,6 +2021,21 @@ static int bpf_map_get_info_by_fd(struct bpf_map *map, ...@@ -2015,6 +2021,21 @@ static int bpf_map_get_info_by_fd(struct bpf_map *map,
return 0; return 0;
} }
static int bpf_btf_get_info_by_fd(struct btf *btf,
const union bpf_attr *attr,
union bpf_attr __user *uattr)
{
struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
u32 info_len = attr->info.info_len;
int err;
err = check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
if (err)
return err;
return btf_get_info_by_fd(btf, attr, uattr);
}
#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
...@@ -2038,7 +2059,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, ...@@ -2038,7 +2059,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
err = bpf_map_get_info_by_fd(f.file->private_data, attr, err = bpf_map_get_info_by_fd(f.file->private_data, attr,
uattr); uattr);
else if (f.file->f_op == &btf_fops) else if (f.file->f_op == &btf_fops)
err = btf_get_info_by_fd(f.file->private_data, attr, uattr); err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
else else
err = -EINVAL; err = -EINVAL;
...@@ -2059,6 +2080,19 @@ static int bpf_btf_load(const union bpf_attr *attr) ...@@ -2059,6 +2080,19 @@ static int bpf_btf_load(const union bpf_attr *attr)
return btf_new_fd(attr); return btf_new_fd(attr);
} }
#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
{
if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
return -EINVAL;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
return btf_get_fd_by_id(attr->btf_id);
}
SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
{ {
union bpf_attr attr = {}; union bpf_attr attr = {};
...@@ -2142,6 +2176,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz ...@@ -2142,6 +2176,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
case BPF_BTF_LOAD: case BPF_BTF_LOAD:
err = bpf_btf_load(&attr); err = bpf_btf_load(&attr);
break; break;
case BPF_BTF_GET_FD_BY_ID:
err = bpf_btf_get_fd_by_id(&attr);
break;
default: default:
err = -EINVAL; err = -EINVAL;
break; break;
......
...@@ -96,6 +96,7 @@ enum bpf_cmd { ...@@ -96,6 +96,7 @@ enum bpf_cmd {
BPF_PROG_QUERY, BPF_PROG_QUERY,
BPF_RAW_TRACEPOINT_OPEN, BPF_RAW_TRACEPOINT_OPEN,
BPF_BTF_LOAD, BPF_BTF_LOAD,
BPF_BTF_GET_FD_BY_ID,
}; };
enum bpf_map_type { enum bpf_map_type {
...@@ -343,6 +344,7 @@ union bpf_attr { ...@@ -343,6 +344,7 @@ union bpf_attr {
__u32 start_id; __u32 start_id;
__u32 prog_id; __u32 prog_id;
__u32 map_id; __u32 map_id;
__u32 btf_id;
}; };
__u32 next_id; __u32 next_id;
__u32 open_flags; __u32 open_flags;
...@@ -2129,6 +2131,15 @@ struct bpf_map_info { ...@@ -2129,6 +2131,15 @@ struct bpf_map_info {
__u32 ifindex; __u32 ifindex;
__u64 netns_dev; __u64 netns_dev;
__u64 netns_ino; __u64 netns_ino;
__u32 btf_id;
__u32 btf_key_id;
__u32 btf_value_id;
} __attribute__((aligned(8)));
struct bpf_btf_info {
__aligned_u64 btf;
__u32 btf_size;
__u32 id;
} __attribute__((aligned(8))); } __attribute__((aligned(8)));
/* User bpf_sock_addr struct to access socket fields and sockaddr struct passed /* User bpf_sock_addr struct to access socket fields and sockaddr struct passed
......
...@@ -458,6 +458,16 @@ int bpf_map_get_fd_by_id(__u32 id) ...@@ -458,6 +458,16 @@ int bpf_map_get_fd_by_id(__u32 id)
return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr)); return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
} }
int bpf_btf_get_fd_by_id(__u32 id)
{
union bpf_attr attr;
bzero(&attr, sizeof(attr));
attr.btf_id = id;
return sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
}
int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len) int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
{ {
union bpf_attr attr; union bpf_attr attr;
......
...@@ -98,6 +98,7 @@ int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); ...@@ -98,6 +98,7 @@ int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
int bpf_prog_get_fd_by_id(__u32 id); int bpf_prog_get_fd_by_id(__u32 id);
int bpf_map_get_fd_by_id(__u32 id); int bpf_map_get_fd_by_id(__u32 id);
int bpf_btf_get_fd_by_id(__u32 id);
int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len); int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len);
int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
__u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt); __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt);
......
This diff is collapsed.
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