trace.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. #ifndef _LINUX_KERNEL_TRACE_H
  2. #define _LINUX_KERNEL_TRACE_H
  3. #include <linux/fs.h>
  4. #include <asm/atomic.h>
  5. #include <linux/sched.h>
  6. #include <linux/clocksource.h>
  7. #include <linux/ring_buffer.h>
  8. #include <linux/mmiotrace.h>
  9. #include <linux/ftrace.h>
  10. #include <trace/boot.h>
  11. #include <trace/kmemtrace.h>
  12. enum trace_type {
  13. __TRACE_FIRST_TYPE = 0,
  14. TRACE_FN,
  15. TRACE_CTX,
  16. TRACE_WAKE,
  17. TRACE_STACK,
  18. TRACE_PRINT,
  19. TRACE_SPECIAL,
  20. TRACE_MMIO_RW,
  21. TRACE_MMIO_MAP,
  22. TRACE_BRANCH,
  23. TRACE_BOOT_CALL,
  24. TRACE_BOOT_RET,
  25. TRACE_GRAPH_RET,
  26. TRACE_GRAPH_ENT,
  27. TRACE_USER_STACK,
  28. TRACE_HW_BRANCHES,
  29. TRACE_KMEM_ALLOC,
  30. TRACE_KMEM_FREE,
  31. TRACE_POWER,
  32. TRACE_BLK,
  33. __TRACE_LAST_TYPE,
  34. };
  35. /*
  36. * The trace entry - the most basic unit of tracing. This is what
  37. * is printed in the end as a single line in the trace output, such as:
  38. *
  39. * bash-15816 [01] 235.197585: idle_cpu <- irq_enter
  40. */
  41. struct trace_entry {
  42. unsigned char type;
  43. unsigned char cpu;
  44. unsigned char flags;
  45. unsigned char preempt_count;
  46. int pid;
  47. int tgid;
  48. };
  49. /*
  50. * Function trace entry - function address and parent function addres:
  51. */
  52. struct ftrace_entry {
  53. struct trace_entry ent;
  54. unsigned long ip;
  55. unsigned long parent_ip;
  56. };
  57. /* Function call entry */
  58. struct ftrace_graph_ent_entry {
  59. struct trace_entry ent;
  60. struct ftrace_graph_ent graph_ent;
  61. };
  62. /* Function return entry */
  63. struct ftrace_graph_ret_entry {
  64. struct trace_entry ent;
  65. struct ftrace_graph_ret ret;
  66. };
  67. extern struct tracer boot_tracer;
  68. /*
  69. * Context switch trace entry - which task (and prio) we switched from/to:
  70. */
  71. struct ctx_switch_entry {
  72. struct trace_entry ent;
  73. unsigned int prev_pid;
  74. unsigned char prev_prio;
  75. unsigned char prev_state;
  76. unsigned int next_pid;
  77. unsigned char next_prio;
  78. unsigned char next_state;
  79. unsigned int next_cpu;
  80. };
  81. /*
  82. * Special (free-form) trace entry:
  83. */
  84. struct special_entry {
  85. struct trace_entry ent;
  86. unsigned long arg1;
  87. unsigned long arg2;
  88. unsigned long arg3;
  89. };
  90. /*
  91. * Stack-trace entry:
  92. */
  93. #define FTRACE_STACK_ENTRIES 8
  94. struct stack_entry {
  95. struct trace_entry ent;
  96. unsigned long caller[FTRACE_STACK_ENTRIES];
  97. };
  98. struct userstack_entry {
  99. struct trace_entry ent;
  100. unsigned long caller[FTRACE_STACK_ENTRIES];
  101. };
  102. /*
  103. * ftrace_printk entry:
  104. */
  105. struct print_entry {
  106. struct trace_entry ent;
  107. unsigned long ip;
  108. int depth;
  109. char buf[];
  110. };
  111. #define TRACE_OLD_SIZE 88
  112. struct trace_field_cont {
  113. unsigned char type;
  114. /* Temporary till we get rid of this completely */
  115. char buf[TRACE_OLD_SIZE - 1];
  116. };
  117. struct trace_mmiotrace_rw {
  118. struct trace_entry ent;
  119. struct mmiotrace_rw rw;
  120. };
  121. struct trace_mmiotrace_map {
  122. struct trace_entry ent;
  123. struct mmiotrace_map map;
  124. };
  125. struct trace_boot_call {
  126. struct trace_entry ent;
  127. struct boot_trace_call boot_call;
  128. };
  129. struct trace_boot_ret {
  130. struct trace_entry ent;
  131. struct boot_trace_ret boot_ret;
  132. };
  133. #define TRACE_FUNC_SIZE 30
  134. #define TRACE_FILE_SIZE 20
  135. struct trace_branch {
  136. struct trace_entry ent;
  137. unsigned line;
  138. char func[TRACE_FUNC_SIZE+1];
  139. char file[TRACE_FILE_SIZE+1];
  140. char correct;
  141. };
  142. struct hw_branch_entry {
  143. struct trace_entry ent;
  144. u64 from;
  145. u64 to;
  146. };
  147. struct trace_power {
  148. struct trace_entry ent;
  149. struct power_trace state_data;
  150. };
  151. struct kmemtrace_alloc_entry {
  152. struct trace_entry ent;
  153. enum kmemtrace_type_id type_id;
  154. unsigned long call_site;
  155. const void *ptr;
  156. size_t bytes_req;
  157. size_t bytes_alloc;
  158. gfp_t gfp_flags;
  159. int node;
  160. };
  161. struct kmemtrace_free_entry {
  162. struct trace_entry ent;
  163. enum kmemtrace_type_id type_id;
  164. unsigned long call_site;
  165. const void *ptr;
  166. };
  167. /*
  168. * trace_flag_type is an enumeration that holds different
  169. * states when a trace occurs. These are:
  170. * IRQS_OFF - interrupts were disabled
  171. * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
  172. * NEED_RESCED - reschedule is requested
  173. * HARDIRQ - inside an interrupt handler
  174. * SOFTIRQ - inside a softirq handler
  175. */
  176. enum trace_flag_type {
  177. TRACE_FLAG_IRQS_OFF = 0x01,
  178. TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
  179. TRACE_FLAG_NEED_RESCHED = 0x04,
  180. TRACE_FLAG_HARDIRQ = 0x08,
  181. TRACE_FLAG_SOFTIRQ = 0x10,
  182. };
  183. #define TRACE_BUF_SIZE 1024
  184. /*
  185. * The CPU trace array - it consists of thousands of trace entries
  186. * plus some other descriptor data: (for example which task started
  187. * the trace, etc.)
  188. */
  189. struct trace_array_cpu {
  190. atomic_t disabled;
  191. /* these fields get copied into max-trace: */
  192. unsigned long trace_idx;
  193. unsigned long overrun;
  194. unsigned long saved_latency;
  195. unsigned long critical_start;
  196. unsigned long critical_end;
  197. unsigned long critical_sequence;
  198. unsigned long nice;
  199. unsigned long policy;
  200. unsigned long rt_priority;
  201. cycle_t preempt_timestamp;
  202. pid_t pid;
  203. uid_t uid;
  204. char comm[TASK_COMM_LEN];
  205. };
  206. struct trace_iterator;
  207. /*
  208. * The trace array - an array of per-CPU trace arrays. This is the
  209. * highest level data structure that individual tracers deal with.
  210. * They have on/off state as well:
  211. */
  212. struct trace_array {
  213. struct ring_buffer *buffer;
  214. unsigned long entries;
  215. int cpu;
  216. cycle_t time_start;
  217. struct task_struct *waiter;
  218. struct trace_array_cpu *data[NR_CPUS];
  219. };
  220. #define FTRACE_CMP_TYPE(var, type) \
  221. __builtin_types_compatible_p(typeof(var), type *)
  222. #undef IF_ASSIGN
  223. #define IF_ASSIGN(var, entry, etype, id) \
  224. if (FTRACE_CMP_TYPE(var, etype)) { \
  225. var = (typeof(var))(entry); \
  226. WARN_ON(id && (entry)->type != id); \
  227. break; \
  228. }
  229. /* Will cause compile errors if type is not found. */
  230. extern void __ftrace_bad_type(void);
  231. /*
  232. * The trace_assign_type is a verifier that the entry type is
  233. * the same as the type being assigned. To add new types simply
  234. * add a line with the following format:
  235. *
  236. * IF_ASSIGN(var, ent, type, id);
  237. *
  238. * Where "type" is the trace type that includes the trace_entry
  239. * as the "ent" item. And "id" is the trace identifier that is
  240. * used in the trace_type enum.
  241. *
  242. * If the type can have more than one id, then use zero.
  243. */
  244. #define trace_assign_type(var, ent) \
  245. do { \
  246. IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN); \
  247. IF_ASSIGN(var, ent, struct ctx_switch_entry, 0); \
  248. IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
  249. IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
  250. IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
  251. IF_ASSIGN(var, ent, struct special_entry, 0); \
  252. IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
  253. TRACE_MMIO_RW); \
  254. IF_ASSIGN(var, ent, struct trace_mmiotrace_map, \
  255. TRACE_MMIO_MAP); \
  256. IF_ASSIGN(var, ent, struct trace_boot_call, TRACE_BOOT_CALL);\
  257. IF_ASSIGN(var, ent, struct trace_boot_ret, TRACE_BOOT_RET);\
  258. IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \
  259. IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry, \
  260. TRACE_GRAPH_ENT); \
  261. IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, \
  262. TRACE_GRAPH_RET); \
  263. IF_ASSIGN(var, ent, struct hw_branch_entry, TRACE_HW_BRANCHES);\
  264. IF_ASSIGN(var, ent, struct trace_power, TRACE_POWER); \
  265. IF_ASSIGN(var, ent, struct kmemtrace_alloc_entry, \
  266. TRACE_KMEM_ALLOC); \
  267. IF_ASSIGN(var, ent, struct kmemtrace_free_entry, \
  268. TRACE_KMEM_FREE); \
  269. __ftrace_bad_type(); \
  270. } while (0)
  271. /* Return values for print_line callback */
  272. enum print_line_t {
  273. TRACE_TYPE_PARTIAL_LINE = 0, /* Retry after flushing the seq */
  274. TRACE_TYPE_HANDLED = 1,
  275. TRACE_TYPE_UNHANDLED = 2 /* Relay to other output functions */
  276. };
  277. /*
  278. * An option specific to a tracer. This is a boolean value.
  279. * The bit is the bit index that sets its value on the
  280. * flags value in struct tracer_flags.
  281. */
  282. struct tracer_opt {
  283. const char *name; /* Will appear on the trace_options file */
  284. u32 bit; /* Mask assigned in val field in tracer_flags */
  285. };
  286. /*
  287. * The set of specific options for a tracer. Your tracer
  288. * have to set the initial value of the flags val.
  289. */
  290. struct tracer_flags {
  291. u32 val;
  292. struct tracer_opt *opts;
  293. };
  294. /* Makes more easy to define a tracer opt */
  295. #define TRACER_OPT(s, b) .name = #s, .bit = b
  296. /*
  297. * A specific tracer, represented by methods that operate on a trace array:
  298. */
  299. struct tracer {
  300. const char *name;
  301. /* Your tracer should raise a warning if init fails */
  302. int (*init)(struct trace_array *tr);
  303. void (*reset)(struct trace_array *tr);
  304. void (*start)(struct trace_array *tr);
  305. void (*stop)(struct trace_array *tr);
  306. void (*open)(struct trace_iterator *iter);
  307. void (*pipe_open)(struct trace_iterator *iter);
  308. void (*close)(struct trace_iterator *iter);
  309. ssize_t (*read)(struct trace_iterator *iter,
  310. struct file *filp, char __user *ubuf,
  311. size_t cnt, loff_t *ppos);
  312. #ifdef CONFIG_FTRACE_STARTUP_TEST
  313. int (*selftest)(struct tracer *trace,
  314. struct trace_array *tr);
  315. #endif
  316. void (*print_header)(struct seq_file *m);
  317. enum print_line_t (*print_line)(struct trace_iterator *iter);
  318. /* If you handled the flag setting, return 0 */
  319. int (*set_flag)(u32 old_flags, u32 bit, int set);
  320. struct tracer *next;
  321. int print_max;
  322. struct tracer_flags *flags;
  323. struct tracer_stat *stats;
  324. };
  325. struct trace_seq {
  326. unsigned char buffer[PAGE_SIZE];
  327. unsigned int len;
  328. unsigned int readpos;
  329. };
  330. /*
  331. * Trace iterator - used by printout routines who present trace
  332. * results to users and which routines might sleep, etc:
  333. */
  334. struct trace_iterator {
  335. struct trace_array *tr;
  336. struct tracer *trace;
  337. void *private;
  338. struct ring_buffer_iter *buffer_iter[NR_CPUS];
  339. /* The below is zeroed out in pipe_read */
  340. struct trace_seq seq;
  341. struct trace_entry *ent;
  342. int cpu;
  343. u64 ts;
  344. unsigned long iter_flags;
  345. loff_t pos;
  346. long idx;
  347. cpumask_var_t started;
  348. };
  349. int tracer_init(struct tracer *t, struct trace_array *tr);
  350. int tracing_is_enabled(void);
  351. void trace_wake_up(void);
  352. void tracing_reset(struct trace_array *tr, int cpu);
  353. void tracing_reset_online_cpus(struct trace_array *tr);
  354. int tracing_open_generic(struct inode *inode, struct file *filp);
  355. struct dentry *tracing_init_dentry(void);
  356. void init_tracer_sysprof_debugfs(struct dentry *d_tracer);
  357. struct ring_buffer_event;
  358. struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
  359. unsigned char type,
  360. unsigned long len,
  361. unsigned long flags,
  362. int pc);
  363. void trace_buffer_unlock_commit(struct trace_array *tr,
  364. struct ring_buffer_event *event,
  365. unsigned long flags, int pc);
  366. struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
  367. struct trace_array_cpu *data);
  368. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  369. int *ent_cpu, u64 *ent_ts);
  370. void tracing_generic_entry_update(struct trace_entry *entry,
  371. unsigned long flags,
  372. int pc);
  373. void ftrace(struct trace_array *tr,
  374. struct trace_array_cpu *data,
  375. unsigned long ip,
  376. unsigned long parent_ip,
  377. unsigned long flags, int pc);
  378. void tracing_sched_switch_trace(struct trace_array *tr,
  379. struct task_struct *prev,
  380. struct task_struct *next,
  381. unsigned long flags, int pc);
  382. void tracing_record_cmdline(struct task_struct *tsk);
  383. void tracing_sched_wakeup_trace(struct trace_array *tr,
  384. struct task_struct *wakee,
  385. struct task_struct *cur,
  386. unsigned long flags, int pc);
  387. void trace_special(struct trace_array *tr,
  388. struct trace_array_cpu *data,
  389. unsigned long arg1,
  390. unsigned long arg2,
  391. unsigned long arg3, int pc);
  392. void trace_function(struct trace_array *tr,
  393. unsigned long ip,
  394. unsigned long parent_ip,
  395. unsigned long flags, int pc);
  396. void trace_graph_return(struct ftrace_graph_ret *trace);
  397. int trace_graph_entry(struct ftrace_graph_ent *trace);
  398. void tracing_start_cmdline_record(void);
  399. void tracing_stop_cmdline_record(void);
  400. void tracing_sched_switch_assign_trace(struct trace_array *tr);
  401. void tracing_stop_sched_switch_record(void);
  402. void tracing_start_sched_switch_record(void);
  403. int register_tracer(struct tracer *type);
  404. void unregister_tracer(struct tracer *type);
  405. extern unsigned long nsecs_to_usecs(unsigned long nsecs);
  406. extern unsigned long tracing_max_latency;
  407. extern unsigned long tracing_thresh;
  408. void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
  409. void update_max_tr_single(struct trace_array *tr,
  410. struct task_struct *tsk, int cpu);
  411. void __trace_stack(struct trace_array *tr,
  412. unsigned long flags,
  413. int skip, int pc);
  414. extern cycle_t ftrace_now(int cpu);
  415. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  416. typedef void
  417. (*tracer_switch_func_t)(void *private,
  418. void *__rq,
  419. struct task_struct *prev,
  420. struct task_struct *next);
  421. struct tracer_switch_ops {
  422. tracer_switch_func_t func;
  423. void *private;
  424. struct tracer_switch_ops *next;
  425. };
  426. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  427. extern char *trace_find_cmdline(int pid);
  428. #ifdef CONFIG_DYNAMIC_FTRACE
  429. extern unsigned long ftrace_update_tot_cnt;
  430. #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
  431. extern int DYN_FTRACE_TEST_NAME(void);
  432. #endif
  433. #ifdef CONFIG_FTRACE_STARTUP_TEST
  434. extern int trace_selftest_startup_function(struct tracer *trace,
  435. struct trace_array *tr);
  436. extern int trace_selftest_startup_irqsoff(struct tracer *trace,
  437. struct trace_array *tr);
  438. extern int trace_selftest_startup_preemptoff(struct tracer *trace,
  439. struct trace_array *tr);
  440. extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
  441. struct trace_array *tr);
  442. extern int trace_selftest_startup_wakeup(struct tracer *trace,
  443. struct trace_array *tr);
  444. extern int trace_selftest_startup_nop(struct tracer *trace,
  445. struct trace_array *tr);
  446. extern int trace_selftest_startup_sched_switch(struct tracer *trace,
  447. struct trace_array *tr);
  448. extern int trace_selftest_startup_sysprof(struct tracer *trace,
  449. struct trace_array *tr);
  450. extern int trace_selftest_startup_branch(struct tracer *trace,
  451. struct trace_array *tr);
  452. #endif /* CONFIG_FTRACE_STARTUP_TEST */
  453. extern void *head_page(struct trace_array_cpu *data);
  454. extern long ns2usecs(cycle_t nsec);
  455. extern int
  456. trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args);
  457. extern unsigned long trace_flags;
  458. /* Standard output formatting function used for function return traces */
  459. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  460. extern enum print_line_t print_graph_function(struct trace_iterator *iter);
  461. #ifdef CONFIG_DYNAMIC_FTRACE
  462. /* TODO: make this variable */
  463. #define FTRACE_GRAPH_MAX_FUNCS 32
  464. extern int ftrace_graph_count;
  465. extern unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS];
  466. static inline int ftrace_graph_addr(unsigned long addr)
  467. {
  468. int i;
  469. if (!ftrace_graph_count || test_tsk_trace_graph(current))
  470. return 1;
  471. for (i = 0; i < ftrace_graph_count; i++) {
  472. if (addr == ftrace_graph_funcs[i])
  473. return 1;
  474. }
  475. return 0;
  476. }
  477. #else
  478. static inline int ftrace_trace_addr(unsigned long addr)
  479. {
  480. return 1;
  481. }
  482. static inline int ftrace_graph_addr(unsigned long addr)
  483. {
  484. return 1;
  485. }
  486. #endif /* CONFIG_DYNAMIC_FTRACE */
  487. #else /* CONFIG_FUNCTION_GRAPH_TRACER */
  488. static inline enum print_line_t
  489. print_graph_function(struct trace_iterator *iter)
  490. {
  491. return TRACE_TYPE_UNHANDLED;
  492. }
  493. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  494. extern struct pid *ftrace_pid_trace;
  495. static inline int ftrace_trace_task(struct task_struct *task)
  496. {
  497. if (!ftrace_pid_trace)
  498. return 1;
  499. return test_tsk_trace_trace(task);
  500. }
  501. /*
  502. * trace_iterator_flags is an enumeration that defines bit
  503. * positions into trace_flags that controls the output.
  504. *
  505. * NOTE: These bits must match the trace_options array in
  506. * trace.c.
  507. */
  508. enum trace_iterator_flags {
  509. TRACE_ITER_PRINT_PARENT = 0x01,
  510. TRACE_ITER_SYM_OFFSET = 0x02,
  511. TRACE_ITER_SYM_ADDR = 0x04,
  512. TRACE_ITER_VERBOSE = 0x08,
  513. TRACE_ITER_RAW = 0x10,
  514. TRACE_ITER_HEX = 0x20,
  515. TRACE_ITER_BIN = 0x40,
  516. TRACE_ITER_BLOCK = 0x80,
  517. TRACE_ITER_STACKTRACE = 0x100,
  518. TRACE_ITER_SCHED_TREE = 0x200,
  519. TRACE_ITER_PRINTK = 0x400,
  520. TRACE_ITER_PREEMPTONLY = 0x800,
  521. TRACE_ITER_BRANCH = 0x1000,
  522. TRACE_ITER_ANNOTATE = 0x2000,
  523. TRACE_ITER_USERSTACKTRACE = 0x4000,
  524. TRACE_ITER_SYM_USEROBJ = 0x8000,
  525. TRACE_ITER_PRINTK_MSGONLY = 0x10000,
  526. TRACE_ITER_CONTEXT_INFO = 0x20000 /* Print pid/cpu/time */
  527. };
  528. /*
  529. * TRACE_ITER_SYM_MASK masks the options in trace_flags that
  530. * control the output of kernel symbols.
  531. */
  532. #define TRACE_ITER_SYM_MASK \
  533. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  534. extern struct tracer nop_trace;
  535. /**
  536. * ftrace_preempt_disable - disable preemption scheduler safe
  537. *
  538. * When tracing can happen inside the scheduler, there exists
  539. * cases that the tracing might happen before the need_resched
  540. * flag is checked. If this happens and the tracer calls
  541. * preempt_enable (after a disable), a schedule might take place
  542. * causing an infinite recursion.
  543. *
  544. * To prevent this, we read the need_recshed flag before
  545. * disabling preemption. When we want to enable preemption we
  546. * check the flag, if it is set, then we call preempt_enable_no_resched.
  547. * Otherwise, we call preempt_enable.
  548. *
  549. * The rational for doing the above is that if need resched is set
  550. * and we have yet to reschedule, we are either in an atomic location
  551. * (where we do not need to check for scheduling) or we are inside
  552. * the scheduler and do not want to resched.
  553. */
  554. static inline int ftrace_preempt_disable(void)
  555. {
  556. int resched;
  557. resched = need_resched();
  558. preempt_disable_notrace();
  559. return resched;
  560. }
  561. /**
  562. * ftrace_preempt_enable - enable preemption scheduler safe
  563. * @resched: the return value from ftrace_preempt_disable
  564. *
  565. * This is a scheduler safe way to enable preemption and not miss
  566. * any preemption checks. The disabled saved the state of preemption.
  567. * If resched is set, then we were either inside an atomic or
  568. * are inside the scheduler (we would have already scheduled
  569. * otherwise). In this case, we do not want to call normal
  570. * preempt_enable, but preempt_enable_no_resched instead.
  571. */
  572. static inline void ftrace_preempt_enable(int resched)
  573. {
  574. if (resched)
  575. preempt_enable_no_resched_notrace();
  576. else
  577. preempt_enable_notrace();
  578. }
  579. #ifdef CONFIG_BRANCH_TRACER
  580. extern int enable_branch_tracing(struct trace_array *tr);
  581. extern void disable_branch_tracing(void);
  582. static inline int trace_branch_enable(struct trace_array *tr)
  583. {
  584. if (trace_flags & TRACE_ITER_BRANCH)
  585. return enable_branch_tracing(tr);
  586. return 0;
  587. }
  588. static inline void trace_branch_disable(void)
  589. {
  590. /* due to races, always disable */
  591. disable_branch_tracing();
  592. }
  593. #else
  594. static inline int trace_branch_enable(struct trace_array *tr)
  595. {
  596. return 0;
  597. }
  598. static inline void trace_branch_disable(void)
  599. {
  600. }
  601. #endif /* CONFIG_BRANCH_TRACER */
  602. #endif /* _LINUX_KERNEL_TRACE_H */