From nobody Wed Apr 8 12:49:43 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 2773EECAAD3 for ; Fri, 9 Sep 2022 14:00:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231881AbiIIOAV (ORCPT ); Fri, 9 Sep 2022 10:00:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231500AbiIIOAP (ORCPT ); Fri, 9 Sep 2022 10:00:15 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03EB95850E; Fri, 9 Sep 2022 07:00:13 -0700 (PDT) Date: Fri, 09 Sep 2022 14:00:10 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1662732011; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jpwUt5u4AO6w3nB/29AuJABWq0skjkMOVZM9Gtwg/do=; b=k23ZDNzcdscRIJf2s1FbeDYqLvzAVGNIXI4jc847W1T1vg3d3B31bUah+C7veT/Eac8dHt FwTTPQA9eGWhpDf/R0+gjgEcgY3YatURgYpdldHUp9ejEpfo4Q6Fe+xupHP6n+ArOi23u8 Nvqzro5M0i3WPUX6Jg/3SHFr8Qz/HimWvow9MW0G5IWN6lhh9KL8d7trHC5HvyGRVmSJg0 KwS1Ao6lH0i4m72vjEvTN2am0djWID8Xx0rVL0CuIhyDi3btCcPoKZTCuXneJKVZMOHQQF zB/I+5XtqXBb65A3kbdaYL7XEsIw2MnYHo73ywfOHugLMJBX+vB9Rzxiv4nMJQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1662732011; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jpwUt5u4AO6w3nB/29AuJABWq0skjkMOVZM9Gtwg/do=; b=YUgGEQmlnL9ARMIko3jwtsZbadd7sTvskxtjx06JXB9e6YmJ2/0RJXDnj0vAamvKQXOqpz MUW0GEgxI06bIMAQ== From: "tip-bot2 for Chengming Zhou" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/psi] sched/psi: Cache parent psi_group to speed up group iteration Cc: Chengming Zhou , "Peter Zijlstra (Intel)" , Johannes Weiner , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220825164111.29534-10-zhouchengming@bytedance.com> References: <20220825164111.29534-10-zhouchengming@bytedance.com> MIME-Version: 1.0 Message-ID: <166273201036.401.1409189625760099103.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the sched/psi branch of tip: Commit-ID: dc86aba751e2867244411adda1562f6664747019 Gitweb: https://git.kernel.org/tip/dc86aba751e2867244411adda1562f666= 4747019 Author: Chengming Zhou AuthorDate: Fri, 26 Aug 2022 00:41:10 +08:00 Committer: Peter Zijlstra CommitterDate: Fri, 09 Sep 2022 11:08:33 +02:00 sched/psi: Cache parent psi_group to speed up group iteration We use iterate_groups() to iterate each level psi_group to update PSI stats, which is a very hot path. In current code, iterate_groups() have to use multiple branches and cgroup_parent() to get parent psi_group for each level, which is not very efficient. This patch cache parent psi_group in struct psi_group, only need to get psi_group of task itself first, then just use group->parent to iterate. Signed-off-by: Chengming Zhou Signed-off-by: Peter Zijlstra (Intel) Acked-by: Johannes Weiner Link: https://lore.kernel.org/r/20220825164111.29534-10-zhouchengming@byted= ance.com --- include/linux/psi_types.h | 2 ++- kernel/sched/psi.c | 49 ++++++++++++++------------------------ 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/include/linux/psi_types.h b/include/linux/psi_types.h index 40c2817..a0b7462 100644 --- a/include/linux/psi_types.h +++ b/include/linux/psi_types.h @@ -151,6 +151,8 @@ struct psi_trigger { }; =20 struct psi_group { + struct psi_group *parent; + /* Protects data used by the aggregator */ struct mutex avgs_lock; =20 diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c index 2545a78..9a8aee8 100644 --- a/kernel/sched/psi.c +++ b/kernel/sched/psi.c @@ -772,27 +772,12 @@ static void psi_group_change(struct psi_group *group,= int cpu, schedule_delayed_work(&group->avgs_work, PSI_FREQ); } =20 -static struct psi_group *iterate_groups(struct task_struct *task, void **i= ter) +static inline struct psi_group *task_psi_group(struct task_struct *task) { - if (*iter =3D=3D &psi_system) - return NULL; - #ifdef CONFIG_CGROUPS - if (static_branch_likely(&psi_cgroups_enabled)) { - struct cgroup *cgroup =3D NULL; - - if (!*iter) - cgroup =3D task->cgroups->dfl_cgrp; - else - cgroup =3D cgroup_parent(*iter); - - if (cgroup && cgroup_parent(cgroup)) { - *iter =3D cgroup; - return cgroup_psi(cgroup); - } - } + if (static_branch_likely(&psi_cgroups_enabled)) + return cgroup_psi(task_dfl_cgroup(task)); #endif - *iter =3D &psi_system; return &psi_system; } =20 @@ -815,7 +800,6 @@ void psi_task_change(struct task_struct *task, int clea= r, int set) { int cpu =3D task_cpu(task); struct psi_group *group; - void *iter =3D NULL; u64 now; =20 if (!task->pid) @@ -825,8 +809,10 @@ void psi_task_change(struct task_struct *task, int cle= ar, int set) =20 now =3D cpu_clock(cpu); =20 - while ((group =3D iterate_groups(task, &iter))) + group =3D task_psi_group(task); + do { psi_group_change(group, cpu, clear, set, now, true); + } while ((group =3D group->parent)); } =20 void psi_task_switch(struct task_struct *prev, struct task_struct *next, @@ -834,7 +820,6 @@ void psi_task_switch(struct task_struct *prev, struct t= ask_struct *next, { struct psi_group *group, *common =3D NULL; int cpu =3D task_cpu(prev); - void *iter; u64 now =3D cpu_clock(cpu); =20 if (next->pid) { @@ -844,8 +829,8 @@ void psi_task_switch(struct task_struct *prev, struct t= ask_struct *next, * ancestors with @prev, those will already have @prev's * TSK_ONCPU bit set, and we can stop the iteration there. */ - iter =3D NULL; - while ((group =3D iterate_groups(next, &iter))) { + group =3D task_psi_group(next); + do { if (per_cpu_ptr(group->pcpu, cpu)->state_mask & PSI_ONCPU) { common =3D group; @@ -853,7 +838,7 @@ void psi_task_switch(struct task_struct *prev, struct t= ask_struct *next, } =20 psi_group_change(group, cpu, 0, TSK_ONCPU, now, true); - } + } while ((group =3D group->parent)); } =20 if (prev->pid) { @@ -886,9 +871,12 @@ void psi_task_switch(struct task_struct *prev, struct = task_struct *next, =20 psi_flags_change(prev, clear, set); =20 - iter =3D NULL; - while ((group =3D iterate_groups(prev, &iter)) && group !=3D common) + group =3D task_psi_group(prev); + do { + if (group =3D=3D common) + break; psi_group_change(group, cpu, clear, set, now, wake_clock); + } while ((group =3D group->parent)); =20 /* * TSK_ONCPU is handled up to the common ancestor. If there are @@ -898,7 +886,7 @@ void psi_task_switch(struct task_struct *prev, struct t= ask_struct *next, */ if ((prev->psi_flags ^ next->psi_flags) & ~TSK_ONCPU) { clear &=3D ~TSK_ONCPU; - for (; group; group =3D iterate_groups(prev, &iter)) + for (; group; group =3D group->parent) psi_group_change(group, cpu, clear, set, now, wake_clock); } } @@ -908,7 +896,6 @@ void psi_task_switch(struct task_struct *prev, struct t= ask_struct *next, void psi_account_irqtime(struct task_struct *task, u32 delta) { int cpu =3D task_cpu(task); - void *iter =3D NULL; struct psi_group *group; struct psi_group_cpu *groupc; u64 now; @@ -918,7 +905,8 @@ void psi_account_irqtime(struct task_struct *task, u32 = delta) =20 now =3D cpu_clock(cpu); =20 - while ((group =3D iterate_groups(task, &iter))) { + group =3D task_psi_group(task); + do { groupc =3D per_cpu_ptr(group->pcpu, cpu); =20 write_seqcount_begin(&groupc->seq); @@ -930,7 +918,7 @@ void psi_account_irqtime(struct task_struct *task, u32 = delta) =20 if (group->poll_states & (1 << PSI_IRQ_FULL)) psi_schedule_poll_work(group, 1); - } + } while ((group =3D group->parent)); } #endif =20 @@ -1010,6 +998,7 @@ int psi_cgroup_alloc(struct cgroup *cgroup) return -ENOMEM; } group_init(cgroup->psi); + cgroup->psi->parent =3D cgroup_psi(cgroup_parent(cgroup)); return 0; }