BPF List
 help / color / mirror / Atom feed
From: Jakub Sitnicki <jakub@cloudflare.com>
To: Jiayuan Chen <mrpre@163.com>
Cc: John Fastabend <john.fastabend@gmail.com>,
	 bpf@vger.kernel.org, martin.lau@linux.dev,  ast@kernel.org,
	 edumazet@google.com, davem@davemloft.net,  dsahern@kernel.org,
	 kuba@kernel.org, pabeni@redhat.com,
	 linux-kernel@vger.kernel.org,  song@kernel.org,
	andrii@kernel.org,  mhal@rbox.co,  yonghong.song@linux.dev,
	daniel@iogearbox.net,  xiyou.wangcong@gmail.com,
	 horms@kernel.org
Subject: Re: [PATCH bpf v3 1/2] bpf: fix wrong copied_seq calculation
Date: Fri, 27 Dec 2024 15:09:57 +0100	[thread overview]
Message-ID: <87v7v5z07e.fsf@cloudflare.com> (raw)
In-Reply-To: <lu7yzq2rsbu2jkffpm4pyinvggs6hrjyyi2h6udtgcq2thfs4k@3b2qsgjo4owm> (Jiayuan Chen's message of "Tue, 24 Dec 2024 15:16:34 +0800")

On Tue, Dec 24, 2024 at 03:16 PM +08, Jiayuan Chen wrote:
> On Mon, Dec 23, 2024 at 11:57:58PM +0800, Jakub Sitnicki wrote:
>> On Mon, Dec 23, 2024 at 09:57 PM +01, Jakub Sitnicki wrote:
>> > On Thu, Dec 19, 2024 at 05:30 PM +08, Jiayuan Chen wrote:
>> >> Currently, not all modules using strparser have issues with
>> >> copied_seq miscalculation. The issue exists mainly with
>> >> bpf::sockmap + strparser because bpf::sockmap implements a
>> >> proprietary read interface for user-land: tcp_bpf_recvmsg_parser().
>> >>
>> >> Both this and strp_recv->tcp_read_sock update copied_seq, leading
>> >> to errors.
>> >>
>> >> This is why I rewrote the tcp_read_sock() interface specifically for
>> >> bpf::sockmap.
>> >
>> > All right. Looks like reusing read_skb is not going to pan out.
>> >
>> > But I think we should not give up just yet. It's easy to add new code.
>> >
>> > We can try to break up and parametrize tcp_read_sock - if other
>> > maintainers are not against it. Does something like this work for you?
>> >
>> >   https://github.com/jsitnicki/linux/commits/review/stp-copied_seq/idea-2/
>> 
>> Actually it reads better if we just add early bailout to tcp_read_sock:
>> 
>>   https://github.com/jsitnicki/linux/commits/review/stp-copied_seq/idea-2.1/
>> 
>> ---8<---
>> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
>> index 6a07d98017f7..6564ea3b6cd4 100644
>> --- a/net/ipv4/tcp.c
>> +++ b/net/ipv4/tcp.c
>> @@ -1565,12 +1565,13 @@ EXPORT_SYMBOL(tcp_recv_skb);
>>   *	  or for 'peeking' the socket using this routine
>>   *	  (although both would be easy to implement).
>>   */
>> -int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
>> -		  sk_read_actor_t recv_actor)
>> +static inline int __tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
>> +				  sk_read_actor_t recv_actor, bool noack,
>> +				  u32 *copied_seq)
>>  {
>>  	struct sk_buff *skb;
>>  	struct tcp_sock *tp = tcp_sk(sk);
>> -	u32 seq = tp->copied_seq;
>> +	u32 seq = *copied_seq;
>>  	u32 offset;
>>  	int copied = 0;
>>  
>> @@ -1624,9 +1625,12 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
>>  		tcp_eat_recv_skb(sk, skb);
>>  		if (!desc->count)
>>  			break;
>> -		WRITE_ONCE(tp->copied_seq, seq);
>> +		WRITE_ONCE(*copied_seq, seq);
>>  	}
>> -	WRITE_ONCE(tp->copied_seq, seq);
>> +	WRITE_ONCE(*copied_seq, seq);
>> +
>> +	if (noack)
>> +		goto out;
>>  
>>  	tcp_rcv_space_adjust(sk);
>>  
>> @@ -1635,10 +1639,25 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
>>  		tcp_recv_skb(sk, seq, &offset);
>>  		tcp_cleanup_rbuf(sk, copied);
>>  	}
>> +out:
>>  	return copied;
>>  }
>> +
>> +int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
>> +		  sk_read_actor_t recv_actor)
>> +{
>> +	return __tcp_read_sock(sk, desc, recv_actor, false,
>> +			       &tcp_sk(sk)->copied_seq);
>> +}
>>  EXPORT_SYMBOL(tcp_read_sock);
>>  
>> +int tcp_read_sock_noack(struct sock *sk, read_descriptor_t *desc,
>> +			sk_read_actor_t recv_actor, u32 *copied_seq)
>> +{
>> +	return __tcp_read_sock(sk, desc, recv_actor, true, copied_seq);
>> +}
>> +EXPORT_SYMBOL(tcp_read_sock_noack);
>> +
>>  int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)
>>  {
>>  	struct sk_buff *skb;
>
> This modification definitely reduces code duplication and makes it more
> elegant compared to my previous idea. Also If we want to avoid modifying
> the strp code and not introduce new ops, perhaps we could revert to the
> simplest solution:
> '''
> void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock)
> {
>     ...
>     sk->sk_data_ready = sk_psock_strp_data_ready;
>     /* Replacement */
>     psock->saved_read_sock = sk->sk_socket->ops->read_sock;
>     sk->sk_socket->ops->read_sock = tcp_read_sock_noack;
> }
> '''
> If acceptable, I can incorporate this approach in the next patch version.

SGTM

  reply	other threads:[~2024-12-27 14:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-18  5:34 [PATCH bpf v3 0/2] bpf: fix wrong copied_seq calculation and add tests Jiayuan Chen
2024-12-18  5:34 ` [PATCH bpf v3 1/2] bpf: fix wrong copied_seq calculation Jiayuan Chen
2024-12-18 15:35   ` Jakub Sitnicki
2024-12-19  9:30     ` Jiayuan Chen
2024-12-20  7:06       ` John Fastabend
2024-12-23 20:57       ` Jakub Sitnicki
2024-12-23 22:57         ` Jakub Sitnicki
2024-12-24  7:16           ` Jiayuan Chen
2024-12-27 14:09             ` Jakub Sitnicki [this message]
2024-12-18  5:34 ` [PATCH bpf v3 2/2] selftests/bpf: add strparser test for bpf Jiayuan Chen

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=87v7v5z07e.fsf@cloudflare.com \
    --to=jakub@cloudflare.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mhal@rbox.co \
    --cc=mrpre@163.com \
    --cc=pabeni@redhat.com \
    --cc=song@kernel.org \
    --cc=xiyou.wangcong@gmail.com \
    --cc=yonghong.song@linux.dev \
    /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