Skip to contentSkip to conversation

cgroup: make per-cgroup pressure stall tracking configurable

PATCH Superseded Suren Baghdasaryan 3 participants 10 messages May 13, 2021 – May 18, 2021

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Suren Baghdasaryan

author maintainer
#0 lore raw

PATCH 1/1 cgroup: make per-cgroup pressure stall tracking configurable

PSI accounts stalls for each cgroup separately and aggregates it at each level of the hierarchy. This causes additional overhead with psi_avgs_work being called for each cgroup in the hierarchy. psi_avgs_work has been highly optimized, however on systems with large number of cgroups the overhead becomes noticeable.
Systems which use PSI only at the system level could avoid this overhead if PSI can be configured to skip per-cgroup stall accounting. Add "cgroup_disable=pressure" kernel command-line option to allow requesting system-wide only pressure stall accounting. When set, it keeps system-wide accounting under /proc/pressure/ but skips accounting for individual cgroups and does not expose PSI nodes in cgroup hierarchy.

---
 .../admin-guide/kernel-parameters.txt         |  9 +++-
 include/linux/cgroup-defs.h                   |  1 +
 include/linux/cgroup.h                        |  7 +++
 kernel/cgroup/cgroup.c                        | 46 +++++++++++++++++++
 kernel/sched/psi.c                            |  8 +++-
 5 files changed, 67 insertions(+), 4 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +7 -2
index cb89dbdedc46..653c62142f07 100644
@@ -497,16 +497,21 @@
 	ccw_timeout_log	[S390]
 			See Documentation/s390/common_io.rst for details.
 
-	cgroup_disable=	[KNL] Disable a particular controller
-			Format: {name of the controller(s) to disable}
+	cgroup_disable=	[KNL] Disable a particular controller or optional feature
+			Format: {name of the controller(s) or feature(s) to disable}
 			The effects of cgroup_disable=foo are:
 			- foo isn't auto-mounted if you mount all cgroups in
 			  a single hierarchy
 			- foo isn't visible as an individually mountable
 			  subsystem
+			- if foo is an optional feature then the feature is
+			  disabled and corresponding cgroup files are not
+			  created
 			{Currently only "memory" controller deal with this and
 			cut the overhead, others just disable the usage. So
 			only cgroup_disable=memory is actually worthy}
+			Specifying "pressure" disables per-cgroup pressure
+			stall information accounting feature
 
 	cgroup_no_v1=	[KNL] Disable cgroup controllers and named hierarchies in v1
 			Format: { { controller | "all" | "named" }
include/linux/cgroup-defs.h +1 -0
index 559ee05f86b2..671f55cac0f0 100644
@@ -110,6 +110,7 @@ enum {
 	CFTYPE_NO_PREFIX	= (1 << 3),	/* (DON'T USE FOR NEW FILES) no subsys prefix */
 	CFTYPE_WORLD_WRITABLE	= (1 << 4),	/* (DON'T USE FOR NEW FILES) S_IWUGO */
 	CFTYPE_DEBUG		= (1 << 5),	/* create when cgroup_debug */
+	CFTYPE_PRESSURE		= (1 << 6),	/* only if pressure feature is enabled */
 
 	/* internal flags, do not use outside cgroup core proper */
 	__CFTYPE_ONLY_ON_DFL	= (1 << 16),	/* only on default hierarchy */
include/linux/cgroup.h +7 -0
index 4f2f79de083e..b929f589968b 100644
@@ -676,6 +676,8 @@ static inline struct psi_group *cgroup_psi(struct cgroup *cgrp)
 	return &cgrp->psi;
 }
 
+bool cgroup_psi_enabled(void);
+
 static inline void cgroup_init_kthreadd(void)
 {
 	/*
@@ -735,6 +737,11 @@ static inline struct psi_group *cgroup_psi(struct cgroup *cgrp)
 	return NULL;
 }
 
+static inline bool cgroup_psi_enabled(void)
+{
+	return false;
+}
+
 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
 					       struct cgroup *ancestor)
 {
kernel/cgroup/cgroup.c +46 -0

100 lines not on this page · raw message

kernel/sched/psi.c +6 -2
index cc25a3cff41f..c73efd7d4fba 100644
@@ -747,9 +747,12 @@ static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
 #ifdef CONFIG_CGROUPS
 	struct cgroup *cgroup = NULL;
 
-	if (!*iter)
+	if (!*iter) {
+		/* Skip to psi_system if per-cgroup accounting is disabled */
+		if (!cgroup_psi_enabled())
+			goto update_sys;
 		cgroup = task->cgroups->dfl_cgrp;
-	else if (*iter == &psi_system)
+	} else if (*iter == &psi_system)
 		return NULL;
 	else
 		cgroup = cgroup_parent(*iter);
@@ -758,6 +761,7 @@ static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
 		*iter = cgroup;
 		return cgroup_psi(cgroup);
 	}
