From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 150CAD2444F for ; Thu, 10 Oct 2024 23:16:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 576536B0083; Thu, 10 Oct 2024 19:16:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 526796B0088; Thu, 10 Oct 2024 19:16:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3EDA86B0089; Thu, 10 Oct 2024 19:16:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 22CEA6B0083 for ; Thu, 10 Oct 2024 19:16:27 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D11C31602FB for ; Thu, 10 Oct 2024 23:16:22 +0000 (UTC) X-FDA: 82659253572.14.5D31813 Received: from mail-io1-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) by imf08.hostedemail.com (Postfix) with ESMTP id 4BDE016000E for ; Thu, 10 Oct 2024 23:16:23 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=google header.b=SSohDz1E; spf=pass (imf08.hostedemail.com: domain of skhan@linuxfoundation.org designates 209.85.166.54 as permitted sender) smtp.mailfrom=skhan@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728602140; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MjTbqXkgY8LUStOR5rf/WpaxQMK/Kx6NjYvjTVOb6hc=; b=Oy2ujDUXlz7cGummGbcgeHwVy3PaNcp383YBJqWvo7wroE9/z2bGwEnfl/GPgwHh9zJ5MA qES/2eT7KTO6MTeceyUmwPQtoJlzDZXUrjmJRY+5yatVTBybqgjTQ6Q7+vL5XHD2mjC92M flE/w9sLzI30FxEBbzvNmEOXaJIWKek= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=google header.b=SSohDz1E; spf=pass (imf08.hostedemail.com: domain of skhan@linuxfoundation.org designates 209.85.166.54 as permitted sender) smtp.mailfrom=skhan@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728602140; a=rsa-sha256; cv=none; b=Ob4ov3MkTjDiBixYvAuLR8oYJR8H+Vm2McJZ3F5mpRcRWMocZJCxNJkmcnLlaaKWc7JaoV D58WFSGvPhjUIS92ok6wUaKFRAVdwhsKJM8wg4Py0kcsOn8Gamu8gf5sKsYpcgJUoWUqhI QZRFqtHpnGGW0zrM82foMP3s3sHzAjA= Received: by mail-io1-f54.google.com with SMTP id ca18e2360f4ac-835453714cfso64367239f.1 for ; Thu, 10 Oct 2024 16:16:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; t=1728602183; x=1729206983; darn=kvack.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=MjTbqXkgY8LUStOR5rf/WpaxQMK/Kx6NjYvjTVOb6hc=; b=SSohDz1EB+LXD0WEzAtmr8sUiAtp2ygBE1iGva4omrXyJ3swaHg7CBzxsEbHvLw5zT /aKQ40ndRLJs893V9qHXmXi6JvaTgAxJQMy0bBnGkHF4fMIw+KqgwqVsRb2qln7KYBy8 88Lo8nguufJbHrO5DyRudU80xy7whPAsrF7+Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728602183; x=1729206983; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MjTbqXkgY8LUStOR5rf/WpaxQMK/Kx6NjYvjTVOb6hc=; b=cXHgNVlg1S6yiEFqGHZiscPIn7RSnx3QKe5CKeenl4n7hzzUY+WfNQPcsQgpQcyEWX 05gqgtGi+ds4NR8lCkN2M8u9jV6bVsok53ilFpoM9YUw5p+lc1Q9F1eYuua5qkm4iqsY RLgUdXeAObVPSPaiKSkqg2RH4q0WiwL5ntL7Wk7Si57TKFWwdd7KJFEK+mxmwCm+HhZb q2JzSX4e8+4lMyyoZuXS4gFXhTbIwcclr7cgt9fvD1B82UgHHYxKdvmTiwWk+LUV+UwY uX2CcYiHMNvrpjIa/O6dcMryg/oUYQniZJ+TrzJnkLeZX9NstX9+fMPX/qv3Wv/LP15V /59A== X-Forwarded-Encrypted: i=1; AJvYcCUbZZe1kRMKN2x5trFUORlX0rrgoVdRG9WZPfkQPX3Iy8q9RT1o9hKlaryBlo3J7kIAjLeBP8fhKg==@kvack.org X-Gm-Message-State: AOJu0YyICesMT3CbyyMuB5VJ+FZ4pQSFC73txUjVSzDWUC2+E7d7tLRg adchoiPBW6cUDWL24VtMPDXGcFyUBZEug5efg1stYBa0ggHPjed7eKZAy3lMBqw= X-Google-Smtp-Source: AGHT+IFlVbQGSILLXeO5nvykYOl8M4yjUO6AwPJGeeduUP5DYUvTqoxodwU5XfkR5e84yQ696UHUgw== X-Received: by 2002:a05:6602:6413:b0:835:359b:8a07 with SMTP id ca18e2360f4ac-837952203bdmr45601839f.16.1728602183521; Thu, 10 Oct 2024 16:16:23 -0700 (PDT) Received: from [192.168.1.128] ([38.175.170.29]) by smtp.gmail.com with ESMTPSA id ca18e2360f4ac-8354b8dfcd1sm45225439f.3.2024.10.10.16.16.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Oct 2024 16:16:23 -0700 (PDT) Message-ID: <1d1190be-f74f-45ab-ac6c-2251d0bec1bc@linuxfoundation.org> Date: Thu, 10 Oct 2024 17:16:22 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 3/3] selftests: pidfd: add tests for PIDFD_SELF_* To: Lorenzo Stoakes , Christian Brauner Cc: Shuah Khan , "Liam R . Howlett" , Suren Baghdasaryan , Vlastimil Babka , pedro.falcato@gmail.com, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, Shuah Khan References: <8917d809e1509c4e0bce02436a493db29e2115b3.1728578231.git.lorenzo.stoakes@oracle.com> Content-Language: en-US From: Shuah Khan In-Reply-To: <8917d809e1509c4e0bce02436a493db29e2115b3.1728578231.git.lorenzo.stoakes@oracle.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspam-User: X-Stat-Signature: sr6pkquq4rao8zhd8ytkgzdjgzrq8o6d X-Rspamd-Queue-Id: 4BDE016000E X-Rspamd-Server: rspam11 X-HE-Tag: 1728602183-916453 X-HE-Meta: U2FsdGVkX1+ev9iYbq7XjTEdw98QH0uJF1dHF0tNAcW3XL+EFCUL/H+ShRKl/qqc1Qyv7WuPQtlGIdXUwfMqBdX2M7X4L0Sk48N8HWsvaGx/LYn+/VfQQe80w/GvROkB7zeI/pWml1d71KBirJ9jGQIIqhaNpt4ooposLIHCeCojafwdv9pPaUlct4iBBDraWVcTizChjaJMZnxqtHHZtCheLTIfKsg1kXoA3pvsk15DlQ3ZHfA5630xNpUEOB05s2j/rVLt88dkJ+g+fKv9Mcg8aXfuVCP3GNHjy6xfhD57hFK9e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4UhYOm1PbK2fnoroVSSBoRZ1Wm5TgZcJphg3bv6UkOt5ese1/DnwCw5GdAdRynM9kYVsOijwRkeMsaElreuQY+Pd2wul1vVyHb54m32fiSMg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 10/10/24 12:15, Lorenzo Stoakes wrote: > Add tests to assert that PIDFD_SELF_* correctly refers to the current > thread and process. > > This is only practically meaningful to pidfd_send_signal() and > pidfd_getfd(), but also explicitly test that we disallow this feature for > setns() where it would make no sense. > > We cannot reasonably wait on ourself using waitid(P_PIDFD, ...) so while in > theory PIDFD_SELF_* would work here, we'd be left blocked if we tried it. > > We defer testing of mm-specific functionality which uses pidfd, namely > process_madvise() and process_mrelease() to mm testing (though note the > latter can not be sensibly tested as it would require the testing process > to be dying). > > Signed-off-by: Lorenzo Stoakes > --- > tools/testing/selftests/pidfd/pidfd.h | 8 ++ > .../selftests/pidfd/pidfd_getfd_test.c | 136 ++++++++++++++++++ > .../selftests/pidfd/pidfd_setns_test.c | 11 ++ > tools/testing/selftests/pidfd/pidfd_test.c | 67 +++++++-- > 4 files changed, 213 insertions(+), 9 deletions(-) > > diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftests/pidfd/pidfd.h > index 88d6830ee004..1640b711889b 100644 > --- a/tools/testing/selftests/pidfd/pidfd.h > +++ b/tools/testing/selftests/pidfd/pidfd.h > @@ -50,6 +50,14 @@ > #define PIDFD_NONBLOCK O_NONBLOCK > #endif > > +/* System header file may not have this available. */ > +#ifndef PIDFD_SELF_THREAD > +#define PIDFD_SELF_THREAD -100 > +#endif > +#ifndef PIDFD_SELF_THREAD_GROUP > +#define PIDFD_SELF_THREAD_GROUP -200 > +#endif > + Can't we pick these up from linux/pidfd.h - patch 2/3 adds them. > /* > * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c > * That means, when it wraps around any pid < 300 will be skipped. > diff --git a/tools/testing/selftests/pidfd/pidfd_getfd_test.c b/tools/testing/selftests/pidfd/pidfd_getfd_test.c > index cd51d547b751..10793fc845ed 100644 > --- a/tools/testing/selftests/pidfd/pidfd_getfd_test.c > +++ b/tools/testing/selftests/pidfd/pidfd_getfd_test.c > @@ -6,6 +6,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -15,6 +16,7 @@ > #include > #include > #include > +#include > #include > #include > > @@ -114,6 +116,89 @@ static int child(int sk) > return ret; > } > > +static int __pidfd_self_thread_worker(unsigned long page_size) > +{ > + int memfd; > + int newfd; > + char *ptr; > + int ret = 0; > + > + /* > + * Unshare our FDs so we have our own set. This means > + * PIDFD_SELF_THREAD_GROUP will fail. > + */ > + if (unshare(CLONE_FILES) < 0) { > + ret = -errno; > + goto exit; > + } > + > + /* Truncate, map in and write to our memfd. */ > + memfd = sys_memfd_create("test_self_child", 0); Missing eror check. > + if (ftruncate(memfd, page_size)) { > + ret = -errno; > + goto exit; Hmm. you probably need scoped cleanup paths. "exit" closes memfd and newfd which isn't open yet and sys_memfd_create() could fail and memfd doesn't need closing? > + } > + > + ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, > + MAP_SHARED, memfd, 0); > + if (ptr == MAP_FAILED) { > + ret = -errno; > + goto exit; > + } > + ptr[0] = 'y'; > + if (munmap(ptr, page_size)) { > + ret = -errno; > + goto exit; > + } > + > + /* Get a thread-local duplicate of our memfd. */ > + newfd = sys_pidfd_getfd(PIDFD_SELF_THREAD, memfd, 0); > + if (newfd < 0) { > + ret = -errno; > + goto exit; Same comment here - "exit" closes newfd > + } > + > + if (memfd == newfd) { > + ret = -EINVAL; > + goto exit; > + } > + > + /* Map in new fd and make sure that the data is as expected. */ > + ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, > + MAP_SHARED, newfd, 0); > + if (ptr == MAP_FAILED) { > + ret = -errno; > + goto exit; > + } > + > + if (ptr[0] != 'y') { > + ret = -EINVAL; > + goto exit; > + } > + > + if (munmap(ptr, page_size)) { > + ret = -errno; > + goto exit; > + } > + > +exit: > + /* Cleanup. */ > + close(newfd); > + close(memfd); > + > + return ret; > +} > + > +static void *pidfd_self_thread_worker(void *arg) > +{ > + unsigned long page_size = (unsigned long)arg; > + int ret; > + > + ret = __pidfd_self_thread_worker(page_size); Don't you want to check error here? > + > + return (void *)(intptr_t)ret; > +} > + > FIXTURE(child) > { > /* > @@ -264,6 +349,57 @@ TEST_F(child, no_strange_EBADF) > EXPECT_EQ(errno, ESRCH); > } > > +TEST(pidfd_self) > +{ > + int memfd = sys_memfd_create("test_self", 0); > + unsigned long page_size = sysconf(_SC_PAGESIZE); > + int newfd; > + char *ptr; > + pthread_t thread; > + void *res; > + int err; > + > + ASSERT_GE(memfd, 0); > + ASSERT_EQ(ftruncate(memfd, page_size), 0); > + > + /* > + * Map so we can assert that the duplicated fd references the same > + * memory. > + */ > + ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, > + MAP_SHARED, memfd, 0); > + ASSERT_NE(ptr, MAP_FAILED); > + ptr[0] = 'x'; > + ASSERT_EQ(munmap(ptr, page_size), 0); > + > + /* Now get a duplicate of our memfd. */ > + newfd = sys_pidfd_getfd(PIDFD_SELF_THREAD_GROUP, memfd, 0); > + ASSERT_GE(newfd, 0); > + ASSERT_NE(memfd, newfd); > + > + /* Now map duplicate fd and make sure it references the same memory. */ > + ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, > + MAP_SHARED, newfd, 0); > + ASSERT_NE(ptr, MAP_FAILED); > + ASSERT_EQ(ptr[0], 'x'); > + ASSERT_EQ(munmap(ptr, page_size), 0); > + > + /* Cleanup. */ > + close(memfd); > + close(newfd); > + > + /* > + * Fire up the thread and assert that we can lookup the thread-specific > + * PIDFD_SELF_THREAD (also aliased by PIDFD_SELF). > + */ > + ASSERT_EQ(pthread_create(&thread, NULL, pidfd_self_thread_worker, > + (void *)page_size), 0); > + ASSERT_EQ(pthread_join(thread, &res), 0); > + err = (int)(intptr_t)res; > + > + ASSERT_EQ(err, 0); > +} > + > #if __NR_pidfd_getfd == -1 > int main(void) > { > diff --git a/tools/testing/selftests/pidfd/pidfd_setns_test.c b/tools/testing/selftests/pidfd/pidfd_setns_test.c > index 7c2a4349170a..bbd39dc5ceb7 100644 > --- a/tools/testing/selftests/pidfd/pidfd_setns_test.c > +++ b/tools/testing/selftests/pidfd/pidfd_setns_test.c > @@ -752,4 +752,15 @@ TEST(setns_einval) > close(fd); > } > > +TEST(setns_pidfd_self_disallowed) > +{ > + ASSERT_EQ(setns(PIDFD_SELF_THREAD, 0), -1); > + EXPECT_EQ(errno, EBADF); > + > + errno = 0; > + > + ASSERT_EQ(setns(PIDFD_SELF_THREAD_GROUP, 0), -1); > + EXPECT_EQ(errno, EBADF); > +} > + > TEST_HARNESS_MAIN > diff --git a/tools/testing/selftests/pidfd/pidfd_test.c b/tools/testing/selftests/pidfd/pidfd_test.c > index 9faa686f90e4..ab5caa0368a1 100644 > --- a/tools/testing/selftests/pidfd/pidfd_test.c > +++ b/tools/testing/selftests/pidfd/pidfd_test.c > @@ -42,12 +42,41 @@ static pid_t pidfd_clone(int flags, int *pidfd, int (*fn)(void *)) > #endif > } > > -static int signal_received; > +static pthread_t signal_received; > > static void set_signal_received_on_sigusr1(int sig) > { > if (sig == SIGUSR1) > - signal_received = 1; > + signal_received = pthread_self(); > +} > + > +static int send_signal(int pidfd) > +{ > + int ret = 0; > + > + if (sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0) < 0) { > + ret = -EINVAL; > + goto exit; > + } > + > + if (signal_received != pthread_self()) { > + ret = -EINVAL; > + goto exit; > + } > + > +exit: > + signal_received = 0; > + return ret; > +} > + > +static void *send_signal_worker(void *arg) > +{ > + int pidfd = (int)(intptr_t)arg; > + int ret; > + > + ret = send_signal(pidfd); > + Same here - don't you have to check ret? > + return (void *)(intptr_t)ret; > } > > /* > @@ -56,8 +85,11 @@ static void set_signal_received_on_sigusr1(int sig) > */ > static int test_pidfd_send_signal_simple_success(void) > { > - int pidfd, ret; > + int pidfd; > const char *test_name = "pidfd_send_signal send SIGUSR1"; > + pthread_t thread; > + void *thread_res; > + int res; > > if (!have_pidfd_send_signal) { > ksft_test_result_skip( > @@ -74,17 +106,34 @@ static int test_pidfd_send_signal_simple_success(void) > > signal(SIGUSR1, set_signal_received_on_sigusr1); > > - ret = sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0); > + send_signal(pidfd); > close(pidfd); > - if (ret < 0) > - ksft_exit_fail_msg("%s test: Failed to send signal\n", > + > + /* Now try the same thing only using PIDFD_SELF_THREAD_GROUP. */ > + res = send_signal(PIDFD_SELF_THREAD_GROUP); > + if (res) > + ksft_exit_fail_msg( > + "%s test: Error %d on PIDFD_SELF_THREAD_GROUP signal\n", > + test_name, res); > + > + /* > + * Now try the same thing in a thread and assert thread ID is equal to > + * worker thread ID. > + */ > + if (pthread_create(&thread, NULL, send_signal_worker, > + (void *)(intptr_t)PIDFD_SELF_THREAD)) > + ksft_exit_fail_msg("%s test: Failed to create thread\n", > test_name); > > - if (signal_received != 1) > - ksft_exit_fail_msg("%s test: Failed to receive signal\n", > + if (pthread_join(thread, &thread_res)) > + ksft_exit_fail_msg("%s test: Failed to join thread\n", > test_name); > + res = (int)(intptr_t)thread_res; > + if (res) > + ksft_exit_fail_msg( > + "%s test: Error %d on PIDFD_SELF_THREAD signal\n", > + test_name, res); > > - signal_received = 0; > ksft_test_result_pass("%s test: Sent signal\n", test_name); > return 0; > } thanks, -- Shuah