From patchwork Fri Jul 26 16:22:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 11061363 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4516B1398 for ; Fri, 26 Jul 2019 16:22:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 329A328968 for ; Fri, 26 Jul 2019 16:22:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 25DDD28B1E; Fri, 26 Jul 2019 16:22:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AFF8128968 for ; Fri, 26 Jul 2019 16:22:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727771AbfGZQWc (ORCPT ); Fri, 26 Jul 2019 12:22:32 -0400 Received: from mail-pl1-f201.google.com ([209.85.214.201]:46069 "EHLO mail-pl1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727616AbfGZQWc (ORCPT ); Fri, 26 Jul 2019 12:22:32 -0400 Received: by mail-pl1-f201.google.com with SMTP id y9so28718311plp.12 for ; Fri, 26 Jul 2019 09:22:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=tjhcSRmIRXUILRS/jy/QiJdV3Z5E1s7xQzD5RJUqH7Q=; b=UO4hLjUItbB6ULBWQOmwqIJJ0t3923sVzDDDeStO7mSA/Bh+vF+0trnk/xlQZkqI5C iwYyPjMiVPlMRR5k+6Hrq87bXuZjU3ZDHWJSBrvBn0Ez1BWq6Rc9z0Ys3mLzmdFxg25y duGoe/AzBtiUob6yFfepgE9R9RJ0LITqZpkSpPt8YAfB2ZPGq1tjefoHVcNcwUqg9l7U Od5f5FjeOMOVeX5z4wWU8swRAndpFG7hfnKGzp/yOUAzC3sBjyITLPaYlUI8uUTOnwDt LV6seuE0PvYIcvQJkpR7uceH3Xr9cOOomgV23HvZjLFmQZdHjT0AMWLagTetoWDrTUxC OKcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=tjhcSRmIRXUILRS/jy/QiJdV3Z5E1s7xQzD5RJUqH7Q=; b=tZpI19Prfs1bRzqHOIFQqNh710XPof/DK0U2Dq2g53e2xXxt473KWS+ZD8ynUnwmg3 IkNYdEO7tAqwx3nhkHKigVUvaBV0956o11btm+iE4LRf4LxsbZ626ZCQppd5WGXRZ16W fbwMQoAxzEIovBiqAq6JaKv8+Q5kBzAXeQ5MkzN5i9AaWk4kx09bsNtZIyc/kx3SJiXM +h2HC52ZzvKgCvCJ+ZvIhyX4tBxgdY8E7JBH6X7TftJO9bZF0fDmBbdFqKU1lI+hyc3h tLvwPviDGBiWAXm+14JIChNT+9sxgEzR8CjE1h+vgvm/YaTFV+B2JUmZ+FGuuiQW9wqM EMEg== X-Gm-Message-State: APjAAAVh0aACsCL30tgnjBi9PVtHrPgHkux6KjBHyaWFlHa21VOMU+Wc SzbNVRvUAfTBxf9kqujXLa/t1W01Ur0= X-Google-Smtp-Source: APXvYqwRGZjimDoN0Ifg4KsikRTFzC8uTbFRXzYuLY+8ZFeCLXhnSEodDmSJP55fWDQxYndQvdCSxD1e+sQ= X-Received: by 2002:a65:4808:: with SMTP id h8mr91147086pgs.22.1564158150843; Fri, 26 Jul 2019 09:22:30 -0700 (PDT) Date: Fri, 26 Jul 2019 09:22:25 -0700 Message-Id: <20190726162226.252750-1-surenb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.709.g102302147b-goog Subject: [PATCH v3 1/2] tests: move common definitions and functions into pidfd.h From: Suren Baghdasaryan To: surenb@google.com Cc: christian@brauner.io, arnd@arndb.de, ebiederm@xmission.com, keescook@chromium.org, joel@joelfernandes.org, dancol@google.com, tglx@linutronix.de, jannh@google.com, dhowells@redhat.com, mtk.manpages@gmail.com, luto@kernel.org, akpm@linux-foundation.org, oleg@redhat.com, cyphar@cyphar.com, torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@android.com Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Move definitions and functions used across different pidfd tests into pidfd.h header. Signed-off-by: Suren Baghdasaryan Reviewed-by: Christian Brauner --- 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) From patchwork Fri Jul 26 16:22:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 11061365 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CEE171398 for ; Fri, 26 Jul 2019 16:22:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BB6DB28B1E for ; Fri, 26 Jul 2019 16:22:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AA3C528B1F; Fri, 26 Jul 2019 16:22:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2F3AD28ACB for ; Fri, 26 Jul 2019 16:22:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727646AbfGZQWe (ORCPT ); Fri, 26 Jul 2019 12:22:34 -0400 Received: from mail-pl1-f201.google.com ([209.85.214.201]:37103 "EHLO mail-pl1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727924AbfGZQWe (ORCPT ); Fri, 26 Jul 2019 12:22:34 -0400 Received: by mail-pl1-f201.google.com with SMTP id n4so28201601plp.4 for ; Fri, 26 Jul 2019 09:22:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=lPOpgqHnscdwzLGtC0H1Ixwb+B+NAOxyqgNtT/tbhC8=; b=F4aIvxS/xG+ArYWWgs9p5OW3h6Ysy911syFIR4ahzJKxhX5vtAkkF7lgL9AVuhNuKA vGsqO0mxxT0UwIisuM9tG/GSMfBO5M0qB1OOOP1rIhIB13JcZcpU402EpxPrgSt2w9Po 0nhzft0hVquavaHsMph5/v8pUDeXegkxdSX0/bo4wrOmpVq/+EgcrXDvYqw6DrfRg3d3 8tzRhZ/eWISrO4py4GGx1YwBnPyqzsGSdAU7n6/eG6uEGT22aYQ4Wxxw8/FebDyE3Fxp Bnx+OSteztSCR/JMHtQVvrUb784u7YobfmhceKH3H3E0iWJAN5gx1tHqMrbgKM5SlxGG ytAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=lPOpgqHnscdwzLGtC0H1Ixwb+B+NAOxyqgNtT/tbhC8=; b=qzniOYkDeaEPNeu2rRFg7hlibfUIoNTSDu7qlgaKyXGcFE0l/8x99tSjz2rNNgTpBL X8+l4D0ZV8dcm3LRFLNLnPFZjCHTSZsfp/YatIFqfgZJvqoM44TeOUZOLyJ97hjltmI8 /4F1wlTHAdrAHacXsI9PxdDnPqwIc1HO9TNvza1PRdIIz1fmJWniSgOJpz/fUKSYljLu sDDMXU+JXaXspjO0GST4aNQQyOA6hburYZumwuhXy2c35mkRKSkZdMBfSVPWGtK4Wa7f pGQeatj2CsU4mKpSe1bQD5BUD8yGDKtE+RhzUEIHNZolyjsNzKccqLT+pZvBUYJuWDOr yM0A== X-Gm-Message-State: APjAAAUZUw9tuBsDMeTIDd5R4MAOrmAcbfwGCQWKF3vZ6vtemTpgKNxN v3U7+guX3XK2U3x4674g/6n93/pNmN4= X-Google-Smtp-Source: APXvYqweliflgWhX3WB84ijGbSSbRjjNArh+t7qP7CvGNxSJEHL3aYOnPpXzOC/3A0B1KYVKh6Bw5Pien4k= X-Received: by 2002:a63:e213:: with SMTP id q19mr90893338pgh.180.1564158153637; Fri, 26 Jul 2019 09:22:33 -0700 (PDT) Date: Fri, 26 Jul 2019 09:22:26 -0700 In-Reply-To: <20190726162226.252750-1-surenb@google.com> Message-Id: <20190726162226.252750-2-surenb@google.com> Mime-Version: 1.0 References: <20190726162226.252750-1-surenb@google.com> X-Mailer: git-send-email 2.22.0.709.g102302147b-goog Subject: [PATCH v3 2/2] tests: add pidfd poll tests From: Suren Baghdasaryan To: surenb@google.com Cc: christian@brauner.io, arnd@arndb.de, ebiederm@xmission.com, keescook@chromium.org, joel@joelfernandes.org, dancol@google.com, tglx@linutronix.de, jannh@google.com, dhowells@redhat.com, mtk.manpages@gmail.com, luto@kernel.org, akpm@linux-foundation.org, oleg@redhat.com, cyphar@cyphar.com, torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, linux-api@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@android.com Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Reviewed-by: Joel Fernandes (Google) --- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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(); +}