trace_events_hist.c 136 KB

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