linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 1/2] tests: move common definitions and functions into pidfd.h
@ 2019-07-26 16:22 Suren Baghdasaryan
  2019-07-26 16:22 ` [PATCH v3 2/2] tests: add pidfd poll tests Suren Baghdasaryan
  2019-08-06 17:50 ` [PATCH v3 1/2] tests: move common definitions and functions into pidfd.h Christian Brauner
  0 siblings, 2 replies; 9+ messages in thread
From: Suren Baghdasaryan @ 2019-07-26 16:22 UTC (permalink / raw)
  To: surenb
  Cc: christian, arnd, ebiederm, keescook, joel, dancol, tglx, jannh,
	dhowells, mtk.manpages, luto, akpm, oleg, cyphar, torvalds, viro,
	linux-api, linux-kselftest, kernel-team

Move definitions and functions used across different pidfd tests into
pidfd.h header.

Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Christian Brauner <christian@brauner.io>
---
Changes since v1 (https://lore.kernel.org/linux-kselftest/20190723173907.196488-1-surenb@google.com)
	- Move common test definitions and functions into pidfd.h as per Christian Brauner
	- Code cleanup and additional error handling as per Christian Brauner
Changes since v2 (https://lore.kernel.org/linux-kselftest/20190725002204.185225-2-surenb@google.com)
	- Added waitpid to prevent zombies as per Yann Droneaud

Patches are based on v5.3-rc1 in Linus' tree.
---
 tools/testing/selftests/pidfd/pidfd.h          | 18 ++++++++++++++++++
 .../testing/selftests/pidfd/pidfd_open_test.c  |  5 -----
 tools/testing/selftests/pidfd/pidfd_test.c     | 10 ----------
 3 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftests/pidfd/pidfd.h
index 8452e910463f..db4377af6be7 100644
--- a/tools/testing/selftests/pidfd/pidfd.h
+++ b/tools/testing/selftests/pidfd/pidfd.h
@@ -16,6 +16,14 @@
 
 #include "../kselftest.h"
 
+#ifndef __NR_pidfd_open
+#define __NR_pidfd_open -1
+#endif
+
+#ifndef __NR_pidfd_send_signal
+#define __NR_pidfd_send_signal -1
+#endif
+
 /*
  * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
  * That means, when it wraps around any pid < 300 will be skipped.
@@ -53,5 +61,15 @@ int wait_for_pid(pid_t pid)
 	return WEXITSTATUS(status);
 }
 
+static inline int sys_pidfd_open(pid_t pid, unsigned int flags)
+{
+	return syscall(__NR_pidfd_open, pid, flags);
+}
+
+static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
+					unsigned int flags)
+{
+	return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
+}
 
 #endif /* __PIDFD_H */
diff --git a/tools/testing/selftests/pidfd/pidfd_open_test.c b/tools/testing/selftests/pidfd/pidfd_open_test.c
index 0377133dd6dc..b9fe75fc3e51 100644
--- a/tools/testing/selftests/pidfd/pidfd_open_test.c
+++ b/tools/testing/selftests/pidfd/pidfd_open_test.c
@@ -22,11 +22,6 @@
 #include "pidfd.h"
 #include "../kselftest.h"
 
-static inline int sys_pidfd_open(pid_t pid, unsigned int flags)
-{
-	return syscall(__NR_pidfd_open, pid, flags);
-}
-
 static int safe_int(const char *numstr, int *converted)
 {
 	char *err = NULL;
diff --git a/tools/testing/selftests/pidfd/pidfd_test.c b/tools/testing/selftests/pidfd/pidfd_test.c
index 7eaa8a3de262..17b2fd621726 100644
--- a/tools/testing/selftests/pidfd/pidfd_test.c
+++ b/tools/testing/selftests/pidfd/pidfd_test.c
@@ -21,10 +21,6 @@
 #include "pidfd.h"
 #include "../kselftest.h"
 
-#ifndef __NR_pidfd_send_signal
-#define __NR_pidfd_send_signal -1
-#endif
-
 #define str(s) _str(s)
 #define _str(s) #s
 #define CHILD_THREAD_MIN_WAIT 3 /* seconds */
@@ -47,12 +43,6 @@ static pid_t pidfd_clone(int flags, int *pidfd, int (*fn)(void *))
 #endif
 }
 
-static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
-					unsigned int flags)
-{
-	return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
-}
-
 static int signal_received;
 
 static void set_signal_received_on_sigusr1(int sig)
-- 
2.22.0.709.g102302147b-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v3 2/2] tests: add pidfd poll tests
  2019-07-26 16:22 [PATCH v3 1/2] tests: move common definitions and functions into pidfd.h Suren Baghdasaryan
