Skip to content

Commit dc86aba

Browse files
Chengming ZhouPeter Zijlstra
authored andcommitted
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 <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Johannes Weiner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 57899a6 commit dc86aba

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

include/linux/psi_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ struct psi_trigger {
151151
};
152152

153153
struct psi_group {
154+
struct psi_group *parent;
155+
154156
/* Protects data used by the aggregator */
155157
struct mutex avgs_lock;
156158

kernel/sched/psi.c

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -772,27 +772,12 @@ static void psi_group_change(struct psi_group *group, int cpu,
772772
schedule_delayed_work(&group->avgs_work, PSI_FREQ);
773773
}
774774

775-
static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
775+
static inline struct psi_group *task_psi_group(struct task_struct *task)
776776
{
777-
if (*iter == &psi_system)
778-
return NULL;
779-
780777
#ifdef CONFIG_CGROUPS
781-
if (static_branch_likely(&psi_cgroups_enabled)) {
782-
struct cgroup *cgroup = NULL;
783-
784-
if (!*iter)
785-
cgroup = task->cgroups->dfl_cgrp;
786-
else
787-
cgroup = cgroup_parent(*iter);
788-
789-
if (cgroup && cgroup_parent(cgroup)) {
790-
*iter = cgroup;
791-
return cgroup_psi(cgroup);
792-
}
793-
}
778+
if (static_branch_likely(&psi_cgroups_enabled))
779+
return cgroup_psi(task_dfl_cgroup(task));
794780
#endif
795-
*iter = &psi_system;
796781
return &psi_system;
797782
}
798783

@@ -815,7 +800,6 @@ void psi_task_change(struct task_struct *task, int clear, int set)
815800
{
816801
int cpu = task_cpu(task);
817802
struct psi_group *group;
818-
void *iter = NULL;
819803
u64 now;
820804

821805
if (!task->pid)
@@ -825,16 +809,17 @@ void psi_task_change(struct task_struct *task, int clear, int set)
825809

826810
now = cpu_clock(cpu);
827811

828-
while ((group = iterate_groups(task, &iter)))
812+
group = task_psi_group(task);
813+
do {
829814
psi_group_change(group, cpu, clear, set, now, true);
815+
} while ((group = group->parent));
830816
}
831817

832818
void psi_task_switch(struct task_struct *prev, struct task_struct *next,
833819
bool sleep)
834820
{
835821
struct psi_group *group, *common = NULL;
836822
int cpu = task_cpu(prev);
837-
void *iter;
838823
u64 now = cpu_clock(cpu);
839824

840825
if (next->pid) {
@@ -844,16 +829,16 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
844829
* ancestors with @prev, those will already have @prev's
845830
* TSK_ONCPU bit set, and we can stop the iteration there.
846831
*/
847-
iter = NULL;
848-
while ((group = iterate_groups(next, &iter))) {
832+
group = task_psi_group(next);
833+
do {
849834
if (per_cpu_ptr(group->pcpu, cpu)->state_mask &
850835
PSI_ONCPU) {
851836
common = group;
852837
break;
853838
}
854839

855840
psi_group_change(group, cpu, 0, TSK_ONCPU, now, true);
856-
}
841+
} while ((group = group->parent));
857842
}
858843

859844
if (prev->pid) {
@@ -886,9 +871,12 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
886871

887872
psi_flags_change(prev, clear, set);
888873

889-
iter = NULL;
890-
while ((group = iterate_groups(prev, &iter)) && group != common)
874+
group = task_psi_group(prev);
875+
do {
876+
if (group == common)
877+
break;
891878
psi_group_change(group, cpu, clear, set, now, wake_clock);
879+
} while ((group = group->parent));
892880

893881
/*
894882
* 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,
898886
*/
899887
if ((prev->psi_flags ^ next->psi_flags) & ~TSK_ONCPU) {
900888
clear &= ~TSK_ONCPU;
901-
for (; group; group = iterate_groups(prev, &iter))
889+
for (; group; group = group->parent)
902890
psi_group_change(group, cpu, clear, set, now, wake_clock);
903891
}
904892
}
@@ -908,7 +896,6 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
908896
void psi_account_irqtime(struct task_struct *task, u32 delta)
909897
{
910898
int cpu = task_cpu(task);
911-
void *iter = NULL;
912899
struct psi_group *group;
913900
struct psi_group_cpu *groupc;
914901
u64 now;
@@ -918,7 +905,8 @@ void psi_account_irqtime(struct task_struct *task, u32 delta)
918905

919906
now = cpu_clock(cpu);
920907

921-
while ((group = iterate_groups(task, &iter))) {
908+
group = task_psi_group(task);
909+
do {
922910
groupc = per_cpu_ptr(group->pcpu, cpu);
923911

924912
write_seqcount_begin(&groupc->seq);
@@ -930,7 +918,7 @@ void psi_account_irqtime(struct task_struct *task, u32 delta)
930918

931919
if (group->poll_states & (1 << PSI_IRQ_FULL))
932920
psi_schedule_poll_work(group, 1);
933-
}
921+
} while ((group = group->parent));
934922
}
935923
#endif
936924

@@ -1010,6 +998,7 @@ int psi_cgroup_alloc(struct cgroup *cgroup)
1010998
return -ENOMEM;
1011999
}
10121000
group_init(cgroup->psi);
1001+
cgroup->psi->parent = cgroup_psi(cgroup_parent(cgroup));
10131002
return 0;
10141003
}
10151004

0 commit comments

Comments
 (0)