Skip to content

Commit a71f402

Browse files
committed
pidfs: get rid of __pidfd_prepare()
Fold it into pidfd_prepare() and rename PIDFD_CLONE to PIDFD_STALE to indicate that the passed pid might not have task linkage and no explicit check for that should be performed. Link: https://lore.kernel.org/[email protected] Reviewed-by: Oleg Nesterov <[email protected]> Reviewed-by: David Rheinsberg <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent fd0a109 commit a71f402

File tree

4 files changed

+44
-65
lines changed

4 files changed

+44
-65
lines changed

fs/pidfs.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ static inline bool pidfs_pid_valid(struct pid *pid, const struct path *path,
768768
{
769769
enum pid_type type;
770770

771-
if (flags & PIDFD_CLONE)
771+
if (flags & PIDFD_STALE)
772772
return true;
773773

774774
/*
@@ -777,10 +777,14 @@ static inline bool pidfs_pid_valid(struct pid *pid, const struct path *path,
777777
* pidfd has been allocated perform another check that the pid
778778
* is still alive. If it is exit information is available even
779779
* if the task gets reaped before the pidfd is returned to
780-
* userspace. The only exception is PIDFD_CLONE where no task
781-
* linkage has been established for @pid yet and the kernel is
782-
* in the middle of process creation so there's nothing for
783-
* pidfs to miss.
780+
* userspace. The only exception are indicated by PIDFD_STALE:
781+
*
782+
* (1) The kernel is in the middle of task creation and thus no
783+
* task linkage has been established yet.
784+
* (2) The caller knows @pid has been registered in pidfs at a
785+
* time when the task was still alive.
786+
*
787+
* In both cases exit information will have been reported.
784788
*/
785789
if (flags & PIDFD_THREAD)
786790
type = PIDTYPE_PID;
@@ -874,11 +878,11 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)
874878
int ret;
875879

876880
/*
877-
* Ensure that PIDFD_CLONE can be passed as a flag without
881+
* Ensure that PIDFD_STALE can be passed as a flag without
878882
* overloading other uapi pidfd flags.
879883
*/
880-
BUILD_BUG_ON(PIDFD_CLONE == PIDFD_THREAD);
881-
BUILD_BUG_ON(PIDFD_CLONE == PIDFD_NONBLOCK);
884+
BUILD_BUG_ON(PIDFD_STALE == PIDFD_THREAD);
885+
BUILD_BUG_ON(PIDFD_STALE == PIDFD_NONBLOCK);
882886

883887
ret = path_from_stashed(&pid->stashed, pidfs_mnt, get_pid(pid), &path);
884888
if (ret < 0)
@@ -887,7 +891,7 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)
887891
if (!pidfs_pid_valid(pid, &path, flags))
888892
return ERR_PTR(-ESRCH);
889893

890-
flags &= ~PIDFD_CLONE;
894+
flags &= ~PIDFD_STALE;
891895
pidfd_file = dentry_open(&path, flags, current_cred());
892896
/* Raise PIDFD_THREAD explicitly as do_dentry_open() strips it. */
893897
if (!IS_ERR(pidfd_file))

include/linux/pid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct file;
7777
struct pid *pidfd_pid(const struct file *file);
7878
struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags);
7979
struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags);
80-
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret);
80+
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret_file);
8181
void do_notify_pidfd(struct task_struct *task);
8282

8383
static inline struct pid *get_pid(struct pid *pid)

include/uapi/linux/pidfd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#define PIDFD_THREAD O_EXCL
1313
#ifdef __KERNEL__
1414
#include <linux/sched.h>
15-
#define PIDFD_CLONE CLONE_PIDFD
15+
#define PIDFD_STALE CLONE_PIDFD
1616
#endif
1717

1818
/* Flags for pidfd_send_signal(). */

kernel/fork.c

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,55 +2035,11 @@ static inline void rcu_copy_process(struct task_struct *p)
20352035
#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
20362036
}
20372037

