Skip to content

Commit 035ff35

Browse files
jsitnickiborkmann
authored andcommitted
net: Generate reuseport group ID on group creation
Commit 736b460 ("net: Add ID (if needed) to sock_reuseport and expose reuseport_lock") has introduced lazy generation of reuseport group IDs that survive group resize. By comparing the identifier we check if BPF reuseport program is not trying to select a socket from a BPF map that belongs to a different reuseport group than the one the packet is for. Because SOCKARRAY used to be the only BPF map type that can be used with reuseport BPF, it was possible to delay the generation of reuseport group ID until a socket from the group was inserted into BPF map for the first time. Now that SOCK{MAP,HASH} can be used with reuseport BPF we have two options, either generate the reuseport ID on map update, like SOCKARRAY does, or allocate an ID from the start when reuseport group gets created. This patch takes the latter approach to keep sockmap free of calls into reuseport code. This streamlines the reuseport_id access as its lifetime now matches the longevity of reuseport object. The cost of this simplification, however, is that we allocate reuseport IDs for all SO_REUSEPORT users. Even those that don't use SOCKARRAY in their setups. With the way identifiers are currently generated, we can have at most S32_MAX reuseport groups, which hopefully is sufficient. If we ever get close to the limit, we can switch an u64 counter like sk_cookie. Another change is that we now always call into SOCKARRAY logic to unlink the socket from the map when unhashing or closing the socket. Previously we did it only when at least one socket from the group was in a BPF map. It is worth noting that this doesn't conflict with sockmap tear-down in case a socket is in a SOCK{MAP,HASH} and belongs to a reuseport group. sockmap tear-down happens first: prot->unhash `- tcp_bpf_unhash |- tcp_bpf_remove | `- while (sk_psock_link_pop(psock)) | `- sk_psock_unlink | `- sock_map_delete_from_link | `- __sock_map_delete | `- sock_map_unref | `- sk_psock_put | `- sk_psock_drop | `- rcu_assign_sk_user_data(sk, NULL) `- inet_unhash `- reuseport_detach_sock `- bpf_sk_reuseport_detach `- WRITE_ONCE(sk->sk_user_data, NULL) Suggested-by: Martin Lau <kafai@fb.com> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20200218171023.844439-10-jakub@cloudflare.com
1 parent 9fed900 commit 035ff35

4 files changed

Lines changed: 22 additions & 47 deletions

File tree

‎include/net/sock_reuseport.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,4 @@ static inline bool reuseport_has_conns(struct sock *sk, bool set)
5555
return ret;
5656
}
5757

58-
int reuseport_get_id(struct sock_reuseport *reuse);
59-
6058
#endif /* _SOCK_REUSEPORT_H */

‎kernel/bpf/reuseport_array.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,6 @@ int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
305305
if (err)
306306
goto put_file_unlock;
307307

308-
/* Ensure reuse->reuseport_id is set */
309-
err = reuseport_get_id(reuse);
310-
if (err < 0)
311-
goto put_file_unlock;
312-
313308
WRITE_ONCE(nsk->sk_user_data, &array->ptrs[index]);
314309
rcu_assign_pointer(array->ptrs[index], nsk);
315310
free_osk = osk;

‎net/core/filter.c‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8641,18 +8641,8 @@ BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
86418641
}
86428642

86438643
if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
8644-
struct sock *sk;
8645-
8646-
if (unlikely(!reuse_kern->reuseport_id))
8647-
/* There is a small race between adding the
8648-
* sk to the map and setting the
8649-
* reuse_kern->reuseport_id.
8650-
* Treat it as the sk has not been added to
8651-
* the bpf map yet.
8652-
*/
8653-
return -ENOENT;
8644+
struct sock *sk = reuse_kern->sk;
86548645

8655-
sk = reuse_kern->sk;
86568646
if (sk->sk_protocol != selected_sk->sk_protocol)
86578647
return -EPROTOTYPE;
86588648
else if (sk->sk_family != selected_sk->sk_family)

‎net/core/sock_reuseport.c‎

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,8 @@
1616

1717
DEFINE_SPINLOCK(reuseport_lock);
1818

19-
#define REUSEPORT_MIN_ID 1
2019
static DEFINE_IDA(reuseport_ida);
2120

22-
int reuseport_get_id(struct sock_reuseport *reuse)
23-
{
24-
int id;
25-
26-
if (reuse->reuseport_id)
27-
return reuse->reuseport_id;
28-
29-
id = ida_simple_get(&reuseport_ida, REUSEPORT_MIN_ID, 0,
30-
/* Called under reuseport_lock */
31-
GFP_ATOMIC);
32-
if (id < 0)
33-
return id;
34-
35-
reuse->reuseport_id = id;
36-
37-
return reuse->reuseport_id;
38-
}
39-
4021
static struct sock_reuseport *__reuseport_alloc(unsigned int max_socks)
4122
{
4223
unsigned int size = sizeof(struct sock_reuseport) +
@@ -55,6 +36,7 @@ static struct sock_reuseport *__reuseport_alloc(unsigned int max_socks)
5536
int reuseport_alloc(struct sock *sk, bool bind_inany)
5637
{
5738
struct sock_reuseport *reuse;
39+
int id, ret = 0;
5840

5941
/* bh lock used since this function call may precede hlist lock in
6042
* soft irq of receive path or setsockopt from process context
@@ -78,10 +60,18 @@ int reuseport_alloc(struct sock *sk, bool bind_inany)
7860

7961
reuse = __reuseport_alloc(INIT_SOCKS);
8062
if (!reuse) {
81-
spin_unlock_bh(&reuseport_lock);
82-
return -ENOMEM;
63+
ret = -ENOMEM;
64+
goto out;
8365
}
8466

67+
id = ida_alloc(&reuseport_ida, GFP_ATOMIC);
68+
if (id < 0) {
69+
kfree(reuse);
70+
ret = id;
71+
goto out;
72+
}
73+
74+
reuse->reuseport_id = id;
8575
reuse->socks[0] = sk;
8676
reuse->num_socks = 1;
8777
reuse->bind_inany = bind_inany;
@@ -90,7 +80,7 @@ int reuseport_alloc(struct sock *sk, bool bind_inany)
9080
out:
9181
spin_unlock_bh(&reuseport_lock);
9282

93-
return 0;
83+
return ret;
9484
}
9585
EXPORT_SYMBOL(reuseport_alloc);
9686

@@ -134,8 +124,7 @@ static void reuseport_free_rcu(struct rcu_head *head)
134124

135125
reuse = container_of(head, struct sock_reuseport, rcu);
136126
sk_reuseport_prog_free(rcu_dereference_protected(reuse->prog, 1));
137-
if (reuse->reuseport_id)
138-
ida_simple_remove(&reuseport_ida, reuse->reuseport_id);
127+
ida_free(&reuseport_ida, reuse->reuseport_id);
139128
kfree(reuse);
140129
}
141130

@@ -199,12 +188,15 @@ void reuseport_detach_sock(struct sock *sk)
199188
reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
200189
lockdep_is_held(&reuseport_lock));
201190

202-
/* At least one of the sk in this reuseport group is added to
203-
* a bpf map. Notify the bpf side. The bpf map logic will
204-
* remove the sk if it is indeed added to a bpf map.
191+
/* Notify the bpf side. The sk may be added to a sockarray
192+
* map. If so, sockarray logic will remove it from the map.
193+
*
194+
* Other bpf map types that work with reuseport, like sockmap,
195+
* don't need an explicit callback from here. They override sk
196+
* unhash/close ops to remove the sk from the map before we
197+
* get to this point.
205198
*/
206-
if (reuse->reuseport_id)
207-
bpf_sk_reuseport_detach(sk);
199+
bpf_sk_reuseport_detach(sk);
208200

209201
rcu_assign_pointer(sk->sk_reuseport_cb, NULL);
210202

0 commit comments

Comments
 (0)