+update_sys:
 #else
 	if (*iter)
 		return NULL;
Signature · 1 line
2.31.1.607.g51e8a6a459-goog
maintainer
#1 lore raw
Quoted 25 lines · Suren Baghdasaryan
kernel/cgroup/cgroup.c +4 -0
+bool cgroup_psi_enabled(void)
+{
+	return (cgroup_feature_disable_mask & (1 << OPT_FEATURE_PRESSURE)) == 0;
+}
kernel/sched/psi.c +5 -2
index cc25a3cff41f..c73efd7d4fba 100644
@@ -747,9 +747,12 @@ static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
 #ifdef CONFIG_CGROUPS
 	struct cgroup *cgroup = NULL;
 
-	if (!*iter)
+	if (!*iter) {
+		/* Skip to psi_system if per-cgroup accounting is disabled */
+		if (!cgroup_psi_enabled())
+			goto update_sys;
 		cgroup = task->cgroups->dfl_cgrp;
-	else if (*iter == &psi_system)
+	} else if (*iter == &psi_system)
 		return NULL;
 	else
 		cgroup = cgroup_parent(*iter);

kernel/sched/psi.c static struct psi_group *iterate_groups(struct task_struct *task, void **iter) · line 767

Quoted 7 lines
kernel/sched/psi.c +1 -0
@@ -758,6 +761,7 @@ static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
 		*iter = cgroup;
 		return cgroup_psi(cgroup);
 	}
+update_sys:
 #else
 	if (*iter)
 		return NULL;

I'm confused; shouldn't that do the same as that #else branch? Also, can you pretty please make cgroup_psi_enabled() a static_key ?

author maintainer
#2 lore raw
Quoted 29 lines · Peter Zijlstra
Quoted 26 lines · Suren Baghdasaryan
kernel/cgroup/cgroup.c +4 -0
+bool cgroup_psi_enabled(void)
+{
+     return (cgroup_feature_disable_mask & (1 << OPT_FEATURE_PRESSURE)) == 0;
+}
kernel/sched/psi.c +5 -2
index cc25a3cff41f..c73efd7d4fba 100644
@@ -747,9 +747,12 @@ static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
 #ifdef CONFIG_CGROUPS
      struct cgroup *cgroup = NULL;
 
-     if (!*iter)
+     if (!*iter) {
+             /* Skip to psi_system if per-cgroup accounting is disabled */
+             if (!cgroup_psi_enabled())
+                     goto update_sys;
              cgroup = task->cgroups->dfl_cgrp;
-     else if (*iter == &psi_system)
+     } else if (*iter == &psi_system)
              return NULL;
      else
              cgroup = cgroup_parent(*iter);
@@ -758,6 +761,7 @@ static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
              *iter = cgroup;
Quoted 8 lines

kernel/sched/psi.c static struct psi_group *iterate_groups(struct task_struct *task, void **iter) · line 767

Quoted 6 lines
kernel/sched/psi.c +1 -0
              return cgroup_psi(cgroup);
      }
+update_sys:
 #else
      if (*iter)
              return NULL;

I'm confused; shouldn't that do the same as that #else branch?

Correct, for this function CONFIG_CGROUPS=n and cgroup_disable=pressure are treated the same. True, from the code it's not very obvious. Do you have some refactoring in mind that would make it more explicit?

Quoted 1 line

