Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1805786rdb; Thu, 7 Dec 2023 09:11:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IGndIqLQB7PSjBjGBNBabYhIYllk2X8pFN/Et1kYw821vPJvum0p242lB50Q2/XuuNsmTKg X-Received: by 2002:a05:6a00:2288:b0:6ce:2731:a095 with SMTP id f8-20020a056a00228800b006ce2731a095mr3036485pfe.68.1701969073787; Thu, 07 Dec 2023 09:11:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701969073; cv=none; d=google.com; s=arc-20160816; b=PsnWR6y05rJVQGtHT1TWDpUPQy5TOYj7rtZM1CiILPtnvbQrH6yS/uHVxY5jTuT0qq d3YAlhapWj8gCXosNUQK2uS1/F7J3N7TuGc1zQ++a5pnOQTmRAftln375LFEkFDw11VY b3ijp/7dg3ZPxB5SbQl5HSrWdlC13uj4H5TKe0ndLIHsjqwMnGcCEx4I41XQfiDcVIr2 CFu2nGlBlceCK0VOiC/wYV/ZwQtjJSFKpyyEgn1ibcl8rYr0FDvBoOdYQWtakC/YGOlj debE3XbzcRfQQ6B7dwGdXgb7cNgDHIPcJymHr+6SfqwG2vHMBQcQHYh/5GLUNtTx1HYd pTEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=WrVPqtASwJJxG2xCWlOFsHZ6qIqzH0f1AOqfJ5algyU=; fh=cqt7tYfUjLVb4iAKPLlMbd5K0yJUwDstCHDT1mZCG8Y=; b=K+uu+9c/vgcphgRRJ6DnezG1U6oepHsoCS9QBdP8AY+9nUtB9oH8joLVG16DCdbN0d cqP5HFCycgFBmg8lXfRb7JSZeXZO4h5h6ow5CO7T9LsAYbtWiPsueAQHKs5WMPJZti4O fxD/Q2azNPu7imOB6NNY7RDiHNONHVEFPK2tuds2s2Yw5xDv79VIdM+pdWKA0tIepLFv SzgODFaOe0WFUKl66QT/VRe3O9ETjfq/a9mS3IoctzoZ7nK155MRmPhQ5Y0EFfFaXSg3 tfjt49HYp4AJsYGJCaXgjVt0kSy9msKajg1+RhsKM5GWApLkgZOW316d6wqyehG2pRgQ IxZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm3 header.b=jZjmcEto; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=j0jrPhiC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id v28-20020a63151c000000b005c6617b52e3si35906pgl.0.2023.12.07.09.11.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 09:11:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm3 header.b=jZjmcEto; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=j0jrPhiC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id F1C9080785E9; Thu, 7 Dec 2023 09:10:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443167AbjLGRJ5 (ORCPT + 99 others); Thu, 7 Dec 2023 12:09:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233045AbjLGRJz (ORCPT ); Thu, 7 Dec 2023 12:09:55 -0500 Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 645E610EB; Thu, 7 Dec 2023 09:10:01 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailout.west.internal (Postfix) with ESMTP id 546973200AC9; Thu, 7 Dec 2023 12:10:00 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Thu, 07 Dec 2023 12:10:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tycho.pizza; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701968999; x= 1702055399; bh=WrVPqtASwJJxG2xCWlOFsHZ6qIqzH0f1AOqfJ5algyU=; b=j ZjmcEtoOj30kW1cqgiWAq21aP7CzqXaAzw2w8Lv5/HvgH/Rbr6YTK4mdCx/iEwS/ l31AsQRxRIB9+7dPhTilQzthaPEo5ao1BP8bs6QvmeLuMoA6zGls3S9Dx88lm99u kEi9PCX3H0CT07vFkFvj3Gq21MzzprOMbuk3X2MwPdKrbjWpDF3UooTeMfTdBh63 yH9IDdZXij/8Gzq64evGu2RlpqGVh2EbE1vziUfE9DuUlnBtm8c5v44tbHHFvJtx 0IG5SpsPgj9LE+VrTta/7wFSoPhcl6px4Ec1l2RHUIV6ZZqPkp0su3Is6tLhB8E0 XOT6InfVS+Nhe8cDT8ZFg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701968999; x= 1702055399; bh=WrVPqtASwJJxG2xCWlOFsHZ6qIqzH0f1AOqfJ5algyU=; b=j 0jrPhiCID6orXxkMLXx1GjprPqIrBs+uorNXbuvzfn9z9b6yo2C5i32Ghn78wppM CdjwlrNZaEU/fm4QOYg7f3ikJORsbQwX2P0nYTcfBQaWjQtnuxJGh4B5rQYCXk2C K4DVwOBudeM0ePyOYDf1h79olALYsosH00ZIrEoPC9ApCg3yL6pQdQ7nDIyFMkKG AaaKUOwEi3XZc5mMJFLvbSCQt1mutiuWvJHBUtsTjDG/x7FBUaR8wD+hlcWgRcZu kjcsraNS/PgszdQoiMhCd9bqzdXo53mjq7SktOzBiB0haqIzmlGXWnB2ssQw5gZb 3sA2Pwfoj7SiVBBFcv5yA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudekfedgfeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepvfihtghh ohcutehnuggvrhhsvghnuceothihtghhohesthihtghhohdrphhiiiiirgeqnecuggftrf grthhtvghrnhepvdegffehledvleejvdethffgieefveevhfeigefffffgheeguedtieek tdeigeeunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epthihtghhohesthihtghhohdrphhiiiiirg X-ME-Proxy: Feedback-ID: i21f147d5:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 7 Dec 2023 12:09:58 -0500 (EST) From: Tycho Andersen To: Christian Brauner Cc: Oleg Nesterov , "Eric W . Biederman" , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Tycho Andersen , Tycho Andersen Subject: [PATCH v2 2/3] selftests/pidfd: add non-thread-group leader tests Date: Thu, 7 Dec 2023 10:09:45 -0700 Message-Id: <20231207170946.130823-2-tycho@tycho.pizza> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231207170946.130823-1-tycho@tycho.pizza> References: <20231207170946.130823-1-tycho@tycho.pizza> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 07 Dec 2023 09:10:24 -0800 (PST) From: Tycho Andersen This adds a family of tests for various behaviors for pidfds of non-thread-group leaders. Signed-off-by: Tycho Andersen --- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 3 +- .../selftests/pidfd/pidfd_non_tgl_test.c | 339 ++++++++++++++++++ 3 files changed, 342 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/pidfd/.gitignore b/tools/testing/selftests/pidfd/.gitignore index 973198a3ec3d..e7532e84a34a 100644 --- a/tools/testing/selftests/pidfd/.gitignore +++ b/tools/testing/selftests/pidfd/.gitignore @@ -6,3 +6,4 @@ pidfd_wait pidfd_fdinfo_test pidfd_getfd_test pidfd_setns_test +pidfd_non_tgl_test diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile index d731e3e76d5b..50e3aa9de05a 100644 --- a/tools/testing/selftests/pidfd/Makefile +++ b/tools/testing/selftests/pidfd/Makefile @@ -2,7 +2,8 @@ CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \ - pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test + pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \ + pidfd_non_tgl_test include ../lib.mk diff --git a/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c b/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c new file mode 100644 index 000000000000..e3992f2d88cf --- /dev/null +++ b/tools/testing/selftests/pidfd/pidfd_non_tgl_test.c @@ -0,0 +1,339 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" +#include "pidfd.h" + +// glibc defaults to 8MB stacks +#define STACK_SIZE (8 * 1024 * 1024) +static char stack[STACK_SIZE]; + +static int thread_sleep(void *) +{ + while (1) + sleep(100); + return 1; +} + +static int fork_task_with_thread(int (*fn)(void *), int sk_pair[2], + pid_t *tgl, pid_t *thread, int *tgl_pidfd, + int *thread_pidfd) +{ + *tgl_pidfd = *thread_pidfd = -1; + + *tgl = fork(); + if (*tgl < 0) { + perror("fork"); + return -1; + } + + if (!*tgl) { + int flags = CLONE_THREAD | CLONE_VM | CLONE_SIGHAND; + pid_t t; + + t = clone(fn, stack + STACK_SIZE, flags, sk_pair); + if (t < 0) { + perror("clone"); + exit(1); + } + + close(sk_pair[1]); + + if (write(sk_pair[0], &t, sizeof(t)) != sizeof(t)) { + perror("read"); + exit(1); + } + + // wait to get killed for various reasons by the tests. + while (1) + sleep(100); + } + + errno = 0; + if (read(sk_pair[1], thread, sizeof(*thread)) != sizeof(*thread)) { + perror("read"); + goto cleanup; + } + + *tgl_pidfd = sys_pidfd_open(*tgl, 0); + if (*tgl_pidfd < 0) { + perror("pidfd_open tgl"); + goto cleanup; + } + + *thread_pidfd = sys_pidfd_open(*thread, 0); + if (*thread_pidfd < 0) { + perror("pidfd"); + goto cleanup; + } + + return 0; + +cleanup: + kill(*tgl, SIGKILL); + if (*tgl_pidfd >= 0) + close(*tgl_pidfd); + if (*thread_pidfd >= 0) + close(*thread_pidfd); + return -1; +} + +static int test_non_tgl_basic(void) +{ + pid_t tgl, thread; + int sk_pair[2], status; + int tgl_pidfd = -1, thread_pidfd = -1; + int ret = KSFT_FAIL; + + if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair) < 0) { + ksft_print_msg("socketpair failed %s\n", strerror(errno)); + return KSFT_FAIL; + } + + if (fork_task_with_thread(thread_sleep, sk_pair, &tgl, &thread, + &tgl_pidfd, &thread_pidfd) < 0) { + return KSFT_FAIL; + } + + /* + * KILL of a thread should still kill everyone + */ + if (sys_pidfd_send_signal(thread_pidfd, SIGKILL, NULL, 0)) { + perror("pidfd_send_signal"); + goto cleanup; + } + + errno = 0; + if (waitpid(tgl, &status, 0) != tgl) { + perror("waitpid tgl"); + goto cleanup; + } + + if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL) { + ksft_print_msg("bad exit status %x\n", status); + goto cleanup; + } + + ret = KSFT_PASS; + +cleanup: + close(sk_pair[0]); + close(sk_pair[1]); + close(tgl_pidfd); + close(thread_pidfd); + return ret; +} + +static int thread_exec(void *arg) +{ + int *sk_pair = arg; + pid_t thread; + + if (read(sk_pair[0], &thread, sizeof(thread)) != sizeof(thread)) { + perror("read"); + exit(1); + } + + execlp("/bin/true", "/bin/true", NULL); + return 1; +} + +static int test_non_tgl_exec(void) +{ + pid_t tgl, thread; + int sk_pair[2]; + int tgl_pidfd = -1, thread_pidfd = -1; + int ret = KSFT_FAIL, ready; + struct pollfd pollfd; + + if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair) < 0) { + ksft_print_msg("socketpair failed %s\n", strerror(errno)); + return KSFT_FAIL; + } + + if (fork_task_with_thread(thread_exec, sk_pair, &tgl, &thread, + &tgl_pidfd, &thread_pidfd) < 0) { + return KSFT_FAIL; + } + + if (write(sk_pair[1], &thread, sizeof(thread)) != sizeof(thread)) { + perror("read"); + goto cleanup; + } + + // thread will exec(), so this pidfd should eventually be dead (i.e. + // poll should return). + pollfd.fd = thread_pidfd; + pollfd.events = POLLIN; + + ready = poll(&pollfd, 1, -1); + if (ready == -1) { + perror("poll"); + goto cleanup; + } + + if (ready != 1) { + ksft_print_msg("bad poll result %d\n", ready); + goto cleanup; + } + + if (pollfd.revents != POLLIN) { + ksft_print_msg("bad poll revents: %x\n", pollfd.revents); + goto cleanup; + } + + errno = 0; + if (sys_pidfd_getfd(thread_pidfd, 0, 0) > 0) { + ksft_print_msg("got a real fd"); + goto cleanup; + } + + if (errno != ESRCH) { + ksft_print_msg("polling invalid thread didn't give ESRCH"); + goto cleanup; + } + + ret = KSFT_PASS; + +cleanup: + close(sk_pair[0]); + close(sk_pair[1]); + close(tgl_pidfd); + close(thread_pidfd); + return ret; +} + +int thread_wait_exit(void *arg) +{ + int *sk_pair = arg; + pid_t thread; + + if (read(sk_pair[0], &thread, sizeof(thread)) != sizeof(thread)) { + perror("read"); + exit(1); + } + + return 0; +} + +static int test_non_tgl_exit(void) +{ + pid_t tgl, thread; + int sk_pair[2], status; + int tgl_pidfd = -1, thread_pidfd = -1; + int ret = KSFT_FAIL, ready; + struct pollfd pollfd; + + if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair) < 0) { + ksft_print_msg("socketpair failed %s\n", strerror(errno)); + return KSFT_FAIL; + } + + if (fork_task_with_thread(thread_wait_exit, sk_pair, &tgl, &thread, + &tgl_pidfd, &thread_pidfd) < 0) { + return KSFT_FAIL; + } + + if (write(sk_pair[1], &thread, sizeof(thread)) != sizeof(thread)) { + perror("write"); + goto cleanup; + } + + // thread will exit, so this pidfd should eventually be dead (i.e. + // poll should return). + pollfd.fd = thread_pidfd; + pollfd.events = POLLIN; + + ready = poll(&pollfd, 1, -1); + if (ready == -1) { + perror("poll"); + goto cleanup; + } + + if (ready != 1) { + ksft_print_msg("bad poll result %d\n", ready); + goto cleanup; + } + + if (pollfd.revents != POLLIN) { + ksft_print_msg("bad poll revents: %x\n", pollfd.revents); + goto cleanup; + } + + errno = 0; + if (sys_pidfd_getfd(thread_pidfd, 0, 0) > 0) { + ksft_print_msg("got a real pidfd"); + goto cleanup; + } + + if (errno != ESRCH) { + ksft_print_msg("polling invalid thread didn't give ESRCH"); + goto cleanup; + } + + close(thread_pidfd); + + // ok, but the thread group *leader* should still be alive + pollfd.fd = tgl_pidfd; + ready = poll(&pollfd, 1, 1); + if (ready == -1) { + perror("poll"); + goto cleanup; + } + + if (ready != 0) { + ksft_print_msg("polling leader returned something?! %x", pollfd.revents); + goto cleanup; + } + + ret = KSFT_PASS; + +cleanup: + kill(tgl, SIGKILL); + if (waitpid(tgl, &status, 0) != tgl) { + perror("waitpid"); + return KSFT_FAIL; + } + + return ret; +} + +#define T(x) { x, #x } +struct pidfd_non_tgl_test { + int (*fn)(); + const char *name; +} tests[] = { + T(test_non_tgl_basic), + T(test_non_tgl_exec), + T(test_non_tgl_exit), +}; +#undef T + +int main(int argc, char *argv[]) +{ + int i, ret = EXIT_SUCCESS; + + for (i = 0; i < ARRAY_SIZE(tests); i++) { + switch (tests[i].fn()) { + case KSFT_PASS: + ksft_test_result_pass("%s\n", tests[i].name); + break; + case KSFT_SKIP: + ksft_test_result_skip("%s\n", tests[i].name); + break; + default: + ret = EXIT_FAILURE; + ksft_test_result_fail("%s\n", tests[i].name); + break; + } + } + + return ret; +} -- 2.34.1