trace.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  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 trace_iterator_increment(struct trace_iterator *iter)
  624. {
  625. iter->idx++;
  626. iter->next_idx[iter->cpu]++;
  627. iter->next_page_idx[iter->cpu]++;
  628. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  629. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  630. iter->next_page_idx[iter->cpu] = 0;
  631. iter->next_page[iter->cpu] =
  632. trace_next_list(data, iter->next_page[iter->cpu]);
  633. }
  634. }
  635. static notrace void trace_consume(struct trace_iterator *iter)
  636. {
  637. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  638. data->trace_tail_idx++;
  639. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  640. data->trace_tail = trace_next_page(data, data->trace_tail);
  641. data->trace_tail_idx = 0;
  642. }
  643. /* Check if we empty it, then reset the index */
  644. if (data->trace_head == data->trace_tail &&
  645. data->trace_head_idx == data->trace_tail_idx)
  646. data->trace_idx = 0;
  647. }
  648. static notrace void *find_next_entry_inc(struct trace_iterator *iter)
  649. {
  650. struct trace_entry *next;
  651. int next_cpu = -1;
  652. next = find_next_entry(iter, &next_cpu);
  653. iter->prev_ent = iter->ent;
  654. iter->prev_cpu = iter->cpu;
  655. iter->ent = next;
  656. iter->cpu = next_cpu;
  657. if (next)
  658. trace_iterator_increment(iter);
  659. return next ? iter : NULL;
  660. }
  661. static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
  662. {
  663. struct trace_iterator *iter = m->private;
  664. void *last_ent = iter->ent;
  665. int i = (int)*pos;
  666. void *ent;
  667. (*pos)++;
  668. /* can't go backwards */
  669. if (iter->idx > i)
  670. return NULL;
  671. if (iter->idx < 0)
  672. ent = find_next_entry_inc(iter);
  673. else
  674. ent = iter;
  675. while (ent && iter->idx < i)
  676. ent = find_next_entry_inc(iter);
  677. iter->pos = *pos;
  678. if (last_ent && !ent)
  679. seq_puts(m, "\n\nvim:ft=help\n");
  680. return ent;
  681. }
  682. static void *s_start(struct seq_file *m, loff_t *pos)
  683. {
  684. struct trace_iterator *iter = m->private;
  685. void *p = NULL;
  686. loff_t l = 0;
  687. int i;
  688. mutex_lock(&trace_types_lock);
  689. if (!current_trace || current_trace != iter->trace)
  690. return NULL;
  691. atomic_inc(&trace_record_cmdline_disabled);
  692. /* let the tracer grab locks here if needed */
  693. if (current_trace->start)
  694. current_trace->start(iter);
  695. if (*pos != iter->pos) {
  696. iter->ent = NULL;
  697. iter->cpu = 0;
  698. iter->idx = -1;
  699. iter->prev_ent = NULL;
  700. iter->prev_cpu = -1;
  701. for_each_possible_cpu(i) {
  702. iter->next_idx[i] = 0;
  703. iter->next_page[i] = NULL;
  704. }
  705. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  706. ;
  707. } else {
  708. l = *pos - 1;
  709. p = s_next(m, p, &l);
  710. }
  711. return p;
  712. }
  713. static void s_stop(struct seq_file *m, void *p)
  714. {
  715. struct trace_iterator *iter = m->private;
  716. atomic_dec(&trace_record_cmdline_disabled);
  717. /* let the tracer release locks here if needed */
  718. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  719. iter->trace->stop(iter);
  720. mutex_unlock(&trace_types_lock);
  721. }
  722. static int
  723. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  724. {
  725. #ifdef CONFIG_KALLSYMS
  726. char str[KSYM_SYMBOL_LEN];
  727. kallsyms_lookup(address, NULL, NULL, NULL, str);
  728. return trace_seq_printf(s, fmt, str);
  729. #endif
  730. return 1;
  731. }
  732. static int
  733. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  734. unsigned long address)
  735. {
  736. #ifdef CONFIG_KALLSYMS
  737. char str[KSYM_SYMBOL_LEN];
  738. sprint_symbol(str, address);
  739. return trace_seq_printf(s, fmt, str);
  740. #endif
  741. return 1;
  742. }
  743. #ifndef CONFIG_64BIT
  744. # define IP_FMT "%08lx"
  745. #else
  746. # define IP_FMT "%016lx"
  747. #endif
  748. static notrace int
  749. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  750. {
  751. int ret;
  752. if (!ip)
  753. return trace_seq_printf(s, "0");
  754. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  755. ret = seq_print_sym_offset(s, "%s", ip);
  756. else
  757. ret = seq_print_sym_short(s, "%s", ip);
  758. if (!ret)
  759. return 0;
  760. if (sym_flags & TRACE_ITER_SYM_ADDR)
  761. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  762. return ret;
  763. }
  764. static notrace void print_lat_help_header(struct seq_file *m)
  765. {
  766. seq_puts(m, "# _------=> CPU# \n");
  767. seq_puts(m, "# / _-----=> irqs-off \n");
  768. seq_puts(m, "# | / _----=> need-resched \n");
  769. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  770. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  771. seq_puts(m, "# |||| / \n");
  772. seq_puts(m, "# ||||| delay \n");
  773. seq_puts(m, "# cmd pid ||||| time | caller \n");
  774. seq_puts(m, "# \\ / ||||| \\ | / \n");
  775. }
  776. static notrace void print_func_help_header(struct seq_file *m)
  777. {
  778. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  779. seq_puts(m, "# | | | | |\n");
  780. }
  781. static notrace void
  782. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  783. {
  784. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  785. struct trace_array *tr = iter->tr;
  786. struct trace_array_cpu *data = tr->data[tr->cpu];
  787. struct tracer *type = current_trace;
  788. unsigned long total = 0;
  789. unsigned long entries = 0;
  790. int cpu;
  791. const char *name = "preemption";
  792. if (type)
  793. name = type->name;
  794. for_each_possible_cpu(cpu) {
  795. if (head_page(tr->data[cpu])) {
  796. total += tr->data[cpu]->trace_idx;
  797. if (tr->data[cpu]->trace_idx > tr->entries)
  798. entries += tr->entries;
  799. else
  800. entries += tr->data[cpu]->trace_idx;
  801. }
  802. }
  803. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  804. name, UTS_RELEASE);
  805. seq_puts(m, "-----------------------------------"
  806. "---------------------------------\n");
  807. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  808. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  809. nsecs_to_usecs(data->saved_latency),
  810. entries,
  811. total,
  812. tr->cpu,
  813. #if defined(CONFIG_PREEMPT_NONE)
  814. "server",
  815. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  816. "desktop",
  817. #elif defined(CONFIG_PREEMPT_DESKTOP)
  818. "preempt",
  819. #else
  820. "unknown",
  821. #endif
  822. /* These are reserved for later use */
  823. 0, 0, 0, 0);
  824. #ifdef CONFIG_SMP
  825. seq_printf(m, " #P:%d)\n", num_online_cpus());
  826. #else
  827. seq_puts(m, ")\n");
  828. #endif
  829. seq_puts(m, " -----------------\n");
  830. seq_printf(m, " | task: %.16s-%d "
  831. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  832. data->comm, data->pid, data->uid, data->nice,
  833. data->policy, data->rt_priority);
  834. seq_puts(m, " -----------------\n");
  835. if (data->critical_start) {
  836. seq_puts(m, " => started at: ");
  837. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  838. trace_print_seq(m, &iter->seq);
  839. seq_puts(m, "\n => ended at: ");
  840. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  841. trace_print_seq(m, &iter->seq);
  842. seq_puts(m, "\n");
  843. }
  844. seq_puts(m, "\n");
  845. }
  846. static notrace void
  847. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  848. {
  849. int hardirq, softirq;
  850. char *comm;
  851. comm = trace_find_cmdline(entry->pid);
  852. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  853. trace_seq_printf(s, "%d", cpu);
  854. trace_seq_printf(s, "%c%c",
  855. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  856. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  857. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  858. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  859. if (hardirq && softirq)
  860. trace_seq_putc(s, 'H');
  861. else {
  862. if (hardirq)
  863. trace_seq_putc(s, 'h');
  864. else {
  865. if (softirq)
  866. trace_seq_putc(s, 's');
  867. else
  868. trace_seq_putc(s, '.');
  869. }
  870. }
  871. if (entry->preempt_count)
  872. trace_seq_printf(s, "%x", entry->preempt_count);
  873. else
  874. trace_seq_puts(s, ".");
  875. }
  876. unsigned long preempt_mark_thresh = 100;
  877. static notrace void
  878. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  879. unsigned long rel_usecs)
  880. {
  881. trace_seq_printf(s, " %4lldus", abs_usecs);
  882. if (rel_usecs > preempt_mark_thresh)
  883. trace_seq_puts(s, "!: ");
  884. else if (rel_usecs > 1)
  885. trace_seq_puts(s, "+: ");
  886. else
  887. trace_seq_puts(s, " : ");
  888. }
  889. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  890. static notrace void
  891. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  892. {
  893. struct trace_seq *s = &iter->seq;
  894. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  895. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  896. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  897. struct trace_entry *entry = iter->ent;
  898. unsigned long abs_usecs;
  899. unsigned long rel_usecs;
  900. char *comm;
  901. int S;
  902. if (!next_entry)
  903. next_entry = entry;
  904. rel_usecs = ns2usecs(next_entry->t - entry->t);
  905. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  906. if (verbose) {
  907. comm = trace_find_cmdline(entry->pid);
  908. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  909. " %ld.%03ldms (+%ld.%03ldms): ",
  910. comm,
  911. entry->pid, cpu, entry->flags,
  912. entry->preempt_count, trace_idx,
  913. ns2usecs(entry->t),
  914. abs_usecs/1000,
  915. abs_usecs % 1000, rel_usecs/1000,
  916. rel_usecs % 1000);
  917. } else {
  918. lat_print_generic(s, entry, cpu);
  919. lat_print_timestamp(s, abs_usecs, rel_usecs);
  920. }
  921. switch (entry->type) {
  922. case TRACE_FN:
  923. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  924. trace_seq_puts(s, " (");
  925. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  926. trace_seq_puts(s, ")\n");
  927. break;
  928. case TRACE_CTX:
  929. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  930. state_to_char[entry->ctx.prev_state] : 'X';
  931. comm = trace_find_cmdline(entry->ctx.next_pid);
  932. trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
  933. entry->ctx.prev_pid,
  934. entry->ctx.prev_prio,
  935. S,
  936. entry->ctx.next_pid,
  937. entry->ctx.next_prio,
  938. comm);
  939. break;
  940. default:
  941. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  942. }
  943. }
  944. static notrace int
  945. print_trace_fmt(struct trace_iterator *iter)
  946. {
  947. struct trace_seq *s = &iter->seq;
  948. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  949. struct trace_entry *entry;
  950. unsigned long usec_rem;
  951. unsigned long long t;
  952. unsigned long secs;
  953. char *comm;
  954. int S;
  955. int ret;
  956. entry = iter->ent;
  957. comm = trace_find_cmdline(iter->ent->pid);
  958. t = ns2usecs(entry->t);
  959. usec_rem = do_div(t, 1000000ULL);
  960. secs = (unsigned long)t;
  961. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  962. if (!ret)
  963. return 0;
  964. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  965. if (!ret)
  966. return 0;
  967. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  968. if (!ret)
  969. return 0;
  970. switch (entry->type) {
  971. case TRACE_FN:
  972. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  973. if (!ret)
  974. return 0;
  975. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  976. entry->fn.parent_ip) {
  977. ret = trace_seq_printf(s, " <-");
  978. if (!ret)
  979. return 0;
  980. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  981. sym_flags);
  982. if (!ret)
  983. return 0;
  984. }
  985. ret = trace_seq_printf(s, "\n");
  986. if (!ret)
  987. return 0;
  988. break;
  989. case TRACE_CTX:
  990. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  991. state_to_char[entry->ctx.prev_state] : 'X';
  992. ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
  993. entry->ctx.prev_pid,
  994. entry->ctx.prev_prio,
  995. S,
  996. entry->ctx.next_pid,
  997. entry->ctx.next_prio);
  998. if (!ret)
  999. return 0;
  1000. break;
  1001. }
  1002. return 1;
  1003. }
  1004. static int trace_empty(struct trace_iterator *iter)
  1005. {
  1006. struct trace_array_cpu *data;
  1007. int cpu;
  1008. for_each_possible_cpu(cpu) {
  1009. data = iter->tr->data[cpu];
  1010. if (head_page(data) && data->trace_idx &&
  1011. (data->trace_tail != data->trace_head ||
  1012. data->trace_tail_idx != data->trace_head_idx))
  1013. return 0;
  1014. }
  1015. return 1;
  1016. }
  1017. static int s_show(struct seq_file *m, void *v)
  1018. {
  1019. struct trace_iterator *iter = v;
  1020. if (iter->ent == NULL) {
  1021. if (iter->tr) {
  1022. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1023. seq_puts(m, "#\n");
  1024. }
  1025. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1026. /* print nothing if the buffers are empty */
  1027. if (trace_empty(iter))
  1028. return 0;
  1029. print_trace_header(m, iter);
  1030. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1031. print_lat_help_header(m);
  1032. } else {
  1033. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1034. print_func_help_header(m);
  1035. }
  1036. } else {
  1037. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1038. print_lat_fmt(iter, iter->idx, iter->cpu);
  1039. else
  1040. print_trace_fmt(iter);
  1041. trace_print_seq(m, &iter->seq);
  1042. }
  1043. return 0;
  1044. }
  1045. static struct seq_operations tracer_seq_ops = {
  1046. .start = s_start,
  1047. .next = s_next,
  1048. .stop = s_stop,
  1049. .show = s_show,
  1050. };
  1051. static struct trace_iterator notrace *
  1052. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1053. {
  1054. struct trace_iterator *iter;
  1055. if (tracing_disabled) {
  1056. *ret = -ENODEV;
  1057. return NULL;
  1058. }
  1059. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1060. if (!iter) {
  1061. *ret = -ENOMEM;
  1062. goto out;
  1063. }
  1064. mutex_lock(&trace_types_lock);
  1065. if (current_trace && current_trace->print_max)
  1066. iter->tr = &max_tr;
  1067. else
  1068. iter->tr = inode->i_private;
  1069. iter->trace = current_trace;
  1070. iter->pos = -1;
  1071. /* TODO stop tracer */
  1072. *ret = seq_open(file, &tracer_seq_ops);
  1073. if (!*ret) {
  1074. struct seq_file *m = file->private_data;
  1075. m->private = iter;
  1076. /* stop the trace while dumping */
  1077. if (iter->tr->ctrl)
  1078. tracer_enabled = 0;
  1079. if (iter->trace && iter->trace->open)
  1080. iter->trace->open(iter);
  1081. } else {
  1082. kfree(iter);
  1083. iter = NULL;
  1084. }
  1085. mutex_unlock(&trace_types_lock);
  1086. out:
  1087. return iter;
  1088. }
  1089. int tracing_open_generic(struct inode *inode, struct file *filp)
  1090. {
  1091. if (tracing_disabled)
  1092. return -ENODEV;
  1093. filp->private_data = inode->i_private;
  1094. return 0;
  1095. }
  1096. int tracing_release(struct inode *inode, struct file *file)
  1097. {
  1098. struct seq_file *m = (struct seq_file *)file->private_data;
  1099. struct trace_iterator *iter = m->private;
  1100. mutex_lock(&trace_types_lock);
  1101. if (iter->trace && iter->trace->close)
  1102. iter->trace->close(iter);
  1103. /* reenable tracing if it was previously enabled */
  1104. if (iter->tr->ctrl)
  1105. tracer_enabled = 1;
  1106. mutex_unlock(&trace_types_lock);
  1107. seq_release(inode, file);
  1108. kfree(iter);
  1109. return 0;
  1110. }
  1111. static int tracing_open(struct inode *inode, struct file *file)
  1112. {
  1113. int ret;
  1114. __tracing_open(inode, file, &ret);
  1115. return ret;
  1116. }
  1117. static int tracing_lt_open(struct inode *inode, struct file *file)
  1118. {
  1119. struct trace_iterator *iter;
  1120. int ret;
  1121. iter = __tracing_open(inode, file, &ret);
  1122. if (!ret)
  1123. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1124. return ret;
  1125. }
  1126. static notrace void *
  1127. t_next(struct seq_file *m, void *v, loff_t *pos)
  1128. {
  1129. struct tracer *t = m->private;
  1130. (*pos)++;
  1131. if (t)
  1132. t = t->next;
  1133. m->private = t;
  1134. return t;
  1135. }
  1136. static void *t_start(struct seq_file *m, loff_t *pos)
  1137. {
  1138. struct tracer *t = m->private;
  1139. loff_t l = 0;
  1140. mutex_lock(&trace_types_lock);
  1141. for (; t && l < *pos; t = t_next(m, t, &l))
  1142. ;
  1143. return t;
  1144. }
  1145. static void t_stop(struct seq_file *m, void *p)
  1146. {
  1147. mutex_unlock(&trace_types_lock);
  1148. }
  1149. static int t_show(struct seq_file *m, void *v)
  1150. {
  1151. struct tracer *t = v;
  1152. if (!t)
  1153. return 0;
  1154. seq_printf(m, "%s", t->name);
  1155. if (t->next)
  1156. seq_putc(m, ' ');
  1157. else
  1158. seq_putc(m, '\n');
  1159. return 0;
  1160. }
  1161. static struct seq_operations show_traces_seq_ops = {
  1162. .start = t_start,
  1163. .next = t_next,
  1164. .stop = t_stop,
  1165. .show = t_show,
  1166. };
  1167. static int show_traces_open(struct inode *inode, struct file *file)
  1168. {
  1169. int ret;
  1170. if (tracing_disabled)
  1171. return -ENODEV;
  1172. ret = seq_open(file, &show_traces_seq_ops);
  1173. if (!ret) {
  1174. struct seq_file *m = file->private_data;
  1175. m->private = trace_types;
  1176. }
  1177. return ret;
  1178. }
  1179. static struct file_operations tracing_fops = {
  1180. .open = tracing_open,
  1181. .read = seq_read,
  1182. .llseek = seq_lseek,
  1183. .release = tracing_release,
  1184. };
  1185. static struct file_operations tracing_lt_fops = {
  1186. .open = tracing_lt_open,
  1187. .read = seq_read,
  1188. .llseek = seq_lseek,
  1189. .release = tracing_release,
  1190. };
  1191. static struct file_operations show_traces_fops = {
  1192. .open = show_traces_open,
  1193. .read = seq_read,
  1194. .release = seq_release,
  1195. };
  1196. static ssize_t
  1197. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1198. size_t cnt, loff_t *ppos)
  1199. {
  1200. char *buf;
  1201. int r = 0;
  1202. int len = 0;
  1203. int i;
  1204. /* calulate max size */
  1205. for (i = 0; trace_options[i]; i++) {
  1206. len += strlen(trace_options[i]);
  1207. len += 3; /* "no" and space */
  1208. }
  1209. /* +2 for \n and \0 */
  1210. buf = kmalloc(len + 2, GFP_KERNEL);
  1211. if (!buf)
  1212. return -ENOMEM;
  1213. for (i = 0; trace_options[i]; i++) {
  1214. if (trace_flags & (1 << i))
  1215. r += sprintf(buf + r, "%s ", trace_options[i]);
  1216. else
  1217. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1218. }
  1219. r += sprintf(buf + r, "\n");
  1220. WARN_ON(r >= len + 2);
  1221. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1222. buf, r);
  1223. kfree(buf);
  1224. return r;
  1225. }
  1226. static ssize_t
  1227. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1228. size_t cnt, loff_t *ppos)
  1229. {
  1230. char buf[64];
  1231. char *cmp = buf;
  1232. int neg = 0;
  1233. int i;
  1234. if (cnt > 63)
  1235. cnt = 63;
  1236. if (copy_from_user(&buf, ubuf, cnt))
  1237. return -EFAULT;
  1238. buf[cnt] = 0;
  1239. if (strncmp(buf, "no", 2) == 0) {
  1240. neg = 1;
  1241. cmp += 2;
  1242. }
  1243. for (i = 0; trace_options[i]; i++) {
  1244. int len = strlen(trace_options[i]);
  1245. if (strncmp(cmp, trace_options[i], len) == 0) {
  1246. if (neg)
  1247. trace_flags &= ~(1 << i);
  1248. else
  1249. trace_flags |= (1 << i);
  1250. break;
  1251. }
  1252. }
  1253. filp->f_pos += cnt;
  1254. return cnt;
  1255. }
  1256. static struct file_operations tracing_iter_fops = {
  1257. .open = tracing_open_generic,
  1258. .read = tracing_iter_ctrl_read,
  1259. .write = tracing_iter_ctrl_write,
  1260. };
  1261. static const char readme_msg[] =
  1262. "tracing mini-HOWTO:\n\n"
  1263. "# mkdir /debug\n"
  1264. "# mount -t debugfs nodev /debug\n\n"
  1265. "# cat /debug/tracing/available_tracers\n"
  1266. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1267. "# cat /debug/tracing/current_tracer\n"
  1268. "none\n"
  1269. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1270. "# cat /debug/tracing/current_tracer\n"
  1271. "sched_switch\n"
  1272. "# cat /debug/tracing/iter_ctrl\n"
  1273. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1274. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1275. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1276. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1277. "echo 0 > /debug/tracing/tracing_enabled\n"
  1278. ;
  1279. static ssize_t
  1280. tracing_readme_read(struct file *filp, char __user *ubuf,
  1281. size_t cnt, loff_t *ppos)
  1282. {
  1283. return simple_read_from_buffer(ubuf, cnt, ppos,
  1284. readme_msg, strlen(readme_msg));
  1285. }
  1286. static struct file_operations tracing_readme_fops = {
  1287. .open = tracing_open_generic,
  1288. .read = tracing_readme_read,
  1289. };
  1290. static ssize_t
  1291. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1292. size_t cnt, loff_t *ppos)
  1293. {
  1294. struct trace_array *tr = filp->private_data;
  1295. char buf[64];
  1296. int r;
  1297. r = sprintf(buf, "%ld\n", tr->ctrl);
  1298. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1299. }
  1300. static ssize_t
  1301. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1302. size_t cnt, loff_t *ppos)
  1303. {
  1304. struct trace_array *tr = filp->private_data;
  1305. long val;
  1306. char buf[64];
  1307. if (cnt > 63)
  1308. cnt = 63;
  1309. if (copy_from_user(&buf, ubuf, cnt))
  1310. return -EFAULT;
  1311. buf[cnt] = 0;
  1312. val = simple_strtoul(buf, NULL, 10);
  1313. val = !!val;
  1314. mutex_lock(&trace_types_lock);
  1315. if (tr->ctrl ^ val) {
  1316. if (val)
  1317. tracer_enabled = 1;
  1318. else
  1319. tracer_enabled = 0;
  1320. tr->ctrl = val;
  1321. if (current_trace && current_trace->ctrl_update)
  1322. current_trace->ctrl_update(tr);
  1323. }
  1324. mutex_unlock(&trace_types_lock);
  1325. filp->f_pos += cnt;
  1326. return cnt;
  1327. }
  1328. static ssize_t
  1329. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1330. size_t cnt, loff_t *ppos)
  1331. {
  1332. char buf[max_tracer_type_len+2];
  1333. int r;
  1334. mutex_lock(&trace_types_lock);
  1335. if (current_trace)
  1336. r = sprintf(buf, "%s\n", current_trace->name);
  1337. else
  1338. r = sprintf(buf, "\n");
  1339. mutex_unlock(&trace_types_lock);
  1340. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1341. }
  1342. static ssize_t
  1343. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1344. size_t cnt, loff_t *ppos)
  1345. {
  1346. struct trace_array *tr = &global_trace;
  1347. struct tracer *t;
  1348. char buf[max_tracer_type_len+1];
  1349. int i;
  1350. if (cnt > max_tracer_type_len)
  1351. cnt = max_tracer_type_len;
  1352. if (copy_from_user(&buf, ubuf, cnt))
  1353. return -EFAULT;
  1354. buf[cnt] = 0;
  1355. /* strip ending whitespace. */
  1356. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1357. buf[i] = 0;
  1358. mutex_lock(&trace_types_lock);
  1359. for (t = trace_types; t; t = t->next) {
  1360. if (strcmp(t->name, buf) == 0)
  1361. break;
  1362. }
  1363. if (!t || t == current_trace)
  1364. goto out;
  1365. if (current_trace && current_trace->reset)
  1366. current_trace->reset(tr);
  1367. current_trace = t;
  1368. if (t->init)
  1369. t->init(tr);
  1370. out:
  1371. mutex_unlock(&trace_types_lock);
  1372. filp->f_pos += cnt;
  1373. return cnt;
  1374. }
  1375. static ssize_t
  1376. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1377. size_t cnt, loff_t *ppos)
  1378. {
  1379. unsigned long *ptr = filp->private_data;
  1380. char buf[64];
  1381. int r;
  1382. r = snprintf(buf, 64, "%ld\n",
  1383. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1384. if (r > 64)
  1385. r = 64;
  1386. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1387. }
  1388. static ssize_t
  1389. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1390. size_t cnt, loff_t *ppos)
  1391. {
  1392. long *ptr = filp->private_data;
  1393. long val;
  1394. char buf[64];
  1395. if (cnt > 63)
  1396. cnt = 63;
  1397. if (copy_from_user(&buf, ubuf, cnt))
  1398. return -EFAULT;
  1399. buf[cnt] = 0;
  1400. val = simple_strtoul(buf, NULL, 10);
  1401. *ptr = val * 1000;
  1402. return cnt;
  1403. }
  1404. static atomic_t tracing_reader;
  1405. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1406. {
  1407. struct trace_iterator *iter;
  1408. if (tracing_disabled)
  1409. return -ENODEV;
  1410. /* We only allow for reader of the pipe */
  1411. if (atomic_inc_return(&tracing_reader) != 1) {
  1412. atomic_dec(&tracing_reader);
  1413. return -EBUSY;
  1414. }
  1415. /* create a buffer to store the information to pass to userspace */
  1416. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1417. if (!iter)
  1418. return -ENOMEM;
  1419. iter->tr = &global_trace;
  1420. filp->private_data = iter;
  1421. return 0;
  1422. }
  1423. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1424. {
  1425. struct trace_iterator *iter = file->private_data;
  1426. kfree(iter);
  1427. atomic_dec(&tracing_reader);
  1428. return 0;
  1429. }
  1430. /*
  1431. * Consumer reader.
  1432. */
  1433. static ssize_t
  1434. tracing_read_pipe(struct file *filp, char __user *ubuf,
  1435. size_t cnt, loff_t *ppos)
  1436. {
  1437. struct trace_iterator *iter = filp->private_data;
  1438. struct trace_array_cpu *data;
  1439. static cpumask_t mask;
  1440. struct trace_entry *entry;
  1441. static int start;
  1442. unsigned long flags;
  1443. int read = 0;
  1444. int cpu;
  1445. int len;
  1446. int ret;
  1447. /* return any leftover data */
  1448. if (iter->seq.len > start) {
  1449. len = iter->seq.len - start;
  1450. if (cnt > len)
  1451. cnt = len;
  1452. ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
  1453. if (ret)
  1454. cnt = -EFAULT;
  1455. start += len;
  1456. return cnt;
  1457. }
  1458. trace_seq_reset(&iter->seq);
  1459. start = 0;
  1460. while (trace_empty(iter)) {
  1461. /*
  1462. * This is a make-shift waitqueue. The reason we don't use
  1463. * an actual wait queue is because:
  1464. * 1) we only ever have one waiter
  1465. * 2) the tracing, traces all functions, we don't want
  1466. * the overhead of calling wake_up and friends
  1467. * (and tracing them too)
  1468. * Anyway, this is really very primitive wakeup.
  1469. */
  1470. set_current_state(TASK_INTERRUPTIBLE);
  1471. iter->tr->waiter = current;
  1472. /* sleep for one second, and try again. */
  1473. schedule_timeout(HZ);
  1474. iter->tr->waiter = NULL;
  1475. if (signal_pending(current))
  1476. return -EINTR;
  1477. /*
  1478. * We block until we read something and tracing is disabled.
  1479. * We still block if tracing is disabled, but we have never
  1480. * read anything. This allows a user to cat this file, and
  1481. * then enable tracing. But after we have read something,
  1482. * we give an EOF when tracing is again disabled.
  1483. *
  1484. * iter->pos will be 0 if we haven't read anything.
  1485. */
  1486. if (!tracer_enabled && iter->pos)
  1487. break;
  1488. continue;
  1489. }
  1490. /* stop when tracing is finished */
  1491. if (trace_empty(iter))
  1492. return 0;
  1493. if (cnt >= PAGE_SIZE)
  1494. cnt = PAGE_SIZE - 1;
  1495. memset(iter, 0, sizeof(*iter));
  1496. iter->tr = &global_trace;
  1497. iter->pos = -1;
  1498. /*
  1499. * We need to stop all tracing on all CPUS to read the
  1500. * the next buffer. This is a bit expensive, but is
  1501. * not done often. We fill all what we can read,
  1502. * and then release the locks again.
  1503. */
  1504. cpus_clear(mask);
  1505. local_irq_save(flags);
  1506. for_each_possible_cpu(cpu) {
  1507. data = iter->tr->data[cpu];
  1508. if (!head_page(data) || !data->trace_idx)
  1509. continue;
  1510. atomic_inc(&data->disabled);
  1511. spin_lock(&data->lock);
  1512. cpu_set(cpu, mask);
  1513. }
  1514. while ((entry = find_next_entry_inc(iter)) != NULL) {
  1515. ret = print_trace_fmt(iter);
  1516. if (!ret)
  1517. break;
  1518. trace_consume(iter);
  1519. if (iter->seq.len >= cnt)
  1520. break;
  1521. }
  1522. for_each_cpu_mask(cpu, mask) {
  1523. data = iter->tr->data[cpu];
  1524. spin_unlock(&data->lock);
  1525. atomic_dec(&data->disabled);
  1526. }
  1527. local_irq_restore(flags);
  1528. /* Now copy what we have to the user */
  1529. read = iter->seq.len;
  1530. if (read > cnt)
  1531. read = cnt;
  1532. ret = copy_to_user(ubuf, iter->seq.buffer, read);
  1533. if (read < iter->seq.len)
  1534. start = read;
  1535. else
  1536. trace_seq_reset(&iter->seq);
  1537. if (ret)
  1538. read = -EFAULT;
  1539. return read;
  1540. }
  1541. static struct file_operations tracing_max_lat_fops = {
  1542. .open = tracing_open_generic,
  1543. .read = tracing_max_lat_read,
  1544. .write = tracing_max_lat_write,
  1545. };
  1546. static struct file_operations tracing_ctrl_fops = {
  1547. .open = tracing_open_generic,
  1548. .read = tracing_ctrl_read,
  1549. .write = tracing_ctrl_write,
  1550. };
  1551. static struct file_operations set_tracer_fops = {
  1552. .open = tracing_open_generic,
  1553. .read = tracing_set_trace_read,
  1554. .write = tracing_set_trace_write,
  1555. };
  1556. static struct file_operations tracing_pipe_fops = {
  1557. .open = tracing_open_pipe,
  1558. .read = tracing_read_pipe,
  1559. .release = tracing_release_pipe,
  1560. };
  1561. #ifdef CONFIG_DYNAMIC_FTRACE
  1562. static ssize_t
  1563. tracing_read_long(struct file *filp, char __user *ubuf,
  1564. size_t cnt, loff_t *ppos)
  1565. {
  1566. unsigned long *p = filp->private_data;
  1567. char buf[64];
  1568. int r;
  1569. r = sprintf(buf, "%ld\n", *p);
  1570. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1571. }
  1572. static struct file_operations tracing_read_long_fops = {
  1573. .open = tracing_open_generic,
  1574. .read = tracing_read_long,
  1575. };
  1576. #endif
  1577. static struct dentry *d_tracer;
  1578. struct dentry *tracing_init_dentry(void)
  1579. {
  1580. static int once;
  1581. if (d_tracer)
  1582. return d_tracer;
  1583. d_tracer = debugfs_create_dir("tracing", NULL);
  1584. if (!d_tracer && !once) {
  1585. once = 1;
  1586. pr_warning("Could not create debugfs directory 'tracing'\n");
  1587. return NULL;
  1588. }
  1589. return d_tracer;
  1590. }
  1591. #ifdef CONFIG_FTRACE_SELFTEST
  1592. /* Let selftest have access to static functions in this file */
  1593. #include "trace_selftest.c"
  1594. #endif
  1595. static __init void tracer_init_debugfs(void)
  1596. {
  1597. struct dentry *d_tracer;
  1598. struct dentry *entry;
  1599. d_tracer = tracing_init_dentry();
  1600. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  1601. &global_trace, &tracing_ctrl_fops);
  1602. if (!entry)
  1603. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  1604. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  1605. NULL, &tracing_iter_fops);
  1606. if (!entry)
  1607. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  1608. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  1609. &global_trace, &tracing_lt_fops);
  1610. if (!entry)
  1611. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  1612. entry = debugfs_create_file("trace", 0444, d_tracer,
  1613. &global_trace, &tracing_fops);
  1614. if (!entry)
  1615. pr_warning("Could not create debugfs 'trace' entry\n");
  1616. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  1617. &global_trace, &show_traces_fops);
  1618. if (!entry)
  1619. pr_warning("Could not create debugfs 'trace' entry\n");
  1620. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  1621. &global_trace, &set_tracer_fops);
  1622. if (!entry)
  1623. pr_warning("Could not create debugfs 'trace' entry\n");
  1624. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  1625. &tracing_max_latency,
  1626. &tracing_max_lat_fops);
  1627. if (!entry)
  1628. pr_warning("Could not create debugfs "
  1629. "'tracing_max_latency' entry\n");
  1630. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  1631. &tracing_thresh, &tracing_max_lat_fops);
  1632. if (!entry)
  1633. pr_warning("Could not create debugfs "
  1634. "'tracing_threash' entry\n");
  1635. entry = debugfs_create_file("README", 0644, d_tracer,
  1636. NULL, &tracing_readme_fops);
  1637. if (!entry)
  1638. pr_warning("Could not create debugfs 'README' entry\n");
  1639. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  1640. NULL, &tracing_pipe_fops);
  1641. if (!entry)
  1642. pr_warning("Could not create debugfs "
  1643. "'tracing_threash' entry\n");
  1644. #ifdef CONFIG_DYNAMIC_FTRACE
  1645. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  1646. &ftrace_update_tot_cnt,
  1647. &tracing_read_long_fops);
  1648. if (!entry)
  1649. pr_warning("Could not create debugfs "
  1650. "'dyn_ftrace_total_info' entry\n");
  1651. #endif
  1652. }
  1653. /* dummy trace to disable tracing */
  1654. static struct tracer no_tracer __read_mostly =
  1655. {
  1656. .name = "none",
  1657. };
  1658. static int trace_alloc_page(void)
  1659. {
  1660. struct trace_array_cpu *data;
  1661. struct page *page, *tmp;
  1662. LIST_HEAD(pages);
  1663. void *array;
  1664. int i;
  1665. /* first allocate a page for each CPU */
  1666. for_each_possible_cpu(i) {
  1667. array = (void *)__get_free_page(GFP_KERNEL);
  1668. if (array == NULL) {
  1669. printk(KERN_ERR "tracer: failed to allocate page"
  1670. "for trace buffer!\n");
  1671. goto free_pages;
  1672. }
  1673. page = virt_to_page(array);
  1674. list_add(&page->lru, &pages);
  1675. /* Only allocate if we are actually using the max trace */
  1676. #ifdef CONFIG_TRACER_MAX_TRACE
  1677. array = (void *)__get_free_page(GFP_KERNEL);
  1678. if (array == NULL) {
  1679. printk(KERN_ERR "tracer: failed to allocate page"
  1680. "for trace buffer!\n");
  1681. goto free_pages;
  1682. }
  1683. page = virt_to_page(array);
  1684. list_add(&page->lru, &pages);
  1685. #endif
  1686. }
  1687. /* Now that we successfully allocate a page per CPU, add them */
  1688. for_each_possible_cpu(i) {
  1689. data = global_trace.data[i];
  1690. spin_lock_init(&data->lock);
  1691. lockdep_set_class(&data->lock, &data->lock_key);
  1692. page = list_entry(pages.next, struct page, lru);
  1693. list_del_init(&page->lru);
  1694. list_add_tail(&page->lru, &data->trace_pages);
  1695. ClearPageLRU(page);
  1696. #ifdef CONFIG_TRACER_MAX_TRACE
  1697. data = max_tr.data[i];
  1698. spin_lock_init(&data->lock);
  1699. lockdep_set_class(&data->lock, &data->lock_key);
  1700. page = list_entry(pages.next, struct page, lru);
  1701. list_del_init(&page->lru);
  1702. list_add_tail(&page->lru, &data->trace_pages);
  1703. SetPageLRU(page);
  1704. #endif
  1705. }
  1706. global_trace.entries += ENTRIES_PER_PAGE;
  1707. return 0;
  1708. free_pages:
  1709. list_for_each_entry_safe(page, tmp, &pages, lru) {
  1710. list_del_init(&page->lru);
  1711. __free_page(page);
  1712. }
  1713. return -ENOMEM;
  1714. }
  1715. __init static int tracer_alloc_buffers(void)
  1716. {
  1717. struct trace_array_cpu *data;
  1718. void *array;
  1719. struct page *page;
  1720. int pages = 0;
  1721. int ret = -ENOMEM;
  1722. int i;
  1723. /* Allocate the first page for all buffers */
  1724. for_each_possible_cpu(i) {
  1725. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  1726. max_tr.data[i] = &per_cpu(max_data, i);
  1727. array = (void *)__get_free_page(GFP_KERNEL);
  1728. if (array == NULL) {
  1729. printk(KERN_ERR "tracer: failed to allocate page"
  1730. "for trace buffer!\n");
  1731. goto free_buffers;
  1732. }
  1733. /* set the array to the list */
  1734. INIT_LIST_HEAD(&data->trace_pages);
  1735. page = virt_to_page(array);
  1736. list_add(&page->lru, &data->trace_pages);
  1737. /* use the LRU flag to differentiate the two buffers */
  1738. ClearPageLRU(page);
  1739. /* Only allocate if we are actually using the max trace */
  1740. #ifdef CONFIG_TRACER_MAX_TRACE
  1741. array = (void *)__get_free_page(GFP_KERNEL);
  1742. if (array == NULL) {
  1743. printk(KERN_ERR "tracer: failed to allocate page"
  1744. "for trace buffer!\n");
  1745. goto free_buffers;
  1746. }
  1747. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  1748. page = virt_to_page(array);
  1749. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  1750. SetPageLRU(page);
  1751. #endif
  1752. }
  1753. /*
  1754. * Since we allocate by orders of pages, we may be able to
  1755. * round up a bit.
  1756. */
  1757. global_trace.entries = ENTRIES_PER_PAGE;
  1758. pages++;
  1759. while (global_trace.entries < trace_nr_entries) {
  1760. if (trace_alloc_page())
  1761. break;
  1762. pages++;
  1763. }
  1764. max_tr.entries = global_trace.entries;
  1765. pr_info("tracer: %d pages allocated for %ld",
  1766. pages, trace_nr_entries);
  1767. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  1768. pr_info(" actual entries %ld\n", global_trace.entries);
  1769. tracer_init_debugfs();
  1770. trace_init_cmdlines();
  1771. register_tracer(&no_tracer);
  1772. current_trace = &no_tracer;
  1773. /* All seems OK, enable tracing */
  1774. tracing_disabled = 0;
  1775. return 0;
  1776. free_buffers:
  1777. for (i-- ; i >= 0; i--) {
  1778. struct page *page, *tmp;
  1779. struct trace_array_cpu *data = global_trace.data[i];
  1780. if (data) {
  1781. list_for_each_entry_safe(page, tmp,
  1782. &data->trace_pages, lru) {
  1783. list_del_init(&page->lru);
  1784. __free_page(page);
  1785. }
  1786. }
  1787. #ifdef CONFIG_TRACER_MAX_TRACE
  1788. data = max_tr.data[i];
  1789. if (data) {
  1790. list_for_each_entry_safe(page, tmp,
  1791. &data->trace_pages, lru) {
  1792. list_del_init(&page->lru);
  1793. __free_page(page);
  1794. }
  1795. }
  1796. #endif
  1797. }
  1798. return ret;
  1799. }
  1800. fs_initcall(tracer_alloc_buffers);