]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
pidfd: add a new supported_mask field
authorChristian Brauner <brauner@kernel.org>
Tue, 28 Oct 2025 08:45:50 +0000 (09:45 +0100)
committerChristian Brauner <brauner@kernel.org>
Thu, 30 Oct 2025 13:25:13 +0000 (14:25 +0100)
Some of the future fields in struct pidfd_info can be optional. If the
kernel has nothing to emit in that field, then it doesn't set the flag
in the reply. This presents a problem: There is currently no way to know
what mask flags the kernel supports since one can't always count on them
being in the reply.

Add a new PIDFD_INFO_SUPPORTED_MASK flag and field that the kernel can
set in the reply. Userspace can use this to determine if the fields it
requires from the kernel are supported. This also gives us a way to
deprecate fields in the future, if that should become necessary.

Link: https://patch.msgid.link/20251028-work-coredump-signal-v1-5-ca449b7b7aa0@kernel.org
Reviewed-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/pidfs.c
include/uapi/linux/pidfd.h

index 7e4d90cc74ff9a40bdd58cc0291d23dda5a7ac9c..204ebd32791a3a4d12e6ab202d1100912c55bf58 100644 (file)
@@ -293,6 +293,14 @@ static __u32 pidfs_coredump_mask(unsigned long mm_flags)
        return 0;
 }
 
+/* This must be updated whenever a new flag is added */
+#define PIDFD_INFO_SUPPORTED (PIDFD_INFO_PID | \
+                             PIDFD_INFO_CREDS | \
+                             PIDFD_INFO_CGROUPID | \
+                             PIDFD_INFO_EXIT | \
+                             PIDFD_INFO_COREDUMP | \
+                             PIDFD_INFO_SUPPORTED_MASK)
+
 static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
 {
        struct pidfd_info __user *uinfo = (struct pidfd_info __user *)arg;
@@ -306,7 +314,7 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
        const struct cred *c;
        __u64 mask;
 
-       BUILD_BUG_ON(sizeof(struct pidfd_info) != PIDFD_INFO_SIZE_VER1);
+       BUILD_BUG_ON(sizeof(struct pidfd_info) != PIDFD_INFO_SIZE_VER2);
 
        if (!uinfo)
                return -EINVAL;
@@ -412,6 +420,13 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
                return -ESRCH;
 
 copy_out:
+       if (mask & PIDFD_INFO_SUPPORTED_MASK) {
+               kinfo.mask |= PIDFD_INFO_SUPPORTED_MASK;
+               kinfo.supported_mask = PIDFD_INFO_SUPPORTED;
+       }
+
+       /* Are there bits in the return mask not present in PIDFD_INFO_SUPPORTED? */
+       WARN_ON_ONCE(~PIDFD_INFO_SUPPORTED & kinfo.mask);
        /*
         * If userspace and the kernel have the same struct size it can just
         * be copied. If userspace provides an older struct, only the bits that
index 6ccbabd9a68d8175fac811166a09069a2fce8ac7..e05caa0e00fe9b0b930666c47e137ae828f2db32 100644 (file)
 #define PIDFD_INFO_CGROUPID            (1UL << 2) /* Always returned if available, even if not requested */
 #define PIDFD_INFO_EXIT                        (1UL << 3) /* Only returned if requested. */
 #define PIDFD_INFO_COREDUMP            (1UL << 4) /* Only returned if requested. */
+#define PIDFD_INFO_SUPPORTED_MASK      (1UL << 5) /* Want/got supported mask flags */
 
 #define PIDFD_INFO_SIZE_VER0           64 /* sizeof first published struct */
 #define PIDFD_INFO_SIZE_VER1           72 /* sizeof second published struct */
+#define PIDFD_INFO_SIZE_VER2           80 /* sizeof third published struct */
 
 /*
  * Values for @coredump_mask in pidfd_info.
@@ -94,6 +96,7 @@ struct pidfd_info {
        __s32 exit_code;
        __u32 coredump_mask;
        __u32 __spare1;
+       __u64 supported_mask;   /* Mask flags that this kernel supports */
 };
 
 #define PIDFS_IOCTL_MAGIC 0xFF