Skip to contentSkip to conversation

ptrace: add ability to retrieve signals without removing from a queue (v2)

PATCH In review Andrey Vagin 5 participants 7 messages Feb 25, 2013 – Feb 26, 2013

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

PATCH 1/1 ptrace: add ability to retrieve signals without removing from a queue (v2)

This patch adds a new ptrace request PTRACE_PEEKSIGINFO.

This request is used to retrieve information about signals starting with the specified sequence number. Siginfo_t structures are copied from the child into the buffer starting at "data".

The argument "addr" is a pointer to struct ptrace_peeksiginfo_args. struct ptrace_peeksiginfo_args {
u64 off; /* from which siginfo to start */
u32 nr; /* how may siginfos to take */
u32 flags;
};

Currently here is only one flag PTRACE_PEEKSIGINFO_SHARED for dumping signals from process-wide shared queue. If this flag is not set, a signal is read from a per-thread queue. A result siginfo contains a kernel part of si_code which usually striped, but it's required for queuing the same siginfo back during restore of pending signals.

The request PTRACE_PEEKSIGINFO returns a number of dumped signals. If a signal with the specified sequence number doesn't exist, ptrace returns 0.

This functionality is required for checkpointing pending signals.

The prototype of this code was developed by Oleg Nesterov.

v2: * don't wrapped on CONFIG_CHECKPOINT_RESTORE. This functionality is
going to be used in gdb.
* use ptrace_peeksiginfo_args, because addr is too small for encoding a signal number and flags.

Cc Roland McGrath <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc David Howells <[email protected]>
Cc Dave Jones <[email protected]>
Cc "Michael Kerrisk (man-pages)" <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Linus Torvalds <torvalds-de/[email protected]>
Cc Pedro Alves <[email protected]>
---
 include/uapi/linux/ptrace.h | 12 +++++++
 kernel/ptrace.c             | 76 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)
include/uapi/linux/ptrace.h +12 -0
index 022ab18..01b4bd8 100644
@@ -5,6 +5,7 @@
 
 /* has the defines to get at the registers. */
 
+#include <linux/types.h>
 
 #define PTRACE_TRACEME		   0
 #define PTRACE_PEEKTEXT		   1
@@ -52,6 +53,17 @@
 #define PTRACE_INTERRUPT	0x4207
 #define PTRACE_LISTEN		0x4208
 
+#define PTRACE_PEEKSIGINFO	0x4209
+
+struct ptrace_peeksiginfo_args {
+	__u64 off;	/* from which siginfo to start */
+	__u32 nr;	/* how may siginfos to take */
+	__u32 flags;
+};
+
+/* Read signals from a shared (process wide) queue */
+#define PTRACE_PEEKSIGINFO_SHARED	(1 << 0)
+
 /* Wait extended result codes for the above trace options.  */
 #define PTRACE_EVENT_FORK	1
 #define PTRACE_EVENT_VFORK	2
kernel/ptrace.c +76 -0

94 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#1 lore raw

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 656

