Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp2464151imd; Fri, 2 Nov 2018 11:52:06 -0700 (PDT) X-Google-Smtp-Source: AJdET5f0szuamlhovUIyAz7vhR1iXyQGdVBKxcXnBiZY7N3ThRfpRRiqBjJV2f++hPoV27BlbYJ+ X-Received: by 2002:a63:b30f:: with SMTP id i15mr4722891pgf.240.1541184726903; Fri, 02 Nov 2018 11:52:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541184726; cv=none; d=google.com; s=arc-20160816; b=zevm2bDFoSWfLVA91gzlJWJynPm6L0Q8jXRXh2z449Xac3d0C6ccmPyK7oGR/3wA7r iIRmlGY+TtDf3FCKpHsIf2H5MbP9YDsAoBsxW8rOR2NG3DXgYZnyDQTYZh//zPv5Ypyd exHo3XM2Y6cVLm03xNunpEZPfv7L/KFqTjbcs3rsuFPSfbU2aiwfMMqzfnLP6c+xZL3R uGTqwSoO1UgCfsKfLV0mmcQFiY9CccAbEG7pWgb5pGdKP34idVR1QGDkaig2n3tDvOSl Fq+4xO3545vwwXEWv6utZQJwwhAzE91kbSPjwrMr6PhkQUe+sCXzf2x5xVvFao/IE7AU cZ+A== 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=nJgMlQZJagEceWMBHmPmqd2Qb/hIpnpZi/HM1iQ/DH4=; b=TbeptFFIHnuMFbpWS0M+Ezj6RwL2eyZqxIs6z5tIzw0XM7tHTZ87dqAfVeWCP8kmK4 TxKQsIe99o8T6E3PPyRNptpn9BHB0VAUosAdVL3etBRKd7E+R7btl/TJal+C3uTho99R hrS7SP0KZ0xJ2I6AZ/rju6MF86+OTybSy07lJ/wTnzgb6en5c1SS4kvGs5CVdJ1eOLYB tGZ5zGA8CF/PbkTz9CTEdEBUXjFjeZ0aAkAw6hKQHTw1k1nQlb+rJvKiOz3G7HDyL8BW a5fMYWRd/+ocLjd+uDaHBNuLXUxWUsQMvWJgi2A0FRr4MeuR+Wc2hOfUR+2nzxWpKvk7 sHEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nszIEoIz; 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 14-v6si34335613pgx.181.2018.11.02.11.51.52; Fri, 02 Nov 2018 11:52:06 -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=nszIEoIz; 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 S1730786AbeKCD7s (ORCPT + 99 others); Fri, 2 Nov 2018 23:59:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:55516 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728743AbeKCD7r (ORCPT ); Fri, 2 Nov 2018 23:59:47 -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 65F242082D; Fri, 2 Nov 2018 18:51:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541184692; bh=D5XKwHj6e5O0DE8cSxrXdROkeN0vmWc/NlcPajMUaJk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nszIEoIzWDW2edTh1y+/QYasOswj9fB5omrNVdfyXhd1SyU36c6xpnDk1QYvnnwrv GxueYe+EqdgW07q7lNcuBrc77YNSHwhcfGx2v3BoDAn7rw2nCwsdaxoi9WLJJCeYNI HUS961GDULykTMNDOBkk7uJauNBFhys9R7SAqduQ= 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.14 039/143] perf/ring_buffer: Prevent concurent ring buffer access Date: Fri, 2 Nov 2018 19:33:44 +0100 Message-Id: <20181102182900.191451763@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181102182857.064326086@linuxfoundation.org> References: <20181102182857.064326086@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.14-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 ee1c07c0b833..991af683ef9e 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8058,6 +8058,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