aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/bpf/syscall.c
diff options
context:
space:
mode:
authorDaniel Borkmann <daniel@iogearbox.net>2016-06-30 11:24:44 -0400
committerDavid S. Miller <davem@davemloft.net>2016-07-01 16:00:47 -0400
commit113214be7f6c98dd6d0435e4765aea8dea91662c (patch)
tree31d2c41c7af6991401c3aff11e7ade3317b4a614 /kernel/bpf/syscall.c
parent1aacde3d22c42281236155c1ef6d7a5aa32a826b (diff)
bpf: refactor bpf_prog_get and type check into helper
Since bpf_prog_get() and program type check is used in a couple of places, refactor this into a small helper function that we can make use of. Since the non RO prog->aux part is not used in performance critical paths and a program destruction via RCU is rather very unlikley when doing the put, we shouldn't have an issue just doing the bpf_prog_get() + prog->type != type check, but actually not taking the ref at all (due to being in fdget() / fdput() section of the bpf fd) is even cleaner and makes the diff smaller as well, so just go for that. Callsites are changed to make use of the new helper where possible. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'kernel/bpf/syscall.c')
-rw-r--r--kernel/bpf/syscall.c27
1 files changed, 19 insertions, 8 deletions
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index f6806a1d7ed9..22863d9872b1 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -657,7 +657,7 @@ int bpf_prog_new_fd(struct bpf_prog *prog)
657 O_RDWR | O_CLOEXEC); 657 O_RDWR | O_CLOEXEC);
658} 658}
659 659
660static struct bpf_prog *__bpf_prog_get(struct fd f) 660static struct bpf_prog *____bpf_prog_get(struct fd f)
661{ 661{
662 if (!f.file) 662 if (!f.file)
663 return ERR_PTR(-EBADF); 663 return ERR_PTR(-EBADF);
@@ -678,24 +678,35 @@ struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
678 return prog; 678 return prog;
679} 679}
680 680
681/* called by sockets/tracing/seccomp before attaching program to an event 681static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
682 * pairs with bpf_prog_put()
683 */
684struct bpf_prog *bpf_prog_get(u32 ufd)
685{ 682{
686 struct fd f = fdget(ufd); 683 struct fd f = fdget(ufd);
687 struct bpf_prog *prog; 684 struct bpf_prog *prog;
688 685
689 prog = __bpf_prog_get(f); 686 prog = ____bpf_prog_get(f);
690 if (IS_ERR(prog)) 687 if (IS_ERR(prog))
691 return prog; 688 return prog;
689 if (type && prog->type != *type) {
690 prog = ERR_PTR(-EINVAL);
691 goto out;
692 }
692 693
693 prog = bpf_prog_inc(prog); 694 prog = bpf_prog_inc(prog);
695out:
694 fdput(f); 696 fdput(f);
695
696 return prog; 697 return prog;
697} 698}
698EXPORT_SYMBOL_GPL(bpf_prog_get); 699
700struct bpf_prog *bpf_prog_get(u32 ufd)
701{
702 return __bpf_prog_get(ufd, NULL);
703}
704
705struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
706{
707 return __bpf_prog_get(ufd, &type);
708}
709EXPORT_SYMBOL_GPL(bpf_prog_get_type);
699 710
700/* last field in 'union bpf_attr' used by this command */ 711/* last field in 'union bpf_attr' used by this command */
701#define BPF_PROG_LOAD_LAST_FIELD kern_version 712#define BPF_PROG_LOAD_LAST_FIELD kern_version