ftrace.c 135 KB

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