Quoted 11 lines
kernel/ptrace.c +11 -0
+	for (i = 0; i < arg.nr; i++) {
+		off = arg.off + i;
+
+		spin_lock_irq(&child->sighand->siglock);
+		list_for_each_entry(q, &pending->list, list) {
+			if (!off--) {
+				copy_siginfo(&info, &q->info);
+				break;
+			}
+		}
+		spin_unlock_irq(&child->sighand->siglock);

What's the point of arg.nr if you for every single siginfo drop the lock and perform linear search anyway?

Thanks,
Pavel

#2 lore raw

2013/2/25 Pavel Emelyanov <[email protected]>:

Quoted 12 lines

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 656

Quoted 11 lines
kernel/ptrace.c +11 -0
+     for (i = 0; i < arg.nr; i++) {
+             off = arg.off + i;
+
+             spin_lock_irq(&child->sighand->siglock);
+             list_for_each_entry(q, &pending->list, list) {
+                     if (!off--) {
+                             copy_siginfo(&info, &q->info);
+                             break;
+                     }
+             }
+             spin_unlock_irq(&child->sighand->siglock);
Quoted 2 lines

What's the point of arg.nr if you for every single siginfo drop the lock and perform linear search anyway?

arg.nr may be useful, if we will decide to optimize this part in a future. Currently the problem with complexity of finding a signal in a queue is true not only for this code. It exists for delivering real-time signals as well. Probably in a future this problem will be fixed for both cases by the same way.

Thanks,
Andrew

#3 lore raw
In reply to Andrey Vagin
Quoted 23 lines · On Mon, 25 Feb 2013 14:06:53 +0400 Andrey Vagin…

This patch adds a new ptrace request PTRACE_PEEKSIGINFO.

This request is used to retrieve information about signals starting with the specified sequence number. Siginfo_t structures are copied from the child into the buffer starting at "data".

The argument "addr" is a pointer to struct ptrace_peeksiginfo_args. struct ptrace_peeksiginfo_args {
u64 off; /* from which siginfo to start */
u32 nr; /* how may siginfos to take */
u32 flags;
};

Currently here is only one flag PTRACE_PEEKSIGINFO_SHARED for dumping signals from process-wide shared queue. If this flag is not set, a signal is read from a per-thread queue. A result siginfo contains a kernel part of si_code which usually striped, but it's required for queuing the same siginfo back during restore of pending signals.

The request PTRACE_PEEKSIGINFO returns a number of dumped signals. If a signal with the specified sequence number doesn't exist, ptrace returns 0.

Quoted 6 lines

This functionality is required for checkpointing pending signals.

The prototype of this code was developed by Oleg Nesterov.

v2: * don't wrapped on CONFIG_CHECKPOINT_RESTORE. This functionality is
going to be used in gdb.

That this will be used by gdb is useful information. Please expand upon this within the main changelog.

Quoted 5 lines

* use ptrace_peeksiginfo_args, because addr is too small for encoding a signal number and flags.

Cc Roland McGrath <[email protected]>
Cc Oleg Nesterov <[email protected]>

Would like an Oleg ack, please.

Quoted 12 lines

...

kernel/ptrace.c +5 -0
@@ -618,6 +619,77 @@ static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
 	return error;
 }
 
+static int ptrace_peek_siginfo(struct task_struct *child,
+				unsigned long addr,
+				unsigned long data)
+{
+	struct ptrace_peeksiginfo_args arg;

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 636

Quoted 10 lines
kernel/ptrace.c +10 -0
+	struct sigpending *pending;
+	struct sigqueue *q;
+	siginfo_t info;
+	long long off;
+	int ret, i;
+
+	ret = copy_from_user(&arg, (void __user *) addr,
+				sizeof(struct ptrace_peeksiginfo_args));
+	if (ret)
+		return ret;

return -EFAULT;

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)

Quoted 7 lines
kernel/ptrace.c +6 -0
+	if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
+		pending = &child->signal->shared_pending;
+	else
+		pending = &child->pending;
 
+	if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
+		return -EINVAL; /* unknown flags */

You could perform this test before the previous one and save an unmeasurably small amount of CPU time in circumstances which never happen!

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 647

Quoted 2 lines
kernel/ptrace.c +2 -0
+	for (i = 0; i < arg.nr; i++) {
+		off = arg.off + i;

"long long" = "u64" + "int".

Wanna have a little think about what we're doing here, see if we can pick the most appropriate types?

`off' could be made local to this loop, which is a bit neater.

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 659

Quoted 11 lines
kernel/ptrace.c +11 -0
+		spin_lock_irq(&child->sighand->siglock);
+		list_for_each_entry(q, &pending->list, list) {
+			if (!off--) {
+				copy_siginfo(&info, &q->info);
+				break;
+			}
+		}
+		spin_unlock_irq(&child->sighand->siglock);
+
+		if (off >= 0)
+			break;

<scratches head>

Oh I see what you did there - the user requested an entry which is beyond the end of the list? A code comment would be nice.

What's the return value in this case? "i". So how does userspace find out what happened?

Please fully describe the interface so things such as this can be understood.

Quoted 14 lines
kernel/ptrace.c +14 -0
+#ifdef CONFIG_COMPAT
+		if (unlikely(is_compat_task())) {
+			compat_siginfo_t __user *uinfo = compat_ptr(data);
+
+			ret = copy_siginfo_to_user32(uinfo, &info);
+			if (!ret)
+				ret = __put_user(info.si_code, &uinfo->si_code);
+		} else
+#endif
+		{
+			siginfo_t __user *uinfo = (siginfo_t __user *) data;
+
+			ret = copy_siginfo_to_user(uinfo, &info);
+			if (!ret)

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 684

Quoted 10 lines
kernel/ptrace.c +10 -0
+				ret = __put_user(info.si_code, &uinfo->si_code);
+		}
+
+		if (ret)
+			break;
+
+		data += sizeof(siginfo_t);
+
+		if (signal_pending(current)) {
+			i++;

What does the i++ do?

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 691

Quoted 7 lines
kernel/ptrace.c +7 -0
+			break;
+		}
+
+		cond_resched();
+	}
+
+	return i ? : ret;

I hate that gcc thing :(

Also, is it buggy? `ret' might be -EFAULT here. We appear to be using read() return semantics here?

Please, document the interface *completely* so that others can review the design and can then check that the implementation matches that design. As it stands, we're reduced to mind-reading.

Quoted 4 lines
(quoted diff) +1 -0
+}
 
...
#4 lore raw
Quoted 3 lines

Please, document the interface *completely* so that others can review the design and can then check that the implementation matches that design. As it stands, we're reduced to mind-reading.

+1.

Andrey, please don't assume that people have read past mails. Even if they have, they're forgetful. Don't make people hunt down old mails to try to put the pieces together. The changelog should include a full description of the interface, and also the sample user-space code that you showed at some point in another mail. That may help you get better review, and it will certainly help make a better man page.

Cheers,

Michael

#5 lore raw
Quoted 5 lines · On Mon, 25 Feb 2013 23:28:11 +0100 "Michael Kerrisk (man-pages)"…
Quoted 3 lines

Please, document the interface *completely* so that others can review the design and can then check that the implementation matches that design. As it stands, we're reduced to mind-reading.

+1.

I suddenly feel less lonely.

Quoted 5 lines

Andrey, please don't assume that people have read past mails. Even if they have, they're forgetful. Don't make people hunt down old mails to try to put the pieces together. The changelog should include a full description of the interface, and also the sample user-space code that you showed at some point in another mail.

If there's sample userspace code, there are all sorts of good reasons for putting that into tools/testing/selftests/your-name-here/

Quoted 2 lines

That may help you get better
review, and it will certainly help make a better man page.

#6 lore raw
In reply to Andrew Morton

Andrew, thank you for the comments. I will fix them and send a new patch. A few comments are inline.

2013/2/26 Andrew Morton <akpm-de/[email protected]>:

Quoted 8 lines

On Mon, 25 Feb 2013 14:06:53 +0400

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 647

Quoted 2 lines
kernel/ptrace.c +2 -0
+     for (i = 0; i < arg.nr; i++) {
+             off = arg.off + i;

"long long" = "u64" + "int".

Wanna have a little think about what we're doing here, see if we can pick the most appropriate types?

A number of signal has the type int, because the system call ptrace() return "int" and my interface supposed to return a number of dumped signals.
"off" is signed to check on a negative value and it can not be more then MAX_RAM / sizeof(siginfo), so
long long is enough. It can be changed on s64 for avoiding misleading.

Quoted 15 lines

`off' could be made local to this loop, which is a bit neater.

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 659

Quoted 11 lines
kernel/ptrace.c +11 -0
+             spin_lock_irq(&child->sighand->siglock);
+             list_for_each_entry(q, &pending->list, list) {
+                     if (!off--) {
+                             copy_siginfo(&info, &q->info);
+                             break;
+                     }
+             }
+             spin_unlock_irq(&child->sighand->siglock);
+
+             if (off >= 0)
+                     break;
Quoted 7 lines

<scratches head>

Oh I see what you did there - the user requested an entry which is beyond the end of the list? A code comment would be nice.

What's the return value in this case? "i". So how does userspace find out what happened?

The request PTRACE_PEEKSIGINFO returns a number of dumped signals. If a signal with the specified sequence number doesn't exist, ptrace returns 0.

Quoted 22 lines

Please fully describe the interface so things such as this can be understood.

Quoted 18 lines
kernel/ptrace.c +18 -0
+#ifdef CONFIG_COMPAT
+             if (unlikely(is_compat_task())) {
+                     compat_siginfo_t __user *uinfo = compat_ptr(data);
+
+                     ret = copy_siginfo_to_user32(uinfo, &info);
+                     if (!ret)
+                             ret = __put_user(info.si_code, &uinfo->si_code);
+             } else
+#endif
+             {
+                     siginfo_t __user *uinfo = (siginfo_t __user *) data;
+
+                     ret = copy_siginfo_to_user(uinfo, &info);
+                     if (!ret)
+                             ret = __put_user(info.si_code, &uinfo->si_code);
+             }
+
+             if (ret)
Quoted 8 lines

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 684

Quoted 6 lines
kernel/ptrace.c +6 -0
+                     break;
+
+             data += sizeof(siginfo_t);
+
+             if (signal_pending(current)) {
+                     i++;

What does the i++ do?

For accounting a current siginfo. I will add a comment here.

Quoted 11 lines

kernel/ptrace.c static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · line 691

Quoted 8 lines
kernel/ptrace.c +7 -0
+                     break;
+             }
+
+             cond_resched();
+     }
+
+     return i ? : ret;

I hate that gcc thing :(

Quoted 2 lines

Also, is it buggy? `ret' might be -EFAULT here. We appear to be using read() return semantics here?

I have tried to implement read() semantics here. ptrace() returns a number of dumped signals even if dumping of the last signal is failed. And it returns an error if not one signal has been dumped successfully.

The similar logic is used in read()
generic_file_aio_read()
....

                retval += desc.written;
                if (desc.error) {
                        retval = retval ?: desc.error;
                        break;
                }

Thanks,
Andrey