ftrace.c 166 KB

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