From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16104C04AB4 for ; Fri, 17 May 2019 11:55:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DB7F720881 for ; Fri, 17 May 2019 11:55:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="ExPm7WvO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728816AbfEQLzM (ORCPT ); Fri, 17 May 2019 07:55:12 -0400 Received: from merlin.infradead.org ([205.233.59.134]:36002 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728280AbfEQLzL (ORCPT ); Fri, 17 May 2019 07:55:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=SH/twYtCaFpv3pgtGBNnjReP8rp7lFQnud0KmfEGM9c=; b=ExPm7WvOkoWez0UsRFhhPLpRIX YogLkO1srqPT2uAt3Dl/84P0MnVxVUZwE1bTO8Tw4/9xModHVgHBnoZHwaKKdMcpBYFjAlpe1L7pb nAreKuNFje/sHv2sqxxZoWHbNXi8ICslR8EZvaIeTrkoPeNpKT5k8Q6QLoQ7z3F5npSyDoY/tWTkR JLf4PCzV8+Id8gh4nqUaETD7MKIctWDaE+RlZXbhKBX5sjWnLx89al9d2f/2mSEXy7PpnJenf4NgF 2wUzkl1uUU47Ca8tqWS+3Sr/hwj7W10hG/C3oaiY20cSlkuZ3Nvp1/K1Aepj10rq35uQyRh2Qnolg LUatcuUg==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hRbRz-00016w-8X; Fri, 17 May 2019 11:54:59 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id B337F2029F888; Fri, 17 May 2019 13:54:56 +0200 (CEST) Message-Id: <20190517115418.224478157@infradead.org> User-Agent: quilt/0.65 Date: Fri, 17 May 2019 13:52:31 +0200 From: Peter Zijlstra To: peterz@infradead.org, mingo@kernel.org Cc: yabinc@google.com, acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org, mark.rutland@arm.com, linux-kernel@vger.kernel.org, Ingo Molnar Subject: [PATCH 1/4] perf/ring_buffer: Fix exposing a temporarily decreased data_head. References: <20190517115230.437269790@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. This can be fixed by moving dec(&rb->nest) to after updating data_head, which prevents the IRQ/NMI above from updating data_head. Cc: Arnaldo Carvalho de Melo Cc: Ingo Molnar Cc: Jiri Olsa Cc: Namhyung Kim Cc: Alexander Shishkin Fixes: ef60777c9abd ("perf: Optimize the perf_output() path by removing IRQ-disables") Signed-off-by: Yabin Cui Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20190516184010.167903-1-yabinc@google.com --- kernel/events/ring_buffer.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) --- a/kernel/events/ring_buffer.c +++ b/kernel/events/ring_buffer.c @@ -51,11 +51,18 @@ static void perf_output_put_handle(struc head = local_read(&rb->head); /* - * IRQ/NMI can happen here, which means we can miss a head update. + * IRQ/NMI can happen here and advance @rb->head, causing our + * load above to be stale. */ - if (!local_dec_and_test(&rb->nest)) + /* + * If this isn't the outermost nesting, we don't have to update + * @rb->user_page->data_head. + */ + if (local_read(&rb->nest) > 1) { + local_dec(&rb->nest); goto out; + } /* * Since the mmap() consumer (userspace) can run on a different CPU: @@ -87,9 +94,18 @@ static void perf_output_put_handle(struc rb->user_page->data_head = head; /* - * Now check if we missed an update -- rely on previous implied - * compiler barriers to force a re-read. + * We must publish the head before decrementing the nest count, + * otherwise an IRQ/NMI can publish a more recent head value and our + * write will (temporarily) publish a stale value. + */ + barrier(); + local_set(&rb->nest, 0); + + /* + * Ensure we decrement @rb->nest before we validate the @rb->head. + * Otherwise we cannot be sure we caught the 'last' nested update. */ + barrier(); if (unlikely(head != local_read(&rb->head))) { local_inc(&rb->nest); goto again;