ftrace.c 145 KB

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