Skip to contentSkip to conversation

cgroup: Handle trailing new line in cgroup.controllers

PATCH In review Richard Palethorpe via ltp 4 participants 5 messages Oct 26, 2023 – Oct 27, 2023 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cgroup: Handle trailing new line in cgroup.controllers

The last item in cgroup.controllers (misc or rdma in my case) contained a new line character which caused the controller search to fail.

This commit avoids including the newline character inside the name comparison.

The search failure caused the "cgroup_regression_test.sh" test to fail with a confusing error when it tries to mount a V1 subsys thus removing the V2 and causing the available set of V2s to change between scans.

According to the V2 docs subsys names can only include lowercase characters and '_'. So we strictly look for those characters.

The newline (and delimiting space) is just what the kernel currently prints. IDK if it is specified anywhere, but if it changes then the error should be obvious.

Fixes 310da3784 ("Add new CGroups APIs")
Cc Petr Vorel <[email protected]>
Cc Marius Kittler <[email protected]>

---

V2:
* Add underscore
* Add length check
* Expand commit message
* Use shorter syntax

 lib/tst_cgroup.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/tst_cgroup.c +12 -1
index 5240aadaa..c5cb20505 100644
@@ -433,9 +433,20 @@ __attribute__ ((nonnull, warn_unused_result))
 static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name)
 {
 	struct cgroup_ctrl *ctrl;
+	int l = 0;
+	char c = ctrl_name[l];
+
+	while (c == '_' || (c >= 'a' && c <= 'z'))
+		c = ctrl_name[++l];
+
+	if (l > 32)
+		tst_res(TWARN, "Subsys name len greater than max known value of MAX_CGROUP_TYPE_NAMELEN: %d > 32", l);
+
+	if (!(c == '\n' || c == '\0'))
+		tst_brk(TBROK, "Unexpected char in %s: %c", ctrl_name, c);
 
 	for_each_ctrl(ctrl) {
-		if (!strcmp(ctrl_name, ctrl->ctrl_name))
+		if (!strncmp(ctrl_name, ctrl->ctrl_name, l))
 			return ctrl;
 	}
 
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 1/1
#2 lore raw
In reply to Richard Palethorpe via ltp

Hi Richie,

Quoted 5 lines

V2:
* Add underscore
* Add length check
* Expand commit message
* Use shorter syntax

+1

Quoted 8 lines
 lib/tst_cgroup.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/tst_cgroup.c +0 -0
index 5240aadaa..c5cb20505 100644

lib/tst_cgroup.c __attribute__ ((nonnull, warn_unused_result)) · line 442

Quoted 10 lines
lib/tst_cgroup.c +7 -0
@@ -433,9 +433,20 @@ __attribute__ ((nonnull, warn_unused_result))
 static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name)
 {
 	struct cgroup_ctrl *ctrl;
+	int l = 0;
+	char c = ctrl_name[l];
+
+	while (c == '_' || (c >= 'a' && c <= 'z'))
+		c = ctrl_name[++l];
+
+	if (l > 32)

+1 for this check.
I would slightly prefer to add MAX_CGROUP_TYPE_NAMELEN definition into our include/tst_cgroup.h, but it can stay as is.

BTW include/linux/cgroup-defs.h contains also MAX_CGROUP_ROOT_NAMELEN, there are also other checks in C files (CGROUP_FILE_NAME_MAX) in case it makes sense to have some checks for these (I have no idea).

Kind regards,
Petr

Quoted 11 lines
lib/tst_cgroup.c +5 -1
+		tst_res(TWARN, "Subsys name len greater than max known value of MAX_CGROUP_TYPE_NAMELEN: %d > 32", l);
 
+
+	if (!(c == '\n' || c == '\0'))
+		tst_brk(TBROK, "Unexpected char in %s: %c", ctrl_name, c);
 
 	for_each_ctrl(ctrl) {
-		if (!strcmp(ctrl_name, ctrl->ctrl_name))
+		if (!strncmp(ctrl_name, ctrl->ctrl_name, l))
 			return ctrl;
 	}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hello,

Quoted 33 lines · Petr Vorel

Hi Richie,

Quoted 5 lines

V2:
* Add underscore
* Add length check
* Expand commit message
* Use shorter syntax

+1

Quoted 8 lines
 lib/tst_cgroup.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/tst_cgroup.c +0 -0
index 5240aadaa..c5cb20505 100644

lib/tst_cgroup.c __attribute__ ((nonnull, warn_unused_result)) · line 442

Quoted 10 lines
lib/tst_cgroup.c +7 -0
@@ -433,9 +433,20 @@ __attribute__ ((nonnull, warn_unused_result))
 static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name)
 {
 	struct cgroup_ctrl *ctrl;
+	int l = 0;
+	char c = ctrl_name[l];
+
+	while (c == '_' || (c >= 'a' && c <= 'z'))
+		c = ctrl_name[++l];
+
+	if (l > 32)

+1 for this check.
I would slightly prefer to add MAX_CGROUP_TYPE_NAMELEN definition into our include/tst_cgroup.h, but it can stay as is.

Quoted 3 lines

BTW include/linux/cgroup-defs.h contains also MAX_CGROUP_ROOT_NAMELEN, there are also other checks in C files (CGROUP_FILE_NAME_MAX) in case it makes sense to have some checks for these (I have no idea).

I'm not sure about this. On the one hand it may detect a bug in the library or kernel. On the other it's not exposed to userland or defined anywhere.

For me the main thing is to avoid janitorial work like creating and merging patches just to increase some internal kernel value that was increased or is ignored in some corner case.

I'll post another patch if there is some obvious way to check the root name length that would help the library.

Thanks for the review, I'll merge this now.

Quoted 15 lines

Kind regards,
Petr

Quoted 11 lines
lib/tst_cgroup.c +5 -1
+		tst_res(TWARN, "Subsys name len greater than max known value of MAX_CGROUP_TYPE_NAMELEN: %d > 32", l);
 
+
+	if (!(c == '\n' || c == '\0'))
+		tst_brk(TBROK, "Unexpected char in %s: %c", ctrl_name, c);
 
 	for_each_ctrl(ctrl) {
-		if (!strcmp(ctrl_name, ctrl->ctrl_name))
+		if (!strncmp(ctrl_name, ctrl->ctrl_name, l))
 			return ctrl;
 	}
Signature · 5 lines
Thank you,
Richard.

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

Hello,

Quoted 32 lines · Petr Vorel

Hi Richie,

Quoted 5 lines

V2:
* Add underscore
* Add length check
* Expand commit message
* Use shorter syntax

+1

Quoted 8 lines
 lib/tst_cgroup.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
lib/tst_cgroup.c +0 -0
index 5240aadaa..c5cb20505 100644

lib/tst_cgroup.c __attribute__ ((nonnull, warn_unused_result)) · line 442

Quoted 10 lines
lib/tst_cgroup.c +7 -0
@@ -433,9 +433,20 @@ __attribute__ ((nonnull, warn_unused_result))
 static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name)
 {
 	struct cgroup_ctrl *ctrl;
+	int l = 0;
+	char c = ctrl_name[l];
+
+	while (c == '_' || (c >= 'a' && c <= 'z'))
+		c = ctrl_name[++l];
+
+	if (l > 32)

+1 for this check.
I would slightly prefer to add MAX_CGROUP_TYPE_NAMELEN definition into our include/tst_cgroup.h, but it can stay as is.

Quoted 7 lines
Quoted 3 lines

BTW include/linux/cgroup-defs.h contains also MAX_CGROUP_ROOT_NAMELEN, there are also other checks in C files (CGROUP_FILE_NAME_MAX) in case it makes sense to have some checks for these (I have no idea).

I'm not sure about this. On the one hand it may detect a bug in the library or kernel. On the other it's not exposed to userland or defined anywhere.

Yep, it's just an idea, I'm not sure myself.

Quoted 3 lines

For me the main thing is to avoid janitorial work like creating and merging patches just to increase some internal kernel value that was increased or is ignored in some corner case.

+1

Quoted 2 lines

I'll post another patch if there is some obvious way to check the root name length that would help the library.

+1

Quoted 1 line

Thanks for the review, I'll merge this now.

Great.

Kind regards,
Petr

Quoted 14 lines
Quoted 14 lines

Kind regards,
Petr

Quoted 11 lines
lib/tst_cgroup.c +5 -1
+		tst_res(TWARN, "Subsys name len greater than max known value of MAX_CGROUP_TYPE_NAMELEN: %d > 32", l);
 
+
+	if (!(c == '\n' || c == '\0'))
+		tst_brk(TBROK, "Unexpected char in %s: %c", ctrl_name, c);
 
 	for_each_ctrl(ctrl) {
-		if (!strcmp(ctrl_name, ctrl->ctrl_name))
+		if (!strncmp(ctrl_name, ctrl->ctrl_name, l))
 			return ctrl;
 	}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp