Re: [PATCH v2 6/7] net/tcp: Add sne_lock to access SNEs

From: Paolo Abeni
Date: Mon Nov 27 2023 - 06:42:11 EST


On Fri, 2023-11-24 at 00:27 +0000, Dmitry Safonov wrote:
> RFC 5925 (6.2):
> > TCP-AO emulates a 64-bit sequence number space by inferring when to
> > increment the high-order 32-bit portion (the SNE) based on
> > transitions in the low-order portion (the TCP sequence number).
>
> snd_sne and rcv_sne are the upper 4 bytes of extended SEQ number.
> Unfortunately, reading two 4-bytes pointers can't be performed
> atomically (without synchronization).
>
> Let's keep it KISS and add an rwlock - that shouldn't create much
> contention as SNE are updated every 4Gb of traffic and the atomic region
> is quite small.
>
> Fixes: 64382c71a557 ("net/tcp: Add TCP-AO SNE support")
> Signed-off-by: Dmitry Safonov <dima@xxxxxxxxxx>
> ---
> include/net/tcp_ao.h | 2 +-
> net/ipv4/tcp_ao.c | 34 +++++++++++++++++++++-------------
> net/ipv4/tcp_input.c | 16 ++++++++++++++--
> 3 files changed, 36 insertions(+), 16 deletions(-)
>
> diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h
> index 647781080613..beea3e6b39e2 100644
> --- a/include/net/tcp_ao.h
> +++ b/include/net/tcp_ao.h
> @@ -123,6 +123,7 @@ struct tcp_ao_info {
> */
> u32 snd_sne;
> u32 rcv_sne;
> + rwlock_t sne_lock;

RW lock are problematic in the networking code, see commit
dbca1596bbb08318f5e3b3b99f8ca0a0d3830a65.

I think you can use a plain spinlock here, as both read and write
appears to be in the fastpath (?!?)

> @@ -781,8 +780,10 @@ int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb,
> *traffic_key = snd_other_key(*key);
> rnext_key = READ_ONCE(ao_info->rnext_key);
> *keyid = rnext_key->rcvid;
> - *sne = tcp_ao_compute_sne(READ_ONCE(ao_info->snd_sne),
> - snd_basis, seq);
> + read_lock_irqsave(&ao_info->sne_lock, flags);
> + *sne = tcp_ao_compute_sne(ao_info->snd_sne,
> + READ_ONCE(*snd_basis), seq);
> + read_unlock_irqrestore(&ao_info->sne_lock, flags);

Why are you using the irqsave variant? bh should suffice.

Cheers,

Paolo