ftrace.c 90 KB

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