ftrace.c 165 KB

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