提交 aa79781b 编写于 作者: D Daniel Borkmann 提交者: David S. Miller

bpf: abstract anon_inode_getfd invocations

Since we're going to use anon_inode_getfd() invocations in more than just
the current places, make a helper function for both, so that we only need
to pass a map/prog pointer to the helper itself in order to get a fd. The
new helpers are called bpf_map_new_fd() and bpf_prog_new_fd().
Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
Acked-by: NAlexei Starovoitov <ast@kernel.org>
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
上级 1d6119ba
...@@ -111,6 +111,12 @@ static const struct file_operations bpf_map_fops = { ...@@ -111,6 +111,12 @@ static const struct file_operations bpf_map_fops = {
.release = bpf_map_release, .release = bpf_map_release,
}; };
static int bpf_map_new_fd(struct bpf_map *map)
{
return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
O_RDWR | O_CLOEXEC);
}
/* helper macro to check that unused fields 'union bpf_attr' are zero */ /* helper macro to check that unused fields 'union bpf_attr' are zero */
#define CHECK_ATTR(CMD) \ #define CHECK_ATTR(CMD) \
memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
...@@ -141,8 +147,7 @@ static int map_create(union bpf_attr *attr) ...@@ -141,8 +147,7 @@ static int map_create(union bpf_attr *attr)
if (err) if (err)
goto free_map; goto free_map;
err = anon_inode_getfd("bpf-map", &bpf_map_fops, map, O_RDWR | O_CLOEXEC); err = bpf_map_new_fd(map);
if (err < 0) if (err < 0)
/* failed to allocate fd */ /* failed to allocate fd */
goto free_map; goto free_map;
...@@ -538,6 +543,12 @@ static const struct file_operations bpf_prog_fops = { ...@@ -538,6 +543,12 @@ static const struct file_operations bpf_prog_fops = {
.release = bpf_prog_release, .release = bpf_prog_release,
}; };
static int bpf_prog_new_fd(struct bpf_prog *prog)
{
return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
O_RDWR | O_CLOEXEC);
}
static struct bpf_prog *get_prog(struct fd f) static struct bpf_prog *get_prog(struct fd f)
{ {
struct bpf_prog *prog; struct bpf_prog *prog;
...@@ -647,7 +658,7 @@ static int bpf_prog_load(union bpf_attr *attr) ...@@ -647,7 +658,7 @@ static int bpf_prog_load(union bpf_attr *attr)
if (err < 0) if (err < 0)
goto free_used_maps; goto free_used_maps;
err = anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, O_RDWR | O_CLOEXEC); err = bpf_prog_new_fd(prog);
if (err < 0) if (err < 0)
/* failed to allocate fd */ /* failed to allocate fd */
goto free_used_maps; goto free_used_maps;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册