trace_events_hist.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812
  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. field = ERR_PTR(-EINVAL);
  2025. goto out;
  2026. }
  2027. }
  2028. if (strcmp(field_name, "common_timestamp") == 0) {
  2029. *flags |= HIST_FIELD_FL_TIMESTAMP;
  2030. hist_data->enable_timestamps = true;
  2031. if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
  2032. hist_data->attrs->ts_in_usecs = true;
  2033. } else if (strcmp(field_name, "cpu") == 0)
  2034. *flags |= HIST_FIELD_FL_CPU;
  2035. else {
  2036. field = trace_find_event_field(file->event_call, field_name);
  2037. if (!field || !field->size) {
  2038. field = ERR_PTR(-EINVAL);
  2039. goto out;
  2040. }
  2041. }
  2042. out:
  2043. kfree(str);
  2044. return field;
  2045. }
  2046. static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
  2047. struct hist_field *var_ref,
  2048. char *var_name)
  2049. {
  2050. struct hist_field *alias = NULL;
  2051. unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
  2052. alias = create_hist_field(hist_data, NULL, flags, var_name);
  2053. if (!alias)
  2054. return NULL;
  2055. alias->fn = var_ref->fn;
  2056. alias->operands[0] = var_ref;
  2057. if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
  2058. destroy_hist_field(alias, 0);
  2059. return NULL;
  2060. }
  2061. return alias;
  2062. }
  2063. static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
  2064. struct trace_event_file *file, char *str,
  2065. unsigned long *flags, char *var_name)
  2066. {
  2067. char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
  2068. struct ftrace_event_field *field = NULL;
  2069. struct hist_field *hist_field = NULL;
  2070. int ret = 0;
  2071. s = strchr(str, '.');
  2072. if (s) {
  2073. s = strchr(++s, '.');
  2074. if (s) {
  2075. ref_system = strsep(&str, ".");
  2076. if (!str) {
  2077. ret = -EINVAL;
  2078. goto out;
  2079. }
  2080. ref_event = strsep(&str, ".");
  2081. if (!str) {
  2082. ret = -EINVAL;
  2083. goto out;
  2084. }
  2085. ref_var = str;
  2086. }
  2087. }
  2088. s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
  2089. if (!s) {
  2090. hist_field = parse_var_ref(hist_data, ref_system, ref_event, ref_var);
  2091. if (hist_field) {
  2092. hist_data->var_refs[hist_data->n_var_refs] = hist_field;
  2093. hist_field->var_ref_idx = hist_data->n_var_refs++;
  2094. if (var_name) {
  2095. hist_field = create_alias(hist_data, hist_field, var_name);
  2096. if (!hist_field) {
  2097. ret = -ENOMEM;
  2098. goto out;
  2099. }
  2100. }
  2101. return hist_field;
  2102. }
  2103. } else
  2104. str = s;
  2105. field = parse_field(hist_data, file, str, flags);
  2106. if (IS_ERR(field)) {
  2107. ret = PTR_ERR(field);
  2108. goto out;
  2109. }
  2110. hist_field = create_hist_field(hist_data, field, *flags, var_name);
  2111. if (!hist_field) {
  2112. ret = -ENOMEM;
  2113. goto out;
  2114. }
  2115. return hist_field;
  2116. out:
  2117. return ERR_PTR(ret);
  2118. }
  2119. static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
  2120. struct trace_event_file *file,
  2121. char *str, unsigned long flags,
  2122. char *var_name, unsigned int level);
  2123. static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
  2124. struct trace_event_file *file,
  2125. char *str, unsigned long flags,
  2126. char *var_name, unsigned int level)
  2127. {
  2128. struct hist_field *operand1, *expr = NULL;
  2129. unsigned long operand_flags;
  2130. int ret = 0;
  2131. char *s;
  2132. /* we support only -(xxx) i.e. explicit parens required */
  2133. if (level > 3) {
  2134. hist_err("Too many subexpressions (3 max): ", str);
  2135. ret = -EINVAL;
  2136. goto free;
  2137. }
  2138. str++; /* skip leading '-' */
  2139. s = strchr(str, '(');
  2140. if (s)
  2141. str++;
  2142. else {
  2143. ret = -EINVAL;
  2144. goto free;
  2145. }
  2146. s = strrchr(str, ')');
  2147. if (s)
  2148. *s = '\0';
  2149. else {
  2150. ret = -EINVAL; /* no closing ')' */
  2151. goto free;
  2152. }
  2153. flags |= HIST_FIELD_FL_EXPR;
  2154. expr = create_hist_field(hist_data, NULL, flags, var_name);
  2155. if (!expr) {
  2156. ret = -ENOMEM;
  2157. goto free;
  2158. }
  2159. operand_flags = 0;
  2160. operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
  2161. if (IS_ERR(operand1)) {
  2162. ret = PTR_ERR(operand1);
  2163. goto free;
  2164. }
  2165. expr->flags |= operand1->flags &
  2166. (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
  2167. expr->fn = hist_field_unary_minus;
  2168. expr->operands[0] = operand1;
  2169. expr->operator = FIELD_OP_UNARY_MINUS;
  2170. expr->name = expr_str(expr, 0);
  2171. expr->type = kstrdup(operand1->type, GFP_KERNEL);
  2172. if (!expr->type) {
  2173. ret = -ENOMEM;
  2174. goto free;
  2175. }
  2176. return expr;
  2177. free:
  2178. destroy_hist_field(expr, 0);
  2179. return ERR_PTR(ret);
  2180. }
  2181. static int check_expr_operands(struct hist_field *operand1,
  2182. struct hist_field *operand2)
  2183. {
  2184. unsigned long operand1_flags = operand1->flags;
  2185. unsigned long operand2_flags = operand2->flags;
  2186. if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
  2187. (operand1_flags & HIST_FIELD_FL_ALIAS)) {
  2188. struct hist_field *var;
  2189. var = find_var_field(operand1->var.hist_data, operand1->name);
  2190. if (!var)
  2191. return -EINVAL;
  2192. operand1_flags = var->flags;
  2193. }
  2194. if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
  2195. (operand2_flags & HIST_FIELD_FL_ALIAS)) {
  2196. struct hist_field *var;
  2197. var = find_var_field(operand2->var.hist_data, operand2->name);
  2198. if (!var)
  2199. return -EINVAL;
  2200. operand2_flags = var->flags;
  2201. }
  2202. if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
  2203. (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
  2204. hist_err("Timestamp units in expression don't match", NULL);
  2205. return -EINVAL;
  2206. }
  2207. return 0;
  2208. }
  2209. static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
  2210. struct trace_event_file *file,
  2211. char *str, unsigned long flags,
  2212. char *var_name, unsigned int level)
  2213. {
  2214. struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
  2215. unsigned long operand_flags;
  2216. int field_op, ret = -EINVAL;
  2217. char *sep, *operand1_str;
  2218. if (level > 3) {
  2219. hist_err("Too many subexpressions (3 max): ", str);
  2220. return ERR_PTR(-EINVAL);
  2221. }
  2222. field_op = contains_operator(str);
  2223. if (field_op == FIELD_OP_NONE)
  2224. return parse_atom(hist_data, file, str, &flags, var_name);
  2225. if (field_op == FIELD_OP_UNARY_MINUS)
  2226. return parse_unary(hist_data, file, str, flags, var_name, ++level);
  2227. switch (field_op) {
  2228. case FIELD_OP_MINUS:
  2229. sep = "-";
  2230. break;
  2231. case FIELD_OP_PLUS:
  2232. sep = "+";
  2233. break;
  2234. default:
  2235. goto free;
  2236. }
  2237. operand1_str = strsep(&str, sep);
  2238. if (!operand1_str || !str)
  2239. goto free;
  2240. operand_flags = 0;
  2241. operand1 = parse_atom(hist_data, file, operand1_str,
  2242. &operand_flags, NULL);
  2243. if (IS_ERR(operand1)) {
  2244. ret = PTR_ERR(operand1);
  2245. operand1 = NULL;
  2246. goto free;
  2247. }
  2248. /* rest of string could be another expression e.g. b+c in a+b+c */
  2249. operand_flags = 0;
  2250. operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
  2251. if (IS_ERR(operand2)) {
  2252. ret = PTR_ERR(operand2);
  2253. operand2 = NULL;
  2254. goto free;
  2255. }
  2256. ret = check_expr_operands(operand1, operand2);
  2257. if (ret)
  2258. goto free;
  2259. flags |= HIST_FIELD_FL_EXPR;
  2260. flags |= operand1->flags &
  2261. (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
  2262. expr = create_hist_field(hist_data, NULL, flags, var_name);
  2263. if (!expr) {
  2264. ret = -ENOMEM;
  2265. goto free;
  2266. }
  2267. operand1->read_once = true;
  2268. operand2->read_once = true;
  2269. expr->operands[0] = operand1;
  2270. expr->operands[1] = operand2;
  2271. expr->operator = field_op;
  2272. expr->name = expr_str(expr, 0);
  2273. expr->type = kstrdup(operand1->type, GFP_KERNEL);
  2274. if (!expr->type) {
  2275. ret = -ENOMEM;
  2276. goto free;
  2277. }
  2278. switch (field_op) {
  2279. case FIELD_OP_MINUS:
  2280. expr->fn = hist_field_minus;
  2281. break;
  2282. case FIELD_OP_PLUS:
  2283. expr->fn = hist_field_plus;
  2284. break;
  2285. default:
  2286. ret = -EINVAL;
  2287. goto free;
  2288. }
  2289. return expr;
  2290. free:
  2291. destroy_hist_field(operand1, 0);
  2292. destroy_hist_field(operand2, 0);
  2293. destroy_hist_field(expr, 0);
  2294. return ERR_PTR(ret);
  2295. }
  2296. static char *find_trigger_filter(struct hist_trigger_data *hist_data,
  2297. struct trace_event_file *file)
  2298. {
  2299. struct event_trigger_data *test;
  2300. list_for_each_entry_rcu(test, &file->triggers, list) {
  2301. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  2302. if (test->private_data == hist_data)
  2303. return test->filter_str;
  2304. }
  2305. }
  2306. return NULL;
  2307. }
  2308. static struct event_command trigger_hist_cmd;
  2309. static int event_hist_trigger_func(struct event_command *cmd_ops,
  2310. struct trace_event_file *file,
  2311. char *glob, char *cmd, char *param);
  2312. static bool compatible_keys(struct hist_trigger_data *target_hist_data,
  2313. struct hist_trigger_data *hist_data,
  2314. unsigned int n_keys)
  2315. {
  2316. struct hist_field *target_hist_field, *hist_field;
  2317. unsigned int n, i, j;
  2318. if (hist_data->n_fields - hist_data->n_vals != n_keys)
  2319. return false;
  2320. i = hist_data->n_vals;
  2321. j = target_hist_data->n_vals;
  2322. for (n = 0; n < n_keys; n++) {
  2323. hist_field = hist_data->fields[i + n];
  2324. target_hist_field = target_hist_data->fields[j + n];
  2325. if (strcmp(hist_field->type, target_hist_field->type) != 0)
  2326. return false;
  2327. if (hist_field->size != target_hist_field->size)
  2328. return false;
  2329. if (hist_field->is_signed != target_hist_field->is_signed)
  2330. return false;
  2331. }
  2332. return true;
  2333. }
  2334. static struct hist_trigger_data *
  2335. find_compatible_hist(struct hist_trigger_data *target_hist_data,
  2336. struct trace_event_file *file)
  2337. {
  2338. struct hist_trigger_data *hist_data;
  2339. struct event_trigger_data *test;
  2340. unsigned int n_keys;
  2341. n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
  2342. list_for_each_entry_rcu(test, &file->triggers, list) {
  2343. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  2344. hist_data = test->private_data;
  2345. if (compatible_keys(target_hist_data, hist_data, n_keys))
  2346. return hist_data;
  2347. }
  2348. }
  2349. return NULL;
  2350. }
  2351. static struct trace_event_file *event_file(struct trace_array *tr,
  2352. char *system, char *event_name)
  2353. {
  2354. struct trace_event_file *file;
  2355. file = find_event_file(tr, system, event_name);
  2356. if (!file)
  2357. return ERR_PTR(-EINVAL);
  2358. return file;
  2359. }
  2360. static struct hist_field *
  2361. find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
  2362. char *system, char *event_name, char *field_name)
  2363. {
  2364. struct hist_field *event_var;
  2365. char *synthetic_name;
  2366. synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
  2367. if (!synthetic_name)
  2368. return ERR_PTR(-ENOMEM);
  2369. strcpy(synthetic_name, "synthetic_");
  2370. strcat(synthetic_name, field_name);
  2371. event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
  2372. kfree(synthetic_name);
  2373. return event_var;
  2374. }
  2375. /**
  2376. * create_field_var_hist - Automatically create a histogram and var for a field
  2377. * @target_hist_data: The target hist trigger
  2378. * @subsys_name: Optional subsystem name
  2379. * @event_name: Optional event name
  2380. * @field_name: The name of the field (and the resulting variable)
  2381. *
  2382. * Hist trigger actions fetch data from variables, not directly from
  2383. * events. However, for convenience, users are allowed to directly
  2384. * specify an event field in an action, which will be automatically
  2385. * converted into a variable on their behalf.
  2386. * If a user specifies a field on an event that isn't the event the
  2387. * histogram currently being defined (the target event histogram), the
  2388. * only way that can be accomplished is if a new hist trigger is
  2389. * created and the field variable defined on that.
  2390. *
  2391. * This function creates a new histogram compatible with the target
  2392. * event (meaning a histogram with the same key as the target
  2393. * histogram), and creates a variable for the specified field, but
  2394. * with 'synthetic_' prepended to the variable name in order to avoid
  2395. * collision with normal field variables.
  2396. *
  2397. * Return: The variable created for the field.
  2398. */
  2399. static struct hist_field *
  2400. create_field_var_hist(struct hist_trigger_data *target_hist_data,
  2401. char *subsys_name, char *event_name, char *field_name)
  2402. {
  2403. struct trace_array *tr = target_hist_data->event_file->tr;
  2404. struct hist_field *event_var = ERR_PTR(-EINVAL);
  2405. struct hist_trigger_data *hist_data;
  2406. unsigned int i, n, first = true;
  2407. struct field_var_hist *var_hist;
  2408. struct trace_event_file *file;
  2409. struct hist_field *key_field;
  2410. char *saved_filter;
  2411. char *cmd;
  2412. int ret;
  2413. if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
  2414. hist_err_event("onmatch: Too many field variables defined: ",
  2415. subsys_name, event_name, field_name);
  2416. return ERR_PTR(-EINVAL);
  2417. }
  2418. file = event_file(tr, subsys_name, event_name);
  2419. if (IS_ERR(file)) {
  2420. hist_err_event("onmatch: Event file not found: ",
  2421. subsys_name, event_name, field_name);
  2422. ret = PTR_ERR(file);
  2423. return ERR_PTR(ret);
  2424. }
  2425. /*
  2426. * Look for a histogram compatible with target. We'll use the
  2427. * found histogram specification to create a new matching
  2428. * histogram with our variable on it. target_hist_data is not
  2429. * yet a registered histogram so we can't use that.
  2430. */
  2431. hist_data = find_compatible_hist(target_hist_data, file);
  2432. if (!hist_data) {
  2433. hist_err_event("onmatch: Matching event histogram not found: ",
  2434. subsys_name, event_name, field_name);
  2435. return ERR_PTR(-EINVAL);
  2436. }
  2437. /* See if a synthetic field variable has already been created */
  2438. event_var = find_synthetic_field_var(target_hist_data, subsys_name,
  2439. event_name, field_name);
  2440. if (!IS_ERR_OR_NULL(event_var))
  2441. return event_var;
  2442. var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
  2443. if (!var_hist)
  2444. return ERR_PTR(-ENOMEM);
  2445. cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
  2446. if (!cmd) {
  2447. kfree(var_hist);
  2448. return ERR_PTR(-ENOMEM);
  2449. }
  2450. /* Use the same keys as the compatible histogram */
  2451. strcat(cmd, "keys=");
  2452. for_each_hist_key_field(i, hist_data) {
  2453. key_field = hist_data->fields[i];
  2454. if (!first)
  2455. strcat(cmd, ",");
  2456. strcat(cmd, key_field->field->name);
  2457. first = false;
  2458. }
  2459. /* Create the synthetic field variable specification */
  2460. strcat(cmd, ":synthetic_");
  2461. strcat(cmd, field_name);
  2462. strcat(cmd, "=");
  2463. strcat(cmd, field_name);
  2464. /* Use the same filter as the compatible histogram */
  2465. saved_filter = find_trigger_filter(hist_data, file);
  2466. if (saved_filter) {
  2467. strcat(cmd, " if ");
  2468. strcat(cmd, saved_filter);
  2469. }
  2470. var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
  2471. if (!var_hist->cmd) {
  2472. kfree(cmd);
  2473. kfree(var_hist);
  2474. return ERR_PTR(-ENOMEM);
  2475. }
  2476. /* Save the compatible histogram information */
  2477. var_hist->hist_data = hist_data;
  2478. /* Create the new histogram with our variable */
  2479. ret = event_hist_trigger_func(&trigger_hist_cmd, file,
  2480. "", "hist", cmd);
  2481. if (ret) {
  2482. kfree(cmd);
  2483. kfree(var_hist->cmd);
  2484. kfree(var_hist);
  2485. hist_err_event("onmatch: Couldn't create histogram for field: ",
  2486. subsys_name, event_name, field_name);
  2487. return ERR_PTR(ret);
  2488. }
  2489. kfree(cmd);
  2490. /* If we can't find the variable, something went wrong */
  2491. event_var = find_synthetic_field_var(target_hist_data, subsys_name,
  2492. event_name, field_name);
  2493. if (IS_ERR_OR_NULL(event_var)) {
  2494. kfree(var_hist->cmd);
  2495. kfree(var_hist);
  2496. hist_err_event("onmatch: Couldn't find synthetic variable: ",
  2497. subsys_name, event_name, field_name);
  2498. return ERR_PTR(-EINVAL);
  2499. }
  2500. n = target_hist_data->n_field_var_hists;
  2501. target_hist_data->field_var_hists[n] = var_hist;
  2502. target_hist_data->n_field_var_hists++;
  2503. return event_var;
  2504. }
  2505. static struct hist_field *
  2506. find_target_event_var(struct hist_trigger_data *hist_data,
  2507. char *subsys_name, char *event_name, char *var_name)
  2508. {
  2509. struct trace_event_file *file = hist_data->event_file;
  2510. struct hist_field *hist_field = NULL;
  2511. if (subsys_name) {
  2512. struct trace_event_call *call;
  2513. if (!event_name)
  2514. return NULL;
  2515. call = file->event_call;
  2516. if (strcmp(subsys_name, call->class->system) != 0)
  2517. return NULL;
  2518. if (strcmp(event_name, trace_event_name(call)) != 0)
  2519. return NULL;
  2520. }
  2521. hist_field = find_var_field(hist_data, var_name);
  2522. return hist_field;
  2523. }
  2524. static inline void __update_field_vars(struct tracing_map_elt *elt,
  2525. struct ring_buffer_event *rbe,
  2526. void *rec,
  2527. struct field_var **field_vars,
  2528. unsigned int n_field_vars,
  2529. unsigned int field_var_str_start)
  2530. {
  2531. struct hist_elt_data *elt_data = elt->private_data;
  2532. unsigned int i, j, var_idx;
  2533. u64 var_val;
  2534. for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
  2535. struct field_var *field_var = field_vars[i];
  2536. struct hist_field *var = field_var->var;
  2537. struct hist_field *val = field_var->val;
  2538. var_val = val->fn(val, elt, rbe, rec);
  2539. var_idx = var->var.idx;
  2540. if (val->flags & HIST_FIELD_FL_STRING) {
  2541. char *str = elt_data->field_var_str[j++];
  2542. char *val_str = (char *)(uintptr_t)var_val;
  2543. strscpy(str, val_str, STR_VAR_LEN_MAX);
  2544. var_val = (u64)(uintptr_t)str;
  2545. }
  2546. tracing_map_set_var(elt, var_idx, var_val);
  2547. }
  2548. }
  2549. static void update_field_vars(struct hist_trigger_data *hist_data,
  2550. struct tracing_map_elt *elt,
  2551. struct ring_buffer_event *rbe,
  2552. void *rec)
  2553. {
  2554. __update_field_vars(elt, rbe, rec, hist_data->field_vars,
  2555. hist_data->n_field_vars, 0);
  2556. }
  2557. static void update_max_vars(struct hist_trigger_data *hist_data,
  2558. struct tracing_map_elt *elt,
  2559. struct ring_buffer_event *rbe,
  2560. void *rec)
  2561. {
  2562. __update_field_vars(elt, rbe, rec, hist_data->max_vars,
  2563. hist_data->n_max_vars, hist_data->n_field_var_str);
  2564. }
  2565. static struct hist_field *create_var(struct hist_trigger_data *hist_data,
  2566. struct trace_event_file *file,
  2567. char *name, int size, const char *type)
  2568. {
  2569. struct hist_field *var;
  2570. int idx;
  2571. if (find_var(hist_data, file, name) && !hist_data->remove) {
  2572. var = ERR_PTR(-EINVAL);
  2573. goto out;
  2574. }
  2575. var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
  2576. if (!var) {
  2577. var = ERR_PTR(-ENOMEM);
  2578. goto out;
  2579. }
  2580. idx = tracing_map_add_var(hist_data->map);
  2581. if (idx < 0) {
  2582. kfree(var);
  2583. var = ERR_PTR(-EINVAL);
  2584. goto out;
  2585. }
  2586. var->flags = HIST_FIELD_FL_VAR;
  2587. var->var.idx = idx;
  2588. var->var.hist_data = var->hist_data = hist_data;
  2589. var->size = size;
  2590. var->var.name = kstrdup(name, GFP_KERNEL);
  2591. var->type = kstrdup(type, GFP_KERNEL);
  2592. if (!var->var.name || !var->type) {
  2593. kfree(var->var.name);
  2594. kfree(var->type);
  2595. kfree(var);
  2596. var = ERR_PTR(-ENOMEM);
  2597. }
  2598. out:
  2599. return var;
  2600. }
  2601. static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
  2602. struct trace_event_file *file,
  2603. char *field_name)
  2604. {
  2605. struct hist_field *val = NULL, *var = NULL;
  2606. unsigned long flags = HIST_FIELD_FL_VAR;
  2607. struct field_var *field_var;
  2608. int ret = 0;
  2609. if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
  2610. hist_err("Too many field variables defined: ", field_name);
  2611. ret = -EINVAL;
  2612. goto err;
  2613. }
  2614. val = parse_atom(hist_data, file, field_name, &flags, NULL);
  2615. if (IS_ERR(val)) {
  2616. hist_err("Couldn't parse field variable: ", field_name);
  2617. ret = PTR_ERR(val);
  2618. goto err;
  2619. }
  2620. var = create_var(hist_data, file, field_name, val->size, val->type);
  2621. if (IS_ERR(var)) {
  2622. hist_err("Couldn't create or find variable: ", field_name);
  2623. kfree(val);
  2624. ret = PTR_ERR(var);
  2625. goto err;
  2626. }
  2627. field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
  2628. if (!field_var) {
  2629. kfree(val);
  2630. kfree(var);
  2631. ret = -ENOMEM;
  2632. goto err;
  2633. }
  2634. field_var->var = var;
  2635. field_var->val = val;
  2636. out:
  2637. return field_var;
  2638. err:
  2639. field_var = ERR_PTR(ret);
  2640. goto out;
  2641. }
  2642. /**
  2643. * create_target_field_var - Automatically create a variable for a field
  2644. * @target_hist_data: The target hist trigger
  2645. * @subsys_name: Optional subsystem name
  2646. * @event_name: Optional event name
  2647. * @var_name: The name of the field (and the resulting variable)
  2648. *
  2649. * Hist trigger actions fetch data from variables, not directly from
  2650. * events. However, for convenience, users are allowed to directly
  2651. * specify an event field in an action, which will be automatically
  2652. * converted into a variable on their behalf.
  2653. * This function creates a field variable with the name var_name on
  2654. * the hist trigger currently being defined on the target event. If
  2655. * subsys_name and event_name are specified, this function simply
  2656. * verifies that they do in fact match the target event subsystem and
  2657. * event name.
  2658. *
  2659. * Return: The variable created for the field.
  2660. */
  2661. static struct field_var *
  2662. create_target_field_var(struct hist_trigger_data *target_hist_data,
  2663. char *subsys_name, char *event_name, char *var_name)
  2664. {
  2665. struct trace_event_file *file = target_hist_data->event_file;
  2666. if (subsys_name) {
  2667. struct trace_event_call *call;
  2668. if (!event_name)
  2669. return NULL;
  2670. call = file->event_call;
  2671. if (strcmp(subsys_name, call->class->system) != 0)
  2672. return NULL;
  2673. if (strcmp(event_name, trace_event_name(call)) != 0)
  2674. return NULL;
  2675. }
  2676. return create_field_var(target_hist_data, file, var_name);
  2677. }
  2678. static void onmax_print(struct seq_file *m,
  2679. struct hist_trigger_data *hist_data,
  2680. struct tracing_map_elt *elt,
  2681. struct action_data *data)
  2682. {
  2683. unsigned int i, save_var_idx, max_idx = data->onmax.max_var->var.idx;
  2684. seq_printf(m, "\n\tmax: %10llu", tracing_map_read_var(elt, max_idx));
  2685. for (i = 0; i < hist_data->n_max_vars; i++) {
  2686. struct hist_field *save_val = hist_data->max_vars[i]->val;
  2687. struct hist_field *save_var = hist_data->max_vars[i]->var;
  2688. u64 val;
  2689. save_var_idx = save_var->var.idx;
  2690. val = tracing_map_read_var(elt, save_var_idx);
  2691. if (save_val->flags & HIST_FIELD_FL_STRING) {
  2692. seq_printf(m, " %s: %-32s", save_var->var.name,
  2693. (char *)(uintptr_t)(val));
  2694. } else
  2695. seq_printf(m, " %s: %10llu", save_var->var.name, val);
  2696. }
  2697. }
  2698. static void onmax_save(struct hist_trigger_data *hist_data,
  2699. struct tracing_map_elt *elt, void *rec,
  2700. struct ring_buffer_event *rbe,
  2701. struct action_data *data, u64 *var_ref_vals)
  2702. {
  2703. unsigned int max_idx = data->onmax.max_var->var.idx;
  2704. unsigned int max_var_ref_idx = data->onmax.max_var_ref_idx;
  2705. u64 var_val, max_val;
  2706. var_val = var_ref_vals[max_var_ref_idx];
  2707. max_val = tracing_map_read_var(elt, max_idx);
  2708. if (var_val <= max_val)
  2709. return;
  2710. tracing_map_set_var(elt, max_idx, var_val);
  2711. update_max_vars(hist_data, elt, rbe, rec);
  2712. }
  2713. static void onmax_destroy(struct action_data *data)
  2714. {
  2715. unsigned int i;
  2716. destroy_hist_field(data->onmax.max_var, 0);
  2717. destroy_hist_field(data->onmax.var, 0);
  2718. kfree(data->onmax.var_str);
  2719. kfree(data->onmax.fn_name);
  2720. for (i = 0; i < data->n_params; i++)
  2721. kfree(data->params[i]);
  2722. kfree(data);
  2723. }
  2724. static int onmax_create(struct hist_trigger_data *hist_data,
  2725. struct action_data *data)
  2726. {
  2727. struct trace_event_file *file = hist_data->event_file;
  2728. struct hist_field *var_field, *ref_field, *max_var;
  2729. unsigned int var_ref_idx = hist_data->n_var_refs;
  2730. struct field_var *field_var;
  2731. char *onmax_var_str, *param;
  2732. unsigned long flags;
  2733. unsigned int i;
  2734. int ret = 0;
  2735. onmax_var_str = data->onmax.var_str;
  2736. if (onmax_var_str[0] != '$') {
  2737. hist_err("onmax: For onmax(x), x must be a variable: ", onmax_var_str);
  2738. return -EINVAL;
  2739. }
  2740. onmax_var_str++;
  2741. var_field = find_target_event_var(hist_data, NULL, NULL, onmax_var_str);
  2742. if (!var_field) {
  2743. hist_err("onmax: Couldn't find onmax variable: ", onmax_var_str);
  2744. return -EINVAL;
  2745. }
  2746. flags = HIST_FIELD_FL_VAR_REF;
  2747. ref_field = create_hist_field(hist_data, NULL, flags, NULL);
  2748. if (!ref_field)
  2749. return -ENOMEM;
  2750. if (init_var_ref(ref_field, var_field, NULL, NULL)) {
  2751. destroy_hist_field(ref_field, 0);
  2752. ret = -ENOMEM;
  2753. goto out;
  2754. }
  2755. hist_data->var_refs[hist_data->n_var_refs] = ref_field;
  2756. ref_field->var_ref_idx = hist_data->n_var_refs++;
  2757. data->onmax.var = ref_field;
  2758. data->fn = onmax_save;
  2759. data->onmax.max_var_ref_idx = var_ref_idx;
  2760. max_var = create_var(hist_data, file, "max", sizeof(u64), "u64");
  2761. if (IS_ERR(max_var)) {
  2762. hist_err("onmax: Couldn't create onmax variable: ", "max");
  2763. ret = PTR_ERR(max_var);
  2764. goto out;
  2765. }
  2766. data->onmax.max_var = max_var;
  2767. for (i = 0; i < data->n_params; i++) {
  2768. param = kstrdup(data->params[i], GFP_KERNEL);
  2769. if (!param) {
  2770. ret = -ENOMEM;
  2771. goto out;
  2772. }
  2773. field_var = create_target_field_var(hist_data, NULL, NULL, param);
  2774. if (IS_ERR(field_var)) {
  2775. hist_err("onmax: Couldn't create field variable: ", param);
  2776. ret = PTR_ERR(field_var);
  2777. kfree(param);
  2778. goto out;
  2779. }
  2780. hist_data->max_vars[hist_data->n_max_vars++] = field_var;
  2781. if (field_var->val->flags & HIST_FIELD_FL_STRING)
  2782. hist_data->n_max_var_str++;
  2783. kfree(param);
  2784. }
  2785. out:
  2786. return ret;
  2787. }
  2788. static int parse_action_params(char *params, struct action_data *data)
  2789. {
  2790. char *param, *saved_param;
  2791. int ret = 0;
  2792. while (params) {
  2793. if (data->n_params >= SYNTH_FIELDS_MAX)
  2794. goto out;
  2795. param = strsep(&params, ",");
  2796. if (!param) {
  2797. ret = -EINVAL;
  2798. goto out;
  2799. }
  2800. param = strstrip(param);
  2801. if (strlen(param) < 2) {
  2802. hist_err("Invalid action param: ", param);
  2803. ret = -EINVAL;
  2804. goto out;
  2805. }
  2806. saved_param = kstrdup(param, GFP_KERNEL);
  2807. if (!saved_param) {
  2808. ret = -ENOMEM;
  2809. goto out;
  2810. }
  2811. data->params[data->n_params++] = saved_param;
  2812. }
  2813. out:
  2814. return ret;
  2815. }
  2816. static struct action_data *onmax_parse(char *str)
  2817. {
  2818. char *onmax_fn_name, *onmax_var_str;
  2819. struct action_data *data;
  2820. int ret = -EINVAL;
  2821. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2822. if (!data)
  2823. return ERR_PTR(-ENOMEM);
  2824. onmax_var_str = strsep(&str, ")");
  2825. if (!onmax_var_str || !str) {
  2826. ret = -EINVAL;
  2827. goto free;
  2828. }
  2829. data->onmax.var_str = kstrdup(onmax_var_str, GFP_KERNEL);
  2830. if (!data->onmax.var_str) {
  2831. ret = -ENOMEM;
  2832. goto free;
  2833. }
  2834. strsep(&str, ".");
  2835. if (!str)
  2836. goto free;
  2837. onmax_fn_name = strsep(&str, "(");
  2838. if (!onmax_fn_name || !str)
  2839. goto free;
  2840. if (strncmp(onmax_fn_name, "save", strlen("save")) == 0) {
  2841. char *params = strsep(&str, ")");
  2842. if (!params) {
  2843. ret = -EINVAL;
  2844. goto free;
  2845. }
  2846. ret = parse_action_params(params, data);
  2847. if (ret)
  2848. goto free;
  2849. } else
  2850. goto free;
  2851. data->onmax.fn_name = kstrdup(onmax_fn_name, GFP_KERNEL);
  2852. if (!data->onmax.fn_name) {
  2853. ret = -ENOMEM;
  2854. goto free;
  2855. }
  2856. out:
  2857. return data;
  2858. free:
  2859. onmax_destroy(data);
  2860. data = ERR_PTR(ret);
  2861. goto out;
  2862. }
  2863. static void onmatch_destroy(struct action_data *data)
  2864. {
  2865. unsigned int i;
  2866. mutex_lock(&synth_event_mutex);
  2867. kfree(data->onmatch.match_event);
  2868. kfree(data->onmatch.match_event_system);
  2869. kfree(data->onmatch.synth_event_name);
  2870. for (i = 0; i < data->n_params; i++)
  2871. kfree(data->params[i]);
  2872. if (data->onmatch.synth_event)
  2873. data->onmatch.synth_event->ref--;
  2874. kfree(data);
  2875. mutex_unlock(&synth_event_mutex);
  2876. }
  2877. static void destroy_field_var(struct field_var *field_var)
  2878. {
  2879. if (!field_var)
  2880. return;
  2881. destroy_hist_field(field_var->var, 0);
  2882. destroy_hist_field(field_var->val, 0);
  2883. kfree(field_var);
  2884. }
  2885. static void destroy_field_vars(struct hist_trigger_data *hist_data)
  2886. {
  2887. unsigned int i;
  2888. for (i = 0; i < hist_data->n_field_vars; i++)
  2889. destroy_field_var(hist_data->field_vars[i]);
  2890. }
  2891. static void save_field_var(struct hist_trigger_data *hist_data,
  2892. struct field_var *field_var)
  2893. {
  2894. hist_data->field_vars[hist_data->n_field_vars++] = field_var;
  2895. if (field_var->val->flags & HIST_FIELD_FL_STRING)
  2896. hist_data->n_field_var_str++;
  2897. }
  2898. static void destroy_synth_var_refs(struct hist_trigger_data *hist_data)
  2899. {
  2900. unsigned int i;
  2901. for (i = 0; i < hist_data->n_synth_var_refs; i++)
  2902. destroy_hist_field(hist_data->synth_var_refs[i], 0);
  2903. }
  2904. static void save_synth_var_ref(struct hist_trigger_data *hist_data,
  2905. struct hist_field *var_ref)
  2906. {
  2907. hist_data->synth_var_refs[hist_data->n_synth_var_refs++] = var_ref;
  2908. hist_data->var_refs[hist_data->n_var_refs] = var_ref;
  2909. var_ref->var_ref_idx = hist_data->n_var_refs++;
  2910. }
  2911. static int check_synth_field(struct synth_event *event,
  2912. struct hist_field *hist_field,
  2913. unsigned int field_pos)
  2914. {
  2915. struct synth_field *field;
  2916. if (field_pos >= event->n_fields)
  2917. return -EINVAL;
  2918. field = event->fields[field_pos];
  2919. if (strcmp(field->type, hist_field->type) != 0)
  2920. return -EINVAL;
  2921. return 0;
  2922. }
  2923. static struct hist_field *
  2924. onmatch_find_var(struct hist_trigger_data *hist_data, struct action_data *data,
  2925. char *system, char *event, char *var)
  2926. {
  2927. struct hist_field *hist_field;
  2928. var++; /* skip '$' */
  2929. hist_field = find_target_event_var(hist_data, system, event, var);
  2930. if (!hist_field) {
  2931. if (!system) {
  2932. system = data->onmatch.match_event_system;
  2933. event = data->onmatch.match_event;
  2934. }
  2935. hist_field = find_event_var(hist_data, system, event, var);
  2936. }
  2937. if (!hist_field)
  2938. hist_err_event("onmatch: Couldn't find onmatch param: $", system, event, var);
  2939. return hist_field;
  2940. }
  2941. static struct hist_field *
  2942. onmatch_create_field_var(struct hist_trigger_data *hist_data,
  2943. struct action_data *data, char *system,
  2944. char *event, char *var)
  2945. {
  2946. struct hist_field *hist_field = NULL;
  2947. struct field_var *field_var;
  2948. /*
  2949. * First try to create a field var on the target event (the
  2950. * currently being defined). This will create a variable for
  2951. * unqualified fields on the target event, or if qualified,
  2952. * target fields that have qualified names matching the target.
  2953. */
  2954. field_var = create_target_field_var(hist_data, system, event, var);
  2955. if (field_var && !IS_ERR(field_var)) {
  2956. save_field_var(hist_data, field_var);
  2957. hist_field = field_var->var;
  2958. } else {
  2959. field_var = NULL;
  2960. /*
  2961. * If no explicit system.event is specfied, default to
  2962. * looking for fields on the onmatch(system.event.xxx)
  2963. * event.
  2964. */
  2965. if (!system) {
  2966. system = data->onmatch.match_event_system;
  2967. event = data->onmatch.match_event;
  2968. }
  2969. /*
  2970. * At this point, we're looking at a field on another
  2971. * event. Because we can't modify a hist trigger on
  2972. * another event to add a variable for a field, we need
  2973. * to create a new trigger on that event and create the
  2974. * variable at the same time.
  2975. */
  2976. hist_field = create_field_var_hist(hist_data, system, event, var);
  2977. if (IS_ERR(hist_field))
  2978. goto free;
  2979. }
  2980. out:
  2981. return hist_field;
  2982. free:
  2983. destroy_field_var(field_var);
  2984. hist_field = NULL;
  2985. goto out;
  2986. }
  2987. static int onmatch_create(struct hist_trigger_data *hist_data,
  2988. struct trace_event_file *file,
  2989. struct action_data *data)
  2990. {
  2991. char *event_name, *param, *system = NULL;
  2992. struct hist_field *hist_field, *var_ref;
  2993. unsigned int i, var_ref_idx;
  2994. unsigned int field_pos = 0;
  2995. struct synth_event *event;
  2996. int ret = 0;
  2997. mutex_lock(&synth_event_mutex);
  2998. event = find_synth_event(data->onmatch.synth_event_name);
  2999. if (!event) {
  3000. hist_err("onmatch: Couldn't find synthetic event: ", data->onmatch.synth_event_name);
  3001. mutex_unlock(&synth_event_mutex);
  3002. return -EINVAL;
  3003. }
  3004. event->ref++;
  3005. mutex_unlock(&synth_event_mutex);
  3006. var_ref_idx = hist_data->n_var_refs;
  3007. for (i = 0; i < data->n_params; i++) {
  3008. char *p;
  3009. p = param = kstrdup(data->params[i], GFP_KERNEL);
  3010. if (!param) {
  3011. ret = -ENOMEM;
  3012. goto err;
  3013. }
  3014. system = strsep(&param, ".");
  3015. if (!param) {
  3016. param = (char *)system;
  3017. system = event_name = NULL;
  3018. } else {
  3019. event_name = strsep(&param, ".");
  3020. if (!param) {
  3021. kfree(p);
  3022. ret = -EINVAL;
  3023. goto err;
  3024. }
  3025. }
  3026. if (param[0] == '$')
  3027. hist_field = onmatch_find_var(hist_data, data, system,
  3028. event_name, param);
  3029. else
  3030. hist_field = onmatch_create_field_var(hist_data, data,
  3031. system,
  3032. event_name,
  3033. param);
  3034. if (!hist_field) {
  3035. kfree(p);
  3036. ret = -EINVAL;
  3037. goto err;
  3038. }
  3039. if (check_synth_field(event, hist_field, field_pos) == 0) {
  3040. var_ref = create_var_ref(hist_field, system, event_name);
  3041. if (!var_ref) {
  3042. kfree(p);
  3043. ret = -ENOMEM;
  3044. goto err;
  3045. }
  3046. save_synth_var_ref(hist_data, var_ref);
  3047. field_pos++;
  3048. kfree(p);
  3049. continue;
  3050. }
  3051. hist_err_event("onmatch: Param type doesn't match synthetic event field type: ",
  3052. system, event_name, param);
  3053. kfree(p);
  3054. ret = -EINVAL;
  3055. goto err;
  3056. }
  3057. if (field_pos != event->n_fields) {
  3058. hist_err("onmatch: Param count doesn't match synthetic event field count: ", event->name);
  3059. ret = -EINVAL;
  3060. goto err;
  3061. }
  3062. data->fn = action_trace;
  3063. data->onmatch.synth_event = event;
  3064. data->onmatch.var_ref_idx = var_ref_idx;
  3065. out:
  3066. return ret;
  3067. err:
  3068. mutex_lock(&synth_event_mutex);
  3069. event->ref--;
  3070. mutex_unlock(&synth_event_mutex);
  3071. goto out;
  3072. }
  3073. static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
  3074. {
  3075. char *match_event, *match_event_system;
  3076. char *synth_event_name, *params;
  3077. struct action_data *data;
  3078. int ret = -EINVAL;
  3079. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3080. if (!data)
  3081. return ERR_PTR(-ENOMEM);
  3082. match_event = strsep(&str, ")");
  3083. if (!match_event || !str) {
  3084. hist_err("onmatch: Missing closing paren: ", match_event);
  3085. goto free;
  3086. }
  3087. match_event_system = strsep(&match_event, ".");
  3088. if (!match_event) {
  3089. hist_err("onmatch: Missing subsystem for match event: ", match_event_system);
  3090. goto free;
  3091. }
  3092. if (IS_ERR(event_file(tr, match_event_system, match_event))) {
  3093. hist_err_event("onmatch: Invalid subsystem or event name: ",
  3094. match_event_system, match_event, NULL);
  3095. goto free;
  3096. }
  3097. data->onmatch.match_event = kstrdup(match_event, GFP_KERNEL);
  3098. if (!data->onmatch.match_event) {
  3099. ret = -ENOMEM;
  3100. goto free;
  3101. }
  3102. data->onmatch.match_event_system = kstrdup(match_event_system, GFP_KERNEL);
  3103. if (!data->onmatch.match_event_system) {
  3104. ret = -ENOMEM;
  3105. goto free;
  3106. }
  3107. strsep(&str, ".");
  3108. if (!str) {
  3109. hist_err("onmatch: Missing . after onmatch(): ", str);
  3110. goto free;
  3111. }
  3112. synth_event_name = strsep(&str, "(");
  3113. if (!synth_event_name || !str) {
  3114. hist_err("onmatch: Missing opening paramlist paren: ", synth_event_name);
  3115. goto free;
  3116. }
  3117. data->onmatch.synth_event_name = kstrdup(synth_event_name, GFP_KERNEL);
  3118. if (!data->onmatch.synth_event_name) {
  3119. ret = -ENOMEM;
  3120. goto free;
  3121. }
  3122. params = strsep(&str, ")");
  3123. if (!params || !str || (str && strlen(str))) {
  3124. hist_err("onmatch: Missing closing paramlist paren: ", params);
  3125. goto free;
  3126. }
  3127. ret = parse_action_params(params, data);
  3128. if (ret)
  3129. goto free;
  3130. out:
  3131. return data;
  3132. free:
  3133. onmatch_destroy(data);
  3134. data = ERR_PTR(ret);
  3135. goto out;
  3136. }
  3137. static int create_hitcount_val(struct hist_trigger_data *hist_data)
  3138. {
  3139. hist_data->fields[HITCOUNT_IDX] =
  3140. create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
  3141. if (!hist_data->fields[HITCOUNT_IDX])
  3142. return -ENOMEM;
  3143. hist_data->n_vals++;
  3144. hist_data->n_fields++;
  3145. if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
  3146. return -EINVAL;
  3147. return 0;
  3148. }
  3149. static int __create_val_field(struct hist_trigger_data *hist_data,
  3150. unsigned int val_idx,
  3151. struct trace_event_file *file,
  3152. char *var_name, char *field_str,
  3153. unsigned long flags)
  3154. {
  3155. struct hist_field *hist_field;
  3156. int ret = 0;
  3157. hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
  3158. if (IS_ERR(hist_field)) {
  3159. ret = PTR_ERR(hist_field);
  3160. goto out;
  3161. }
  3162. hist_data->fields[val_idx] = hist_field;
  3163. ++hist_data->n_vals;
  3164. ++hist_data->n_fields;
  3165. if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
  3166. ret = -EINVAL;
  3167. out:
  3168. return ret;
  3169. }
  3170. static int create_val_field(struct hist_trigger_data *hist_data,
  3171. unsigned int val_idx,
  3172. struct trace_event_file *file,
  3173. char *field_str)
  3174. {
  3175. if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
  3176. return -EINVAL;
  3177. return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
  3178. }
  3179. static int create_var_field(struct hist_trigger_data *hist_data,
  3180. unsigned int val_idx,
  3181. struct trace_event_file *file,
  3182. char *var_name, char *expr_str)
  3183. {
  3184. unsigned long flags = 0;
  3185. if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
  3186. return -EINVAL;
  3187. if (find_var(hist_data, file, var_name) && !hist_data->remove) {
  3188. hist_err("Variable already defined: ", var_name);
  3189. return -EINVAL;
  3190. }
  3191. flags |= HIST_FIELD_FL_VAR;
  3192. hist_data->n_vars++;
  3193. if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
  3194. return -EINVAL;
  3195. return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
  3196. }
  3197. static int create_val_fields(struct hist_trigger_data *hist_data,
  3198. struct trace_event_file *file)
  3199. {
  3200. char *fields_str, *field_str;
  3201. unsigned int i, j = 1;
  3202. int ret;
  3203. ret = create_hitcount_val(hist_data);
  3204. if (ret)
  3205. goto out;
  3206. fields_str = hist_data->attrs->vals_str;
  3207. if (!fields_str)
  3208. goto out;
  3209. strsep(&fields_str, "=");
  3210. if (!fields_str)
  3211. goto out;
  3212. for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
  3213. j < TRACING_MAP_VALS_MAX; i++) {
  3214. field_str = strsep(&fields_str, ",");
  3215. if (!field_str)
  3216. break;
  3217. if (strcmp(field_str, "hitcount") == 0)
  3218. continue;
  3219. ret = create_val_field(hist_data, j++, file, field_str);
  3220. if (ret)
  3221. goto out;
  3222. }
  3223. if (fields_str && (strcmp(fields_str, "hitcount") != 0))
  3224. ret = -EINVAL;
  3225. out:
  3226. return ret;
  3227. }
  3228. static int create_key_field(struct hist_trigger_data *hist_data,
  3229. unsigned int key_idx,
  3230. unsigned int key_offset,
  3231. struct trace_event_file *file,
  3232. char *field_str)
  3233. {
  3234. struct hist_field *hist_field = NULL;
  3235. unsigned long flags = 0;
  3236. unsigned int key_size;
  3237. int ret = 0;
  3238. if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
  3239. return -EINVAL;
  3240. flags |= HIST_FIELD_FL_KEY;
  3241. if (strcmp(field_str, "stacktrace") == 0) {
  3242. flags |= HIST_FIELD_FL_STACKTRACE;
  3243. key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
  3244. hist_field = create_hist_field(hist_data, NULL, flags, NULL);
  3245. } else {
  3246. hist_field = parse_expr(hist_data, file, field_str, flags,
  3247. NULL, 0);
  3248. if (IS_ERR(hist_field)) {
  3249. ret = PTR_ERR(hist_field);
  3250. goto out;
  3251. }
  3252. if (hist_field->flags & HIST_FIELD_FL_VAR_REF) {
  3253. hist_err("Using variable references as keys not supported: ", field_str);
  3254. destroy_hist_field(hist_field, 0);
  3255. ret = -EINVAL;
  3256. goto out;
  3257. }
  3258. key_size = hist_field->size;
  3259. }
  3260. hist_data->fields[key_idx] = hist_field;
  3261. key_size = ALIGN(key_size, sizeof(u64));
  3262. hist_data->fields[key_idx]->size = key_size;
  3263. hist_data->fields[key_idx]->offset = key_offset;
  3264. hist_data->key_size += key_size;
  3265. if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
  3266. ret = -EINVAL;
  3267. goto out;
  3268. }
  3269. hist_data->n_keys++;
  3270. hist_data->n_fields++;
  3271. if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
  3272. return -EINVAL;
  3273. ret = key_size;
  3274. out:
  3275. return ret;
  3276. }
  3277. static int create_key_fields(struct hist_trigger_data *hist_data,
  3278. struct trace_event_file *file)
  3279. {
  3280. unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
  3281. char *fields_str, *field_str;
  3282. int ret = -EINVAL;
  3283. fields_str = hist_data->attrs->keys_str;
  3284. if (!fields_str)
  3285. goto out;
  3286. strsep(&fields_str, "=");
  3287. if (!fields_str)
  3288. goto out;
  3289. for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
  3290. field_str = strsep(&fields_str, ",");
  3291. if (!field_str)
  3292. break;
  3293. ret = create_key_field(hist_data, i, key_offset,
  3294. file, field_str);
  3295. if (ret < 0)
  3296. goto out;
  3297. key_offset += ret;
  3298. }
  3299. if (fields_str) {
  3300. ret = -EINVAL;
  3301. goto out;
  3302. }
  3303. ret = 0;
  3304. out:
  3305. return ret;
  3306. }
  3307. static int create_var_fields(struct hist_trigger_data *hist_data,
  3308. struct trace_event_file *file)
  3309. {
  3310. unsigned int i, j = hist_data->n_vals;
  3311. int ret = 0;
  3312. unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
  3313. for (i = 0; i < n_vars; i++) {
  3314. char *var_name = hist_data->attrs->var_defs.name[i];
  3315. char *expr = hist_data->attrs->var_defs.expr[i];
  3316. ret = create_var_field(hist_data, j++, file, var_name, expr);
  3317. if (ret)
  3318. goto out;
  3319. }
  3320. out:
  3321. return ret;
  3322. }
  3323. static void free_var_defs(struct hist_trigger_data *hist_data)
  3324. {
  3325. unsigned int i;
  3326. for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
  3327. kfree(hist_data->attrs->var_defs.name[i]);
  3328. kfree(hist_data->attrs->var_defs.expr[i]);
  3329. }
  3330. hist_data->attrs->var_defs.n_vars = 0;
  3331. }
  3332. static int parse_var_defs(struct hist_trigger_data *hist_data)
  3333. {
  3334. char *s, *str, *var_name, *field_str;
  3335. unsigned int i, j, n_vars = 0;
  3336. int ret = 0;
  3337. for (i = 0; i < hist_data->attrs->n_assignments; i++) {
  3338. str = hist_data->attrs->assignment_str[i];
  3339. for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
  3340. field_str = strsep(&str, ",");
  3341. if (!field_str)
  3342. break;
  3343. var_name = strsep(&field_str, "=");
  3344. if (!var_name || !field_str) {
  3345. hist_err("Malformed assignment: ", var_name);
  3346. ret = -EINVAL;
  3347. goto free;
  3348. }
  3349. if (n_vars == TRACING_MAP_VARS_MAX) {
  3350. hist_err("Too many variables defined: ", var_name);
  3351. ret = -EINVAL;
  3352. goto free;
  3353. }
  3354. s = kstrdup(var_name, GFP_KERNEL);
  3355. if (!s) {
  3356. ret = -ENOMEM;
  3357. goto free;
  3358. }
  3359. hist_data->attrs->var_defs.name[n_vars] = s;
  3360. s = kstrdup(field_str, GFP_KERNEL);
  3361. if (!s) {
  3362. kfree(hist_data->attrs->var_defs.name[n_vars]);
  3363. ret = -ENOMEM;
  3364. goto free;
  3365. }
  3366. hist_data->attrs->var_defs.expr[n_vars++] = s;
  3367. hist_data->attrs->var_defs.n_vars = n_vars;
  3368. }
  3369. }
  3370. return ret;
  3371. free:
  3372. free_var_defs(hist_data);
  3373. return ret;
  3374. }
  3375. static int create_hist_fields(struct hist_trigger_data *hist_data,
  3376. struct trace_event_file *file)
  3377. {
  3378. int ret;
  3379. ret = parse_var_defs(hist_data);
  3380. if (ret)
  3381. goto out;
  3382. ret = create_val_fields(hist_data, file);
  3383. if (ret)
  3384. goto out;
  3385. ret = create_var_fields(hist_data, file);
  3386. if (ret)
  3387. goto out;
  3388. ret = create_key_fields(hist_data, file);
  3389. if (ret)
  3390. goto out;
  3391. out:
  3392. free_var_defs(hist_data);
  3393. return ret;
  3394. }
  3395. static int is_descending(const char *str)
  3396. {
  3397. if (!str)
  3398. return 0;
  3399. if (strcmp(str, "descending") == 0)
  3400. return 1;
  3401. if (strcmp(str, "ascending") == 0)
  3402. return 0;
  3403. return -EINVAL;
  3404. }
  3405. static int create_sort_keys(struct hist_trigger_data *hist_data)
  3406. {
  3407. char *fields_str = hist_data->attrs->sort_key_str;
  3408. struct tracing_map_sort_key *sort_key;
  3409. int descending, ret = 0;
  3410. unsigned int i, j, k;
  3411. hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
  3412. if (!fields_str)
  3413. goto out;
  3414. strsep(&fields_str, "=");
  3415. if (!fields_str) {
  3416. ret = -EINVAL;
  3417. goto out;
  3418. }
  3419. for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
  3420. struct hist_field *hist_field;
  3421. char *field_str, *field_name;
  3422. const char *test_name;
  3423. sort_key = &hist_data->sort_keys[i];
  3424. field_str = strsep(&fields_str, ",");
  3425. if (!field_str) {
  3426. if (i == 0)
  3427. ret = -EINVAL;
  3428. break;
  3429. }
  3430. if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
  3431. ret = -EINVAL;
  3432. break;
  3433. }
  3434. field_name = strsep(&field_str, ".");
  3435. if (!field_name) {
  3436. ret = -EINVAL;
  3437. break;
  3438. }
  3439. if (strcmp(field_name, "hitcount") == 0) {
  3440. descending = is_descending(field_str);
  3441. if (descending < 0) {
  3442. ret = descending;
  3443. break;
  3444. }
  3445. sort_key->descending = descending;
  3446. continue;
  3447. }
  3448. for (j = 1, k = 1; j < hist_data->n_fields; j++) {
  3449. unsigned int idx;
  3450. hist_field = hist_data->fields[j];
  3451. if (hist_field->flags & HIST_FIELD_FL_VAR)
  3452. continue;
  3453. idx = k++;
  3454. test_name = hist_field_name(hist_field, 0);
  3455. if (strcmp(field_name, test_name) == 0) {
  3456. sort_key->field_idx = idx;
  3457. descending = is_descending(field_str);
  3458. if (descending < 0) {
  3459. ret = descending;
  3460. goto out;
  3461. }
  3462. sort_key->descending = descending;
  3463. break;
  3464. }
  3465. }
  3466. if (j == hist_data->n_fields) {
  3467. ret = -EINVAL;
  3468. break;
  3469. }
  3470. }
  3471. hist_data->n_sort_keys = i;
  3472. out:
  3473. return ret;
  3474. }
  3475. static void destroy_actions(struct hist_trigger_data *hist_data)
  3476. {
  3477. unsigned int i;
  3478. for (i = 0; i < hist_data->n_actions; i++) {
  3479. struct action_data *data = hist_data->actions[i];
  3480. if (data->fn == action_trace)
  3481. onmatch_destroy(data);
  3482. else if (data->fn == onmax_save)
  3483. onmax_destroy(data);
  3484. else
  3485. kfree(data);
  3486. }
  3487. }
  3488. static int parse_actions(struct hist_trigger_data *hist_data)
  3489. {
  3490. struct trace_array *tr = hist_data->event_file->tr;
  3491. struct action_data *data;
  3492. unsigned int i;
  3493. int ret = 0;
  3494. char *str;
  3495. for (i = 0; i < hist_data->attrs->n_actions; i++) {
  3496. str = hist_data->attrs->action_str[i];
  3497. if (strncmp(str, "onmatch(", strlen("onmatch(")) == 0) {
  3498. char *action_str = str + strlen("onmatch(");
  3499. data = onmatch_parse(tr, action_str);
  3500. if (IS_ERR(data)) {
  3501. ret = PTR_ERR(data);
  3502. break;
  3503. }
  3504. data->fn = action_trace;
  3505. } else if (strncmp(str, "onmax(", strlen("onmax(")) == 0) {
  3506. char *action_str = str + strlen("onmax(");
  3507. data = onmax_parse(action_str);
  3508. if (IS_ERR(data)) {
  3509. ret = PTR_ERR(data);
  3510. break;
  3511. }
  3512. data->fn = onmax_save;
  3513. } else {
  3514. ret = -EINVAL;
  3515. break;
  3516. }
  3517. hist_data->actions[hist_data->n_actions++] = data;
  3518. }
  3519. return ret;
  3520. }
  3521. static int create_actions(struct hist_trigger_data *hist_data,
  3522. struct trace_event_file *file)
  3523. {
  3524. struct action_data *data;
  3525. unsigned int i;
  3526. int ret = 0;
  3527. for (i = 0; i < hist_data->attrs->n_actions; i++) {
  3528. data = hist_data->actions[i];
  3529. if (data->fn == action_trace) {
  3530. ret = onmatch_create(hist_data, file, data);
  3531. if (ret)
  3532. return ret;
  3533. } else if (data->fn == onmax_save) {
  3534. ret = onmax_create(hist_data, data);
  3535. if (ret)
  3536. return ret;
  3537. }
  3538. }
  3539. return ret;
  3540. }
  3541. static void print_actions(struct seq_file *m,
  3542. struct hist_trigger_data *hist_data,
  3543. struct tracing_map_elt *elt)
  3544. {
  3545. unsigned int i;
  3546. for (i = 0; i < hist_data->n_actions; i++) {
  3547. struct action_data *data = hist_data->actions[i];
  3548. if (data->fn == onmax_save)
  3549. onmax_print(m, hist_data, elt, data);
  3550. }
  3551. }
  3552. static void print_onmax_spec(struct seq_file *m,
  3553. struct hist_trigger_data *hist_data,
  3554. struct action_data *data)
  3555. {
  3556. unsigned int i;
  3557. seq_puts(m, ":onmax(");
  3558. seq_printf(m, "%s", data->onmax.var_str);
  3559. seq_printf(m, ").%s(", data->onmax.fn_name);
  3560. for (i = 0; i < hist_data->n_max_vars; i++) {
  3561. seq_printf(m, "%s", hist_data->max_vars[i]->var->var.name);
  3562. if (i < hist_data->n_max_vars - 1)
  3563. seq_puts(m, ",");
  3564. }
  3565. seq_puts(m, ")");
  3566. }
  3567. static void print_onmatch_spec(struct seq_file *m,
  3568. struct hist_trigger_data *hist_data,
  3569. struct action_data *data)
  3570. {
  3571. unsigned int i;
  3572. seq_printf(m, ":onmatch(%s.%s).", data->onmatch.match_event_system,
  3573. data->onmatch.match_event);
  3574. seq_printf(m, "%s(", data->onmatch.synth_event->name);
  3575. for (i = 0; i < data->n_params; i++) {
  3576. if (i)
  3577. seq_puts(m, ",");
  3578. seq_printf(m, "%s", data->params[i]);
  3579. }
  3580. seq_puts(m, ")");
  3581. }
  3582. static bool actions_match(struct hist_trigger_data *hist_data,
  3583. struct hist_trigger_data *hist_data_test)
  3584. {
  3585. unsigned int i, j;
  3586. if (hist_data->n_actions != hist_data_test->n_actions)
  3587. return false;
  3588. for (i = 0; i < hist_data->n_actions; i++) {
  3589. struct action_data *data = hist_data->actions[i];
  3590. struct action_data *data_test = hist_data_test->actions[i];
  3591. if (data->fn != data_test->fn)
  3592. return false;
  3593. if (data->n_params != data_test->n_params)
  3594. return false;
  3595. for (j = 0; j < data->n_params; j++) {
  3596. if (strcmp(data->params[j], data_test->params[j]) != 0)
  3597. return false;
  3598. }
  3599. if (data->fn == action_trace) {
  3600. if (strcmp(data->onmatch.synth_event_name,
  3601. data_test->onmatch.synth_event_name) != 0)
  3602. return false;
  3603. if (strcmp(data->onmatch.match_event_system,
  3604. data_test->onmatch.match_event_system) != 0)
  3605. return false;
  3606. if (strcmp(data->onmatch.match_event,
  3607. data_test->onmatch.match_event) != 0)
  3608. return false;
  3609. } else if (data->fn == onmax_save) {
  3610. if (strcmp(data->onmax.var_str,
  3611. data_test->onmax.var_str) != 0)
  3612. return false;
  3613. if (strcmp(data->onmax.fn_name,
  3614. data_test->onmax.fn_name) != 0)
  3615. return false;
  3616. }
  3617. }
  3618. return true;
  3619. }
  3620. static void print_actions_spec(struct seq_file *m,
  3621. struct hist_trigger_data *hist_data)
  3622. {
  3623. unsigned int i;
  3624. for (i = 0; i < hist_data->n_actions; i++) {
  3625. struct action_data *data = hist_data->actions[i];
  3626. if (data->fn == action_trace)
  3627. print_onmatch_spec(m, hist_data, data);
  3628. else if (data->fn == onmax_save)
  3629. print_onmax_spec(m, hist_data, data);
  3630. }
  3631. }
  3632. static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
  3633. {
  3634. unsigned int i;
  3635. for (i = 0; i < hist_data->n_field_var_hists; i++) {
  3636. kfree(hist_data->field_var_hists[i]->cmd);
  3637. kfree(hist_data->field_var_hists[i]);
  3638. }
  3639. }
  3640. static void destroy_hist_data(struct hist_trigger_data *hist_data)
  3641. {
  3642. if (!hist_data)
  3643. return;
  3644. destroy_hist_trigger_attrs(hist_data->attrs);
  3645. destroy_hist_fields(hist_data);
  3646. tracing_map_destroy(hist_data->map);
  3647. destroy_actions(hist_data);
  3648. destroy_field_vars(hist_data);
  3649. destroy_field_var_hists(hist_data);
  3650. destroy_synth_var_refs(hist_data);
  3651. kfree(hist_data);
  3652. }
  3653. static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
  3654. {
  3655. struct tracing_map *map = hist_data->map;
  3656. struct ftrace_event_field *field;
  3657. struct hist_field *hist_field;
  3658. int i, idx = 0;
  3659. for_each_hist_field(i, hist_data) {
  3660. hist_field = hist_data->fields[i];
  3661. if (hist_field->flags & HIST_FIELD_FL_KEY) {
  3662. tracing_map_cmp_fn_t cmp_fn;
  3663. field = hist_field->field;
  3664. if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
  3665. cmp_fn = tracing_map_cmp_none;
  3666. else if (!field)
  3667. cmp_fn = tracing_map_cmp_num(hist_field->size,
  3668. hist_field->is_signed);
  3669. else if (is_string_field(field))
  3670. cmp_fn = tracing_map_cmp_string;
  3671. else
  3672. cmp_fn = tracing_map_cmp_num(field->size,
  3673. field->is_signed);
  3674. idx = tracing_map_add_key_field(map,
  3675. hist_field->offset,
  3676. cmp_fn);
  3677. } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
  3678. idx = tracing_map_add_sum_field(map);
  3679. if (idx < 0)
  3680. return idx;
  3681. if (hist_field->flags & HIST_FIELD_FL_VAR) {
  3682. idx = tracing_map_add_var(map);
  3683. if (idx < 0)
  3684. return idx;
  3685. hist_field->var.idx = idx;
  3686. hist_field->var.hist_data = hist_data;
  3687. }
  3688. }
  3689. return 0;
  3690. }
  3691. static struct hist_trigger_data *
  3692. create_hist_data(unsigned int map_bits,
  3693. struct hist_trigger_attrs *attrs,
  3694. struct trace_event_file *file,
  3695. bool remove)
  3696. {
  3697. const struct tracing_map_ops *map_ops = NULL;
  3698. struct hist_trigger_data *hist_data;
  3699. int ret = 0;
  3700. hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
  3701. if (!hist_data)
  3702. return ERR_PTR(-ENOMEM);
  3703. hist_data->attrs = attrs;
  3704. hist_data->remove = remove;
  3705. hist_data->event_file = file;
  3706. ret = parse_actions(hist_data);
  3707. if (ret)
  3708. goto free;
  3709. ret = create_hist_fields(hist_data, file);
  3710. if (ret)
  3711. goto free;
  3712. ret = create_sort_keys(hist_data);
  3713. if (ret)
  3714. goto free;
  3715. map_ops = &hist_trigger_elt_data_ops;
  3716. hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
  3717. map_ops, hist_data);
  3718. if (IS_ERR(hist_data->map)) {
  3719. ret = PTR_ERR(hist_data->map);
  3720. hist_data->map = NULL;
  3721. goto free;
  3722. }
  3723. ret = create_tracing_map_fields(hist_data);
  3724. if (ret)
  3725. goto free;
  3726. out:
  3727. return hist_data;
  3728. free:
  3729. hist_data->attrs = NULL;
  3730. destroy_hist_data(hist_data);
  3731. hist_data = ERR_PTR(ret);
  3732. goto out;
  3733. }
  3734. static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
  3735. struct tracing_map_elt *elt, void *rec,
  3736. struct ring_buffer_event *rbe,
  3737. u64 *var_ref_vals)
  3738. {
  3739. struct hist_elt_data *elt_data;
  3740. struct hist_field *hist_field;
  3741. unsigned int i, var_idx;
  3742. u64 hist_val;
  3743. elt_data = elt->private_data;
  3744. elt_data->var_ref_vals = var_ref_vals;
  3745. for_each_hist_val_field(i, hist_data) {
  3746. hist_field = hist_data->fields[i];
  3747. hist_val = hist_field->fn(hist_field, elt, rbe, rec);
  3748. if (hist_field->flags & HIST_FIELD_FL_VAR) {
  3749. var_idx = hist_field->var.idx;
  3750. tracing_map_set_var(elt, var_idx, hist_val);
  3751. continue;
  3752. }
  3753. tracing_map_update_sum(elt, i, hist_val);
  3754. }
  3755. for_each_hist_key_field(i, hist_data) {
  3756. hist_field = hist_data->fields[i];
  3757. if (hist_field->flags & HIST_FIELD_FL_VAR) {
  3758. hist_val = hist_field->fn(hist_field, elt, rbe, rec);
  3759. var_idx = hist_field->var.idx;
  3760. tracing_map_set_var(elt, var_idx, hist_val);
  3761. }
  3762. }
  3763. update_field_vars(hist_data, elt, rbe, rec);
  3764. }
  3765. static inline void add_to_key(char *compound_key, void *key,
  3766. struct hist_field *key_field, void *rec)
  3767. {
  3768. size_t size = key_field->size;
  3769. if (key_field->flags & HIST_FIELD_FL_STRING) {
  3770. struct ftrace_event_field *field;
  3771. field = key_field->field;
  3772. if (field->filter_type == FILTER_DYN_STRING)
  3773. size = *(u32 *)(rec + field->offset) >> 16;
  3774. else if (field->filter_type == FILTER_PTR_STRING)
  3775. size = strlen(key);
  3776. else if (field->filter_type == FILTER_STATIC_STRING)
  3777. size = field->size;
  3778. /* ensure NULL-termination */
  3779. if (size > key_field->size - 1)
  3780. size = key_field->size - 1;
  3781. }
  3782. memcpy(compound_key + key_field->offset, key, size);
  3783. }
  3784. static void
  3785. hist_trigger_actions(struct hist_trigger_data *hist_data,
  3786. struct tracing_map_elt *elt, void *rec,
  3787. struct ring_buffer_event *rbe, u64 *var_ref_vals)
  3788. {
  3789. struct action_data *data;
  3790. unsigned int i;
  3791. for (i = 0; i < hist_data->n_actions; i++) {
  3792. data = hist_data->actions[i];
  3793. data->fn(hist_data, elt, rec, rbe, data, var_ref_vals);
  3794. }
  3795. }
  3796. static void event_hist_trigger(struct event_trigger_data *data, void *rec,
  3797. struct ring_buffer_event *rbe)
  3798. {
  3799. struct hist_trigger_data *hist_data = data->private_data;
  3800. bool use_compound_key = (hist_data->n_keys > 1);
  3801. unsigned long entries[HIST_STACKTRACE_DEPTH];
  3802. u64 var_ref_vals[TRACING_MAP_VARS_MAX];
  3803. char compound_key[HIST_KEY_SIZE_MAX];
  3804. struct tracing_map_elt *elt = NULL;
  3805. struct stack_trace stacktrace;
  3806. struct hist_field *key_field;
  3807. u64 field_contents;
  3808. void *key = NULL;
  3809. unsigned int i;
  3810. memset(compound_key, 0, hist_data->key_size);
  3811. for_each_hist_key_field(i, hist_data) {
  3812. key_field = hist_data->fields[i];
  3813. if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
  3814. stacktrace.max_entries = HIST_STACKTRACE_DEPTH;
  3815. stacktrace.entries = entries;
  3816. stacktrace.nr_entries = 0;
  3817. stacktrace.skip = HIST_STACKTRACE_SKIP;
  3818. memset(stacktrace.entries, 0, HIST_STACKTRACE_SIZE);
  3819. save_stack_trace(&stacktrace);
  3820. key = entries;
  3821. } else {
  3822. field_contents = key_field->fn(key_field, elt, rbe, rec);
  3823. if (key_field->flags & HIST_FIELD_FL_STRING) {
  3824. key = (void *)(unsigned long)field_contents;
  3825. use_compound_key = true;
  3826. } else
  3827. key = (void *)&field_contents;
  3828. }
  3829. if (use_compound_key)
  3830. add_to_key(compound_key, key, key_field, rec);
  3831. }
  3832. if (use_compound_key)
  3833. key = compound_key;
  3834. if (hist_data->n_var_refs &&
  3835. !resolve_var_refs(hist_data, key, var_ref_vals, false))
  3836. return;
  3837. elt = tracing_map_insert(hist_data->map, key);
  3838. if (!elt)
  3839. return;
  3840. hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
  3841. if (resolve_var_refs(hist_data, key, var_ref_vals, true))
  3842. hist_trigger_actions(hist_data, elt, rec, rbe, var_ref_vals);
  3843. }
  3844. static void hist_trigger_stacktrace_print(struct seq_file *m,
  3845. unsigned long *stacktrace_entries,
  3846. unsigned int max_entries)
  3847. {
  3848. char str[KSYM_SYMBOL_LEN];
  3849. unsigned int spaces = 8;
  3850. unsigned int i;
  3851. for (i = 0; i < max_entries; i++) {
  3852. if (stacktrace_entries[i] == ULONG_MAX)
  3853. return;
  3854. seq_printf(m, "%*c", 1 + spaces, ' ');
  3855. sprint_symbol(str, stacktrace_entries[i]);
  3856. seq_printf(m, "%s\n", str);
  3857. }
  3858. }
  3859. static void
  3860. hist_trigger_entry_print(struct seq_file *m,
  3861. struct hist_trigger_data *hist_data, void *key,
  3862. struct tracing_map_elt *elt)
  3863. {
  3864. struct hist_field *key_field;
  3865. char str[KSYM_SYMBOL_LEN];
  3866. bool multiline = false;
  3867. const char *field_name;
  3868. unsigned int i;
  3869. u64 uval;
  3870. seq_puts(m, "{ ");
  3871. for_each_hist_key_field(i, hist_data) {
  3872. key_field = hist_data->fields[i];
  3873. if (i > hist_data->n_vals)
  3874. seq_puts(m, ", ");
  3875. field_name = hist_field_name(key_field, 0);
  3876. if (key_field->flags & HIST_FIELD_FL_HEX) {
  3877. uval = *(u64 *)(key + key_field->offset);
  3878. seq_printf(m, "%s: %llx", field_name, uval);
  3879. } else if (key_field->flags & HIST_FIELD_FL_SYM) {
  3880. uval = *(u64 *)(key + key_field->offset);
  3881. sprint_symbol_no_offset(str, uval);
  3882. seq_printf(m, "%s: [%llx] %-45s", field_name,
  3883. uval, str);
  3884. } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
  3885. uval = *(u64 *)(key + key_field->offset);
  3886. sprint_symbol(str, uval);
  3887. seq_printf(m, "%s: [%llx] %-55s", field_name,
  3888. uval, str);
  3889. } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
  3890. struct hist_elt_data *elt_data = elt->private_data;
  3891. char *comm;
  3892. if (WARN_ON_ONCE(!elt_data))
  3893. return;
  3894. comm = elt_data->comm;
  3895. uval = *(u64 *)(key + key_field->offset);
  3896. seq_printf(m, "%s: %-16s[%10llu]", field_name,
  3897. comm, uval);
  3898. } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
  3899. const char *syscall_name;
  3900. uval = *(u64 *)(key + key_field->offset);
  3901. syscall_name = get_syscall_name(uval);
  3902. if (!syscall_name)
  3903. syscall_name = "unknown_syscall";
  3904. seq_printf(m, "%s: %-30s[%3llu]", field_name,
  3905. syscall_name, uval);
  3906. } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
  3907. seq_puts(m, "stacktrace:\n");
  3908. hist_trigger_stacktrace_print(m,
  3909. key + key_field->offset,
  3910. HIST_STACKTRACE_DEPTH);
  3911. multiline = true;
  3912. } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
  3913. seq_printf(m, "%s: ~ 2^%-2llu", field_name,
  3914. *(u64 *)(key + key_field->offset));
  3915. } else if (key_field->flags & HIST_FIELD_FL_STRING) {
  3916. seq_printf(m, "%s: %-50s", field_name,
  3917. (char *)(key + key_field->offset));
  3918. } else {
  3919. uval = *(u64 *)(key + key_field->offset);
  3920. seq_printf(m, "%s: %10llu", field_name, uval);
  3921. }
  3922. }
  3923. if (!multiline)
  3924. seq_puts(m, " ");
  3925. seq_puts(m, "}");
  3926. seq_printf(m, " hitcount: %10llu",
  3927. tracing_map_read_sum(elt, HITCOUNT_IDX));
  3928. for (i = 1; i < hist_data->n_vals; i++) {
  3929. field_name = hist_field_name(hist_data->fields[i], 0);
  3930. if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
  3931. hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
  3932. continue;
  3933. if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
  3934. seq_printf(m, " %s: %10llx", field_name,
  3935. tracing_map_read_sum(elt, i));
  3936. } else {
  3937. seq_printf(m, " %s: %10llu", field_name,
  3938. tracing_map_read_sum(elt, i));
  3939. }
  3940. }
  3941. print_actions(m, hist_data, elt);
  3942. seq_puts(m, "\n");
  3943. }
  3944. static int print_entries(struct seq_file *m,
  3945. struct hist_trigger_data *hist_data)
  3946. {
  3947. struct tracing_map_sort_entry **sort_entries = NULL;
  3948. struct tracing_map *map = hist_data->map;
  3949. int i, n_entries;
  3950. n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
  3951. hist_data->n_sort_keys,
  3952. &sort_entries);
  3953. if (n_entries < 0)
  3954. return n_entries;
  3955. for (i = 0; i < n_entries; i++)
  3956. hist_trigger_entry_print(m, hist_data,
  3957. sort_entries[i]->key,
  3958. sort_entries[i]->elt);
  3959. tracing_map_destroy_sort_entries(sort_entries, n_entries);
  3960. return n_entries;
  3961. }
  3962. static void hist_trigger_show(struct seq_file *m,
  3963. struct event_trigger_data *data, int n)
  3964. {
  3965. struct hist_trigger_data *hist_data;
  3966. int n_entries;
  3967. if (n > 0)
  3968. seq_puts(m, "\n\n");
  3969. seq_puts(m, "# event histogram\n#\n# trigger info: ");
  3970. data->ops->print(m, data->ops, data);
  3971. seq_puts(m, "#\n\n");
  3972. hist_data = data->private_data;
  3973. n_entries = print_entries(m, hist_data);
  3974. if (n_entries < 0)
  3975. n_entries = 0;
  3976. seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n",
  3977. (u64)atomic64_read(&hist_data->map->hits),
  3978. n_entries, (u64)atomic64_read(&hist_data->map->drops));
  3979. }
  3980. static int hist_show(struct seq_file *m, void *v)
  3981. {
  3982. struct event_trigger_data *data;
  3983. struct trace_event_file *event_file;
  3984. int n = 0, ret = 0;
  3985. mutex_lock(&event_mutex);
  3986. event_file = event_file_data(m->private);
  3987. if (unlikely(!event_file)) {
  3988. ret = -ENODEV;
  3989. goto out_unlock;
  3990. }
  3991. list_for_each_entry_rcu(data, &event_file->triggers, list) {
  3992. if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
  3993. hist_trigger_show(m, data, n++);
  3994. }
  3995. if (have_hist_err()) {
  3996. seq_printf(m, "\nERROR: %s\n", hist_err_str);
  3997. seq_printf(m, " Last command: %s\n", last_hist_cmd);
  3998. }
  3999. out_unlock:
  4000. mutex_unlock(&event_mutex);
  4001. return ret;
  4002. }
  4003. static int event_hist_open(struct inode *inode, struct file *file)
  4004. {
  4005. return single_open(file, hist_show, file);
  4006. }
  4007. const struct file_operations event_hist_fops = {
  4008. .open = event_hist_open,
  4009. .read = seq_read,
  4010. .llseek = seq_lseek,
  4011. .release = single_release,
  4012. };
  4013. static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
  4014. {
  4015. const char *field_name = hist_field_name(hist_field, 0);
  4016. if (hist_field->var.name)
  4017. seq_printf(m, "%s=", hist_field->var.name);
  4018. if (hist_field->flags & HIST_FIELD_FL_CPU)
  4019. seq_puts(m, "cpu");
  4020. else if (field_name) {
  4021. if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
  4022. hist_field->flags & HIST_FIELD_FL_ALIAS)
  4023. seq_putc(m, '$');
  4024. seq_printf(m, "%s", field_name);
  4025. } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
  4026. seq_puts(m, "common_timestamp");
  4027. }
  4028. static int event_hist_trigger_print(struct seq_file *m,
  4029. struct event_trigger_ops *ops,
  4030. struct event_trigger_data *data)
  4031. {
  4032. struct hist_trigger_data *hist_data = data->private_data;
  4033. struct hist_field *field;
  4034. bool have_var = false;
  4035. unsigned int i;
  4036. seq_puts(m, "hist:");
  4037. if (data->name)
  4038. seq_printf(m, "%s:", data->name);
  4039. seq_puts(m, "keys=");
  4040. for_each_hist_key_field(i, hist_data) {
  4041. field = hist_data->fields[i];
  4042. if (i > hist_data->n_vals)
  4043. seq_puts(m, ",");
  4044. if (field->flags & HIST_FIELD_FL_STACKTRACE)
  4045. seq_puts(m, "stacktrace");
  4046. else
  4047. hist_field_print(m, field);
  4048. }
  4049. seq_puts(m, ":vals=");
  4050. for_each_hist_val_field(i, hist_data) {
  4051. field = hist_data->fields[i];
  4052. if (field->flags & HIST_FIELD_FL_VAR) {
  4053. have_var = true;
  4054. continue;
  4055. }
  4056. if (i == HITCOUNT_IDX)
  4057. seq_puts(m, "hitcount");
  4058. else {
  4059. seq_puts(m, ",");
  4060. hist_field_print(m, field);
  4061. }
  4062. }
  4063. if (have_var) {
  4064. unsigned int n = 0;
  4065. seq_puts(m, ":");
  4066. for_each_hist_val_field(i, hist_data) {
  4067. field = hist_data->fields[i];
  4068. if (field->flags & HIST_FIELD_FL_VAR) {
  4069. if (n++)
  4070. seq_puts(m, ",");
  4071. hist_field_print(m, field);
  4072. }
  4073. }
  4074. }
  4075. seq_puts(m, ":sort=");
  4076. for (i = 0; i < hist_data->n_sort_keys; i++) {
  4077. struct tracing_map_sort_key *sort_key;
  4078. unsigned int idx, first_key_idx;
  4079. /* skip VAR vals */
  4080. first_key_idx = hist_data->n_vals - hist_data->n_vars;
  4081. sort_key = &hist_data->sort_keys[i];
  4082. idx = sort_key->field_idx;
  4083. if (WARN_ON(idx >= HIST_FIELDS_MAX))
  4084. return -EINVAL;
  4085. if (i > 0)
  4086. seq_puts(m, ",");
  4087. if (idx == HITCOUNT_IDX)
  4088. seq_puts(m, "hitcount");
  4089. else {
  4090. if (idx >= first_key_idx)
  4091. idx += hist_data->n_vars;
  4092. hist_field_print(m, hist_data->fields[idx]);
  4093. }
  4094. if (sort_key->descending)
  4095. seq_puts(m, ".descending");
  4096. }
  4097. seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
  4098. if (hist_data->enable_timestamps)
  4099. seq_printf(m, ":clock=%s", hist_data->attrs->clock);
  4100. print_actions_spec(m, hist_data);
  4101. if (data->filter_str)
  4102. seq_printf(m, " if %s", data->filter_str);
  4103. if (data->paused)
  4104. seq_puts(m, " [paused]");
  4105. else
  4106. seq_puts(m, " [active]");
  4107. seq_putc(m, '\n');
  4108. return 0;
  4109. }
  4110. static int event_hist_trigger_init(struct event_trigger_ops *ops,
  4111. struct event_trigger_data *data)
  4112. {
  4113. struct hist_trigger_data *hist_data = data->private_data;
  4114. if (!data->ref && hist_data->attrs->name)
  4115. save_named_trigger(hist_data->attrs->name, data);
  4116. data->ref++;
  4117. return 0;
  4118. }
  4119. static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
  4120. {
  4121. struct trace_event_file *file;
  4122. unsigned int i;
  4123. char *cmd;
  4124. int ret;
  4125. for (i = 0; i < hist_data->n_field_var_hists; i++) {
  4126. file = hist_data->field_var_hists[i]->hist_data->event_file;
  4127. cmd = hist_data->field_var_hists[i]->cmd;
  4128. ret = event_hist_trigger_func(&trigger_hist_cmd, file,
  4129. "!hist", "hist", cmd);
  4130. }
  4131. }
  4132. static void event_hist_trigger_free(struct event_trigger_ops *ops,
  4133. struct event_trigger_data *data)
  4134. {
  4135. struct hist_trigger_data *hist_data = data->private_data;
  4136. if (WARN_ON_ONCE(data->ref <= 0))
  4137. return;
  4138. data->ref--;
  4139. if (!data->ref) {
  4140. if (data->name)
  4141. del_named_trigger(data);
  4142. trigger_data_free(data);
  4143. remove_hist_vars(hist_data);
  4144. unregister_field_var_hists(hist_data);
  4145. destroy_hist_data(hist_data);
  4146. }
  4147. }
  4148. static struct event_trigger_ops event_hist_trigger_ops = {
  4149. .func = event_hist_trigger,
  4150. .print = event_hist_trigger_print,
  4151. .init = event_hist_trigger_init,
  4152. .free = event_hist_trigger_free,
  4153. };
  4154. static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
  4155. struct event_trigger_data *data)
  4156. {
  4157. data->ref++;
  4158. save_named_trigger(data->named_data->name, data);
  4159. event_hist_trigger_init(ops, data->named_data);
  4160. return 0;
  4161. }
  4162. static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
  4163. struct event_trigger_data *data)
  4164. {
  4165. if (WARN_ON_ONCE(data->ref <= 0))
  4166. return;
  4167. event_hist_trigger_free(ops, data->named_data);
  4168. data->ref--;
  4169. if (!data->ref) {
  4170. del_named_trigger(data);
  4171. trigger_data_free(data);
  4172. }
  4173. }
  4174. static struct event_trigger_ops event_hist_trigger_named_ops = {
  4175. .func = event_hist_trigger,
  4176. .print = event_hist_trigger_print,
  4177. .init = event_hist_trigger_named_init,
  4178. .free = event_hist_trigger_named_free,
  4179. };
  4180. static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
  4181. char *param)
  4182. {
  4183. return &event_hist_trigger_ops;
  4184. }
  4185. static void hist_clear(struct event_trigger_data *data)
  4186. {
  4187. struct hist_trigger_data *hist_data = data->private_data;
  4188. if (data->name)
  4189. pause_named_trigger(data);
  4190. synchronize_sched();
  4191. tracing_map_clear(hist_data->map);
  4192. if (data->name)
  4193. unpause_named_trigger(data);
  4194. }
  4195. static bool compatible_field(struct ftrace_event_field *field,
  4196. struct ftrace_event_field *test_field)
  4197. {
  4198. if (field == test_field)
  4199. return true;
  4200. if (field == NULL || test_field == NULL)
  4201. return false;
  4202. if (strcmp(field->name, test_field->name) != 0)
  4203. return false;
  4204. if (strcmp(field->type, test_field->type) != 0)
  4205. return false;
  4206. if (field->size != test_field->size)
  4207. return false;
  4208. if (field->is_signed != test_field->is_signed)
  4209. return false;
  4210. return true;
  4211. }
  4212. static bool hist_trigger_match(struct event_trigger_data *data,
  4213. struct event_trigger_data *data_test,
  4214. struct event_trigger_data *named_data,
  4215. bool ignore_filter)
  4216. {
  4217. struct tracing_map_sort_key *sort_key, *sort_key_test;
  4218. struct hist_trigger_data *hist_data, *hist_data_test;
  4219. struct hist_field *key_field, *key_field_test;
  4220. unsigned int i;
  4221. if (named_data && (named_data != data_test) &&
  4222. (named_data != data_test->named_data))
  4223. return false;
  4224. if (!named_data && is_named_trigger(data_test))
  4225. return false;
  4226. hist_data = data->private_data;
  4227. hist_data_test = data_test->private_data;
  4228. if (hist_data->n_vals != hist_data_test->n_vals ||
  4229. hist_data->n_fields != hist_data_test->n_fields ||
  4230. hist_data->n_sort_keys != hist_data_test->n_sort_keys)
  4231. return false;
  4232. if (!ignore_filter) {
  4233. if ((data->filter_str && !data_test->filter_str) ||
  4234. (!data->filter_str && data_test->filter_str))
  4235. return false;
  4236. }
  4237. for_each_hist_field(i, hist_data) {
  4238. key_field = hist_data->fields[i];
  4239. key_field_test = hist_data_test->fields[i];
  4240. if (key_field->flags != key_field_test->flags)
  4241. return false;
  4242. if (!compatible_field(key_field->field, key_field_test->field))
  4243. return false;
  4244. if (key_field->offset != key_field_test->offset)
  4245. return false;
  4246. if (key_field->size != key_field_test->size)
  4247. return false;
  4248. if (key_field->is_signed != key_field_test->is_signed)
  4249. return false;
  4250. if (!!key_field->var.name != !!key_field_test->var.name)
  4251. return false;
  4252. if (key_field->var.name &&
  4253. strcmp(key_field->var.name, key_field_test->var.name) != 0)
  4254. return false;
  4255. }
  4256. for (i = 0; i < hist_data->n_sort_keys; i++) {
  4257. sort_key = &hist_data->sort_keys[i];
  4258. sort_key_test = &hist_data_test->sort_keys[i];
  4259. if (sort_key->field_idx != sort_key_test->field_idx ||
  4260. sort_key->descending != sort_key_test->descending)
  4261. return false;
  4262. }
  4263. if (!ignore_filter && data->filter_str &&
  4264. (strcmp(data->filter_str, data_test->filter_str) != 0))
  4265. return false;
  4266. if (!actions_match(hist_data, hist_data_test))
  4267. return false;
  4268. return true;
  4269. }
  4270. static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
  4271. struct event_trigger_data *data,
  4272. struct trace_event_file *file)
  4273. {
  4274. struct hist_trigger_data *hist_data = data->private_data;
  4275. struct event_trigger_data *test, *named_data = NULL;
  4276. int ret = 0;
  4277. if (hist_data->attrs->name) {
  4278. named_data = find_named_trigger(hist_data->attrs->name);
  4279. if (named_data) {
  4280. if (!hist_trigger_match(data, named_data, named_data,
  4281. true)) {
  4282. hist_err("Named hist trigger doesn't match existing named trigger (includes variables): ", hist_data->attrs->name);
  4283. ret = -EINVAL;
  4284. goto out;
  4285. }
  4286. }
  4287. }
  4288. if (hist_data->attrs->name && !named_data)
  4289. goto new;
  4290. list_for_each_entry_rcu(test, &file->triggers, list) {
  4291. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  4292. if (!hist_trigger_match(data, test, named_data, false))
  4293. continue;
  4294. if (hist_data->attrs->pause)
  4295. test->paused = true;
  4296. else if (hist_data->attrs->cont)
  4297. test->paused = false;
  4298. else if (hist_data->attrs->clear)
  4299. hist_clear(test);
  4300. else {
  4301. hist_err("Hist trigger already exists", NULL);
  4302. ret = -EEXIST;
  4303. }
  4304. goto out;
  4305. }
  4306. }
  4307. new:
  4308. if (hist_data->attrs->cont || hist_data->attrs->clear) {
  4309. hist_err("Can't clear or continue a nonexistent hist trigger", NULL);
  4310. ret = -ENOENT;
  4311. goto out;
  4312. }
  4313. if (hist_data->attrs->pause)
  4314. data->paused = true;
  4315. if (named_data) {
  4316. data->private_data = named_data->private_data;
  4317. set_named_trigger_data(data, named_data);
  4318. data->ops = &event_hist_trigger_named_ops;
  4319. }
  4320. if (data->ops->init) {
  4321. ret = data->ops->init(data->ops, data);
  4322. if (ret < 0)
  4323. goto out;
  4324. }
  4325. if (hist_data->enable_timestamps) {
  4326. char *clock = hist_data->attrs->clock;
  4327. ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
  4328. if (ret) {
  4329. hist_err("Couldn't set trace_clock: ", clock);
  4330. goto out;
  4331. }
  4332. tracing_set_time_stamp_abs(file->tr, true);
  4333. }
  4334. if (named_data)
  4335. destroy_hist_data(hist_data);
  4336. ret++;
  4337. out:
  4338. return ret;
  4339. }
  4340. static int hist_trigger_enable(struct event_trigger_data *data,
  4341. struct trace_event_file *file)
  4342. {
  4343. int ret = 0;
  4344. list_add_tail_rcu(&data->list, &file->triggers);
  4345. update_cond_flag(file);
  4346. if (trace_event_trigger_enable_disable(file, 1) < 0) {
  4347. list_del_rcu(&data->list);
  4348. update_cond_flag(file);
  4349. ret--;
  4350. }
  4351. return ret;
  4352. }
  4353. static bool have_hist_trigger_match(struct event_trigger_data *data,
  4354. struct trace_event_file *file)
  4355. {
  4356. struct hist_trigger_data *hist_data = data->private_data;
  4357. struct event_trigger_data *test, *named_data = NULL;
  4358. bool match = false;
  4359. if (hist_data->attrs->name)
  4360. named_data = find_named_trigger(hist_data->attrs->name);
  4361. list_for_each_entry_rcu(test, &file->triggers, list) {
  4362. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  4363. if (hist_trigger_match(data, test, named_data, false)) {
  4364. match = true;
  4365. break;
  4366. }
  4367. }
  4368. }
  4369. return match;
  4370. }
  4371. static bool hist_trigger_check_refs(struct event_trigger_data *data,
  4372. struct trace_event_file *file)
  4373. {
  4374. struct hist_trigger_data *hist_data = data->private_data;
  4375. struct event_trigger_data *test, *named_data = NULL;
  4376. if (hist_data->attrs->name)
  4377. named_data = find_named_trigger(hist_data->attrs->name);
  4378. list_for_each_entry_rcu(test, &file->triggers, list) {
  4379. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  4380. if (!hist_trigger_match(data, test, named_data, false))
  4381. continue;
  4382. hist_data = test->private_data;
  4383. if (check_var_refs(hist_data))
  4384. return true;
  4385. break;
  4386. }
  4387. }
  4388. return false;
  4389. }
  4390. static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
  4391. struct event_trigger_data *data,
  4392. struct trace_event_file *file)
  4393. {
  4394. struct hist_trigger_data *hist_data = data->private_data;
  4395. struct event_trigger_data *test, *named_data = NULL;
  4396. bool unregistered = false;
  4397. if (hist_data->attrs->name)
  4398. named_data = find_named_trigger(hist_data->attrs->name);
  4399. list_for_each_entry_rcu(test, &file->triggers, list) {
  4400. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  4401. if (!hist_trigger_match(data, test, named_data, false))
  4402. continue;
  4403. unregistered = true;
  4404. list_del_rcu(&test->list);
  4405. trace_event_trigger_enable_disable(file, 0);
  4406. update_cond_flag(file);
  4407. break;
  4408. }
  4409. }
  4410. if (unregistered && test->ops->free)
  4411. test->ops->free(test->ops, test);
  4412. if (hist_data->enable_timestamps) {
  4413. if (!hist_data->remove || unregistered)
  4414. tracing_set_time_stamp_abs(file->tr, false);
  4415. }
  4416. }
  4417. static bool hist_file_check_refs(struct trace_event_file *file)
  4418. {
  4419. struct hist_trigger_data *hist_data;
  4420. struct event_trigger_data *test;
  4421. list_for_each_entry_rcu(test, &file->triggers, list) {
  4422. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  4423. hist_data = test->private_data;
  4424. if (check_var_refs(hist_data))
  4425. return true;
  4426. }
  4427. }
  4428. return false;
  4429. }
  4430. static void hist_unreg_all(struct trace_event_file *file)
  4431. {
  4432. struct event_trigger_data *test, *n;
  4433. struct hist_trigger_data *hist_data;
  4434. struct synth_event *se;
  4435. const char *se_name;
  4436. if (hist_file_check_refs(file))
  4437. return;
  4438. list_for_each_entry_safe(test, n, &file->triggers, list) {
  4439. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  4440. hist_data = test->private_data;
  4441. list_del_rcu(&test->list);
  4442. trace_event_trigger_enable_disable(file, 0);
  4443. mutex_lock(&synth_event_mutex);
  4444. se_name = trace_event_name(file->event_call);
  4445. se = find_synth_event(se_name);
  4446. if (se)
  4447. se->ref--;
  4448. mutex_unlock(&synth_event_mutex);
  4449. update_cond_flag(file);
  4450. if (hist_data->enable_timestamps)
  4451. tracing_set_time_stamp_abs(file->tr, false);
  4452. if (test->ops->free)
  4453. test->ops->free(test->ops, test);
  4454. }
  4455. }
  4456. }
  4457. static int event_hist_trigger_func(struct event_command *cmd_ops,
  4458. struct trace_event_file *file,
  4459. char *glob, char *cmd, char *param)
  4460. {
  4461. unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
  4462. struct event_trigger_data *trigger_data;
  4463. struct hist_trigger_attrs *attrs;
  4464. struct event_trigger_ops *trigger_ops;
  4465. struct hist_trigger_data *hist_data;
  4466. struct synth_event *se;
  4467. const char *se_name;
  4468. bool remove = false;
  4469. char *trigger, *p;
  4470. int ret = 0;
  4471. if (glob && strlen(glob)) {
  4472. last_cmd_set(param);
  4473. hist_err_clear();
  4474. }
  4475. if (!param)
  4476. return -EINVAL;
  4477. if (glob[0] == '!')
  4478. remove = true;
  4479. /*
  4480. * separate the trigger from the filter (k:v [if filter])
  4481. * allowing for whitespace in the trigger
  4482. */
  4483. p = trigger = param;
  4484. do {
  4485. p = strstr(p, "if");
  4486. if (!p)
  4487. break;
  4488. if (p == param)
  4489. return -EINVAL;
  4490. if (*(p - 1) != ' ' && *(p - 1) != '\t') {
  4491. p++;
  4492. continue;
  4493. }
  4494. if (p >= param + strlen(param) - strlen("if") - 1)
  4495. return -EINVAL;
  4496. if (*(p + strlen("if")) != ' ' && *(p + strlen("if")) != '\t') {
  4497. p++;
  4498. continue;
  4499. }
  4500. break;
  4501. } while (p);
  4502. if (!p)
  4503. param = NULL;
  4504. else {
  4505. *(p - 1) = '\0';
  4506. param = strstrip(p);
  4507. trigger = strstrip(trigger);
  4508. }
  4509. attrs = parse_hist_trigger_attrs(trigger);
  4510. if (IS_ERR(attrs))
  4511. return PTR_ERR(attrs);
  4512. if (attrs->map_bits)
  4513. hist_trigger_bits = attrs->map_bits;
  4514. hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
  4515. if (IS_ERR(hist_data)) {
  4516. destroy_hist_trigger_attrs(attrs);
  4517. return PTR_ERR(hist_data);
  4518. }
  4519. trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
  4520. trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
  4521. if (!trigger_data) {
  4522. ret = -ENOMEM;
  4523. goto out_free;
  4524. }
  4525. trigger_data->count = -1;
  4526. trigger_data->ops = trigger_ops;
  4527. trigger_data->cmd_ops = cmd_ops;
  4528. INIT_LIST_HEAD(&trigger_data->list);
  4529. RCU_INIT_POINTER(trigger_data->filter, NULL);
  4530. trigger_data->private_data = hist_data;
  4531. /* if param is non-empty, it's supposed to be a filter */
  4532. if (param && cmd_ops->set_filter) {
  4533. ret = cmd_ops->set_filter(param, trigger_data, file);
  4534. if (ret < 0)
  4535. goto out_free;
  4536. }
  4537. if (remove) {
  4538. if (!have_hist_trigger_match(trigger_data, file))
  4539. goto out_free;
  4540. if (hist_trigger_check_refs(trigger_data, file)) {
  4541. ret = -EBUSY;
  4542. goto out_free;
  4543. }
  4544. cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
  4545. mutex_lock(&synth_event_mutex);
  4546. se_name = trace_event_name(file->event_call);
  4547. se = find_synth_event(se_name);
  4548. if (se)
  4549. se->ref--;
  4550. mutex_unlock(&synth_event_mutex);
  4551. ret = 0;
  4552. goto out_free;
  4553. }
  4554. ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
  4555. /*
  4556. * The above returns on success the # of triggers registered,
  4557. * but if it didn't register any it returns zero. Consider no
  4558. * triggers registered a failure too.
  4559. */
  4560. if (!ret) {
  4561. if (!(attrs->pause || attrs->cont || attrs->clear))
  4562. ret = -ENOENT;
  4563. goto out_free;
  4564. } else if (ret < 0)
  4565. goto out_free;
  4566. if (get_named_trigger_data(trigger_data))
  4567. goto enable;
  4568. if (has_hist_vars(hist_data))
  4569. save_hist_vars(hist_data);
  4570. ret = create_actions(hist_data, file);
  4571. if (ret)
  4572. goto out_unreg;
  4573. ret = tracing_map_init(hist_data->map);
  4574. if (ret)
  4575. goto out_unreg;
  4576. enable:
  4577. ret = hist_trigger_enable(trigger_data, file);
  4578. if (ret)
  4579. goto out_unreg;
  4580. mutex_lock(&synth_event_mutex);
  4581. se_name = trace_event_name(file->event_call);
  4582. se = find_synth_event(se_name);
  4583. if (se)
  4584. se->ref++;
  4585. mutex_unlock(&synth_event_mutex);
  4586. /* Just return zero, not the number of registered triggers */
  4587. ret = 0;
  4588. out:
  4589. if (ret == 0)
  4590. hist_err_clear();
  4591. return ret;
  4592. out_unreg:
  4593. cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
  4594. out_free:
  4595. if (cmd_ops->set_filter)
  4596. cmd_ops->set_filter(NULL, trigger_data, NULL);
  4597. remove_hist_vars(hist_data);
  4598. kfree(trigger_data);
  4599. destroy_hist_data(hist_data);
  4600. goto out;
  4601. }
  4602. static struct event_command trigger_hist_cmd = {
  4603. .name = "hist",
  4604. .trigger_type = ETT_EVENT_HIST,
  4605. .flags = EVENT_CMD_FL_NEEDS_REC,
  4606. .func = event_hist_trigger_func,
  4607. .reg = hist_register_trigger,
  4608. .unreg = hist_unregister_trigger,
  4609. .unreg_all = hist_unreg_all,
  4610. .get_trigger_ops = event_hist_get_trigger_ops,
  4611. .set_filter = set_trigger_filter,
  4612. };
  4613. __init int register_trigger_hist_cmd(void)
  4614. {
  4615. int ret;
  4616. ret = register_event_command(&trigger_hist_cmd);
  4617. WARN_ON(ret < 0);
  4618. return ret;
  4619. }
  4620. static void
  4621. hist_enable_trigger(struct event_trigger_data *data, void *rec,
  4622. struct ring_buffer_event *event)
  4623. {
  4624. struct enable_trigger_data *enable_data = data->private_data;
  4625. struct event_trigger_data *test;
  4626. list_for_each_entry_rcu(test, &enable_data->file->triggers, list) {
  4627. if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
  4628. if (enable_data->enable)
  4629. test->paused = false;
  4630. else
  4631. test->paused = true;
  4632. }
  4633. }
  4634. }
  4635. static void
  4636. hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
  4637. struct ring_buffer_event *event)
  4638. {
  4639. if (!data->count)
  4640. return;
  4641. if (data->count != -1)
  4642. (data->count)--;
  4643. hist_enable_trigger(data, rec, event);
  4644. }
  4645. static struct event_trigger_ops hist_enable_trigger_ops = {
  4646. .func = hist_enable_trigger,
  4647. .print = event_enable_trigger_print,
  4648. .init = event_trigger_init,
  4649. .free = event_enable_trigger_free,
  4650. };
  4651. static struct event_trigger_ops hist_enable_count_trigger_ops = {
  4652. .func = hist_enable_count_trigger,
  4653. .print = event_enable_trigger_print,
  4654. .init = event_trigger_init,
  4655. .free = event_enable_trigger_free,
  4656. };
  4657. static struct event_trigger_ops hist_disable_trigger_ops = {
  4658. .func = hist_enable_trigger,
  4659. .print = event_enable_trigger_print,
  4660. .init = event_trigger_init,
  4661. .free = event_enable_trigger_free,
  4662. };
  4663. static struct event_trigger_ops hist_disable_count_trigger_ops = {
  4664. .func = hist_enable_count_trigger,
  4665. .print = event_enable_trigger_print,
  4666. .init = event_trigger_init,
  4667. .free = event_enable_trigger_free,
  4668. };
  4669. static struct event_trigger_ops *
  4670. hist_enable_get_trigger_ops(char *cmd, char *param)
  4671. {
  4672. struct event_trigger_ops *ops;
  4673. bool enable;
  4674. enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
  4675. if (enable)
  4676. ops = param ? &hist_enable_count_trigger_ops :
  4677. &hist_enable_trigger_ops;
  4678. else
  4679. ops = param ? &hist_disable_count_trigger_ops :
  4680. &hist_disable_trigger_ops;
  4681. return ops;
  4682. }
  4683. static void hist_enable_unreg_all(struct trace_event_file *file)
  4684. {
  4685. struct event_trigger_data *test, *n;
  4686. list_for_each_entry_safe(test, n, &file->triggers, list) {
  4687. if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
  4688. list_del_rcu(&test->list);
  4689. update_cond_flag(file);
  4690. trace_event_trigger_enable_disable(file, 0);
  4691. if (test->ops->free)
  4692. test->ops->free(test->ops, test);
  4693. }
  4694. }
  4695. }
  4696. static struct event_command trigger_hist_enable_cmd = {
  4697. .name = ENABLE_HIST_STR,
  4698. .trigger_type = ETT_HIST_ENABLE,
  4699. .func = event_enable_trigger_func,
  4700. .reg = event_enable_register_trigger,
  4701. .unreg = event_enable_unregister_trigger,
  4702. .unreg_all = hist_enable_unreg_all,
  4703. .get_trigger_ops = hist_enable_get_trigger_ops,
  4704. .set_filter = set_trigger_filter,
  4705. };
  4706. static struct event_command trigger_hist_disable_cmd = {
  4707. .name = DISABLE_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 __init void unregister_trigger_hist_enable_disable_cmds(void)
  4717. {
  4718. unregister_event_command(&trigger_hist_enable_cmd);
  4719. unregister_event_command(&trigger_hist_disable_cmd);
  4720. }
  4721. __init int register_trigger_hist_enable_disable_cmds(void)
  4722. {
  4723. int ret;
  4724. ret = register_event_command(&trigger_hist_enable_cmd);
  4725. if (WARN_ON(ret < 0))
  4726. return ret;
  4727. ret = register_event_command(&trigger_hist_disable_cmd);
  4728. if (WARN_ON(ret < 0))
  4729. unregister_trigger_hist_enable_disable_cmds();
  4730. return ret;
  4731. }
  4732. static __init int trace_events_hist_init(void)
  4733. {
  4734. struct dentry *entry = NULL;
  4735. struct dentry *d_tracer;
  4736. int err = 0;
  4737. d_tracer = tracing_init_dentry();
  4738. if (IS_ERR(d_tracer)) {
  4739. err = PTR_ERR(d_tracer);
  4740. goto err;
  4741. }
  4742. entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
  4743. NULL, &synth_events_fops);
  4744. if (!entry) {
  4745. err = -ENODEV;
  4746. goto err;
  4747. }
  4748. return err;
  4749. err:
  4750. pr_warn("Could not create tracefs 'synthetic_events' entry\n");
  4751. return err;
  4752. }
  4753. fs_initcall(trace_events_hist_init);