ftrace.c 139 KB

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