Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2231609ybi; Thu, 20 Jun 2019 11:16:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqzzbYDHHT7/ZyWcxB1fN6vVlwJ8fpcwA/B2c+MPXb2XgqWALBWB73n/WrDsIe9M5nmGS7SR X-Received: by 2002:a17:902:9a49:: with SMTP id x9mr106419846plv.282.1561054597252; Thu, 20 Jun 2019 11:16:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561054597; cv=none; d=google.com; s=arc-20160816; b=kCcAbeoDBkDLKV5IaxzFAXkx+KK/4I8r/0wUOrb5VJn18FHtDBtZPTWkzFMXgayY0R OW7RF58n6h6d3ayfFIvt9Bq2CC1otSIhS/o5NXwSyWdsvLZ712C3wiydq61FeKCT3RUv 6pGnLS9B/3OrnEleC6j9pK2fQBHSluxlI8LplIXzbirw20ZgEH22fLJeYhCTU8+oWGZG 3R6djsgQnZfrrOUL9PMEKWb2n3iK3Ok26ZIpkooC03oQ6ys5ia+DUAXKSfdyfc8yg/jD TSYxnrP8ADHjyzbrBWoty96weN/c5b0fQZdIwoh7fR6ETifvxOETsWABcio9xKKh8yxm jf2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=vkmxeBK/v6K2Y80vCFXoKlFPHHzc+w++4aD+XIYdpI8=; b=E20vLNPfXiMJRHl9Jl6l9ohiHa2FUBYZKk7OspXHh7WSSavfXMKBK4tH7cYD6gGwt8 r+xpmfOBZYu651Eu3sadF6EydrHgrnn9l+ZsAwEzIxD9VObuueGga8bcvzkdbQ1uPdM9 lCzoNz/CWekTOOB37DYDXEao42M4CjID8FlOFUyPCQ3225awr7UZsZCeDW9QFPMvth1e sP2RaiSeI7qqscYxKABNf4DFp9Z5ZLztDk+vD8zNhYWG0d721FteAZp3JDW+7OLvZ99t emy2hdyiuzr8VKsQiFt8IUFtkvcYxpRwj08tJFuqCDyqEIZ1H+M6ieG2nNJQJ/43vrSK mNZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Qb2HsCOX; 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 d126si188878pgc.46.2019.06.20.11.16.22; Thu, 20 Jun 2019 11:16:37 -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=pass header.i=@kernel.org header.s=default header.b=Qb2HsCOX; 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 S1729462AbfFTSQS (ORCPT + 99 others); Thu, 20 Jun 2019 14:16:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:45340 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729744AbfFTSQP (ORCPT ); Thu, 20 Jun 2019 14:16:15 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 81CFE205F4; Thu, 20 Jun 2019 18:16:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1561054574; bh=v/vNDN6E0szz0Hmj3pzT3NLPezLmBzFhdmb1zb2l6hI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qb2HsCOXzDbhgydQAyO0LUC785GNsaVZzAAIDmT8zRj574YChAol9buhBbk7qhXm9 l/OTdQywjk41/r6MNsSkMKDYD7IUSdE7APpKPOdwDiiV8KjTrGyM/iA9xDFp8+wwSZ o6xFAm/Sy9pxg/PKhqP++nkc7P5wsoiFvVr5jHSE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yabin Cui , "Peter Zijlstra (Intel)" , Alexander Shishkin , Arnaldo Carvalho de Melo , Arnaldo Carvalho de Melo , Jiri Olsa , Linus Torvalds , Namhyung Kim , Stephane Eranian , Thomas Gleixner , Vince Weaver , mark.rutland@arm.com, Ingo Molnar , Sasha Levin Subject: [PATCH 5.1 48/98] perf/ring_buffer: Fix exposing a temporarily decreased data_head Date: Thu, 20 Jun 2019 19:57:15 +0200 Message-Id: <20190620174351.394999456@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190620174349.443386789@linuxfoundation.org> References: <20190620174349.443386789@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 1b038c6e05ff70a1e66e3e571c2e6106bdb75f53 ] 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. [ Split up by peterz. ] Signed-off-by: Yabin Cui Signed-off-by: Peter Zijlstra (Intel) Cc: Alexander Shishkin Cc: Arnaldo Carvalho de Melo Cc: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Linus Torvalds Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Thomas Gleixner Cc: Vince Weaver Cc: mark.rutland@arm.com Fixes: ef60777c9abd ("perf: Optimize the perf_output() path by removing IRQ-disables") Link: http://lkml.kernel.org/r/20190517115418.224478157@infradead.org Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- kernel/events/ring_buffer.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c index 674b35383491..009467a60578 100644 --- a/kernel/events/ring_buffer.c +++ b/kernel/events/ring_buffer.c @@ -51,11 +51,18 @@ static void perf_output_put_handle(struct perf_output_handle *handle) 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(struct perf_output_handle *handle) 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; -- 2.20.1