trace.c 263 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ring buffer based function tracer
  4. *
  5. * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
  6. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  7. *
  8. * Originally taken from the RT patch by:
  9. * Arnaldo Carvalho de Melo <acme@redhat.com>
  10. *
  11. * Based on code from the latency_tracer, that is:
  12. * Copyright (C) 2004-2006 Ingo Molnar
  13. * Copyright (C) 2004 Nadia Yvette Chambers
  14. */
  15. #include <linux/ring_buffer.h>
  16. #include <linux/utsname.h>
  17. #include <linux/stacktrace.h>
  18. #include <linux/writeback.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/security.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/irqflags.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/tracefs.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/hardirq.h>
  27. #include <linux/linkage.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/cleanup.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/ftrace.h>
  32. #include <linux/module.h>
  33. #include <linux/percpu.h>
  34. #include <linux/splice.h>
  35. #include <linux/kdebug.h>
  36. #include <linux/string.h>
  37. #include <linux/mount.h>
  38. #include <linux/rwsem.h>
  39. #include <linux/slab.h>
  40. #include <linux/ctype.h>
  41. #include <linux/init.h>
  42. #include <linux/panic_notifier.h>
  43. #include <linux/poll.h>
  44. #include <linux/nmi.h>
  45. #include <linux/fs.h>
  46. #include <linux/trace.h>
  47. #include <linux/sched/clock.h>
  48. #include <linux/sched/rt.h>
  49. #include <linux/fsnotify.h>
  50. #include <linux/irq_work.h>
  51. #include <linux/workqueue.h>
  52. #include <asm/setup.h> /* COMMAND_LINE_SIZE */
  53. #include "trace.h"
  54. #include "trace_output.h"
  55. #ifdef CONFIG_FTRACE_STARTUP_TEST
  56. /*
  57. * We need to change this state when a selftest is running.
  58. * A selftest will lurk into the ring-buffer to count the
  59. * entries inserted during the selftest although some concurrent
  60. * insertions into the ring-buffer such as trace_printk could occurred
  61. * at the same time, giving false positive or negative results.
  62. */
  63. static bool __read_mostly tracing_selftest_running;
  64. /*
  65. * If boot-time tracing including tracers/events via kernel cmdline
  66. * is running, we do not want to run SELFTEST.
  67. */
  68. bool __read_mostly tracing_selftest_disabled;
  69. void __init disable_tracing_selftest(const char *reason)
  70. {
  71. if (!tracing_selftest_disabled) {
  72. tracing_selftest_disabled = true;
  73. pr_info("Ftrace startup test is disabled due to %s\n", reason);
  74. }
  75. }
  76. #else
  77. #define tracing_selftest_running 0
  78. #define tracing_selftest_disabled 0
  79. #endif
  80. /* Pipe tracepoints to printk */
  81. static struct trace_iterator *tracepoint_print_iter;
  82. int tracepoint_printk;
  83. static bool tracepoint_printk_stop_on_boot __initdata;
  84. static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
  85. /* For tracers that don't implement custom flags */
  86. static struct tracer_opt dummy_tracer_opt[] = {
  87. { }
  88. };
  89. static int
  90. dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
  91. {
  92. return 0;
  93. }
  94. /*
  95. * To prevent the comm cache from being overwritten when no
  96. * tracing is active, only save the comm when a trace event
  97. * occurred.
  98. */
  99. DEFINE_PER_CPU(bool, trace_taskinfo_save);
  100. /*
  101. * Kill all tracing for good (never come back).
  102. * It is initialized to 1 but will turn to zero if the initialization
  103. * of the tracer is successful. But that is the only place that sets
  104. * this back to zero.
  105. */
  106. static int tracing_disabled = 1;
  107. cpumask_var_t __read_mostly tracing_buffer_mask;
  108. /*
  109. * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
  110. *
  111. * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
  112. * is set, then ftrace_dump is called. This will output the contents
  113. * of the ftrace buffers to the console. This is very useful for
  114. * capturing traces that lead to crashes and outputing it to a
  115. * serial console.
  116. *
  117. * It is default off, but you can enable it with either specifying
  118. * "ftrace_dump_on_oops" in the kernel command line, or setting
  119. * /proc/sys/kernel/ftrace_dump_on_oops
  120. * Set 1 if you want to dump buffers of all CPUs
  121. * Set 2 if you want to dump the buffer of the CPU that triggered oops
  122. * Set instance name if you want to dump the specific trace instance
  123. * Multiple instance dump is also supported, and instances are seperated
  124. * by commas.
  125. */
  126. /* Set to string format zero to disable by default */
  127. char ftrace_dump_on_oops[MAX_TRACER_SIZE] = "0";
  128. /* When set, tracing will stop when a WARN*() is hit */
  129. int __disable_trace_on_warning;
  130. #ifdef CONFIG_TRACE_EVAL_MAP_FILE
  131. /* Map of enums to their values, for "eval_map" file */
  132. struct trace_eval_map_head {
  133. struct module *mod;
  134. unsigned long length;
  135. };
  136. union trace_eval_map_item;
  137. struct trace_eval_map_tail {
  138. /*
  139. * "end" is first and points to NULL as it must be different
  140. * than "mod" or "eval_string"
  141. */
  142. union trace_eval_map_item *next;
  143. const char *end; /* points to NULL */
  144. };
  145. static DEFINE_MUTEX(trace_eval_mutex);
  146. /*
  147. * The trace_eval_maps are saved in an array with two extra elements,
  148. * one at the beginning, and one at the end. The beginning item contains
  149. * the count of the saved maps (head.length), and the module they
  150. * belong to if not built in (head.mod). The ending item contains a
  151. * pointer to the next array of saved eval_map items.
  152. */
  153. union trace_eval_map_item {
  154. struct trace_eval_map map;
  155. struct trace_eval_map_head head;
  156. struct trace_eval_map_tail tail;
  157. };
  158. static union trace_eval_map_item *trace_eval_maps;
  159. #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
  160. int tracing_set_tracer(struct trace_array *tr, const char *buf);
  161. static void ftrace_trace_userstack(struct trace_array *tr,
  162. struct trace_buffer *buffer,
  163. unsigned int trace_ctx);
  164. static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
  165. static char *default_bootup_tracer;
  166. static bool allocate_snapshot;
  167. static bool snapshot_at_boot;
  168. static char boot_instance_info[COMMAND_LINE_SIZE] __initdata;
  169. static int boot_instance_index;
  170. static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata;
  171. static int boot_snapshot_index;
  172. static int __init set_cmdline_ftrace(char *str)
  173. {
  174. strscpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
  175. default_bootup_tracer = bootup_tracer_buf;
  176. /* We are using ftrace early, expand it */
  177. trace_set_ring_buffer_expanded(NULL);
  178. return 1;
  179. }
  180. __setup("ftrace=", set_cmdline_ftrace);
  181. int ftrace_dump_on_oops_enabled(void)
  182. {
  183. if (!strcmp("0", ftrace_dump_on_oops))
  184. return 0;
  185. else
  186. return 1;
  187. }
  188. static int __init set_ftrace_dump_on_oops(char *str)
  189. {
  190. if (!*str) {
  191. strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);
  192. return 1;
  193. }
  194. if (*str == ',') {
  195. strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);
  196. strscpy(ftrace_dump_on_oops + 1, str, MAX_TRACER_SIZE - 1);
  197. return 1;
  198. }
  199. if (*str++ == '=') {
  200. strscpy(ftrace_dump_on_oops, str, MAX_TRACER_SIZE);
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
  206. static int __init stop_trace_on_warning(char *str)
  207. {
  208. if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
  209. __disable_trace_on_warning = 1;
  210. return 1;
  211. }
  212. __setup("traceoff_on_warning", stop_trace_on_warning);
  213. static int __init boot_alloc_snapshot(char *str)
  214. {
  215. char *slot = boot_snapshot_info + boot_snapshot_index;
  216. int left = sizeof(boot_snapshot_info) - boot_snapshot_index;
  217. int ret;
  218. if (str[0] == '=') {
  219. str++;
  220. if (strlen(str) >= left)
  221. return -1;
  222. ret = snprintf(slot, left, "%s\t", str);
  223. boot_snapshot_index += ret;
  224. } else {
  225. allocate_snapshot = true;
  226. /* We also need the main ring buffer expanded */
  227. trace_set_ring_buffer_expanded(NULL);
  228. }
  229. return 1;
  230. }
  231. __setup("alloc_snapshot", boot_alloc_snapshot);
  232. static int __init boot_snapshot(char *str)
  233. {
  234. snapshot_at_boot = true;
  235. boot_alloc_snapshot(str);
  236. return 1;
  237. }
  238. __setup("ftrace_boot_snapshot", boot_snapshot);
  239. static int __init boot_instance(char *str)
  240. {
  241. char *slot = boot_instance_info + boot_instance_index;
  242. int left = sizeof(boot_instance_info) - boot_instance_index;
  243. int ret;
  244. if (strlen(str) >= left)
  245. return -1;
  246. ret = snprintf(slot, left, "%s\t", str);
  247. boot_instance_index += ret;
  248. return 1;
  249. }
  250. __setup("trace_instance=", boot_instance);
  251. static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
  252. static int __init set_trace_boot_options(char *str)
  253. {
  254. strscpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
  255. return 1;
  256. }
  257. __setup("trace_options=", set_trace_boot_options);
  258. static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
  259. static char *trace_boot_clock __initdata;
  260. static int __init set_trace_boot_clock(char *str)
  261. {
  262. strscpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
  263. trace_boot_clock = trace_boot_clock_buf;
  264. return 1;
  265. }
  266. __setup("trace_clock=", set_trace_boot_clock);
  267. static int __init set_tracepoint_printk(char *str)
  268. {
  269. /* Ignore the "tp_printk_stop_on_boot" param */
  270. if (*str == '_')
  271. return 0;
  272. if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
  273. tracepoint_printk = 1;
  274. return 1;
  275. }
  276. __setup("tp_printk", set_tracepoint_printk);
  277. static int __init set_tracepoint_printk_stop(char *str)
  278. {
  279. tracepoint_printk_stop_on_boot = true;
  280. return 1;
  281. }
  282. __setup("tp_printk_stop_on_boot", set_tracepoint_printk_stop);
  283. unsigned long long ns2usecs(u64 nsec)
  284. {
  285. nsec += 500;
  286. do_div(nsec, 1000);
  287. return nsec;
  288. }
  289. static void
  290. trace_process_export(struct trace_export *export,
  291. struct ring_buffer_event *event, int flag)
  292. {
  293. struct trace_entry *entry;
  294. unsigned int size = 0;
  295. if (export->flags & flag) {
  296. entry = ring_buffer_event_data(event);
  297. size = ring_buffer_event_length(event);
  298. export->write(export, entry, size);
  299. }
  300. }
  301. static DEFINE_MUTEX(ftrace_export_lock);
  302. static struct trace_export __rcu *ftrace_exports_list __read_mostly;
  303. static DEFINE_STATIC_KEY_FALSE(trace_function_exports_enabled);
  304. static DEFINE_STATIC_KEY_FALSE(trace_event_exports_enabled);
  305. static DEFINE_STATIC_KEY_FALSE(trace_marker_exports_enabled);
  306. static inline void ftrace_exports_enable(struct trace_export *export)
  307. {
  308. if (export->flags & TRACE_EXPORT_FUNCTION)
  309. static_branch_inc(&trace_function_exports_enabled);
  310. if (export->flags & TRACE_EXPORT_EVENT)
  311. static_branch_inc(&trace_event_exports_enabled);
  312. if (export->flags & TRACE_EXPORT_MARKER)
  313. static_branch_inc(&trace_marker_exports_enabled);
  314. }
  315. static inline void ftrace_exports_disable(struct trace_export *export)
  316. {
  317. if (export->flags & TRACE_EXPORT_FUNCTION)
  318. static_branch_dec(&trace_function_exports_enabled);
  319. if (export->flags & TRACE_EXPORT_EVENT)
  320. static_branch_dec(&trace_event_exports_enabled);
  321. if (export->flags & TRACE_EXPORT_MARKER)
  322. static_branch_dec(&trace_marker_exports_enabled);
  323. }
  324. static void ftrace_exports(struct ring_buffer_event *event, int flag)
  325. {
  326. struct trace_export *export;
  327. preempt_disable_notrace();
  328. export = rcu_dereference_raw_check(ftrace_exports_list);
  329. while (export) {
  330. trace_process_export(export, event, flag);
  331. export = rcu_dereference_raw_check(export->next);
  332. }
  333. preempt_enable_notrace();
  334. }
  335. static inline void
  336. add_trace_export(struct trace_export **list, struct trace_export *export)
  337. {
  338. rcu_assign_pointer(export->next, *list);
  339. /*
  340. * We are entering export into the list but another
  341. * CPU might be walking that list. We need to make sure
  342. * the export->next pointer is valid before another CPU sees
  343. * the export pointer included into the list.
  344. */
  345. rcu_assign_pointer(*list, export);
  346. }
  347. static inline int
  348. rm_trace_export(struct trace_export **list, struct trace_export *export)
  349. {
  350. struct trace_export **p;
  351. for (p = list; *p != NULL; p = &(*p)->next)
  352. if (*p == export)
  353. break;
  354. if (*p != export)
  355. return -1;
  356. rcu_assign_pointer(*p, (*p)->next);
  357. return 0;
  358. }
  359. static inline void
  360. add_ftrace_export(struct trace_export **list, struct trace_export *export)
  361. {
  362. ftrace_exports_enable(export);
  363. add_trace_export(list, export);
  364. }
  365. static inline int
  366. rm_ftrace_export(struct trace_export **list, struct trace_export *export)
  367. {
  368. int ret;
  369. ret = rm_trace_export(list, export);
  370. ftrace_exports_disable(export);
  371. return ret;
  372. }
  373. int register_ftrace_export(struct trace_export *export)
  374. {
  375. if (WARN_ON_ONCE(!export->write))
  376. return -1;
  377. mutex_lock(&ftrace_export_lock);
  378. add_ftrace_export(&ftrace_exports_list, export);
  379. mutex_unlock(&ftrace_export_lock);
  380. return 0;
  381. }
  382. EXPORT_SYMBOL_GPL(register_ftrace_export);
  383. int unregister_ftrace_export(struct trace_export *export)
  384. {
  385. int ret;
  386. mutex_lock(&ftrace_export_lock);
  387. ret = rm_ftrace_export(&ftrace_exports_list, export);
  388. mutex_unlock(&ftrace_export_lock);
  389. return ret;
  390. }
  391. EXPORT_SYMBOL_GPL(unregister_ftrace_export);
  392. /* trace_flags holds trace_options default values */
  393. #define TRACE_DEFAULT_FLAGS \
  394. (FUNCTION_DEFAULT_FLAGS | \
  395. TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
  396. TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
  397. TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
  398. TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | \
  399. TRACE_ITER_HASH_PTR | TRACE_ITER_TRACE_PRINTK)
  400. /* trace_options that are only supported by global_trace */
  401. #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
  402. TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
  403. /* trace_flags that are default zero for instances */
  404. #define ZEROED_TRACE_FLAGS \
  405. (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK | TRACE_ITER_TRACE_PRINTK)
  406. /*
  407. * The global_trace is the descriptor that holds the top-level tracing
  408. * buffers for the live tracing.
  409. */
  410. static struct trace_array global_trace = {
  411. .trace_flags = TRACE_DEFAULT_FLAGS,
  412. };
  413. static struct trace_array *printk_trace = &global_trace;
  414. static __always_inline bool printk_binsafe(struct trace_array *tr)
  415. {
  416. /*
  417. * The binary format of traceprintk can cause a crash if used
  418. * by a buffer from another boot. Force the use of the
  419. * non binary version of trace_printk if the trace_printk
  420. * buffer is a boot mapped ring buffer.
  421. */
  422. return !(tr->flags & TRACE_ARRAY_FL_BOOT);
  423. }
  424. static void update_printk_trace(struct trace_array *tr)
  425. {
  426. if (printk_trace == tr)
  427. return;
  428. printk_trace->trace_flags &= ~TRACE_ITER_TRACE_PRINTK;
  429. printk_trace = tr;
  430. tr->trace_flags |= TRACE_ITER_TRACE_PRINTK;
  431. }
  432. void trace_set_ring_buffer_expanded(struct trace_array *tr)
  433. {
  434. if (!tr)
  435. tr = &global_trace;
  436. tr->ring_buffer_expanded = true;
  437. }
  438. LIST_HEAD(ftrace_trace_arrays);
  439. int trace_array_get(struct trace_array *this_tr)
  440. {
  441. struct trace_array *tr;
  442. guard(mutex)(&trace_types_lock);
  443. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  444. if (tr == this_tr) {
  445. tr->ref++;
  446. return 0;
  447. }
  448. }
  449. return -ENODEV;
  450. }
  451. static void __trace_array_put(struct trace_array *this_tr)
  452. {
  453. WARN_ON(!this_tr->ref);
  454. this_tr->ref--;
  455. }
  456. /**
  457. * trace_array_put - Decrement the reference counter for this trace array.
  458. * @this_tr : pointer to the trace array
  459. *
  460. * NOTE: Use this when we no longer need the trace array returned by
  461. * trace_array_get_by_name(). This ensures the trace array can be later
  462. * destroyed.
  463. *
  464. */
  465. void trace_array_put(struct trace_array *this_tr)
  466. {
  467. if (!this_tr)
  468. return;
  469. mutex_lock(&trace_types_lock);
  470. __trace_array_put(this_tr);
  471. mutex_unlock(&trace_types_lock);
  472. }
  473. EXPORT_SYMBOL_GPL(trace_array_put);
  474. int tracing_check_open_get_tr(struct trace_array *tr)
  475. {
  476. int ret;
  477. ret = security_locked_down(LOCKDOWN_TRACEFS);
  478. if (ret)
  479. return ret;
  480. if (tracing_disabled)
  481. return -ENODEV;
  482. if (tr && trace_array_get(tr) < 0)
  483. return -ENODEV;
  484. return 0;
  485. }
  486. int call_filter_check_discard(struct trace_event_call *call, void *rec,
  487. struct trace_buffer *buffer,
  488. struct ring_buffer_event *event)
  489. {
  490. if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
  491. !filter_match_preds(call->filter, rec)) {
  492. __trace_event_discard_commit(buffer, event);
  493. return 1;
  494. }
  495. return 0;
  496. }
  497. /**
  498. * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
  499. * @filtered_pids: The list of pids to check
  500. * @search_pid: The PID to find in @filtered_pids
  501. *
  502. * Returns true if @search_pid is found in @filtered_pids, and false otherwise.
  503. */
  504. bool
  505. trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
  506. {
  507. return trace_pid_list_is_set(filtered_pids, search_pid);
  508. }
  509. /**
  510. * trace_ignore_this_task - should a task be ignored for tracing
  511. * @filtered_pids: The list of pids to check
  512. * @filtered_no_pids: The list of pids not to be traced
  513. * @task: The task that should be ignored if not filtered
  514. *
  515. * Checks if @task should be traced or not from @filtered_pids.
  516. * Returns true if @task should *NOT* be traced.
  517. * Returns false if @task should be traced.
  518. */
  519. bool
  520. trace_ignore_this_task(struct trace_pid_list *filtered_pids,
  521. struct trace_pid_list *filtered_no_pids,
  522. struct task_struct *task)
  523. {
  524. /*
  525. * If filtered_no_pids is not empty, and the task's pid is listed
  526. * in filtered_no_pids, then return true.
  527. * Otherwise, if filtered_pids is empty, that means we can
  528. * trace all tasks. If it has content, then only trace pids
  529. * within filtered_pids.
  530. */
  531. return (filtered_pids &&
  532. !trace_find_filtered_pid(filtered_pids, task->pid)) ||
  533. (filtered_no_pids &&
  534. trace_find_filtered_pid(filtered_no_pids, task->pid));
  535. }
  536. /**
  537. * trace_filter_add_remove_task - Add or remove a task from a pid_list
  538. * @pid_list: The list to modify
  539. * @self: The current task for fork or NULL for exit
  540. * @task: The task to add or remove
  541. *
  542. * If adding a task, if @self is defined, the task is only added if @self
  543. * is also included in @pid_list. This happens on fork and tasks should
  544. * only be added when the parent is listed. If @self is NULL, then the
  545. * @task pid will be removed from the list, which would happen on exit
  546. * of a task.
  547. */
  548. void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
  549. struct task_struct *self,
  550. struct task_struct *task)
  551. {
  552. if (!pid_list)
  553. return;
  554. /* For forks, we only add if the forking task is listed */
  555. if (self) {
  556. if (!trace_find_filtered_pid(pid_list, self->pid))
  557. return;
  558. }
  559. /* "self" is set for forks, and NULL for exits */
  560. if (self)
  561. trace_pid_list_set(pid_list, task->pid);
  562. else
  563. trace_pid_list_clear(pid_list, task->pid);
  564. }
  565. /**
  566. * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
  567. * @pid_list: The pid list to show
  568. * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
  569. * @pos: The position of the file
  570. *
  571. * This is used by the seq_file "next" operation to iterate the pids
  572. * listed in a trace_pid_list structure.
  573. *
  574. * Returns the pid+1 as we want to display pid of zero, but NULL would
  575. * stop the iteration.
  576. */
  577. void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
  578. {
  579. long pid = (unsigned long)v;
  580. unsigned int next;
  581. (*pos)++;
  582. /* pid already is +1 of the actual previous bit */
  583. if (trace_pid_list_next(pid_list, pid, &next) < 0)
  584. return NULL;
  585. pid = next;
  586. /* Return pid + 1 to allow zero to be represented */
  587. return (void *)(pid + 1);
  588. }
  589. /**
  590. * trace_pid_start - Used for seq_file to start reading pid lists
  591. * @pid_list: The pid list to show
  592. * @pos: The position of the file
  593. *
  594. * This is used by seq_file "start" operation to start the iteration
  595. * of listing pids.
  596. *
  597. * Returns the pid+1 as we want to display pid of zero, but NULL would
  598. * stop the iteration.
  599. */
  600. void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
  601. {
  602. unsigned long pid;
  603. unsigned int first;
  604. loff_t l = 0;
  605. if (trace_pid_list_first(pid_list, &first) < 0)
  606. return NULL;
  607. pid = first;
  608. /* Return pid + 1 so that zero can be the exit value */
  609. for (pid++; pid && l < *pos;
  610. pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
  611. ;
  612. return (void *)pid;
  613. }
  614. /**
  615. * trace_pid_show - show the current pid in seq_file processing
  616. * @m: The seq_file structure to write into
  617. * @v: A void pointer of the pid (+1) value to display
  618. *
  619. * Can be directly used by seq_file operations to display the current
  620. * pid value.
  621. */
  622. int trace_pid_show(struct seq_file *m, void *v)
  623. {
  624. unsigned long pid = (unsigned long)v - 1;
  625. seq_printf(m, "%lu\n", pid);
  626. return 0;
  627. }
  628. /* 128 should be much more than enough */
  629. #define PID_BUF_SIZE 127
  630. int trace_pid_write(struct trace_pid_list *filtered_pids,
  631. struct trace_pid_list **new_pid_list,
  632. const char __user *ubuf, size_t cnt)
  633. {
  634. struct trace_pid_list *pid_list;
  635. struct trace_parser parser;
  636. unsigned long val;
  637. int nr_pids = 0;
  638. ssize_t read = 0;
  639. ssize_t ret;
  640. loff_t pos;
  641. pid_t pid;
  642. if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
  643. return -ENOMEM;
  644. /*
  645. * Always recreate a new array. The write is an all or nothing
  646. * operation. Always create a new array when adding new pids by
  647. * the user. If the operation fails, then the current list is
  648. * not modified.
  649. */
  650. pid_list = trace_pid_list_alloc();
  651. if (!pid_list) {
  652. trace_parser_put(&parser);
  653. return -ENOMEM;
  654. }
  655. if (filtered_pids) {
  656. /* copy the current bits to the new max */
  657. ret = trace_pid_list_first(filtered_pids, &pid);
  658. while (!ret) {
  659. ret = trace_pid_list_set(pid_list, pid);
  660. if (ret < 0)
  661. goto out;
  662. ret = trace_pid_list_next(filtered_pids, pid + 1, &pid);
  663. nr_pids++;
  664. }
  665. }
  666. ret = 0;
  667. while (cnt > 0) {
  668. pos = 0;
  669. ret = trace_get_user(&parser, ubuf, cnt, &pos);
  670. if (ret < 0)
  671. break;
  672. read += ret;
  673. ubuf += ret;
  674. cnt -= ret;
  675. if (!trace_parser_loaded(&parser))
  676. break;
  677. ret = -EINVAL;
  678. if (kstrtoul(parser.buffer, 0, &val))
  679. break;
  680. pid = (pid_t)val;
  681. if (trace_pid_list_set(pid_list, pid) < 0) {
  682. ret = -1;
  683. break;
  684. }
  685. nr_pids++;
  686. trace_parser_clear(&parser);
  687. ret = 0;
  688. }
  689. out:
  690. trace_parser_put(&parser);
  691. if (ret < 0) {
  692. trace_pid_list_free(pid_list);
  693. return ret;
  694. }
  695. if (!nr_pids) {
  696. /* Cleared the list of pids */
  697. trace_pid_list_free(pid_list);
  698. pid_list = NULL;
  699. }
  700. *new_pid_list = pid_list;
  701. return read;
  702. }
  703. static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu)
  704. {
  705. u64 ts;
  706. /* Early boot up does not have a buffer yet */
  707. if (!buf->buffer)
  708. return trace_clock_local();
  709. ts = ring_buffer_time_stamp(buf->buffer);
  710. ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
  711. return ts;
  712. }
  713. u64 ftrace_now(int cpu)
  714. {
  715. return buffer_ftrace_now(&global_trace.array_buffer, cpu);
  716. }
  717. /**
  718. * tracing_is_enabled - Show if global_trace has been enabled
  719. *
  720. * Shows if the global trace has been enabled or not. It uses the
  721. * mirror flag "buffer_disabled" to be used in fast paths such as for
  722. * the irqsoff tracer. But it may be inaccurate due to races. If you
  723. * need to know the accurate state, use tracing_is_on() which is a little
  724. * slower, but accurate.
  725. */
  726. int tracing_is_enabled(void)
  727. {
  728. /*
  729. * For quick access (irqsoff uses this in fast path), just
  730. * return the mirror variable of the state of the ring buffer.
  731. * It's a little racy, but we don't really care.
  732. */
  733. smp_rmb();
  734. return !global_trace.buffer_disabled;
  735. }
  736. /*
  737. * trace_buf_size is the size in bytes that is allocated
  738. * for a buffer. Note, the number of bytes is always rounded
  739. * to page size.
  740. *
  741. * This number is purposely set to a low number of 16384.
  742. * If the dump on oops happens, it will be much appreciated
  743. * to not have to wait for all that output. Anyway this can be
  744. * boot time and run time configurable.
  745. */
  746. #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
  747. static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
  748. /* trace_types holds a link list of available tracers. */
  749. static struct tracer *trace_types __read_mostly;
  750. /*
  751. * trace_types_lock is used to protect the trace_types list.
  752. */
  753. DEFINE_MUTEX(trace_types_lock);
  754. /*
  755. * serialize the access of the ring buffer
  756. *
  757. * ring buffer serializes readers, but it is low level protection.
  758. * The validity of the events (which returns by ring_buffer_peek() ..etc)
  759. * are not protected by ring buffer.
  760. *
  761. * The content of events may become garbage if we allow other process consumes
  762. * these events concurrently:
  763. * A) the page of the consumed events may become a normal page
  764. * (not reader page) in ring buffer, and this page will be rewritten
  765. * by events producer.
  766. * B) The page of the consumed events may become a page for splice_read,
  767. * and this page will be returned to system.
  768. *
  769. * These primitives allow multi process access to different cpu ring buffer
  770. * concurrently.
  771. *
  772. * These primitives don't distinguish read-only and read-consume access.
  773. * Multi read-only access are also serialized.
  774. */
  775. #ifdef CONFIG_SMP
  776. static DECLARE_RWSEM(all_cpu_access_lock);
  777. static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
  778. static inline void trace_access_lock(int cpu)
  779. {
  780. if (cpu == RING_BUFFER_ALL_CPUS) {
  781. /* gain it for accessing the whole ring buffer. */
  782. down_write(&all_cpu_access_lock);
  783. } else {
  784. /* gain it for accessing a cpu ring buffer. */
  785. /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
  786. down_read(&all_cpu_access_lock);
  787. /* Secondly block other access to this @cpu ring buffer. */
  788. mutex_lock(&per_cpu(cpu_access_lock, cpu));
  789. }
  790. }
  791. static inline void trace_access_unlock(int cpu)
  792. {
  793. if (cpu == RING_BUFFER_ALL_CPUS) {
  794. up_write(&all_cpu_access_lock);
  795. } else {
  796. mutex_unlock(&per_cpu(cpu_access_lock, cpu));
  797. up_read(&all_cpu_access_lock);
  798. }
  799. }
  800. static inline void trace_access_lock_init(void)
  801. {
  802. int cpu;
  803. for_each_possible_cpu(cpu)
  804. mutex_init(&per_cpu(cpu_access_lock, cpu));
  805. }
  806. #else
  807. static DEFINE_MUTEX(access_lock);
  808. static inline void trace_access_lock(int cpu)
  809. {
  810. (void)cpu;
  811. mutex_lock(&access_lock);
  812. }
  813. static inline void trace_access_unlock(int cpu)
  814. {
  815. (void)cpu;
  816. mutex_unlock(&access_lock);
  817. }
  818. static inline void trace_access_lock_init(void)
  819. {
  820. }
  821. #endif
  822. #ifdef CONFIG_STACKTRACE
  823. static void __ftrace_trace_stack(struct trace_array *tr,
  824. struct trace_buffer *buffer,
  825. unsigned int trace_ctx,
  826. int skip, struct pt_regs *regs);
  827. static inline void ftrace_trace_stack(struct trace_array *tr,
  828. struct trace_buffer *buffer,
  829. unsigned int trace_ctx,
  830. int skip, struct pt_regs *regs);
  831. #else
  832. static inline void __ftrace_trace_stack(struct trace_array *tr,
  833. struct trace_buffer *buffer,
  834. unsigned int trace_ctx,
  835. int skip, struct pt_regs *regs)
  836. {
  837. }
  838. static inline void ftrace_trace_stack(struct trace_array *tr,
  839. struct trace_buffer *buffer,
  840. unsigned long trace_ctx,
  841. int skip, struct pt_regs *regs)
  842. {
  843. }
  844. #endif
  845. static __always_inline void
  846. trace_event_setup(struct ring_buffer_event *event,
  847. int type, unsigned int trace_ctx)
  848. {
  849. struct trace_entry *ent = ring_buffer_event_data(event);
  850. tracing_generic_entry_update(ent, type, trace_ctx);
  851. }
  852. static __always_inline struct ring_buffer_event *
  853. __trace_buffer_lock_reserve(struct trace_buffer *buffer,
  854. int type,
  855. unsigned long len,
  856. unsigned int trace_ctx)
  857. {
  858. struct ring_buffer_event *event;
  859. event = ring_buffer_lock_reserve(buffer, len);
  860. if (event != NULL)
  861. trace_event_setup(event, type, trace_ctx);
  862. return event;
  863. }
  864. void tracer_tracing_on(struct trace_array *tr)
  865. {
  866. if (tr->array_buffer.buffer)
  867. ring_buffer_record_on(tr->array_buffer.buffer);
  868. /*
  869. * This flag is looked at when buffers haven't been allocated
  870. * yet, or by some tracers (like irqsoff), that just want to
  871. * know if the ring buffer has been disabled, but it can handle
  872. * races of where it gets disabled but we still do a record.
  873. * As the check is in the fast path of the tracers, it is more
  874. * important to be fast than accurate.
  875. */
  876. tr->buffer_disabled = 0;
  877. /* Make the flag seen by readers */
  878. smp_wmb();
  879. }
  880. /**
  881. * tracing_on - enable tracing buffers
  882. *
  883. * This function enables tracing buffers that may have been
  884. * disabled with tracing_off.
  885. */
  886. void tracing_on(void)
  887. {
  888. tracer_tracing_on(&global_trace);
  889. }
  890. EXPORT_SYMBOL_GPL(tracing_on);
  891. static __always_inline void
  892. __buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event)
  893. {
  894. __this_cpu_write(trace_taskinfo_save, true);
  895. /* If this is the temp buffer, we need to commit fully */
  896. if (this_cpu_read(trace_buffered_event) == event) {
  897. /* Length is in event->array[0] */
  898. ring_buffer_write(buffer, event->array[0], &event->array[1]);
  899. /* Release the temp buffer */
  900. this_cpu_dec(trace_buffered_event_cnt);
  901. /* ring_buffer_unlock_commit() enables preemption */
  902. preempt_enable_notrace();
  903. } else
  904. ring_buffer_unlock_commit(buffer);
  905. }
  906. int __trace_array_puts(struct trace_array *tr, unsigned long ip,
  907. const char *str, int size)
  908. {
  909. struct ring_buffer_event *event;
  910. struct trace_buffer *buffer;
  911. struct print_entry *entry;
  912. unsigned int trace_ctx;
  913. int alloc;
  914. if (!(tr->trace_flags & TRACE_ITER_PRINTK))
  915. return 0;
  916. if (unlikely(tracing_selftest_running && tr == &global_trace))
  917. return 0;
  918. if (unlikely(tracing_disabled))
  919. return 0;
  920. alloc = sizeof(*entry) + size + 2; /* possible \n added */
  921. trace_ctx = tracing_gen_ctx();
  922. buffer = tr->array_buffer.buffer;
  923. ring_buffer_nest_start(buffer);
  924. event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
  925. trace_ctx);
  926. if (!event) {
  927. size = 0;
  928. goto out;
  929. }
  930. entry = ring_buffer_event_data(event);
  931. entry->ip = ip;
  932. memcpy(&entry->buf, str, size);
  933. /* Add a newline if necessary */
  934. if (entry->buf[size - 1] != '\n') {
  935. entry->buf[size] = '\n';
  936. entry->buf[size + 1] = '\0';
  937. } else
  938. entry->buf[size] = '\0';
  939. __buffer_unlock_commit(buffer, event);
  940. ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
  941. out:
  942. ring_buffer_nest_end(buffer);
  943. return size;
  944. }
  945. EXPORT_SYMBOL_GPL(__trace_array_puts);
  946. /**
  947. * __trace_puts - write a constant string into the trace buffer.
  948. * @ip: The address of the caller
  949. * @str: The constant string to write
  950. * @size: The size of the string.
  951. */
  952. int __trace_puts(unsigned long ip, const char *str, int size)
  953. {
  954. return __trace_array_puts(printk_trace, ip, str, size);
  955. }
  956. EXPORT_SYMBOL_GPL(__trace_puts);
  957. /**
  958. * __trace_bputs - write the pointer to a constant string into trace buffer
  959. * @ip: The address of the caller
  960. * @str: The constant string to write to the buffer to
  961. */
  962. int __trace_bputs(unsigned long ip, const char *str)
  963. {
  964. struct trace_array *tr = READ_ONCE(printk_trace);
  965. struct ring_buffer_event *event;
  966. struct trace_buffer *buffer;
  967. struct bputs_entry *entry;
  968. unsigned int trace_ctx;
  969. int size = sizeof(struct bputs_entry);
  970. int ret = 0;
  971. if (!printk_binsafe(tr))
  972. return __trace_puts(ip, str, strlen(str));
  973. if (!(tr->trace_flags & TRACE_ITER_PRINTK))
  974. return 0;
  975. if (unlikely(tracing_selftest_running || tracing_disabled))
  976. return 0;
  977. trace_ctx = tracing_gen_ctx();
  978. buffer = tr->array_buffer.buffer;
  979. ring_buffer_nest_start(buffer);
  980. event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
  981. trace_ctx);
  982. if (!event)
  983. goto out;
  984. entry = ring_buffer_event_data(event);
  985. entry->ip = ip;
  986. entry->str = str;
  987. __buffer_unlock_commit(buffer, event);
  988. ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
  989. ret = 1;
  990. out:
  991. ring_buffer_nest_end(buffer);
  992. return ret;
  993. }
  994. EXPORT_SYMBOL_GPL(__trace_bputs);
  995. #ifdef CONFIG_TRACER_SNAPSHOT
  996. static void tracing_snapshot_instance_cond(struct trace_array *tr,
  997. void *cond_data)
  998. {
  999. struct tracer *tracer = tr->current_trace;
  1000. unsigned long flags;
  1001. if (in_nmi()) {
  1002. trace_array_puts(tr, "*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
  1003. trace_array_puts(tr, "*** snapshot is being ignored ***\n");
  1004. return;
  1005. }
  1006. if (!tr->allocated_snapshot) {
  1007. trace_array_puts(tr, "*** SNAPSHOT NOT ALLOCATED ***\n");
  1008. trace_array_puts(tr, "*** stopping trace here! ***\n");
  1009. tracer_tracing_off(tr);
  1010. return;
  1011. }
  1012. /* Note, snapshot can not be used when the tracer uses it */
  1013. if (tracer->use_max_tr) {
  1014. trace_array_puts(tr, "*** LATENCY TRACER ACTIVE ***\n");
  1015. trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
  1016. return;
  1017. }
  1018. if (tr->mapped) {
  1019. trace_array_puts(tr, "*** BUFFER MEMORY MAPPED ***\n");
  1020. trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
  1021. return;
  1022. }
  1023. local_irq_save(flags);
  1024. update_max_tr(tr, current, smp_processor_id(), cond_data);
  1025. local_irq_restore(flags);
  1026. }
  1027. void tracing_snapshot_instance(struct trace_array *tr)
  1028. {
  1029. tracing_snapshot_instance_cond(tr, NULL);
  1030. }
  1031. /**
  1032. * tracing_snapshot - take a snapshot of the current buffer.
  1033. *
  1034. * This causes a swap between the snapshot buffer and the current live
  1035. * tracing buffer. You can use this to take snapshots of the live
  1036. * trace when some condition is triggered, but continue to trace.
  1037. *
  1038. * Note, make sure to allocate the snapshot with either
  1039. * a tracing_snapshot_alloc(), or by doing it manually
  1040. * with: echo 1 > /sys/kernel/tracing/snapshot
  1041. *
  1042. * If the snapshot buffer is not allocated, it will stop tracing.
  1043. * Basically making a permanent snapshot.
  1044. */
  1045. void tracing_snapshot(void)
  1046. {
  1047. struct trace_array *tr = &global_trace;
  1048. tracing_snapshot_instance(tr);
  1049. }
  1050. EXPORT_SYMBOL_GPL(tracing_snapshot);
  1051. /**
  1052. * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
  1053. * @tr: The tracing instance to snapshot
  1054. * @cond_data: The data to be tested conditionally, and possibly saved
  1055. *
  1056. * This is the same as tracing_snapshot() except that the snapshot is
  1057. * conditional - the snapshot will only happen if the
  1058. * cond_snapshot.update() implementation receiving the cond_data
  1059. * returns true, which means that the trace array's cond_snapshot
  1060. * update() operation used the cond_data to determine whether the
  1061. * snapshot should be taken, and if it was, presumably saved it along
  1062. * with the snapshot.
  1063. */
  1064. void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
  1065. {
  1066. tracing_snapshot_instance_cond(tr, cond_data);
  1067. }
  1068. EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
  1069. /**
  1070. * tracing_cond_snapshot_data - get the user data associated with a snapshot
  1071. * @tr: The tracing instance
  1072. *
  1073. * When the user enables a conditional snapshot using
  1074. * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
  1075. * with the snapshot. This accessor is used to retrieve it.
  1076. *
  1077. * Should not be called from cond_snapshot.update(), since it takes
  1078. * the tr->max_lock lock, which the code calling
  1079. * cond_snapshot.update() has already done.
  1080. *
  1081. * Returns the cond_data associated with the trace array's snapshot.
  1082. */
  1083. void *tracing_cond_snapshot_data(struct trace_array *tr)
  1084. {
  1085. void *cond_data = NULL;
  1086. local_irq_disable();
  1087. arch_spin_lock(&tr->max_lock);
  1088. if (tr->cond_snapshot)
  1089. cond_data = tr->cond_snapshot->cond_data;
  1090. arch_spin_unlock(&tr->max_lock);
  1091. local_irq_enable();
  1092. return cond_data;
  1093. }
  1094. EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
  1095. static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
  1096. struct array_buffer *size_buf, int cpu_id);
  1097. static void set_buffer_entries(struct array_buffer *buf, unsigned long val);
  1098. int tracing_alloc_snapshot_instance(struct trace_array *tr)
  1099. {
  1100. int order;
  1101. int ret;
  1102. if (!tr->allocated_snapshot) {
  1103. /* Make the snapshot buffer have the same order as main buffer */
  1104. order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
  1105. ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, order);
  1106. if (ret < 0)
  1107. return ret;
  1108. /* allocate spare buffer */
  1109. ret = resize_buffer_duplicate_size(&tr->max_buffer,
  1110. &tr->array_buffer, RING_BUFFER_ALL_CPUS);
  1111. if (ret < 0)
  1112. return ret;
  1113. tr->allocated_snapshot = true;
  1114. }
  1115. return 0;
  1116. }
  1117. static void free_snapshot(struct trace_array *tr)
  1118. {
  1119. /*
  1120. * We don't free the ring buffer. instead, resize it because
  1121. * The max_tr ring buffer has some state (e.g. ring->clock) and
  1122. * we want preserve it.
  1123. */
  1124. ring_buffer_subbuf_order_set(tr->max_buffer.buffer, 0);
  1125. ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
  1126. set_buffer_entries(&tr->max_buffer, 1);
  1127. tracing_reset_online_cpus(&tr->max_buffer);
  1128. tr->allocated_snapshot = false;
  1129. }
  1130. static int tracing_arm_snapshot_locked(struct trace_array *tr)
  1131. {
  1132. int ret;
  1133. lockdep_assert_held(&trace_types_lock);
  1134. spin_lock(&tr->snapshot_trigger_lock);
  1135. if (tr->snapshot == UINT_MAX || tr->mapped) {
  1136. spin_unlock(&tr->snapshot_trigger_lock);
  1137. return -EBUSY;
  1138. }
  1139. tr->snapshot++;
  1140. spin_unlock(&tr->snapshot_trigger_lock);
  1141. ret = tracing_alloc_snapshot_instance(tr);
  1142. if (ret) {
  1143. spin_lock(&tr->snapshot_trigger_lock);
  1144. tr->snapshot--;
  1145. spin_unlock(&tr->snapshot_trigger_lock);
  1146. }
  1147. return ret;
  1148. }
  1149. int tracing_arm_snapshot(struct trace_array *tr)
  1150. {
  1151. int ret;
  1152. mutex_lock(&trace_types_lock);
  1153. ret = tracing_arm_snapshot_locked(tr);
  1154. mutex_unlock(&trace_types_lock);
  1155. return ret;
  1156. }
  1157. void tracing_disarm_snapshot(struct trace_array *tr)
  1158. {
  1159. spin_lock(&tr->snapshot_trigger_lock);
  1160. if (!WARN_ON(!tr->snapshot))
  1161. tr->snapshot--;
  1162. spin_unlock(&tr->snapshot_trigger_lock);
  1163. }
  1164. /**
  1165. * tracing_alloc_snapshot - allocate snapshot buffer.
  1166. *
  1167. * This only allocates the snapshot buffer if it isn't already
  1168. * allocated - it doesn't also take a snapshot.
  1169. *
  1170. * This is meant to be used in cases where the snapshot buffer needs
  1171. * to be set up for events that can't sleep but need to be able to
  1172. * trigger a snapshot.
  1173. */
  1174. int tracing_alloc_snapshot(void)
  1175. {
  1176. struct trace_array *tr = &global_trace;
  1177. int ret;
  1178. ret = tracing_alloc_snapshot_instance(tr);
  1179. WARN_ON(ret < 0);
  1180. return ret;
  1181. }
  1182. EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
  1183. /**
  1184. * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
  1185. *
  1186. * This is similar to tracing_snapshot(), but it will allocate the
  1187. * snapshot buffer if it isn't already allocated. Use this only
  1188. * where it is safe to sleep, as the allocation may sleep.
  1189. *
  1190. * This causes a swap between the snapshot buffer and the current live
  1191. * tracing buffer. You can use this to take snapshots of the live
  1192. * trace when some condition is triggered, but continue to trace.
  1193. */
  1194. void tracing_snapshot_alloc(void)
  1195. {
  1196. int ret;
  1197. ret = tracing_alloc_snapshot();
  1198. if (ret < 0)
  1199. return;
  1200. tracing_snapshot();
  1201. }
  1202. EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
  1203. /**
  1204. * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
  1205. * @tr: The tracing instance
  1206. * @cond_data: User data to associate with the snapshot
  1207. * @update: Implementation of the cond_snapshot update function
  1208. *
  1209. * Check whether the conditional snapshot for the given instance has
  1210. * already been enabled, or if the current tracer is already using a
  1211. * snapshot; if so, return -EBUSY, else create a cond_snapshot and
  1212. * save the cond_data and update function inside.
  1213. *
  1214. * Returns 0 if successful, error otherwise.
  1215. */
  1216. int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
  1217. cond_update_fn_t update)
  1218. {
  1219. struct cond_snapshot *cond_snapshot __free(kfree) =
  1220. kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
  1221. int ret;
  1222. if (!cond_snapshot)
  1223. return -ENOMEM;
  1224. cond_snapshot->cond_data = cond_data;
  1225. cond_snapshot->update = update;
  1226. guard(mutex)(&trace_types_lock);
  1227. if (tr->current_trace->use_max_tr)
  1228. return -EBUSY;
  1229. /*
  1230. * The cond_snapshot can only change to NULL without the
  1231. * trace_types_lock. We don't care if we race with it going
  1232. * to NULL, but we want to make sure that it's not set to
  1233. * something other than NULL when we get here, which we can
  1234. * do safely with only holding the trace_types_lock and not
  1235. * having to take the max_lock.
  1236. */
  1237. if (tr->cond_snapshot)
  1238. return -EBUSY;
  1239. ret = tracing_arm_snapshot_locked(tr);
  1240. if (ret)
  1241. return ret;
  1242. local_irq_disable();
  1243. arch_spin_lock(&tr->max_lock);
  1244. tr->cond_snapshot = no_free_ptr(cond_snapshot);
  1245. arch_spin_unlock(&tr->max_lock);
  1246. local_irq_enable();
  1247. return 0;
  1248. }
  1249. EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
  1250. /**
  1251. * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
  1252. * @tr: The tracing instance
  1253. *
  1254. * Check whether the conditional snapshot for the given instance is
  1255. * enabled; if so, free the cond_snapshot associated with it,
  1256. * otherwise return -EINVAL.
  1257. *
  1258. * Returns 0 if successful, error otherwise.
  1259. */
  1260. int tracing_snapshot_cond_disable(struct trace_array *tr)
  1261. {
  1262. int ret = 0;
  1263. local_irq_disable();
  1264. arch_spin_lock(&tr->max_lock);
  1265. if (!tr->cond_snapshot)
  1266. ret = -EINVAL;
  1267. else {
  1268. kfree(tr->cond_snapshot);
  1269. tr->cond_snapshot = NULL;
  1270. }
  1271. arch_spin_unlock(&tr->max_lock);
  1272. local_irq_enable();
  1273. tracing_disarm_snapshot(tr);
  1274. return ret;
  1275. }
  1276. EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
  1277. #else
  1278. void tracing_snapshot(void)
  1279. {
  1280. WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
  1281. }
  1282. EXPORT_SYMBOL_GPL(tracing_snapshot);
  1283. void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
  1284. {
  1285. WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
  1286. }
  1287. EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
  1288. int tracing_alloc_snapshot(void)
  1289. {
  1290. WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
  1291. return -ENODEV;
  1292. }
  1293. EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
  1294. void tracing_snapshot_alloc(void)
  1295. {
  1296. /* Give warning */
  1297. tracing_snapshot();
  1298. }
  1299. EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
  1300. void *tracing_cond_snapshot_data(struct trace_array *tr)
  1301. {
  1302. return NULL;
  1303. }
  1304. EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
  1305. int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
  1306. {
  1307. return -ENODEV;
  1308. }
  1309. EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
  1310. int tracing_snapshot_cond_disable(struct trace_array *tr)
  1311. {
  1312. return false;
  1313. }
  1314. EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
  1315. #define free_snapshot(tr) do { } while (0)
  1316. #define tracing_arm_snapshot_locked(tr) ({ -EBUSY; })
  1317. #endif /* CONFIG_TRACER_SNAPSHOT */
  1318. void tracer_tracing_off(struct trace_array *tr)
  1319. {
  1320. if (tr->array_buffer.buffer)
  1321. ring_buffer_record_off(tr->array_buffer.buffer);
  1322. /*
  1323. * This flag is looked at when buffers haven't been allocated
  1324. * yet, or by some tracers (like irqsoff), that just want to
  1325. * know if the ring buffer has been disabled, but it can handle
  1326. * races of where it gets disabled but we still do a record.
  1327. * As the check is in the fast path of the tracers, it is more
  1328. * important to be fast than accurate.
  1329. */
  1330. tr->buffer_disabled = 1;
  1331. /* Make the flag seen by readers */
  1332. smp_wmb();
  1333. }
  1334. /**
  1335. * tracing_off - turn off tracing buffers
  1336. *
  1337. * This function stops the tracing buffers from recording data.
  1338. * It does not disable any overhead the tracers themselves may
  1339. * be causing. This function simply causes all recording to
  1340. * the ring buffers to fail.
  1341. */
  1342. void tracing_off(void)
  1343. {
  1344. tracer_tracing_off(&global_trace);
  1345. }
  1346. EXPORT_SYMBOL_GPL(tracing_off);
  1347. void disable_trace_on_warning(void)
  1348. {
  1349. if (__disable_trace_on_warning) {
  1350. trace_array_printk_buf(global_trace.array_buffer.buffer, _THIS_IP_,
  1351. "Disabling tracing due to warning\n");
  1352. tracing_off();
  1353. }
  1354. }
  1355. /**
  1356. * tracer_tracing_is_on - show real state of ring buffer enabled
  1357. * @tr : the trace array to know if ring buffer is enabled
  1358. *
  1359. * Shows real state of the ring buffer if it is enabled or not.
  1360. */
  1361. bool tracer_tracing_is_on(struct trace_array *tr)
  1362. {
  1363. if (tr->array_buffer.buffer)
  1364. return ring_buffer_record_is_set_on(tr->array_buffer.buffer);
  1365. return !tr->buffer_disabled;
  1366. }
  1367. /**
  1368. * tracing_is_on - show state of ring buffers enabled
  1369. */
  1370. int tracing_is_on(void)
  1371. {
  1372. return tracer_tracing_is_on(&global_trace);
  1373. }
  1374. EXPORT_SYMBOL_GPL(tracing_is_on);
  1375. static int __init set_buf_size(char *str)
  1376. {
  1377. unsigned long buf_size;
  1378. if (!str)
  1379. return 0;
  1380. buf_size = memparse(str, &str);
  1381. /*
  1382. * nr_entries can not be zero and the startup
  1383. * tests require some buffer space. Therefore
  1384. * ensure we have at least 4096 bytes of buffer.
  1385. */
  1386. trace_buf_size = max(4096UL, buf_size);
  1387. return 1;
  1388. }
  1389. __setup("trace_buf_size=", set_buf_size);
  1390. static int __init set_tracing_thresh(char *str)
  1391. {
  1392. unsigned long threshold;
  1393. int ret;
  1394. if (!str)
  1395. return 0;
  1396. ret = kstrtoul(str, 0, &threshold);
  1397. if (ret < 0)
  1398. return 0;
  1399. tracing_thresh = threshold * 1000;
  1400. return 1;
  1401. }
  1402. __setup("tracing_thresh=", set_tracing_thresh);
  1403. unsigned long nsecs_to_usecs(unsigned long nsecs)
  1404. {
  1405. return nsecs / 1000;
  1406. }
  1407. /*
  1408. * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
  1409. * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
  1410. * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
  1411. * of strings in the order that the evals (enum) were defined.
  1412. */
  1413. #undef C
  1414. #define C(a, b) b
  1415. /* These must match the bit positions in trace_iterator_flags */
  1416. static const char *trace_options[] = {
  1417. TRACE_FLAGS
  1418. NULL
  1419. };
  1420. static struct {
  1421. u64 (*func)(void);
  1422. const char *name;
  1423. int in_ns; /* is this clock in nanoseconds? */
  1424. } trace_clocks[] = {
  1425. { trace_clock_local, "local", 1 },
  1426. { trace_clock_global, "global", 1 },
  1427. { trace_clock_counter, "counter", 0 },
  1428. { trace_clock_jiffies, "uptime", 0 },
  1429. { trace_clock, "perf", 1 },
  1430. { ktime_get_mono_fast_ns, "mono", 1 },
  1431. { ktime_get_raw_fast_ns, "mono_raw", 1 },
  1432. { ktime_get_boot_fast_ns, "boot", 1 },
  1433. { ktime_get_tai_fast_ns, "tai", 1 },
  1434. ARCH_TRACE_CLOCKS
  1435. };
  1436. bool trace_clock_in_ns(struct trace_array *tr)
  1437. {
  1438. if (trace_clocks[tr->clock_id].in_ns)
  1439. return true;
  1440. return false;
  1441. }
  1442. /*
  1443. * trace_parser_get_init - gets the buffer for trace parser
  1444. */
  1445. int trace_parser_get_init(struct trace_parser *parser, int size)
  1446. {
  1447. memset(parser, 0, sizeof(*parser));
  1448. parser->buffer = kmalloc(size, GFP_KERNEL);
  1449. if (!parser->buffer)
  1450. return 1;
  1451. parser->size = size;
  1452. return 0;
  1453. }
  1454. /*
  1455. * trace_parser_put - frees the buffer for trace parser
  1456. */
  1457. void trace_parser_put(struct trace_parser *parser)
  1458. {
  1459. kfree(parser->buffer);
  1460. parser->buffer = NULL;
  1461. }
  1462. /*
  1463. * trace_get_user - reads the user input string separated by space
  1464. * (matched by isspace(ch))
  1465. *
  1466. * For each string found the 'struct trace_parser' is updated,
  1467. * and the function returns.
  1468. *
  1469. * Returns number of bytes read.
  1470. *
  1471. * See kernel/trace/trace.h for 'struct trace_parser' details.
  1472. */
  1473. int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  1474. size_t cnt, loff_t *ppos)
  1475. {
  1476. char ch;
  1477. size_t read = 0;
  1478. ssize_t ret;
  1479. if (!*ppos)
  1480. trace_parser_clear(parser);
  1481. ret = get_user(ch, ubuf++);
  1482. if (ret)
  1483. goto fail;
  1484. read++;
  1485. cnt--;
  1486. /*
  1487. * The parser is not finished with the last write,
  1488. * continue reading the user input without skipping spaces.
  1489. */
  1490. if (!parser->cont) {
  1491. /* skip white space */
  1492. while (cnt && isspace(ch)) {
  1493. ret = get_user(ch, ubuf++);
  1494. if (ret)
  1495. goto fail;
  1496. read++;
  1497. cnt--;
  1498. }
  1499. parser->idx = 0;
  1500. /* only spaces were written */
  1501. if (isspace(ch) || !ch) {
  1502. *ppos += read;
  1503. return read;
  1504. }
  1505. }
  1506. /* read the non-space input */
  1507. while (cnt && !isspace(ch) && ch) {
  1508. if (parser->idx < parser->size - 1)
  1509. parser->buffer[parser->idx++] = ch;
  1510. else {
  1511. ret = -EINVAL;
  1512. goto fail;
  1513. }
  1514. ret = get_user(ch, ubuf++);
  1515. if (ret)
  1516. goto fail;
  1517. read++;
  1518. cnt--;
  1519. }
  1520. /* We either got finished input or we have to wait for another call. */
  1521. if (isspace(ch) || !ch) {
  1522. parser->buffer[parser->idx] = 0;
  1523. parser->cont = false;
  1524. } else if (parser->idx < parser->size - 1) {
  1525. parser->cont = true;
  1526. parser->buffer[parser->idx++] = ch;
  1527. /* Make sure the parsed string always terminates with '\0'. */
  1528. parser->buffer[parser->idx] = 0;
  1529. } else {
  1530. ret = -EINVAL;
  1531. goto fail;
  1532. }
  1533. *ppos += read;
  1534. return read;
  1535. fail:
  1536. trace_parser_fail(parser);
  1537. return ret;
  1538. }
  1539. /* TODO add a seq_buf_to_buffer() */
  1540. static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
  1541. {
  1542. int len;
  1543. if (trace_seq_used(s) <= s->readpos)
  1544. return -EBUSY;
  1545. len = trace_seq_used(s) - s->readpos;
  1546. if (cnt > len)
  1547. cnt = len;
  1548. memcpy(buf, s->buffer + s->readpos, cnt);
  1549. s->readpos += cnt;
  1550. return cnt;
  1551. }
  1552. unsigned long __read_mostly tracing_thresh;
  1553. #ifdef CONFIG_TRACER_MAX_TRACE
  1554. static const struct file_operations tracing_max_lat_fops;
  1555. #ifdef LATENCY_FS_NOTIFY
  1556. static struct workqueue_struct *fsnotify_wq;
  1557. static void latency_fsnotify_workfn(struct work_struct *work)
  1558. {
  1559. struct trace_array *tr = container_of(work, struct trace_array,
  1560. fsnotify_work);
  1561. fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY);
  1562. }
  1563. static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
  1564. {
  1565. struct trace_array *tr = container_of(iwork, struct trace_array,
  1566. fsnotify_irqwork);
  1567. queue_work(fsnotify_wq, &tr->fsnotify_work);
  1568. }
  1569. static void trace_create_maxlat_file(struct trace_array *tr,
  1570. struct dentry *d_tracer)
  1571. {
  1572. INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
  1573. init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
  1574. tr->d_max_latency = trace_create_file("tracing_max_latency",
  1575. TRACE_MODE_WRITE,
  1576. d_tracer, tr,
  1577. &tracing_max_lat_fops);
  1578. }
  1579. __init static int latency_fsnotify_init(void)
  1580. {
  1581. fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
  1582. WQ_UNBOUND | WQ_HIGHPRI, 0);
  1583. if (!fsnotify_wq) {
  1584. pr_err("Unable to allocate tr_max_lat_wq\n");
  1585. return -ENOMEM;
  1586. }
  1587. return 0;
  1588. }
  1589. late_initcall_sync(latency_fsnotify_init);
  1590. void latency_fsnotify(struct trace_array *tr)
  1591. {
  1592. if (!fsnotify_wq)
  1593. return;
  1594. /*
  1595. * We cannot call queue_work(&tr->fsnotify_work) from here because it's
  1596. * possible that we are called from __schedule() or do_idle(), which
  1597. * could cause a deadlock.
  1598. */
  1599. irq_work_queue(&tr->fsnotify_irqwork);
  1600. }
  1601. #else /* !LATENCY_FS_NOTIFY */
  1602. #define trace_create_maxlat_file(tr, d_tracer) \
  1603. trace_create_file("tracing_max_latency", TRACE_MODE_WRITE, \
  1604. d_tracer, tr, &tracing_max_lat_fops)
  1605. #endif
  1606. /*
  1607. * Copy the new maximum trace into the separate maximum-trace
  1608. * structure. (this way the maximum trace is permanently saved,
  1609. * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
  1610. */
  1611. static void
  1612. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  1613. {
  1614. struct array_buffer *trace_buf = &tr->array_buffer;
  1615. struct array_buffer *max_buf = &tr->max_buffer;
  1616. struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
  1617. struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
  1618. max_buf->cpu = cpu;
  1619. max_buf->time_start = data->preempt_timestamp;
  1620. max_data->saved_latency = tr->max_latency;
  1621. max_data->critical_start = data->critical_start;
  1622. max_data->critical_end = data->critical_end;
  1623. strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
  1624. max_data->pid = tsk->pid;
  1625. /*
  1626. * If tsk == current, then use current_uid(), as that does not use
  1627. * RCU. The irq tracer can be called out of RCU scope.
  1628. */
  1629. if (tsk == current)
  1630. max_data->uid = current_uid();
  1631. else
  1632. max_data->uid = task_uid(tsk);
  1633. max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  1634. max_data->policy = tsk->policy;
  1635. max_data->rt_priority = tsk->rt_priority;
  1636. /* record this tasks comm */
  1637. tracing_record_cmdline(tsk);
  1638. latency_fsnotify(tr);
  1639. }
  1640. /**
  1641. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  1642. * @tr: tracer
  1643. * @tsk: the task with the latency
  1644. * @cpu: The cpu that initiated the trace.
  1645. * @cond_data: User data associated with a conditional snapshot
  1646. *
  1647. * Flip the buffers between the @tr and the max_tr and record information
  1648. * about which task was the cause of this latency.
  1649. */
  1650. void
  1651. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
  1652. void *cond_data)
  1653. {
  1654. if (tr->stop_count)
  1655. return;
  1656. WARN_ON_ONCE(!irqs_disabled());
  1657. if (!tr->allocated_snapshot) {
  1658. /* Only the nop tracer should hit this when disabling */
  1659. WARN_ON_ONCE(tr->current_trace != &nop_trace);
  1660. return;
  1661. }
  1662. arch_spin_lock(&tr->max_lock);
  1663. /* Inherit the recordable setting from array_buffer */
  1664. if (ring_buffer_record_is_set_on(tr->array_buffer.buffer))
  1665. ring_buffer_record_on(tr->max_buffer.buffer);
  1666. else
  1667. ring_buffer_record_off(tr->max_buffer.buffer);
  1668. #ifdef CONFIG_TRACER_SNAPSHOT
  1669. if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data)) {
  1670. arch_spin_unlock(&tr->max_lock);
  1671. return;
  1672. }
  1673. #endif
  1674. swap(tr->array_buffer.buffer, tr->max_buffer.buffer);
  1675. __update_max_tr(tr, tsk, cpu);
  1676. arch_spin_unlock(&tr->max_lock);
  1677. /* Any waiters on the old snapshot buffer need to wake up */
  1678. ring_buffer_wake_waiters(tr->array_buffer.buffer, RING_BUFFER_ALL_CPUS);
  1679. }
  1680. /**
  1681. * update_max_tr_single - only copy one trace over, and reset the rest
  1682. * @tr: tracer
  1683. * @tsk: task with the latency
  1684. * @cpu: the cpu of the buffer to copy.
  1685. *
  1686. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  1687. */
  1688. void
  1689. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  1690. {
  1691. int ret;
  1692. if (tr->stop_count)
  1693. return;
  1694. WARN_ON_ONCE(!irqs_disabled());
  1695. if (!tr->allocated_snapshot) {
  1696. /* Only the nop tracer should hit this when disabling */
  1697. WARN_ON_ONCE(tr->current_trace != &nop_trace);
  1698. return;
  1699. }
  1700. arch_spin_lock(&tr->max_lock);
  1701. ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu);
  1702. if (ret == -EBUSY) {
  1703. /*
  1704. * We failed to swap the buffer due to a commit taking
  1705. * place on this CPU. We fail to record, but we reset
  1706. * the max trace buffer (no one writes directly to it)
  1707. * and flag that it failed.
  1708. * Another reason is resize is in progress.
  1709. */
  1710. trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
  1711. "Failed to swap buffers due to commit or resize in progress\n");
  1712. }
  1713. WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
  1714. __update_max_tr(tr, tsk, cpu);
  1715. arch_spin_unlock(&tr->max_lock);
  1716. }
  1717. #endif /* CONFIG_TRACER_MAX_TRACE */
  1718. struct pipe_wait {
  1719. struct trace_iterator *iter;
  1720. int wait_index;
  1721. };
  1722. static bool wait_pipe_cond(void *data)
  1723. {
  1724. struct pipe_wait *pwait = data;
  1725. struct trace_iterator *iter = pwait->iter;
  1726. if (atomic_read_acquire(&iter->wait_index) != pwait->wait_index)
  1727. return true;
  1728. return iter->closed;
  1729. }
  1730. static int wait_on_pipe(struct trace_iterator *iter, int full)
  1731. {
  1732. struct pipe_wait pwait;
  1733. int ret;
  1734. /* Iterators are static, they should be filled or empty */
  1735. if (trace_buffer_iter(iter, iter->cpu_file))
  1736. return 0;
  1737. pwait.wait_index = atomic_read_acquire(&iter->wait_index);
  1738. pwait.iter = iter;
  1739. ret = ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file, full,
  1740. wait_pipe_cond, &pwait);
  1741. #ifdef CONFIG_TRACER_MAX_TRACE
  1742. /*
  1743. * Make sure this is still the snapshot buffer, as if a snapshot were
  1744. * to happen, this would now be the main buffer.
  1745. */
  1746. if (iter->snapshot)
  1747. iter->array_buffer = &iter->tr->max_buffer;
  1748. #endif
  1749. return ret;
  1750. }
  1751. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1752. static bool selftests_can_run;
  1753. struct trace_selftests {
  1754. struct list_head list;
  1755. struct tracer *type;
  1756. };
  1757. static LIST_HEAD(postponed_selftests);
  1758. static int save_selftest(struct tracer *type)
  1759. {
  1760. struct trace_selftests *selftest;
  1761. selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
  1762. if (!selftest)
  1763. return -ENOMEM;
  1764. selftest->type = type;
  1765. list_add(&selftest->list, &postponed_selftests);
  1766. return 0;
  1767. }
  1768. static int run_tracer_selftest(struct tracer *type)
  1769. {
  1770. struct trace_array *tr = &global_trace;
  1771. struct tracer *saved_tracer = tr->current_trace;
  1772. int ret;
  1773. if (!type->selftest || tracing_selftest_disabled)
  1774. return 0;
  1775. /*
  1776. * If a tracer registers early in boot up (before scheduling is
  1777. * initialized and such), then do not run its selftests yet.
  1778. * Instead, run it a little later in the boot process.
  1779. */
  1780. if (!selftests_can_run)
  1781. return save_selftest(type);
  1782. if (!tracing_is_on()) {
  1783. pr_warn("Selftest for tracer %s skipped due to tracing disabled\n",
  1784. type->name);
  1785. return 0;
  1786. }
  1787. /*
  1788. * Run a selftest on this tracer.
  1789. * Here we reset the trace buffer, and set the current
  1790. * tracer to be this tracer. The tracer can then run some
  1791. * internal tracing to verify that everything is in order.
  1792. * If we fail, we do not register this tracer.
  1793. */
  1794. tracing_reset_online_cpus(&tr->array_buffer);
  1795. tr->current_trace = type;
  1796. #ifdef CONFIG_TRACER_MAX_TRACE
  1797. if (type->use_max_tr) {
  1798. /* If we expanded the buffers, make sure the max is expanded too */
  1799. if (tr->ring_buffer_expanded)
  1800. ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
  1801. RING_BUFFER_ALL_CPUS);
  1802. tr->allocated_snapshot = true;
  1803. }
  1804. #endif
  1805. /* the test is responsible for initializing and enabling */
  1806. pr_info("Testing tracer %s: ", type->name);
  1807. ret = type->selftest(type, tr);
  1808. /* the test is responsible for resetting too */
  1809. tr->current_trace = saved_tracer;
  1810. if (ret) {
  1811. printk(KERN_CONT "FAILED!\n");
  1812. /* Add the warning after printing 'FAILED' */
  1813. WARN_ON(1);
  1814. return -1;
  1815. }
  1816. /* Only reset on passing, to avoid touching corrupted buffers */
  1817. tracing_reset_online_cpus(&tr->array_buffer);
  1818. #ifdef CONFIG_TRACER_MAX_TRACE
  1819. if (type->use_max_tr) {
  1820. tr->allocated_snapshot = false;
  1821. /* Shrink the max buffer again */
  1822. if (tr->ring_buffer_expanded)
  1823. ring_buffer_resize(tr->max_buffer.buffer, 1,
  1824. RING_BUFFER_ALL_CPUS);
  1825. }
  1826. #endif
  1827. printk(KERN_CONT "PASSED\n");
  1828. return 0;
  1829. }
  1830. static int do_run_tracer_selftest(struct tracer *type)
  1831. {
  1832. int ret;
  1833. /*
  1834. * Tests can take a long time, especially if they are run one after the
  1835. * other, as does happen during bootup when all the tracers are
  1836. * registered. This could cause the soft lockup watchdog to trigger.
  1837. */
  1838. cond_resched();
  1839. tracing_selftest_running = true;
  1840. ret = run_tracer_selftest(type);
  1841. tracing_selftest_running = false;
  1842. return ret;
  1843. }
  1844. static __init int init_trace_selftests(void)
  1845. {
  1846. struct trace_selftests *p, *n;
  1847. struct tracer *t, **last;
  1848. int ret;
  1849. selftests_can_run = true;
  1850. guard(mutex)(&trace_types_lock);
  1851. if (list_empty(&postponed_selftests))
  1852. return 0;
  1853. pr_info("Running postponed tracer tests:\n");
  1854. tracing_selftest_running = true;
  1855. list_for_each_entry_safe(p, n, &postponed_selftests, list) {
  1856. /* This loop can take minutes when sanitizers are enabled, so
  1857. * lets make sure we allow RCU processing.
  1858. */
  1859. cond_resched();
  1860. ret = run_tracer_selftest(p->type);
  1861. /* If the test fails, then warn and remove from available_tracers */
  1862. if (ret < 0) {
  1863. WARN(1, "tracer: %s failed selftest, disabling\n",
  1864. p->type->name);
  1865. last = &trace_types;
  1866. for (t = trace_types; t; t = t->next) {
  1867. if (t == p->type) {
  1868. *last = t->next;
  1869. break;
  1870. }
  1871. last = &t->next;
  1872. }
  1873. }
  1874. list_del(&p->list);
  1875. kfree(p);
  1876. }
  1877. tracing_selftest_running = false;
  1878. return 0;
  1879. }
  1880. core_initcall(init_trace_selftests);
  1881. #else
  1882. static inline int do_run_tracer_selftest(struct tracer *type)
  1883. {
  1884. return 0;
  1885. }
  1886. #endif /* CONFIG_FTRACE_STARTUP_TEST */
  1887. static void add_tracer_options(struct trace_array *tr, struct tracer *t);
  1888. static void __init apply_trace_boot_options(void);
  1889. /**
  1890. * register_tracer - register a tracer with the ftrace system.
  1891. * @type: the plugin for the tracer
  1892. *
  1893. * Register a new plugin tracer.
  1894. */
  1895. int __init register_tracer(struct tracer *type)
  1896. {
  1897. struct tracer *t;
  1898. int ret = 0;
  1899. if (!type->name) {
  1900. pr_info("Tracer must have a name\n");
  1901. return -1;
  1902. }
  1903. if (strlen(type->name) >= MAX_TRACER_SIZE) {
  1904. pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
  1905. return -1;
  1906. }
  1907. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  1908. pr_warn("Can not register tracer %s due to lockdown\n",
  1909. type->name);
  1910. return -EPERM;
  1911. }
  1912. mutex_lock(&trace_types_lock);
  1913. for (t = trace_types; t; t = t->next) {
  1914. if (strcmp(type->name, t->name) == 0) {
  1915. /* already found */
  1916. pr_info("Tracer %s already registered\n",
  1917. type->name);
  1918. ret = -1;
  1919. goto out;
  1920. }
  1921. }
  1922. if (!type->set_flag)
  1923. type->set_flag = &dummy_set_flag;
  1924. if (!type->flags) {
  1925. /*allocate a dummy tracer_flags*/
  1926. type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
  1927. if (!type->flags) {
  1928. ret = -ENOMEM;
  1929. goto out;
  1930. }
  1931. type->flags->val = 0;
  1932. type->flags->opts = dummy_tracer_opt;
  1933. } else
  1934. if (!type->flags->opts)
  1935. type->flags->opts = dummy_tracer_opt;
  1936. /* store the tracer for __set_tracer_option */
  1937. type->flags->trace = type;
  1938. ret = do_run_tracer_selftest(type);
  1939. if (ret < 0)
  1940. goto out;
  1941. type->next = trace_types;
  1942. trace_types = type;
  1943. add_tracer_options(&global_trace, type);
  1944. out:
  1945. mutex_unlock(&trace_types_lock);
  1946. if (ret || !default_bootup_tracer)
  1947. return ret;
  1948. if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
  1949. return 0;
  1950. printk(KERN_INFO "Starting tracer '%s'\n", type->name);
  1951. /* Do we want this tracer to start on bootup? */
  1952. tracing_set_tracer(&global_trace, type->name);
  1953. default_bootup_tracer = NULL;
  1954. apply_trace_boot_options();
  1955. /* disable other selftests, since this will break it. */
  1956. disable_tracing_selftest("running a tracer");
  1957. return 0;
  1958. }
  1959. static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
  1960. {
  1961. struct trace_buffer *buffer = buf->buffer;
  1962. if (!buffer)
  1963. return;
  1964. ring_buffer_record_disable(buffer);
  1965. /* Make sure all commits have finished */
  1966. synchronize_rcu();
  1967. ring_buffer_reset_cpu(buffer, cpu);
  1968. ring_buffer_record_enable(buffer);
  1969. }
  1970. void tracing_reset_online_cpus(struct array_buffer *buf)
  1971. {
  1972. struct trace_buffer *buffer = buf->buffer;
  1973. if (!buffer)
  1974. return;
  1975. ring_buffer_record_disable(buffer);
  1976. /* Make sure all commits have finished */
  1977. synchronize_rcu();
  1978. buf->time_start = buffer_ftrace_now(buf, buf->cpu);
  1979. ring_buffer_reset_online_cpus(buffer);
  1980. ring_buffer_record_enable(buffer);
  1981. }
  1982. static void tracing_reset_all_cpus(struct array_buffer *buf)
  1983. {
  1984. struct trace_buffer *buffer = buf->buffer;
  1985. if (!buffer)
  1986. return;
  1987. ring_buffer_record_disable(buffer);
  1988. /* Make sure all commits have finished */
  1989. synchronize_rcu();
  1990. buf->time_start = buffer_ftrace_now(buf, buf->cpu);
  1991. ring_buffer_reset(buffer);
  1992. ring_buffer_record_enable(buffer);
  1993. }
  1994. /* Must have trace_types_lock held */
  1995. void tracing_reset_all_online_cpus_unlocked(void)
  1996. {
  1997. struct trace_array *tr;
  1998. lockdep_assert_held(&trace_types_lock);
  1999. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  2000. if (!tr->clear_trace)
  2001. continue;
  2002. tr->clear_trace = false;
  2003. tracing_reset_online_cpus(&tr->array_buffer);
  2004. #ifdef CONFIG_TRACER_MAX_TRACE
  2005. tracing_reset_online_cpus(&tr->max_buffer);
  2006. #endif
  2007. }
  2008. }
  2009. void tracing_reset_all_online_cpus(void)
  2010. {
  2011. mutex_lock(&trace_types_lock);
  2012. tracing_reset_all_online_cpus_unlocked();
  2013. mutex_unlock(&trace_types_lock);
  2014. }
  2015. int is_tracing_stopped(void)
  2016. {
  2017. return global_trace.stop_count;
  2018. }
  2019. static void tracing_start_tr(struct trace_array *tr)
  2020. {
  2021. struct trace_buffer *buffer;
  2022. unsigned long flags;
  2023. if (tracing_disabled)
  2024. return;
  2025. raw_spin_lock_irqsave(&tr->start_lock, flags);
  2026. if (--tr->stop_count) {
  2027. if (WARN_ON_ONCE(tr->stop_count < 0)) {
  2028. /* Someone screwed up their debugging */
  2029. tr->stop_count = 0;
  2030. }
  2031. goto out;
  2032. }
  2033. /* Prevent the buffers from switching */
  2034. arch_spin_lock(&tr->max_lock);
  2035. buffer = tr->array_buffer.buffer;
  2036. if (buffer)
  2037. ring_buffer_record_enable(buffer);
  2038. #ifdef CONFIG_TRACER_MAX_TRACE
  2039. buffer = tr->max_buffer.buffer;
  2040. if (buffer)
  2041. ring_buffer_record_enable(buffer);
  2042. #endif
  2043. arch_spin_unlock(&tr->max_lock);
  2044. out:
  2045. raw_spin_unlock_irqrestore(&tr->start_lock, flags);
  2046. }
  2047. /**
  2048. * tracing_start - quick start of the tracer
  2049. *
  2050. * If tracing is enabled but was stopped by tracing_stop,
  2051. * this will start the tracer back up.
  2052. */
  2053. void tracing_start(void)
  2054. {
  2055. return tracing_start_tr(&global_trace);
  2056. }
  2057. static void tracing_stop_tr(struct trace_array *tr)
  2058. {
  2059. struct trace_buffer *buffer;
  2060. unsigned long flags;
  2061. raw_spin_lock_irqsave(&tr->start_lock, flags);
  2062. if (tr->stop_count++)
  2063. goto out;
  2064. /* Prevent the buffers from switching */
  2065. arch_spin_lock(&tr->max_lock);
  2066. buffer = tr->array_buffer.buffer;
  2067. if (buffer)
  2068. ring_buffer_record_disable(buffer);
  2069. #ifdef CONFIG_TRACER_MAX_TRACE
  2070. buffer = tr->max_buffer.buffer;
  2071. if (buffer)
  2072. ring_buffer_record_disable(buffer);
  2073. #endif
  2074. arch_spin_unlock(&tr->max_lock);
  2075. out:
  2076. raw_spin_unlock_irqrestore(&tr->start_lock, flags);
  2077. }
  2078. /**
  2079. * tracing_stop - quick stop of the tracer
  2080. *
  2081. * Light weight way to stop tracing. Use in conjunction with
  2082. * tracing_start.
  2083. */
  2084. void tracing_stop(void)
  2085. {
  2086. return tracing_stop_tr(&global_trace);
  2087. }
  2088. /*
  2089. * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
  2090. * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
  2091. * simplifies those functions and keeps them in sync.
  2092. */
  2093. enum print_line_t trace_handle_return(struct trace_seq *s)
  2094. {
  2095. return trace_seq_has_overflowed(s) ?
  2096. TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
  2097. }
  2098. EXPORT_SYMBOL_GPL(trace_handle_return);
  2099. static unsigned short migration_disable_value(void)
  2100. {
  2101. #if defined(CONFIG_SMP)
  2102. return current->migration_disabled;
  2103. #else
  2104. return 0;
  2105. #endif
  2106. }
  2107. unsigned int tracing_gen_ctx_irq_test(unsigned int irqs_status)
  2108. {
  2109. unsigned int trace_flags = irqs_status;
  2110. unsigned int pc;
  2111. pc = preempt_count();
  2112. if (pc & NMI_MASK)
  2113. trace_flags |= TRACE_FLAG_NMI;
  2114. if (pc & HARDIRQ_MASK)
  2115. trace_flags |= TRACE_FLAG_HARDIRQ;
  2116. if (in_serving_softirq())
  2117. trace_flags |= TRACE_FLAG_SOFTIRQ;
  2118. if (softirq_count() >> (SOFTIRQ_SHIFT + 1))
  2119. trace_flags |= TRACE_FLAG_BH_OFF;
  2120. if (tif_need_resched())
  2121. trace_flags |= TRACE_FLAG_NEED_RESCHED;
  2122. if (test_preempt_need_resched())
  2123. trace_flags |= TRACE_FLAG_PREEMPT_RESCHED;
  2124. return (trace_flags << 16) | (min_t(unsigned int, pc & 0xff, 0xf)) |
  2125. (min_t(unsigned int, migration_disable_value(), 0xf)) << 4;
  2126. }
  2127. struct ring_buffer_event *
  2128. trace_buffer_lock_reserve(struct trace_buffer *buffer,
  2129. int type,
  2130. unsigned long len,
  2131. unsigned int trace_ctx)
  2132. {
  2133. return __trace_buffer_lock_reserve(buffer, type, len, trace_ctx);
  2134. }
  2135. DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
  2136. DEFINE_PER_CPU(int, trace_buffered_event_cnt);
  2137. static int trace_buffered_event_ref;
  2138. /**
  2139. * trace_buffered_event_enable - enable buffering events
  2140. *
  2141. * When events are being filtered, it is quicker to use a temporary
  2142. * buffer to write the event data into if there's a likely chance
  2143. * that it will not be committed. The discard of the ring buffer
  2144. * is not as fast as committing, and is much slower than copying
  2145. * a commit.
  2146. *
  2147. * When an event is to be filtered, allocate per cpu buffers to
  2148. * write the event data into, and if the event is filtered and discarded
  2149. * it is simply dropped, otherwise, the entire data is to be committed
  2150. * in one shot.
  2151. */
  2152. void trace_buffered_event_enable(void)
  2153. {
  2154. struct ring_buffer_event *event;
  2155. struct page *page;
  2156. int cpu;
  2157. WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
  2158. if (trace_buffered_event_ref++)
  2159. return;
  2160. for_each_tracing_cpu(cpu) {
  2161. page = alloc_pages_node(cpu_to_node(cpu),
  2162. GFP_KERNEL | __GFP_NORETRY, 0);
  2163. /* This is just an optimization and can handle failures */
  2164. if (!page) {
  2165. pr_err("Failed to allocate event buffer\n");
  2166. break;
  2167. }
  2168. event = page_address(page);
  2169. memset(event, 0, sizeof(*event));
  2170. per_cpu(trace_buffered_event, cpu) = event;
  2171. preempt_disable();
  2172. if (cpu == smp_processor_id() &&
  2173. __this_cpu_read(trace_buffered_event) !=
  2174. per_cpu(trace_buffered_event, cpu))
  2175. WARN_ON_ONCE(1);
  2176. preempt_enable();
  2177. }
  2178. }
  2179. static void enable_trace_buffered_event(void *data)
  2180. {
  2181. /* Probably not needed, but do it anyway */
  2182. smp_rmb();
  2183. this_cpu_dec(trace_buffered_event_cnt);
  2184. }
  2185. static void disable_trace_buffered_event(void *data)
  2186. {
  2187. this_cpu_inc(trace_buffered_event_cnt);
  2188. }
  2189. /**
  2190. * trace_buffered_event_disable - disable buffering events
  2191. *
  2192. * When a filter is removed, it is faster to not use the buffered
  2193. * events, and to commit directly into the ring buffer. Free up
  2194. * the temp buffers when there are no more users. This requires
  2195. * special synchronization with current events.
  2196. */
  2197. void trace_buffered_event_disable(void)
  2198. {
  2199. int cpu;
  2200. WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
  2201. if (WARN_ON_ONCE(!trace_buffered_event_ref))
  2202. return;
  2203. if (--trace_buffered_event_ref)
  2204. return;
  2205. /* For each CPU, set the buffer as used. */
  2206. on_each_cpu_mask(tracing_buffer_mask, disable_trace_buffered_event,
  2207. NULL, true);
  2208. /* Wait for all current users to finish */
  2209. synchronize_rcu();
  2210. for_each_tracing_cpu(cpu) {
  2211. free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
  2212. per_cpu(trace_buffered_event, cpu) = NULL;
  2213. }
  2214. /*
  2215. * Wait for all CPUs that potentially started checking if they can use
  2216. * their event buffer only after the previous synchronize_rcu() call and
  2217. * they still read a valid pointer from trace_buffered_event. It must be
  2218. * ensured they don't see cleared trace_buffered_event_cnt else they
  2219. * could wrongly decide to use the pointed-to buffer which is now freed.
  2220. */
  2221. synchronize_rcu();
  2222. /* For each CPU, relinquish the buffer */
  2223. on_each_cpu_mask(tracing_buffer_mask, enable_trace_buffered_event, NULL,
  2224. true);
  2225. }
  2226. static struct trace_buffer *temp_buffer;
  2227. struct ring_buffer_event *
  2228. trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
  2229. struct trace_event_file *trace_file,
  2230. int type, unsigned long len,
  2231. unsigned int trace_ctx)
  2232. {
  2233. struct ring_buffer_event *entry;
  2234. struct trace_array *tr = trace_file->tr;
  2235. int val;
  2236. *current_rb = tr->array_buffer.buffer;
  2237. if (!tr->no_filter_buffering_ref &&
  2238. (trace_file->flags & (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED))) {
  2239. preempt_disable_notrace();
  2240. /*
  2241. * Filtering is on, so try to use the per cpu buffer first.
  2242. * This buffer will simulate a ring_buffer_event,
  2243. * where the type_len is zero and the array[0] will
  2244. * hold the full length.
  2245. * (see include/linux/ring-buffer.h for details on
  2246. * how the ring_buffer_event is structured).
  2247. *
  2248. * Using a temp buffer during filtering and copying it
  2249. * on a matched filter is quicker than writing directly
  2250. * into the ring buffer and then discarding it when
  2251. * it doesn't match. That is because the discard
  2252. * requires several atomic operations to get right.
  2253. * Copying on match and doing nothing on a failed match
  2254. * is still quicker than no copy on match, but having
  2255. * to discard out of the ring buffer on a failed match.
  2256. */
  2257. if ((entry = __this_cpu_read(trace_buffered_event))) {
  2258. int max_len = PAGE_SIZE - struct_size(entry, array, 1);
  2259. val = this_cpu_inc_return(trace_buffered_event_cnt);
  2260. /*
  2261. * Preemption is disabled, but interrupts and NMIs
  2262. * can still come in now. If that happens after
  2263. * the above increment, then it will have to go
  2264. * back to the old method of allocating the event
  2265. * on the ring buffer, and if the filter fails, it
  2266. * will have to call ring_buffer_discard_commit()
  2267. * to remove it.
  2268. *
  2269. * Need to also check the unlikely case that the
  2270. * length is bigger than the temp buffer size.
  2271. * If that happens, then the reserve is pretty much
  2272. * guaranteed to fail, as the ring buffer currently
  2273. * only allows events less than a page. But that may
  2274. * change in the future, so let the ring buffer reserve
  2275. * handle the failure in that case.
  2276. */
  2277. if (val == 1 && likely(len <= max_len)) {
  2278. trace_event_setup(entry, type, trace_ctx);
  2279. entry->array[0] = len;
  2280. /* Return with preemption disabled */
  2281. return entry;
  2282. }
  2283. this_cpu_dec(trace_buffered_event_cnt);
  2284. }
  2285. /* __trace_buffer_lock_reserve() disables preemption */
  2286. preempt_enable_notrace();
  2287. }
  2288. entry = __trace_buffer_lock_reserve(*current_rb, type, len,
  2289. trace_ctx);
  2290. /*
  2291. * If tracing is off, but we have triggers enabled
  2292. * we still need to look at the event data. Use the temp_buffer
  2293. * to store the trace event for the trigger to use. It's recursive
  2294. * safe and will not be recorded anywhere.
  2295. */
  2296. if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
  2297. *current_rb = temp_buffer;
  2298. entry = __trace_buffer_lock_reserve(*current_rb, type, len,
  2299. trace_ctx);
  2300. }
  2301. return entry;
  2302. }
  2303. EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
  2304. static DEFINE_RAW_SPINLOCK(tracepoint_iter_lock);
  2305. static DEFINE_MUTEX(tracepoint_printk_mutex);
  2306. static void output_printk(struct trace_event_buffer *fbuffer)
  2307. {
  2308. struct trace_event_call *event_call;
  2309. struct trace_event_file *file;
  2310. struct trace_event *event;
  2311. unsigned long flags;
  2312. struct trace_iterator *iter = tracepoint_print_iter;
  2313. /* We should never get here if iter is NULL */
  2314. if (WARN_ON_ONCE(!iter))
  2315. return;
  2316. event_call = fbuffer->trace_file->event_call;
  2317. if (!event_call || !event_call->event.funcs ||
  2318. !event_call->event.funcs->trace)
  2319. return;
  2320. file = fbuffer->trace_file;
  2321. if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
  2322. (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
  2323. !filter_match_preds(file->filter, fbuffer->entry)))
  2324. return;
  2325. event = &fbuffer->trace_file->event_call->event;
  2326. raw_spin_lock_irqsave(&tracepoint_iter_lock, flags);
  2327. trace_seq_init(&iter->seq);
  2328. iter->ent = fbuffer->entry;
  2329. event_call->event.funcs->trace(iter, 0, event);
  2330. trace_seq_putc(&iter->seq, 0);
  2331. printk("%s", iter->seq.buffer);
  2332. raw_spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
  2333. }
  2334. int tracepoint_printk_sysctl(const struct ctl_table *table, int write,
  2335. void *buffer, size_t *lenp,
  2336. loff_t *ppos)
  2337. {
  2338. int save_tracepoint_printk;
  2339. int ret;
  2340. guard(mutex)(&tracepoint_printk_mutex);
  2341. save_tracepoint_printk = tracepoint_printk;
  2342. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  2343. /*
  2344. * This will force exiting early, as tracepoint_printk
  2345. * is always zero when tracepoint_printk_iter is not allocated
  2346. */
  2347. if (!tracepoint_print_iter)
  2348. tracepoint_printk = 0;
  2349. if (save_tracepoint_printk == tracepoint_printk)
  2350. return ret;
  2351. if (tracepoint_printk)
  2352. static_key_enable(&tracepoint_printk_key.key);
  2353. else
  2354. static_key_disable(&tracepoint_printk_key.key);
  2355. return ret;
  2356. }
  2357. void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
  2358. {
  2359. enum event_trigger_type tt = ETT_NONE;
  2360. struct trace_event_file *file = fbuffer->trace_file;
  2361. if (__event_trigger_test_discard(file, fbuffer->buffer, fbuffer->event,
  2362. fbuffer->entry, &tt))
  2363. goto discard;
  2364. if (static_key_false(&tracepoint_printk_key.key))
  2365. output_printk(fbuffer);
  2366. if (static_branch_unlikely(&trace_event_exports_enabled))
  2367. ftrace_exports(fbuffer->event, TRACE_EXPORT_EVENT);
  2368. trace_buffer_unlock_commit_regs(file->tr, fbuffer->buffer,
  2369. fbuffer->event, fbuffer->trace_ctx, fbuffer->regs);
  2370. discard:
  2371. if (tt)
  2372. event_triggers_post_call(file, tt);
  2373. }
  2374. EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
  2375. /*
  2376. * Skip 3:
  2377. *
  2378. * trace_buffer_unlock_commit_regs()
  2379. * trace_event_buffer_commit()
  2380. * trace_event_raw_event_xxx()
  2381. */
  2382. # define STACK_SKIP 3
  2383. void trace_buffer_unlock_commit_regs(struct trace_array *tr,
  2384. struct trace_buffer *buffer,
  2385. struct ring_buffer_event *event,
  2386. unsigned int trace_ctx,
  2387. struct pt_regs *regs)
  2388. {
  2389. __buffer_unlock_commit(buffer, event);
  2390. /*
  2391. * If regs is not set, then skip the necessary functions.
  2392. * Note, we can still get here via blktrace, wakeup tracer
  2393. * and mmiotrace, but that's ok if they lose a function or
  2394. * two. They are not that meaningful.
  2395. */
  2396. ftrace_trace_stack(tr, buffer, trace_ctx, regs ? 0 : STACK_SKIP, regs);
  2397. ftrace_trace_userstack(tr, buffer, trace_ctx);
  2398. }
  2399. /*
  2400. * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
  2401. */
  2402. void
  2403. trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
  2404. struct ring_buffer_event *event)
  2405. {
  2406. __buffer_unlock_commit(buffer, event);
  2407. }
  2408. void
  2409. trace_function(struct trace_array *tr, unsigned long ip, unsigned long
  2410. parent_ip, unsigned int trace_ctx)
  2411. {
  2412. struct trace_event_call *call = &event_function;
  2413. struct trace_buffer *buffer = tr->array_buffer.buffer;
  2414. struct ring_buffer_event *event;
  2415. struct ftrace_entry *entry;
  2416. event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
  2417. trace_ctx);
  2418. if (!event)
  2419. return;
  2420. entry = ring_buffer_event_data(event);
  2421. entry->ip = ip;
  2422. entry->parent_ip = parent_ip;
  2423. if (!call_filter_check_discard(call, entry, buffer, event)) {
  2424. if (static_branch_unlikely(&trace_function_exports_enabled))
  2425. ftrace_exports(event, TRACE_EXPORT_FUNCTION);
  2426. __buffer_unlock_commit(buffer, event);
  2427. }
  2428. }
  2429. #ifdef CONFIG_STACKTRACE
  2430. /* Allow 4 levels of nesting: normal, softirq, irq, NMI */
  2431. #define FTRACE_KSTACK_NESTING 4
  2432. #define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING)
  2433. struct ftrace_stack {
  2434. unsigned long calls[FTRACE_KSTACK_ENTRIES];
  2435. };
  2436. struct ftrace_stacks {
  2437. struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
  2438. };
  2439. static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
  2440. static DEFINE_PER_CPU(int, ftrace_stack_reserve);
  2441. static void __ftrace_trace_stack(struct trace_array *tr,
  2442. struct trace_buffer *buffer,
  2443. unsigned int trace_ctx,
  2444. int skip, struct pt_regs *regs)
  2445. {
  2446. struct trace_event_call *call = &event_kernel_stack;
  2447. struct ring_buffer_event *event;
  2448. unsigned int size, nr_entries;
  2449. struct ftrace_stack *fstack;
  2450. struct stack_entry *entry;
  2451. int stackidx;
  2452. /*
  2453. * Add one, for this function and the call to save_stack_trace()
  2454. * If regs is set, then these functions will not be in the way.
  2455. */
  2456. #ifndef CONFIG_UNWINDER_ORC
  2457. if (!regs)
  2458. skip++;
  2459. #endif
  2460. preempt_disable_notrace();
  2461. stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
  2462. /* This should never happen. If it does, yell once and skip */
  2463. if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING))
  2464. goto out;
  2465. /*
  2466. * The above __this_cpu_inc_return() is 'atomic' cpu local. An
  2467. * interrupt will either see the value pre increment or post
  2468. * increment. If the interrupt happens pre increment it will have
  2469. * restored the counter when it returns. We just need a barrier to
  2470. * keep gcc from moving things around.
  2471. */
  2472. barrier();
  2473. fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
  2474. size = ARRAY_SIZE(fstack->calls);
  2475. if (regs) {
  2476. nr_entries = stack_trace_save_regs(regs, fstack->calls,
  2477. size, skip);
  2478. } else {
  2479. nr_entries = stack_trace_save(fstack->calls, size, skip);
  2480. }
  2481. #ifdef CONFIG_DYNAMIC_FTRACE
  2482. /* Mark entry of stack trace as trampoline code */
  2483. if (tr->ops && tr->ops->trampoline) {
  2484. unsigned long tramp_start = tr->ops->trampoline;
  2485. unsigned long tramp_end = tramp_start + tr->ops->trampoline_size;
  2486. unsigned long *calls = fstack->calls;
  2487. for (int i = 0; i < nr_entries; i++) {
  2488. if (calls[i] >= tramp_start && calls[i] < tramp_end)
  2489. calls[i] = FTRACE_TRAMPOLINE_MARKER;
  2490. }
  2491. }
  2492. #endif
  2493. event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
  2494. struct_size(entry, caller, nr_entries),
  2495. trace_ctx);
  2496. if (!event)
  2497. goto out;
  2498. entry = ring_buffer_event_data(event);
  2499. entry->size = nr_entries;
  2500. memcpy(&entry->caller, fstack->calls,
  2501. flex_array_size(entry, caller, nr_entries));
  2502. if (!call_filter_check_discard(call, entry, buffer, event))
  2503. __buffer_unlock_commit(buffer, event);
  2504. out:
  2505. /* Again, don't let gcc optimize things here */
  2506. barrier();
  2507. __this_cpu_dec(ftrace_stack_reserve);
  2508. preempt_enable_notrace();
  2509. }
  2510. static inline void ftrace_trace_stack(struct trace_array *tr,
  2511. struct trace_buffer *buffer,
  2512. unsigned int trace_ctx,
  2513. int skip, struct pt_regs *regs)
  2514. {
  2515. if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
  2516. return;
  2517. __ftrace_trace_stack(tr, buffer, trace_ctx, skip, regs);
  2518. }
  2519. void __trace_stack(struct trace_array *tr, unsigned int trace_ctx,
  2520. int skip)
  2521. {
  2522. struct trace_buffer *buffer = tr->array_buffer.buffer;
  2523. if (rcu_is_watching()) {
  2524. __ftrace_trace_stack(tr, buffer, trace_ctx, skip, NULL);
  2525. return;
  2526. }
  2527. if (WARN_ON_ONCE(IS_ENABLED(CONFIG_GENERIC_ENTRY)))
  2528. return;
  2529. /*
  2530. * When an NMI triggers, RCU is enabled via ct_nmi_enter(),
  2531. * but if the above rcu_is_watching() failed, then the NMI
  2532. * triggered someplace critical, and ct_irq_enter() should
  2533. * not be called from NMI.
  2534. */
  2535. if (unlikely(in_nmi()))
  2536. return;
  2537. ct_irq_enter_irqson();
  2538. __ftrace_trace_stack(tr, buffer, trace_ctx, skip, NULL);
  2539. ct_irq_exit_irqson();
  2540. }
  2541. /**
  2542. * trace_dump_stack - record a stack back trace in the trace buffer
  2543. * @skip: Number of functions to skip (helper handlers)
  2544. */
  2545. void trace_dump_stack(int skip)
  2546. {
  2547. if (tracing_disabled || tracing_selftest_running)
  2548. return;
  2549. #ifndef CONFIG_UNWINDER_ORC
  2550. /* Skip 1 to skip this function. */
  2551. skip++;
  2552. #endif
  2553. __ftrace_trace_stack(printk_trace, printk_trace->array_buffer.buffer,
  2554. tracing_gen_ctx(), skip, NULL);
  2555. }
  2556. EXPORT_SYMBOL_GPL(trace_dump_stack);
  2557. #ifdef CONFIG_USER_STACKTRACE_SUPPORT
  2558. static DEFINE_PER_CPU(int, user_stack_count);
  2559. static void
  2560. ftrace_trace_userstack(struct trace_array *tr,
  2561. struct trace_buffer *buffer, unsigned int trace_ctx)
  2562. {
  2563. struct trace_event_call *call = &event_user_stack;
  2564. struct ring_buffer_event *event;
  2565. struct userstack_entry *entry;
  2566. if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
  2567. return;
  2568. /*
  2569. * NMIs can not handle page faults, even with fix ups.
  2570. * The save user stack can (and often does) fault.
  2571. */
  2572. if (unlikely(in_nmi()))
  2573. return;
  2574. /*
  2575. * prevent recursion, since the user stack tracing may
  2576. * trigger other kernel events.
  2577. */
  2578. preempt_disable();
  2579. if (__this_cpu_read(user_stack_count))
  2580. goto out;
  2581. __this_cpu_inc(user_stack_count);
  2582. event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
  2583. sizeof(*entry), trace_ctx);
  2584. if (!event)
  2585. goto out_drop_count;
  2586. entry = ring_buffer_event_data(event);
  2587. entry->tgid = current->tgid;
  2588. memset(&entry->caller, 0, sizeof(entry->caller));
  2589. stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
  2590. if (!call_filter_check_discard(call, entry, buffer, event))
  2591. __buffer_unlock_commit(buffer, event);
  2592. out_drop_count:
  2593. __this_cpu_dec(user_stack_count);
  2594. out:
  2595. preempt_enable();
  2596. }
  2597. #else /* CONFIG_USER_STACKTRACE_SUPPORT */
  2598. static void ftrace_trace_userstack(struct trace_array *tr,
  2599. struct trace_buffer *buffer,
  2600. unsigned int trace_ctx)
  2601. {
  2602. }
  2603. #endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
  2604. #endif /* CONFIG_STACKTRACE */
  2605. static inline void
  2606. func_repeats_set_delta_ts(struct func_repeats_entry *entry,
  2607. unsigned long long delta)
  2608. {
  2609. entry->bottom_delta_ts = delta & U32_MAX;
  2610. entry->top_delta_ts = (delta >> 32);
  2611. }
  2612. void trace_last_func_repeats(struct trace_array *tr,
  2613. struct trace_func_repeats *last_info,
  2614. unsigned int trace_ctx)
  2615. {
  2616. struct trace_buffer *buffer = tr->array_buffer.buffer;
  2617. struct func_repeats_entry *entry;
  2618. struct ring_buffer_event *event;
  2619. u64 delta;
  2620. event = __trace_buffer_lock_reserve(buffer, TRACE_FUNC_REPEATS,
  2621. sizeof(*entry), trace_ctx);
  2622. if (!event)
  2623. return;
  2624. delta = ring_buffer_event_time_stamp(buffer, event) -
  2625. last_info->ts_last_call;
  2626. entry = ring_buffer_event_data(event);
  2627. entry->ip = last_info->ip;
  2628. entry->parent_ip = last_info->parent_ip;
  2629. entry->count = last_info->count;
  2630. func_repeats_set_delta_ts(entry, delta);
  2631. __buffer_unlock_commit(buffer, event);
  2632. }
  2633. /* created for use with alloc_percpu */
  2634. struct trace_buffer_struct {
  2635. int nesting;
  2636. char buffer[4][TRACE_BUF_SIZE];
  2637. };
  2638. static struct trace_buffer_struct __percpu *trace_percpu_buffer;
  2639. /*
  2640. * This allows for lockless recording. If we're nested too deeply, then
  2641. * this returns NULL.
  2642. */
  2643. static char *get_trace_buf(void)
  2644. {
  2645. struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
  2646. if (!trace_percpu_buffer || buffer->nesting >= 4)
  2647. return NULL;
  2648. buffer->nesting++;
  2649. /* Interrupts must see nesting incremented before we use the buffer */
  2650. barrier();
  2651. return &buffer->buffer[buffer->nesting - 1][0];
  2652. }
  2653. static void put_trace_buf(void)
  2654. {
  2655. /* Don't let the decrement of nesting leak before this */
  2656. barrier();
  2657. this_cpu_dec(trace_percpu_buffer->nesting);
  2658. }
  2659. static int alloc_percpu_trace_buffer(void)
  2660. {
  2661. struct trace_buffer_struct __percpu *buffers;
  2662. if (trace_percpu_buffer)
  2663. return 0;
  2664. buffers = alloc_percpu(struct trace_buffer_struct);
  2665. if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer"))
  2666. return -ENOMEM;
  2667. trace_percpu_buffer = buffers;
  2668. return 0;
  2669. }
  2670. static int buffers_allocated;
  2671. void trace_printk_init_buffers(void)
  2672. {
  2673. if (buffers_allocated)
  2674. return;
  2675. if (alloc_percpu_trace_buffer())
  2676. return;
  2677. /* trace_printk() is for debug use only. Don't use it in production. */
  2678. pr_warn("\n");
  2679. pr_warn("**********************************************************\n");
  2680. pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  2681. pr_warn("** **\n");
  2682. pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
  2683. pr_warn("** **\n");
  2684. pr_warn("** This means that this is a DEBUG kernel and it is **\n");
  2685. pr_warn("** unsafe for production use. **\n");
  2686. pr_warn("** **\n");
  2687. pr_warn("** If you see this message and you are not debugging **\n");
  2688. pr_warn("** the kernel, report this immediately to your vendor! **\n");
  2689. pr_warn("** **\n");
  2690. pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  2691. pr_warn("**********************************************************\n");
  2692. /* Expand the buffers to set size */
  2693. tracing_update_buffers(&global_trace);
  2694. buffers_allocated = 1;
  2695. /*
  2696. * trace_printk_init_buffers() can be called by modules.
  2697. * If that happens, then we need to start cmdline recording
  2698. * directly here. If the global_trace.buffer is already
  2699. * allocated here, then this was called by module code.
  2700. */
  2701. if (global_trace.array_buffer.buffer)
  2702. tracing_start_cmdline_record();
  2703. }
  2704. EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
  2705. void trace_printk_start_comm(void)
  2706. {
  2707. /* Start tracing comms if trace printk is set */
  2708. if (!buffers_allocated)
  2709. return;
  2710. tracing_start_cmdline_record();
  2711. }
  2712. static void trace_printk_start_stop_comm(int enabled)
  2713. {
  2714. if (!buffers_allocated)
  2715. return;
  2716. if (enabled)
  2717. tracing_start_cmdline_record();
  2718. else
  2719. tracing_stop_cmdline_record();
  2720. }
  2721. /**
  2722. * trace_vbprintk - write binary msg to tracing buffer
  2723. * @ip: The address of the caller
  2724. * @fmt: The string format to write to the buffer
  2725. * @args: Arguments for @fmt
  2726. */
  2727. int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
  2728. {
  2729. struct trace_event_call *call = &event_bprint;
  2730. struct ring_buffer_event *event;
  2731. struct trace_buffer *buffer;
  2732. struct trace_array *tr = READ_ONCE(printk_trace);
  2733. struct bprint_entry *entry;
  2734. unsigned int trace_ctx;
  2735. char *tbuffer;
  2736. int len = 0, size;
  2737. if (!printk_binsafe(tr))
  2738. return trace_vprintk(ip, fmt, args);
  2739. if (unlikely(tracing_selftest_running || tracing_disabled))
  2740. return 0;
  2741. /* Don't pollute graph traces with trace_vprintk internals */
  2742. pause_graph_tracing();
  2743. trace_ctx = tracing_gen_ctx();
  2744. preempt_disable_notrace();
  2745. tbuffer = get_trace_buf();
  2746. if (!tbuffer) {
  2747. len = 0;
  2748. goto out_nobuffer;
  2749. }
  2750. len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
  2751. if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
  2752. goto out_put;
  2753. size = sizeof(*entry) + sizeof(u32) * len;
  2754. buffer = tr->array_buffer.buffer;
  2755. ring_buffer_nest_start(buffer);
  2756. event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
  2757. trace_ctx);
  2758. if (!event)
  2759. goto out;
  2760. entry = ring_buffer_event_data(event);
  2761. entry->ip = ip;
  2762. entry->fmt = fmt;
  2763. memcpy(entry->buf, tbuffer, sizeof(u32) * len);
  2764. if (!call_filter_check_discard(call, entry, buffer, event)) {
  2765. __buffer_unlock_commit(buffer, event);
  2766. ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL);
  2767. }
  2768. out:
  2769. ring_buffer_nest_end(buffer);
  2770. out_put:
  2771. put_trace_buf();
  2772. out_nobuffer:
  2773. preempt_enable_notrace();
  2774. unpause_graph_tracing();
  2775. return len;
  2776. }
  2777. EXPORT_SYMBOL_GPL(trace_vbprintk);
  2778. static __printf(3, 0)
  2779. int __trace_array_vprintk(struct trace_buffer *buffer,
  2780. unsigned long ip, const char *fmt, va_list args)
  2781. {
  2782. struct trace_event_call *call = &event_print;
  2783. struct ring_buffer_event *event;
  2784. int len = 0, size;
  2785. struct print_entry *entry;
  2786. unsigned int trace_ctx;
  2787. char *tbuffer;
  2788. if (tracing_disabled)
  2789. return 0;
  2790. /* Don't pollute graph traces with trace_vprintk internals */
  2791. pause_graph_tracing();
  2792. trace_ctx = tracing_gen_ctx();
  2793. preempt_disable_notrace();
  2794. tbuffer = get_trace_buf();
  2795. if (!tbuffer) {
  2796. len = 0;
  2797. goto out_nobuffer;
  2798. }
  2799. len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
  2800. size = sizeof(*entry) + len + 1;
  2801. ring_buffer_nest_start(buffer);
  2802. event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  2803. trace_ctx);
  2804. if (!event)
  2805. goto out;
  2806. entry = ring_buffer_event_data(event);
  2807. entry->ip = ip;
  2808. memcpy(&entry->buf, tbuffer, len + 1);
  2809. if (!call_filter_check_discard(call, entry, buffer, event)) {
  2810. __buffer_unlock_commit(buffer, event);
  2811. ftrace_trace_stack(printk_trace, buffer, trace_ctx, 6, NULL);
  2812. }
  2813. out:
  2814. ring_buffer_nest_end(buffer);
  2815. put_trace_buf();
  2816. out_nobuffer:
  2817. preempt_enable_notrace();
  2818. unpause_graph_tracing();
  2819. return len;
  2820. }
  2821. int trace_array_vprintk(struct trace_array *tr,
  2822. unsigned long ip, const char *fmt, va_list args)
  2823. {
  2824. if (tracing_selftest_running && tr == &global_trace)
  2825. return 0;
  2826. return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
  2827. }
  2828. /**
  2829. * trace_array_printk - Print a message to a specific instance
  2830. * @tr: The instance trace_array descriptor
  2831. * @ip: The instruction pointer that this is called from.
  2832. * @fmt: The format to print (printf format)
  2833. *
  2834. * If a subsystem sets up its own instance, they have the right to
  2835. * printk strings into their tracing instance buffer using this
  2836. * function. Note, this function will not write into the top level
  2837. * buffer (use trace_printk() for that), as writing into the top level
  2838. * buffer should only have events that can be individually disabled.
  2839. * trace_printk() is only used for debugging a kernel, and should not
  2840. * be ever incorporated in normal use.
  2841. *
  2842. * trace_array_printk() can be used, as it will not add noise to the
  2843. * top level tracing buffer.
  2844. *
  2845. * Note, trace_array_init_printk() must be called on @tr before this
  2846. * can be used.
  2847. */
  2848. int trace_array_printk(struct trace_array *tr,
  2849. unsigned long ip, const char *fmt, ...)
  2850. {
  2851. int ret;
  2852. va_list ap;
  2853. if (!tr)
  2854. return -ENOENT;
  2855. /* This is only allowed for created instances */
  2856. if (tr == &global_trace)
  2857. return 0;
  2858. if (!(tr->trace_flags & TRACE_ITER_PRINTK))
  2859. return 0;
  2860. va_start(ap, fmt);
  2861. ret = trace_array_vprintk(tr, ip, fmt, ap);
  2862. va_end(ap);
  2863. return ret;
  2864. }
  2865. EXPORT_SYMBOL_GPL(trace_array_printk);
  2866. /**
  2867. * trace_array_init_printk - Initialize buffers for trace_array_printk()
  2868. * @tr: The trace array to initialize the buffers for
  2869. *
  2870. * As trace_array_printk() only writes into instances, they are OK to
  2871. * have in the kernel (unlike trace_printk()). This needs to be called
  2872. * before trace_array_printk() can be used on a trace_array.
  2873. */
  2874. int trace_array_init_printk(struct trace_array *tr)
  2875. {
  2876. if (!tr)
  2877. return -ENOENT;
  2878. /* This is only allowed for created instances */
  2879. if (tr == &global_trace)
  2880. return -EINVAL;
  2881. return alloc_percpu_trace_buffer();
  2882. }
  2883. EXPORT_SYMBOL_GPL(trace_array_init_printk);
  2884. int trace_array_printk_buf(struct trace_buffer *buffer,
  2885. unsigned long ip, const char *fmt, ...)
  2886. {
  2887. int ret;
  2888. va_list ap;
  2889. if (!(printk_trace->trace_flags & TRACE_ITER_PRINTK))
  2890. return 0;
  2891. va_start(ap, fmt);
  2892. ret = __trace_array_vprintk(buffer, ip, fmt, ap);
  2893. va_end(ap);
  2894. return ret;
  2895. }
  2896. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  2897. {
  2898. return trace_array_vprintk(printk_trace, ip, fmt, args);
  2899. }
  2900. EXPORT_SYMBOL_GPL(trace_vprintk);
  2901. static void trace_iterator_increment(struct trace_iterator *iter)
  2902. {
  2903. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
  2904. iter->idx++;
  2905. if (buf_iter)
  2906. ring_buffer_iter_advance(buf_iter);
  2907. }
  2908. static struct trace_entry *
  2909. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
  2910. unsigned long *lost_events)
  2911. {
  2912. struct ring_buffer_event *event;
  2913. struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
  2914. if (buf_iter) {
  2915. event = ring_buffer_iter_peek(buf_iter, ts);
  2916. if (lost_events)
  2917. *lost_events = ring_buffer_iter_dropped(buf_iter) ?
  2918. (unsigned long)-1 : 0;
  2919. } else {
  2920. event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
  2921. lost_events);
  2922. }
  2923. if (event) {
  2924. iter->ent_size = ring_buffer_event_length(event);
  2925. return ring_buffer_event_data(event);
  2926. }
  2927. iter->ent_size = 0;
  2928. return NULL;
  2929. }
  2930. static struct trace_entry *
  2931. __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
  2932. unsigned long *missing_events, u64 *ent_ts)
  2933. {
  2934. struct trace_buffer *buffer = iter->array_buffer->buffer;
  2935. struct trace_entry *ent, *next = NULL;
  2936. unsigned long lost_events = 0, next_lost = 0;
  2937. int cpu_file = iter->cpu_file;
  2938. u64 next_ts = 0, ts;
  2939. int next_cpu = -1;
  2940. int next_size = 0;
  2941. int cpu;
  2942. /*
  2943. * If we are in a per_cpu trace file, don't bother by iterating over
  2944. * all cpu and peek directly.
  2945. */
  2946. if (cpu_file > RING_BUFFER_ALL_CPUS) {
  2947. if (ring_buffer_empty_cpu(buffer, cpu_file))
  2948. return NULL;
  2949. ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
  2950. if (ent_cpu)
  2951. *ent_cpu = cpu_file;
  2952. return ent;
  2953. }
  2954. for_each_tracing_cpu(cpu) {
  2955. if (ring_buffer_empty_cpu(buffer, cpu))
  2956. continue;
  2957. ent = peek_next_entry(iter, cpu, &ts, &lost_events);
  2958. /*
  2959. * Pick the entry with the smallest timestamp:
  2960. */
  2961. if (ent && (!next || ts < next_ts)) {
  2962. next = ent;
  2963. next_cpu = cpu;
  2964. next_ts = ts;
  2965. next_lost = lost_events;
  2966. next_size = iter->ent_size;
  2967. }
  2968. }
  2969. iter->ent_size = next_size;
  2970. if (ent_cpu)
  2971. *ent_cpu = next_cpu;
  2972. if (ent_ts)
  2973. *ent_ts = next_ts;
  2974. if (missing_events)
  2975. *missing_events = next_lost;
  2976. return next;
  2977. }
  2978. #define STATIC_FMT_BUF_SIZE 128
  2979. static char static_fmt_buf[STATIC_FMT_BUF_SIZE];
  2980. char *trace_iter_expand_format(struct trace_iterator *iter)
  2981. {
  2982. char *tmp;
  2983. /*
  2984. * iter->tr is NULL when used with tp_printk, which makes
  2985. * this get called where it is not safe to call krealloc().
  2986. */
  2987. if (!iter->tr || iter->fmt == static_fmt_buf)
  2988. return NULL;
  2989. tmp = krealloc(iter->fmt, iter->fmt_size + STATIC_FMT_BUF_SIZE,
  2990. GFP_KERNEL);
  2991. if (tmp) {
  2992. iter->fmt_size += STATIC_FMT_BUF_SIZE;
  2993. iter->fmt = tmp;
  2994. }
  2995. return tmp;
  2996. }
  2997. /* Returns true if the string is safe to dereference from an event */
  2998. static bool trace_safe_str(struct trace_iterator *iter, const char *str)
  2999. {
  3000. unsigned long addr = (unsigned long)str;
  3001. struct trace_event *trace_event;
  3002. struct trace_event_call *event;
  3003. /* OK if part of the event data */
  3004. if ((addr >= (unsigned long)iter->ent) &&
  3005. (addr < (unsigned long)iter->ent + iter->ent_size))
  3006. return true;
  3007. /* OK if part of the temp seq buffer */
  3008. if ((addr >= (unsigned long)iter->tmp_seq.buffer) &&
  3009. (addr < (unsigned long)iter->tmp_seq.buffer + TRACE_SEQ_BUFFER_SIZE))
  3010. return true;
  3011. /* Core rodata can not be freed */
  3012. if (is_kernel_rodata(addr))
  3013. return true;
  3014. if (trace_is_tracepoint_string(str))
  3015. return true;
  3016. /*
  3017. * Now this could be a module event, referencing core module
  3018. * data, which is OK.
  3019. */
  3020. if (!iter->ent)
  3021. return false;
  3022. trace_event = ftrace_find_event(iter->ent->type);
  3023. if (!trace_event)
  3024. return false;
  3025. event = container_of(trace_event, struct trace_event_call, event);
  3026. if ((event->flags & TRACE_EVENT_FL_DYNAMIC) || !event->module)
  3027. return false;
  3028. /* Would rather have rodata, but this will suffice */
  3029. if (within_module_core(addr, event->module))
  3030. return true;
  3031. return false;
  3032. }
  3033. /**
  3034. * ignore_event - Check dereferenced fields while writing to the seq buffer
  3035. * @iter: The iterator that holds the seq buffer and the event being printed
  3036. *
  3037. * At boot up, test_event_printk() will flag any event that dereferences
  3038. * a string with "%s" that does exist in the ring buffer. It may still
  3039. * be valid, as the string may point to a static string in the kernel
  3040. * rodata that never gets freed. But if the string pointer is pointing
  3041. * to something that was allocated, there's a chance that it can be freed
  3042. * by the time the user reads the trace. This would cause a bad memory
  3043. * access by the kernel and possibly crash the system.
  3044. *
  3045. * This function will check if the event has any fields flagged as needing
  3046. * to be checked at runtime and perform those checks.
  3047. *
  3048. * If it is found that a field is unsafe, it will write into the @iter->seq
  3049. * a message stating what was found to be unsafe.
  3050. *
  3051. * @return: true if the event is unsafe and should be ignored,
  3052. * false otherwise.
  3053. */
  3054. bool ignore_event(struct trace_iterator *iter)
  3055. {
  3056. struct ftrace_event_field *field;
  3057. struct trace_event *trace_event;
  3058. struct trace_event_call *event;
  3059. struct list_head *head;
  3060. struct trace_seq *seq;
  3061. const void *ptr;
  3062. trace_event = ftrace_find_event(iter->ent->type);
  3063. seq = &iter->seq;
  3064. if (!trace_event) {
  3065. trace_seq_printf(seq, "EVENT ID %d NOT FOUND?\n", iter->ent->type);
  3066. return true;
  3067. }
  3068. event = container_of(trace_event, struct trace_event_call, event);
  3069. if (!(event->flags & TRACE_EVENT_FL_TEST_STR))
  3070. return false;
  3071. head = trace_get_fields(event);
  3072. if (!head) {
  3073. trace_seq_printf(seq, "FIELDS FOR EVENT '%s' NOT FOUND?\n",
  3074. trace_event_name(event));
  3075. return true;
  3076. }
  3077. /* Offsets are from the iter->ent that points to the raw event */
  3078. ptr = iter->ent;
  3079. list_for_each_entry(field, head, link) {
  3080. const char *str;
  3081. bool good;
  3082. if (!field->needs_test)
  3083. continue;
  3084. str = *(const char **)(ptr + field->offset);
  3085. good = trace_safe_str(iter, str);
  3086. /*
  3087. * If you hit this warning, it is likely that the
  3088. * trace event in question used %s on a string that
  3089. * was saved at the time of the event, but may not be
  3090. * around when the trace is read. Use __string(),
  3091. * __assign_str() and __get_str() helpers in the TRACE_EVENT()
  3092. * instead. See samples/trace_events/trace-events-sample.h
  3093. * for reference.
  3094. */
  3095. if (WARN_ONCE(!good, "event '%s' has unsafe pointer field '%s'",
  3096. trace_event_name(event), field->name)) {
  3097. trace_seq_printf(seq, "EVENT %s: HAS UNSAFE POINTER FIELD '%s'\n",
  3098. trace_event_name(event), field->name);
  3099. return true;
  3100. }
  3101. }
  3102. return false;
  3103. }
  3104. const char *trace_event_format(struct trace_iterator *iter, const char *fmt)
  3105. {
  3106. const char *p, *new_fmt;
  3107. char *q;
  3108. if (WARN_ON_ONCE(!fmt))
  3109. return fmt;
  3110. if (!iter->tr || iter->tr->trace_flags & TRACE_ITER_HASH_PTR)
  3111. return fmt;
  3112. p = fmt;
  3113. new_fmt = q = iter->fmt;
  3114. while (*p) {
  3115. if (unlikely(q - new_fmt + 3 > iter->fmt_size)) {
  3116. if (!trace_iter_expand_format(iter))
  3117. return fmt;
  3118. q += iter->fmt - new_fmt;
  3119. new_fmt = iter->fmt;
  3120. }
  3121. *q++ = *p++;
  3122. /* Replace %p with %px */
  3123. if (p[-1] == '%') {
  3124. if (p[0] == '%') {
  3125. *q++ = *p++;
  3126. } else if (p[0] == 'p' && !isalnum(p[1])) {
  3127. *q++ = *p++;
  3128. *q++ = 'x';
  3129. }
  3130. }
  3131. }
  3132. *q = '\0';
  3133. return new_fmt;
  3134. }
  3135. #define STATIC_TEMP_BUF_SIZE 128
  3136. static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4);
  3137. /* Find the next real entry, without updating the iterator itself */
  3138. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  3139. int *ent_cpu, u64 *ent_ts)
  3140. {
  3141. /* __find_next_entry will reset ent_size */
  3142. int ent_size = iter->ent_size;
  3143. struct trace_entry *entry;
  3144. /*
  3145. * If called from ftrace_dump(), then the iter->temp buffer
  3146. * will be the static_temp_buf and not created from kmalloc.
  3147. * If the entry size is greater than the buffer, we can
  3148. * not save it. Just return NULL in that case. This is only
  3149. * used to add markers when two consecutive events' time
  3150. * stamps have a large delta. See trace_print_lat_context()
  3151. */
  3152. if (iter->temp == static_temp_buf &&
  3153. STATIC_TEMP_BUF_SIZE < ent_size)
  3154. return NULL;
  3155. /*
  3156. * The __find_next_entry() may call peek_next_entry(), which may
  3157. * call ring_buffer_peek() that may make the contents of iter->ent
  3158. * undefined. Need to copy iter->ent now.
  3159. */
  3160. if (iter->ent && iter->ent != iter->temp) {
  3161. if ((!iter->temp || iter->temp_size < iter->ent_size) &&
  3162. !WARN_ON_ONCE(iter->temp == static_temp_buf)) {
  3163. void *temp;
  3164. temp = kmalloc(iter->ent_size, GFP_KERNEL);
  3165. if (!temp)
  3166. return NULL;
  3167. kfree(iter->temp);
  3168. iter->temp = temp;
  3169. iter->temp_size = iter->ent_size;
  3170. }
  3171. memcpy(iter->temp, iter->ent, iter->ent_size);
  3172. iter->ent = iter->temp;
  3173. }
  3174. entry = __find_next_entry(iter, ent_cpu, NULL, ent_ts);
  3175. /* Put back the original ent_size */
  3176. iter->ent_size = ent_size;
  3177. return entry;
  3178. }
  3179. /* Find the next real entry, and increment the iterator to the next entry */
  3180. void *trace_find_next_entry_inc(struct trace_iterator *iter)
  3181. {
  3182. iter->ent = __find_next_entry(iter, &iter->cpu,
  3183. &iter->lost_events, &iter->ts);
  3184. if (iter->ent)
  3185. trace_iterator_increment(iter);
  3186. return iter->ent ? iter : NULL;
  3187. }
  3188. static void trace_consume(struct trace_iterator *iter)
  3189. {
  3190. ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
  3191. &iter->lost_events);
  3192. }
  3193. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  3194. {
  3195. struct trace_iterator *iter = m->private;
  3196. int i = (int)*pos;
  3197. void *ent;
  3198. WARN_ON_ONCE(iter->leftover);
  3199. (*pos)++;
  3200. /* can't go backwards */
  3201. if (iter->idx > i)
  3202. return NULL;
  3203. if (iter->idx < 0)
  3204. ent = trace_find_next_entry_inc(iter);
  3205. else
  3206. ent = iter;
  3207. while (ent && iter->idx < i)
  3208. ent = trace_find_next_entry_inc(iter);
  3209. iter->pos = *pos;
  3210. return ent;
  3211. }
  3212. void tracing_iter_reset(struct trace_iterator *iter, int cpu)
  3213. {
  3214. struct ring_buffer_iter *buf_iter;
  3215. unsigned long entries = 0;
  3216. u64 ts;
  3217. per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
  3218. buf_iter = trace_buffer_iter(iter, cpu);
  3219. if (!buf_iter)
  3220. return;
  3221. ring_buffer_iter_reset(buf_iter);
  3222. /*
  3223. * We could have the case with the max latency tracers
  3224. * that a reset never took place on a cpu. This is evident
  3225. * by the timestamp being before the start of the buffer.
  3226. */
  3227. while (ring_buffer_iter_peek(buf_iter, &ts)) {
  3228. if (ts >= iter->array_buffer->time_start)
  3229. break;
  3230. entries++;
  3231. ring_buffer_iter_advance(buf_iter);
  3232. /* This could be a big loop */
  3233. cond_resched();
  3234. }
  3235. per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
  3236. }
  3237. /*
  3238. * The current tracer is copied to avoid a global locking
  3239. * all around.
  3240. */
  3241. static void *s_start(struct seq_file *m, loff_t *pos)
  3242. {
  3243. struct trace_iterator *iter = m->private;
  3244. struct trace_array *tr = iter->tr;
  3245. int cpu_file = iter->cpu_file;
  3246. void *p = NULL;
  3247. loff_t l = 0;
  3248. int cpu;
  3249. mutex_lock(&trace_types_lock);
  3250. if (unlikely(tr->current_trace != iter->trace)) {
  3251. /* Close iter->trace before switching to the new current tracer */
  3252. if (iter->trace->close)
  3253. iter->trace->close(iter);
  3254. iter->trace = tr->current_trace;
  3255. /* Reopen the new current tracer */
  3256. if (iter->trace->open)
  3257. iter->trace->open(iter);
  3258. }
  3259. mutex_unlock(&trace_types_lock);
  3260. #ifdef CONFIG_TRACER_MAX_TRACE
  3261. if (iter->snapshot && iter->trace->use_max_tr)
  3262. return ERR_PTR(-EBUSY);
  3263. #endif
  3264. if (*pos != iter->pos) {
  3265. iter->ent = NULL;
  3266. iter->cpu = 0;
  3267. iter->idx = -1;
  3268. if (cpu_file == RING_BUFFER_ALL_CPUS) {
  3269. for_each_tracing_cpu(cpu)
  3270. tracing_iter_reset(iter, cpu);
  3271. } else
  3272. tracing_iter_reset(iter, cpu_file);
  3273. iter->leftover = 0;
  3274. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  3275. ;
  3276. } else {
  3277. /*
  3278. * If we overflowed the seq_file before, then we want
  3279. * to just reuse the trace_seq buffer again.
  3280. */
  3281. if (iter->leftover)
  3282. p = iter;
  3283. else {
  3284. l = *pos - 1;
  3285. p = s_next(m, p, &l);
  3286. }
  3287. }
  3288. trace_event_read_lock();
  3289. trace_access_lock(cpu_file);
  3290. return p;
  3291. }
  3292. static void s_stop(struct seq_file *m, void *p)
  3293. {
  3294. struct trace_iterator *iter = m->private;
  3295. #ifdef CONFIG_TRACER_MAX_TRACE
  3296. if (iter->snapshot && iter->trace->use_max_tr)
  3297. return;
  3298. #endif
  3299. trace_access_unlock(iter->cpu_file);
  3300. trace_event_read_unlock();
  3301. }
  3302. static void
  3303. get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
  3304. unsigned long *entries, int cpu)
  3305. {
  3306. unsigned long count;
  3307. count = ring_buffer_entries_cpu(buf->buffer, cpu);
  3308. /*
  3309. * If this buffer has skipped entries, then we hold all
  3310. * entries for the trace and we need to ignore the
  3311. * ones before the time stamp.
  3312. */
  3313. if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
  3314. count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
  3315. /* total is the same as the entries */
  3316. *total = count;
  3317. } else
  3318. *total = count +
  3319. ring_buffer_overrun_cpu(buf->buffer, cpu);
  3320. *entries = count;
  3321. }
  3322. static void
  3323. get_total_entries(struct array_buffer *buf,
  3324. unsigned long *total, unsigned long *entries)
  3325. {
  3326. unsigned long t, e;
  3327. int cpu;
  3328. *total = 0;
  3329. *entries = 0;
  3330. for_each_tracing_cpu(cpu) {
  3331. get_total_entries_cpu(buf, &t, &e, cpu);
  3332. *total += t;
  3333. *entries += e;
  3334. }
  3335. }
  3336. unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
  3337. {
  3338. unsigned long total, entries;
  3339. if (!tr)
  3340. tr = &global_trace;
  3341. get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
  3342. return entries;
  3343. }
  3344. unsigned long trace_total_entries(struct trace_array *tr)
  3345. {
  3346. unsigned long total, entries;
  3347. if (!tr)
  3348. tr = &global_trace;
  3349. get_total_entries(&tr->array_buffer, &total, &entries);
  3350. return entries;
  3351. }
  3352. static void print_lat_help_header(struct seq_file *m)
  3353. {
  3354. seq_puts(m, "# _------=> CPU# \n"
  3355. "# / _-----=> irqs-off/BH-disabled\n"
  3356. "# | / _----=> need-resched \n"
  3357. "# || / _---=> hardirq/softirq \n"
  3358. "# ||| / _--=> preempt-depth \n"
  3359. "# |||| / _-=> migrate-disable \n"
  3360. "# ||||| / delay \n"
  3361. "# cmd pid |||||| time | caller \n"
  3362. "# \\ / |||||| \\ | / \n");
  3363. }
  3364. static void print_event_info(struct array_buffer *buf, struct seq_file *m)
  3365. {
  3366. unsigned long total;
  3367. unsigned long entries;
  3368. get_total_entries(buf, &total, &entries);
  3369. seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
  3370. entries, total, num_online_cpus());
  3371. seq_puts(m, "#\n");
  3372. }
  3373. static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
  3374. unsigned int flags)
  3375. {
  3376. bool tgid = flags & TRACE_ITER_RECORD_TGID;
  3377. print_event_info(buf, m);
  3378. seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? " TGID " : "");
  3379. seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
  3380. }
  3381. static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
  3382. unsigned int flags)
  3383. {
  3384. bool tgid = flags & TRACE_ITER_RECORD_TGID;
  3385. static const char space[] = " ";
  3386. int prec = tgid ? 12 : 2;
  3387. print_event_info(buf, m);
  3388. seq_printf(m, "# %.*s _-----=> irqs-off/BH-disabled\n", prec, space);
  3389. seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
  3390. seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
  3391. seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
  3392. seq_printf(m, "# %.*s||| / _-=> migrate-disable\n", prec, space);
  3393. seq_printf(m, "# %.*s|||| / delay\n", prec, space);
  3394. seq_printf(m, "# TASK-PID %.*s CPU# ||||| TIMESTAMP FUNCTION\n", prec, " TGID ");
  3395. seq_printf(m, "# | | %.*s | ||||| | |\n", prec, " | ");
  3396. }
  3397. void
  3398. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  3399. {
  3400. unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
  3401. struct array_buffer *buf = iter->array_buffer;
  3402. struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
  3403. struct tracer *type = iter->trace;
  3404. unsigned long entries;
  3405. unsigned long total;
  3406. const char *name = type->name;
  3407. get_total_entries(buf, &total, &entries);
  3408. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  3409. name, init_utsname()->release);
  3410. seq_puts(m, "# -----------------------------------"
  3411. "---------------------------------\n");
  3412. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  3413. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  3414. nsecs_to_usecs(data->saved_latency),
  3415. entries,
  3416. total,
  3417. buf->cpu,
  3418. preempt_model_none() ? "server" :
  3419. preempt_model_voluntary() ? "desktop" :
  3420. preempt_model_full() ? "preempt" :
  3421. preempt_model_rt() ? "preempt_rt" :
  3422. "unknown",
  3423. /* These are reserved for later use */
  3424. 0, 0, 0, 0);
  3425. #ifdef CONFIG_SMP
  3426. seq_printf(m, " #P:%d)\n", num_online_cpus());
  3427. #else
  3428. seq_puts(m, ")\n");
  3429. #endif
  3430. seq_puts(m, "# -----------------\n");
  3431. seq_printf(m, "# | task: %.16s-%d "
  3432. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  3433. data->comm, data->pid,
  3434. from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
  3435. data->policy, data->rt_priority);
  3436. seq_puts(m, "# -----------------\n");
  3437. if (data->critical_start) {
  3438. seq_puts(m, "# => started at: ");
  3439. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  3440. trace_print_seq(m, &iter->seq);
  3441. seq_puts(m, "\n# => ended at: ");
  3442. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  3443. trace_print_seq(m, &iter->seq);
  3444. seq_puts(m, "\n#\n");
  3445. }
  3446. seq_puts(m, "#\n");
  3447. }
  3448. static void test_cpu_buff_start(struct trace_iterator *iter)
  3449. {
  3450. struct trace_seq *s = &iter->seq;
  3451. struct trace_array *tr = iter->tr;
  3452. if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
  3453. return;
  3454. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  3455. return;
  3456. if (cpumask_available(iter->started) &&
  3457. cpumask_test_cpu(iter->cpu, iter->started))
  3458. return;
  3459. if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
  3460. return;
  3461. if (cpumask_available(iter->started))
  3462. cpumask_set_cpu(iter->cpu, iter->started);
  3463. /* Don't print started cpu buffer for the first entry of the trace */
  3464. if (iter->idx > 1)
  3465. trace_seq_printf(s, "##### CPU %u buffer started ####\n",
  3466. iter->cpu);
  3467. }
  3468. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  3469. {
  3470. struct trace_array *tr = iter->tr;
  3471. struct trace_seq *s = &iter->seq;
  3472. unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
  3473. struct trace_entry *entry;
  3474. struct trace_event *event;
  3475. entry = iter->ent;
  3476. test_cpu_buff_start(iter);
  3477. event = ftrace_find_event(entry->type);
  3478. if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
  3479. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  3480. trace_print_lat_context(iter);
  3481. else
  3482. trace_print_context(iter);
  3483. }
  3484. if (trace_seq_has_overflowed(s))
  3485. return TRACE_TYPE_PARTIAL_LINE;
  3486. if (event) {
  3487. if (tr->trace_flags & TRACE_ITER_FIELDS)
  3488. return print_event_fields(iter, event);
  3489. /*
  3490. * For TRACE_EVENT() events, the print_fmt is not
  3491. * safe to use if the array has delta offsets
  3492. * Force printing via the fields.
  3493. */
  3494. if ((tr->text_delta || tr->data_delta) &&
  3495. event->type > __TRACE_LAST_TYPE)
  3496. return print_event_fields(iter, event);
  3497. return event->funcs->trace(iter, sym_flags, event);
  3498. }
  3499. trace_seq_printf(s, "Unknown type %d\n", entry->type);
  3500. return trace_handle_return(s);
  3501. }
  3502. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  3503. {
  3504. struct trace_array *tr = iter->tr;
  3505. struct trace_seq *s = &iter->seq;
  3506. struct trace_entry *entry;
  3507. struct trace_event *event;
  3508. entry = iter->ent;
  3509. if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
  3510. trace_seq_printf(s, "%d %d %llu ",
  3511. entry->pid, iter->cpu, iter->ts);
  3512. if (trace_seq_has_overflowed(s))
  3513. return TRACE_TYPE_PARTIAL_LINE;
  3514. event = ftrace_find_event(entry->type);
  3515. if (event)
  3516. return event->funcs->raw(iter, 0, event);
  3517. trace_seq_printf(s, "%d ?\n", entry->type);
  3518. return trace_handle_return(s);
  3519. }
  3520. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  3521. {
  3522. struct trace_array *tr = iter->tr;
  3523. struct trace_seq *s = &iter->seq;
  3524. unsigned char newline = '\n';
  3525. struct trace_entry *entry;
  3526. struct trace_event *event;
  3527. entry = iter->ent;
  3528. if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
  3529. SEQ_PUT_HEX_FIELD(s, entry->pid);
  3530. SEQ_PUT_HEX_FIELD(s, iter->cpu);
  3531. SEQ_PUT_HEX_FIELD(s, iter->ts);
  3532. if (trace_seq_has_overflowed(s))
  3533. return TRACE_TYPE_PARTIAL_LINE;
  3534. }
  3535. event = ftrace_find_event(entry->type);
  3536. if (event) {
  3537. enum print_line_t ret = event->funcs->hex(iter, 0, event);
  3538. if (ret != TRACE_TYPE_HANDLED)
  3539. return ret;
  3540. }
  3541. SEQ_PUT_FIELD(s, newline);
  3542. return trace_handle_return(s);
  3543. }
  3544. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  3545. {
  3546. struct trace_array *tr = iter->tr;
  3547. struct trace_seq *s = &iter->seq;
  3548. struct trace_entry *entry;
  3549. struct trace_event *event;
  3550. entry = iter->ent;
  3551. if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
  3552. SEQ_PUT_FIELD(s, entry->pid);
  3553. SEQ_PUT_FIELD(s, iter->cpu);
  3554. SEQ_PUT_FIELD(s, iter->ts);
  3555. if (trace_seq_has_overflowed(s))
  3556. return TRACE_TYPE_PARTIAL_LINE;
  3557. }
  3558. event = ftrace_find_event(entry->type);
  3559. return event ? event->funcs->binary(iter, 0, event) :
  3560. TRACE_TYPE_HANDLED;
  3561. }
  3562. int trace_empty(struct trace_iterator *iter)
  3563. {
  3564. struct ring_buffer_iter *buf_iter;
  3565. int cpu;
  3566. /* If we are looking at one CPU buffer, only check that one */
  3567. if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
  3568. cpu = iter->cpu_file;
  3569. buf_iter = trace_buffer_iter(iter, cpu);
  3570. if (buf_iter) {
  3571. if (!ring_buffer_iter_empty(buf_iter))
  3572. return 0;
  3573. } else {
  3574. if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
  3575. return 0;
  3576. }
  3577. return 1;
  3578. }
  3579. for_each_tracing_cpu(cpu) {
  3580. buf_iter = trace_buffer_iter(iter, cpu);
  3581. if (buf_iter) {
  3582. if (!ring_buffer_iter_empty(buf_iter))
  3583. return 0;
  3584. } else {
  3585. if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
  3586. return 0;
  3587. }
  3588. }
  3589. return 1;
  3590. }
  3591. /* Called with trace_event_read_lock() held. */
  3592. enum print_line_t print_trace_line(struct trace_iterator *iter)
  3593. {
  3594. struct trace_array *tr = iter->tr;
  3595. unsigned long trace_flags = tr->trace_flags;
  3596. enum print_line_t ret;
  3597. if (iter->lost_events) {
  3598. if (iter->lost_events == (unsigned long)-1)
  3599. trace_seq_printf(&iter->seq, "CPU:%d [LOST EVENTS]\n",
  3600. iter->cpu);
  3601. else
  3602. trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
  3603. iter->cpu, iter->lost_events);
  3604. if (trace_seq_has_overflowed(&iter->seq))
  3605. return TRACE_TYPE_PARTIAL_LINE;
  3606. }
  3607. if (iter->trace && iter->trace->print_line) {
  3608. ret = iter->trace->print_line(iter);
  3609. if (ret != TRACE_TYPE_UNHANDLED)
  3610. return ret;
  3611. }
  3612. if (iter->ent->type == TRACE_BPUTS &&
  3613. trace_flags & TRACE_ITER_PRINTK &&
  3614. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  3615. return trace_print_bputs_msg_only(iter);
  3616. if (iter->ent->type == TRACE_BPRINT &&
  3617. trace_flags & TRACE_ITER_PRINTK &&
  3618. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  3619. return trace_print_bprintk_msg_only(iter);
  3620. if (iter->ent->type == TRACE_PRINT &&
  3621. trace_flags & TRACE_ITER_PRINTK &&
  3622. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  3623. return trace_print_printk_msg_only(iter);
  3624. if (trace_flags & TRACE_ITER_BIN)
  3625. return print_bin_fmt(iter);
  3626. if (trace_flags & TRACE_ITER_HEX)
  3627. return print_hex_fmt(iter);
  3628. if (trace_flags & TRACE_ITER_RAW)
  3629. return print_raw_fmt(iter);
  3630. return print_trace_fmt(iter);
  3631. }
  3632. void trace_latency_header(struct seq_file *m)
  3633. {
  3634. struct trace_iterator *iter = m->private;
  3635. struct trace_array *tr = iter->tr;
  3636. /* print nothing if the buffers are empty */
  3637. if (trace_empty(iter))
  3638. return;
  3639. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  3640. print_trace_header(m, iter);
  3641. if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
  3642. print_lat_help_header(m);
  3643. }
  3644. void trace_default_header(struct seq_file *m)
  3645. {
  3646. struct trace_iterator *iter = m->private;
  3647. struct trace_array *tr = iter->tr;
  3648. unsigned long trace_flags = tr->trace_flags;
  3649. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  3650. return;
  3651. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  3652. /* print nothing if the buffers are empty */
  3653. if (trace_empty(iter))
  3654. return;
  3655. print_trace_header(m, iter);
  3656. if (!(trace_flags & TRACE_ITER_VERBOSE))
  3657. print_lat_help_header(m);
  3658. } else {
  3659. if (!(trace_flags & TRACE_ITER_VERBOSE)) {
  3660. if (trace_flags & TRACE_ITER_IRQ_INFO)
  3661. print_func_help_header_irq(iter->array_buffer,
  3662. m, trace_flags);
  3663. else
  3664. print_func_help_header(iter->array_buffer, m,
  3665. trace_flags);
  3666. }
  3667. }
  3668. }
  3669. static void test_ftrace_alive(struct seq_file *m)
  3670. {
  3671. if (!ftrace_is_dead())
  3672. return;
  3673. seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
  3674. "# MAY BE MISSING FUNCTION EVENTS\n");
  3675. }
  3676. #ifdef CONFIG_TRACER_MAX_TRACE
  3677. static void show_snapshot_main_help(struct seq_file *m)
  3678. {
  3679. seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
  3680. "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
  3681. "# Takes a snapshot of the main buffer.\n"
  3682. "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
  3683. "# (Doesn't have to be '2' works with any number that\n"
  3684. "# is not a '0' or '1')\n");
  3685. }
  3686. static void show_snapshot_percpu_help(struct seq_file *m)
  3687. {
  3688. seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
  3689. #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
  3690. seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
  3691. "# Takes a snapshot of the main buffer for this cpu.\n");
  3692. #else
  3693. seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
  3694. "# Must use main snapshot file to allocate.\n");
  3695. #endif
  3696. seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
  3697. "# (Doesn't have to be '2' works with any number that\n"
  3698. "# is not a '0' or '1')\n");
  3699. }
  3700. static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
  3701. {
  3702. if (iter->tr->allocated_snapshot)
  3703. seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
  3704. else
  3705. seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
  3706. seq_puts(m, "# Snapshot commands:\n");
  3707. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  3708. show_snapshot_main_help(m);
  3709. else
  3710. show_snapshot_percpu_help(m);
  3711. }
  3712. #else
  3713. /* Should never be called */
  3714. static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
  3715. #endif
  3716. static int s_show(struct seq_file *m, void *v)
  3717. {
  3718. struct trace_iterator *iter = v;
  3719. int ret;
  3720. if (iter->ent == NULL) {
  3721. if (iter->tr) {
  3722. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  3723. seq_puts(m, "#\n");
  3724. test_ftrace_alive(m);
  3725. }
  3726. if (iter->snapshot && trace_empty(iter))
  3727. print_snapshot_help(m, iter);
  3728. else if (iter->trace && iter->trace->print_header)
  3729. iter->trace->print_header(m);
  3730. else
  3731. trace_default_header(m);
  3732. } else if (iter->leftover) {
  3733. /*
  3734. * If we filled the seq_file buffer earlier, we
  3735. * want to just show it now.
  3736. */
  3737. ret = trace_print_seq(m, &iter->seq);
  3738. /* ret should this time be zero, but you never know */
  3739. iter->leftover = ret;
  3740. } else {
  3741. ret = print_trace_line(iter);
  3742. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3743. iter->seq.full = 0;
  3744. trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
  3745. }
  3746. ret = trace_print_seq(m, &iter->seq);
  3747. /*
  3748. * If we overflow the seq_file buffer, then it will
  3749. * ask us for this data again at start up.
  3750. * Use that instead.
  3751. * ret is 0 if seq_file write succeeded.
  3752. * -1 otherwise.
  3753. */
  3754. iter->leftover = ret;
  3755. }
  3756. return 0;
  3757. }
  3758. /*
  3759. * Should be used after trace_array_get(), trace_types_lock
  3760. * ensures that i_cdev was already initialized.
  3761. */
  3762. static inline int tracing_get_cpu(struct inode *inode)
  3763. {
  3764. if (inode->i_cdev) /* See trace_create_cpu_file() */
  3765. return (long)inode->i_cdev - 1;
  3766. return RING_BUFFER_ALL_CPUS;
  3767. }
  3768. static const struct seq_operations tracer_seq_ops = {
  3769. .start = s_start,
  3770. .next = s_next,
  3771. .stop = s_stop,
  3772. .show = s_show,
  3773. };
  3774. /*
  3775. * Note, as iter itself can be allocated and freed in different
  3776. * ways, this function is only used to free its content, and not
  3777. * the iterator itself. The only requirement to all the allocations
  3778. * is that it must zero all fields (kzalloc), as freeing works with
  3779. * ethier allocated content or NULL.
  3780. */
  3781. static void free_trace_iter_content(struct trace_iterator *iter)
  3782. {
  3783. /* The fmt is either NULL, allocated or points to static_fmt_buf */
  3784. if (iter->fmt != static_fmt_buf)
  3785. kfree(iter->fmt);
  3786. kfree(iter->temp);
  3787. kfree(iter->buffer_iter);
  3788. mutex_destroy(&iter->mutex);
  3789. free_cpumask_var(iter->started);
  3790. }
  3791. static struct trace_iterator *
  3792. __tracing_open(struct inode *inode, struct file *file, bool snapshot)
  3793. {
  3794. struct trace_array *tr = inode->i_private;
  3795. struct trace_iterator *iter;
  3796. int cpu;
  3797. if (tracing_disabled)
  3798. return ERR_PTR(-ENODEV);
  3799. iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
  3800. if (!iter)
  3801. return ERR_PTR(-ENOMEM);
  3802. iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
  3803. GFP_KERNEL);
  3804. if (!iter->buffer_iter)
  3805. goto release;
  3806. /*
  3807. * trace_find_next_entry() may need to save off iter->ent.
  3808. * It will place it into the iter->temp buffer. As most
  3809. * events are less than 128, allocate a buffer of that size.
  3810. * If one is greater, then trace_find_next_entry() will
  3811. * allocate a new buffer to adjust for the bigger iter->ent.
  3812. * It's not critical if it fails to get allocated here.
  3813. */
  3814. iter->temp = kmalloc(128, GFP_KERNEL);
  3815. if (iter->temp)
  3816. iter->temp_size = 128;
  3817. /*
  3818. * trace_event_printf() may need to modify given format
  3819. * string to replace %p with %px so that it shows real address
  3820. * instead of hash value. However, that is only for the event
  3821. * tracing, other tracer may not need. Defer the allocation
  3822. * until it is needed.
  3823. */
  3824. iter->fmt = NULL;
  3825. iter->fmt_size = 0;
  3826. mutex_lock(&trace_types_lock);
  3827. iter->trace = tr->current_trace;
  3828. if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
  3829. goto fail;
  3830. iter->tr = tr;
  3831. #ifdef CONFIG_TRACER_MAX_TRACE
  3832. /* Currently only the top directory has a snapshot */
  3833. if (tr->current_trace->print_max || snapshot)
  3834. iter->array_buffer = &tr->max_buffer;
  3835. else
  3836. #endif
  3837. iter->array_buffer = &tr->array_buffer;
  3838. iter->snapshot = snapshot;
  3839. iter->pos = -1;
  3840. iter->cpu_file = tracing_get_cpu(inode);
  3841. mutex_init(&iter->mutex);
  3842. /* Notify the tracer early; before we stop tracing. */
  3843. if (iter->trace->open)
  3844. iter->trace->open(iter);
  3845. /* Annotate start of buffers if we had overruns */
  3846. if (ring_buffer_overruns(iter->array_buffer->buffer))
  3847. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  3848. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  3849. if (trace_clocks[tr->clock_id].in_ns)
  3850. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  3851. /*
  3852. * If pause-on-trace is enabled, then stop the trace while
  3853. * dumping, unless this is the "snapshot" file
  3854. */
  3855. if (!iter->snapshot && (tr->trace_flags & TRACE_ITER_PAUSE_ON_TRACE))
  3856. tracing_stop_tr(tr);
  3857. if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
  3858. for_each_tracing_cpu(cpu) {
  3859. iter->buffer_iter[cpu] =
  3860. ring_buffer_read_start(iter->array_buffer->buffer,
  3861. cpu, GFP_KERNEL);
  3862. tracing_iter_reset(iter, cpu);
  3863. }
  3864. } else {
  3865. cpu = iter->cpu_file;
  3866. iter->buffer_iter[cpu] =
  3867. ring_buffer_read_start(iter->array_buffer->buffer,
  3868. cpu, GFP_KERNEL);
  3869. tracing_iter_reset(iter, cpu);
  3870. }
  3871. mutex_unlock(&trace_types_lock);
  3872. return iter;
  3873. fail:
  3874. mutex_unlock(&trace_types_lock);
  3875. free_trace_iter_content(iter);
  3876. release:
  3877. seq_release_private(inode, file);
  3878. return ERR_PTR(-ENOMEM);
  3879. }
  3880. int tracing_open_generic(struct inode *inode, struct file *filp)
  3881. {
  3882. int ret;
  3883. ret = tracing_check_open_get_tr(NULL);
  3884. if (ret)
  3885. return ret;
  3886. filp->private_data = inode->i_private;
  3887. return 0;
  3888. }
  3889. bool tracing_is_disabled(void)
  3890. {
  3891. return (tracing_disabled) ? true: false;
  3892. }
  3893. /*
  3894. * Open and update trace_array ref count.
  3895. * Must have the current trace_array passed to it.
  3896. */
  3897. int tracing_open_generic_tr(struct inode *inode, struct file *filp)
  3898. {
  3899. struct trace_array *tr = inode->i_private;
  3900. int ret;
  3901. ret = tracing_check_open_get_tr(tr);
  3902. if (ret)
  3903. return ret;
  3904. filp->private_data = inode->i_private;
  3905. return 0;
  3906. }
  3907. /*
  3908. * The private pointer of the inode is the trace_event_file.
  3909. * Update the tr ref count associated to it.
  3910. */
  3911. int tracing_open_file_tr(struct inode *inode, struct file *filp)
  3912. {
  3913. struct trace_event_file *file = inode->i_private;
  3914. int ret;
  3915. ret = tracing_check_open_get_tr(file->tr);
  3916. if (ret)
  3917. return ret;
  3918. mutex_lock(&event_mutex);
  3919. /* Fail if the file is marked for removal */
  3920. if (file->flags & EVENT_FILE_FL_FREED) {
  3921. trace_array_put(file->tr);
  3922. ret = -ENODEV;
  3923. } else {
  3924. event_file_get(file);
  3925. }
  3926. mutex_unlock(&event_mutex);
  3927. if (ret)
  3928. return ret;
  3929. filp->private_data = inode->i_private;
  3930. return 0;
  3931. }
  3932. int tracing_release_file_tr(struct inode *inode, struct file *filp)
  3933. {
  3934. struct trace_event_file *file = inode->i_private;
  3935. trace_array_put(file->tr);
  3936. event_file_put(file);
  3937. return 0;
  3938. }
  3939. int tracing_single_release_file_tr(struct inode *inode, struct file *filp)
  3940. {
  3941. tracing_release_file_tr(inode, filp);
  3942. return single_release(inode, filp);
  3943. }
  3944. static int tracing_mark_open(struct inode *inode, struct file *filp)
  3945. {
  3946. stream_open(inode, filp);
  3947. return tracing_open_generic_tr(inode, filp);
  3948. }
  3949. static int tracing_release(struct inode *inode, struct file *file)
  3950. {
  3951. struct trace_array *tr = inode->i_private;
  3952. struct seq_file *m = file->private_data;
  3953. struct trace_iterator *iter;
  3954. int cpu;
  3955. if (!(file->f_mode & FMODE_READ)) {
  3956. trace_array_put(tr);
  3957. return 0;
  3958. }
  3959. /* Writes do not use seq_file */
  3960. iter = m->private;
  3961. mutex_lock(&trace_types_lock);
  3962. for_each_tracing_cpu(cpu) {
  3963. if (iter->buffer_iter[cpu])
  3964. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  3965. }
  3966. if (iter->trace && iter->trace->close)
  3967. iter->trace->close(iter);
  3968. if (!iter->snapshot && tr->stop_count)
  3969. /* reenable tracing if it was previously enabled */
  3970. tracing_start_tr(tr);
  3971. __trace_array_put(tr);
  3972. mutex_unlock(&trace_types_lock);
  3973. free_trace_iter_content(iter);
  3974. seq_release_private(inode, file);
  3975. return 0;
  3976. }
  3977. int tracing_release_generic_tr(struct inode *inode, struct file *file)
  3978. {
  3979. struct trace_array *tr = inode->i_private;
  3980. trace_array_put(tr);
  3981. return 0;
  3982. }
  3983. static int tracing_single_release_tr(struct inode *inode, struct file *file)
  3984. {
  3985. struct trace_array *tr = inode->i_private;
  3986. trace_array_put(tr);
  3987. return single_release(inode, file);
  3988. }
  3989. static int tracing_open(struct inode *inode, struct file *file)
  3990. {
  3991. struct trace_array *tr = inode->i_private;
  3992. struct trace_iterator *iter;
  3993. int ret;
  3994. ret = tracing_check_open_get_tr(tr);
  3995. if (ret)
  3996. return ret;
  3997. /* If this file was open for write, then erase contents */
  3998. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  3999. int cpu = tracing_get_cpu(inode);
  4000. struct array_buffer *trace_buf = &tr->array_buffer;
  4001. #ifdef CONFIG_TRACER_MAX_TRACE
  4002. if (tr->current_trace->print_max)
  4003. trace_buf = &tr->max_buffer;
  4004. #endif
  4005. if (cpu == RING_BUFFER_ALL_CPUS)
  4006. tracing_reset_online_cpus(trace_buf);
  4007. else
  4008. tracing_reset_cpu(trace_buf, cpu);
  4009. }
  4010. if (file->f_mode & FMODE_READ) {
  4011. iter = __tracing_open(inode, file, false);
  4012. if (IS_ERR(iter))
  4013. ret = PTR_ERR(iter);
  4014. else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
  4015. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  4016. }
  4017. if (ret < 0)
  4018. trace_array_put(tr);
  4019. return ret;
  4020. }
  4021. /*
  4022. * Some tracers are not suitable for instance buffers.
  4023. * A tracer is always available for the global array (toplevel)
  4024. * or if it explicitly states that it is.
  4025. */
  4026. static bool
  4027. trace_ok_for_array(struct tracer *t, struct trace_array *tr)
  4028. {
  4029. #ifdef CONFIG_TRACER_SNAPSHOT
  4030. /* arrays with mapped buffer range do not have snapshots */
  4031. if (tr->range_addr_start && t->use_max_tr)
  4032. return false;
  4033. #endif
  4034. return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
  4035. }
  4036. /* Find the next tracer that this trace array may use */
  4037. static struct tracer *
  4038. get_tracer_for_array(struct trace_array *tr, struct tracer *t)
  4039. {
  4040. while (t && !trace_ok_for_array(t, tr))
  4041. t = t->next;
  4042. return t;
  4043. }
  4044. static void *
  4045. t_next(struct seq_file *m, void *v, loff_t *pos)
  4046. {
  4047. struct trace_array *tr = m->private;
  4048. struct tracer *t = v;
  4049. (*pos)++;
  4050. if (t)
  4051. t = get_tracer_for_array(tr, t->next);
  4052. return t;
  4053. }
  4054. static void *t_start(struct seq_file *m, loff_t *pos)
  4055. {
  4056. struct trace_array *tr = m->private;
  4057. struct tracer *t;
  4058. loff_t l = 0;
  4059. mutex_lock(&trace_types_lock);
  4060. t = get_tracer_for_array(tr, trace_types);
  4061. for (; t && l < *pos; t = t_next(m, t, &l))
  4062. ;
  4063. return t;
  4064. }
  4065. static void t_stop(struct seq_file *m, void *p)
  4066. {
  4067. mutex_unlock(&trace_types_lock);
  4068. }
  4069. static int t_show(struct seq_file *m, void *v)
  4070. {
  4071. struct tracer *t = v;
  4072. if (!t)
  4073. return 0;
  4074. seq_puts(m, t->name);
  4075. if (t->next)
  4076. seq_putc(m, ' ');
  4077. else
  4078. seq_putc(m, '\n');
  4079. return 0;
  4080. }
  4081. static const struct seq_operations show_traces_seq_ops = {
  4082. .start = t_start,
  4083. .next = t_next,
  4084. .stop = t_stop,
  4085. .show = t_show,
  4086. };
  4087. static int show_traces_open(struct inode *inode, struct file *file)
  4088. {
  4089. struct trace_array *tr = inode->i_private;
  4090. struct seq_file *m;
  4091. int ret;
  4092. ret = tracing_check_open_get_tr(tr);
  4093. if (ret)
  4094. return ret;
  4095. ret = seq_open(file, &show_traces_seq_ops);
  4096. if (ret) {
  4097. trace_array_put(tr);
  4098. return ret;
  4099. }
  4100. m = file->private_data;
  4101. m->private = tr;
  4102. return 0;
  4103. }
  4104. static int tracing_seq_release(struct inode *inode, struct file *file)
  4105. {
  4106. struct trace_array *tr = inode->i_private;
  4107. trace_array_put(tr);
  4108. return seq_release(inode, file);
  4109. }
  4110. static ssize_t
  4111. tracing_write_stub(struct file *filp, const char __user *ubuf,
  4112. size_t count, loff_t *ppos)
  4113. {
  4114. return count;
  4115. }
  4116. loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
  4117. {
  4118. int ret;
  4119. if (file->f_mode & FMODE_READ)
  4120. ret = seq_lseek(file, offset, whence);
  4121. else
  4122. file->f_pos = ret = 0;
  4123. return ret;
  4124. }
  4125. static const struct file_operations tracing_fops = {
  4126. .open = tracing_open,
  4127. .read = seq_read,
  4128. .read_iter = seq_read_iter,
  4129. .splice_read = copy_splice_read,
  4130. .write = tracing_write_stub,
  4131. .llseek = tracing_lseek,
  4132. .release = tracing_release,
  4133. };
  4134. static const struct file_operations show_traces_fops = {
  4135. .open = show_traces_open,
  4136. .read = seq_read,
  4137. .llseek = seq_lseek,
  4138. .release = tracing_seq_release,
  4139. };
  4140. static ssize_t
  4141. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  4142. size_t count, loff_t *ppos)
  4143. {
  4144. struct trace_array *tr = file_inode(filp)->i_private;
  4145. char *mask_str;
  4146. int len;
  4147. len = snprintf(NULL, 0, "%*pb\n",
  4148. cpumask_pr_args(tr->tracing_cpumask)) + 1;
  4149. mask_str = kmalloc(len, GFP_KERNEL);
  4150. if (!mask_str)
  4151. return -ENOMEM;
  4152. len = snprintf(mask_str, len, "%*pb\n",
  4153. cpumask_pr_args(tr->tracing_cpumask));
  4154. if (len >= count) {
  4155. count = -EINVAL;
  4156. goto out_err;
  4157. }
  4158. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
  4159. out_err:
  4160. kfree(mask_str);
  4161. return count;
  4162. }
  4163. int tracing_set_cpumask(struct trace_array *tr,
  4164. cpumask_var_t tracing_cpumask_new)
  4165. {
  4166. int cpu;
  4167. if (!tr)
  4168. return -EINVAL;
  4169. local_irq_disable();
  4170. arch_spin_lock(&tr->max_lock);
  4171. for_each_tracing_cpu(cpu) {
  4172. /*
  4173. * Increase/decrease the disabled counter if we are
  4174. * about to flip a bit in the cpumask:
  4175. */
  4176. if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
  4177. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  4178. atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
  4179. ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
  4180. #ifdef CONFIG_TRACER_MAX_TRACE
  4181. ring_buffer_record_disable_cpu(tr->max_buffer.buffer, cpu);
  4182. #endif
  4183. }
  4184. if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
  4185. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  4186. atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
  4187. ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
  4188. #ifdef CONFIG_TRACER_MAX_TRACE
  4189. ring_buffer_record_enable_cpu(tr->max_buffer.buffer, cpu);
  4190. #endif
  4191. }
  4192. }
  4193. arch_spin_unlock(&tr->max_lock);
  4194. local_irq_enable();
  4195. cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
  4196. return 0;
  4197. }
  4198. static ssize_t
  4199. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  4200. size_t count, loff_t *ppos)
  4201. {
  4202. struct trace_array *tr = file_inode(filp)->i_private;
  4203. cpumask_var_t tracing_cpumask_new;
  4204. int err;
  4205. if (count == 0 || count > KMALLOC_MAX_SIZE)
  4206. return -EINVAL;
  4207. if (!zalloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  4208. return -ENOMEM;
  4209. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  4210. if (err)
  4211. goto err_free;
  4212. err = tracing_set_cpumask(tr, tracing_cpumask_new);
  4213. if (err)
  4214. goto err_free;
  4215. free_cpumask_var(tracing_cpumask_new);
  4216. return count;
  4217. err_free:
  4218. free_cpumask_var(tracing_cpumask_new);
  4219. return err;
  4220. }
  4221. static const struct file_operations tracing_cpumask_fops = {
  4222. .open = tracing_open_generic_tr,
  4223. .read = tracing_cpumask_read,
  4224. .write = tracing_cpumask_write,
  4225. .release = tracing_release_generic_tr,
  4226. .llseek = generic_file_llseek,
  4227. };
  4228. static int tracing_trace_options_show(struct seq_file *m, void *v)
  4229. {
  4230. struct tracer_opt *trace_opts;
  4231. struct trace_array *tr = m->private;
  4232. u32 tracer_flags;
  4233. int i;
  4234. guard(mutex)(&trace_types_lock);
  4235. tracer_flags = tr->current_trace->flags->val;
  4236. trace_opts = tr->current_trace->flags->opts;
  4237. for (i = 0; trace_options[i]; i++) {
  4238. if (tr->trace_flags & (1 << i))
  4239. seq_printf(m, "%s\n", trace_options[i]);
  4240. else
  4241. seq_printf(m, "no%s\n", trace_options[i]);
  4242. }
  4243. for (i = 0; trace_opts[i].name; i++) {
  4244. if (tracer_flags & trace_opts[i].bit)
  4245. seq_printf(m, "%s\n", trace_opts[i].name);
  4246. else
  4247. seq_printf(m, "no%s\n", trace_opts[i].name);
  4248. }
  4249. return 0;
  4250. }
  4251. static int __set_tracer_option(struct trace_array *tr,
  4252. struct tracer_flags *tracer_flags,
  4253. struct tracer_opt *opts, int neg)
  4254. {
  4255. struct tracer *trace = tracer_flags->trace;
  4256. int ret;
  4257. ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
  4258. if (ret)
  4259. return ret;
  4260. if (neg)
  4261. tracer_flags->val &= ~opts->bit;
  4262. else
  4263. tracer_flags->val |= opts->bit;
  4264. return 0;
  4265. }
  4266. /* Try to assign a tracer specific option */
  4267. static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
  4268. {
  4269. struct tracer *trace = tr->current_trace;
  4270. struct tracer_flags *tracer_flags = trace->flags;
  4271. struct tracer_opt *opts = NULL;
  4272. int i;
  4273. for (i = 0; tracer_flags->opts[i].name; i++) {
  4274. opts = &tracer_flags->opts[i];
  4275. if (strcmp(cmp, opts->name) == 0)
  4276. return __set_tracer_option(tr, trace->flags, opts, neg);
  4277. }
  4278. return -EINVAL;
  4279. }
  4280. /* Some tracers require overwrite to stay enabled */
  4281. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
  4282. {
  4283. if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
  4284. return -1;
  4285. return 0;
  4286. }
  4287. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
  4288. {
  4289. if ((mask == TRACE_ITER_RECORD_TGID) ||
  4290. (mask == TRACE_ITER_RECORD_CMD) ||
  4291. (mask == TRACE_ITER_TRACE_PRINTK))
  4292. lockdep_assert_held(&event_mutex);
  4293. /* do nothing if flag is already set */
  4294. if (!!(tr->trace_flags & mask) == !!enabled)
  4295. return 0;
  4296. /* Give the tracer a chance to approve the change */
  4297. if (tr->current_trace->flag_changed)
  4298. if (tr->current_trace->flag_changed(tr, mask, !!enabled))
  4299. return -EINVAL;
  4300. if (mask == TRACE_ITER_TRACE_PRINTK) {
  4301. if (enabled) {
  4302. update_printk_trace(tr);
  4303. } else {
  4304. /*
  4305. * The global_trace cannot clear this.
  4306. * It's flag only gets cleared if another instance sets it.
  4307. */
  4308. if (printk_trace == &global_trace)
  4309. return -EINVAL;
  4310. /*
  4311. * An instance must always have it set.
  4312. * by default, that's the global_trace instane.
  4313. */
  4314. if (printk_trace == tr)
  4315. update_printk_trace(&global_trace);
  4316. }
  4317. }
  4318. if (enabled)
  4319. tr->trace_flags |= mask;
  4320. else
  4321. tr->trace_flags &= ~mask;
  4322. if (mask == TRACE_ITER_RECORD_CMD)
  4323. trace_event_enable_cmd_record(enabled);
  4324. if (mask == TRACE_ITER_RECORD_TGID) {
  4325. if (trace_alloc_tgid_map() < 0) {
  4326. tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
  4327. return -ENOMEM;
  4328. }
  4329. trace_event_enable_tgid_record(enabled);
  4330. }
  4331. if (mask == TRACE_ITER_EVENT_FORK)
  4332. trace_event_follow_fork(tr, enabled);
  4333. if (mask == TRACE_ITER_FUNC_FORK)
  4334. ftrace_pid_follow_fork(tr, enabled);
  4335. if (mask == TRACE_ITER_OVERWRITE) {
  4336. ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
  4337. #ifdef CONFIG_TRACER_MAX_TRACE
  4338. ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
  4339. #endif
  4340. }
  4341. if (mask == TRACE_ITER_PRINTK) {
  4342. trace_printk_start_stop_comm(enabled);
  4343. trace_printk_control(enabled);
  4344. }
  4345. return 0;
  4346. }
  4347. int trace_set_options(struct trace_array *tr, char *option)
  4348. {
  4349. char *cmp;
  4350. int neg = 0;
  4351. int ret;
  4352. size_t orig_len = strlen(option);
  4353. int len;
  4354. cmp = strstrip(option);
  4355. len = str_has_prefix(cmp, "no");
  4356. if (len)
  4357. neg = 1;
  4358. cmp += len;
  4359. mutex_lock(&event_mutex);
  4360. mutex_lock(&trace_types_lock);
  4361. ret = match_string(trace_options, -1, cmp);
  4362. /* If no option could be set, test the specific tracer options */
  4363. if (ret < 0)
  4364. ret = set_tracer_option(tr, cmp, neg);
  4365. else
  4366. ret = set_tracer_flag(tr, 1 << ret, !neg);
  4367. mutex_unlock(&trace_types_lock);
  4368. mutex_unlock(&event_mutex);
  4369. /*
  4370. * If the first trailing whitespace is replaced with '\0' by strstrip,
  4371. * turn it back into a space.
  4372. */
  4373. if (orig_len > strlen(option))
  4374. option[strlen(option)] = ' ';
  4375. return ret;
  4376. }
  4377. static void __init apply_trace_boot_options(void)
  4378. {
  4379. char *buf = trace_boot_options_buf;
  4380. char *option;
  4381. while (true) {
  4382. option = strsep(&buf, ",");
  4383. if (!option)
  4384. break;
  4385. if (*option)
  4386. trace_set_options(&global_trace, option);
  4387. /* Put back the comma to allow this to be called again */
  4388. if (buf)
  4389. *(buf - 1) = ',';
  4390. }
  4391. }
  4392. static ssize_t
  4393. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  4394. size_t cnt, loff_t *ppos)
  4395. {
  4396. struct seq_file *m = filp->private_data;
  4397. struct trace_array *tr = m->private;
  4398. char buf[64];
  4399. int ret;
  4400. if (cnt >= sizeof(buf))
  4401. return -EINVAL;
  4402. if (copy_from_user(buf, ubuf, cnt))
  4403. return -EFAULT;
  4404. buf[cnt] = 0;
  4405. ret = trace_set_options(tr, buf);
  4406. if (ret < 0)
  4407. return ret;
  4408. *ppos += cnt;
  4409. return cnt;
  4410. }
  4411. static int tracing_trace_options_open(struct inode *inode, struct file *file)
  4412. {
  4413. struct trace_array *tr = inode->i_private;
  4414. int ret;
  4415. ret = tracing_check_open_get_tr(tr);
  4416. if (ret)
  4417. return ret;
  4418. ret = single_open(file, tracing_trace_options_show, inode->i_private);
  4419. if (ret < 0)
  4420. trace_array_put(tr);
  4421. return ret;
  4422. }
  4423. static const struct file_operations tracing_iter_fops = {
  4424. .open = tracing_trace_options_open,
  4425. .read = seq_read,
  4426. .llseek = seq_lseek,
  4427. .release = tracing_single_release_tr,
  4428. .write = tracing_trace_options_write,
  4429. };
  4430. static const char readme_msg[] =
  4431. "tracing mini-HOWTO:\n\n"
  4432. "By default tracefs removes all OTH file permission bits.\n"
  4433. "When mounting tracefs an optional group id can be specified\n"
  4434. "which adds the group to every directory and file in tracefs:\n\n"
  4435. "\t e.g. mount -t tracefs [-o [gid=<gid>]] nodev /sys/kernel/tracing\n\n"
  4436. "# echo 0 > tracing_on : quick way to disable tracing\n"
  4437. "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
  4438. " Important files:\n"
  4439. " trace\t\t\t- The static contents of the buffer\n"
  4440. "\t\t\t To clear the buffer write into this file: echo > trace\n"
  4441. " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
  4442. " current_tracer\t- function and latency tracers\n"
  4443. " available_tracers\t- list of configured tracers for current_tracer\n"
  4444. " error_log\t- error log for failed commands (that support it)\n"
  4445. " buffer_size_kb\t- view and modify size of per cpu buffer\n"
  4446. " buffer_total_size_kb - view total size of all cpu buffers\n\n"
  4447. " trace_clock\t\t- change the clock used to order events\n"
  4448. " local: Per cpu clock but may not be synced across CPUs\n"
  4449. " global: Synced across CPUs but slows tracing down.\n"
  4450. " counter: Not a clock, but just an increment\n"
  4451. " uptime: Jiffy counter from time of boot\n"
  4452. " perf: Same clock that perf events use\n"
  4453. #ifdef CONFIG_X86_64
  4454. " x86-tsc: TSC cycle counter\n"
  4455. #endif
  4456. "\n timestamp_mode\t- view the mode used to timestamp events\n"
  4457. " delta: Delta difference against a buffer-wide timestamp\n"
  4458. " absolute: Absolute (standalone) timestamp\n"
  4459. "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
  4460. "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
  4461. " tracing_cpumask\t- Limit which CPUs to trace\n"
  4462. " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
  4463. "\t\t\t Remove sub-buffer with rmdir\n"
  4464. " trace_options\t\t- Set format or modify how tracing happens\n"
  4465. "\t\t\t Disable an option by prefixing 'no' to the\n"
  4466. "\t\t\t option name\n"
  4467. " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
  4468. #ifdef CONFIG_DYNAMIC_FTRACE
  4469. "\n available_filter_functions - list of functions that can be filtered on\n"
  4470. " set_ftrace_filter\t- echo function name in here to only trace these\n"
  4471. "\t\t\t functions\n"
  4472. "\t accepts: func_full_name or glob-matching-pattern\n"
  4473. "\t modules: Can select a group via module\n"
  4474. "\t Format: :mod:<module-name>\n"
  4475. "\t example: echo :mod:ext3 > set_ftrace_filter\n"
  4476. "\t triggers: a command to perform when function is hit\n"
  4477. "\t Format: <function>:<trigger>[:count]\n"
  4478. "\t trigger: traceon, traceoff\n"
  4479. "\t\t enable_event:<system>:<event>\n"
  4480. "\t\t disable_event:<system>:<event>\n"
  4481. #ifdef CONFIG_STACKTRACE
  4482. "\t\t stacktrace\n"
  4483. #endif
  4484. #ifdef CONFIG_TRACER_SNAPSHOT
  4485. "\t\t snapshot\n"
  4486. #endif
  4487. "\t\t dump\n"
  4488. "\t\t cpudump\n"
  4489. "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
  4490. "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
  4491. "\t The first one will disable tracing every time do_fault is hit\n"
  4492. "\t The second will disable tracing at most 3 times when do_trap is hit\n"
  4493. "\t The first time do trap is hit and it disables tracing, the\n"
  4494. "\t counter will decrement to 2. If tracing is already disabled,\n"
  4495. "\t the counter will not decrement. It only decrements when the\n"
  4496. "\t trigger did work\n"
  4497. "\t To remove trigger without count:\n"
  4498. "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
  4499. "\t To remove trigger with a count:\n"
  4500. "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
  4501. " set_ftrace_notrace\t- echo function name in here to never trace.\n"
  4502. "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
  4503. "\t modules: Can select a group via module command :mod:\n"
  4504. "\t Does not accept triggers\n"
  4505. #endif /* CONFIG_DYNAMIC_FTRACE */
  4506. #ifdef CONFIG_FUNCTION_TRACER
  4507. " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
  4508. "\t\t (function)\n"
  4509. " set_ftrace_notrace_pid\t- Write pid(s) to not function trace those pids\n"
  4510. "\t\t (function)\n"
  4511. #endif
  4512. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  4513. " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
  4514. " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
  4515. " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
  4516. #endif
  4517. #ifdef CONFIG_TRACER_SNAPSHOT
  4518. "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
  4519. "\t\t\t snapshot buffer. Read the contents for more\n"
  4520. "\t\t\t information\n"
  4521. #endif
  4522. #ifdef CONFIG_STACK_TRACER
  4523. " stack_trace\t\t- Shows the max stack trace when active\n"
  4524. " stack_max_size\t- Shows current max stack size that was traced\n"
  4525. "\t\t\t Write into this file to reset the max size (trigger a\n"
  4526. "\t\t\t new trace)\n"
  4527. #ifdef CONFIG_DYNAMIC_FTRACE
  4528. " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
  4529. "\t\t\t traces\n"
  4530. #endif
  4531. #endif /* CONFIG_STACK_TRACER */
  4532. #ifdef CONFIG_DYNAMIC_EVENTS
  4533. " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
  4534. "\t\t\t Write into this file to define/undefine new trace events.\n"
  4535. #endif
  4536. #ifdef CONFIG_KPROBE_EVENTS
  4537. " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
  4538. "\t\t\t Write into this file to define/undefine new trace events.\n"
  4539. #endif
  4540. #ifdef CONFIG_UPROBE_EVENTS
  4541. " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
  4542. "\t\t\t Write into this file to define/undefine new trace events.\n"
  4543. #endif
  4544. #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) || \
  4545. defined(CONFIG_FPROBE_EVENTS)
  4546. "\t accepts: event-definitions (one definition per line)\n"
  4547. #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
  4548. "\t Format: p[:[<group>/][<event>]] <place> [<args>]\n"
  4549. "\t r[maxactive][:[<group>/][<event>]] <place> [<args>]\n"
  4550. #endif
  4551. #ifdef CONFIG_FPROBE_EVENTS
  4552. "\t f[:[<group>/][<event>]] <func-name>[%return] [<args>]\n"
  4553. "\t t[:[<group>/][<event>]] <tracepoint> [<args>]\n"
  4554. #endif
  4555. #ifdef CONFIG_HIST_TRIGGERS
  4556. "\t s:[synthetic/]<event> <field> [<field>]\n"
  4557. #endif
  4558. "\t e[:[<group>/][<event>]] <attached-group>.<attached-event> [<args>] [if <filter>]\n"
  4559. "\t -:[<group>/][<event>]\n"
  4560. #ifdef CONFIG_KPROBE_EVENTS
  4561. "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
  4562. "place (kretprobe): [<module>:]<symbol>[+<offset>]%return|<memaddr>\n"
  4563. #endif
  4564. #ifdef CONFIG_UPROBE_EVENTS
  4565. " place (uprobe): <path>:<offset>[%return][(ref_ctr_offset)]\n"
  4566. #endif
  4567. "\t args: <name>=fetcharg[:type]\n"
  4568. "\t fetcharg: (%<register>|$<efield>), @<address>, @<symbol>[+|-<offset>],\n"
  4569. #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
  4570. "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
  4571. #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
  4572. "\t <argname>[->field[->field|.field...]],\n"
  4573. #endif
  4574. #else
  4575. "\t $stack<index>, $stack, $retval, $comm,\n"
  4576. #endif
  4577. "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
  4578. "\t kernel return probes support: $retval, $arg<N>, $comm\n"
  4579. "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
  4580. "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
  4581. "\t symstr, %pd/%pD, <type>\\[<array-size>\\]\n"
  4582. #ifdef CONFIG_HIST_TRIGGERS
  4583. "\t field: <stype> <name>;\n"
  4584. "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
  4585. "\t [unsigned] char/int/long\n"
  4586. #endif
  4587. "\t efield: For event probes ('e' types), the field is on of the fields\n"
  4588. "\t of the <attached-group>/<attached-event>.\n"
  4589. #endif
  4590. " events/\t\t- Directory containing all trace event subsystems:\n"
  4591. " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
  4592. " events/<system>/\t- Directory containing all trace events for <system>:\n"
  4593. " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
  4594. "\t\t\t events\n"
  4595. " filter\t\t- If set, only events passing filter are traced\n"
  4596. " events/<system>/<event>/\t- Directory containing control files for\n"
  4597. "\t\t\t <event>:\n"
  4598. " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
  4599. " filter\t\t- If set, only events passing filter are traced\n"
  4600. " trigger\t\t- If set, a command to perform when event is hit\n"
  4601. "\t Format: <trigger>[:count][if <filter>]\n"
  4602. "\t trigger: traceon, traceoff\n"
  4603. "\t enable_event:<system>:<event>\n"
  4604. "\t disable_event:<system>:<event>\n"
  4605. #ifdef CONFIG_HIST_TRIGGERS
  4606. "\t enable_hist:<system>:<event>\n"
  4607. "\t disable_hist:<system>:<event>\n"
  4608. #endif
  4609. #ifdef CONFIG_STACKTRACE
  4610. "\t\t stacktrace\n"
  4611. #endif
  4612. #ifdef CONFIG_TRACER_SNAPSHOT
  4613. "\t\t snapshot\n"
  4614. #endif
  4615. #ifdef CONFIG_HIST_TRIGGERS
  4616. "\t\t hist (see below)\n"
  4617. #endif
  4618. "\t example: echo traceoff > events/block/block_unplug/trigger\n"
  4619. "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
  4620. "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
  4621. "\t events/block/block_unplug/trigger\n"
  4622. "\t The first disables tracing every time block_unplug is hit.\n"
  4623. "\t The second disables tracing the first 3 times block_unplug is hit.\n"
  4624. "\t The third enables the kmalloc event the first 3 times block_unplug\n"
  4625. "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
  4626. "\t Like function triggers, the counter is only decremented if it\n"
  4627. "\t enabled or disabled tracing.\n"
  4628. "\t To remove a trigger without a count:\n"
  4629. "\t echo '!<trigger> > <system>/<event>/trigger\n"
  4630. "\t To remove a trigger with a count:\n"
  4631. "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
  4632. "\t Filters can be ignored when removing a trigger.\n"
  4633. #ifdef CONFIG_HIST_TRIGGERS
  4634. " hist trigger\t- If set, event hits are aggregated into a hash table\n"
  4635. "\t Format: hist:keys=<field1[,field2,...]>\n"
  4636. "\t [:<var1>=<field|var_ref|numeric_literal>[,<var2>=...]]\n"
  4637. "\t [:values=<field1[,field2,...]>]\n"
  4638. "\t [:sort=<field1[,field2,...]>]\n"
  4639. "\t [:size=#entries]\n"
  4640. "\t [:pause][:continue][:clear]\n"
  4641. "\t [:name=histname1]\n"
  4642. "\t [:nohitcount]\n"
  4643. "\t [:<handler>.<action>]\n"
  4644. "\t [if <filter>]\n\n"
  4645. "\t Note, special fields can be used as well:\n"
  4646. "\t common_timestamp - to record current timestamp\n"
  4647. "\t common_cpu - to record the CPU the event happened on\n"
  4648. "\n"
  4649. "\t A hist trigger variable can be:\n"
  4650. "\t - a reference to a field e.g. x=current_timestamp,\n"
  4651. "\t - a reference to another variable e.g. y=$x,\n"
  4652. "\t - a numeric literal: e.g. ms_per_sec=1000,\n"
  4653. "\t - an arithmetic expression: e.g. time_secs=current_timestamp/1000\n"
  4654. "\n"
  4655. "\t hist trigger arithmetic expressions support addition(+), subtraction(-),\n"
  4656. "\t multiplication(*) and division(/) operators. An operand can be either a\n"
  4657. "\t variable reference, field or numeric literal.\n"
  4658. "\n"
  4659. "\t When a matching event is hit, an entry is added to a hash\n"
  4660. "\t table using the key(s) and value(s) named, and the value of a\n"
  4661. "\t sum called 'hitcount' is incremented. Keys and values\n"
  4662. "\t correspond to fields in the event's format description. Keys\n"
  4663. "\t can be any field, or the special string 'common_stacktrace'.\n"
  4664. "\t Compound keys consisting of up to two fields can be specified\n"
  4665. "\t by the 'keys' keyword. Values must correspond to numeric\n"
  4666. "\t fields. Sort keys consisting of up to two fields can be\n"
  4667. "\t specified using the 'sort' keyword. The sort direction can\n"
  4668. "\t be modified by appending '.descending' or '.ascending' to a\n"
  4669. "\t sort field. The 'size' parameter can be used to specify more\n"
  4670. "\t or fewer than the default 2048 entries for the hashtable size.\n"
  4671. "\t If a hist trigger is given a name using the 'name' parameter,\n"
  4672. "\t its histogram data will be shared with other triggers of the\n"
  4673. "\t same name, and trigger hits will update this common data.\n\n"
  4674. "\t Reading the 'hist' file for the event will dump the hash\n"
  4675. "\t table in its entirety to stdout. If there are multiple hist\n"
  4676. "\t triggers attached to an event, there will be a table for each\n"
  4677. "\t trigger in the output. The table displayed for a named\n"
  4678. "\t trigger will be the same as any other instance having the\n"
  4679. "\t same name. The default format used to display a given field\n"
  4680. "\t can be modified by appending any of the following modifiers\n"
  4681. "\t to the field name, as applicable:\n\n"
  4682. "\t .hex display a number as a hex value\n"
  4683. "\t .sym display an address as a symbol\n"
  4684. "\t .sym-offset display an address as a symbol and offset\n"
  4685. "\t .execname display a common_pid as a program name\n"
  4686. "\t .syscall display a syscall id as a syscall name\n"
  4687. "\t .log2 display log2 value rather than raw number\n"
  4688. "\t .buckets=size display values in groups of size rather than raw number\n"
  4689. "\t .usecs display a common_timestamp in microseconds\n"
  4690. "\t .percent display a number of percentage value\n"
  4691. "\t .graph display a bar-graph of a value\n\n"
  4692. "\t The 'pause' parameter can be used to pause an existing hist\n"
  4693. "\t trigger or to start a hist trigger but not log any events\n"
  4694. "\t until told to do so. 'continue' can be used to start or\n"
  4695. "\t restart a paused hist trigger.\n\n"
  4696. "\t The 'clear' parameter will clear the contents of a running\n"
  4697. "\t hist trigger and leave its current paused/active state\n"
  4698. "\t unchanged.\n\n"
  4699. "\t The 'nohitcount' (or NOHC) parameter will suppress display of\n"
  4700. "\t raw hitcount in the histogram.\n\n"
  4701. "\t The enable_hist and disable_hist triggers can be used to\n"
  4702. "\t have one event conditionally start and stop another event's\n"
  4703. "\t already-attached hist trigger. The syntax is analogous to\n"
  4704. "\t the enable_event and disable_event triggers.\n\n"
  4705. "\t Hist trigger handlers and actions are executed whenever a\n"
  4706. "\t a histogram entry is added or updated. They take the form:\n\n"
  4707. "\t <handler>.<action>\n\n"
  4708. "\t The available handlers are:\n\n"
  4709. "\t onmatch(matching.event) - invoke on addition or update\n"
  4710. "\t onmax(var) - invoke if var exceeds current max\n"
  4711. "\t onchange(var) - invoke action if var changes\n\n"
  4712. "\t The available actions are:\n\n"
  4713. "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
  4714. "\t save(field,...) - save current event fields\n"
  4715. #ifdef CONFIG_TRACER_SNAPSHOT
  4716. "\t snapshot() - snapshot the trace buffer\n\n"
  4717. #endif
  4718. #ifdef CONFIG_SYNTH_EVENTS
  4719. " events/synthetic_events\t- Create/append/remove/show synthetic events\n"
  4720. "\t Write into this file to define/undefine new synthetic events.\n"
  4721. "\t example: echo 'myevent u64 lat; char name[]; long[] stack' >> synthetic_events\n"
  4722. #endif
  4723. #endif
  4724. ;
  4725. static ssize_t
  4726. tracing_readme_read(struct file *filp, char __user *ubuf,
  4727. size_t cnt, loff_t *ppos)
  4728. {
  4729. return simple_read_from_buffer(ubuf, cnt, ppos,
  4730. readme_msg, strlen(readme_msg));
  4731. }
  4732. static const struct file_operations tracing_readme_fops = {
  4733. .open = tracing_open_generic,
  4734. .read = tracing_readme_read,
  4735. .llseek = generic_file_llseek,
  4736. };
  4737. #ifdef CONFIG_TRACE_EVAL_MAP_FILE
  4738. static union trace_eval_map_item *
  4739. update_eval_map(union trace_eval_map_item *ptr)
  4740. {
  4741. if (!ptr->map.eval_string) {
  4742. if (ptr->tail.next) {
  4743. ptr = ptr->tail.next;
  4744. /* Set ptr to the next real item (skip head) */
  4745. ptr++;
  4746. } else
  4747. return NULL;
  4748. }
  4749. return ptr;
  4750. }
  4751. static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
  4752. {
  4753. union trace_eval_map_item *ptr = v;
  4754. /*
  4755. * Paranoid! If ptr points to end, we don't want to increment past it.
  4756. * This really should never happen.
  4757. */
  4758. (*pos)++;
  4759. ptr = update_eval_map(ptr);
  4760. if (WARN_ON_ONCE(!ptr))
  4761. return NULL;
  4762. ptr++;
  4763. ptr = update_eval_map(ptr);
  4764. return ptr;
  4765. }
  4766. static void *eval_map_start(struct seq_file *m, loff_t *pos)
  4767. {
  4768. union trace_eval_map_item *v;
  4769. loff_t l = 0;
  4770. mutex_lock(&trace_eval_mutex);
  4771. v = trace_eval_maps;
  4772. if (v)
  4773. v++;
  4774. while (v && l < *pos) {
  4775. v = eval_map_next(m, v, &l);
  4776. }
  4777. return v;
  4778. }
  4779. static void eval_map_stop(struct seq_file *m, void *v)
  4780. {
  4781. mutex_unlock(&trace_eval_mutex);
  4782. }
  4783. static int eval_map_show(struct seq_file *m, void *v)
  4784. {
  4785. union trace_eval_map_item *ptr = v;
  4786. seq_printf(m, "%s %ld (%s)\n",
  4787. ptr->map.eval_string, ptr->map.eval_value,
  4788. ptr->map.system);
  4789. return 0;
  4790. }
  4791. static const struct seq_operations tracing_eval_map_seq_ops = {
  4792. .start = eval_map_start,
  4793. .next = eval_map_next,
  4794. .stop = eval_map_stop,
  4795. .show = eval_map_show,
  4796. };
  4797. static int tracing_eval_map_open(struct inode *inode, struct file *filp)
  4798. {
  4799. int ret;
  4800. ret = tracing_check_open_get_tr(NULL);
  4801. if (ret)
  4802. return ret;
  4803. return seq_open(filp, &tracing_eval_map_seq_ops);
  4804. }
  4805. static const struct file_operations tracing_eval_map_fops = {
  4806. .open = tracing_eval_map_open,
  4807. .read = seq_read,
  4808. .llseek = seq_lseek,
  4809. .release = seq_release,
  4810. };
  4811. static inline union trace_eval_map_item *
  4812. trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
  4813. {
  4814. /* Return tail of array given the head */
  4815. return ptr + ptr->head.length + 1;
  4816. }
  4817. static void
  4818. trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
  4819. int len)
  4820. {
  4821. struct trace_eval_map **stop;
  4822. struct trace_eval_map **map;
  4823. union trace_eval_map_item *map_array;
  4824. union trace_eval_map_item *ptr;
  4825. stop = start + len;
  4826. /*
  4827. * The trace_eval_maps contains the map plus a head and tail item,
  4828. * where the head holds the module and length of array, and the
  4829. * tail holds a pointer to the next list.
  4830. */
  4831. map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
  4832. if (!map_array) {
  4833. pr_warn("Unable to allocate trace eval mapping\n");
  4834. return;
  4835. }
  4836. guard(mutex)(&trace_eval_mutex);
  4837. if (!trace_eval_maps)
  4838. trace_eval_maps = map_array;
  4839. else {
  4840. ptr = trace_eval_maps;
  4841. for (;;) {
  4842. ptr = trace_eval_jmp_to_tail(ptr);
  4843. if (!ptr->tail.next)
  4844. break;
  4845. ptr = ptr->tail.next;
  4846. }
  4847. ptr->tail.next = map_array;
  4848. }
  4849. map_array->head.mod = mod;
  4850. map_array->head.length = len;
  4851. map_array++;
  4852. for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
  4853. map_array->map = **map;
  4854. map_array++;
  4855. }
  4856. memset(map_array, 0, sizeof(*map_array));
  4857. }
  4858. static void trace_create_eval_file(struct dentry *d_tracer)
  4859. {
  4860. trace_create_file("eval_map", TRACE_MODE_READ, d_tracer,
  4861. NULL, &tracing_eval_map_fops);
  4862. }
  4863. #else /* CONFIG_TRACE_EVAL_MAP_FILE */
  4864. static inline void trace_create_eval_file(struct dentry *d_tracer) { }
  4865. static inline void trace_insert_eval_map_file(struct module *mod,
  4866. struct trace_eval_map **start, int len) { }
  4867. #endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
  4868. static void trace_insert_eval_map(struct module *mod,
  4869. struct trace_eval_map **start, int len)
  4870. {
  4871. struct trace_eval_map **map;
  4872. if (len <= 0)
  4873. return;
  4874. map = start;
  4875. trace_event_eval_update(map, len);
  4876. trace_insert_eval_map_file(mod, start, len);
  4877. }
  4878. static ssize_t
  4879. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  4880. size_t cnt, loff_t *ppos)
  4881. {
  4882. struct trace_array *tr = filp->private_data;
  4883. char buf[MAX_TRACER_SIZE+2];
  4884. int r;
  4885. mutex_lock(&trace_types_lock);
  4886. r = sprintf(buf, "%s\n", tr->current_trace->name);
  4887. mutex_unlock(&trace_types_lock);
  4888. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  4889. }
  4890. int tracer_init(struct tracer *t, struct trace_array *tr)
  4891. {
  4892. tracing_reset_online_cpus(&tr->array_buffer);
  4893. return t->init(tr);
  4894. }
  4895. static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
  4896. {
  4897. int cpu;
  4898. for_each_tracing_cpu(cpu)
  4899. per_cpu_ptr(buf->data, cpu)->entries = val;
  4900. }
  4901. static void update_buffer_entries(struct array_buffer *buf, int cpu)
  4902. {
  4903. if (cpu == RING_BUFFER_ALL_CPUS) {
  4904. set_buffer_entries(buf, ring_buffer_size(buf->buffer, 0));
  4905. } else {
  4906. per_cpu_ptr(buf->data, cpu)->entries = ring_buffer_size(buf->buffer, cpu);
  4907. }
  4908. }
  4909. #ifdef CONFIG_TRACER_MAX_TRACE
  4910. /* resize @tr's buffer to the size of @size_tr's entries */
  4911. static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
  4912. struct array_buffer *size_buf, int cpu_id)
  4913. {
  4914. int cpu, ret = 0;
  4915. if (cpu_id == RING_BUFFER_ALL_CPUS) {
  4916. for_each_tracing_cpu(cpu) {
  4917. ret = ring_buffer_resize(trace_buf->buffer,
  4918. per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
  4919. if (ret < 0)
  4920. break;
  4921. per_cpu_ptr(trace_buf->data, cpu)->entries =
  4922. per_cpu_ptr(size_buf->data, cpu)->entries;
  4923. }
  4924. } else {
  4925. ret = ring_buffer_resize(trace_buf->buffer,
  4926. per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
  4927. if (ret == 0)
  4928. per_cpu_ptr(trace_buf->data, cpu_id)->entries =
  4929. per_cpu_ptr(size_buf->data, cpu_id)->entries;
  4930. }
  4931. return ret;
  4932. }
  4933. #endif /* CONFIG_TRACER_MAX_TRACE */
  4934. static int __tracing_resize_ring_buffer(struct trace_array *tr,
  4935. unsigned long size, int cpu)
  4936. {
  4937. int ret;
  4938. /*
  4939. * If kernel or user changes the size of the ring buffer
  4940. * we use the size that was given, and we can forget about
  4941. * expanding it later.
  4942. */
  4943. trace_set_ring_buffer_expanded(tr);
  4944. /* May be called before buffers are initialized */
  4945. if (!tr->array_buffer.buffer)
  4946. return 0;
  4947. /* Do not allow tracing while resizing ring buffer */
  4948. tracing_stop_tr(tr);
  4949. ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
  4950. if (ret < 0)
  4951. goto out_start;
  4952. #ifdef CONFIG_TRACER_MAX_TRACE
  4953. if (!tr->allocated_snapshot)
  4954. goto out;
  4955. ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
  4956. if (ret < 0) {
  4957. int r = resize_buffer_duplicate_size(&tr->array_buffer,
  4958. &tr->array_buffer, cpu);
  4959. if (r < 0) {
  4960. /*
  4961. * AARGH! We are left with different
  4962. * size max buffer!!!!
  4963. * The max buffer is our "snapshot" buffer.
  4964. * When a tracer needs a snapshot (one of the
  4965. * latency tracers), it swaps the max buffer
  4966. * with the saved snap shot. We succeeded to
  4967. * update the size of the main buffer, but failed to
  4968. * update the size of the max buffer. But when we tried
  4969. * to reset the main buffer to the original size, we
  4970. * failed there too. This is very unlikely to
  4971. * happen, but if it does, warn and kill all
  4972. * tracing.
  4973. */
  4974. WARN_ON(1);
  4975. tracing_disabled = 1;
  4976. }
  4977. goto out_start;
  4978. }
  4979. update_buffer_entries(&tr->max_buffer, cpu);
  4980. out:
  4981. #endif /* CONFIG_TRACER_MAX_TRACE */
  4982. update_buffer_entries(&tr->array_buffer, cpu);
  4983. out_start:
  4984. tracing_start_tr(tr);
  4985. return ret;
  4986. }
  4987. ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
  4988. unsigned long size, int cpu_id)
  4989. {
  4990. guard(mutex)(&trace_types_lock);
  4991. if (cpu_id != RING_BUFFER_ALL_CPUS) {
  4992. /* make sure, this cpu is enabled in the mask */
  4993. if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask))
  4994. return -EINVAL;
  4995. }
  4996. return __tracing_resize_ring_buffer(tr, size, cpu_id);
  4997. }
  4998. static void update_last_data(struct trace_array *tr)
  4999. {
  5000. if (!tr->text_delta && !tr->data_delta)
  5001. return;
  5002. /*
  5003. * Need to clear all CPU buffers as there cannot be events
  5004. * from the previous boot mixed with events with this boot
  5005. * as that will cause a confusing trace. Need to clear all
  5006. * CPU buffers, even for those that may currently be offline.
  5007. */
  5008. tracing_reset_all_cpus(&tr->array_buffer);
  5009. /* Using current data now */
  5010. tr->text_delta = 0;
  5011. tr->data_delta = 0;
  5012. }
  5013. /**
  5014. * tracing_update_buffers - used by tracing facility to expand ring buffers
  5015. * @tr: The tracing instance
  5016. *
  5017. * To save on memory when the tracing is never used on a system with it
  5018. * configured in. The ring buffers are set to a minimum size. But once
  5019. * a user starts to use the tracing facility, then they need to grow
  5020. * to their default size.
  5021. *
  5022. * This function is to be called when a tracer is about to be used.
  5023. */
  5024. int tracing_update_buffers(struct trace_array *tr)
  5025. {
  5026. int ret = 0;
  5027. mutex_lock(&trace_types_lock);
  5028. update_last_data(tr);
  5029. if (!tr->ring_buffer_expanded)
  5030. ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
  5031. RING_BUFFER_ALL_CPUS);
  5032. mutex_unlock(&trace_types_lock);
  5033. return ret;
  5034. }
  5035. struct trace_option_dentry;
  5036. static void
  5037. create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
  5038. /*
  5039. * Used to clear out the tracer before deletion of an instance.
  5040. * Must have trace_types_lock held.
  5041. */
  5042. static void tracing_set_nop(struct trace_array *tr)
  5043. {
  5044. if (tr->current_trace == &nop_trace)
  5045. return;
  5046. tr->current_trace->enabled--;
  5047. if (tr->current_trace->reset)
  5048. tr->current_trace->reset(tr);
  5049. tr->current_trace = &nop_trace;
  5050. }
  5051. static bool tracer_options_updated;
  5052. static void add_tracer_options(struct trace_array *tr, struct tracer *t)
  5053. {
  5054. /* Only enable if the directory has been created already. */
  5055. if (!tr->dir)
  5056. return;
  5057. /* Only create trace option files after update_tracer_options finish */
  5058. if (!tracer_options_updated)
  5059. return;
  5060. create_trace_option_files(tr, t);
  5061. }
  5062. int tracing_set_tracer(struct trace_array *tr, const char *buf)
  5063. {
  5064. struct tracer *t;
  5065. #ifdef CONFIG_TRACER_MAX_TRACE
  5066. bool had_max_tr;
  5067. #endif
  5068. int ret;
  5069. guard(mutex)(&trace_types_lock);
  5070. update_last_data(tr);
  5071. if (!tr->ring_buffer_expanded) {
  5072. ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
  5073. RING_BUFFER_ALL_CPUS);
  5074. if (ret < 0)
  5075. return ret;
  5076. ret = 0;
  5077. }
  5078. for (t = trace_types; t; t = t->next) {
  5079. if (strcmp(t->name, buf) == 0)
  5080. break;
  5081. }
  5082. if (!t)
  5083. return -EINVAL;
  5084. if (t == tr->current_trace)
  5085. return 0;
  5086. #ifdef CONFIG_TRACER_SNAPSHOT
  5087. if (t->use_max_tr) {
  5088. local_irq_disable();
  5089. arch_spin_lock(&tr->max_lock);
  5090. ret = tr->cond_snapshot ? -EBUSY : 0;
  5091. arch_spin_unlock(&tr->max_lock);
  5092. local_irq_enable();
  5093. if (ret)
  5094. return ret;
  5095. }
  5096. #endif
  5097. /* Some tracers won't work on kernel command line */
  5098. if (system_state < SYSTEM_RUNNING && t->noboot) {
  5099. pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
  5100. t->name);
  5101. return 0;
  5102. }
  5103. /* Some tracers are only allowed for the top level buffer */
  5104. if (!trace_ok_for_array(t, tr))
  5105. return -EINVAL;
  5106. /* If trace pipe files are being read, we can't change the tracer */
  5107. if (tr->trace_ref)
  5108. return -EBUSY;
  5109. trace_branch_disable();
  5110. tr->current_trace->enabled--;
  5111. if (tr->current_trace->reset)
  5112. tr->current_trace->reset(tr);
  5113. #ifdef CONFIG_TRACER_MAX_TRACE
  5114. had_max_tr = tr->current_trace->use_max_tr;
  5115. /* Current trace needs to be nop_trace before synchronize_rcu */
  5116. tr->current_trace = &nop_trace;
  5117. if (had_max_tr && !t->use_max_tr) {
  5118. /*
  5119. * We need to make sure that the update_max_tr sees that
  5120. * current_trace changed to nop_trace to keep it from
  5121. * swapping the buffers after we resize it.
  5122. * The update_max_tr is called from interrupts disabled
  5123. * so a synchronized_sched() is sufficient.
  5124. */
  5125. synchronize_rcu();
  5126. free_snapshot(tr);
  5127. tracing_disarm_snapshot(tr);
  5128. }
  5129. if (!had_max_tr && t->use_max_tr) {
  5130. ret = tracing_arm_snapshot_locked(tr);
  5131. if (ret)
  5132. return ret;
  5133. }
  5134. #else
  5135. tr->current_trace = &nop_trace;
  5136. #endif
  5137. if (t->init) {
  5138. ret = tracer_init(t, tr);
  5139. if (ret) {
  5140. #ifdef CONFIG_TRACER_MAX_TRACE
  5141. if (t->use_max_tr)
  5142. tracing_disarm_snapshot(tr);
  5143. #endif
  5144. return ret;
  5145. }
  5146. }
  5147. tr->current_trace = t;
  5148. tr->current_trace->enabled++;
  5149. trace_branch_enable(tr);
  5150. return 0;
  5151. }
  5152. static ssize_t
  5153. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  5154. size_t cnt, loff_t *ppos)
  5155. {
  5156. struct trace_array *tr = filp->private_data;
  5157. char buf[MAX_TRACER_SIZE+1];
  5158. char *name;
  5159. size_t ret;
  5160. int err;
  5161. ret = cnt;
  5162. if (cnt > MAX_TRACER_SIZE)
  5163. cnt = MAX_TRACER_SIZE;
  5164. if (copy_from_user(buf, ubuf, cnt))
  5165. return -EFAULT;
  5166. buf[cnt] = 0;
  5167. name = strim(buf);
  5168. err = tracing_set_tracer(tr, name);
  5169. if (err)
  5170. return err;
  5171. *ppos += ret;
  5172. return ret;
  5173. }
  5174. static ssize_t
  5175. tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
  5176. size_t cnt, loff_t *ppos)
  5177. {
  5178. char buf[64];
  5179. int r;
  5180. r = snprintf(buf, sizeof(buf), "%ld\n",
  5181. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  5182. if (r > sizeof(buf))
  5183. r = sizeof(buf);
  5184. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  5185. }
  5186. static ssize_t
  5187. tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
  5188. size_t cnt, loff_t *ppos)
  5189. {
  5190. unsigned long val;
  5191. int ret;
  5192. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  5193. if (ret)
  5194. return ret;
  5195. *ptr = val * 1000;
  5196. return cnt;
  5197. }
  5198. static ssize_t
  5199. tracing_thresh_read(struct file *filp, char __user *ubuf,
  5200. size_t cnt, loff_t *ppos)
  5201. {
  5202. return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
  5203. }
  5204. static ssize_t
  5205. tracing_thresh_write(struct file *filp, const char __user *ubuf,
  5206. size_t cnt, loff_t *ppos)
  5207. {
  5208. struct trace_array *tr = filp->private_data;
  5209. int ret;
  5210. guard(mutex)(&trace_types_lock);
  5211. ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
  5212. if (ret < 0)
  5213. return ret;
  5214. if (tr->current_trace->update_thresh) {
  5215. ret = tr->current_trace->update_thresh(tr);
  5216. if (ret < 0)
  5217. return ret;
  5218. }
  5219. return cnt;
  5220. }
  5221. #ifdef CONFIG_TRACER_MAX_TRACE
  5222. static ssize_t
  5223. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  5224. size_t cnt, loff_t *ppos)
  5225. {
  5226. struct trace_array *tr = filp->private_data;
  5227. return tracing_nsecs_read(&tr->max_latency, ubuf, cnt, ppos);
  5228. }
  5229. static ssize_t
  5230. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  5231. size_t cnt, loff_t *ppos)
  5232. {
  5233. struct trace_array *tr = filp->private_data;
  5234. return tracing_nsecs_write(&tr->max_latency, ubuf, cnt, ppos);
  5235. }
  5236. #endif
  5237. static int open_pipe_on_cpu(struct trace_array *tr, int cpu)
  5238. {
  5239. if (cpu == RING_BUFFER_ALL_CPUS) {
  5240. if (cpumask_empty(tr->pipe_cpumask)) {
  5241. cpumask_setall(tr->pipe_cpumask);
  5242. return 0;
  5243. }
  5244. } else if (!cpumask_test_cpu(cpu, tr->pipe_cpumask)) {
  5245. cpumask_set_cpu(cpu, tr->pipe_cpumask);
  5246. return 0;
  5247. }
  5248. return -EBUSY;
  5249. }
  5250. static void close_pipe_on_cpu(struct trace_array *tr, int cpu)
  5251. {
  5252. if (cpu == RING_BUFFER_ALL_CPUS) {
  5253. WARN_ON(!cpumask_full(tr->pipe_cpumask));
  5254. cpumask_clear(tr->pipe_cpumask);
  5255. } else {
  5256. WARN_ON(!cpumask_test_cpu(cpu, tr->pipe_cpumask));
  5257. cpumask_clear_cpu(cpu, tr->pipe_cpumask);
  5258. }
  5259. }
  5260. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  5261. {
  5262. struct trace_array *tr = inode->i_private;
  5263. struct trace_iterator *iter;
  5264. int cpu;
  5265. int ret;
  5266. ret = tracing_check_open_get_tr(tr);
  5267. if (ret)
  5268. return ret;
  5269. mutex_lock(&trace_types_lock);
  5270. cpu = tracing_get_cpu(inode);
  5271. ret = open_pipe_on_cpu(tr, cpu);
  5272. if (ret)
  5273. goto fail_pipe_on_cpu;
  5274. /* create a buffer to store the information to pass to userspace */
  5275. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  5276. if (!iter) {
  5277. ret = -ENOMEM;
  5278. goto fail_alloc_iter;
  5279. }
  5280. trace_seq_init(&iter->seq);
  5281. iter->trace = tr->current_trace;
  5282. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  5283. ret = -ENOMEM;
  5284. goto fail;
  5285. }
  5286. /* trace pipe does not show start of buffer */
  5287. cpumask_setall(iter->started);
  5288. if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
  5289. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  5290. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  5291. if (trace_clocks[tr->clock_id].in_ns)
  5292. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  5293. iter->tr = tr;
  5294. iter->array_buffer = &tr->array_buffer;
  5295. iter->cpu_file = cpu;
  5296. mutex_init(&iter->mutex);
  5297. filp->private_data = iter;
  5298. if (iter->trace->pipe_open)
  5299. iter->trace->pipe_open(iter);
  5300. nonseekable_open(inode, filp);
  5301. tr->trace_ref++;
  5302. mutex_unlock(&trace_types_lock);
  5303. return ret;
  5304. fail:
  5305. kfree(iter);
  5306. fail_alloc_iter:
  5307. close_pipe_on_cpu(tr, cpu);
  5308. fail_pipe_on_cpu:
  5309. __trace_array_put(tr);
  5310. mutex_unlock(&trace_types_lock);
  5311. return ret;
  5312. }
  5313. static int tracing_release_pipe(struct inode *inode, struct file *file)
  5314. {
  5315. struct trace_iterator *iter = file->private_data;
  5316. struct trace_array *tr = inode->i_private;
  5317. mutex_lock(&trace_types_lock);
  5318. tr->trace_ref--;
  5319. if (iter->trace->pipe_close)
  5320. iter->trace->pipe_close(iter);
  5321. close_pipe_on_cpu(tr, iter->cpu_file);
  5322. mutex_unlock(&trace_types_lock);
  5323. free_trace_iter_content(iter);
  5324. kfree(iter);
  5325. trace_array_put(tr);
  5326. return 0;
  5327. }
  5328. static __poll_t
  5329. trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
  5330. {
  5331. struct trace_array *tr = iter->tr;
  5332. /* Iterators are static, they should be filled or empty */
  5333. if (trace_buffer_iter(iter, iter->cpu_file))
  5334. return EPOLLIN | EPOLLRDNORM;
  5335. if (tr->trace_flags & TRACE_ITER_BLOCK)
  5336. /*
  5337. * Always select as readable when in blocking mode
  5338. */
  5339. return EPOLLIN | EPOLLRDNORM;
  5340. else
  5341. return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
  5342. filp, poll_table, iter->tr->buffer_percent);
  5343. }
  5344. static __poll_t
  5345. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  5346. {
  5347. struct trace_iterator *iter = filp->private_data;
  5348. return trace_poll(iter, filp, poll_table);
  5349. }
  5350. /* Must be called with iter->mutex held. */
  5351. static int tracing_wait_pipe(struct file *filp)
  5352. {
  5353. struct trace_iterator *iter = filp->private_data;
  5354. int ret;
  5355. while (trace_empty(iter)) {
  5356. if ((filp->f_flags & O_NONBLOCK)) {
  5357. return -EAGAIN;
  5358. }
  5359. /*
  5360. * We block until we read something and tracing is disabled.
  5361. * We still block if tracing is disabled, but we have never
  5362. * read anything. This allows a user to cat this file, and
  5363. * then enable tracing. But after we have read something,
  5364. * we give an EOF when tracing is again disabled.
  5365. *
  5366. * iter->pos will be 0 if we haven't read anything.
  5367. */
  5368. if (!tracer_tracing_is_on(iter->tr) && iter->pos)
  5369. break;
  5370. mutex_unlock(&iter->mutex);
  5371. ret = wait_on_pipe(iter, 0);
  5372. mutex_lock(&iter->mutex);
  5373. if (ret)
  5374. return ret;
  5375. }
  5376. return 1;
  5377. }
  5378. /*
  5379. * Consumer reader.
  5380. */
  5381. static ssize_t
  5382. tracing_read_pipe(struct file *filp, char __user *ubuf,
  5383. size_t cnt, loff_t *ppos)
  5384. {
  5385. struct trace_iterator *iter = filp->private_data;
  5386. ssize_t sret;
  5387. /*
  5388. * Avoid more than one consumer on a single file descriptor
  5389. * This is just a matter of traces coherency, the ring buffer itself
  5390. * is protected.
  5391. */
  5392. guard(mutex)(&iter->mutex);
  5393. /* return any leftover data */
  5394. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  5395. if (sret != -EBUSY)
  5396. return sret;
  5397. trace_seq_init(&iter->seq);
  5398. if (iter->trace->read) {
  5399. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  5400. if (sret)
  5401. return sret;
  5402. }
  5403. waitagain:
  5404. sret = tracing_wait_pipe(filp);
  5405. if (sret <= 0)
  5406. return sret;
  5407. /* stop when tracing is finished */
  5408. if (trace_empty(iter))
  5409. return 0;
  5410. if (cnt >= TRACE_SEQ_BUFFER_SIZE)
  5411. cnt = TRACE_SEQ_BUFFER_SIZE - 1;
  5412. /* reset all but tr, trace, and overruns */
  5413. trace_iterator_reset(iter);
  5414. cpumask_clear(iter->started);
  5415. trace_seq_init(&iter->seq);
  5416. trace_event_read_lock();
  5417. trace_access_lock(iter->cpu_file);
  5418. while (trace_find_next_entry_inc(iter) != NULL) {
  5419. enum print_line_t ret;
  5420. int save_len = iter->seq.seq.len;
  5421. ret = print_trace_line(iter);
  5422. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  5423. /*
  5424. * If one print_trace_line() fills entire trace_seq in one shot,
  5425. * trace_seq_to_user() will returns -EBUSY because save_len == 0,
  5426. * In this case, we need to consume it, otherwise, loop will peek
  5427. * this event next time, resulting in an infinite loop.
  5428. */
  5429. if (save_len == 0) {
  5430. iter->seq.full = 0;
  5431. trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
  5432. trace_consume(iter);
  5433. break;
  5434. }
  5435. /* In other cases, don't print partial lines */
  5436. iter->seq.seq.len = save_len;
  5437. break;
  5438. }
  5439. if (ret != TRACE_TYPE_NO_CONSUME)
  5440. trace_consume(iter);
  5441. if (trace_seq_used(&iter->seq) >= cnt)
  5442. break;
  5443. /*
  5444. * Setting the full flag means we reached the trace_seq buffer
  5445. * size and we should leave by partial output condition above.
  5446. * One of the trace_seq_* functions is not used properly.
  5447. */
  5448. WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
  5449. iter->ent->type);
  5450. }
  5451. trace_access_unlock(iter->cpu_file);
  5452. trace_event_read_unlock();
  5453. /* Now copy what we have to the user */
  5454. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  5455. if (iter->seq.readpos >= trace_seq_used(&iter->seq))
  5456. trace_seq_init(&iter->seq);
  5457. /*
  5458. * If there was nothing to send to user, in spite of consuming trace
  5459. * entries, go back to wait for more entries.
  5460. */
  5461. if (sret == -EBUSY)
  5462. goto waitagain;
  5463. return sret;
  5464. }
  5465. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  5466. unsigned int idx)
  5467. {
  5468. __free_page(spd->pages[idx]);
  5469. }
  5470. static size_t
  5471. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  5472. {
  5473. size_t count;
  5474. int save_len;
  5475. int ret;
  5476. /* Seq buffer is page-sized, exactly what we need. */
  5477. for (;;) {
  5478. save_len = iter->seq.seq.len;
  5479. ret = print_trace_line(iter);
  5480. if (trace_seq_has_overflowed(&iter->seq)) {
  5481. iter->seq.seq.len = save_len;
  5482. break;
  5483. }
  5484. /*
  5485. * This should not be hit, because it should only
  5486. * be set if the iter->seq overflowed. But check it
  5487. * anyway to be safe.
  5488. */
  5489. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  5490. iter->seq.seq.len = save_len;
  5491. break;
  5492. }
  5493. count = trace_seq_used(&iter->seq) - save_len;
  5494. if (rem < count) {
  5495. rem = 0;
  5496. iter->seq.seq.len = save_len;
  5497. break;
  5498. }
  5499. if (ret != TRACE_TYPE_NO_CONSUME)
  5500. trace_consume(iter);
  5501. rem -= count;
  5502. if (!trace_find_next_entry_inc(iter)) {
  5503. rem = 0;
  5504. iter->ent = NULL;
  5505. break;
  5506. }
  5507. }
  5508. return rem;
  5509. }
  5510. static ssize_t tracing_splice_read_pipe(struct file *filp,
  5511. loff_t *ppos,
  5512. struct pipe_inode_info *pipe,
  5513. size_t len,
  5514. unsigned int flags)
  5515. {
  5516. struct page *pages_def[PIPE_DEF_BUFFERS];
  5517. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  5518. struct trace_iterator *iter = filp->private_data;
  5519. struct splice_pipe_desc spd = {
  5520. .pages = pages_def,
  5521. .partial = partial_def,
  5522. .nr_pages = 0, /* This gets updated below. */
  5523. .nr_pages_max = PIPE_DEF_BUFFERS,
  5524. .ops = &default_pipe_buf_ops,
  5525. .spd_release = tracing_spd_release_pipe,
  5526. };
  5527. ssize_t ret;
  5528. size_t rem;
  5529. unsigned int i;
  5530. if (splice_grow_spd(pipe, &spd))
  5531. return -ENOMEM;
  5532. mutex_lock(&iter->mutex);
  5533. if (iter->trace->splice_read) {
  5534. ret = iter->trace->splice_read(iter, filp,
  5535. ppos, pipe, len, flags);
  5536. if (ret)
  5537. goto out_err;
  5538. }
  5539. ret = tracing_wait_pipe(filp);
  5540. if (ret <= 0)
  5541. goto out_err;
  5542. if (!iter->ent && !trace_find_next_entry_inc(iter)) {
  5543. ret = -EFAULT;
  5544. goto out_err;
  5545. }
  5546. trace_event_read_lock();
  5547. trace_access_lock(iter->cpu_file);
  5548. /* Fill as many pages as possible. */
  5549. for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
  5550. spd.pages[i] = alloc_page(GFP_KERNEL);
  5551. if (!spd.pages[i])
  5552. break;
  5553. rem = tracing_fill_pipe_page(rem, iter);
  5554. /* Copy the data into the page, so we can start over. */
  5555. ret = trace_seq_to_buffer(&iter->seq,
  5556. page_address(spd.pages[i]),
  5557. min((size_t)trace_seq_used(&iter->seq),
  5558. (size_t)PAGE_SIZE));
  5559. if (ret < 0) {
  5560. __free_page(spd.pages[i]);
  5561. break;
  5562. }
  5563. spd.partial[i].offset = 0;
  5564. spd.partial[i].len = ret;
  5565. trace_seq_init(&iter->seq);
  5566. }
  5567. trace_access_unlock(iter->cpu_file);
  5568. trace_event_read_unlock();
  5569. mutex_unlock(&iter->mutex);
  5570. spd.nr_pages = i;
  5571. if (i)
  5572. ret = splice_to_pipe(pipe, &spd);
  5573. else
  5574. ret = 0;
  5575. out:
  5576. splice_shrink_spd(&spd);
  5577. return ret;
  5578. out_err:
  5579. mutex_unlock(&iter->mutex);
  5580. goto out;
  5581. }
  5582. static ssize_t
  5583. tracing_entries_read(struct file *filp, char __user *ubuf,
  5584. size_t cnt, loff_t *ppos)
  5585. {
  5586. struct inode *inode = file_inode(filp);
  5587. struct trace_array *tr = inode->i_private;
  5588. int cpu = tracing_get_cpu(inode);
  5589. char buf[64];
  5590. int r = 0;
  5591. ssize_t ret;
  5592. mutex_lock(&trace_types_lock);
  5593. if (cpu == RING_BUFFER_ALL_CPUS) {
  5594. int cpu, buf_size_same;
  5595. unsigned long size;
  5596. size = 0;
  5597. buf_size_same = 1;
  5598. /* check if all cpu sizes are same */
  5599. for_each_tracing_cpu(cpu) {
  5600. /* fill in the size from first enabled cpu */
  5601. if (size == 0)
  5602. size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
  5603. if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
  5604. buf_size_same = 0;
  5605. break;
  5606. }
  5607. }
  5608. if (buf_size_same) {
  5609. if (!tr->ring_buffer_expanded)
  5610. r = sprintf(buf, "%lu (expanded: %lu)\n",
  5611. size >> 10,
  5612. trace_buf_size >> 10);
  5613. else
  5614. r = sprintf(buf, "%lu\n", size >> 10);
  5615. } else
  5616. r = sprintf(buf, "X\n");
  5617. } else
  5618. r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
  5619. mutex_unlock(&trace_types_lock);
  5620. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  5621. return ret;
  5622. }
  5623. static ssize_t
  5624. tracing_entries_write(struct file *filp, const char __user *ubuf,
  5625. size_t cnt, loff_t *ppos)
  5626. {
  5627. struct inode *inode = file_inode(filp);
  5628. struct trace_array *tr = inode->i_private;
  5629. unsigned long val;
  5630. int ret;
  5631. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  5632. if (ret)
  5633. return ret;
  5634. /* must have at least 1 entry */
  5635. if (!val)
  5636. return -EINVAL;
  5637. /* value is in KB */
  5638. val <<= 10;
  5639. ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
  5640. if (ret < 0)
  5641. return ret;
  5642. *ppos += cnt;
  5643. return cnt;
  5644. }
  5645. static ssize_t
  5646. tracing_total_entries_read(struct file *filp, char __user *ubuf,
  5647. size_t cnt, loff_t *ppos)
  5648. {
  5649. struct trace_array *tr = filp->private_data;
  5650. char buf[64];
  5651. int r, cpu;
  5652. unsigned long size = 0, expanded_size = 0;
  5653. mutex_lock(&trace_types_lock);
  5654. for_each_tracing_cpu(cpu) {
  5655. size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
  5656. if (!tr->ring_buffer_expanded)
  5657. expanded_size += trace_buf_size >> 10;
  5658. }
  5659. if (tr->ring_buffer_expanded)
  5660. r = sprintf(buf, "%lu\n", size);
  5661. else
  5662. r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
  5663. mutex_unlock(&trace_types_lock);
  5664. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  5665. }
  5666. static ssize_t
  5667. tracing_last_boot_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  5668. {
  5669. struct trace_array *tr = filp->private_data;
  5670. struct seq_buf seq;
  5671. char buf[64];
  5672. seq_buf_init(&seq, buf, 64);
  5673. seq_buf_printf(&seq, "text delta:\t%ld\n", tr->text_delta);
  5674. seq_buf_printf(&seq, "data delta:\t%ld\n", tr->data_delta);
  5675. return simple_read_from_buffer(ubuf, cnt, ppos, buf, seq_buf_used(&seq));
  5676. }
  5677. static int tracing_buffer_meta_open(struct inode *inode, struct file *filp)
  5678. {
  5679. struct trace_array *tr = inode->i_private;
  5680. int cpu = tracing_get_cpu(inode);
  5681. int ret;
  5682. ret = tracing_check_open_get_tr(tr);
  5683. if (ret)
  5684. return ret;
  5685. ret = ring_buffer_meta_seq_init(filp, tr->array_buffer.buffer, cpu);
  5686. if (ret < 0)
  5687. __trace_array_put(tr);
  5688. return ret;
  5689. }
  5690. static ssize_t
  5691. tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
  5692. size_t cnt, loff_t *ppos)
  5693. {
  5694. /*
  5695. * There is no need to read what the user has written, this function
  5696. * is just to make sure that there is no error when "echo" is used
  5697. */
  5698. *ppos += cnt;
  5699. return cnt;
  5700. }
  5701. static int
  5702. tracing_free_buffer_release(struct inode *inode, struct file *filp)
  5703. {
  5704. struct trace_array *tr = inode->i_private;
  5705. /* disable tracing ? */
  5706. if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
  5707. tracer_tracing_off(tr);
  5708. /* resize the ring buffer to 0 */
  5709. tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
  5710. trace_array_put(tr);
  5711. return 0;
  5712. }
  5713. #define TRACE_MARKER_MAX_SIZE 4096
  5714. static ssize_t
  5715. tracing_mark_write(struct file *filp, const char __user *ubuf,
  5716. size_t cnt, loff_t *fpos)
  5717. {
  5718. struct trace_array *tr = filp->private_data;
  5719. struct ring_buffer_event *event;
  5720. enum event_trigger_type tt = ETT_NONE;
  5721. struct trace_buffer *buffer;
  5722. struct print_entry *entry;
  5723. int meta_size;
  5724. ssize_t written;
  5725. size_t size;
  5726. int len;
  5727. /* Used in tracing_mark_raw_write() as well */
  5728. #define FAULTED_STR "<faulted>"
  5729. #define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
  5730. if (tracing_disabled)
  5731. return -EINVAL;
  5732. if (!(tr->trace_flags & TRACE_ITER_MARKERS))
  5733. return -EINVAL;
  5734. if ((ssize_t)cnt < 0)
  5735. return -EINVAL;
  5736. if (cnt > TRACE_MARKER_MAX_SIZE)
  5737. cnt = TRACE_MARKER_MAX_SIZE;
  5738. meta_size = sizeof(*entry) + 2; /* add '\0' and possible '\n' */
  5739. again:
  5740. size = cnt + meta_size;
  5741. /* If less than "<faulted>", then make sure we can still add that */
  5742. if (cnt < FAULTED_SIZE)
  5743. size += FAULTED_SIZE - cnt;
  5744. buffer = tr->array_buffer.buffer;
  5745. event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  5746. tracing_gen_ctx());
  5747. if (unlikely(!event)) {
  5748. /*
  5749. * If the size was greater than what was allowed, then
  5750. * make it smaller and try again.
  5751. */
  5752. if (size > ring_buffer_max_event_size(buffer)) {
  5753. /* cnt < FAULTED size should never be bigger than max */
  5754. if (WARN_ON_ONCE(cnt < FAULTED_SIZE))
  5755. return -EBADF;
  5756. cnt = ring_buffer_max_event_size(buffer) - meta_size;
  5757. /* The above should only happen once */
  5758. if (WARN_ON_ONCE(cnt + meta_size == size))
  5759. return -EBADF;
  5760. goto again;
  5761. }
  5762. /* Ring buffer disabled, return as if not open for write */
  5763. return -EBADF;
  5764. }
  5765. entry = ring_buffer_event_data(event);
  5766. entry->ip = _THIS_IP_;
  5767. len = copy_from_user_nofault(&entry->buf, ubuf, cnt);
  5768. if (len) {
  5769. memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
  5770. cnt = FAULTED_SIZE;
  5771. written = -EFAULT;
  5772. } else
  5773. written = cnt;
  5774. if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
  5775. /* do not add \n before testing triggers, but add \0 */
  5776. entry->buf[cnt] = '\0';
  5777. tt = event_triggers_call(tr->trace_marker_file, buffer, entry, event);
  5778. }
  5779. if (entry->buf[cnt - 1] != '\n') {
  5780. entry->buf[cnt] = '\n';
  5781. entry->buf[cnt + 1] = '\0';
  5782. } else
  5783. entry->buf[cnt] = '\0';
  5784. if (static_branch_unlikely(&trace_marker_exports_enabled))
  5785. ftrace_exports(event, TRACE_EXPORT_MARKER);
  5786. __buffer_unlock_commit(buffer, event);
  5787. if (tt)
  5788. event_triggers_post_call(tr->trace_marker_file, tt);
  5789. return written;
  5790. }
  5791. static ssize_t
  5792. tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
  5793. size_t cnt, loff_t *fpos)
  5794. {
  5795. struct trace_array *tr = filp->private_data;
  5796. struct ring_buffer_event *event;
  5797. struct trace_buffer *buffer;
  5798. struct raw_data_entry *entry;
  5799. ssize_t written;
  5800. int size;
  5801. int len;
  5802. #define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
  5803. if (tracing_disabled)
  5804. return -EINVAL;
  5805. if (!(tr->trace_flags & TRACE_ITER_MARKERS))
  5806. return -EINVAL;
  5807. /* The marker must at least have a tag id */
  5808. if (cnt < sizeof(unsigned int))
  5809. return -EINVAL;
  5810. size = sizeof(*entry) + cnt;
  5811. if (cnt < FAULT_SIZE_ID)
  5812. size += FAULT_SIZE_ID - cnt;
  5813. buffer = tr->array_buffer.buffer;
  5814. if (size > ring_buffer_max_event_size(buffer))
  5815. return -EINVAL;
  5816. event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
  5817. tracing_gen_ctx());
  5818. if (!event)
  5819. /* Ring buffer disabled, return as if not open for write */
  5820. return -EBADF;
  5821. entry = ring_buffer_event_data(event);
  5822. len = copy_from_user_nofault(&entry->id, ubuf, cnt);
  5823. if (len) {
  5824. entry->id = -1;
  5825. memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
  5826. written = -EFAULT;
  5827. } else
  5828. written = cnt;
  5829. __buffer_unlock_commit(buffer, event);
  5830. return written;
  5831. }
  5832. static int tracing_clock_show(struct seq_file *m, void *v)
  5833. {
  5834. struct trace_array *tr = m->private;
  5835. int i;
  5836. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
  5837. seq_printf(m,
  5838. "%s%s%s%s", i ? " " : "",
  5839. i == tr->clock_id ? "[" : "", trace_clocks[i].name,
  5840. i == tr->clock_id ? "]" : "");
  5841. seq_putc(m, '\n');
  5842. return 0;
  5843. }
  5844. int tracing_set_clock(struct trace_array *tr, const char *clockstr)
  5845. {
  5846. int i;
  5847. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
  5848. if (strcmp(trace_clocks[i].name, clockstr) == 0)
  5849. break;
  5850. }
  5851. if (i == ARRAY_SIZE(trace_clocks))
  5852. return -EINVAL;
  5853. mutex_lock(&trace_types_lock);
  5854. tr->clock_id = i;
  5855. ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
  5856. /*
  5857. * New clock may not be consistent with the previous clock.
  5858. * Reset the buffer so that it doesn't have incomparable timestamps.
  5859. */
  5860. tracing_reset_online_cpus(&tr->array_buffer);
  5861. #ifdef CONFIG_TRACER_MAX_TRACE
  5862. if (tr->max_buffer.buffer)
  5863. ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
  5864. tracing_reset_online_cpus(&tr->max_buffer);
  5865. #endif
  5866. mutex_unlock(&trace_types_lock);
  5867. return 0;
  5868. }
  5869. static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
  5870. size_t cnt, loff_t *fpos)
  5871. {
  5872. struct seq_file *m = filp->private_data;
  5873. struct trace_array *tr = m->private;
  5874. char buf[64];
  5875. const char *clockstr;
  5876. int ret;
  5877. if (cnt >= sizeof(buf))
  5878. return -EINVAL;
  5879. if (copy_from_user(buf, ubuf, cnt))
  5880. return -EFAULT;
  5881. buf[cnt] = 0;
  5882. clockstr = strstrip(buf);
  5883. ret = tracing_set_clock(tr, clockstr);
  5884. if (ret)
  5885. return ret;
  5886. *fpos += cnt;
  5887. return cnt;
  5888. }
  5889. static int tracing_clock_open(struct inode *inode, struct file *file)
  5890. {
  5891. struct trace_array *tr = inode->i_private;
  5892. int ret;
  5893. ret = tracing_check_open_get_tr(tr);
  5894. if (ret)
  5895. return ret;
  5896. ret = single_open(file, tracing_clock_show, inode->i_private);
  5897. if (ret < 0)
  5898. trace_array_put(tr);
  5899. return ret;
  5900. }
  5901. static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
  5902. {
  5903. struct trace_array *tr = m->private;
  5904. mutex_lock(&trace_types_lock);
  5905. if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
  5906. seq_puts(m, "delta [absolute]\n");
  5907. else
  5908. seq_puts(m, "[delta] absolute\n");
  5909. mutex_unlock(&trace_types_lock);
  5910. return 0;
  5911. }
  5912. static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
  5913. {
  5914. struct trace_array *tr = inode->i_private;
  5915. int ret;
  5916. ret = tracing_check_open_get_tr(tr);
  5917. if (ret)
  5918. return ret;
  5919. ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
  5920. if (ret < 0)
  5921. trace_array_put(tr);
  5922. return ret;
  5923. }
  5924. u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_event *rbe)
  5925. {
  5926. if (rbe == this_cpu_read(trace_buffered_event))
  5927. return ring_buffer_time_stamp(buffer);
  5928. return ring_buffer_event_time_stamp(buffer, rbe);
  5929. }
  5930. /*
  5931. * Set or disable using the per CPU trace_buffer_event when possible.
  5932. */
  5933. int tracing_set_filter_buffering(struct trace_array *tr, bool set)
  5934. {
  5935. guard(mutex)(&trace_types_lock);
  5936. if (set && tr->no_filter_buffering_ref++)
  5937. return 0;
  5938. if (!set) {
  5939. if (WARN_ON_ONCE(!tr->no_filter_buffering_ref))
  5940. return -EINVAL;
  5941. --tr->no_filter_buffering_ref;
  5942. }
  5943. return 0;
  5944. }
  5945. struct ftrace_buffer_info {
  5946. struct trace_iterator iter;
  5947. void *spare;
  5948. unsigned int spare_cpu;
  5949. unsigned int spare_size;
  5950. unsigned int read;
  5951. };
  5952. #ifdef CONFIG_TRACER_SNAPSHOT
  5953. static int tracing_snapshot_open(struct inode *inode, struct file *file)
  5954. {
  5955. struct trace_array *tr = inode->i_private;
  5956. struct trace_iterator *iter;
  5957. struct seq_file *m;
  5958. int ret;
  5959. ret = tracing_check_open_get_tr(tr);
  5960. if (ret)
  5961. return ret;
  5962. if (file->f_mode & FMODE_READ) {
  5963. iter = __tracing_open(inode, file, true);
  5964. if (IS_ERR(iter))
  5965. ret = PTR_ERR(iter);
  5966. } else {
  5967. /* Writes still need the seq_file to hold the private data */
  5968. ret = -ENOMEM;
  5969. m = kzalloc(sizeof(*m), GFP_KERNEL);
  5970. if (!m)
  5971. goto out;
  5972. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  5973. if (!iter) {
  5974. kfree(m);
  5975. goto out;
  5976. }
  5977. ret = 0;
  5978. iter->tr = tr;
  5979. iter->array_buffer = &tr->max_buffer;
  5980. iter->cpu_file = tracing_get_cpu(inode);
  5981. m->private = iter;
  5982. file->private_data = m;
  5983. }
  5984. out:
  5985. if (ret < 0)
  5986. trace_array_put(tr);
  5987. return ret;
  5988. }
  5989. static void tracing_swap_cpu_buffer(void *tr)
  5990. {
  5991. update_max_tr_single((struct trace_array *)tr, current, smp_processor_id());
  5992. }
  5993. static ssize_t
  5994. tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
  5995. loff_t *ppos)
  5996. {
  5997. struct seq_file *m = filp->private_data;
  5998. struct trace_iterator *iter = m->private;
  5999. struct trace_array *tr = iter->tr;
  6000. unsigned long val;
  6001. int ret;
  6002. ret = tracing_update_buffers(tr);
  6003. if (ret < 0)
  6004. return ret;
  6005. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  6006. if (ret)
  6007. return ret;
  6008. guard(mutex)(&trace_types_lock);
  6009. if (tr->current_trace->use_max_tr)
  6010. return -EBUSY;
  6011. local_irq_disable();
  6012. arch_spin_lock(&tr->max_lock);
  6013. if (tr->cond_snapshot)
  6014. ret = -EBUSY;
  6015. arch_spin_unlock(&tr->max_lock);
  6016. local_irq_enable();
  6017. if (ret)
  6018. return ret;
  6019. switch (val) {
  6020. case 0:
  6021. if (iter->cpu_file != RING_BUFFER_ALL_CPUS)
  6022. return -EINVAL;
  6023. if (tr->allocated_snapshot)
  6024. free_snapshot(tr);
  6025. break;
  6026. case 1:
  6027. /* Only allow per-cpu swap if the ring buffer supports it */
  6028. #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
  6029. if (iter->cpu_file != RING_BUFFER_ALL_CPUS)
  6030. return -EINVAL;
  6031. #endif
  6032. if (tr->allocated_snapshot)
  6033. ret = resize_buffer_duplicate_size(&tr->max_buffer,
  6034. &tr->array_buffer, iter->cpu_file);
  6035. ret = tracing_arm_snapshot_locked(tr);
  6036. if (ret)
  6037. return ret;
  6038. /* Now, we're going to swap */
  6039. if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
  6040. local_irq_disable();
  6041. update_max_tr(tr, current, smp_processor_id(), NULL);
  6042. local_irq_enable();
  6043. } else {
  6044. smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer,
  6045. (void *)tr, 1);
  6046. }
  6047. tracing_disarm_snapshot(tr);
  6048. break;
  6049. default:
  6050. if (tr->allocated_snapshot) {
  6051. if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
  6052. tracing_reset_online_cpus(&tr->max_buffer);
  6053. else
  6054. tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
  6055. }
  6056. break;
  6057. }
  6058. if (ret >= 0) {
  6059. *ppos += cnt;
  6060. ret = cnt;
  6061. }
  6062. return ret;
  6063. }
  6064. static int tracing_snapshot_release(struct inode *inode, struct file *file)
  6065. {
  6066. struct seq_file *m = file->private_data;
  6067. int ret;
  6068. ret = tracing_release(inode, file);
  6069. if (file->f_mode & FMODE_READ)
  6070. return ret;
  6071. /* If write only, the seq_file is just a stub */
  6072. if (m)
  6073. kfree(m->private);
  6074. kfree(m);
  6075. return 0;
  6076. }
  6077. static int tracing_buffers_open(struct inode *inode, struct file *filp);
  6078. static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
  6079. size_t count, loff_t *ppos);
  6080. static int tracing_buffers_release(struct inode *inode, struct file *file);
  6081. static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  6082. struct pipe_inode_info *pipe, size_t len, unsigned int flags);
  6083. static int snapshot_raw_open(struct inode *inode, struct file *filp)
  6084. {
  6085. struct ftrace_buffer_info *info;
  6086. int ret;
  6087. /* The following checks for tracefs lockdown */
  6088. ret = tracing_buffers_open(inode, filp);
  6089. if (ret < 0)
  6090. return ret;
  6091. info = filp->private_data;
  6092. if (info->iter.trace->use_max_tr) {
  6093. tracing_buffers_release(inode, filp);
  6094. return -EBUSY;
  6095. }
  6096. info->iter.snapshot = true;
  6097. info->iter.array_buffer = &info->iter.tr->max_buffer;
  6098. return ret;
  6099. }
  6100. #endif /* CONFIG_TRACER_SNAPSHOT */
  6101. static const struct file_operations tracing_thresh_fops = {
  6102. .open = tracing_open_generic,
  6103. .read = tracing_thresh_read,
  6104. .write = tracing_thresh_write,
  6105. .llseek = generic_file_llseek,
  6106. };
  6107. #ifdef CONFIG_TRACER_MAX_TRACE
  6108. static const struct file_operations tracing_max_lat_fops = {
  6109. .open = tracing_open_generic_tr,
  6110. .read = tracing_max_lat_read,
  6111. .write = tracing_max_lat_write,
  6112. .llseek = generic_file_llseek,
  6113. .release = tracing_release_generic_tr,
  6114. };
  6115. #endif
  6116. static const struct file_operations set_tracer_fops = {
  6117. .open = tracing_open_generic_tr,
  6118. .read = tracing_set_trace_read,
  6119. .write = tracing_set_trace_write,
  6120. .llseek = generic_file_llseek,
  6121. .release = tracing_release_generic_tr,
  6122. };
  6123. static const struct file_operations tracing_pipe_fops = {
  6124. .open = tracing_open_pipe,
  6125. .poll = tracing_poll_pipe,
  6126. .read = tracing_read_pipe,
  6127. .splice_read = tracing_splice_read_pipe,
  6128. .release = tracing_release_pipe,
  6129. };
  6130. static const struct file_operations tracing_entries_fops = {
  6131. .open = tracing_open_generic_tr,
  6132. .read = tracing_entries_read,
  6133. .write = tracing_entries_write,
  6134. .llseek = generic_file_llseek,
  6135. .release = tracing_release_generic_tr,
  6136. };
  6137. static const struct file_operations tracing_buffer_meta_fops = {
  6138. .open = tracing_buffer_meta_open,
  6139. .read = seq_read,
  6140. .llseek = seq_lseek,
  6141. .release = tracing_seq_release,
  6142. };
  6143. static const struct file_operations tracing_total_entries_fops = {
  6144. .open = tracing_open_generic_tr,
  6145. .read = tracing_total_entries_read,
  6146. .llseek = generic_file_llseek,
  6147. .release = tracing_release_generic_tr,
  6148. };
  6149. static const struct file_operations tracing_free_buffer_fops = {
  6150. .open = tracing_open_generic_tr,
  6151. .write = tracing_free_buffer_write,
  6152. .release = tracing_free_buffer_release,
  6153. };
  6154. static const struct file_operations tracing_mark_fops = {
  6155. .open = tracing_mark_open,
  6156. .write = tracing_mark_write,
  6157. .release = tracing_release_generic_tr,
  6158. };
  6159. static const struct file_operations tracing_mark_raw_fops = {
  6160. .open = tracing_mark_open,
  6161. .write = tracing_mark_raw_write,
  6162. .release = tracing_release_generic_tr,
  6163. };
  6164. static const struct file_operations trace_clock_fops = {
  6165. .open = tracing_clock_open,
  6166. .read = seq_read,
  6167. .llseek = seq_lseek,
  6168. .release = tracing_single_release_tr,
  6169. .write = tracing_clock_write,
  6170. };
  6171. static const struct file_operations trace_time_stamp_mode_fops = {
  6172. .open = tracing_time_stamp_mode_open,
  6173. .read = seq_read,
  6174. .llseek = seq_lseek,
  6175. .release = tracing_single_release_tr,
  6176. };
  6177. static const struct file_operations last_boot_fops = {
  6178. .open = tracing_open_generic_tr,
  6179. .read = tracing_last_boot_read,
  6180. .llseek = generic_file_llseek,
  6181. .release = tracing_release_generic_tr,
  6182. };
  6183. #ifdef CONFIG_TRACER_SNAPSHOT
  6184. static const struct file_operations snapshot_fops = {
  6185. .open = tracing_snapshot_open,
  6186. .read = seq_read,
  6187. .write = tracing_snapshot_write,
  6188. .llseek = tracing_lseek,
  6189. .release = tracing_snapshot_release,
  6190. };
  6191. static const struct file_operations snapshot_raw_fops = {
  6192. .open = snapshot_raw_open,
  6193. .read = tracing_buffers_read,
  6194. .release = tracing_buffers_release,
  6195. .splice_read = tracing_buffers_splice_read,
  6196. };
  6197. #endif /* CONFIG_TRACER_SNAPSHOT */
  6198. /*
  6199. * trace_min_max_write - Write a u64 value to a trace_min_max_param struct
  6200. * @filp: The active open file structure
  6201. * @ubuf: The userspace provided buffer to read value into
  6202. * @cnt: The maximum number of bytes to read
  6203. * @ppos: The current "file" position
  6204. *
  6205. * This function implements the write interface for a struct trace_min_max_param.
  6206. * The filp->private_data must point to a trace_min_max_param structure that
  6207. * defines where to write the value, the min and the max acceptable values,
  6208. * and a lock to protect the write.
  6209. */
  6210. static ssize_t
  6211. trace_min_max_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos)
  6212. {
  6213. struct trace_min_max_param *param = filp->private_data;
  6214. u64 val;
  6215. int err;
  6216. if (!param)
  6217. return -EFAULT;
  6218. err = kstrtoull_from_user(ubuf, cnt, 10, &val);
  6219. if (err)
  6220. return err;
  6221. if (param->lock)
  6222. mutex_lock(param->lock);
  6223. if (param->min && val < *param->min)
  6224. err = -EINVAL;
  6225. if (param->max && val > *param->max)
  6226. err = -EINVAL;
  6227. if (!err)
  6228. *param->val = val;
  6229. if (param->lock)
  6230. mutex_unlock(param->lock);
  6231. if (err)
  6232. return err;
  6233. return cnt;
  6234. }
  6235. /*
  6236. * trace_min_max_read - Read a u64 value from a trace_min_max_param struct
  6237. * @filp: The active open file structure
  6238. * @ubuf: The userspace provided buffer to read value into
  6239. * @cnt: The maximum number of bytes to read
  6240. * @ppos: The current "file" position
  6241. *
  6242. * This function implements the read interface for a struct trace_min_max_param.
  6243. * The filp->private_data must point to a trace_min_max_param struct with valid
  6244. * data.
  6245. */
  6246. static ssize_t
  6247. trace_min_max_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  6248. {
  6249. struct trace_min_max_param *param = filp->private_data;
  6250. char buf[U64_STR_SIZE];
  6251. int len;
  6252. u64 val;
  6253. if (!param)
  6254. return -EFAULT;
  6255. val = *param->val;
  6256. if (cnt > sizeof(buf))
  6257. cnt = sizeof(buf);
  6258. len = snprintf(buf, sizeof(buf), "%llu\n", val);
  6259. return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  6260. }
  6261. const struct file_operations trace_min_max_fops = {
  6262. .open = tracing_open_generic,
  6263. .read = trace_min_max_read,
  6264. .write = trace_min_max_write,
  6265. };
  6266. #define TRACING_LOG_ERRS_MAX 8
  6267. #define TRACING_LOG_LOC_MAX 128
  6268. #define CMD_PREFIX " Command: "
  6269. struct err_info {
  6270. const char **errs; /* ptr to loc-specific array of err strings */
  6271. u8 type; /* index into errs -> specific err string */
  6272. u16 pos; /* caret position */
  6273. u64 ts;
  6274. };
  6275. struct tracing_log_err {
  6276. struct list_head list;
  6277. struct err_info info;
  6278. char loc[TRACING_LOG_LOC_MAX]; /* err location */
  6279. char *cmd; /* what caused err */
  6280. };
  6281. static DEFINE_MUTEX(tracing_err_log_lock);
  6282. static struct tracing_log_err *alloc_tracing_log_err(int len)
  6283. {
  6284. struct tracing_log_err *err;
  6285. err = kzalloc(sizeof(*err), GFP_KERNEL);
  6286. if (!err)
  6287. return ERR_PTR(-ENOMEM);
  6288. err->cmd = kzalloc(len, GFP_KERNEL);
  6289. if (!err->cmd) {
  6290. kfree(err);
  6291. return ERR_PTR(-ENOMEM);
  6292. }
  6293. return err;
  6294. }
  6295. static void free_tracing_log_err(struct tracing_log_err *err)
  6296. {
  6297. kfree(err->cmd);
  6298. kfree(err);
  6299. }
  6300. static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr,
  6301. int len)
  6302. {
  6303. struct tracing_log_err *err;
  6304. char *cmd;
  6305. if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
  6306. err = alloc_tracing_log_err(len);
  6307. if (PTR_ERR(err) != -ENOMEM)
  6308. tr->n_err_log_entries++;
  6309. return err;
  6310. }
  6311. cmd = kzalloc(len, GFP_KERNEL);
  6312. if (!cmd)
  6313. return ERR_PTR(-ENOMEM);
  6314. err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
  6315. kfree(err->cmd);
  6316. err->cmd = cmd;
  6317. list_del(&err->list);
  6318. return err;
  6319. }
  6320. /**
  6321. * err_pos - find the position of a string within a command for error careting
  6322. * @cmd: The tracing command that caused the error
  6323. * @str: The string to position the caret at within @cmd
  6324. *
  6325. * Finds the position of the first occurrence of @str within @cmd. The
  6326. * return value can be passed to tracing_log_err() for caret placement
  6327. * within @cmd.
  6328. *
  6329. * Returns the index within @cmd of the first occurrence of @str or 0
  6330. * if @str was not found.
  6331. */
  6332. unsigned int err_pos(char *cmd, const char *str)
  6333. {
  6334. char *found;
  6335. if (WARN_ON(!strlen(cmd)))
  6336. return 0;
  6337. found = strstr(cmd, str);
  6338. if (found)
  6339. return found - cmd;
  6340. return 0;
  6341. }
  6342. /**
  6343. * tracing_log_err - write an error to the tracing error log
  6344. * @tr: The associated trace array for the error (NULL for top level array)
  6345. * @loc: A string describing where the error occurred
  6346. * @cmd: The tracing command that caused the error
  6347. * @errs: The array of loc-specific static error strings
  6348. * @type: The index into errs[], which produces the specific static err string
  6349. * @pos: The position the caret should be placed in the cmd
  6350. *
  6351. * Writes an error into tracing/error_log of the form:
  6352. *
  6353. * <loc>: error: <text>
  6354. * Command: <cmd>
  6355. * ^
  6356. *
  6357. * tracing/error_log is a small log file containing the last
  6358. * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
  6359. * unless there has been a tracing error, and the error log can be
  6360. * cleared and have its memory freed by writing the empty string in
  6361. * truncation mode to it i.e. echo > tracing/error_log.
  6362. *
  6363. * NOTE: the @errs array along with the @type param are used to
  6364. * produce a static error string - this string is not copied and saved
  6365. * when the error is logged - only a pointer to it is saved. See
  6366. * existing callers for examples of how static strings are typically
  6367. * defined for use with tracing_log_err().
  6368. */
  6369. void tracing_log_err(struct trace_array *tr,
  6370. const char *loc, const char *cmd,
  6371. const char **errs, u8 type, u16 pos)
  6372. {
  6373. struct tracing_log_err *err;
  6374. int len = 0;
  6375. if (!tr)
  6376. tr = &global_trace;
  6377. len += sizeof(CMD_PREFIX) + 2 * sizeof("\n") + strlen(cmd) + 1;
  6378. guard(mutex)(&tracing_err_log_lock);
  6379. err = get_tracing_log_err(tr, len);
  6380. if (PTR_ERR(err) == -ENOMEM)
  6381. return;
  6382. snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
  6383. snprintf(err->cmd, len, "\n" CMD_PREFIX "%s\n", cmd);
  6384. err->info.errs = errs;
  6385. err->info.type = type;
  6386. err->info.pos = pos;
  6387. err->info.ts = local_clock();
  6388. list_add_tail(&err->list, &tr->err_log);
  6389. }
  6390. static void clear_tracing_err_log(struct trace_array *tr)
  6391. {
  6392. struct tracing_log_err *err, *next;
  6393. mutex_lock(&tracing_err_log_lock);
  6394. list_for_each_entry_safe(err, next, &tr->err_log, list) {
  6395. list_del(&err->list);
  6396. free_tracing_log_err(err);
  6397. }
  6398. tr->n_err_log_entries = 0;
  6399. mutex_unlock(&tracing_err_log_lock);
  6400. }
  6401. static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
  6402. {
  6403. struct trace_array *tr = m->private;
  6404. mutex_lock(&tracing_err_log_lock);
  6405. return seq_list_start(&tr->err_log, *pos);
  6406. }
  6407. static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
  6408. {
  6409. struct trace_array *tr = m->private;
  6410. return seq_list_next(v, &tr->err_log, pos);
  6411. }
  6412. static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
  6413. {
  6414. mutex_unlock(&tracing_err_log_lock);
  6415. }
  6416. static void tracing_err_log_show_pos(struct seq_file *m, u16 pos)
  6417. {
  6418. u16 i;
  6419. for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
  6420. seq_putc(m, ' ');
  6421. for (i = 0; i < pos; i++)
  6422. seq_putc(m, ' ');
  6423. seq_puts(m, "^\n");
  6424. }
  6425. static int tracing_err_log_seq_show(struct seq_file *m, void *v)
  6426. {
  6427. struct tracing_log_err *err = v;
  6428. if (err) {
  6429. const char *err_text = err->info.errs[err->info.type];
  6430. u64 sec = err->info.ts;
  6431. u32 nsec;
  6432. nsec = do_div(sec, NSEC_PER_SEC);
  6433. seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
  6434. err->loc, err_text);
  6435. seq_printf(m, "%s", err->cmd);
  6436. tracing_err_log_show_pos(m, err->info.pos);
  6437. }
  6438. return 0;
  6439. }
  6440. static const struct seq_operations tracing_err_log_seq_ops = {
  6441. .start = tracing_err_log_seq_start,
  6442. .next = tracing_err_log_seq_next,
  6443. .stop = tracing_err_log_seq_stop,
  6444. .show = tracing_err_log_seq_show
  6445. };
  6446. static int tracing_err_log_open(struct inode *inode, struct file *file)
  6447. {
  6448. struct trace_array *tr = inode->i_private;
  6449. int ret = 0;
  6450. ret = tracing_check_open_get_tr(tr);
  6451. if (ret)
  6452. return ret;
  6453. /* If this file was opened for write, then erase contents */
  6454. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
  6455. clear_tracing_err_log(tr);
  6456. if (file->f_mode & FMODE_READ) {
  6457. ret = seq_open(file, &tracing_err_log_seq_ops);
  6458. if (!ret) {
  6459. struct seq_file *m = file->private_data;
  6460. m->private = tr;
  6461. } else {
  6462. trace_array_put(tr);
  6463. }
  6464. }
  6465. return ret;
  6466. }
  6467. static ssize_t tracing_err_log_write(struct file *file,
  6468. const char __user *buffer,
  6469. size_t count, loff_t *ppos)
  6470. {
  6471. return count;
  6472. }
  6473. static int tracing_err_log_release(struct inode *inode, struct file *file)
  6474. {
  6475. struct trace_array *tr = inode->i_private;
  6476. trace_array_put(tr);
  6477. if (file->f_mode & FMODE_READ)
  6478. seq_release(inode, file);
  6479. return 0;
  6480. }
  6481. static const struct file_operations tracing_err_log_fops = {
  6482. .open = tracing_err_log_open,
  6483. .write = tracing_err_log_write,
  6484. .read = seq_read,
  6485. .llseek = tracing_lseek,
  6486. .release = tracing_err_log_release,
  6487. };
  6488. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  6489. {
  6490. struct trace_array *tr = inode->i_private;
  6491. struct ftrace_buffer_info *info;
  6492. int ret;
  6493. ret = tracing_check_open_get_tr(tr);
  6494. if (ret)
  6495. return ret;
  6496. info = kvzalloc(sizeof(*info), GFP_KERNEL);
  6497. if (!info) {
  6498. trace_array_put(tr);
  6499. return -ENOMEM;
  6500. }
  6501. mutex_lock(&trace_types_lock);
  6502. info->iter.tr = tr;
  6503. info->iter.cpu_file = tracing_get_cpu(inode);
  6504. info->iter.trace = tr->current_trace;
  6505. info->iter.array_buffer = &tr->array_buffer;
  6506. info->spare = NULL;
  6507. /* Force reading ring buffer for first read */
  6508. info->read = (unsigned int)-1;
  6509. filp->private_data = info;
  6510. tr->trace_ref++;
  6511. mutex_unlock(&trace_types_lock);
  6512. ret = nonseekable_open(inode, filp);
  6513. if (ret < 0)
  6514. trace_array_put(tr);
  6515. return ret;
  6516. }
  6517. static __poll_t
  6518. tracing_buffers_poll(struct file *filp, poll_table *poll_table)
  6519. {
  6520. struct ftrace_buffer_info *info = filp->private_data;
  6521. struct trace_iterator *iter = &info->iter;
  6522. return trace_poll(iter, filp, poll_table);
  6523. }
  6524. static ssize_t
  6525. tracing_buffers_read(struct file *filp, char __user *ubuf,
  6526. size_t count, loff_t *ppos)
  6527. {
  6528. struct ftrace_buffer_info *info = filp->private_data;
  6529. struct trace_iterator *iter = &info->iter;
  6530. void *trace_data;
  6531. int page_size;
  6532. ssize_t ret = 0;
  6533. ssize_t size;
  6534. if (!count)
  6535. return 0;
  6536. #ifdef CONFIG_TRACER_MAX_TRACE
  6537. if (iter->snapshot && iter->tr->current_trace->use_max_tr)
  6538. return -EBUSY;
  6539. #endif
  6540. page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer);
  6541. /* Make sure the spare matches the current sub buffer size */
  6542. if (info->spare) {
  6543. if (page_size != info->spare_size) {
  6544. ring_buffer_free_read_page(iter->array_buffer->buffer,
  6545. info->spare_cpu, info->spare);
  6546. info->spare = NULL;
  6547. }
  6548. }
  6549. if (!info->spare) {
  6550. info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
  6551. iter->cpu_file);
  6552. if (IS_ERR(info->spare)) {
  6553. ret = PTR_ERR(info->spare);
  6554. info->spare = NULL;
  6555. } else {
  6556. info->spare_cpu = iter->cpu_file;
  6557. info->spare_size = page_size;
  6558. }
  6559. }
  6560. if (!info->spare)
  6561. return ret;
  6562. /* Do we have previous read data to read? */
  6563. if (info->read < page_size)
  6564. goto read;
  6565. again:
  6566. trace_access_lock(iter->cpu_file);
  6567. ret = ring_buffer_read_page(iter->array_buffer->buffer,
  6568. info->spare,
  6569. count,
  6570. iter->cpu_file, 0);
  6571. trace_access_unlock(iter->cpu_file);
  6572. if (ret < 0) {
  6573. if (trace_empty(iter) && !iter->closed) {
  6574. if ((filp->f_flags & O_NONBLOCK))
  6575. return -EAGAIN;
  6576. ret = wait_on_pipe(iter, 0);
  6577. if (ret)
  6578. return ret;
  6579. goto again;
  6580. }
  6581. return 0;
  6582. }
  6583. info->read = 0;
  6584. read:
  6585. size = page_size - info->read;
  6586. if (size > count)
  6587. size = count;
  6588. trace_data = ring_buffer_read_page_data(info->spare);
  6589. ret = copy_to_user(ubuf, trace_data + info->read, size);
  6590. if (ret == size)
  6591. return -EFAULT;
  6592. size -= ret;
  6593. *ppos += size;
  6594. info->read += size;
  6595. return size;
  6596. }
  6597. static int tracing_buffers_flush(struct file *file, fl_owner_t id)
  6598. {
  6599. struct ftrace_buffer_info *info = file->private_data;
  6600. struct trace_iterator *iter = &info->iter;
  6601. iter->closed = true;
  6602. /* Make sure the waiters see the new wait_index */
  6603. (void)atomic_fetch_inc_release(&iter->wait_index);
  6604. ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
  6605. return 0;
  6606. }
  6607. static int tracing_buffers_release(struct inode *inode, struct file *file)
  6608. {
  6609. struct ftrace_buffer_info *info = file->private_data;
  6610. struct trace_iterator *iter = &info->iter;
  6611. mutex_lock(&trace_types_lock);
  6612. iter->tr->trace_ref--;
  6613. __trace_array_put(iter->tr);
  6614. if (info->spare)
  6615. ring_buffer_free_read_page(iter->array_buffer->buffer,
  6616. info->spare_cpu, info->spare);
  6617. kvfree(info);
  6618. mutex_unlock(&trace_types_lock);
  6619. return 0;
  6620. }
  6621. struct buffer_ref {
  6622. struct trace_buffer *buffer;
  6623. void *page;
  6624. int cpu;
  6625. refcount_t refcount;
  6626. };
  6627. static void buffer_ref_release(struct buffer_ref *ref)
  6628. {
  6629. if (!refcount_dec_and_test(&ref->refcount))
  6630. return;
  6631. ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
  6632. kfree(ref);
  6633. }
  6634. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  6635. struct pipe_buffer *buf)
  6636. {
  6637. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  6638. buffer_ref_release(ref);
  6639. buf->private = 0;
  6640. }
  6641. static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  6642. struct pipe_buffer *buf)
  6643. {
  6644. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  6645. if (refcount_read(&ref->refcount) > INT_MAX/2)
  6646. return false;
  6647. refcount_inc(&ref->refcount);
  6648. return true;
  6649. }
  6650. /* Pipe buffer operations for a buffer. */
  6651. static const struct pipe_buf_operations buffer_pipe_buf_ops = {
  6652. .release = buffer_pipe_buf_release,
  6653. .get = buffer_pipe_buf_get,
  6654. };
  6655. /*
  6656. * Callback from splice_to_pipe(), if we need to release some pages
  6657. * at the end of the spd in case we error'ed out in filling the pipe.
  6658. */
  6659. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  6660. {
  6661. struct buffer_ref *ref =
  6662. (struct buffer_ref *)spd->partial[i].private;
  6663. buffer_ref_release(ref);
  6664. spd->partial[i].private = 0;
  6665. }
  6666. static ssize_t
  6667. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  6668. struct pipe_inode_info *pipe, size_t len,
  6669. unsigned int flags)
  6670. {
  6671. struct ftrace_buffer_info *info = file->private_data;
  6672. struct trace_iterator *iter = &info->iter;
  6673. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  6674. struct page *pages_def[PIPE_DEF_BUFFERS];
  6675. struct splice_pipe_desc spd = {
  6676. .pages = pages_def,
  6677. .partial = partial_def,
  6678. .nr_pages_max = PIPE_DEF_BUFFERS,
  6679. .ops = &buffer_pipe_buf_ops,
  6680. .spd_release = buffer_spd_release,
  6681. };
  6682. struct buffer_ref *ref;
  6683. bool woken = false;
  6684. int page_size;
  6685. int entries, i;
  6686. ssize_t ret = 0;
  6687. #ifdef CONFIG_TRACER_MAX_TRACE
  6688. if (iter->snapshot && iter->tr->current_trace->use_max_tr)
  6689. return -EBUSY;
  6690. #endif
  6691. page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer);
  6692. if (*ppos & (page_size - 1))
  6693. return -EINVAL;
  6694. if (len & (page_size - 1)) {
  6695. if (len < page_size)
  6696. return -EINVAL;
  6697. len &= (~(page_size - 1));
  6698. }
  6699. if (splice_grow_spd(pipe, &spd))
  6700. return -ENOMEM;
  6701. again:
  6702. trace_access_lock(iter->cpu_file);
  6703. entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
  6704. for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= page_size) {
  6705. struct page *page;
  6706. int r;
  6707. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  6708. if (!ref) {
  6709. ret = -ENOMEM;
  6710. break;
  6711. }
  6712. refcount_set(&ref->refcount, 1);
  6713. ref->buffer = iter->array_buffer->buffer;
  6714. ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
  6715. if (IS_ERR(ref->page)) {
  6716. ret = PTR_ERR(ref->page);
  6717. ref->page = NULL;
  6718. kfree(ref);
  6719. break;
  6720. }
  6721. ref->cpu = iter->cpu_file;
  6722. r = ring_buffer_read_page(ref->buffer, ref->page,
  6723. len, iter->cpu_file, 1);
  6724. if (r < 0) {
  6725. ring_buffer_free_read_page(ref->buffer, ref->cpu,
  6726. ref->page);
  6727. kfree(ref);
  6728. break;
  6729. }
  6730. page = virt_to_page(ring_buffer_read_page_data(ref->page));
  6731. spd.pages[i] = page;
  6732. spd.partial[i].len = page_size;
  6733. spd.partial[i].offset = 0;
  6734. spd.partial[i].private = (unsigned long)ref;
  6735. spd.nr_pages++;
  6736. *ppos += page_size;
  6737. entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
  6738. }
  6739. trace_access_unlock(iter->cpu_file);
  6740. spd.nr_pages = i;
  6741. /* did we read anything? */
  6742. if (!spd.nr_pages) {
  6743. if (ret)
  6744. goto out;
  6745. if (woken)
  6746. goto out;
  6747. ret = -EAGAIN;
  6748. if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
  6749. goto out;
  6750. ret = wait_on_pipe(iter, iter->snapshot ? 0 : iter->tr->buffer_percent);
  6751. if (ret)
  6752. goto out;
  6753. /* No need to wait after waking up when tracing is off */
  6754. if (!tracer_tracing_is_on(iter->tr))
  6755. goto out;
  6756. /* Iterate one more time to collect any new data then exit */
  6757. woken = true;
  6758. goto again;
  6759. }
  6760. ret = splice_to_pipe(pipe, &spd);
  6761. out:
  6762. splice_shrink_spd(&spd);
  6763. return ret;
  6764. }
  6765. static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  6766. {
  6767. struct ftrace_buffer_info *info = file->private_data;
  6768. struct trace_iterator *iter = &info->iter;
  6769. int err;
  6770. if (cmd == TRACE_MMAP_IOCTL_GET_READER) {
  6771. if (!(file->f_flags & O_NONBLOCK)) {
  6772. err = ring_buffer_wait(iter->array_buffer->buffer,
  6773. iter->cpu_file,
  6774. iter->tr->buffer_percent,
  6775. NULL, NULL);
  6776. if (err)
  6777. return err;
  6778. }
  6779. return ring_buffer_map_get_reader(iter->array_buffer->buffer,
  6780. iter->cpu_file);
  6781. } else if (cmd) {
  6782. return -ENOTTY;
  6783. }
  6784. /*
  6785. * An ioctl call with cmd 0 to the ring buffer file will wake up all
  6786. * waiters
  6787. */
  6788. mutex_lock(&trace_types_lock);
  6789. /* Make sure the waiters see the new wait_index */
  6790. (void)atomic_fetch_inc_release(&iter->wait_index);
  6791. ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
  6792. mutex_unlock(&trace_types_lock);
  6793. return 0;
  6794. }
  6795. #ifdef CONFIG_TRACER_MAX_TRACE
  6796. static int get_snapshot_map(struct trace_array *tr)
  6797. {
  6798. int err = 0;
  6799. /*
  6800. * Called with mmap_lock held. lockdep would be unhappy if we would now
  6801. * take trace_types_lock. Instead use the specific
  6802. * snapshot_trigger_lock.
  6803. */
  6804. spin_lock(&tr->snapshot_trigger_lock);
  6805. if (tr->snapshot || tr->mapped == UINT_MAX)
  6806. err = -EBUSY;
  6807. else
  6808. tr->mapped++;
  6809. spin_unlock(&tr->snapshot_trigger_lock);
  6810. /* Wait for update_max_tr() to observe iter->tr->mapped */
  6811. if (tr->mapped == 1)
  6812. synchronize_rcu();
  6813. return err;
  6814. }
  6815. static void put_snapshot_map(struct trace_array *tr)
  6816. {
  6817. spin_lock(&tr->snapshot_trigger_lock);
  6818. if (!WARN_ON(!tr->mapped))
  6819. tr->mapped--;
  6820. spin_unlock(&tr->snapshot_trigger_lock);
  6821. }
  6822. #else
  6823. static inline int get_snapshot_map(struct trace_array *tr) { return 0; }
  6824. static inline void put_snapshot_map(struct trace_array *tr) { }
  6825. #endif
  6826. static void tracing_buffers_mmap_close(struct vm_area_struct *vma)
  6827. {
  6828. struct ftrace_buffer_info *info = vma->vm_file->private_data;
  6829. struct trace_iterator *iter = &info->iter;
  6830. WARN_ON(ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file));
  6831. put_snapshot_map(iter->tr);
  6832. }
  6833. static const struct vm_operations_struct tracing_buffers_vmops = {
  6834. .close = tracing_buffers_mmap_close,
  6835. };
  6836. static int tracing_buffers_mmap(struct file *filp, struct vm_area_struct *vma)
  6837. {
  6838. struct ftrace_buffer_info *info = filp->private_data;
  6839. struct trace_iterator *iter = &info->iter;
  6840. int ret = 0;
  6841. /* Currently the boot mapped buffer is not supported for mmap */
  6842. if (iter->tr->flags & TRACE_ARRAY_FL_BOOT)
  6843. return -ENODEV;
  6844. ret = get_snapshot_map(iter->tr);
  6845. if (ret)
  6846. return ret;
  6847. ret = ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file, vma);
  6848. if (ret)
  6849. put_snapshot_map(iter->tr);
  6850. vma->vm_ops = &tracing_buffers_vmops;
  6851. return ret;
  6852. }
  6853. static const struct file_operations tracing_buffers_fops = {
  6854. .open = tracing_buffers_open,
  6855. .read = tracing_buffers_read,
  6856. .poll = tracing_buffers_poll,
  6857. .release = tracing_buffers_release,
  6858. .flush = tracing_buffers_flush,
  6859. .splice_read = tracing_buffers_splice_read,
  6860. .unlocked_ioctl = tracing_buffers_ioctl,
  6861. .mmap = tracing_buffers_mmap,
  6862. };
  6863. static ssize_t
  6864. tracing_stats_read(struct file *filp, char __user *ubuf,
  6865. size_t count, loff_t *ppos)
  6866. {
  6867. struct inode *inode = file_inode(filp);
  6868. struct trace_array *tr = inode->i_private;
  6869. struct array_buffer *trace_buf = &tr->array_buffer;
  6870. int cpu = tracing_get_cpu(inode);
  6871. struct trace_seq *s;
  6872. unsigned long cnt;
  6873. unsigned long long t;
  6874. unsigned long usec_rem;
  6875. s = kmalloc(sizeof(*s), GFP_KERNEL);
  6876. if (!s)
  6877. return -ENOMEM;
  6878. trace_seq_init(s);
  6879. cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
  6880. trace_seq_printf(s, "entries: %ld\n", cnt);
  6881. cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
  6882. trace_seq_printf(s, "overrun: %ld\n", cnt);
  6883. cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
  6884. trace_seq_printf(s, "commit overrun: %ld\n", cnt);
  6885. cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
  6886. trace_seq_printf(s, "bytes: %ld\n", cnt);
  6887. if (trace_clocks[tr->clock_id].in_ns) {
  6888. /* local or global for trace_clock */
  6889. t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  6890. usec_rem = do_div(t, USEC_PER_SEC);
  6891. trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
  6892. t, usec_rem);
  6893. t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer));
  6894. usec_rem = do_div(t, USEC_PER_SEC);
  6895. trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
  6896. } else {
  6897. /* counter or tsc mode for trace_clock */
  6898. trace_seq_printf(s, "oldest event ts: %llu\n",
  6899. ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
  6900. trace_seq_printf(s, "now ts: %llu\n",
  6901. ring_buffer_time_stamp(trace_buf->buffer));
  6902. }
  6903. cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
  6904. trace_seq_printf(s, "dropped events: %ld\n", cnt);
  6905. cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
  6906. trace_seq_printf(s, "read events: %ld\n", cnt);
  6907. count = simple_read_from_buffer(ubuf, count, ppos,
  6908. s->buffer, trace_seq_used(s));
  6909. kfree(s);
  6910. return count;
  6911. }
  6912. static const struct file_operations tracing_stats_fops = {
  6913. .open = tracing_open_generic_tr,
  6914. .read = tracing_stats_read,
  6915. .llseek = generic_file_llseek,
  6916. .release = tracing_release_generic_tr,
  6917. };
  6918. #ifdef CONFIG_DYNAMIC_FTRACE
  6919. static ssize_t
  6920. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  6921. size_t cnt, loff_t *ppos)
  6922. {
  6923. ssize_t ret;
  6924. char *buf;
  6925. int r;
  6926. /* 256 should be plenty to hold the amount needed */
  6927. buf = kmalloc(256, GFP_KERNEL);
  6928. if (!buf)
  6929. return -ENOMEM;
  6930. r = scnprintf(buf, 256, "%ld pages:%ld groups: %ld\n",
  6931. ftrace_update_tot_cnt,
  6932. ftrace_number_of_pages,
  6933. ftrace_number_of_groups);
  6934. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  6935. kfree(buf);
  6936. return ret;
  6937. }
  6938. static const struct file_operations tracing_dyn_info_fops = {
  6939. .open = tracing_open_generic,
  6940. .read = tracing_read_dyn_info,
  6941. .llseek = generic_file_llseek,
  6942. };
  6943. #endif /* CONFIG_DYNAMIC_FTRACE */
  6944. #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
  6945. static void
  6946. ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
  6947. struct trace_array *tr, struct ftrace_probe_ops *ops,
  6948. void *data)
  6949. {
  6950. tracing_snapshot_instance(tr);
  6951. }
  6952. static void
  6953. ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
  6954. struct trace_array *tr, struct ftrace_probe_ops *ops,
  6955. void *data)
  6956. {
  6957. struct ftrace_func_mapper *mapper = data;
  6958. long *count = NULL;
  6959. if (mapper)
  6960. count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
  6961. if (count) {
  6962. if (*count <= 0)
  6963. return;
  6964. (*count)--;
  6965. }
  6966. tracing_snapshot_instance(tr);
  6967. }
  6968. static int
  6969. ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
  6970. struct ftrace_probe_ops *ops, void *data)
  6971. {
  6972. struct ftrace_func_mapper *mapper = data;
  6973. long *count = NULL;
  6974. seq_printf(m, "%ps:", (void *)ip);
  6975. seq_puts(m, "snapshot");
  6976. if (mapper)
  6977. count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
  6978. if (count)
  6979. seq_printf(m, ":count=%ld\n", *count);
  6980. else
  6981. seq_puts(m, ":unlimited\n");
  6982. return 0;
  6983. }
  6984. static int
  6985. ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
  6986. unsigned long ip, void *init_data, void **data)
  6987. {
  6988. struct ftrace_func_mapper *mapper = *data;
  6989. if (!mapper) {
  6990. mapper = allocate_ftrace_func_mapper();
  6991. if (!mapper)
  6992. return -ENOMEM;
  6993. *data = mapper;
  6994. }
  6995. return ftrace_func_mapper_add_ip(mapper, ip, init_data);
  6996. }
  6997. static void
  6998. ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
  6999. unsigned long ip, void *data)
  7000. {
  7001. struct ftrace_func_mapper *mapper = data;
  7002. if (!ip) {
  7003. if (!mapper)
  7004. return;
  7005. free_ftrace_func_mapper(mapper, NULL);
  7006. return;
  7007. }
  7008. ftrace_func_mapper_remove_ip(mapper, ip);
  7009. }
  7010. static struct ftrace_probe_ops snapshot_probe_ops = {
  7011. .func = ftrace_snapshot,
  7012. .print = ftrace_snapshot_print,
  7013. };
  7014. static struct ftrace_probe_ops snapshot_count_probe_ops = {
  7015. .func = ftrace_count_snapshot,
  7016. .print = ftrace_snapshot_print,
  7017. .init = ftrace_snapshot_init,
  7018. .free = ftrace_snapshot_free,
  7019. };
  7020. static int
  7021. ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
  7022. char *glob, char *cmd, char *param, int enable)
  7023. {
  7024. struct ftrace_probe_ops *ops;
  7025. void *count = (void *)-1;
  7026. char *number;
  7027. int ret;
  7028. if (!tr)
  7029. return -ENODEV;
  7030. /* hash funcs only work with set_ftrace_filter */
  7031. if (!enable)
  7032. return -EINVAL;
  7033. ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
  7034. if (glob[0] == '!') {
  7035. ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
  7036. if (!ret)
  7037. tracing_disarm_snapshot(tr);
  7038. return ret;
  7039. }
  7040. if (!param)
  7041. goto out_reg;
  7042. number = strsep(&param, ":");
  7043. if (!strlen(number))
  7044. goto out_reg;
  7045. /*
  7046. * We use the callback data field (which is a pointer)
  7047. * as our counter.
  7048. */
  7049. ret = kstrtoul(number, 0, (unsigned long *)&count);
  7050. if (ret)
  7051. return ret;
  7052. out_reg:
  7053. ret = tracing_arm_snapshot(tr);
  7054. if (ret < 0)
  7055. return ret;
  7056. ret = register_ftrace_function_probe(glob, tr, ops, count);
  7057. if (ret < 0)
  7058. tracing_disarm_snapshot(tr);
  7059. return ret < 0 ? ret : 0;
  7060. }
  7061. static struct ftrace_func_command ftrace_snapshot_cmd = {
  7062. .name = "snapshot",
  7063. .func = ftrace_trace_snapshot_callback,
  7064. };
  7065. static __init int register_snapshot_cmd(void)
  7066. {
  7067. return register_ftrace_command(&ftrace_snapshot_cmd);
  7068. }
  7069. #else
  7070. static inline __init int register_snapshot_cmd(void) { return 0; }
  7071. #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
  7072. static struct dentry *tracing_get_dentry(struct trace_array *tr)
  7073. {
  7074. if (WARN_ON(!tr->dir))
  7075. return ERR_PTR(-ENODEV);
  7076. /* Top directory uses NULL as the parent */
  7077. if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
  7078. return NULL;
  7079. /* All sub buffers have a descriptor */
  7080. return tr->dir;
  7081. }
  7082. static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
  7083. {
  7084. struct dentry *d_tracer;
  7085. if (tr->percpu_dir)
  7086. return tr->percpu_dir;
  7087. d_tracer = tracing_get_dentry(tr);
  7088. if (IS_ERR(d_tracer))
  7089. return NULL;
  7090. tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
  7091. MEM_FAIL(!tr->percpu_dir,
  7092. "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
  7093. return tr->percpu_dir;
  7094. }
  7095. static struct dentry *
  7096. trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
  7097. void *data, long cpu, const struct file_operations *fops)
  7098. {
  7099. struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
  7100. if (ret) /* See tracing_get_cpu() */
  7101. d_inode(ret)->i_cdev = (void *)(cpu + 1);
  7102. return ret;
  7103. }
  7104. static void
  7105. tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
  7106. {
  7107. struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
  7108. struct dentry *d_cpu;
  7109. char cpu_dir[30]; /* 30 characters should be more than enough */
  7110. if (!d_percpu)
  7111. return;
  7112. snprintf(cpu_dir, 30, "cpu%ld", cpu);
  7113. d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
  7114. if (!d_cpu) {
  7115. pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
  7116. return;
  7117. }
  7118. /* per cpu trace_pipe */
  7119. trace_create_cpu_file("trace_pipe", TRACE_MODE_READ, d_cpu,
  7120. tr, cpu, &tracing_pipe_fops);
  7121. /* per cpu trace */
  7122. trace_create_cpu_file("trace", TRACE_MODE_WRITE, d_cpu,
  7123. tr, cpu, &tracing_fops);
  7124. trace_create_cpu_file("trace_pipe_raw", TRACE_MODE_READ, d_cpu,
  7125. tr, cpu, &tracing_buffers_fops);
  7126. trace_create_cpu_file("stats", TRACE_MODE_READ, d_cpu,
  7127. tr, cpu, &tracing_stats_fops);
  7128. trace_create_cpu_file("buffer_size_kb", TRACE_MODE_READ, d_cpu,
  7129. tr, cpu, &tracing_entries_fops);
  7130. if (tr->range_addr_start)
  7131. trace_create_cpu_file("buffer_meta", TRACE_MODE_READ, d_cpu,
  7132. tr, cpu, &tracing_buffer_meta_fops);
  7133. #ifdef CONFIG_TRACER_SNAPSHOT
  7134. if (!tr->range_addr_start) {
  7135. trace_create_cpu_file("snapshot", TRACE_MODE_WRITE, d_cpu,
  7136. tr, cpu, &snapshot_fops);
  7137. trace_create_cpu_file("snapshot_raw", TRACE_MODE_READ, d_cpu,
  7138. tr, cpu, &snapshot_raw_fops);
  7139. }
  7140. #endif
  7141. }
  7142. #ifdef CONFIG_FTRACE_SELFTEST
  7143. /* Let selftest have access to static functions in this file */
  7144. #include "trace_selftest.c"
  7145. #endif
  7146. static ssize_t
  7147. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  7148. loff_t *ppos)
  7149. {
  7150. struct trace_option_dentry *topt = filp->private_data;
  7151. char *buf;
  7152. if (topt->flags->val & topt->opt->bit)
  7153. buf = "1\n";
  7154. else
  7155. buf = "0\n";
  7156. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  7157. }
  7158. static ssize_t
  7159. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  7160. loff_t *ppos)
  7161. {
  7162. struct trace_option_dentry *topt = filp->private_data;
  7163. unsigned long val;
  7164. int ret;
  7165. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  7166. if (ret)
  7167. return ret;
  7168. if (val != 0 && val != 1)
  7169. return -EINVAL;
  7170. if (!!(topt->flags->val & topt->opt->bit) != val) {
  7171. mutex_lock(&trace_types_lock);
  7172. ret = __set_tracer_option(topt->tr, topt->flags,
  7173. topt->opt, !val);
  7174. mutex_unlock(&trace_types_lock);
  7175. if (ret)
  7176. return ret;
  7177. }
  7178. *ppos += cnt;
  7179. return cnt;
  7180. }
  7181. static int tracing_open_options(struct inode *inode, struct file *filp)
  7182. {
  7183. struct trace_option_dentry *topt = inode->i_private;
  7184. int ret;
  7185. ret = tracing_check_open_get_tr(topt->tr);
  7186. if (ret)
  7187. return ret;
  7188. filp->private_data = inode->i_private;
  7189. return 0;
  7190. }
  7191. static int tracing_release_options(struct inode *inode, struct file *file)
  7192. {
  7193. struct trace_option_dentry *topt = file->private_data;
  7194. trace_array_put(topt->tr);
  7195. return 0;
  7196. }
  7197. static const struct file_operations trace_options_fops = {
  7198. .open = tracing_open_options,
  7199. .read = trace_options_read,
  7200. .write = trace_options_write,
  7201. .llseek = generic_file_llseek,
  7202. .release = tracing_release_options,
  7203. };
  7204. /*
  7205. * In order to pass in both the trace_array descriptor as well as the index
  7206. * to the flag that the trace option file represents, the trace_array
  7207. * has a character array of trace_flags_index[], which holds the index
  7208. * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
  7209. * The address of this character array is passed to the flag option file
  7210. * read/write callbacks.
  7211. *
  7212. * In order to extract both the index and the trace_array descriptor,
  7213. * get_tr_index() uses the following algorithm.
  7214. *
  7215. * idx = *ptr;
  7216. *
  7217. * As the pointer itself contains the address of the index (remember
  7218. * index[1] == 1).
  7219. *
  7220. * Then to get the trace_array descriptor, by subtracting that index
  7221. * from the ptr, we get to the start of the index itself.
  7222. *
  7223. * ptr - idx == &index[0]
  7224. *
  7225. * Then a simple container_of() from that pointer gets us to the
  7226. * trace_array descriptor.
  7227. */
  7228. static void get_tr_index(void *data, struct trace_array **ptr,
  7229. unsigned int *pindex)
  7230. {
  7231. *pindex = *(unsigned char *)data;
  7232. *ptr = container_of(data - *pindex, struct trace_array,
  7233. trace_flags_index);
  7234. }
  7235. static ssize_t
  7236. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  7237. loff_t *ppos)
  7238. {
  7239. void *tr_index = filp->private_data;
  7240. struct trace_array *tr;
  7241. unsigned int index;
  7242. char *buf;
  7243. get_tr_index(tr_index, &tr, &index);
  7244. if (tr->trace_flags & (1 << index))
  7245. buf = "1\n";
  7246. else
  7247. buf = "0\n";
  7248. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  7249. }
  7250. static ssize_t
  7251. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  7252. loff_t *ppos)
  7253. {
  7254. void *tr_index = filp->private_data;
  7255. struct trace_array *tr;
  7256. unsigned int index;
  7257. unsigned long val;
  7258. int ret;
  7259. get_tr_index(tr_index, &tr, &index);
  7260. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  7261. if (ret)
  7262. return ret;
  7263. if (val != 0 && val != 1)
  7264. return -EINVAL;
  7265. mutex_lock(&event_mutex);
  7266. mutex_lock(&trace_types_lock);
  7267. ret = set_tracer_flag(tr, 1 << index, val);
  7268. mutex_unlock(&trace_types_lock);
  7269. mutex_unlock(&event_mutex);
  7270. if (ret < 0)
  7271. return ret;
  7272. *ppos += cnt;
  7273. return cnt;
  7274. }
  7275. static const struct file_operations trace_options_core_fops = {
  7276. .open = tracing_open_generic,
  7277. .read = trace_options_core_read,
  7278. .write = trace_options_core_write,
  7279. .llseek = generic_file_llseek,
  7280. };
  7281. struct dentry *trace_create_file(const char *name,
  7282. umode_t mode,
  7283. struct dentry *parent,
  7284. void *data,
  7285. const struct file_operations *fops)
  7286. {
  7287. struct dentry *ret;
  7288. ret = tracefs_create_file(name, mode, parent, data, fops);
  7289. if (!ret)
  7290. pr_warn("Could not create tracefs '%s' entry\n", name);
  7291. return ret;
  7292. }
  7293. static struct dentry *trace_options_init_dentry(struct trace_array *tr)
  7294. {
  7295. struct dentry *d_tracer;
  7296. if (tr->options)
  7297. return tr->options;
  7298. d_tracer = tracing_get_dentry(tr);
  7299. if (IS_ERR(d_tracer))
  7300. return NULL;
  7301. tr->options = tracefs_create_dir("options", d_tracer);
  7302. if (!tr->options) {
  7303. pr_warn("Could not create tracefs directory 'options'\n");
  7304. return NULL;
  7305. }
  7306. return tr->options;
  7307. }
  7308. static void
  7309. create_trace_option_file(struct trace_array *tr,
  7310. struct trace_option_dentry *topt,
  7311. struct tracer_flags *flags,
  7312. struct tracer_opt *opt)
  7313. {
  7314. struct dentry *t_options;
  7315. t_options = trace_options_init_dentry(tr);
  7316. if (!t_options)
  7317. return;
  7318. topt->flags = flags;
  7319. topt->opt = opt;
  7320. topt->tr = tr;
  7321. topt->entry = trace_create_file(opt->name, TRACE_MODE_WRITE,
  7322. t_options, topt, &trace_options_fops);
  7323. }
  7324. static void
  7325. create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
  7326. {
  7327. struct trace_option_dentry *topts;
  7328. struct trace_options *tr_topts;
  7329. struct tracer_flags *flags;
  7330. struct tracer_opt *opts;
  7331. int cnt;
  7332. int i;
  7333. if (!tracer)
  7334. return;
  7335. flags = tracer->flags;
  7336. if (!flags || !flags->opts)
  7337. return;
  7338. /*
  7339. * If this is an instance, only create flags for tracers
  7340. * the instance may have.
  7341. */
  7342. if (!trace_ok_for_array(tracer, tr))
  7343. return;
  7344. for (i = 0; i < tr->nr_topts; i++) {
  7345. /* Make sure there's no duplicate flags. */
  7346. if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
  7347. return;
  7348. }
  7349. opts = flags->opts;
  7350. for (cnt = 0; opts[cnt].name; cnt++)
  7351. ;
  7352. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  7353. if (!topts)
  7354. return;
  7355. tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
  7356. GFP_KERNEL);
  7357. if (!tr_topts) {
  7358. kfree(topts);
  7359. return;
  7360. }
  7361. tr->topts = tr_topts;
  7362. tr->topts[tr->nr_topts].tracer = tracer;
  7363. tr->topts[tr->nr_topts].topts = topts;
  7364. tr->nr_topts++;
  7365. for (cnt = 0; opts[cnt].name; cnt++) {
  7366. create_trace_option_file(tr, &topts[cnt], flags,
  7367. &opts[cnt]);
  7368. MEM_FAIL(topts[cnt].entry == NULL,
  7369. "Failed to create trace option: %s",
  7370. opts[cnt].name);
  7371. }
  7372. }
  7373. static struct dentry *
  7374. create_trace_option_core_file(struct trace_array *tr,
  7375. const char *option, long index)
  7376. {
  7377. struct dentry *t_options;
  7378. t_options = trace_options_init_dentry(tr);
  7379. if (!t_options)
  7380. return NULL;
  7381. return trace_create_file(option, TRACE_MODE_WRITE, t_options,
  7382. (void *)&tr->trace_flags_index[index],
  7383. &trace_options_core_fops);
  7384. }
  7385. static void create_trace_options_dir(struct trace_array *tr)
  7386. {
  7387. struct dentry *t_options;
  7388. bool top_level = tr == &global_trace;
  7389. int i;
  7390. t_options = trace_options_init_dentry(tr);
  7391. if (!t_options)
  7392. return;
  7393. for (i = 0; trace_options[i]; i++) {
  7394. if (top_level ||
  7395. !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
  7396. create_trace_option_core_file(tr, trace_options[i], i);
  7397. }
  7398. }
  7399. static ssize_t
  7400. rb_simple_read(struct file *filp, char __user *ubuf,
  7401. size_t cnt, loff_t *ppos)
  7402. {
  7403. struct trace_array *tr = filp->private_data;
  7404. char buf[64];
  7405. int r;
  7406. r = tracer_tracing_is_on(tr);
  7407. r = sprintf(buf, "%d\n", r);
  7408. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  7409. }
  7410. static ssize_t
  7411. rb_simple_write(struct file *filp, const char __user *ubuf,
  7412. size_t cnt, loff_t *ppos)
  7413. {
  7414. struct trace_array *tr = filp->private_data;
  7415. struct trace_buffer *buffer = tr->array_buffer.buffer;
  7416. unsigned long val;
  7417. int ret;
  7418. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  7419. if (ret)
  7420. return ret;
  7421. if (buffer) {
  7422. mutex_lock(&trace_types_lock);
  7423. if (!!val == tracer_tracing_is_on(tr)) {
  7424. val = 0; /* do nothing */
  7425. } else if (val) {
  7426. tracer_tracing_on(tr);
  7427. if (tr->current_trace->start)
  7428. tr->current_trace->start(tr);
  7429. } else {
  7430. tracer_tracing_off(tr);
  7431. if (tr->current_trace->stop)
  7432. tr->current_trace->stop(tr);
  7433. /* Wake up any waiters */
  7434. ring_buffer_wake_waiters(buffer, RING_BUFFER_ALL_CPUS);
  7435. }
  7436. mutex_unlock(&trace_types_lock);
  7437. }
  7438. (*ppos)++;
  7439. return cnt;
  7440. }
  7441. static const struct file_operations rb_simple_fops = {
  7442. .open = tracing_open_generic_tr,
  7443. .read = rb_simple_read,
  7444. .write = rb_simple_write,
  7445. .release = tracing_release_generic_tr,
  7446. .llseek = default_llseek,
  7447. };
  7448. static ssize_t
  7449. buffer_percent_read(struct file *filp, char __user *ubuf,
  7450. size_t cnt, loff_t *ppos)
  7451. {
  7452. struct trace_array *tr = filp->private_data;
  7453. char buf[64];
  7454. int r;
  7455. r = tr->buffer_percent;
  7456. r = sprintf(buf, "%d\n", r);
  7457. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  7458. }
  7459. static ssize_t
  7460. buffer_percent_write(struct file *filp, const char __user *ubuf,
  7461. size_t cnt, loff_t *ppos)
  7462. {
  7463. struct trace_array *tr = filp->private_data;
  7464. unsigned long val;
  7465. int ret;
  7466. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  7467. if (ret)
  7468. return ret;
  7469. if (val > 100)
  7470. return -EINVAL;
  7471. tr->buffer_percent = val;
  7472. (*ppos)++;
  7473. return cnt;
  7474. }
  7475. static const struct file_operations buffer_percent_fops = {
  7476. .open = tracing_open_generic_tr,
  7477. .read = buffer_percent_read,
  7478. .write = buffer_percent_write,
  7479. .release = tracing_release_generic_tr,
  7480. .llseek = default_llseek,
  7481. };
  7482. static ssize_t
  7483. buffer_subbuf_size_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  7484. {
  7485. struct trace_array *tr = filp->private_data;
  7486. size_t size;
  7487. char buf[64];
  7488. int order;
  7489. int r;
  7490. order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
  7491. size = (PAGE_SIZE << order) / 1024;
  7492. r = sprintf(buf, "%zd\n", size);
  7493. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  7494. }
  7495. static ssize_t
  7496. buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,
  7497. size_t cnt, loff_t *ppos)
  7498. {
  7499. struct trace_array *tr = filp->private_data;
  7500. unsigned long val;
  7501. int old_order;
  7502. int order;
  7503. int pages;
  7504. int ret;
  7505. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  7506. if (ret)
  7507. return ret;
  7508. val *= 1024; /* value passed in is in KB */
  7509. pages = DIV_ROUND_UP(val, PAGE_SIZE);
  7510. order = fls(pages - 1);
  7511. /* limit between 1 and 128 system pages */
  7512. if (order < 0 || order > 7)
  7513. return -EINVAL;
  7514. /* Do not allow tracing while changing the order of the ring buffer */
  7515. tracing_stop_tr(tr);
  7516. old_order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
  7517. if (old_order == order)
  7518. goto out;
  7519. ret = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, order);
  7520. if (ret)
  7521. goto out;
  7522. #ifdef CONFIG_TRACER_MAX_TRACE
  7523. if (!tr->allocated_snapshot)
  7524. goto out_max;
  7525. ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, order);
  7526. if (ret) {
  7527. /* Put back the old order */
  7528. cnt = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, old_order);
  7529. if (WARN_ON_ONCE(cnt)) {
  7530. /*
  7531. * AARGH! We are left with different orders!
  7532. * The max buffer is our "snapshot" buffer.
  7533. * When a tracer needs a snapshot (one of the
  7534. * latency tracers), it swaps the max buffer
  7535. * with the saved snap shot. We succeeded to
  7536. * update the order of the main buffer, but failed to
  7537. * update the order of the max buffer. But when we tried
  7538. * to reset the main buffer to the original size, we
  7539. * failed there too. This is very unlikely to
  7540. * happen, but if it does, warn and kill all
  7541. * tracing.
  7542. */
  7543. tracing_disabled = 1;
  7544. }
  7545. goto out;
  7546. }
  7547. out_max:
  7548. #endif
  7549. (*ppos)++;
  7550. out:
  7551. if (ret)
  7552. cnt = ret;
  7553. tracing_start_tr(tr);
  7554. return cnt;
  7555. }
  7556. static const struct file_operations buffer_subbuf_size_fops = {
  7557. .open = tracing_open_generic_tr,
  7558. .read = buffer_subbuf_size_read,
  7559. .write = buffer_subbuf_size_write,
  7560. .release = tracing_release_generic_tr,
  7561. .llseek = default_llseek,
  7562. };
  7563. static struct dentry *trace_instance_dir;
  7564. static void
  7565. init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
  7566. static int
  7567. allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
  7568. {
  7569. enum ring_buffer_flags rb_flags;
  7570. rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
  7571. buf->tr = tr;
  7572. if (tr->range_addr_start && tr->range_addr_size) {
  7573. buf->buffer = ring_buffer_alloc_range(size, rb_flags, 0,
  7574. tr->range_addr_start,
  7575. tr->range_addr_size);
  7576. ring_buffer_last_boot_delta(buf->buffer,
  7577. &tr->text_delta, &tr->data_delta);
  7578. /*
  7579. * This is basically the same as a mapped buffer,
  7580. * with the same restrictions.
  7581. */
  7582. tr->mapped++;
  7583. } else {
  7584. buf->buffer = ring_buffer_alloc(size, rb_flags);
  7585. }
  7586. if (!buf->buffer)
  7587. return -ENOMEM;
  7588. buf->data = alloc_percpu(struct trace_array_cpu);
  7589. if (!buf->data) {
  7590. ring_buffer_free(buf->buffer);
  7591. buf->buffer = NULL;
  7592. return -ENOMEM;
  7593. }
  7594. /* Allocate the first page for all buffers */
  7595. set_buffer_entries(&tr->array_buffer,
  7596. ring_buffer_size(tr->array_buffer.buffer, 0));
  7597. return 0;
  7598. }
  7599. static void free_trace_buffer(struct array_buffer *buf)
  7600. {
  7601. if (buf->buffer) {
  7602. ring_buffer_free(buf->buffer);
  7603. buf->buffer = NULL;
  7604. free_percpu(buf->data);
  7605. buf->data = NULL;
  7606. }
  7607. }
  7608. static int allocate_trace_buffers(struct trace_array *tr, int size)
  7609. {
  7610. int ret;
  7611. ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
  7612. if (ret)
  7613. return ret;
  7614. #ifdef CONFIG_TRACER_MAX_TRACE
  7615. /* Fix mapped buffer trace arrays do not have snapshot buffers */
  7616. if (tr->range_addr_start)
  7617. return 0;
  7618. ret = allocate_trace_buffer(tr, &tr->max_buffer,
  7619. allocate_snapshot ? size : 1);
  7620. if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
  7621. free_trace_buffer(&tr->array_buffer);
  7622. return -ENOMEM;
  7623. }
  7624. tr->allocated_snapshot = allocate_snapshot;
  7625. allocate_snapshot = false;
  7626. #endif
  7627. return 0;
  7628. }
  7629. static void free_trace_buffers(struct trace_array *tr)
  7630. {
  7631. if (!tr)
  7632. return;
  7633. free_trace_buffer(&tr->array_buffer);
  7634. #ifdef CONFIG_TRACER_MAX_TRACE
  7635. free_trace_buffer(&tr->max_buffer);
  7636. #endif
  7637. }
  7638. static void init_trace_flags_index(struct trace_array *tr)
  7639. {
  7640. int i;
  7641. /* Used by the trace options files */
  7642. for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
  7643. tr->trace_flags_index[i] = i;
  7644. }
  7645. static void __update_tracer_options(struct trace_array *tr)
  7646. {
  7647. struct tracer *t;
  7648. for (t = trace_types; t; t = t->next)
  7649. add_tracer_options(tr, t);
  7650. }
  7651. static void update_tracer_options(struct trace_array *tr)
  7652. {
  7653. mutex_lock(&trace_types_lock);
  7654. tracer_options_updated = true;
  7655. __update_tracer_options(tr);
  7656. mutex_unlock(&trace_types_lock);
  7657. }
  7658. /* Must have trace_types_lock held */
  7659. struct trace_array *trace_array_find(const char *instance)
  7660. {
  7661. struct trace_array *tr, *found = NULL;
  7662. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  7663. if (tr->name && strcmp(tr->name, instance) == 0) {
  7664. found = tr;
  7665. break;
  7666. }
  7667. }
  7668. return found;
  7669. }
  7670. struct trace_array *trace_array_find_get(const char *instance)
  7671. {
  7672. struct trace_array *tr;
  7673. mutex_lock(&trace_types_lock);
  7674. tr = trace_array_find(instance);
  7675. if (tr)
  7676. tr->ref++;
  7677. mutex_unlock(&trace_types_lock);
  7678. return tr;
  7679. }
  7680. static int trace_array_create_dir(struct trace_array *tr)
  7681. {
  7682. int ret;
  7683. tr->dir = tracefs_create_dir(tr->name, trace_instance_dir);
  7684. if (!tr->dir)
  7685. return -EINVAL;
  7686. ret = event_trace_add_tracer(tr->dir, tr);
  7687. if (ret) {
  7688. tracefs_remove(tr->dir);
  7689. return ret;
  7690. }
  7691. init_tracer_tracefs(tr, tr->dir);
  7692. __update_tracer_options(tr);
  7693. return ret;
  7694. }
  7695. static struct trace_array *
  7696. trace_array_create_systems(const char *name, const char *systems,
  7697. unsigned long range_addr_start,
  7698. unsigned long range_addr_size)
  7699. {
  7700. struct trace_array *tr;
  7701. int ret;
  7702. ret = -ENOMEM;
  7703. tr = kzalloc(sizeof(*tr), GFP_KERNEL);
  7704. if (!tr)
  7705. return ERR_PTR(ret);
  7706. tr->name = kstrdup(name, GFP_KERNEL);
  7707. if (!tr->name)
  7708. goto out_free_tr;
  7709. if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
  7710. goto out_free_tr;
  7711. if (!zalloc_cpumask_var(&tr->pipe_cpumask, GFP_KERNEL))
  7712. goto out_free_tr;
  7713. if (systems) {
  7714. tr->system_names = kstrdup_const(systems, GFP_KERNEL);
  7715. if (!tr->system_names)
  7716. goto out_free_tr;
  7717. }
  7718. /* Only for boot up memory mapped ring buffers */
  7719. tr->range_addr_start = range_addr_start;
  7720. tr->range_addr_size = range_addr_size;
  7721. tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
  7722. cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
  7723. raw_spin_lock_init(&tr->start_lock);
  7724. tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  7725. #ifdef CONFIG_TRACER_MAX_TRACE
  7726. spin_lock_init(&tr->snapshot_trigger_lock);
  7727. #endif
  7728. tr->current_trace = &nop_trace;
  7729. INIT_LIST_HEAD(&tr->systems);
  7730. INIT_LIST_HEAD(&tr->events);
  7731. INIT_LIST_HEAD(&tr->hist_vars);
  7732. INIT_LIST_HEAD(&tr->err_log);
  7733. if (allocate_trace_buffers(tr, trace_buf_size) < 0)
  7734. goto out_free_tr;
  7735. /* The ring buffer is defaultly expanded */
  7736. trace_set_ring_buffer_expanded(tr);
  7737. if (ftrace_allocate_ftrace_ops(tr) < 0)
  7738. goto out_free_tr;
  7739. ftrace_init_trace_array(tr);
  7740. init_trace_flags_index(tr);
  7741. if (trace_instance_dir) {
  7742. ret = trace_array_create_dir(tr);
  7743. if (ret)
  7744. goto out_free_tr;
  7745. } else
  7746. __trace_early_add_events(tr);
  7747. list_add(&tr->list, &ftrace_trace_arrays);
  7748. tr->ref++;
  7749. return tr;
  7750. out_free_tr:
  7751. ftrace_free_ftrace_ops(tr);
  7752. free_trace_buffers(tr);
  7753. free_cpumask_var(tr->pipe_cpumask);
  7754. free_cpumask_var(tr->tracing_cpumask);
  7755. kfree_const(tr->system_names);
  7756. kfree(tr->name);
  7757. kfree(tr);
  7758. return ERR_PTR(ret);
  7759. }
  7760. static struct trace_array *trace_array_create(const char *name)
  7761. {
  7762. return trace_array_create_systems(name, NULL, 0, 0);
  7763. }
  7764. static int instance_mkdir(const char *name)
  7765. {
  7766. struct trace_array *tr;
  7767. int ret;
  7768. guard(mutex)(&event_mutex);
  7769. guard(mutex)(&trace_types_lock);
  7770. ret = -EEXIST;
  7771. if (trace_array_find(name))
  7772. return -EEXIST;
  7773. tr = trace_array_create(name);
  7774. ret = PTR_ERR_OR_ZERO(tr);
  7775. return ret;
  7776. }
  7777. static u64 map_pages(u64 start, u64 size)
  7778. {
  7779. struct page **pages;
  7780. phys_addr_t page_start;
  7781. unsigned int page_count;
  7782. unsigned int i;
  7783. void *vaddr;
  7784. page_count = DIV_ROUND_UP(size, PAGE_SIZE);
  7785. page_start = start;
  7786. pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
  7787. if (!pages)
  7788. return 0;
  7789. for (i = 0; i < page_count; i++) {
  7790. phys_addr_t addr = page_start + i * PAGE_SIZE;
  7791. pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
  7792. }
  7793. vaddr = vmap(pages, page_count, VM_MAP, PAGE_KERNEL);
  7794. kfree(pages);
  7795. return (u64)(unsigned long)vaddr;
  7796. }
  7797. /**
  7798. * trace_array_get_by_name - Create/Lookup a trace array, given its name.
  7799. * @name: The name of the trace array to be looked up/created.
  7800. * @systems: A list of systems to create event directories for (NULL for all)
  7801. *
  7802. * Returns pointer to trace array with given name.
  7803. * NULL, if it cannot be created.
  7804. *
  7805. * NOTE: This function increments the reference counter associated with the
  7806. * trace array returned. This makes sure it cannot be freed while in use.
  7807. * Use trace_array_put() once the trace array is no longer needed.
  7808. * If the trace_array is to be freed, trace_array_destroy() needs to
  7809. * be called after the trace_array_put(), or simply let user space delete
  7810. * it from the tracefs instances directory. But until the
  7811. * trace_array_put() is called, user space can not delete it.
  7812. *
  7813. */
  7814. struct trace_array *trace_array_get_by_name(const char *name, const char *systems)
  7815. {
  7816. struct trace_array *tr;
  7817. guard(mutex)(&event_mutex);
  7818. guard(mutex)(&trace_types_lock);
  7819. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  7820. if (tr->name && strcmp(tr->name, name) == 0) {
  7821. tr->ref++;
  7822. return tr;
  7823. }
  7824. }
  7825. tr = trace_array_create_systems(name, systems, 0, 0);
  7826. if (IS_ERR(tr))
  7827. tr = NULL;
  7828. else
  7829. tr->ref++;
  7830. return tr;
  7831. }
  7832. EXPORT_SYMBOL_GPL(trace_array_get_by_name);
  7833. static int __remove_instance(struct trace_array *tr)
  7834. {
  7835. int i;
  7836. /* Reference counter for a newly created trace array = 1. */
  7837. if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
  7838. return -EBUSY;
  7839. list_del(&tr->list);
  7840. /* Disable all the flags that were enabled coming in */
  7841. for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
  7842. if ((1 << i) & ZEROED_TRACE_FLAGS)
  7843. set_tracer_flag(tr, 1 << i, 0);
  7844. }
  7845. if (printk_trace == tr)
  7846. update_printk_trace(&global_trace);
  7847. tracing_set_nop(tr);
  7848. clear_ftrace_function_probes(tr);
  7849. event_trace_del_tracer(tr);
  7850. ftrace_clear_pids(tr);
  7851. ftrace_destroy_function_files(tr);
  7852. tracefs_remove(tr->dir);
  7853. free_percpu(tr->last_func_repeats);
  7854. free_trace_buffers(tr);
  7855. clear_tracing_err_log(tr);
  7856. for (i = 0; i < tr->nr_topts; i++) {
  7857. kfree(tr->topts[i].topts);
  7858. }
  7859. kfree(tr->topts);
  7860. free_cpumask_var(tr->pipe_cpumask);
  7861. free_cpumask_var(tr->tracing_cpumask);
  7862. kfree_const(tr->system_names);
  7863. kfree(tr->name);
  7864. kfree(tr);
  7865. return 0;
  7866. }
  7867. int trace_array_destroy(struct trace_array *this_tr)
  7868. {
  7869. struct trace_array *tr;
  7870. if (!this_tr)
  7871. return -EINVAL;
  7872. guard(mutex)(&event_mutex);
  7873. guard(mutex)(&trace_types_lock);
  7874. /* Making sure trace array exists before destroying it. */
  7875. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  7876. if (tr == this_tr)
  7877. return __remove_instance(tr);
  7878. }
  7879. return -ENODEV;
  7880. }
  7881. EXPORT_SYMBOL_GPL(trace_array_destroy);
  7882. static int instance_rmdir(const char *name)
  7883. {
  7884. struct trace_array *tr;
  7885. guard(mutex)(&event_mutex);
  7886. guard(mutex)(&trace_types_lock);
  7887. tr = trace_array_find(name);
  7888. if (!tr)
  7889. return -ENODEV;
  7890. return __remove_instance(tr);
  7891. }
  7892. static __init void create_trace_instances(struct dentry *d_tracer)
  7893. {
  7894. struct trace_array *tr;
  7895. trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
  7896. instance_mkdir,
  7897. instance_rmdir);
  7898. if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n"))
  7899. return;
  7900. guard(mutex)(&event_mutex);
  7901. guard(mutex)(&trace_types_lock);
  7902. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  7903. if (!tr->name)
  7904. continue;
  7905. if (MEM_FAIL(trace_array_create_dir(tr) < 0,
  7906. "Failed to create instance directory\n"))
  7907. return;
  7908. }
  7909. }
  7910. static void
  7911. init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
  7912. {
  7913. int cpu;
  7914. trace_create_file("available_tracers", TRACE_MODE_READ, d_tracer,
  7915. tr, &show_traces_fops);
  7916. trace_create_file("current_tracer", TRACE_MODE_WRITE, d_tracer,
  7917. tr, &set_tracer_fops);
  7918. trace_create_file("tracing_cpumask", TRACE_MODE_WRITE, d_tracer,
  7919. tr, &tracing_cpumask_fops);
  7920. trace_create_file("trace_options", TRACE_MODE_WRITE, d_tracer,
  7921. tr, &tracing_iter_fops);
  7922. trace_create_file("trace", TRACE_MODE_WRITE, d_tracer,
  7923. tr, &tracing_fops);
  7924. trace_create_file("trace_pipe", TRACE_MODE_READ, d_tracer,
  7925. tr, &tracing_pipe_fops);
  7926. trace_create_file("buffer_size_kb", TRACE_MODE_WRITE, d_tracer,
  7927. tr, &tracing_entries_fops);
  7928. trace_create_file("buffer_total_size_kb", TRACE_MODE_READ, d_tracer,
  7929. tr, &tracing_total_entries_fops);
  7930. trace_create_file("free_buffer", 0200, d_tracer,
  7931. tr, &tracing_free_buffer_fops);
  7932. trace_create_file("trace_marker", 0220, d_tracer,
  7933. tr, &tracing_mark_fops);
  7934. tr->trace_marker_file = __find_event_file(tr, "ftrace", "print");
  7935. trace_create_file("trace_marker_raw", 0220, d_tracer,
  7936. tr, &tracing_mark_raw_fops);
  7937. trace_create_file("trace_clock", TRACE_MODE_WRITE, d_tracer, tr,
  7938. &trace_clock_fops);
  7939. trace_create_file("tracing_on", TRACE_MODE_WRITE, d_tracer,
  7940. tr, &rb_simple_fops);
  7941. trace_create_file("timestamp_mode", TRACE_MODE_READ, d_tracer, tr,
  7942. &trace_time_stamp_mode_fops);
  7943. tr->buffer_percent = 50;
  7944. trace_create_file("buffer_percent", TRACE_MODE_WRITE, d_tracer,
  7945. tr, &buffer_percent_fops);
  7946. trace_create_file("buffer_subbuf_size_kb", TRACE_MODE_WRITE, d_tracer,
  7947. tr, &buffer_subbuf_size_fops);
  7948. create_trace_options_dir(tr);
  7949. #ifdef CONFIG_TRACER_MAX_TRACE
  7950. trace_create_maxlat_file(tr, d_tracer);
  7951. #endif
  7952. if (ftrace_create_function_files(tr, d_tracer))
  7953. MEM_FAIL(1, "Could not allocate function filter files");
  7954. if (tr->range_addr_start) {
  7955. trace_create_file("last_boot_info", TRACE_MODE_READ, d_tracer,
  7956. tr, &last_boot_fops);
  7957. #ifdef CONFIG_TRACER_SNAPSHOT
  7958. } else {
  7959. trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer,
  7960. tr, &snapshot_fops);
  7961. #endif
  7962. }
  7963. trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer,
  7964. tr, &tracing_err_log_fops);
  7965. for_each_tracing_cpu(cpu)
  7966. tracing_init_tracefs_percpu(tr, cpu);
  7967. ftrace_init_tracefs(tr, d_tracer);
  7968. }
  7969. static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
  7970. {
  7971. struct vfsmount *mnt;
  7972. struct file_system_type *type;
  7973. /*
  7974. * To maintain backward compatibility for tools that mount
  7975. * debugfs to get to the tracing facility, tracefs is automatically
  7976. * mounted to the debugfs/tracing directory.
  7977. */
  7978. type = get_fs_type("tracefs");
  7979. if (!type)
  7980. return NULL;
  7981. mnt = vfs_submount(mntpt, type, "tracefs", NULL);
  7982. put_filesystem(type);
  7983. if (IS_ERR(mnt))
  7984. return NULL;
  7985. mntget(mnt);
  7986. return mnt;
  7987. }
  7988. /**
  7989. * tracing_init_dentry - initialize top level trace array
  7990. *
  7991. * This is called when creating files or directories in the tracing
  7992. * directory. It is called via fs_initcall() by any of the boot up code
  7993. * and expects to return the dentry of the top level tracing directory.
  7994. */
  7995. int tracing_init_dentry(void)
  7996. {
  7997. struct trace_array *tr = &global_trace;
  7998. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  7999. pr_warn("Tracing disabled due to lockdown\n");
  8000. return -EPERM;
  8001. }
  8002. /* The top level trace array uses NULL as parent */
  8003. if (tr->dir)
  8004. return 0;
  8005. if (WARN_ON(!tracefs_initialized()))
  8006. return -ENODEV;
  8007. /*
  8008. * As there may still be users that expect the tracing
  8009. * files to exist in debugfs/tracing, we must automount
  8010. * the tracefs file system there, so older tools still
  8011. * work with the newer kernel.
  8012. */
  8013. tr->dir = debugfs_create_automount("tracing", NULL,
  8014. trace_automount, NULL);
  8015. return 0;
  8016. }
  8017. extern struct trace_eval_map *__start_ftrace_eval_maps[];
  8018. extern struct trace_eval_map *__stop_ftrace_eval_maps[];
  8019. static struct workqueue_struct *eval_map_wq __initdata;
  8020. static struct work_struct eval_map_work __initdata;
  8021. static struct work_struct tracerfs_init_work __initdata;
  8022. static void __init eval_map_work_func(struct work_struct *work)
  8023. {
  8024. int len;
  8025. len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
  8026. trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
  8027. }
  8028. static int __init trace_eval_init(void)
  8029. {
  8030. INIT_WORK(&eval_map_work, eval_map_work_func);
  8031. eval_map_wq = alloc_workqueue("eval_map_wq", WQ_UNBOUND, 0);
  8032. if (!eval_map_wq) {
  8033. pr_err("Unable to allocate eval_map_wq\n");
  8034. /* Do work here */
  8035. eval_map_work_func(&eval_map_work);
  8036. return -ENOMEM;
  8037. }
  8038. queue_work(eval_map_wq, &eval_map_work);
  8039. return 0;
  8040. }
  8041. subsys_initcall(trace_eval_init);
  8042. static int __init trace_eval_sync(void)
  8043. {
  8044. /* Make sure the eval map updates are finished */
  8045. if (eval_map_wq)
  8046. destroy_workqueue(eval_map_wq);
  8047. return 0;
  8048. }
  8049. late_initcall_sync(trace_eval_sync);
  8050. #ifdef CONFIG_MODULES
  8051. static void trace_module_add_evals(struct module *mod)
  8052. {
  8053. if (!mod->num_trace_evals)
  8054. return;
  8055. /*
  8056. * Modules with bad taint do not have events created, do
  8057. * not bother with enums either.
  8058. */
  8059. if (trace_module_has_bad_taint(mod))
  8060. return;
  8061. trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
  8062. }
  8063. #ifdef CONFIG_TRACE_EVAL_MAP_FILE
  8064. static void trace_module_remove_evals(struct module *mod)
  8065. {
  8066. union trace_eval_map_item *map;
  8067. union trace_eval_map_item **last = &trace_eval_maps;
  8068. if (!mod->num_trace_evals)
  8069. return;
  8070. guard(mutex)(&trace_eval_mutex);
  8071. map = trace_eval_maps;
  8072. while (map) {
  8073. if (map->head.mod == mod)
  8074. break;
  8075. map = trace_eval_jmp_to_tail(map);
  8076. last = &map->tail.next;
  8077. map = map->tail.next;
  8078. }
  8079. if (!map)
  8080. return;
  8081. *last = trace_eval_jmp_to_tail(map)->tail.next;
  8082. kfree(map);
  8083. }
  8084. #else
  8085. static inline void trace_module_remove_evals(struct module *mod) { }
  8086. #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
  8087. static int trace_module_notify(struct notifier_block *self,
  8088. unsigned long val, void *data)
  8089. {
  8090. struct module *mod = data;
  8091. switch (val) {
  8092. case MODULE_STATE_COMING:
  8093. trace_module_add_evals(mod);
  8094. break;
  8095. case MODULE_STATE_GOING:
  8096. trace_module_remove_evals(mod);
  8097. break;
  8098. }
  8099. return NOTIFY_OK;
  8100. }
  8101. static struct notifier_block trace_module_nb = {
  8102. .notifier_call = trace_module_notify,
  8103. .priority = 0,
  8104. };
  8105. #endif /* CONFIG_MODULES */
  8106. static __init void tracer_init_tracefs_work_func(struct work_struct *work)
  8107. {
  8108. event_trace_init();
  8109. init_tracer_tracefs(&global_trace, NULL);
  8110. ftrace_init_tracefs_toplevel(&global_trace, NULL);
  8111. trace_create_file("tracing_thresh", TRACE_MODE_WRITE, NULL,
  8112. &global_trace, &tracing_thresh_fops);
  8113. trace_create_file("README", TRACE_MODE_READ, NULL,
  8114. NULL, &tracing_readme_fops);
  8115. trace_create_file("saved_cmdlines", TRACE_MODE_READ, NULL,
  8116. NULL, &tracing_saved_cmdlines_fops);
  8117. trace_create_file("saved_cmdlines_size", TRACE_MODE_WRITE, NULL,
  8118. NULL, &tracing_saved_cmdlines_size_fops);
  8119. trace_create_file("saved_tgids", TRACE_MODE_READ, NULL,
  8120. NULL, &tracing_saved_tgids_fops);
  8121. trace_create_eval_file(NULL);
  8122. #ifdef CONFIG_MODULES
  8123. register_module_notifier(&trace_module_nb);
  8124. #endif
  8125. #ifdef CONFIG_DYNAMIC_FTRACE
  8126. trace_create_file("dyn_ftrace_total_info", TRACE_MODE_READ, NULL,
  8127. NULL, &tracing_dyn_info_fops);
  8128. #endif
  8129. create_trace_instances(NULL);
  8130. update_tracer_options(&global_trace);
  8131. }
  8132. static __init int tracer_init_tracefs(void)
  8133. {
  8134. int ret;
  8135. trace_access_lock_init();
  8136. ret = tracing_init_dentry();
  8137. if (ret)
  8138. return 0;
  8139. if (eval_map_wq) {
  8140. INIT_WORK(&tracerfs_init_work, tracer_init_tracefs_work_func);
  8141. queue_work(eval_map_wq, &tracerfs_init_work);
  8142. } else {
  8143. tracer_init_tracefs_work_func(NULL);
  8144. }
  8145. rv_init_interface();
  8146. return 0;
  8147. }
  8148. fs_initcall(tracer_init_tracefs);
  8149. static int trace_die_panic_handler(struct notifier_block *self,
  8150. unsigned long ev, void *unused);
  8151. static struct notifier_block trace_panic_notifier = {
  8152. .notifier_call = trace_die_panic_handler,
  8153. .priority = INT_MAX - 1,
  8154. };
  8155. static struct notifier_block trace_die_notifier = {
  8156. .notifier_call = trace_die_panic_handler,
  8157. .priority = INT_MAX - 1,
  8158. };
  8159. /*
  8160. * The idea is to execute the following die/panic callback early, in order
  8161. * to avoid showing irrelevant information in the trace (like other panic
  8162. * notifier functions); we are the 2nd to run, after hung_task/rcu_stall
  8163. * warnings get disabled (to prevent potential log flooding).
  8164. */
  8165. static int trace_die_panic_handler(struct notifier_block *self,
  8166. unsigned long ev, void *unused)
  8167. {
  8168. if (!ftrace_dump_on_oops_enabled())
  8169. return NOTIFY_DONE;
  8170. /* The die notifier requires DIE_OOPS to trigger */
  8171. if (self == &trace_die_notifier && ev != DIE_OOPS)
  8172. return NOTIFY_DONE;
  8173. ftrace_dump(DUMP_PARAM);
  8174. return NOTIFY_DONE;
  8175. }
  8176. /*
  8177. * printk is set to max of 1024, we really don't need it that big.
  8178. * Nothing should be printing 1000 characters anyway.
  8179. */
  8180. #define TRACE_MAX_PRINT 1000
  8181. /*
  8182. * Define here KERN_TRACE so that we have one place to modify
  8183. * it if we decide to change what log level the ftrace dump
  8184. * should be at.
  8185. */
  8186. #define KERN_TRACE KERN_EMERG
  8187. void
  8188. trace_printk_seq(struct trace_seq *s)
  8189. {
  8190. /* Probably should print a warning here. */
  8191. if (s->seq.len >= TRACE_MAX_PRINT)
  8192. s->seq.len = TRACE_MAX_PRINT;
  8193. /*
  8194. * More paranoid code. Although the buffer size is set to
  8195. * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
  8196. * an extra layer of protection.
  8197. */
  8198. if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
  8199. s->seq.len = s->seq.size - 1;
  8200. /* should be zero ended, but we are paranoid. */
  8201. s->buffer[s->seq.len] = 0;
  8202. printk(KERN_TRACE "%s", s->buffer);
  8203. trace_seq_init(s);
  8204. }
  8205. static void trace_init_iter(struct trace_iterator *iter, struct trace_array *tr)
  8206. {
  8207. iter->tr = tr;
  8208. iter->trace = iter->tr->current_trace;
  8209. iter->cpu_file = RING_BUFFER_ALL_CPUS;
  8210. iter->array_buffer = &tr->array_buffer;
  8211. if (iter->trace && iter->trace->open)
  8212. iter->trace->open(iter);
  8213. /* Annotate start of buffers if we had overruns */
  8214. if (ring_buffer_overruns(iter->array_buffer->buffer))
  8215. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  8216. /* Output in nanoseconds only if we are using a clock in nanoseconds. */
  8217. if (trace_clocks[iter->tr->clock_id].in_ns)
  8218. iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
  8219. /* Can not use kmalloc for iter.temp and iter.fmt */
  8220. iter->temp = static_temp_buf;
  8221. iter->temp_size = STATIC_TEMP_BUF_SIZE;
  8222. iter->fmt = static_fmt_buf;
  8223. iter->fmt_size = STATIC_FMT_BUF_SIZE;
  8224. }
  8225. void trace_init_global_iter(struct trace_iterator *iter)
  8226. {
  8227. trace_init_iter(iter, &global_trace);
  8228. }
  8229. static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_mode)
  8230. {
  8231. /* use static because iter can be a bit big for the stack */
  8232. static struct trace_iterator iter;
  8233. unsigned int old_userobj;
  8234. unsigned long flags;
  8235. int cnt = 0, cpu;
  8236. /*
  8237. * Always turn off tracing when we dump.
  8238. * We don't need to show trace output of what happens
  8239. * between multiple crashes.
  8240. *
  8241. * If the user does a sysrq-z, then they can re-enable
  8242. * tracing with echo 1 > tracing_on.
  8243. */
  8244. tracer_tracing_off(tr);
  8245. local_irq_save(flags);
  8246. /* Simulate the iterator */
  8247. trace_init_iter(&iter, tr);
  8248. for_each_tracing_cpu(cpu) {
  8249. atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
  8250. }
  8251. old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
  8252. /* don't look at user memory in panic mode */
  8253. tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  8254. if (dump_mode == DUMP_ORIG)
  8255. iter.cpu_file = raw_smp_processor_id();
  8256. else
  8257. iter.cpu_file = RING_BUFFER_ALL_CPUS;
  8258. if (tr == &global_trace)
  8259. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  8260. else
  8261. printk(KERN_TRACE "Dumping ftrace instance %s buffer:\n", tr->name);
  8262. /* Did function tracer already get disabled? */
  8263. if (ftrace_is_dead()) {
  8264. printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  8265. printk("# MAY BE MISSING FUNCTION EVENTS\n");
  8266. }
  8267. /*
  8268. * We need to stop all tracing on all CPUS to read
  8269. * the next buffer. This is a bit expensive, but is
  8270. * not done often. We fill all what we can read,
  8271. * and then release the locks again.
  8272. */
  8273. while (!trace_empty(&iter)) {
  8274. if (!cnt)
  8275. printk(KERN_TRACE "---------------------------------\n");
  8276. cnt++;
  8277. trace_iterator_reset(&iter);
  8278. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  8279. if (trace_find_next_entry_inc(&iter) != NULL) {
  8280. int ret;
  8281. ret = print_trace_line(&iter);
  8282. if (ret != TRACE_TYPE_NO_CONSUME)
  8283. trace_consume(&iter);
  8284. trace_printk_seq(&iter.seq);
  8285. }
  8286. touch_nmi_watchdog();
  8287. }
  8288. if (!cnt)
  8289. printk(KERN_TRACE " (ftrace buffer empty)\n");
  8290. else
  8291. printk(KERN_TRACE "---------------------------------\n");
  8292. tr->trace_flags |= old_userobj;
  8293. for_each_tracing_cpu(cpu) {
  8294. atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
  8295. }
  8296. local_irq_restore(flags);
  8297. }
  8298. static void ftrace_dump_by_param(void)
  8299. {
  8300. bool first_param = true;
  8301. char dump_param[MAX_TRACER_SIZE];
  8302. char *buf, *token, *inst_name;
  8303. struct trace_array *tr;
  8304. strscpy(dump_param, ftrace_dump_on_oops, MAX_TRACER_SIZE);
  8305. buf = dump_param;
  8306. while ((token = strsep(&buf, ",")) != NULL) {
  8307. if (first_param) {
  8308. first_param = false;
  8309. if (!strcmp("0", token))
  8310. continue;
  8311. else if (!strcmp("1", token)) {
  8312. ftrace_dump_one(&global_trace, DUMP_ALL);
  8313. continue;
  8314. }
  8315. else if (!strcmp("2", token) ||
  8316. !strcmp("orig_cpu", token)) {
  8317. ftrace_dump_one(&global_trace, DUMP_ORIG);
  8318. continue;
  8319. }
  8320. }
  8321. inst_name = strsep(&token, "=");
  8322. tr = trace_array_find(inst_name);
  8323. if (!tr) {
  8324. printk(KERN_TRACE "Instance %s not found\n", inst_name);
  8325. continue;
  8326. }
  8327. if (token && (!strcmp("2", token) ||
  8328. !strcmp("orig_cpu", token)))
  8329. ftrace_dump_one(tr, DUMP_ORIG);
  8330. else
  8331. ftrace_dump_one(tr, DUMP_ALL);
  8332. }
  8333. }
  8334. void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
  8335. {
  8336. static atomic_t dump_running;
  8337. /* Only allow one dump user at a time. */
  8338. if (atomic_inc_return(&dump_running) != 1) {
  8339. atomic_dec(&dump_running);
  8340. return;
  8341. }
  8342. switch (oops_dump_mode) {
  8343. case DUMP_ALL:
  8344. ftrace_dump_one(&global_trace, DUMP_ALL);
  8345. break;
  8346. case DUMP_ORIG:
  8347. ftrace_dump_one(&global_trace, DUMP_ORIG);
  8348. break;
  8349. case DUMP_PARAM:
  8350. ftrace_dump_by_param();
  8351. break;
  8352. case DUMP_NONE:
  8353. break;
  8354. default:
  8355. printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
  8356. ftrace_dump_one(&global_trace, DUMP_ALL);
  8357. }
  8358. atomic_dec(&dump_running);
  8359. }
  8360. EXPORT_SYMBOL_GPL(ftrace_dump);
  8361. #define WRITE_BUFSIZE 4096
  8362. ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
  8363. size_t count, loff_t *ppos,
  8364. int (*createfn)(const char *))
  8365. {
  8366. char *kbuf, *buf, *tmp;
  8367. int ret = 0;
  8368. size_t done = 0;
  8369. size_t size;
  8370. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  8371. if (!kbuf)
  8372. return -ENOMEM;
  8373. while (done < count) {
  8374. size = count - done;
  8375. if (size >= WRITE_BUFSIZE)
  8376. size = WRITE_BUFSIZE - 1;
  8377. if (copy_from_user(kbuf, buffer + done, size)) {
  8378. ret = -EFAULT;
  8379. goto out;
  8380. }
  8381. kbuf[size] = '\0';
  8382. buf = kbuf;
  8383. do {
  8384. tmp = strchr(buf, '\n');
  8385. if (tmp) {
  8386. *tmp = '\0';
  8387. size = tmp - buf + 1;
  8388. } else {
  8389. size = strlen(buf);
  8390. if (done + size < count) {
  8391. if (buf != kbuf)
  8392. break;
  8393. /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
  8394. pr_warn("Line length is too long: Should be less than %d\n",
  8395. WRITE_BUFSIZE - 2);
  8396. ret = -EINVAL;
  8397. goto out;
  8398. }
  8399. }
  8400. done += size;
  8401. /* Remove comments */
  8402. tmp = strchr(buf, '#');
  8403. if (tmp)
  8404. *tmp = '\0';
  8405. ret = createfn(buf);
  8406. if (ret)
  8407. goto out;
  8408. buf += size;
  8409. } while (done < count);
  8410. }
  8411. ret = done;
  8412. out:
  8413. kfree(kbuf);
  8414. return ret;
  8415. }
  8416. #ifdef CONFIG_TRACER_MAX_TRACE
  8417. __init static bool tr_needs_alloc_snapshot(const char *name)
  8418. {
  8419. char *test;
  8420. int len = strlen(name);
  8421. bool ret;
  8422. if (!boot_snapshot_index)
  8423. return false;
  8424. if (strncmp(name, boot_snapshot_info, len) == 0 &&
  8425. boot_snapshot_info[len] == '\t')
  8426. return true;
  8427. test = kmalloc(strlen(name) + 3, GFP_KERNEL);
  8428. if (!test)
  8429. return false;
  8430. sprintf(test, "\t%s\t", name);
  8431. ret = strstr(boot_snapshot_info, test) == NULL;
  8432. kfree(test);
  8433. return ret;
  8434. }
  8435. __init static void do_allocate_snapshot(const char *name)
  8436. {
  8437. if (!tr_needs_alloc_snapshot(name))
  8438. return;
  8439. /*
  8440. * When allocate_snapshot is set, the next call to
  8441. * allocate_trace_buffers() (called by trace_array_get_by_name())
  8442. * will allocate the snapshot buffer. That will alse clear
  8443. * this flag.
  8444. */
  8445. allocate_snapshot = true;
  8446. }
  8447. #else
  8448. static inline void do_allocate_snapshot(const char *name) { }
  8449. #endif
  8450. __init static void enable_instances(void)
  8451. {
  8452. struct trace_array *tr;
  8453. char *curr_str;
  8454. char *name;
  8455. char *str;
  8456. char *tok;
  8457. /* A tab is always appended */
  8458. boot_instance_info[boot_instance_index - 1] = '\0';
  8459. str = boot_instance_info;
  8460. while ((curr_str = strsep(&str, "\t"))) {
  8461. phys_addr_t start = 0;
  8462. phys_addr_t size = 0;
  8463. unsigned long addr = 0;
  8464. bool traceprintk = false;
  8465. bool traceoff = false;
  8466. char *flag_delim;
  8467. char *addr_delim;
  8468. tok = strsep(&curr_str, ",");
  8469. flag_delim = strchr(tok, '^');
  8470. addr_delim = strchr(tok, '@');
  8471. if (addr_delim)
  8472. *addr_delim++ = '\0';
  8473. if (flag_delim)
  8474. *flag_delim++ = '\0';
  8475. name = tok;
  8476. if (flag_delim) {
  8477. char *flag;
  8478. while ((flag = strsep(&flag_delim, "^"))) {
  8479. if (strcmp(flag, "traceoff") == 0) {
  8480. traceoff = true;
  8481. } else if ((strcmp(flag, "printk") == 0) ||
  8482. (strcmp(flag, "traceprintk") == 0) ||
  8483. (strcmp(flag, "trace_printk") == 0)) {
  8484. traceprintk = true;
  8485. } else {
  8486. pr_info("Tracing: Invalid instance flag '%s' for %s\n",
  8487. flag, name);
  8488. }
  8489. }
  8490. }
  8491. tok = addr_delim;
  8492. if (tok && isdigit(*tok)) {
  8493. start = memparse(tok, &tok);
  8494. if (!start) {
  8495. pr_warn("Tracing: Invalid boot instance address for %s\n",
  8496. name);
  8497. continue;
  8498. }
  8499. if (*tok != ':') {
  8500. pr_warn("Tracing: No size specified for instance %s\n", name);
  8501. continue;
  8502. }
  8503. tok++;
  8504. size = memparse(tok, &tok);
  8505. if (!size) {
  8506. pr_warn("Tracing: Invalid boot instance size for %s\n",
  8507. name);
  8508. continue;
  8509. }
  8510. } else if (tok) {
  8511. if (!reserve_mem_find_by_name(tok, &start, &size)) {
  8512. start = 0;
  8513. pr_warn("Failed to map boot instance %s to %s\n", name, tok);
  8514. continue;
  8515. }
  8516. }
  8517. if (start) {
  8518. addr = map_pages(start, size);
  8519. if (addr) {
  8520. pr_info("Tracing: mapped boot instance %s at physical memory %pa of size 0x%lx\n",
  8521. name, &start, (unsigned long)size);
  8522. } else {
  8523. pr_warn("Tracing: Failed to map boot instance %s\n", name);
  8524. continue;
  8525. }
  8526. } else {
  8527. /* Only non mapped buffers have snapshot buffers */
  8528. if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE))
  8529. do_allocate_snapshot(name);
  8530. }
  8531. tr = trace_array_create_systems(name, NULL, addr, size);
  8532. if (IS_ERR(tr)) {
  8533. pr_warn("Tracing: Failed to create instance buffer %s\n", curr_str);
  8534. continue;
  8535. }
  8536. if (traceoff)
  8537. tracer_tracing_off(tr);
  8538. if (traceprintk)
  8539. update_printk_trace(tr);
  8540. /*
  8541. * If start is set, then this is a mapped buffer, and
  8542. * cannot be deleted by user space, so keep the reference
  8543. * to it.
  8544. */
  8545. if (start) {
  8546. tr->flags |= TRACE_ARRAY_FL_BOOT;
  8547. tr->ref++;
  8548. }
  8549. while ((tok = strsep(&curr_str, ","))) {
  8550. early_enable_events(tr, tok, true);
  8551. }
  8552. }
  8553. }
  8554. __init static int tracer_alloc_buffers(void)
  8555. {
  8556. int ring_buf_size;
  8557. int ret = -ENOMEM;
  8558. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  8559. pr_warn("Tracing disabled due to lockdown\n");
  8560. return -EPERM;
  8561. }
  8562. /*
  8563. * Make sure we don't accidentally add more trace options
  8564. * than we have bits for.
  8565. */
  8566. BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
  8567. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  8568. return -ENOMEM;
  8569. if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
  8570. goto out_free_buffer_mask;
  8571. /* Only allocate trace_printk buffers if a trace_printk exists */
  8572. if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt)
  8573. /* Must be called before global_trace.buffer is allocated */
  8574. trace_printk_init_buffers();
  8575. /* To save memory, keep the ring buffer size to its minimum */
  8576. if (global_trace.ring_buffer_expanded)
  8577. ring_buf_size = trace_buf_size;
  8578. else
  8579. ring_buf_size = 1;
  8580. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  8581. cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
  8582. raw_spin_lock_init(&global_trace.start_lock);
  8583. /*
  8584. * The prepare callbacks allocates some memory for the ring buffer. We
  8585. * don't free the buffer if the CPU goes down. If we were to free
  8586. * the buffer, then the user would lose any trace that was in the
  8587. * buffer. The memory will be removed once the "instance" is removed.
  8588. */
  8589. ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
  8590. "trace/RB:prepare", trace_rb_cpu_prepare,
  8591. NULL);
  8592. if (ret < 0)
  8593. goto out_free_cpumask;
  8594. /* Used for event triggers */
  8595. ret = -ENOMEM;
  8596. temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
  8597. if (!temp_buffer)
  8598. goto out_rm_hp_state;
  8599. if (trace_create_savedcmd() < 0)
  8600. goto out_free_temp_buffer;
  8601. if (!zalloc_cpumask_var(&global_trace.pipe_cpumask, GFP_KERNEL))
  8602. goto out_free_savedcmd;
  8603. /* TODO: make the number of buffers hot pluggable with CPUS */
  8604. if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
  8605. MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n");
  8606. goto out_free_pipe_cpumask;
  8607. }
  8608. if (global_trace.buffer_disabled)
  8609. tracing_off();
  8610. if (trace_boot_clock) {
  8611. ret = tracing_set_clock(&global_trace, trace_boot_clock);
  8612. if (ret < 0)
  8613. pr_warn("Trace clock %s not defined, going back to default\n",
  8614. trace_boot_clock);
  8615. }
  8616. /*
  8617. * register_tracer() might reference current_trace, so it
  8618. * needs to be set before we register anything. This is
  8619. * just a bootstrap of current_trace anyway.
  8620. */
  8621. global_trace.current_trace = &nop_trace;
  8622. global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  8623. #ifdef CONFIG_TRACER_MAX_TRACE
  8624. spin_lock_init(&global_trace.snapshot_trigger_lock);
  8625. #endif
  8626. ftrace_init_global_array_ops(&global_trace);
  8627. init_trace_flags_index(&global_trace);
  8628. register_tracer(&nop_trace);
  8629. /* Function tracing may start here (via kernel command line) */
  8630. init_function_trace();
  8631. /* All seems OK, enable tracing */
  8632. tracing_disabled = 0;
  8633. atomic_notifier_chain_register(&panic_notifier_list,
  8634. &trace_panic_notifier);
  8635. register_die_notifier(&trace_die_notifier);
  8636. global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
  8637. INIT_LIST_HEAD(&global_trace.systems);
  8638. INIT_LIST_HEAD(&global_trace.events);
  8639. INIT_LIST_HEAD(&global_trace.hist_vars);
  8640. INIT_LIST_HEAD(&global_trace.err_log);
  8641. list_add(&global_trace.list, &ftrace_trace_arrays);
  8642. apply_trace_boot_options();
  8643. register_snapshot_cmd();
  8644. return 0;
  8645. out_free_pipe_cpumask:
  8646. free_cpumask_var(global_trace.pipe_cpumask);
  8647. out_free_savedcmd:
  8648. trace_free_saved_cmdlines_buffer();
  8649. out_free_temp_buffer:
  8650. ring_buffer_free(temp_buffer);
  8651. out_rm_hp_state:
  8652. cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
  8653. out_free_cpumask:
  8654. free_cpumask_var(global_trace.tracing_cpumask);
  8655. out_free_buffer_mask:
  8656. free_cpumask_var(tracing_buffer_mask);
  8657. return ret;
  8658. }
  8659. void __init ftrace_boot_snapshot(void)
  8660. {
  8661. #ifdef CONFIG_TRACER_MAX_TRACE
  8662. struct trace_array *tr;
  8663. if (!snapshot_at_boot)
  8664. return;
  8665. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  8666. if (!tr->allocated_snapshot)
  8667. continue;
  8668. tracing_snapshot_instance(tr);
  8669. trace_array_puts(tr, "** Boot snapshot taken **\n");
  8670. }
  8671. #endif
  8672. }
  8673. void __init early_trace_init(void)
  8674. {
  8675. if (tracepoint_printk) {
  8676. tracepoint_print_iter =
  8677. kzalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
  8678. if (MEM_FAIL(!tracepoint_print_iter,
  8679. "Failed to allocate trace iterator\n"))
  8680. tracepoint_printk = 0;
  8681. else
  8682. static_key_enable(&tracepoint_printk_key.key);
  8683. }
  8684. tracer_alloc_buffers();
  8685. init_events();
  8686. }
  8687. void __init trace_init(void)
  8688. {
  8689. trace_event_init();
  8690. if (boot_instance_index)
  8691. enable_instances();
  8692. }
  8693. __init static void clear_boot_tracer(void)
  8694. {
  8695. /*
  8696. * The default tracer at boot buffer is an init section.
  8697. * This function is called in lateinit. If we did not
  8698. * find the boot tracer, then clear it out, to prevent
  8699. * later registration from accessing the buffer that is
  8700. * about to be freed.
  8701. */
  8702. if (!default_bootup_tracer)
  8703. return;
  8704. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  8705. default_bootup_tracer);
  8706. default_bootup_tracer = NULL;
  8707. }
  8708. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  8709. __init static void tracing_set_default_clock(void)
  8710. {
  8711. /* sched_clock_stable() is determined in late_initcall */
  8712. if (!trace_boot_clock && !sched_clock_stable()) {
  8713. if (security_locked_down(LOCKDOWN_TRACEFS)) {
  8714. pr_warn("Can not set tracing clock due to lockdown\n");
  8715. return;
  8716. }
  8717. printk(KERN_WARNING
  8718. "Unstable clock detected, switching default tracing clock to \"global\"\n"
  8719. "If you want to keep using the local clock, then add:\n"
  8720. " \"trace_clock=local\"\n"
  8721. "on the kernel command line\n");
  8722. tracing_set_clock(&global_trace, "global");
  8723. }
  8724. }
  8725. #else
  8726. static inline void tracing_set_default_clock(void) { }
  8727. #endif
  8728. __init static int late_trace_init(void)
  8729. {
  8730. if (tracepoint_printk && tracepoint_printk_stop_on_boot) {
  8731. static_key_disable(&tracepoint_printk_key.key);
  8732. tracepoint_printk = 0;
  8733. }
  8734. tracing_set_default_clock();
  8735. clear_boot_tracer();
  8736. return 0;
  8737. }
  8738. late_initcall_sync(late_trace_init);