Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2277274imu; Fri, 14 Dec 2018 08:28:41 -0800 (PST) X-Google-Smtp-Source: AFSGD/UbO3zVlSNdfggwlkITyJi040zj43rf02ujlfOZKVo8kDOsSKvn1FECryt5/qNC+o6ht9YL X-Received: by 2002:a62:3948:: with SMTP id g69mr3513495pfa.114.1544804921459; Fri, 14 Dec 2018 08:28:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544804921; cv=none; d=google.com; s=arc-20160816; b=uTSVA+pD5YdRWhz11yUqEcI7gN0KLLuC0xPUX1TUfJt0qSR8/H6p0/hB9rlnWtd98K CTORN6E4bpOkEPRc9CUd2+cNx4jpQYspusxjqV/Ofxn0crqArJCC3Nrmof97G0BubHdH kLI6qhiJVQCb731xfsBAjWal0cH4pfxAT18ZOXPXHuekNxyCzS0S/9oZomGG1fbVPBBm 2dqwWcydQNIzVy+dzYV07URzg0m38P5gjCwSG8W2XFvz/ySFgKDtIJv6anzYlsEVa8kt Pj6PWFqm6Nc8qCneVy2GhbZOLbnYXJU2GFCeL9+n7nvy2cewqX7m3vN7QENW43+d9AwD KNZQ== 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=gXWWyehvXxqDnU1MFcJfogMiWVwQhHons28RgXgkmOw=; b=QpFNS43Uoy+0v8hLn8OLpw0V3O+ZdEeZE1WnrNi4fSq28tAIdf4MjXOfmYw5nWSEgY 97I8LQRP+9Oa1Qts2aZZSX/Hxxq11wz+KcXS0OgSAYO5d8W89yVMzrpoM4jVJqcFvwMN UglgHpN/9l+Rz7vOgKv14VPXOdKO2odpHZ015/wBymD49SZ2Mwg8d+aHS5t18FfJ14Cp v1B34o6AC/35xebGs97zLEMhN3F/QF0N/hG7d2+T4+Xq9O04IqqG1/0HZeCoTlEv1NfJ 3IPy1FDjCjBf1cqc+BQ75KnxJau4ajOYl/DdkhJjtIAhgcWcIZxipyW7cdTsh17zH/DH PmRQ== 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 m75si4317408pga.432.2018.12.14.08.28.26; Fri, 14 Dec 2018 08:28:41 -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 S1729963AbeLNQZy (ORCPT + 99 others); Fri, 14 Dec 2018 11:25:54 -0500 Received: from foss.arm.com ([217.140.101.70]:54890 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727795AbeLNQZy (ORCPT ); Fri, 14 Dec 2018 11:25:54 -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 B999480D; Fri, 14 Dec 2018 08:25:53 -0800 (PST) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 898603F59C; Fri, 14 Dec 2018 08:25:53 -0800 (PST) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id E3D4C1AE087D; Fri, 14 Dec 2018 16:25:52 +0000 (GMT) Date: Fri, 14 Dec 2018 16:25:52 +0000 From: Will Deacon To: Kees Cook Cc: yulei.kernel@gmail.com, Stefani Seibold , Peter Zijlstra , "Paul E. McKenney" , mkelly@xevo.com, Jiri Kosina , LKML , yuleixzhang@tencent.com, xiaoguangrong@tencent.com Subject: Re: [PATCH] kfifo: add memory barrier in kfifo to prevent data loss Message-ID: <20181214162552.GD8148@edgewater-inn.cambridge.arm.com> References: <20181211034032.32338-1-yuleixzhang@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.11.1+30 (d10eec459b35) () Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 11, 2018 at 04:50:34PM -0800, 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! I haven't looked at the guts of kfifo before and I'm fully prepared to believe that there are ordering problems in there. However, I'm having a hard time matching the implementation to the snippets above. Please could you provide the description of the consumer/producer interaction as above, but annotated with the function/macro names? There are things like kfifo_get() using smp_wmb(), which looks suspicious, but doesn't appear to be what you're reporting here. Thanks, Will