Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp2457134imd; Fri, 2 Nov 2018 11:43:56 -0700 (PDT) X-Google-Smtp-Source: AJdET5dtjuMBom8VZYsQ1sWh4sBI/demYe30hO6qEJeLVSLeBAtJ8fquLbUDQq6KfB+hSstGStZ2 X-Received: by 2002:a63:6704:: with SMTP id b4mr6059744pgc.100.1541184236464; Fri, 02 Nov 2018 11:43:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541184236; cv=none; d=google.com; s=arc-20160816; b=qX5sFCS+8CwmFVoo11xMb/nY4ZMxh2f44NToqnP+AsMx/037RxG/raANp/Pm2baDmd CG4sqj/ltWeB7r5whHcU1k+TX90xpnL8SvKWbW3ZFV43Ncoz+ogjzunl1A4QJpQdKOnZ 3HutmIE35rKN7rmtHKL2SzMftj/tjasea0GV4AXCv5qR/kJ+JreSwt1JepWXV1x+CwNN IEIGm5nxrF54yxHPNc4FucIF6rhj9GXs+tPevrKnUu9jgePPiImPZJEfadC4rw1wR1HN 3JsP3fG0R2il9kwgbhc2opfjhMKWKhnTiamxQrH25yU0xAI5TB/B07NFak0Wu0UV9U9p tKbA== 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=iuT6didh1XGUwwTI8xYOJDWIkmlIRBZS41CQV8leL+A=; b=v2IAHjKCxzad2NczPWSVDq2qNE+Q3VVfSzLDpvhIwJLXdAB8PvBEm09QdszQRQqU6+ OobYY3xG109jF/QmnnW9ECg7UdSK7QpWvSnNSMamdE114RCUj4Bmuvirht9sEMYFIMxO wGc9Lh+YjiHrPqkU2w3ZCBHocm0gIo+TmkVYYHLfWNkgIEmVFCVWcz7FVD4rukXW1oD0 TB9/WY98nnx3WNnjUmOrK8C6IZaxdBjlmOJs7J0V2TGyfxh4MaXv6X4X7YHUwUv9d7dY QCQ+Td7AGi+mX9BJ/o4PfD6IHK9mcoT7rsBftdniOBocrTy88B9gcL/GkbOVPOs1FctP BWdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Xb/bKStu"; 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 w19-v6si34599951pgl.278.2018.11.02.11.43.42; Fri, 02 Nov 2018 11:43:56 -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="Xb/bKStu"; 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 S1729562AbeKCDtb (ORCPT + 99 others); Fri, 2 Nov 2018 23:49:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:44186 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728699AbeKCDtb (ORCPT ); Fri, 2 Nov 2018 23:49:31 -0400 Received: from localhost (5356596B.cm-6-7b.dynamic.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 7B27320847; Fri, 2 Nov 2018 18:41:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541184079; bh=IwVqwrcnAT2e6IVtLvKjsoX3RTV0Q4XBg5tNDnXlJOA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xb/bKStuSUlJJP632e+Mu4Mk2uTSYNTDsA+4omriEN4ANgzV8NpD/kRTbWkq67Gxo b1+uimGwymw9sMTpVW8RjVJ8DNFUrWb7iFBzwPQE4tdNby3/fuzSNU5/1SeDOMXFZW t6gWM3f+bHEYxIPnathUOIFbQ3BpAkCPdlh9usrM= 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.18 055/150] perf/ring_buffer: Prevent concurent ring buffer access Date: Fri, 2 Nov 2018 19:33:37 +0100 Message-Id: <20181102182907.350944384@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181102182902.250560510@linuxfoundation.org> References: <20181102182902.250560510@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.18-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 b1ed5e99d9c6..fc072b7f839d 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8319,6 +8319,8 @@ void perf_tp_event(u16 event_type, 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