ftrace.c 159 KB

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