Commit 3b701699 authored by Jakub Sitnicki's avatar Jakub Sitnicki Committed by Alexei Starovoitov

flow_dissector: Pull BPF program assignment up to bpf-netns

Prepare for using bpf_prog_array to store attached programs by moving out
code that updates the attached program out of flow dissector.

Managing bpf_prog_array is more involved than updating a single bpf_prog
pointer. This will let us do it all from one place, bpf/net_namespace.c, in
the subsequent patch.

No functional change intended.
Signed-off-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200625141357.910330-2-jakub@cloudflare.com
parent 517bbe19
...@@ -372,7 +372,8 @@ flow_dissector_init_keys(struct flow_dissector_key_control *key_control, ...@@ -372,7 +372,8 @@ flow_dissector_init_keys(struct flow_dissector_key_control *key_control,
} }
#ifdef CONFIG_BPF_SYSCALL #ifdef CONFIG_BPF_SYSCALL
int flow_dissector_bpf_prog_attach(struct net *net, struct bpf_prog *prog); int flow_dissector_bpf_prog_attach_check(struct net *net,
struct bpf_prog *prog);
#endif /* CONFIG_BPF_SYSCALL */ #endif /* CONFIG_BPF_SYSCALL */
#endif #endif
...@@ -189,6 +189,7 @@ int netns_bpf_prog_query(const union bpf_attr *attr, ...@@ -189,6 +189,7 @@ int netns_bpf_prog_query(const union bpf_attr *attr,
int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog) int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
{ {
enum netns_bpf_attach_type type; enum netns_bpf_attach_type type;
struct bpf_prog *attached;
struct net *net; struct net *net;
int ret; int ret;
...@@ -207,12 +208,26 @@ int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog) ...@@ -207,12 +208,26 @@ int netns_bpf_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
switch (type) { switch (type) {
case NETNS_BPF_FLOW_DISSECTOR: case NETNS_BPF_FLOW_DISSECTOR:
ret = flow_dissector_bpf_prog_attach(net, prog); ret = flow_dissector_bpf_prog_attach_check(net, prog);
break; break;
default: default:
ret = -EINVAL; ret = -EINVAL;
break; break;
} }
if (ret)
goto out_unlock;
attached = rcu_dereference_protected(net->bpf.progs[type],
lockdep_is_held(&netns_bpf_mutex));
if (attached == prog) {
/* The same program cannot be attached twice */
ret = -EINVAL;
goto out_unlock;
}
rcu_assign_pointer(net->bpf.progs[type], prog);
if (attached)
bpf_prog_put(attached);
out_unlock: out_unlock:
mutex_unlock(&netns_bpf_mutex); mutex_unlock(&netns_bpf_mutex);
...@@ -277,7 +292,7 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link, ...@@ -277,7 +292,7 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
switch (type) { switch (type) {
case NETNS_BPF_FLOW_DISSECTOR: case NETNS_BPF_FLOW_DISSECTOR:
err = flow_dissector_bpf_prog_attach(net, link->prog); err = flow_dissector_bpf_prog_attach_check(net, link->prog);
break; break;
default: default:
err = -EINVAL; err = -EINVAL;
...@@ -286,6 +301,7 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link, ...@@ -286,6 +301,7 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
if (err) if (err)
goto out_unlock; goto out_unlock;
rcu_assign_pointer(net->bpf.progs[type], link->prog);
net->bpf.links[type] = link; net->bpf.links[type] = link;
out_unlock: out_unlock:
......
...@@ -70,10 +70,10 @@ void skb_flow_dissector_init(struct flow_dissector *flow_dissector, ...@@ -70,10 +70,10 @@ void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
EXPORT_SYMBOL(skb_flow_dissector_init); EXPORT_SYMBOL(skb_flow_dissector_init);
#ifdef CONFIG_BPF_SYSCALL #ifdef CONFIG_BPF_SYSCALL
int flow_dissector_bpf_prog_attach(struct net *net, struct bpf_prog *prog) int flow_dissector_bpf_prog_attach_check(struct net *net,
struct bpf_prog *prog)
{ {
enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR; enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR;
struct bpf_prog *attached;
if (net == &init_net) { if (net == &init_net) {
/* BPF flow dissector in the root namespace overrides /* BPF flow dissector in the root namespace overrides
...@@ -97,15 +97,6 @@ int flow_dissector_bpf_prog_attach(struct net *net, struct bpf_prog *prog) ...@@ -97,15 +97,6 @@ int flow_dissector_bpf_prog_attach(struct net *net, struct bpf_prog *prog)
return -EEXIST; return -EEXIST;
} }
attached = rcu_dereference_protected(net->bpf.progs[type],
lockdep_is_held(&netns_bpf_mutex));
if (attached == prog)
/* The same program cannot be attached twice */
return -EINVAL;
rcu_assign_pointer(net->bpf.progs[type], prog);
if (attached)
bpf_prog_put(attached);
return 0; return 0;
} }
#endif /* CONFIG_BPF_SYSCALL */ #endif /* CONFIG_BPF_SYSCALL */
......
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