Skip to contentSkip to conversation

lib: rewrite cgroup_find_ctrl with using for_each_ctrl

PATCH In review Li Wang 3 participants 5 messages Jan 13, 2022 – Jan 24, 2022 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 lib: rewrite cgroup_find_ctrl with using for_each_ctrl

It is safe to start from controllers[0] to traverse each of the controller whatever V2 or V1, then we can make use of it in the cgroup_find_ctrl() function.

---
 lib/tst_cgroup.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
lib/tst_cgroup.c +7 -8
index 2ef599d9e..10b65364b 100644
@@ -214,7 +214,7 @@ static const char *cgroup_v2_ltp_mount = "unified";
 #define for_each_v1_root(r)			\
 	for ((r) = roots + 1; (r)->ver; (r)++)
 #define for_each_ctrl(ctrl)			\
-	for ((ctrl) = controllers + 1; (ctrl)->ctrl_name; (ctrl)++)
+	for ((ctrl) = controllers; (ctrl)->ctrl_name; (ctrl)++)
 
 /* In all cases except one, this only loops once.
  *
@@ -325,15 +325,14 @@ void tst_cgroup_print_config(void)
 __attribute__ ((nonnull, warn_unused_result))
 static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name)
 {
-	struct cgroup_ctrl *ctrl = controllers;
-
-	while (ctrl->ctrl_name && strcmp(ctrl_name, ctrl->ctrl_name))
-		ctrl++;
+	struct cgroup_ctrl *ctrl;
 
-	if (!ctrl->ctrl_name)
-		ctrl = NULL;
+	for_each_ctrl(ctrl) {
+		if (!strcmp(ctrl_name, ctrl->ctrl_name))
+			return ctrl;
+	}
 
-	return ctrl;
+	return NULL;
 }
 
 /* Determine if a mounted cgroup hierarchy is unique and record it if so.
Signature · 5 lines
2.31.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw

Hello Li,

Quoted 3 lines · Li Wang

It is safe to start from controllers[0] to traverse each of the controller whatever V2 or V1, then we can make use of it in the cgroup_find_ctrl() function.

Right, it seems we never set ctrl_root on "cgroup" nor is it added to ctrl_field. So it will be skipped in other loops. This might not be what people expect, but I'm not sure what to do about that.

Quoted 31 lines
---
 lib/tst_cgroup.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
lib/tst_cgroup.c +2 -7
index 2ef599d9e..10b65364b 100644
@@ -214,7 +214,7 @@ static const char *cgroup_v2_ltp_mount = "unified";
 #define for_each_v1_root(r)			\
 	for ((r) = roots + 1; (r)->ver; (r)++)
 #define for_each_ctrl(ctrl)			\
-	for ((ctrl) = controllers + 1; (ctrl)->ctrl_name; (ctrl)++)
+	for ((ctrl) = controllers; (ctrl)->ctrl_name; (ctrl)++)
 
 /* In all cases except one, this only loops once.
  *
@@ -325,15 +325,14 @@ void tst_cgroup_print_config(void)
 __attribute__ ((nonnull, warn_unused_result))
 static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name)
 {
-	struct cgroup_ctrl *ctrl = controllers;
-
-	while (ctrl->ctrl_name && strcmp(ctrl_name, ctrl->ctrl_name))
-		ctrl++;
+	struct cgroup_ctrl *ctrl;
 
-	if (!ctrl->ctrl_name)
-		ctrl = NULL;

lib/tst_cgroup.c void tst_cgroup_print_config(void) · line 338

Quoted 10 lines
lib/tst_cgroup.c +5 -1
+	for_each_ctrl(ctrl) {
+		if (!strcmp(ctrl_name, ctrl->ctrl_name))
+			return ctrl;
+	}
 
-	return ctrl;
+	return NULL;
 }
 
 /* Determine if a mounted cgroup hierarchy is unique and record it if so.

Nice simplification!

Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#2 lore raw
Quoted 7 lines · Richard Palethorpe
Quoted 3 lines

It is safe to start from controllers[0] to traverse each of the controller whatever V2 or V1, then we can make use of it in the cgroup_find_ctrl() function.

Right, it seems we never set ctrl_root on "cgroup" nor is it added to ctrl_field. So it will be skipped in other loops. This might not be what people expect, but I'm not sure what to do about that.

Yes, but that's fine. It's because of the difference between V1 and V2.

Quoted 6 lines
Quoted 2 lines

/* Determine if a mounted cgroup hierarchy is unique and record it if so.

Nice simplification!

Thanks for the review, I will merge this after the new release.

Signature · 6 lines
Regards,
Li Wang


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#4 lore raw

Hi all,

Patch merged, thanks!

Quoted 9 lines · Petr Vorel

Hi Li,

LGTM.

Kind regards,
Petr

Signature · 6 lines
Regards,
Li Wang


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp