[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20190110123427.GD27065@fuggles.cambridge.arm.com>
Date: Thu, 10 Jan 2019 12:34:27 +0000
From: Will Deacon <will.deacon@....com>
To: "xiaoguangrong(Xiao Guangrong)" <xiaoguangrong@...cent.com>
Cc: Kees Cook <keescook@...omium.org>,
"yulei.kernel@...il.com" <yulei.kernel@...il.com>,
Stefani Seibold <stefani@...bold.net>,
Peter Zijlstra <peterz@...radead.org>,
"Paul E. McKenney" <paulmck@...ux.vnet.ibm.com>,
"mkelly@...o.com" <mkelly@...o.com>, Jiri Kosina <jkosina@...e.cz>,
LKML <linux-kernel@...r.kernel.org>,
yuleixzhang(张誉磊)
<yuleixzhang@...cent.com>
Subject: Re: [PATCH] kfifo: add memory barrier in kfifo to prevent data loss
On Thu, Jan 03, 2019 at 07:43:10AM +0000, xiaoguangrong(Xiao Guangrong) wrote:
> On 12/12/18 8:50 AM, Kees Cook wrote:
> > On Mon, Dec 10, 2018 at 7:41 PM <yulei.kernel@...il.com> wrote:
> >>
> >> From: Yulei Zhang <yuleixzhang@...cent.com>
> >>
> >> Early this year we spot there may be two issues in kernel
> >> kfifo.
> >>
> >> One is reported by Xiao Guangrong to linux kernel.
> >> https://lkml.org/lkml/2018/5/11/58
> >> In current kfifo implementation there are missing memory
> >> barrier in the read side, so that without proper barrier
> >> between reading the kfifo->in and fetching the data there
> >> is potential ordering issue.
> >>
> >> Beside that, there is another potential issue in kfifo,
> >> please consider the following case:
> >> at the beginning
> >> ring->size = 4
> >> ring->out = 0
> >> ring->in = 4
> >>
> >> Consumer Producer
> >> --------------- --------------
> >> index = ring->out; /* index == 0 */
> >> ring->out++; /* ring->out == 1 */
> >> < Re-Order >
> >> out = ring->out;
> >> if (ring->in - out >= ring->mask)
> >> return -EFULL;
> >> /* see the ring is not full */
> >> index = ring->in & ring->mask;
> >> /* index == 0 */
> >> ring->data[index] = new_data;
> >> ring->in++;
> >>
> >> data = ring->data[index];
> >> /* you will find the old data is overwritten by the new_data */
> >>
> >> In order to avoid the issue:
> >> 1) for the consumer, we should read the ring->data[] out before
> >> updating ring->out
> >> 2) for the producer, we should read ring->out before updating
> >> ring->data[]
> >>
> >> So in this patch we introduce the following four functions which
> >> are wrapped with proper memory barrier and keep in pairs to make
> >> sure the in and out index are fetched and updated in order to avoid
> >> data loss.
> >>
> >> kfifo_read_index_in()
> >> kfifo_write_index_in()
> >> kfifo_read_index_out()
> >> kfifo_write_index_out()
> >>
> >> Signed-off-by: Yulei Zhang <yuleixzhang@...cent.com>
> >> Signed-off-by: Guangrong Xiao <xiaoguangrong@...cent.com>
> >
> > I've added some more people to CC that might want to see this. Thanks
> > for sending this!
>
> Hi,
>
> Ping... could anyone have a look? ;)
I've started looking at kfifo, but I suspect it needs a fair amount more
work than your patch. Please stay tuned.
Will
Powered by blists - more mailing lists