2038-
/**
2039-
* __pidfd_prepare - allocate a new pidfd_file and reserve a pidfd
2040-
* @pid: the struct pid for which to create a pidfd
2041-
* @flags: flags of the new @pidfd
2042-
* @ret: Where to return the file for the pidfd.
2043-
*
2044-
* Allocate a new file that stashes @pid and reserve a new pidfd number in the
2045-
* caller's file descriptor table. The pidfd is reserved but not installed yet.
2046-
*
2047-
* The helper doesn't perform checks on @pid which makes it useful for pidfds
2048-
* created via CLONE_PIDFD where @pid has no task attached when the pidfd and
2049-
* pidfd file are prepared.
2050-
*
2051-
* If this function returns successfully the caller is responsible to either
2052-
* call fd_install() passing the returned pidfd and pidfd file as arguments in
2053-
* order to install the pidfd into its file descriptor table or they must use
2054-
* put_unused_fd() and fput() on the returned pidfd and pidfd file
2055-
* respectively.
2056-
*
2057-
* This function is useful when a pidfd must already be reserved but there
2058-
* might still be points of failure afterwards and the caller wants to ensure
2059-
* that no pidfd is leaked into its file descriptor table.
2060-
*
2061-
* Return: On success, a reserved pidfd is returned from the function and a new
2062-
* pidfd file is returned in the last argument to the function. On
2063-
* error, a negative error code is returned from the function and the
2064-
* last argument remains unchanged.
2065-
*/
2066-
static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
2067-
{
2068-
struct file *pidfd_file;
2069-
2070-
CLASS(get_unused_fd, pidfd)(O_CLOEXEC);
2071-
if (pidfd < 0)
2072-
return pidfd;
2073-
2074-
pidfd_file = pidfs_alloc_file(pid, flags | O_RDWR);
2075-
if (IS_ERR(pidfd_file))
2076-
return PTR_ERR(pidfd_file);
2077-
2078-
*ret = pidfd_file;
2079-
return take_fd(pidfd);
2080-
}
2081-
20822038
/**
20832039
* pidfd_prepare - allocate a new pidfd_file and reserve a pidfd
20842040
* @pid: the struct pid for which to create a pidfd
20852041
* @flags: flags of the new @pidfd
2086-
* @ret: Where to return the pidfd.
2042+
* @ret_file: return the new pidfs file
20872043
*
20882044
* Allocate a new file that stashes @pid and reserve a new pidfd number in the
20892045
* caller's file descriptor table. The pidfd is reserved but not installed yet.
@@ -2106,16 +2062,26 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re
21062062
* error, a negative error code is returned from the function and the
21072063
* last argument remains unchanged.
21082064
*/
2109-
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
2065+
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret_file)
21102066
{
2067+
struct file *pidfs_file;
2068+
21112069
/*
2112-
* While holding the pidfd waitqueue lock removing the task
2113-
* linkage for the thread-group leader pid (PIDTYPE_TGID) isn't
2114-
* possible. Thus, if there's still task linkage for PIDTYPE_PID
2115-
* not having thread-group leader linkage for the pid means it
2116-
* wasn't a thread-group leader in the first place.
2070+
* PIDFD_STALE is only allowed to be passed if the caller knows
2071+
* that @pid is already registered in pidfs and thus
2072+
* PIDFD_INFO_EXIT information is guaranteed to be available.
21172073
*/
2118-
scoped_guard(spinlock_irq, &pid->wait_pidfd.lock) {
2074+
if (!(flags & PIDFD_STALE)) {
2075+
/*
2076+
* While holding the pidfd waitqueue lock removing the
2077+
* task linkage for the thread-group leader pid
2078+
* (PIDTYPE_TGID) isn't possible. Thus, if there's still
2079+
* task linkage for PIDTYPE_PID not having thread-group
2080+
* leader linkage for the pid means it wasn't a
2081+
* thread-group leader in the first place.
2082+
*/
2083+
guard(spinlock_irq)(&pid->wait_pidfd.lock);
2084+
21192085
/* Task has already been reaped. */
21202086
if (!pid_has_task(pid, PIDTYPE_PID))
21212087
return -ESRCH;
@@ -2128,7 +2094,16 @@ int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
21282094
return -ENOENT;
21292095
}
21302096

2131-
return __pidfd_prepare(pid, flags, ret);
2097+
CLASS(get_unused_fd, pidfd)(O_CLOEXEC);
2098+
if (pidfd < 0)
2099+
return pidfd;
2100+
2101+
pidfs_file = pidfs_alloc_file(pid, flags | O_RDWR);
2102+
if (IS_ERR(pidfs_file))
2103+
return PTR_ERR(pidfs_file);
2104+
2105+
*ret_file = pidfs_file;
2106+
return take_fd(pidfd);
21322107
}
21332108

21342109
static void __delayed_free_task(struct rcu_head *rhp)
@@ -2477,7 +2452,7 @@ __latent_entropy struct task_struct *copy_process(
24772452
* Note that no task has been attached to @pid yet indicate
24782453
* that via CLONE_PIDFD.
24792454
*/
2480-
retval = __pidfd_prepare(pid, flags | PIDFD_CLONE, &pidfile);
2455+
retval = pidfd_prepare(pid, flags | PIDFD_STALE, &pidfile);
24812456
if (retval < 0)
24822457
goto bad_fork_free_pid;
24832458
pidfd = retval;

0 commit comments

Comments
 (0)