ftrace.c 166 KB

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