Subject: Re: [PATCH net v2 1/2] mlx5: fix possible ptp queue fifo overflow

From: Jakub Kicinski <[email-masked]>
Date: Wed, 25 Jan 2023 14:02:02 -0800

On Tue, 24 Jan 2023 16:03:42 +0000 Vadim Fedorenko wrote:
> > Are you sure this works for all cases?
> > Directly comparing indexes of a ring buffer seems dangerous.
> > We'd need to compare like this:
> > 
> > 	(s16)(skb_cc - skb_id) < 0
> >  
> 
> Here I would like to count (and skip re-syncing) all the packets that 
> are not going to be in FIFO. Your suggestion will not work for the 
> simplest example. Imagine we have FIFO for 16 elements, and current 
> counters are:
>   (consumer) skb_cc = 13, (producer) skb_pc = 15, so 3 packets are in.
> Then skb_id = 10 arrives out-of-order. It will be counted because of 
> (skb_cc > skb_id), but will not be catched by (skb_cc - skb_id) < 0.

Oh, I may be confused about what the producer and consumer are.
The point I was trying to make is that comparing indexes on rings is
hard. Instead of writing:

	if (a < b)

you need to write:

	if ((signed)(a - b) < 0)

"mathematically" it's the same, but in "wrapping logic" it works
because if you're further than half a ring around then it counts 
as a second negation..