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