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 7CCA1C02192 for ; Wed, 5 Feb 2025 09:29:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D25528002A; Wed, 5 Feb 2025 04:29:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 07FC4280028; Wed, 5 Feb 2025 04:29:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E611628002A; Wed, 5 Feb 2025 04:29:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C9784280028 for ; Wed, 5 Feb 2025 04:29:14 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8C2B11A08C9 for ; Wed, 5 Feb 2025 09:29:14 +0000 (UTC) X-FDA: 83085367428.17.B6813DC Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf24.hostedemail.com (Postfix) with ESMTP id D3070180010 for ; Wed, 5 Feb 2025 09:29:12 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eg6yHtoI; spf=pass (imf24.hostedemail.com: domain of brauner@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=brauner@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738747753; 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=dd1hGzjRT4579yLaFejwIwfRuRaiw71TNsVh8T0Mid4=; b=ZBVD1t1DAE9/nbsyyyhKktl3+2Jv9tK85x44kKvLMQKdMKGe2hAR1aq1aQLyo+e2dvAA3D e/GCn5oWi+o2TZvu/AVstp3hMqlGGPjDrrI0kYkdvAk4HHykFcXNJZ72OwqpoX3ChMDdhG N2DD+78o6vD60l2C8GEfNxbvk2+tCkI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738747753; a=rsa-sha256; cv=none; b=zGUjeVPTpi3TIeh5NFp1qbFnQKCPpWWTaPTcYhf7yQA0Yzpjdpd/hYYjk5mBC5u/4TFWne 0cmK5u6AbQsX0YYCCZuMXZGp0J9WJ6GDn107POBkN8ONFbcqzOJgcTtRO4xP5Z0Q9FexEN 4V1Ao30/49q9vRv1RlspGCF0lteOaFo= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eg6yHtoI; spf=pass (imf24.hostedemail.com: domain of brauner@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=brauner@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 0299C5C609F; Wed, 5 Feb 2025 09:28:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E8972C4CEE3; Wed, 5 Feb 2025 09:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738747751; bh=Ms21WWQxt0hgCHNGn7VoI88OcIK4i02A5VsFEvG24mk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eg6yHtoIaP23lOBbUoIZktLdaXSm0/rQFB+Gu8dr+q6+oDohpMbSz2yg63hKd5qcR JIEQigQOx6zSCL8PfwFDkT74//d+oqTy+g/WnEeE9UP5rDeXqV9BLxGh+S9eSB5FCQ 0EH4OaodiU249ytvGjb03wGCsyxxcB6ysVIfWcaagRWsD3RQE40qXgte1c/SSodkak STW8sPJ3yqHfZTCbEMSHiStohjiCgW6+p701EXd/PQ3EyNGW8ToP6boCW+4mBJYH6M 9GZOR6yUYN177p9P2oqWDE9hTWfwmjDylNbdW650fOmV8bB+tmA0ieFWuJ9AWuGJmk SqgS9GzJbdTnQ== Date: Wed, 5 Feb 2025 10:29:03 +0100 From: Christian Brauner To: Suren Baghdasaryan Cc: Lorenzo Stoakes , Shuah Khan , "Liam R . Howlett" , 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, Oliver Sang , John Hubbard , Tejun Heo , Johannes Weiner , Michal Koutny , Andrew Morton , Shakeel Butt , Elliott Hughes Subject: Re: [PATCH v7 0/6] introduce PIDFD_SELF* sentinels Message-ID: <20250205-gewahrsam-einnahmen-9b580054f501@brauner> References: <20250204-joggen-buddeln-29e5ca75abb7@brauner> <7a8a1719-466f-4e10-b1eb-9e9e1ef8ad52@lucifer.local> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Queue-Id: D3070180010 X-Stat-Signature: zo5npcof67phhqod4n8kihwkgontwgn4 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1738747752-699675 X-HE-Meta: U2FsdGVkX1/DkXg7ZsXOmtNBv7N9++Fw+xmgw1bQJCOwNVE8ZeidwNsCP3xUF6Q+GNdtJC9xQqhVbH7Cm0uDOHVLUfxU3R8miFCr3AwCbt2yhIfdD/fP3a+tAhShkBexQrD0cQxn3ibksyk4j7CyQ5zNH86+aVLrYw65GjTpZ8+Aj9YCcLz9cqHR4WFWJFrHYu2RCgymXx0NXsREcddlu3D5/4qqZ7Jr3f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qW10lUA+DtEBYa4wsHkLm1u0J/LYtLKYrCNMR3kHF0JdWfm6CYVfj4Tr7gn39WyvaCAFUfWyrs+7CzDaWbojPdC1YN5c9Q1pEUEZX1f9g/6KcQ6UL0IUfPcMqanHUOW4Eil7Tba6pVbknXpzVQ+vVLmxASqNPnoJARNPzSPod7PW7SWnQ== 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 Tue, Feb 04, 2025 at 09:43:31AM -0800, Suren Baghdasaryan wrote: > On Tue, Feb 4, 2025 at 2:01 AM Lorenzo Stoakes > wrote: > > > > On Tue, Feb 04, 2025 at 10:46:35AM +0100, Christian Brauner wrote: > > > On Thu, 30 Jan 2025 20:40:25 +0000, Lorenzo Stoakes wrote: > > > > If you wish to utilise a pidfd interface to refer to the current process or > > > > thread it is rather cumbersome, requiring something like: > > > > > > > > int pidfd = pidfd_open(getpid(), 0 or PIDFD_THREAD); > > > > > > > > ... > > > > > > > > [...] > > > > > > Updated merge message. I've slightly rearranged pidfd_send_signal() so > > > we don't have to call CLASS(fd, f)(pidfd) unconditionally anymore. > > > > Sounds good and thank you! Glad to get this in :) > > Sorry, a bit late to the party... > > We were discussing MADV_GUARD_INSTALL use with Android Bionic team and > the possibility of caching pidfd_open() result for reuse when > installing multiple guards, however doing that in libraries would pose > issues as we can't predict the user behavior, which can fork() in > between such calls. That would be an additional reason why having > these sentinels is beneficial. Ok, added this to the cover letter as well. Note that starting with v6.14 pidfs supports file handles. This works because pidfs provides each pidfd with a unique 64bit inode number that is exposed in statx(). On 64-bit the ->st_ino simply is the inode number. On 32-bit the unique identifier can be reconstructed using ->st_ino and the inode generation number which can be retrieved via the FS_IOC_GETVERSION ioctl. So the 64-bit identifier on 32-bit is reconstructed by using ->st_ino as the lower 32-bits and the 32-bit generation number as the upper 32-bits. Also note that since the introduction of pidfs each struct pid will refer to a different inode but the same struct pid will refer to the same inode if it's opened multiple times. In contrast to pre-pidfs pidfds where each struct pid refered to the same inode. IOW, with pidfs statx() is sufficient to compare to pidfds whether they refer to the same process. On 64-bit it's sufficient to do the usual st1->st_dev == st2->st_dev && st1->st_ino == st2->st_ino and on 32-bit you will want to also compare the generation number: TEST_F(pidfd_bind_mount, reopen) { int pidfd; char proc_path[PATH_MAX]; sprintf(proc_path, "/proc/self/fd/%d", self->pidfd); pidfd = open(proc_path, O_RDONLY | O_NOCTTY | O_CLOEXEC); ASSERT_GE(pidfd, 0); ASSERT_GE(fstat(self->pidfd, &self->st2), 0); ASSERT_EQ(ioctl(self->pidfd, FS_IOC_GETVERSION, &self->gen2), 0); ASSERT_TRUE(self->st1.st_dev == self->st2.st_dev && self->st1.st_ino == self->st2.st_ino); ASSERT_TRUE(self->gen1 == self->gen2); ASSERT_EQ(close(pidfd), 0); } Plus, you can bind-mount them now. In any case, this allows us to create file handles that are unique for the lifetime of the system. Please see tools/testing/selftests/pidfd/pidfd_file_handle_test.c for how that works. The gist is that decoding and encoding for pidfs is unprivileged and the only requirement we have is that the process the file handle resolves to must be valid in the caller's pid namespace hierarchy: TEST_F(file_handle, file_handle_child_pidns) { int mnt_id; struct file_handle *fh; int pidfd = -EBADF; struct stat st1, st2; fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ); ASSERT_NE(fh, NULL); memset(fh, 0, sizeof(struct file_handle) + MAX_HANDLE_SZ); fh->handle_bytes = MAX_HANDLE_SZ; ASSERT_EQ(name_to_handle_at(self->child_pidfd2, "", fh, &mnt_id, AT_EMPTY_PATH), 0); ASSERT_EQ(fstat(self->child_pidfd2, &st1), 0); pidfd = open_by_handle_at(self->pidfd, fh, 0); ASSERT_GE(pidfd, 0); ASSERT_EQ(fstat(pidfd, &st2), 0); ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); ASSERT_EQ(close(pidfd), 0); pidfd = open_by_handle_at(self->pidfd, fh, O_CLOEXEC); ASSERT_GE(pidfd, 0); ASSERT_EQ(fstat(pidfd, &st2), 0); ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); ASSERT_EQ(close(pidfd), 0); pidfd = open_by_handle_at(self->pidfd, fh, O_NONBLOCK); ASSERT_GE(pidfd, 0); ASSERT_EQ(fstat(pidfd, &st2), 0); ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); ASSERT_EQ(close(pidfd), 0); free(fh); } So you don't need to keep the fd open. > > > > > > > > > > --- > > > > > > Applied to the vfs-6.15.pidfs branch of the vfs/vfs.git tree. > > > Patches in the vfs-6.15.pidfs branch should appear in linux-next soon. > > > > > > Please report any outstanding bugs that were missed during review in a > > > new review to the original patch series allowing us to drop it. > > > > > > It's encouraged to provide Acked-bys and Reviewed-bys even though the > > > patch has now been applied. If possible patch trailers will be updated. > > > > > > Note that commit hashes shown below are subject to change due to rebase, > > > trailer updates or similar. If in doubt, please check the listed branch. > > > > > > tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git > > > branch: vfs-6.15.pidfs > > > > > > [1/6] pidfd: add PIDFD_SELF* sentinels to refer to own thread/process > > > https://git.kernel.org/vfs/vfs/c/e6e4ed42f8d8 > > > [2/6] selftests/pidfd: add missing system header imcludes to pidfd tests > > > https://git.kernel.org/vfs/vfs/c/c9f04f4a251d > > > [3/6] tools: testing: separate out wait_for_pid() into helper header > > > https://git.kernel.org/vfs/vfs/c/fb67fe44116e > > > [4/6] selftests: pidfd: add pidfd.h UAPI wrapper > > > https://git.kernel.org/vfs/vfs/c/ac331e56724d > > > [5/6] selftests: pidfd: add tests for PIDFD_SELF_* > > > https://git.kernel.org/vfs/vfs/c/881a3515c191 > > > [6/6] selftests/mm: use PIDFD_SELF in guard pages test > > > https://git.kernel.org/vfs/vfs/c/b4703f056f42