Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1874045imu; Thu, 10 Jan 2019 04:37:02 -0800 (PST) X-Google-Smtp-Source: ALg8bN45BNBEf+xD52DpS3Rzc/rDHeOn8iGOk1pEnV1C0LeWlwFMnLg6ah/WRO2KpveOT8FathFs X-Received: by 2002:a63:8c6:: with SMTP id 189mr9260103pgi.322.1547123822582; Thu, 10 Jan 2019 04:37:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547123822; cv=none; d=google.com; s=arc-20160816; b=onK748Xp1hYz6AdXGYBrN8G32RWZ7ub6ptujqiu0dlaGLN0mXHcwEcviI6SdRiiV3x bxbC2vSsJTngdGl5ZOTfpc4xUkACPP7HrirtI4IgpP9I6tVEAQc1BA+HK5f2EYd5yssc 9bGZ5ZM0QjoZN38C2ueJj/cHL3PsSNwkrcVJN5zjyVjU1eqdspuImjO9rsV4B6TMnwmP o3xjNjZteQpapGe4CULb4Qow8tlXBCTlFVBm5NdDTceLIX0WudGyBSKSQazW56wsaUoL J7WNBO+Vqvd13jSteEch7lIU8wdv4wOg3YBXDB8KQaRKg3sF/Ny1jx+By3QyHiLP7RvN ZITA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=mbzGjxz9VjfqDI0HiRdDjm6CIpA1+54CDDYZamxOrAY=; b=YMsXqV/P67xo5btn3lLqKCqkC4WpLuhB1S10nbk4PyzO4PNvBWHam47GIaBxHROsa3 gI+zYhDR1bYDhf+zYIrmfZaQoNb/hhCG+fLjtoQG4gKvrj/Ln+s0dtABR77FJ969WvXW y+LpnqcGxZbjDTSPJhVqZD6RNUV3XEoQZp53aA0C1lVIKp7RVOyK6G2Oq6k6FcncpeLR Uu9UjzPMnwbdMBsTWlev1XohSZe53VD9mlnNySEwtvaqM1WGphh7zK1LyohjSZZ+J07g /xvC2q7j9Hq9rY4u6SSRhqSqAw8t5xRsOpF/2LEKij7Rv0cuCyLhYOWK2N+Qrlsavxh7 YRaw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y4si11081591pfk.172.2019.01.10.04.36.46; Thu, 10 Jan 2019 04:37:02 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728377AbfAJMec (ORCPT + 99 others); Thu, 10 Jan 2019 07:34:32 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:33592 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727534AbfAJMec (ORCPT ); Thu, 10 Jan 2019 07:34:32 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C85A480D; Thu, 10 Jan 2019 04:34:31 -0800 (PST) Received: from fuggles.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 06A3D3F5AF; Thu, 10 Jan 2019 04:34:29 -0800 (PST) Date: Thu, 10 Jan 2019 12:34:27 +0000 From: Will Deacon To: "xiaoguangrong(Xiao Guangrong)" Cc: Kees Cook , "yulei.kernel@gmail.com" , Stefani Seibold , Peter Zijlstra , "Paul E. McKenney" , "mkelly@xevo.com" , Jiri Kosina , LKML , =?utf-8?B?eXVsZWl4emhhbmco5byg6KqJ56OKKQ==?= Subject: Re: [PATCH] kfifo: add memory barrier in kfifo to prevent data loss Message-ID: <20190110123427.GD27065@fuggles.cambridge.arm.com> References: <20181211034032.32338-1-yuleixzhang@tencent.com> <019819C6E743904A88B43F7DDEED99BBA7E74E37@EXMBX-SZMAIL006.tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <019819C6E743904A88B43F7DDEED99BBA7E74E37@EXMBX-SZMAIL006.tencent.com> User-Agent: Mutt/1.11.1+86 (6f28e57d73f2) () Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 wrote: > >> > >> From: Yulei Zhang > >> > >> 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 > >> Signed-off-by: Guangrong Xiao > > > > 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