ftrace.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111
  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 Nadia Yvette Chambers
  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/bsearch.h>
  25. #include <linux/module.h>
  26. #include <linux/ftrace.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/slab.h>
  29. #include <linux/ctype.h>
  30. #include <linux/sort.h>
  31. #include <linux/list.h>
  32. #include <linux/hash.h>
  33. #include <linux/rcupdate.h>
  34. #include <trace/events/sched.h>
  35. #include <asm/setup.h>
  36. #include "trace_output.h"
  37. #include "trace_stat.h"
  38. #define FTRACE_WARN_ON(cond) \
  39. ({ \
  40. int ___r = cond; \
  41. if (WARN_ON(___r)) \
  42. ftrace_kill(); \
  43. ___r; \
  44. })
  45. #define FTRACE_WARN_ON_ONCE(cond) \
  46. ({ \
  47. int ___r = cond; \
  48. if (WARN_ON_ONCE(___r)) \
  49. ftrace_kill(); \
  50. ___r; \
  51. })
  52. /* hash bits for specific function selection */
  53. #define FTRACE_HASH_BITS 7
  54. #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
  55. #define FTRACE_HASH_DEFAULT_BITS 10
  56. #define FTRACE_HASH_MAX_BITS 12
  57. #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
  58. #ifdef CONFIG_DYNAMIC_FTRACE
  59. #define INIT_REGEX_LOCK(opsname) \
  60. .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
  61. #else
  62. #define INIT_REGEX_LOCK(opsname)
  63. #endif
  64. static struct ftrace_ops ftrace_list_end __read_mostly = {
  65. .func = ftrace_stub,
  66. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
  67. };
  68. /* ftrace_enabled is a method to turn ftrace on or off */
  69. int ftrace_enabled __read_mostly;
  70. static int last_ftrace_enabled;
  71. /* Quick disabling of function tracer. */
  72. int function_trace_stop __read_mostly;
  73. /* Current function tracing op */
  74. struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
  75. /* List for set_ftrace_pid's pids. */
  76. LIST_HEAD(ftrace_pids);
  77. struct ftrace_pid {
  78. struct list_head list;
  79. struct pid *pid;
  80. };
  81. /*
  82. * ftrace_disabled is set when an anomaly is discovered.
  83. * ftrace_disabled is much stronger than ftrace_enabled.
  84. */
  85. static int ftrace_disabled __read_mostly;
  86. static DEFINE_MUTEX(ftrace_lock);
  87. static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
  88. static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
  89. static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
  90. ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
  91. ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
  92. static struct ftrace_ops global_ops;
  93. static struct ftrace_ops control_ops;
  94. #if ARCH_SUPPORTS_FTRACE_OPS
  95. static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  96. struct ftrace_ops *op, struct pt_regs *regs);
  97. #else
  98. /* See comment below, where ftrace_ops_list_func is defined */
  99. static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
  100. #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
  101. #endif
  102. /*
  103. * Traverse the ftrace_global_list, invoking all entries. The reason that we
  104. * can use rcu_dereference_raw_notrace() is that elements removed from this list
  105. * are simply leaked, so there is no need to interact with a grace-period
  106. * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
  107. * concurrent insertions into the ftrace_global_list.
  108. *
  109. * Silly Alpha and silly pointer-speculation compiler optimizations!
  110. */
  111. #define do_for_each_ftrace_op(op, list) \
  112. op = rcu_dereference_raw_notrace(list); \
  113. do
  114. /*
  115. * Optimized for just a single item in the list (as that is the normal case).
  116. */
  117. #define while_for_each_ftrace_op(op) \
  118. while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
  119. unlikely((op) != &ftrace_list_end))
  120. static inline void ftrace_ops_init(struct ftrace_ops *ops)
  121. {
  122. #ifdef CONFIG_DYNAMIC_FTRACE
  123. if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
  124. mutex_init(&ops->regex_lock);
  125. ops->flags |= FTRACE_OPS_FL_INITIALIZED;
  126. }
  127. #endif
  128. }
  129. /**
  130. * ftrace_nr_registered_ops - return number of ops registered
  131. *
  132. * Returns the number of ftrace_ops registered and tracing functions
  133. */
  134. int ftrace_nr_registered_ops(void)
  135. {
  136. struct ftrace_ops *ops;
  137. int cnt = 0;
  138. mutex_lock(&ftrace_lock);
  139. for (ops = ftrace_ops_list;
  140. ops != &ftrace_list_end; ops = ops->next)
  141. cnt++;
  142. mutex_unlock(&ftrace_lock);
  143. return cnt;
  144. }
  145. static void
  146. ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
  147. struct ftrace_ops *op, struct pt_regs *regs)
  148. {
  149. int bit;
  150. bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
  151. if (bit < 0)
  152. return;
  153. do_for_each_ftrace_op(op, ftrace_global_list) {
  154. op->func(ip, parent_ip, op, regs);
  155. } while_for_each_ftrace_op(op);
  156. trace_clear_recursion(bit);
  157. }
  158. static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
  159. struct ftrace_ops *op, struct pt_regs *regs)
  160. {
  161. if (!test_tsk_trace_trace(current))
  162. return;
  163. ftrace_pid_function(ip, parent_ip, op, regs);
  164. }
  165. static void set_ftrace_pid_function(ftrace_func_t func)
  166. {
  167. /* do not set ftrace_pid_function to itself! */
  168. if (func != ftrace_pid_func)
  169. ftrace_pid_function = func;
  170. }
  171. /**
  172. * clear_ftrace_function - reset the ftrace function
  173. *
  174. * This NULLs the ftrace function and in essence stops
  175. * tracing. There may be lag
  176. */
  177. void clear_ftrace_function(void)
  178. {
  179. ftrace_trace_function = ftrace_stub;
  180. ftrace_pid_function = ftrace_stub;
  181. }
  182. static void control_ops_disable_all(struct ftrace_ops *ops)
  183. {
  184. int cpu;
  185. for_each_possible_cpu(cpu)
  186. *per_cpu_ptr(ops->disabled, cpu) = 1;
  187. }
  188. static int control_ops_alloc(struct ftrace_ops *ops)
  189. {
  190. int __percpu *disabled;
  191. disabled = alloc_percpu(int);
  192. if (!disabled)
  193. return -ENOMEM;
  194. ops->disabled = disabled;
  195. control_ops_disable_all(ops);
  196. return 0;
  197. }
  198. static void control_ops_free(struct ftrace_ops *ops)
  199. {
  200. free_percpu(ops->disabled);
  201. }
  202. static void update_global_ops(void)
  203. {
  204. ftrace_func_t func;
  205. /*
  206. * If there's only one function registered, then call that
  207. * function directly. Otherwise, we need to iterate over the
  208. * registered callers.
  209. */
  210. if (ftrace_global_list == &ftrace_list_end ||
  211. ftrace_global_list->next == &ftrace_list_end) {
  212. func = ftrace_global_list->func;
  213. /*
  214. * As we are calling the function directly.
  215. * If it does not have recursion protection,
  216. * the function_trace_op needs to be updated
  217. * accordingly.
  218. */
  219. if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
  220. global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
  221. else
  222. global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
  223. } else {
  224. func = ftrace_global_list_func;
  225. /* The list has its own recursion protection. */
  226. global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
  227. }
  228. /* If we filter on pids, update to use the pid function */
  229. if (!list_empty(&ftrace_pids)) {
  230. set_ftrace_pid_function(func);
  231. func = ftrace_pid_func;
  232. }
  233. global_ops.func = func;
  234. }
  235. static void update_ftrace_function(void)
  236. {
  237. ftrace_func_t func;
  238. update_global_ops();
  239. /*
  240. * If we are at the end of the list and this ops is
  241. * recursion safe and not dynamic and the arch supports passing ops,
  242. * then have the mcount trampoline call the function directly.
  243. */
  244. if (ftrace_ops_list == &ftrace_list_end ||
  245. (ftrace_ops_list->next == &ftrace_list_end &&
  246. !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
  247. (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
  248. !FTRACE_FORCE_LIST_FUNC)) {
  249. /* Set the ftrace_ops that the arch callback uses */
  250. if (ftrace_ops_list == &global_ops)
  251. function_trace_op = ftrace_global_list;
  252. else
  253. function_trace_op = ftrace_ops_list;
  254. func = ftrace_ops_list->func;
  255. } else {
  256. /* Just use the default ftrace_ops */
  257. function_trace_op = &ftrace_list_end;
  258. func = ftrace_ops_list_func;
  259. }
  260. ftrace_trace_function = func;
  261. }
  262. static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  263. {
  264. ops->next = *list;
  265. /*
  266. * We are entering ops into the list but another
  267. * CPU might be walking that list. We need to make sure
  268. * the ops->next pointer is valid before another CPU sees
  269. * the ops pointer included into the list.
  270. */
  271. rcu_assign_pointer(*list, ops);
  272. }
  273. static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
  274. {
  275. struct ftrace_ops **p;
  276. /*
  277. * If we are removing the last function, then simply point
  278. * to the ftrace_stub.
  279. */
  280. if (*list == ops && ops->next == &ftrace_list_end) {
  281. *list = &ftrace_list_end;
  282. return 0;
  283. }
  284. for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
  285. if (*p == ops)
  286. break;
  287. if (*p != ops)
  288. return -1;
  289. *p = (*p)->next;
  290. return 0;
  291. }
  292. static void add_ftrace_list_ops(struct ftrace_ops **list,
  293. struct ftrace_ops *main_ops,
  294. struct ftrace_ops *ops)
  295. {
  296. int first = *list == &ftrace_list_end;
  297. add_ftrace_ops(list, ops);
  298. if (first)
  299. add_ftrace_ops(&ftrace_ops_list, main_ops);
  300. }
  301. static int remove_ftrace_list_ops(struct ftrace_ops **list,
  302. struct ftrace_ops *main_ops,
  303. struct ftrace_ops *ops)
  304. {
  305. int ret = remove_ftrace_ops(list, ops);
  306. if (!ret && *list == &ftrace_list_end)
  307. ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
  308. return ret;
  309. }
  310. static int __register_ftrace_function(struct ftrace_ops *ops)
  311. {
  312. if (unlikely(ftrace_disabled))
  313. return -ENODEV;
  314. if (FTRACE_WARN_ON(ops == &global_ops))
  315. return -EINVAL;
  316. if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
  317. return -EBUSY;
  318. /* We don't support both control and global flags set. */
  319. if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
  320. return -EINVAL;
  321. #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  322. /*
  323. * If the ftrace_ops specifies SAVE_REGS, then it only can be used
  324. * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
  325. * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
  326. */
  327. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
  328. !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
  329. return -EINVAL;
  330. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
  331. ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
  332. #endif
  333. if (!core_kernel_data((unsigned long)ops))
  334. ops->flags |= FTRACE_OPS_FL_DYNAMIC;
  335. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  336. add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
  337. ops->flags |= FTRACE_OPS_FL_ENABLED;
  338. } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
  339. if (control_ops_alloc(ops))
  340. return -ENOMEM;
  341. add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
  342. } else
  343. add_ftrace_ops(&ftrace_ops_list, ops);
  344. if (ftrace_enabled)
  345. update_ftrace_function();
  346. return 0;
  347. }
  348. static void ftrace_sync(struct work_struct *work)
  349. {
  350. /*
  351. * This function is just a stub to implement a hard force
  352. * of synchronize_sched(). This requires synchronizing
  353. * tasks even in userspace and idle.
  354. *
  355. * Yes, function tracing is rude.
  356. */
  357. }
  358. static int __unregister_ftrace_function(struct ftrace_ops *ops)
  359. {
  360. int ret;
  361. if (ftrace_disabled)
  362. return -ENODEV;
  363. if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
  364. return -EBUSY;
  365. if (FTRACE_WARN_ON(ops == &global_ops))
  366. return -EINVAL;
  367. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  368. ret = remove_ftrace_list_ops(&ftrace_global_list,
  369. &global_ops, ops);
  370. if (!ret)
  371. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  372. } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
  373. ret = remove_ftrace_list_ops(&ftrace_control_list,
  374. &control_ops, ops);
  375. if (!ret) {
  376. /*
  377. * The ftrace_ops is now removed from the list,
  378. * so there'll be no new users. We must ensure
  379. * all current users are done before we free
  380. * the control data.
  381. * Note synchronize_sched() is not enough, as we
  382. * use preempt_disable() to do RCU, but the function
  383. * tracer can be called where RCU is not active
  384. * (before user_exit()).
  385. */
  386. schedule_on_each_cpu(ftrace_sync);
  387. control_ops_free(ops);
  388. }
  389. } else
  390. ret = remove_ftrace_ops(&ftrace_ops_list, ops);
  391. if (ret < 0)
  392. return ret;
  393. if (ftrace_enabled)
  394. update_ftrace_function();
  395. /*
  396. * Dynamic ops may be freed, we must make sure that all
  397. * callers are done before leaving this function.
  398. *
  399. * Again, normal synchronize_sched() is not good enough.
  400. * We need to do a hard force of sched synchronization.
  401. */
  402. if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
  403. schedule_on_each_cpu(ftrace_sync);
  404. return 0;
  405. }
  406. static void ftrace_update_pid_func(void)
  407. {
  408. /* Only do something if we are tracing something */
  409. if (ftrace_trace_function == ftrace_stub)
  410. return;
  411. update_ftrace_function();
  412. }
  413. #ifdef CONFIG_FUNCTION_PROFILER
  414. struct ftrace_profile {
  415. struct hlist_node node;
  416. unsigned long ip;
  417. unsigned long counter;
  418. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  419. unsigned long long time;
  420. unsigned long long time_squared;
  421. #endif
  422. };
  423. struct ftrace_profile_page {
  424. struct ftrace_profile_page *next;
  425. unsigned long index;
  426. struct ftrace_profile records[];
  427. };
  428. struct ftrace_profile_stat {
  429. atomic_t disabled;
  430. struct hlist_head *hash;
  431. struct ftrace_profile_page *pages;
  432. struct ftrace_profile_page *start;
  433. struct tracer_stat stat;
  434. };
  435. #define PROFILE_RECORDS_SIZE \
  436. (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
  437. #define PROFILES_PER_PAGE \
  438. (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
  439. static int ftrace_profile_enabled __read_mostly;
  440. /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
  441. static DEFINE_MUTEX(ftrace_profile_lock);
  442. static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
  443. #define FTRACE_PROFILE_HASH_BITS 10
  444. #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
  445. static void *
  446. function_stat_next(void *v, int idx)
  447. {
  448. struct ftrace_profile *rec = v;
  449. struct ftrace_profile_page *pg;
  450. pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
  451. again:
  452. if (idx != 0)
  453. rec++;
  454. if ((void *)rec >= (void *)&pg->records[pg->index]) {
  455. pg = pg->next;
  456. if (!pg)
  457. return NULL;
  458. rec = &pg->records[0];
  459. if (!rec->counter)
  460. goto again;
  461. }
  462. return rec;
  463. }
  464. static void *function_stat_start(struct tracer_stat *trace)
  465. {
  466. struct ftrace_profile_stat *stat =
  467. container_of(trace, struct ftrace_profile_stat, stat);
  468. if (!stat || !stat->start)
  469. return NULL;
  470. return function_stat_next(&stat->start->records[0], 0);
  471. }
  472. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  473. /* function graph compares on total time */
  474. static int function_stat_cmp(void *p1, void *p2)
  475. {
  476. struct ftrace_profile *a = p1;
  477. struct ftrace_profile *b = p2;
  478. if (a->time < b->time)
  479. return -1;
  480. if (a->time > b->time)
  481. return 1;
  482. else
  483. return 0;
  484. }
  485. #else
  486. /* not function graph compares against hits */
  487. static int function_stat_cmp(void *p1, void *p2)
  488. {
  489. struct ftrace_profile *a = p1;
  490. struct ftrace_profile *b = p2;
  491. if (a->counter < b->counter)
  492. return -1;
  493. if (a->counter > b->counter)
  494. return 1;
  495. else
  496. return 0;
  497. }
  498. #endif
  499. static int function_stat_headers(struct seq_file *m)
  500. {
  501. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  502. seq_printf(m, " Function "
  503. "Hit Time Avg s^2\n"
  504. " -------- "
  505. "--- ---- --- ---\n");
  506. #else
  507. seq_printf(m, " Function Hit\n"
  508. " -------- ---\n");
  509. #endif
  510. return 0;
  511. }
  512. static int function_stat_show(struct seq_file *m, void *v)
  513. {
  514. struct ftrace_profile *rec = v;
  515. char str[KSYM_SYMBOL_LEN];
  516. int ret = 0;
  517. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  518. static struct trace_seq s;
  519. unsigned long long avg;
  520. unsigned long long stddev;
  521. #endif
  522. mutex_lock(&ftrace_profile_lock);
  523. /* we raced with function_profile_reset() */
  524. if (unlikely(rec->counter == 0)) {
  525. ret = -EBUSY;
  526. goto out;
  527. }
  528. kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
  529. seq_printf(m, " %-30.30s %10lu", str, rec->counter);
  530. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  531. seq_printf(m, " ");
  532. avg = rec->time;
  533. do_div(avg, rec->counter);
  534. /* Sample standard deviation (s^2) */
  535. if (rec->counter <= 1)
  536. stddev = 0;
  537. else {
  538. /*
  539. * Apply Welford's method:
  540. * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
  541. */
  542. stddev = rec->counter * rec->time_squared -
  543. rec->time * rec->time;
  544. /*
  545. * Divide only 1000 for ns^2 -> us^2 conversion.
  546. * trace_print_graph_duration will divide 1000 again.
  547. */
  548. do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
  549. }
  550. trace_seq_init(&s);
  551. trace_print_graph_duration(rec->time, &s);
  552. trace_seq_puts(&s, " ");
  553. trace_print_graph_duration(avg, &s);
  554. trace_seq_puts(&s, " ");
  555. trace_print_graph_duration(stddev, &s);
  556. trace_print_seq(m, &s);
  557. #endif
  558. seq_putc(m, '\n');
  559. out:
  560. mutex_unlock(&ftrace_profile_lock);
  561. return ret;
  562. }
  563. static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
  564. {
  565. struct ftrace_profile_page *pg;
  566. pg = stat->pages = stat->start;
  567. while (pg) {
  568. memset(pg->records, 0, PROFILE_RECORDS_SIZE);
  569. pg->index = 0;
  570. pg = pg->next;
  571. }
  572. memset(stat->hash, 0,
  573. FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
  574. }
  575. int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
  576. {
  577. struct ftrace_profile_page *pg;
  578. int functions;
  579. int pages;
  580. int i;
  581. /* If we already allocated, do nothing */
  582. if (stat->pages)
  583. return 0;
  584. stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
  585. if (!stat->pages)
  586. return -ENOMEM;
  587. #ifdef CONFIG_DYNAMIC_FTRACE
  588. functions = ftrace_update_tot_cnt;
  589. #else
  590. /*
  591. * We do not know the number of functions that exist because
  592. * dynamic tracing is what counts them. With past experience
  593. * we have around 20K functions. That should be more than enough.
  594. * It is highly unlikely we will execute every function in
  595. * the kernel.
  596. */
  597. functions = 20000;
  598. #endif
  599. pg = stat->start = stat->pages;
  600. pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
  601. for (i = 1; i < pages; i++) {
  602. pg->next = (void *)get_zeroed_page(GFP_KERNEL);
  603. if (!pg->next)
  604. goto out_free;
  605. pg = pg->next;
  606. }
  607. return 0;
  608. out_free:
  609. pg = stat->start;
  610. while (pg) {
  611. unsigned long tmp = (unsigned long)pg;
  612. pg = pg->next;
  613. free_page(tmp);
  614. }
  615. stat->pages = NULL;
  616. stat->start = NULL;
  617. return -ENOMEM;
  618. }
  619. static int ftrace_profile_init_cpu(int cpu)
  620. {
  621. struct ftrace_profile_stat *stat;
  622. int size;
  623. stat = &per_cpu(ftrace_profile_stats, cpu);
  624. if (stat->hash) {
  625. /* If the profile is already created, simply reset it */
  626. ftrace_profile_reset(stat);
  627. return 0;
  628. }
  629. /*
  630. * We are profiling all functions, but usually only a few thousand
  631. * functions are hit. We'll make a hash of 1024 items.
  632. */
  633. size = FTRACE_PROFILE_HASH_SIZE;
  634. stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
  635. if (!stat->hash)
  636. return -ENOMEM;
  637. /* Preallocate the function profiling pages */
  638. if (ftrace_profile_pages_init(stat) < 0) {
  639. kfree(stat->hash);
  640. stat->hash = NULL;
  641. return -ENOMEM;
  642. }
  643. return 0;
  644. }
  645. static int ftrace_profile_init(void)
  646. {
  647. int cpu;
  648. int ret = 0;
  649. for_each_online_cpu(cpu) {
  650. ret = ftrace_profile_init_cpu(cpu);
  651. if (ret)
  652. break;
  653. }
  654. return ret;
  655. }
  656. /* interrupts must be disabled */
  657. static struct ftrace_profile *
  658. ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
  659. {
  660. struct ftrace_profile *rec;
  661. struct hlist_head *hhd;
  662. unsigned long key;
  663. key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
  664. hhd = &stat->hash[key];
  665. if (hlist_empty(hhd))
  666. return NULL;
  667. hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
  668. if (rec->ip == ip)
  669. return rec;
  670. }
  671. return NULL;
  672. }
  673. static void ftrace_add_profile(struct ftrace_profile_stat *stat,
  674. struct ftrace_profile *rec)
  675. {
  676. unsigned long key;
  677. key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
  678. hlist_add_head_rcu(&rec->node, &stat->hash[key]);
  679. }
  680. /*
  681. * The memory is already allocated, this simply finds a new record to use.
  682. */
  683. static struct ftrace_profile *
  684. ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
  685. {
  686. struct ftrace_profile *rec = NULL;
  687. /* prevent recursion (from NMIs) */
  688. if (atomic_inc_return(&stat->disabled) != 1)
  689. goto out;
  690. /*
  691. * Try to find the function again since an NMI
  692. * could have added it
  693. */
  694. rec = ftrace_find_profiled_func(stat, ip);
  695. if (rec)
  696. goto out;
  697. if (stat->pages->index == PROFILES_PER_PAGE) {
  698. if (!stat->pages->next)
  699. goto out;
  700. stat->pages = stat->pages->next;
  701. }
  702. rec = &stat->pages->records[stat->pages->index++];
  703. rec->ip = ip;
  704. ftrace_add_profile(stat, rec);
  705. out:
  706. atomic_dec(&stat->disabled);
  707. return rec;
  708. }
  709. static void
  710. function_profile_call(unsigned long ip, unsigned long parent_ip,
  711. struct ftrace_ops *ops, struct pt_regs *regs)
  712. {
  713. struct ftrace_profile_stat *stat;
  714. struct ftrace_profile *rec;
  715. unsigned long flags;
  716. if (!ftrace_profile_enabled)
  717. return;
  718. local_irq_save(flags);
  719. stat = &__get_cpu_var(ftrace_profile_stats);
  720. if (!stat->hash || !ftrace_profile_enabled)
  721. goto out;
  722. rec = ftrace_find_profiled_func(stat, ip);
  723. if (!rec) {
  724. rec = ftrace_profile_alloc(stat, ip);
  725. if (!rec)
  726. goto out;
  727. }
  728. rec->counter++;
  729. out:
  730. local_irq_restore(flags);
  731. }
  732. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  733. static int profile_graph_entry(struct ftrace_graph_ent *trace)
  734. {
  735. function_profile_call(trace->func, 0, NULL, NULL);
  736. return 1;
  737. }
  738. static void profile_graph_return(struct ftrace_graph_ret *trace)
  739. {
  740. struct ftrace_profile_stat *stat;
  741. unsigned long long calltime;
  742. struct ftrace_profile *rec;
  743. unsigned long flags;
  744. local_irq_save(flags);
  745. stat = &__get_cpu_var(ftrace_profile_stats);
  746. if (!stat->hash || !ftrace_profile_enabled)
  747. goto out;
  748. /* If the calltime was zero'd ignore it */
  749. if (!trace->calltime)
  750. goto out;
  751. calltime = trace->rettime - trace->calltime;
  752. if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
  753. int index;
  754. index = trace->depth;
  755. /* Append this call time to the parent time to subtract */
  756. if (index)
  757. current->ret_stack[index - 1].subtime += calltime;
  758. if (current->ret_stack[index].subtime < calltime)
  759. calltime -= current->ret_stack[index].subtime;
  760. else
  761. calltime = 0;
  762. }
  763. rec = ftrace_find_profiled_func(stat, trace->func);
  764. if (rec) {
  765. rec->time += calltime;
  766. rec->time_squared += calltime * calltime;
  767. }
  768. out:
  769. local_irq_restore(flags);
  770. }
  771. static int register_ftrace_profiler(void)
  772. {
  773. return register_ftrace_graph(&profile_graph_return,
  774. &profile_graph_entry);
  775. }
  776. static void unregister_ftrace_profiler(void)
  777. {
  778. unregister_ftrace_graph();
  779. }
  780. #else
  781. static struct ftrace_ops ftrace_profile_ops __read_mostly = {
  782. .func = function_profile_call,
  783. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
  784. INIT_REGEX_LOCK(ftrace_profile_ops)
  785. };
  786. static int register_ftrace_profiler(void)
  787. {
  788. return register_ftrace_function(&ftrace_profile_ops);
  789. }
  790. static void unregister_ftrace_profiler(void)
  791. {
  792. unregister_ftrace_function(&ftrace_profile_ops);
  793. }
  794. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  795. static ssize_t
  796. ftrace_profile_write(struct file *filp, const char __user *ubuf,
  797. size_t cnt, loff_t *ppos)
  798. {
  799. unsigned long val;
  800. int ret;
  801. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  802. if (ret)
  803. return ret;
  804. val = !!val;
  805. mutex_lock(&ftrace_profile_lock);
  806. if (ftrace_profile_enabled ^ val) {
  807. if (val) {
  808. ret = ftrace_profile_init();
  809. if (ret < 0) {
  810. cnt = ret;
  811. goto out;
  812. }
  813. ret = register_ftrace_profiler();
  814. if (ret < 0) {
  815. cnt = ret;
  816. goto out;
  817. }
  818. ftrace_profile_enabled = 1;
  819. } else {
  820. ftrace_profile_enabled = 0;
  821. /*
  822. * unregister_ftrace_profiler calls stop_machine
  823. * so this acts like an synchronize_sched.
  824. */
  825. unregister_ftrace_profiler();
  826. }
  827. }
  828. out:
  829. mutex_unlock(&ftrace_profile_lock);
  830. *ppos += cnt;
  831. return cnt;
  832. }
  833. static ssize_t
  834. ftrace_profile_read(struct file *filp, char __user *ubuf,
  835. size_t cnt, loff_t *ppos)
  836. {
  837. char buf[64]; /* big enough to hold a number */
  838. int r;
  839. r = sprintf(buf, "%u\n", ftrace_profile_enabled);
  840. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  841. }
  842. static const struct file_operations ftrace_profile_fops = {
  843. .open = tracing_open_generic,
  844. .read = ftrace_profile_read,
  845. .write = ftrace_profile_write,
  846. .llseek = default_llseek,
  847. };
  848. /* used to initialize the real stat files */
  849. static struct tracer_stat function_stats __initdata = {
  850. .name = "functions",
  851. .stat_start = function_stat_start,
  852. .stat_next = function_stat_next,
  853. .stat_cmp = function_stat_cmp,
  854. .stat_headers = function_stat_headers,
  855. .stat_show = function_stat_show
  856. };
  857. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  858. {
  859. struct ftrace_profile_stat *stat;
  860. struct dentry *entry;
  861. char *name;
  862. int ret;
  863. int cpu;
  864. for_each_possible_cpu(cpu) {
  865. stat = &per_cpu(ftrace_profile_stats, cpu);
  866. /* allocate enough for function name + cpu number */
  867. name = kmalloc(32, GFP_KERNEL);
  868. if (!name) {
  869. /*
  870. * The files created are permanent, if something happens
  871. * we still do not free memory.
  872. */
  873. WARN(1,
  874. "Could not allocate stat file for cpu %d\n",
  875. cpu);
  876. return;
  877. }
  878. stat->stat = function_stats;
  879. snprintf(name, 32, "function%d", cpu);
  880. stat->stat.name = name;
  881. ret = register_stat_tracer(&stat->stat);
  882. if (ret) {
  883. WARN(1,
  884. "Could not register function stat for cpu %d\n",
  885. cpu);
  886. kfree(name);
  887. return;
  888. }
  889. }
  890. entry = debugfs_create_file("function_profile_enabled", 0644,
  891. d_tracer, NULL, &ftrace_profile_fops);
  892. if (!entry)
  893. pr_warning("Could not create debugfs "
  894. "'function_profile_enabled' entry\n");
  895. }
  896. #else /* CONFIG_FUNCTION_PROFILER */
  897. static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
  898. {
  899. }
  900. #endif /* CONFIG_FUNCTION_PROFILER */
  901. static struct pid * const ftrace_swapper_pid = &init_struct_pid;
  902. loff_t
  903. ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
  904. {
  905. loff_t ret;
  906. if (file->f_mode & FMODE_READ)
  907. ret = seq_lseek(file, offset, whence);
  908. else
  909. file->f_pos = ret = 1;
  910. return ret;
  911. }
  912. #ifdef CONFIG_DYNAMIC_FTRACE
  913. #ifndef CONFIG_FTRACE_MCOUNT_RECORD
  914. # error Dynamic ftrace depends on MCOUNT_RECORD
  915. #endif
  916. static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
  917. struct ftrace_func_probe {
  918. struct hlist_node node;
  919. struct ftrace_probe_ops *ops;
  920. unsigned long flags;
  921. unsigned long ip;
  922. void *data;
  923. struct list_head free_list;
  924. };
  925. struct ftrace_func_entry {
  926. struct hlist_node hlist;
  927. unsigned long ip;
  928. };
  929. struct ftrace_hash {
  930. unsigned long size_bits;
  931. struct hlist_head *buckets;
  932. unsigned long count;
  933. struct rcu_head rcu;
  934. };
  935. /*
  936. * We make these constant because no one should touch them,
  937. * but they are used as the default "empty hash", to avoid allocating
  938. * it all the time. These are in a read only section such that if
  939. * anyone does try to modify it, it will cause an exception.
  940. */
  941. static const struct hlist_head empty_buckets[1];
  942. static const struct ftrace_hash empty_hash = {
  943. .buckets = (struct hlist_head *)empty_buckets,
  944. };
  945. #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
  946. static struct ftrace_ops global_ops = {
  947. .func = ftrace_stub,
  948. .notrace_hash = EMPTY_HASH,
  949. .filter_hash = EMPTY_HASH,
  950. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
  951. INIT_REGEX_LOCK(global_ops)
  952. };
  953. struct ftrace_page {
  954. struct ftrace_page *next;
  955. struct dyn_ftrace *records;
  956. int index;
  957. int size;
  958. };
  959. static struct ftrace_page *ftrace_new_pgs;
  960. #define ENTRY_SIZE sizeof(struct dyn_ftrace)
  961. #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
  962. /* estimate from running different kernels */
  963. #define NR_TO_INIT 10000
  964. static struct ftrace_page *ftrace_pages_start;
  965. static struct ftrace_page *ftrace_pages;
  966. static bool ftrace_hash_empty(struct ftrace_hash *hash)
  967. {
  968. return !hash || !hash->count;
  969. }
  970. static struct ftrace_func_entry *
  971. ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
  972. {
  973. unsigned long key;
  974. struct ftrace_func_entry *entry;
  975. struct hlist_head *hhd;
  976. if (ftrace_hash_empty(hash))
  977. return NULL;
  978. if (hash->size_bits > 0)
  979. key = hash_long(ip, hash->size_bits);
  980. else
  981. key = 0;
  982. hhd = &hash->buckets[key];
  983. hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
  984. if (entry->ip == ip)
  985. return entry;
  986. }
  987. return NULL;
  988. }
  989. static void __add_hash_entry(struct ftrace_hash *hash,
  990. struct ftrace_func_entry *entry)
  991. {
  992. struct hlist_head *hhd;
  993. unsigned long key;
  994. if (hash->size_bits)
  995. key = hash_long(entry->ip, hash->size_bits);
  996. else
  997. key = 0;
  998. hhd = &hash->buckets[key];
  999. hlist_add_head(&entry->hlist, hhd);
  1000. hash->count++;
  1001. }
  1002. static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
  1003. {
  1004. struct ftrace_func_entry *entry;
  1005. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1006. if (!entry)
  1007. return -ENOMEM;
  1008. entry->ip = ip;
  1009. __add_hash_entry(hash, entry);
  1010. return 0;
  1011. }
  1012. static void
  1013. free_hash_entry(struct ftrace_hash *hash,
  1014. struct ftrace_func_entry *entry)
  1015. {
  1016. hlist_del(&entry->hlist);
  1017. kfree(entry);
  1018. hash->count--;
  1019. }
  1020. static void
  1021. remove_hash_entry(struct ftrace_hash *hash,
  1022. struct ftrace_func_entry *entry)
  1023. {
  1024. hlist_del(&entry->hlist);
  1025. hash->count--;
  1026. }
  1027. static void ftrace_hash_clear(struct ftrace_hash *hash)
  1028. {
  1029. struct hlist_head *hhd;
  1030. struct hlist_node *tn;
  1031. struct ftrace_func_entry *entry;
  1032. int size = 1 << hash->size_bits;
  1033. int i;
  1034. if (!hash->count)
  1035. return;
  1036. for (i = 0; i < size; i++) {
  1037. hhd = &hash->buckets[i];
  1038. hlist_for_each_entry_safe(entry, tn, hhd, hlist)
  1039. free_hash_entry(hash, entry);
  1040. }
  1041. FTRACE_WARN_ON(hash->count);
  1042. }
  1043. static void free_ftrace_hash(struct ftrace_hash *hash)
  1044. {
  1045. if (!hash || hash == EMPTY_HASH)
  1046. return;
  1047. ftrace_hash_clear(hash);
  1048. kfree(hash->buckets);
  1049. kfree(hash);
  1050. }
  1051. static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
  1052. {
  1053. struct ftrace_hash *hash;
  1054. hash = container_of(rcu, struct ftrace_hash, rcu);
  1055. free_ftrace_hash(hash);
  1056. }
  1057. static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
  1058. {
  1059. if (!hash || hash == EMPTY_HASH)
  1060. return;
  1061. call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
  1062. }
  1063. void ftrace_free_filter(struct ftrace_ops *ops)
  1064. {
  1065. ftrace_ops_init(ops);
  1066. free_ftrace_hash(ops->filter_hash);
  1067. free_ftrace_hash(ops->notrace_hash);
  1068. }
  1069. static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
  1070. {
  1071. struct ftrace_hash *hash;
  1072. int size;
  1073. hash = kzalloc(sizeof(*hash), GFP_KERNEL);
  1074. if (!hash)
  1075. return NULL;
  1076. size = 1 << size_bits;
  1077. hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
  1078. if (!hash->buckets) {
  1079. kfree(hash);
  1080. return NULL;
  1081. }
  1082. hash->size_bits = size_bits;
  1083. return hash;
  1084. }
  1085. static struct ftrace_hash *
  1086. alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
  1087. {
  1088. struct ftrace_func_entry *entry;
  1089. struct ftrace_hash *new_hash;
  1090. int size;
  1091. int ret;
  1092. int i;
  1093. new_hash = alloc_ftrace_hash(size_bits);
  1094. if (!new_hash)
  1095. return NULL;
  1096. /* Empty hash? */
  1097. if (ftrace_hash_empty(hash))
  1098. return new_hash;
  1099. size = 1 << hash->size_bits;
  1100. for (i = 0; i < size; i++) {
  1101. hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
  1102. ret = add_hash_entry(new_hash, entry->ip);
  1103. if (ret < 0)
  1104. goto free_hash;
  1105. }
  1106. }
  1107. FTRACE_WARN_ON(new_hash->count != hash->count);
  1108. return new_hash;
  1109. free_hash:
  1110. free_ftrace_hash(new_hash);
  1111. return NULL;
  1112. }
  1113. static void
  1114. ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
  1115. static void
  1116. ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
  1117. static int
  1118. ftrace_hash_move(struct ftrace_ops *ops, int enable,
  1119. struct ftrace_hash **dst, struct ftrace_hash *src)
  1120. {
  1121. struct ftrace_func_entry *entry;
  1122. struct hlist_node *tn;
  1123. struct hlist_head *hhd;
  1124. struct ftrace_hash *old_hash;
  1125. struct ftrace_hash *new_hash;
  1126. int size = src->count;
  1127. int bits = 0;
  1128. int ret;
  1129. int i;
  1130. /*
  1131. * Remove the current set, update the hash and add
  1132. * them back.
  1133. */
  1134. ftrace_hash_rec_disable(ops, enable);
  1135. /*
  1136. * If the new source is empty, just free dst and assign it
  1137. * the empty_hash.
  1138. */
  1139. if (!src->count) {
  1140. free_ftrace_hash_rcu(*dst);
  1141. rcu_assign_pointer(*dst, EMPTY_HASH);
  1142. /* still need to update the function records */
  1143. ret = 0;
  1144. goto out;
  1145. }
  1146. /*
  1147. * Make the hash size about 1/2 the # found
  1148. */
  1149. for (size /= 2; size; size >>= 1)
  1150. bits++;
  1151. /* Don't allocate too much */
  1152. if (bits > FTRACE_HASH_MAX_BITS)
  1153. bits = FTRACE_HASH_MAX_BITS;
  1154. ret = -ENOMEM;
  1155. new_hash = alloc_ftrace_hash(bits);
  1156. if (!new_hash)
  1157. goto out;
  1158. size = 1 << src->size_bits;
  1159. for (i = 0; i < size; i++) {
  1160. hhd = &src->buckets[i];
  1161. hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
  1162. remove_hash_entry(src, entry);
  1163. __add_hash_entry(new_hash, entry);
  1164. }
  1165. }
  1166. old_hash = *dst;
  1167. rcu_assign_pointer(*dst, new_hash);
  1168. free_ftrace_hash_rcu(old_hash);
  1169. ret = 0;
  1170. out:
  1171. /*
  1172. * Enable regardless of ret:
  1173. * On success, we enable the new hash.
  1174. * On failure, we re-enable the original hash.
  1175. */
  1176. ftrace_hash_rec_enable(ops, enable);
  1177. return ret;
  1178. }
  1179. /*
  1180. * Test the hashes for this ops to see if we want to call
  1181. * the ops->func or not.
  1182. *
  1183. * It's a match if the ip is in the ops->filter_hash or
  1184. * the filter_hash does not exist or is empty,
  1185. * AND
  1186. * the ip is not in the ops->notrace_hash.
  1187. *
  1188. * This needs to be called with preemption disabled as
  1189. * the hashes are freed with call_rcu_sched().
  1190. */
  1191. static int
  1192. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
  1193. {
  1194. struct ftrace_hash *filter_hash;
  1195. struct ftrace_hash *notrace_hash;
  1196. int ret;
  1197. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  1198. /*
  1199. * There's a small race when adding ops that the ftrace handler
  1200. * that wants regs, may be called without them. We can not
  1201. * allow that handler to be called if regs is NULL.
  1202. */
  1203. if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
  1204. return 0;
  1205. #endif
  1206. filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
  1207. notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
  1208. if ((ftrace_hash_empty(filter_hash) ||
  1209. ftrace_lookup_ip(filter_hash, ip)) &&
  1210. (ftrace_hash_empty(notrace_hash) ||
  1211. !ftrace_lookup_ip(notrace_hash, ip)))
  1212. ret = 1;
  1213. else
  1214. ret = 0;
  1215. return ret;
  1216. }
  1217. /*
  1218. * This is a double for. Do not use 'break' to break out of the loop,
  1219. * you must use a goto.
  1220. */
  1221. #define do_for_each_ftrace_rec(pg, rec) \
  1222. for (pg = ftrace_pages_start; pg; pg = pg->next) { \
  1223. int _____i; \
  1224. for (_____i = 0; _____i < pg->index; _____i++) { \
  1225. rec = &pg->records[_____i];
  1226. #define while_for_each_ftrace_rec() \
  1227. } \
  1228. }
  1229. static int ftrace_cmp_recs(const void *a, const void *b)
  1230. {
  1231. const struct dyn_ftrace *key = a;
  1232. const struct dyn_ftrace *rec = b;
  1233. if (key->flags < rec->ip)
  1234. return -1;
  1235. if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
  1236. return 1;
  1237. return 0;
  1238. }
  1239. static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
  1240. {
  1241. struct ftrace_page *pg;
  1242. struct dyn_ftrace *rec;
  1243. struct dyn_ftrace key;
  1244. key.ip = start;
  1245. key.flags = end; /* overload flags, as it is unsigned long */
  1246. for (pg = ftrace_pages_start; pg; pg = pg->next) {
  1247. if (end < pg->records[0].ip ||
  1248. start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
  1249. continue;
  1250. rec = bsearch(&key, pg->records, pg->index,
  1251. sizeof(struct dyn_ftrace),
  1252. ftrace_cmp_recs);
  1253. if (rec)
  1254. return rec->ip;
  1255. }
  1256. return 0;
  1257. }
  1258. /**
  1259. * ftrace_location - return true if the ip giving is a traced location
  1260. * @ip: the instruction pointer to check
  1261. *
  1262. * Returns rec->ip if @ip given is a pointer to a ftrace location.
  1263. * That is, the instruction that is either a NOP or call to
  1264. * the function tracer. It checks the ftrace internal tables to
  1265. * determine if the address belongs or not.
  1266. */
  1267. unsigned long ftrace_location(unsigned long ip)
  1268. {
  1269. return ftrace_location_range(ip, ip);
  1270. }
  1271. /**
  1272. * ftrace_text_reserved - return true if range contains an ftrace location
  1273. * @start: start of range to search
  1274. * @end: end of range to search (inclusive). @end points to the last byte to check.
  1275. *
  1276. * Returns 1 if @start and @end contains a ftrace location.
  1277. * That is, the instruction that is either a NOP or call to
  1278. * the function tracer. It checks the ftrace internal tables to
  1279. * determine if the address belongs or not.
  1280. */
  1281. int ftrace_text_reserved(void *start, void *end)
  1282. {
  1283. unsigned long ret;
  1284. ret = ftrace_location_range((unsigned long)start,
  1285. (unsigned long)end);
  1286. return (int)!!ret;
  1287. }
  1288. static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
  1289. int filter_hash,
  1290. bool inc)
  1291. {
  1292. struct ftrace_hash *hash;
  1293. struct ftrace_hash *other_hash;
  1294. struct ftrace_page *pg;
  1295. struct dyn_ftrace *rec;
  1296. int count = 0;
  1297. int all = 0;
  1298. /* Only update if the ops has been registered */
  1299. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1300. return;
  1301. /*
  1302. * In the filter_hash case:
  1303. * If the count is zero, we update all records.
  1304. * Otherwise we just update the items in the hash.
  1305. *
  1306. * In the notrace_hash case:
  1307. * We enable the update in the hash.
  1308. * As disabling notrace means enabling the tracing,
  1309. * and enabling notrace means disabling, the inc variable
  1310. * gets inversed.
  1311. */
  1312. if (filter_hash) {
  1313. hash = ops->filter_hash;
  1314. other_hash = ops->notrace_hash;
  1315. if (ftrace_hash_empty(hash))
  1316. all = 1;
  1317. } else {
  1318. inc = !inc;
  1319. hash = ops->notrace_hash;
  1320. other_hash = ops->filter_hash;
  1321. /*
  1322. * If the notrace hash has no items,
  1323. * then there's nothing to do.
  1324. */
  1325. if (ftrace_hash_empty(hash))
  1326. return;
  1327. }
  1328. do_for_each_ftrace_rec(pg, rec) {
  1329. int in_other_hash = 0;
  1330. int in_hash = 0;
  1331. int match = 0;
  1332. if (all) {
  1333. /*
  1334. * Only the filter_hash affects all records.
  1335. * Update if the record is not in the notrace hash.
  1336. */
  1337. if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
  1338. match = 1;
  1339. } else {
  1340. in_hash = !!ftrace_lookup_ip(hash, rec->ip);
  1341. in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
  1342. /*
  1343. *
  1344. */
  1345. if (filter_hash && in_hash && !in_other_hash)
  1346. match = 1;
  1347. else if (!filter_hash && in_hash &&
  1348. (in_other_hash || ftrace_hash_empty(other_hash)))
  1349. match = 1;
  1350. }
  1351. if (!match)
  1352. continue;
  1353. if (inc) {
  1354. rec->flags++;
  1355. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
  1356. return;
  1357. /*
  1358. * If any ops wants regs saved for this function
  1359. * then all ops will get saved regs.
  1360. */
  1361. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
  1362. rec->flags |= FTRACE_FL_REGS;
  1363. } else {
  1364. if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
  1365. return;
  1366. rec->flags--;
  1367. }
  1368. count++;
  1369. /* Shortcut, if we handled all records, we are done. */
  1370. if (!all && count == hash->count)
  1371. return;
  1372. } while_for_each_ftrace_rec();
  1373. }
  1374. static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
  1375. int filter_hash)
  1376. {
  1377. __ftrace_hash_rec_update(ops, filter_hash, 0);
  1378. }
  1379. static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
  1380. int filter_hash)
  1381. {
  1382. __ftrace_hash_rec_update(ops, filter_hash, 1);
  1383. }
  1384. static void print_ip_ins(const char *fmt, unsigned char *p)
  1385. {
  1386. int i;
  1387. printk(KERN_CONT "%s", fmt);
  1388. for (i = 0; i < MCOUNT_INSN_SIZE; i++)
  1389. printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
  1390. }
  1391. /**
  1392. * ftrace_bug - report and shutdown function tracer
  1393. * @failed: The failed type (EFAULT, EINVAL, EPERM)
  1394. * @ip: The address that failed
  1395. *
  1396. * The arch code that enables or disables the function tracing
  1397. * can call ftrace_bug() when it has detected a problem in
  1398. * modifying the code. @failed should be one of either:
  1399. * EFAULT - if the problem happens on reading the @ip address
  1400. * EINVAL - if what is read at @ip is not what was expected
  1401. * EPERM - if the problem happens on writting to the @ip address
  1402. */
  1403. void ftrace_bug(int failed, unsigned long ip)
  1404. {
  1405. switch (failed) {
  1406. case -EFAULT:
  1407. FTRACE_WARN_ON_ONCE(1);
  1408. pr_info("ftrace faulted on modifying ");
  1409. print_ip_sym(ip);
  1410. break;
  1411. case -EINVAL:
  1412. FTRACE_WARN_ON_ONCE(1);
  1413. pr_info("ftrace failed to modify ");
  1414. print_ip_sym(ip);
  1415. print_ip_ins(" actual: ", (unsigned char *)ip);
  1416. printk(KERN_CONT "\n");
  1417. break;
  1418. case -EPERM:
  1419. FTRACE_WARN_ON_ONCE(1);
  1420. pr_info("ftrace faulted on writing ");
  1421. print_ip_sym(ip);
  1422. break;
  1423. default:
  1424. FTRACE_WARN_ON_ONCE(1);
  1425. pr_info("ftrace faulted on unknown error ");
  1426. print_ip_sym(ip);
  1427. }
  1428. }
  1429. static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
  1430. {
  1431. unsigned long flag = 0UL;
  1432. /*
  1433. * If we are updating calls:
  1434. *
  1435. * If the record has a ref count, then we need to enable it
  1436. * because someone is using it.
  1437. *
  1438. * Otherwise we make sure its disabled.
  1439. *
  1440. * If we are disabling calls, then disable all records that
  1441. * are enabled.
  1442. */
  1443. if (enable && (rec->flags & ~FTRACE_FL_MASK))
  1444. flag = FTRACE_FL_ENABLED;
  1445. /*
  1446. * If enabling and the REGS flag does not match the REGS_EN, then
  1447. * do not ignore this record. Set flags to fail the compare against
  1448. * ENABLED.
  1449. */
  1450. if (flag &&
  1451. (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
  1452. flag |= FTRACE_FL_REGS;
  1453. /* If the state of this record hasn't changed, then do nothing */
  1454. if ((rec->flags & FTRACE_FL_ENABLED) == flag)
  1455. return FTRACE_UPDATE_IGNORE;
  1456. if (flag) {
  1457. /* Save off if rec is being enabled (for return value) */
  1458. flag ^= rec->flags & FTRACE_FL_ENABLED;
  1459. if (update) {
  1460. rec->flags |= FTRACE_FL_ENABLED;
  1461. if (flag & FTRACE_FL_REGS) {
  1462. if (rec->flags & FTRACE_FL_REGS)
  1463. rec->flags |= FTRACE_FL_REGS_EN;
  1464. else
  1465. rec->flags &= ~FTRACE_FL_REGS_EN;
  1466. }
  1467. }
  1468. /*
  1469. * If this record is being updated from a nop, then
  1470. * return UPDATE_MAKE_CALL.
  1471. * Otherwise, if the EN flag is set, then return
  1472. * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
  1473. * from the non-save regs, to a save regs function.
  1474. * Otherwise,
  1475. * return UPDATE_MODIFY_CALL to tell the caller to convert
  1476. * from the save regs, to a non-save regs function.
  1477. */
  1478. if (flag & FTRACE_FL_ENABLED)
  1479. return FTRACE_UPDATE_MAKE_CALL;
  1480. else if (rec->flags & FTRACE_FL_REGS_EN)
  1481. return FTRACE_UPDATE_MODIFY_CALL_REGS;
  1482. else
  1483. return FTRACE_UPDATE_MODIFY_CALL;
  1484. }
  1485. if (update) {
  1486. /* If there's no more users, clear all flags */
  1487. if (!(rec->flags & ~FTRACE_FL_MASK))
  1488. rec->flags = 0;
  1489. else
  1490. /* Just disable the record (keep REGS state) */
  1491. rec->flags &= ~FTRACE_FL_ENABLED;
  1492. }
  1493. return FTRACE_UPDATE_MAKE_NOP;
  1494. }
  1495. /**
  1496. * ftrace_update_record, set a record that now is tracing or not
  1497. * @rec: the record to update
  1498. * @enable: set to 1 if the record is tracing, zero to force disable
  1499. *
  1500. * The records that represent all functions that can be traced need
  1501. * to be updated when tracing has been enabled.
  1502. */
  1503. int ftrace_update_record(struct dyn_ftrace *rec, int enable)
  1504. {
  1505. return ftrace_check_record(rec, enable, 1);
  1506. }
  1507. /**
  1508. * ftrace_test_record, check if the record has been enabled or not
  1509. * @rec: the record to test
  1510. * @enable: set to 1 to check if enabled, 0 if it is disabled
  1511. *
  1512. * The arch code may need to test if a record is already set to
  1513. * tracing to determine how to modify the function code that it
  1514. * represents.
  1515. */
  1516. int ftrace_test_record(struct dyn_ftrace *rec, int enable)
  1517. {
  1518. return ftrace_check_record(rec, enable, 0);
  1519. }
  1520. static int
  1521. __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
  1522. {
  1523. unsigned long ftrace_old_addr;
  1524. unsigned long ftrace_addr;
  1525. int ret;
  1526. ret = ftrace_update_record(rec, enable);
  1527. if (rec->flags & FTRACE_FL_REGS)
  1528. ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
  1529. else
  1530. ftrace_addr = (unsigned long)FTRACE_ADDR;
  1531. switch (ret) {
  1532. case FTRACE_UPDATE_IGNORE:
  1533. return 0;
  1534. case FTRACE_UPDATE_MAKE_CALL:
  1535. return ftrace_make_call(rec, ftrace_addr);
  1536. case FTRACE_UPDATE_MAKE_NOP:
  1537. return ftrace_make_nop(NULL, rec, ftrace_addr);
  1538. case FTRACE_UPDATE_MODIFY_CALL_REGS:
  1539. case FTRACE_UPDATE_MODIFY_CALL:
  1540. if (rec->flags & FTRACE_FL_REGS)
  1541. ftrace_old_addr = (unsigned long)FTRACE_ADDR;
  1542. else
  1543. ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
  1544. return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
  1545. }
  1546. return -1; /* unknow ftrace bug */
  1547. }
  1548. void __weak ftrace_replace_code(int enable)
  1549. {
  1550. struct dyn_ftrace *rec;
  1551. struct ftrace_page *pg;
  1552. int failed;
  1553. if (unlikely(ftrace_disabled))
  1554. return;
  1555. do_for_each_ftrace_rec(pg, rec) {
  1556. failed = __ftrace_replace_code(rec, enable);
  1557. if (failed) {
  1558. ftrace_bug(failed, rec->ip);
  1559. /* Stop processing */
  1560. return;
  1561. }
  1562. } while_for_each_ftrace_rec();
  1563. }
  1564. struct ftrace_rec_iter {
  1565. struct ftrace_page *pg;
  1566. int index;
  1567. };
  1568. /**
  1569. * ftrace_rec_iter_start, start up iterating over traced functions
  1570. *
  1571. * Returns an iterator handle that is used to iterate over all
  1572. * the records that represent address locations where functions
  1573. * are traced.
  1574. *
  1575. * May return NULL if no records are available.
  1576. */
  1577. struct ftrace_rec_iter *ftrace_rec_iter_start(void)
  1578. {
  1579. /*
  1580. * We only use a single iterator.
  1581. * Protected by the ftrace_lock mutex.
  1582. */
  1583. static struct ftrace_rec_iter ftrace_rec_iter;
  1584. struct ftrace_rec_iter *iter = &ftrace_rec_iter;
  1585. iter->pg = ftrace_pages_start;
  1586. iter->index = 0;
  1587. /* Could have empty pages */
  1588. while (iter->pg && !iter->pg->index)
  1589. iter->pg = iter->pg->next;
  1590. if (!iter->pg)
  1591. return NULL;
  1592. return iter;
  1593. }
  1594. /**
  1595. * ftrace_rec_iter_next, get the next record to process.
  1596. * @iter: The handle to the iterator.
  1597. *
  1598. * Returns the next iterator after the given iterator @iter.
  1599. */
  1600. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
  1601. {
  1602. iter->index++;
  1603. if (iter->index >= iter->pg->index) {
  1604. iter->pg = iter->pg->next;
  1605. iter->index = 0;
  1606. /* Could have empty pages */
  1607. while (iter->pg && !iter->pg->index)
  1608. iter->pg = iter->pg->next;
  1609. }
  1610. if (!iter->pg)
  1611. return NULL;
  1612. return iter;
  1613. }
  1614. /**
  1615. * ftrace_rec_iter_record, get the record at the iterator location
  1616. * @iter: The current iterator location
  1617. *
  1618. * Returns the record that the current @iter is at.
  1619. */
  1620. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
  1621. {
  1622. return &iter->pg->records[iter->index];
  1623. }
  1624. static int
  1625. ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
  1626. {
  1627. unsigned long ip;
  1628. int ret;
  1629. ip = rec->ip;
  1630. if (unlikely(ftrace_disabled))
  1631. return 0;
  1632. ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
  1633. if (ret) {
  1634. ftrace_bug(ret, ip);
  1635. return 0;
  1636. }
  1637. return 1;
  1638. }
  1639. /*
  1640. * archs can override this function if they must do something
  1641. * before the modifying code is performed.
  1642. */
  1643. int __weak ftrace_arch_code_modify_prepare(void)
  1644. {
  1645. return 0;
  1646. }
  1647. /*
  1648. * archs can override this function if they must do something
  1649. * after the modifying code is performed.
  1650. */
  1651. int __weak ftrace_arch_code_modify_post_process(void)
  1652. {
  1653. return 0;
  1654. }
  1655. void ftrace_modify_all_code(int command)
  1656. {
  1657. int update = command & FTRACE_UPDATE_TRACE_FUNC;
  1658. /*
  1659. * If the ftrace_caller calls a ftrace_ops func directly,
  1660. * we need to make sure that it only traces functions it
  1661. * expects to trace. When doing the switch of functions,
  1662. * we need to update to the ftrace_ops_list_func first
  1663. * before the transition between old and new calls are set,
  1664. * as the ftrace_ops_list_func will check the ops hashes
  1665. * to make sure the ops are having the right functions
  1666. * traced.
  1667. */
  1668. if (update)
  1669. ftrace_update_ftrace_func(ftrace_ops_list_func);
  1670. if (command & FTRACE_UPDATE_CALLS)
  1671. ftrace_replace_code(1);
  1672. else if (command & FTRACE_DISABLE_CALLS)
  1673. ftrace_replace_code(0);
  1674. if (update && ftrace_trace_function != ftrace_ops_list_func)
  1675. ftrace_update_ftrace_func(ftrace_trace_function);
  1676. if (command & FTRACE_START_FUNC_RET)
  1677. ftrace_enable_ftrace_graph_caller();
  1678. else if (command & FTRACE_STOP_FUNC_RET)
  1679. ftrace_disable_ftrace_graph_caller();
  1680. }
  1681. static int __ftrace_modify_code(void *data)
  1682. {
  1683. int *command = data;
  1684. ftrace_modify_all_code(*command);
  1685. return 0;
  1686. }
  1687. /**
  1688. * ftrace_run_stop_machine, go back to the stop machine method
  1689. * @command: The command to tell ftrace what to do
  1690. *
  1691. * If an arch needs to fall back to the stop machine method, the
  1692. * it can call this function.
  1693. */
  1694. void ftrace_run_stop_machine(int command)
  1695. {
  1696. stop_machine(__ftrace_modify_code, &command, NULL);
  1697. }
  1698. /**
  1699. * arch_ftrace_update_code, modify the code to trace or not trace
  1700. * @command: The command that needs to be done
  1701. *
  1702. * Archs can override this function if it does not need to
  1703. * run stop_machine() to modify code.
  1704. */
  1705. void __weak arch_ftrace_update_code(int command)
  1706. {
  1707. ftrace_run_stop_machine(command);
  1708. }
  1709. static void ftrace_run_update_code(int command)
  1710. {
  1711. int ret;
  1712. ret = ftrace_arch_code_modify_prepare();
  1713. FTRACE_WARN_ON(ret);
  1714. if (ret)
  1715. return;
  1716. /*
  1717. * Do not call function tracer while we update the code.
  1718. * We are in stop machine.
  1719. */
  1720. function_trace_stop++;
  1721. /*
  1722. * By default we use stop_machine() to modify the code.
  1723. * But archs can do what ever they want as long as it
  1724. * is safe. The stop_machine() is the safest, but also
  1725. * produces the most overhead.
  1726. */
  1727. arch_ftrace_update_code(command);
  1728. function_trace_stop--;
  1729. ret = ftrace_arch_code_modify_post_process();
  1730. FTRACE_WARN_ON(ret);
  1731. }
  1732. static ftrace_func_t saved_ftrace_func;
  1733. static int ftrace_start_up;
  1734. static int global_start_up;
  1735. static void ftrace_startup_enable(int command)
  1736. {
  1737. if (saved_ftrace_func != ftrace_trace_function) {
  1738. saved_ftrace_func = ftrace_trace_function;
  1739. command |= FTRACE_UPDATE_TRACE_FUNC;
  1740. }
  1741. if (!command || !ftrace_enabled)
  1742. return;
  1743. ftrace_run_update_code(command);
  1744. }
  1745. static int ftrace_startup(struct ftrace_ops *ops, int command)
  1746. {
  1747. bool hash_enable = true;
  1748. if (unlikely(ftrace_disabled))
  1749. return -ENODEV;
  1750. ftrace_start_up++;
  1751. command |= FTRACE_UPDATE_CALLS;
  1752. /* ops marked global share the filter hashes */
  1753. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1754. ops = &global_ops;
  1755. /* Don't update hash if global is already set */
  1756. if (global_start_up)
  1757. hash_enable = false;
  1758. global_start_up++;
  1759. }
  1760. ops->flags |= FTRACE_OPS_FL_ENABLED;
  1761. if (hash_enable)
  1762. ftrace_hash_rec_enable(ops, 1);
  1763. ftrace_startup_enable(command);
  1764. return 0;
  1765. }
  1766. static void ftrace_shutdown(struct ftrace_ops *ops, int command)
  1767. {
  1768. bool hash_disable = true;
  1769. if (unlikely(ftrace_disabled))
  1770. return;
  1771. ftrace_start_up--;
  1772. /*
  1773. * Just warn in case of unbalance, no need to kill ftrace, it's not
  1774. * critical but the ftrace_call callers may be never nopped again after
  1775. * further ftrace uses.
  1776. */
  1777. WARN_ON_ONCE(ftrace_start_up < 0);
  1778. if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
  1779. ops = &global_ops;
  1780. global_start_up--;
  1781. WARN_ON_ONCE(global_start_up < 0);
  1782. /* Don't update hash if global still has users */
  1783. if (global_start_up) {
  1784. WARN_ON_ONCE(!ftrace_start_up);
  1785. hash_disable = false;
  1786. }
  1787. }
  1788. if (hash_disable)
  1789. ftrace_hash_rec_disable(ops, 1);
  1790. if (ops != &global_ops || !global_start_up)
  1791. ops->flags &= ~FTRACE_OPS_FL_ENABLED;
  1792. command |= FTRACE_UPDATE_CALLS;
  1793. if (saved_ftrace_func != ftrace_trace_function) {
  1794. saved_ftrace_func = ftrace_trace_function;
  1795. command |= FTRACE_UPDATE_TRACE_FUNC;
  1796. }
  1797. if (!command || !ftrace_enabled)
  1798. return;
  1799. ftrace_run_update_code(command);
  1800. }
  1801. static void ftrace_startup_sysctl(void)
  1802. {
  1803. if (unlikely(ftrace_disabled))
  1804. return;
  1805. /* Force update next time */
  1806. saved_ftrace_func = NULL;
  1807. /* ftrace_start_up is true if we want ftrace running */
  1808. if (ftrace_start_up)
  1809. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  1810. }
  1811. static void ftrace_shutdown_sysctl(void)
  1812. {
  1813. if (unlikely(ftrace_disabled))
  1814. return;
  1815. /* ftrace_start_up is true if ftrace is running */
  1816. if (ftrace_start_up)
  1817. ftrace_run_update_code(FTRACE_DISABLE_CALLS);
  1818. }
  1819. static cycle_t ftrace_update_time;
  1820. static unsigned long ftrace_update_cnt;
  1821. unsigned long ftrace_update_tot_cnt;
  1822. static inline int ops_traces_mod(struct ftrace_ops *ops)
  1823. {
  1824. /*
  1825. * Filter_hash being empty will default to trace module.
  1826. * But notrace hash requires a test of individual module functions.
  1827. */
  1828. return ftrace_hash_empty(ops->filter_hash) &&
  1829. ftrace_hash_empty(ops->notrace_hash);
  1830. }
  1831. /*
  1832. * Check if the current ops references the record.
  1833. *
  1834. * If the ops traces all functions, then it was already accounted for.
  1835. * If the ops does not trace the current record function, skip it.
  1836. * If the ops ignores the function via notrace filter, skip it.
  1837. */
  1838. static inline bool
  1839. ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
  1840. {
  1841. /* If ops isn't enabled, ignore it */
  1842. if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
  1843. return 0;
  1844. /* If ops traces all mods, we already accounted for it */
  1845. if (ops_traces_mod(ops))
  1846. return 0;
  1847. /* The function must be in the filter */
  1848. if (!ftrace_hash_empty(ops->filter_hash) &&
  1849. !ftrace_lookup_ip(ops->filter_hash, rec->ip))
  1850. return 0;
  1851. /* If in notrace hash, we ignore it too */
  1852. if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
  1853. return 0;
  1854. return 1;
  1855. }
  1856. static int referenced_filters(struct dyn_ftrace *rec)
  1857. {
  1858. struct ftrace_ops *ops;
  1859. int cnt = 0;
  1860. for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
  1861. if (ops_references_rec(ops, rec))
  1862. cnt++;
  1863. }
  1864. return cnt;
  1865. }
  1866. static int ftrace_update_code(struct module *mod)
  1867. {
  1868. struct ftrace_page *pg;
  1869. struct dyn_ftrace *p;
  1870. cycle_t start, stop;
  1871. unsigned long ref = 0;
  1872. bool test = false;
  1873. int i;
  1874. /*
  1875. * When adding a module, we need to check if tracers are
  1876. * currently enabled and if they are set to trace all functions.
  1877. * If they are, we need to enable the module functions as well
  1878. * as update the reference counts for those function records.
  1879. */
  1880. if (mod) {
  1881. struct ftrace_ops *ops;
  1882. for (ops = ftrace_ops_list;
  1883. ops != &ftrace_list_end; ops = ops->next) {
  1884. if (ops->flags & FTRACE_OPS_FL_ENABLED) {
  1885. if (ops_traces_mod(ops))
  1886. ref++;
  1887. else
  1888. test = true;
  1889. }
  1890. }
  1891. }
  1892. start = ftrace_now(raw_smp_processor_id());
  1893. ftrace_update_cnt = 0;
  1894. for (pg = ftrace_new_pgs; pg; pg = pg->next) {
  1895. for (i = 0; i < pg->index; i++) {
  1896. int cnt = ref;
  1897. /* If something went wrong, bail without enabling anything */
  1898. if (unlikely(ftrace_disabled))
  1899. return -1;
  1900. p = &pg->records[i];
  1901. if (test)
  1902. cnt += referenced_filters(p);
  1903. p->flags = cnt;
  1904. /*
  1905. * Do the initial record conversion from mcount jump
  1906. * to the NOP instructions.
  1907. */
  1908. if (!ftrace_code_disable(mod, p))
  1909. break;
  1910. ftrace_update_cnt++;
  1911. /*
  1912. * If the tracing is enabled, go ahead and enable the record.
  1913. *
  1914. * The reason not to enable the record immediatelly is the
  1915. * inherent check of ftrace_make_nop/ftrace_make_call for
  1916. * correct previous instructions. Making first the NOP
  1917. * conversion puts the module to the correct state, thus
  1918. * passing the ftrace_make_call check.
  1919. */
  1920. if (ftrace_start_up && cnt) {
  1921. int failed = __ftrace_replace_code(p, 1);
  1922. if (failed)
  1923. ftrace_bug(failed, p->ip);
  1924. }
  1925. }
  1926. }
  1927. ftrace_new_pgs = NULL;
  1928. stop = ftrace_now(raw_smp_processor_id());
  1929. ftrace_update_time = stop - start;
  1930. ftrace_update_tot_cnt += ftrace_update_cnt;
  1931. return 0;
  1932. }
  1933. static int ftrace_allocate_records(struct ftrace_page *pg, int count)
  1934. {
  1935. int order;
  1936. int cnt;
  1937. if (WARN_ON(!count))
  1938. return -EINVAL;
  1939. order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
  1940. /*
  1941. * We want to fill as much as possible. No more than a page
  1942. * may be empty.
  1943. */
  1944. while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
  1945. order--;
  1946. again:
  1947. pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  1948. if (!pg->records) {
  1949. /* if we can't allocate this size, try something smaller */
  1950. if (!order)
  1951. return -ENOMEM;
  1952. order >>= 1;
  1953. goto again;
  1954. }
  1955. cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
  1956. pg->size = cnt;
  1957. if (cnt > count)
  1958. cnt = count;
  1959. return cnt;
  1960. }
  1961. static struct ftrace_page *
  1962. ftrace_allocate_pages(unsigned long num_to_init)
  1963. {
  1964. struct ftrace_page *start_pg;
  1965. struct ftrace_page *pg;
  1966. int order;
  1967. int cnt;
  1968. if (!num_to_init)
  1969. return 0;
  1970. start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  1971. if (!pg)
  1972. return NULL;
  1973. /*
  1974. * Try to allocate as much as possible in one continues
  1975. * location that fills in all of the space. We want to
  1976. * waste as little space as possible.
  1977. */
  1978. for (;;) {
  1979. cnt = ftrace_allocate_records(pg, num_to_init);
  1980. if (cnt < 0)
  1981. goto free_pages;
  1982. num_to_init -= cnt;
  1983. if (!num_to_init)
  1984. break;
  1985. pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
  1986. if (!pg->next)
  1987. goto free_pages;
  1988. pg = pg->next;
  1989. }
  1990. return start_pg;
  1991. free_pages:
  1992. while (start_pg) {
  1993. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  1994. free_pages((unsigned long)pg->records, order);
  1995. start_pg = pg->next;
  1996. kfree(pg);
  1997. pg = start_pg;
  1998. }
  1999. pr_info("ftrace: FAILED to allocate memory for functions\n");
  2000. return NULL;
  2001. }
  2002. static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
  2003. {
  2004. int cnt;
  2005. if (!num_to_init) {
  2006. pr_info("ftrace: No functions to be traced?\n");
  2007. return -1;
  2008. }
  2009. cnt = num_to_init / ENTRIES_PER_PAGE;
  2010. pr_info("ftrace: allocating %ld entries in %d pages\n",
  2011. num_to_init, cnt + 1);
  2012. return 0;
  2013. }
  2014. #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
  2015. struct ftrace_iterator {
  2016. loff_t pos;
  2017. loff_t func_pos;
  2018. struct ftrace_page *pg;
  2019. struct dyn_ftrace *func;
  2020. struct ftrace_func_probe *probe;
  2021. struct trace_parser parser;
  2022. struct ftrace_hash *hash;
  2023. struct ftrace_ops *ops;
  2024. int hidx;
  2025. int idx;
  2026. unsigned flags;
  2027. };
  2028. static void *
  2029. t_hash_next(struct seq_file *m, loff_t *pos)
  2030. {
  2031. struct ftrace_iterator *iter = m->private;
  2032. struct hlist_node *hnd = NULL;
  2033. struct hlist_head *hhd;
  2034. (*pos)++;
  2035. iter->pos = *pos;
  2036. if (iter->probe)
  2037. hnd = &iter->probe->node;
  2038. retry:
  2039. if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
  2040. return NULL;
  2041. hhd = &ftrace_func_hash[iter->hidx];
  2042. if (hlist_empty(hhd)) {
  2043. iter->hidx++;
  2044. hnd = NULL;
  2045. goto retry;
  2046. }
  2047. if (!hnd)
  2048. hnd = hhd->first;
  2049. else {
  2050. hnd = hnd->next;
  2051. if (!hnd) {
  2052. iter->hidx++;
  2053. goto retry;
  2054. }
  2055. }
  2056. if (WARN_ON_ONCE(!hnd))
  2057. return NULL;
  2058. iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
  2059. return iter;
  2060. }
  2061. static void *t_hash_start(struct seq_file *m, loff_t *pos)
  2062. {
  2063. struct ftrace_iterator *iter = m->private;
  2064. void *p = NULL;
  2065. loff_t l;
  2066. if (!(iter->flags & FTRACE_ITER_DO_HASH))
  2067. return NULL;
  2068. if (iter->func_pos > *pos)
  2069. return NULL;
  2070. iter->hidx = 0;
  2071. for (l = 0; l <= (*pos - iter->func_pos); ) {
  2072. p = t_hash_next(m, &l);
  2073. if (!p)
  2074. break;
  2075. }
  2076. if (!p)
  2077. return NULL;
  2078. /* Only set this if we have an item */
  2079. iter->flags |= FTRACE_ITER_HASH;
  2080. return iter;
  2081. }
  2082. static int
  2083. t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
  2084. {
  2085. struct ftrace_func_probe *rec;
  2086. rec = iter->probe;
  2087. if (WARN_ON_ONCE(!rec))
  2088. return -EIO;
  2089. if (rec->ops->print)
  2090. return rec->ops->print(m, rec->ip, rec->ops, rec->data);
  2091. seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
  2092. if (rec->data)
  2093. seq_printf(m, ":%p", rec->data);
  2094. seq_putc(m, '\n');
  2095. return 0;
  2096. }
  2097. static void *
  2098. t_next(struct seq_file *m, void *v, loff_t *pos)
  2099. {
  2100. struct ftrace_iterator *iter = m->private;
  2101. struct ftrace_ops *ops = iter->ops;
  2102. struct dyn_ftrace *rec = NULL;
  2103. if (unlikely(ftrace_disabled))
  2104. return NULL;
  2105. if (iter->flags & FTRACE_ITER_HASH)
  2106. return t_hash_next(m, pos);
  2107. (*pos)++;
  2108. iter->pos = iter->func_pos = *pos;
  2109. if (iter->flags & FTRACE_ITER_PRINTALL)
  2110. return t_hash_start(m, pos);
  2111. retry:
  2112. if (iter->idx >= iter->pg->index) {
  2113. if (iter->pg->next) {
  2114. iter->pg = iter->pg->next;
  2115. iter->idx = 0;
  2116. goto retry;
  2117. }
  2118. } else {
  2119. rec = &iter->pg->records[iter->idx++];
  2120. if (((iter->flags & FTRACE_ITER_FILTER) &&
  2121. !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
  2122. ((iter->flags & FTRACE_ITER_NOTRACE) &&
  2123. !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
  2124. ((iter->flags & FTRACE_ITER_ENABLED) &&
  2125. !(rec->flags & FTRACE_FL_ENABLED))) {
  2126. rec = NULL;
  2127. goto retry;
  2128. }
  2129. }
  2130. if (!rec)
  2131. return t_hash_start(m, pos);
  2132. iter->func = rec;
  2133. return iter;
  2134. }
  2135. static void reset_iter_read(struct ftrace_iterator *iter)
  2136. {
  2137. iter->pos = 0;
  2138. iter->func_pos = 0;
  2139. iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
  2140. }
  2141. static void *t_start(struct seq_file *m, loff_t *pos)
  2142. {
  2143. struct ftrace_iterator *iter = m->private;
  2144. struct ftrace_ops *ops = iter->ops;
  2145. void *p = NULL;
  2146. loff_t l;
  2147. mutex_lock(&ftrace_lock);
  2148. if (unlikely(ftrace_disabled))
  2149. return NULL;
  2150. /*
  2151. * If an lseek was done, then reset and start from beginning.
  2152. */
  2153. if (*pos < iter->pos)
  2154. reset_iter_read(iter);
  2155. /*
  2156. * For set_ftrace_filter reading, if we have the filter
  2157. * off, we can short cut and just print out that all
  2158. * functions are enabled.
  2159. */
  2160. if (iter->flags & FTRACE_ITER_FILTER &&
  2161. ftrace_hash_empty(ops->filter_hash)) {
  2162. if (*pos > 0)
  2163. return t_hash_start(m, pos);
  2164. iter->flags |= FTRACE_ITER_PRINTALL;
  2165. /* reset in case of seek/pread */
  2166. iter->flags &= ~FTRACE_ITER_HASH;
  2167. return iter;
  2168. }
  2169. if (iter->flags & FTRACE_ITER_HASH)
  2170. return t_hash_start(m, pos);
  2171. /*
  2172. * Unfortunately, we need to restart at ftrace_pages_start
  2173. * every time we let go of the ftrace_mutex. This is because
  2174. * those pointers can change without the lock.
  2175. */
  2176. iter->pg = ftrace_pages_start;
  2177. iter->idx = 0;
  2178. for (l = 0; l <= *pos; ) {
  2179. p = t_next(m, p, &l);
  2180. if (!p)
  2181. break;
  2182. }
  2183. if (!p)
  2184. return t_hash_start(m, pos);
  2185. return iter;
  2186. }
  2187. static void t_stop(struct seq_file *m, void *p)
  2188. {
  2189. mutex_unlock(&ftrace_lock);
  2190. }
  2191. static int t_show(struct seq_file *m, void *v)
  2192. {
  2193. struct ftrace_iterator *iter = m->private;
  2194. struct dyn_ftrace *rec;
  2195. if (iter->flags & FTRACE_ITER_HASH)
  2196. return t_hash_show(m, iter);
  2197. if (iter->flags & FTRACE_ITER_PRINTALL) {
  2198. seq_printf(m, "#### all functions enabled ####\n");
  2199. return 0;
  2200. }
  2201. rec = iter->func;
  2202. if (!rec)
  2203. return 0;
  2204. seq_printf(m, "%ps", (void *)rec->ip);
  2205. if (iter->flags & FTRACE_ITER_ENABLED)
  2206. seq_printf(m, " (%ld)%s",
  2207. rec->flags & ~FTRACE_FL_MASK,
  2208. rec->flags & FTRACE_FL_REGS ? " R" : "");
  2209. seq_printf(m, "\n");
  2210. return 0;
  2211. }
  2212. static const struct seq_operations show_ftrace_seq_ops = {
  2213. .start = t_start,
  2214. .next = t_next,
  2215. .stop = t_stop,
  2216. .show = t_show,
  2217. };
  2218. static int
  2219. ftrace_avail_open(struct inode *inode, struct file *file)
  2220. {
  2221. struct ftrace_iterator *iter;
  2222. if (unlikely(ftrace_disabled))
  2223. return -ENODEV;
  2224. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2225. if (iter) {
  2226. iter->pg = ftrace_pages_start;
  2227. iter->ops = &global_ops;
  2228. }
  2229. return iter ? 0 : -ENOMEM;
  2230. }
  2231. static int
  2232. ftrace_enabled_open(struct inode *inode, struct file *file)
  2233. {
  2234. struct ftrace_iterator *iter;
  2235. if (unlikely(ftrace_disabled))
  2236. return -ENODEV;
  2237. iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
  2238. if (iter) {
  2239. iter->pg = ftrace_pages_start;
  2240. iter->flags = FTRACE_ITER_ENABLED;
  2241. iter->ops = &global_ops;
  2242. }
  2243. return iter ? 0 : -ENOMEM;
  2244. }
  2245. static void ftrace_filter_reset(struct ftrace_hash *hash)
  2246. {
  2247. mutex_lock(&ftrace_lock);
  2248. ftrace_hash_clear(hash);
  2249. mutex_unlock(&ftrace_lock);
  2250. }
  2251. /**
  2252. * ftrace_regex_open - initialize function tracer filter files
  2253. * @ops: The ftrace_ops that hold the hash filters
  2254. * @flag: The type of filter to process
  2255. * @inode: The inode, usually passed in to your open routine
  2256. * @file: The file, usually passed in to your open routine
  2257. *
  2258. * ftrace_regex_open() initializes the filter files for the
  2259. * @ops. Depending on @flag it may process the filter hash or
  2260. * the notrace hash of @ops. With this called from the open
  2261. * routine, you can use ftrace_filter_write() for the write
  2262. * routine if @flag has FTRACE_ITER_FILTER set, or
  2263. * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
  2264. * ftrace_filter_lseek() should be used as the lseek routine, and
  2265. * release must call ftrace_regex_release().
  2266. */
  2267. int
  2268. ftrace_regex_open(struct ftrace_ops *ops, int flag,
  2269. struct inode *inode, struct file *file)
  2270. {
  2271. struct ftrace_iterator *iter;
  2272. struct ftrace_hash *hash;
  2273. int ret = 0;
  2274. ftrace_ops_init(ops);
  2275. if (unlikely(ftrace_disabled))
  2276. return -ENODEV;
  2277. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2278. if (!iter)
  2279. return -ENOMEM;
  2280. if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
  2281. kfree(iter);
  2282. return -ENOMEM;
  2283. }
  2284. iter->ops = ops;
  2285. iter->flags = flag;
  2286. mutex_lock(&ops->regex_lock);
  2287. if (flag & FTRACE_ITER_NOTRACE)
  2288. hash = ops->notrace_hash;
  2289. else
  2290. hash = ops->filter_hash;
  2291. if (file->f_mode & FMODE_WRITE) {
  2292. iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
  2293. if (!iter->hash) {
  2294. trace_parser_put(&iter->parser);
  2295. kfree(iter);
  2296. ret = -ENOMEM;
  2297. goto out_unlock;
  2298. }
  2299. }
  2300. if ((file->f_mode & FMODE_WRITE) &&
  2301. (file->f_flags & O_TRUNC))
  2302. ftrace_filter_reset(iter->hash);
  2303. if (file->f_mode & FMODE_READ) {
  2304. iter->pg = ftrace_pages_start;
  2305. ret = seq_open(file, &show_ftrace_seq_ops);
  2306. if (!ret) {
  2307. struct seq_file *m = file->private_data;
  2308. m->private = iter;
  2309. } else {
  2310. /* Failed */
  2311. free_ftrace_hash(iter->hash);
  2312. trace_parser_put(&iter->parser);
  2313. kfree(iter);
  2314. }
  2315. } else
  2316. file->private_data = iter;
  2317. out_unlock:
  2318. mutex_unlock(&ops->regex_lock);
  2319. return ret;
  2320. }
  2321. static int
  2322. ftrace_filter_open(struct inode *inode, struct file *file)
  2323. {
  2324. return ftrace_regex_open(&global_ops,
  2325. FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
  2326. inode, file);
  2327. }
  2328. static int
  2329. ftrace_notrace_open(struct inode *inode, struct file *file)
  2330. {
  2331. return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
  2332. inode, file);
  2333. }
  2334. static int ftrace_match(char *str, char *regex, int len, int type)
  2335. {
  2336. int matched = 0;
  2337. int slen;
  2338. switch (type) {
  2339. case MATCH_FULL:
  2340. if (strcmp(str, regex) == 0)
  2341. matched = 1;
  2342. break;
  2343. case MATCH_FRONT_ONLY:
  2344. if (strncmp(str, regex, len) == 0)
  2345. matched = 1;
  2346. break;
  2347. case MATCH_MIDDLE_ONLY:
  2348. if (strstr(str, regex))
  2349. matched = 1;
  2350. break;
  2351. case MATCH_END_ONLY:
  2352. slen = strlen(str);
  2353. if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
  2354. matched = 1;
  2355. break;
  2356. }
  2357. return matched;
  2358. }
  2359. static int
  2360. enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
  2361. {
  2362. struct ftrace_func_entry *entry;
  2363. int ret = 0;
  2364. entry = ftrace_lookup_ip(hash, rec->ip);
  2365. if (not) {
  2366. /* Do nothing if it doesn't exist */
  2367. if (!entry)
  2368. return 0;
  2369. free_hash_entry(hash, entry);
  2370. } else {
  2371. /* Do nothing if it exists */
  2372. if (entry)
  2373. return 0;
  2374. ret = add_hash_entry(hash, rec->ip);
  2375. }
  2376. return ret;
  2377. }
  2378. static int
  2379. ftrace_match_record(struct dyn_ftrace *rec, char *mod,
  2380. char *regex, int len, int type)
  2381. {
  2382. char str[KSYM_SYMBOL_LEN];
  2383. char *modname;
  2384. kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
  2385. if (mod) {
  2386. /* module lookup requires matching the module */
  2387. if (!modname || strcmp(modname, mod))
  2388. return 0;
  2389. /* blank search means to match all funcs in the mod */
  2390. if (!len)
  2391. return 1;
  2392. }
  2393. return ftrace_match(str, regex, len, type);
  2394. }
  2395. static int
  2396. match_records(struct ftrace_hash *hash, char *buff,
  2397. int len, char *mod, int not)
  2398. {
  2399. unsigned search_len = 0;
  2400. struct ftrace_page *pg;
  2401. struct dyn_ftrace *rec;
  2402. int type = MATCH_FULL;
  2403. char *search = buff;
  2404. int found = 0;
  2405. int ret;
  2406. if (len) {
  2407. type = filter_parse_regex(buff, len, &search, &not);
  2408. search_len = strlen(search);
  2409. }
  2410. mutex_lock(&ftrace_lock);
  2411. if (unlikely(ftrace_disabled))
  2412. goto out_unlock;
  2413. do_for_each_ftrace_rec(pg, rec) {
  2414. if (ftrace_match_record(rec, mod, search, search_len, type)) {
  2415. ret = enter_record(hash, rec, not);
  2416. if (ret < 0) {
  2417. found = ret;
  2418. goto out_unlock;
  2419. }
  2420. found = 1;
  2421. }
  2422. } while_for_each_ftrace_rec();
  2423. out_unlock:
  2424. mutex_unlock(&ftrace_lock);
  2425. return found;
  2426. }
  2427. static int
  2428. ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
  2429. {
  2430. return match_records(hash, buff, len, NULL, 0);
  2431. }
  2432. static int
  2433. ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
  2434. {
  2435. int not = 0;
  2436. /* blank or '*' mean the same */
  2437. if (strcmp(buff, "*") == 0)
  2438. buff[0] = 0;
  2439. /* handle the case of 'dont filter this module' */
  2440. if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
  2441. buff[0] = 0;
  2442. not = 1;
  2443. }
  2444. return match_records(hash, buff, strlen(buff), mod, not);
  2445. }
  2446. /*
  2447. * We register the module command as a template to show others how
  2448. * to register the a command as well.
  2449. */
  2450. static int
  2451. ftrace_mod_callback(struct ftrace_hash *hash,
  2452. char *func, char *cmd, char *param, int enable)
  2453. {
  2454. char *mod;
  2455. int ret = -EINVAL;
  2456. /*
  2457. * cmd == 'mod' because we only registered this func
  2458. * for the 'mod' ftrace_func_command.
  2459. * But if you register one func with multiple commands,
  2460. * you can tell which command was used by the cmd
  2461. * parameter.
  2462. */
  2463. /* we must have a module name */
  2464. if (!param)
  2465. return ret;
  2466. mod = strsep(&param, ":");
  2467. if (!strlen(mod))
  2468. return ret;
  2469. ret = ftrace_match_module_records(hash, func, mod);
  2470. if (!ret)
  2471. ret = -EINVAL;
  2472. if (ret < 0)
  2473. return ret;
  2474. return 0;
  2475. }
  2476. static struct ftrace_func_command ftrace_mod_cmd = {
  2477. .name = "mod",
  2478. .func = ftrace_mod_callback,
  2479. };
  2480. static int __init ftrace_mod_cmd_init(void)
  2481. {
  2482. return register_ftrace_command(&ftrace_mod_cmd);
  2483. }
  2484. core_initcall(ftrace_mod_cmd_init);
  2485. static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
  2486. struct ftrace_ops *op, struct pt_regs *pt_regs)
  2487. {
  2488. struct ftrace_func_probe *entry;
  2489. struct hlist_head *hhd;
  2490. unsigned long key;
  2491. key = hash_long(ip, FTRACE_HASH_BITS);
  2492. hhd = &ftrace_func_hash[key];
  2493. if (hlist_empty(hhd))
  2494. return;
  2495. /*
  2496. * Disable preemption for these calls to prevent a RCU grace
  2497. * period. This syncs the hash iteration and freeing of items
  2498. * on the hash. rcu_read_lock is too dangerous here.
  2499. */
  2500. preempt_disable_notrace();
  2501. hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
  2502. if (entry->ip == ip)
  2503. entry->ops->func(ip, parent_ip, &entry->data);
  2504. }
  2505. preempt_enable_notrace();
  2506. }
  2507. static struct ftrace_ops trace_probe_ops __read_mostly =
  2508. {
  2509. .func = function_trace_probe_call,
  2510. .flags = FTRACE_OPS_FL_INITIALIZED,
  2511. INIT_REGEX_LOCK(trace_probe_ops)
  2512. };
  2513. static int ftrace_probe_registered;
  2514. static void __enable_ftrace_function_probe(void)
  2515. {
  2516. int ret;
  2517. int i;
  2518. if (ftrace_probe_registered) {
  2519. /* still need to update the function call sites */
  2520. if (ftrace_enabled)
  2521. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  2522. return;
  2523. }
  2524. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2525. struct hlist_head *hhd = &ftrace_func_hash[i];
  2526. if (hhd->first)
  2527. break;
  2528. }
  2529. /* Nothing registered? */
  2530. if (i == FTRACE_FUNC_HASHSIZE)
  2531. return;
  2532. ret = __register_ftrace_function(&trace_probe_ops);
  2533. if (!ret)
  2534. ret = ftrace_startup(&trace_probe_ops, 0);
  2535. ftrace_probe_registered = 1;
  2536. }
  2537. static void __disable_ftrace_function_probe(void)
  2538. {
  2539. int ret;
  2540. int i;
  2541. if (!ftrace_probe_registered)
  2542. return;
  2543. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2544. struct hlist_head *hhd = &ftrace_func_hash[i];
  2545. if (hhd->first)
  2546. return;
  2547. }
  2548. /* no more funcs left */
  2549. ret = __unregister_ftrace_function(&trace_probe_ops);
  2550. if (!ret)
  2551. ftrace_shutdown(&trace_probe_ops, 0);
  2552. ftrace_probe_registered = 0;
  2553. }
  2554. static void ftrace_free_entry(struct ftrace_func_probe *entry)
  2555. {
  2556. if (entry->ops->free)
  2557. entry->ops->free(entry->ops, entry->ip, &entry->data);
  2558. kfree(entry);
  2559. }
  2560. int
  2561. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2562. void *data)
  2563. {
  2564. struct ftrace_func_probe *entry;
  2565. struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
  2566. struct ftrace_hash *hash;
  2567. struct ftrace_page *pg;
  2568. struct dyn_ftrace *rec;
  2569. int type, len, not;
  2570. unsigned long key;
  2571. int count = 0;
  2572. char *search;
  2573. int ret;
  2574. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2575. len = strlen(search);
  2576. /* we do not support '!' for function probes */
  2577. if (WARN_ON(not))
  2578. return -EINVAL;
  2579. mutex_lock(&trace_probe_ops.regex_lock);
  2580. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  2581. if (!hash) {
  2582. count = -ENOMEM;
  2583. goto out;
  2584. }
  2585. if (unlikely(ftrace_disabled)) {
  2586. count = -ENODEV;
  2587. goto out;
  2588. }
  2589. mutex_lock(&ftrace_lock);
  2590. do_for_each_ftrace_rec(pg, rec) {
  2591. if (!ftrace_match_record(rec, NULL, search, len, type))
  2592. continue;
  2593. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2594. if (!entry) {
  2595. /* If we did not process any, then return error */
  2596. if (!count)
  2597. count = -ENOMEM;
  2598. goto out_unlock;
  2599. }
  2600. count++;
  2601. entry->data = data;
  2602. /*
  2603. * The caller might want to do something special
  2604. * for each function we find. We call the callback
  2605. * to give the caller an opportunity to do so.
  2606. */
  2607. if (ops->init) {
  2608. if (ops->init(ops, rec->ip, &entry->data) < 0) {
  2609. /* caller does not like this func */
  2610. kfree(entry);
  2611. continue;
  2612. }
  2613. }
  2614. ret = enter_record(hash, rec, 0);
  2615. if (ret < 0) {
  2616. kfree(entry);
  2617. count = ret;
  2618. goto out_unlock;
  2619. }
  2620. entry->ops = ops;
  2621. entry->ip = rec->ip;
  2622. key = hash_long(entry->ip, FTRACE_HASH_BITS);
  2623. hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
  2624. } while_for_each_ftrace_rec();
  2625. ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
  2626. if (ret < 0)
  2627. count = ret;
  2628. __enable_ftrace_function_probe();
  2629. out_unlock:
  2630. mutex_unlock(&ftrace_lock);
  2631. out:
  2632. mutex_unlock(&trace_probe_ops.regex_lock);
  2633. free_ftrace_hash(hash);
  2634. return count;
  2635. }
  2636. enum {
  2637. PROBE_TEST_FUNC = 1,
  2638. PROBE_TEST_DATA = 2
  2639. };
  2640. static void
  2641. __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2642. void *data, int flags)
  2643. {
  2644. struct ftrace_func_entry *rec_entry;
  2645. struct ftrace_func_probe *entry;
  2646. struct ftrace_func_probe *p;
  2647. struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
  2648. struct list_head free_list;
  2649. struct ftrace_hash *hash;
  2650. struct hlist_node *tmp;
  2651. char str[KSYM_SYMBOL_LEN];
  2652. int type = MATCH_FULL;
  2653. int i, len = 0;
  2654. char *search;
  2655. if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
  2656. glob = NULL;
  2657. else if (glob) {
  2658. int not;
  2659. type = filter_parse_regex(glob, strlen(glob), &search, &not);
  2660. len = strlen(search);
  2661. /* we do not support '!' for function probes */
  2662. if (WARN_ON(not))
  2663. return;
  2664. }
  2665. mutex_lock(&trace_probe_ops.regex_lock);
  2666. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  2667. if (!hash)
  2668. /* Hmm, should report this somehow */
  2669. goto out_unlock;
  2670. INIT_LIST_HEAD(&free_list);
  2671. for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
  2672. struct hlist_head *hhd = &ftrace_func_hash[i];
  2673. hlist_for_each_entry_safe(entry, tmp, hhd, node) {
  2674. /* break up if statements for readability */
  2675. if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
  2676. continue;
  2677. if ((flags & PROBE_TEST_DATA) && entry->data != data)
  2678. continue;
  2679. /* do this last, since it is the most expensive */
  2680. if (glob) {
  2681. kallsyms_lookup(entry->ip, NULL, NULL,
  2682. NULL, str);
  2683. if (!ftrace_match(str, glob, len, type))
  2684. continue;
  2685. }
  2686. rec_entry = ftrace_lookup_ip(hash, entry->ip);
  2687. /* It is possible more than one entry had this ip */
  2688. if (rec_entry)
  2689. free_hash_entry(hash, rec_entry);
  2690. hlist_del_rcu(&entry->node);
  2691. list_add(&entry->free_list, &free_list);
  2692. }
  2693. }
  2694. mutex_lock(&ftrace_lock);
  2695. __disable_ftrace_function_probe();
  2696. /*
  2697. * Remove after the disable is called. Otherwise, if the last
  2698. * probe is removed, a null hash means *all enabled*.
  2699. */
  2700. ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
  2701. synchronize_sched();
  2702. list_for_each_entry_safe(entry, p, &free_list, free_list) {
  2703. list_del(&entry->free_list);
  2704. ftrace_free_entry(entry);
  2705. }
  2706. mutex_unlock(&ftrace_lock);
  2707. out_unlock:
  2708. mutex_unlock(&trace_probe_ops.regex_lock);
  2709. free_ftrace_hash(hash);
  2710. }
  2711. void
  2712. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  2713. void *data)
  2714. {
  2715. __unregister_ftrace_function_probe(glob, ops, data,
  2716. PROBE_TEST_FUNC | PROBE_TEST_DATA);
  2717. }
  2718. void
  2719. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
  2720. {
  2721. __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
  2722. }
  2723. void unregister_ftrace_function_probe_all(char *glob)
  2724. {
  2725. __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
  2726. }
  2727. static LIST_HEAD(ftrace_commands);
  2728. static DEFINE_MUTEX(ftrace_cmd_mutex);
  2729. int register_ftrace_command(struct ftrace_func_command *cmd)
  2730. {
  2731. struct ftrace_func_command *p;
  2732. int ret = 0;
  2733. mutex_lock(&ftrace_cmd_mutex);
  2734. list_for_each_entry(p, &ftrace_commands, list) {
  2735. if (strcmp(cmd->name, p->name) == 0) {
  2736. ret = -EBUSY;
  2737. goto out_unlock;
  2738. }
  2739. }
  2740. list_add(&cmd->list, &ftrace_commands);
  2741. out_unlock:
  2742. mutex_unlock(&ftrace_cmd_mutex);
  2743. return ret;
  2744. }
  2745. int unregister_ftrace_command(struct ftrace_func_command *cmd)
  2746. {
  2747. struct ftrace_func_command *p, *n;
  2748. int ret = -ENODEV;
  2749. mutex_lock(&ftrace_cmd_mutex);
  2750. list_for_each_entry_safe(p, n, &ftrace_commands, list) {
  2751. if (strcmp(cmd->name, p->name) == 0) {
  2752. ret = 0;
  2753. list_del_init(&p->list);
  2754. goto out_unlock;
  2755. }
  2756. }
  2757. out_unlock:
  2758. mutex_unlock(&ftrace_cmd_mutex);
  2759. return ret;
  2760. }
  2761. static int ftrace_process_regex(struct ftrace_hash *hash,
  2762. char *buff, int len, int enable)
  2763. {
  2764. char *func, *command, *next = buff;
  2765. struct ftrace_func_command *p;
  2766. int ret = -EINVAL;
  2767. func = strsep(&next, ":");
  2768. if (!next) {
  2769. ret = ftrace_match_records(hash, func, len);
  2770. if (!ret)
  2771. ret = -EINVAL;
  2772. if (ret < 0)
  2773. return ret;
  2774. return 0;
  2775. }
  2776. /* command found */
  2777. command = strsep(&next, ":");
  2778. mutex_lock(&ftrace_cmd_mutex);
  2779. list_for_each_entry(p, &ftrace_commands, list) {
  2780. if (strcmp(p->name, command) == 0) {
  2781. ret = p->func(hash, func, command, next, enable);
  2782. goto out_unlock;
  2783. }
  2784. }
  2785. out_unlock:
  2786. mutex_unlock(&ftrace_cmd_mutex);
  2787. return ret;
  2788. }
  2789. static ssize_t
  2790. ftrace_regex_write(struct file *file, const char __user *ubuf,
  2791. size_t cnt, loff_t *ppos, int enable)
  2792. {
  2793. struct ftrace_iterator *iter;
  2794. struct trace_parser *parser;
  2795. ssize_t ret, read;
  2796. if (!cnt)
  2797. return 0;
  2798. if (file->f_mode & FMODE_READ) {
  2799. struct seq_file *m = file->private_data;
  2800. iter = m->private;
  2801. } else
  2802. iter = file->private_data;
  2803. if (unlikely(ftrace_disabled))
  2804. return -ENODEV;
  2805. /* iter->hash is a local copy, so we don't need regex_lock */
  2806. parser = &iter->parser;
  2807. read = trace_get_user(parser, ubuf, cnt, ppos);
  2808. if (read >= 0 && trace_parser_loaded(parser) &&
  2809. !trace_parser_cont(parser)) {
  2810. ret = ftrace_process_regex(iter->hash, parser->buffer,
  2811. parser->idx, enable);
  2812. trace_parser_clear(parser);
  2813. if (ret < 0)
  2814. goto out;
  2815. }
  2816. ret = read;
  2817. out:
  2818. return ret;
  2819. }
  2820. ssize_t
  2821. ftrace_filter_write(struct file *file, const char __user *ubuf,
  2822. size_t cnt, loff_t *ppos)
  2823. {
  2824. return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
  2825. }
  2826. ssize_t
  2827. ftrace_notrace_write(struct file *file, const char __user *ubuf,
  2828. size_t cnt, loff_t *ppos)
  2829. {
  2830. return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
  2831. }
  2832. static int
  2833. ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
  2834. {
  2835. struct ftrace_func_entry *entry;
  2836. if (!ftrace_location(ip))
  2837. return -EINVAL;
  2838. if (remove) {
  2839. entry = ftrace_lookup_ip(hash, ip);
  2840. if (!entry)
  2841. return -ENOENT;
  2842. free_hash_entry(hash, entry);
  2843. return 0;
  2844. }
  2845. return add_hash_entry(hash, ip);
  2846. }
  2847. static void ftrace_ops_update_code(struct ftrace_ops *ops)
  2848. {
  2849. if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
  2850. ftrace_run_update_code(FTRACE_UPDATE_CALLS);
  2851. }
  2852. static int
  2853. ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
  2854. unsigned long ip, int remove, int reset, int enable)
  2855. {
  2856. struct ftrace_hash **orig_hash;
  2857. struct ftrace_hash *hash;
  2858. int ret;
  2859. /* All global ops uses the global ops filters */
  2860. if (ops->flags & FTRACE_OPS_FL_GLOBAL)
  2861. ops = &global_ops;
  2862. if (unlikely(ftrace_disabled))
  2863. return -ENODEV;
  2864. mutex_lock(&ops->regex_lock);
  2865. if (enable)
  2866. orig_hash = &ops->filter_hash;
  2867. else
  2868. orig_hash = &ops->notrace_hash;
  2869. hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
  2870. if (!hash) {
  2871. ret = -ENOMEM;
  2872. goto out_regex_unlock;
  2873. }
  2874. if (reset)
  2875. ftrace_filter_reset(hash);
  2876. if (buf && !ftrace_match_records(hash, buf, len)) {
  2877. ret = -EINVAL;
  2878. goto out_regex_unlock;
  2879. }
  2880. if (ip) {
  2881. ret = ftrace_match_addr(hash, ip, remove);
  2882. if (ret < 0)
  2883. goto out_regex_unlock;
  2884. }
  2885. mutex_lock(&ftrace_lock);
  2886. ret = ftrace_hash_move(ops, enable, orig_hash, hash);
  2887. if (!ret)
  2888. ftrace_ops_update_code(ops);
  2889. mutex_unlock(&ftrace_lock);
  2890. out_regex_unlock:
  2891. mutex_unlock(&ops->regex_lock);
  2892. free_ftrace_hash(hash);
  2893. return ret;
  2894. }
  2895. static int
  2896. ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
  2897. int reset, int enable)
  2898. {
  2899. return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
  2900. }
  2901. /**
  2902. * ftrace_set_filter_ip - set a function to filter on in ftrace by address
  2903. * @ops - the ops to set the filter with
  2904. * @ip - the address to add to or remove from the filter.
  2905. * @remove - non zero to remove the ip from the filter
  2906. * @reset - non zero to reset all filters before applying this filter.
  2907. *
  2908. * Filters denote which functions should be enabled when tracing is enabled
  2909. * If @ip is NULL, it failes to update filter.
  2910. */
  2911. int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
  2912. int remove, int reset)
  2913. {
  2914. ftrace_ops_init(ops);
  2915. return ftrace_set_addr(ops, ip, remove, reset, 1);
  2916. }
  2917. EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
  2918. static int
  2919. ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
  2920. int reset, int enable)
  2921. {
  2922. return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
  2923. }
  2924. /**
  2925. * ftrace_set_filter - set a function to filter on in ftrace
  2926. * @ops - the ops to set the filter with
  2927. * @buf - the string that holds the function filter text.
  2928. * @len - the length of the string.
  2929. * @reset - non zero to reset all filters before applying this filter.
  2930. *
  2931. * Filters denote which functions should be enabled when tracing is enabled.
  2932. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2933. */
  2934. int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  2935. int len, int reset)
  2936. {
  2937. ftrace_ops_init(ops);
  2938. return ftrace_set_regex(ops, buf, len, reset, 1);
  2939. }
  2940. EXPORT_SYMBOL_GPL(ftrace_set_filter);
  2941. /**
  2942. * ftrace_set_notrace - set a function to not trace in ftrace
  2943. * @ops - the ops to set the notrace filter with
  2944. * @buf - the string that holds the function notrace text.
  2945. * @len - the length of the string.
  2946. * @reset - non zero to reset all filters before applying this filter.
  2947. *
  2948. * Notrace Filters denote which functions should not be enabled when tracing
  2949. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2950. * for tracing.
  2951. */
  2952. int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  2953. int len, int reset)
  2954. {
  2955. ftrace_ops_init(ops);
  2956. return ftrace_set_regex(ops, buf, len, reset, 0);
  2957. }
  2958. EXPORT_SYMBOL_GPL(ftrace_set_notrace);
  2959. /**
  2960. * ftrace_set_filter - set a function to filter on in ftrace
  2961. * @ops - the ops to set the filter with
  2962. * @buf - the string that holds the function filter text.
  2963. * @len - the length of the string.
  2964. * @reset - non zero to reset all filters before applying this filter.
  2965. *
  2966. * Filters denote which functions should be enabled when tracing is enabled.
  2967. * If @buf is NULL and reset is set, all functions will be enabled for tracing.
  2968. */
  2969. void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
  2970. {
  2971. ftrace_set_regex(&global_ops, buf, len, reset, 1);
  2972. }
  2973. EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
  2974. /**
  2975. * ftrace_set_notrace - set a function to not trace in ftrace
  2976. * @ops - the ops to set the notrace filter with
  2977. * @buf - the string that holds the function notrace text.
  2978. * @len - the length of the string.
  2979. * @reset - non zero to reset all filters before applying this filter.
  2980. *
  2981. * Notrace Filters denote which functions should not be enabled when tracing
  2982. * is enabled. If @buf is NULL and reset is set, all functions will be enabled
  2983. * for tracing.
  2984. */
  2985. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
  2986. {
  2987. ftrace_set_regex(&global_ops, buf, len, reset, 0);
  2988. }
  2989. EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
  2990. /*
  2991. * command line interface to allow users to set filters on boot up.
  2992. */
  2993. #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
  2994. static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
  2995. static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
  2996. /* Used by function selftest to not test if filter is set */
  2997. bool ftrace_filter_param __initdata;
  2998. static int __init set_ftrace_notrace(char *str)
  2999. {
  3000. ftrace_filter_param = true;
  3001. strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
  3002. return 1;
  3003. }
  3004. __setup("ftrace_notrace=", set_ftrace_notrace);
  3005. static int __init set_ftrace_filter(char *str)
  3006. {
  3007. ftrace_filter_param = true;
  3008. strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
  3009. return 1;
  3010. }
  3011. __setup("ftrace_filter=", set_ftrace_filter);
  3012. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3013. static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
  3014. static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
  3015. static int __init set_graph_function(char *str)
  3016. {
  3017. strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
  3018. return 1;
  3019. }
  3020. __setup("ftrace_graph_filter=", set_graph_function);
  3021. static void __init set_ftrace_early_graph(char *buf)
  3022. {
  3023. int ret;
  3024. char *func;
  3025. while (buf) {
  3026. func = strsep(&buf, ",");
  3027. /* we allow only one expression at a time */
  3028. ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
  3029. FTRACE_GRAPH_MAX_FUNCS, func);
  3030. if (ret)
  3031. printk(KERN_DEBUG "ftrace: function %s not "
  3032. "traceable\n", func);
  3033. }
  3034. }
  3035. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3036. void __init
  3037. ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
  3038. {
  3039. char *func;
  3040. ftrace_ops_init(ops);
  3041. while (buf) {
  3042. func = strsep(&buf, ",");
  3043. ftrace_set_regex(ops, func, strlen(func), 0, enable);
  3044. }
  3045. }
  3046. static void __init set_ftrace_early_filters(void)
  3047. {
  3048. if (ftrace_filter_buf[0])
  3049. ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
  3050. if (ftrace_notrace_buf[0])
  3051. ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
  3052. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3053. if (ftrace_graph_buf[0])
  3054. set_ftrace_early_graph(ftrace_graph_buf);
  3055. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3056. }
  3057. int ftrace_regex_release(struct inode *inode, struct file *file)
  3058. {
  3059. struct seq_file *m = (struct seq_file *)file->private_data;
  3060. struct ftrace_iterator *iter;
  3061. struct ftrace_hash **orig_hash;
  3062. struct trace_parser *parser;
  3063. int filter_hash;
  3064. int ret;
  3065. if (file->f_mode & FMODE_READ) {
  3066. iter = m->private;
  3067. seq_release(inode, file);
  3068. } else
  3069. iter = file->private_data;
  3070. parser = &iter->parser;
  3071. if (trace_parser_loaded(parser)) {
  3072. parser->buffer[parser->idx] = 0;
  3073. ftrace_match_records(iter->hash, parser->buffer, parser->idx);
  3074. }
  3075. trace_parser_put(parser);
  3076. mutex_lock(&iter->ops->regex_lock);
  3077. if (file->f_mode & FMODE_WRITE) {
  3078. filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
  3079. if (filter_hash)
  3080. orig_hash = &iter->ops->filter_hash;
  3081. else
  3082. orig_hash = &iter->ops->notrace_hash;
  3083. mutex_lock(&ftrace_lock);
  3084. ret = ftrace_hash_move(iter->ops, filter_hash,
  3085. orig_hash, iter->hash);
  3086. if (!ret)
  3087. ftrace_ops_update_code(iter->ops);
  3088. mutex_unlock(&ftrace_lock);
  3089. }
  3090. mutex_unlock(&iter->ops->regex_lock);
  3091. free_ftrace_hash(iter->hash);
  3092. kfree(iter);
  3093. return 0;
  3094. }
  3095. static const struct file_operations ftrace_avail_fops = {
  3096. .open = ftrace_avail_open,
  3097. .read = seq_read,
  3098. .llseek = seq_lseek,
  3099. .release = seq_release_private,
  3100. };
  3101. static const struct file_operations ftrace_enabled_fops = {
  3102. .open = ftrace_enabled_open,
  3103. .read = seq_read,
  3104. .llseek = seq_lseek,
  3105. .release = seq_release_private,
  3106. };
  3107. static const struct file_operations ftrace_filter_fops = {
  3108. .open = ftrace_filter_open,
  3109. .read = seq_read,
  3110. .write = ftrace_filter_write,
  3111. .llseek = ftrace_filter_lseek,
  3112. .release = ftrace_regex_release,
  3113. };
  3114. static const struct file_operations ftrace_notrace_fops = {
  3115. .open = ftrace_notrace_open,
  3116. .read = seq_read,
  3117. .write = ftrace_notrace_write,
  3118. .llseek = ftrace_filter_lseek,
  3119. .release = ftrace_regex_release,
  3120. };
  3121. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3122. static DEFINE_MUTEX(graph_lock);
  3123. int ftrace_graph_count;
  3124. int ftrace_graph_notrace_count;
  3125. unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  3126. unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
  3127. struct ftrace_graph_data {
  3128. unsigned long *table;
  3129. size_t size;
  3130. int *count;
  3131. const struct seq_operations *seq_ops;
  3132. };
  3133. static void *
  3134. __g_next(struct seq_file *m, loff_t *pos)
  3135. {
  3136. struct ftrace_graph_data *fgd = m->private;
  3137. if (*pos >= *fgd->count)
  3138. return NULL;
  3139. return &fgd->table[*pos];
  3140. }
  3141. static void *
  3142. g_next(struct seq_file *m, void *v, loff_t *pos)
  3143. {
  3144. (*pos)++;
  3145. return __g_next(m, pos);
  3146. }
  3147. static void *g_start(struct seq_file *m, loff_t *pos)
  3148. {
  3149. struct ftrace_graph_data *fgd = m->private;
  3150. mutex_lock(&graph_lock);
  3151. /* Nothing, tell g_show to print all functions are enabled */
  3152. if (!*fgd->count && !*pos)
  3153. return (void *)1;
  3154. return __g_next(m, pos);
  3155. }
  3156. static void g_stop(struct seq_file *m, void *p)
  3157. {
  3158. mutex_unlock(&graph_lock);
  3159. }
  3160. static int g_show(struct seq_file *m, void *v)
  3161. {
  3162. unsigned long *ptr = v;
  3163. if (!ptr)
  3164. return 0;
  3165. if (ptr == (unsigned long *)1) {
  3166. seq_printf(m, "#### all functions enabled ####\n");
  3167. return 0;
  3168. }
  3169. seq_printf(m, "%ps\n", (void *)*ptr);
  3170. return 0;
  3171. }
  3172. static const struct seq_operations ftrace_graph_seq_ops = {
  3173. .start = g_start,
  3174. .next = g_next,
  3175. .stop = g_stop,
  3176. .show = g_show,
  3177. };
  3178. static int
  3179. __ftrace_graph_open(struct inode *inode, struct file *file,
  3180. struct ftrace_graph_data *fgd)
  3181. {
  3182. int ret = 0;
  3183. mutex_lock(&graph_lock);
  3184. if ((file->f_mode & FMODE_WRITE) &&
  3185. (file->f_flags & O_TRUNC)) {
  3186. *fgd->count = 0;
  3187. memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
  3188. }
  3189. mutex_unlock(&graph_lock);
  3190. if (file->f_mode & FMODE_READ) {
  3191. ret = seq_open(file, fgd->seq_ops);
  3192. if (!ret) {
  3193. struct seq_file *m = file->private_data;
  3194. m->private = fgd;
  3195. }
  3196. } else
  3197. file->private_data = fgd;
  3198. return ret;
  3199. }
  3200. static int
  3201. ftrace_graph_open(struct inode *inode, struct file *file)
  3202. {
  3203. struct ftrace_graph_data *fgd;
  3204. if (unlikely(ftrace_disabled))
  3205. return -ENODEV;
  3206. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  3207. if (fgd == NULL)
  3208. return -ENOMEM;
  3209. fgd->table = ftrace_graph_funcs;
  3210. fgd->size = FTRACE_GRAPH_MAX_FUNCS;
  3211. fgd->count = &ftrace_graph_count;
  3212. fgd->seq_ops = &ftrace_graph_seq_ops;
  3213. return __ftrace_graph_open(inode, file, fgd);
  3214. }
  3215. static int
  3216. ftrace_graph_notrace_open(struct inode *inode, struct file *file)
  3217. {
  3218. struct ftrace_graph_data *fgd;
  3219. if (unlikely(ftrace_disabled))
  3220. return -ENODEV;
  3221. fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
  3222. if (fgd == NULL)
  3223. return -ENOMEM;
  3224. fgd->table = ftrace_graph_notrace_funcs;
  3225. fgd->size = FTRACE_GRAPH_MAX_FUNCS;
  3226. fgd->count = &ftrace_graph_notrace_count;
  3227. fgd->seq_ops = &ftrace_graph_seq_ops;
  3228. return __ftrace_graph_open(inode, file, fgd);
  3229. }
  3230. static int
  3231. ftrace_graph_release(struct inode *inode, struct file *file)
  3232. {
  3233. if (file->f_mode & FMODE_READ) {
  3234. struct seq_file *m = file->private_data;
  3235. kfree(m->private);
  3236. seq_release(inode, file);
  3237. } else {
  3238. kfree(file->private_data);
  3239. }
  3240. return 0;
  3241. }
  3242. static int
  3243. ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
  3244. {
  3245. struct dyn_ftrace *rec;
  3246. struct ftrace_page *pg;
  3247. int search_len;
  3248. int fail = 1;
  3249. int type, not;
  3250. char *search;
  3251. bool exists;
  3252. int i;
  3253. /* decode regex */
  3254. type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
  3255. if (!not && *idx >= size)
  3256. return -EBUSY;
  3257. search_len = strlen(search);
  3258. mutex_lock(&ftrace_lock);
  3259. if (unlikely(ftrace_disabled)) {
  3260. mutex_unlock(&ftrace_lock);
  3261. return -ENODEV;
  3262. }
  3263. do_for_each_ftrace_rec(pg, rec) {
  3264. if (ftrace_match_record(rec, NULL, search, search_len, type)) {
  3265. /* if it is in the array */
  3266. exists = false;
  3267. for (i = 0; i < *idx; i++) {
  3268. if (array[i] == rec->ip) {
  3269. exists = true;
  3270. break;
  3271. }
  3272. }
  3273. if (!not) {
  3274. fail = 0;
  3275. if (!exists) {
  3276. array[(*idx)++] = rec->ip;
  3277. if (*idx >= size)
  3278. goto out;
  3279. }
  3280. } else {
  3281. if (exists) {
  3282. array[i] = array[--(*idx)];
  3283. array[*idx] = 0;
  3284. fail = 0;
  3285. }
  3286. }
  3287. }
  3288. } while_for_each_ftrace_rec();
  3289. out:
  3290. mutex_unlock(&ftrace_lock);
  3291. if (fail)
  3292. return -EINVAL;
  3293. return 0;
  3294. }
  3295. static ssize_t
  3296. ftrace_graph_write(struct file *file, const char __user *ubuf,
  3297. size_t cnt, loff_t *ppos)
  3298. {
  3299. struct trace_parser parser;
  3300. ssize_t read, ret = 0;
  3301. struct ftrace_graph_data *fgd = file->private_data;
  3302. if (!cnt)
  3303. return 0;
  3304. if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
  3305. return -ENOMEM;
  3306. read = trace_get_user(&parser, ubuf, cnt, ppos);
  3307. if (read >= 0 && trace_parser_loaded((&parser))) {
  3308. parser.buffer[parser.idx] = 0;
  3309. mutex_lock(&graph_lock);
  3310. /* we allow only one expression at a time */
  3311. ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
  3312. parser.buffer);
  3313. mutex_unlock(&graph_lock);
  3314. }
  3315. if (!ret)
  3316. ret = read;
  3317. trace_parser_put(&parser);
  3318. return ret;
  3319. }
  3320. static const struct file_operations ftrace_graph_fops = {
  3321. .open = ftrace_graph_open,
  3322. .read = seq_read,
  3323. .write = ftrace_graph_write,
  3324. .llseek = ftrace_filter_lseek,
  3325. .release = ftrace_graph_release,
  3326. };
  3327. static const struct file_operations ftrace_graph_notrace_fops = {
  3328. .open = ftrace_graph_notrace_open,
  3329. .read = seq_read,
  3330. .write = ftrace_graph_write,
  3331. .llseek = ftrace_filter_lseek,
  3332. .release = ftrace_graph_release,
  3333. };
  3334. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3335. static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
  3336. {
  3337. trace_create_file("available_filter_functions", 0444,
  3338. d_tracer, NULL, &ftrace_avail_fops);
  3339. trace_create_file("enabled_functions", 0444,
  3340. d_tracer, NULL, &ftrace_enabled_fops);
  3341. trace_create_file("set_ftrace_filter", 0644, d_tracer,
  3342. NULL, &ftrace_filter_fops);
  3343. trace_create_file("set_ftrace_notrace", 0644, d_tracer,
  3344. NULL, &ftrace_notrace_fops);
  3345. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3346. trace_create_file("set_graph_function", 0444, d_tracer,
  3347. NULL,
  3348. &ftrace_graph_fops);
  3349. trace_create_file("set_graph_notrace", 0444, d_tracer,
  3350. NULL,
  3351. &ftrace_graph_notrace_fops);
  3352. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  3353. return 0;
  3354. }
  3355. static int ftrace_cmp_ips(const void *a, const void *b)
  3356. {
  3357. const unsigned long *ipa = a;
  3358. const unsigned long *ipb = b;
  3359. if (*ipa > *ipb)
  3360. return 1;
  3361. if (*ipa < *ipb)
  3362. return -1;
  3363. return 0;
  3364. }
  3365. static void ftrace_swap_ips(void *a, void *b, int size)
  3366. {
  3367. unsigned long *ipa = a;
  3368. unsigned long *ipb = b;
  3369. unsigned long t;
  3370. t = *ipa;
  3371. *ipa = *ipb;
  3372. *ipb = t;
  3373. }
  3374. static int ftrace_process_locs(struct module *mod,
  3375. unsigned long *start,
  3376. unsigned long *end)
  3377. {
  3378. struct ftrace_page *start_pg;
  3379. struct ftrace_page *pg;
  3380. struct dyn_ftrace *rec;
  3381. unsigned long count;
  3382. unsigned long *p;
  3383. unsigned long addr;
  3384. unsigned long flags = 0; /* Shut up gcc */
  3385. int ret = -ENOMEM;
  3386. count = end - start;
  3387. if (!count)
  3388. return 0;
  3389. sort(start, count, sizeof(*start),
  3390. ftrace_cmp_ips, ftrace_swap_ips);
  3391. start_pg = ftrace_allocate_pages(count);
  3392. if (!start_pg)
  3393. return -ENOMEM;
  3394. mutex_lock(&ftrace_lock);
  3395. /*
  3396. * Core and each module needs their own pages, as
  3397. * modules will free them when they are removed.
  3398. * Force a new page to be allocated for modules.
  3399. */
  3400. if (!mod) {
  3401. WARN_ON(ftrace_pages || ftrace_pages_start);
  3402. /* First initialization */
  3403. ftrace_pages = ftrace_pages_start = start_pg;
  3404. } else {
  3405. if (!ftrace_pages)
  3406. goto out;
  3407. if (WARN_ON(ftrace_pages->next)) {
  3408. /* Hmm, we have free pages? */
  3409. while (ftrace_pages->next)
  3410. ftrace_pages = ftrace_pages->next;
  3411. }
  3412. ftrace_pages->next = start_pg;
  3413. }
  3414. p = start;
  3415. pg = start_pg;
  3416. while (p < end) {
  3417. addr = ftrace_call_adjust(*p++);
  3418. /*
  3419. * Some architecture linkers will pad between
  3420. * the different mcount_loc sections of different
  3421. * object files to satisfy alignments.
  3422. * Skip any NULL pointers.
  3423. */
  3424. if (!addr)
  3425. continue;
  3426. if (pg->index == pg->size) {
  3427. /* We should have allocated enough */
  3428. if (WARN_ON(!pg->next))
  3429. break;
  3430. pg = pg->next;
  3431. }
  3432. rec = &pg->records[pg->index++];
  3433. rec->ip = addr;
  3434. }
  3435. /* We should have used all pages */
  3436. WARN_ON(pg->next);
  3437. /* Assign the last page to ftrace_pages */
  3438. ftrace_pages = pg;
  3439. /* These new locations need to be initialized */
  3440. ftrace_new_pgs = start_pg;
  3441. /*
  3442. * We only need to disable interrupts on start up
  3443. * because we are modifying code that an interrupt
  3444. * may execute, and the modification is not atomic.
  3445. * But for modules, nothing runs the code we modify
  3446. * until we are finished with it, and there's no
  3447. * reason to cause large interrupt latencies while we do it.
  3448. */
  3449. if (!mod)
  3450. local_irq_save(flags);
  3451. ftrace_update_code(mod);
  3452. if (!mod)
  3453. local_irq_restore(flags);
  3454. ret = 0;
  3455. out:
  3456. mutex_unlock(&ftrace_lock);
  3457. return ret;
  3458. }
  3459. #ifdef CONFIG_MODULES
  3460. #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
  3461. void ftrace_release_mod(struct module *mod)
  3462. {
  3463. struct dyn_ftrace *rec;
  3464. struct ftrace_page **last_pg;
  3465. struct ftrace_page *pg;
  3466. int order;
  3467. mutex_lock(&ftrace_lock);
  3468. if (ftrace_disabled)
  3469. goto out_unlock;
  3470. /*
  3471. * Each module has its own ftrace_pages, remove
  3472. * them from the list.
  3473. */
  3474. last_pg = &ftrace_pages_start;
  3475. for (pg = ftrace_pages_start; pg; pg = *last_pg) {
  3476. rec = &pg->records[0];
  3477. if (within_module_core(rec->ip, mod)) {
  3478. /*
  3479. * As core pages are first, the first
  3480. * page should never be a module page.
  3481. */
  3482. if (WARN_ON(pg == ftrace_pages_start))
  3483. goto out_unlock;
  3484. /* Check if we are deleting the last page */
  3485. if (pg == ftrace_pages)
  3486. ftrace_pages = next_to_ftrace_page(last_pg);
  3487. *last_pg = pg->next;
  3488. order = get_count_order(pg->size / ENTRIES_PER_PAGE);
  3489. free_pages((unsigned long)pg->records, order);
  3490. kfree(pg);
  3491. } else
  3492. last_pg = &pg->next;
  3493. }
  3494. out_unlock:
  3495. mutex_unlock(&ftrace_lock);
  3496. }
  3497. static void ftrace_init_module(struct module *mod,
  3498. unsigned long *start, unsigned long *end)
  3499. {
  3500. if (ftrace_disabled || start == end)
  3501. return;
  3502. ftrace_process_locs(mod, start, end);
  3503. }
  3504. static int ftrace_module_notify_enter(struct notifier_block *self,
  3505. unsigned long val, void *data)
  3506. {
  3507. struct module *mod = data;
  3508. if (val == MODULE_STATE_COMING)
  3509. ftrace_init_module(mod, mod->ftrace_callsites,
  3510. mod->ftrace_callsites +
  3511. mod->num_ftrace_callsites);
  3512. return 0;
  3513. }
  3514. static int ftrace_module_notify_exit(struct notifier_block *self,
  3515. unsigned long val, void *data)
  3516. {
  3517. struct module *mod = data;
  3518. if (val == MODULE_STATE_GOING)
  3519. ftrace_release_mod(mod);
  3520. return 0;
  3521. }
  3522. #else
  3523. static int ftrace_module_notify_enter(struct notifier_block *self,
  3524. unsigned long val, void *data)
  3525. {
  3526. return 0;
  3527. }
  3528. static int ftrace_module_notify_exit(struct notifier_block *self,
  3529. unsigned long val, void *data)
  3530. {
  3531. return 0;
  3532. }
  3533. #endif /* CONFIG_MODULES */
  3534. struct notifier_block ftrace_module_enter_nb = {
  3535. .notifier_call = ftrace_module_notify_enter,
  3536. .priority = INT_MAX, /* Run before anything that can use kprobes */
  3537. };
  3538. struct notifier_block ftrace_module_exit_nb = {
  3539. .notifier_call = ftrace_module_notify_exit,
  3540. .priority = INT_MIN, /* Run after anything that can remove kprobes */
  3541. };
  3542. extern unsigned long __start_mcount_loc[];
  3543. extern unsigned long __stop_mcount_loc[];
  3544. void __init ftrace_init(void)
  3545. {
  3546. unsigned long count, addr, flags;
  3547. int ret;
  3548. /* Keep the ftrace pointer to the stub */
  3549. addr = (unsigned long)ftrace_stub;
  3550. local_irq_save(flags);
  3551. ftrace_dyn_arch_init(&addr);
  3552. local_irq_restore(flags);
  3553. /* ftrace_dyn_arch_init places the return code in addr */
  3554. if (addr)
  3555. goto failed;
  3556. count = __stop_mcount_loc - __start_mcount_loc;
  3557. ret = ftrace_dyn_table_alloc(count);
  3558. if (ret)
  3559. goto failed;
  3560. last_ftrace_enabled = ftrace_enabled = 1;
  3561. ret = ftrace_process_locs(NULL,
  3562. __start_mcount_loc,
  3563. __stop_mcount_loc);
  3564. ret = register_module_notifier(&ftrace_module_enter_nb);
  3565. if (ret)
  3566. pr_warning("Failed to register trace ftrace module enter notifier\n");
  3567. ret = register_module_notifier(&ftrace_module_exit_nb);
  3568. if (ret)
  3569. pr_warning("Failed to register trace ftrace module exit notifier\n");
  3570. set_ftrace_early_filters();
  3571. return;
  3572. failed:
  3573. ftrace_disabled = 1;
  3574. }
  3575. #else
  3576. static struct ftrace_ops global_ops = {
  3577. .func = ftrace_stub,
  3578. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
  3579. INIT_REGEX_LOCK(global_ops)
  3580. };
  3581. static int __init ftrace_nodyn_init(void)
  3582. {
  3583. ftrace_enabled = 1;
  3584. return 0;
  3585. }
  3586. core_initcall(ftrace_nodyn_init);
  3587. static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
  3588. static inline void ftrace_startup_enable(int command) { }
  3589. /* Keep as macros so we do not need to define the commands */
  3590. # define ftrace_startup(ops, command) \
  3591. ({ \
  3592. (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
  3593. 0; \
  3594. })
  3595. # define ftrace_shutdown(ops, command) do { } while (0)
  3596. # define ftrace_startup_sysctl() do { } while (0)
  3597. # define ftrace_shutdown_sysctl() do { } while (0)
  3598. static inline int
  3599. ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
  3600. {
  3601. return 1;
  3602. }
  3603. #endif /* CONFIG_DYNAMIC_FTRACE */
  3604. static void
  3605. ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
  3606. struct ftrace_ops *op, struct pt_regs *regs)
  3607. {
  3608. if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
  3609. return;
  3610. /*
  3611. * Some of the ops may be dynamically allocated,
  3612. * they must be freed after a synchronize_sched().
  3613. */
  3614. preempt_disable_notrace();
  3615. trace_recursion_set(TRACE_CONTROL_BIT);
  3616. do_for_each_ftrace_op(op, ftrace_control_list) {
  3617. if (!(op->flags & FTRACE_OPS_FL_STUB) &&
  3618. !ftrace_function_local_disabled(op) &&
  3619. ftrace_ops_test(op, ip, regs))
  3620. op->func(ip, parent_ip, op, regs);
  3621. } while_for_each_ftrace_op(op);
  3622. trace_recursion_clear(TRACE_CONTROL_BIT);
  3623. preempt_enable_notrace();
  3624. }
  3625. static struct ftrace_ops control_ops = {
  3626. .func = ftrace_ops_control_func,
  3627. .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
  3628. INIT_REGEX_LOCK(control_ops)
  3629. };
  3630. static inline void
  3631. __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  3632. struct ftrace_ops *ignored, struct pt_regs *regs)
  3633. {
  3634. struct ftrace_ops *op;
  3635. int bit;
  3636. if (function_trace_stop)
  3637. return;
  3638. bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
  3639. if (bit < 0)
  3640. return;
  3641. /*
  3642. * Some of the ops may be dynamically allocated,
  3643. * they must be freed after a synchronize_sched().
  3644. */
  3645. preempt_disable_notrace();
  3646. do_for_each_ftrace_op(op, ftrace_ops_list) {
  3647. if (ftrace_ops_test(op, ip, regs))
  3648. op->func(ip, parent_ip, op, regs);
  3649. } while_for_each_ftrace_op(op);
  3650. preempt_enable_notrace();
  3651. trace_clear_recursion(bit);
  3652. }
  3653. /*
  3654. * Some archs only support passing ip and parent_ip. Even though
  3655. * the list function ignores the op parameter, we do not want any
  3656. * C side effects, where a function is called without the caller
  3657. * sending a third parameter.
  3658. * Archs are to support both the regs and ftrace_ops at the same time.
  3659. * If they support ftrace_ops, it is assumed they support regs.
  3660. * If call backs want to use regs, they must either check for regs
  3661. * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
  3662. * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
  3663. * An architecture can pass partial regs with ftrace_ops and still
  3664. * set the ARCH_SUPPORT_FTARCE_OPS.
  3665. */
  3666. #if ARCH_SUPPORTS_FTRACE_OPS
  3667. static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
  3668. struct ftrace_ops *op, struct pt_regs *regs)
  3669. {
  3670. __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
  3671. }
  3672. #else
  3673. static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
  3674. {
  3675. __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
  3676. }
  3677. #endif
  3678. static void clear_ftrace_swapper(void)
  3679. {
  3680. struct task_struct *p;
  3681. int cpu;
  3682. get_online_cpus();
  3683. for_each_online_cpu(cpu) {
  3684. p = idle_task(cpu);
  3685. clear_tsk_trace_trace(p);
  3686. }
  3687. put_online_cpus();
  3688. }
  3689. static void set_ftrace_swapper(void)
  3690. {
  3691. struct task_struct *p;
  3692. int cpu;
  3693. get_online_cpus();
  3694. for_each_online_cpu(cpu) {
  3695. p = idle_task(cpu);
  3696. set_tsk_trace_trace(p);
  3697. }
  3698. put_online_cpus();
  3699. }
  3700. static void clear_ftrace_pid(struct pid *pid)
  3701. {
  3702. struct task_struct *p;
  3703. rcu_read_lock();
  3704. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3705. clear_tsk_trace_trace(p);
  3706. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3707. rcu_read_unlock();
  3708. put_pid(pid);
  3709. }
  3710. static void set_ftrace_pid(struct pid *pid)
  3711. {
  3712. struct task_struct *p;
  3713. rcu_read_lock();
  3714. do_each_pid_task(pid, PIDTYPE_PID, p) {
  3715. set_tsk_trace_trace(p);
  3716. } while_each_pid_task(pid, PIDTYPE_PID, p);
  3717. rcu_read_unlock();
  3718. }
  3719. static void clear_ftrace_pid_task(struct pid *pid)
  3720. {
  3721. if (pid == ftrace_swapper_pid)
  3722. clear_ftrace_swapper();
  3723. else
  3724. clear_ftrace_pid(pid);
  3725. }
  3726. static void set_ftrace_pid_task(struct pid *pid)
  3727. {
  3728. if (pid == ftrace_swapper_pid)
  3729. set_ftrace_swapper();
  3730. else
  3731. set_ftrace_pid(pid);
  3732. }
  3733. static int ftrace_pid_add(int p)
  3734. {
  3735. struct pid *pid;
  3736. struct ftrace_pid *fpid;
  3737. int ret = -EINVAL;
  3738. mutex_lock(&ftrace_lock);
  3739. if (!p)
  3740. pid = ftrace_swapper_pid;
  3741. else
  3742. pid = find_get_pid(p);
  3743. if (!pid)
  3744. goto out;
  3745. ret = 0;
  3746. list_for_each_entry(fpid, &ftrace_pids, list)
  3747. if (fpid->pid == pid)
  3748. goto out_put;
  3749. ret = -ENOMEM;
  3750. fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
  3751. if (!fpid)
  3752. goto out_put;
  3753. list_add(&fpid->list, &ftrace_pids);
  3754. fpid->pid = pid;
  3755. set_ftrace_pid_task(pid);
  3756. ftrace_update_pid_func();
  3757. ftrace_startup_enable(0);
  3758. mutex_unlock(&ftrace_lock);
  3759. return 0;
  3760. out_put:
  3761. if (pid != ftrace_swapper_pid)
  3762. put_pid(pid);
  3763. out:
  3764. mutex_unlock(&ftrace_lock);
  3765. return ret;
  3766. }
  3767. static void ftrace_pid_reset(void)
  3768. {
  3769. struct ftrace_pid *fpid, *safe;
  3770. mutex_lock(&ftrace_lock);
  3771. list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
  3772. struct pid *pid = fpid->pid;
  3773. clear_ftrace_pid_task(pid);
  3774. list_del(&fpid->list);
  3775. kfree(fpid);
  3776. }
  3777. ftrace_update_pid_func();
  3778. ftrace_startup_enable(0);
  3779. mutex_unlock(&ftrace_lock);
  3780. }
  3781. static void *fpid_start(struct seq_file *m, loff_t *pos)
  3782. {
  3783. mutex_lock(&ftrace_lock);
  3784. if (list_empty(&ftrace_pids) && (!*pos))
  3785. return (void *) 1;
  3786. return seq_list_start(&ftrace_pids, *pos);
  3787. }
  3788. static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
  3789. {
  3790. if (v == (void *)1)
  3791. return NULL;
  3792. return seq_list_next(v, &ftrace_pids, pos);
  3793. }
  3794. static void fpid_stop(struct seq_file *m, void *p)
  3795. {
  3796. mutex_unlock(&ftrace_lock);
  3797. }
  3798. static int fpid_show(struct seq_file *m, void *v)
  3799. {
  3800. const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
  3801. if (v == (void *)1) {
  3802. seq_printf(m, "no pid\n");
  3803. return 0;
  3804. }
  3805. if (fpid->pid == ftrace_swapper_pid)
  3806. seq_printf(m, "swapper tasks\n");
  3807. else
  3808. seq_printf(m, "%u\n", pid_vnr(fpid->pid));
  3809. return 0;
  3810. }
  3811. static const struct seq_operations ftrace_pid_sops = {
  3812. .start = fpid_start,
  3813. .next = fpid_next,
  3814. .stop = fpid_stop,
  3815. .show = fpid_show,
  3816. };
  3817. static int
  3818. ftrace_pid_open(struct inode *inode, struct file *file)
  3819. {
  3820. int ret = 0;
  3821. if ((file->f_mode & FMODE_WRITE) &&
  3822. (file->f_flags & O_TRUNC))
  3823. ftrace_pid_reset();
  3824. if (file->f_mode & FMODE_READ)
  3825. ret = seq_open(file, &ftrace_pid_sops);
  3826. return ret;
  3827. }
  3828. static ssize_t
  3829. ftrace_pid_write(struct file *filp, const char __user *ubuf,
  3830. size_t cnt, loff_t *ppos)
  3831. {
  3832. char buf[64], *tmp;
  3833. long val;
  3834. int ret;
  3835. if (cnt >= sizeof(buf))
  3836. return -EINVAL;
  3837. if (copy_from_user(&buf, ubuf, cnt))
  3838. return -EFAULT;
  3839. buf[cnt] = 0;
  3840. /*
  3841. * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
  3842. * to clean the filter quietly.
  3843. */
  3844. tmp = strstrip(buf);
  3845. if (strlen(tmp) == 0)
  3846. return 1;
  3847. ret = kstrtol(tmp, 10, &val);
  3848. if (ret < 0)
  3849. return ret;
  3850. ret = ftrace_pid_add(val);
  3851. return ret ? ret : cnt;
  3852. }
  3853. static int
  3854. ftrace_pid_release(struct inode *inode, struct file *file)
  3855. {
  3856. if (file->f_mode & FMODE_READ)
  3857. seq_release(inode, file);
  3858. return 0;
  3859. }
  3860. static const struct file_operations ftrace_pid_fops = {
  3861. .open = ftrace_pid_open,
  3862. .write = ftrace_pid_write,
  3863. .read = seq_read,
  3864. .llseek = ftrace_filter_lseek,
  3865. .release = ftrace_pid_release,
  3866. };
  3867. static __init int ftrace_init_debugfs(void)
  3868. {
  3869. struct dentry *d_tracer;
  3870. d_tracer = tracing_init_dentry();
  3871. if (!d_tracer)
  3872. return 0;
  3873. ftrace_init_dyn_debugfs(d_tracer);
  3874. trace_create_file("set_ftrace_pid", 0644, d_tracer,
  3875. NULL, &ftrace_pid_fops);
  3876. ftrace_profile_debugfs(d_tracer);
  3877. return 0;
  3878. }
  3879. fs_initcall(ftrace_init_debugfs);
  3880. /**
  3881. * ftrace_kill - kill ftrace
  3882. *
  3883. * This function should be used by panic code. It stops ftrace
  3884. * but in a not so nice way. If you need to simply kill ftrace
  3885. * from a non-atomic section, use ftrace_kill.
  3886. */
  3887. void ftrace_kill(void)
  3888. {
  3889. ftrace_disabled = 1;
  3890. ftrace_enabled = 0;
  3891. clear_ftrace_function();
  3892. }
  3893. /**
  3894. * Test if ftrace is dead or not.
  3895. */
  3896. int ftrace_is_dead(void)
  3897. {
  3898. return ftrace_disabled;
  3899. }
  3900. /**
  3901. * register_ftrace_function - register a function for profiling
  3902. * @ops - ops structure that holds the function for profiling.
  3903. *
  3904. * Register a function to be called by all functions in the
  3905. * kernel.
  3906. *
  3907. * Note: @ops->func and all the functions it calls must be labeled
  3908. * with "notrace", otherwise it will go into a
  3909. * recursive loop.
  3910. */
  3911. int register_ftrace_function(struct ftrace_ops *ops)
  3912. {
  3913. int ret = -1;
  3914. ftrace_ops_init(ops);
  3915. mutex_lock(&ftrace_lock);
  3916. ret = __register_ftrace_function(ops);
  3917. if (!ret)
  3918. ret = ftrace_startup(ops, 0);
  3919. mutex_unlock(&ftrace_lock);
  3920. return ret;
  3921. }
  3922. EXPORT_SYMBOL_GPL(register_ftrace_function);
  3923. /**
  3924. * unregister_ftrace_function - unregister a function for profiling.
  3925. * @ops - ops structure that holds the function to unregister
  3926. *
  3927. * Unregister a function that was added to be called by ftrace profiling.
  3928. */
  3929. int unregister_ftrace_function(struct ftrace_ops *ops)
  3930. {
  3931. int ret;
  3932. mutex_lock(&ftrace_lock);
  3933. ret = __unregister_ftrace_function(ops);
  3934. if (!ret)
  3935. ftrace_shutdown(ops, 0);
  3936. mutex_unlock(&ftrace_lock);
  3937. return ret;
  3938. }
  3939. EXPORT_SYMBOL_GPL(unregister_ftrace_function);
  3940. int
  3941. ftrace_enable_sysctl(struct ctl_table *table, int write,
  3942. void __user *buffer, size_t *lenp,
  3943. loff_t *ppos)
  3944. {
  3945. int ret = -ENODEV;
  3946. mutex_lock(&ftrace_lock);
  3947. if (unlikely(ftrace_disabled))
  3948. goto out;
  3949. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  3950. if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
  3951. goto out;
  3952. last_ftrace_enabled = !!ftrace_enabled;
  3953. if (ftrace_enabled) {
  3954. ftrace_startup_sysctl();
  3955. /* we are starting ftrace again */
  3956. if (ftrace_ops_list != &ftrace_list_end)
  3957. update_ftrace_function();
  3958. } else {
  3959. /* stopping ftrace calls (just send to ftrace_stub) */
  3960. ftrace_trace_function = ftrace_stub;
  3961. ftrace_shutdown_sysctl();
  3962. }
  3963. out:
  3964. mutex_unlock(&ftrace_lock);
  3965. return ret;
  3966. }
  3967. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  3968. static int ftrace_graph_active;
  3969. static struct notifier_block ftrace_suspend_notifier;
  3970. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  3971. {
  3972. return 0;
  3973. }
  3974. /* The callbacks that hook a function */
  3975. trace_func_graph_ret_t ftrace_graph_return =
  3976. (trace_func_graph_ret_t)ftrace_stub;
  3977. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  3978. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  3979. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  3980. {
  3981. int i;
  3982. int ret = 0;
  3983. unsigned long flags;
  3984. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  3985. struct task_struct *g, *t;
  3986. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  3987. ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
  3988. * sizeof(struct ftrace_ret_stack),
  3989. GFP_KERNEL);
  3990. if (!ret_stack_list[i]) {
  3991. start = 0;
  3992. end = i;
  3993. ret = -ENOMEM;
  3994. goto free;
  3995. }
  3996. }
  3997. read_lock_irqsave(&tasklist_lock, flags);
  3998. do_each_thread(g, t) {
  3999. if (start == end) {
  4000. ret = -EAGAIN;
  4001. goto unlock;
  4002. }
  4003. if (t->ret_stack == NULL) {
  4004. atomic_set(&t->tracing_graph_pause, 0);
  4005. atomic_set(&t->trace_overrun, 0);
  4006. t->curr_ret_stack = -1;
  4007. /* Make sure the tasks see the -1 first: */
  4008. smp_wmb();
  4009. t->ret_stack = ret_stack_list[start++];
  4010. }
  4011. } while_each_thread(g, t);
  4012. unlock:
  4013. read_unlock_irqrestore(&tasklist_lock, flags);
  4014. free:
  4015. for (i = start; i < end; i++)
  4016. kfree(ret_stack_list[i]);
  4017. return ret;
  4018. }
  4019. static void
  4020. ftrace_graph_probe_sched_switch(void *ignore,
  4021. struct task_struct *prev, struct task_struct *next)
  4022. {
  4023. unsigned long long timestamp;
  4024. int index;
  4025. /*
  4026. * Does the user want to count the time a function was asleep.
  4027. * If so, do not update the time stamps.
  4028. */
  4029. if (trace_flags & TRACE_ITER_SLEEP_TIME)
  4030. return;
  4031. timestamp = trace_clock_local();
  4032. prev->ftrace_timestamp = timestamp;
  4033. /* only process tasks that we timestamped */
  4034. if (!next->ftrace_timestamp)
  4035. return;
  4036. /*
  4037. * Update all the counters in next to make up for the
  4038. * time next was sleeping.
  4039. */
  4040. timestamp -= next->ftrace_timestamp;
  4041. for (index = next->curr_ret_stack; index >= 0; index--)
  4042. next->ret_stack[index].calltime += timestamp;
  4043. }
  4044. /* Allocate a return stack for each task */
  4045. static int start_graph_tracing(void)
  4046. {
  4047. struct ftrace_ret_stack **ret_stack_list;
  4048. int ret, cpu;
  4049. ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
  4050. sizeof(struct ftrace_ret_stack *),
  4051. GFP_KERNEL);
  4052. if (!ret_stack_list)
  4053. return -ENOMEM;
  4054. /* The cpu_boot init_task->ret_stack will never be freed */
  4055. for_each_online_cpu(cpu) {
  4056. if (!idle_task(cpu)->ret_stack)
  4057. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  4058. }
  4059. do {
  4060. ret = alloc_retstack_tasklist(ret_stack_list);
  4061. } while (ret == -EAGAIN);
  4062. if (!ret) {
  4063. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  4064. if (ret)
  4065. pr_info("ftrace_graph: Couldn't activate tracepoint"
  4066. " probe to kernel_sched_switch\n");
  4067. }
  4068. kfree(ret_stack_list);
  4069. return ret;
  4070. }
  4071. /*
  4072. * Hibernation protection.
  4073. * The state of the current task is too much unstable during
  4074. * suspend/restore to disk. We want to protect against that.
  4075. */
  4076. static int
  4077. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  4078. void *unused)
  4079. {
  4080. switch (state) {
  4081. case PM_HIBERNATION_PREPARE:
  4082. pause_graph_tracing();
  4083. break;
  4084. case PM_POST_HIBERNATION:
  4085. unpause_graph_tracing();
  4086. break;
  4087. }
  4088. return NOTIFY_DONE;
  4089. }
  4090. int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  4091. trace_func_graph_ent_t entryfunc)
  4092. {
  4093. int ret = 0;
  4094. mutex_lock(&ftrace_lock);
  4095. /* we currently allow only one tracer registered at a time */
  4096. if (ftrace_graph_active) {
  4097. ret = -EBUSY;
  4098. goto out;
  4099. }
  4100. ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
  4101. register_pm_notifier(&ftrace_suspend_notifier);
  4102. ftrace_graph_active++;
  4103. ret = start_graph_tracing();
  4104. if (ret) {
  4105. ftrace_graph_active--;
  4106. goto out;
  4107. }
  4108. ftrace_graph_return = retfunc;
  4109. ftrace_graph_entry = entryfunc;
  4110. ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
  4111. out:
  4112. mutex_unlock(&ftrace_lock);
  4113. return ret;
  4114. }
  4115. void unregister_ftrace_graph(void)
  4116. {
  4117. mutex_lock(&ftrace_lock);
  4118. if (unlikely(!ftrace_graph_active))
  4119. goto out;
  4120. ftrace_graph_active--;
  4121. ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
  4122. ftrace_graph_entry = ftrace_graph_entry_stub;
  4123. ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
  4124. unregister_pm_notifier(&ftrace_suspend_notifier);
  4125. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  4126. out:
  4127. mutex_unlock(&ftrace_lock);
  4128. }
  4129. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  4130. static void
  4131. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  4132. {
  4133. atomic_set(&t->tracing_graph_pause, 0);
  4134. atomic_set(&t->trace_overrun, 0);
  4135. t->ftrace_timestamp = 0;
  4136. /* make curr_ret_stack visible before we add the ret_stack */
  4137. smp_wmb();
  4138. t->ret_stack = ret_stack;
  4139. }
  4140. /*
  4141. * Allocate a return stack for the idle task. May be the first
  4142. * time through, or it may be done by CPU hotplug online.
  4143. */
  4144. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  4145. {
  4146. t->curr_ret_stack = -1;
  4147. /*
  4148. * The idle task has no parent, it either has its own
  4149. * stack or no stack at all.
  4150. */
  4151. if (t->ret_stack)
  4152. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  4153. if (ftrace_graph_active) {
  4154. struct ftrace_ret_stack *ret_stack;
  4155. ret_stack = per_cpu(idle_ret_stack, cpu);
  4156. if (!ret_stack) {
  4157. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  4158. * sizeof(struct ftrace_ret_stack),
  4159. GFP_KERNEL);
  4160. if (!ret_stack)
  4161. return;
  4162. per_cpu(idle_ret_stack, cpu) = ret_stack;
  4163. }
  4164. graph_init_task(t, ret_stack);
  4165. }
  4166. }
  4167. /* Allocate a return stack for newly created task */
  4168. void ftrace_graph_init_task(struct task_struct *t)
  4169. {
  4170. /* Make sure we do not use the parent ret_stack */
  4171. t->ret_stack = NULL;
  4172. t->curr_ret_stack = -1;
  4173. if (ftrace_graph_active) {
  4174. struct ftrace_ret_stack *ret_stack;
  4175. ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
  4176. * sizeof(struct ftrace_ret_stack),
  4177. GFP_KERNEL);
  4178. if (!ret_stack)
  4179. return;
  4180. graph_init_task(t, ret_stack);
  4181. }
  4182. }
  4183. void ftrace_graph_exit_task(struct task_struct *t)
  4184. {
  4185. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  4186. t->ret_stack = NULL;
  4187. /* NULL must become visible to IRQs before we free it: */
  4188. barrier();
  4189. kfree(ret_stack);
  4190. }
  4191. void ftrace_graph_stop(void)
  4192. {
  4193. ftrace_stop();
  4194. }
  4195. #endif