1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
4 #include <linux/skmsg.h>
5 #include <linux/skbuff.h>
6 #include <linux/scatterlist.h>
12 static bool sk_msg_try_coalesce_ok(struct sk_msg *msg, int elem_first_coalesce)
14 if (msg->sg.end > msg->sg.start &&
15 elem_first_coalesce < msg->sg.end)
18 if (msg->sg.end < msg->sg.start &&
19 (elem_first_coalesce > msg->sg.start ||
20 elem_first_coalesce < msg->sg.end))
26 int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
27 int elem_first_coalesce)
29 struct page_frag *pfrag = sk_page_frag(sk);
30 u32 osize = msg->sg.size;
35 struct scatterlist *sge;
39 if (!sk_page_frag_refill(sk, pfrag)) {
44 orig_offset = pfrag->offset;
45 use = min_t(int, len, pfrag->size - orig_offset);
46 if (!sk_wmem_schedule(sk, use)) {
52 sk_msg_iter_var_prev(i);
53 sge = &msg->sg.data[i];
55 if (sk_msg_try_coalesce_ok(msg, elem_first_coalesce) &&
56 sg_page(sge) == pfrag->page &&
57 sge->offset + sge->length == orig_offset) {
60 if (sk_msg_full(msg)) {
65 sge = &msg->sg.data[msg->sg.end];
67 sg_set_page(sge, pfrag->page, use, orig_offset);
68 get_page(pfrag->page);
69 sk_msg_iter_next(msg, end);
72 sk_mem_charge(sk, use);
81 sk_msg_trim(sk, msg, osize);
84 EXPORT_SYMBOL_GPL(sk_msg_alloc);
86 int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
89 int i = src->sg.start;
90 struct scatterlist *sge = sk_msg_elem(src, i);
91 struct scatterlist *sgd = NULL;
95 if (sge->length > off)
98 sk_msg_iter_var_next(i);
99 if (i == src->sg.end && off)
101 sge = sk_msg_elem(src, i);
105 sge_len = sge->length - off;
110 sgd = sk_msg_elem(dst, dst->sg.end - 1);
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);
126 sk_mem_charge(sk, sge_len);
127 sk_msg_iter_var_next(i);
128 if (i == src->sg.end && len)
130 sge = sk_msg_elem(src, i);
135 EXPORT_SYMBOL_GPL(sk_msg_clone);
137 void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes)
139 int i = msg->sg.start;
142 struct scatterlist *sge = sk_msg_elem(msg, i);
144 if (bytes < sge->length) {
145 sge->length -= bytes;
146 sge->offset += bytes;
147 sk_mem_uncharge(sk, bytes);
151 sk_mem_uncharge(sk, sge->length);
152 bytes -= sge->length;
155 sk_msg_iter_var_next(i);
156 } while (bytes && i != msg->sg.end);
159 EXPORT_SYMBOL_GPL(sk_msg_return_zero);
161 void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes)
163 int i = msg->sg.start;
166 struct scatterlist *sge = &msg->sg.data[i];
167 int uncharge = (bytes < sge->length) ? bytes : sge->length;
169 sk_mem_uncharge(sk, uncharge);
171 sk_msg_iter_var_next(i);
172 } while (i != msg->sg.end);
174 EXPORT_SYMBOL_GPL(sk_msg_return);
176 static int sk_msg_free_elem(struct sock *sk, struct sk_msg *msg, u32 i,
179 struct scatterlist *sge = sk_msg_elem(msg, i);
180 u32 len = sge->length;
182 /* When the skb owns the memory we free it from consume_skb path. */
185 sk_mem_uncharge(sk, len);
186 put_page(sg_page(sge));
188 memset(sge, 0, sizeof(*sge));
192 static int __sk_msg_free(struct sock *sk, struct sk_msg *msg, u32 i,
195 struct scatterlist *sge = sk_msg_elem(msg, i);
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);
205 consume_skb(msg->skb);
210 int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg)
212 return __sk_msg_free(sk, msg, msg->sg.start, false);
214 EXPORT_SYMBOL_GPL(sk_msg_free_nocharge);
216 int sk_msg_free(struct sock *sk, struct sk_msg *msg)
218 return __sk_msg_free(sk, msg, msg->sg.start, true);
220 EXPORT_SYMBOL_GPL(sk_msg_free);
222 static void __sk_msg_free_partial(struct sock *sk, struct sk_msg *msg,
223 u32 bytes, bool charge)
225 struct scatterlist *sge;
226 u32 i = msg->sg.start;
229 sge = sk_msg_elem(msg, i);
232 if (bytes < sge->length) {
234 sk_mem_uncharge(sk, bytes);
235 sge->length -= bytes;
236 sge->offset += bytes;
237 msg->sg.size -= bytes;
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);
250 void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes)
252 __sk_msg_free_partial(sk, msg, bytes, true);
254 EXPORT_SYMBOL_GPL(sk_msg_free_partial);
256 void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
259 __sk_msg_free_partial(sk, msg, bytes, false);
262 void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len)
264 int trim = msg->sg.size - len;
272 sk_msg_iter_var_prev(i);
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);
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;
289 sk_msg_iter_var_next(i);
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.
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);
305 msg->sg.copybreak = msg->sg.data[i].length;
308 EXPORT_SYMBOL_GPL(sk_msg_trim);
310 int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
311 struct sk_msg *msg, u32 bytes)
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;
321 maxpages = to_max_pages - num_elems;
327 copied = iov_iter_get_pages(from, pages, bytes, maxpages,
334 iov_iter_advance(from, copied);
336 msg->sg.size += 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);
347 sk_msg_iter_next(msg, end);
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.
355 msg->sg.copybreak = 0;
356 msg->sg.curr = msg->sg.end;
359 /* Revert iov_iter updates, msg will need to use 'trim' later if it
360 * also needs to be cleared.
363 iov_iter_revert(from, msg->sg.size - orig);
366 EXPORT_SYMBOL_GPL(sk_msg_zerocopy_from_iter);
368 int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
369 struct sk_msg *msg, u32 bytes)
371 int ret = -ENOSPC, i = msg->sg.curr;
372 struct scatterlist *sge;
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)
384 sge = sk_msg_elem(msg, i);
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);
394 ret = copy_from_iter(to, copy, from);
402 msg->sg.copybreak = 0;
403 sk_msg_iter_var_next(i);
404 } while (i != msg->sg.end);
409 EXPORT_SYMBOL_GPL(sk_msg_memcopy_from_iter);
411 /* Receive sk_msg from psock->ingress_msg to @msg. */
412 int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg,
415 struct iov_iter *iter = &msg->msg_iter;
416 int peek = flags & MSG_PEEK;
417 struct sk_msg *msg_rx;
420 msg_rx = sk_psock_peek_msg(psock);
421 while (copied != len) {
422 struct scatterlist *sge;
424 if (unlikely(!msg_rx))
427 i = msg_rx->sg.start;
432 sge = sk_msg_elem(msg_rx, i);
435 if (copied + copy > len)
437 copy = copy_page_to_iter(page, sge->offset, copy, iter);
439 return copied ? copied : -EFAULT;
446 sk_mem_uncharge(sk, copy);
447 msg_rx->sg.size -= copy;
450 sk_msg_iter_var_next(i);
455 /* Lets not optimize peek case if copy_page_to_iter
456 * didn't copy the entire length lets just break.
458 if (copy != sge->length)
460 sk_msg_iter_var_next(i);
465 } while (i != msg_rx->sg.end);
467 if (unlikely(peek)) {
468 msg_rx = sk_psock_next_msg(psock, msg_rx);
474 msg_rx->sg.start = i;
475 if (!sge->length && msg_rx->sg.start == msg_rx->sg.end) {
476 msg_rx = sk_psock_dequeue_msg(psock);
477 kfree_sk_msg(msg_rx);
479 msg_rx = sk_psock_peek_msg(psock);
484 EXPORT_SYMBOL_GPL(sk_msg_recvmsg);
486 bool sk_msg_is_readable(struct sock *sk)
488 struct sk_psock *psock;
492 psock = sk_psock(sk);
494 empty = list_empty(&psock->ingress_msg);
498 EXPORT_SYMBOL_GPL(sk_msg_is_readable);
500 static struct sk_msg *sk_psock_create_ingress_msg(struct sock *sk,
505 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
508 if (!sk_rmem_schedule(sk, skb, skb->truesize))
511 msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_KERNEL);
519 static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
521 struct sk_psock *psock,
527 /* skb linearize may fail with ENOMEM, but lets simply try again
528 * later if this happens. Under memory pressure we don't want to
529 * drop the skb. We need to linearize the skb so that the mapping
530 * in skb_to_sgvec can not error.
532 if (skb_linearize(skb))
534 num_sge = skb_to_sgvec(skb, msg->sg.data, off, len);
535 if (unlikely(num_sge < 0))
540 msg->sg.size = copied;
541 msg->sg.end = num_sge;
544 sk_psock_queue_msg(psock, msg);
545 sk_psock_data_ready(sk, psock);
549 static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb,
552 static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,
555 struct sock *sk = psock->sk;
559 /* If we are receiving on the same sock skb->sk is already assigned,
560 * skip memory accounting and owner transition seeing it already set
563 if (unlikely(skb->sk == sk))
564 return sk_psock_skb_ingress_self(psock, skb, off, len);
565 msg = sk_psock_create_ingress_msg(sk, skb);
569 /* This will transition ownership of the data from the socket where
570 * the BPF program was run initiating the redirect to the socket
571 * we will eventually receive this data on. The data will be released
572 * from skb_consume found in __tcp_bpf_recvmsg() after its been copied
575 skb_set_owner_r(skb, sk);
576 err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg);
582 /* Puts an skb on the ingress queue of the socket already assigned to the
583 * skb. In this case we do not need to check memory limits or skb_set_owner_r
584 * because the skb is already accounted for here.
586 static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb,
589 struct sk_msg *msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_ATOMIC);
590 struct sock *sk = psock->sk;
596 skb_set_owner_r(skb, sk);
597 err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg);
603 static int sk_psock_handle_skb(struct sk_psock *psock, struct sk_buff *skb,
604 u32 off, u32 len, bool ingress)
607 if (!sock_writeable(psock->sk))
609 return skb_send_sock(psock->sk, skb, off, len);
611 return sk_psock_skb_ingress(psock, skb, off, len);
614 static void sk_psock_skb_state(struct sk_psock *psock,
615 struct sk_psock_work_state *state,
619 spin_lock_bh(&psock->ingress_lock);
620 if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
625 sock_drop(psock->sk, skb);
627 spin_unlock_bh(&psock->ingress_lock);
630 static void sk_psock_backlog(struct work_struct *work)
632 struct sk_psock *psock = container_of(work, struct sk_psock, work);
633 struct sk_psock_work_state *state = &psock->work_state;
634 struct sk_buff *skb = NULL;
639 mutex_lock(&psock->work_mutex);
640 if (unlikely(state->skb)) {
641 spin_lock_bh(&psock->ingress_lock);
646 spin_unlock_bh(&psock->ingress_lock);
651 while ((skb = skb_dequeue(&psock->ingress_skb))) {
654 if (skb_bpf_strparser(skb)) {
655 struct strp_msg *stm = strp_msg(skb);
661 ingress = skb_bpf_ingress(skb);
662 skb_bpf_redirect_clear(skb);
665 if (!sock_flag(psock->sk, SOCK_DEAD))
666 ret = sk_psock_handle_skb(psock, skb, off,
669 if (ret == -EAGAIN) {
670 sk_psock_skb_state(psock, state, skb,
674 /* Hard errors break pipe and stop xmit. */
675 sk_psock_report_error(psock, ret ? -ret : EPIPE);
676 sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
677 sock_drop(psock->sk, skb);
688 mutex_unlock(&psock->work_mutex);
691 struct sk_psock *sk_psock_init(struct sock *sk, int node)
693 struct sk_psock *psock;
696 write_lock_bh(&sk->sk_callback_lock);
698 if (sk_is_inet(sk) && inet_csk_has_ulp(sk)) {
699 psock = ERR_PTR(-EINVAL);
703 if (sk->sk_user_data) {
704 psock = ERR_PTR(-EBUSY);
708 psock = kzalloc_node(sizeof(*psock), GFP_ATOMIC | __GFP_NOWARN, node);
710 psock = ERR_PTR(-ENOMEM);
714 prot = READ_ONCE(sk->sk_prot);
716 psock->eval = __SK_NONE;
717 psock->sk_proto = prot;
718 psock->saved_unhash = prot->unhash;
719 psock->saved_close = prot->close;
720 psock->saved_write_space = sk->sk_write_space;
722 INIT_LIST_HEAD(&psock->link);
723 spin_lock_init(&psock->link_lock);
725 INIT_WORK(&psock->work, sk_psock_backlog);
726 mutex_init(&psock->work_mutex);
727 INIT_LIST_HEAD(&psock->ingress_msg);
728 spin_lock_init(&psock->ingress_lock);
729 skb_queue_head_init(&psock->ingress_skb);
731 sk_psock_set_state(psock, SK_PSOCK_TX_ENABLED);
732 refcount_set(&psock->refcnt, 1);
734 rcu_assign_sk_user_data_nocopy(sk, psock);
738 write_unlock_bh(&sk->sk_callback_lock);
741 EXPORT_SYMBOL_GPL(sk_psock_init);
743 struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock)
745 struct sk_psock_link *link;
747 spin_lock_bh(&psock->link_lock);
748 link = list_first_entry_or_null(&psock->link, struct sk_psock_link,
751 list_del(&link->list);
752 spin_unlock_bh(&psock->link_lock);
756 static void __sk_psock_purge_ingress_msg(struct sk_psock *psock)
758 struct sk_msg *msg, *tmp;
760 list_for_each_entry_safe(msg, tmp, &psock->ingress_msg, list) {
761 list_del(&msg->list);
762 sk_msg_free(psock->sk, msg);
767 static void __sk_psock_zap_ingress(struct sk_psock *psock)
771 while ((skb = skb_dequeue(&psock->ingress_skb)) != NULL) {
772 skb_bpf_redirect_clear(skb);
773 sock_drop(psock->sk, skb);
775 kfree_skb(psock->work_state.skb);
776 /* We null the skb here to ensure that calls to sk_psock_backlog
777 * do not pick up the free'd skb.
779 psock->work_state.skb = NULL;
780 __sk_psock_purge_ingress_msg(psock);
783 static void sk_psock_link_destroy(struct sk_psock *psock)
785 struct sk_psock_link *link, *tmp;
787 list_for_each_entry_safe(link, tmp, &psock->link, list) {
788 list_del(&link->list);
789 sk_psock_free_link(link);
793 void sk_psock_stop(struct sk_psock *psock, bool wait)
795 spin_lock_bh(&psock->ingress_lock);
796 sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
797 sk_psock_cork_free(psock);
798 __sk_psock_zap_ingress(psock);
799 spin_unlock_bh(&psock->ingress_lock);
802 cancel_work_sync(&psock->work);
805 static void sk_psock_done_strp(struct sk_psock *psock);
807 static void sk_psock_destroy(struct work_struct *work)
809 struct sk_psock *psock = container_of(to_rcu_work(work),
810 struct sk_psock, rwork);
811 /* No sk_callback_lock since already detached. */
813 sk_psock_done_strp(psock);
815 cancel_work_sync(&psock->work);
816 mutex_destroy(&psock->work_mutex);
818 psock_progs_drop(&psock->progs);
820 sk_psock_link_destroy(psock);
821 sk_psock_cork_free(psock);
824 sock_put(psock->sk_redir);
829 void sk_psock_drop(struct sock *sk, struct sk_psock *psock)
831 write_lock_bh(&sk->sk_callback_lock);
832 sk_psock_restore_proto(sk, psock);
833 rcu_assign_sk_user_data(sk, NULL);
834 if (psock->progs.stream_parser)
835 sk_psock_stop_strp(sk, psock);
836 else if (psock->progs.stream_verdict || psock->progs.skb_verdict)
837 sk_psock_stop_verdict(sk, psock);
838 write_unlock_bh(&sk->sk_callback_lock);
840 sk_psock_stop(psock, false);
842 INIT_RCU_WORK(&psock->rwork, sk_psock_destroy);
843 queue_rcu_work(system_wq, &psock->rwork);
845 EXPORT_SYMBOL_GPL(sk_psock_drop);
847 static int sk_psock_map_verd(int verdict, bool redir)
851 return redir ? __SK_REDIRECT : __SK_PASS;
860 int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
863 struct bpf_prog *prog;
867 prog = READ_ONCE(psock->progs.msg_parser);
868 if (unlikely(!prog)) {
873 sk_msg_compute_data_pointers(msg);
875 ret = bpf_prog_run_pin_on_cpu(prog, msg);
876 ret = sk_psock_map_verd(ret, msg->sk_redir);
877 psock->apply_bytes = msg->apply_bytes;
878 if (ret == __SK_REDIRECT) {
880 sock_put(psock->sk_redir);
881 psock->sk_redir = msg->sk_redir;
882 if (!psock->sk_redir) {
886 sock_hold(psock->sk_redir);
892 EXPORT_SYMBOL_GPL(sk_psock_msg_verdict);
894 static int sk_psock_skb_redirect(struct sk_psock *from, struct sk_buff *skb)
896 struct sk_psock *psock_other;
897 struct sock *sk_other;
899 sk_other = skb_bpf_redirect_fetch(skb);
900 /* This error is a buggy BPF program, it returned a redirect
901 * return code, but then didn't set a redirect interface.
903 if (unlikely(!sk_other)) {
904 skb_bpf_redirect_clear(skb);
905 sock_drop(from->sk, skb);
908 psock_other = sk_psock(sk_other);
909 /* This error indicates the socket is being torn down or had another
910 * error that caused the pipe to break. We can't send a packet on
911 * a socket that is in this state so we drop the skb.
913 if (!psock_other || sock_flag(sk_other, SOCK_DEAD)) {
914 skb_bpf_redirect_clear(skb);
915 sock_drop(from->sk, skb);
918 spin_lock_bh(&psock_other->ingress_lock);
919 if (!sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED)) {
920 spin_unlock_bh(&psock_other->ingress_lock);
921 skb_bpf_redirect_clear(skb);
922 sock_drop(from->sk, skb);
926 skb_queue_tail(&psock_other->ingress_skb, skb);
927 schedule_work(&psock_other->work);
928 spin_unlock_bh(&psock_other->ingress_lock);
932 static void sk_psock_tls_verdict_apply(struct sk_buff *skb,
933 struct sk_psock *from, int verdict)
937 sk_psock_skb_redirect(from, skb);
946 int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb)
948 struct bpf_prog *prog;
952 prog = READ_ONCE(psock->progs.stream_verdict);
956 skb_bpf_redirect_clear(skb);
957 ret = bpf_prog_run_pin_on_cpu(prog, skb);
958 ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
961 sk_psock_tls_verdict_apply(skb, psock, ret);
965 EXPORT_SYMBOL_GPL(sk_psock_tls_strp_read);
967 static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
970 struct sock *sk_other;
977 sk_other = psock->sk;
978 if (sock_flag(sk_other, SOCK_DEAD) ||
979 !sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
980 skb_bpf_redirect_clear(skb);
984 skb_bpf_set_ingress(skb);
986 /* If the queue is empty then we can submit directly
987 * into the msg queue. If its not empty we have to
988 * queue work otherwise we may get OOO data. Otherwise,
989 * if sk_psock_skb_ingress errors will be handled by
990 * retrying later from workqueue.
992 if (skb_queue_empty(&psock->ingress_skb)) {
995 if (skb_bpf_strparser(skb)) {
996 struct strp_msg *stm = strp_msg(skb);
1001 err = sk_psock_skb_ingress_self(psock, skb, off, len);
1004 spin_lock_bh(&psock->ingress_lock);
1005 if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
1006 skb_queue_tail(&psock->ingress_skb, skb);
1007 schedule_work(&psock->work);
1010 spin_unlock_bh(&psock->ingress_lock);
1012 skb_bpf_redirect_clear(skb);
1018 err = sk_psock_skb_redirect(psock, skb);
1023 sock_drop(psock->sk, skb);
1029 static void sk_psock_write_space(struct sock *sk)
1031 struct sk_psock *psock;
1032 void (*write_space)(struct sock *sk) = NULL;
1035 psock = sk_psock(sk);
1036 if (likely(psock)) {
1037 if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED))
1038 schedule_work(&psock->work);
1039 write_space = psock->saved_write_space;
1046 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
1047 static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb)
1049 struct sk_psock *psock;
1050 struct bpf_prog *prog;
1051 int ret = __SK_DROP;
1056 psock = sk_psock(sk);
1057 if (unlikely(!psock)) {
1061 prog = READ_ONCE(psock->progs.stream_verdict);
1065 skb_bpf_redirect_clear(skb);
1066 ret = bpf_prog_run_pin_on_cpu(prog, skb);
1068 skb_bpf_set_strparser(skb);
1069 ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
1072 sk_psock_verdict_apply(psock, skb, ret);
1077 static int sk_psock_strp_read_done(struct strparser *strp, int err)
1082 static int sk_psock_strp_parse(struct strparser *strp, struct sk_buff *skb)
1084 struct sk_psock *psock = container_of(strp, struct sk_psock, strp);
1085 struct bpf_prog *prog;
1089 prog = READ_ONCE(psock->progs.stream_parser);
1091 skb->sk = psock->sk;
1092 ret = bpf_prog_run_pin_on_cpu(prog, skb);
1099 /* Called with socket lock held. */
1100 static void sk_psock_strp_data_ready(struct sock *sk)
1102 struct sk_psock *psock;
1105 psock = sk_psock(sk);
1106 if (likely(psock)) {
1107 if (tls_sw_has_ctx_rx(sk)) {
1108 psock->saved_data_ready(sk);
1110 write_lock_bh(&sk->sk_callback_lock);
1111 strp_data_ready(&psock->strp);
1112 write_unlock_bh(&sk->sk_callback_lock);
1118 int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock)
1120 static const struct strp_callbacks cb = {
1121 .rcv_msg = sk_psock_strp_read,
1122 .read_sock_done = sk_psock_strp_read_done,
1123 .parse_msg = sk_psock_strp_parse,
1126 return strp_init(&psock->strp, sk, &cb);
1129 void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock)
1131 if (psock->saved_data_ready)
1134 psock->saved_data_ready = sk->sk_data_ready;
1135 sk->sk_data_ready = sk_psock_strp_data_ready;
1136 sk->sk_write_space = sk_psock_write_space;
1139 void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
1141 psock_set_prog(&psock->progs.stream_parser, NULL);
1143 if (!psock->saved_data_ready)
1146 sk->sk_data_ready = psock->saved_data_ready;
1147 psock->saved_data_ready = NULL;
1148 strp_stop(&psock->strp);
1151 static void sk_psock_done_strp(struct sk_psock *psock)
1153 /* Parser has been stopped */
1154 if (psock->progs.stream_parser)
1155 strp_done(&psock->strp);
1158 static void sk_psock_done_strp(struct sk_psock *psock)
1161 #endif /* CONFIG_BPF_STREAM_PARSER */
1163 static int sk_psock_verdict_recv(read_descriptor_t *desc, struct sk_buff *skb,
1164 unsigned int offset, size_t orig_len)
1166 struct sock *sk = (struct sock *)desc->arg.data;
1167 struct sk_psock *psock;
1168 struct bpf_prog *prog;
1169 int ret = __SK_DROP;
1172 /* clone here so sk_eat_skb() in tcp_read_sock does not drop our data */
1173 skb = skb_clone(skb, GFP_ATOMIC);
1175 desc->error = -ENOMEM;
1180 psock = sk_psock(sk);
1181 if (unlikely(!psock)) {
1186 prog = READ_ONCE(psock->progs.stream_verdict);
1188 prog = READ_ONCE(psock->progs.skb_verdict);
1192 skb_bpf_redirect_clear(skb);
1193 ret = bpf_prog_run_pin_on_cpu(prog, skb);
1194 ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
1197 if (sk_psock_verdict_apply(psock, skb, ret) < 0)
1204 static void sk_psock_verdict_data_ready(struct sock *sk)
1206 struct socket *sock = sk->sk_socket;
1207 read_descriptor_t desc;
1209 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
1216 sock->ops->read_sock(sk, &desc, sk_psock_verdict_recv);
1219 void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)
1221 if (psock->saved_data_ready)
1224 psock->saved_data_ready = sk->sk_data_ready;
1225 sk->sk_data_ready = sk_psock_verdict_data_ready;
1226 sk->sk_write_space = sk_psock_write_space;
1229 void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)
1231 psock_set_prog(&psock->progs.stream_verdict, NULL);
1232 psock_set_prog(&psock->progs.skb_verdict, NULL);
1234 if (!psock->saved_data_ready)
1237 sk->sk_data_ready = psock->saved_data_ready;
1238 psock->saved_data_ready = NULL;