Skip to contentSkip to conversation

pidfd_prepare: don't pass O_RDWR to pidfs_alloc_file()

PATCH New Oleg Nesterov 1 participant 1 message Feb 25

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pidfd_prepare: don't pass O_RDWR to pidfs_alloc_file()

pidfs_alloc_file() does "flags |= O_RDWR" unconditionally.

In fact pidfd files (and some users of anon_inode_getfile_fmode) do not need O_RDWR/FMODE_WRITE, add a comment to pidfs_alloc_file().

I guess this is an accidental legacy from 628ff7c1d8d8 ("anonfd: Allow making anon files read-only") which added O_RDWR to every anon_inode_getf*() call, then it was copied into the initial implementation of sys_pidfd_open().

---
 fs/pidfs.c    | 2 +-
 kernel/fork.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/pidfs.c +1 -1
index 318253344b5c..0e0176f5574f 100644
@@ -1106,7 +1106,7 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)
 	VFS_WARN_ON_ONCE(!pid->attr);
 
 	flags &= ~PIDFD_STALE;
-	flags |= O_RDWR;
+	flags |= O_RDWR; /* currently this is not necessary */
 	pidfd_file = dentry_open(&path, flags, current_cred());
 	/* Raise PIDFD_THREAD explicitly as do_dentry_open() strips it. */
 	if (!IS_ERR(pidfd_file))
kernel/fork.c +1 -1
index e832da9d15a4..80171158b0cd 100644
@@ -1895,7 +1895,7 @@ int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret_file)
 	if (pidfd < 0)
 		return pidfd;
 
-	pidfs_file = pidfs_alloc_file(pid, flags | O_RDWR);
+	pidfs_file = pidfs_alloc_file(pid, flags);
 	if (IS_ERR(pidfs_file))
 		return PTR_ERR(pidfs_file);
 
Signature · 1 line
2.52.0