@ 2019-07-26 16:22 ` Suren Baghdasaryan
  2019-07-26 17:07   ` Joel Fernandes
                     ` (2 more replies)
  2019-08-06 17:50 ` [PATCH v3 1/2] tests: move common definitions and functions into pidfd.h Christian Brauner
  1 sibling, 3 replies; 9+ messages in thread
From: Suren Baghdasaryan @ 2019-07-26 16:22 UTC (permalink / raw)
  To: surenb
  Cc: christian, arnd, ebiederm, keescook, joel, dancol, tglx, jannh,
	dhowells, mtk.manpages, luto, akpm, oleg, cyphar, torvalds, viro,
	linux-api, linux-kselftest, kernel-team

This adds testing for polling on pidfd of a process being killed. Test runs
10000 iterations by default to stress test pidfd polling functionality.
It accepts an optional command-line parameter to override the number or
iterations to run.
Specifically, it tests for:
- pidfd_open on a child process succeeds
- pidfd_send_signal on a child process succeeds
- polling on pidfd succeeds and returns exactly one event
- returned event is POLLIN
- event is received within 3 secs of the process being killed

10000 iterations was chosen because of the race condition being tested
which is not consistently reproducible but usually is revealed after less
than 2000 iterations.
Reveals race fixed by commit b191d6491be6 ("pidfd: fix a poll race when setting exit_state")

Signed-off-by: Suren Baghdasaryan <surenb@google.com>
---
 tools/testing/selftests/pidfd/.gitignore      |   1 +
 tools/testing/selftests/pidfd/Makefile        |   2 +-
 .../testing/selftests/pidfd/pidfd_poll_test.c | 117 ++++++++++++++++++
 3 files changed, 119 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/pidfd/pidfd_poll_test.c

diff --git a/tools/testing/selftests/pidfd/.gitignore b/tools/testing/selftests/pidfd/.gitignore
index 16d84d117bc0..a67896347d34 100644
--- a/tools/testing/selftests/pidfd/.gitignore
+++ b/tools/testing/selftests/pidfd/.gitignore
@@ -1,2 +1,3 @@
 pidfd_open_test
+pidfd_poll_test
 pidfd_test
diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile
index 720b2d884b3c..ed58b7108d18 100644
--- a/tools/testing/selftests/pidfd/Makefile
+++ b/tools/testing/selftests/pidfd/Makefile
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -g -I../../../../usr/include/ -lpthread
 
-TEST_GEN_PROGS := pidfd_test pidfd_open_test
+TEST_GEN_PROGS := pidfd_test pidfd_open_test pidfd_poll_test
 
 include ../lib.mk
 
diff --git a/tools/testing/selftests/pidfd/pidfd_poll_test.c b/tools/testing/selftests/pidfd/pidfd_poll_test.c
new file mode 100644
index 000000000000..f1b62b91e53e
--- /dev/null
+++ b/tools/testing/selftests/pidfd/pidfd_poll_test.c
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <linux/types.h>
+#include <linux/wait.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syscall.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "pidfd.h"
+#include "../kselftest.h"
+
+static bool timeout;
+
+static void handle_alarm(int sig)
+{
+	timeout = true;
+}
+
+int main(int argc, char **argv)
+{
+	struct pollfd fds;
+	int iter, nevents;
+	int nr_iterations = 10000;
+
+	fds.events = POLLIN;
+
+	if (argc > 2)
+		ksft_exit_fail_msg("Unexpected command line argument\n");
+
+	if (argc == 2) {
+		nr_iterations = atoi(argv[1]);
+		if (nr_iterations <= 0)
+			ksft_exit_fail_msg("invalid input parameter %s\n",
+					argv[1]);
+	}
+
+	ksft_print_msg("running pidfd poll test for %d iterations\n",
+		nr_iterations);
+
+	for (iter = 0; iter < nr_iterations; iter++) {
+		int pidfd;
+		int child_pid = fork();
+
+		if (child_pid < 0) {
+			if (errno == EAGAIN) {
+				iter--;
+				continue;
+			}
+			ksft_exit_fail_msg(
+				"%s - failed to fork a child process\n",
+				strerror(errno));
+		}
+
+		if (child_pid == 0) {
+			/* Child process just sleeps for a min and exits */
+			sleep(60);
+			exit(EXIT_SUCCESS);
+		}
+
+		/* Parent kills the child and waits for its death */
+		pidfd = sys_pidfd_open(child_pid, 0);
+		if (pidfd < 0)
+			ksft_exit_fail_msg("%s - pidfd_open failed\n",
+					strerror(errno));
+
+		/* Setup 3 sec alarm - plenty of time */
+		if (signal(SIGALRM, handle_alarm) == SIG_ERR)
+			ksft_exit_fail_msg("%s - signal failed\n",
+					strerror(errno));
+		alarm(3);
+
+		/* Send SIGKILL to the child */
+		if (sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0))
+			ksft_exit_fail_msg("%s - pidfd_send_signal failed\n",
+					strerror(errno));
+
+		/* Wait for the death notification */
+		fds.fd = pidfd;
+		nevents = poll(&fds, 1, -1);
+
+		/* Check for error conditions */
+		if (nevents < 0)
+			ksft_exit_fail_msg("%s - poll failed\n",
+					strerror(errno));
+
+		if (nevents != 1)
+			ksft_exit_fail_msg("unexpected poll result: %d\n",
+					nevents);
+
+		if (!(fds.revents & POLLIN))
+			ksft_exit_fail_msg(
+				"unexpected event type received: 0x%x\n",
+				fds.revents);
+
+		if (timeout)
+			ksft_exit_fail_msg(
+				"death notification wait timeout\n");
+
+		close(pidfd);
+		// Wait for child to prevent zombies
+		if (waitpid(child_pid, NULL, 0) < 0)
+			ksft_exit_fail_msg("%s - waitpid failed\n",
+					strerror(errno));
+
+	}
+
+	ksft_test_result_pass("pidfd poll test: pass\n");
+	return ksft_exit_pass();
+}
-- 
2.22.0.709.g102302147b-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] tests: add pidfd poll tests
  2019-07-26 16:22 ` [PATCH v3 2/2] tests: add pidfd poll tests Suren Baghdasaryan
@ 2019-07-26 17:07   ` Joel Fernandes
  2019-07-26 17:24     ` Suren Baghdasaryan
  2019-07-27  8:41     ` Christian Brauner
  2019-07-27 18:24   ` Yann Droneaud
  2019-08-06 17:51   ` Christian Brauner
  2 siblings, 2 replies; 9+ messages in thread
