ftrace.c 160 KB

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