Skip to contentSkip to conversation

cgroup: Simplify cgroup_disable loop given limited count

PATCH In review Zhen Ni 2 participants 2 messages Jul 30, 2025 cgroup

lore
Share & tools
lore

Zhen Ni

author
#0 lore raw

PATCH 1/1 cgroup: Simplify cgroup_disable loop given limited count

This patch refactors the cgroup_disable loop in cgroup_disable() to leverage the fact that OPT_FEATURE_COUNT can only be 0 or 1, making the loop structure unnecessary complex.

Key insights:
- OPT_FEATURE_COUNT is defined as an enum value that is either:
* 0 when CONFIG_PSI is disabled (no optional features)
* 1 when CONFIG_PSI is enabled (only "pressure" feature)
- Therefore, the loop will either:
* Run 0 times (when no features exist)
* Run exactly 1 time (when only one feature exists)

Performance considerations:
- The change has no performance impact since the loop runs at most once

---
 kernel/cgroup/cgroup.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/cgroup/cgroup.c +5 -5
index a723b7dc6e4e..71059b61b341 100644
@@ -6895,11 +6895,11 @@ static int __init cgroup_disable(char *str)
 		}
 
 		for (i = 0; i < OPT_FEATURE_COUNT; i++) {
-			if (strcmp(token, cgroup_opt_feature_names[i]))
-				continue;
-			cgroup_feature_disable_mask |= 1 << i;
-			pr_info("Disabling %s control group feature\n",
-				cgroup_opt_feature_names[i]);
+			if (!strcmp(token, cgroup_opt_feature_names[i])) {
+				cgroup_feature_disable_mask |= 1 << i;
+				pr_info("Disabling %s control group feature\n",
+					cgroup_opt_feature_names[i]);
+			}
 			break;
 		}
 	}
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 3 lines · Zhen Ni

This patch refactors the cgroup_disable loop in cgroup_disable() to leverage the fact that OPT_FEATURE_COUNT can only be 0 or 1, making the loop structure unnecessary complex.

(The complexity measure here is subjective.)
But this rework would make the parsing subtly broken when enum cgroup_opt_features is expandded in the future, it should be generic not crafted for a particular value.

Michal