trace_events_hist.c 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * trace_events_hist - trace event hist triggers
  4. *
  5. * Copyright (C) 2015 Tom Zanussi <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kallsyms.h>
  9. #include <linux/security.h>
  10. #include <linux/mutex.h>
  11. #include <linux/slab.h>
  12. #include <linux/stacktrace.h>
  13. #include <linux/rculist.h>
  14. #include <linux/tracefs.h>
  15. /* for gfp flag names */
  16. #include <linux/trace_events.h>
  17. #include <trace/events/mmflags.h>
  18. #include "tracing_map.h"
  19. #include "trace_synth.h"
  20. #define ERRORS \
  21. C(NONE, "No error"), \
  22. C(DUPLICATE_VAR, "Variable already defined"), \
  23. C(VAR_NOT_UNIQUE, "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
  24. C(TOO_MANY_VARS, "Too many variables defined"), \
  25. C(MALFORMED_ASSIGNMENT, "Malformed assignment"), \
  26. C(NAMED_MISMATCH, "Named hist trigger doesn't match existing named trigger (includes variables)"), \
  27. C(TRIGGER_EEXIST, "Hist trigger already exists"), \
  28. C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \
  29. C(SET_CLOCK_FAIL, "Couldn't set trace_clock"), \
  30. C(BAD_FIELD_MODIFIER, "Invalid field modifier"), \
  31. C(TOO_MANY_SUBEXPR, "Too many subexpressions (3 max)"), \
  32. C(TIMESTAMP_MISMATCH, "Timestamp units in expression don't match"), \
  33. C(TOO_MANY_FIELD_VARS, "Too many field variables defined"), \
  34. C(EVENT_FILE_NOT_FOUND, "Event file not found"), \
  35. C(HIST_NOT_FOUND, "Matching event histogram not found"), \
  36. C(HIST_CREATE_FAIL, "Couldn't create histogram for field"), \
  37. C(SYNTH_VAR_NOT_FOUND, "Couldn't find synthetic variable"), \
  38. C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"), \
  39. C(SYNTH_TYPE_MISMATCH, "Param type doesn't match synthetic event field type"), \
  40. C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \
  41. C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"), \
  42. C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"), \
  43. C(ONX_NOT_VAR, "For onmax(x) or onchange(x), x must be a variable"), \
  44. C(ONX_VAR_NOT_FOUND, "Couldn't find onmax or onchange variable"), \
  45. C(ONX_VAR_CREATE_FAIL, "Couldn't create onmax or onchange variable"), \
  46. C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"), \
  47. C(TOO_MANY_PARAMS, "Too many action params"), \
  48. C(PARAM_NOT_FOUND, "Couldn't find param"), \
  49. C(INVALID_PARAM, "Invalid action param"), \
  50. C(ACTION_NOT_FOUND, "No action found"), \
  51. C(NO_SAVE_PARAMS, "No params found for save()"), \
  52. C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
  53. C(ACTION_MISMATCH, "Handler doesn't support action"), \
  54. C(NO_CLOSING_PAREN, "No closing paren found"), \
  55. C(SUBSYS_NOT_FOUND, "Missing subsystem"), \
  56. C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"), \
  57. C(INVALID_REF_KEY, "Using variable references in keys not supported"), \
  58. C(VAR_NOT_FOUND, "Couldn't find variable"), \
  59. C(FIELD_NOT_FOUND, "Couldn't find field"), \
  60. C(EMPTY_ASSIGNMENT, "Empty assignment"), \
  61. C(INVALID_SORT_MODIFIER,"Invalid sort modifier"), \
  62. C(EMPTY_SORT_FIELD, "Empty sort field"), \
  63. C(TOO_MANY_SORT_FIELDS, "Too many sort fields (Max = 2)"), \
  64. C(INVALID_SORT_FIELD, "Sort field must be a key or a val"), \
  65. C(INVALID_STR_OPERAND, "String type can not be an operand in expression"), \
  66. C(EXPECT_NUMBER, "Expecting numeric literal"), \
  67. C(UNARY_MINUS_SUBEXPR, "Unary minus not supported in sub-expressions"), \
  68. C(DIVISION_BY_ZERO, "Division by zero"),
  69. #undef C
  70. #define C(a, b) HIST_ERR_##a
  71. enum { ERRORS };
  72. #undef C
  73. #define C(a, b) b
  74. static const char *err_text[] = { ERRORS };
  75. struct hist_field;
  76. typedef u64 (*hist_field_fn_t) (struct hist_field *field,
  77. struct tracing_map_elt *elt,
  78. struct trace_buffer *buffer,
  79. struct ring_buffer_event *rbe,
  80. void *event);
  81. #define HIST_FIELD_OPERANDS_MAX 2
  82. #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
  83. #define HIST_ACTIONS_MAX 8
  84. #define HIST_CONST_DIGITS_MAX 21
  85. #define HIST_DIV_SHIFT 20 /* For optimizing division by constants */
  86. enum field_op_id {
  87. FIELD_OP_NONE,
  88. FIELD_OP_PLUS,
  89. FIELD_OP_MINUS,
  90. FIELD_OP_UNARY_MINUS,
  91. FIELD_OP_DIV,
  92. FIELD_OP_MULT,
  93. };
  94. enum hist_field_fn {
  95. HIST_FIELD_FN_NOP,
  96. HIST_FIELD_FN_VAR_REF,
  97. HIST_FIELD_FN_COUNTER,
  98. HIST_FIELD_FN_CONST,
  99. HIST_FIELD_FN_LOG2,
  100. HIST_FIELD_FN_BUCKET,
  101. HIST_FIELD_FN_TIMESTAMP,
  102. HIST_FIELD_FN_CPU,
  103. HIST_FIELD_FN_STRING,
  104. HIST_FIELD_FN_DYNSTRING,
  105. HIST_FIELD_FN_RELDYNSTRING,
  106. HIST_FIELD_FN_PSTRING,
  107. HIST_FIELD_FN_S64,
  108. HIST_FIELD_FN_U64,
  109. HIST_FIELD_FN_S32,
  110. HIST_FIELD_FN_U32,
  111. HIST_FIELD_FN_S16,
  112. HIST_FIELD_FN_U16,
  113. HIST_FIELD_FN_S8,
  114. HIST_FIELD_FN_U8,
  115. HIST_FIELD_FN_UMINUS,
  116. HIST_FIELD_FN_MINUS,
  117. HIST_FIELD_FN_PLUS,
  118. HIST_FIELD_FN_DIV,
  119. HIST_FIELD_FN_MULT,
  120. HIST_FIELD_FN_DIV_POWER2,
  121. HIST_FIELD_FN_DIV_NOT_POWER2,
  122. HIST_FIELD_FN_DIV_MULT_SHIFT,
  123. HIST_FIELD_FN_EXECNAME,
  124. };
  125. /*
  126. * A hist_var (histogram variable) contains variable information for
  127. * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
  128. * flag set. A hist_var has a variable name e.g. ts0, and is
  129. * associated with a given histogram trigger, as specified by
  130. * hist_data. The hist_var idx is the unique index assigned to the
  131. * variable by the hist trigger's tracing_map. The idx is what is
  132. * used to set a variable's value and, by a variable reference, to
  133. * retrieve it.
  134. */
  135. struct hist_var {
  136. char *name;
  137. struct hist_trigger_data *hist_data;
  138. unsigned int idx;
  139. };
  140. struct hist_field {
  141. struct ftrace_event_field *field;
  142. unsigned long flags;
  143. unsigned long buckets;
  144. const char *type;
  145. struct hist_field *operands[HIST_FIELD_OPERANDS_MAX];
  146. struct hist_trigger_data *hist_data;
  147. enum hist_field_fn fn_num;
  148. unsigned int ref;
  149. unsigned int size;
  150. unsigned int offset;
  151. unsigned int is_signed;
  152. /*
  153. * Variable fields contain variable-specific info in var.
  154. */
  155. struct hist_var var;
  156. enum field_op_id operator;
  157. char *system;
  158. char *event_name;
  159. /*
  160. * The name field is used for EXPR and VAR_REF fields. VAR
  161. * fields contain the variable name in var.name.
  162. */
  163. char *name;
  164. /*
  165. * When a histogram trigger is hit, if it has any references
  166. * to variables, the values of those variables are collected
  167. * into a var_ref_vals array by resolve_var_refs(). The
  168. * current value of each variable is read from the tracing_map
  169. * using the hist field's hist_var.idx and entered into the
  170. * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
  171. */
  172. unsigned int var_ref_idx;
  173. bool read_once;
  174. unsigned int var_str_idx;
  175. /* Numeric literals are represented as u64 */
  176. u64 constant;
  177. /* Used to optimize division by constants */
  178. u64 div_multiplier;
  179. };
  180. static u64 hist_fn_call(struct hist_field *hist_field,
  181. struct tracing_map_elt *elt,
  182. struct trace_buffer *buffer,
  183. struct ring_buffer_event *rbe,
  184. void *event);
  185. static u64 hist_field_const(struct hist_field *field,
  186. struct tracing_map_elt *elt,
  187. struct trace_buffer *buffer,
  188. struct ring_buffer_event *rbe,
  189. void *event)
  190. {
  191. return field->constant;
  192. }
  193. static u64 hist_field_counter(struct hist_field *field,
  194. struct tracing_map_elt *elt,
  195. struct trace_buffer *buffer,
  196. struct ring_buffer_event *rbe,
  197. void *event)
  198. {
  199. return 1;
  200. }
  201. static u64 hist_field_string(struct hist_field *hist_field,
  202. struct tracing_map_elt *elt,
  203. struct trace_buffer *buffer,
  204. struct ring_buffer_event *rbe,
  205. void *event)
  206. {
  207. char *addr = (char *)(event + hist_field->field->offset);
  208. return (u64)(unsigned long)addr;
  209. }
  210. static u64 hist_field_dynstring(struct hist_field *hist_field,
  211. struct tracing_map_elt *elt,
  212. struct trace_buffer *buffer,
  213. struct ring_buffer_event *rbe,
  214. void *event)
  215. {
  216. u32 str_item = *(u32 *)(event + hist_field->field->offset);
  217. int str_loc = str_item & 0xffff;
  218. char *addr = (char *)(event + str_loc);
  219. return (u64)(unsigned long)addr;
  220. }
  221. static u64 hist_field_reldynstring(struct hist_field *hist_field,
  222. struct tracing_map_elt *elt,
  223. struct trace_buffer *buffer,
  224. struct ring_buffer_event *rbe,
  225. void *event)
  226. {
  227. u32 *item = event + hist_field->field->offset;
  228. u32 str_item = *item;
  229. int str_loc = str_item & 0xffff;
  230. char *addr = (char *)&item[1] + str_loc;
  231. return (u64)(unsigned long)addr;
  232. }
  233. static u64 hist_field_pstring(struct hist_field *hist_field,
  234. struct tracing_map_elt *elt,
  235. struct trace_buffer *buffer,
  236. struct ring_buffer_event *rbe,
  237. void *event)
  238. {
  239. char **addr = (char **)(event + hist_field->field->offset);
  240. return (u64)(unsigned long)*addr;
  241. }
  242. static u64 hist_field_log2(struct hist_field *hist_field,
  243. struct tracing_map_elt *elt,
  244. struct trace_buffer *buffer,
  245. struct ring_buffer_event *rbe,
  246. void *event)
  247. {
  248. struct hist_field *operand = hist_field->operands[0];
  249. u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
  250. return (u64) ilog2(roundup_pow_of_two(val));
  251. }
  252. static u64 hist_field_bucket(struct hist_field *hist_field,
  253. struct tracing_map_elt *elt,
  254. struct trace_buffer *buffer,
  255. struct ring_buffer_event *rbe,
  256. void *event)
  257. {
  258. struct hist_field *operand = hist_field->operands[0];
  259. unsigned long buckets = hist_field->buckets;
  260. u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
  261. if (WARN_ON_ONCE(!buckets))
  262. return val;
  263. if (val >= LONG_MAX)
  264. val = div64_ul(val, buckets);
  265. else
  266. val = (u64)((unsigned long)val / buckets);
  267. return val * buckets;
  268. }
  269. static u64 hist_field_plus(struct hist_field *hist_field,
  270. struct tracing_map_elt *elt,
  271. struct trace_buffer *buffer,
  272. struct ring_buffer_event *rbe,
  273. void *event)
  274. {
  275. struct hist_field *operand1 = hist_field->operands[0];
  276. struct hist_field *operand2 = hist_field->operands[1];
  277. u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
  278. u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
  279. return val1 + val2;
  280. }
  281. static u64 hist_field_minus(struct hist_field *hist_field,
  282. struct tracing_map_elt *elt,
  283. struct trace_buffer *buffer,
  284. struct ring_buffer_event *rbe,
  285. void *event)
  286. {
  287. struct hist_field *operand1 = hist_field->operands[0];
  288. struct hist_field *operand2 = hist_field->operands[1];
  289. u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
  290. u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
  291. return val1 - val2;
  292. }
  293. static u64 hist_field_div(struct hist_field *hist_field,
  294. struct tracing_map_elt *elt,
  295. struct trace_buffer *buffer,
  296. struct ring_buffer_event *rbe,
  297. void *event)
  298. {
  299. struct hist_field *operand1 = hist_field->operands[0];
  300. struct hist_field *operand2 = hist_field->operands[1];
  301. u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
  302. u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
  303. /* Return -1 for the undefined case */
  304. if (!val2)
  305. return -1;
  306. /* Use shift if the divisor is a power of 2 */
  307. if (!(val2 & (val2 - 1)))
  308. return val1 >> __ffs64(val2);
  309. return div64_u64(val1, val2);
  310. }
  311. static u64 div_by_power_of_two(struct hist_field *hist_field,
  312. struct tracing_map_elt *elt,
  313. struct trace_buffer *buffer,
  314. struct ring_buffer_event *rbe,
  315. void *event)
  316. {
  317. struct hist_field *operand1 = hist_field->operands[0];
  318. struct hist_field *operand2 = hist_field->operands[1];
  319. u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
  320. return val1 >> __ffs64(operand2->constant);
  321. }
  322. static u64 div_by_not_power_of_two(struct hist_field *hist_field,
  323. struct tracing_map_elt *elt,
  324. struct trace_buffer *buffer,
  325. struct ring_buffer_event *rbe,
  326. void *event)
  327. {
  328. struct hist_field *operand1 = hist_field->operands[0];
  329. struct hist_field *operand2 = hist_field->operands[1];
  330. u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
  331. return div64_u64(val1, operand2->constant);
  332. }
  333. static u64 div_by_mult_and_shift(struct hist_field *hist_field,
  334. struct tracing_map_elt *elt,
  335. struct trace_buffer *buffer,
  336. struct ring_buffer_event *rbe,
  337. void *event)
  338. {
  339. struct hist_field *operand1 = hist_field->operands[0];
  340. struct hist_field *operand2 = hist_field->operands[1];
  341. u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
  342. /*
  343. * If the divisor is a constant, do a multiplication and shift instead.
  344. *
  345. * Choose Z = some power of 2. If Y <= Z, then:
  346. * X / Y = (X * (Z / Y)) / Z
  347. *
  348. * (Z / Y) is a constant (mult) which is calculated at parse time, so:
  349. * X / Y = (X * mult) / Z
  350. *
  351. * The division by Z can be replaced by a shift since Z is a power of 2:
  352. * X / Y = (X * mult) >> HIST_DIV_SHIFT
  353. *
  354. * As long, as X < Z the results will not be off by more than 1.
  355. */
  356. if (val1 < (1 << HIST_DIV_SHIFT)) {
  357. u64 mult = operand2->div_multiplier;
  358. return (val1 * mult + ((1 << HIST_DIV_SHIFT) - 1)) >> HIST_DIV_SHIFT;
  359. }
  360. return div64_u64(val1, operand2->constant);
  361. }
  362. static u64 hist_field_mult(struct hist_field *hist_field,
  363. struct tracing_map_elt *elt,
  364. struct trace_buffer *buffer,
  365. struct ring_buffer_event *rbe,
  366. void *event)
  367. {
  368. struct hist_field *operand1 = hist_field->operands[0];
  369. struct hist_field *operand2 = hist_field->operands[1];
  370. u64 val1 = hist_fn_call(operand1, elt, buffer, rbe, event);
  371. u64 val2 = hist_fn_call(operand2, elt, buffer, rbe, event);
  372. return val1 * val2;
  373. }
  374. static u64 hist_field_unary_minus(struct hist_field *hist_field,
  375. struct tracing_map_elt *elt,
  376. struct trace_buffer *buffer,
  377. struct ring_buffer_event *rbe,
  378. void *event)
  379. {
  380. struct hist_field *operand = hist_field->operands[0];
  381. s64 sval = (s64)hist_fn_call(operand, elt, buffer, rbe, event);
  382. u64 val = (u64)-sval;
  383. return val;
  384. }
  385. #define DEFINE_HIST_FIELD_FN(type) \
  386. static u64 hist_field_##type(struct hist_field *hist_field, \
  387. struct tracing_map_elt *elt, \
  388. struct trace_buffer *buffer, \
  389. struct ring_buffer_event *rbe, \
  390. void *event) \
  391. { \
  392. type *addr = (type *)(event + hist_field->field->offset); \
  393. \
  394. return (u64)(unsigned long)*addr; \
  395. }
  396. DEFINE_HIST_FIELD_FN(s64);
  397. DEFINE_HIST_FIELD_FN(u64);
  398. DEFINE_HIST_FIELD_FN(s32);
  399. DEFINE_HIST_FIELD_FN(u32);
  400. DEFINE_HIST_FIELD_FN(s16);
  401. DEFINE_HIST_FIELD_FN(u16);
  402. DEFINE_HIST_FIELD_FN(s8);
  403. DEFINE_HIST_FIELD_FN(u8);
  404. #define for_each_hist_field(i, hist_data) \
  405. for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
  406. #define for_each_hist_val_field(i, hist_data) \
  407. for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
  408. #define for_each_hist_key_field(i, hist_data) \
  409. for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
  410. #define HITCOUNT_IDX 0
  411. #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
  412. enum hist_field_flags {
  413. HIST_FIELD_FL_HITCOUNT = 1 << 0,
  414. HIST_FIELD_FL_KEY = 1 << 1,
  415. HIST_FIELD_FL_STRING = 1 << 2,
  416. HIST_FIELD_FL_HEX = 1 << 3,
  417. HIST_FIELD_FL_SYM = 1 << 4,
  418. HIST_FIELD_FL_SYM_OFFSET = 1 << 5,
  419. HIST_FIELD_FL_EXECNAME = 1 << 6,
  420. HIST_FIELD_FL_SYSCALL = 1 << 7,
  421. HIST_FIELD_FL_STACKTRACE = 1 << 8,
  422. HIST_FIELD_FL_LOG2 = 1 << 9,
  423. HIST_FIELD_FL_TIMESTAMP = 1 << 10,
  424. HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11,
  425. HIST_FIELD_FL_VAR = 1 << 12,
  426. HIST_FIELD_FL_EXPR = 1 << 13,
  427. HIST_FIELD_FL_VAR_REF = 1 << 14,
  428. HIST_FIELD_FL_CPU = 1 << 15,
  429. HIST_FIELD_FL_ALIAS = 1 << 16,
  430. HIST_FIELD_FL_BUCKET = 1 << 17,
  431. HIST_FIELD_FL_CONST = 1 << 18,
  432. HIST_FIELD_FL_PERCENT = 1 << 19,
  433. HIST_FIELD_FL_GRAPH = 1 << 20,
  434. };
  435. struct var_defs {
  436. unsigned int n_vars;
  437. char *name[TRACING_MAP_VARS_MAX];
  438. char *expr[TRACING_MAP_VARS_MAX];
  439. };
  440. struct hist_trigger_attrs {
  441. char *keys_str;
  442. char *vals_str;
  443. char *sort_key_str;
  444. char *name;
  445. char *clock;
  446. bool pause;
  447. bool cont;
  448. bool clear;
  449. bool ts_in_usecs;
  450. unsigned int map_bits;
  451. char *assignment_str[TRACING_MAP_VARS_MAX];
  452. unsigned int n_assignments;
  453. char *action_str[HIST_ACTIONS_MAX];
  454. unsigned int n_actions;
  455. struct var_defs var_defs;
  456. };
  457. struct field_var {
  458. struct hist_field *var;
  459. struct hist_field *val;
  460. };
  461. struct field_var_hist {
  462. struct hist_trigger_data *hist_data;
  463. char *cmd;
  464. };
  465. struct hist_trigger_data {
  466. struct hist_field *fields[HIST_FIELDS_MAX];
  467. unsigned int n_vals;
  468. unsigned int n_keys;
  469. unsigned int n_fields;
  470. unsigned int n_vars;
  471. unsigned int n_var_str;
  472. unsigned int key_size;
  473. struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX];
  474. unsigned int n_sort_keys;
  475. struct trace_event_file *event_file;
  476. struct hist_trigger_attrs *attrs;
  477. struct tracing_map *map;
  478. bool enable_timestamps;
  479. bool remove;
  480. struct hist_field *var_refs[TRACING_MAP_VARS_MAX];
  481. unsigned int n_var_refs;
  482. struct action_data *actions[HIST_ACTIONS_MAX];
  483. unsigned int n_actions;
  484. struct field_var *field_vars[SYNTH_FIELDS_MAX];
  485. unsigned int n_field_vars;
  486. unsigned int n_field_var_str;
  487. struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX];
  488. unsigned int n_field_var_hists;
  489. struct field_var *save_vars[SYNTH_FIELDS_MAX];
  490. unsigned int n_save_vars;
  491. unsigned int n_save_var_str;
  492. };
  493. struct action_data;
  494. typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
  495. struct tracing_map_elt *elt,
  496. struct trace_buffer *buffer, void *rec,
  497. struct ring_buffer_event *rbe, void *key,
  498. struct action_data *data, u64 *var_ref_vals);
  499. typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
  500. enum handler_id {
  501. HANDLER_ONMATCH = 1,
  502. HANDLER_ONMAX,
  503. HANDLER_ONCHANGE,
  504. };
  505. enum action_id {
  506. ACTION_SAVE = 1,
  507. ACTION_TRACE,
  508. ACTION_SNAPSHOT,
  509. };
  510. struct action_data {
  511. enum handler_id handler;
  512. enum action_id action;
  513. char *action_name;
  514. action_fn_t fn;
  515. unsigned int n_params;
  516. char *params[SYNTH_FIELDS_MAX];
  517. /*
  518. * When a histogram trigger is hit, the values of any
  519. * references to variables, including variables being passed
  520. * as parameters to synthetic events, are collected into a
  521. * var_ref_vals array. This var_ref_idx array is an array of
  522. * indices into the var_ref_vals array, one for each synthetic
  523. * event param, and is passed to the synthetic event
  524. * invocation.
  525. */
  526. unsigned int var_ref_idx[SYNTH_FIELDS_MAX];
  527. struct synth_event *synth_event;
  528. bool use_trace_keyword;
  529. char *synth_event_name;
  530. union {
  531. struct {
  532. char *event;
  533. char *event_system;
  534. } match_data;
  535. struct {
  536. /*
  537. * var_str contains the $-unstripped variable
  538. * name referenced by var_ref, and used when
  539. * printing the action. Because var_ref
  540. * creation is deferred to create_actions(),
  541. * we need a per-action way to save it until
  542. * then, thus var_str.
  543. */
  544. char *var_str;
  545. /*
  546. * var_ref refers to the variable being
  547. * tracked e.g onmax($var).
  548. */
  549. struct hist_field *var_ref;
  550. /*
  551. * track_var contains the 'invisible' tracking
  552. * variable created to keep the current
  553. * e.g. max value.
  554. */
  555. struct hist_field *track_var;
  556. check_track_val_fn_t check_val;
  557. action_fn_t save_data;
  558. } track_data;
  559. };
  560. };
  561. struct track_data {
  562. u64 track_val;
  563. bool updated;
  564. unsigned int key_len;
  565. void *key;
  566. struct tracing_map_elt elt;
  567. struct action_data *action_data;
  568. struct hist_trigger_data *hist_data;
  569. };
  570. struct hist_elt_data {
  571. char *comm;
  572. u64 *var_ref_vals;
  573. char **field_var_str;
  574. int n_field_var_str;
  575. };
  576. struct snapshot_context {
  577. struct tracing_map_elt *elt;
  578. void *key;
  579. };
  580. /*
  581. * Returns the specific division function to use if the divisor
  582. * is constant. This avoids extra branches when the trigger is hit.
  583. */
  584. static enum hist_field_fn hist_field_get_div_fn(struct hist_field *divisor)
  585. {
  586. u64 div = divisor->constant;
  587. if (!(div & (div - 1)))
  588. return HIST_FIELD_FN_DIV_POWER2;
  589. /* If the divisor is too large, do a regular division */
  590. if (div > (1 << HIST_DIV_SHIFT))
  591. return HIST_FIELD_FN_DIV_NOT_POWER2;
  592. divisor->div_multiplier = div64_u64((u64)(1 << HIST_DIV_SHIFT), div);
  593. return HIST_FIELD_FN_DIV_MULT_SHIFT;
  594. }
  595. static void track_data_free(struct track_data *track_data)
  596. {
  597. struct hist_elt_data *elt_data;
  598. if (!track_data)
  599. return;
  600. kfree(track_data->key);
  601. elt_data = track_data->elt.private_data;
  602. if (elt_data) {
  603. kfree(elt_data->comm);
  604. kfree(elt_data);
  605. }
  606. kfree(track_data);
  607. }
  608. static struct track_data *track_data_alloc(unsigned int key_len,
  609. struct action_data *action_data,
  610. struct hist_trigger_data *hist_data)
  611. {
  612. struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
  613. struct hist_elt_data *elt_data;
  614. if (!data)
  615. return ERR_PTR(-ENOMEM);
  616. data->key = kzalloc(key_len, GFP_KERNEL);
  617. if (!data->key) {
  618. track_data_free(data);
  619. return ERR_PTR(-ENOMEM);
  620. }
  621. data->key_len = key_len;
  622. data->action_data = action_data;
  623. data->hist_data = hist_data;
  624. elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
  625. if (!elt_data) {
  626. track_data_free(data);
  627. return ERR_PTR(-ENOMEM);
  628. }
  629. data->elt.private_data = elt_data;
  630. elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
  631. if (!elt_data->comm) {
  632. track_data_free(data);
  633. return ERR_PTR(-ENOMEM);
  634. }
  635. return data;
  636. }
  637. #define HIST_PREFIX "hist:"
  638. static char *last_cmd;
  639. static char last_cmd_loc[MAX_FILTER_STR_VAL];
  640. static int errpos(char *str)
  641. {
  642. if (!str || !last_cmd)
  643. return 0;
  644. return err_pos(last_cmd, str);
  645. }
  646. static void last_cmd_set(struct trace_event_file *file, char *str)
  647. {
  648. const char *system = NULL, *name = NULL;
  649. struct trace_event_call *call;
  650. int len;
  651. if (!str)
  652. return;
  653. /* sizeof() contains the nul byte */
  654. len = sizeof(HIST_PREFIX) + strlen(str);
  655. kfree(last_cmd);
  656. last_cmd = kzalloc(len, GFP_KERNEL);
  657. if (!last_cmd)
  658. return;
  659. strcpy(last_cmd, HIST_PREFIX);
  660. /* Again, sizeof() contains the nul byte */
  661. len -= sizeof(HIST_PREFIX);
  662. strncat(last_cmd, str, len);
  663. if (file) {
  664. call = file->event_call;
  665. system = call->class->system;
  666. if (system) {
  667. name = trace_event_name(call);
  668. if (!name)
  669. system = NULL;
  670. }
  671. }
  672. if (system)
  673. snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, HIST_PREFIX "%s:%s", system, name);
  674. }
  675. static void hist_err(struct trace_array *tr, u8 err_type, u16 err_pos)
  676. {
  677. if (!last_cmd)
  678. return;
  679. tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
  680. err_type, err_pos);
  681. }
  682. static void hist_err_clear(void)
  683. {
  684. if (last_cmd)
  685. last_cmd[0] = '\0';
  686. last_cmd_loc[0] = '\0';
  687. }
  688. typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
  689. unsigned int *var_ref_idx);
  690. static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
  691. unsigned int *var_ref_idx)
  692. {
  693. struct tracepoint *tp = event->tp;
  694. if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
  695. struct tracepoint_func *probe_func_ptr;
  696. synth_probe_func_t probe_func;
  697. void *__data;
  698. if (!(cpu_online(raw_smp_processor_id())))
  699. return;
  700. probe_func_ptr = rcu_dereference_sched((tp)->funcs);
  701. if (probe_func_ptr) {
  702. do {
  703. probe_func = probe_func_ptr->func;
  704. __data = probe_func_ptr->data;
  705. probe_func(__data, var_ref_vals, var_ref_idx);
  706. } while ((++probe_func_ptr)->func);
  707. }
  708. }
  709. }
  710. static void action_trace(struct hist_trigger_data *hist_data,
  711. struct tracing_map_elt *elt,
  712. struct trace_buffer *buffer, void *rec,
  713. struct ring_buffer_event *rbe, void *key,
  714. struct action_data *data, u64 *var_ref_vals)
  715. {
  716. struct synth_event *event = data->synth_event;
  717. trace_synth(event, var_ref_vals, data->var_ref_idx);
  718. }
  719. struct hist_var_data {
  720. struct list_head list;
  721. struct hist_trigger_data *hist_data;
  722. };
  723. static u64 hist_field_timestamp(struct hist_field *hist_field,
  724. struct tracing_map_elt *elt,
  725. struct trace_buffer *buffer,
  726. struct ring_buffer_event *rbe,
  727. void *event)
  728. {
  729. struct hist_trigger_data *hist_data = hist_field->hist_data;
  730. struct trace_array *tr = hist_data->event_file->tr;
  731. u64 ts = ring_buffer_event_time_stamp(buffer, rbe);
  732. if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
  733. ts = ns2usecs(ts);
  734. return ts;
  735. }
  736. static u64 hist_field_cpu(struct hist_field *hist_field,
  737. struct tracing_map_elt *elt,
  738. struct trace_buffer *buffer,
  739. struct ring_buffer_event *rbe,
  740. void *event)
  741. {
  742. int cpu = smp_processor_id();
  743. return cpu;
  744. }
  745. /**
  746. * check_field_for_var_ref - Check if a VAR_REF field references a variable
  747. * @hist_field: The VAR_REF field to check
  748. * @var_data: The hist trigger that owns the variable
  749. * @var_idx: The trigger variable identifier
  750. *
  751. * Check the given VAR_REF field to see whether or not it references
  752. * the given variable associated with the given trigger.
  753. *
  754. * Return: The VAR_REF field if it does reference the variable, NULL if not
  755. */
  756. static struct hist_field *
  757. check_field_for_var_ref(struct hist_field *hist_field,
  758. struct hist_trigger_data *var_data,
  759. unsigned int var_idx)
  760. {
  761. WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
  762. if (hist_field && hist_field->var.idx == var_idx &&
  763. hist_field->var.hist_data == var_data)
  764. return hist_field;
  765. return NULL;
  766. }
  767. /**
  768. * find_var_ref - Check if a trigger has a reference to a trigger variable
  769. * @hist_data: The hist trigger that might have a reference to the variable
  770. * @var_data: The hist trigger that owns the variable
  771. * @var_idx: The trigger variable identifier
  772. *
  773. * Check the list of var_refs[] on the first hist trigger to see
  774. * whether any of them are references to the variable on the second
  775. * trigger.
  776. *
  777. * Return: The VAR_REF field referencing the variable if so, NULL if not
  778. */
  779. static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
  780. struct hist_trigger_data *var_data,
  781. unsigned int var_idx)
  782. {
  783. struct hist_field *hist_field;
  784. unsigned int i;
  785. for (i = 0; i < hist_data->n_var_refs; i++) {
  786. hist_field = hist_data->var_refs[i];
  787. if (check_field_for_var_ref(hist_field, var_data, var_idx))
  788. return hist_field;
  789. }
  790. return NULL;
  791. }
  792. /**
  793. * find_any_var_ref - Check if there is a reference to a given trigger variable
  794. * @hist_data: The hist trigger
  795. * @var_idx: The trigger variable identifier
  796. *
  797. * Check to see whether the given variable is currently referenced by
  798. * any other trigger.
  799. *
  800. * The trigger the variable is defined on is explicitly excluded - the
  801. * assumption being that a self-reference doesn't prevent a trigger
  802. * from being removed.
  803. *
  804. * Return: The VAR_REF field referencing the variable if so, NULL if not
  805. */
  806. static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
  807. unsigned int var_idx)
  808. {
  809. struct trace_array *tr = hist_data->event_file->tr;
  810. struct hist_field *found = NULL;
  811. struct hist_var_data *var_data;
  812. list_for_each_entry(var_data, &tr->hist_vars, list) {
  813. if (var_data->hist_data == hist_data)
  814. continue;
  815. found = find_var_ref(var_data->hist_data, hist_data, var_idx);
  816. if (found)
  817. break;
  818. }
  819. return found;
  820. }
  821. /**
  822. * check_var_refs - Check if there is a reference to any of trigger's variables
  823. * @hist_data: The hist trigger
  824. *
  825. * A trigger can define one or more variables. If any one of them is
  826. * currently referenced by any other trigger, this function will
  827. * determine that.
  828. *
  829. * Typically used to determine whether or not a trigger can be removed
  830. * - if there are any references to a trigger's variables, it cannot.
  831. *
  832. * Return: True if there is a reference to any of trigger's variables
  833. */
  834. static bool check_var_refs(struct hist_trigger_data *hist_data)
  835. {
  836. struct hist_field *field;
  837. bool found = false;
  838. int i;
  839. for_each_hist_field(i, hist_data) {
  840. field = hist_data->fields[i];
  841. if (field && field->flags & HIST_FIELD_FL_VAR) {
  842. if (find_any_var_ref(hist_data, field->var.idx)) {
  843. found = true;
  844. break;
  845. }
  846. }
  847. }
  848. return found;
  849. }
  850. static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
  851. {
  852. struct trace_array *tr = hist_data->event_file->tr;
  853. struct hist_var_data *var_data, *found = NULL;
  854. list_for_each_entry(var_data, &tr->hist_vars, list) {
  855. if (var_data->hist_data == hist_data) {
  856. found = var_data;
  857. break;
  858. }
  859. }
  860. return found;
  861. }
  862. static bool field_has_hist_vars(struct hist_field *hist_field,
  863. unsigned int level)
  864. {
  865. int i;
  866. if (level > 3)
  867. return false;
  868. if (!hist_field)
  869. return false;
  870. if (hist_field->flags & HIST_FIELD_FL_VAR ||
  871. hist_field->flags & HIST_FIELD_FL_VAR_REF)
  872. return true;
  873. for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
  874. struct hist_field *operand;
  875. operand = hist_field->operands[i];
  876. if (field_has_hist_vars(operand, level + 1))
  877. return true;
  878. }
  879. return false;
  880. }
  881. static bool has_hist_vars(struct hist_trigger_data *hist_data)
  882. {
  883. struct hist_field *hist_field;
  884. int i;
  885. for_each_hist_field(i, hist_data) {
  886. hist_field = hist_data->fields[i];
  887. if (field_has_hist_vars(hist_field, 0))
  888. return true;
  889. }
  890. return false;
  891. }
  892. static int save_hist_vars(struct hist_trigger_data *hist_data)
  893. {
  894. struct trace_array *tr = hist_data->event_file->tr;
  895. struct hist_var_data *var_data;
  896. var_data = find_hist_vars(hist_data);
  897. if (var_data)
  898. return 0;
  899. if (tracing_check_open_get_tr(tr))
  900. return -ENODEV;
  901. var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
  902. if (!var_data) {
  903. trace_array_put(tr);
  904. return -ENOMEM;
  905. }
  906. var_data->hist_data = hist_data;
  907. list_add(&var_data->list, &tr->hist_vars);
  908. return 0;
  909. }
  910. static void remove_hist_vars(struct hist_trigger_data *hist_data)
  911. {
  912. struct trace_array *tr = hist_data->event_file->tr;
  913. struct hist_var_data *var_data;
  914. var_data = find_hist_vars(hist_data);
  915. if (!var_data)
  916. return;
  917. if (WARN_ON(check_var_refs(hist_data)))
  918. return;
  919. list_del(&var_data->list);
  920. kfree(var_data);
  921. trace_array_put(tr);
  922. }
  923. static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
  924. const char *var_name)
  925. {
  926. struct hist_field *hist_field, *found = NULL;
  927. int i;
  928. for_each_hist_field(i, hist_data) {
  929. hist_field = hist_data->fields[i];
  930. if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
  931. strcmp(hist_field->var.name, var_name) == 0) {
  932. found = hist_field;
  933. break;
  934. }
  935. }
  936. return found;
  937. }
  938. static struct hist_field *find_var(struct hist_trigger_data *hist_data,
  939. struct trace_event_file *file,
  940. const char *var_name)
  941. {
  942. struct hist_trigger_data *test_data;
  943. struct event_trigger_data *test;
  944. struct hist_field *hist_field;
  945. lockdep_assert_held(&event_mutex);
  946. hist_field = find_var_field(hist_data, var_name);
  947. if (hist_field)
  948. return hist_field;
  949. list_for_each_entry(test, &file->triggers, list) {
  950. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  951. test_data = test->private_data;
  952. hist_field = find_var_field(test_data, var_name);
  953. if (hist_field)
  954. return hist_field;
  955. }
  956. }
  957. return NULL;
  958. }
  959. static struct trace_event_file *find_var_file(struct trace_array *tr,
  960. char *system,
  961. char *event_name,
  962. char *var_name)
  963. {
  964. struct hist_trigger_data *var_hist_data;
  965. struct hist_var_data *var_data;
  966. struct trace_event_file *file, *found = NULL;
  967. if (system)
  968. return find_event_file(tr, system, event_name);
  969. list_for_each_entry(var_data, &tr->hist_vars, list) {
  970. var_hist_data = var_data->hist_data;
  971. file = var_hist_data->event_file;
  972. if (file == found)
  973. continue;
  974. if (find_var_field(var_hist_data, var_name)) {
  975. if (found) {
  976. hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
  977. return NULL;
  978. }
  979. found = file;
  980. }
  981. }
  982. return found;
  983. }
  984. static struct hist_field *find_file_var(struct trace_event_file *file,
  985. const char *var_name)
  986. {
  987. struct hist_trigger_data *test_data;
  988. struct event_trigger_data *test;
  989. struct hist_field *hist_field;
  990. lockdep_assert_held(&event_mutex);
  991. list_for_each_entry(test, &file->triggers, list) {
  992. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  993. test_data = test->private_data;
  994. hist_field = find_var_field(test_data, var_name);
  995. if (hist_field)
  996. return hist_field;
  997. }
  998. }
  999. return NULL;
  1000. }
  1001. static struct hist_field *
  1002. find_match_var(struct hist_trigger_data *hist_data, char *var_name)
  1003. {
  1004. struct trace_array *tr = hist_data->event_file->tr;
  1005. struct hist_field *hist_field, *found = NULL;
  1006. struct trace_event_file *file;
  1007. unsigned int i;
  1008. for (i = 0; i < hist_data->n_actions; i++) {
  1009. struct action_data *data = hist_data->actions[i];
  1010. if (data->handler == HANDLER_ONMATCH) {
  1011. char *system = data->match_data.event_system;
  1012. char *event_name = data->match_data.event;
  1013. file = find_var_file(tr, system, event_name, var_name);
  1014. if (!file)
  1015. continue;
  1016. hist_field = find_file_var(file, var_name);
  1017. if (hist_field) {
  1018. if (found) {
  1019. hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
  1020. errpos(var_name));
  1021. return ERR_PTR(-EINVAL);
  1022. }
  1023. found = hist_field;
  1024. }
  1025. }
  1026. }
  1027. return found;
  1028. }
  1029. static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
  1030. char *system,
  1031. char *event_name,
  1032. char *var_name)
  1033. {
  1034. struct trace_array *tr = hist_data->event_file->tr;
  1035. struct hist_field *hist_field = NULL;
  1036. struct trace_event_file *file;
  1037. if (!system || !event_name) {
  1038. hist_field = find_match_var(hist_data, var_name);
  1039. if (IS_ERR(hist_field))
  1040. return NULL;
  1041. if (hist_field)
  1042. return hist_field;
  1043. }
  1044. file = find_var_file(tr, system, event_name, var_name);
  1045. if (!file)
  1046. return NULL;
  1047. hist_field = find_file_var(file, var_name);
  1048. return hist_field;
  1049. }
  1050. static u64 hist_field_var_ref(struct hist_field *hist_field,
  1051. struct tracing_map_elt *elt,
  1052. struct trace_buffer *buffer,
  1053. struct ring_buffer_event *rbe,
  1054. void *event)
  1055. {
  1056. struct hist_elt_data *elt_data;
  1057. u64 var_val = 0;
  1058. if (WARN_ON_ONCE(!elt))
  1059. return var_val;
  1060. elt_data = elt->private_data;
  1061. var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
  1062. return var_val;
  1063. }
  1064. static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
  1065. u64 *var_ref_vals, bool self)
  1066. {
  1067. struct hist_trigger_data *var_data;
  1068. struct tracing_map_elt *var_elt;
  1069. struct hist_field *hist_field;
  1070. unsigned int i, var_idx;
  1071. bool resolved = true;
  1072. u64 var_val = 0;
  1073. for (i = 0; i < hist_data->n_var_refs; i++) {
  1074. hist_field = hist_data->var_refs[i];
  1075. var_idx = hist_field->var.idx;
  1076. var_data = hist_field->var.hist_data;
  1077. if (var_data == NULL) {
  1078. resolved = false;
  1079. break;
  1080. }
  1081. if ((self && var_data != hist_data) ||
  1082. (!self && var_data == hist_data))
  1083. continue;
  1084. var_elt = tracing_map_lookup(var_data->map, key);
  1085. if (!var_elt) {
  1086. resolved = false;
  1087. break;
  1088. }
  1089. if (!tracing_map_var_set(var_elt, var_idx)) {
  1090. resolved = false;
  1091. break;
  1092. }
  1093. if (self || !hist_field->read_once)
  1094. var_val = tracing_map_read_var(var_elt, var_idx);
  1095. else
  1096. var_val = tracing_map_read_var_once(var_elt, var_idx);
  1097. var_ref_vals[i] = var_val;
  1098. }
  1099. return resolved;
  1100. }
  1101. static const char *hist_field_name(struct hist_field *field,
  1102. unsigned int level)
  1103. {
  1104. const char *field_name = "";
  1105. if (WARN_ON_ONCE(!field))
  1106. return field_name;
  1107. if (level > 1)
  1108. return field_name;
  1109. if (field->field)
  1110. field_name = field->field->name;
  1111. else if (field->flags & HIST_FIELD_FL_LOG2 ||
  1112. field->flags & HIST_FIELD_FL_ALIAS ||
  1113. field->flags & HIST_FIELD_FL_BUCKET)
  1114. field_name = hist_field_name(field->operands[0], ++level);
  1115. else if (field->flags & HIST_FIELD_FL_CPU)
  1116. field_name = "common_cpu";
  1117. else if (field->flags & HIST_FIELD_FL_EXPR ||
  1118. field->flags & HIST_FIELD_FL_VAR_REF) {
  1119. if (field->system) {
  1120. static char full_name[MAX_FILTER_STR_VAL];
  1121. strcat(full_name, field->system);
  1122. strcat(full_name, ".");
  1123. strcat(full_name, field->event_name);
  1124. strcat(full_name, ".");
  1125. strcat(full_name, field->name);
  1126. field_name = full_name;
  1127. } else
  1128. field_name = field->name;
  1129. } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
  1130. field_name = "common_timestamp";
  1131. if (field_name == NULL)
  1132. field_name = "";
  1133. return field_name;
  1134. }
  1135. static enum hist_field_fn select_value_fn(int field_size, int field_is_signed)
  1136. {
  1137. switch (field_size) {
  1138. case 8:
  1139. if (field_is_signed)
  1140. return HIST_FIELD_FN_S64;
  1141. else
  1142. return HIST_FIELD_FN_U64;
  1143. case 4:
  1144. if (field_is_signed)
  1145. return HIST_FIELD_FN_S32;
  1146. else
  1147. return HIST_FIELD_FN_U32;
  1148. case 2:
  1149. if (field_is_signed)
  1150. return HIST_FIELD_FN_S16;
  1151. else
  1152. return HIST_FIELD_FN_U16;
  1153. case 1:
  1154. if (field_is_signed)
  1155. return HIST_FIELD_FN_S8;
  1156. else
  1157. return HIST_FIELD_FN_U8;
  1158. }
  1159. return HIST_FIELD_FN_NOP;
  1160. }
  1161. static int parse_map_size(char *str)
  1162. {
  1163. unsigned long size, map_bits;
  1164. int ret;
  1165. ret = kstrtoul(str, 0, &size);
  1166. if (ret)
  1167. goto out;
  1168. map_bits = ilog2(roundup_pow_of_two(size));
  1169. if (map_bits < TRACING_MAP_BITS_MIN ||
  1170. map_bits > TRACING_MAP_BITS_MAX)
  1171. ret = -EINVAL;
  1172. else
  1173. ret = map_bits;
  1174. out:
  1175. return ret;
  1176. }
  1177. static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
  1178. {
  1179. unsigned int i;
  1180. if (!attrs)
  1181. return;
  1182. for (i = 0; i < attrs->n_assignments; i++)
  1183. kfree(attrs->assignment_str[i]);
  1184. for (i = 0; i < attrs->n_actions; i++)
  1185. kfree(attrs->action_str[i]);
  1186. kfree(attrs->name);
  1187. kfree(attrs->sort_key_str);
  1188. kfree(attrs->keys_str);
  1189. kfree(attrs->vals_str);
  1190. kfree(attrs->clock);
  1191. kfree(attrs);
  1192. }
  1193. static int parse_action(char *str, struct hist_trigger_attrs *attrs)
  1194. {
  1195. int ret = -EINVAL;
  1196. if (attrs->n_actions >= HIST_ACTIONS_MAX)
  1197. return ret;
  1198. if ((str_has_prefix(str, "onmatch(")) ||
  1199. (str_has_prefix(str, "onmax(")) ||
  1200. (str_has_prefix(str, "onchange("))) {
  1201. attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
  1202. if (!attrs->action_str[attrs->n_actions]) {
  1203. ret = -ENOMEM;
  1204. return ret;
  1205. }
  1206. attrs->n_actions++;
  1207. ret = 0;
  1208. }
  1209. return ret;
  1210. }
  1211. static int parse_assignment(struct trace_array *tr,
  1212. char *str, struct hist_trigger_attrs *attrs)
  1213. {
  1214. int len, ret = 0;
  1215. if ((len = str_has_prefix(str, "key=")) ||
  1216. (len = str_has_prefix(str, "keys="))) {
  1217. attrs->keys_str = kstrdup(str + len, GFP_KERNEL);
  1218. if (!attrs->keys_str) {
  1219. ret = -ENOMEM;
  1220. goto out;
  1221. }
  1222. } else if ((len = str_has_prefix(str, "val=")) ||
  1223. (len = str_has_prefix(str, "vals=")) ||
  1224. (len = str_has_prefix(str, "values="))) {
  1225. attrs->vals_str = kstrdup(str + len, GFP_KERNEL);
  1226. if (!attrs->vals_str) {
  1227. ret = -ENOMEM;
  1228. goto out;
  1229. }
  1230. } else if ((len = str_has_prefix(str, "sort="))) {
  1231. attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL);
  1232. if (!attrs->sort_key_str) {
  1233. ret = -ENOMEM;
  1234. goto out;
  1235. }
  1236. } else if (str_has_prefix(str, "name=")) {
  1237. attrs->name = kstrdup(str, GFP_KERNEL);
  1238. if (!attrs->name) {
  1239. ret = -ENOMEM;
  1240. goto out;
  1241. }
  1242. } else if ((len = str_has_prefix(str, "clock="))) {
  1243. str += len;
  1244. str = strstrip(str);
  1245. attrs->clock = kstrdup(str, GFP_KERNEL);
  1246. if (!attrs->clock) {
  1247. ret = -ENOMEM;
  1248. goto out;
  1249. }
  1250. } else if ((len = str_has_prefix(str, "size="))) {
  1251. int map_bits = parse_map_size(str + len);
  1252. if (map_bits < 0) {
  1253. ret = map_bits;
  1254. goto out;
  1255. }
  1256. attrs->map_bits = map_bits;
  1257. } else {
  1258. char *assignment;
  1259. if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
  1260. hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
  1261. ret = -EINVAL;
  1262. goto out;
  1263. }
  1264. assignment = kstrdup(str, GFP_KERNEL);
  1265. if (!assignment) {
  1266. ret = -ENOMEM;
  1267. goto out;
  1268. }
  1269. attrs->assignment_str[attrs->n_assignments++] = assignment;
  1270. }
  1271. out:
  1272. return ret;
  1273. }
  1274. static struct hist_trigger_attrs *
  1275. parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
  1276. {
  1277. struct hist_trigger_attrs *attrs;
  1278. int ret = 0;
  1279. attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
  1280. if (!attrs)
  1281. return ERR_PTR(-ENOMEM);
  1282. while (trigger_str) {
  1283. char *str = strsep(&trigger_str, ":");
  1284. char *rhs;
  1285. rhs = strchr(str, '=');
  1286. if (rhs) {
  1287. if (!strlen(++rhs)) {
  1288. ret = -EINVAL;
  1289. hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str));
  1290. goto free;
  1291. }
  1292. ret = parse_assignment(tr, str, attrs);
  1293. if (ret)
  1294. goto free;
  1295. } else if (strcmp(str, "pause") == 0)
  1296. attrs->pause = true;
  1297. else if ((strcmp(str, "cont") == 0) ||
  1298. (strcmp(str, "continue") == 0))
  1299. attrs->cont = true;
  1300. else if (strcmp(str, "clear") == 0)
  1301. attrs->clear = true;
  1302. else {
  1303. ret = parse_action(str, attrs);
  1304. if (ret)
  1305. goto free;
  1306. }
  1307. }
  1308. if (!attrs->keys_str) {
  1309. ret = -EINVAL;
  1310. goto free;
  1311. }
  1312. if (!attrs->clock) {
  1313. attrs->clock = kstrdup("global", GFP_KERNEL);
  1314. if (!attrs->clock) {
  1315. ret = -ENOMEM;
  1316. goto free;
  1317. }
  1318. }
  1319. return attrs;
  1320. free:
  1321. destroy_hist_trigger_attrs(attrs);
  1322. return ERR_PTR(ret);
  1323. }
  1324. static inline void save_comm(char *comm, struct task_struct *task)
  1325. {
  1326. if (!task->pid) {
  1327. strcpy(comm, "<idle>");
  1328. return;
  1329. }
  1330. if (WARN_ON_ONCE(task->pid < 0)) {
  1331. strcpy(comm, "<XXX>");
  1332. return;
  1333. }
  1334. strncpy(comm, task->comm, TASK_COMM_LEN);
  1335. }
  1336. static void hist_elt_data_free(struct hist_elt_data *elt_data)
  1337. {
  1338. unsigned int i;
  1339. for (i = 0; i < elt_data->n_field_var_str; i++)
  1340. kfree(elt_data->field_var_str[i]);
  1341. kfree(elt_data->field_var_str);
  1342. kfree(elt_data->comm);
  1343. kfree(elt_data);
  1344. }
  1345. static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
  1346. {
  1347. struct hist_elt_data *elt_data = elt->private_data;
  1348. hist_elt_data_free(elt_data);
  1349. }
  1350. static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
  1351. {
  1352. struct hist_trigger_data *hist_data = elt->map->private_data;
  1353. unsigned int size = TASK_COMM_LEN;
  1354. struct hist_elt_data *elt_data;
  1355. struct hist_field *hist_field;
  1356. unsigned int i, n_str;
  1357. elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
  1358. if (!elt_data)
  1359. return -ENOMEM;
  1360. for_each_hist_field(i, hist_data) {
  1361. hist_field = hist_data->fields[i];
  1362. if (hist_field->flags & HIST_FIELD_FL_EXECNAME) {
  1363. elt_data->comm = kzalloc(size, GFP_KERNEL);
  1364. if (!elt_data->comm) {
  1365. kfree(elt_data);
  1366. return -ENOMEM;
  1367. }
  1368. break;
  1369. }
  1370. }
  1371. n_str = hist_data->n_field_var_str + hist_data->n_save_var_str +
  1372. hist_data->n_var_str;
  1373. if (n_str > SYNTH_FIELDS_MAX) {
  1374. hist_elt_data_free(elt_data);
  1375. return -EINVAL;
  1376. }
  1377. BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1));
  1378. size = STR_VAR_LEN_MAX;
  1379. elt_data->field_var_str = kcalloc(n_str, sizeof(char *), GFP_KERNEL);
  1380. if (!elt_data->field_var_str) {
  1381. hist_elt_data_free(elt_data);
  1382. return -EINVAL;
  1383. }
  1384. elt_data->n_field_var_str = n_str;
  1385. for (i = 0; i < n_str; i++) {
  1386. elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
  1387. if (!elt_data->field_var_str[i]) {
  1388. hist_elt_data_free(elt_data);
  1389. return -ENOMEM;
  1390. }
  1391. }
  1392. elt->private_data = elt_data;
  1393. return 0;
  1394. }
  1395. static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
  1396. {
  1397. struct hist_elt_data *elt_data = elt->private_data;
  1398. if (elt_data->comm)
  1399. save_comm(elt_data->comm, current);
  1400. }
  1401. static const struct tracing_map_ops hist_trigger_elt_data_ops = {
  1402. .elt_alloc = hist_trigger_elt_data_alloc,
  1403. .elt_free = hist_trigger_elt_data_free,
  1404. .elt_init = hist_trigger_elt_data_init,
  1405. };
  1406. static const char *get_hist_field_flags(struct hist_field *hist_field)
  1407. {
  1408. const char *flags_str = NULL;
  1409. if (hist_field->flags & HIST_FIELD_FL_HEX)
  1410. flags_str = "hex";
  1411. else if (hist_field->flags & HIST_FIELD_FL_SYM)
  1412. flags_str = "sym";
  1413. else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
  1414. flags_str = "sym-offset";
  1415. else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
  1416. flags_str = "execname";
  1417. else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
  1418. flags_str = "syscall";
  1419. else if (hist_field->flags & HIST_FIELD_FL_LOG2)
  1420. flags_str = "log2";
  1421. else if (hist_field->flags & HIST_FIELD_FL_BUCKET)
  1422. flags_str = "buckets";
  1423. else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
  1424. flags_str = "usecs";
  1425. else if (hist_field->flags & HIST_FIELD_FL_PERCENT)
  1426. flags_str = "percent";
  1427. else if (hist_field->flags & HIST_FIELD_FL_GRAPH)
  1428. flags_str = "graph";
  1429. return flags_str;
  1430. }
  1431. static void expr_field_str(struct hist_field *field, char *expr)
  1432. {
  1433. if (field->flags & HIST_FIELD_FL_VAR_REF)
  1434. strcat(expr, "$");
  1435. else if (field->flags & HIST_FIELD_FL_CONST) {
  1436. char str[HIST_CONST_DIGITS_MAX];
  1437. snprintf(str, HIST_CONST_DIGITS_MAX, "%llu", field->constant);
  1438. strcat(expr, str);
  1439. }
  1440. strcat(expr, hist_field_name(field, 0));
  1441. if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
  1442. const char *flags_str = get_hist_field_flags(field);
  1443. if (flags_str) {
  1444. strcat(expr, ".");
  1445. strcat(expr, flags_str);
  1446. }
  1447. }
  1448. }
  1449. static char *expr_str(struct hist_field *field, unsigned int level)
  1450. {
  1451. char *expr;
  1452. if (level > 1)
  1453. return NULL;
  1454. expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
  1455. if (!expr)
  1456. return NULL;
  1457. if (!field->operands[0]) {
  1458. expr_field_str(field, expr);
  1459. return expr;
  1460. }
  1461. if (field->operator == FIELD_OP_UNARY_MINUS) {
  1462. char *subexpr;
  1463. strcat(expr, "-(");
  1464. subexpr = expr_str(field->operands[0], ++level);
  1465. if (!subexpr) {
  1466. kfree(expr);
  1467. return NULL;
  1468. }
  1469. strcat(expr, subexpr);
  1470. strcat(expr, ")");
  1471. kfree(subexpr);
  1472. return expr;
  1473. }
  1474. expr_field_str(field->operands[0], expr);
  1475. switch (field->operator) {
  1476. case FIELD_OP_MINUS:
  1477. strcat(expr, "-");
  1478. break;
  1479. case FIELD_OP_PLUS:
  1480. strcat(expr, "+");
  1481. break;
  1482. case FIELD_OP_DIV:
  1483. strcat(expr, "/");
  1484. break;
  1485. case FIELD_OP_MULT:
  1486. strcat(expr, "*");
  1487. break;
  1488. default:
  1489. kfree(expr);
  1490. return NULL;
  1491. }
  1492. expr_field_str(field->operands[1], expr);
  1493. return expr;
  1494. }
  1495. /*
  1496. * If field_op != FIELD_OP_NONE, *sep points to the root operator
  1497. * of the expression tree to be evaluated.
  1498. */
  1499. static int contains_operator(char *str, char **sep)
  1500. {
  1501. enum field_op_id field_op = FIELD_OP_NONE;
  1502. char *minus_op, *plus_op, *div_op, *mult_op;
  1503. /*
  1504. * Report the last occurrence of the operators first, so that the
  1505. * expression is evaluated left to right. This is important since
  1506. * subtraction and division are not associative.
  1507. *
  1508. * e.g
  1509. * 64/8/4/2 is 1, i.e 64/8/4/2 = ((64/8)/4)/2
  1510. * 14-7-5-2 is 0, i.e 14-7-5-2 = ((14-7)-5)-2
  1511. */
  1512. /*
  1513. * First, find lower precedence addition and subtraction
  1514. * since the expression will be evaluated recursively.
  1515. */
  1516. minus_op = strrchr(str, '-');
  1517. if (minus_op) {
  1518. /*
  1519. * Unary minus is not supported in sub-expressions. If
  1520. * present, it is always the next root operator.
  1521. */
  1522. if (minus_op == str) {
  1523. field_op = FIELD_OP_UNARY_MINUS;
  1524. goto out;
  1525. }
  1526. field_op = FIELD_OP_MINUS;
  1527. }
  1528. plus_op = strrchr(str, '+');
  1529. if (plus_op || minus_op) {
  1530. /*
  1531. * For operators of the same precedence use to rightmost as the
  1532. * root, so that the expression is evaluated left to right.
  1533. */
  1534. if (plus_op > minus_op)
  1535. field_op = FIELD_OP_PLUS;
  1536. goto out;
  1537. }
  1538. /*
  1539. * Multiplication and division have higher precedence than addition and
  1540. * subtraction.
  1541. */
  1542. div_op = strrchr(str, '/');
  1543. if (div_op)
  1544. field_op = FIELD_OP_DIV;
  1545. mult_op = strrchr(str, '*');
  1546. /*
  1547. * For operators of the same precedence use to rightmost as the
  1548. * root, so that the expression is evaluated left to right.
  1549. */
  1550. if (mult_op > div_op)
  1551. field_op = FIELD_OP_MULT;
  1552. out:
  1553. if (sep) {
  1554. switch (field_op) {
  1555. case FIELD_OP_UNARY_MINUS:
  1556. case FIELD_OP_MINUS:
  1557. *sep = minus_op;
  1558. break;
  1559. case FIELD_OP_PLUS:
  1560. *sep = plus_op;
  1561. break;
  1562. case FIELD_OP_DIV:
  1563. *sep = div_op;
  1564. break;
  1565. case FIELD_OP_MULT:
  1566. *sep = mult_op;
  1567. break;
  1568. case FIELD_OP_NONE:
  1569. default:
  1570. *sep = NULL;
  1571. break;
  1572. }
  1573. }
  1574. return field_op;
  1575. }
  1576. static void get_hist_field(struct hist_field *hist_field)
  1577. {
  1578. hist_field->ref++;
  1579. }
  1580. static void __destroy_hist_field(struct hist_field *hist_field)
  1581. {
  1582. if (--hist_field->ref > 1)
  1583. return;
  1584. kfree(hist_field->var.name);
  1585. kfree(hist_field->name);
  1586. /* Can likely be a const */
  1587. kfree_const(hist_field->type);
  1588. kfree(hist_field->system);
  1589. kfree(hist_field->event_name);
  1590. kfree(hist_field);
  1591. }
  1592. static void destroy_hist_field(struct hist_field *hist_field,
  1593. unsigned int level)
  1594. {
  1595. unsigned int i;
  1596. if (level > 3)
  1597. return;
  1598. if (!hist_field)
  1599. return;
  1600. if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
  1601. return; /* var refs will be destroyed separately */
  1602. for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
  1603. destroy_hist_field(hist_field->operands[i], level + 1);
  1604. __destroy_hist_field(hist_field);
  1605. }
  1606. static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
  1607. struct ftrace_event_field *field,
  1608. unsigned long flags,
  1609. char *var_name)
  1610. {
  1611. struct hist_field *hist_field;
  1612. if (field && is_function_field(field))
  1613. return NULL;
  1614. hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
  1615. if (!hist_field)
  1616. return NULL;
  1617. hist_field->ref = 1;
  1618. hist_field->hist_data = hist_data;
  1619. if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
  1620. goto out; /* caller will populate */
  1621. if (flags & HIST_FIELD_FL_VAR_REF) {
  1622. hist_field->fn_num = HIST_FIELD_FN_VAR_REF;
  1623. goto out;
  1624. }
  1625. if (flags & HIST_FIELD_FL_HITCOUNT) {
  1626. hist_field->fn_num = HIST_FIELD_FN_COUNTER;
  1627. hist_field->size = sizeof(u64);
  1628. hist_field->type = "u64";
  1629. goto out;
  1630. }
  1631. if (flags & HIST_FIELD_FL_CONST) {
  1632. hist_field->fn_num = HIST_FIELD_FN_CONST;
  1633. hist_field->size = sizeof(u64);
  1634. hist_field->type = kstrdup("u64", GFP_KERNEL);
  1635. if (!hist_field->type)
  1636. goto free;
  1637. goto out;
  1638. }
  1639. if (flags & HIST_FIELD_FL_STACKTRACE) {
  1640. hist_field->fn_num = HIST_FIELD_FN_NOP;
  1641. goto out;
  1642. }
  1643. if (flags & (HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET)) {
  1644. unsigned long fl = flags & ~(HIST_FIELD_FL_LOG2 | HIST_FIELD_FL_BUCKET);
  1645. hist_field->fn_num = flags & HIST_FIELD_FL_LOG2 ? HIST_FIELD_FN_LOG2 :
  1646. HIST_FIELD_FN_BUCKET;
  1647. hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
  1648. if (!hist_field->operands[0])
  1649. goto free;
  1650. hist_field->size = hist_field->operands[0]->size;
  1651. hist_field->type = kstrdup_const(hist_field->operands[0]->type, GFP_KERNEL);
  1652. if (!hist_field->type)
  1653. goto free;
  1654. goto out;
  1655. }
  1656. if (flags & HIST_FIELD_FL_TIMESTAMP) {
  1657. hist_field->fn_num = HIST_FIELD_FN_TIMESTAMP;
  1658. hist_field->size = sizeof(u64);
  1659. hist_field->type = "u64";
  1660. goto out;
  1661. }
  1662. if (flags & HIST_FIELD_FL_CPU) {
  1663. hist_field->fn_num = HIST_FIELD_FN_CPU;
  1664. hist_field->size = sizeof(int);
  1665. hist_field->type = "unsigned int";
  1666. goto out;
  1667. }
  1668. if (WARN_ON_ONCE(!field))
  1669. goto out;
  1670. /* Pointers to strings are just pointers and dangerous to dereference */
  1671. if (is_string_field(field) &&
  1672. (field->filter_type != FILTER_PTR_STRING)) {
  1673. flags |= HIST_FIELD_FL_STRING;
  1674. hist_field->size = MAX_FILTER_STR_VAL;
  1675. hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
  1676. if (!hist_field->type)
  1677. goto free;
  1678. if (field->filter_type == FILTER_STATIC_STRING) {
  1679. hist_field->fn_num = HIST_FIELD_FN_STRING;
  1680. hist_field->size = field->size;
  1681. } else if (field->filter_type == FILTER_DYN_STRING) {
  1682. hist_field->fn_num = HIST_FIELD_FN_DYNSTRING;
  1683. } else if (field->filter_type == FILTER_RDYN_STRING)
  1684. hist_field->fn_num = HIST_FIELD_FN_RELDYNSTRING;
  1685. else
  1686. hist_field->fn_num = HIST_FIELD_FN_PSTRING;
  1687. } else {
  1688. hist_field->size = field->size;
  1689. hist_field->is_signed = field->is_signed;
  1690. hist_field->type = kstrdup_const(field->type, GFP_KERNEL);
  1691. if (!hist_field->type)
  1692. goto free;
  1693. hist_field->fn_num = select_value_fn(field->size,
  1694. field->is_signed);
  1695. if (hist_field->fn_num == HIST_FIELD_FN_NOP) {
  1696. destroy_hist_field(hist_field, 0);
  1697. return NULL;
  1698. }
  1699. }
  1700. out:
  1701. hist_field->field = field;
  1702. hist_field->flags = flags;
  1703. if (var_name) {
  1704. hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
  1705. if (!hist_field->var.name)
  1706. goto free;
  1707. }
  1708. return hist_field;
  1709. free:
  1710. destroy_hist_field(hist_field, 0);
  1711. return NULL;
  1712. }
  1713. static void destroy_hist_fields(struct hist_trigger_data *hist_data)
  1714. {
  1715. unsigned int i;
  1716. for (i = 0; i < HIST_FIELDS_MAX; i++) {
  1717. if (hist_data->fields[i]) {
  1718. destroy_hist_field(hist_data->fields[i], 0);
  1719. hist_data->fields[i] = NULL;
  1720. }
  1721. }
  1722. for (i = 0; i < hist_data->n_var_refs; i++) {
  1723. WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
  1724. __destroy_hist_field(hist_data->var_refs[i]);
  1725. hist_data->var_refs[i] = NULL;
  1726. }
  1727. }
  1728. static int init_var_ref(struct hist_field *ref_field,
  1729. struct hist_field *var_field,
  1730. char *system, char *event_name)
  1731. {
  1732. int err = 0;
  1733. ref_field->var.idx = var_field->var.idx;
  1734. ref_field->var.hist_data = var_field->hist_data;
  1735. ref_field->size = var_field->size;
  1736. ref_field->is_signed = var_field->is_signed;
  1737. ref_field->flags |= var_field->flags &
  1738. (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
  1739. if (system) {
  1740. ref_field->system = kstrdup(system, GFP_KERNEL);
  1741. if (!ref_field->system)
  1742. return -ENOMEM;
  1743. }
  1744. if (event_name) {
  1745. ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
  1746. if (!ref_field->event_name) {
  1747. err = -ENOMEM;
  1748. goto free;
  1749. }
  1750. }
  1751. if (var_field->var.name) {
  1752. ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
  1753. if (!ref_field->name) {
  1754. err = -ENOMEM;
  1755. goto free;
  1756. }
  1757. } else if (var_field->name) {
  1758. ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
  1759. if (!ref_field->name) {
  1760. err = -ENOMEM;
  1761. goto free;
  1762. }
  1763. }
  1764. ref_field->type = kstrdup_const(var_field->type, GFP_KERNEL);
  1765. if (!ref_field->type) {
  1766. err = -ENOMEM;
  1767. goto free;
  1768. }
  1769. out:
  1770. return err;
  1771. free:
  1772. kfree(ref_field->system);
  1773. ref_field->system = NULL;
  1774. kfree(ref_field->event_name);
  1775. ref_field->event_name = NULL;
  1776. kfree(ref_field->name);
  1777. ref_field->name = NULL;
  1778. goto out;
  1779. }
  1780. static int find_var_ref_idx(struct hist_trigger_data *hist_data,
  1781. struct hist_field *var_field)
  1782. {
  1783. struct hist_field *ref_field;
  1784. int i;
  1785. for (i = 0; i < hist_data->n_var_refs; i++) {
  1786. ref_field = hist_data->var_refs[i];
  1787. if (ref_field->var.idx == var_field->var.idx &&
  1788. ref_field->var.hist_data == var_field->hist_data)
  1789. return i;
  1790. }
  1791. return -ENOENT;
  1792. }
  1793. /**
  1794. * create_var_ref - Create a variable reference and attach it to trigger
  1795. * @hist_data: The trigger that will be referencing the variable
  1796. * @var_field: The VAR field to create a reference to
  1797. * @system: The optional system string
  1798. * @event_name: The optional event_name string
  1799. *
  1800. * Given a variable hist_field, create a VAR_REF hist_field that
  1801. * represents a reference to it.
  1802. *
  1803. * This function also adds the reference to the trigger that
  1804. * now references the variable.
  1805. *
  1806. * Return: The VAR_REF field if successful, NULL if not
  1807. */
  1808. static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
  1809. struct hist_field *var_field,
  1810. char *system, char *event_name)
  1811. {
  1812. unsigned long flags = HIST_FIELD_FL_VAR_REF;
  1813. struct hist_field *ref_field;
  1814. int i;
  1815. /* Check if the variable already exists */
  1816. for (i = 0; i < hist_data->n_var_refs; i++) {
  1817. ref_field = hist_data->var_refs[i];
  1818. if (ref_field->var.idx == var_field->var.idx &&
  1819. ref_field->var.hist_data == var_field->hist_data) {
  1820. get_hist_field(ref_field);
  1821. return ref_field;
  1822. }
  1823. }
  1824. /* Sanity check to avoid out-of-bound write on 'hist_data->var_refs' */
  1825. if (hist_data->n_var_refs >= TRACING_MAP_VARS_MAX)
  1826. return NULL;
  1827. ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
  1828. if (ref_field) {
  1829. if (init_var_ref(ref_field, var_field, system, event_name)) {
  1830. destroy_hist_field(ref_field, 0);
  1831. return NULL;
  1832. }
  1833. hist_data->var_refs[hist_data->n_var_refs] = ref_field;
  1834. ref_field->var_ref_idx = hist_data->n_var_refs++;
  1835. }
  1836. return ref_field;
  1837. }
  1838. static bool is_var_ref(char *var_name)
  1839. {
  1840. if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
  1841. return false;
  1842. return true;
  1843. }
  1844. static char *field_name_from_var(struct hist_trigger_data *hist_data,
  1845. char *var_name)
  1846. {
  1847. char *name, *field;
  1848. unsigned int i;
  1849. for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
  1850. name = hist_data->attrs->var_defs.name[i];
  1851. if (strcmp(var_name, name) == 0) {
  1852. field = hist_data->attrs->var_defs.expr[i];
  1853. if (contains_operator(field, NULL) || is_var_ref(field))
  1854. continue;
  1855. return field;
  1856. }
  1857. }
  1858. return NULL;
  1859. }
  1860. static char *local_field_var_ref(struct hist_trigger_data *hist_data,
  1861. char *system, char *event_name,
  1862. char *var_name)
  1863. {
  1864. struct trace_event_call *call;
  1865. if (system && event_name) {
  1866. call = hist_data->event_file->event_call;
  1867. if (strcmp(system, call->class->system) != 0)
  1868. return NULL;
  1869. if (strcmp(event_name, trace_event_name(call)) != 0)
  1870. return NULL;
  1871. }
  1872. if (!!system != !!event_name)
  1873. return NULL;
  1874. if (!is_var_ref(var_name))
  1875. return NULL;
  1876. var_name++;
  1877. return field_name_from_var(hist_data, var_name);
  1878. }
  1879. static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
  1880. char *system, char *event_name,
  1881. char *var_name)
  1882. {
  1883. struct hist_field *var_field = NULL, *ref_field = NULL;
  1884. struct trace_array *tr = hist_data->event_file->tr;
  1885. if (!is_var_ref(var_name))
  1886. return NULL;
  1887. var_name++;
  1888. var_field = find_event_var(hist_data, system, event_name, var_name);
  1889. if (var_field)
  1890. ref_field = create_var_ref(hist_data, var_field,
  1891. system, event_name);
  1892. if (!ref_field)
  1893. hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
  1894. return ref_field;
  1895. }
  1896. static struct ftrace_event_field *
  1897. parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
  1898. char *field_str, unsigned long *flags, unsigned long *buckets)
  1899. {
  1900. struct ftrace_event_field *field = NULL;
  1901. char *field_name, *modifier, *str;
  1902. struct trace_array *tr = file->tr;
  1903. modifier = str = kstrdup(field_str, GFP_KERNEL);
  1904. if (!modifier)
  1905. return ERR_PTR(-ENOMEM);
  1906. field_name = strsep(&modifier, ".");
  1907. if (modifier) {
  1908. if (strcmp(modifier, "hex") == 0)
  1909. *flags |= HIST_FIELD_FL_HEX;
  1910. else if (strcmp(modifier, "sym") == 0)
  1911. *flags |= HIST_FIELD_FL_SYM;
  1912. /*
  1913. * 'sym-offset' occurrences in the trigger string are modified
  1914. * to 'symXoffset' to simplify arithmetic expression parsing.
  1915. */
  1916. else if (strcmp(modifier, "symXoffset") == 0)
  1917. *flags |= HIST_FIELD_FL_SYM_OFFSET;
  1918. else if ((strcmp(modifier, "execname") == 0) &&
  1919. (strcmp(field_name, "common_pid") == 0))
  1920. *flags |= HIST_FIELD_FL_EXECNAME;
  1921. else if (strcmp(modifier, "syscall") == 0)
  1922. *flags |= HIST_FIELD_FL_SYSCALL;
  1923. else if (strcmp(modifier, "log2") == 0)
  1924. *flags |= HIST_FIELD_FL_LOG2;
  1925. else if (strcmp(modifier, "usecs") == 0)
  1926. *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
  1927. else if (strncmp(modifier, "bucket", 6) == 0) {
  1928. int ret;
  1929. modifier += 6;
  1930. if (*modifier == 's')
  1931. modifier++;
  1932. if (*modifier != '=')
  1933. goto error;
  1934. modifier++;
  1935. ret = kstrtoul(modifier, 0, buckets);
  1936. if (ret || !(*buckets))
  1937. goto error;
  1938. *flags |= HIST_FIELD_FL_BUCKET;
  1939. } else if (strncmp(modifier, "percent", 7) == 0) {
  1940. if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
  1941. goto error;
  1942. *flags |= HIST_FIELD_FL_PERCENT;
  1943. } else if (strncmp(modifier, "graph", 5) == 0) {
  1944. if (*flags & (HIST_FIELD_FL_VAR | HIST_FIELD_FL_KEY))
  1945. goto error;
  1946. *flags |= HIST_FIELD_FL_GRAPH;
  1947. } else {
  1948. error:
  1949. hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
  1950. field = ERR_PTR(-EINVAL);
  1951. goto out;
  1952. }
  1953. }
  1954. if (strcmp(field_name, "common_timestamp") == 0) {
  1955. *flags |= HIST_FIELD_FL_TIMESTAMP;
  1956. hist_data->enable_timestamps = true;
  1957. if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
  1958. hist_data->attrs->ts_in_usecs = true;
  1959. } else if (strcmp(field_name, "common_cpu") == 0)
  1960. *flags |= HIST_FIELD_FL_CPU;
  1961. else {
  1962. field = trace_find_event_field(file->event_call, field_name);
  1963. if (!field || !field->size) {
  1964. /*
  1965. * For backward compatibility, if field_name
  1966. * was "cpu", then we treat this the same as
  1967. * common_cpu. This also works for "CPU".
  1968. */
  1969. if (field && field->filter_type == FILTER_CPU) {
  1970. *flags |= HIST_FIELD_FL_CPU;
  1971. } else {
  1972. hist_err(tr, HIST_ERR_FIELD_NOT_FOUND,
  1973. errpos(field_name));
  1974. field = ERR_PTR(-EINVAL);
  1975. goto out;
  1976. }
  1977. }
  1978. }
  1979. out:
  1980. kfree(str);
  1981. return field;
  1982. }
  1983. static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
  1984. struct hist_field *var_ref,
  1985. char *var_name)
  1986. {
  1987. struct hist_field *alias = NULL;
  1988. unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
  1989. alias = create_hist_field(hist_data, NULL, flags, var_name);
  1990. if (!alias)
  1991. return NULL;
  1992. alias->fn_num = var_ref->fn_num;
  1993. alias->operands[0] = var_ref;
  1994. if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
  1995. destroy_hist_field(alias, 0);
  1996. return NULL;
  1997. }
  1998. alias->var_ref_idx = var_ref->var_ref_idx;
  1999. return alias;
  2000. }
  2001. static struct hist_field *parse_const(struct hist_trigger_data *hist_data,
  2002. char *str, char *var_name,
  2003. unsigned long *flags)
  2004. {
  2005. struct trace_array *tr = hist_data->event_file->tr;
  2006. struct hist_field *field = NULL;
  2007. u64 constant;
  2008. if (kstrtoull(str, 0, &constant)) {
  2009. hist_err(tr, HIST_ERR_EXPECT_NUMBER, errpos(str));
  2010. return NULL;
  2011. }
  2012. *flags |= HIST_FIELD_FL_CONST;
  2013. field = create_hist_field(hist_data, NULL, *flags, var_name);
  2014. if (!field)
  2015. return NULL;
  2016. field->constant = constant;
  2017. return field;
  2018. }
  2019. static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
  2020. struct trace_event_file *file, char *str,
  2021. unsigned long *flags, char *var_name)
  2022. {
  2023. char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
  2024. struct ftrace_event_field *field = NULL;
  2025. struct hist_field *hist_field = NULL;
  2026. unsigned long buckets = 0;
  2027. int ret = 0;
  2028. if (isdigit(str[0])) {
  2029. hist_field = parse_const(hist_data, str, var_name, flags);
  2030. if (!hist_field) {
  2031. ret = -EINVAL;
  2032. goto out;
  2033. }
  2034. return hist_field;
  2035. }
  2036. s = strchr(str, '.');
  2037. if (s) {
  2038. s = strchr(++s, '.');
  2039. if (s) {
  2040. ref_system = strsep(&str, ".");
  2041. if (!str) {
  2042. ret = -EINVAL;
  2043. goto out;
  2044. }
  2045. ref_event = strsep(&str, ".");
  2046. if (!str) {
  2047. ret = -EINVAL;
  2048. goto out;
  2049. }
  2050. ref_var = str;
  2051. }
  2052. }
  2053. s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
  2054. if (!s) {
  2055. hist_field = parse_var_ref(hist_data, ref_system,
  2056. ref_event, ref_var);
  2057. if (hist_field) {
  2058. if (var_name) {
  2059. hist_field = create_alias(hist_data, hist_field, var_name);
  2060. if (!hist_field) {
  2061. ret = -ENOMEM;
  2062. goto out;
  2063. }
  2064. }
  2065. return hist_field;
  2066. }
  2067. } else
  2068. str = s;
  2069. field = parse_field(hist_data, file, str, flags, &buckets);
  2070. if (IS_ERR(field)) {
  2071. ret = PTR_ERR(field);
  2072. goto out;
  2073. }
  2074. hist_field = create_hist_field(hist_data, field, *flags, var_name);
  2075. if (!hist_field) {
  2076. ret = -ENOMEM;
  2077. goto out;
  2078. }
  2079. hist_field->buckets = buckets;
  2080. return hist_field;
  2081. out:
  2082. return ERR_PTR(ret);
  2083. }
  2084. static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
  2085. struct trace_event_file *file,
  2086. char *str, unsigned long flags,
  2087. char *var_name, unsigned int *n_subexprs);
  2088. static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
  2089. struct trace_event_file *file,
  2090. char *str, unsigned long flags,
  2091. char *var_name, unsigned int *n_subexprs)
  2092. {
  2093. struct hist_field *operand1, *expr = NULL;
  2094. unsigned long operand_flags;
  2095. int ret = 0;
  2096. char *s;
  2097. /* Unary minus operator, increment n_subexprs */
  2098. ++*n_subexprs;
  2099. /* we support only -(xxx) i.e. explicit parens required */
  2100. if (*n_subexprs > 3) {
  2101. hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
  2102. ret = -EINVAL;
  2103. goto free;
  2104. }
  2105. str++; /* skip leading '-' */
  2106. s = strchr(str, '(');
  2107. if (s)
  2108. str++;
  2109. else {
  2110. ret = -EINVAL;
  2111. goto free;
  2112. }
  2113. s = strrchr(str, ')');
  2114. if (s) {
  2115. /* unary minus not supported in sub-expressions */
  2116. if (*(s+1) != '\0') {
  2117. hist_err(file->tr, HIST_ERR_UNARY_MINUS_SUBEXPR,
  2118. errpos(str));
  2119. ret = -EINVAL;
  2120. goto free;
  2121. }
  2122. *s = '\0';
  2123. }
  2124. else {
  2125. ret = -EINVAL; /* no closing ')' */
  2126. goto free;
  2127. }
  2128. flags |= HIST_FIELD_FL_EXPR;
  2129. expr = create_hist_field(hist_data, NULL, flags, var_name);
  2130. if (!expr) {
  2131. ret = -ENOMEM;
  2132. goto free;
  2133. }
  2134. operand_flags = 0;
  2135. operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
  2136. if (IS_ERR(operand1)) {
  2137. ret = PTR_ERR(operand1);
  2138. goto free;
  2139. }
  2140. if (operand1->flags & HIST_FIELD_FL_STRING) {
  2141. /* String type can not be the operand of unary operator. */
  2142. hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
  2143. destroy_hist_field(operand1, 0);
  2144. ret = -EINVAL;
  2145. goto free;
  2146. }
  2147. expr->flags |= operand1->flags &
  2148. (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
  2149. expr->fn_num = HIST_FIELD_FN_UMINUS;
  2150. expr->operands[0] = operand1;
  2151. expr->size = operand1->size;
  2152. expr->is_signed = operand1->is_signed;
  2153. expr->operator = FIELD_OP_UNARY_MINUS;
  2154. expr->name = expr_str(expr, 0);
  2155. expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
  2156. if (!expr->type) {
  2157. ret = -ENOMEM;
  2158. goto free;
  2159. }
  2160. return expr;
  2161. free:
  2162. destroy_hist_field(expr, 0);
  2163. return ERR_PTR(ret);
  2164. }
  2165. /*
  2166. * If the operands are var refs, return pointers the
  2167. * variable(s) referenced in var1 and var2, else NULL.
  2168. */
  2169. static int check_expr_operands(struct trace_array *tr,
  2170. struct hist_field *operand1,
  2171. struct hist_field *operand2,
  2172. struct hist_field **var1,
  2173. struct hist_field **var2)
  2174. {
  2175. unsigned long operand1_flags = operand1->flags;
  2176. unsigned long operand2_flags = operand2->flags;
  2177. if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
  2178. (operand1_flags & HIST_FIELD_FL_ALIAS)) {
  2179. struct hist_field *var;
  2180. var = find_var_field(operand1->var.hist_data, operand1->name);
  2181. if (!var)
  2182. return -EINVAL;
  2183. operand1_flags = var->flags;
  2184. *var1 = var;
  2185. }
  2186. if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
  2187. (operand2_flags & HIST_FIELD_FL_ALIAS)) {
  2188. struct hist_field *var;
  2189. var = find_var_field(operand2->var.hist_data, operand2->name);
  2190. if (!var)
  2191. return -EINVAL;
  2192. operand2_flags = var->flags;
  2193. *var2 = var;
  2194. }
  2195. if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
  2196. (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
  2197. hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
  2198. return -EINVAL;
  2199. }
  2200. return 0;
  2201. }
  2202. static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
  2203. struct trace_event_file *file,
  2204. char *str, unsigned long flags,
  2205. char *var_name, unsigned int *n_subexprs)
  2206. {
  2207. struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
  2208. struct hist_field *var1 = NULL, *var2 = NULL;
  2209. unsigned long operand_flags, operand2_flags;
  2210. int field_op, ret = -EINVAL;
  2211. char *sep, *operand1_str;
  2212. enum hist_field_fn op_fn;
  2213. bool combine_consts;
  2214. if (*n_subexprs > 3) {
  2215. hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
  2216. return ERR_PTR(-EINVAL);
  2217. }
  2218. field_op = contains_operator(str, &sep);
  2219. if (field_op == FIELD_OP_NONE)
  2220. return parse_atom(hist_data, file, str, &flags, var_name);
  2221. if (field_op == FIELD_OP_UNARY_MINUS)
  2222. return parse_unary(hist_data, file, str, flags, var_name, n_subexprs);
  2223. /* Binary operator found, increment n_subexprs */
  2224. ++*n_subexprs;
  2225. /* Split the expression string at the root operator */
  2226. if (!sep)
  2227. return ERR_PTR(-EINVAL);
  2228. *sep = '\0';
  2229. operand1_str = str;
  2230. str = sep+1;
  2231. /* Binary operator requires both operands */
  2232. if (*operand1_str == '\0' || *str == '\0')
  2233. return ERR_PTR(-EINVAL);
  2234. operand_flags = 0;
  2235. /* LHS of string is an expression e.g. a+b in a+b+c */
  2236. operand1 = parse_expr(hist_data, file, operand1_str, operand_flags, NULL, n_subexprs);
  2237. if (IS_ERR(operand1))
  2238. return ERR_CAST(operand1);
  2239. if (operand1->flags & HIST_FIELD_FL_STRING) {
  2240. hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str));
  2241. ret = -EINVAL;
  2242. goto free_op1;
  2243. }
  2244. /* RHS of string is another expression e.g. c in a+b+c */
  2245. operand_flags = 0;
  2246. operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, n_subexprs);
  2247. if (IS_ERR(operand2)) {
  2248. ret = PTR_ERR(operand2);
  2249. goto free_op1;
  2250. }
  2251. if (operand2->flags & HIST_FIELD_FL_STRING) {
  2252. hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str));
  2253. ret = -EINVAL;
  2254. goto free_operands;
  2255. }
  2256. switch (field_op) {
  2257. case FIELD_OP_MINUS:
  2258. op_fn = HIST_FIELD_FN_MINUS;
  2259. break;
  2260. case FIELD_OP_PLUS:
  2261. op_fn = HIST_FIELD_FN_PLUS;
  2262. break;
  2263. case FIELD_OP_DIV:
  2264. op_fn = HIST_FIELD_FN_DIV;
  2265. break;
  2266. case FIELD_OP_MULT:
  2267. op_fn = HIST_FIELD_FN_MULT;
  2268. break;
  2269. default:
  2270. ret = -EINVAL;
  2271. goto free_operands;
  2272. }
  2273. ret = check_expr_operands(file->tr, operand1, operand2, &var1, &var2);
  2274. if (ret)
  2275. goto free_operands;
  2276. operand_flags = var1 ? var1->flags : operand1->flags;
  2277. operand2_flags = var2 ? var2->flags : operand2->flags;
  2278. /*
  2279. * If both operands are constant, the expression can be
  2280. * collapsed to a single constant.
  2281. */
  2282. combine_consts = operand_flags & operand2_flags & HIST_FIELD_FL_CONST;
  2283. flags |= combine_consts ? HIST_FIELD_FL_CONST : HIST_FIELD_FL_EXPR;
  2284. flags |= operand1->flags &
  2285. (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
  2286. expr = create_hist_field(hist_data, NULL, flags, var_name);
  2287. if (!expr) {
  2288. ret = -ENOMEM;
  2289. goto free_operands;
  2290. }
  2291. operand1->read_once = true;
  2292. operand2->read_once = true;
  2293. /* The operands are now owned and free'd by 'expr' */
  2294. expr->operands[0] = operand1;
  2295. expr->operands[1] = operand2;
  2296. if (field_op == FIELD_OP_DIV &&
  2297. operand2_flags & HIST_FIELD_FL_CONST) {
  2298. u64 divisor = var2 ? var2->constant : operand2->constant;
  2299. if (!divisor) {
  2300. hist_err(file->tr, HIST_ERR_DIVISION_BY_ZERO, errpos(str));
  2301. ret = -EDOM;
  2302. goto free_expr;
  2303. }
  2304. /*
  2305. * Copy the divisor here so we don't have to look it up
  2306. * later if this is a var ref
  2307. */
  2308. operand2->constant = divisor;
  2309. op_fn = hist_field_get_div_fn(operand2);
  2310. }
  2311. expr->fn_num = op_fn;
  2312. if (combine_consts) {
  2313. if (var1)
  2314. expr->operands[0] = var1;
  2315. if (var2)
  2316. expr->operands[1] = var2;
  2317. expr->constant = hist_fn_call(expr, NULL, NULL, NULL, NULL);
  2318. expr->fn_num = HIST_FIELD_FN_CONST;
  2319. expr->operands[0] = NULL;
  2320. expr->operands[1] = NULL;
  2321. /*
  2322. * var refs won't be destroyed immediately
  2323. * See: destroy_hist_field()
  2324. */
  2325. destroy_hist_field(operand2, 0);
  2326. destroy_hist_field(operand1, 0);
  2327. expr->name = expr_str(expr, 0);
  2328. } else {
  2329. /* The operand sizes should be the same, so just pick one */
  2330. expr->size = operand1->size;
  2331. expr->is_signed = operand1->is_signed;
  2332. expr->operator = field_op;
  2333. expr->type = kstrdup_const(operand1->type, GFP_KERNEL);
  2334. if (!expr->type) {
  2335. ret = -ENOMEM;
  2336. goto free_expr;
  2337. }
  2338. expr->name = expr_str(expr, 0);
  2339. }
  2340. return expr;
  2341. free_operands:
  2342. destroy_hist_field(operand2, 0);
  2343. free_op1:
  2344. destroy_hist_field(operand1, 0);
  2345. return ERR_PTR(ret);
  2346. free_expr:
  2347. destroy_hist_field(expr, 0);
  2348. return ERR_PTR(ret);
  2349. }
  2350. static char *find_trigger_filter(struct hist_trigger_data *hist_data,
  2351. struct trace_event_file *file)
  2352. {
  2353. struct event_trigger_data *test;
  2354. lockdep_assert_held(&event_mutex);
  2355. list_for_each_entry(test, &file->triggers, list) {
  2356. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  2357. if (test->private_data == hist_data)
  2358. return test->filter_str;
  2359. }
  2360. }
  2361. return NULL;
  2362. }
  2363. static struct event_command trigger_hist_cmd;
  2364. static int event_hist_trigger_parse(struct event_command *cmd_ops,
  2365. struct trace_event_file *file,
  2366. char *glob, char *cmd,
  2367. char *param_and_filter);
  2368. static bool compatible_keys(struct hist_trigger_data *target_hist_data,
  2369. struct hist_trigger_data *hist_data,
  2370. unsigned int n_keys)
  2371. {
  2372. struct hist_field *target_hist_field, *hist_field;
  2373. unsigned int n, i, j;
  2374. if (hist_data->n_fields - hist_data->n_vals != n_keys)
  2375. return false;
  2376. i = hist_data->n_vals;
  2377. j = target_hist_data->n_vals;
  2378. for (n = 0; n < n_keys; n++) {
  2379. hist_field = hist_data->fields[i + n];
  2380. target_hist_field = target_hist_data->fields[j + n];
  2381. if (strcmp(hist_field->type, target_hist_field->type) != 0)
  2382. return false;
  2383. if (hist_field->size != target_hist_field->size)
  2384. return false;
  2385. if (hist_field->is_signed != target_hist_field->is_signed)
  2386. return false;
  2387. }
  2388. return true;
  2389. }
  2390. static struct hist_trigger_data *
  2391. find_compatible_hist(struct hist_trigger_data *target_hist_data,
  2392. struct trace_event_file *file)
  2393. {
  2394. struct hist_trigger_data *hist_data;
  2395. struct event_trigger_data *test;
  2396. unsigned int n_keys;
  2397. lockdep_assert_held(&event_mutex);
  2398. n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
  2399. list_for_each_entry(test, &file->triggers, list) {
  2400. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  2401. hist_data = test->private_data;
  2402. if (compatible_keys(target_hist_data, hist_data, n_keys))
  2403. return hist_data;
  2404. }
  2405. }
  2406. return NULL;
  2407. }
  2408. static struct trace_event_file *event_file(struct trace_array *tr,
  2409. char *system, char *event_name)
  2410. {
  2411. struct trace_event_file *file;
  2412. file = __find_event_file(tr, system, event_name);
  2413. if (!file)
  2414. return ERR_PTR(-EINVAL);
  2415. return file;
  2416. }
  2417. static struct hist_field *
  2418. find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
  2419. char *system, char *event_name, char *field_name)
  2420. {
  2421. struct hist_field *event_var;
  2422. char *synthetic_name;
  2423. synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
  2424. if (!synthetic_name)
  2425. return ERR_PTR(-ENOMEM);
  2426. strcpy(synthetic_name, "synthetic_");
  2427. strcat(synthetic_name, field_name);
  2428. event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
  2429. kfree(synthetic_name);
  2430. return event_var;
  2431. }
  2432. /**
  2433. * create_field_var_hist - Automatically create a histogram and var for a field
  2434. * @target_hist_data: The target hist trigger
  2435. * @subsys_name: Optional subsystem name
  2436. * @event_name: Optional event name
  2437. * @field_name: The name of the field (and the resulting variable)
  2438. *
  2439. * Hist trigger actions fetch data from variables, not directly from
  2440. * events. However, for convenience, users are allowed to directly
  2441. * specify an event field in an action, which will be automatically
  2442. * converted into a variable on their behalf.
  2443. *
  2444. * If a user specifies a field on an event that isn't the event the
  2445. * histogram currently being defined (the target event histogram), the
  2446. * only way that can be accomplished is if a new hist trigger is
  2447. * created and the field variable defined on that.
  2448. *
  2449. * This function creates a new histogram compatible with the target
  2450. * event (meaning a histogram with the same key as the target
  2451. * histogram), and creates a variable for the specified field, but
  2452. * with 'synthetic_' prepended to the variable name in order to avoid
  2453. * collision with normal field variables.
  2454. *
  2455. * Return: The variable created for the field.
  2456. */
  2457. static struct hist_field *
  2458. create_field_var_hist(struct hist_trigger_data *target_hist_data,
  2459. char *subsys_name, char *event_name, char *field_name)
  2460. {
  2461. struct trace_array *tr = target_hist_data->event_file->tr;
  2462. struct hist_trigger_data *hist_data;
  2463. unsigned int i, n, first = true;
  2464. struct field_var_hist *var_hist;
  2465. struct trace_event_file *file;
  2466. struct hist_field *key_field;
  2467. struct hist_field *event_var;
  2468. char *saved_filter;
  2469. char *cmd;
  2470. int ret;
  2471. if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
  2472. hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
  2473. return ERR_PTR(-EINVAL);
  2474. }
  2475. file = event_file(tr, subsys_name, event_name);
  2476. if (IS_ERR(file)) {
  2477. hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
  2478. ret = PTR_ERR(file);
  2479. return ERR_PTR(ret);
  2480. }
  2481. /*
  2482. * Look for a histogram compatible with target. We'll use the
  2483. * found histogram specification to create a new matching
  2484. * histogram with our variable on it. target_hist_data is not
  2485. * yet a registered histogram so we can't use that.
  2486. */
  2487. hist_data = find_compatible_hist(target_hist_data, file);
  2488. if (!hist_data) {
  2489. hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
  2490. return ERR_PTR(-EINVAL);
  2491. }
  2492. /* See if a synthetic field variable has already been created */
  2493. event_var = find_synthetic_field_var(target_hist_data, subsys_name,
  2494. event_name, field_name);
  2495. if (!IS_ERR_OR_NULL(event_var))
  2496. return event_var;
  2497. var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
  2498. if (!var_hist)
  2499. return ERR_PTR(-ENOMEM);
  2500. cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
  2501. if (!cmd) {
  2502. kfree(var_hist);
  2503. return ERR_PTR(-ENOMEM);
  2504. }
  2505. /* Use the same keys as the compatible histogram */
  2506. strcat(cmd, "keys=");
  2507. for_each_hist_key_field(i, hist_data) {
  2508. key_field = hist_data->fields[i];
  2509. if (!first)
  2510. strcat(cmd, ",");
  2511. strcat(cmd, key_field->field->name);
  2512. first = false;
  2513. }
  2514. /* Create the synthetic field variable specification */
  2515. strcat(cmd, ":synthetic_");
  2516. strcat(cmd, field_name);
  2517. strcat(cmd, "=");
  2518. strcat(cmd, field_name);
  2519. /* Use the same filter as the compatible histogram */
  2520. saved_filter = find_trigger_filter(hist_data, file);
  2521. if (saved_filter) {
  2522. strcat(cmd, " if ");
  2523. strcat(cmd, saved_filter);
  2524. }
  2525. var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
  2526. if (!var_hist->cmd) {
  2527. kfree(cmd);
  2528. kfree(var_hist);
  2529. return ERR_PTR(-ENOMEM);
  2530. }
  2531. /* Save the compatible histogram information */
  2532. var_hist->hist_data = hist_data;
  2533. /* Create the new histogram with our variable */
  2534. ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
  2535. "", "hist", cmd);
  2536. if (ret) {
  2537. kfree(cmd);
  2538. kfree(var_hist->cmd);
  2539. kfree(var_hist);
  2540. hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
  2541. return ERR_PTR(ret);
  2542. }
  2543. kfree(cmd);
  2544. /* If we can't find the variable, something went wrong */
  2545. event_var = find_synthetic_field_var(target_hist_data, subsys_name,
  2546. event_name, field_name);
  2547. if (IS_ERR_OR_NULL(event_var)) {
  2548. kfree(var_hist->cmd);
  2549. kfree(var_hist);
  2550. hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
  2551. return ERR_PTR(-EINVAL);
  2552. }
  2553. n = target_hist_data->n_field_var_hists;
  2554. target_hist_data->field_var_hists[n] = var_hist;
  2555. target_hist_data->n_field_var_hists++;
  2556. return event_var;
  2557. }
  2558. static struct hist_field *
  2559. find_target_event_var(struct hist_trigger_data *hist_data,
  2560. char *subsys_name, char *event_name, char *var_name)
  2561. {
  2562. struct trace_event_file *file = hist_data->event_file;
  2563. struct hist_field *hist_field = NULL;
  2564. if (subsys_name) {
  2565. struct trace_event_call *call;
  2566. if (!event_name)
  2567. return NULL;
  2568. call = file->event_call;
  2569. if (strcmp(subsys_name, call->class->system) != 0)
  2570. return NULL;
  2571. if (strcmp(event_name, trace_event_name(call)) != 0)
  2572. return NULL;
  2573. }
  2574. hist_field = find_var_field(hist_data, var_name);
  2575. return hist_field;
  2576. }
  2577. static inline void __update_field_vars(struct tracing_map_elt *elt,
  2578. struct trace_buffer *buffer,
  2579. struct ring_buffer_event *rbe,
  2580. void *rec,
  2581. struct field_var **field_vars,
  2582. unsigned int n_field_vars,
  2583. unsigned int field_var_str_start)
  2584. {
  2585. struct hist_elt_data *elt_data = elt->private_data;
  2586. unsigned int i, j, var_idx;
  2587. u64 var_val;
  2588. for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
  2589. struct field_var *field_var = field_vars[i];
  2590. struct hist_field *var = field_var->var;
  2591. struct hist_field *val = field_var->val;
  2592. var_val = hist_fn_call(val, elt, buffer, rbe, rec);
  2593. var_idx = var->var.idx;
  2594. if (val->flags & HIST_FIELD_FL_STRING) {
  2595. char *str = elt_data->field_var_str[j++];
  2596. char *val_str = (char *)(uintptr_t)var_val;
  2597. unsigned int size;
  2598. size = min(val->size, STR_VAR_LEN_MAX);
  2599. strscpy(str, val_str, size);
  2600. var_val = (u64)(uintptr_t)str;
  2601. }
  2602. tracing_map_set_var(elt, var_idx, var_val);
  2603. }
  2604. }
  2605. static void update_field_vars(struct hist_trigger_data *hist_data,
  2606. struct tracing_map_elt *elt,
  2607. struct trace_buffer *buffer,
  2608. struct ring_buffer_event *rbe,
  2609. void *rec)
  2610. {
  2611. __update_field_vars(elt, buffer, rbe, rec, hist_data->field_vars,
  2612. hist_data->n_field_vars, 0);
  2613. }
  2614. static void save_track_data_vars(struct hist_trigger_data *hist_data,
  2615. struct tracing_map_elt *elt,
  2616. struct trace_buffer *buffer, void *rec,
  2617. struct ring_buffer_event *rbe, void *key,
  2618. struct action_data *data, u64 *var_ref_vals)
  2619. {
  2620. __update_field_vars(elt, buffer, rbe, rec, hist_data->save_vars,
  2621. hist_data->n_save_vars, hist_data->n_field_var_str);
  2622. }
  2623. static struct hist_field *create_var(struct hist_trigger_data *hist_data,
  2624. struct trace_event_file *file,
  2625. char *name, int size, const char *type)
  2626. {
  2627. struct hist_field *var;
  2628. int idx;
  2629. if (find_var(hist_data, file, name) && !hist_data->remove) {
  2630. var = ERR_PTR(-EINVAL);
  2631. goto out;
  2632. }
  2633. var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
  2634. if (!var) {
  2635. var = ERR_PTR(-ENOMEM);
  2636. goto out;
  2637. }
  2638. idx = tracing_map_add_var(hist_data->map);
  2639. if (idx < 0) {
  2640. kfree(var);
  2641. var = ERR_PTR(-EINVAL);
  2642. goto out;
  2643. }
  2644. var->ref = 1;
  2645. var->flags = HIST_FIELD_FL_VAR;
  2646. var->var.idx = idx;
  2647. var->var.hist_data = var->hist_data = hist_data;
  2648. var->size = size;
  2649. var->var.name = kstrdup(name, GFP_KERNEL);
  2650. var->type = kstrdup_const(type, GFP_KERNEL);
  2651. if (!var->var.name || !var->type) {
  2652. kfree_const(var->type);
  2653. kfree(var->var.name);
  2654. kfree(var);
  2655. var = ERR_PTR(-ENOMEM);
  2656. }
  2657. out:
  2658. return var;
  2659. }
  2660. static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
  2661. struct trace_event_file *file,
  2662. char *field_name)
  2663. {
  2664. struct hist_field *val = NULL, *var = NULL;
  2665. unsigned long flags = HIST_FIELD_FL_VAR;
  2666. struct trace_array *tr = file->tr;
  2667. struct field_var *field_var;
  2668. int ret = 0;
  2669. if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
  2670. hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
  2671. ret = -EINVAL;
  2672. goto err;
  2673. }
  2674. val = parse_atom(hist_data, file, field_name, &flags, NULL);
  2675. if (IS_ERR(val)) {
  2676. hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
  2677. ret = PTR_ERR(val);
  2678. goto err;
  2679. }
  2680. var = create_var(hist_data, file, field_name, val->size, val->type);
  2681. if (IS_ERR(var)) {
  2682. hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
  2683. kfree(val);
  2684. ret = PTR_ERR(var);
  2685. goto err;
  2686. }
  2687. field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
  2688. if (!field_var) {
  2689. kfree(val);
  2690. kfree(var);
  2691. ret = -ENOMEM;
  2692. goto err;
  2693. }
  2694. field_var->var = var;
  2695. field_var->val = val;
  2696. out:
  2697. return field_var;
  2698. err:
  2699. field_var = ERR_PTR(ret);
  2700. goto out;
  2701. }
  2702. /**
  2703. * create_target_field_var - Automatically create a variable for a field
  2704. * @target_hist_data: The target hist trigger
  2705. * @subsys_name: Optional subsystem name
  2706. * @event_name: Optional event name
  2707. * @var_name: The name of the field (and the resulting variable)
  2708. *
  2709. * Hist trigger actions fetch data from variables, not directly from
  2710. * events. However, for convenience, users are allowed to directly
  2711. * specify an event field in an action, which will be automatically
  2712. * converted into a variable on their behalf.
  2713. *
  2714. * This function creates a field variable with the name var_name on
  2715. * the hist trigger currently being defined on the target event. If
  2716. * subsys_name and event_name are specified, this function simply
  2717. * verifies that they do in fact match the target event subsystem and
  2718. * event name.
  2719. *
  2720. * Return: The variable created for the field.
  2721. */
  2722. static struct field_var *
  2723. create_target_field_var(struct hist_trigger_data *target_hist_data,
  2724. char *subsys_name, char *event_name, char *var_name)
  2725. {
  2726. struct trace_event_file *file = target_hist_data->event_file;
  2727. if (subsys_name) {
  2728. struct trace_event_call *call;
  2729. if (!event_name)
  2730. return NULL;
  2731. call = file->event_call;
  2732. if (strcmp(subsys_name, call->class->system) != 0)
  2733. return NULL;
  2734. if (strcmp(event_name, trace_event_name(call)) != 0)
  2735. return NULL;
  2736. }
  2737. return create_field_var(target_hist_data, file, var_name);
  2738. }
  2739. static bool check_track_val_max(u64 track_val, u64 var_val)
  2740. {
  2741. if (var_val <= track_val)
  2742. return false;
  2743. return true;
  2744. }
  2745. static bool check_track_val_changed(u64 track_val, u64 var_val)
  2746. {
  2747. if (var_val == track_val)
  2748. return false;
  2749. return true;
  2750. }
  2751. static u64 get_track_val(struct hist_trigger_data *hist_data,
  2752. struct tracing_map_elt *elt,
  2753. struct action_data *data)
  2754. {
  2755. unsigned int track_var_idx = data->track_data.track_var->var.idx;
  2756. u64 track_val;
  2757. track_val = tracing_map_read_var(elt, track_var_idx);
  2758. return track_val;
  2759. }
  2760. static void save_track_val(struct hist_trigger_data *hist_data,
  2761. struct tracing_map_elt *elt,
  2762. struct action_data *data, u64 var_val)
  2763. {
  2764. unsigned int track_var_idx = data->track_data.track_var->var.idx;
  2765. tracing_map_set_var(elt, track_var_idx, var_val);
  2766. }
  2767. static void save_track_data(struct hist_trigger_data *hist_data,
  2768. struct tracing_map_elt *elt,
  2769. struct trace_buffer *buffer, void *rec,
  2770. struct ring_buffer_event *rbe, void *key,
  2771. struct action_data *data, u64 *var_ref_vals)
  2772. {
  2773. if (data->track_data.save_data)
  2774. data->track_data.save_data(hist_data, elt, buffer, rec, rbe,
  2775. key, data, var_ref_vals);
  2776. }
  2777. static bool check_track_val(struct tracing_map_elt *elt,
  2778. struct action_data *data,
  2779. u64 var_val)
  2780. {
  2781. struct hist_trigger_data *hist_data;
  2782. u64 track_val;
  2783. hist_data = data->track_data.track_var->hist_data;
  2784. track_val = get_track_val(hist_data, elt, data);
  2785. return data->track_data.check_val(track_val, var_val);
  2786. }
  2787. #ifdef CONFIG_TRACER_SNAPSHOT
  2788. static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
  2789. {
  2790. /* called with tr->max_lock held */
  2791. struct track_data *track_data = tr->cond_snapshot->cond_data;
  2792. struct hist_elt_data *elt_data, *track_elt_data;
  2793. struct snapshot_context *context = cond_data;
  2794. struct action_data *action;
  2795. u64 track_val;
  2796. if (!track_data)
  2797. return false;
  2798. action = track_data->action_data;
  2799. track_val = get_track_val(track_data->hist_data, context->elt,
  2800. track_data->action_data);
  2801. if (!action->track_data.check_val(track_data->track_val, track_val))
  2802. return false;
  2803. track_data->track_val = track_val;
  2804. memcpy(track_data->key, context->key, track_data->key_len);
  2805. elt_data = context->elt->private_data;
  2806. track_elt_data = track_data->elt.private_data;
  2807. if (elt_data->comm)
  2808. strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
  2809. track_data->updated = true;
  2810. return true;
  2811. }
  2812. static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
  2813. struct tracing_map_elt *elt,
  2814. struct trace_buffer *buffer, void *rec,
  2815. struct ring_buffer_event *rbe, void *key,
  2816. struct action_data *data,
  2817. u64 *var_ref_vals)
  2818. {
  2819. struct trace_event_file *file = hist_data->event_file;
  2820. struct snapshot_context context;
  2821. context.elt = elt;
  2822. context.key = key;
  2823. tracing_snapshot_cond(file->tr, &context);
  2824. }
  2825. static void hist_trigger_print_key(struct seq_file *m,
  2826. struct hist_trigger_data *hist_data,
  2827. void *key,
  2828. struct tracing_map_elt *elt);
  2829. static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
  2830. {
  2831. unsigned int i;
  2832. if (!hist_data->n_actions)
  2833. return NULL;
  2834. for (i = 0; i < hist_data->n_actions; i++) {
  2835. struct action_data *data = hist_data->actions[i];
  2836. if (data->action == ACTION_SNAPSHOT)
  2837. return data;
  2838. }
  2839. return NULL;
  2840. }
  2841. static void track_data_snapshot_print(struct seq_file *m,
  2842. struct hist_trigger_data *hist_data)
  2843. {
  2844. struct trace_event_file *file = hist_data->event_file;
  2845. struct track_data *track_data;
  2846. struct action_data *action;
  2847. track_data = tracing_cond_snapshot_data(file->tr);
  2848. if (!track_data)
  2849. return;
  2850. if (!track_data->updated)
  2851. return;
  2852. action = snapshot_action(hist_data);
  2853. if (!action)
  2854. return;
  2855. seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n");
  2856. seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
  2857. action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
  2858. action->track_data.var_str, track_data->track_val);
  2859. seq_puts(m, "\ttriggered by event with key: ");
  2860. hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
  2861. seq_putc(m, '\n');
  2862. }
  2863. #else
  2864. static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
  2865. {
  2866. return false;
  2867. }
  2868. static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
  2869. struct tracing_map_elt *elt,
  2870. struct trace_buffer *buffer, void *rec,
  2871. struct ring_buffer_event *rbe, void *key,
  2872. struct action_data *data,
  2873. u64 *var_ref_vals) {}
  2874. static void track_data_snapshot_print(struct seq_file *m,
  2875. struct hist_trigger_data *hist_data) {}
  2876. #endif /* CONFIG_TRACER_SNAPSHOT */
  2877. static void track_data_print(struct seq_file *m,
  2878. struct hist_trigger_data *hist_data,
  2879. struct tracing_map_elt *elt,
  2880. struct action_data *data)
  2881. {
  2882. u64 track_val = get_track_val(hist_data, elt, data);
  2883. unsigned int i, save_var_idx;
  2884. if (data->handler == HANDLER_ONMAX)
  2885. seq_printf(m, "\n\tmax: %10llu", track_val);
  2886. else if (data->handler == HANDLER_ONCHANGE)
  2887. seq_printf(m, "\n\tchanged: %10llu", track_val);
  2888. if (data->action == ACTION_SNAPSHOT)
  2889. return;
  2890. for (i = 0; i < hist_data->n_save_vars; i++) {
  2891. struct hist_field *save_val = hist_data->save_vars[i]->val;
  2892. struct hist_field *save_var = hist_data->save_vars[i]->var;
  2893. u64 val;
  2894. save_var_idx = save_var->var.idx;
  2895. val = tracing_map_read_var(elt, save_var_idx);
  2896. if (save_val->flags & HIST_FIELD_FL_STRING) {
  2897. seq_printf(m, " %s: %-32s", save_var->var.name,
  2898. (char *)(uintptr_t)(val));
  2899. } else
  2900. seq_printf(m, " %s: %10llu", save_var->var.name, val);
  2901. }
  2902. }
  2903. static void ontrack_action(struct hist_trigger_data *hist_data,
  2904. struct tracing_map_elt *elt,
  2905. struct trace_buffer *buffer, void *rec,
  2906. struct ring_buffer_event *rbe, void *key,
  2907. struct action_data *data, u64 *var_ref_vals)
  2908. {
  2909. u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
  2910. if (check_track_val(elt, data, var_val)) {
  2911. save_track_val(hist_data, elt, data, var_val);
  2912. save_track_data(hist_data, elt, buffer, rec, rbe,
  2913. key, data, var_ref_vals);
  2914. }
  2915. }
  2916. static void action_data_destroy(struct action_data *data)
  2917. {
  2918. unsigned int i;
  2919. lockdep_assert_held(&event_mutex);
  2920. kfree(data->action_name);
  2921. for (i = 0; i < data->n_params; i++)
  2922. kfree(data->params[i]);
  2923. if (data->synth_event)
  2924. data->synth_event->ref--;
  2925. kfree(data->synth_event_name);
  2926. kfree(data);
  2927. }
  2928. static void track_data_destroy(struct hist_trigger_data *hist_data,
  2929. struct action_data *data)
  2930. {
  2931. struct trace_event_file *file = hist_data->event_file;
  2932. destroy_hist_field(data->track_data.track_var, 0);
  2933. if (data->action == ACTION_SNAPSHOT) {
  2934. struct track_data *track_data;
  2935. track_data = tracing_cond_snapshot_data(file->tr);
  2936. if (track_data && track_data->hist_data == hist_data) {
  2937. tracing_snapshot_cond_disable(file->tr);
  2938. track_data_free(track_data);
  2939. }
  2940. }
  2941. kfree(data->track_data.var_str);
  2942. action_data_destroy(data);
  2943. }
  2944. static int action_create(struct hist_trigger_data *hist_data,
  2945. struct action_data *data);
  2946. static int track_data_create(struct hist_trigger_data *hist_data,
  2947. struct action_data *data)
  2948. {
  2949. struct hist_field *var_field, *ref_field, *track_var = NULL;
  2950. struct trace_event_file *file = hist_data->event_file;
  2951. struct trace_array *tr = file->tr;
  2952. char *track_data_var_str;
  2953. int ret = 0;
  2954. track_data_var_str = data->track_data.var_str;
  2955. if (track_data_var_str[0] != '$') {
  2956. hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
  2957. return -EINVAL;
  2958. }
  2959. track_data_var_str++;
  2960. var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
  2961. if (!var_field) {
  2962. hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
  2963. return -EINVAL;
  2964. }
  2965. ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
  2966. if (!ref_field)
  2967. return -ENOMEM;
  2968. data->track_data.var_ref = ref_field;
  2969. if (data->handler == HANDLER_ONMAX)
  2970. track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
  2971. if (IS_ERR(track_var)) {
  2972. hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
  2973. ret = PTR_ERR(track_var);
  2974. goto out;
  2975. }
  2976. if (data->handler == HANDLER_ONCHANGE)
  2977. track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
  2978. if (IS_ERR(track_var)) {
  2979. hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
  2980. ret = PTR_ERR(track_var);
  2981. goto out;
  2982. }
  2983. data->track_data.track_var = track_var;
  2984. ret = action_create(hist_data, data);
  2985. out:
  2986. return ret;
  2987. }
  2988. static int parse_action_params(struct trace_array *tr, char *params,
  2989. struct action_data *data)
  2990. {
  2991. char *param, *saved_param;
  2992. bool first_param = true;
  2993. int ret = 0;
  2994. while (params) {
  2995. if (data->n_params >= SYNTH_FIELDS_MAX) {
  2996. hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
  2997. ret = -EINVAL;
  2998. goto out;
  2999. }
  3000. param = strsep(&params, ",");
  3001. if (!param) {
  3002. hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
  3003. ret = -EINVAL;
  3004. goto out;
  3005. }
  3006. param = strstrip(param);
  3007. if (strlen(param) < 2) {
  3008. hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
  3009. ret = -EINVAL;
  3010. goto out;
  3011. }
  3012. saved_param = kstrdup(param, GFP_KERNEL);
  3013. if (!saved_param) {
  3014. ret = -ENOMEM;
  3015. goto out;
  3016. }
  3017. if (first_param && data->use_trace_keyword) {
  3018. data->synth_event_name = saved_param;
  3019. first_param = false;
  3020. continue;
  3021. }
  3022. first_param = false;
  3023. data->params[data->n_params++] = saved_param;
  3024. }
  3025. out:
  3026. return ret;
  3027. }
  3028. static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
  3029. enum handler_id handler)
  3030. {
  3031. char *action_name;
  3032. int ret = 0;
  3033. strsep(&str, ".");
  3034. if (!str) {
  3035. hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
  3036. ret = -EINVAL;
  3037. goto out;
  3038. }
  3039. action_name = strsep(&str, "(");
  3040. if (!action_name || !str) {
  3041. hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
  3042. ret = -EINVAL;
  3043. goto out;
  3044. }
  3045. if (str_has_prefix(action_name, "save")) {
  3046. char *params = strsep(&str, ")");
  3047. if (!params) {
  3048. hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
  3049. ret = -EINVAL;
  3050. goto out;
  3051. }
  3052. ret = parse_action_params(tr, params, data);
  3053. if (ret)
  3054. goto out;
  3055. if (handler == HANDLER_ONMAX)
  3056. data->track_data.check_val = check_track_val_max;
  3057. else if (handler == HANDLER_ONCHANGE)
  3058. data->track_data.check_val = check_track_val_changed;
  3059. else {
  3060. hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
  3061. ret = -EINVAL;
  3062. goto out;
  3063. }
  3064. data->track_data.save_data = save_track_data_vars;
  3065. data->fn = ontrack_action;
  3066. data->action = ACTION_SAVE;
  3067. } else if (str_has_prefix(action_name, "snapshot")) {
  3068. char *params = strsep(&str, ")");
  3069. if (!str) {
  3070. hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
  3071. ret = -EINVAL;
  3072. goto out;
  3073. }
  3074. if (handler == HANDLER_ONMAX)
  3075. data->track_data.check_val = check_track_val_max;
  3076. else if (handler == HANDLER_ONCHANGE)
  3077. data->track_data.check_val = check_track_val_changed;
  3078. else {
  3079. hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
  3080. ret = -EINVAL;
  3081. goto out;
  3082. }
  3083. data->track_data.save_data = save_track_data_snapshot;
  3084. data->fn = ontrack_action;
  3085. data->action = ACTION_SNAPSHOT;
  3086. } else {
  3087. char *params = strsep(&str, ")");
  3088. if (str_has_prefix(action_name, "trace"))
  3089. data->use_trace_keyword = true;
  3090. if (params) {
  3091. ret = parse_action_params(tr, params, data);
  3092. if (ret)
  3093. goto out;
  3094. }
  3095. if (handler == HANDLER_ONMAX)
  3096. data->track_data.check_val = check_track_val_max;
  3097. else if (handler == HANDLER_ONCHANGE)
  3098. data->track_data.check_val = check_track_val_changed;
  3099. if (handler != HANDLER_ONMATCH) {
  3100. data->track_data.save_data = action_trace;
  3101. data->fn = ontrack_action;
  3102. } else
  3103. data->fn = action_trace;
  3104. data->action = ACTION_TRACE;
  3105. }
  3106. data->action_name = kstrdup(action_name, GFP_KERNEL);
  3107. if (!data->action_name) {
  3108. ret = -ENOMEM;
  3109. goto out;
  3110. }
  3111. data->handler = handler;
  3112. out:
  3113. return ret;
  3114. }
  3115. static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
  3116. char *str, enum handler_id handler)
  3117. {
  3118. struct action_data *data;
  3119. int ret = -EINVAL;
  3120. char *var_str;
  3121. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3122. if (!data)
  3123. return ERR_PTR(-ENOMEM);
  3124. var_str = strsep(&str, ")");
  3125. if (!var_str || !str) {
  3126. ret = -EINVAL;
  3127. goto free;
  3128. }
  3129. data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
  3130. if (!data->track_data.var_str) {
  3131. ret = -ENOMEM;
  3132. goto free;
  3133. }
  3134. ret = action_parse(hist_data->event_file->tr, str, data, handler);
  3135. if (ret)
  3136. goto free;
  3137. out:
  3138. return data;
  3139. free:
  3140. track_data_destroy(hist_data, data);
  3141. data = ERR_PTR(ret);
  3142. goto out;
  3143. }
  3144. static void onmatch_destroy(struct action_data *data)
  3145. {
  3146. kfree(data->match_data.event);
  3147. kfree(data->match_data.event_system);
  3148. action_data_destroy(data);
  3149. }
  3150. static void destroy_field_var(struct field_var *field_var)
  3151. {
  3152. if (!field_var)
  3153. return;
  3154. destroy_hist_field(field_var->var, 0);
  3155. destroy_hist_field(field_var->val, 0);
  3156. kfree(field_var);
  3157. }
  3158. static void destroy_field_vars(struct hist_trigger_data *hist_data)
  3159. {
  3160. unsigned int i;
  3161. for (i = 0; i < hist_data->n_field_vars; i++)
  3162. destroy_field_var(hist_data->field_vars[i]);
  3163. for (i = 0; i < hist_data->n_save_vars; i++)
  3164. destroy_field_var(hist_data->save_vars[i]);
  3165. }
  3166. static void save_field_var(struct hist_trigger_data *hist_data,
  3167. struct field_var *field_var)
  3168. {
  3169. hist_data->field_vars[hist_data->n_field_vars++] = field_var;
  3170. if (field_var->val->flags & HIST_FIELD_FL_STRING)
  3171. hist_data->n_field_var_str++;
  3172. }
  3173. static int check_synth_field(struct synth_event *event,
  3174. struct hist_field *hist_field,
  3175. unsigned int field_pos)
  3176. {
  3177. struct synth_field *field;
  3178. if (field_pos >= event->n_fields)
  3179. return -EINVAL;
  3180. field = event->fields[field_pos];
  3181. /*
  3182. * A dynamic string synth field can accept static or
  3183. * dynamic. A static string synth field can only accept a
  3184. * same-sized static string, which is checked for later.
  3185. */
  3186. if (strstr(hist_field->type, "char[") && field->is_string
  3187. && field->is_dynamic)
  3188. return 0;
  3189. if (strstr(hist_field->type, "long[") && field->is_stack)
  3190. return 0;
  3191. if (strcmp(field->type, hist_field->type) != 0) {
  3192. if (field->size != hist_field->size ||
  3193. (!field->is_string && field->is_signed != hist_field->is_signed))
  3194. return -EINVAL;
  3195. }
  3196. return 0;
  3197. }
  3198. static struct hist_field *
  3199. trace_action_find_var(struct hist_trigger_data *hist_data,
  3200. struct action_data *data,
  3201. char *system, char *event, char *var)
  3202. {
  3203. struct trace_array *tr = hist_data->event_file->tr;
  3204. struct hist_field *hist_field;
  3205. var++; /* skip '$' */
  3206. hist_field = find_target_event_var(hist_data, system, event, var);
  3207. if (!hist_field) {
  3208. if (!system && data->handler == HANDLER_ONMATCH) {
  3209. system = data->match_data.event_system;
  3210. event = data->match_data.event;
  3211. }
  3212. hist_field = find_event_var(hist_data, system, event, var);
  3213. }
  3214. if (!hist_field)
  3215. hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
  3216. return hist_field;
  3217. }
  3218. static struct hist_field *
  3219. trace_action_create_field_var(struct hist_trigger_data *hist_data,
  3220. struct action_data *data, char *system,
  3221. char *event, char *var)
  3222. {
  3223. struct hist_field *hist_field = NULL;
  3224. struct field_var *field_var;
  3225. /*
  3226. * First try to create a field var on the target event (the
  3227. * currently being defined). This will create a variable for
  3228. * unqualified fields on the target event, or if qualified,
  3229. * target fields that have qualified names matching the target.
  3230. */
  3231. field_var = create_target_field_var(hist_data, system, event, var);
  3232. if (field_var && !IS_ERR(field_var)) {
  3233. save_field_var(hist_data, field_var);
  3234. hist_field = field_var->var;
  3235. } else {
  3236. field_var = NULL;
  3237. /*
  3238. * If no explicit system.event is specified, default to
  3239. * looking for fields on the onmatch(system.event.xxx)
  3240. * event.
  3241. */
  3242. if (!system && data->handler == HANDLER_ONMATCH) {
  3243. system = data->match_data.event_system;
  3244. event = data->match_data.event;
  3245. }
  3246. if (!event)
  3247. goto free;
  3248. /*
  3249. * At this point, we're looking at a field on another
  3250. * event. Because we can't modify a hist trigger on
  3251. * another event to add a variable for a field, we need
  3252. * to create a new trigger on that event and create the
  3253. * variable at the same time.
  3254. */
  3255. hist_field = create_field_var_hist(hist_data, system, event, var);
  3256. if (IS_ERR(hist_field))
  3257. goto free;
  3258. }
  3259. out:
  3260. return hist_field;
  3261. free:
  3262. destroy_field_var(field_var);
  3263. hist_field = NULL;
  3264. goto out;
  3265. }
  3266. static int trace_action_create(struct hist_trigger_data *hist_data,
  3267. struct action_data *data)
  3268. {
  3269. struct trace_array *tr = hist_data->event_file->tr;
  3270. char *event_name, *param, *system = NULL;
  3271. struct hist_field *hist_field, *var_ref;
  3272. unsigned int i;
  3273. unsigned int field_pos = 0;
  3274. struct synth_event *event;
  3275. char *synth_event_name;
  3276. int var_ref_idx, ret = 0;
  3277. lockdep_assert_held(&event_mutex);
  3278. /* Sanity check to avoid out-of-bound write on 'data->var_ref_idx' */
  3279. if (data->n_params > SYNTH_FIELDS_MAX)
  3280. return -EINVAL;
  3281. if (data->use_trace_keyword)
  3282. synth_event_name = data->synth_event_name;
  3283. else
  3284. synth_event_name = data->action_name;
  3285. event = find_synth_event(synth_event_name);
  3286. if (!event) {
  3287. hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
  3288. return -EINVAL;
  3289. }
  3290. event->ref++;
  3291. for (i = 0; i < data->n_params; i++) {
  3292. char *p;
  3293. p = param = kstrdup(data->params[i], GFP_KERNEL);
  3294. if (!param) {
  3295. ret = -ENOMEM;
  3296. goto err;
  3297. }
  3298. system = strsep(&param, ".");
  3299. if (!param) {
  3300. param = (char *)system;
  3301. system = event_name = NULL;
  3302. } else {
  3303. event_name = strsep(&param, ".");
  3304. if (!param) {
  3305. kfree(p);
  3306. ret = -EINVAL;
  3307. goto err;
  3308. }
  3309. }
  3310. if (param[0] == '$')
  3311. hist_field = trace_action_find_var(hist_data, data,
  3312. system, event_name,
  3313. param);
  3314. else
  3315. hist_field = trace_action_create_field_var(hist_data,
  3316. data,
  3317. system,
  3318. event_name,
  3319. param);
  3320. if (!hist_field) {
  3321. kfree(p);
  3322. ret = -EINVAL;
  3323. goto err;
  3324. }
  3325. if (check_synth_field(event, hist_field, field_pos) == 0) {
  3326. var_ref = create_var_ref(hist_data, hist_field,
  3327. system, event_name);
  3328. if (!var_ref) {
  3329. kfree(p);
  3330. ret = -ENOMEM;
  3331. goto err;
  3332. }
  3333. var_ref_idx = find_var_ref_idx(hist_data, var_ref);
  3334. if (WARN_ON(var_ref_idx < 0)) {
  3335. kfree(p);
  3336. ret = var_ref_idx;
  3337. goto err;
  3338. }
  3339. data->var_ref_idx[i] = var_ref_idx;
  3340. field_pos++;
  3341. kfree(p);
  3342. continue;
  3343. }
  3344. hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
  3345. kfree(p);
  3346. ret = -EINVAL;
  3347. goto err;
  3348. }
  3349. if (field_pos != event->n_fields) {
  3350. hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
  3351. ret = -EINVAL;
  3352. goto err;
  3353. }
  3354. data->synth_event = event;
  3355. out:
  3356. return ret;
  3357. err:
  3358. event->ref--;
  3359. goto out;
  3360. }
  3361. static int action_create(struct hist_trigger_data *hist_data,
  3362. struct action_data *data)
  3363. {
  3364. struct trace_event_file *file = hist_data->event_file;
  3365. struct trace_array *tr = file->tr;
  3366. struct track_data *track_data;
  3367. struct field_var *field_var;
  3368. unsigned int i;
  3369. char *param;
  3370. int ret = 0;
  3371. if (data->action == ACTION_TRACE)
  3372. return trace_action_create(hist_data, data);
  3373. if (data->action == ACTION_SNAPSHOT) {
  3374. track_data = track_data_alloc(hist_data->key_size, data, hist_data);
  3375. if (IS_ERR(track_data)) {
  3376. ret = PTR_ERR(track_data);
  3377. goto out;
  3378. }
  3379. ret = tracing_snapshot_cond_enable(file->tr, track_data,
  3380. cond_snapshot_update);
  3381. if (ret)
  3382. track_data_free(track_data);
  3383. goto out;
  3384. }
  3385. if (data->action == ACTION_SAVE) {
  3386. if (hist_data->n_save_vars) {
  3387. ret = -EEXIST;
  3388. hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
  3389. goto out;
  3390. }
  3391. for (i = 0; i < data->n_params; i++) {
  3392. param = kstrdup(data->params[i], GFP_KERNEL);
  3393. if (!param) {
  3394. ret = -ENOMEM;
  3395. goto out;
  3396. }
  3397. field_var = create_target_field_var(hist_data, NULL, NULL, param);
  3398. if (IS_ERR(field_var)) {
  3399. hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
  3400. errpos(param));
  3401. ret = PTR_ERR(field_var);
  3402. kfree(param);
  3403. goto out;
  3404. }
  3405. hist_data->save_vars[hist_data->n_save_vars++] = field_var;
  3406. if (field_var->val->flags & HIST_FIELD_FL_STRING)
  3407. hist_data->n_save_var_str++;
  3408. kfree(param);
  3409. }
  3410. }
  3411. out:
  3412. return ret;
  3413. }
  3414. static int onmatch_create(struct hist_trigger_data *hist_data,
  3415. struct action_data *data)
  3416. {
  3417. return action_create(hist_data, data);
  3418. }
  3419. static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
  3420. {
  3421. char *match_event, *match_event_system;
  3422. struct action_data *data;
  3423. int ret = -EINVAL;
  3424. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3425. if (!data)
  3426. return ERR_PTR(-ENOMEM);
  3427. match_event = strsep(&str, ")");
  3428. if (!match_event || !str) {
  3429. hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
  3430. goto free;
  3431. }
  3432. match_event_system = strsep(&match_event, ".");
  3433. if (!match_event) {
  3434. hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
  3435. goto free;
  3436. }
  3437. if (IS_ERR(event_file(tr, match_event_system, match_event))) {
  3438. hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
  3439. goto free;
  3440. }
  3441. data->match_data.event = kstrdup(match_event, GFP_KERNEL);
  3442. if (!data->match_data.event) {
  3443. ret = -ENOMEM;
  3444. goto free;
  3445. }
  3446. data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
  3447. if (!data->match_data.event_system) {
  3448. ret = -ENOMEM;
  3449. goto free;
  3450. }
  3451. ret = action_parse(tr, str, data, HANDLER_ONMATCH);
  3452. if (ret)
  3453. goto free;
  3454. out:
  3455. return data;
  3456. free:
  3457. onmatch_destroy(data);
  3458. data = ERR_PTR(ret);
  3459. goto out;
  3460. }
  3461. static int create_hitcount_val(struct hist_trigger_data *hist_data)
  3462. {
  3463. hist_data->fields[HITCOUNT_IDX] =
  3464. create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
  3465. if (!hist_data->fields[HITCOUNT_IDX])
  3466. return -ENOMEM;
  3467. hist_data->n_vals++;
  3468. hist_data->n_fields++;
  3469. if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
  3470. return -EINVAL;
  3471. return 0;
  3472. }
  3473. static int __create_val_field(struct hist_trigger_data *hist_data,
  3474. unsigned int val_idx,
  3475. struct trace_event_file *file,
  3476. char *var_name, char *field_str,
  3477. unsigned long flags)
  3478. {
  3479. struct hist_field *hist_field;
  3480. int ret = 0, n_subexprs = 0;
  3481. hist_field = parse_expr(hist_data, file, field_str, flags, var_name, &n_subexprs);
  3482. if (IS_ERR(hist_field)) {
  3483. ret = PTR_ERR(hist_field);
  3484. goto out;
  3485. }
  3486. /* values and variables should not have some modifiers */
  3487. if (hist_field->flags & HIST_FIELD_FL_VAR) {
  3488. /* Variable */
  3489. if (hist_field->flags & (HIST_FIELD_FL_GRAPH | HIST_FIELD_FL_PERCENT |
  3490. HIST_FIELD_FL_BUCKET | HIST_FIELD_FL_LOG2))
  3491. goto err;
  3492. } else {
  3493. /* Value */
  3494. if (hist_field->flags & (HIST_FIELD_FL_GRAPH | HIST_FIELD_FL_PERCENT |
  3495. HIST_FIELD_FL_BUCKET | HIST_FIELD_FL_LOG2 |
  3496. HIST_FIELD_FL_SYM | HIST_FIELD_FL_SYM_OFFSET |
  3497. HIST_FIELD_FL_SYSCALL | HIST_FIELD_FL_STACKTRACE))
  3498. goto err;
  3499. }
  3500. hist_data->fields[val_idx] = hist_field;
  3501. ++hist_data->n_vals;
  3502. ++hist_data->n_fields;
  3503. if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
  3504. ret = -EINVAL;
  3505. out:
  3506. return ret;
  3507. err:
  3508. hist_err(file->tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(field_str));
  3509. return -EINVAL;
  3510. }
  3511. static int create_val_field(struct hist_trigger_data *hist_data,
  3512. unsigned int val_idx,
  3513. struct trace_event_file *file,
  3514. char *field_str)
  3515. {
  3516. if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
  3517. return -EINVAL;
  3518. return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
  3519. }
  3520. static const char no_comm[] = "(no comm)";
  3521. static u64 hist_field_execname(struct hist_field *hist_field,
  3522. struct tracing_map_elt *elt,
  3523. struct trace_buffer *buffer,
  3524. struct ring_buffer_event *rbe,
  3525. void *event)
  3526. {
  3527. struct hist_elt_data *elt_data;
  3528. if (WARN_ON_ONCE(!elt))
  3529. return (u64)(unsigned long)no_comm;
  3530. elt_data = elt->private_data;
  3531. if (WARN_ON_ONCE(!elt_data->comm))
  3532. return (u64)(unsigned long)no_comm;
  3533. return (u64)(unsigned long)(elt_data->comm);
  3534. }
  3535. static u64 hist_fn_call(struct hist_field *hist_field,
  3536. struct tracing_map_elt *elt,
  3537. struct trace_buffer *buffer,
  3538. struct ring_buffer_event *rbe,
  3539. void *event)
  3540. {
  3541. switch (hist_field->fn_num) {
  3542. case HIST_FIELD_FN_VAR_REF:
  3543. return hist_field_var_ref(hist_field, elt, buffer, rbe, event);
  3544. case HIST_FIELD_FN_COUNTER:
  3545. return hist_field_counter(hist_field, elt, buffer, rbe, event);
  3546. case HIST_FIELD_FN_CONST:
  3547. return hist_field_const(hist_field, elt, buffer, rbe, event);
  3548. case HIST_FIELD_FN_LOG2:
  3549. return hist_field_log2(hist_field, elt, buffer, rbe, event);
  3550. case HIST_FIELD_FN_BUCKET:
  3551. return hist_field_bucket(hist_field, elt, buffer, rbe, event);
  3552. case HIST_FIELD_FN_TIMESTAMP:
  3553. return hist_field_timestamp(hist_field, elt, buffer, rbe, event);
  3554. case HIST_FIELD_FN_CPU:
  3555. return hist_field_cpu(hist_field, elt, buffer, rbe, event);
  3556. case HIST_FIELD_FN_STRING:
  3557. return hist_field_string(hist_field, elt, buffer, rbe, event);
  3558. case HIST_FIELD_FN_DYNSTRING:
  3559. return hist_field_dynstring(hist_field, elt, buffer, rbe, event);
  3560. case HIST_FIELD_FN_RELDYNSTRING:
  3561. return hist_field_reldynstring(hist_field, elt, buffer, rbe, event);
  3562. case HIST_FIELD_FN_PSTRING:
  3563. return hist_field_pstring(hist_field, elt, buffer, rbe, event);
  3564. case HIST_FIELD_FN_S64:
  3565. return hist_field_s64(hist_field, elt, buffer, rbe, event);
  3566. case HIST_FIELD_FN_U64:
  3567. return hist_field_u64(hist_field, elt, buffer, rbe, event);
  3568. case HIST_FIELD_FN_S32:
  3569. return hist_field_s32(hist_field, elt, buffer, rbe, event);
  3570. case HIST_FIELD_FN_U32:
  3571. return hist_field_u32(hist_field, elt, buffer, rbe, event);
  3572. case HIST_FIELD_FN_S16:
  3573. return hist_field_s16(hist_field, elt, buffer, rbe, event);
  3574. case HIST_FIELD_FN_U16:
  3575. return hist_field_u16(hist_field, elt, buffer, rbe, event);
  3576. case HIST_FIELD_FN_S8:
  3577. return hist_field_s8(hist_field, elt, buffer, rbe, event);
  3578. case HIST_FIELD_FN_U8:
  3579. return hist_field_u8(hist_field, elt, buffer, rbe, event);
  3580. case HIST_FIELD_FN_UMINUS:
  3581. return hist_field_unary_minus(hist_field, elt, buffer, rbe, event);
  3582. case HIST_FIELD_FN_MINUS:
  3583. return hist_field_minus(hist_field, elt, buffer, rbe, event);
  3584. case HIST_FIELD_FN_PLUS:
  3585. return hist_field_plus(hist_field, elt, buffer, rbe, event);
  3586. case HIST_FIELD_FN_DIV:
  3587. return hist_field_div(hist_field, elt, buffer, rbe, event);
  3588. case HIST_FIELD_FN_MULT:
  3589. return hist_field_mult(hist_field, elt, buffer, rbe, event);
  3590. case HIST_FIELD_FN_DIV_POWER2:
  3591. return div_by_power_of_two(hist_field, elt, buffer, rbe, event);
  3592. case HIST_FIELD_FN_DIV_NOT_POWER2:
  3593. return div_by_not_power_of_two(hist_field, elt, buffer, rbe, event);
  3594. case HIST_FIELD_FN_DIV_MULT_SHIFT:
  3595. return div_by_mult_and_shift(hist_field, elt, buffer, rbe, event);
  3596. case HIST_FIELD_FN_EXECNAME:
  3597. return hist_field_execname(hist_field, elt, buffer, rbe, event);
  3598. default:
  3599. return 0;
  3600. }
  3601. }
  3602. /* Convert a var that points to common_pid.execname to a string */
  3603. static void update_var_execname(struct hist_field *hist_field)
  3604. {
  3605. hist_field->flags = HIST_FIELD_FL_STRING | HIST_FIELD_FL_VAR |
  3606. HIST_FIELD_FL_EXECNAME;
  3607. hist_field->size = MAX_FILTER_STR_VAL;
  3608. hist_field->is_signed = 0;
  3609. kfree_const(hist_field->type);
  3610. hist_field->type = "char[]";
  3611. hist_field->fn_num = HIST_FIELD_FN_EXECNAME;
  3612. }
  3613. static int create_var_field(struct hist_trigger_data *hist_data,
  3614. unsigned int val_idx,
  3615. struct trace_event_file *file,
  3616. char *var_name, char *expr_str)
  3617. {
  3618. struct trace_array *tr = hist_data->event_file->tr;
  3619. unsigned long flags = 0;
  3620. int ret;
  3621. if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
  3622. return -EINVAL;
  3623. if (find_var(hist_data, file, var_name) && !hist_data->remove) {
  3624. hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
  3625. return -EINVAL;
  3626. }
  3627. flags |= HIST_FIELD_FL_VAR;
  3628. hist_data->n_vars++;
  3629. if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
  3630. return -EINVAL;
  3631. ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
  3632. if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_EXECNAME)
  3633. update_var_execname(hist_data->fields[val_idx]);
  3634. if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING)
  3635. hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
  3636. return ret;
  3637. }
  3638. static int create_val_fields(struct hist_trigger_data *hist_data,
  3639. struct trace_event_file *file)
  3640. {
  3641. char *fields_str, *field_str;
  3642. unsigned int i, j = 1;
  3643. int ret;
  3644. ret = create_hitcount_val(hist_data);
  3645. if (ret)
  3646. goto out;
  3647. fields_str = hist_data->attrs->vals_str;
  3648. if (!fields_str)
  3649. goto out;
  3650. for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
  3651. j < TRACING_MAP_VALS_MAX; i++) {
  3652. field_str = strsep(&fields_str, ",");
  3653. if (!field_str)
  3654. break;
  3655. if (strcmp(field_str, "hitcount") == 0)
  3656. continue;
  3657. ret = create_val_field(hist_data, j++, file, field_str);
  3658. if (ret)
  3659. goto out;
  3660. }
  3661. if (fields_str && (strcmp(fields_str, "hitcount") != 0))
  3662. ret = -EINVAL;
  3663. out:
  3664. return ret;
  3665. }
  3666. static int create_key_field(struct hist_trigger_data *hist_data,
  3667. unsigned int key_idx,
  3668. unsigned int key_offset,
  3669. struct trace_event_file *file,
  3670. char *field_str)
  3671. {
  3672. struct trace_array *tr = hist_data->event_file->tr;
  3673. struct hist_field *hist_field = NULL;
  3674. unsigned long flags = 0;
  3675. unsigned int key_size;
  3676. int ret = 0, n_subexprs = 0;
  3677. if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
  3678. return -EINVAL;
  3679. flags |= HIST_FIELD_FL_KEY;
  3680. if (strcmp(field_str, "stacktrace") == 0) {
  3681. flags |= HIST_FIELD_FL_STACKTRACE;
  3682. key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
  3683. hist_field = create_hist_field(hist_data, NULL, flags, NULL);
  3684. } else {
  3685. hist_field = parse_expr(hist_data, file, field_str, flags,
  3686. NULL, &n_subexprs);
  3687. if (IS_ERR(hist_field)) {
  3688. ret = PTR_ERR(hist_field);
  3689. goto out;
  3690. }
  3691. if (field_has_hist_vars(hist_field, 0)) {
  3692. hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
  3693. destroy_hist_field(hist_field, 0);
  3694. ret = -EINVAL;
  3695. goto out;
  3696. }
  3697. key_size = hist_field->size;
  3698. }
  3699. hist_data->fields[key_idx] = hist_field;
  3700. key_size = ALIGN(key_size, sizeof(u64));
  3701. hist_data->fields[key_idx]->size = key_size;
  3702. hist_data->fields[key_idx]->offset = key_offset;
  3703. hist_data->key_size += key_size;
  3704. if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
  3705. ret = -EINVAL;
  3706. goto out;
  3707. }
  3708. hist_data->n_keys++;
  3709. hist_data->n_fields++;
  3710. if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
  3711. return -EINVAL;
  3712. ret = key_size;
  3713. out:
  3714. return ret;
  3715. }
  3716. static int create_key_fields(struct hist_trigger_data *hist_data,
  3717. struct trace_event_file *file)
  3718. {
  3719. unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
  3720. char *fields_str, *field_str;
  3721. int ret = -EINVAL;
  3722. fields_str = hist_data->attrs->keys_str;
  3723. if (!fields_str)
  3724. goto out;
  3725. for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
  3726. field_str = strsep(&fields_str, ",");
  3727. if (!field_str)
  3728. break;
  3729. ret = create_key_field(hist_data, i, key_offset,
  3730. file, field_str);
  3731. if (ret < 0)
  3732. goto out;
  3733. key_offset += ret;
  3734. }
  3735. if (fields_str) {
  3736. ret = -EINVAL;
  3737. goto out;
  3738. }
  3739. ret = 0;
  3740. out:
  3741. return ret;
  3742. }
  3743. static int create_var_fields(struct hist_trigger_data *hist_data,
  3744. struct trace_event_file *file)
  3745. {
  3746. unsigned int i, j = hist_data->n_vals;
  3747. int ret = 0;
  3748. unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
  3749. for (i = 0; i < n_vars; i++) {
  3750. char *var_name = hist_data->attrs->var_defs.name[i];
  3751. char *expr = hist_data->attrs->var_defs.expr[i];
  3752. ret = create_var_field(hist_data, j++, file, var_name, expr);
  3753. if (ret)
  3754. goto out;
  3755. }
  3756. out:
  3757. return ret;
  3758. }
  3759. static void free_var_defs(struct hist_trigger_data *hist_data)
  3760. {
  3761. unsigned int i;
  3762. for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
  3763. kfree(hist_data->attrs->var_defs.name[i]);
  3764. kfree(hist_data->attrs->var_defs.expr[i]);
  3765. }
  3766. hist_data->attrs->var_defs.n_vars = 0;
  3767. }
  3768. static int parse_var_defs(struct hist_trigger_data *hist_data)
  3769. {
  3770. struct trace_array *tr = hist_data->event_file->tr;
  3771. char *s, *str, *var_name, *field_str;
  3772. unsigned int i, j, n_vars = 0;
  3773. int ret = 0;
  3774. for (i = 0; i < hist_data->attrs->n_assignments; i++) {
  3775. str = hist_data->attrs->assignment_str[i];
  3776. for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
  3777. field_str = strsep(&str, ",");
  3778. if (!field_str)
  3779. break;
  3780. var_name = strsep(&field_str, "=");
  3781. if (!var_name || !field_str) {
  3782. hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
  3783. errpos(var_name));
  3784. ret = -EINVAL;
  3785. goto free;
  3786. }
  3787. if (n_vars == TRACING_MAP_VARS_MAX) {
  3788. hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
  3789. ret = -EINVAL;
  3790. goto free;
  3791. }
  3792. s = kstrdup(var_name, GFP_KERNEL);
  3793. if (!s) {
  3794. ret = -ENOMEM;
  3795. goto free;
  3796. }
  3797. hist_data->attrs->var_defs.name[n_vars] = s;
  3798. s = kstrdup(field_str, GFP_KERNEL);
  3799. if (!s) {
  3800. kfree(hist_data->attrs->var_defs.name[n_vars]);
  3801. hist_data->attrs->var_defs.name[n_vars] = NULL;
  3802. ret = -ENOMEM;
  3803. goto free;
  3804. }
  3805. hist_data->attrs->var_defs.expr[n_vars++] = s;
  3806. hist_data->attrs->var_defs.n_vars = n_vars;
  3807. }
  3808. }
  3809. return ret;
  3810. free:
  3811. free_var_defs(hist_data);
  3812. return ret;
  3813. }
  3814. static int create_hist_fields(struct hist_trigger_data *hist_data,
  3815. struct trace_event_file *file)
  3816. {
  3817. int ret;
  3818. ret = parse_var_defs(hist_data);
  3819. if (ret)
  3820. return ret;
  3821. ret = create_val_fields(hist_data, file);
  3822. if (ret)
  3823. goto out;
  3824. ret = create_var_fields(hist_data, file);
  3825. if (ret)
  3826. goto out;
  3827. ret = create_key_fields(hist_data, file);
  3828. out:
  3829. free_var_defs(hist_data);
  3830. return ret;
  3831. }
  3832. static int is_descending(struct trace_array *tr, const char *str)
  3833. {
  3834. if (!str)
  3835. return 0;
  3836. if (strcmp(str, "descending") == 0)
  3837. return 1;
  3838. if (strcmp(str, "ascending") == 0)
  3839. return 0;
  3840. hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str));
  3841. return -EINVAL;
  3842. }
  3843. static int create_sort_keys(struct hist_trigger_data *hist_data)
  3844. {
  3845. struct trace_array *tr = hist_data->event_file->tr;
  3846. char *fields_str = hist_data->attrs->sort_key_str;
  3847. struct tracing_map_sort_key *sort_key;
  3848. int descending, ret = 0;
  3849. unsigned int i, j, k;
  3850. hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
  3851. if (!fields_str)
  3852. goto out;
  3853. for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
  3854. struct hist_field *hist_field;
  3855. char *field_str, *field_name;
  3856. const char *test_name;
  3857. sort_key = &hist_data->sort_keys[i];
  3858. field_str = strsep(&fields_str, ",");
  3859. if (!field_str)
  3860. break;
  3861. if (!*field_str) {
  3862. ret = -EINVAL;
  3863. hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
  3864. break;
  3865. }
  3866. if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
  3867. hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort="));
  3868. ret = -EINVAL;
  3869. break;
  3870. }
  3871. field_name = strsep(&field_str, ".");
  3872. if (!field_name || !*field_name) {
  3873. ret = -EINVAL;
  3874. hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort="));
  3875. break;
  3876. }
  3877. if (strcmp(field_name, "hitcount") == 0) {
  3878. descending = is_descending(tr, field_str);
  3879. if (descending < 0) {
  3880. ret = descending;
  3881. break;
  3882. }
  3883. sort_key->descending = descending;
  3884. continue;
  3885. }
  3886. for (j = 1, k = 1; j < hist_data->n_fields; j++) {
  3887. unsigned int idx;
  3888. hist_field = hist_data->fields[j];
  3889. if (hist_field->flags & HIST_FIELD_FL_VAR)
  3890. continue;
  3891. idx = k++;
  3892. test_name = hist_field_name(hist_field, 0);
  3893. if (strcmp(field_name, test_name) == 0) {
  3894. sort_key->field_idx = idx;
  3895. descending = is_descending(tr, field_str);
  3896. if (descending < 0) {
  3897. ret = descending;
  3898. goto out;
  3899. }
  3900. sort_key->descending = descending;
  3901. break;
  3902. }
  3903. }
  3904. if (j == hist_data->n_fields) {
  3905. ret = -EINVAL;
  3906. hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name));
  3907. break;
  3908. }
  3909. }
  3910. hist_data->n_sort_keys = i;
  3911. out:
  3912. return ret;
  3913. }
  3914. static void destroy_actions(struct hist_trigger_data *hist_data)
  3915. {
  3916. unsigned int i;
  3917. for (i = 0; i < hist_data->n_actions; i++) {
  3918. struct action_data *data = hist_data->actions[i];
  3919. if (data->handler == HANDLER_ONMATCH)
  3920. onmatch_destroy(data);
  3921. else if (data->handler == HANDLER_ONMAX ||
  3922. data->handler == HANDLER_ONCHANGE)
  3923. track_data_destroy(hist_data, data);
  3924. else
  3925. kfree(data);
  3926. }
  3927. }
  3928. static int parse_actions(struct hist_trigger_data *hist_data)
  3929. {
  3930. struct trace_array *tr = hist_data->event_file->tr;
  3931. struct action_data *data;
  3932. unsigned int i;
  3933. int ret = 0;
  3934. char *str;
  3935. int len;
  3936. for (i = 0; i < hist_data->attrs->n_actions; i++) {
  3937. str = hist_data->attrs->action_str[i];
  3938. if ((len = str_has_prefix(str, "onmatch("))) {
  3939. char *action_str = str + len;
  3940. data = onmatch_parse(tr, action_str);
  3941. if (IS_ERR(data)) {
  3942. ret = PTR_ERR(data);
  3943. break;
  3944. }
  3945. } else if ((len = str_has_prefix(str, "onmax("))) {
  3946. char *action_str = str + len;
  3947. data = track_data_parse(hist_data, action_str,
  3948. HANDLER_ONMAX);
  3949. if (IS_ERR(data)) {
  3950. ret = PTR_ERR(data);
  3951. break;
  3952. }
  3953. } else if ((len = str_has_prefix(str, "onchange("))) {
  3954. char *action_str = str + len;
  3955. data = track_data_parse(hist_data, action_str,
  3956. HANDLER_ONCHANGE);
  3957. if (IS_ERR(data)) {
  3958. ret = PTR_ERR(data);
  3959. break;
  3960. }
  3961. } else {
  3962. ret = -EINVAL;
  3963. break;
  3964. }
  3965. hist_data->actions[hist_data->n_actions++] = data;
  3966. }
  3967. return ret;
  3968. }
  3969. static int create_actions(struct hist_trigger_data *hist_data)
  3970. {
  3971. struct action_data *data;
  3972. unsigned int i;
  3973. int ret = 0;
  3974. for (i = 0; i < hist_data->attrs->n_actions; i++) {
  3975. data = hist_data->actions[i];
  3976. if (data->handler == HANDLER_ONMATCH) {
  3977. ret = onmatch_create(hist_data, data);
  3978. if (ret)
  3979. break;
  3980. } else if (data->handler == HANDLER_ONMAX ||
  3981. data->handler == HANDLER_ONCHANGE) {
  3982. ret = track_data_create(hist_data, data);
  3983. if (ret)
  3984. break;
  3985. } else {
  3986. ret = -EINVAL;
  3987. break;
  3988. }
  3989. }
  3990. return ret;
  3991. }
  3992. static void print_actions(struct seq_file *m,
  3993. struct hist_trigger_data *hist_data,
  3994. struct tracing_map_elt *elt)
  3995. {
  3996. unsigned int i;
  3997. for (i = 0; i < hist_data->n_actions; i++) {
  3998. struct action_data *data = hist_data->actions[i];
  3999. if (data->action == ACTION_SNAPSHOT)
  4000. continue;
  4001. if (data->handler == HANDLER_ONMAX ||
  4002. data->handler == HANDLER_ONCHANGE)
  4003. track_data_print(m, hist_data, elt, data);
  4004. }
  4005. }
  4006. static void print_action_spec(struct seq_file *m,
  4007. struct hist_trigger_data *hist_data,
  4008. struct action_data *data)
  4009. {
  4010. unsigned int i;
  4011. if (data->action == ACTION_SAVE) {
  4012. for (i = 0; i < hist_data->n_save_vars; i++) {
  4013. seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
  4014. if (i < hist_data->n_save_vars - 1)
  4015. seq_puts(m, ",");
  4016. }
  4017. } else if (data->action == ACTION_TRACE) {
  4018. if (data->use_trace_keyword)
  4019. seq_printf(m, "%s", data->synth_event_name);
  4020. for (i = 0; i < data->n_params; i++) {
  4021. if (i || data->use_trace_keyword)
  4022. seq_puts(m, ",");
  4023. seq_printf(m, "%s", data->params[i]);
  4024. }
  4025. }
  4026. }
  4027. static void print_track_data_spec(struct seq_file *m,
  4028. struct hist_trigger_data *hist_data,
  4029. struct action_data *data)
  4030. {
  4031. if (data->handler == HANDLER_ONMAX)
  4032. seq_puts(m, ":onmax(");
  4033. else if (data->handler == HANDLER_ONCHANGE)
  4034. seq_puts(m, ":onchange(");
  4035. seq_printf(m, "%s", data->track_data.var_str);
  4036. seq_printf(m, ").%s(", data->action_name);
  4037. print_action_spec(m, hist_data, data);
  4038. seq_puts(m, ")");
  4039. }
  4040. static void print_onmatch_spec(struct seq_file *m,
  4041. struct hist_trigger_data *hist_data,
  4042. struct action_data *data)
  4043. {
  4044. seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
  4045. data->match_data.event);
  4046. seq_printf(m, "%s(", data->action_name);
  4047. print_action_spec(m, hist_data, data);
  4048. seq_puts(m, ")");
  4049. }
  4050. static bool actions_match(struct hist_trigger_data *hist_data,
  4051. struct hist_trigger_data *hist_data_test)
  4052. {
  4053. unsigned int i, j;
  4054. if (hist_data->n_actions != hist_data_test->n_actions)
  4055. return false;
  4056. for (i = 0; i < hist_data->n_actions; i++) {
  4057. struct action_data *data = hist_data->actions[i];
  4058. struct action_data *data_test = hist_data_test->actions[i];
  4059. char *action_name, *action_name_test;
  4060. if (data->handler != data_test->handler)
  4061. return false;
  4062. if (data->action != data_test->action)
  4063. return false;
  4064. if (data->n_params != data_test->n_params)
  4065. return false;
  4066. for (j = 0; j < data->n_params; j++) {
  4067. if (strcmp(data->params[j], data_test->params[j]) != 0)
  4068. return false;
  4069. }
  4070. if (data->use_trace_keyword)
  4071. action_name = data->synth_event_name;
  4072. else
  4073. action_name = data->action_name;
  4074. if (data_test->use_trace_keyword)
  4075. action_name_test = data_test->synth_event_name;
  4076. else
  4077. action_name_test = data_test->action_name;
  4078. if (strcmp(action_name, action_name_test) != 0)
  4079. return false;
  4080. if (data->handler == HANDLER_ONMATCH) {
  4081. if (strcmp(data->match_data.event_system,
  4082. data_test->match_data.event_system) != 0)
  4083. return false;
  4084. if (strcmp(data->match_data.event,
  4085. data_test->match_data.event) != 0)
  4086. return false;
  4087. } else if (data->handler == HANDLER_ONMAX ||
  4088. data->handler == HANDLER_ONCHANGE) {
  4089. if (strcmp(data->track_data.var_str,
  4090. data_test->track_data.var_str) != 0)
  4091. return false;
  4092. }
  4093. }
  4094. return true;
  4095. }
  4096. static void print_actions_spec(struct seq_file *m,
  4097. struct hist_trigger_data *hist_data)
  4098. {
  4099. unsigned int i;
  4100. for (i = 0; i < hist_data->n_actions; i++) {
  4101. struct action_data *data = hist_data->actions[i];
  4102. if (data->handler == HANDLER_ONMATCH)
  4103. print_onmatch_spec(m, hist_data, data);
  4104. else if (data->handler == HANDLER_ONMAX ||
  4105. data->handler == HANDLER_ONCHANGE)
  4106. print_track_data_spec(m, hist_data, data);
  4107. }
  4108. }
  4109. static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
  4110. {
  4111. unsigned int i;
  4112. for (i = 0; i < hist_data->n_field_var_hists; i++) {
  4113. kfree(hist_data->field_var_hists[i]->cmd);
  4114. kfree(hist_data->field_var_hists[i]);
  4115. }
  4116. }
  4117. static void destroy_hist_data(struct hist_trigger_data *hist_data)
  4118. {
  4119. if (!hist_data)
  4120. return;
  4121. destroy_hist_trigger_attrs(hist_data->attrs);
  4122. destroy_hist_fields(hist_data);
  4123. tracing_map_destroy(hist_data->map);
  4124. destroy_actions(hist_data);
  4125. destroy_field_vars(hist_data);
  4126. destroy_field_var_hists(hist_data);
  4127. kfree(hist_data);
  4128. }
  4129. static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
  4130. {
  4131. struct tracing_map *map = hist_data->map;
  4132. struct ftrace_event_field *field;
  4133. struct hist_field *hist_field;
  4134. int i, idx = 0;
  4135. for_each_hist_field(i, hist_data) {
  4136. hist_field = hist_data->fields[i];
  4137. if (hist_field->flags & HIST_FIELD_FL_KEY) {
  4138. tracing_map_cmp_fn_t cmp_fn;
  4139. field = hist_field->field;
  4140. if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
  4141. cmp_fn = tracing_map_cmp_none;
  4142. else if (!field || hist_field->flags & HIST_FIELD_FL_CPU)
  4143. cmp_fn = tracing_map_cmp_num(hist_field->size,
  4144. hist_field->is_signed);
  4145. else if (is_string_field(field))
  4146. cmp_fn = tracing_map_cmp_string;
  4147. else
  4148. cmp_fn = tracing_map_cmp_num(field->size,
  4149. field->is_signed);
  4150. idx = tracing_map_add_key_field(map,
  4151. hist_field->offset,
  4152. cmp_fn);
  4153. } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
  4154. idx = tracing_map_add_sum_field(map);
  4155. if (idx < 0)
  4156. return idx;
  4157. if (hist_field->flags & HIST_FIELD_FL_VAR) {
  4158. idx = tracing_map_add_var(map);
  4159. if (idx < 0)
  4160. return idx;
  4161. hist_field->var.idx = idx;
  4162. hist_field->var.hist_data = hist_data;
  4163. }
  4164. }
  4165. return 0;
  4166. }
  4167. static struct hist_trigger_data *
  4168. create_hist_data(unsigned int map_bits,
  4169. struct hist_trigger_attrs *attrs,
  4170. struct trace_event_file *file,
  4171. bool remove)
  4172. {
  4173. const struct tracing_map_ops *map_ops = NULL;
  4174. struct hist_trigger_data *hist_data;
  4175. int ret = 0;
  4176. hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
  4177. if (!hist_data)
  4178. return ERR_PTR(-ENOMEM);
  4179. hist_data->attrs = attrs;
  4180. hist_data->remove = remove;
  4181. hist_data->event_file = file;
  4182. ret = parse_actions(hist_data);
  4183. if (ret)
  4184. goto free;
  4185. ret = create_hist_fields(hist_data, file);
  4186. if (ret)
  4187. goto free;
  4188. ret = create_sort_keys(hist_data);
  4189. if (ret)
  4190. goto free;
  4191. map_ops = &hist_trigger_elt_data_ops;
  4192. hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
  4193. map_ops, hist_data);
  4194. if (IS_ERR(hist_data->map)) {
  4195. ret = PTR_ERR(hist_data->map);
  4196. hist_data->map = NULL;
  4197. goto free;
  4198. }
  4199. ret = create_tracing_map_fields(hist_data);
  4200. if (ret)
  4201. goto free;
  4202. out:
  4203. return hist_data;
  4204. free:
  4205. hist_data->attrs = NULL;
  4206. destroy_hist_data(hist_data);
  4207. hist_data = ERR_PTR(ret);
  4208. goto out;
  4209. }
  4210. static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
  4211. struct tracing_map_elt *elt,
  4212. struct trace_buffer *buffer, void *rec,
  4213. struct ring_buffer_event *rbe,
  4214. u64 *var_ref_vals)
  4215. {
  4216. struct hist_elt_data *elt_data;
  4217. struct hist_field *hist_field;
  4218. unsigned int i, var_idx;
  4219. u64 hist_val;
  4220. elt_data = elt->private_data;
  4221. elt_data->var_ref_vals = var_ref_vals;
  4222. for_each_hist_val_field(i, hist_data) {
  4223. hist_field = hist_data->fields[i];
  4224. hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
  4225. if (hist_field->flags & HIST_FIELD_FL_VAR) {
  4226. var_idx = hist_field->var.idx;
  4227. if (hist_field->flags & HIST_FIELD_FL_STRING) {
  4228. unsigned int str_start, var_str_idx, idx;
  4229. char *str, *val_str;
  4230. unsigned int size;
  4231. str_start = hist_data->n_field_var_str +
  4232. hist_data->n_save_var_str;
  4233. var_str_idx = hist_field->var_str_idx;
  4234. idx = str_start + var_str_idx;
  4235. str = elt_data->field_var_str[idx];
  4236. val_str = (char *)(uintptr_t)hist_val;
  4237. size = min(hist_field->size, STR_VAR_LEN_MAX);
  4238. strscpy(str, val_str, size);
  4239. hist_val = (u64)(uintptr_t)str;
  4240. }
  4241. tracing_map_set_var(elt, var_idx, hist_val);
  4242. continue;
  4243. }
  4244. tracing_map_update_sum(elt, i, hist_val);
  4245. }
  4246. for_each_hist_key_field(i, hist_data) {
  4247. hist_field = hist_data->fields[i];
  4248. if (hist_field->flags & HIST_FIELD_FL_VAR) {
  4249. hist_val = hist_fn_call(hist_field, elt, buffer, rbe, rec);
  4250. var_idx = hist_field->var.idx;
  4251. tracing_map_set_var(elt, var_idx, hist_val);
  4252. }
  4253. }
  4254. update_field_vars(hist_data, elt, buffer, rbe, rec);
  4255. }
  4256. static inline void add_to_key(char *compound_key, void *key,
  4257. struct hist_field *key_field, void *rec)
  4258. {
  4259. size_t size = key_field->size;
  4260. if (key_field->flags & HIST_FIELD_FL_STRING) {
  4261. struct ftrace_event_field *field;
  4262. field = key_field->field;
  4263. if (field->filter_type == FILTER_DYN_STRING ||
  4264. field->filter_type == FILTER_RDYN_STRING)
  4265. size = *(u32 *)(rec + field->offset) >> 16;
  4266. else if (field->filter_type == FILTER_STATIC_STRING)
  4267. size = field->size;
  4268. /* ensure NULL-termination */
  4269. if (size > key_field->size - 1)
  4270. size = key_field->size - 1;
  4271. strncpy(compound_key + key_field->offset, (char *)key, size);
  4272. } else
  4273. memcpy(compound_key + key_field->offset, key, size);
  4274. }
  4275. static void
  4276. hist_trigger_actions(struct hist_trigger_data *hist_data,
  4277. struct tracing_map_elt *elt,
  4278. struct trace_buffer *buffer, void *rec,
  4279. struct ring_buffer_event *rbe, void *key,
  4280. u64 *var_ref_vals)
  4281. {
  4282. struct action_data *data;
  4283. unsigned int i;
  4284. for (i = 0; i < hist_data->n_actions; i++) {
  4285. data = hist_data->actions[i];
  4286. data->fn(hist_data, elt, buffer, rec, rbe, key, data, var_ref_vals);
  4287. }
  4288. }
  4289. static void event_hist_trigger(struct event_trigger_data *data,
  4290. struct trace_buffer *buffer, void *rec,
  4291. struct ring_buffer_event *rbe)
  4292. {
  4293. struct hist_trigger_data *hist_data = data->private_data;
  4294. bool use_compound_key = (hist_data->n_keys > 1);
  4295. unsigned long entries[HIST_STACKTRACE_DEPTH];
  4296. u64 var_ref_vals[TRACING_MAP_VARS_MAX];
  4297. char compound_key[HIST_KEY_SIZE_MAX];
  4298. struct tracing_map_elt *elt = NULL;
  4299. struct hist_field *key_field;
  4300. u64 field_contents;
  4301. void *key = NULL;
  4302. unsigned int i;
  4303. if (unlikely(!rbe))
  4304. return;
  4305. memset(compound_key, 0, hist_data->key_size);
  4306. for_each_hist_key_field(i, hist_data) {
  4307. key_field = hist_data->fields[i];
  4308. if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
  4309. memset(entries, 0, HIST_STACKTRACE_SIZE);
  4310. stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
  4311. HIST_STACKTRACE_SKIP);
  4312. key = entries;
  4313. } else {
  4314. field_contents = hist_fn_call(key_field, elt, buffer, rbe, rec);
  4315. if (key_field->flags & HIST_FIELD_FL_STRING) {
  4316. key = (void *)(unsigned long)field_contents;
  4317. use_compound_key = true;
  4318. } else
  4319. key = (void *)&field_contents;
  4320. }
  4321. if (use_compound_key)
  4322. add_to_key(compound_key, key, key_field, rec);
  4323. }
  4324. if (use_compound_key)
  4325. key = compound_key;
  4326. if (hist_data->n_var_refs &&
  4327. !resolve_var_refs(hist_data, key, var_ref_vals, false))
  4328. return;
  4329. elt = tracing_map_insert(hist_data->map, key);
  4330. if (!elt)
  4331. return;
  4332. hist_trigger_elt_update(hist_data, elt, buffer, rec, rbe, var_ref_vals);
  4333. if (resolve_var_refs(hist_data, key, var_ref_vals, true))
  4334. hist_trigger_actions(hist_data, elt, buffer, rec, rbe, key, var_ref_vals);
  4335. }
  4336. static void hist_trigger_stacktrace_print(struct seq_file *m,
  4337. unsigned long *stacktrace_entries,
  4338. unsigned int max_entries)
  4339. {
  4340. unsigned int spaces = 8;
  4341. unsigned int i;
  4342. for (i = 0; i < max_entries; i++) {
  4343. if (!stacktrace_entries[i])
  4344. return;
  4345. seq_printf(m, "%*c", 1 + spaces, ' ');
  4346. seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]);
  4347. }
  4348. }
  4349. static void hist_trigger_print_key(struct seq_file *m,
  4350. struct hist_trigger_data *hist_data,
  4351. void *key,
  4352. struct tracing_map_elt *elt)
  4353. {
  4354. struct hist_field *key_field;
  4355. bool multiline = false;
  4356. const char *field_name;
  4357. unsigned int i;
  4358. u64 uval;
  4359. seq_puts(m, "{ ");
  4360. for_each_hist_key_field(i, hist_data) {
  4361. key_field = hist_data->fields[i];
  4362. if (i > hist_data->n_vals)
  4363. seq_puts(m, ", ");
  4364. field_name = hist_field_name(key_field, 0);
  4365. if (key_field->flags & HIST_FIELD_FL_HEX) {
  4366. uval = *(u64 *)(key + key_field->offset);
  4367. seq_printf(m, "%s: %llx", field_name, uval);
  4368. } else if (key_field->flags & HIST_FIELD_FL_SYM) {
  4369. uval = *(u64 *)(key + key_field->offset);
  4370. seq_printf(m, "%s: [%llx] %-45ps", field_name,
  4371. uval, (void *)(uintptr_t)uval);
  4372. } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
  4373. uval = *(u64 *)(key + key_field->offset);
  4374. seq_printf(m, "%s: [%llx] %-55pS", field_name,
  4375. uval, (void *)(uintptr_t)uval);
  4376. } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
  4377. struct hist_elt_data *elt_data = elt->private_data;
  4378. char *comm;
  4379. if (WARN_ON_ONCE(!elt_data))
  4380. return;
  4381. comm = elt_data->comm;
  4382. uval = *(u64 *)(key + key_field->offset);
  4383. seq_printf(m, "%s: %-16s[%10llu]", field_name,
  4384. comm, uval);
  4385. } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
  4386. const char *syscall_name;
  4387. uval = *(u64 *)(key + key_field->offset);
  4388. syscall_name = get_syscall_name(uval);
  4389. if (!syscall_name)
  4390. syscall_name = "unknown_syscall";
  4391. seq_printf(m, "%s: %-30s[%3llu]", field_name,
  4392. syscall_name, uval);
  4393. } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
  4394. seq_puts(m, "stacktrace:\n");
  4395. hist_trigger_stacktrace_print(m,
  4396. key + key_field->offset,
  4397. HIST_STACKTRACE_DEPTH);
  4398. multiline = true;
  4399. } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
  4400. seq_printf(m, "%s: ~ 2^%-2llu", field_name,
  4401. *(u64 *)(key + key_field->offset));
  4402. } else if (key_field->flags & HIST_FIELD_FL_BUCKET) {
  4403. unsigned long buckets = key_field->buckets;
  4404. uval = *(u64 *)(key + key_field->offset);
  4405. seq_printf(m, "%s: ~ %llu-%llu", field_name,
  4406. uval, uval + buckets -1);
  4407. } else if (key_field->flags & HIST_FIELD_FL_STRING) {
  4408. seq_printf(m, "%s: %-50s", field_name,
  4409. (char *)(key + key_field->offset));
  4410. } else {
  4411. uval = *(u64 *)(key + key_field->offset);
  4412. seq_printf(m, "%s: %10llu", field_name, uval);
  4413. }
  4414. }
  4415. if (!multiline)
  4416. seq_puts(m, " ");
  4417. seq_puts(m, "}");
  4418. }
  4419. /* Get the 100 times of the percentage of @val in @total */
  4420. static inline unsigned int __get_percentage(u64 val, u64 total)
  4421. {
  4422. if (!total)
  4423. goto div0;
  4424. if (val < (U64_MAX / 10000))
  4425. return (unsigned int)div64_ul(val * 10000, total);
  4426. total = div64_u64(total, 10000);
  4427. if (!total)
  4428. goto div0;
  4429. return (unsigned int)div64_ul(val, total);
  4430. div0:
  4431. return val ? UINT_MAX : 0;
  4432. }
  4433. #define BAR_CHAR '#'
  4434. static inline const char *__fill_bar_str(char *buf, int size, u64 val, u64 max)
  4435. {
  4436. unsigned int len = __get_percentage(val, max);
  4437. int i;
  4438. if (len == UINT_MAX) {
  4439. snprintf(buf, size, "[ERROR]");
  4440. return buf;
  4441. }
  4442. len = len * size / 10000;
  4443. for (i = 0; i < len && i < size; i++)
  4444. buf[i] = BAR_CHAR;
  4445. while (i < size)
  4446. buf[i++] = ' ';
  4447. buf[size] = '\0';
  4448. return buf;
  4449. }
  4450. struct hist_val_stat {
  4451. u64 max;
  4452. u64 total;
  4453. };
  4454. static void hist_trigger_print_val(struct seq_file *m, unsigned int idx,
  4455. const char *field_name, unsigned long flags,
  4456. struct hist_val_stat *stats,
  4457. struct tracing_map_elt *elt)
  4458. {
  4459. u64 val = tracing_map_read_sum(elt, idx);
  4460. unsigned int pc;
  4461. char bar[21];
  4462. if (flags & HIST_FIELD_FL_PERCENT) {
  4463. pc = __get_percentage(val, stats[idx].total);
  4464. if (pc == UINT_MAX)
  4465. seq_printf(m, " %s (%%):[ERROR]", field_name);
  4466. else
  4467. seq_printf(m, " %s (%%): %3u.%02u", field_name,
  4468. pc / 100, pc % 100);
  4469. } else if (flags & HIST_FIELD_FL_GRAPH) {
  4470. seq_printf(m, " %s: %20s", field_name,
  4471. __fill_bar_str(bar, 20, val, stats[idx].max));
  4472. } else if (flags & HIST_FIELD_FL_HEX) {
  4473. seq_printf(m, " %s: %10llx", field_name, val);
  4474. } else {
  4475. seq_printf(m, " %s: %10llu", field_name, val);
  4476. }
  4477. }
  4478. static void hist_trigger_entry_print(struct seq_file *m,
  4479. struct hist_trigger_data *hist_data,
  4480. struct hist_val_stat *stats,
  4481. void *key,
  4482. struct tracing_map_elt *elt)
  4483. {
  4484. const char *field_name;
  4485. unsigned int i = HITCOUNT_IDX;
  4486. unsigned long flags;
  4487. hist_trigger_print_key(m, hist_data, key, elt);
  4488. /* At first, show the raw hitcount always */
  4489. hist_trigger_print_val(m, i, "hitcount", 0, stats, elt);
  4490. for (i = 1; i < hist_data->n_vals; i++) {
  4491. field_name = hist_field_name(hist_data->fields[i], 0);
  4492. flags = hist_data->fields[i]->flags;
  4493. if (flags & HIST_FIELD_FL_VAR || flags & HIST_FIELD_FL_EXPR)
  4494. continue;
  4495. seq_puts(m, " ");
  4496. hist_trigger_print_val(m, i, field_name, flags, stats, elt);
  4497. }
  4498. print_actions(m, hist_data, elt);
  4499. seq_puts(m, "\n");
  4500. }
  4501. static int print_entries(struct seq_file *m,
  4502. struct hist_trigger_data *hist_data)
  4503. {
  4504. struct tracing_map_sort_entry **sort_entries = NULL;
  4505. struct tracing_map *map = hist_data->map;
  4506. int i, j, n_entries;
  4507. struct hist_val_stat *stats = NULL;
  4508. u64 val;
  4509. n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
  4510. hist_data->n_sort_keys,
  4511. &sort_entries);
  4512. if (n_entries < 0)
  4513. return n_entries;
  4514. /* Calculate the max and the total for each field if needed. */
  4515. for (j = 0; j < hist_data->n_vals; j++) {
  4516. if (!(hist_data->fields[j]->flags &
  4517. (HIST_FIELD_FL_PERCENT | HIST_FIELD_FL_GRAPH)))
  4518. continue;
  4519. if (!stats) {
  4520. stats = kcalloc(hist_data->n_vals, sizeof(*stats),
  4521. GFP_KERNEL);
  4522. if (!stats) {
  4523. n_entries = -ENOMEM;
  4524. goto out;
  4525. }
  4526. }
  4527. for (i = 0; i < n_entries; i++) {
  4528. val = tracing_map_read_sum(sort_entries[i]->elt, j);
  4529. stats[j].total += val;
  4530. if (stats[j].max < val)
  4531. stats[j].max = val;
  4532. }
  4533. }
  4534. for (i = 0; i < n_entries; i++)
  4535. hist_trigger_entry_print(m, hist_data, stats,
  4536. sort_entries[i]->key,
  4537. sort_entries[i]->elt);
  4538. kfree(stats);
  4539. out:
  4540. tracing_map_destroy_sort_entries(sort_entries, n_entries);
  4541. return n_entries;
  4542. }
  4543. static void hist_trigger_show(struct seq_file *m,
  4544. struct event_trigger_data *data, int n)
  4545. {
  4546. struct hist_trigger_data *hist_data;
  4547. int n_entries;
  4548. if (n > 0)
  4549. seq_puts(m, "\n\n");
  4550. seq_puts(m, "# event histogram\n#\n# trigger info: ");
  4551. data->ops->print(m, data);
  4552. seq_puts(m, "#\n\n");
  4553. hist_data = data->private_data;
  4554. n_entries = print_entries(m, hist_data);
  4555. if (n_entries < 0)
  4556. n_entries = 0;
  4557. track_data_snapshot_print(m, hist_data);
  4558. seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
  4559. (u64)atomic64_read(&hist_data->map->hits),
  4560. n_entries, (u64)atomic64_read(&hist_data->map->drops));
  4561. }
  4562. static int hist_show(struct seq_file *m, void *v)
  4563. {
  4564. struct event_trigger_data *data;
  4565. struct trace_event_file *event_file;
  4566. int n = 0, ret = 0;
  4567. mutex_lock(&event_mutex);
  4568. event_file = event_file_data(m->private);
  4569. if (unlikely(!event_file)) {
  4570. ret = -ENODEV;
  4571. goto out_unlock;
  4572. }
  4573. list_for_each_entry(data, &event_file->triggers, list) {
  4574. if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
  4575. hist_trigger_show(m, data, n++);
  4576. }
  4577. out_unlock:
  4578. mutex_unlock(&event_mutex);
  4579. return ret;
  4580. }
  4581. static int event_hist_open(struct inode *inode, struct file *file)
  4582. {
  4583. int ret;
  4584. ret = security_locked_down(LOCKDOWN_TRACEFS);
  4585. if (ret)
  4586. return ret;
  4587. return single_open(file, hist_show, file);
  4588. }
  4589. const struct file_operations event_hist_fops = {
  4590. .open = event_hist_open,
  4591. .read = seq_read,
  4592. .llseek = seq_lseek,
  4593. .release = single_release,
  4594. };
  4595. #ifdef CONFIG_HIST_TRIGGERS_DEBUG
  4596. static void hist_field_debug_show_flags(struct seq_file *m,
  4597. unsigned long flags)
  4598. {
  4599. seq_puts(m, " flags:\n");
  4600. if (flags & HIST_FIELD_FL_KEY)
  4601. seq_puts(m, " HIST_FIELD_FL_KEY\n");
  4602. else if (flags & HIST_FIELD_FL_HITCOUNT)
  4603. seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n");
  4604. else if (flags & HIST_FIELD_FL_VAR)
  4605. seq_puts(m, " HIST_FIELD_FL_VAR\n");
  4606. else if (flags & HIST_FIELD_FL_VAR_REF)
  4607. seq_puts(m, " HIST_FIELD_FL_VAR_REF\n");
  4608. else
  4609. seq_puts(m, " VAL: normal u64 value\n");
  4610. if (flags & HIST_FIELD_FL_ALIAS)
  4611. seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
  4612. else if (flags & HIST_FIELD_FL_CONST)
  4613. seq_puts(m, " HIST_FIELD_FL_CONST\n");
  4614. }
  4615. static int hist_field_debug_show(struct seq_file *m,
  4616. struct hist_field *field, unsigned long flags)
  4617. {
  4618. if ((field->flags & flags) != flags) {
  4619. seq_printf(m, "ERROR: bad flags - %lx\n", flags);
  4620. return -EINVAL;
  4621. }
  4622. hist_field_debug_show_flags(m, field->flags);
  4623. if (field->field)
  4624. seq_printf(m, " ftrace_event_field name: %s\n",
  4625. field->field->name);
  4626. if (field->flags & HIST_FIELD_FL_VAR) {
  4627. seq_printf(m, " var.name: %s\n", field->var.name);
  4628. seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
  4629. field->var.idx);
  4630. }
  4631. if (field->flags & HIST_FIELD_FL_CONST)
  4632. seq_printf(m, " constant: %llu\n", field->constant);
  4633. if (field->flags & HIST_FIELD_FL_ALIAS)
  4634. seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
  4635. field->var_ref_idx);
  4636. if (field->flags & HIST_FIELD_FL_VAR_REF) {
  4637. seq_printf(m, " name: %s\n", field->name);
  4638. seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
  4639. field->var.idx);
  4640. seq_printf(m, " var.hist_data: %p\n", field->var.hist_data);
  4641. seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
  4642. field->var_ref_idx);
  4643. if (field->system)
  4644. seq_printf(m, " system: %s\n", field->system);
  4645. if (field->event_name)
  4646. seq_printf(m, " event_name: %s\n", field->event_name);
  4647. }
  4648. seq_printf(m, " type: %s\n", field->type);
  4649. seq_printf(m, " size: %u\n", field->size);
  4650. seq_printf(m, " is_signed: %u\n", field->is_signed);
  4651. return 0;
  4652. }
  4653. static int field_var_debug_show(struct seq_file *m,
  4654. struct field_var *field_var, unsigned int i,
  4655. bool save_vars)
  4656. {
  4657. const char *vars_name = save_vars ? "save_vars" : "field_vars";
  4658. struct hist_field *field;
  4659. int ret = 0;
  4660. seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i);
  4661. field = field_var->var;
  4662. seq_printf(m, "\n %s[%d].var:\n", vars_name, i);
  4663. hist_field_debug_show_flags(m, field->flags);
  4664. seq_printf(m, " var.name: %s\n", field->var.name);
  4665. seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n",
  4666. field->var.idx);
  4667. field = field_var->val;
  4668. seq_printf(m, "\n %s[%d].val:\n", vars_name, i);
  4669. if (field->field)
  4670. seq_printf(m, " ftrace_event_field name: %s\n",
  4671. field->field->name);
  4672. else {
  4673. ret = -EINVAL;
  4674. goto out;
  4675. }
  4676. seq_printf(m, " type: %s\n", field->type);
  4677. seq_printf(m, " size: %u\n", field->size);
  4678. seq_printf(m, " is_signed: %u\n", field->is_signed);
  4679. out:
  4680. return ret;
  4681. }
  4682. static int hist_action_debug_show(struct seq_file *m,
  4683. struct action_data *data, int i)
  4684. {
  4685. int ret = 0;
  4686. if (data->handler == HANDLER_ONMAX ||
  4687. data->handler == HANDLER_ONCHANGE) {
  4688. seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i);
  4689. ret = hist_field_debug_show(m, data->track_data.var_ref,
  4690. HIST_FIELD_FL_VAR_REF);
  4691. if (ret)
  4692. goto out;
  4693. seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i);
  4694. ret = hist_field_debug_show(m, data->track_data.track_var,
  4695. HIST_FIELD_FL_VAR);
  4696. if (ret)
  4697. goto out;
  4698. }
  4699. if (data->handler == HANDLER_ONMATCH) {
  4700. seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n",
  4701. i, data->match_data.event_system);
  4702. seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n",
  4703. i, data->match_data.event);
  4704. }
  4705. out:
  4706. return ret;
  4707. }
  4708. static int hist_actions_debug_show(struct seq_file *m,
  4709. struct hist_trigger_data *hist_data)
  4710. {
  4711. int i, ret = 0;
  4712. if (hist_data->n_actions)
  4713. seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n");
  4714. for (i = 0; i < hist_data->n_actions; i++) {
  4715. struct action_data *action = hist_data->actions[i];
  4716. ret = hist_action_debug_show(m, action, i);
  4717. if (ret)
  4718. goto out;
  4719. }
  4720. if (hist_data->n_save_vars)
  4721. seq_puts(m, "\n save action variables (save() params):\n");
  4722. for (i = 0; i < hist_data->n_save_vars; i++) {
  4723. ret = field_var_debug_show(m, hist_data->save_vars[i], i, true);
  4724. if (ret)
  4725. goto out;
  4726. }
  4727. out:
  4728. return ret;
  4729. }
  4730. static void hist_trigger_debug_show(struct seq_file *m,
  4731. struct event_trigger_data *data, int n)
  4732. {
  4733. struct hist_trigger_data *hist_data;
  4734. int i, ret;
  4735. if (n > 0)
  4736. seq_puts(m, "\n\n");
  4737. seq_puts(m, "# event histogram\n#\n# trigger info: ");
  4738. data->ops->print(m, data);
  4739. seq_puts(m, "#\n\n");
  4740. hist_data = data->private_data;
  4741. seq_printf(m, "hist_data: %p\n\n", hist_data);
  4742. seq_printf(m, " n_vals: %u\n", hist_data->n_vals);
  4743. seq_printf(m, " n_keys: %u\n", hist_data->n_keys);
  4744. seq_printf(m, " n_fields: %u\n", hist_data->n_fields);
  4745. seq_puts(m, "\n val fields:\n\n");
  4746. seq_puts(m, " hist_data->fields[0]:\n");
  4747. ret = hist_field_debug_show(m, hist_data->fields[0],
  4748. HIST_FIELD_FL_HITCOUNT);
  4749. if (ret)
  4750. return;
  4751. for (i = 1; i < hist_data->n_vals; i++) {
  4752. seq_printf(m, "\n hist_data->fields[%d]:\n", i);
  4753. ret = hist_field_debug_show(m, hist_data->fields[i], 0);
  4754. if (ret)
  4755. return;
  4756. }
  4757. seq_puts(m, "\n key fields:\n");
  4758. for (i = hist_data->n_vals; i < hist_data->n_fields; i++) {
  4759. seq_printf(m, "\n hist_data->fields[%d]:\n", i);
  4760. ret = hist_field_debug_show(m, hist_data->fields[i],
  4761. HIST_FIELD_FL_KEY);
  4762. if (ret)
  4763. return;
  4764. }
  4765. if (hist_data->n_var_refs)
  4766. seq_puts(m, "\n variable reference fields:\n");
  4767. for (i = 0; i < hist_data->n_var_refs; i++) {
  4768. seq_printf(m, "\n hist_data->var_refs[%d]:\n", i);
  4769. ret = hist_field_debug_show(m, hist_data->var_refs[i],
  4770. HIST_FIELD_FL_VAR_REF);
  4771. if (ret)
  4772. return;
  4773. }
  4774. if (hist_data->n_field_vars)
  4775. seq_puts(m, "\n field variables:\n");
  4776. for (i = 0; i < hist_data->n_field_vars; i++) {
  4777. ret = field_var_debug_show(m, hist_data->field_vars[i], i, false);
  4778. if (ret)
  4779. return;
  4780. }
  4781. ret = hist_actions_debug_show(m, hist_data);
  4782. if (ret)
  4783. return;
  4784. }
  4785. static int hist_debug_show(struct seq_file *m, void *v)
  4786. {
  4787. struct event_trigger_data *data;
  4788. struct trace_event_file *event_file;
  4789. int n = 0, ret = 0;
  4790. mutex_lock(&event_mutex);
  4791. event_file = event_file_data(m->private);
  4792. if (unlikely(!event_file)) {
  4793. ret = -ENODEV;
  4794. goto out_unlock;
  4795. }
  4796. list_for_each_entry(data, &event_file->triggers, list) {
  4797. if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
  4798. hist_trigger_debug_show(m, data, n++);
  4799. }
  4800. out_unlock:
  4801. mutex_unlock(&event_mutex);
  4802. return ret;
  4803. }
  4804. static int event_hist_debug_open(struct inode *inode, struct file *file)
  4805. {
  4806. int ret;
  4807. ret = security_locked_down(LOCKDOWN_TRACEFS);
  4808. if (ret)
  4809. return ret;
  4810. return single_open(file, hist_debug_show, file);
  4811. }
  4812. const struct file_operations event_hist_debug_fops = {
  4813. .open = event_hist_debug_open,
  4814. .read = seq_read,
  4815. .llseek = seq_lseek,
  4816. .release = single_release,
  4817. };
  4818. #endif
  4819. static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
  4820. {
  4821. const char *field_name = hist_field_name(hist_field, 0);
  4822. if (hist_field->var.name)
  4823. seq_printf(m, "%s=", hist_field->var.name);
  4824. if (hist_field->flags & HIST_FIELD_FL_CPU)
  4825. seq_puts(m, "common_cpu");
  4826. else if (hist_field->flags & HIST_FIELD_FL_CONST)
  4827. seq_printf(m, "%llu", hist_field->constant);
  4828. else if (field_name) {
  4829. if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
  4830. hist_field->flags & HIST_FIELD_FL_ALIAS)
  4831. seq_putc(m, '$');
  4832. seq_printf(m, "%s", field_name);
  4833. } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
  4834. seq_puts(m, "common_timestamp");
  4835. if (hist_field->flags) {
  4836. if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
  4837. !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
  4838. const char *flags = get_hist_field_flags(hist_field);
  4839. if (flags)
  4840. seq_printf(m, ".%s", flags);
  4841. }
  4842. }
  4843. if (hist_field->buckets)
  4844. seq_printf(m, "=%ld", hist_field->buckets);
  4845. }
  4846. static int event_hist_trigger_print(struct seq_file *m,
  4847. struct event_trigger_data *data)
  4848. {
  4849. struct hist_trigger_data *hist_data = data->private_data;
  4850. struct hist_field *field;
  4851. bool have_var = false;
  4852. unsigned int i;
  4853. seq_puts(m, HIST_PREFIX);
  4854. if (data->name)
  4855. seq_printf(m, "%s:", data->name);
  4856. seq_puts(m, "keys=");
  4857. for_each_hist_key_field(i, hist_data) {
  4858. field = hist_data->fields[i];
  4859. if (i > hist_data->n_vals)
  4860. seq_puts(m, ",");
  4861. if (field->flags & HIST_FIELD_FL_STACKTRACE)
  4862. seq_puts(m, "stacktrace");
  4863. else
  4864. hist_field_print(m, field);
  4865. }
  4866. seq_puts(m, ":vals=");
  4867. for_each_hist_val_field(i, hist_data) {
  4868. field = hist_data->fields[i];
  4869. if (field->flags & HIST_FIELD_FL_VAR) {
  4870. have_var = true;
  4871. continue;
  4872. }
  4873. if (i == HITCOUNT_IDX)
  4874. seq_puts(m, "hitcount");
  4875. else {
  4876. seq_puts(m, ",");
  4877. hist_field_print(m, field);
  4878. }
  4879. }
  4880. if (have_var) {
  4881. unsigned int n = 0;
  4882. seq_puts(m, ":");
  4883. for_each_hist_val_field(i, hist_data) {
  4884. field = hist_data->fields[i];
  4885. if (field->flags & HIST_FIELD_FL_VAR) {
  4886. if (n++)
  4887. seq_puts(m, ",");
  4888. hist_field_print(m, field);
  4889. }
  4890. }
  4891. }
  4892. seq_puts(m, ":sort=");
  4893. for (i = 0; i < hist_data->n_sort_keys; i++) {
  4894. struct tracing_map_sort_key *sort_key;
  4895. unsigned int idx, first_key_idx;
  4896. /* skip VAR vals */
  4897. first_key_idx = hist_data->n_vals - hist_data->n_vars;
  4898. sort_key = &hist_data->sort_keys[i];
  4899. idx = sort_key->field_idx;
  4900. if (WARN_ON(idx >= HIST_FIELDS_MAX))
  4901. return -EINVAL;
  4902. if (i > 0)
  4903. seq_puts(m, ",");
  4904. if (idx == HITCOUNT_IDX)
  4905. seq_puts(m, "hitcount");
  4906. else {
  4907. if (idx >= first_key_idx)
  4908. idx += hist_data->n_vars;
  4909. hist_field_print(m, hist_data->fields[idx]);
  4910. }
  4911. if (sort_key->descending)
  4912. seq_puts(m, ".descending");
  4913. }
  4914. seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
  4915. if (hist_data->enable_timestamps)
  4916. seq_printf(m, ":clock=%s", hist_data->attrs->clock);
  4917. print_actions_spec(m, hist_data);
  4918. if (data->filter_str)
  4919. seq_printf(m, " if %s", data->filter_str);
  4920. if (data->paused)
  4921. seq_puts(m, " [paused]");
  4922. else
  4923. seq_puts(m, " [active]");
  4924. seq_putc(m, '\n');
  4925. return 0;
  4926. }
  4927. static int event_hist_trigger_init(struct event_trigger_data *data)
  4928. {
  4929. struct hist_trigger_data *hist_data = data->private_data;
  4930. if (!data->ref && hist_data->attrs->name)
  4931. save_named_trigger(hist_data->attrs->name, data);
  4932. data->ref++;
  4933. return 0;
  4934. }
  4935. static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
  4936. {
  4937. struct trace_event_file *file;
  4938. unsigned int i;
  4939. char *cmd;
  4940. int ret;
  4941. for (i = 0; i < hist_data->n_field_var_hists; i++) {
  4942. file = hist_data->field_var_hists[i]->hist_data->event_file;
  4943. cmd = hist_data->field_var_hists[i]->cmd;
  4944. ret = event_hist_trigger_parse(&trigger_hist_cmd, file,
  4945. "!hist", "hist", cmd);
  4946. WARN_ON_ONCE(ret < 0);
  4947. }
  4948. }
  4949. static void event_hist_trigger_free(struct event_trigger_data *data)
  4950. {
  4951. struct hist_trigger_data *hist_data = data->private_data;
  4952. if (WARN_ON_ONCE(data->ref <= 0))
  4953. return;
  4954. data->ref--;
  4955. if (!data->ref) {
  4956. if (data->name)
  4957. del_named_trigger(data);
  4958. trigger_data_free(data);
  4959. remove_hist_vars(hist_data);
  4960. unregister_field_var_hists(hist_data);
  4961. destroy_hist_data(hist_data);
  4962. }
  4963. }
  4964. static struct event_trigger_ops event_hist_trigger_ops = {
  4965. .trigger = event_hist_trigger,
  4966. .print = event_hist_trigger_print,
  4967. .init = event_hist_trigger_init,
  4968. .free = event_hist_trigger_free,
  4969. };
  4970. static int event_hist_trigger_named_init(struct event_trigger_data *data)
  4971. {
  4972. data->ref++;
  4973. save_named_trigger(data->named_data->name, data);
  4974. event_hist_trigger_init(data->named_data);
  4975. return 0;
  4976. }
  4977. static void event_hist_trigger_named_free(struct event_trigger_data *data)
  4978. {
  4979. if (WARN_ON_ONCE(data->ref <= 0))
  4980. return;
  4981. event_hist_trigger_free(data->named_data);
  4982. data->ref--;
  4983. if (!data->ref) {
  4984. del_named_trigger(data);
  4985. trigger_data_free(data);
  4986. }
  4987. }
  4988. static struct event_trigger_ops event_hist_trigger_named_ops = {
  4989. .trigger = event_hist_trigger,
  4990. .print = event_hist_trigger_print,
  4991. .init = event_hist_trigger_named_init,
  4992. .free = event_hist_trigger_named_free,
  4993. };
  4994. static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
  4995. char *param)
  4996. {
  4997. return &event_hist_trigger_ops;
  4998. }
  4999. static void hist_clear(struct event_trigger_data *data)
  5000. {
  5001. struct hist_trigger_data *hist_data = data->private_data;
  5002. if (data->name)
  5003. pause_named_trigger(data);
  5004. tracepoint_synchronize_unregister();
  5005. tracing_map_clear(hist_data->map);
  5006. if (data->name)
  5007. unpause_named_trigger(data);
  5008. }
  5009. static bool compatible_field(struct ftrace_event_field *field,
  5010. struct ftrace_event_field *test_field)
  5011. {
  5012. if (field == test_field)
  5013. return true;
  5014. if (field == NULL || test_field == NULL)
  5015. return false;
  5016. if (strcmp(field->name, test_field->name) != 0)
  5017. return false;
  5018. if (strcmp(field->type, test_field->type) != 0)
  5019. return false;
  5020. if (field->size != test_field->size)
  5021. return false;
  5022. if (field->is_signed != test_field->is_signed)
  5023. return false;
  5024. return true;
  5025. }
  5026. static bool hist_trigger_match(struct event_trigger_data *data,
  5027. struct event_trigger_data *data_test,
  5028. struct event_trigger_data *named_data,
  5029. bool ignore_filter)
  5030. {
  5031. struct tracing_map_sort_key *sort_key, *sort_key_test;
  5032. struct hist_trigger_data *hist_data, *hist_data_test;
  5033. struct hist_field *key_field, *key_field_test;
  5034. unsigned int i;
  5035. if (named_data && (named_data != data_test) &&
  5036. (named_data != data_test->named_data))
  5037. return false;
  5038. if (!named_data && is_named_trigger(data_test))
  5039. return false;
  5040. hist_data = data->private_data;
  5041. hist_data_test = data_test->private_data;
  5042. if (hist_data->n_vals != hist_data_test->n_vals ||
  5043. hist_data->n_fields != hist_data_test->n_fields ||
  5044. hist_data->n_sort_keys != hist_data_test->n_sort_keys)
  5045. return false;
  5046. if (!ignore_filter) {
  5047. if ((data->filter_str && !data_test->filter_str) ||
  5048. (!data->filter_str && data_test->filter_str))
  5049. return false;
  5050. }
  5051. for_each_hist_field(i, hist_data) {
  5052. key_field = hist_data->fields[i];
  5053. key_field_test = hist_data_test->fields[i];
  5054. if (key_field->flags != key_field_test->flags)
  5055. return false;
  5056. if (!compatible_field(key_field->field, key_field_test->field))
  5057. return false;
  5058. if (key_field->offset != key_field_test->offset)
  5059. return false;
  5060. if (key_field->size != key_field_test->size)
  5061. return false;
  5062. if (key_field->is_signed != key_field_test->is_signed)
  5063. return false;
  5064. if (!!key_field->var.name != !!key_field_test->var.name)
  5065. return false;
  5066. if (key_field->var.name &&
  5067. strcmp(key_field->var.name, key_field_test->var.name) != 0)
  5068. return false;
  5069. }
  5070. for (i = 0; i < hist_data->n_sort_keys; i++) {
  5071. sort_key = &hist_data->sort_keys[i];
  5072. sort_key_test = &hist_data_test->sort_keys[i];
  5073. if (sort_key->field_idx != sort_key_test->field_idx ||
  5074. sort_key->descending != sort_key_test->descending)
  5075. return false;
  5076. }
  5077. if (!ignore_filter && data->filter_str &&
  5078. (strcmp(data->filter_str, data_test->filter_str) != 0))
  5079. return false;
  5080. if (!actions_match(hist_data, hist_data_test))
  5081. return false;
  5082. return true;
  5083. }
  5084. static bool existing_hist_update_only(char *glob,
  5085. struct event_trigger_data *data,
  5086. struct trace_event_file *file)
  5087. {
  5088. struct hist_trigger_data *hist_data = data->private_data;
  5089. struct event_trigger_data *test, *named_data = NULL;
  5090. bool updated = false;
  5091. if (!hist_data->attrs->pause && !hist_data->attrs->cont &&
  5092. !hist_data->attrs->clear)
  5093. goto out;
  5094. if (hist_data->attrs->name) {
  5095. named_data = find_named_trigger(hist_data->attrs->name);
  5096. if (named_data) {
  5097. if (!hist_trigger_match(data, named_data, named_data,
  5098. true))
  5099. goto out;
  5100. }
  5101. }
  5102. if (hist_data->attrs->name && !named_data)
  5103. goto out;
  5104. list_for_each_entry(test, &file->triggers, list) {
  5105. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  5106. if (!hist_trigger_match(data, test, named_data, false))
  5107. continue;
  5108. if (hist_data->attrs->pause)
  5109. test->paused = true;
  5110. else if (hist_data->attrs->cont)
  5111. test->paused = false;
  5112. else if (hist_data->attrs->clear)
  5113. hist_clear(test);
  5114. updated = true;
  5115. goto out;
  5116. }
  5117. }
  5118. out:
  5119. return updated;
  5120. }
  5121. static int hist_register_trigger(char *glob,
  5122. struct event_trigger_data *data,
  5123. struct trace_event_file *file)
  5124. {
  5125. struct hist_trigger_data *hist_data = data->private_data;
  5126. struct event_trigger_data *test, *named_data = NULL;
  5127. struct trace_array *tr = file->tr;
  5128. int ret = 0;
  5129. if (hist_data->attrs->name) {
  5130. named_data = find_named_trigger(hist_data->attrs->name);
  5131. if (named_data) {
  5132. if (!hist_trigger_match(data, named_data, named_data,
  5133. true)) {
  5134. hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
  5135. ret = -EINVAL;
  5136. goto out;
  5137. }
  5138. }
  5139. }
  5140. if (hist_data->attrs->name && !named_data)
  5141. goto new;
  5142. lockdep_assert_held(&event_mutex);
  5143. list_for_each_entry(test, &file->triggers, list) {
  5144. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  5145. if (hist_trigger_match(data, test, named_data, false)) {
  5146. hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
  5147. ret = -EEXIST;
  5148. goto out;
  5149. }
  5150. }
  5151. }
  5152. new:
  5153. if (hist_data->attrs->cont || hist_data->attrs->clear) {
  5154. hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
  5155. ret = -ENOENT;
  5156. goto out;
  5157. }
  5158. if (hist_data->attrs->pause)
  5159. data->paused = true;
  5160. if (named_data) {
  5161. data->private_data = named_data->private_data;
  5162. set_named_trigger_data(data, named_data);
  5163. data->ops = &event_hist_trigger_named_ops;
  5164. }
  5165. if (data->ops->init) {
  5166. ret = data->ops->init(data);
  5167. if (ret < 0)
  5168. goto out;
  5169. }
  5170. if (hist_data->enable_timestamps) {
  5171. char *clock = hist_data->attrs->clock;
  5172. ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
  5173. if (ret) {
  5174. hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
  5175. goto out;
  5176. }
  5177. tracing_set_filter_buffering(file->tr, true);
  5178. }
  5179. if (named_data)
  5180. destroy_hist_data(hist_data);
  5181. out:
  5182. return ret;
  5183. }
  5184. static int hist_trigger_enable(struct event_trigger_data *data,
  5185. struct trace_event_file *file)
  5186. {
  5187. int ret = 0;
  5188. list_add_tail_rcu(&data->list, &file->triggers);
  5189. update_cond_flag(file);
  5190. if (trace_event_trigger_enable_disable(file, 1) < 0) {
  5191. list_del_rcu(&data->list);
  5192. update_cond_flag(file);
  5193. ret--;
  5194. }
  5195. return ret;
  5196. }
  5197. static bool have_hist_trigger_match(struct event_trigger_data *data,
  5198. struct trace_event_file *file)
  5199. {
  5200. struct hist_trigger_data *hist_data = data->private_data;
  5201. struct event_trigger_data *test, *named_data = NULL;
  5202. bool match = false;
  5203. lockdep_assert_held(&event_mutex);
  5204. if (hist_data->attrs->name)
  5205. named_data = find_named_trigger(hist_data->attrs->name);
  5206. list_for_each_entry(test, &file->triggers, list) {
  5207. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  5208. if (hist_trigger_match(data, test, named_data, false)) {
  5209. match = true;
  5210. break;
  5211. }
  5212. }
  5213. }
  5214. return match;
  5215. }
  5216. static bool hist_trigger_check_refs(struct event_trigger_data *data,
  5217. struct trace_event_file *file)
  5218. {
  5219. struct hist_trigger_data *hist_data = data->private_data;
  5220. struct event_trigger_data *test, *named_data = NULL;
  5221. lockdep_assert_held(&event_mutex);
  5222. if (hist_data->attrs->name)
  5223. named_data = find_named_trigger(hist_data->attrs->name);
  5224. list_for_each_entry(test, &file->triggers, list) {
  5225. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  5226. if (!hist_trigger_match(data, test, named_data, false))
  5227. continue;
  5228. hist_data = test->private_data;
  5229. if (check_var_refs(hist_data))
  5230. return true;
  5231. break;
  5232. }
  5233. }
  5234. return false;
  5235. }
  5236. static void hist_unregister_trigger(char *glob,
  5237. struct event_trigger_data *data,
  5238. struct trace_event_file *file)
  5239. {
  5240. struct event_trigger_data *test = NULL, *iter, *named_data = NULL;
  5241. struct hist_trigger_data *hist_data = data->private_data;
  5242. lockdep_assert_held(&event_mutex);
  5243. if (hist_data->attrs->name)
  5244. named_data = find_named_trigger(hist_data->attrs->name);
  5245. list_for_each_entry(iter, &file->triggers, list) {
  5246. if (iter->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  5247. if (!hist_trigger_match(data, iter, named_data, false))
  5248. continue;
  5249. test = iter;
  5250. list_del_rcu(&test->list);
  5251. trace_event_trigger_enable_disable(file, 0);
  5252. update_cond_flag(file);
  5253. break;
  5254. }
  5255. }
  5256. if (test && test->ops->free)
  5257. test->ops->free(test);
  5258. if (hist_data->enable_timestamps) {
  5259. if (!hist_data->remove || test)
  5260. tracing_set_filter_buffering(file->tr, false);
  5261. }
  5262. }
  5263. static bool hist_file_check_refs(struct trace_event_file *file)
  5264. {
  5265. struct hist_trigger_data *hist_data;
  5266. struct event_trigger_data *test;
  5267. lockdep_assert_held(&event_mutex);
  5268. list_for_each_entry(test, &file->triggers, list) {
  5269. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  5270. hist_data = test->private_data;
  5271. if (check_var_refs(hist_data))
  5272. return true;
  5273. }
  5274. }
  5275. return false;
  5276. }
  5277. static void hist_unreg_all(struct trace_event_file *file)
  5278. {
  5279. struct event_trigger_data *test, *n;
  5280. struct hist_trigger_data *hist_data;
  5281. struct synth_event *se;
  5282. const char *se_name;
  5283. lockdep_assert_held(&event_mutex);
  5284. if (hist_file_check_refs(file))
  5285. return;
  5286. list_for_each_entry_safe(test, n, &file->triggers, list) {
  5287. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  5288. hist_data = test->private_data;
  5289. list_del_rcu(&test->list);
  5290. trace_event_trigger_enable_disable(file, 0);
  5291. se_name = trace_event_name(file->event_call);
  5292. se = find_synth_event(se_name);
  5293. if (se)
  5294. se->ref--;
  5295. update_cond_flag(file);
  5296. if (hist_data->enable_timestamps)
  5297. tracing_set_filter_buffering(file->tr, false);
  5298. if (test->ops->free)
  5299. test->ops->free(test);
  5300. }
  5301. }
  5302. }
  5303. static int event_hist_trigger_parse(struct event_command *cmd_ops,
  5304. struct trace_event_file *file,
  5305. char *glob, char *cmd,
  5306. char *param_and_filter)
  5307. {
  5308. unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
  5309. struct event_trigger_data *trigger_data;
  5310. struct hist_trigger_attrs *attrs;
  5311. struct hist_trigger_data *hist_data;
  5312. char *param, *filter, *p, *start;
  5313. struct synth_event *se;
  5314. const char *se_name;
  5315. bool remove;
  5316. int ret = 0;
  5317. lockdep_assert_held(&event_mutex);
  5318. if (WARN_ON(!glob))
  5319. return -EINVAL;
  5320. if (glob[0]) {
  5321. hist_err_clear();
  5322. last_cmd_set(file, param_and_filter);
  5323. }
  5324. remove = event_trigger_check_remove(glob);
  5325. if (event_trigger_empty_param(param_and_filter))
  5326. return -EINVAL;
  5327. /*
  5328. * separate the trigger from the filter (k:v [if filter])
  5329. * allowing for whitespace in the trigger
  5330. */
  5331. p = param = param_and_filter;
  5332. do {
  5333. p = strstr(p, "if");
  5334. if (!p)
  5335. break;
  5336. if (p == param_and_filter)
  5337. return -EINVAL;
  5338. if (*(p - 1) != ' ' && *(p - 1) != '\t') {
  5339. p++;
  5340. continue;
  5341. }
  5342. if (p >= param_and_filter + strlen(param_and_filter) - (sizeof("if") - 1) - 1)
  5343. return -EINVAL;
  5344. if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
  5345. p++;
  5346. continue;
  5347. }
  5348. break;
  5349. } while (1);
  5350. if (!p)
  5351. filter = NULL;
  5352. else {
  5353. *(p - 1) = '\0';
  5354. filter = strstrip(p);
  5355. param = strstrip(param);
  5356. }
  5357. /*
  5358. * To simplify arithmetic expression parsing, replace occurrences of
  5359. * '.sym-offset' modifier with '.symXoffset'
  5360. */
  5361. start = strstr(param, ".sym-offset");
  5362. while (start) {
  5363. *(start + 4) = 'X';
  5364. start = strstr(start + 11, ".sym-offset");
  5365. }
  5366. attrs = parse_hist_trigger_attrs(file->tr, param);
  5367. if (IS_ERR(attrs))
  5368. return PTR_ERR(attrs);
  5369. if (attrs->map_bits)
  5370. hist_trigger_bits = attrs->map_bits;
  5371. hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
  5372. if (IS_ERR(hist_data)) {
  5373. destroy_hist_trigger_attrs(attrs);
  5374. return PTR_ERR(hist_data);
  5375. }
  5376. trigger_data = event_trigger_alloc(cmd_ops, cmd, param, hist_data);
  5377. if (!trigger_data) {
  5378. ret = -ENOMEM;
  5379. goto out_free;
  5380. }
  5381. ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
  5382. if (ret < 0)
  5383. goto out_free;
  5384. if (remove) {
  5385. if (!have_hist_trigger_match(trigger_data, file))
  5386. goto out_free;
  5387. if (hist_trigger_check_refs(trigger_data, file)) {
  5388. ret = -EBUSY;
  5389. goto out_free;
  5390. }
  5391. event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
  5392. se_name = trace_event_name(file->event_call);
  5393. se = find_synth_event(se_name);
  5394. if (se)
  5395. se->ref--;
  5396. ret = 0;
  5397. goto out_free;
  5398. }
  5399. if (existing_hist_update_only(glob, trigger_data, file))
  5400. goto out_free;
  5401. ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
  5402. if (ret < 0)
  5403. goto out_free;
  5404. if (get_named_trigger_data(trigger_data))
  5405. goto enable;
  5406. ret = create_actions(hist_data);
  5407. if (ret)
  5408. goto out_unreg;
  5409. if (has_hist_vars(hist_data) || hist_data->n_var_refs) {
  5410. ret = save_hist_vars(hist_data);
  5411. if (ret)
  5412. goto out_unreg;
  5413. }
  5414. ret = tracing_map_init(hist_data->map);
  5415. if (ret)
  5416. goto out_unreg;
  5417. enable:
  5418. ret = hist_trigger_enable(trigger_data, file);
  5419. if (ret)
  5420. goto out_unreg;
  5421. se_name = trace_event_name(file->event_call);
  5422. se = find_synth_event(se_name);
  5423. if (se)
  5424. se->ref++;
  5425. out:
  5426. if (ret == 0 && glob[0])
  5427. hist_err_clear();
  5428. return ret;
  5429. out_unreg:
  5430. event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
  5431. out_free:
  5432. event_trigger_reset_filter(cmd_ops, trigger_data);
  5433. remove_hist_vars(hist_data);
  5434. kfree(trigger_data);
  5435. destroy_hist_data(hist_data);
  5436. goto out;
  5437. }
  5438. static struct event_command trigger_hist_cmd = {
  5439. .name = "hist",
  5440. .trigger_type = ETT_EVENT_HIST,
  5441. .flags = EVENT_CMD_FL_NEEDS_REC,
  5442. .parse = event_hist_trigger_parse,
  5443. .reg = hist_register_trigger,
  5444. .unreg = hist_unregister_trigger,
  5445. .unreg_all = hist_unreg_all,
  5446. .get_trigger_ops = event_hist_get_trigger_ops,
  5447. .set_filter = set_trigger_filter,
  5448. };
  5449. __init int register_trigger_hist_cmd(void)
  5450. {
  5451. int ret;
  5452. ret = register_event_command(&trigger_hist_cmd);
  5453. WARN_ON(ret < 0);
  5454. return ret;
  5455. }
  5456. static void
  5457. hist_enable_trigger(struct event_trigger_data *data,
  5458. struct trace_buffer *buffer, void *rec,
  5459. struct ring_buffer_event *event)
  5460. {
  5461. struct enable_trigger_data *enable_data = data->private_data;
  5462. struct event_trigger_data *test;
  5463. list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
  5464. lockdep_is_held(&event_mutex)) {
  5465. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  5466. if (enable_data->enable)
  5467. test->paused = false;
  5468. else
  5469. test->paused = true;
  5470. }
  5471. }
  5472. }
  5473. static void
  5474. hist_enable_count_trigger(struct event_trigger_data *data,
  5475. struct trace_buffer *buffer, void *rec,
  5476. struct ring_buffer_event *event)
  5477. {
  5478. if (!data->count)
  5479. return;
  5480. if (data->count != -1)
  5481. (data->count)--;
  5482. hist_enable_trigger(data, buffer, rec, event);
  5483. }
  5484. static struct event_trigger_ops hist_enable_trigger_ops = {
  5485. .trigger = hist_enable_trigger,
  5486. .print = event_enable_trigger_print,
  5487. .init = event_trigger_init,
  5488. .free = event_enable_trigger_free,
  5489. };
  5490. static struct event_trigger_ops hist_enable_count_trigger_ops = {
  5491. .trigger = hist_enable_count_trigger,
  5492. .print = event_enable_trigger_print,
  5493. .init = event_trigger_init,
  5494. .free = event_enable_trigger_free,
  5495. };
  5496. static struct event_trigger_ops hist_disable_trigger_ops = {
  5497. .trigger = hist_enable_trigger,
  5498. .print = event_enable_trigger_print,
  5499. .init = event_trigger_init,
  5500. .free = event_enable_trigger_free,
  5501. };
  5502. static struct event_trigger_ops hist_disable_count_trigger_ops = {
  5503. .trigger = hist_enable_count_trigger,
  5504. .print = event_enable_trigger_print,
  5505. .init = event_trigger_init,
  5506. .free = event_enable_trigger_free,
  5507. };
  5508. static struct event_trigger_ops *
  5509. hist_enable_get_trigger_ops(char *cmd, char *param)
  5510. {
  5511. struct event_trigger_ops *ops;
  5512. bool enable;
  5513. enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
  5514. if (enable)
  5515. ops = param ? &hist_enable_count_trigger_ops :
  5516. &hist_enable_trigger_ops;
  5517. else
  5518. ops = param ? &hist_disable_count_trigger_ops :
  5519. &hist_disable_trigger_ops;
  5520. return ops;
  5521. }
  5522. static void hist_enable_unreg_all(struct trace_event_file *file)
  5523. {
  5524. struct event_trigger_data *test, *n;
  5525. list_for_each_entry_safe(test, n, &file->triggers, list) {
  5526. if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
  5527. list_del_rcu(&test->list);
  5528. update_cond_flag(file);
  5529. trace_event_trigger_enable_disable(file, 0);
  5530. if (test->ops->free)
  5531. test->ops->free(test);
  5532. }
  5533. }
  5534. }
  5535. static struct event_command trigger_hist_enable_cmd = {
  5536. .name = ENABLE_HIST_STR,
  5537. .trigger_type = ETT_HIST_ENABLE,
  5538. .parse = event_enable_trigger_parse,
  5539. .reg = event_enable_register_trigger,
  5540. .unreg = event_enable_unregister_trigger,
  5541. .unreg_all = hist_enable_unreg_all,
  5542. .get_trigger_ops = hist_enable_get_trigger_ops,
  5543. .set_filter = set_trigger_filter,
  5544. };
  5545. static struct event_command trigger_hist_disable_cmd = {
  5546. .name = DISABLE_HIST_STR,
  5547. .trigger_type = ETT_HIST_ENABLE,
  5548. .parse = event_enable_trigger_parse,
  5549. .reg = event_enable_register_trigger,
  5550. .unreg = event_enable_unregister_trigger,
  5551. .unreg_all = hist_enable_unreg_all,
  5552. .get_trigger_ops = hist_enable_get_trigger_ops,
  5553. .set_filter = set_trigger_filter,
  5554. };
  5555. static __init void unregister_trigger_hist_enable_disable_cmds(void)
  5556. {
  5557. unregister_event_command(&trigger_hist_enable_cmd);
  5558. unregister_event_command(&trigger_hist_disable_cmd);
  5559. }
  5560. __init int register_trigger_hist_enable_disable_cmds(void)
  5561. {
  5562. int ret;
  5563. ret = register_event_command(&trigger_hist_enable_cmd);
  5564. if (WARN_ON(ret < 0))
  5565. return ret;
  5566. ret = register_event_command(&trigger_hist_disable_cmd);
  5567. if (WARN_ON(ret < 0))
  5568. unregister_trigger_hist_enable_disable_cmds();
  5569. return ret;
  5570. }