Also, can you pretty please make cgroup_psi_enabled() a static_key ?

Certainly, will post an update on Monday.
Thanks for the feedback, Peter!

Quoted 4 lines

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]

maintainer
#3 lore raw
Quoted 4 lines · Suren Baghdasaryan

Correct, for this function CONFIG_CGROUPS=n and cgroup_disable=pressure are treated the same. True, from the code it's not very obvious. Do you have some refactoring in mind that would make it more explicit?

Does this make sense?

kernel/sched/psi.c +16 -14
@@ -744,24 +744,26 @@ static void psi_group_change(struct psi_
 
 static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
 {
+	if (cgroup_psi_enabled()) {
 #ifdef CONFIG_CGROUPS
-	struct cgroup *cgroup = NULL;
+		struct cgroup *cgroup = NULL;
 
-	if (!*iter)
-		cgroup = task->cgroups->dfl_cgrp;
-	else if (*iter == &psi_system)
-		return NULL;
-	else
-		cgroup = cgroup_parent(*iter);
+		if (!*iter)
+			cgroup = task->cgroups->dfl_cgrp;
+		else if (*iter == &psi_system)
+			return NULL;
+		else
+			cgroup = cgroup_parent(*iter);
 
-	if (cgroup && cgroup_parent(cgroup)) {
-		*iter = cgroup;
-		return cgroup_psi(cgroup);
-	}
-#else
-	if (*iter)
-		return NULL;
+		if (cgroup && cgroup_parent(cgroup)) {
+			*iter = cgroup;
+			return cgroup_psi(cgroup);
+		}
 #endif
+	} else {
+		if (*iter)
+			return NULL;
+	}
 	*iter = &psi_system;
 	return &psi_system;
 }
author maintainer
#4 lore raw
Quoted 43 lines · Peter Zijlstra
Quoted 4 lines · Suren Baghdasaryan

Correct, for this function CONFIG_CGROUPS=n and cgroup_disable=pressure are treated the same. True, from the code it's not very obvious. Do you have some refactoring in mind that would make it more explicit?

Does this make sense?

kernel/sched/psi.c +10 -14
@@ -744,24 +744,26 @@ static void psi_group_change(struct psi_
 
 static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
 {
+       if (cgroup_psi_enabled()) {
 #ifdef CONFIG_CGROUPS
-       struct cgroup *cgroup = NULL;
+               struct cgroup *cgroup = NULL;
 
-       if (!*iter)
-               cgroup = task->cgroups->dfl_cgrp;
-       else if (*iter == &psi_system)
-               return NULL;
-       else
-               cgroup = cgroup_parent(*iter);
+               if (!*iter)
+                       cgroup = task->cgroups->dfl_cgrp;
+               else if (*iter == &psi_system)
+                       return NULL;
+               else
+                       cgroup = cgroup_parent(*iter);
 
-       if (cgroup && cgroup_parent(cgroup)) {
-               *iter = cgroup;
-               return cgroup_psi(cgroup);
-       }
-#else
-       if (*iter)
-               return NULL;
+               if (cgroup && cgroup_parent(cgroup)) {
+                       *iter = cgroup;

kernel/sched/psi.c static void psi_group_change(struct psi_ · line 769

Quoted 10 lines
kernel/sched/psi.c +6 -0
+                       return cgroup_psi(cgroup);
+               }
 #endif
+       } else {
+               if (*iter)
+                       return NULL;
+       }
        *iter = &psi_system;
        return &psi_system;
 }

Hmm. Looks like the case when cgroup_psi_enabled()==true and CONFIG_CGROUPS=n would miss the "if (*iter) return NULL;" condition. Effectively with CONFIG_CGROUPS=n this becomes:

       if (cgroup_psi_enabled()) {           <== assume this is true
#ifdef CONFIG_CGROUPS                <== compiled out
#endif
       } else {
               if (*iter)                                  <== this
statement will never execute
                       return NULL;
       }
       *iter = &psi_system;
        return &psi_system;
Quoted 4 lines

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

author maintainer
#5 lore raw
Quoted 60 lines · Suren Baghdasaryan
Quoted 43 lines · Peter Zijlstra
Quoted 4 lines · Suren Baghdasaryan

Correct, for this function CONFIG_CGROUPS=n and cgroup_disable=pressure are treated the same. True, from the code it's not very obvious. Do you have some refactoring in mind that would make it more explicit?

Does this make sense?

kernel/sched/psi.c +10 -14
@@ -744,24 +744,26 @@ static void psi_group_change(struct psi_
 
 static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
 {
+       if (cgroup_psi_enabled()) {
 #ifdef CONFIG_CGROUPS
-       struct cgroup *cgroup = NULL;
+               struct cgroup *cgroup = NULL;
 
-       if (!*iter)
-               cgroup = task->cgroups->dfl_cgrp;
-       else if (*iter == &psi_system)
-               return NULL;
-       else
-               cgroup = cgroup_parent(*iter);
+               if (!*iter)
+                       cgroup = task->cgroups->dfl_cgrp;
+               else if (*iter == &psi_system)
+                       return NULL;
+               else
+                       cgroup = cgroup_parent(*iter);
 
-       if (cgroup && cgroup_parent(cgroup)) {
-               *iter = cgroup;
-               return cgroup_psi(cgroup);
-       }
-#else
-       if (*iter)
-               return NULL;
+               if (cgroup && cgroup_parent(cgroup)) {
+                       *iter = cgroup;

kernel/sched/psi.c static void psi_group_change(struct psi_ · line 769

Quoted 10 lines
kernel/sched/psi.c +6 -0
+                       return cgroup_psi(cgroup);
+               }
 #endif
+       } else {
+               if (*iter)
+                       return NULL;
+       }
        *iter = &psi_system;
        return &psi_system;
 }

Hmm. Looks like the case when cgroup_psi_enabled()==true and CONFIG_CGROUPS=n would miss the "if (*iter) return NULL;" condition. Effectively with CONFIG_CGROUPS=n this becomes:

Quoted 11 lines
       if (cgroup_psi_enabled()) {           <== assume this is true
#ifdef CONFIG_CGROUPS                <== compiled out
#endif
       } else {
               if (*iter)                                  <== this
statement will never execute
                       return NULL;
       }
       *iter = &psi_system;
        return &psi_system;

Ah, sorry. I forgot that CONFIG_CGROUPS=n would force cgroup_psi_enabled()==false (the way function is defined in cgroup.h), so (CONFIG_CGROUPS=n && cgroup_psi_enabled()==true) is an invalid configuration. I think adding a comment to your suggestion would make it more clear.
So your suggestion seems to work. I'll test it and include it in the next revision. Thanks!

Quoted 4 lines
Quoted 4 lines

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]

author maintainer
#6 lore raw
Quoted 74 lines · Suren Baghdasaryan
Quoted 60 lines · Suren Baghdasaryan
Quoted 43 lines · Peter Zijlstra
Quoted 4 lines · Suren Baghdasaryan

Correct, for this function CONFIG_CGROUPS=n and cgroup_disable=pressure are treated the same. True, from the code it's not very obvious. Do you have some refactoring in mind that would make it more explicit?

Does this make sense?

kernel/sched/psi.c +10 -14
@@ -744,24 +744,26 @@ static void psi_group_change(struct psi_
 
 static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
 {
+       if (cgroup_psi_enabled()) {
 #ifdef CONFIG_CGROUPS
-       struct cgroup *cgroup = NULL;
+               struct cgroup *cgroup = NULL;
 
-       if (!*iter)
-               cgroup = task->cgroups->dfl_cgrp;
-       else if (*iter == &psi_system)
-               return NULL;
-       else
-               cgroup = cgroup_parent(*iter);
+               if (!*iter)
+                       cgroup = task->cgroups->dfl_cgrp;
+               else if (*iter == &psi_system)
+                       return NULL;
+               else
+                       cgroup = cgroup_parent(*iter);
 
-       if (cgroup && cgroup_parent(cgroup)) {
-               *iter = cgroup;
-               return cgroup_psi(cgroup);
-       }
-#else
-       if (*iter)
-               return NULL;
+               if (cgroup && cgroup_parent(cgroup)) {
+                       *iter = cgroup;

kernel/sched/psi.c static void psi_group_change(struct psi_ · line 769

Quoted 10 lines
kernel/sched/psi.c +6 -0
+                       return cgroup_psi(cgroup);
+               }
 #endif
+       } else {
+               if (*iter)
+                       return NULL;
+       }
        *iter = &psi_system;
        return &psi_system;
 }

Hmm. Looks like the case when cgroup_psi_enabled()==true and CONFIG_CGROUPS=n would miss the "if (*iter) return NULL;" condition. Effectively with CONFIG_CGROUPS=n this becomes:

Quoted 11 lines
       if (cgroup_psi_enabled()) {           <== assume this is true
#ifdef CONFIG_CGROUPS                <== compiled out
#endif
       } else {
               if (*iter)                                  <== this
statement will never execute
                       return NULL;
       }
       *iter = &psi_system;
        return &psi_system;
Quoted 7 lines

Ah, sorry. I forgot that CONFIG_CGROUPS=n would force cgroup_psi_enabled()==false (the way function is defined in cgroup.h), so (CONFIG_CGROUPS=n && cgroup_psi_enabled()==true) is an invalid configuration. I think adding a comment to your suggestion would make it more clear.
So your suggestion seems to work. I'll test it and include it in the next revision. Thanks!

After reworking the code to add a static key I had to expand the #ifdef CONFIG_CGROUPS section, so I think a code refactoring below would make sense. It localizes config-specific code and it has the same exact code for CONFIG_CGROUPS=n and for cgroup_psi_enabled()==false. WDYT?:

kernel/sched/psi.c +3 -0
@@ -181,6 +181,7 @@ struct psi_group psi_system = {
 };
 
 static void psi_avgs_work(struct work_struct *work);
+static void cgroup_iterator_init(void);
 
 static void group_init(struct psi_group *group)
 {
@@ -211,6 +212,8 @@ void __init psi_init(void)
                 return;
         }
 
+        cgroup_iterator_init();
+
         psi_period = jiffies_to_nsecs(PSI_FREQ);
         group_init(&psi_system);
 }
@@ -742,11 +745,31 @@ 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 *sys_group_iterator(struct task_struct *task,
+ void **iter)
{
+ *iter = &psi_system;
+ return &psi_system;
+}
+
#ifdef CONFIG_CGROUPS
+
+DEFINE_STATIC_KEY_FALSE(psi_cgroups_disabled);
+
+static void cgroup_iterator_init(void)
+{
+ if (!cgroup_psi_enabled())
+ static_branch_enable(&psi_cgroups_disabled);
+}
+
+static struct psi_group *iterate_groups(struct task_struct *task, void **iter)

+{
         struct cgroup *cgroup = NULL;

+ /* Skip to psi_system if per-cgroup accounting is disabled */
+ if (static_branch_unlikely(&psi_cgroups_disabled))
+ return *iter ? NULL : sys_group_iterator(task, iter);
+

         if (!*iter)
                 cgroup = task->cgroups->dfl_cgrp;
         else if (*iter == &psi_system)
@@ -758,14 +781,20 @@ static struct psi_group *iterate_groups(struct
task_struct *task, void **iter)
                 *iter = cgroup;
                 return cgroup_psi(cgroup);
         }

-#else
- if (*iter)
- return NULL;
-#endif
- *iter = &psi_system;
- return &psi_system;
+
+ return sys_group_iterator(task, iter);
}

+#else /* CONFIG_CGROUPS */
+static inline void cgroup_iterator_init(void) {}
+
+static struct psi_group *iterate_groups(struct task_struct *task, void **iter) +{
+ return *iter ? NULL : sys_group_iterator(task, iter);
+}
+
+#endif /* CONFIG_CGROUPS */
+

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]

maintainer
#7 lore raw
Quoted 48 lines · Suren Baghdasaryan

After reworking the code to add a static key I had to expand the #ifdef CONFIG_CGROUPS section, so I think a code refactoring below would make sense. It localizes config-specific code and it has the same exact code for CONFIG_CGROUPS=n and for cgroup_psi_enabled()==false. WDYT?:

kernel/sched/psi.c +18 -1
@@ -181,6 +181,7 @@ struct psi_group psi_system = {
 };
 
 static void psi_avgs_work(struct work_struct *work);
+static void cgroup_iterator_init(void);
 
 static void group_init(struct psi_group *group)
 {
@@ -211,6 +212,8 @@ void __init psi_init(void)
                 return;
         }
 
+        cgroup_iterator_init();
+
         psi_period = jiffies_to_nsecs(PSI_FREQ);
         group_init(&psi_system);
 }
@@ -742,11 +745,31 @@ 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 *sys_group_iterator(struct task_struct *task,
+                                                   void **iter)
 {
+        *iter = &psi_system;
+        return &psi_system;
+}
+
 #ifdef CONFIG_CGROUPS
+
+DEFINE_STATIC_KEY_FALSE(psi_cgroups_disabled);
+
+static void cgroup_iterator_init(void)
+{
+        if (!cgroup_psi_enabled())
+                static_branch_enable(&psi_cgroups_disabled);
+}
+

kernel/sched/psi.c static void psi_group_change(struct psi_group · line 775

Quoted 10 lines
kernel/sched/psi.c +6 -0
+static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
+{
         struct cgroup *cgroup = NULL;
 
+        /* Skip to psi_system if per-cgroup accounting is disabled */
+        if (static_branch_unlikely(&psi_cgroups_disabled))
+                return *iter ? NULL : sys_group_iterator(task, iter);
+
         if (!*iter)
                 cgroup = task->cgroups->dfl_cgrp;

That looks over-engineered. You have to check iter whether cgroups are enabled or not. Pulling the jump label check up doesn't save anything, but it ends up duplicating code.

What you had in the beginning was better, it just had the system label in an unexpected place where it would check iter twice in a row.

The (*iter == &psi_system) check inside the cgroups branch has the same purpose as the (*iter) check in the else branch. We could consolidate that by pulling it up front.

If we wrap the entire cgroup iteration block into the static branch, IMO it becomes a bit clearer as well.

How about this?

static struct psi_group *iterate_groups(struct task_struct *task, void **iter)

{
	if (*iter == &psi_system)
		return NULL;
#ifdef CONFIG_CGROUPS
	if (!static_branch_likely(&psi_cgroups_disabled)) {
		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);
		}
	}

#endif

	*iter = &psi_system;
	return &psi_system;
}
author maintainer
#8 lore raw
Quoted 96 lines · Johannes Weiner
Quoted 48 lines · Suren Baghdasaryan

After reworking the code to add a static key I had to expand the #ifdef CONFIG_CGROUPS section, so I think a code refactoring below would make sense. It localizes config-specific code and it has the same exact code for CONFIG_CGROUPS=n and for cgroup_psi_enabled()==false. WDYT?:

kernel/sched/psi.c +18 -1
@@ -181,6 +181,7 @@ struct psi_group psi_system = {
 };
 
 static void psi_avgs_work(struct work_struct *work);
+static void cgroup_iterator_init(void);
 
 static void group_init(struct psi_group *group)
 {
@@ -211,6 +212,8 @@ void __init psi_init(void)
                 return;
         }
 
+        cgroup_iterator_init();
+
         psi_period = jiffies_to_nsecs(PSI_FREQ);
         group_init(&psi_system);
 }
@@ -742,11 +745,31 @@ 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 *sys_group_iterator(struct task_struct *task,
+                                                   void **iter)
 {
+        *iter = &psi_system;
+        return &psi_system;
+}
+
 #ifdef CONFIG_CGROUPS
+
+DEFINE_STATIC_KEY_FALSE(psi_cgroups_disabled);
+
+static void cgroup_iterator_init(void)
+{
+        if (!cgroup_psi_enabled())
+                static_branch_enable(&psi_cgroups_disabled);
+}
+

kernel/sched/psi.c static void psi_group_change(struct psi_group · line 775

Quoted 10 lines
kernel/sched/psi.c +6 -0
+static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
+{
         struct cgroup *cgroup = NULL;
 
+        /* Skip to psi_system if per-cgroup accounting is disabled */
+        if (static_branch_unlikely(&psi_cgroups_disabled))
+                return *iter ? NULL : sys_group_iterator(task, iter);
+
         if (!*iter)
                 cgroup = task->cgroups->dfl_cgrp;

That looks over-engineered. You have to check iter whether cgroups are enabled or not. Pulling the jump label check up doesn't save anything, but it ends up duplicating code.

What you had in the beginning was better, it just had the system label in an unexpected place where it would check iter twice in a row.

The (*iter == &psi_system) check inside the cgroups branch has the same purpose as the (*iter) check in the else branch. We could consolidate that by pulling it up front.

If we wrap the entire cgroup iteration block into the static branch, IMO it becomes a bit clearer as well.

How about this?

static struct psi_group *iterate_groups(struct task_struct *task, void **iter)

{
        if (*iter == &psi_system)
                return NULL;
#ifdef CONFIG_CGROUPS
        if (!static_branch_likely(&psi_cgroups_disabled)) {
                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);
                }
        }
Quoted 5 lines

#endif

        *iter = &psi_system;
        return &psi_system;
}

This looks great to me. Will use it in the next version. Thanks!

author maintainer
#9 lore raw
Quoted 100 lines · Suren Baghdasaryan
Quoted 98 lines · Johannes Weiner
Quoted 48 lines · Suren Baghdasaryan

After reworking the code to add a static key I had to expand the #ifdef CONFIG_CGROUPS section, so I think a code refactoring below would make sense. It localizes config-specific code and it has the same exact code for CONFIG_CGROUPS=n and for cgroup_psi_enabled()==false. WDYT?:

kernel/sched/psi.c +18 -1
@@ -181,6 +181,7 @@ struct psi_group psi_system = {
 };
 
 static void psi_avgs_work(struct work_struct *work);
+static void cgroup_iterator_init(void);
 
 static void group_init(struct psi_group *group)
 {
@@ -211,6 +212,8 @@ void __init psi_init(void)
                 return;
         }
 
+        cgroup_iterator_init();
+
         psi_period = jiffies_to_nsecs(PSI_FREQ);
         group_init(&psi_system);
 }
