ftrace.c 137 KB

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