[LTP] [PATCH v1] tst_cgroup.c: Force tst_cg_scan only scan specific cgroup version if needs_ver exist

Wei Gao via ltp
2024-11-08T05:37:46Z

tst_cgroup.c will scan cgroup mount point and check system's cgroup support status, both
cgroupv1 and cgroupv2, if your case only need check cgroupv1(such as needs_cgroup_ver = TST_CG_V1)
but your system contain cgroupv2 mount point, then TCONF will happen tell you your system already
mount cgroupv2, so test case will be skipped.

This change will fix above scenario, force check specific version of cgroup once you set needs_ver,
then test will continue try to create needs_ver of cgroup mount point.

This patch can be used by following cpuset02 case:
https://patchwork.ozlabs.org/project/ltp/patch/20240930135809.9300-1-wegao@suse.com/

Signed-off-by: Wei Gao <wegao@suse.com>
---
include/tst_cgroup.h | 2 +-
lib/newlib_tests/tst_cgroup01.c | 2 +-
lib/newlib_tests/tst_cgroup02.c | 2 +-
lib/tst_cgroup.c | 15 +++++++--------
testcases/lib/tst_cgctl.c | 4 ++--
5 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/include/tst_cgroup.h b/include/tst_cgroup.h
index d23a8e652..ff1c4ece0 100644
--- a/include/tst_cgroup.h
+++ b/include/tst_cgroup.h
@@ -117,7 +117,7 @@ extern const struct tst_cg_group *const tst_cg_drain;
/* Search the system for mounted cgroups and available
* controllers. Called automatically by tst_cg_require.
*/
-void tst_cg_scan(void);
+void tst_cg_scan(enum tst_cg_ver needs_ver);
/* Print the config detected by tst_cg_scan and print the internal
* state associated with each controller. Output can be passed to
* tst_cg_load_config to configure the internal state to that of the
diff --git a/lib/newlib_tests/tst_cgroup01.c b/lib/newlib_tests/tst_cgroup01.c
index eda0c548d..00bced72b 100644
--- a/lib/newlib_tests/tst_cgroup01.c
+++ b/lib/newlib_tests/tst_cgroup01.c
@@ -23,7 +23,7 @@ static void setup(void)
{
cgopts.needs_ver = !!only_mount_v1 ? TST_CG_V1 : 0;

- tst_cg_scan();
+ tst_cg_scan(0);
tst_cg_print_config();

tst_cg_require("memory", &cgopts);
diff --git a/lib/newlib_tests/tst_cgroup02.c b/lib/newlib_tests/tst_cgroup02.c
index de2ca1812..47d1c6b2a 100644
--- a/lib/newlib_tests/tst_cgroup02.c
+++ b/lib/newlib_tests/tst_cgroup02.c
@@ -58,7 +58,7 @@ static void setup(void)
{
cgopts.needs_ver = !!only_mount_v1 ? TST_CG_V1 : 0;

- tst_cg_scan();
+ tst_cg_scan(0);
tst_cg_print_config();

tst_cg_require("memory", &cgopts);
diff --git a/lib/tst_cgroup.c b/lib/tst_cgroup.c
index 1404116a5..652bb70b5 100644
--- a/lib/tst_cgroup.c
+++ b/lib/tst_cgroup.c
@@ -657,7 +657,7 @@ discard:
close(mnt_dfd);
}

-void tst_cg_scan(void)
+void tst_cg_scan(enum tst_cg_ver needs_ver)
{
struct mntent *mnt;
FILE *f = setmntent("/proc/self/mounts", "r");
@@ -674,10 +674,9 @@ void tst_cg_scan(void)
}

do {
- if (strncmp(mnt->mnt_type, "cgroup", 6))
- continue;
-
- cgroup_root_scan(mnt->mnt_type, mnt->mnt_dir, mnt->mnt_opts);
+ if ((!strcmp(mnt->mnt_type, "cgroup") && (needs_ver != TST_CG_V2)) ||
+ (!strcmp(mnt->mnt_type, "cgroup2") && (needs_ver != TST_CG_V1)))
+ cgroup_root_scan(mnt->mnt_type, mnt->mnt_dir, mnt->mnt_opts);
} while ((mnt = getmntent(f)));
}

@@ -717,7 +716,7 @@ mount:

if (!ret) {
tst_res(TINFO, "Mounted V2 CGroups on %s", mnt_path);
- tst_cg_scan();
+ tst_cg_scan(TST_CG_V2);
roots[0].we_mounted_it = 1;
return;
}
@@ -775,7 +774,7 @@ mount:
}

tst_res(TINFO, "Mounted V1 %s CGroup on %s", ctrl->ctrl_name, mnt_path);
- tst_cg_scan();
+ tst_cg_scan(TST_CG_V1);
if (!ctrl->ctrl_root)
return;

@@ -856,7 +855,7 @@ void tst_cg_require(const char *const ctrl_name,
if (ctrl->ctrl_root)
goto mkdirs;

- tst_cg_scan();
+ tst_cg_scan(options->needs_ver);

if (ctrl->ctrl_root)
goto mkdirs;
diff --git a/testcases/lib/tst_cgctl.c b/testcases/lib/tst_cgctl.c
index 2685bef81..6f324f780 100644
--- a/testcases/lib/tst_cgctl.c
+++ b/testcases/lib/tst_cgctl.c
@@ -30,7 +30,7 @@ static int cgctl_require(const char *ctrl, int test_pid)

static int cgctl_cleanup(const char *const config)
{
- tst_cg_scan();
+ tst_cg_scan(0);
tst_cg_load_config(config);
tst_cg_cleanup();

@@ -39,7 +39,7 @@ static int cgctl_cleanup(const char *const config)

static int cgctl_print(void)
{
- tst_cg_scan();
+ tst_cg_scan(0);
tst_cg_print_config();

return 0;
--
2.35.3