trace_events.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * event tracer
  4. *
  5. * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  6. *
  7. * - Added format output of fields of the trace point.
  8. * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
  9. *
  10. */
  11. #define pr_fmt(fmt) fmt
  12. #include <linux/workqueue.h>
  13. #include <linux/security.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/kthread.h>
  16. #include <linux/tracefs.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/module.h>
  19. #include <linux/ctype.h>
  20. #include <linux/sort.h>
  21. #include <linux/slab.h>
  22. #include <linux/delay.h>
  23. #include <trace/events/sched.h>
  24. #include <trace/syscall.h>
  25. #include <asm/setup.h>
  26. #include "trace_output.h"
  27. #undef TRACE_SYSTEM
  28. #define TRACE_SYSTEM "TRACE_SYSTEM"
  29. DEFINE_MUTEX(event_mutex);
  30. LIST_HEAD(ftrace_events);
  31. static LIST_HEAD(ftrace_generic_fields);
  32. static LIST_HEAD(ftrace_common_fields);
  33. static bool eventdir_initialized;
  34. static LIST_HEAD(module_strings);
  35. struct module_string {
  36. struct list_head next;
  37. struct module *module;
  38. char *str;
  39. };
  40. #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
  41. static struct kmem_cache *field_cachep;
  42. static struct kmem_cache *file_cachep;
  43. static inline int system_refcount(struct event_subsystem *system)
  44. {
  45. return system->ref_count;
  46. }
  47. static int system_refcount_inc(struct event_subsystem *system)
  48. {
  49. return system->ref_count++;
  50. }
  51. static int system_refcount_dec(struct event_subsystem *system)
  52. {
  53. return --system->ref_count;
  54. }
  55. /* Double loops, do not use break, only goto's work */
  56. #define do_for_each_event_file(tr, file) \
  57. list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
  58. list_for_each_entry(file, &tr->events, list)
  59. #define do_for_each_event_file_safe(tr, file) \
  60. list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
  61. struct trace_event_file *___n; \
  62. list_for_each_entry_safe(file, ___n, &tr->events, list)
  63. #define while_for_each_event_file() \
  64. }
  65. static struct ftrace_event_field *
  66. __find_event_field(struct list_head *head, const char *name)
  67. {
  68. struct ftrace_event_field *field;
  69. list_for_each_entry(field, head, link) {
  70. if (!strcmp(field->name, name))
  71. return field;
  72. }
  73. return NULL;
  74. }
  75. struct ftrace_event_field *
  76. trace_find_event_field(struct trace_event_call *call, char *name)
  77. {
  78. struct ftrace_event_field *field;
  79. struct list_head *head;
  80. head = trace_get_fields(call);
  81. field = __find_event_field(head, name);
  82. if (field)
  83. return field;
  84. field = __find_event_field(&ftrace_generic_fields, name);
  85. if (field)
  86. return field;
  87. return __find_event_field(&ftrace_common_fields, name);
  88. }
  89. static int __trace_define_field(struct list_head *head, const char *type,
  90. const char *name, int offset, int size,
  91. int is_signed, int filter_type, int len,
  92. int need_test)
  93. {
  94. struct ftrace_event_field *field;
  95. field = kmem_cache_alloc(field_cachep, GFP_TRACE);
  96. if (!field)
  97. return -ENOMEM;
  98. field->name = name;
  99. field->type = type;
  100. if (filter_type == FILTER_OTHER)
  101. field->filter_type = filter_assign_type(type);
  102. else
  103. field->filter_type = filter_type;
  104. field->offset = offset;
  105. field->size = size;
  106. field->is_signed = is_signed;
  107. field->needs_test = need_test;
  108. field->len = len;
  109. list_add(&field->link, head);
  110. return 0;
  111. }
  112. int trace_define_field(struct trace_event_call *call, const char *type,
  113. const char *name, int offset, int size, int is_signed,
  114. int filter_type)
  115. {
  116. struct list_head *head;
  117. if (WARN_ON(!call->class))
  118. return 0;
  119. head = trace_get_fields(call);
  120. return __trace_define_field(head, type, name, offset, size,
  121. is_signed, filter_type, 0, 0);
  122. }
  123. EXPORT_SYMBOL_GPL(trace_define_field);
  124. static int trace_define_field_ext(struct trace_event_call *call, const char *type,
  125. const char *name, int offset, int size, int is_signed,
  126. int filter_type, int len, int need_test)
  127. {
  128. struct list_head *head;
  129. if (WARN_ON(!call->class))
  130. return 0;
  131. head = trace_get_fields(call);
  132. return __trace_define_field(head, type, name, offset, size,
  133. is_signed, filter_type, len, need_test);
  134. }
  135. #define __generic_field(type, item, filter_type) \
  136. ret = __trace_define_field(&ftrace_generic_fields, #type, \
  137. #item, 0, 0, is_signed_type(type), \
  138. filter_type, 0, 0); \
  139. if (ret) \
  140. return ret;
  141. #define __common_field(type, item) \
  142. ret = __trace_define_field(&ftrace_common_fields, #type, \
  143. "common_" #item, \
  144. offsetof(typeof(ent), item), \
  145. sizeof(ent.item), \
  146. is_signed_type(type), FILTER_OTHER, \
  147. 0, 0); \
  148. if (ret) \
  149. return ret;
  150. static int trace_define_generic_fields(void)
  151. {
  152. int ret;
  153. __generic_field(int, CPU, FILTER_CPU);
  154. __generic_field(int, cpu, FILTER_CPU);
  155. __generic_field(int, common_cpu, FILTER_CPU);
  156. __generic_field(char *, COMM, FILTER_COMM);
  157. __generic_field(char *, comm, FILTER_COMM);
  158. __generic_field(char *, stacktrace, FILTER_STACKTRACE);
  159. __generic_field(char *, STACKTRACE, FILTER_STACKTRACE);
  160. return ret;
  161. }
  162. static int trace_define_common_fields(void)
  163. {
  164. int ret;
  165. struct trace_entry ent;
  166. __common_field(unsigned short, type);
  167. __common_field(unsigned char, flags);
  168. /* Holds both preempt_count and migrate_disable */
  169. __common_field(unsigned char, preempt_count);
  170. __common_field(int, pid);
  171. return ret;
  172. }
  173. static void trace_destroy_fields(struct trace_event_call *call)
  174. {
  175. struct ftrace_event_field *field, *next;
  176. struct list_head *head;
  177. head = trace_get_fields(call);
  178. list_for_each_entry_safe(field, next, head, link) {
  179. list_del(&field->link);
  180. kmem_cache_free(field_cachep, field);
  181. }
  182. }
  183. /*
  184. * run-time version of trace_event_get_offsets_<call>() that returns the last
  185. * accessible offset of trace fields excluding __dynamic_array bytes
  186. */
  187. int trace_event_get_offsets(struct trace_event_call *call)
  188. {
  189. struct ftrace_event_field *tail;
  190. struct list_head *head;
  191. head = trace_get_fields(call);
  192. /*
  193. * head->next points to the last field with the largest offset,
  194. * since it was added last by trace_define_field()
  195. */
  196. tail = list_first_entry(head, struct ftrace_event_field, link);
  197. return tail->offset + tail->size;
  198. }
  199. static struct trace_event_fields *find_event_field(const char *fmt,
  200. struct trace_event_call *call)
  201. {
  202. struct trace_event_fields *field = call->class->fields_array;
  203. const char *p = fmt;
  204. int len;
  205. if (!(len = str_has_prefix(fmt, "REC->")))
  206. return NULL;
  207. fmt += len;
  208. for (p = fmt; *p; p++) {
  209. if (!isalnum(*p) && *p != '_')
  210. break;
  211. }
  212. len = p - fmt;
  213. for (; field->type; field++) {
  214. if (strncmp(field->name, fmt, len) || field->name[len])
  215. continue;
  216. return field;
  217. }
  218. return NULL;
  219. }
  220. /*
  221. * Check if the referenced field is an array and return true,
  222. * as arrays are OK to dereference.
  223. */
  224. static bool test_field(const char *fmt, struct trace_event_call *call)
  225. {
  226. struct trace_event_fields *field;
  227. field = find_event_field(fmt, call);
  228. if (!field)
  229. return false;
  230. /* This is an array and is OK to dereference. */
  231. return strchr(field->type, '[') != NULL;
  232. }
  233. /* Look for a string within an argument */
  234. static bool find_print_string(const char *arg, const char *str, const char *end)
  235. {
  236. const char *r;
  237. r = strstr(arg, str);
  238. return r && r < end;
  239. }
  240. /* Return true if the argument pointer is safe */
  241. static bool process_pointer(const char *fmt, int len, struct trace_event_call *call)
  242. {
  243. const char *r, *e, *a;
  244. e = fmt + len;
  245. /* Find the REC-> in the argument */
  246. r = strstr(fmt, "REC->");
  247. if (r && r < e) {
  248. /*
  249. * Addresses of events on the buffer, or an array on the buffer is
  250. * OK to dereference. There's ways to fool this, but
  251. * this is to catch common mistakes, not malicious code.
  252. */
  253. a = strchr(fmt, '&');
  254. if ((a && (a < r)) || test_field(r, call))
  255. return true;
  256. } else if (find_print_string(fmt, "__get_dynamic_array(", e)) {
  257. return true;
  258. } else if (find_print_string(fmt, "__get_rel_dynamic_array(", e)) {
  259. return true;
  260. } else if (find_print_string(fmt, "__get_dynamic_array_len(", e)) {
  261. return true;
  262. } else if (find_print_string(fmt, "__get_rel_dynamic_array_len(", e)) {
  263. return true;
  264. } else if (find_print_string(fmt, "__get_sockaddr(", e)) {
  265. return true;
  266. } else if (find_print_string(fmt, "__get_rel_sockaddr(", e)) {
  267. return true;
  268. }
  269. return false;
  270. }
  271. /* Return true if the string is safe */
  272. static bool process_string(const char *fmt, int len, struct trace_event_call *call)
  273. {
  274. struct trace_event_fields *field;
  275. const char *r, *e, *s;
  276. e = fmt + len;
  277. /*
  278. * There are several helper functions that return strings.
  279. * If the argument contains a function, then assume its field is valid.
  280. * It is considered that the argument has a function if it has:
  281. * alphanumeric or '_' before a parenthesis.
  282. */
  283. s = fmt;
  284. do {
  285. r = strstr(s, "(");
  286. if (!r || r >= e)
  287. break;
  288. for (int i = 1; r - i >= s; i++) {
  289. char ch = *(r - i);
  290. if (isspace(ch))
  291. continue;
  292. if (isalnum(ch) || ch == '_')
  293. return true;
  294. /* Anything else, this isn't a function */
  295. break;
  296. }
  297. /* A function could be wrapped in parenthesis, try the next one */
  298. s = r + 1;
  299. } while (s < e);
  300. /*
  301. * Check for arrays. If the argument has: foo[REC->val]
  302. * then it is very likely that foo is an array of strings
  303. * that are safe to use.
  304. */
  305. r = strstr(s, "[");
  306. if (r && r < e) {
  307. r = strstr(r, "REC->");
  308. if (r && r < e)
  309. return true;
  310. }
  311. /*
  312. * If there's any strings in the argument consider this arg OK as it
  313. * could be: REC->field ? "foo" : "bar" and we don't want to get into
  314. * verifying that logic here.
  315. */
  316. if (find_print_string(fmt, "\"", e))
  317. return true;
  318. /* Dereferenced strings are also valid like any other pointer */
  319. if (process_pointer(fmt, len, call))
  320. return true;
  321. /* Make sure the field is found */
  322. field = find_event_field(fmt, call);
  323. if (!field)
  324. return false;
  325. /* Test this field's string before printing the event */
  326. call->flags |= TRACE_EVENT_FL_TEST_STR;
  327. field->needs_test = 1;
  328. return true;
  329. }
  330. static void handle_dereference_arg(const char *arg_str, u64 string_flags, int len,
  331. u64 *dereference_flags, int arg,
  332. struct trace_event_call *call)
  333. {
  334. if (string_flags & (1ULL << arg)) {
  335. if (process_string(arg_str, len, call))
  336. *dereference_flags &= ~(1ULL << arg);
  337. } else if (process_pointer(arg_str, len, call))
  338. *dereference_flags &= ~(1ULL << arg);
  339. else
  340. pr_warn("TRACE EVENT ERROR: Bad dereference argument: '%.*s'\n",
  341. len, arg_str);
  342. }
  343. /*
  344. * Examine the print fmt of the event looking for unsafe dereference
  345. * pointers using %p* that could be recorded in the trace event and
  346. * much later referenced after the pointer was freed. Dereferencing
  347. * pointers are OK, if it is dereferenced into the event itself.
  348. */
  349. static void test_event_printk(struct trace_event_call *call)
  350. {
  351. u64 dereference_flags = 0;
  352. u64 string_flags = 0;
  353. bool first = true;
  354. const char *fmt;
  355. int parens = 0;
  356. char in_quote = 0;
  357. int start_arg = 0;
  358. int arg = 0;
  359. int i, e;
  360. fmt = call->print_fmt;
  361. if (!fmt)
  362. return;
  363. for (i = 0; fmt[i]; i++) {
  364. switch (fmt[i]) {
  365. case '\\':
  366. i++;
  367. if (!fmt[i])
  368. return;
  369. continue;
  370. case '"':
  371. case '\'':
  372. /*
  373. * The print fmt starts with a string that
  374. * is processed first to find %p* usage,
  375. * then after the first string, the print fmt
  376. * contains arguments that are used to check
  377. * if the dereferenced %p* usage is safe.
  378. */
  379. if (first) {
  380. if (fmt[i] == '\'')
  381. continue;
  382. if (in_quote) {
  383. arg = 0;
  384. first = false;
  385. /*
  386. * If there was no %p* uses
  387. * the fmt is OK.
  388. */
  389. if (!dereference_flags)
  390. return;
  391. }
  392. }
  393. if (in_quote) {
  394. if (in_quote == fmt[i])
  395. in_quote = 0;
  396. } else {
  397. in_quote = fmt[i];
  398. }
  399. continue;
  400. case '%':
  401. if (!first || !in_quote)
  402. continue;
  403. i++;
  404. if (!fmt[i])
  405. return;
  406. switch (fmt[i]) {
  407. case '%':
  408. continue;
  409. case 'p':
  410. do_pointer:
  411. /* Find dereferencing fields */
  412. switch (fmt[i + 1]) {
  413. case 'B': case 'R': case 'r':
  414. case 'b': case 'M': case 'm':
  415. case 'I': case 'i': case 'E':
  416. case 'U': case 'V': case 'N':
  417. case 'a': case 'd': case 'D':
  418. case 'g': case 't': case 'C':
  419. case 'O': case 'f':
  420. if (WARN_ONCE(arg == 63,
  421. "Too many args for event: %s",
  422. trace_event_name(call)))
  423. return;
  424. dereference_flags |= 1ULL << arg;
  425. }
  426. break;
  427. default:
  428. {
  429. bool star = false;
  430. int j;
  431. /* Increment arg if %*s exists. */
  432. for (j = 0; fmt[i + j]; j++) {
  433. if (isdigit(fmt[i + j]) ||
  434. fmt[i + j] == '.')
  435. continue;
  436. if (fmt[i + j] == '*') {
  437. star = true;
  438. /* Handle %*pbl case */
  439. if (!j && fmt[i + 1] == 'p') {
  440. arg++;
  441. i++;
  442. goto do_pointer;
  443. }
  444. continue;
  445. }
  446. if ((fmt[i + j] == 's')) {
  447. if (star)
  448. arg++;
  449. if (WARN_ONCE(arg == 63,
  450. "Too many args for event: %s",
  451. trace_event_name(call)))
  452. return;
  453. dereference_flags |= 1ULL << arg;
  454. string_flags |= 1ULL << arg;
  455. }
  456. break;
  457. }
  458. break;
  459. } /* default */
  460. } /* switch */
  461. arg++;
  462. continue;
  463. case '(':
  464. if (in_quote)
  465. continue;
  466. parens++;
  467. continue;
  468. case ')':
  469. if (in_quote)
  470. continue;
  471. parens--;
  472. if (WARN_ONCE(parens < 0,
  473. "Paren mismatch for event: %s\narg='%s'\n%*s",
  474. trace_event_name(call),
  475. fmt + start_arg,
  476. (i - start_arg) + 5, "^"))
  477. return;
  478. continue;
  479. case ',':
  480. if (in_quote || parens)
  481. continue;
  482. e = i;
  483. i++;
  484. while (isspace(fmt[i]))
  485. i++;
  486. /*
  487. * If start_arg is zero, then this is the start of the
  488. * first argument. The processing of the argument happens
  489. * when the end of the argument is found, as it needs to
  490. * handle parenthesis and such.
  491. */
  492. if (!start_arg) {
  493. start_arg = i;
  494. /* Balance out the i++ in the for loop */
  495. i--;
  496. continue;
  497. }
  498. if (dereference_flags & (1ULL << arg)) {
  499. handle_dereference_arg(fmt + start_arg, string_flags,
  500. e - start_arg,
  501. &dereference_flags, arg, call);
  502. }
  503. start_arg = i;
  504. arg++;
  505. /* Balance out the i++ in the for loop */
  506. i--;
  507. }
  508. }
  509. if (dereference_flags & (1ULL << arg)) {
  510. handle_dereference_arg(fmt + start_arg, string_flags,
  511. i - start_arg,
  512. &dereference_flags, arg, call);
  513. }
  514. /*
  515. * If you triggered the below warning, the trace event reported
  516. * uses an unsafe dereference pointer %p*. As the data stored
  517. * at the trace event time may no longer exist when the trace
  518. * event is printed, dereferencing to the original source is
  519. * unsafe. The source of the dereference must be copied into the
  520. * event itself, and the dereference must access the copy instead.
  521. */
  522. if (WARN_ON_ONCE(dereference_flags)) {
  523. arg = 1;
  524. while (!(dereference_flags & 1)) {
  525. dereference_flags >>= 1;
  526. arg++;
  527. }
  528. pr_warn("event %s has unsafe dereference of argument %d\n",
  529. trace_event_name(call), arg);
  530. pr_warn("print_fmt: %s\n", fmt);
  531. }
  532. }
  533. int trace_event_raw_init(struct trace_event_call *call)
  534. {
  535. int id;
  536. id = register_trace_event(&call->event);
  537. if (!id)
  538. return -ENODEV;
  539. test_event_printk(call);
  540. return 0;
  541. }
  542. EXPORT_SYMBOL_GPL(trace_event_raw_init);
  543. bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
  544. {
  545. struct trace_array *tr = trace_file->tr;
  546. struct trace_pid_list *no_pid_list;
  547. struct trace_pid_list *pid_list;
  548. pid_list = rcu_dereference_raw(tr->filtered_pids);
  549. no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
  550. if (!pid_list && !no_pid_list)
  551. return false;
  552. /*
  553. * This is recorded at every sched_switch for this task.
  554. * Thus, even if the task migrates the ignore value will be the same.
  555. */
  556. return this_cpu_read(tr->array_buffer.data->ignore_pid) != 0;
  557. }
  558. EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
  559. /**
  560. * trace_event_buffer_reserve - reserve space on the ring buffer for an event
  561. * @fbuffer: information about how to save the event
  562. * @trace_file: the instance file descriptor for the event
  563. * @len: The length of the event
  564. *
  565. * The @fbuffer has information about the ring buffer and data will
  566. * be added to it to be used by the call to trace_event_buffer_commit().
  567. * The @trace_file is the desrciptor with information about the status
  568. * of the given event for a specific trace_array instance.
  569. * The @len is the length of data to save for the event.
  570. *
  571. * Returns a pointer to the data on the ring buffer or NULL if the
  572. * event was not reserved (event was filtered, too big, or the buffer
  573. * simply was disabled for write).
  574. */
  575. void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
  576. struct trace_event_file *trace_file,
  577. unsigned long len)
  578. {
  579. struct trace_event_call *event_call = trace_file->event_call;
  580. if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
  581. trace_event_ignore_this_pid(trace_file))
  582. return NULL;
  583. /*
  584. * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
  585. * preemption (adding one to the preempt_count). Since we are
  586. * interested in the preempt_count at the time the tracepoint was
  587. * hit, we need to subtract one to offset the increment.
  588. */
  589. fbuffer->trace_ctx = tracing_gen_ctx_dec();
  590. fbuffer->trace_file = trace_file;
  591. fbuffer->event =
  592. trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
  593. event_call->event.type, len,
  594. fbuffer->trace_ctx);
  595. if (!fbuffer->event)
  596. return NULL;
  597. fbuffer->regs = NULL;
  598. fbuffer->entry = ring_buffer_event_data(fbuffer->event);
  599. return fbuffer->entry;
  600. }
  601. EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
  602. int trace_event_reg(struct trace_event_call *call,
  603. enum trace_reg type, void *data)
  604. {
  605. struct trace_event_file *file = data;
  606. WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
  607. switch (type) {
  608. case TRACE_REG_REGISTER:
  609. return tracepoint_probe_register(call->tp,
  610. call->class->probe,
  611. file);
  612. case TRACE_REG_UNREGISTER:
  613. tracepoint_probe_unregister(call->tp,
  614. call->class->probe,
  615. file);
  616. return 0;
  617. #ifdef CONFIG_PERF_EVENTS
  618. case TRACE_REG_PERF_REGISTER:
  619. if (!call->class->perf_probe)
  620. return -ENODEV;
  621. return tracepoint_probe_register(call->tp,
  622. call->class->perf_probe,
  623. call);
  624. case TRACE_REG_PERF_UNREGISTER:
  625. tracepoint_probe_unregister(call->tp,
  626. call->class->perf_probe,
  627. call);
  628. return 0;
  629. case TRACE_REG_PERF_OPEN:
  630. case TRACE_REG_PERF_CLOSE:
  631. case TRACE_REG_PERF_ADD:
  632. case TRACE_REG_PERF_DEL:
  633. return 0;
  634. #endif
  635. }
  636. return 0;
  637. }
  638. EXPORT_SYMBOL_GPL(trace_event_reg);
  639. void trace_event_enable_cmd_record(bool enable)
  640. {
  641. struct trace_event_file *file;
  642. struct trace_array *tr;
  643. lockdep_assert_held(&event_mutex);
  644. do_for_each_event_file(tr, file) {
  645. if (!(file->flags & EVENT_FILE_FL_ENABLED))
  646. continue;
  647. if (enable) {
  648. tracing_start_cmdline_record();
  649. set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  650. } else {
  651. tracing_stop_cmdline_record();
  652. clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  653. }
  654. } while_for_each_event_file();
  655. }
  656. void trace_event_enable_tgid_record(bool enable)
  657. {
  658. struct trace_event_file *file;
  659. struct trace_array *tr;
  660. lockdep_assert_held(&event_mutex);
  661. do_for_each_event_file(tr, file) {
  662. if (!(file->flags & EVENT_FILE_FL_ENABLED))
  663. continue;
  664. if (enable) {
  665. tracing_start_tgid_record();
  666. set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
  667. } else {
  668. tracing_stop_tgid_record();
  669. clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
  670. &file->flags);
  671. }
  672. } while_for_each_event_file();
  673. }
  674. static int __ftrace_event_enable_disable(struct trace_event_file *file,
  675. int enable, int soft_disable)
  676. {
  677. struct trace_event_call *call = file->event_call;
  678. struct trace_array *tr = file->tr;
  679. bool soft_mode = atomic_read(&file->sm_ref) != 0;
  680. int ret = 0;
  681. int disable;
  682. switch (enable) {
  683. case 0:
  684. /*
  685. * When soft_disable is set and enable is cleared, the sm_ref
  686. * reference counter is decremented. If it reaches 0, we want
  687. * to clear the SOFT_DISABLED flag but leave the event in the
  688. * state that it was. That is, if the event was enabled and
  689. * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
  690. * is set we do not want the event to be enabled before we
  691. * clear the bit.
  692. *
  693. * When soft_disable is not set but the soft_mode is,
  694. * we do nothing. Do not disable the tracepoint, otherwise
  695. * "soft enable"s (clearing the SOFT_DISABLED bit) won't work.
  696. */
  697. if (soft_disable) {
  698. if (atomic_dec_return(&file->sm_ref) > 0)
  699. break;
  700. disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
  701. soft_mode = false;
  702. /* Disable use of trace_buffered_event */
  703. trace_buffered_event_disable();
  704. } else
  705. disable = !soft_mode;
  706. if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
  707. clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
  708. if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
  709. tracing_stop_cmdline_record();
  710. clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  711. }
  712. if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
  713. tracing_stop_tgid_record();
  714. clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
  715. }
  716. ret = call->class->reg(call, TRACE_REG_UNREGISTER, file);
  717. WARN_ON_ONCE(ret);
  718. }
  719. /* If in soft mode, just set the SOFT_DISABLE_BIT, else clear it */
  720. if (soft_mode)
  721. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  722. else
  723. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  724. break;
  725. case 1:
  726. /*
  727. * When soft_disable is set and enable is set, we want to
  728. * register the tracepoint for the event, but leave the event
  729. * as is. That means, if the event was already enabled, we do
  730. * nothing. If the event is disabled, we set SOFT_DISABLED
  731. * before enabling the event tracepoint, so it still seems
  732. * to be disabled.
  733. */
  734. if (!soft_disable)
  735. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  736. else {
  737. if (atomic_inc_return(&file->sm_ref) > 1)
  738. break;
  739. /* Enable use of trace_buffered_event */
  740. trace_buffered_event_enable();
  741. }
  742. if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
  743. bool cmd = false, tgid = false;
  744. /* Keep the event disabled, when going to soft mode. */
  745. if (soft_disable)
  746. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
  747. if (tr->trace_flags & TRACE_ITER(RECORD_CMD)) {
  748. cmd = true;
  749. tracing_start_cmdline_record();
  750. set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
  751. }
  752. if (tr->trace_flags & TRACE_ITER(RECORD_TGID)) {
  753. tgid = true;
  754. tracing_start_tgid_record();
  755. set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
  756. }
  757. ret = call->class->reg(call, TRACE_REG_REGISTER, file);
  758. if (ret) {
  759. if (cmd)
  760. tracing_stop_cmdline_record();
  761. if (tgid)
  762. tracing_stop_tgid_record();
  763. pr_info("event trace: Could not enable event "
  764. "%s\n", trace_event_name(call));
  765. break;
  766. }
  767. set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
  768. /* WAS_ENABLED gets set but never cleared. */
  769. set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
  770. }
  771. break;
  772. }
  773. return ret;
  774. }
  775. int trace_event_enable_disable(struct trace_event_file *file,
  776. int enable, int soft_disable)
  777. {
  778. return __ftrace_event_enable_disable(file, enable, soft_disable);
  779. }
  780. static int ftrace_event_enable_disable(struct trace_event_file *file,
  781. int enable)
  782. {
  783. return __ftrace_event_enable_disable(file, enable, 0);
  784. }
  785. #ifdef CONFIG_MODULES
  786. struct event_mod_load {
  787. struct list_head list;
  788. char *module;
  789. char *match;
  790. char *system;
  791. char *event;
  792. };
  793. static void free_event_mod(struct event_mod_load *event_mod)
  794. {
  795. list_del(&event_mod->list);
  796. kfree(event_mod->module);
  797. kfree(event_mod->match);
  798. kfree(event_mod->system);
  799. kfree(event_mod->event);
  800. kfree(event_mod);
  801. }
  802. static void clear_mod_events(struct trace_array *tr)
  803. {
  804. struct event_mod_load *event_mod, *n;
  805. list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) {
  806. free_event_mod(event_mod);
  807. }
  808. }
  809. static int remove_cache_mod(struct trace_array *tr, const char *mod,
  810. const char *match, const char *system, const char *event)
  811. {
  812. struct event_mod_load *event_mod, *n;
  813. int ret = -EINVAL;
  814. list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) {
  815. if (strcmp(event_mod->module, mod) != 0)
  816. continue;
  817. if (match && strcmp(event_mod->match, match) != 0)
  818. continue;
  819. if (system &&
  820. (!event_mod->system || strcmp(event_mod->system, system) != 0))
  821. continue;
  822. if (event &&
  823. (!event_mod->event || strcmp(event_mod->event, event) != 0))
  824. continue;
  825. free_event_mod(event_mod);
  826. ret = 0;
  827. }
  828. return ret;
  829. }
  830. static int cache_mod(struct trace_array *tr, const char *mod, int set,
  831. const char *match, const char *system, const char *event)
  832. {
  833. struct event_mod_load *event_mod;
  834. /* If the module exists, then this just failed to find an event */
  835. if (module_exists(mod))
  836. return -EINVAL;
  837. /* See if this is to remove a cached filter */
  838. if (!set)
  839. return remove_cache_mod(tr, mod, match, system, event);
  840. event_mod = kzalloc_obj(*event_mod);
  841. if (!event_mod)
  842. return -ENOMEM;
  843. INIT_LIST_HEAD(&event_mod->list);
  844. event_mod->module = kstrdup(mod, GFP_KERNEL);
  845. if (!event_mod->module)
  846. goto out_free;
  847. if (match) {
  848. event_mod->match = kstrdup(match, GFP_KERNEL);
  849. if (!event_mod->match)
  850. goto out_free;
  851. }
  852. if (system) {
  853. event_mod->system = kstrdup(system, GFP_KERNEL);
  854. if (!event_mod->system)
  855. goto out_free;
  856. }
  857. if (event) {
  858. event_mod->event = kstrdup(event, GFP_KERNEL);
  859. if (!event_mod->event)
  860. goto out_free;
  861. }
  862. list_add(&event_mod->list, &tr->mod_events);
  863. return 0;
  864. out_free:
  865. free_event_mod(event_mod);
  866. return -ENOMEM;
  867. }
  868. #else /* CONFIG_MODULES */
  869. static inline void clear_mod_events(struct trace_array *tr) { }
  870. static int cache_mod(struct trace_array *tr, const char *mod, int set,
  871. const char *match, const char *system, const char *event)
  872. {
  873. return -EINVAL;
  874. }
  875. #endif
  876. static void ftrace_clear_events(struct trace_array *tr)
  877. {
  878. struct trace_event_file *file;
  879. mutex_lock(&event_mutex);
  880. list_for_each_entry(file, &tr->events, list) {
  881. ftrace_event_enable_disable(file, 0);
  882. }
  883. clear_mod_events(tr);
  884. mutex_unlock(&event_mutex);
  885. }
  886. static void
  887. event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
  888. {
  889. struct trace_pid_list *pid_list;
  890. struct trace_array *tr = data;
  891. guard(preempt)();
  892. pid_list = rcu_dereference_raw(tr->filtered_pids);
  893. trace_filter_add_remove_task(pid_list, NULL, task);
  894. pid_list = rcu_dereference_raw(tr->filtered_no_pids);
  895. trace_filter_add_remove_task(pid_list, NULL, task);
  896. }
  897. static void
  898. event_filter_pid_sched_process_fork(void *data,
  899. struct task_struct *self,
  900. struct task_struct *task)
  901. {
  902. struct trace_pid_list *pid_list;
  903. struct trace_array *tr = data;
  904. guard(preempt)();
  905. pid_list = rcu_dereference_sched(tr->filtered_pids);
  906. trace_filter_add_remove_task(pid_list, self, task);
  907. pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  908. trace_filter_add_remove_task(pid_list, self, task);
  909. }
  910. void trace_event_follow_fork(struct trace_array *tr, bool enable)
  911. {
  912. if (enable) {
  913. register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
  914. tr, INT_MIN);
  915. register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
  916. tr, INT_MAX);
  917. } else {
  918. unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
  919. tr);
  920. unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
  921. tr);
  922. }
  923. }
  924. static void
  925. event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
  926. struct task_struct *prev,
  927. struct task_struct *next,
  928. unsigned int prev_state)
  929. {
  930. struct trace_array *tr = data;
  931. struct trace_pid_list *no_pid_list;
  932. struct trace_pid_list *pid_list;
  933. bool ret;
  934. pid_list = rcu_dereference_sched(tr->filtered_pids);
  935. no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  936. /*
  937. * Sched switch is funny, as we only want to ignore it
  938. * in the notrace case if both prev and next should be ignored.
  939. */
  940. ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
  941. trace_ignore_this_task(NULL, no_pid_list, next);
  942. this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
  943. (trace_ignore_this_task(pid_list, NULL, prev) &&
  944. trace_ignore_this_task(pid_list, NULL, next)));
  945. }
  946. static void
  947. event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
  948. struct task_struct *prev,
  949. struct task_struct *next,
  950. unsigned int prev_state)
  951. {
  952. struct trace_array *tr = data;
  953. struct trace_pid_list *no_pid_list;
  954. struct trace_pid_list *pid_list;
  955. pid_list = rcu_dereference_sched(tr->filtered_pids);
  956. no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  957. this_cpu_write(tr->array_buffer.data->ignore_pid,
  958. trace_ignore_this_task(pid_list, no_pid_list, next));
  959. }
  960. static void
  961. event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
  962. {
  963. struct trace_array *tr = data;
  964. struct trace_pid_list *no_pid_list;
  965. struct trace_pid_list *pid_list;
  966. /* Nothing to do if we are already tracing */
  967. if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
  968. return;
  969. pid_list = rcu_dereference_sched(tr->filtered_pids);
  970. no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  971. this_cpu_write(tr->array_buffer.data->ignore_pid,
  972. trace_ignore_this_task(pid_list, no_pid_list, task));
  973. }
  974. static void
  975. event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
  976. {
  977. struct trace_array *tr = data;
  978. struct trace_pid_list *no_pid_list;
  979. struct trace_pid_list *pid_list;
  980. /* Nothing to do if we are not tracing */
  981. if (this_cpu_read(tr->array_buffer.data->ignore_pid))
  982. return;
  983. pid_list = rcu_dereference_sched(tr->filtered_pids);
  984. no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  985. /* Set tracing if current is enabled */
  986. this_cpu_write(tr->array_buffer.data->ignore_pid,
  987. trace_ignore_this_task(pid_list, no_pid_list, current));
  988. }
  989. static void unregister_pid_events(struct trace_array *tr)
  990. {
  991. unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
  992. unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
  993. unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
  994. unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
  995. unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
  996. unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
  997. unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
  998. unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
  999. }
  1000. static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
  1001. {
  1002. struct trace_pid_list *pid_list;
  1003. struct trace_pid_list *no_pid_list;
  1004. struct trace_event_file *file;
  1005. int cpu;
  1006. pid_list = rcu_dereference_protected(tr->filtered_pids,
  1007. lockdep_is_held(&event_mutex));
  1008. no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
  1009. lockdep_is_held(&event_mutex));
  1010. /* Make sure there's something to do */
  1011. if (!pid_type_enabled(type, pid_list, no_pid_list))
  1012. return;
  1013. if (!still_need_pid_events(type, pid_list, no_pid_list)) {
  1014. unregister_pid_events(tr);
  1015. list_for_each_entry(file, &tr->events, list) {
  1016. clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
  1017. }
  1018. for_each_possible_cpu(cpu)
  1019. per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
  1020. }
  1021. if (type & TRACE_PIDS)
  1022. rcu_assign_pointer(tr->filtered_pids, NULL);
  1023. if (type & TRACE_NO_PIDS)
  1024. rcu_assign_pointer(tr->filtered_no_pids, NULL);
  1025. /* Wait till all users are no longer using pid filtering */
  1026. tracepoint_synchronize_unregister();
  1027. if ((type & TRACE_PIDS) && pid_list)
  1028. trace_pid_list_free(pid_list);
  1029. if ((type & TRACE_NO_PIDS) && no_pid_list)
  1030. trace_pid_list_free(no_pid_list);
  1031. }
  1032. static void ftrace_clear_event_pids(struct trace_array *tr, int type)
  1033. {
  1034. mutex_lock(&event_mutex);
  1035. __ftrace_clear_event_pids(tr, type);
  1036. mutex_unlock(&event_mutex);
  1037. }
  1038. static void __put_system(struct event_subsystem *system)
  1039. {
  1040. struct event_filter *filter = system->filter;
  1041. WARN_ON_ONCE(system_refcount(system) == 0);
  1042. if (system_refcount_dec(system))
  1043. return;
  1044. list_del(&system->list);
  1045. if (filter) {
  1046. kfree(filter->filter_string);
  1047. kfree(filter);
  1048. }
  1049. kfree_const(system->name);
  1050. kfree(system);
  1051. }
  1052. static void __get_system(struct event_subsystem *system)
  1053. {
  1054. WARN_ON_ONCE(system_refcount(system) == 0);
  1055. system_refcount_inc(system);
  1056. }
  1057. static void __get_system_dir(struct trace_subsystem_dir *dir)
  1058. {
  1059. WARN_ON_ONCE(dir->ref_count == 0);
  1060. dir->ref_count++;
  1061. __get_system(dir->subsystem);
  1062. }
  1063. static void __put_system_dir(struct trace_subsystem_dir *dir)
  1064. {
  1065. WARN_ON_ONCE(dir->ref_count == 0);
  1066. /* If the subsystem is about to be freed, the dir must be too */
  1067. WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
  1068. __put_system(dir->subsystem);
  1069. if (!--dir->ref_count)
  1070. kfree(dir);
  1071. }
  1072. static void put_system(struct trace_subsystem_dir *dir)
  1073. {
  1074. mutex_lock(&event_mutex);
  1075. __put_system_dir(dir);
  1076. mutex_unlock(&event_mutex);
  1077. }
  1078. static void remove_subsystem(struct trace_subsystem_dir *dir)
  1079. {
  1080. if (!dir)
  1081. return;
  1082. if (!--dir->nr_events) {
  1083. eventfs_remove_dir(dir->ei);
  1084. list_del(&dir->list);
  1085. __put_system_dir(dir);
  1086. }
  1087. }
  1088. void event_file_get(struct trace_event_file *file)
  1089. {
  1090. refcount_inc(&file->ref);
  1091. }
  1092. void event_file_put(struct trace_event_file *file)
  1093. {
  1094. if (WARN_ON_ONCE(!refcount_read(&file->ref))) {
  1095. if (file->flags & EVENT_FILE_FL_FREED)
  1096. kmem_cache_free(file_cachep, file);
  1097. return;
  1098. }
  1099. if (refcount_dec_and_test(&file->ref)) {
  1100. /* Count should only go to zero when it is freed */
  1101. if (WARN_ON_ONCE(!(file->flags & EVENT_FILE_FL_FREED)))
  1102. return;
  1103. kmem_cache_free(file_cachep, file);
  1104. }
  1105. }
  1106. static void remove_event_file_dir(struct trace_event_file *file)
  1107. {
  1108. eventfs_remove_dir(file->ei);
  1109. list_del(&file->list);
  1110. remove_subsystem(file->system);
  1111. free_event_filter(file->filter);
  1112. file->flags |= EVENT_FILE_FL_FREED;
  1113. event_file_put(file);
  1114. /* Wake up hist poll waiters to notice the EVENT_FILE_FL_FREED flag. */
  1115. hist_poll_wakeup();
  1116. }
  1117. /*
  1118. * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
  1119. */
  1120. static int
  1121. __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
  1122. const char *sub, const char *event, int set,
  1123. const char *mod)
  1124. {
  1125. struct trace_event_file *file;
  1126. struct trace_event_call *call;
  1127. char *module __free(kfree) = NULL;
  1128. const char *name;
  1129. int ret = -EINVAL;
  1130. int eret = 0;
  1131. if (mod) {
  1132. char *p;
  1133. module = kstrdup(mod, GFP_KERNEL);
  1134. if (!module)
  1135. return -ENOMEM;
  1136. /* Replace all '-' with '_' as that's what modules do */
  1137. for (p = strchr(module, '-'); p; p = strchr(p + 1, '-'))
  1138. *p = '_';
  1139. }
  1140. list_for_each_entry(file, &tr->events, list) {
  1141. call = file->event_call;
  1142. /* If a module is specified, skip events that are not that module */
  1143. if (module && (!call->module || strcmp(module_name(call->module), module)))
  1144. continue;
  1145. name = trace_event_name(call);
  1146. if (!name || !call->class || !call->class->reg)
  1147. continue;
  1148. if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
  1149. continue;
  1150. if (match &&
  1151. strcmp(match, name) != 0 &&
  1152. strcmp(match, call->class->system) != 0)
  1153. continue;
  1154. if (sub && strcmp(sub, call->class->system) != 0)
  1155. continue;
  1156. if (event && strcmp(event, name) != 0)
  1157. continue;
  1158. ret = ftrace_event_enable_disable(file, set);
  1159. /*
  1160. * Save the first error and return that. Some events
  1161. * may still have been enabled, but let the user
  1162. * know that something went wrong.
  1163. */
  1164. if (ret && !eret)
  1165. eret = ret;
  1166. ret = eret;
  1167. }
  1168. /*
  1169. * If this is a module setting and nothing was found,
  1170. * check if the module was loaded. If it wasn't cache it.
  1171. */
  1172. if (module && ret == -EINVAL && !eret)
  1173. ret = cache_mod(tr, module, set, match, sub, event);
  1174. return ret;
  1175. }
  1176. static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
  1177. const char *sub, const char *event, int set,
  1178. const char *mod)
  1179. {
  1180. int ret;
  1181. mutex_lock(&event_mutex);
  1182. ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set, mod);
  1183. mutex_unlock(&event_mutex);
  1184. return ret;
  1185. }
  1186. int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
  1187. {
  1188. char *event = NULL, *sub = NULL, *match, *mod;
  1189. int ret;
  1190. if (!tr)
  1191. return -ENOENT;
  1192. /* Modules events can be appended with :mod:<module> */
  1193. mod = strstr(buf, ":mod:");
  1194. if (mod) {
  1195. *mod = '\0';
  1196. /* move to the module name */
  1197. mod += 5;
  1198. }
  1199. /*
  1200. * The buf format can be <subsystem>:<event-name>
  1201. * *:<event-name> means any event by that name.
  1202. * :<event-name> is the same.
  1203. *
  1204. * <subsystem>:* means all events in that subsystem
  1205. * <subsystem>: means the same.
  1206. *
  1207. * <name> (no ':') means all events in a subsystem with
  1208. * the name <name> or any event that matches <name>
  1209. */
  1210. match = strsep(&buf, ":");
  1211. if (buf) {
  1212. sub = match;
  1213. event = buf;
  1214. match = NULL;
  1215. if (!strlen(sub) || strcmp(sub, "*") == 0)
  1216. sub = NULL;
  1217. if (!strlen(event) || strcmp(event, "*") == 0)
  1218. event = NULL;
  1219. } else if (mod) {
  1220. /* Allow wildcard for no length or star */
  1221. if (!strlen(match) || strcmp(match, "*") == 0)
  1222. match = NULL;
  1223. }
  1224. ret = __ftrace_set_clr_event(tr, match, sub, event, set, mod);
  1225. /* Put back the colon to allow this to be called again */
  1226. if (buf)
  1227. *(buf - 1) = ':';
  1228. return ret;
  1229. }
  1230. /**
  1231. * trace_set_clr_event - enable or disable an event
  1232. * @system: system name to match (NULL for any system)
  1233. * @event: event name to match (NULL for all events, within system)
  1234. * @set: 1 to enable, 0 to disable
  1235. *
  1236. * This is a way for other parts of the kernel to enable or disable
  1237. * event recording.
  1238. *
  1239. * Returns 0 on success, -EINVAL if the parameters do not match any
  1240. * registered events.
  1241. */
  1242. int trace_set_clr_event(const char *system, const char *event, int set)
  1243. {
  1244. struct trace_array *tr = top_trace_array();
  1245. if (!tr)
  1246. return -ENODEV;
  1247. return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL);
  1248. }
  1249. EXPORT_SYMBOL_GPL(trace_set_clr_event);
  1250. /**
  1251. * trace_array_set_clr_event - enable or disable an event for a trace array.
  1252. * @tr: concerned trace array.
  1253. * @system: system name to match (NULL for any system)
  1254. * @event: event name to match (NULL for all events, within system)
  1255. * @enable: true to enable, false to disable
  1256. *
  1257. * This is a way for other parts of the kernel to enable or disable
  1258. * event recording.
  1259. *
  1260. * Returns 0 on success, -EINVAL if the parameters do not match any
  1261. * registered events.
  1262. */
  1263. int trace_array_set_clr_event(struct trace_array *tr, const char *system,
  1264. const char *event, bool enable)
  1265. {
  1266. int set;
  1267. if (!tr)
  1268. return -ENOENT;
  1269. set = (enable == true) ? 1 : 0;
  1270. return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL);
  1271. }
  1272. EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
  1273. /* 128 should be much more than enough */
  1274. #define EVENT_BUF_SIZE 127
  1275. static ssize_t
  1276. ftrace_event_write(struct file *file, const char __user *ubuf,
  1277. size_t cnt, loff_t *ppos)
  1278. {
  1279. struct trace_parser parser;
  1280. struct seq_file *m = file->private_data;
  1281. struct trace_array *tr = m->private;
  1282. ssize_t read, ret;
  1283. if (!cnt)
  1284. return 0;
  1285. ret = tracing_update_buffers(tr);
  1286. if (ret < 0)
  1287. return ret;
  1288. if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
  1289. return -ENOMEM;
  1290. read = trace_get_user(&parser, ubuf, cnt, ppos);
  1291. if (read >= 0 && trace_parser_loaded((&parser))) {
  1292. int set = 1;
  1293. if (*parser.buffer == '!')
  1294. set = 0;
  1295. ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
  1296. if (ret)
  1297. goto out_put;
  1298. }
  1299. ret = read;
  1300. out_put:
  1301. trace_parser_put(&parser);
  1302. return ret;
  1303. }
  1304. static void *
  1305. t_next(struct seq_file *m, void *v, loff_t *pos)
  1306. {
  1307. struct trace_event_file *file = v;
  1308. struct trace_event_call *call;
  1309. struct trace_array *tr = m->private;
  1310. (*pos)++;
  1311. list_for_each_entry_continue(file, &tr->events, list) {
  1312. call = file->event_call;
  1313. /*
  1314. * The ftrace subsystem is for showing formats only.
  1315. * They can not be enabled or disabled via the event files.
  1316. */
  1317. if (call->class && call->class->reg &&
  1318. !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
  1319. return file;
  1320. }
  1321. return NULL;
  1322. }
  1323. static void *t_start(struct seq_file *m, loff_t *pos)
  1324. {
  1325. struct trace_event_file *file;
  1326. struct trace_array *tr = m->private;
  1327. loff_t l;
  1328. mutex_lock(&event_mutex);
  1329. file = list_entry(&tr->events, struct trace_event_file, list);
  1330. for (l = 0; l <= *pos; ) {
  1331. file = t_next(m, file, &l);
  1332. if (!file)
  1333. break;
  1334. }
  1335. return file;
  1336. }
  1337. enum set_event_iter_type {
  1338. SET_EVENT_FILE,
  1339. SET_EVENT_MOD,
  1340. };
  1341. struct set_event_iter {
  1342. enum set_event_iter_type type;
  1343. union {
  1344. struct trace_event_file *file;
  1345. struct event_mod_load *event_mod;
  1346. };
  1347. };
  1348. static void *
  1349. s_next(struct seq_file *m, void *v, loff_t *pos)
  1350. {
  1351. struct set_event_iter *iter = v;
  1352. struct trace_event_file *file;
  1353. struct trace_array *tr = m->private;
  1354. (*pos)++;
  1355. if (iter->type == SET_EVENT_FILE) {
  1356. file = iter->file;
  1357. list_for_each_entry_continue(file, &tr->events, list) {
  1358. if (file->flags & EVENT_FILE_FL_ENABLED) {
  1359. iter->file = file;
  1360. return iter;
  1361. }
  1362. }
  1363. #ifdef CONFIG_MODULES
  1364. iter->type = SET_EVENT_MOD;
  1365. iter->event_mod = list_entry(&tr->mod_events, struct event_mod_load, list);
  1366. #endif
  1367. }
  1368. #ifdef CONFIG_MODULES
  1369. list_for_each_entry_continue(iter->event_mod, &tr->mod_events, list)
  1370. return iter;
  1371. #endif
  1372. /*
  1373. * The iter is allocated in s_start() and passed via the 'v'
  1374. * parameter. To stop the iterator, NULL must be returned. But
  1375. * the return value is what the 'v' parameter in s_stop() receives
  1376. * and frees. Free iter here as it will no longer be used.
  1377. */
  1378. kfree(iter);
  1379. return NULL;
  1380. }
  1381. static void *s_start(struct seq_file *m, loff_t *pos)
  1382. {
  1383. struct trace_array *tr = m->private;
  1384. struct set_event_iter *iter;
  1385. loff_t l;
  1386. iter = kzalloc_obj(*iter);
  1387. mutex_lock(&event_mutex);
  1388. if (!iter)
  1389. return NULL;
  1390. iter->type = SET_EVENT_FILE;
  1391. iter->file = list_entry(&tr->events, struct trace_event_file, list);
  1392. for (l = 0; l <= *pos; ) {
  1393. iter = s_next(m, iter, &l);
  1394. if (!iter)
  1395. break;
  1396. }
  1397. return iter;
  1398. }
  1399. static int t_show(struct seq_file *m, void *v)
  1400. {
  1401. struct trace_event_file *file = v;
  1402. struct trace_event_call *call = file->event_call;
  1403. if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
  1404. seq_printf(m, "%s:", call->class->system);
  1405. seq_printf(m, "%s\n", trace_event_name(call));
  1406. return 0;
  1407. }
  1408. static void t_stop(struct seq_file *m, void *p)
  1409. {
  1410. mutex_unlock(&event_mutex);
  1411. }
  1412. static int get_call_len(struct trace_event_call *call)
  1413. {
  1414. int len;
  1415. /* Get the length of "<system>:<event>" */
  1416. len = strlen(call->class->system) + 1;
  1417. len += strlen(trace_event_name(call));
  1418. /* Set the index to 32 bytes to separate event from data */
  1419. return len >= 32 ? 1 : 32 - len;
  1420. }
  1421. /**
  1422. * t_show_filters - seq_file callback to display active event filters
  1423. * @m: The seq_file interface for formatted output
  1424. * @v: The current trace_event_file being iterated
  1425. *
  1426. * Identifies and prints active filters for the current event file in the
  1427. * iteration. If a filter is applied to the current event and, if so,
  1428. * prints the system name, event name, and the filter string.
  1429. */
  1430. static int t_show_filters(struct seq_file *m, void *v)
  1431. {
  1432. struct trace_event_file *file = v;
  1433. struct trace_event_call *call = file->event_call;
  1434. struct event_filter *filter;
  1435. int len;
  1436. guard(rcu)();
  1437. filter = rcu_dereference(file->filter);
  1438. if (!filter || !filter->filter_string)
  1439. return 0;
  1440. len = get_call_len(call);
  1441. seq_printf(m, "%s:%s%*.s%s\n", call->class->system,
  1442. trace_event_name(call), len, "", filter->filter_string);
  1443. return 0;
  1444. }
  1445. /**
  1446. * t_show_triggers - seq_file callback to display active event triggers
  1447. * @m: The seq_file interface for formatted output
  1448. * @v: The current trace_event_file being iterated
  1449. *
  1450. * Iterates through the trigger list of the current event file and prints
  1451. * each active trigger's configuration using its associated print
  1452. * operation.
  1453. */
  1454. static int t_show_triggers(struct seq_file *m, void *v)
  1455. {
  1456. struct trace_event_file *file = v;
  1457. struct trace_event_call *call = file->event_call;
  1458. struct event_trigger_data *data;
  1459. int len;
  1460. /*
  1461. * The event_mutex is held by t_start(), protecting the
  1462. * file->triggers list traversal.
  1463. */
  1464. if (list_empty(&file->triggers))
  1465. return 0;
  1466. len = get_call_len(call);
  1467. list_for_each_entry_rcu(data, &file->triggers, list) {
  1468. seq_printf(m, "%s:%s%*.s", call->class->system,
  1469. trace_event_name(call), len, "");
  1470. data->cmd_ops->print(m, data);
  1471. }
  1472. return 0;
  1473. }
  1474. #ifdef CONFIG_MODULES
  1475. static int s_show(struct seq_file *m, void *v)
  1476. {
  1477. struct set_event_iter *iter = v;
  1478. const char *system;
  1479. const char *event;
  1480. if (iter->type == SET_EVENT_FILE)
  1481. return t_show(m, iter->file);
  1482. /* When match is set, system and event are not */
  1483. if (iter->event_mod->match) {
  1484. seq_printf(m, "%s:mod:%s\n", iter->event_mod->match,
  1485. iter->event_mod->module);
  1486. return 0;
  1487. }
  1488. system = iter->event_mod->system ? : "*";
  1489. event = iter->event_mod->event ? : "*";
  1490. seq_printf(m, "%s:%s:mod:%s\n", system, event, iter->event_mod->module);
  1491. return 0;
  1492. }
  1493. #else /* CONFIG_MODULES */
  1494. static int s_show(struct seq_file *m, void *v)
  1495. {
  1496. struct set_event_iter *iter = v;
  1497. return t_show(m, iter->file);
  1498. }
  1499. #endif
  1500. static void s_stop(struct seq_file *m, void *v)
  1501. {
  1502. kfree(v);
  1503. t_stop(m, NULL);
  1504. }
  1505. static void *
  1506. __next(struct seq_file *m, void *v, loff_t *pos, int type)
  1507. {
  1508. struct trace_array *tr = m->private;
  1509. struct trace_pid_list *pid_list;
  1510. if (type == TRACE_PIDS)
  1511. pid_list = rcu_dereference_sched(tr->filtered_pids);
  1512. else
  1513. pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  1514. return trace_pid_next(pid_list, v, pos);
  1515. }
  1516. static void *
  1517. p_next(struct seq_file *m, void *v, loff_t *pos)
  1518. {
  1519. return __next(m, v, pos, TRACE_PIDS);
  1520. }
  1521. static void *
  1522. np_next(struct seq_file *m, void *v, loff_t *pos)
  1523. {
  1524. return __next(m, v, pos, TRACE_NO_PIDS);
  1525. }
  1526. static void *__start(struct seq_file *m, loff_t *pos, int type)
  1527. __acquires(RCU)
  1528. {
  1529. struct trace_pid_list *pid_list;
  1530. struct trace_array *tr = m->private;
  1531. /*
  1532. * Grab the mutex, to keep calls to p_next() having the same
  1533. * tr->filtered_pids as p_start() has.
  1534. * If we just passed the tr->filtered_pids around, then RCU would
  1535. * have been enough, but doing that makes things more complex.
  1536. */
  1537. mutex_lock(&event_mutex);
  1538. rcu_read_lock_sched();
  1539. if (type == TRACE_PIDS)
  1540. pid_list = rcu_dereference_sched(tr->filtered_pids);
  1541. else
  1542. pid_list = rcu_dereference_sched(tr->filtered_no_pids);
  1543. if (!pid_list)
  1544. return NULL;
  1545. return trace_pid_start(pid_list, pos);
  1546. }
  1547. static void *p_start(struct seq_file *m, loff_t *pos)
  1548. __acquires(RCU)
  1549. {
  1550. return __start(m, pos, TRACE_PIDS);
  1551. }
  1552. static void *np_start(struct seq_file *m, loff_t *pos)
  1553. __acquires(RCU)
  1554. {
  1555. return __start(m, pos, TRACE_NO_PIDS);
  1556. }
  1557. static void p_stop(struct seq_file *m, void *p)
  1558. __releases(RCU)
  1559. {
  1560. rcu_read_unlock_sched();
  1561. mutex_unlock(&event_mutex);
  1562. }
  1563. static ssize_t
  1564. event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  1565. loff_t *ppos)
  1566. {
  1567. struct trace_event_file *file;
  1568. unsigned long flags;
  1569. char buf[4] = "0";
  1570. mutex_lock(&event_mutex);
  1571. file = event_file_file(filp);
  1572. if (likely(file))
  1573. flags = file->flags;
  1574. mutex_unlock(&event_mutex);
  1575. if (!file)
  1576. return -ENODEV;
  1577. if (flags & EVENT_FILE_FL_ENABLED &&
  1578. !(flags & EVENT_FILE_FL_SOFT_DISABLED))
  1579. strcpy(buf, "1");
  1580. if (atomic_read(&file->sm_ref) != 0)
  1581. strcat(buf, "*");
  1582. strcat(buf, "\n");
  1583. return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
  1584. }
  1585. static ssize_t
  1586. event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1587. loff_t *ppos)
  1588. {
  1589. struct trace_event_file *file;
  1590. unsigned long val;
  1591. int ret;
  1592. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  1593. if (ret)
  1594. return ret;
  1595. guard(mutex)(&event_mutex);
  1596. switch (val) {
  1597. case 0:
  1598. case 1:
  1599. file = event_file_file(filp);
  1600. if (!file)
  1601. return -ENODEV;
  1602. ret = tracing_update_buffers(file->tr);
  1603. if (ret < 0)
  1604. return ret;
  1605. ret = ftrace_event_enable_disable(file, val);
  1606. if (ret < 0)
  1607. return ret;
  1608. break;
  1609. default:
  1610. return -EINVAL;
  1611. }
  1612. *ppos += cnt;
  1613. return cnt;
  1614. }
  1615. /*
  1616. * Returns:
  1617. * 0 : no events exist?
  1618. * 1 : all events are disabled
  1619. * 2 : all events are enabled
  1620. * 3 : some events are enabled and some are enabled
  1621. */
  1622. int trace_events_enabled(struct trace_array *tr, const char *system)
  1623. {
  1624. struct trace_event_call *call;
  1625. struct trace_event_file *file;
  1626. int set = 0;
  1627. guard(mutex)(&event_mutex);
  1628. list_for_each_entry(file, &tr->events, list) {
  1629. call = file->event_call;
  1630. if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
  1631. !trace_event_name(call) || !call->class || !call->class->reg)
  1632. continue;
  1633. if (system && strcmp(call->class->system, system) != 0)
  1634. continue;
  1635. /*
  1636. * We need to find out if all the events are set
  1637. * or if all events or cleared, or if we have
  1638. * a mixture.
  1639. */
  1640. set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
  1641. /*
  1642. * If we have a mixture, no need to look further.
  1643. */
  1644. if (set == 3)
  1645. break;
  1646. }
  1647. return set;
  1648. }
  1649. static ssize_t
  1650. system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
  1651. loff_t *ppos)
  1652. {
  1653. const char set_to_char[4] = { '?', '0', '1', 'X' };
  1654. struct trace_subsystem_dir *dir = filp->private_data;
  1655. struct event_subsystem *system = dir->subsystem;
  1656. struct trace_array *tr = dir->tr;
  1657. char buf[2];
  1658. int set;
  1659. int ret;
  1660. set = trace_events_enabled(tr, system ? system->name : NULL);
  1661. buf[0] = set_to_char[set];
  1662. buf[1] = '\n';
  1663. ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  1664. return ret;
  1665. }
  1666. static ssize_t
  1667. system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1668. loff_t *ppos)
  1669. {
  1670. struct trace_subsystem_dir *dir = filp->private_data;
  1671. struct event_subsystem *system = dir->subsystem;
  1672. const char *name = NULL;
  1673. unsigned long val;
  1674. ssize_t ret;
  1675. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  1676. if (ret)
  1677. return ret;
  1678. ret = tracing_update_buffers(dir->tr);
  1679. if (ret < 0)
  1680. return ret;
  1681. if (val != 0 && val != 1)
  1682. return -EINVAL;
  1683. /*
  1684. * Opening of "enable" adds a ref count to system,
  1685. * so the name is safe to use.
  1686. */
  1687. if (system)
  1688. name = system->name;
  1689. ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val, NULL);
  1690. if (ret)
  1691. goto out;
  1692. ret = cnt;
  1693. out:
  1694. *ppos += cnt;
  1695. return ret;
  1696. }
  1697. enum {
  1698. FORMAT_HEADER = 1,
  1699. FORMAT_FIELD_SEPERATOR = 2,
  1700. FORMAT_PRINTFMT = 3,
  1701. };
  1702. static void *f_next(struct seq_file *m, void *v, loff_t *pos)
  1703. {
  1704. struct trace_event_file *file = event_file_data(m->private);
  1705. struct trace_event_call *call = file->event_call;
  1706. struct list_head *common_head = &ftrace_common_fields;
  1707. struct list_head *head = trace_get_fields(call);
  1708. struct list_head *node = v;
  1709. (*pos)++;
  1710. switch ((unsigned long)v) {
  1711. case FORMAT_HEADER:
  1712. node = common_head;
  1713. break;
  1714. case FORMAT_FIELD_SEPERATOR:
  1715. node = head;
  1716. break;
  1717. case FORMAT_PRINTFMT:
  1718. /* all done */
  1719. return NULL;
  1720. }
  1721. node = node->prev;
  1722. if (node == common_head)
  1723. return (void *)FORMAT_FIELD_SEPERATOR;
  1724. else if (node == head)
  1725. return (void *)FORMAT_PRINTFMT;
  1726. else
  1727. return node;
  1728. }
  1729. static int f_show(struct seq_file *m, void *v)
  1730. {
  1731. struct trace_event_file *file = event_file_data(m->private);
  1732. struct trace_event_call *call = file->event_call;
  1733. struct ftrace_event_field *field;
  1734. const char *array_descriptor;
  1735. switch ((unsigned long)v) {
  1736. case FORMAT_HEADER:
  1737. seq_printf(m, "name: %s\n", trace_event_name(call));
  1738. seq_printf(m, "ID: %d\n", call->event.type);
  1739. seq_puts(m, "format:\n");
  1740. return 0;
  1741. case FORMAT_FIELD_SEPERATOR:
  1742. seq_putc(m, '\n');
  1743. return 0;
  1744. case FORMAT_PRINTFMT:
  1745. seq_printf(m, "\nprint fmt: %s\n",
  1746. call->print_fmt);
  1747. return 0;
  1748. }
  1749. field = list_entry(v, struct ftrace_event_field, link);
  1750. /*
  1751. * Smartly shows the array type(except dynamic array).
  1752. * Normal:
  1753. * field:TYPE VAR
  1754. * If TYPE := TYPE[LEN], it is shown:
  1755. * field:TYPE VAR[LEN]
  1756. */
  1757. array_descriptor = strchr(field->type, '[');
  1758. if (str_has_prefix(field->type, "__data_loc"))
  1759. array_descriptor = NULL;
  1760. if (!array_descriptor)
  1761. seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  1762. field->type, field->name, field->offset,
  1763. field->size, !!field->is_signed);
  1764. else if (field->len)
  1765. seq_printf(m, "\tfield:%.*s %s[%d];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  1766. (int)(array_descriptor - field->type),
  1767. field->type, field->name,
  1768. field->len, field->offset,
  1769. field->size, !!field->is_signed);
  1770. else
  1771. seq_printf(m, "\tfield:%.*s %s[];\toffset:%u;\tsize:%u;\tsigned:%d;\n",
  1772. (int)(array_descriptor - field->type),
  1773. field->type, field->name,
  1774. field->offset, field->size, !!field->is_signed);
  1775. return 0;
  1776. }
  1777. static void *f_start(struct seq_file *m, loff_t *pos)
  1778. {
  1779. struct trace_event_file *file;
  1780. void *p = (void *)FORMAT_HEADER;
  1781. loff_t l = 0;
  1782. /* ->stop() is called even if ->start() fails */
  1783. mutex_lock(&event_mutex);
  1784. file = event_file_file(m->private);
  1785. if (!file)
  1786. return ERR_PTR(-ENODEV);
  1787. while (l < *pos && p)
  1788. p = f_next(m, p, &l);
  1789. return p;
  1790. }
  1791. static void f_stop(struct seq_file *m, void *p)
  1792. {
  1793. mutex_unlock(&event_mutex);
  1794. }
  1795. static const struct seq_operations trace_format_seq_ops = {
  1796. .start = f_start,
  1797. .next = f_next,
  1798. .stop = f_stop,
  1799. .show = f_show,
  1800. };
  1801. static int trace_format_open(struct inode *inode, struct file *file)
  1802. {
  1803. struct seq_file *m;
  1804. int ret;
  1805. /* Do we want to hide event format files on tracefs lockdown? */
  1806. ret = seq_open(file, &trace_format_seq_ops);
  1807. if (ret < 0)
  1808. return ret;
  1809. m = file->private_data;
  1810. m->private = file;
  1811. return 0;
  1812. }
  1813. #ifdef CONFIG_PERF_EVENTS
  1814. static ssize_t
  1815. event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  1816. {
  1817. int id = (long)event_file_data(filp);
  1818. char buf[32];
  1819. int len;
  1820. if (unlikely(!id))
  1821. return -ENODEV;
  1822. len = sprintf(buf, "%d\n", id);
  1823. return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  1824. }
  1825. #endif
  1826. static ssize_t
  1827. event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  1828. loff_t *ppos)
  1829. {
  1830. struct trace_event_file *file;
  1831. struct trace_seq *s;
  1832. int r = -ENODEV;
  1833. if (*ppos)
  1834. return 0;
  1835. s = kmalloc_obj(*s);
  1836. if (!s)
  1837. return -ENOMEM;
  1838. trace_seq_init(s);
  1839. mutex_lock(&event_mutex);
  1840. file = event_file_file(filp);
  1841. if (file)
  1842. print_event_filter(file, s);
  1843. mutex_unlock(&event_mutex);
  1844. if (file)
  1845. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1846. s->buffer, trace_seq_used(s));
  1847. kfree(s);
  1848. return r;
  1849. }
  1850. static ssize_t
  1851. event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1852. loff_t *ppos)
  1853. {
  1854. struct trace_event_file *file;
  1855. char *buf;
  1856. int err = -ENODEV;
  1857. if (cnt >= PAGE_SIZE)
  1858. return -EINVAL;
  1859. buf = memdup_user_nul(ubuf, cnt);
  1860. if (IS_ERR(buf))
  1861. return PTR_ERR(buf);
  1862. mutex_lock(&event_mutex);
  1863. file = event_file_file(filp);
  1864. if (file) {
  1865. if (file->flags & EVENT_FILE_FL_FREED)
  1866. err = -ENODEV;
  1867. else
  1868. err = apply_event_filter(file, buf);
  1869. }
  1870. mutex_unlock(&event_mutex);
  1871. kfree(buf);
  1872. if (err < 0)
  1873. return err;
  1874. *ppos += cnt;
  1875. return cnt;
  1876. }
  1877. static LIST_HEAD(event_subsystems);
  1878. static int subsystem_open(struct inode *inode, struct file *filp)
  1879. {
  1880. struct trace_subsystem_dir *dir = NULL, *iter_dir;
  1881. struct trace_array *tr = NULL, *iter_tr;
  1882. struct event_subsystem *system = NULL;
  1883. int ret;
  1884. if (unlikely(tracing_disabled))
  1885. return -ENODEV;
  1886. /* Make sure the system still exists */
  1887. mutex_lock(&event_mutex);
  1888. mutex_lock(&trace_types_lock);
  1889. list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) {
  1890. list_for_each_entry(iter_dir, &iter_tr->systems, list) {
  1891. if (iter_dir == inode->i_private) {
  1892. /* Don't open systems with no events */
  1893. tr = iter_tr;
  1894. dir = iter_dir;
  1895. if (dir->nr_events) {
  1896. __get_system_dir(dir);
  1897. system = dir->subsystem;
  1898. }
  1899. goto exit_loop;
  1900. }
  1901. }
  1902. }
  1903. exit_loop:
  1904. mutex_unlock(&trace_types_lock);
  1905. mutex_unlock(&event_mutex);
  1906. if (!system)
  1907. return -ENODEV;
  1908. /* Still need to increment the ref count of the system */
  1909. if (trace_array_get(tr) < 0) {
  1910. put_system(dir);
  1911. return -ENODEV;
  1912. }
  1913. ret = tracing_open_generic(inode, filp);
  1914. if (ret < 0) {
  1915. trace_array_put(tr);
  1916. put_system(dir);
  1917. }
  1918. return ret;
  1919. }
  1920. static int system_tr_open(struct inode *inode, struct file *filp)
  1921. {
  1922. struct trace_subsystem_dir *dir;
  1923. struct trace_array *tr = inode->i_private;
  1924. int ret;
  1925. /* Make a temporary dir that has no system but points to tr */
  1926. dir = kzalloc_obj(*dir);
  1927. if (!dir)
  1928. return -ENOMEM;
  1929. ret = tracing_open_generic_tr(inode, filp);
  1930. if (ret < 0) {
  1931. kfree(dir);
  1932. return ret;
  1933. }
  1934. dir->tr = tr;
  1935. filp->private_data = dir;
  1936. return 0;
  1937. }
  1938. static int subsystem_release(struct inode *inode, struct file *file)
  1939. {
  1940. struct trace_subsystem_dir *dir = file->private_data;
  1941. trace_array_put(dir->tr);
  1942. /*
  1943. * If dir->subsystem is NULL, then this is a temporary
  1944. * descriptor that was made for a trace_array to enable
  1945. * all subsystems.
  1946. */
  1947. if (dir->subsystem)
  1948. put_system(dir);
  1949. else
  1950. kfree(dir);
  1951. return 0;
  1952. }
  1953. static ssize_t
  1954. subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
  1955. loff_t *ppos)
  1956. {
  1957. struct trace_subsystem_dir *dir = filp->private_data;
  1958. struct event_subsystem *system = dir->subsystem;
  1959. struct trace_seq *s;
  1960. int r;
  1961. if (*ppos)
  1962. return 0;
  1963. s = kmalloc_obj(*s);
  1964. if (!s)
  1965. return -ENOMEM;
  1966. trace_seq_init(s);
  1967. print_subsystem_event_filter(system, s);
  1968. r = simple_read_from_buffer(ubuf, cnt, ppos,
  1969. s->buffer, trace_seq_used(s));
  1970. kfree(s);
  1971. return r;
  1972. }
  1973. static ssize_t
  1974. subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1975. loff_t *ppos)
  1976. {
  1977. struct trace_subsystem_dir *dir = filp->private_data;
  1978. char *buf;
  1979. int err;
  1980. if (cnt >= PAGE_SIZE)
  1981. return -EINVAL;
  1982. buf = memdup_user_nul(ubuf, cnt);
  1983. if (IS_ERR(buf))
  1984. return PTR_ERR(buf);
  1985. err = apply_subsystem_event_filter(dir, buf);
  1986. kfree(buf);
  1987. if (err < 0)
  1988. return err;
  1989. *ppos += cnt;
  1990. return cnt;
  1991. }
  1992. static ssize_t
  1993. show_header_page_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  1994. {
  1995. struct trace_array *tr = filp->private_data;
  1996. struct trace_seq *s;
  1997. int r;
  1998. if (*ppos)
  1999. return 0;
  2000. s = kmalloc_obj(*s);
  2001. if (!s)
  2002. return -ENOMEM;
  2003. trace_seq_init(s);
  2004. ring_buffer_print_page_header(tr->array_buffer.buffer, s);
  2005. r = simple_read_from_buffer(ubuf, cnt, ppos,
  2006. s->buffer, trace_seq_used(s));
  2007. kfree(s);
  2008. return r;
  2009. }
  2010. static ssize_t
  2011. show_header_event_file(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  2012. {
  2013. struct trace_seq *s;
  2014. int r;
  2015. if (*ppos)
  2016. return 0;
  2017. s = kmalloc_obj(*s);
  2018. if (!s)
  2019. return -ENOMEM;
  2020. trace_seq_init(s);
  2021. ring_buffer_print_entry_header(s);
  2022. r = simple_read_from_buffer(ubuf, cnt, ppos,
  2023. s->buffer, trace_seq_used(s));
  2024. kfree(s);
  2025. return r;
  2026. }
  2027. static void ignore_task_cpu(void *data)
  2028. {
  2029. struct trace_array *tr = data;
  2030. struct trace_pid_list *pid_list;
  2031. struct trace_pid_list *no_pid_list;
  2032. /*
  2033. * This function is called by on_each_cpu() while the
  2034. * event_mutex is held.
  2035. */
  2036. pid_list = rcu_dereference_protected(tr->filtered_pids,
  2037. mutex_is_locked(&event_mutex));
  2038. no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
  2039. mutex_is_locked(&event_mutex));
  2040. this_cpu_write(tr->array_buffer.data->ignore_pid,
  2041. trace_ignore_this_task(pid_list, no_pid_list, current));
  2042. }
  2043. static void register_pid_events(struct trace_array *tr)
  2044. {
  2045. /*
  2046. * Register a probe that is called before all other probes
  2047. * to set ignore_pid if next or prev do not match.
  2048. * Register a probe this is called after all other probes
  2049. * to only keep ignore_pid set if next pid matches.
  2050. */
  2051. register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
  2052. tr, INT_MAX);
  2053. register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
  2054. tr, 0);
  2055. register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
  2056. tr, INT_MAX);
  2057. register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
  2058. tr, 0);
  2059. register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
  2060. tr, INT_MAX);
  2061. register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
  2062. tr, 0);
  2063. register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
  2064. tr, INT_MAX);
  2065. register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
  2066. tr, 0);
  2067. }
  2068. static ssize_t
  2069. event_pid_write(struct file *filp, const char __user *ubuf,
  2070. size_t cnt, loff_t *ppos, int type)
  2071. {
  2072. struct seq_file *m = filp->private_data;
  2073. struct trace_array *tr = m->private;
  2074. struct trace_pid_list *filtered_pids = NULL;
  2075. struct trace_pid_list *other_pids = NULL;
  2076. struct trace_pid_list *pid_list;
  2077. struct trace_event_file *file;
  2078. ssize_t ret;
  2079. if (!cnt)
  2080. return 0;
  2081. ret = tracing_update_buffers(tr);
  2082. if (ret < 0)
  2083. return ret;
  2084. guard(mutex)(&event_mutex);
  2085. if (type == TRACE_PIDS) {
  2086. filtered_pids = rcu_dereference_protected(tr->filtered_pids,
  2087. lockdep_is_held(&event_mutex));
  2088. other_pids = rcu_dereference_protected(tr->filtered_no_pids,
  2089. lockdep_is_held(&event_mutex));
  2090. } else {
  2091. filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
  2092. lockdep_is_held(&event_mutex));
  2093. other_pids = rcu_dereference_protected(tr->filtered_pids,
  2094. lockdep_is_held(&event_mutex));
  2095. }
  2096. ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
  2097. if (ret < 0)
  2098. return ret;
  2099. if (type == TRACE_PIDS)
  2100. rcu_assign_pointer(tr->filtered_pids, pid_list);
  2101. else
  2102. rcu_assign_pointer(tr->filtered_no_pids, pid_list);
  2103. list_for_each_entry(file, &tr->events, list) {
  2104. set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
  2105. }
  2106. if (filtered_pids) {
  2107. tracepoint_synchronize_unregister();
  2108. trace_pid_list_free(filtered_pids);
  2109. } else if (pid_list && !other_pids) {
  2110. register_pid_events(tr);
  2111. }
  2112. /*
  2113. * Ignoring of pids is done at task switch. But we have to
  2114. * check for those tasks that are currently running.
  2115. * Always do this in case a pid was appended or removed.
  2116. */
  2117. on_each_cpu(ignore_task_cpu, tr, 1);
  2118. *ppos += ret;
  2119. return ret;
  2120. }
  2121. static ssize_t
  2122. ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
  2123. size_t cnt, loff_t *ppos)
  2124. {
  2125. return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
  2126. }
  2127. static ssize_t
  2128. ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
  2129. size_t cnt, loff_t *ppos)
  2130. {
  2131. return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
  2132. }
  2133. static int ftrace_event_avail_open(struct inode *inode, struct file *file);
  2134. static int ftrace_event_set_open(struct inode *inode, struct file *file);
  2135. static int ftrace_event_show_filters_open(struct inode *inode, struct file *file);
  2136. static int ftrace_event_show_triggers_open(struct inode *inode, struct file *file);
  2137. static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
  2138. static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
  2139. static int ftrace_event_release(struct inode *inode, struct file *file);
  2140. static const struct seq_operations show_event_seq_ops = {
  2141. .start = t_start,
  2142. .next = t_next,
  2143. .show = t_show,
  2144. .stop = t_stop,
  2145. };
  2146. static const struct seq_operations show_set_event_seq_ops = {
  2147. .start = s_start,
  2148. .next = s_next,
  2149. .show = s_show,
  2150. .stop = s_stop,
  2151. };
  2152. static const struct seq_operations show_show_event_filters_seq_ops = {
  2153. .start = t_start,
  2154. .next = t_next,
  2155. .show = t_show_filters,
  2156. .stop = t_stop,
  2157. };
  2158. static const struct seq_operations show_show_event_triggers_seq_ops = {
  2159. .start = t_start,
  2160. .next = t_next,
  2161. .show = t_show_triggers,
  2162. .stop = t_stop,
  2163. };
  2164. static const struct seq_operations show_set_pid_seq_ops = {
  2165. .start = p_start,
  2166. .next = p_next,
  2167. .show = trace_pid_show,
  2168. .stop = p_stop,
  2169. };
  2170. static const struct seq_operations show_set_no_pid_seq_ops = {
  2171. .start = np_start,
  2172. .next = np_next,
  2173. .show = trace_pid_show,
  2174. .stop = p_stop,
  2175. };
  2176. static const struct file_operations ftrace_avail_fops = {
  2177. .open = ftrace_event_avail_open,
  2178. .read = seq_read,
  2179. .llseek = seq_lseek,
  2180. .release = seq_release,
  2181. };
  2182. static const struct file_operations ftrace_set_event_fops = {
  2183. .open = ftrace_event_set_open,
  2184. .read = seq_read,
  2185. .write = ftrace_event_write,
  2186. .llseek = seq_lseek,
  2187. .release = ftrace_event_release,
  2188. };
  2189. static const struct file_operations ftrace_show_event_filters_fops = {
  2190. .open = ftrace_event_show_filters_open,
  2191. .read = seq_read,
  2192. .llseek = seq_lseek,
  2193. .release = seq_release,
  2194. };
  2195. static const struct file_operations ftrace_show_event_triggers_fops = {
  2196. .open = ftrace_event_show_triggers_open,
  2197. .read = seq_read,
  2198. .llseek = seq_lseek,
  2199. .release = seq_release,
  2200. };
  2201. static const struct file_operations ftrace_set_event_pid_fops = {
  2202. .open = ftrace_event_set_pid_open,
  2203. .read = seq_read,
  2204. .write = ftrace_event_pid_write,
  2205. .llseek = seq_lseek,
  2206. .release = ftrace_event_release,
  2207. };
  2208. static const struct file_operations ftrace_set_event_notrace_pid_fops = {
  2209. .open = ftrace_event_set_npid_open,
  2210. .read = seq_read,
  2211. .write = ftrace_event_npid_write,
  2212. .llseek = seq_lseek,
  2213. .release = ftrace_event_release,
  2214. };
  2215. static const struct file_operations ftrace_enable_fops = {
  2216. .open = tracing_open_file_tr,
  2217. .read = event_enable_read,
  2218. .write = event_enable_write,
  2219. .release = tracing_release_file_tr,
  2220. .llseek = default_llseek,
  2221. };
  2222. static const struct file_operations ftrace_event_format_fops = {
  2223. .open = trace_format_open,
  2224. .read = seq_read,
  2225. .llseek = seq_lseek,
  2226. .release = seq_release,
  2227. };
  2228. #ifdef CONFIG_PERF_EVENTS
  2229. static const struct file_operations ftrace_event_id_fops = {
  2230. .read = event_id_read,
  2231. .llseek = default_llseek,
  2232. };
  2233. #endif
  2234. static const struct file_operations ftrace_event_filter_fops = {
  2235. .open = tracing_open_file_tr,
  2236. .read = event_filter_read,
  2237. .write = event_filter_write,
  2238. .release = tracing_release_file_tr,
  2239. .llseek = default_llseek,
  2240. };
  2241. static const struct file_operations ftrace_subsystem_filter_fops = {
  2242. .open = subsystem_open,
  2243. .read = subsystem_filter_read,
  2244. .write = subsystem_filter_write,
  2245. .llseek = default_llseek,
  2246. .release = subsystem_release,
  2247. };
  2248. static const struct file_operations ftrace_system_enable_fops = {
  2249. .open = subsystem_open,
  2250. .read = system_enable_read,
  2251. .write = system_enable_write,
  2252. .llseek = default_llseek,
  2253. .release = subsystem_release,
  2254. };
  2255. static const struct file_operations ftrace_tr_enable_fops = {
  2256. .open = system_tr_open,
  2257. .read = system_enable_read,
  2258. .write = system_enable_write,
  2259. .llseek = default_llseek,
  2260. .release = subsystem_release,
  2261. };
  2262. static const struct file_operations ftrace_show_header_page_fops = {
  2263. .open = tracing_open_generic_tr,
  2264. .read = show_header_page_file,
  2265. .llseek = default_llseek,
  2266. .release = tracing_release_generic_tr,
  2267. };
  2268. static const struct file_operations ftrace_show_header_event_fops = {
  2269. .open = tracing_open_generic_tr,
  2270. .read = show_header_event_file,
  2271. .llseek = default_llseek,
  2272. .release = tracing_release_generic_tr,
  2273. };
  2274. static int
  2275. ftrace_event_open(struct inode *inode, struct file *file,
  2276. const struct seq_operations *seq_ops)
  2277. {
  2278. struct seq_file *m;
  2279. int ret;
  2280. ret = security_locked_down(LOCKDOWN_TRACEFS);
  2281. if (ret)
  2282. return ret;
  2283. ret = seq_open(file, seq_ops);
  2284. if (ret < 0)
  2285. return ret;
  2286. m = file->private_data;
  2287. /* copy tr over to seq ops */
  2288. m->private = inode->i_private;
  2289. return ret;
  2290. }
  2291. static int ftrace_event_release(struct inode *inode, struct file *file)
  2292. {
  2293. struct trace_array *tr = inode->i_private;
  2294. trace_array_put(tr);
  2295. return seq_release(inode, file);
  2296. }
  2297. static int
  2298. ftrace_event_avail_open(struct inode *inode, struct file *file)
  2299. {
  2300. const struct seq_operations *seq_ops = &show_event_seq_ops;
  2301. /* Checks for tracefs lockdown */
  2302. return ftrace_event_open(inode, file, seq_ops);
  2303. }
  2304. static int
  2305. ftrace_event_set_open(struct inode *inode, struct file *file)
  2306. {
  2307. const struct seq_operations *seq_ops = &show_set_event_seq_ops;
  2308. struct trace_array *tr = inode->i_private;
  2309. int ret;
  2310. ret = tracing_check_open_get_tr(tr);
  2311. if (ret)
  2312. return ret;
  2313. if ((file->f_mode & FMODE_WRITE) &&
  2314. (file->f_flags & O_TRUNC))
  2315. ftrace_clear_events(tr);
  2316. ret = ftrace_event_open(inode, file, seq_ops);
  2317. if (ret < 0)
  2318. trace_array_put(tr);
  2319. return ret;
  2320. }
  2321. /**
  2322. * ftrace_event_show_filters_open - open interface for set_event_filters
  2323. * @inode: The inode of the file
  2324. * @file: The file being opened
  2325. *
  2326. * Connects the set_event_filters file to the sequence operations
  2327. * required to iterate over and display active event filters.
  2328. */
  2329. static int
  2330. ftrace_event_show_filters_open(struct inode *inode, struct file *file)
  2331. {
  2332. return ftrace_event_open(inode, file, &show_show_event_filters_seq_ops);
  2333. }
  2334. /**
  2335. * ftrace_event_show_triggers_open - open interface for show_event_triggers
  2336. * @inode: The inode of the file
  2337. * @file: The file being opened
  2338. *
  2339. * Connects the show_event_triggers file to the sequence operations
  2340. * required to iterate over and display active event triggers.
  2341. */
  2342. static int
  2343. ftrace_event_show_triggers_open(struct inode *inode, struct file *file)
  2344. {
  2345. return ftrace_event_open(inode, file, &show_show_event_triggers_seq_ops);
  2346. }
  2347. static int
  2348. ftrace_event_set_pid_open(struct inode *inode, struct file *file)
  2349. {
  2350. const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
  2351. struct trace_array *tr = inode->i_private;
  2352. int ret;
  2353. ret = tracing_check_open_get_tr(tr);
  2354. if (ret)
  2355. return ret;
  2356. if ((file->f_mode & FMODE_WRITE) &&
  2357. (file->f_flags & O_TRUNC))
  2358. ftrace_clear_event_pids(tr, TRACE_PIDS);
  2359. ret = ftrace_event_open(inode, file, seq_ops);
  2360. if (ret < 0)
  2361. trace_array_put(tr);
  2362. return ret;
  2363. }
  2364. static int
  2365. ftrace_event_set_npid_open(struct inode *inode, struct file *file)
  2366. {
  2367. const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
  2368. struct trace_array *tr = inode->i_private;
  2369. int ret;
  2370. ret = tracing_check_open_get_tr(tr);
  2371. if (ret)
  2372. return ret;
  2373. if ((file->f_mode & FMODE_WRITE) &&
  2374. (file->f_flags & O_TRUNC))
  2375. ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
  2376. ret = ftrace_event_open(inode, file, seq_ops);
  2377. if (ret < 0)
  2378. trace_array_put(tr);
  2379. return ret;
  2380. }
  2381. static struct event_subsystem *
  2382. create_new_subsystem(const char *name)
  2383. {
  2384. struct event_subsystem *system;
  2385. /* need to create new entry */
  2386. system = kmalloc_obj(*system);
  2387. if (!system)
  2388. return NULL;
  2389. system->ref_count = 1;
  2390. /* Only allocate if dynamic (kprobes and modules) */
  2391. system->name = kstrdup_const(name, GFP_KERNEL);
  2392. if (!system->name)
  2393. goto out_free;
  2394. system->filter = kzalloc_obj(struct event_filter);
  2395. if (!system->filter)
  2396. goto out_free;
  2397. list_add(&system->list, &event_subsystems);
  2398. return system;
  2399. out_free:
  2400. kfree_const(system->name);
  2401. kfree(system);
  2402. return NULL;
  2403. }
  2404. static int system_callback(const char *name, umode_t *mode, void **data,
  2405. const struct file_operations **fops)
  2406. {
  2407. if (strcmp(name, "filter") == 0)
  2408. *fops = &ftrace_subsystem_filter_fops;
  2409. else if (strcmp(name, "enable") == 0)
  2410. *fops = &ftrace_system_enable_fops;
  2411. else
  2412. return 0;
  2413. *mode = TRACE_MODE_WRITE;
  2414. return 1;
  2415. }
  2416. static struct eventfs_inode *
  2417. event_subsystem_dir(struct trace_array *tr, const char *name,
  2418. struct trace_event_file *file, struct eventfs_inode *parent)
  2419. {
  2420. struct event_subsystem *system, *iter;
  2421. struct trace_subsystem_dir *dir;
  2422. struct eventfs_inode *ei;
  2423. int nr_entries;
  2424. static struct eventfs_entry system_entries[] = {
  2425. {
  2426. .name = "filter",
  2427. .callback = system_callback,
  2428. },
  2429. {
  2430. .name = "enable",
  2431. .callback = system_callback,
  2432. }
  2433. };
  2434. /* First see if we did not already create this dir */
  2435. list_for_each_entry(dir, &tr->systems, list) {
  2436. system = dir->subsystem;
  2437. if (strcmp(system->name, name) == 0) {
  2438. dir->nr_events++;
  2439. file->system = dir;
  2440. return dir->ei;
  2441. }
  2442. }
  2443. /* Now see if the system itself exists. */
  2444. system = NULL;
  2445. list_for_each_entry(iter, &event_subsystems, list) {
  2446. if (strcmp(iter->name, name) == 0) {
  2447. system = iter;
  2448. break;
  2449. }
  2450. }
  2451. dir = kmalloc_obj(*dir);
  2452. if (!dir)
  2453. goto out_fail;
  2454. if (!system) {
  2455. system = create_new_subsystem(name);
  2456. if (!system)
  2457. goto out_free;
  2458. } else
  2459. __get_system(system);
  2460. /* ftrace only has directories no files */
  2461. if (strcmp(name, "ftrace") == 0)
  2462. nr_entries = 0;
  2463. else
  2464. nr_entries = ARRAY_SIZE(system_entries);
  2465. ei = eventfs_create_dir(name, parent, system_entries, nr_entries, dir);
  2466. if (IS_ERR(ei)) {
  2467. pr_warn("Failed to create system directory %s\n", name);
  2468. __put_system(system);
  2469. goto out_free;
  2470. }
  2471. dir->ei = ei;
  2472. dir->tr = tr;
  2473. dir->ref_count = 1;
  2474. dir->nr_events = 1;
  2475. dir->subsystem = system;
  2476. file->system = dir;
  2477. list_add(&dir->list, &tr->systems);
  2478. return dir->ei;
  2479. out_free:
  2480. kfree(dir);
  2481. out_fail:
  2482. /* Only print this message if failed on memory allocation */
  2483. if (!dir || !system)
  2484. pr_warn("No memory to create event subsystem %s\n", name);
  2485. return NULL;
  2486. }
  2487. static int
  2488. event_define_fields(struct trace_event_call *call)
  2489. {
  2490. struct list_head *head;
  2491. int ret = 0;
  2492. /*
  2493. * Other events may have the same class. Only update
  2494. * the fields if they are not already defined.
  2495. */
  2496. head = trace_get_fields(call);
  2497. if (list_empty(head)) {
  2498. struct trace_event_fields *field = call->class->fields_array;
  2499. unsigned int offset = sizeof(struct trace_entry);
  2500. for (; field->type; field++) {
  2501. if (field->type == TRACE_FUNCTION_TYPE) {
  2502. field->define_fields(call);
  2503. break;
  2504. }
  2505. offset = ALIGN(offset, field->align);
  2506. ret = trace_define_field_ext(call, field->type, field->name,
  2507. offset, field->size,
  2508. field->is_signed, field->filter_type,
  2509. field->len, field->needs_test);
  2510. if (WARN_ON_ONCE(ret)) {
  2511. pr_err("error code is %d\n", ret);
  2512. break;
  2513. }
  2514. offset += field->size;
  2515. }
  2516. }
  2517. return ret;
  2518. }
  2519. static int event_callback(const char *name, umode_t *mode, void **data,
  2520. const struct file_operations **fops)
  2521. {
  2522. struct trace_event_file *file = *data;
  2523. struct trace_event_call *call = file->event_call;
  2524. if (strcmp(name, "format") == 0) {
  2525. *mode = TRACE_MODE_READ;
  2526. *fops = &ftrace_event_format_fops;
  2527. return 1;
  2528. }
  2529. /*
  2530. * Only event directories that can be enabled should have
  2531. * triggers or filters, with the exception of the "print"
  2532. * event that can have a "trigger" file.
  2533. */
  2534. if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
  2535. if (call->class->reg && strcmp(name, "enable") == 0) {
  2536. *mode = TRACE_MODE_WRITE;
  2537. *fops = &ftrace_enable_fops;
  2538. return 1;
  2539. }
  2540. if (strcmp(name, "filter") == 0) {
  2541. *mode = TRACE_MODE_WRITE;
  2542. *fops = &ftrace_event_filter_fops;
  2543. return 1;
  2544. }
  2545. }
  2546. if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
  2547. strcmp(trace_event_name(call), "print") == 0) {
  2548. if (strcmp(name, "trigger") == 0) {
  2549. *mode = TRACE_MODE_WRITE;
  2550. *fops = &event_trigger_fops;
  2551. return 1;
  2552. }
  2553. }
  2554. #ifdef CONFIG_PERF_EVENTS
  2555. if (call->event.type && call->class->reg &&
  2556. strcmp(name, "id") == 0) {
  2557. *mode = TRACE_MODE_READ;
  2558. *data = (void *)(long)call->event.type;
  2559. *fops = &ftrace_event_id_fops;
  2560. return 1;
  2561. }
  2562. #endif
  2563. #ifdef CONFIG_HIST_TRIGGERS
  2564. if (strcmp(name, "hist") == 0) {
  2565. *mode = TRACE_MODE_READ;
  2566. *fops = &event_hist_fops;
  2567. return 1;
  2568. }
  2569. #endif
  2570. #ifdef CONFIG_HIST_TRIGGERS_DEBUG
  2571. if (strcmp(name, "hist_debug") == 0) {
  2572. *mode = TRACE_MODE_READ;
  2573. *fops = &event_hist_debug_fops;
  2574. return 1;
  2575. }
  2576. #endif
  2577. #ifdef CONFIG_TRACE_EVENT_INJECT
  2578. if (call->event.type && call->class->reg &&
  2579. strcmp(name, "inject") == 0) {
  2580. *mode = 0200;
  2581. *fops = &event_inject_fops;
  2582. return 1;
  2583. }
  2584. #endif
  2585. return 0;
  2586. }
  2587. /* The file is incremented on creation and freeing the enable file decrements it */
  2588. static void event_release(const char *name, void *data)
  2589. {
  2590. struct trace_event_file *file = data;
  2591. event_file_put(file);
  2592. }
  2593. static int
  2594. event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file)
  2595. {
  2596. struct trace_event_call *call = file->event_call;
  2597. struct trace_array *tr = file->tr;
  2598. struct eventfs_inode *e_events;
  2599. struct eventfs_inode *ei;
  2600. const char *name;
  2601. int nr_entries;
  2602. int ret;
  2603. static struct eventfs_entry event_entries[] = {
  2604. {
  2605. .name = "enable",
  2606. .callback = event_callback,
  2607. .release = event_release,
  2608. },
  2609. {
  2610. .name = "filter",
  2611. .callback = event_callback,
  2612. },
  2613. {
  2614. .name = "trigger",
  2615. .callback = event_callback,
  2616. },
  2617. {
  2618. .name = "format",
  2619. .callback = event_callback,
  2620. },
  2621. #ifdef CONFIG_PERF_EVENTS
  2622. {
  2623. .name = "id",
  2624. .callback = event_callback,
  2625. },
  2626. #endif
  2627. #ifdef CONFIG_HIST_TRIGGERS
  2628. {
  2629. .name = "hist",
  2630. .callback = event_callback,
  2631. },
  2632. #endif
  2633. #ifdef CONFIG_HIST_TRIGGERS_DEBUG
  2634. {
  2635. .name = "hist_debug",
  2636. .callback = event_callback,
  2637. },
  2638. #endif
  2639. #ifdef CONFIG_TRACE_EVENT_INJECT
  2640. {
  2641. .name = "inject",
  2642. .callback = event_callback,
  2643. },
  2644. #endif
  2645. };
  2646. /*
  2647. * If the trace point header did not define TRACE_SYSTEM
  2648. * then the system would be called "TRACE_SYSTEM". This should
  2649. * never happen.
  2650. */
  2651. if (WARN_ON_ONCE(strcmp(call->class->system, TRACE_SYSTEM) == 0))
  2652. return -ENODEV;
  2653. e_events = event_subsystem_dir(tr, call->class->system, file, parent);
  2654. if (!e_events)
  2655. return -ENOMEM;
  2656. nr_entries = ARRAY_SIZE(event_entries);
  2657. name = trace_event_name(call);
  2658. ei = eventfs_create_dir(name, e_events, event_entries, nr_entries, file);
  2659. if (IS_ERR(ei)) {
  2660. pr_warn("Could not create tracefs '%s' directory\n", name);
  2661. return -1;
  2662. }
  2663. file->ei = ei;
  2664. ret = event_define_fields(call);
  2665. if (ret < 0) {
  2666. pr_warn("Could not initialize trace point events/%s\n", name);
  2667. return ret;
  2668. }
  2669. /* Gets decremented on freeing of the "enable" file */
  2670. event_file_get(file);
  2671. return 0;
  2672. }
  2673. static void remove_event_from_tracers(struct trace_event_call *call)
  2674. {
  2675. struct trace_event_file *file;
  2676. struct trace_array *tr;
  2677. do_for_each_event_file_safe(tr, file) {
  2678. if (file->event_call != call)
  2679. continue;
  2680. remove_event_file_dir(file);
  2681. /*
  2682. * The do_for_each_event_file_safe() is
  2683. * a double loop. After finding the call for this
  2684. * trace_array, we use break to jump to the next
  2685. * trace_array.
  2686. */
  2687. break;
  2688. } while_for_each_event_file();
  2689. }
  2690. static void event_remove(struct trace_event_call *call)
  2691. {
  2692. struct trace_array *tr;
  2693. struct trace_event_file *file;
  2694. do_for_each_event_file(tr, file) {
  2695. if (file->event_call != call)
  2696. continue;
  2697. if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
  2698. tr->clear_trace = true;
  2699. ftrace_event_enable_disable(file, 0);
  2700. /*
  2701. * The do_for_each_event_file() is
  2702. * a double loop. After finding the call for this
  2703. * trace_array, we use break to jump to the next
  2704. * trace_array.
  2705. */
  2706. break;
  2707. } while_for_each_event_file();
  2708. if (call->event.funcs)
  2709. __unregister_trace_event(&call->event);
  2710. remove_event_from_tracers(call);
  2711. list_del(&call->list);
  2712. }
  2713. static int event_init(struct trace_event_call *call)
  2714. {
  2715. int ret = 0;
  2716. const char *name;
  2717. name = trace_event_name(call);
  2718. if (WARN_ON(!name))
  2719. return -EINVAL;
  2720. if (call->class->raw_init) {
  2721. ret = call->class->raw_init(call);
  2722. if (ret < 0 && ret != -ENOSYS)
  2723. pr_warn("Could not initialize trace events/%s\n", name);
  2724. }
  2725. return ret;
  2726. }
  2727. static int
  2728. __register_event(struct trace_event_call *call, struct module *mod)
  2729. {
  2730. int ret;
  2731. ret = event_init(call);
  2732. if (ret < 0)
  2733. return ret;
  2734. down_write(&trace_event_sem);
  2735. list_add(&call->list, &ftrace_events);
  2736. up_write(&trace_event_sem);
  2737. if (call->flags & TRACE_EVENT_FL_DYNAMIC)
  2738. atomic_set(&call->refcnt, 0);
  2739. else
  2740. call->module = mod;
  2741. return 0;
  2742. }
  2743. static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
  2744. {
  2745. int rlen;
  2746. int elen;
  2747. /* Find the length of the eval value as a string */
  2748. elen = snprintf(ptr, 0, "%ld", map->eval_value);
  2749. /* Make sure there's enough room to replace the string with the value */
  2750. if (len < elen)
  2751. return NULL;
  2752. snprintf(ptr, elen + 1, "%ld", map->eval_value);
  2753. /* Get the rest of the string of ptr */
  2754. rlen = strlen(ptr + len);
  2755. memmove(ptr + elen, ptr + len, rlen);
  2756. /* Make sure we end the new string */
  2757. ptr[elen + rlen] = 0;
  2758. return ptr + elen;
  2759. }
  2760. static void update_event_printk(struct trace_event_call *call,
  2761. struct trace_eval_map *map)
  2762. {
  2763. char *ptr;
  2764. int quote = 0;
  2765. int len = strlen(map->eval_string);
  2766. for (ptr = call->print_fmt; *ptr; ptr++) {
  2767. if (*ptr == '\\') {
  2768. ptr++;
  2769. /* paranoid */
  2770. if (!*ptr)
  2771. break;
  2772. continue;
  2773. }
  2774. if (*ptr == '"') {
  2775. quote ^= 1;
  2776. continue;
  2777. }
  2778. if (quote)
  2779. continue;
  2780. if (isdigit(*ptr)) {
  2781. /* skip numbers */
  2782. do {
  2783. ptr++;
  2784. /* Check for alpha chars like ULL */
  2785. } while (isalnum(*ptr));
  2786. if (!*ptr)
  2787. break;
  2788. /*
  2789. * A number must have some kind of delimiter after
  2790. * it, and we can ignore that too.
  2791. */
  2792. continue;
  2793. }
  2794. if (isalpha(*ptr) || *ptr == '_') {
  2795. if (strncmp(map->eval_string, ptr, len) == 0 &&
  2796. !isalnum(ptr[len]) && ptr[len] != '_') {
  2797. ptr = eval_replace(ptr, map, len);
  2798. /* enum/sizeof string smaller than value */
  2799. if (WARN_ON_ONCE(!ptr))
  2800. return;
  2801. /*
  2802. * No need to decrement here, as eval_replace()
  2803. * returns the pointer to the character passed
  2804. * the eval, and two evals can not be placed
  2805. * back to back without something in between.
  2806. * We can skip that something in between.
  2807. */
  2808. continue;
  2809. }
  2810. skip_more:
  2811. do {
  2812. ptr++;
  2813. } while (isalnum(*ptr) || *ptr == '_');
  2814. if (!*ptr)
  2815. break;
  2816. /*
  2817. * If what comes after this variable is a '.' or
  2818. * '->' then we can continue to ignore that string.
  2819. */
  2820. if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
  2821. ptr += *ptr == '.' ? 1 : 2;
  2822. if (!*ptr)
  2823. break;
  2824. goto skip_more;
  2825. }
  2826. /*
  2827. * Once again, we can skip the delimiter that came
  2828. * after the string.
  2829. */
  2830. continue;
  2831. }
  2832. }
  2833. }
  2834. static void add_str_to_module(struct module *module, char *str)
  2835. {
  2836. struct module_string *modstr;
  2837. modstr = kmalloc_obj(*modstr);
  2838. /*
  2839. * If we failed to allocate memory here, then we'll just
  2840. * let the str memory leak when the module is removed.
  2841. * If this fails to allocate, there's worse problems than
  2842. * a leaked string on module removal.
  2843. */
  2844. if (WARN_ON_ONCE(!modstr))
  2845. return;
  2846. modstr->module = module;
  2847. modstr->str = str;
  2848. list_add(&modstr->next, &module_strings);
  2849. }
  2850. #define ATTRIBUTE_STR "__attribute__("
  2851. #define ATTRIBUTE_STR_LEN (sizeof(ATTRIBUTE_STR) - 1)
  2852. /* Remove all __attribute__() from @type. Return allocated string or @type. */
  2853. static char *sanitize_field_type(const char *type)
  2854. {
  2855. char *attr, *tmp, *next, *ret = (char *)type;
  2856. int depth;
  2857. next = (char *)type;
  2858. while ((attr = strstr(next, ATTRIBUTE_STR))) {
  2859. /* Retry if "__attribute__(" is a part of another word. */
  2860. if (attr != next && !isspace(attr[-1])) {
  2861. next = attr + ATTRIBUTE_STR_LEN;
  2862. continue;
  2863. }
  2864. if (ret == type) {
  2865. ret = kstrdup(type, GFP_KERNEL);
  2866. if (WARN_ON_ONCE(!ret))
  2867. return NULL;
  2868. attr = ret + (attr - type);
  2869. }
  2870. /* the ATTRIBUTE_STR already has the first '(' */
  2871. depth = 1;
  2872. next = attr + ATTRIBUTE_STR_LEN;
  2873. do {
  2874. tmp = strpbrk(next, "()");
  2875. /* There is unbalanced parentheses */
  2876. if (WARN_ON_ONCE(!tmp)) {
  2877. kfree(ret);
  2878. return (char *)type;
  2879. }
  2880. if (*tmp == '(')
  2881. depth++;
  2882. else
  2883. depth--;
  2884. next = tmp + 1;
  2885. } while (depth > 0);
  2886. next = skip_spaces(next);
  2887. strcpy(attr, next);
  2888. next = attr;
  2889. }
  2890. return ret;
  2891. }
  2892. static char *find_replacable_eval(const char *type, const char *eval_string,
  2893. int len)
  2894. {
  2895. char *ptr;
  2896. if (!eval_string)
  2897. return NULL;
  2898. ptr = strchr(type, '[');
  2899. if (!ptr)
  2900. return NULL;
  2901. ptr++;
  2902. if (!isalpha(*ptr) && *ptr != '_')
  2903. return NULL;
  2904. if (strncmp(eval_string, ptr, len) != 0)
  2905. return NULL;
  2906. return ptr;
  2907. }
  2908. static void update_event_fields(struct trace_event_call *call,
  2909. struct trace_eval_map *map)
  2910. {
  2911. struct ftrace_event_field *field;
  2912. const char *eval_string = NULL;
  2913. struct list_head *head;
  2914. int len = 0;
  2915. char *ptr;
  2916. char *str;
  2917. /* Dynamic events should never have field maps */
  2918. if (call->flags & TRACE_EVENT_FL_DYNAMIC)
  2919. return;
  2920. if (map) {
  2921. eval_string = map->eval_string;
  2922. len = strlen(map->eval_string);
  2923. }
  2924. head = trace_get_fields(call);
  2925. list_for_each_entry(field, head, link) {
  2926. str = sanitize_field_type(field->type);
  2927. if (!str)
  2928. return;
  2929. ptr = find_replacable_eval(str, eval_string, len);
  2930. if (ptr) {
  2931. if (str == field->type) {
  2932. str = kstrdup(field->type, GFP_KERNEL);
  2933. if (WARN_ON_ONCE(!str))
  2934. return;
  2935. ptr = str + (ptr - field->type);
  2936. }
  2937. ptr = eval_replace(ptr, map, len);
  2938. /* enum/sizeof string smaller than value */
  2939. if (WARN_ON_ONCE(!ptr)) {
  2940. kfree(str);
  2941. continue;
  2942. }
  2943. }
  2944. if (str == field->type)
  2945. continue;
  2946. /*
  2947. * If the event is part of a module, then we need to free the string
  2948. * when the module is removed. Otherwise, it will stay allocated
  2949. * until a reboot.
  2950. */
  2951. if (call->module)
  2952. add_str_to_module(call->module, str);
  2953. field->type = str;
  2954. if (field->filter_type == FILTER_OTHER)
  2955. field->filter_type = filter_assign_type(field->type);
  2956. }
  2957. }
  2958. /* Update all events for replacing eval and sanitizing */
  2959. void trace_event_update_all(struct trace_eval_map **map, int len)
  2960. {
  2961. struct trace_event_call *call, *p;
  2962. const char *last_system = NULL;
  2963. bool first = false;
  2964. bool updated;
  2965. int last_i;
  2966. int i;
  2967. down_write(&trace_event_sem);
  2968. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  2969. /* events are usually grouped together with systems */
  2970. if (!last_system || call->class->system != last_system) {
  2971. first = true;
  2972. last_i = 0;
  2973. last_system = call->class->system;
  2974. }
  2975. updated = false;
  2976. /*
  2977. * Since calls are grouped by systems, the likelihood that the
  2978. * next call in the iteration belongs to the same system as the
  2979. * previous call is high. As an optimization, we skip searching
  2980. * for a map[] that matches the call's system if the last call
  2981. * was from the same system. That's what last_i is for. If the
  2982. * call has the same system as the previous call, then last_i
  2983. * will be the index of the first map[] that has a matching
  2984. * system.
  2985. */
  2986. for (i = last_i; i < len; i++) {
  2987. if (call->class->system == map[i]->system) {
  2988. /* Save the first system if need be */
  2989. if (first) {
  2990. last_i = i;
  2991. first = false;
  2992. }
  2993. update_event_printk(call, map[i]);
  2994. update_event_fields(call, map[i]);
  2995. updated = true;
  2996. }
  2997. }
  2998. /* If not updated yet, update field for sanitizing. */
  2999. if (!updated)
  3000. update_event_fields(call, NULL);
  3001. cond_resched();
  3002. }
  3003. up_write(&trace_event_sem);
  3004. }
  3005. static bool event_in_systems(struct trace_event_call *call,
  3006. const char *systems)
  3007. {
  3008. const char *system;
  3009. const char *p;
  3010. if (!systems)
  3011. return true;
  3012. system = call->class->system;
  3013. p = strstr(systems, system);
  3014. if (!p)
  3015. return false;
  3016. if (p != systems && !isspace(*(p - 1)) && *(p - 1) != ',')
  3017. return false;
  3018. p += strlen(system);
  3019. return !*p || isspace(*p) || *p == ',';
  3020. }
  3021. #ifdef CONFIG_HIST_TRIGGERS
  3022. /*
  3023. * Wake up waiter on the hist_poll_wq from irq_work because the hist trigger
  3024. * may happen in any context.
  3025. */
  3026. static void hist_poll_event_irq_work(struct irq_work *work)
  3027. {
  3028. wake_up_all(&hist_poll_wq);
  3029. }
  3030. DEFINE_IRQ_WORK(hist_poll_work, hist_poll_event_irq_work);
  3031. DECLARE_WAIT_QUEUE_HEAD(hist_poll_wq);
  3032. #endif
  3033. static struct trace_event_file *
  3034. trace_create_new_event(struct trace_event_call *call,
  3035. struct trace_array *tr)
  3036. {
  3037. struct trace_pid_list *no_pid_list;
  3038. struct trace_pid_list *pid_list;
  3039. struct trace_event_file *file;
  3040. unsigned int first;
  3041. if (!event_in_systems(call, tr->system_names))
  3042. return NULL;
  3043. file = kmem_cache_alloc(file_cachep, GFP_TRACE);
  3044. if (!file)
  3045. return ERR_PTR(-ENOMEM);
  3046. pid_list = rcu_dereference_protected(tr->filtered_pids,
  3047. lockdep_is_held(&event_mutex));
  3048. no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
  3049. lockdep_is_held(&event_mutex));
  3050. if (!trace_pid_list_first(pid_list, &first) ||
  3051. !trace_pid_list_first(no_pid_list, &first))
  3052. file->flags |= EVENT_FILE_FL_PID_FILTER;
  3053. file->event_call = call;
  3054. file->tr = tr;
  3055. atomic_set(&file->sm_ref, 0);
  3056. atomic_set(&file->tm_ref, 0);
  3057. INIT_LIST_HEAD(&file->triggers);
  3058. list_add(&file->list, &tr->events);
  3059. refcount_set(&file->ref, 1);
  3060. return file;
  3061. }
  3062. #define MAX_BOOT_TRIGGERS 32
  3063. static struct boot_triggers {
  3064. const char *event;
  3065. char *trigger;
  3066. } bootup_triggers[MAX_BOOT_TRIGGERS];
  3067. static char bootup_trigger_buf[COMMAND_LINE_SIZE];
  3068. static int nr_boot_triggers;
  3069. static __init int setup_trace_triggers(char *str)
  3070. {
  3071. char *trigger;
  3072. char *buf;
  3073. int i;
  3074. strscpy(bootup_trigger_buf, str, COMMAND_LINE_SIZE);
  3075. trace_set_ring_buffer_expanded(NULL);
  3076. disable_tracing_selftest("running event triggers");
  3077. buf = bootup_trigger_buf;
  3078. for (i = 0; i < MAX_BOOT_TRIGGERS; i++) {
  3079. trigger = strsep(&buf, ",");
  3080. if (!trigger)
  3081. break;
  3082. bootup_triggers[i].event = strsep(&trigger, ".");
  3083. bootup_triggers[i].trigger = trigger;
  3084. if (!bootup_triggers[i].trigger)
  3085. break;
  3086. }
  3087. nr_boot_triggers = i;
  3088. return 1;
  3089. }
  3090. __setup("trace_trigger=", setup_trace_triggers);
  3091. /* Add an event to a trace directory */
  3092. static int
  3093. __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
  3094. {
  3095. struct trace_event_file *file;
  3096. file = trace_create_new_event(call, tr);
  3097. /*
  3098. * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed
  3099. * allocation, or NULL if the event is not part of the tr->system_names.
  3100. * When the event is not part of the tr->system_names, return zero, not
  3101. * an error.
  3102. */
  3103. if (!file)
  3104. return 0;
  3105. if (IS_ERR(file))
  3106. return PTR_ERR(file);
  3107. if (eventdir_initialized)
  3108. return event_create_dir(tr->event_dir, file);
  3109. else
  3110. return event_define_fields(call);
  3111. }
  3112. static void trace_early_triggers(struct trace_event_file *file, const char *name)
  3113. {
  3114. int ret;
  3115. int i;
  3116. for (i = 0; i < nr_boot_triggers; i++) {
  3117. if (strcmp(name, bootup_triggers[i].event))
  3118. continue;
  3119. mutex_lock(&event_mutex);
  3120. ret = trigger_process_regex(file, bootup_triggers[i].trigger);
  3121. mutex_unlock(&event_mutex);
  3122. if (ret)
  3123. pr_err("Failed to register trigger '%s' on event %s\n",
  3124. bootup_triggers[i].trigger,
  3125. bootup_triggers[i].event);
  3126. }
  3127. }
  3128. /*
  3129. * Just create a descriptor for early init. A descriptor is required
  3130. * for enabling events at boot. We want to enable events before
  3131. * the filesystem is initialized.
  3132. */
  3133. static int
  3134. __trace_early_add_new_event(struct trace_event_call *call,
  3135. struct trace_array *tr)
  3136. {
  3137. struct trace_event_file *file;
  3138. int ret;
  3139. file = trace_create_new_event(call, tr);
  3140. /*
  3141. * trace_create_new_event() returns ERR_PTR(-ENOMEM) if failed
  3142. * allocation, or NULL if the event is not part of the tr->system_names.
  3143. * When the event is not part of the tr->system_names, return zero, not
  3144. * an error.
  3145. */
  3146. if (!file)
  3147. return 0;
  3148. if (IS_ERR(file))
  3149. return PTR_ERR(file);
  3150. ret = event_define_fields(call);
  3151. if (ret)
  3152. return ret;
  3153. trace_early_triggers(file, trace_event_name(call));
  3154. return 0;
  3155. }
  3156. struct ftrace_module_file_ops;
  3157. static void __add_event_to_tracers(struct trace_event_call *call);
  3158. /* Add an additional event_call dynamically */
  3159. int trace_add_event_call(struct trace_event_call *call)
  3160. {
  3161. int ret;
  3162. lockdep_assert_held(&event_mutex);
  3163. guard(mutex)(&trace_types_lock);
  3164. ret = __register_event(call, NULL);
  3165. if (ret < 0)
  3166. return ret;
  3167. __add_event_to_tracers(call);
  3168. return ret;
  3169. }
  3170. EXPORT_SYMBOL_GPL(trace_add_event_call);
  3171. /*
  3172. * Must be called under locking of trace_types_lock, event_mutex and
  3173. * trace_event_sem.
  3174. */
  3175. static void __trace_remove_event_call(struct trace_event_call *call)
  3176. {
  3177. event_remove(call);
  3178. trace_destroy_fields(call);
  3179. }
  3180. static int probe_remove_event_call(struct trace_event_call *call)
  3181. {
  3182. struct trace_array *tr;
  3183. struct trace_event_file *file;
  3184. #ifdef CONFIG_PERF_EVENTS
  3185. if (call->perf_refcount)
  3186. return -EBUSY;
  3187. #endif
  3188. do_for_each_event_file(tr, file) {
  3189. if (file->event_call != call)
  3190. continue;
  3191. /*
  3192. * We can't rely on ftrace_event_enable_disable(enable => 0)
  3193. * we are going to do, soft mode can suppress
  3194. * TRACE_REG_UNREGISTER.
  3195. */
  3196. if (file->flags & EVENT_FILE_FL_ENABLED)
  3197. goto busy;
  3198. if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
  3199. tr->clear_trace = true;
  3200. /*
  3201. * The do_for_each_event_file_safe() is
  3202. * a double loop. After finding the call for this
  3203. * trace_array, we use break to jump to the next
  3204. * trace_array.
  3205. */
  3206. break;
  3207. } while_for_each_event_file();
  3208. __trace_remove_event_call(call);
  3209. return 0;
  3210. busy:
  3211. /* No need to clear the trace now */
  3212. list_for_each_entry(tr, &ftrace_trace_arrays, list) {
  3213. tr->clear_trace = false;
  3214. }
  3215. return -EBUSY;
  3216. }
  3217. /* Remove an event_call */
  3218. int trace_remove_event_call(struct trace_event_call *call)
  3219. {
  3220. int ret;
  3221. lockdep_assert_held(&event_mutex);
  3222. mutex_lock(&trace_types_lock);
  3223. down_write(&trace_event_sem);
  3224. ret = probe_remove_event_call(call);
  3225. up_write(&trace_event_sem);
  3226. mutex_unlock(&trace_types_lock);
  3227. return ret;
  3228. }
  3229. EXPORT_SYMBOL_GPL(trace_remove_event_call);
  3230. #define for_each_event(event, start, end) \
  3231. for (event = start; \
  3232. (unsigned long)event < (unsigned long)end; \
  3233. event++)
  3234. #ifdef CONFIG_MODULES
  3235. static void update_mod_cache(struct trace_array *tr, struct module *mod)
  3236. {
  3237. struct event_mod_load *event_mod, *n;
  3238. list_for_each_entry_safe(event_mod, n, &tr->mod_events, list) {
  3239. if (strcmp(event_mod->module, mod->name) != 0)
  3240. continue;
  3241. __ftrace_set_clr_event_nolock(tr, event_mod->match,
  3242. event_mod->system,
  3243. event_mod->event, 1, mod->name);
  3244. free_event_mod(event_mod);
  3245. }
  3246. }
  3247. static void update_cache_events(struct module *mod)
  3248. {
  3249. struct trace_array *tr;
  3250. list_for_each_entry(tr, &ftrace_trace_arrays, list)
  3251. update_mod_cache(tr, mod);
  3252. }
  3253. static void trace_module_add_events(struct module *mod)
  3254. {
  3255. struct trace_event_call **call, **start, **end;
  3256. if (!mod->num_trace_events)
  3257. return;
  3258. /* Don't add infrastructure for mods without tracepoints */
  3259. if (trace_module_has_bad_taint(mod)) {
  3260. pr_err("%s: module has bad taint, not creating trace events\n",
  3261. mod->name);
  3262. return;
  3263. }
  3264. start = mod->trace_events;
  3265. end = mod->trace_events + mod->num_trace_events;
  3266. for_each_event(call, start, end) {
  3267. __register_event(*call, mod);
  3268. __add_event_to_tracers(*call);
  3269. }
  3270. update_cache_events(mod);
  3271. }
  3272. static void trace_module_remove_events(struct module *mod)
  3273. {
  3274. struct trace_event_call *call, *p;
  3275. struct module_string *modstr, *m;
  3276. down_write(&trace_event_sem);
  3277. list_for_each_entry_safe(call, p, &ftrace_events, list) {
  3278. if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module)
  3279. continue;
  3280. if (call->module == mod)
  3281. __trace_remove_event_call(call);
  3282. }
  3283. /* Check for any strings allocated for this module */
  3284. list_for_each_entry_safe(modstr, m, &module_strings, next) {
  3285. if (modstr->module != mod)
  3286. continue;
  3287. list_del(&modstr->next);
  3288. kfree(modstr->str);
  3289. kfree(modstr);
  3290. }
  3291. up_write(&trace_event_sem);
  3292. /*
  3293. * It is safest to reset the ring buffer if the module being unloaded
  3294. * registered any events that were used. The only worry is if
  3295. * a new module gets loaded, and takes on the same id as the events
  3296. * of this module. When printing out the buffer, traced events left
  3297. * over from this module may be passed to the new module events and
  3298. * unexpected results may occur.
  3299. */
  3300. tracing_reset_all_online_cpus_unlocked();
  3301. }
  3302. static int trace_module_notify(struct notifier_block *self,
  3303. unsigned long val, void *data)
  3304. {
  3305. struct module *mod = data;
  3306. mutex_lock(&event_mutex);
  3307. mutex_lock(&trace_types_lock);
  3308. switch (val) {
  3309. case MODULE_STATE_COMING:
  3310. trace_module_add_events(mod);
  3311. break;
  3312. case MODULE_STATE_GOING:
  3313. trace_module_remove_events(mod);
  3314. break;
  3315. }
  3316. mutex_unlock(&trace_types_lock);
  3317. mutex_unlock(&event_mutex);
  3318. return NOTIFY_OK;
  3319. }
  3320. static struct notifier_block trace_module_nb = {
  3321. .notifier_call = trace_module_notify,
  3322. .priority = 1, /* higher than trace.c module notify */
  3323. };
  3324. #endif /* CONFIG_MODULES */
  3325. /* Create a new event directory structure for a trace directory. */
  3326. static void
  3327. __trace_add_event_dirs(struct trace_array *tr)
  3328. {
  3329. struct trace_event_call *call;
  3330. int ret;
  3331. lockdep_assert_held(&trace_event_sem);
  3332. list_for_each_entry(call, &ftrace_events, list) {
  3333. ret = __trace_add_new_event(call, tr);
  3334. if (ret < 0)
  3335. pr_warn("Could not create directory for event %s\n",
  3336. trace_event_name(call));
  3337. }
  3338. }
  3339. /* Returns any file that matches the system and event */
  3340. struct trace_event_file *
  3341. __find_event_file(struct trace_array *tr, const char *system, const char *event)
  3342. {
  3343. struct trace_event_file *file;
  3344. struct trace_event_call *call;
  3345. const char *name;
  3346. list_for_each_entry(file, &tr->events, list) {
  3347. call = file->event_call;
  3348. name = trace_event_name(call);
  3349. if (!name || !call->class)
  3350. continue;
  3351. if (strcmp(event, name) == 0 &&
  3352. strcmp(system, call->class->system) == 0)
  3353. return file;
  3354. }
  3355. return NULL;
  3356. }
  3357. /* Returns valid trace event files that match system and event */
  3358. struct trace_event_file *
  3359. find_event_file(struct trace_array *tr, const char *system, const char *event)
  3360. {
  3361. struct trace_event_file *file;
  3362. file = __find_event_file(tr, system, event);
  3363. if (!file || !file->event_call->class->reg ||
  3364. file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
  3365. return NULL;
  3366. return file;
  3367. }
  3368. /**
  3369. * trace_get_event_file - Find and return a trace event file
  3370. * @instance: The name of the trace instance containing the event
  3371. * @system: The name of the system containing the event
  3372. * @event: The name of the event
  3373. *
  3374. * Return a trace event file given the trace instance name, trace
  3375. * system, and trace event name. If the instance name is NULL, it
  3376. * refers to the top-level trace array.
  3377. *
  3378. * This function will look it up and return it if found, after calling
  3379. * trace_array_get() to prevent the instance from going away, and
  3380. * increment the event's module refcount to prevent it from being
  3381. * removed.
  3382. *
  3383. * To release the file, call trace_put_event_file(), which will call
  3384. * trace_array_put() and decrement the event's module refcount.
  3385. *
  3386. * Return: The trace event on success, ERR_PTR otherwise.
  3387. */
  3388. struct trace_event_file *trace_get_event_file(const char *instance,
  3389. const char *system,
  3390. const char *event)
  3391. {
  3392. struct trace_array *tr = top_trace_array();
  3393. struct trace_event_file *file = NULL;
  3394. int ret = -EINVAL;
  3395. if (instance) {
  3396. tr = trace_array_find_get(instance);
  3397. if (!tr)
  3398. return ERR_PTR(-ENOENT);
  3399. } else {
  3400. ret = trace_array_get(tr);
  3401. if (ret)
  3402. return ERR_PTR(ret);
  3403. }
  3404. guard(mutex)(&event_mutex);
  3405. file = find_event_file(tr, system, event);
  3406. if (!file) {
  3407. trace_array_put(tr);
  3408. return ERR_PTR(-EINVAL);
  3409. }
  3410. /* Don't let event modules unload while in use */
  3411. ret = trace_event_try_get_ref(file->event_call);
  3412. if (!ret) {
  3413. trace_array_put(tr);
  3414. return ERR_PTR(-EBUSY);
  3415. }
  3416. return file;
  3417. }
  3418. EXPORT_SYMBOL_GPL(trace_get_event_file);
  3419. /**
  3420. * trace_put_event_file - Release a file from trace_get_event_file()
  3421. * @file: The trace event file
  3422. *
  3423. * If a file was retrieved using trace_get_event_file(), this should
  3424. * be called when it's no longer needed. It will cancel the previous
  3425. * trace_array_get() called by that function, and decrement the
  3426. * event's module refcount.
  3427. */
  3428. void trace_put_event_file(struct trace_event_file *file)
  3429. {
  3430. mutex_lock(&event_mutex);
  3431. trace_event_put_ref(file->event_call);
  3432. mutex_unlock(&event_mutex);
  3433. trace_array_put(file->tr);
  3434. }
  3435. EXPORT_SYMBOL_GPL(trace_put_event_file);
  3436. #ifdef CONFIG_DYNAMIC_FTRACE
  3437. struct event_probe_data {
  3438. struct trace_event_file *file;
  3439. unsigned long count;
  3440. int ref;
  3441. bool enable;
  3442. };
  3443. static void update_event_probe(struct event_probe_data *data)
  3444. {
  3445. if (data->enable)
  3446. clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
  3447. else
  3448. set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
  3449. }
  3450. static void
  3451. event_enable_probe(unsigned long ip, unsigned long parent_ip,
  3452. struct trace_array *tr, struct ftrace_probe_ops *ops,
  3453. void *data)
  3454. {
  3455. struct ftrace_func_mapper *mapper = data;
  3456. struct event_probe_data *edata;
  3457. void **pdata;
  3458. pdata = ftrace_func_mapper_find_ip(mapper, ip);
  3459. if (!pdata || !*pdata)
  3460. return;
  3461. edata = *pdata;
  3462. update_event_probe(edata);
  3463. }
  3464. static void
  3465. event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
  3466. struct trace_array *tr, struct ftrace_probe_ops *ops,
  3467. void *data)
  3468. {
  3469. struct ftrace_func_mapper *mapper = data;
  3470. struct event_probe_data *edata;
  3471. void **pdata;
  3472. pdata = ftrace_func_mapper_find_ip(mapper, ip);
  3473. if (!pdata || !*pdata)
  3474. return;
  3475. edata = *pdata;
  3476. if (!edata->count)
  3477. return;
  3478. /* Skip if the event is in a state we want to switch to */
  3479. if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
  3480. return;
  3481. if (edata->count != -1)
  3482. (edata->count)--;
  3483. update_event_probe(edata);
  3484. }
  3485. static int
  3486. event_enable_print(struct seq_file *m, unsigned long ip,
  3487. struct ftrace_probe_ops *ops, void *data)
  3488. {
  3489. struct ftrace_func_mapper *mapper = data;
  3490. struct event_probe_data *edata;
  3491. void **pdata;
  3492. pdata = ftrace_func_mapper_find_ip(mapper, ip);
  3493. if (WARN_ON_ONCE(!pdata || !*pdata))
  3494. return 0;
  3495. edata = *pdata;
  3496. seq_printf(m, "%ps:", (void *)ip);
  3497. seq_printf(m, "%s:%s:%s",
  3498. edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
  3499. edata->file->event_call->class->system,
  3500. trace_event_name(edata->file->event_call));
  3501. if (edata->count == -1)
  3502. seq_puts(m, ":unlimited\n");
  3503. else
  3504. seq_printf(m, ":count=%ld\n", edata->count);
  3505. return 0;
  3506. }
  3507. static int
  3508. event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
  3509. unsigned long ip, void *init_data, void **data)
  3510. {
  3511. struct ftrace_func_mapper *mapper = *data;
  3512. struct event_probe_data *edata = init_data;
  3513. int ret;
  3514. if (!mapper) {
  3515. mapper = allocate_ftrace_func_mapper();
  3516. if (!mapper)
  3517. return -ENODEV;
  3518. *data = mapper;
  3519. }
  3520. ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
  3521. if (ret < 0)
  3522. return ret;
  3523. edata->ref++;
  3524. return 0;
  3525. }
  3526. static int free_probe_data(void *data)
  3527. {
  3528. struct event_probe_data *edata = data;
  3529. edata->ref--;
  3530. if (!edata->ref) {
  3531. /* Remove soft mode */
  3532. __ftrace_event_enable_disable(edata->file, 0, 1);
  3533. trace_event_put_ref(edata->file->event_call);
  3534. kfree(edata);
  3535. }
  3536. return 0;
  3537. }
  3538. static void
  3539. event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
  3540. unsigned long ip, void *data)
  3541. {
  3542. struct ftrace_func_mapper *mapper = data;
  3543. struct event_probe_data *edata;
  3544. if (!ip) {
  3545. if (!mapper)
  3546. return;
  3547. free_ftrace_func_mapper(mapper, free_probe_data);
  3548. return;
  3549. }
  3550. edata = ftrace_func_mapper_remove_ip(mapper, ip);
  3551. if (WARN_ON_ONCE(!edata))
  3552. return;
  3553. if (WARN_ON_ONCE(edata->ref <= 0))
  3554. return;
  3555. free_probe_data(edata);
  3556. }
  3557. static struct ftrace_probe_ops event_enable_probe_ops = {
  3558. .func = event_enable_probe,
  3559. .print = event_enable_print,
  3560. .init = event_enable_init,
  3561. .free = event_enable_free,
  3562. };
  3563. static struct ftrace_probe_ops event_enable_count_probe_ops = {
  3564. .func = event_enable_count_probe,
  3565. .print = event_enable_print,
  3566. .init = event_enable_init,
  3567. .free = event_enable_free,
  3568. };
  3569. static struct ftrace_probe_ops event_disable_probe_ops = {
  3570. .func = event_enable_probe,
  3571. .print = event_enable_print,
  3572. .init = event_enable_init,
  3573. .free = event_enable_free,
  3574. };
  3575. static struct ftrace_probe_ops event_disable_count_probe_ops = {
  3576. .func = event_enable_count_probe,
  3577. .print = event_enable_print,
  3578. .init = event_enable_init,
  3579. .free = event_enable_free,
  3580. };
  3581. static int
  3582. event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
  3583. char *glob, char *cmd, char *param, int enabled)
  3584. {
  3585. struct trace_event_file *file;
  3586. struct ftrace_probe_ops *ops;
  3587. struct event_probe_data *data;
  3588. unsigned long count = -1;
  3589. const char *system;
  3590. const char *event;
  3591. char *number;
  3592. bool enable;
  3593. int ret;
  3594. if (!tr)
  3595. return -ENODEV;
  3596. /* hash funcs only work with set_ftrace_filter */
  3597. if (!enabled || !param)
  3598. return -EINVAL;
  3599. system = strsep(&param, ":");
  3600. if (!param)
  3601. return -EINVAL;
  3602. event = strsep(&param, ":");
  3603. guard(mutex)(&event_mutex);
  3604. file = find_event_file(tr, system, event);
  3605. if (!file)
  3606. return -EINVAL;
  3607. enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
  3608. if (enable)
  3609. ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
  3610. else
  3611. ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
  3612. if (glob[0] == '!')
  3613. return unregister_ftrace_function_probe_func(glob+1, tr, ops);
  3614. if (param) {
  3615. number = strsep(&param, ":");
  3616. if (!strlen(number))
  3617. return -EINVAL;
  3618. /*
  3619. * We use the callback data field (which is a pointer)
  3620. * as our counter.
  3621. */
  3622. ret = kstrtoul(number, 0, &count);
  3623. if (ret)
  3624. return ret;
  3625. }
  3626. /* Don't let event modules unload while probe registered */
  3627. ret = trace_event_try_get_ref(file->event_call);
  3628. if (!ret)
  3629. return -EBUSY;
  3630. ret = __ftrace_event_enable_disable(file, 1, 1);
  3631. if (ret < 0)
  3632. goto out_put;
  3633. ret = -ENOMEM;
  3634. data = kzalloc_obj(*data);
  3635. if (!data)
  3636. goto out_put;
  3637. data->enable = enable;
  3638. data->count = count;
  3639. data->file = file;
  3640. ret = register_ftrace_function_probe(glob, tr, ops, data);
  3641. /*
  3642. * The above returns on success the # of functions enabled,
  3643. * but if it didn't find any functions it returns zero.
  3644. * Consider no functions a failure too.
  3645. */
  3646. /* Just return zero, not the number of enabled functions */
  3647. if (ret > 0)
  3648. return 0;
  3649. kfree(data);
  3650. if (!ret)
  3651. ret = -ENOENT;
  3652. __ftrace_event_enable_disable(file, 0, 1);
  3653. out_put:
  3654. trace_event_put_ref(file->event_call);
  3655. return ret;
  3656. }
  3657. static struct ftrace_func_command event_enable_cmd = {
  3658. .name = ENABLE_EVENT_STR,
  3659. .func = event_enable_func,
  3660. };
  3661. static struct ftrace_func_command event_disable_cmd = {
  3662. .name = DISABLE_EVENT_STR,
  3663. .func = event_enable_func,
  3664. };
  3665. static __init int register_event_cmds(void)
  3666. {
  3667. int ret;
  3668. ret = register_ftrace_command(&event_enable_cmd);
  3669. if (WARN_ON(ret < 0))
  3670. return ret;
  3671. ret = register_ftrace_command(&event_disable_cmd);
  3672. if (WARN_ON(ret < 0))
  3673. unregister_ftrace_command(&event_enable_cmd);
  3674. return ret;
  3675. }
  3676. #else
  3677. static inline int register_event_cmds(void) { return 0; }
  3678. #endif /* CONFIG_DYNAMIC_FTRACE */
  3679. /*
  3680. * The top level array and trace arrays created by boot-time tracing
  3681. * have already had its trace_event_file descriptors created in order
  3682. * to allow for early events to be recorded.
  3683. * This function is called after the tracefs has been initialized,
  3684. * and we now have to create the files associated to the events.
  3685. */
  3686. static void __trace_early_add_event_dirs(struct trace_array *tr)
  3687. {
  3688. struct trace_event_file *file;
  3689. int ret;
  3690. list_for_each_entry(file, &tr->events, list) {
  3691. ret = event_create_dir(tr->event_dir, file);
  3692. if (ret < 0)
  3693. pr_warn("Could not create directory for event %s\n",
  3694. trace_event_name(file->event_call));
  3695. }
  3696. }
  3697. /*
  3698. * For early boot up, the top trace array and the trace arrays created
  3699. * by boot-time tracing require to have a list of events that can be
  3700. * enabled. This must be done before the filesystem is set up in order
  3701. * to allow events to be traced early.
  3702. */
  3703. void __trace_early_add_events(struct trace_array *tr)
  3704. {
  3705. struct trace_event_call *call;
  3706. int ret;
  3707. list_for_each_entry(call, &ftrace_events, list) {
  3708. /* Early boot up should not have any modules loaded */
  3709. if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) &&
  3710. WARN_ON_ONCE(call->module))
  3711. continue;
  3712. ret = __trace_early_add_new_event(call, tr);
  3713. if (ret < 0)
  3714. pr_warn("Could not create early event %s\n",
  3715. trace_event_name(call));
  3716. }
  3717. }
  3718. /* Remove the event directory structure for a trace directory. */
  3719. static void
  3720. __trace_remove_event_dirs(struct trace_array *tr)
  3721. {
  3722. struct trace_event_file *file, *next;
  3723. list_for_each_entry_safe(file, next, &tr->events, list)
  3724. remove_event_file_dir(file);
  3725. }
  3726. static void __add_event_to_tracers(struct trace_event_call *call)
  3727. {
  3728. struct trace_array *tr;
  3729. list_for_each_entry(tr, &ftrace_trace_arrays, list)
  3730. __trace_add_new_event(call, tr);
  3731. }
  3732. extern struct trace_event_call *__start_ftrace_events[];
  3733. extern struct trace_event_call *__stop_ftrace_events[];
  3734. static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
  3735. static __init int setup_trace_event(char *str)
  3736. {
  3737. if (bootup_event_buf[0] != '\0')
  3738. strlcat(bootup_event_buf, ",", COMMAND_LINE_SIZE);
  3739. strlcat(bootup_event_buf, str, COMMAND_LINE_SIZE);
  3740. trace_set_ring_buffer_expanded(NULL);
  3741. disable_tracing_selftest("running event tracing");
  3742. return 1;
  3743. }
  3744. __setup("trace_event=", setup_trace_event);
  3745. static int events_callback(const char *name, umode_t *mode, void **data,
  3746. const struct file_operations **fops)
  3747. {
  3748. if (strcmp(name, "enable") == 0) {
  3749. *mode = TRACE_MODE_WRITE;
  3750. *fops = &ftrace_tr_enable_fops;
  3751. return 1;
  3752. }
  3753. if (strcmp(name, "header_page") == 0) {
  3754. *mode = TRACE_MODE_READ;
  3755. *fops = &ftrace_show_header_page_fops;
  3756. } else if (strcmp(name, "header_event") == 0) {
  3757. *mode = TRACE_MODE_READ;
  3758. *fops = &ftrace_show_header_event_fops;
  3759. } else
  3760. return 0;
  3761. return 1;
  3762. }
  3763. /* Expects to have event_mutex held when called */
  3764. static int
  3765. create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
  3766. {
  3767. struct eventfs_inode *e_events;
  3768. struct dentry *entry;
  3769. int nr_entries;
  3770. static struct eventfs_entry events_entries[] = {
  3771. {
  3772. .name = "enable",
  3773. .callback = events_callback,
  3774. },
  3775. {
  3776. .name = "header_page",
  3777. .callback = events_callback,
  3778. },
  3779. {
  3780. .name = "header_event",
  3781. .callback = events_callback,
  3782. },
  3783. };
  3784. entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent,
  3785. tr, &ftrace_set_event_fops);
  3786. if (!entry)
  3787. return -ENOMEM;
  3788. trace_create_file("show_event_filters", TRACE_MODE_READ, parent, tr,
  3789. &ftrace_show_event_filters_fops);
  3790. trace_create_file("show_event_triggers", TRACE_MODE_READ, parent, tr,
  3791. &ftrace_show_event_triggers_fops);
  3792. nr_entries = ARRAY_SIZE(events_entries);
  3793. e_events = eventfs_create_events_dir("events", parent, events_entries,
  3794. nr_entries, tr);
  3795. if (IS_ERR(e_events)) {
  3796. pr_warn("Could not create tracefs 'events' directory\n");
  3797. return -ENOMEM;
  3798. }
  3799. /* There are not as crucial, just warn if they are not created */
  3800. trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent,
  3801. tr, &ftrace_set_event_pid_fops);
  3802. trace_create_file("set_event_notrace_pid",
  3803. TRACE_MODE_WRITE, parent, tr,
  3804. &ftrace_set_event_notrace_pid_fops);
  3805. tr->event_dir = e_events;
  3806. return 0;
  3807. }
  3808. /**
  3809. * event_trace_add_tracer - add a instance of a trace_array to events
  3810. * @parent: The parent dentry to place the files/directories for events in
  3811. * @tr: The trace array associated with these events
  3812. *
  3813. * When a new instance is created, it needs to set up its events
  3814. * directory, as well as other files associated with events. It also
  3815. * creates the event hierarchy in the @parent/events directory.
  3816. *
  3817. * Returns 0 on success.
  3818. *
  3819. * Must be called with event_mutex held.
  3820. */
  3821. int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
  3822. {
  3823. int ret;
  3824. lockdep_assert_held(&event_mutex);
  3825. ret = create_event_toplevel_files(parent, tr);
  3826. if (ret)
  3827. goto out;
  3828. down_write(&trace_event_sem);
  3829. /* If tr already has the event list, it is initialized in early boot. */
  3830. if (unlikely(!list_empty(&tr->events)))
  3831. __trace_early_add_event_dirs(tr);
  3832. else
  3833. __trace_add_event_dirs(tr);
  3834. up_write(&trace_event_sem);
  3835. out:
  3836. return ret;
  3837. }
  3838. /*
  3839. * The top trace array already had its file descriptors created.
  3840. * Now the files themselves need to be created.
  3841. */
  3842. static __init int
  3843. early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
  3844. {
  3845. int ret;
  3846. guard(mutex)(&event_mutex);
  3847. ret = create_event_toplevel_files(parent, tr);
  3848. if (ret)
  3849. return ret;
  3850. down_write(&trace_event_sem);
  3851. __trace_early_add_event_dirs(tr);
  3852. up_write(&trace_event_sem);
  3853. return 0;
  3854. }
  3855. /* Must be called with event_mutex held */
  3856. int event_trace_del_tracer(struct trace_array *tr)
  3857. {
  3858. lockdep_assert_held(&event_mutex);
  3859. /* Disable any event triggers and associated soft-disabled events */
  3860. clear_event_triggers(tr);
  3861. /* Clear the pid list */
  3862. __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
  3863. /* Disable any running events */
  3864. __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0, NULL);
  3865. /* Make sure no more events are being executed */
  3866. tracepoint_synchronize_unregister();
  3867. down_write(&trace_event_sem);
  3868. __trace_remove_event_dirs(tr);
  3869. eventfs_remove_events_dir(tr->event_dir);
  3870. up_write(&trace_event_sem);
  3871. tr->event_dir = NULL;
  3872. return 0;
  3873. }
  3874. static __init int event_trace_memsetup(void)
  3875. {
  3876. field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
  3877. file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
  3878. return 0;
  3879. }
  3880. /*
  3881. * Helper function to enable or disable a comma-separated list of events
  3882. * from the bootup buffer.
  3883. */
  3884. static __init void __early_set_events(struct trace_array *tr, char *buf, bool enable)
  3885. {
  3886. char *token;
  3887. while ((token = strsep(&buf, ","))) {
  3888. if (*token) {
  3889. if (enable) {
  3890. if (ftrace_set_clr_event(tr, token, 1))
  3891. pr_warn("Failed to enable trace event: %s\n", token);
  3892. } else {
  3893. ftrace_set_clr_event(tr, token, 0);
  3894. }
  3895. }
  3896. /* Put back the comma to allow this to be called again */
  3897. if (buf)
  3898. *(buf - 1) = ',';
  3899. }
  3900. }
  3901. /**
  3902. * early_enable_events - enable events from the bootup buffer
  3903. * @tr: The trace array to enable the events in
  3904. * @buf: The buffer containing the comma separated list of events
  3905. * @disable_first: If true, disable all events in @buf before enabling them
  3906. *
  3907. * This function enables events from the bootup buffer. If @disable_first
  3908. * is true, it will first disable all events in the buffer before enabling
  3909. * them.
  3910. *
  3911. * For syscall events, which rely on a global refcount to register the
  3912. * SYSCALL_WORK_SYSCALL_TRACEPOINT flag (especially for pid 1), we must
  3913. * ensure the refcount hits zero before re-enabling them. A simple
  3914. * "disable then enable" per-event is not enough if multiple syscalls are
  3915. * used, as the refcount will stay above zero. Thus, we need a two-phase
  3916. * approach: disable all, then enable all.
  3917. */
  3918. __init void
  3919. early_enable_events(struct trace_array *tr, char *buf, bool disable_first)
  3920. {
  3921. if (disable_first)
  3922. __early_set_events(tr, buf, false);
  3923. __early_set_events(tr, buf, true);
  3924. }
  3925. static __init int event_trace_enable(void)
  3926. {
  3927. struct trace_array *tr = top_trace_array();
  3928. struct trace_event_call **iter, *call;
  3929. int ret;
  3930. if (!tr)
  3931. return -ENODEV;
  3932. for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
  3933. call = *iter;
  3934. ret = event_init(call);
  3935. if (!ret)
  3936. list_add(&call->list, &ftrace_events);
  3937. }
  3938. register_trigger_cmds();
  3939. /*
  3940. * We need the top trace array to have a working set of trace
  3941. * points at early init, before the debug files and directories
  3942. * are created. Create the file entries now, and attach them
  3943. * to the actual file dentries later.
  3944. */
  3945. __trace_early_add_events(tr);
  3946. early_enable_events(tr, bootup_event_buf, false);
  3947. trace_printk_start_comm();
  3948. register_event_cmds();
  3949. return 0;
  3950. }
  3951. /*
  3952. * event_trace_enable() is called from trace_event_init() first to
  3953. * initialize events and perhaps start any events that are on the
  3954. * command line. Unfortunately, there are some events that will not
  3955. * start this early, like the system call tracepoints that need
  3956. * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
  3957. * event_trace_enable() is called before pid 1 starts, and this flag
  3958. * is never set, making the syscall tracepoint never get reached, but
  3959. * the event is enabled regardless (and not doing anything).
  3960. */
  3961. static __init int event_trace_enable_again(void)
  3962. {
  3963. struct trace_array *tr;
  3964. tr = top_trace_array();
  3965. if (!tr)
  3966. return -ENODEV;
  3967. early_enable_events(tr, bootup_event_buf, true);
  3968. return 0;
  3969. }
  3970. early_initcall(event_trace_enable_again);
  3971. /* Init fields which doesn't related to the tracefs */
  3972. static __init int event_trace_init_fields(void)
  3973. {
  3974. if (trace_define_generic_fields())
  3975. pr_warn("tracing: Failed to allocated generic fields");
  3976. if (trace_define_common_fields())
  3977. pr_warn("tracing: Failed to allocate common fields");
  3978. return 0;
  3979. }
  3980. __init int event_trace_init(void)
  3981. {
  3982. struct trace_array *tr;
  3983. int ret;
  3984. tr = top_trace_array();
  3985. if (!tr)
  3986. return -ENODEV;
  3987. trace_create_file("available_events", TRACE_MODE_READ,
  3988. NULL, tr, &ftrace_avail_fops);
  3989. ret = early_event_add_tracer(NULL, tr);
  3990. if (ret)
  3991. return ret;
  3992. #ifdef CONFIG_MODULES
  3993. ret = register_module_notifier(&trace_module_nb);
  3994. if (ret)
  3995. pr_warn("Failed to register trace events module notifier\n");
  3996. #endif
  3997. eventdir_initialized = true;
  3998. return 0;
  3999. }
  4000. void __init trace_event_init(void)
  4001. {
  4002. event_trace_memsetup();
  4003. init_ftrace_syscalls();
  4004. event_trace_enable();
  4005. event_trace_init_fields();
  4006. }
  4007. #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
  4008. static DEFINE_SPINLOCK(test_spinlock);
  4009. static DEFINE_SPINLOCK(test_spinlock_irq);
  4010. static DEFINE_MUTEX(test_mutex);
  4011. static __init void test_work(struct work_struct *dummy)
  4012. {
  4013. spin_lock(&test_spinlock);
  4014. spin_lock_irq(&test_spinlock_irq);
  4015. udelay(1);
  4016. spin_unlock_irq(&test_spinlock_irq);
  4017. spin_unlock(&test_spinlock);
  4018. mutex_lock(&test_mutex);
  4019. msleep(1);
  4020. mutex_unlock(&test_mutex);
  4021. }
  4022. static __init int event_test_thread(void *unused)
  4023. {
  4024. void *test_malloc;
  4025. test_malloc = kmalloc(1234, GFP_KERNEL);
  4026. if (!test_malloc)
  4027. pr_info("failed to kmalloc\n");
  4028. schedule_on_each_cpu(test_work);
  4029. kfree(test_malloc);
  4030. set_current_state(TASK_INTERRUPTIBLE);
  4031. while (!kthread_should_stop()) {
  4032. schedule();
  4033. set_current_state(TASK_INTERRUPTIBLE);
  4034. }
  4035. __set_current_state(TASK_RUNNING);
  4036. return 0;
  4037. }
  4038. /*
  4039. * Do various things that may trigger events.
  4040. */
  4041. static __init void event_test_stuff(void)
  4042. {
  4043. struct task_struct *test_thread;
  4044. test_thread = kthread_run(event_test_thread, NULL, "test-events");
  4045. msleep(1);
  4046. kthread_stop(test_thread);
  4047. }
  4048. /*
  4049. * For every trace event defined, we will test each trace point separately,
  4050. * and then by groups, and finally all trace points.
  4051. */
  4052. static __init void event_trace_self_tests(void)
  4053. {
  4054. struct trace_subsystem_dir *dir;
  4055. struct trace_event_file *file;
  4056. struct trace_event_call *call;
  4057. struct event_subsystem *system;
  4058. struct trace_array *tr;
  4059. int ret;
  4060. tr = top_trace_array();
  4061. if (!tr)
  4062. return;
  4063. pr_info("Running tests on trace events:\n");
  4064. list_for_each_entry(file, &tr->events, list) {
  4065. call = file->event_call;
  4066. /* Only test those that have a probe */
  4067. if (!call->class || !call->class->probe)
  4068. continue;
  4069. /*
  4070. * Testing syscall events here is pretty useless, but
  4071. * we still do it if configured. But this is time consuming.
  4072. * What we really need is a user thread to perform the
  4073. * syscalls as we test.
  4074. */
  4075. #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
  4076. if (call->class->system &&
  4077. strcmp(call->class->system, "syscalls") == 0)
  4078. continue;
  4079. #endif
  4080. pr_info("Testing event %s: ", trace_event_name(call));
  4081. /*
  4082. * If an event is already enabled, someone is using
  4083. * it and the self test should not be on.
  4084. */
  4085. if (file->flags & EVENT_FILE_FL_ENABLED) {
  4086. pr_warn("Enabled event during self test!\n");
  4087. WARN_ON_ONCE(1);
  4088. continue;
  4089. }
  4090. ftrace_event_enable_disable(file, 1);
  4091. event_test_stuff();
  4092. ftrace_event_enable_disable(file, 0);
  4093. pr_cont("OK\n");
  4094. }
  4095. /* Now test at the sub system level */
  4096. pr_info("Running tests on trace event systems:\n");
  4097. list_for_each_entry(dir, &tr->systems, list) {
  4098. system = dir->subsystem;
  4099. /* the ftrace system is special, skip it */
  4100. if (strcmp(system->name, "ftrace") == 0)
  4101. continue;
  4102. pr_info("Testing event system %s: ", system->name);
  4103. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1, NULL);
  4104. if (WARN_ON_ONCE(ret)) {
  4105. pr_warn("error enabling system %s\n",
  4106. system->name);
  4107. continue;
  4108. }
  4109. event_test_stuff();
  4110. ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0, NULL);
  4111. if (WARN_ON_ONCE(ret)) {
  4112. pr_warn("error disabling system %s\n",
  4113. system->name);
  4114. continue;
  4115. }
  4116. pr_cont("OK\n");
  4117. }
  4118. /* Test with all events enabled */
  4119. pr_info("Running tests on all trace events:\n");
  4120. pr_info("Testing all events: ");
  4121. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1, NULL);
  4122. if (WARN_ON_ONCE(ret)) {
  4123. pr_warn("error enabling all events\n");
  4124. return;
  4125. }
  4126. event_test_stuff();
  4127. /* reset sysname */
  4128. ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0, NULL);
  4129. if (WARN_ON_ONCE(ret)) {
  4130. pr_warn("error disabling all events\n");
  4131. return;
  4132. }
  4133. pr_cont("OK\n");
  4134. }
  4135. #ifdef CONFIG_FUNCTION_TRACER
  4136. static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
  4137. static struct trace_event_file event_trace_file __initdata;
  4138. static void __init
  4139. function_test_events_call(unsigned long ip, unsigned long parent_ip,
  4140. struct ftrace_ops *op, struct ftrace_regs *regs)
  4141. {
  4142. struct trace_buffer *buffer;
  4143. struct ring_buffer_event *event;
  4144. struct ftrace_entry *entry;
  4145. unsigned int trace_ctx;
  4146. long disabled;
  4147. int cpu;
  4148. trace_ctx = tracing_gen_ctx();
  4149. preempt_disable_notrace();
  4150. cpu = raw_smp_processor_id();
  4151. disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
  4152. if (disabled != 1)
  4153. goto out;
  4154. event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
  4155. TRACE_FN, sizeof(*entry),
  4156. trace_ctx);
  4157. if (!event)
  4158. goto out;
  4159. entry = ring_buffer_event_data(event);
  4160. entry->ip = ip;
  4161. entry->parent_ip = parent_ip;
  4162. event_trigger_unlock_commit(&event_trace_file, buffer, event,
  4163. entry, trace_ctx);
  4164. out:
  4165. atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
  4166. preempt_enable_notrace();
  4167. }
  4168. static struct ftrace_ops trace_ops __initdata =
  4169. {
  4170. .func = function_test_events_call,
  4171. };
  4172. static __init void event_trace_self_test_with_function(void)
  4173. {
  4174. int ret;
  4175. event_trace_file.tr = top_trace_array();
  4176. if (WARN_ON(!event_trace_file.tr))
  4177. return;
  4178. ret = register_ftrace_function(&trace_ops);
  4179. if (WARN_ON(ret < 0)) {
  4180. pr_info("Failed to enable function tracer for event tests\n");
  4181. return;
  4182. }
  4183. pr_info("Running tests again, along with the function tracer\n");
  4184. event_trace_self_tests();
  4185. unregister_ftrace_function(&trace_ops);
  4186. }
  4187. #else
  4188. static __init void event_trace_self_test_with_function(void)
  4189. {
  4190. }
  4191. #endif
  4192. static __init int event_trace_self_tests_init(void)
  4193. {
  4194. if (!tracing_selftest_disabled) {
  4195. event_trace_self_tests();
  4196. event_trace_self_test_with_function();
  4197. }
  4198. return 0;
  4199. }
  4200. late_initcall(event_trace_self_tests_init);
  4201. #endif