@@ -2035,55 +2035,11 @@ static inline void rcu_copy_process(struct task_struct *p)
2035
2035
#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
2036
2036
}
2037
2037
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
-
2082
2038
/**
2083
2039
* pidfd_prepare - allocate a new pidfd_file and reserve a pidfd
2084
2040
* @pid: the struct pid for which to create a pidfd
2085
2041
* @flags: flags of the new @pidfd
2086
- * @ret: Where to return the pidfd.
2042
+ * @ret_file: return the new pidfs file
2087
2043
*
2088
2044
* Allocate a new file that stashes @pid and reserve a new pidfd number in the
2089
2045
* 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
2106
2062
* error, a negative error code is returned from the function and the
2107
2063
* last argument remains unchanged.
2108
2064
*/
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 )
2110
2066
{
2067
+ struct file * pidfs_file ;
2068
+
2111
2069
/*
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.
2117
2073
*/
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
+
2119
2085
/* Task has already been reaped. */
2120
2086
if (!pid_has_task (pid , PIDTYPE_PID ))
2121
2087
return - ESRCH ;
@@ -2128,7 +2094,16 @@ int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
2128
2094
return - ENOENT ;
2129
2095
}
2130
2096
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 );
2132
2107
}
2133
2108
2134
2109
static void __delayed_free_task (struct rcu_head * rhp )
@@ -2477,7 +2452,7 @@ __latent_entropy struct task_struct *copy_process(
2477
2452
* Note that no task has been attached to @pid yet indicate
2478
2453
* that via CLONE_PIDFD.
2479
2454
*/
2480
- retval = __pidfd_prepare (pid , flags | PIDFD_CLONE , & pidfile );
2455
+ retval = pidfd_prepare (pid , flags | PIDFD_STALE , & pidfile );
2481
2456
if (retval < 0 )
2482
2457
goto bad_fork_free_pid ;
2483
2458
pidfd = retval ;
0 commit comments