trace.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073
  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/poll.h>
  28. #include <linux/gfp.h>
  29. #include <linux/fs.h>
  30. #include <linux/writeback.h>
  31. #include <linux/stacktrace.h>
  32. #include "trace.h"
  33. unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
  34. unsigned long __read_mostly tracing_thresh;
  35. static unsigned long __read_mostly tracing_nr_buffers;
  36. static cpumask_t __read_mostly tracing_buffer_mask;
  37. #define for_each_tracing_cpu(cpu) \
  38. for_each_cpu_mask(cpu, tracing_buffer_mask)
  39. /* dummy trace to disable tracing */
  40. static struct tracer no_tracer __read_mostly = {
  41. .name = "none",
  42. };
  43. static int trace_alloc_page(void);
  44. static int trace_free_page(void);
  45. static int tracing_disabled = 1;
  46. static unsigned long tracing_pages_allocated;
  47. long
  48. ns2usecs(cycle_t nsec)
  49. {
  50. nsec += 500;
  51. do_div(nsec, 1000);
  52. return nsec;
  53. }
  54. cycle_t ftrace_now(int cpu)
  55. {
  56. return cpu_clock(cpu);
  57. }
  58. /*
  59. * The global_trace is the descriptor that holds the tracing
  60. * buffers for the live tracing. For each CPU, it contains
  61. * a link list of pages that will store trace entries. The
  62. * page descriptor of the pages in the memory is used to hold
  63. * the link list by linking the lru item in the page descriptor
  64. * to each of the pages in the buffer per CPU.
  65. *
  66. * For each active CPU there is a data field that holds the
  67. * pages for the buffer for that CPU. Each CPU has the same number
  68. * of pages allocated for its buffer.
  69. */
  70. static struct trace_array global_trace;
  71. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  72. /*
  73. * The max_tr is used to snapshot the global_trace when a maximum
  74. * latency is reached. Some tracers will use this to store a maximum
  75. * trace while it continues examining live traces.
  76. *
  77. * The buffers for the max_tr are set up the same as the global_trace.
  78. * When a snapshot is taken, the link list of the max_tr is swapped
  79. * with the link list of the global_trace and the buffers are reset for
  80. * the global_trace so the tracing can continue.
  81. */
  82. static struct trace_array max_tr;
  83. static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
  84. /* tracer_enabled is used to toggle activation of a tracer */
  85. static int tracer_enabled = 1;
  86. /*
  87. * trace_nr_entries is the number of entries that is allocated
  88. * for a buffer. Note, the number of entries is always rounded
  89. * to ENTRIES_PER_PAGE.
  90. */
  91. static unsigned long trace_nr_entries = 65536UL;
  92. /* trace_types holds a link list of available tracers. */
  93. static struct tracer *trace_types __read_mostly;
  94. /* current_trace points to the tracer that is currently active */
  95. static struct tracer *current_trace __read_mostly;
  96. /*
  97. * max_tracer_type_len is used to simplify the allocating of
  98. * buffers to read userspace tracer names. We keep track of
  99. * the longest tracer name registered.
  100. */
  101. static int max_tracer_type_len;
  102. /*
  103. * trace_types_lock is used to protect the trace_types list.
  104. * This lock is also used to keep user access serialized.
  105. * Accesses from userspace will grab this lock while userspace
  106. * activities happen inside the kernel.
  107. */
  108. static DEFINE_MUTEX(trace_types_lock);
  109. /* trace_wait is a waitqueue for tasks blocked on trace_poll */
  110. static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
  111. /* trace_flags holds iter_ctrl options */
  112. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
  113. /**
  114. * trace_wake_up - wake up tasks waiting for trace input
  115. *
  116. * Simply wakes up any task that is blocked on the trace_wait
  117. * queue. These is used with trace_poll for tasks polling the trace.
  118. */
  119. void trace_wake_up(void)
  120. {
  121. /*
  122. * The runqueue_is_locked() can fail, but this is the best we
  123. * have for now:
  124. */
  125. if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
  126. wake_up(&trace_wait);
  127. }
  128. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
  129. static int __init set_nr_entries(char *str)
  130. {
  131. unsigned long nr_entries;
  132. int ret;
  133. if (!str)
  134. return 0;
  135. ret = strict_strtoul(str, 0, &nr_entries);
  136. /* nr_entries can not be zero */
  137. if (ret < 0 || nr_entries == 0)
  138. return 0;
  139. trace_nr_entries = nr_entries;
  140. return 1;
  141. }
  142. __setup("trace_entries=", set_nr_entries);
  143. unsigned long nsecs_to_usecs(unsigned long nsecs)
  144. {
  145. return nsecs / 1000;
  146. }
  147. /*
  148. * trace_flag_type is an enumeration that holds different
  149. * states when a trace occurs. These are:
  150. * IRQS_OFF - interrupts were disabled
  151. * NEED_RESCED - reschedule is requested
  152. * HARDIRQ - inside an interrupt handler
  153. * SOFTIRQ - inside a softirq handler
  154. */
  155. enum trace_flag_type {
  156. TRACE_FLAG_IRQS_OFF = 0x01,
  157. TRACE_FLAG_NEED_RESCHED = 0x02,
  158. TRACE_FLAG_HARDIRQ = 0x04,
  159. TRACE_FLAG_SOFTIRQ = 0x08,
  160. };
  161. /*
  162. * TRACE_ITER_SYM_MASK masks the options in trace_flags that
  163. * control the output of kernel symbols.
  164. */
  165. #define TRACE_ITER_SYM_MASK \
  166. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  167. /* These must match the bit postions in trace_iterator_flags */
  168. static const char *trace_options[] = {
  169. "print-parent",
  170. "sym-offset",
  171. "sym-addr",
  172. "verbose",
  173. "raw",
  174. "hex",
  175. "bin",
  176. "block",
  177. "stacktrace",
  178. "sched-tree",
  179. NULL
  180. };
  181. /*
  182. * ftrace_max_lock is used to protect the swapping of buffers
  183. * when taking a max snapshot. The buffers themselves are
  184. * protected by per_cpu spinlocks. But the action of the swap
  185. * needs its own lock.
  186. *
  187. * This is defined as a raw_spinlock_t in order to help
  188. * with performance when lockdep debugging is enabled.
  189. */
  190. static raw_spinlock_t ftrace_max_lock =
  191. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  192. /*
  193. * Copy the new maximum trace into the separate maximum-trace
  194. * structure. (this way the maximum trace is permanently saved,
  195. * for later retrieval via /debugfs/tracing/latency_trace)
  196. */
  197. static void
  198. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  199. {
  200. struct trace_array_cpu *data = tr->data[cpu];
  201. max_tr.cpu = cpu;
  202. max_tr.time_start = data->preempt_timestamp;
  203. data = max_tr.data[cpu];
  204. data->saved_latency = tracing_max_latency;
  205. memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
  206. data->pid = tsk->pid;
  207. data->uid = tsk->uid;
  208. data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  209. data->policy = tsk->policy;
  210. data->rt_priority = tsk->rt_priority;
  211. /* record this tasks comm */
  212. tracing_record_cmdline(current);
  213. }
  214. /**
  215. * check_pages - integrity check of trace buffers
  216. *
  217. * As a safty measure we check to make sure the data pages have not
  218. * been corrupted. TODO: configure to disable this because it adds
  219. * a bit of overhead.
  220. */
  221. void check_pages(struct trace_array_cpu *data)
  222. {
  223. struct page *page, *tmp;
  224. BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
  225. BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
  226. list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
  227. BUG_ON(page->lru.next->prev != &page->lru);
  228. BUG_ON(page->lru.prev->next != &page->lru);
  229. }
  230. }
  231. /**
  232. * head_page - page address of the first page in per_cpu buffer.
  233. *
  234. * head_page returns the page address of the first page in
  235. * a per_cpu buffer. This also preforms various consistency
  236. * checks to make sure the buffer has not been corrupted.
  237. */
  238. void *head_page(struct trace_array_cpu *data)
  239. {
  240. struct page *page;
  241. check_pages(data);
  242. if (list_empty(&data->trace_pages))
  243. return NULL;
  244. page = list_entry(data->trace_pages.next, struct page, lru);
  245. BUG_ON(&page->lru == &data->trace_pages);
  246. return page_address(page);
  247. }
  248. /**
  249. * trace_seq_printf - sequence printing of trace information
  250. * @s: trace sequence descriptor
  251. * @fmt: printf format string
  252. *
  253. * The tracer may use either sequence operations or its own
  254. * copy to user routines. To simplify formating of a trace
  255. * trace_seq_printf is used to store strings into a special
  256. * buffer (@s). Then the output may be either used by
  257. * the sequencer or pulled into another buffer.
  258. */
  259. int
  260. trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  261. {
  262. int len = (PAGE_SIZE - 1) - s->len;
  263. va_list ap;
  264. int ret;
  265. if (!len)
  266. return 0;
  267. va_start(ap, fmt);
  268. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  269. va_end(ap);
  270. /* If we can't write it all, don't bother writing anything */
  271. if (ret >= len)
  272. return 0;
  273. s->len += ret;
  274. return len;
  275. }
  276. /**
  277. * trace_seq_puts - trace sequence printing of simple string
  278. * @s: trace sequence descriptor
  279. * @str: simple string to record
  280. *
  281. * The tracer may use either the sequence operations or its own
  282. * copy to user routines. This function records a simple string
  283. * into a special buffer (@s) for later retrieval by a sequencer
  284. * or other mechanism.
  285. */
  286. static int
  287. trace_seq_puts(struct trace_seq *s, const char *str)
  288. {
  289. int len = strlen(str);
  290. if (len > ((PAGE_SIZE - 1) - s->len))
  291. return 0;
  292. memcpy(s->buffer + s->len, str, len);
  293. s->len += len;
  294. return len;
  295. }
  296. static int
  297. trace_seq_putc(struct trace_seq *s, unsigned char c)
  298. {
  299. if (s->len >= (PAGE_SIZE - 1))
  300. return 0;
  301. s->buffer[s->len++] = c;
  302. return 1;
  303. }
  304. static int
  305. trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
  306. {
  307. if (len > ((PAGE_SIZE - 1) - s->len))
  308. return 0;
  309. memcpy(s->buffer + s->len, mem, len);
  310. s->len += len;
  311. return len;
  312. }
  313. #define HEX_CHARS 17
  314. static const char hex2asc[] = "0123456789abcdef";
  315. static int
  316. trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
  317. {
  318. unsigned char hex[HEX_CHARS];
  319. unsigned char *data = mem;
  320. unsigned char byte;
  321. int i, j;
  322. BUG_ON(len >= HEX_CHARS);
  323. #ifdef __BIG_ENDIAN
  324. for (i = 0, j = 0; i < len; i++) {
  325. #else
  326. for (i = len-1, j = 0; i >= 0; i--) {
  327. #endif
  328. byte = data[i];
  329. hex[j++] = hex2asc[byte & 0x0f];
  330. hex[j++] = hex2asc[byte >> 4];
  331. }
  332. hex[j++] = ' ';
  333. return trace_seq_putmem(s, hex, j);
  334. }
  335. static void
  336. trace_seq_reset(struct trace_seq *s)
  337. {
  338. s->len = 0;
  339. s->readpos = 0;
  340. }
  341. ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
  342. {
  343. int len;
  344. int ret;
  345. if (s->len <= s->readpos)
  346. return -EBUSY;
  347. len = s->len - s->readpos;
  348. if (cnt > len)
  349. cnt = len;
  350. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  351. if (ret)
  352. return -EFAULT;
  353. s->readpos += len;
  354. return cnt;
  355. }
  356. static void
  357. trace_print_seq(struct seq_file *m, struct trace_seq *s)
  358. {
  359. int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
  360. s->buffer[len] = 0;
  361. seq_puts(m, s->buffer);
  362. trace_seq_reset(s);
  363. }
  364. /*
  365. * flip the trace buffers between two trace descriptors.
  366. * This usually is the buffers between the global_trace and
  367. * the max_tr to record a snapshot of a current trace.
  368. *
  369. * The ftrace_max_lock must be held.
  370. */
  371. static void
  372. flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
  373. {
  374. struct list_head flip_pages;
  375. INIT_LIST_HEAD(&flip_pages);
  376. memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
  377. sizeof(struct trace_array_cpu) -
  378. offsetof(struct trace_array_cpu, trace_head_idx));
  379. check_pages(tr1);
  380. check_pages(tr2);
  381. list_splice_init(&tr1->trace_pages, &flip_pages);
  382. list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
  383. list_splice_init(&flip_pages, &tr2->trace_pages);
  384. BUG_ON(!list_empty(&flip_pages));
  385. check_pages(tr1);
  386. check_pages(tr2);
  387. }
  388. /**
  389. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  390. * @tr: tracer
  391. * @tsk: the task with the latency
  392. * @cpu: The cpu that initiated the trace.
  393. *
  394. * Flip the buffers between the @tr and the max_tr and record information
  395. * about which task was the cause of this latency.
  396. */
  397. void
  398. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  399. {
  400. struct trace_array_cpu *data;
  401. int i;
  402. WARN_ON_ONCE(!irqs_disabled());
  403. __raw_spin_lock(&ftrace_max_lock);
  404. /* clear out all the previous traces */
  405. for_each_tracing_cpu(i) {
  406. data = tr->data[i];
  407. flip_trace(max_tr.data[i], data);
  408. tracing_reset(data);
  409. }
  410. __update_max_tr(tr, tsk, cpu);
  411. __raw_spin_unlock(&ftrace_max_lock);
  412. }
  413. /**
  414. * update_max_tr_single - only copy one trace over, and reset the rest
  415. * @tr - tracer
  416. * @tsk - task with the latency
  417. * @cpu - the cpu of the buffer to copy.
  418. *
  419. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  420. */
  421. void
  422. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  423. {
  424. struct trace_array_cpu *data = tr->data[cpu];
  425. int i;
  426. WARN_ON_ONCE(!irqs_disabled());
  427. __raw_spin_lock(&ftrace_max_lock);
  428. for_each_tracing_cpu(i)
  429. tracing_reset(max_tr.data[i]);
  430. flip_trace(max_tr.data[cpu], data);
  431. tracing_reset(data);
  432. __update_max_tr(tr, tsk, cpu);
  433. __raw_spin_unlock(&ftrace_max_lock);
  434. }
  435. /**
  436. * register_tracer - register a tracer with the ftrace system.
  437. * @type - the plugin for the tracer
  438. *
  439. * Register a new plugin tracer.
  440. */
  441. int register_tracer(struct tracer *type)
  442. {
  443. struct tracer *t;
  444. int len;
  445. int ret = 0;
  446. if (!type->name) {
  447. pr_info("Tracer must have a name\n");
  448. return -1;
  449. }
  450. mutex_lock(&trace_types_lock);
  451. for (t = trace_types; t; t = t->next) {
  452. if (strcmp(type->name, t->name) == 0) {
  453. /* already found */
  454. pr_info("Trace %s already registered\n",
  455. type->name);
  456. ret = -1;
  457. goto out;
  458. }
  459. }
  460. #ifdef CONFIG_FTRACE_STARTUP_TEST
  461. if (type->selftest) {
  462. struct tracer *saved_tracer = current_trace;
  463. struct trace_array_cpu *data;
  464. struct trace_array *tr = &global_trace;
  465. int saved_ctrl = tr->ctrl;
  466. int i;
  467. /*
  468. * Run a selftest on this tracer.
  469. * Here we reset the trace buffer, and set the current
  470. * tracer to be this tracer. The tracer can then run some
  471. * internal tracing to verify that everything is in order.
  472. * If we fail, we do not register this tracer.
  473. */
  474. for_each_tracing_cpu(i) {
  475. data = tr->data[i];
  476. if (!head_page(data))
  477. continue;
  478. tracing_reset(data);
  479. }
  480. current_trace = type;
  481. tr->ctrl = 0;
  482. /* the test is responsible for initializing and enabling */
  483. pr_info("Testing tracer %s: ", type->name);
  484. ret = type->selftest(type, tr);
  485. /* the test is responsible for resetting too */
  486. current_trace = saved_tracer;
  487. tr->ctrl = saved_ctrl;
  488. if (ret) {
  489. printk(KERN_CONT "FAILED!\n");
  490. goto out;
  491. }
  492. /* Only reset on passing, to avoid touching corrupted buffers */
  493. for_each_tracing_cpu(i) {
  494. data = tr->data[i];
  495. if (!head_page(data))
  496. continue;
  497. tracing_reset(data);
  498. }
  499. printk(KERN_CONT "PASSED\n");
  500. }
  501. #endif
  502. type->next = trace_types;
  503. trace_types = type;
  504. len = strlen(type->name);
  505. if (len > max_tracer_type_len)
  506. max_tracer_type_len = len;
  507. out:
  508. mutex_unlock(&trace_types_lock);
  509. return ret;
  510. }
  511. void unregister_tracer(struct tracer *type)
  512. {
  513. struct tracer **t;
  514. int len;
  515. mutex_lock(&trace_types_lock);
  516. for (t = &trace_types; *t; t = &(*t)->next) {
  517. if (*t == type)
  518. goto found;
  519. }
  520. pr_info("Trace %s not registered\n", type->name);
  521. goto out;
  522. found:
  523. *t = (*t)->next;
  524. if (strlen(type->name) != max_tracer_type_len)
  525. goto out;
  526. max_tracer_type_len = 0;
  527. for (t = &trace_types; *t; t = &(*t)->next) {
  528. len = strlen((*t)->name);
  529. if (len > max_tracer_type_len)
  530. max_tracer_type_len = len;
  531. }
  532. out:
  533. mutex_unlock(&trace_types_lock);
  534. }
  535. void tracing_reset(struct trace_array_cpu *data)
  536. {
  537. data->trace_idx = 0;
  538. data->overrun = 0;
  539. data->trace_head = data->trace_tail = head_page(data);
  540. data->trace_head_idx = 0;
  541. data->trace_tail_idx = 0;
  542. }
  543. #define SAVED_CMDLINES 128
  544. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  545. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  546. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  547. static int cmdline_idx;
  548. static DEFINE_SPINLOCK(trace_cmdline_lock);
  549. /* trace in all context switches */
  550. atomic_t trace_record_cmdline_enabled __read_mostly;
  551. /* temporary disable recording */
  552. atomic_t trace_record_cmdline_disabled __read_mostly;
  553. static void trace_init_cmdlines(void)
  554. {
  555. memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
  556. memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
  557. cmdline_idx = 0;
  558. }
  559. void trace_stop_cmdline_recording(void);
  560. static void trace_save_cmdline(struct task_struct *tsk)
  561. {
  562. unsigned map;
  563. unsigned idx;
  564. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  565. return;
  566. /*
  567. * It's not the end of the world if we don't get
  568. * the lock, but we also don't want to spin
  569. * nor do we want to disable interrupts,
  570. * so if we miss here, then better luck next time.
  571. */
  572. if (!spin_trylock(&trace_cmdline_lock))
  573. return;
  574. idx = map_pid_to_cmdline[tsk->pid];
  575. if (idx >= SAVED_CMDLINES) {
  576. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  577. map = map_cmdline_to_pid[idx];
  578. if (map <= PID_MAX_DEFAULT)
  579. map_pid_to_cmdline[map] = (unsigned)-1;
  580. map_pid_to_cmdline[tsk->pid] = idx;
  581. cmdline_idx = idx;
  582. }
  583. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  584. spin_unlock(&trace_cmdline_lock);
  585. }
  586. static char *trace_find_cmdline(int pid)
  587. {
  588. char *cmdline = "<...>";
  589. unsigned map;
  590. if (!pid)
  591. return "<idle>";
  592. if (pid > PID_MAX_DEFAULT)
  593. goto out;
  594. map = map_pid_to_cmdline[pid];
  595. if (map >= SAVED_CMDLINES)
  596. goto out;
  597. cmdline = saved_cmdlines[map];
  598. out:
  599. return cmdline;
  600. }
  601. void tracing_record_cmdline(struct task_struct *tsk)
  602. {
  603. if (atomic_read(&trace_record_cmdline_disabled))
  604. return;
  605. trace_save_cmdline(tsk);
  606. }
  607. static inline struct list_head *
  608. trace_next_list(struct trace_array_cpu *data, struct list_head *next)
  609. {
  610. /*
  611. * Roundrobin - but skip the head (which is not a real page):
  612. */
  613. next = next->next;
  614. if (unlikely(next == &data->trace_pages))
  615. next = next->next;
  616. BUG_ON(next == &data->trace_pages);
  617. return next;
  618. }
  619. static inline void *
  620. trace_next_page(struct trace_array_cpu *data, void *addr)
  621. {
  622. struct list_head *next;
  623. struct page *page;
  624. page = virt_to_page(addr);
  625. next = trace_next_list(data, &page->lru);
  626. page = list_entry(next, struct page, lru);
  627. return page_address(page);
  628. }
  629. static inline struct trace_entry *
  630. tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
  631. {
  632. unsigned long idx, idx_next;
  633. struct trace_entry *entry;
  634. data->trace_idx++;
  635. idx = data->trace_head_idx;
  636. idx_next = idx + 1;
  637. BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
  638. entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
  639. if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
  640. data->trace_head = trace_next_page(data, data->trace_head);
  641. idx_next = 0;
  642. }
  643. if (data->trace_head == data->trace_tail &&
  644. idx_next == data->trace_tail_idx) {
  645. /* overrun */
  646. data->overrun++;
  647. data->trace_tail_idx++;
  648. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  649. data->trace_tail =
  650. trace_next_page(data, data->trace_tail);
  651. data->trace_tail_idx = 0;
  652. }
  653. }
  654. data->trace_head_idx = idx_next;
  655. return entry;
  656. }
  657. static inline void
  658. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
  659. {
  660. struct task_struct *tsk = current;
  661. unsigned long pc;
  662. pc = preempt_count();
  663. entry->preempt_count = pc & 0xff;
  664. entry->pid = (tsk) ? tsk->pid : 0;
  665. entry->t = ftrace_now(raw_smp_processor_id());
  666. entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  667. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  668. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  669. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  670. }
  671. void
  672. trace_function(struct trace_array *tr, struct trace_array_cpu *data,
  673. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  674. {
  675. struct trace_entry *entry;
  676. unsigned long irq_flags;
  677. raw_local_irq_save(irq_flags);
  678. __raw_spin_lock(&data->lock);
  679. entry = tracing_get_trace_entry(tr, data);
  680. tracing_generic_entry_update(entry, flags);
  681. entry->type = TRACE_FN;
  682. entry->fn.ip = ip;
  683. entry->fn.parent_ip = parent_ip;
  684. __raw_spin_unlock(&data->lock);
  685. raw_local_irq_restore(irq_flags);
  686. }
  687. void
  688. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  689. unsigned long ip, unsigned long parent_ip, unsigned long flags)
  690. {
  691. if (likely(!atomic_read(&data->disabled)))
  692. trace_function(tr, data, ip, parent_ip, flags);
  693. }
  694. #ifdef CONFIG_MMIOTRACE
  695. void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
  696. struct mmiotrace_rw *rw)
  697. {
  698. struct trace_entry *entry;
  699. unsigned long irq_flags;
  700. raw_local_irq_save(irq_flags);
  701. __raw_spin_lock(&data->lock);
  702. entry = tracing_get_trace_entry(tr, data);
  703. tracing_generic_entry_update(entry, 0);
  704. entry->type = TRACE_MMIO_RW;
  705. entry->mmiorw = *rw;
  706. __raw_spin_unlock(&data->lock);
  707. raw_local_irq_restore(irq_flags);
  708. trace_wake_up();
  709. }
  710. void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
  711. struct mmiotrace_map *map)
  712. {
  713. struct trace_entry *entry;
  714. unsigned long irq_flags;
  715. raw_local_irq_save(irq_flags);
  716. __raw_spin_lock(&data->lock);
  717. entry = tracing_get_trace_entry(tr, data);
  718. tracing_generic_entry_update(entry, 0);
  719. entry->type = TRACE_MMIO_MAP;
  720. entry->mmiomap = *map;
  721. __raw_spin_unlock(&data->lock);
  722. raw_local_irq_restore(irq_flags);
  723. trace_wake_up();
  724. }
  725. #endif
  726. void __trace_stack(struct trace_array *tr,
  727. struct trace_array_cpu *data,
  728. unsigned long flags,
  729. int skip)
  730. {
  731. struct trace_entry *entry;
  732. struct stack_trace trace;
  733. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  734. return;
  735. entry = tracing_get_trace_entry(tr, data);
  736. tracing_generic_entry_update(entry, flags);
  737. entry->type = TRACE_STACK;
  738. memset(&entry->stack, 0, sizeof(entry->stack));
  739. trace.nr_entries = 0;
  740. trace.max_entries = FTRACE_STACK_ENTRIES;
  741. trace.skip = skip;
  742. trace.entries = entry->stack.caller;
  743. save_stack_trace(&trace);
  744. }
  745. void
  746. __trace_special(void *__tr, void *__data,
  747. unsigned long arg1, unsigned long arg2, unsigned long arg3)
  748. {
  749. struct trace_array_cpu *data = __data;
  750. struct trace_array *tr = __tr;
  751. struct trace_entry *entry;
  752. unsigned long irq_flags;
  753. raw_local_irq_save(irq_flags);
  754. __raw_spin_lock(&data->lock);
  755. entry = tracing_get_trace_entry(tr, data);
  756. tracing_generic_entry_update(entry, 0);
  757. entry->type = TRACE_SPECIAL;
  758. entry->special.arg1 = arg1;
  759. entry->special.arg2 = arg2;
  760. entry->special.arg3 = arg3;
  761. __trace_stack(tr, data, irq_flags, 4);
  762. __raw_spin_unlock(&data->lock);
  763. raw_local_irq_restore(irq_flags);
  764. trace_wake_up();
  765. }
  766. void
  767. tracing_sched_switch_trace(struct trace_array *tr,
  768. struct trace_array_cpu *data,
  769. struct task_struct *prev,
  770. struct task_struct *next,
  771. unsigned long flags)
  772. {
  773. struct trace_entry *entry;
  774. unsigned long irq_flags;
  775. raw_local_irq_save(irq_flags);
  776. __raw_spin_lock(&data->lock);
  777. entry = tracing_get_trace_entry(tr, data);
  778. tracing_generic_entry_update(entry, flags);
  779. entry->type = TRACE_CTX;
  780. entry->ctx.prev_pid = prev->pid;
  781. entry->ctx.prev_prio = prev->prio;
  782. entry->ctx.prev_state = prev->state;
  783. entry->ctx.next_pid = next->pid;
  784. entry->ctx.next_prio = next->prio;
  785. entry->ctx.next_state = next->state;
  786. __trace_stack(tr, data, flags, 5);
  787. __raw_spin_unlock(&data->lock);
  788. raw_local_irq_restore(irq_flags);
  789. }
  790. void
  791. tracing_sched_wakeup_trace(struct trace_array *tr,
  792. struct trace_array_cpu *data,
  793. struct task_struct *wakee,
  794. struct task_struct *curr,
  795. unsigned long flags)
  796. {
  797. struct trace_entry *entry;
  798. unsigned long irq_flags;
  799. raw_local_irq_save(irq_flags);
  800. __raw_spin_lock(&data->lock);
  801. entry = tracing_get_trace_entry(tr, data);
  802. tracing_generic_entry_update(entry, flags);
  803. entry->type = TRACE_WAKE;
  804. entry->ctx.prev_pid = curr->pid;
  805. entry->ctx.prev_prio = curr->prio;
  806. entry->ctx.prev_state = curr->state;
  807. entry->ctx.next_pid = wakee->pid;
  808. entry->ctx.next_prio = wakee->prio;
  809. entry->ctx.next_state = wakee->state;
  810. __trace_stack(tr, data, flags, 6);
  811. __raw_spin_unlock(&data->lock);
  812. raw_local_irq_restore(irq_flags);
  813. trace_wake_up();
  814. }
  815. #ifdef CONFIG_FTRACE
  816. static void
  817. function_trace_call(unsigned long ip, unsigned long parent_ip)
  818. {
  819. struct trace_array *tr = &global_trace;
  820. struct trace_array_cpu *data;
  821. unsigned long flags;
  822. long disabled;
  823. int cpu;
  824. if (unlikely(!tracer_enabled))
  825. return;
  826. local_irq_save(flags);
  827. cpu = raw_smp_processor_id();
  828. data = tr->data[cpu];
  829. disabled = atomic_inc_return(&data->disabled);
  830. if (likely(disabled == 1))
  831. trace_function(tr, data, ip, parent_ip, flags);
  832. atomic_dec(&data->disabled);
  833. local_irq_restore(flags);
  834. }
  835. static struct ftrace_ops trace_ops __read_mostly =
  836. {
  837. .func = function_trace_call,
  838. };
  839. void tracing_start_function_trace(void)
  840. {
  841. register_ftrace_function(&trace_ops);
  842. }
  843. void tracing_stop_function_trace(void)
  844. {
  845. unregister_ftrace_function(&trace_ops);
  846. }
  847. #endif
  848. enum trace_file_type {
  849. TRACE_FILE_LAT_FMT = 1,
  850. };
  851. static struct trace_entry *
  852. trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
  853. struct trace_iterator *iter, int cpu)
  854. {
  855. struct page *page;
  856. struct trace_entry *array;
  857. if (iter->next_idx[cpu] >= tr->entries ||
  858. iter->next_idx[cpu] >= data->trace_idx ||
  859. (data->trace_head == data->trace_tail &&
  860. data->trace_head_idx == data->trace_tail_idx))
  861. return NULL;
  862. if (!iter->next_page[cpu]) {
  863. /* Initialize the iterator for this cpu trace buffer */
  864. WARN_ON(!data->trace_tail);
  865. page = virt_to_page(data->trace_tail);
  866. iter->next_page[cpu] = &page->lru;
  867. iter->next_page_idx[cpu] = data->trace_tail_idx;
  868. }
  869. page = list_entry(iter->next_page[cpu], struct page, lru);
  870. BUG_ON(&data->trace_pages == &page->lru);
  871. array = page_address(page);
  872. WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
  873. return &array[iter->next_page_idx[cpu]];
  874. }
  875. static struct trace_entry *
  876. find_next_entry(struct trace_iterator *iter, int *ent_cpu)
  877. {
  878. struct trace_array *tr = iter->tr;
  879. struct trace_entry *ent, *next = NULL;
  880. int next_cpu = -1;
  881. int cpu;
  882. for_each_tracing_cpu(cpu) {
  883. if (!head_page(tr->data[cpu]))
  884. continue;
  885. ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
  886. /*
  887. * Pick the entry with the smallest timestamp:
  888. */
  889. if (ent && (!next || ent->t < next->t)) {
  890. next = ent;
  891. next_cpu = cpu;
  892. }
  893. }
  894. if (ent_cpu)
  895. *ent_cpu = next_cpu;
  896. return next;
  897. }
  898. static void trace_iterator_increment(struct trace_iterator *iter)
  899. {
  900. iter->idx++;
  901. iter->next_idx[iter->cpu]++;
  902. iter->next_page_idx[iter->cpu]++;
  903. if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
  904. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  905. iter->next_page_idx[iter->cpu] = 0;
  906. iter->next_page[iter->cpu] =
  907. trace_next_list(data, iter->next_page[iter->cpu]);
  908. }
  909. }
  910. static void trace_consume(struct trace_iterator *iter)
  911. {
  912. struct trace_array_cpu *data = iter->tr->data[iter->cpu];
  913. data->trace_tail_idx++;
  914. if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
  915. data->trace_tail = trace_next_page(data, data->trace_tail);
  916. data->trace_tail_idx = 0;
  917. }
  918. /* Check if we empty it, then reset the index */
  919. if (data->trace_head == data->trace_tail &&
  920. data->trace_head_idx == data->trace_tail_idx)
  921. data->trace_idx = 0;
  922. }
  923. static void *find_next_entry_inc(struct trace_iterator *iter)
  924. {
  925. struct trace_entry *next;
  926. int next_cpu = -1;
  927. next = find_next_entry(iter, &next_cpu);
  928. iter->prev_ent = iter->ent;
  929. iter->prev_cpu = iter->cpu;
  930. iter->ent = next;
  931. iter->cpu = next_cpu;
  932. if (next)
  933. trace_iterator_increment(iter);
  934. return next ? iter : NULL;
  935. }
  936. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  937. {
  938. struct trace_iterator *iter = m->private;
  939. void *last_ent = iter->ent;
  940. int i = (int)*pos;
  941. void *ent;
  942. (*pos)++;
  943. /* can't go backwards */
  944. if (iter->idx > i)
  945. return NULL;
  946. if (iter->idx < 0)
  947. ent = find_next_entry_inc(iter);
  948. else
  949. ent = iter;
  950. while (ent && iter->idx < i)
  951. ent = find_next_entry_inc(iter);
  952. iter->pos = *pos;
  953. if (last_ent && !ent)
  954. seq_puts(m, "\n\nvim:ft=help\n");
  955. return ent;
  956. }
  957. static void *s_start(struct seq_file *m, loff_t *pos)
  958. {
  959. struct trace_iterator *iter = m->private;
  960. void *p = NULL;
  961. loff_t l = 0;
  962. int i;
  963. mutex_lock(&trace_types_lock);
  964. if (!current_trace || current_trace != iter->trace) {
  965. mutex_unlock(&trace_types_lock);
  966. return NULL;
  967. }
  968. atomic_inc(&trace_record_cmdline_disabled);
  969. /* let the tracer grab locks here if needed */
  970. if (current_trace->start)
  971. current_trace->start(iter);
  972. if (*pos != iter->pos) {
  973. iter->ent = NULL;
  974. iter->cpu = 0;
  975. iter->idx = -1;
  976. iter->prev_ent = NULL;
  977. iter->prev_cpu = -1;
  978. for_each_tracing_cpu(i) {
  979. iter->next_idx[i] = 0;
  980. iter->next_page[i] = NULL;
  981. }
  982. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  983. ;
  984. } else {
  985. l = *pos - 1;
  986. p = s_next(m, p, &l);
  987. }
  988. return p;
  989. }
  990. static void s_stop(struct seq_file *m, void *p)
  991. {
  992. struct trace_iterator *iter = m->private;
  993. atomic_dec(&trace_record_cmdline_disabled);
  994. /* let the tracer release locks here if needed */
  995. if (current_trace && current_trace == iter->trace && iter->trace->stop)
  996. iter->trace->stop(iter);
  997. mutex_unlock(&trace_types_lock);
  998. }
  999. static int
  1000. seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
  1001. {
  1002. #ifdef CONFIG_KALLSYMS
  1003. char str[KSYM_SYMBOL_LEN];
  1004. kallsyms_lookup(address, NULL, NULL, NULL, str);
  1005. return trace_seq_printf(s, fmt, str);
  1006. #endif
  1007. return 1;
  1008. }
  1009. static int
  1010. seq_print_sym_offset(struct trace_seq *s, const char *fmt,
  1011. unsigned long address)
  1012. {
  1013. #ifdef CONFIG_KALLSYMS
  1014. char str[KSYM_SYMBOL_LEN];
  1015. sprint_symbol(str, address);
  1016. return trace_seq_printf(s, fmt, str);
  1017. #endif
  1018. return 1;
  1019. }
  1020. #ifndef CONFIG_64BIT
  1021. # define IP_FMT "%08lx"
  1022. #else
  1023. # define IP_FMT "%016lx"
  1024. #endif
  1025. static int
  1026. seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
  1027. {
  1028. int ret;
  1029. if (!ip)
  1030. return trace_seq_printf(s, "0");
  1031. if (sym_flags & TRACE_ITER_SYM_OFFSET)
  1032. ret = seq_print_sym_offset(s, "%s", ip);
  1033. else
  1034. ret = seq_print_sym_short(s, "%s", ip);
  1035. if (!ret)
  1036. return 0;
  1037. if (sym_flags & TRACE_ITER_SYM_ADDR)
  1038. ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
  1039. return ret;
  1040. }
  1041. static void print_lat_help_header(struct seq_file *m)
  1042. {
  1043. seq_puts(m, "# _------=> CPU# \n");
  1044. seq_puts(m, "# / _-----=> irqs-off \n");
  1045. seq_puts(m, "# | / _----=> need-resched \n");
  1046. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1047. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1048. seq_puts(m, "# |||| / \n");
  1049. seq_puts(m, "# ||||| delay \n");
  1050. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1051. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1052. }
  1053. static void print_func_help_header(struct seq_file *m)
  1054. {
  1055. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1056. seq_puts(m, "# | | | | |\n");
  1057. }
  1058. static void
  1059. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1060. {
  1061. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1062. struct trace_array *tr = iter->tr;
  1063. struct trace_array_cpu *data = tr->data[tr->cpu];
  1064. struct tracer *type = current_trace;
  1065. unsigned long total = 0;
  1066. unsigned long entries = 0;
  1067. int cpu;
  1068. const char *name = "preemption";
  1069. if (type)
  1070. name = type->name;
  1071. for_each_tracing_cpu(cpu) {
  1072. if (head_page(tr->data[cpu])) {
  1073. total += tr->data[cpu]->trace_idx;
  1074. if (tr->data[cpu]->trace_idx > tr->entries)
  1075. entries += tr->entries;
  1076. else
  1077. entries += tr->data[cpu]->trace_idx;
  1078. }
  1079. }
  1080. seq_printf(m, "%s latency trace v1.1.5 on %s\n",
  1081. name, UTS_RELEASE);
  1082. seq_puts(m, "-----------------------------------"
  1083. "---------------------------------\n");
  1084. seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
  1085. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1086. nsecs_to_usecs(data->saved_latency),
  1087. entries,
  1088. total,
  1089. tr->cpu,
  1090. #if defined(CONFIG_PREEMPT_NONE)
  1091. "server",
  1092. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1093. "desktop",
  1094. #elif defined(CONFIG_PREEMPT_DESKTOP)
  1095. "preempt",
  1096. #else
  1097. "unknown",
  1098. #endif
  1099. /* These are reserved for later use */
  1100. 0, 0, 0, 0);
  1101. #ifdef CONFIG_SMP
  1102. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1103. #else
  1104. seq_puts(m, ")\n");
  1105. #endif
  1106. seq_puts(m, " -----------------\n");
  1107. seq_printf(m, " | task: %.16s-%d "
  1108. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1109. data->comm, data->pid, data->uid, data->nice,
  1110. data->policy, data->rt_priority);
  1111. seq_puts(m, " -----------------\n");
  1112. if (data->critical_start) {
  1113. seq_puts(m, " => started at: ");
  1114. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1115. trace_print_seq(m, &iter->seq);
  1116. seq_puts(m, "\n => ended at: ");
  1117. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1118. trace_print_seq(m, &iter->seq);
  1119. seq_puts(m, "\n");
  1120. }
  1121. seq_puts(m, "\n");
  1122. }
  1123. static void
  1124. lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
  1125. {
  1126. int hardirq, softirq;
  1127. char *comm;
  1128. comm = trace_find_cmdline(entry->pid);
  1129. trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
  1130. trace_seq_printf(s, "%d", cpu);
  1131. trace_seq_printf(s, "%c%c",
  1132. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
  1133. ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
  1134. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  1135. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  1136. if (hardirq && softirq) {
  1137. trace_seq_putc(s, 'H');
  1138. } else {
  1139. if (hardirq) {
  1140. trace_seq_putc(s, 'h');
  1141. } else {
  1142. if (softirq)
  1143. trace_seq_putc(s, 's');
  1144. else
  1145. trace_seq_putc(s, '.');
  1146. }
  1147. }
  1148. if (entry->preempt_count)
  1149. trace_seq_printf(s, "%x", entry->preempt_count);
  1150. else
  1151. trace_seq_puts(s, ".");
  1152. }
  1153. unsigned long preempt_mark_thresh = 100;
  1154. static void
  1155. lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
  1156. unsigned long rel_usecs)
  1157. {
  1158. trace_seq_printf(s, " %4lldus", abs_usecs);
  1159. if (rel_usecs > preempt_mark_thresh)
  1160. trace_seq_puts(s, "!: ");
  1161. else if (rel_usecs > 1)
  1162. trace_seq_puts(s, "+: ");
  1163. else
  1164. trace_seq_puts(s, " : ");
  1165. }
  1166. static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
  1167. static int
  1168. print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
  1169. {
  1170. struct trace_seq *s = &iter->seq;
  1171. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1172. struct trace_entry *next_entry = find_next_entry(iter, NULL);
  1173. unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
  1174. struct trace_entry *entry = iter->ent;
  1175. unsigned long abs_usecs;
  1176. unsigned long rel_usecs;
  1177. char *comm;
  1178. int S, T;
  1179. int i;
  1180. unsigned state;
  1181. if (!next_entry)
  1182. next_entry = entry;
  1183. rel_usecs = ns2usecs(next_entry->t - entry->t);
  1184. abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
  1185. if (verbose) {
  1186. comm = trace_find_cmdline(entry->pid);
  1187. trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
  1188. " %ld.%03ldms (+%ld.%03ldms): ",
  1189. comm,
  1190. entry->pid, cpu, entry->flags,
  1191. entry->preempt_count, trace_idx,
  1192. ns2usecs(entry->t),
  1193. abs_usecs/1000,
  1194. abs_usecs % 1000, rel_usecs/1000,
  1195. rel_usecs % 1000);
  1196. } else {
  1197. lat_print_generic(s, entry, cpu);
  1198. lat_print_timestamp(s, abs_usecs, rel_usecs);
  1199. }
  1200. switch (entry->type) {
  1201. case TRACE_FN:
  1202. seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1203. trace_seq_puts(s, " (");
  1204. seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
  1205. trace_seq_puts(s, ")\n");
  1206. break;
  1207. case TRACE_CTX:
  1208. case TRACE_WAKE:
  1209. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1210. state_to_char[entry->ctx.next_state] : 'X';
  1211. state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
  1212. S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
  1213. comm = trace_find_cmdline(entry->ctx.next_pid);
  1214. trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
  1215. entry->ctx.prev_pid,
  1216. entry->ctx.prev_prio,
  1217. S, entry->type == TRACE_CTX ? "==>" : " +",
  1218. entry->ctx.next_pid,
  1219. entry->ctx.next_prio,
  1220. T, comm);
  1221. break;
  1222. case TRACE_SPECIAL:
  1223. trace_seq_printf(s, "# %ld %ld %ld\n",
  1224. entry->special.arg1,
  1225. entry->special.arg2,
  1226. entry->special.arg3);
  1227. break;
  1228. case TRACE_STACK:
  1229. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1230. if (i)
  1231. trace_seq_puts(s, " <= ");
  1232. seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
  1233. }
  1234. trace_seq_puts(s, "\n");
  1235. break;
  1236. default:
  1237. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  1238. }
  1239. return 1;
  1240. }
  1241. static int print_trace_fmt(struct trace_iterator *iter)
  1242. {
  1243. struct trace_seq *s = &iter->seq;
  1244. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1245. struct trace_entry *entry;
  1246. unsigned long usec_rem;
  1247. unsigned long long t;
  1248. unsigned long secs;
  1249. char *comm;
  1250. int ret;
  1251. int S, T;
  1252. int i;
  1253. entry = iter->ent;
  1254. comm = trace_find_cmdline(iter->ent->pid);
  1255. t = ns2usecs(entry->t);
  1256. usec_rem = do_div(t, 1000000ULL);
  1257. secs = (unsigned long)t;
  1258. ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
  1259. if (!ret)
  1260. return 0;
  1261. ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
  1262. if (!ret)
  1263. return 0;
  1264. ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
  1265. if (!ret)
  1266. return 0;
  1267. switch (entry->type) {
  1268. case TRACE_FN:
  1269. ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
  1270. if (!ret)
  1271. return 0;
  1272. if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
  1273. entry->fn.parent_ip) {
  1274. ret = trace_seq_printf(s, " <-");
  1275. if (!ret)
  1276. return 0;
  1277. ret = seq_print_ip_sym(s, entry->fn.parent_ip,
  1278. sym_flags);
  1279. if (!ret)
  1280. return 0;
  1281. }
  1282. ret = trace_seq_printf(s, "\n");
  1283. if (!ret)
  1284. return 0;
  1285. break;
  1286. case TRACE_CTX:
  1287. case TRACE_WAKE:
  1288. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1289. state_to_char[entry->ctx.prev_state] : 'X';
  1290. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1291. state_to_char[entry->ctx.next_state] : 'X';
  1292. ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
  1293. entry->ctx.prev_pid,
  1294. entry->ctx.prev_prio,
  1295. S,
  1296. entry->type == TRACE_CTX ? "==>" : " +",
  1297. entry->ctx.next_pid,
  1298. entry->ctx.next_prio,
  1299. T);
  1300. if (!ret)
  1301. return 0;
  1302. break;
  1303. case TRACE_SPECIAL:
  1304. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1305. entry->special.arg1,
  1306. entry->special.arg2,
  1307. entry->special.arg3);
  1308. if (!ret)
  1309. return 0;
  1310. break;
  1311. case TRACE_STACK:
  1312. for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
  1313. if (i) {
  1314. ret = trace_seq_puts(s, " <= ");
  1315. if (!ret)
  1316. return 0;
  1317. }
  1318. ret = seq_print_ip_sym(s, entry->stack.caller[i],
  1319. sym_flags);
  1320. if (!ret)
  1321. return 0;
  1322. }
  1323. ret = trace_seq_puts(s, "\n");
  1324. if (!ret)
  1325. return 0;
  1326. break;
  1327. }
  1328. return 1;
  1329. }
  1330. static int print_raw_fmt(struct trace_iterator *iter)
  1331. {
  1332. struct trace_seq *s = &iter->seq;
  1333. struct trace_entry *entry;
  1334. int ret;
  1335. int S, T;
  1336. entry = iter->ent;
  1337. ret = trace_seq_printf(s, "%d %d %llu ",
  1338. entry->pid, iter->cpu, entry->t);
  1339. if (!ret)
  1340. return 0;
  1341. switch (entry->type) {
  1342. case TRACE_FN:
  1343. ret = trace_seq_printf(s, "%x %x\n",
  1344. entry->fn.ip, entry->fn.parent_ip);
  1345. if (!ret)
  1346. return 0;
  1347. break;
  1348. case TRACE_CTX:
  1349. case TRACE_WAKE:
  1350. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1351. state_to_char[entry->ctx.prev_state] : 'X';
  1352. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1353. state_to_char[entry->ctx.next_state] : 'X';
  1354. if (entry->type == TRACE_WAKE)
  1355. S = '+';
  1356. ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
  1357. entry->ctx.prev_pid,
  1358. entry->ctx.prev_prio,
  1359. S,
  1360. entry->ctx.next_pid,
  1361. entry->ctx.next_prio,
  1362. T);
  1363. if (!ret)
  1364. return 0;
  1365. break;
  1366. case TRACE_SPECIAL:
  1367. case TRACE_STACK:
  1368. ret = trace_seq_printf(s, "# %ld %ld %ld\n",
  1369. entry->special.arg1,
  1370. entry->special.arg2,
  1371. entry->special.arg3);
  1372. if (!ret)
  1373. return 0;
  1374. break;
  1375. }
  1376. return 1;
  1377. }
  1378. #define SEQ_PUT_FIELD_RET(s, x) \
  1379. do { \
  1380. if (!trace_seq_putmem(s, &(x), sizeof(x))) \
  1381. return 0; \
  1382. } while (0)
  1383. #define SEQ_PUT_HEX_FIELD_RET(s, x) \
  1384. do { \
  1385. if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
  1386. return 0; \
  1387. } while (0)
  1388. static int print_hex_fmt(struct trace_iterator *iter)
  1389. {
  1390. struct trace_seq *s = &iter->seq;
  1391. unsigned char newline = '\n';
  1392. struct trace_entry *entry;
  1393. int S, T;
  1394. entry = iter->ent;
  1395. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1396. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1397. SEQ_PUT_HEX_FIELD_RET(s, entry->t);
  1398. switch (entry->type) {
  1399. case TRACE_FN:
  1400. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
  1401. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1402. break;
  1403. case TRACE_CTX:
  1404. case TRACE_WAKE:
  1405. S = entry->ctx.prev_state < sizeof(state_to_char) ?
  1406. state_to_char[entry->ctx.prev_state] : 'X';
  1407. T = entry->ctx.next_state < sizeof(state_to_char) ?
  1408. state_to_char[entry->ctx.next_state] : 'X';
  1409. if (entry->type == TRACE_WAKE)
  1410. S = '+';
  1411. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
  1412. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
  1413. SEQ_PUT_HEX_FIELD_RET(s, S);
  1414. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
  1415. SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
  1416. SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
  1417. SEQ_PUT_HEX_FIELD_RET(s, T);
  1418. break;
  1419. case TRACE_SPECIAL:
  1420. case TRACE_STACK:
  1421. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
  1422. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
  1423. SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
  1424. break;
  1425. }
  1426. SEQ_PUT_FIELD_RET(s, newline);
  1427. return 1;
  1428. }
  1429. static int print_bin_fmt(struct trace_iterator *iter)
  1430. {
  1431. struct trace_seq *s = &iter->seq;
  1432. struct trace_entry *entry;
  1433. entry = iter->ent;
  1434. SEQ_PUT_FIELD_RET(s, entry->pid);
  1435. SEQ_PUT_FIELD_RET(s, entry->cpu);
  1436. SEQ_PUT_FIELD_RET(s, entry->t);
  1437. switch (entry->type) {
  1438. case TRACE_FN:
  1439. SEQ_PUT_FIELD_RET(s, entry->fn.ip);
  1440. SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
  1441. break;
  1442. case TRACE_CTX:
  1443. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
  1444. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
  1445. SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
  1446. SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
  1447. SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
  1448. SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
  1449. break;
  1450. case TRACE_SPECIAL:
  1451. case TRACE_STACK:
  1452. SEQ_PUT_FIELD_RET(s, entry->special.arg1);
  1453. SEQ_PUT_FIELD_RET(s, entry->special.arg2);
  1454. SEQ_PUT_FIELD_RET(s, entry->special.arg3);
  1455. break;
  1456. }
  1457. return 1;
  1458. }
  1459. static int trace_empty(struct trace_iterator *iter)
  1460. {
  1461. struct trace_array_cpu *data;
  1462. int cpu;
  1463. for_each_tracing_cpu(cpu) {
  1464. data = iter->tr->data[cpu];
  1465. if (head_page(data) && data->trace_idx &&
  1466. (data->trace_tail != data->trace_head ||
  1467. data->trace_tail_idx != data->trace_head_idx))
  1468. return 0;
  1469. }
  1470. return 1;
  1471. }
  1472. static int print_trace_line(struct trace_iterator *iter)
  1473. {
  1474. if (iter->trace && iter->trace->print_line)
  1475. return iter->trace->print_line(iter);
  1476. if (trace_flags & TRACE_ITER_BIN)
  1477. return print_bin_fmt(iter);
  1478. if (trace_flags & TRACE_ITER_HEX)
  1479. return print_hex_fmt(iter);
  1480. if (trace_flags & TRACE_ITER_RAW)
  1481. return print_raw_fmt(iter);
  1482. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1483. return print_lat_fmt(iter, iter->idx, iter->cpu);
  1484. return print_trace_fmt(iter);
  1485. }
  1486. static int s_show(struct seq_file *m, void *v)
  1487. {
  1488. struct trace_iterator *iter = v;
  1489. if (iter->ent == NULL) {
  1490. if (iter->tr) {
  1491. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1492. seq_puts(m, "#\n");
  1493. }
  1494. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1495. /* print nothing if the buffers are empty */
  1496. if (trace_empty(iter))
  1497. return 0;
  1498. print_trace_header(m, iter);
  1499. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1500. print_lat_help_header(m);
  1501. } else {
  1502. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1503. print_func_help_header(m);
  1504. }
  1505. } else {
  1506. print_trace_line(iter);
  1507. trace_print_seq(m, &iter->seq);
  1508. }
  1509. return 0;
  1510. }
  1511. static struct seq_operations tracer_seq_ops = {
  1512. .start = s_start,
  1513. .next = s_next,
  1514. .stop = s_stop,
  1515. .show = s_show,
  1516. };
  1517. static struct trace_iterator *
  1518. __tracing_open(struct inode *inode, struct file *file, int *ret)
  1519. {
  1520. struct trace_iterator *iter;
  1521. if (tracing_disabled) {
  1522. *ret = -ENODEV;
  1523. return NULL;
  1524. }
  1525. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1526. if (!iter) {
  1527. *ret = -ENOMEM;
  1528. goto out;
  1529. }
  1530. mutex_lock(&trace_types_lock);
  1531. if (current_trace && current_trace->print_max)
  1532. iter->tr = &max_tr;
  1533. else
  1534. iter->tr = inode->i_private;
  1535. iter->trace = current_trace;
  1536. iter->pos = -1;
  1537. /* TODO stop tracer */
  1538. *ret = seq_open(file, &tracer_seq_ops);
  1539. if (!*ret) {
  1540. struct seq_file *m = file->private_data;
  1541. m->private = iter;
  1542. /* stop the trace while dumping */
  1543. if (iter->tr->ctrl)
  1544. tracer_enabled = 0;
  1545. if (iter->trace && iter->trace->open)
  1546. iter->trace->open(iter);
  1547. } else {
  1548. kfree(iter);
  1549. iter = NULL;
  1550. }
  1551. mutex_unlock(&trace_types_lock);
  1552. out:
  1553. return iter;
  1554. }
  1555. int tracing_open_generic(struct inode *inode, struct file *filp)
  1556. {
  1557. if (tracing_disabled)
  1558. return -ENODEV;
  1559. filp->private_data = inode->i_private;
  1560. return 0;
  1561. }
  1562. int tracing_release(struct inode *inode, struct file *file)
  1563. {
  1564. struct seq_file *m = (struct seq_file *)file->private_data;
  1565. struct trace_iterator *iter = m->private;
  1566. mutex_lock(&trace_types_lock);
  1567. if (iter->trace && iter->trace->close)
  1568. iter->trace->close(iter);
  1569. /* reenable tracing if it was previously enabled */
  1570. if (iter->tr->ctrl)
  1571. tracer_enabled = 1;
  1572. mutex_unlock(&trace_types_lock);
  1573. seq_release(inode, file);
  1574. kfree(iter);
  1575. return 0;
  1576. }
  1577. static int tracing_open(struct inode *inode, struct file *file)
  1578. {
  1579. int ret;
  1580. __tracing_open(inode, file, &ret);
  1581. return ret;
  1582. }
  1583. static int tracing_lt_open(struct inode *inode, struct file *file)
  1584. {
  1585. struct trace_iterator *iter;
  1586. int ret;
  1587. iter = __tracing_open(inode, file, &ret);
  1588. if (!ret)
  1589. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1590. return ret;
  1591. }
  1592. static void *
  1593. t_next(struct seq_file *m, void *v, loff_t *pos)
  1594. {
  1595. struct tracer *t = m->private;
  1596. (*pos)++;
  1597. if (t)
  1598. t = t->next;
  1599. m->private = t;
  1600. return t;
  1601. }
  1602. static void *t_start(struct seq_file *m, loff_t *pos)
  1603. {
  1604. struct tracer *t = m->private;
  1605. loff_t l = 0;
  1606. mutex_lock(&trace_types_lock);
  1607. for (; t && l < *pos; t = t_next(m, t, &l))
  1608. ;
  1609. return t;
  1610. }
  1611. static void t_stop(struct seq_file *m, void *p)
  1612. {
  1613. mutex_unlock(&trace_types_lock);
  1614. }
  1615. static int t_show(struct seq_file *m, void *v)
  1616. {
  1617. struct tracer *t = v;
  1618. if (!t)
  1619. return 0;
  1620. seq_printf(m, "%s", t->name);
  1621. if (t->next)
  1622. seq_putc(m, ' ');
  1623. else
  1624. seq_putc(m, '\n');
  1625. return 0;
  1626. }
  1627. static struct seq_operations show_traces_seq_ops = {
  1628. .start = t_start,
  1629. .next = t_next,
  1630. .stop = t_stop,
  1631. .show = t_show,
  1632. };
  1633. static int show_traces_open(struct inode *inode, struct file *file)
  1634. {
  1635. int ret;
  1636. if (tracing_disabled)
  1637. return -ENODEV;
  1638. ret = seq_open(file, &show_traces_seq_ops);
  1639. if (!ret) {
  1640. struct seq_file *m = file->private_data;
  1641. m->private = trace_types;
  1642. }
  1643. return ret;
  1644. }
  1645. static struct file_operations tracing_fops = {
  1646. .open = tracing_open,
  1647. .read = seq_read,
  1648. .llseek = seq_lseek,
  1649. .release = tracing_release,
  1650. };
  1651. static struct file_operations tracing_lt_fops = {
  1652. .open = tracing_lt_open,
  1653. .read = seq_read,
  1654. .llseek = seq_lseek,
  1655. .release = tracing_release,
  1656. };
  1657. static struct file_operations show_traces_fops = {
  1658. .open = show_traces_open,
  1659. .read = seq_read,
  1660. .release = seq_release,
  1661. };
  1662. /*
  1663. * Only trace on a CPU if the bitmask is set:
  1664. */
  1665. static cpumask_t tracing_cpumask = CPU_MASK_ALL;
  1666. /*
  1667. * When tracing/tracing_cpu_mask is modified then this holds
  1668. * the new bitmask we are about to install:
  1669. */
  1670. static cpumask_t tracing_cpumask_new;
  1671. /*
  1672. * The tracer itself will not take this lock, but still we want
  1673. * to provide a consistent cpumask to user-space:
  1674. */
  1675. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1676. /*
  1677. * Temporary storage for the character representation of the
  1678. * CPU bitmask (and one more byte for the newline):
  1679. */
  1680. static char mask_str[NR_CPUS + 1];
  1681. static ssize_t
  1682. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1683. size_t count, loff_t *ppos)
  1684. {
  1685. int len;
  1686. mutex_lock(&tracing_cpumask_update_lock);
  1687. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1688. if (count - len < 2) {
  1689. count = -EINVAL;
  1690. goto out_err;
  1691. }
  1692. len += sprintf(mask_str + len, "\n");
  1693. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1694. out_err:
  1695. mutex_unlock(&tracing_cpumask_update_lock);
  1696. return count;
  1697. }
  1698. static ssize_t
  1699. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1700. size_t count, loff_t *ppos)
  1701. {
  1702. int err, cpu;
  1703. mutex_lock(&tracing_cpumask_update_lock);
  1704. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1705. if (err)
  1706. goto err_unlock;
  1707. raw_local_irq_disable();
  1708. __raw_spin_lock(&ftrace_max_lock);
  1709. for_each_tracing_cpu(cpu) {
  1710. /*
  1711. * Increase/decrease the disabled counter if we are
  1712. * about to flip a bit in the cpumask:
  1713. */
  1714. if (cpu_isset(cpu, tracing_cpumask) &&
  1715. !cpu_isset(cpu, tracing_cpumask_new)) {
  1716. atomic_inc(&global_trace.data[cpu]->disabled);
  1717. }
  1718. if (!cpu_isset(cpu, tracing_cpumask) &&
  1719. cpu_isset(cpu, tracing_cpumask_new)) {
  1720. atomic_dec(&global_trace.data[cpu]->disabled);
  1721. }
  1722. }
  1723. __raw_spin_unlock(&ftrace_max_lock);
  1724. raw_local_irq_enable();
  1725. tracing_cpumask = tracing_cpumask_new;
  1726. mutex_unlock(&tracing_cpumask_update_lock);
  1727. return count;
  1728. err_unlock:
  1729. mutex_unlock(&tracing_cpumask_update_lock);
  1730. return err;
  1731. }
  1732. static struct file_operations tracing_cpumask_fops = {
  1733. .open = tracing_open_generic,
  1734. .read = tracing_cpumask_read,
  1735. .write = tracing_cpumask_write,
  1736. };
  1737. static ssize_t
  1738. tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
  1739. size_t cnt, loff_t *ppos)
  1740. {
  1741. char *buf;
  1742. int r = 0;
  1743. int len = 0;
  1744. int i;
  1745. /* calulate max size */
  1746. for (i = 0; trace_options[i]; i++) {
  1747. len += strlen(trace_options[i]);
  1748. len += 3; /* "no" and space */
  1749. }
  1750. /* +2 for \n and \0 */
  1751. buf = kmalloc(len + 2, GFP_KERNEL);
  1752. if (!buf)
  1753. return -ENOMEM;
  1754. for (i = 0; trace_options[i]; i++) {
  1755. if (trace_flags & (1 << i))
  1756. r += sprintf(buf + r, "%s ", trace_options[i]);
  1757. else
  1758. r += sprintf(buf + r, "no%s ", trace_options[i]);
  1759. }
  1760. r += sprintf(buf + r, "\n");
  1761. WARN_ON(r >= len + 2);
  1762. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1763. kfree(buf);
  1764. return r;
  1765. }
  1766. static ssize_t
  1767. tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
  1768. size_t cnt, loff_t *ppos)
  1769. {
  1770. char buf[64];
  1771. char *cmp = buf;
  1772. int neg = 0;
  1773. int i;
  1774. if (cnt >= sizeof(buf))
  1775. return -EINVAL;
  1776. if (copy_from_user(&buf, ubuf, cnt))
  1777. return -EFAULT;
  1778. buf[cnt] = 0;
  1779. if (strncmp(buf, "no", 2) == 0) {
  1780. neg = 1;
  1781. cmp += 2;
  1782. }
  1783. for (i = 0; trace_options[i]; i++) {
  1784. int len = strlen(trace_options[i]);
  1785. if (strncmp(cmp, trace_options[i], len) == 0) {
  1786. if (neg)
  1787. trace_flags &= ~(1 << i);
  1788. else
  1789. trace_flags |= (1 << i);
  1790. break;
  1791. }
  1792. }
  1793. /*
  1794. * If no option could be set, return an error:
  1795. */
  1796. if (!trace_options[i])
  1797. return -EINVAL;
  1798. filp->f_pos += cnt;
  1799. return cnt;
  1800. }
  1801. static struct file_operations tracing_iter_fops = {
  1802. .open = tracing_open_generic,
  1803. .read = tracing_iter_ctrl_read,
  1804. .write = tracing_iter_ctrl_write,
  1805. };
  1806. static const char readme_msg[] =
  1807. "tracing mini-HOWTO:\n\n"
  1808. "# mkdir /debug\n"
  1809. "# mount -t debugfs nodev /debug\n\n"
  1810. "# cat /debug/tracing/available_tracers\n"
  1811. "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
  1812. "# cat /debug/tracing/current_tracer\n"
  1813. "none\n"
  1814. "# echo sched_switch > /debug/tracing/current_tracer\n"
  1815. "# cat /debug/tracing/current_tracer\n"
  1816. "sched_switch\n"
  1817. "# cat /debug/tracing/iter_ctrl\n"
  1818. "noprint-parent nosym-offset nosym-addr noverbose\n"
  1819. "# echo print-parent > /debug/tracing/iter_ctrl\n"
  1820. "# echo 1 > /debug/tracing/tracing_enabled\n"
  1821. "# cat /debug/tracing/trace > /tmp/trace.txt\n"
  1822. "echo 0 > /debug/tracing/tracing_enabled\n"
  1823. ;
  1824. static ssize_t
  1825. tracing_readme_read(struct file *filp, char __user *ubuf,
  1826. size_t cnt, loff_t *ppos)
  1827. {
  1828. return simple_read_from_buffer(ubuf, cnt, ppos,
  1829. readme_msg, strlen(readme_msg));
  1830. }
  1831. static struct file_operations tracing_readme_fops = {
  1832. .open = tracing_open_generic,
  1833. .read = tracing_readme_read,
  1834. };
  1835. static ssize_t
  1836. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  1837. size_t cnt, loff_t *ppos)
  1838. {
  1839. struct trace_array *tr = filp->private_data;
  1840. char buf[64];
  1841. int r;
  1842. r = sprintf(buf, "%ld\n", tr->ctrl);
  1843. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1844. }
  1845. static ssize_t
  1846. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  1847. size_t cnt, loff_t *ppos)
  1848. {
  1849. struct trace_array *tr = filp->private_data;
  1850. char buf[64];
  1851. long val;
  1852. int ret;
  1853. if (cnt >= sizeof(buf))
  1854. return -EINVAL;
  1855. if (copy_from_user(&buf, ubuf, cnt))
  1856. return -EFAULT;
  1857. buf[cnt] = 0;
  1858. ret = strict_strtoul(buf, 10, &val);
  1859. if (ret < 0)
  1860. return ret;
  1861. val = !!val;
  1862. mutex_lock(&trace_types_lock);
  1863. if (tr->ctrl ^ val) {
  1864. if (val)
  1865. tracer_enabled = 1;
  1866. else
  1867. tracer_enabled = 0;
  1868. tr->ctrl = val;
  1869. if (current_trace && current_trace->ctrl_update)
  1870. current_trace->ctrl_update(tr);
  1871. }
  1872. mutex_unlock(&trace_types_lock);
  1873. filp->f_pos += cnt;
  1874. return cnt;
  1875. }
  1876. static ssize_t
  1877. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  1878. size_t cnt, loff_t *ppos)
  1879. {
  1880. char buf[max_tracer_type_len+2];
  1881. int r;
  1882. mutex_lock(&trace_types_lock);
  1883. if (current_trace)
  1884. r = sprintf(buf, "%s\n", current_trace->name);
  1885. else
  1886. r = sprintf(buf, "\n");
  1887. mutex_unlock(&trace_types_lock);
  1888. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1889. }
  1890. static ssize_t
  1891. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  1892. size_t cnt, loff_t *ppos)
  1893. {
  1894. struct trace_array *tr = &global_trace;
  1895. struct tracer *t;
  1896. char buf[max_tracer_type_len+1];
  1897. int i;
  1898. if (cnt > max_tracer_type_len)
  1899. cnt = max_tracer_type_len;
  1900. if (copy_from_user(&buf, ubuf, cnt))
  1901. return -EFAULT;
  1902. buf[cnt] = 0;
  1903. /* strip ending whitespace. */
  1904. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  1905. buf[i] = 0;
  1906. mutex_lock(&trace_types_lock);
  1907. for (t = trace_types; t; t = t->next) {
  1908. if (strcmp(t->name, buf) == 0)
  1909. break;
  1910. }
  1911. if (!t || t == current_trace)
  1912. goto out;
  1913. if (current_trace && current_trace->reset)
  1914. current_trace->reset(tr);
  1915. current_trace = t;
  1916. if (t->init)
  1917. t->init(tr);
  1918. out:
  1919. mutex_unlock(&trace_types_lock);
  1920. filp->f_pos += cnt;
  1921. return cnt;
  1922. }
  1923. static ssize_t
  1924. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  1925. size_t cnt, loff_t *ppos)
  1926. {
  1927. unsigned long *ptr = filp->private_data;
  1928. char buf[64];
  1929. int r;
  1930. r = snprintf(buf, sizeof(buf), "%ld\n",
  1931. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  1932. if (r > sizeof(buf))
  1933. r = sizeof(buf);
  1934. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  1935. }
  1936. static ssize_t
  1937. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  1938. size_t cnt, loff_t *ppos)
  1939. {
  1940. long *ptr = filp->private_data;
  1941. char buf[64];
  1942. long val;
  1943. int ret;
  1944. if (cnt >= sizeof(buf))
  1945. return -EINVAL;
  1946. if (copy_from_user(&buf, ubuf, cnt))
  1947. return -EFAULT;
  1948. buf[cnt] = 0;
  1949. ret = strict_strtoul(buf, 10, &val);
  1950. if (ret < 0)
  1951. return ret;
  1952. *ptr = val * 1000;
  1953. return cnt;
  1954. }
  1955. static atomic_t tracing_reader;
  1956. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  1957. {
  1958. struct trace_iterator *iter;
  1959. if (tracing_disabled)
  1960. return -ENODEV;
  1961. /* We only allow for reader of the pipe */
  1962. if (atomic_inc_return(&tracing_reader) != 1) {
  1963. atomic_dec(&tracing_reader);
  1964. return -EBUSY;
  1965. }
  1966. /* create a buffer to store the information to pass to userspace */
  1967. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1968. if (!iter)
  1969. return -ENOMEM;
  1970. mutex_lock(&trace_types_lock);
  1971. iter->tr = &global_trace;
  1972. iter->trace = current_trace;
  1973. filp->private_data = iter;
  1974. if (iter->trace->pipe_open)
  1975. iter->trace->pipe_open(iter);
  1976. mutex_unlock(&trace_types_lock);
  1977. return 0;
  1978. }
  1979. static int tracing_release_pipe(struct inode *inode, struct file *file)
  1980. {
  1981. struct trace_iterator *iter = file->private_data;
  1982. kfree(iter);
  1983. atomic_dec(&tracing_reader);
  1984. return 0;
  1985. }
  1986. static unsigned int
  1987. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  1988. {
  1989. struct trace_iterator *iter = filp->private_data;
  1990. if (trace_flags & TRACE_ITER_BLOCK) {
  1991. /*
  1992. * Always select as readable when in blocking mode
  1993. */
  1994. return POLLIN | POLLRDNORM;
  1995. } else {
  1996. if (!trace_empty(iter))
  1997. return POLLIN | POLLRDNORM;
  1998. poll_wait(filp, &trace_wait, poll_table);
  1999. if (!trace_empty(iter))
  2000. return POLLIN | POLLRDNORM;
  2001. return 0;
  2002. }
  2003. }
  2004. /*
  2005. * Consumer reader.
  2006. */
  2007. static ssize_t
  2008. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2009. size_t cnt, loff_t *ppos)
  2010. {
  2011. struct trace_iterator *iter = filp->private_data;
  2012. struct trace_array_cpu *data;
  2013. static cpumask_t mask;
  2014. unsigned long flags;
  2015. #ifdef CONFIG_FTRACE
  2016. int ftrace_save;
  2017. #endif
  2018. int cpu;
  2019. ssize_t sret;
  2020. /* return any leftover data */
  2021. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2022. if (sret != -EBUSY)
  2023. return sret;
  2024. sret = 0;
  2025. trace_seq_reset(&iter->seq);
  2026. mutex_lock(&trace_types_lock);
  2027. if (iter->trace->read) {
  2028. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2029. if (sret)
  2030. goto out;
  2031. }
  2032. while (trace_empty(iter)) {
  2033. if ((filp->f_flags & O_NONBLOCK)) {
  2034. sret = -EAGAIN;
  2035. goto out;
  2036. }
  2037. /*
  2038. * This is a make-shift waitqueue. The reason we don't use
  2039. * an actual wait queue is because:
  2040. * 1) we only ever have one waiter
  2041. * 2) the tracing, traces all functions, we don't want
  2042. * the overhead of calling wake_up and friends
  2043. * (and tracing them too)
  2044. * Anyway, this is really very primitive wakeup.
  2045. */
  2046. set_current_state(TASK_INTERRUPTIBLE);
  2047. iter->tr->waiter = current;
  2048. mutex_unlock(&trace_types_lock);
  2049. /* sleep for 100 msecs, and try again. */
  2050. schedule_timeout(HZ/10);
  2051. mutex_lock(&trace_types_lock);
  2052. iter->tr->waiter = NULL;
  2053. if (signal_pending(current)) {
  2054. sret = -EINTR;
  2055. goto out;
  2056. }
  2057. if (iter->trace != current_trace)
  2058. goto out;
  2059. /*
  2060. * We block until we read something and tracing is disabled.
  2061. * We still block if tracing is disabled, but we have never
  2062. * read anything. This allows a user to cat this file, and
  2063. * then enable tracing. But after we have read something,
  2064. * we give an EOF when tracing is again disabled.
  2065. *
  2066. * iter->pos will be 0 if we haven't read anything.
  2067. */
  2068. if (!tracer_enabled && iter->pos)
  2069. break;
  2070. continue;
  2071. }
  2072. /* stop when tracing is finished */
  2073. if (trace_empty(iter))
  2074. goto out;
  2075. if (cnt >= PAGE_SIZE)
  2076. cnt = PAGE_SIZE - 1;
  2077. /* reset all but tr, trace, and overruns */
  2078. memset(&iter->seq, 0,
  2079. sizeof(struct trace_iterator) -
  2080. offsetof(struct trace_iterator, seq));
  2081. iter->pos = -1;
  2082. /*
  2083. * We need to stop all tracing on all CPUS to read the
  2084. * the next buffer. This is a bit expensive, but is
  2085. * not done often. We fill all what we can read,
  2086. * and then release the locks again.
  2087. */
  2088. cpus_clear(mask);
  2089. local_irq_save(flags);
  2090. #ifdef CONFIG_FTRACE
  2091. ftrace_save = ftrace_enabled;
  2092. ftrace_enabled = 0;
  2093. #endif
  2094. smp_wmb();
  2095. for_each_tracing_cpu(cpu) {
  2096. data = iter->tr->data[cpu];
  2097. if (!head_page(data) || !data->trace_idx)
  2098. continue;
  2099. atomic_inc(&data->disabled);
  2100. cpu_set(cpu, mask);
  2101. }
  2102. for_each_cpu_mask(cpu, mask) {
  2103. data = iter->tr->data[cpu];
  2104. __raw_spin_lock(&data->lock);
  2105. if (data->overrun > iter->last_overrun[cpu])
  2106. iter->overrun[cpu] +=
  2107. data->overrun - iter->last_overrun[cpu];
  2108. iter->last_overrun[cpu] = data->overrun;
  2109. }
  2110. while (find_next_entry_inc(iter) != NULL) {
  2111. int ret;
  2112. int len = iter->seq.len;
  2113. ret = print_trace_line(iter);
  2114. if (!ret) {
  2115. /* don't print partial lines */
  2116. iter->seq.len = len;
  2117. break;
  2118. }
  2119. trace_consume(iter);
  2120. if (iter->seq.len >= cnt)
  2121. break;
  2122. }
  2123. for_each_cpu_mask(cpu, mask) {
  2124. data = iter->tr->data[cpu];
  2125. __raw_spin_unlock(&data->lock);
  2126. }
  2127. for_each_cpu_mask(cpu, mask) {
  2128. data = iter->tr->data[cpu];
  2129. atomic_dec(&data->disabled);
  2130. }
  2131. #ifdef CONFIG_FTRACE
  2132. ftrace_enabled = ftrace_save;
  2133. #endif
  2134. local_irq_restore(flags);
  2135. /* Now copy what we have to the user */
  2136. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2137. if (iter->seq.readpos >= iter->seq.len)
  2138. trace_seq_reset(&iter->seq);
  2139. if (sret == -EBUSY)
  2140. sret = 0;
  2141. out:
  2142. mutex_unlock(&trace_types_lock);
  2143. return sret;
  2144. }
  2145. static ssize_t
  2146. tracing_entries_read(struct file *filp, char __user *ubuf,
  2147. size_t cnt, loff_t *ppos)
  2148. {
  2149. struct trace_array *tr = filp->private_data;
  2150. char buf[64];
  2151. int r;
  2152. r = sprintf(buf, "%lu\n", tr->entries);
  2153. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2154. }
  2155. static ssize_t
  2156. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2157. size_t cnt, loff_t *ppos)
  2158. {
  2159. unsigned long val;
  2160. char buf[64];
  2161. int ret;
  2162. if (cnt >= sizeof(buf))
  2163. return -EINVAL;
  2164. if (copy_from_user(&buf, ubuf, cnt))
  2165. return -EFAULT;
  2166. buf[cnt] = 0;
  2167. ret = strict_strtoul(buf, 10, &val);
  2168. if (ret < 0)
  2169. return ret;
  2170. /* must have at least 1 entry */
  2171. if (!val)
  2172. return -EINVAL;
  2173. mutex_lock(&trace_types_lock);
  2174. if (current_trace != &no_tracer) {
  2175. cnt = -EBUSY;
  2176. pr_info("ftrace: set current_tracer to none"
  2177. " before modifying buffer size\n");
  2178. goto out;
  2179. }
  2180. if (val > global_trace.entries) {
  2181. long pages_requested;
  2182. unsigned long freeable_pages;
  2183. /* make sure we have enough memory before mapping */
  2184. pages_requested =
  2185. (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
  2186. /* account for each buffer (and max_tr) */
  2187. pages_requested *= tracing_nr_buffers * 2;
  2188. /* Check for overflow */
  2189. if (pages_requested < 0) {
  2190. cnt = -ENOMEM;
  2191. goto out;
  2192. }
  2193. freeable_pages = determine_dirtyable_memory();
  2194. /* we only allow to request 1/4 of useable memory */
  2195. if (pages_requested >
  2196. ((freeable_pages + tracing_pages_allocated) / 4)) {
  2197. cnt = -ENOMEM;
  2198. goto out;
  2199. }
  2200. while (global_trace.entries < val) {
  2201. if (trace_alloc_page()) {
  2202. cnt = -ENOMEM;
  2203. goto out;
  2204. }
  2205. /* double check that we don't go over the known pages */
  2206. if (tracing_pages_allocated > pages_requested)
  2207. break;
  2208. }
  2209. } else {
  2210. /* include the number of entries in val (inc of page entries) */
  2211. while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
  2212. trace_free_page();
  2213. }
  2214. filp->f_pos += cnt;
  2215. out:
  2216. max_tr.entries = global_trace.entries;
  2217. mutex_unlock(&trace_types_lock);
  2218. return cnt;
  2219. }
  2220. static struct file_operations tracing_max_lat_fops = {
  2221. .open = tracing_open_generic,
  2222. .read = tracing_max_lat_read,
  2223. .write = tracing_max_lat_write,
  2224. };
  2225. static struct file_operations tracing_ctrl_fops = {
  2226. .open = tracing_open_generic,
  2227. .read = tracing_ctrl_read,
  2228. .write = tracing_ctrl_write,
  2229. };
  2230. static struct file_operations set_tracer_fops = {
  2231. .open = tracing_open_generic,
  2232. .read = tracing_set_trace_read,
  2233. .write = tracing_set_trace_write,
  2234. };
  2235. static struct file_operations tracing_pipe_fops = {
  2236. .open = tracing_open_pipe,
  2237. .poll = tracing_poll_pipe,
  2238. .read = tracing_read_pipe,
  2239. .release = tracing_release_pipe,
  2240. };
  2241. static struct file_operations tracing_entries_fops = {
  2242. .open = tracing_open_generic,
  2243. .read = tracing_entries_read,
  2244. .write = tracing_entries_write,
  2245. };
  2246. #ifdef CONFIG_DYNAMIC_FTRACE
  2247. static ssize_t
  2248. tracing_read_long(struct file *filp, char __user *ubuf,
  2249. size_t cnt, loff_t *ppos)
  2250. {
  2251. unsigned long *p = filp->private_data;
  2252. char buf[64];
  2253. int r;
  2254. r = sprintf(buf, "%ld\n", *p);
  2255. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2256. }
  2257. static struct file_operations tracing_read_long_fops = {
  2258. .open = tracing_open_generic,
  2259. .read = tracing_read_long,
  2260. };
  2261. #endif
  2262. static struct dentry *d_tracer;
  2263. struct dentry *tracing_init_dentry(void)
  2264. {
  2265. static int once;
  2266. if (d_tracer)
  2267. return d_tracer;
  2268. d_tracer = debugfs_create_dir("tracing", NULL);
  2269. if (!d_tracer && !once) {
  2270. once = 1;
  2271. pr_warning("Could not create debugfs directory 'tracing'\n");
  2272. return NULL;
  2273. }
  2274. return d_tracer;
  2275. }
  2276. #ifdef CONFIG_FTRACE_SELFTEST
  2277. /* Let selftest have access to static functions in this file */
  2278. #include "trace_selftest.c"
  2279. #endif
  2280. static __init void tracer_init_debugfs(void)
  2281. {
  2282. struct dentry *d_tracer;
  2283. struct dentry *entry;
  2284. d_tracer = tracing_init_dentry();
  2285. entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
  2286. &global_trace, &tracing_ctrl_fops);
  2287. if (!entry)
  2288. pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
  2289. entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
  2290. NULL, &tracing_iter_fops);
  2291. if (!entry)
  2292. pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
  2293. entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
  2294. NULL, &tracing_cpumask_fops);
  2295. if (!entry)
  2296. pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
  2297. entry = debugfs_create_file("latency_trace", 0444, d_tracer,
  2298. &global_trace, &tracing_lt_fops);
  2299. if (!entry)
  2300. pr_warning("Could not create debugfs 'latency_trace' entry\n");
  2301. entry = debugfs_create_file("trace", 0444, d_tracer,
  2302. &global_trace, &tracing_fops);
  2303. if (!entry)
  2304. pr_warning("Could not create debugfs 'trace' entry\n");
  2305. entry = debugfs_create_file("available_tracers", 0444, d_tracer,
  2306. &global_trace, &show_traces_fops);
  2307. if (!entry)
  2308. pr_warning("Could not create debugfs 'trace' entry\n");
  2309. entry = debugfs_create_file("current_tracer", 0444, d_tracer,
  2310. &global_trace, &set_tracer_fops);
  2311. if (!entry)
  2312. pr_warning("Could not create debugfs 'trace' entry\n");
  2313. entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
  2314. &tracing_max_latency,
  2315. &tracing_max_lat_fops);
  2316. if (!entry)
  2317. pr_warning("Could not create debugfs "
  2318. "'tracing_max_latency' entry\n");
  2319. entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
  2320. &tracing_thresh, &tracing_max_lat_fops);
  2321. if (!entry)
  2322. pr_warning("Could not create debugfs "
  2323. "'tracing_threash' entry\n");
  2324. entry = debugfs_create_file("README", 0644, d_tracer,
  2325. NULL, &tracing_readme_fops);
  2326. if (!entry)
  2327. pr_warning("Could not create debugfs 'README' entry\n");
  2328. entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
  2329. NULL, &tracing_pipe_fops);
  2330. if (!entry)
  2331. pr_warning("Could not create debugfs "
  2332. "'tracing_threash' entry\n");
  2333. entry = debugfs_create_file("trace_entries", 0644, d_tracer,
  2334. &global_trace, &tracing_entries_fops);
  2335. if (!entry)
  2336. pr_warning("Could not create debugfs "
  2337. "'tracing_threash' entry\n");
  2338. #ifdef CONFIG_DYNAMIC_FTRACE
  2339. entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  2340. &ftrace_update_tot_cnt,
  2341. &tracing_read_long_fops);
  2342. if (!entry)
  2343. pr_warning("Could not create debugfs "
  2344. "'dyn_ftrace_total_info' entry\n");
  2345. #endif
  2346. }
  2347. static int trace_alloc_page(void)
  2348. {
  2349. struct trace_array_cpu *data;
  2350. struct page *page, *tmp;
  2351. LIST_HEAD(pages);
  2352. void *array;
  2353. unsigned pages_allocated = 0;
  2354. int i;
  2355. /* first allocate a page for each CPU */
  2356. for_each_tracing_cpu(i) {
  2357. array = (void *)__get_free_page(GFP_KERNEL);
  2358. if (array == NULL) {
  2359. printk(KERN_ERR "tracer: failed to allocate page"
  2360. "for trace buffer!\n");
  2361. goto free_pages;
  2362. }
  2363. pages_allocated++;
  2364. page = virt_to_page(array);
  2365. list_add(&page->lru, &pages);
  2366. /* Only allocate if we are actually using the max trace */
  2367. #ifdef CONFIG_TRACER_MAX_TRACE
  2368. array = (void *)__get_free_page(GFP_KERNEL);
  2369. if (array == NULL) {
  2370. printk(KERN_ERR "tracer: failed to allocate page"
  2371. "for trace buffer!\n");
  2372. goto free_pages;
  2373. }
  2374. pages_allocated++;
  2375. page = virt_to_page(array);
  2376. list_add(&page->lru, &pages);
  2377. #endif
  2378. }
  2379. /* Now that we successfully allocate a page per CPU, add them */
  2380. for_each_tracing_cpu(i) {
  2381. data = global_trace.data[i];
  2382. page = list_entry(pages.next, struct page, lru);
  2383. list_del_init(&page->lru);
  2384. list_add_tail(&page->lru, &data->trace_pages);
  2385. ClearPageLRU(page);
  2386. #ifdef CONFIG_TRACER_MAX_TRACE
  2387. data = max_tr.data[i];
  2388. page = list_entry(pages.next, struct page, lru);
  2389. list_del_init(&page->lru);
  2390. list_add_tail(&page->lru, &data->trace_pages);
  2391. SetPageLRU(page);
  2392. #endif
  2393. }
  2394. tracing_pages_allocated += pages_allocated;
  2395. global_trace.entries += ENTRIES_PER_PAGE;
  2396. return 0;
  2397. free_pages:
  2398. list_for_each_entry_safe(page, tmp, &pages, lru) {
  2399. list_del_init(&page->lru);
  2400. __free_page(page);
  2401. }
  2402. return -ENOMEM;
  2403. }
  2404. static int trace_free_page(void)
  2405. {
  2406. struct trace_array_cpu *data;
  2407. struct page *page;
  2408. struct list_head *p;
  2409. int i;
  2410. int ret = 0;
  2411. /* free one page from each buffer */
  2412. for_each_tracing_cpu(i) {
  2413. data = global_trace.data[i];
  2414. p = data->trace_pages.next;
  2415. if (p == &data->trace_pages) {
  2416. /* should never happen */
  2417. WARN_ON(1);
  2418. tracing_disabled = 1;
  2419. ret = -1;
  2420. break;
  2421. }
  2422. page = list_entry(p, struct page, lru);
  2423. ClearPageLRU(page);
  2424. list_del(&page->lru);
  2425. tracing_pages_allocated--;
  2426. tracing_pages_allocated--;
  2427. __free_page(page);
  2428. tracing_reset(data);
  2429. #ifdef CONFIG_TRACER_MAX_TRACE
  2430. data = max_tr.data[i];
  2431. p = data->trace_pages.next;
  2432. if (p == &data->trace_pages) {
  2433. /* should never happen */
  2434. WARN_ON(1);
  2435. tracing_disabled = 1;
  2436. ret = -1;
  2437. break;
  2438. }
  2439. page = list_entry(p, struct page, lru);
  2440. ClearPageLRU(page);
  2441. list_del(&page->lru);
  2442. __free_page(page);
  2443. tracing_reset(data);
  2444. #endif
  2445. }
  2446. global_trace.entries -= ENTRIES_PER_PAGE;
  2447. return ret;
  2448. }
  2449. __init static int tracer_alloc_buffers(void)
  2450. {
  2451. struct trace_array_cpu *data;
  2452. void *array;
  2453. struct page *page;
  2454. int pages = 0;
  2455. int ret = -ENOMEM;
  2456. int i;
  2457. global_trace.ctrl = tracer_enabled;
  2458. /* TODO: make the number of buffers hot pluggable with CPUS */
  2459. tracing_nr_buffers = num_possible_cpus();
  2460. tracing_buffer_mask = cpu_possible_map;
  2461. /* Allocate the first page for all buffers */
  2462. for_each_tracing_cpu(i) {
  2463. data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  2464. max_tr.data[i] = &per_cpu(max_data, i);
  2465. array = (void *)__get_free_page(GFP_KERNEL);
  2466. if (array == NULL) {
  2467. printk(KERN_ERR "tracer: failed to allocate page"
  2468. "for trace buffer!\n");
  2469. goto free_buffers;
  2470. }
  2471. /* set the array to the list */
  2472. INIT_LIST_HEAD(&data->trace_pages);
  2473. page = virt_to_page(array);
  2474. list_add(&page->lru, &data->trace_pages);
  2475. /* use the LRU flag to differentiate the two buffers */
  2476. ClearPageLRU(page);
  2477. data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2478. max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  2479. /* Only allocate if we are actually using the max trace */
  2480. #ifdef CONFIG_TRACER_MAX_TRACE
  2481. array = (void *)__get_free_page(GFP_KERNEL);
  2482. if (array == NULL) {
  2483. printk(KERN_ERR "tracer: failed to allocate page"
  2484. "for trace buffer!\n");
  2485. goto free_buffers;
  2486. }
  2487. INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
  2488. page = virt_to_page(array);
  2489. list_add(&page->lru, &max_tr.data[i]->trace_pages);
  2490. SetPageLRU(page);
  2491. #endif
  2492. }
  2493. /*
  2494. * Since we allocate by orders of pages, we may be able to
  2495. * round up a bit.
  2496. */
  2497. global_trace.entries = ENTRIES_PER_PAGE;
  2498. pages++;
  2499. while (global_trace.entries < trace_nr_entries) {
  2500. if (trace_alloc_page())
  2501. break;
  2502. pages++;
  2503. }
  2504. max_tr.entries = global_trace.entries;
  2505. pr_info("tracer: %d pages allocated for %ld",
  2506. pages, trace_nr_entries);
  2507. pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
  2508. pr_info(" actual entries %ld\n", global_trace.entries);
  2509. tracer_init_debugfs();
  2510. trace_init_cmdlines();
  2511. register_tracer(&no_tracer);
  2512. current_trace = &no_tracer;
  2513. /* All seems OK, enable tracing */
  2514. tracing_disabled = 0;
  2515. return 0;
  2516. free_buffers:
  2517. for (i-- ; i >= 0; i--) {
  2518. struct page *page, *tmp;
  2519. struct trace_array_cpu *data = global_trace.data[i];
  2520. if (data) {
  2521. list_for_each_entry_safe(page, tmp,
  2522. &data->trace_pages, lru) {
  2523. list_del_init(&page->lru);
  2524. __free_page(page);
  2525. }
  2526. }
  2527. #ifdef CONFIG_TRACER_MAX_TRACE
  2528. data = max_tr.data[i];
  2529. if (data) {
  2530. list_for_each_entry_safe(page, tmp,
  2531. &data->trace_pages, lru) {
  2532. list_del_init(&page->lru);
  2533. __free_page(page);
  2534. }
  2535. }
  2536. #endif
  2537. }
  2538. return ret;
  2539. }
  2540. fs_initcall(tracer_alloc_buffers);