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