From nobody Sat Jun 20 01:02:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1BDEC433EF for ; Fri, 25 Mar 2022 03:54:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349754AbiCYDzm (ORCPT ); Thu, 24 Mar 2022 23:55:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346155AbiCYDzh (ORCPT ); Thu, 24 Mar 2022 23:55:37 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 802803615E for ; Thu, 24 Mar 2022 20:54:03 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id t2so5483111pfj.10 for ; Thu, 24 Mar 2022 20:54:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tGpRT3RxXk4Z+RfR3AojOlITuboxzWYBrYGSuiIe62w=; b=0n3o9lEO7HMh1Ibbvwofn5MhvEUv16HR8EAAwUriiSOE90yW0FijiFEknOzu9YvjV4 HhU2WE6N1ci1Y0YaSsx/Gko/31tOcZjQqora6kOJSNRmbzidIihNXzvhaVbHnpr+BWVn qohbXL0XjLUsS/g76JYe5i6aoD7jfWG2S3hnFrvUXDfW0obobntT0CR0QGVgf9FMRH+m D+Q7rjGmGOCsIBVVC2IMwJG2XjYvlr6nQ6Lbp3VhbtiaXwcHbG+PkMt/RYJ/cm8Jicwa JsQ7LFggflYV3LWZ1KiMFlyheFV7DxLAFyJinon/McdWdv2H8L6NuhO9+BHkclHzL2gx +VxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tGpRT3RxXk4Z+RfR3AojOlITuboxzWYBrYGSuiIe62w=; b=8QMVdvbg1cz9nlqEPNJ3hDVvcy3xQp37/UhQtQCXY/6PNtTVUrjav44GQDbE9SOEPP N/HGu/Z9LgxqPoJ3OfBAFlDTxW3hPwM10M/1vt3xUe47jLOVX8O+obriuMziTESvW2O1 UY51lvzxd9RLLX9p367rXMU/uUxm5cq1MQXQpdi5j45aZzJLP5bAbW3mKUEQkK0MtjUg 2TGhi88jBhDjVmwsyRsPEKKVIM4gCiyYtHdFq9NvbXOrnJHklL4PmkR5mbjLbTi6jYL9 6DQceAvgy2Aqj0J+UK4X6o1y11FtI9m+eVqkSCmNTyUeMyeHJ7GmDMNDhHTmGvGpr13G VdXg== X-Gm-Message-State: AOAM533v18pRVtxhgC9AjkwrI37FKMUzYYV6LuKL6qQNhygMa+z/DxFs VgYO9LbMySontj2t+RtKiBf/zEoT0qCEEQ== X-Google-Smtp-Source: ABdhPJymYZEopM6Js2KSnwiNLa/qHCUe6hsL81AMukl23CTJqDFabGSoTIB1oLb2bAWbQQ0OITEw5Q== X-Received: by 2002:a65:568d:0:b0:382:2e6f:f098 with SMTP id v13-20020a65568d000000b003822e6ff098mr6357523pgs.242.1648180443050; Thu, 24 Mar 2022 20:54:03 -0700 (PDT) Received: from C02CV1DAMD6P.bytedance.net ([2409:8a28:e6a:5860:acb7:c92:7e7f:45ef]) by smtp.gmail.com with ESMTPSA id u10-20020a17090a2b8a00b001c6594e5ddcsm4286504pjd.15.2022.03.24.20.53.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Mar 2022 20:54:02 -0700 (PDT) From: Chengming Zhou To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, eranian@google.com Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, Chengming Zhou Subject: [PATCH v3 1/5] perf/core: Don't pass task around when ctx sched in Date: Fri, 25 Mar 2022 11:53:14 +0800 Message-Id: <20220325035318.42168-2-zhouchengming@bytedance.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325035318.42168-1-zhouchengming@bytedance.com> References: <20220325035318.42168-1-zhouchengming@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The current code pass task around for ctx_sched_in(), only to get perf_cgroup of the task, then update the timestamp of it and its ancestors and set them to active. But we can use cpuctx->cgrp to get active perf_cgroup and its ancestors since cpuctx->cgrp has been set before ctx_sched_in(). This patch remove the task argument in ctx_sched_in() and cleanup related code. Signed-off-by: Chengming Zhou --- kernel/events/core.c | 58 ++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index cfde994ce61c..d50f45012c05 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -574,8 +574,7 @@ static void cpu_ctx_sched_out(struct perf_cpu_context *= cpuctx, enum event_type_t event_type); =20 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, - enum event_type_t event_type, - struct task_struct *task); + enum event_type_t event_type); =20 static void update_context_time(struct perf_event_context *ctx); static u64 perf_event_time(struct perf_event *event); @@ -801,10 +800,10 @@ static inline void update_cgrp_time_from_event(struct= perf_event *event) } =20 static inline void -perf_cgroup_set_timestamp(struct task_struct *task, - struct perf_event_context *ctx) +perf_cgroup_set_timestamp(struct perf_cpu_context *cpuctx) { - struct perf_cgroup *cgrp; + struct perf_event_context *ctx =3D &cpuctx->ctx; + struct perf_cgroup *cgrp =3D cpuctx->cgrp; struct perf_cgroup_info *info; struct cgroup_subsys_state *css; =20 @@ -813,10 +812,10 @@ perf_cgroup_set_timestamp(struct task_struct *task, * ensure we do not access cgroup data * unless we have the cgroup pinned (css_get) */ - if (!task || !ctx->nr_cgroups) + if (!cgrp) return; =20 - cgrp =3D perf_cgroup_from_task(task, ctx); + WARN_ON_ONCE(!ctx->nr_cgroups); =20 for (css =3D &cgrp->css; css; css =3D css->parent) { cgrp =3D container_of(css, struct perf_cgroup, css); @@ -869,14 +868,14 @@ static void perf_cgroup_switch(struct task_struct *ta= sk, int mode) WARN_ON_ONCE(cpuctx->cgrp); /* * set cgrp before ctxsw in to allow - * event_filter_match() to not have to pass - * task around + * perf_cgroup_set_timestamp() in ctx_sched_in() + * to not have to pass task around * we pass the cpuctx->ctx to perf_cgroup_from_task() * because cgorup events are only per-cpu */ cpuctx->cgrp =3D perf_cgroup_from_task(task, &cpuctx->ctx); - cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); + cpu_ctx_sched_in(cpuctx, EVENT_ALL); } perf_pmu_enable(cpuctx->ctx.pmu); perf_ctx_unlock(cpuctx, cpuctx->task_ctx); @@ -1118,8 +1117,7 @@ static inline int perf_cgroup_connect(pid_t pid, stru= ct perf_event *event, } =20 static inline void -perf_cgroup_set_timestamp(struct task_struct *task, - struct perf_event_context *ctx) +perf_cgroup_set_timestamp(struct perf_cpu_context *cpuctx) { } =20 @@ -2713,8 +2711,7 @@ static void ctx_sched_out(struct perf_event_context *= ctx, static void ctx_sched_in(struct perf_event_context *ctx, struct perf_cpu_context *cpuctx, - enum event_type_t event_type, - struct task_struct *task); + enum event_type_t event_type); =20 static void task_ctx_sched_out(struct perf_cpu_context *cpuctx, struct perf_event_context *ctx, @@ -2730,15 +2727,14 @@ static void task_ctx_sched_out(struct perf_cpu_cont= ext *cpuctx, } =20 static void perf_event_sched_in(struct perf_cpu_context *cpuctx, - struct perf_event_context *ctx, - struct task_struct *task) + struct perf_event_context *ctx) { - cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); + cpu_ctx_sched_in(cpuctx, EVENT_PINNED); if (ctx) - ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); - cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); + ctx_sched_in(ctx, cpuctx, EVENT_PINNED); + cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE); if (ctx) - ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); + ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE); } =20 /* @@ -2788,7 +2784,7 @@ static void ctx_resched(struct perf_cpu_context *cpuc= tx, else if (ctx_event_type & EVENT_PINNED) cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); =20 - perf_event_sched_in(cpuctx, task_ctx, current); + perf_event_sched_in(cpuctx, task_ctx); perf_pmu_enable(cpuctx->ctx.pmu); } =20 @@ -3011,7 +3007,7 @@ static void __perf_event_enable(struct perf_event *ev= ent, return; =20 if (!event_filter_match(event)) { - ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); + ctx_sched_in(ctx, cpuctx, EVENT_TIME); return; } =20 @@ -3020,7 +3016,7 @@ static void __perf_event_enable(struct perf_event *ev= ent, * then don't put it on unless the group is on. */ if (leader !=3D event && leader->state !=3D PERF_EVENT_STATE_ACTIVE) { - ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); + ctx_sched_in(ctx, cpuctx, EVENT_TIME); return; } =20 @@ -3865,8 +3861,7 @@ ctx_flexible_sched_in(struct perf_event_context *ctx, static void ctx_sched_in(struct perf_event_context *ctx, struct perf_cpu_context *cpuctx, - enum event_type_t event_type, - struct task_struct *task) + enum event_type_t event_type) { int is_active =3D ctx->is_active; =20 @@ -3878,7 +3873,7 @@ ctx_sched_in(struct perf_event_context *ctx, if (is_active ^ EVENT_TIME) { /* start ctx time */ __update_context_time(ctx, false); - perf_cgroup_set_timestamp(task, ctx); + perf_cgroup_set_timestamp(cpuctx); /* * CPU-release for the below ->is_active store, * see __load_acquire() in perf_event_time_now() @@ -3909,12 +3904,11 @@ ctx_sched_in(struct perf_event_context *ctx, } =20 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, - enum event_type_t event_type, - struct task_struct *task) + enum event_type_t event_type) { struct perf_event_context *ctx =3D &cpuctx->ctx; =20 - ctx_sched_in(ctx, cpuctx, event_type, task); + ctx_sched_in(ctx, cpuctx, event_type); } =20 static void perf_event_context_sched_in(struct perf_event_context *ctx, @@ -3956,7 +3950,7 @@ static void perf_event_context_sched_in(struct perf_e= vent_context *ctx, */ if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); - perf_event_sched_in(cpuctx, ctx, task); + perf_event_sched_in(cpuctx, ctx); =20 if (cpuctx->sched_cb_usage && pmu->sched_task) pmu->sched_task(cpuctx->task_ctx, true); @@ -4267,7 +4261,7 @@ static bool perf_rotate_context(struct perf_cpu_conte= xt *cpuctx) if (cpu_event) rotate_ctx(&cpuctx->ctx, cpu_event); =20 - perf_event_sched_in(cpuctx, task_ctx, current); + perf_event_sched_in(cpuctx, task_ctx); =20 perf_pmu_enable(cpuctx->ctx.pmu); perf_ctx_unlock(cpuctx, cpuctx->task_ctx); @@ -4339,7 +4333,7 @@ static void perf_event_enable_on_exec(int ctxn) clone_ctx =3D unclone_ctx(ctx); ctx_resched(cpuctx, ctx, event_type); } else { - ctx_sched_in(ctx, cpuctx, EVENT_TIME, current); + ctx_sched_in(ctx, cpuctx, EVENT_TIME); } perf_ctx_unlock(cpuctx, ctx); =20 --=20 2.20.1 From nobody Sat Jun 20 01:02:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44DF4C433FE for ; Fri, 25 Mar 2022 03:54:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351533AbiCYDzu (ORCPT ); Thu, 24 Mar 2022 23:55:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352153AbiCYDzo (ORCPT ); Thu, 24 Mar 2022 23:55:44 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AB7A387AE for ; Thu, 24 Mar 2022 20:54:10 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id g19so5481670pfc.9 for ; Thu, 24 Mar 2022 20:54:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ggHOE2PxmNXtiInLf6967ShWoItkzb18mMZl8JKes8Y=; b=ufLqG0BJBtiwrAE9n5nzk9Ts4LaagK2DBj+a+J6R6A/f+VMKjJOYl/xxQPFjTljx/I pF6rbzi5ggn6MkAtE62zeasQBCYvkKGy7xxCE8HMryWK68xdn6sU08OpouD3E0EH2FOI 4igt/FyZUyEvnrGK/6+6o1FHofyjTC4OhctHJ1f15Drde3pUkuIg0kn1frgcxq2Enbqu /Sd3msFHpw8I6YhCHMDovCw4lG55M5r/qHIzlUgl2Yy/+6ps7pQ48TmQHZ8tvQ/07RuD DMZvoQP1jAZOkMes7LlMohoJpF00WDNRvPFySwkvhQ/LyNbEjySOkcKjYnN5sWmlmmDu 0rcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ggHOE2PxmNXtiInLf6967ShWoItkzb18mMZl8JKes8Y=; b=vw1SOEHImpkNfh6DiiCkCaSRjp6HtI7KXri/JKldN3FbOUKUVZLzMsHuZS19XtjwAt xoabyeIxSZhYAAatcOsm+W2stI281rODxdzOhlb5+03o78+5d2m0rrfuiF2g3Wqg7qq4 mmiCF1QEeRwrA1jYncDpLeP3vQ7JWeudhvql8nJqaCqBdVPeupBHsWDoVFTNuftjuSlT ddOefxBowbNlVbe+qGthc5JIKCKrY9SZUCFQwoylCQCAUGrqsdyp6SzveGGlUQyEJ7YI oBw7JvlYvWv/KfKJm6U2xWCBEI9ZPz3T3EqxTk4BGQuYAMvebtuEWT357JFTnDUJC9i9 Rm/g== X-Gm-Message-State: AOAM531Y39w60K61jj3fUkR5g86tZG7c2fR64cDgjFi2rMKfmgtSMEP4 LNXV2YdUW5iFzJLKB/Tdq3wCgQ== X-Google-Smtp-Source: ABdhPJxAxMqoQGKyey8iorX7uMSU6WSv8TUeL7QH87mYJFfDXv2QYrkHOcVozid9s8crn4p9ltdpKA== X-Received: by 2002:a05:6a00:16c7:b0:4f7:e497:69b8 with SMTP id l7-20020a056a0016c700b004f7e49769b8mr8304309pfc.6.1648180449569; Thu, 24 Mar 2022 20:54:09 -0700 (PDT) Received: from C02CV1DAMD6P.bytedance.net ([2409:8a28:e6a:5860:acb7:c92:7e7f:45ef]) by smtp.gmail.com with ESMTPSA id u10-20020a17090a2b8a00b001c6594e5ddcsm4286504pjd.15.2022.03.24.20.54.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Mar 2022 20:54:09 -0700 (PDT) From: Chengming Zhou To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, eranian@google.com Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, Chengming Zhou Subject: [PATCH v3 2/5] perf/core: Use perf_cgroup_info->active to check if cgroup is active Date: Fri, 25 Mar 2022 11:53:15 +0800 Message-Id: <20220325035318.42168-3-zhouchengming@bytedance.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325035318.42168-1-zhouchengming@bytedance.com> References: <20220325035318.42168-1-zhouchengming@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Since we use perf_cgroup_set_timestamp() to start cgroup time and set active to 1, then use update_cgrp_time_from_cpuctx() to stop cgroup time and set active to 0. We can use info->active directly to check if cgroup is active. Signed-off-by: Chengming Zhou --- kernel/events/core.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index d50f45012c05..dd985c77bc37 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -780,7 +780,6 @@ static inline void update_cgrp_time_from_cpuctx(struct = perf_cpu_context *cpuctx, static inline void update_cgrp_time_from_event(struct perf_event *event) { struct perf_cgroup_info *info; - struct perf_cgroup *cgrp; =20 /* * ensure we access cgroup data only when needed and @@ -789,14 +788,12 @@ static inline void update_cgrp_time_from_event(struct= perf_event *event) if (!is_cgroup_event(event)) return; =20 - cgrp =3D perf_cgroup_from_task(current, event->ctx); + info =3D this_cpu_ptr(event->cgrp->info); /* * Do not update time when cgroup is not active */ - if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup)) { - info =3D this_cpu_ptr(event->cgrp->info); + if (info->active) __update_cgrp_time(info, perf_clock(), true); - } } =20 static inline void --=20 2.20.1 From nobody Sat Jun 20 01:02:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48844C433F5 for ; Fri, 25 Mar 2022 03:54:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356583AbiCYDz4 (ORCPT ); Thu, 24 Mar 2022 23:55:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354760AbiCYDzv (ORCPT ); Thu, 24 Mar 2022 23:55:51 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C3BC3B018 for ; Thu, 24 Mar 2022 20:54:16 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id g3so6822118plo.6 for ; Thu, 24 Mar 2022 20:54:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=J0LhTTw9Vun6svrqvKc5N0TblTZhNbhimv3XzOgm9U8=; b=EdXqfngfPLwlPOx4i8FCvtA4gN5E1opa3zpKfbspNq0IG+ST7TnywUV76VfdDVBgJM irpWCQkZ1dBZ42r1p4JnnIk01lKq2Rw/D6rXnYnOW7jphqvg3lHa2ivf9aAdAWUplbW9 nPdDjV0E9xw4f09dgE/2c4hzobAbE0OBBeOLCZ7VtpyAZJm/K9bW4d+o/v2Eq7CMAFTr YL08g9xNH95YCyF3FxaR5PZNbWil0s2stoR2COtTzUxsQv0kQCmV8YRGjmFN5idP+B45 nbz71RPb1U4szlXjgctxPfb/BzxPKzOypbv7CeEA3hFCVaVpNp5qsoAg4v2v/75s5reW BUzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=J0LhTTw9Vun6svrqvKc5N0TblTZhNbhimv3XzOgm9U8=; b=WIxn+7uRIMiavgGhqKUULBIeiVfDLLPWXa9GCcmS4vzO8vn4x8vEqA8Jo9XPmrggLA guuP6hVEN/y09msI6v2PXePmk6FmK+heXvGvYtcYM9xCRn1BTx8e8wfDCmAJrUaqvXAj Qrg6kYhPdXJ34avOB9NKZ7qcpVpxZyq2QJNo1brT9IcwWklZPshdzsvU1+76SXAs9sZC 1Nm2Qmo6BmPhPHSipLxwnM56qEFxCadZFUWSOIempHe7b+tyOTT3OpndX5OBR/afDIds jw2537GOiMytQI/CnC2wYyxy3Ty003B1G9S+PbcDTeqpoTrntpwenkINHGj0FZvOegUR iHdg== X-Gm-Message-State: AOAM5331qKSxObrmm9PBb/8bTNHpDXPk5e8LJAVnc8DKJy5cFA3WDUVm /dHISDRFdVMuWX2neNVLCyGAEg== X-Google-Smtp-Source: ABdhPJzhak+gVuerVwpZ2n/ocAh6wTFPCwP+XrfCNMYSqRxLV6nPg6Wn5h2UxTUIC5T4ENBC1GOkUg== X-Received: by 2002:a17:90a:d584:b0:1bc:e520:91f2 with SMTP id v4-20020a17090ad58400b001bce52091f2mr22816450pju.192.1648180456095; Thu, 24 Mar 2022 20:54:16 -0700 (PDT) Received: from C02CV1DAMD6P.bytedance.net ([2409:8a28:e6a:5860:acb7:c92:7e7f:45ef]) by smtp.gmail.com with ESMTPSA id u10-20020a17090a2b8a00b001c6594e5ddcsm4286504pjd.15.2022.03.24.20.54.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Mar 2022 20:54:15 -0700 (PDT) From: Chengming Zhou To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, eranian@google.com Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, Chengming Zhou Subject: [PATCH v3 3/5] perf/core: Don't need event_filter_match in merge_sched_in() Date: Fri, 25 Mar 2022 11:53:16 +0800 Message-Id: <20220325035318.42168-4-zhouchengming@bytedance.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325035318.42168-1-zhouchengming@bytedance.com> References: <20220325035318.42168-1-zhouchengming@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" There is one obselete comment in perf_cgroup_switch(), since we don't use event_filter_match() when event_sched_out(). Then found we needn't to use event_filter_match() in merge_sched_in() too. Because now we use the perf_event groups RB-tree to get the exact matched perf_events, don't need to go through the event_filter_match() to check if matched again. Signed-off-by: Chengming Zhou --- kernel/events/core.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index dd985c77bc37..225d408deb1a 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -856,7 +856,8 @@ static void perf_cgroup_switch(struct task_struct *task= , int mode) cpu_ctx_sched_out(cpuctx, EVENT_ALL); /* * must not be done before ctxswout due - * to event_filter_match() in event_sched_out() + * to update_cgrp_time_from_cpuctx() in + * ctx_sched_out() */ cpuctx->cgrp =3D NULL; } @@ -3804,9 +3805,6 @@ static int merge_sched_in(struct perf_event *event, v= oid *data) if (event->state <=3D PERF_EVENT_STATE_OFF) return 0; =20 - if (!event_filter_match(event)) - return 0; - if (group_can_go_on(event, cpuctx, *can_add_hw)) { if (!group_sched_in(event, cpuctx, ctx)) list_add_tail(&event->active_list, get_event_list(event)); --=20 2.20.1 From nobody Sat Jun 20 01:02:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3AF1DC433F5 for ; Fri, 25 Mar 2022 03:54:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354760AbiCYD4F (ORCPT ); Thu, 24 Mar 2022 23:56:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356473AbiCYDz4 (ORCPT ); Thu, 24 Mar 2022 23:55:56 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C7753B00E for ; Thu, 24 Mar 2022 20:54:23 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id a16-20020a17090a6d9000b001c7d6c1bb13so2106269pjk.4 for ; Thu, 24 Mar 2022 20:54:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kViP0YZwCzQ2NYrGCdQJL04EDq6Hs7gB4dDqIa+z+FQ=; b=rZcHFNM9N5rPgY6bOsS1WHrzDFcD7H1ZlCh4SPwRhxG731//pxt+BUDFhy5yEpUDhQ nvnoBCHgvoXJ59QAXfS85SkaqNDlfYiHZXwbXCwAxUjby9PuA8G9t/1iOJJdvkHzE1wh n/iMCBA9F9NXzTrRMBCTq2nrTWvg8kwLFOXsv/zbHkV+mq3kEqWc15IpxNghfBhHprGB hMqVCVlFvarBfZoPReM7UXDaduqUXcxqyIWI7eK+GZyv9PCwyoWpdN37f7yxmhyVnCXF HbLlXKeAsmeLr/QgCRrE4zpsKgviyzxUoOuy8xTOiQeDivRSHqLgV8gpCt1wnWG1hDnb GHSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kViP0YZwCzQ2NYrGCdQJL04EDq6Hs7gB4dDqIa+z+FQ=; b=6VIVTOrWaa/PrekCHMPzvbfEaIl1xeFlOFDPIz7fdvqDIqljKwkAlhQp2OIQDcyzPE A/8Ab5qaI2twtYrAvC54vc2bnMBMFjGcxNVxRL69weHiBrCJ4jm5HG6AlurZJkrrrb/m G362X+hXrsSfkB/HiBuHnN1ewrCleq4XM5f9Bah2HY5sZZTi1GOB92nBAnSDY4y+eCLh UjCS8+RgXTOVOPkEfXp0MZ78TJQcfnncIpjFAqFfIQWD9tuH5ZWfvYDOagMp4mnp3/UF kGqlFZVx5G4R0unILl5+zudxzrr0yaPMkIdfN+uOwpnXSGnmwFINTXJfYKk4uOj5qiWi 18gA== X-Gm-Message-State: AOAM531r/RlLcl9dNN35+g2JHdmKZogl3s/Xw2ZbxqIIYDFAj2KKPkcP 1TpO1miOsFhs/EdmjkCuLpKQIQ== X-Google-Smtp-Source: ABdhPJzMRWeLJFCAC3h4QaPVxwge1LOeOKXDMd75mDE1kd1eFPN4J6V5RcwrytxdIvze+g3n2+zPBA== X-Received: by 2002:a17:90a:c595:b0:1c7:bb62:4487 with SMTP id l21-20020a17090ac59500b001c7bb624487mr7626491pjt.190.1648180462647; Thu, 24 Mar 2022 20:54:22 -0700 (PDT) Received: from C02CV1DAMD6P.bytedance.net ([2409:8a28:e6a:5860:acb7:c92:7e7f:45ef]) by smtp.gmail.com with ESMTPSA id u10-20020a17090a2b8a00b001c6594e5ddcsm4286504pjd.15.2022.03.24.20.54.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Mar 2022 20:54:22 -0700 (PDT) From: Chengming Zhou To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, eranian@google.com Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, Chengming Zhou Subject: [PATCH v3 4/5] perf/core: Fix perf_cgroup_switch() Date: Fri, 25 Mar 2022 11:53:17 +0800 Message-Id: <20220325035318.42168-5-zhouchengming@bytedance.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325035318.42168-1-zhouchengming@bytedance.com> References: <20220325035318.42168-1-zhouchengming@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" There is a race problem that can trigger WARN_ON_ONCE(cpuctx->cgrp) in perf_cgroup_switch(). CPU1 CPU2 perf_cgroup_sched_out(prev, next) cgrp1 =3D perf_cgroup_from_task(prev) cgrp2 =3D perf_cgroup_from_task(next) if (cgrp1 !=3D cgrp2) perf_cgroup_switch(prev, PERF_CGROUP_SWOUT) cgroup_migrate_execute() task->cgroups =3D ? perf_cgroup_attach() task_function_call(task, __perf_cgroup_move) perf_cgroup_sched_in(prev, next) cgrp1 =3D perf_cgroup_from_task(prev) cgrp2 =3D perf_cgroup_from_task(next) if (cgrp1 !=3D cgrp2) perf_cgroup_switch(next, PERF_CGROUP_SWIN) __perf_cgroup_move() perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN) The commit a8d757ef076f ("perf events: Fix slow and broken cgroup context switch code") want to skip perf_cgroup_switch() when the perf_cgroup of "prev" and "next" are the same. But task->cgroups can change in concurrent with context_switch() in cgroup_migrate_execute(). If cgrp1 =3D=3D cgrp2 in sched_out(), cpuctx won't do sched_out. Then task->cgroups changed cause cgrp1 !=3D cgrp2 in sched_in(), cpuctx will do sched_in. So trigger WARN_ON_ONCE(cpuctx->cgrp). Even though __perf_cgroup_move() will be synchronized as the context switch disables the interrupt, context_switch() still can see the task->cgroups is changing in the middle, since task->cgroups changed before sending IPI. So we have to combine perf_cgroup_sched_in() into perf_cgroup_sched_out(), unified into perf_cgroup_switch(), to fix the incosistency between perf_cgroup_sched_out() and perf_cgroup_sched_in(). But we can't just compare prev->cgroups with next->cgroups to decide whether to skip cpuctx sched_out/in since the prev->cgroups is changing too. For example: CPU1 CPU2 cgroup_migrate_execute() prev->cgroups =3D ? perf_cgroup_attach() task_function_call(task, __perf_cgroup_move) perf_cgroup_switch(task) cgrp1 =3D perf_cgroup_from_task(prev) cgrp2 =3D perf_cgroup_from_task(next) if (cgrp1 !=3D cgrp2) cpuctx sched_out/in ... task_function_call() will return -ESRCH In the above example, prev->cgroups changing cause (cgrp1 =3D=3D cgrp2) to be true, so skip cpuctx sched_out/in. And later task_function_call() would return -ESRCH since the prev task isn't running on cpu anymore. So we would leave perf_events of the old prev->cgroups still sched on the CPU, which is wrong. The solution is that we should use cpuctx->cgrp to compare with the next task's perf_cgroup. Since cpuctx->cgrp can only be changed on local CPU, and we have irq disabled, we can read cpuctx->cgrp to compare without holding ctx lock. Fixes: a8d757ef076f ("perf events: Fix slow and broken cgroup context switc= h code") Signed-off-by: Chengming Zhou --- kernel/events/core.c | 133 ++++++++----------------------------------- 1 file changed, 25 insertions(+), 108 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 225d408deb1a..d559fbbd3aaa 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -824,17 +824,12 @@ perf_cgroup_set_timestamp(struct perf_cpu_context *cp= uctx) =20 static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list); =20 -#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ -#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ - /* * reschedule events based on the cgroup constraint of task. - * - * mode SWOUT : schedule out everything - * mode SWIN : schedule in based on cgroup for next */ -static void perf_cgroup_switch(struct task_struct *task, int mode) +static void perf_cgroup_switch(struct task_struct *task) { + struct perf_cgroup *cgrp; struct perf_cpu_context *cpuctx, *tmp; struct list_head *list; unsigned long flags; @@ -845,36 +840,31 @@ static void perf_cgroup_switch(struct task_struct *ta= sk, int mode) */ local_irq_save(flags); =20 + cgrp =3D perf_cgroup_from_task(task, NULL); + list =3D this_cpu_ptr(&cgrp_cpuctx_list); list_for_each_entry_safe(cpuctx, tmp, list, cgrp_cpuctx_entry) { WARN_ON_ONCE(cpuctx->ctx.nr_cgroups =3D=3D 0); + if (READ_ONCE(cpuctx->cgrp) =3D=3D cgrp) + continue; =20 perf_ctx_lock(cpuctx, cpuctx->task_ctx); perf_pmu_disable(cpuctx->ctx.pmu); =20 - if (mode & PERF_CGROUP_SWOUT) { - cpu_ctx_sched_out(cpuctx, EVENT_ALL); - /* - * must not be done before ctxswout due - * to update_cgrp_time_from_cpuctx() in - * ctx_sched_out() - */ - cpuctx->cgrp =3D NULL; - } + cpu_ctx_sched_out(cpuctx, EVENT_ALL); + /* + * must not be done before ctxswout due + * to update_cgrp_time_from_cpuctx() in + * ctx_sched_out() + */ + cpuctx->cgrp =3D cgrp; + /* + * set cgrp before ctxsw in to allow + * perf_cgroup_set_timestamp() in ctx_sched_in() + * to not have to pass task around + */ + cpu_ctx_sched_in(cpuctx, EVENT_ALL); =20 - if (mode & PERF_CGROUP_SWIN) { - WARN_ON_ONCE(cpuctx->cgrp); - /* - * set cgrp before ctxsw in to allow - * perf_cgroup_set_timestamp() in ctx_sched_in() - * to not have to pass task around - * we pass the cpuctx->ctx to perf_cgroup_from_task() - * because cgorup events are only per-cpu - */ - cpuctx->cgrp =3D perf_cgroup_from_task(task, - &cpuctx->ctx); - cpu_ctx_sched_in(cpuctx, EVENT_ALL); - } perf_pmu_enable(cpuctx->ctx.pmu); perf_ctx_unlock(cpuctx, cpuctx->task_ctx); } @@ -882,58 +872,6 @@ static void perf_cgroup_switch(struct task_struct *tas= k, int mode) local_irq_restore(flags); } =20 -static inline void perf_cgroup_sched_out(struct task_struct *task, - struct task_struct *next) -{ - struct perf_cgroup *cgrp1; - struct perf_cgroup *cgrp2 =3D NULL; - - rcu_read_lock(); - /* - * we come here when we know perf_cgroup_events > 0 - * we do not need to pass the ctx here because we know - * we are holding the rcu lock - */ - cgrp1 =3D perf_cgroup_from_task(task, NULL); - cgrp2 =3D perf_cgroup_from_task(next, NULL); - - /* - * only schedule out current cgroup events if we know - * that we are switching to a different cgroup. Otherwise, - * do no touch the cgroup events. - */ - if (cgrp1 !=3D cgrp2) - perf_cgroup_switch(task, PERF_CGROUP_SWOUT); - - rcu_read_unlock(); -} - -static inline void perf_cgroup_sched_in(struct task_struct *prev, - struct task_struct *task) -{ - struct perf_cgroup *cgrp1; - struct perf_cgroup *cgrp2 =3D NULL; - - rcu_read_lock(); - /* - * we come here when we know perf_cgroup_events > 0 - * we do not need to pass the ctx here because we know - * we are holding the rcu lock - */ - cgrp1 =3D perf_cgroup_from_task(task, NULL); - cgrp2 =3D perf_cgroup_from_task(prev, NULL); - - /* - * only need to schedule in cgroup events if we are changing - * cgroup during ctxsw. Cgroup events were not scheduled - * out of ctxsw out if that was not the case. - */ - if (cgrp1 !=3D cgrp2) - perf_cgroup_switch(task, PERF_CGROUP_SWIN); - - rcu_read_unlock(); -} - static int perf_cgroup_ensure_storage(struct perf_event *event, struct cgroup_subsys_state *css) { @@ -1097,16 +1035,6 @@ static inline void update_cgrp_time_from_cpuctx(stru= ct perf_cpu_context *cpuctx, { } =20 -static inline void perf_cgroup_sched_out(struct task_struct *task, - struct task_struct *next) -{ -} - -static inline void perf_cgroup_sched_in(struct task_struct *prev, - struct task_struct *task) -{ -} - static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, struct perf_event_attr *attr, struct perf_event *group_leader) @@ -1119,11 +1047,6 @@ perf_cgroup_set_timestamp(struct perf_cpu_context *c= puctx) { } =20 -static inline void -perf_cgroup_switch(struct task_struct *task, struct task_struct *next) -{ -} - static inline u64 perf_cgroup_event_time(struct perf_event *event) { return 0; @@ -1143,6 +1066,10 @@ static inline void perf_cgroup_event_disable(struct perf_event *event, struct perf_event_cont= ext *ctx) { } + +static void perf_cgroup_switch(struct task_struct *task) +{ +} #endif =20 /* @@ -3662,7 +3589,7 @@ void __perf_event_task_sched_out(struct task_struct *= task, * cgroup event are system-wide mode only */ if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) - perf_cgroup_sched_out(task, next); + perf_cgroup_switch(next); } =20 /* @@ -3973,16 +3900,6 @@ void __perf_event_task_sched_in(struct task_struct *= prev, struct perf_event_context *ctx; int ctxn; =20 - /* - * If cgroup events exist on this CPU, then we need to check if we have - * to switch in PMU state; cgroup event are system-wide mode only. - * - * Since cgroup events are CPU events, we must schedule these in before - * we schedule in the task events. - */ - if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) - perf_cgroup_sched_in(prev, task); - for_each_task_context_nr(ctxn) { ctx =3D task->perf_event_ctxp[ctxn]; if (likely(!ctx)) @@ -13551,7 +13468,7 @@ static int __perf_cgroup_move(void *info) { struct task_struct *task =3D info; rcu_read_lock(); - perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); + perf_cgroup_switch(task); rcu_read_unlock(); return 0; } --=20 2.20.1 From nobody Sat Jun 20 01:02:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58EA3C433EF for ; Fri, 25 Mar 2022 03:54:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354136AbiCYD4N (ORCPT ); Thu, 24 Mar 2022 23:56:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356709AbiCYD4D (ORCPT ); Thu, 24 Mar 2022 23:56:03 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAAB13ED0B for ; Thu, 24 Mar 2022 20:54:29 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id s11so5467164pfu.13 for ; Thu, 24 Mar 2022 20:54:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZOjaONZKMKdSsuIYVocxhHlKftggb493wbtIiiPtVs0=; b=neNoe4SioYRiOAfumI2Qi6ZhxWfffkqMOShngq9XvSSMjo2sp4G17lYNkJXA1KtSYx Rb1KhAw8FXGu4Nno8BugqdMKDAxma23HIiMojIq8Ex3CSJ59l4JhpJeEmKAed3X5zcDO hJn+ZQ/qiOigxtZykRAKzOdTsjhhwP1G3lY+HdXIqHIcXxnpLv+ozSdXC1C8cj6r0QmH 6zjn8mb1IoT1icbos6NbDdabmN7tSK0u4NMIzPQuLw3fpjkUr42p4QfVqVtGuKrvtr6P hBNh8LpM8f+WCk2nD3kHd+KhTIo482bAzzpw/n5Rmt6IpYMsE3sOflgu6wlBPQ0qj+zi y82A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZOjaONZKMKdSsuIYVocxhHlKftggb493wbtIiiPtVs0=; b=FPFu7AWHxaDIMPmxFxFE22R/Y9mlg/bfY7SsnLaCQ2hL/OfMgCgpRuPaCY31iPNiO4 5yXX0OFBnG56e6kNLXBl4RCwQW9KX8xJh/Vl8sbXnAzx+gwKYQAVr926GWTALDKlvvUd 0SQ1uJ6yISswqf6RQ/ikvo00mALU4CITu/znIEbxsG+c8hlq42vySuax2+Bp5uvpc353 abJn4atI3TJzcLG/H5q93oF7dq1ckf3i+1pqhQmDi3UkzL04vyfIUu3N6XlfCcWk5kwu zcetBt+6555Lfx4uUw8to46vLJ/ahdRuBSdk2vniGenu1/JJbBoBZxA9unHiaU+G024e 0fdw== X-Gm-Message-State: AOAM531B5bPSVaV5A9XkK/TEG7DSjYhk12yEiEun91l9i2sUbutQkPzA yy7nuK1za9PYe8jRUXhZQmyNsw== X-Google-Smtp-Source: ABdhPJzgnlXnTMAAP50rmjk1RpE7Pq+oO0/kB529+we11ivjrAxlbhS4IWrReGZpq+lU0mKL4MF3Jg== X-Received: by 2002:aa7:8385:0:b0:4f6:ef47:e943 with SMTP id u5-20020aa78385000000b004f6ef47e943mr8184087pfm.38.1648180469209; Thu, 24 Mar 2022 20:54:29 -0700 (PDT) Received: from C02CV1DAMD6P.bytedance.net ([2409:8a28:e6a:5860:acb7:c92:7e7f:45ef]) by smtp.gmail.com with ESMTPSA id u10-20020a17090a2b8a00b001c6594e5ddcsm4286504pjd.15.2022.03.24.20.54.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Mar 2022 20:54:28 -0700 (PDT) From: Chengming Zhou To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, eranian@google.com Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, Chengming Zhou Subject: [PATCH v3 5/5] perf/core: Always set cpuctx cgrp when enable cgroup event Date: Fri, 25 Mar 2022 11:53:18 +0800 Message-Id: <20220325035318.42168-6-zhouchengming@bytedance.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220325035318.42168-1-zhouchengming@bytedance.com> References: <20220325035318.42168-1-zhouchengming@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When enable a cgroup event, cpuctx->cgrp setting is conditional on the current task cgrp matching the event's cgroup, so have to do it for every new event. It brings complexity but no advantage. To keep it simple, this patch would always set cpuctx->cgrp when enable the first cgroup event, and reset to NULL when disable the last cgroup event. Signed-off-by: Chengming Zhou --- kernel/events/core.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index d559fbbd3aaa..ccd4dfdf4810 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -967,22 +967,10 @@ perf_cgroup_event_enable(struct perf_event *event, st= ruct perf_event_context *ct */ cpuctx =3D container_of(ctx, struct perf_cpu_context, ctx); =20 - /* - * Since setting cpuctx->cgrp is conditional on the current @cgrp - * matching the event's cgroup, we must do this for every new event, - * because if the first would mismatch, the second would not try again - * and we would leave cpuctx->cgrp unset. - */ - if (ctx->is_active && !cpuctx->cgrp) { - struct perf_cgroup *cgrp =3D perf_cgroup_from_task(current, ctx); - - if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup)) - cpuctx->cgrp =3D cgrp; - } - if (ctx->nr_cgroups++) return; =20 + cpuctx->cgrp =3D perf_cgroup_from_task(current, ctx); list_add(&cpuctx->cgrp_cpuctx_entry, per_cpu_ptr(&cgrp_cpuctx_list, event->cpu)); } @@ -1004,9 +992,7 @@ perf_cgroup_event_disable(struct perf_event *event, st= ruct perf_event_context *c if (--ctx->nr_cgroups) return; =20 - if (ctx->is_active && cpuctx->cgrp) - cpuctx->cgrp =3D NULL; - + cpuctx->cgrp =3D NULL; list_del(&cpuctx->cgrp_cpuctx_entry); } =20 --=20 2.20.1