From: Daniel Borkmann <daniel@iogearbox.net>
To: John Fastabend <john.fastabend@gmail.com>,
ast@kernel.org, jakub@cloudflare.com
Cc: bpf@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: [bpf PATCH 1/5] bpf, sockmap: fix partial copy_page_to_iter so progress can still be made
Date: Thu, 12 Nov 2020 01:19:29 +0100 [thread overview]
Message-ID: <1aa5f637-c044-3dc6-09d5-0b5dc0521f91@iogearbox.net> (raw)
In-Reply-To: <160477787531.608263.10144789972668918015.stgit@john-XPS-13-9370>
On 11/7/20 8:37 PM, John Fastabend wrote:
> If copy_page_to_iter() fails or even partially completes, but with fewer
> bytes copied than expected we currently reset sg.start and return EFAULT.
> This proves problematic if we already copied data into the user buffer
> before we return an error. Because we leave the copied data in the user
> buffer and fail to unwind the scatterlist so kernel side believes data
> has been copied and user side believes data has _not_ been received.
>
> Expected behavior should be to return number of bytes copied and then
> on the next read we need to return the error assuming its still there. This
> can happen if we have a copy length spanning multiple scatterlist elements
> and one or more complete before the error is hit.
>
> The error is rare enough though that my normal testing with server side
> programs, such as nginx, httpd, envoy, etc., I have never seen this. The
> only reliable way to reproduce that I've found is to stream movies over
> my browser for a day or so and wait for it to hang. Not very scientific,
> but with a few extra WARN_ON()s in the code the bug was obvious.
>
> When we review the errors from copy_page_to_iter() it seems we are hitting
> a page fault from copy_page_to_iter_iovec() where the code checks
> fault_in_pages_writeable(buf, copy) where buf is the user buffer. It
> also seems typical server applications don't hit this case.
>
> The other way to try and reproduce this is run the sockmap selftest tool
> test_sockmap with data verification enabled, but it doesn't reproduce the
> fault. Perhaps we can trigger this case artificially somehow from the
> test tools. I haven't sorted out a way to do that yet though.
>
> Fixes: 604326b41a6fb ("bpf, sockmap: convert to generic sk_msg interface")
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>
> ---
> net/ipv4/tcp_bpf.c | 15 ++++++++++-----
> 1 file changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
> index 37f4cb2bba5c..3709d679436e 100644
> --- a/net/ipv4/tcp_bpf.c
> +++ b/net/ipv4/tcp_bpf.c
> @@ -15,8 +15,8 @@ int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
> {
> struct iov_iter *iter = &msg->msg_iter;
> int peek = flags & MSG_PEEK;
> - int i, ret, copied = 0;
> struct sk_msg *msg_rx;
> + int i, copied = 0;
>
> msg_rx = list_first_entry_or_null(&psock->ingress_msg,
> struct sk_msg, list);
> @@ -37,10 +37,9 @@ int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
> page = sg_page(sge);
> if (copied + copy > len)
> copy = len - copied;
> - ret = copy_page_to_iter(page, sge->offset, copy, iter);
> - if (ret != copy) {
> - msg_rx->sg.start = i;
> - return -EFAULT;
> + copy = copy_page_to_iter(page, sge->offset, copy, iter);
> + if (!copy) {
> + return copied ? copied : -EFAULT;
> }
nit: no need for {}
>
> copied += copy;
> @@ -56,6 +55,11 @@ int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
> put_page(page);
> }
> } else {
> + /* Lets not optimize peek case if copy_page_to_iter
> + * didn't copy the entire length lets just break.
> + */
> + if (copy != sge->length)
> + goto out;
nit: return copied;
Rest lgtm for this one.
> sk_msg_iter_var_next(i);
> }
>
> @@ -82,6 +86,7 @@ int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
> struct sk_msg, list);
> }
>
> +out:
> return copied;
> }
> EXPORT_SYMBOL_GPL(__tcp_bpf_recvmsg);
>
>
next prev parent reply other threads:[~2020-11-12 1:32 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-07 19:37 [bpf PATCH 0/5] sockmap fixes John Fastabend
2020-11-07 19:37 ` [bpf PATCH 1/5] bpf, sockmap: fix partial copy_page_to_iter so progress can still be made John Fastabend
2020-11-12 0:19 ` Daniel Borkmann [this message]
2020-11-12 19:56 ` John Fastabend
2020-11-07 19:38 ` [bpf PATCH 2/5] bpf, sockmap: Ensure SO_RCVBUF memory is observed on ingress redirect John Fastabend
2020-11-07 19:38 ` [bpf PATCH 3/5] bpf, sockmap: Avoid returning unneeded EAGAIN when redirecting to self John Fastabend
2020-11-12 0:22 ` Daniel Borkmann
2020-11-12 19:52 ` John Fastabend
2020-11-07 19:38 ` [bpf PATCH 4/5] bpf, sockmap: Handle memory acct if skb_verdict prog redirects " John Fastabend
2020-11-07 19:39 ` [bpf PATCH 5/5] bpf, sockmap: Avoid failures from skb_to_sgvec when skb has frag_list John Fastabend
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1aa5f637-c044-3dc6-09d5-0b5dc0521f91@iogearbox.net \
--to=daniel@iogearbox.net \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=jakub@cloudflare.com \
--cc=john.fastabend@gmail.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox