ftrace.c 137 KB

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