[PATCH] pidfd_prepare: don't pass O_RDWR to pidfs_alloc_file()
From: Oleg Nesterov
Date: Wed Feb 25 2026 - 11:21:04 EST
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().
Signed-off-by: Oleg Nesterov <oleg@xxxxxxxxxx>
---
fs/pidfs.c | 2 +-
kernel/fork.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/fs/pidfs.c b/fs/pidfs.c
index 318253344b5c..0e0176f5574f 100644
--- a/fs/pidfs.c
+++ b/fs/pidfs.c
@@ -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))
diff --git a/kernel/fork.c b/kernel/fork.c
index e832da9d15a4..80171158b0cd 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -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);
--
2.52.0