ftrace.c 137 KB

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