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