2018-05-11 07:27:31

by Xiao Guangrong

[permalink] [raw]
Subject: Is read barrier missed in kfifo?


Hi,

Currently, there is no read barrier between reading the index
(kfifo.in) and fetching the real data from the fifo.

I am afraid that will cause the vfifo is observed as not empty
however the data is not actually ready for read. Right?

Thanks!


2018-05-11 08:07:44

by Stefani Seibold

[permalink] [raw]
Subject: Re: Is read barrier missed in kfifo?

My guts thinks you are right. Feel free to send a patch...

Am Freitag, den 11.05.2018, 15:25 +0800 schrieb Xiao Guangrong:
> Hi,
>
> Currently, there is no read barrier between reading the index
> (kfifo.in) and fetching the real data from the fifo.
>
> I am afraid that will cause the vfifo is observed as not empty
> however the data is not actually ready for read. Right?
>
> Thanks!

2018-05-11 08:34:42

by Peter Zijlstra

[permalink] [raw]
Subject: Re: Is read barrier missed in kfifo?

On Fri, May 11, 2018 at 03:25:18PM +0800, Xiao Guangrong wrote:
>
> Hi,
>
> Currently, there is no read barrier between reading the index
> (kfifo.in) and fetching the real data from the fifo.
>
> I am afraid that will cause the vfifo is observed as not empty
> however the data is not actually ready for read. Right?

That code is decidedly dodgy indeed. I can only see smp_wmb() but no
matching barriers at all -- therefore the code is almost certainly as
good as not having any barriers at all.

I would suggest you try and convert the code to smp_store_release() and
smp_load_acquire() while you're at it.

2018-05-11 16:20:42

by Paul E. McKenney

[permalink] [raw]
Subject: Re: Is read barrier missed in kfifo?

On Fri, May 11, 2018 at 10:32:42AM +0200, Peter Zijlstra wrote:
> On Fri, May 11, 2018 at 03:25:18PM +0800, Xiao Guangrong wrote:
> >
> > Hi,
> >
> > Currently, there is no read barrier between reading the index
> > (kfifo.in) and fetching the real data from the fifo.
> >
> > I am afraid that will cause the vfifo is observed as not empty
> > however the data is not actually ready for read. Right?
>
> That code is decidedly dodgy indeed. I can only see smp_wmb() but no
> matching barriers at all -- therefore the code is almost certainly as
> good as not having any barriers at all.
>
> I would suggest you try and convert the code to smp_store_release() and
> smp_load_acquire() while you're at it.

Isn't this one of the places where we rely on control dependencies?

Thanx, Paul


2018-05-14 07:14:11

by Peter Zijlstra

[permalink] [raw]
Subject: Re: Is read barrier missed in kfifo?

On Fri, May 11, 2018 at 09:20:53AM -0700, Paul E. McKenney wrote:
> On Fri, May 11, 2018 at 10:32:42AM +0200, Peter Zijlstra wrote:
> > On Fri, May 11, 2018 at 03:25:18PM +0800, Xiao Guangrong wrote:
> > >
> > > Hi,
> > >
> > > Currently, there is no read barrier between reading the index
> > > (kfifo.in) and fetching the real data from the fifo.
> > >
> > > I am afraid that will cause the vfifo is observed as not empty
> > > however the data is not actually ready for read. Right?
> >
> > That code is decidedly dodgy indeed. I can only see smp_wmb() but no
> > matching barriers at all -- therefore the code is almost certainly as
> > good as not having any barriers at all.
> >
> > I would suggest you try and convert the code to smp_store_release() and
> > smp_load_acquire() while you're at it.
>
> Isn't this one of the places where we rely on control dependencies?

Then it bloody well should have a comment. But at least one side of the
fifo needs a read barrier I think. We can rely on a ctrl-dep on the
write side, where we read the head/tail values, compute space and then
conditionally allow writes to happen.

But on the read side it's all reads and ctrl-dep doesn't help anything.

2018-05-14 13:24:46

by Paul E. McKenney

[permalink] [raw]
Subject: Re: Is read barrier missed in kfifo?

On Mon, May 14, 2018 at 08:57:55AM +0200, Peter Zijlstra wrote:
> On Fri, May 11, 2018 at 09:20:53AM -0700, Paul E. McKenney wrote:
> > On Fri, May 11, 2018 at 10:32:42AM +0200, Peter Zijlstra wrote:
> > > On Fri, May 11, 2018 at 03:25:18PM +0800, Xiao Guangrong wrote:
> > > >
> > > > Hi,
> > > >
> > > > Currently, there is no read barrier between reading the index
> > > > (kfifo.in) and fetching the real data from the fifo.
> > > >
> > > > I am afraid that will cause the vfifo is observed as not empty
> > > > however the data is not actually ready for read. Right?
> > >
> > > That code is decidedly dodgy indeed. I can only see smp_wmb() but no
> > > matching barriers at all -- therefore the code is almost certainly as
> > > good as not having any barriers at all.
> > >
> > > I would suggest you try and convert the code to smp_store_release() and
> > > smp_load_acquire() while you're at it.
> >
> > Isn't this one of the places where we rely on control dependencies?
>
> Then it bloody well should have a comment. But at least one side of the
> fifo needs a read barrier I think. We can rely on a ctrl-dep on the
> write side, where we read the head/tail values, compute space and then
> conditionally allow writes to happen.
>
> But on the read side it's all reads and ctrl-dep doesn't help anything.

Agreed, for a control depdendency to help, there does need to be a
control dependency. ;-)

Thanx, Paul