ftrace.c 123 KB

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