ftrace.c 138 KB

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