xref: /linux/net/core/sock_reuseport.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * To speed up listener socket lookup, create an array to store all sockets
4  * listening on the same port.  This allows a decision to be made after finding
5  * the first socket.  An optional BPF program can also be configured for
6  * selecting the socket index from the array of available sockets.
7  */
8 
9 #include <net/inet_connection_sock.h>
10 #include <net/ip.h>
11 #include <net/sock_reuseport.h>
12 #include <linux/bpf.h>
13 #include <linux/idr.h>
14 #include <linux/filter.h>
15 #include <linux/rcupdate.h>
16 
17 #define INIT_SOCKS 128
18 
19 DEFINE_SPINLOCK(reuseport_lock);
20 
21 static DEFINE_IDA(reuseport_ida);
22 static int reuseport_resurrect(struct sock *sk, struct sock_reuseport *old_reuse,
23 			       struct sock_reuseport *reuse, bool bind_inany);
24 
25 void reuseport_has_conns_set(struct sock *sk)
26 {
27 	struct sock_reuseport *reuse;
28 
29 	if (!rcu_access_pointer(sk->sk_reuseport_cb))
30 		return;
31 
32 	spin_lock_bh(&reuseport_lock);
33 	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
34 					  lockdep_is_held(&reuseport_lock));
35 	if (likely(reuse))
36 		reuse->has_conns = 1;
37 	spin_unlock_bh(&reuseport_lock);
38 }
39 EXPORT_SYMBOL(reuseport_has_conns_set);
40 
41 static void __reuseport_get_incoming_cpu(struct sock_reuseport *reuse)
42 {
43 	/* Paired with READ_ONCE() in reuseport_select_sock_by_hash(). */
44 	WRITE_ONCE(reuse->incoming_cpu, reuse->incoming_cpu + 1);
45 }
46 
47 static void __reuseport_put_incoming_cpu(struct sock_reuseport *reuse)
48 {
49 	/* Paired with READ_ONCE() in reuseport_select_sock_by_hash(). */
50 	WRITE_ONCE(reuse->incoming_cpu, reuse->incoming_cpu - 1);
51 }
52 
53 static void reuseport_get_incoming_cpu(struct sock *sk, struct sock_reuseport *reuse)
54 {
55 	if (sk->sk_incoming_cpu >= 0)
56 		__reuseport_get_incoming_cpu(reuse);
57 }
58 
59 static void reuseport_put_incoming_cpu(struct sock *sk, struct sock_reuseport *reuse)
60 {
61 	if (sk->sk_incoming_cpu >= 0)
62 		__reuseport_put_incoming_cpu(reuse);
63 }
64 
65 void reuseport_update_incoming_cpu(struct sock *sk, int val)
66 {
67 	struct sock_reuseport *reuse;
68 	int old_sk_incoming_cpu;
69 
70 	if (unlikely(!rcu_access_pointer(sk->sk_reuseport_cb))) {
71 		/* Paired with REAE_ONCE() in sk_incoming_cpu_update()
72 		 * and compute_score().
73 		 */
74 		WRITE_ONCE(sk->sk_incoming_cpu, val);
75 		return;
76 	}
77 
78 	spin_lock_bh(&reuseport_lock);
79 
80 	/* This must be done under reuseport_lock to avoid a race with
81 	 * reuseport_grow(), which accesses sk->sk_incoming_cpu without
82 	 * lock_sock() when detaching a shutdown()ed sk.
83 	 *
84 	 * Paired with READ_ONCE() in reuseport_select_sock_by_hash().
85 	 */
86 	old_sk_incoming_cpu = sk->sk_incoming_cpu;
87 	WRITE_ONCE(sk->sk_incoming_cpu, val);
88 
89 	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
90 					  lockdep_is_held(&reuseport_lock));
91 
92 	/* reuseport_grow() has detached a closed sk. */
93 	if (!reuse)
94 		goto out;
95 
96 	if (old_sk_incoming_cpu < 0 && val >= 0)
97 		__reuseport_get_incoming_cpu(reuse);
98 	else if (old_sk_incoming_cpu >= 0 && val < 0)
99 		__reuseport_put_incoming_cpu(reuse);
100 
101 out:
102 	spin_unlock_bh(&reuseport_lock);
103 }
104 
105 static int reuseport_sock_index(struct sock *sk,
106 				const struct sock_reuseport *reuse,
107 				bool closed)
108 {
109 	int left, right;
110 
111 	if (!closed) {
112 		left = 0;
113 		right = reuse->num_socks;
114 	} else {
115 		left = reuse->max_socks - reuse->num_closed_socks;
116 		right = reuse->max_socks;
117 	}
118 
119 	for (; left < right; left++)
120 		if (reuse->socks[left] == sk)
121 			return left;
122 	return -1;
123 }
124 
125 static void __reuseport_add_sock(struct sock *sk,
126 				 struct sock_reuseport *reuse)
127 {
128 	WRITE_ONCE(reuse->socks[reuse->num_socks], sk);
129 	/* paired with smp_rmb() in reuseport_(select|migrate)_sock() */
130 	smp_wmb();
131 	WRITE_ONCE(reuse->num_socks, reuse->num_socks + 1);
132 	reuseport_get_incoming_cpu(sk, reuse);
133 }
134 
135 static bool __reuseport_detach_sock(struct sock *sk,
136 				    struct sock_reuseport *reuse)
137 {
138 	int i = reuseport_sock_index(sk, reuse, false);
139 
140 	if (i == -1)
141 		return false;
142 
143 	WRITE_ONCE(reuse->socks[i], reuse->socks[reuse->num_socks - 1]);
144 	WRITE_ONCE(reuse->num_socks, reuse->num_socks - 1);
145 	reuseport_put_incoming_cpu(sk, reuse);
146 
147 	return true;
148 }
149 
150 static void __reuseport_add_closed_sock(struct sock *sk,
151 					struct sock_reuseport *reuse)
152 {
153 	WRITE_ONCE(reuse->socks[reuse->max_socks - reuse->num_closed_socks - 1], sk);
154 	/* paired with READ_ONCE() in inet_csk_bind_conflict() */
155 	WRITE_ONCE(reuse->num_closed_socks, reuse->num_closed_socks + 1);
156 	reuseport_get_incoming_cpu(sk, reuse);
157 }
158 
159 static bool __reuseport_detach_closed_sock(struct sock *sk,
160 					   struct sock_reuseport *reuse)
161 {
162 	int i = reuseport_sock_index(sk, reuse, true);
163 
164 	if (i == -1)
165 		return false;
166 
167 	WRITE_ONCE(reuse->socks[i],
168 		   reuse->socks[reuse->max_socks - reuse->num_closed_socks]);
169 	/* paired with READ_ONCE() in inet_csk_bind_conflict() */
170 	WRITE_ONCE(reuse->num_closed_socks, reuse->num_closed_socks - 1);
171 	reuseport_put_incoming_cpu(sk, reuse);
172 
173 	return true;
174 }
175 
176 static struct sock_reuseport *__reuseport_alloc(unsigned int max_socks)
177 {
178 	struct sock_reuseport *reuse;
179 
180 	reuse = kzalloc_flex(*reuse, socks, max_socks, GFP_ATOMIC);
181 	if (!reuse)
182 		return NULL;
183 
184 	reuse->max_socks = max_socks;
185 
186 	RCU_INIT_POINTER(reuse->prog, NULL);
187 	return reuse;
188 }
189 
190 int reuseport_alloc(struct sock *sk, bool bind_inany)
191 {
192 	struct sock_reuseport *reuse;
193 	int id, ret = 0;
194 
195 	/* bh lock used since this function call may precede hlist lock in
196 	 * soft irq of receive path or setsockopt from process context
197 	 */
198 	spin_lock_bh(&reuseport_lock);
199 
200 	/* Allocation attempts can occur concurrently via the setsockopt path
201 	 * and the bind/hash path.  Nothing to do when we lose the race.
202 	 */
203 	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
204 					  lockdep_is_held(&reuseport_lock));
205 	if (reuse) {
206 		if (reuse->num_closed_socks) {
207 			/* sk was shutdown()ed before */
208 			ret = reuseport_resurrect(sk, reuse, NULL, bind_inany);
209 			goto out;
210 		}
211 
212 		/* Only set reuse->bind_inany if the bind_inany is true.
213 		 * Otherwise, it will overwrite the reuse->bind_inany
214 		 * which was set by the bind/hash path.
215 		 */
216 		if (bind_inany)
217 			reuse->bind_inany = bind_inany;
218 		goto out;
219 	}
220 
221 	reuse = __reuseport_alloc(INIT_SOCKS);
222 	if (!reuse) {
223 		ret = -ENOMEM;
224 		goto out;
225 	}
226 
227 	id = ida_alloc(&reuseport_ida, GFP_ATOMIC);
228 	if (id < 0) {
229 		kfree(reuse);
230 		ret = id;
231 		goto out;
232 	}
233 
234 	reuse->reuseport_id = id;
235 	reuse->bind_inany = bind_inany;
236 	reuse->socks[0] = sk;
237 	reuse->num_socks = 1;
238 	reuseport_get_incoming_cpu(sk, reuse);
239 	rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
240 
241 out:
242 	spin_unlock_bh(&reuseport_lock);
243 
244 	return ret;
245 }
246 EXPORT_SYMBOL(reuseport_alloc);
247 
248 static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse)
249 {
250 	struct sock_reuseport *more_reuse;
251 	u32 more_socks_size, i;
252 
253 	more_socks_size = reuse->max_socks * 2U;
254 	if (more_socks_size > U16_MAX) {
255 		if (reuse->num_closed_socks) {
256 			/* Make room by removing a closed sk.
257 			 * The child has already been migrated.
258 			 * Only reqsk left at this point.
259 			 */
260 			struct sock *sk;
261 
262 			sk = reuse->socks[reuse->max_socks - reuse->num_closed_socks];
263 			RCU_INIT_POINTER(sk->sk_reuseport_cb, NULL);
264 			__reuseport_detach_closed_sock(sk, reuse);
265 
266 			return reuse;
267 		}
268 
269 		return NULL;
270 	}
271 
272 	more_reuse = __reuseport_alloc(more_socks_size);
273 	if (!more_reuse)
274 		return NULL;
275 
276 	more_reuse->num_socks = reuse->num_socks;
277 	more_reuse->num_closed_socks = reuse->num_closed_socks;
278 	more_reuse->prog = reuse->prog;
279 	more_reuse->reuseport_id = reuse->reuseport_id;
280 	more_reuse->bind_inany = reuse->bind_inany;
281 	more_reuse->has_conns = reuse->has_conns;
282 	more_reuse->incoming_cpu = reuse->incoming_cpu;
283 
284 	memcpy(more_reuse->socks, reuse->socks,
285 	       reuse->num_socks * sizeof(struct sock *));
286 	memcpy(more_reuse->socks +
287 	       (more_reuse->max_socks - more_reuse->num_closed_socks),
288 	       reuse->socks + (reuse->max_socks - reuse->num_closed_socks),
289 	       reuse->num_closed_socks * sizeof(struct sock *));
290 	more_reuse->synq_overflow_ts = READ_ONCE(reuse->synq_overflow_ts);
291 
292 	for (i = 0; i < reuse->max_socks; ++i)
293 		rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
294 				   more_reuse);
295 
296 	/* Note: we use kfree_rcu here instead of reuseport_free_rcu so
297 	 * that reuse and more_reuse can temporarily share a reference
298 	 * to prog.
299 	 */
300 	kfree_rcu(reuse, rcu);
301 	return more_reuse;
302 }
303 
304 static void reuseport_free_rcu(struct rcu_head *head)
305 {
306 	struct sock_reuseport *reuse;
307 
308 	reuse = container_of(head, struct sock_reuseport, rcu);
309 	sk_reuseport_prog_free(rcu_dereference_protected(reuse->prog, 1));
310 	ida_free(&reuseport_ida, reuse->reuseport_id);
311 	kfree(reuse);
312 }
313 
314 /**
315  *  reuseport_add_sock - Add a socket to the reuseport group of another.
316  *  @sk:  New socket to add to the group.
317  *  @sk2: Socket belonging to the existing reuseport group.
318  *  @bind_inany: Whether or not the group is bound to a local INANY address.
319  *
320  *  May return ENOMEM and not add socket to group under memory pressure.
321  */
322 int reuseport_add_sock(struct sock *sk, struct sock *sk2, bool bind_inany)
323 {
324 	struct sock_reuseport *old_reuse, *reuse;
325 
326 	if (!rcu_access_pointer(sk2->sk_reuseport_cb)) {
327 		int err = reuseport_alloc(sk2, bind_inany);
328 
329 		if (err)
330 			return err;
331 	}
332 
333 	spin_lock_bh(&reuseport_lock);
334 	reuse = rcu_dereference_protected(sk2->sk_reuseport_cb,
335 					  lockdep_is_held(&reuseport_lock));
336 	old_reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
337 					      lockdep_is_held(&reuseport_lock));
338 	if (old_reuse && old_reuse->num_closed_socks) {
339 		/* sk was shutdown()ed before */
340 		int err = reuseport_resurrect(sk, old_reuse, reuse, reuse->bind_inany);
341 
342 		spin_unlock_bh(&reuseport_lock);
343 		return err;
344 	}
345 
346 	if (old_reuse && old_reuse->num_socks != 1) {
347 		spin_unlock_bh(&reuseport_lock);
348 		return -EBUSY;
349 	}
350 
351 	if (reuse->num_socks + reuse->num_closed_socks == reuse->max_socks) {
352 		reuse = reuseport_grow(reuse);
353 		if (!reuse) {
354 			spin_unlock_bh(&reuseport_lock);
355 			return -ENOMEM;
356 		}
357 	}
358 
359 	__reuseport_add_sock(sk, reuse);
360 	rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
361 
362 	spin_unlock_bh(&reuseport_lock);
363 
364 	if (old_reuse)
365 		call_rcu(&old_reuse->rcu, reuseport_free_rcu);
366 	return 0;
367 }
368 EXPORT_SYMBOL(reuseport_add_sock);
369 
370 static int reuseport_resurrect(struct sock *sk, struct sock_reuseport *old_reuse,
371 			       struct sock_reuseport *reuse, bool bind_inany)
372 {
373 	if (old_reuse == reuse) {
374 		/* If sk was in the same reuseport group, just pop sk out of
375 		 * the closed section and push sk into the listening section.
376 		 */
377 		__reuseport_detach_closed_sock(sk, old_reuse);
378 		__reuseport_add_sock(sk, old_reuse);
379 		return 0;
380 	}
381 
382 	if (!reuse) {
383 		/* In bind()/listen() path, we cannot carry over the eBPF prog
384 		 * for the shutdown()ed socket. In setsockopt() path, we should
385 		 * not change the eBPF prog of listening sockets by attaching a
386 		 * prog to the shutdown()ed socket. Thus, we will allocate a new
387 		 * reuseport group and detach sk from the old group.
388 		 */
389 		int id;
390 
391 		reuse = __reuseport_alloc(INIT_SOCKS);
392 		if (!reuse)
393 			return -ENOMEM;
394 
395 		id = ida_alloc(&reuseport_ida, GFP_ATOMIC);
396 		if (id < 0) {
397 			kfree(reuse);
398 			return id;
399 		}
400 
401 		reuse->reuseport_id = id;
402 		reuse->bind_inany = bind_inany;
403 	} else {
404 		/* Move sk from the old group to the new one if
405 		 * - all the other listeners in the old group were close()d or
406 		 *   shutdown()ed, and then sk2 has listen()ed on the same port
407 		 * OR
408 		 * - sk listen()ed without bind() (or with autobind), was
409 		 *   shutdown()ed, and then listen()s on another port which
410 		 *   sk2 listen()s on.
411 		 */
412 		if (reuse->num_socks + reuse->num_closed_socks == reuse->max_socks) {
413 			reuse = reuseport_grow(reuse);
414 			if (!reuse)
415 				return -ENOMEM;
416 		}
417 	}
418 
419 	__reuseport_detach_closed_sock(sk, old_reuse);
420 	__reuseport_add_sock(sk, reuse);
421 	rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
422 
423 	if (old_reuse->num_socks + old_reuse->num_closed_socks == 0)
424 		call_rcu(&old_reuse->rcu, reuseport_free_rcu);
425 
426 	return 0;
427 }
428 
429 void reuseport_detach_sock(struct sock *sk)
430 {
431 	struct sock_reuseport *reuse;
432 
433 	spin_lock_bh(&reuseport_lock);
434 	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
435 					  lockdep_is_held(&reuseport_lock));
436 
437 	/* reuseport_grow() has detached a closed sk */
438 	if (!reuse)
439 		goto out;
440 
441 	/* Notify the bpf side. The sk may be added to a sockarray
442 	 * map. If so, sockarray logic will remove it from the map.
443 	 *
444 	 * Other bpf map types that work with reuseport, like sockmap,
445 	 * don't need an explicit callback from here. They override sk
446 	 * unhash/close ops to remove the sk from the map before we
447 	 * get to this point.
448 	 */
449 	bpf_sk_reuseport_detach(sk);
450 
451 	rcu_assign_pointer(sk->sk_reuseport_cb, NULL);
452 
453 	if (!__reuseport_detach_closed_sock(sk, reuse))
454 		__reuseport_detach_sock(sk, reuse);
455 
456 	if (reuse->num_socks + reuse->num_closed_socks == 0)
457 		call_rcu(&reuse->rcu, reuseport_free_rcu);
458 
459 out:
460 	spin_unlock_bh(&reuseport_lock);
461 }
462 EXPORT_SYMBOL(reuseport_detach_sock);
463 
464 void reuseport_stop_listen_sock(struct sock *sk)
465 {
466 	if (sk->sk_protocol == IPPROTO_TCP) {
467 		struct sock_reuseport *reuse;
468 		struct bpf_prog *prog;
469 
470 		spin_lock_bh(&reuseport_lock);
471 
472 		reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
473 						  lockdep_is_held(&reuseport_lock));
474 		prog = rcu_dereference_protected(reuse->prog,
475 						 lockdep_is_held(&reuseport_lock));
476 
477 		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req) ||
478 		    (prog && prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE)) {
479 			/* Migration capable, move sk from the listening section
480 			 * to the closed section.
481 			 */
482 			bpf_sk_reuseport_detach(sk);
483 
484 			__reuseport_detach_sock(sk, reuse);
485 			__reuseport_add_closed_sock(sk, reuse);
486 
487 			spin_unlock_bh(&reuseport_lock);
488 			return;
489 		}
490 
491 		spin_unlock_bh(&reuseport_lock);
492 	}
493 
494 	/* Not capable to do migration, detach immediately */
495 	reuseport_detach_sock(sk);
496 }
497 EXPORT_SYMBOL(reuseport_stop_listen_sock);
498 
499 static struct sock *run_bpf_filter(struct sock_reuseport *reuse, u16 socks,
500 				   struct bpf_prog *prog, struct sk_buff *skb,
501 				   int hdr_len)
502 {
503 	struct sk_buff *nskb = NULL;
504 	u32 index;
505 
506 	if (skb_shared(skb)) {
507 		nskb = skb_clone(skb, GFP_ATOMIC);
508 		if (!nskb)
509 			return NULL;
510 		skb = nskb;
511 	}
512 
513 	/* temporarily advance data past protocol header */
514 	if (!pskb_pull(skb, hdr_len)) {
515 		kfree_skb(nskb);
516 		return NULL;
517 	}
518 	index = bpf_prog_run_save_cb(prog, skb);
519 	__skb_push(skb, hdr_len);
520 
521 	consume_skb(nskb);
522 
523 	if (index >= socks)
524 		return NULL;
525 
526 	return READ_ONCE(reuse->socks[index]);
527 }
528 
529 static struct sock *reuseport_select_sock_by_hash(struct sock_reuseport *reuse,
530 						  u32 hash, u16 num_socks)
531 {
532 	struct sock *first_valid_sk = NULL;
533 	int i, j;
534 
535 	i = j = reciprocal_scale(hash, num_socks);
536 	do {
537 		struct sock *sk = READ_ONCE(reuse->socks[i]);
538 
539 		if (sk->sk_state != TCP_ESTABLISHED) {
540 			/* Paired with WRITE_ONCE() in __reuseport_(get|put)_incoming_cpu(). */
541 			if (!READ_ONCE(reuse->incoming_cpu))
542 				return sk;
543 
544 			/* Paired with WRITE_ONCE() in reuseport_update_incoming_cpu(). */
545 			if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
546 				return sk;
547 
548 			if (!first_valid_sk)
549 				first_valid_sk = sk;
550 		}
551 
552 		i++;
553 		if (i >= num_socks)
554 			i = 0;
555 	} while (i != j);
556 
557 	return first_valid_sk;
558 }
559 
560 /**
561  *  reuseport_select_sock - Select a socket from an SO_REUSEPORT group.
562  *  @sk: First socket in the group.
563  *  @hash: When no BPF filter is available, use this hash to select.
564  *  @skb: skb to run through BPF filter.
565  *  @hdr_len: BPF filter expects skb data pointer at payload data.  If
566  *    the skb does not yet point at the payload, this parameter represents
567  *    how far the pointer needs to advance to reach the payload.
568  *  Returns a socket that should receive the packet (or NULL on error).
569  */
570 struct sock *reuseport_select_sock(struct sock *sk,
571 				   u32 hash,
572 				   struct sk_buff *skb,
573 				   int hdr_len)
574 {
575 	struct sock_reuseport *reuse;
576 	struct bpf_prog *prog;
577 	struct sock *sk2 = NULL;
578 	u16 socks;
579 
580 	rcu_read_lock();
581 	reuse = rcu_dereference(sk->sk_reuseport_cb);
582 
583 	/* if memory allocation failed or add call is not yet complete */
584 	if (!reuse)
585 		goto out;
586 
587 	prog = rcu_dereference(reuse->prog);
588 	socks = READ_ONCE(reuse->num_socks);
589 	if (likely(socks)) {
590 		/* paired with smp_wmb() in __reuseport_add_sock() */
591 		smp_rmb();
592 
593 		if (!prog || !skb)
594 			goto select_by_hash;
595 
596 		if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
597 			sk2 = bpf_run_sk_reuseport(reuse, sk, prog, skb, NULL, hash);
598 		else
599 			sk2 = run_bpf_filter(reuse, socks, prog, skb, hdr_len);
600 
601 select_by_hash:
602 		/* no bpf or invalid bpf result: fall back to hash usage */
603 		if (!sk2)
604 			sk2 = reuseport_select_sock_by_hash(reuse, hash, socks);
605 	}
606 
607 out:
608 	rcu_read_unlock();
609 	return sk2;
610 }
611 EXPORT_SYMBOL(reuseport_select_sock);
612 
613 /**
614  *  reuseport_migrate_sock - Select a socket from an SO_REUSEPORT group.
615  *  @sk: close()ed or shutdown()ed socket in the group.
616  *  @migrating_sk: ESTABLISHED/SYN_RECV full socket in the accept queue or
617  *    NEW_SYN_RECV request socket during 3WHS.
618  *  @skb: skb to run through BPF filter.
619  *  Returns a socket (with sk_refcnt +1) that should accept the child socket
620  *  (or NULL on error).
621  */
622 struct sock *reuseport_migrate_sock(struct sock *sk,
623 				    struct sock *migrating_sk,
624 				    struct sk_buff *skb)
625 {
626 	struct sock_reuseport *reuse;
627 	struct sock *nsk = NULL;
628 	bool allocated = false;
629 	struct bpf_prog *prog;
630 	u16 socks;
631 	u32 hash;
632 
633 	rcu_read_lock();
634 
635 	reuse = rcu_dereference(sk->sk_reuseport_cb);
636 	if (!reuse)
637 		goto out;
638 
639 	socks = READ_ONCE(reuse->num_socks);
640 	if (unlikely(!socks))
641 		goto failure;
642 
643 	/* paired with smp_wmb() in __reuseport_add_sock() */
644 	smp_rmb();
645 
646 	hash = migrating_sk->sk_hash;
647 	prog = rcu_dereference(reuse->prog);
648 	if (!prog || prog->expected_attach_type != BPF_SK_REUSEPORT_SELECT_OR_MIGRATE) {
649 		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req))
650 			goto select_by_hash;
651 		goto failure;
652 	}
653 
654 	if (!skb) {
655 		skb = alloc_skb(0, GFP_ATOMIC);
656 		if (!skb)
657 			goto failure;
658 		allocated = true;
659 	}
660 
661 	nsk = bpf_run_sk_reuseport(reuse, sk, prog, skb, migrating_sk, hash);
662 
663 	if (allocated)
664 		kfree_skb(skb);
665 
666 select_by_hash:
667 	if (!nsk)
668 		nsk = reuseport_select_sock_by_hash(reuse, hash, socks);
669 
670 	if (IS_ERR_OR_NULL(nsk) || unlikely(!refcount_inc_not_zero(&nsk->sk_refcnt))) {
671 		nsk = NULL;
672 		goto failure;
673 	}
674 
675 out:
676 	rcu_read_unlock();
677 	return nsk;
678 
679 failure:
680 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMIGRATEREQFAILURE);
681 	goto out;
682 }
683 EXPORT_SYMBOL(reuseport_migrate_sock);
684 
685 int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog)
686 {
687 	struct sock_reuseport *reuse;
688 	struct bpf_prog *old_prog;
689 
690 	if (sk_unhashed(sk)) {
691 		int err;
692 
693 		if (!sk->sk_reuseport)
694 			return -EINVAL;
695 
696 		/* Do not put a shutdown()ed TCP listener back into the
697 		 * listening section of a reuseport group while it is in
698 		 * TCP_CLOSE: requests can still migrate to it.
699 		 */
700 		if (sk_is_tcp(sk) &&
701 		    inet_csk(sk)->unhashed_state == TCP_LISTEN)
702 			return -EINVAL;
703 
704 		err = reuseport_alloc(sk, false);
705 		if (err)
706 			return err;
707 	} else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
708 		/* The socket wasn't bound with SO_REUSEPORT */
709 		return -EINVAL;
710 	}
711 
712 	spin_lock_bh(&reuseport_lock);
713 	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
714 					  lockdep_is_held(&reuseport_lock));
715 	old_prog = rcu_dereference_protected(reuse->prog,
716 					     lockdep_is_held(&reuseport_lock));
717 	rcu_assign_pointer(reuse->prog, prog);
718 	spin_unlock_bh(&reuseport_lock);
719 
720 	sk_reuseport_prog_free(old_prog);
721 	return 0;
722 }
723 EXPORT_SYMBOL(reuseport_attach_prog);
724 
725 int reuseport_detach_prog(struct sock *sk)
726 {
727 	struct sock_reuseport *reuse;
728 	struct bpf_prog *old_prog;
729 
730 	old_prog = NULL;
731 	spin_lock_bh(&reuseport_lock);
732 	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
733 					  lockdep_is_held(&reuseport_lock));
734 
735 	/* reuse must be checked after acquiring the reuseport_lock
736 	 * because reuseport_grow() can detach a closed sk.
737 	 */
738 	if (!reuse) {
739 		spin_unlock_bh(&reuseport_lock);
740 		return sk->sk_reuseport ? -ENOENT : -EINVAL;
741 	}
742 
743 	if (sk_unhashed(sk) && reuse->num_closed_socks) {
744 		spin_unlock_bh(&reuseport_lock);
745 		return -ENOENT;
746 	}
747 
748 	old_prog = rcu_replace_pointer(reuse->prog, old_prog,
749 				       lockdep_is_held(&reuseport_lock));
750 	spin_unlock_bh(&reuseport_lock);
751 
752 	if (!old_prog)
753 		return -ENOENT;
754 
755 	sk_reuseport_prog_free(old_prog);
756 	return 0;
757 }
758 EXPORT_SYMBOL(reuseport_detach_prog);
759