Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp2015629rwn; Fri, 9 Sep 2022 07:25:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR6nxKXjTN60jtY4J28wQluTpbPA0l+LTZCJP+vQxBuvIFpJkeFm+MQcOgusrBKnIUVuDTcl X-Received: by 2002:a17:902:f782:b0:173:1206:cee0 with SMTP id q2-20020a170902f78200b001731206cee0mr13738157pln.130.1662733536794; Fri, 09 Sep 2022 07:25:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662733536; cv=none; d=google.com; s=arc-20160816; b=U/IL+ns+PMile2EZJslmjQ03rIyz24tD9lHtyLpMlehUL42r1IbsRoLGu7gjSSWO1e T3MEsLZbOeOQUUqUqUtm0+0oBisvh/g/NM9AVMDNI/zWq8327/Q2mfTd6ZuxWgxowKKW UxE4l0YqIHVKlt0skXxh3LskZ/wgjhQO5RW/drBAK3GCdx3Rfy4BV/o+c4YWmlZtt8mx lSIU53ikXBoe9UgokOqDb5S90YCo1SYyTUWd0KQFRU3BR+Ktn3B4KLeIfRipTCRo2kD/ 8GBJ2PO4iYKTA68xqwWEvFhMn1jgtzmKpl4yGmAxgZaXFs34Czva+TZVSVqPTKS71lMB IdhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=jpwUt5u4AO6w3nB/29AuJABWq0skjkMOVZM9Gtwg/do=; b=Vuo4EaGjg0IkU9WV+IU66WsA1kSqps8VyDwkHzZ8xEz6ozQxUqtZHng8uag3zgSLje PwzQqM/RIWq098q2rWHRS12plsp5x39rmapULJ5cbV3m9AxdlI77++ANhLYy+0RGuYrW F6cEcZh5BWecFsqRtn1/6wxIHsPRofVqJBnZz82mIjA1koUU7mdAzVSton+/el5dumRA CV+sp6pdEkEK20d47mqvhTR7QeJVB6LmvFz/J6oPnBnCfx6zWmeHTgEhf6LB6Qp4MciJ ePIkf9eLrjU2Z06MSCqE7Ht5EffuKj/ZOwXMYHCs7petsXN09j1wdQK5+1TQz2SoRjA8 zqsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=k23ZDNzc; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=YUgGEQml; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o11-20020a635d4b000000b0042b828bf15bsi646386pgm.287.2022.09.09.07.25.22; Fri, 09 Sep 2022 07:25:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=k23ZDNzc; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=YUgGEQml; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231833AbiIIOAU (ORCPT + 99 others); Fri, 9 Sep 2022 10:00:20 -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: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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/dc86aba751e2867244411adda1562f6664747019 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@bytedance.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 { }; struct psi_group { + struct psi_group *parent; + /* Protects data used by the aggregator */ struct mutex avgs_lock; 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); } -static struct psi_group *iterate_groups(struct task_struct *task, void **iter) +static inline struct psi_group *task_psi_group(struct task_struct *task) { - if (*iter == &psi_system) - return NULL; - #ifdef CONFIG_CGROUPS - if (static_branch_likely(&psi_cgroups_enabled)) { - struct cgroup *cgroup = NULL; - - if (!*iter) - cgroup = task->cgroups->dfl_cgrp; - else - cgroup = cgroup_parent(*iter); - - if (cgroup && cgroup_parent(cgroup)) { - *iter = cgroup; - return cgroup_psi(cgroup); - } - } + if (static_branch_likely(&psi_cgroups_enabled)) + return cgroup_psi(task_dfl_cgroup(task)); #endif - *iter = &psi_system; return &psi_system; } @@ -815,7 +800,6 @@ void psi_task_change(struct task_struct *task, int clear, int set) { int cpu = task_cpu(task); struct psi_group *group; - void *iter = NULL; u64 now; if (!task->pid) @@ -825,8 +809,10 @@ void psi_task_change(struct task_struct *task, int clear, int set) now = cpu_clock(cpu); - while ((group = iterate_groups(task, &iter))) + group = task_psi_group(task); + do { psi_group_change(group, cpu, clear, set, now, true); + } while ((group = group->parent)); } 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 task_struct *next, { struct psi_group *group, *common = NULL; int cpu = task_cpu(prev); - void *iter; u64 now = cpu_clock(cpu); if (next->pid) { @@ -844,8 +829,8 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next, * ancestors with @prev, those will already have @prev's * TSK_ONCPU bit set, and we can stop the iteration there. */ - iter = NULL; - while ((group = iterate_groups(next, &iter))) { + group = task_psi_group(next); + do { if (per_cpu_ptr(group->pcpu, cpu)->state_mask & PSI_ONCPU) { common = group; @@ -853,7 +838,7 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next, } psi_group_change(group, cpu, 0, TSK_ONCPU, now, true); - } + } while ((group = group->parent)); } if (prev->pid) { @@ -886,9 +871,12 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next, psi_flags_change(prev, clear, set); - iter = NULL; - while ((group = iterate_groups(prev, &iter)) && group != common) + group = task_psi_group(prev); + do { + if (group == common) + break; psi_group_change(group, cpu, clear, set, now, wake_clock); + } while ((group = group->parent)); /* * 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 task_struct *next, */ if ((prev->psi_flags ^ next->psi_flags) & ~TSK_ONCPU) { clear &= ~TSK_ONCPU; - for (; group; group = iterate_groups(prev, &iter)) + for (; group; group = 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 task_struct *next, void psi_account_irqtime(struct task_struct *task, u32 delta) { int cpu = task_cpu(task); - void *iter = 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) now = cpu_clock(cpu); - while ((group = iterate_groups(task, &iter))) { + group = task_psi_group(task); + do { groupc = per_cpu_ptr(group->pcpu, cpu); write_seqcount_begin(&groupc->seq); @@ -930,7 +918,7 @@ void psi_account_irqtime(struct task_struct *task, u32 delta) if (group->poll_states & (1 << PSI_IRQ_FULL)) psi_schedule_poll_work(group, 1); - } + } while ((group = group->parent)); } #endif @@ -1010,6 +998,7 @@ int psi_cgroup_alloc(struct cgroup *cgroup) return -ENOMEM; } group_init(cgroup->psi); + cgroup->psi->parent = cgroup_psi(cgroup_parent(cgroup)); return 0; }