Skip to contentSkip to conversation

random dumb (or maybe not) question about sparc64

DISCUSSION Chris Torek 2 participants 5 messages Dec 9, 2008 – Dec 17, 2008

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

Here's something I think is possibly a bug:

The ptrace code uses flushw_user() to push user windows ... good enough, except, a bunch of other code then also does synchronize_user_stack(), but ptrace does not.

So, how does this work if you happen to hit a paged-out page in ptrace at the perfect inopportune time?

(This depends on whether you are ptrace()ing yourself, of course, which is an odd thing to do. But it might also hit if you stop a process that was pushing its registers to a paged-out page and once resumed, is going to finish copying spilled windows from the thread info reg_window[] array.)

There could be something obvious that I am missing, though.

(This came up as an aside while I was fussing with some moderately ugly stack tracing code, doing user stack traces within the kernel.)

Chris

maintainer
#1 lore raw

From: Chris Torek <[email protected]>
Date: Tue, 09 Dec 2008 04:46:52 -0700

Quoted 13 lines

The ptrace code uses flushw_user() to push user windows ... good enough, except, a bunch of other code then also does synchronize_user_stack(), but ptrace does not.

So, how does this work if you happen to hit a paged-out page in ptrace at the perfect inopportune time?

(This depends on whether you are ptrace()ing yourself, of course, which is an odd thing to do. But it might also hit if you stop a process that was pushing its registers to a paged-out page and once resumed, is going to finish copying spilled windows from the thread info reg_window[] array.)

Quoted 4 lines

There could be something obvious that I am missing, though.

(This came up as an aside while I was fussing with some moderately ugly stack tracing code, doing user stack traces within the kernel.)

Good catch, that is indeed a bug.

Another way to handle this would be to add a ptrace fetch operation that allows grabbing the window buffer from the thread_info struct.

Those flushw_user() calls you see in the regset code never execute for ptrace's normal usage (tracing another task). The task blocks first, then the tracing parent gets on the cpu and inspects the child reg state.

The ptrace inferior child task switch flushes the windows as a side effect.

We can't do this stack push unconditionally, otherwise we might recurse. We might be getting the signal because of a failed window buffer save to the stack.

Perhaps the best place would be in the arch_ptrace_stop() hook. I believe IA64 uses this for the register backing store on that cpu. It seems to be created exactly for the case we are discussing here. :)

What do you think?

maintainer
#2 lore raw
In reply to Chris Torek

From: David Miller <[email protected]>
Date: Tue, 09 Dec 2008 04:12:15 -0800 (PST)

Quoted 6 lines

From: Chris Torek <[email protected]>
Date: Tue, 09 Dec 2008 04:46:52 -0700

Quoted 3 lines

The ptrace code uses flushw_user() to push user windows ... good enough, except, a bunch of other code then also does synchronize_user_stack(), but ptrace does not.

...

Quoted 6 lines

Perhaps the best place would be in the arch_ptrace_stop() hook. I believe IA64 uses this for the register backing store on that cpu. It seems to be created exactly for the case we are discussing here. :)

What do you think?

I guess Chris is not excited enough about this issue to reply :-)

Anyways, here is the fix I'll probably push upstream. 32-bit sparc will need something similar and I'll implement that too.

Thanks for the report:

arch/sparc/include/asm/ptrace_64.h +9 -0
index 84e969f..cd6fbfc 100644
@@ -114,6 +114,7 @@ struct sparc_trapf {
 #ifdef __KERNEL__
 
 #include <linux/threads.h>
+#include <asm/system.h>
 
 static inline int pt_regs_trap_type(struct pt_regs *regs)
 {
@@ -130,6 +131,14 @@ static inline bool pt_regs_clear_syscall(struct pt_regs *regs)
 	return (regs->tstate &= ~TSTATE_SYSCALL);
 }
 
+#define arch_ptrace_stop_needed(exit_code, info) \
+({	flush_user_windows(); \
+	get_thread_wsaved() != 0; \
+})
+
+#define arch_ptrace_stop(exit_code, info) \
+	synchronize_user_stack()
+
 struct global_reg_snapshot {
 	unsigned long		tstate;
 	unsigned long		tpc;
#3 lore raw
In reply to Chris Torek
Quoted 2 lines

I guess Chris is not excited enough about this issue to reply :-)

Heh, more like, back-burnered, and not sufficiently knowledgeable about the ptrace code to have commented earlier.

I do now have some (sparc64) code for doing oprofile stack traces on interrupts I can send in now, though. There's one possibly-a-bug issue I'm not at all sure about, and various possible style items, and of course for those using sparc32, someone would have to make appropriate adjustments.

Is there any interest in the oprofile code?

Chris

maintainer
#4 lore raw
In reply to Chris Torek

From: Chris Torek <[email protected]>
Date: Wed, 17 Dec 2008 02:17:59 -0700

Quoted 6 lines
Quoted 2 lines

I guess Chris is not excited enough about this issue to reply :-)

Heh, more like, back-burnered, and not sufficiently knowledgeable about the ptrace code to have commented earlier.

Quoted 7 lines

I do now have some (sparc64) code for doing oprofile stack traces on interrupts I can send in now, though. There's one possibly-a-bug issue I'm not at all sure about, and various possible style items, and of course for those using sparc32, someone would have to make appropriate adjustments.

Is there any interest in the oprofile code?

Sure.

I added NMI oprofile support in the sparc-next-2.6 tree so you probably want to submit your changes relative to that:

	git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-next-2.6.git