ftrace.c 140 KB

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