Re: [PATCH v2] selftests: add tests for clone3()

From: Adrian Reber
Date: Fri Nov 08 2019 - 01:44:37 EST


On Wed, Nov 06, 2019 at 04:59:15PM +0100, Christian Brauner wrote:
> On Mon, Nov 04, 2019 at 02:18:46PM +0100, Adrian Reber wrote:
> > This adds tests for clone3() with different values and sizes
> > of struct clone_args.
> >
> > This selftest was initially part of of the clone3() with PID selftest.
> > After that patch was almost merged Eugene sent out a couple of patches
> > to fix problems with these test.
> >
> > This commit now only contains the clone3() selftest after the LPC
> > decision to rework clone3() with PID to allow setting the PID in
> > multiple PID namespaces including all of Eugene's patches.
> >
> > Signed-off-by: Eugene Syromiatnikov <esyr@xxxxxxxxxx>
> > Signed-off-by: Adrian Reber <areber@xxxxxxxxxx>
>
> Resending, since mutt messed-up the quoting due to a new configuration I
> was testing.
>
> A few more comments below.
>
> Also, would you be open to adding tests here for the newly added .stack
> and .stack_size API (cf. [1])?
>
> [1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fa729c4df558936b4a1a7b3e2234011f44ede28b

As mentioned in your follow-up email. Let's do that in a later patch.

> > ---
> > v2:
> > - Applied Christian's suggestions
> > - Skip root-only tests when running as non-root
> > ---
> > MAINTAINERS | 1 +
> > tools/testing/selftests/Makefile | 1 +
> > tools/testing/selftests/clone3/.gitignore | 1 +
> > tools/testing/selftests/clone3/Makefile | 7 +
> > tools/testing/selftests/clone3/clone3.c | 225 ++++++++++++++++++++++
> > 5 files changed, 235 insertions(+)
> > create mode 100644 tools/testing/selftests/clone3/.gitignore
> > create mode 100644 tools/testing/selftests/clone3/Makefile
> > create mode 100644 tools/testing/selftests/clone3/clone3.c
> >
> > diff --git a/MAINTAINERS b/MAINTAINERS
> > index cba1095547fd..0040b7a6410b 100644
> > --- a/MAINTAINERS
> > +++ b/MAINTAINERS
> > @@ -12829,6 +12829,7 @@ S: Maintained
> > T: git git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git
> > F: samples/pidfd/
> > F: tools/testing/selftests/pidfd/
> > +F: tools/testing/selftests/clone3/
> > K: (?i)pidfd
> > K: (?i)clone3
> > K: \b(clone_args|kernel_clone_args)\b
> > diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
> > index 4cdbae6f4e61..ad442364218a 100644
> > --- a/tools/testing/selftests/Makefile
> > +++ b/tools/testing/selftests/Makefile
> > @@ -4,6 +4,7 @@ TARGETS += bpf
> > TARGETS += breakpoints
> > TARGETS += capabilities
> > TARGETS += cgroup
> > +TARGETS += clone3
> > TARGETS += cpufreq
> > TARGETS += cpu-hotplug
> > TARGETS += drivers/dma-buf
> > diff --git a/tools/testing/selftests/clone3/.gitignore b/tools/testing/selftests/clone3/.gitignore
> > new file mode 100644
> > index 000000000000..85d9d3ba2524
> > --- /dev/null
> > +++ b/tools/testing/selftests/clone3/.gitignore
> > @@ -0,0 +1 @@
> > +clone3
> > diff --git a/tools/testing/selftests/clone3/Makefile b/tools/testing/selftests/clone3/Makefile
> > new file mode 100644
> > index 000000000000..ea922c014ae4
> > --- /dev/null
> > +++ b/tools/testing/selftests/clone3/Makefile
> > @@ -0,0 +1,7 @@
> > +# SPDX-License-Identifier: GPL-2.0
> > +
> > +CFLAGS += -I../../../../usr/include/
> > +
> > +TEST_GEN_PROGS := clone3
> > +
> > +include ../lib.mk
> > diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c
> > new file mode 100644
> > index 000000000000..a982d95189bf
> > --- /dev/null
> > +++ b/tools/testing/selftests/clone3/clone3.c
> > @@ -0,0 +1,225 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +/* Based on Christian Brauner's clone3() example */
> > +
> > +#define _GNU_SOURCE
> > +#include <errno.h>
> > +#include <inttypes.h>
> > +#include <linux/types.h>
> > +#include <linux/sched.h>
> > +#include <stdint.h>
> > +#include <stdio.h>
> > +#include <stdlib.h>
> > +#include <sys/syscall.h>
> > +#include <sys/types.h>
> > +#include <sys/un.h>
> > +#include <sys/wait.h>
> > +#include <unistd.h>
> > +#include <sched.h>
> > +
> > +#include "../kselftest.h"
> > +
> > +/*
> > + * Different sizes of struct clone_args
> > + */
> > +#ifndef CLONE3_ARGS_SIZE_V0
> > +#define CLONE3_ARGS_SIZE_V0 64
> > +#endif
> > +
> > +enum test_mode {
> > + CLONE3_ARGS_NO_TEST,
> > + CLONE3_ARGS_ALL_0,
> > + CLONE3_ARGS_ALL_1,
> > + CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG,
> > + CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG,
> > + CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG,
> > + CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG,
> > +};
> > +
> > +static pid_t raw_clone(struct clone_args *args, size_t size)
> > +{
> > + return syscall(__NR_clone3, args, size);
> > +}
> > +
> > +static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
> > +{
> > + struct clone_args args = {
> > + .flags = flags,
> > + .exit_signal = SIGCHLD,
> > + };
> > +
> > + struct clone_args_extended {
> > + struct clone_args args;
> > + __aligned_u64 excess_space[2];
> > + } args_ext;
> > +
> > + pid_t pid = -1;
> > + int status;
> > +
> > + memset(&args_ext, 0, sizeof(args_ext));
> > + if (size > sizeof(struct clone_args))
> > + args_ext.excess_space[1] = 1;
> > +
> > + if (size == 0)
> > + size = sizeof(struct clone_args);
> > +
> > + switch (test_mode) {
> > + case CLONE3_ARGS_ALL_0:
> > + args.flags = 0;
> > + args.exit_signal = 0;
> > + break;
> > + case CLONE3_ARGS_ALL_1:
>
> I don't fully understand this test case. What is this for exactly?

Not sure myself. It was just to make sure clone3() does not something
unexpected when given wrong and unexpected input. It is the opposite to
setting everything to zero. Not sure how much sense it makes, but as it
already exists I would say to just keep it.

> > + args.flags = 1;
> > + args.pidfd = 1;
> > + args.child_tid = 1;
> > + args.parent_tid = 1;
> > + args.exit_signal = 1;
> > + args.stack = 1;
> > + args.stack_size = 1;
> > + args.tls = 1;
> > + break;
> > + case CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG:
[...]

Let me know if you think that the CLONE3_ARGS_ALL_1 should really be
removed. I will fix the other two things you mentioned and resend a new
version.

Adrian