ftrace.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149
  1. /*
  2. * Infrastructure for profiling code inserted by 'gcc -pg'.
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally ported from the -rt patch by:
  8. * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code in the latency_tracer, that is:
  11. *
  12. * Copyright (C) 2004-2006 Ingo Molnar
  13. * Copyright (C) 2004 William Lee Irwin III
  14. */
  15. #include <linux/stop_machine.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/kallsyms.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/suspend.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/kthread.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/kprobes.h>
  25. #include <linux/ftrace.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/ctype.h>
  28. #include <linux/list.h>
  29. #include <linux/hash.h>
  30. #include <trace/sched.h>
  31. #include <asm/ftrace.h>
  32. #include "trace.h"
  33. #include "trace_stat.h"
  34. #define FTRACE_WARN_ON(cond) \
  35. do { \
  36. if (WARN_ON(cond)) \
  37. ftrace_kill(); \
  38. } while (0)
  39. #define FTRACE_WARN_ON_ONCE(cond) \
  40. do { \
  41. if (WARN_ON_ONCE(cond)) \
  42. ftrace_kill(); \
  43. } while (0)
  44. /* hash bits for specific function selection */
  45. #define FTRACE_HASH_BITS 7
  46. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  47. /* ftrace_enabled is a method to turn ftrace on or off */
  48. int ftrace_enabled __read_mostly;
  49. static int last_ftrace_enabled;
  50. /* Quick disabling of function tracer. */
  51. int function_trace_stop;
  52. /*
  53. * ftrace_disabled is set when an anomaly is discovered.
  54. * ftrace_disabled is much stronger than ftrace_enabled.
  55. */
  56. static int ftrace_disabled __read_mostly;
  57. static DEFINE_MUTEX(ftrace_lock);
  58. static struct ftrace_ops ftrace_list_end __read_mostly =
  59. {
  60. .func = ftrace_stub,
  61. };
  62. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  63. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  64. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  65. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  66. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  67. {
  68. struct ftrace_ops *op = ftrace_list;
  69. /* in case someone actually ports this to alpha! */
  70. read_barrier_depends();
  71. while (op != &ftrace_list_end) {
  72. /* silly alpha */
  73. read_barrier_depends();
  74. op->func(ip, parent_ip);
  75. op = op->next;
  76. };
  77. }
  78. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  79. {
  80. if (!test_tsk_trace_trace(current))
  81. return;
  82. ftrace_pid_function(ip, parent_ip);
  83. }
  84. static void set_ftrace_pid_function(ftrace_func_t func)
  85. {
  86. /* do not set ftrace_pid_function to itself! */
  87. if (func != ftrace_pid_func)
  88. ftrace_pid_function = func;
  89. }
  90. /**
  91. * clear_ftrace_function - reset the ftrace function
  92. *
  93. * This NULLs the ftrace function and in essence stops
  94. * tracing. There may be lag
  95. */
  96. void clear_ftrace_function(void)
  97. {
  98. ftrace_trace_function = ftrace_stub;
  99. __ftrace_trace_function = ftrace_stub;
  100. ftrace_pid_function = ftrace_stub;
  101. }
  102. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  103. /*
  104. * For those archs that do not test ftrace_trace_stop in their
  105. * mcount call site, we need to do it from C.
  106. */
  107. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  108. {
  109. if (function_trace_stop)
  110. return;
  111. __ftrace_trace_function(ip, parent_ip);
  112. }
  113. #endif
  114. static int __register_ftrace_function(struct ftrace_ops *ops)
  115. {
  116. ops->next = ftrace_list;
  117. /*
  118. * We are entering ops into the ftrace_list but another
  119. * CPU might be walking that list. We need to make sure
  120. * the ops->next pointer is valid before another CPU sees
  121. * the ops pointer included into the ftrace_list.
  122. */
  123. smp_wmb();
  124. ftrace_list = ops;
  125. if (ftrace_enabled) {
  126. ftrace_func_t func;
  127. if (ops->next == &ftrace_list_end)
  128. func = ops->func;
  129. else
  130. func = ftrace_list_func;
  131. if (ftrace_pid_trace) {
  132. set_ftrace_pid_function(func);
  133. func = ftrace_pid_func;
  134. }
  135. /*
  136. * For one func, simply call it directly.
  137. * For more than one func, call the chain.
  138. */
  139. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  140. ftrace_trace_function = func;
  141. #else
  142. __ftrace_trace_function = func;
  143. ftrace_trace_function = ftrace_test_stop_func;
  144. #endif
  145. }
  146. return 0;
  147. }
  148. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  149. {
  150. struct ftrace_ops **p;
  151. /*
  152. * If we are removing the last function, then simply point
  153. * to the ftrace_stub.
  154. */
  155. if (ftrace_list == ops && ops->next == &ftrace_list_end) {
  156. ftrace_trace_function = ftrace_stub;
  157. ftrace_list = &ftrace_list_end;
  158. return 0;
  159. }
  160. for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
  161. if (*p == ops)
  162. break;
  163. if (*p != ops)
  164. return -1;
  165. *p = (*p)->next;
  166. if (ftrace_enabled) {
  167. /* If we only have one func left, then call that directly */
  168. if (ftrace_list->next == &ftrace_list_end) {
  169. ftrace_func_t func = ftrace_list->func;
  170. if (ftrace_pid_trace) {
  171. set_ftrace_pid_function(func);
  172. func = ftrace_pid_func;
  173. }
  174. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  175. ftrace_trace_function = func;
  176. #else
  177. __ftrace_trace_function = func;
  178. #endif
  179. }
  180. }
  181. return 0;
  182. }
  183. static void ftrace_update_pid_func(void)
  184. {
  185. ftrace_func_t func;
  186. if (ftrace_trace_function == ftrace_stub)
  187. return;
  188. func = ftrace_trace_function;
  189. if (ftrace_pid_trace) {
  190. set_ftrace_pid_function(func);
  191. func = ftrace_pid_func;
  192. } else {
  193. if (func == ftrace_pid_func)
  194. func = ftrace_pid_function;
  195. }
  196. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  197. ftrace_trace_function = func;
  198. #else
  199. __ftrace_trace_function = func;
  200. #endif
  201. }
  202. #ifdef CONFIG_FUNCTION_PROFILER
  203. struct ftrace_profile {
  204. struct hlist_node node;
  205. unsigned long ip;
  206. unsigned long counter;
  207. };
  208. struct ftrace_profile_page {
  209. struct ftrace_profile_page *next;
  210. unsigned long index;
  211. struct ftrace_profile records[];
  212. };
  213. #define PROFILE_RECORDS_SIZE \
  214. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  215. #define PROFILES_PER_PAGE \
  216. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  217. /* TODO: make these percpu, to prevent cache line bouncing */
  218. static struct ftrace_profile_page *profile_pages_start;
  219. static struct ftrace_profile_page *profile_pages;
  220. static struct hlist_head *ftrace_profile_hash;
  221. static int ftrace_profile_bits;
  222. static int ftrace_profile_enabled;
  223. static DEFINE_MUTEX(ftrace_profile_lock);
  224. static DEFINE_PER_CPU(atomic_t, ftrace_profile_disable);
  225. #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
  226. static raw_spinlock_t ftrace_profile_rec_lock =
  227. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  228. static void *
  229. function_stat_next(void *v, int idx)
  230. {
  231. struct ftrace_profile *rec = v;
  232. struct ftrace_profile_page *pg;
  233. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  234. again:
  235. rec++;
  236. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  237. pg = pg->next;
  238. if (!pg)
  239. return NULL;
  240. rec = &pg->records[0];
  241. if (!rec->counter)
  242. goto again;
  243. }
  244. return rec;
  245. }
  246. static void *function_stat_start(struct tracer_stat *trace)
  247. {
  248. return function_stat_next(&profile_pages_start->records[0], 0);
  249. }
  250. static int function_stat_cmp(void *p1, void *p2)
  251. {
  252. struct ftrace_profile *a = p1;
  253. struct ftrace_profile *b = p2;
  254. if (a->counter < b->counter)
  255. return -1;
  256. if (a->counter > b->counter)
  257. return 1;
  258. else
  259. return 0;
  260. }
  261. static int function_stat_headers(struct seq_file *m)
  262. {
  263. seq_printf(m, " Function Hit\n"
  264. " -------- ---\n");
  265. return 0;
  266. }
  267. static int function_stat_show(struct seq_file *m, void *v)
  268. {
  269. struct ftrace_profile *rec = v;
  270. char str[KSYM_SYMBOL_LEN];
  271. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  272. seq_printf(m, " %-30.30s %10lu\n", str, rec->counter);
  273. return 0;
  274. }
  275. static struct tracer_stat function_stats = {
  276. .name = "functions",
  277. .stat_start = function_stat_start,
  278. .stat_next = function_stat_next,
  279. .stat_cmp = function_stat_cmp,
  280. .stat_headers = function_stat_headers,
  281. .stat_show = function_stat_show
  282. };
  283. static void ftrace_profile_reset(void)
  284. {
  285. struct ftrace_profile_page *pg;
  286. pg = profile_pages = profile_pages_start;
  287. while (pg) {
  288. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  289. pg->index = 0;
  290. pg = pg->next;
  291. }
  292. memset(ftrace_profile_hash, 0,
  293. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  294. }
  295. int ftrace_profile_pages_init(void)
  296. {
  297. struct ftrace_profile_page *pg;
  298. int i;
  299. /* If we already allocated, do nothing */
  300. if (profile_pages)
  301. return 0;
  302. profile_pages = (void *)get_zeroed_page(GFP_KERNEL);
  303. if (!profile_pages)
  304. return -ENOMEM;
  305. pg = profile_pages_start = profile_pages;
  306. /* allocate 10 more pages to start */
  307. for (i = 0; i < 10; i++) {
  308. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  309. /*
  310. * We only care about allocating profile_pages, if
  311. * we failed to allocate here, hopefully we will allocate
  312. * later.
  313. */
  314. if (!pg->next)
  315. break;
  316. pg = pg->next;
  317. }
  318. return 0;
  319. }
  320. static int ftrace_profile_init(void)
  321. {
  322. int size;
  323. if (ftrace_profile_hash) {
  324. /* If the profile is already created, simply reset it */
  325. ftrace_profile_reset();
  326. return 0;
  327. }
  328. /*
  329. * We are profiling all functions, but usually only a few thousand
  330. * functions are hit. We'll make a hash of 1024 items.
  331. */
  332. size = FTRACE_PROFILE_HASH_SIZE;
  333. ftrace_profile_hash =
  334. kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
  335. if (!ftrace_profile_hash)
  336. return -ENOMEM;
  337. size--;
  338. for (; size; size >>= 1)
  339. ftrace_profile_bits++;
  340. /* Preallocate a few pages */
  341. if (ftrace_profile_pages_init() < 0) {
  342. kfree(ftrace_profile_hash);
  343. ftrace_profile_hash = NULL;
  344. return -ENOMEM;
  345. }
  346. return 0;
  347. }
  348. /* interrupts must be disabled */
  349. static struct ftrace_profile *ftrace_find_profiled_func(unsigned long ip)
  350. {
  351. struct ftrace_profile *rec;
  352. struct hlist_head *hhd;
  353. struct hlist_node *n;
  354. unsigned long key;
  355. key = hash_long(ip, ftrace_profile_bits);
  356. hhd = &ftrace_profile_hash[key];
  357. if (hlist_empty(hhd))
  358. return NULL;
  359. hlist_for_each_entry_rcu(rec, n, hhd, node) {
  360. if (rec->ip == ip)
  361. return rec;
  362. }
  363. return NULL;
  364. }
  365. static void ftrace_add_profile(struct ftrace_profile *rec)
  366. {
  367. unsigned long key;
  368. key = hash_long(rec->ip, ftrace_profile_bits);
  369. hlist_add_head_rcu(&rec->node, &ftrace_profile_hash[key]);
  370. }
  371. /* Interrupts must be disabled calling this */
  372. static struct ftrace_profile *
  373. ftrace_profile_alloc(unsigned long ip, bool alloc_safe)
  374. {
  375. struct ftrace_profile *rec = NULL;
  376. /* prevent recursion */
  377. if (atomic_inc_return(&__get_cpu_var(ftrace_profile_disable)) != 1)
  378. goto out;
  379. __raw_spin_lock(&ftrace_profile_rec_lock);
  380. /* Try to always keep another page available */
  381. if (!profile_pages->next && alloc_safe)
  382. profile_pages->next = (void *)get_zeroed_page(GFP_ATOMIC);
  383. /*
  384. * Try to find the function again since another
  385. * task on another CPU could have added it
  386. */
  387. rec = ftrace_find_profiled_func(ip);
  388. if (rec)
  389. goto out_unlock;
  390. if (profile_pages->index == PROFILES_PER_PAGE) {
  391. if (!profile_pages->next)
  392. goto out_unlock;
  393. profile_pages = profile_pages->next;
  394. }
  395. rec = &profile_pages->records[profile_pages->index++];
  396. rec->ip = ip;
  397. ftrace_add_profile(rec);
  398. out_unlock:
  399. __raw_spin_unlock(&ftrace_profile_rec_lock);
  400. out:
  401. atomic_dec(&__get_cpu_var(ftrace_profile_disable));
  402. return rec;
  403. }
  404. /*
  405. * If we are not in an interrupt, or softirq and
  406. * and interrupts are disabled and preemption is not enabled
  407. * (not in a spinlock) then it should be safe to allocate memory.
  408. */
  409. static bool ftrace_safe_to_allocate(void)
  410. {
  411. return !in_interrupt() && irqs_disabled() && !preempt_count();
  412. }
  413. static void
  414. function_profile_call(unsigned long ip, unsigned long parent_ip)
  415. {
  416. struct ftrace_profile *rec;
  417. unsigned long flags;
  418. bool alloc_safe;
  419. if (!ftrace_profile_enabled)
  420. return;
  421. alloc_safe = ftrace_safe_to_allocate();
  422. local_irq_save(flags);
  423. rec = ftrace_find_profiled_func(ip);
  424. if (!rec) {
  425. rec = ftrace_profile_alloc(ip, alloc_safe);
  426. if (!rec)
  427. goto out;
  428. }
  429. rec->counter++;
  430. out:
  431. local_irq_restore(flags);
  432. }
  433. static struct ftrace_ops ftrace_profile_ops __read_mostly =
  434. {
  435. .func = function_profile_call,
  436. };
  437. static ssize_t
  438. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  439. size_t cnt, loff_t *ppos)
  440. {
  441. unsigned long val;
  442. char buf[64];
  443. int ret;
  444. if (cnt >= sizeof(buf))
  445. return -EINVAL;
  446. if (copy_from_user(&buf, ubuf, cnt))
  447. return -EFAULT;
  448. buf[cnt] = 0;
  449. ret = strict_strtoul(buf, 10, &val);
  450. if (ret < 0)
  451. return ret;
  452. val = !!val;
  453. mutex_lock(&ftrace_profile_lock);
  454. if (ftrace_profile_enabled ^ val) {
  455. if (val) {
  456. ret = ftrace_profile_init();
  457. if (ret < 0) {
  458. cnt = ret;
  459. goto out;
  460. }
  461. register_ftrace_function(&ftrace_profile_ops);
  462. ftrace_profile_enabled = 1;
  463. } else {
  464. ftrace_profile_enabled = 0;
  465. unregister_ftrace_function(&ftrace_profile_ops);
  466. }
  467. }
  468. out:
  469. mutex_unlock(&ftrace_profile_lock);
  470. filp->f_pos += cnt;
  471. return cnt;
  472. }
  473. static ssize_t
  474. ftrace_profile_read(struct file *filp, char __user *ubuf,
  475. size_t cnt, loff_t *ppos)
  476. {
  477. char buf[64];
  478. int r;
  479. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  480. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  481. }
  482. static const struct file_operations ftrace_profile_fops = {
  483. .open = tracing_open_generic,
  484. .read = ftrace_profile_read,
  485. .write = ftrace_profile_write,
  486. };
  487. static void ftrace_profile_debugfs(struct dentry *d_tracer)
  488. {
  489. struct dentry *entry;
  490. int ret;
  491. ret = register_stat_tracer(&function_stats);
  492. if (ret) {
  493. pr_warning("Warning: could not register "
  494. "function stats\n");
  495. return;
  496. }
  497. entry = debugfs_create_file("function_profile_enabled", 0644,
  498. d_tracer, NULL, &ftrace_profile_fops);
  499. if (!entry)
  500. pr_warning("Could not create debugfs "
  501. "'function_profile_enabled' entry\n");
  502. }
  503. #else /* CONFIG_FUNCTION_PROFILER */
  504. static void ftrace_profile_debugfs(struct dentry *d_tracer)
  505. {
  506. }
  507. #endif /* CONFIG_FUNCTION_PROFILER */
  508. /* set when tracing only a pid */
  509. struct pid *ftrace_pid_trace;
  510. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  511. #ifdef CONFIG_DYNAMIC_FTRACE
  512. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  513. # error Dynamic ftrace depends on MCOUNT_RECORD
  514. #endif
  515. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  516. struct ftrace_func_probe {
  517. struct hlist_node node;
  518. struct ftrace_probe_ops *ops;
  519. unsigned long flags;
  520. unsigned long ip;
  521. void *data;
  522. struct rcu_head rcu;
  523. };
  524. enum {
  525. FTRACE_ENABLE_CALLS = (1 << 0),
  526. FTRACE_DISABLE_CALLS = (1 << 1),
  527. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  528. FTRACE_ENABLE_MCOUNT = (1 << 3),
  529. FTRACE_DISABLE_MCOUNT = (1 << 4),
  530. FTRACE_START_FUNC_RET = (1 << 5),
  531. FTRACE_STOP_FUNC_RET = (1 << 6),
  532. };
  533. static int ftrace_filtered;
  534. static struct dyn_ftrace *ftrace_new_addrs;
  535. static DEFINE_MUTEX(ftrace_regex_lock);
  536. struct ftrace_page {
  537. struct ftrace_page *next;
  538. int index;
  539. struct dyn_ftrace records[];
  540. };
  541. #define ENTRIES_PER_PAGE \
  542. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  543. /* estimate from running different kernels */
  544. #define NR_TO_INIT 10000
  545. static struct ftrace_page *ftrace_pages_start;
  546. static struct ftrace_page *ftrace_pages;
  547. static struct dyn_ftrace *ftrace_free_records;
  548. /*
  549. * This is a double for. Do not use 'break' to break out of the loop,
  550. * you must use a goto.
  551. */
  552. #define do_for_each_ftrace_rec(pg, rec) \
  553. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  554. int _____i; \
  555. for (_____i = 0; _____i < pg->index; _____i++) { \
  556. rec = &pg->records[_____i];
  557. #define while_for_each_ftrace_rec() \
  558. } \
  559. }
  560. #ifdef CONFIG_KPROBES
  561. static int frozen_record_count;
  562. static inline void freeze_record(struct dyn_ftrace *rec)
  563. {
  564. if (!(rec->flags & FTRACE_FL_FROZEN)) {
  565. rec->flags |= FTRACE_FL_FROZEN;
  566. frozen_record_count++;
  567. }
  568. }
  569. static inline void unfreeze_record(struct dyn_ftrace *rec)
  570. {
  571. if (rec->flags & FTRACE_FL_FROZEN) {
  572. rec->flags &= ~FTRACE_FL_FROZEN;
  573. frozen_record_count--;
  574. }
  575. }
  576. static inline int record_frozen(struct dyn_ftrace *rec)
  577. {
  578. return rec->flags & FTRACE_FL_FROZEN;
  579. }
  580. #else
  581. # define freeze_record(rec) ({ 0; })
  582. # define unfreeze_record(rec) ({ 0; })
  583. # define record_frozen(rec) ({ 0; })
  584. #endif /* CONFIG_KPROBES */
  585. static void ftrace_free_rec(struct dyn_ftrace *rec)
  586. {
  587. rec->freelist = ftrace_free_records;
  588. ftrace_free_records = rec;
  589. rec->flags |= FTRACE_FL_FREE;
  590. }
  591. void ftrace_release(void *start, unsigned long size)
  592. {
  593. struct dyn_ftrace *rec;
  594. struct ftrace_page *pg;
  595. unsigned long s = (unsigned long)start;
  596. unsigned long e = s + size;
  597. if (ftrace_disabled || !start)
  598. return;
  599. mutex_lock(&ftrace_lock);
  600. do_for_each_ftrace_rec(pg, rec) {
  601. if ((rec->ip >= s) && (rec->ip < e) &&
  602. !(rec->flags & FTRACE_FL_FREE))
  603. ftrace_free_rec(rec);
  604. } while_for_each_ftrace_rec();
  605. mutex_unlock(&ftrace_lock);
  606. }
  607. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  608. {
  609. struct dyn_ftrace *rec;
  610. /* First check for freed records */
  611. if (ftrace_free_records) {
  612. rec = ftrace_free_records;
  613. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  614. FTRACE_WARN_ON_ONCE(1);
  615. ftrace_free_records = NULL;
  616. return NULL;
  617. }
  618. ftrace_free_records = rec->freelist;
  619. memset(rec, 0, sizeof(*rec));
  620. return rec;
  621. }
  622. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  623. if (!ftrace_pages->next) {
  624. /* allocate another page */
  625. ftrace_pages->next =
  626. (void *)get_zeroed_page(GFP_KERNEL);
  627. if (!ftrace_pages->next)
  628. return NULL;
  629. }
  630. ftrace_pages = ftrace_pages->next;
  631. }
  632. return &ftrace_pages->records[ftrace_pages->index++];
  633. }
  634. static struct dyn_ftrace *
  635. ftrace_record_ip(unsigned long ip)
  636. {
  637. struct dyn_ftrace *rec;
  638. if (ftrace_disabled)
  639. return NULL;
  640. rec = ftrace_alloc_dyn_node(ip);
  641. if (!rec)
  642. return NULL;
  643. rec->ip = ip;
  644. rec->newlist = ftrace_new_addrs;
  645. ftrace_new_addrs = rec;
  646. return rec;
  647. }
  648. static void print_ip_ins(const char *fmt, unsigned char *p)
  649. {
  650. int i;
  651. printk(KERN_CONT "%s", fmt);
  652. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  653. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  654. }
  655. static void ftrace_bug(int failed, unsigned long ip)
  656. {
  657. switch (failed) {
  658. case -EFAULT:
  659. FTRACE_WARN_ON_ONCE(1);
  660. pr_info("ftrace faulted on modifying ");
  661. print_ip_sym(ip);
  662. break;
  663. case -EINVAL:
  664. FTRACE_WARN_ON_ONCE(1);
  665. pr_info("ftrace failed to modify ");
  666. print_ip_sym(ip);
  667. print_ip_ins(" actual: ", (unsigned char *)ip);
  668. printk(KERN_CONT "\n");
  669. break;
  670. case -EPERM:
  671. FTRACE_WARN_ON_ONCE(1);
  672. pr_info("ftrace faulted on writing ");
  673. print_ip_sym(ip);
  674. break;
  675. default:
  676. FTRACE_WARN_ON_ONCE(1);
  677. pr_info("ftrace faulted on unknown error ");
  678. print_ip_sym(ip);
  679. }
  680. }
  681. static int
  682. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  683. {
  684. unsigned long ftrace_addr;
  685. unsigned long ip, fl;
  686. ftrace_addr = (unsigned long)FTRACE_ADDR;
  687. ip = rec->ip;
  688. /*
  689. * If this record is not to be traced and
  690. * it is not enabled then do nothing.
  691. *
  692. * If this record is not to be traced and
  693. * it is enabled then disable it.
  694. *
  695. */
  696. if (rec->flags & FTRACE_FL_NOTRACE) {
  697. if (rec->flags & FTRACE_FL_ENABLED)
  698. rec->flags &= ~FTRACE_FL_ENABLED;
  699. else
  700. return 0;
  701. } else if (ftrace_filtered && enable) {
  702. /*
  703. * Filtering is on:
  704. */
  705. fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
  706. /* Record is filtered and enabled, do nothing */
  707. if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
  708. return 0;
  709. /* Record is not filtered or enabled, do nothing */
  710. if (!fl)
  711. return 0;
  712. /* Record is not filtered but enabled, disable it */
  713. if (fl == FTRACE_FL_ENABLED)
  714. rec->flags &= ~FTRACE_FL_ENABLED;
  715. else
  716. /* Otherwise record is filtered but not enabled, enable it */
  717. rec->flags |= FTRACE_FL_ENABLED;
  718. } else {
  719. /* Disable or not filtered */
  720. if (enable) {
  721. /* if record is enabled, do nothing */
  722. if (rec->flags & FTRACE_FL_ENABLED)
  723. return 0;
  724. rec->flags |= FTRACE_FL_ENABLED;
  725. } else {
  726. /* if record is not enabled, do nothing */
  727. if (!(rec->flags & FTRACE_FL_ENABLED))
  728. return 0;
  729. rec->flags &= ~FTRACE_FL_ENABLED;
  730. }
  731. }
  732. if (rec->flags & FTRACE_FL_ENABLED)
  733. return ftrace_make_call(rec, ftrace_addr);
  734. else
  735. return ftrace_make_nop(NULL, rec, ftrace_addr);
  736. }
  737. static void ftrace_replace_code(int enable)
  738. {
  739. struct dyn_ftrace *rec;
  740. struct ftrace_page *pg;
  741. int failed;
  742. do_for_each_ftrace_rec(pg, rec) {
  743. /*
  744. * Skip over free records, records that have
  745. * failed and not converted.
  746. */
  747. if (rec->flags & FTRACE_FL_FREE ||
  748. rec->flags & FTRACE_FL_FAILED ||
  749. !(rec->flags & FTRACE_FL_CONVERTED))
  750. continue;
  751. /* ignore updates to this record's mcount site */
  752. if (get_kprobe((void *)rec->ip)) {
  753. freeze_record(rec);
  754. continue;
  755. } else {
  756. unfreeze_record(rec);
  757. }
  758. failed = __ftrace_replace_code(rec, enable);
  759. if (failed) {
  760. rec->flags |= FTRACE_FL_FAILED;
  761. if ((system_state == SYSTEM_BOOTING) ||
  762. !core_kernel_text(rec->ip)) {
  763. ftrace_free_rec(rec);
  764. } else {
  765. ftrace_bug(failed, rec->ip);
  766. /* Stop processing */
  767. return;
  768. }
  769. }
  770. } while_for_each_ftrace_rec();
  771. }
  772. static int
  773. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  774. {
  775. unsigned long ip;
  776. int ret;
  777. ip = rec->ip;
  778. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  779. if (ret) {
  780. ftrace_bug(ret, ip);
  781. rec->flags |= FTRACE_FL_FAILED;
  782. return 0;
  783. }
  784. return 1;
  785. }
  786. /*
  787. * archs can override this function if they must do something
  788. * before the modifying code is performed.
  789. */
  790. int __weak ftrace_arch_code_modify_prepare(void)
  791. {
  792. return 0;
  793. }
  794. /*
  795. * archs can override this function if they must do something
  796. * after the modifying code is performed.
  797. */
  798. int __weak ftrace_arch_code_modify_post_process(void)
  799. {
  800. return 0;
  801. }
  802. static int __ftrace_modify_code(void *data)
  803. {
  804. int *command = data;
  805. if (*command & FTRACE_ENABLE_CALLS)
  806. ftrace_replace_code(1);
  807. else if (*command & FTRACE_DISABLE_CALLS)
  808. ftrace_replace_code(0);
  809. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  810. ftrace_update_ftrace_func(ftrace_trace_function);
  811. if (*command & FTRACE_START_FUNC_RET)
  812. ftrace_enable_ftrace_graph_caller();
  813. else if (*command & FTRACE_STOP_FUNC_RET)
  814. ftrace_disable_ftrace_graph_caller();
  815. return 0;
  816. }
  817. static void ftrace_run_update_code(int command)
  818. {
  819. int ret;
  820. ret = ftrace_arch_code_modify_prepare();
  821. FTRACE_WARN_ON(ret);
  822. if (ret)
  823. return;
  824. stop_machine(__ftrace_modify_code, &command, NULL);
  825. ret = ftrace_arch_code_modify_post_process();
  826. FTRACE_WARN_ON(ret);
  827. }
  828. static ftrace_func_t saved_ftrace_func;
  829. static int ftrace_start_up;
  830. static void ftrace_startup_enable(int command)
  831. {
  832. if (saved_ftrace_func != ftrace_trace_function) {
  833. saved_ftrace_func = ftrace_trace_function;
  834. command |= FTRACE_UPDATE_TRACE_FUNC;
  835. }
  836. if (!command || !ftrace_enabled)
  837. return;
  838. ftrace_run_update_code(command);
  839. }
  840. static void ftrace_startup(int command)
  841. {
  842. if (unlikely(ftrace_disabled))
  843. return;
  844. ftrace_start_up++;
  845. command |= FTRACE_ENABLE_CALLS;
  846. ftrace_startup_enable(command);
  847. }
  848. static void ftrace_shutdown(int command)
  849. {
  850. if (unlikely(ftrace_disabled))
  851. return;
  852. ftrace_start_up--;
  853. if (!ftrace_start_up)
  854. command |= FTRACE_DISABLE_CALLS;
  855. if (saved_ftrace_func != ftrace_trace_function) {
  856. saved_ftrace_func = ftrace_trace_function;
  857. command |= FTRACE_UPDATE_TRACE_FUNC;
  858. }
  859. if (!command || !ftrace_enabled)
  860. return;
  861. ftrace_run_update_code(command);
  862. }
  863. static void ftrace_startup_sysctl(void)
  864. {
  865. int command = FTRACE_ENABLE_MCOUNT;
  866. if (unlikely(ftrace_disabled))
  867. return;
  868. /* Force update next time */
  869. saved_ftrace_func = NULL;
  870. /* ftrace_start_up is true if we want ftrace running */
  871. if (ftrace_start_up)
  872. command |= FTRACE_ENABLE_CALLS;
  873. ftrace_run_update_code(command);
  874. }
  875. static void ftrace_shutdown_sysctl(void)
  876. {
  877. int command = FTRACE_DISABLE_MCOUNT;
  878. if (unlikely(ftrace_disabled))
  879. return;
  880. /* ftrace_start_up is true if ftrace is running */
  881. if (ftrace_start_up)
  882. command |= FTRACE_DISABLE_CALLS;
  883. ftrace_run_update_code(command);
  884. }
  885. static cycle_t ftrace_update_time;
  886. static unsigned long ftrace_update_cnt;
  887. unsigned long ftrace_update_tot_cnt;
  888. static int ftrace_update_code(struct module *mod)
  889. {
  890. struct dyn_ftrace *p;
  891. cycle_t start, stop;
  892. start = ftrace_now(raw_smp_processor_id());
  893. ftrace_update_cnt = 0;
  894. while (ftrace_new_addrs) {
  895. /* If something went wrong, bail without enabling anything */
  896. if (unlikely(ftrace_disabled))
  897. return -1;
  898. p = ftrace_new_addrs;
  899. ftrace_new_addrs = p->newlist;
  900. p->flags = 0L;
  901. /* convert record (i.e, patch mcount-call with NOP) */
  902. if (ftrace_code_disable(mod, p)) {
  903. p->flags |= FTRACE_FL_CONVERTED;
  904. ftrace_update_cnt++;
  905. } else
  906. ftrace_free_rec(p);
  907. }
  908. stop = ftrace_now(raw_smp_processor_id());
  909. ftrace_update_time = stop - start;
  910. ftrace_update_tot_cnt += ftrace_update_cnt;
  911. return 0;
  912. }
  913. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  914. {
  915. struct ftrace_page *pg;
  916. int cnt;
  917. int i;
  918. /* allocate a few pages */
  919. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  920. if (!ftrace_pages_start)
  921. return -1;
  922. /*
  923. * Allocate a few more pages.
  924. *
  925. * TODO: have some parser search vmlinux before
  926. * final linking to find all calls to ftrace.
  927. * Then we can:
  928. * a) know how many pages to allocate.
  929. * and/or
  930. * b) set up the table then.
  931. *
  932. * The dynamic code is still necessary for
  933. * modules.
  934. */
  935. pg = ftrace_pages = ftrace_pages_start;
  936. cnt = num_to_init / ENTRIES_PER_PAGE;
  937. pr_info("ftrace: allocating %ld entries in %d pages\n",
  938. num_to_init, cnt + 1);
  939. for (i = 0; i < cnt; i++) {
  940. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  941. /* If we fail, we'll try later anyway */
  942. if (!pg->next)
  943. break;
  944. pg = pg->next;
  945. }
  946. return 0;
  947. }
  948. enum {
  949. FTRACE_ITER_FILTER = (1 << 0),
  950. FTRACE_ITER_CONT = (1 << 1),
  951. FTRACE_ITER_NOTRACE = (1 << 2),
  952. FTRACE_ITER_FAILURES = (1 << 3),
  953. FTRACE_ITER_PRINTALL = (1 << 4),
  954. FTRACE_ITER_HASH = (1 << 5),
  955. };
  956. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  957. struct ftrace_iterator {
  958. struct ftrace_page *pg;
  959. int hidx;
  960. int idx;
  961. unsigned flags;
  962. unsigned char buffer[FTRACE_BUFF_MAX+1];
  963. unsigned buffer_idx;
  964. unsigned filtered;
  965. };
  966. static void *
  967. t_hash_next(struct seq_file *m, void *v, loff_t *pos)
  968. {
  969. struct ftrace_iterator *iter = m->private;
  970. struct hlist_node *hnd = v;
  971. struct hlist_head *hhd;
  972. WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
  973. (*pos)++;
  974. retry:
  975. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  976. return NULL;
  977. hhd = &ftrace_func_hash[iter->hidx];
  978. if (hlist_empty(hhd)) {
  979. iter->hidx++;
  980. hnd = NULL;
  981. goto retry;
  982. }
  983. if (!hnd)
  984. hnd = hhd->first;
  985. else {
  986. hnd = hnd->next;
  987. if (!hnd) {
  988. iter->hidx++;
  989. goto retry;
  990. }
  991. }
  992. return hnd;
  993. }
  994. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  995. {
  996. struct ftrace_iterator *iter = m->private;
  997. void *p = NULL;
  998. iter->flags |= FTRACE_ITER_HASH;
  999. return t_hash_next(m, p, pos);
  1000. }
  1001. static int t_hash_show(struct seq_file *m, void *v)
  1002. {
  1003. struct ftrace_func_probe *rec;
  1004. struct hlist_node *hnd = v;
  1005. char str[KSYM_SYMBOL_LEN];
  1006. rec = hlist_entry(hnd, struct ftrace_func_probe, node);
  1007. if (rec->ops->print)
  1008. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1009. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1010. seq_printf(m, "%s:", str);
  1011. kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
  1012. seq_printf(m, "%s", str);
  1013. if (rec->data)
  1014. seq_printf(m, ":%p", rec->data);
  1015. seq_putc(m, '\n');
  1016. return 0;
  1017. }
  1018. static void *
  1019. t_next(struct seq_file *m, void *v, loff_t *pos)
  1020. {
  1021. struct ftrace_iterator *iter = m->private;
  1022. struct dyn_ftrace *rec = NULL;
  1023. if (iter->flags & FTRACE_ITER_HASH)
  1024. return t_hash_next(m, v, pos);
  1025. (*pos)++;
  1026. if (iter->flags & FTRACE_ITER_PRINTALL)
  1027. return NULL;
  1028. retry:
  1029. if (iter->idx >= iter->pg->index) {
  1030. if (iter->pg->next) {
  1031. iter->pg = iter->pg->next;
  1032. iter->idx = 0;
  1033. goto retry;
  1034. } else {
  1035. iter->idx = -1;
  1036. }
  1037. } else {
  1038. rec = &iter->pg->records[iter->idx++];
  1039. if ((rec->flags & FTRACE_FL_FREE) ||
  1040. (!(iter->flags & FTRACE_ITER_FAILURES) &&
  1041. (rec->flags & FTRACE_FL_FAILED)) ||
  1042. ((iter->flags & FTRACE_ITER_FAILURES) &&
  1043. !(rec->flags & FTRACE_FL_FAILED)) ||
  1044. ((iter->flags & FTRACE_ITER_FILTER) &&
  1045. !(rec->flags & FTRACE_FL_FILTER)) ||
  1046. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1047. !(rec->flags & FTRACE_FL_NOTRACE))) {
  1048. rec = NULL;
  1049. goto retry;
  1050. }
  1051. }
  1052. return rec;
  1053. }
  1054. static void *t_start(struct seq_file *m, loff_t *pos)
  1055. {
  1056. struct ftrace_iterator *iter = m->private;
  1057. void *p = NULL;
  1058. mutex_lock(&ftrace_lock);
  1059. /*
  1060. * For set_ftrace_filter reading, if we have the filter
  1061. * off, we can short cut and just print out that all
  1062. * functions are enabled.
  1063. */
  1064. if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
  1065. if (*pos > 0)
  1066. return t_hash_start(m, pos);
  1067. iter->flags |= FTRACE_ITER_PRINTALL;
  1068. (*pos)++;
  1069. return iter;
  1070. }
  1071. if (iter->flags & FTRACE_ITER_HASH)
  1072. return t_hash_start(m, pos);
  1073. if (*pos > 0) {
  1074. if (iter->idx < 0)
  1075. return p;
  1076. (*pos)--;
  1077. iter->idx--;
  1078. }
  1079. p = t_next(m, p, pos);
  1080. if (!p)
  1081. return t_hash_start(m, pos);
  1082. return p;
  1083. }
  1084. static void t_stop(struct seq_file *m, void *p)
  1085. {
  1086. mutex_unlock(&ftrace_lock);
  1087. }
  1088. static int t_show(struct seq_file *m, void *v)
  1089. {
  1090. struct ftrace_iterator *iter = m->private;
  1091. struct dyn_ftrace *rec = v;
  1092. char str[KSYM_SYMBOL_LEN];
  1093. if (iter->flags & FTRACE_ITER_HASH)
  1094. return t_hash_show(m, v);
  1095. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1096. seq_printf(m, "#### all functions enabled ####\n");
  1097. return 0;
  1098. }
  1099. if (!rec)
  1100. return 0;
  1101. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1102. seq_printf(m, "%s\n", str);
  1103. return 0;
  1104. }
  1105. static struct seq_operations show_ftrace_seq_ops = {
  1106. .start = t_start,
  1107. .next = t_next,
  1108. .stop = t_stop,
  1109. .show = t_show,
  1110. };
  1111. static int
  1112. ftrace_avail_open(struct inode *inode, struct file *file)
  1113. {
  1114. struct ftrace_iterator *iter;
  1115. int ret;
  1116. if (unlikely(ftrace_disabled))
  1117. return -ENODEV;
  1118. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1119. if (!iter)
  1120. return -ENOMEM;
  1121. iter->pg = ftrace_pages_start;
  1122. ret = seq_open(file, &show_ftrace_seq_ops);
  1123. if (!ret) {
  1124. struct seq_file *m = file->private_data;
  1125. m->private = iter;
  1126. } else {
  1127. kfree(iter);
  1128. }
  1129. return ret;
  1130. }
  1131. int ftrace_avail_release(struct inode *inode, struct file *file)
  1132. {
  1133. struct seq_file *m = (struct seq_file *)file->private_data;
  1134. struct ftrace_iterator *iter = m->private;
  1135. seq_release(inode, file);
  1136. kfree(iter);
  1137. return 0;
  1138. }
  1139. static int
  1140. ftrace_failures_open(struct inode *inode, struct file *file)
  1141. {
  1142. int ret;
  1143. struct seq_file *m;
  1144. struct ftrace_iterator *iter;
  1145. ret = ftrace_avail_open(inode, file);
  1146. if (!ret) {
  1147. m = (struct seq_file *)file->private_data;
  1148. iter = (struct ftrace_iterator *)m->private;
  1149. iter->flags = FTRACE_ITER_FAILURES;
  1150. }
  1151. return ret;
  1152. }
  1153. static void ftrace_filter_reset(int enable)
  1154. {
  1155. struct ftrace_page *pg;
  1156. struct dyn_ftrace *rec;
  1157. unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1158. mutex_lock(&ftrace_lock);
  1159. if (enable)
  1160. ftrace_filtered = 0;
  1161. do_for_each_ftrace_rec(pg, rec) {
  1162. if (rec->flags & FTRACE_FL_FAILED)
  1163. continue;
  1164. rec->flags &= ~type;
  1165. } while_for_each_ftrace_rec();
  1166. mutex_unlock(&ftrace_lock);
  1167. }
  1168. static int
  1169. ftrace_regex_open(struct inode *inode, struct file *file, int enable)
  1170. {
  1171. struct ftrace_iterator *iter;
  1172. int ret = 0;
  1173. if (unlikely(ftrace_disabled))
  1174. return -ENODEV;
  1175. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1176. if (!iter)
  1177. return -ENOMEM;
  1178. mutex_lock(&ftrace_regex_lock);
  1179. if ((file->f_mode & FMODE_WRITE) &&
  1180. !(file->f_flags & O_APPEND))
  1181. ftrace_filter_reset(enable);
  1182. if (file->f_mode & FMODE_READ) {
  1183. iter->pg = ftrace_pages_start;
  1184. iter->flags = enable ? FTRACE_ITER_FILTER :
  1185. FTRACE_ITER_NOTRACE;
  1186. ret = seq_open(file, &show_ftrace_seq_ops);
  1187. if (!ret) {
  1188. struct seq_file *m = file->private_data;
  1189. m->private = iter;
  1190. } else
  1191. kfree(iter);
  1192. } else
  1193. file->private_data = iter;
  1194. mutex_unlock(&ftrace_regex_lock);
  1195. return ret;
  1196. }
  1197. static int
  1198. ftrace_filter_open(struct inode *inode, struct file *file)
  1199. {
  1200. return ftrace_regex_open(inode, file, 1);
  1201. }
  1202. static int
  1203. ftrace_notrace_open(struct inode *inode, struct file *file)
  1204. {
  1205. return ftrace_regex_open(inode, file, 0);
  1206. }
  1207. static loff_t
  1208. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1209. {
  1210. loff_t ret;
  1211. if (file->f_mode & FMODE_READ)
  1212. ret = seq_lseek(file, offset, origin);
  1213. else
  1214. file->f_pos = ret = 1;
  1215. return ret;
  1216. }
  1217. enum {
  1218. MATCH_FULL,
  1219. MATCH_FRONT_ONLY,
  1220. MATCH_MIDDLE_ONLY,
  1221. MATCH_END_ONLY,
  1222. };
  1223. /*
  1224. * (static function - no need for kernel doc)
  1225. *
  1226. * Pass in a buffer containing a glob and this function will
  1227. * set search to point to the search part of the buffer and
  1228. * return the type of search it is (see enum above).
  1229. * This does modify buff.
  1230. *
  1231. * Returns enum type.
  1232. * search returns the pointer to use for comparison.
  1233. * not returns 1 if buff started with a '!'
  1234. * 0 otherwise.
  1235. */
  1236. static int
  1237. ftrace_setup_glob(char *buff, int len, char **search, int *not)
  1238. {
  1239. int type = MATCH_FULL;
  1240. int i;
  1241. if (buff[0] == '!') {
  1242. *not = 1;
  1243. buff++;
  1244. len--;
  1245. } else
  1246. *not = 0;
  1247. *search = buff;
  1248. for (i = 0; i < len; i++) {
  1249. if (buff[i] == '*') {
  1250. if (!i) {
  1251. *search = buff + 1;
  1252. type = MATCH_END_ONLY;
  1253. } else {
  1254. if (type == MATCH_END_ONLY)
  1255. type = MATCH_MIDDLE_ONLY;
  1256. else
  1257. type = MATCH_FRONT_ONLY;
  1258. buff[i] = 0;
  1259. break;
  1260. }
  1261. }
  1262. }
  1263. return type;
  1264. }
  1265. static int ftrace_match(char *str, char *regex, int len, int type)
  1266. {
  1267. int matched = 0;
  1268. char *ptr;
  1269. switch (type) {
  1270. case MATCH_FULL:
  1271. if (strcmp(str, regex) == 0)
  1272. matched = 1;
  1273. break;
  1274. case MATCH_FRONT_ONLY:
  1275. if (strncmp(str, regex, len) == 0)
  1276. matched = 1;
  1277. break;
  1278. case MATCH_MIDDLE_ONLY:
  1279. if (strstr(str, regex))
  1280. matched = 1;
  1281. break;
  1282. case MATCH_END_ONLY:
  1283. ptr = strstr(str, regex);
  1284. if (ptr && (ptr[len] == 0))
  1285. matched = 1;
  1286. break;
  1287. }
  1288. return matched;
  1289. }
  1290. static int
  1291. ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
  1292. {
  1293. char str[KSYM_SYMBOL_LEN];
  1294. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  1295. return ftrace_match(str, regex, len, type);
  1296. }
  1297. static void ftrace_match_records(char *buff, int len, int enable)
  1298. {
  1299. unsigned int search_len;
  1300. struct ftrace_page *pg;
  1301. struct dyn_ftrace *rec;
  1302. unsigned long flag;
  1303. char *search;
  1304. int type;
  1305. int not;
  1306. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1307. type = ftrace_setup_glob(buff, len, &search, &not);
  1308. search_len = strlen(search);
  1309. mutex_lock(&ftrace_lock);
  1310. do_for_each_ftrace_rec(pg, rec) {
  1311. if (rec->flags & FTRACE_FL_FAILED)
  1312. continue;
  1313. if (ftrace_match_record(rec, search, search_len, type)) {
  1314. if (not)
  1315. rec->flags &= ~flag;
  1316. else
  1317. rec->flags |= flag;
  1318. }
  1319. /*
  1320. * Only enable filtering if we have a function that
  1321. * is filtered on.
  1322. */
  1323. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1324. ftrace_filtered = 1;
  1325. } while_for_each_ftrace_rec();
  1326. mutex_unlock(&ftrace_lock);
  1327. }
  1328. static int
  1329. ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
  1330. char *regex, int len, int type)
  1331. {
  1332. char str[KSYM_SYMBOL_LEN];
  1333. char *modname;
  1334. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1335. if (!modname || strcmp(modname, mod))
  1336. return 0;
  1337. /* blank search means to match all funcs in the mod */
  1338. if (len)
  1339. return ftrace_match(str, regex, len, type);
  1340. else
  1341. return 1;
  1342. }
  1343. static void ftrace_match_module_records(char *buff, char *mod, int enable)
  1344. {
  1345. unsigned search_len = 0;
  1346. struct ftrace_page *pg;
  1347. struct dyn_ftrace *rec;
  1348. int type = MATCH_FULL;
  1349. char *search = buff;
  1350. unsigned long flag;
  1351. int not = 0;
  1352. flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
  1353. /* blank or '*' mean the same */
  1354. if (strcmp(buff, "*") == 0)
  1355. buff[0] = 0;
  1356. /* handle the case of 'dont filter this module' */
  1357. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1358. buff[0] = 0;
  1359. not = 1;
  1360. }
  1361. if (strlen(buff)) {
  1362. type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
  1363. search_len = strlen(search);
  1364. }
  1365. mutex_lock(&ftrace_lock);
  1366. do_for_each_ftrace_rec(pg, rec) {
  1367. if (rec->flags & FTRACE_FL_FAILED)
  1368. continue;
  1369. if (ftrace_match_module_record(rec, mod,
  1370. search, search_len, type)) {
  1371. if (not)
  1372. rec->flags &= ~flag;
  1373. else
  1374. rec->flags |= flag;
  1375. }
  1376. if (enable && (rec->flags & FTRACE_FL_FILTER))
  1377. ftrace_filtered = 1;
  1378. } while_for_each_ftrace_rec();
  1379. mutex_unlock(&ftrace_lock);
  1380. }
  1381. /*
  1382. * We register the module command as a template to show others how
  1383. * to register the a command as well.
  1384. */
  1385. static int
  1386. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1387. {
  1388. char *mod;
  1389. /*
  1390. * cmd == 'mod' because we only registered this func
  1391. * for the 'mod' ftrace_func_command.
  1392. * But if you register one func with multiple commands,
  1393. * you can tell which command was used by the cmd
  1394. * parameter.
  1395. */
  1396. /* we must have a module name */
  1397. if (!param)
  1398. return -EINVAL;
  1399. mod = strsep(&param, ":");
  1400. if (!strlen(mod))
  1401. return -EINVAL;
  1402. ftrace_match_module_records(func, mod, enable);
  1403. return 0;
  1404. }
  1405. static struct ftrace_func_command ftrace_mod_cmd = {
  1406. .name = "mod",
  1407. .func = ftrace_mod_callback,
  1408. };
  1409. static int __init ftrace_mod_cmd_init(void)
  1410. {
  1411. return register_ftrace_command(&ftrace_mod_cmd);
  1412. }
  1413. device_initcall(ftrace_mod_cmd_init);
  1414. static void
  1415. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1416. {
  1417. struct ftrace_func_probe *entry;
  1418. struct hlist_head *hhd;
  1419. struct hlist_node *n;
  1420. unsigned long key;
  1421. int resched;
  1422. key = hash_long(ip, FTRACE_HASH_BITS);
  1423. hhd = &ftrace_func_hash[key];
  1424. if (hlist_empty(hhd))
  1425. return;
  1426. /*
  1427. * Disable preemption for these calls to prevent a RCU grace
  1428. * period. This syncs the hash iteration and freeing of items
  1429. * on the hash. rcu_read_lock is too dangerous here.
  1430. */
  1431. resched = ftrace_preempt_disable();
  1432. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1433. if (entry->ip == ip)
  1434. entry->ops->func(ip, parent_ip, &entry->data);
  1435. }
  1436. ftrace_preempt_enable(resched);
  1437. }
  1438. static struct ftrace_ops trace_probe_ops __read_mostly =
  1439. {
  1440. .func = function_trace_probe_call,
  1441. };
  1442. static int ftrace_probe_registered;
  1443. static void __enable_ftrace_function_probe(void)
  1444. {
  1445. int i;
  1446. if (ftrace_probe_registered)
  1447. return;
  1448. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1449. struct hlist_head *hhd = &ftrace_func_hash[i];
  1450. if (hhd->first)
  1451. break;
  1452. }
  1453. /* Nothing registered? */
  1454. if (i == FTRACE_FUNC_HASHSIZE)
  1455. return;
  1456. __register_ftrace_function(&trace_probe_ops);
  1457. ftrace_startup(0);
  1458. ftrace_probe_registered = 1;
  1459. }
  1460. static void __disable_ftrace_function_probe(void)
  1461. {
  1462. int i;
  1463. if (!ftrace_probe_registered)
  1464. return;
  1465. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1466. struct hlist_head *hhd = &ftrace_func_hash[i];
  1467. if (hhd->first)
  1468. return;
  1469. }
  1470. /* no more funcs left */
  1471. __unregister_ftrace_function(&trace_probe_ops);
  1472. ftrace_shutdown(0);
  1473. ftrace_probe_registered = 0;
  1474. }
  1475. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1476. {
  1477. struct ftrace_func_probe *entry =
  1478. container_of(rhp, struct ftrace_func_probe, rcu);
  1479. if (entry->ops->free)
  1480. entry->ops->free(&entry->data);
  1481. kfree(entry);
  1482. }
  1483. int
  1484. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1485. void *data)
  1486. {
  1487. struct ftrace_func_probe *entry;
  1488. struct ftrace_page *pg;
  1489. struct dyn_ftrace *rec;
  1490. int type, len, not;
  1491. unsigned long key;
  1492. int count = 0;
  1493. char *search;
  1494. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1495. len = strlen(search);
  1496. /* we do not support '!' for function probes */
  1497. if (WARN_ON(not))
  1498. return -EINVAL;
  1499. mutex_lock(&ftrace_lock);
  1500. do_for_each_ftrace_rec(pg, rec) {
  1501. if (rec->flags & FTRACE_FL_FAILED)
  1502. continue;
  1503. if (!ftrace_match_record(rec, search, len, type))
  1504. continue;
  1505. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1506. if (!entry) {
  1507. /* If we did not process any, then return error */
  1508. if (!count)
  1509. count = -ENOMEM;
  1510. goto out_unlock;
  1511. }
  1512. count++;
  1513. entry->data = data;
  1514. /*
  1515. * The caller might want to do something special
  1516. * for each function we find. We call the callback
  1517. * to give the caller an opportunity to do so.
  1518. */
  1519. if (ops->callback) {
  1520. if (ops->callback(rec->ip, &entry->data) < 0) {
  1521. /* caller does not like this func */
  1522. kfree(entry);
  1523. continue;
  1524. }
  1525. }
  1526. entry->ops = ops;
  1527. entry->ip = rec->ip;
  1528. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  1529. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  1530. } while_for_each_ftrace_rec();
  1531. __enable_ftrace_function_probe();
  1532. out_unlock:
  1533. mutex_unlock(&ftrace_lock);
  1534. return count;
  1535. }
  1536. enum {
  1537. PROBE_TEST_FUNC = 1,
  1538. PROBE_TEST_DATA = 2
  1539. };
  1540. static void
  1541. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1542. void *data, int flags)
  1543. {
  1544. struct ftrace_func_probe *entry;
  1545. struct hlist_node *n, *tmp;
  1546. char str[KSYM_SYMBOL_LEN];
  1547. int type = MATCH_FULL;
  1548. int i, len = 0;
  1549. char *search;
  1550. if (glob && (strcmp(glob, "*") || !strlen(glob)))
  1551. glob = NULL;
  1552. else {
  1553. int not;
  1554. type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
  1555. len = strlen(search);
  1556. /* we do not support '!' for function probes */
  1557. if (WARN_ON(not))
  1558. return;
  1559. }
  1560. mutex_lock(&ftrace_lock);
  1561. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1562. struct hlist_head *hhd = &ftrace_func_hash[i];
  1563. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  1564. /* break up if statements for readability */
  1565. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  1566. continue;
  1567. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  1568. continue;
  1569. /* do this last, since it is the most expensive */
  1570. if (glob) {
  1571. kallsyms_lookup(entry->ip, NULL, NULL,
  1572. NULL, str);
  1573. if (!ftrace_match(str, glob, len, type))
  1574. continue;
  1575. }
  1576. hlist_del(&entry->node);
  1577. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  1578. }
  1579. }
  1580. __disable_ftrace_function_probe();
  1581. mutex_unlock(&ftrace_lock);
  1582. }
  1583. void
  1584. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1585. void *data)
  1586. {
  1587. __unregister_ftrace_function_probe(glob, ops, data,
  1588. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  1589. }
  1590. void
  1591. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  1592. {
  1593. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  1594. }
  1595. void unregister_ftrace_function_probe_all(char *glob)
  1596. {
  1597. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  1598. }
  1599. static LIST_HEAD(ftrace_commands);
  1600. static DEFINE_MUTEX(ftrace_cmd_mutex);
  1601. int register_ftrace_command(struct ftrace_func_command *cmd)
  1602. {
  1603. struct ftrace_func_command *p;
  1604. int ret = 0;
  1605. mutex_lock(&ftrace_cmd_mutex);
  1606. list_for_each_entry(p, &ftrace_commands, list) {
  1607. if (strcmp(cmd->name, p->name) == 0) {
  1608. ret = -EBUSY;
  1609. goto out_unlock;
  1610. }
  1611. }
  1612. list_add(&cmd->list, &ftrace_commands);
  1613. out_unlock:
  1614. mutex_unlock(&ftrace_cmd_mutex);
  1615. return ret;
  1616. }
  1617. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  1618. {
  1619. struct ftrace_func_command *p, *n;
  1620. int ret = -ENODEV;
  1621. mutex_lock(&ftrace_cmd_mutex);
  1622. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  1623. if (strcmp(cmd->name, p->name) == 0) {
  1624. ret = 0;
  1625. list_del_init(&p->list);
  1626. goto out_unlock;
  1627. }
  1628. }
  1629. out_unlock:
  1630. mutex_unlock(&ftrace_cmd_mutex);
  1631. return ret;
  1632. }
  1633. static int ftrace_process_regex(char *buff, int len, int enable)
  1634. {
  1635. char *func, *command, *next = buff;
  1636. struct ftrace_func_command *p;
  1637. int ret = -EINVAL;
  1638. func = strsep(&next, ":");
  1639. if (!next) {
  1640. ftrace_match_records(func, len, enable);
  1641. return 0;
  1642. }
  1643. /* command found */
  1644. command = strsep(&next, ":");
  1645. mutex_lock(&ftrace_cmd_mutex);
  1646. list_for_each_entry(p, &ftrace_commands, list) {
  1647. if (strcmp(p->name, command) == 0) {
  1648. ret = p->func(func, command, next, enable);
  1649. goto out_unlock;
  1650. }
  1651. }
  1652. out_unlock:
  1653. mutex_unlock(&ftrace_cmd_mutex);
  1654. return ret;
  1655. }
  1656. static ssize_t
  1657. ftrace_regex_write(struct file *file, const char __user *ubuf,
  1658. size_t cnt, loff_t *ppos, int enable)
  1659. {
  1660. struct ftrace_iterator *iter;
  1661. char ch;
  1662. size_t read = 0;
  1663. ssize_t ret;
  1664. if (!cnt || cnt < 0)
  1665. return 0;
  1666. mutex_lock(&ftrace_regex_lock);
  1667. if (file->f_mode & FMODE_READ) {
  1668. struct seq_file *m = file->private_data;
  1669. iter = m->private;
  1670. } else
  1671. iter = file->private_data;
  1672. if (!*ppos) {
  1673. iter->flags &= ~FTRACE_ITER_CONT;
  1674. iter->buffer_idx = 0;
  1675. }
  1676. ret = get_user(ch, ubuf++);
  1677. if (ret)
  1678. goto out;
  1679. read++;
  1680. cnt--;
  1681. if (!(iter->flags & ~FTRACE_ITER_CONT)) {
  1682. /* skip white space */
  1683. while (cnt && isspace(ch)) {
  1684. ret = get_user(ch, ubuf++);
  1685. if (ret)
  1686. goto out;
  1687. read++;
  1688. cnt--;
  1689. }
  1690. if (isspace(ch)) {
  1691. file->f_pos += read;
  1692. ret = read;
  1693. goto out;
  1694. }
  1695. iter->buffer_idx = 0;
  1696. }
  1697. while (cnt && !isspace(ch)) {
  1698. if (iter->buffer_idx < FTRACE_BUFF_MAX)
  1699. iter->buffer[iter->buffer_idx++] = ch;
  1700. else {
  1701. ret = -EINVAL;
  1702. goto out;
  1703. }
  1704. ret = get_user(ch, ubuf++);
  1705. if (ret)
  1706. goto out;
  1707. read++;
  1708. cnt--;
  1709. }
  1710. if (isspace(ch)) {
  1711. iter->filtered++;
  1712. iter->buffer[iter->buffer_idx] = 0;
  1713. ret = ftrace_process_regex(iter->buffer,
  1714. iter->buffer_idx, enable);
  1715. if (ret)
  1716. goto out;
  1717. iter->buffer_idx = 0;
  1718. } else
  1719. iter->flags |= FTRACE_ITER_CONT;
  1720. file->f_pos += read;
  1721. ret = read;
  1722. out:
  1723. mutex_unlock(&ftrace_regex_lock);
  1724. return ret;
  1725. }
  1726. static ssize_t
  1727. ftrace_filter_write(struct file *file, const char __user *ubuf,
  1728. size_t cnt, loff_t *ppos)
  1729. {
  1730. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  1731. }
  1732. static ssize_t
  1733. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  1734. size_t cnt, loff_t *ppos)
  1735. {
  1736. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  1737. }
  1738. static void
  1739. ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
  1740. {
  1741. if (unlikely(ftrace_disabled))
  1742. return;
  1743. mutex_lock(&ftrace_regex_lock);
  1744. if (reset)
  1745. ftrace_filter_reset(enable);
  1746. if (buf)
  1747. ftrace_match_records(buf, len, enable);
  1748. mutex_unlock(&ftrace_regex_lock);
  1749. }
  1750. /**
  1751. * ftrace_set_filter - set a function to filter on in ftrace
  1752. * @buf - the string that holds the function filter text.
  1753. * @len - the length of the string.
  1754. * @reset - non zero to reset all filters before applying this filter.
  1755. *
  1756. * Filters denote which functions should be enabled when tracing is enabled.
  1757. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  1758. */
  1759. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  1760. {
  1761. ftrace_set_regex(buf, len, reset, 1);
  1762. }
  1763. /**
  1764. * ftrace_set_notrace - set a function to not trace in ftrace
  1765. * @buf - the string that holds the function notrace text.
  1766. * @len - the length of the string.
  1767. * @reset - non zero to reset all filters before applying this filter.
  1768. *
  1769. * Notrace Filters denote which functions should not be enabled when tracing
  1770. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  1771. * for tracing.
  1772. */
  1773. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  1774. {
  1775. ftrace_set_regex(buf, len, reset, 0);
  1776. }
  1777. static int
  1778. ftrace_regex_release(struct inode *inode, struct file *file, int enable)
  1779. {
  1780. struct seq_file *m = (struct seq_file *)file->private_data;
  1781. struct ftrace_iterator *iter;
  1782. mutex_lock(&ftrace_regex_lock);
  1783. if (file->f_mode & FMODE_READ) {
  1784. iter = m->private;
  1785. seq_release(inode, file);
  1786. } else
  1787. iter = file->private_data;
  1788. if (iter->buffer_idx) {
  1789. iter->filtered++;
  1790. iter->buffer[iter->buffer_idx] = 0;
  1791. ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
  1792. }
  1793. mutex_lock(&ftrace_lock);
  1794. if (ftrace_start_up && ftrace_enabled)
  1795. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1796. mutex_unlock(&ftrace_lock);
  1797. kfree(iter);
  1798. mutex_unlock(&ftrace_regex_lock);
  1799. return 0;
  1800. }
  1801. static int
  1802. ftrace_filter_release(struct inode *inode, struct file *file)
  1803. {
  1804. return ftrace_regex_release(inode, file, 1);
  1805. }
  1806. static int
  1807. ftrace_notrace_release(struct inode *inode, struct file *file)
  1808. {
  1809. return ftrace_regex_release(inode, file, 0);
  1810. }
  1811. static const struct file_operations ftrace_avail_fops = {
  1812. .open = ftrace_avail_open,
  1813. .read = seq_read,
  1814. .llseek = seq_lseek,
  1815. .release = ftrace_avail_release,
  1816. };
  1817. static const struct file_operations ftrace_failures_fops = {
  1818. .open = ftrace_failures_open,
  1819. .read = seq_read,
  1820. .llseek = seq_lseek,
  1821. .release = ftrace_avail_release,
  1822. };
  1823. static const struct file_operations ftrace_filter_fops = {
  1824. .open = ftrace_filter_open,
  1825. .read = seq_read,
  1826. .write = ftrace_filter_write,
  1827. .llseek = ftrace_regex_lseek,
  1828. .release = ftrace_filter_release,
  1829. };
  1830. static const struct file_operations ftrace_notrace_fops = {
  1831. .open = ftrace_notrace_open,
  1832. .read = seq_read,
  1833. .write = ftrace_notrace_write,
  1834. .llseek = ftrace_regex_lseek,
  1835. .release = ftrace_notrace_release,
  1836. };
  1837. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1838. static DEFINE_MUTEX(graph_lock);
  1839. int ftrace_graph_count;
  1840. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  1841. static void *
  1842. g_next(struct seq_file *m, void *v, loff_t *pos)
  1843. {
  1844. unsigned long *array = m->private;
  1845. int index = *pos;
  1846. (*pos)++;
  1847. if (index >= ftrace_graph_count)
  1848. return NULL;
  1849. return &array[index];
  1850. }
  1851. static void *g_start(struct seq_file *m, loff_t *pos)
  1852. {
  1853. void *p = NULL;
  1854. mutex_lock(&graph_lock);
  1855. /* Nothing, tell g_show to print all functions are enabled */
  1856. if (!ftrace_graph_count && !*pos)
  1857. return (void *)1;
  1858. p = g_next(m, p, pos);
  1859. return p;
  1860. }
  1861. static void g_stop(struct seq_file *m, void *p)
  1862. {
  1863. mutex_unlock(&graph_lock);
  1864. }
  1865. static int g_show(struct seq_file *m, void *v)
  1866. {
  1867. unsigned long *ptr = v;
  1868. char str[KSYM_SYMBOL_LEN];
  1869. if (!ptr)
  1870. return 0;
  1871. if (ptr == (unsigned long *)1) {
  1872. seq_printf(m, "#### all functions enabled ####\n");
  1873. return 0;
  1874. }
  1875. kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
  1876. seq_printf(m, "%s\n", str);
  1877. return 0;
  1878. }
  1879. static struct seq_operations ftrace_graph_seq_ops = {
  1880. .start = g_start,
  1881. .next = g_next,
  1882. .stop = g_stop,
  1883. .show = g_show,
  1884. };
  1885. static int
  1886. ftrace_graph_open(struct inode *inode, struct file *file)
  1887. {
  1888. int ret = 0;
  1889. if (unlikely(ftrace_disabled))
  1890. return -ENODEV;
  1891. mutex_lock(&graph_lock);
  1892. if ((file->f_mode & FMODE_WRITE) &&
  1893. !(file->f_flags & O_APPEND)) {
  1894. ftrace_graph_count = 0;
  1895. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  1896. }
  1897. if (file->f_mode & FMODE_READ) {
  1898. ret = seq_open(file, &ftrace_graph_seq_ops);
  1899. if (!ret) {
  1900. struct seq_file *m = file->private_data;
  1901. m->private = ftrace_graph_funcs;
  1902. }
  1903. } else
  1904. file->private_data = ftrace_graph_funcs;
  1905. mutex_unlock(&graph_lock);
  1906. return ret;
  1907. }
  1908. static int
  1909. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  1910. {
  1911. struct dyn_ftrace *rec;
  1912. struct ftrace_page *pg;
  1913. int search_len;
  1914. int found = 0;
  1915. int type, not;
  1916. char *search;
  1917. bool exists;
  1918. int i;
  1919. if (ftrace_disabled)
  1920. return -ENODEV;
  1921. /* decode regex */
  1922. type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
  1923. if (not)
  1924. return -EINVAL;
  1925. search_len = strlen(search);
  1926. mutex_lock(&ftrace_lock);
  1927. do_for_each_ftrace_rec(pg, rec) {
  1928. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  1929. break;
  1930. if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
  1931. continue;
  1932. if (ftrace_match_record(rec, search, search_len, type)) {
  1933. /* ensure it is not already in the array */
  1934. exists = false;
  1935. for (i = 0; i < *idx; i++)
  1936. if (array[i] == rec->ip) {
  1937. exists = true;
  1938. break;
  1939. }
  1940. if (!exists) {
  1941. array[(*idx)++] = rec->ip;
  1942. found = 1;
  1943. }
  1944. }
  1945. } while_for_each_ftrace_rec();
  1946. mutex_unlock(&ftrace_lock);
  1947. return found ? 0 : -EINVAL;
  1948. }
  1949. static ssize_t
  1950. ftrace_graph_write(struct file *file, const char __user *ubuf,
  1951. size_t cnt, loff_t *ppos)
  1952. {
  1953. unsigned char buffer[FTRACE_BUFF_MAX+1];
  1954. unsigned long *array;
  1955. size_t read = 0;
  1956. ssize_t ret;
  1957. int index = 0;
  1958. char ch;
  1959. if (!cnt || cnt < 0)
  1960. return 0;
  1961. mutex_lock(&graph_lock);
  1962. if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
  1963. ret = -EBUSY;
  1964. goto out;
  1965. }
  1966. if (file->f_mode & FMODE_READ) {
  1967. struct seq_file *m = file->private_data;
  1968. array = m->private;
  1969. } else
  1970. array = file->private_data;
  1971. ret = get_user(ch, ubuf++);
  1972. if (ret)
  1973. goto out;
  1974. read++;
  1975. cnt--;
  1976. /* skip white space */
  1977. while (cnt && isspace(ch)) {
  1978. ret = get_user(ch, ubuf++);
  1979. if (ret)
  1980. goto out;
  1981. read++;
  1982. cnt--;
  1983. }
  1984. if (isspace(ch)) {
  1985. *ppos += read;
  1986. ret = read;
  1987. goto out;
  1988. }
  1989. while (cnt && !isspace(ch)) {
  1990. if (index < FTRACE_BUFF_MAX)
  1991. buffer[index++] = ch;
  1992. else {
  1993. ret = -EINVAL;
  1994. goto out;
  1995. }
  1996. ret = get_user(ch, ubuf++);
  1997. if (ret)
  1998. goto out;
  1999. read++;
  2000. cnt--;
  2001. }
  2002. buffer[index] = 0;
  2003. /* we allow only one expression at a time */
  2004. ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
  2005. if (ret)
  2006. goto out;
  2007. file->f_pos += read;
  2008. ret = read;
  2009. out:
  2010. mutex_unlock(&graph_lock);
  2011. return ret;
  2012. }
  2013. static const struct file_operations ftrace_graph_fops = {
  2014. .open = ftrace_graph_open,
  2015. .read = seq_read,
  2016. .write = ftrace_graph_write,
  2017. };
  2018. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2019. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2020. {
  2021. struct dentry *entry;
  2022. entry = debugfs_create_file("available_filter_functions", 0444,
  2023. d_tracer, NULL, &ftrace_avail_fops);
  2024. if (!entry)
  2025. pr_warning("Could not create debugfs "
  2026. "'available_filter_functions' entry\n");
  2027. entry = debugfs_create_file("failures", 0444,
  2028. d_tracer, NULL, &ftrace_failures_fops);
  2029. if (!entry)
  2030. pr_warning("Could not create debugfs 'failures' entry\n");
  2031. entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
  2032. NULL, &ftrace_filter_fops);
  2033. if (!entry)
  2034. pr_warning("Could not create debugfs "
  2035. "'set_ftrace_filter' entry\n");
  2036. entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
  2037. NULL, &ftrace_notrace_fops);
  2038. if (!entry)
  2039. pr_warning("Could not create debugfs "
  2040. "'set_ftrace_notrace' entry\n");
  2041. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2042. entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
  2043. NULL,
  2044. &ftrace_graph_fops);
  2045. if (!entry)
  2046. pr_warning("Could not create debugfs "
  2047. "'set_graph_function' entry\n");
  2048. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2049. return 0;
  2050. }
  2051. static int ftrace_convert_nops(struct module *mod,
  2052. unsigned long *start,
  2053. unsigned long *end)
  2054. {
  2055. unsigned long *p;
  2056. unsigned long addr;
  2057. unsigned long flags;
  2058. mutex_lock(&ftrace_lock);
  2059. p = start;
  2060. while (p < end) {
  2061. addr = ftrace_call_adjust(*p++);
  2062. /*
  2063. * Some architecture linkers will pad between
  2064. * the different mcount_loc sections of different
  2065. * object files to satisfy alignments.
  2066. * Skip any NULL pointers.
  2067. */
  2068. if (!addr)
  2069. continue;
  2070. ftrace_record_ip(addr);
  2071. }
  2072. /* disable interrupts to prevent kstop machine */
  2073. local_irq_save(flags);
  2074. ftrace_update_code(mod);
  2075. local_irq_restore(flags);
  2076. mutex_unlock(&ftrace_lock);
  2077. return 0;
  2078. }
  2079. void ftrace_init_module(struct module *mod,
  2080. unsigned long *start, unsigned long *end)
  2081. {
  2082. if (ftrace_disabled || start == end)
  2083. return;
  2084. ftrace_convert_nops(mod, start, end);
  2085. }
  2086. extern unsigned long __start_mcount_loc[];
  2087. extern unsigned long __stop_mcount_loc[];
  2088. void __init ftrace_init(void)
  2089. {
  2090. unsigned long count, addr, flags;
  2091. int ret;
  2092. /* Keep the ftrace pointer to the stub */
  2093. addr = (unsigned long)ftrace_stub;
  2094. local_irq_save(flags);
  2095. ftrace_dyn_arch_init(&addr);
  2096. local_irq_restore(flags);
  2097. /* ftrace_dyn_arch_init places the return code in addr */
  2098. if (addr)
  2099. goto failed;
  2100. count = __stop_mcount_loc - __start_mcount_loc;
  2101. ret = ftrace_dyn_table_alloc(count);
  2102. if (ret)
  2103. goto failed;
  2104. last_ftrace_enabled = ftrace_enabled = 1;
  2105. ret = ftrace_convert_nops(NULL,
  2106. __start_mcount_loc,
  2107. __stop_mcount_loc);
  2108. return;
  2109. failed:
  2110. ftrace_disabled = 1;
  2111. }
  2112. #else
  2113. static int __init ftrace_nodyn_init(void)
  2114. {
  2115. ftrace_enabled = 1;
  2116. return 0;
  2117. }
  2118. device_initcall(ftrace_nodyn_init);
  2119. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  2120. static inline void ftrace_startup_enable(int command) { }
  2121. /* Keep as macros so we do not need to define the commands */
  2122. # define ftrace_startup(command) do { } while (0)
  2123. # define ftrace_shutdown(command) do { } while (0)
  2124. # define ftrace_startup_sysctl() do { } while (0)
  2125. # define ftrace_shutdown_sysctl() do { } while (0)
  2126. #endif /* CONFIG_DYNAMIC_FTRACE */
  2127. static ssize_t
  2128. ftrace_pid_read(struct file *file, char __user *ubuf,
  2129. size_t cnt, loff_t *ppos)
  2130. {
  2131. char buf[64];
  2132. int r;
  2133. if (ftrace_pid_trace == ftrace_swapper_pid)
  2134. r = sprintf(buf, "swapper tasks\n");
  2135. else if (ftrace_pid_trace)
  2136. r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
  2137. else
  2138. r = sprintf(buf, "no pid\n");
  2139. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2140. }
  2141. static void clear_ftrace_swapper(void)
  2142. {
  2143. struct task_struct *p;
  2144. int cpu;
  2145. get_online_cpus();
  2146. for_each_online_cpu(cpu) {
  2147. p = idle_task(cpu);
  2148. clear_tsk_trace_trace(p);
  2149. }
  2150. put_online_cpus();
  2151. }
  2152. static void set_ftrace_swapper(void)
  2153. {
  2154. struct task_struct *p;
  2155. int cpu;
  2156. get_online_cpus();
  2157. for_each_online_cpu(cpu) {
  2158. p = idle_task(cpu);
  2159. set_tsk_trace_trace(p);
  2160. }
  2161. put_online_cpus();
  2162. }
  2163. static void clear_ftrace_pid(struct pid *pid)
  2164. {
  2165. struct task_struct *p;
  2166. rcu_read_lock();
  2167. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2168. clear_tsk_trace_trace(p);
  2169. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2170. rcu_read_unlock();
  2171. put_pid(pid);
  2172. }
  2173. static void set_ftrace_pid(struct pid *pid)
  2174. {
  2175. struct task_struct *p;
  2176. rcu_read_lock();
  2177. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2178. set_tsk_trace_trace(p);
  2179. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2180. rcu_read_unlock();
  2181. }
  2182. static void clear_ftrace_pid_task(struct pid **pid)
  2183. {
  2184. if (*pid == ftrace_swapper_pid)
  2185. clear_ftrace_swapper();
  2186. else
  2187. clear_ftrace_pid(*pid);
  2188. *pid = NULL;
  2189. }
  2190. static void set_ftrace_pid_task(struct pid *pid)
  2191. {
  2192. if (pid == ftrace_swapper_pid)
  2193. set_ftrace_swapper();
  2194. else
  2195. set_ftrace_pid(pid);
  2196. }
  2197. static ssize_t
  2198. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  2199. size_t cnt, loff_t *ppos)
  2200. {
  2201. struct pid *pid;
  2202. char buf[64];
  2203. long val;
  2204. int ret;
  2205. if (cnt >= sizeof(buf))
  2206. return -EINVAL;
  2207. if (copy_from_user(&buf, ubuf, cnt))
  2208. return -EFAULT;
  2209. buf[cnt] = 0;
  2210. ret = strict_strtol(buf, 10, &val);
  2211. if (ret < 0)
  2212. return ret;
  2213. mutex_lock(&ftrace_lock);
  2214. if (val < 0) {
  2215. /* disable pid tracing */
  2216. if (!ftrace_pid_trace)
  2217. goto out;
  2218. clear_ftrace_pid_task(&ftrace_pid_trace);
  2219. } else {
  2220. /* swapper task is special */
  2221. if (!val) {
  2222. pid = ftrace_swapper_pid;
  2223. if (pid == ftrace_pid_trace)
  2224. goto out;
  2225. } else {
  2226. pid = find_get_pid(val);
  2227. if (pid == ftrace_pid_trace) {
  2228. put_pid(pid);
  2229. goto out;
  2230. }
  2231. }
  2232. if (ftrace_pid_trace)
  2233. clear_ftrace_pid_task(&ftrace_pid_trace);
  2234. if (!pid)
  2235. goto out;
  2236. ftrace_pid_trace = pid;
  2237. set_ftrace_pid_task(ftrace_pid_trace);
  2238. }
  2239. /* update the function call */
  2240. ftrace_update_pid_func();
  2241. ftrace_startup_enable(0);
  2242. out:
  2243. mutex_unlock(&ftrace_lock);
  2244. return cnt;
  2245. }
  2246. static const struct file_operations ftrace_pid_fops = {
  2247. .read = ftrace_pid_read,
  2248. .write = ftrace_pid_write,
  2249. };
  2250. static __init int ftrace_init_debugfs(void)
  2251. {
  2252. struct dentry *d_tracer;
  2253. struct dentry *entry;
  2254. d_tracer = tracing_init_dentry();
  2255. if (!d_tracer)
  2256. return 0;
  2257. ftrace_init_dyn_debugfs(d_tracer);
  2258. entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
  2259. NULL, &ftrace_pid_fops);
  2260. if (!entry)
  2261. pr_warning("Could not create debugfs "
  2262. "'set_ftrace_pid' entry\n");
  2263. ftrace_profile_debugfs(d_tracer);
  2264. return 0;
  2265. }
  2266. fs_initcall(ftrace_init_debugfs);
  2267. /**
  2268. * ftrace_kill - kill ftrace
  2269. *
  2270. * This function should be used by panic code. It stops ftrace
  2271. * but in a not so nice way. If you need to simply kill ftrace
  2272. * from a non-atomic section, use ftrace_kill.
  2273. */
  2274. void ftrace_kill(void)
  2275. {
  2276. ftrace_disabled = 1;
  2277. ftrace_enabled = 0;
  2278. clear_ftrace_function();
  2279. }
  2280. /**
  2281. * register_ftrace_function - register a function for profiling
  2282. * @ops - ops structure that holds the function for profiling.
  2283. *
  2284. * Register a function to be called by all functions in the
  2285. * kernel.
  2286. *
  2287. * Note: @ops->func and all the functions it calls must be labeled
  2288. * with "notrace", otherwise it will go into a
  2289. * recursive loop.
  2290. */
  2291. int register_ftrace_function(struct ftrace_ops *ops)
  2292. {
  2293. int ret;
  2294. if (unlikely(ftrace_disabled))
  2295. return -1;
  2296. mutex_lock(&ftrace_lock);
  2297. ret = __register_ftrace_function(ops);
  2298. ftrace_startup(0);
  2299. mutex_unlock(&ftrace_lock);
  2300. return ret;
  2301. }
  2302. /**
  2303. * unregister_ftrace_function - unregister a function for profiling.
  2304. * @ops - ops structure that holds the function to unregister
  2305. *
  2306. * Unregister a function that was added to be called by ftrace profiling.
  2307. */
  2308. int unregister_ftrace_function(struct ftrace_ops *ops)
  2309. {
  2310. int ret;
  2311. mutex_lock(&ftrace_lock);
  2312. ret = __unregister_ftrace_function(ops);
  2313. ftrace_shutdown(0);
  2314. mutex_unlock(&ftrace_lock);
  2315. return ret;
  2316. }
  2317. int
  2318. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2319. struct file *file, void __user *buffer, size_t *lenp,
  2320. loff_t *ppos)
  2321. {
  2322. int ret;
  2323. if (unlikely(ftrace_disabled))
  2324. return -ENODEV;
  2325. mutex_lock(&ftrace_lock);
  2326. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  2327. if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
  2328. goto out;
  2329. last_ftrace_enabled = ftrace_enabled;
  2330. if (ftrace_enabled) {
  2331. ftrace_startup_sysctl();
  2332. /* we are starting ftrace again */
  2333. if (ftrace_list != &ftrace_list_end) {
  2334. if (ftrace_list->next == &ftrace_list_end)
  2335. ftrace_trace_function = ftrace_list->func;
  2336. else
  2337. ftrace_trace_function = ftrace_list_func;
  2338. }
  2339. } else {
  2340. /* stopping ftrace calls (just send to ftrace_stub) */
  2341. ftrace_trace_function = ftrace_stub;
  2342. ftrace_shutdown_sysctl();
  2343. }
  2344. out:
  2345. mutex_unlock(&ftrace_lock);
  2346. return ret;
  2347. }
  2348. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2349. static atomic_t ftrace_graph_active;
  2350. static struct notifier_block ftrace_suspend_notifier;
  2351. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2352. {
  2353. return 0;
  2354. }
  2355. /* The callbacks that hook a function */
  2356. trace_func_graph_ret_t ftrace_graph_return =
  2357. (trace_func_graph_ret_t)ftrace_stub;
  2358. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2359. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2360. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2361. {
  2362. int i;
  2363. int ret = 0;
  2364. unsigned long flags;
  2365. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  2366. struct task_struct *g, *t;
  2367. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  2368. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  2369. * sizeof(struct ftrace_ret_stack),
  2370. GFP_KERNEL);
  2371. if (!ret_stack_list[i]) {
  2372. start = 0;
  2373. end = i;
  2374. ret = -ENOMEM;
  2375. goto free;
  2376. }
  2377. }
  2378. read_lock_irqsave(&tasklist_lock, flags);
  2379. do_each_thread(g, t) {
  2380. if (start == end) {
  2381. ret = -EAGAIN;
  2382. goto unlock;
  2383. }
  2384. if (t->ret_stack == NULL) {
  2385. t->curr_ret_stack = -1;
  2386. /* Make sure IRQs see the -1 first: */
  2387. barrier();
  2388. t->ret_stack = ret_stack_list[start++];
  2389. atomic_set(&t->tracing_graph_pause, 0);
  2390. atomic_set(&t->trace_overrun, 0);
  2391. }
  2392. } while_each_thread(g, t);
  2393. unlock:
  2394. read_unlock_irqrestore(&tasklist_lock, flags);
  2395. free:
  2396. for (i = start; i < end; i++)
  2397. kfree(ret_stack_list[i]);
  2398. return ret;
  2399. }
  2400. static void
  2401. ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
  2402. struct task_struct *next)
  2403. {
  2404. unsigned long long timestamp;
  2405. int index;
  2406. /*
  2407. * Does the user want to count the time a function was asleep.
  2408. * If so, do not update the time stamps.
  2409. */
  2410. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  2411. return;
  2412. timestamp = trace_clock_local();
  2413. prev->ftrace_timestamp = timestamp;
  2414. /* only process tasks that we timestamped */
  2415. if (!next->ftrace_timestamp)
  2416. return;
  2417. /*
  2418. * Update all the counters in next to make up for the
  2419. * time next was sleeping.
  2420. */
  2421. timestamp -= next->ftrace_timestamp;
  2422. for (index = next->curr_ret_stack; index >= 0; index--)
  2423. next->ret_stack[index].calltime += timestamp;
  2424. }
  2425. /* Allocate a return stack for each task */
  2426. static int start_graph_tracing(void)
  2427. {
  2428. struct ftrace_ret_stack **ret_stack_list;
  2429. int ret, cpu;
  2430. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  2431. sizeof(struct ftrace_ret_stack *),
  2432. GFP_KERNEL);
  2433. if (!ret_stack_list)
  2434. return -ENOMEM;
  2435. /* The cpu_boot init_task->ret_stack will never be freed */
  2436. for_each_online_cpu(cpu)
  2437. ftrace_graph_init_task(idle_task(cpu));
  2438. do {
  2439. ret = alloc_retstack_tasklist(ret_stack_list);
  2440. } while (ret == -EAGAIN);
  2441. if (!ret) {
  2442. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2443. if (ret)
  2444. pr_info("ftrace_graph: Couldn't activate tracepoint"
  2445. " probe to kernel_sched_switch\n");
  2446. }
  2447. kfree(ret_stack_list);
  2448. return ret;
  2449. }
  2450. /*
  2451. * Hibernation protection.
  2452. * The state of the current task is too much unstable during
  2453. * suspend/restore to disk. We want to protect against that.
  2454. */
  2455. static int
  2456. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  2457. void *unused)
  2458. {
  2459. switch (state) {
  2460. case PM_HIBERNATION_PREPARE:
  2461. pause_graph_tracing();
  2462. break;
  2463. case PM_POST_HIBERNATION:
  2464. unpause_graph_tracing();
  2465. break;
  2466. }
  2467. return NOTIFY_DONE;
  2468. }
  2469. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  2470. trace_func_graph_ent_t entryfunc)
  2471. {
  2472. int ret = 0;
  2473. mutex_lock(&ftrace_lock);
  2474. /* we currently allow only one tracer registered at a time */
  2475. if (atomic_read(&ftrace_graph_active)) {
  2476. ret = -EBUSY;
  2477. goto out;
  2478. }
  2479. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  2480. register_pm_notifier(&ftrace_suspend_notifier);
  2481. atomic_inc(&ftrace_graph_active);
  2482. ret = start_graph_tracing();
  2483. if (ret) {
  2484. atomic_dec(&ftrace_graph_active);
  2485. goto out;
  2486. }
  2487. ftrace_graph_return = retfunc;
  2488. ftrace_graph_entry = entryfunc;
  2489. ftrace_startup(FTRACE_START_FUNC_RET);
  2490. out:
  2491. mutex_unlock(&ftrace_lock);
  2492. return ret;
  2493. }
  2494. void unregister_ftrace_graph(void)
  2495. {
  2496. mutex_lock(&ftrace_lock);
  2497. atomic_dec(&ftrace_graph_active);
  2498. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
  2499. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  2500. ftrace_graph_entry = ftrace_graph_entry_stub;
  2501. ftrace_shutdown(FTRACE_STOP_FUNC_RET);
  2502. unregister_pm_notifier(&ftrace_suspend_notifier);
  2503. mutex_unlock(&ftrace_lock);
  2504. }
  2505. /* Allocate a return stack for newly created task */
  2506. void ftrace_graph_init_task(struct task_struct *t)
  2507. {
  2508. if (atomic_read(&ftrace_graph_active)) {
  2509. t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  2510. * sizeof(struct ftrace_ret_stack),
  2511. GFP_KERNEL);
  2512. if (!t->ret_stack)
  2513. return;
  2514. t->curr_ret_stack = -1;
  2515. atomic_set(&t->tracing_graph_pause, 0);
  2516. atomic_set(&t->trace_overrun, 0);
  2517. t->ftrace_timestamp = 0;
  2518. } else
  2519. t->ret_stack = NULL;
  2520. }
  2521. void ftrace_graph_exit_task(struct task_struct *t)
  2522. {
  2523. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  2524. t->ret_stack = NULL;
  2525. /* NULL must become visible to IRQs before we free it: */
  2526. barrier();
  2527. kfree(ret_stack);
  2528. }
  2529. void ftrace_graph_stop(void)
  2530. {
  2531. ftrace_stop();
  2532. }
  2533. #endif