From: Joel Fernandes @ 2019-07-26 17:07 UTC (permalink / raw)
  To: Suren Baghdasaryan
  Cc: christian, arnd, ebiederm, keescook, dancol, tglx, jannh,
	dhowells, mtk.manpages, luto, akpm, oleg, cyphar, torvalds, viro,
	linux-api, linux-kselftest, kernel-team

On Fri, Jul 26, 2019 at 09:22:26AM -0700, Suren Baghdasaryan wrote:
> This adds testing for polling on pidfd of a process being killed. Test runs
> 10000 iterations by default to stress test pidfd polling functionality.
> It accepts an optional command-line parameter to override the number or
> iterations to run.
> Specifically, it tests for:
> - pidfd_open on a child process succeeds
> - pidfd_send_signal on a child process succeeds
> - polling on pidfd succeeds and returns exactly one event
> - returned event is POLLIN
> - event is received within 3 secs of the process being killed
> 
> 10000 iterations was chosen because of the race condition being tested
> which is not consistently reproducible but usually is revealed after less
> than 2000 iterations.
> Reveals race fixed by commit b191d6491be6 ("pidfd: fix a poll race when setting exit_state")
> 
> Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> ---

> +		close(pidfd);
> +		// Wait for child to prevent zombies

Comment style should be /* */, but I think Christian would be kind enough to
fix that when he applies the patch so shouldn't need a resend.

Thanks Suren and Christian!

Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] tests: add pidfd poll tests
  2019-07-26 17:07   ` Joel Fernandes
@ 2019-07-26 17:24     ` Suren Baghdasaryan
  2019-07-27  8:41     ` Christian Brauner
  1 sibling, 0 replies; 9+ messages in thread
From: Suren Baghdasaryan @ 2019-07-26 17:24 UTC (permalink / raw)
  To: Joel Fernandes
  Cc: Christian Brauner, arnd, Eric W. Biederman, Kees Cook,
	Daniel Colascione, tglx, Jann Horn, dhowells, mtk.manpages, luto,
	Andrew Morton, Oleg Nesterov, cyphar, Linus Torvalds, viro,
	linux-api, linux-kselftest, kernel-team

On Fri, Jul 26, 2019 at 10:07 AM Joel Fernandes <joel@joelfernandes.org> wrote:
>
> On Fri, Jul 26, 2019 at 09:22:26AM -0700, Suren Baghdasaryan wrote:
> > This adds testing for polling on pidfd of a process being killed. Test runs
> > 10000 iterations by default to stress test pidfd polling functionality.
> > It accepts an optional command-line parameter to override the number or
> > iterations to run.
> > Specifically, it tests for:
> > - pidfd_open on a child process succeeds
> > - pidfd_send_signal on a child process succeeds
> > - polling on pidfd succeeds and returns exactly one event
> > - returned event is POLLIN
> > - event is received within 3 secs of the process being killed
> >
> > 10000 iterations was chosen because of the race condition being tested
> > which is not consistently reproducible but usually is revealed after less
> > than 2000 iterations.
> > Reveals race fixed by commit b191d6491be6 ("pidfd: fix a poll race when setting exit_state")
> >
> > Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> > ---
>
> > +             close(pidfd);
> > +             // Wait for child to prevent zombies
>
> Comment style should be /* */, but I think Christian would be kind enough to
> fix that when he applies the patch so shouldn't need a resend.

Agrh! That's what I get for constantly switching between kernel and
userspace development :\
Christian please let me know if I should respin this or you can fix it
when taking this patch.
Thanks!

>
> Thanks Suren and Christian!
>
> Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] tests: add pidfd poll tests
  2019-07-26 17:07   ` Joel Fernandes
  2019-07-26 17:24     ` Suren Baghdasaryan
@ 2019-07-27  8:41     ` Christian Brauner
  1 sibling, 0 replies; 9+ messages in thread
