Skip to content

Commit cdefbf2

Browse files
oleg-nesterovbrauner
authored andcommitted
pidfd: cleanup the usage of __pidfd_prepare's flags
- make pidfd_create() static. - Don't pass O_RDWR | O_CLOEXEC to __pidfd_prepare() in copy_process(), __pidfd_prepare() adds these flags unconditionally. - Kill the flags check in __pidfd_prepare(). sys_pidfd_open() checks the flags itself, all other users of pidfd_prepare() pass flags = 0. If we need a sanity check for those other in kernel users then WARN_ON_ONCE(flags & ~PIDFD_NONBLOCK) makes more sense. - Don't pass O_RDWR to get_unused_fd_flags(), it ignores everything except O_CLOEXEC. - Don't pass O_CLOEXEC to anon_inode_getfile(), it ignores everything except O_ACCMODE | O_NONBLOCK. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Link: https://lore.kernel.org/r/20240125161734.GA778@redhat.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent b639585 commit cdefbf2

3 files changed

Lines changed: 4 additions & 8 deletions

File tree

include/linux/pid.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ struct file;
7373
extern struct pid *pidfd_pid(const struct file *file);
7474
struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags);
7575
struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags);
76-
int pidfd_create(struct pid *pid, unsigned int flags);
7776
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret);
7877

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

kernel/fork.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,15 +2130,12 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re
21302130
int pidfd;
21312131
struct file *pidfd_file;
21322132

2133-
if (flags & ~(O_NONBLOCK | O_RDWR | O_CLOEXEC))
2134-
return -EINVAL;
2135-
2136-
pidfd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
2133+
pidfd = get_unused_fd_flags(O_CLOEXEC);
21372134
if (pidfd < 0)
21382135
return pidfd;
21392136

21402137
pidfd_file = anon_inode_getfile("[pidfd]", &pidfd_fops, pid,
2141-
flags | O_RDWR | O_CLOEXEC);
2138+
flags | O_RDWR);
21422139
if (IS_ERR(pidfd_file)) {
21432140
put_unused_fd(pidfd);
21442141
return PTR_ERR(pidfd_file);
@@ -2524,7 +2521,7 @@ __latent_entropy struct task_struct *copy_process(
25242521
*/
25252522
if (clone_flags & CLONE_PIDFD) {
25262523
/* Note that no task has been attached to @pid yet. */
2527-
retval = __pidfd_prepare(pid, O_RDWR | O_CLOEXEC, &pidfile);
2524+
retval = __pidfd_prepare(pid, 0, &pidfile);
25282525
if (retval < 0)
25292526
goto bad_fork_free_pid;
25302527
pidfd = retval;

kernel/pid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
595595
* Return: On success, a cloexec pidfd is returned.
596596
* On error, a negative errno number will be returned.
597597
*/
598-
int pidfd_create(struct pid *pid, unsigned int flags)
598+
static int pidfd_create(struct pid *pid, unsigned int flags)
599599
{
600600
int pidfd;
601601
struct file *pidfd_file;

0 commit comments

Comments
 (0)