Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2457864imm; Mon, 28 May 2018 08:26:27 -0700 (PDT) X-Google-Smtp-Source: AB8JxZq+Cp3gmOYAx+DGoHNpZm6X4PqEqIVKKvQ+io8YJH+bQasjT25yMbC20lKQV8sY8ED5r5CD X-Received: by 2002:a17:902:26a:: with SMTP id 97-v6mr14201865plc.367.1527521187376; Mon, 28 May 2018 08:26:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527521187; cv=none; d=google.com; s=arc-20160816; b=nIR+fpcYJBQFPw2nUMxsRWcD3PEwBTY9r3ys1VKKPe+SwO2VRXr3pzmrpjpglLCpa0 vdD8HkAvaSA6naeG+zLmPfTVTKSmlUZH2XOMBiR0Bf1xjGfvYZWLvkCRav56km/kTV6p I1KT4Jkd1QhiuwE1xJf+F+robEZGwhPRm6V5Y3ElJNNy8quIpTsyIZZ3Ovn+ayCXa+YZ v6zSuDtO74d5Vl68d3j9Eh+PeMr7u7e+YYxgTaJZW7AHebt/X8i5T6o1qx8LzAy3rEHC fRYUjaLNe7rIM7r2XLadhxRnm5IOgDQ72TKh6+sIehDkGZ1SjY/E+zUw7N6Xwj75fOsc Yy8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=efX523OYAPPFo3ZppU0CyAmHdhjcD1couSjZZcXBvqM=; b=TdcrzBLyHT7zMbLq/PcUTzT2RGjo6yuBO5gJUXi9hRX0CS+rM86yPtEghfNzrZUqpM bZUFfDxSOt52r1E7Oju7aEhd2M3708cFH7OULUjRGewDgbOqNyGGfcNttJcKZkNTKlEN Q56qTLN1J1hW4Y1LDhhwkmdzNUJ1DpMEVGT9QrZ+mww/Nod4u2f+WKTbY0Z5vFLncj0z ABGVXdcSeFNXXb3boCe08Jt0aiE2p9aR4VbSuetaFq9Qa4mRk9O+LHkc+YwAsa6kqPz7 JIFXp1vPpYdx6gmYU6JivKOrUYTUcAwQgDYLR7J0nza08s3OcL9dspSZwlqC4BAD1Qhw 8haA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=f//rA3ks; 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 l12-v6si24314325pgr.367.2018.05.28.08.26.12; Mon, 28 May 2018 08:26:27 -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=f//rA3ks; 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 S1034374AbeE1PZc (ORCPT + 99 others); Mon, 28 May 2018 11:25:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:40152 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1033144AbeE1KUa (ORCPT ); Mon, 28 May 2018 06:20:30 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (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 88B2320843; Mon, 28 May 2018 10:20:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527502830; bh=ImTSAnIq/9cjoChwfKXTvkj0M4hGeUSUPbSpPyna31Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f//rA3kspcFHgzW6L/RlTdl3dSjJ/1bp2BSiJIuQ/ADv0gNWRNk53TDMbIstmbRPY QtdmoEMOOShZdrisfi6gv7TEOnsnSAIbmWVBw+khXNIEnE5rV/8sTYcbBuCFyq+MNC dqTjJLnESORyzS+p3IK4yqvI1ctqnKbFJ5PiU3As= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ephraim Park , Song Liu , "Peter Zijlstra (Intel)" , jolsa@redhat.com, kernel-team@fb.com, Alexander Shishkin , Arnaldo Carvalho de Melo , Linus Torvalds , Stephane Eranian , Thomas Gleixner , Vince Weaver , Ingo Molnar , Sasha Levin Subject: [PATCH 4.4 142/268] perf/cgroup: Fix child event counting bug Date: Mon, 28 May 2018 12:01:56 +0200 Message-Id: <20180528100218.394351853@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100202.045206534@linuxfoundation.org> References: <20180528100202.045206534@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Song Liu [ Upstream commit c917e0f259908e75bd2a65877e25f9d90c22c848 ] When a perf_event is attached to parent cgroup, it should count events for all children cgroups: parent_group <---- perf_event \ - child_group <---- process(es) However, in our tests, we found this perf_event cannot report reliable results. Here is an example case: # create cgroups mkdir -p /sys/fs/cgroup/p/c # start perf for parent group perf stat -e instructions -G "p" # on another console, run test process in child cgroup: stressapptest -s 2 -M 1000 & echo $! > /sys/fs/cgroup/p/c/cgroup.procs # after the test process is done, stop perf in the first console shows instructions p The instruction should not be "not counted" as the process runs in the child cgroup. We found this is because perf_event->cgrp and cpuctx->cgrp are not identical, thus perf_event->cgrp are not updated properly. This patch fixes this by updating perf_cgroup properly for ancestor cgroup(s). Reported-by: Ephraim Park Signed-off-by: Song Liu Signed-off-by: Peter Zijlstra (Intel) Cc: Cc: Cc: Alexander Shishkin Cc: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Thomas Gleixner Cc: Vince Weaver Link: http://lkml.kernel.org/r/20180312165943.1057894-1-songliubraving@fb.com Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- kernel/events/core.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -419,9 +419,15 @@ static inline void __update_cgrp_time(st static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) { - struct perf_cgroup *cgrp_out = cpuctx->cgrp; - if (cgrp_out) - __update_cgrp_time(cgrp_out); + struct perf_cgroup *cgrp = cpuctx->cgrp; + struct cgroup_subsys_state *css; + + if (cgrp) { + for (css = &cgrp->css; css; css = css->parent) { + cgrp = container_of(css, struct perf_cgroup, css); + __update_cgrp_time(cgrp); + } + } } static inline void update_cgrp_time_from_event(struct perf_event *event) @@ -449,6 +455,7 @@ perf_cgroup_set_timestamp(struct task_st { struct perf_cgroup *cgrp; struct perf_cgroup_info *info; + struct cgroup_subsys_state *css; /* * ctx->lock held by caller @@ -459,8 +466,12 @@ perf_cgroup_set_timestamp(struct task_st return; cgrp = perf_cgroup_from_task(task, ctx); - info = this_cpu_ptr(cgrp->info); - info->timestamp = ctx->timestamp; + + for (css = &cgrp->css; css; css = css->parent) { + cgrp = container_of(css, struct perf_cgroup, css); + info = this_cpu_ptr(cgrp->info); + info->timestamp = ctx->timestamp; + } } #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */