preemptive kernel: ptrace fix
RFC PATCH 1/1 preemptive kernel: ptrace fix
I noticed (actually, I had a thought ptrace wouldnt work under preemption) that strace sometimes locks under the preemptible kernel, although is killable by CTRL-C.
The following is from arch/i386/kernel/ptrace.c :: syscall_trace:
1 current->state = TASK_STOPPED;
2 notify_parent(current, SIGCHLD);
3 schedule();
What if, between line one and two, preemption occurs? Now the task state is TASK_STOPPED, and thus will never be rescheduled. I presume this is the problem. The attached patch fixes this.
Running multiple straces in a tight loop for over an hour shows me the problem is fixed. Its obvious the above is a problem anyhow.
However, doing an `strace strace whatever' (ie, stracing strace), it still enters a stopped state about 20% of the time (before the patch, it locked almost 100%). I can't figure out why. Comments?
Yes, stracing strace is contorted and I don't care, but something else is obviously wrong -- you can do so on a non-preemption machine.
diff -urN linux-2.4.9-ac14-preempt/arch/i386/kernel/ptrace.c linux/arch/i386/kernel/ptrace.c > patch-rml-2.4.9-ac14-preempt-strace-fix-1
--- linux-2.4.9-ac14-preempt/arch/i386/kernel/ptrace.c Sat Sep 22 23:20:41 2001+++ linux/arch/i386/kernel/ptrace.c Sat Sep 22 23:42:11 2001
@@ -455,9 +455,11 @@
between a syscall stop and SIGTRAP delivery */
current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
? 0x80 : 0);
+ ctx_sw_off();
current->state = TASK_STOPPED;
notify_parent(current, SIGCHLD);
schedule();
+ ctx_sw_on();
/*
* this isn't the same as continuing with a signal, but it will do
* for normal use. strace only continues with a signal if the
Signature · 3 lines
Robert M. Love rml at ufl.edu rml at tech9.net
Quoted 3 lines · Robert Love
However, doing an `strace strace whatever' (ie, stracing strace), it still enters a stopped state about 20% of the time (before the patch, it locked almost 100%). I can't figure out why. Comments?
I fixed it. I am not overly sure why it fixes it, but the following patch (on top of the previous) fixes all the ptrace problems I can find.
In auditing the code, I fixed another problem which I will explain...
I'll put out a final patch in a bit...
arch/i386/kernel/signal.c +4 -0
@@ -611,9 +611,11 @@if ((current->ptrace &PT_PTRACED ) && signr !=SIGKILL ) {/* Let the debugger run. */ current->exit_code = signr; +ctx_sw_off (); current->state =TASK_STOPPED ;notify_parent (current,SIGCHLD );schedule (); +ctx_sw_on ();/* We're back. Did the debugger cancel the sig? */ if (!(signr = current->exit_code))
@@ -667,11 +669,13 @@/* FALLTHRU */ case SIGSTOP : +ctx_sw_off (); current->state =TASK_STOPPED ; current->exit_code = signr;if (!(current->p_pptr->sig->action[SIGCHLD -1 ].sa.sa_flags &SA_NOCLDSTOP ))notify_parent (current,SIGCHLD );schedule (); +ctx_sw_on ();continue ;case SIGQUIT :case SIGILL :case SIGTRAP :
Signature · 3 lines
Robert M. Love rml at ufl.edu rml at tech9.net