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