Skip to content

Commit ce3aa9c

Browse files
jsitnickiAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf, netns: Handle multiple link attachments
Extend the BPF netns link callbacks to rebuild (grow/shrink) or update the prog_array at given position when link gets attached/updated/released. This let's us lift the limit of having just one link attached for the new attach type introduced by subsequent patch. No functional changes intended. Signed-off-by: Jakub Sitnicki <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent bfdfa51 commit ce3aa9c

File tree

3 files changed

+139
-9
lines changed

3 files changed

+139
-9
lines changed

include/linux/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,9 @@ int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
928928

929929
void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
930930
struct bpf_prog *old_prog);
931+
int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
932+
int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
933+
struct bpf_prog *prog);
931934
int bpf_prog_array_copy_info(struct bpf_prog_array *array,
932935
u32 *prog_ids, u32 request_cnt,
933936
u32 *prog_cnt);

kernel/bpf/core.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,61 @@ void bpf_prog_array_delete_safe(struct bpf_prog_array *array,
19581958
}
19591959
}
19601960

1961+
/**
1962+
* bpf_prog_array_delete_safe_at() - Replaces the program at the given
1963+
* index into the program array with
1964+
* a dummy no-op program.
1965+
* @array: a bpf_prog_array
1966+
* @index: the index of the program to replace
1967+
*
1968+
* Skips over dummy programs, by not counting them, when calculating
1969+
* the the position of the program to replace.
1970+
*
1971+
* Return:
1972+
* * 0 - Success
1973+
* * -EINVAL - Invalid index value. Must be a non-negative integer.
1974+
* * -ENOENT - Index out of range
1975+
*/
1976+
int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index)
1977+
{
1978+
return bpf_prog_array_update_at(array, index, &dummy_bpf_prog.prog);
1979+
}
1980+
1981+
/**
1982+
* bpf_prog_array_update_at() - Updates the program at the given index
1983+
* into the program array.
1984+
* @array: a bpf_prog_array
1985+
* @index: the index of the program to update
1986+
* @prog: the program to insert into the array
1987+
*
1988+
* Skips over dummy programs, by not counting them, when calculating
1989+
* the position of the program to update.
1990+
*
1991+
* Return:
1992+
* * 0 - Success
1993+
* * -EINVAL - Invalid index value. Must be a non-negative integer.
1994+
* * -ENOENT - Index out of range
1995+
*/
1996+
int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
1997+
struct bpf_prog *prog)
1998+
{
1999+
struct bpf_prog_array_item *item;
2000+
2001+
if (unlikely(index < 0))
2002+
return -EINVAL;
2003+
2004+
for (item = array->items; item->prog; item++) {
2005+
if (item->prog == &dummy_bpf_prog.prog)
2006+
continue;
2007+
if (!index) {
2008+
WRITE_ONCE(item->prog, prog);
2009+
return 0;
2010+
}
2011+
index--;
2012+
}
2013+
return -ENOENT;
2014+
}
2015+
19612016
int bpf_prog_array_copy(struct bpf_prog_array *old_array,
19622017
struct bpf_prog *exclude_prog,
19632018
struct bpf_prog *include_prog,

kernel/bpf/net_namespace.c

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,50 @@ static void netns_bpf_run_array_detach(struct net *net,
3636
bpf_prog_array_free(run_array);
3737
}
3838

39+
static int link_index(struct net *net, enum netns_bpf_attach_type type,
40+
struct bpf_netns_link *link)
41+
{
42+
struct bpf_netns_link *pos;
43+
int i = 0;
44+
45+
list_for_each_entry(pos, &net->bpf.links[type], node) {
46+
if (pos == link)
47+
return i;
48+
i++;
49+
}
50+
return -ENOENT;
51+
}
52+
53+
static int link_count(struct net *net, enum netns_bpf_attach_type type)
54+
{
55+
struct list_head *pos;
56+
int i = 0;
57+
58+
list_for_each(pos, &net->bpf.links[type])
59+
i++;
60+
return i;
61+
}
62+
63+
static void fill_prog_array(struct net *net, enum netns_bpf_attach_type type,
64+
struct bpf_prog_array *prog_array)
65+
{
66+
struct bpf_netns_link *pos;
67+
unsigned int i = 0;
68+
69+
list_for_each_entry(pos, &net->bpf.links[type], node) {
70+
prog_array->items[i].prog = pos->link.prog;
71+
i++;
72+
}
73+
}
74+
3975
static void bpf_netns_link_release(struct bpf_link *link)
4076
{
4177
struct bpf_netns_link *net_link =
4278
container_of(link, struct bpf_netns_link, link);
4379
enum netns_bpf_attach_type type = net_link->netns_type;
80+
struct bpf_prog_array *old_array, *new_array;
4481
struct net *net;
82+
int cnt, idx;
4583

4684
mutex_lock(&netns_bpf_mutex);
4785

@@ -53,9 +91,27 @@ static void bpf_netns_link_release(struct bpf_link *link)
5391
if (!net)
5492
goto out_unlock;
5593

56-
netns_bpf_run_array_detach(net, type);
94+
/* Remember link position in case of safe delete */
95+
idx = link_index(net, type, net_link);
5796
list_del(&net_link->node);
5897

98+
cnt = link_count(net, type);
99+
if (!cnt) {
100+
netns_bpf_run_array_detach(net, type);
101+
goto out_unlock;
102+
}
103+
104+
old_array = rcu_dereference_protected(net->bpf.run_array[type],
105+
lockdep_is_held(&netns_bpf_mutex));
106+
new_array = bpf_prog_array_alloc(cnt, GFP_KERNEL);
107+
if (!new_array) {
108+
WARN_ON(bpf_prog_array_delete_safe_at(old_array, idx));
109+
goto out_unlock;
110+
}
111+
fill_prog_array(net, type, new_array);
112+
rcu_assign_pointer(net->bpf.run_array[type], new_array);
113+
bpf_prog_array_free(old_array);
114+
59115
out_unlock:
60116
mutex_unlock(&netns_bpf_mutex);
61117
}
@@ -77,7 +133,7 @@ static int bpf_netns_link_update_prog(struct bpf_link *link,
77133
enum netns_bpf_attach_type type = net_link->netns_type;
78134
struct bpf_prog_array *run_array;
79135
struct net *net;
80-
int ret = 0;
136+
int idx, ret;
81137

82138
if (old_prog && old_prog != link->prog)
83139
return -EPERM;
@@ -95,7 +151,10 @@ static int bpf_netns_link_update_prog(struct bpf_link *link,
95151

96152
run_array = rcu_dereference_protected(net->bpf.run_array[type],
97153
lockdep_is_held(&netns_bpf_mutex));
98-
WRITE_ONCE(run_array->items[0].prog, new_prog);
154+
idx = link_index(net, type, net_link);
155+
ret = bpf_prog_array_update_at(run_array, idx, new_prog);
156+
if (ret)
157+
goto out_unlock;
99158

100159
old_prog = xchg(&link->prog, new_prog);
101160
bpf_prog_put(old_prog);
@@ -309,18 +368,28 @@ int netns_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
309368
return ret;
310369
}
311370

371+
static int netns_bpf_max_progs(enum netns_bpf_attach_type type)
372+
{
373+
switch (type) {
374+
case NETNS_BPF_FLOW_DISSECTOR:
375+
return 1;
376+
default:
377+
return 0;
378+
}
379+
}
380+
312381
static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
313382
enum netns_bpf_attach_type type)
314383
{
315384
struct bpf_netns_link *net_link =
316385
container_of(link, struct bpf_netns_link, link);
317386
struct bpf_prog_array *run_array;
318-
int err;
387+
int cnt, err;
319388

320389
mutex_lock(&netns_bpf_mutex);
321390

322-
/* Allow attaching only one prog or link for now */
323-
if (!list_empty(&net->bpf.links[type])) {
391+
cnt = link_count(net, type);
392+
if (cnt >= netns_bpf_max_progs(type)) {
324393
err = -E2BIG;
325394
goto out_unlock;
326395
}
@@ -341,16 +410,19 @@ static int netns_bpf_link_attach(struct net *net, struct bpf_link *link,
341410
if (err)
342411
goto out_unlock;
343412

344-
run_array = bpf_prog_array_alloc(1, GFP_KERNEL);
413+
run_array = bpf_prog_array_alloc(cnt + 1, GFP_KERNEL);
345414
if (!run_array) {
346415
err = -ENOMEM;
347416
goto out_unlock;
348417
}
349-
run_array->items[0].prog = link->prog;
350-
rcu_assign_pointer(net->bpf.run_array[type], run_array);
351418

352419
list_add_tail(&net_link->node, &net->bpf.links[type]);
353420

421+
fill_prog_array(net, type, run_array);
422+
run_array = rcu_replace_pointer(net->bpf.run_array[type], run_array,
423+
lockdep_is_held(&netns_bpf_mutex));
424+
bpf_prog_array_free(run_array);
425+
354426
out_unlock:
355427
mutex_unlock(&netns_bpf_mutex);
356428
return err;

0 commit comments

Comments
 (0)