Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp36953imu; Thu, 8 Nov 2018 14:19:13 -0800 (PST) X-Google-Smtp-Source: AJdET5frKnglJcNzylDsHkZw+Qt+j4i8VBuH2LhQw2f+iC04OxMVhZH3hPFVekEK8SLtyJ+IljgJ X-Received: by 2002:a63:1b48:: with SMTP id b8mr5304457pgm.187.1541715553256; Thu, 08 Nov 2018 14:19:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541715553; cv=none; d=google.com; s=arc-20160816; b=ERetJJSZRKA5/+k8IjDFf4281yHNVhrgdGBZ1RaF1ihRj24gCM/aDLkBdR/wmbOUdv Iqd/tif/B0GA8eUm/i5R+hEU+O/syw3FFZUNPQKLC93QCQ1dXNBRDe/0YxDyWFoOlbdF lQ07kSHYnxzKtSVw/f89GGbsDECr3OgOK/i9WRxVJHRoYIssHx8JB6rdLu5soYuNTuZQ gbpQK/M/Af9LCXpRVtRtYFPB8+ZIGfJFZyjAAj8ZQjmIhNFaziCG9HUQ70D7JZquxO35 8XxYDwp4PkP3vDs3gNH61WIztmoJxRYLY7Dc3uaNG4cfMwKyzjSmASfEQbV1bkftEwbH mIRw== 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=ZapotoHPlIGNvtGF23ZBP7w6FD8v1CzWnjdK6lTNSBI=; b=Eb2lld6oyxilalD4MkRByP9G0J/6hZK1nb2W6C+in5bDB7RebpoWcz/U2KejLXGJma fV/4SQNETEByYuLchS4BN2tsUueHr7J4VLXhcOiohR913NHVzHXQR7cjDrcpaGZaH8H9 dVZ6rXKL00JD82vMkwOv15dxXwYs5mjSOD93KQrBqfIQAKK5OIGY18vQO5OTbNkpvKSH fXQYZH4U213wp9oUYAD6kfIXt8OCxokb7ChdOUBMbK0e8vsFbXOqYPSErie4DdEmZWJ+ keQxcYPyEcl3xNdIOr9a1jxK0reqcsJr9bUWcHC0S9mUp3RKQ4WJ+GGaQoOorZxfCU4C LXGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cOxYTDAz; 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 g14-v6si5160846pgf.10.2018.11.08.14.18.58; Thu, 08 Nov 2018 14:19:13 -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=cOxYTDAz; 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 S1732061AbeKIHoQ (ORCPT + 99 others); Fri, 9 Nov 2018 02:44:16 -0500 Received: from mail.kernel.org ([198.145.29.99]:37058 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731993AbeKIHoP (ORCPT ); Fri, 9 Nov 2018 02:44:15 -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 F031621019; Thu, 8 Nov 2018 22:06:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541714804; bh=RNv5JV/WI/SNEVhg9NAOt0tkoxVGOkXQw4AvHeL0Zp0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cOxYTDAzsJhM5FpSzEEv7qC1EdON5Wpy3s1S+SJ26fljTw1dZhajgzdiQuCTCAxSG CeUrZA8ChGqEGbsmEAbT6njO/g8H8NuYOAUV7us5KMp1VGdIj2yuIDve6c++cST/zd tKcP1tVVGswbalTPOS4RVkOVjvA37SvhI2R76F4Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andi Kleen , Jiri Olsa , "Peter Zijlstra (Intel)" , Alexander Shishkin , Arnaldo Carvalho de Melo , Jiri Olsa , Linus Torvalds , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Sasha Levin Subject: [PATCH 4.9 109/171] perf/core: Fix locking for children siblings group read Date: Thu, 8 Nov 2018 13:51:19 -0800 Message-Id: <20181108215135.236783346@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181108215127.257643509@linuxfoundation.org> References: <20181108215127.257643509@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.9-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 2aeb1883547626d82c597cce2c99f0b9c62e2425 ] We're missing ctx lock when iterating children siblings within the perf_read path for group reading. Following race and crash can happen: User space doing read syscall on event group leader: T1: perf_read lock event->ctx->mutex perf_read_group lock leader->child_mutex __perf_read_group_add(child) list_for_each_entry(sub, &leader->sibling_list, group_entry) ----> sub might be invalid at this point, because it could get removed via perf_event_exit_task_context in T2 Child exiting and cleaning up its events: T2: perf_event_exit_task_context lock ctx->mutex list_for_each_entry_safe(child_event, next, &child_ctx->event_list,... perf_event_exit_event(child) lock ctx->lock perf_group_detach(child) unlock ctx->lock ----> child is removed from sibling_list without any sync with T1 path above ... free_event(child) Before the child is removed from the leader's child_list, (and thus is omitted from perf_read_group processing), we need to ensure that perf_read_group touches child's siblings under its ctx->lock. Peter further notes: | One additional note; this bug got exposed by commit: | | ba5213ae6b88 ("perf/core: Correct event creation with PERF_FORMAT_GROUP") | | which made it possible to actually trigger this code-path. Tested-by: Andi Kleen Signed-off-by: Jiri Olsa Acked-by: Peter Zijlstra (Intel) Cc: Alexander Shishkin Cc: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Peter Zijlstra Cc: Thomas Gleixner Fixes: ba5213ae6b88 ("perf/core: Correct event creation with PERF_FORMAT_GROUP") Link: http://lkml.kernel.org/r/20170720141455.2106-1-jolsa@kernel.org Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- kernel/events/core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/events/core.c b/kernel/events/core.c index 95bd00d9f2c3..06b359af4322 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -4331,7 +4331,9 @@ EXPORT_SYMBOL_GPL(perf_event_read_value); static int __perf_read_group_add(struct perf_event *leader, u64 read_format, u64 *values) { + struct perf_event_context *ctx = leader->ctx; struct perf_event *sub; + unsigned long flags; int n = 1; /* skip @nr */ int ret; @@ -4361,12 +4363,15 @@ static int __perf_read_group_add(struct perf_event *leader, if (read_format & PERF_FORMAT_ID) values[n++] = primary_event_id(leader); + raw_spin_lock_irqsave(&ctx->lock, flags); + list_for_each_entry(sub, &leader->sibling_list, group_entry) { values[n++] += perf_event_count(sub); if (read_format & PERF_FORMAT_ID) values[n++] = primary_event_id(sub); } + raw_spin_unlock_irqrestore(&ctx->lock, flags); return 0; } -- 2.17.1