ftrace.c 141 KB

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