xref: /linux/kernel/trace/trace_functions_graph.c (revision c132be2c4fcc1150ad0791c2a85dd4c9ad0bd0c8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Function graph tracer.
5  * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
6  * Mostly borrowed from function tracer which
7  * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
8  *
9  */
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
12 #include <linux/interrupt.h>
13 #include <linux/slab.h>
14 #include <linux/fs.h>
15 
16 #include "trace.h"
17 #include "trace_output.h"
18 
19 /* When set, irq functions will be ignored */
20 static int ftrace_graph_skip_irqs;
21 
22 struct fgraph_cpu_data {
23 	pid_t		last_pid;
24 	int		depth;
25 	int		depth_irq;
26 	int		ignore;
27 	unsigned long	enter_funcs[FTRACE_RETFUNC_DEPTH];
28 };
29 
30 struct fgraph_data {
31 	struct fgraph_cpu_data __percpu *cpu_data;
32 
33 	/* Place to preserve last processed entry. */
34 	struct ftrace_graph_ent_entry	ent;
35 	struct ftrace_graph_ret_entry	ret;
36 	int				failed;
37 	int				cpu;
38 };
39 
40 #define TRACE_GRAPH_INDENT	2
41 
42 unsigned int fgraph_max_depth;
43 
44 static struct tracer_opt trace_opts[] = {
45 	/* Display overruns? (for self-debug purpose) */
46 	{ TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
47 	/* Display CPU ? */
48 	{ TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
49 	/* Display Overhead ? */
50 	{ TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
51 	/* Display proc name/pid */
52 	{ TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
53 	/* Display duration of execution */
54 	{ TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
55 	/* Display absolute time of an entry */
56 	{ TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
57 	/* Display interrupts */
58 	{ TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },
59 	/* Display function name after trailing } */
60 	{ TRACER_OPT(funcgraph-tail, TRACE_GRAPH_PRINT_TAIL) },
61 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL
62 	/* Display function return value ? */
63 	{ TRACER_OPT(funcgraph-retval, TRACE_GRAPH_PRINT_RETVAL) },
64 	/* Display function return value in hexadecimal format ? */
65 	{ TRACER_OPT(funcgraph-retval-hex, TRACE_GRAPH_PRINT_RETVAL_HEX) },
66 #endif
67 	/* Include sleep time (scheduled out) between entry and return */
68 	{ TRACER_OPT(sleep-time, TRACE_GRAPH_SLEEP_TIME) },
69 
70 #ifdef CONFIG_FUNCTION_PROFILER
71 	/* Include time within nested functions */
72 	{ TRACER_OPT(graph-time, TRACE_GRAPH_GRAPH_TIME) },
73 #endif
74 
75 	{ } /* Empty entry */
76 };
77 
78 static struct tracer_flags tracer_flags = {
79 	/* Don't display overruns, proc, or tail by default */
80 	.val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
81 	       TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS |
82 	       TRACE_GRAPH_SLEEP_TIME | TRACE_GRAPH_GRAPH_TIME,
83 	.opts = trace_opts
84 };
85 
86 /*
87  * DURATION column is being also used to display IRQ signs,
88  * following values are used by print_graph_irq and others
89  * to fill in space into DURATION column.
90  */
91 enum {
92 	FLAGS_FILL_FULL  = 1 << TRACE_GRAPH_PRINT_FILL_SHIFT,
93 	FLAGS_FILL_START = 2 << TRACE_GRAPH_PRINT_FILL_SHIFT,
94 	FLAGS_FILL_END   = 3 << TRACE_GRAPH_PRINT_FILL_SHIFT,
95 };
96 
97 static void
98 print_graph_duration(struct trace_array *tr, unsigned long long duration,
99 		     struct trace_seq *s, u32 flags);
100 
101 int __trace_graph_entry(struct trace_array *tr,
102 				struct ftrace_graph_ent *trace,
103 				unsigned int trace_ctx)
104 {
105 	struct trace_event_call *call = &event_funcgraph_entry;
106 	struct ring_buffer_event *event;
107 	struct trace_buffer *buffer = tr->array_buffer.buffer;
108 	struct ftrace_graph_ent_entry *entry;
109 
110 	event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
111 					  sizeof(*entry), trace_ctx);
112 	if (!event)
113 		return 0;
114 	entry	= ring_buffer_event_data(event);
115 	entry->graph_ent			= *trace;
116 	if (!call_filter_check_discard(call, entry, buffer, event))
117 		trace_buffer_unlock_commit_nostack(buffer, event);
118 
119 	return 1;
120 }
121 
122 static inline int ftrace_graph_ignore_irqs(void)
123 {
124 	if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
125 		return 0;
126 
127 	return in_hardirq();
128 }
129 
130 int trace_graph_entry(struct ftrace_graph_ent *trace,
131 		      struct fgraph_ops *gops)
132 {
133 	struct trace_array *tr = gops->private;
134 	struct trace_array_cpu *data;
135 	unsigned long flags;
136 	unsigned int trace_ctx;
137 	long disabled;
138 	int ret;
139 	int cpu;
140 
141 	if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT))
142 		return 0;
143 
144 	/*
145 	 * Do not trace a function if it's filtered by set_graph_notrace.
146 	 * Make the index of ret stack negative to indicate that it should
147 	 * ignore further functions.  But it needs its own ret stack entry
148 	 * to recover the original index in order to continue tracing after
149 	 * returning from the function.
150 	 */
151 	if (ftrace_graph_notrace_addr(trace->func)) {
152 		trace_recursion_set(TRACE_GRAPH_NOTRACE_BIT);
153 		/*
154 		 * Need to return 1 to have the return called
155 		 * that will clear the NOTRACE bit.
156 		 */
157 		return 1;
158 	}
159 
160 	if (!ftrace_trace_task(tr))
161 		return 0;
162 
163 	if (ftrace_graph_ignore_func(trace))
164 		return 0;
165 
166 	if (ftrace_graph_ignore_irqs())
167 		return 0;
168 
169 	/*
170 	 * Stop here if tracing_threshold is set. We only write function return
171 	 * events to the ring buffer.
172 	 */
173 	if (tracing_thresh)
174 		return 1;
175 
176 	local_irq_save(flags);
177 	cpu = raw_smp_processor_id();
178 	data = per_cpu_ptr(tr->array_buffer.data, cpu);
179 	disabled = atomic_inc_return(&data->disabled);
180 	if (likely(disabled == 1)) {
181 		trace_ctx = tracing_gen_ctx_flags(flags);
182 		ret = __trace_graph_entry(tr, trace, trace_ctx);
183 	} else {
184 		ret = 0;
185 	}
186 
187 	atomic_dec(&data->disabled);
188 	local_irq_restore(flags);
189 
190 	return ret;
191 }
192 
193 static void
194 __trace_graph_function(struct trace_array *tr,
195 		unsigned long ip, unsigned int trace_ctx)
196 {
197 	u64 time = trace_clock_local();
198 	struct ftrace_graph_ent ent = {
199 		.func  = ip,
200 		.depth = 0,
201 	};
202 	struct ftrace_graph_ret ret = {
203 		.func     = ip,
204 		.depth    = 0,
205 		.calltime = time,
206 		.rettime  = time,
207 	};
208 
209 	__trace_graph_entry(tr, &ent, trace_ctx);
210 	__trace_graph_return(tr, &ret, trace_ctx);
211 }
212 
213 void
214 trace_graph_function(struct trace_array *tr,
215 		unsigned long ip, unsigned long parent_ip,
216 		unsigned int trace_ctx)
217 {
218 	__trace_graph_function(tr, ip, trace_ctx);
219 }
220 
221 void __trace_graph_return(struct trace_array *tr,
222 				struct ftrace_graph_ret *trace,
223 				unsigned int trace_ctx)
224 {
225 	struct trace_event_call *call = &event_funcgraph_exit;
226 	struct ring_buffer_event *event;
227 	struct trace_buffer *buffer = tr->array_buffer.buffer;
228 	struct ftrace_graph_ret_entry *entry;
229 
230 	event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
231 					  sizeof(*entry), trace_ctx);
232 	if (!event)
233 		return;
234 	entry	= ring_buffer_event_data(event);
235 	entry->ret				= *trace;
236 	if (!call_filter_check_discard(call, entry, buffer, event))
237 		trace_buffer_unlock_commit_nostack(buffer, event);
238 }
239 
240 void trace_graph_return(struct ftrace_graph_ret *trace,
241 			struct fgraph_ops *gops)
242 {
243 	struct trace_array *tr = gops->private;
244 	struct trace_array_cpu *data;
245 	unsigned long flags;
246 	unsigned int trace_ctx;
247 	long disabled;
248 	int cpu;
249 
250 	ftrace_graph_addr_finish(trace);
251 
252 	if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {
253 		trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);
254 		return;
255 	}
256 
257 	local_irq_save(flags);
258 	cpu = raw_smp_processor_id();
259 	data = per_cpu_ptr(tr->array_buffer.data, cpu);
260 	disabled = atomic_inc_return(&data->disabled);
261 	if (likely(disabled == 1)) {
262 		trace_ctx = tracing_gen_ctx_flags(flags);
263 		__trace_graph_return(tr, trace, trace_ctx);
264 	}
265 	atomic_dec(&data->disabled);
266 	local_irq_restore(flags);
267 }
268 
269 static void trace_graph_thresh_return(struct ftrace_graph_ret *trace,
270 				      struct fgraph_ops *gops)
271 {
272 	ftrace_graph_addr_finish(trace);
273 
274 	if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {
275 		trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);
276 		return;
277 	}
278 
279 	if (tracing_thresh &&
280 	    (trace->rettime - trace->calltime < tracing_thresh))
281 		return;
282 	else
283 		trace_graph_return(trace, gops);
284 }
285 
286 static struct fgraph_ops funcgraph_ops = {
287 	.entryfunc = &trace_graph_entry,
288 	.retfunc = &trace_graph_return,
289 };
290 
291 int allocate_fgraph_ops(struct trace_array *tr, struct ftrace_ops *ops)
292 {
293 	struct fgraph_ops *gops;
294 
295 	gops = kzalloc(sizeof(*gops), GFP_KERNEL);
296 	if (!gops)
297 		return -ENOMEM;
298 
299 	gops->entryfunc = &trace_graph_entry;
300 	gops->retfunc = &trace_graph_return;
301 
302 	tr->gops = gops;
303 	gops->private = tr;
304 
305 	fgraph_init_ops(&gops->ops, ops);
306 
307 	return 0;
308 }
309 
310 void free_fgraph_ops(struct trace_array *tr)
311 {
312 	kfree(tr->gops);
313 }
314 
315 __init void init_array_fgraph_ops(struct trace_array *tr, struct ftrace_ops *ops)
316 {
317 	tr->gops = &funcgraph_ops;
318 	funcgraph_ops.private = tr;
319 	fgraph_init_ops(&tr->gops->ops, ops);
320 }
321 
322 static int graph_trace_init(struct trace_array *tr)
323 {
324 	int ret;
325 
326 	tr->gops->entryfunc = trace_graph_entry;
327 
328 	if (tracing_thresh)
329 		tr->gops->retfunc = trace_graph_thresh_return;
330 	else
331 		tr->gops->retfunc = trace_graph_return;
332 
333 	/* Make gops functions are visible before we start tracing */
334 	smp_mb();
335 
336 	ret = register_ftrace_graph(tr->gops);
337 	if (ret)
338 		return ret;
339 	tracing_start_cmdline_record();
340 
341 	return 0;
342 }
343 
344 static void graph_trace_reset(struct trace_array *tr)
345 {
346 	tracing_stop_cmdline_record();
347 	unregister_ftrace_graph(tr->gops);
348 }
349 
350 static int graph_trace_update_thresh(struct trace_array *tr)
351 {
352 	graph_trace_reset(tr);
353 	return graph_trace_init(tr);
354 }
355 
356 static int max_bytes_for_cpu;
357 
358 static void print_graph_cpu(struct trace_seq *s, int cpu)
359 {
360 	/*
361 	 * Start with a space character - to make it stand out
362 	 * to the right a bit when trace output is pasted into
363 	 * email:
364 	 */
365 	trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
366 }
367 
368 #define TRACE_GRAPH_PROCINFO_LENGTH	14
369 
370 static void print_graph_proc(struct trace_seq *s, pid_t pid)
371 {
372 	char comm[TASK_COMM_LEN];
373 	/* sign + log10(MAX_INT) + '\0' */
374 	char pid_str[11];
375 	int spaces = 0;
376 	int len;
377 	int i;
378 
379 	trace_find_cmdline(pid, comm);
380 	comm[7] = '\0';
381 	sprintf(pid_str, "%d", pid);
382 
383 	/* 1 stands for the "-" character */
384 	len = strlen(comm) + strlen(pid_str) + 1;
385 
386 	if (len < TRACE_GRAPH_PROCINFO_LENGTH)
387 		spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
388 
389 	/* First spaces to align center */
390 	for (i = 0; i < spaces / 2; i++)
391 		trace_seq_putc(s, ' ');
392 
393 	trace_seq_printf(s, "%s-%s", comm, pid_str);
394 
395 	/* Last spaces to align center */
396 	for (i = 0; i < spaces - (spaces / 2); i++)
397 		trace_seq_putc(s, ' ');
398 }
399 
400 
401 static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
402 {
403 	trace_seq_putc(s, ' ');
404 	trace_print_lat_fmt(s, entry);
405 	trace_seq_puts(s, " | ");
406 }
407 
408 /* If the pid changed since the last trace, output this event */
409 static void
410 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
411 {
412 	pid_t prev_pid;
413 	pid_t *last_pid;
414 
415 	if (!data)
416 		return;
417 
418 	last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
419 
420 	if (*last_pid == pid)
421 		return;
422 
423 	prev_pid = *last_pid;
424 	*last_pid = pid;
425 
426 	if (prev_pid == -1)
427 		return;
428 /*
429  * Context-switch trace line:
430 
431  ------------------------------------------
432  | 1)  migration/0--1  =>  sshd-1755
433  ------------------------------------------
434 
435  */
436 	trace_seq_puts(s, " ------------------------------------------\n");
437 	print_graph_cpu(s, cpu);
438 	print_graph_proc(s, prev_pid);
439 	trace_seq_puts(s, " => ");
440 	print_graph_proc(s, pid);
441 	trace_seq_puts(s, "\n ------------------------------------------\n\n");
442 }
443 
444 static struct ftrace_graph_ret_entry *
445 get_return_for_leaf(struct trace_iterator *iter,
446 		struct ftrace_graph_ent_entry *curr)
447 {
448 	struct fgraph_data *data = iter->private;
449 	struct ring_buffer_iter *ring_iter = NULL;
450 	struct ring_buffer_event *event;
451 	struct ftrace_graph_ret_entry *next;
452 
453 	/*
454 	 * If the previous output failed to write to the seq buffer,
455 	 * then we just reuse the data from before.
456 	 */
457 	if (data && data->failed) {
458 		curr = &data->ent;
459 		next = &data->ret;
460 	} else {
461 
462 		ring_iter = trace_buffer_iter(iter, iter->cpu);
463 
464 		/* First peek to compare current entry and the next one */
465 		if (ring_iter)
466 			event = ring_buffer_iter_peek(ring_iter, NULL);
467 		else {
468 			/*
469 			 * We need to consume the current entry to see
470 			 * the next one.
471 			 */
472 			ring_buffer_consume(iter->array_buffer->buffer, iter->cpu,
473 					    NULL, NULL);
474 			event = ring_buffer_peek(iter->array_buffer->buffer, iter->cpu,
475 						 NULL, NULL);
476 		}
477 
478 		if (!event)
479 			return NULL;
480 
481 		next = ring_buffer_event_data(event);
482 
483 		if (data) {
484 			/*
485 			 * Save current and next entries for later reference
486 			 * if the output fails.
487 			 */
488 			data->ent = *curr;
489 			/*
490 			 * If the next event is not a return type, then
491 			 * we only care about what type it is. Otherwise we can
492 			 * safely copy the entire event.
493 			 */
494 			if (next->ent.type == TRACE_GRAPH_RET)
495 				data->ret = *next;
496 			else
497 				data->ret.ent.type = next->ent.type;
498 		}
499 	}
500 
501 	if (next->ent.type != TRACE_GRAPH_RET)
502 		return NULL;
503 
504 	if (curr->ent.pid != next->ent.pid ||
505 			curr->graph_ent.func != next->ret.func)
506 		return NULL;
507 
508 	/* this is a leaf, now advance the iterator */
509 	if (ring_iter)
510 		ring_buffer_iter_advance(ring_iter);
511 
512 	return next;
513 }
514 
515 static void print_graph_abs_time(u64 t, struct trace_seq *s)
516 {
517 	unsigned long usecs_rem;
518 
519 	usecs_rem = do_div(t, NSEC_PER_SEC);
520 	usecs_rem /= 1000;
521 
522 	trace_seq_printf(s, "%5lu.%06lu |  ",
523 			 (unsigned long)t, usecs_rem);
524 }
525 
526 static void
527 print_graph_rel_time(struct trace_iterator *iter, struct trace_seq *s)
528 {
529 	unsigned long long usecs;
530 
531 	usecs = iter->ts - iter->array_buffer->time_start;
532 	do_div(usecs, NSEC_PER_USEC);
533 
534 	trace_seq_printf(s, "%9llu us |  ", usecs);
535 }
536 
537 static void
538 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
539 		enum trace_type type, int cpu, pid_t pid, u32 flags)
540 {
541 	struct trace_array *tr = iter->tr;
542 	struct trace_seq *s = &iter->seq;
543 	struct trace_entry *ent = iter->ent;
544 
545 	if (addr < (unsigned long)__irqentry_text_start ||
546 		addr >= (unsigned long)__irqentry_text_end)
547 		return;
548 
549 	if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
550 		/* Absolute time */
551 		if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
552 			print_graph_abs_time(iter->ts, s);
553 
554 		/* Relative time */
555 		if (flags & TRACE_GRAPH_PRINT_REL_TIME)
556 			print_graph_rel_time(iter, s);
557 
558 		/* Cpu */
559 		if (flags & TRACE_GRAPH_PRINT_CPU)
560 			print_graph_cpu(s, cpu);
561 
562 		/* Proc */
563 		if (flags & TRACE_GRAPH_PRINT_PROC) {
564 			print_graph_proc(s, pid);
565 			trace_seq_puts(s, " | ");
566 		}
567 
568 		/* Latency format */
569 		if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
570 			print_graph_lat_fmt(s, ent);
571 	}
572 
573 	/* No overhead */
574 	print_graph_duration(tr, 0, s, flags | FLAGS_FILL_START);
575 
576 	if (type == TRACE_GRAPH_ENT)
577 		trace_seq_puts(s, "==========>");
578 	else
579 		trace_seq_puts(s, "<==========");
580 
581 	print_graph_duration(tr, 0, s, flags | FLAGS_FILL_END);
582 	trace_seq_putc(s, '\n');
583 }
584 
585 void
586 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
587 {
588 	unsigned long nsecs_rem = do_div(duration, 1000);
589 	/* log10(ULONG_MAX) + '\0' */
590 	char usecs_str[21];
591 	char nsecs_str[5];
592 	int len;
593 	int i;
594 
595 	sprintf(usecs_str, "%lu", (unsigned long) duration);
596 
597 	/* Print msecs */
598 	trace_seq_printf(s, "%s", usecs_str);
599 
600 	len = strlen(usecs_str);
601 
602 	/* Print nsecs (we don't want to exceed 7 numbers) */
603 	if (len < 7) {
604 		size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
605 
606 		snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
607 		trace_seq_printf(s, ".%s", nsecs_str);
608 		len += strlen(nsecs_str) + 1;
609 	}
610 
611 	trace_seq_puts(s, " us ");
612 
613 	/* Print remaining spaces to fit the row's width */
614 	for (i = len; i < 8; i++)
615 		trace_seq_putc(s, ' ');
616 }
617 
618 static void
619 print_graph_duration(struct trace_array *tr, unsigned long long duration,
620 		     struct trace_seq *s, u32 flags)
621 {
622 	if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
623 	    !(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
624 		return;
625 
626 	/* No real adata, just filling the column with spaces */
627 	switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
628 	case FLAGS_FILL_FULL:
629 		trace_seq_puts(s, "              |  ");
630 		return;
631 	case FLAGS_FILL_START:
632 		trace_seq_puts(s, "  ");
633 		return;
634 	case FLAGS_FILL_END:
635 		trace_seq_puts(s, " |");
636 		return;
637 	}
638 
639 	/* Signal a overhead of time execution to the output */
640 	if (flags & TRACE_GRAPH_PRINT_OVERHEAD)
641 		trace_seq_printf(s, "%c ", trace_find_mark(duration));
642 	else
643 		trace_seq_puts(s, "  ");
644 
645 	trace_print_graph_duration(duration, s);
646 	trace_seq_puts(s, "|  ");
647 }
648 
649 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL
650 
651 #define __TRACE_GRAPH_PRINT_RETVAL TRACE_GRAPH_PRINT_RETVAL
652 
653 static void print_graph_retval(struct trace_seq *s, unsigned long retval,
654 				bool leaf, void *func, bool hex_format)
655 {
656 	unsigned long err_code = 0;
657 
658 	if (retval == 0 || hex_format)
659 		goto done;
660 
661 	/* Check if the return value matches the negative format */
662 	if (IS_ENABLED(CONFIG_64BIT) && (retval & BIT(31)) &&
663 		(((u64)retval) >> 32) == 0) {
664 		/* sign extension */
665 		err_code = (unsigned long)(s32)retval;
666 	} else {
667 		err_code = retval;
668 	}
669 
670 	if (!IS_ERR_VALUE(err_code))
671 		err_code = 0;
672 
673 done:
674 	if (leaf) {
675 		if (hex_format || (err_code == 0))
676 			trace_seq_printf(s, "%ps(); /* = 0x%lx */\n",
677 					func, retval);
678 		else
679 			trace_seq_printf(s, "%ps(); /* = %ld */\n",
680 					func, err_code);
681 	} else {
682 		if (hex_format || (err_code == 0))
683 			trace_seq_printf(s, "} /* %ps = 0x%lx */\n",
684 					func, retval);
685 		else
686 			trace_seq_printf(s, "} /* %ps = %ld */\n",
687 					func, err_code);
688 	}
689 }
690 
691 #else
692 
693 #define __TRACE_GRAPH_PRINT_RETVAL 0
694 
695 #define print_graph_retval(_seq, _retval, _leaf, _func, _format) do {} while (0)
696 
697 #endif
698 
699 /* Case of a leaf function on its call entry */
700 static enum print_line_t
701 print_graph_entry_leaf(struct trace_iterator *iter,
702 		struct ftrace_graph_ent_entry *entry,
703 		struct ftrace_graph_ret_entry *ret_entry,
704 		struct trace_seq *s, u32 flags)
705 {
706 	struct fgraph_data *data = iter->private;
707 	struct trace_array *tr = iter->tr;
708 	struct ftrace_graph_ret *graph_ret;
709 	struct ftrace_graph_ent *call;
710 	unsigned long long duration;
711 	int cpu = iter->cpu;
712 	int i;
713 
714 	graph_ret = &ret_entry->ret;
715 	call = &entry->graph_ent;
716 	duration = graph_ret->rettime - graph_ret->calltime;
717 
718 	if (data) {
719 		struct fgraph_cpu_data *cpu_data;
720 
721 		cpu_data = per_cpu_ptr(data->cpu_data, cpu);
722 
723 		/*
724 		 * Comments display at + 1 to depth. Since
725 		 * this is a leaf function, keep the comments
726 		 * equal to this depth.
727 		 */
728 		cpu_data->depth = call->depth - 1;
729 
730 		/* No need to keep this function around for this depth */
731 		if (call->depth < FTRACE_RETFUNC_DEPTH &&
732 		    !WARN_ON_ONCE(call->depth < 0))
733 			cpu_data->enter_funcs[call->depth] = 0;
734 	}
735 
736 	/* Overhead and duration */
737 	print_graph_duration(tr, duration, s, flags);
738 
739 	/* Function */
740 	for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
741 		trace_seq_putc(s, ' ');
742 
743 	/*
744 	 * Write out the function return value if the option function-retval is
745 	 * enabled.
746 	 */
747 	if (flags & __TRACE_GRAPH_PRINT_RETVAL)
748 		print_graph_retval(s, graph_ret->retval, true, (void *)call->func,
749 				!!(flags & TRACE_GRAPH_PRINT_RETVAL_HEX));
750 	else
751 		trace_seq_printf(s, "%ps();\n", (void *)call->func);
752 
753 	print_graph_irq(iter, graph_ret->func, TRACE_GRAPH_RET,
754 			cpu, iter->ent->pid, flags);
755 
756 	return trace_handle_return(s);
757 }
758 
759 static enum print_line_t
760 print_graph_entry_nested(struct trace_iterator *iter,
761 			 struct ftrace_graph_ent_entry *entry,
762 			 struct trace_seq *s, int cpu, u32 flags)
763 {
764 	struct ftrace_graph_ent *call = &entry->graph_ent;
765 	struct fgraph_data *data = iter->private;
766 	struct trace_array *tr = iter->tr;
767 	int i;
768 
769 	if (data) {
770 		struct fgraph_cpu_data *cpu_data;
771 		int cpu = iter->cpu;
772 
773 		cpu_data = per_cpu_ptr(data->cpu_data, cpu);
774 		cpu_data->depth = call->depth;
775 
776 		/* Save this function pointer to see if the exit matches */
777 		if (call->depth < FTRACE_RETFUNC_DEPTH &&
778 		    !WARN_ON_ONCE(call->depth < 0))
779 			cpu_data->enter_funcs[call->depth] = call->func;
780 	}
781 
782 	/* No time */
783 	print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
784 
785 	/* Function */
786 	for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
787 		trace_seq_putc(s, ' ');
788 
789 	trace_seq_printf(s, "%ps() {\n", (void *)call->func);
790 
791 	if (trace_seq_has_overflowed(s))
792 		return TRACE_TYPE_PARTIAL_LINE;
793 
794 	/*
795 	 * we already consumed the current entry to check the next one
796 	 * and see if this is a leaf.
797 	 */
798 	return TRACE_TYPE_NO_CONSUME;
799 }
800 
801 static void
802 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
803 		     int type, unsigned long addr, u32 flags)
804 {
805 	struct fgraph_data *data = iter->private;
806 	struct trace_entry *ent = iter->ent;
807 	struct trace_array *tr = iter->tr;
808 	int cpu = iter->cpu;
809 
810 	/* Pid */
811 	verif_pid(s, ent->pid, cpu, data);
812 
813 	if (type)
814 		/* Interrupt */
815 		print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
816 
817 	if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
818 		return;
819 
820 	/* Absolute time */
821 	if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
822 		print_graph_abs_time(iter->ts, s);
823 
824 	/* Relative time */
825 	if (flags & TRACE_GRAPH_PRINT_REL_TIME)
826 		print_graph_rel_time(iter, s);
827 
828 	/* Cpu */
829 	if (flags & TRACE_GRAPH_PRINT_CPU)
830 		print_graph_cpu(s, cpu);
831 
832 	/* Proc */
833 	if (flags & TRACE_GRAPH_PRINT_PROC) {
834 		print_graph_proc(s, ent->pid);
835 		trace_seq_puts(s, " | ");
836 	}
837 
838 	/* Latency format */
839 	if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
840 		print_graph_lat_fmt(s, ent);
841 
842 	return;
843 }
844 
845 /*
846  * Entry check for irq code
847  *
848  * returns 1 if
849  *  - we are inside irq code
850  *  - we just entered irq code
851  *
852  * returns 0 if
853  *  - funcgraph-interrupts option is set
854  *  - we are not inside irq code
855  */
856 static int
857 check_irq_entry(struct trace_iterator *iter, u32 flags,
858 		unsigned long addr, int depth)
859 {
860 	int cpu = iter->cpu;
861 	int *depth_irq;
862 	struct fgraph_data *data = iter->private;
863 
864 	/*
865 	 * If we are either displaying irqs, or we got called as
866 	 * a graph event and private data does not exist,
867 	 * then we bypass the irq check.
868 	 */
869 	if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
870 	    (!data))
871 		return 0;
872 
873 	depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
874 
875 	/*
876 	 * We are inside the irq code
877 	 */
878 	if (*depth_irq >= 0)
879 		return 1;
880 
881 	if ((addr < (unsigned long)__irqentry_text_start) ||
882 	    (addr >= (unsigned long)__irqentry_text_end))
883 		return 0;
884 
885 	/*
886 	 * We are entering irq code.
887 	 */
888 	*depth_irq = depth;
889 	return 1;
890 }
891 
892 /*
893  * Return check for irq code
894  *
895  * returns 1 if
896  *  - we are inside irq code
897  *  - we just left irq code
898  *
899  * returns 0 if
900  *  - funcgraph-interrupts option is set
901  *  - we are not inside irq code
902  */
903 static int
904 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
905 {
906 	int cpu = iter->cpu;
907 	int *depth_irq;
908 	struct fgraph_data *data = iter->private;
909 
910 	/*
911 	 * If we are either displaying irqs, or we got called as
912 	 * a graph event and private data does not exist,
913 	 * then we bypass the irq check.
914 	 */
915 	if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
916 	    (!data))
917 		return 0;
918 
919 	depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
920 
921 	/*
922 	 * We are not inside the irq code.
923 	 */
924 	if (*depth_irq == -1)
925 		return 0;
926 
927 	/*
928 	 * We are inside the irq code, and this is returning entry.
929 	 * Let's not trace it and clear the entry depth, since
930 	 * we are out of irq code.
931 	 *
932 	 * This condition ensures that we 'leave the irq code' once
933 	 * we are out of the entry depth. Thus protecting us from
934 	 * the RETURN entry loss.
935 	 */
936 	if (*depth_irq >= depth) {
937 		*depth_irq = -1;
938 		return 1;
939 	}
940 
941 	/*
942 	 * We are inside the irq code, and this is not the entry.
943 	 */
944 	return 1;
945 }
946 
947 static enum print_line_t
948 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
949 			struct trace_iterator *iter, u32 flags)
950 {
951 	struct fgraph_data *data = iter->private;
952 	struct ftrace_graph_ent *call = &field->graph_ent;
953 	struct ftrace_graph_ret_entry *leaf_ret;
954 	static enum print_line_t ret;
955 	int cpu = iter->cpu;
956 
957 	if (check_irq_entry(iter, flags, call->func, call->depth))
958 		return TRACE_TYPE_HANDLED;
959 
960 	print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
961 
962 	leaf_ret = get_return_for_leaf(iter, field);
963 	if (leaf_ret)
964 		ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
965 	else
966 		ret = print_graph_entry_nested(iter, field, s, cpu, flags);
967 
968 	if (data) {
969 		/*
970 		 * If we failed to write our output, then we need to make
971 		 * note of it. Because we already consumed our entry.
972 		 */
973 		if (s->full) {
974 			data->failed = 1;
975 			data->cpu = cpu;
976 		} else
977 			data->failed = 0;
978 	}
979 
980 	return ret;
981 }
982 
983 static enum print_line_t
984 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
985 		   struct trace_entry *ent, struct trace_iterator *iter,
986 		   u32 flags)
987 {
988 	unsigned long long duration = trace->rettime - trace->calltime;
989 	struct fgraph_data *data = iter->private;
990 	struct trace_array *tr = iter->tr;
991 	pid_t pid = ent->pid;
992 	int cpu = iter->cpu;
993 	int func_match = 1;
994 	int i;
995 
996 	if (check_irq_return(iter, flags, trace->depth))
997 		return TRACE_TYPE_HANDLED;
998 
999 	if (data) {
1000 		struct fgraph_cpu_data *cpu_data;
1001 		int cpu = iter->cpu;
1002 
1003 		cpu_data = per_cpu_ptr(data->cpu_data, cpu);
1004 
1005 		/*
1006 		 * Comments display at + 1 to depth. This is the
1007 		 * return from a function, we now want the comments
1008 		 * to display at the same level of the bracket.
1009 		 */
1010 		cpu_data->depth = trace->depth - 1;
1011 
1012 		if (trace->depth < FTRACE_RETFUNC_DEPTH &&
1013 		    !WARN_ON_ONCE(trace->depth < 0)) {
1014 			if (cpu_data->enter_funcs[trace->depth] != trace->func)
1015 				func_match = 0;
1016 			cpu_data->enter_funcs[trace->depth] = 0;
1017 		}
1018 	}
1019 
1020 	print_graph_prologue(iter, s, 0, 0, flags);
1021 
1022 	/* Overhead and duration */
1023 	print_graph_duration(tr, duration, s, flags);
1024 
1025 	/* Closing brace */
1026 	for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
1027 		trace_seq_putc(s, ' ');
1028 
1029 	/*
1030 	 * Always write out the function name and its return value if the
1031 	 * function-retval option is enabled.
1032 	 */
1033 	if (flags & __TRACE_GRAPH_PRINT_RETVAL) {
1034 		print_graph_retval(s, trace->retval, false, (void *)trace->func,
1035 			!!(flags & TRACE_GRAPH_PRINT_RETVAL_HEX));
1036 	} else {
1037 		/*
1038 		 * If the return function does not have a matching entry,
1039 		 * then the entry was lost. Instead of just printing
1040 		 * the '}' and letting the user guess what function this
1041 		 * belongs to, write out the function name. Always do
1042 		 * that if the funcgraph-tail option is enabled.
1043 		 */
1044 		if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
1045 			trace_seq_puts(s, "}\n");
1046 		else
1047 			trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
1048 	}
1049 
1050 	/* Overrun */
1051 	if (flags & TRACE_GRAPH_PRINT_OVERRUN)
1052 		trace_seq_printf(s, " (Overruns: %u)\n",
1053 				 trace->overrun);
1054 
1055 	print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1056 			cpu, pid, flags);
1057 
1058 	return trace_handle_return(s);
1059 }
1060 
1061 static enum print_line_t
1062 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1063 		    struct trace_iterator *iter, u32 flags)
1064 {
1065 	struct trace_array *tr = iter->tr;
1066 	unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
1067 	struct fgraph_data *data = iter->private;
1068 	struct trace_event *event;
1069 	int depth = 0;
1070 	int ret;
1071 	int i;
1072 
1073 	if (data)
1074 		depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
1075 
1076 	print_graph_prologue(iter, s, 0, 0, flags);
1077 
1078 	/* No time */
1079 	print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
1080 
1081 	/* Indentation */
1082 	if (depth > 0)
1083 		for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
1084 			trace_seq_putc(s, ' ');
1085 
1086 	/* The comment */
1087 	trace_seq_puts(s, "/* ");
1088 
1089 	switch (iter->ent->type) {
1090 	case TRACE_BPUTS:
1091 		ret = trace_print_bputs_msg_only(iter);
1092 		if (ret != TRACE_TYPE_HANDLED)
1093 			return ret;
1094 		break;
1095 	case TRACE_BPRINT:
1096 		ret = trace_print_bprintk_msg_only(iter);
1097 		if (ret != TRACE_TYPE_HANDLED)
1098 			return ret;
1099 		break;
1100 	case TRACE_PRINT:
1101 		ret = trace_print_printk_msg_only(iter);
1102 		if (ret != TRACE_TYPE_HANDLED)
1103 			return ret;
1104 		break;
1105 	default:
1106 		event = ftrace_find_event(ent->type);
1107 		if (!event)
1108 			return TRACE_TYPE_UNHANDLED;
1109 
1110 		ret = event->funcs->trace(iter, sym_flags, event);
1111 		if (ret != TRACE_TYPE_HANDLED)
1112 			return ret;
1113 	}
1114 
1115 	if (trace_seq_has_overflowed(s))
1116 		goto out;
1117 
1118 	/* Strip ending newline */
1119 	if (s->buffer[s->seq.len - 1] == '\n') {
1120 		s->buffer[s->seq.len - 1] = '\0';
1121 		s->seq.len--;
1122 	}
1123 
1124 	trace_seq_puts(s, " */\n");
1125  out:
1126 	return trace_handle_return(s);
1127 }
1128 
1129 
1130 enum print_line_t
1131 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1132 {
1133 	struct ftrace_graph_ent_entry *field;
1134 	struct fgraph_data *data = iter->private;
1135 	struct trace_entry *entry = iter->ent;
1136 	struct trace_seq *s = &iter->seq;
1137 	int cpu = iter->cpu;
1138 	int ret;
1139 
1140 	if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1141 		per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1142 		return TRACE_TYPE_HANDLED;
1143 	}
1144 
1145 	/*
1146 	 * If the last output failed, there's a possibility we need
1147 	 * to print out the missing entry which would never go out.
1148 	 */
1149 	if (data && data->failed) {
1150 		field = &data->ent;
1151 		iter->cpu = data->cpu;
1152 		ret = print_graph_entry(field, s, iter, flags);
1153 		if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1154 			per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1155 			ret = TRACE_TYPE_NO_CONSUME;
1156 		}
1157 		iter->cpu = cpu;
1158 		return ret;
1159 	}
1160 
1161 	switch (entry->type) {
1162 	case TRACE_GRAPH_ENT: {
1163 		/*
1164 		 * print_graph_entry() may consume the current event,
1165 		 * thus @field may become invalid, so we need to save it.
1166 		 * sizeof(struct ftrace_graph_ent_entry) is very small,
1167 		 * it can be safely saved at the stack.
1168 		 */
1169 		struct ftrace_graph_ent_entry saved;
1170 		trace_assign_type(field, entry);
1171 		saved = *field;
1172 		return print_graph_entry(&saved, s, iter, flags);
1173 	}
1174 	case TRACE_GRAPH_RET: {
1175 		struct ftrace_graph_ret_entry *field;
1176 		trace_assign_type(field, entry);
1177 		return print_graph_return(&field->ret, s, entry, iter, flags);
1178 	}
1179 	case TRACE_STACK:
1180 	case TRACE_FN:
1181 		/* dont trace stack and functions as comments */
1182 		return TRACE_TYPE_UNHANDLED;
1183 
1184 	default:
1185 		return print_graph_comment(s, entry, iter, flags);
1186 	}
1187 
1188 	return TRACE_TYPE_HANDLED;
1189 }
1190 
1191 static enum print_line_t
1192 print_graph_function(struct trace_iterator *iter)
1193 {
1194 	return print_graph_function_flags(iter, tracer_flags.val);
1195 }
1196 
1197 static enum print_line_t
1198 print_graph_function_event(struct trace_iterator *iter, int flags,
1199 			   struct trace_event *event)
1200 {
1201 	return print_graph_function(iter);
1202 }
1203 
1204 static void print_lat_header(struct seq_file *s, u32 flags)
1205 {
1206 	static const char spaces[] = "                "	/* 16 spaces */
1207 		"    "					/* 4 spaces */
1208 		"                 ";			/* 17 spaces */
1209 	int size = 0;
1210 
1211 	if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1212 		size += 16;
1213 	if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1214 		size += 16;
1215 	if (flags & TRACE_GRAPH_PRINT_CPU)
1216 		size += 4;
1217 	if (flags & TRACE_GRAPH_PRINT_PROC)
1218 		size += 17;
1219 
1220 	seq_printf(s, "#%.*s  _-----=> irqs-off        \n", size, spaces);
1221 	seq_printf(s, "#%.*s / _----=> need-resched    \n", size, spaces);
1222 	seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1223 	seq_printf(s, "#%.*s|| / _--=> preempt-depth   \n", size, spaces);
1224 	seq_printf(s, "#%.*s||| /                      \n", size, spaces);
1225 }
1226 
1227 static void __print_graph_headers_flags(struct trace_array *tr,
1228 					struct seq_file *s, u32 flags)
1229 {
1230 	int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT;
1231 
1232 	if (lat)
1233 		print_lat_header(s, flags);
1234 
1235 	/* 1st line */
1236 	seq_putc(s, '#');
1237 	if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1238 		seq_puts(s, "     TIME       ");
1239 	if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1240 		seq_puts(s, "   REL TIME     ");
1241 	if (flags & TRACE_GRAPH_PRINT_CPU)
1242 		seq_puts(s, " CPU");
1243 	if (flags & TRACE_GRAPH_PRINT_PROC)
1244 		seq_puts(s, "  TASK/PID       ");
1245 	if (lat)
1246 		seq_puts(s, "||||   ");
1247 	if (flags & TRACE_GRAPH_PRINT_DURATION)
1248 		seq_puts(s, "  DURATION   ");
1249 	seq_puts(s, "               FUNCTION CALLS\n");
1250 
1251 	/* 2nd line */
1252 	seq_putc(s, '#');
1253 	if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1254 		seq_puts(s, "      |         ");
1255 	if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1256 		seq_puts(s, "      |         ");
1257 	if (flags & TRACE_GRAPH_PRINT_CPU)
1258 		seq_puts(s, " |  ");
1259 	if (flags & TRACE_GRAPH_PRINT_PROC)
1260 		seq_puts(s, "   |    |        ");
1261 	if (lat)
1262 		seq_puts(s, "||||   ");
1263 	if (flags & TRACE_GRAPH_PRINT_DURATION)
1264 		seq_puts(s, "   |   |      ");
1265 	seq_puts(s, "               |   |   |   |\n");
1266 }
1267 
1268 static void print_graph_headers(struct seq_file *s)
1269 {
1270 	print_graph_headers_flags(s, tracer_flags.val);
1271 }
1272 
1273 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1274 {
1275 	struct trace_iterator *iter = s->private;
1276 	struct trace_array *tr = iter->tr;
1277 
1278 	if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
1279 		return;
1280 
1281 	if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) {
1282 		/* print nothing if the buffers are empty */
1283 		if (trace_empty(iter))
1284 			return;
1285 
1286 		print_trace_header(s, iter);
1287 	}
1288 
1289 	__print_graph_headers_flags(tr, s, flags);
1290 }
1291 
1292 void graph_trace_open(struct trace_iterator *iter)
1293 {
1294 	/* pid and depth on the last trace processed */
1295 	struct fgraph_data *data;
1296 	gfp_t gfpflags;
1297 	int cpu;
1298 
1299 	iter->private = NULL;
1300 
1301 	/* We can be called in atomic context via ftrace_dump() */
1302 	gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
1303 
1304 	data = kzalloc(sizeof(*data), gfpflags);
1305 	if (!data)
1306 		goto out_err;
1307 
1308 	data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags);
1309 	if (!data->cpu_data)
1310 		goto out_err_free;
1311 
1312 	for_each_possible_cpu(cpu) {
1313 		pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1314 		int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1315 		int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1316 		int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1317 
1318 		*pid = -1;
1319 		*depth = 0;
1320 		*ignore = 0;
1321 		*depth_irq = -1;
1322 	}
1323 
1324 	iter->private = data;
1325 
1326 	return;
1327 
1328  out_err_free:
1329 	kfree(data);
1330  out_err:
1331 	pr_warn("function graph tracer: not enough memory\n");
1332 }
1333 
1334 void graph_trace_close(struct trace_iterator *iter)
1335 {
1336 	struct fgraph_data *data = iter->private;
1337 
1338 	if (data) {
1339 		free_percpu(data->cpu_data);
1340 		kfree(data);
1341 	}
1342 }
1343 
1344 static int
1345 func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1346 {
1347 	if (bit == TRACE_GRAPH_PRINT_IRQS)
1348 		ftrace_graph_skip_irqs = !set;
1349 
1350 	if (bit == TRACE_GRAPH_SLEEP_TIME)
1351 		ftrace_graph_sleep_time_control(set);
1352 
1353 	if (bit == TRACE_GRAPH_GRAPH_TIME)
1354 		ftrace_graph_graph_time_control(set);
1355 
1356 	return 0;
1357 }
1358 
1359 static struct trace_event_functions graph_functions = {
1360 	.trace		= print_graph_function_event,
1361 };
1362 
1363 static struct trace_event graph_trace_entry_event = {
1364 	.type		= TRACE_GRAPH_ENT,
1365 	.funcs		= &graph_functions,
1366 };
1367 
1368 static struct trace_event graph_trace_ret_event = {
1369 	.type		= TRACE_GRAPH_RET,
1370 	.funcs		= &graph_functions
1371 };
1372 
1373 static struct tracer graph_trace __tracer_data = {
1374 	.name		= "function_graph",
1375 	.update_thresh	= graph_trace_update_thresh,
1376 	.open		= graph_trace_open,
1377 	.pipe_open	= graph_trace_open,
1378 	.close		= graph_trace_close,
1379 	.pipe_close	= graph_trace_close,
1380 	.init		= graph_trace_init,
1381 	.reset		= graph_trace_reset,
1382 	.print_line	= print_graph_function,
1383 	.print_header	= print_graph_headers,
1384 	.flags		= &tracer_flags,
1385 	.set_flag	= func_graph_set_flag,
1386 	.allow_instances = true,
1387 #ifdef CONFIG_FTRACE_SELFTEST
1388 	.selftest	= trace_selftest_startup_function_graph,
1389 #endif
1390 };
1391 
1392 
1393 static ssize_t
1394 graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
1395 		  loff_t *ppos)
1396 {
1397 	unsigned long val;
1398 	int ret;
1399 
1400 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1401 	if (ret)
1402 		return ret;
1403 
1404 	fgraph_max_depth = val;
1405 
1406 	*ppos += cnt;
1407 
1408 	return cnt;
1409 }
1410 
1411 static ssize_t
1412 graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
1413 		 loff_t *ppos)
1414 {
1415 	char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
1416 	int n;
1417 
1418 	n = sprintf(buf, "%d\n", fgraph_max_depth);
1419 
1420 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
1421 }
1422 
1423 static const struct file_operations graph_depth_fops = {
1424 	.open		= tracing_open_generic,
1425 	.write		= graph_depth_write,
1426 	.read		= graph_depth_read,
1427 	.llseek		= generic_file_llseek,
1428 };
1429 
1430 static __init int init_graph_tracefs(void)
1431 {
1432 	int ret;
1433 
1434 	ret = tracing_init_dentry();
1435 	if (ret)
1436 		return 0;
1437 
1438 	trace_create_file("max_graph_depth", TRACE_MODE_WRITE, NULL,
1439 			  NULL, &graph_depth_fops);
1440 
1441 	return 0;
1442 }
1443 fs_initcall(init_graph_tracefs);
1444 
1445 static __init int init_graph_trace(void)
1446 {
1447 	max_bytes_for_cpu = snprintf(NULL, 0, "%u", nr_cpu_ids - 1);
1448 
1449 	if (!register_trace_event(&graph_trace_entry_event)) {
1450 		pr_warn("Warning: could not register graph trace events\n");
1451 		return 1;
1452 	}
1453 
1454 	if (!register_trace_event(&graph_trace_ret_event)) {
1455 		pr_warn("Warning: could not register graph trace events\n");
1456 		return 1;
1457 	}
1458 
1459 	return register_tracer(&graph_trace);
1460 }
1461 
1462 core_initcall(init_graph_trace);
1463