From: Christian Brauner @ 2019-07-27  8:41 UTC (permalink / raw)
  To: Joel Fernandes
  Cc: Suren Baghdasaryan, arnd, ebiederm, keescook, dancol, tglx,
	jannh, dhowells, mtk.manpages, luto, akpm, oleg, cyphar,
	torvalds, viro, linux-api, linux-kselftest, kernel-team

On Fri, Jul 26, 2019 at 01:07:28PM -0400, Joel Fernandes wrote:
> On Fri, Jul 26, 2019 at 09:22:26AM -0700, Suren Baghdasaryan wrote:
> > This adds testing for polling on pidfd of a process being killed. Test runs
> > 10000 iterations by default to stress test pidfd polling functionality.
> > It accepts an optional command-line parameter to override the number or
> > iterations to run.
> > Specifically, it tests for:
> > - pidfd_open on a child process succeeds
> > - pidfd_send_signal on a child process succeeds
> > - polling on pidfd succeeds and returns exactly one event
> > - returned event is POLLIN
> > - event is received within 3 secs of the process being killed
> > 
> > 10000 iterations was chosen because of the race condition being tested
> > which is not consistently reproducible but usually is revealed after less
> > than 2000 iterations.
> > Reveals race fixed by commit b191d6491be6 ("pidfd: fix a poll race when setting exit_state")
> > 
> > Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> > ---
> 
> > +		close(pidfd);
> > +		// Wait for child to prevent zombies
> 
> Comment style should be /* */, but I think Christian would be kind enough to
> fix that when he applies the patch so shouldn't need a resend.

No, I will force you to resend this whole thing. :)
Jk, I'll replace the // when I apply it to my tree; no problem. :)

> 
> Thanks Suren and Christian!
> 
> Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] tests: add pidfd poll tests
  2019-07-26 16:22 ` [PATCH v3 2/2] tests: add pidfd poll tests Suren Baghdasaryan
  2019-07-26 17:07   ` Joel Fernandes
