ftrace.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968
  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/ftrace.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/slab.h>
  27. #include <linux/ctype.h>
  28. #include <linux/list.h>
  29. #include <linux/hash.h>
  30. #include <linux/rcupdate.h>
  31. #include <trace/events/sched.h>
  32. #include <asm/ftrace.h>
  33. #include <asm/setup.h>
  34. #include "trace_output.h"
  35. #include "trace_stat.h"
  36. #define FTRACE_WARN_ON(cond) \
  37. ({ \
  38. int ___r = cond; \
  39. if (WARN_ON(___r)) \
  40. ftrace_kill(); \
  41. ___r; \
  42. })
  43. #define FTRACE_WARN_ON_ONCE(cond) \
  44. ({ \
  45. int ___r = cond; \
  46. if (WARN_ON_ONCE(___r)) \
  47. ftrace_kill(); \
  48. ___r; \
  49. })
  50. /* hash bits for specific function selection */
  51. #define FTRACE_HASH_BITS 7
  52. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  53. #define FTRACE_HASH_DEFAULT_BITS 10
  54. #define FTRACE_HASH_MAX_BITS 12
  55. /* ftrace_enabled is a method to turn ftrace on or off */
  56. int ftrace_enabled __read_mostly;
  57. static int last_ftrace_enabled;
  58. /* Quick disabling of function tracer. */
  59. int function_trace_stop;
  60. /* List for set_ftrace_pid's pids. */
  61. LIST_HEAD(ftrace_pids);
  62. struct ftrace_pid {
  63. struct list_head list;
  64. struct pid *pid;
  65. };
  66. /*
  67. * ftrace_disabled is set when an anomaly is discovered.
  68. * ftrace_disabled is much stronger than ftrace_enabled.
  69. */
  70. static int ftrace_disabled __read_mostly;
  71. static DEFINE_MUTEX(ftrace_lock);
  72. static struct ftrace_ops ftrace_list_end __read_mostly =
  73. {
  74. .func = ftrace_stub,
  75. };
  76. static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
  77. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  78. ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
  79. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  80. static struct ftrace_ops global_ops;
  81. /*
  82. * Traverse the ftrace_list, invoking all entries. The reason that we
  83. * can use rcu_dereference_raw() is that elements removed from this list
  84. * are simply leaked, so there is no need to interact with a grace-period
  85. * mechanism. The rcu_dereference_raw() calls are needed to handle
  86. * concurrent insertions into the ftrace_list.
  87. *
  88. * Silly Alpha and silly pointer-speculation compiler optimizations!
  89. */
  90. static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
  91. {
  92. struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
  93. while (op != &ftrace_list_end) {
  94. op->func(ip, parent_ip);
  95. op = rcu_dereference_raw(op->next); /*see above*/
  96. };
  97. }
  98. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
  99. {
  100. if (!test_tsk_trace_trace(current))
  101. return;
  102. ftrace_pid_function(ip, parent_ip);
  103. }
  104. static void set_ftrace_pid_function(ftrace_func_t func)
  105. {
  106. /* do not set ftrace_pid_function to itself! */
  107. if (func != ftrace_pid_func)
  108. ftrace_pid_function = func;
  109. }
  110. /**
  111. * clear_ftrace_function - reset the ftrace function
  112. *
  113. * This NULLs the ftrace function and in essence stops
  114. * tracing. There may be lag
  115. */
  116. void clear_ftrace_function(void)
  117. {
  118. ftrace_trace_function = ftrace_stub;
  119. __ftrace_trace_function = ftrace_stub;
  120. ftrace_pid_function = ftrace_stub;
  121. }
  122. #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  123. /*
  124. * For those archs that do not test ftrace_trace_stop in their
  125. * mcount call site, we need to do it from C.
  126. */
  127. static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
  128. {
  129. if (function_trace_stop)
  130. return;
  131. __ftrace_trace_function(ip, parent_ip);
  132. }
  133. #endif
  134. static void update_global_ops(void)
  135. {
  136. ftrace_func_t func;
  137. /*
  138. * If there's only one function registered, then call that
  139. * function directly. Otherwise, we need to iterate over the
  140. * registered callers.
  141. */
  142. if (ftrace_list == &ftrace_list_end ||
  143. ftrace_list->next == &ftrace_list_end)
  144. func = ftrace_list->func;
  145. else
  146. func = ftrace_list_func;
  147. /* If we filter on pids, update to use the pid function */
  148. if (!list_empty(&ftrace_pids)) {
  149. set_ftrace_pid_function(func);
  150. func = ftrace_pid_func;
  151. }
  152. global_ops.func = func;
  153. }
  154. static void update_ftrace_function(void)
  155. {
  156. ftrace_func_t func;
  157. update_global_ops();
  158. func = global_ops.func;
  159. #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
  160. ftrace_trace_function = func;
  161. #else
  162. __ftrace_trace_function = func;
  163. ftrace_trace_function = ftrace_test_stop_func;
  164. #endif
  165. }
  166. static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  167. {
  168. ops->next = *list;
  169. /*
  170. * We are entering ops into the ftrace_list but another
  171. * CPU might be walking that list. We need to make sure
  172. * the ops->next pointer is valid before another CPU sees
  173. * the ops pointer included into the ftrace_list.
  174. */
  175. rcu_assign_pointer(*list, ops);
  176. }
  177. static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  178. {
  179. struct ftrace_ops **p;
  180. /*
  181. * If we are removing the last function, then simply point
  182. * to the ftrace_stub.
  183. */
  184. if (*list == ops && ops->next == &ftrace_list_end) {
  185. *list = &ftrace_list_end;
  186. return 0;
  187. }
  188. for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
  189. if (*p == ops)
  190. break;
  191. if (*p != ops)
  192. return -1;
  193. *p = (*p)->next;
  194. return 0;
  195. }
  196. static int __register_ftrace_function(struct ftrace_ops *ops)
  197. {
  198. if (ftrace_disabled)
  199. return -ENODEV;
  200. if (FTRACE_WARN_ON(ops == &global_ops))
  201. return -EINVAL;
  202. add_ftrace_ops(&ftrace_list, ops);
  203. if (ftrace_enabled)
  204. update_ftrace_function();
  205. return 0;
  206. }
  207. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  208. {
  209. int ret;
  210. if (ftrace_disabled)
  211. return -ENODEV;
  212. if (FTRACE_WARN_ON(ops == &global_ops))
  213. return -EINVAL;
  214. ret = remove_ftrace_ops(&ftrace_list, ops);
  215. if (ret < 0)
  216. return ret;
  217. if (ftrace_enabled)
  218. update_ftrace_function();
  219. return 0;
  220. }
  221. static void ftrace_update_pid_func(void)
  222. {
  223. /* Only do something if we are tracing something */
  224. if (ftrace_trace_function == ftrace_stub)
  225. return;
  226. update_ftrace_function();
  227. }
  228. #ifdef CONFIG_FUNCTION_PROFILER
  229. struct ftrace_profile {
  230. struct hlist_node node;
  231. unsigned long ip;
  232. unsigned long counter;
  233. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  234. unsigned long long time;
  235. unsigned long long time_squared;
  236. #endif
  237. };
  238. struct ftrace_profile_page {
  239. struct ftrace_profile_page *next;
  240. unsigned long index;
  241. struct ftrace_profile records[];
  242. };
  243. struct ftrace_profile_stat {
  244. atomic_t disabled;
  245. struct hlist_head *hash;
  246. struct ftrace_profile_page *pages;
  247. struct ftrace_profile_page *start;
  248. struct tracer_stat stat;
  249. };
  250. #define PROFILE_RECORDS_SIZE \
  251. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  252. #define PROFILES_PER_PAGE \
  253. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  254. static int ftrace_profile_bits __read_mostly;
  255. static int ftrace_profile_enabled __read_mostly;
  256. /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
  257. static DEFINE_MUTEX(ftrace_profile_lock);
  258. static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
  259. #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
  260. static void *
  261. function_stat_next(void *v, int idx)
  262. {
  263. struct ftrace_profile *rec = v;
  264. struct ftrace_profile_page *pg;
  265. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  266. again:
  267. if (idx != 0)
  268. rec++;
  269. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  270. pg = pg->next;
  271. if (!pg)
  272. return NULL;
  273. rec = &pg->records[0];
  274. if (!rec->counter)
  275. goto again;
  276. }
  277. return rec;
  278. }
  279. static void *function_stat_start(struct tracer_stat *trace)
  280. {
  281. struct ftrace_profile_stat *stat =
  282. container_of(trace, struct ftrace_profile_stat, stat);
  283. if (!stat || !stat->start)
  284. return NULL;
  285. return function_stat_next(&stat->start->records[0], 0);
  286. }
  287. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  288. /* function graph compares on total time */
  289. static int function_stat_cmp(void *p1, void *p2)
  290. {
  291. struct ftrace_profile *a = p1;
  292. struct ftrace_profile *b = p2;
  293. if (a->time < b->time)
  294. return -1;
  295. if (a->time > b->time)
  296. return 1;
  297. else
  298. return 0;
  299. }
  300. #else
  301. /* not function graph compares against hits */
  302. static int function_stat_cmp(void *p1, void *p2)
  303. {
  304. struct ftrace_profile *a = p1;
  305. struct ftrace_profile *b = p2;
  306. if (a->counter < b->counter)
  307. return -1;
  308. if (a->counter > b->counter)
  309. return 1;
  310. else
  311. return 0;
  312. }
  313. #endif
  314. static int function_stat_headers(struct seq_file *m)
  315. {
  316. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  317. seq_printf(m, " Function "
  318. "Hit Time Avg s^2\n"
  319. " -------- "
  320. "--- ---- --- ---\n");
  321. #else
  322. seq_printf(m, " Function Hit\n"
  323. " -------- ---\n");
  324. #endif
  325. return 0;
  326. }
  327. static int function_stat_show(struct seq_file *m, void *v)
  328. {
  329. struct ftrace_profile *rec = v;
  330. char str[KSYM_SYMBOL_LEN];
  331. int ret = 0;
  332. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  333. static struct trace_seq s;
  334. unsigned long long avg;
  335. unsigned long long stddev;
  336. #endif
  337. mutex_lock(&ftrace_profile_lock);
  338. /* we raced with function_profile_reset() */
  339. if (unlikely(rec->counter == 0)) {
  340. ret = -EBUSY;
  341. goto out;
  342. }
  343. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  344. seq_printf(m, " %-30.30s %10lu", str, rec->counter);
  345. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  346. seq_printf(m, " ");
  347. avg = rec->time;
  348. do_div(avg, rec->counter);
  349. /* Sample standard deviation (s^2) */
  350. if (rec->counter <= 1)
  351. stddev = 0;
  352. else {
  353. stddev = rec->time_squared - rec->counter * avg * avg;
  354. /*
  355. * Divide only 1000 for ns^2 -> us^2 conversion.
  356. * trace_print_graph_duration will divide 1000 again.
  357. */
  358. do_div(stddev, (rec->counter - 1) * 1000);
  359. }
  360. trace_seq_init(&s);
  361. trace_print_graph_duration(rec->time, &s);
  362. trace_seq_puts(&s, " ");
  363. trace_print_graph_duration(avg, &s);
  364. trace_seq_puts(&s, " ");
  365. trace_print_graph_duration(stddev, &s);
  366. trace_print_seq(m, &s);
  367. #endif
  368. seq_putc(m, '\n');
  369. out:
  370. mutex_unlock(&ftrace_profile_lock);
  371. return ret;
  372. }
  373. static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
  374. {
  375. struct ftrace_profile_page *pg;
  376. pg = stat->pages = stat->start;
  377. while (pg) {
  378. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  379. pg->index = 0;
  380. pg = pg->next;
  381. }
  382. memset(stat->hash, 0,
  383. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  384. }
  385. int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
  386. {
  387. struct ftrace_profile_page *pg;
  388. int functions;
  389. int pages;
  390. int i;
  391. /* If we already allocated, do nothing */
  392. if (stat->pages)
  393. return 0;
  394. stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
  395. if (!stat->pages)
  396. return -ENOMEM;
  397. #ifdef CONFIG_DYNAMIC_FTRACE
  398. functions = ftrace_update_tot_cnt;
  399. #else
  400. /*
  401. * We do not know the number of functions that exist because
  402. * dynamic tracing is what counts them. With past experience
  403. * we have around 20K functions. That should be more than enough.
  404. * It is highly unlikely we will execute every function in
  405. * the kernel.
  406. */
  407. functions = 20000;
  408. #endif
  409. pg = stat->start = stat->pages;
  410. pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
  411. for (i = 0; i < pages; i++) {
  412. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  413. if (!pg->next)
  414. goto out_free;
  415. pg = pg->next;
  416. }
  417. return 0;
  418. out_free:
  419. pg = stat->start;
  420. while (pg) {
  421. unsigned long tmp = (unsigned long)pg;
  422. pg = pg->next;
  423. free_page(tmp);
  424. }
  425. free_page((unsigned long)stat->pages);
  426. stat->pages = NULL;
  427. stat->start = NULL;
  428. return -ENOMEM;
  429. }
  430. static int ftrace_profile_init_cpu(int cpu)
  431. {
  432. struct ftrace_profile_stat *stat;
  433. int size;
  434. stat = &per_cpu(ftrace_profile_stats, cpu);
  435. if (stat->hash) {
  436. /* If the profile is already created, simply reset it */
  437. ftrace_profile_reset(stat);
  438. return 0;
  439. }
  440. /*
  441. * We are profiling all functions, but usually only a few thousand
  442. * functions are hit. We'll make a hash of 1024 items.
  443. */
  444. size = FTRACE_PROFILE_HASH_SIZE;
  445. stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
  446. if (!stat->hash)
  447. return -ENOMEM;
  448. if (!ftrace_profile_bits) {
  449. size--;
  450. for (; size; size >>= 1)
  451. ftrace_profile_bits++;
  452. }
  453. /* Preallocate the function profiling pages */
  454. if (ftrace_profile_pages_init(stat) < 0) {
  455. kfree(stat->hash);
  456. stat->hash = NULL;
  457. return -ENOMEM;
  458. }
  459. return 0;
  460. }
  461. static int ftrace_profile_init(void)
  462. {
  463. int cpu;
  464. int ret = 0;
  465. for_each_online_cpu(cpu) {
  466. ret = ftrace_profile_init_cpu(cpu);
  467. if (ret)
  468. break;
  469. }
  470. return ret;
  471. }
  472. /* interrupts must be disabled */
  473. static struct ftrace_profile *
  474. ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
  475. {
  476. struct ftrace_profile *rec;
  477. struct hlist_head *hhd;
  478. struct hlist_node *n;
  479. unsigned long key;
  480. key = hash_long(ip, ftrace_profile_bits);
  481. hhd = &stat->hash[key];
  482. if (hlist_empty(hhd))
  483. return NULL;
  484. hlist_for_each_entry_rcu(rec, n, hhd, node) {
  485. if (rec->ip == ip)
  486. return rec;
  487. }
  488. return NULL;
  489. }
  490. static void ftrace_add_profile(struct ftrace_profile_stat *stat,
  491. struct ftrace_profile *rec)
  492. {
  493. unsigned long key;
  494. key = hash_long(rec->ip, ftrace_profile_bits);
  495. hlist_add_head_rcu(&rec->node, &stat->hash[key]);
  496. }
  497. /*
  498. * The memory is already allocated, this simply finds a new record to use.
  499. */
  500. static struct ftrace_profile *
  501. ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
  502. {
  503. struct ftrace_profile *rec = NULL;
  504. /* prevent recursion (from NMIs) */
  505. if (atomic_inc_return(&stat->disabled) != 1)
  506. goto out;
  507. /*
  508. * Try to find the function again since an NMI
  509. * could have added it
  510. */
  511. rec = ftrace_find_profiled_func(stat, ip);
  512. if (rec)
  513. goto out;
  514. if (stat->pages->index == PROFILES_PER_PAGE) {
  515. if (!stat->pages->next)
  516. goto out;
  517. stat->pages = stat->pages->next;
  518. }
  519. rec = &stat->pages->records[stat->pages->index++];
  520. rec->ip = ip;
  521. ftrace_add_profile(stat, rec);
  522. out:
  523. atomic_dec(&stat->disabled);
  524. return rec;
  525. }
  526. static void
  527. function_profile_call(unsigned long ip, unsigned long parent_ip)
  528. {
  529. struct ftrace_profile_stat *stat;
  530. struct ftrace_profile *rec;
  531. unsigned long flags;
  532. if (!ftrace_profile_enabled)
  533. return;
  534. local_irq_save(flags);
  535. stat = &__get_cpu_var(ftrace_profile_stats);
  536. if (!stat->hash || !ftrace_profile_enabled)
  537. goto out;
  538. rec = ftrace_find_profiled_func(stat, ip);
  539. if (!rec) {
  540. rec = ftrace_profile_alloc(stat, ip);
  541. if (!rec)
  542. goto out;
  543. }
  544. rec->counter++;
  545. out:
  546. local_irq_restore(flags);
  547. }
  548. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  549. static int profile_graph_entry(struct ftrace_graph_ent *trace)
  550. {
  551. function_profile_call(trace->func, 0);
  552. return 1;
  553. }
  554. static void profile_graph_return(struct ftrace_graph_ret *trace)
  555. {
  556. struct ftrace_profile_stat *stat;
  557. unsigned long long calltime;
  558. struct ftrace_profile *rec;
  559. unsigned long flags;
  560. local_irq_save(flags);
  561. stat = &__get_cpu_var(ftrace_profile_stats);
  562. if (!stat->hash || !ftrace_profile_enabled)
  563. goto out;
  564. /* If the calltime was zero'd ignore it */
  565. if (!trace->calltime)
  566. goto out;
  567. calltime = trace->rettime - trace->calltime;
  568. if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
  569. int index;
  570. index = trace->depth;
  571. /* Append this call time to the parent time to subtract */
  572. if (index)
  573. current->ret_stack[index - 1].subtime += calltime;
  574. if (current->ret_stack[index].subtime < calltime)
  575. calltime -= current->ret_stack[index].subtime;
  576. else
  577. calltime = 0;
  578. }
  579. rec = ftrace_find_profiled_func(stat, trace->func);
  580. if (rec) {
  581. rec->time += calltime;
  582. rec->time_squared += calltime * calltime;
  583. }
  584. out:
  585. local_irq_restore(flags);
  586. }
  587. static int register_ftrace_profiler(void)
  588. {
  589. return register_ftrace_graph(&profile_graph_return,
  590. &profile_graph_entry);
  591. }
  592. static void unregister_ftrace_profiler(void)
  593. {
  594. unregister_ftrace_graph();
  595. }
  596. #else
  597. static struct ftrace_ops ftrace_profile_ops __read_mostly =
  598. {
  599. .func = function_profile_call,
  600. };
  601. static int register_ftrace_profiler(void)
  602. {
  603. return register_ftrace_function(&ftrace_profile_ops);
  604. }
  605. static void unregister_ftrace_profiler(void)
  606. {
  607. unregister_ftrace_function(&ftrace_profile_ops);
  608. }
  609. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  610. static ssize_t
  611. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  612. size_t cnt, loff_t *ppos)
  613. {
  614. unsigned long val;
  615. char buf[64]; /* big enough to hold a number */
  616. int ret;
  617. if (cnt >= sizeof(buf))
  618. return -EINVAL;
  619. if (copy_from_user(&buf, ubuf, cnt))
  620. return -EFAULT;
  621. buf[cnt] = 0;
  622. ret = strict_strtoul(buf, 10, &val);
  623. if (ret < 0)
  624. return ret;
  625. val = !!val;
  626. mutex_lock(&ftrace_profile_lock);
  627. if (ftrace_profile_enabled ^ val) {
  628. if (val) {
  629. ret = ftrace_profile_init();
  630. if (ret < 0) {
  631. cnt = ret;
  632. goto out;
  633. }
  634. ret = register_ftrace_profiler();
  635. if (ret < 0) {
  636. cnt = ret;
  637. goto out;
  638. }
  639. ftrace_profile_enabled = 1;
  640. } else {
  641. ftrace_profile_enabled = 0;
  642. /*
  643. * unregister_ftrace_profiler calls stop_machine
  644. * so this acts like an synchronize_sched.
  645. */
  646. unregister_ftrace_profiler();
  647. }
  648. }
  649. out:
  650. mutex_unlock(&ftrace_profile_lock);
  651. *ppos += cnt;
  652. return cnt;
  653. }
  654. static ssize_t
  655. ftrace_profile_read(struct file *filp, char __user *ubuf,
  656. size_t cnt, loff_t *ppos)
  657. {
  658. char buf[64]; /* big enough to hold a number */
  659. int r;
  660. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  661. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  662. }
  663. static const struct file_operations ftrace_profile_fops = {
  664. .open = tracing_open_generic,
  665. .read = ftrace_profile_read,
  666. .write = ftrace_profile_write,
  667. .llseek = default_llseek,
  668. };
  669. /* used to initialize the real stat files */
  670. static struct tracer_stat function_stats __initdata = {
  671. .name = "functions",
  672. .stat_start = function_stat_start,
  673. .stat_next = function_stat_next,
  674. .stat_cmp = function_stat_cmp,
  675. .stat_headers = function_stat_headers,
  676. .stat_show = function_stat_show
  677. };
  678. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  679. {
  680. struct ftrace_profile_stat *stat;
  681. struct dentry *entry;
  682. char *name;
  683. int ret;
  684. int cpu;
  685. for_each_possible_cpu(cpu) {
  686. stat = &per_cpu(ftrace_profile_stats, cpu);
  687. /* allocate enough for function name + cpu number */
  688. name = kmalloc(32, GFP_KERNEL);
  689. if (!name) {
  690. /*
  691. * The files created are permanent, if something happens
  692. * we still do not free memory.
  693. */
  694. WARN(1,
  695. "Could not allocate stat file for cpu %d\n",
  696. cpu);
  697. return;
  698. }
  699. stat->stat = function_stats;
  700. snprintf(name, 32, "function%d", cpu);
  701. stat->stat.name = name;
  702. ret = register_stat_tracer(&stat->stat);
  703. if (ret) {
  704. WARN(1,
  705. "Could not register function stat for cpu %d\n",
  706. cpu);
  707. kfree(name);
  708. return;
  709. }
  710. }
  711. entry = debugfs_create_file("function_profile_enabled", 0644,
  712. d_tracer, NULL, &ftrace_profile_fops);
  713. if (!entry)
  714. pr_warning("Could not create debugfs "
  715. "'function_profile_enabled' entry\n");
  716. }
  717. #else /* CONFIG_FUNCTION_PROFILER */
  718. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  719. {
  720. }
  721. #endif /* CONFIG_FUNCTION_PROFILER */
  722. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  723. #ifdef CONFIG_DYNAMIC_FTRACE
  724. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  725. # error Dynamic ftrace depends on MCOUNT_RECORD
  726. #endif
  727. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  728. struct ftrace_func_probe {
  729. struct hlist_node node;
  730. struct ftrace_probe_ops *ops;
  731. unsigned long flags;
  732. unsigned long ip;
  733. void *data;
  734. struct rcu_head rcu;
  735. };
  736. enum {
  737. FTRACE_ENABLE_CALLS = (1 << 0),
  738. FTRACE_DISABLE_CALLS = (1 << 1),
  739. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  740. FTRACE_START_FUNC_RET = (1 << 3),
  741. FTRACE_STOP_FUNC_RET = (1 << 4),
  742. };
  743. struct ftrace_func_entry {
  744. struct hlist_node hlist;
  745. unsigned long ip;
  746. };
  747. struct ftrace_hash {
  748. unsigned long size_bits;
  749. struct hlist_head *buckets;
  750. unsigned long count;
  751. struct rcu_head rcu;
  752. };
  753. /*
  754. * We make these constant because no one should touch them,
  755. * but they are used as the default "empty hash", to avoid allocating
  756. * it all the time. These are in a read only section such that if
  757. * anyone does try to modify it, it will cause an exception.
  758. */
  759. static const struct hlist_head empty_buckets[1];
  760. static const struct ftrace_hash empty_hash = {
  761. .buckets = (struct hlist_head *)empty_buckets,
  762. };
  763. #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
  764. enum {
  765. FTRACE_OPS_FL_ENABLED = 1,
  766. };
  767. static struct ftrace_ops global_ops = {
  768. .func = ftrace_stub,
  769. .notrace_hash = EMPTY_HASH,
  770. .filter_hash = EMPTY_HASH,
  771. };
  772. static struct dyn_ftrace *ftrace_new_addrs;
  773. static DEFINE_MUTEX(ftrace_regex_lock);
  774. struct ftrace_page {
  775. struct ftrace_page *next;
  776. int index;
  777. struct dyn_ftrace records[];
  778. };
  779. #define ENTRIES_PER_PAGE \
  780. ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
  781. /* estimate from running different kernels */
  782. #define NR_TO_INIT 10000
  783. static struct ftrace_page *ftrace_pages_start;
  784. static struct ftrace_page *ftrace_pages;
  785. static struct dyn_ftrace *ftrace_free_records;
  786. static struct ftrace_func_entry *
  787. ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  788. {
  789. unsigned long key;
  790. struct ftrace_func_entry *entry;
  791. struct hlist_head *hhd;
  792. struct hlist_node *n;
  793. if (!hash->count)
  794. return NULL;
  795. if (hash->size_bits > 0)
  796. key = hash_long(ip, hash->size_bits);
  797. else
  798. key = 0;
  799. hhd = &hash->buckets[key];
  800. hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
  801. if (entry->ip == ip)
  802. return entry;
  803. }
  804. return NULL;
  805. }
  806. static void __add_hash_entry(struct ftrace_hash *hash,
  807. struct ftrace_func_entry *entry)
  808. {
  809. struct hlist_head *hhd;
  810. unsigned long key;
  811. if (hash->size_bits)
  812. key = hash_long(entry->ip, hash->size_bits);
  813. else
  814. key = 0;
  815. hhd = &hash->buckets[key];
  816. hlist_add_head(&entry->hlist, hhd);
  817. hash->count++;
  818. }
  819. static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
  820. {
  821. struct ftrace_func_entry *entry;
  822. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  823. if (!entry)
  824. return -ENOMEM;
  825. entry->ip = ip;
  826. __add_hash_entry(hash, entry);
  827. return 0;
  828. }
  829. static void
  830. free_hash_entry(struct ftrace_hash *hash,
  831. struct ftrace_func_entry *entry)
  832. {
  833. hlist_del(&entry->hlist);
  834. kfree(entry);
  835. hash->count--;
  836. }
  837. static void
  838. remove_hash_entry(struct ftrace_hash *hash,
  839. struct ftrace_func_entry *entry)
  840. {
  841. hlist_del(&entry->hlist);
  842. hash->count--;
  843. }
  844. static void ftrace_hash_clear(struct ftrace_hash *hash)
  845. {
  846. struct hlist_head *hhd;
  847. struct hlist_node *tp, *tn;
  848. struct ftrace_func_entry *entry;
  849. int size = 1 << hash->size_bits;
  850. int i;
  851. if (!hash->count)
  852. return;
  853. for (i = 0; i < size; i++) {
  854. hhd = &hash->buckets[i];
  855. hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
  856. free_hash_entry(hash, entry);
  857. }
  858. FTRACE_WARN_ON(hash->count);
  859. }
  860. static void free_ftrace_hash(struct ftrace_hash *hash)
  861. {
  862. if (!hash || hash == EMPTY_HASH)
  863. return;
  864. ftrace_hash_clear(hash);
  865. kfree(hash->buckets);
  866. kfree(hash);
  867. }
  868. static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
  869. {
  870. struct ftrace_hash *hash;
  871. hash = container_of(rcu, struct ftrace_hash, rcu);
  872. free_ftrace_hash(hash);
  873. }
  874. static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
  875. {
  876. if (!hash || hash == EMPTY_HASH)
  877. return;
  878. call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
  879. }
  880. static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
  881. {
  882. struct ftrace_hash *hash;
  883. int size;
  884. hash = kzalloc(sizeof(*hash), GFP_KERNEL);
  885. if (!hash)
  886. return NULL;
  887. size = 1 << size_bits;
  888. hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
  889. if (!hash->buckets) {
  890. kfree(hash);
  891. return NULL;
  892. }
  893. hash->size_bits = size_bits;
  894. return hash;
  895. }
  896. static struct ftrace_hash *
  897. alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
  898. {
  899. struct ftrace_func_entry *entry;
  900. struct ftrace_hash *new_hash;
  901. struct hlist_node *tp;
  902. int size;
  903. int ret;
  904. int i;
  905. new_hash = alloc_ftrace_hash(size_bits);
  906. if (!new_hash)
  907. return NULL;
  908. /* Empty hash? */
  909. if (!hash || !hash->count)
  910. return new_hash;
  911. size = 1 << hash->size_bits;
  912. for (i = 0; i < size; i++) {
  913. hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
  914. ret = add_hash_entry(new_hash, entry->ip);
  915. if (ret < 0)
  916. goto free_hash;
  917. }
  918. }
  919. FTRACE_WARN_ON(new_hash->count != hash->count);
  920. return new_hash;
  921. free_hash:
  922. free_ftrace_hash(new_hash);
  923. return NULL;
  924. }
  925. static int
  926. ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src)
  927. {
  928. struct ftrace_func_entry *entry;
  929. struct hlist_node *tp, *tn;
  930. struct hlist_head *hhd;
  931. struct ftrace_hash *old_hash;
  932. struct ftrace_hash *new_hash;
  933. unsigned long key;
  934. int size = src->count;
  935. int bits = 0;
  936. int i;
  937. /*
  938. * If the new source is empty, just free dst and assign it
  939. * the empty_hash.
  940. */
  941. if (!src->count) {
  942. free_ftrace_hash_rcu(*dst);
  943. rcu_assign_pointer(*dst, EMPTY_HASH);
  944. return 0;
  945. }
  946. /*
  947. * Make the hash size about 1/2 the # found
  948. */
  949. for (size /= 2; size; size >>= 1)
  950. bits++;
  951. /* Don't allocate too much */
  952. if (bits > FTRACE_HASH_MAX_BITS)
  953. bits = FTRACE_HASH_MAX_BITS;
  954. new_hash = alloc_ftrace_hash(bits);
  955. if (!new_hash)
  956. return -ENOMEM;
  957. size = 1 << src->size_bits;
  958. for (i = 0; i < size; i++) {
  959. hhd = &src->buckets[i];
  960. hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
  961. if (bits > 0)
  962. key = hash_long(entry->ip, bits);
  963. else
  964. key = 0;
  965. remove_hash_entry(src, entry);
  966. __add_hash_entry(new_hash, entry);
  967. }
  968. }
  969. old_hash = *dst;
  970. rcu_assign_pointer(*dst, new_hash);
  971. free_ftrace_hash_rcu(old_hash);
  972. return 0;
  973. }
  974. /*
  975. * This is a double for. Do not use 'break' to break out of the loop,
  976. * you must use a goto.
  977. */
  978. #define do_for_each_ftrace_rec(pg, rec) \
  979. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  980. int _____i; \
  981. for (_____i = 0; _____i < pg->index; _____i++) { \
  982. rec = &pg->records[_____i];
  983. #define while_for_each_ftrace_rec() \
  984. } \
  985. }
  986. static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
  987. int filter_hash,
  988. bool inc)
  989. {
  990. struct ftrace_hash *hash;
  991. struct ftrace_hash *other_hash;
  992. struct ftrace_page *pg;
  993. struct dyn_ftrace *rec;
  994. int count = 0;
  995. int all = 0;
  996. /* Only update if the ops has been registered */
  997. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  998. return;
  999. /*
  1000. * In the filter_hash case:
  1001. * If the count is zero, we update all records.
  1002. * Otherwise we just update the items in the hash.
  1003. *
  1004. * In the notrace_hash case:
  1005. * We enable the update in the hash.
  1006. * As disabling notrace means enabling the tracing,
  1007. * and enabling notrace means disabling, the inc variable
  1008. * gets inversed.
  1009. */
  1010. if (filter_hash) {
  1011. hash = ops->filter_hash;
  1012. other_hash = ops->notrace_hash;
  1013. if (!hash->count)
  1014. all = 1;
  1015. } else {
  1016. inc = !inc;
  1017. hash = ops->notrace_hash;
  1018. other_hash = ops->filter_hash;
  1019. /*
  1020. * If the notrace hash has no items,
  1021. * then there's nothing to do.
  1022. */
  1023. if (!hash->count)
  1024. return;
  1025. }
  1026. do_for_each_ftrace_rec(pg, rec) {
  1027. int in_other_hash = 0;
  1028. int in_hash = 0;
  1029. int match = 0;
  1030. if (all) {
  1031. /*
  1032. * Only the filter_hash affects all records.
  1033. * Update if the record is not in the notrace hash.
  1034. */
  1035. if (!ftrace_lookup_ip(other_hash, rec->ip))
  1036. match = 1;
  1037. } else {
  1038. in_hash = !!ftrace_lookup_ip(hash, rec->ip);
  1039. in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
  1040. /*
  1041. *
  1042. */
  1043. if (filter_hash && in_hash && !in_other_hash)
  1044. match = 1;
  1045. else if (!filter_hash && in_hash &&
  1046. (in_other_hash || !other_hash->count))
  1047. match = 1;
  1048. }
  1049. if (!match)
  1050. continue;
  1051. if (inc) {
  1052. rec->flags++;
  1053. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
  1054. return;
  1055. } else {
  1056. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
  1057. return;
  1058. rec->flags--;
  1059. }
  1060. count++;
  1061. /* Shortcut, if we handled all records, we are done. */
  1062. if (!all && count == hash->count)
  1063. return;
  1064. } while_for_each_ftrace_rec();
  1065. }
  1066. static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
  1067. int filter_hash)
  1068. {
  1069. __ftrace_hash_rec_update(ops, filter_hash, 0);
  1070. }
  1071. static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
  1072. int filter_hash)
  1073. {
  1074. __ftrace_hash_rec_update(ops, filter_hash, 1);
  1075. }
  1076. static void ftrace_free_rec(struct dyn_ftrace *rec)
  1077. {
  1078. rec->freelist = ftrace_free_records;
  1079. ftrace_free_records = rec;
  1080. rec->flags |= FTRACE_FL_FREE;
  1081. }
  1082. static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
  1083. {
  1084. struct dyn_ftrace *rec;
  1085. /* First check for freed records */
  1086. if (ftrace_free_records) {
  1087. rec = ftrace_free_records;
  1088. if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
  1089. FTRACE_WARN_ON_ONCE(1);
  1090. ftrace_free_records = NULL;
  1091. return NULL;
  1092. }
  1093. ftrace_free_records = rec->freelist;
  1094. memset(rec, 0, sizeof(*rec));
  1095. return rec;
  1096. }
  1097. if (ftrace_pages->index == ENTRIES_PER_PAGE) {
  1098. if (!ftrace_pages->next) {
  1099. /* allocate another page */
  1100. ftrace_pages->next =
  1101. (void *)get_zeroed_page(GFP_KERNEL);
  1102. if (!ftrace_pages->next)
  1103. return NULL;
  1104. }
  1105. ftrace_pages = ftrace_pages->next;
  1106. }
  1107. return &ftrace_pages->records[ftrace_pages->index++];
  1108. }
  1109. static struct dyn_ftrace *
  1110. ftrace_record_ip(unsigned long ip)
  1111. {
  1112. struct dyn_ftrace *rec;
  1113. if (ftrace_disabled)
  1114. return NULL;
  1115. rec = ftrace_alloc_dyn_node(ip);
  1116. if (!rec)
  1117. return NULL;
  1118. rec->ip = ip;
  1119. rec->newlist = ftrace_new_addrs;
  1120. ftrace_new_addrs = rec;
  1121. return rec;
  1122. }
  1123. static void print_ip_ins(const char *fmt, unsigned char *p)
  1124. {
  1125. int i;
  1126. printk(KERN_CONT "%s", fmt);
  1127. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  1128. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  1129. }
  1130. static void ftrace_bug(int failed, unsigned long ip)
  1131. {
  1132. switch (failed) {
  1133. case -EFAULT:
  1134. FTRACE_WARN_ON_ONCE(1);
  1135. pr_info("ftrace faulted on modifying ");
  1136. print_ip_sym(ip);
  1137. break;
  1138. case -EINVAL:
  1139. FTRACE_WARN_ON_ONCE(1);
  1140. pr_info("ftrace failed to modify ");
  1141. print_ip_sym(ip);
  1142. print_ip_ins(" actual: ", (unsigned char *)ip);
  1143. printk(KERN_CONT "\n");
  1144. break;
  1145. case -EPERM:
  1146. FTRACE_WARN_ON_ONCE(1);
  1147. pr_info("ftrace faulted on writing ");
  1148. print_ip_sym(ip);
  1149. break;
  1150. default:
  1151. FTRACE_WARN_ON_ONCE(1);
  1152. pr_info("ftrace faulted on unknown error ");
  1153. print_ip_sym(ip);
  1154. }
  1155. }
  1156. /* Return 1 if the address range is reserved for ftrace */
  1157. int ftrace_text_reserved(void *start, void *end)
  1158. {
  1159. struct dyn_ftrace *rec;
  1160. struct ftrace_page *pg;
  1161. do_for_each_ftrace_rec(pg, rec) {
  1162. if (rec->ip <= (unsigned long)end &&
  1163. rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
  1164. return 1;
  1165. } while_for_each_ftrace_rec();
  1166. return 0;
  1167. }
  1168. static int
  1169. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  1170. {
  1171. unsigned long ftrace_addr;
  1172. unsigned long flag = 0UL;
  1173. ftrace_addr = (unsigned long)FTRACE_ADDR;
  1174. /*
  1175. * If we are enabling tracing:
  1176. *
  1177. * If the record has a ref count, then we need to enable it
  1178. * because someone is using it.
  1179. *
  1180. * Otherwise we make sure its disabled.
  1181. *
  1182. * If we are disabling tracing, then disable all records that
  1183. * are enabled.
  1184. */
  1185. if (enable && (rec->flags & ~FTRACE_FL_MASK))
  1186. flag = FTRACE_FL_ENABLED;
  1187. /* If the state of this record hasn't changed, then do nothing */
  1188. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1189. return 0;
  1190. if (flag) {
  1191. rec->flags |= FTRACE_FL_ENABLED;
  1192. return ftrace_make_call(rec, ftrace_addr);
  1193. }
  1194. rec->flags &= ~FTRACE_FL_ENABLED;
  1195. return ftrace_make_nop(NULL, rec, ftrace_addr);
  1196. }
  1197. static void ftrace_replace_code(int enable)
  1198. {
  1199. struct dyn_ftrace *rec;
  1200. struct ftrace_page *pg;
  1201. int failed;
  1202. if (unlikely(ftrace_disabled))
  1203. return;
  1204. do_for_each_ftrace_rec(pg, rec) {
  1205. /* Skip over free records */
  1206. if (rec->flags & FTRACE_FL_FREE)
  1207. continue;
  1208. failed = __ftrace_replace_code(rec, enable);
  1209. if (failed) {
  1210. ftrace_bug(failed, rec->ip);
  1211. /* Stop processing */
  1212. return;
  1213. }
  1214. } while_for_each_ftrace_rec();
  1215. }
  1216. static int
  1217. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  1218. {
  1219. unsigned long ip;
  1220. int ret;
  1221. ip = rec->ip;
  1222. if (unlikely(ftrace_disabled))
  1223. return 0;
  1224. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  1225. if (ret) {
  1226. ftrace_bug(ret, ip);
  1227. return 0;
  1228. }
  1229. return 1;
  1230. }
  1231. /*
  1232. * archs can override this function if they must do something
  1233. * before the modifying code is performed.
  1234. */
  1235. int __weak ftrace_arch_code_modify_prepare(void)
  1236. {
  1237. return 0;
  1238. }
  1239. /*
  1240. * archs can override this function if they must do something
  1241. * after the modifying code is performed.
  1242. */
  1243. int __weak ftrace_arch_code_modify_post_process(void)
  1244. {
  1245. return 0;
  1246. }
  1247. static int __ftrace_modify_code(void *data)
  1248. {
  1249. int *command = data;
  1250. if (*command & FTRACE_ENABLE_CALLS)
  1251. ftrace_replace_code(1);
  1252. else if (*command & FTRACE_DISABLE_CALLS)
  1253. ftrace_replace_code(0);
  1254. if (*command & FTRACE_UPDATE_TRACE_FUNC)
  1255. ftrace_update_ftrace_func(ftrace_trace_function);
  1256. if (*command & FTRACE_START_FUNC_RET)
  1257. ftrace_enable_ftrace_graph_caller();
  1258. else if (*command & FTRACE_STOP_FUNC_RET)
  1259. ftrace_disable_ftrace_graph_caller();
  1260. return 0;
  1261. }
  1262. static void ftrace_run_update_code(int command)
  1263. {
  1264. int ret;
  1265. ret = ftrace_arch_code_modify_prepare();
  1266. FTRACE_WARN_ON(ret);
  1267. if (ret)
  1268. return;
  1269. stop_machine(__ftrace_modify_code, &command, NULL);
  1270. ret = ftrace_arch_code_modify_post_process();
  1271. FTRACE_WARN_ON(ret);
  1272. }
  1273. static ftrace_func_t saved_ftrace_func;
  1274. static int ftrace_start_up;
  1275. static void ftrace_startup_enable(int command)
  1276. {
  1277. if (saved_ftrace_func != ftrace_trace_function) {
  1278. saved_ftrace_func = ftrace_trace_function;
  1279. command |= FTRACE_UPDATE_TRACE_FUNC;
  1280. }
  1281. if (!command || !ftrace_enabled)
  1282. return;
  1283. ftrace_run_update_code(command);
  1284. }
  1285. static void ftrace_startup(struct ftrace_ops *ops, int command)
  1286. {
  1287. if (unlikely(ftrace_disabled))
  1288. return;
  1289. ftrace_start_up++;
  1290. command |= FTRACE_ENABLE_CALLS;
  1291. ops->flags |= FTRACE_OPS_FL_ENABLED;
  1292. if (ftrace_start_up == 1)
  1293. ftrace_hash_rec_enable(ops, 1);
  1294. ftrace_startup_enable(command);
  1295. }
  1296. static void ftrace_shutdown(struct ftrace_ops *ops, int command)
  1297. {
  1298. if (unlikely(ftrace_disabled))
  1299. return;
  1300. ftrace_start_up--;
  1301. /*
  1302. * Just warn in case of unbalance, no need to kill ftrace, it's not
  1303. * critical but the ftrace_call callers may be never nopped again after
  1304. * further ftrace uses.
  1305. */
  1306. WARN_ON_ONCE(ftrace_start_up < 0);
  1307. if (!ftrace_start_up)
  1308. ftrace_hash_rec_disable(ops, 1);
  1309. if (!ftrace_start_up) {
  1310. command |= FTRACE_DISABLE_CALLS;
  1311. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  1312. }
  1313. if (saved_ftrace_func != ftrace_trace_function) {
  1314. saved_ftrace_func = ftrace_trace_function;
  1315. command |= FTRACE_UPDATE_TRACE_FUNC;
  1316. }
  1317. if (!command || !ftrace_enabled)
  1318. return;
  1319. ftrace_run_update_code(command);
  1320. }
  1321. static void ftrace_startup_sysctl(void)
  1322. {
  1323. if (unlikely(ftrace_disabled))
  1324. return;
  1325. /* Force update next time */
  1326. saved_ftrace_func = NULL;
  1327. /* ftrace_start_up is true if we want ftrace running */
  1328. if (ftrace_start_up)
  1329. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  1330. }
  1331. static void ftrace_shutdown_sysctl(void)
  1332. {
  1333. if (unlikely(ftrace_disabled))
  1334. return;
  1335. /* ftrace_start_up is true if ftrace is running */
  1336. if (ftrace_start_up)
  1337. ftrace_run_update_code(FTRACE_DISABLE_CALLS);
  1338. }
  1339. static cycle_t ftrace_update_time;
  1340. static unsigned long ftrace_update_cnt;
  1341. unsigned long ftrace_update_tot_cnt;
  1342. static int ftrace_update_code(struct module *mod)
  1343. {
  1344. struct dyn_ftrace *p;
  1345. cycle_t start, stop;
  1346. start = ftrace_now(raw_smp_processor_id());
  1347. ftrace_update_cnt = 0;
  1348. while (ftrace_new_addrs) {
  1349. /* If something went wrong, bail without enabling anything */
  1350. if (unlikely(ftrace_disabled))
  1351. return -1;
  1352. p = ftrace_new_addrs;
  1353. ftrace_new_addrs = p->newlist;
  1354. p->flags = 0L;
  1355. /*
  1356. * Do the initial record conversion from mcount jump
  1357. * to the NOP instructions.
  1358. */
  1359. if (!ftrace_code_disable(mod, p)) {
  1360. ftrace_free_rec(p);
  1361. /* Game over */
  1362. break;
  1363. }
  1364. ftrace_update_cnt++;
  1365. /*
  1366. * If the tracing is enabled, go ahead and enable the record.
  1367. *
  1368. * The reason not to enable the record immediatelly is the
  1369. * inherent check of ftrace_make_nop/ftrace_make_call for
  1370. * correct previous instructions. Making first the NOP
  1371. * conversion puts the module to the correct state, thus
  1372. * passing the ftrace_make_call check.
  1373. */
  1374. if (ftrace_start_up) {
  1375. int failed = __ftrace_replace_code(p, 1);
  1376. if (failed) {
  1377. ftrace_bug(failed, p->ip);
  1378. ftrace_free_rec(p);
  1379. }
  1380. }
  1381. }
  1382. stop = ftrace_now(raw_smp_processor_id());
  1383. ftrace_update_time = stop - start;
  1384. ftrace_update_tot_cnt += ftrace_update_cnt;
  1385. return 0;
  1386. }
  1387. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  1388. {
  1389. struct ftrace_page *pg;
  1390. int cnt;
  1391. int i;
  1392. /* allocate a few pages */
  1393. ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
  1394. if (!ftrace_pages_start)
  1395. return -1;
  1396. /*
  1397. * Allocate a few more pages.
  1398. *
  1399. * TODO: have some parser search vmlinux before
  1400. * final linking to find all calls to ftrace.
  1401. * Then we can:
  1402. * a) know how many pages to allocate.
  1403. * and/or
  1404. * b) set up the table then.
  1405. *
  1406. * The dynamic code is still necessary for
  1407. * modules.
  1408. */
  1409. pg = ftrace_pages = ftrace_pages_start;
  1410. cnt = num_to_init / ENTRIES_PER_PAGE;
  1411. pr_info("ftrace: allocating %ld entries in %d pages\n",
  1412. num_to_init, cnt + 1);
  1413. for (i = 0; i < cnt; i++) {
  1414. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  1415. /* If we fail, we'll try later anyway */
  1416. if (!pg->next)
  1417. break;
  1418. pg = pg->next;
  1419. }
  1420. return 0;
  1421. }
  1422. enum {
  1423. FTRACE_ITER_FILTER = (1 << 0),
  1424. FTRACE_ITER_NOTRACE = (1 << 1),
  1425. FTRACE_ITER_PRINTALL = (1 << 2),
  1426. FTRACE_ITER_HASH = (1 << 3),
  1427. FTRACE_ITER_ENABLED = (1 << 4),
  1428. };
  1429. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  1430. struct ftrace_iterator {
  1431. loff_t pos;
  1432. loff_t func_pos;
  1433. struct ftrace_page *pg;
  1434. struct dyn_ftrace *func;
  1435. struct ftrace_func_probe *probe;
  1436. struct trace_parser parser;
  1437. struct ftrace_hash *hash;
  1438. struct ftrace_ops *ops;
  1439. int hidx;
  1440. int idx;
  1441. unsigned flags;
  1442. };
  1443. static void *
  1444. t_hash_next(struct seq_file *m, loff_t *pos)
  1445. {
  1446. struct ftrace_iterator *iter = m->private;
  1447. struct hlist_node *hnd = NULL;
  1448. struct hlist_head *hhd;
  1449. (*pos)++;
  1450. iter->pos = *pos;
  1451. if (iter->probe)
  1452. hnd = &iter->probe->node;
  1453. retry:
  1454. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  1455. return NULL;
  1456. hhd = &ftrace_func_hash[iter->hidx];
  1457. if (hlist_empty(hhd)) {
  1458. iter->hidx++;
  1459. hnd = NULL;
  1460. goto retry;
  1461. }
  1462. if (!hnd)
  1463. hnd = hhd->first;
  1464. else {
  1465. hnd = hnd->next;
  1466. if (!hnd) {
  1467. iter->hidx++;
  1468. goto retry;
  1469. }
  1470. }
  1471. if (WARN_ON_ONCE(!hnd))
  1472. return NULL;
  1473. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  1474. return iter;
  1475. }
  1476. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  1477. {
  1478. struct ftrace_iterator *iter = m->private;
  1479. void *p = NULL;
  1480. loff_t l;
  1481. if (iter->func_pos > *pos)
  1482. return NULL;
  1483. iter->hidx = 0;
  1484. for (l = 0; l <= (*pos - iter->func_pos); ) {
  1485. p = t_hash_next(m, &l);
  1486. if (!p)
  1487. break;
  1488. }
  1489. if (!p)
  1490. return NULL;
  1491. /* Only set this if we have an item */
  1492. iter->flags |= FTRACE_ITER_HASH;
  1493. return iter;
  1494. }
  1495. static int
  1496. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  1497. {
  1498. struct ftrace_func_probe *rec;
  1499. rec = iter->probe;
  1500. if (WARN_ON_ONCE(!rec))
  1501. return -EIO;
  1502. if (rec->ops->print)
  1503. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  1504. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  1505. if (rec->data)
  1506. seq_printf(m, ":%p", rec->data);
  1507. seq_putc(m, '\n');
  1508. return 0;
  1509. }
  1510. static void *
  1511. t_next(struct seq_file *m, void *v, loff_t *pos)
  1512. {
  1513. struct ftrace_iterator *iter = m->private;
  1514. struct ftrace_ops *ops = &global_ops;
  1515. struct dyn_ftrace *rec = NULL;
  1516. if (unlikely(ftrace_disabled))
  1517. return NULL;
  1518. if (iter->flags & FTRACE_ITER_HASH)
  1519. return t_hash_next(m, pos);
  1520. (*pos)++;
  1521. iter->pos = iter->func_pos = *pos;
  1522. if (iter->flags & FTRACE_ITER_PRINTALL)
  1523. return t_hash_start(m, pos);
  1524. retry:
  1525. if (iter->idx >= iter->pg->index) {
  1526. if (iter->pg->next) {
  1527. iter->pg = iter->pg->next;
  1528. iter->idx = 0;
  1529. goto retry;
  1530. }
  1531. } else {
  1532. rec = &iter->pg->records[iter->idx++];
  1533. if ((rec->flags & FTRACE_FL_FREE) ||
  1534. ((iter->flags & FTRACE_ITER_FILTER) &&
  1535. !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
  1536. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  1537. !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
  1538. ((iter->flags & FTRACE_ITER_ENABLED) &&
  1539. !(rec->flags & ~FTRACE_FL_MASK))) {
  1540. rec = NULL;
  1541. goto retry;
  1542. }
  1543. }
  1544. if (!rec)
  1545. return t_hash_start(m, pos);
  1546. iter->func = rec;
  1547. return iter;
  1548. }
  1549. static void reset_iter_read(struct ftrace_iterator *iter)
  1550. {
  1551. iter->pos = 0;
  1552. iter->func_pos = 0;
  1553. iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
  1554. }
  1555. static void *t_start(struct seq_file *m, loff_t *pos)
  1556. {
  1557. struct ftrace_iterator *iter = m->private;
  1558. struct ftrace_ops *ops = &global_ops;
  1559. void *p = NULL;
  1560. loff_t l;
  1561. mutex_lock(&ftrace_lock);
  1562. if (unlikely(ftrace_disabled))
  1563. return NULL;
  1564. /*
  1565. * If an lseek was done, then reset and start from beginning.
  1566. */
  1567. if (*pos < iter->pos)
  1568. reset_iter_read(iter);
  1569. /*
  1570. * For set_ftrace_filter reading, if we have the filter
  1571. * off, we can short cut and just print out that all
  1572. * functions are enabled.
  1573. */
  1574. if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
  1575. if (*pos > 0)
  1576. return t_hash_start(m, pos);
  1577. iter->flags |= FTRACE_ITER_PRINTALL;
  1578. /* reset in case of seek/pread */
  1579. iter->flags &= ~FTRACE_ITER_HASH;
  1580. return iter;
  1581. }
  1582. if (iter->flags & FTRACE_ITER_HASH)
  1583. return t_hash_start(m, pos);
  1584. /*
  1585. * Unfortunately, we need to restart at ftrace_pages_start
  1586. * every time we let go of the ftrace_mutex. This is because
  1587. * those pointers can change without the lock.
  1588. */
  1589. iter->pg = ftrace_pages_start;
  1590. iter->idx = 0;
  1591. for (l = 0; l <= *pos; ) {
  1592. p = t_next(m, p, &l);
  1593. if (!p)
  1594. break;
  1595. }
  1596. if (!p) {
  1597. if (iter->flags & FTRACE_ITER_FILTER)
  1598. return t_hash_start(m, pos);
  1599. return NULL;
  1600. }
  1601. return iter;
  1602. }
  1603. static void t_stop(struct seq_file *m, void *p)
  1604. {
  1605. mutex_unlock(&ftrace_lock);
  1606. }
  1607. static int t_show(struct seq_file *m, void *v)
  1608. {
  1609. struct ftrace_iterator *iter = m->private;
  1610. struct dyn_ftrace *rec;
  1611. if (iter->flags & FTRACE_ITER_HASH)
  1612. return t_hash_show(m, iter);
  1613. if (iter->flags & FTRACE_ITER_PRINTALL) {
  1614. seq_printf(m, "#### all functions enabled ####\n");
  1615. return 0;
  1616. }
  1617. rec = iter->func;
  1618. if (!rec)
  1619. return 0;
  1620. seq_printf(m, "%ps", (void *)rec->ip);
  1621. if (iter->flags & FTRACE_ITER_ENABLED)
  1622. seq_printf(m, " (%ld)",
  1623. rec->flags & ~FTRACE_FL_MASK);
  1624. seq_printf(m, "\n");
  1625. return 0;
  1626. }
  1627. static const struct seq_operations show_ftrace_seq_ops = {
  1628. .start = t_start,
  1629. .next = t_next,
  1630. .stop = t_stop,
  1631. .show = t_show,
  1632. };
  1633. static int
  1634. ftrace_avail_open(struct inode *inode, struct file *file)
  1635. {
  1636. struct ftrace_iterator *iter;
  1637. int ret;
  1638. if (unlikely(ftrace_disabled))
  1639. return -ENODEV;
  1640. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1641. if (!iter)
  1642. return -ENOMEM;
  1643. iter->pg = ftrace_pages_start;
  1644. ret = seq_open(file, &show_ftrace_seq_ops);
  1645. if (!ret) {
  1646. struct seq_file *m = file->private_data;
  1647. m->private = iter;
  1648. } else {
  1649. kfree(iter);
  1650. }
  1651. return ret;
  1652. }
  1653. static int
  1654. ftrace_enabled_open(struct inode *inode, struct file *file)
  1655. {
  1656. struct ftrace_iterator *iter;
  1657. int ret;
  1658. if (unlikely(ftrace_disabled))
  1659. return -ENODEV;
  1660. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1661. if (!iter)
  1662. return -ENOMEM;
  1663. iter->pg = ftrace_pages_start;
  1664. iter->flags = FTRACE_ITER_ENABLED;
  1665. ret = seq_open(file, &show_ftrace_seq_ops);
  1666. if (!ret) {
  1667. struct seq_file *m = file->private_data;
  1668. m->private = iter;
  1669. } else {
  1670. kfree(iter);
  1671. }
  1672. return ret;
  1673. }
  1674. static void ftrace_filter_reset(struct ftrace_hash *hash)
  1675. {
  1676. mutex_lock(&ftrace_lock);
  1677. ftrace_hash_clear(hash);
  1678. mutex_unlock(&ftrace_lock);
  1679. }
  1680. static int
  1681. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  1682. struct inode *inode, struct file *file)
  1683. {
  1684. struct ftrace_iterator *iter;
  1685. struct ftrace_hash *hash;
  1686. int ret = 0;
  1687. if (unlikely(ftrace_disabled))
  1688. return -ENODEV;
  1689. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1690. if (!iter)
  1691. return -ENOMEM;
  1692. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  1693. kfree(iter);
  1694. return -ENOMEM;
  1695. }
  1696. if (flag & FTRACE_ITER_NOTRACE)
  1697. hash = ops->notrace_hash;
  1698. else
  1699. hash = ops->filter_hash;
  1700. iter->ops = ops;
  1701. iter->flags = flag;
  1702. if (file->f_mode & FMODE_WRITE) {
  1703. mutex_lock(&ftrace_lock);
  1704. iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
  1705. mutex_unlock(&ftrace_lock);
  1706. if (!iter->hash) {
  1707. trace_parser_put(&iter->parser);
  1708. kfree(iter);
  1709. return -ENOMEM;
  1710. }
  1711. }
  1712. mutex_lock(&ftrace_regex_lock);
  1713. if ((file->f_mode & FMODE_WRITE) &&
  1714. (file->f_flags & O_TRUNC))
  1715. ftrace_filter_reset(iter->hash);
  1716. if (file->f_mode & FMODE_READ) {
  1717. iter->pg = ftrace_pages_start;
  1718. ret = seq_open(file, &show_ftrace_seq_ops);
  1719. if (!ret) {
  1720. struct seq_file *m = file->private_data;
  1721. m->private = iter;
  1722. } else {
  1723. /* Failed */
  1724. free_ftrace_hash(iter->hash);
  1725. trace_parser_put(&iter->parser);
  1726. kfree(iter);
  1727. }
  1728. } else
  1729. file->private_data = iter;
  1730. mutex_unlock(&ftrace_regex_lock);
  1731. return ret;
  1732. }
  1733. static int
  1734. ftrace_filter_open(struct inode *inode, struct file *file)
  1735. {
  1736. return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
  1737. inode, file);
  1738. }
  1739. static int
  1740. ftrace_notrace_open(struct inode *inode, struct file *file)
  1741. {
  1742. return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
  1743. inode, file);
  1744. }
  1745. static loff_t
  1746. ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
  1747. {
  1748. loff_t ret;
  1749. if (file->f_mode & FMODE_READ)
  1750. ret = seq_lseek(file, offset, origin);
  1751. else
  1752. file->f_pos = ret = 1;
  1753. return ret;
  1754. }
  1755. static int ftrace_match(char *str, char *regex, int len, int type)
  1756. {
  1757. int matched = 0;
  1758. int slen;
  1759. switch (type) {
  1760. case MATCH_FULL:
  1761. if (strcmp(str, regex) == 0)
  1762. matched = 1;
  1763. break;
  1764. case MATCH_FRONT_ONLY:
  1765. if (strncmp(str, regex, len) == 0)
  1766. matched = 1;
  1767. break;
  1768. case MATCH_MIDDLE_ONLY:
  1769. if (strstr(str, regex))
  1770. matched = 1;
  1771. break;
  1772. case MATCH_END_ONLY:
  1773. slen = strlen(str);
  1774. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  1775. matched = 1;
  1776. break;
  1777. }
  1778. return matched;
  1779. }
  1780. static int
  1781. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
  1782. {
  1783. struct ftrace_func_entry *entry;
  1784. int ret = 0;
  1785. entry = ftrace_lookup_ip(hash, rec->ip);
  1786. if (not) {
  1787. /* Do nothing if it doesn't exist */
  1788. if (!entry)
  1789. return 0;
  1790. free_hash_entry(hash, entry);
  1791. } else {
  1792. /* Do nothing if it exists */
  1793. if (entry)
  1794. return 0;
  1795. ret = add_hash_entry(hash, rec->ip);
  1796. }
  1797. return ret;
  1798. }
  1799. static int
  1800. ftrace_match_record(struct dyn_ftrace *rec, char *mod,
  1801. char *regex, int len, int type)
  1802. {
  1803. char str[KSYM_SYMBOL_LEN];
  1804. char *modname;
  1805. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  1806. if (mod) {
  1807. /* module lookup requires matching the module */
  1808. if (!modname || strcmp(modname, mod))
  1809. return 0;
  1810. /* blank search means to match all funcs in the mod */
  1811. if (!len)
  1812. return 1;
  1813. }
  1814. return ftrace_match(str, regex, len, type);
  1815. }
  1816. static int
  1817. match_records(struct ftrace_hash *hash, char *buff,
  1818. int len, char *mod, int not)
  1819. {
  1820. unsigned search_len = 0;
  1821. struct ftrace_page *pg;
  1822. struct dyn_ftrace *rec;
  1823. int type = MATCH_FULL;
  1824. char *search = buff;
  1825. int found = 0;
  1826. int ret;
  1827. if (len) {
  1828. type = filter_parse_regex(buff, len, &search, &not);
  1829. search_len = strlen(search);
  1830. }
  1831. mutex_lock(&ftrace_lock);
  1832. if (unlikely(ftrace_disabled))
  1833. goto out_unlock;
  1834. do_for_each_ftrace_rec(pg, rec) {
  1835. if (ftrace_match_record(rec, mod, search, search_len, type)) {
  1836. ret = enter_record(hash, rec, not);
  1837. if (ret < 0) {
  1838. found = ret;
  1839. goto out_unlock;
  1840. }
  1841. found = 1;
  1842. }
  1843. } while_for_each_ftrace_rec();
  1844. out_unlock:
  1845. mutex_unlock(&ftrace_lock);
  1846. return found;
  1847. }
  1848. static int
  1849. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  1850. {
  1851. return match_records(hash, buff, len, NULL, 0);
  1852. }
  1853. static int
  1854. ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
  1855. {
  1856. int not = 0;
  1857. /* blank or '*' mean the same */
  1858. if (strcmp(buff, "*") == 0)
  1859. buff[0] = 0;
  1860. /* handle the case of 'dont filter this module' */
  1861. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  1862. buff[0] = 0;
  1863. not = 1;
  1864. }
  1865. return match_records(hash, buff, strlen(buff), mod, not);
  1866. }
  1867. /*
  1868. * We register the module command as a template to show others how
  1869. * to register the a command as well.
  1870. */
  1871. static int
  1872. ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
  1873. {
  1874. struct ftrace_ops *ops = &global_ops;
  1875. struct ftrace_hash *hash;
  1876. char *mod;
  1877. int ret = -EINVAL;
  1878. /*
  1879. * cmd == 'mod' because we only registered this func
  1880. * for the 'mod' ftrace_func_command.
  1881. * But if you register one func with multiple commands,
  1882. * you can tell which command was used by the cmd
  1883. * parameter.
  1884. */
  1885. /* we must have a module name */
  1886. if (!param)
  1887. return ret;
  1888. mod = strsep(&param, ":");
  1889. if (!strlen(mod))
  1890. return ret;
  1891. if (enable)
  1892. hash = ops->filter_hash;
  1893. else
  1894. hash = ops->notrace_hash;
  1895. ret = ftrace_match_module_records(hash, func, mod);
  1896. if (!ret)
  1897. ret = -EINVAL;
  1898. if (ret < 0)
  1899. return ret;
  1900. return 0;
  1901. }
  1902. static struct ftrace_func_command ftrace_mod_cmd = {
  1903. .name = "mod",
  1904. .func = ftrace_mod_callback,
  1905. };
  1906. static int __init ftrace_mod_cmd_init(void)
  1907. {
  1908. return register_ftrace_command(&ftrace_mod_cmd);
  1909. }
  1910. device_initcall(ftrace_mod_cmd_init);
  1911. static void
  1912. function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
  1913. {
  1914. struct ftrace_func_probe *entry;
  1915. struct hlist_head *hhd;
  1916. struct hlist_node *n;
  1917. unsigned long key;
  1918. key = hash_long(ip, FTRACE_HASH_BITS);
  1919. hhd = &ftrace_func_hash[key];
  1920. if (hlist_empty(hhd))
  1921. return;
  1922. /*
  1923. * Disable preemption for these calls to prevent a RCU grace
  1924. * period. This syncs the hash iteration and freeing of items
  1925. * on the hash. rcu_read_lock is too dangerous here.
  1926. */
  1927. preempt_disable_notrace();
  1928. hlist_for_each_entry_rcu(entry, n, hhd, node) {
  1929. if (entry->ip == ip)
  1930. entry->ops->func(ip, parent_ip, &entry->data);
  1931. }
  1932. preempt_enable_notrace();
  1933. }
  1934. static struct ftrace_ops trace_probe_ops __read_mostly =
  1935. {
  1936. .func = function_trace_probe_call,
  1937. };
  1938. static int ftrace_probe_registered;
  1939. static void __enable_ftrace_function_probe(void)
  1940. {
  1941. int i;
  1942. if (ftrace_probe_registered)
  1943. return;
  1944. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1945. struct hlist_head *hhd = &ftrace_func_hash[i];
  1946. if (hhd->first)
  1947. break;
  1948. }
  1949. /* Nothing registered? */
  1950. if (i == FTRACE_FUNC_HASHSIZE)
  1951. return;
  1952. __register_ftrace_function(&trace_probe_ops);
  1953. ftrace_startup(&global_ops, 0);
  1954. ftrace_probe_registered = 1;
  1955. }
  1956. static void __disable_ftrace_function_probe(void)
  1957. {
  1958. int i;
  1959. if (!ftrace_probe_registered)
  1960. return;
  1961. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  1962. struct hlist_head *hhd = &ftrace_func_hash[i];
  1963. if (hhd->first)
  1964. return;
  1965. }
  1966. /* no more funcs left */
  1967. __unregister_ftrace_function(&trace_probe_ops);
  1968. ftrace_shutdown(&global_ops, 0);
  1969. ftrace_probe_registered = 0;
  1970. }
  1971. static void ftrace_free_entry_rcu(struct rcu_head *rhp)
  1972. {
  1973. struct ftrace_func_probe *entry =
  1974. container_of(rhp, struct ftrace_func_probe, rcu);
  1975. if (entry->ops->free)
  1976. entry->ops->free(&entry->data);
  1977. kfree(entry);
  1978. }
  1979. int
  1980. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  1981. void *data)
  1982. {
  1983. struct ftrace_func_probe *entry;
  1984. struct ftrace_page *pg;
  1985. struct dyn_ftrace *rec;
  1986. int type, len, not;
  1987. unsigned long key;
  1988. int count = 0;
  1989. char *search;
  1990. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  1991. len = strlen(search);
  1992. /* we do not support '!' for function probes */
  1993. if (WARN_ON(not))
  1994. return -EINVAL;
  1995. mutex_lock(&ftrace_lock);
  1996. if (unlikely(ftrace_disabled))
  1997. goto out_unlock;
  1998. do_for_each_ftrace_rec(pg, rec) {
  1999. if (!ftrace_match_record(rec, NULL, search, len, type))
  2000. continue;
  2001. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2002. if (!entry) {
  2003. /* If we did not process any, then return error */
  2004. if (!count)
  2005. count = -ENOMEM;
  2006. goto out_unlock;
  2007. }
  2008. count++;
  2009. entry->data = data;
  2010. /*
  2011. * The caller might want to do something special
  2012. * for each function we find. We call the callback
  2013. * to give the caller an opportunity to do so.
  2014. */
  2015. if (ops->callback) {
  2016. if (ops->callback(rec->ip, &entry->data) < 0) {
  2017. /* caller does not like this func */
  2018. kfree(entry);
  2019. continue;
  2020. }
  2021. }
  2022. entry->ops = ops;
  2023. entry->ip = rec->ip;
  2024. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  2025. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  2026. } while_for_each_ftrace_rec();
  2027. __enable_ftrace_function_probe();
  2028. out_unlock:
  2029. mutex_unlock(&ftrace_lock);
  2030. return count;
  2031. }
  2032. enum {
  2033. PROBE_TEST_FUNC = 1,
  2034. PROBE_TEST_DATA = 2
  2035. };
  2036. static void
  2037. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2038. void *data, int flags)
  2039. {
  2040. struct ftrace_func_probe *entry;
  2041. struct hlist_node *n, *tmp;
  2042. char str[KSYM_SYMBOL_LEN];
  2043. int type = MATCH_FULL;
  2044. int i, len = 0;
  2045. char *search;
  2046. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  2047. glob = NULL;
  2048. else if (glob) {
  2049. int not;
  2050. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2051. len = strlen(search);
  2052. /* we do not support '!' for function probes */
  2053. if (WARN_ON(not))
  2054. return;
  2055. }
  2056. mutex_lock(&ftrace_lock);
  2057. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2058. struct hlist_head *hhd = &ftrace_func_hash[i];
  2059. hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
  2060. /* break up if statements for readability */
  2061. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  2062. continue;
  2063. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  2064. continue;
  2065. /* do this last, since it is the most expensive */
  2066. if (glob) {
  2067. kallsyms_lookup(entry->ip, NULL, NULL,
  2068. NULL, str);
  2069. if (!ftrace_match(str, glob, len, type))
  2070. continue;
  2071. }
  2072. hlist_del(&entry->node);
  2073. call_rcu(&entry->rcu, ftrace_free_entry_rcu);
  2074. }
  2075. }
  2076. __disable_ftrace_function_probe();
  2077. mutex_unlock(&ftrace_lock);
  2078. }
  2079. void
  2080. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2081. void *data)
  2082. {
  2083. __unregister_ftrace_function_probe(glob, ops, data,
  2084. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  2085. }
  2086. void
  2087. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  2088. {
  2089. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  2090. }
  2091. void unregister_ftrace_function_probe_all(char *glob)
  2092. {
  2093. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  2094. }
  2095. static LIST_HEAD(ftrace_commands);
  2096. static DEFINE_MUTEX(ftrace_cmd_mutex);
  2097. int register_ftrace_command(struct ftrace_func_command *cmd)
  2098. {
  2099. struct ftrace_func_command *p;
  2100. int ret = 0;
  2101. mutex_lock(&ftrace_cmd_mutex);
  2102. list_for_each_entry(p, &ftrace_commands, list) {
  2103. if (strcmp(cmd->name, p->name) == 0) {
  2104. ret = -EBUSY;
  2105. goto out_unlock;
  2106. }
  2107. }
  2108. list_add(&cmd->list, &ftrace_commands);
  2109. out_unlock:
  2110. mutex_unlock(&ftrace_cmd_mutex);
  2111. return ret;
  2112. }
  2113. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  2114. {
  2115. struct ftrace_func_command *p, *n;
  2116. int ret = -ENODEV;
  2117. mutex_lock(&ftrace_cmd_mutex);
  2118. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  2119. if (strcmp(cmd->name, p->name) == 0) {
  2120. ret = 0;
  2121. list_del_init(&p->list);
  2122. goto out_unlock;
  2123. }
  2124. }
  2125. out_unlock:
  2126. mutex_unlock(&ftrace_cmd_mutex);
  2127. return ret;
  2128. }
  2129. static int ftrace_process_regex(struct ftrace_hash *hash,
  2130. char *buff, int len, int enable)
  2131. {
  2132. char *func, *command, *next = buff;
  2133. struct ftrace_func_command *p;
  2134. int ret;
  2135. func = strsep(&next, ":");
  2136. if (!next) {
  2137. ret = ftrace_match_records(hash, func, len);
  2138. if (!ret)
  2139. ret = -EINVAL;
  2140. if (ret < 0)
  2141. return ret;
  2142. return 0;
  2143. }
  2144. /* command found */
  2145. command = strsep(&next, ":");
  2146. mutex_lock(&ftrace_cmd_mutex);
  2147. list_for_each_entry(p, &ftrace_commands, list) {
  2148. if (strcmp(p->name, command) == 0) {
  2149. ret = p->func(func, command, next, enable);
  2150. goto out_unlock;
  2151. }
  2152. }
  2153. out_unlock:
  2154. mutex_unlock(&ftrace_cmd_mutex);
  2155. return ret;
  2156. }
  2157. static ssize_t
  2158. ftrace_regex_write(struct file *file, const char __user *ubuf,
  2159. size_t cnt, loff_t *ppos, int enable)
  2160. {
  2161. struct ftrace_iterator *iter;
  2162. struct trace_parser *parser;
  2163. ssize_t ret, read;
  2164. if (!cnt)
  2165. return 0;
  2166. mutex_lock(&ftrace_regex_lock);
  2167. ret = -ENODEV;
  2168. if (unlikely(ftrace_disabled))
  2169. goto out_unlock;
  2170. if (file->f_mode & FMODE_READ) {
  2171. struct seq_file *m = file->private_data;
  2172. iter = m->private;
  2173. } else
  2174. iter = file->private_data;
  2175. parser = &iter->parser;
  2176. read = trace_get_user(parser, ubuf, cnt, ppos);
  2177. if (read >= 0 && trace_parser_loaded(parser) &&
  2178. !trace_parser_cont(parser)) {
  2179. ret = ftrace_process_regex(iter->hash, parser->buffer,
  2180. parser->idx, enable);
  2181. trace_parser_clear(parser);
  2182. if (ret)
  2183. goto out_unlock;
  2184. }
  2185. ret = read;
  2186. out_unlock:
  2187. mutex_unlock(&ftrace_regex_lock);
  2188. return ret;
  2189. }
  2190. static ssize_t
  2191. ftrace_filter_write(struct file *file, const char __user *ubuf,
  2192. size_t cnt, loff_t *ppos)
  2193. {
  2194. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  2195. }
  2196. static ssize_t
  2197. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  2198. size_t cnt, loff_t *ppos)
  2199. {
  2200. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  2201. }
  2202. static int
  2203. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  2204. int reset, int enable)
  2205. {
  2206. struct ftrace_hash **orig_hash;
  2207. struct ftrace_hash *hash;
  2208. int ret;
  2209. if (unlikely(ftrace_disabled))
  2210. return -ENODEV;
  2211. if (enable)
  2212. orig_hash = &ops->filter_hash;
  2213. else
  2214. orig_hash = &ops->notrace_hash;
  2215. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  2216. if (!hash)
  2217. return -ENOMEM;
  2218. mutex_lock(&ftrace_regex_lock);
  2219. if (reset)
  2220. ftrace_filter_reset(hash);
  2221. if (buf)
  2222. ftrace_match_records(hash, buf, len);
  2223. mutex_lock(&ftrace_lock);
  2224. ret = ftrace_hash_move(orig_hash, hash);
  2225. mutex_unlock(&ftrace_lock);
  2226. mutex_unlock(&ftrace_regex_lock);
  2227. free_ftrace_hash(hash);
  2228. return ret;
  2229. }
  2230. /**
  2231. * ftrace_set_filter - set a function to filter on in ftrace
  2232. * @buf - the string that holds the function filter text.
  2233. * @len - the length of the string.
  2234. * @reset - non zero to reset all filters before applying this filter.
  2235. *
  2236. * Filters denote which functions should be enabled when tracing is enabled.
  2237. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2238. */
  2239. void ftrace_set_filter(unsigned char *buf, int len, int reset)
  2240. {
  2241. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  2242. }
  2243. /**
  2244. * ftrace_set_notrace - set a function to not trace in ftrace
  2245. * @buf - the string that holds the function notrace text.
  2246. * @len - the length of the string.
  2247. * @reset - non zero to reset all filters before applying this filter.
  2248. *
  2249. * Notrace Filters denote which functions should not be enabled when tracing
  2250. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2251. * for tracing.
  2252. */
  2253. void ftrace_set_notrace(unsigned char *buf, int len, int reset)
  2254. {
  2255. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  2256. }
  2257. /*
  2258. * command line interface to allow users to set filters on boot up.
  2259. */
  2260. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  2261. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  2262. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  2263. static int __init set_ftrace_notrace(char *str)
  2264. {
  2265. strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  2266. return 1;
  2267. }
  2268. __setup("ftrace_notrace=", set_ftrace_notrace);
  2269. static int __init set_ftrace_filter(char *str)
  2270. {
  2271. strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  2272. return 1;
  2273. }
  2274. __setup("ftrace_filter=", set_ftrace_filter);
  2275. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2276. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  2277. static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
  2278. static int __init set_graph_function(char *str)
  2279. {
  2280. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  2281. return 1;
  2282. }
  2283. __setup("ftrace_graph_filter=", set_graph_function);
  2284. static void __init set_ftrace_early_graph(char *buf)
  2285. {
  2286. int ret;
  2287. char *func;
  2288. while (buf) {
  2289. func = strsep(&buf, ",");
  2290. /* we allow only one expression at a time */
  2291. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2292. func);
  2293. if (ret)
  2294. printk(KERN_DEBUG "ftrace: function %s not "
  2295. "traceable\n", func);
  2296. }
  2297. }
  2298. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2299. static void __init
  2300. set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  2301. {
  2302. char *func;
  2303. while (buf) {
  2304. func = strsep(&buf, ",");
  2305. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  2306. }
  2307. }
  2308. static void __init set_ftrace_early_filters(void)
  2309. {
  2310. if (ftrace_filter_buf[0])
  2311. set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
  2312. if (ftrace_notrace_buf[0])
  2313. set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
  2314. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2315. if (ftrace_graph_buf[0])
  2316. set_ftrace_early_graph(ftrace_graph_buf);
  2317. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2318. }
  2319. static int
  2320. ftrace_regex_release(struct inode *inode, struct file *file)
  2321. {
  2322. struct seq_file *m = (struct seq_file *)file->private_data;
  2323. struct ftrace_iterator *iter;
  2324. struct ftrace_hash **orig_hash;
  2325. struct trace_parser *parser;
  2326. int filter_hash;
  2327. int ret;
  2328. mutex_lock(&ftrace_regex_lock);
  2329. if (file->f_mode & FMODE_READ) {
  2330. iter = m->private;
  2331. seq_release(inode, file);
  2332. } else
  2333. iter = file->private_data;
  2334. parser = &iter->parser;
  2335. if (trace_parser_loaded(parser)) {
  2336. parser->buffer[parser->idx] = 0;
  2337. ftrace_match_records(iter->hash, parser->buffer, parser->idx);
  2338. }
  2339. trace_parser_put(parser);
  2340. if (file->f_mode & FMODE_WRITE) {
  2341. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  2342. if (filter_hash)
  2343. orig_hash = &iter->ops->filter_hash;
  2344. else
  2345. orig_hash = &iter->ops->notrace_hash;
  2346. mutex_lock(&ftrace_lock);
  2347. /*
  2348. * Remove the current set, update the hash and add
  2349. * them back.
  2350. */
  2351. ftrace_hash_rec_disable(iter->ops, filter_hash);
  2352. ret = ftrace_hash_move(orig_hash, iter->hash);
  2353. if (!ret) {
  2354. ftrace_hash_rec_enable(iter->ops, filter_hash);
  2355. if (iter->ops->flags & FTRACE_OPS_FL_ENABLED
  2356. && ftrace_enabled)
  2357. ftrace_run_update_code(FTRACE_ENABLE_CALLS);
  2358. }
  2359. mutex_unlock(&ftrace_lock);
  2360. }
  2361. free_ftrace_hash(iter->hash);
  2362. kfree(iter);
  2363. mutex_unlock(&ftrace_regex_lock);
  2364. return 0;
  2365. }
  2366. static const struct file_operations ftrace_avail_fops = {
  2367. .open = ftrace_avail_open,
  2368. .read = seq_read,
  2369. .llseek = seq_lseek,
  2370. .release = seq_release_private,
  2371. };
  2372. static const struct file_operations ftrace_enabled_fops = {
  2373. .open = ftrace_enabled_open,
  2374. .read = seq_read,
  2375. .llseek = seq_lseek,
  2376. .release = seq_release_private,
  2377. };
  2378. static const struct file_operations ftrace_filter_fops = {
  2379. .open = ftrace_filter_open,
  2380. .read = seq_read,
  2381. .write = ftrace_filter_write,
  2382. .llseek = ftrace_regex_lseek,
  2383. .release = ftrace_regex_release,
  2384. };
  2385. static const struct file_operations ftrace_notrace_fops = {
  2386. .open = ftrace_notrace_open,
  2387. .read = seq_read,
  2388. .write = ftrace_notrace_write,
  2389. .llseek = ftrace_regex_lseek,
  2390. .release = ftrace_regex_release,
  2391. };
  2392. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2393. static DEFINE_MUTEX(graph_lock);
  2394. int ftrace_graph_count;
  2395. int ftrace_graph_filter_enabled;
  2396. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  2397. static void *
  2398. __g_next(struct seq_file *m, loff_t *pos)
  2399. {
  2400. if (*pos >= ftrace_graph_count)
  2401. return NULL;
  2402. return &ftrace_graph_funcs[*pos];
  2403. }
  2404. static void *
  2405. g_next(struct seq_file *m, void *v, loff_t *pos)
  2406. {
  2407. (*pos)++;
  2408. return __g_next(m, pos);
  2409. }
  2410. static void *g_start(struct seq_file *m, loff_t *pos)
  2411. {
  2412. mutex_lock(&graph_lock);
  2413. /* Nothing, tell g_show to print all functions are enabled */
  2414. if (!ftrace_graph_filter_enabled && !*pos)
  2415. return (void *)1;
  2416. return __g_next(m, pos);
  2417. }
  2418. static void g_stop(struct seq_file *m, void *p)
  2419. {
  2420. mutex_unlock(&graph_lock);
  2421. }
  2422. static int g_show(struct seq_file *m, void *v)
  2423. {
  2424. unsigned long *ptr = v;
  2425. if (!ptr)
  2426. return 0;
  2427. if (ptr == (unsigned long *)1) {
  2428. seq_printf(m, "#### all functions enabled ####\n");
  2429. return 0;
  2430. }
  2431. seq_printf(m, "%ps\n", (void *)*ptr);
  2432. return 0;
  2433. }
  2434. static const struct seq_operations ftrace_graph_seq_ops = {
  2435. .start = g_start,
  2436. .next = g_next,
  2437. .stop = g_stop,
  2438. .show = g_show,
  2439. };
  2440. static int
  2441. ftrace_graph_open(struct inode *inode, struct file *file)
  2442. {
  2443. int ret = 0;
  2444. if (unlikely(ftrace_disabled))
  2445. return -ENODEV;
  2446. mutex_lock(&graph_lock);
  2447. if ((file->f_mode & FMODE_WRITE) &&
  2448. (file->f_flags & O_TRUNC)) {
  2449. ftrace_graph_filter_enabled = 0;
  2450. ftrace_graph_count = 0;
  2451. memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
  2452. }
  2453. mutex_unlock(&graph_lock);
  2454. if (file->f_mode & FMODE_READ)
  2455. ret = seq_open(file, &ftrace_graph_seq_ops);
  2456. return ret;
  2457. }
  2458. static int
  2459. ftrace_graph_release(struct inode *inode, struct file *file)
  2460. {
  2461. if (file->f_mode & FMODE_READ)
  2462. seq_release(inode, file);
  2463. return 0;
  2464. }
  2465. static int
  2466. ftrace_set_func(unsigned long *array, int *idx, char *buffer)
  2467. {
  2468. struct dyn_ftrace *rec;
  2469. struct ftrace_page *pg;
  2470. int search_len;
  2471. int fail = 1;
  2472. int type, not;
  2473. char *search;
  2474. bool exists;
  2475. int i;
  2476. /* decode regex */
  2477. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  2478. if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
  2479. return -EBUSY;
  2480. search_len = strlen(search);
  2481. mutex_lock(&ftrace_lock);
  2482. if (unlikely(ftrace_disabled)) {
  2483. mutex_unlock(&ftrace_lock);
  2484. return -ENODEV;
  2485. }
  2486. do_for_each_ftrace_rec(pg, rec) {
  2487. if (rec->flags & FTRACE_FL_FREE)
  2488. continue;
  2489. if (ftrace_match_record(rec, NULL, search, search_len, type)) {
  2490. /* if it is in the array */
  2491. exists = false;
  2492. for (i = 0; i < *idx; i++) {
  2493. if (array[i] == rec->ip) {
  2494. exists = true;
  2495. break;
  2496. }
  2497. }
  2498. if (!not) {
  2499. fail = 0;
  2500. if (!exists) {
  2501. array[(*idx)++] = rec->ip;
  2502. if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
  2503. goto out;
  2504. }
  2505. } else {
  2506. if (exists) {
  2507. array[i] = array[--(*idx)];
  2508. array[*idx] = 0;
  2509. fail = 0;
  2510. }
  2511. }
  2512. }
  2513. } while_for_each_ftrace_rec();
  2514. out:
  2515. mutex_unlock(&ftrace_lock);
  2516. if (fail)
  2517. return -EINVAL;
  2518. ftrace_graph_filter_enabled = 1;
  2519. return 0;
  2520. }
  2521. static ssize_t
  2522. ftrace_graph_write(struct file *file, const char __user *ubuf,
  2523. size_t cnt, loff_t *ppos)
  2524. {
  2525. struct trace_parser parser;
  2526. ssize_t read, ret;
  2527. if (!cnt)
  2528. return 0;
  2529. mutex_lock(&graph_lock);
  2530. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
  2531. ret = -ENOMEM;
  2532. goto out_unlock;
  2533. }
  2534. read = trace_get_user(&parser, ubuf, cnt, ppos);
  2535. if (read >= 0 && trace_parser_loaded((&parser))) {
  2536. parser.buffer[parser.idx] = 0;
  2537. /* we allow only one expression at a time */
  2538. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  2539. parser.buffer);
  2540. if (ret)
  2541. goto out_free;
  2542. }
  2543. ret = read;
  2544. out_free:
  2545. trace_parser_put(&parser);
  2546. out_unlock:
  2547. mutex_unlock(&graph_lock);
  2548. return ret;
  2549. }
  2550. static const struct file_operations ftrace_graph_fops = {
  2551. .open = ftrace_graph_open,
  2552. .read = seq_read,
  2553. .write = ftrace_graph_write,
  2554. .release = ftrace_graph_release,
  2555. .llseek = seq_lseek,
  2556. };
  2557. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2558. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  2559. {
  2560. trace_create_file("available_filter_functions", 0444,
  2561. d_tracer, NULL, &ftrace_avail_fops);
  2562. trace_create_file("enabled_functions", 0444,
  2563. d_tracer, NULL, &ftrace_enabled_fops);
  2564. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  2565. NULL, &ftrace_filter_fops);
  2566. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  2567. NULL, &ftrace_notrace_fops);
  2568. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2569. trace_create_file("set_graph_function", 0444, d_tracer,
  2570. NULL,
  2571. &ftrace_graph_fops);
  2572. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  2573. return 0;
  2574. }
  2575. static int ftrace_process_locs(struct module *mod,
  2576. unsigned long *start,
  2577. unsigned long *end)
  2578. {
  2579. unsigned long *p;
  2580. unsigned long addr;
  2581. mutex_lock(&ftrace_lock);
  2582. p = start;
  2583. while (p < end) {
  2584. addr = ftrace_call_adjust(*p++);
  2585. /*
  2586. * Some architecture linkers will pad between
  2587. * the different mcount_loc sections of different
  2588. * object files to satisfy alignments.
  2589. * Skip any NULL pointers.
  2590. */
  2591. if (!addr)
  2592. continue;
  2593. ftrace_record_ip(addr);
  2594. }
  2595. ftrace_update_code(mod);
  2596. mutex_unlock(&ftrace_lock);
  2597. return 0;
  2598. }
  2599. #ifdef CONFIG_MODULES
  2600. void ftrace_release_mod(struct module *mod)
  2601. {
  2602. struct dyn_ftrace *rec;
  2603. struct ftrace_page *pg;
  2604. mutex_lock(&ftrace_lock);
  2605. if (ftrace_disabled)
  2606. goto out_unlock;
  2607. do_for_each_ftrace_rec(pg, rec) {
  2608. if (within_module_core(rec->ip, mod)) {
  2609. /*
  2610. * rec->ip is changed in ftrace_free_rec()
  2611. * It should not between s and e if record was freed.
  2612. */
  2613. FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
  2614. ftrace_free_rec(rec);
  2615. }
  2616. } while_for_each_ftrace_rec();
  2617. out_unlock:
  2618. mutex_unlock(&ftrace_lock);
  2619. }
  2620. static void ftrace_init_module(struct module *mod,
  2621. unsigned long *start, unsigned long *end)
  2622. {
  2623. if (ftrace_disabled || start == end)
  2624. return;
  2625. ftrace_process_locs(mod, start, end);
  2626. }
  2627. static int ftrace_module_notify(struct notifier_block *self,
  2628. unsigned long val, void *data)
  2629. {
  2630. struct module *mod = data;
  2631. switch (val) {
  2632. case MODULE_STATE_COMING:
  2633. ftrace_init_module(mod, mod->ftrace_callsites,
  2634. mod->ftrace_callsites +
  2635. mod->num_ftrace_callsites);
  2636. break;
  2637. case MODULE_STATE_GOING:
  2638. ftrace_release_mod(mod);
  2639. break;
  2640. }
  2641. return 0;
  2642. }
  2643. #else
  2644. static int ftrace_module_notify(struct notifier_block *self,
  2645. unsigned long val, void *data)
  2646. {
  2647. return 0;
  2648. }
  2649. #endif /* CONFIG_MODULES */
  2650. struct notifier_block ftrace_module_nb = {
  2651. .notifier_call = ftrace_module_notify,
  2652. .priority = 0,
  2653. };
  2654. extern unsigned long __start_mcount_loc[];
  2655. extern unsigned long __stop_mcount_loc[];
  2656. void __init ftrace_init(void)
  2657. {
  2658. unsigned long count, addr, flags;
  2659. int ret;
  2660. /* Keep the ftrace pointer to the stub */
  2661. addr = (unsigned long)ftrace_stub;
  2662. local_irq_save(flags);
  2663. ftrace_dyn_arch_init(&addr);
  2664. local_irq_restore(flags);
  2665. /* ftrace_dyn_arch_init places the return code in addr */
  2666. if (addr)
  2667. goto failed;
  2668. count = __stop_mcount_loc - __start_mcount_loc;
  2669. ret = ftrace_dyn_table_alloc(count);
  2670. if (ret)
  2671. goto failed;
  2672. last_ftrace_enabled = ftrace_enabled = 1;
  2673. ret = ftrace_process_locs(NULL,
  2674. __start_mcount_loc,
  2675. __stop_mcount_loc);
  2676. ret = register_module_notifier(&ftrace_module_nb);
  2677. if (ret)
  2678. pr_warning("Failed to register trace ftrace module notifier\n");
  2679. set_ftrace_early_filters();
  2680. return;
  2681. failed:
  2682. ftrace_disabled = 1;
  2683. }
  2684. #else
  2685. static struct ftrace_ops global_ops = {
  2686. .func = ftrace_stub,
  2687. };
  2688. static int __init ftrace_nodyn_init(void)
  2689. {
  2690. ftrace_enabled = 1;
  2691. return 0;
  2692. }
  2693. device_initcall(ftrace_nodyn_init);
  2694. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  2695. static inline void ftrace_startup_enable(int command) { }
  2696. /* Keep as macros so we do not need to define the commands */
  2697. # define ftrace_startup(ops, command) do { } while (0)
  2698. # define ftrace_shutdown(ops, command) do { } while (0)
  2699. # define ftrace_startup_sysctl() do { } while (0)
  2700. # define ftrace_shutdown_sysctl() do { } while (0)
  2701. #endif /* CONFIG_DYNAMIC_FTRACE */
  2702. static void clear_ftrace_swapper(void)
  2703. {
  2704. struct task_struct *p;
  2705. int cpu;
  2706. get_online_cpus();
  2707. for_each_online_cpu(cpu) {
  2708. p = idle_task(cpu);
  2709. clear_tsk_trace_trace(p);
  2710. }
  2711. put_online_cpus();
  2712. }
  2713. static void set_ftrace_swapper(void)
  2714. {
  2715. struct task_struct *p;
  2716. int cpu;
  2717. get_online_cpus();
  2718. for_each_online_cpu(cpu) {
  2719. p = idle_task(cpu);
  2720. set_tsk_trace_trace(p);
  2721. }
  2722. put_online_cpus();
  2723. }
  2724. static void clear_ftrace_pid(struct pid *pid)
  2725. {
  2726. struct task_struct *p;
  2727. rcu_read_lock();
  2728. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2729. clear_tsk_trace_trace(p);
  2730. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2731. rcu_read_unlock();
  2732. put_pid(pid);
  2733. }
  2734. static void set_ftrace_pid(struct pid *pid)
  2735. {
  2736. struct task_struct *p;
  2737. rcu_read_lock();
  2738. do_each_pid_task(pid, PIDTYPE_PID, p) {
  2739. set_tsk_trace_trace(p);
  2740. } while_each_pid_task(pid, PIDTYPE_PID, p);
  2741. rcu_read_unlock();
  2742. }
  2743. static void clear_ftrace_pid_task(struct pid *pid)
  2744. {
  2745. if (pid == ftrace_swapper_pid)
  2746. clear_ftrace_swapper();
  2747. else
  2748. clear_ftrace_pid(pid);
  2749. }
  2750. static void set_ftrace_pid_task(struct pid *pid)
  2751. {
  2752. if (pid == ftrace_swapper_pid)
  2753. set_ftrace_swapper();
  2754. else
  2755. set_ftrace_pid(pid);
  2756. }
  2757. static int ftrace_pid_add(int p)
  2758. {
  2759. struct pid *pid;
  2760. struct ftrace_pid *fpid;
  2761. int ret = -EINVAL;
  2762. mutex_lock(&ftrace_lock);
  2763. if (!p)
  2764. pid = ftrace_swapper_pid;
  2765. else
  2766. pid = find_get_pid(p);
  2767. if (!pid)
  2768. goto out;
  2769. ret = 0;
  2770. list_for_each_entry(fpid, &ftrace_pids, list)
  2771. if (fpid->pid == pid)
  2772. goto out_put;
  2773. ret = -ENOMEM;
  2774. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  2775. if (!fpid)
  2776. goto out_put;
  2777. list_add(&fpid->list, &ftrace_pids);
  2778. fpid->pid = pid;
  2779. set_ftrace_pid_task(pid);
  2780. ftrace_update_pid_func();
  2781. ftrace_startup_enable(0);
  2782. mutex_unlock(&ftrace_lock);
  2783. return 0;
  2784. out_put:
  2785. if (pid != ftrace_swapper_pid)
  2786. put_pid(pid);
  2787. out:
  2788. mutex_unlock(&ftrace_lock);
  2789. return ret;
  2790. }
  2791. static void ftrace_pid_reset(void)
  2792. {
  2793. struct ftrace_pid *fpid, *safe;
  2794. mutex_lock(&ftrace_lock);
  2795. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  2796. struct pid *pid = fpid->pid;
  2797. clear_ftrace_pid_task(pid);
  2798. list_del(&fpid->list);
  2799. kfree(fpid);
  2800. }
  2801. ftrace_update_pid_func();
  2802. ftrace_startup_enable(0);
  2803. mutex_unlock(&ftrace_lock);
  2804. }
  2805. static void *fpid_start(struct seq_file *m, loff_t *pos)
  2806. {
  2807. mutex_lock(&ftrace_lock);
  2808. if (list_empty(&ftrace_pids) && (!*pos))
  2809. return (void *) 1;
  2810. return seq_list_start(&ftrace_pids, *pos);
  2811. }
  2812. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  2813. {
  2814. if (v == (void *)1)
  2815. return NULL;
  2816. return seq_list_next(v, &ftrace_pids, pos);
  2817. }
  2818. static void fpid_stop(struct seq_file *m, void *p)
  2819. {
  2820. mutex_unlock(&ftrace_lock);
  2821. }
  2822. static int fpid_show(struct seq_file *m, void *v)
  2823. {
  2824. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  2825. if (v == (void *)1) {
  2826. seq_printf(m, "no pid\n");
  2827. return 0;
  2828. }
  2829. if (fpid->pid == ftrace_swapper_pid)
  2830. seq_printf(m, "swapper tasks\n");
  2831. else
  2832. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  2833. return 0;
  2834. }
  2835. static const struct seq_operations ftrace_pid_sops = {
  2836. .start = fpid_start,
  2837. .next = fpid_next,
  2838. .stop = fpid_stop,
  2839. .show = fpid_show,
  2840. };
  2841. static int
  2842. ftrace_pid_open(struct inode *inode, struct file *file)
  2843. {
  2844. int ret = 0;
  2845. if ((file->f_mode & FMODE_WRITE) &&
  2846. (file->f_flags & O_TRUNC))
  2847. ftrace_pid_reset();
  2848. if (file->f_mode & FMODE_READ)
  2849. ret = seq_open(file, &ftrace_pid_sops);
  2850. return ret;
  2851. }
  2852. static ssize_t
  2853. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  2854. size_t cnt, loff_t *ppos)
  2855. {
  2856. char buf[64], *tmp;
  2857. long val;
  2858. int ret;
  2859. if (cnt >= sizeof(buf))
  2860. return -EINVAL;
  2861. if (copy_from_user(&buf, ubuf, cnt))
  2862. return -EFAULT;
  2863. buf[cnt] = 0;
  2864. /*
  2865. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  2866. * to clean the filter quietly.
  2867. */
  2868. tmp = strstrip(buf);
  2869. if (strlen(tmp) == 0)
  2870. return 1;
  2871. ret = strict_strtol(tmp, 10, &val);
  2872. if (ret < 0)
  2873. return ret;
  2874. ret = ftrace_pid_add(val);
  2875. return ret ? ret : cnt;
  2876. }
  2877. static int
  2878. ftrace_pid_release(struct inode *inode, struct file *file)
  2879. {
  2880. if (file->f_mode & FMODE_READ)
  2881. seq_release(inode, file);
  2882. return 0;
  2883. }
  2884. static const struct file_operations ftrace_pid_fops = {
  2885. .open = ftrace_pid_open,
  2886. .write = ftrace_pid_write,
  2887. .read = seq_read,
  2888. .llseek = seq_lseek,
  2889. .release = ftrace_pid_release,
  2890. };
  2891. static __init int ftrace_init_debugfs(void)
  2892. {
  2893. struct dentry *d_tracer;
  2894. d_tracer = tracing_init_dentry();
  2895. if (!d_tracer)
  2896. return 0;
  2897. ftrace_init_dyn_debugfs(d_tracer);
  2898. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  2899. NULL, &ftrace_pid_fops);
  2900. ftrace_profile_debugfs(d_tracer);
  2901. return 0;
  2902. }
  2903. fs_initcall(ftrace_init_debugfs);
  2904. /**
  2905. * ftrace_kill - kill ftrace
  2906. *
  2907. * This function should be used by panic code. It stops ftrace
  2908. * but in a not so nice way. If you need to simply kill ftrace
  2909. * from a non-atomic section, use ftrace_kill.
  2910. */
  2911. void ftrace_kill(void)
  2912. {
  2913. ftrace_disabled = 1;
  2914. ftrace_enabled = 0;
  2915. clear_ftrace_function();
  2916. }
  2917. /**
  2918. * register_ftrace_function - register a function for profiling
  2919. * @ops - ops structure that holds the function for profiling.
  2920. *
  2921. * Register a function to be called by all functions in the
  2922. * kernel.
  2923. *
  2924. * Note: @ops->func and all the functions it calls must be labeled
  2925. * with "notrace", otherwise it will go into a
  2926. * recursive loop.
  2927. */
  2928. int register_ftrace_function(struct ftrace_ops *ops)
  2929. {
  2930. int ret = -1;
  2931. mutex_lock(&ftrace_lock);
  2932. if (unlikely(ftrace_disabled))
  2933. goto out_unlock;
  2934. ret = __register_ftrace_function(ops);
  2935. ftrace_startup(&global_ops, 0);
  2936. out_unlock:
  2937. mutex_unlock(&ftrace_lock);
  2938. return ret;
  2939. }
  2940. /**
  2941. * unregister_ftrace_function - unregister a function for profiling.
  2942. * @ops - ops structure that holds the function to unregister
  2943. *
  2944. * Unregister a function that was added to be called by ftrace profiling.
  2945. */
  2946. int unregister_ftrace_function(struct ftrace_ops *ops)
  2947. {
  2948. int ret;
  2949. mutex_lock(&ftrace_lock);
  2950. ret = __unregister_ftrace_function(ops);
  2951. ftrace_shutdown(&global_ops, 0);
  2952. mutex_unlock(&ftrace_lock);
  2953. return ret;
  2954. }
  2955. int
  2956. ftrace_enable_sysctl(struct ctl_table *table, int write,
  2957. void __user *buffer, size_t *lenp,
  2958. loff_t *ppos)
  2959. {
  2960. int ret = -ENODEV;
  2961. mutex_lock(&ftrace_lock);
  2962. if (unlikely(ftrace_disabled))
  2963. goto out;
  2964. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2965. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  2966. goto out;
  2967. last_ftrace_enabled = !!ftrace_enabled;
  2968. if (ftrace_enabled) {
  2969. ftrace_startup_sysctl();
  2970. /* we are starting ftrace again */
  2971. if (ftrace_list != &ftrace_list_end) {
  2972. if (ftrace_list->next == &ftrace_list_end)
  2973. ftrace_trace_function = ftrace_list->func;
  2974. else
  2975. ftrace_trace_function = ftrace_list_func;
  2976. }
  2977. } else {
  2978. /* stopping ftrace calls (just send to ftrace_stub) */
  2979. ftrace_trace_function = ftrace_stub;
  2980. ftrace_shutdown_sysctl();
  2981. }
  2982. out:
  2983. mutex_unlock(&ftrace_lock);
  2984. return ret;
  2985. }
  2986. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  2987. static int ftrace_graph_active;
  2988. static struct notifier_block ftrace_suspend_notifier;
  2989. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  2990. {
  2991. return 0;
  2992. }
  2993. /* The callbacks that hook a function */
  2994. trace_func_graph_ret_t ftrace_graph_return =
  2995. (trace_func_graph_ret_t)ftrace_stub;
  2996. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  2997. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  2998. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  2999. {
  3000. int i;
  3001. int ret = 0;
  3002. unsigned long flags;
  3003. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  3004. struct task_struct *g, *t;
  3005. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  3006. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  3007. * sizeof(struct ftrace_ret_stack),
  3008. GFP_KERNEL);
  3009. if (!ret_stack_list[i]) {
  3010. start = 0;
  3011. end = i;
  3012. ret = -ENOMEM;
  3013. goto free;
  3014. }
  3015. }
  3016. read_lock_irqsave(&tasklist_lock, flags);
  3017. do_each_thread(g, t) {
  3018. if (start == end) {
  3019. ret = -EAGAIN;
  3020. goto unlock;
  3021. }
  3022. if (t->ret_stack == NULL) {
  3023. atomic_set(&t->tracing_graph_pause, 0);
  3024. atomic_set(&t->trace_overrun, 0);
  3025. t->curr_ret_stack = -1;
  3026. /* Make sure the tasks see the -1 first: */
  3027. smp_wmb();
  3028. t->ret_stack = ret_stack_list[start++];
  3029. }
  3030. } while_each_thread(g, t);
  3031. unlock:
  3032. read_unlock_irqrestore(&tasklist_lock, flags);
  3033. free:
  3034. for (i = start; i < end; i++)
  3035. kfree(ret_stack_list[i]);
  3036. return ret;
  3037. }
  3038. static void
  3039. ftrace_graph_probe_sched_switch(void *ignore,
  3040. struct task_struct *prev, struct task_struct *next)
  3041. {
  3042. unsigned long long timestamp;
  3043. int index;
  3044. /*
  3045. * Does the user want to count the time a function was asleep.
  3046. * If so, do not update the time stamps.
  3047. */
  3048. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  3049. return;
  3050. timestamp = trace_clock_local();
  3051. prev->ftrace_timestamp = timestamp;
  3052. /* only process tasks that we timestamped */
  3053. if (!next->ftrace_timestamp)
  3054. return;
  3055. /*
  3056. * Update all the counters in next to make up for the
  3057. * time next was sleeping.
  3058. */
  3059. timestamp -= next->ftrace_timestamp;
  3060. for (index = next->curr_ret_stack; index >= 0; index--)
  3061. next->ret_stack[index].calltime += timestamp;
  3062. }
  3063. /* Allocate a return stack for each task */
  3064. static int start_graph_tracing(void)
  3065. {
  3066. struct ftrace_ret_stack **ret_stack_list;
  3067. int ret, cpu;
  3068. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  3069. sizeof(struct ftrace_ret_stack *),
  3070. GFP_KERNEL);
  3071. if (!ret_stack_list)
  3072. return -ENOMEM;
  3073. /* The cpu_boot init_task->ret_stack will never be freed */
  3074. for_each_online_cpu(cpu) {
  3075. if (!idle_task(cpu)->ret_stack)
  3076. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  3077. }
  3078. do {
  3079. ret = alloc_retstack_tasklist(ret_stack_list);
  3080. } while (ret == -EAGAIN);
  3081. if (!ret) {
  3082. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3083. if (ret)
  3084. pr_info("ftrace_graph: Couldn't activate tracepoint"
  3085. " probe to kernel_sched_switch\n");
  3086. }
  3087. kfree(ret_stack_list);
  3088. return ret;
  3089. }
  3090. /*
  3091. * Hibernation protection.
  3092. * The state of the current task is too much unstable during
  3093. * suspend/restore to disk. We want to protect against that.
  3094. */
  3095. static int
  3096. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  3097. void *unused)
  3098. {
  3099. switch (state) {
  3100. case PM_HIBERNATION_PREPARE:
  3101. pause_graph_tracing();
  3102. break;
  3103. case PM_POST_HIBERNATION:
  3104. unpause_graph_tracing();
  3105. break;
  3106. }
  3107. return NOTIFY_DONE;
  3108. }
  3109. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  3110. trace_func_graph_ent_t entryfunc)
  3111. {
  3112. int ret = 0;
  3113. mutex_lock(&ftrace_lock);
  3114. /* we currently allow only one tracer registered at a time */
  3115. if (ftrace_graph_active) {
  3116. ret = -EBUSY;
  3117. goto out;
  3118. }
  3119. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  3120. register_pm_notifier(&ftrace_suspend_notifier);
  3121. ftrace_graph_active++;
  3122. ret = start_graph_tracing();
  3123. if (ret) {
  3124. ftrace_graph_active--;
  3125. goto out;
  3126. }
  3127. ftrace_graph_return = retfunc;
  3128. ftrace_graph_entry = entryfunc;
  3129. ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
  3130. out:
  3131. mutex_unlock(&ftrace_lock);
  3132. return ret;
  3133. }
  3134. void unregister_ftrace_graph(void)
  3135. {
  3136. mutex_lock(&ftrace_lock);
  3137. if (unlikely(!ftrace_graph_active))
  3138. goto out;
  3139. ftrace_graph_active--;
  3140. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  3141. ftrace_graph_entry = ftrace_graph_entry_stub;
  3142. ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
  3143. unregister_pm_notifier(&ftrace_suspend_notifier);
  3144. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  3145. out:
  3146. mutex_unlock(&ftrace_lock);
  3147. }
  3148. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  3149. static void
  3150. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  3151. {
  3152. atomic_set(&t->tracing_graph_pause, 0);
  3153. atomic_set(&t->trace_overrun, 0);
  3154. t->ftrace_timestamp = 0;
  3155. /* make curr_ret_stack visible before we add the ret_stack */
  3156. smp_wmb();
  3157. t->ret_stack = ret_stack;
  3158. }
  3159. /*
  3160. * Allocate a return stack for the idle task. May be the first
  3161. * time through, or it may be done by CPU hotplug online.
  3162. */
  3163. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  3164. {
  3165. t->curr_ret_stack = -1;
  3166. /*
  3167. * The idle task has no parent, it either has its own
  3168. * stack or no stack at all.
  3169. */
  3170. if (t->ret_stack)
  3171. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  3172. if (ftrace_graph_active) {
  3173. struct ftrace_ret_stack *ret_stack;
  3174. ret_stack = per_cpu(idle_ret_stack, cpu);
  3175. if (!ret_stack) {
  3176. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3177. * sizeof(struct ftrace_ret_stack),
  3178. GFP_KERNEL);
  3179. if (!ret_stack)
  3180. return;
  3181. per_cpu(idle_ret_stack, cpu) = ret_stack;
  3182. }
  3183. graph_init_task(t, ret_stack);
  3184. }
  3185. }
  3186. /* Allocate a return stack for newly created task */
  3187. void ftrace_graph_init_task(struct task_struct *t)
  3188. {
  3189. /* Make sure we do not use the parent ret_stack */
  3190. t->ret_stack = NULL;
  3191. t->curr_ret_stack = -1;
  3192. if (ftrace_graph_active) {
  3193. struct ftrace_ret_stack *ret_stack;
  3194. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  3195. * sizeof(struct ftrace_ret_stack),
  3196. GFP_KERNEL);
  3197. if (!ret_stack)
  3198. return;
  3199. graph_init_task(t, ret_stack);
  3200. }
  3201. }
  3202. void ftrace_graph_exit_task(struct task_struct *t)
  3203. {
  3204. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  3205. t->ret_stack = NULL;
  3206. /* NULL must become visible to IRQs before we free it: */
  3207. barrier();
  3208. kfree(ret_stack);
  3209. }
  3210. void ftrace_graph_stop(void)
  3211. {
  3212. ftrace_stop();
  3213. }
  3214. #endif