Re: [tracing] cd8f62b481: BUG:sleeping_function_called_from_invalid_context_at_mm/slab.h

From: Steven Rostedt
Date: Wed Apr 01 2020 - 10:21:18 EST


On Wed, 1 Apr 2020 23:07:00 +0900
Masami Hiramatsu <mhiramat@xxxxxxxxxx> wrote:

> Hi Steve,
>
> On Thu, 26 Mar 2020 17:12:56 +0800
> kernel test robot <rong.a.chen@xxxxxxxxx> wrote:
>
> > FYI, we noticed the following commit (built with gcc-7):
> >
> > commit: cd8f62b481530fafbeacee0d3283b60bcf42d854 ("[PATCH 02/12 v2] tracing: Save off entry when peeking at next entry")
> > url: https://github.com/0day-ci/linux/commits/Steven-Rostedt/ring-buffer-tracing-Remove-disabling-of-ring-buffer-while-reading-trace-file/20200320-073240
> >
>
> Hmm, this seems that we can not call kmalloc() in ftrace_dump().
> Maybe we can fix it by
> - Use GFP_ATOMIC.
> or
> - Do not use iter->temp if it is NULL. (it is safe since ftrace_dump() stops tracing)
>
> What would you think?
>

Thanks for the reminder, I knew there was something that I had to deal with
and forgot to mark this report!

I already looked at it, and yes, this is an issue, but for PREEMPT_RT even
GFP_ATOMIC will fail. As it's not critical to record it, we can just check
for in atomic and not bother with the allocation.

-- Steve

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 6519b7afc499..7f1466253ca8 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -3487,6 +3487,14 @@ struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
*/
if (iter->ent && iter->ent != iter->temp) {
if (!iter->temp || iter->temp_size < iter->ent_size) {
+ /*
+ * This function is only used to add markers between
+ * events that are far apart (see trace_print_lat_context()),
+ * but if this is called in an atomic context (like NMIs)
+ * we can't call kmalloc(), thus just return NULL.
+ */
+ if (in_atomic() || irqs_disabled())
+ return NULL;
kfree(iter->temp);
iter->temp = kmalloc(iter->ent_size, GFP_KERNEL);
if (!iter->temp)