linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* deadlock caused by printk
@ 2011-03-25 12:15 Ming Lei
  2011-03-25 12:21 ` Peter Zijlstra
  0 siblings, 1 reply; 7+ messages in thread
From: Ming Lei @ 2011-03-25 12:15 UTC (permalink / raw)
  To: torvalds, Peter Zijlstra, Ingo Molnar; +Cc: Linux Kernel Mailing List

Hi,

These days I was studying "perf code" and try to dump some debug
info in event_sched_out(): kernel/perf_event.c by printk, but found
system can hang easily.

After digging into related code for several days, I find the hang is
caused by spinlock deadlock when acquiring rq->lock in task_rq_lock(),
which is called in the path below:

          try_to_wake_up<-wake_up_process<-up<-console_unlock<-printk.

I wonder if there are some usage conventions about printk, which says
explicitly that printk can not by used in some places of kernel. Or does the
above show that it is a bug of kernel?

If the above is really a limitation of printk, have we other dump functions
which may be used safely in some places of kernel source code where
printk is not allowed.

thanks,
-- 
Lei Ming

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: deadlock caused by printk
  2011-03-25 12:15 deadlock caused by printk Ming Lei
@ 2011-03-25 12:21 ` Peter Zijlstra
  2011-03-25 12:26   ` Peter Zijlstra
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Zijlstra @ 2011-03-25 12:21 UTC (permalink / raw)
  To: Ming Lei; +Cc: torvalds, Ingo Molnar, Linux Kernel Mailing List

On Fri, 2011-03-25 at 20:15 +0800, Ming Lei wrote:
> Hi,
> 
> These days I was studying "perf code" and try to dump some debug
> info in event_sched_out(): kernel/perf_event.c by printk, but found
> system can hang easily.
> 
> After digging into related code for several days, I find the hang is
> caused by spinlock deadlock when acquiring rq->lock in task_rq_lock(),
> which is called in the path below:
> 
>           try_to_wake_up<-wake_up_process<-up<-console_unlock<-printk.
> 
> I wonder if there are some usage conventions about printk, which says
> explicitly that printk can not by used in some places of kernel. Or does the
> above show that it is a bug of kernel?
> 
> If the above is really a limitation of printk, have we other dump functions
> which may be used safely in some places of kernel source code where
> printk is not allowed.

Don't use printk from NMI context, that's never going to work well.

One thing you could to is use trace_printk() and use ftrace to debug
perf.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: deadlock caused by printk
  2011-03-25 12:21 ` Peter Zijlstra
@ 2011-03-25 12:26   ` Peter Zijlstra
  2011-03-25 14:30     ` Ming Lei
  2011-03-25 18:40     ` Steven Rostedt
  0 siblings, 2 replies; 7+ messages in thread
From: Peter Zijlstra @ 2011-03-25 12:26 UTC (permalink / raw)
  To: Ming Lei; +Cc: torvalds, Ingo Molnar, Linux Kernel Mailing List

On Fri, 2011-03-25 at 13:21 +0100, Peter Zijlstra wrote:
> On Fri, 2011-03-25 at 20:15 +0800, Ming Lei wrote:
> > Hi,
> > 
> > These days I was studying "perf code" and try to dump some debug
> > info in event_sched_out(): kernel/perf_event.c by printk, but found
> > system can hang easily.
> > 
> > After digging into related code for several days, I find the hang is
> > caused by spinlock deadlock when acquiring rq->lock in task_rq_lock(),
> > which is called in the path below:
> > 
> >           try_to_wake_up<-wake_up_process<-up<-console_unlock<-printk.
> > 
> > I wonder if there are some usage conventions about printk, which says
> > explicitly that printk can not by used in some places of kernel. Or does the
> > above show that it is a bug of kernel?
> > 
> > If the above is really a limitation of printk, have we other dump functions
> > which may be used safely in some places of kernel source code where
> > printk is not allowed.
> 
> Don't use printk from NMI context, that's never going to work well.

OK, so I probably should shut down the computer and go back to being
ill, the thing you mentioned isn't in fact NMI context, but yeah using
it from inside the scheduler isn't going to be fun either.

> One thing you could to is use trace_printk() and use ftrace to debug
> perf.

That should still work.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: deadlock caused by printk
  2011-03-25 12:26   ` Peter Zijlstra
