Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp422118yba; Wed, 15 May 2019 03:49:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqyA7p0+Ux7L2J5WnU1Y207BBjcAGJW0uLstVhU0WakbUu7RFb/M7j58/z96s1v85bbwpDr2 X-Received: by 2002:a17:902:a405:: with SMTP id p5mr38383445plq.51.1557917399781; Wed, 15 May 2019 03:49:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557917399; cv=none; d=google.com; s=arc-20160816; b=ZFFVHqf2b3djMOmvEQqXSrpmluPVtVTccqcobLDmplioQ3NoKd69/o7Ry9eVT/OA1A opjlW+oZsgXckMnru1Rsk8CJuNXdqgrI8vHdgzoGbnWb9tJg7BWdM3GxRv/ELKr+WZA7 Z7/oySV8iSaEwQb4OlAHhkQNWUgoMRtLddDmABvMIrH5kuSL60pYzOUSsq61V4DEwk4f yuDqsm5vIaL1JnTtkEmcPx8FLJfgIWe03RSzhX5QSU0J1/tHrp2NeyNPhvsNGs0l8WfP FW8eZ+seBlmU7ER1tQaZ7gYTuknBLDA36FaYKRUUpHLlp9Tk/wfBAW0gT9TzpjTFCIo1 GOLQ== 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-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=eT98c9ZJEKzSBV1OwAhd/D5fyFJk9KO5ces9PubNUPQ=; b=S9u+9o0hr1COkrCUEH1FX9YXrF5vEucKBnnsXIad/xKkmNFyCU3424qCCoi+x4uBaE 8EaMxXBPrCAwYuIvfZLVF+M/Cu59NWiDkzfhv8+GY4JP8oHoDHGZDgJHaAnSloUuhG+K Txu7zJUmX7nh+J/ODwuWVEFKnJhAPruPgbOoxAL53vLfQsQCLInfFXYL5O7k+8pUghtT LmwcqZIi26GX8KDsI80EhzDUcYvnGoDWdzNkOxZWU/IAJM4ZxGMwc8E35LbKQxkQzLvB GXpEilPSkS/Nt2WDMq9JxGVPrsIuujDytBMn1TJHkiEtMb1j+DY8A3Wtyvy+0IB55VQi PiUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=BtNC5WcF; 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 k69si1842376pfb.73.2019.05.15.03.49.45; Wed, 15 May 2019 03:49:59 -0700 (PDT) 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; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=BtNC5WcF; 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 S1726684AbfEOKs3 (ORCPT + 99 others); Wed, 15 May 2019 06:48:29 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:44456 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725953AbfEOKs3 (ORCPT ); Wed, 15 May 2019 06:48:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=eT98c9ZJEKzSBV1OwAhd/D5fyFJk9KO5ces9PubNUPQ=; b=BtNC5WcFTIWlxnIopvRtU44XL UBiXr92kU2jaHbFs/6BoO4H5WendDQQ0uCEAZYamVzI+yLaVnUtaekqULM9LsigCBnIjvUi6UnHUa A3KZPajbJWY0ZGAogherjThCh1qWHjTobQFNx5hOmGFGzlq6GEDPhncFkVvKdOLjTbY9+BW4fSib0 r4gv/Xa6pGQwmqrrGQmmFuzZu8nEoXRB3RGiegOL0CQ/nQW164JPz7W/n5Hn3NDuppOQoPtK1LHxr 3nT5lDlhoKPIVvgnxJfuWc1pMpoTocueIYPAAXoF0XhyRBMqa+wIJ+MzD+iXjIgOHLXwOObd1E3lS 0sc7kTEfA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hQrSV-0003zJ-2E; Wed, 15 May 2019 10:48:27 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id EDC9E20297D4B; Wed, 15 May 2019 12:48:24 +0200 (CEST) Date: Wed, 15 May 2019 12:48:24 +0200 From: Peter Zijlstra To: Yabin Cui Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-kernel@vger.kernel.org Subject: Re: [PATCH] perf/ring_buffer: Fix exposing a temporarily decreased data_head. Message-ID: <20190515104824.GD2623@hirez.programming.kicks-ass.net> References: <20190515003059.23920-1-yabinc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190515003059.23920-1-yabinc@google.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 14, 2019 at 05:30:59PM -0700, Yabin Cui wrote: > In perf_output_put_handle(), an IRQ/NMI can happen in below location and > write records to the same ring buffer: > ... > local_dec_and_test(&rb->nest) > ... <-- an IRQ/NMI can happen here > rb->user_page->data_head = head; > ... > > In this case, a value A is written to data_head in the IRQ, then a value > B is written to data_head after the IRQ. And A > B. As a result, > data_head is temporarily decreased from A to B. And a reader may see > data_head < data_tail if it read the buffer frequently enough, which > creates unexpected behaviors. Indeed, good catch! Have you observed this, or is this patch due to code inspection? > This can be fixed by moving dec(&rb->nest) to after updating data_head, > which prevents the IRQ/NMI above from updating data_head. > > Signed-off-by: Yabin Cui > --- > kernel/events/ring_buffer.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c > index 674b35383491..0b9aefe13b04 100644 > --- a/kernel/events/ring_buffer.c > +++ b/kernel/events/ring_buffer.c > @@ -54,8 +54,10 @@ static void perf_output_put_handle(struct perf_output_handle *handle) > * IRQ/NMI can happen here, which means we can miss a head update. > */ > > - if (!local_dec_and_test(&rb->nest)) > + if (local_read(&rb->nest) > 1) { > + local_dec(&rb->nest); > goto out; > + } > > /* > * Since the mmap() consumer (userspace) can run on a different CPU: > @@ -86,6 +88,13 @@ static void perf_output_put_handle(struct perf_output_handle *handle) > smp_wmb(); /* B, matches C */ > rb->user_page->data_head = head; At the very least this needs: barrier(); > + /* > + * Clear rb->nest after updating data_head. This prevents IRQ/NMI from > + * updating data_head before us. If that happens, we will expose a > + * temporarily decreased data_head. > + */ > + local_set(&rb->nest, 0); Since you rely on the 'decrement' (1->0) to happen after we've written head. And similarly, you need: barrier(); Because we must re-check the head after we've 'decremented'. > /* > * Now check if we missed an update -- rely on previous implied > * compiler barriers to force a re-read. A more paranoid person might've written: WARN_ON_ONCE(local_cmpxchg(&rb->nest, 1, 0) != 1); which would've also implied both barrier()s.