From patchwork Sat May 22 19:14:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12274659 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 992C7C4707E for ; Sat, 22 May 2021 19:14:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6D92B611AD for ; Sat, 22 May 2021 19:14:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231361AbhEVTPy (ORCPT ); Sat, 22 May 2021 15:15:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231316AbhEVTPy (ORCPT ); Sat, 22 May 2021 15:15:54 -0400 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E719DC06174A; Sat, 22 May 2021 12:14:27 -0700 (PDT) Received: by mail-pj1-x102c.google.com with SMTP id b9-20020a17090a9909b029015cf9effaeaso9045809pjp.5; Sat, 22 May 2021 12:14:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=utSDi74JoxgeFkn6+VzF9KRMzUGip+skVUxRXuPWjX0=; b=O3Jb4RA9S0GMCsd4shWuiFkOxy9/nxrhDF27HPPnJ+uhndrD9sCZq9nvcDVnWy9OlF PvSblcJrheeTGOi2T2tSlmsLvlDLzYlyZrY6Jq4ogkCYLehrbVHdVPY/Jnr+LZKbjfdu RQNEzPg3DpPIPoSmB9DmbWUxvou8DqIivH0yN5biEPgb17f0j8PXXwY1p8uiPOQkcDle dly3nMKnligCnzktPvrcg/UcPuHswuO1kIJWHwvldWIYT/vbJlfwNP2aQMl/vVV9q2xf eXXeiDNSsOHlXT4a7ekpqKSfHOZbKNV7zok2RB8gzcda71sPXc1ll71bclx1Iqjakrcx 9hXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=utSDi74JoxgeFkn6+VzF9KRMzUGip+skVUxRXuPWjX0=; b=NbT1L7Ot5kPmi6Ftwss8vbR933fkqtgMypG1OuvlFHTBxbOR/RNnH+g+5FYfahiB1I 2JFBR5gYGQh007GzwgBFcrnwUYRrrUodvGo0flRG289wVRn54E2OA9XC31h8Yl2jl1nx eeWpVOUilWA8beqUO/1O0q5zjQHZWUVHVPTnDbncyn3PO0ngbesw3DqesIArY/1FjZWr jt9PXj/7DM6l9l3+X8ZFbzmU6EiJonD98Mghp+Ds9uYl/xB9DX9GZJIzAoU1FLhyIsE2 JPqMSMekg8hz9y6k4HGmqRwXsRGBmMrieIuxcsx2dB6seZ9gJhjZEwhhc0modb5sUAEc IvCg== X-Gm-Message-State: AOAM530pfN0tMBe3PkPOVqbTBq3+nCSln/zN0JTIghHL9i8GhLl7WDcn EJAZXirHIl0bAYTco4ENjqknVlF3931fxQ== X-Google-Smtp-Source: ABdhPJzOEq+IGq4SaVUSWZyo+VF/FOKwBPkfnUWcIV22wqZeLeMlvNzY0x4Dg2d5DZyUcHFFX1mlzA== X-Received: by 2002:a17:90a:94ca:: with SMTP id j10mr16638908pjw.59.1621710867325; Sat, 22 May 2021 12:14:27 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:14cd:343f:4e27:51d7]) by smtp.gmail.com with ESMTPSA id 5sm6677531pfe.32.2021.05.22.12.14.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 12:14:27 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v2 1/7] skmsg: improve udp_bpf_recvmsg() accuracy Date: Sat, 22 May 2021 12:14:05 -0700 Message-Id: <20210522191411.21446-2-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522191411.21446-1-xiyou.wangcong@gmail.com> References: <20210522191411.21446-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang I tried to reuse sk_msg_wait_data() for different protocols, but it turns out it can not be simply reused. For example, UDP actually uses two queues to receive skb: udp_sk(sk)->reader_queue and sk->sk_receive_queue. So we have to check both of them to know whether we have received any packet. Also, UDP does not lock the sock during BH Rx path, it makes no sense for its ->recvmsg() to lock the sock. It is always possible for ->recvmsg() to be called before packets actually arrive in the receive queue, we just use best effort to make it accurate here. Fixes: 1f5be6b3b063 ("udp: Implement udp_bpf_recvmsg() for sockmap") Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- include/linux/skmsg.h | 2 -- net/core/skmsg.c | 23 --------------------- net/ipv4/tcp_bpf.c | 24 +++++++++++++++++++++- net/ipv4/udp_bpf.c | 47 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 65 insertions(+), 31 deletions(-) diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h index aba0f0f429be..e3d080c299f6 100644 --- a/include/linux/skmsg.h +++ b/include/linux/skmsg.h @@ -126,8 +126,6 @@ int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from, struct sk_msg *msg, u32 bytes); int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from, struct sk_msg *msg, u32 bytes); -int sk_msg_wait_data(struct sock *sk, struct sk_psock *psock, int flags, - long timeo, int *err); int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg, int len, int flags); diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 43ce17a6a585..f9a81b314e4c 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -399,29 +399,6 @@ int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from, } EXPORT_SYMBOL_GPL(sk_msg_memcopy_from_iter); -int sk_msg_wait_data(struct sock *sk, struct sk_psock *psock, int flags, - long timeo, int *err) -{ - DEFINE_WAIT_FUNC(wait, woken_wake_function); - int ret = 0; - - if (sk->sk_shutdown & RCV_SHUTDOWN) - return 1; - - if (!timeo) - return ret; - - add_wait_queue(sk_sleep(sk), &wait); - sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); - ret = sk_wait_event(sk, &timeo, - !list_empty(&psock->ingress_msg) || - !skb_queue_empty(&sk->sk_receive_queue), &wait); - sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); - remove_wait_queue(sk_sleep(sk), &wait); - return ret; -} -EXPORT_SYMBOL_GPL(sk_msg_wait_data); - /* Receive sk_msg from psock->ingress_msg to @msg. */ int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg, int len, int flags) diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index ad9d17923fc5..bb49b52d7be8 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -163,6 +163,28 @@ static bool tcp_bpf_stream_read(const struct sock *sk) return !empty; } +static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock, int flags, + long timeo, int *err) +{ + DEFINE_WAIT_FUNC(wait, woken_wake_function); + int ret = 0; + + if (sk->sk_shutdown & RCV_SHUTDOWN) + return 1; + + if (!timeo) + return ret; + + add_wait_queue(sk_sleep(sk), &wait); + sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); + ret = sk_wait_event(sk, &timeo, + !list_empty(&psock->ingress_msg) || + !skb_queue_empty(&sk->sk_receive_queue), &wait); + sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); + remove_wait_queue(sk_sleep(sk), &wait); + return ret; +} + static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len) { @@ -188,7 +210,7 @@ static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, long timeo; timeo = sock_rcvtimeo(sk, nonblock); - data = sk_msg_wait_data(sk, psock, flags, timeo, &err); + data = tcp_msg_wait_data(sk, psock, flags, timeo, &err); if (data) { if (!sk_psock_queue_empty(psock)) goto msg_bytes_ready; diff --git a/net/ipv4/udp_bpf.c b/net/ipv4/udp_bpf.c index 954c4591a6fd..565a70040c57 100644 --- a/net/ipv4/udp_bpf.c +++ b/net/ipv4/udp_bpf.c @@ -21,6 +21,45 @@ static int sk_udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, return udp_prot.recvmsg(sk, msg, len, noblock, flags, addr_len); } +static bool udp_sk_has_data(struct sock *sk) +{ + return !skb_queue_empty(&udp_sk(sk)->reader_queue) || + !skb_queue_empty(&sk->sk_receive_queue); +} + +static bool psock_has_data(struct sk_psock *psock) +{ + return !skb_queue_empty(&psock->ingress_skb) || + !sk_psock_queue_empty(psock); +} + +#define udp_msg_has_data(__sk, __psock) \ + ({ udp_sk_has_data(__sk) || psock_has_data(__psock); }) + +static int udp_msg_wait_data(struct sock *sk, struct sk_psock *psock, int flags, + long timeo, int *err) +{ + DEFINE_WAIT_FUNC(wait, woken_wake_function); + int ret = 0; + + if (sk->sk_shutdown & RCV_SHUTDOWN) + return 1; + + if (!timeo) + return ret; + + add_wait_queue(sk_sleep(sk), &wait); + sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); + ret = udp_msg_has_data(sk, psock); + if (!ret) { + wait_woken(&wait, TASK_INTERRUPTIBLE, timeo); + ret = udp_msg_has_data(sk, psock); + } + sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); + remove_wait_queue(sk_sleep(sk), &wait); + return ret; +} + static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len) { @@ -34,8 +73,7 @@ static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, if (unlikely(!psock)) return sk_udp_recvmsg(sk, msg, len, nonblock, flags, addr_len); - lock_sock(sk); - if (sk_psock_queue_empty(psock)) { + if (!psock_has_data(psock)) { ret = sk_udp_recvmsg(sk, msg, len, nonblock, flags, addr_len); goto out; } @@ -47,9 +85,9 @@ static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, long timeo; timeo = sock_rcvtimeo(sk, nonblock); - data = sk_msg_wait_data(sk, psock, flags, timeo, &err); + data = udp_msg_wait_data(sk, psock, flags, timeo, &err); if (data) { - if (!sk_psock_queue_empty(psock)) + if (psock_has_data(psock)) goto msg_bytes_ready; ret = sk_udp_recvmsg(sk, msg, len, nonblock, flags, addr_len); goto out; @@ -62,7 +100,6 @@ static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, } ret = copied; out: - release_sock(sk); sk_psock_put(sk, psock); return ret; } From patchwork Sat May 22 19:14:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12274657 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97682C2B9F2 for ; Sat, 22 May 2021 19:14:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A4FA61183 for ; Sat, 22 May 2021 19:14:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231341AbhEVTPy (ORCPT ); Sat, 22 May 2021 15:15:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231310AbhEVTPx (ORCPT ); Sat, 22 May 2021 15:15:53 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5FB6C0613ED; Sat, 22 May 2021 12:14:28 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id 29so5864710pgu.11; Sat, 22 May 2021 12:14:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2SXBq0RQ1G77Xbs0xalfygk3bDY+dvdl+rT4iV6EI+0=; b=T83V5ESLM8NUisbiZnuKbKvdK/SGDei4HMI4n/CIqWYECkZk6w238Cnzr80SwYrf2J RbJEIKOWc8lOuOv3TYbalnMQH7JRWtYto3rTxjFzAo9e9eH/BnmFnGJvjEswBGPqBOYc uyp6HUnmqDli8Qj1AMthe6C9Heb5ugbO9LJxyK43CXwEhmmDk5TGBRMBhihuTRjVC9B0 1ZZ9YECN6qtRlF7+q4TN+gCQEeEO1QmnvEcjSsJ4mJO+MzrmMY7g3hT85jgLv2s5Z1vw 5Jd1vcuKgpgZsxeNvGDL258zbGba/Z9CA/kyhyIclK28b7p6EOLWrQ55B0PMY80qVAHV 0erA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2SXBq0RQ1G77Xbs0xalfygk3bDY+dvdl+rT4iV6EI+0=; b=AFg/yYHC6UsVI484AZe1/8PODx0UK9HrhS05Hhpc3KCBGSjDpBLxdiAPGBaRzxYKni 5IW9w/ZarBxnG2BOli/fSscMl7KsQhVbEy/wqTjC3c7MPl8vmbVY4EBFAaDyvNe2fDoH gLYgeAn0chYVkoQKi/xaGNDxHJhfYqmFnHZGl9RIx1w8jEf5sxZ4srZbJNHs/dDai8qi b+cIJXfyZZhr3Y1Lz9LUqNyBeZmND+ZLzjsUOLWELLZfPLX8ba6EK4x85EyJFBeZqZOz uyez3S6lcoWhLjs3C1/aDOG2LIW1gdBP4sRWxSAxhx0L/l5jIhgb9DdTwozb9UeXncoZ Fp+Q== X-Gm-Message-State: AOAM531XT0x8CSrE3pfND7NYuclnZR1/SMJVCw51GTQcpg0tVV/jW86S QRoUdyl3IEnMASt00P1hjoMAxrr5ZcKAQA== X-Google-Smtp-Source: ABdhPJzuYP5w2sWMMa3ZoM5sZp/MSGUKffcG20L7YSWdFeLIjERyJMXd4Sj2SZz77+l3yC8puS5LmA== X-Received: by 2002:a05:6a00:170c:b029:2dc:dd8f:e083 with SMTP id h12-20020a056a00170cb02902dcdd8fe083mr16265036pfc.77.1621710868301; Sat, 22 May 2021 12:14:28 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:14cd:343f:4e27:51d7]) by smtp.gmail.com with ESMTPSA id 5sm6677531pfe.32.2021.05.22.12.14.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 12:14:28 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , Jiang Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v2 2/7] selftests/bpf: Retry for EAGAIN in udp_redir_to_connected() Date: Sat, 22 May 2021 12:14:06 -0700 Message-Id: <20210522191411.21446-3-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522191411.21446-1-xiyou.wangcong@gmail.com> References: <20210522191411.21446-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang We use non-blocking sockets for testing sockmap redirections, and got some random EAGAIN errors from UDP tests. There is no guarantee the packet would be immediately available to receive as soon as it is sent out, even on the local host. For UDP, this is especially true because it does not lock the sock during BH (unlike the TCP path). This is probably why we only saw this error in UDP cases. No matter how hard we try to make the queue empty check accurate, it is always possible for recvmsg() to beat ->sk_data_ready(). Therefore, we should just retry in case of EAGAIN. Fixes: d6378af615275 ("selftests/bpf: Add a test case for udp sockmap") Reported-by: Jiang Wang Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- tools/testing/selftests/bpf/prog_tests/sockmap_listen.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c index 648d9ae898d2..01ab11259809 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -1610,6 +1610,7 @@ static void udp_redir_to_connected(int family, int sotype, int sock_mapfd, struct sockaddr_storage addr; int c0, c1, p0, p1; unsigned int pass; + int retries = 100; socklen_t len; int err, n; u64 value; @@ -1686,9 +1687,13 @@ static void udp_redir_to_connected(int family, int sotype, int sock_mapfd, if (pass != 1) FAIL("%s: want pass count 1, have %d", log_prefix, pass); +again: n = read(mode == REDIR_INGRESS ? p0 : c0, &b, 1); - if (n < 0) + if (n < 0) { + if (errno == EAGAIN && retries--) + goto again; FAIL_ERRNO("%s: read", log_prefix); + } if (n == 0) FAIL("%s: incomplete read", log_prefix); From patchwork Sat May 22 19:14:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12274663 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB88FC47081 for ; Sat, 22 May 2021 19:14:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C14A561177 for ; Sat, 22 May 2021 19:14:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231378AbhEVTP4 (ORCPT ); Sat, 22 May 2021 15:15:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231345AbhEVTPy (ORCPT ); Sat, 22 May 2021 15:15:54 -0400 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2386C0613ED; Sat, 22 May 2021 12:14:29 -0700 (PDT) Received: by mail-pl1-x630.google.com with SMTP id e15so6009054plh.1; Sat, 22 May 2021 12:14:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DV0T/Qr/nJx8jfvW3ukFjANC4TKgQt+cqMliLspYgy0=; b=ZE1N44WA4KPSr0Vi0rHkh0It84dEXbFFoW8rskLDxjORPbjDfvUT71qtgrQcQQj+5s pRNDdw2EdjqajmvFexzct+5igNBSPNQC9EUZZjvlhaqi+t9WQUDcNzXz77vJRZZCn01A 5fnNf6UH4TVIbzjwAhMKI3uGePVbe8uiuKfAIKIsKatEuzdU8K9zC8TK+8U7fElXriSs Bi4/HOIcdmv7iPDeLVh7rCE3rhYgVbdMHrtSkc9lZox4FLHmQ4GRb3pKi5Q5lUGge43g m/UQIOrl0TYHHxx9vxnfuSiB0vNw9ocYA87pJ2wN7Tv+t6b2sAV7NrdVqR1XgEyusP6x O+Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DV0T/Qr/nJx8jfvW3ukFjANC4TKgQt+cqMliLspYgy0=; b=BjhCxSUGQ89pUgFsImxaPSM0/9+/SN16ztd0cNnULlmm3c3EF77GnX39/Uo4qED1iQ rsWe1Dydyt44Dn0dSr5ja6uWxbq6nnLKKIp2VtjYZC8w7Oap6VAm2WvvUHO2Z5n2WITE Sy70ExeWIR6I6WFfNlhy2ki8sw9l5+vGZyFeWs0GinBMFNojg8f2W0hmJt1wEfg0VkLq XIyEMjjSueX0LqEJa3N8/6VQHDAjgvXM52U9AyMl1cTbuq5gDH4BL5pKJefXMk2x2Gcx rfgkq8WDKWhN5K0i0k42UcMV3ZWy4ejmhg/ls9kUjWJAwSJY2DnxrVAPUDffVZDJFs77 RnEA== X-Gm-Message-State: AOAM531moFcb7th9+kmfhgGswZAWZrqa2czKNKV5wG0a/qn+KAw+eM9x gpOy5zTsgVF9YAN0s5jUwXKyMYcT+xNq/g== X-Google-Smtp-Source: ABdhPJy7PKe6Msqh0+RN81iuU8fe0VxxYtUjCZz6JweSulFHGqMRrkIQPzViGZ6qJkNCYHsli2j+Tw== X-Received: by 2002:a17:90a:17a6:: with SMTP id q35mr17017410pja.118.1621710869228; Sat, 22 May 2021 12:14:29 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:14cd:343f:4e27:51d7]) by smtp.gmail.com with ESMTPSA id 5sm6677531pfe.32.2021.05.22.12.14.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 12:14:28 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v2 3/7] udp: fix a memory leak in udp_read_sock() Date: Sat, 22 May 2021 12:14:07 -0700 Message-Id: <20210522191411.21446-4-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522191411.21446-1-xiyou.wangcong@gmail.com> References: <20210522191411.21446-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang sk_psock_verdict_recv() clones the skb and uses the clone afterward, so udp_read_sock() should free the skb after using it, regardless of error or not. This fixes a real kmemleak. Fixes: d7f571188ecf ("udp: Implement ->read_sock() for sockmap") Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/ipv4/udp.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 15f5504adf5b..e31d67fd5183 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1798,11 +1798,13 @@ int udp_read_sock(struct sock *sk, read_descriptor_t *desc, if (used <= 0) { if (!copied) copied = used; + kfree_skb(skb); break; } else if (used <= skb->len) { copied += used; } + kfree_skb(skb); if (!desc->count) break; } From patchwork Sat May 22 19:14:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12274665 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08FA3C47083 for ; Sat, 22 May 2021 19:14:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DA8B8611AD for ; Sat, 22 May 2021 19:14:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231386AbhEVTP4 (ORCPT ); Sat, 22 May 2021 15:15:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231377AbhEVTPz (ORCPT ); Sat, 22 May 2021 15:15:55 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4CF6C061574; Sat, 22 May 2021 12:14:30 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id t11so12674955pjm.0; Sat, 22 May 2021 12:14:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lX//LiYCi9V25eCHy4xTyI2ugZeYUnZTGWUsgm/854w=; b=YtKsDUBlMiq8Xu8M6BOGp2WSgVJrRDJGEjagsEAR2G1OHHUE2imDtY9gMzlNCRcV+Z 7X8f7pnq04BfpIjdSEGD01K5uvcRW5NBgr3mJcRVORWKrpWaJc3qQdy0UroIPHB1TRCH 94mdH5hPnkA3cpjExUouL4749Tu3+jVpqNskRE1eBHAvTEad3yD8ZwBUhOm1zGZG1FTi dUh7spWTbjXiiwhCusKvj0bM85BxiMtfpPQeDj0NY3FA7OUrraxZvWkLRrmXP1nqIP4l fgqwsyPvyjQsux2gpEU6ReWF7nRq2vc2FCNlDSGH0+e4vmYenC/cMp39Kqg/NznZoHI3 50sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lX//LiYCi9V25eCHy4xTyI2ugZeYUnZTGWUsgm/854w=; b=byCo5yAQNsNCB9HM+Av/vziBZACTRVeC9jMHZgc41Th8G/IVM9vNnG3IEErNRa2yF2 ybBZgqvoiC3/tGLyrHGWRGtLeFfPwu/SLQ1pUI6c7qLgrBWhziFMY2w6Ja9JnGo+RSQu +/QABiwmAldzrlLPzAiYwawoAf5stPAZtBqqm6JPpiRjAgzSbrYc752Oj4IQINhk1hIe s8A9U9/YgoqOjtPaIuYMMnYWS6MQbk9AnHd9fhvQARrKoUFT9N4fDmRGC3mxsD8GpYOx oDDlNF7z1NhQ6rCUwRt17qkF36FNszEtgXv0aynuafE64/ke6GvQryMPh150BNB+zyzM XsOw== X-Gm-Message-State: AOAM530VOoSRMf78eaTXknAE1XFVjlMfPe1ZB81V3at3VKWU7VG2DL3O nkwNzc0hKYKObb0YexTvXJG+o9asELKF6g== X-Google-Smtp-Source: ABdhPJwpv+3abu5TxI4O3VoXl9HK1yDkHp2NsdLBZMhTa8Ig7sgXK2JHGHdXbJaE7KGBeFQXx3nrgg== X-Received: by 2002:a17:90b:1185:: with SMTP id gk5mr16468559pjb.168.1621710870161; Sat, 22 May 2021 12:14:30 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:14cd:343f:4e27:51d7]) by smtp.gmail.com with ESMTPSA id 5sm6677531pfe.32.2021.05.22.12.14.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 12:14:29 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v2 4/7] skmsg: fix a memory leak in sk_psock_verdict_apply() Date: Sat, 22 May 2021 12:14:08 -0700 Message-Id: <20210522191411.21446-5-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522191411.21446-1-xiyou.wangcong@gmail.com> References: <20210522191411.21446-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang If the dest psock does not set SK_PSOCK_TX_ENABLED, then the skb can't be queued anywhere so hould be dropped. This one is found during code review. Fixes: 799aa7f98d53 ("skmsg: Avoid lock_sock() in sk_psock_backlog()") Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/core/skmsg.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/core/skmsg.c b/net/core/skmsg.c index f9a81b314e4c..de68a3cd33f1 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -922,8 +922,11 @@ static void sk_psock_verdict_apply(struct sk_psock *psock, if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) { skb_queue_tail(&psock->ingress_skb, skb); schedule_work(&psock->work); + err = 0; } spin_unlock_bh(&psock->ingress_lock); + if (err < 0) + goto out_free; } break; case __SK_REDIRECT: From patchwork Sat May 22 19:14:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12274667 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F6B5C47082 for ; Sat, 22 May 2021 19:14:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3F21261183 for ; Sat, 22 May 2021 19:14:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231411AbhEVTP7 (ORCPT ); Sat, 22 May 2021 15:15:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231393AbhEVTP5 (ORCPT ); Sat, 22 May 2021 15:15:57 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EB7EC061574; Sat, 22 May 2021 12:14:31 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id q6so12657273pjj.2; Sat, 22 May 2021 12:14:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qUzy5jKeZW77NKJ44yeMmAeQlr5TyXVRobrel27IuQw=; b=WdIEFLLzujUuZSeaRc6G11W+YBz9vNnRz28GGhaTIPUw2xEXBxBOEvRndIZrnBGFiC E4PCN1/xO7pHU8kHBZtsPSdx6lCBDqvWNSiSeYfWEdElSlB5XIYjnap+Wufx7EvJUJbs w8LEJCmS32BrI5Vc/uQw8Pyf8qlJgfT0HOUT+KPLQmYfFL03LcRyiw6VTVAnyoeoVD65 ivc5q5fLEeO/B0uLFivCa8IhYtE06blZem5SM50NCMBKQnHlamWUAFv2kMlUQ/uOP7jj nM4f2cTBA3TGq9I/fNRjDNWff8mLQB0Icl5M14IKmEunW0rMBVN+BEiigFMRqqQbU2ee roIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qUzy5jKeZW77NKJ44yeMmAeQlr5TyXVRobrel27IuQw=; b=kxsSPx6K354wF7XCitwb40UZgzE0Pp/JUeWN/lo/fjMs0nLQVlTj5FXfT36nsEQegq 5/yUChe54HfDks9yAlXjHkwroZPXkAFVlHd7VpOSoTwtC2HHduByCkOV8KjQOEXe/Jco nL/i9YueTgGqTUeZ1l/GZeXdXS5pUlKQnfniftqu553gm1m6xhbRaTUYuVLA/L2YKGe8 rksWWKnSQ9MC6zs1EAwgBmeR1SGV+Kw+jz9Nm27nC5YWwSolp28p6XaKho557V0ktd+9 vZSgIi1g5XEYO0TuJ57ZLZJLFFfedXzLBW6Uc6HHTxuzEli/rgUagiGNpMgnLH+xM98X 8l7w== X-Gm-Message-State: AOAM530r98ckaMJCUv8x5Ps1awirPQf+jvSQ7T+sstf6OpR/yqhnVp6B tJimN5A8nKT4MuFUTkqVjm4ntgu3gjXE5A== X-Google-Smtp-Source: ABdhPJxLPbOLt9gT0paVc3/T3RxYK9QxmK1a/+DHV5zsS91kpbdDj0IHlndyD+0GHiw4Iq9SBocn5g== X-Received: by 2002:a17:90a:8b8a:: with SMTP id z10mr16984010pjn.22.1621710871108; Sat, 22 May 2021 12:14:31 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:14cd:343f:4e27:51d7]) by smtp.gmail.com with ESMTPSA id 5sm6677531pfe.32.2021.05.22.12.14.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 12:14:30 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v2 5/7] skmsg: teach sk_psock_verdict_apply() to return errors Date: Sat, 22 May 2021 12:14:09 -0700 Message-Id: <20210522191411.21446-6-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522191411.21446-1-xiyou.wangcong@gmail.com> References: <20210522191411.21446-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang Currently sk_psock_verdict_apply() is void, but it handles some error conditions too. Its caller is impossible to learn whether it succeeds or fails, especially sk_psock_verdict_recv(). Make it return int to indicate error cases and propagate errors to callers properly. Fixes: ef5659280eb1 ("bpf, sockmap: Allow skipping sk_skb parser program") Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/core/skmsg.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/net/core/skmsg.c b/net/core/skmsg.c index de68a3cd33f1..335fc60f5d22 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -824,7 +824,7 @@ int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock, } EXPORT_SYMBOL_GPL(sk_psock_msg_verdict); -static void sk_psock_skb_redirect(struct sk_buff *skb) +static int sk_psock_skb_redirect(struct sk_buff *skb) { struct sk_psock *psock_other; struct sock *sk_other; @@ -835,7 +835,7 @@ static void sk_psock_skb_redirect(struct sk_buff *skb) */ if (unlikely(!sk_other)) { kfree_skb(skb); - return; + return -EIO; } psock_other = sk_psock(sk_other); /* This error indicates the socket is being torn down or had another @@ -844,18 +844,19 @@ static void sk_psock_skb_redirect(struct sk_buff *skb) */ if (!psock_other || sock_flag(sk_other, SOCK_DEAD)) { kfree_skb(skb); - return; + return -EIO; } spin_lock_bh(&psock_other->ingress_lock); if (!sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED)) { spin_unlock_bh(&psock_other->ingress_lock); kfree_skb(skb); - return; + return -EIO; } skb_queue_tail(&psock_other->ingress_skb, skb); schedule_work(&psock_other->work); spin_unlock_bh(&psock_other->ingress_lock); + return 0; } static void sk_psock_tls_verdict_apply(struct sk_buff *skb, struct sock *sk, int verdict) @@ -892,14 +893,15 @@ int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb) } EXPORT_SYMBOL_GPL(sk_psock_tls_strp_read); -static void sk_psock_verdict_apply(struct sk_psock *psock, - struct sk_buff *skb, int verdict) +static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb, + int verdict) { struct sock *sk_other; - int err = -EIO; + int err = 0; switch (verdict) { case __SK_PASS: + err = -EIO; sk_other = psock->sk; if (sock_flag(sk_other, SOCK_DEAD) || !sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) { @@ -930,13 +932,15 @@ static void sk_psock_verdict_apply(struct sk_psock *psock, } break; case __SK_REDIRECT: - sk_psock_skb_redirect(skb); + err = sk_psock_skb_redirect(skb); break; case __SK_DROP: default: out_free: kfree_skb(skb); } + + return err; } static void sk_psock_write_space(struct sock *sk) @@ -1103,7 +1107,8 @@ static int sk_psock_verdict_recv(read_descriptor_t *desc, struct sk_buff *skb, ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb)); skb->sk = NULL; } - sk_psock_verdict_apply(psock, skb, ret); + if (sk_psock_verdict_apply(psock, skb, ret) < 0) + len = 0; out: rcu_read_unlock(); return len; From patchwork Sat May 22 19:14:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12274671 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75A74C4707F for ; Sat, 22 May 2021 19:14:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5231E61177 for ; Sat, 22 May 2021 19:14:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231428AbhEVTQA (ORCPT ); Sat, 22 May 2021 15:16:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231402AbhEVTP6 (ORCPT ); Sat, 22 May 2021 15:15:58 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82190C06174A; Sat, 22 May 2021 12:14:32 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id s4so11079212plg.12; Sat, 22 May 2021 12:14:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DcX8fhKGj8R427RducM9Ix+CQKTH1UdCiDQd5Xwx4xg=; b=D+54d9ffWQpaqYrrntfrvzAGdAd1QgOlVpkhz3uQVzK6ELaM0t6NnbQgCw+5aNan0g HQHQrdAwMTrpbmTaFZ8Z2+CZ53Z+TcZROpfD4Pvth2WwPAei8h/Fd6rEZV1nfxdUZcal SM5jmQwTXiIP3Bvih7uyfctv9KPYw3DafdxMt4VtcX55sFIyVTq+UniIGjgSN463XOrV 2DivQL7UwHbteKAiS9zbjvENM5xirncUNp6hW8x/NPmOb3R+YesikdxuV61yQvIBArkl TSRENdUzYENwAMJnWE7wFPKJh5gx9TbA2uG/8at9I7anQE+izehFmRbduj+Z7SkTJxb4 syeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DcX8fhKGj8R427RducM9Ix+CQKTH1UdCiDQd5Xwx4xg=; b=lbYsTe3BDkOiZsaxJy88E3Mdi+WVxm7ygZ4/MFpk+DnA5sshuo6ZM4i3yuSMX4Yx7p 8LRUZpgPfal3T7Y5cwQtLfpBPFloVyGmYN3md9uIUItM0/qGqt9q33ltCjFGvRP6PdYH 9pA6scoTcEHT5X25R9VqqdC/6559WRDD4xnDJvwaST5Q4OxzbB//AO3+XECPZDhFkJA/ 8UxgaYXT0LKdHSCuzq+Mv1z6zXWlsYwwPkHin0OQ5rmmrbMaZQGVJrxb6hUfHX+tkzWb nyowYklmNjwnBLioCRCO7SulKpxzQ3erqioH/uRECYGVdaW/PXka48zOnQ6PmliG4Irg fLbw== X-Gm-Message-State: AOAM533CRR7iR3aaz+aMzN/ImgfrZmqBkuaycj68UomrrqyDrxRaAzQ0 6b8qRsGXyZzXWjbjyVED6zn9aYmrwW2qvQ== X-Google-Smtp-Source: ABdhPJx/EQ9Xv5OYmvfL0rfAfehaHbJm+Rf8GFBhJw92L0ENx+dYXSh2sfA3ZYrmxWwZ7CBgP8pYvg== X-Received: by 2002:a17:902:b609:b029:ec:e80d:7ebd with SMTP id b9-20020a170902b609b02900ece80d7ebdmr18484134pls.75.1621710872004; Sat, 22 May 2021 12:14:32 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:14cd:343f:4e27:51d7]) by smtp.gmail.com with ESMTPSA id 5sm6677531pfe.32.2021.05.22.12.14.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 12:14:31 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v2 6/7] skmsg: pass source psock to sk_psock_skb_redirect() Date: Sat, 22 May 2021 12:14:10 -0700 Message-Id: <20210522191411.21446-7-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522191411.21446-1-xiyou.wangcong@gmail.com> References: <20210522191411.21446-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang sk_psock_skb_redirect() only takes skb as a parameter, we will need to know where this skb is from, so just pass the source psock to this function as a new parameter. This patch prepares for the next one. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/core/skmsg.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 335fc60f5d22..7b2c25038a48 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -824,7 +824,7 @@ int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock, } EXPORT_SYMBOL_GPL(sk_psock_msg_verdict); -static int sk_psock_skb_redirect(struct sk_buff *skb) +static int sk_psock_skb_redirect(struct sk_psock *from, struct sk_buff *skb) { struct sk_psock *psock_other; struct sock *sk_other; @@ -859,11 +859,12 @@ static int sk_psock_skb_redirect(struct sk_buff *skb) return 0; } -static void sk_psock_tls_verdict_apply(struct sk_buff *skb, struct sock *sk, int verdict) +static void sk_psock_tls_verdict_apply(struct sk_buff *skb, + struct sk_psock *from, int verdict) { switch (verdict) { case __SK_REDIRECT: - sk_psock_skb_redirect(skb); + sk_psock_skb_redirect(from, skb); break; case __SK_PASS: case __SK_DROP: @@ -887,7 +888,7 @@ int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb) ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb)); skb->sk = NULL; } - sk_psock_tls_verdict_apply(skb, psock->sk, ret); + sk_psock_tls_verdict_apply(skb, psock, ret); rcu_read_unlock(); return ret; } @@ -932,7 +933,7 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb, } break; case __SK_REDIRECT: - err = sk_psock_skb_redirect(skb); + err = sk_psock_skb_redirect(psock, skb); break; case __SK_DROP: default: From patchwork Sat May 22 19:14:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12274669 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88D77C47086 for ; Sat, 22 May 2021 19:14:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7CE1761073 for ; Sat, 22 May 2021 19:14:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231431AbhEVTQB (ORCPT ); Sat, 22 May 2021 15:16:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231345AbhEVTP7 (ORCPT ); Sat, 22 May 2021 15:15:59 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77A7FC061574; Sat, 22 May 2021 12:14:33 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id q15so16947076pgg.12; Sat, 22 May 2021 12:14:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0ExFCYsih7s7PdTmahlL0A+vWzuU6an/DuLev5Jw+4s=; b=JHYJx1UnCo418NP9GOf2tZxcIdzrw9W+qB4UwJr1SzVChrov3WK8VPknOuxwdHMRXi cBKEXhAXU/dMJIYlOZrDZGFCZZmbvlxsA995qq/QrEVc9AKsDXmIDh2Efpq6GVkPBDVV rmwBBnS8+15VDwdZ3OzAZj8bJgYQ3EsLF54DzAv1FSOuFaERdMi4gvwmi3eqK35GM5EZ 3qq2RHrjQXPueLBb9U3RiFrkJlWNSbzClQGq/SVKbbPupZs5KykXx3Vg+T1cBF39EdFN WBq+4N19rYMK4IcON1qvV0upvZTUbLl8naf1ROqvO8ou1kOQkPnm8mav7oXquvFFO0XH Ov8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0ExFCYsih7s7PdTmahlL0A+vWzuU6an/DuLev5Jw+4s=; b=X2jHqC1yBaUbrYuK4KD7GP9ifphF4mZwvPDh3emkk7KR0t6nhdlQzKS8L95WJPmJBi uXL+Lk/8FwQwODjMe+otP72Vutb5r8b1acVu797FV76nhMPLthDXjYgLHjbTm3EOOTIq t0BpYJQ88QdG+/fZybUiu/0uLkllH41B1Q0M6C4qyz6qNWhxFNyzjUKgPzRPifeP6oBp rddkiDhrZP/hRQqP3aStglXhSYk4E+Pa8oZZV6PU5cLJireZhWd1535EimDV1h2fxYDg i9DxINwYy4T/cS2LPqnqYMCjn1Cg3xTVDdhPRM9DdN6fNeGyGk8lhRWSt/7xoVzpUEPk 9LSw== X-Gm-Message-State: AOAM533n+FxoNZvfn5vNDtP1YQNrKJ9uFVv8MB10pZrCAkgYvbCKjCmU HBUmzFxckLyyBx5myEXrC0OmEKUxg71EJA== X-Google-Smtp-Source: ABdhPJzXQN+BbgDhpFggMreFL5FXJsZeEWMse6jtHlb72dNa33jcpKkbVyEj1e00P2yGPAiajYZ2Bw== X-Received: by 2002:a65:50c5:: with SMTP id s5mr5429636pgp.138.1621710872920; Sat, 22 May 2021 12:14:32 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:14cd:343f:4e27:51d7]) by smtp.gmail.com with ESMTPSA id 5sm6677531pfe.32.2021.05.22.12.14.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 12:14:32 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v2 7/7] skmsg: increase sk->sk_drops when dropping packets Date: Sat, 22 May 2021 12:14:11 -0700 Message-Id: <20210522191411.21446-8-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522191411.21446-1-xiyou.wangcong@gmail.com> References: <20210522191411.21446-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang It is hard to observe packet drops without increase relevant drop counters, here we should increase sk->sk_drops which is a protocol-independent counter. Fortunately psock is always assocaited with a struct sock, we can just use psock->sk. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/core/skmsg.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 7b2c25038a48..de3af8152d07 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -578,6 +578,12 @@ static int sk_psock_handle_skb(struct sk_psock *psock, struct sk_buff *skb, return sk_psock_skb_ingress(psock, skb); } +static void sock_drop(struct sock *sk, struct sk_buff *skb) +{ + sk_drops_add(sk, skb); + kfree_skb(skb); +} + static void sk_psock_backlog(struct work_struct *work) { struct sk_psock *psock = container_of(work, struct sk_psock, work); @@ -617,7 +623,7 @@ static void sk_psock_backlog(struct work_struct *work) /* Hard errors break pipe and stop xmit. */ sk_psock_report_error(psock, ret ? -ret : EPIPE); sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED); - kfree_skb(skb); + sock_drop(psock->sk, skb); goto end; } off += ret; @@ -625,7 +631,7 @@ static void sk_psock_backlog(struct work_struct *work) } while (len); if (!ingress) - kfree_skb(skb); + sock_drop(psock->sk, skb); } end: mutex_unlock(&psock->work_mutex); @@ -708,7 +714,7 @@ static void __sk_psock_zap_ingress(struct sk_psock *psock) while ((skb = skb_dequeue(&psock->ingress_skb)) != NULL) { skb_bpf_redirect_clear(skb); - kfree_skb(skb); + sock_drop(psock->sk, skb); } __sk_psock_purge_ingress_msg(psock); } @@ -834,7 +840,7 @@ static int sk_psock_skb_redirect(struct sk_psock *from, struct sk_buff *skb) * return code, but then didn't set a redirect interface. */ if (unlikely(!sk_other)) { - kfree_skb(skb); + sock_drop(from->sk, skb); return -EIO; } psock_other = sk_psock(sk_other); @@ -843,13 +849,13 @@ static int sk_psock_skb_redirect(struct sk_psock *from, struct sk_buff *skb) * a socket that is in this state so we drop the skb. */ if (!psock_other || sock_flag(sk_other, SOCK_DEAD)) { - kfree_skb(skb); + sock_drop(from->sk, skb); return -EIO; } spin_lock_bh(&psock_other->ingress_lock); if (!sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED)) { spin_unlock_bh(&psock_other->ingress_lock); - kfree_skb(skb); + sock_drop(from->sk, skb); return -EIO; } @@ -938,7 +944,7 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb, case __SK_DROP: default: out_free: - kfree_skb(skb); + sock_drop(psock->sk, skb); } return err; @@ -973,7 +979,7 @@ static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb) sk = strp->sk; psock = sk_psock(sk); if (unlikely(!psock)) { - kfree_skb(skb); + sock_drop(sk, skb); goto out; } prog = READ_ONCE(psock->progs.stream_verdict); @@ -1094,7 +1100,7 @@ static int sk_psock_verdict_recv(read_descriptor_t *desc, struct sk_buff *skb, psock = sk_psock(sk); if (unlikely(!psock)) { len = 0; - kfree_skb(skb); + sock_drop(sk, skb); goto out; } prog = READ_ONCE(psock->progs.stream_verdict);