trace_events_hist.c 133 KB

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