pidfds: add coredump_code field to pidfd_info
v4 cover pidfds: add coredump_code field to pidfd_info
This patchs series adds a new field called coredump_code to struct pidfd_info, as well as the relevant selftests. Note that the coredump selftests are currently not passing, and the following patch is needed to fix them:
https://lore.kernel.org/lkml/ab2kI0PI_Vk6bU88@NH27D9T0LF/
I have not included it in this series as it is not directly related to the coredump_code changes.
v1: https://lore.kernel.org/lkml/ab29J6KsQm8Xg3LR@NH27D9T0LF/
v2: Add coredump_pad to struct pidfd_info to ensure the struct has the same size on both 64 bit and 32 bit systems. The issue was spotted by the kernel test robot.
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/ab68fUmCK4An1UH-@NH27D9T0LF/
v3: Sending as a new thread, same as v2 otherwise.
https://lore.kernel.org/lkml/acBRj-DSCpqlKt6t@NH27D9T0LF/
v4: Split kernel code from selftest code as suggested by Christian Brauner. Add
Acked-by: Oleg Nesterov to the commit adding the coredump_code field. Thanks!
https://lore.kernel.org/lkml/20260323-hochsehen-kurort-862912a1d7b5@brauner/
https://lore.kernel.org/lkml/[email protected]/
Emanuele Rocca (2):
pidfds: add coredump_code field to pidfd_info
selftests: check pidfd_info->coredump_code correctness
fs/pidfs.c | 12 ++++--- include/uapi/linux/pidfd.h | 4 +++ .../coredump/coredump_socket_protocol_test.c | 26 +++++++++++++++ .../selftests/coredump/coredump_socket_test.c | 32 +++++++++++++++++++ .../coredump/coredump_test_helpers.c | 4 +-- tools/testing/selftests/pidfd/pidfd.h | 5 +++ .../testing/selftests/pidfd/pidfd_info_test.c | 1 + 7 files changed, 78 insertions(+), 6 deletions(-)
Signature · 1 line
2.47.3
PATCH v5 1/2 pidfds: add coredump_code field to pidfd_info
The struct pidfd_info currently exposes in a field called coredump_signal the signal number (si_signo) that triggered the dump (for example, 11 for SIGSEGV). However, it is also valuable to understand the reason why that signal was sent. This additional context is provided by the signal code (si_code), such as 2 for SEGV_ACCERR.
Add a new field to struct pidfd_info called coredump_code with the value of si_code for the benefit of sysadmins who pipe core dumps to user-space programs for later analysis. The following snippet illustrates a simplified C program that consumes coredump_signal and coredump_code, and then logs core dump signals and codes to a file:
int pidfd = (int)atoi(argv[1]);
struct pidfd_info info = {
.mask = PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP,
};
if (ioctl(pidfd, PIDFD_GET_INFO, &info) == 0)
if (info.mask & PIDFD_INFO_COREDUMP)
fprintf(f, "PID=%d, si_signo: %d si_code: %d\n",
info.pid, info.coredump_signal, info.coredump_code);Assuming the program is installed under /usr/local/bin/core-logger, core dump processing can be enabled by setting /proc/sys/kernel/core_pattern to '|/usr/local/bin/dumpstuff %F'.
systemd-coredump(8) already uses pidfds to process core dumps, and it could be extended to include the values of coredump_code too.
--- fs/pidfs.c | 12 ++++++++---- include/uapi/linux/pidfd.h | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-)
fs/pidfs.c +8 -4
@@ -55,6 +55,7 @@ struct pidfs_attr { };__u32 coredump_mask;__u32 coredump_signal; +__u32 coredump_code; };static struct rhashtable pidfs_ino_ht;
@@ -331,7 +332,8 @@ static __u32 pidfs_coredump_mask(unsigned long mm_flags) PIDFD_INFO_EXIT | \ PIDFD_INFO_COREDUMP | \ PIDFD_INFO_SUPPORTED_MASK | \ - PIDFD_INFO_COREDUMP_SIGNAL) + PIDFD_INFO_COREDUMP_SIGNAL | \ + PIDFD_INFO_COREDUMP_CODE)static long pidfd_info (struct file *file,unsigned int cmd,unsigned long arg) {
@@ -345,7 +347,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_VER2 ); +BUILD_BUG_ON (sizeof (struct pidfd_info ) !=PIDFD_INFO_SIZE_VER3 );if (!uinfo)return -EINVAL ;
@@ -378,9 +380,10 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)if (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.mask |=PIDFD_INFO_COREDUMP |PIDFD_INFO_COREDUMP_SIGNAL |PIDFD_INFO_COREDUMP_CODE ; kinfo.coredump_mask = attr->coredump_mask; kinfo.coredump_signal = attr->coredump_signal; + kinfo.coredump_code = attr->coredump_code; } }
@@ -730,8 +733,9 @@ void pidfs_coredump(const struct coredump_params *cprm)PIDFD_COREDUMPED ;/* If coredumping is set to skip we should never end up here. */ VFS_WARN_ON_ONCE (attr->coredump_mask &PIDFD_COREDUMP_SKIP ); -/* Expose the signal number that caused the coredump. */ +/* Expose the signal number and code that caused the coredump. */ attr->coredump_signal = cprm->siginfo->si_signo; + attr->coredump_code = cprm->siginfo->si_code;smp_wmb ();set_bit (PIDFS_ATTR_BIT_COREDUMP , &attr->attr_mask); }
include/uapi/linux/pidfd.h +4 -0
@@ -28,10 +28,12 @@#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_COREDUMP_CODE (1UL << 7)/* 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 */ #define PIDFD_INFO_SIZE_VER2 80/* sizeof third published struct */ +#define PIDFD_INFO_SIZE_VER3 88/* sizeof fourth published struct */ /* * Values for @coredump_mask in pidfd_info.
@@ -98,6 +100,8 @@ struct pidfd_info {struct /* coredump info */ {__u32 coredump_mask;__u32 coredump_signal; +__u32 coredump_code; +__u32 coredump_pad;/* align supported_mask to 8 bytes */ };__u64 supported_mask;/* Mask flags that this kernel supports */ };
Signature · 1 line
2.47.3
PATCH v5 2/2 selftests: check pidfd_info->coredump_code correctness
In reply to Emanuele RoccaExtend the coredump_socket and coredump_socket_protocol selftests to verify that the field coredump_code is set as expected in struct pidfd_info.
--- .../coredump/coredump_socket_protocol_test.c | 26 +++++++++++++++ .../selftests/coredump/coredump_socket_test.c | 32 +++++++++++++++++++ .../coredump/coredump_test_helpers.c | 4 +-- tools/testing/selftests/pidfd/pidfd.h | 5 +++ .../testing/selftests/pidfd/pidfd_info_test.c | 1 + 5 files changed, 66 insertions(+), 2 deletions(-)
tools/testing/selftests/coredump/coredump_socket_protocol_test.c +26 -0
@@ -1004,6 +1004,8 @@ TEST_F(coredump, socket_request_invalid_size_large)* * Verify that when using socket-based coredump protocol, * the coredump_signal field is correctly exposed as SIGSEGV. +* Also check that the coredump_code field is correctly exposed +* as SEGV_MAPERR. */ TEST_F(coredump, socket_coredump_signal_sigsegv) {
@@ -1079,6 +1081,18 @@ TEST_F(coredump, socket_coredump_signal_sigsegv) goto out; } +/* Verify coredump_code is available and correct */ +if (!(info.mask &PIDFD_INFO_COREDUMP_CODE )) { +fprintf (stderr,"socket_coredump_signal_sigsegv: PIDFD_INFO_COREDUMP_CODE not set in mask\n" ); + goto out; + } + +if (info.coredump_code !=SEGV_MAPERR ) { +fprintf (stderr,"socket_coredump_signal_sigsegv: coredump_code=%d, expected SEGV_MAPERR=%d\n" , + info.coredump_code,SEGV_MAPERR ); + goto out; + } +if (!read_coredump_req (fd_coredump, &req)) {fprintf (stderr,"socket_coredump_signal_sigsegv: read_coredump_req failed\n" ); goto out;
@@ -1128,6 +1142,8 @@ TEST_F(coredump, socket_coredump_signal_sigsegv)ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP ));ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP_SIGNAL ));ASSERT_EQ (info.coredump_signal,SIGSEGV ); +ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP_CODE )); +ASSERT_EQ (info.coredump_code,SEGV_MAPERR );wait_and_check_coredump_server (pid_coredump_server, _metadata, self); }
@@ -1137,6 +1153,8 @@ TEST_F(coredump, socket_coredump_signal_sigsegv)* * Verify that when using socket-based coredump protocol, * the coredump_signal field is correctly exposed as SIGABRT. +* Also check that the coredump_code field is correctly exposed +* as SI_TKILL. */ TEST_F(coredump, socket_coredump_signal_sigabrt) {
@@ -1212,6 +1230,12 @@ TEST_F(coredump, socket_coredump_signal_sigabrt) goto out; } +if (info.coredump_code !=SI_TKILL ) { +fprintf (stderr,"socket_coredump_signal_sigabrt: coredump_code=%d, expected SI_TKILL=%d\n" , + info.coredump_code,SI_TKILL ); + goto out; + } +if (!read_coredump_req (fd_coredump, &req)) {fprintf (stderr,"socket_coredump_signal_sigabrt: read_coredump_req failed\n" ); goto out;
@@ -1261,6 +1285,8 @@ TEST_F(coredump, socket_coredump_signal_sigabrt)ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP ));ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP_SIGNAL ));ASSERT_EQ (info.coredump_signal,SIGABRT ); +ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP_CODE )); +ASSERT_EQ (info.coredump_code,SI_TKILL );wait_and_check_coredump_server (pid_coredump_server, _metadata, self); }
tools/testing/selftests/coredump/coredump_socket_test.c +32 -0
@@ -435,6 +435,8 @@ TEST_F(coredump, socket_no_listener)* * Verify that when using simple socket-based coredump (@ pattern), * the coredump_signal field is correctly exposed as SIGSEGV. +* Also check that the coredump_code field is correctly exposed +* as SEGV_MAPERR. */ TEST_F(coredump, socket_coredump_signal_sigsegv) {
@@ -509,6 +511,18 @@ TEST_F(coredump, socket_coredump_signal_sigsegv) goto out; } +/* Verify coredump_code is available and correct */ +if (!(info.mask &PIDFD_INFO_COREDUMP_CODE )) { +fprintf (stderr,"socket_coredump_signal_sigsegv: PIDFD_INFO_COREDUMP_CODE not set in mask\n" ); + goto out; + } + +if (info.coredump_code !=SEGV_MAPERR ) { +fprintf (stderr,"socket_coredump_signal_sigsegv: coredump_code=%d, expected SEGV_MAPERR=%d\n" , + info.coredump_code,SEGV_MAPERR ); + goto out; + } + fd_core_file =open_coredump_tmpfile (self->fd_tmpfs_detached);if (fd_core_file <0 ) {fprintf (stderr,"socket_coredump_signal_sigsegv: open_coredump_tmpfile failed: %m\n" );
@@ -572,6 +586,8 @@ TEST_F(coredump, socket_coredump_signal_sigsegv)ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP ));ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP_SIGNAL ));ASSERT_EQ (info.coredump_signal,SIGSEGV ); +ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP_CODE )); +ASSERT_EQ (info.coredump_code,SEGV_MAPERR );wait_and_check_coredump_server (pid_coredump_server, _metadata, self); }
@@ -581,6 +597,8 @@ TEST_F(coredump, socket_coredump_signal_sigsegv)* * Verify that when using simple socket-based coredump (@ pattern), * the coredump_signal field is correctly exposed as SIGABRT. +* Also check that the coredump_code field is correctly exposed +* as SI_TKILL. */ TEST_F(coredump, socket_coredump_signal_sigabrt) {
@@ -655,6 +673,18 @@ TEST_F(coredump, socket_coredump_signal_sigabrt) goto out; } +/* Verify coredump_code is available and correct */ +if (!(info.mask &PIDFD_INFO_COREDUMP_CODE )) { +fprintf (stderr,"socket_coredump_signal_sigabrt: PIDFD_INFO_COREDUMP_CODE not set in mask\n" ); + goto out; + } + +if (info.coredump_code !=SI_TKILL ) { +fprintf (stderr,"socket_coredump_signal_sigabrt: coredump_code=%d, expected SI_TKILL=%d\n" , + info.coredump_code,SI_TKILL ); + goto out; + } + fd_core_file =open_coredump_tmpfile (self->fd_tmpfs_detached);if (fd_core_file <0 ) {fprintf (stderr,"socket_coredump_signal_sigabrt: open_coredump_tmpfile failed: %m\n" );
@@ -718,6 +748,8 @@ TEST_F(coredump, socket_coredump_signal_sigabrt)ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP ));ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP_SIGNAL ));ASSERT_EQ (info.coredump_signal,SIGABRT ); +ASSERT_TRUE (!!(info.mask &PIDFD_INFO_COREDUMP_CODE )); +ASSERT_EQ (info.coredump_code,SI_TKILL );wait_and_check_coredump_server (pid_coredump_server, _metadata, self); }
tools/testing/selftests/coredump/coredump_test_helpers.c +2 -2
@@ -148,8 +148,8 @@ bool get_pidfd_info(int fd_peer_pidfd, struct pidfd_info *info)fprintf (stderr,"get_pidfd_info: ioctl(PIDFD_GET_INFO) failed: %m\n" );return false; } -fprintf (stderr,"get_pidfd_info: mask=0x%llx, coredump_mask=0x%x, coredump_signal=%d\n" , - (unsigned long long )info->mask, info->coredump_mask, info->coredump_signal); +fprintf (stderr,"get_pidfd_info: mask=0x%llx, coredump_mask=0x%x, coredump_signal=%d, coredump_code=%d\n" , + (unsigned long long )info->mask, info->coredump_mask, info->coredump_signal, info->coredump_code);return true; }
tools/testing/selftests/pidfd/pidfd.h +5 -0
@@ -156,6 +156,10 @@#define PIDFD_INFO_COREDUMP_SIGNAL (1UL << 6)#endif +#ifndef PIDFD_INFO_COREDUMP_CODE +#define PIDFD_INFO_COREDUMP_CODE (1UL << 7) +#endif +#ifndef PIDFD_COREDUMPED #define PIDFD_COREDUMPED (1U << 0)/* Did crash and... */ #endif
@@ -194,6 +198,7 @@ struct pidfd_info {struct {__u32 coredump_mask;__u32 coredump_signal; +__u32 coredump_code; };__u64 supported_mask; };
tools/testing/selftests/pidfd/pidfd_info_test.c +1 -0
@@ -724,6 +724,7 @@ TEST(supported_mask_field)ASSERT_TRUE (!!(info.supported_mask &PIDFD_INFO_COREDUMP ));ASSERT_TRUE (!!(info.supported_mask &PIDFD_INFO_SUPPORTED_MASK ));ASSERT_TRUE (!!(info.supported_mask &PIDFD_INFO_COREDUMP_SIGNAL )); +ASSERT_TRUE (!!(info.supported_mask &PIDFD_INFO_COREDUMP_CODE ));/* Clean up */ sys_pidfd_send_signal (pidfd,SIGKILL ,NULL ,0 );
Signature · 1 line
2.47.3
Quoted 8 lines · Emanuele Rocca
This patchs series adds a new field called
coredump_codeto structpidfd_info, as well as the relevant selftests. Note that the coredump selftests are currently not passing, and the following patch is needed to fix them:
https://lore.kernel.org/lkml/ab2kI0PI_Vk6bU88@NH27D9T0LF/
I have not included it in this series as it is not directly related to thecoredump_codechanges.[...]
Applied to the vfs-7.1.pidfs branch of the vfs/vfs.git tree. Patches in the vfs-7.1.pidfs branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.1.pidfs
[1/2] pidfds: add coredump_code field to pidfd_info
https://git.kernel.org/vfs/vfs/c/701f7f4fbabb [2/2] selftests: check pidfd_info->coredump_code correctness https://git.kernel.org/vfs/vfs/c/7aaa4915cb69