@@ -742,11 +745,31 @@ 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 *sys_group_iterator(struct task_struct *task,
+                                                   void **iter)
 {
+        *iter = &psi_system;
+        return &psi_system;
+}
+
 #ifdef CONFIG_CGROUPS
+
+DEFINE_STATIC_KEY_FALSE(psi_cgroups_disabled);
+
+static void cgroup_iterator_init(void)
+{
+        if (!cgroup_psi_enabled())
+                static_branch_enable(&psi_cgroups_disabled);
+}
+

kernel/sched/psi.c static void psi_group_change(struct psi_group · line 775

Quoted 10 lines
kernel/sched/psi.c +6 -0
+static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
+{
         struct cgroup *cgroup = NULL;
 
+        /* Skip to psi_system if per-cgroup accounting is disabled */
+        if (static_branch_unlikely(&psi_cgroups_disabled))
+                return *iter ? NULL : sys_group_iterator(task, iter);
+
         if (!*iter)
                 cgroup = task->cgroups->dfl_cgrp;

That looks over-engineered. You have to check iter whether cgroups are enabled or not. Pulling the jump label check up doesn't save anything, but it ends up duplicating code.

What you had in the beginning was better, it just had the system label in an unexpected place where it would check iter twice in a row.

The (*iter == &psi_system) check inside the cgroups branch has the same purpose as the (*iter) check in the else branch. We could consolidate that by pulling it up front.

If we wrap the entire cgroup iteration block into the static branch, IMO it becomes a bit clearer as well.

How about this?

static struct psi_group *iterate_groups(struct task_struct *task, void **iter)

{
        if (*iter == &psi_system)
                return NULL;
#ifdef CONFIG_CGROUPS
        if (!static_branch_likely(&psi_cgroups_disabled)) {
                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);
                }
        }

#endif

Quoted 5 lines
Quoted 3 lines
        *iter = &psi_system;
        return &psi_system;
}

This looks great to me. Will use it in the next version. Thanks!

V2 is posted at https://lore.kernel.org/patchwork/patch/1430980