[LTP] [PATCH v2] clone3: Add clone3's clone_args cgroup
Cyril Hrubis
chrubis@suse.cz
Fri Apr 21 15:29:15 CEST 2023
Hi!
> --- /dev/null
> +++ b/testcases/kernel/syscalls/clone3/clone303.c
> @@ -0,0 +1,98 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2023 SUSE LLC <wegao@suse.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * This test case check clone3 CLONE_INTO_CGROUP flag
> + *
> + */
> +
> +#define _GNU_SOURCE
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/wait.h>
> +
> +#include "tst_test.h"
> +#include "lapi/sched.h"
> +#include "lapi/pidfd.h"
> +
> +#define BUF_LEN 20
> +#define CLONE_INTO_CGROUP 0x200000000ULL
This should be added as a fallback definition into lapi/sched.h
> +static struct tst_cg_group *cg_child_test_simple;
> +static struct clone_args *args;
> +
> +static pid_t clone_into_cgroup(int cgroup_fd)
> +{
> + pid_t pid;
> +
> + args->flags = CLONE_INTO_CGROUP;
> + args->exit_signal = SIGCHLD;
> + args->cgroup = cgroup_fd;
> +
> + pid = clone3(args, sizeof(*args));
> +
> + if (pid < 0)
> + tst_res(TFAIL | TTERRNO, "clone3() failed !");
> +
> + return pid;
> +}
> +
> +static void run(void)
> +{
> + char path[100];
> + int fd;
> + pid_t pid;
> +
> + cg_child_test_simple = tst_cg_group_mk(tst_cg, "cg_test_simple");
> + sprintf(path, "/sys/fs/cgroup/ltp/%s/%s", tst_cg_group_name(tst_cg), tst_cg_group_name(cg_child_test_simple));
There is no guarantee that the cgroup is mounted in this hardcoded path
and that the top level directory for LTP tests is called ltp.
Moreover we already do have a file descriptor pointing to the directory
for cgroup v2 opened in the cgroup library. All we need is a function in
the library that would find the take the tst_cg_group and find the right
cgroup_dir in the dirs[] array.
> + tst_res(TINFO, "cgroup path is %s", path);
> + fd = SAFE_OPEN(path, O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_PATH);
> +
> + pid = clone_into_cgroup(fd);
> +
> + if (!pid) {
> + TST_CHECKPOINT_WAIT(0);
Just add reutrn; here and drop the else below.
> + } else {
> +
> + char buf[BUF_LEN];
> +
> + SAFE_CG_READ(cg_child_test_simple, "cgroup.procs", buf, BUF_LEN);
^
sizeof(buf);
> +
> + int x = atoi(buf);
> +
> + if (x == pid)
> + tst_res(TPASS, "clone3 case pass!");
> + else
> + tst_brk(TFAIL | TTERRNO, "clone3() failed !");
> +
> + TST_CHECKPOINT_WAKE(0);
> +
> + SAFE_WAITPID(pid, NULL, 0);
> +
> + cg_child_test_simple = tst_cg_group_rm(cg_child_test_simple);
> + }
> +}
> +
> +static void setup(void)
> +{
> + clone3_supported_by_kernel();
> +}
> +
> +static struct tst_test test = {
> + .test_all = run,
> + .setup = setup,
> + .forks_child = 1,
> + .max_runtime = 20,
> + .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
> + .needs_cgroup_ver = TST_CG_V2,
> + .needs_checkpoints = 1,
> + .min_kver = "5.7",
> + .bufs = (struct tst_buffers []) {
> + {&args, .size = sizeof(*args)},
> + {},
> + }
> +};
> --
> 2.35.3
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list