trace_events_hist.c 132 KB

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