Skip to content

Commit aa79781

Browse files
borkmanndavem330
authored andcommitted
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: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1d6119b commit aa79781

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

kernel/bpf/syscall.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ static const struct file_operations bpf_map_fops = {
111111
.release = bpf_map_release,
112112
};
113113

114+
static int bpf_map_new_fd(struct bpf_map *map)
115+
{
116+
return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
117+
O_RDWR | O_CLOEXEC);
118+
}
119+
114120
/* helper macro to check that unused fields 'union bpf_attr' are zero */
115121
#define CHECK_ATTR(CMD) \
116122
memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
@@ -141,8 +147,7 @@ static int map_create(union bpf_attr *attr)
141147
if (err)
142148
goto free_map;
143149

144-
err = anon_inode_getfd("bpf-map", &bpf_map_fops, map, O_RDWR | O_CLOEXEC);
145-
150+
err = bpf_map_new_fd(map);
146151
if (err < 0)
147152
/* failed to allocate fd */
148153
goto free_map;
@@ -538,6 +543,12 @@ static const struct file_operations bpf_prog_fops = {
538543
.release = bpf_prog_release,
539544
};
540545

546+
static int bpf_prog_new_fd(struct bpf_prog *prog)
547+
{
548+
return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
549+
O_RDWR | O_CLOEXEC);
550+
}
551+
541552
static struct bpf_prog *get_prog(struct fd f)
542553
{
543554
struct bpf_prog *prog;
@@ -647,7 +658,7 @@ static int bpf_prog_load(union bpf_attr *attr)
647658
if (err < 0)
648659
goto free_used_maps;
649660

650-
err = anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, O_RDWR | O_CLOEXEC);
661+
err = bpf_prog_new_fd(prog);
651662
if (err < 0)
652663
/* failed to allocate fd */
653664
goto free_used_maps;

0 commit comments

Comments
 (0)