@ 2019-07-27 18:24   ` Yann Droneaud
  2019-08-06 18:03     ` Suren Baghdasaryan
  2019-08-06 17:51   ` Christian Brauner
  2 siblings, 1 reply; 9+ messages in thread
From: Yann Droneaud @ 2019-07-27 18:24 UTC (permalink / raw)
  To: Suren Baghdasaryan
  Cc: christian, arnd, ebiederm, keescook, joel, dancol, tglx, jannh,
	dhowells, mtk.manpages, luto, akpm, oleg, cyphar, torvalds, viro,
	linux-api, linux-kselftest, kernel-team

Hi,

Le vendredi 26 juillet 2019 à 09:22 -0700, Suren Baghdasaryan a écrit :
> This adds testing for polling on pidfd of a process being killed. Test runs
> 10000 iterations by default to stress test pidfd polling functionality.
> It accepts an optional command-line parameter to override the number or
> iterations to run.
> Specifically, it tests for:
> - pidfd_open on a child process succeeds
> - pidfd_send_signal on a child process succeeds
> - polling on pidfd succeeds and returns exactly one event
> - returned event is POLLIN
> - event is received within 3 secs of the process being killed
> 
> 10000 iterations was chosen because of the race condition being tested
> which is not consistently reproducible but usually is revealed after less
> than 2000 iterations.
> Reveals race fixed by commit b191d6491be6 ("pidfd: fix a poll race when setting exit_state")
> 
> Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> ---
>  tools/testing/selftests/pidfd/.gitignore      |   1 +
>  tools/testing/selftests/pidfd/Makefile        |   2 +-
>  .../testing/selftests/pidfd/pidfd_poll_test.c | 117 ++++++++++++++++++
>  3 files changed, 119 insertions(+), 1 deletion(-)
>  create mode 100644 tools/testing/selftests/pidfd/pidfd_poll_test.c
> 
> diff --git a/tools/testing/selftests/pidfd/.gitignore b/tools/testing/selftests/pidfd/.gitignore
> index 16d84d117bc0..a67896347d34 100644
> --- a/tools/testing/selftests/pidfd/.gitignore
> +++ b/tools/testing/selftests/pidfd/.gitignore
> @@ -1,2 +1,3 @@
>  pidfd_open_test
> +pidfd_poll_test
>  pidfd_test
> diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile
> index 720b2d884b3c..ed58b7108d18 100644
> --- a/tools/testing/selftests/pidfd/Makefile
> +++ b/tools/testing/selftests/pidfd/Makefile
> @@ -1,7 +1,7 @@
>  # SPDX-License-Identifier: GPL-2.0-only
>  CFLAGS += -g -I../../../../usr/include/ -lpthread
>  
> -TEST_GEN_PROGS := pidfd_test pidfd_open_test
> +TEST_GEN_PROGS := pidfd_test pidfd_open_test pidfd_poll_test
>  
>  include ../lib.mk
>  
> diff --git a/tools/testing/selftests/pidfd/pidfd_poll_test.c b/tools/testing/selftests/pidfd/pidfd_poll_test.c
> new file mode 100644
> index 000000000000..f1b62b91e53e
> --- /dev/null
> +++ b/tools/testing/selftests/pidfd/pidfd_poll_test.c
> @@ -0,0 +1,117 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#define _GNU_SOURCE
> +#include <errno.h>
> +#include <linux/types.h>
> +#include <linux/wait.h>
> +#include <poll.h>
> +#include <signal.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <syscall.h>
> +#include <sys/wait.h>
> +#include <unistd.h>
> +
> +#include "pidfd.h"
> +#include "../kselftest.h"
> +
> +static bool timeout;
> +
> +static void handle_alarm(int sig)
> +{
> +	timeout = true;
> +}

Not needed if poll() timeout is used instead.

> +
> +int main(int argc, char **argv)
> +{
> +	struct pollfd fds;
> +	int iter, nevents;
> +	int nr_iterations = 10000;
> +
> +	fds.events = POLLIN;
> +
> +	if (argc > 2)
> +		ksft_exit_fail_msg("Unexpected command line argument\n");
> +
> +	if (argc == 2) {
> +		nr_iterations = atoi(argv[1]);
> +		if (nr_iterations <= 0)
> +			ksft_exit_fail_msg("invalid input parameter %s\n",
> +					argv[1]);
> +	}
> +
> +	ksft_print_msg("running pidfd poll test for %d iterations\n",
> +		nr_iterations);
> +
> +	for (iter = 0; iter < nr_iterations; iter++) {
> +		int pidfd;
> +		int child_pid = fork();
> +
> +		if (child_pid < 0) {
> +			if (errno == EAGAIN) {
> +				iter--;
> +				continue;
> +			}
> +			ksft_exit_fail_msg(
> +				"%s - failed to fork a child process\n",
> +				strerror(errno));
> +		}
> +
> +		if (child_pid == 0) {
> +			/* Child process just sleeps for a min and exits */
> +			sleep(60);

Instead of relying on a timer for no reason, I would use the following:

	while (1)
		pause();

> +			exit(EXIT_SUCCESS);
> +		}
> +
> +		/* Parent kills the child and waits for its death */
> +		pidfd = sys_pidfd_open(child_pid, 0);
> +		if (pidfd < 0)
> +			ksft_exit_fail_msg("%s - pidfd_open failed\n",
> +					strerror(errno));
> +
> +		/* Setup 3 sec alarm - plenty of time */
> +		if (signal(SIGALRM, handle_alarm) == SIG_ERR)
> +			ksft_exit_fail_msg("%s - signal failed\n",
> +					strerror(errno));
> +		alarm(3);
> +

Would the poll() timeout be more simpler to use than relying on
SIGALRM: no need to setup signal, no need for handler, no need for
timeout variable.

> +		/* Send SIGKILL to the child */
> +		if (sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0))
> +			ksft_exit_fail_msg("%s - pidfd_send_signal failed\n",
> +					strerror(errno));
> +
> +		/* Wait for the death notification */
> +		fds.fd = pidfd;
> +		nevents = poll(&fds, 1, -1);
> +

With

        nevents = poll(&fds, 1, 3000);

> +		/* Check for error conditions */
> +		if (nevents < 0)
> +			ksft_exit_fail_msg("%s - poll failed\n",
> +					strerror(errno));
> +

And
	if (nevents == 0)
		ksft_exit_fail_msg(
			"death notification wait timeout\n");
 
> +		if (nevents != 1)
> +			ksft_exit_fail_msg("unexpected poll result: %d\n",
> +					nevents);
> +
> +		if (!(fds.revents & POLLIN))
> +			ksft_exit_fail_msg(
> +				"unexpected event type received: 0x%x\n",
> +				fds.revents);
> +
> +		if (timeout)
> +			ksft_exit_fail_msg(
> +				"death notification wait timeout\n");
> +
> +		close(pidfd);
> +		// Wait for child to prevent zombies
> +		if (waitpid(child_pid, NULL, 0) < 0)
> +			ksft_exit_fail_msg("%s - waitpid failed\n",
> +					strerror(errno));

I feel safer now you defeated the zombies army :) Thanks.

> +
> +	}
> +
> +	ksft_test_result_pass("pidfd poll test: pass\n");
> +	return ksft_exit_pass();
> +}

Regards

-- 
Yann Droneaud
OPTEYA



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 1/2] tests: move common definitions and functions into pidfd.h
  2019-07-26 16:22 [PATCH v3 1/2] tests: move common definitions and functions into pidfd.h Suren Baghdasaryan
  2019-07-26 16:22 ` [PATCH v3 2/2] tests: add pidfd poll tests Suren Baghdasaryan
