From patchwork Fri Oct 6 11:17:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Levin, Alexander \(Sasha Levin\)" X-Patchwork-Id: 115044 Delivered-To: patch@linaro.org Received: by 10.140.22.163 with SMTP id 32csp1667058qgn; Fri, 6 Oct 2017 04:18:29 -0700 (PDT) X-Google-Smtp-Source: AOwi7QATYPThmTFROz1jkokjcfX2DGLKdY1HNIh+NrOsXBwTNeS4sY7/zaHB6JgzRaKC/nSXzX/N X-Received: by 10.84.132.42 with SMTP id 39mr1703984ple.146.1507288709045; Fri, 06 Oct 2017 04:18:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507288709; cv=none; d=google.com; s=arc-20160816; b=j89UtdXf42Ri6ZBwgUVD2SeZsscTvd/V7If3/IP4UFkHXuGzUXBoEOqgJ/L+vDvfvY Zu0uiWj6nnH4/O9xrGurtHFKz1PeIAZs5ZWyqj4w2Kscu8+RMV6uWfdUjzbkGv6oF0b1 6hiEmrtkOSNrs4Toju1dJ230bFHWy51/8lktZMdJi0lu6uGEjmVw+Dq5o2vnVcBPpkJH qlWwnwpbbQDRUGs1mwKzMHUzM5FpQSP1YjHu0LqP2kjoXX4Cyo8gb4dLpmcdd360YqpA KSF5Ag9OTCSJwly8B8uFp2upVo11Kmm6jrXRVpcRRFjCh73aZ+2bZJ3Gk7lWFORvwGTC lHJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:to:dkim-signature :dkim-signature:cc:from:dkim-signature:arc-authentication-results; bh=9d9gcglXIjS+7Ah4k7RXQYZ+C2rHXi5nJ9Tc0pvtAsw=; b=H20Z3MZvQyiya6v4xhWP6jEFummOjZdAouy4/URSm5j2FG/wUkPEJquXas3TVLyqb2 RrYDUY0PFIXr9XWcPx0imAdM2WWKL1yvw/VxRqLQ1iv+jHDGVx+A57TgiquNvJdOH41r zOZQgki8amtsptEDZFrzrb0pYT8B+sqdyUuNxD92v5D87Xgb3J9V+aLMkGFC4sEB+M9c Wg/OXDrsqYWRhAQnVtCsuS5qNnZ2ijAMnEqh1tEeGp4Jutri4AEzoRI0bffQ0vCfheFS f3gjeLR5RwjtpzhgPisDYVRQW9hV+6nl1h58Opl/sIvFsgxsq6Lg2nVC1Hhdn5ukrn4w MzDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@verizon.com header.s=corp header.b=nwoIuddd; dkim=fail header.i=@verizon.com header.s=corp header.b=V+bt4LGZ; dkim=fail header.i=@verizon.com header.s=corp header.b=V+bt4LGZ; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=verizon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u186si935285pgc.823.2017.10.06.04.18.28; Fri, 06 Oct 2017 04:18:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of stable-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=@verizon.com header.s=corp header.b=nwoIuddd; dkim=fail header.i=@verizon.com header.s=corp header.b=V+bt4LGZ; dkim=fail header.i=@verizon.com header.s=corp header.b=V+bt4LGZ; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=verizon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752036AbdJFLS1 (ORCPT + 8 others); Fri, 6 Oct 2017 07:18:27 -0400 Received: from omzsmtpe02.verizonbusiness.com ([199.249.25.209]:44225 "EHLO omzsmtpe02.verizonbusiness.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751997AbdJFLST (ORCPT ); Fri, 6 Oct 2017 07:18:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=verizon.com; i=@verizon.com; q=dns/txt; s=corp; t=1507288699; x=1538824699; h=from:cc:to:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=qcjKvdeQso9A4H/10erflpmXRBZ41Ljza7d8c8W4RHg=; b=nwoIudddFSSZHnN1lv2tqySYLaAfvBKm7jo3/6TwKKxkav2mGgs4YbRL nouBKsEaWya/ENJNowviX3yF09hUb21+Tdk1PnDRCDOEB96G5iQI8oelN h55d1qgkgixtNMQZEL++UZ2TGQlaMGrBKVYoRWChpU8AeXhXh00lhq1Wz 0=; Received: from unknown (HELO fldsmtpi02.verizon.com) ([166.68.71.144]) by omzsmtpe02.verizonbusiness.com with ESMTP; 06 Oct 2017 11:18:17 +0000 From: "Levin, Alexander (Sasha Levin)" Cc: Mark Rutland , Peter Zijlstra , Ingo Molnar , "Levin, Alexander (Sasha Levin)" Received: from rogue-10-255-192-101.rogue.vzwcorp.com (HELO apollo.verizonwireless.com) ([10.255.192.101]) by fldsmtpi02.verizon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 06 Oct 2017 11:18:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=verizon.com; i=@verizon.com; q=dns/txt; s=corp; t=1507288693; x=1538824693; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=qcjKvdeQso9A4H/10erflpmXRBZ41Ljza7d8c8W4RHg=; b=V+bt4LGZspOyL+im6xP9+psRbyFKpqNSFgrhHLmKxcQovIutbNheXTXR 6GOP1EyTlEKy7eyQrg5QA7vTo2AGdBdv1qs4PENtTC0e4v2cPVkeF1XUg lutUQDrJCneYz/ealo0DFdmGze1AfuFIUuP5PDj4IOzC1UUpwJwH11YBs g=; Received: from endeavour.tdc.vzwcorp.com (HELO eris.verizonwireless.com) ([10.254.88.163]) by apollo.verizonwireless.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 06 Oct 2017 07:18:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=verizon.com; i=@verizon.com; q=dns/txt; s=corp; t=1507288693; x=1538824693; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=qcjKvdeQso9A4H/10erflpmXRBZ41Ljza7d8c8W4RHg=; b=V+bt4LGZspOyL+im6xP9+psRbyFKpqNSFgrhHLmKxcQovIutbNheXTXR 6GOP1EyTlEKy7eyQrg5QA7vTo2AGdBdv1qs4PENtTC0e4v2cPVkeF1XUg lutUQDrJCneYz/ealo0DFdmGze1AfuFIUuP5PDj4IOzC1UUpwJwH11YBs g=; X-Host: endeavour.tdc.vzwcorp.com Received: from ohtwi1exh002.uswin.ad.vzwcorp.com ([10.144.218.44]) by eris.verizonwireless.com with ESMTP/TLS/AES128-SHA256; 06 Oct 2017 11:18:12 +0000 Received: from tbwexch23apd.uswin.ad.vzwcorp.com (153.114.162.47) by OHTWI1EXH002.uswin.ad.vzwcorp.com (10.144.218.44) with Microsoft SMTP Server (TLS) id 14.3.248.2; Fri, 6 Oct 2017 07:18:12 -0400 Received: from OMZP1LUMXCA17.uswin.ad.vzwcorp.com (144.8.22.195) by tbwexch23apd.uswin.ad.vzwcorp.com (153.114.162.47) with Microsoft SMTP Server (TLS) id 15.0.1263.5; Fri, 6 Oct 2017 07:18:12 -0400 Received: from OMZP1LUMXCA17.uswin.ad.vzwcorp.com (144.8.22.195) by OMZP1LUMXCA17.uswin.ad.vzwcorp.com (144.8.22.195) with Microsoft SMTP Server (TLS) id 15.0.1263.5; Fri, 6 Oct 2017 06:18:11 -0500 Received: from OMZP1LUMXCA17.uswin.ad.vzwcorp.com ([144.8.22.195]) by OMZP1LUMXCA17.uswin.ad.vzwcorp.com ([144.8.22.195]) with mapi id 15.00.1263.000; Fri, 6 Oct 2017 06:18:11 -0500 To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" Subject: [added to the v4.1 stable tree] perf/core: Fix group {cpu,task} validation Thread-Topic: [added to the v4.1 stable tree] perf/core: Fix group {cpu, task} validation Thread-Index: AQHTPpSxMkyov8lxlE29eUUCoVhHXQ== Date: Fri, 6 Oct 2017 11:17:28 +0000 Message-ID: <20171006111704.25872-34-alexander.levin@verizon.com> References: <20171006111704.25872-1-alexander.levin@verizon.com> In-Reply-To: <20171006111704.25872-1-alexander.levin@verizon.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.144.60.250] MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Mark Rutland This patch has been added to the v4.1 stable tree. If you have any objections, please let us know. -- 2.11.0 =============== [ Upstream commit 64aee2a965cf2954a038b5522f11d2cd2f0f8f3e ] Regardless of which events form a group, it does not make sense for the events to target different tasks and/or CPUs, as this leaves the group inconsistent and impossible to schedule. The core perf code assumes that these are consistent across (successfully intialised) groups. Core perf code only verifies this when moving SW events into a HW context. Thus, we can violate this requirement for pure SW groups and pure HW groups, unless the relevant PMU driver happens to perform this verification itself. These mismatched groups subsequently wreak havoc elsewhere. For example, we handle watchpoints as SW events, and reserve watchpoint HW on a per-CPU basis at pmu::event_init() time to ensure that any event that is initialised is guaranteed to have a slot at pmu::add() time. However, the core code only checks the group leader's cpu filter (via event_filter_match()), and can thus install follower events onto CPUs violating thier (mismatched) CPU filters, potentially installing them into a CPU without sufficient reserved slots. This can be triggered with the below test case, resulting in warnings from arch backends. #define _GNU_SOURCE #include #include #include #include #include #include #include static int perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags) { return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags); } char watched_char; struct perf_event_attr wp_attr = { .type = PERF_TYPE_BREAKPOINT, .bp_type = HW_BREAKPOINT_RW, .bp_addr = (unsigned long)&watched_char, .bp_len = 1, .size = sizeof(wp_attr), }; int main(int argc, char *argv[]) { int leader, ret; cpu_set_t cpus; /* * Force use of CPU0 to ensure our CPU0-bound events get scheduled. */ CPU_ZERO(&cpus); CPU_SET(0, &cpus); ret = sched_setaffinity(0, sizeof(cpus), &cpus); if (ret) { printf("Unable to set cpu affinity\n"); return 1; } /* open leader event, bound to this task, CPU0 only */ leader = perf_event_open(&wp_attr, 0, 0, -1, 0); if (leader < 0) { printf("Couldn't open leader: %d\n", leader); return 1; } /* * Open a follower event that is bound to the same task, but a * different CPU. This means that the group should never be possible to * schedule. */ ret = perf_event_open(&wp_attr, 0, 1, leader, 0); if (ret < 0) { printf("Couldn't open mismatched follower: %d\n", ret); return 1; } else { printf("Opened leader/follower with mismastched CPUs\n"); } /* * Open as many independent events as we can, all bound to the same * task, CPU0 only. */ do { ret = perf_event_open(&wp_attr, 0, 0, -1, 0); } while (ret >= 0); /* * Force enable/disble all events to trigger the erronoeous * installation of the follower event. */ printf("Opened all events. Toggling..\n"); for (;;) { prctl(PR_TASK_PERF_EVENTS_DISABLE, 0, 0, 0, 0); prctl(PR_TASK_PERF_EVENTS_ENABLE, 0, 0, 0, 0); } return 0; } Fix this by validating this requirement regardless of whether we're moving events. Signed-off-by: Mark Rutland Signed-off-by: Peter Zijlstra (Intel) Cc: Alexander Shishkin Cc: Arnaldo Carvalho de Melo Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Zhou Chengming Cc: stable@vger.kernel.org Link: http://lkml.kernel.org/r/1498142498-15758-1-git-send-email-mark.rutland@arm.com Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- kernel/events/core.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index e871080bc44e..e5553bdaf6c2 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8102,28 +8102,27 @@ SYSCALL_DEFINE5(perf_event_open, goto err_context; /* - * Do not allow to attach to a group in a different - * task or CPU context: + * Make sure we're both events for the same CPU; + * grouping events for different CPUs is broken; since + * you can never concurrently schedule them anyhow. */ - if (move_group) { - /* - * Make sure we're both on the same task, or both - * per-cpu events. - */ - if (group_leader->ctx->task != ctx->task) - goto err_context; + if (group_leader->cpu != event->cpu) + goto err_context; - /* - * Make sure we're both events for the same CPU; - * grouping events for different CPUs is broken; since - * you can never concurrently schedule them anyhow. - */ - if (group_leader->cpu != event->cpu) - goto err_context; - } else { - if (group_leader->ctx != ctx) - goto err_context; - } + /* + * Make sure we're both on the same task, or both + * per-CPU events. + */ + if (group_leader->ctx->task != ctx->task) + goto err_context; + + /* + * Do not allow to attach to a group in a different task + * or CPU context. If we're moving SW events, we'll fix + * this up later, so allow that. + */ + if (!move_group && group_leader->ctx != ctx) + goto err_context; /* * Only a group leader can be exclusive or pinned