Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp51647imu; Thu, 8 Nov 2018 14:36:45 -0800 (PST) X-Google-Smtp-Source: AJdET5cd1s0THwOuLmsXdyJhRMzszZOBGnBsa3dTJYGOFvSlJbWctA8LKfA58z3rRxbLr3q//bfw X-Received: by 2002:a63:50b:: with SMTP id 11mr5186793pgf.411.1541716605177; Thu, 08 Nov 2018 14:36:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541716605; cv=none; d=google.com; s=arc-20160816; b=s3n1nqR7QCvfNEfpReqsVe625LxBuOUZd9tXwrTHSSE+Ox1rJXM6JYW6MJ/OjVnBFs n2ounha4dK0k2y2C62OVTUeW74p5VX8C6gvc5RPMJKz+Y3GwQKUfoLoWWciT2WX478rO NrrcpIywYH3yDcz0IxWtfyF//kRKRwD5kT85dLxkF+MlxwnLGEZj0Dmh203q3mwy+7WB Rf6jx64+xoyRiFwnrLW4W83GUmAIH9W1EKoerXAnu11xXbcaqP8cj1JiJc//hePcycWe cHm6KuoZmwUi0O41QTXNx1NGJ0VAB6RKfTKtD4LS05jPfRifMH5ogwTQoDCj8OgnpcfZ ISVw== 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=7HG7RXCzZlak8BtTUGvqtzVYQR/WoUIVOUs5lvxp9+E=; b=U1/3dftG80mStRCPUn2xefac+Gx/o7Ab/P/KmXK6swOP7L2ro2rYx0ljOL71JN+u/M djQXwtS5Gpw/2XRuHQoXTYbFHwyjkGkny5tS6IP3Rlc8lwrdypPJeT0diU326iWiFsUn YE3HwcgwHIMg4eWrsd19bgy+nw4iTDwxfcLjddwskncfBGvv9jszG9qLYHsJZNpejIni uzT9F7LV4Xan4ahNCVmgOLdDPiRXvgkJN3iPEDYP1vs+RVwNrIn9pB870SvXpudwnp9Z raKldgjumsS5d44N1OQb8G76Py89JPI3jrxHjCyTL1ufnFsAzftcUpR9vJHLh7FAp7Lx 2P7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="B/LZJhZ3"; 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 w5-v6si4528362pgj.506.2018.11.08.14.36.29; Thu, 08 Nov 2018 14:36:45 -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; dkim=pass header.i=@kernel.org header.s=default header.b="B/LZJhZ3"; 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 S1730083AbeKIHhH (ORCPT + 99 others); Fri, 9 Nov 2018 02:37:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:54802 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728345AbeKIHhG (ORCPT ); Fri, 9 Nov 2018 02:37:06 -0500 Received: from localhost (unknown [208.72.13.198]) (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 EB331208E3; Thu, 8 Nov 2018 21:59:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541714377; bh=tpSTC+mQJWlBc2UokP0wU+WBZyHQNE8O5AzazlrlgVs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B/LZJhZ3s+AF8MkMpAeHDZMhN+sLUB9kFrppF0exEf979ZxtqIFcU97gdf9qCrt3a N0UHCLRylqCDErCHClqm0YH4qBQzl13XoujEjnQyd7GD/EJqx5W311FtcXbY5Di9s7 6hRCq6cavcfauvA8Ocuwsw9K0FRh4UlNKi/BQ4Dw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jiri Olsa , "Peter Zijlstra (Intel)" , Alexander Shishkin , Andrew Vagin , Arnaldo Carvalho de Melo , Arnaldo Carvalho de Melo , Jiri Olsa , Linus Torvalds , Namhyung Kim , Stephane Eranian , Thomas Gleixner , Vince Weaver , Ingo Molnar , Sasha Levin Subject: [PATCH 4.4 017/114] perf/ring_buffer: Prevent concurent ring buffer access Date: Thu, 8 Nov 2018 13:50:32 -0800 Message-Id: <20181108215100.717156961@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181108215059.051093652@linuxfoundation.org> References: <20181108215059.051093652@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit cd6fb677ce7e460c25bdd66f689734102ec7d642 ] Some of the scheduling tracepoints allow the perf_tp_event code to write to ring buffer under different cpu than the code is running on. This results in corrupted ring buffer data demonstrated in following perf commands: # perf record -e 'sched:sched_switch,sched:sched_wakeup' perf bench sched messaging # Running 'sched/messaging' benchmark: # 20 sender and receiver processes per group # 10 groups == 400 processes run Total time: 0.383 [sec] [ perf record: Woken up 8 times to write data ] 0x42b890 [0]: failed to process type: -1765585640 [ perf record: Captured and wrote 4.825 MB perf.data (29669 samples) ] # perf report --stdio 0x42b890 [0]: failed to process type: -1765585640 The reason for the corruption are some of the scheduling tracepoints, that have __perf_task dfined and thus allow to store data to another cpu ring buffer: sched_waking sched_wakeup sched_wakeup_new sched_stat_wait sched_stat_sleep sched_stat_iowait sched_stat_blocked The perf_tp_event function first store samples for current cpu related events defined for tracepoint: hlist_for_each_entry_rcu(event, head, hlist_entry) perf_swevent_event(event, count, &data, regs); And then iterates events of the 'task' and store the sample for any task's event that passes tracepoint checks: ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { if (event->attr.type != PERF_TYPE_TRACEPOINT) continue; if (event->attr.config != entry->type) continue; perf_swevent_event(event, count, &data, regs); } Above code can race with same code running on another cpu, ending up with 2 cpus trying to store under the same ring buffer, which is specifically not allowed. This patch prevents the problem, by allowing only events with the same current cpu to receive the event. NOTE: this requires the use of (per-task-)per-cpu buffers for this feature to work; perf-record does this. Signed-off-by: Jiri Olsa [peterz: small edits to Changelog] Signed-off-by: Peter Zijlstra (Intel) Cc: Alexander Shishkin Cc: Andrew Vagin 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 Fixes: e6dab5ffab59 ("perf/trace: Add ability to set a target task for events") Link: http://lkml.kernel.org/r/20180923161343.GB15054@krava Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- kernel/events/core.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/events/core.c b/kernel/events/core.c index 990ac41d8a5f..330fcd1b1822 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7018,6 +7018,8 @@ void perf_tp_event(u64 addr, u64 count, void *record, int entry_size, goto unlock; list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { + if (event->cpu != smp_processor_id()) + continue; if (event->attr.type != PERF_TYPE_TRACEPOINT) continue; if (event->attr.config != entry->type) -- 2.17.1