linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: "Oleg Nesterov" <oleg@redhat.com>,
	"Amir Goldstein" <amir73il@gmail.com>,
	"Aleksa Sarai" <cyphar@cyphar.com>,
	"Yu Watanabe" <watanabe.yu+github@gmail.com>,
	"Josef Bacik" <josef@toxicpanda.com>,
	"Jeff Layton" <jlayton@kernel.org>,
	"Jann Horn" <jannh@google.com>,
	"Luca Boccassi" <luca.boccassi@gmail.com>,
	"Alexander Mikhalitsyn" <alexander@mihalicyn.com>,
	linux-kernel@vger.kernel.org,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Jan Kara" <jack@suse.cz>,
	"Lennart Poettering" <lennart@poettering.net>,
	"Mike Yuan" <me@yhndnzj.com>,
	"Zbigniew Jędrzejewski-Szmek" <zbyszek@in.waw.pl>,
	"Christian Brauner" <brauner@kernel.org>
Subject: [PATCH 08/22] pidfs: expose coredump signal
Date: Tue, 28 Oct 2025 09:45:53 +0100	[thread overview]
Message-ID: <20251028-work-coredump-signal-v1-8-ca449b7b7aa0@kernel.org> (raw)
In-Reply-To: <20251028-work-coredump-signal-v1-0-ca449b7b7aa0@kernel.org>

Userspace needs access to the signal that caused the coredump before the
coredumping process has been reaped. Expose it as part of the coredump
information in struct pidfd_info. After the process has been reaped that
info is also available as part of PIDFD_INFO_EXIT's exit_code field.

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 fs/pidfs.c                 | 30 +++++++++++++++++++-----------
 include/uapi/linux/pidfd.h |  7 +++++--
 2 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/fs/pidfs.c b/fs/pidfs.c
index a3b80be3b98b..354ceb2126e7 100644
--- a/fs/pidfs.c
+++ b/fs/pidfs.c
@@ -41,6 +41,7 @@ void pidfs_get_root(struct path *path)
 
 enum pidfs_attr_mask_bits {
 	PIDFS_ATTR_BIT_EXIT	= 0,
+	PIDFS_ATTR_BIT_COREDUMP	= 1,
 };
 
 struct pidfs_attr {
@@ -51,6 +52,7 @@ struct pidfs_attr {
 		__s32 exit_code;
 	};
 	__u32 coredump_mask;
+	__u32 coredump_signal;
 };
 
 static struct rb_root pidfs_ino_tree = RB_ROOT;
@@ -297,7 +299,8 @@ static __u32 pidfs_coredump_mask(unsigned long mm_flags)
 			      PIDFD_INFO_CGROUPID | \
 			      PIDFD_INFO_EXIT | \
 			      PIDFD_INFO_COREDUMP | \
-			      PIDFD_INFO_SUPPORTED_MASK)
+			      PIDFD_INFO_SUPPORTED_MASK | \
+			      PIDFD_INFO_COREDUMP_SIGNAL)
 
 static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
 {
@@ -342,9 +345,12 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
 	}
 
 	if (mask & PIDFD_INFO_COREDUMP) {
-		kinfo.coredump_mask = READ_ONCE(attr->coredump_mask);
-		if (kinfo.coredump_mask)
-			kinfo.mask |= PIDFD_INFO_COREDUMP;
+		if (test_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask)) {
+			smp_rmb();
+			kinfo.mask |= PIDFD_INFO_COREDUMP | PIDFD_INFO_COREDUMP_SIGNAL;
+			kinfo.coredump_mask = attr->coredump_mask;
+			kinfo.coredump_signal = attr->coredump_signal;
+		}
 	}
 
 	task = get_pid_task(pid, PIDTYPE_PID);
@@ -370,6 +376,7 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
 
 			kinfo.coredump_mask = pidfs_coredump_mask(flags);
 			kinfo.mask |= PIDFD_INFO_COREDUMP;
+			/* No coredump actually took place, so no coredump signal. */
 		}
 	}
 
@@ -666,20 +673,21 @@ void pidfs_coredump(const struct coredump_params *cprm)
 {
 	struct pid *pid = cprm->pid;
 	struct pidfs_attr *attr;
-	__u32 coredump_mask = 0;
 
 	attr = READ_ONCE(pid->attr);
 
 	VFS_WARN_ON_ONCE(!attr);
 	VFS_WARN_ON_ONCE(attr == PIDFS_PID_DEAD);
 
-	/* Note how we were coredumped. */
-	coredump_mask = pidfs_coredump_mask(cprm->mm_flags);
-	/* Note that we actually did coredump. */
-	coredump_mask |= PIDFD_COREDUMPED;
+	/* Note how we were coredumped and that we coredumped. */
+	attr->coredump_mask = pidfs_coredump_mask(cprm->mm_flags) |
+			      PIDFD_COREDUMPED;
 	/* If coredumping is set to skip we should never end up here. */
-	VFS_WARN_ON_ONCE(coredump_mask & PIDFD_COREDUMP_SKIP);
-	smp_store_release(&attr->coredump_mask, coredump_mask);
+	VFS_WARN_ON_ONCE(attr->coredump_mask & PIDFD_COREDUMP_SKIP);
+	/* Expose the signal number that caused the coredump. */
+	attr->coredump_signal = cprm->siginfo->si_signo;
+	smp_wmb();
+	set_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask);
 }
 #endif
 