@ 2011-03-25 14:30     ` Ming Lei
  2011-03-25 14:50       ` Peter Zijlstra
  2011-03-25 18:40     ` Steven Rostedt
  1 sibling, 1 reply; 7+ messages in thread
From: Ming Lei @ 2011-03-25 14:30 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: torvalds, Ingo Molnar, Linux Kernel Mailing List

Hi,

2011/3/25 Peter Zijlstra <peterz@infradead.org>:
>> One thing you could to is use trace_printk() and use ftrace to debug
>> perf.
>
> That should still work.

I will try it, thanks.

But seems ftrace can't dump out the messages if system hangs, so could
we introduce a smart and simple "printk" which can be used in any
contexts?

thanks,
-- 
Lei Ming

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: deadlock caused by printk
  2011-03-25 14:30     ` Ming Lei
@ 2011-03-25 14:50       ` Peter Zijlstra
  2011-03-25 18:41         ` Steven Rostedt
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Zijlstra @ 2011-03-25 14:50 UTC (permalink / raw)
  To: Ming Lei; +Cc: torvalds, Ingo Molnar, Linux Kernel Mailing List

On Fri, 2011-03-25 at 22:30 +0800, Ming Lei wrote:
> 
> But seems ftrace can't dump out the messages if system hangs,

True.

>  so could
> we introduce a smart and simple "printk" which can be used in any
> contexts? 

I think you could use early_printk() for that, it doesn't bother with
all the syslog muck.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: deadlock caused by printk
  2011-03-25 12:26   ` Peter Zijlstra
  2011-03-25 14:30     ` Ming Lei
@ 2011-03-25 18:40     ` Steven Rostedt
  1 sibling, 0 replies; 7+ messages in thread
From: Steven Rostedt @ 2011-03-25 18:40 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Ming Lei, torvalds, Ingo Molnar, Linux Kernel Mailing List

On Fri, Mar 25, 2011 at 01:26:00PM +0100, Peter Zijlstra wrote:
> On Fri, 2011-03-25 at 13:21 +0100, Peter Zijlstra wrote:
> > On Fri, 2011-03-25 at 20:15 +0800, Ming Lei wrote:
> > > Hi,
> > > 
> > > These days I was studying "perf code" and try to dump some debug
> > > info in event_sched_out(): kernel/perf_event.c by printk, but found
> > > system can hang easily.
> > > 
> > > After digging into related code for several days, I find the hang is
> > > caused by spinlock deadlock when acquiring rq->lock in task_rq_lock(),
> > > which is called in the path below:
> > > 
> > >           try_to_wake_up<-wake_up_process<-up<-console_unlock<-printk.

I've brought this up before, and basically was told "don't do that" ;)

> > > 
> > > I wonder if there are some usage conventions about printk, which says
> > > explicitly that printk can not by used in some places of kernel. Or does the
> > > above show that it is a bug of kernel?

Just don't use it when the rq lock his held. Which you probably don't
want to anyway since most paths that require rq lock can flood the
console with printk messages.

> 
> > One thing you could to is use trace_printk() and use ftrace to debug
> > perf.
> 
> That should still work.

Yep, trace_printk() is the perfect thing to use to debug anything. I
tried hard to make it work in all contexts.

You can also set ftrace_dump_on_opps on the command line, or echo 1 into
/proc/sys/kernel/ftrace_dump_on_oops at run time if you want the ftrace
buffer to write to the console if the box crashes.

And finally, early_printk() works in these contexts too, as it doesn't
grab any locks. But because it does not, it can easily get corrupted
buffers.

-- Steve


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: deadlock caused by printk
  2011-03-25 14:50       ` Peter Zijlstra
@ 2011-03-25 18:41         ` Steven Rostedt
  0 siblings, 0 replies; 7+ messages in thread
From: Steven Rostedt @ 2011-03-25 18:41 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Ming Lei, torvalds, Ingo Molnar, Linux Kernel Mailing List

On Fri, Mar 25, 2011 at 03:50:27PM +0100, Peter Zijlstra wrote:
> On Fri, 2011-03-25 at 22:30 +0800, Ming Lei wrote:
> > 
> > But seems ftrace can't dump out the messages if system hangs,
> 
> True.

Well, matters how the system hangs. Add in a nmi watchdog or other
watchdogs which can also trigger the ftrace_dump_on_oops.

> 
> >  so could
> > we introduce a smart and simple "printk" which can be used in any
> > contexts? 
> 
> I think you could use early_printk() for that, it doesn't bother with
> all the syslog muck.
>

Heh, I should have read the thread before replying to the first post ;)

-- Steve


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-03-25 18:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-25 12:15 deadlock caused by printk Ming Lei
2011-03-25 12:21 ` Peter Zijlstra
2011-03-25 12:26   ` Peter Zijlstra
2011-03-25 14:30     ` Ming Lei
2011-03-25 14:50       ` Peter Zijlstra
2011-03-25 18:41         ` Steven Rostedt
2011-03-25 18:40     ` Steven Rostedt

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).