xref: /openbmc/linux/net/core/skmsg.c (revision 6cd70754)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3 
4 #include <linux/skmsg.h>
5 #include <linux/skbuff.h>
6 #include <linux/scatterlist.h>
7 
8 #include <net/sock.h>
9 #include <net/tcp.h>
10 #include <net/tls.h>
11 
12 static bool sk_msg_try_coalesce_ok(struct sk_msg *msg, int elem_first_coalesce)
13 {
14 	if (msg->sg.end > msg->sg.start &&
15 	    elem_first_coalesce < msg->sg.end)
16 		return true;
17 
18 	if (msg->sg.end < msg->sg.start &&
19 	    (elem_first_coalesce > msg->sg.start ||
20 	     elem_first_coalesce < msg->sg.end))
21 		return true;
22 
23 	return false;
24 }
25 
26 int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
27 		 int elem_first_coalesce)
28 {
29 	struct page_frag *pfrag = sk_page_frag(sk);
30 	int ret = 0;
31 
32 	len -= msg->sg.size;
33 	while (len > 0) {
34 		struct scatterlist *sge;
35 		u32 orig_offset;
36 		int use, i;
37 
38 		if (!sk_page_frag_refill(sk, pfrag))
39 			return -ENOMEM;
40 
41 		orig_offset = pfrag->offset;
42 		use = min_t(int, len, pfrag->size - orig_offset);
43 		if (!sk_wmem_schedule(sk, use))
44 			return -ENOMEM;
45 
46 		i = msg->sg.end;
47 		sk_msg_iter_var_prev(i);
48 		sge = &msg->sg.data[i];
49 
50 		if (sk_msg_try_coalesce_ok(msg, elem_first_coalesce) &&
51 		    sg_page(sge) == pfrag->page &&
52 		    sge->offset + sge->length == orig_offset) {
53 			sge->length += use;
54 		} else {
55 			if (sk_msg_full(msg)) {
56 				ret = -ENOSPC;
57 				break;
58 			}
59 
60 			sge = &msg->sg.data[msg->sg.end];
61 			sg_unmark_end(sge);
62 			sg_set_page(sge, pfrag->page, use, orig_offset);
63 			get_page(pfrag->page);
64 			sk_msg_iter_next(msg, end);
65 		}
66 
67 		sk_mem_charge(sk, use);
68 		msg->sg.size += use;
69 		pfrag->offset += use;
70 		len -= use;
71 	}
72 
73 	return ret;
74 }
75 EXPORT_SYMBOL_GPL(sk_msg_alloc);
76 
77 int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
78 		 u32 off, u32 len)
79 {
80 	int i = src->sg.start;
81 	struct scatterlist *sge = sk_msg_elem(src, i);
82 	struct scatterlist *sgd = NULL;
83 	u32 sge_len, sge_off;
84 
85 	while (off) {
86 		if (sge->length > off)
87 			break;
88 		off -= sge->length;
89 		sk_msg_iter_var_next(i);
90 		if (i == src->sg.end && off)
91 			return -ENOSPC;
92 		sge = sk_msg_elem(src, i);
93 	}
94 
95 	while (len) {
96 		sge_len = sge->length - off;
97 		if (sge_len > len)
98 			sge_len = len;
99 
100 		if (dst->sg.end)
101 			sgd = sk_msg_elem(dst, dst->sg.end - 1);
102 
103 		if (sgd &&
104 		    (sg_page(sge) == sg_page(sgd)) &&
105 		    (sg_virt(sge) + off == sg_virt(sgd) + sgd->length)) {
106 			sgd->length += sge_len;
107 			dst->sg.size += sge_len;
108 		} else if (!sk_msg_full(dst)) {
109 			sge_off = sge->offset + off;
110 			sk_msg_page_add(dst, sg_page(sge), sge_len, sge_off);
111 		} else {
112 			return -ENOSPC;
113 		}
114 
115 		off = 0;
116 		len -= sge_len;
117 		sk_mem_charge(sk, sge_len);
118 		sk_msg_iter_var_next(i);
119 		if (i == src->sg.end && len)
120 			return -ENOSPC;
121 		sge = sk_msg_elem(src, i);
122 	}
123 
124 	return 0;
125 }
126 EXPORT_SYMBOL_GPL(sk_msg_clone);
127 
128 void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes)
129 {
130 	int i = msg->sg.start;
131 
132 	do {
133 		struct scatterlist *sge = sk_msg_elem(msg, i);
134 
135 		if (bytes < sge->length) {
136 			sge->length -= bytes;
137 			sge->offset += bytes;
138 			sk_mem_uncharge(sk, bytes);
139 			break;
140 		}
141 
142 		sk_mem_uncharge(sk, sge->length);
143 		bytes -= sge->length;
144 		sge->length = 0;
145 		sge->offset = 0;
146 		sk_msg_iter_var_next(i);
147 	} while (bytes && i != msg->sg.end);
148 	msg->sg.start = i;
149 }
150 EXPORT_SYMBOL_GPL(sk_msg_return_zero);
151 
152 void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes)
153 {
154 	int i = msg->sg.start;
155 
156 	do {
157 		struct scatterlist *sge = &msg->sg.data[i];
158 		int uncharge = (bytes < sge->length) ? bytes : sge->length;
159 
160 		sk_mem_uncharge(sk, uncharge);
161 		bytes -= uncharge;
162 		sk_msg_iter_var_next(i);
163 	} while (i != msg->sg.end);
164 }
165 EXPORT_SYMBOL_GPL(sk_msg_return);
166 
167 static int sk_msg_free_elem(struct sock *sk, struct sk_msg *msg, u32 i,
168 			    bool charge)
169 {
170 	struct scatterlist *sge = sk_msg_elem(msg, i);
171 	u32 len = sge->length;
172 
173 	/* When the skb owns the memory we free it from consume_skb path. */
174 	if (!msg->skb) {
175 		if (charge)
176 			sk_mem_uncharge(sk, len);
177 		put_page(sg_page(sge));
178 	}
179 	memset(sge, 0, sizeof(*sge));
180 	return len;
181 }
182 
183 static int __sk_msg_free(struct sock *sk, struct sk_msg *msg, u32 i,
184 			 bool charge)
185 {
186 	struct scatterlist *sge = sk_msg_elem(msg, i);
187 	int freed = 0;
188 
189 	while (msg->sg.size) {
190 		msg->sg.size -= sge->length;
191 		freed += sk_msg_free_elem(sk, msg, i, charge);
192 		sk_msg_iter_var_next(i);
193 		sk_msg_check_to_free(msg, i, msg->sg.size);
194 		sge = sk_msg_elem(msg, i);
195 	}
196 	consume_skb(msg->skb);
197 	sk_msg_init(msg);
198 	return freed;
199 }
200 
201 int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg)
202 {
203 	return __sk_msg_free(sk, msg, msg->sg.start, false);
204 }
205 EXPORT_SYMBOL_GPL(sk_msg_free_nocharge);
206 
207 int sk_msg_free(struct sock *sk, struct sk_msg *msg)
208 {
209 	return __sk_msg_free(sk, msg, msg->sg.start, true);
210 }
211 EXPORT_SYMBOL_GPL(sk_msg_free);
212 
213 static void __sk_msg_free_partial(struct sock *sk, struct sk_msg *msg,
214 				  u32 bytes, bool charge)
215 {
216 	struct scatterlist *sge;
217 	u32 i = msg->sg.start;
218 
219 	while (bytes) {
220 		sge = sk_msg_elem(msg, i);
221 		if (!sge->length)
222 			break;
223 		if (bytes < sge->length) {
224 			if (charge)
225 				sk_mem_uncharge(sk, bytes);
226 			sge->length -= bytes;
227 			sge->offset += bytes;
228 			msg->sg.size -= bytes;
229 			break;
230 		}
231 
232 		msg->sg.size -= sge->length;
233 		bytes -= sge->length;
234 		sk_msg_free_elem(sk, msg, i, charge);
235 		sk_msg_iter_var_next(i);
236 		sk_msg_check_to_free(msg, i, bytes);
237 	}
238 	msg->sg.start = i;
239 }
240 
241 void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes)
242 {
243 	__sk_msg_free_partial(sk, msg, bytes, true);
244 }
245 EXPORT_SYMBOL_GPL(sk_msg_free_partial);
246 
247 void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
248 				  u32 bytes)
249 {
250 	__sk_msg_free_partial(sk, msg, bytes, false);
251 }
252 
253 void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len)
254 {
255 	int trim = msg->sg.size - len;
256 	u32 i = msg->sg.end;
257 
258 	if (trim <= 0) {
259 		WARN_ON(trim < 0);
260 		return;
261 	}
262 
263 	sk_msg_iter_var_prev(i);
264 	msg->sg.size = len;
265 	while (msg->sg.data[i].length &&
266 	       trim >= msg->sg.data[i].length) {
267 		trim -= msg->sg.data[i].length;
268 		sk_msg_free_elem(sk, msg, i, true);
269 		sk_msg_iter_var_prev(i);
270 		if (!trim)
271 			goto out;
272 	}
273 
274 	msg->sg.data[i].length -= trim;
275 	sk_mem_uncharge(sk, trim);
276 	/* Adjust copybreak if it falls into the trimmed part of last buf */
277 	if (msg->sg.curr == i && msg->sg.copybreak > msg->sg.data[i].length)
278 		msg->sg.copybreak = msg->sg.data[i].length;
279 out:
280 	sk_msg_iter_var_next(i);
281 	msg->sg.end = i;
282 
283 	/* If we trim data a full sg elem before curr pointer update
284 	 * copybreak and current so that any future copy operations
285 	 * start at new copy location.
286 	 * However trimed data that has not yet been used in a copy op
287 	 * does not require an update.
288 	 */
289 	if (!msg->sg.size) {
290 		msg->sg.curr = msg->sg.start;
291 		msg->sg.copybreak = 0;
292 	} else if (sk_msg_iter_dist(msg->sg.start, msg->sg.curr) >=
293 		   sk_msg_iter_dist(msg->sg.start, msg->sg.end)) {
294 		sk_msg_iter_var_prev(i);
295 		msg->sg.curr = i;
296 		msg->sg.copybreak = msg->sg.data[i].length;
297 	}
298 }
299 EXPORT_SYMBOL_GPL(sk_msg_trim);
300 
301 int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
302 			      struct sk_msg *msg, u32 bytes)
303 {
304 	int i, maxpages, ret = 0, num_elems = sk_msg_elem_used(msg);
305 	const int to_max_pages = MAX_MSG_FRAGS;
306 	struct page *pages[MAX_MSG_FRAGS];
307 	ssize_t orig, copied, use, offset;
308 
309 	orig = msg->sg.size;
310 	while (bytes > 0) {
311 		i = 0;
312 		maxpages = to_max_pages - num_elems;
313 		if (maxpages == 0) {
314 			ret = -EFAULT;
315 			goto out;
316 		}
317 
318 		copied = iov_iter_get_pages(from, pages, bytes, maxpages,
319 					    &offset);
320 		if (copied <= 0) {
321 			ret = -EFAULT;
322 			goto out;
323 		}
324 
325 		iov_iter_advance(from, copied);
326 		bytes -= copied;
327 		msg->sg.size += copied;
328 
329 		while (copied) {
330 			use = min_t(int, copied, PAGE_SIZE - offset);
331 			sg_set_page(&msg->sg.data[msg->sg.end],
332 				    pages[i], use, offset);
333 			sg_unmark_end(&msg->sg.data[msg->sg.end]);
334 			sk_mem_charge(sk, use);
335 
336 			offset = 0;
337 			copied -= use;
338 			sk_msg_iter_next(msg, end);
339 			num_elems++;
340 			i++;
341 		}
342 		/* When zerocopy is mixed with sk_msg_*copy* operations we
343 		 * may have a copybreak set in this case clear and prefer
344 		 * zerocopy remainder when possible.
345 		 */
346 		msg->sg.copybreak = 0;
347 		msg->sg.curr = msg->sg.end;
348 	}
349 out:
350 	/* Revert iov_iter updates, msg will need to use 'trim' later if it
351 	 * also needs to be cleared.
352 	 */
353 	if (ret)
354 		iov_iter_revert(from, msg->sg.size - orig);
355 	return ret;
356 }
357 EXPORT_SYMBOL_GPL(sk_msg_zerocopy_from_iter);
358 
359 int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
360 			     struct sk_msg *msg, u32 bytes)
361 {
362 	int ret = -ENOSPC, i = msg->sg.curr;
363 	struct scatterlist *sge;
364 	u32 copy, buf_size;
365 	void *to;
366 
367 	do {
368 		sge = sk_msg_elem(msg, i);
369 		/* This is possible if a trim operation shrunk the buffer */
370 		if (msg->sg.copybreak >= sge->length) {
371 			msg->sg.copybreak = 0;
372 			sk_msg_iter_var_next(i);
373 			if (i == msg->sg.end)
374 				break;
375 			sge = sk_msg_elem(msg, i);
376 		}
377 
378 		buf_size = sge->length - msg->sg.copybreak;
379 		copy = (buf_size > bytes) ? bytes : buf_size;
380 		to = sg_virt(sge) + msg->sg.copybreak;
381 		msg->sg.copybreak += copy;
382 		if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY)
383 			ret = copy_from_iter_nocache(to, copy, from);
384 		else
385 			ret = copy_from_iter(to, copy, from);
386 		if (ret != copy) {
387 			ret = -EFAULT;
388 			goto out;
389 		}
390 		bytes -= copy;
391 		if (!bytes)
392 			break;
393 		msg->sg.copybreak = 0;
394 		sk_msg_iter_var_next(i);
395 	} while (i != msg->sg.end);
396 out:
397 	msg->sg.curr = i;
398 	return ret;
399 }
400 EXPORT_SYMBOL_GPL(sk_msg_memcopy_from_iter);
401 
402 static struct sk_msg *sk_psock_create_ingress_msg(struct sock *sk,
403 						  struct sk_buff *skb)
404 {
405 	struct sk_msg *msg;
406 
407 	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
408 		return NULL;
409 
410 	if (!sk_rmem_schedule(sk, skb, skb->truesize))
411 		return NULL;
412 
413 	msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_ATOMIC);
414 	if (unlikely(!msg))
415 		return NULL;
416 
417 	sk_msg_init(msg);
418 	return msg;
419 }
420 
421 static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
422 					struct sk_psock *psock,
423 					struct sock *sk,
424 					struct sk_msg *msg)
425 {
426 	int num_sge, copied;
427 
428 	/* skb linearize may fail with ENOMEM, but lets simply try again
429 	 * later if this happens. Under memory pressure we don't want to
430 	 * drop the skb. We need to linearize the skb so that the mapping
431 	 * in skb_to_sgvec can not error.
432 	 */
433 	if (skb_linearize(skb))
434 		return -EAGAIN;
435 	num_sge = skb_to_sgvec(skb, msg->sg.data, 0, skb->len);
436 	if (unlikely(num_sge < 0)) {
437 		kfree(msg);
438 		return num_sge;
439 	}
440 
441 	copied = skb->len;
442 	msg->sg.start = 0;
443 	msg->sg.size = copied;
444 	msg->sg.end = num_sge;
445 	msg->skb = skb;
446 
447 	sk_psock_queue_msg(psock, msg);
448 	sk_psock_data_ready(sk, psock);
449 	return copied;
450 }
451 
452 static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb);
453 
454 static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb)
455 {
456 	struct sock *sk = psock->sk;
457 	struct sk_msg *msg;
458 
459 	/* If we are receiving on the same sock skb->sk is already assigned,
460 	 * skip memory accounting and owner transition seeing it already set
461 	 * correctly.
462 	 */
463 	if (unlikely(skb->sk == sk))
464 		return sk_psock_skb_ingress_self(psock, skb);
465 	msg = sk_psock_create_ingress_msg(sk, skb);
466 	if (!msg)
467 		return -EAGAIN;
468 
469 	/* This will transition ownership of the data from the socket where
470 	 * the BPF program was run initiating the redirect to the socket
471 	 * we will eventually receive this data on. The data will be released
472 	 * from skb_consume found in __tcp_bpf_recvmsg() after its been copied
473 	 * into user buffers.
474 	 */
475 	skb_set_owner_r(skb, sk);
476 	return sk_psock_skb_ingress_enqueue(skb, psock, sk, msg);
477 }
478 
479 /* Puts an skb on the ingress queue of the socket already assigned to the
480  * skb. In this case we do not need to check memory limits or skb_set_owner_r
481  * because the skb is already accounted for here.
482  */
483 static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb)
484 {
485 	struct sk_msg *msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_ATOMIC);
486 	struct sock *sk = psock->sk;
487 
488 	if (unlikely(!msg))
489 		return -EAGAIN;
490 	sk_msg_init(msg);
491 	skb_set_owner_r(skb, sk);
492 	return sk_psock_skb_ingress_enqueue(skb, psock, sk, msg);
493 }
494 
495 static int sk_psock_handle_skb(struct sk_psock *psock, struct sk_buff *skb,
496 			       u32 off, u32 len, bool ingress)
497 {
498 	if (!ingress) {
499 		if (!sock_writeable(psock->sk))
500 			return -EAGAIN;
501 		return skb_send_sock_locked(psock->sk, skb, off, len);
502 	}
503 	return sk_psock_skb_ingress(psock, skb);
504 }
505 
506 static void sk_psock_backlog(struct work_struct *work)
507 {
508 	struct sk_psock *psock = container_of(work, struct sk_psock, work);
509 	struct sk_psock_work_state *state = &psock->work_state;
510 	struct sk_buff *skb;
511 	bool ingress;
512 	u32 len, off;
513 	int ret;
514 
515 	/* Lock sock to avoid losing sk_socket during loop. */
516 	lock_sock(psock->sk);
517 	if (state->skb) {
518 		skb = state->skb;
519 		len = state->len;
520 		off = state->off;
521 		state->skb = NULL;
522 		goto start;
523 	}
524 
525 	while ((skb = skb_dequeue(&psock->ingress_skb))) {
526 		len = skb->len;
527 		off = 0;
528 start:
529 		ingress = tcp_skb_bpf_ingress(skb);
530 		do {
531 			ret = -EIO;
532 			if (likely(psock->sk->sk_socket))
533 				ret = sk_psock_handle_skb(psock, skb, off,
534 							  len, ingress);
535 			if (ret <= 0) {
536 				if (ret == -EAGAIN) {
537 					state->skb = skb;
538 					state->len = len;
539 					state->off = off;
540 					goto end;
541 				}
542 				/* Hard errors break pipe and stop xmit. */
543 				sk_psock_report_error(psock, ret ? -ret : EPIPE);
544 				sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
545 				kfree_skb(skb);
546 				goto end;
547 			}
548 			off += ret;
549 			len -= ret;
550 		} while (len);
551 
552 		if (!ingress)
553 			kfree_skb(skb);
554 	}
555 end:
556 	release_sock(psock->sk);
557 }
558 
559 struct sk_psock *sk_psock_init(struct sock *sk, int node)
560 {
561 	struct sk_psock *psock;
562 	struct proto *prot;
563 
564 	write_lock_bh(&sk->sk_callback_lock);
565 
566 	if (inet_csk_has_ulp(sk)) {
567 		psock = ERR_PTR(-EINVAL);
568 		goto out;
569 	}
570 
571 	if (sk->sk_user_data) {
572 		psock = ERR_PTR(-EBUSY);
573 		goto out;
574 	}
575 
576 	psock = kzalloc_node(sizeof(*psock), GFP_ATOMIC | __GFP_NOWARN, node);
577 	if (!psock) {
578 		psock = ERR_PTR(-ENOMEM);
579 		goto out;
580 	}
581 
582 	prot = READ_ONCE(sk->sk_prot);
583 	psock->sk = sk;
584 	psock->eval = __SK_NONE;
585 	psock->sk_proto = prot;
586 	psock->saved_unhash = prot->unhash;
587 	psock->saved_close = prot->close;
588 	psock->saved_write_space = sk->sk_write_space;
589 
590 	INIT_LIST_HEAD(&psock->link);
591 	spin_lock_init(&psock->link_lock);
592 
593 	INIT_WORK(&psock->work, sk_psock_backlog);
594 	INIT_LIST_HEAD(&psock->ingress_msg);
595 	skb_queue_head_init(&psock->ingress_skb);
596 
597 	sk_psock_set_state(psock, SK_PSOCK_TX_ENABLED);
598 	refcount_set(&psock->refcnt, 1);
599 
600 	rcu_assign_sk_user_data_nocopy(sk, psock);
601 	sock_hold(sk);
602 
603 out:
604 	write_unlock_bh(&sk->sk_callback_lock);
605 	return psock;
606 }
607 EXPORT_SYMBOL_GPL(sk_psock_init);
608 
609 struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock)
610 {
611 	struct sk_psock_link *link;
612 
613 	spin_lock_bh(&psock->link_lock);
614 	link = list_first_entry_or_null(&psock->link, struct sk_psock_link,
615 					list);
616 	if (link)
617 		list_del(&link->list);
618 	spin_unlock_bh(&psock->link_lock);
619 	return link;
620 }
621 
622 void __sk_psock_purge_ingress_msg(struct sk_psock *psock)
623 {
624 	struct sk_msg *msg, *tmp;
625 
626 	list_for_each_entry_safe(msg, tmp, &psock->ingress_msg, list) {
627 		list_del(&msg->list);
628 		sk_msg_free(psock->sk, msg);
629 		kfree(msg);
630 	}
631 }
632 
633 static void sk_psock_zap_ingress(struct sk_psock *psock)
634 {
635 	__skb_queue_purge(&psock->ingress_skb);
636 	__sk_psock_purge_ingress_msg(psock);
637 }
638 
639 static void sk_psock_link_destroy(struct sk_psock *psock)
640 {
641 	struct sk_psock_link *link, *tmp;
642 
643 	list_for_each_entry_safe(link, tmp, &psock->link, list) {
644 		list_del(&link->list);
645 		sk_psock_free_link(link);
646 	}
647 }
648 
649 static void sk_psock_destroy_deferred(struct work_struct *gc)
650 {
651 	struct sk_psock *psock = container_of(gc, struct sk_psock, gc);
652 
653 	/* No sk_callback_lock since already detached. */
654 
655 	/* Parser has been stopped */
656 	if (psock->progs.skb_parser)
657 		strp_done(&psock->parser.strp);
658 
659 	cancel_work_sync(&psock->work);
660 
661 	psock_progs_drop(&psock->progs);
662 
663 	sk_psock_link_destroy(psock);
664 	sk_psock_cork_free(psock);
665 	sk_psock_zap_ingress(psock);
666 
667 	if (psock->sk_redir)
668 		sock_put(psock->sk_redir);
669 	sock_put(psock->sk);
670 	kfree(psock);
671 }
672 
673 static void sk_psock_destroy(struct rcu_head *rcu)
674 {
675 	struct sk_psock *psock = container_of(rcu, struct sk_psock, rcu);
676 
677 	INIT_WORK(&psock->gc, sk_psock_destroy_deferred);
678 	schedule_work(&psock->gc);
679 }
680 
681 void sk_psock_drop(struct sock *sk, struct sk_psock *psock)
682 {
683 	sk_psock_cork_free(psock);
684 	sk_psock_zap_ingress(psock);
685 
686 	write_lock_bh(&sk->sk_callback_lock);
687 	sk_psock_restore_proto(sk, psock);
688 	rcu_assign_sk_user_data(sk, NULL);
689 	if (psock->progs.skb_parser)
690 		sk_psock_stop_strp(sk, psock);
691 	else if (psock->progs.skb_verdict)
692 		sk_psock_stop_verdict(sk, psock);
693 	write_unlock_bh(&sk->sk_callback_lock);
694 	sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
695 
696 	call_rcu(&psock->rcu, sk_psock_destroy);
697 }
698 EXPORT_SYMBOL_GPL(sk_psock_drop);
699 
700 static int sk_psock_map_verd(int verdict, bool redir)
701 {
702 	switch (verdict) {
703 	case SK_PASS:
704 		return redir ? __SK_REDIRECT : __SK_PASS;
705 	case SK_DROP:
706 	default:
707 		break;
708 	}
709 
710 	return __SK_DROP;
711 }
712 
713 int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
714 			 struct sk_msg *msg)
715 {
716 	struct bpf_prog *prog;
717 	int ret;
718 
719 	rcu_read_lock();
720 	prog = READ_ONCE(psock->progs.msg_parser);
721 	if (unlikely(!prog)) {
722 		ret = __SK_PASS;
723 		goto out;
724 	}
725 
726 	sk_msg_compute_data_pointers(msg);
727 	msg->sk = sk;
728 	ret = bpf_prog_run_pin_on_cpu(prog, msg);
729 	ret = sk_psock_map_verd(ret, msg->sk_redir);
730 	psock->apply_bytes = msg->apply_bytes;
731 	if (ret == __SK_REDIRECT) {
732 		if (psock->sk_redir)
733 			sock_put(psock->sk_redir);
734 		psock->sk_redir = msg->sk_redir;
735 		if (!psock->sk_redir) {
736 			ret = __SK_DROP;
737 			goto out;
738 		}
739 		sock_hold(psock->sk_redir);
740 	}
741 out:
742 	rcu_read_unlock();
743 	return ret;
744 }
745 EXPORT_SYMBOL_GPL(sk_psock_msg_verdict);
746 
747 static int sk_psock_bpf_run(struct sk_psock *psock, struct bpf_prog *prog,
748 			    struct sk_buff *skb)
749 {
750 	bpf_compute_data_end_sk_skb(skb);
751 	return bpf_prog_run_pin_on_cpu(prog, skb);
752 }
753 
754 static struct sk_psock *sk_psock_from_strp(struct strparser *strp)
755 {
756 	struct sk_psock_parser *parser;
757 
758 	parser = container_of(strp, struct sk_psock_parser, strp);
759 	return container_of(parser, struct sk_psock, parser);
760 }
761 
762 static void sk_psock_skb_redirect(struct sk_buff *skb)
763 {
764 	struct sk_psock *psock_other;
765 	struct sock *sk_other;
766 
767 	sk_other = tcp_skb_bpf_redirect_fetch(skb);
768 	/* This error is a buggy BPF program, it returned a redirect
769 	 * return code, but then didn't set a redirect interface.
770 	 */
771 	if (unlikely(!sk_other)) {
772 		kfree_skb(skb);
773 		return;
774 	}
775 	psock_other = sk_psock(sk_other);
776 	/* This error indicates the socket is being torn down or had another
777 	 * error that caused the pipe to break. We can't send a packet on
778 	 * a socket that is in this state so we drop the skb.
779 	 */
780 	if (!psock_other || sock_flag(sk_other, SOCK_DEAD) ||
781 	    !sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED)) {
782 		kfree_skb(skb);
783 		return;
784 	}
785 
786 	skb_queue_tail(&psock_other->ingress_skb, skb);
787 	schedule_work(&psock_other->work);
788 }
789 
790 static void sk_psock_tls_verdict_apply(struct sk_buff *skb, struct sock *sk, int verdict)
791 {
792 	switch (verdict) {
793 	case __SK_REDIRECT:
794 		sk_psock_skb_redirect(skb);
795 		break;
796 	case __SK_PASS:
797 	case __SK_DROP:
798 	default:
799 		break;
800 	}
801 }
802 
803 int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb)
804 {
805 	struct bpf_prog *prog;
806 	int ret = __SK_PASS;
807 
808 	rcu_read_lock();
809 	prog = READ_ONCE(psock->progs.skb_verdict);
810 	if (likely(prog)) {
811 		skb->sk = psock->sk;
812 		tcp_skb_bpf_redirect_clear(skb);
813 		ret = sk_psock_bpf_run(psock, prog, skb);
814 		ret = sk_psock_map_verd(ret, tcp_skb_bpf_redirect_fetch(skb));
815 		skb->sk = NULL;
816 	}
817 	sk_psock_tls_verdict_apply(skb, psock->sk, ret);
818 	rcu_read_unlock();
819 	return ret;
820 }
821 EXPORT_SYMBOL_GPL(sk_psock_tls_strp_read);
822 
823 static void sk_psock_verdict_apply(struct sk_psock *psock,
824 				   struct sk_buff *skb, int verdict)
825 {
826 	struct tcp_skb_cb *tcp;
827 	struct sock *sk_other;
828 	int err = -EIO;
829 
830 	switch (verdict) {
831 	case __SK_PASS:
832 		sk_other = psock->sk;
833 		if (sock_flag(sk_other, SOCK_DEAD) ||
834 		    !sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
835 			goto out_free;
836 		}
837 
838 		tcp = TCP_SKB_CB(skb);
839 		tcp->bpf.flags |= BPF_F_INGRESS;
840 
841 		/* If the queue is empty then we can submit directly
842 		 * into the msg queue. If its not empty we have to
843 		 * queue work otherwise we may get OOO data. Otherwise,
844 		 * if sk_psock_skb_ingress errors will be handled by
845 		 * retrying later from workqueue.
846 		 */
847 		if (skb_queue_empty(&psock->ingress_skb)) {
848 			err = sk_psock_skb_ingress_self(psock, skb);
849 		}
850 		if (err < 0) {
851 			skb_queue_tail(&psock->ingress_skb, skb);
852 			schedule_work(&psock->work);
853 		}
854 		break;
855 	case __SK_REDIRECT:
856 		sk_psock_skb_redirect(skb);
857 		break;
858 	case __SK_DROP:
859 	default:
860 out_free:
861 		kfree_skb(skb);
862 	}
863 }
864 
865 static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb)
866 {
867 	struct sk_psock *psock;
868 	struct bpf_prog *prog;
869 	int ret = __SK_DROP;
870 	struct sock *sk;
871 
872 	rcu_read_lock();
873 	sk = strp->sk;
874 	psock = sk_psock(sk);
875 	if (unlikely(!psock)) {
876 		kfree_skb(skb);
877 		goto out;
878 	}
879 	prog = READ_ONCE(psock->progs.skb_verdict);
880 	if (likely(prog)) {
881 		skb->sk = sk;
882 		tcp_skb_bpf_redirect_clear(skb);
883 		ret = sk_psock_bpf_run(psock, prog, skb);
884 		ret = sk_psock_map_verd(ret, tcp_skb_bpf_redirect_fetch(skb));
885 		skb->sk = NULL;
886 	}
887 	sk_psock_verdict_apply(psock, skb, ret);
888 out:
889 	rcu_read_unlock();
890 }
891 
892 static int sk_psock_strp_read_done(struct strparser *strp, int err)
893 {
894 	return err;
895 }
896 
897 static int sk_psock_strp_parse(struct strparser *strp, struct sk_buff *skb)
898 {
899 	struct sk_psock *psock = sk_psock_from_strp(strp);
900 	struct bpf_prog *prog;
901 	int ret = skb->len;
902 
903 	rcu_read_lock();
904 	prog = READ_ONCE(psock->progs.skb_parser);
905 	if (likely(prog)) {
906 		skb->sk = psock->sk;
907 		ret = sk_psock_bpf_run(psock, prog, skb);
908 		skb->sk = NULL;
909 	}
910 	rcu_read_unlock();
911 	return ret;
912 }
913 
914 /* Called with socket lock held. */
915 static void sk_psock_strp_data_ready(struct sock *sk)
916 {
917 	struct sk_psock *psock;
918 
919 	rcu_read_lock();
920 	psock = sk_psock(sk);
921 	if (likely(psock)) {
922 		if (tls_sw_has_ctx_rx(sk)) {
923 			psock->parser.saved_data_ready(sk);
924 		} else {
925 			write_lock_bh(&sk->sk_callback_lock);
926 			strp_data_ready(&psock->parser.strp);
927 			write_unlock_bh(&sk->sk_callback_lock);
928 		}
929 	}
930 	rcu_read_unlock();
931 }
932 
933 static int sk_psock_verdict_recv(read_descriptor_t *desc, struct sk_buff *skb,
934 				 unsigned int offset, size_t orig_len)
935 {
936 	struct sock *sk = (struct sock *)desc->arg.data;
937 	struct sk_psock *psock;
938 	struct bpf_prog *prog;
939 	int ret = __SK_DROP;
940 	int len = skb->len;
941 
942 	/* clone here so sk_eat_skb() in tcp_read_sock does not drop our data */
943 	skb = skb_clone(skb, GFP_ATOMIC);
944 	if (!skb) {
945 		desc->error = -ENOMEM;
946 		return 0;
947 	}
948 
949 	rcu_read_lock();
950 	psock = sk_psock(sk);
951 	if (unlikely(!psock)) {
952 		len = 0;
953 		kfree_skb(skb);
954 		goto out;
955 	}
956 	prog = READ_ONCE(psock->progs.skb_verdict);
957 	if (likely(prog)) {
958 		skb->sk = sk;
959 		tcp_skb_bpf_redirect_clear(skb);
960 		ret = sk_psock_bpf_run(psock, prog, skb);
961 		ret = sk_psock_map_verd(ret, tcp_skb_bpf_redirect_fetch(skb));
962 		skb->sk = NULL;
963 	}
964 	sk_psock_verdict_apply(psock, skb, ret);
965 out:
966 	rcu_read_unlock();
967 	return len;
968 }
969 
970 static void sk_psock_verdict_data_ready(struct sock *sk)
971 {
972 	struct socket *sock = sk->sk_socket;
973 	read_descriptor_t desc;
974 
975 	if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
976 		return;
977 
978 	desc.arg.data = sk;
979 	desc.error = 0;
980 	desc.count = 1;
981 
982 	sock->ops->read_sock(sk, &desc, sk_psock_verdict_recv);
983 }
984 
985 static void sk_psock_write_space(struct sock *sk)
986 {
987 	struct sk_psock *psock;
988 	void (*write_space)(struct sock *sk) = NULL;
989 
990 	rcu_read_lock();
991 	psock = sk_psock(sk);
992 	if (likely(psock)) {
993 		if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED))
994 			schedule_work(&psock->work);
995 		write_space = psock->saved_write_space;
996 	}
997 	rcu_read_unlock();
998 	if (write_space)
999 		write_space(sk);
1000 }
1001 
1002 int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock)
1003 {
1004 	static const struct strp_callbacks cb = {
1005 		.rcv_msg	= sk_psock_strp_read,
1006 		.read_sock_done	= sk_psock_strp_read_done,
1007 		.parse_msg	= sk_psock_strp_parse,
1008 	};
1009 
1010 	psock->parser.enabled = false;
1011 	return strp_init(&psock->parser.strp, sk, &cb);
1012 }
1013 
1014 void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)
1015 {
1016 	struct sk_psock_parser *parser = &psock->parser;
1017 
1018 	if (parser->enabled)
1019 		return;
1020 
1021 	parser->saved_data_ready = sk->sk_data_ready;
1022 	sk->sk_data_ready = sk_psock_verdict_data_ready;
1023 	sk->sk_write_space = sk_psock_write_space;
1024 	parser->enabled = true;
1025 }
1026 
1027 void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock)
1028 {
1029 	struct sk_psock_parser *parser = &psock->parser;
1030 
1031 	if (parser->enabled)
1032 		return;
1033 
1034 	parser->saved_data_ready = sk->sk_data_ready;
1035 	sk->sk_data_ready = sk_psock_strp_data_ready;
1036 	sk->sk_write_space = sk_psock_write_space;
1037 	parser->enabled = true;
1038 }
1039 
1040 void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
1041 {
1042 	struct sk_psock_parser *parser = &psock->parser;
1043 
1044 	if (!parser->enabled)
1045 		return;
1046 
1047 	sk->sk_data_ready = parser->saved_data_ready;
1048 	parser->saved_data_ready = NULL;
1049 	strp_stop(&parser->strp);
1050 	parser->enabled = false;
1051 }
1052 
1053 void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)
1054 {
1055 	struct sk_psock_parser *parser = &psock->parser;
1056 
1057 	if (!parser->enabled)
1058 		return;
1059 
1060 	sk->sk_data_ready = parser->saved_data_ready;
1061 	parser->saved_data_ready = NULL;
1062 	parser->enabled = false;
1063 }
1064