diff --git a/include/uapi/linux/pidfd.h b/include/uapi/linux/pidfd.h
index e05caa0e00fe..ea9a6811fc76 100644
--- a/include/uapi/linux/pidfd.h
+++ b/include/uapi/linux/pidfd.h
@@ -27,6 +27,7 @@
 #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_COREDUMP_SIGNAL	(1UL << 6) /* Always returned if PIDFD_INFO_COREDUMP is requested. */
 
 #define PIDFD_INFO_SIZE_VER0		64 /* sizeof first published struct */
 #define PIDFD_INFO_SIZE_VER1		72 /* sizeof second published struct */
@@ -94,8 +95,10 @@ struct pidfd_info {
 	__u32 fsuid;
 	__u32 fsgid;
 	__s32 exit_code;
-	__u32 coredump_mask;
-	__u32 __spare1;
+	struct /* coredump info */ {
+		__u32 coredump_mask;
+		__u32 coredump_signal;
+	};
 	__u64 supported_mask;	/* Mask flags that this kernel supports */
 };
 

-- 
2.47.3


  parent reply	other threads:[~2025-10-28  8:46 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-28  8:45 [PATCH 00/22] coredump: cleanups & pidfd extension Christian Brauner
2025-10-28  8:45 ` [PATCH 01/22] pidfs: use guard() for task_lock Christian Brauner
2025-11-03 15:19   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 02/22] pidfs: fix PIDFD_INFO_COREDUMP handling Christian Brauner
2025-11-03 15:35   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 03/22] pidfs: add missing PIDFD_INFO_SIZE_VER1 Christian Brauner
2025-11-03 15:36   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 04/22] pidfs: add missing BUILD_BUG_ON() assert on struct pidfd_info Christian Brauner
2025-11-03 15:36   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 05/22] pidfd: add a new supported_mask field Christian Brauner
2025-11-03 15:38   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 06/22] pidfs: prepare to drop exit_info pointer Christian Brauner
2025-11-03 15:47   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 07/22] pidfs: drop struct pidfs_exit_info Christian Brauner
2025-11-04  8:45   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` Christian Brauner [this message]
2025-11-04  8:45   ` [PATCH 08/22] pidfs: expose coredump signal Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 09/22] selftests/pidfd: update pidfd header Christian Brauner
2025-11-04  8:52   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 10/22] selftests/pidfd: add first supported_mask test Christian Brauner
2025-11-04  8:53   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 11/22] selftests/pidfd: add second " Christian Brauner
2025-11-04  8:53   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 12/22] selftests/coredump: split out common helpers Christian Brauner
2025-11-04  9:11   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 13/22] selftests/coredump: split out coredump socket tests Christian Brauner
2025-11-04  9:11   ` Alexander Mikhalitsyn
2025-10-28  8:45 ` [PATCH 14/22] selftests/coredump: fix userspace client detection Christian Brauner
2025-11-04  9:12   ` Alexander Mikhalitsyn
2025-10-28  8:46 ` [PATCH 15/22] selftests/coredump: fix userspace coredump " Christian Brauner
2025-11-04  9:12   ` Alexander Mikhalitsyn
2025-10-28  8:46 ` [PATCH 16/22] selftests/coredump: handle edge-triggered epoll correctly Christian Brauner
2025-11-04  9:12   ` Alexander Mikhalitsyn
2025-10-28  8:46 ` [PATCH 17/22] selftests/coredump: add debug logging to test helpers Christian Brauner
2025-11-04  9:13   ` Alexander Mikhalitsyn
2025-10-28  8:46 ` [PATCH 18/22] selftests/coredump: add debug logging to coredump socket tests Christian Brauner
2025-11-04  9:13   ` Alexander Mikhalitsyn
2025-10-28  8:46 ` [PATCH 19/22] selftests/coredump: add debug logging to coredump socket protocol tests Christian Brauner
2025-11-04  9:13   ` Alexander Mikhalitsyn
2025-10-28  8:46 ` [PATCH 20/22] selftests/coredump: ignore ENOSPC errors Christian Brauner
2025-11-04  9:18   ` Alexander Mikhalitsyn
2025-10-28  8:46 ` [PATCH 21/22] selftests/coredump: add first PIDFD_INFO_COREDUMP_SIGNAL test Christian Brauner
2025-11-04  9:18   ` Alexander Mikhalitsyn
2025-10-28  8:46 ` [PATCH 22/22] selftests/coredump: add second " Christian Brauner
2025-11-04  9:19   ` Alexander Mikhalitsyn
2025-11-02 17:03 ` [PATCH 00/22] coredump: cleanups & pidfd extension Oleg Nesterov
2025-11-04 21:00   ` Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251028-work-coredump-signal-v1-8-ca449b7b7aa0@kernel.org \
    --to=brauner@kernel.org \
    --cc=alexander@mihalicyn.com \
    --cc=amir73il@gmail.com \
    --cc=cyphar@cyphar.com \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=jlayton@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=lennart@poettering.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luca.boccassi@gmail.com \
    --cc=me@yhndnzj.com \
    --cc=oleg@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=watanabe.yu+github@gmail.com \
    --cc=zbyszek@in.waw.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).