Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4823200pxj; Wed, 9 Jun 2021 02:44:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTDFLVmAc69o8r1FMJbA1egR3tsE5ock6eIoX9j2bCSCs1nSTs4NePow4b/Ck8LapeDSZc X-Received: by 2002:a05:6602:2d12:: with SMTP id c18mr16219815iow.170.1623231877636; Wed, 09 Jun 2021 02:44:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623231877; cv=none; d=google.com; s=arc-20160816; b=sf1D/0qGEyKF4CzZz0FGyAvRqfRmM7zID0DqpwcL/OXPMDgfrG0tjN0hsQiuuWGw7a bZSjd8ZJoGJcayPEm6hmicTxaT9uU3E8QcfCgUHGUpyIfJaoCdY92CS+z9A52YBRVLrQ WDPma4XsSc1zSdQwcbCZaG04GdWllGJf+EmMXAKWkQ+0rMuOawjwyBYZj1vmgS5ARhau Ma8oBOHL/revvPxHDSR8JoSnBQxk82xcTGtPgbP9phaontnI7paSTmZ+fboYrNHaZ+iC 48xDKbsNRpuPohwvzvkP5W7Hh2OCCqz0W1gxGe3JfekjsD8wx4JinyuzNboX1Qo8Yy9y 4t0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ngl2Y5laiwpCmeyWr6BryKJXN+AcejHvXi3j7g2clhI=; b=xXh+hpHQO/w/3Fbr9mhg3gEEDGyORpfFRnZOrv3DYBjA0UciUPBmxgAl4Y46mdTs8E a2MVsF1BaLXSAJ0PxfZG+H3EHxroDzWDmQLpNtjbQVLqgfWqKYVIZPanxp35wOa4xwAY B7rEF6O43/tUw03427qFYMGJYIHaHSkZdMTGPJGEUGFs3uk7HQ7mLRDV1jC8ShODGxy/ WuKmZqmXsThlbYkq9IrXcqf9CAm7QmSniIud9apZjwskTj7UoYhjq2O4ZdiF+zrWxUo5 jHNU16SE8ntm0Ktd7YZGf+TcjKRTBi7x9HzuNAOLa4cVGOrQfaaWIcQDShmg2YIZ0Xtp 3HDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rf7P3Yyc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z2si2555717jat.82.2021.06.09.02.44.25; Wed, 09 Jun 2021 02:44:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rf7P3Yyc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235435AbhFHSni (ORCPT + 99 others); Tue, 8 Jun 2021 14:43:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:37652 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235391AbhFHSj4 (ORCPT ); Tue, 8 Jun 2021 14:39:56 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2F13E6142D; Tue, 8 Jun 2021 18:34:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623177269; bh=W8PC+5cy/PBfmPbNY6yr9Y0XcPs97LVB5bTj8otjupU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rf7P3Yyc1k1eFtuSB+ZZn8MF7JouBHr3NloJ43avI/lnSplzDjvQjIW0Sipx81EGB 2nXWRCOWQ0MII5v8EqwZpsTf7MvLMYPoMQpJCC+75S9GNLXGJr06y6mLHRQ5xYASLw cfBg38+MOFcEo4hKcu7Wn71hbbgUmBLnFqjl9i40= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ian Rogers , "Peter Zijlstra (Intel)" , Alexander Shishkin , Arnaldo Carvalho de Melo , Arnaldo Carvalho de Melo , Borislav Petkov , Jiri Olsa , Kan Liang , Linus Torvalds , Namhyung Kim , Stephane Eranian , Thomas Gleixner , Vince Weaver , Ingo Molnar , Wen Yang Subject: [PATCH 4.19 50/58] perf/cgroups: Dont rotate events for cgroups unnecessarily Date: Tue, 8 Jun 2021 20:27:31 +0200 Message-Id: <20210608175933.924635262@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210608175932.263480586@linuxfoundation.org> References: <20210608175932.263480586@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ian Rogers commit fd7d55172d1e2e501e6da0a5c1de25f06612dc2e upstream. Currently perf_rotate_context assumes that if the context's nr_events != nr_active a rotation is necessary for perf event multiplexing. With cgroups, nr_events is the total count of events for all cgroups and nr_active will not include events in a cgroup other than the current task's. This makes rotation appear necessary for cgroups when it is not. Add a perf_event_context flag that is set when rotation is necessary. Clear the flag during sched_out and set it when a flexible sched_in fails due to resources. Signed-off-by: Ian Rogers Signed-off-by: Peter Zijlstra (Intel) Cc: Alexander Shishkin Cc: Arnaldo Carvalho de Melo Cc: Arnaldo Carvalho de Melo Cc: Borislav Petkov Cc: Jiri Olsa Cc: Kan Liang Cc: Linus Torvalds Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Thomas Gleixner Cc: Vince Weaver Link: https://lkml.kernel.org/r/20190601082722.44543-1-irogers@google.com Signed-off-by: Ingo Molnar Signed-off-by: Wen Yang Signed-off-by: Greg Kroah-Hartman --- include/linux/perf_event.h | 5 +++++ kernel/events/core.c | 42 ++++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 20 deletions(-) --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -747,6 +747,11 @@ struct perf_event_context { int nr_stat; int nr_freq; int rotate_disable; + /* + * Set when nr_events != nr_active, except tolerant to events not + * necessary to be active due to scheduling constraints, such as cgroups. + */ + int rotate_necessary; atomic_t refcount; struct task_struct *task; --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -2952,6 +2952,12 @@ static void ctx_sched_out(struct perf_ev if (!ctx->nr_active || !(is_active & EVENT_ALL)) return; + /* + * If we had been multiplexing, no rotations are necessary, now no events + * are active. + */ + ctx->rotate_necessary = 0; + perf_pmu_disable(ctx->pmu); if (is_active & EVENT_PINNED) { list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list) @@ -3319,10 +3325,13 @@ static int flexible_sched_in(struct perf return 0; if (group_can_go_on(event, sid->cpuctx, sid->can_add_hw)) { - if (!group_sched_in(event, sid->cpuctx, sid->ctx)) - list_add_tail(&event->active_list, &sid->ctx->flexible_active); - else + int ret = group_sched_in(event, sid->cpuctx, sid->ctx); + if (ret) { sid->can_add_hw = 0; + sid->ctx->rotate_necessary = 1; + return 0; + } + list_add_tail(&event->active_list, &sid->ctx->flexible_active); } return 0; @@ -3690,24 +3699,17 @@ ctx_first_active(struct perf_event_conte static bool perf_rotate_context(struct perf_cpu_context *cpuctx) { struct perf_event *cpu_event = NULL, *task_event = NULL; - bool cpu_rotate = false, task_rotate = false; - struct perf_event_context *ctx = NULL; + struct perf_event_context *task_ctx = NULL; + int cpu_rotate, task_rotate; /* * Since we run this from IRQ context, nobody can install new * events, thus the event count values are stable. */ - if (cpuctx->ctx.nr_events) { - if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) - cpu_rotate = true; - } - - ctx = cpuctx->task_ctx; - if (ctx && ctx->nr_events) { - if (ctx->nr_events != ctx->nr_active) - task_rotate = true; - } + cpu_rotate = cpuctx->ctx.rotate_necessary; + task_ctx = cpuctx->task_ctx; + task_rotate = task_ctx ? task_ctx->rotate_necessary : 0; if (!(cpu_rotate || task_rotate)) return false; @@ -3716,7 +3718,7 @@ static bool perf_rotate_context(struct p perf_pmu_disable(cpuctx->ctx.pmu); if (task_rotate) - task_event = ctx_first_active(ctx); + task_event = ctx_first_active(task_ctx); if (cpu_rotate) cpu_event = ctx_first_active(&cpuctx->ctx); @@ -3724,17 +3726,17 @@ static bool perf_rotate_context(struct p * As per the order given at ctx_resched() first 'pop' task flexible * and then, if needed CPU flexible. */ - if (task_event || (ctx && cpu_event)) - ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); + if (task_event || (task_ctx && cpu_event)) + ctx_sched_out(task_ctx, cpuctx, EVENT_FLEXIBLE); if (cpu_event) cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); if (task_event) - rotate_ctx(ctx, task_event); + rotate_ctx(task_ctx, task_event); if (cpu_event) rotate_ctx(&cpuctx->ctx, cpu_event); - perf_event_sched_in(cpuctx, ctx, current); + perf_event_sched_in(cpuctx, task_ctx, current); perf_pmu_enable(cpuctx->ctx.pmu); perf_ctx_unlock(cpuctx, cpuctx->task_ctx);