@ 2019-08-06 17:50 ` Christian Brauner
  1 sibling, 0 replies; 9+ messages in thread
From: Christian Brauner @ 2019-08-06 17:50 UTC (permalink / raw)
  To: Suren Baghdasaryan
  Cc: christian, arnd, ebiederm, keescook, joel, dancol, tglx, jannh,
	dhowells, mtk.manpages, luto, akpm, oleg, cyphar, torvalds, viro,
	linux-api, linux-kselftest, kernel-team

On Fri, Jul 26, 2019 at 09:22:25AM -0700, Suren Baghdasaryan wrote:
> Move definitions and functions used across different pidfd tests into
> pidfd.h header.
> 
> Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> Reviewed-by: Christian Brauner <christian@brauner.io>

Applied-to:
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=pidfd

Tracked-in:
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=for-next

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] tests: add pidfd poll tests
  2019-07-26 16:22 ` [PATCH v3 2/2] tests: add pidfd poll tests Suren Baghdasaryan
  2019-07-26 17:07   ` Joel Fernandes
  2019-07-27 18:24   ` Yann Droneaud
@ 2019-08-06 17:51   ` Christian Brauner
  2 siblings, 0 replies; 9+ messages in thread
From: Christian Brauner @ 2019-08-06 17:51 UTC (permalink / raw)
  To: Suren Baghdasaryan
  Cc: christian, arnd, ebiederm, keescook, joel, dancol, tglx, jannh,
	dhowells, mtk.manpages, luto, akpm, oleg, cyphar, torvalds, viro,
	linux-api, linux-kselftest, kernel-team

On Fri, Jul 26, 2019 at 09:22:26AM -0700, Suren Baghdasaryan wrote:
> This adds testing for polling on pidfd of a process being killed. Test runs
> 10000 iterations by default to stress test pidfd polling functionality.
> It accepts an optional command-line parameter to override the number or
> iterations to run.
> Specifically, it tests for:
> - pidfd_open on a child process succeeds
> - pidfd_send_signal on a child process succeeds
> - polling on pidfd succeeds and returns exactly one event
> - returned event is POLLIN
> - event is received within 3 secs of the process being killed
> 
> 10000 iterations was chosen because of the race condition being tested
> which is not consistently reproducible but usually is revealed after less
> than 2000 iterations.
> Reveals race fixed by commit b191d6491be6 ("pidfd: fix a poll race when setting exit_state")
> 
> Signed-off-by: Suren Baghdasaryan <surenb@google.com>

Applied-to:
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=pidfd

Tracked-in:
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=for-next

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v3 2/2] tests: add pidfd poll tests
  2019-07-27 18:24   ` Yann Droneaud
