trace.c 49 KB

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