trace.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. /*
  2. * ring buffer based function tracer
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally taken from the RT patch by:
  8. * Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code from the latency_tracer, that is:
  11. * Copyright (C) 2004-2006 Ingo Molnar
  12. * Copyright (C) 2004 William Lee Irwin III
  13. */
  14. #include <linux/utsrelease.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/linkage.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/module.h>
  24. #include <linux/percpu.h>
  25. #include <linux/ctype.h>
  26. #include <linux/init.h>
  27. #include <linux/gfp.h>
  28. #include <linux/fs.h>
  29. #include "trace.h"
  30. unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
  31. unsigned long __read_mostly tracing_thresh;
  32. static int tracing_disabled = 1;
  33. static long notrace
  34. ns2usecs(cycle_t nsec)
  35. {
  36. nsec += 500;
  37. do_div(nsec, 1000);
  38. return nsec;
  39. }
  40. static const int time_sync_freq_max = 128;
  41. static const cycle_t time_sync_thresh = 100000;
  42. static DEFINE_PER_CPU(cycle_t, time_offset);
  43. static DEFINE_PER_CPU(cycle_t, prev_cpu_time);
  44. static DEFINE_PER_CPU(int, time_sync_count);
  45. static DEFINE_PER_CPU(int, time_sync_freq);
  46. /*
  47. * Global lock which we take every now and then to synchronize
  48. * the CPUs time. This method is not warp-safe, but it's good
  49. * enough to synchronize slowly diverging time sources and thus
  50. * it's good enough for tracing:
  51. */
  52. static DEFINE_SPINLOCK(time_sync_lock);
  53. static cycle_t prev_global_time;
  54. static notrace cycle_t __ftrace_now_sync(cycles_t time, int cpu)
  55. {
  56. unsigned long flags;
  57. spin_lock_irqsave(&time_sync_lock, flags);
  58. /*
  59. * Update the synchronization frequency:
  60. */
  61. if (per_cpu(time_sync_freq, cpu) < time_sync_freq_max)
  62. per_cpu(time_sync_freq, cpu) *= 2;
  63. per_cpu(time_sync_count, cpu) = per_cpu(time_sync_freq, cpu);
  64. if (time < prev_global_time) {
  65. per_cpu(time_offset, cpu) += prev_global_time - time;
  66. time = prev_global_time;
  67. } else {
  68. prev_global_time = time;
  69. }
  70. spin_unlock_irqrestore(&time_sync_lock, flags);
  71. return time;
  72. }
  73. notrace cycle_t ftrace_now(int cpu)
  74. {
  75. cycle_t prev_cpu_time, time, delta_time;
  76. prev_cpu_time = per_cpu(prev_cpu_time, cpu);
  77. time = sched_clock() + per_cpu(time_offset, cpu);
  78. delta_time = time-prev_cpu_time;
  79. if (unlikely(delta_time > time_sync_thresh ||
  80. --per_cpu(time_sync_count, cpu) <= 0))
  81. time = __ftrace_now_sync(time, cpu);
  82. return time;
  83. }
  84. static atomic_t tracer_counter;
  85. static struct trace_array global_trace;
  86. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  87. static struct trace_array max_tr;
  88. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  89. static int tracer_enabled;
  90. static unsigned long trace_nr_entries = 16384UL;
  91. static struct tracer *trace_types __read_mostly;
  92. static struct tracer *current_trace __read_mostly;
  93. static int max_tracer_type_len;
  94. static DEFINE_MUTEX(trace_types_lock);
  95. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  96. static int __init set_nr_entries(char *str)
  97. {
  98. if (!str)
  99. return 0;
  100. trace_nr_entries = simple_strtoul(str, &str, 0);
  101. return 1;
  102. }
  103. __setup("trace_entries=", set_nr_entries);
  104. unsigned long nsecs_to_usecs(unsigned long nsecs)
  105. {
  106. return nsecs / 1000;
  107. }
  108. enum trace_type {
  109. __TRACE_FIRST_TYPE = 0,
  110. TRACE_FN,
  111. TRACE_CTX,
  112. __TRACE_LAST_TYPE
  113. };
  114. enum trace_flag_type {
  115. TRACE_FLAG_IRQS_OFF = 0x01,
  116. TRACE_FLAG_NEED_RESCHED = 0x02,
  117. TRACE_FLAG_HARDIRQ = 0x04,
  118. TRACE_FLAG_SOFTIRQ = 0x08,
  119. };
  120. enum trace_iterator_flags {
  121. TRACE_ITER_PRINT_PARENT = 0x01,
  122. TRACE_ITER_SYM_OFFSET = 0x02,
  123. TRACE_ITER_SYM_ADDR = 0x04,
  124. TRACE_ITER_VERBOSE = 0x08,
  125. };
  126. #define TRACE_ITER_SYM_MASK \
  127. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  128. /* These must match the bit postions above */
  129. static const char *trace_options[] = {
  130. "print-parent",
  131. "sym-offset",
  132. "sym-addr",
  133. "verbose",
  134. NULL
  135. };
  136. static unsigned trace_flags;
  137. static DEFINE_SPINLOCK(ftrace_max_lock);
  138. /*
  139. * Copy the new maximum trace into the separate maximum-trace
  140. * structure. (this way the maximum trace is permanently saved,
  141. * for later retrieval via /debugfs/tracing/latency_trace)
  142. */
  143. static notrace void
  144. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  145. {
  146. struct trace_array_cpu *data = tr->data[cpu];
  147. max_tr.cpu = cpu;
  148. max_tr.time_start = data->preempt_timestamp;
  149. data = max_tr.data[cpu];
  150. data->saved_latency = tracing_max_latency;
  151. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  152. data->pid = tsk->pid;
  153. data->uid = tsk->uid;
  154. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  155. data->policy = tsk->policy;
  156. data->rt_priority = tsk->rt_priority;
  157. /* record this tasks comm */
  158. tracing_record_cmdline(current);
  159. }
  160. void check_pages(struct trace_array_cpu *data)
  161. {
  162. struct page *page, *tmp;
  163. BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
  164. BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
  165. list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
  166. BUG_ON(page->lru.next->prev != &page->lru);
  167. BUG_ON(page->lru.prev->next != &page->lru);
  168. }
  169. }
  170. void *head_page(struct trace_array_cpu *data)
  171. {
  172. struct page *page;
  173. check_pages(data);
  174. if (list_empty(&data->trace_pages))
  175. return NULL;
  176. page = list_entry(data->trace_pages.next, struct page, lru);
  177. BUG_ON(&page->lru == &data->trace_pages);
  178. return page_address(page);
  179. }
  180. static notrace int
  181. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  182. {
  183. int len = (PAGE_SIZE - 1) - s->len;
  184. va_list ap;
  185. int ret;
  186. if (!len)
  187. return 0;
  188. va_start(ap, fmt);
  189. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  190. va_end(ap);
  191. /* If we can't write it all, don't bother writing anything */
  192. if (ret > len)
  193. return 0;
  194. s->len += ret;
  195. return len;
  196. }
  197. static notrace int
  198. trace_seq_puts(struct trace_seq *s, const char *str)
  199. {
  200. int len = strlen(str);
  201. if (len > ((PAGE_SIZE - 1) - s->len))
  202. return 0;
  203. memcpy(s->buffer + s->len, str, len);
  204. s->len += len;
  205. return len;
  206. }
  207. static notrace int
  208. trace_seq_putc(struct trace_seq *s, unsigned char c)
  209. {
  210. if (s->len >= (PAGE_SIZE - 1))
  211. return 0;
  212. s->buffer[s->len++] = c;
  213. return 1;
  214. }
  215. static notrace void
  216. trace_seq_reset(struct trace_seq *s)
  217. {
  218. s->len = 0;
  219. }
  220. static notrace void
  221. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  222. {
  223. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  224. s->buffer[len] = 0;
  225. seq_puts(m, s->buffer);
  226. trace_seq_reset(s);
  227. }
  228. notrace static void
  229. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  230. {
  231. struct list_head flip_pages;
  232. INIT_LIST_HEAD(&flip_pages);
  233. memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
  234. sizeof(struct trace_array_cpu) -
  235. offsetof(struct trace_array_cpu, trace_head_idx));
  236. check_pages(tr1);
  237. check_pages(tr2);
  238. list_splice_init(&tr1->trace_pages, &flip_pages);
  239. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  240. list_splice_init(&flip_pages, &tr2->trace_pages);
  241. BUG_ON(!list_empty(&flip_pages));
  242. check_pages(tr1);
  243. check_pages(tr2);
  244. }
  245. notrace void
  246. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  247. {
  248. struct trace_array_cpu *data;
  249. int i;
  250. WARN_ON_ONCE(!irqs_disabled());
  251. spin_lock(&ftrace_max_lock);
  252. /* clear out all the previous traces */
  253. for_each_possible_cpu(i) {
  254. data = tr->data[i];
  255. flip_trace(max_tr.data[i], data);
  256. tracing_reset(data);
  257. }
  258. __update_max_tr(tr, tsk, cpu);
  259. spin_unlock(&ftrace_max_lock);
  260. }
  261. /**
  262. * update_max_tr_single - only copy one trace over, and reset the rest
  263. * @tr - tracer
  264. * @tsk - task with the latency
  265. * @cpu - the cpu of the buffer to copy.
  266. */
  267. notrace void
  268. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  269. {
  270. struct trace_array_cpu *data = tr->data[cpu];
  271. int i;
  272. WARN_ON_ONCE(!irqs_disabled());
  273. spin_lock(&ftrace_max_lock);
  274. for_each_possible_cpu(i)
  275. tracing_reset(max_tr.data[i]);
  276. flip_trace(max_tr.data[cpu], data);
  277. tracing_reset(data);
  278. __update_max_tr(tr, tsk, cpu);
  279. spin_unlock(&ftrace_max_lock);
  280. }
  281. int register_tracer(struct tracer *type)
  282. {
  283. struct tracer *t;
  284. int len;
  285. int ret = 0;
  286. if (!type->name) {
  287. pr_info("Tracer must have a name\n");
  288. return -1;
  289. }
  290. mutex_lock(&trace_types_lock);
  291. for (t = trace_types; t; t = t->next) {
  292. if (strcmp(type->name, t->name) == 0) {
  293. /* already found */
  294. pr_info("Trace %s already registered\n",
  295. type->name);
  296. ret = -1;
  297. goto out;
  298. }
  299. }
  300. #ifdef CONFIG_FTRACE_STARTUP_TEST
  301. if (type->selftest) {
  302. struct tracer *saved_tracer = current_trace;
  303. struct trace_array_cpu *data;
  304. struct trace_array *tr = &global_trace;
  305. int saved_ctrl = tr->ctrl;
  306. int i;
  307. /*
  308. * Run a selftest on this tracer.
  309. * Here we reset the trace buffer, and set the current
  310. * tracer to be this tracer. The tracer can then run some
  311. * internal tracing to verify that everything is in order.
  312. * If we fail, we do not register this tracer.
  313. */
  314. for_each_possible_cpu(i) {
  315. data = tr->data[i];
  316. if (!head_page(data))
  317. continue;
  318. tracing_reset(data);
  319. }
  320. current_trace = type;
  321. tr->ctrl = 0;
  322. /* the test is responsible for initializing and enabling */
  323. pr_info("Testing tracer %s: ", type->name);
  324. ret = type->selftest(type, tr);
  325. /* the test is responsible for resetting too */
  326. current_trace = saved_tracer;
  327. tr->ctrl = saved_ctrl;
  328. if (ret) {
  329. printk(KERN_CONT "FAILED!\n");
  330. goto out;
  331. }
  332. /* Only reset on passing, to avoid touching corrupted buffers */
  333. for_each_possible_cpu(i) {
  334. data = tr->data[i];
  335. if (!head_page(data))
  336. continue;
  337. tracing_reset(data);
  338. }
  339. printk(KERN_CONT "PASSED\n");
  340. }
  341. #endif
  342. type->next = trace_types;
  343. trace_types = type;
  344. len = strlen(type->name);
  345. if (len > max_tracer_type_len)
  346. max_tracer_type_len = len;
  347. out:
  348. mutex_unlock(&trace_types_lock);
  349. return ret;
  350. }
  351. void unregister_tracer(struct tracer *type)
  352. {
  353. struct tracer **t;
  354. int len;
  355. mutex_lock(&trace_types_lock);
  356. for (t = &trace_types; *t; t = &(*t)->next) {
  357. if (*t == type)
  358. goto found;
  359. }
  360. pr_info("Trace %s not registered\n", type->name);
  361. goto out;
  362. found:
  363. *t = (*t)->next;
  364. if (strlen(type->name) != max_tracer_type_len)
  365. goto out;
  366. max_tracer_type_len = 0;
  367. for (t = &trace_types; *t; t = &(*t)->next) {
  368. len = strlen((*t)->name);
  369. if (len > max_tracer_type_len)
  370. max_tracer_type_len = len;
  371. }
  372. out:
  373. mutex_unlock(&trace_types_lock);
  374. }
  375. notrace void tracing_reset(struct trace_array_cpu *data)
  376. {
  377. data->trace_idx = 0;
  378. data->trace_head = data->trace_tail = head_page(data);
  379. data->trace_head_idx = 0;
  380. data->trace_tail_idx = 0;
  381. }
  382. #ifdef CONFIG_FTRACE
  383. static notrace void
  384. function_trace_call(unsigned long ip, unsigned long parent_ip)
  385. {
  386. struct trace_array *tr = &global_trace;
  387. struct trace_array_cpu *data;
  388. unsigned long flags;
  389. long disabled;
  390. int cpu;
  391. if (unlikely(!tracer_enabled))
  392. return;
  393. local_irq_save(flags);
  394. cpu = raw_smp_processor_id();
  395. data = tr->data[cpu];
  396. disabled = atomic_inc_return(&data->disabled);
  397. if (likely(disabled == 1))
  398. ftrace(tr, data, ip, parent_ip, flags);
  399. atomic_dec(&data->disabled);
  400. local_irq_restore(flags);
  401. }
  402. static struct ftrace_ops trace_ops __read_mostly =
  403. {
  404. .func = function_trace_call,
  405. };
  406. #endif
  407. notrace void tracing_start_function_trace(void)
  408. {
  409. register_ftrace_function(&trace_ops);
  410. }
  411. notrace void tracing_stop_function_trace(void)
  412. {
  413. unregister_ftrace_function(&trace_ops);
  414. }
  415. #define SAVED_CMDLINES 128
  416. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  417. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  418. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  419. static int cmdline_idx;
  420. static DEFINE_SPINLOCK(trace_cmdline_lock);
  421. atomic_t trace_record_cmdline_disabled;
  422. static void trace_init_cmdlines(void)
  423. {
  424. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  425. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  426. cmdline_idx = 0;
  427. }
  428. notrace void trace_stop_cmdline_recording(void);
  429. static notrace void trace_save_cmdline(struct task_struct *tsk)
  430. {
  431. unsigned map;
  432. unsigned idx;
  433. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  434. return;
  435. /*
  436. * It's not the end of the world if we don't get
  437. * the lock, but we also don't want to spin
  438. * nor do we want to disable interrupts,
  439. * so if we miss here, then better luck next time.
  440. */
  441. if (!spin_trylock(&trace_cmdline_lock))
  442. return;
  443. idx = map_pid_to_cmdline[tsk->pid];
  444. if (idx >= SAVED_CMDLINES) {
  445. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  446. map = map_cmdline_to_pid[idx];
  447. if (map <= PID_MAX_DEFAULT)
  448. map_pid_to_cmdline[map] = (unsigned)-1;
  449. map_pid_to_cmdline[tsk->pid] = idx;
  450. cmdline_idx = idx;
  451. }
  452. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  453. spin_unlock(&trace_cmdline_lock);
  454. }
  455. static notrace char *trace_find_cmdline(int pid)
  456. {
  457. char *cmdline = "<...>";
  458. unsigned map;
  459. if (!pid)
  460. return "<idle>";
  461. if (pid > PID_MAX_DEFAULT)
  462. goto out;
  463. map = map_pid_to_cmdline[pid];
  464. if (map >= SAVED_CMDLINES)
  465. goto out;
  466. cmdline = saved_cmdlines[map];
  467. out:
  468. return cmdline;
  469. }
  470. notrace void tracing_record_cmdline(struct task_struct *tsk)
  471. {
  472. if (atomic_read(&trace_record_cmdline_disabled))
  473. return;
  474. trace_save_cmdline(tsk);
  475. }
  476. static inline notrace struct list_head *
  477. trace_next_list(struct trace_array_cpu *data, struct list_head *next)
  478. {
  479. /*
  480. * Roundrobin - but skip the head (which is not a real page):
  481. */
  482. next = next->next;
  483. if (unlikely(next == &data->trace_pages))
  484. next = next->next;
  485. BUG_ON(next == &data->trace_pages);
  486. return next;
  487. }
  488. static inline notrace void *
  489. trace_next_page(struct trace_array_cpu *data, void *addr)
  490. {
  491. struct list_head *next;
  492. struct page *page;
  493. page = virt_to_page(addr);
  494. next = trace_next_list(data, &page->lru);
  495. page = list_entry(next, struct page, lru);
  496. return page_address(page);
  497. }
  498. static inline notrace struct trace_entry *
  499. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  500. {
  501. unsigned long idx, idx_next;
  502. struct trace_entry *entry;
  503. data->trace_idx++;
  504. idx = data->trace_head_idx;
  505. idx_next = idx + 1;
  506. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  507. entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
  508. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  509. data->trace_head = trace_next_page(data, data->trace_head);
  510. idx_next = 0;
  511. }
  512. if (data->trace_head == data->trace_tail &&
  513. idx_next == data->trace_tail_idx) {
  514. /* overrun */
  515. data->trace_tail_idx++;
  516. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  517. data->trace_tail =
  518. trace_next_page(data, data->trace_tail);
  519. data->trace_tail_idx = 0;
  520. }
  521. }
  522. data->trace_head_idx = idx_next;
  523. return entry;
  524. }
  525. static inline notrace void
  526. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  527. {
  528. struct task_struct *tsk = current;
  529. unsigned long pc;
  530. pc = preempt_count();
  531. entry->idx = atomic_inc_return(&tracer_counter);
  532. entry->preempt_count = pc & 0xff;
  533. entry->pid = tsk->pid;
  534. entry->t = ftrace_now(raw_smp_processor_id());
  535. entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  536. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  537. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  538. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  539. }
  540. notrace void
  541. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  542. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  543. {
  544. struct trace_entry *entry;
  545. spin_lock(&data->lock);
  546. entry = tracing_get_trace_entry(tr, data);
  547. tracing_generic_entry_update(entry, flags);
  548. entry->type = TRACE_FN;
  549. entry->fn.ip = ip;
  550. entry->fn.parent_ip = parent_ip;
  551. spin_unlock(&data->lock);
  552. }
  553. notrace void
  554. tracing_sched_switch_trace(struct trace_array *tr,
  555. struct trace_array_cpu *data,
  556. struct task_struct *prev, struct task_struct *next,
  557. unsigned long flags)
  558. {
  559. struct trace_entry *entry;
  560. spin_lock(&data->lock);
  561. entry = tracing_get_trace_entry(tr, data);
  562. tracing_generic_entry_update(entry, flags);
  563. entry->type = TRACE_CTX;
  564. entry->ctx.prev_pid = prev->pid;
  565. entry->ctx.prev_prio = prev->prio;
  566. entry->ctx.prev_state = prev->state;
  567. entry->ctx.next_pid = next->pid;
  568. entry->ctx.next_prio = next->prio;
  569. spin_unlock(&data->lock);
  570. }
  571. enum trace_file_type {
  572. TRACE_FILE_LAT_FMT = 1,
  573. };
  574. static struct trace_entry *
  575. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  576. struct trace_iterator *iter, int cpu)
  577. {
  578. struct page *page;
  579. struct trace_entry *array;
  580. if (iter->next_idx[cpu] >= tr->entries ||
  581. iter->next_idx[cpu] >= data->trace_idx ||
  582. (data->trace_head == data->trace_tail &&
  583. data->trace_head_idx == data->trace_tail_idx))
  584. return NULL;
  585. if (!iter->next_page[cpu]) {
  586. /* Initialize the iterator for this cpu trace buffer */
  587. WARN_ON(!data->trace_tail);
  588. page = virt_to_page(data->trace_tail);
  589. iter->next_page[cpu] = &page->lru;
  590. iter->next_page_idx[cpu] = data->trace_tail_idx;
  591. }
  592. page = list_entry(iter->next_page[cpu], struct page, lru);
  593. BUG_ON(&data->trace_pages == &page->lru);
  594. array = page_address(page);
  595. /* Still possible to catch up to the tail */
  596. if (iter->next_idx[cpu] && array == data->trace_tail &&
  597. iter->next_page_idx[cpu] == data->trace_tail_idx)
  598. return NULL;
  599. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  600. return &array[iter->next_page_idx[cpu]];
  601. }
  602. static struct notrace trace_entry *
  603. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  604. {
  605. struct trace_array *tr = iter->tr;
  606. struct trace_entry *ent, *next = NULL;
  607. int next_cpu = -1;
  608. int cpu;
  609. for_each_possible_cpu(cpu) {
  610. if (!head_page(tr->data[cpu]))
  611. continue;
  612. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  613. if (ent &&
  614. (!next || (long)(next->idx - ent->idx) > 0)) {
  615. next = ent;
  616. next_cpu = cpu;
  617. }
  618. }
  619. if (ent_cpu)
  620. *ent_cpu = next_cpu;
  621. return next;
  622. }
  623. static notrace void
  624. trace_iterator_increment(struct trace_iterator *iter)
  625. {
  626. iter->idx++;
  627. iter->next_idx[iter->cpu]++;
  628. iter->next_page_idx[iter->cpu]++;
  629. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  630. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  631. iter->next_page_idx[iter->cpu] = 0;
  632. iter->next_page[iter->cpu] =
  633. trace_next_list(data, iter->next_page[iter->cpu]);
  634. }
  635. }
  636. static notrace void
  637. trace_consume(struct trace_iterator *iter)
  638. {
  639. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  640. data->trace_tail_idx++;
  641. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  642. data->trace_tail = trace_next_page(data, data->trace_tail);
  643. data->trace_tail_idx = 0;
  644. }
  645. /* Check if we empty it, then reset the index */
  646. if (data->trace_head == data->trace_tail &&
  647. data->trace_head_idx == data->trace_tail_idx)
  648. data->trace_idx = 0;
  649. trace_iterator_increment(iter);
  650. }
  651. static notrace void *
  652. find_next_entry_inc(struct trace_iterator *iter)
  653. {
  654. struct trace_entry *next;
  655. int next_cpu = -1;
  656. next = find_next_entry(iter, &next_cpu);
  657. iter->prev_ent = iter->ent;
  658. iter->prev_cpu = iter->cpu;
  659. iter->ent = next;
  660. iter->cpu = next_cpu;
  661. if (next)
  662. trace_iterator_increment(iter);
  663. return next ? iter : NULL;
  664. }
  665. static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
  666. {
  667. struct trace_iterator *iter = m->private;
  668. void *last_ent = iter->ent;
  669. int i = (int)*pos;
  670. void *ent;
  671. (*pos)++;
  672. /* can't go backwards */
  673. if (iter->idx > i)
  674. return NULL;
  675. if (iter->idx < 0)
  676. ent = find_next_entry_inc(iter);
  677. else
  678. ent = iter;
  679. while (ent && iter->idx < i)
  680. ent = find_next_entry_inc(iter);
  681. iter->pos = *pos;
  682. if (last_ent && !ent)
  683. seq_puts(m, "\n\nvim:ft=help\n");
  684. return ent;
  685. }
  686. static void *s_start(struct seq_file *m, loff_t *pos)
  687. {
  688. struct trace_iterator *iter = m->private;
  689. void *p = NULL;
  690. loff_t l = 0;
  691. int i;
  692. mutex_lock(&trace_types_lock);
  693. if (!current_trace || current_trace != iter->trace)
  694. return NULL;
  695. atomic_inc(&trace_record_cmdline_disabled);
  696. /* let the tracer grab locks here if needed */
  697. if (current_trace->start)
  698. current_trace->start(iter);
  699. if (*pos != iter->pos) {
  700. iter->ent = NULL;
  701. iter->cpu = 0;
  702. iter->idx = -1;
  703. iter->prev_ent = NULL;
  704. iter->prev_cpu = -1;
  705. for_each_possible_cpu(i) {
  706. iter->next_idx[i] = 0;
  707. iter->next_page[i] = NULL;
  708. }
  709. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  710. ;
  711. } else {
  712. l = *pos - 1;
  713. p = s_next(m, p, &l);
  714. }
  715. return p;
  716. }
  717. static void s_stop(struct seq_file *m, void *p)
  718. {
  719. struct trace_iterator *iter = m->private;
  720. atomic_dec(&trace_record_cmdline_disabled);
  721. /* let the tracer release locks here if needed */
  722. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  723. iter->trace->stop(iter);
  724. mutex_unlock(&trace_types_lock);
  725. }
  726. static int
  727. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  728. {
  729. #ifdef CONFIG_KALLSYMS
  730. char str[KSYM_SYMBOL_LEN];
  731. kallsyms_lookup(address, NULL, NULL, NULL, str);
  732. return trace_seq_printf(s, fmt, str);
  733. #endif
  734. return 1;
  735. }
  736. static int
  737. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  738. unsigned long address)
  739. {
  740. #ifdef CONFIG_KALLSYMS
  741. char str[KSYM_SYMBOL_LEN];
  742. sprint_symbol(str, address);
  743. return trace_seq_printf(s, fmt, str);
  744. #endif
  745. return 1;
  746. }
  747. #ifndef CONFIG_64BIT
  748. # define IP_FMT "%08lx"
  749. #else
  750. # define IP_FMT "%016lx"
  751. #endif
  752. static notrace int
  753. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  754. {
  755. int ret;
  756. if (!ip)
  757. return trace_seq_printf(s, "0");
  758. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  759. ret = seq_print_sym_offset(s, "%s", ip);
  760. else
  761. ret = seq_print_sym_short(s, "%s", ip);
  762. if (!ret)
  763. return 0;
  764. if (sym_flags & TRACE_ITER_SYM_ADDR)
  765. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  766. return ret;
  767. }
  768. static notrace void print_lat_help_header(struct seq_file *m)
  769. {
  770. seq_puts(m, "# _------=> CPU# \n");
  771. seq_puts(m, "# / _-----=> irqs-off \n");
  772. seq_puts(m, "# | / _----=> need-resched \n");
  773. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  774. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  775. seq_puts(m, "# |||| / \n");
  776. seq_puts(m, "# ||||| delay \n");
  777. seq_puts(m, "# cmd pid ||||| time | caller \n");
  778. seq_puts(m, "# \\ / ||||| \\ | / \n");
  779. }
  780. static notrace void print_func_help_header(struct seq_file *m)
  781. {
  782. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  783. seq_puts(m, "# | | | | |\n");
  784. }
  785. static notrace void
  786. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  787. {
  788. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  789. struct trace_array *tr = iter->tr;
  790. struct trace_array_cpu *data = tr->data[tr->cpu];
  791. struct tracer *type = current_trace;
  792. unsigned long total = 0;
  793. unsigned long entries = 0;
  794. int cpu;
  795. const char *name = "preemption";
  796. if (type)
  797. name = type->name;
  798. for_each_possible_cpu(cpu) {
  799. if (head_page(tr->data[cpu])) {
  800. total += tr->data[cpu]->trace_idx;
  801. if (tr->data[cpu]->trace_idx > tr->entries)
  802. entries += tr->entries;
  803. else
  804. entries += tr->data[cpu]->trace_idx;
  805. }
  806. }
  807. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  808. name, UTS_RELEASE);
  809. seq_puts(m, "-----------------------------------"
  810. "---------------------------------\n");
  811. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  812. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  813. nsecs_to_usecs(data->saved_latency),
  814. entries,
  815. total,
  816. tr->cpu,
  817. #if defined(CONFIG_PREEMPT_NONE)
  818. "server",
  819. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  820. "desktop",
  821. #elif defined(CONFIG_PREEMPT_DESKTOP)
  822. "preempt",
  823. #else
  824. "unknown",
  825. #endif
  826. /* These are reserved for later use */
  827. 0, 0, 0, 0);
  828. #ifdef CONFIG_SMP
  829. seq_printf(m, " #P:%d)\n", num_online_cpus());
  830. #else
  831. seq_puts(m, ")\n");
  832. #endif
  833. seq_puts(m, " -----------------\n");
  834. seq_printf(m, " | task: %.16s-%d "
  835. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  836. data->comm, data->pid, data->uid, data->nice,
  837. data->policy, data->rt_priority);
  838. seq_puts(m, " -----------------\n");
  839. if (data->critical_start) {
  840. seq_puts(m, " => started at: ");
  841. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  842. trace_print_seq(m, &iter->seq);
  843. seq_puts(m, "\n => ended at: ");
  844. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  845. trace_print_seq(m, &iter->seq);
  846. seq_puts(m, "\n");
  847. }
  848. seq_puts(m, "\n");
  849. }
  850. static notrace void
  851. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  852. {
  853. int hardirq, softirq;
  854. char *comm;
  855. comm = trace_find_cmdline(entry->pid);
  856. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  857. trace_seq_printf(s, "%d", cpu);
  858. trace_seq_printf(s, "%c%c",
  859. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  860. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  861. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  862. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  863. if (hardirq && softirq)
  864. trace_seq_putc(s, 'H');
  865. else {
  866. if (hardirq)
  867. trace_seq_putc(s, 'h');
  868. else {
  869. if (softirq)
  870. trace_seq_putc(s, 's');
  871. else
  872. trace_seq_putc(s, '.');
  873. }
  874. }
  875. if (entry->preempt_count)
  876. trace_seq_printf(s, "%x", entry->preempt_count);
  877. else
  878. trace_seq_puts(s, ".");
  879. }
  880. unsigned long preempt_mark_thresh = 100;
  881. static notrace void
  882. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  883. unsigned long rel_usecs)
  884. {
  885. trace_seq_printf(s, " %4lldus", abs_usecs);
  886. if (rel_usecs > preempt_mark_thresh)
  887. trace_seq_puts(s, "!: ");
  888. else if (rel_usecs > 1)
  889. trace_seq_puts(s, "+: ");
  890. else
  891. trace_seq_puts(s, " : ");
  892. }
  893. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  894. static notrace void
  895. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  896. {
  897. struct trace_seq *s = &iter->seq;
  898. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  899. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  900. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  901. struct trace_entry *entry = iter->ent;
  902. unsigned long abs_usecs;
  903. unsigned long rel_usecs;
  904. char *comm;
  905. int S;
  906. if (!next_entry)
  907. next_entry = entry;
  908. rel_usecs = ns2usecs(next_entry->t - entry->t);
  909. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  910. if (verbose) {
  911. comm = trace_find_cmdline(entry->pid);
  912. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  913. " %ld.%03ldms (+%ld.%03ldms): ",
  914. comm,
  915. entry->pid, cpu, entry->flags,
  916. entry->preempt_count, trace_idx,
  917. ns2usecs(entry->t),
  918. abs_usecs/1000,
  919. abs_usecs % 1000, rel_usecs/1000,
  920. rel_usecs % 1000);
  921. } else {
  922. lat_print_generic(s, entry, cpu);
  923. lat_print_timestamp(s, abs_usecs, rel_usecs);
  924. }
  925. switch (entry->type) {
  926. case TRACE_FN:
  927. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  928. trace_seq_puts(s, " (");
  929. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  930. trace_seq_puts(s, ")\n");
  931. break;
  932. case TRACE_CTX:
  933. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  934. state_to_char[entry->ctx.prev_state] : 'X';
  935. comm = trace_find_cmdline(entry->ctx.next_pid);
  936. trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
  937. entry->ctx.prev_pid,
  938. entry->ctx.prev_prio,
  939. S,
  940. entry->ctx.next_pid,
  941. entry->ctx.next_prio,
  942. comm);
  943. break;
  944. default:
  945. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  946. }
  947. }
  948. static notrace void sync_time_offset(struct trace_iterator *iter)
  949. {
  950. struct trace_array_cpu *prev_array, *array;
  951. struct trace_entry *prev_entry, *entry;
  952. cycle_t prev_t, t;
  953. entry = iter->ent;
  954. prev_entry = iter->prev_ent;
  955. if (!prev_entry)
  956. return;
  957. prev_array = iter->tr->data[iter->prev_cpu];
  958. array = iter->tr->data[iter->cpu];
  959. prev_t = prev_entry->t + prev_array->time_offset;
  960. t = entry->t + array->time_offset;
  961. /*
  962. * If time goes backwards we increase the offset of
  963. * the current array, to not have observable time warps.
  964. * This will quickly synchronize the time offsets of
  965. * multiple CPUs:
  966. */
  967. if (t < prev_t)
  968. array->time_offset += prev_t - t;
  969. }
  970. static notrace int
  971. print_trace_fmt(struct trace_iterator *iter)
  972. {
  973. struct trace_seq *s = &iter->seq;
  974. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  975. struct trace_entry *entry;
  976. unsigned long usec_rem;
  977. unsigned long long t;
  978. unsigned long secs;
  979. char *comm;
  980. int S;
  981. int ret;
  982. sync_time_offset(iter);
  983. entry = iter->ent;
  984. comm = trace_find_cmdline(iter->ent->pid);
  985. t = ns2usecs(entry->t + iter->tr->data[iter->cpu]->time_offset);
  986. usec_rem = do_div(t, 1000000ULL);
  987. secs = (unsigned long)t;
  988. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  989. if (!ret)
  990. return 0;
  991. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  992. if (!ret)
  993. return 0;
  994. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  995. if (!ret)
  996. return 0;
  997. switch (entry->type) {
  998. case TRACE_FN:
  999. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1000. if (!ret)
  1001. return 0;
  1002. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1003. entry->fn.parent_ip) {
  1004. ret = trace_seq_printf(s, " <-");
  1005. if (!ret)
  1006. return 0;
  1007. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  1008. sym_flags);
  1009. if (!ret)
  1010. return 0;
  1011. }
  1012. ret = trace_seq_printf(s, "\n");
  1013. if (!ret)
  1014. return 0;
  1015. break;
  1016. case TRACE_CTX:
  1017. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1018. state_to_char[entry->ctx.prev_state] : 'X';
  1019. ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
  1020. entry->ctx.prev_pid,
  1021. entry->ctx.prev_prio,
  1022. S,
  1023. entry->ctx.next_pid,
  1024. entry->ctx.next_prio);
  1025. if (!ret)
  1026. return 0;
  1027. break;
  1028. }
  1029. return 1;
  1030. }
  1031. static int trace_empty(struct trace_iterator *iter)
  1032. {
  1033. struct trace_array_cpu *data;
  1034. int cpu;
  1035. for_each_possible_cpu(cpu) {
  1036. data = iter->tr->data[cpu];
  1037. if (head_page(data) && data->trace_idx &&
  1038. (data->trace_tail != data->trace_head ||
  1039. data->trace_tail_idx != data->trace_head_idx))
  1040. return 0;
  1041. }
  1042. return 1;
  1043. }
  1044. static int s_show(struct seq_file *m, void *v)
  1045. {
  1046. struct trace_iterator *iter = v;
  1047. if (iter->ent == NULL) {
  1048. if (iter->tr) {
  1049. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1050. seq_puts(m, "#\n");
  1051. }
  1052. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1053. /* print nothing if the buffers are empty */
  1054. if (trace_empty(iter))
  1055. return 0;
  1056. print_trace_header(m, iter);
  1057. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1058. print_lat_help_header(m);
  1059. } else {
  1060. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1061. print_func_help_header(m);
  1062. }
  1063. } else {
  1064. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1065. print_lat_fmt(iter, iter->idx, iter->cpu);
  1066. else
  1067. print_trace_fmt(iter);
  1068. trace_print_seq(m, &iter->seq);
  1069. }
  1070. return 0;
  1071. }
  1072. static struct seq_operations tracer_seq_ops = {
  1073. .start = s_start,
  1074. .next = s_next,
  1075. .stop = s_stop,
  1076. .show = s_show,
  1077. };
  1078. static struct trace_iterator notrace *
  1079. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1080. {
  1081. struct trace_iterator *iter;
  1082. if (tracing_disabled) {
  1083. *ret = -ENODEV;
  1084. return NULL;
  1085. }
  1086. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1087. if (!iter) {
  1088. *ret = -ENOMEM;
  1089. goto out;
  1090. }
  1091. mutex_lock(&trace_types_lock);
  1092. if (current_trace && current_trace->print_max)
  1093. iter->tr = &max_tr;
  1094. else
  1095. iter->tr = inode->i_private;
  1096. iter->trace = current_trace;
  1097. iter->pos = -1;
  1098. /* TODO stop tracer */
  1099. *ret = seq_open(file, &tracer_seq_ops);
  1100. if (!*ret) {
  1101. struct seq_file *m = file->private_data;
  1102. m->private = iter;
  1103. /* stop the trace while dumping */
  1104. if (iter->tr->ctrl)
  1105. tracer_enabled = 0;
  1106. if (iter->trace && iter->trace->open)
  1107. iter->trace->open(iter);
  1108. } else {
  1109. kfree(iter);
  1110. iter = NULL;
  1111. }
  1112. mutex_unlock(&trace_types_lock);
  1113. out:
  1114. return iter;
  1115. }
  1116. int tracing_open_generic(struct inode *inode, struct file *filp)
  1117. {
  1118. if (tracing_disabled)
  1119. return -ENODEV;
  1120. filp->private_data = inode->i_private;
  1121. return 0;
  1122. }
  1123. int tracing_release(struct inode *inode, struct file *file)
  1124. {
  1125. struct seq_file *m = (struct seq_file *)file->private_data;
  1126. struct trace_iterator *iter = m->private;
  1127. mutex_lock(&trace_types_lock);
  1128. if (iter->trace && iter->trace->close)
  1129. iter->trace->close(iter);
  1130. /* reenable tracing if it was previously enabled */
  1131. if (iter->tr->ctrl)
  1132. tracer_enabled = 1;
  1133. mutex_unlock(&trace_types_lock);
  1134. seq_release(inode, file);
  1135. kfree(iter);
  1136. return 0;
  1137. }
  1138. static int tracing_open(struct inode *inode, struct file *file)
  1139. {
  1140. int ret;
  1141. __tracing_open(inode, file, &ret);
  1142. return ret;
  1143. }
  1144. static int tracing_lt_open(struct inode *inode, struct file *file)
  1145. {
  1146. struct trace_iterator *iter;
  1147. int ret;
  1148. iter = __tracing_open(inode, file, &ret);
  1149. if (!ret)
  1150. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1151. return ret;
  1152. }
  1153. static notrace void *
  1154. t_next(struct seq_file *m, void *v, loff_t *pos)
  1155. {
  1156. struct tracer *t = m->private;
  1157. (*pos)++;
  1158. if (t)
  1159. t = t->next;
  1160. m->private = t;
  1161. return t;
  1162. }
  1163. static void *t_start(struct seq_file *m, loff_t *pos)
  1164. {
  1165. struct tracer *t = m->private;
  1166. loff_t l = 0;
  1167. mutex_lock(&trace_types_lock);
  1168. for (; t && l < *pos; t = t_next(m, t, &l))
  1169. ;
  1170. return t;
  1171. }
  1172. static void t_stop(struct seq_file *m, void *p)
  1173. {
  1174. mutex_unlock(&trace_types_lock);
  1175. }
  1176. static int t_show(struct seq_file *m, void *v)
  1177. {
  1178. struct tracer *t = v;
  1179. if (!t)
  1180. return 0;
  1181. seq_printf(m, "%s", t->name);
  1182. if (t->next)
  1183. seq_putc(m, ' ');
  1184. else
  1185. seq_putc(m, '\n');
  1186. return 0;
  1187. }
  1188. static struct seq_operations show_traces_seq_ops = {
  1189. .start = t_start,
  1190. .next = t_next,
  1191. .stop = t_stop,
  1192. .show = t_show,
  1193. };
  1194. static int show_traces_open(struct inode *inode, struct file *file)
  1195. {
  1196. int ret;
  1197. if (tracing_disabled)
  1198. return -ENODEV;
  1199. ret = seq_open(file, &show_traces_seq_ops);
  1200. if (!ret) {
  1201. struct seq_file *m = file->private_data;
  1202. m->private = trace_types;
  1203. }
  1204. return ret;
  1205. }
  1206. static struct file_operations tracing_fops = {
  1207. .open = tracing_open,
  1208. .read = seq_read,
  1209. .llseek = seq_lseek,
  1210. .release = tracing_release,
  1211. };
  1212. static struct file_operations tracing_lt_fops = {
  1213. .open = tracing_lt_open,
  1214. .read = seq_read,
  1215. .llseek = seq_lseek,
  1216. .release = tracing_release,
  1217. };
  1218. static struct file_operations show_traces_fops = {
  1219. .open = show_traces_open,
  1220. .read = seq_read,
  1221. .release = seq_release,
  1222. };
  1223. static ssize_t
  1224. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1225. size_t cnt, loff_t *ppos)
  1226. {
  1227. char *buf;
  1228. int r = 0;
  1229. int len = 0;
  1230. int i;
  1231. /* calulate max size */
  1232. for (i = 0; trace_options[i]; i++) {
  1233. len += strlen(trace_options[i]);
  1234. len += 3; /* "no" and space */
  1235. }
  1236. /* +2 for \n and \0 */
  1237. buf = kmalloc(len + 2, GFP_KERNEL);
  1238. if (!buf)
  1239. return -ENOMEM;
  1240. for (i = 0; trace_options[i]; i++) {
  1241. if (trace_flags & (1 << i))
  1242. r += sprintf(buf + r, "%s ", trace_options[i]);
  1243. else
  1244. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1245. }
  1246. r += sprintf(buf + r, "\n");
  1247. WARN_ON(r >= len + 2);
  1248. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1249. buf, r);
  1250. kfree(buf);
  1251. return r;
  1252. }
  1253. static ssize_t
  1254. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1255. size_t cnt, loff_t *ppos)
  1256. {
  1257. char buf[64];
  1258. char *cmp = buf;
  1259. int neg = 0;
  1260. int i;
  1261. if (cnt > 63)
  1262. cnt = 63;
  1263. if (copy_from_user(&buf, ubuf, cnt))
  1264. return -EFAULT;
  1265. buf[cnt] = 0;
  1266. if (strncmp(buf, "no", 2) == 0) {
  1267. neg = 1;
  1268. cmp += 2;
  1269. }
  1270. for (i = 0; trace_options[i]; i++) {
  1271. int len = strlen(trace_options[i]);
  1272. if (strncmp(cmp, trace_options[i], len) == 0) {
  1273. if (neg)
  1274. trace_flags &= ~(1 << i);
  1275. else
  1276. trace_flags |= (1 << i);
  1277. break;
  1278. }
  1279. }
  1280. filp->f_pos += cnt;
  1281. return cnt;
  1282. }
  1283. static struct file_operations tracing_iter_fops = {
  1284. .open = tracing_open_generic,
  1285. .read = tracing_iter_ctrl_read,
  1286. .write = tracing_iter_ctrl_write,
  1287. };
  1288. static const char readme_msg[] =
  1289. "tracing mini-HOWTO:\n\n"
  1290. "# mkdir /debug\n"
  1291. "# mount -t debugfs nodev /debug\n\n"
  1292. "# cat /debug/tracing/available_tracers\n"
  1293. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1294. "# cat /debug/tracing/current_tracer\n"
  1295. "none\n"
  1296. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1297. "# cat /debug/tracing/current_tracer\n"
  1298. "sched_switch\n"
  1299. "# cat /debug/tracing/iter_ctrl\n"
  1300. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1301. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1302. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1303. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1304. "echo 0 > /debug/tracing/tracing_enabled\n"
  1305. ;
  1306. static ssize_t
  1307. tracing_readme_read(struct file *filp, char __user *ubuf,
  1308. size_t cnt, loff_t *ppos)
  1309. {
  1310. return simple_read_from_buffer(ubuf, cnt, ppos,
  1311. readme_msg, strlen(readme_msg));
  1312. }
  1313. static struct file_operations tracing_readme_fops = {
  1314. .open = tracing_open_generic,
  1315. .read = tracing_readme_read,
  1316. };
  1317. static ssize_t
  1318. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1319. size_t cnt, loff_t *ppos)
  1320. {
  1321. struct trace_array *tr = filp->private_data;
  1322. char buf[64];
  1323. int r;
  1324. r = sprintf(buf, "%ld\n", tr->ctrl);
  1325. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1326. }
  1327. static ssize_t
  1328. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1329. size_t cnt, loff_t *ppos)
  1330. {
  1331. struct trace_array *tr = filp->private_data;
  1332. long val;
  1333. char buf[64];
  1334. if (cnt > 63)
  1335. cnt = 63;
  1336. if (copy_from_user(&buf, ubuf, cnt))
  1337. return -EFAULT;
  1338. buf[cnt] = 0;
  1339. val = simple_strtoul(buf, NULL, 10);
  1340. val = !!val;
  1341. mutex_lock(&trace_types_lock);
  1342. if (tr->ctrl ^ val) {
  1343. if (val)
  1344. tracer_enabled = 1;
  1345. else
  1346. tracer_enabled = 0;
  1347. tr->ctrl = val;
  1348. if (current_trace && current_trace->ctrl_update)
  1349. current_trace->ctrl_update(tr);
  1350. }
  1351. mutex_unlock(&trace_types_lock);
  1352. filp->f_pos += cnt;
  1353. return cnt;
  1354. }
  1355. static ssize_t
  1356. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1357. size_t cnt, loff_t *ppos)
  1358. {
  1359. char buf[max_tracer_type_len+2];
  1360. int r;
  1361. mutex_lock(&trace_types_lock);
  1362. if (current_trace)
  1363. r = sprintf(buf, "%s\n", current_trace->name);
  1364. else
  1365. r = sprintf(buf, "\n");
  1366. mutex_unlock(&trace_types_lock);
  1367. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1368. }
  1369. static ssize_t
  1370. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1371. size_t cnt, loff_t *ppos)
  1372. {
  1373. struct trace_array *tr = &global_trace;
  1374. struct tracer *t;
  1375. char buf[max_tracer_type_len+1];
  1376. int i;
  1377. if (cnt > max_tracer_type_len)
  1378. cnt = max_tracer_type_len;
  1379. if (copy_from_user(&buf, ubuf, cnt))
  1380. return -EFAULT;
  1381. buf[cnt] = 0;
  1382. /* strip ending whitespace. */
  1383. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1384. buf[i] = 0;
  1385. mutex_lock(&trace_types_lock);
  1386. for (t = trace_types; t; t = t->next) {
  1387. if (strcmp(t->name, buf) == 0)
  1388. break;
  1389. }
  1390. if (!t || t == current_trace)
  1391. goto out;
  1392. if (current_trace && current_trace->reset)
  1393. current_trace->reset(tr);
  1394. current_trace = t;
  1395. if (t->init)
  1396. t->init(tr);
  1397. out:
  1398. mutex_unlock(&trace_types_lock);
  1399. filp->f_pos += cnt;
  1400. return cnt;
  1401. }
  1402. static ssize_t
  1403. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1404. size_t cnt, loff_t *ppos)
  1405. {
  1406. unsigned long *ptr = filp->private_data;
  1407. char buf[64];
  1408. int r;
  1409. r = snprintf(buf, 64, "%ld\n",
  1410. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1411. if (r > 64)
  1412. r = 64;
  1413. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1414. }
  1415. static ssize_t
  1416. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1417. size_t cnt, loff_t *ppos)
  1418. {
  1419. long *ptr = filp->private_data;
  1420. long val;
  1421. char buf[64];
  1422. if (cnt > 63)
  1423. cnt = 63;
  1424. if (copy_from_user(&buf, ubuf, cnt))
  1425. return -EFAULT;
  1426. buf[cnt] = 0;
  1427. val = simple_strtoul(buf, NULL, 10);
  1428. *ptr = val * 1000;
  1429. return cnt;
  1430. }
  1431. static atomic_t tracing_reader;
  1432. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1433. {
  1434. struct trace_iterator *iter;
  1435. if (tracing_disabled)
  1436. return -ENODEV;
  1437. /* We only allow for reader of the pipe */
  1438. if (atomic_inc_return(&tracing_reader) != 1) {
  1439. atomic_dec(&tracing_reader);
  1440. return -EBUSY;
  1441. }
  1442. /* create a buffer to store the information to pass to userspace */
  1443. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1444. if (!iter)
  1445. return -ENOMEM;
  1446. iter->tr = &global_trace;
  1447. filp->private_data = iter;
  1448. return 0;
  1449. }
  1450. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1451. {
  1452. struct trace_iterator *iter = file->private_data;
  1453. kfree(iter);
  1454. atomic_dec(&tracing_reader);
  1455. return 0;
  1456. }
  1457. /*
  1458. * Consumer reader.
  1459. */
  1460. static ssize_t
  1461. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1462. size_t cnt, loff_t *ppos)
  1463. {
  1464. struct trace_iterator *iter = filp->private_data;
  1465. struct trace_array_cpu *data;
  1466. static cpumask_t mask;
  1467. struct trace_entry *entry;
  1468. static int start;
  1469. unsigned long flags;
  1470. int read = 0;
  1471. int cpu;
  1472. int len;
  1473. int ret;
  1474. /* return any leftover data */
  1475. if (iter->seq.len > start) {
  1476. len = iter->seq.len - start;
  1477. if (cnt > len)
  1478. cnt = len;
  1479. ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
  1480. if (ret)
  1481. cnt = -EFAULT;
  1482. start += len;
  1483. return cnt;
  1484. }
  1485. trace_seq_reset(&iter->seq);
  1486. start = 0;
  1487. while (trace_empty(iter)) {
  1488. /*
  1489. * This is a make-shift waitqueue. The reason we don't use
  1490. * an actual wait queue is because:
  1491. * 1) we only ever have one waiter
  1492. * 2) the tracing, traces all functions, we don't want
  1493. * the overhead of calling wake_up and friends
  1494. * (and tracing them too)
  1495. * Anyway, this is really very primitive wakeup.
  1496. */
  1497. set_current_state(TASK_INTERRUPTIBLE);
  1498. iter->tr->waiter = current;
  1499. /* sleep for one second, and try again. */
  1500. schedule_timeout(HZ);
  1501. iter->tr->waiter = NULL;
  1502. if (signal_pending(current))
  1503. return -EINTR;
  1504. /*
  1505. * We block until we read something and tracing is disabled.
  1506. * We still block if tracing is disabled, but we have never
  1507. * read anything. This allows a user to cat this file, and
  1508. * then enable tracing. But after we have read something,
  1509. * we give an EOF when tracing is again disabled.
  1510. *
  1511. * iter->pos will be 0 if we haven't read anything.
  1512. */
  1513. if (!tracer_enabled && iter->pos)
  1514. break;
  1515. continue;
  1516. }
  1517. /* stop when tracing is finished */
  1518. if (trace_empty(iter))
  1519. return 0;
  1520. if (cnt >= PAGE_SIZE)
  1521. cnt = PAGE_SIZE - 1;
  1522. memset(iter, 0, sizeof(*iter));
  1523. iter->tr = &global_trace;
  1524. iter->pos = -1;
  1525. /*
  1526. * We need to stop all tracing on all CPUS to read the
  1527. * the next buffer. This is a bit expensive, but is
  1528. * not done often. We fill all what we can read,
  1529. * and then release the locks again.
  1530. */
  1531. cpus_clear(mask);
  1532. local_irq_save(flags);
  1533. for_each_possible_cpu(cpu) {
  1534. data = iter->tr->data[cpu];
  1535. if (!head_page(data) || !data->trace_idx)
  1536. continue;
  1537. atomic_inc(&data->disabled);
  1538. spin_lock(&data->lock);
  1539. cpu_set(cpu, mask);
  1540. }
  1541. while ((entry = find_next_entry(iter, &cpu))) {
  1542. if (!entry)
  1543. break;
  1544. iter->ent = entry;
  1545. iter->cpu = cpu;
  1546. ret = print_trace_fmt(iter);
  1547. if (!ret)
  1548. break;
  1549. trace_consume(iter);
  1550. if (iter->seq.len >= cnt)
  1551. break;
  1552. }
  1553. for_each_cpu_mask(cpu, mask) {
  1554. data = iter->tr->data[cpu];
  1555. spin_unlock(&data->lock);
  1556. atomic_dec(&data->disabled);
  1557. }
  1558. local_irq_restore(flags);
  1559. /* Now copy what we have to the user */
  1560. read = iter->seq.len;
  1561. if (read > cnt)
  1562. read = cnt;
  1563. ret = copy_to_user(ubuf, iter->seq.buffer, read);
  1564. if (read < iter->seq.len)
  1565. start = read;
  1566. else
  1567. trace_seq_reset(&iter->seq);
  1568. if (ret)
  1569. read = -EFAULT;
  1570. return read;
  1571. }
  1572. static struct file_operations tracing_max_lat_fops = {
  1573. .open = tracing_open_generic,
  1574. .read = tracing_max_lat_read,
  1575. .write = tracing_max_lat_write,
  1576. };
  1577. static struct file_operations tracing_ctrl_fops = {
  1578. .open = tracing_open_generic,
  1579. .read = tracing_ctrl_read,
  1580. .write = tracing_ctrl_write,
  1581. };
  1582. static struct file_operations set_tracer_fops = {
  1583. .open = tracing_open_generic,
  1584. .read = tracing_set_trace_read,
  1585. .write = tracing_set_trace_write,
  1586. };
  1587. static struct file_operations tracing_pipe_fops = {
  1588. .open = tracing_open_pipe,
  1589. .read = tracing_read_pipe,
  1590. .release = tracing_release_pipe,
  1591. };
  1592. #ifdef CONFIG_DYNAMIC_FTRACE
  1593. static ssize_t
  1594. tracing_read_long(struct file *filp, char __user *ubuf,
  1595. size_t cnt, loff_t *ppos)
  1596. {
  1597. unsigned long *p = filp->private_data;
  1598. char buf[64];
  1599. int r;
  1600. r = sprintf(buf, "%ld\n", *p);
  1601. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1602. }
  1603. static struct file_operations tracing_read_long_fops = {
  1604. .open = tracing_open_generic,
  1605. .read = tracing_read_long,
  1606. };
  1607. #endif
  1608. static struct dentry *d_tracer;
  1609. struct dentry *tracing_init_dentry(void)
  1610. {
  1611. static int once;
  1612. if (d_tracer)
  1613. return d_tracer;
  1614. d_tracer = debugfs_create_dir("tracing", NULL);
  1615. if (!d_tracer && !once) {
  1616. once = 1;
  1617. pr_warning("Could not create debugfs directory 'tracing'\n");
  1618. return NULL;
  1619. }
  1620. return d_tracer;
  1621. }
  1622. #ifdef CONFIG_FTRACE_SELFTEST
  1623. /* Let selftest have access to static functions in this file */
  1624. #include "trace_selftest.c"
  1625. #endif
  1626. static __init void tracer_init_debugfs(void)
  1627. {
  1628. struct dentry *d_tracer;
  1629. struct dentry *entry;
  1630. d_tracer = tracing_init_dentry();
  1631. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  1632. &global_trace, &tracing_ctrl_fops);
  1633. if (!entry)
  1634. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  1635. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  1636. NULL, &tracing_iter_fops);
  1637. if (!entry)
  1638. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  1639. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  1640. &global_trace, &tracing_lt_fops);
  1641. if (!entry)
  1642. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  1643. entry = debugfs_create_file("trace", 0444, d_tracer,
  1644. &global_trace, &tracing_fops);
  1645. if (!entry)
  1646. pr_warning("Could not create debugfs 'trace' entry\n");
  1647. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  1648. &global_trace, &show_traces_fops);
  1649. if (!entry)
  1650. pr_warning("Could not create debugfs 'trace' entry\n");
  1651. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  1652. &global_trace, &set_tracer_fops);
  1653. if (!entry)
  1654. pr_warning("Could not create debugfs 'trace' entry\n");
  1655. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  1656. &tracing_max_latency,
  1657. &tracing_max_lat_fops);
  1658. if (!entry)
  1659. pr_warning("Could not create debugfs "
  1660. "'tracing_max_latency' entry\n");
  1661. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  1662. &tracing_thresh, &tracing_max_lat_fops);
  1663. if (!entry)
  1664. pr_warning("Could not create debugfs "
  1665. "'tracing_threash' entry\n");
  1666. entry = debugfs_create_file("README", 0644, d_tracer,
  1667. NULL, &tracing_readme_fops);
  1668. if (!entry)
  1669. pr_warning("Could not create debugfs 'README' entry\n");
  1670. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  1671. NULL, &tracing_pipe_fops);
  1672. if (!entry)
  1673. pr_warning("Could not create debugfs "
  1674. "'tracing_threash' entry\n");
  1675. #ifdef CONFIG_DYNAMIC_FTRACE
  1676. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  1677. &ftrace_update_tot_cnt,
  1678. &tracing_read_long_fops);
  1679. if (!entry)
  1680. pr_warning("Could not create debugfs "
  1681. "'dyn_ftrace_total_info' entry\n");
  1682. #endif
  1683. }
  1684. /* dummy trace to disable tracing */
  1685. static struct tracer no_tracer __read_mostly =
  1686. {
  1687. .name = "none",
  1688. };
  1689. static int trace_alloc_page(void)
  1690. {
  1691. struct trace_array_cpu *data;
  1692. struct page *page, *tmp;
  1693. LIST_HEAD(pages);
  1694. void *array;
  1695. int i;
  1696. /* first allocate a page for each CPU */
  1697. for_each_possible_cpu(i) {
  1698. array = (void *)__get_free_page(GFP_KERNEL);
  1699. if (array == NULL) {
  1700. printk(KERN_ERR "tracer: failed to allocate page"
  1701. "for trace buffer!\n");
  1702. goto free_pages;
  1703. }
  1704. page = virt_to_page(array);
  1705. list_add(&page->lru, &pages);
  1706. /* Only allocate if we are actually using the max trace */
  1707. #ifdef CONFIG_TRACER_MAX_TRACE
  1708. array = (void *)__get_free_page(GFP_KERNEL);
  1709. if (array == NULL) {
  1710. printk(KERN_ERR "tracer: failed to allocate page"
  1711. "for trace buffer!\n");
  1712. goto free_pages;
  1713. }
  1714. page = virt_to_page(array);
  1715. list_add(&page->lru, &pages);
  1716. #endif
  1717. }
  1718. /* Now that we successfully allocate a page per CPU, add them */
  1719. for_each_possible_cpu(i) {
  1720. data = global_trace.data[i];
  1721. spin_lock_init(&data->lock);
  1722. lockdep_set_class(&data->lock, &data->lock_key);
  1723. page = list_entry(pages.next, struct page, lru);
  1724. list_del_init(&page->lru);
  1725. list_add_tail(&page->lru, &data->trace_pages);
  1726. ClearPageLRU(page);
  1727. #ifdef CONFIG_TRACER_MAX_TRACE
  1728. data = max_tr.data[i];
  1729. spin_lock_init(&data->lock);
  1730. lockdep_set_class(&data->lock, &data->lock_key);
  1731. page = list_entry(pages.next, struct page, lru);
  1732. list_del_init(&page->lru);
  1733. list_add_tail(&page->lru, &data->trace_pages);
  1734. SetPageLRU(page);
  1735. #endif
  1736. }
  1737. global_trace.entries += ENTRIES_PER_PAGE;
  1738. return 0;
  1739. free_pages:
  1740. list_for_each_entry_safe(page, tmp, &pages, lru) {
  1741. list_del_init(&page->lru);
  1742. __free_page(page);
  1743. }
  1744. return -ENOMEM;
  1745. }
  1746. __init static int tracer_alloc_buffers(void)
  1747. {
  1748. struct trace_array_cpu *data;
  1749. void *array;
  1750. struct page *page;
  1751. int pages = 0;
  1752. int ret = -ENOMEM;
  1753. int i;
  1754. /* Allocate the first page for all buffers */
  1755. for_each_possible_cpu(i) {
  1756. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  1757. max_tr.data[i] = &per_cpu(max_data, i);
  1758. array = (void *)__get_free_page(GFP_KERNEL);
  1759. if (array == NULL) {
  1760. printk(KERN_ERR "tracer: failed to allocate page"
  1761. "for trace buffer!\n");
  1762. goto free_buffers;
  1763. }
  1764. /* set the array to the list */
  1765. INIT_LIST_HEAD(&data->trace_pages);
  1766. page = virt_to_page(array);
  1767. list_add(&page->lru, &data->trace_pages);
  1768. /* use the LRU flag to differentiate the two buffers */
  1769. ClearPageLRU(page);
  1770. /* Only allocate if we are actually using the max trace */
  1771. #ifdef CONFIG_TRACER_MAX_TRACE
  1772. array = (void *)__get_free_page(GFP_KERNEL);
  1773. if (array == NULL) {
  1774. printk(KERN_ERR "tracer: failed to allocate page"
  1775. "for trace buffer!\n");
  1776. goto free_buffers;
  1777. }
  1778. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  1779. page = virt_to_page(array);
  1780. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  1781. SetPageLRU(page);
  1782. #endif
  1783. }
  1784. /*
  1785. * Since we allocate by orders of pages, we may be able to
  1786. * round up a bit.
  1787. */
  1788. global_trace.entries = ENTRIES_PER_PAGE;
  1789. pages++;
  1790. while (global_trace.entries < trace_nr_entries) {
  1791. if (trace_alloc_page())
  1792. break;
  1793. pages++;
  1794. }
  1795. max_tr.entries = global_trace.entries;
  1796. pr_info("tracer: %d pages allocated for %ld",
  1797. pages, trace_nr_entries);
  1798. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  1799. pr_info(" actual entries %ld\n", global_trace.entries);
  1800. tracer_init_debugfs();
  1801. trace_init_cmdlines();
  1802. register_tracer(&no_tracer);
  1803. current_trace = &no_tracer;
  1804. /* All seems OK, enable tracing */
  1805. tracing_disabled = 0;
  1806. return 0;
  1807. free_buffers:
  1808. for (i-- ; i >= 0; i--) {
  1809. struct page *page, *tmp;
  1810. struct trace_array_cpu *data = global_trace.data[i];
  1811. if (data) {
  1812. list_for_each_entry_safe(page, tmp,
  1813. &data->trace_pages, lru) {
  1814. list_del_init(&page->lru);
  1815. __free_page(page);
  1816. }
  1817. }
  1818. #ifdef CONFIG_TRACER_MAX_TRACE
  1819. data = max_tr.data[i];
  1820. if (data) {
  1821. list_for_each_entry_safe(page, tmp,
  1822. &data->trace_pages, lru) {
  1823. list_del_init(&page->lru);
  1824. __free_page(page);
  1825. }
  1826. }
  1827. #endif
  1828. }
  1829. return ret;
  1830. }
  1831. fs_initcall(tracer_alloc_buffers);