@ 2019-08-06 18:03     ` Suren Baghdasaryan
  0 siblings, 0 replies; 9+ messages in thread
From: Suren Baghdasaryan @ 2019-08-06 18:03 UTC (permalink / raw)
  To: Yann Droneaud
  Cc: Christian Brauner, arnd, Eric W. Biederman, Kees Cook,
	Joel Fernandes, Daniel Colascione, Thomas Gleixner, Jann Horn,
	dhowells, mtk.manpages, luto, Andrew Morton, Oleg Nesterov,
	cyphar, Linus Torvalds, viro, linux-api, linux-kselftest,
	kernel-team

On Sat, Jul 27, 2019 at 11:24 AM Yann Droneaud <ydroneaud@opteya.com> wrote:
>
> Hi,

Hi Yann,
 So sorry I missed your email. My email filters were messed up and I
fixed them now but missed a bunch of emails :(

>
> Le vendredi 26 juillet 2019 à 09:22 -0700, Suren Baghdasaryan a écrit :
> > This adds testing for polling on pidfd of a process being killed. Test runs
> > 10000 iterations by default to stress test pidfd polling functionality.
> > It accepts an optional command-line parameter to override the number or
> > iterations to run.
> > Specifically, it tests for:
> > - pidfd_open on a child process succeeds
> > - pidfd_send_signal on a child process succeeds
> > - polling on pidfd succeeds and returns exactly one event
> > - returned event is POLLIN
> > - event is received within 3 secs of the process being killed
> >
> > 10000 iterations was chosen because of the race condition being tested
> > which is not consistently reproducible but usually is revealed after less
> > than 2000 iterations.
> > Reveals race fixed by commit b191d6491be6 ("pidfd: fix a poll race when setting exit_state")
> >
> > Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> > ---
> >  tools/testing/selftests/pidfd/.gitignore      |   1 +
> >  tools/testing/selftests/pidfd/Makefile        |   2 +-
> >  .../testing/selftests/pidfd/pidfd_poll_test.c | 117 ++++++++++++++++++
> >  3 files changed, 119 insertions(+), 1 deletion(-)
> >  create mode 100644 tools/testing/selftests/pidfd/pidfd_poll_test.c
> >
> > diff --git a/tools/testing/selftests/pidfd/.gitignore b/tools/testing/selftests/pidfd/.gitignore
> > index 16d84d117bc0..a67896347d34 100644
> > --- a/tools/testing/selftests/pidfd/.gitignore
> > +++ b/tools/testing/selftests/pidfd/.gitignore
> > @@ -1,2 +1,3 @@
> >  pidfd_open_test
> > +pidfd_poll_test
> >  pidfd_test
> > diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile
> > index 720b2d884b3c..ed58b7108d18 100644
> > --- a/tools/testing/selftests/pidfd/Makefile
> > +++ b/tools/testing/selftests/pidfd/Makefile
> > @@ -1,7 +1,7 @@
> >  # SPDX-License-Identifier: GPL-2.0-only
> >  CFLAGS += -g -I../../../../usr/include/ -lpthread
> >
> > -TEST_GEN_PROGS := pidfd_test pidfd_open_test
> > +TEST_GEN_PROGS := pidfd_test pidfd_open_test pidfd_poll_test
> >
> >  include ../lib.mk
> >
> > diff --git a/tools/testing/selftests/pidfd/pidfd_poll_test.c b/tools/testing/selftests/pidfd/pidfd_poll_test.c
> > new file mode 100644
> > index 000000000000..f1b62b91e53e
> > --- /dev/null
> > +++ b/tools/testing/selftests/pidfd/pidfd_poll_test.c
> > @@ -0,0 +1,117 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +
> > +#define _GNU_SOURCE
> > +#include <errno.h>
> > +#include <linux/types.h>
> > +#include <linux/wait.h>
> > +#include <poll.h>
> > +#include <signal.h>
> > +#include <stdbool.h>
> > +#include <stdio.h>
> > +#include <stdlib.h>
> > +#include <string.h>
> > +#include <syscall.h>
> > +#include <sys/wait.h>
> > +#include <unistd.h>
> > +
> > +#include "pidfd.h"
> > +#include "../kselftest.h"
> > +
> > +static bool timeout;
> > +
> > +static void handle_alarm(int sig)
> > +{
> > +     timeout = true;
> > +}
>
> Not needed if poll() timeout is used instead.
>
> > +
> > +int main(int argc, char **argv)
> > +{
> > +     struct pollfd fds;
> > +     int iter, nevents;
> > +     int nr_iterations = 10000;
> > +
> > +     fds.events = POLLIN;
> > +
> > +     if (argc > 2)
> > +             ksft_exit_fail_msg("Unexpected command line argument\n");
> > +
> > +     if (argc == 2) {
> > +             nr_iterations = atoi(argv[1]);
> > +             if (nr_iterations <= 0)
> > +                     ksft_exit_fail_msg("invalid input parameter %s\n",
> > +                                     argv[1]);
> > +     }
> > +
> > +     ksft_print_msg("running pidfd poll test for %d iterations\n",
> > +             nr_iterations);
> > +
> > +     for (iter = 0; iter < nr_iterations; iter++) {
> > +             int pidfd;
> > +             int child_pid = fork();
> > +
> > +             if (child_pid < 0) {
> > +                     if (errno == EAGAIN) {
> > +                             iter--;
> > +                             continue;
> > +                     }
> > +                     ksft_exit_fail_msg(
> > +                             "%s - failed to fork a child process\n",
> > +                             strerror(errno));
> > +             }
> > +
> > +             if (child_pid == 0) {
> > +                     /* Child process just sleeps for a min and exits */
> > +                     sleep(60);
>
> Instead of relying on a timer for no reason, I would use the following:
>
>         while (1)
>                 pause();
>

Yeah, not much difference I think. If you don't strongly object I'll
keep it this way.

> > +                     exit(EXIT_SUCCESS);
> > +             }
> > +
> > +             /* Parent kills the child and waits for its death */
> > +             pidfd = sys_pidfd_open(child_pid, 0);
> > +             if (pidfd < 0)
> > +                     ksft_exit_fail_msg("%s - pidfd_open failed\n",
> > +                                     strerror(errno));
> > +
> > +             /* Setup 3 sec alarm - plenty of time */
> > +             if (signal(SIGALRM, handle_alarm) == SIG_ERR)
> > +                     ksft_exit_fail_msg("%s - signal failed\n",
> > +                                     strerror(errno));
> > +             alarm(3);
> > +
>
> Would the poll() timeout be more simpler to use than relying on
> SIGALRM: no need to setup signal, no need for handler, no need for
> timeout variable.

Unfortunately that would not work because after timeout is passed
poll() checks for the condition one last time, it sees that the
process is dead (the condition is satisfied) and it returns nevents==1
(event did happen) instead of nevents==0 (poll timed out). I did
implement it this way originally and was surprised that it never timed
out even when I saw a 3 sec delay but then figured it out after going
through the kernel code.

> > +             /* Send SIGKILL to the child */
> > +             if (sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0))
> > +                     ksft_exit_fail_msg("%s - pidfd_send_signal failed\n",
> > +                                     strerror(errno));
> > +
> > +             /* Wait for the death notification */
> > +             fds.fd = pidfd;
> > +             nevents = poll(&fds, 1, -1);
> > +
>
> With
>
>         nevents = poll(&fds, 1, 3000);
>
> > +             /* Check for error conditions */
> > +             if (nevents < 0)
> > +                     ksft_exit_fail_msg("%s - poll failed\n",
> > +                                     strerror(errno));
> > +
>
> And
>         if (nevents == 0)
>                 ksft_exit_fail_msg(
>                         "death notification wait timeout\n");
>
> > +             if (nevents != 1)
> > +                     ksft_exit_fail_msg("unexpected poll result: %d\n",
> > +                                     nevents);
> > +
> > +             if (!(fds.revents & POLLIN))
> > +                     ksft_exit_fail_msg(
> > +                             "unexpected event type received: 0x%x\n",
> > +                             fds.revents);
> > +
> > +             if (timeout)
> > +                     ksft_exit_fail_msg(
> > +                             "death notification wait timeout\n");
> > +
> > +             close(pidfd);
> > +             // Wait for child to prevent zombies
> > +             if (waitpid(child_pid, NULL, 0) < 0)
> > +                     ksft_exit_fail_msg("%s - waitpid failed\n",
> > +                                     strerror(errno));
>
> I feel safer now you defeated the zombies army :) Thanks.

Thanks for pointing it out!

>
> > +
> > +     }
> > +
> > +     ksft_test_result_pass("pidfd poll test: pass\n");
> > +     return ksft_exit_pass();
> > +}
>
> Regards
>
> --
> Yann Droneaud
> OPTEYA
>

Thanks,
Suren.

>
> --
> To unsubscribe from this group and stop receiving emails from it, send an email to kernel-team+unsubscribe@android.com.
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2019-08-06 18:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-26 16:22 [PATCH v3 1/2] tests: move common definitions and functions into pidfd.h Suren Baghdasaryan
2019-07-26 16:22 ` [PATCH v3 2/2] tests: add pidfd poll tests Suren Baghdasaryan
2019-07-26 17:07   ` Joel Fernandes
2019-07-26 17:24     ` Suren Baghdasaryan
2019-07-27  8:41     ` Christian Brauner
2019-07-27 18:24   ` Yann Droneaud
2019-08-06 18:03     ` Suren Baghdasaryan
2019-08-06 17:51   ` Christian Brauner
2019-08-06 17:50 ` [PATCH v3 1/2] tests: move common definitions and functions into pidfd.h Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).