Re: [PATCH net-next] tcp: use likely() for srtt check in tcp_rtt_estimator()

Eric Dumazet <[email protected]>
Newsgroups org.kernel.vger.netdev,org.kernel.vger.linux-kernel
Message-ID <CANn89iK8KSx+EfuANe6JuP=K0DrgTAiToPDH5L0chOj+uO9NUA@mail.gmail.com>
On Sat, Aug 8, 2026 at 1:01 PM Ziran Zhang <[email protected]> wrote:
>
> In tcp_rtt_estimator(), srtt is initialized to a non-zero value
> after the first RTT measurement (srtt = m << 3) and never returns
> to zero for the lifetime of the connection. Thus, the "srtt != 0"
> branch is almost always true.
>
> Marking it with likely() helps the compiler generate better code
> for this hot path.
>
> No functional change is introduced.
>
> Signed-off-by: Ziran Zhang <[email protected]>
> ---
>  net/ipv4/tcp_input.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 5b6378b94..a4c4d4760 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -1089,7 +1089,7 @@ static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
>          * does not matter how to _calculate_ it. Seems, it was trap
>          * that VJ failed to avoid. 8)
>          */
> -       if (srtt != 0) {
> +       if (likely(srtt != 0)) {
>                 m -= (srtt >> 3);       /* m is now error in rtt est */
>                 srtt += m;              /* rtt = 7/8 rtt + 1/8 new */
>                 if (m < 0) {

Many flows are short lived, unfortunately.

I would suggest not adding a likely() here, and let FDO/LTO decide
here on a case by case.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.