Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2988240imu; Sun, 9 Dec 2018 14:23:56 -0800 (PST) X-Google-Smtp-Source: AFSGD/VJYPDQ1R9iKZtaBjNh02t9VgjV2/ARucn8fL+Yr56BZ0UZ4bG03L6W3Hrqsn/MgwmZB/ZX X-Received: by 2002:a65:484c:: with SMTP id i12mr8780128pgs.309.1544394236310; Sun, 09 Dec 2018 14:23:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544394236; cv=none; d=google.com; s=arc-20160816; b=qchBl1k+X3b66BKVhU0xF43Z6k7MrwNeYe/IroQEcm+8X1LfYHYDFIQfWKPViCCrdr YwUGQaYAVhpq5KiQWxZjrYjNGW7VFmIiM02GpzjfgLEbAJaT7IDnhgOjStB4lhEEuiNR 9N2uK0ynm2Y2VTOJNjm8CLhf2LurSHrkWvqnKuGVXF00n2YfqcGcUr08B7NTL3Bj6hBM VG5ftbGUe4X+KI8ZXR+7HndCJuD9y+uuRsqf4ebe3pnvwwDSBhNhMARzoU2vRjxTtASv Z59JgOab9qcoEA+TbuLlpenWlJg1tIm/zSa5Qq+Ms5XKMegmwFacgcIJg8ks08pMtr2R T/Gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=DRlGYC3hBbJaGtfgoL7c3fdzhkzcFgwGod4+7vUpx6c=; b=kcnqapL6GK3i7gQ3mbvwxeY8nViG3tSJmBtv1Z7QUpoIdVYPtclcmbDjceQ9SGMnKx frDSTYSNWhDVhgQe8q4nA+2cpha2at7iZlZZ8OmydxoRLOmJHiLPDNz9x2qGtOuwFe0x v9/z8F1isKPy5z1uOziVziYF1TCJaW/kSWpkunRo5aSrrykF45a0Ai3z5qXYmTleM0l2 EO62nD533GwuD2mHlUnExQPuYKDCmXGq1U6onKf2Uj/zmFMX7K38yt9LGVfH3vJvgz2/ 2iCs+1ubfgJZM6S/7bonZ3Qq4VQtD0T+E04mLVWuHMZ6VnclDmgHhSg81wctEXBcn1qG TIFQ== ARC-Authentication-Results: i=1; mx.google.com; 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 8si8545093plc.88.2018.12.09.14.23.41; Sun, 09 Dec 2018 14:23:56 -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; 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 S1727895AbeLIWVA (ORCPT + 99 others); Sun, 9 Dec 2018 17:21:00 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:35352 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726541AbeLIVzY (ORCPT ); Sun, 9 Dec 2018 16:55:24 -0500 Received: from pub.yeoldevic.com ([81.174.156.145] helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gW72n-0002pr-8f; Sun, 09 Dec 2018 21:55:21 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gW72k-0003dt-Ap; Sun, 09 Dec 2018 21:55:18 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Vince Weaver" , "Linus Torvalds" , "Stephane Eranian" , "Arnaldo Carvalho de Melo" , "Jiri Olsa" , "Thomas Gleixner" , "Andrew Vagin" , "Namhyung Kim" , "Jiri Olsa" , "Peter Zijlstra (Intel)" , "Alexander Shishkin" , "Arnaldo Carvalho de Melo" , "Ingo Molnar" Date: Sun, 09 Dec 2018 21:50:33 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 289/328] perf/ring_buffer: Prevent concurent ring buffer access In-Reply-To: X-SA-Exim-Connect-IP: 81.174.156.145 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.62-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Jiri Olsa commit cd6fb677ce7e460c25bdd66f689734102ec7d642 upstream. 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: Ben Hutchings --- kernel/events/core.c | 2 ++ 1 file changed, 2 insertions(+) --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6276,6 +6276,8 @@ void perf_tp_event(u64 addr, u64 count, 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)